2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 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)
1082 declarator = declarator->declarator;
1090 cp_parameter_declarator *no_parameters;
1092 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1093 DECLARATOR and DEFAULT_ARGUMENT. */
1095 cp_parameter_declarator *
1096 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1097 cp_declarator *declarator,
1098 tree default_argument)
1100 cp_parameter_declarator *parameter;
1102 parameter = ((cp_parameter_declarator *)
1103 alloc_declarator (sizeof (cp_parameter_declarator)));
1104 parameter->next = NULL;
1105 if (decl_specifiers)
1106 parameter->decl_specifiers = *decl_specifiers;
1108 clear_decl_specs (¶meter->decl_specifiers);
1109 parameter->declarator = declarator;
1110 parameter->default_argument = default_argument;
1111 parameter->ellipsis_p = false;
1116 /* Returns true iff DECLARATOR is a declaration for a function. */
1119 function_declarator_p (const cp_declarator *declarator)
1123 if (declarator->kind == cdk_function
1124 && declarator->declarator->kind == cdk_id)
1126 if (declarator->kind == cdk_id
1127 || declarator->kind == cdk_error)
1129 declarator = declarator->declarator;
1139 A cp_parser parses the token stream as specified by the C++
1140 grammar. Its job is purely parsing, not semantic analysis. For
1141 example, the parser breaks the token stream into declarators,
1142 expressions, statements, and other similar syntactic constructs.
1143 It does not check that the types of the expressions on either side
1144 of an assignment-statement are compatible, or that a function is
1145 not declared with a parameter of type `void'.
1147 The parser invokes routines elsewhere in the compiler to perform
1148 semantic analysis and to build up the abstract syntax tree for the
1151 The parser (and the template instantiation code, which is, in a
1152 way, a close relative of parsing) are the only parts of the
1153 compiler that should be calling push_scope and pop_scope, or
1154 related functions. The parser (and template instantiation code)
1155 keeps track of what scope is presently active; everything else
1156 should simply honor that. (The code that generates static
1157 initializers may also need to set the scope, in order to check
1158 access control correctly when emitting the initializers.)
1163 The parser is of the standard recursive-descent variety. Upcoming
1164 tokens in the token stream are examined in order to determine which
1165 production to use when parsing a non-terminal. Some C++ constructs
1166 require arbitrary look ahead to disambiguate. For example, it is
1167 impossible, in the general case, to tell whether a statement is an
1168 expression or declaration without scanning the entire statement.
1169 Therefore, the parser is capable of "parsing tentatively." When the
1170 parser is not sure what construct comes next, it enters this mode.
1171 Then, while we attempt to parse the construct, the parser queues up
1172 error messages, rather than issuing them immediately, and saves the
1173 tokens it consumes. If the construct is parsed successfully, the
1174 parser "commits", i.e., it issues any queued error messages and
1175 the tokens that were being preserved are permanently discarded.
1176 If, however, the construct is not parsed successfully, the parser
1177 rolls back its state completely so that it can resume parsing using
1178 a different alternative.
1183 The performance of the parser could probably be improved substantially.
1184 We could often eliminate the need to parse tentatively by looking ahead
1185 a little bit. In some places, this approach might not entirely eliminate
1186 the need to parse tentatively, but it might still speed up the average
1189 /* Flags that are passed to some parsing functions. These values can
1190 be bitwise-ored together. */
1192 typedef enum cp_parser_flags
1195 CP_PARSER_FLAGS_NONE = 0x0,
1196 /* The construct is optional. If it is not present, then no error
1197 should be issued. */
1198 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1199 /* When parsing a type-specifier, do not allow user-defined types. */
1200 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1203 /* The different kinds of declarators we want to parse. */
1205 typedef enum cp_parser_declarator_kind
1207 /* We want an abstract declarator. */
1208 CP_PARSER_DECLARATOR_ABSTRACT,
1209 /* We want a named declarator. */
1210 CP_PARSER_DECLARATOR_NAMED,
1211 /* We don't mind, but the name must be an unqualified-id. */
1212 CP_PARSER_DECLARATOR_EITHER
1213 } cp_parser_declarator_kind;
1215 /* The precedence values used to parse binary expressions. The minimum value
1216 of PREC must be 1, because zero is reserved to quickly discriminate
1217 binary operators from other tokens. */
1222 PREC_LOGICAL_OR_EXPRESSION,
1223 PREC_LOGICAL_AND_EXPRESSION,
1224 PREC_INCLUSIVE_OR_EXPRESSION,
1225 PREC_EXCLUSIVE_OR_EXPRESSION,
1226 PREC_AND_EXPRESSION,
1227 PREC_EQUALITY_EXPRESSION,
1228 PREC_RELATIONAL_EXPRESSION,
1229 PREC_SHIFT_EXPRESSION,
1230 PREC_ADDITIVE_EXPRESSION,
1231 PREC_MULTIPLICATIVE_EXPRESSION,
1233 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1236 /* A mapping from a token type to a corresponding tree node type, with a
1237 precedence value. */
1239 typedef struct cp_parser_binary_operations_map_node
1241 /* The token type. */
1242 enum cpp_ttype token_type;
1243 /* The corresponding tree code. */
1244 enum tree_code tree_type;
1245 /* The precedence of this operator. */
1246 enum cp_parser_prec prec;
1247 } cp_parser_binary_operations_map_node;
1249 /* The status of a tentative parse. */
1251 typedef enum cp_parser_status_kind
1253 /* No errors have occurred. */
1254 CP_PARSER_STATUS_KIND_NO_ERROR,
1255 /* An error has occurred. */
1256 CP_PARSER_STATUS_KIND_ERROR,
1257 /* We are committed to this tentative parse, whether or not an error
1259 CP_PARSER_STATUS_KIND_COMMITTED
1260 } cp_parser_status_kind;
1262 typedef struct cp_parser_expression_stack_entry
1264 /* Left hand side of the binary operation we are currently
1267 /* Original tree code for left hand side, if it was a binary
1268 expression itself (used for -Wparentheses). */
1269 enum tree_code lhs_type;
1270 /* Tree code for the binary operation we are parsing. */
1271 enum tree_code tree_type;
1272 /* Precedence of the binary operation we are parsing. */
1274 } cp_parser_expression_stack_entry;
1276 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1277 entries because precedence levels on the stack are monotonically
1279 typedef struct cp_parser_expression_stack_entry
1280 cp_parser_expression_stack[NUM_PREC_VALUES];
1282 /* Context that is saved and restored when parsing tentatively. */
1283 typedef struct cp_parser_context GTY (())
1285 /* If this is a tentative parsing context, the status of the
1287 enum cp_parser_status_kind status;
1288 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1289 that are looked up in this context must be looked up both in the
1290 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1291 the context of the containing expression. */
1294 /* The next parsing context in the stack. */
1295 struct cp_parser_context *next;
1296 } cp_parser_context;
1300 /* Constructors and destructors. */
1302 static cp_parser_context *cp_parser_context_new
1303 (cp_parser_context *);
1305 /* Class variables. */
1307 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309 /* The operator-precedence table used by cp_parser_binary_expression.
1310 Transformed into an associative array (binops_by_token) by
1313 static const cp_parser_binary_operations_map_node binops[] = {
1314 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1315 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1322 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1325 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1328 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1333 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1346 /* The same as binops, but initialized by cp_parser_new so that
1347 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1349 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351 /* Constructors and destructors. */
1353 /* Construct a new context. The context below this one on the stack
1354 is given by NEXT. */
1356 static cp_parser_context *
1357 cp_parser_context_new (cp_parser_context* next)
1359 cp_parser_context *context;
1361 /* Allocate the storage. */
1362 if (cp_parser_context_free_list != NULL)
1364 /* Pull the first entry from the free list. */
1365 context = cp_parser_context_free_list;
1366 cp_parser_context_free_list = context->next;
1367 memset (context, 0, sizeof (*context));
1370 context = GGC_CNEW (cp_parser_context);
1372 /* No errors have occurred yet in this context. */
1373 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1374 /* If this is not the bottomost context, copy information that we
1375 need from the previous context. */
1378 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1379 expression, then we are parsing one in this context, too. */
1380 context->object_type = next->object_type;
1381 /* Thread the stack. */
1382 context->next = next;
1388 /* The cp_parser structure represents the C++ parser. */
1390 typedef struct cp_parser GTY(())
1392 /* The lexer from which we are obtaining tokens. */
1395 /* The scope in which names should be looked up. If NULL_TREE, then
1396 we look up names in the scope that is currently open in the
1397 source program. If non-NULL, this is either a TYPE or
1398 NAMESPACE_DECL for the scope in which we should look. It can
1399 also be ERROR_MARK, when we've parsed a bogus scope.
1401 This value is not cleared automatically after a name is looked
1402 up, so we must be careful to clear it before starting a new look
1403 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1404 will look up `Z' in the scope of `X', rather than the current
1405 scope.) Unfortunately, it is difficult to tell when name lookup
1406 is complete, because we sometimes peek at a token, look it up,
1407 and then decide not to consume it. */
1410 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411 last lookup took place. OBJECT_SCOPE is used if an expression
1412 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413 respectively. QUALIFYING_SCOPE is used for an expression of the
1414 form "X::Y"; it refers to X. */
1416 tree qualifying_scope;
1418 /* A stack of parsing contexts. All but the bottom entry on the
1419 stack will be tentative contexts.
1421 We parse tentatively in order to determine which construct is in
1422 use in some situations. For example, in order to determine
1423 whether a statement is an expression-statement or a
1424 declaration-statement we parse it tentatively as a
1425 declaration-statement. If that fails, we then reparse the same
1426 token stream as an expression-statement. */
1427 cp_parser_context *context;
1429 /* True if we are parsing GNU C++. If this flag is not set, then
1430 GNU extensions are not recognized. */
1431 bool allow_gnu_extensions_p;
1433 /* TRUE if the `>' token should be interpreted as the greater-than
1434 operator. FALSE if it is the end of a template-id or
1435 template-parameter-list. In C++0x mode, this flag also applies to
1436 `>>' tokens, which are viewed as two consecutive `>' tokens when
1437 this flag is FALSE. */
1438 bool greater_than_is_operator_p;
1440 /* TRUE if default arguments are allowed within a parameter list
1441 that starts at this point. FALSE if only a gnu extension makes
1442 them permissible. */
1443 bool default_arg_ok_p;
1445 /* TRUE if we are parsing an integral constant-expression. See
1446 [expr.const] for a precise definition. */
1447 bool integral_constant_expression_p;
1449 /* TRUE if we are parsing an integral constant-expression -- but a
1450 non-constant expression should be permitted as well. This flag
1451 is used when parsing an array bound so that GNU variable-length
1452 arrays are tolerated. */
1453 bool allow_non_integral_constant_expression_p;
1455 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456 been seen that makes the expression non-constant. */
1457 bool non_integral_constant_expression_p;
1459 /* TRUE if local variable names and `this' are forbidden in the
1461 bool local_variables_forbidden_p;
1463 /* TRUE if the declaration we are parsing is part of a
1464 linkage-specification of the form `extern string-literal
1466 bool in_unbraced_linkage_specification_p;
1468 /* TRUE if we are presently parsing a declarator, after the
1469 direct-declarator. */
1470 bool in_declarator_p;
1472 /* TRUE if we are presently parsing a template-argument-list. */
1473 bool in_template_argument_list_p;
1475 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476 to IN_OMP_BLOCK if parsing OpenMP structured block and
1477 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1478 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479 iteration-statement, OpenMP block or loop within that switch. */
1480 #define IN_SWITCH_STMT 1
1481 #define IN_ITERATION_STMT 2
1482 #define IN_OMP_BLOCK 4
1483 #define IN_OMP_FOR 8
1484 #define IN_IF_STMT 16
1485 unsigned char in_statement;
1487 /* TRUE if we are presently parsing the body of a switch statement.
1488 Note that this doesn't quite overlap with in_statement above.
1489 The difference relates to giving the right sets of error messages:
1490 "case not in switch" vs "break statement used with OpenMP...". */
1491 bool in_switch_statement_p;
1493 /* TRUE if we are parsing a type-id in an expression context. In
1494 such a situation, both "type (expr)" and "type (type)" are valid
1496 bool in_type_id_in_expr_p;
1498 /* TRUE if we are currently in a header file where declarations are
1499 implicitly extern "C". */
1500 bool implicit_extern_c;
1502 /* TRUE if strings in expressions should be translated to the execution
1504 bool translate_strings_p;
1506 /* TRUE if we are presently parsing the body of a function, but not
1508 bool in_function_body;
1510 /* If non-NULL, then we are parsing a construct where new type
1511 definitions are not permitted. The string stored here will be
1512 issued as an error message if a type is defined. */
1513 const char *type_definition_forbidden_message;
1515 /* A list of lists. The outer list is a stack, used for member
1516 functions of local classes. At each level there are two sub-list,
1517 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519 TREE_VALUE's. The functions are chained in reverse declaration
1522 The TREE_PURPOSE sublist contains those functions with default
1523 arguments that need post processing, and the TREE_VALUE sublist
1524 contains those functions with definitions that need post
1527 These lists can only be processed once the outermost class being
1528 defined is complete. */
1529 tree unparsed_functions_queues;
1531 /* The number of classes whose definitions are currently in
1533 unsigned num_classes_being_defined;
1535 /* The number of template parameter lists that apply directly to the
1536 current declaration. */
1537 unsigned num_template_parameter_lists;
1542 /* Constructors and destructors. */
1544 static cp_parser *cp_parser_new
1547 /* Routines to parse various constructs.
1549 Those that return `tree' will return the error_mark_node (rather
1550 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551 Sometimes, they will return an ordinary node if error-recovery was
1552 attempted, even though a parse error occurred. So, to check
1553 whether or not a parse error occurred, you should always use
1554 cp_parser_error_occurred. If the construct is optional (indicated
1555 either by an `_opt' in the name of the function that does the
1556 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557 the construct is not present. */
1559 /* Lexical conventions [gram.lex] */
1561 static tree cp_parser_identifier
1563 static tree cp_parser_string_literal
1564 (cp_parser *, bool, bool);
1566 /* Basic concepts [gram.basic] */
1568 static bool cp_parser_translation_unit
1571 /* Expressions [gram.expr] */
1573 static tree cp_parser_primary_expression
1574 (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576 (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582 (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_class_or_namespace_name
1584 (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586 (cp_parser *, bool, bool, bool);
1587 static tree cp_parser_postfix_open_square_expression
1588 (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1591 static tree cp_parser_parenthesized_expression_list
1592 (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594 (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596 (cp_parser *, bool, bool);
1597 static enum tree_code cp_parser_unary_operator
1599 static tree cp_parser_new_expression
1601 static tree cp_parser_new_placement
1603 static tree cp_parser_new_type_id
1604 (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1607 static cp_declarator *cp_parser_direct_new_declarator
1609 static tree cp_parser_new_initializer
1611 static tree cp_parser_delete_expression
1613 static tree cp_parser_cast_expression
1614 (cp_parser *, bool, bool);
1615 static tree cp_parser_binary_expression
1616 (cp_parser *, bool);
1617 static tree cp_parser_question_colon_clause
1618 (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620 (cp_parser *, bool);
1621 static enum tree_code cp_parser_assignment_operator_opt
1623 static tree cp_parser_expression
1624 (cp_parser *, bool);
1625 static tree cp_parser_constant_expression
1626 (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1630 /* Statements [gram.stmt.stmt] */
1632 static void cp_parser_statement
1633 (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1636 static tree cp_parser_expression_statement
1637 (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639 (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641 (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643 (cp_parser *, bool *);
1644 static tree cp_parser_condition
1646 static tree cp_parser_iteration_statement
1648 static void cp_parser_for_init_statement
1650 static tree cp_parser_jump_statement
1652 static void cp_parser_declaration_statement
1655 static tree cp_parser_implicitly_scoped_statement
1656 (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1660 /* Declarations [gram.dcl.dcl] */
1662 static void cp_parser_declaration_seq_opt
1664 static void cp_parser_declaration
1666 static void cp_parser_block_declaration
1667 (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669 (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1674 static tree cp_parser_function_specifier_opt
1675 (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679 static tree cp_parser_simple_type_specifier
1680 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1683 static tree cp_parser_elaborated_type_specifier
1684 (cp_parser *, bool, bool);
1685 static tree cp_parser_enum_specifier
1687 static void cp_parser_enumerator_list
1688 (cp_parser *, tree);
1689 static void cp_parser_enumerator_definition
1690 (cp_parser *, tree);
1691 static tree cp_parser_namespace_name
1693 static void cp_parser_namespace_definition
1695 static void cp_parser_namespace_body
1697 static tree cp_parser_qualified_namespace_specifier
1699 static void cp_parser_namespace_alias_definition
1701 static bool cp_parser_using_declaration
1702 (cp_parser *, bool);
1703 static void cp_parser_using_directive
1705 static void cp_parser_asm_definition
1707 static void cp_parser_linkage_specification
1709 static void cp_parser_static_assert
1710 (cp_parser *, bool);
1711 static tree cp_parser_decltype
1714 /* Declarators [gram.dcl.decl] */
1716 static tree cp_parser_init_declarator
1717 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1718 static cp_declarator *cp_parser_declarator
1719 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1720 static cp_declarator *cp_parser_direct_declarator
1721 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1722 static enum tree_code cp_parser_ptr_operator
1723 (cp_parser *, tree *, cp_cv_quals *);
1724 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1726 static tree cp_parser_declarator_id
1727 (cp_parser *, bool);
1728 static tree cp_parser_type_id
1730 static void cp_parser_type_specifier_seq
1731 (cp_parser *, bool, cp_decl_specifier_seq *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1734 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1735 (cp_parser *, bool *);
1736 static cp_parameter_declarator *cp_parser_parameter_declaration
1737 (cp_parser *, bool, bool *);
1738 static tree cp_parser_default_argument
1739 (cp_parser *, bool);
1740 static void cp_parser_function_body
1742 static tree cp_parser_initializer
1743 (cp_parser *, bool *, bool *);
1744 static tree cp_parser_initializer_clause
1745 (cp_parser *, bool *);
1746 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1747 (cp_parser *, bool *);
1749 static bool cp_parser_ctor_initializer_opt_and_function_body
1752 /* Classes [gram.class] */
1754 static tree cp_parser_class_name
1755 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1756 static tree cp_parser_class_specifier
1758 static tree cp_parser_class_head
1759 (cp_parser *, bool *, tree *, tree *);
1760 static enum tag_types cp_parser_class_key
1762 static void cp_parser_member_specification_opt
1764 static void cp_parser_member_declaration
1766 static tree cp_parser_pure_specifier
1768 static tree cp_parser_constant_initializer
1771 /* Derived classes [gram.class.derived] */
1773 static tree cp_parser_base_clause
1775 static tree cp_parser_base_specifier
1778 /* Special member functions [gram.special] */
1780 static tree cp_parser_conversion_function_id
1782 static tree cp_parser_conversion_type_id
1784 static cp_declarator *cp_parser_conversion_declarator_opt
1786 static bool cp_parser_ctor_initializer_opt
1788 static void cp_parser_mem_initializer_list
1790 static tree cp_parser_mem_initializer
1792 static tree cp_parser_mem_initializer_id
1795 /* Overloading [gram.over] */
1797 static tree cp_parser_operator_function_id
1799 static tree cp_parser_operator
1802 /* Templates [gram.temp] */
1804 static void cp_parser_template_declaration
1805 (cp_parser *, bool);
1806 static tree cp_parser_template_parameter_list
1808 static tree cp_parser_template_parameter
1809 (cp_parser *, bool *, bool *);
1810 static tree cp_parser_type_parameter
1811 (cp_parser *, bool *);
1812 static tree cp_parser_template_id
1813 (cp_parser *, bool, bool, bool);
1814 static tree cp_parser_template_name
1815 (cp_parser *, bool, bool, bool, bool *);
1816 static tree cp_parser_template_argument_list
1818 static tree cp_parser_template_argument
1820 static void cp_parser_explicit_instantiation
1822 static void cp_parser_explicit_specialization
1825 /* Exception handling [gram.exception] */
1827 static tree cp_parser_try_block
1829 static bool cp_parser_function_try_block
1831 static void cp_parser_handler_seq
1833 static void cp_parser_handler
1835 static tree cp_parser_exception_declaration
1837 static tree cp_parser_throw_expression
1839 static tree cp_parser_exception_specification_opt
1841 static tree cp_parser_type_id_list
1844 /* GNU Extensions */
1846 static tree cp_parser_asm_specification_opt
1848 static tree cp_parser_asm_operand_list
1850 static tree cp_parser_asm_clobber_list
1852 static tree cp_parser_attributes_opt
1854 static tree cp_parser_attribute_list
1856 static bool cp_parser_extension_opt
1857 (cp_parser *, int *);
1858 static void cp_parser_label_declaration
1861 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1862 static bool cp_parser_pragma
1863 (cp_parser *, enum pragma_context);
1865 /* Objective-C++ Productions */
1867 static tree cp_parser_objc_message_receiver
1869 static tree cp_parser_objc_message_args
1871 static tree cp_parser_objc_message_expression
1873 static tree cp_parser_objc_encode_expression
1875 static tree cp_parser_objc_defs_expression
1877 static tree cp_parser_objc_protocol_expression
1879 static tree cp_parser_objc_selector_expression
1881 static tree cp_parser_objc_expression
1883 static bool cp_parser_objc_selector_p
1885 static tree cp_parser_objc_selector
1887 static tree cp_parser_objc_protocol_refs_opt
1889 static void cp_parser_objc_declaration
1891 static tree cp_parser_objc_statement
1894 /* Utility Routines */
1896 static tree cp_parser_lookup_name
1897 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1898 static tree cp_parser_lookup_name_simple
1899 (cp_parser *, tree);
1900 static tree cp_parser_maybe_treat_template_as_class
1902 static bool cp_parser_check_declarator_template_parameters
1903 (cp_parser *, cp_declarator *);
1904 static bool cp_parser_check_template_parameters
1905 (cp_parser *, unsigned);
1906 static tree cp_parser_simple_cast_expression
1908 static tree cp_parser_global_scope_opt
1909 (cp_parser *, bool);
1910 static bool cp_parser_constructor_declarator_p
1911 (cp_parser *, bool);
1912 static tree cp_parser_function_definition_from_specifiers_and_declarator
1913 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1914 static tree cp_parser_function_definition_after_declarator
1915 (cp_parser *, bool);
1916 static void cp_parser_template_declaration_after_export
1917 (cp_parser *, bool);
1918 static void cp_parser_perform_template_parameter_access_checks
1919 (VEC (deferred_access_check,gc)*);
1920 static tree cp_parser_single_declaration
1921 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1922 static tree cp_parser_functional_cast
1923 (cp_parser *, tree);
1924 static tree cp_parser_save_member_function_body
1925 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1926 static tree cp_parser_enclosed_template_argument_list
1928 static void cp_parser_save_default_args
1929 (cp_parser *, tree);
1930 static void cp_parser_late_parsing_for_member
1931 (cp_parser *, tree);
1932 static void cp_parser_late_parsing_default_args
1933 (cp_parser *, tree);
1934 static tree cp_parser_sizeof_operand
1935 (cp_parser *, enum rid);
1936 static tree cp_parser_trait_expr
1937 (cp_parser *, enum rid);
1938 static bool cp_parser_declares_only_class_p
1940 static void cp_parser_set_storage_class
1941 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1942 static void cp_parser_set_decl_spec_type
1943 (cp_decl_specifier_seq *, tree, bool);
1944 static bool cp_parser_friend_p
1945 (const cp_decl_specifier_seq *);
1946 static cp_token *cp_parser_require
1947 (cp_parser *, enum cpp_ttype, const char *);
1948 static cp_token *cp_parser_require_keyword
1949 (cp_parser *, enum rid, const char *);
1950 static bool cp_parser_token_starts_function_definition_p
1952 static bool cp_parser_next_token_starts_class_definition_p
1954 static bool cp_parser_next_token_ends_template_argument_p
1956 static bool cp_parser_nth_token_starts_template_argument_list_p
1957 (cp_parser *, size_t);
1958 static enum tag_types cp_parser_token_is_class_key
1960 static void cp_parser_check_class_key
1961 (enum tag_types, tree type);
1962 static void cp_parser_check_access_in_redeclaration
1964 static bool cp_parser_optional_template_keyword
1966 static void cp_parser_pre_parsed_nested_name_specifier
1968 static void cp_parser_cache_group
1969 (cp_parser *, enum cpp_ttype, unsigned);
1970 static void cp_parser_parse_tentatively
1972 static void cp_parser_commit_to_tentative_parse
1974 static void cp_parser_abort_tentative_parse
1976 static bool cp_parser_parse_definitely
1978 static inline bool cp_parser_parsing_tentatively
1980 static bool cp_parser_uncommitted_to_tentative_parse_p
1982 static void cp_parser_error
1983 (cp_parser *, const char *);
1984 static void cp_parser_name_lookup_error
1985 (cp_parser *, tree, tree, const char *);
1986 static bool cp_parser_simulate_error
1988 static bool cp_parser_check_type_definition
1990 static void cp_parser_check_for_definition_in_return_type
1991 (cp_declarator *, tree);
1992 static void cp_parser_check_for_invalid_template_id
1993 (cp_parser *, tree);
1994 static bool cp_parser_non_integral_constant_expression
1995 (cp_parser *, const char *);
1996 static void cp_parser_diagnose_invalid_type_name
1997 (cp_parser *, tree, tree);
1998 static bool cp_parser_parse_and_diagnose_invalid_type_name
2000 static int cp_parser_skip_to_closing_parenthesis
2001 (cp_parser *, bool, bool, bool);
2002 static void cp_parser_skip_to_end_of_statement
2004 static void cp_parser_consume_semicolon_at_end_of_statement
2006 static void cp_parser_skip_to_end_of_block_or_statement
2008 static bool cp_parser_skip_to_closing_brace
2010 static void cp_parser_skip_to_end_of_template_parameter_list
2012 static void cp_parser_skip_to_pragma_eol
2013 (cp_parser*, cp_token *);
2014 static bool cp_parser_error_occurred
2016 static bool cp_parser_allow_gnu_extensions_p
2018 static bool cp_parser_is_string_literal
2020 static bool cp_parser_is_keyword
2021 (cp_token *, enum rid);
2022 static tree cp_parser_make_typename_type
2023 (cp_parser *, tree, tree);
2024 static cp_declarator * cp_parser_make_indirect_declarator
2025 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2027 /* Returns nonzero if we are parsing tentatively. */
2030 cp_parser_parsing_tentatively (cp_parser* parser)
2032 return parser->context->next != NULL;
2035 /* Returns nonzero if TOKEN is a string literal. */
2038 cp_parser_is_string_literal (cp_token* token)
2040 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2043 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2046 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2048 return token->keyword == keyword;
2051 /* If not parsing tentatively, issue a diagnostic of the form
2052 FILE:LINE: MESSAGE before TOKEN
2053 where TOKEN is the next token in the input stream. MESSAGE
2054 (specified by the caller) is usually of the form "expected
2058 cp_parser_error (cp_parser* parser, const char* message)
2060 if (!cp_parser_simulate_error (parser))
2062 cp_token *token = cp_lexer_peek_token (parser->lexer);
2063 /* This diagnostic makes more sense if it is tagged to the line
2064 of the token we just peeked at. */
2065 cp_lexer_set_source_position_from_token (token);
2067 if (token->type == CPP_PRAGMA)
2069 error ("%<#pragma%> is not allowed here");
2070 cp_parser_skip_to_pragma_eol (parser, token);
2074 c_parse_error (message,
2075 /* Because c_parser_error does not understand
2076 CPP_KEYWORD, keywords are treated like
2078 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2083 /* Issue an error about name-lookup failing. NAME is the
2084 IDENTIFIER_NODE DECL is the result of
2085 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2086 the thing that we hoped to find. */
2089 cp_parser_name_lookup_error (cp_parser* parser,
2092 const char* desired)
2094 /* If name lookup completely failed, tell the user that NAME was not
2096 if (decl == error_mark_node)
2098 if (parser->scope && parser->scope != global_namespace)
2099 error ("%<%E::%E%> has not been declared",
2100 parser->scope, name);
2101 else if (parser->scope == global_namespace)
2102 error ("%<::%E%> has not been declared", name);
2103 else if (parser->object_scope
2104 && !CLASS_TYPE_P (parser->object_scope))
2105 error ("request for member %qE in non-class type %qT",
2106 name, parser->object_scope);
2107 else if (parser->object_scope)
2108 error ("%<%T::%E%> has not been declared",
2109 parser->object_scope, name);
2111 error ("%qE has not been declared", name);
2113 else if (parser->scope && parser->scope != global_namespace)
2114 error ("%<%E::%E%> %s", parser->scope, name, desired);
2115 else if (parser->scope == global_namespace)
2116 error ("%<::%E%> %s", name, desired);
2118 error ("%qE %s", name, desired);
2121 /* If we are parsing tentatively, remember that an error has occurred
2122 during this tentative parse. Returns true if the error was
2123 simulated; false if a message should be issued by the caller. */
2126 cp_parser_simulate_error (cp_parser* parser)
2128 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2130 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2136 /* Check for repeated decl-specifiers. */
2139 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2143 for (ds = ds_first; ds != ds_last; ++ds)
2145 unsigned count = decl_specs->specs[(int)ds];
2148 /* The "long" specifier is a special case because of "long long". */
2152 error ("%<long long long%> is too long for GCC");
2153 else if (pedantic && !in_system_header && warn_long_long
2154 && cxx_dialect == cxx98)
2155 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2159 static const char *const decl_spec_names[] = {
2175 error ("duplicate %qs", decl_spec_names[(int)ds]);
2180 /* This function is called when a type is defined. If type
2181 definitions are forbidden at this point, an error message is
2185 cp_parser_check_type_definition (cp_parser* parser)
2187 /* If types are forbidden here, issue a message. */
2188 if (parser->type_definition_forbidden_message)
2190 /* Use `%s' to print the string in case there are any escape
2191 characters in the message. */
2192 error ("%s", parser->type_definition_forbidden_message);
2198 /* This function is called when the DECLARATOR is processed. The TYPE
2199 was a type defined in the decl-specifiers. If it is invalid to
2200 define a type in the decl-specifiers for DECLARATOR, an error is
2204 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2207 /* [dcl.fct] forbids type definitions in return types.
2208 Unfortunately, it's not easy to know whether or not we are
2209 processing a return type until after the fact. */
2211 && (declarator->kind == cdk_pointer
2212 || declarator->kind == cdk_reference
2213 || declarator->kind == cdk_ptrmem))
2214 declarator = declarator->declarator;
2216 && declarator->kind == cdk_function)
2218 error ("new types may not be defined in a return type");
2219 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2224 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2225 "<" in any valid C++ program. If the next token is indeed "<",
2226 issue a message warning the user about what appears to be an
2227 invalid attempt to form a template-id. */
2230 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2233 cp_token_position start = 0;
2235 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2238 error ("%qT is not a template", type);
2239 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2240 error ("%qE is not a template", type);
2242 error ("invalid template-id");
2243 /* Remember the location of the invalid "<". */
2244 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2245 start = cp_lexer_token_position (parser->lexer, true);
2246 /* Consume the "<". */
2247 cp_lexer_consume_token (parser->lexer);
2248 /* Parse the template arguments. */
2249 cp_parser_enclosed_template_argument_list (parser);
2250 /* Permanently remove the invalid template arguments so that
2251 this error message is not issued again. */
2253 cp_lexer_purge_tokens_after (parser->lexer, start);
2257 /* If parsing an integral constant-expression, issue an error message
2258 about the fact that THING appeared and return true. Otherwise,
2259 return false. In either case, set
2260 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2263 cp_parser_non_integral_constant_expression (cp_parser *parser,
2266 parser->non_integral_constant_expression_p = true;
2267 if (parser->integral_constant_expression_p)
2269 if (!parser->allow_non_integral_constant_expression_p)
2271 error ("%s cannot appear in a constant-expression", thing);
2278 /* Emit a diagnostic for an invalid type name. SCOPE is the
2279 qualifying scope (or NULL, if none) for ID. This function commits
2280 to the current active tentative parse, if any. (Otherwise, the
2281 problematic construct might be encountered again later, resulting
2282 in duplicate error messages.) */
2285 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2287 tree decl, old_scope;
2288 /* Try to lookup the identifier. */
2289 old_scope = parser->scope;
2290 parser->scope = scope;
2291 decl = cp_parser_lookup_name_simple (parser, id);
2292 parser->scope = old_scope;
2293 /* If the lookup found a template-name, it means that the user forgot
2294 to specify an argument list. Emit a useful error message. */
2295 if (TREE_CODE (decl) == TEMPLATE_DECL)
2296 error ("invalid use of template-name %qE without an argument list", decl);
2297 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2298 error ("invalid use of destructor %qD as a type", id);
2299 else if (TREE_CODE (decl) == TYPE_DECL)
2300 /* Something like 'unsigned A a;' */
2301 error ("invalid combination of multiple type-specifiers");
2302 else if (!parser->scope)
2304 /* Issue an error message. */
2305 error ("%qE does not name a type", id);
2306 /* If we're in a template class, it's possible that the user was
2307 referring to a type from a base class. For example:
2309 template <typename T> struct A { typedef T X; };
2310 template <typename T> struct B : public A<T> { X x; };
2312 The user should have said "typename A<T>::X". */
2313 if (processing_template_decl && current_class_type
2314 && TYPE_BINFO (current_class_type))
2318 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2322 tree base_type = BINFO_TYPE (b);
2323 if (CLASS_TYPE_P (base_type)
2324 && dependent_type_p (base_type))
2327 /* Go from a particular instantiation of the
2328 template (which will have an empty TYPE_FIELDs),
2329 to the main version. */
2330 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2331 for (field = TYPE_FIELDS (base_type);
2333 field = TREE_CHAIN (field))
2334 if (TREE_CODE (field) == TYPE_DECL
2335 && DECL_NAME (field) == id)
2337 inform ("(perhaps %<typename %T::%E%> was intended)",
2338 BINFO_TYPE (b), id);
2347 /* Here we diagnose qualified-ids where the scope is actually correct,
2348 but the identifier does not resolve to a valid type name. */
2349 else if (parser->scope != error_mark_node)
2351 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2352 error ("%qE in namespace %qE does not name a type",
2354 else if (TYPE_P (parser->scope))
2355 error ("%qE in class %qT does not name a type", id, parser->scope);
2359 cp_parser_commit_to_tentative_parse (parser);
2362 /* Check for a common situation where a type-name should be present,
2363 but is not, and issue a sensible error message. Returns true if an
2364 invalid type-name was detected.
2366 The situation handled by this function are variable declarations of the
2367 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2368 Usually, `ID' should name a type, but if we got here it means that it
2369 does not. We try to emit the best possible error message depending on
2370 how exactly the id-expression looks like. */
2373 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2377 cp_parser_parse_tentatively (parser);
2378 id = cp_parser_id_expression (parser,
2379 /*template_keyword_p=*/false,
2380 /*check_dependency_p=*/true,
2381 /*template_p=*/NULL,
2382 /*declarator_p=*/true,
2383 /*optional_p=*/false);
2384 /* After the id-expression, there should be a plain identifier,
2385 otherwise this is not a simple variable declaration. Also, if
2386 the scope is dependent, we cannot do much. */
2387 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2388 || (parser->scope && TYPE_P (parser->scope)
2389 && dependent_type_p (parser->scope))
2390 || TREE_CODE (id) == TYPE_DECL)
2392 cp_parser_abort_tentative_parse (parser);
2395 if (!cp_parser_parse_definitely (parser))
2398 /* Emit a diagnostic for the invalid type. */
2399 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2400 /* Skip to the end of the declaration; there's no point in
2401 trying to process it. */
2402 cp_parser_skip_to_end_of_block_or_statement (parser);
2406 /* Consume tokens up to, and including, the next non-nested closing `)'.
2407 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2408 are doing error recovery. Returns -1 if OR_COMMA is true and we
2409 found an unnested comma. */
2412 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2417 unsigned paren_depth = 0;
2418 unsigned brace_depth = 0;
2420 if (recovering && !or_comma
2421 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2426 cp_token * token = cp_lexer_peek_token (parser->lexer);
2428 switch (token->type)
2431 case CPP_PRAGMA_EOL:
2432 /* If we've run out of tokens, then there is no closing `)'. */
2436 /* This matches the processing in skip_to_end_of_statement. */
2441 case CPP_OPEN_BRACE:
2444 case CPP_CLOSE_BRACE:
2450 if (recovering && or_comma && !brace_depth && !paren_depth)
2454 case CPP_OPEN_PAREN:
2459 case CPP_CLOSE_PAREN:
2460 if (!brace_depth && !paren_depth--)
2463 cp_lexer_consume_token (parser->lexer);
2472 /* Consume the token. */
2473 cp_lexer_consume_token (parser->lexer);
2477 /* Consume tokens until we reach the end of the current statement.
2478 Normally, that will be just before consuming a `;'. However, if a
2479 non-nested `}' comes first, then we stop before consuming that. */
2482 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2484 unsigned nesting_depth = 0;
2488 cp_token *token = cp_lexer_peek_token (parser->lexer);
2490 switch (token->type)
2493 case CPP_PRAGMA_EOL:
2494 /* If we've run out of tokens, stop. */
2498 /* If the next token is a `;', we have reached the end of the
2504 case CPP_CLOSE_BRACE:
2505 /* If this is a non-nested '}', stop before consuming it.
2506 That way, when confronted with something like:
2510 we stop before consuming the closing '}', even though we
2511 have not yet reached a `;'. */
2512 if (nesting_depth == 0)
2515 /* If it is the closing '}' for a block that we have
2516 scanned, stop -- but only after consuming the token.
2522 we will stop after the body of the erroneously declared
2523 function, but before consuming the following `typedef'
2525 if (--nesting_depth == 0)
2527 cp_lexer_consume_token (parser->lexer);
2531 case CPP_OPEN_BRACE:
2539 /* Consume the token. */
2540 cp_lexer_consume_token (parser->lexer);
2544 /* This function is called at the end of a statement or declaration.
2545 If the next token is a semicolon, it is consumed; otherwise, error
2546 recovery is attempted. */
2549 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2551 /* Look for the trailing `;'. */
2552 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2554 /* If there is additional (erroneous) input, skip to the end of
2556 cp_parser_skip_to_end_of_statement (parser);
2557 /* If the next token is now a `;', consume it. */
2558 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2559 cp_lexer_consume_token (parser->lexer);
2563 /* Skip tokens until we have consumed an entire block, or until we
2564 have consumed a non-nested `;'. */
2567 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2569 int nesting_depth = 0;
2571 while (nesting_depth >= 0)
2573 cp_token *token = cp_lexer_peek_token (parser->lexer);
2575 switch (token->type)
2578 case CPP_PRAGMA_EOL:
2579 /* If we've run out of tokens, stop. */
2583 /* Stop if this is an unnested ';'. */
2588 case CPP_CLOSE_BRACE:
2589 /* Stop if this is an unnested '}', or closes the outermost
2596 case CPP_OPEN_BRACE:
2605 /* Consume the token. */
2606 cp_lexer_consume_token (parser->lexer);
2610 /* Skip tokens until a non-nested closing curly brace is the next
2611 token, or there are no more tokens. Return true in the first case,
2615 cp_parser_skip_to_closing_brace (cp_parser *parser)
2617 unsigned nesting_depth = 0;
2621 cp_token *token = cp_lexer_peek_token (parser->lexer);
2623 switch (token->type)
2626 case CPP_PRAGMA_EOL:
2627 /* If we've run out of tokens, stop. */
2630 case CPP_CLOSE_BRACE:
2631 /* If the next token is a non-nested `}', then we have reached
2632 the end of the current block. */
2633 if (nesting_depth-- == 0)
2637 case CPP_OPEN_BRACE:
2638 /* If it the next token is a `{', then we are entering a new
2639 block. Consume the entire block. */
2647 /* Consume the token. */
2648 cp_lexer_consume_token (parser->lexer);
2652 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2653 parameter is the PRAGMA token, allowing us to purge the entire pragma
2657 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2661 parser->lexer->in_pragma = false;
2664 token = cp_lexer_consume_token (parser->lexer);
2665 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2667 /* Ensure that the pragma is not parsed again. */
2668 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2671 /* Require pragma end of line, resyncing with it as necessary. The
2672 arguments are as for cp_parser_skip_to_pragma_eol. */
2675 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2677 parser->lexer->in_pragma = false;
2678 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2679 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2682 /* This is a simple wrapper around make_typename_type. When the id is
2683 an unresolved identifier node, we can provide a superior diagnostic
2684 using cp_parser_diagnose_invalid_type_name. */
2687 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2690 if (TREE_CODE (id) == IDENTIFIER_NODE)
2692 result = make_typename_type (scope, id, typename_type,
2693 /*complain=*/tf_none);
2694 if (result == error_mark_node)
2695 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2698 return make_typename_type (scope, id, typename_type, tf_error);
2701 /* This is a wrapper around the
2702 make_{pointer,ptrmem,reference}_declarator functions that decides
2703 which one to call based on the CODE and CLASS_TYPE arguments. The
2704 CODE argument should be one of the values returned by
2705 cp_parser_ptr_operator. */
2706 static cp_declarator *
2707 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2708 cp_cv_quals cv_qualifiers,
2709 cp_declarator *target)
2711 if (code == ERROR_MARK)
2712 return cp_error_declarator;
2714 if (code == INDIRECT_REF)
2715 if (class_type == NULL_TREE)
2716 return make_pointer_declarator (cv_qualifiers, target);
2718 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2719 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2720 return make_reference_declarator (cv_qualifiers, target, false);
2721 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2722 return make_reference_declarator (cv_qualifiers, target, true);
2726 /* Create a new C++ parser. */
2729 cp_parser_new (void)
2735 /* cp_lexer_new_main is called before calling ggc_alloc because
2736 cp_lexer_new_main might load a PCH file. */
2737 lexer = cp_lexer_new_main ();
2739 /* Initialize the binops_by_token so that we can get the tree
2740 directly from the token. */
2741 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2742 binops_by_token[binops[i].token_type] = binops[i];
2744 parser = GGC_CNEW (cp_parser);
2745 parser->lexer = lexer;
2746 parser->context = cp_parser_context_new (NULL);
2748 /* For now, we always accept GNU extensions. */
2749 parser->allow_gnu_extensions_p = 1;
2751 /* The `>' token is a greater-than operator, not the end of a
2753 parser->greater_than_is_operator_p = true;
2755 parser->default_arg_ok_p = true;
2757 /* We are not parsing a constant-expression. */
2758 parser->integral_constant_expression_p = false;
2759 parser->allow_non_integral_constant_expression_p = false;
2760 parser->non_integral_constant_expression_p = false;
2762 /* Local variable names are not forbidden. */
2763 parser->local_variables_forbidden_p = false;
2765 /* We are not processing an `extern "C"' declaration. */
2766 parser->in_unbraced_linkage_specification_p = false;
2768 /* We are not processing a declarator. */
2769 parser->in_declarator_p = false;
2771 /* We are not processing a template-argument-list. */
2772 parser->in_template_argument_list_p = false;
2774 /* We are not in an iteration statement. */
2775 parser->in_statement = 0;
2777 /* We are not in a switch statement. */
2778 parser->in_switch_statement_p = false;
2780 /* We are not parsing a type-id inside an expression. */
2781 parser->in_type_id_in_expr_p = false;
2783 /* Declarations aren't implicitly extern "C". */
2784 parser->implicit_extern_c = false;
2786 /* String literals should be translated to the execution character set. */
2787 parser->translate_strings_p = true;
2789 /* We are not parsing a function body. */
2790 parser->in_function_body = false;
2792 /* The unparsed function queue is empty. */
2793 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2795 /* There are no classes being defined. */
2796 parser->num_classes_being_defined = 0;
2798 /* No template parameters apply. */
2799 parser->num_template_parameter_lists = 0;
2804 /* Create a cp_lexer structure which will emit the tokens in CACHE
2805 and push it onto the parser's lexer stack. This is used for delayed
2806 parsing of in-class method bodies and default arguments, and should
2807 not be confused with tentative parsing. */
2809 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2811 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2812 lexer->next = parser->lexer;
2813 parser->lexer = lexer;
2815 /* Move the current source position to that of the first token in the
2817 cp_lexer_set_source_position_from_token (lexer->next_token);
2820 /* Pop the top lexer off the parser stack. This is never used for the
2821 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2823 cp_parser_pop_lexer (cp_parser *parser)
2825 cp_lexer *lexer = parser->lexer;
2826 parser->lexer = lexer->next;
2827 cp_lexer_destroy (lexer);
2829 /* Put the current source position back where it was before this
2830 lexer was pushed. */
2831 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2834 /* Lexical conventions [gram.lex] */
2836 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2840 cp_parser_identifier (cp_parser* parser)
2844 /* Look for the identifier. */
2845 token = cp_parser_require (parser, CPP_NAME, "identifier");
2846 /* Return the value. */
2847 return token ? token->u.value : error_mark_node;
2850 /* Parse a sequence of adjacent string constants. Returns a
2851 TREE_STRING representing the combined, nul-terminated string
2852 constant. If TRANSLATE is true, translate the string to the
2853 execution character set. If WIDE_OK is true, a wide string is
2856 C++98 [lex.string] says that if a narrow string literal token is
2857 adjacent to a wide string literal token, the behavior is undefined.
2858 However, C99 6.4.5p4 says that this results in a wide string literal.
2859 We follow C99 here, for consistency with the C front end.
2861 This code is largely lifted from lex_string() in c-lex.c.
2863 FUTURE: ObjC++ will need to handle @-strings here. */
2865 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2870 struct obstack str_ob;
2871 cpp_string str, istr, *strs;
2874 tok = cp_lexer_peek_token (parser->lexer);
2875 if (!cp_parser_is_string_literal (tok))
2877 cp_parser_error (parser, "expected string-literal");
2878 return error_mark_node;
2881 /* Try to avoid the overhead of creating and destroying an obstack
2882 for the common case of just one string. */
2883 if (!cp_parser_is_string_literal
2884 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2886 cp_lexer_consume_token (parser->lexer);
2888 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2889 str.len = TREE_STRING_LENGTH (tok->u.value);
2891 if (tok->type == CPP_WSTRING)
2898 gcc_obstack_init (&str_ob);
2903 cp_lexer_consume_token (parser->lexer);
2905 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2906 str.len = TREE_STRING_LENGTH (tok->u.value);
2907 if (tok->type == CPP_WSTRING)
2910 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2912 tok = cp_lexer_peek_token (parser->lexer);
2914 while (cp_parser_is_string_literal (tok));
2916 strs = (cpp_string *) obstack_finish (&str_ob);
2919 if (wide && !wide_ok)
2921 cp_parser_error (parser, "a wide string is invalid in this context");
2925 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2926 (parse_in, strs, count, &istr, wide))
2928 value = build_string (istr.len, (const char *)istr.text);
2929 free (CONST_CAST (unsigned char *, istr.text));
2931 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2932 value = fix_string_type (value);
2935 /* cpp_interpret_string has issued an error. */
2936 value = error_mark_node;
2939 obstack_free (&str_ob, 0);
2945 /* Basic concepts [gram.basic] */
2947 /* Parse a translation-unit.
2950 declaration-seq [opt]
2952 Returns TRUE if all went well. */
2955 cp_parser_translation_unit (cp_parser* parser)
2957 /* The address of the first non-permanent object on the declarator
2959 static void *declarator_obstack_base;
2963 /* Create the declarator obstack, if necessary. */
2964 if (!cp_error_declarator)
2966 gcc_obstack_init (&declarator_obstack);
2967 /* Create the error declarator. */
2968 cp_error_declarator = make_declarator (cdk_error);
2969 /* Create the empty parameter list. */
2970 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2971 /* Remember where the base of the declarator obstack lies. */
2972 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2975 cp_parser_declaration_seq_opt (parser);
2977 /* If there are no tokens left then all went well. */
2978 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2980 /* Get rid of the token array; we don't need it any more. */
2981 cp_lexer_destroy (parser->lexer);
2982 parser->lexer = NULL;
2984 /* This file might have been a context that's implicitly extern
2985 "C". If so, pop the lang context. (Only relevant for PCH.) */
2986 if (parser->implicit_extern_c)
2988 pop_lang_context ();
2989 parser->implicit_extern_c = false;
2993 finish_translation_unit ();
2999 cp_parser_error (parser, "expected declaration");
3003 /* Make sure the declarator obstack was fully cleaned up. */
3004 gcc_assert (obstack_next_free (&declarator_obstack)
3005 == declarator_obstack_base);
3007 /* All went well. */
3011 /* Expressions [gram.expr] */
3013 /* Parse a primary-expression.
3024 ( compound-statement )
3025 __builtin_va_arg ( assignment-expression , type-id )
3026 __builtin_offsetof ( type-id , offsetof-expression )
3029 __has_nothrow_assign ( type-id )
3030 __has_nothrow_constructor ( type-id )
3031 __has_nothrow_copy ( type-id )
3032 __has_trivial_assign ( type-id )
3033 __has_trivial_constructor ( type-id )
3034 __has_trivial_copy ( type-id )
3035 __has_trivial_destructor ( type-id )
3036 __has_virtual_destructor ( type-id )
3037 __is_abstract ( type-id )
3038 __is_base_of ( type-id , type-id )
3039 __is_class ( type-id )
3040 __is_convertible_to ( type-id , type-id )
3041 __is_empty ( type-id )
3042 __is_enum ( type-id )
3043 __is_pod ( type-id )
3044 __is_polymorphic ( type-id )
3045 __is_union ( type-id )
3047 Objective-C++ Extension:
3055 ADDRESS_P is true iff this expression was immediately preceded by
3056 "&" and therefore might denote a pointer-to-member. CAST_P is true
3057 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3058 true iff this expression is a template argument.
3060 Returns a representation of the expression. Upon return, *IDK
3061 indicates what kind of id-expression (if any) was present. */
3064 cp_parser_primary_expression (cp_parser *parser,
3067 bool template_arg_p,
3072 /* Assume the primary expression is not an id-expression. */
3073 *idk = CP_ID_KIND_NONE;
3075 /* Peek at the next token. */
3076 token = cp_lexer_peek_token (parser->lexer);
3077 switch (token->type)
3088 token = cp_lexer_consume_token (parser->lexer);
3089 /* Floating-point literals are only allowed in an integral
3090 constant expression if they are cast to an integral or
3091 enumeration type. */
3092 if (TREE_CODE (token->u.value) == REAL_CST
3093 && parser->integral_constant_expression_p
3096 /* CAST_P will be set even in invalid code like "int(2.7 +
3097 ...)". Therefore, we have to check that the next token
3098 is sure to end the cast. */
3101 cp_token *next_token;
3103 next_token = cp_lexer_peek_token (parser->lexer);
3104 if (/* The comma at the end of an
3105 enumerator-definition. */
3106 next_token->type != CPP_COMMA
3107 /* The curly brace at the end of an enum-specifier. */
3108 && next_token->type != CPP_CLOSE_BRACE
3109 /* The end of a statement. */
3110 && next_token->type != CPP_SEMICOLON
3111 /* The end of the cast-expression. */
3112 && next_token->type != CPP_CLOSE_PAREN
3113 /* The end of an array bound. */
3114 && next_token->type != CPP_CLOSE_SQUARE
3115 /* The closing ">" in a template-argument-list. */
3116 && (next_token->type != CPP_GREATER
3117 || parser->greater_than_is_operator_p)
3118 /* C++0x only: A ">>" treated like two ">" tokens,
3119 in a template-argument-list. */
3120 && (next_token->type != CPP_RSHIFT
3121 || (cxx_dialect == cxx98)
3122 || parser->greater_than_is_operator_p))
3126 /* If we are within a cast, then the constraint that the
3127 cast is to an integral or enumeration type will be
3128 checked at that point. If we are not within a cast, then
3129 this code is invalid. */
3131 cp_parser_non_integral_constant_expression
3132 (parser, "floating-point literal");
3134 return token->u.value;
3138 /* ??? Should wide strings be allowed when parser->translate_strings_p
3139 is false (i.e. in attributes)? If not, we can kill the third
3140 argument to cp_parser_string_literal. */
3141 return cp_parser_string_literal (parser,
3142 parser->translate_strings_p,
3145 case CPP_OPEN_PAREN:
3148 bool saved_greater_than_is_operator_p;
3150 /* Consume the `('. */
3151 cp_lexer_consume_token (parser->lexer);
3152 /* Within a parenthesized expression, a `>' token is always
3153 the greater-than operator. */
3154 saved_greater_than_is_operator_p
3155 = parser->greater_than_is_operator_p;
3156 parser->greater_than_is_operator_p = true;
3157 /* If we see `( { ' then we are looking at the beginning of
3158 a GNU statement-expression. */
3159 if (cp_parser_allow_gnu_extensions_p (parser)
3160 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3162 /* Statement-expressions are not allowed by the standard. */
3164 pedwarn ("ISO C++ forbids braced-groups within expressions");
3166 /* And they're not allowed outside of a function-body; you
3167 cannot, for example, write:
3169 int i = ({ int j = 3; j + 1; });
3171 at class or namespace scope. */
3172 if (!parser->in_function_body
3173 || parser->in_template_argument_list_p)
3175 error ("statement-expressions are not allowed outside "
3176 "functions nor in template-argument lists");
3177 cp_parser_skip_to_end_of_block_or_statement (parser);
3178 expr = error_mark_node;
3182 /* Start the statement-expression. */
3183 expr = begin_stmt_expr ();
3184 /* Parse the compound-statement. */
3185 cp_parser_compound_statement (parser, expr, false);
3187 expr = finish_stmt_expr (expr, false);
3192 /* Parse the parenthesized expression. */
3193 expr = cp_parser_expression (parser, cast_p);
3194 /* Let the front end know that this expression was
3195 enclosed in parentheses. This matters in case, for
3196 example, the expression is of the form `A::B', since
3197 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3199 finish_parenthesized_expr (expr);
3201 /* The `>' token might be the end of a template-id or
3202 template-parameter-list now. */
3203 parser->greater_than_is_operator_p
3204 = saved_greater_than_is_operator_p;
3205 /* Consume the `)'. */
3206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3207 cp_parser_skip_to_end_of_statement (parser);
3213 switch (token->keyword)
3215 /* These two are the boolean literals. */
3217 cp_lexer_consume_token (parser->lexer);
3218 return boolean_true_node;
3220 cp_lexer_consume_token (parser->lexer);
3221 return boolean_false_node;
3223 /* The `__null' literal. */
3225 cp_lexer_consume_token (parser->lexer);
3228 /* Recognize the `this' keyword. */
3230 cp_lexer_consume_token (parser->lexer);
3231 if (parser->local_variables_forbidden_p)
3233 error ("%<this%> may not be used in this context");
3234 return error_mark_node;
3236 /* Pointers cannot appear in constant-expressions. */
3237 if (cp_parser_non_integral_constant_expression (parser,
3239 return error_mark_node;
3240 return finish_this_expr ();
3242 /* The `operator' keyword can be the beginning of an
3247 case RID_FUNCTION_NAME:
3248 case RID_PRETTY_FUNCTION_NAME:
3249 case RID_C99_FUNCTION_NAME:
3250 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3251 __func__ are the names of variables -- but they are
3252 treated specially. Therefore, they are handled here,
3253 rather than relying on the generic id-expression logic
3254 below. Grammatically, these names are id-expressions.
3256 Consume the token. */
3257 token = cp_lexer_consume_token (parser->lexer);
3258 /* Look up the name. */
3259 return finish_fname (token->u.value);
3266 /* The `__builtin_va_arg' construct is used to handle
3267 `va_arg'. Consume the `__builtin_va_arg' token. */
3268 cp_lexer_consume_token (parser->lexer);
3269 /* Look for the opening `('. */
3270 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3271 /* Now, parse the assignment-expression. */
3272 expression = cp_parser_assignment_expression (parser,
3274 /* Look for the `,'. */
3275 cp_parser_require (parser, CPP_COMMA, "`,'");
3276 /* Parse the type-id. */
3277 type = cp_parser_type_id (parser);
3278 /* Look for the closing `)'. */
3279 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3280 /* Using `va_arg' in a constant-expression is not
3282 if (cp_parser_non_integral_constant_expression (parser,
3284 return error_mark_node;
3285 return build_x_va_arg (expression, type);
3289 return cp_parser_builtin_offsetof (parser);
3291 case RID_HAS_NOTHROW_ASSIGN:
3292 case RID_HAS_NOTHROW_CONSTRUCTOR:
3293 case RID_HAS_NOTHROW_COPY:
3294 case RID_HAS_TRIVIAL_ASSIGN:
3295 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3296 case RID_HAS_TRIVIAL_COPY:
3297 case RID_HAS_TRIVIAL_DESTRUCTOR:
3298 case RID_HAS_VIRTUAL_DESTRUCTOR:
3299 case RID_IS_ABSTRACT:
3300 case RID_IS_BASE_OF:
3302 case RID_IS_CONVERTIBLE_TO:
3306 case RID_IS_POLYMORPHIC:
3308 return cp_parser_trait_expr (parser, token->keyword);
3310 /* Objective-C++ expressions. */
3312 case RID_AT_PROTOCOL:
3313 case RID_AT_SELECTOR:
3314 return cp_parser_objc_expression (parser);
3317 cp_parser_error (parser, "expected primary-expression");
3318 return error_mark_node;
3321 /* An id-expression can start with either an identifier, a
3322 `::' as the beginning of a qualified-id, or the "operator"
3326 case CPP_TEMPLATE_ID:
3327 case CPP_NESTED_NAME_SPECIFIER:
3331 const char *error_msg;
3336 /* Parse the id-expression. */
3338 = cp_parser_id_expression (parser,
3339 /*template_keyword_p=*/false,
3340 /*check_dependency_p=*/true,
3342 /*declarator_p=*/false,
3343 /*optional_p=*/false);
3344 if (id_expression == error_mark_node)
3345 return error_mark_node;
3346 token = cp_lexer_peek_token (parser->lexer);
3347 done = (token->type != CPP_OPEN_SQUARE
3348 && token->type != CPP_OPEN_PAREN
3349 && token->type != CPP_DOT
3350 && token->type != CPP_DEREF
3351 && token->type != CPP_PLUS_PLUS
3352 && token->type != CPP_MINUS_MINUS);
3353 /* If we have a template-id, then no further lookup is
3354 required. If the template-id was for a template-class, we
3355 will sometimes have a TYPE_DECL at this point. */
3356 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3357 || TREE_CODE (id_expression) == TYPE_DECL)
3358 decl = id_expression;
3359 /* Look up the name. */
3362 tree ambiguous_decls;
3364 decl = cp_parser_lookup_name (parser, id_expression,
3367 /*is_namespace=*/false,
3368 /*check_dependency=*/true,
3370 /* If the lookup was ambiguous, an error will already have
3372 if (ambiguous_decls)
3373 return error_mark_node;
3375 /* In Objective-C++, an instance variable (ivar) may be preferred
3376 to whatever cp_parser_lookup_name() found. */
3377 decl = objc_lookup_ivar (decl, id_expression);
3379 /* If name lookup gives us a SCOPE_REF, then the
3380 qualifying scope was dependent. */
3381 if (TREE_CODE (decl) == SCOPE_REF)
3383 /* At this point, we do not know if DECL is a valid
3384 integral constant expression. We assume that it is
3385 in fact such an expression, so that code like:
3387 template <int N> struct A {
3391 is accepted. At template-instantiation time, we
3392 will check that B<N>::i is actually a constant. */
3395 /* Check to see if DECL is a local variable in a context
3396 where that is forbidden. */
3397 if (parser->local_variables_forbidden_p
3398 && local_variable_p (decl))
3400 /* It might be that we only found DECL because we are
3401 trying to be generous with pre-ISO scoping rules.
3402 For example, consider:
3406 for (int i = 0; i < 10; ++i) {}
3407 extern void f(int j = i);
3410 Here, name look up will originally find the out
3411 of scope `i'. We need to issue a warning message,
3412 but then use the global `i'. */
3413 decl = check_for_out_of_scope_variable (decl);
3414 if (local_variable_p (decl))
3416 error ("local variable %qD may not appear in this context",
3418 return error_mark_node;
3423 decl = (finish_id_expression
3424 (id_expression, decl, parser->scope,
3426 parser->integral_constant_expression_p,
3427 parser->allow_non_integral_constant_expression_p,
3428 &parser->non_integral_constant_expression_p,
3429 template_p, done, address_p,
3433 cp_parser_error (parser, error_msg);
3437 /* Anything else is an error. */
3439 /* ...unless we have an Objective-C++ message or string literal,
3441 if (c_dialect_objc ()
3442 && (token->type == CPP_OPEN_SQUARE
3443 || token->type == CPP_OBJC_STRING))
3444 return cp_parser_objc_expression (parser);
3446 cp_parser_error (parser, "expected primary-expression");
3447 return error_mark_node;
3451 /* Parse an id-expression.
3458 :: [opt] nested-name-specifier template [opt] unqualified-id
3460 :: operator-function-id
3463 Return a representation of the unqualified portion of the
3464 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3465 a `::' or nested-name-specifier.
3467 Often, if the id-expression was a qualified-id, the caller will
3468 want to make a SCOPE_REF to represent the qualified-id. This
3469 function does not do this in order to avoid wastefully creating
3470 SCOPE_REFs when they are not required.
3472 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3475 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3476 uninstantiated templates.
3478 If *TEMPLATE_P is non-NULL, it is set to true iff the
3479 `template' keyword is used to explicitly indicate that the entity
3480 named is a template.
3482 If DECLARATOR_P is true, the id-expression is appearing as part of
3483 a declarator, rather than as part of an expression. */
3486 cp_parser_id_expression (cp_parser *parser,
3487 bool template_keyword_p,
3488 bool check_dependency_p,
3493 bool global_scope_p;
3494 bool nested_name_specifier_p;
3496 /* Assume the `template' keyword was not used. */
3498 *template_p = template_keyword_p;
3500 /* Look for the optional `::' operator. */
3502 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3504 /* Look for the optional nested-name-specifier. */
3505 nested_name_specifier_p
3506 = (cp_parser_nested_name_specifier_opt (parser,
3507 /*typename_keyword_p=*/false,
3512 /* If there is a nested-name-specifier, then we are looking at
3513 the first qualified-id production. */
3514 if (nested_name_specifier_p)
3517 tree saved_object_scope;
3518 tree saved_qualifying_scope;
3519 tree unqualified_id;
3522 /* See if the next token is the `template' keyword. */
3524 template_p = &is_template;
3525 *template_p = cp_parser_optional_template_keyword (parser);
3526 /* Name lookup we do during the processing of the
3527 unqualified-id might obliterate SCOPE. */
3528 saved_scope = parser->scope;
3529 saved_object_scope = parser->object_scope;
3530 saved_qualifying_scope = parser->qualifying_scope;
3531 /* Process the final unqualified-id. */
3532 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3535 /*optional_p=*/false);
3536 /* Restore the SAVED_SCOPE for our caller. */
3537 parser->scope = saved_scope;
3538 parser->object_scope = saved_object_scope;
3539 parser->qualifying_scope = saved_qualifying_scope;
3541 return unqualified_id;
3543 /* Otherwise, if we are in global scope, then we are looking at one
3544 of the other qualified-id productions. */
3545 else if (global_scope_p)
3550 /* Peek at the next token. */
3551 token = cp_lexer_peek_token (parser->lexer);
3553 /* If it's an identifier, and the next token is not a "<", then
3554 we can avoid the template-id case. This is an optimization
3555 for this common case. */
3556 if (token->type == CPP_NAME
3557 && !cp_parser_nth_token_starts_template_argument_list_p
3559 return cp_parser_identifier (parser);
3561 cp_parser_parse_tentatively (parser);
3562 /* Try a template-id. */
3563 id = cp_parser_template_id (parser,
3564 /*template_keyword_p=*/false,
3565 /*check_dependency_p=*/true,
3567 /* If that worked, we're done. */
3568 if (cp_parser_parse_definitely (parser))
3571 /* Peek at the next token. (Changes in the token buffer may
3572 have invalidated the pointer obtained above.) */
3573 token = cp_lexer_peek_token (parser->lexer);
3575 switch (token->type)
3578 return cp_parser_identifier (parser);
3581 if (token->keyword == RID_OPERATOR)
3582 return cp_parser_operator_function_id (parser);
3586 cp_parser_error (parser, "expected id-expression");
3587 return error_mark_node;
3591 return cp_parser_unqualified_id (parser, template_keyword_p,
3592 /*check_dependency_p=*/true,
3597 /* Parse an unqualified-id.
3601 operator-function-id
3602 conversion-function-id
3606 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3607 keyword, in a construct like `A::template ...'.
3609 Returns a representation of unqualified-id. For the `identifier'
3610 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3611 production a BIT_NOT_EXPR is returned; the operand of the
3612 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3613 other productions, see the documentation accompanying the
3614 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3615 names are looked up in uninstantiated templates. If DECLARATOR_P
3616 is true, the unqualified-id is appearing as part of a declarator,
3617 rather than as part of an expression. */
3620 cp_parser_unqualified_id (cp_parser* parser,
3621 bool template_keyword_p,
3622 bool check_dependency_p,
3628 /* Peek at the next token. */
3629 token = cp_lexer_peek_token (parser->lexer);
3631 switch (token->type)
3637 /* We don't know yet whether or not this will be a
3639 cp_parser_parse_tentatively (parser);
3640 /* Try a template-id. */
3641 id = cp_parser_template_id (parser, template_keyword_p,
3644 /* If it worked, we're done. */
3645 if (cp_parser_parse_definitely (parser))
3647 /* Otherwise, it's an ordinary identifier. */
3648 return cp_parser_identifier (parser);
3651 case CPP_TEMPLATE_ID:
3652 return cp_parser_template_id (parser, template_keyword_p,
3659 tree qualifying_scope;
3664 /* Consume the `~' token. */
3665 cp_lexer_consume_token (parser->lexer);
3666 /* Parse the class-name. The standard, as written, seems to
3669 template <typename T> struct S { ~S (); };
3670 template <typename T> S<T>::~S() {}
3672 is invalid, since `~' must be followed by a class-name, but
3673 `S<T>' is dependent, and so not known to be a class.
3674 That's not right; we need to look in uninstantiated
3675 templates. A further complication arises from:
3677 template <typename T> void f(T t) {
3681 Here, it is not possible to look up `T' in the scope of `T'
3682 itself. We must look in both the current scope, and the
3683 scope of the containing complete expression.
3685 Yet another issue is:
3694 The standard does not seem to say that the `S' in `~S'
3695 should refer to the type `S' and not the data member
3698 /* DR 244 says that we look up the name after the "~" in the
3699 same scope as we looked up the qualifying name. That idea
3700 isn't fully worked out; it's more complicated than that. */
3701 scope = parser->scope;
3702 object_scope = parser->object_scope;
3703 qualifying_scope = parser->qualifying_scope;
3705 /* Check for invalid scopes. */
3706 if (scope == error_mark_node)
3708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3709 cp_lexer_consume_token (parser->lexer);
3710 return error_mark_node;
3712 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3714 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3715 error ("scope %qT before %<~%> is not a class-name", scope);
3716 cp_parser_simulate_error (parser);
3717 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3718 cp_lexer_consume_token (parser->lexer);
3719 return error_mark_node;
3721 gcc_assert (!scope || TYPE_P (scope));
3723 /* If the name is of the form "X::~X" it's OK. */
3724 token = cp_lexer_peek_token (parser->lexer);
3726 && token->type == CPP_NAME
3727 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3729 && constructor_name_p (token->u.value, scope))
3731 cp_lexer_consume_token (parser->lexer);
3732 return build_nt (BIT_NOT_EXPR, scope);
3735 /* If there was an explicit qualification (S::~T), first look
3736 in the scope given by the qualification (i.e., S). */
3738 type_decl = NULL_TREE;
3741 cp_parser_parse_tentatively (parser);
3742 type_decl = cp_parser_class_name (parser,
3743 /*typename_keyword_p=*/false,
3744 /*template_keyword_p=*/false,
3746 /*check_dependency=*/false,
3747 /*class_head_p=*/false,
3749 if (cp_parser_parse_definitely (parser))
3752 /* In "N::S::~S", look in "N" as well. */
3753 if (!done && scope && qualifying_scope)
3755 cp_parser_parse_tentatively (parser);
3756 parser->scope = qualifying_scope;
3757 parser->object_scope = NULL_TREE;
3758 parser->qualifying_scope = NULL_TREE;
3760 = cp_parser_class_name (parser,
3761 /*typename_keyword_p=*/false,
3762 /*template_keyword_p=*/false,
3764 /*check_dependency=*/false,
3765 /*class_head_p=*/false,
3767 if (cp_parser_parse_definitely (parser))
3770 /* In "p->S::~T", look in the scope given by "*p" as well. */
3771 else if (!done && object_scope)
3773 cp_parser_parse_tentatively (parser);
3774 parser->scope = object_scope;
3775 parser->object_scope = NULL_TREE;
3776 parser->qualifying_scope = NULL_TREE;
3778 = cp_parser_class_name (parser,
3779 /*typename_keyword_p=*/false,
3780 /*template_keyword_p=*/false,
3782 /*check_dependency=*/false,
3783 /*class_head_p=*/false,
3785 if (cp_parser_parse_definitely (parser))
3788 /* Look in the surrounding context. */
3791 parser->scope = NULL_TREE;
3792 parser->object_scope = NULL_TREE;
3793 parser->qualifying_scope = NULL_TREE;
3795 = cp_parser_class_name (parser,
3796 /*typename_keyword_p=*/false,
3797 /*template_keyword_p=*/false,
3799 /*check_dependency=*/false,
3800 /*class_head_p=*/false,
3803 /* If an error occurred, assume that the name of the
3804 destructor is the same as the name of the qualifying
3805 class. That allows us to keep parsing after running
3806 into ill-formed destructor names. */
3807 if (type_decl == error_mark_node && scope)
3808 return build_nt (BIT_NOT_EXPR, scope);
3809 else if (type_decl == error_mark_node)
3810 return error_mark_node;
3812 /* Check that destructor name and scope match. */
3813 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3815 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3816 error ("declaration of %<~%T%> as member of %qT",
3818 cp_parser_simulate_error (parser);
3819 return error_mark_node;
3824 A typedef-name that names a class shall not be used as the
3825 identifier in the declarator for a destructor declaration. */
3827 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3828 && !DECL_SELF_REFERENCE_P (type_decl)
3829 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3830 error ("typedef-name %qD used as destructor declarator",
3833 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3837 if (token->keyword == RID_OPERATOR)
3841 /* This could be a template-id, so we try that first. */
3842 cp_parser_parse_tentatively (parser);
3843 /* Try a template-id. */
3844 id = cp_parser_template_id (parser, template_keyword_p,
3845 /*check_dependency_p=*/true,
3847 /* If that worked, we're done. */
3848 if (cp_parser_parse_definitely (parser))
3850 /* We still don't know whether we're looking at an
3851 operator-function-id or a conversion-function-id. */
3852 cp_parser_parse_tentatively (parser);
3853 /* Try an operator-function-id. */
3854 id = cp_parser_operator_function_id (parser);
3855 /* If that didn't work, try a conversion-function-id. */
3856 if (!cp_parser_parse_definitely (parser))
3857 id = cp_parser_conversion_function_id (parser);
3866 cp_parser_error (parser, "expected unqualified-id");
3867 return error_mark_node;
3871 /* Parse an (optional) nested-name-specifier.
3873 nested-name-specifier:
3874 class-or-namespace-name :: nested-name-specifier [opt]
3875 class-or-namespace-name :: template nested-name-specifier [opt]
3877 PARSER->SCOPE should be set appropriately before this function is
3878 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3879 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3882 Sets PARSER->SCOPE to the class (TYPE) or namespace
3883 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3884 it unchanged if there is no nested-name-specifier. Returns the new
3885 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3887 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3888 part of a declaration and/or decl-specifier. */
3891 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3892 bool typename_keyword_p,
3893 bool check_dependency_p,
3895 bool is_declaration)
3897 bool success = false;
3898 cp_token_position start = 0;
3901 /* Remember where the nested-name-specifier starts. */
3902 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3904 start = cp_lexer_token_position (parser->lexer, false);
3905 push_deferring_access_checks (dk_deferred);
3912 tree saved_qualifying_scope;
3913 bool template_keyword_p;
3915 /* Spot cases that cannot be the beginning of a
3916 nested-name-specifier. */
3917 token = cp_lexer_peek_token (parser->lexer);
3919 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3920 the already parsed nested-name-specifier. */
3921 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3923 /* Grab the nested-name-specifier and continue the loop. */
3924 cp_parser_pre_parsed_nested_name_specifier (parser);
3925 /* If we originally encountered this nested-name-specifier
3926 with IS_DECLARATION set to false, we will not have
3927 resolved TYPENAME_TYPEs, so we must do so here. */
3929 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3931 new_scope = resolve_typename_type (parser->scope,
3932 /*only_current_p=*/false);
3933 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3934 parser->scope = new_scope;
3940 /* Spot cases that cannot be the beginning of a
3941 nested-name-specifier. On the second and subsequent times
3942 through the loop, we look for the `template' keyword. */
3943 if (success && token->keyword == RID_TEMPLATE)
3945 /* A template-id can start a nested-name-specifier. */
3946 else if (token->type == CPP_TEMPLATE_ID)
3950 /* If the next token is not an identifier, then it is
3951 definitely not a class-or-namespace-name. */
3952 if (token->type != CPP_NAME)
3954 /* If the following token is neither a `<' (to begin a
3955 template-id), nor a `::', then we are not looking at a
3956 nested-name-specifier. */
3957 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3958 if (token->type != CPP_SCOPE
3959 && !cp_parser_nth_token_starts_template_argument_list_p
3964 /* The nested-name-specifier is optional, so we parse
3966 cp_parser_parse_tentatively (parser);
3968 /* Look for the optional `template' keyword, if this isn't the
3969 first time through the loop. */
3971 template_keyword_p = cp_parser_optional_template_keyword (parser);
3973 template_keyword_p = false;
3975 /* Save the old scope since the name lookup we are about to do
3976 might destroy it. */
3977 old_scope = parser->scope;
3978 saved_qualifying_scope = parser->qualifying_scope;
3979 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3980 look up names in "X<T>::I" in order to determine that "Y" is
3981 a template. So, if we have a typename at this point, we make
3982 an effort to look through it. */
3984 && !typename_keyword_p
3986 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3987 parser->scope = resolve_typename_type (parser->scope,
3988 /*only_current_p=*/false);
3989 /* Parse the qualifying entity. */
3991 = cp_parser_class_or_namespace_name (parser,
3997 /* Look for the `::' token. */
3998 cp_parser_require (parser, CPP_SCOPE, "`::'");
4000 /* If we found what we wanted, we keep going; otherwise, we're
4002 if (!cp_parser_parse_definitely (parser))
4004 bool error_p = false;
4006 /* Restore the OLD_SCOPE since it was valid before the
4007 failed attempt at finding the last
4008 class-or-namespace-name. */
4009 parser->scope = old_scope;
4010 parser->qualifying_scope = saved_qualifying_scope;
4011 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4013 /* If the next token is an identifier, and the one after
4014 that is a `::', then any valid interpretation would have
4015 found a class-or-namespace-name. */
4016 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4017 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4019 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4022 token = cp_lexer_consume_token (parser->lexer);
4025 if (!token->ambiguous_p)
4028 tree ambiguous_decls;
4030 decl = cp_parser_lookup_name (parser, token->u.value,
4032 /*is_template=*/false,
4033 /*is_namespace=*/false,
4034 /*check_dependency=*/true,
4036 if (TREE_CODE (decl) == TEMPLATE_DECL)
4037 error ("%qD used without template parameters", decl);
4038 else if (ambiguous_decls)
4040 error ("reference to %qD is ambiguous",
4042 print_candidates (ambiguous_decls);
4043 decl = error_mark_node;
4046 cp_parser_name_lookup_error
4047 (parser, token->u.value, decl,
4048 "is not a class or namespace");
4050 parser->scope = error_mark_node;
4052 /* Treat this as a successful nested-name-specifier
4057 If the name found is not a class-name (clause
4058 _class_) or namespace-name (_namespace.def_), the
4059 program is ill-formed. */
4062 cp_lexer_consume_token (parser->lexer);
4066 /* We've found one valid nested-name-specifier. */
4068 /* Name lookup always gives us a DECL. */
4069 if (TREE_CODE (new_scope) == TYPE_DECL)
4070 new_scope = TREE_TYPE (new_scope);
4071 /* Uses of "template" must be followed by actual templates. */
4072 if (template_keyword_p
4073 && !(CLASS_TYPE_P (new_scope)
4074 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4075 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4076 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4077 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4078 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4079 == TEMPLATE_ID_EXPR)))
4080 pedwarn (TYPE_P (new_scope)
4081 ? "%qT is not a template"
4082 : "%qD is not a template",
4084 /* If it is a class scope, try to complete it; we are about to
4085 be looking up names inside the class. */
4086 if (TYPE_P (new_scope)
4087 /* Since checking types for dependency can be expensive,
4088 avoid doing it if the type is already complete. */
4089 && !COMPLETE_TYPE_P (new_scope)
4090 /* Do not try to complete dependent types. */
4091 && !dependent_type_p (new_scope))
4093 new_scope = complete_type (new_scope);
4094 /* If it is a typedef to current class, use the current
4095 class instead, as the typedef won't have any names inside
4097 if (!COMPLETE_TYPE_P (new_scope)
4098 && currently_open_class (new_scope))
4099 new_scope = TYPE_MAIN_VARIANT (new_scope);
4101 /* Make sure we look in the right scope the next time through
4103 parser->scope = new_scope;
4106 /* If parsing tentatively, replace the sequence of tokens that makes
4107 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4108 token. That way, should we re-parse the token stream, we will
4109 not have to repeat the effort required to do the parse, nor will
4110 we issue duplicate error messages. */
4111 if (success && start)
4115 token = cp_lexer_token_at (parser->lexer, start);
4116 /* Reset the contents of the START token. */
4117 token->type = CPP_NESTED_NAME_SPECIFIER;
4118 /* Retrieve any deferred checks. Do not pop this access checks yet
4119 so the memory will not be reclaimed during token replacing below. */
4120 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4121 token->u.tree_check_value->value = parser->scope;
4122 token->u.tree_check_value->checks = get_deferred_access_checks ();
4123 token->u.tree_check_value->qualifying_scope =
4124 parser->qualifying_scope;
4125 token->keyword = RID_MAX;
4127 /* Purge all subsequent tokens. */
4128 cp_lexer_purge_tokens_after (parser->lexer, start);
4132 pop_to_parent_deferring_access_checks ();
4134 return success ? parser->scope : NULL_TREE;
4137 /* Parse a nested-name-specifier. See
4138 cp_parser_nested_name_specifier_opt for details. This function
4139 behaves identically, except that it will an issue an error if no
4140 nested-name-specifier is present. */
4143 cp_parser_nested_name_specifier (cp_parser *parser,
4144 bool typename_keyword_p,
4145 bool check_dependency_p,
4147 bool is_declaration)
4151 /* Look for the nested-name-specifier. */
4152 scope = cp_parser_nested_name_specifier_opt (parser,
4157 /* If it was not present, issue an error message. */
4160 cp_parser_error (parser, "expected nested-name-specifier");
4161 parser->scope = NULL_TREE;
4167 /* Parse a class-or-namespace-name.
4169 class-or-namespace-name:
4173 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4174 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4175 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4176 TYPE_P is TRUE iff the next name should be taken as a class-name,
4177 even the same name is declared to be another entity in the same
4180 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4181 specified by the class-or-namespace-name. If neither is found the
4182 ERROR_MARK_NODE is returned. */
4185 cp_parser_class_or_namespace_name (cp_parser *parser,
4186 bool typename_keyword_p,
4187 bool template_keyword_p,
4188 bool check_dependency_p,
4190 bool is_declaration)
4193 tree saved_qualifying_scope;
4194 tree saved_object_scope;
4198 /* Before we try to parse the class-name, we must save away the
4199 current PARSER->SCOPE since cp_parser_class_name will destroy
4201 saved_scope = parser->scope;
4202 saved_qualifying_scope = parser->qualifying_scope;
4203 saved_object_scope = parser->object_scope;
4204 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4205 there is no need to look for a namespace-name. */
4206 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4208 cp_parser_parse_tentatively (parser);
4209 scope = cp_parser_class_name (parser,
4212 type_p ? class_type : none_type,
4214 /*class_head_p=*/false,
4216 /* If that didn't work, try for a namespace-name. */
4217 if (!only_class_p && !cp_parser_parse_definitely (parser))
4219 /* Restore the saved scope. */
4220 parser->scope = saved_scope;
4221 parser->qualifying_scope = saved_qualifying_scope;
4222 parser->object_scope = saved_object_scope;
4223 /* If we are not looking at an identifier followed by the scope
4224 resolution operator, then this is not part of a
4225 nested-name-specifier. (Note that this function is only used
4226 to parse the components of a nested-name-specifier.) */
4227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4228 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4229 return error_mark_node;
4230 scope = cp_parser_namespace_name (parser);
4236 /* Parse a postfix-expression.
4240 postfix-expression [ expression ]
4241 postfix-expression ( expression-list [opt] )
4242 simple-type-specifier ( expression-list [opt] )
4243 typename :: [opt] nested-name-specifier identifier
4244 ( expression-list [opt] )
4245 typename :: [opt] nested-name-specifier template [opt] template-id
4246 ( expression-list [opt] )
4247 postfix-expression . template [opt] id-expression
4248 postfix-expression -> template [opt] id-expression
4249 postfix-expression . pseudo-destructor-name
4250 postfix-expression -> pseudo-destructor-name
4251 postfix-expression ++
4252 postfix-expression --
4253 dynamic_cast < type-id > ( expression )
4254 static_cast < type-id > ( expression )
4255 reinterpret_cast < type-id > ( expression )
4256 const_cast < type-id > ( expression )
4257 typeid ( expression )
4263 ( type-id ) { initializer-list , [opt] }
4265 This extension is a GNU version of the C99 compound-literal
4266 construct. (The C99 grammar uses `type-name' instead of `type-id',
4267 but they are essentially the same concept.)
4269 If ADDRESS_P is true, the postfix expression is the operand of the
4270 `&' operator. CAST_P is true if this expression is the target of a
4273 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4274 class member access expressions [expr.ref].
4276 Returns a representation of the expression. */
4279 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4280 bool member_access_only_p)
4284 cp_id_kind idk = CP_ID_KIND_NONE;
4285 tree postfix_expression = NULL_TREE;
4286 bool is_member_access = false;
4288 /* Peek at the next token. */
4289 token = cp_lexer_peek_token (parser->lexer);
4290 /* Some of the productions are determined by keywords. */
4291 keyword = token->keyword;
4301 const char *saved_message;
4303 /* All of these can be handled in the same way from the point
4304 of view of parsing. Begin by consuming the token
4305 identifying the cast. */
4306 cp_lexer_consume_token (parser->lexer);
4308 /* New types cannot be defined in the cast. */
4309 saved_message = parser->type_definition_forbidden_message;
4310 parser->type_definition_forbidden_message
4311 = "types may not be defined in casts";
4313 /* Look for the opening `<'. */
4314 cp_parser_require (parser, CPP_LESS, "`<'");
4315 /* Parse the type to which we are casting. */
4316 type = cp_parser_type_id (parser);
4317 /* Look for the closing `>'. */
4318 cp_parser_require (parser, CPP_GREATER, "`>'");
4319 /* Restore the old message. */
4320 parser->type_definition_forbidden_message = saved_message;
4322 /* And the expression which is being cast. */
4323 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4324 expression = cp_parser_expression (parser, /*cast_p=*/true);
4325 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4327 /* Only type conversions to integral or enumeration types
4328 can be used in constant-expressions. */
4329 if (!cast_valid_in_integral_constant_expression_p (type)
4330 && (cp_parser_non_integral_constant_expression
4332 "a cast to a type other than an integral or "
4333 "enumeration type")))
4334 return error_mark_node;
4340 = build_dynamic_cast (type, expression);
4344 = build_static_cast (type, expression);
4348 = build_reinterpret_cast (type, expression);
4352 = build_const_cast (type, expression);
4363 const char *saved_message;
4364 bool saved_in_type_id_in_expr_p;
4366 /* Consume the `typeid' token. */
4367 cp_lexer_consume_token (parser->lexer);
4368 /* Look for the `(' token. */
4369 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4370 /* Types cannot be defined in a `typeid' expression. */
4371 saved_message = parser->type_definition_forbidden_message;
4372 parser->type_definition_forbidden_message
4373 = "types may not be defined in a `typeid\' expression";
4374 /* We can't be sure yet whether we're looking at a type-id or an
4376 cp_parser_parse_tentatively (parser);
4377 /* Try a type-id first. */
4378 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4379 parser->in_type_id_in_expr_p = true;
4380 type = cp_parser_type_id (parser);
4381 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4382 /* Look for the `)' token. Otherwise, we can't be sure that
4383 we're not looking at an expression: consider `typeid (int
4384 (3))', for example. */
4385 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4386 /* If all went well, simply lookup the type-id. */
4387 if (cp_parser_parse_definitely (parser))
4388 postfix_expression = get_typeid (type);
4389 /* Otherwise, fall back to the expression variant. */
4394 /* Look for an expression. */
4395 expression = cp_parser_expression (parser, /*cast_p=*/false);
4396 /* Compute its typeid. */
4397 postfix_expression = build_typeid (expression);
4398 /* Look for the `)' token. */
4399 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4401 /* Restore the saved message. */
4402 parser->type_definition_forbidden_message = saved_message;
4403 /* `typeid' may not appear in an integral constant expression. */
4404 if (cp_parser_non_integral_constant_expression(parser,
4405 "`typeid' operator"))
4406 return error_mark_node;
4413 /* The syntax permitted here is the same permitted for an
4414 elaborated-type-specifier. */
4415 type = cp_parser_elaborated_type_specifier (parser,
4416 /*is_friend=*/false,
4417 /*is_declaration=*/false);
4418 postfix_expression = cp_parser_functional_cast (parser, type);
4426 /* If the next thing is a simple-type-specifier, we may be
4427 looking at a functional cast. We could also be looking at
4428 an id-expression. So, we try the functional cast, and if
4429 that doesn't work we fall back to the primary-expression. */
4430 cp_parser_parse_tentatively (parser);
4431 /* Look for the simple-type-specifier. */
4432 type = cp_parser_simple_type_specifier (parser,
4433 /*decl_specs=*/NULL,
4434 CP_PARSER_FLAGS_NONE);
4435 /* Parse the cast itself. */
4436 if (!cp_parser_error_occurred (parser))
4438 = cp_parser_functional_cast (parser, type);
4439 /* If that worked, we're done. */
4440 if (cp_parser_parse_definitely (parser))
4443 /* If the functional-cast didn't work out, try a
4444 compound-literal. */
4445 if (cp_parser_allow_gnu_extensions_p (parser)
4446 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4448 VEC(constructor_elt,gc) *initializer_list = NULL;
4449 bool saved_in_type_id_in_expr_p;
4451 cp_parser_parse_tentatively (parser);
4452 /* Consume the `('. */
4453 cp_lexer_consume_token (parser->lexer);
4454 /* Parse the type. */
4455 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4456 parser->in_type_id_in_expr_p = true;
4457 type = cp_parser_type_id (parser);
4458 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4459 /* Look for the `)'. */
4460 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4461 /* Look for the `{'. */
4462 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4463 /* If things aren't going well, there's no need to
4465 if (!cp_parser_error_occurred (parser))
4467 bool non_constant_p;
4468 /* Parse the initializer-list. */
4470 = cp_parser_initializer_list (parser, &non_constant_p);
4471 /* Allow a trailing `,'. */
4472 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4473 cp_lexer_consume_token (parser->lexer);
4474 /* Look for the final `}'. */
4475 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4477 /* If that worked, we're definitely looking at a
4478 compound-literal expression. */
4479 if (cp_parser_parse_definitely (parser))
4481 /* Warn the user that a compound literal is not
4482 allowed in standard C++. */
4484 pedwarn ("ISO C++ forbids compound-literals");
4485 /* For simplicity, we disallow compound literals in
4486 constant-expressions. We could
4487 allow compound literals of integer type, whose
4488 initializer was a constant, in constant
4489 expressions. Permitting that usage, as a further
4490 extension, would not change the meaning of any
4491 currently accepted programs. (Of course, as
4492 compound literals are not part of ISO C++, the
4493 standard has nothing to say.) */
4494 if (cp_parser_non_integral_constant_expression
4495 (parser, "non-constant compound literals"))
4497 postfix_expression = error_mark_node;
4500 /* Form the representation of the compound-literal. */
4502 = finish_compound_literal (type, initializer_list);
4507 /* It must be a primary-expression. */
4509 = cp_parser_primary_expression (parser, address_p, cast_p,
4510 /*template_arg_p=*/false,
4516 /* Keep looping until the postfix-expression is complete. */
4519 if (idk == CP_ID_KIND_UNQUALIFIED
4520 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4521 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4522 /* It is not a Koenig lookup function call. */
4524 = unqualified_name_lookup_error (postfix_expression);
4526 /* Peek at the next token. */
4527 token = cp_lexer_peek_token (parser->lexer);
4529 switch (token->type)
4531 case CPP_OPEN_SQUARE:
4533 = cp_parser_postfix_open_square_expression (parser,
4536 idk = CP_ID_KIND_NONE;
4537 is_member_access = false;
4540 case CPP_OPEN_PAREN:
4541 /* postfix-expression ( expression-list [opt] ) */
4544 bool is_builtin_constant_p;
4545 bool saved_integral_constant_expression_p = false;
4546 bool saved_non_integral_constant_expression_p = false;
4549 is_member_access = false;
4551 is_builtin_constant_p
4552 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4553 if (is_builtin_constant_p)
4555 /* The whole point of __builtin_constant_p is to allow
4556 non-constant expressions to appear as arguments. */
4557 saved_integral_constant_expression_p
4558 = parser->integral_constant_expression_p;
4559 saved_non_integral_constant_expression_p
4560 = parser->non_integral_constant_expression_p;
4561 parser->integral_constant_expression_p = false;
4563 args = (cp_parser_parenthesized_expression_list
4564 (parser, /*is_attribute_list=*/false,
4565 /*cast_p=*/false, /*allow_expansion_p=*/true,
4566 /*non_constant_p=*/NULL));
4567 if (is_builtin_constant_p)
4569 parser->integral_constant_expression_p
4570 = saved_integral_constant_expression_p;
4571 parser->non_integral_constant_expression_p
4572 = saved_non_integral_constant_expression_p;
4575 if (args == error_mark_node)
4577 postfix_expression = error_mark_node;
4581 /* Function calls are not permitted in
4582 constant-expressions. */
4583 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4584 && cp_parser_non_integral_constant_expression (parser,
4587 postfix_expression = error_mark_node;
4592 if (idk == CP_ID_KIND_UNQUALIFIED)
4594 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4600 = perform_koenig_lookup (postfix_expression, args);
4604 = unqualified_fn_lookup_error (postfix_expression);
4606 /* We do not perform argument-dependent lookup if
4607 normal lookup finds a non-function, in accordance
4608 with the expected resolution of DR 218. */
4609 else if (args && is_overloaded_fn (postfix_expression))
4611 tree fn = get_first_fn (postfix_expression);
4613 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4614 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4616 /* Only do argument dependent lookup if regular
4617 lookup does not find a set of member functions.
4618 [basic.lookup.koenig]/2a */
4619 if (!DECL_FUNCTION_MEMBER_P (fn))
4623 = perform_koenig_lookup (postfix_expression, args);
4628 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4630 tree instance = TREE_OPERAND (postfix_expression, 0);
4631 tree fn = TREE_OPERAND (postfix_expression, 1);
4633 if (processing_template_decl
4634 && (type_dependent_expression_p (instance)
4635 || (!BASELINK_P (fn)
4636 && TREE_CODE (fn) != FIELD_DECL)
4637 || type_dependent_expression_p (fn)
4638 || any_type_dependent_arguments_p (args)))
4641 = build_nt_call_list (postfix_expression, args);
4645 if (BASELINK_P (fn))
4647 = (build_new_method_call
4648 (instance, fn, args, NULL_TREE,
4649 (idk == CP_ID_KIND_QUALIFIED
4650 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4654 = finish_call_expr (postfix_expression, args,
4655 /*disallow_virtual=*/false,
4656 /*koenig_p=*/false);
4658 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4659 || TREE_CODE (postfix_expression) == MEMBER_REF
4660 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4661 postfix_expression = (build_offset_ref_call_from_tree
4662 (postfix_expression, args));
4663 else if (idk == CP_ID_KIND_QUALIFIED)
4664 /* A call to a static class member, or a namespace-scope
4667 = finish_call_expr (postfix_expression, args,
4668 /*disallow_virtual=*/true,
4671 /* All other function calls. */
4673 = finish_call_expr (postfix_expression, args,
4674 /*disallow_virtual=*/false,
4677 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4678 idk = CP_ID_KIND_NONE;
4684 /* postfix-expression . template [opt] id-expression
4685 postfix-expression . pseudo-destructor-name
4686 postfix-expression -> template [opt] id-expression
4687 postfix-expression -> pseudo-destructor-name */
4689 /* Consume the `.' or `->' operator. */
4690 cp_lexer_consume_token (parser->lexer);
4693 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4697 is_member_access = true;
4701 /* postfix-expression ++ */
4702 /* Consume the `++' token. */
4703 cp_lexer_consume_token (parser->lexer);
4704 /* Generate a representation for the complete expression. */
4706 = finish_increment_expr (postfix_expression,
4707 POSTINCREMENT_EXPR);
4708 /* Increments may not appear in constant-expressions. */
4709 if (cp_parser_non_integral_constant_expression (parser,
4711 postfix_expression = error_mark_node;
4712 idk = CP_ID_KIND_NONE;
4713 is_member_access = false;
4716 case CPP_MINUS_MINUS:
4717 /* postfix-expression -- */
4718 /* Consume the `--' token. */
4719 cp_lexer_consume_token (parser->lexer);
4720 /* Generate a representation for the complete expression. */
4722 = finish_increment_expr (postfix_expression,
4723 POSTDECREMENT_EXPR);
4724 /* Decrements may not appear in constant-expressions. */
4725 if (cp_parser_non_integral_constant_expression (parser,
4727 postfix_expression = error_mark_node;
4728 idk = CP_ID_KIND_NONE;
4729 is_member_access = false;
4733 if (member_access_only_p)
4734 return is_member_access? postfix_expression : error_mark_node;
4736 return postfix_expression;
4740 /* We should never get here. */
4742 return error_mark_node;
4745 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4746 by cp_parser_builtin_offsetof. We're looking for
4748 postfix-expression [ expression ]
4750 FOR_OFFSETOF is set if we're being called in that context, which
4751 changes how we deal with integer constant expressions. */
4754 cp_parser_postfix_open_square_expression (cp_parser *parser,
4755 tree postfix_expression,
4760 /* Consume the `[' token. */
4761 cp_lexer_consume_token (parser->lexer);
4763 /* Parse the index expression. */
4764 /* ??? For offsetof, there is a question of what to allow here. If
4765 offsetof is not being used in an integral constant expression context,
4766 then we *could* get the right answer by computing the value at runtime.
4767 If we are in an integral constant expression context, then we might
4768 could accept any constant expression; hard to say without analysis.
4769 Rather than open the barn door too wide right away, allow only integer
4770 constant expressions here. */
4772 index = cp_parser_constant_expression (parser, false, NULL);
4774 index = cp_parser_expression (parser, /*cast_p=*/false);
4776 /* Look for the closing `]'. */
4777 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4779 /* Build the ARRAY_REF. */
4780 postfix_expression = grok_array_decl (postfix_expression, index);
4782 /* When not doing offsetof, array references are not permitted in
4783 constant-expressions. */
4785 && (cp_parser_non_integral_constant_expression
4786 (parser, "an array reference")))
4787 postfix_expression = error_mark_node;
4789 return postfix_expression;
4792 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4793 by cp_parser_builtin_offsetof. We're looking for
4795 postfix-expression . template [opt] id-expression
4796 postfix-expression . pseudo-destructor-name
4797 postfix-expression -> template [opt] id-expression
4798 postfix-expression -> pseudo-destructor-name
4800 FOR_OFFSETOF is set if we're being called in that context. That sorta
4801 limits what of the above we'll actually accept, but nevermind.
4802 TOKEN_TYPE is the "." or "->" token, which will already have been
4803 removed from the stream. */
4806 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4807 enum cpp_ttype token_type,
4808 tree postfix_expression,
4809 bool for_offsetof, cp_id_kind *idk)
4813 bool pseudo_destructor_p;
4814 tree scope = NULL_TREE;
4816 /* If this is a `->' operator, dereference the pointer. */
4817 if (token_type == CPP_DEREF)
4818 postfix_expression = build_x_arrow (postfix_expression);
4819 /* Check to see whether or not the expression is type-dependent. */
4820 dependent_p = type_dependent_expression_p (postfix_expression);
4821 /* The identifier following the `->' or `.' is not qualified. */
4822 parser->scope = NULL_TREE;
4823 parser->qualifying_scope = NULL_TREE;
4824 parser->object_scope = NULL_TREE;
4825 *idk = CP_ID_KIND_NONE;
4826 /* Enter the scope corresponding to the type of the object
4827 given by the POSTFIX_EXPRESSION. */
4828 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4830 scope = TREE_TYPE (postfix_expression);
4831 /* According to the standard, no expression should ever have
4832 reference type. Unfortunately, we do not currently match
4833 the standard in this respect in that our internal representation
4834 of an expression may have reference type even when the standard
4835 says it does not. Therefore, we have to manually obtain the
4836 underlying type here. */
4837 scope = non_reference (scope);
4838 /* The type of the POSTFIX_EXPRESSION must be complete. */
4839 if (scope == unknown_type_node)
4841 error ("%qE does not have class type", postfix_expression);
4845 scope = complete_type_or_else (scope, NULL_TREE);
4846 /* Let the name lookup machinery know that we are processing a
4847 class member access expression. */
4848 parser->context->object_type = scope;
4849 /* If something went wrong, we want to be able to discern that case,
4850 as opposed to the case where there was no SCOPE due to the type
4851 of expression being dependent. */
4853 scope = error_mark_node;
4854 /* If the SCOPE was erroneous, make the various semantic analysis
4855 functions exit quickly -- and without issuing additional error
4857 if (scope == error_mark_node)
4858 postfix_expression = error_mark_node;
4861 /* Assume this expression is not a pseudo-destructor access. */
4862 pseudo_destructor_p = false;
4864 /* If the SCOPE is a scalar type, then, if this is a valid program,
4865 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4866 is type dependent, it can be pseudo-destructor-name or something else.
4867 Try to parse it as pseudo-destructor-name first. */
4868 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4873 cp_parser_parse_tentatively (parser);
4874 /* Parse the pseudo-destructor-name. */
4876 cp_parser_pseudo_destructor_name (parser, &s, &type);
4878 && (cp_parser_error_occurred (parser)
4879 || TREE_CODE (type) != TYPE_DECL
4880 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4881 cp_parser_abort_tentative_parse (parser);
4882 else if (cp_parser_parse_definitely (parser))
4884 pseudo_destructor_p = true;
4886 = finish_pseudo_destructor_expr (postfix_expression,
4887 s, TREE_TYPE (type));
4891 if (!pseudo_destructor_p)
4893 /* If the SCOPE is not a scalar type, we are looking at an
4894 ordinary class member access expression, rather than a
4895 pseudo-destructor-name. */
4897 /* Parse the id-expression. */
4898 name = (cp_parser_id_expression
4900 cp_parser_optional_template_keyword (parser),
4901 /*check_dependency_p=*/true,
4903 /*declarator_p=*/false,
4904 /*optional_p=*/false));
4905 /* In general, build a SCOPE_REF if the member name is qualified.
4906 However, if the name was not dependent and has already been
4907 resolved; there is no need to build the SCOPE_REF. For example;
4909 struct X { void f(); };
4910 template <typename T> void f(T* t) { t->X::f(); }
4912 Even though "t" is dependent, "X::f" is not and has been resolved
4913 to a BASELINK; there is no need to include scope information. */
4915 /* But we do need to remember that there was an explicit scope for
4916 virtual function calls. */
4918 *idk = CP_ID_KIND_QUALIFIED;
4920 /* If the name is a template-id that names a type, we will get a
4921 TYPE_DECL here. That is invalid code. */
4922 if (TREE_CODE (name) == TYPE_DECL)
4924 error ("invalid use of %qD", name);
4925 postfix_expression = error_mark_node;
4929 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4931 name = build_qualified_name (/*type=*/NULL_TREE,
4935 parser->scope = NULL_TREE;
4936 parser->qualifying_scope = NULL_TREE;
4937 parser->object_scope = NULL_TREE;
4939 if (scope && name && BASELINK_P (name))
4940 adjust_result_of_qualified_name_lookup
4941 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4943 = finish_class_member_access_expr (postfix_expression, name,
4948 /* We no longer need to look up names in the scope of the object on
4949 the left-hand side of the `.' or `->' operator. */
4950 parser->context->object_type = NULL_TREE;
4952 /* Outside of offsetof, these operators may not appear in
4953 constant-expressions. */
4955 && (cp_parser_non_integral_constant_expression
4956 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4957 postfix_expression = error_mark_node;
4959 return postfix_expression;
4962 /* Parse a parenthesized expression-list.
4965 assignment-expression
4966 expression-list, assignment-expression
4971 identifier, expression-list
4973 CAST_P is true if this expression is the target of a cast.
4975 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4978 Returns a TREE_LIST. The TREE_VALUE of each node is a
4979 representation of an assignment-expression. Note that a TREE_LIST
4980 is returned even if there is only a single expression in the list.
4981 error_mark_node is returned if the ( and or ) are
4982 missing. NULL_TREE is returned on no expressions. The parentheses
4983 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4984 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4985 indicates whether or not all of the expressions in the list were
4989 cp_parser_parenthesized_expression_list (cp_parser* parser,
4990 bool is_attribute_list,
4992 bool allow_expansion_p,
4993 bool *non_constant_p)
4995 tree expression_list = NULL_TREE;
4996 bool fold_expr_p = is_attribute_list;
4997 tree identifier = NULL_TREE;
4998 bool saved_greater_than_is_operator_p;
5000 /* Assume all the expressions will be constant. */
5002 *non_constant_p = false;
5004 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5005 return error_mark_node;
5007 /* Within a parenthesized expression, a `>' token is always
5008 the greater-than operator. */
5009 saved_greater_than_is_operator_p
5010 = parser->greater_than_is_operator_p;
5011 parser->greater_than_is_operator_p = true;
5013 /* Consume expressions until there are no more. */
5014 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5019 /* At the beginning of attribute lists, check to see if the
5020 next token is an identifier. */
5021 if (is_attribute_list
5022 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5026 /* Consume the identifier. */
5027 token = cp_lexer_consume_token (parser->lexer);
5028 /* Save the identifier. */
5029 identifier = token->u.value;
5033 /* Parse the next assignment-expression. */
5036 bool expr_non_constant_p;
5037 expr = (cp_parser_constant_expression
5038 (parser, /*allow_non_constant_p=*/true,
5039 &expr_non_constant_p));
5040 if (expr_non_constant_p)
5041 *non_constant_p = true;
5044 expr = cp_parser_assignment_expression (parser, cast_p);
5047 expr = fold_non_dependent_expr (expr);
5049 /* If we have an ellipsis, then this is an expression
5051 if (allow_expansion_p
5052 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5054 /* Consume the `...'. */
5055 cp_lexer_consume_token (parser->lexer);
5057 /* Build the argument pack. */
5058 expr = make_pack_expansion (expr);
5061 /* Add it to the list. We add error_mark_node
5062 expressions to the list, so that we can still tell if
5063 the correct form for a parenthesized expression-list
5064 is found. That gives better errors. */
5065 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5067 if (expr == error_mark_node)
5071 /* After the first item, attribute lists look the same as
5072 expression lists. */
5073 is_attribute_list = false;
5076 /* If the next token isn't a `,', then we are done. */
5077 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5080 /* Otherwise, consume the `,' and keep going. */
5081 cp_lexer_consume_token (parser->lexer);
5084 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5089 /* We try and resync to an unnested comma, as that will give the
5090 user better diagnostics. */
5091 ending = cp_parser_skip_to_closing_parenthesis (parser,
5092 /*recovering=*/true,
5094 /*consume_paren=*/true);
5099 parser->greater_than_is_operator_p
5100 = saved_greater_than_is_operator_p;
5101 return error_mark_node;
5105 parser->greater_than_is_operator_p
5106 = saved_greater_than_is_operator_p;
5108 /* We built up the list in reverse order so we must reverse it now. */
5109 expression_list = nreverse (expression_list);
5111 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5113 return expression_list;
5116 /* Parse a pseudo-destructor-name.
5118 pseudo-destructor-name:
5119 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5120 :: [opt] nested-name-specifier template template-id :: ~ type-name
5121 :: [opt] nested-name-specifier [opt] ~ type-name
5123 If either of the first two productions is used, sets *SCOPE to the
5124 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5125 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5126 or ERROR_MARK_NODE if the parse fails. */
5129 cp_parser_pseudo_destructor_name (cp_parser* parser,
5133 bool nested_name_specifier_p;
5135 /* Assume that things will not work out. */
5136 *type = error_mark_node;
5138 /* Look for the optional `::' operator. */
5139 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5140 /* Look for the optional nested-name-specifier. */
5141 nested_name_specifier_p
5142 = (cp_parser_nested_name_specifier_opt (parser,
5143 /*typename_keyword_p=*/false,
5144 /*check_dependency_p=*/true,
5146 /*is_declaration=*/true)
5148 /* Now, if we saw a nested-name-specifier, we might be doing the
5149 second production. */
5150 if (nested_name_specifier_p
5151 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5153 /* Consume the `template' keyword. */
5154 cp_lexer_consume_token (parser->lexer);
5155 /* Parse the template-id. */
5156 cp_parser_template_id (parser,
5157 /*template_keyword_p=*/true,
5158 /*check_dependency_p=*/false,
5159 /*is_declaration=*/true);
5160 /* Look for the `::' token. */
5161 cp_parser_require (parser, CPP_SCOPE, "`::'");
5163 /* If the next token is not a `~', then there might be some
5164 additional qualification. */
5165 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5167 /* Look for the type-name. */
5168 *scope = TREE_TYPE (cp_parser_type_name (parser));
5170 if (*scope == error_mark_node)
5173 /* If we don't have ::~, then something has gone wrong. Since
5174 the only caller of this function is looking for something
5175 after `.' or `->' after a scalar type, most likely the
5176 program is trying to get a member of a non-aggregate
5178 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5179 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5181 cp_parser_error (parser, "request for member of non-aggregate type");
5185 /* Look for the `::' token. */
5186 cp_parser_require (parser, CPP_SCOPE, "`::'");
5191 /* Look for the `~'. */
5192 cp_parser_require (parser, CPP_COMPL, "`~'");
5193 /* Look for the type-name again. We are not responsible for
5194 checking that it matches the first type-name. */
5195 *type = cp_parser_type_name (parser);
5198 /* Parse a unary-expression.
5204 unary-operator cast-expression
5205 sizeof unary-expression
5213 __extension__ cast-expression
5214 __alignof__ unary-expression
5215 __alignof__ ( type-id )
5216 __real__ cast-expression
5217 __imag__ cast-expression
5220 ADDRESS_P is true iff the unary-expression is appearing as the
5221 operand of the `&' operator. CAST_P is true if this expression is
5222 the target of a cast.
5224 Returns a representation of the expression. */
5227 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5230 enum tree_code unary_operator;
5232 /* Peek at the next token. */
5233 token = cp_lexer_peek_token (parser->lexer);
5234 /* Some keywords give away the kind of expression. */
5235 if (token->type == CPP_KEYWORD)
5237 enum rid keyword = token->keyword;
5247 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5248 /* Consume the token. */
5249 cp_lexer_consume_token (parser->lexer);
5250 /* Parse the operand. */
5251 operand = cp_parser_sizeof_operand (parser, keyword);
5253 if (TYPE_P (operand))
5254 return cxx_sizeof_or_alignof_type (operand, op, true);
5256 return cxx_sizeof_or_alignof_expr (operand, op);
5260 return cp_parser_new_expression (parser);
5263 return cp_parser_delete_expression (parser);
5267 /* The saved value of the PEDANTIC flag. */
5271 /* Save away the PEDANTIC flag. */
5272 cp_parser_extension_opt (parser, &saved_pedantic);
5273 /* Parse the cast-expression. */
5274 expr = cp_parser_simple_cast_expression (parser);
5275 /* Restore the PEDANTIC flag. */
5276 pedantic = saved_pedantic;
5286 /* Consume the `__real__' or `__imag__' token. */
5287 cp_lexer_consume_token (parser->lexer);
5288 /* Parse the cast-expression. */
5289 expression = cp_parser_simple_cast_expression (parser);
5290 /* Create the complete representation. */
5291 return build_x_unary_op ((keyword == RID_REALPART
5292 ? REALPART_EXPR : IMAGPART_EXPR),
5302 /* Look for the `:: new' and `:: delete', which also signal the
5303 beginning of a new-expression, or delete-expression,
5304 respectively. If the next token is `::', then it might be one of
5306 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5310 /* See if the token after the `::' is one of the keywords in
5311 which we're interested. */
5312 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5313 /* If it's `new', we have a new-expression. */
5314 if (keyword == RID_NEW)
5315 return cp_parser_new_expression (parser);
5316 /* Similarly, for `delete'. */
5317 else if (keyword == RID_DELETE)
5318 return cp_parser_delete_expression (parser);
5321 /* Look for a unary operator. */
5322 unary_operator = cp_parser_unary_operator (token);
5323 /* The `++' and `--' operators can be handled similarly, even though
5324 they are not technically unary-operators in the grammar. */
5325 if (unary_operator == ERROR_MARK)
5327 if (token->type == CPP_PLUS_PLUS)
5328 unary_operator = PREINCREMENT_EXPR;
5329 else if (token->type == CPP_MINUS_MINUS)
5330 unary_operator = PREDECREMENT_EXPR;
5331 /* Handle the GNU address-of-label extension. */
5332 else if (cp_parser_allow_gnu_extensions_p (parser)
5333 && token->type == CPP_AND_AND)
5338 /* Consume the '&&' token. */
5339 cp_lexer_consume_token (parser->lexer);
5340 /* Look for the identifier. */
5341 identifier = cp_parser_identifier (parser);
5342 /* Create an expression representing the address. */
5343 expression = finish_label_address_expr (identifier);
5344 if (cp_parser_non_integral_constant_expression (parser,
5345 "the address of a label"))
5346 expression = error_mark_node;
5350 if (unary_operator != ERROR_MARK)
5352 tree cast_expression;
5353 tree expression = error_mark_node;
5354 const char *non_constant_p = NULL;
5356 /* Consume the operator token. */
5357 token = cp_lexer_consume_token (parser->lexer);
5358 /* Parse the cast-expression. */
5360 = cp_parser_cast_expression (parser,
5361 unary_operator == ADDR_EXPR,
5363 /* Now, build an appropriate representation. */
5364 switch (unary_operator)
5367 non_constant_p = "`*'";
5368 expression = build_x_indirect_ref (cast_expression, "unary *");
5372 non_constant_p = "`&'";
5375 expression = build_x_unary_op (unary_operator, cast_expression);
5378 case PREINCREMENT_EXPR:
5379 case PREDECREMENT_EXPR:
5380 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5383 case UNARY_PLUS_EXPR:
5385 case TRUTH_NOT_EXPR:
5386 expression = finish_unary_op_expr (unary_operator, cast_expression);
5394 && cp_parser_non_integral_constant_expression (parser,
5396 expression = error_mark_node;
5401 return cp_parser_postfix_expression (parser, address_p, cast_p,
5402 /*member_access_only_p=*/false);
5405 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5406 unary-operator, the corresponding tree code is returned. */
5408 static enum tree_code
5409 cp_parser_unary_operator (cp_token* token)
5411 switch (token->type)
5414 return INDIRECT_REF;
5420 return UNARY_PLUS_EXPR;
5426 return TRUTH_NOT_EXPR;
5429 return BIT_NOT_EXPR;
5436 /* Parse a new-expression.
5439 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5440 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5442 Returns a representation of the expression. */
5445 cp_parser_new_expression (cp_parser* parser)
5447 bool global_scope_p;
5453 /* Look for the optional `::' operator. */
5455 = (cp_parser_global_scope_opt (parser,
5456 /*current_scope_valid_p=*/false)
5458 /* Look for the `new' operator. */
5459 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5460 /* There's no easy way to tell a new-placement from the
5461 `( type-id )' construct. */
5462 cp_parser_parse_tentatively (parser);
5463 /* Look for a new-placement. */
5464 placement = cp_parser_new_placement (parser);
5465 /* If that didn't work out, there's no new-placement. */
5466 if (!cp_parser_parse_definitely (parser))
5467 placement = NULL_TREE;
5469 /* If the next token is a `(', then we have a parenthesized
5471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5473 /* Consume the `('. */
5474 cp_lexer_consume_token (parser->lexer);
5475 /* Parse the type-id. */
5476 type = cp_parser_type_id (parser);
5477 /* Look for the closing `)'. */
5478 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5479 /* There should not be a direct-new-declarator in this production,
5480 but GCC used to allowed this, so we check and emit a sensible error
5481 message for this case. */
5482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5484 error ("array bound forbidden after parenthesized type-id");
5485 inform ("try removing the parentheses around the type-id");
5486 cp_parser_direct_new_declarator (parser);
5490 /* Otherwise, there must be a new-type-id. */
5492 type = cp_parser_new_type_id (parser, &nelts);
5494 /* If the next token is a `(', then we have a new-initializer. */
5495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5496 initializer = cp_parser_new_initializer (parser);
5498 initializer = NULL_TREE;
5500 /* A new-expression may not appear in an integral constant
5502 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5503 return error_mark_node;
5505 /* Create a representation of the new-expression. */
5506 return build_new (placement, type, nelts, initializer, global_scope_p);
5509 /* Parse a new-placement.
5514 Returns the same representation as for an expression-list. */
5517 cp_parser_new_placement (cp_parser* parser)
5519 tree expression_list;
5521 /* Parse the expression-list. */
5522 expression_list = (cp_parser_parenthesized_expression_list
5523 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5524 /*non_constant_p=*/NULL));
5526 return expression_list;
5529 /* Parse a new-type-id.
5532 type-specifier-seq new-declarator [opt]
5534 Returns the TYPE allocated. If the new-type-id indicates an array
5535 type, *NELTS is set to the number of elements in the last array
5536 bound; the TYPE will not include the last array bound. */
5539 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5541 cp_decl_specifier_seq type_specifier_seq;
5542 cp_declarator *new_declarator;
5543 cp_declarator *declarator;
5544 cp_declarator *outer_declarator;
5545 const char *saved_message;
5548 /* The type-specifier sequence must not contain type definitions.
5549 (It cannot contain declarations of new types either, but if they
5550 are not definitions we will catch that because they are not
5552 saved_message = parser->type_definition_forbidden_message;
5553 parser->type_definition_forbidden_message
5554 = "types may not be defined in a new-type-id";
5555 /* Parse the type-specifier-seq. */
5556 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5557 &type_specifier_seq);
5558 /* Restore the old message. */
5559 parser->type_definition_forbidden_message = saved_message;
5560 /* Parse the new-declarator. */
5561 new_declarator = cp_parser_new_declarator_opt (parser);
5563 /* Determine the number of elements in the last array dimension, if
5566 /* Skip down to the last array dimension. */
5567 declarator = new_declarator;
5568 outer_declarator = NULL;
5569 while (declarator && (declarator->kind == cdk_pointer
5570 || declarator->kind == cdk_ptrmem))
5572 outer_declarator = declarator;
5573 declarator = declarator->declarator;
5576 && declarator->kind == cdk_array
5577 && declarator->declarator
5578 && declarator->declarator->kind == cdk_array)
5580 outer_declarator = declarator;
5581 declarator = declarator->declarator;
5584 if (declarator && declarator->kind == cdk_array)
5586 *nelts = declarator->u.array.bounds;
5587 if (*nelts == error_mark_node)
5588 *nelts = integer_one_node;
5590 if (outer_declarator)
5591 outer_declarator->declarator = declarator->declarator;
5593 new_declarator = NULL;
5596 type = groktypename (&type_specifier_seq, new_declarator);
5600 /* Parse an (optional) new-declarator.
5603 ptr-operator new-declarator [opt]
5604 direct-new-declarator
5606 Returns the declarator. */
5608 static cp_declarator *
5609 cp_parser_new_declarator_opt (cp_parser* parser)
5611 enum tree_code code;
5613 cp_cv_quals cv_quals;
5615 /* We don't know if there's a ptr-operator next, or not. */
5616 cp_parser_parse_tentatively (parser);
5617 /* Look for a ptr-operator. */
5618 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5619 /* If that worked, look for more new-declarators. */
5620 if (cp_parser_parse_definitely (parser))
5622 cp_declarator *declarator;
5624 /* Parse another optional declarator. */
5625 declarator = cp_parser_new_declarator_opt (parser);
5627 return cp_parser_make_indirect_declarator
5628 (code, type, cv_quals, declarator);
5631 /* If the next token is a `[', there is a direct-new-declarator. */
5632 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5633 return cp_parser_direct_new_declarator (parser);
5638 /* Parse a direct-new-declarator.
5640 direct-new-declarator:
5642 direct-new-declarator [constant-expression]
5646 static cp_declarator *
5647 cp_parser_direct_new_declarator (cp_parser* parser)
5649 cp_declarator *declarator = NULL;
5655 /* Look for the opening `['. */
5656 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5657 /* The first expression is not required to be constant. */
5660 expression = cp_parser_expression (parser, /*cast_p=*/false);
5661 /* The standard requires that the expression have integral
5662 type. DR 74 adds enumeration types. We believe that the
5663 real intent is that these expressions be handled like the
5664 expression in a `switch' condition, which also allows
5665 classes with a single conversion to integral or
5666 enumeration type. */
5667 if (!processing_template_decl)
5670 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5675 error ("expression in new-declarator must have integral "
5676 "or enumeration type");
5677 expression = error_mark_node;
5681 /* But all the other expressions must be. */
5684 = cp_parser_constant_expression (parser,
5685 /*allow_non_constant=*/false,
5687 /* Look for the closing `]'. */
5688 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5690 /* Add this bound to the declarator. */
5691 declarator = make_array_declarator (declarator, expression);
5693 /* If the next token is not a `[', then there are no more
5695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5702 /* Parse a new-initializer.
5705 ( expression-list [opt] )
5707 Returns a representation of the expression-list. If there is no
5708 expression-list, VOID_ZERO_NODE is returned. */
5711 cp_parser_new_initializer (cp_parser* parser)
5713 tree expression_list;
5715 expression_list = (cp_parser_parenthesized_expression_list
5716 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5717 /*non_constant_p=*/NULL));
5718 if (!expression_list)
5719 expression_list = void_zero_node;
5721 return expression_list;
5724 /* Parse a delete-expression.
5727 :: [opt] delete cast-expression
5728 :: [opt] delete [ ] cast-expression
5730 Returns a representation of the expression. */
5733 cp_parser_delete_expression (cp_parser* parser)
5735 bool global_scope_p;
5739 /* Look for the optional `::' operator. */
5741 = (cp_parser_global_scope_opt (parser,
5742 /*current_scope_valid_p=*/false)
5744 /* Look for the `delete' keyword. */
5745 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5746 /* See if the array syntax is in use. */
5747 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5749 /* Consume the `[' token. */
5750 cp_lexer_consume_token (parser->lexer);
5751 /* Look for the `]' token. */
5752 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5753 /* Remember that this is the `[]' construct. */
5759 /* Parse the cast-expression. */
5760 expression = cp_parser_simple_cast_expression (parser);
5762 /* A delete-expression may not appear in an integral constant
5764 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5765 return error_mark_node;
5767 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5770 /* Parse a cast-expression.
5774 ( type-id ) cast-expression
5776 ADDRESS_P is true iff the unary-expression is appearing as the
5777 operand of the `&' operator. CAST_P is true if this expression is
5778 the target of a cast.
5780 Returns a representation of the expression. */
5783 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5785 /* If it's a `(', then we might be looking at a cast. */
5786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5788 tree type = NULL_TREE;
5789 tree expr = NULL_TREE;
5790 bool compound_literal_p;
5791 const char *saved_message;
5793 /* There's no way to know yet whether or not this is a cast.
5794 For example, `(int (3))' is a unary-expression, while `(int)
5795 3' is a cast. So, we resort to parsing tentatively. */
5796 cp_parser_parse_tentatively (parser);
5797 /* Types may not be defined in a cast. */
5798 saved_message = parser->type_definition_forbidden_message;
5799 parser->type_definition_forbidden_message
5800 = "types may not be defined in casts";
5801 /* Consume the `('. */
5802 cp_lexer_consume_token (parser->lexer);
5803 /* A very tricky bit is that `(struct S) { 3 }' is a
5804 compound-literal (which we permit in C++ as an extension).
5805 But, that construct is not a cast-expression -- it is a
5806 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5807 is legal; if the compound-literal were a cast-expression,
5808 you'd need an extra set of parentheses.) But, if we parse
5809 the type-id, and it happens to be a class-specifier, then we
5810 will commit to the parse at that point, because we cannot
5811 undo the action that is done when creating a new class. So,
5812 then we cannot back up and do a postfix-expression.
5814 Therefore, we scan ahead to the closing `)', and check to see
5815 if the token after the `)' is a `{'. If so, we are not
5816 looking at a cast-expression.
5818 Save tokens so that we can put them back. */
5819 cp_lexer_save_tokens (parser->lexer);
5820 /* Skip tokens until the next token is a closing parenthesis.
5821 If we find the closing `)', and the next token is a `{', then
5822 we are looking at a compound-literal. */
5824 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5825 /*consume_paren=*/true)
5826 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5827 /* Roll back the tokens we skipped. */
5828 cp_lexer_rollback_tokens (parser->lexer);
5829 /* If we were looking at a compound-literal, simulate an error
5830 so that the call to cp_parser_parse_definitely below will
5832 if (compound_literal_p)
5833 cp_parser_simulate_error (parser);
5836 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5837 parser->in_type_id_in_expr_p = true;
5838 /* Look for the type-id. */
5839 type = cp_parser_type_id (parser);
5840 /* Look for the closing `)'. */
5841 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5842 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5845 /* Restore the saved message. */
5846 parser->type_definition_forbidden_message = saved_message;
5848 /* If ok so far, parse the dependent expression. We cannot be
5849 sure it is a cast. Consider `(T ())'. It is a parenthesized
5850 ctor of T, but looks like a cast to function returning T
5851 without a dependent expression. */
5852 if (!cp_parser_error_occurred (parser))
5853 expr = cp_parser_cast_expression (parser,
5854 /*address_p=*/false,
5857 if (cp_parser_parse_definitely (parser))
5859 /* Warn about old-style casts, if so requested. */
5860 if (warn_old_style_cast
5861 && !in_system_header
5862 && !VOID_TYPE_P (type)
5863 && current_lang_name != lang_name_c)
5864 warning (OPT_Wold_style_cast, "use of old-style cast");
5866 /* Only type conversions to integral or enumeration types
5867 can be used in constant-expressions. */
5868 if (!cast_valid_in_integral_constant_expression_p (type)
5869 && (cp_parser_non_integral_constant_expression
5871 "a cast to a type other than an integral or "
5872 "enumeration type")))
5873 return error_mark_node;
5875 /* Perform the cast. */
5876 expr = build_c_cast (type, expr);
5881 /* If we get here, then it's not a cast, so it must be a
5882 unary-expression. */
5883 return cp_parser_unary_expression (parser, address_p, cast_p);
5886 /* Parse a binary expression of the general form:
5890 pm-expression .* cast-expression
5891 pm-expression ->* cast-expression
5893 multiplicative-expression:
5895 multiplicative-expression * pm-expression
5896 multiplicative-expression / pm-expression
5897 multiplicative-expression % pm-expression
5899 additive-expression:
5900 multiplicative-expression
5901 additive-expression + multiplicative-expression
5902 additive-expression - multiplicative-expression
5906 shift-expression << additive-expression
5907 shift-expression >> additive-expression
5909 relational-expression:
5911 relational-expression < shift-expression
5912 relational-expression > shift-expression
5913 relational-expression <= shift-expression
5914 relational-expression >= shift-expression
5918 relational-expression:
5919 relational-expression <? shift-expression
5920 relational-expression >? shift-expression
5922 equality-expression:
5923 relational-expression
5924 equality-expression == relational-expression
5925 equality-expression != relational-expression
5929 and-expression & equality-expression
5931 exclusive-or-expression:
5933 exclusive-or-expression ^ and-expression
5935 inclusive-or-expression:
5936 exclusive-or-expression
5937 inclusive-or-expression | exclusive-or-expression
5939 logical-and-expression:
5940 inclusive-or-expression
5941 logical-and-expression && inclusive-or-expression
5943 logical-or-expression:
5944 logical-and-expression
5945 logical-or-expression || logical-and-expression
5947 All these are implemented with a single function like:
5950 simple-cast-expression
5951 binary-expression <token> binary-expression
5953 CAST_P is true if this expression is the target of a cast.
5955 The binops_by_token map is used to get the tree codes for each <token> type.
5956 binary-expressions are associated according to a precedence table. */
5958 #define TOKEN_PRECEDENCE(token) \
5959 (((token->type == CPP_GREATER \
5960 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5961 && !parser->greater_than_is_operator_p) \
5962 ? PREC_NOT_OPERATOR \
5963 : binops_by_token[token->type].prec)
5966 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5968 cp_parser_expression_stack stack;
5969 cp_parser_expression_stack_entry *sp = &stack[0];
5972 enum tree_code tree_type, lhs_type, rhs_type;
5973 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5976 /* Parse the first expression. */
5977 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5978 lhs_type = ERROR_MARK;
5982 /* Get an operator token. */
5983 token = cp_lexer_peek_token (parser->lexer);
5985 if (warn_cxx0x_compat
5986 && token->type == CPP_RSHIFT
5987 && !parser->greater_than_is_operator_p)
5989 warning (OPT_Wc__0x_compat,
5990 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5992 warning (OPT_Wc__0x_compat,
5993 "suggest parentheses around %<>>%> expression");
5996 new_prec = TOKEN_PRECEDENCE (token);
5998 /* Popping an entry off the stack means we completed a subexpression:
5999 - either we found a token which is not an operator (`>' where it is not
6000 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6001 will happen repeatedly;
6002 - or, we found an operator which has lower priority. This is the case
6003 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6005 if (new_prec <= prec)
6014 tree_type = binops_by_token[token->type].tree_type;
6016 /* We used the operator token. */
6017 cp_lexer_consume_token (parser->lexer);
6019 /* Extract another operand. It may be the RHS of this expression
6020 or the LHS of a new, higher priority expression. */
6021 rhs = cp_parser_simple_cast_expression (parser);
6022 rhs_type = ERROR_MARK;
6024 /* Get another operator token. Look up its precedence to avoid
6025 building a useless (immediately popped) stack entry for common
6026 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6027 token = cp_lexer_peek_token (parser->lexer);
6028 lookahead_prec = TOKEN_PRECEDENCE (token);
6029 if (lookahead_prec > new_prec)
6031 /* ... and prepare to parse the RHS of the new, higher priority
6032 expression. Since precedence levels on the stack are
6033 monotonically increasing, we do not have to care about
6036 sp->tree_type = tree_type;
6038 sp->lhs_type = lhs_type;
6041 lhs_type = rhs_type;
6043 new_prec = lookahead_prec;
6047 /* If the stack is not empty, we have parsed into LHS the right side
6048 (`4' in the example above) of an expression we had suspended.
6049 We can use the information on the stack to recover the LHS (`3')
6050 from the stack together with the tree code (`MULT_EXPR'), and
6051 the precedence of the higher level subexpression
6052 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6053 which will be used to actually build the additive expression. */
6056 tree_type = sp->tree_type;
6058 rhs_type = lhs_type;
6060 lhs_type = sp->lhs_type;
6063 overloaded_p = false;
6064 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6066 lhs_type = tree_type;
6068 /* If the binary operator required the use of an overloaded operator,
6069 then this expression cannot be an integral constant-expression.
6070 An overloaded operator can be used even if both operands are
6071 otherwise permissible in an integral constant-expression if at
6072 least one of the operands is of enumeration type. */
6075 && (cp_parser_non_integral_constant_expression
6076 (parser, "calls to overloaded operators")))
6077 return error_mark_node;
6084 /* Parse the `? expression : assignment-expression' part of a
6085 conditional-expression. The LOGICAL_OR_EXPR is the
6086 logical-or-expression that started the conditional-expression.
6087 Returns a representation of the entire conditional-expression.
6089 This routine is used by cp_parser_assignment_expression.
6091 ? expression : assignment-expression
6095 ? : assignment-expression */
6098 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6101 tree assignment_expr;
6103 /* Consume the `?' token. */
6104 cp_lexer_consume_token (parser->lexer);
6105 if (cp_parser_allow_gnu_extensions_p (parser)
6106 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6107 /* Implicit true clause. */
6110 /* Parse the expression. */
6111 expr = cp_parser_expression (parser, /*cast_p=*/false);
6113 /* The next token should be a `:'. */
6114 cp_parser_require (parser, CPP_COLON, "`:'");
6115 /* Parse the assignment-expression. */
6116 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6118 /* Build the conditional-expression. */
6119 return build_x_conditional_expr (logical_or_expr,
6124 /* Parse an assignment-expression.
6126 assignment-expression:
6127 conditional-expression
6128 logical-or-expression assignment-operator assignment_expression
6131 CAST_P is true if this expression is the target of a cast.
6133 Returns a representation for the expression. */
6136 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6140 /* If the next token is the `throw' keyword, then we're looking at
6141 a throw-expression. */
6142 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6143 expr = cp_parser_throw_expression (parser);
6144 /* Otherwise, it must be that we are looking at a
6145 logical-or-expression. */
6148 /* Parse the binary expressions (logical-or-expression). */
6149 expr = cp_parser_binary_expression (parser, cast_p);
6150 /* If the next token is a `?' then we're actually looking at a
6151 conditional-expression. */
6152 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6153 return cp_parser_question_colon_clause (parser, expr);
6156 enum tree_code assignment_operator;
6158 /* If it's an assignment-operator, we're using the second
6161 = cp_parser_assignment_operator_opt (parser);
6162 if (assignment_operator != ERROR_MARK)
6166 /* Parse the right-hand side of the assignment. */
6167 rhs = cp_parser_assignment_expression (parser, cast_p);
6168 /* An assignment may not appear in a
6169 constant-expression. */
6170 if (cp_parser_non_integral_constant_expression (parser,
6172 return error_mark_node;
6173 /* Build the assignment expression. */
6174 expr = build_x_modify_expr (expr,
6175 assignment_operator,
6184 /* Parse an (optional) assignment-operator.
6186 assignment-operator: one of
6187 = *= /= %= += -= >>= <<= &= ^= |=
6191 assignment-operator: one of
6194 If the next token is an assignment operator, the corresponding tree
6195 code is returned, and the token is consumed. For example, for
6196 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6197 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6198 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6199 operator, ERROR_MARK is returned. */
6201 static enum tree_code
6202 cp_parser_assignment_operator_opt (cp_parser* parser)
6207 /* Peek at the next toen. */
6208 token = cp_lexer_peek_token (parser->lexer);
6210 switch (token->type)
6221 op = TRUNC_DIV_EXPR;
6225 op = TRUNC_MOD_EXPR;
6257 /* Nothing else is an assignment operator. */
6261 /* If it was an assignment operator, consume it. */
6262 if (op != ERROR_MARK)
6263 cp_lexer_consume_token (parser->lexer);
6268 /* Parse an expression.
6271 assignment-expression
6272 expression , assignment-expression
6274 CAST_P is true if this expression is the target of a cast.
6276 Returns a representation of the expression. */
6279 cp_parser_expression (cp_parser* parser, bool cast_p)
6281 tree expression = NULL_TREE;
6285 tree assignment_expression;
6287 /* Parse the next assignment-expression. */
6288 assignment_expression
6289 = cp_parser_assignment_expression (parser, cast_p);
6290 /* If this is the first assignment-expression, we can just
6293 expression = assignment_expression;
6295 expression = build_x_compound_expr (expression,
6296 assignment_expression);
6297 /* If the next token is not a comma, then we are done with the
6299 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6301 /* Consume the `,'. */
6302 cp_lexer_consume_token (parser->lexer);
6303 /* A comma operator cannot appear in a constant-expression. */
6304 if (cp_parser_non_integral_constant_expression (parser,
6305 "a comma operator"))
6306 expression = error_mark_node;
6312 /* Parse a constant-expression.
6314 constant-expression:
6315 conditional-expression
6317 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6318 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6319 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6320 is false, NON_CONSTANT_P should be NULL. */
6323 cp_parser_constant_expression (cp_parser* parser,
6324 bool allow_non_constant_p,
6325 bool *non_constant_p)
6327 bool saved_integral_constant_expression_p;
6328 bool saved_allow_non_integral_constant_expression_p;
6329 bool saved_non_integral_constant_expression_p;
6332 /* It might seem that we could simply parse the
6333 conditional-expression, and then check to see if it were
6334 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6335 one that the compiler can figure out is constant, possibly after
6336 doing some simplifications or optimizations. The standard has a
6337 precise definition of constant-expression, and we must honor
6338 that, even though it is somewhat more restrictive.
6344 is not a legal declaration, because `(2, 3)' is not a
6345 constant-expression. The `,' operator is forbidden in a
6346 constant-expression. However, GCC's constant-folding machinery
6347 will fold this operation to an INTEGER_CST for `3'. */
6349 /* Save the old settings. */
6350 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6351 saved_allow_non_integral_constant_expression_p
6352 = parser->allow_non_integral_constant_expression_p;
6353 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6354 /* We are now parsing a constant-expression. */
6355 parser->integral_constant_expression_p = true;
6356 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6357 parser->non_integral_constant_expression_p = false;
6358 /* Although the grammar says "conditional-expression", we parse an
6359 "assignment-expression", which also permits "throw-expression"
6360 and the use of assignment operators. In the case that
6361 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6362 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6363 actually essential that we look for an assignment-expression.
6364 For example, cp_parser_initializer_clauses uses this function to
6365 determine whether a particular assignment-expression is in fact
6367 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6368 /* Restore the old settings. */
6369 parser->integral_constant_expression_p
6370 = saved_integral_constant_expression_p;
6371 parser->allow_non_integral_constant_expression_p
6372 = saved_allow_non_integral_constant_expression_p;
6373 if (allow_non_constant_p)
6374 *non_constant_p = parser->non_integral_constant_expression_p;
6375 else if (parser->non_integral_constant_expression_p)
6376 expression = error_mark_node;
6377 parser->non_integral_constant_expression_p
6378 = saved_non_integral_constant_expression_p;
6383 /* Parse __builtin_offsetof.
6385 offsetof-expression:
6386 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6388 offsetof-member-designator:
6390 | offsetof-member-designator "." id-expression
6391 | offsetof-member-designator "[" expression "]" */
6394 cp_parser_builtin_offsetof (cp_parser *parser)
6396 int save_ice_p, save_non_ice_p;
6400 /* We're about to accept non-integral-constant things, but will
6401 definitely yield an integral constant expression. Save and
6402 restore these values around our local parsing. */
6403 save_ice_p = parser->integral_constant_expression_p;
6404 save_non_ice_p = parser->non_integral_constant_expression_p;
6406 /* Consume the "__builtin_offsetof" token. */
6407 cp_lexer_consume_token (parser->lexer);
6408 /* Consume the opening `('. */
6409 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6410 /* Parse the type-id. */
6411 type = cp_parser_type_id (parser);
6412 /* Look for the `,'. */
6413 cp_parser_require (parser, CPP_COMMA, "`,'");
6415 /* Build the (type *)null that begins the traditional offsetof macro. */
6416 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6418 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6419 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6423 cp_token *token = cp_lexer_peek_token (parser->lexer);
6424 switch (token->type)
6426 case CPP_OPEN_SQUARE:
6427 /* offsetof-member-designator "[" expression "]" */
6428 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6432 /* offsetof-member-designator "." identifier */
6433 cp_lexer_consume_token (parser->lexer);
6434 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6438 case CPP_CLOSE_PAREN:
6439 /* Consume the ")" token. */
6440 cp_lexer_consume_token (parser->lexer);
6444 /* Error. We know the following require will fail, but
6445 that gives the proper error message. */
6446 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6447 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6448 expr = error_mark_node;
6454 /* If we're processing a template, we can't finish the semantics yet.
6455 Otherwise we can fold the entire expression now. */
6456 if (processing_template_decl)
6457 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6459 expr = finish_offsetof (expr);
6462 parser->integral_constant_expression_p = save_ice_p;
6463 parser->non_integral_constant_expression_p = save_non_ice_p;
6468 /* Parse a trait expression. */
6471 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6474 tree type1, type2 = NULL_TREE;
6475 bool binary = false;
6476 cp_decl_specifier_seq decl_specs;
6480 case RID_HAS_NOTHROW_ASSIGN:
6481 kind = CPTK_HAS_NOTHROW_ASSIGN;
6483 case RID_HAS_NOTHROW_CONSTRUCTOR:
6484 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6486 case RID_HAS_NOTHROW_COPY:
6487 kind = CPTK_HAS_NOTHROW_COPY;
6489 case RID_HAS_TRIVIAL_ASSIGN:
6490 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6492 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6493 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6495 case RID_HAS_TRIVIAL_COPY:
6496 kind = CPTK_HAS_TRIVIAL_COPY;
6498 case RID_HAS_TRIVIAL_DESTRUCTOR:
6499 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6501 case RID_HAS_VIRTUAL_DESTRUCTOR:
6502 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6504 case RID_IS_ABSTRACT:
6505 kind = CPTK_IS_ABSTRACT;
6507 case RID_IS_BASE_OF:
6508 kind = CPTK_IS_BASE_OF;
6512 kind = CPTK_IS_CLASS;
6514 case RID_IS_CONVERTIBLE_TO:
6515 kind = CPTK_IS_CONVERTIBLE_TO;
6519 kind = CPTK_IS_EMPTY;
6522 kind = CPTK_IS_ENUM;
6527 case RID_IS_POLYMORPHIC:
6528 kind = CPTK_IS_POLYMORPHIC;
6531 kind = CPTK_IS_UNION;
6537 /* Consume the token. */
6538 cp_lexer_consume_token (parser->lexer);
6540 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6542 type1 = cp_parser_type_id (parser);
6544 if (type1 == error_mark_node)
6545 return error_mark_node;
6547 /* Build a trivial decl-specifier-seq. */
6548 clear_decl_specs (&decl_specs);
6549 decl_specs.type = type1;
6551 /* Call grokdeclarator to figure out what type this is. */
6552 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6553 /*initialized=*/0, /*attrlist=*/NULL);
6557 cp_parser_require (parser, CPP_COMMA, "`,'");
6559 type2 = cp_parser_type_id (parser);
6561 if (type2 == error_mark_node)
6562 return error_mark_node;
6564 /* Build a trivial decl-specifier-seq. */
6565 clear_decl_specs (&decl_specs);
6566 decl_specs.type = type2;
6568 /* Call grokdeclarator to figure out what type this is. */
6569 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6570 /*initialized=*/0, /*attrlist=*/NULL);
6573 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6575 /* Complete the trait expression, which may mean either processing
6576 the trait expr now or saving it for template instantiation. */
6577 return finish_trait_expr (kind, type1, type2);
6580 /* Statements [gram.stmt.stmt] */
6582 /* Parse a statement.
6586 expression-statement
6591 declaration-statement
6594 IN_COMPOUND is true when the statement is nested inside a
6595 cp_parser_compound_statement; this matters for certain pragmas.
6597 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6598 is a (possibly labeled) if statement which is not enclosed in braces
6599 and has an else clause. This is used to implement -Wparentheses. */
6602 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6603 bool in_compound, bool *if_p)
6607 location_t statement_location;
6612 /* There is no statement yet. */
6613 statement = NULL_TREE;
6614 /* Peek at the next token. */
6615 token = cp_lexer_peek_token (parser->lexer);
6616 /* Remember the location of the first token in the statement. */
6617 statement_location = token->location;
6618 /* If this is a keyword, then that will often determine what kind of
6619 statement we have. */
6620 if (token->type == CPP_KEYWORD)
6622 enum rid keyword = token->keyword;
6628 /* Looks like a labeled-statement with a case label.
6629 Parse the label, and then use tail recursion to parse
6631 cp_parser_label_for_labeled_statement (parser);
6636 statement = cp_parser_selection_statement (parser, if_p);
6642 statement = cp_parser_iteration_statement (parser);
6649 statement = cp_parser_jump_statement (parser);
6652 /* Objective-C++ exception-handling constructs. */
6655 case RID_AT_FINALLY:
6656 case RID_AT_SYNCHRONIZED:
6658 statement = cp_parser_objc_statement (parser);
6662 statement = cp_parser_try_block (parser);
6666 /* This must be a namespace alias definition. */
6667 cp_parser_declaration_statement (parser);
6671 /* It might be a keyword like `int' that can start a
6672 declaration-statement. */
6676 else if (token->type == CPP_NAME)
6678 /* If the next token is a `:', then we are looking at a
6679 labeled-statement. */
6680 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6681 if (token->type == CPP_COLON)
6683 /* Looks like a labeled-statement with an ordinary label.
6684 Parse the label, and then use tail recursion to parse
6686 cp_parser_label_for_labeled_statement (parser);
6690 /* Anything that starts with a `{' must be a compound-statement. */
6691 else if (token->type == CPP_OPEN_BRACE)
6692 statement = cp_parser_compound_statement (parser, NULL, false);
6693 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6694 a statement all its own. */
6695 else if (token->type == CPP_PRAGMA)
6697 /* Only certain OpenMP pragmas are attached to statements, and thus
6698 are considered statements themselves. All others are not. In
6699 the context of a compound, accept the pragma as a "statement" and
6700 return so that we can check for a close brace. Otherwise we
6701 require a real statement and must go back and read one. */
6703 cp_parser_pragma (parser, pragma_compound);
6704 else if (!cp_parser_pragma (parser, pragma_stmt))
6708 else if (token->type == CPP_EOF)
6710 cp_parser_error (parser, "expected statement");
6714 /* Everything else must be a declaration-statement or an
6715 expression-statement. Try for the declaration-statement
6716 first, unless we are looking at a `;', in which case we know that
6717 we have an expression-statement. */
6720 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6722 cp_parser_parse_tentatively (parser);
6723 /* Try to parse the declaration-statement. */
6724 cp_parser_declaration_statement (parser);
6725 /* If that worked, we're done. */
6726 if (cp_parser_parse_definitely (parser))
6729 /* Look for an expression-statement instead. */
6730 statement = cp_parser_expression_statement (parser, in_statement_expr);
6733 /* Set the line number for the statement. */
6734 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6735 SET_EXPR_LOCATION (statement, statement_location);
6738 /* Parse the label for a labeled-statement, i.e.
6741 case constant-expression :
6745 case constant-expression ... constant-expression : statement
6747 When a label is parsed without errors, the label is added to the
6748 parse tree by the finish_* functions, so this function doesn't
6749 have to return the label. */
6752 cp_parser_label_for_labeled_statement (cp_parser* parser)
6756 /* The next token should be an identifier. */
6757 token = cp_lexer_peek_token (parser->lexer);
6758 if (token->type != CPP_NAME
6759 && token->type != CPP_KEYWORD)
6761 cp_parser_error (parser, "expected labeled-statement");
6765 switch (token->keyword)
6772 /* Consume the `case' token. */
6773 cp_lexer_consume_token (parser->lexer);
6774 /* Parse the constant-expression. */
6775 expr = cp_parser_constant_expression (parser,
6776 /*allow_non_constant_p=*/false,
6779 ellipsis = cp_lexer_peek_token (parser->lexer);
6780 if (ellipsis->type == CPP_ELLIPSIS)
6782 /* Consume the `...' token. */
6783 cp_lexer_consume_token (parser->lexer);
6785 cp_parser_constant_expression (parser,
6786 /*allow_non_constant_p=*/false,
6788 /* We don't need to emit warnings here, as the common code
6789 will do this for us. */
6792 expr_hi = NULL_TREE;
6794 if (parser->in_switch_statement_p)
6795 finish_case_label (expr, expr_hi);
6797 error ("case label %qE not within a switch statement", expr);
6802 /* Consume the `default' token. */
6803 cp_lexer_consume_token (parser->lexer);
6805 if (parser->in_switch_statement_p)
6806 finish_case_label (NULL_TREE, NULL_TREE);
6808 error ("case label not within a switch statement");
6812 /* Anything else must be an ordinary label. */
6813 finish_label_stmt (cp_parser_identifier (parser));
6817 /* Require the `:' token. */
6818 cp_parser_require (parser, CPP_COLON, "`:'");
6821 /* Parse an expression-statement.
6823 expression-statement:
6826 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6827 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6828 indicates whether this expression-statement is part of an
6829 expression statement. */
6832 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6834 tree statement = NULL_TREE;
6836 /* If the next token is a ';', then there is no expression
6838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6839 statement = cp_parser_expression (parser, /*cast_p=*/false);
6841 /* Consume the final `;'. */
6842 cp_parser_consume_semicolon_at_end_of_statement (parser);
6844 if (in_statement_expr
6845 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6846 /* This is the final expression statement of a statement
6848 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6850 statement = finish_expr_stmt (statement);
6857 /* Parse a compound-statement.
6860 { statement-seq [opt] }
6865 { label-declaration-seq [opt] statement-seq [opt] }
6867 label-declaration-seq:
6869 label-declaration-seq label-declaration
6871 Returns a tree representing the statement. */
6874 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6879 /* Consume the `{'. */
6880 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6881 return error_mark_node;
6882 /* Begin the compound-statement. */
6883 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6884 /* If the next keyword is `__label__' we have a label declaration. */
6885 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6886 cp_parser_label_declaration (parser);
6887 /* Parse an (optional) statement-seq. */
6888 cp_parser_statement_seq_opt (parser, in_statement_expr);
6889 /* Finish the compound-statement. */
6890 finish_compound_stmt (compound_stmt);
6891 /* Consume the `}'. */
6892 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6894 return compound_stmt;
6897 /* Parse an (optional) statement-seq.
6901 statement-seq [opt] statement */
6904 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6906 /* Scan statements until there aren't any more. */
6909 cp_token *token = cp_lexer_peek_token (parser->lexer);
6911 /* If we're looking at a `}', then we've run out of statements. */
6912 if (token->type == CPP_CLOSE_BRACE
6913 || token->type == CPP_EOF
6914 || token->type == CPP_PRAGMA_EOL)
6917 /* If we are in a compound statement and find 'else' then
6918 something went wrong. */
6919 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6921 if (parser->in_statement & IN_IF_STMT)
6925 token = cp_lexer_consume_token (parser->lexer);
6926 error ("%<else%> without a previous %<if%>");
6930 /* Parse the statement. */
6931 cp_parser_statement (parser, in_statement_expr, true, NULL);
6935 /* Parse a selection-statement.
6937 selection-statement:
6938 if ( condition ) statement
6939 if ( condition ) statement else statement
6940 switch ( condition ) statement
6942 Returns the new IF_STMT or SWITCH_STMT.
6944 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6945 is a (possibly labeled) if statement which is not enclosed in
6946 braces and has an else clause. This is used to implement
6950 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6958 /* Peek at the next token. */
6959 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6961 /* See what kind of keyword it is. */
6962 keyword = token->keyword;
6971 /* Look for the `('. */
6972 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6974 cp_parser_skip_to_end_of_statement (parser);
6975 return error_mark_node;
6978 /* Begin the selection-statement. */
6979 if (keyword == RID_IF)
6980 statement = begin_if_stmt ();
6982 statement = begin_switch_stmt ();
6984 /* Parse the condition. */
6985 condition = cp_parser_condition (parser);
6986 /* Look for the `)'. */
6987 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6988 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6989 /*consume_paren=*/true);
6991 if (keyword == RID_IF)
6994 unsigned char in_statement;
6996 /* Add the condition. */
6997 finish_if_stmt_cond (condition, statement);
6999 /* Parse the then-clause. */
7000 in_statement = parser->in_statement;
7001 parser->in_statement |= IN_IF_STMT;
7002 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7003 parser->in_statement = in_statement;
7005 finish_then_clause (statement);
7007 /* If the next token is `else', parse the else-clause. */
7008 if (cp_lexer_next_token_is_keyword (parser->lexer,
7011 /* Consume the `else' keyword. */
7012 cp_lexer_consume_token (parser->lexer);
7013 begin_else_clause (statement);
7014 /* Parse the else-clause. */
7015 cp_parser_implicitly_scoped_statement (parser, NULL);
7016 finish_else_clause (statement);
7018 /* If we are currently parsing a then-clause, then
7019 IF_P will not be NULL. We set it to true to
7020 indicate that this if statement has an else clause.
7021 This may trigger the Wparentheses warning below
7022 when we get back up to the parent if statement. */
7028 /* This if statement does not have an else clause. If
7029 NESTED_IF is true, then the then-clause is an if
7030 statement which does have an else clause. We warn
7031 about the potential ambiguity. */
7033 warning (OPT_Wparentheses,
7034 ("%Hsuggest explicit braces "
7035 "to avoid ambiguous %<else%>"),
7036 EXPR_LOCUS (statement));
7039 /* Now we're all done with the if-statement. */
7040 finish_if_stmt (statement);
7044 bool in_switch_statement_p;
7045 unsigned char in_statement;
7047 /* Add the condition. */
7048 finish_switch_cond (condition, statement);
7050 /* Parse the body of the switch-statement. */
7051 in_switch_statement_p = parser->in_switch_statement_p;
7052 in_statement = parser->in_statement;
7053 parser->in_switch_statement_p = true;
7054 parser->in_statement |= IN_SWITCH_STMT;
7055 cp_parser_implicitly_scoped_statement (parser, NULL);
7056 parser->in_switch_statement_p = in_switch_statement_p;
7057 parser->in_statement = in_statement;
7059 /* Now we're all done with the switch-statement. */
7060 finish_switch_stmt (statement);
7068 cp_parser_error (parser, "expected selection-statement");
7069 return error_mark_node;
7073 /* Parse a condition.
7077 type-specifier-seq declarator = assignment-expression
7082 type-specifier-seq declarator asm-specification [opt]
7083 attributes [opt] = assignment-expression
7085 Returns the expression that should be tested. */
7088 cp_parser_condition (cp_parser* parser)
7090 cp_decl_specifier_seq type_specifiers;
7091 const char *saved_message;
7093 /* Try the declaration first. */
7094 cp_parser_parse_tentatively (parser);
7095 /* New types are not allowed in the type-specifier-seq for a
7097 saved_message = parser->type_definition_forbidden_message;
7098 parser->type_definition_forbidden_message
7099 = "types may not be defined in conditions";
7100 /* Parse the type-specifier-seq. */
7101 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7103 /* Restore the saved message. */
7104 parser->type_definition_forbidden_message = saved_message;
7105 /* If all is well, we might be looking at a declaration. */
7106 if (!cp_parser_error_occurred (parser))
7109 tree asm_specification;
7111 cp_declarator *declarator;
7112 tree initializer = NULL_TREE;
7114 /* Parse the declarator. */
7115 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7116 /*ctor_dtor_or_conv_p=*/NULL,
7117 /*parenthesized_p=*/NULL,
7118 /*member_p=*/false);
7119 /* Parse the attributes. */
7120 attributes = cp_parser_attributes_opt (parser);
7121 /* Parse the asm-specification. */
7122 asm_specification = cp_parser_asm_specification_opt (parser);
7123 /* If the next token is not an `=', then we might still be
7124 looking at an expression. For example:
7128 looks like a decl-specifier-seq and a declarator -- but then
7129 there is no `=', so this is an expression. */
7130 cp_parser_require (parser, CPP_EQ, "`='");
7131 /* If we did see an `=', then we are looking at a declaration
7133 if (cp_parser_parse_definitely (parser))
7136 bool non_constant_p;
7138 /* Create the declaration. */
7139 decl = start_decl (declarator, &type_specifiers,
7140 /*initialized_p=*/true,
7141 attributes, /*prefix_attributes=*/NULL_TREE,
7143 /* Parse the assignment-expression. */
7145 = cp_parser_constant_expression (parser,
7146 /*allow_non_constant_p=*/true,
7148 if (!non_constant_p)
7149 initializer = fold_non_dependent_expr (initializer);
7151 /* Process the initializer. */
7152 cp_finish_decl (decl,
7153 initializer, !non_constant_p,
7155 LOOKUP_ONLYCONVERTING);
7158 pop_scope (pushed_scope);
7160 return convert_from_reference (decl);
7163 /* If we didn't even get past the declarator successfully, we are
7164 definitely not looking at a declaration. */
7166 cp_parser_abort_tentative_parse (parser);
7168 /* Otherwise, we are looking at an expression. */
7169 return cp_parser_expression (parser, /*cast_p=*/false);
7172 /* We check for a ) immediately followed by ; with no whitespacing
7173 between. This is used to issue a warning for:
7181 as the semicolon is probably extraneous.
7183 On parse errors, the next token might not be a ), so do nothing in
7187 check_empty_body (cp_parser* parser, const char* type)
7190 cp_token *close_paren;
7191 expanded_location close_loc;
7192 expanded_location semi_loc;
7194 close_paren = cp_lexer_peek_token (parser->lexer);
7195 if (close_paren->type != CPP_CLOSE_PAREN)
7198 close_loc = expand_location (close_paren->location);
7199 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7201 if (token->type != CPP_SEMICOLON
7202 || (token->flags & PREV_WHITE))
7205 semi_loc = expand_location (token->location);
7206 if (close_loc.line == semi_loc.line
7207 #ifdef USE_MAPPED_LOCATION
7208 && close_loc.column+1 == semi_loc.column
7211 warning (OPT_Wempty_body,
7212 "suggest a space before %<;%> or explicit braces around empty "
7213 "body in %<%s%> statement",
7217 /* Parse an iteration-statement.
7219 iteration-statement:
7220 while ( condition ) statement
7221 do statement while ( expression ) ;
7222 for ( for-init-statement condition [opt] ; expression [opt] )
7225 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7228 cp_parser_iteration_statement (cp_parser* parser)
7233 unsigned char in_statement;
7235 /* Peek at the next token. */
7236 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7238 return error_mark_node;
7240 /* Remember whether or not we are already within an iteration
7242 in_statement = parser->in_statement;
7244 /* See what kind of keyword it is. */
7245 keyword = token->keyword;
7252 /* Begin the while-statement. */
7253 statement = begin_while_stmt ();
7254 /* Look for the `('. */
7255 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7256 /* Parse the condition. */
7257 condition = cp_parser_condition (parser);
7258 finish_while_stmt_cond (condition, statement);
7259 check_empty_body (parser, "while");
7260 /* Look for the `)'. */
7261 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7262 /* Parse the dependent statement. */
7263 parser->in_statement = IN_ITERATION_STMT;
7264 cp_parser_already_scoped_statement (parser);
7265 parser->in_statement = in_statement;
7266 /* We're done with the while-statement. */
7267 finish_while_stmt (statement);
7275 /* Begin the do-statement. */
7276 statement = begin_do_stmt ();
7277 /* Parse the body of the do-statement. */
7278 parser->in_statement = IN_ITERATION_STMT;
7279 cp_parser_implicitly_scoped_statement (parser, NULL);
7280 parser->in_statement = in_statement;
7281 finish_do_body (statement);
7282 /* Look for the `while' keyword. */
7283 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7284 /* Look for the `('. */
7285 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7286 /* Parse the expression. */
7287 expression = cp_parser_expression (parser, /*cast_p=*/false);
7288 /* We're done with the do-statement. */
7289 finish_do_stmt (expression, statement);
7290 /* Look for the `)'. */
7291 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7292 /* Look for the `;'. */
7293 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7299 tree condition = NULL_TREE;
7300 tree expression = NULL_TREE;
7302 /* Begin the for-statement. */
7303 statement = begin_for_stmt ();
7304 /* Look for the `('. */
7305 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7306 /* Parse the initialization. */
7307 cp_parser_for_init_statement (parser);
7308 finish_for_init_stmt (statement);
7310 /* If there's a condition, process it. */
7311 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7312 condition = cp_parser_condition (parser);
7313 finish_for_cond (condition, statement);
7314 /* Look for the `;'. */
7315 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7317 /* If there's an expression, process it. */
7318 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7319 expression = cp_parser_expression (parser, /*cast_p=*/false);
7320 finish_for_expr (expression, statement);
7321 check_empty_body (parser, "for");
7322 /* Look for the `)'. */
7323 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7325 /* Parse the body of the for-statement. */
7326 parser->in_statement = IN_ITERATION_STMT;
7327 cp_parser_already_scoped_statement (parser);
7328 parser->in_statement = in_statement;
7330 /* We're done with the for-statement. */
7331 finish_for_stmt (statement);
7336 cp_parser_error (parser, "expected iteration-statement");
7337 statement = error_mark_node;
7344 /* Parse a for-init-statement.
7347 expression-statement
7348 simple-declaration */
7351 cp_parser_for_init_statement (cp_parser* parser)
7353 /* If the next token is a `;', then we have an empty
7354 expression-statement. Grammatically, this is also a
7355 simple-declaration, but an invalid one, because it does not
7356 declare anything. Therefore, if we did not handle this case
7357 specially, we would issue an error message about an invalid
7359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7361 /* We're going to speculatively look for a declaration, falling back
7362 to an expression, if necessary. */
7363 cp_parser_parse_tentatively (parser);
7364 /* Parse the declaration. */
7365 cp_parser_simple_declaration (parser,
7366 /*function_definition_allowed_p=*/false);
7367 /* If the tentative parse failed, then we shall need to look for an
7368 expression-statement. */
7369 if (cp_parser_parse_definitely (parser))
7373 cp_parser_expression_statement (parser, false);
7376 /* Parse a jump-statement.
7381 return expression [opt] ;
7389 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7392 cp_parser_jump_statement (cp_parser* parser)
7394 tree statement = error_mark_node;
7397 unsigned char in_statement;
7399 /* Peek at the next token. */
7400 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7402 return error_mark_node;
7404 /* See what kind of keyword it is. */
7405 keyword = token->keyword;
7409 in_statement = parser->in_statement & ~IN_IF_STMT;
7410 switch (in_statement)
7413 error ("break statement not within loop or switch");
7416 gcc_assert ((in_statement & IN_SWITCH_STMT)
7417 || in_statement == IN_ITERATION_STMT);
7418 statement = finish_break_stmt ();
7421 error ("invalid exit from OpenMP structured block");
7424 error ("break statement used with OpenMP for loop");
7427 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7431 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7434 error ("continue statement not within a loop");
7436 case IN_ITERATION_STMT:
7438 statement = finish_continue_stmt ();
7441 error ("invalid exit from OpenMP structured block");
7446 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7453 /* If the next token is a `;', then there is no
7455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7456 expr = cp_parser_expression (parser, /*cast_p=*/false);
7459 /* Build the return-statement. */
7460 statement = finish_return_stmt (expr);
7461 /* Look for the final `;'. */
7462 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7467 /* Create the goto-statement. */
7468 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7470 /* Issue a warning about this use of a GNU extension. */
7472 pedwarn ("ISO C++ forbids computed gotos");
7473 /* Consume the '*' token. */
7474 cp_lexer_consume_token (parser->lexer);
7475 /* Parse the dependent expression. */
7476 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7479 finish_goto_stmt (cp_parser_identifier (parser));
7480 /* Look for the final `;'. */
7481 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7485 cp_parser_error (parser, "expected jump-statement");
7492 /* Parse a declaration-statement.
7494 declaration-statement:
7495 block-declaration */
7498 cp_parser_declaration_statement (cp_parser* parser)
7502 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7503 p = obstack_alloc (&declarator_obstack, 0);
7505 /* Parse the block-declaration. */
7506 cp_parser_block_declaration (parser, /*statement_p=*/true);
7508 /* Free any declarators allocated. */
7509 obstack_free (&declarator_obstack, p);
7511 /* Finish off the statement. */
7515 /* Some dependent statements (like `if (cond) statement'), are
7516 implicitly in their own scope. In other words, if the statement is
7517 a single statement (as opposed to a compound-statement), it is
7518 none-the-less treated as if it were enclosed in braces. Any
7519 declarations appearing in the dependent statement are out of scope
7520 after control passes that point. This function parses a statement,
7521 but ensures that is in its own scope, even if it is not a
7524 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7525 is a (possibly labeled) if statement which is not enclosed in
7526 braces and has an else clause. This is used to implement
7529 Returns the new statement. */
7532 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7539 /* Mark if () ; with a special NOP_EXPR. */
7540 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7542 cp_lexer_consume_token (parser->lexer);
7543 statement = add_stmt (build_empty_stmt ());
7545 /* if a compound is opened, we simply parse the statement directly. */
7546 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7547 statement = cp_parser_compound_statement (parser, NULL, false);
7548 /* If the token is not a `{', then we must take special action. */
7551 /* Create a compound-statement. */
7552 statement = begin_compound_stmt (0);
7553 /* Parse the dependent-statement. */
7554 cp_parser_statement (parser, NULL_TREE, false, if_p);
7555 /* Finish the dummy compound-statement. */
7556 finish_compound_stmt (statement);
7559 /* Return the statement. */
7563 /* For some dependent statements (like `while (cond) statement'), we
7564 have already created a scope. Therefore, even if the dependent
7565 statement is a compound-statement, we do not want to create another
7569 cp_parser_already_scoped_statement (cp_parser* parser)
7571 /* If the token is a `{', then we must take special action. */
7572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7573 cp_parser_statement (parser, NULL_TREE, false, NULL);
7576 /* Avoid calling cp_parser_compound_statement, so that we
7577 don't create a new scope. Do everything else by hand. */
7578 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7579 cp_parser_statement_seq_opt (parser, NULL_TREE);
7580 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7584 /* Declarations [gram.dcl.dcl] */
7586 /* Parse an optional declaration-sequence.
7590 declaration-seq declaration */
7593 cp_parser_declaration_seq_opt (cp_parser* parser)
7599 token = cp_lexer_peek_token (parser->lexer);
7601 if (token->type == CPP_CLOSE_BRACE
7602 || token->type == CPP_EOF
7603 || token->type == CPP_PRAGMA_EOL)
7606 if (token->type == CPP_SEMICOLON)
7608 /* A declaration consisting of a single semicolon is
7609 invalid. Allow it unless we're being pedantic. */
7610 cp_lexer_consume_token (parser->lexer);
7611 if (pedantic && !in_system_header)
7612 pedwarn ("extra %<;%>");
7616 /* If we're entering or exiting a region that's implicitly
7617 extern "C", modify the lang context appropriately. */
7618 if (!parser->implicit_extern_c && token->implicit_extern_c)
7620 push_lang_context (lang_name_c);
7621 parser->implicit_extern_c = true;
7623 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7625 pop_lang_context ();
7626 parser->implicit_extern_c = false;
7629 if (token->type == CPP_PRAGMA)
7631 /* A top-level declaration can consist solely of a #pragma.
7632 A nested declaration cannot, so this is done here and not
7633 in cp_parser_declaration. (A #pragma at block scope is
7634 handled in cp_parser_statement.) */
7635 cp_parser_pragma (parser, pragma_external);
7639 /* Parse the declaration itself. */
7640 cp_parser_declaration (parser);
7644 /* Parse a declaration.
7649 template-declaration
7650 explicit-instantiation
7651 explicit-specialization
7652 linkage-specification
7653 namespace-definition
7658 __extension__ declaration */
7661 cp_parser_declaration (cp_parser* parser)
7668 /* Check for the `__extension__' keyword. */
7669 if (cp_parser_extension_opt (parser, &saved_pedantic))
7671 /* Parse the qualified declaration. */
7672 cp_parser_declaration (parser);
7673 /* Restore the PEDANTIC flag. */
7674 pedantic = saved_pedantic;
7679 /* Try to figure out what kind of declaration is present. */
7680 token1 = *cp_lexer_peek_token (parser->lexer);
7682 if (token1.type != CPP_EOF)
7683 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7686 token2.type = CPP_EOF;
7687 token2.keyword = RID_MAX;
7690 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7691 p = obstack_alloc (&declarator_obstack, 0);
7693 /* If the next token is `extern' and the following token is a string
7694 literal, then we have a linkage specification. */
7695 if (token1.keyword == RID_EXTERN
7696 && cp_parser_is_string_literal (&token2))
7697 cp_parser_linkage_specification (parser);
7698 /* If the next token is `template', then we have either a template
7699 declaration, an explicit instantiation, or an explicit
7701 else if (token1.keyword == RID_TEMPLATE)
7703 /* `template <>' indicates a template specialization. */
7704 if (token2.type == CPP_LESS
7705 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7706 cp_parser_explicit_specialization (parser);
7707 /* `template <' indicates a template declaration. */
7708 else if (token2.type == CPP_LESS)
7709 cp_parser_template_declaration (parser, /*member_p=*/false);
7710 /* Anything else must be an explicit instantiation. */
7712 cp_parser_explicit_instantiation (parser);
7714 /* If the next token is `export', then we have a template
7716 else if (token1.keyword == RID_EXPORT)
7717 cp_parser_template_declaration (parser, /*member_p=*/false);
7718 /* If the next token is `extern', 'static' or 'inline' and the one
7719 after that is `template', we have a GNU extended explicit
7720 instantiation directive. */
7721 else if (cp_parser_allow_gnu_extensions_p (parser)
7722 && (token1.keyword == RID_EXTERN
7723 || token1.keyword == RID_STATIC
7724 || token1.keyword == RID_INLINE)
7725 && token2.keyword == RID_TEMPLATE)
7726 cp_parser_explicit_instantiation (parser);
7727 /* If the next token is `namespace', check for a named or unnamed
7728 namespace definition. */
7729 else if (token1.keyword == RID_NAMESPACE
7730 && (/* A named namespace definition. */
7731 (token2.type == CPP_NAME
7732 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7734 /* An unnamed namespace definition. */
7735 || token2.type == CPP_OPEN_BRACE
7736 || token2.keyword == RID_ATTRIBUTE))
7737 cp_parser_namespace_definition (parser);
7738 /* Objective-C++ declaration/definition. */
7739 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7740 cp_parser_objc_declaration (parser);
7741 /* We must have either a block declaration or a function
7744 /* Try to parse a block-declaration, or a function-definition. */
7745 cp_parser_block_declaration (parser, /*statement_p=*/false);
7747 /* Free any declarators allocated. */
7748 obstack_free (&declarator_obstack, p);
7751 /* Parse a block-declaration.
7756 namespace-alias-definition
7763 __extension__ block-declaration
7768 static_assert-declaration
7770 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7771 part of a declaration-statement. */
7774 cp_parser_block_declaration (cp_parser *parser,
7780 /* Check for the `__extension__' keyword. */
7781 if (cp_parser_extension_opt (parser, &saved_pedantic))
7783 /* Parse the qualified declaration. */
7784 cp_parser_block_declaration (parser, statement_p);
7785 /* Restore the PEDANTIC flag. */
7786 pedantic = saved_pedantic;
7791 /* Peek at the next token to figure out which kind of declaration is
7793 token1 = cp_lexer_peek_token (parser->lexer);
7795 /* If the next keyword is `asm', we have an asm-definition. */
7796 if (token1->keyword == RID_ASM)
7799 cp_parser_commit_to_tentative_parse (parser);
7800 cp_parser_asm_definition (parser);
7802 /* If the next keyword is `namespace', we have a
7803 namespace-alias-definition. */
7804 else if (token1->keyword == RID_NAMESPACE)
7805 cp_parser_namespace_alias_definition (parser);
7806 /* If the next keyword is `using', we have either a
7807 using-declaration or a using-directive. */
7808 else if (token1->keyword == RID_USING)
7813 cp_parser_commit_to_tentative_parse (parser);
7814 /* If the token after `using' is `namespace', then we have a
7816 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7817 if (token2->keyword == RID_NAMESPACE)
7818 cp_parser_using_directive (parser);
7819 /* Otherwise, it's a using-declaration. */
7821 cp_parser_using_declaration (parser,
7822 /*access_declaration_p=*/false);
7824 /* If the next keyword is `__label__' we have a misplaced label
7826 else if (token1->keyword == RID_LABEL)
7828 cp_lexer_consume_token (parser->lexer);
7829 error ("%<__label__%> not at the beginning of a block");
7830 cp_parser_skip_to_end_of_statement (parser);
7831 /* If the next token is now a `;', consume it. */
7832 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7833 cp_lexer_consume_token (parser->lexer);
7835 /* If the next token is `static_assert' we have a static assertion. */
7836 else if (token1->keyword == RID_STATIC_ASSERT)
7837 cp_parser_static_assert (parser, /*member_p=*/false);
7838 /* Anything else must be a simple-declaration. */
7840 cp_parser_simple_declaration (parser, !statement_p);
7843 /* Parse a simple-declaration.
7846 decl-specifier-seq [opt] init-declarator-list [opt] ;
7848 init-declarator-list:
7850 init-declarator-list , init-declarator
7852 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7853 function-definition as a simple-declaration. */
7856 cp_parser_simple_declaration (cp_parser* parser,
7857 bool function_definition_allowed_p)
7859 cp_decl_specifier_seq decl_specifiers;
7860 int declares_class_or_enum;
7861 bool saw_declarator;
7863 /* Defer access checks until we know what is being declared; the
7864 checks for names appearing in the decl-specifier-seq should be
7865 done as if we were in the scope of the thing being declared. */
7866 push_deferring_access_checks (dk_deferred);
7868 /* Parse the decl-specifier-seq. We have to keep track of whether
7869 or not the decl-specifier-seq declares a named class or
7870 enumeration type, since that is the only case in which the
7871 init-declarator-list is allowed to be empty.
7875 In a simple-declaration, the optional init-declarator-list can be
7876 omitted only when declaring a class or enumeration, that is when
7877 the decl-specifier-seq contains either a class-specifier, an
7878 elaborated-type-specifier, or an enum-specifier. */
7879 cp_parser_decl_specifier_seq (parser,
7880 CP_PARSER_FLAGS_OPTIONAL,
7882 &declares_class_or_enum);
7883 /* We no longer need to defer access checks. */
7884 stop_deferring_access_checks ();
7886 /* In a block scope, a valid declaration must always have a
7887 decl-specifier-seq. By not trying to parse declarators, we can
7888 resolve the declaration/expression ambiguity more quickly. */
7889 if (!function_definition_allowed_p
7890 && !decl_specifiers.any_specifiers_p)
7892 cp_parser_error (parser, "expected declaration");
7896 /* If the next two tokens are both identifiers, the code is
7897 erroneous. The usual cause of this situation is code like:
7901 where "T" should name a type -- but does not. */
7902 if (!decl_specifiers.type
7903 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7905 /* If parsing tentatively, we should commit; we really are
7906 looking at a declaration. */
7907 cp_parser_commit_to_tentative_parse (parser);
7912 /* If we have seen at least one decl-specifier, and the next token
7913 is not a parenthesis, then we must be looking at a declaration.
7914 (After "int (" we might be looking at a functional cast.) */
7915 if (decl_specifiers.any_specifiers_p
7916 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7917 cp_parser_commit_to_tentative_parse (parser);
7919 /* Keep going until we hit the `;' at the end of the simple
7921 saw_declarator = false;
7922 while (cp_lexer_next_token_is_not (parser->lexer,
7926 bool function_definition_p;
7931 /* If we are processing next declarator, coma is expected */
7932 token = cp_lexer_peek_token (parser->lexer);
7933 gcc_assert (token->type == CPP_COMMA);
7934 cp_lexer_consume_token (parser->lexer);
7937 saw_declarator = true;
7939 /* Parse the init-declarator. */
7940 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7942 function_definition_allowed_p,
7944 declares_class_or_enum,
7945 &function_definition_p);
7946 /* If an error occurred while parsing tentatively, exit quickly.
7947 (That usually happens when in the body of a function; each
7948 statement is treated as a declaration-statement until proven
7950 if (cp_parser_error_occurred (parser))
7952 /* Handle function definitions specially. */
7953 if (function_definition_p)
7955 /* If the next token is a `,', then we are probably
7956 processing something like:
7960 which is erroneous. */
7961 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7962 error ("mixing declarations and function-definitions is forbidden");
7963 /* Otherwise, we're done with the list of declarators. */
7966 pop_deferring_access_checks ();
7970 /* The next token should be either a `,' or a `;'. */
7971 token = cp_lexer_peek_token (parser->lexer);
7972 /* If it's a `,', there are more declarators to come. */
7973 if (token->type == CPP_COMMA)
7974 /* will be consumed next time around */;
7975 /* If it's a `;', we are done. */
7976 else if (token->type == CPP_SEMICOLON)
7978 /* Anything else is an error. */
7981 /* If we have already issued an error message we don't need
7982 to issue another one. */
7983 if (decl != error_mark_node
7984 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7985 cp_parser_error (parser, "expected %<,%> or %<;%>");
7986 /* Skip tokens until we reach the end of the statement. */
7987 cp_parser_skip_to_end_of_statement (parser);
7988 /* If the next token is now a `;', consume it. */
7989 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7990 cp_lexer_consume_token (parser->lexer);
7993 /* After the first time around, a function-definition is not
7994 allowed -- even if it was OK at first. For example:
7999 function_definition_allowed_p = false;
8002 /* Issue an error message if no declarators are present, and the
8003 decl-specifier-seq does not itself declare a class or
8005 if (!saw_declarator)
8007 if (cp_parser_declares_only_class_p (parser))
8008 shadow_tag (&decl_specifiers);
8009 /* Perform any deferred access checks. */
8010 perform_deferred_access_checks ();
8013 /* Consume the `;'. */
8014 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8017 pop_deferring_access_checks ();
8020 /* Parse a decl-specifier-seq.
8023 decl-specifier-seq [opt] decl-specifier
8026 storage-class-specifier
8037 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8039 The parser flags FLAGS is used to control type-specifier parsing.
8041 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8044 1: one of the decl-specifiers is an elaborated-type-specifier
8045 (i.e., a type declaration)
8046 2: one of the decl-specifiers is an enum-specifier or a
8047 class-specifier (i.e., a type definition)
8052 cp_parser_decl_specifier_seq (cp_parser* parser,
8053 cp_parser_flags flags,
8054 cp_decl_specifier_seq *decl_specs,
8055 int* declares_class_or_enum)
8057 bool constructor_possible_p = !parser->in_declarator_p;
8059 /* Clear DECL_SPECS. */
8060 clear_decl_specs (decl_specs);
8062 /* Assume no class or enumeration type is declared. */
8063 *declares_class_or_enum = 0;
8065 /* Keep reading specifiers until there are no more to read. */
8069 bool found_decl_spec;
8072 /* Peek at the next token. */
8073 token = cp_lexer_peek_token (parser->lexer);
8074 /* Handle attributes. */
8075 if (token->keyword == RID_ATTRIBUTE)
8077 /* Parse the attributes. */
8078 decl_specs->attributes
8079 = chainon (decl_specs->attributes,
8080 cp_parser_attributes_opt (parser));
8083 /* Assume we will find a decl-specifier keyword. */
8084 found_decl_spec = true;
8085 /* If the next token is an appropriate keyword, we can simply
8086 add it to the list. */
8087 switch (token->keyword)
8092 if (!at_class_scope_p ())
8094 error ("%<friend%> used outside of class");
8095 cp_lexer_purge_token (parser->lexer);
8099 ++decl_specs->specs[(int) ds_friend];
8100 /* Consume the token. */
8101 cp_lexer_consume_token (parser->lexer);
8105 /* function-specifier:
8112 cp_parser_function_specifier_opt (parser, decl_specs);
8118 ++decl_specs->specs[(int) ds_typedef];
8119 /* Consume the token. */
8120 cp_lexer_consume_token (parser->lexer);
8121 /* A constructor declarator cannot appear in a typedef. */
8122 constructor_possible_p = false;
8123 /* The "typedef" keyword can only occur in a declaration; we
8124 may as well commit at this point. */
8125 cp_parser_commit_to_tentative_parse (parser);
8127 if (decl_specs->storage_class != sc_none)
8128 decl_specs->conflicting_specifiers_p = true;
8131 /* storage-class-specifier:
8145 /* Consume the token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8150 /* Consume the token. */
8151 cp_lexer_consume_token (parser->lexer);
8152 ++decl_specs->specs[(int) ds_thread];
8156 /* We did not yet find a decl-specifier yet. */
8157 found_decl_spec = false;
8161 /* Constructors are a special case. The `S' in `S()' is not a
8162 decl-specifier; it is the beginning of the declarator. */
8165 && constructor_possible_p
8166 && (cp_parser_constructor_declarator_p
8167 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8169 /* If we don't have a DECL_SPEC yet, then we must be looking at
8170 a type-specifier. */
8171 if (!found_decl_spec && !constructor_p)
8173 int decl_spec_declares_class_or_enum;
8174 bool is_cv_qualifier;
8178 = cp_parser_type_specifier (parser, flags,
8180 /*is_declaration=*/true,
8181 &decl_spec_declares_class_or_enum,
8184 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8186 /* If this type-specifier referenced a user-defined type
8187 (a typedef, class-name, etc.), then we can't allow any
8188 more such type-specifiers henceforth.
8192 The longest sequence of decl-specifiers that could
8193 possibly be a type name is taken as the
8194 decl-specifier-seq of a declaration. The sequence shall
8195 be self-consistent as described below.
8199 As a general rule, at most one type-specifier is allowed
8200 in the complete decl-specifier-seq of a declaration. The
8201 only exceptions are the following:
8203 -- const or volatile can be combined with any other
8206 -- signed or unsigned can be combined with char, long,
8214 void g (const int Pc);
8216 Here, Pc is *not* part of the decl-specifier seq; it's
8217 the declarator. Therefore, once we see a type-specifier
8218 (other than a cv-qualifier), we forbid any additional
8219 user-defined types. We *do* still allow things like `int
8220 int' to be considered a decl-specifier-seq, and issue the
8221 error message later. */
8222 if (type_spec && !is_cv_qualifier)
8223 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8224 /* A constructor declarator cannot follow a type-specifier. */
8227 constructor_possible_p = false;
8228 found_decl_spec = true;
8232 /* If we still do not have a DECL_SPEC, then there are no more
8234 if (!found_decl_spec)
8237 decl_specs->any_specifiers_p = true;
8238 /* After we see one decl-specifier, further decl-specifiers are
8240 flags |= CP_PARSER_FLAGS_OPTIONAL;
8243 cp_parser_check_decl_spec (decl_specs);
8245 /* Don't allow a friend specifier with a class definition. */
8246 if (decl_specs->specs[(int) ds_friend] != 0
8247 && (*declares_class_or_enum & 2))
8248 error ("class definition may not be declared a friend");
8251 /* Parse an (optional) storage-class-specifier.
8253 storage-class-specifier:
8262 storage-class-specifier:
8265 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8268 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8270 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8278 /* Consume the token. */
8279 return cp_lexer_consume_token (parser->lexer)->u.value;
8286 /* Parse an (optional) function-specifier.
8293 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8294 Updates DECL_SPECS, if it is non-NULL. */
8297 cp_parser_function_specifier_opt (cp_parser* parser,
8298 cp_decl_specifier_seq *decl_specs)
8300 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8304 ++decl_specs->specs[(int) ds_inline];
8308 /* 14.5.2.3 [temp.mem]
8310 A member function template shall not be virtual. */
8311 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8312 error ("templates may not be %<virtual%>");
8313 else if (decl_specs)
8314 ++decl_specs->specs[(int) ds_virtual];
8319 ++decl_specs->specs[(int) ds_explicit];
8326 /* Consume the token. */
8327 return cp_lexer_consume_token (parser->lexer)->u.value;
8330 /* Parse a linkage-specification.
8332 linkage-specification:
8333 extern string-literal { declaration-seq [opt] }
8334 extern string-literal declaration */
8337 cp_parser_linkage_specification (cp_parser* parser)
8341 /* Look for the `extern' keyword. */
8342 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8344 /* Look for the string-literal. */
8345 linkage = cp_parser_string_literal (parser, false, false);
8347 /* Transform the literal into an identifier. If the literal is a
8348 wide-character string, or contains embedded NULs, then we can't
8349 handle it as the user wants. */
8350 if (strlen (TREE_STRING_POINTER (linkage))
8351 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8353 cp_parser_error (parser, "invalid linkage-specification");
8354 /* Assume C++ linkage. */
8355 linkage = lang_name_cplusplus;
8358 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8360 /* We're now using the new linkage. */
8361 push_lang_context (linkage);
8363 /* If the next token is a `{', then we're using the first
8365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8367 /* Consume the `{' token. */
8368 cp_lexer_consume_token (parser->lexer);
8369 /* Parse the declarations. */
8370 cp_parser_declaration_seq_opt (parser);
8371 /* Look for the closing `}'. */
8372 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8374 /* Otherwise, there's just one declaration. */
8377 bool saved_in_unbraced_linkage_specification_p;
8379 saved_in_unbraced_linkage_specification_p
8380 = parser->in_unbraced_linkage_specification_p;
8381 parser->in_unbraced_linkage_specification_p = true;
8382 cp_parser_declaration (parser);
8383 parser->in_unbraced_linkage_specification_p
8384 = saved_in_unbraced_linkage_specification_p;
8387 /* We're done with the linkage-specification. */
8388 pop_lang_context ();
8391 /* Parse a static_assert-declaration.
8393 static_assert-declaration:
8394 static_assert ( constant-expression , string-literal ) ;
8396 If MEMBER_P, this static_assert is a class member. */
8399 cp_parser_static_assert(cp_parser *parser, bool member_p)
8404 location_t saved_loc;
8406 /* Peek at the `static_assert' token so we can keep track of exactly
8407 where the static assertion started. */
8408 token = cp_lexer_peek_token (parser->lexer);
8409 saved_loc = token->location;
8411 /* Look for the `static_assert' keyword. */
8412 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8416 /* We know we are in a static assertion; commit to any tentative
8418 if (cp_parser_parsing_tentatively (parser))
8419 cp_parser_commit_to_tentative_parse (parser);
8421 /* Parse the `(' starting the static assertion condition. */
8422 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8424 /* Parse the constant-expression. */
8426 cp_parser_constant_expression (parser,
8427 /*allow_non_constant_p=*/false,
8428 /*non_constant_p=*/NULL);
8430 /* Parse the separating `,'. */
8431 cp_parser_require (parser, CPP_COMMA, "`,'");
8433 /* Parse the string-literal message. */
8434 message = cp_parser_string_literal (parser,
8435 /*translate=*/false,
8438 /* A `)' completes the static assertion. */
8439 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8440 cp_parser_skip_to_closing_parenthesis (parser,
8441 /*recovering=*/true,
8443 /*consume_paren=*/true);
8445 /* A semicolon terminates the declaration. */
8446 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8448 /* Complete the static assertion, which may mean either processing
8449 the static assert now or saving it for template instantiation. */
8450 finish_static_assert (condition, message, saved_loc, member_p);
8453 /* Parse a `decltype' type. Returns the type.
8455 simple-type-specifier:
8456 decltype ( expression ) */
8459 cp_parser_decltype (cp_parser *parser)
8462 bool id_expression_or_member_access_p = false;
8463 const char *saved_message;
8464 bool saved_integral_constant_expression_p;
8465 bool saved_non_integral_constant_expression_p;
8467 /* Look for the `decltype' token. */
8468 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8469 return error_mark_node;
8471 /* Types cannot be defined in a `decltype' expression. Save away the
8473 saved_message = parser->type_definition_forbidden_message;
8475 /* And create the new one. */
8476 parser->type_definition_forbidden_message
8477 = "types may not be defined in `decltype' expressions";
8479 /* The restrictions on constant-expressions do not apply inside
8480 decltype expressions. */
8481 saved_integral_constant_expression_p
8482 = parser->integral_constant_expression_p;
8483 saved_non_integral_constant_expression_p
8484 = parser->non_integral_constant_expression_p;
8485 parser->integral_constant_expression_p = false;
8487 /* Do not actually evaluate the expression. */
8490 /* Parse the opening `('. */
8491 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8492 return error_mark_node;
8494 /* First, try parsing an id-expression. */
8495 cp_parser_parse_tentatively (parser);
8496 expr = cp_parser_id_expression (parser,
8497 /*template_keyword_p=*/false,
8498 /*check_dependency_p=*/true,
8499 /*template_p=*/NULL,
8500 /*declarator_p=*/false,
8501 /*optional_p=*/false);
8503 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8505 bool non_integral_constant_expression_p = false;
8506 tree id_expression = expr;
8508 const char *error_msg;
8510 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8511 /* Lookup the name we got back from the id-expression. */
8512 expr = cp_parser_lookup_name (parser, expr,
8514 /*is_template=*/false,
8515 /*is_namespace=*/false,
8516 /*check_dependency=*/true,
8517 /*ambiguous_decls=*/NULL);
8520 && expr != error_mark_node
8521 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8522 && TREE_CODE (expr) != TYPE_DECL
8523 && (TREE_CODE (expr) != BIT_NOT_EXPR
8524 || !TYPE_P (TREE_OPERAND (expr, 0)))
8525 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8527 /* Complete lookup of the id-expression. */
8528 expr = (finish_id_expression
8529 (id_expression, expr, parser->scope, &idk,
8530 /*integral_constant_expression_p=*/false,
8531 /*allow_non_integral_constant_expression_p=*/true,
8532 &non_integral_constant_expression_p,
8533 /*template_p=*/false,
8535 /*address_p=*/false,
8536 /*template_arg_p=*/false,
8539 if (expr == error_mark_node)
8540 /* We found an id-expression, but it was something that we
8541 should not have found. This is an error, not something
8542 we can recover from, so note that we found an
8543 id-expression and we'll recover as gracefully as
8545 id_expression_or_member_access_p = true;
8549 && expr != error_mark_node
8550 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8551 /* We have an id-expression. */
8552 id_expression_or_member_access_p = true;
8555 if (!id_expression_or_member_access_p)
8557 /* Abort the id-expression parse. */
8558 cp_parser_abort_tentative_parse (parser);
8560 /* Parsing tentatively, again. */
8561 cp_parser_parse_tentatively (parser);
8563 /* Parse a class member access. */
8564 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8566 /*member_access_only_p=*/true);
8569 && expr != error_mark_node
8570 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8571 /* We have an id-expression. */
8572 id_expression_or_member_access_p = true;
8575 if (id_expression_or_member_access_p)
8576 /* We have parsed the complete id-expression or member access. */
8577 cp_parser_parse_definitely (parser);
8580 /* Abort our attempt to parse an id-expression or member access
8582 cp_parser_abort_tentative_parse (parser);
8584 /* Parse a full expression. */
8585 expr = cp_parser_expression (parser, /*cast_p=*/false);
8588 /* Go back to evaluating expressions. */
8591 /* Restore the old message and the integral constant expression
8593 parser->type_definition_forbidden_message = saved_message;
8594 parser->integral_constant_expression_p
8595 = saved_integral_constant_expression_p;
8596 parser->non_integral_constant_expression_p
8597 = saved_non_integral_constant_expression_p;
8599 if (expr == error_mark_node)
8601 /* Skip everything up to the closing `)'. */
8602 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8603 /*consume_paren=*/true);
8604 return error_mark_node;
8607 /* Parse to the closing `)'. */
8608 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8610 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8611 /*consume_paren=*/true);
8612 return error_mark_node;
8615 return finish_decltype_type (expr, id_expression_or_member_access_p);
8618 /* Special member functions [gram.special] */
8620 /* Parse a conversion-function-id.
8622 conversion-function-id:
8623 operator conversion-type-id
8625 Returns an IDENTIFIER_NODE representing the operator. */
8628 cp_parser_conversion_function_id (cp_parser* parser)
8632 tree saved_qualifying_scope;
8633 tree saved_object_scope;
8634 tree pushed_scope = NULL_TREE;
8636 /* Look for the `operator' token. */
8637 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8638 return error_mark_node;
8639 /* When we parse the conversion-type-id, the current scope will be
8640 reset. However, we need that information in able to look up the
8641 conversion function later, so we save it here. */
8642 saved_scope = parser->scope;
8643 saved_qualifying_scope = parser->qualifying_scope;
8644 saved_object_scope = parser->object_scope;
8645 /* We must enter the scope of the class so that the names of
8646 entities declared within the class are available in the
8647 conversion-type-id. For example, consider:
8654 S::operator I() { ... }
8656 In order to see that `I' is a type-name in the definition, we
8657 must be in the scope of `S'. */
8659 pushed_scope = push_scope (saved_scope);
8660 /* Parse the conversion-type-id. */
8661 type = cp_parser_conversion_type_id (parser);
8662 /* Leave the scope of the class, if any. */
8664 pop_scope (pushed_scope);
8665 /* Restore the saved scope. */
8666 parser->scope = saved_scope;
8667 parser->qualifying_scope = saved_qualifying_scope;
8668 parser->object_scope = saved_object_scope;
8669 /* If the TYPE is invalid, indicate failure. */
8670 if (type == error_mark_node)
8671 return error_mark_node;
8672 return mangle_conv_op_name_for_type (type);
8675 /* Parse a conversion-type-id:
8678 type-specifier-seq conversion-declarator [opt]
8680 Returns the TYPE specified. */
8683 cp_parser_conversion_type_id (cp_parser* parser)
8686 cp_decl_specifier_seq type_specifiers;
8687 cp_declarator *declarator;
8688 tree type_specified;
8690 /* Parse the attributes. */
8691 attributes = cp_parser_attributes_opt (parser);
8692 /* Parse the type-specifiers. */
8693 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8695 /* If that didn't work, stop. */
8696 if (type_specifiers.type == error_mark_node)
8697 return error_mark_node;
8698 /* Parse the conversion-declarator. */
8699 declarator = cp_parser_conversion_declarator_opt (parser);
8701 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8702 /*initialized=*/0, &attributes);
8704 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8705 return type_specified;
8708 /* Parse an (optional) conversion-declarator.
8710 conversion-declarator:
8711 ptr-operator conversion-declarator [opt]
8715 static cp_declarator *
8716 cp_parser_conversion_declarator_opt (cp_parser* parser)
8718 enum tree_code code;
8720 cp_cv_quals cv_quals;
8722 /* We don't know if there's a ptr-operator next, or not. */
8723 cp_parser_parse_tentatively (parser);
8724 /* Try the ptr-operator. */
8725 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8726 /* If it worked, look for more conversion-declarators. */
8727 if (cp_parser_parse_definitely (parser))
8729 cp_declarator *declarator;
8731 /* Parse another optional declarator. */
8732 declarator = cp_parser_conversion_declarator_opt (parser);
8734 return cp_parser_make_indirect_declarator
8735 (code, class_type, cv_quals, declarator);
8741 /* Parse an (optional) ctor-initializer.
8744 : mem-initializer-list
8746 Returns TRUE iff the ctor-initializer was actually present. */
8749 cp_parser_ctor_initializer_opt (cp_parser* parser)
8751 /* If the next token is not a `:', then there is no
8752 ctor-initializer. */
8753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8755 /* Do default initialization of any bases and members. */
8756 if (DECL_CONSTRUCTOR_P (current_function_decl))
8757 finish_mem_initializers (NULL_TREE);
8762 /* Consume the `:' token. */
8763 cp_lexer_consume_token (parser->lexer);
8764 /* And the mem-initializer-list. */
8765 cp_parser_mem_initializer_list (parser);
8770 /* Parse a mem-initializer-list.
8772 mem-initializer-list:
8773 mem-initializer ... [opt]
8774 mem-initializer ... [opt] , mem-initializer-list */
8777 cp_parser_mem_initializer_list (cp_parser* parser)
8779 tree mem_initializer_list = NULL_TREE;
8781 /* Let the semantic analysis code know that we are starting the
8782 mem-initializer-list. */
8783 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8784 error ("only constructors take base initializers");
8786 /* Loop through the list. */
8789 tree mem_initializer;
8791 /* Parse the mem-initializer. */
8792 mem_initializer = cp_parser_mem_initializer (parser);
8793 /* If the next token is a `...', we're expanding member initializers. */
8794 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8796 /* Consume the `...'. */
8797 cp_lexer_consume_token (parser->lexer);
8799 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8800 can be expanded but members cannot. */
8801 if (mem_initializer != error_mark_node
8802 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8804 error ("cannot expand initializer for member %<%D%>",
8805 TREE_PURPOSE (mem_initializer));
8806 mem_initializer = error_mark_node;
8809 /* Construct the pack expansion type. */
8810 if (mem_initializer != error_mark_node)
8811 mem_initializer = make_pack_expansion (mem_initializer);
8813 /* Add it to the list, unless it was erroneous. */
8814 if (mem_initializer != error_mark_node)
8816 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8817 mem_initializer_list = mem_initializer;
8819 /* If the next token is not a `,', we're done. */
8820 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8822 /* Consume the `,' token. */
8823 cp_lexer_consume_token (parser->lexer);
8826 /* Perform semantic analysis. */
8827 if (DECL_CONSTRUCTOR_P (current_function_decl))
8828 finish_mem_initializers (mem_initializer_list);
8831 /* Parse a mem-initializer.
8834 mem-initializer-id ( expression-list [opt] )
8839 ( expression-list [opt] )
8841 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8842 class) or FIELD_DECL (for a non-static data member) to initialize;
8843 the TREE_VALUE is the expression-list. An empty initialization
8844 list is represented by void_list_node. */
8847 cp_parser_mem_initializer (cp_parser* parser)
8849 tree mem_initializer_id;
8850 tree expression_list;
8853 /* Find out what is being initialized. */
8854 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8856 pedwarn ("anachronistic old-style base class initializer");
8857 mem_initializer_id = NULL_TREE;
8860 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8861 member = expand_member_init (mem_initializer_id);
8862 if (member && !DECL_P (member))
8863 in_base_initializer = 1;
8866 = cp_parser_parenthesized_expression_list (parser, false,
8868 /*allow_expansion_p=*/true,
8869 /*non_constant_p=*/NULL);
8870 if (expression_list == error_mark_node)
8871 return error_mark_node;
8872 if (!expression_list)
8873 expression_list = void_type_node;
8875 in_base_initializer = 0;
8877 return member ? build_tree_list (member, expression_list) : error_mark_node;
8880 /* Parse a mem-initializer-id.
8883 :: [opt] nested-name-specifier [opt] class-name
8886 Returns a TYPE indicating the class to be initializer for the first
8887 production. Returns an IDENTIFIER_NODE indicating the data member
8888 to be initialized for the second production. */
8891 cp_parser_mem_initializer_id (cp_parser* parser)
8893 bool global_scope_p;
8894 bool nested_name_specifier_p;
8895 bool template_p = false;
8898 /* `typename' is not allowed in this context ([temp.res]). */
8899 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8901 error ("keyword %<typename%> not allowed in this context (a qualified "
8902 "member initializer is implicitly a type)");
8903 cp_lexer_consume_token (parser->lexer);
8905 /* Look for the optional `::' operator. */
8907 = (cp_parser_global_scope_opt (parser,
8908 /*current_scope_valid_p=*/false)
8910 /* Look for the optional nested-name-specifier. The simplest way to
8915 The keyword `typename' is not permitted in a base-specifier or
8916 mem-initializer; in these contexts a qualified name that
8917 depends on a template-parameter is implicitly assumed to be a
8920 is to assume that we have seen the `typename' keyword at this
8922 nested_name_specifier_p
8923 = (cp_parser_nested_name_specifier_opt (parser,
8924 /*typename_keyword_p=*/true,
8925 /*check_dependency_p=*/true,
8927 /*is_declaration=*/true)
8929 if (nested_name_specifier_p)
8930 template_p = cp_parser_optional_template_keyword (parser);
8931 /* If there is a `::' operator or a nested-name-specifier, then we
8932 are definitely looking for a class-name. */
8933 if (global_scope_p || nested_name_specifier_p)
8934 return cp_parser_class_name (parser,
8935 /*typename_keyword_p=*/true,
8936 /*template_keyword_p=*/template_p,
8938 /*check_dependency_p=*/true,
8939 /*class_head_p=*/false,
8940 /*is_declaration=*/true);
8941 /* Otherwise, we could also be looking for an ordinary identifier. */
8942 cp_parser_parse_tentatively (parser);
8943 /* Try a class-name. */
8944 id = cp_parser_class_name (parser,
8945 /*typename_keyword_p=*/true,
8946 /*template_keyword_p=*/false,
8948 /*check_dependency_p=*/true,
8949 /*class_head_p=*/false,
8950 /*is_declaration=*/true);
8951 /* If we found one, we're done. */
8952 if (cp_parser_parse_definitely (parser))
8954 /* Otherwise, look for an ordinary identifier. */
8955 return cp_parser_identifier (parser);
8958 /* Overloading [gram.over] */
8960 /* Parse an operator-function-id.
8962 operator-function-id:
8965 Returns an IDENTIFIER_NODE for the operator which is a
8966 human-readable spelling of the identifier, e.g., `operator +'. */
8969 cp_parser_operator_function_id (cp_parser* parser)
8971 /* Look for the `operator' keyword. */
8972 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8973 return error_mark_node;
8974 /* And then the name of the operator itself. */
8975 return cp_parser_operator (parser);
8978 /* Parse an operator.
8981 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8982 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8983 || ++ -- , ->* -> () []
8990 Returns an IDENTIFIER_NODE for the operator which is a
8991 human-readable spelling of the identifier, e.g., `operator +'. */
8994 cp_parser_operator (cp_parser* parser)
8996 tree id = NULL_TREE;
8999 /* Peek at the next token. */
9000 token = cp_lexer_peek_token (parser->lexer);
9001 /* Figure out which operator we have. */
9002 switch (token->type)
9008 /* The keyword should be either `new' or `delete'. */
9009 if (token->keyword == RID_NEW)
9011 else if (token->keyword == RID_DELETE)
9016 /* Consume the `new' or `delete' token. */
9017 cp_lexer_consume_token (parser->lexer);
9019 /* Peek at the next token. */
9020 token = cp_lexer_peek_token (parser->lexer);
9021 /* If it's a `[' token then this is the array variant of the
9023 if (token->type == CPP_OPEN_SQUARE)
9025 /* Consume the `[' token. */
9026 cp_lexer_consume_token (parser->lexer);
9027 /* Look for the `]' token. */
9028 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9029 id = ansi_opname (op == NEW_EXPR
9030 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9032 /* Otherwise, we have the non-array variant. */
9034 id = ansi_opname (op);
9040 id = ansi_opname (PLUS_EXPR);
9044 id = ansi_opname (MINUS_EXPR);
9048 id = ansi_opname (MULT_EXPR);
9052 id = ansi_opname (TRUNC_DIV_EXPR);
9056 id = ansi_opname (TRUNC_MOD_EXPR);
9060 id = ansi_opname (BIT_XOR_EXPR);
9064 id = ansi_opname (BIT_AND_EXPR);
9068 id = ansi_opname (BIT_IOR_EXPR);
9072 id = ansi_opname (BIT_NOT_EXPR);
9076 id = ansi_opname (TRUTH_NOT_EXPR);
9080 id = ansi_assopname (NOP_EXPR);
9084 id = ansi_opname (LT_EXPR);
9088 id = ansi_opname (GT_EXPR);
9092 id = ansi_assopname (PLUS_EXPR);
9096 id = ansi_assopname (MINUS_EXPR);
9100 id = ansi_assopname (MULT_EXPR);
9104 id = ansi_assopname (TRUNC_DIV_EXPR);
9108 id = ansi_assopname (TRUNC_MOD_EXPR);
9112 id = ansi_assopname (BIT_XOR_EXPR);
9116 id = ansi_assopname (BIT_AND_EXPR);
9120 id = ansi_assopname (BIT_IOR_EXPR);
9124 id = ansi_opname (LSHIFT_EXPR);
9128 id = ansi_opname (RSHIFT_EXPR);
9132 id = ansi_assopname (LSHIFT_EXPR);
9136 id = ansi_assopname (RSHIFT_EXPR);
9140 id = ansi_opname (EQ_EXPR);
9144 id = ansi_opname (NE_EXPR);
9148 id = ansi_opname (LE_EXPR);
9151 case CPP_GREATER_EQ:
9152 id = ansi_opname (GE_EXPR);
9156 id = ansi_opname (TRUTH_ANDIF_EXPR);
9160 id = ansi_opname (TRUTH_ORIF_EXPR);
9164 id = ansi_opname (POSTINCREMENT_EXPR);
9167 case CPP_MINUS_MINUS:
9168 id = ansi_opname (PREDECREMENT_EXPR);
9172 id = ansi_opname (COMPOUND_EXPR);
9175 case CPP_DEREF_STAR:
9176 id = ansi_opname (MEMBER_REF);
9180 id = ansi_opname (COMPONENT_REF);
9183 case CPP_OPEN_PAREN:
9184 /* Consume the `('. */
9185 cp_lexer_consume_token (parser->lexer);
9186 /* Look for the matching `)'. */
9187 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9188 return ansi_opname (CALL_EXPR);
9190 case CPP_OPEN_SQUARE:
9191 /* Consume the `['. */
9192 cp_lexer_consume_token (parser->lexer);
9193 /* Look for the matching `]'. */
9194 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9195 return ansi_opname (ARRAY_REF);
9198 /* Anything else is an error. */
9202 /* If we have selected an identifier, we need to consume the
9205 cp_lexer_consume_token (parser->lexer);
9206 /* Otherwise, no valid operator name was present. */
9209 cp_parser_error (parser, "expected operator");
9210 id = error_mark_node;
9216 /* Parse a template-declaration.
9218 template-declaration:
9219 export [opt] template < template-parameter-list > declaration
9221 If MEMBER_P is TRUE, this template-declaration occurs within a
9224 The grammar rule given by the standard isn't correct. What
9227 template-declaration:
9228 export [opt] template-parameter-list-seq
9229 decl-specifier-seq [opt] init-declarator [opt] ;
9230 export [opt] template-parameter-list-seq
9233 template-parameter-list-seq:
9234 template-parameter-list-seq [opt]
9235 template < template-parameter-list > */
9238 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9240 /* Check for `export'. */
9241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9243 /* Consume the `export' token. */
9244 cp_lexer_consume_token (parser->lexer);
9245 /* Warn that we do not support `export'. */
9246 warning (0, "keyword %<export%> not implemented, and will be ignored");
9249 cp_parser_template_declaration_after_export (parser, member_p);
9252 /* Parse a template-parameter-list.
9254 template-parameter-list:
9256 template-parameter-list , template-parameter
9258 Returns a TREE_LIST. Each node represents a template parameter.
9259 The nodes are connected via their TREE_CHAINs. */
9262 cp_parser_template_parameter_list (cp_parser* parser)
9264 tree parameter_list = NULL_TREE;
9266 begin_template_parm_list ();
9272 bool is_parameter_pack;
9274 /* Parse the template-parameter. */
9275 parameter = cp_parser_template_parameter (parser,
9277 &is_parameter_pack);
9278 /* Add it to the list. */
9279 if (parameter != error_mark_node)
9280 parameter_list = process_template_parm (parameter_list,
9286 tree err_parm = build_tree_list (parameter, parameter);
9287 TREE_VALUE (err_parm) = error_mark_node;
9288 parameter_list = chainon (parameter_list, err_parm);
9291 /* Peek at the next token. */
9292 token = cp_lexer_peek_token (parser->lexer);
9293 /* If it's not a `,', we're done. */
9294 if (token->type != CPP_COMMA)
9296 /* Otherwise, consume the `,' token. */
9297 cp_lexer_consume_token (parser->lexer);
9300 return end_template_parm_list (parameter_list);
9303 /* Parse a template-parameter.
9307 parameter-declaration
9309 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9310 the parameter. The TREE_PURPOSE is the default value, if any.
9311 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9312 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9313 set to true iff this parameter is a parameter pack. */
9316 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9317 bool *is_parameter_pack)
9320 cp_parameter_declarator *parameter_declarator;
9321 cp_declarator *id_declarator;
9324 /* Assume it is a type parameter or a template parameter. */
9325 *is_non_type = false;
9326 /* Assume it not a parameter pack. */
9327 *is_parameter_pack = false;
9328 /* Peek at the next token. */
9329 token = cp_lexer_peek_token (parser->lexer);
9330 /* If it is `class' or `template', we have a type-parameter. */
9331 if (token->keyword == RID_TEMPLATE)
9332 return cp_parser_type_parameter (parser, is_parameter_pack);
9333 /* If it is `class' or `typename' we do not know yet whether it is a
9334 type parameter or a non-type parameter. Consider:
9336 template <typename T, typename T::X X> ...
9340 template <class C, class D*> ...
9342 Here, the first parameter is a type parameter, and the second is
9343 a non-type parameter. We can tell by looking at the token after
9344 the identifier -- if it is a `,', `=', or `>' then we have a type
9346 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9348 /* Peek at the token after `class' or `typename'. */
9349 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9350 /* If it's an ellipsis, we have a template type parameter
9352 if (token->type == CPP_ELLIPSIS)
9353 return cp_parser_type_parameter (parser, is_parameter_pack);
9354 /* If it's an identifier, skip it. */
9355 if (token->type == CPP_NAME)
9356 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9357 /* Now, see if the token looks like the end of a template
9359 if (token->type == CPP_COMMA
9360 || token->type == CPP_EQ
9361 || token->type == CPP_GREATER)
9362 return cp_parser_type_parameter (parser, is_parameter_pack);
9365 /* Otherwise, it is a non-type parameter.
9369 When parsing a default template-argument for a non-type
9370 template-parameter, the first non-nested `>' is taken as the end
9371 of the template parameter-list rather than a greater-than
9373 *is_non_type = true;
9374 parameter_declarator
9375 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9376 /*parenthesized_p=*/NULL);
9378 /* If the parameter declaration is marked as a parameter pack, set
9379 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9380 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9382 if (parameter_declarator
9383 && parameter_declarator->declarator
9384 && parameter_declarator->declarator->parameter_pack_p)
9386 *is_parameter_pack = true;
9387 parameter_declarator->declarator->parameter_pack_p = false;
9390 /* If the next token is an ellipsis, and we don't already have it
9391 marked as a parameter pack, then we have a parameter pack (that
9392 has no declarator). */
9393 if (!*is_parameter_pack
9394 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9395 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9397 /* Consume the `...'. */
9398 cp_lexer_consume_token (parser->lexer);
9399 maybe_warn_variadic_templates ();
9401 *is_parameter_pack = true;
9403 /* Parameter packs cannot have default arguments. However, a
9404 user may try to do so, so we'll parse them and give an
9405 appropriate diagnostic here. */
9406 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9408 /* Consume the `='. */
9409 cp_lexer_consume_token (parser->lexer);
9411 /* Find the name of the parameter pack. */
9412 id_declarator = parameter_declarator->declarator;
9413 while (id_declarator && id_declarator->kind != cdk_id)
9414 id_declarator = id_declarator->declarator;
9416 if (id_declarator && id_declarator->kind == cdk_id)
9417 error ("template parameter pack %qD cannot have a default argument",
9418 id_declarator->u.id.unqualified_name);
9420 error ("template parameter pack cannot have a default argument");
9422 /* Parse the default argument, but throw away the result. */
9423 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9427 parm = grokdeclarator (parameter_declarator->declarator,
9428 ¶meter_declarator->decl_specifiers,
9429 PARM, /*initialized=*/0,
9431 if (parm == error_mark_node)
9432 return error_mark_node;
9434 return build_tree_list (parameter_declarator->default_argument, parm);
9437 /* Parse a type-parameter.
9440 class identifier [opt]
9441 class identifier [opt] = type-id
9442 typename identifier [opt]
9443 typename identifier [opt] = type-id
9444 template < template-parameter-list > class identifier [opt]
9445 template < template-parameter-list > class identifier [opt]
9448 GNU Extension (variadic templates):
9451 class ... identifier [opt]
9452 typename ... identifier [opt]
9454 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9455 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9456 the declaration of the parameter.
9458 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9461 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9466 /* Look for a keyword to tell us what kind of parameter this is. */
9467 token = cp_parser_require (parser, CPP_KEYWORD,
9468 "`class', `typename', or `template'");
9470 return error_mark_node;
9472 switch (token->keyword)
9478 tree default_argument;
9480 /* If the next token is an ellipsis, we have a template
9482 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9484 /* Consume the `...' token. */
9485 cp_lexer_consume_token (parser->lexer);
9486 maybe_warn_variadic_templates ();
9488 *is_parameter_pack = true;
9491 /* If the next token is an identifier, then it names the
9493 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9494 identifier = cp_parser_identifier (parser);
9496 identifier = NULL_TREE;
9498 /* Create the parameter. */
9499 parameter = finish_template_type_parm (class_type_node, identifier);
9501 /* If the next token is an `=', we have a default argument. */
9502 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9504 /* Consume the `=' token. */
9505 cp_lexer_consume_token (parser->lexer);
9506 /* Parse the default-argument. */
9507 push_deferring_access_checks (dk_no_deferred);
9508 default_argument = cp_parser_type_id (parser);
9510 /* Template parameter packs cannot have default
9512 if (*is_parameter_pack)
9515 error ("template parameter pack %qD cannot have a default argument",
9518 error ("template parameter packs cannot have default arguments");
9519 default_argument = NULL_TREE;
9521 pop_deferring_access_checks ();
9524 default_argument = NULL_TREE;
9526 /* Create the combined representation of the parameter and the
9527 default argument. */
9528 parameter = build_tree_list (default_argument, parameter);
9534 tree parameter_list;
9536 tree default_argument;
9538 /* Look for the `<'. */
9539 cp_parser_require (parser, CPP_LESS, "`<'");
9540 /* Parse the template-parameter-list. */
9541 parameter_list = cp_parser_template_parameter_list (parser);
9542 /* Look for the `>'. */
9543 cp_parser_require (parser, CPP_GREATER, "`>'");
9544 /* Look for the `class' keyword. */
9545 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9546 /* If the next token is an ellipsis, we have a template
9548 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9550 /* Consume the `...' token. */
9551 cp_lexer_consume_token (parser->lexer);
9552 maybe_warn_variadic_templates ();
9554 *is_parameter_pack = true;
9556 /* If the next token is an `=', then there is a
9557 default-argument. If the next token is a `>', we are at
9558 the end of the parameter-list. If the next token is a `,',
9559 then we are at the end of this parameter. */
9560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9561 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9562 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9564 identifier = cp_parser_identifier (parser);
9565 /* Treat invalid names as if the parameter were nameless. */
9566 if (identifier == error_mark_node)
9567 identifier = NULL_TREE;
9570 identifier = NULL_TREE;
9572 /* Create the template parameter. */
9573 parameter = finish_template_template_parm (class_type_node,
9576 /* If the next token is an `=', then there is a
9577 default-argument. */
9578 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9582 /* Consume the `='. */
9583 cp_lexer_consume_token (parser->lexer);
9584 /* Parse the id-expression. */
9585 push_deferring_access_checks (dk_no_deferred);
9587 = cp_parser_id_expression (parser,
9588 /*template_keyword_p=*/false,
9589 /*check_dependency_p=*/true,
9590 /*template_p=*/&is_template,
9591 /*declarator_p=*/false,
9592 /*optional_p=*/false);
9593 if (TREE_CODE (default_argument) == TYPE_DECL)
9594 /* If the id-expression was a template-id that refers to
9595 a template-class, we already have the declaration here,
9596 so no further lookup is needed. */
9599 /* Look up the name. */
9601 = cp_parser_lookup_name (parser, default_argument,
9603 /*is_template=*/is_template,
9604 /*is_namespace=*/false,
9605 /*check_dependency=*/true,
9606 /*ambiguous_decls=*/NULL);
9607 /* See if the default argument is valid. */
9609 = check_template_template_default_arg (default_argument);
9611 /* Template parameter packs cannot have default
9613 if (*is_parameter_pack)
9616 error ("template parameter pack %qD cannot have a default argument",
9619 error ("template parameter packs cannot have default arguments");
9620 default_argument = NULL_TREE;
9622 pop_deferring_access_checks ();
9625 default_argument = NULL_TREE;
9627 /* Create the combined representation of the parameter and the
9628 default argument. */
9629 parameter = build_tree_list (default_argument, parameter);
9641 /* Parse a template-id.
9644 template-name < template-argument-list [opt] >
9646 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9647 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9648 returned. Otherwise, if the template-name names a function, or set
9649 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9650 names a class, returns a TYPE_DECL for the specialization.
9652 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9653 uninstantiated templates. */
9656 cp_parser_template_id (cp_parser *parser,
9657 bool template_keyword_p,
9658 bool check_dependency_p,
9659 bool is_declaration)
9665 cp_token_position start_of_id = 0;
9666 deferred_access_check *chk;
9667 VEC (deferred_access_check,gc) *access_check;
9668 cp_token *next_token, *next_token_2;
9671 /* If the next token corresponds to a template-id, there is no need
9673 next_token = cp_lexer_peek_token (parser->lexer);
9674 if (next_token->type == CPP_TEMPLATE_ID)
9676 struct tree_check *check_value;
9678 /* Get the stored value. */
9679 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9680 /* Perform any access checks that were deferred. */
9681 access_check = check_value->checks;
9685 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9688 perform_or_defer_access_check (chk->binfo,
9693 /* Return the stored value. */
9694 return check_value->value;
9697 /* Avoid performing name lookup if there is no possibility of
9698 finding a template-id. */
9699 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9700 || (next_token->type == CPP_NAME
9701 && !cp_parser_nth_token_starts_template_argument_list_p
9704 cp_parser_error (parser, "expected template-id");
9705 return error_mark_node;
9708 /* Remember where the template-id starts. */
9709 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9710 start_of_id = cp_lexer_token_position (parser->lexer, false);
9712 push_deferring_access_checks (dk_deferred);
9714 /* Parse the template-name. */
9715 is_identifier = false;
9716 template = cp_parser_template_name (parser, template_keyword_p,
9720 if (template == error_mark_node || is_identifier)
9722 pop_deferring_access_checks ();
9726 /* If we find the sequence `[:' after a template-name, it's probably
9727 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9728 parse correctly the argument list. */
9729 next_token = cp_lexer_peek_token (parser->lexer);
9730 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9731 if (next_token->type == CPP_OPEN_SQUARE
9732 && next_token->flags & DIGRAPH
9733 && next_token_2->type == CPP_COLON
9734 && !(next_token_2->flags & PREV_WHITE))
9736 cp_parser_parse_tentatively (parser);
9737 /* Change `:' into `::'. */
9738 next_token_2->type = CPP_SCOPE;
9739 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9741 cp_lexer_consume_token (parser->lexer);
9742 /* Parse the arguments. */
9743 arguments = cp_parser_enclosed_template_argument_list (parser);
9744 if (!cp_parser_parse_definitely (parser))
9746 /* If we couldn't parse an argument list, then we revert our changes
9747 and return simply an error. Maybe this is not a template-id
9749 next_token_2->type = CPP_COLON;
9750 cp_parser_error (parser, "expected %<<%>");
9751 pop_deferring_access_checks ();
9752 return error_mark_node;
9754 /* Otherwise, emit an error about the invalid digraph, but continue
9755 parsing because we got our argument list. */
9756 pedwarn ("%<<::%> cannot begin a template-argument list");
9757 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9758 "between %<<%> and %<::%>");
9759 if (!flag_permissive)
9764 inform ("(if you use -fpermissive G++ will accept your code)");
9771 /* Look for the `<' that starts the template-argument-list. */
9772 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9774 pop_deferring_access_checks ();
9775 return error_mark_node;
9777 /* Parse the arguments. */
9778 arguments = cp_parser_enclosed_template_argument_list (parser);
9781 /* Build a representation of the specialization. */
9782 if (TREE_CODE (template) == IDENTIFIER_NODE)
9783 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9784 else if (DECL_CLASS_TEMPLATE_P (template)
9785 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9787 bool entering_scope;
9788 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9789 template (rather than some instantiation thereof) only if
9790 is not nested within some other construct. For example, in
9791 "template <typename T> void f(T) { A<T>::", A<T> is just an
9792 instantiation of A. */
9793 entering_scope = (template_parm_scope_p ()
9794 && cp_lexer_next_token_is (parser->lexer,
9797 = finish_template_type (template, arguments, entering_scope);
9801 /* If it's not a class-template or a template-template, it should be
9802 a function-template. */
9803 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9804 || TREE_CODE (template) == OVERLOAD
9805 || BASELINK_P (template)));
9807 template_id = lookup_template_function (template, arguments);
9810 /* If parsing tentatively, replace the sequence of tokens that makes
9811 up the template-id with a CPP_TEMPLATE_ID token. That way,
9812 should we re-parse the token stream, we will not have to repeat
9813 the effort required to do the parse, nor will we issue duplicate
9814 error messages about problems during instantiation of the
9818 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9820 /* Reset the contents of the START_OF_ID token. */
9821 token->type = CPP_TEMPLATE_ID;
9822 /* Retrieve any deferred checks. Do not pop this access checks yet
9823 so the memory will not be reclaimed during token replacing below. */
9824 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9825 token->u.tree_check_value->value = template_id;
9826 token->u.tree_check_value->checks = get_deferred_access_checks ();
9827 token->keyword = RID_MAX;
9829 /* Purge all subsequent tokens. */
9830 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9832 /* ??? Can we actually assume that, if template_id ==
9833 error_mark_node, we will have issued a diagnostic to the
9834 user, as opposed to simply marking the tentative parse as
9836 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9837 error ("parse error in template argument list");
9840 pop_deferring_access_checks ();
9844 /* Parse a template-name.
9849 The standard should actually say:
9853 operator-function-id
9855 A defect report has been filed about this issue.
9857 A conversion-function-id cannot be a template name because they cannot
9858 be part of a template-id. In fact, looking at this code:
9862 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9863 It is impossible to call a templated conversion-function-id with an
9864 explicit argument list, since the only allowed template parameter is
9865 the type to which it is converting.
9867 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9868 `template' keyword, in a construction like:
9872 In that case `f' is taken to be a template-name, even though there
9873 is no way of knowing for sure.
9875 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9876 name refers to a set of overloaded functions, at least one of which
9877 is a template, or an IDENTIFIER_NODE with the name of the template,
9878 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9879 names are looked up inside uninstantiated templates. */
9882 cp_parser_template_name (cp_parser* parser,
9883 bool template_keyword_p,
9884 bool check_dependency_p,
9885 bool is_declaration,
9886 bool *is_identifier)
9892 /* If the next token is `operator', then we have either an
9893 operator-function-id or a conversion-function-id. */
9894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9896 /* We don't know whether we're looking at an
9897 operator-function-id or a conversion-function-id. */
9898 cp_parser_parse_tentatively (parser);
9899 /* Try an operator-function-id. */
9900 identifier = cp_parser_operator_function_id (parser);
9901 /* If that didn't work, try a conversion-function-id. */
9902 if (!cp_parser_parse_definitely (parser))
9904 cp_parser_error (parser, "expected template-name");
9905 return error_mark_node;
9908 /* Look for the identifier. */
9910 identifier = cp_parser_identifier (parser);
9912 /* If we didn't find an identifier, we don't have a template-id. */
9913 if (identifier == error_mark_node)
9914 return error_mark_node;
9916 /* If the name immediately followed the `template' keyword, then it
9917 is a template-name. However, if the next token is not `<', then
9918 we do not treat it as a template-name, since it is not being used
9919 as part of a template-id. This enables us to handle constructs
9922 template <typename T> struct S { S(); };
9923 template <typename T> S<T>::S();
9925 correctly. We would treat `S' as a template -- if it were `S<T>'
9926 -- but we do not if there is no `<'. */
9928 if (processing_template_decl
9929 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9931 /* In a declaration, in a dependent context, we pretend that the
9932 "template" keyword was present in order to improve error
9933 recovery. For example, given:
9935 template <typename T> void f(T::X<int>);
9937 we want to treat "X<int>" as a template-id. */
9939 && !template_keyword_p
9940 && parser->scope && TYPE_P (parser->scope)
9941 && check_dependency_p
9942 && dependent_type_p (parser->scope)
9943 /* Do not do this for dtors (or ctors), since they never
9944 need the template keyword before their name. */
9945 && !constructor_name_p (identifier, parser->scope))
9947 cp_token_position start = 0;
9949 /* Explain what went wrong. */
9950 error ("non-template %qD used as template", identifier);
9951 inform ("use %<%T::template %D%> to indicate that it is a template",
9952 parser->scope, identifier);
9953 /* If parsing tentatively, find the location of the "<" token. */
9954 if (cp_parser_simulate_error (parser))
9955 start = cp_lexer_token_position (parser->lexer, true);
9956 /* Parse the template arguments so that we can issue error
9957 messages about them. */
9958 cp_lexer_consume_token (parser->lexer);
9959 cp_parser_enclosed_template_argument_list (parser);
9960 /* Skip tokens until we find a good place from which to
9961 continue parsing. */
9962 cp_parser_skip_to_closing_parenthesis (parser,
9963 /*recovering=*/true,
9965 /*consume_paren=*/false);
9966 /* If parsing tentatively, permanently remove the
9967 template argument list. That will prevent duplicate
9968 error messages from being issued about the missing
9969 "template" keyword. */
9971 cp_lexer_purge_tokens_after (parser->lexer, start);
9973 *is_identifier = true;
9977 /* If the "template" keyword is present, then there is generally
9978 no point in doing name-lookup, so we just return IDENTIFIER.
9979 But, if the qualifying scope is non-dependent then we can
9980 (and must) do name-lookup normally. */
9981 if (template_keyword_p
9983 || (TYPE_P (parser->scope)
9984 && dependent_type_p (parser->scope))))
9988 /* Look up the name. */
9989 decl = cp_parser_lookup_name (parser, identifier,
9991 /*is_template=*/false,
9992 /*is_namespace=*/false,
9994 /*ambiguous_decls=*/NULL);
9995 decl = maybe_get_template_decl_from_type_decl (decl);
9997 /* If DECL is a template, then the name was a template-name. */
9998 if (TREE_CODE (decl) == TEMPLATE_DECL)
10002 tree fn = NULL_TREE;
10004 /* The standard does not explicitly indicate whether a name that
10005 names a set of overloaded declarations, some of which are
10006 templates, is a template-name. However, such a name should
10007 be a template-name; otherwise, there is no way to form a
10008 template-id for the overloaded templates. */
10009 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10010 if (TREE_CODE (fns) == OVERLOAD)
10011 for (fn = fns; fn; fn = OVL_NEXT (fn))
10012 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10017 /* The name does not name a template. */
10018 cp_parser_error (parser, "expected template-name");
10019 return error_mark_node;
10023 /* If DECL is dependent, and refers to a function, then just return
10024 its name; we will look it up again during template instantiation. */
10025 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10027 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10028 if (TYPE_P (scope) && dependent_type_p (scope))
10035 /* Parse a template-argument-list.
10037 template-argument-list:
10038 template-argument ... [opt]
10039 template-argument-list , template-argument ... [opt]
10041 Returns a TREE_VEC containing the arguments. */
10044 cp_parser_template_argument_list (cp_parser* parser)
10046 tree fixed_args[10];
10047 unsigned n_args = 0;
10048 unsigned alloced = 10;
10049 tree *arg_ary = fixed_args;
10051 bool saved_in_template_argument_list_p;
10053 bool saved_non_ice_p;
10055 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10056 parser->in_template_argument_list_p = true;
10057 /* Even if the template-id appears in an integral
10058 constant-expression, the contents of the argument list do
10060 saved_ice_p = parser->integral_constant_expression_p;
10061 parser->integral_constant_expression_p = false;
10062 saved_non_ice_p = parser->non_integral_constant_expression_p;
10063 parser->non_integral_constant_expression_p = false;
10064 /* Parse the arguments. */
10070 /* Consume the comma. */
10071 cp_lexer_consume_token (parser->lexer);
10073 /* Parse the template-argument. */
10074 argument = cp_parser_template_argument (parser);
10076 /* If the next token is an ellipsis, we're expanding a template
10078 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10080 /* Consume the `...' token. */
10081 cp_lexer_consume_token (parser->lexer);
10083 /* Make the argument into a TYPE_PACK_EXPANSION or
10084 EXPR_PACK_EXPANSION. */
10085 argument = make_pack_expansion (argument);
10088 if (n_args == alloced)
10092 if (arg_ary == fixed_args)
10094 arg_ary = XNEWVEC (tree, alloced);
10095 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10098 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10100 arg_ary[n_args++] = argument;
10102 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10104 vec = make_tree_vec (n_args);
10107 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10109 if (arg_ary != fixed_args)
10111 parser->non_integral_constant_expression_p = saved_non_ice_p;
10112 parser->integral_constant_expression_p = saved_ice_p;
10113 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10117 /* Parse a template-argument.
10120 assignment-expression
10124 The representation is that of an assignment-expression, type-id, or
10125 id-expression -- except that the qualified id-expression is
10126 evaluated, so that the value returned is either a DECL or an
10129 Although the standard says "assignment-expression", it forbids
10130 throw-expressions or assignments in the template argument.
10131 Therefore, we use "conditional-expression" instead. */
10134 cp_parser_template_argument (cp_parser* parser)
10139 bool maybe_type_id = false;
10143 /* There's really no way to know what we're looking at, so we just
10144 try each alternative in order.
10148 In a template-argument, an ambiguity between a type-id and an
10149 expression is resolved to a type-id, regardless of the form of
10150 the corresponding template-parameter.
10152 Therefore, we try a type-id first. */
10153 cp_parser_parse_tentatively (parser);
10154 argument = cp_parser_type_id (parser);
10155 /* If there was no error parsing the type-id but the next token is a '>>',
10156 we probably found a typo for '> >'. But there are type-id which are
10157 also valid expressions. For instance:
10159 struct X { int operator >> (int); };
10160 template <int V> struct Foo {};
10163 Here 'X()' is a valid type-id of a function type, but the user just
10164 wanted to write the expression "X() >> 5". Thus, we remember that we
10165 found a valid type-id, but we still try to parse the argument as an
10166 expression to see what happens. */
10167 if (!cp_parser_error_occurred (parser)
10168 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10170 maybe_type_id = true;
10171 cp_parser_abort_tentative_parse (parser);
10175 /* If the next token isn't a `,' or a `>', then this argument wasn't
10176 really finished. This means that the argument is not a valid
10178 if (!cp_parser_next_token_ends_template_argument_p (parser))
10179 cp_parser_error (parser, "expected template-argument");
10180 /* If that worked, we're done. */
10181 if (cp_parser_parse_definitely (parser))
10184 /* We're still not sure what the argument will be. */
10185 cp_parser_parse_tentatively (parser);
10186 /* Try a template. */
10187 argument = cp_parser_id_expression (parser,
10188 /*template_keyword_p=*/false,
10189 /*check_dependency_p=*/true,
10191 /*declarator_p=*/false,
10192 /*optional_p=*/false);
10193 /* If the next token isn't a `,' or a `>', then this argument wasn't
10194 really finished. */
10195 if (!cp_parser_next_token_ends_template_argument_p (parser))
10196 cp_parser_error (parser, "expected template-argument");
10197 if (!cp_parser_error_occurred (parser))
10199 /* Figure out what is being referred to. If the id-expression
10200 was for a class template specialization, then we will have a
10201 TYPE_DECL at this point. There is no need to do name lookup
10202 at this point in that case. */
10203 if (TREE_CODE (argument) != TYPE_DECL)
10204 argument = cp_parser_lookup_name (parser, argument,
10206 /*is_template=*/template_p,
10207 /*is_namespace=*/false,
10208 /*check_dependency=*/true,
10209 /*ambiguous_decls=*/NULL);
10210 if (TREE_CODE (argument) != TEMPLATE_DECL
10211 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10212 cp_parser_error (parser, "expected template-name");
10214 if (cp_parser_parse_definitely (parser))
10216 /* It must be a non-type argument. There permitted cases are given
10217 in [temp.arg.nontype]:
10219 -- an integral constant-expression of integral or enumeration
10222 -- the name of a non-type template-parameter; or
10224 -- the name of an object or function with external linkage...
10226 -- the address of an object or function with external linkage...
10228 -- a pointer to member... */
10229 /* Look for a non-type template parameter. */
10230 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10232 cp_parser_parse_tentatively (parser);
10233 argument = cp_parser_primary_expression (parser,
10234 /*adress_p=*/false,
10236 /*template_arg_p=*/true,
10238 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10239 || !cp_parser_next_token_ends_template_argument_p (parser))
10240 cp_parser_simulate_error (parser);
10241 if (cp_parser_parse_definitely (parser))
10245 /* If the next token is "&", the argument must be the address of an
10246 object or function with external linkage. */
10247 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10249 cp_lexer_consume_token (parser->lexer);
10250 /* See if we might have an id-expression. */
10251 token = cp_lexer_peek_token (parser->lexer);
10252 if (token->type == CPP_NAME
10253 || token->keyword == RID_OPERATOR
10254 || token->type == CPP_SCOPE
10255 || token->type == CPP_TEMPLATE_ID
10256 || token->type == CPP_NESTED_NAME_SPECIFIER)
10258 cp_parser_parse_tentatively (parser);
10259 argument = cp_parser_primary_expression (parser,
10262 /*template_arg_p=*/true,
10264 if (cp_parser_error_occurred (parser)
10265 || !cp_parser_next_token_ends_template_argument_p (parser))
10266 cp_parser_abort_tentative_parse (parser);
10269 if (TREE_CODE (argument) == INDIRECT_REF)
10271 gcc_assert (REFERENCE_REF_P (argument));
10272 argument = TREE_OPERAND (argument, 0);
10275 if (TREE_CODE (argument) == VAR_DECL)
10277 /* A variable without external linkage might still be a
10278 valid constant-expression, so no error is issued here
10279 if the external-linkage check fails. */
10280 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10281 cp_parser_simulate_error (parser);
10283 else if (is_overloaded_fn (argument))
10284 /* All overloaded functions are allowed; if the external
10285 linkage test does not pass, an error will be issued
10289 && (TREE_CODE (argument) == OFFSET_REF
10290 || TREE_CODE (argument) == SCOPE_REF))
10291 /* A pointer-to-member. */
10293 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10296 cp_parser_simulate_error (parser);
10298 if (cp_parser_parse_definitely (parser))
10301 argument = build_x_unary_op (ADDR_EXPR, argument);
10306 /* If the argument started with "&", there are no other valid
10307 alternatives at this point. */
10310 cp_parser_error (parser, "invalid non-type template argument");
10311 return error_mark_node;
10314 /* If the argument wasn't successfully parsed as a type-id followed
10315 by '>>', the argument can only be a constant expression now.
10316 Otherwise, we try parsing the constant-expression tentatively,
10317 because the argument could really be a type-id. */
10319 cp_parser_parse_tentatively (parser);
10320 argument = cp_parser_constant_expression (parser,
10321 /*allow_non_constant_p=*/false,
10322 /*non_constant_p=*/NULL);
10323 argument = fold_non_dependent_expr (argument);
10324 if (!maybe_type_id)
10326 if (!cp_parser_next_token_ends_template_argument_p (parser))
10327 cp_parser_error (parser, "expected template-argument");
10328 if (cp_parser_parse_definitely (parser))
10330 /* We did our best to parse the argument as a non type-id, but that
10331 was the only alternative that matched (albeit with a '>' after
10332 it). We can assume it's just a typo from the user, and a
10333 diagnostic will then be issued. */
10334 return cp_parser_type_id (parser);
10337 /* Parse an explicit-instantiation.
10339 explicit-instantiation:
10340 template declaration
10342 Although the standard says `declaration', what it really means is:
10344 explicit-instantiation:
10345 template decl-specifier-seq [opt] declarator [opt] ;
10347 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10348 supposed to be allowed. A defect report has been filed about this
10353 explicit-instantiation:
10354 storage-class-specifier template
10355 decl-specifier-seq [opt] declarator [opt] ;
10356 function-specifier template
10357 decl-specifier-seq [opt] declarator [opt] ; */
10360 cp_parser_explicit_instantiation (cp_parser* parser)
10362 int declares_class_or_enum;
10363 cp_decl_specifier_seq decl_specifiers;
10364 tree extension_specifier = NULL_TREE;
10366 /* Look for an (optional) storage-class-specifier or
10367 function-specifier. */
10368 if (cp_parser_allow_gnu_extensions_p (parser))
10370 extension_specifier
10371 = cp_parser_storage_class_specifier_opt (parser);
10372 if (!extension_specifier)
10373 extension_specifier
10374 = cp_parser_function_specifier_opt (parser,
10375 /*decl_specs=*/NULL);
10378 /* Look for the `template' keyword. */
10379 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10380 /* Let the front end know that we are processing an explicit
10382 begin_explicit_instantiation ();
10383 /* [temp.explicit] says that we are supposed to ignore access
10384 control while processing explicit instantiation directives. */
10385 push_deferring_access_checks (dk_no_check);
10386 /* Parse a decl-specifier-seq. */
10387 cp_parser_decl_specifier_seq (parser,
10388 CP_PARSER_FLAGS_OPTIONAL,
10390 &declares_class_or_enum);
10391 /* If there was exactly one decl-specifier, and it declared a class,
10392 and there's no declarator, then we have an explicit type
10394 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10398 type = check_tag_decl (&decl_specifiers);
10399 /* Turn access control back on for names used during
10400 template instantiation. */
10401 pop_deferring_access_checks ();
10403 do_type_instantiation (type, extension_specifier,
10404 /*complain=*/tf_error);
10408 cp_declarator *declarator;
10411 /* Parse the declarator. */
10413 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10414 /*ctor_dtor_or_conv_p=*/NULL,
10415 /*parenthesized_p=*/NULL,
10416 /*member_p=*/false);
10417 if (declares_class_or_enum & 2)
10418 cp_parser_check_for_definition_in_return_type (declarator,
10419 decl_specifiers.type);
10420 if (declarator != cp_error_declarator)
10422 decl = grokdeclarator (declarator, &decl_specifiers,
10423 NORMAL, 0, &decl_specifiers.attributes);
10424 /* Turn access control back on for names used during
10425 template instantiation. */
10426 pop_deferring_access_checks ();
10427 /* Do the explicit instantiation. */
10428 do_decl_instantiation (decl, extension_specifier);
10432 pop_deferring_access_checks ();
10433 /* Skip the body of the explicit instantiation. */
10434 cp_parser_skip_to_end_of_statement (parser);
10437 /* We're done with the instantiation. */
10438 end_explicit_instantiation ();
10440 cp_parser_consume_semicolon_at_end_of_statement (parser);
10443 /* Parse an explicit-specialization.
10445 explicit-specialization:
10446 template < > declaration
10448 Although the standard says `declaration', what it really means is:
10450 explicit-specialization:
10451 template <> decl-specifier [opt] init-declarator [opt] ;
10452 template <> function-definition
10453 template <> explicit-specialization
10454 template <> template-declaration */
10457 cp_parser_explicit_specialization (cp_parser* parser)
10459 bool need_lang_pop;
10460 /* Look for the `template' keyword. */
10461 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10462 /* Look for the `<'. */
10463 cp_parser_require (parser, CPP_LESS, "`<'");
10464 /* Look for the `>'. */
10465 cp_parser_require (parser, CPP_GREATER, "`>'");
10466 /* We have processed another parameter list. */
10467 ++parser->num_template_parameter_lists;
10470 A template ... explicit specialization ... shall not have C
10472 if (current_lang_name == lang_name_c)
10474 error ("template specialization with C linkage");
10475 /* Give it C++ linkage to avoid confusing other parts of the
10477 push_lang_context (lang_name_cplusplus);
10478 need_lang_pop = true;
10481 need_lang_pop = false;
10482 /* Let the front end know that we are beginning a specialization. */
10483 if (!begin_specialization ())
10485 end_specialization ();
10486 cp_parser_skip_to_end_of_block_or_statement (parser);
10490 /* If the next keyword is `template', we need to figure out whether
10491 or not we're looking a template-declaration. */
10492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10494 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10495 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10496 cp_parser_template_declaration_after_export (parser,
10497 /*member_p=*/false);
10499 cp_parser_explicit_specialization (parser);
10502 /* Parse the dependent declaration. */
10503 cp_parser_single_declaration (parser,
10505 /*member_p=*/false,
10506 /*explicit_specialization_p=*/true,
10507 /*friend_p=*/NULL);
10508 /* We're done with the specialization. */
10509 end_specialization ();
10510 /* For the erroneous case of a template with C linkage, we pushed an
10511 implicit C++ linkage scope; exit that scope now. */
10513 pop_lang_context ();
10514 /* We're done with this parameter list. */
10515 --parser->num_template_parameter_lists;
10518 /* Parse a type-specifier.
10521 simple-type-specifier
10524 elaborated-type-specifier
10532 Returns a representation of the type-specifier. For a
10533 class-specifier, enum-specifier, or elaborated-type-specifier, a
10534 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10536 The parser flags FLAGS is used to control type-specifier parsing.
10538 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10539 in a decl-specifier-seq.
10541 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10542 class-specifier, enum-specifier, or elaborated-type-specifier, then
10543 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10544 if a type is declared; 2 if it is defined. Otherwise, it is set to
10547 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10548 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10549 is set to FALSE. */
10552 cp_parser_type_specifier (cp_parser* parser,
10553 cp_parser_flags flags,
10554 cp_decl_specifier_seq *decl_specs,
10555 bool is_declaration,
10556 int* declares_class_or_enum,
10557 bool* is_cv_qualifier)
10559 tree type_spec = NULL_TREE;
10562 cp_decl_spec ds = ds_last;
10564 /* Assume this type-specifier does not declare a new type. */
10565 if (declares_class_or_enum)
10566 *declares_class_or_enum = 0;
10567 /* And that it does not specify a cv-qualifier. */
10568 if (is_cv_qualifier)
10569 *is_cv_qualifier = false;
10570 /* Peek at the next token. */
10571 token = cp_lexer_peek_token (parser->lexer);
10573 /* If we're looking at a keyword, we can use that to guide the
10574 production we choose. */
10575 keyword = token->keyword;
10579 /* Look for the enum-specifier. */
10580 type_spec = cp_parser_enum_specifier (parser);
10581 /* If that worked, we're done. */
10584 if (declares_class_or_enum)
10585 *declares_class_or_enum = 2;
10587 cp_parser_set_decl_spec_type (decl_specs,
10589 /*user_defined_p=*/true);
10593 goto elaborated_type_specifier;
10595 /* Any of these indicate either a class-specifier, or an
10596 elaborated-type-specifier. */
10600 /* Parse tentatively so that we can back up if we don't find a
10601 class-specifier. */
10602 cp_parser_parse_tentatively (parser);
10603 /* Look for the class-specifier. */
10604 type_spec = cp_parser_class_specifier (parser);
10605 /* If that worked, we're done. */
10606 if (cp_parser_parse_definitely (parser))
10608 if (declares_class_or_enum)
10609 *declares_class_or_enum = 2;
10611 cp_parser_set_decl_spec_type (decl_specs,
10613 /*user_defined_p=*/true);
10617 /* Fall through. */
10618 elaborated_type_specifier:
10619 /* We're declaring (not defining) a class or enum. */
10620 if (declares_class_or_enum)
10621 *declares_class_or_enum = 1;
10623 /* Fall through. */
10625 /* Look for an elaborated-type-specifier. */
10627 = (cp_parser_elaborated_type_specifier
10629 decl_specs && decl_specs->specs[(int) ds_friend],
10632 cp_parser_set_decl_spec_type (decl_specs,
10634 /*user_defined_p=*/true);
10639 if (is_cv_qualifier)
10640 *is_cv_qualifier = true;
10645 if (is_cv_qualifier)
10646 *is_cv_qualifier = true;
10651 if (is_cv_qualifier)
10652 *is_cv_qualifier = true;
10656 /* The `__complex__' keyword is a GNU extension. */
10664 /* Handle simple keywords. */
10669 ++decl_specs->specs[(int)ds];
10670 decl_specs->any_specifiers_p = true;
10672 return cp_lexer_consume_token (parser->lexer)->u.value;
10675 /* If we do not already have a type-specifier, assume we are looking
10676 at a simple-type-specifier. */
10677 type_spec = cp_parser_simple_type_specifier (parser,
10681 /* If we didn't find a type-specifier, and a type-specifier was not
10682 optional in this context, issue an error message. */
10683 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10685 cp_parser_error (parser, "expected type specifier");
10686 return error_mark_node;
10692 /* Parse a simple-type-specifier.
10694 simple-type-specifier:
10695 :: [opt] nested-name-specifier [opt] type-name
10696 :: [opt] nested-name-specifier template template-id
10711 simple-type-specifier:
10712 decltype ( expression )
10716 simple-type-specifier:
10717 __typeof__ unary-expression
10718 __typeof__ ( type-id )
10720 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10721 appropriately updated. */
10724 cp_parser_simple_type_specifier (cp_parser* parser,
10725 cp_decl_specifier_seq *decl_specs,
10726 cp_parser_flags flags)
10728 tree type = NULL_TREE;
10731 /* Peek at the next token. */
10732 token = cp_lexer_peek_token (parser->lexer);
10734 /* If we're looking at a keyword, things are easy. */
10735 switch (token->keyword)
10739 decl_specs->explicit_char_p = true;
10740 type = char_type_node;
10743 type = wchar_type_node;
10746 type = boolean_type_node;
10750 ++decl_specs->specs[(int) ds_short];
10751 type = short_integer_type_node;
10755 decl_specs->explicit_int_p = true;
10756 type = integer_type_node;
10760 ++decl_specs->specs[(int) ds_long];
10761 type = long_integer_type_node;
10765 ++decl_specs->specs[(int) ds_signed];
10766 type = integer_type_node;
10770 ++decl_specs->specs[(int) ds_unsigned];
10771 type = unsigned_type_node;
10774 type = float_type_node;
10777 type = double_type_node;
10780 type = void_type_node;
10784 /* Parse the `decltype' type. */
10785 type = cp_parser_decltype (parser);
10788 cp_parser_set_decl_spec_type (decl_specs, type,
10789 /*user_defined_p=*/true);
10794 /* Consume the `typeof' token. */
10795 cp_lexer_consume_token (parser->lexer);
10796 /* Parse the operand to `typeof'. */
10797 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10798 /* If it is not already a TYPE, take its type. */
10799 if (!TYPE_P (type))
10800 type = finish_typeof (type);
10803 cp_parser_set_decl_spec_type (decl_specs, type,
10804 /*user_defined_p=*/true);
10812 /* If the type-specifier was for a built-in type, we're done. */
10817 /* Record the type. */
10819 && (token->keyword != RID_SIGNED
10820 && token->keyword != RID_UNSIGNED
10821 && token->keyword != RID_SHORT
10822 && token->keyword != RID_LONG))
10823 cp_parser_set_decl_spec_type (decl_specs,
10825 /*user_defined=*/false);
10827 decl_specs->any_specifiers_p = true;
10829 /* Consume the token. */
10830 id = cp_lexer_consume_token (parser->lexer)->u.value;
10832 /* There is no valid C++ program where a non-template type is
10833 followed by a "<". That usually indicates that the user thought
10834 that the type was a template. */
10835 cp_parser_check_for_invalid_template_id (parser, type);
10837 return TYPE_NAME (type);
10840 /* The type-specifier must be a user-defined type. */
10841 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10846 /* Don't gobble tokens or issue error messages if this is an
10847 optional type-specifier. */
10848 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10849 cp_parser_parse_tentatively (parser);
10851 /* Look for the optional `::' operator. */
10853 = (cp_parser_global_scope_opt (parser,
10854 /*current_scope_valid_p=*/false)
10856 /* Look for the nested-name specifier. */
10858 = (cp_parser_nested_name_specifier_opt (parser,
10859 /*typename_keyword_p=*/false,
10860 /*check_dependency_p=*/true,
10862 /*is_declaration=*/false)
10864 /* If we have seen a nested-name-specifier, and the next token
10865 is `template', then we are using the template-id production. */
10867 && cp_parser_optional_template_keyword (parser))
10869 /* Look for the template-id. */
10870 type = cp_parser_template_id (parser,
10871 /*template_keyword_p=*/true,
10872 /*check_dependency_p=*/true,
10873 /*is_declaration=*/false);
10874 /* If the template-id did not name a type, we are out of
10876 if (TREE_CODE (type) != TYPE_DECL)
10878 cp_parser_error (parser, "expected template-id for type");
10882 /* Otherwise, look for a type-name. */
10884 type = cp_parser_type_name (parser);
10885 /* Keep track of all name-lookups performed in class scopes. */
10889 && TREE_CODE (type) == TYPE_DECL
10890 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10891 maybe_note_name_used_in_class (DECL_NAME (type), type);
10892 /* If it didn't work out, we don't have a TYPE. */
10893 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10894 && !cp_parser_parse_definitely (parser))
10896 if (type && decl_specs)
10897 cp_parser_set_decl_spec_type (decl_specs, type,
10898 /*user_defined=*/true);
10901 /* If we didn't get a type-name, issue an error message. */
10902 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10904 cp_parser_error (parser, "expected type-name");
10905 return error_mark_node;
10908 /* There is no valid C++ program where a non-template type is
10909 followed by a "<". That usually indicates that the user thought
10910 that the type was a template. */
10911 if (type && type != error_mark_node)
10913 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10914 If it is, then the '<'...'>' enclose protocol names rather than
10915 template arguments, and so everything is fine. */
10916 if (c_dialect_objc ()
10917 && (objc_is_id (type) || objc_is_class_name (type)))
10919 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10920 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10922 /* Clobber the "unqualified" type previously entered into
10923 DECL_SPECS with the new, improved protocol-qualified version. */
10925 decl_specs->type = qual_type;
10930 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10936 /* Parse a type-name.
10949 Returns a TYPE_DECL for the type. */
10952 cp_parser_type_name (cp_parser* parser)
10957 /* We can't know yet whether it is a class-name or not. */
10958 cp_parser_parse_tentatively (parser);
10959 /* Try a class-name. */
10960 type_decl = cp_parser_class_name (parser,
10961 /*typename_keyword_p=*/false,
10962 /*template_keyword_p=*/false,
10964 /*check_dependency_p=*/true,
10965 /*class_head_p=*/false,
10966 /*is_declaration=*/false);
10967 /* If it's not a class-name, keep looking. */
10968 if (!cp_parser_parse_definitely (parser))
10970 /* It must be a typedef-name or an enum-name. */
10971 identifier = cp_parser_identifier (parser);
10972 if (identifier == error_mark_node)
10973 return error_mark_node;
10975 /* Look up the type-name. */
10976 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10978 if (TREE_CODE (type_decl) != TYPE_DECL
10979 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10981 /* See if this is an Objective-C type. */
10982 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10983 tree type = objc_get_protocol_qualified_type (identifier, protos);
10985 type_decl = TYPE_NAME (type);
10988 /* Issue an error if we did not find a type-name. */
10989 if (TREE_CODE (type_decl) != TYPE_DECL)
10991 if (!cp_parser_simulate_error (parser))
10992 cp_parser_name_lookup_error (parser, identifier, type_decl,
10994 type_decl = error_mark_node;
10996 /* Remember that the name was used in the definition of the
10997 current class so that we can check later to see if the
10998 meaning would have been different after the class was
10999 entirely defined. */
11000 else if (type_decl != error_mark_node
11002 maybe_note_name_used_in_class (identifier, type_decl);
11009 /* Parse an elaborated-type-specifier. Note that the grammar given
11010 here incorporates the resolution to DR68.
11012 elaborated-type-specifier:
11013 class-key :: [opt] nested-name-specifier [opt] identifier
11014 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11015 enum :: [opt] nested-name-specifier [opt] identifier
11016 typename :: [opt] nested-name-specifier identifier
11017 typename :: [opt] nested-name-specifier template [opt]
11022 elaborated-type-specifier:
11023 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11024 class-key attributes :: [opt] nested-name-specifier [opt]
11025 template [opt] template-id
11026 enum attributes :: [opt] nested-name-specifier [opt] identifier
11028 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11029 declared `friend'. If IS_DECLARATION is TRUE, then this
11030 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11031 something is being declared.
11033 Returns the TYPE specified. */
11036 cp_parser_elaborated_type_specifier (cp_parser* parser,
11038 bool is_declaration)
11040 enum tag_types tag_type;
11042 tree type = NULL_TREE;
11043 tree attributes = NULL_TREE;
11045 /* See if we're looking at the `enum' keyword. */
11046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11048 /* Consume the `enum' token. */
11049 cp_lexer_consume_token (parser->lexer);
11050 /* Remember that it's an enumeration type. */
11051 tag_type = enum_type;
11052 /* Parse the attributes. */
11053 attributes = cp_parser_attributes_opt (parser);
11055 /* Or, it might be `typename'. */
11056 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11059 /* Consume the `typename' token. */
11060 cp_lexer_consume_token (parser->lexer);
11061 /* Remember that it's a `typename' type. */
11062 tag_type = typename_type;
11063 /* The `typename' keyword is only allowed in templates. */
11064 if (!processing_template_decl)
11065 pedwarn ("using %<typename%> outside of template");
11067 /* Otherwise it must be a class-key. */
11070 tag_type = cp_parser_class_key (parser);
11071 if (tag_type == none_type)
11072 return error_mark_node;
11073 /* Parse the attributes. */
11074 attributes = cp_parser_attributes_opt (parser);
11077 /* Look for the `::' operator. */
11078 cp_parser_global_scope_opt (parser,
11079 /*current_scope_valid_p=*/false);
11080 /* Look for the nested-name-specifier. */
11081 if (tag_type == typename_type)
11083 if (!cp_parser_nested_name_specifier (parser,
11084 /*typename_keyword_p=*/true,
11085 /*check_dependency_p=*/true,
11088 return error_mark_node;
11091 /* Even though `typename' is not present, the proposed resolution
11092 to Core Issue 180 says that in `class A<T>::B', `B' should be
11093 considered a type-name, even if `A<T>' is dependent. */
11094 cp_parser_nested_name_specifier_opt (parser,
11095 /*typename_keyword_p=*/true,
11096 /*check_dependency_p=*/true,
11099 /* For everything but enumeration types, consider a template-id.
11100 For an enumeration type, consider only a plain identifier. */
11101 if (tag_type != enum_type)
11103 bool template_p = false;
11106 /* Allow the `template' keyword. */
11107 template_p = cp_parser_optional_template_keyword (parser);
11108 /* If we didn't see `template', we don't know if there's a
11109 template-id or not. */
11111 cp_parser_parse_tentatively (parser);
11112 /* Parse the template-id. */
11113 decl = cp_parser_template_id (parser, template_p,
11114 /*check_dependency_p=*/true,
11116 /* If we didn't find a template-id, look for an ordinary
11118 if (!template_p && !cp_parser_parse_definitely (parser))
11120 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11121 in effect, then we must assume that, upon instantiation, the
11122 template will correspond to a class. */
11123 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11124 && tag_type == typename_type)
11125 type = make_typename_type (parser->scope, decl,
11127 /*complain=*/tf_error);
11129 type = TREE_TYPE (decl);
11134 identifier = cp_parser_identifier (parser);
11136 if (identifier == error_mark_node)
11138 parser->scope = NULL_TREE;
11139 return error_mark_node;
11142 /* For a `typename', we needn't call xref_tag. */
11143 if (tag_type == typename_type
11144 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11145 return cp_parser_make_typename_type (parser, parser->scope,
11147 /* Look up a qualified name in the usual way. */
11151 tree ambiguous_decls;
11153 decl = cp_parser_lookup_name (parser, identifier,
11155 /*is_template=*/false,
11156 /*is_namespace=*/false,
11157 /*check_dependency=*/true,
11160 /* If the lookup was ambiguous, an error will already have been
11162 if (ambiguous_decls)
11163 return error_mark_node;
11165 /* If we are parsing friend declaration, DECL may be a
11166 TEMPLATE_DECL tree node here. However, we need to check
11167 whether this TEMPLATE_DECL results in valid code. Consider
11168 the following example:
11171 template <class T> class C {};
11174 template <class T> friend class N::C; // #1, valid code
11176 template <class T> class Y {
11177 friend class N::C; // #2, invalid code
11180 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11181 name lookup of `N::C'. We see that friend declaration must
11182 be template for the code to be valid. Note that
11183 processing_template_decl does not work here since it is
11184 always 1 for the above two cases. */
11186 decl = (cp_parser_maybe_treat_template_as_class
11187 (decl, /*tag_name_p=*/is_friend
11188 && parser->num_template_parameter_lists));
11190 if (TREE_CODE (decl) != TYPE_DECL)
11192 cp_parser_diagnose_invalid_type_name (parser,
11195 return error_mark_node;
11198 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11200 bool allow_template = (parser->num_template_parameter_lists
11201 || DECL_SELF_REFERENCE_P (decl));
11202 type = check_elaborated_type_specifier (tag_type, decl,
11205 if (type == error_mark_node)
11206 return error_mark_node;
11209 /* Forward declarations of nested types, such as
11214 are invalid unless all components preceding the final '::'
11215 are complete. If all enclosing types are complete, these
11216 declarations become merely pointless.
11218 Invalid forward declarations of nested types are errors
11219 caught elsewhere in parsing. Those that are pointless arrive
11222 if (cp_parser_declares_only_class_p (parser)
11223 && !is_friend && !processing_explicit_instantiation)
11224 warning (0, "declaration %qD does not declare anything", decl);
11226 type = TREE_TYPE (decl);
11230 /* An elaborated-type-specifier sometimes introduces a new type and
11231 sometimes names an existing type. Normally, the rule is that it
11232 introduces a new type only if there is not an existing type of
11233 the same name already in scope. For example, given:
11236 void f() { struct S s; }
11238 the `struct S' in the body of `f' is the same `struct S' as in
11239 the global scope; the existing definition is used. However, if
11240 there were no global declaration, this would introduce a new
11241 local class named `S'.
11243 An exception to this rule applies to the following code:
11245 namespace N { struct S; }
11247 Here, the elaborated-type-specifier names a new type
11248 unconditionally; even if there is already an `S' in the
11249 containing scope this declaration names a new type.
11250 This exception only applies if the elaborated-type-specifier
11251 forms the complete declaration:
11255 A declaration consisting solely of `class-key identifier ;' is
11256 either a redeclaration of the name in the current scope or a
11257 forward declaration of the identifier as a class name. It
11258 introduces the name into the current scope.
11260 We are in this situation precisely when the next token is a `;'.
11262 An exception to the exception is that a `friend' declaration does
11263 *not* name a new type; i.e., given:
11265 struct S { friend struct T; };
11267 `T' is not a new type in the scope of `S'.
11269 Also, `new struct S' or `sizeof (struct S)' never results in the
11270 definition of a new type; a new type can only be declared in a
11271 declaration context. */
11277 /* Friends have special name lookup rules. */
11278 ts = ts_within_enclosing_non_class;
11279 else if (is_declaration
11280 && cp_lexer_next_token_is (parser->lexer,
11282 /* This is a `class-key identifier ;' */
11288 (parser->num_template_parameter_lists
11289 && (cp_parser_next_token_starts_class_definition_p (parser)
11290 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11291 /* An unqualified name was used to reference this type, so
11292 there were no qualifying templates. */
11293 if (!cp_parser_check_template_parameters (parser,
11294 /*num_templates=*/0))
11295 return error_mark_node;
11296 type = xref_tag (tag_type, identifier, ts, template_p);
11300 if (type == error_mark_node)
11301 return error_mark_node;
11303 /* Allow attributes on forward declarations of classes. */
11306 if (TREE_CODE (type) == TYPENAME_TYPE)
11307 warning (OPT_Wattributes,
11308 "attributes ignored on uninstantiated type");
11309 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11310 && ! processing_explicit_instantiation)
11311 warning (OPT_Wattributes,
11312 "attributes ignored on template instantiation");
11313 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11314 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11316 warning (OPT_Wattributes,
11317 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11320 if (tag_type != enum_type)
11321 cp_parser_check_class_key (tag_type, type);
11323 /* A "<" cannot follow an elaborated type specifier. If that
11324 happens, the user was probably trying to form a template-id. */
11325 cp_parser_check_for_invalid_template_id (parser, type);
11330 /* Parse an enum-specifier.
11333 enum identifier [opt] { enumerator-list [opt] }
11336 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11339 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11340 if the token stream isn't an enum-specifier after all. */
11343 cp_parser_enum_specifier (cp_parser* parser)
11349 /* Parse tentatively so that we can back up if we don't find a
11351 cp_parser_parse_tentatively (parser);
11353 /* Caller guarantees that the current token is 'enum', an identifier
11354 possibly follows, and the token after that is an opening brace.
11355 If we don't have an identifier, fabricate an anonymous name for
11356 the enumeration being defined. */
11357 cp_lexer_consume_token (parser->lexer);
11359 attributes = cp_parser_attributes_opt (parser);
11361 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11362 identifier = cp_parser_identifier (parser);
11364 identifier = make_anon_name ();
11366 /* Look for the `{' but don't consume it yet. */
11367 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11368 cp_parser_simulate_error (parser);
11370 if (!cp_parser_parse_definitely (parser))
11373 /* Issue an error message if type-definitions are forbidden here. */
11374 if (!cp_parser_check_type_definition (parser))
11375 type = error_mark_node;
11377 /* Create the new type. We do this before consuming the opening
11378 brace so the enum will be recorded as being on the line of its
11379 tag (or the 'enum' keyword, if there is no tag). */
11380 type = start_enum (identifier);
11382 /* Consume the opening brace. */
11383 cp_lexer_consume_token (parser->lexer);
11385 if (type == error_mark_node)
11387 cp_parser_skip_to_end_of_block_or_statement (parser);
11388 return error_mark_node;
11391 /* If the next token is not '}', then there are some enumerators. */
11392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11393 cp_parser_enumerator_list (parser, type);
11395 /* Consume the final '}'. */
11396 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11398 /* Look for trailing attributes to apply to this enumeration, and
11399 apply them if appropriate. */
11400 if (cp_parser_allow_gnu_extensions_p (parser))
11402 tree trailing_attr = cp_parser_attributes_opt (parser);
11403 cplus_decl_attributes (&type,
11405 (int) ATTR_FLAG_TYPE_IN_PLACE);
11408 /* Finish up the enumeration. */
11409 finish_enum (type);
11414 /* Parse an enumerator-list. The enumerators all have the indicated
11418 enumerator-definition
11419 enumerator-list , enumerator-definition */
11422 cp_parser_enumerator_list (cp_parser* parser, tree type)
11426 /* Parse an enumerator-definition. */
11427 cp_parser_enumerator_definition (parser, type);
11429 /* If the next token is not a ',', we've reached the end of
11431 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11433 /* Otherwise, consume the `,' and keep going. */
11434 cp_lexer_consume_token (parser->lexer);
11435 /* If the next token is a `}', there is a trailing comma. */
11436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11438 if (pedantic && !in_system_header)
11439 pedwarn ("comma at end of enumerator list");
11445 /* Parse an enumerator-definition. The enumerator has the indicated
11448 enumerator-definition:
11450 enumerator = constant-expression
11456 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11461 /* Look for the identifier. */
11462 identifier = cp_parser_identifier (parser);
11463 if (identifier == error_mark_node)
11466 /* If the next token is an '=', then there is an explicit value. */
11467 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11469 /* Consume the `=' token. */
11470 cp_lexer_consume_token (parser->lexer);
11471 /* Parse the value. */
11472 value = cp_parser_constant_expression (parser,
11473 /*allow_non_constant_p=*/false,
11479 /* Create the enumerator. */
11480 build_enumerator (identifier, value, type);
11483 /* Parse a namespace-name.
11486 original-namespace-name
11489 Returns the NAMESPACE_DECL for the namespace. */
11492 cp_parser_namespace_name (cp_parser* parser)
11495 tree namespace_decl;
11497 /* Get the name of the namespace. */
11498 identifier = cp_parser_identifier (parser);
11499 if (identifier == error_mark_node)
11500 return error_mark_node;
11502 /* Look up the identifier in the currently active scope. Look only
11503 for namespaces, due to:
11505 [basic.lookup.udir]
11507 When looking up a namespace-name in a using-directive or alias
11508 definition, only namespace names are considered.
11512 [basic.lookup.qual]
11514 During the lookup of a name preceding the :: scope resolution
11515 operator, object, function, and enumerator names are ignored.
11517 (Note that cp_parser_class_or_namespace_name only calls this
11518 function if the token after the name is the scope resolution
11520 namespace_decl = cp_parser_lookup_name (parser, identifier,
11522 /*is_template=*/false,
11523 /*is_namespace=*/true,
11524 /*check_dependency=*/true,
11525 /*ambiguous_decls=*/NULL);
11526 /* If it's not a namespace, issue an error. */
11527 if (namespace_decl == error_mark_node
11528 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11530 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11531 error ("%qD is not a namespace-name", identifier);
11532 cp_parser_error (parser, "expected namespace-name");
11533 namespace_decl = error_mark_node;
11536 return namespace_decl;
11539 /* Parse a namespace-definition.
11541 namespace-definition:
11542 named-namespace-definition
11543 unnamed-namespace-definition
11545 named-namespace-definition:
11546 original-namespace-definition
11547 extension-namespace-definition
11549 original-namespace-definition:
11550 namespace identifier { namespace-body }
11552 extension-namespace-definition:
11553 namespace original-namespace-name { namespace-body }
11555 unnamed-namespace-definition:
11556 namespace { namespace-body } */
11559 cp_parser_namespace_definition (cp_parser* parser)
11561 tree identifier, attribs;
11562 bool has_visibility;
11564 /* Look for the `namespace' keyword. */
11565 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11567 /* Get the name of the namespace. We do not attempt to distinguish
11568 between an original-namespace-definition and an
11569 extension-namespace-definition at this point. The semantic
11570 analysis routines are responsible for that. */
11571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11572 identifier = cp_parser_identifier (parser);
11574 identifier = NULL_TREE;
11576 /* Parse any specified attributes. */
11577 attribs = cp_parser_attributes_opt (parser);
11579 /* Look for the `{' to start the namespace. */
11580 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11581 /* Start the namespace. */
11582 push_namespace (identifier);
11584 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11586 /* Parse the body of the namespace. */
11587 cp_parser_namespace_body (parser);
11589 #ifdef HANDLE_PRAGMA_VISIBILITY
11590 if (has_visibility)
11594 /* Finish the namespace. */
11596 /* Look for the final `}'. */
11597 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11600 /* Parse a namespace-body.
11603 declaration-seq [opt] */
11606 cp_parser_namespace_body (cp_parser* parser)
11608 cp_parser_declaration_seq_opt (parser);
11611 /* Parse a namespace-alias-definition.
11613 namespace-alias-definition:
11614 namespace identifier = qualified-namespace-specifier ; */
11617 cp_parser_namespace_alias_definition (cp_parser* parser)
11620 tree namespace_specifier;
11622 /* Look for the `namespace' keyword. */
11623 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11624 /* Look for the identifier. */
11625 identifier = cp_parser_identifier (parser);
11626 if (identifier == error_mark_node)
11628 /* Look for the `=' token. */
11629 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11630 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11632 error ("%<namespace%> definition is not allowed here");
11633 /* Skip the definition. */
11634 cp_lexer_consume_token (parser->lexer);
11635 if (cp_parser_skip_to_closing_brace (parser))
11636 cp_lexer_consume_token (parser->lexer);
11639 cp_parser_require (parser, CPP_EQ, "`='");
11640 /* Look for the qualified-namespace-specifier. */
11641 namespace_specifier
11642 = cp_parser_qualified_namespace_specifier (parser);
11643 /* Look for the `;' token. */
11644 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11646 /* Register the alias in the symbol table. */
11647 do_namespace_alias (identifier, namespace_specifier);
11650 /* Parse a qualified-namespace-specifier.
11652 qualified-namespace-specifier:
11653 :: [opt] nested-name-specifier [opt] namespace-name
11655 Returns a NAMESPACE_DECL corresponding to the specified
11659 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11661 /* Look for the optional `::'. */
11662 cp_parser_global_scope_opt (parser,
11663 /*current_scope_valid_p=*/false);
11665 /* Look for the optional nested-name-specifier. */
11666 cp_parser_nested_name_specifier_opt (parser,
11667 /*typename_keyword_p=*/false,
11668 /*check_dependency_p=*/true,
11670 /*is_declaration=*/true);
11672 return cp_parser_namespace_name (parser);
11675 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11676 access declaration.
11679 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11680 using :: unqualified-id ;
11682 access-declaration:
11688 cp_parser_using_declaration (cp_parser* parser,
11689 bool access_declaration_p)
11692 bool typename_p = false;
11693 bool global_scope_p;
11698 if (access_declaration_p)
11699 cp_parser_parse_tentatively (parser);
11702 /* Look for the `using' keyword. */
11703 cp_parser_require_keyword (parser, RID_USING, "`using'");
11705 /* Peek at the next token. */
11706 token = cp_lexer_peek_token (parser->lexer);
11707 /* See if it's `typename'. */
11708 if (token->keyword == RID_TYPENAME)
11710 /* Remember that we've seen it. */
11712 /* Consume the `typename' token. */
11713 cp_lexer_consume_token (parser->lexer);
11717 /* Look for the optional global scope qualification. */
11719 = (cp_parser_global_scope_opt (parser,
11720 /*current_scope_valid_p=*/false)
11723 /* If we saw `typename', or didn't see `::', then there must be a
11724 nested-name-specifier present. */
11725 if (typename_p || !global_scope_p)
11726 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11727 /*check_dependency_p=*/true,
11729 /*is_declaration=*/true);
11730 /* Otherwise, we could be in either of the two productions. In that
11731 case, treat the nested-name-specifier as optional. */
11733 qscope = cp_parser_nested_name_specifier_opt (parser,
11734 /*typename_keyword_p=*/false,
11735 /*check_dependency_p=*/true,
11737 /*is_declaration=*/true);
11739 qscope = global_namespace;
11741 if (access_declaration_p && cp_parser_error_occurred (parser))
11742 /* Something has already gone wrong; there's no need to parse
11743 further. Since an error has occurred, the return value of
11744 cp_parser_parse_definitely will be false, as required. */
11745 return cp_parser_parse_definitely (parser);
11747 /* Parse the unqualified-id. */
11748 identifier = cp_parser_unqualified_id (parser,
11749 /*template_keyword_p=*/false,
11750 /*check_dependency_p=*/true,
11751 /*declarator_p=*/true,
11752 /*optional_p=*/false);
11754 if (access_declaration_p)
11756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11757 cp_parser_simulate_error (parser);
11758 if (!cp_parser_parse_definitely (parser))
11762 /* The function we call to handle a using-declaration is different
11763 depending on what scope we are in. */
11764 if (qscope == error_mark_node || identifier == error_mark_node)
11766 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11767 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11768 /* [namespace.udecl]
11770 A using declaration shall not name a template-id. */
11771 error ("a template-id may not appear in a using-declaration");
11774 if (at_class_scope_p ())
11776 /* Create the USING_DECL. */
11777 decl = do_class_using_decl (parser->scope, identifier);
11779 if (check_for_bare_parameter_packs (decl))
11782 /* Add it to the list of members in this class. */
11783 finish_member_declaration (decl);
11787 decl = cp_parser_lookup_name_simple (parser, identifier);
11788 if (decl == error_mark_node)
11789 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11790 else if (check_for_bare_parameter_packs (decl))
11792 else if (!at_namespace_scope_p ())
11793 do_local_using_decl (decl, qscope, identifier);
11795 do_toplevel_using_decl (decl, qscope, identifier);
11799 /* Look for the final `;'. */
11800 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11805 /* Parse a using-directive.
11808 using namespace :: [opt] nested-name-specifier [opt]
11809 namespace-name ; */
11812 cp_parser_using_directive (cp_parser* parser)
11814 tree namespace_decl;
11817 /* Look for the `using' keyword. */
11818 cp_parser_require_keyword (parser, RID_USING, "`using'");
11819 /* And the `namespace' keyword. */
11820 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11821 /* Look for the optional `::' operator. */
11822 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11823 /* And the optional nested-name-specifier. */
11824 cp_parser_nested_name_specifier_opt (parser,
11825 /*typename_keyword_p=*/false,
11826 /*check_dependency_p=*/true,
11828 /*is_declaration=*/true);
11829 /* Get the namespace being used. */
11830 namespace_decl = cp_parser_namespace_name (parser);
11831 /* And any specified attributes. */
11832 attribs = cp_parser_attributes_opt (parser);
11833 /* Update the symbol table. */
11834 parse_using_directive (namespace_decl, attribs);
11835 /* Look for the final `;'. */
11836 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11839 /* Parse an asm-definition.
11842 asm ( string-literal ) ;
11847 asm volatile [opt] ( string-literal ) ;
11848 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11849 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11850 : asm-operand-list [opt] ) ;
11851 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11852 : asm-operand-list [opt]
11853 : asm-operand-list [opt] ) ; */
11856 cp_parser_asm_definition (cp_parser* parser)
11859 tree outputs = NULL_TREE;
11860 tree inputs = NULL_TREE;
11861 tree clobbers = NULL_TREE;
11863 bool volatile_p = false;
11864 bool extended_p = false;
11865 bool invalid_inputs_p = false;
11866 bool invalid_outputs_p = false;
11868 /* Look for the `asm' keyword. */
11869 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11870 /* See if the next token is `volatile'. */
11871 if (cp_parser_allow_gnu_extensions_p (parser)
11872 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11874 /* Remember that we saw the `volatile' keyword. */
11876 /* Consume the token. */
11877 cp_lexer_consume_token (parser->lexer);
11879 /* Look for the opening `('. */
11880 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11882 /* Look for the string. */
11883 string = cp_parser_string_literal (parser, false, false);
11884 if (string == error_mark_node)
11886 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11887 /*consume_paren=*/true);
11891 /* If we're allowing GNU extensions, check for the extended assembly
11892 syntax. Unfortunately, the `:' tokens need not be separated by
11893 a space in C, and so, for compatibility, we tolerate that here
11894 too. Doing that means that we have to treat the `::' operator as
11896 if (cp_parser_allow_gnu_extensions_p (parser)
11897 && parser->in_function_body
11898 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11899 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11901 bool inputs_p = false;
11902 bool clobbers_p = false;
11904 /* The extended syntax was used. */
11907 /* Look for outputs. */
11908 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11910 /* Consume the `:'. */
11911 cp_lexer_consume_token (parser->lexer);
11912 /* Parse the output-operands. */
11913 if (cp_lexer_next_token_is_not (parser->lexer,
11915 && cp_lexer_next_token_is_not (parser->lexer,
11917 && cp_lexer_next_token_is_not (parser->lexer,
11919 outputs = cp_parser_asm_operand_list (parser);
11921 if (outputs == error_mark_node)
11922 invalid_outputs_p = true;
11924 /* If the next token is `::', there are no outputs, and the
11925 next token is the beginning of the inputs. */
11926 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11927 /* The inputs are coming next. */
11930 /* Look for inputs. */
11932 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11934 /* Consume the `:' or `::'. */
11935 cp_lexer_consume_token (parser->lexer);
11936 /* Parse the output-operands. */
11937 if (cp_lexer_next_token_is_not (parser->lexer,
11939 && cp_lexer_next_token_is_not (parser->lexer,
11941 inputs = cp_parser_asm_operand_list (parser);
11943 if (inputs == error_mark_node)
11944 invalid_inputs_p = true;
11946 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11947 /* The clobbers are coming next. */
11950 /* Look for clobbers. */
11952 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11954 /* Consume the `:' or `::'. */
11955 cp_lexer_consume_token (parser->lexer);
11956 /* Parse the clobbers. */
11957 if (cp_lexer_next_token_is_not (parser->lexer,
11959 clobbers = cp_parser_asm_clobber_list (parser);
11962 /* Look for the closing `)'. */
11963 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11964 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11965 /*consume_paren=*/true);
11966 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11968 if (!invalid_inputs_p && !invalid_outputs_p)
11970 /* Create the ASM_EXPR. */
11971 if (parser->in_function_body)
11973 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11975 /* If the extended syntax was not used, mark the ASM_EXPR. */
11978 tree temp = asm_stmt;
11979 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11980 temp = TREE_OPERAND (temp, 0);
11982 ASM_INPUT_P (temp) = 1;
11986 cgraph_add_asm_node (string);
11990 /* Declarators [gram.dcl.decl] */
11992 /* Parse an init-declarator.
11995 declarator initializer [opt]
12000 declarator asm-specification [opt] attributes [opt] initializer [opt]
12002 function-definition:
12003 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12005 decl-specifier-seq [opt] declarator function-try-block
12009 function-definition:
12010 __extension__ function-definition
12012 The DECL_SPECIFIERS apply to this declarator. Returns a
12013 representation of the entity declared. If MEMBER_P is TRUE, then
12014 this declarator appears in a class scope. The new DECL created by
12015 this declarator is returned.
12017 The CHECKS are access checks that should be performed once we know
12018 what entity is being declared (and, therefore, what classes have
12021 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12022 for a function-definition here as well. If the declarator is a
12023 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12024 be TRUE upon return. By that point, the function-definition will
12025 have been completely parsed.
12027 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12031 cp_parser_init_declarator (cp_parser* parser,
12032 cp_decl_specifier_seq *decl_specifiers,
12033 VEC (deferred_access_check,gc)* checks,
12034 bool function_definition_allowed_p,
12036 int declares_class_or_enum,
12037 bool* function_definition_p)
12040 cp_declarator *declarator;
12041 tree prefix_attributes;
12043 tree asm_specification;
12045 tree decl = NULL_TREE;
12047 bool is_initialized;
12048 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12049 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12051 enum cpp_ttype initialization_kind;
12052 bool is_parenthesized_init = false;
12053 bool is_non_constant_init;
12054 int ctor_dtor_or_conv_p;
12056 tree pushed_scope = NULL;
12058 /* Gather the attributes that were provided with the
12059 decl-specifiers. */
12060 prefix_attributes = decl_specifiers->attributes;
12062 /* Assume that this is not the declarator for a function
12064 if (function_definition_p)
12065 *function_definition_p = false;
12067 /* Defer access checks while parsing the declarator; we cannot know
12068 what names are accessible until we know what is being
12070 resume_deferring_access_checks ();
12072 /* Parse the declarator. */
12074 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12075 &ctor_dtor_or_conv_p,
12076 /*parenthesized_p=*/NULL,
12077 /*member_p=*/false);
12078 /* Gather up the deferred checks. */
12079 stop_deferring_access_checks ();
12081 /* If the DECLARATOR was erroneous, there's no need to go
12083 if (declarator == cp_error_declarator)
12084 return error_mark_node;
12086 /* Check that the number of template-parameter-lists is OK. */
12087 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12088 return error_mark_node;
12090 if (declares_class_or_enum & 2)
12091 cp_parser_check_for_definition_in_return_type (declarator,
12092 decl_specifiers->type);
12094 /* Figure out what scope the entity declared by the DECLARATOR is
12095 located in. `grokdeclarator' sometimes changes the scope, so
12096 we compute it now. */
12097 scope = get_scope_of_declarator (declarator);
12099 /* If we're allowing GNU extensions, look for an asm-specification
12101 if (cp_parser_allow_gnu_extensions_p (parser))
12103 /* Look for an asm-specification. */
12104 asm_specification = cp_parser_asm_specification_opt (parser);
12105 /* And attributes. */
12106 attributes = cp_parser_attributes_opt (parser);
12110 asm_specification = NULL_TREE;
12111 attributes = NULL_TREE;
12114 /* Peek at the next token. */
12115 token = cp_lexer_peek_token (parser->lexer);
12116 /* Check to see if the token indicates the start of a
12117 function-definition. */
12118 if (cp_parser_token_starts_function_definition_p (token))
12120 if (!function_definition_allowed_p)
12122 /* If a function-definition should not appear here, issue an
12124 cp_parser_error (parser,
12125 "a function-definition is not allowed here");
12126 return error_mark_node;
12130 /* Neither attributes nor an asm-specification are allowed
12131 on a function-definition. */
12132 if (asm_specification)
12133 error ("an asm-specification is not allowed on a function-definition");
12135 error ("attributes are not allowed on a function-definition");
12136 /* This is a function-definition. */
12137 *function_definition_p = true;
12139 /* Parse the function definition. */
12141 decl = cp_parser_save_member_function_body (parser,
12144 prefix_attributes);
12147 = (cp_parser_function_definition_from_specifiers_and_declarator
12148 (parser, decl_specifiers, prefix_attributes, declarator));
12156 Only in function declarations for constructors, destructors, and
12157 type conversions can the decl-specifier-seq be omitted.
12159 We explicitly postpone this check past the point where we handle
12160 function-definitions because we tolerate function-definitions
12161 that are missing their return types in some modes. */
12162 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12164 cp_parser_error (parser,
12165 "expected constructor, destructor, or type conversion");
12166 return error_mark_node;
12169 /* An `=' or an `(' indicates an initializer. */
12170 if (token->type == CPP_EQ
12171 || token->type == CPP_OPEN_PAREN)
12173 is_initialized = true;
12174 initialization_kind = token->type;
12178 /* If the init-declarator isn't initialized and isn't followed by a
12179 `,' or `;', it's not a valid init-declarator. */
12180 if (token->type != CPP_COMMA
12181 && token->type != CPP_SEMICOLON)
12183 cp_parser_error (parser, "expected initializer");
12184 return error_mark_node;
12186 is_initialized = false;
12187 initialization_kind = CPP_EOF;
12190 /* Because start_decl has side-effects, we should only call it if we
12191 know we're going ahead. By this point, we know that we cannot
12192 possibly be looking at any other construct. */
12193 cp_parser_commit_to_tentative_parse (parser);
12195 /* If the decl specifiers were bad, issue an error now that we're
12196 sure this was intended to be a declarator. Then continue
12197 declaring the variable(s), as int, to try to cut down on further
12199 if (decl_specifiers->any_specifiers_p
12200 && decl_specifiers->type == error_mark_node)
12202 cp_parser_error (parser, "invalid type in declaration");
12203 decl_specifiers->type = integer_type_node;
12206 /* Check to see whether or not this declaration is a friend. */
12207 friend_p = cp_parser_friend_p (decl_specifiers);
12209 /* Enter the newly declared entry in the symbol table. If we're
12210 processing a declaration in a class-specifier, we wait until
12211 after processing the initializer. */
12214 if (parser->in_unbraced_linkage_specification_p)
12215 decl_specifiers->storage_class = sc_extern;
12216 decl = start_decl (declarator, decl_specifiers,
12217 is_initialized, attributes, prefix_attributes,
12221 /* Enter the SCOPE. That way unqualified names appearing in the
12222 initializer will be looked up in SCOPE. */
12223 pushed_scope = push_scope (scope);
12225 /* Perform deferred access control checks, now that we know in which
12226 SCOPE the declared entity resides. */
12227 if (!member_p && decl)
12229 tree saved_current_function_decl = NULL_TREE;
12231 /* If the entity being declared is a function, pretend that we
12232 are in its scope. If it is a `friend', it may have access to
12233 things that would not otherwise be accessible. */
12234 if (TREE_CODE (decl) == FUNCTION_DECL)
12236 saved_current_function_decl = current_function_decl;
12237 current_function_decl = decl;
12240 /* Perform access checks for template parameters. */
12241 cp_parser_perform_template_parameter_access_checks (checks);
12243 /* Perform the access control checks for the declarator and the
12244 the decl-specifiers. */
12245 perform_deferred_access_checks ();
12247 /* Restore the saved value. */
12248 if (TREE_CODE (decl) == FUNCTION_DECL)
12249 current_function_decl = saved_current_function_decl;
12252 /* Parse the initializer. */
12253 initializer = NULL_TREE;
12254 is_parenthesized_init = false;
12255 is_non_constant_init = true;
12256 if (is_initialized)
12258 if (function_declarator_p (declarator))
12260 if (initialization_kind == CPP_EQ)
12261 initializer = cp_parser_pure_specifier (parser);
12264 /* If the declaration was erroneous, we don't really
12265 know what the user intended, so just silently
12266 consume the initializer. */
12267 if (decl != error_mark_node)
12268 error ("initializer provided for function");
12269 cp_parser_skip_to_closing_parenthesis (parser,
12270 /*recovering=*/true,
12271 /*or_comma=*/false,
12272 /*consume_paren=*/true);
12276 initializer = cp_parser_initializer (parser,
12277 &is_parenthesized_init,
12278 &is_non_constant_init);
12281 /* The old parser allows attributes to appear after a parenthesized
12282 initializer. Mark Mitchell proposed removing this functionality
12283 on the GCC mailing lists on 2002-08-13. This parser accepts the
12284 attributes -- but ignores them. */
12285 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12286 if (cp_parser_attributes_opt (parser))
12287 warning (OPT_Wattributes,
12288 "attributes after parenthesized initializer ignored");
12290 /* For an in-class declaration, use `grokfield' to create the
12296 pop_scope (pushed_scope);
12297 pushed_scope = false;
12299 decl = grokfield (declarator, decl_specifiers,
12300 initializer, !is_non_constant_init,
12301 /*asmspec=*/NULL_TREE,
12302 prefix_attributes);
12303 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12304 cp_parser_save_default_args (parser, decl);
12307 /* Finish processing the declaration. But, skip friend
12309 if (!friend_p && decl && decl != error_mark_node)
12311 cp_finish_decl (decl,
12312 initializer, !is_non_constant_init,
12314 /* If the initializer is in parentheses, then this is
12315 a direct-initialization, which means that an
12316 `explicit' constructor is OK. Otherwise, an
12317 `explicit' constructor cannot be used. */
12318 ((is_parenthesized_init || !is_initialized)
12319 ? 0 : LOOKUP_ONLYCONVERTING));
12321 else if ((cxx_dialect != cxx98) && friend_p
12322 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12323 /* Core issue #226 (C++0x only): A default template-argument
12324 shall not be specified in a friend class template
12326 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12327 /*is_partial=*/0, /*is_friend_decl=*/1);
12329 if (!friend_p && pushed_scope)
12330 pop_scope (pushed_scope);
12335 /* Parse a declarator.
12339 ptr-operator declarator
12341 abstract-declarator:
12342 ptr-operator abstract-declarator [opt]
12343 direct-abstract-declarator
12348 attributes [opt] direct-declarator
12349 attributes [opt] ptr-operator declarator
12351 abstract-declarator:
12352 attributes [opt] ptr-operator abstract-declarator [opt]
12353 attributes [opt] direct-abstract-declarator
12355 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12356 detect constructor, destructor or conversion operators. It is set
12357 to -1 if the declarator is a name, and +1 if it is a
12358 function. Otherwise it is set to zero. Usually you just want to
12359 test for >0, but internally the negative value is used.
12361 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12362 a decl-specifier-seq unless it declares a constructor, destructor,
12363 or conversion. It might seem that we could check this condition in
12364 semantic analysis, rather than parsing, but that makes it difficult
12365 to handle something like `f()'. We want to notice that there are
12366 no decl-specifiers, and therefore realize that this is an
12367 expression, not a declaration.)
12369 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12370 the declarator is a direct-declarator of the form "(...)".
12372 MEMBER_P is true iff this declarator is a member-declarator. */
12374 static cp_declarator *
12375 cp_parser_declarator (cp_parser* parser,
12376 cp_parser_declarator_kind dcl_kind,
12377 int* ctor_dtor_or_conv_p,
12378 bool* parenthesized_p,
12382 cp_declarator *declarator;
12383 enum tree_code code;
12384 cp_cv_quals cv_quals;
12386 tree attributes = NULL_TREE;
12388 /* Assume this is not a constructor, destructor, or type-conversion
12390 if (ctor_dtor_or_conv_p)
12391 *ctor_dtor_or_conv_p = 0;
12393 if (cp_parser_allow_gnu_extensions_p (parser))
12394 attributes = cp_parser_attributes_opt (parser);
12396 /* Peek at the next token. */
12397 token = cp_lexer_peek_token (parser->lexer);
12399 /* Check for the ptr-operator production. */
12400 cp_parser_parse_tentatively (parser);
12401 /* Parse the ptr-operator. */
12402 code = cp_parser_ptr_operator (parser,
12405 /* If that worked, then we have a ptr-operator. */
12406 if (cp_parser_parse_definitely (parser))
12408 /* If a ptr-operator was found, then this declarator was not
12410 if (parenthesized_p)
12411 *parenthesized_p = true;
12412 /* The dependent declarator is optional if we are parsing an
12413 abstract-declarator. */
12414 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12415 cp_parser_parse_tentatively (parser);
12417 /* Parse the dependent declarator. */
12418 declarator = cp_parser_declarator (parser, dcl_kind,
12419 /*ctor_dtor_or_conv_p=*/NULL,
12420 /*parenthesized_p=*/NULL,
12421 /*member_p=*/false);
12423 /* If we are parsing an abstract-declarator, we must handle the
12424 case where the dependent declarator is absent. */
12425 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12426 && !cp_parser_parse_definitely (parser))
12429 declarator = cp_parser_make_indirect_declarator
12430 (code, class_type, cv_quals, declarator);
12432 /* Everything else is a direct-declarator. */
12435 if (parenthesized_p)
12436 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12438 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12439 ctor_dtor_or_conv_p,
12443 if (attributes && declarator && declarator != cp_error_declarator)
12444 declarator->attributes = attributes;
12449 /* Parse a direct-declarator or direct-abstract-declarator.
12453 direct-declarator ( parameter-declaration-clause )
12454 cv-qualifier-seq [opt]
12455 exception-specification [opt]
12456 direct-declarator [ constant-expression [opt] ]
12459 direct-abstract-declarator:
12460 direct-abstract-declarator [opt]
12461 ( parameter-declaration-clause )
12462 cv-qualifier-seq [opt]
12463 exception-specification [opt]
12464 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12465 ( abstract-declarator )
12467 Returns a representation of the declarator. DCL_KIND is
12468 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12469 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12470 we are parsing a direct-declarator. It is
12471 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12472 of ambiguity we prefer an abstract declarator, as per
12473 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12474 cp_parser_declarator. */
12476 static cp_declarator *
12477 cp_parser_direct_declarator (cp_parser* parser,
12478 cp_parser_declarator_kind dcl_kind,
12479 int* ctor_dtor_or_conv_p,
12483 cp_declarator *declarator = NULL;
12484 tree scope = NULL_TREE;
12485 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12486 bool saved_in_declarator_p = parser->in_declarator_p;
12488 tree pushed_scope = NULL_TREE;
12492 /* Peek at the next token. */
12493 token = cp_lexer_peek_token (parser->lexer);
12494 if (token->type == CPP_OPEN_PAREN)
12496 /* This is either a parameter-declaration-clause, or a
12497 parenthesized declarator. When we know we are parsing a
12498 named declarator, it must be a parenthesized declarator
12499 if FIRST is true. For instance, `(int)' is a
12500 parameter-declaration-clause, with an omitted
12501 direct-abstract-declarator. But `((*))', is a
12502 parenthesized abstract declarator. Finally, when T is a
12503 template parameter `(T)' is a
12504 parameter-declaration-clause, and not a parenthesized
12507 We first try and parse a parameter-declaration-clause,
12508 and then try a nested declarator (if FIRST is true).
12510 It is not an error for it not to be a
12511 parameter-declaration-clause, even when FIRST is
12517 The first is the declaration of a function while the
12518 second is a the definition of a variable, including its
12521 Having seen only the parenthesis, we cannot know which of
12522 these two alternatives should be selected. Even more
12523 complex are examples like:
12528 The former is a function-declaration; the latter is a
12529 variable initialization.
12531 Thus again, we try a parameter-declaration-clause, and if
12532 that fails, we back out and return. */
12534 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12536 cp_parameter_declarator *params;
12537 unsigned saved_num_template_parameter_lists;
12539 /* In a member-declarator, the only valid interpretation
12540 of a parenthesis is the start of a
12541 parameter-declaration-clause. (It is invalid to
12542 initialize a static data member with a parenthesized
12543 initializer; only the "=" form of initialization is
12546 cp_parser_parse_tentatively (parser);
12548 /* Consume the `('. */
12549 cp_lexer_consume_token (parser->lexer);
12552 /* If this is going to be an abstract declarator, we're
12553 in a declarator and we can't have default args. */
12554 parser->default_arg_ok_p = false;
12555 parser->in_declarator_p = true;
12558 /* Inside the function parameter list, surrounding
12559 template-parameter-lists do not apply. */
12560 saved_num_template_parameter_lists
12561 = parser->num_template_parameter_lists;
12562 parser->num_template_parameter_lists = 0;
12564 /* Parse the parameter-declaration-clause. */
12565 params = cp_parser_parameter_declaration_clause (parser);
12567 parser->num_template_parameter_lists
12568 = saved_num_template_parameter_lists;
12570 /* If all went well, parse the cv-qualifier-seq and the
12571 exception-specification. */
12572 if (member_p || cp_parser_parse_definitely (parser))
12574 cp_cv_quals cv_quals;
12575 tree exception_specification;
12577 if (ctor_dtor_or_conv_p)
12578 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12580 /* Consume the `)'. */
12581 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12583 /* Parse the cv-qualifier-seq. */
12584 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12585 /* And the exception-specification. */
12586 exception_specification
12587 = cp_parser_exception_specification_opt (parser);
12589 /* Create the function-declarator. */
12590 declarator = make_call_declarator (declarator,
12593 exception_specification);
12594 /* Any subsequent parameter lists are to do with
12595 return type, so are not those of the declared
12597 parser->default_arg_ok_p = false;
12599 /* Repeat the main loop. */
12604 /* If this is the first, we can try a parenthesized
12608 bool saved_in_type_id_in_expr_p;
12610 parser->default_arg_ok_p = saved_default_arg_ok_p;
12611 parser->in_declarator_p = saved_in_declarator_p;
12613 /* Consume the `('. */
12614 cp_lexer_consume_token (parser->lexer);
12615 /* Parse the nested declarator. */
12616 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12617 parser->in_type_id_in_expr_p = true;
12619 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12620 /*parenthesized_p=*/NULL,
12622 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12624 /* Expect a `)'. */
12625 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12626 declarator = cp_error_declarator;
12627 if (declarator == cp_error_declarator)
12630 goto handle_declarator;
12632 /* Otherwise, we must be done. */
12636 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12637 && token->type == CPP_OPEN_SQUARE)
12639 /* Parse an array-declarator. */
12642 if (ctor_dtor_or_conv_p)
12643 *ctor_dtor_or_conv_p = 0;
12646 parser->default_arg_ok_p = false;
12647 parser->in_declarator_p = true;
12648 /* Consume the `['. */
12649 cp_lexer_consume_token (parser->lexer);
12650 /* Peek at the next token. */
12651 token = cp_lexer_peek_token (parser->lexer);
12652 /* If the next token is `]', then there is no
12653 constant-expression. */
12654 if (token->type != CPP_CLOSE_SQUARE)
12656 bool non_constant_p;
12659 = cp_parser_constant_expression (parser,
12660 /*allow_non_constant=*/true,
12662 if (!non_constant_p)
12663 bounds = fold_non_dependent_expr (bounds);
12664 /* Normally, the array bound must be an integral constant
12665 expression. However, as an extension, we allow VLAs
12666 in function scopes. */
12667 else if (!parser->in_function_body)
12669 error ("array bound is not an integer constant");
12670 bounds = error_mark_node;
12674 bounds = NULL_TREE;
12675 /* Look for the closing `]'. */
12676 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12678 declarator = cp_error_declarator;
12682 declarator = make_array_declarator (declarator, bounds);
12684 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12686 tree qualifying_scope;
12687 tree unqualified_name;
12688 special_function_kind sfk;
12690 bool pack_expansion_p = false;
12692 /* Parse a declarator-id */
12693 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12696 cp_parser_parse_tentatively (parser);
12698 /* If we see an ellipsis, we should be looking at a
12700 if (token->type == CPP_ELLIPSIS)
12702 /* Consume the `...' */
12703 cp_lexer_consume_token (parser->lexer);
12705 pack_expansion_p = true;
12710 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12711 qualifying_scope = parser->scope;
12716 if (!unqualified_name && pack_expansion_p)
12718 /* Check whether an error occurred. */
12719 okay = !cp_parser_error_occurred (parser);
12721 /* We already consumed the ellipsis to mark a
12722 parameter pack, but we have no way to report it,
12723 so abort the tentative parse. We will be exiting
12724 immediately anyway. */
12725 cp_parser_abort_tentative_parse (parser);
12728 okay = cp_parser_parse_definitely (parser);
12731 unqualified_name = error_mark_node;
12732 else if (unqualified_name
12733 && (qualifying_scope
12734 || (TREE_CODE (unqualified_name)
12735 != IDENTIFIER_NODE)))
12737 cp_parser_error (parser, "expected unqualified-id");
12738 unqualified_name = error_mark_node;
12742 if (!unqualified_name)
12744 if (unqualified_name == error_mark_node)
12746 declarator = cp_error_declarator;
12747 pack_expansion_p = false;
12748 declarator->parameter_pack_p = false;
12752 if (qualifying_scope && at_namespace_scope_p ()
12753 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12755 /* In the declaration of a member of a template class
12756 outside of the class itself, the SCOPE will sometimes
12757 be a TYPENAME_TYPE. For example, given:
12759 template <typename T>
12760 int S<T>::R::i = 3;
12762 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12763 this context, we must resolve S<T>::R to an ordinary
12764 type, rather than a typename type.
12766 The reason we normally avoid resolving TYPENAME_TYPEs
12767 is that a specialization of `S' might render
12768 `S<T>::R' not a type. However, if `S' is
12769 specialized, then this `i' will not be used, so there
12770 is no harm in resolving the types here. */
12773 /* Resolve the TYPENAME_TYPE. */
12774 type = resolve_typename_type (qualifying_scope,
12775 /*only_current_p=*/false);
12776 /* If that failed, the declarator is invalid. */
12777 if (TREE_CODE (type) == TYPENAME_TYPE)
12778 error ("%<%T::%E%> is not a type",
12779 TYPE_CONTEXT (qualifying_scope),
12780 TYPE_IDENTIFIER (qualifying_scope));
12781 qualifying_scope = type;
12786 if (unqualified_name)
12790 if (qualifying_scope
12791 && CLASS_TYPE_P (qualifying_scope))
12792 class_type = qualifying_scope;
12794 class_type = current_class_type;
12796 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12798 tree name_type = TREE_TYPE (unqualified_name);
12799 if (class_type && same_type_p (name_type, class_type))
12801 if (qualifying_scope
12802 && CLASSTYPE_USE_TEMPLATE (name_type))
12804 error ("invalid use of constructor as a template");
12805 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12806 "name the constructor in a qualified name",
12808 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12809 class_type, name_type);
12810 declarator = cp_error_declarator;
12814 unqualified_name = constructor_name (class_type);
12818 /* We do not attempt to print the declarator
12819 here because we do not have enough
12820 information about its original syntactic
12822 cp_parser_error (parser, "invalid declarator");
12823 declarator = cp_error_declarator;
12830 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12831 sfk = sfk_destructor;
12832 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12833 sfk = sfk_conversion;
12834 else if (/* There's no way to declare a constructor
12835 for an anonymous type, even if the type
12836 got a name for linkage purposes. */
12837 !TYPE_WAS_ANONYMOUS (class_type)
12838 && constructor_name_p (unqualified_name,
12841 unqualified_name = constructor_name (class_type);
12842 sfk = sfk_constructor;
12845 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12846 *ctor_dtor_or_conv_p = -1;
12849 declarator = make_id_declarator (qualifying_scope,
12852 declarator->id_loc = token->location;
12853 declarator->parameter_pack_p = pack_expansion_p;
12855 if (pack_expansion_p)
12856 maybe_warn_variadic_templates ();
12858 handle_declarator:;
12859 scope = get_scope_of_declarator (declarator);
12861 /* Any names that appear after the declarator-id for a
12862 member are looked up in the containing scope. */
12863 pushed_scope = push_scope (scope);
12864 parser->in_declarator_p = true;
12865 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12866 || (declarator && declarator->kind == cdk_id))
12867 /* Default args are only allowed on function
12869 parser->default_arg_ok_p = saved_default_arg_ok_p;
12871 parser->default_arg_ok_p = false;
12880 /* For an abstract declarator, we might wind up with nothing at this
12881 point. That's an error; the declarator is not optional. */
12883 cp_parser_error (parser, "expected declarator");
12885 /* If we entered a scope, we must exit it now. */
12887 pop_scope (pushed_scope);
12889 parser->default_arg_ok_p = saved_default_arg_ok_p;
12890 parser->in_declarator_p = saved_in_declarator_p;
12895 /* Parse a ptr-operator.
12898 * cv-qualifier-seq [opt]
12900 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12905 & cv-qualifier-seq [opt]
12907 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12908 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12909 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12910 filled in with the TYPE containing the member. *CV_QUALS is
12911 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12912 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12913 Note that the tree codes returned by this function have nothing
12914 to do with the types of trees that will be eventually be created
12915 to represent the pointer or reference type being parsed. They are
12916 just constants with suggestive names. */
12917 static enum tree_code
12918 cp_parser_ptr_operator (cp_parser* parser,
12920 cp_cv_quals *cv_quals)
12922 enum tree_code code = ERROR_MARK;
12925 /* Assume that it's not a pointer-to-member. */
12927 /* And that there are no cv-qualifiers. */
12928 *cv_quals = TYPE_UNQUALIFIED;
12930 /* Peek at the next token. */
12931 token = cp_lexer_peek_token (parser->lexer);
12933 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12934 if (token->type == CPP_MULT)
12935 code = INDIRECT_REF;
12936 else if (token->type == CPP_AND)
12938 else if ((cxx_dialect != cxx98) &&
12939 token->type == CPP_AND_AND) /* C++0x only */
12940 code = NON_LVALUE_EXPR;
12942 if (code != ERROR_MARK)
12944 /* Consume the `*', `&' or `&&'. */
12945 cp_lexer_consume_token (parser->lexer);
12947 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12948 `&', if we are allowing GNU extensions. (The only qualifier
12949 that can legally appear after `&' is `restrict', but that is
12950 enforced during semantic analysis. */
12951 if (code == INDIRECT_REF
12952 || cp_parser_allow_gnu_extensions_p (parser))
12953 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12957 /* Try the pointer-to-member case. */
12958 cp_parser_parse_tentatively (parser);
12959 /* Look for the optional `::' operator. */
12960 cp_parser_global_scope_opt (parser,
12961 /*current_scope_valid_p=*/false);
12962 /* Look for the nested-name specifier. */
12963 cp_parser_nested_name_specifier (parser,
12964 /*typename_keyword_p=*/false,
12965 /*check_dependency_p=*/true,
12967 /*is_declaration=*/false);
12968 /* If we found it, and the next token is a `*', then we are
12969 indeed looking at a pointer-to-member operator. */
12970 if (!cp_parser_error_occurred (parser)
12971 && cp_parser_require (parser, CPP_MULT, "`*'"))
12973 /* Indicate that the `*' operator was used. */
12974 code = INDIRECT_REF;
12976 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12977 error ("%qD is a namespace", parser->scope);
12980 /* The type of which the member is a member is given by the
12982 *type = parser->scope;
12983 /* The next name will not be qualified. */
12984 parser->scope = NULL_TREE;
12985 parser->qualifying_scope = NULL_TREE;
12986 parser->object_scope = NULL_TREE;
12987 /* Look for the optional cv-qualifier-seq. */
12988 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12991 /* If that didn't work we don't have a ptr-operator. */
12992 if (!cp_parser_parse_definitely (parser))
12993 cp_parser_error (parser, "expected ptr-operator");
12999 /* Parse an (optional) cv-qualifier-seq.
13002 cv-qualifier cv-qualifier-seq [opt]
13013 Returns a bitmask representing the cv-qualifiers. */
13016 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13018 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13023 cp_cv_quals cv_qualifier;
13025 /* Peek at the next token. */
13026 token = cp_lexer_peek_token (parser->lexer);
13027 /* See if it's a cv-qualifier. */
13028 switch (token->keyword)
13031 cv_qualifier = TYPE_QUAL_CONST;
13035 cv_qualifier = TYPE_QUAL_VOLATILE;
13039 cv_qualifier = TYPE_QUAL_RESTRICT;
13043 cv_qualifier = TYPE_UNQUALIFIED;
13050 if (cv_quals & cv_qualifier)
13052 error ("duplicate cv-qualifier");
13053 cp_lexer_purge_token (parser->lexer);
13057 cp_lexer_consume_token (parser->lexer);
13058 cv_quals |= cv_qualifier;
13065 /* Parse a declarator-id.
13069 :: [opt] nested-name-specifier [opt] type-name
13071 In the `id-expression' case, the value returned is as for
13072 cp_parser_id_expression if the id-expression was an unqualified-id.
13073 If the id-expression was a qualified-id, then a SCOPE_REF is
13074 returned. The first operand is the scope (either a NAMESPACE_DECL
13075 or TREE_TYPE), but the second is still just a representation of an
13079 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13082 /* The expression must be an id-expression. Assume that qualified
13083 names are the names of types so that:
13086 int S<T>::R::i = 3;
13088 will work; we must treat `S<T>::R' as the name of a type.
13089 Similarly, assume that qualified names are templates, where
13093 int S<T>::R<T>::i = 3;
13096 id = cp_parser_id_expression (parser,
13097 /*template_keyword_p=*/false,
13098 /*check_dependency_p=*/false,
13099 /*template_p=*/NULL,
13100 /*declarator_p=*/true,
13102 if (id && BASELINK_P (id))
13103 id = BASELINK_FUNCTIONS (id);
13107 /* Parse a type-id.
13110 type-specifier-seq abstract-declarator [opt]
13112 Returns the TYPE specified. */
13115 cp_parser_type_id (cp_parser* parser)
13117 cp_decl_specifier_seq type_specifier_seq;
13118 cp_declarator *abstract_declarator;
13120 /* Parse the type-specifier-seq. */
13121 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13122 &type_specifier_seq);
13123 if (type_specifier_seq.type == error_mark_node)
13124 return error_mark_node;
13126 /* There might or might not be an abstract declarator. */
13127 cp_parser_parse_tentatively (parser);
13128 /* Look for the declarator. */
13129 abstract_declarator
13130 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13131 /*parenthesized_p=*/NULL,
13132 /*member_p=*/false);
13133 /* Check to see if there really was a declarator. */
13134 if (!cp_parser_parse_definitely (parser))
13135 abstract_declarator = NULL;
13137 return groktypename (&type_specifier_seq, abstract_declarator);
13140 /* Parse a type-specifier-seq.
13142 type-specifier-seq:
13143 type-specifier type-specifier-seq [opt]
13147 type-specifier-seq:
13148 attributes type-specifier-seq [opt]
13150 If IS_CONDITION is true, we are at the start of a "condition",
13151 e.g., we've just seen "if (".
13153 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13156 cp_parser_type_specifier_seq (cp_parser* parser,
13158 cp_decl_specifier_seq *type_specifier_seq)
13160 bool seen_type_specifier = false;
13161 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13163 /* Clear the TYPE_SPECIFIER_SEQ. */
13164 clear_decl_specs (type_specifier_seq);
13166 /* Parse the type-specifiers and attributes. */
13169 tree type_specifier;
13170 bool is_cv_qualifier;
13172 /* Check for attributes first. */
13173 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13175 type_specifier_seq->attributes =
13176 chainon (type_specifier_seq->attributes,
13177 cp_parser_attributes_opt (parser));
13181 /* Look for the type-specifier. */
13182 type_specifier = cp_parser_type_specifier (parser,
13184 type_specifier_seq,
13185 /*is_declaration=*/false,
13188 if (!type_specifier)
13190 /* If the first type-specifier could not be found, this is not a
13191 type-specifier-seq at all. */
13192 if (!seen_type_specifier)
13194 cp_parser_error (parser, "expected type-specifier");
13195 type_specifier_seq->type = error_mark_node;
13198 /* If subsequent type-specifiers could not be found, the
13199 type-specifier-seq is complete. */
13203 seen_type_specifier = true;
13204 /* The standard says that a condition can be:
13206 type-specifier-seq declarator = assignment-expression
13213 we should treat the "S" as a declarator, not as a
13214 type-specifier. The standard doesn't say that explicitly for
13215 type-specifier-seq, but it does say that for
13216 decl-specifier-seq in an ordinary declaration. Perhaps it
13217 would be clearer just to allow a decl-specifier-seq here, and
13218 then add a semantic restriction that if any decl-specifiers
13219 that are not type-specifiers appear, the program is invalid. */
13220 if (is_condition && !is_cv_qualifier)
13221 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13224 cp_parser_check_decl_spec (type_specifier_seq);
13227 /* Parse a parameter-declaration-clause.
13229 parameter-declaration-clause:
13230 parameter-declaration-list [opt] ... [opt]
13231 parameter-declaration-list , ...
13233 Returns a representation for the parameter declarations. A return
13234 value of NULL indicates a parameter-declaration-clause consisting
13235 only of an ellipsis. */
13237 static cp_parameter_declarator *
13238 cp_parser_parameter_declaration_clause (cp_parser* parser)
13240 cp_parameter_declarator *parameters;
13245 /* Peek at the next token. */
13246 token = cp_lexer_peek_token (parser->lexer);
13247 /* Check for trivial parameter-declaration-clauses. */
13248 if (token->type == CPP_ELLIPSIS)
13250 /* Consume the `...' token. */
13251 cp_lexer_consume_token (parser->lexer);
13254 else if (token->type == CPP_CLOSE_PAREN)
13255 /* There are no parameters. */
13257 #ifndef NO_IMPLICIT_EXTERN_C
13258 if (in_system_header && current_class_type == NULL
13259 && current_lang_name == lang_name_c)
13263 return no_parameters;
13265 /* Check for `(void)', too, which is a special case. */
13266 else if (token->keyword == RID_VOID
13267 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13268 == CPP_CLOSE_PAREN))
13270 /* Consume the `void' token. */
13271 cp_lexer_consume_token (parser->lexer);
13272 /* There are no parameters. */
13273 return no_parameters;
13276 /* Parse the parameter-declaration-list. */
13277 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13278 /* If a parse error occurred while parsing the
13279 parameter-declaration-list, then the entire
13280 parameter-declaration-clause is erroneous. */
13284 /* Peek at the next token. */
13285 token = cp_lexer_peek_token (parser->lexer);
13286 /* If it's a `,', the clause should terminate with an ellipsis. */
13287 if (token->type == CPP_COMMA)
13289 /* Consume the `,'. */
13290 cp_lexer_consume_token (parser->lexer);
13291 /* Expect an ellipsis. */
13293 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13295 /* It might also be `...' if the optional trailing `,' was
13297 else if (token->type == CPP_ELLIPSIS)
13299 /* Consume the `...' token. */
13300 cp_lexer_consume_token (parser->lexer);
13301 /* And remember that we saw it. */
13305 ellipsis_p = false;
13307 /* Finish the parameter list. */
13308 if (parameters && ellipsis_p)
13309 parameters->ellipsis_p = true;
13314 /* Parse a parameter-declaration-list.
13316 parameter-declaration-list:
13317 parameter-declaration
13318 parameter-declaration-list , parameter-declaration
13320 Returns a representation of the parameter-declaration-list, as for
13321 cp_parser_parameter_declaration_clause. However, the
13322 `void_list_node' is never appended to the list. Upon return,
13323 *IS_ERROR will be true iff an error occurred. */
13325 static cp_parameter_declarator *
13326 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13328 cp_parameter_declarator *parameters = NULL;
13329 cp_parameter_declarator **tail = ¶meters;
13330 bool saved_in_unbraced_linkage_specification_p;
13332 /* Assume all will go well. */
13334 /* The special considerations that apply to a function within an
13335 unbraced linkage specifications do not apply to the parameters
13336 to the function. */
13337 saved_in_unbraced_linkage_specification_p
13338 = parser->in_unbraced_linkage_specification_p;
13339 parser->in_unbraced_linkage_specification_p = false;
13341 /* Look for more parameters. */
13344 cp_parameter_declarator *parameter;
13345 bool parenthesized_p;
13346 /* Parse the parameter. */
13348 = cp_parser_parameter_declaration (parser,
13349 /*template_parm_p=*/false,
13352 /* If a parse error occurred parsing the parameter declaration,
13353 then the entire parameter-declaration-list is erroneous. */
13360 /* Add the new parameter to the list. */
13362 tail = ¶meter->next;
13364 /* Peek at the next token. */
13365 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13366 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13367 /* These are for Objective-C++ */
13368 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13369 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13370 /* The parameter-declaration-list is complete. */
13372 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13376 /* Peek at the next token. */
13377 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13378 /* If it's an ellipsis, then the list is complete. */
13379 if (token->type == CPP_ELLIPSIS)
13381 /* Otherwise, there must be more parameters. Consume the
13383 cp_lexer_consume_token (parser->lexer);
13384 /* When parsing something like:
13386 int i(float f, double d)
13388 we can tell after seeing the declaration for "f" that we
13389 are not looking at an initialization of a variable "i",
13390 but rather at the declaration of a function "i".
13392 Due to the fact that the parsing of template arguments
13393 (as specified to a template-id) requires backtracking we
13394 cannot use this technique when inside a template argument
13396 if (!parser->in_template_argument_list_p
13397 && !parser->in_type_id_in_expr_p
13398 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13399 /* However, a parameter-declaration of the form
13400 "foat(f)" (which is a valid declaration of a
13401 parameter "f") can also be interpreted as an
13402 expression (the conversion of "f" to "float"). */
13403 && !parenthesized_p)
13404 cp_parser_commit_to_tentative_parse (parser);
13408 cp_parser_error (parser, "expected %<,%> or %<...%>");
13409 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13410 cp_parser_skip_to_closing_parenthesis (parser,
13411 /*recovering=*/true,
13412 /*or_comma=*/false,
13413 /*consume_paren=*/false);
13418 parser->in_unbraced_linkage_specification_p
13419 = saved_in_unbraced_linkage_specification_p;
13424 /* Parse a parameter declaration.
13426 parameter-declaration:
13427 decl-specifier-seq ... [opt] declarator
13428 decl-specifier-seq declarator = assignment-expression
13429 decl-specifier-seq ... [opt] abstract-declarator [opt]
13430 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13432 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13433 declares a template parameter. (In that case, a non-nested `>'
13434 token encountered during the parsing of the assignment-expression
13435 is not interpreted as a greater-than operator.)
13437 Returns a representation of the parameter, or NULL if an error
13438 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13439 true iff the declarator is of the form "(p)". */
13441 static cp_parameter_declarator *
13442 cp_parser_parameter_declaration (cp_parser *parser,
13443 bool template_parm_p,
13444 bool *parenthesized_p)
13446 int declares_class_or_enum;
13447 bool greater_than_is_operator_p;
13448 cp_decl_specifier_seq decl_specifiers;
13449 cp_declarator *declarator;
13450 tree default_argument;
13452 const char *saved_message;
13454 /* In a template parameter, `>' is not an operator.
13458 When parsing a default template-argument for a non-type
13459 template-parameter, the first non-nested `>' is taken as the end
13460 of the template parameter-list rather than a greater-than
13462 greater_than_is_operator_p = !template_parm_p;
13464 /* Type definitions may not appear in parameter types. */
13465 saved_message = parser->type_definition_forbidden_message;
13466 parser->type_definition_forbidden_message
13467 = "types may not be defined in parameter types";
13469 /* Parse the declaration-specifiers. */
13470 cp_parser_decl_specifier_seq (parser,
13471 CP_PARSER_FLAGS_NONE,
13473 &declares_class_or_enum);
13474 /* If an error occurred, there's no reason to attempt to parse the
13475 rest of the declaration. */
13476 if (cp_parser_error_occurred (parser))
13478 parser->type_definition_forbidden_message = saved_message;
13482 /* Peek at the next token. */
13483 token = cp_lexer_peek_token (parser->lexer);
13485 /* If the next token is a `)', `,', `=', `>', or `...', then there
13486 is no declarator. However, when variadic templates are enabled,
13487 there may be a declarator following `...'. */
13488 if (token->type == CPP_CLOSE_PAREN
13489 || token->type == CPP_COMMA
13490 || token->type == CPP_EQ
13491 || token->type == CPP_GREATER)
13494 if (parenthesized_p)
13495 *parenthesized_p = false;
13497 /* Otherwise, there should be a declarator. */
13500 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13501 parser->default_arg_ok_p = false;
13503 /* After seeing a decl-specifier-seq, if the next token is not a
13504 "(", there is no possibility that the code is a valid
13505 expression. Therefore, if parsing tentatively, we commit at
13507 if (!parser->in_template_argument_list_p
13508 /* In an expression context, having seen:
13512 we cannot be sure whether we are looking at a
13513 function-type (taking a "char" as a parameter) or a cast
13514 of some object of type "char" to "int". */
13515 && !parser->in_type_id_in_expr_p
13516 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13517 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13518 cp_parser_commit_to_tentative_parse (parser);
13519 /* Parse the declarator. */
13520 declarator = cp_parser_declarator (parser,
13521 CP_PARSER_DECLARATOR_EITHER,
13522 /*ctor_dtor_or_conv_p=*/NULL,
13524 /*member_p=*/false);
13525 parser->default_arg_ok_p = saved_default_arg_ok_p;
13526 /* After the declarator, allow more attributes. */
13527 decl_specifiers.attributes
13528 = chainon (decl_specifiers.attributes,
13529 cp_parser_attributes_opt (parser));
13532 /* If the next token is an ellipsis, and we have not seen a
13533 declarator name, and the type of the declarator contains parameter
13534 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13535 a parameter pack expansion expression. Otherwise, leave the
13536 ellipsis for a C-style variadic function. */
13537 token = cp_lexer_peek_token (parser->lexer);
13538 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13540 tree type = decl_specifiers.type;
13542 if (type && DECL_P (type))
13543 type = TREE_TYPE (type);
13546 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13547 && declarator_can_be_parameter_pack (declarator)
13548 && (!declarator || !declarator->parameter_pack_p)
13549 && uses_parameter_packs (type))
13551 /* Consume the `...'. */
13552 cp_lexer_consume_token (parser->lexer);
13553 maybe_warn_variadic_templates ();
13555 /* Build a pack expansion type */
13557 declarator->parameter_pack_p = true;
13559 decl_specifiers.type = make_pack_expansion (type);
13563 /* The restriction on defining new types applies only to the type
13564 of the parameter, not to the default argument. */
13565 parser->type_definition_forbidden_message = saved_message;
13567 /* If the next token is `=', then process a default argument. */
13568 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13570 /* Consume the `='. */
13571 cp_lexer_consume_token (parser->lexer);
13573 /* If we are defining a class, then the tokens that make up the
13574 default argument must be saved and processed later. */
13575 if (!template_parm_p && at_class_scope_p ()
13576 && TYPE_BEING_DEFINED (current_class_type))
13578 unsigned depth = 0;
13579 cp_token *first_token;
13582 /* Add tokens until we have processed the entire default
13583 argument. We add the range [first_token, token). */
13584 first_token = cp_lexer_peek_token (parser->lexer);
13589 /* Peek at the next token. */
13590 token = cp_lexer_peek_token (parser->lexer);
13591 /* What we do depends on what token we have. */
13592 switch (token->type)
13594 /* In valid code, a default argument must be
13595 immediately followed by a `,' `)', or `...'. */
13597 case CPP_CLOSE_PAREN:
13599 /* If we run into a non-nested `;', `}', or `]',
13600 then the code is invalid -- but the default
13601 argument is certainly over. */
13602 case CPP_SEMICOLON:
13603 case CPP_CLOSE_BRACE:
13604 case CPP_CLOSE_SQUARE:
13607 /* Update DEPTH, if necessary. */
13608 else if (token->type == CPP_CLOSE_PAREN
13609 || token->type == CPP_CLOSE_BRACE
13610 || token->type == CPP_CLOSE_SQUARE)
13614 case CPP_OPEN_PAREN:
13615 case CPP_OPEN_SQUARE:
13616 case CPP_OPEN_BRACE:
13621 if (cxx_dialect == cxx98)
13623 /* Fall through for C++0x, which treats the `>>'
13624 operator like two `>' tokens in certain
13628 /* If we see a non-nested `>', and `>' is not an
13629 operator, then it marks the end of the default
13631 if (!depth && !greater_than_is_operator_p)
13635 /* If we run out of tokens, issue an error message. */
13637 case CPP_PRAGMA_EOL:
13638 error ("file ends in default argument");
13644 /* In these cases, we should look for template-ids.
13645 For example, if the default argument is
13646 `X<int, double>()', we need to do name lookup to
13647 figure out whether or not `X' is a template; if
13648 so, the `,' does not end the default argument.
13650 That is not yet done. */
13657 /* If we've reached the end, stop. */
13661 /* Add the token to the token block. */
13662 token = cp_lexer_consume_token (parser->lexer);
13665 /* Create a DEFAULT_ARG to represent the unparsed default
13667 default_argument = make_node (DEFAULT_ARG);
13668 DEFARG_TOKENS (default_argument)
13669 = cp_token_cache_new (first_token, token);
13670 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13672 /* Outside of a class definition, we can just parse the
13673 assignment-expression. */
13676 = cp_parser_default_argument (parser, template_parm_p);
13678 if (!parser->default_arg_ok_p)
13680 if (!flag_pedantic_errors)
13681 warning (0, "deprecated use of default argument for parameter of non-function");
13684 error ("default arguments are only permitted for function parameters");
13685 default_argument = NULL_TREE;
13688 else if ((declarator && declarator->parameter_pack_p)
13689 || (decl_specifiers.type
13690 && PACK_EXPANSION_P (decl_specifiers.type)))
13692 const char* kind = template_parm_p? "template " : "";
13694 /* Find the name of the parameter pack. */
13695 cp_declarator *id_declarator = declarator;
13696 while (id_declarator && id_declarator->kind != cdk_id)
13697 id_declarator = id_declarator->declarator;
13699 if (id_declarator && id_declarator->kind == cdk_id)
13700 error ("%sparameter pack %qD cannot have a default argument",
13701 kind, id_declarator->u.id.unqualified_name);
13703 error ("%sparameter pack cannot have a default argument",
13706 default_argument = NULL_TREE;
13710 default_argument = NULL_TREE;
13712 return make_parameter_declarator (&decl_specifiers,
13717 /* Parse a default argument and return it.
13719 TEMPLATE_PARM_P is true if this is a default argument for a
13720 non-type template parameter. */
13722 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13724 tree default_argument = NULL_TREE;
13725 bool saved_greater_than_is_operator_p;
13726 bool saved_local_variables_forbidden_p;
13728 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13730 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13731 parser->greater_than_is_operator_p = !template_parm_p;
13732 /* Local variable names (and the `this' keyword) may not
13733 appear in a default argument. */
13734 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13735 parser->local_variables_forbidden_p = true;
13736 /* The default argument expression may cause implicitly
13737 defined member functions to be synthesized, which will
13738 result in garbage collection. We must treat this
13739 situation as if we were within the body of function so as
13740 to avoid collecting live data on the stack. */
13742 /* Parse the assignment-expression. */
13743 if (template_parm_p)
13744 push_deferring_access_checks (dk_no_deferred);
13746 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13747 if (template_parm_p)
13748 pop_deferring_access_checks ();
13749 /* Restore saved state. */
13751 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13752 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13754 return default_argument;
13757 /* Parse a function-body.
13760 compound_statement */
13763 cp_parser_function_body (cp_parser *parser)
13765 cp_parser_compound_statement (parser, NULL, false);
13768 /* Parse a ctor-initializer-opt followed by a function-body. Return
13769 true if a ctor-initializer was present. */
13772 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13775 bool ctor_initializer_p;
13777 /* Begin the function body. */
13778 body = begin_function_body ();
13779 /* Parse the optional ctor-initializer. */
13780 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13781 /* Parse the function-body. */
13782 cp_parser_function_body (parser);
13783 /* Finish the function body. */
13784 finish_function_body (body);
13786 return ctor_initializer_p;
13789 /* Parse an initializer.
13792 = initializer-clause
13793 ( expression-list )
13795 Returns an expression representing the initializer. If no
13796 initializer is present, NULL_TREE is returned.
13798 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13799 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13800 set to FALSE if there is no initializer present. If there is an
13801 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13802 is set to true; otherwise it is set to false. */
13805 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13806 bool* non_constant_p)
13811 /* Peek at the next token. */
13812 token = cp_lexer_peek_token (parser->lexer);
13814 /* Let our caller know whether or not this initializer was
13816 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13817 /* Assume that the initializer is constant. */
13818 *non_constant_p = false;
13820 if (token->type == CPP_EQ)
13822 /* Consume the `='. */
13823 cp_lexer_consume_token (parser->lexer);
13824 /* Parse the initializer-clause. */
13825 init = cp_parser_initializer_clause (parser, non_constant_p);
13827 else if (token->type == CPP_OPEN_PAREN)
13828 init = cp_parser_parenthesized_expression_list (parser, false,
13830 /*allow_expansion_p=*/true,
13834 /* Anything else is an error. */
13835 cp_parser_error (parser, "expected initializer");
13836 init = error_mark_node;
13842 /* Parse an initializer-clause.
13844 initializer-clause:
13845 assignment-expression
13846 { initializer-list , [opt] }
13849 Returns an expression representing the initializer.
13851 If the `assignment-expression' production is used the value
13852 returned is simply a representation for the expression.
13854 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13855 the elements of the initializer-list (or NULL, if the last
13856 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13857 NULL_TREE. There is no way to detect whether or not the optional
13858 trailing `,' was provided. NON_CONSTANT_P is as for
13859 cp_parser_initializer. */
13862 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13866 /* Assume the expression is constant. */
13867 *non_constant_p = false;
13869 /* If it is not a `{', then we are looking at an
13870 assignment-expression. */
13871 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13874 = cp_parser_constant_expression (parser,
13875 /*allow_non_constant_p=*/true,
13877 if (!*non_constant_p)
13878 initializer = fold_non_dependent_expr (initializer);
13882 /* Consume the `{' token. */
13883 cp_lexer_consume_token (parser->lexer);
13884 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13885 initializer = make_node (CONSTRUCTOR);
13886 /* If it's not a `}', then there is a non-trivial initializer. */
13887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13889 /* Parse the initializer list. */
13890 CONSTRUCTOR_ELTS (initializer)
13891 = cp_parser_initializer_list (parser, non_constant_p);
13892 /* A trailing `,' token is allowed. */
13893 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13894 cp_lexer_consume_token (parser->lexer);
13896 /* Now, there should be a trailing `}'. */
13897 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13900 return initializer;
13903 /* Parse an initializer-list.
13906 initializer-clause ... [opt]
13907 initializer-list , initializer-clause ... [opt]
13912 identifier : initializer-clause
13913 initializer-list, identifier : initializer-clause
13915 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13916 for the initializer. If the INDEX of the elt is non-NULL, it is the
13917 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13918 as for cp_parser_initializer. */
13920 static VEC(constructor_elt,gc) *
13921 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13923 VEC(constructor_elt,gc) *v = NULL;
13925 /* Assume all of the expressions are constant. */
13926 *non_constant_p = false;
13928 /* Parse the rest of the list. */
13934 bool clause_non_constant_p;
13936 /* If the next token is an identifier and the following one is a
13937 colon, we are looking at the GNU designated-initializer
13939 if (cp_parser_allow_gnu_extensions_p (parser)
13940 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13941 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13943 /* Warn the user that they are using an extension. */
13945 pedwarn ("ISO C++ does not allow designated initializers");
13946 /* Consume the identifier. */
13947 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13948 /* Consume the `:'. */
13949 cp_lexer_consume_token (parser->lexer);
13952 identifier = NULL_TREE;
13954 /* Parse the initializer. */
13955 initializer = cp_parser_initializer_clause (parser,
13956 &clause_non_constant_p);
13957 /* If any clause is non-constant, so is the entire initializer. */
13958 if (clause_non_constant_p)
13959 *non_constant_p = true;
13961 /* If we have an ellipsis, this is an initializer pack
13963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13965 /* Consume the `...'. */
13966 cp_lexer_consume_token (parser->lexer);
13968 /* Turn the initializer into an initializer expansion. */
13969 initializer = make_pack_expansion (initializer);
13972 /* Add it to the vector. */
13973 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13975 /* If the next token is not a comma, we have reached the end of
13977 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13980 /* Peek at the next token. */
13981 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13982 /* If the next token is a `}', then we're still done. An
13983 initializer-clause can have a trailing `,' after the
13984 initializer-list and before the closing `}'. */
13985 if (token->type == CPP_CLOSE_BRACE)
13988 /* Consume the `,' token. */
13989 cp_lexer_consume_token (parser->lexer);
13995 /* Classes [gram.class] */
13997 /* Parse a class-name.
14003 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14004 to indicate that names looked up in dependent types should be
14005 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14006 keyword has been used to indicate that the name that appears next
14007 is a template. TAG_TYPE indicates the explicit tag given before
14008 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14009 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14010 is the class being defined in a class-head.
14012 Returns the TYPE_DECL representing the class. */
14015 cp_parser_class_name (cp_parser *parser,
14016 bool typename_keyword_p,
14017 bool template_keyword_p,
14018 enum tag_types tag_type,
14019 bool check_dependency_p,
14021 bool is_declaration)
14028 /* All class-names start with an identifier. */
14029 token = cp_lexer_peek_token (parser->lexer);
14030 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14032 cp_parser_error (parser, "expected class-name");
14033 return error_mark_node;
14036 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14037 to a template-id, so we save it here. */
14038 scope = parser->scope;
14039 if (scope == error_mark_node)
14040 return error_mark_node;
14042 /* Any name names a type if we're following the `typename' keyword
14043 in a qualified name where the enclosing scope is type-dependent. */
14044 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14045 && dependent_type_p (scope));
14046 /* Handle the common case (an identifier, but not a template-id)
14048 if (token->type == CPP_NAME
14049 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14051 cp_token *identifier_token;
14055 /* Look for the identifier. */
14056 identifier_token = cp_lexer_peek_token (parser->lexer);
14057 ambiguous_p = identifier_token->ambiguous_p;
14058 identifier = cp_parser_identifier (parser);
14059 /* If the next token isn't an identifier, we are certainly not
14060 looking at a class-name. */
14061 if (identifier == error_mark_node)
14062 decl = error_mark_node;
14063 /* If we know this is a type-name, there's no need to look it
14065 else if (typename_p)
14069 tree ambiguous_decls;
14070 /* If we already know that this lookup is ambiguous, then
14071 we've already issued an error message; there's no reason
14075 cp_parser_simulate_error (parser);
14076 return error_mark_node;
14078 /* If the next token is a `::', then the name must be a type
14081 [basic.lookup.qual]
14083 During the lookup for a name preceding the :: scope
14084 resolution operator, object, function, and enumerator
14085 names are ignored. */
14086 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14087 tag_type = typename_type;
14088 /* Look up the name. */
14089 decl = cp_parser_lookup_name (parser, identifier,
14091 /*is_template=*/false,
14092 /*is_namespace=*/false,
14093 check_dependency_p,
14095 if (ambiguous_decls)
14097 error ("reference to %qD is ambiguous", identifier);
14098 print_candidates (ambiguous_decls);
14099 if (cp_parser_parsing_tentatively (parser))
14101 identifier_token->ambiguous_p = true;
14102 cp_parser_simulate_error (parser);
14104 return error_mark_node;
14110 /* Try a template-id. */
14111 decl = cp_parser_template_id (parser, template_keyword_p,
14112 check_dependency_p,
14114 if (decl == error_mark_node)
14115 return error_mark_node;
14118 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14120 /* If this is a typename, create a TYPENAME_TYPE. */
14121 if (typename_p && decl != error_mark_node)
14123 decl = make_typename_type (scope, decl, typename_type,
14124 /*complain=*/tf_error);
14125 if (decl != error_mark_node)
14126 decl = TYPE_NAME (decl);
14129 /* Check to see that it is really the name of a class. */
14130 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14131 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14132 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14133 /* Situations like this:
14135 template <typename T> struct A {
14136 typename T::template X<int>::I i;
14139 are problematic. Is `T::template X<int>' a class-name? The
14140 standard does not seem to be definitive, but there is no other
14141 valid interpretation of the following `::'. Therefore, those
14142 names are considered class-names. */
14144 decl = make_typename_type (scope, decl, tag_type, tf_error);
14145 if (decl != error_mark_node)
14146 decl = TYPE_NAME (decl);
14148 else if (TREE_CODE (decl) != TYPE_DECL
14149 || TREE_TYPE (decl) == error_mark_node
14150 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14151 decl = error_mark_node;
14153 if (decl == error_mark_node)
14154 cp_parser_error (parser, "expected class-name");
14159 /* Parse a class-specifier.
14162 class-head { member-specification [opt] }
14164 Returns the TREE_TYPE representing the class. */
14167 cp_parser_class_specifier (cp_parser* parser)
14171 tree attributes = NULL_TREE;
14172 int has_trailing_semicolon;
14173 bool nested_name_specifier_p;
14174 unsigned saved_num_template_parameter_lists;
14175 bool saved_in_function_body;
14176 tree old_scope = NULL_TREE;
14177 tree scope = NULL_TREE;
14180 push_deferring_access_checks (dk_no_deferred);
14182 /* Parse the class-head. */
14183 type = cp_parser_class_head (parser,
14184 &nested_name_specifier_p,
14187 /* If the class-head was a semantic disaster, skip the entire body
14191 cp_parser_skip_to_end_of_block_or_statement (parser);
14192 pop_deferring_access_checks ();
14193 return error_mark_node;
14196 /* Look for the `{'. */
14197 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14199 pop_deferring_access_checks ();
14200 return error_mark_node;
14203 /* Process the base classes. If they're invalid, skip the
14204 entire class body. */
14205 if (!xref_basetypes (type, bases))
14207 /* Consuming the closing brace yields better error messages
14209 if (cp_parser_skip_to_closing_brace (parser))
14210 cp_lexer_consume_token (parser->lexer);
14211 pop_deferring_access_checks ();
14212 return error_mark_node;
14215 /* Issue an error message if type-definitions are forbidden here. */
14216 cp_parser_check_type_definition (parser);
14217 /* Remember that we are defining one more class. */
14218 ++parser->num_classes_being_defined;
14219 /* Inside the class, surrounding template-parameter-lists do not
14221 saved_num_template_parameter_lists
14222 = parser->num_template_parameter_lists;
14223 parser->num_template_parameter_lists = 0;
14224 /* We are not in a function body. */
14225 saved_in_function_body = parser->in_function_body;
14226 parser->in_function_body = false;
14228 /* Start the class. */
14229 if (nested_name_specifier_p)
14231 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14232 old_scope = push_inner_scope (scope);
14234 type = begin_class_definition (type, attributes);
14236 if (type == error_mark_node)
14237 /* If the type is erroneous, skip the entire body of the class. */
14238 cp_parser_skip_to_closing_brace (parser);
14240 /* Parse the member-specification. */
14241 cp_parser_member_specification_opt (parser);
14243 /* Look for the trailing `}'. */
14244 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14245 /* We get better error messages by noticing a common problem: a
14246 missing trailing `;'. */
14247 token = cp_lexer_peek_token (parser->lexer);
14248 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14249 /* Look for trailing attributes to apply to this class. */
14250 if (cp_parser_allow_gnu_extensions_p (parser))
14251 attributes = cp_parser_attributes_opt (parser);
14252 if (type != error_mark_node)
14253 type = finish_struct (type, attributes);
14254 if (nested_name_specifier_p)
14255 pop_inner_scope (old_scope, scope);
14256 /* If this class is not itself within the scope of another class,
14257 then we need to parse the bodies of all of the queued function
14258 definitions. Note that the queued functions defined in a class
14259 are not always processed immediately following the
14260 class-specifier for that class. Consider:
14263 struct B { void f() { sizeof (A); } };
14266 If `f' were processed before the processing of `A' were
14267 completed, there would be no way to compute the size of `A'.
14268 Note that the nesting we are interested in here is lexical --
14269 not the semantic nesting given by TYPE_CONTEXT. In particular,
14272 struct A { struct B; };
14273 struct A::B { void f() { } };
14275 there is no need to delay the parsing of `A::B::f'. */
14276 if (--parser->num_classes_being_defined == 0)
14280 tree class_type = NULL_TREE;
14281 tree pushed_scope = NULL_TREE;
14283 /* In a first pass, parse default arguments to the functions.
14284 Then, in a second pass, parse the bodies of the functions.
14285 This two-phased approach handles cases like:
14293 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14294 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14295 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14296 TREE_PURPOSE (parser->unparsed_functions_queues)
14297 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14299 fn = TREE_VALUE (queue_entry);
14300 /* If there are default arguments that have not yet been processed,
14301 take care of them now. */
14302 if (class_type != TREE_PURPOSE (queue_entry))
14305 pop_scope (pushed_scope);
14306 class_type = TREE_PURPOSE (queue_entry);
14307 pushed_scope = push_scope (class_type);
14309 /* Make sure that any template parameters are in scope. */
14310 maybe_begin_member_template_processing (fn);
14311 /* Parse the default argument expressions. */
14312 cp_parser_late_parsing_default_args (parser, fn);
14313 /* Remove any template parameters from the symbol table. */
14314 maybe_end_member_template_processing ();
14317 pop_scope (pushed_scope);
14318 /* Now parse the body of the functions. */
14319 for (TREE_VALUE (parser->unparsed_functions_queues)
14320 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14321 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14322 TREE_VALUE (parser->unparsed_functions_queues)
14323 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14325 /* Figure out which function we need to process. */
14326 fn = TREE_VALUE (queue_entry);
14327 /* Parse the function. */
14328 cp_parser_late_parsing_for_member (parser, fn);
14332 /* Put back any saved access checks. */
14333 pop_deferring_access_checks ();
14335 /* Restore saved state. */
14336 parser->in_function_body = saved_in_function_body;
14337 parser->num_template_parameter_lists
14338 = saved_num_template_parameter_lists;
14343 /* Parse a class-head.
14346 class-key identifier [opt] base-clause [opt]
14347 class-key nested-name-specifier identifier base-clause [opt]
14348 class-key nested-name-specifier [opt] template-id
14352 class-key attributes identifier [opt] base-clause [opt]
14353 class-key attributes nested-name-specifier identifier base-clause [opt]
14354 class-key attributes nested-name-specifier [opt] template-id
14357 Upon return BASES is initialized to the list of base classes (or
14358 NULL, if there are none) in the same form returned by
14359 cp_parser_base_clause.
14361 Returns the TYPE of the indicated class. Sets
14362 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14363 involving a nested-name-specifier was used, and FALSE otherwise.
14365 Returns error_mark_node if this is not a class-head.
14367 Returns NULL_TREE if the class-head is syntactically valid, but
14368 semantically invalid in a way that means we should skip the entire
14369 body of the class. */
14372 cp_parser_class_head (cp_parser* parser,
14373 bool* nested_name_specifier_p,
14374 tree *attributes_p,
14377 tree nested_name_specifier;
14378 enum tag_types class_key;
14379 tree id = NULL_TREE;
14380 tree type = NULL_TREE;
14382 bool template_id_p = false;
14383 bool qualified_p = false;
14384 bool invalid_nested_name_p = false;
14385 bool invalid_explicit_specialization_p = false;
14386 tree pushed_scope = NULL_TREE;
14387 unsigned num_templates;
14389 /* Assume no nested-name-specifier will be present. */
14390 *nested_name_specifier_p = false;
14391 /* Assume no template parameter lists will be used in defining the
14395 *bases = NULL_TREE;
14397 /* Look for the class-key. */
14398 class_key = cp_parser_class_key (parser);
14399 if (class_key == none_type)
14400 return error_mark_node;
14402 /* Parse the attributes. */
14403 attributes = cp_parser_attributes_opt (parser);
14405 /* If the next token is `::', that is invalid -- but sometimes
14406 people do try to write:
14410 Handle this gracefully by accepting the extra qualifier, and then
14411 issuing an error about it later if this really is a
14412 class-head. If it turns out just to be an elaborated type
14413 specifier, remain silent. */
14414 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14415 qualified_p = true;
14417 push_deferring_access_checks (dk_no_check);
14419 /* Determine the name of the class. Begin by looking for an
14420 optional nested-name-specifier. */
14421 nested_name_specifier
14422 = cp_parser_nested_name_specifier_opt (parser,
14423 /*typename_keyword_p=*/false,
14424 /*check_dependency_p=*/false,
14426 /*is_declaration=*/false);
14427 /* If there was a nested-name-specifier, then there *must* be an
14429 if (nested_name_specifier)
14431 /* Although the grammar says `identifier', it really means
14432 `class-name' or `template-name'. You are only allowed to
14433 define a class that has already been declared with this
14436 The proposed resolution for Core Issue 180 says that wherever
14437 you see `class T::X' you should treat `X' as a type-name.
14439 It is OK to define an inaccessible class; for example:
14441 class A { class B; };
14444 We do not know if we will see a class-name, or a
14445 template-name. We look for a class-name first, in case the
14446 class-name is a template-id; if we looked for the
14447 template-name first we would stop after the template-name. */
14448 cp_parser_parse_tentatively (parser);
14449 type = cp_parser_class_name (parser,
14450 /*typename_keyword_p=*/false,
14451 /*template_keyword_p=*/false,
14453 /*check_dependency_p=*/false,
14454 /*class_head_p=*/true,
14455 /*is_declaration=*/false);
14456 /* If that didn't work, ignore the nested-name-specifier. */
14457 if (!cp_parser_parse_definitely (parser))
14459 invalid_nested_name_p = true;
14460 id = cp_parser_identifier (parser);
14461 if (id == error_mark_node)
14464 /* If we could not find a corresponding TYPE, treat this
14465 declaration like an unqualified declaration. */
14466 if (type == error_mark_node)
14467 nested_name_specifier = NULL_TREE;
14468 /* Otherwise, count the number of templates used in TYPE and its
14469 containing scopes. */
14474 for (scope = TREE_TYPE (type);
14475 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14476 scope = (TYPE_P (scope)
14477 ? TYPE_CONTEXT (scope)
14478 : DECL_CONTEXT (scope)))
14480 && CLASS_TYPE_P (scope)
14481 && CLASSTYPE_TEMPLATE_INFO (scope)
14482 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14483 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14487 /* Otherwise, the identifier is optional. */
14490 /* We don't know whether what comes next is a template-id,
14491 an identifier, or nothing at all. */
14492 cp_parser_parse_tentatively (parser);
14493 /* Check for a template-id. */
14494 id = cp_parser_template_id (parser,
14495 /*template_keyword_p=*/false,
14496 /*check_dependency_p=*/true,
14497 /*is_declaration=*/true);
14498 /* If that didn't work, it could still be an identifier. */
14499 if (!cp_parser_parse_definitely (parser))
14501 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14502 id = cp_parser_identifier (parser);
14508 template_id_p = true;
14513 pop_deferring_access_checks ();
14516 cp_parser_check_for_invalid_template_id (parser, id);
14518 /* If it's not a `:' or a `{' then we can't really be looking at a
14519 class-head, since a class-head only appears as part of a
14520 class-specifier. We have to detect this situation before calling
14521 xref_tag, since that has irreversible side-effects. */
14522 if (!cp_parser_next_token_starts_class_definition_p (parser))
14524 cp_parser_error (parser, "expected %<{%> or %<:%>");
14525 return error_mark_node;
14528 /* At this point, we're going ahead with the class-specifier, even
14529 if some other problem occurs. */
14530 cp_parser_commit_to_tentative_parse (parser);
14531 /* Issue the error about the overly-qualified name now. */
14533 cp_parser_error (parser,
14534 "global qualification of class name is invalid");
14535 else if (invalid_nested_name_p)
14536 cp_parser_error (parser,
14537 "qualified name does not name a class");
14538 else if (nested_name_specifier)
14542 /* Reject typedef-names in class heads. */
14543 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14545 error ("invalid class name in declaration of %qD", type);
14550 /* Figure out in what scope the declaration is being placed. */
14551 scope = current_scope ();
14552 /* If that scope does not contain the scope in which the
14553 class was originally declared, the program is invalid. */
14554 if (scope && !is_ancestor (scope, nested_name_specifier))
14556 if (at_namespace_scope_p ())
14557 error ("declaration of %qD in namespace %qD which does not "
14558 "enclose %qD", type, scope, nested_name_specifier);
14560 error ("declaration of %qD in %qD which does not enclose %qD",
14561 type, scope, nested_name_specifier);
14567 A declarator-id shall not be qualified exception of the
14568 definition of a ... nested class outside of its class
14569 ... [or] a the definition or explicit instantiation of a
14570 class member of a namespace outside of its namespace. */
14571 if (scope == nested_name_specifier)
14573 pedwarn ("extra qualification ignored");
14574 nested_name_specifier = NULL_TREE;
14578 /* An explicit-specialization must be preceded by "template <>". If
14579 it is not, try to recover gracefully. */
14580 if (at_namespace_scope_p ()
14581 && parser->num_template_parameter_lists == 0
14584 error ("an explicit specialization must be preceded by %<template <>%>");
14585 invalid_explicit_specialization_p = true;
14586 /* Take the same action that would have been taken by
14587 cp_parser_explicit_specialization. */
14588 ++parser->num_template_parameter_lists;
14589 begin_specialization ();
14591 /* There must be no "return" statements between this point and the
14592 end of this function; set "type "to the correct return value and
14593 use "goto done;" to return. */
14594 /* Make sure that the right number of template parameters were
14596 if (!cp_parser_check_template_parameters (parser, num_templates))
14598 /* If something went wrong, there is no point in even trying to
14599 process the class-definition. */
14604 /* Look up the type. */
14607 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14608 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14609 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14611 error ("function template %qD redeclared as a class template", id);
14612 type = error_mark_node;
14616 type = TREE_TYPE (id);
14617 type = maybe_process_partial_specialization (type);
14619 if (nested_name_specifier)
14620 pushed_scope = push_scope (nested_name_specifier);
14622 else if (nested_name_specifier)
14628 template <typename T> struct S { struct T };
14629 template <typename T> struct S<T>::T { };
14631 we will get a TYPENAME_TYPE when processing the definition of
14632 `S::T'. We need to resolve it to the actual type before we
14633 try to define it. */
14634 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14636 class_type = resolve_typename_type (TREE_TYPE (type),
14637 /*only_current_p=*/false);
14638 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14639 type = TYPE_NAME (class_type);
14642 cp_parser_error (parser, "could not resolve typename type");
14643 type = error_mark_node;
14647 maybe_process_partial_specialization (TREE_TYPE (type));
14648 class_type = current_class_type;
14649 /* Enter the scope indicated by the nested-name-specifier. */
14650 pushed_scope = push_scope (nested_name_specifier);
14651 /* Get the canonical version of this type. */
14652 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14653 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14654 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14656 type = push_template_decl (type);
14657 if (type == error_mark_node)
14664 type = TREE_TYPE (type);
14665 *nested_name_specifier_p = true;
14667 else /* The name is not a nested name. */
14669 /* If the class was unnamed, create a dummy name. */
14671 id = make_anon_name ();
14672 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14673 parser->num_template_parameter_lists);
14676 /* Indicate whether this class was declared as a `class' or as a
14678 if (TREE_CODE (type) == RECORD_TYPE)
14679 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14680 cp_parser_check_class_key (class_key, type);
14682 /* If this type was already complete, and we see another definition,
14683 that's an error. */
14684 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14686 error ("redefinition of %q#T", type);
14687 error ("previous definition of %q+#T", type);
14691 else if (type == error_mark_node)
14694 /* We will have entered the scope containing the class; the names of
14695 base classes should be looked up in that context. For example:
14697 struct A { struct B {}; struct C; };
14698 struct A::C : B {};
14702 /* Get the list of base-classes, if there is one. */
14703 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14704 *bases = cp_parser_base_clause (parser);
14707 /* Leave the scope given by the nested-name-specifier. We will
14708 enter the class scope itself while processing the members. */
14710 pop_scope (pushed_scope);
14712 if (invalid_explicit_specialization_p)
14714 end_specialization ();
14715 --parser->num_template_parameter_lists;
14717 *attributes_p = attributes;
14721 /* Parse a class-key.
14728 Returns the kind of class-key specified, or none_type to indicate
14731 static enum tag_types
14732 cp_parser_class_key (cp_parser* parser)
14735 enum tag_types tag_type;
14737 /* Look for the class-key. */
14738 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14742 /* Check to see if the TOKEN is a class-key. */
14743 tag_type = cp_parser_token_is_class_key (token);
14745 cp_parser_error (parser, "expected class-key");
14749 /* Parse an (optional) member-specification.
14751 member-specification:
14752 member-declaration member-specification [opt]
14753 access-specifier : member-specification [opt] */
14756 cp_parser_member_specification_opt (cp_parser* parser)
14763 /* Peek at the next token. */
14764 token = cp_lexer_peek_token (parser->lexer);
14765 /* If it's a `}', or EOF then we've seen all the members. */
14766 if (token->type == CPP_CLOSE_BRACE
14767 || token->type == CPP_EOF
14768 || token->type == CPP_PRAGMA_EOL)
14771 /* See if this token is a keyword. */
14772 keyword = token->keyword;
14776 case RID_PROTECTED:
14778 /* Consume the access-specifier. */
14779 cp_lexer_consume_token (parser->lexer);
14780 /* Remember which access-specifier is active. */
14781 current_access_specifier = token->u.value;
14782 /* Look for the `:'. */
14783 cp_parser_require (parser, CPP_COLON, "`:'");
14787 /* Accept #pragmas at class scope. */
14788 if (token->type == CPP_PRAGMA)
14790 cp_parser_pragma (parser, pragma_external);
14794 /* Otherwise, the next construction must be a
14795 member-declaration. */
14796 cp_parser_member_declaration (parser);
14801 /* Parse a member-declaration.
14803 member-declaration:
14804 decl-specifier-seq [opt] member-declarator-list [opt] ;
14805 function-definition ; [opt]
14806 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14808 template-declaration
14810 member-declarator-list:
14812 member-declarator-list , member-declarator
14815 declarator pure-specifier [opt]
14816 declarator constant-initializer [opt]
14817 identifier [opt] : constant-expression
14821 member-declaration:
14822 __extension__ member-declaration
14825 declarator attributes [opt] pure-specifier [opt]
14826 declarator attributes [opt] constant-initializer [opt]
14827 identifier [opt] attributes [opt] : constant-expression
14831 member-declaration:
14832 static_assert-declaration */
14835 cp_parser_member_declaration (cp_parser* parser)
14837 cp_decl_specifier_seq decl_specifiers;
14838 tree prefix_attributes;
14840 int declares_class_or_enum;
14843 int saved_pedantic;
14845 /* Check for the `__extension__' keyword. */
14846 if (cp_parser_extension_opt (parser, &saved_pedantic))
14849 cp_parser_member_declaration (parser);
14850 /* Restore the old value of the PEDANTIC flag. */
14851 pedantic = saved_pedantic;
14856 /* Check for a template-declaration. */
14857 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14859 /* An explicit specialization here is an error condition, and we
14860 expect the specialization handler to detect and report this. */
14861 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14862 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14863 cp_parser_explicit_specialization (parser);
14865 cp_parser_template_declaration (parser, /*member_p=*/true);
14870 /* Check for a using-declaration. */
14871 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14873 /* Parse the using-declaration. */
14874 cp_parser_using_declaration (parser,
14875 /*access_declaration_p=*/false);
14879 /* Check for @defs. */
14880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14883 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14884 ivar = ivar_chains;
14888 ivar = TREE_CHAIN (member);
14889 TREE_CHAIN (member) = NULL_TREE;
14890 finish_member_declaration (member);
14895 /* If the next token is `static_assert' we have a static assertion. */
14896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14898 cp_parser_static_assert (parser, /*member_p=*/true);
14902 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14905 /* Parse the decl-specifier-seq. */
14906 cp_parser_decl_specifier_seq (parser,
14907 CP_PARSER_FLAGS_OPTIONAL,
14909 &declares_class_or_enum);
14910 prefix_attributes = decl_specifiers.attributes;
14911 decl_specifiers.attributes = NULL_TREE;
14912 /* Check for an invalid type-name. */
14913 if (!decl_specifiers.type
14914 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14916 /* If there is no declarator, then the decl-specifier-seq should
14918 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14920 /* If there was no decl-specifier-seq, and the next token is a
14921 `;', then we have something like:
14927 Each member-declaration shall declare at least one member
14928 name of the class. */
14929 if (!decl_specifiers.any_specifiers_p)
14931 cp_token *token = cp_lexer_peek_token (parser->lexer);
14932 if (pedantic && !token->in_system_header)
14933 pedwarn ("%Hextra %<;%>", &token->location);
14939 /* See if this declaration is a friend. */
14940 friend_p = cp_parser_friend_p (&decl_specifiers);
14941 /* If there were decl-specifiers, check to see if there was
14942 a class-declaration. */
14943 type = check_tag_decl (&decl_specifiers);
14944 /* Nested classes have already been added to the class, but
14945 a `friend' needs to be explicitly registered. */
14948 /* If the `friend' keyword was present, the friend must
14949 be introduced with a class-key. */
14950 if (!declares_class_or_enum)
14951 error ("a class-key must be used when declaring a friend");
14954 template <typename T> struct A {
14955 friend struct A<T>::B;
14958 A<T>::B will be represented by a TYPENAME_TYPE, and
14959 therefore not recognized by check_tag_decl. */
14961 && decl_specifiers.type
14962 && TYPE_P (decl_specifiers.type))
14963 type = decl_specifiers.type;
14964 if (!type || !TYPE_P (type))
14965 error ("friend declaration does not name a class or "
14968 make_friend_class (current_class_type, type,
14969 /*complain=*/true);
14971 /* If there is no TYPE, an error message will already have
14973 else if (!type || type == error_mark_node)
14975 /* An anonymous aggregate has to be handled specially; such
14976 a declaration really declares a data member (with a
14977 particular type), as opposed to a nested class. */
14978 else if (ANON_AGGR_TYPE_P (type))
14980 /* Remove constructors and such from TYPE, now that we
14981 know it is an anonymous aggregate. */
14982 fixup_anonymous_aggr (type);
14983 /* And make the corresponding data member. */
14984 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14985 /* Add it to the class. */
14986 finish_member_declaration (decl);
14989 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14994 /* See if these declarations will be friends. */
14995 friend_p = cp_parser_friend_p (&decl_specifiers);
14997 /* Keep going until we hit the `;' at the end of the
14999 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15001 tree attributes = NULL_TREE;
15002 tree first_attribute;
15004 /* Peek at the next token. */
15005 token = cp_lexer_peek_token (parser->lexer);
15007 /* Check for a bitfield declaration. */
15008 if (token->type == CPP_COLON
15009 || (token->type == CPP_NAME
15010 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15016 /* Get the name of the bitfield. Note that we cannot just
15017 check TOKEN here because it may have been invalidated by
15018 the call to cp_lexer_peek_nth_token above. */
15019 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15020 identifier = cp_parser_identifier (parser);
15022 identifier = NULL_TREE;
15024 /* Consume the `:' token. */
15025 cp_lexer_consume_token (parser->lexer);
15026 /* Get the width of the bitfield. */
15028 = cp_parser_constant_expression (parser,
15029 /*allow_non_constant=*/false,
15032 /* Look for attributes that apply to the bitfield. */
15033 attributes = cp_parser_attributes_opt (parser);
15034 /* Remember which attributes are prefix attributes and
15036 first_attribute = attributes;
15037 /* Combine the attributes. */
15038 attributes = chainon (prefix_attributes, attributes);
15040 /* Create the bitfield declaration. */
15041 decl = grokbitfield (identifier
15042 ? make_id_declarator (NULL_TREE,
15048 /* Apply the attributes. */
15049 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15053 cp_declarator *declarator;
15055 tree asm_specification;
15056 int ctor_dtor_or_conv_p;
15058 /* Parse the declarator. */
15060 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15061 &ctor_dtor_or_conv_p,
15062 /*parenthesized_p=*/NULL,
15063 /*member_p=*/true);
15065 /* If something went wrong parsing the declarator, make sure
15066 that we at least consume some tokens. */
15067 if (declarator == cp_error_declarator)
15069 /* Skip to the end of the statement. */
15070 cp_parser_skip_to_end_of_statement (parser);
15071 /* If the next token is not a semicolon, that is
15072 probably because we just skipped over the body of
15073 a function. So, we consume a semicolon if
15074 present, but do not issue an error message if it
15076 if (cp_lexer_next_token_is (parser->lexer,
15078 cp_lexer_consume_token (parser->lexer);
15082 if (declares_class_or_enum & 2)
15083 cp_parser_check_for_definition_in_return_type
15084 (declarator, decl_specifiers.type);
15086 /* Look for an asm-specification. */
15087 asm_specification = cp_parser_asm_specification_opt (parser);
15088 /* Look for attributes that apply to the declaration. */
15089 attributes = cp_parser_attributes_opt (parser);
15090 /* Remember which attributes are prefix attributes and
15092 first_attribute = attributes;
15093 /* Combine the attributes. */
15094 attributes = chainon (prefix_attributes, attributes);
15096 /* If it's an `=', then we have a constant-initializer or a
15097 pure-specifier. It is not correct to parse the
15098 initializer before registering the member declaration
15099 since the member declaration should be in scope while
15100 its initializer is processed. However, the rest of the
15101 front end does not yet provide an interface that allows
15102 us to handle this correctly. */
15103 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15107 A pure-specifier shall be used only in the declaration of
15108 a virtual function.
15110 A member-declarator can contain a constant-initializer
15111 only if it declares a static member of integral or
15114 Therefore, if the DECLARATOR is for a function, we look
15115 for a pure-specifier; otherwise, we look for a
15116 constant-initializer. When we call `grokfield', it will
15117 perform more stringent semantics checks. */
15118 if (function_declarator_p (declarator))
15119 initializer = cp_parser_pure_specifier (parser);
15121 /* Parse the initializer. */
15122 initializer = cp_parser_constant_initializer (parser);
15124 /* Otherwise, there is no initializer. */
15126 initializer = NULL_TREE;
15128 /* See if we are probably looking at a function
15129 definition. We are certainly not looking at a
15130 member-declarator. Calling `grokfield' has
15131 side-effects, so we must not do it unless we are sure
15132 that we are looking at a member-declarator. */
15133 if (cp_parser_token_starts_function_definition_p
15134 (cp_lexer_peek_token (parser->lexer)))
15136 /* The grammar does not allow a pure-specifier to be
15137 used when a member function is defined. (It is
15138 possible that this fact is an oversight in the
15139 standard, since a pure function may be defined
15140 outside of the class-specifier. */
15142 error ("pure-specifier on function-definition");
15143 decl = cp_parser_save_member_function_body (parser,
15147 /* If the member was not a friend, declare it here. */
15149 finish_member_declaration (decl);
15150 /* Peek at the next token. */
15151 token = cp_lexer_peek_token (parser->lexer);
15152 /* If the next token is a semicolon, consume it. */
15153 if (token->type == CPP_SEMICOLON)
15154 cp_lexer_consume_token (parser->lexer);
15158 /* Create the declaration. */
15159 decl = grokfield (declarator, &decl_specifiers,
15160 initializer, /*init_const_expr_p=*/true,
15165 /* Reset PREFIX_ATTRIBUTES. */
15166 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15167 attributes = TREE_CHAIN (attributes);
15169 TREE_CHAIN (attributes) = NULL_TREE;
15171 /* If there is any qualification still in effect, clear it
15172 now; we will be starting fresh with the next declarator. */
15173 parser->scope = NULL_TREE;
15174 parser->qualifying_scope = NULL_TREE;
15175 parser->object_scope = NULL_TREE;
15176 /* If it's a `,', then there are more declarators. */
15177 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15178 cp_lexer_consume_token (parser->lexer);
15179 /* If the next token isn't a `;', then we have a parse error. */
15180 else if (cp_lexer_next_token_is_not (parser->lexer,
15183 cp_parser_error (parser, "expected %<;%>");
15184 /* Skip tokens until we find a `;'. */
15185 cp_parser_skip_to_end_of_statement (parser);
15192 /* Add DECL to the list of members. */
15194 finish_member_declaration (decl);
15196 if (TREE_CODE (decl) == FUNCTION_DECL)
15197 cp_parser_save_default_args (parser, decl);
15202 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15205 /* Parse a pure-specifier.
15210 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15211 Otherwise, ERROR_MARK_NODE is returned. */
15214 cp_parser_pure_specifier (cp_parser* parser)
15218 /* Look for the `=' token. */
15219 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15220 return error_mark_node;
15221 /* Look for the `0' token. */
15222 token = cp_lexer_consume_token (parser->lexer);
15223 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15224 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15226 cp_parser_error (parser,
15227 "invalid pure specifier (only `= 0' is allowed)");
15228 cp_parser_skip_to_end_of_statement (parser);
15229 return error_mark_node;
15231 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15233 error ("templates may not be %<virtual%>");
15234 return error_mark_node;
15237 return integer_zero_node;
15240 /* Parse a constant-initializer.
15242 constant-initializer:
15243 = constant-expression
15245 Returns a representation of the constant-expression. */
15248 cp_parser_constant_initializer (cp_parser* parser)
15250 /* Look for the `=' token. */
15251 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15252 return error_mark_node;
15254 /* It is invalid to write:
15256 struct S { static const int i = { 7 }; };
15259 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15261 cp_parser_error (parser,
15262 "a brace-enclosed initializer is not allowed here");
15263 /* Consume the opening brace. */
15264 cp_lexer_consume_token (parser->lexer);
15265 /* Skip the initializer. */
15266 cp_parser_skip_to_closing_brace (parser);
15267 /* Look for the trailing `}'. */
15268 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15270 return error_mark_node;
15273 return cp_parser_constant_expression (parser,
15274 /*allow_non_constant=*/false,
15278 /* Derived classes [gram.class.derived] */
15280 /* Parse a base-clause.
15283 : base-specifier-list
15285 base-specifier-list:
15286 base-specifier ... [opt]
15287 base-specifier-list , base-specifier ... [opt]
15289 Returns a TREE_LIST representing the base-classes, in the order in
15290 which they were declared. The representation of each node is as
15291 described by cp_parser_base_specifier.
15293 In the case that no bases are specified, this function will return
15294 NULL_TREE, not ERROR_MARK_NODE. */
15297 cp_parser_base_clause (cp_parser* parser)
15299 tree bases = NULL_TREE;
15301 /* Look for the `:' that begins the list. */
15302 cp_parser_require (parser, CPP_COLON, "`:'");
15304 /* Scan the base-specifier-list. */
15309 bool pack_expansion_p = false;
15311 /* Look for the base-specifier. */
15312 base = cp_parser_base_specifier (parser);
15313 /* Look for the (optional) ellipsis. */
15314 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15316 /* Consume the `...'. */
15317 cp_lexer_consume_token (parser->lexer);
15319 pack_expansion_p = true;
15322 /* Add BASE to the front of the list. */
15323 if (base != error_mark_node)
15325 if (pack_expansion_p)
15326 /* Make this a pack expansion type. */
15327 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15330 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15332 TREE_CHAIN (base) = bases;
15336 /* Peek at the next token. */
15337 token = cp_lexer_peek_token (parser->lexer);
15338 /* If it's not a comma, then the list is complete. */
15339 if (token->type != CPP_COMMA)
15341 /* Consume the `,'. */
15342 cp_lexer_consume_token (parser->lexer);
15345 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15346 base class had a qualified name. However, the next name that
15347 appears is certainly not qualified. */
15348 parser->scope = NULL_TREE;
15349 parser->qualifying_scope = NULL_TREE;
15350 parser->object_scope = NULL_TREE;
15352 return nreverse (bases);
15355 /* Parse a base-specifier.
15358 :: [opt] nested-name-specifier [opt] class-name
15359 virtual access-specifier [opt] :: [opt] nested-name-specifier
15361 access-specifier virtual [opt] :: [opt] nested-name-specifier
15364 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15365 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15366 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15367 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15370 cp_parser_base_specifier (cp_parser* parser)
15374 bool virtual_p = false;
15375 bool duplicate_virtual_error_issued_p = false;
15376 bool duplicate_access_error_issued_p = false;
15377 bool class_scope_p, template_p;
15378 tree access = access_default_node;
15381 /* Process the optional `virtual' and `access-specifier'. */
15384 /* Peek at the next token. */
15385 token = cp_lexer_peek_token (parser->lexer);
15386 /* Process `virtual'. */
15387 switch (token->keyword)
15390 /* If `virtual' appears more than once, issue an error. */
15391 if (virtual_p && !duplicate_virtual_error_issued_p)
15393 cp_parser_error (parser,
15394 "%<virtual%> specified more than once in base-specified");
15395 duplicate_virtual_error_issued_p = true;
15400 /* Consume the `virtual' token. */
15401 cp_lexer_consume_token (parser->lexer);
15406 case RID_PROTECTED:
15408 /* If more than one access specifier appears, issue an
15410 if (access != access_default_node
15411 && !duplicate_access_error_issued_p)
15413 cp_parser_error (parser,
15414 "more than one access specifier in base-specified");
15415 duplicate_access_error_issued_p = true;
15418 access = ridpointers[(int) token->keyword];
15420 /* Consume the access-specifier. */
15421 cp_lexer_consume_token (parser->lexer);
15430 /* It is not uncommon to see programs mechanically, erroneously, use
15431 the 'typename' keyword to denote (dependent) qualified types
15432 as base classes. */
15433 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15435 if (!processing_template_decl)
15436 error ("keyword %<typename%> not allowed outside of templates");
15438 error ("keyword %<typename%> not allowed in this context "
15439 "(the base class is implicitly a type)");
15440 cp_lexer_consume_token (parser->lexer);
15443 /* Look for the optional `::' operator. */
15444 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15445 /* Look for the nested-name-specifier. The simplest way to
15450 The keyword `typename' is not permitted in a base-specifier or
15451 mem-initializer; in these contexts a qualified name that
15452 depends on a template-parameter is implicitly assumed to be a
15455 is to pretend that we have seen the `typename' keyword at this
15457 cp_parser_nested_name_specifier_opt (parser,
15458 /*typename_keyword_p=*/true,
15459 /*check_dependency_p=*/true,
15461 /*is_declaration=*/true);
15462 /* If the base class is given by a qualified name, assume that names
15463 we see are type names or templates, as appropriate. */
15464 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15465 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15467 /* Finally, look for the class-name. */
15468 type = cp_parser_class_name (parser,
15472 /*check_dependency_p=*/true,
15473 /*class_head_p=*/false,
15474 /*is_declaration=*/true);
15476 if (type == error_mark_node)
15477 return error_mark_node;
15479 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15482 /* Exception handling [gram.exception] */
15484 /* Parse an (optional) exception-specification.
15486 exception-specification:
15487 throw ( type-id-list [opt] )
15489 Returns a TREE_LIST representing the exception-specification. The
15490 TREE_VALUE of each node is a type. */
15493 cp_parser_exception_specification_opt (cp_parser* parser)
15498 /* Peek at the next token. */
15499 token = cp_lexer_peek_token (parser->lexer);
15500 /* If it's not `throw', then there's no exception-specification. */
15501 if (!cp_parser_is_keyword (token, RID_THROW))
15504 /* Consume the `throw'. */
15505 cp_lexer_consume_token (parser->lexer);
15507 /* Look for the `('. */
15508 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15510 /* Peek at the next token. */
15511 token = cp_lexer_peek_token (parser->lexer);
15512 /* If it's not a `)', then there is a type-id-list. */
15513 if (token->type != CPP_CLOSE_PAREN)
15515 const char *saved_message;
15517 /* Types may not be defined in an exception-specification. */
15518 saved_message = parser->type_definition_forbidden_message;
15519 parser->type_definition_forbidden_message
15520 = "types may not be defined in an exception-specification";
15521 /* Parse the type-id-list. */
15522 type_id_list = cp_parser_type_id_list (parser);
15523 /* Restore the saved message. */
15524 parser->type_definition_forbidden_message = saved_message;
15527 type_id_list = empty_except_spec;
15529 /* Look for the `)'. */
15530 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15532 return type_id_list;
15535 /* Parse an (optional) type-id-list.
15539 type-id-list , type-id ... [opt]
15541 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15542 in the order that the types were presented. */
15545 cp_parser_type_id_list (cp_parser* parser)
15547 tree types = NULL_TREE;
15554 /* Get the next type-id. */
15555 type = cp_parser_type_id (parser);
15556 /* Parse the optional ellipsis. */
15557 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15559 /* Consume the `...'. */
15560 cp_lexer_consume_token (parser->lexer);
15562 /* Turn the type into a pack expansion expression. */
15563 type = make_pack_expansion (type);
15565 /* Add it to the list. */
15566 types = add_exception_specifier (types, type, /*complain=*/1);
15567 /* Peek at the next token. */
15568 token = cp_lexer_peek_token (parser->lexer);
15569 /* If it is not a `,', we are done. */
15570 if (token->type != CPP_COMMA)
15572 /* Consume the `,'. */
15573 cp_lexer_consume_token (parser->lexer);
15576 return nreverse (types);
15579 /* Parse a try-block.
15582 try compound-statement handler-seq */
15585 cp_parser_try_block (cp_parser* parser)
15589 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15590 try_block = begin_try_block ();
15591 cp_parser_compound_statement (parser, NULL, true);
15592 finish_try_block (try_block);
15593 cp_parser_handler_seq (parser);
15594 finish_handler_sequence (try_block);
15599 /* Parse a function-try-block.
15601 function-try-block:
15602 try ctor-initializer [opt] function-body handler-seq */
15605 cp_parser_function_try_block (cp_parser* parser)
15607 tree compound_stmt;
15609 bool ctor_initializer_p;
15611 /* Look for the `try' keyword. */
15612 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15614 /* Let the rest of the front end know where we are. */
15615 try_block = begin_function_try_block (&compound_stmt);
15616 /* Parse the function-body. */
15618 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15619 /* We're done with the `try' part. */
15620 finish_function_try_block (try_block);
15621 /* Parse the handlers. */
15622 cp_parser_handler_seq (parser);
15623 /* We're done with the handlers. */
15624 finish_function_handler_sequence (try_block, compound_stmt);
15626 return ctor_initializer_p;
15629 /* Parse a handler-seq.
15632 handler handler-seq [opt] */
15635 cp_parser_handler_seq (cp_parser* parser)
15641 /* Parse the handler. */
15642 cp_parser_handler (parser);
15643 /* Peek at the next token. */
15644 token = cp_lexer_peek_token (parser->lexer);
15645 /* If it's not `catch' then there are no more handlers. */
15646 if (!cp_parser_is_keyword (token, RID_CATCH))
15651 /* Parse a handler.
15654 catch ( exception-declaration ) compound-statement */
15657 cp_parser_handler (cp_parser* parser)
15662 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15663 handler = begin_handler ();
15664 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15665 declaration = cp_parser_exception_declaration (parser);
15666 finish_handler_parms (declaration, handler);
15667 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15668 cp_parser_compound_statement (parser, NULL, false);
15669 finish_handler (handler);
15672 /* Parse an exception-declaration.
15674 exception-declaration:
15675 type-specifier-seq declarator
15676 type-specifier-seq abstract-declarator
15680 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15681 ellipsis variant is used. */
15684 cp_parser_exception_declaration (cp_parser* parser)
15686 cp_decl_specifier_seq type_specifiers;
15687 cp_declarator *declarator;
15688 const char *saved_message;
15690 /* If it's an ellipsis, it's easy to handle. */
15691 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15693 /* Consume the `...' token. */
15694 cp_lexer_consume_token (parser->lexer);
15698 /* Types may not be defined in exception-declarations. */
15699 saved_message = parser->type_definition_forbidden_message;
15700 parser->type_definition_forbidden_message
15701 = "types may not be defined in exception-declarations";
15703 /* Parse the type-specifier-seq. */
15704 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15706 /* If it's a `)', then there is no declarator. */
15707 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15710 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15711 /*ctor_dtor_or_conv_p=*/NULL,
15712 /*parenthesized_p=*/NULL,
15713 /*member_p=*/false);
15715 /* Restore the saved message. */
15716 parser->type_definition_forbidden_message = saved_message;
15718 if (!type_specifiers.any_specifiers_p)
15719 return error_mark_node;
15721 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15724 /* Parse a throw-expression.
15727 throw assignment-expression [opt]
15729 Returns a THROW_EXPR representing the throw-expression. */
15732 cp_parser_throw_expression (cp_parser* parser)
15737 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15738 token = cp_lexer_peek_token (parser->lexer);
15739 /* Figure out whether or not there is an assignment-expression
15740 following the "throw" keyword. */
15741 if (token->type == CPP_COMMA
15742 || token->type == CPP_SEMICOLON
15743 || token->type == CPP_CLOSE_PAREN
15744 || token->type == CPP_CLOSE_SQUARE
15745 || token->type == CPP_CLOSE_BRACE
15746 || token->type == CPP_COLON)
15747 expression = NULL_TREE;
15749 expression = cp_parser_assignment_expression (parser,
15752 return build_throw (expression);
15755 /* GNU Extensions */
15757 /* Parse an (optional) asm-specification.
15760 asm ( string-literal )
15762 If the asm-specification is present, returns a STRING_CST
15763 corresponding to the string-literal. Otherwise, returns
15767 cp_parser_asm_specification_opt (cp_parser* parser)
15770 tree asm_specification;
15772 /* Peek at the next token. */
15773 token = cp_lexer_peek_token (parser->lexer);
15774 /* If the next token isn't the `asm' keyword, then there's no
15775 asm-specification. */
15776 if (!cp_parser_is_keyword (token, RID_ASM))
15779 /* Consume the `asm' token. */
15780 cp_lexer_consume_token (parser->lexer);
15781 /* Look for the `('. */
15782 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15784 /* Look for the string-literal. */
15785 asm_specification = cp_parser_string_literal (parser, false, false);
15787 /* Look for the `)'. */
15788 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15790 return asm_specification;
15793 /* Parse an asm-operand-list.
15797 asm-operand-list , asm-operand
15800 string-literal ( expression )
15801 [ string-literal ] string-literal ( expression )
15803 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15804 each node is the expression. The TREE_PURPOSE is itself a
15805 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15806 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15807 is a STRING_CST for the string literal before the parenthesis. Returns
15808 ERROR_MARK_NODE if any of the operands are invalid. */
15811 cp_parser_asm_operand_list (cp_parser* parser)
15813 tree asm_operands = NULL_TREE;
15814 bool invalid_operands = false;
15818 tree string_literal;
15822 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15824 /* Consume the `[' token. */
15825 cp_lexer_consume_token (parser->lexer);
15826 /* Read the operand name. */
15827 name = cp_parser_identifier (parser);
15828 if (name != error_mark_node)
15829 name = build_string (IDENTIFIER_LENGTH (name),
15830 IDENTIFIER_POINTER (name));
15831 /* Look for the closing `]'. */
15832 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15836 /* Look for the string-literal. */
15837 string_literal = cp_parser_string_literal (parser, false, false);
15839 /* Look for the `('. */
15840 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15841 /* Parse the expression. */
15842 expression = cp_parser_expression (parser, /*cast_p=*/false);
15843 /* Look for the `)'. */
15844 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15846 if (name == error_mark_node
15847 || string_literal == error_mark_node
15848 || expression == error_mark_node)
15849 invalid_operands = true;
15851 /* Add this operand to the list. */
15852 asm_operands = tree_cons (build_tree_list (name, string_literal),
15855 /* If the next token is not a `,', there are no more
15857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15859 /* Consume the `,'. */
15860 cp_lexer_consume_token (parser->lexer);
15863 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15866 /* Parse an asm-clobber-list.
15870 asm-clobber-list , string-literal
15872 Returns a TREE_LIST, indicating the clobbers in the order that they
15873 appeared. The TREE_VALUE of each node is a STRING_CST. */
15876 cp_parser_asm_clobber_list (cp_parser* parser)
15878 tree clobbers = NULL_TREE;
15882 tree string_literal;
15884 /* Look for the string literal. */
15885 string_literal = cp_parser_string_literal (parser, false, false);
15886 /* Add it to the list. */
15887 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15888 /* If the next token is not a `,', then the list is
15890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15892 /* Consume the `,' token. */
15893 cp_lexer_consume_token (parser->lexer);
15899 /* Parse an (optional) series of attributes.
15902 attributes attribute
15905 __attribute__ (( attribute-list [opt] ))
15907 The return value is as for cp_parser_attribute_list. */
15910 cp_parser_attributes_opt (cp_parser* parser)
15912 tree attributes = NULL_TREE;
15917 tree attribute_list;
15919 /* Peek at the next token. */
15920 token = cp_lexer_peek_token (parser->lexer);
15921 /* If it's not `__attribute__', then we're done. */
15922 if (token->keyword != RID_ATTRIBUTE)
15925 /* Consume the `__attribute__' keyword. */
15926 cp_lexer_consume_token (parser->lexer);
15927 /* Look for the two `(' tokens. */
15928 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15929 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15931 /* Peek at the next token. */
15932 token = cp_lexer_peek_token (parser->lexer);
15933 if (token->type != CPP_CLOSE_PAREN)
15934 /* Parse the attribute-list. */
15935 attribute_list = cp_parser_attribute_list (parser);
15937 /* If the next token is a `)', then there is no attribute
15939 attribute_list = NULL;
15941 /* Look for the two `)' tokens. */
15942 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15943 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15945 /* Add these new attributes to the list. */
15946 attributes = chainon (attributes, attribute_list);
15952 /* Parse an attribute-list.
15956 attribute-list , attribute
15960 identifier ( identifier )
15961 identifier ( identifier , expression-list )
15962 identifier ( expression-list )
15964 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15965 to an attribute. The TREE_PURPOSE of each node is the identifier
15966 indicating which attribute is in use. The TREE_VALUE represents
15967 the arguments, if any. */
15970 cp_parser_attribute_list (cp_parser* parser)
15972 tree attribute_list = NULL_TREE;
15973 bool save_translate_strings_p = parser->translate_strings_p;
15975 parser->translate_strings_p = false;
15982 /* Look for the identifier. We also allow keywords here; for
15983 example `__attribute__ ((const))' is legal. */
15984 token = cp_lexer_peek_token (parser->lexer);
15985 if (token->type == CPP_NAME
15986 || token->type == CPP_KEYWORD)
15988 tree arguments = NULL_TREE;
15990 /* Consume the token. */
15991 token = cp_lexer_consume_token (parser->lexer);
15993 /* Save away the identifier that indicates which attribute
15995 identifier = token->u.value;
15996 attribute = build_tree_list (identifier, NULL_TREE);
15998 /* Peek at the next token. */
15999 token = cp_lexer_peek_token (parser->lexer);
16000 /* If it's an `(', then parse the attribute arguments. */
16001 if (token->type == CPP_OPEN_PAREN)
16003 arguments = cp_parser_parenthesized_expression_list
16004 (parser, true, /*cast_p=*/false,
16005 /*allow_expansion_p=*/false,
16006 /*non_constant_p=*/NULL);
16007 /* Save the arguments away. */
16008 TREE_VALUE (attribute) = arguments;
16011 if (arguments != error_mark_node)
16013 /* Add this attribute to the list. */
16014 TREE_CHAIN (attribute) = attribute_list;
16015 attribute_list = attribute;
16018 token = cp_lexer_peek_token (parser->lexer);
16020 /* Now, look for more attributes. If the next token isn't a
16021 `,', we're done. */
16022 if (token->type != CPP_COMMA)
16025 /* Consume the comma and keep going. */
16026 cp_lexer_consume_token (parser->lexer);
16028 parser->translate_strings_p = save_translate_strings_p;
16030 /* We built up the list in reverse order. */
16031 return nreverse (attribute_list);
16034 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16035 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16036 current value of the PEDANTIC flag, regardless of whether or not
16037 the `__extension__' keyword is present. The caller is responsible
16038 for restoring the value of the PEDANTIC flag. */
16041 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16043 /* Save the old value of the PEDANTIC flag. */
16044 *saved_pedantic = pedantic;
16046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16048 /* Consume the `__extension__' token. */
16049 cp_lexer_consume_token (parser->lexer);
16050 /* We're not being pedantic while the `__extension__' keyword is
16060 /* Parse a label declaration.
16063 __label__ label-declarator-seq ;
16065 label-declarator-seq:
16066 identifier , label-declarator-seq
16070 cp_parser_label_declaration (cp_parser* parser)
16072 /* Look for the `__label__' keyword. */
16073 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16079 /* Look for an identifier. */
16080 identifier = cp_parser_identifier (parser);
16081 /* If we failed, stop. */
16082 if (identifier == error_mark_node)
16084 /* Declare it as a label. */
16085 finish_label_decl (identifier);
16086 /* If the next token is a `;', stop. */
16087 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16089 /* Look for the `,' separating the label declarations. */
16090 cp_parser_require (parser, CPP_COMMA, "`,'");
16093 /* Look for the final `;'. */
16094 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16097 /* Support Functions */
16099 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16100 NAME should have one of the representations used for an
16101 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16102 is returned. If PARSER->SCOPE is a dependent type, then a
16103 SCOPE_REF is returned.
16105 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16106 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16107 was formed. Abstractly, such entities should not be passed to this
16108 function, because they do not need to be looked up, but it is
16109 simpler to check for this special case here, rather than at the
16112 In cases not explicitly covered above, this function returns a
16113 DECL, OVERLOAD, or baselink representing the result of the lookup.
16114 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16117 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16118 (e.g., "struct") that was used. In that case bindings that do not
16119 refer to types are ignored.
16121 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16124 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16127 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16130 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16131 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16132 NULL_TREE otherwise. */
16135 cp_parser_lookup_name (cp_parser *parser, tree name,
16136 enum tag_types tag_type,
16139 bool check_dependency,
16140 tree *ambiguous_decls)
16144 tree object_type = parser->context->object_type;
16146 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16147 flags |= LOOKUP_COMPLAIN;
16149 /* Assume that the lookup will be unambiguous. */
16150 if (ambiguous_decls)
16151 *ambiguous_decls = NULL_TREE;
16153 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16154 no longer valid. Note that if we are parsing tentatively, and
16155 the parse fails, OBJECT_TYPE will be automatically restored. */
16156 parser->context->object_type = NULL_TREE;
16158 if (name == error_mark_node)
16159 return error_mark_node;
16161 /* A template-id has already been resolved; there is no lookup to
16163 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16165 if (BASELINK_P (name))
16167 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16168 == TEMPLATE_ID_EXPR);
16172 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16173 it should already have been checked to make sure that the name
16174 used matches the type being destroyed. */
16175 if (TREE_CODE (name) == BIT_NOT_EXPR)
16179 /* Figure out to which type this destructor applies. */
16181 type = parser->scope;
16182 else if (object_type)
16183 type = object_type;
16185 type = current_class_type;
16186 /* If that's not a class type, there is no destructor. */
16187 if (!type || !CLASS_TYPE_P (type))
16188 return error_mark_node;
16189 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16190 lazily_declare_fn (sfk_destructor, type);
16191 if (!CLASSTYPE_DESTRUCTORS (type))
16192 return error_mark_node;
16193 /* If it was a class type, return the destructor. */
16194 return CLASSTYPE_DESTRUCTORS (type);
16197 /* By this point, the NAME should be an ordinary identifier. If
16198 the id-expression was a qualified name, the qualifying scope is
16199 stored in PARSER->SCOPE at this point. */
16200 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16202 /* Perform the lookup. */
16207 if (parser->scope == error_mark_node)
16208 return error_mark_node;
16210 /* If the SCOPE is dependent, the lookup must be deferred until
16211 the template is instantiated -- unless we are explicitly
16212 looking up names in uninstantiated templates. Even then, we
16213 cannot look up the name if the scope is not a class type; it
16214 might, for example, be a template type parameter. */
16215 dependent_p = (TYPE_P (parser->scope)
16216 && !(parser->in_declarator_p
16217 && currently_open_class (parser->scope))
16218 && dependent_type_p (parser->scope));
16219 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16226 /* The resolution to Core Issue 180 says that `struct
16227 A::B' should be considered a type-name, even if `A'
16229 type = make_typename_type (parser->scope, name, tag_type,
16230 /*complain=*/tf_error);
16231 decl = TYPE_NAME (type);
16233 else if (is_template
16234 && (cp_parser_next_token_ends_template_argument_p (parser)
16235 || cp_lexer_next_token_is (parser->lexer,
16237 decl = make_unbound_class_template (parser->scope,
16239 /*complain=*/tf_error);
16241 decl = build_qualified_name (/*type=*/NULL_TREE,
16242 parser->scope, name,
16247 tree pushed_scope = NULL_TREE;
16249 /* If PARSER->SCOPE is a dependent type, then it must be a
16250 class type, and we must not be checking dependencies;
16251 otherwise, we would have processed this lookup above. So
16252 that PARSER->SCOPE is not considered a dependent base by
16253 lookup_member, we must enter the scope here. */
16255 pushed_scope = push_scope (parser->scope);
16256 /* If the PARSER->SCOPE is a template specialization, it
16257 may be instantiated during name lookup. In that case,
16258 errors may be issued. Even if we rollback the current
16259 tentative parse, those errors are valid. */
16260 decl = lookup_qualified_name (parser->scope, name,
16261 tag_type != none_type,
16262 /*complain=*/true);
16264 pop_scope (pushed_scope);
16266 parser->qualifying_scope = parser->scope;
16267 parser->object_scope = NULL_TREE;
16269 else if (object_type)
16271 tree object_decl = NULL_TREE;
16272 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16273 OBJECT_TYPE is not a class. */
16274 if (CLASS_TYPE_P (object_type))
16275 /* If the OBJECT_TYPE is a template specialization, it may
16276 be instantiated during name lookup. In that case, errors
16277 may be issued. Even if we rollback the current tentative
16278 parse, those errors are valid. */
16279 object_decl = lookup_member (object_type,
16282 tag_type != none_type);
16283 /* Look it up in the enclosing context, too. */
16284 decl = lookup_name_real (name, tag_type != none_type,
16286 /*block_p=*/true, is_namespace, flags);
16287 parser->object_scope = object_type;
16288 parser->qualifying_scope = NULL_TREE;
16290 decl = object_decl;
16294 decl = lookup_name_real (name, tag_type != none_type,
16296 /*block_p=*/true, is_namespace, flags);
16297 parser->qualifying_scope = NULL_TREE;
16298 parser->object_scope = NULL_TREE;
16301 /* If the lookup failed, let our caller know. */
16302 if (!decl || decl == error_mark_node)
16303 return error_mark_node;
16305 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16306 if (TREE_CODE (decl) == TREE_LIST)
16308 if (ambiguous_decls)
16309 *ambiguous_decls = decl;
16310 /* The error message we have to print is too complicated for
16311 cp_parser_error, so we incorporate its actions directly. */
16312 if (!cp_parser_simulate_error (parser))
16314 error ("reference to %qD is ambiguous", name);
16315 print_candidates (decl);
16317 return error_mark_node;
16320 gcc_assert (DECL_P (decl)
16321 || TREE_CODE (decl) == OVERLOAD
16322 || TREE_CODE (decl) == SCOPE_REF
16323 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16324 || BASELINK_P (decl));
16326 /* If we have resolved the name of a member declaration, check to
16327 see if the declaration is accessible. When the name resolves to
16328 set of overloaded functions, accessibility is checked when
16329 overload resolution is done.
16331 During an explicit instantiation, access is not checked at all,
16332 as per [temp.explicit]. */
16334 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16339 /* Like cp_parser_lookup_name, but for use in the typical case where
16340 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16341 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16344 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16346 return cp_parser_lookup_name (parser, name,
16348 /*is_template=*/false,
16349 /*is_namespace=*/false,
16350 /*check_dependency=*/true,
16351 /*ambiguous_decls=*/NULL);
16354 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16355 the current context, return the TYPE_DECL. If TAG_NAME_P is
16356 true, the DECL indicates the class being defined in a class-head,
16357 or declared in an elaborated-type-specifier.
16359 Otherwise, return DECL. */
16362 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16364 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16365 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16368 template <typename T> struct B;
16371 template <typename T> struct A::B {};
16373 Similarly, in an elaborated-type-specifier:
16375 namespace N { struct X{}; }
16378 template <typename T> friend struct N::X;
16381 However, if the DECL refers to a class type, and we are in
16382 the scope of the class, then the name lookup automatically
16383 finds the TYPE_DECL created by build_self_reference rather
16384 than a TEMPLATE_DECL. For example, in:
16386 template <class T> struct S {
16390 there is no need to handle such case. */
16392 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16393 return DECL_TEMPLATE_RESULT (decl);
16398 /* If too many, or too few, template-parameter lists apply to the
16399 declarator, issue an error message. Returns TRUE if all went well,
16400 and FALSE otherwise. */
16403 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16404 cp_declarator *declarator)
16406 unsigned num_templates;
16408 /* We haven't seen any classes that involve template parameters yet. */
16411 switch (declarator->kind)
16414 if (declarator->u.id.qualifying_scope)
16419 scope = declarator->u.id.qualifying_scope;
16420 member = declarator->u.id.unqualified_name;
16422 while (scope && CLASS_TYPE_P (scope))
16424 /* You're supposed to have one `template <...>'
16425 for every template class, but you don't need one
16426 for a full specialization. For example:
16428 template <class T> struct S{};
16429 template <> struct S<int> { void f(); };
16430 void S<int>::f () {}
16432 is correct; there shouldn't be a `template <>' for
16433 the definition of `S<int>::f'. */
16434 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16435 /* If SCOPE does not have template information of any
16436 kind, then it is not a template, nor is it nested
16437 within a template. */
16439 if (explicit_class_specialization_p (scope))
16441 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16444 scope = TYPE_CONTEXT (scope);
16447 else if (TREE_CODE (declarator->u.id.unqualified_name)
16448 == TEMPLATE_ID_EXPR)
16449 /* If the DECLARATOR has the form `X<y>' then it uses one
16450 additional level of template parameters. */
16453 return cp_parser_check_template_parameters (parser,
16459 case cdk_reference:
16461 return (cp_parser_check_declarator_template_parameters
16462 (parser, declarator->declarator));
16468 gcc_unreachable ();
16473 /* NUM_TEMPLATES were used in the current declaration. If that is
16474 invalid, return FALSE and issue an error messages. Otherwise,
16478 cp_parser_check_template_parameters (cp_parser* parser,
16479 unsigned num_templates)
16481 /* If there are more template classes than parameter lists, we have
16484 template <class T> void S<T>::R<T>::f (); */
16485 if (parser->num_template_parameter_lists < num_templates)
16487 error ("too few template-parameter-lists");
16490 /* If there are the same number of template classes and parameter
16491 lists, that's OK. */
16492 if (parser->num_template_parameter_lists == num_templates)
16494 /* If there are more, but only one more, then we are referring to a
16495 member template. That's OK too. */
16496 if (parser->num_template_parameter_lists == num_templates + 1)
16498 /* Otherwise, there are too many template parameter lists. We have
16501 template <class T> template <class U> void S::f(); */
16502 error ("too many template-parameter-lists");
16506 /* Parse an optional `::' token indicating that the following name is
16507 from the global namespace. If so, PARSER->SCOPE is set to the
16508 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16509 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16510 Returns the new value of PARSER->SCOPE, if the `::' token is
16511 present, and NULL_TREE otherwise. */
16514 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16518 /* Peek at the next token. */
16519 token = cp_lexer_peek_token (parser->lexer);
16520 /* If we're looking at a `::' token then we're starting from the
16521 global namespace, not our current location. */
16522 if (token->type == CPP_SCOPE)
16524 /* Consume the `::' token. */
16525 cp_lexer_consume_token (parser->lexer);
16526 /* Set the SCOPE so that we know where to start the lookup. */
16527 parser->scope = global_namespace;
16528 parser->qualifying_scope = global_namespace;
16529 parser->object_scope = NULL_TREE;
16531 return parser->scope;
16533 else if (!current_scope_valid_p)
16535 parser->scope = NULL_TREE;
16536 parser->qualifying_scope = NULL_TREE;
16537 parser->object_scope = NULL_TREE;
16543 /* Returns TRUE if the upcoming token sequence is the start of a
16544 constructor declarator. If FRIEND_P is true, the declarator is
16545 preceded by the `friend' specifier. */
16548 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16550 bool constructor_p;
16551 tree type_decl = NULL_TREE;
16552 bool nested_name_p;
16553 cp_token *next_token;
16555 /* The common case is that this is not a constructor declarator, so
16556 try to avoid doing lots of work if at all possible. It's not
16557 valid declare a constructor at function scope. */
16558 if (parser->in_function_body)
16560 /* And only certain tokens can begin a constructor declarator. */
16561 next_token = cp_lexer_peek_token (parser->lexer);
16562 if (next_token->type != CPP_NAME
16563 && next_token->type != CPP_SCOPE
16564 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16565 && next_token->type != CPP_TEMPLATE_ID)
16568 /* Parse tentatively; we are going to roll back all of the tokens
16570 cp_parser_parse_tentatively (parser);
16571 /* Assume that we are looking at a constructor declarator. */
16572 constructor_p = true;
16574 /* Look for the optional `::' operator. */
16575 cp_parser_global_scope_opt (parser,
16576 /*current_scope_valid_p=*/false);
16577 /* Look for the nested-name-specifier. */
16579 = (cp_parser_nested_name_specifier_opt (parser,
16580 /*typename_keyword_p=*/false,
16581 /*check_dependency_p=*/false,
16583 /*is_declaration=*/false)
16585 /* Outside of a class-specifier, there must be a
16586 nested-name-specifier. */
16587 if (!nested_name_p &&
16588 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16590 constructor_p = false;
16591 /* If we still think that this might be a constructor-declarator,
16592 look for a class-name. */
16597 template <typename T> struct S { S(); };
16598 template <typename T> S<T>::S ();
16600 we must recognize that the nested `S' names a class.
16603 template <typename T> S<T>::S<T> ();
16605 we must recognize that the nested `S' names a template. */
16606 type_decl = cp_parser_class_name (parser,
16607 /*typename_keyword_p=*/false,
16608 /*template_keyword_p=*/false,
16610 /*check_dependency_p=*/false,
16611 /*class_head_p=*/false,
16612 /*is_declaration=*/false);
16613 /* If there was no class-name, then this is not a constructor. */
16614 constructor_p = !cp_parser_error_occurred (parser);
16617 /* If we're still considering a constructor, we have to see a `(',
16618 to begin the parameter-declaration-clause, followed by either a
16619 `)', an `...', or a decl-specifier. We need to check for a
16620 type-specifier to avoid being fooled into thinking that:
16624 is a constructor. (It is actually a function named `f' that
16625 takes one parameter (of type `int') and returns a value of type
16628 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16631 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16632 /* A parameter declaration begins with a decl-specifier,
16633 which is either the "attribute" keyword, a storage class
16634 specifier, or (usually) a type-specifier. */
16635 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16638 tree pushed_scope = NULL_TREE;
16639 unsigned saved_num_template_parameter_lists;
16641 /* Names appearing in the type-specifier should be looked up
16642 in the scope of the class. */
16643 if (current_class_type)
16647 type = TREE_TYPE (type_decl);
16648 if (TREE_CODE (type) == TYPENAME_TYPE)
16650 type = resolve_typename_type (type,
16651 /*only_current_p=*/false);
16652 if (TREE_CODE (type) == TYPENAME_TYPE)
16654 cp_parser_abort_tentative_parse (parser);
16658 pushed_scope = push_scope (type);
16661 /* Inside the constructor parameter list, surrounding
16662 template-parameter-lists do not apply. */
16663 saved_num_template_parameter_lists
16664 = parser->num_template_parameter_lists;
16665 parser->num_template_parameter_lists = 0;
16667 /* Look for the type-specifier. */
16668 cp_parser_type_specifier (parser,
16669 CP_PARSER_FLAGS_NONE,
16670 /*decl_specs=*/NULL,
16671 /*is_declarator=*/true,
16672 /*declares_class_or_enum=*/NULL,
16673 /*is_cv_qualifier=*/NULL);
16675 parser->num_template_parameter_lists
16676 = saved_num_template_parameter_lists;
16678 /* Leave the scope of the class. */
16680 pop_scope (pushed_scope);
16682 constructor_p = !cp_parser_error_occurred (parser);
16686 constructor_p = false;
16687 /* We did not really want to consume any tokens. */
16688 cp_parser_abort_tentative_parse (parser);
16690 return constructor_p;
16693 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16694 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16695 they must be performed once we are in the scope of the function.
16697 Returns the function defined. */
16700 cp_parser_function_definition_from_specifiers_and_declarator
16701 (cp_parser* parser,
16702 cp_decl_specifier_seq *decl_specifiers,
16704 const cp_declarator *declarator)
16709 /* Begin the function-definition. */
16710 success_p = start_function (decl_specifiers, declarator, attributes);
16712 /* The things we're about to see are not directly qualified by any
16713 template headers we've seen thus far. */
16714 reset_specialization ();
16716 /* If there were names looked up in the decl-specifier-seq that we
16717 did not check, check them now. We must wait until we are in the
16718 scope of the function to perform the checks, since the function
16719 might be a friend. */
16720 perform_deferred_access_checks ();
16724 /* Skip the entire function. */
16725 cp_parser_skip_to_end_of_block_or_statement (parser);
16726 fn = error_mark_node;
16728 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16730 /* Seen already, skip it. An error message has already been output. */
16731 cp_parser_skip_to_end_of_block_or_statement (parser);
16732 fn = current_function_decl;
16733 current_function_decl = NULL_TREE;
16734 /* If this is a function from a class, pop the nested class. */
16735 if (current_class_name)
16736 pop_nested_class ();
16739 fn = cp_parser_function_definition_after_declarator (parser,
16740 /*inline_p=*/false);
16745 /* Parse the part of a function-definition that follows the
16746 declarator. INLINE_P is TRUE iff this function is an inline
16747 function defined with a class-specifier.
16749 Returns the function defined. */
16752 cp_parser_function_definition_after_declarator (cp_parser* parser,
16756 bool ctor_initializer_p = false;
16757 bool saved_in_unbraced_linkage_specification_p;
16758 bool saved_in_function_body;
16759 unsigned saved_num_template_parameter_lists;
16761 saved_in_function_body = parser->in_function_body;
16762 parser->in_function_body = true;
16763 /* If the next token is `return', then the code may be trying to
16764 make use of the "named return value" extension that G++ used to
16766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16768 /* Consume the `return' keyword. */
16769 cp_lexer_consume_token (parser->lexer);
16770 /* Look for the identifier that indicates what value is to be
16772 cp_parser_identifier (parser);
16773 /* Issue an error message. */
16774 error ("named return values are no longer supported");
16775 /* Skip tokens until we reach the start of the function body. */
16778 cp_token *token = cp_lexer_peek_token (parser->lexer);
16779 if (token->type == CPP_OPEN_BRACE
16780 || token->type == CPP_EOF
16781 || token->type == CPP_PRAGMA_EOL)
16783 cp_lexer_consume_token (parser->lexer);
16786 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16787 anything declared inside `f'. */
16788 saved_in_unbraced_linkage_specification_p
16789 = parser->in_unbraced_linkage_specification_p;
16790 parser->in_unbraced_linkage_specification_p = false;
16791 /* Inside the function, surrounding template-parameter-lists do not
16793 saved_num_template_parameter_lists
16794 = parser->num_template_parameter_lists;
16795 parser->num_template_parameter_lists = 0;
16796 /* If the next token is `try', then we are looking at a
16797 function-try-block. */
16798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16799 ctor_initializer_p = cp_parser_function_try_block (parser);
16800 /* A function-try-block includes the function-body, so we only do
16801 this next part if we're not processing a function-try-block. */
16804 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16806 /* Finish the function. */
16807 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16808 (inline_p ? 2 : 0));
16809 /* Generate code for it, if necessary. */
16810 expand_or_defer_fn (fn);
16811 /* Restore the saved values. */
16812 parser->in_unbraced_linkage_specification_p
16813 = saved_in_unbraced_linkage_specification_p;
16814 parser->num_template_parameter_lists
16815 = saved_num_template_parameter_lists;
16816 parser->in_function_body = saved_in_function_body;
16821 /* Parse a template-declaration, assuming that the `export' (and
16822 `extern') keywords, if present, has already been scanned. MEMBER_P
16823 is as for cp_parser_template_declaration. */
16826 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16828 tree decl = NULL_TREE;
16829 VEC (deferred_access_check,gc) *checks;
16830 tree parameter_list;
16831 bool friend_p = false;
16832 bool need_lang_pop;
16834 /* Look for the `template' keyword. */
16835 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16839 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16841 if (at_class_scope_p () && current_function_decl)
16843 /* 14.5.2.2 [temp.mem]
16845 A local class shall not have member templates. */
16846 error ("invalid declaration of member template in local class");
16847 cp_parser_skip_to_end_of_block_or_statement (parser);
16852 A template ... shall not have C linkage. */
16853 if (current_lang_name == lang_name_c)
16855 error ("template with C linkage");
16856 /* Give it C++ linkage to avoid confusing other parts of the
16858 push_lang_context (lang_name_cplusplus);
16859 need_lang_pop = true;
16862 need_lang_pop = false;
16864 /* We cannot perform access checks on the template parameter
16865 declarations until we know what is being declared, just as we
16866 cannot check the decl-specifier list. */
16867 push_deferring_access_checks (dk_deferred);
16869 /* If the next token is `>', then we have an invalid
16870 specialization. Rather than complain about an invalid template
16871 parameter, issue an error message here. */
16872 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16874 cp_parser_error (parser, "invalid explicit specialization");
16875 begin_specialization ();
16876 parameter_list = NULL_TREE;
16879 /* Parse the template parameters. */
16880 parameter_list = cp_parser_template_parameter_list (parser);
16882 /* Get the deferred access checks from the parameter list. These
16883 will be checked once we know what is being declared, as for a
16884 member template the checks must be performed in the scope of the
16885 class containing the member. */
16886 checks = get_deferred_access_checks ();
16888 /* Look for the `>'. */
16889 cp_parser_skip_to_end_of_template_parameter_list (parser);
16890 /* We just processed one more parameter list. */
16891 ++parser->num_template_parameter_lists;
16892 /* If the next token is `template', there are more template
16894 if (cp_lexer_next_token_is_keyword (parser->lexer,
16896 cp_parser_template_declaration_after_export (parser, member_p);
16899 /* There are no access checks when parsing a template, as we do not
16900 know if a specialization will be a friend. */
16901 push_deferring_access_checks (dk_no_check);
16902 decl = cp_parser_single_declaration (parser,
16905 /*explicit_specialization_p=*/false,
16907 pop_deferring_access_checks ();
16909 /* If this is a member template declaration, let the front
16911 if (member_p && !friend_p && decl)
16913 if (TREE_CODE (decl) == TYPE_DECL)
16914 cp_parser_check_access_in_redeclaration (decl);
16916 decl = finish_member_template_decl (decl);
16918 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16919 make_friend_class (current_class_type, TREE_TYPE (decl),
16920 /*complain=*/true);
16922 /* We are done with the current parameter list. */
16923 --parser->num_template_parameter_lists;
16925 pop_deferring_access_checks ();
16928 finish_template_decl (parameter_list);
16930 /* Register member declarations. */
16931 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16932 finish_member_declaration (decl);
16933 /* For the erroneous case of a template with C linkage, we pushed an
16934 implicit C++ linkage scope; exit that scope now. */
16936 pop_lang_context ();
16937 /* If DECL is a function template, we must return to parse it later.
16938 (Even though there is no definition, there might be default
16939 arguments that need handling.) */
16940 if (member_p && decl
16941 && (TREE_CODE (decl) == FUNCTION_DECL
16942 || DECL_FUNCTION_TEMPLATE_P (decl)))
16943 TREE_VALUE (parser->unparsed_functions_queues)
16944 = tree_cons (NULL_TREE, decl,
16945 TREE_VALUE (parser->unparsed_functions_queues));
16948 /* Perform the deferred access checks from a template-parameter-list.
16949 CHECKS is a TREE_LIST of access checks, as returned by
16950 get_deferred_access_checks. */
16953 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16955 ++processing_template_parmlist;
16956 perform_access_checks (checks);
16957 --processing_template_parmlist;
16960 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16961 `function-definition' sequence. MEMBER_P is true, this declaration
16962 appears in a class scope.
16964 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16965 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16968 cp_parser_single_declaration (cp_parser* parser,
16969 VEC (deferred_access_check,gc)* checks,
16971 bool explicit_specialization_p,
16974 int declares_class_or_enum;
16975 tree decl = NULL_TREE;
16976 cp_decl_specifier_seq decl_specifiers;
16977 bool function_definition_p = false;
16979 /* This function is only used when processing a template
16981 gcc_assert (innermost_scope_kind () == sk_template_parms
16982 || innermost_scope_kind () == sk_template_spec);
16984 /* Defer access checks until we know what is being declared. */
16985 push_deferring_access_checks (dk_deferred);
16987 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16989 cp_parser_decl_specifier_seq (parser,
16990 CP_PARSER_FLAGS_OPTIONAL,
16992 &declares_class_or_enum);
16994 *friend_p = cp_parser_friend_p (&decl_specifiers);
16996 /* There are no template typedefs. */
16997 if (decl_specifiers.specs[(int) ds_typedef])
16999 error ("template declaration of %qs", "typedef");
17000 decl = error_mark_node;
17003 /* Gather up the access checks that occurred the
17004 decl-specifier-seq. */
17005 stop_deferring_access_checks ();
17007 /* Check for the declaration of a template class. */
17008 if (declares_class_or_enum)
17010 if (cp_parser_declares_only_class_p (parser))
17012 decl = shadow_tag (&decl_specifiers);
17017 friend template <typename T> struct A<T>::B;
17020 A<T>::B will be represented by a TYPENAME_TYPE, and
17021 therefore not recognized by shadow_tag. */
17022 if (friend_p && *friend_p
17024 && decl_specifiers.type
17025 && TYPE_P (decl_specifiers.type))
17026 decl = decl_specifiers.type;
17028 if (decl && decl != error_mark_node)
17029 decl = TYPE_NAME (decl);
17031 decl = error_mark_node;
17033 /* Perform access checks for template parameters. */
17034 cp_parser_perform_template_parameter_access_checks (checks);
17037 /* If it's not a template class, try for a template function. If
17038 the next token is a `;', then this declaration does not declare
17039 anything. But, if there were errors in the decl-specifiers, then
17040 the error might well have come from an attempted class-specifier.
17041 In that case, there's no need to warn about a missing declarator. */
17043 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17044 || decl_specifiers.type != error_mark_node))
17046 decl = cp_parser_init_declarator (parser,
17049 /*function_definition_allowed_p=*/true,
17051 declares_class_or_enum,
17052 &function_definition_p);
17054 /* 7.1.1-1 [dcl.stc]
17056 A storage-class-specifier shall not be specified in an explicit
17057 specialization... */
17059 && explicit_specialization_p
17060 && decl_specifiers.storage_class != sc_none)
17062 error ("explicit template specialization cannot have a storage class");
17063 decl = error_mark_node;
17067 pop_deferring_access_checks ();
17069 /* Clear any current qualification; whatever comes next is the start
17070 of something new. */
17071 parser->scope = NULL_TREE;
17072 parser->qualifying_scope = NULL_TREE;
17073 parser->object_scope = NULL_TREE;
17074 /* Look for a trailing `;' after the declaration. */
17075 if (!function_definition_p
17076 && (decl == error_mark_node
17077 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17078 cp_parser_skip_to_end_of_block_or_statement (parser);
17083 /* Parse a cast-expression that is not the operand of a unary "&". */
17086 cp_parser_simple_cast_expression (cp_parser *parser)
17088 return cp_parser_cast_expression (parser, /*address_p=*/false,
17092 /* Parse a functional cast to TYPE. Returns an expression
17093 representing the cast. */
17096 cp_parser_functional_cast (cp_parser* parser, tree type)
17098 tree expression_list;
17102 = cp_parser_parenthesized_expression_list (parser, false,
17104 /*allow_expansion_p=*/true,
17105 /*non_constant_p=*/NULL);
17107 cast = build_functional_cast (type, expression_list);
17108 /* [expr.const]/1: In an integral constant expression "only type
17109 conversions to integral or enumeration type can be used". */
17110 if (TREE_CODE (type) == TYPE_DECL)
17111 type = TREE_TYPE (type);
17112 if (cast != error_mark_node
17113 && !cast_valid_in_integral_constant_expression_p (type)
17114 && (cp_parser_non_integral_constant_expression
17115 (parser, "a call to a constructor")))
17116 return error_mark_node;
17120 /* Save the tokens that make up the body of a member function defined
17121 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17122 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17123 specifiers applied to the declaration. Returns the FUNCTION_DECL
17124 for the member function. */
17127 cp_parser_save_member_function_body (cp_parser* parser,
17128 cp_decl_specifier_seq *decl_specifiers,
17129 cp_declarator *declarator,
17136 /* Create the function-declaration. */
17137 fn = start_method (decl_specifiers, declarator, attributes);
17138 /* If something went badly wrong, bail out now. */
17139 if (fn == error_mark_node)
17141 /* If there's a function-body, skip it. */
17142 if (cp_parser_token_starts_function_definition_p
17143 (cp_lexer_peek_token (parser->lexer)))
17144 cp_parser_skip_to_end_of_block_or_statement (parser);
17145 return error_mark_node;
17148 /* Remember it, if there default args to post process. */
17149 cp_parser_save_default_args (parser, fn);
17151 /* Save away the tokens that make up the body of the
17153 first = parser->lexer->next_token;
17154 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17155 /* Handle function try blocks. */
17156 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17157 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17158 last = parser->lexer->next_token;
17160 /* Save away the inline definition; we will process it when the
17161 class is complete. */
17162 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17163 DECL_PENDING_INLINE_P (fn) = 1;
17165 /* We need to know that this was defined in the class, so that
17166 friend templates are handled correctly. */
17167 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17169 /* We're done with the inline definition. */
17170 finish_method (fn);
17172 /* Add FN to the queue of functions to be parsed later. */
17173 TREE_VALUE (parser->unparsed_functions_queues)
17174 = tree_cons (NULL_TREE, fn,
17175 TREE_VALUE (parser->unparsed_functions_queues));
17180 /* Parse a template-argument-list, as well as the trailing ">" (but
17181 not the opening ">"). See cp_parser_template_argument_list for the
17185 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17189 tree saved_qualifying_scope;
17190 tree saved_object_scope;
17191 bool saved_greater_than_is_operator_p;
17192 bool saved_skip_evaluation;
17196 When parsing a template-id, the first non-nested `>' is taken as
17197 the end of the template-argument-list rather than a greater-than
17199 saved_greater_than_is_operator_p
17200 = parser->greater_than_is_operator_p;
17201 parser->greater_than_is_operator_p = false;
17202 /* Parsing the argument list may modify SCOPE, so we save it
17204 saved_scope = parser->scope;
17205 saved_qualifying_scope = parser->qualifying_scope;
17206 saved_object_scope = parser->object_scope;
17207 /* We need to evaluate the template arguments, even though this
17208 template-id may be nested within a "sizeof". */
17209 saved_skip_evaluation = skip_evaluation;
17210 skip_evaluation = false;
17211 /* Parse the template-argument-list itself. */
17212 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17213 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17214 arguments = NULL_TREE;
17216 arguments = cp_parser_template_argument_list (parser);
17217 /* Look for the `>' that ends the template-argument-list. If we find
17218 a '>>' instead, it's probably just a typo. */
17219 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17221 if (cxx_dialect != cxx98)
17223 /* In C++0x, a `>>' in a template argument list or cast
17224 expression is considered to be two separate `>'
17225 tokens. So, change the current token to a `>', but don't
17226 consume it: it will be consumed later when the outer
17227 template argument list (or cast expression) is parsed.
17228 Note that this replacement of `>' for `>>' is necessary
17229 even if we are parsing tentatively: in the tentative
17230 case, after calling
17231 cp_parser_enclosed_template_argument_list we will always
17232 throw away all of the template arguments and the first
17233 closing `>', either because the template argument list
17234 was erroneous or because we are replacing those tokens
17235 with a CPP_TEMPLATE_ID token. The second `>' (which will
17236 not have been thrown away) is needed either to close an
17237 outer template argument list or to complete a new-style
17239 cp_token *token = cp_lexer_peek_token (parser->lexer);
17240 token->type = CPP_GREATER;
17242 else if (!saved_greater_than_is_operator_p)
17244 /* If we're in a nested template argument list, the '>>' has
17245 to be a typo for '> >'. We emit the error message, but we
17246 continue parsing and we push a '>' as next token, so that
17247 the argument list will be parsed correctly. Note that the
17248 global source location is still on the token before the
17249 '>>', so we need to say explicitly where we want it. */
17250 cp_token *token = cp_lexer_peek_token (parser->lexer);
17251 error ("%H%<>>%> should be %<> >%> "
17252 "within a nested template argument list",
17255 token->type = CPP_GREATER;
17259 /* If this is not a nested template argument list, the '>>'
17260 is a typo for '>'. Emit an error message and continue.
17261 Same deal about the token location, but here we can get it
17262 right by consuming the '>>' before issuing the diagnostic. */
17263 cp_lexer_consume_token (parser->lexer);
17264 error ("spurious %<>>%>, use %<>%> to terminate "
17265 "a template argument list");
17269 cp_parser_skip_to_end_of_template_parameter_list (parser);
17270 /* The `>' token might be a greater-than operator again now. */
17271 parser->greater_than_is_operator_p
17272 = saved_greater_than_is_operator_p;
17273 /* Restore the SAVED_SCOPE. */
17274 parser->scope = saved_scope;
17275 parser->qualifying_scope = saved_qualifying_scope;
17276 parser->object_scope = saved_object_scope;
17277 skip_evaluation = saved_skip_evaluation;
17282 /* MEMBER_FUNCTION is a member function, or a friend. If default
17283 arguments, or the body of the function have not yet been parsed,
17287 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17289 /* If this member is a template, get the underlying
17291 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17292 member_function = DECL_TEMPLATE_RESULT (member_function);
17294 /* There should not be any class definitions in progress at this
17295 point; the bodies of members are only parsed outside of all class
17297 gcc_assert (parser->num_classes_being_defined == 0);
17298 /* While we're parsing the member functions we might encounter more
17299 classes. We want to handle them right away, but we don't want
17300 them getting mixed up with functions that are currently in the
17302 parser->unparsed_functions_queues
17303 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17305 /* Make sure that any template parameters are in scope. */
17306 maybe_begin_member_template_processing (member_function);
17308 /* If the body of the function has not yet been parsed, parse it
17310 if (DECL_PENDING_INLINE_P (member_function))
17312 tree function_scope;
17313 cp_token_cache *tokens;
17315 /* The function is no longer pending; we are processing it. */
17316 tokens = DECL_PENDING_INLINE_INFO (member_function);
17317 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17318 DECL_PENDING_INLINE_P (member_function) = 0;
17320 /* If this is a local class, enter the scope of the containing
17322 function_scope = current_function_decl;
17323 if (function_scope)
17324 push_function_context_to (function_scope);
17327 /* Push the body of the function onto the lexer stack. */
17328 cp_parser_push_lexer_for_tokens (parser, tokens);
17330 /* Let the front end know that we going to be defining this
17332 start_preparsed_function (member_function, NULL_TREE,
17333 SF_PRE_PARSED | SF_INCLASS_INLINE);
17335 /* Don't do access checking if it is a templated function. */
17336 if (processing_template_decl)
17337 push_deferring_access_checks (dk_no_check);
17339 /* Now, parse the body of the function. */
17340 cp_parser_function_definition_after_declarator (parser,
17341 /*inline_p=*/true);
17343 if (processing_template_decl)
17344 pop_deferring_access_checks ();
17346 /* Leave the scope of the containing function. */
17347 if (function_scope)
17348 pop_function_context_from (function_scope);
17349 cp_parser_pop_lexer (parser);
17352 /* Remove any template parameters from the symbol table. */
17353 maybe_end_member_template_processing ();
17355 /* Restore the queue. */
17356 parser->unparsed_functions_queues
17357 = TREE_CHAIN (parser->unparsed_functions_queues);
17360 /* If DECL contains any default args, remember it on the unparsed
17361 functions queue. */
17364 cp_parser_save_default_args (cp_parser* parser, tree decl)
17368 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17370 probe = TREE_CHAIN (probe))
17371 if (TREE_PURPOSE (probe))
17373 TREE_PURPOSE (parser->unparsed_functions_queues)
17374 = tree_cons (current_class_type, decl,
17375 TREE_PURPOSE (parser->unparsed_functions_queues));
17380 /* FN is a FUNCTION_DECL which may contains a parameter with an
17381 unparsed DEFAULT_ARG. Parse the default args now. This function
17382 assumes that the current scope is the scope in which the default
17383 argument should be processed. */
17386 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17388 bool saved_local_variables_forbidden_p;
17391 /* While we're parsing the default args, we might (due to the
17392 statement expression extension) encounter more classes. We want
17393 to handle them right away, but we don't want them getting mixed
17394 up with default args that are currently in the queue. */
17395 parser->unparsed_functions_queues
17396 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17398 /* Local variable names (and the `this' keyword) may not appear
17399 in a default argument. */
17400 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17401 parser->local_variables_forbidden_p = true;
17403 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17405 parm = TREE_CHAIN (parm))
17407 cp_token_cache *tokens;
17408 tree default_arg = TREE_PURPOSE (parm);
17410 VEC(tree,gc) *insts;
17417 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17418 /* This can happen for a friend declaration for a function
17419 already declared with default arguments. */
17422 /* Push the saved tokens for the default argument onto the parser's
17424 tokens = DEFARG_TOKENS (default_arg);
17425 cp_parser_push_lexer_for_tokens (parser, tokens);
17427 /* Parse the assignment-expression. */
17428 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17430 if (!processing_template_decl)
17431 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17433 TREE_PURPOSE (parm) = parsed_arg;
17435 /* Update any instantiations we've already created. */
17436 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17437 VEC_iterate (tree, insts, ix, copy); ix++)
17438 TREE_PURPOSE (copy) = parsed_arg;
17440 /* If the token stream has not been completely used up, then
17441 there was extra junk after the end of the default
17443 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17444 cp_parser_error (parser, "expected %<,%>");
17446 /* Revert to the main lexer. */
17447 cp_parser_pop_lexer (parser);
17450 /* Make sure no default arg is missing. */
17451 check_default_args (fn);
17453 /* Restore the state of local_variables_forbidden_p. */
17454 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17456 /* Restore the queue. */
17457 parser->unparsed_functions_queues
17458 = TREE_CHAIN (parser->unparsed_functions_queues);
17461 /* Parse the operand of `sizeof' (or a similar operator). Returns
17462 either a TYPE or an expression, depending on the form of the
17463 input. The KEYWORD indicates which kind of expression we have
17467 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17469 static const char *format;
17470 tree expr = NULL_TREE;
17471 const char *saved_message;
17473 bool saved_integral_constant_expression_p;
17474 bool saved_non_integral_constant_expression_p;
17475 bool pack_expansion_p = false;
17477 /* Initialize FORMAT the first time we get here. */
17479 format = "types may not be defined in '%s' expressions";
17481 /* Types cannot be defined in a `sizeof' expression. Save away the
17483 saved_message = parser->type_definition_forbidden_message;
17484 /* And create the new one. */
17485 parser->type_definition_forbidden_message = tmp
17486 = XNEWVEC (char, strlen (format)
17487 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17489 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17491 /* The restrictions on constant-expressions do not apply inside
17492 sizeof expressions. */
17493 saved_integral_constant_expression_p
17494 = parser->integral_constant_expression_p;
17495 saved_non_integral_constant_expression_p
17496 = parser->non_integral_constant_expression_p;
17497 parser->integral_constant_expression_p = false;
17499 /* If it's a `...', then we are computing the length of a parameter
17501 if (keyword == RID_SIZEOF
17502 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17504 /* Consume the `...'. */
17505 cp_lexer_consume_token (parser->lexer);
17506 maybe_warn_variadic_templates ();
17508 /* Note that this is an expansion. */
17509 pack_expansion_p = true;
17512 /* Do not actually evaluate the expression. */
17514 /* If it's a `(', then we might be looking at the type-id
17516 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17519 bool saved_in_type_id_in_expr_p;
17521 /* We can't be sure yet whether we're looking at a type-id or an
17523 cp_parser_parse_tentatively (parser);
17524 /* Consume the `('. */
17525 cp_lexer_consume_token (parser->lexer);
17526 /* Parse the type-id. */
17527 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17528 parser->in_type_id_in_expr_p = true;
17529 type = cp_parser_type_id (parser);
17530 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17531 /* Now, look for the trailing `)'. */
17532 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17533 /* If all went well, then we're done. */
17534 if (cp_parser_parse_definitely (parser))
17536 cp_decl_specifier_seq decl_specs;
17538 /* Build a trivial decl-specifier-seq. */
17539 clear_decl_specs (&decl_specs);
17540 decl_specs.type = type;
17542 /* Call grokdeclarator to figure out what type this is. */
17543 expr = grokdeclarator (NULL,
17547 /*attrlist=*/NULL);
17551 /* If the type-id production did not work out, then we must be
17552 looking at the unary-expression production. */
17554 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17557 if (pack_expansion_p)
17558 /* Build a pack expansion. */
17559 expr = make_pack_expansion (expr);
17561 /* Go back to evaluating expressions. */
17564 /* Free the message we created. */
17566 /* And restore the old one. */
17567 parser->type_definition_forbidden_message = saved_message;
17568 parser->integral_constant_expression_p
17569 = saved_integral_constant_expression_p;
17570 parser->non_integral_constant_expression_p
17571 = saved_non_integral_constant_expression_p;
17576 /* If the current declaration has no declarator, return true. */
17579 cp_parser_declares_only_class_p (cp_parser *parser)
17581 /* If the next token is a `;' or a `,' then there is no
17583 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17584 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17587 /* Update the DECL_SPECS to reflect the storage class indicated by
17591 cp_parser_set_storage_class (cp_parser *parser,
17592 cp_decl_specifier_seq *decl_specs,
17595 cp_storage_class storage_class;
17597 if (parser->in_unbraced_linkage_specification_p)
17599 error ("invalid use of %qD in linkage specification",
17600 ridpointers[keyword]);
17603 else if (decl_specs->storage_class != sc_none)
17605 decl_specs->conflicting_specifiers_p = true;
17609 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17610 && decl_specs->specs[(int) ds_thread])
17612 error ("%<__thread%> before %qD", ridpointers[keyword]);
17613 decl_specs->specs[(int) ds_thread] = 0;
17619 storage_class = sc_auto;
17622 storage_class = sc_register;
17625 storage_class = sc_static;
17628 storage_class = sc_extern;
17631 storage_class = sc_mutable;
17634 gcc_unreachable ();
17636 decl_specs->storage_class = storage_class;
17638 /* A storage class specifier cannot be applied alongside a typedef
17639 specifier. If there is a typedef specifier present then set
17640 conflicting_specifiers_p which will trigger an error later
17641 on in grokdeclarator. */
17642 if (decl_specs->specs[(int)ds_typedef])
17643 decl_specs->conflicting_specifiers_p = true;
17646 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17647 is true, the type is a user-defined type; otherwise it is a
17648 built-in type specified by a keyword. */
17651 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17653 bool user_defined_p)
17655 decl_specs->any_specifiers_p = true;
17657 /* If the user tries to redeclare bool or wchar_t (with, for
17658 example, in "typedef int wchar_t;") we remember that this is what
17659 happened. In system headers, we ignore these declarations so
17660 that G++ can work with system headers that are not C++-safe. */
17661 if (decl_specs->specs[(int) ds_typedef]
17663 && (type_spec == boolean_type_node
17664 || type_spec == wchar_type_node)
17665 && (decl_specs->type
17666 || decl_specs->specs[(int) ds_long]
17667 || decl_specs->specs[(int) ds_short]
17668 || decl_specs->specs[(int) ds_unsigned]
17669 || decl_specs->specs[(int) ds_signed]))
17671 decl_specs->redefined_builtin_type = type_spec;
17672 if (!decl_specs->type)
17674 decl_specs->type = type_spec;
17675 decl_specs->user_defined_type_p = false;
17678 else if (decl_specs->type)
17679 decl_specs->multiple_types_p = true;
17682 decl_specs->type = type_spec;
17683 decl_specs->user_defined_type_p = user_defined_p;
17684 decl_specs->redefined_builtin_type = NULL_TREE;
17688 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17689 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17692 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17694 return decl_specifiers->specs[(int) ds_friend] != 0;
17697 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17698 issue an error message indicating that TOKEN_DESC was expected.
17700 Returns the token consumed, if the token had the appropriate type.
17701 Otherwise, returns NULL. */
17704 cp_parser_require (cp_parser* parser,
17705 enum cpp_ttype type,
17706 const char* token_desc)
17708 if (cp_lexer_next_token_is (parser->lexer, type))
17709 return cp_lexer_consume_token (parser->lexer);
17712 /* Output the MESSAGE -- unless we're parsing tentatively. */
17713 if (!cp_parser_simulate_error (parser))
17715 char *message = concat ("expected ", token_desc, NULL);
17716 cp_parser_error (parser, message);
17723 /* An error message is produced if the next token is not '>'.
17724 All further tokens are skipped until the desired token is
17725 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17728 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17730 /* Current level of '< ... >'. */
17731 unsigned level = 0;
17732 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17733 unsigned nesting_depth = 0;
17735 /* Are we ready, yet? If not, issue error message. */
17736 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17739 /* Skip tokens until the desired token is found. */
17742 /* Peek at the next token. */
17743 switch (cp_lexer_peek_token (parser->lexer)->type)
17746 if (!nesting_depth)
17751 if (cxx_dialect == cxx98)
17752 /* C++0x views the `>>' operator as two `>' tokens, but
17755 else if (!nesting_depth && level-- == 0)
17757 /* We've hit a `>>' where the first `>' closes the
17758 template argument list, and the second `>' is
17759 spurious. Just consume the `>>' and stop; we've
17760 already produced at least one error. */
17761 cp_lexer_consume_token (parser->lexer);
17764 /* Fall through for C++0x, so we handle the second `>' in
17768 if (!nesting_depth && level-- == 0)
17770 /* We've reached the token we want, consume it and stop. */
17771 cp_lexer_consume_token (parser->lexer);
17776 case CPP_OPEN_PAREN:
17777 case CPP_OPEN_SQUARE:
17781 case CPP_CLOSE_PAREN:
17782 case CPP_CLOSE_SQUARE:
17783 if (nesting_depth-- == 0)
17788 case CPP_PRAGMA_EOL:
17789 case CPP_SEMICOLON:
17790 case CPP_OPEN_BRACE:
17791 case CPP_CLOSE_BRACE:
17792 /* The '>' was probably forgotten, don't look further. */
17799 /* Consume this token. */
17800 cp_lexer_consume_token (parser->lexer);
17804 /* If the next token is the indicated keyword, consume it. Otherwise,
17805 issue an error message indicating that TOKEN_DESC was expected.
17807 Returns the token consumed, if the token had the appropriate type.
17808 Otherwise, returns NULL. */
17811 cp_parser_require_keyword (cp_parser* parser,
17813 const char* token_desc)
17815 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17817 if (token && token->keyword != keyword)
17819 dyn_string_t error_msg;
17821 /* Format the error message. */
17822 error_msg = dyn_string_new (0);
17823 dyn_string_append_cstr (error_msg, "expected ");
17824 dyn_string_append_cstr (error_msg, token_desc);
17825 cp_parser_error (parser, error_msg->s);
17826 dyn_string_delete (error_msg);
17833 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17834 function-definition. */
17837 cp_parser_token_starts_function_definition_p (cp_token* token)
17839 return (/* An ordinary function-body begins with an `{'. */
17840 token->type == CPP_OPEN_BRACE
17841 /* A ctor-initializer begins with a `:'. */
17842 || token->type == CPP_COLON
17843 /* A function-try-block begins with `try'. */
17844 || token->keyword == RID_TRY
17845 /* The named return value extension begins with `return'. */
17846 || token->keyword == RID_RETURN);
17849 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17853 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17857 token = cp_lexer_peek_token (parser->lexer);
17858 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17861 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17862 C++0x) ending a template-argument. */
17865 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17869 token = cp_lexer_peek_token (parser->lexer);
17870 return (token->type == CPP_COMMA
17871 || token->type == CPP_GREATER
17872 || token->type == CPP_ELLIPSIS
17873 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17876 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17877 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17880 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17885 token = cp_lexer_peek_nth_token (parser->lexer, n);
17886 if (token->type == CPP_LESS)
17888 /* Check for the sequence `<::' in the original code. It would be lexed as
17889 `[:', where `[' is a digraph, and there is no whitespace before
17891 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17894 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17895 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17901 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17902 or none_type otherwise. */
17904 static enum tag_types
17905 cp_parser_token_is_class_key (cp_token* token)
17907 switch (token->keyword)
17912 return record_type;
17921 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17924 cp_parser_check_class_key (enum tag_types class_key, tree type)
17926 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17927 pedwarn ("%qs tag used in naming %q#T",
17928 class_key == union_type ? "union"
17929 : class_key == record_type ? "struct" : "class",
17933 /* Issue an error message if DECL is redeclared with different
17934 access than its original declaration [class.access.spec/3].
17935 This applies to nested classes and nested class templates.
17939 cp_parser_check_access_in_redeclaration (tree decl)
17941 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17944 if ((TREE_PRIVATE (decl)
17945 != (current_access_specifier == access_private_node))
17946 || (TREE_PROTECTED (decl)
17947 != (current_access_specifier == access_protected_node)))
17948 error ("%qD redeclared with different access", decl);
17951 /* Look for the `template' keyword, as a syntactic disambiguator.
17952 Return TRUE iff it is present, in which case it will be
17956 cp_parser_optional_template_keyword (cp_parser *parser)
17958 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17960 /* The `template' keyword can only be used within templates;
17961 outside templates the parser can always figure out what is a
17962 template and what is not. */
17963 if (!processing_template_decl)
17965 error ("%<template%> (as a disambiguator) is only allowed "
17966 "within templates");
17967 /* If this part of the token stream is rescanned, the same
17968 error message would be generated. So, we purge the token
17969 from the stream. */
17970 cp_lexer_purge_token (parser->lexer);
17975 /* Consume the `template' keyword. */
17976 cp_lexer_consume_token (parser->lexer);
17984 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17985 set PARSER->SCOPE, and perform other related actions. */
17988 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17991 struct tree_check *check_value;
17992 deferred_access_check *chk;
17993 VEC (deferred_access_check,gc) *checks;
17995 /* Get the stored value. */
17996 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17997 /* Perform any access checks that were deferred. */
17998 checks = check_value->checks;
18002 VEC_iterate (deferred_access_check, checks, i, chk) ;
18005 perform_or_defer_access_check (chk->binfo,
18010 /* Set the scope from the stored value. */
18011 parser->scope = check_value->value;
18012 parser->qualifying_scope = check_value->qualifying_scope;
18013 parser->object_scope = NULL_TREE;
18016 /* Consume tokens up through a non-nested END token. */
18019 cp_parser_cache_group (cp_parser *parser,
18020 enum cpp_ttype end,
18027 /* Abort a parenthesized expression if we encounter a brace. */
18028 if ((end == CPP_CLOSE_PAREN || depth == 0)
18029 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18031 /* If we've reached the end of the file, stop. */
18032 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18033 || (end != CPP_PRAGMA_EOL
18034 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18036 /* Consume the next token. */
18037 token = cp_lexer_consume_token (parser->lexer);
18038 /* See if it starts a new group. */
18039 if (token->type == CPP_OPEN_BRACE)
18041 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18045 else if (token->type == CPP_OPEN_PAREN)
18046 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18047 else if (token->type == CPP_PRAGMA)
18048 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18049 else if (token->type == end)
18054 /* Begin parsing tentatively. We always save tokens while parsing
18055 tentatively so that if the tentative parsing fails we can restore the
18059 cp_parser_parse_tentatively (cp_parser* parser)
18061 /* Enter a new parsing context. */
18062 parser->context = cp_parser_context_new (parser->context);
18063 /* Begin saving tokens. */
18064 cp_lexer_save_tokens (parser->lexer);
18065 /* In order to avoid repetitive access control error messages,
18066 access checks are queued up until we are no longer parsing
18068 push_deferring_access_checks (dk_deferred);
18071 /* Commit to the currently active tentative parse. */
18074 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18076 cp_parser_context *context;
18079 /* Mark all of the levels as committed. */
18080 lexer = parser->lexer;
18081 for (context = parser->context; context->next; context = context->next)
18083 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18085 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18086 while (!cp_lexer_saving_tokens (lexer))
18087 lexer = lexer->next;
18088 cp_lexer_commit_tokens (lexer);
18092 /* Abort the currently active tentative parse. All consumed tokens
18093 will be rolled back, and no diagnostics will be issued. */
18096 cp_parser_abort_tentative_parse (cp_parser* parser)
18098 cp_parser_simulate_error (parser);
18099 /* Now, pretend that we want to see if the construct was
18100 successfully parsed. */
18101 cp_parser_parse_definitely (parser);
18104 /* Stop parsing tentatively. If a parse error has occurred, restore the
18105 token stream. Otherwise, commit to the tokens we have consumed.
18106 Returns true if no error occurred; false otherwise. */
18109 cp_parser_parse_definitely (cp_parser* parser)
18111 bool error_occurred;
18112 cp_parser_context *context;
18114 /* Remember whether or not an error occurred, since we are about to
18115 destroy that information. */
18116 error_occurred = cp_parser_error_occurred (parser);
18117 /* Remove the topmost context from the stack. */
18118 context = parser->context;
18119 parser->context = context->next;
18120 /* If no parse errors occurred, commit to the tentative parse. */
18121 if (!error_occurred)
18123 /* Commit to the tokens read tentatively, unless that was
18125 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18126 cp_lexer_commit_tokens (parser->lexer);
18128 pop_to_parent_deferring_access_checks ();
18130 /* Otherwise, if errors occurred, roll back our state so that things
18131 are just as they were before we began the tentative parse. */
18134 cp_lexer_rollback_tokens (parser->lexer);
18135 pop_deferring_access_checks ();
18137 /* Add the context to the front of the free list. */
18138 context->next = cp_parser_context_free_list;
18139 cp_parser_context_free_list = context;
18141 return !error_occurred;
18144 /* Returns true if we are parsing tentatively and are not committed to
18145 this tentative parse. */
18148 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18150 return (cp_parser_parsing_tentatively (parser)
18151 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18154 /* Returns nonzero iff an error has occurred during the most recent
18155 tentative parse. */
18158 cp_parser_error_occurred (cp_parser* parser)
18160 return (cp_parser_parsing_tentatively (parser)
18161 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18164 /* Returns nonzero if GNU extensions are allowed. */
18167 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18169 return parser->allow_gnu_extensions_p;
18172 /* Objective-C++ Productions */
18175 /* Parse an Objective-C expression, which feeds into a primary-expression
18179 objc-message-expression
18180 objc-string-literal
18181 objc-encode-expression
18182 objc-protocol-expression
18183 objc-selector-expression
18185 Returns a tree representation of the expression. */
18188 cp_parser_objc_expression (cp_parser* parser)
18190 /* Try to figure out what kind of declaration is present. */
18191 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18195 case CPP_OPEN_SQUARE:
18196 return cp_parser_objc_message_expression (parser);
18198 case CPP_OBJC_STRING:
18199 kwd = cp_lexer_consume_token (parser->lexer);
18200 return objc_build_string_object (kwd->u.value);
18203 switch (kwd->keyword)
18205 case RID_AT_ENCODE:
18206 return cp_parser_objc_encode_expression (parser);
18208 case RID_AT_PROTOCOL:
18209 return cp_parser_objc_protocol_expression (parser);
18211 case RID_AT_SELECTOR:
18212 return cp_parser_objc_selector_expression (parser);
18218 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18219 cp_parser_skip_to_end_of_block_or_statement (parser);
18222 return error_mark_node;
18225 /* Parse an Objective-C message expression.
18227 objc-message-expression:
18228 [ objc-message-receiver objc-message-args ]
18230 Returns a representation of an Objective-C message. */
18233 cp_parser_objc_message_expression (cp_parser* parser)
18235 tree receiver, messageargs;
18237 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18238 receiver = cp_parser_objc_message_receiver (parser);
18239 messageargs = cp_parser_objc_message_args (parser);
18240 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18242 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18245 /* Parse an objc-message-receiver.
18247 objc-message-receiver:
18249 simple-type-specifier
18251 Returns a representation of the type or expression. */
18254 cp_parser_objc_message_receiver (cp_parser* parser)
18258 /* An Objective-C message receiver may be either (1) a type
18259 or (2) an expression. */
18260 cp_parser_parse_tentatively (parser);
18261 rcv = cp_parser_expression (parser, false);
18263 if (cp_parser_parse_definitely (parser))
18266 rcv = cp_parser_simple_type_specifier (parser,
18267 /*decl_specs=*/NULL,
18268 CP_PARSER_FLAGS_NONE);
18270 return objc_get_class_reference (rcv);
18273 /* Parse the arguments and selectors comprising an Objective-C message.
18278 objc-selector-args , objc-comma-args
18280 objc-selector-args:
18281 objc-selector [opt] : assignment-expression
18282 objc-selector-args objc-selector [opt] : assignment-expression
18285 assignment-expression
18286 objc-comma-args , assignment-expression
18288 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18289 selector arguments and TREE_VALUE containing a list of comma
18293 cp_parser_objc_message_args (cp_parser* parser)
18295 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18296 bool maybe_unary_selector_p = true;
18297 cp_token *token = cp_lexer_peek_token (parser->lexer);
18299 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18301 tree selector = NULL_TREE, arg;
18303 if (token->type != CPP_COLON)
18304 selector = cp_parser_objc_selector (parser);
18306 /* Detect if we have a unary selector. */
18307 if (maybe_unary_selector_p
18308 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18309 return build_tree_list (selector, NULL_TREE);
18311 maybe_unary_selector_p = false;
18312 cp_parser_require (parser, CPP_COLON, "`:'");
18313 arg = cp_parser_assignment_expression (parser, false);
18316 = chainon (sel_args,
18317 build_tree_list (selector, arg));
18319 token = cp_lexer_peek_token (parser->lexer);
18322 /* Handle non-selector arguments, if any. */
18323 while (token->type == CPP_COMMA)
18327 cp_lexer_consume_token (parser->lexer);
18328 arg = cp_parser_assignment_expression (parser, false);
18331 = chainon (addl_args,
18332 build_tree_list (NULL_TREE, arg));
18334 token = cp_lexer_peek_token (parser->lexer);
18337 return build_tree_list (sel_args, addl_args);
18340 /* Parse an Objective-C encode expression.
18342 objc-encode-expression:
18343 @encode objc-typename
18345 Returns an encoded representation of the type argument. */
18348 cp_parser_objc_encode_expression (cp_parser* parser)
18352 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18353 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18354 type = complete_type (cp_parser_type_id (parser));
18355 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18359 error ("%<@encode%> must specify a type as an argument");
18360 return error_mark_node;
18363 return objc_build_encode_expr (type);
18366 /* Parse an Objective-C @defs expression. */
18369 cp_parser_objc_defs_expression (cp_parser *parser)
18373 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18374 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18375 name = cp_parser_identifier (parser);
18376 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18378 return objc_get_class_ivars (name);
18381 /* Parse an Objective-C protocol expression.
18383 objc-protocol-expression:
18384 @protocol ( identifier )
18386 Returns a representation of the protocol expression. */
18389 cp_parser_objc_protocol_expression (cp_parser* parser)
18393 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18394 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18395 proto = cp_parser_identifier (parser);
18396 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18398 return objc_build_protocol_expr (proto);
18401 /* Parse an Objective-C selector expression.
18403 objc-selector-expression:
18404 @selector ( objc-method-signature )
18406 objc-method-signature:
18412 objc-selector-seq objc-selector :
18414 Returns a representation of the method selector. */
18417 cp_parser_objc_selector_expression (cp_parser* parser)
18419 tree sel_seq = NULL_TREE;
18420 bool maybe_unary_selector_p = true;
18423 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18424 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18425 token = cp_lexer_peek_token (parser->lexer);
18427 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18428 || token->type == CPP_SCOPE)
18430 tree selector = NULL_TREE;
18432 if (token->type != CPP_COLON
18433 || token->type == CPP_SCOPE)
18434 selector = cp_parser_objc_selector (parser);
18436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18437 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18439 /* Detect if we have a unary selector. */
18440 if (maybe_unary_selector_p)
18442 sel_seq = selector;
18443 goto finish_selector;
18447 cp_parser_error (parser, "expected %<:%>");
18450 maybe_unary_selector_p = false;
18451 token = cp_lexer_consume_token (parser->lexer);
18453 if (token->type == CPP_SCOPE)
18456 = chainon (sel_seq,
18457 build_tree_list (selector, NULL_TREE));
18459 = chainon (sel_seq,
18460 build_tree_list (NULL_TREE, NULL_TREE));
18464 = chainon (sel_seq,
18465 build_tree_list (selector, NULL_TREE));
18467 token = cp_lexer_peek_token (parser->lexer);
18471 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18473 return objc_build_selector_expr (sel_seq);
18476 /* Parse a list of identifiers.
18478 objc-identifier-list:
18480 objc-identifier-list , identifier
18482 Returns a TREE_LIST of identifier nodes. */
18485 cp_parser_objc_identifier_list (cp_parser* parser)
18487 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18488 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18490 while (sep->type == CPP_COMMA)
18492 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18493 list = chainon (list,
18494 build_tree_list (NULL_TREE,
18495 cp_parser_identifier (parser)));
18496 sep = cp_lexer_peek_token (parser->lexer);
18502 /* Parse an Objective-C alias declaration.
18504 objc-alias-declaration:
18505 @compatibility_alias identifier identifier ;
18507 This function registers the alias mapping with the Objective-C front end.
18508 It returns nothing. */
18511 cp_parser_objc_alias_declaration (cp_parser* parser)
18515 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18516 alias = cp_parser_identifier (parser);
18517 orig = cp_parser_identifier (parser);
18518 objc_declare_alias (alias, orig);
18519 cp_parser_consume_semicolon_at_end_of_statement (parser);
18522 /* Parse an Objective-C class forward-declaration.
18524 objc-class-declaration:
18525 @class objc-identifier-list ;
18527 The function registers the forward declarations with the Objective-C
18528 front end. It returns nothing. */
18531 cp_parser_objc_class_declaration (cp_parser* parser)
18533 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18534 objc_declare_class (cp_parser_objc_identifier_list (parser));
18535 cp_parser_consume_semicolon_at_end_of_statement (parser);
18538 /* Parse a list of Objective-C protocol references.
18540 objc-protocol-refs-opt:
18541 objc-protocol-refs [opt]
18543 objc-protocol-refs:
18544 < objc-identifier-list >
18546 Returns a TREE_LIST of identifiers, if any. */
18549 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18551 tree protorefs = NULL_TREE;
18553 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18555 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18556 protorefs = cp_parser_objc_identifier_list (parser);
18557 cp_parser_require (parser, CPP_GREATER, "`>'");
18563 /* Parse a Objective-C visibility specification. */
18566 cp_parser_objc_visibility_spec (cp_parser* parser)
18568 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18570 switch (vis->keyword)
18572 case RID_AT_PRIVATE:
18573 objc_set_visibility (2);
18575 case RID_AT_PROTECTED:
18576 objc_set_visibility (0);
18578 case RID_AT_PUBLIC:
18579 objc_set_visibility (1);
18585 /* Eat '@private'/'@protected'/'@public'. */
18586 cp_lexer_consume_token (parser->lexer);
18589 /* Parse an Objective-C method type. */
18592 cp_parser_objc_method_type (cp_parser* parser)
18594 objc_set_method_type
18595 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18600 /* Parse an Objective-C protocol qualifier. */
18603 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18605 tree quals = NULL_TREE, node;
18606 cp_token *token = cp_lexer_peek_token (parser->lexer);
18608 node = token->u.value;
18610 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18611 && (node == ridpointers [(int) RID_IN]
18612 || node == ridpointers [(int) RID_OUT]
18613 || node == ridpointers [(int) RID_INOUT]
18614 || node == ridpointers [(int) RID_BYCOPY]
18615 || node == ridpointers [(int) RID_BYREF]
18616 || node == ridpointers [(int) RID_ONEWAY]))
18618 quals = tree_cons (NULL_TREE, node, quals);
18619 cp_lexer_consume_token (parser->lexer);
18620 token = cp_lexer_peek_token (parser->lexer);
18621 node = token->u.value;
18627 /* Parse an Objective-C typename. */
18630 cp_parser_objc_typename (cp_parser* parser)
18632 tree typename = NULL_TREE;
18634 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18636 tree proto_quals, cp_type = NULL_TREE;
18638 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18639 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18641 /* An ObjC type name may consist of just protocol qualifiers, in which
18642 case the type shall default to 'id'. */
18643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18644 cp_type = cp_parser_type_id (parser);
18646 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18647 typename = build_tree_list (proto_quals, cp_type);
18653 /* Check to see if TYPE refers to an Objective-C selector name. */
18656 cp_parser_objc_selector_p (enum cpp_ttype type)
18658 return (type == CPP_NAME || type == CPP_KEYWORD
18659 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18660 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18661 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18662 || type == CPP_XOR || type == CPP_XOR_EQ);
18665 /* Parse an Objective-C selector. */
18668 cp_parser_objc_selector (cp_parser* parser)
18670 cp_token *token = cp_lexer_consume_token (parser->lexer);
18672 if (!cp_parser_objc_selector_p (token->type))
18674 error ("invalid Objective-C++ selector name");
18675 return error_mark_node;
18678 /* C++ operator names are allowed to appear in ObjC selectors. */
18679 switch (token->type)
18681 case CPP_AND_AND: return get_identifier ("and");
18682 case CPP_AND_EQ: return get_identifier ("and_eq");
18683 case CPP_AND: return get_identifier ("bitand");
18684 case CPP_OR: return get_identifier ("bitor");
18685 case CPP_COMPL: return get_identifier ("compl");
18686 case CPP_NOT: return get_identifier ("not");
18687 case CPP_NOT_EQ: return get_identifier ("not_eq");
18688 case CPP_OR_OR: return get_identifier ("or");
18689 case CPP_OR_EQ: return get_identifier ("or_eq");
18690 case CPP_XOR: return get_identifier ("xor");
18691 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18692 default: return token->u.value;
18696 /* Parse an Objective-C params list. */
18699 cp_parser_objc_method_keyword_params (cp_parser* parser)
18701 tree params = NULL_TREE;
18702 bool maybe_unary_selector_p = true;
18703 cp_token *token = cp_lexer_peek_token (parser->lexer);
18705 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18707 tree selector = NULL_TREE, typename, identifier;
18709 if (token->type != CPP_COLON)
18710 selector = cp_parser_objc_selector (parser);
18712 /* Detect if we have a unary selector. */
18713 if (maybe_unary_selector_p
18714 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18717 maybe_unary_selector_p = false;
18718 cp_parser_require (parser, CPP_COLON, "`:'");
18719 typename = cp_parser_objc_typename (parser);
18720 identifier = cp_parser_identifier (parser);
18724 objc_build_keyword_decl (selector,
18728 token = cp_lexer_peek_token (parser->lexer);
18734 /* Parse the non-keyword Objective-C params. */
18737 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18739 tree params = make_node (TREE_LIST);
18740 cp_token *token = cp_lexer_peek_token (parser->lexer);
18741 *ellipsisp = false; /* Initially, assume no ellipsis. */
18743 while (token->type == CPP_COMMA)
18745 cp_parameter_declarator *parmdecl;
18748 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18749 token = cp_lexer_peek_token (parser->lexer);
18751 if (token->type == CPP_ELLIPSIS)
18753 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18758 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18759 parm = grokdeclarator (parmdecl->declarator,
18760 &parmdecl->decl_specifiers,
18761 PARM, /*initialized=*/0,
18762 /*attrlist=*/NULL);
18764 chainon (params, build_tree_list (NULL_TREE, parm));
18765 token = cp_lexer_peek_token (parser->lexer);
18771 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18774 cp_parser_objc_interstitial_code (cp_parser* parser)
18776 cp_token *token = cp_lexer_peek_token (parser->lexer);
18778 /* If the next token is `extern' and the following token is a string
18779 literal, then we have a linkage specification. */
18780 if (token->keyword == RID_EXTERN
18781 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18782 cp_parser_linkage_specification (parser);
18783 /* Handle #pragma, if any. */
18784 else if (token->type == CPP_PRAGMA)
18785 cp_parser_pragma (parser, pragma_external);
18786 /* Allow stray semicolons. */
18787 else if (token->type == CPP_SEMICOLON)
18788 cp_lexer_consume_token (parser->lexer);
18789 /* Finally, try to parse a block-declaration, or a function-definition. */
18791 cp_parser_block_declaration (parser, /*statement_p=*/false);
18794 /* Parse a method signature. */
18797 cp_parser_objc_method_signature (cp_parser* parser)
18799 tree rettype, kwdparms, optparms;
18800 bool ellipsis = false;
18802 cp_parser_objc_method_type (parser);
18803 rettype = cp_parser_objc_typename (parser);
18804 kwdparms = cp_parser_objc_method_keyword_params (parser);
18805 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18807 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18810 /* Pars an Objective-C method prototype list. */
18813 cp_parser_objc_method_prototype_list (cp_parser* parser)
18815 cp_token *token = cp_lexer_peek_token (parser->lexer);
18817 while (token->keyword != RID_AT_END)
18819 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18821 objc_add_method_declaration
18822 (cp_parser_objc_method_signature (parser));
18823 cp_parser_consume_semicolon_at_end_of_statement (parser);
18826 /* Allow for interspersed non-ObjC++ code. */
18827 cp_parser_objc_interstitial_code (parser);
18829 token = cp_lexer_peek_token (parser->lexer);
18832 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18833 objc_finish_interface ();
18836 /* Parse an Objective-C method definition list. */
18839 cp_parser_objc_method_definition_list (cp_parser* parser)
18841 cp_token *token = cp_lexer_peek_token (parser->lexer);
18843 while (token->keyword != RID_AT_END)
18847 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18849 push_deferring_access_checks (dk_deferred);
18850 objc_start_method_definition
18851 (cp_parser_objc_method_signature (parser));
18853 /* For historical reasons, we accept an optional semicolon. */
18854 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18855 cp_lexer_consume_token (parser->lexer);
18857 perform_deferred_access_checks ();
18858 stop_deferring_access_checks ();
18859 meth = cp_parser_function_definition_after_declarator (parser,
18861 pop_deferring_access_checks ();
18862 objc_finish_method_definition (meth);
18865 /* Allow for interspersed non-ObjC++ code. */
18866 cp_parser_objc_interstitial_code (parser);
18868 token = cp_lexer_peek_token (parser->lexer);
18871 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18872 objc_finish_implementation ();
18875 /* Parse Objective-C ivars. */
18878 cp_parser_objc_class_ivars (cp_parser* parser)
18880 cp_token *token = cp_lexer_peek_token (parser->lexer);
18882 if (token->type != CPP_OPEN_BRACE)
18883 return; /* No ivars specified. */
18885 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18886 token = cp_lexer_peek_token (parser->lexer);
18888 while (token->type != CPP_CLOSE_BRACE)
18890 cp_decl_specifier_seq declspecs;
18891 int decl_class_or_enum_p;
18892 tree prefix_attributes;
18894 cp_parser_objc_visibility_spec (parser);
18896 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18899 cp_parser_decl_specifier_seq (parser,
18900 CP_PARSER_FLAGS_OPTIONAL,
18902 &decl_class_or_enum_p);
18903 prefix_attributes = declspecs.attributes;
18904 declspecs.attributes = NULL_TREE;
18906 /* Keep going until we hit the `;' at the end of the
18908 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18910 tree width = NULL_TREE, attributes, first_attribute, decl;
18911 cp_declarator *declarator = NULL;
18912 int ctor_dtor_or_conv_p;
18914 /* Check for a (possibly unnamed) bitfield declaration. */
18915 token = cp_lexer_peek_token (parser->lexer);
18916 if (token->type == CPP_COLON)
18919 if (token->type == CPP_NAME
18920 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18923 /* Get the name of the bitfield. */
18924 declarator = make_id_declarator (NULL_TREE,
18925 cp_parser_identifier (parser),
18929 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18930 /* Get the width of the bitfield. */
18932 = cp_parser_constant_expression (parser,
18933 /*allow_non_constant=*/false,
18938 /* Parse the declarator. */
18940 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18941 &ctor_dtor_or_conv_p,
18942 /*parenthesized_p=*/NULL,
18943 /*member_p=*/false);
18946 /* Look for attributes that apply to the ivar. */
18947 attributes = cp_parser_attributes_opt (parser);
18948 /* Remember which attributes are prefix attributes and
18950 first_attribute = attributes;
18951 /* Combine the attributes. */
18952 attributes = chainon (prefix_attributes, attributes);
18956 /* Create the bitfield declaration. */
18957 decl = grokbitfield (declarator, &declspecs, width);
18958 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18961 decl = grokfield (declarator, &declspecs,
18962 NULL_TREE, /*init_const_expr_p=*/false,
18963 NULL_TREE, attributes);
18965 /* Add the instance variable. */
18966 objc_add_instance_variable (decl);
18968 /* Reset PREFIX_ATTRIBUTES. */
18969 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18970 attributes = TREE_CHAIN (attributes);
18972 TREE_CHAIN (attributes) = NULL_TREE;
18974 token = cp_lexer_peek_token (parser->lexer);
18976 if (token->type == CPP_COMMA)
18978 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18984 cp_parser_consume_semicolon_at_end_of_statement (parser);
18985 token = cp_lexer_peek_token (parser->lexer);
18988 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18989 /* For historical reasons, we accept an optional semicolon. */
18990 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18991 cp_lexer_consume_token (parser->lexer);
18994 /* Parse an Objective-C protocol declaration. */
18997 cp_parser_objc_protocol_declaration (cp_parser* parser)
18999 tree proto, protorefs;
19002 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19005 error ("identifier expected after %<@protocol%>");
19009 /* See if we have a forward declaration or a definition. */
19010 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19012 /* Try a forward declaration first. */
19013 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19015 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19017 cp_parser_consume_semicolon_at_end_of_statement (parser);
19020 /* Ok, we got a full-fledged definition (or at least should). */
19023 proto = cp_parser_identifier (parser);
19024 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19025 objc_start_protocol (proto, protorefs);
19026 cp_parser_objc_method_prototype_list (parser);
19030 /* Parse an Objective-C superclass or category. */
19033 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19036 cp_token *next = cp_lexer_peek_token (parser->lexer);
19038 *super = *categ = NULL_TREE;
19039 if (next->type == CPP_COLON)
19041 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19042 *super = cp_parser_identifier (parser);
19044 else if (next->type == CPP_OPEN_PAREN)
19046 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19047 *categ = cp_parser_identifier (parser);
19048 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19052 /* Parse an Objective-C class interface. */
19055 cp_parser_objc_class_interface (cp_parser* parser)
19057 tree name, super, categ, protos;
19059 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19060 name = cp_parser_identifier (parser);
19061 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19062 protos = cp_parser_objc_protocol_refs_opt (parser);
19064 /* We have either a class or a category on our hands. */
19066 objc_start_category_interface (name, categ, protos);
19069 objc_start_class_interface (name, super, protos);
19070 /* Handle instance variable declarations, if any. */
19071 cp_parser_objc_class_ivars (parser);
19072 objc_continue_interface ();
19075 cp_parser_objc_method_prototype_list (parser);
19078 /* Parse an Objective-C class implementation. */
19081 cp_parser_objc_class_implementation (cp_parser* parser)
19083 tree name, super, categ;
19085 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19086 name = cp_parser_identifier (parser);
19087 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19089 /* We have either a class or a category on our hands. */
19091 objc_start_category_implementation (name, categ);
19094 objc_start_class_implementation (name, super);
19095 /* Handle instance variable declarations, if any. */
19096 cp_parser_objc_class_ivars (parser);
19097 objc_continue_implementation ();
19100 cp_parser_objc_method_definition_list (parser);
19103 /* Consume the @end token and finish off the implementation. */
19106 cp_parser_objc_end_implementation (cp_parser* parser)
19108 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19109 objc_finish_implementation ();
19112 /* Parse an Objective-C declaration. */
19115 cp_parser_objc_declaration (cp_parser* parser)
19117 /* Try to figure out what kind of declaration is present. */
19118 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19120 switch (kwd->keyword)
19123 cp_parser_objc_alias_declaration (parser);
19126 cp_parser_objc_class_declaration (parser);
19128 case RID_AT_PROTOCOL:
19129 cp_parser_objc_protocol_declaration (parser);
19131 case RID_AT_INTERFACE:
19132 cp_parser_objc_class_interface (parser);
19134 case RID_AT_IMPLEMENTATION:
19135 cp_parser_objc_class_implementation (parser);
19138 cp_parser_objc_end_implementation (parser);
19141 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19142 cp_parser_skip_to_end_of_block_or_statement (parser);
19146 /* Parse an Objective-C try-catch-finally statement.
19148 objc-try-catch-finally-stmt:
19149 @try compound-statement objc-catch-clause-seq [opt]
19150 objc-finally-clause [opt]
19152 objc-catch-clause-seq:
19153 objc-catch-clause objc-catch-clause-seq [opt]
19156 @catch ( exception-declaration ) compound-statement
19158 objc-finally-clause
19159 @finally compound-statement
19161 Returns NULL_TREE. */
19164 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19165 location_t location;
19168 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19169 location = cp_lexer_peek_token (parser->lexer)->location;
19170 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19171 node, lest it get absorbed into the surrounding block. */
19172 stmt = push_stmt_list ();
19173 cp_parser_compound_statement (parser, NULL, false);
19174 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19176 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19178 cp_parameter_declarator *parmdecl;
19181 cp_lexer_consume_token (parser->lexer);
19182 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19183 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19184 parm = grokdeclarator (parmdecl->declarator,
19185 &parmdecl->decl_specifiers,
19186 PARM, /*initialized=*/0,
19187 /*attrlist=*/NULL);
19188 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19189 objc_begin_catch_clause (parm);
19190 cp_parser_compound_statement (parser, NULL, false);
19191 objc_finish_catch_clause ();
19194 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19196 cp_lexer_consume_token (parser->lexer);
19197 location = cp_lexer_peek_token (parser->lexer)->location;
19198 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19199 node, lest it get absorbed into the surrounding block. */
19200 stmt = push_stmt_list ();
19201 cp_parser_compound_statement (parser, NULL, false);
19202 objc_build_finally_clause (location, pop_stmt_list (stmt));
19205 return objc_finish_try_stmt ();
19208 /* Parse an Objective-C synchronized statement.
19210 objc-synchronized-stmt:
19211 @synchronized ( expression ) compound-statement
19213 Returns NULL_TREE. */
19216 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19217 location_t location;
19220 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19222 location = cp_lexer_peek_token (parser->lexer)->location;
19223 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19224 lock = cp_parser_expression (parser, false);
19225 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19227 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19228 node, lest it get absorbed into the surrounding block. */
19229 stmt = push_stmt_list ();
19230 cp_parser_compound_statement (parser, NULL, false);
19232 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19235 /* Parse an Objective-C throw statement.
19238 @throw assignment-expression [opt] ;
19240 Returns a constructed '@throw' statement. */
19243 cp_parser_objc_throw_statement (cp_parser *parser) {
19244 tree expr = NULL_TREE;
19246 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19249 expr = cp_parser_assignment_expression (parser, false);
19251 cp_parser_consume_semicolon_at_end_of_statement (parser);
19253 return objc_build_throw_stmt (expr);
19256 /* Parse an Objective-C statement. */
19259 cp_parser_objc_statement (cp_parser * parser) {
19260 /* Try to figure out what kind of declaration is present. */
19261 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19263 switch (kwd->keyword)
19266 return cp_parser_objc_try_catch_finally_statement (parser);
19267 case RID_AT_SYNCHRONIZED:
19268 return cp_parser_objc_synchronized_statement (parser);
19270 return cp_parser_objc_throw_statement (parser);
19272 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19273 cp_parser_skip_to_end_of_block_or_statement (parser);
19276 return error_mark_node;
19279 /* OpenMP 2.5 parsing routines. */
19281 /* Returns name of the next clause.
19282 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19283 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19284 returned and the token is consumed. */
19286 static pragma_omp_clause
19287 cp_parser_omp_clause_name (cp_parser *parser)
19289 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19292 result = PRAGMA_OMP_CLAUSE_IF;
19293 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19294 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19295 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19296 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19297 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19299 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19300 const char *p = IDENTIFIER_POINTER (id);
19305 if (!strcmp ("copyin", p))
19306 result = PRAGMA_OMP_CLAUSE_COPYIN;
19307 else if (!strcmp ("copyprivate", p))
19308 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19311 if (!strcmp ("firstprivate", p))
19312 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19315 if (!strcmp ("lastprivate", p))
19316 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19319 if (!strcmp ("nowait", p))
19320 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19321 else if (!strcmp ("num_threads", p))
19322 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19325 if (!strcmp ("ordered", p))
19326 result = PRAGMA_OMP_CLAUSE_ORDERED;
19329 if (!strcmp ("reduction", p))
19330 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19333 if (!strcmp ("schedule", p))
19334 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19335 else if (!strcmp ("shared", p))
19336 result = PRAGMA_OMP_CLAUSE_SHARED;
19341 if (result != PRAGMA_OMP_CLAUSE_NONE)
19342 cp_lexer_consume_token (parser->lexer);
19347 /* Validate that a clause of the given type does not already exist. */
19350 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19354 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19355 if (OMP_CLAUSE_CODE (c) == code)
19357 error ("too many %qs clauses", name);
19365 variable-list , identifier
19367 In addition, we match a closing parenthesis. An opening parenthesis
19368 will have been consumed by the caller.
19370 If KIND is nonzero, create the appropriate node and install the decl
19371 in OMP_CLAUSE_DECL and add the node to the head of the list.
19373 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19374 return the list created. */
19377 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19384 name = cp_parser_id_expression (parser, /*template_p=*/false,
19385 /*check_dependency_p=*/true,
19386 /*template_p=*/NULL,
19387 /*declarator_p=*/false,
19388 /*optional_p=*/false);
19389 if (name == error_mark_node)
19392 decl = cp_parser_lookup_name_simple (parser, name);
19393 if (decl == error_mark_node)
19394 cp_parser_name_lookup_error (parser, name, decl, NULL);
19395 else if (kind != 0)
19397 tree u = build_omp_clause (kind);
19398 OMP_CLAUSE_DECL (u) = decl;
19399 OMP_CLAUSE_CHAIN (u) = list;
19403 list = tree_cons (decl, NULL_TREE, list);
19406 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19408 cp_lexer_consume_token (parser->lexer);
19411 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19415 /* Try to resync to an unnested comma. Copied from
19416 cp_parser_parenthesized_expression_list. */
19418 ending = cp_parser_skip_to_closing_parenthesis (parser,
19419 /*recovering=*/true,
19421 /*consume_paren=*/true);
19429 /* Similarly, but expect leading and trailing parenthesis. This is a very
19430 common case for omp clauses. */
19433 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19435 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19436 return cp_parser_omp_var_list_no_open (parser, kind, list);
19441 default ( shared | none ) */
19444 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19446 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19449 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19451 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19453 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19454 const char *p = IDENTIFIER_POINTER (id);
19459 if (strcmp ("none", p) != 0)
19461 kind = OMP_CLAUSE_DEFAULT_NONE;
19465 if (strcmp ("shared", p) != 0)
19467 kind = OMP_CLAUSE_DEFAULT_SHARED;
19474 cp_lexer_consume_token (parser->lexer);
19479 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19482 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19483 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19484 /*or_comma=*/false,
19485 /*consume_paren=*/true);
19487 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19490 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19491 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19492 OMP_CLAUSE_CHAIN (c) = list;
19493 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19499 if ( expression ) */
19502 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19509 t = cp_parser_condition (parser);
19511 if (t == error_mark_node
19512 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19513 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19514 /*or_comma=*/false,
19515 /*consume_paren=*/true);
19517 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19519 c = build_omp_clause (OMP_CLAUSE_IF);
19520 OMP_CLAUSE_IF_EXPR (c) = t;
19521 OMP_CLAUSE_CHAIN (c) = list;
19530 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19534 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19536 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19537 OMP_CLAUSE_CHAIN (c) = list;
19542 num_threads ( expression ) */
19545 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19549 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19552 t = cp_parser_expression (parser, false);
19554 if (t == error_mark_node
19555 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19556 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19557 /*or_comma=*/false,
19558 /*consume_paren=*/true);
19560 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19562 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19563 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19564 OMP_CLAUSE_CHAIN (c) = list;
19573 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19577 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19579 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19580 OMP_CLAUSE_CHAIN (c) = list;
19585 reduction ( reduction-operator : variable-list )
19587 reduction-operator:
19588 One of: + * - & ^ | && || */
19591 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19593 enum tree_code code;
19596 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19599 switch (cp_lexer_peek_token (parser->lexer)->type)
19611 code = BIT_AND_EXPR;
19614 code = BIT_XOR_EXPR;
19617 code = BIT_IOR_EXPR;
19620 code = TRUTH_ANDIF_EXPR;
19623 code = TRUTH_ORIF_EXPR;
19626 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19628 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19629 /*or_comma=*/false,
19630 /*consume_paren=*/true);
19633 cp_lexer_consume_token (parser->lexer);
19635 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19638 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19639 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19640 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19646 schedule ( schedule-kind )
19647 schedule ( schedule-kind , expression )
19650 static | dynamic | guided | runtime */
19653 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19657 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19660 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19662 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19664 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19665 const char *p = IDENTIFIER_POINTER (id);
19670 if (strcmp ("dynamic", p) != 0)
19672 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19676 if (strcmp ("guided", p) != 0)
19678 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19682 if (strcmp ("runtime", p) != 0)
19684 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19691 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19692 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19695 cp_lexer_consume_token (parser->lexer);
19697 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19699 cp_lexer_consume_token (parser->lexer);
19701 t = cp_parser_assignment_expression (parser, false);
19703 if (t == error_mark_node)
19705 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19706 error ("schedule %<runtime%> does not take "
19707 "a %<chunk_size%> parameter");
19709 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19711 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19714 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19717 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19718 OMP_CLAUSE_CHAIN (c) = list;
19722 cp_parser_error (parser, "invalid schedule kind");
19724 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19725 /*or_comma=*/false,
19726 /*consume_paren=*/true);
19730 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19731 is a bitmask in MASK. Return the list of clauses found; the result
19732 of clause default goes in *pdefault. */
19735 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19736 const char *where, cp_token *pragma_tok)
19738 tree clauses = NULL;
19741 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19743 pragma_omp_clause c_kind;
19744 const char *c_name;
19745 tree prev = clauses;
19747 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19748 cp_lexer_consume_token (parser->lexer);
19750 c_kind = cp_parser_omp_clause_name (parser);
19755 case PRAGMA_OMP_CLAUSE_COPYIN:
19756 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19759 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19760 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19762 c_name = "copyprivate";
19764 case PRAGMA_OMP_CLAUSE_DEFAULT:
19765 clauses = cp_parser_omp_clause_default (parser, clauses);
19766 c_name = "default";
19768 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19769 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19771 c_name = "firstprivate";
19773 case PRAGMA_OMP_CLAUSE_IF:
19774 clauses = cp_parser_omp_clause_if (parser, clauses);
19777 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19778 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19780 c_name = "lastprivate";
19782 case PRAGMA_OMP_CLAUSE_NOWAIT:
19783 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19786 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19787 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19788 c_name = "num_threads";
19790 case PRAGMA_OMP_CLAUSE_ORDERED:
19791 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19792 c_name = "ordered";
19794 case PRAGMA_OMP_CLAUSE_PRIVATE:
19795 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19797 c_name = "private";
19799 case PRAGMA_OMP_CLAUSE_REDUCTION:
19800 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19801 c_name = "reduction";
19803 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19804 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19805 c_name = "schedule";
19807 case PRAGMA_OMP_CLAUSE_SHARED:
19808 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19813 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19817 if (((mask >> c_kind) & 1) == 0)
19819 /* Remove the invalid clause(s) from the list to avoid
19820 confusing the rest of the compiler. */
19822 error ("%qs is not valid for %qs", c_name, where);
19826 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19827 return finish_omp_clauses (clauses);
19834 In practice, we're also interested in adding the statement to an
19835 outer node. So it is convenient if we work around the fact that
19836 cp_parser_statement calls add_stmt. */
19839 cp_parser_begin_omp_structured_block (cp_parser *parser)
19841 unsigned save = parser->in_statement;
19843 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19844 This preserves the "not within loop or switch" style error messages
19845 for nonsense cases like
19851 if (parser->in_statement)
19852 parser->in_statement = IN_OMP_BLOCK;
19858 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19860 parser->in_statement = save;
19864 cp_parser_omp_structured_block (cp_parser *parser)
19866 tree stmt = begin_omp_structured_block ();
19867 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19869 cp_parser_statement (parser, NULL_TREE, false, NULL);
19871 cp_parser_end_omp_structured_block (parser, save);
19872 return finish_omp_structured_block (stmt);
19876 # pragma omp atomic new-line
19880 x binop= expr | x++ | ++x | x-- | --x
19882 +, *, -, /, &, ^, |, <<, >>
19884 where x is an lvalue expression with scalar type. */
19887 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19890 enum tree_code code;
19892 cp_parser_require_pragma_eol (parser, pragma_tok);
19894 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19896 switch (TREE_CODE (lhs))
19901 case PREINCREMENT_EXPR:
19902 case POSTINCREMENT_EXPR:
19903 lhs = TREE_OPERAND (lhs, 0);
19905 rhs = integer_one_node;
19908 case PREDECREMENT_EXPR:
19909 case POSTDECREMENT_EXPR:
19910 lhs = TREE_OPERAND (lhs, 0);
19912 rhs = integer_one_node;
19916 switch (cp_lexer_peek_token (parser->lexer)->type)
19922 code = TRUNC_DIV_EXPR;
19930 case CPP_LSHIFT_EQ:
19931 code = LSHIFT_EXPR;
19933 case CPP_RSHIFT_EQ:
19934 code = RSHIFT_EXPR;
19937 code = BIT_AND_EXPR;
19940 code = BIT_IOR_EXPR;
19943 code = BIT_XOR_EXPR;
19946 cp_parser_error (parser,
19947 "invalid operator for %<#pragma omp atomic%>");
19950 cp_lexer_consume_token (parser->lexer);
19952 rhs = cp_parser_expression (parser, false);
19953 if (rhs == error_mark_node)
19957 finish_omp_atomic (code, lhs, rhs);
19958 cp_parser_consume_semicolon_at_end_of_statement (parser);
19962 cp_parser_skip_to_end_of_block_or_statement (parser);
19967 # pragma omp barrier new-line */
19970 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19972 cp_parser_require_pragma_eol (parser, pragma_tok);
19973 finish_omp_barrier ();
19977 # pragma omp critical [(name)] new-line
19978 structured-block */
19981 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19983 tree stmt, name = NULL;
19985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19987 cp_lexer_consume_token (parser->lexer);
19989 name = cp_parser_identifier (parser);
19991 if (name == error_mark_node
19992 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19994 /*or_comma=*/false,
19995 /*consume_paren=*/true);
19996 if (name == error_mark_node)
19999 cp_parser_require_pragma_eol (parser, pragma_tok);
20001 stmt = cp_parser_omp_structured_block (parser);
20002 return c_finish_omp_critical (stmt, name);
20006 # pragma omp flush flush-vars[opt] new-line
20009 ( variable-list ) */
20012 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20015 (void) cp_parser_omp_var_list (parser, 0, NULL);
20016 cp_parser_require_pragma_eol (parser, pragma_tok);
20018 finish_omp_flush ();
20021 /* Parse the restricted form of the for statment allowed by OpenMP. */
20024 cp_parser_omp_for_loop (cp_parser *parser)
20026 tree init, cond, incr, body, decl, pre_body;
20029 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20031 cp_parser_error (parser, "for statement expected");
20034 loc = cp_lexer_consume_token (parser->lexer)->location;
20035 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20038 init = decl = NULL;
20039 pre_body = push_stmt_list ();
20040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20042 cp_decl_specifier_seq type_specifiers;
20044 /* First, try to parse as an initialized declaration. See
20045 cp_parser_condition, from whence the bulk of this is copied. */
20047 cp_parser_parse_tentatively (parser);
20048 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20050 if (!cp_parser_error_occurred (parser))
20052 tree asm_specification, attributes;
20053 cp_declarator *declarator;
20055 declarator = cp_parser_declarator (parser,
20056 CP_PARSER_DECLARATOR_NAMED,
20057 /*ctor_dtor_or_conv_p=*/NULL,
20058 /*parenthesized_p=*/NULL,
20059 /*member_p=*/false);
20060 attributes = cp_parser_attributes_opt (parser);
20061 asm_specification = cp_parser_asm_specification_opt (parser);
20063 cp_parser_require (parser, CPP_EQ, "`='");
20064 if (cp_parser_parse_definitely (parser))
20068 decl = start_decl (declarator, &type_specifiers,
20069 /*initialized_p=*/false, attributes,
20070 /*prefix_attributes=*/NULL_TREE,
20073 init = cp_parser_assignment_expression (parser, false);
20075 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20076 asm_specification, LOOKUP_ONLYCONVERTING);
20079 pop_scope (pushed_scope);
20083 cp_parser_abort_tentative_parse (parser);
20085 /* If parsing as an initialized declaration failed, try again as
20086 a simple expression. */
20088 init = cp_parser_expression (parser, false);
20090 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20091 pre_body = pop_stmt_list (pre_body);
20094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20095 cond = cp_parser_condition (parser);
20096 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20100 incr = cp_parser_expression (parser, false);
20102 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20103 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20104 /*or_comma=*/false,
20105 /*consume_paren=*/true);
20107 /* Note that we saved the original contents of this flag when we entered
20108 the structured block, and so we don't need to re-save it here. */
20109 parser->in_statement = IN_OMP_FOR;
20111 /* Note that the grammar doesn't call for a structured block here,
20112 though the loop as a whole is a structured block. */
20113 body = push_stmt_list ();
20114 cp_parser_statement (parser, NULL_TREE, false, NULL);
20115 body = pop_stmt_list (body);
20117 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20121 #pragma omp for for-clause[optseq] new-line
20124 #define OMP_FOR_CLAUSE_MASK \
20125 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20126 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20127 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20128 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20129 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20130 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20131 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20134 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20136 tree clauses, sb, ret;
20139 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20140 "#pragma omp for", pragma_tok);
20142 sb = begin_omp_structured_block ();
20143 save = cp_parser_begin_omp_structured_block (parser);
20145 ret = cp_parser_omp_for_loop (parser);
20147 OMP_FOR_CLAUSES (ret) = clauses;
20149 cp_parser_end_omp_structured_block (parser, save);
20150 add_stmt (finish_omp_structured_block (sb));
20156 # pragma omp master new-line
20157 structured-block */
20160 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20162 cp_parser_require_pragma_eol (parser, pragma_tok);
20163 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20167 # pragma omp ordered new-line
20168 structured-block */
20171 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20173 cp_parser_require_pragma_eol (parser, pragma_tok);
20174 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20180 { section-sequence }
20183 section-directive[opt] structured-block
20184 section-sequence section-directive structured-block */
20187 cp_parser_omp_sections_scope (cp_parser *parser)
20189 tree stmt, substmt;
20190 bool error_suppress = false;
20193 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20196 stmt = push_stmt_list ();
20198 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20202 substmt = begin_omp_structured_block ();
20203 save = cp_parser_begin_omp_structured_block (parser);
20207 cp_parser_statement (parser, NULL_TREE, false, NULL);
20209 tok = cp_lexer_peek_token (parser->lexer);
20210 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20212 if (tok->type == CPP_CLOSE_BRACE)
20214 if (tok->type == CPP_EOF)
20218 cp_parser_end_omp_structured_block (parser, save);
20219 substmt = finish_omp_structured_block (substmt);
20220 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20221 add_stmt (substmt);
20226 tok = cp_lexer_peek_token (parser->lexer);
20227 if (tok->type == CPP_CLOSE_BRACE)
20229 if (tok->type == CPP_EOF)
20232 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20234 cp_lexer_consume_token (parser->lexer);
20235 cp_parser_require_pragma_eol (parser, tok);
20236 error_suppress = false;
20238 else if (!error_suppress)
20240 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20241 error_suppress = true;
20244 substmt = cp_parser_omp_structured_block (parser);
20245 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20246 add_stmt (substmt);
20248 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20250 substmt = pop_stmt_list (stmt);
20252 stmt = make_node (OMP_SECTIONS);
20253 TREE_TYPE (stmt) = void_type_node;
20254 OMP_SECTIONS_BODY (stmt) = substmt;
20261 # pragma omp sections sections-clause[optseq] newline
20264 #define OMP_SECTIONS_CLAUSE_MASK \
20265 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20266 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20267 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20268 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20269 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20272 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20276 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20277 "#pragma omp sections", pragma_tok);
20279 ret = cp_parser_omp_sections_scope (parser);
20281 OMP_SECTIONS_CLAUSES (ret) = clauses;
20287 # pragma parallel parallel-clause new-line
20288 # pragma parallel for parallel-for-clause new-line
20289 # pragma parallel sections parallel-sections-clause new-line */
20291 #define OMP_PARALLEL_CLAUSE_MASK \
20292 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20293 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20294 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20295 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20296 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20297 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20298 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20299 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20302 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20304 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20305 const char *p_name = "#pragma omp parallel";
20306 tree stmt, clauses, par_clause, ws_clause, block;
20307 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20310 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20312 cp_lexer_consume_token (parser->lexer);
20313 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20314 p_name = "#pragma omp parallel for";
20315 mask |= OMP_FOR_CLAUSE_MASK;
20316 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20318 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20320 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20321 const char *p = IDENTIFIER_POINTER (id);
20322 if (strcmp (p, "sections") == 0)
20324 cp_lexer_consume_token (parser->lexer);
20325 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20326 p_name = "#pragma omp parallel sections";
20327 mask |= OMP_SECTIONS_CLAUSE_MASK;
20328 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20332 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20333 block = begin_omp_parallel ();
20334 save = cp_parser_begin_omp_structured_block (parser);
20338 case PRAGMA_OMP_PARALLEL:
20339 cp_parser_statement (parser, NULL_TREE, false, NULL);
20340 par_clause = clauses;
20343 case PRAGMA_OMP_PARALLEL_FOR:
20344 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20345 stmt = cp_parser_omp_for_loop (parser);
20347 OMP_FOR_CLAUSES (stmt) = ws_clause;
20350 case PRAGMA_OMP_PARALLEL_SECTIONS:
20351 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20352 stmt = cp_parser_omp_sections_scope (parser);
20354 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20358 gcc_unreachable ();
20361 cp_parser_end_omp_structured_block (parser, save);
20362 stmt = finish_omp_parallel (par_clause, block);
20363 if (p_kind != PRAGMA_OMP_PARALLEL)
20364 OMP_PARALLEL_COMBINED (stmt) = 1;
20369 # pragma omp single single-clause[optseq] new-line
20370 structured-block */
20372 #define OMP_SINGLE_CLAUSE_MASK \
20373 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20374 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20375 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20376 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20379 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20381 tree stmt = make_node (OMP_SINGLE);
20382 TREE_TYPE (stmt) = void_type_node;
20384 OMP_SINGLE_CLAUSES (stmt)
20385 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20386 "#pragma omp single", pragma_tok);
20387 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20389 return add_stmt (stmt);
20393 # pragma omp threadprivate (variable-list) */
20396 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20400 vars = cp_parser_omp_var_list (parser, 0, NULL);
20401 cp_parser_require_pragma_eol (parser, pragma_tok);
20403 finish_omp_threadprivate (vars);
20406 /* Main entry point to OpenMP statement pragmas. */
20409 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20413 switch (pragma_tok->pragma_kind)
20415 case PRAGMA_OMP_ATOMIC:
20416 cp_parser_omp_atomic (parser, pragma_tok);
20418 case PRAGMA_OMP_CRITICAL:
20419 stmt = cp_parser_omp_critical (parser, pragma_tok);
20421 case PRAGMA_OMP_FOR:
20422 stmt = cp_parser_omp_for (parser, pragma_tok);
20424 case PRAGMA_OMP_MASTER:
20425 stmt = cp_parser_omp_master (parser, pragma_tok);
20427 case PRAGMA_OMP_ORDERED:
20428 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20430 case PRAGMA_OMP_PARALLEL:
20431 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20433 case PRAGMA_OMP_SECTIONS:
20434 stmt = cp_parser_omp_sections (parser, pragma_tok);
20436 case PRAGMA_OMP_SINGLE:
20437 stmt = cp_parser_omp_single (parser, pragma_tok);
20440 gcc_unreachable ();
20444 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20449 static GTY (()) cp_parser *the_parser;
20452 /* Special handling for the first token or line in the file. The first
20453 thing in the file might be #pragma GCC pch_preprocess, which loads a
20454 PCH file, which is a GC collection point. So we need to handle this
20455 first pragma without benefit of an existing lexer structure.
20457 Always returns one token to the caller in *FIRST_TOKEN. This is
20458 either the true first token of the file, or the first token after
20459 the initial pragma. */
20462 cp_parser_initial_pragma (cp_token *first_token)
20466 cp_lexer_get_preprocessor_token (NULL, first_token);
20467 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20470 cp_lexer_get_preprocessor_token (NULL, first_token);
20471 if (first_token->type == CPP_STRING)
20473 name = first_token->u.value;
20475 cp_lexer_get_preprocessor_token (NULL, first_token);
20476 if (first_token->type != CPP_PRAGMA_EOL)
20477 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20480 error ("expected string literal");
20482 /* Skip to the end of the pragma. */
20483 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20484 cp_lexer_get_preprocessor_token (NULL, first_token);
20486 /* Now actually load the PCH file. */
20488 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20490 /* Read one more token to return to our caller. We have to do this
20491 after reading the PCH file in, since its pointers have to be
20493 cp_lexer_get_preprocessor_token (NULL, first_token);
20496 /* Normal parsing of a pragma token. Here we can (and must) use the
20500 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20502 cp_token *pragma_tok;
20505 pragma_tok = cp_lexer_consume_token (parser->lexer);
20506 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20507 parser->lexer->in_pragma = true;
20509 id = pragma_tok->pragma_kind;
20512 case PRAGMA_GCC_PCH_PREPROCESS:
20513 error ("%<#pragma GCC pch_preprocess%> must be first");
20516 case PRAGMA_OMP_BARRIER:
20519 case pragma_compound:
20520 cp_parser_omp_barrier (parser, pragma_tok);
20523 error ("%<#pragma omp barrier%> may only be "
20524 "used in compound statements");
20531 case PRAGMA_OMP_FLUSH:
20534 case pragma_compound:
20535 cp_parser_omp_flush (parser, pragma_tok);
20538 error ("%<#pragma omp flush%> may only be "
20539 "used in compound statements");
20546 case PRAGMA_OMP_THREADPRIVATE:
20547 cp_parser_omp_threadprivate (parser, pragma_tok);
20550 case PRAGMA_OMP_ATOMIC:
20551 case PRAGMA_OMP_CRITICAL:
20552 case PRAGMA_OMP_FOR:
20553 case PRAGMA_OMP_MASTER:
20554 case PRAGMA_OMP_ORDERED:
20555 case PRAGMA_OMP_PARALLEL:
20556 case PRAGMA_OMP_SECTIONS:
20557 case PRAGMA_OMP_SINGLE:
20558 if (context == pragma_external)
20560 cp_parser_omp_construct (parser, pragma_tok);
20563 case PRAGMA_OMP_SECTION:
20564 error ("%<#pragma omp section%> may only be used in "
20565 "%<#pragma omp sections%> construct");
20569 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20570 c_invoke_pragma_handler (id);
20574 cp_parser_error (parser, "expected declaration specifiers");
20578 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20582 /* The interface the pragma parsers have to the lexer. */
20585 pragma_lex (tree *value)
20588 enum cpp_ttype ret;
20590 tok = cp_lexer_peek_token (the_parser->lexer);
20593 *value = tok->u.value;
20595 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20597 else if (ret == CPP_STRING)
20598 *value = cp_parser_string_literal (the_parser, false, false);
20601 cp_lexer_consume_token (the_parser->lexer);
20602 if (ret == CPP_KEYWORD)
20610 /* External interface. */
20612 /* Parse one entire translation unit. */
20615 c_parse_file (void)
20617 bool error_occurred;
20618 static bool already_called = false;
20620 if (already_called)
20622 sorry ("inter-module optimizations not implemented for C++");
20625 already_called = true;
20627 the_parser = cp_parser_new ();
20628 push_deferring_access_checks (flag_access_control
20629 ? dk_no_deferred : dk_no_check);
20630 error_occurred = cp_parser_translation_unit (the_parser);
20634 #include "gt-cp-parser.h"