2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The input file stack index at which this token was found. */
83 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
84 /* The value associated with this token, if any. */
85 union cp_token_value {
86 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
87 struct tree_check* GTY((tag ("1"))) tree_check_value;
88 /* Use for all other tokens. */
89 tree GTY((tag ("0"))) value;
90 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91 /* The location at which this token was found. */
95 /* We use a stack of token pointer for saving token sets. */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
100 static cp_token eof_token =
102 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
103 #if USE_MAPPED_LOCATION
110 /* The cp_lexer structure represents the C++ lexer. It is responsible
111 for managing the token stream from the preprocessor and supplying
112 it to the parser. Tokens are never added to the cp_lexer after
115 typedef struct cp_lexer GTY (())
117 /* The memory allocated for the buffer. NULL if this lexer does not
118 own the token buffer. */
119 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
120 /* If the lexer owns the buffer, this is the number of tokens in the
122 size_t buffer_length;
124 /* A pointer just past the last available token. The tokens
125 in this lexer are [buffer, last_token). */
126 cp_token_position GTY ((skip)) last_token;
128 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
129 no more available tokens. */
130 cp_token_position GTY ((skip)) next_token;
132 /* A stack indicating positions at which cp_lexer_save_tokens was
133 called. The top entry is the most recent position at which we
134 began saving tokens. If the stack is non-empty, we are saving
136 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138 /* The next lexer in a linked list of lexers. */
139 struct cp_lexer *next;
141 /* True if we should output debugging information. */
144 /* True if we're in the context of parsing a pragma, and should not
145 increment past the end-of-line marker. */
149 /* cp_token_cache is a range of tokens. There is no need to represent
150 allocate heap memory for it, since tokens are never removed from the
151 lexer's array. There is also no need for the GC to walk through
152 a cp_token_cache, since everything in here is referenced through
155 typedef struct cp_token_cache GTY(())
157 /* The beginning of the token range. */
158 cp_token * GTY((skip)) first;
160 /* Points immediately after the last token in the range. */
161 cp_token * GTY ((skip)) last;
166 static cp_lexer *cp_lexer_new_main
168 static cp_lexer *cp_lexer_new_from_tokens
169 (cp_token_cache *tokens);
170 static void cp_lexer_destroy
172 static int cp_lexer_saving_tokens
174 static cp_token_position cp_lexer_token_position
176 static cp_token *cp_lexer_token_at
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_get_preprocessor_token
179 (cp_lexer *, cp_token *);
180 static inline cp_token *cp_lexer_peek_token
182 static cp_token *cp_lexer_peek_nth_token
183 (cp_lexer *, size_t);
184 static inline bool cp_lexer_next_token_is
185 (cp_lexer *, enum cpp_ttype);
186 static bool cp_lexer_next_token_is_not
187 (cp_lexer *, enum cpp_ttype);
188 static bool cp_lexer_next_token_is_keyword
189 (cp_lexer *, enum rid);
190 static cp_token *cp_lexer_consume_token
192 static void cp_lexer_purge_token
194 static void cp_lexer_purge_tokens_after
195 (cp_lexer *, cp_token_position);
196 static void cp_lexer_save_tokens
198 static void cp_lexer_commit_tokens
200 static void cp_lexer_rollback_tokens
202 #ifdef ENABLE_CHECKING
203 static void cp_lexer_print_token
204 (FILE *, cp_token *);
205 static inline bool cp_lexer_debugging_p
207 static void cp_lexer_start_debugging
208 (cp_lexer *) ATTRIBUTE_UNUSED;
209 static void cp_lexer_stop_debugging
210 (cp_lexer *) ATTRIBUTE_UNUSED;
212 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
213 about passing NULL to functions that require non-NULL arguments
214 (fputs, fprintf). It will never be used, so all we need is a value
215 of the right type that's guaranteed not to be NULL. */
216 #define cp_lexer_debug_stream stdout
217 #define cp_lexer_print_token(str, tok) (void) 0
218 #define cp_lexer_debugging_p(lexer) 0
219 #endif /* ENABLE_CHECKING */
221 static cp_token_cache *cp_token_cache_new
222 (cp_token *, cp_token *);
224 static void cp_parser_initial_pragma
227 /* Manifest constants. */
228 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
229 #define CP_SAVED_TOKEN_STACK 5
231 /* A token type for keywords, as opposed to ordinary identifiers. */
232 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234 /* A token type for template-ids. If a template-id is processed while
235 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
236 the value of the CPP_TEMPLATE_ID is whatever was returned by
237 cp_parser_template_id. */
238 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240 /* A token type for nested-name-specifiers. If a
241 nested-name-specifier is processed while parsing tentatively, it is
242 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
243 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
244 cp_parser_nested_name_specifier_opt. */
245 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247 /* A token type for tokens that are not tokens at all; these are used
248 to represent slots in the array where there used to be a token
249 that has now been deleted. */
250 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252 /* The number of token types, including C++-specific ones. */
253 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
257 #ifdef ENABLE_CHECKING
258 /* The stream to which debugging output should be written. */
259 static FILE *cp_lexer_debug_stream;
260 #endif /* ENABLE_CHECKING */
262 /* Create a new main C++ lexer, the lexer that gets tokens from the
266 cp_lexer_new_main (void)
268 cp_token first_token;
275 /* It's possible that parsing the first pragma will load a PCH file,
276 which is a GC collection point. So we have to do that before
277 allocating any memory. */
278 cp_parser_initial_pragma (&first_token);
280 /* Tell c_lex_with_flags not to merge string constants. */
281 c_lex_return_raw_strings = true;
283 c_common_no_more_pch ();
285 /* Allocate the memory. */
286 lexer = GGC_CNEW (cp_lexer);
288 #ifdef ENABLE_CHECKING
289 /* Initially we are not debugging. */
290 lexer->debugging_p = false;
291 #endif /* ENABLE_CHECKING */
292 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
293 CP_SAVED_TOKEN_STACK);
295 /* Create the buffer. */
296 alloc = CP_LEXER_BUFFER_SIZE;
297 buffer = GGC_NEWVEC (cp_token, alloc);
299 /* Put the first token in the buffer. */
304 /* Get the remaining tokens from the preprocessor. */
305 while (pos->type != CPP_EOF)
312 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
313 pos = buffer + space;
315 cp_lexer_get_preprocessor_token (lexer, pos);
317 lexer->buffer = buffer;
318 lexer->buffer_length = alloc - space;
319 lexer->last_token = pos;
320 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
322 /* Subsequent preprocessor diagnostics should use compiler
323 diagnostic functions to get the compiler source location. */
324 cpp_get_options (parse_in)->client_diagnostic = true;
325 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
327 gcc_assert (lexer->next_token->type != CPP_PURGED);
331 /* Create a new lexer whose token stream is primed with the tokens in
332 CACHE. When these tokens are exhausted, no new tokens will be read. */
335 cp_lexer_new_from_tokens (cp_token_cache *cache)
337 cp_token *first = cache->first;
338 cp_token *last = cache->last;
339 cp_lexer *lexer = GGC_CNEW (cp_lexer);
341 /* We do not own the buffer. */
342 lexer->buffer = NULL;
343 lexer->buffer_length = 0;
344 lexer->next_token = first == last ? &eof_token : first;
345 lexer->last_token = last;
347 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
348 CP_SAVED_TOKEN_STACK);
350 #ifdef ENABLE_CHECKING
351 /* Initially we are not debugging. */
352 lexer->debugging_p = false;
355 gcc_assert (lexer->next_token->type != CPP_PURGED);
359 /* Frees all resources associated with LEXER. */
362 cp_lexer_destroy (cp_lexer *lexer)
365 ggc_free (lexer->buffer);
366 VEC_free (cp_token_position, heap, lexer->saved_tokens);
370 /* Returns nonzero if debugging information should be output. */
372 #ifdef ENABLE_CHECKING
375 cp_lexer_debugging_p (cp_lexer *lexer)
377 return lexer->debugging_p;
380 #endif /* ENABLE_CHECKING */
382 static inline cp_token_position
383 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
385 gcc_assert (!previous_p || lexer->next_token != &eof_token);
387 return lexer->next_token - previous_p;
390 static inline cp_token *
391 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
396 /* nonzero if we are presently saving tokens. */
399 cp_lexer_saving_tokens (const cp_lexer* lexer)
401 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
404 /* Store the next token from the preprocessor in *TOKEN. Return true
408 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
411 static int is_extern_c = 0;
413 /* Get a new token from the preprocessor. */
415 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
416 token->input_file_stack_index = input_file_stack_tick;
417 token->keyword = RID_MAX;
418 token->pragma_kind = PRAGMA_NONE;
419 token->in_system_header = in_system_header;
421 /* On some systems, some header files are surrounded by an
422 implicit extern "C" block. Set a flag in the token if it
423 comes from such a header. */
424 is_extern_c += pending_lang_change;
425 pending_lang_change = 0;
426 token->implicit_extern_c = is_extern_c > 0;
428 /* Check to see if this token is a keyword. */
429 if (token->type == CPP_NAME)
431 if (C_IS_RESERVED_WORD (token->u.value))
433 /* Mark this token as a keyword. */
434 token->type = CPP_KEYWORD;
435 /* Record which keyword. */
436 token->keyword = C_RID_CODE (token->u.value);
437 /* Update the value. Some keywords are mapped to particular
438 entities, rather than simply having the value of the
439 corresponding IDENTIFIER_NODE. For example, `__const' is
440 mapped to `const'. */
441 token->u.value = ridpointers[token->keyword];
445 if (warn_cxx0x_compat
446 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
447 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
449 /* Warn about the C++0x keyword (but still treat it as
451 warning (OPT_Wc__0x_compat,
452 "identifier %<%s%> will become a keyword in C++0x",
453 IDENTIFIER_POINTER (token->u.value));
455 /* Clear out the C_RID_CODE so we don't warn about this
456 particular identifier-turned-keyword again. */
457 C_RID_CODE (token->u.value) = RID_MAX;
460 token->ambiguous_p = false;
461 token->keyword = RID_MAX;
464 /* Handle Objective-C++ keywords. */
465 else if (token->type == CPP_AT_NAME)
467 token->type = CPP_KEYWORD;
468 switch (C_RID_CODE (token->u.value))
470 /* Map 'class' to '@class', 'private' to '@private', etc. */
471 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
472 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
473 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
474 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
475 case RID_THROW: token->keyword = RID_AT_THROW; break;
476 case RID_TRY: token->keyword = RID_AT_TRY; break;
477 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
478 default: token->keyword = C_RID_CODE (token->u.value);
481 else if (token->type == CPP_PRAGMA)
483 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
484 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
485 token->u.value = NULL_TREE;
489 /* Update the globals input_location and in_system_header and the
490 input file stack from TOKEN. */
492 cp_lexer_set_source_position_from_token (cp_token *token)
494 if (token->type != CPP_EOF)
496 input_location = token->location;
497 in_system_header = token->in_system_header;
498 restore_input_file_stack (token->input_file_stack_index);
502 /* Return a pointer to the next token in the token stream, but do not
505 static inline cp_token *
506 cp_lexer_peek_token (cp_lexer *lexer)
508 if (cp_lexer_debugging_p (lexer))
510 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
511 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
512 putc ('\n', cp_lexer_debug_stream);
514 return lexer->next_token;
517 /* Return true if the next token has the indicated TYPE. */
520 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
522 return cp_lexer_peek_token (lexer)->type == type;
525 /* Return true if the next token does not have the indicated TYPE. */
528 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
530 return !cp_lexer_next_token_is (lexer, type);
533 /* Return true if the next token is the indicated KEYWORD. */
536 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
538 return cp_lexer_peek_token (lexer)->keyword == keyword;
541 /* Return true if the next token is a keyword for a decl-specifier. */
544 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
548 token = cp_lexer_peek_token (lexer);
549 switch (token->keyword)
551 /* Storage classes. */
558 /* Elaborated type specifiers. */
564 /* Simple type specifiers. */
576 /* GNU extensions. */
579 /* C++0x extensions. */
588 /* Return a pointer to the Nth token in the token stream. If N is 1,
589 then this is precisely equivalent to cp_lexer_peek_token (except
590 that it is not inline). One would like to disallow that case, but
591 there is one case (cp_parser_nth_token_starts_template_id) where
592 the caller passes a variable for N and it might be 1. */
595 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
599 /* N is 1-based, not zero-based. */
602 if (cp_lexer_debugging_p (lexer))
603 fprintf (cp_lexer_debug_stream,
604 "cp_lexer: peeking ahead %ld at token: ", (long)n);
607 token = lexer->next_token;
608 gcc_assert (!n || token != &eof_token);
612 if (token == lexer->last_token)
618 if (token->type != CPP_PURGED)
622 if (cp_lexer_debugging_p (lexer))
624 cp_lexer_print_token (cp_lexer_debug_stream, token);
625 putc ('\n', cp_lexer_debug_stream);
631 /* Return the next token, and advance the lexer's next_token pointer
632 to point to the next non-purged token. */
635 cp_lexer_consume_token (cp_lexer* lexer)
637 cp_token *token = lexer->next_token;
639 gcc_assert (token != &eof_token);
640 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
645 if (lexer->next_token == lexer->last_token)
647 lexer->next_token = &eof_token;
652 while (lexer->next_token->type == CPP_PURGED);
654 cp_lexer_set_source_position_from_token (token);
656 /* Provide debugging output. */
657 if (cp_lexer_debugging_p (lexer))
659 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
660 cp_lexer_print_token (cp_lexer_debug_stream, token);
661 putc ('\n', cp_lexer_debug_stream);
667 /* Permanently remove the next token from the token stream, and
668 advance the next_token pointer to refer to the next non-purged
672 cp_lexer_purge_token (cp_lexer *lexer)
674 cp_token *tok = lexer->next_token;
676 gcc_assert (tok != &eof_token);
677 tok->type = CPP_PURGED;
678 tok->location = UNKNOWN_LOCATION;
679 tok->u.value = NULL_TREE;
680 tok->keyword = RID_MAX;
685 if (tok == lexer->last_token)
691 while (tok->type == CPP_PURGED);
692 lexer->next_token = tok;
695 /* Permanently remove all tokens after TOK, up to, but not
696 including, the token that will be returned next by
697 cp_lexer_peek_token. */
700 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
702 cp_token *peek = lexer->next_token;
704 if (peek == &eof_token)
705 peek = lexer->last_token;
707 gcc_assert (tok < peek);
709 for ( tok += 1; tok != peek; tok += 1)
711 tok->type = CPP_PURGED;
712 tok->location = UNKNOWN_LOCATION;
713 tok->u.value = NULL_TREE;
714 tok->keyword = RID_MAX;
718 /* Begin saving tokens. All tokens consumed after this point will be
722 cp_lexer_save_tokens (cp_lexer* lexer)
724 /* Provide debugging output. */
725 if (cp_lexer_debugging_p (lexer))
726 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
728 VEC_safe_push (cp_token_position, heap,
729 lexer->saved_tokens, lexer->next_token);
732 /* Commit to the portion of the token stream most recently saved. */
735 cp_lexer_commit_tokens (cp_lexer* lexer)
737 /* Provide debugging output. */
738 if (cp_lexer_debugging_p (lexer))
739 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
741 VEC_pop (cp_token_position, lexer->saved_tokens);
744 /* Return all tokens saved since the last call to cp_lexer_save_tokens
745 to the token stream. Stop saving tokens. */
748 cp_lexer_rollback_tokens (cp_lexer* lexer)
750 /* Provide debugging output. */
751 if (cp_lexer_debugging_p (lexer))
752 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
754 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
757 /* Print a representation of the TOKEN on the STREAM. */
759 #ifdef ENABLE_CHECKING
762 cp_lexer_print_token (FILE * stream, cp_token *token)
764 /* We don't use cpp_type2name here because the parser defines
765 a few tokens of its own. */
766 static const char *const token_names[] = {
767 /* cpplib-defined token types */
773 /* C++ parser token types - see "Manifest constants", above. */
776 "NESTED_NAME_SPECIFIER",
780 /* If we have a name for the token, print it out. Otherwise, we
781 simply give the numeric code. */
782 gcc_assert (token->type < ARRAY_SIZE(token_names));
783 fputs (token_names[token->type], stream);
785 /* For some tokens, print the associated data. */
789 /* Some keywords have a value that is not an IDENTIFIER_NODE.
790 For example, `struct' is mapped to an INTEGER_CST. */
791 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
793 /* else fall through */
795 fputs (IDENTIFIER_POINTER (token->u.value), stream);
800 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
808 /* Start emitting debugging information. */
811 cp_lexer_start_debugging (cp_lexer* lexer)
813 lexer->debugging_p = true;
816 /* Stop emitting debugging information. */
819 cp_lexer_stop_debugging (cp_lexer* lexer)
821 lexer->debugging_p = false;
824 #endif /* ENABLE_CHECKING */
826 /* Create a new cp_token_cache, representing a range of tokens. */
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
831 cp_token_cache *cache = GGC_NEW (cp_token_cache);
832 cache->first = first;
838 /* Decl-specifiers. */
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
845 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
850 /* Nothing other than the parser should be creating declarators;
851 declarators are a semi-syntactic representation of C++ entities.
852 Other parts of the front end that need to create entities (like
853 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
855 static cp_declarator *make_call_declarator
856 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
857 static cp_declarator *make_array_declarator
858 (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860 (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862 (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864 (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866 (cp_cv_quals, tree, cp_declarator *);
868 /* An erroneous declarator. */
869 static cp_declarator *cp_error_declarator;
871 /* The obstack on which declarators and related data structures are
873 static struct obstack declarator_obstack;
875 /* Alloc BYTES from the declarator memory pool. */
878 alloc_declarator (size_t bytes)
880 return obstack_alloc (&declarator_obstack, bytes);
883 /* Allocate a declarator of the indicated KIND. Clear fields that are
884 common to all declarators. */
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
889 cp_declarator *declarator;
891 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892 declarator->kind = kind;
893 declarator->attributes = NULL_TREE;
894 declarator->declarator = NULL;
895 declarator->parameter_pack_p = false;
900 /* Make a declarator for a generalized identifier. If
901 QUALIFYING_SCOPE is non-NULL, the identifier is
902 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903 UNQUALIFIED_NAME. SFK indicates the kind of special function this
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908 special_function_kind sfk)
910 cp_declarator *declarator;
912 /* It is valid to write:
914 class C { void f(); };
918 The standard is not clear about whether `typedef const C D' is
919 legal; as of 2002-09-15 the committee is considering that
920 question. EDG 3.0 allows that syntax. Therefore, we do as
922 if (qualifying_scope && TYPE_P (qualifying_scope))
923 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
925 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
929 declarator = make_declarator (cdk_id);
930 declarator->u.id.qualifying_scope = qualifying_scope;
931 declarator->u.id.unqualified_name = unqualified_name;
932 declarator->u.id.sfk = sfk;
937 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
938 of modifiers such as const or volatile to apply to the pointer
939 type, represented as identifiers. */
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
944 cp_declarator *declarator;
946 declarator = make_declarator (cdk_pointer);
947 declarator->declarator = target;
948 declarator->u.pointer.qualifiers = cv_qualifiers;
949 declarator->u.pointer.class_type = NULL_TREE;
952 declarator->parameter_pack_p = target->parameter_pack_p;
953 target->parameter_pack_p = false;
956 declarator->parameter_pack_p = false;
961 /* Like make_pointer_declarator -- but for references. */
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
967 cp_declarator *declarator;
969 declarator = make_declarator (cdk_reference);
970 declarator->declarator = target;
971 declarator->u.reference.qualifiers = cv_qualifiers;
972 declarator->u.reference.rvalue_ref = rvalue_ref;
975 declarator->parameter_pack_p = target->parameter_pack_p;
976 target->parameter_pack_p = false;
979 declarator->parameter_pack_p = false;
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985 member of CLASS_TYPE. */
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989 cp_declarator *pointee)
991 cp_declarator *declarator;
993 declarator = make_declarator (cdk_ptrmem);
994 declarator->declarator = pointee;
995 declarator->u.pointer.qualifiers = cv_qualifiers;
996 declarator->u.pointer.class_type = class_type;
1000 declarator->parameter_pack_p = pointee->parameter_pack_p;
1001 pointee->parameter_pack_p = false;
1004 declarator->parameter_pack_p = false;
1009 /* Make a declarator for the function given by TARGET, with the
1010 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1011 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1012 indicates what exceptions can be thrown. */
1015 make_call_declarator (cp_declarator *target,
1016 cp_parameter_declarator *parms,
1017 cp_cv_quals cv_qualifiers,
1018 tree exception_specification)
1020 cp_declarator *declarator;
1022 declarator = make_declarator (cdk_function);
1023 declarator->declarator = target;
1024 declarator->u.function.parameters = parms;
1025 declarator->u.function.qualifiers = cv_qualifiers;
1026 declarator->u.function.exception_specification = exception_specification;
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1033 declarator->parameter_pack_p = false;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1055 declarator->parameter_pack_p = false;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1070 while (declarator && !found)
1072 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 void cp_parser_function_body
1740 static tree cp_parser_initializer
1741 (cp_parser *, bool *, bool *);
1742 static tree cp_parser_initializer_clause
1743 (cp_parser *, bool *);
1744 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1745 (cp_parser *, bool *);
1747 static bool cp_parser_ctor_initializer_opt_and_function_body
1750 /* Classes [gram.class] */
1752 static tree cp_parser_class_name
1753 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1754 static tree cp_parser_class_specifier
1756 static tree cp_parser_class_head
1757 (cp_parser *, bool *, tree *, tree *);
1758 static enum tag_types cp_parser_class_key
1760 static void cp_parser_member_specification_opt
1762 static void cp_parser_member_declaration
1764 static tree cp_parser_pure_specifier
1766 static tree cp_parser_constant_initializer
1769 /* Derived classes [gram.class.derived] */
1771 static tree cp_parser_base_clause
1773 static tree cp_parser_base_specifier
1776 /* Special member functions [gram.special] */
1778 static tree cp_parser_conversion_function_id
1780 static tree cp_parser_conversion_type_id
1782 static cp_declarator *cp_parser_conversion_declarator_opt
1784 static bool cp_parser_ctor_initializer_opt
1786 static void cp_parser_mem_initializer_list
1788 static tree cp_parser_mem_initializer
1790 static tree cp_parser_mem_initializer_id
1793 /* Overloading [gram.over] */
1795 static tree cp_parser_operator_function_id
1797 static tree cp_parser_operator
1800 /* Templates [gram.temp] */
1802 static void cp_parser_template_declaration
1803 (cp_parser *, bool);
1804 static tree cp_parser_template_parameter_list
1806 static tree cp_parser_template_parameter
1807 (cp_parser *, bool *, bool *);
1808 static tree cp_parser_type_parameter
1809 (cp_parser *, bool *);
1810 static tree cp_parser_template_id
1811 (cp_parser *, bool, bool, bool);
1812 static tree cp_parser_template_name
1813 (cp_parser *, bool, bool, bool, bool *);
1814 static tree cp_parser_template_argument_list
1816 static tree cp_parser_template_argument
1818 static void cp_parser_explicit_instantiation
1820 static void cp_parser_explicit_specialization
1823 /* Exception handling [gram.exception] */
1825 static tree cp_parser_try_block
1827 static bool cp_parser_function_try_block
1829 static void cp_parser_handler_seq
1831 static void cp_parser_handler
1833 static tree cp_parser_exception_declaration
1835 static tree cp_parser_throw_expression
1837 static tree cp_parser_exception_specification_opt
1839 static tree cp_parser_type_id_list
1842 /* GNU Extensions */
1844 static tree cp_parser_asm_specification_opt
1846 static tree cp_parser_asm_operand_list
1848 static tree cp_parser_asm_clobber_list
1850 static tree cp_parser_attributes_opt
1852 static tree cp_parser_attribute_list
1854 static bool cp_parser_extension_opt
1855 (cp_parser *, int *);
1856 static void cp_parser_label_declaration
1859 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1860 static bool cp_parser_pragma
1861 (cp_parser *, enum pragma_context);
1863 /* Objective-C++ Productions */
1865 static tree cp_parser_objc_message_receiver
1867 static tree cp_parser_objc_message_args
1869 static tree cp_parser_objc_message_expression
1871 static tree cp_parser_objc_encode_expression
1873 static tree cp_parser_objc_defs_expression
1875 static tree cp_parser_objc_protocol_expression
1877 static tree cp_parser_objc_selector_expression
1879 static tree cp_parser_objc_expression
1881 static bool cp_parser_objc_selector_p
1883 static tree cp_parser_objc_selector
1885 static tree cp_parser_objc_protocol_refs_opt
1887 static void cp_parser_objc_declaration
1889 static tree cp_parser_objc_statement
1892 /* Utility Routines */
1894 static tree cp_parser_lookup_name
1895 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1896 static tree cp_parser_lookup_name_simple
1897 (cp_parser *, tree);
1898 static tree cp_parser_maybe_treat_template_as_class
1900 static bool cp_parser_check_declarator_template_parameters
1901 (cp_parser *, cp_declarator *);
1902 static bool cp_parser_check_template_parameters
1903 (cp_parser *, unsigned);
1904 static tree cp_parser_simple_cast_expression
1906 static tree cp_parser_global_scope_opt
1907 (cp_parser *, bool);
1908 static bool cp_parser_constructor_declarator_p
1909 (cp_parser *, bool);
1910 static tree cp_parser_function_definition_from_specifiers_and_declarator
1911 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1912 static tree cp_parser_function_definition_after_declarator
1913 (cp_parser *, bool);
1914 static void cp_parser_template_declaration_after_export
1915 (cp_parser *, bool);
1916 static void cp_parser_perform_template_parameter_access_checks
1917 (VEC (deferred_access_check,gc)*);
1918 static tree cp_parser_single_declaration
1919 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1920 static tree cp_parser_functional_cast
1921 (cp_parser *, tree);
1922 static tree cp_parser_save_member_function_body
1923 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1924 static tree cp_parser_enclosed_template_argument_list
1926 static void cp_parser_save_default_args
1927 (cp_parser *, tree);
1928 static void cp_parser_late_parsing_for_member
1929 (cp_parser *, tree);
1930 static void cp_parser_late_parsing_default_args
1931 (cp_parser *, tree);
1932 static tree cp_parser_sizeof_operand
1933 (cp_parser *, enum rid);
1934 static tree cp_parser_trait_expr
1935 (cp_parser *, enum rid);
1936 static bool cp_parser_declares_only_class_p
1938 static void cp_parser_set_storage_class
1939 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1940 static void cp_parser_set_decl_spec_type
1941 (cp_decl_specifier_seq *, tree, bool);
1942 static bool cp_parser_friend_p
1943 (const cp_decl_specifier_seq *);
1944 static cp_token *cp_parser_require
1945 (cp_parser *, enum cpp_ttype, const char *);
1946 static cp_token *cp_parser_require_keyword
1947 (cp_parser *, enum rid, const char *);
1948 static bool cp_parser_token_starts_function_definition_p
1950 static bool cp_parser_next_token_starts_class_definition_p
1952 static bool cp_parser_next_token_ends_template_argument_p
1954 static bool cp_parser_nth_token_starts_template_argument_list_p
1955 (cp_parser *, size_t);
1956 static enum tag_types cp_parser_token_is_class_key
1958 static void cp_parser_check_class_key
1959 (enum tag_types, tree type);
1960 static void cp_parser_check_access_in_redeclaration
1962 static bool cp_parser_optional_template_keyword
1964 static void cp_parser_pre_parsed_nested_name_specifier
1966 static void cp_parser_cache_group
1967 (cp_parser *, enum cpp_ttype, unsigned);
1968 static void cp_parser_parse_tentatively
1970 static void cp_parser_commit_to_tentative_parse
1972 static void cp_parser_abort_tentative_parse
1974 static bool cp_parser_parse_definitely
1976 static inline bool cp_parser_parsing_tentatively
1978 static bool cp_parser_uncommitted_to_tentative_parse_p
1980 static void cp_parser_error
1981 (cp_parser *, const char *);
1982 static void cp_parser_name_lookup_error
1983 (cp_parser *, tree, tree, const char *);
1984 static bool cp_parser_simulate_error
1986 static bool cp_parser_check_type_definition
1988 static void cp_parser_check_for_definition_in_return_type
1989 (cp_declarator *, tree);
1990 static void cp_parser_check_for_invalid_template_id
1991 (cp_parser *, tree);
1992 static bool cp_parser_non_integral_constant_expression
1993 (cp_parser *, const char *);
1994 static void cp_parser_diagnose_invalid_type_name
1995 (cp_parser *, tree, tree);
1996 static bool cp_parser_parse_and_diagnose_invalid_type_name
1998 static int cp_parser_skip_to_closing_parenthesis
1999 (cp_parser *, bool, bool, bool);
2000 static void cp_parser_skip_to_end_of_statement
2002 static void cp_parser_consume_semicolon_at_end_of_statement
2004 static void cp_parser_skip_to_end_of_block_or_statement
2006 static bool cp_parser_skip_to_closing_brace
2008 static void cp_parser_skip_to_end_of_template_parameter_list
2010 static void cp_parser_skip_to_pragma_eol
2011 (cp_parser*, cp_token *);
2012 static bool cp_parser_error_occurred
2014 static bool cp_parser_allow_gnu_extensions_p
2016 static bool cp_parser_is_string_literal
2018 static bool cp_parser_is_keyword
2019 (cp_token *, enum rid);
2020 static tree cp_parser_make_typename_type
2021 (cp_parser *, tree, tree);
2022 static cp_declarator * cp_parser_make_indirect_declarator
2023 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2025 /* Returns nonzero if we are parsing tentatively. */
2028 cp_parser_parsing_tentatively (cp_parser* parser)
2030 return parser->context->next != NULL;
2033 /* Returns nonzero if TOKEN is a string literal. */
2036 cp_parser_is_string_literal (cp_token* token)
2038 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2041 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2044 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2046 return token->keyword == keyword;
2049 /* If not parsing tentatively, issue a diagnostic of the form
2050 FILE:LINE: MESSAGE before TOKEN
2051 where TOKEN is the next token in the input stream. MESSAGE
2052 (specified by the caller) is usually of the form "expected
2056 cp_parser_error (cp_parser* parser, const char* message)
2058 if (!cp_parser_simulate_error (parser))
2060 cp_token *token = cp_lexer_peek_token (parser->lexer);
2061 /* This diagnostic makes more sense if it is tagged to the line
2062 of the token we just peeked at. */
2063 cp_lexer_set_source_position_from_token (token);
2065 if (token->type == CPP_PRAGMA)
2067 error ("%<#pragma%> is not allowed here");
2068 cp_parser_skip_to_pragma_eol (parser, token);
2072 c_parse_error (message,
2073 /* Because c_parser_error does not understand
2074 CPP_KEYWORD, keywords are treated like
2076 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2081 /* Issue an error about name-lookup failing. NAME is the
2082 IDENTIFIER_NODE DECL is the result of
2083 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2084 the thing that we hoped to find. */
2087 cp_parser_name_lookup_error (cp_parser* parser,
2090 const char* desired)
2092 /* If name lookup completely failed, tell the user that NAME was not
2094 if (decl == error_mark_node)
2096 if (parser->scope && parser->scope != global_namespace)
2097 error ("%<%E::%E%> has not been declared",
2098 parser->scope, name);
2099 else if (parser->scope == global_namespace)
2100 error ("%<::%E%> has not been declared", name);
2101 else if (parser->object_scope
2102 && !CLASS_TYPE_P (parser->object_scope))
2103 error ("request for member %qE in non-class type %qT",
2104 name, parser->object_scope);
2105 else if (parser->object_scope)
2106 error ("%<%T::%E%> has not been declared",
2107 parser->object_scope, name);
2109 error ("%qE has not been declared", name);
2111 else if (parser->scope && parser->scope != global_namespace)
2112 error ("%<%E::%E%> %s", parser->scope, name, desired);
2113 else if (parser->scope == global_namespace)
2114 error ("%<::%E%> %s", name, desired);
2116 error ("%qE %s", name, desired);
2119 /* If we are parsing tentatively, remember that an error has occurred
2120 during this tentative parse. Returns true if the error was
2121 simulated; false if a message should be issued by the caller. */
2124 cp_parser_simulate_error (cp_parser* parser)
2126 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2128 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2134 /* Check for repeated decl-specifiers. */
2137 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2141 for (ds = ds_first; ds != ds_last; ++ds)
2143 unsigned count = decl_specs->specs[(int)ds];
2146 /* The "long" specifier is a special case because of "long long". */
2150 error ("%<long long long%> is too long for GCC");
2151 else if (pedantic && !in_system_header && warn_long_long)
2152 pedwarn ("ISO C++ does not support %<long long%>");
2156 static const char *const decl_spec_names[] = {
2172 error ("duplicate %qs", decl_spec_names[(int)ds]);
2177 /* This function is called when a type is defined. If type
2178 definitions are forbidden at this point, an error message is
2182 cp_parser_check_type_definition (cp_parser* parser)
2184 /* If types are forbidden here, issue a message. */
2185 if (parser->type_definition_forbidden_message)
2187 /* Use `%s' to print the string in case there are any escape
2188 characters in the message. */
2189 error ("%s", parser->type_definition_forbidden_message);
2195 /* This function is called when the DECLARATOR is processed. The TYPE
2196 was a type defined in the decl-specifiers. If it is invalid to
2197 define a type in the decl-specifiers for DECLARATOR, an error is
2201 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2204 /* [dcl.fct] forbids type definitions in return types.
2205 Unfortunately, it's not easy to know whether or not we are
2206 processing a return type until after the fact. */
2208 && (declarator->kind == cdk_pointer
2209 || declarator->kind == cdk_reference
2210 || declarator->kind == cdk_ptrmem))
2211 declarator = declarator->declarator;
2213 && declarator->kind == cdk_function)
2215 error ("new types may not be defined in a return type");
2216 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2221 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2222 "<" in any valid C++ program. If the next token is indeed "<",
2223 issue a message warning the user about what appears to be an
2224 invalid attempt to form a template-id. */
2227 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2230 cp_token_position start = 0;
2232 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2235 error ("%qT is not a template", type);
2236 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2237 error ("%qE is not a template", type);
2239 error ("invalid template-id");
2240 /* Remember the location of the invalid "<". */
2241 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2242 start = cp_lexer_token_position (parser->lexer, true);
2243 /* Consume the "<". */
2244 cp_lexer_consume_token (parser->lexer);
2245 /* Parse the template arguments. */
2246 cp_parser_enclosed_template_argument_list (parser);
2247 /* Permanently remove the invalid template arguments so that
2248 this error message is not issued again. */
2250 cp_lexer_purge_tokens_after (parser->lexer, start);
2254 /* If parsing an integral constant-expression, issue an error message
2255 about the fact that THING appeared and return true. Otherwise,
2256 return false. In either case, set
2257 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2260 cp_parser_non_integral_constant_expression (cp_parser *parser,
2263 parser->non_integral_constant_expression_p = true;
2264 if (parser->integral_constant_expression_p)
2266 if (!parser->allow_non_integral_constant_expression_p)
2268 error ("%s cannot appear in a constant-expression", thing);
2275 /* Emit a diagnostic for an invalid type name. SCOPE is the
2276 qualifying scope (or NULL, if none) for ID. This function commits
2277 to the current active tentative parse, if any. (Otherwise, the
2278 problematic construct might be encountered again later, resulting
2279 in duplicate error messages.) */
2282 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2284 tree decl, old_scope;
2285 /* Try to lookup the identifier. */
2286 old_scope = parser->scope;
2287 parser->scope = scope;
2288 decl = cp_parser_lookup_name_simple (parser, id);
2289 parser->scope = old_scope;
2290 /* If the lookup found a template-name, it means that the user forgot
2291 to specify an argument list. Emit a useful error message. */
2292 if (TREE_CODE (decl) == TEMPLATE_DECL)
2293 error ("invalid use of template-name %qE without an argument list", decl);
2294 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2295 error ("invalid use of destructor %qD as a type", id);
2296 else if (TREE_CODE (decl) == TYPE_DECL)
2297 /* Something like 'unsigned A a;' */
2298 error ("invalid combination of multiple type-specifiers");
2299 else if (!parser->scope)
2301 /* Issue an error message. */
2302 error ("%qE does not name a type", id);
2303 /* If we're in a template class, it's possible that the user was
2304 referring to a type from a base class. For example:
2306 template <typename T> struct A { typedef T X; };
2307 template <typename T> struct B : public A<T> { X x; };
2309 The user should have said "typename A<T>::X". */
2310 if (processing_template_decl && current_class_type
2311 && TYPE_BINFO (current_class_type))
2315 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2319 tree base_type = BINFO_TYPE (b);
2320 if (CLASS_TYPE_P (base_type)
2321 && dependent_type_p (base_type))
2324 /* Go from a particular instantiation of the
2325 template (which will have an empty TYPE_FIELDs),
2326 to the main version. */
2327 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2328 for (field = TYPE_FIELDS (base_type);
2330 field = TREE_CHAIN (field))
2331 if (TREE_CODE (field) == TYPE_DECL
2332 && DECL_NAME (field) == id)
2334 inform ("(perhaps %<typename %T::%E%> was intended)",
2335 BINFO_TYPE (b), id);
2344 /* Here we diagnose qualified-ids where the scope is actually correct,
2345 but the identifier does not resolve to a valid type name. */
2346 else if (parser->scope != error_mark_node)
2348 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2349 error ("%qE in namespace %qE does not name a type",
2351 else if (TYPE_P (parser->scope))
2352 error ("%qE in class %qT does not name a type", id, parser->scope);
2356 cp_parser_commit_to_tentative_parse (parser);
2359 /* Check for a common situation where a type-name should be present,
2360 but is not, and issue a sensible error message. Returns true if an
2361 invalid type-name was detected.
2363 The situation handled by this function are variable declarations of the
2364 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2365 Usually, `ID' should name a type, but if we got here it means that it
2366 does not. We try to emit the best possible error message depending on
2367 how exactly the id-expression looks like. */
2370 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2374 cp_parser_parse_tentatively (parser);
2375 id = cp_parser_id_expression (parser,
2376 /*template_keyword_p=*/false,
2377 /*check_dependency_p=*/true,
2378 /*template_p=*/NULL,
2379 /*declarator_p=*/true,
2380 /*optional_p=*/false);
2381 /* After the id-expression, there should be a plain identifier,
2382 otherwise this is not a simple variable declaration. Also, if
2383 the scope is dependent, we cannot do much. */
2384 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2385 || (parser->scope && TYPE_P (parser->scope)
2386 && dependent_type_p (parser->scope))
2387 || TREE_CODE (id) == TYPE_DECL)
2389 cp_parser_abort_tentative_parse (parser);
2392 if (!cp_parser_parse_definitely (parser))
2395 /* Emit a diagnostic for the invalid type. */
2396 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2397 /* Skip to the end of the declaration; there's no point in
2398 trying to process it. */
2399 cp_parser_skip_to_end_of_block_or_statement (parser);
2403 /* Consume tokens up to, and including, the next non-nested closing `)'.
2404 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2405 are doing error recovery. Returns -1 if OR_COMMA is true and we
2406 found an unnested comma. */
2409 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2414 unsigned paren_depth = 0;
2415 unsigned brace_depth = 0;
2417 if (recovering && !or_comma
2418 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2423 cp_token * token = cp_lexer_peek_token (parser->lexer);
2425 switch (token->type)
2428 case CPP_PRAGMA_EOL:
2429 /* If we've run out of tokens, then there is no closing `)'. */
2433 /* This matches the processing in skip_to_end_of_statement. */
2438 case CPP_OPEN_BRACE:
2441 case CPP_CLOSE_BRACE:
2447 if (recovering && or_comma && !brace_depth && !paren_depth)
2451 case CPP_OPEN_PAREN:
2456 case CPP_CLOSE_PAREN:
2457 if (!brace_depth && !paren_depth--)
2460 cp_lexer_consume_token (parser->lexer);
2469 /* Consume the token. */
2470 cp_lexer_consume_token (parser->lexer);
2474 /* Consume tokens until we reach the end of the current statement.
2475 Normally, that will be just before consuming a `;'. However, if a
2476 non-nested `}' comes first, then we stop before consuming that. */
2479 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2481 unsigned nesting_depth = 0;
2485 cp_token *token = cp_lexer_peek_token (parser->lexer);
2487 switch (token->type)
2490 case CPP_PRAGMA_EOL:
2491 /* If we've run out of tokens, stop. */
2495 /* If the next token is a `;', we have reached the end of the
2501 case CPP_CLOSE_BRACE:
2502 /* If this is a non-nested '}', stop before consuming it.
2503 That way, when confronted with something like:
2507 we stop before consuming the closing '}', even though we
2508 have not yet reached a `;'. */
2509 if (nesting_depth == 0)
2512 /* If it is the closing '}' for a block that we have
2513 scanned, stop -- but only after consuming the token.
2519 we will stop after the body of the erroneously declared
2520 function, but before consuming the following `typedef'
2522 if (--nesting_depth == 0)
2524 cp_lexer_consume_token (parser->lexer);
2528 case CPP_OPEN_BRACE:
2536 /* Consume the token. */
2537 cp_lexer_consume_token (parser->lexer);
2541 /* This function is called at the end of a statement or declaration.
2542 If the next token is a semicolon, it is consumed; otherwise, error
2543 recovery is attempted. */
2546 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2548 /* Look for the trailing `;'. */
2549 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2551 /* If there is additional (erroneous) input, skip to the end of
2553 cp_parser_skip_to_end_of_statement (parser);
2554 /* If the next token is now a `;', consume it. */
2555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2556 cp_lexer_consume_token (parser->lexer);
2560 /* Skip tokens until we have consumed an entire block, or until we
2561 have consumed a non-nested `;'. */
2564 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2566 int nesting_depth = 0;
2568 while (nesting_depth >= 0)
2570 cp_token *token = cp_lexer_peek_token (parser->lexer);
2572 switch (token->type)
2575 case CPP_PRAGMA_EOL:
2576 /* If we've run out of tokens, stop. */
2580 /* Stop if this is an unnested ';'. */
2585 case CPP_CLOSE_BRACE:
2586 /* Stop if this is an unnested '}', or closes the outermost
2593 case CPP_OPEN_BRACE:
2602 /* Consume the token. */
2603 cp_lexer_consume_token (parser->lexer);
2607 /* Skip tokens until a non-nested closing curly brace is the next
2608 token, or there are no more tokens. Return true in the first case,
2612 cp_parser_skip_to_closing_brace (cp_parser *parser)
2614 unsigned nesting_depth = 0;
2618 cp_token *token = cp_lexer_peek_token (parser->lexer);
2620 switch (token->type)
2623 case CPP_PRAGMA_EOL:
2624 /* If we've run out of tokens, stop. */
2627 case CPP_CLOSE_BRACE:
2628 /* If the next token is a non-nested `}', then we have reached
2629 the end of the current block. */
2630 if (nesting_depth-- == 0)
2634 case CPP_OPEN_BRACE:
2635 /* If it the next token is a `{', then we are entering a new
2636 block. Consume the entire block. */
2644 /* Consume the token. */
2645 cp_lexer_consume_token (parser->lexer);
2649 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2650 parameter is the PRAGMA token, allowing us to purge the entire pragma
2654 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2658 parser->lexer->in_pragma = false;
2661 token = cp_lexer_consume_token (parser->lexer);
2662 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2664 /* Ensure that the pragma is not parsed again. */
2665 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2668 /* Require pragma end of line, resyncing with it as necessary. The
2669 arguments are as for cp_parser_skip_to_pragma_eol. */
2672 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2674 parser->lexer->in_pragma = false;
2675 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2676 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2679 /* This is a simple wrapper around make_typename_type. When the id is
2680 an unresolved identifier node, we can provide a superior diagnostic
2681 using cp_parser_diagnose_invalid_type_name. */
2684 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2687 if (TREE_CODE (id) == IDENTIFIER_NODE)
2689 result = make_typename_type (scope, id, typename_type,
2690 /*complain=*/tf_none);
2691 if (result == error_mark_node)
2692 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2695 return make_typename_type (scope, id, typename_type, tf_error);
2698 /* This is a wrapper around the
2699 make_{pointer,ptrmem,reference}_declarator functions that decides
2700 which one to call based on the CODE and CLASS_TYPE arguments. The
2701 CODE argument should be one of the values returned by
2702 cp_parser_ptr_operator. */
2703 static cp_declarator *
2704 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2705 cp_cv_quals cv_qualifiers,
2706 cp_declarator *target)
2708 if (code == ERROR_MARK)
2709 return cp_error_declarator;
2711 if (code == INDIRECT_REF)
2712 if (class_type == NULL_TREE)
2713 return make_pointer_declarator (cv_qualifiers, target);
2715 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2716 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2717 return make_reference_declarator (cv_qualifiers, target, false);
2718 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2719 return make_reference_declarator (cv_qualifiers, target, true);
2723 /* Create a new C++ parser. */
2726 cp_parser_new (void)
2732 /* cp_lexer_new_main is called before calling ggc_alloc because
2733 cp_lexer_new_main might load a PCH file. */
2734 lexer = cp_lexer_new_main ();
2736 /* Initialize the binops_by_token so that we can get the tree
2737 directly from the token. */
2738 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2739 binops_by_token[binops[i].token_type] = binops[i];
2741 parser = GGC_CNEW (cp_parser);
2742 parser->lexer = lexer;
2743 parser->context = cp_parser_context_new (NULL);
2745 /* For now, we always accept GNU extensions. */
2746 parser->allow_gnu_extensions_p = 1;
2748 /* The `>' token is a greater-than operator, not the end of a
2750 parser->greater_than_is_operator_p = true;
2752 parser->default_arg_ok_p = true;
2754 /* We are not parsing a constant-expression. */
2755 parser->integral_constant_expression_p = false;
2756 parser->allow_non_integral_constant_expression_p = false;
2757 parser->non_integral_constant_expression_p = false;
2759 /* Local variable names are not forbidden. */
2760 parser->local_variables_forbidden_p = false;
2762 /* We are not processing an `extern "C"' declaration. */
2763 parser->in_unbraced_linkage_specification_p = false;
2765 /* We are not processing a declarator. */
2766 parser->in_declarator_p = false;
2768 /* We are not processing a template-argument-list. */
2769 parser->in_template_argument_list_p = false;
2771 /* We are not in an iteration statement. */
2772 parser->in_statement = 0;
2774 /* We are not in a switch statement. */
2775 parser->in_switch_statement_p = false;
2777 /* We are not parsing a type-id inside an expression. */
2778 parser->in_type_id_in_expr_p = false;
2780 /* Declarations aren't implicitly extern "C". */
2781 parser->implicit_extern_c = false;
2783 /* String literals should be translated to the execution character set. */
2784 parser->translate_strings_p = true;
2786 /* We are not parsing a function body. */
2787 parser->in_function_body = false;
2789 /* The unparsed function queue is empty. */
2790 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2792 /* There are no classes being defined. */
2793 parser->num_classes_being_defined = 0;
2795 /* No template parameters apply. */
2796 parser->num_template_parameter_lists = 0;
2801 /* Create a cp_lexer structure which will emit the tokens in CACHE
2802 and push it onto the parser's lexer stack. This is used for delayed
2803 parsing of in-class method bodies and default arguments, and should
2804 not be confused with tentative parsing. */
2806 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2808 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2809 lexer->next = parser->lexer;
2810 parser->lexer = lexer;
2812 /* Move the current source position to that of the first token in the
2814 cp_lexer_set_source_position_from_token (lexer->next_token);
2817 /* Pop the top lexer off the parser stack. This is never used for the
2818 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2820 cp_parser_pop_lexer (cp_parser *parser)
2822 cp_lexer *lexer = parser->lexer;
2823 parser->lexer = lexer->next;
2824 cp_lexer_destroy (lexer);
2826 /* Put the current source position back where it was before this
2827 lexer was pushed. */
2828 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2831 /* Lexical conventions [gram.lex] */
2833 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2837 cp_parser_identifier (cp_parser* parser)
2841 /* Look for the identifier. */
2842 token = cp_parser_require (parser, CPP_NAME, "identifier");
2843 /* Return the value. */
2844 return token ? token->u.value : error_mark_node;
2847 /* Parse a sequence of adjacent string constants. Returns a
2848 TREE_STRING representing the combined, nul-terminated string
2849 constant. If TRANSLATE is true, translate the string to the
2850 execution character set. If WIDE_OK is true, a wide string is
2853 C++98 [lex.string] says that if a narrow string literal token is
2854 adjacent to a wide string literal token, the behavior is undefined.
2855 However, C99 6.4.5p4 says that this results in a wide string literal.
2856 We follow C99 here, for consistency with the C front end.
2858 This code is largely lifted from lex_string() in c-lex.c.
2860 FUTURE: ObjC++ will need to handle @-strings here. */
2862 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2867 struct obstack str_ob;
2868 cpp_string str, istr, *strs;
2871 tok = cp_lexer_peek_token (parser->lexer);
2872 if (!cp_parser_is_string_literal (tok))
2874 cp_parser_error (parser, "expected string-literal");
2875 return error_mark_node;
2878 /* Try to avoid the overhead of creating and destroying an obstack
2879 for the common case of just one string. */
2880 if (!cp_parser_is_string_literal
2881 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2883 cp_lexer_consume_token (parser->lexer);
2885 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2886 str.len = TREE_STRING_LENGTH (tok->u.value);
2888 if (tok->type == CPP_WSTRING)
2895 gcc_obstack_init (&str_ob);
2900 cp_lexer_consume_token (parser->lexer);
2902 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2903 str.len = TREE_STRING_LENGTH (tok->u.value);
2904 if (tok->type == CPP_WSTRING)
2907 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2909 tok = cp_lexer_peek_token (parser->lexer);
2911 while (cp_parser_is_string_literal (tok));
2913 strs = (cpp_string *) obstack_finish (&str_ob);
2916 if (wide && !wide_ok)
2918 cp_parser_error (parser, "a wide string is invalid in this context");
2922 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2923 (parse_in, strs, count, &istr, wide))
2925 value = build_string (istr.len, (const char *)istr.text);
2926 free (CONST_CAST (istr.text));
2928 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2929 value = fix_string_type (value);
2932 /* cpp_interpret_string has issued an error. */
2933 value = error_mark_node;
2936 obstack_free (&str_ob, 0);
2942 /* Basic concepts [gram.basic] */
2944 /* Parse a translation-unit.
2947 declaration-seq [opt]
2949 Returns TRUE if all went well. */
2952 cp_parser_translation_unit (cp_parser* parser)
2954 /* The address of the first non-permanent object on the declarator
2956 static void *declarator_obstack_base;
2960 /* Create the declarator obstack, if necessary. */
2961 if (!cp_error_declarator)
2963 gcc_obstack_init (&declarator_obstack);
2964 /* Create the error declarator. */
2965 cp_error_declarator = make_declarator (cdk_error);
2966 /* Create the empty parameter list. */
2967 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2968 /* Remember where the base of the declarator obstack lies. */
2969 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2972 cp_parser_declaration_seq_opt (parser);
2974 /* If there are no tokens left then all went well. */
2975 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2977 /* Get rid of the token array; we don't need it any more. */
2978 cp_lexer_destroy (parser->lexer);
2979 parser->lexer = NULL;
2981 /* This file might have been a context that's implicitly extern
2982 "C". If so, pop the lang context. (Only relevant for PCH.) */
2983 if (parser->implicit_extern_c)
2985 pop_lang_context ();
2986 parser->implicit_extern_c = false;
2990 finish_translation_unit ();
2996 cp_parser_error (parser, "expected declaration");
3000 /* Make sure the declarator obstack was fully cleaned up. */
3001 gcc_assert (obstack_next_free (&declarator_obstack)
3002 == declarator_obstack_base);
3004 /* All went well. */
3008 /* Expressions [gram.expr] */
3010 /* Parse a primary-expression.
3021 ( compound-statement )
3022 __builtin_va_arg ( assignment-expression , type-id )
3023 __builtin_offsetof ( type-id , offsetof-expression )
3026 __has_nothrow_assign ( type-id )
3027 __has_nothrow_constructor ( type-id )
3028 __has_nothrow_copy ( type-id )
3029 __has_trivial_assign ( type-id )
3030 __has_trivial_constructor ( type-id )
3031 __has_trivial_copy ( type-id )
3032 __has_trivial_destructor ( type-id )
3033 __has_virtual_destructor ( type-id )
3034 __is_abstract ( type-id )
3035 __is_base_of ( type-id , type-id )
3036 __is_class ( type-id )
3037 __is_convertible_to ( type-id , type-id )
3038 __is_empty ( type-id )
3039 __is_enum ( type-id )
3040 __is_pod ( type-id )
3041 __is_polymorphic ( type-id )
3042 __is_union ( type-id )
3044 Objective-C++ Extension:
3052 ADDRESS_P is true iff this expression was immediately preceded by
3053 "&" and therefore might denote a pointer-to-member. CAST_P is true
3054 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3055 true iff this expression is a template argument.
3057 Returns a representation of the expression. Upon return, *IDK
3058 indicates what kind of id-expression (if any) was present. */
3061 cp_parser_primary_expression (cp_parser *parser,
3064 bool template_arg_p,
3069 /* Assume the primary expression is not an id-expression. */
3070 *idk = CP_ID_KIND_NONE;
3072 /* Peek at the next token. */
3073 token = cp_lexer_peek_token (parser->lexer);
3074 switch (token->type)
3085 token = cp_lexer_consume_token (parser->lexer);
3086 /* Floating-point literals are only allowed in an integral
3087 constant expression if they are cast to an integral or
3088 enumeration type. */
3089 if (TREE_CODE (token->u.value) == REAL_CST
3090 && parser->integral_constant_expression_p
3093 /* CAST_P will be set even in invalid code like "int(2.7 +
3094 ...)". Therefore, we have to check that the next token
3095 is sure to end the cast. */
3098 cp_token *next_token;
3100 next_token = cp_lexer_peek_token (parser->lexer);
3101 if (/* The comma at the end of an
3102 enumerator-definition. */
3103 next_token->type != CPP_COMMA
3104 /* The curly brace at the end of an enum-specifier. */
3105 && next_token->type != CPP_CLOSE_BRACE
3106 /* The end of a statement. */
3107 && next_token->type != CPP_SEMICOLON
3108 /* The end of the cast-expression. */
3109 && next_token->type != CPP_CLOSE_PAREN
3110 /* The end of an array bound. */
3111 && next_token->type != CPP_CLOSE_SQUARE
3112 /* The closing ">" in a template-argument-list. */
3113 && (next_token->type != CPP_GREATER
3114 || parser->greater_than_is_operator_p)
3115 /* C++0x only: A ">>" treated like two ">" tokens,
3116 in a template-argument-list. */
3117 && (next_token->type != CPP_RSHIFT
3118 || (cxx_dialect == cxx98)
3119 || parser->greater_than_is_operator_p))
3123 /* If we are within a cast, then the constraint that the
3124 cast is to an integral or enumeration type will be
3125 checked at that point. If we are not within a cast, then
3126 this code is invalid. */
3128 cp_parser_non_integral_constant_expression
3129 (parser, "floating-point literal");
3131 return token->u.value;
3135 /* ??? Should wide strings be allowed when parser->translate_strings_p
3136 is false (i.e. in attributes)? If not, we can kill the third
3137 argument to cp_parser_string_literal. */
3138 return cp_parser_string_literal (parser,
3139 parser->translate_strings_p,
3142 case CPP_OPEN_PAREN:
3145 bool saved_greater_than_is_operator_p;
3147 /* Consume the `('. */
3148 cp_lexer_consume_token (parser->lexer);
3149 /* Within a parenthesized expression, a `>' token is always
3150 the greater-than operator. */
3151 saved_greater_than_is_operator_p
3152 = parser->greater_than_is_operator_p;
3153 parser->greater_than_is_operator_p = true;
3154 /* If we see `( { ' then we are looking at the beginning of
3155 a GNU statement-expression. */
3156 if (cp_parser_allow_gnu_extensions_p (parser)
3157 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3159 /* Statement-expressions are not allowed by the standard. */
3161 pedwarn ("ISO C++ forbids braced-groups within expressions");
3163 /* And they're not allowed outside of a function-body; you
3164 cannot, for example, write:
3166 int i = ({ int j = 3; j + 1; });
3168 at class or namespace scope. */
3169 if (!parser->in_function_body)
3171 error ("statement-expressions are allowed only inside functions");
3172 cp_parser_skip_to_end_of_block_or_statement (parser);
3173 expr = error_mark_node;
3177 /* Start the statement-expression. */
3178 expr = begin_stmt_expr ();
3179 /* Parse the compound-statement. */
3180 cp_parser_compound_statement (parser, expr, false);
3182 expr = finish_stmt_expr (expr, false);
3187 /* Parse the parenthesized expression. */
3188 expr = cp_parser_expression (parser, cast_p);
3189 /* Let the front end know that this expression was
3190 enclosed in parentheses. This matters in case, for
3191 example, the expression is of the form `A::B', since
3192 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3194 finish_parenthesized_expr (expr);
3196 /* The `>' token might be the end of a template-id or
3197 template-parameter-list now. */
3198 parser->greater_than_is_operator_p
3199 = saved_greater_than_is_operator_p;
3200 /* Consume the `)'. */
3201 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3202 cp_parser_skip_to_end_of_statement (parser);
3208 switch (token->keyword)
3210 /* These two are the boolean literals. */
3212 cp_lexer_consume_token (parser->lexer);
3213 return boolean_true_node;
3215 cp_lexer_consume_token (parser->lexer);
3216 return boolean_false_node;
3218 /* The `__null' literal. */
3220 cp_lexer_consume_token (parser->lexer);
3223 /* Recognize the `this' keyword. */
3225 cp_lexer_consume_token (parser->lexer);
3226 if (parser->local_variables_forbidden_p)
3228 error ("%<this%> may not be used in this context");
3229 return error_mark_node;
3231 /* Pointers cannot appear in constant-expressions. */
3232 if (cp_parser_non_integral_constant_expression (parser,
3234 return error_mark_node;
3235 return finish_this_expr ();
3237 /* The `operator' keyword can be the beginning of an
3242 case RID_FUNCTION_NAME:
3243 case RID_PRETTY_FUNCTION_NAME:
3244 case RID_C99_FUNCTION_NAME:
3245 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3246 __func__ are the names of variables -- but they are
3247 treated specially. Therefore, they are handled here,
3248 rather than relying on the generic id-expression logic
3249 below. Grammatically, these names are id-expressions.
3251 Consume the token. */
3252 token = cp_lexer_consume_token (parser->lexer);
3253 /* Look up the name. */
3254 return finish_fname (token->u.value);
3261 /* The `__builtin_va_arg' construct is used to handle
3262 `va_arg'. Consume the `__builtin_va_arg' token. */
3263 cp_lexer_consume_token (parser->lexer);
3264 /* Look for the opening `('. */
3265 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3266 /* Now, parse the assignment-expression. */
3267 expression = cp_parser_assignment_expression (parser,
3269 /* Look for the `,'. */
3270 cp_parser_require (parser, CPP_COMMA, "`,'");
3271 /* Parse the type-id. */
3272 type = cp_parser_type_id (parser);
3273 /* Look for the closing `)'. */
3274 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3275 /* Using `va_arg' in a constant-expression is not
3277 if (cp_parser_non_integral_constant_expression (parser,
3279 return error_mark_node;
3280 return build_x_va_arg (expression, type);
3284 return cp_parser_builtin_offsetof (parser);
3286 case RID_HAS_NOTHROW_ASSIGN:
3287 case RID_HAS_NOTHROW_CONSTRUCTOR:
3288 case RID_HAS_NOTHROW_COPY:
3289 case RID_HAS_TRIVIAL_ASSIGN:
3290 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3291 case RID_HAS_TRIVIAL_COPY:
3292 case RID_HAS_TRIVIAL_DESTRUCTOR:
3293 case RID_HAS_VIRTUAL_DESTRUCTOR:
3294 case RID_IS_ABSTRACT:
3295 case RID_IS_BASE_OF:
3297 case RID_IS_CONVERTIBLE_TO:
3301 case RID_IS_POLYMORPHIC:
3303 return cp_parser_trait_expr (parser, token->keyword);
3305 /* Objective-C++ expressions. */
3307 case RID_AT_PROTOCOL:
3308 case RID_AT_SELECTOR:
3309 return cp_parser_objc_expression (parser);
3312 cp_parser_error (parser, "expected primary-expression");
3313 return error_mark_node;
3316 /* An id-expression can start with either an identifier, a
3317 `::' as the beginning of a qualified-id, or the "operator"
3321 case CPP_TEMPLATE_ID:
3322 case CPP_NESTED_NAME_SPECIFIER:
3326 const char *error_msg;
3331 /* Parse the id-expression. */
3333 = cp_parser_id_expression (parser,
3334 /*template_keyword_p=*/false,
3335 /*check_dependency_p=*/true,
3337 /*declarator_p=*/false,
3338 /*optional_p=*/false);
3339 if (id_expression == error_mark_node)
3340 return error_mark_node;
3341 token = cp_lexer_peek_token (parser->lexer);
3342 done = (token->type != CPP_OPEN_SQUARE
3343 && token->type != CPP_OPEN_PAREN
3344 && token->type != CPP_DOT
3345 && token->type != CPP_DEREF
3346 && token->type != CPP_PLUS_PLUS
3347 && token->type != CPP_MINUS_MINUS);
3348 /* If we have a template-id, then no further lookup is
3349 required. If the template-id was for a template-class, we
3350 will sometimes have a TYPE_DECL at this point. */
3351 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3352 || TREE_CODE (id_expression) == TYPE_DECL)
3353 decl = id_expression;
3354 /* Look up the name. */
3357 tree ambiguous_decls;
3359 decl = cp_parser_lookup_name (parser, id_expression,
3362 /*is_namespace=*/false,
3363 /*check_dependency=*/true,
3365 /* If the lookup was ambiguous, an error will already have
3367 if (ambiguous_decls)
3368 return error_mark_node;
3370 /* In Objective-C++, an instance variable (ivar) may be preferred
3371 to whatever cp_parser_lookup_name() found. */
3372 decl = objc_lookup_ivar (decl, id_expression);
3374 /* If name lookup gives us a SCOPE_REF, then the
3375 qualifying scope was dependent. */
3376 if (TREE_CODE (decl) == SCOPE_REF)
3378 /* At this point, we do not know if DECL is a valid
3379 integral constant expression. We assume that it is
3380 in fact such an expression, so that code like:
3382 template <int N> struct A {
3386 is accepted. At template-instantiation time, we
3387 will check that B<N>::i is actually a constant. */
3390 /* Check to see if DECL is a local variable in a context
3391 where that is forbidden. */
3392 if (parser->local_variables_forbidden_p
3393 && local_variable_p (decl))
3395 /* It might be that we only found DECL because we are
3396 trying to be generous with pre-ISO scoping rules.
3397 For example, consider:
3401 for (int i = 0; i < 10; ++i) {}
3402 extern void f(int j = i);
3405 Here, name look up will originally find the out
3406 of scope `i'. We need to issue a warning message,
3407 but then use the global `i'. */
3408 decl = check_for_out_of_scope_variable (decl);
3409 if (local_variable_p (decl))
3411 error ("local variable %qD may not appear in this context",
3413 return error_mark_node;
3418 decl = (finish_id_expression
3419 (id_expression, decl, parser->scope,
3421 parser->integral_constant_expression_p,
3422 parser->allow_non_integral_constant_expression_p,
3423 &parser->non_integral_constant_expression_p,
3424 template_p, done, address_p,
3428 cp_parser_error (parser, error_msg);
3432 /* Anything else is an error. */
3434 /* ...unless we have an Objective-C++ message or string literal,
3436 if (c_dialect_objc ()
3437 && (token->type == CPP_OPEN_SQUARE
3438 || token->type == CPP_OBJC_STRING))
3439 return cp_parser_objc_expression (parser);
3441 cp_parser_error (parser, "expected primary-expression");
3442 return error_mark_node;
3446 /* Parse an id-expression.
3453 :: [opt] nested-name-specifier template [opt] unqualified-id
3455 :: operator-function-id
3458 Return a representation of the unqualified portion of the
3459 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3460 a `::' or nested-name-specifier.
3462 Often, if the id-expression was a qualified-id, the caller will
3463 want to make a SCOPE_REF to represent the qualified-id. This
3464 function does not do this in order to avoid wastefully creating
3465 SCOPE_REFs when they are not required.
3467 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3470 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3471 uninstantiated templates.
3473 If *TEMPLATE_P is non-NULL, it is set to true iff the
3474 `template' keyword is used to explicitly indicate that the entity
3475 named is a template.
3477 If DECLARATOR_P is true, the id-expression is appearing as part of
3478 a declarator, rather than as part of an expression. */
3481 cp_parser_id_expression (cp_parser *parser,
3482 bool template_keyword_p,
3483 bool check_dependency_p,
3488 bool global_scope_p;
3489 bool nested_name_specifier_p;
3491 /* Assume the `template' keyword was not used. */
3493 *template_p = template_keyword_p;
3495 /* Look for the optional `::' operator. */
3497 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3499 /* Look for the optional nested-name-specifier. */
3500 nested_name_specifier_p
3501 = (cp_parser_nested_name_specifier_opt (parser,
3502 /*typename_keyword_p=*/false,
3507 /* If there is a nested-name-specifier, then we are looking at
3508 the first qualified-id production. */
3509 if (nested_name_specifier_p)
3512 tree saved_object_scope;
3513 tree saved_qualifying_scope;
3514 tree unqualified_id;
3517 /* See if the next token is the `template' keyword. */
3519 template_p = &is_template;
3520 *template_p = cp_parser_optional_template_keyword (parser);
3521 /* Name lookup we do during the processing of the
3522 unqualified-id might obliterate SCOPE. */
3523 saved_scope = parser->scope;
3524 saved_object_scope = parser->object_scope;
3525 saved_qualifying_scope = parser->qualifying_scope;
3526 /* Process the final unqualified-id. */
3527 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3530 /*optional_p=*/false);
3531 /* Restore the SAVED_SCOPE for our caller. */
3532 parser->scope = saved_scope;
3533 parser->object_scope = saved_object_scope;
3534 parser->qualifying_scope = saved_qualifying_scope;
3536 return unqualified_id;
3538 /* Otherwise, if we are in global scope, then we are looking at one
3539 of the other qualified-id productions. */
3540 else if (global_scope_p)
3545 /* Peek at the next token. */
3546 token = cp_lexer_peek_token (parser->lexer);
3548 /* If it's an identifier, and the next token is not a "<", then
3549 we can avoid the template-id case. This is an optimization
3550 for this common case. */
3551 if (token->type == CPP_NAME
3552 && !cp_parser_nth_token_starts_template_argument_list_p
3554 return cp_parser_identifier (parser);
3556 cp_parser_parse_tentatively (parser);
3557 /* Try a template-id. */
3558 id = cp_parser_template_id (parser,
3559 /*template_keyword_p=*/false,
3560 /*check_dependency_p=*/true,
3562 /* If that worked, we're done. */
3563 if (cp_parser_parse_definitely (parser))
3566 /* Peek at the next token. (Changes in the token buffer may
3567 have invalidated the pointer obtained above.) */
3568 token = cp_lexer_peek_token (parser->lexer);
3570 switch (token->type)
3573 return cp_parser_identifier (parser);
3576 if (token->keyword == RID_OPERATOR)
3577 return cp_parser_operator_function_id (parser);
3581 cp_parser_error (parser, "expected id-expression");
3582 return error_mark_node;
3586 return cp_parser_unqualified_id (parser, template_keyword_p,
3587 /*check_dependency_p=*/true,
3592 /* Parse an unqualified-id.
3596 operator-function-id
3597 conversion-function-id
3601 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3602 keyword, in a construct like `A::template ...'.
3604 Returns a representation of unqualified-id. For the `identifier'
3605 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3606 production a BIT_NOT_EXPR is returned; the operand of the
3607 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3608 other productions, see the documentation accompanying the
3609 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3610 names are looked up in uninstantiated templates. If DECLARATOR_P
3611 is true, the unqualified-id is appearing as part of a declarator,
3612 rather than as part of an expression. */
3615 cp_parser_unqualified_id (cp_parser* parser,
3616 bool template_keyword_p,
3617 bool check_dependency_p,
3623 /* Peek at the next token. */
3624 token = cp_lexer_peek_token (parser->lexer);
3626 switch (token->type)
3632 /* We don't know yet whether or not this will be a
3634 cp_parser_parse_tentatively (parser);
3635 /* Try a template-id. */
3636 id = cp_parser_template_id (parser, template_keyword_p,
3639 /* If it worked, we're done. */
3640 if (cp_parser_parse_definitely (parser))
3642 /* Otherwise, it's an ordinary identifier. */
3643 return cp_parser_identifier (parser);
3646 case CPP_TEMPLATE_ID:
3647 return cp_parser_template_id (parser, template_keyword_p,
3654 tree qualifying_scope;
3659 /* Consume the `~' token. */
3660 cp_lexer_consume_token (parser->lexer);
3661 /* Parse the class-name. The standard, as written, seems to
3664 template <typename T> struct S { ~S (); };
3665 template <typename T> S<T>::~S() {}
3667 is invalid, since `~' must be followed by a class-name, but
3668 `S<T>' is dependent, and so not known to be a class.
3669 That's not right; we need to look in uninstantiated
3670 templates. A further complication arises from:
3672 template <typename T> void f(T t) {
3676 Here, it is not possible to look up `T' in the scope of `T'
3677 itself. We must look in both the current scope, and the
3678 scope of the containing complete expression.
3680 Yet another issue is:
3689 The standard does not seem to say that the `S' in `~S'
3690 should refer to the type `S' and not the data member
3693 /* DR 244 says that we look up the name after the "~" in the
3694 same scope as we looked up the qualifying name. That idea
3695 isn't fully worked out; it's more complicated than that. */
3696 scope = parser->scope;
3697 object_scope = parser->object_scope;
3698 qualifying_scope = parser->qualifying_scope;
3700 /* Check for invalid scopes. */
3701 if (scope == error_mark_node)
3703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3704 cp_lexer_consume_token (parser->lexer);
3705 return error_mark_node;
3707 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3709 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3710 error ("scope %qT before %<~%> is not a class-name", scope);
3711 cp_parser_simulate_error (parser);
3712 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3713 cp_lexer_consume_token (parser->lexer);
3714 return error_mark_node;
3716 gcc_assert (!scope || TYPE_P (scope));
3718 /* If the name is of the form "X::~X" it's OK. */
3719 token = cp_lexer_peek_token (parser->lexer);
3721 && token->type == CPP_NAME
3722 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3724 && constructor_name_p (token->u.value, scope))
3726 cp_lexer_consume_token (parser->lexer);
3727 return build_nt (BIT_NOT_EXPR, scope);
3730 /* If there was an explicit qualification (S::~T), first look
3731 in the scope given by the qualification (i.e., S). */
3733 type_decl = NULL_TREE;
3736 cp_parser_parse_tentatively (parser);
3737 type_decl = cp_parser_class_name (parser,
3738 /*typename_keyword_p=*/false,
3739 /*template_keyword_p=*/false,
3741 /*check_dependency=*/false,
3742 /*class_head_p=*/false,
3744 if (cp_parser_parse_definitely (parser))
3747 /* In "N::S::~S", look in "N" as well. */
3748 if (!done && scope && qualifying_scope)
3750 cp_parser_parse_tentatively (parser);
3751 parser->scope = qualifying_scope;
3752 parser->object_scope = NULL_TREE;
3753 parser->qualifying_scope = NULL_TREE;
3755 = cp_parser_class_name (parser,
3756 /*typename_keyword_p=*/false,
3757 /*template_keyword_p=*/false,
3759 /*check_dependency=*/false,
3760 /*class_head_p=*/false,
3762 if (cp_parser_parse_definitely (parser))
3765 /* In "p->S::~T", look in the scope given by "*p" as well. */
3766 else if (!done && object_scope)
3768 cp_parser_parse_tentatively (parser);
3769 parser->scope = object_scope;
3770 parser->object_scope = NULL_TREE;
3771 parser->qualifying_scope = NULL_TREE;
3773 = cp_parser_class_name (parser,
3774 /*typename_keyword_p=*/false,
3775 /*template_keyword_p=*/false,
3777 /*check_dependency=*/false,
3778 /*class_head_p=*/false,
3780 if (cp_parser_parse_definitely (parser))
3783 /* Look in the surrounding context. */
3786 parser->scope = NULL_TREE;
3787 parser->object_scope = NULL_TREE;
3788 parser->qualifying_scope = NULL_TREE;
3790 = cp_parser_class_name (parser,
3791 /*typename_keyword_p=*/false,
3792 /*template_keyword_p=*/false,
3794 /*check_dependency=*/false,
3795 /*class_head_p=*/false,
3798 /* If an error occurred, assume that the name of the
3799 destructor is the same as the name of the qualifying
3800 class. That allows us to keep parsing after running
3801 into ill-formed destructor names. */
3802 if (type_decl == error_mark_node && scope)
3803 return build_nt (BIT_NOT_EXPR, scope);
3804 else if (type_decl == error_mark_node)
3805 return error_mark_node;
3807 /* Check that destructor name and scope match. */
3808 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3810 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3811 error ("declaration of %<~%T%> as member of %qT",
3813 cp_parser_simulate_error (parser);
3814 return error_mark_node;
3819 A typedef-name that names a class shall not be used as the
3820 identifier in the declarator for a destructor declaration. */
3822 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3823 && !DECL_SELF_REFERENCE_P (type_decl)
3824 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3825 error ("typedef-name %qD used as destructor declarator",
3828 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3832 if (token->keyword == RID_OPERATOR)
3836 /* This could be a template-id, so we try that first. */
3837 cp_parser_parse_tentatively (parser);
3838 /* Try a template-id. */
3839 id = cp_parser_template_id (parser, template_keyword_p,
3840 /*check_dependency_p=*/true,
3842 /* If that worked, we're done. */
3843 if (cp_parser_parse_definitely (parser))
3845 /* We still don't know whether we're looking at an
3846 operator-function-id or a conversion-function-id. */
3847 cp_parser_parse_tentatively (parser);
3848 /* Try an operator-function-id. */
3849 id = cp_parser_operator_function_id (parser);
3850 /* If that didn't work, try a conversion-function-id. */
3851 if (!cp_parser_parse_definitely (parser))
3852 id = cp_parser_conversion_function_id (parser);
3861 cp_parser_error (parser, "expected unqualified-id");
3862 return error_mark_node;
3866 /* Parse an (optional) nested-name-specifier.
3868 nested-name-specifier:
3869 class-or-namespace-name :: nested-name-specifier [opt]
3870 class-or-namespace-name :: template nested-name-specifier [opt]
3872 PARSER->SCOPE should be set appropriately before this function is
3873 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3874 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3877 Sets PARSER->SCOPE to the class (TYPE) or namespace
3878 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3879 it unchanged if there is no nested-name-specifier. Returns the new
3880 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3882 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3883 part of a declaration and/or decl-specifier. */
3886 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3887 bool typename_keyword_p,
3888 bool check_dependency_p,
3890 bool is_declaration)
3892 bool success = false;
3893 cp_token_position start = 0;
3896 /* Remember where the nested-name-specifier starts. */
3897 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3899 start = cp_lexer_token_position (parser->lexer, false);
3900 push_deferring_access_checks (dk_deferred);
3907 tree saved_qualifying_scope;
3908 bool template_keyword_p;
3910 /* Spot cases that cannot be the beginning of a
3911 nested-name-specifier. */
3912 token = cp_lexer_peek_token (parser->lexer);
3914 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3915 the already parsed nested-name-specifier. */
3916 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3918 /* Grab the nested-name-specifier and continue the loop. */
3919 cp_parser_pre_parsed_nested_name_specifier (parser);
3920 /* If we originally encountered this nested-name-specifier
3921 with IS_DECLARATION set to false, we will not have
3922 resolved TYPENAME_TYPEs, so we must do so here. */
3924 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3926 new_scope = resolve_typename_type (parser->scope,
3927 /*only_current_p=*/false);
3928 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3929 parser->scope = new_scope;
3935 /* Spot cases that cannot be the beginning of a
3936 nested-name-specifier. On the second and subsequent times
3937 through the loop, we look for the `template' keyword. */
3938 if (success && token->keyword == RID_TEMPLATE)
3940 /* A template-id can start a nested-name-specifier. */
3941 else if (token->type == CPP_TEMPLATE_ID)
3945 /* If the next token is not an identifier, then it is
3946 definitely not a class-or-namespace-name. */
3947 if (token->type != CPP_NAME)
3949 /* If the following token is neither a `<' (to begin a
3950 template-id), nor a `::', then we are not looking at a
3951 nested-name-specifier. */
3952 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3953 if (token->type != CPP_SCOPE
3954 && !cp_parser_nth_token_starts_template_argument_list_p
3959 /* The nested-name-specifier is optional, so we parse
3961 cp_parser_parse_tentatively (parser);
3963 /* Look for the optional `template' keyword, if this isn't the
3964 first time through the loop. */
3966 template_keyword_p = cp_parser_optional_template_keyword (parser);
3968 template_keyword_p = false;
3970 /* Save the old scope since the name lookup we are about to do
3971 might destroy it. */
3972 old_scope = parser->scope;
3973 saved_qualifying_scope = parser->qualifying_scope;
3974 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3975 look up names in "X<T>::I" in order to determine that "Y" is
3976 a template. So, if we have a typename at this point, we make
3977 an effort to look through it. */
3979 && !typename_keyword_p
3981 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3982 parser->scope = resolve_typename_type (parser->scope,
3983 /*only_current_p=*/false);
3984 /* Parse the qualifying entity. */
3986 = cp_parser_class_or_namespace_name (parser,
3992 /* Look for the `::' token. */
3993 cp_parser_require (parser, CPP_SCOPE, "`::'");
3995 /* If we found what we wanted, we keep going; otherwise, we're
3997 if (!cp_parser_parse_definitely (parser))
3999 bool error_p = false;
4001 /* Restore the OLD_SCOPE since it was valid before the
4002 failed attempt at finding the last
4003 class-or-namespace-name. */
4004 parser->scope = old_scope;
4005 parser->qualifying_scope = saved_qualifying_scope;
4006 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4008 /* If the next token is an identifier, and the one after
4009 that is a `::', then any valid interpretation would have
4010 found a class-or-namespace-name. */
4011 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4012 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4014 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4017 token = cp_lexer_consume_token (parser->lexer);
4020 if (!token->ambiguous_p)
4023 tree ambiguous_decls;
4025 decl = cp_parser_lookup_name (parser, token->u.value,
4027 /*is_template=*/false,
4028 /*is_namespace=*/false,
4029 /*check_dependency=*/true,
4031 if (TREE_CODE (decl) == TEMPLATE_DECL)
4032 error ("%qD used without template parameters", decl);
4033 else if (ambiguous_decls)
4035 error ("reference to %qD is ambiguous",
4037 print_candidates (ambiguous_decls);
4038 decl = error_mark_node;
4041 cp_parser_name_lookup_error
4042 (parser, token->u.value, decl,
4043 "is not a class or namespace");
4045 parser->scope = error_mark_node;
4047 /* Treat this as a successful nested-name-specifier
4052 If the name found is not a class-name (clause
4053 _class_) or namespace-name (_namespace.def_), the
4054 program is ill-formed. */
4057 cp_lexer_consume_token (parser->lexer);
4061 /* We've found one valid nested-name-specifier. */
4063 /* Name lookup always gives us a DECL. */
4064 if (TREE_CODE (new_scope) == TYPE_DECL)
4065 new_scope = TREE_TYPE (new_scope);
4066 /* Uses of "template" must be followed by actual templates. */
4067 if (template_keyword_p
4068 && !(CLASS_TYPE_P (new_scope)
4069 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4070 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4071 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4072 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4073 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4074 == TEMPLATE_ID_EXPR)))
4075 pedwarn (TYPE_P (new_scope)
4076 ? "%qT is not a template"
4077 : "%qD is not a template",
4079 /* If it is a class scope, try to complete it; we are about to
4080 be looking up names inside the class. */
4081 if (TYPE_P (new_scope)
4082 /* Since checking types for dependency can be expensive,
4083 avoid doing it if the type is already complete. */
4084 && !COMPLETE_TYPE_P (new_scope)
4085 /* Do not try to complete dependent types. */
4086 && !dependent_type_p (new_scope))
4087 new_scope = complete_type (new_scope);
4088 /* Make sure we look in the right scope the next time through
4090 parser->scope = new_scope;
4093 /* If parsing tentatively, replace the sequence of tokens that makes
4094 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4095 token. That way, should we re-parse the token stream, we will
4096 not have to repeat the effort required to do the parse, nor will
4097 we issue duplicate error messages. */
4098 if (success && start)
4102 token = cp_lexer_token_at (parser->lexer, start);
4103 /* Reset the contents of the START token. */
4104 token->type = CPP_NESTED_NAME_SPECIFIER;
4105 /* Retrieve any deferred checks. Do not pop this access checks yet
4106 so the memory will not be reclaimed during token replacing below. */
4107 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4108 token->u.tree_check_value->value = parser->scope;
4109 token->u.tree_check_value->checks = get_deferred_access_checks ();
4110 token->u.tree_check_value->qualifying_scope =
4111 parser->qualifying_scope;
4112 token->keyword = RID_MAX;
4114 /* Purge all subsequent tokens. */
4115 cp_lexer_purge_tokens_after (parser->lexer, start);
4119 pop_to_parent_deferring_access_checks ();
4121 return success ? parser->scope : NULL_TREE;
4124 /* Parse a nested-name-specifier. See
4125 cp_parser_nested_name_specifier_opt for details. This function
4126 behaves identically, except that it will an issue an error if no
4127 nested-name-specifier is present. */
4130 cp_parser_nested_name_specifier (cp_parser *parser,
4131 bool typename_keyword_p,
4132 bool check_dependency_p,
4134 bool is_declaration)
4138 /* Look for the nested-name-specifier. */
4139 scope = cp_parser_nested_name_specifier_opt (parser,
4144 /* If it was not present, issue an error message. */
4147 cp_parser_error (parser, "expected nested-name-specifier");
4148 parser->scope = NULL_TREE;
4154 /* Parse a class-or-namespace-name.
4156 class-or-namespace-name:
4160 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4161 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4162 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4163 TYPE_P is TRUE iff the next name should be taken as a class-name,
4164 even the same name is declared to be another entity in the same
4167 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4168 specified by the class-or-namespace-name. If neither is found the
4169 ERROR_MARK_NODE is returned. */
4172 cp_parser_class_or_namespace_name (cp_parser *parser,
4173 bool typename_keyword_p,
4174 bool template_keyword_p,
4175 bool check_dependency_p,
4177 bool is_declaration)
4180 tree saved_qualifying_scope;
4181 tree saved_object_scope;
4185 /* Before we try to parse the class-name, we must save away the
4186 current PARSER->SCOPE since cp_parser_class_name will destroy
4188 saved_scope = parser->scope;
4189 saved_qualifying_scope = parser->qualifying_scope;
4190 saved_object_scope = parser->object_scope;
4191 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4192 there is no need to look for a namespace-name. */
4193 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4195 cp_parser_parse_tentatively (parser);
4196 scope = cp_parser_class_name (parser,
4199 type_p ? class_type : none_type,
4201 /*class_head_p=*/false,
4203 /* If that didn't work, try for a namespace-name. */
4204 if (!only_class_p && !cp_parser_parse_definitely (parser))
4206 /* Restore the saved scope. */
4207 parser->scope = saved_scope;
4208 parser->qualifying_scope = saved_qualifying_scope;
4209 parser->object_scope = saved_object_scope;
4210 /* If we are not looking at an identifier followed by the scope
4211 resolution operator, then this is not part of a
4212 nested-name-specifier. (Note that this function is only used
4213 to parse the components of a nested-name-specifier.) */
4214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4215 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4216 return error_mark_node;
4217 scope = cp_parser_namespace_name (parser);
4223 /* Parse a postfix-expression.
4227 postfix-expression [ expression ]
4228 postfix-expression ( expression-list [opt] )
4229 simple-type-specifier ( expression-list [opt] )
4230 typename :: [opt] nested-name-specifier identifier
4231 ( expression-list [opt] )
4232 typename :: [opt] nested-name-specifier template [opt] template-id
4233 ( expression-list [opt] )
4234 postfix-expression . template [opt] id-expression
4235 postfix-expression -> template [opt] id-expression
4236 postfix-expression . pseudo-destructor-name
4237 postfix-expression -> pseudo-destructor-name
4238 postfix-expression ++
4239 postfix-expression --
4240 dynamic_cast < type-id > ( expression )
4241 static_cast < type-id > ( expression )
4242 reinterpret_cast < type-id > ( expression )
4243 const_cast < type-id > ( expression )
4244 typeid ( expression )
4250 ( type-id ) { initializer-list , [opt] }
4252 This extension is a GNU version of the C99 compound-literal
4253 construct. (The C99 grammar uses `type-name' instead of `type-id',
4254 but they are essentially the same concept.)
4256 If ADDRESS_P is true, the postfix expression is the operand of the
4257 `&' operator. CAST_P is true if this expression is the target of a
4260 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4261 class member access expressions [expr.ref].
4263 Returns a representation of the expression. */
4266 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4267 bool member_access_only_p)
4271 cp_id_kind idk = CP_ID_KIND_NONE;
4272 tree postfix_expression = NULL_TREE;
4273 bool is_member_access = false;
4275 /* Peek at the next token. */
4276 token = cp_lexer_peek_token (parser->lexer);
4277 /* Some of the productions are determined by keywords. */
4278 keyword = token->keyword;
4288 const char *saved_message;
4290 /* All of these can be handled in the same way from the point
4291 of view of parsing. Begin by consuming the token
4292 identifying the cast. */
4293 cp_lexer_consume_token (parser->lexer);
4295 /* New types cannot be defined in the cast. */
4296 saved_message = parser->type_definition_forbidden_message;
4297 parser->type_definition_forbidden_message
4298 = "types may not be defined in casts";
4300 /* Look for the opening `<'. */
4301 cp_parser_require (parser, CPP_LESS, "`<'");
4302 /* Parse the type to which we are casting. */
4303 type = cp_parser_type_id (parser);
4304 /* Look for the closing `>'. */
4305 cp_parser_require (parser, CPP_GREATER, "`>'");
4306 /* Restore the old message. */
4307 parser->type_definition_forbidden_message = saved_message;
4309 /* And the expression which is being cast. */
4310 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4311 expression = cp_parser_expression (parser, /*cast_p=*/true);
4312 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4314 /* Only type conversions to integral or enumeration types
4315 can be used in constant-expressions. */
4316 if (!cast_valid_in_integral_constant_expression_p (type)
4317 && (cp_parser_non_integral_constant_expression
4319 "a cast to a type other than an integral or "
4320 "enumeration type")))
4321 return error_mark_node;
4327 = build_dynamic_cast (type, expression);
4331 = build_static_cast (type, expression);
4335 = build_reinterpret_cast (type, expression);
4339 = build_const_cast (type, expression);
4350 const char *saved_message;
4351 bool saved_in_type_id_in_expr_p;
4353 /* Consume the `typeid' token. */
4354 cp_lexer_consume_token (parser->lexer);
4355 /* Look for the `(' token. */
4356 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4357 /* Types cannot be defined in a `typeid' expression. */
4358 saved_message = parser->type_definition_forbidden_message;
4359 parser->type_definition_forbidden_message
4360 = "types may not be defined in a `typeid\' expression";
4361 /* We can't be sure yet whether we're looking at a type-id or an
4363 cp_parser_parse_tentatively (parser);
4364 /* Try a type-id first. */
4365 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4366 parser->in_type_id_in_expr_p = true;
4367 type = cp_parser_type_id (parser);
4368 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4369 /* Look for the `)' token. Otherwise, we can't be sure that
4370 we're not looking at an expression: consider `typeid (int
4371 (3))', for example. */
4372 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4373 /* If all went well, simply lookup the type-id. */
4374 if (cp_parser_parse_definitely (parser))
4375 postfix_expression = get_typeid (type);
4376 /* Otherwise, fall back to the expression variant. */
4381 /* Look for an expression. */
4382 expression = cp_parser_expression (parser, /*cast_p=*/false);
4383 /* Compute its typeid. */
4384 postfix_expression = build_typeid (expression);
4385 /* Look for the `)' token. */
4386 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4388 /* Restore the saved message. */
4389 parser->type_definition_forbidden_message = saved_message;
4390 /* `typeid' may not appear in an integral constant expression. */
4391 if (cp_parser_non_integral_constant_expression(parser,
4392 "`typeid' operator"))
4393 return error_mark_node;
4400 /* The syntax permitted here is the same permitted for an
4401 elaborated-type-specifier. */
4402 type = cp_parser_elaborated_type_specifier (parser,
4403 /*is_friend=*/false,
4404 /*is_declaration=*/false);
4405 postfix_expression = cp_parser_functional_cast (parser, type);
4413 /* If the next thing is a simple-type-specifier, we may be
4414 looking at a functional cast. We could also be looking at
4415 an id-expression. So, we try the functional cast, and if
4416 that doesn't work we fall back to the primary-expression. */
4417 cp_parser_parse_tentatively (parser);
4418 /* Look for the simple-type-specifier. */
4419 type = cp_parser_simple_type_specifier (parser,
4420 /*decl_specs=*/NULL,
4421 CP_PARSER_FLAGS_NONE);
4422 /* Parse the cast itself. */
4423 if (!cp_parser_error_occurred (parser))
4425 = cp_parser_functional_cast (parser, type);
4426 /* If that worked, we're done. */
4427 if (cp_parser_parse_definitely (parser))
4430 /* If the functional-cast didn't work out, try a
4431 compound-literal. */
4432 if (cp_parser_allow_gnu_extensions_p (parser)
4433 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4435 VEC(constructor_elt,gc) *initializer_list = NULL;
4436 bool saved_in_type_id_in_expr_p;
4438 cp_parser_parse_tentatively (parser);
4439 /* Consume the `('. */
4440 cp_lexer_consume_token (parser->lexer);
4441 /* Parse the type. */
4442 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4443 parser->in_type_id_in_expr_p = true;
4444 type = cp_parser_type_id (parser);
4445 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4446 /* Look for the `)'. */
4447 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4448 /* Look for the `{'. */
4449 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4450 /* If things aren't going well, there's no need to
4452 if (!cp_parser_error_occurred (parser))
4454 bool non_constant_p;
4455 /* Parse the initializer-list. */
4457 = cp_parser_initializer_list (parser, &non_constant_p);
4458 /* Allow a trailing `,'. */
4459 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4460 cp_lexer_consume_token (parser->lexer);
4461 /* Look for the final `}'. */
4462 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4464 /* If that worked, we're definitely looking at a
4465 compound-literal expression. */
4466 if (cp_parser_parse_definitely (parser))
4468 /* Warn the user that a compound literal is not
4469 allowed in standard C++. */
4471 pedwarn ("ISO C++ forbids compound-literals");
4472 /* For simplicity, we disallow compound literals in
4473 constant-expressions. We could
4474 allow compound literals of integer type, whose
4475 initializer was a constant, in constant
4476 expressions. Permitting that usage, as a further
4477 extension, would not change the meaning of any
4478 currently accepted programs. (Of course, as
4479 compound literals are not part of ISO C++, the
4480 standard has nothing to say.) */
4481 if (cp_parser_non_integral_constant_expression
4482 (parser, "non-constant compound literals"))
4484 postfix_expression = error_mark_node;
4487 /* Form the representation of the compound-literal. */
4489 = finish_compound_literal (type, initializer_list);
4494 /* It must be a primary-expression. */
4496 = cp_parser_primary_expression (parser, address_p, cast_p,
4497 /*template_arg_p=*/false,
4503 /* Keep looping until the postfix-expression is complete. */
4506 if (idk == CP_ID_KIND_UNQUALIFIED
4507 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4508 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4509 /* It is not a Koenig lookup function call. */
4511 = unqualified_name_lookup_error (postfix_expression);
4513 /* Peek at the next token. */
4514 token = cp_lexer_peek_token (parser->lexer);
4516 switch (token->type)
4518 case CPP_OPEN_SQUARE:
4520 = cp_parser_postfix_open_square_expression (parser,
4523 idk = CP_ID_KIND_NONE;
4524 is_member_access = false;
4527 case CPP_OPEN_PAREN:
4528 /* postfix-expression ( expression-list [opt] ) */
4531 bool is_builtin_constant_p;
4532 bool saved_integral_constant_expression_p = false;
4533 bool saved_non_integral_constant_expression_p = false;
4536 is_member_access = false;
4538 is_builtin_constant_p
4539 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4540 if (is_builtin_constant_p)
4542 /* The whole point of __builtin_constant_p is to allow
4543 non-constant expressions to appear as arguments. */
4544 saved_integral_constant_expression_p
4545 = parser->integral_constant_expression_p;
4546 saved_non_integral_constant_expression_p
4547 = parser->non_integral_constant_expression_p;
4548 parser->integral_constant_expression_p = false;
4550 args = (cp_parser_parenthesized_expression_list
4551 (parser, /*is_attribute_list=*/false,
4552 /*cast_p=*/false, /*allow_expansion_p=*/true,
4553 /*non_constant_p=*/NULL));
4554 if (is_builtin_constant_p)
4556 parser->integral_constant_expression_p
4557 = saved_integral_constant_expression_p;
4558 parser->non_integral_constant_expression_p
4559 = saved_non_integral_constant_expression_p;
4562 if (args == error_mark_node)
4564 postfix_expression = error_mark_node;
4568 /* Function calls are not permitted in
4569 constant-expressions. */
4570 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4571 && cp_parser_non_integral_constant_expression (parser,
4574 postfix_expression = error_mark_node;
4579 if (idk == CP_ID_KIND_UNQUALIFIED)
4581 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4587 = perform_koenig_lookup (postfix_expression, args);
4591 = unqualified_fn_lookup_error (postfix_expression);
4593 /* We do not perform argument-dependent lookup if
4594 normal lookup finds a non-function, in accordance
4595 with the expected resolution of DR 218. */
4596 else if (args && is_overloaded_fn (postfix_expression))
4598 tree fn = get_first_fn (postfix_expression);
4600 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4601 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4603 /* Only do argument dependent lookup if regular
4604 lookup does not find a set of member functions.
4605 [basic.lookup.koenig]/2a */
4606 if (!DECL_FUNCTION_MEMBER_P (fn))
4610 = perform_koenig_lookup (postfix_expression, args);
4615 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4617 tree instance = TREE_OPERAND (postfix_expression, 0);
4618 tree fn = TREE_OPERAND (postfix_expression, 1);
4620 if (processing_template_decl
4621 && (type_dependent_expression_p (instance)
4622 || (!BASELINK_P (fn)
4623 && TREE_CODE (fn) != FIELD_DECL)
4624 || type_dependent_expression_p (fn)
4625 || any_type_dependent_arguments_p (args)))
4628 = build_nt_call_list (postfix_expression, args);
4632 if (BASELINK_P (fn))
4634 = (build_new_method_call
4635 (instance, fn, args, NULL_TREE,
4636 (idk == CP_ID_KIND_QUALIFIED
4637 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4641 = finish_call_expr (postfix_expression, args,
4642 /*disallow_virtual=*/false,
4643 /*koenig_p=*/false);
4645 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4646 || TREE_CODE (postfix_expression) == MEMBER_REF
4647 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4648 postfix_expression = (build_offset_ref_call_from_tree
4649 (postfix_expression, args));
4650 else if (idk == CP_ID_KIND_QUALIFIED)
4651 /* A call to a static class member, or a namespace-scope
4654 = finish_call_expr (postfix_expression, args,
4655 /*disallow_virtual=*/true,
4658 /* All other function calls. */
4660 = finish_call_expr (postfix_expression, args,
4661 /*disallow_virtual=*/false,
4664 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4665 idk = CP_ID_KIND_NONE;
4671 /* postfix-expression . template [opt] id-expression
4672 postfix-expression . pseudo-destructor-name
4673 postfix-expression -> template [opt] id-expression
4674 postfix-expression -> pseudo-destructor-name */
4676 /* Consume the `.' or `->' operator. */
4677 cp_lexer_consume_token (parser->lexer);
4680 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4684 is_member_access = true;
4688 /* postfix-expression ++ */
4689 /* Consume the `++' token. */
4690 cp_lexer_consume_token (parser->lexer);
4691 /* Generate a representation for the complete expression. */
4693 = finish_increment_expr (postfix_expression,
4694 POSTINCREMENT_EXPR);
4695 /* Increments may not appear in constant-expressions. */
4696 if (cp_parser_non_integral_constant_expression (parser,
4698 postfix_expression = error_mark_node;
4699 idk = CP_ID_KIND_NONE;
4700 is_member_access = false;
4703 case CPP_MINUS_MINUS:
4704 /* postfix-expression -- */
4705 /* Consume the `--' token. */
4706 cp_lexer_consume_token (parser->lexer);
4707 /* Generate a representation for the complete expression. */
4709 = finish_increment_expr (postfix_expression,
4710 POSTDECREMENT_EXPR);
4711 /* Decrements may not appear in constant-expressions. */
4712 if (cp_parser_non_integral_constant_expression (parser,
4714 postfix_expression = error_mark_node;
4715 idk = CP_ID_KIND_NONE;
4716 is_member_access = false;
4720 if (member_access_only_p)
4721 return is_member_access? postfix_expression : error_mark_node;
4723 return postfix_expression;
4727 /* We should never get here. */
4729 return error_mark_node;
4732 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4733 by cp_parser_builtin_offsetof. We're looking for
4735 postfix-expression [ expression ]
4737 FOR_OFFSETOF is set if we're being called in that context, which
4738 changes how we deal with integer constant expressions. */
4741 cp_parser_postfix_open_square_expression (cp_parser *parser,
4742 tree postfix_expression,
4747 /* Consume the `[' token. */
4748 cp_lexer_consume_token (parser->lexer);
4750 /* Parse the index expression. */
4751 /* ??? For offsetof, there is a question of what to allow here. If
4752 offsetof is not being used in an integral constant expression context,
4753 then we *could* get the right answer by computing the value at runtime.
4754 If we are in an integral constant expression context, then we might
4755 could accept any constant expression; hard to say without analysis.
4756 Rather than open the barn door too wide right away, allow only integer
4757 constant expressions here. */
4759 index = cp_parser_constant_expression (parser, false, NULL);
4761 index = cp_parser_expression (parser, /*cast_p=*/false);
4763 /* Look for the closing `]'. */
4764 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4766 /* Build the ARRAY_REF. */
4767 postfix_expression = grok_array_decl (postfix_expression, index);
4769 /* When not doing offsetof, array references are not permitted in
4770 constant-expressions. */
4772 && (cp_parser_non_integral_constant_expression
4773 (parser, "an array reference")))
4774 postfix_expression = error_mark_node;
4776 return postfix_expression;
4779 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4780 by cp_parser_builtin_offsetof. We're looking for
4782 postfix-expression . template [opt] id-expression
4783 postfix-expression . pseudo-destructor-name
4784 postfix-expression -> template [opt] id-expression
4785 postfix-expression -> pseudo-destructor-name
4787 FOR_OFFSETOF is set if we're being called in that context. That sorta
4788 limits what of the above we'll actually accept, but nevermind.
4789 TOKEN_TYPE is the "." or "->" token, which will already have been
4790 removed from the stream. */
4793 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4794 enum cpp_ttype token_type,
4795 tree postfix_expression,
4796 bool for_offsetof, cp_id_kind *idk)
4800 bool pseudo_destructor_p;
4801 tree scope = NULL_TREE;
4803 /* If this is a `->' operator, dereference the pointer. */
4804 if (token_type == CPP_DEREF)
4805 postfix_expression = build_x_arrow (postfix_expression);
4806 /* Check to see whether or not the expression is type-dependent. */
4807 dependent_p = type_dependent_expression_p (postfix_expression);
4808 /* The identifier following the `->' or `.' is not qualified. */
4809 parser->scope = NULL_TREE;
4810 parser->qualifying_scope = NULL_TREE;
4811 parser->object_scope = NULL_TREE;
4812 *idk = CP_ID_KIND_NONE;
4813 /* Enter the scope corresponding to the type of the object
4814 given by the POSTFIX_EXPRESSION. */
4815 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4817 scope = TREE_TYPE (postfix_expression);
4818 /* According to the standard, no expression should ever have
4819 reference type. Unfortunately, we do not currently match
4820 the standard in this respect in that our internal representation
4821 of an expression may have reference type even when the standard
4822 says it does not. Therefore, we have to manually obtain the
4823 underlying type here. */
4824 scope = non_reference (scope);
4825 /* The type of the POSTFIX_EXPRESSION must be complete. */
4826 if (scope == unknown_type_node)
4828 error ("%qE does not have class type", postfix_expression);
4832 scope = complete_type_or_else (scope, NULL_TREE);
4833 /* Let the name lookup machinery know that we are processing a
4834 class member access expression. */
4835 parser->context->object_type = scope;
4836 /* If something went wrong, we want to be able to discern that case,
4837 as opposed to the case where there was no SCOPE due to the type
4838 of expression being dependent. */
4840 scope = error_mark_node;
4841 /* If the SCOPE was erroneous, make the various semantic analysis
4842 functions exit quickly -- and without issuing additional error
4844 if (scope == error_mark_node)
4845 postfix_expression = error_mark_node;
4848 /* Assume this expression is not a pseudo-destructor access. */
4849 pseudo_destructor_p = false;
4851 /* If the SCOPE is a scalar type, then, if this is a valid program,
4852 we must be looking at a pseudo-destructor-name. */
4853 if (scope && SCALAR_TYPE_P (scope))
4858 cp_parser_parse_tentatively (parser);
4859 /* Parse the pseudo-destructor-name. */
4861 cp_parser_pseudo_destructor_name (parser, &s, &type);
4862 if (cp_parser_parse_definitely (parser))
4864 pseudo_destructor_p = true;
4866 = finish_pseudo_destructor_expr (postfix_expression,
4867 s, TREE_TYPE (type));
4871 if (!pseudo_destructor_p)
4873 /* If the SCOPE is not a scalar type, we are looking at an
4874 ordinary class member access expression, rather than a
4875 pseudo-destructor-name. */
4877 /* Parse the id-expression. */
4878 name = (cp_parser_id_expression
4880 cp_parser_optional_template_keyword (parser),
4881 /*check_dependency_p=*/true,
4883 /*declarator_p=*/false,
4884 /*optional_p=*/false));
4885 /* In general, build a SCOPE_REF if the member name is qualified.
4886 However, if the name was not dependent and has already been
4887 resolved; there is no need to build the SCOPE_REF. For example;
4889 struct X { void f(); };
4890 template <typename T> void f(T* t) { t->X::f(); }
4892 Even though "t" is dependent, "X::f" is not and has been resolved
4893 to a BASELINK; there is no need to include scope information. */
4895 /* But we do need to remember that there was an explicit scope for
4896 virtual function calls. */
4898 *idk = CP_ID_KIND_QUALIFIED;
4900 /* If the name is a template-id that names a type, we will get a
4901 TYPE_DECL here. That is invalid code. */
4902 if (TREE_CODE (name) == TYPE_DECL)
4904 error ("invalid use of %qD", name);
4905 postfix_expression = error_mark_node;
4909 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4911 name = build_qualified_name (/*type=*/NULL_TREE,
4915 parser->scope = NULL_TREE;
4916 parser->qualifying_scope = NULL_TREE;
4917 parser->object_scope = NULL_TREE;
4919 if (scope && name && BASELINK_P (name))
4920 adjust_result_of_qualified_name_lookup
4921 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4923 = finish_class_member_access_expr (postfix_expression, name,
4928 /* We no longer need to look up names in the scope of the object on
4929 the left-hand side of the `.' or `->' operator. */
4930 parser->context->object_type = NULL_TREE;
4932 /* Outside of offsetof, these operators may not appear in
4933 constant-expressions. */
4935 && (cp_parser_non_integral_constant_expression
4936 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4937 postfix_expression = error_mark_node;
4939 return postfix_expression;
4942 /* Parse a parenthesized expression-list.
4945 assignment-expression
4946 expression-list, assignment-expression
4951 identifier, expression-list
4953 CAST_P is true if this expression is the target of a cast.
4955 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4958 Returns a TREE_LIST. The TREE_VALUE of each node is a
4959 representation of an assignment-expression. Note that a TREE_LIST
4960 is returned even if there is only a single expression in the list.
4961 error_mark_node is returned if the ( and or ) are
4962 missing. NULL_TREE is returned on no expressions. The parentheses
4963 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4964 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4965 indicates whether or not all of the expressions in the list were
4969 cp_parser_parenthesized_expression_list (cp_parser* parser,
4970 bool is_attribute_list,
4972 bool allow_expansion_p,
4973 bool *non_constant_p)
4975 tree expression_list = NULL_TREE;
4976 bool fold_expr_p = is_attribute_list;
4977 tree identifier = NULL_TREE;
4979 /* Assume all the expressions will be constant. */
4981 *non_constant_p = false;
4983 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4984 return error_mark_node;
4986 /* Consume expressions until there are no more. */
4987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4992 /* At the beginning of attribute lists, check to see if the
4993 next token is an identifier. */
4994 if (is_attribute_list
4995 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4999 /* Consume the identifier. */
5000 token = cp_lexer_consume_token (parser->lexer);
5001 /* Save the identifier. */
5002 identifier = token->u.value;
5006 /* Parse the next assignment-expression. */
5009 bool expr_non_constant_p;
5010 expr = (cp_parser_constant_expression
5011 (parser, /*allow_non_constant_p=*/true,
5012 &expr_non_constant_p));
5013 if (expr_non_constant_p)
5014 *non_constant_p = true;
5017 expr = cp_parser_assignment_expression (parser, cast_p);
5020 expr = fold_non_dependent_expr (expr);
5022 /* If we have an ellipsis, then this is an expression
5024 if (allow_expansion_p
5025 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5027 /* Consume the `...'. */
5028 cp_lexer_consume_token (parser->lexer);
5030 /* Build the argument pack. */
5031 expr = make_pack_expansion (expr);
5034 /* Add it to the list. We add error_mark_node
5035 expressions to the list, so that we can still tell if
5036 the correct form for a parenthesized expression-list
5037 is found. That gives better errors. */
5038 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5040 if (expr == error_mark_node)
5044 /* After the first item, attribute lists look the same as
5045 expression lists. */
5046 is_attribute_list = false;
5049 /* If the next token isn't a `,', then we are done. */
5050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5053 /* Otherwise, consume the `,' and keep going. */
5054 cp_lexer_consume_token (parser->lexer);
5057 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5062 /* We try and resync to an unnested comma, as that will give the
5063 user better diagnostics. */
5064 ending = cp_parser_skip_to_closing_parenthesis (parser,
5065 /*recovering=*/true,
5067 /*consume_paren=*/true);
5071 return error_mark_node;
5074 /* We built up the list in reverse order so we must reverse it now. */
5075 expression_list = nreverse (expression_list);
5077 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5079 return expression_list;
5082 /* Parse a pseudo-destructor-name.
5084 pseudo-destructor-name:
5085 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5086 :: [opt] nested-name-specifier template template-id :: ~ type-name
5087 :: [opt] nested-name-specifier [opt] ~ type-name
5089 If either of the first two productions is used, sets *SCOPE to the
5090 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5091 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5092 or ERROR_MARK_NODE if the parse fails. */
5095 cp_parser_pseudo_destructor_name (cp_parser* parser,
5099 bool nested_name_specifier_p;
5101 /* Assume that things will not work out. */
5102 *type = error_mark_node;
5104 /* Look for the optional `::' operator. */
5105 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5106 /* Look for the optional nested-name-specifier. */
5107 nested_name_specifier_p
5108 = (cp_parser_nested_name_specifier_opt (parser,
5109 /*typename_keyword_p=*/false,
5110 /*check_dependency_p=*/true,
5112 /*is_declaration=*/true)
5114 /* Now, if we saw a nested-name-specifier, we might be doing the
5115 second production. */
5116 if (nested_name_specifier_p
5117 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5119 /* Consume the `template' keyword. */
5120 cp_lexer_consume_token (parser->lexer);
5121 /* Parse the template-id. */
5122 cp_parser_template_id (parser,
5123 /*template_keyword_p=*/true,
5124 /*check_dependency_p=*/false,
5125 /*is_declaration=*/true);
5126 /* Look for the `::' token. */
5127 cp_parser_require (parser, CPP_SCOPE, "`::'");
5129 /* If the next token is not a `~', then there might be some
5130 additional qualification. */
5131 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5133 /* Look for the type-name. */
5134 *scope = TREE_TYPE (cp_parser_type_name (parser));
5136 if (*scope == error_mark_node)
5139 /* If we don't have ::~, then something has gone wrong. Since
5140 the only caller of this function is looking for something
5141 after `.' or `->' after a scalar type, most likely the
5142 program is trying to get a member of a non-aggregate
5144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5145 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5147 cp_parser_error (parser, "request for member of non-aggregate type");
5151 /* Look for the `::' token. */
5152 cp_parser_require (parser, CPP_SCOPE, "`::'");
5157 /* Look for the `~'. */
5158 cp_parser_require (parser, CPP_COMPL, "`~'");
5159 /* Look for the type-name again. We are not responsible for
5160 checking that it matches the first type-name. */
5161 *type = cp_parser_type_name (parser);
5164 /* Parse a unary-expression.
5170 unary-operator cast-expression
5171 sizeof unary-expression
5179 __extension__ cast-expression
5180 __alignof__ unary-expression
5181 __alignof__ ( type-id )
5182 __real__ cast-expression
5183 __imag__ cast-expression
5186 ADDRESS_P is true iff the unary-expression is appearing as the
5187 operand of the `&' operator. CAST_P is true if this expression is
5188 the target of a cast.
5190 Returns a representation of the expression. */
5193 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5196 enum tree_code unary_operator;
5198 /* Peek at the next token. */
5199 token = cp_lexer_peek_token (parser->lexer);
5200 /* Some keywords give away the kind of expression. */
5201 if (token->type == CPP_KEYWORD)
5203 enum rid keyword = token->keyword;
5213 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5214 /* Consume the token. */
5215 cp_lexer_consume_token (parser->lexer);
5216 /* Parse the operand. */
5217 operand = cp_parser_sizeof_operand (parser, keyword);
5219 if (TYPE_P (operand))
5220 return cxx_sizeof_or_alignof_type (operand, op, true);
5222 return cxx_sizeof_or_alignof_expr (operand, op);
5226 return cp_parser_new_expression (parser);
5229 return cp_parser_delete_expression (parser);
5233 /* The saved value of the PEDANTIC flag. */
5237 /* Save away the PEDANTIC flag. */
5238 cp_parser_extension_opt (parser, &saved_pedantic);
5239 /* Parse the cast-expression. */
5240 expr = cp_parser_simple_cast_expression (parser);
5241 /* Restore the PEDANTIC flag. */
5242 pedantic = saved_pedantic;
5252 /* Consume the `__real__' or `__imag__' token. */
5253 cp_lexer_consume_token (parser->lexer);
5254 /* Parse the cast-expression. */
5255 expression = cp_parser_simple_cast_expression (parser);
5256 /* Create the complete representation. */
5257 return build_x_unary_op ((keyword == RID_REALPART
5258 ? REALPART_EXPR : IMAGPART_EXPR),
5268 /* Look for the `:: new' and `:: delete', which also signal the
5269 beginning of a new-expression, or delete-expression,
5270 respectively. If the next token is `::', then it might be one of
5272 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5276 /* See if the token after the `::' is one of the keywords in
5277 which we're interested. */
5278 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5279 /* If it's `new', we have a new-expression. */
5280 if (keyword == RID_NEW)
5281 return cp_parser_new_expression (parser);
5282 /* Similarly, for `delete'. */
5283 else if (keyword == RID_DELETE)
5284 return cp_parser_delete_expression (parser);
5287 /* Look for a unary operator. */
5288 unary_operator = cp_parser_unary_operator (token);
5289 /* The `++' and `--' operators can be handled similarly, even though
5290 they are not technically unary-operators in the grammar. */
5291 if (unary_operator == ERROR_MARK)
5293 if (token->type == CPP_PLUS_PLUS)
5294 unary_operator = PREINCREMENT_EXPR;
5295 else if (token->type == CPP_MINUS_MINUS)
5296 unary_operator = PREDECREMENT_EXPR;
5297 /* Handle the GNU address-of-label extension. */
5298 else if (cp_parser_allow_gnu_extensions_p (parser)
5299 && token->type == CPP_AND_AND)
5303 /* Consume the '&&' token. */
5304 cp_lexer_consume_token (parser->lexer);
5305 /* Look for the identifier. */
5306 identifier = cp_parser_identifier (parser);
5307 /* Create an expression representing the address. */
5308 return finish_label_address_expr (identifier);
5311 if (unary_operator != ERROR_MARK)
5313 tree cast_expression;
5314 tree expression = error_mark_node;
5315 const char *non_constant_p = NULL;
5317 /* Consume the operator token. */
5318 token = cp_lexer_consume_token (parser->lexer);
5319 /* Parse the cast-expression. */
5321 = cp_parser_cast_expression (parser,
5322 unary_operator == ADDR_EXPR,
5324 /* Now, build an appropriate representation. */
5325 switch (unary_operator)
5328 non_constant_p = "`*'";
5329 expression = build_x_indirect_ref (cast_expression, "unary *");
5333 non_constant_p = "`&'";
5336 expression = build_x_unary_op (unary_operator, cast_expression);
5339 case PREINCREMENT_EXPR:
5340 case PREDECREMENT_EXPR:
5341 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5344 case UNARY_PLUS_EXPR:
5346 case TRUTH_NOT_EXPR:
5347 expression = finish_unary_op_expr (unary_operator, cast_expression);
5355 && cp_parser_non_integral_constant_expression (parser,
5357 expression = error_mark_node;
5362 return cp_parser_postfix_expression (parser, address_p, cast_p,
5363 /*member_access_only_p=*/false);
5366 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5367 unary-operator, the corresponding tree code is returned. */
5369 static enum tree_code
5370 cp_parser_unary_operator (cp_token* token)
5372 switch (token->type)
5375 return INDIRECT_REF;
5381 return UNARY_PLUS_EXPR;
5387 return TRUTH_NOT_EXPR;
5390 return BIT_NOT_EXPR;
5397 /* Parse a new-expression.
5400 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5401 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5403 Returns a representation of the expression. */
5406 cp_parser_new_expression (cp_parser* parser)
5408 bool global_scope_p;
5414 /* Look for the optional `::' operator. */
5416 = (cp_parser_global_scope_opt (parser,
5417 /*current_scope_valid_p=*/false)
5419 /* Look for the `new' operator. */
5420 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5421 /* There's no easy way to tell a new-placement from the
5422 `( type-id )' construct. */
5423 cp_parser_parse_tentatively (parser);
5424 /* Look for a new-placement. */
5425 placement = cp_parser_new_placement (parser);
5426 /* If that didn't work out, there's no new-placement. */
5427 if (!cp_parser_parse_definitely (parser))
5428 placement = NULL_TREE;
5430 /* If the next token is a `(', then we have a parenthesized
5432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5434 /* Consume the `('. */
5435 cp_lexer_consume_token (parser->lexer);
5436 /* Parse the type-id. */
5437 type = cp_parser_type_id (parser);
5438 /* Look for the closing `)'. */
5439 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5440 /* There should not be a direct-new-declarator in this production,
5441 but GCC used to allowed this, so we check and emit a sensible error
5442 message for this case. */
5443 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5445 error ("array bound forbidden after parenthesized type-id");
5446 inform ("try removing the parentheses around the type-id");
5447 cp_parser_direct_new_declarator (parser);
5451 /* Otherwise, there must be a new-type-id. */
5453 type = cp_parser_new_type_id (parser, &nelts);
5455 /* If the next token is a `(', then we have a new-initializer. */
5456 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5457 initializer = cp_parser_new_initializer (parser);
5459 initializer = NULL_TREE;
5461 /* A new-expression may not appear in an integral constant
5463 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5464 return error_mark_node;
5466 /* Create a representation of the new-expression. */
5467 return build_new (placement, type, nelts, initializer, global_scope_p);
5470 /* Parse a new-placement.
5475 Returns the same representation as for an expression-list. */
5478 cp_parser_new_placement (cp_parser* parser)
5480 tree expression_list;
5482 /* Parse the expression-list. */
5483 expression_list = (cp_parser_parenthesized_expression_list
5484 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5485 /*non_constant_p=*/NULL));
5487 return expression_list;
5490 /* Parse a new-type-id.
5493 type-specifier-seq new-declarator [opt]
5495 Returns the TYPE allocated. If the new-type-id indicates an array
5496 type, *NELTS is set to the number of elements in the last array
5497 bound; the TYPE will not include the last array bound. */
5500 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5502 cp_decl_specifier_seq type_specifier_seq;
5503 cp_declarator *new_declarator;
5504 cp_declarator *declarator;
5505 cp_declarator *outer_declarator;
5506 const char *saved_message;
5509 /* The type-specifier sequence must not contain type definitions.
5510 (It cannot contain declarations of new types either, but if they
5511 are not definitions we will catch that because they are not
5513 saved_message = parser->type_definition_forbidden_message;
5514 parser->type_definition_forbidden_message
5515 = "types may not be defined in a new-type-id";
5516 /* Parse the type-specifier-seq. */
5517 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5518 &type_specifier_seq);
5519 /* Restore the old message. */
5520 parser->type_definition_forbidden_message = saved_message;
5521 /* Parse the new-declarator. */
5522 new_declarator = cp_parser_new_declarator_opt (parser);
5524 /* Determine the number of elements in the last array dimension, if
5527 /* Skip down to the last array dimension. */
5528 declarator = new_declarator;
5529 outer_declarator = NULL;
5530 while (declarator && (declarator->kind == cdk_pointer
5531 || declarator->kind == cdk_ptrmem))
5533 outer_declarator = declarator;
5534 declarator = declarator->declarator;
5537 && declarator->kind == cdk_array
5538 && declarator->declarator
5539 && declarator->declarator->kind == cdk_array)
5541 outer_declarator = declarator;
5542 declarator = declarator->declarator;
5545 if (declarator && declarator->kind == cdk_array)
5547 *nelts = declarator->u.array.bounds;
5548 if (*nelts == error_mark_node)
5549 *nelts = integer_one_node;
5551 if (outer_declarator)
5552 outer_declarator->declarator = declarator->declarator;
5554 new_declarator = NULL;
5557 type = groktypename (&type_specifier_seq, new_declarator);
5561 /* Parse an (optional) new-declarator.
5564 ptr-operator new-declarator [opt]
5565 direct-new-declarator
5567 Returns the declarator. */
5569 static cp_declarator *
5570 cp_parser_new_declarator_opt (cp_parser* parser)
5572 enum tree_code code;
5574 cp_cv_quals cv_quals;
5576 /* We don't know if there's a ptr-operator next, or not. */
5577 cp_parser_parse_tentatively (parser);
5578 /* Look for a ptr-operator. */
5579 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5580 /* If that worked, look for more new-declarators. */
5581 if (cp_parser_parse_definitely (parser))
5583 cp_declarator *declarator;
5585 /* Parse another optional declarator. */
5586 declarator = cp_parser_new_declarator_opt (parser);
5588 return cp_parser_make_indirect_declarator
5589 (code, type, cv_quals, declarator);
5592 /* If the next token is a `[', there is a direct-new-declarator. */
5593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5594 return cp_parser_direct_new_declarator (parser);
5599 /* Parse a direct-new-declarator.
5601 direct-new-declarator:
5603 direct-new-declarator [constant-expression]
5607 static cp_declarator *
5608 cp_parser_direct_new_declarator (cp_parser* parser)
5610 cp_declarator *declarator = NULL;
5616 /* Look for the opening `['. */
5617 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5618 /* The first expression is not required to be constant. */
5621 expression = cp_parser_expression (parser, /*cast_p=*/false);
5622 /* The standard requires that the expression have integral
5623 type. DR 74 adds enumeration types. We believe that the
5624 real intent is that these expressions be handled like the
5625 expression in a `switch' condition, which also allows
5626 classes with a single conversion to integral or
5627 enumeration type. */
5628 if (!processing_template_decl)
5631 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5636 error ("expression in new-declarator must have integral "
5637 "or enumeration type");
5638 expression = error_mark_node;
5642 /* But all the other expressions must be. */
5645 = cp_parser_constant_expression (parser,
5646 /*allow_non_constant=*/false,
5648 /* Look for the closing `]'. */
5649 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5651 /* Add this bound to the declarator. */
5652 declarator = make_array_declarator (declarator, expression);
5654 /* If the next token is not a `[', then there are no more
5656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5663 /* Parse a new-initializer.
5666 ( expression-list [opt] )
5668 Returns a representation of the expression-list. If there is no
5669 expression-list, VOID_ZERO_NODE is returned. */
5672 cp_parser_new_initializer (cp_parser* parser)
5674 tree expression_list;
5676 expression_list = (cp_parser_parenthesized_expression_list
5677 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5678 /*non_constant_p=*/NULL));
5679 if (!expression_list)
5680 expression_list = void_zero_node;
5682 return expression_list;
5685 /* Parse a delete-expression.
5688 :: [opt] delete cast-expression
5689 :: [opt] delete [ ] cast-expression
5691 Returns a representation of the expression. */
5694 cp_parser_delete_expression (cp_parser* parser)
5696 bool global_scope_p;
5700 /* Look for the optional `::' operator. */
5702 = (cp_parser_global_scope_opt (parser,
5703 /*current_scope_valid_p=*/false)
5705 /* Look for the `delete' keyword. */
5706 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5707 /* See if the array syntax is in use. */
5708 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5710 /* Consume the `[' token. */
5711 cp_lexer_consume_token (parser->lexer);
5712 /* Look for the `]' token. */
5713 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5714 /* Remember that this is the `[]' construct. */
5720 /* Parse the cast-expression. */
5721 expression = cp_parser_simple_cast_expression (parser);
5723 /* A delete-expression may not appear in an integral constant
5725 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5726 return error_mark_node;
5728 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5731 /* Parse a cast-expression.
5735 ( type-id ) cast-expression
5737 ADDRESS_P is true iff the unary-expression is appearing as the
5738 operand of the `&' operator. CAST_P is true if this expression is
5739 the target of a cast.
5741 Returns a representation of the expression. */
5744 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5746 /* If it's a `(', then we might be looking at a cast. */
5747 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5749 tree type = NULL_TREE;
5750 tree expr = NULL_TREE;
5751 bool compound_literal_p;
5752 const char *saved_message;
5754 /* There's no way to know yet whether or not this is a cast.
5755 For example, `(int (3))' is a unary-expression, while `(int)
5756 3' is a cast. So, we resort to parsing tentatively. */
5757 cp_parser_parse_tentatively (parser);
5758 /* Types may not be defined in a cast. */
5759 saved_message = parser->type_definition_forbidden_message;
5760 parser->type_definition_forbidden_message
5761 = "types may not be defined in casts";
5762 /* Consume the `('. */
5763 cp_lexer_consume_token (parser->lexer);
5764 /* A very tricky bit is that `(struct S) { 3 }' is a
5765 compound-literal (which we permit in C++ as an extension).
5766 But, that construct is not a cast-expression -- it is a
5767 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5768 is legal; if the compound-literal were a cast-expression,
5769 you'd need an extra set of parentheses.) But, if we parse
5770 the type-id, and it happens to be a class-specifier, then we
5771 will commit to the parse at that point, because we cannot
5772 undo the action that is done when creating a new class. So,
5773 then we cannot back up and do a postfix-expression.
5775 Therefore, we scan ahead to the closing `)', and check to see
5776 if the token after the `)' is a `{'. If so, we are not
5777 looking at a cast-expression.
5779 Save tokens so that we can put them back. */
5780 cp_lexer_save_tokens (parser->lexer);
5781 /* Skip tokens until the next token is a closing parenthesis.
5782 If we find the closing `)', and the next token is a `{', then
5783 we are looking at a compound-literal. */
5785 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5786 /*consume_paren=*/true)
5787 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5788 /* Roll back the tokens we skipped. */
5789 cp_lexer_rollback_tokens (parser->lexer);
5790 /* If we were looking at a compound-literal, simulate an error
5791 so that the call to cp_parser_parse_definitely below will
5793 if (compound_literal_p)
5794 cp_parser_simulate_error (parser);
5797 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5798 parser->in_type_id_in_expr_p = true;
5799 /* Look for the type-id. */
5800 type = cp_parser_type_id (parser);
5801 /* Look for the closing `)'. */
5802 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5803 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5806 /* Restore the saved message. */
5807 parser->type_definition_forbidden_message = saved_message;
5809 /* If ok so far, parse the dependent expression. We cannot be
5810 sure it is a cast. Consider `(T ())'. It is a parenthesized
5811 ctor of T, but looks like a cast to function returning T
5812 without a dependent expression. */
5813 if (!cp_parser_error_occurred (parser))
5814 expr = cp_parser_cast_expression (parser,
5815 /*address_p=*/false,
5818 if (cp_parser_parse_definitely (parser))
5820 /* Warn about old-style casts, if so requested. */
5821 if (warn_old_style_cast
5822 && !in_system_header
5823 && !VOID_TYPE_P (type)
5824 && current_lang_name != lang_name_c)
5825 warning (OPT_Wold_style_cast, "use of old-style cast");
5827 /* Only type conversions to integral or enumeration types
5828 can be used in constant-expressions. */
5829 if (!cast_valid_in_integral_constant_expression_p (type)
5830 && (cp_parser_non_integral_constant_expression
5832 "a cast to a type other than an integral or "
5833 "enumeration type")))
5834 return error_mark_node;
5836 /* Perform the cast. */
5837 expr = build_c_cast (type, expr);
5842 /* If we get here, then it's not a cast, so it must be a
5843 unary-expression. */
5844 return cp_parser_unary_expression (parser, address_p, cast_p);
5847 /* Parse a binary expression of the general form:
5851 pm-expression .* cast-expression
5852 pm-expression ->* cast-expression
5854 multiplicative-expression:
5856 multiplicative-expression * pm-expression
5857 multiplicative-expression / pm-expression
5858 multiplicative-expression % pm-expression
5860 additive-expression:
5861 multiplicative-expression
5862 additive-expression + multiplicative-expression
5863 additive-expression - multiplicative-expression
5867 shift-expression << additive-expression
5868 shift-expression >> additive-expression
5870 relational-expression:
5872 relational-expression < shift-expression
5873 relational-expression > shift-expression
5874 relational-expression <= shift-expression
5875 relational-expression >= shift-expression
5879 relational-expression:
5880 relational-expression <? shift-expression
5881 relational-expression >? shift-expression
5883 equality-expression:
5884 relational-expression
5885 equality-expression == relational-expression
5886 equality-expression != relational-expression
5890 and-expression & equality-expression
5892 exclusive-or-expression:
5894 exclusive-or-expression ^ and-expression
5896 inclusive-or-expression:
5897 exclusive-or-expression
5898 inclusive-or-expression | exclusive-or-expression
5900 logical-and-expression:
5901 inclusive-or-expression
5902 logical-and-expression && inclusive-or-expression
5904 logical-or-expression:
5905 logical-and-expression
5906 logical-or-expression || logical-and-expression
5908 All these are implemented with a single function like:
5911 simple-cast-expression
5912 binary-expression <token> binary-expression
5914 CAST_P is true if this expression is the target of a cast.
5916 The binops_by_token map is used to get the tree codes for each <token> type.
5917 binary-expressions are associated according to a precedence table. */
5919 #define TOKEN_PRECEDENCE(token) \
5920 (((token->type == CPP_GREATER \
5921 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5922 && !parser->greater_than_is_operator_p) \
5923 ? PREC_NOT_OPERATOR \
5924 : binops_by_token[token->type].prec)
5927 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5929 cp_parser_expression_stack stack;
5930 cp_parser_expression_stack_entry *sp = &stack[0];
5933 enum tree_code tree_type, lhs_type, rhs_type;
5934 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5937 /* Parse the first expression. */
5938 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5939 lhs_type = ERROR_MARK;
5943 /* Get an operator token. */
5944 token = cp_lexer_peek_token (parser->lexer);
5946 if (warn_cxx0x_compat
5947 && token->type == CPP_RSHIFT
5948 && !parser->greater_than_is_operator_p)
5950 warning (OPT_Wc__0x_compat,
5951 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5953 warning (OPT_Wc__0x_compat,
5954 "suggest parentheses around %<>>%> expression");
5957 new_prec = TOKEN_PRECEDENCE (token);
5959 /* Popping an entry off the stack means we completed a subexpression:
5960 - either we found a token which is not an operator (`>' where it is not
5961 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5962 will happen repeatedly;
5963 - or, we found an operator which has lower priority. This is the case
5964 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5966 if (new_prec <= prec)
5975 tree_type = binops_by_token[token->type].tree_type;
5977 /* We used the operator token. */
5978 cp_lexer_consume_token (parser->lexer);
5980 /* Extract another operand. It may be the RHS of this expression
5981 or the LHS of a new, higher priority expression. */
5982 rhs = cp_parser_simple_cast_expression (parser);
5983 rhs_type = ERROR_MARK;
5985 /* Get another operator token. Look up its precedence to avoid
5986 building a useless (immediately popped) stack entry for common
5987 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5988 token = cp_lexer_peek_token (parser->lexer);
5989 lookahead_prec = TOKEN_PRECEDENCE (token);
5990 if (lookahead_prec > new_prec)
5992 /* ... and prepare to parse the RHS of the new, higher priority
5993 expression. Since precedence levels on the stack are
5994 monotonically increasing, we do not have to care about
5997 sp->tree_type = tree_type;
5999 sp->lhs_type = lhs_type;
6002 lhs_type = rhs_type;
6004 new_prec = lookahead_prec;
6008 /* If the stack is not empty, we have parsed into LHS the right side
6009 (`4' in the example above) of an expression we had suspended.
6010 We can use the information on the stack to recover the LHS (`3')
6011 from the stack together with the tree code (`MULT_EXPR'), and
6012 the precedence of the higher level subexpression
6013 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6014 which will be used to actually build the additive expression. */
6017 tree_type = sp->tree_type;
6019 rhs_type = lhs_type;
6021 lhs_type = sp->lhs_type;
6024 overloaded_p = false;
6025 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6027 lhs_type = tree_type;
6029 /* If the binary operator required the use of an overloaded operator,
6030 then this expression cannot be an integral constant-expression.
6031 An overloaded operator can be used even if both operands are
6032 otherwise permissible in an integral constant-expression if at
6033 least one of the operands is of enumeration type. */
6036 && (cp_parser_non_integral_constant_expression
6037 (parser, "calls to overloaded operators")))
6038 return error_mark_node;
6045 /* Parse the `? expression : assignment-expression' part of a
6046 conditional-expression. The LOGICAL_OR_EXPR is the
6047 logical-or-expression that started the conditional-expression.
6048 Returns a representation of the entire conditional-expression.
6050 This routine is used by cp_parser_assignment_expression.
6052 ? expression : assignment-expression
6056 ? : assignment-expression */
6059 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6062 tree assignment_expr;
6064 /* Consume the `?' token. */
6065 cp_lexer_consume_token (parser->lexer);
6066 if (cp_parser_allow_gnu_extensions_p (parser)
6067 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6068 /* Implicit true clause. */
6071 /* Parse the expression. */
6072 expr = cp_parser_expression (parser, /*cast_p=*/false);
6074 /* The next token should be a `:'. */
6075 cp_parser_require (parser, CPP_COLON, "`:'");
6076 /* Parse the assignment-expression. */
6077 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6079 /* Build the conditional-expression. */
6080 return build_x_conditional_expr (logical_or_expr,
6085 /* Parse an assignment-expression.
6087 assignment-expression:
6088 conditional-expression
6089 logical-or-expression assignment-operator assignment_expression
6092 CAST_P is true if this expression is the target of a cast.
6094 Returns a representation for the expression. */
6097 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6101 /* If the next token is the `throw' keyword, then we're looking at
6102 a throw-expression. */
6103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6104 expr = cp_parser_throw_expression (parser);
6105 /* Otherwise, it must be that we are looking at a
6106 logical-or-expression. */
6109 /* Parse the binary expressions (logical-or-expression). */
6110 expr = cp_parser_binary_expression (parser, cast_p);
6111 /* If the next token is a `?' then we're actually looking at a
6112 conditional-expression. */
6113 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6114 return cp_parser_question_colon_clause (parser, expr);
6117 enum tree_code assignment_operator;
6119 /* If it's an assignment-operator, we're using the second
6122 = cp_parser_assignment_operator_opt (parser);
6123 if (assignment_operator != ERROR_MARK)
6127 /* Parse the right-hand side of the assignment. */
6128 rhs = cp_parser_assignment_expression (parser, cast_p);
6129 /* An assignment may not appear in a
6130 constant-expression. */
6131 if (cp_parser_non_integral_constant_expression (parser,
6133 return error_mark_node;
6134 /* Build the assignment expression. */
6135 expr = build_x_modify_expr (expr,
6136 assignment_operator,
6145 /* Parse an (optional) assignment-operator.
6147 assignment-operator: one of
6148 = *= /= %= += -= >>= <<= &= ^= |=
6152 assignment-operator: one of
6155 If the next token is an assignment operator, the corresponding tree
6156 code is returned, and the token is consumed. For example, for
6157 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6158 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6159 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6160 operator, ERROR_MARK is returned. */
6162 static enum tree_code
6163 cp_parser_assignment_operator_opt (cp_parser* parser)
6168 /* Peek at the next toen. */
6169 token = cp_lexer_peek_token (parser->lexer);
6171 switch (token->type)
6182 op = TRUNC_DIV_EXPR;
6186 op = TRUNC_MOD_EXPR;
6218 /* Nothing else is an assignment operator. */
6222 /* If it was an assignment operator, consume it. */
6223 if (op != ERROR_MARK)
6224 cp_lexer_consume_token (parser->lexer);
6229 /* Parse an expression.
6232 assignment-expression
6233 expression , assignment-expression
6235 CAST_P is true if this expression is the target of a cast.
6237 Returns a representation of the expression. */
6240 cp_parser_expression (cp_parser* parser, bool cast_p)
6242 tree expression = NULL_TREE;
6246 tree assignment_expression;
6248 /* Parse the next assignment-expression. */
6249 assignment_expression
6250 = cp_parser_assignment_expression (parser, cast_p);
6251 /* If this is the first assignment-expression, we can just
6254 expression = assignment_expression;
6256 expression = build_x_compound_expr (expression,
6257 assignment_expression);
6258 /* If the next token is not a comma, then we are done with the
6260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6262 /* Consume the `,'. */
6263 cp_lexer_consume_token (parser->lexer);
6264 /* A comma operator cannot appear in a constant-expression. */
6265 if (cp_parser_non_integral_constant_expression (parser,
6266 "a comma operator"))
6267 expression = error_mark_node;
6273 /* Parse a constant-expression.
6275 constant-expression:
6276 conditional-expression
6278 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6279 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6280 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6281 is false, NON_CONSTANT_P should be NULL. */
6284 cp_parser_constant_expression (cp_parser* parser,
6285 bool allow_non_constant_p,
6286 bool *non_constant_p)
6288 bool saved_integral_constant_expression_p;
6289 bool saved_allow_non_integral_constant_expression_p;
6290 bool saved_non_integral_constant_expression_p;
6293 /* It might seem that we could simply parse the
6294 conditional-expression, and then check to see if it were
6295 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6296 one that the compiler can figure out is constant, possibly after
6297 doing some simplifications or optimizations. The standard has a
6298 precise definition of constant-expression, and we must honor
6299 that, even though it is somewhat more restrictive.
6305 is not a legal declaration, because `(2, 3)' is not a
6306 constant-expression. The `,' operator is forbidden in a
6307 constant-expression. However, GCC's constant-folding machinery
6308 will fold this operation to an INTEGER_CST for `3'. */
6310 /* Save the old settings. */
6311 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6312 saved_allow_non_integral_constant_expression_p
6313 = parser->allow_non_integral_constant_expression_p;
6314 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6315 /* We are now parsing a constant-expression. */
6316 parser->integral_constant_expression_p = true;
6317 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6318 parser->non_integral_constant_expression_p = false;
6319 /* Although the grammar says "conditional-expression", we parse an
6320 "assignment-expression", which also permits "throw-expression"
6321 and the use of assignment operators. In the case that
6322 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6323 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6324 actually essential that we look for an assignment-expression.
6325 For example, cp_parser_initializer_clauses uses this function to
6326 determine whether a particular assignment-expression is in fact
6328 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6329 /* Restore the old settings. */
6330 parser->integral_constant_expression_p
6331 = saved_integral_constant_expression_p;
6332 parser->allow_non_integral_constant_expression_p
6333 = saved_allow_non_integral_constant_expression_p;
6334 if (allow_non_constant_p)
6335 *non_constant_p = parser->non_integral_constant_expression_p;
6336 else if (parser->non_integral_constant_expression_p)
6337 expression = error_mark_node;
6338 parser->non_integral_constant_expression_p
6339 = saved_non_integral_constant_expression_p;
6344 /* Parse __builtin_offsetof.
6346 offsetof-expression:
6347 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6349 offsetof-member-designator:
6351 | offsetof-member-designator "." id-expression
6352 | offsetof-member-designator "[" expression "]" */
6355 cp_parser_builtin_offsetof (cp_parser *parser)
6357 int save_ice_p, save_non_ice_p;
6361 /* We're about to accept non-integral-constant things, but will
6362 definitely yield an integral constant expression. Save and
6363 restore these values around our local parsing. */
6364 save_ice_p = parser->integral_constant_expression_p;
6365 save_non_ice_p = parser->non_integral_constant_expression_p;
6367 /* Consume the "__builtin_offsetof" token. */
6368 cp_lexer_consume_token (parser->lexer);
6369 /* Consume the opening `('. */
6370 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6371 /* Parse the type-id. */
6372 type = cp_parser_type_id (parser);
6373 /* Look for the `,'. */
6374 cp_parser_require (parser, CPP_COMMA, "`,'");
6376 /* Build the (type *)null that begins the traditional offsetof macro. */
6377 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6379 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6380 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6384 cp_token *token = cp_lexer_peek_token (parser->lexer);
6385 switch (token->type)
6387 case CPP_OPEN_SQUARE:
6388 /* offsetof-member-designator "[" expression "]" */
6389 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6393 /* offsetof-member-designator "." identifier */
6394 cp_lexer_consume_token (parser->lexer);
6395 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6399 case CPP_CLOSE_PAREN:
6400 /* Consume the ")" token. */
6401 cp_lexer_consume_token (parser->lexer);
6405 /* Error. We know the following require will fail, but
6406 that gives the proper error message. */
6407 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6408 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6409 expr = error_mark_node;
6415 /* If we're processing a template, we can't finish the semantics yet.
6416 Otherwise we can fold the entire expression now. */
6417 if (processing_template_decl)
6418 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6420 expr = finish_offsetof (expr);
6423 parser->integral_constant_expression_p = save_ice_p;
6424 parser->non_integral_constant_expression_p = save_non_ice_p;
6429 /* Parse a trait expression. */
6432 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6435 tree type1, type2 = NULL_TREE;
6436 bool binary = false;
6437 cp_decl_specifier_seq decl_specs;
6441 case RID_HAS_NOTHROW_ASSIGN:
6442 kind = CPTK_HAS_NOTHROW_ASSIGN;
6444 case RID_HAS_NOTHROW_CONSTRUCTOR:
6445 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6447 case RID_HAS_NOTHROW_COPY:
6448 kind = CPTK_HAS_NOTHROW_COPY;
6450 case RID_HAS_TRIVIAL_ASSIGN:
6451 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6453 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6454 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6456 case RID_HAS_TRIVIAL_COPY:
6457 kind = CPTK_HAS_TRIVIAL_COPY;
6459 case RID_HAS_TRIVIAL_DESTRUCTOR:
6460 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6462 case RID_HAS_VIRTUAL_DESTRUCTOR:
6463 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6465 case RID_IS_ABSTRACT:
6466 kind = CPTK_IS_ABSTRACT;
6468 case RID_IS_BASE_OF:
6469 kind = CPTK_IS_BASE_OF;
6473 kind = CPTK_IS_CLASS;
6475 case RID_IS_CONVERTIBLE_TO:
6476 kind = CPTK_IS_CONVERTIBLE_TO;
6480 kind = CPTK_IS_EMPTY;
6483 kind = CPTK_IS_ENUM;
6488 case RID_IS_POLYMORPHIC:
6489 kind = CPTK_IS_POLYMORPHIC;
6492 kind = CPTK_IS_UNION;
6498 /* Consume the token. */
6499 cp_lexer_consume_token (parser->lexer);
6501 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6503 type1 = cp_parser_type_id (parser);
6505 /* Build a trivial decl-specifier-seq. */
6506 clear_decl_specs (&decl_specs);
6507 decl_specs.type = type1;
6509 /* Call grokdeclarator to figure out what type this is. */
6510 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6511 /*initialized=*/0, /*attrlist=*/NULL);
6515 cp_parser_require (parser, CPP_COMMA, "`,'");
6517 type2 = cp_parser_type_id (parser);
6519 /* Build a trivial decl-specifier-seq. */
6520 clear_decl_specs (&decl_specs);
6521 decl_specs.type = type2;
6523 /* Call grokdeclarator to figure out what type this is. */
6524 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6525 /*initialized=*/0, /*attrlist=*/NULL);
6528 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6530 /* Complete the trait expr, which may mean either processing the
6531 static assert now or saving it for template instantiation. */
6532 return finish_trait_expr (kind, type1, type2);
6535 /* Statements [gram.stmt.stmt] */
6537 /* Parse a statement.
6541 expression-statement
6546 declaration-statement
6549 IN_COMPOUND is true when the statement is nested inside a
6550 cp_parser_compound_statement; this matters for certain pragmas.
6552 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6553 is a (possibly labeled) if statement which is not enclosed in braces
6554 and has an else clause. This is used to implement -Wparentheses. */
6557 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6558 bool in_compound, bool *if_p)
6562 location_t statement_location;
6567 /* There is no statement yet. */
6568 statement = NULL_TREE;
6569 /* Peek at the next token. */
6570 token = cp_lexer_peek_token (parser->lexer);
6571 /* Remember the location of the first token in the statement. */
6572 statement_location = token->location;
6573 /* If this is a keyword, then that will often determine what kind of
6574 statement we have. */
6575 if (token->type == CPP_KEYWORD)
6577 enum rid keyword = token->keyword;
6583 /* Looks like a labeled-statement with a case label.
6584 Parse the label, and then use tail recursion to parse
6586 cp_parser_label_for_labeled_statement (parser);
6591 statement = cp_parser_selection_statement (parser, if_p);
6597 statement = cp_parser_iteration_statement (parser);
6604 statement = cp_parser_jump_statement (parser);
6607 /* Objective-C++ exception-handling constructs. */
6610 case RID_AT_FINALLY:
6611 case RID_AT_SYNCHRONIZED:
6613 statement = cp_parser_objc_statement (parser);
6617 statement = cp_parser_try_block (parser);
6621 /* This must be a namespace alias definition. */
6622 cp_parser_declaration_statement (parser);
6626 /* It might be a keyword like `int' that can start a
6627 declaration-statement. */
6631 else if (token->type == CPP_NAME)
6633 /* If the next token is a `:', then we are looking at a
6634 labeled-statement. */
6635 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6636 if (token->type == CPP_COLON)
6638 /* Looks like a labeled-statement with an ordinary label.
6639 Parse the label, and then use tail recursion to parse
6641 cp_parser_label_for_labeled_statement (parser);
6645 /* Anything that starts with a `{' must be a compound-statement. */
6646 else if (token->type == CPP_OPEN_BRACE)
6647 statement = cp_parser_compound_statement (parser, NULL, false);
6648 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6649 a statement all its own. */
6650 else if (token->type == CPP_PRAGMA)
6652 /* Only certain OpenMP pragmas are attached to statements, and thus
6653 are considered statements themselves. All others are not. In
6654 the context of a compound, accept the pragma as a "statement" and
6655 return so that we can check for a close brace. Otherwise we
6656 require a real statement and must go back and read one. */
6658 cp_parser_pragma (parser, pragma_compound);
6659 else if (!cp_parser_pragma (parser, pragma_stmt))
6663 else if (token->type == CPP_EOF)
6665 cp_parser_error (parser, "expected statement");
6669 /* Everything else must be a declaration-statement or an
6670 expression-statement. Try for the declaration-statement
6671 first, unless we are looking at a `;', in which case we know that
6672 we have an expression-statement. */
6675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6677 cp_parser_parse_tentatively (parser);
6678 /* Try to parse the declaration-statement. */
6679 cp_parser_declaration_statement (parser);
6680 /* If that worked, we're done. */
6681 if (cp_parser_parse_definitely (parser))
6684 /* Look for an expression-statement instead. */
6685 statement = cp_parser_expression_statement (parser, in_statement_expr);
6688 /* Set the line number for the statement. */
6689 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6690 SET_EXPR_LOCATION (statement, statement_location);
6693 /* Parse the label for a labeled-statement, i.e.
6696 case constant-expression :
6700 case constant-expression ... constant-expression : statement
6702 When a label is parsed without errors, the label is added to the
6703 parse tree by the finish_* functions, so this function doesn't
6704 have to return the label. */
6707 cp_parser_label_for_labeled_statement (cp_parser* parser)
6711 /* The next token should be an identifier. */
6712 token = cp_lexer_peek_token (parser->lexer);
6713 if (token->type != CPP_NAME
6714 && token->type != CPP_KEYWORD)
6716 cp_parser_error (parser, "expected labeled-statement");
6720 switch (token->keyword)
6727 /* Consume the `case' token. */
6728 cp_lexer_consume_token (parser->lexer);
6729 /* Parse the constant-expression. */
6730 expr = cp_parser_constant_expression (parser,
6731 /*allow_non_constant_p=*/false,
6734 ellipsis = cp_lexer_peek_token (parser->lexer);
6735 if (ellipsis->type == CPP_ELLIPSIS)
6737 /* Consume the `...' token. */
6738 cp_lexer_consume_token (parser->lexer);
6740 cp_parser_constant_expression (parser,
6741 /*allow_non_constant_p=*/false,
6743 /* We don't need to emit warnings here, as the common code
6744 will do this for us. */
6747 expr_hi = NULL_TREE;
6749 if (parser->in_switch_statement_p)
6750 finish_case_label (expr, expr_hi);
6752 error ("case label %qE not within a switch statement", expr);
6757 /* Consume the `default' token. */
6758 cp_lexer_consume_token (parser->lexer);
6760 if (parser->in_switch_statement_p)
6761 finish_case_label (NULL_TREE, NULL_TREE);
6763 error ("case label not within a switch statement");
6767 /* Anything else must be an ordinary label. */
6768 finish_label_stmt (cp_parser_identifier (parser));
6772 /* Require the `:' token. */
6773 cp_parser_require (parser, CPP_COLON, "`:'");
6776 /* Parse an expression-statement.
6778 expression-statement:
6781 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6782 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6783 indicates whether this expression-statement is part of an
6784 expression statement. */
6787 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6789 tree statement = NULL_TREE;
6791 /* If the next token is a ';', then there is no expression
6793 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6794 statement = cp_parser_expression (parser, /*cast_p=*/false);
6796 /* Consume the final `;'. */
6797 cp_parser_consume_semicolon_at_end_of_statement (parser);
6799 if (in_statement_expr
6800 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6801 /* This is the final expression statement of a statement
6803 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6805 statement = finish_expr_stmt (statement);
6812 /* Parse a compound-statement.
6815 { statement-seq [opt] }
6817 Returns a tree representing the statement. */
6820 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6825 /* Consume the `{'. */
6826 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6827 return error_mark_node;
6828 /* Begin the compound-statement. */
6829 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6830 /* Parse an (optional) statement-seq. */
6831 cp_parser_statement_seq_opt (parser, in_statement_expr);
6832 /* Finish the compound-statement. */
6833 finish_compound_stmt (compound_stmt);
6834 /* Consume the `}'. */
6835 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6837 return compound_stmt;
6840 /* Parse an (optional) statement-seq.
6844 statement-seq [opt] statement */
6847 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6849 /* Scan statements until there aren't any more. */
6852 cp_token *token = cp_lexer_peek_token (parser->lexer);
6854 /* If we're looking at a `}', then we've run out of statements. */
6855 if (token->type == CPP_CLOSE_BRACE
6856 || token->type == CPP_EOF
6857 || token->type == CPP_PRAGMA_EOL)
6860 /* If we are in a compound statement and find 'else' then
6861 something went wrong. */
6862 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6864 if (parser->in_statement & IN_IF_STMT)
6868 token = cp_lexer_consume_token (parser->lexer);
6869 error ("%<else%> without a previous %<if%>");
6873 /* Parse the statement. */
6874 cp_parser_statement (parser, in_statement_expr, true, NULL);
6878 /* Parse a selection-statement.
6880 selection-statement:
6881 if ( condition ) statement
6882 if ( condition ) statement else statement
6883 switch ( condition ) statement
6885 Returns the new IF_STMT or SWITCH_STMT.
6887 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6888 is a (possibly labeled) if statement which is not enclosed in
6889 braces and has an else clause. This is used to implement
6893 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6901 /* Peek at the next token. */
6902 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6904 /* See what kind of keyword it is. */
6905 keyword = token->keyword;
6914 /* Look for the `('. */
6915 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6917 cp_parser_skip_to_end_of_statement (parser);
6918 return error_mark_node;
6921 /* Begin the selection-statement. */
6922 if (keyword == RID_IF)
6923 statement = begin_if_stmt ();
6925 statement = begin_switch_stmt ();
6927 /* Parse the condition. */
6928 condition = cp_parser_condition (parser);
6929 /* Look for the `)'. */
6930 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6931 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6932 /*consume_paren=*/true);
6934 if (keyword == RID_IF)
6937 unsigned char in_statement;
6939 /* Add the condition. */
6940 finish_if_stmt_cond (condition, statement);
6942 /* Parse the then-clause. */
6943 in_statement = parser->in_statement;
6944 parser->in_statement |= IN_IF_STMT;
6945 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6946 parser->in_statement = in_statement;
6948 finish_then_clause (statement);
6950 /* If the next token is `else', parse the else-clause. */
6951 if (cp_lexer_next_token_is_keyword (parser->lexer,
6954 /* Consume the `else' keyword. */
6955 cp_lexer_consume_token (parser->lexer);
6956 begin_else_clause (statement);
6957 /* Parse the else-clause. */
6958 cp_parser_implicitly_scoped_statement (parser, NULL);
6959 finish_else_clause (statement);
6961 /* If we are currently parsing a then-clause, then
6962 IF_P will not be NULL. We set it to true to
6963 indicate that this if statement has an else clause.
6964 This may trigger the Wparentheses warning below
6965 when we get back up to the parent if statement. */
6971 /* This if statement does not have an else clause. If
6972 NESTED_IF is true, then the then-clause is an if
6973 statement which does have an else clause. We warn
6974 about the potential ambiguity. */
6976 warning (OPT_Wparentheses,
6977 ("%Hsuggest explicit braces "
6978 "to avoid ambiguous %<else%>"),
6979 EXPR_LOCUS (statement));
6982 /* Now we're all done with the if-statement. */
6983 finish_if_stmt (statement);
6987 bool in_switch_statement_p;
6988 unsigned char in_statement;
6990 /* Add the condition. */
6991 finish_switch_cond (condition, statement);
6993 /* Parse the body of the switch-statement. */
6994 in_switch_statement_p = parser->in_switch_statement_p;
6995 in_statement = parser->in_statement;
6996 parser->in_switch_statement_p = true;
6997 parser->in_statement |= IN_SWITCH_STMT;
6998 cp_parser_implicitly_scoped_statement (parser, NULL);
6999 parser->in_switch_statement_p = in_switch_statement_p;
7000 parser->in_statement = in_statement;
7002 /* Now we're all done with the switch-statement. */
7003 finish_switch_stmt (statement);
7011 cp_parser_error (parser, "expected selection-statement");
7012 return error_mark_node;
7016 /* Parse a condition.
7020 type-specifier-seq declarator = assignment-expression
7025 type-specifier-seq declarator asm-specification [opt]
7026 attributes [opt] = assignment-expression
7028 Returns the expression that should be tested. */
7031 cp_parser_condition (cp_parser* parser)
7033 cp_decl_specifier_seq type_specifiers;
7034 const char *saved_message;
7036 /* Try the declaration first. */
7037 cp_parser_parse_tentatively (parser);
7038 /* New types are not allowed in the type-specifier-seq for a
7040 saved_message = parser->type_definition_forbidden_message;
7041 parser->type_definition_forbidden_message
7042 = "types may not be defined in conditions";
7043 /* Parse the type-specifier-seq. */
7044 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7046 /* Restore the saved message. */
7047 parser->type_definition_forbidden_message = saved_message;
7048 /* If all is well, we might be looking at a declaration. */
7049 if (!cp_parser_error_occurred (parser))
7052 tree asm_specification;
7054 cp_declarator *declarator;
7055 tree initializer = NULL_TREE;
7057 /* Parse the declarator. */
7058 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7059 /*ctor_dtor_or_conv_p=*/NULL,
7060 /*parenthesized_p=*/NULL,
7061 /*member_p=*/false);
7062 /* Parse the attributes. */
7063 attributes = cp_parser_attributes_opt (parser);
7064 /* Parse the asm-specification. */
7065 asm_specification = cp_parser_asm_specification_opt (parser);
7066 /* If the next token is not an `=', then we might still be
7067 looking at an expression. For example:
7071 looks like a decl-specifier-seq and a declarator -- but then
7072 there is no `=', so this is an expression. */
7073 cp_parser_require (parser, CPP_EQ, "`='");
7074 /* If we did see an `=', then we are looking at a declaration
7076 if (cp_parser_parse_definitely (parser))
7079 bool non_constant_p;
7081 /* Create the declaration. */
7082 decl = start_decl (declarator, &type_specifiers,
7083 /*initialized_p=*/true,
7084 attributes, /*prefix_attributes=*/NULL_TREE,
7086 /* Parse the assignment-expression. */
7088 = cp_parser_constant_expression (parser,
7089 /*allow_non_constant_p=*/true,
7091 if (!non_constant_p)
7092 initializer = fold_non_dependent_expr (initializer);
7094 /* Process the initializer. */
7095 cp_finish_decl (decl,
7096 initializer, !non_constant_p,
7098 LOOKUP_ONLYCONVERTING);
7101 pop_scope (pushed_scope);
7103 return convert_from_reference (decl);
7106 /* If we didn't even get past the declarator successfully, we are
7107 definitely not looking at a declaration. */
7109 cp_parser_abort_tentative_parse (parser);
7111 /* Otherwise, we are looking at an expression. */
7112 return cp_parser_expression (parser, /*cast_p=*/false);
7115 /* We check for a ) immediately followed by ; with no whitespacing
7116 between. This is used to issue a warning for:
7124 as the semicolon is probably extraneous.
7126 On parse errors, the next token might not be a ), so do nothing in
7130 check_empty_body (cp_parser* parser, const char* type)
7133 cp_token *close_paren;
7134 expanded_location close_loc;
7135 expanded_location semi_loc;
7137 close_paren = cp_lexer_peek_token (parser->lexer);
7138 if (close_paren->type != CPP_CLOSE_PAREN)
7141 close_loc = expand_location (close_paren->location);
7142 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7144 if (token->type != CPP_SEMICOLON
7145 || (token->flags & PREV_WHITE))
7148 semi_loc = expand_location (token->location);
7149 if (close_loc.line == semi_loc.line
7150 #ifdef USE_MAPPED_LOCATION
7151 && close_loc.column+1 == semi_loc.column
7154 warning (OPT_Wempty_body,
7155 "suggest a space before %<;%> or explicit braces around empty "
7156 "body in %<%s%> statement",
7160 /* Parse an iteration-statement.
7162 iteration-statement:
7163 while ( condition ) statement
7164 do statement while ( expression ) ;
7165 for ( for-init-statement condition [opt] ; expression [opt] )
7168 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7171 cp_parser_iteration_statement (cp_parser* parser)
7176 unsigned char in_statement;
7178 /* Peek at the next token. */
7179 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7181 return error_mark_node;
7183 /* Remember whether or not we are already within an iteration
7185 in_statement = parser->in_statement;
7187 /* See what kind of keyword it is. */
7188 keyword = token->keyword;
7195 /* Begin the while-statement. */
7196 statement = begin_while_stmt ();
7197 /* Look for the `('. */
7198 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7199 /* Parse the condition. */
7200 condition = cp_parser_condition (parser);
7201 finish_while_stmt_cond (condition, statement);
7202 check_empty_body (parser, "while");
7203 /* Look for the `)'. */
7204 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7205 /* Parse the dependent statement. */
7206 parser->in_statement = IN_ITERATION_STMT;
7207 cp_parser_already_scoped_statement (parser);
7208 parser->in_statement = in_statement;
7209 /* We're done with the while-statement. */
7210 finish_while_stmt (statement);
7218 /* Begin the do-statement. */
7219 statement = begin_do_stmt ();
7220 /* Parse the body of the do-statement. */
7221 parser->in_statement = IN_ITERATION_STMT;
7222 cp_parser_implicitly_scoped_statement (parser, NULL);
7223 parser->in_statement = in_statement;
7224 finish_do_body (statement);
7225 /* Look for the `while' keyword. */
7226 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7227 /* Look for the `('. */
7228 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7229 /* Parse the expression. */
7230 expression = cp_parser_expression (parser, /*cast_p=*/false);
7231 /* We're done with the do-statement. */
7232 finish_do_stmt (expression, statement);
7233 /* Look for the `)'. */
7234 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7235 /* Look for the `;'. */
7236 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7242 tree condition = NULL_TREE;
7243 tree expression = NULL_TREE;
7245 /* Begin the for-statement. */
7246 statement = begin_for_stmt ();
7247 /* Look for the `('. */
7248 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7249 /* Parse the initialization. */
7250 cp_parser_for_init_statement (parser);
7251 finish_for_init_stmt (statement);
7253 /* If there's a condition, process it. */
7254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7255 condition = cp_parser_condition (parser);
7256 finish_for_cond (condition, statement);
7257 /* Look for the `;'. */
7258 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7260 /* If there's an expression, process it. */
7261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7262 expression = cp_parser_expression (parser, /*cast_p=*/false);
7263 finish_for_expr (expression, statement);
7264 check_empty_body (parser, "for");
7265 /* Look for the `)'. */
7266 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7268 /* Parse the body of the for-statement. */
7269 parser->in_statement = IN_ITERATION_STMT;
7270 cp_parser_already_scoped_statement (parser);
7271 parser->in_statement = in_statement;
7273 /* We're done with the for-statement. */
7274 finish_for_stmt (statement);
7279 cp_parser_error (parser, "expected iteration-statement");
7280 statement = error_mark_node;
7287 /* Parse a for-init-statement.
7290 expression-statement
7291 simple-declaration */
7294 cp_parser_for_init_statement (cp_parser* parser)
7296 /* If the next token is a `;', then we have an empty
7297 expression-statement. Grammatically, this is also a
7298 simple-declaration, but an invalid one, because it does not
7299 declare anything. Therefore, if we did not handle this case
7300 specially, we would issue an error message about an invalid
7302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7304 /* We're going to speculatively look for a declaration, falling back
7305 to an expression, if necessary. */
7306 cp_parser_parse_tentatively (parser);
7307 /* Parse the declaration. */
7308 cp_parser_simple_declaration (parser,
7309 /*function_definition_allowed_p=*/false);
7310 /* If the tentative parse failed, then we shall need to look for an
7311 expression-statement. */
7312 if (cp_parser_parse_definitely (parser))
7316 cp_parser_expression_statement (parser, false);
7319 /* Parse a jump-statement.
7324 return expression [opt] ;
7332 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7335 cp_parser_jump_statement (cp_parser* parser)
7337 tree statement = error_mark_node;
7340 unsigned char in_statement;
7342 /* Peek at the next token. */
7343 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7345 return error_mark_node;
7347 /* See what kind of keyword it is. */
7348 keyword = token->keyword;
7352 in_statement = parser->in_statement & ~IN_IF_STMT;
7353 switch (in_statement)
7356 error ("break statement not within loop or switch");
7359 gcc_assert ((in_statement & IN_SWITCH_STMT)
7360 || in_statement == IN_ITERATION_STMT);
7361 statement = finish_break_stmt ();
7364 error ("invalid exit from OpenMP structured block");
7367 error ("break statement used with OpenMP for loop");
7370 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7374 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7377 error ("continue statement not within a loop");
7379 case IN_ITERATION_STMT:
7381 statement = finish_continue_stmt ();
7384 error ("invalid exit from OpenMP structured block");
7389 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7396 /* If the next token is a `;', then there is no
7398 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7399 expr = cp_parser_expression (parser, /*cast_p=*/false);
7402 /* Build the return-statement. */
7403 statement = finish_return_stmt (expr);
7404 /* Look for the final `;'. */
7405 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7410 /* Create the goto-statement. */
7411 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7413 /* Issue a warning about this use of a GNU extension. */
7415 pedwarn ("ISO C++ forbids computed gotos");
7416 /* Consume the '*' token. */
7417 cp_lexer_consume_token (parser->lexer);
7418 /* Parse the dependent expression. */
7419 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7422 finish_goto_stmt (cp_parser_identifier (parser));
7423 /* Look for the final `;'. */
7424 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7428 cp_parser_error (parser, "expected jump-statement");
7435 /* Parse a declaration-statement.
7437 declaration-statement:
7438 block-declaration */
7441 cp_parser_declaration_statement (cp_parser* parser)
7445 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7446 p = obstack_alloc (&declarator_obstack, 0);
7448 /* Parse the block-declaration. */
7449 cp_parser_block_declaration (parser, /*statement_p=*/true);
7451 /* Free any declarators allocated. */
7452 obstack_free (&declarator_obstack, p);
7454 /* Finish off the statement. */
7458 /* Some dependent statements (like `if (cond) statement'), are
7459 implicitly in their own scope. In other words, if the statement is
7460 a single statement (as opposed to a compound-statement), it is
7461 none-the-less treated as if it were enclosed in braces. Any
7462 declarations appearing in the dependent statement are out of scope
7463 after control passes that point. This function parses a statement,
7464 but ensures that is in its own scope, even if it is not a
7467 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7468 is a (possibly labeled) if statement which is not enclosed in
7469 braces and has an else clause. This is used to implement
7472 Returns the new statement. */
7475 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7482 /* Mark if () ; with a special NOP_EXPR. */
7483 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7485 cp_lexer_consume_token (parser->lexer);
7486 statement = add_stmt (build_empty_stmt ());
7488 /* if a compound is opened, we simply parse the statement directly. */
7489 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7490 statement = cp_parser_compound_statement (parser, NULL, false);
7491 /* If the token is not a `{', then we must take special action. */
7494 /* Create a compound-statement. */
7495 statement = begin_compound_stmt (0);
7496 /* Parse the dependent-statement. */
7497 cp_parser_statement (parser, NULL_TREE, false, if_p);
7498 /* Finish the dummy compound-statement. */
7499 finish_compound_stmt (statement);
7502 /* Return the statement. */
7506 /* For some dependent statements (like `while (cond) statement'), we
7507 have already created a scope. Therefore, even if the dependent
7508 statement is a compound-statement, we do not want to create another
7512 cp_parser_already_scoped_statement (cp_parser* parser)
7514 /* If the token is a `{', then we must take special action. */
7515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7516 cp_parser_statement (parser, NULL_TREE, false, NULL);
7519 /* Avoid calling cp_parser_compound_statement, so that we
7520 don't create a new scope. Do everything else by hand. */
7521 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7522 cp_parser_statement_seq_opt (parser, NULL_TREE);
7523 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7527 /* Declarations [gram.dcl.dcl] */
7529 /* Parse an optional declaration-sequence.
7533 declaration-seq declaration */
7536 cp_parser_declaration_seq_opt (cp_parser* parser)
7542 token = cp_lexer_peek_token (parser->lexer);
7544 if (token->type == CPP_CLOSE_BRACE
7545 || token->type == CPP_EOF
7546 || token->type == CPP_PRAGMA_EOL)
7549 if (token->type == CPP_SEMICOLON)
7551 /* A declaration consisting of a single semicolon is
7552 invalid. Allow it unless we're being pedantic. */
7553 cp_lexer_consume_token (parser->lexer);
7554 if (pedantic && !in_system_header)
7555 pedwarn ("extra %<;%>");
7559 /* If we're entering or exiting a region that's implicitly
7560 extern "C", modify the lang context appropriately. */
7561 if (!parser->implicit_extern_c && token->implicit_extern_c)
7563 push_lang_context (lang_name_c);
7564 parser->implicit_extern_c = true;
7566 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7568 pop_lang_context ();
7569 parser->implicit_extern_c = false;
7572 if (token->type == CPP_PRAGMA)
7574 /* A top-level declaration can consist solely of a #pragma.
7575 A nested declaration cannot, so this is done here and not
7576 in cp_parser_declaration. (A #pragma at block scope is
7577 handled in cp_parser_statement.) */
7578 cp_parser_pragma (parser, pragma_external);
7582 /* Parse the declaration itself. */
7583 cp_parser_declaration (parser);
7587 /* Parse a declaration.
7592 template-declaration
7593 explicit-instantiation
7594 explicit-specialization
7595 linkage-specification
7596 namespace-definition
7601 __extension__ declaration */
7604 cp_parser_declaration (cp_parser* parser)
7611 /* Check for the `__extension__' keyword. */
7612 if (cp_parser_extension_opt (parser, &saved_pedantic))
7614 /* Parse the qualified declaration. */
7615 cp_parser_declaration (parser);
7616 /* Restore the PEDANTIC flag. */
7617 pedantic = saved_pedantic;
7622 /* Try to figure out what kind of declaration is present. */
7623 token1 = *cp_lexer_peek_token (parser->lexer);
7625 if (token1.type != CPP_EOF)
7626 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7629 token2.type = CPP_EOF;
7630 token2.keyword = RID_MAX;
7633 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7634 p = obstack_alloc (&declarator_obstack, 0);
7636 /* If the next token is `extern' and the following token is a string
7637 literal, then we have a linkage specification. */
7638 if (token1.keyword == RID_EXTERN
7639 && cp_parser_is_string_literal (&token2))
7640 cp_parser_linkage_specification (parser);
7641 /* If the next token is `template', then we have either a template
7642 declaration, an explicit instantiation, or an explicit
7644 else if (token1.keyword == RID_TEMPLATE)
7646 /* `template <>' indicates a template specialization. */
7647 if (token2.type == CPP_LESS
7648 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7649 cp_parser_explicit_specialization (parser);
7650 /* `template <' indicates a template declaration. */
7651 else if (token2.type == CPP_LESS)
7652 cp_parser_template_declaration (parser, /*member_p=*/false);
7653 /* Anything else must be an explicit instantiation. */
7655 cp_parser_explicit_instantiation (parser);
7657 /* If the next token is `export', then we have a template
7659 else if (token1.keyword == RID_EXPORT)
7660 cp_parser_template_declaration (parser, /*member_p=*/false);
7661 /* If the next token is `extern', 'static' or 'inline' and the one
7662 after that is `template', we have a GNU extended explicit
7663 instantiation directive. */
7664 else if (cp_parser_allow_gnu_extensions_p (parser)
7665 && (token1.keyword == RID_EXTERN
7666 || token1.keyword == RID_STATIC
7667 || token1.keyword == RID_INLINE)
7668 && token2.keyword == RID_TEMPLATE)
7669 cp_parser_explicit_instantiation (parser);
7670 /* If the next token is `namespace', check for a named or unnamed
7671 namespace definition. */
7672 else if (token1.keyword == RID_NAMESPACE
7673 && (/* A named namespace definition. */
7674 (token2.type == CPP_NAME
7675 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7677 /* An unnamed namespace definition. */
7678 || token2.type == CPP_OPEN_BRACE
7679 || token2.keyword == RID_ATTRIBUTE))
7680 cp_parser_namespace_definition (parser);
7681 /* Objective-C++ declaration/definition. */
7682 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7683 cp_parser_objc_declaration (parser);
7684 /* We must have either a block declaration or a function
7687 /* Try to parse a block-declaration, or a function-definition. */
7688 cp_parser_block_declaration (parser, /*statement_p=*/false);
7690 /* Free any declarators allocated. */
7691 obstack_free (&declarator_obstack, p);
7694 /* Parse a block-declaration.
7699 namespace-alias-definition
7706 __extension__ block-declaration
7712 static_assert-declaration
7714 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7715 part of a declaration-statement. */
7718 cp_parser_block_declaration (cp_parser *parser,
7724 /* Check for the `__extension__' keyword. */
7725 if (cp_parser_extension_opt (parser, &saved_pedantic))
7727 /* Parse the qualified declaration. */
7728 cp_parser_block_declaration (parser, statement_p);
7729 /* Restore the PEDANTIC flag. */
7730 pedantic = saved_pedantic;
7735 /* Peek at the next token to figure out which kind of declaration is
7737 token1 = cp_lexer_peek_token (parser->lexer);
7739 /* If the next keyword is `asm', we have an asm-definition. */
7740 if (token1->keyword == RID_ASM)
7743 cp_parser_commit_to_tentative_parse (parser);
7744 cp_parser_asm_definition (parser);
7746 /* If the next keyword is `namespace', we have a
7747 namespace-alias-definition. */
7748 else if (token1->keyword == RID_NAMESPACE)
7749 cp_parser_namespace_alias_definition (parser);
7750 /* If the next keyword is `using', we have either a
7751 using-declaration or a using-directive. */
7752 else if (token1->keyword == RID_USING)
7757 cp_parser_commit_to_tentative_parse (parser);
7758 /* If the token after `using' is `namespace', then we have a
7760 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7761 if (token2->keyword == RID_NAMESPACE)
7762 cp_parser_using_directive (parser);
7763 /* Otherwise, it's a using-declaration. */
7765 cp_parser_using_declaration (parser,
7766 /*access_declaration_p=*/false);
7768 /* If the next keyword is `__label__' we have a label declaration. */
7769 else if (token1->keyword == RID_LABEL)
7772 cp_parser_commit_to_tentative_parse (parser);
7773 cp_parser_label_declaration (parser);
7775 /* If the next token is `static_assert' we have a static assertion. */
7776 else if (token1->keyword == RID_STATIC_ASSERT)
7777 cp_parser_static_assert (parser, /*member_p=*/false);
7778 /* Anything else must be a simple-declaration. */
7780 cp_parser_simple_declaration (parser, !statement_p);
7783 /* Parse a simple-declaration.
7786 decl-specifier-seq [opt] init-declarator-list [opt] ;
7788 init-declarator-list:
7790 init-declarator-list , init-declarator
7792 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7793 function-definition as a simple-declaration. */
7796 cp_parser_simple_declaration (cp_parser* parser,
7797 bool function_definition_allowed_p)
7799 cp_decl_specifier_seq decl_specifiers;
7800 int declares_class_or_enum;
7801 bool saw_declarator;
7803 /* Defer access checks until we know what is being declared; the
7804 checks for names appearing in the decl-specifier-seq should be
7805 done as if we were in the scope of the thing being declared. */
7806 push_deferring_access_checks (dk_deferred);
7808 /* Parse the decl-specifier-seq. We have to keep track of whether
7809 or not the decl-specifier-seq declares a named class or
7810 enumeration type, since that is the only case in which the
7811 init-declarator-list is allowed to be empty.
7815 In a simple-declaration, the optional init-declarator-list can be
7816 omitted only when declaring a class or enumeration, that is when
7817 the decl-specifier-seq contains either a class-specifier, an
7818 elaborated-type-specifier, or an enum-specifier. */
7819 cp_parser_decl_specifier_seq (parser,
7820 CP_PARSER_FLAGS_OPTIONAL,
7822 &declares_class_or_enum);
7823 /* We no longer need to defer access checks. */
7824 stop_deferring_access_checks ();
7826 /* In a block scope, a valid declaration must always have a
7827 decl-specifier-seq. By not trying to parse declarators, we can
7828 resolve the declaration/expression ambiguity more quickly. */
7829 if (!function_definition_allowed_p
7830 && !decl_specifiers.any_specifiers_p)
7832 cp_parser_error (parser, "expected declaration");
7836 /* If the next two tokens are both identifiers, the code is
7837 erroneous. The usual cause of this situation is code like:
7841 where "T" should name a type -- but does not. */
7842 if (!decl_specifiers.type
7843 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7845 /* If parsing tentatively, we should commit; we really are
7846 looking at a declaration. */
7847 cp_parser_commit_to_tentative_parse (parser);
7852 /* If we have seen at least one decl-specifier, and the next token
7853 is not a parenthesis, then we must be looking at a declaration.
7854 (After "int (" we might be looking at a functional cast.) */
7855 if (decl_specifiers.any_specifiers_p
7856 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7857 cp_parser_commit_to_tentative_parse (parser);
7859 /* Keep going until we hit the `;' at the end of the simple
7861 saw_declarator = false;
7862 while (cp_lexer_next_token_is_not (parser->lexer,
7866 bool function_definition_p;
7871 /* If we are processing next declarator, coma is expected */
7872 token = cp_lexer_peek_token (parser->lexer);
7873 gcc_assert (token->type == CPP_COMMA);
7874 cp_lexer_consume_token (parser->lexer);
7877 saw_declarator = true;
7879 /* Parse the init-declarator. */
7880 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7882 function_definition_allowed_p,
7884 declares_class_or_enum,
7885 &function_definition_p);
7886 /* If an error occurred while parsing tentatively, exit quickly.
7887 (That usually happens when in the body of a function; each
7888 statement is treated as a declaration-statement until proven
7890 if (cp_parser_error_occurred (parser))
7892 /* Handle function definitions specially. */
7893 if (function_definition_p)
7895 /* If the next token is a `,', then we are probably
7896 processing something like:
7900 which is erroneous. */
7901 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7902 error ("mixing declarations and function-definitions is forbidden");
7903 /* Otherwise, we're done with the list of declarators. */
7906 pop_deferring_access_checks ();
7910 /* The next token should be either a `,' or a `;'. */
7911 token = cp_lexer_peek_token (parser->lexer);
7912 /* If it's a `,', there are more declarators to come. */
7913 if (token->type == CPP_COMMA)
7914 /* will be consumed next time around */;
7915 /* If it's a `;', we are done. */
7916 else if (token->type == CPP_SEMICOLON)
7918 /* Anything else is an error. */
7921 /* If we have already issued an error message we don't need
7922 to issue another one. */
7923 if (decl != error_mark_node
7924 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7925 cp_parser_error (parser, "expected %<,%> or %<;%>");
7926 /* Skip tokens until we reach the end of the statement. */
7927 cp_parser_skip_to_end_of_statement (parser);
7928 /* If the next token is now a `;', consume it. */
7929 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7930 cp_lexer_consume_token (parser->lexer);
7933 /* After the first time around, a function-definition is not
7934 allowed -- even if it was OK at first. For example:
7939 function_definition_allowed_p = false;
7942 /* Issue an error message if no declarators are present, and the
7943 decl-specifier-seq does not itself declare a class or
7945 if (!saw_declarator)
7947 if (cp_parser_declares_only_class_p (parser))
7948 shadow_tag (&decl_specifiers);
7949 /* Perform any deferred access checks. */
7950 perform_deferred_access_checks ();
7953 /* Consume the `;'. */
7954 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7957 pop_deferring_access_checks ();
7960 /* Parse a decl-specifier-seq.
7963 decl-specifier-seq [opt] decl-specifier
7966 storage-class-specifier
7977 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7979 The parser flags FLAGS is used to control type-specifier parsing.
7981 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7984 1: one of the decl-specifiers is an elaborated-type-specifier
7985 (i.e., a type declaration)
7986 2: one of the decl-specifiers is an enum-specifier or a
7987 class-specifier (i.e., a type definition)
7992 cp_parser_decl_specifier_seq (cp_parser* parser,
7993 cp_parser_flags flags,
7994 cp_decl_specifier_seq *decl_specs,
7995 int* declares_class_or_enum)
7997 bool constructor_possible_p = !parser->in_declarator_p;
7999 /* Clear DECL_SPECS. */
8000 clear_decl_specs (decl_specs);
8002 /* Assume no class or enumeration type is declared. */
8003 *declares_class_or_enum = 0;
8005 /* Keep reading specifiers until there are no more to read. */
8009 bool found_decl_spec;
8012 /* Peek at the next token. */
8013 token = cp_lexer_peek_token (parser->lexer);
8014 /* Handle attributes. */
8015 if (token->keyword == RID_ATTRIBUTE)
8017 /* Parse the attributes. */
8018 decl_specs->attributes
8019 = chainon (decl_specs->attributes,
8020 cp_parser_attributes_opt (parser));
8023 /* Assume we will find a decl-specifier keyword. */
8024 found_decl_spec = true;
8025 /* If the next token is an appropriate keyword, we can simply
8026 add it to the list. */
8027 switch (token->keyword)
8032 if (!at_class_scope_p ())
8034 error ("%<friend%> used outside of class");
8035 cp_lexer_purge_token (parser->lexer);
8039 ++decl_specs->specs[(int) ds_friend];
8040 /* Consume the token. */
8041 cp_lexer_consume_token (parser->lexer);
8045 /* function-specifier:
8052 cp_parser_function_specifier_opt (parser, decl_specs);
8058 ++decl_specs->specs[(int) ds_typedef];
8059 /* Consume the token. */
8060 cp_lexer_consume_token (parser->lexer);
8061 /* A constructor declarator cannot appear in a typedef. */
8062 constructor_possible_p = false;
8063 /* The "typedef" keyword can only occur in a declaration; we
8064 may as well commit at this point. */
8065 cp_parser_commit_to_tentative_parse (parser);
8067 if (decl_specs->storage_class != sc_none)
8068 decl_specs->conflicting_specifiers_p = true;
8071 /* storage-class-specifier:
8085 /* Consume the token. */
8086 cp_lexer_consume_token (parser->lexer);
8087 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8090 /* Consume the token. */
8091 cp_lexer_consume_token (parser->lexer);
8092 ++decl_specs->specs[(int) ds_thread];
8096 /* We did not yet find a decl-specifier yet. */
8097 found_decl_spec = false;
8101 /* Constructors are a special case. The `S' in `S()' is not a
8102 decl-specifier; it is the beginning of the declarator. */
8105 && constructor_possible_p
8106 && (cp_parser_constructor_declarator_p
8107 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8109 /* If we don't have a DECL_SPEC yet, then we must be looking at
8110 a type-specifier. */
8111 if (!found_decl_spec && !constructor_p)
8113 int decl_spec_declares_class_or_enum;
8114 bool is_cv_qualifier;
8118 = cp_parser_type_specifier (parser, flags,
8120 /*is_declaration=*/true,
8121 &decl_spec_declares_class_or_enum,
8124 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8126 /* If this type-specifier referenced a user-defined type
8127 (a typedef, class-name, etc.), then we can't allow any
8128 more such type-specifiers henceforth.
8132 The longest sequence of decl-specifiers that could
8133 possibly be a type name is taken as the
8134 decl-specifier-seq of a declaration. The sequence shall
8135 be self-consistent as described below.
8139 As a general rule, at most one type-specifier is allowed
8140 in the complete decl-specifier-seq of a declaration. The
8141 only exceptions are the following:
8143 -- const or volatile can be combined with any other
8146 -- signed or unsigned can be combined with char, long,
8154 void g (const int Pc);
8156 Here, Pc is *not* part of the decl-specifier seq; it's
8157 the declarator. Therefore, once we see a type-specifier
8158 (other than a cv-qualifier), we forbid any additional
8159 user-defined types. We *do* still allow things like `int
8160 int' to be considered a decl-specifier-seq, and issue the
8161 error message later. */
8162 if (type_spec && !is_cv_qualifier)
8163 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8164 /* A constructor declarator cannot follow a type-specifier. */
8167 constructor_possible_p = false;
8168 found_decl_spec = true;
8172 /* If we still do not have a DECL_SPEC, then there are no more
8174 if (!found_decl_spec)
8177 decl_specs->any_specifiers_p = true;
8178 /* After we see one decl-specifier, further decl-specifiers are
8180 flags |= CP_PARSER_FLAGS_OPTIONAL;
8183 cp_parser_check_decl_spec (decl_specs);
8185 /* Don't allow a friend specifier with a class definition. */
8186 if (decl_specs->specs[(int) ds_friend] != 0
8187 && (*declares_class_or_enum & 2))
8188 error ("class definition may not be declared a friend");
8191 /* Parse an (optional) storage-class-specifier.
8193 storage-class-specifier:
8202 storage-class-specifier:
8205 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8208 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8210 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8218 /* Consume the token. */
8219 return cp_lexer_consume_token (parser->lexer)->u.value;
8226 /* Parse an (optional) function-specifier.
8233 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8234 Updates DECL_SPECS, if it is non-NULL. */
8237 cp_parser_function_specifier_opt (cp_parser* parser,
8238 cp_decl_specifier_seq *decl_specs)
8240 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8244 ++decl_specs->specs[(int) ds_inline];
8248 /* 14.5.2.3 [temp.mem]
8250 A member function template shall not be virtual. */
8251 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8252 error ("templates may not be %<virtual%>");
8253 else if (decl_specs)
8254 ++decl_specs->specs[(int) ds_virtual];
8259 ++decl_specs->specs[(int) ds_explicit];
8266 /* Consume the token. */
8267 return cp_lexer_consume_token (parser->lexer)->u.value;
8270 /* Parse a linkage-specification.
8272 linkage-specification:
8273 extern string-literal { declaration-seq [opt] }
8274 extern string-literal declaration */
8277 cp_parser_linkage_specification (cp_parser* parser)
8281 /* Look for the `extern' keyword. */
8282 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8284 /* Look for the string-literal. */
8285 linkage = cp_parser_string_literal (parser, false, false);
8287 /* Transform the literal into an identifier. If the literal is a
8288 wide-character string, or contains embedded NULs, then we can't
8289 handle it as the user wants. */
8290 if (strlen (TREE_STRING_POINTER (linkage))
8291 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8293 cp_parser_error (parser, "invalid linkage-specification");
8294 /* Assume C++ linkage. */
8295 linkage = lang_name_cplusplus;
8298 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8300 /* We're now using the new linkage. */
8301 push_lang_context (linkage);
8303 /* If the next token is a `{', then we're using the first
8305 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8307 /* Consume the `{' token. */
8308 cp_lexer_consume_token (parser->lexer);
8309 /* Parse the declarations. */
8310 cp_parser_declaration_seq_opt (parser);
8311 /* Look for the closing `}'. */
8312 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8314 /* Otherwise, there's just one declaration. */
8317 bool saved_in_unbraced_linkage_specification_p;
8319 saved_in_unbraced_linkage_specification_p
8320 = parser->in_unbraced_linkage_specification_p;
8321 parser->in_unbraced_linkage_specification_p = true;
8322 cp_parser_declaration (parser);
8323 parser->in_unbraced_linkage_specification_p
8324 = saved_in_unbraced_linkage_specification_p;
8327 /* We're done with the linkage-specification. */
8328 pop_lang_context ();
8331 /* Parse a static_assert-declaration.
8333 static_assert-declaration:
8334 static_assert ( constant-expression , string-literal ) ;
8336 If MEMBER_P, this static_assert is a class member. */
8339 cp_parser_static_assert(cp_parser *parser, bool member_p)
8344 location_t saved_loc;
8346 /* Peek at the `static_assert' token so we can keep track of exactly
8347 where the static assertion started. */
8348 token = cp_lexer_peek_token (parser->lexer);
8349 saved_loc = token->location;
8351 /* Look for the `static_assert' keyword. */
8352 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8356 /* We know we are in a static assertion; commit to any tentative
8358 if (cp_parser_parsing_tentatively (parser))
8359 cp_parser_commit_to_tentative_parse (parser);
8361 /* Parse the `(' starting the static assertion condition. */
8362 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8364 /* Parse the constant-expression. */
8366 cp_parser_constant_expression (parser,
8367 /*allow_non_constant_p=*/false,
8368 /*non_constant_p=*/NULL);
8370 /* Parse the separating `,'. */
8371 cp_parser_require (parser, CPP_COMMA, "`,'");
8373 /* Parse the string-literal message. */
8374 message = cp_parser_string_literal (parser,
8375 /*translate=*/false,
8378 /* A `)' completes the static assertion. */
8379 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8380 cp_parser_skip_to_closing_parenthesis (parser,
8381 /*recovering=*/true,
8383 /*consume_paren=*/true);
8385 /* A semicolon terminates the declaration. */
8386 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8388 /* Complete the static assertion, which may mean either processing
8389 the static assert now or saving it for template instantiation. */
8390 finish_static_assert (condition, message, saved_loc, member_p);
8393 /* Parse a `decltype' type. Returns the type.
8395 simple-type-specifier:
8396 decltype ( expression ) */
8399 cp_parser_decltype (cp_parser *parser)
8402 bool id_expression_or_member_access_p = false;
8403 const char *saved_message;
8404 bool saved_integral_constant_expression_p;
8405 bool saved_non_integral_constant_expression_p;
8407 /* Look for the `decltype' token. */
8408 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8409 return error_mark_node;
8411 /* Types cannot be defined in a `decltype' expression. Save away the
8413 saved_message = parser->type_definition_forbidden_message;
8415 /* And create the new one. */
8416 parser->type_definition_forbidden_message
8417 = "types may not be defined in `decltype' expressions";
8419 /* The restrictions on constant-expressions do not apply inside
8420 decltype expressions. */
8421 saved_integral_constant_expression_p
8422 = parser->integral_constant_expression_p;
8423 saved_non_integral_constant_expression_p
8424 = parser->non_integral_constant_expression_p;
8425 parser->integral_constant_expression_p = false;
8427 /* Do not actually evaluate the expression. */
8430 /* Parse the opening `('. */
8431 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8433 /* First, try parsing an id-expression. */
8434 cp_parser_parse_tentatively (parser);
8435 expr = cp_parser_id_expression (parser,
8436 /*template_keyword_p=*/false,
8437 /*check_dependency_p=*/true,
8438 /*template_p=*/NULL,
8439 /*declarator_p=*/false,
8440 /*optional_p=*/false);
8442 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8444 bool non_integral_constant_expression_p = false;
8445 tree id_expression = expr;
8447 const char *error_msg;
8449 /* Lookup the name we got back from the id-expression. */
8450 expr = cp_parser_lookup_name (parser, expr,
8452 /*is_template=*/false,
8453 /*is_namespace=*/false,
8454 /*check_dependency=*/true,
8455 /*ambiguous_decls=*/NULL);
8458 && expr != error_mark_node
8459 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8460 && TREE_CODE (expr) != TYPE_DECL
8461 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8463 /* Complete lookup of the id-expression. */
8464 expr = (finish_id_expression
8465 (id_expression, expr, parser->scope, &idk,
8466 /*integral_constant_expression_p=*/false,
8467 /*allow_non_integral_constant_expression_p=*/true,
8468 &non_integral_constant_expression_p,
8469 /*template_p=*/false,
8471 /*address_p=*/false,
8472 /*template_arg_p=*/false,
8475 if (expr == error_mark_node)
8476 /* We found an id-expression, but it was something that we
8477 should not have found. This is an error, not something
8478 we can recover from, so note that we found an
8479 id-expression and we'll recover as gracefully as
8481 id_expression_or_member_access_p = true;
8485 && expr != error_mark_node
8486 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8487 /* We have an id-expression. */
8488 id_expression_or_member_access_p = true;
8491 if (!id_expression_or_member_access_p)
8493 /* Abort the id-expression parse. */
8494 cp_parser_abort_tentative_parse (parser);
8496 /* Parsing tentatively, again. */
8497 cp_parser_parse_tentatively (parser);
8499 /* Parse a class member access. */
8500 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8502 /*member_access_only_p=*/true);
8505 && expr != error_mark_node
8506 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8507 /* We have an id-expression. */
8508 id_expression_or_member_access_p = true;
8511 if (id_expression_or_member_access_p)
8512 /* We have parsed the complete id-expression or member access. */
8513 cp_parser_parse_definitely (parser);
8516 /* Abort our attempt to parse an id-expression or member access
8518 cp_parser_abort_tentative_parse (parser);
8520 /* Parse a full expression. */
8521 expr = cp_parser_expression (parser, /*cast_p=*/false);
8524 /* Go back to evaluating expressions. */
8527 /* Restore the old message and the integral constant expression
8529 parser->type_definition_forbidden_message = saved_message;
8530 parser->integral_constant_expression_p
8531 = saved_integral_constant_expression_p;
8532 parser->non_integral_constant_expression_p
8533 = saved_non_integral_constant_expression_p;
8535 if (expr == error_mark_node)
8537 /* Skip everything up to the closing `)'. */
8538 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8539 /*consume_paren=*/true);
8540 return error_mark_node;
8543 /* Parse to the closing `)'. */
8544 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8545 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8546 /*consume_paren=*/true);
8548 return finish_decltype_type (expr, id_expression_or_member_access_p);
8551 /* Special member functions [gram.special] */
8553 /* Parse a conversion-function-id.
8555 conversion-function-id:
8556 operator conversion-type-id
8558 Returns an IDENTIFIER_NODE representing the operator. */
8561 cp_parser_conversion_function_id (cp_parser* parser)
8565 tree saved_qualifying_scope;
8566 tree saved_object_scope;
8567 tree pushed_scope = NULL_TREE;
8569 /* Look for the `operator' token. */
8570 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8571 return error_mark_node;
8572 /* When we parse the conversion-type-id, the current scope will be
8573 reset. However, we need that information in able to look up the
8574 conversion function later, so we save it here. */
8575 saved_scope = parser->scope;
8576 saved_qualifying_scope = parser->qualifying_scope;
8577 saved_object_scope = parser->object_scope;
8578 /* We must enter the scope of the class so that the names of
8579 entities declared within the class are available in the
8580 conversion-type-id. For example, consider:
8587 S::operator I() { ... }
8589 In order to see that `I' is a type-name in the definition, we
8590 must be in the scope of `S'. */
8592 pushed_scope = push_scope (saved_scope);
8593 /* Parse the conversion-type-id. */
8594 type = cp_parser_conversion_type_id (parser);
8595 /* Leave the scope of the class, if any. */
8597 pop_scope (pushed_scope);
8598 /* Restore the saved scope. */
8599 parser->scope = saved_scope;
8600 parser->qualifying_scope = saved_qualifying_scope;
8601 parser->object_scope = saved_object_scope;
8602 /* If the TYPE is invalid, indicate failure. */
8603 if (type == error_mark_node)
8604 return error_mark_node;
8605 return mangle_conv_op_name_for_type (type);
8608 /* Parse a conversion-type-id:
8611 type-specifier-seq conversion-declarator [opt]
8613 Returns the TYPE specified. */
8616 cp_parser_conversion_type_id (cp_parser* parser)
8619 cp_decl_specifier_seq type_specifiers;
8620 cp_declarator *declarator;
8621 tree type_specified;
8623 /* Parse the attributes. */
8624 attributes = cp_parser_attributes_opt (parser);
8625 /* Parse the type-specifiers. */
8626 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8628 /* If that didn't work, stop. */
8629 if (type_specifiers.type == error_mark_node)
8630 return error_mark_node;
8631 /* Parse the conversion-declarator. */
8632 declarator = cp_parser_conversion_declarator_opt (parser);
8634 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8635 /*initialized=*/0, &attributes);
8637 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8638 return type_specified;
8641 /* Parse an (optional) conversion-declarator.
8643 conversion-declarator:
8644 ptr-operator conversion-declarator [opt]
8648 static cp_declarator *
8649 cp_parser_conversion_declarator_opt (cp_parser* parser)
8651 enum tree_code code;
8653 cp_cv_quals cv_quals;
8655 /* We don't know if there's a ptr-operator next, or not. */
8656 cp_parser_parse_tentatively (parser);
8657 /* Try the ptr-operator. */
8658 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8659 /* If it worked, look for more conversion-declarators. */
8660 if (cp_parser_parse_definitely (parser))
8662 cp_declarator *declarator;
8664 /* Parse another optional declarator. */
8665 declarator = cp_parser_conversion_declarator_opt (parser);
8667 return cp_parser_make_indirect_declarator
8668 (code, class_type, cv_quals, declarator);
8674 /* Parse an (optional) ctor-initializer.
8677 : mem-initializer-list
8679 Returns TRUE iff the ctor-initializer was actually present. */
8682 cp_parser_ctor_initializer_opt (cp_parser* parser)
8684 /* If the next token is not a `:', then there is no
8685 ctor-initializer. */
8686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8688 /* Do default initialization of any bases and members. */
8689 if (DECL_CONSTRUCTOR_P (current_function_decl))
8690 finish_mem_initializers (NULL_TREE);
8695 /* Consume the `:' token. */
8696 cp_lexer_consume_token (parser->lexer);
8697 /* And the mem-initializer-list. */
8698 cp_parser_mem_initializer_list (parser);
8703 /* Parse a mem-initializer-list.
8705 mem-initializer-list:
8706 mem-initializer ... [opt]
8707 mem-initializer ... [opt] , mem-initializer-list */
8710 cp_parser_mem_initializer_list (cp_parser* parser)
8712 tree mem_initializer_list = NULL_TREE;
8714 /* Let the semantic analysis code know that we are starting the
8715 mem-initializer-list. */
8716 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8717 error ("only constructors take base initializers");
8719 /* Loop through the list. */
8722 tree mem_initializer;
8724 /* Parse the mem-initializer. */
8725 mem_initializer = cp_parser_mem_initializer (parser);
8726 /* If the next token is a `...', we're expanding member initializers. */
8727 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8729 /* Consume the `...'. */
8730 cp_lexer_consume_token (parser->lexer);
8732 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8733 can be expanded but members cannot. */
8734 if (mem_initializer != error_mark_node
8735 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8737 error ("cannot expand initializer for member %<%D%>",
8738 TREE_PURPOSE (mem_initializer));
8739 mem_initializer = error_mark_node;
8742 /* Construct the pack expansion type. */
8743 if (mem_initializer != error_mark_node)
8744 mem_initializer = make_pack_expansion (mem_initializer);
8746 /* Add it to the list, unless it was erroneous. */
8747 if (mem_initializer != error_mark_node)
8749 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8750 mem_initializer_list = mem_initializer;
8752 /* If the next token is not a `,', we're done. */
8753 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8755 /* Consume the `,' token. */
8756 cp_lexer_consume_token (parser->lexer);
8759 /* Perform semantic analysis. */
8760 if (DECL_CONSTRUCTOR_P (current_function_decl))
8761 finish_mem_initializers (mem_initializer_list);
8764 /* Parse a mem-initializer.
8767 mem-initializer-id ( expression-list [opt] )
8772 ( expression-list [opt] )
8774 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8775 class) or FIELD_DECL (for a non-static data member) to initialize;
8776 the TREE_VALUE is the expression-list. An empty initialization
8777 list is represented by void_list_node. */
8780 cp_parser_mem_initializer (cp_parser* parser)
8782 tree mem_initializer_id;
8783 tree expression_list;
8786 /* Find out what is being initialized. */
8787 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8789 pedwarn ("anachronistic old-style base class initializer");
8790 mem_initializer_id = NULL_TREE;
8793 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8794 member = expand_member_init (mem_initializer_id);
8795 if (member && !DECL_P (member))
8796 in_base_initializer = 1;
8799 = cp_parser_parenthesized_expression_list (parser, false,
8801 /*allow_expansion_p=*/true,
8802 /*non_constant_p=*/NULL);
8803 if (expression_list == error_mark_node)
8804 return error_mark_node;
8805 if (!expression_list)
8806 expression_list = void_type_node;
8808 in_base_initializer = 0;
8810 return member ? build_tree_list (member, expression_list) : error_mark_node;
8813 /* Parse a mem-initializer-id.
8816 :: [opt] nested-name-specifier [opt] class-name
8819 Returns a TYPE indicating the class to be initializer for the first
8820 production. Returns an IDENTIFIER_NODE indicating the data member
8821 to be initialized for the second production. */
8824 cp_parser_mem_initializer_id (cp_parser* parser)
8826 bool global_scope_p;
8827 bool nested_name_specifier_p;
8828 bool template_p = false;
8831 /* `typename' is not allowed in this context ([temp.res]). */
8832 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8834 error ("keyword %<typename%> not allowed in this context (a qualified "
8835 "member initializer is implicitly a type)");
8836 cp_lexer_consume_token (parser->lexer);
8838 /* Look for the optional `::' operator. */
8840 = (cp_parser_global_scope_opt (parser,
8841 /*current_scope_valid_p=*/false)
8843 /* Look for the optional nested-name-specifier. The simplest way to
8848 The keyword `typename' is not permitted in a base-specifier or
8849 mem-initializer; in these contexts a qualified name that
8850 depends on a template-parameter is implicitly assumed to be a
8853 is to assume that we have seen the `typename' keyword at this
8855 nested_name_specifier_p
8856 = (cp_parser_nested_name_specifier_opt (parser,
8857 /*typename_keyword_p=*/true,
8858 /*check_dependency_p=*/true,
8860 /*is_declaration=*/true)
8862 if (nested_name_specifier_p)
8863 template_p = cp_parser_optional_template_keyword (parser);
8864 /* If there is a `::' operator or a nested-name-specifier, then we
8865 are definitely looking for a class-name. */
8866 if (global_scope_p || nested_name_specifier_p)
8867 return cp_parser_class_name (parser,
8868 /*typename_keyword_p=*/true,
8869 /*template_keyword_p=*/template_p,
8871 /*check_dependency_p=*/true,
8872 /*class_head_p=*/false,
8873 /*is_declaration=*/true);
8874 /* Otherwise, we could also be looking for an ordinary identifier. */
8875 cp_parser_parse_tentatively (parser);
8876 /* Try a class-name. */
8877 id = cp_parser_class_name (parser,
8878 /*typename_keyword_p=*/true,
8879 /*template_keyword_p=*/false,
8881 /*check_dependency_p=*/true,
8882 /*class_head_p=*/false,
8883 /*is_declaration=*/true);
8884 /* If we found one, we're done. */
8885 if (cp_parser_parse_definitely (parser))
8887 /* Otherwise, look for an ordinary identifier. */
8888 return cp_parser_identifier (parser);
8891 /* Overloading [gram.over] */
8893 /* Parse an operator-function-id.
8895 operator-function-id:
8898 Returns an IDENTIFIER_NODE for the operator which is a
8899 human-readable spelling of the identifier, e.g., `operator +'. */
8902 cp_parser_operator_function_id (cp_parser* parser)
8904 /* Look for the `operator' keyword. */
8905 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8906 return error_mark_node;
8907 /* And then the name of the operator itself. */
8908 return cp_parser_operator (parser);
8911 /* Parse an operator.
8914 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8915 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8916 || ++ -- , ->* -> () []
8923 Returns an IDENTIFIER_NODE for the operator which is a
8924 human-readable spelling of the identifier, e.g., `operator +'. */
8927 cp_parser_operator (cp_parser* parser)
8929 tree id = NULL_TREE;
8932 /* Peek at the next token. */
8933 token = cp_lexer_peek_token (parser->lexer);
8934 /* Figure out which operator we have. */
8935 switch (token->type)
8941 /* The keyword should be either `new' or `delete'. */
8942 if (token->keyword == RID_NEW)
8944 else if (token->keyword == RID_DELETE)
8949 /* Consume the `new' or `delete' token. */
8950 cp_lexer_consume_token (parser->lexer);
8952 /* Peek at the next token. */
8953 token = cp_lexer_peek_token (parser->lexer);
8954 /* If it's a `[' token then this is the array variant of the
8956 if (token->type == CPP_OPEN_SQUARE)
8958 /* Consume the `[' token. */
8959 cp_lexer_consume_token (parser->lexer);
8960 /* Look for the `]' token. */
8961 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8962 id = ansi_opname (op == NEW_EXPR
8963 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8965 /* Otherwise, we have the non-array variant. */
8967 id = ansi_opname (op);
8973 id = ansi_opname (PLUS_EXPR);
8977 id = ansi_opname (MINUS_EXPR);
8981 id = ansi_opname (MULT_EXPR);
8985 id = ansi_opname (TRUNC_DIV_EXPR);
8989 id = ansi_opname (TRUNC_MOD_EXPR);
8993 id = ansi_opname (BIT_XOR_EXPR);
8997 id = ansi_opname (BIT_AND_EXPR);
9001 id = ansi_opname (BIT_IOR_EXPR);
9005 id = ansi_opname (BIT_NOT_EXPR);
9009 id = ansi_opname (TRUTH_NOT_EXPR);
9013 id = ansi_assopname (NOP_EXPR);
9017 id = ansi_opname (LT_EXPR);
9021 id = ansi_opname (GT_EXPR);
9025 id = ansi_assopname (PLUS_EXPR);
9029 id = ansi_assopname (MINUS_EXPR);
9033 id = ansi_assopname (MULT_EXPR);
9037 id = ansi_assopname (TRUNC_DIV_EXPR);
9041 id = ansi_assopname (TRUNC_MOD_EXPR);
9045 id = ansi_assopname (BIT_XOR_EXPR);
9049 id = ansi_assopname (BIT_AND_EXPR);
9053 id = ansi_assopname (BIT_IOR_EXPR);
9057 id = ansi_opname (LSHIFT_EXPR);
9061 id = ansi_opname (RSHIFT_EXPR);
9065 id = ansi_assopname (LSHIFT_EXPR);
9069 id = ansi_assopname (RSHIFT_EXPR);
9073 id = ansi_opname (EQ_EXPR);
9077 id = ansi_opname (NE_EXPR);
9081 id = ansi_opname (LE_EXPR);
9084 case CPP_GREATER_EQ:
9085 id = ansi_opname (GE_EXPR);
9089 id = ansi_opname (TRUTH_ANDIF_EXPR);
9093 id = ansi_opname (TRUTH_ORIF_EXPR);
9097 id = ansi_opname (POSTINCREMENT_EXPR);
9100 case CPP_MINUS_MINUS:
9101 id = ansi_opname (PREDECREMENT_EXPR);
9105 id = ansi_opname (COMPOUND_EXPR);
9108 case CPP_DEREF_STAR:
9109 id = ansi_opname (MEMBER_REF);
9113 id = ansi_opname (COMPONENT_REF);
9116 case CPP_OPEN_PAREN:
9117 /* Consume the `('. */
9118 cp_lexer_consume_token (parser->lexer);
9119 /* Look for the matching `)'. */
9120 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9121 return ansi_opname (CALL_EXPR);
9123 case CPP_OPEN_SQUARE:
9124 /* Consume the `['. */
9125 cp_lexer_consume_token (parser->lexer);
9126 /* Look for the matching `]'. */
9127 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9128 return ansi_opname (ARRAY_REF);
9131 /* Anything else is an error. */
9135 /* If we have selected an identifier, we need to consume the
9138 cp_lexer_consume_token (parser->lexer);
9139 /* Otherwise, no valid operator name was present. */
9142 cp_parser_error (parser, "expected operator");
9143 id = error_mark_node;
9149 /* Parse a template-declaration.
9151 template-declaration:
9152 export [opt] template < template-parameter-list > declaration
9154 If MEMBER_P is TRUE, this template-declaration occurs within a
9157 The grammar rule given by the standard isn't correct. What
9160 template-declaration:
9161 export [opt] template-parameter-list-seq
9162 decl-specifier-seq [opt] init-declarator [opt] ;
9163 export [opt] template-parameter-list-seq
9166 template-parameter-list-seq:
9167 template-parameter-list-seq [opt]
9168 template < template-parameter-list > */
9171 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9173 /* Check for `export'. */
9174 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9176 /* Consume the `export' token. */
9177 cp_lexer_consume_token (parser->lexer);
9178 /* Warn that we do not support `export'. */
9179 warning (0, "keyword %<export%> not implemented, and will be ignored");
9182 cp_parser_template_declaration_after_export (parser, member_p);
9185 /* Parse a template-parameter-list.
9187 template-parameter-list:
9189 template-parameter-list , template-parameter
9191 Returns a TREE_LIST. Each node represents a template parameter.
9192 The nodes are connected via their TREE_CHAINs. */
9195 cp_parser_template_parameter_list (cp_parser* parser)
9197 tree parameter_list = NULL_TREE;
9199 begin_template_parm_list ();
9205 bool is_parameter_pack;
9207 /* Parse the template-parameter. */
9208 parameter = cp_parser_template_parameter (parser,
9210 &is_parameter_pack);
9211 /* Add it to the list. */
9212 if (parameter != error_mark_node)
9213 parameter_list = process_template_parm (parameter_list,
9219 tree err_parm = build_tree_list (parameter, parameter);
9220 TREE_VALUE (err_parm) = error_mark_node;
9221 parameter_list = chainon (parameter_list, err_parm);
9224 /* Peek at the next token. */
9225 token = cp_lexer_peek_token (parser->lexer);
9226 /* If it's not a `,', we're done. */
9227 if (token->type != CPP_COMMA)
9229 /* Otherwise, consume the `,' token. */
9230 cp_lexer_consume_token (parser->lexer);
9233 return end_template_parm_list (parameter_list);
9236 /* Parse a template-parameter.
9240 parameter-declaration
9242 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9243 the parameter. The TREE_PURPOSE is the default value, if any.
9244 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9245 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9246 set to true iff this parameter is a parameter pack. */
9249 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9250 bool *is_parameter_pack)
9253 cp_parameter_declarator *parameter_declarator;
9256 /* Assume it is a type parameter or a template parameter. */
9257 *is_non_type = false;
9258 /* Assume it not a parameter pack. */
9259 *is_parameter_pack = false;
9260 /* Peek at the next token. */
9261 token = cp_lexer_peek_token (parser->lexer);
9262 /* If it is `class' or `template', we have a type-parameter. */
9263 if (token->keyword == RID_TEMPLATE)
9264 return cp_parser_type_parameter (parser, is_parameter_pack);
9265 /* If it is `class' or `typename' we do not know yet whether it is a
9266 type parameter or a non-type parameter. Consider:
9268 template <typename T, typename T::X X> ...
9272 template <class C, class D*> ...
9274 Here, the first parameter is a type parameter, and the second is
9275 a non-type parameter. We can tell by looking at the token after
9276 the identifier -- if it is a `,', `=', or `>' then we have a type
9278 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9280 /* Peek at the token after `class' or `typename'. */
9281 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9282 /* If it's an ellipsis, we have a template type parameter
9284 if (token->type == CPP_ELLIPSIS)
9285 return cp_parser_type_parameter (parser, is_parameter_pack);
9286 /* If it's an identifier, skip it. */
9287 if (token->type == CPP_NAME)
9288 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9289 /* Now, see if the token looks like the end of a template
9291 if (token->type == CPP_COMMA
9292 || token->type == CPP_EQ
9293 || token->type == CPP_GREATER)
9294 return cp_parser_type_parameter (parser, is_parameter_pack);
9297 /* Otherwise, it is a non-type parameter.
9301 When parsing a default template-argument for a non-type
9302 template-parameter, the first non-nested `>' is taken as the end
9303 of the template parameter-list rather than a greater-than
9305 *is_non_type = true;
9306 parameter_declarator
9307 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9308 /*parenthesized_p=*/NULL);
9310 /* If the parameter declaration is marked as a parameter pack, set
9311 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9312 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9314 if (parameter_declarator
9315 && parameter_declarator->declarator
9316 && parameter_declarator->declarator->parameter_pack_p)
9318 *is_parameter_pack = true;
9319 parameter_declarator->declarator->parameter_pack_p = false;
9322 /* If the next token is an ellipsis, and we don't already have it
9323 marked as a parameter pack, then we have a parameter pack (that
9324 has no declarator); */
9325 if (!*is_parameter_pack
9326 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9327 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9329 /* Consume the `...'. */
9330 cp_lexer_consume_token (parser->lexer);
9331 maybe_warn_variadic_templates ();
9333 *is_parameter_pack = true;
9336 parm = grokdeclarator (parameter_declarator->declarator,
9337 ¶meter_declarator->decl_specifiers,
9338 PARM, /*initialized=*/0,
9340 if (parm == error_mark_node)
9341 return error_mark_node;
9343 return build_tree_list (parameter_declarator->default_argument, parm);
9346 /* Parse a type-parameter.
9349 class identifier [opt]
9350 class identifier [opt] = type-id
9351 typename identifier [opt]
9352 typename identifier [opt] = type-id
9353 template < template-parameter-list > class identifier [opt]
9354 template < template-parameter-list > class identifier [opt]
9357 GNU Extension (variadic templates):
9360 class ... identifier [opt]
9361 typename ... identifier [opt]
9363 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9364 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9365 the declaration of the parameter.
9367 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9370 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9375 /* Look for a keyword to tell us what kind of parameter this is. */
9376 token = cp_parser_require (parser, CPP_KEYWORD,
9377 "`class', `typename', or `template'");
9379 return error_mark_node;
9381 switch (token->keyword)
9387 tree default_argument;
9389 /* If the next token is an ellipsis, we have a template
9391 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9393 /* Consume the `...' token. */
9394 cp_lexer_consume_token (parser->lexer);
9395 maybe_warn_variadic_templates ();
9397 *is_parameter_pack = true;
9400 /* If the next token is an identifier, then it names the
9402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9403 identifier = cp_parser_identifier (parser);
9405 identifier = NULL_TREE;
9407 /* Create the parameter. */
9408 parameter = finish_template_type_parm (class_type_node, identifier);
9410 /* If the next token is an `=', we have a default argument. */
9411 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9413 /* Consume the `=' token. */
9414 cp_lexer_consume_token (parser->lexer);
9415 /* Parse the default-argument. */
9416 push_deferring_access_checks (dk_no_deferred);
9417 default_argument = cp_parser_type_id (parser);
9419 /* Template parameter packs cannot have default
9421 if (*is_parameter_pack)
9424 error ("template parameter pack %qD cannot have a default argument",
9427 error ("template parameter packs cannot have default arguments");
9428 default_argument = NULL_TREE;
9430 pop_deferring_access_checks ();
9433 default_argument = NULL_TREE;
9435 /* Create the combined representation of the parameter and the
9436 default argument. */
9437 parameter = build_tree_list (default_argument, parameter);
9443 tree parameter_list;
9445 tree default_argument;
9447 /* Look for the `<'. */
9448 cp_parser_require (parser, CPP_LESS, "`<'");
9449 /* Parse the template-parameter-list. */
9450 parameter_list = cp_parser_template_parameter_list (parser);
9451 /* Look for the `>'. */
9452 cp_parser_require (parser, CPP_GREATER, "`>'");
9453 /* Look for the `class' keyword. */
9454 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9455 /* If the next token is an ellipsis, we have a template
9457 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9459 /* Consume the `...' token. */
9460 cp_lexer_consume_token (parser->lexer);
9461 maybe_warn_variadic_templates ();
9463 *is_parameter_pack = true;
9465 /* If the next token is an `=', then there is a
9466 default-argument. If the next token is a `>', we are at
9467 the end of the parameter-list. If the next token is a `,',
9468 then we are at the end of this parameter. */
9469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9470 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9471 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9473 identifier = cp_parser_identifier (parser);
9474 /* Treat invalid names as if the parameter were nameless. */
9475 if (identifier == error_mark_node)
9476 identifier = NULL_TREE;
9479 identifier = NULL_TREE;
9481 /* Create the template parameter. */
9482 parameter = finish_template_template_parm (class_type_node,
9485 /* If the next token is an `=', then there is a
9486 default-argument. */
9487 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9491 /* Consume the `='. */
9492 cp_lexer_consume_token (parser->lexer);
9493 /* Parse the id-expression. */
9494 push_deferring_access_checks (dk_no_deferred);
9496 = cp_parser_id_expression (parser,
9497 /*template_keyword_p=*/false,
9498 /*check_dependency_p=*/true,
9499 /*template_p=*/&is_template,
9500 /*declarator_p=*/false,
9501 /*optional_p=*/false);
9502 if (TREE_CODE (default_argument) == TYPE_DECL)
9503 /* If the id-expression was a template-id that refers to
9504 a template-class, we already have the declaration here,
9505 so no further lookup is needed. */
9508 /* Look up the name. */
9510 = cp_parser_lookup_name (parser, default_argument,
9512 /*is_template=*/is_template,
9513 /*is_namespace=*/false,
9514 /*check_dependency=*/true,
9515 /*ambiguous_decls=*/NULL);
9516 /* See if the default argument is valid. */
9518 = check_template_template_default_arg (default_argument);
9520 /* Template parameter packs cannot have default
9522 if (*is_parameter_pack)
9525 error ("template parameter pack %qD cannot have a default argument",
9528 error ("template parameter packs cannot have default arguments");
9529 default_argument = NULL_TREE;
9531 pop_deferring_access_checks ();
9534 default_argument = NULL_TREE;
9536 /* Create the combined representation of the parameter and the
9537 default argument. */
9538 parameter = build_tree_list (default_argument, parameter);
9550 /* Parse a template-id.
9553 template-name < template-argument-list [opt] >
9555 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9556 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9557 returned. Otherwise, if the template-name names a function, or set
9558 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9559 names a class, returns a TYPE_DECL for the specialization.
9561 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9562 uninstantiated templates. */
9565 cp_parser_template_id (cp_parser *parser,
9566 bool template_keyword_p,
9567 bool check_dependency_p,
9568 bool is_declaration)
9574 cp_token_position start_of_id = 0;
9575 deferred_access_check *chk;
9576 VEC (deferred_access_check,gc) *access_check;
9577 cp_token *next_token, *next_token_2;
9580 /* If the next token corresponds to a template-id, there is no need
9582 next_token = cp_lexer_peek_token (parser->lexer);
9583 if (next_token->type == CPP_TEMPLATE_ID)
9585 struct tree_check *check_value;
9587 /* Get the stored value. */
9588 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9589 /* Perform any access checks that were deferred. */
9590 access_check = check_value->checks;
9594 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9597 perform_or_defer_access_check (chk->binfo,
9602 /* Return the stored value. */
9603 return check_value->value;
9606 /* Avoid performing name lookup if there is no possibility of
9607 finding a template-id. */
9608 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9609 || (next_token->type == CPP_NAME
9610 && !cp_parser_nth_token_starts_template_argument_list_p
9613 cp_parser_error (parser, "expected template-id");
9614 return error_mark_node;
9617 /* Remember where the template-id starts. */
9618 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9619 start_of_id = cp_lexer_token_position (parser->lexer, false);
9621 push_deferring_access_checks (dk_deferred);
9623 /* Parse the template-name. */
9624 is_identifier = false;
9625 template = cp_parser_template_name (parser, template_keyword_p,
9629 if (template == error_mark_node || is_identifier)
9631 pop_deferring_access_checks ();
9635 /* If we find the sequence `[:' after a template-name, it's probably
9636 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9637 parse correctly the argument list. */
9638 next_token = cp_lexer_peek_token (parser->lexer);
9639 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9640 if (next_token->type == CPP_OPEN_SQUARE
9641 && next_token->flags & DIGRAPH
9642 && next_token_2->type == CPP_COLON
9643 && !(next_token_2->flags & PREV_WHITE))
9645 cp_parser_parse_tentatively (parser);
9646 /* Change `:' into `::'. */
9647 next_token_2->type = CPP_SCOPE;
9648 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9650 cp_lexer_consume_token (parser->lexer);
9651 /* Parse the arguments. */
9652 arguments = cp_parser_enclosed_template_argument_list (parser);
9653 if (!cp_parser_parse_definitely (parser))
9655 /* If we couldn't parse an argument list, then we revert our changes
9656 and return simply an error. Maybe this is not a template-id
9658 next_token_2->type = CPP_COLON;
9659 cp_parser_error (parser, "expected %<<%>");
9660 pop_deferring_access_checks ();
9661 return error_mark_node;
9663 /* Otherwise, emit an error about the invalid digraph, but continue
9664 parsing because we got our argument list. */
9665 pedwarn ("%<<::%> cannot begin a template-argument list");
9666 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9667 "between %<<%> and %<::%>");
9668 if (!flag_permissive)
9673 inform ("(if you use -fpermissive G++ will accept your code)");
9680 /* Look for the `<' that starts the template-argument-list. */
9681 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9683 pop_deferring_access_checks ();
9684 return error_mark_node;
9686 /* Parse the arguments. */
9687 arguments = cp_parser_enclosed_template_argument_list (parser);
9690 /* Build a representation of the specialization. */
9691 if (TREE_CODE (template) == IDENTIFIER_NODE)
9692 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9693 else if (DECL_CLASS_TEMPLATE_P (template)
9694 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9696 bool entering_scope;
9697 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9698 template (rather than some instantiation thereof) only if
9699 is not nested within some other construct. For example, in
9700 "template <typename T> void f(T) { A<T>::", A<T> is just an
9701 instantiation of A. */
9702 entering_scope = (template_parm_scope_p ()
9703 && cp_lexer_next_token_is (parser->lexer,
9706 = finish_template_type (template, arguments, entering_scope);
9710 /* If it's not a class-template or a template-template, it should be
9711 a function-template. */
9712 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9713 || TREE_CODE (template) == OVERLOAD
9714 || BASELINK_P (template)));
9716 template_id = lookup_template_function (template, arguments);
9719 /* If parsing tentatively, replace the sequence of tokens that makes
9720 up the template-id with a CPP_TEMPLATE_ID token. That way,
9721 should we re-parse the token stream, we will not have to repeat
9722 the effort required to do the parse, nor will we issue duplicate
9723 error messages about problems during instantiation of the
9727 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9729 /* Reset the contents of the START_OF_ID token. */
9730 token->type = CPP_TEMPLATE_ID;
9731 /* Retrieve any deferred checks. Do not pop this access checks yet
9732 so the memory will not be reclaimed during token replacing below. */
9733 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9734 token->u.tree_check_value->value = template_id;
9735 token->u.tree_check_value->checks = get_deferred_access_checks ();
9736 token->keyword = RID_MAX;
9738 /* Purge all subsequent tokens. */
9739 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9741 /* ??? Can we actually assume that, if template_id ==
9742 error_mark_node, we will have issued a diagnostic to the
9743 user, as opposed to simply marking the tentative parse as
9745 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9746 error ("parse error in template argument list");
9749 pop_deferring_access_checks ();
9753 /* Parse a template-name.
9758 The standard should actually say:
9762 operator-function-id
9764 A defect report has been filed about this issue.
9766 A conversion-function-id cannot be a template name because they cannot
9767 be part of a template-id. In fact, looking at this code:
9771 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9772 It is impossible to call a templated conversion-function-id with an
9773 explicit argument list, since the only allowed template parameter is
9774 the type to which it is converting.
9776 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9777 `template' keyword, in a construction like:
9781 In that case `f' is taken to be a template-name, even though there
9782 is no way of knowing for sure.
9784 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9785 name refers to a set of overloaded functions, at least one of which
9786 is a template, or an IDENTIFIER_NODE with the name of the template,
9787 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9788 names are looked up inside uninstantiated templates. */
9791 cp_parser_template_name (cp_parser* parser,
9792 bool template_keyword_p,
9793 bool check_dependency_p,
9794 bool is_declaration,
9795 bool *is_identifier)
9801 /* If the next token is `operator', then we have either an
9802 operator-function-id or a conversion-function-id. */
9803 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9805 /* We don't know whether we're looking at an
9806 operator-function-id or a conversion-function-id. */
9807 cp_parser_parse_tentatively (parser);
9808 /* Try an operator-function-id. */
9809 identifier = cp_parser_operator_function_id (parser);
9810 /* If that didn't work, try a conversion-function-id. */
9811 if (!cp_parser_parse_definitely (parser))
9813 cp_parser_error (parser, "expected template-name");
9814 return error_mark_node;
9817 /* Look for the identifier. */
9819 identifier = cp_parser_identifier (parser);
9821 /* If we didn't find an identifier, we don't have a template-id. */
9822 if (identifier == error_mark_node)
9823 return error_mark_node;
9825 /* If the name immediately followed the `template' keyword, then it
9826 is a template-name. However, if the next token is not `<', then
9827 we do not treat it as a template-name, since it is not being used
9828 as part of a template-id. This enables us to handle constructs
9831 template <typename T> struct S { S(); };
9832 template <typename T> S<T>::S();
9834 correctly. We would treat `S' as a template -- if it were `S<T>'
9835 -- but we do not if there is no `<'. */
9837 if (processing_template_decl
9838 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9840 /* In a declaration, in a dependent context, we pretend that the
9841 "template" keyword was present in order to improve error
9842 recovery. For example, given:
9844 template <typename T> void f(T::X<int>);
9846 we want to treat "X<int>" as a template-id. */
9848 && !template_keyword_p
9849 && parser->scope && TYPE_P (parser->scope)
9850 && check_dependency_p
9851 && dependent_type_p (parser->scope)
9852 /* Do not do this for dtors (or ctors), since they never
9853 need the template keyword before their name. */
9854 && !constructor_name_p (identifier, parser->scope))
9856 cp_token_position start = 0;
9858 /* Explain what went wrong. */
9859 error ("non-template %qD used as template", identifier);
9860 inform ("use %<%T::template %D%> to indicate that it is a template",
9861 parser->scope, identifier);
9862 /* If parsing tentatively, find the location of the "<" token. */
9863 if (cp_parser_simulate_error (parser))
9864 start = cp_lexer_token_position (parser->lexer, true);
9865 /* Parse the template arguments so that we can issue error
9866 messages about them. */
9867 cp_lexer_consume_token (parser->lexer);
9868 cp_parser_enclosed_template_argument_list (parser);
9869 /* Skip tokens until we find a good place from which to
9870 continue parsing. */
9871 cp_parser_skip_to_closing_parenthesis (parser,
9872 /*recovering=*/true,
9874 /*consume_paren=*/false);
9875 /* If parsing tentatively, permanently remove the
9876 template argument list. That will prevent duplicate
9877 error messages from being issued about the missing
9878 "template" keyword. */
9880 cp_lexer_purge_tokens_after (parser->lexer, start);
9882 *is_identifier = true;
9886 /* If the "template" keyword is present, then there is generally
9887 no point in doing name-lookup, so we just return IDENTIFIER.
9888 But, if the qualifying scope is non-dependent then we can
9889 (and must) do name-lookup normally. */
9890 if (template_keyword_p
9892 || (TYPE_P (parser->scope)
9893 && dependent_type_p (parser->scope))))
9897 /* Look up the name. */
9898 decl = cp_parser_lookup_name (parser, identifier,
9900 /*is_template=*/false,
9901 /*is_namespace=*/false,
9903 /*ambiguous_decls=*/NULL);
9904 decl = maybe_get_template_decl_from_type_decl (decl);
9906 /* If DECL is a template, then the name was a template-name. */
9907 if (TREE_CODE (decl) == TEMPLATE_DECL)
9911 tree fn = NULL_TREE;
9913 /* The standard does not explicitly indicate whether a name that
9914 names a set of overloaded declarations, some of which are
9915 templates, is a template-name. However, such a name should
9916 be a template-name; otherwise, there is no way to form a
9917 template-id for the overloaded templates. */
9918 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9919 if (TREE_CODE (fns) == OVERLOAD)
9920 for (fn = fns; fn; fn = OVL_NEXT (fn))
9921 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9926 /* The name does not name a template. */
9927 cp_parser_error (parser, "expected template-name");
9928 return error_mark_node;
9932 /* If DECL is dependent, and refers to a function, then just return
9933 its name; we will look it up again during template instantiation. */
9934 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9936 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9937 if (TYPE_P (scope) && dependent_type_p (scope))
9944 /* Parse a template-argument-list.
9946 template-argument-list:
9947 template-argument ... [opt]
9948 template-argument-list , template-argument ... [opt]
9950 Returns a TREE_VEC containing the arguments. */
9953 cp_parser_template_argument_list (cp_parser* parser)
9955 tree fixed_args[10];
9956 unsigned n_args = 0;
9957 unsigned alloced = 10;
9958 tree *arg_ary = fixed_args;
9960 bool saved_in_template_argument_list_p;
9962 bool saved_non_ice_p;
9964 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9965 parser->in_template_argument_list_p = true;
9966 /* Even if the template-id appears in an integral
9967 constant-expression, the contents of the argument list do
9969 saved_ice_p = parser->integral_constant_expression_p;
9970 parser->integral_constant_expression_p = false;
9971 saved_non_ice_p = parser->non_integral_constant_expression_p;
9972 parser->non_integral_constant_expression_p = false;
9973 /* Parse the arguments. */
9979 /* Consume the comma. */
9980 cp_lexer_consume_token (parser->lexer);
9982 /* Parse the template-argument. */
9983 argument = cp_parser_template_argument (parser);
9985 /* If the next token is an ellipsis, we're expanding a template
9987 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9989 /* Consume the `...' token. */
9990 cp_lexer_consume_token (parser->lexer);
9992 /* Make the argument into a TYPE_PACK_EXPANSION or
9993 EXPR_PACK_EXPANSION. */
9994 argument = make_pack_expansion (argument);
9997 if (n_args == alloced)
10001 if (arg_ary == fixed_args)
10003 arg_ary = XNEWVEC (tree, alloced);
10004 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10007 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10009 arg_ary[n_args++] = argument;
10011 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10013 vec = make_tree_vec (n_args);
10016 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10018 if (arg_ary != fixed_args)
10020 parser->non_integral_constant_expression_p = saved_non_ice_p;
10021 parser->integral_constant_expression_p = saved_ice_p;
10022 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10026 /* Parse a template-argument.
10029 assignment-expression
10033 The representation is that of an assignment-expression, type-id, or
10034 id-expression -- except that the qualified id-expression is
10035 evaluated, so that the value returned is either a DECL or an
10038 Although the standard says "assignment-expression", it forbids
10039 throw-expressions or assignments in the template argument.
10040 Therefore, we use "conditional-expression" instead. */
10043 cp_parser_template_argument (cp_parser* parser)
10048 bool maybe_type_id = false;
10052 /* There's really no way to know what we're looking at, so we just
10053 try each alternative in order.
10057 In a template-argument, an ambiguity between a type-id and an
10058 expression is resolved to a type-id, regardless of the form of
10059 the corresponding template-parameter.
10061 Therefore, we try a type-id first. */
10062 cp_parser_parse_tentatively (parser);
10063 argument = cp_parser_type_id (parser);
10064 /* If there was no error parsing the type-id but the next token is a '>>',
10065 we probably found a typo for '> >'. But there are type-id which are
10066 also valid expressions. For instance:
10068 struct X { int operator >> (int); };
10069 template <int V> struct Foo {};
10072 Here 'X()' is a valid type-id of a function type, but the user just
10073 wanted to write the expression "X() >> 5". Thus, we remember that we
10074 found a valid type-id, but we still try to parse the argument as an
10075 expression to see what happens. */
10076 if (!cp_parser_error_occurred (parser)
10077 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10079 maybe_type_id = true;
10080 cp_parser_abort_tentative_parse (parser);
10084 /* If the next token isn't a `,' or a `>', then this argument wasn't
10085 really finished. This means that the argument is not a valid
10087 if (!cp_parser_next_token_ends_template_argument_p (parser))
10088 cp_parser_error (parser, "expected template-argument");
10089 /* If that worked, we're done. */
10090 if (cp_parser_parse_definitely (parser))
10093 /* We're still not sure what the argument will be. */
10094 cp_parser_parse_tentatively (parser);
10095 /* Try a template. */
10096 argument = cp_parser_id_expression (parser,
10097 /*template_keyword_p=*/false,
10098 /*check_dependency_p=*/true,
10100 /*declarator_p=*/false,
10101 /*optional_p=*/false);
10102 /* If the next token isn't a `,' or a `>', then this argument wasn't
10103 really finished. */
10104 if (!cp_parser_next_token_ends_template_argument_p (parser))
10105 cp_parser_error (parser, "expected template-argument");
10106 if (!cp_parser_error_occurred (parser))
10108 /* Figure out what is being referred to. If the id-expression
10109 was for a class template specialization, then we will have a
10110 TYPE_DECL at this point. There is no need to do name lookup
10111 at this point in that case. */
10112 if (TREE_CODE (argument) != TYPE_DECL)
10113 argument = cp_parser_lookup_name (parser, argument,
10115 /*is_template=*/template_p,
10116 /*is_namespace=*/false,
10117 /*check_dependency=*/true,
10118 /*ambiguous_decls=*/NULL);
10119 if (TREE_CODE (argument) != TEMPLATE_DECL
10120 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10121 cp_parser_error (parser, "expected template-name");
10123 if (cp_parser_parse_definitely (parser))
10125 /* It must be a non-type argument. There permitted cases are given
10126 in [temp.arg.nontype]:
10128 -- an integral constant-expression of integral or enumeration
10131 -- the name of a non-type template-parameter; or
10133 -- the name of an object or function with external linkage...
10135 -- the address of an object or function with external linkage...
10137 -- a pointer to member... */
10138 /* Look for a non-type template parameter. */
10139 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10141 cp_parser_parse_tentatively (parser);
10142 argument = cp_parser_primary_expression (parser,
10143 /*adress_p=*/false,
10145 /*template_arg_p=*/true,
10147 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10148 || !cp_parser_next_token_ends_template_argument_p (parser))
10149 cp_parser_simulate_error (parser);
10150 if (cp_parser_parse_definitely (parser))
10154 /* If the next token is "&", the argument must be the address of an
10155 object or function with external linkage. */
10156 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10158 cp_lexer_consume_token (parser->lexer);
10159 /* See if we might have an id-expression. */
10160 token = cp_lexer_peek_token (parser->lexer);
10161 if (token->type == CPP_NAME
10162 || token->keyword == RID_OPERATOR
10163 || token->type == CPP_SCOPE
10164 || token->type == CPP_TEMPLATE_ID
10165 || token->type == CPP_NESTED_NAME_SPECIFIER)
10167 cp_parser_parse_tentatively (parser);
10168 argument = cp_parser_primary_expression (parser,
10171 /*template_arg_p=*/true,
10173 if (cp_parser_error_occurred (parser)
10174 || !cp_parser_next_token_ends_template_argument_p (parser))
10175 cp_parser_abort_tentative_parse (parser);
10178 if (TREE_CODE (argument) == INDIRECT_REF)
10180 gcc_assert (REFERENCE_REF_P (argument));
10181 argument = TREE_OPERAND (argument, 0);
10184 if (TREE_CODE (argument) == VAR_DECL)
10186 /* A variable without external linkage might still be a
10187 valid constant-expression, so no error is issued here
10188 if the external-linkage check fails. */
10189 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10190 cp_parser_simulate_error (parser);
10192 else if (is_overloaded_fn (argument))
10193 /* All overloaded functions are allowed; if the external
10194 linkage test does not pass, an error will be issued
10198 && (TREE_CODE (argument) == OFFSET_REF
10199 || TREE_CODE (argument) == SCOPE_REF))
10200 /* A pointer-to-member. */
10202 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10205 cp_parser_simulate_error (parser);
10207 if (cp_parser_parse_definitely (parser))
10210 argument = build_x_unary_op (ADDR_EXPR, argument);
10215 /* If the argument started with "&", there are no other valid
10216 alternatives at this point. */
10219 cp_parser_error (parser, "invalid non-type template argument");
10220 return error_mark_node;
10223 /* If the argument wasn't successfully parsed as a type-id followed
10224 by '>>', the argument can only be a constant expression now.
10225 Otherwise, we try parsing the constant-expression tentatively,
10226 because the argument could really be a type-id. */
10228 cp_parser_parse_tentatively (parser);
10229 argument = cp_parser_constant_expression (parser,
10230 /*allow_non_constant_p=*/false,
10231 /*non_constant_p=*/NULL);
10232 argument = fold_non_dependent_expr (argument);
10233 if (!maybe_type_id)
10235 if (!cp_parser_next_token_ends_template_argument_p (parser))
10236 cp_parser_error (parser, "expected template-argument");
10237 if (cp_parser_parse_definitely (parser))
10239 /* We did our best to parse the argument as a non type-id, but that
10240 was the only alternative that matched (albeit with a '>' after
10241 it). We can assume it's just a typo from the user, and a
10242 diagnostic will then be issued. */
10243 return cp_parser_type_id (parser);
10246 /* Parse an explicit-instantiation.
10248 explicit-instantiation:
10249 template declaration
10251 Although the standard says `declaration', what it really means is:
10253 explicit-instantiation:
10254 template decl-specifier-seq [opt] declarator [opt] ;
10256 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10257 supposed to be allowed. A defect report has been filed about this
10262 explicit-instantiation:
10263 storage-class-specifier template
10264 decl-specifier-seq [opt] declarator [opt] ;
10265 function-specifier template
10266 decl-specifier-seq [opt] declarator [opt] ; */
10269 cp_parser_explicit_instantiation (cp_parser* parser)
10271 int declares_class_or_enum;
10272 cp_decl_specifier_seq decl_specifiers;
10273 tree extension_specifier = NULL_TREE;
10275 /* Look for an (optional) storage-class-specifier or
10276 function-specifier. */
10277 if (cp_parser_allow_gnu_extensions_p (parser))
10279 extension_specifier
10280 = cp_parser_storage_class_specifier_opt (parser);
10281 if (!extension_specifier)
10282 extension_specifier
10283 = cp_parser_function_specifier_opt (parser,
10284 /*decl_specs=*/NULL);
10287 /* Look for the `template' keyword. */
10288 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10289 /* Let the front end know that we are processing an explicit
10291 begin_explicit_instantiation ();
10292 /* [temp.explicit] says that we are supposed to ignore access
10293 control while processing explicit instantiation directives. */
10294 push_deferring_access_checks (dk_no_check);
10295 /* Parse a decl-specifier-seq. */
10296 cp_parser_decl_specifier_seq (parser,
10297 CP_PARSER_FLAGS_OPTIONAL,
10299 &declares_class_or_enum);
10300 /* If there was exactly one decl-specifier, and it declared a class,
10301 and there's no declarator, then we have an explicit type
10303 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10307 type = check_tag_decl (&decl_specifiers);
10308 /* Turn access control back on for names used during
10309 template instantiation. */
10310 pop_deferring_access_checks ();
10312 do_type_instantiation (type, extension_specifier,
10313 /*complain=*/tf_error);
10317 cp_declarator *declarator;
10320 /* Parse the declarator. */
10322 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10323 /*ctor_dtor_or_conv_p=*/NULL,
10324 /*parenthesized_p=*/NULL,
10325 /*member_p=*/false);
10326 if (declares_class_or_enum & 2)
10327 cp_parser_check_for_definition_in_return_type (declarator,
10328 decl_specifiers.type);
10329 if (declarator != cp_error_declarator)
10331 decl = grokdeclarator (declarator, &decl_specifiers,
10332 NORMAL, 0, &decl_specifiers.attributes);
10333 /* Turn access control back on for names used during
10334 template instantiation. */
10335 pop_deferring_access_checks ();
10336 /* Do the explicit instantiation. */
10337 do_decl_instantiation (decl, extension_specifier);
10341 pop_deferring_access_checks ();
10342 /* Skip the body of the explicit instantiation. */
10343 cp_parser_skip_to_end_of_statement (parser);
10346 /* We're done with the instantiation. */
10347 end_explicit_instantiation ();
10349 cp_parser_consume_semicolon_at_end_of_statement (parser);
10352 /* Parse an explicit-specialization.
10354 explicit-specialization:
10355 template < > declaration
10357 Although the standard says `declaration', what it really means is:
10359 explicit-specialization:
10360 template <> decl-specifier [opt] init-declarator [opt] ;
10361 template <> function-definition
10362 template <> explicit-specialization
10363 template <> template-declaration */
10366 cp_parser_explicit_specialization (cp_parser* parser)
10368 bool need_lang_pop;
10369 /* Look for the `template' keyword. */
10370 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10371 /* Look for the `<'. */
10372 cp_parser_require (parser, CPP_LESS, "`<'");
10373 /* Look for the `>'. */
10374 cp_parser_require (parser, CPP_GREATER, "`>'");
10375 /* We have processed another parameter list. */
10376 ++parser->num_template_parameter_lists;
10379 A template ... explicit specialization ... shall not have C
10381 if (current_lang_name == lang_name_c)
10383 error ("template specialization with C linkage");
10384 /* Give it C++ linkage to avoid confusing other parts of the
10386 push_lang_context (lang_name_cplusplus);
10387 need_lang_pop = true;
10390 need_lang_pop = false;
10391 /* Let the front end know that we are beginning a specialization. */
10392 if (!begin_specialization ())
10394 end_specialization ();
10395 cp_parser_skip_to_end_of_block_or_statement (parser);
10399 /* If the next keyword is `template', we need to figure out whether
10400 or not we're looking a template-declaration. */
10401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10403 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10404 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10405 cp_parser_template_declaration_after_export (parser,
10406 /*member_p=*/false);
10408 cp_parser_explicit_specialization (parser);
10411 /* Parse the dependent declaration. */
10412 cp_parser_single_declaration (parser,
10414 /*member_p=*/false,
10415 /*explicit_specialization_p=*/true,
10416 /*friend_p=*/NULL);
10417 /* We're done with the specialization. */
10418 end_specialization ();
10419 /* For the erroneous case of a template with C linkage, we pushed an
10420 implicit C++ linkage scope; exit that scope now. */
10422 pop_lang_context ();
10423 /* We're done with this parameter list. */
10424 --parser->num_template_parameter_lists;
10427 /* Parse a type-specifier.
10430 simple-type-specifier
10433 elaborated-type-specifier
10441 Returns a representation of the type-specifier. For a
10442 class-specifier, enum-specifier, or elaborated-type-specifier, a
10443 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10445 The parser flags FLAGS is used to control type-specifier parsing.
10447 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10448 in a decl-specifier-seq.
10450 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10451 class-specifier, enum-specifier, or elaborated-type-specifier, then
10452 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10453 if a type is declared; 2 if it is defined. Otherwise, it is set to
10456 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10457 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10458 is set to FALSE. */
10461 cp_parser_type_specifier (cp_parser* parser,
10462 cp_parser_flags flags,
10463 cp_decl_specifier_seq *decl_specs,
10464 bool is_declaration,
10465 int* declares_class_or_enum,
10466 bool* is_cv_qualifier)
10468 tree type_spec = NULL_TREE;
10471 cp_decl_spec ds = ds_last;
10473 /* Assume this type-specifier does not declare a new type. */
10474 if (declares_class_or_enum)
10475 *declares_class_or_enum = 0;
10476 /* And that it does not specify a cv-qualifier. */
10477 if (is_cv_qualifier)
10478 *is_cv_qualifier = false;
10479 /* Peek at the next token. */
10480 token = cp_lexer_peek_token (parser->lexer);
10482 /* If we're looking at a keyword, we can use that to guide the
10483 production we choose. */
10484 keyword = token->keyword;
10488 /* Look for the enum-specifier. */
10489 type_spec = cp_parser_enum_specifier (parser);
10490 /* If that worked, we're done. */
10493 if (declares_class_or_enum)
10494 *declares_class_or_enum = 2;
10496 cp_parser_set_decl_spec_type (decl_specs,
10498 /*user_defined_p=*/true);
10502 goto elaborated_type_specifier;
10504 /* Any of these indicate either a class-specifier, or an
10505 elaborated-type-specifier. */
10509 /* Parse tentatively so that we can back up if we don't find a
10510 class-specifier. */
10511 cp_parser_parse_tentatively (parser);
10512 /* Look for the class-specifier. */
10513 type_spec = cp_parser_class_specifier (parser);
10514 /* If that worked, we're done. */
10515 if (cp_parser_parse_definitely (parser))
10517 if (declares_class_or_enum)
10518 *declares_class_or_enum = 2;
10520 cp_parser_set_decl_spec_type (decl_specs,
10522 /*user_defined_p=*/true);
10526 /* Fall through. */
10527 elaborated_type_specifier:
10528 /* We're declaring (not defining) a class or enum. */
10529 if (declares_class_or_enum)
10530 *declares_class_or_enum = 1;
10532 /* Fall through. */
10534 /* Look for an elaborated-type-specifier. */
10536 = (cp_parser_elaborated_type_specifier
10538 decl_specs && decl_specs->specs[(int) ds_friend],
10541 cp_parser_set_decl_spec_type (decl_specs,
10543 /*user_defined_p=*/true);
10548 if (is_cv_qualifier)
10549 *is_cv_qualifier = true;
10554 if (is_cv_qualifier)
10555 *is_cv_qualifier = true;
10560 if (is_cv_qualifier)
10561 *is_cv_qualifier = true;
10565 /* The `__complex__' keyword is a GNU extension. */
10573 /* Handle simple keywords. */
10578 ++decl_specs->specs[(int)ds];
10579 decl_specs->any_specifiers_p = true;
10581 return cp_lexer_consume_token (parser->lexer)->u.value;
10584 /* If we do not already have a type-specifier, assume we are looking
10585 at a simple-type-specifier. */
10586 type_spec = cp_parser_simple_type_specifier (parser,
10590 /* If we didn't find a type-specifier, and a type-specifier was not
10591 optional in this context, issue an error message. */
10592 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10594 cp_parser_error (parser, "expected type specifier");
10595 return error_mark_node;
10601 /* Parse a simple-type-specifier.
10603 simple-type-specifier:
10604 :: [opt] nested-name-specifier [opt] type-name
10605 :: [opt] nested-name-specifier template template-id
10620 simple-type-specifier:
10621 decltype ( expression )
10625 simple-type-specifier:
10626 __typeof__ unary-expression
10627 __typeof__ ( type-id )
10629 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10630 appropriately updated. */
10633 cp_parser_simple_type_specifier (cp_parser* parser,
10634 cp_decl_specifier_seq *decl_specs,
10635 cp_parser_flags flags)
10637 tree type = NULL_TREE;
10640 /* Peek at the next token. */
10641 token = cp_lexer_peek_token (parser->lexer);
10643 /* If we're looking at a keyword, things are easy. */
10644 switch (token->keyword)
10648 decl_specs->explicit_char_p = true;
10649 type = char_type_node;
10652 type = wchar_type_node;
10655 type = boolean_type_node;
10659 ++decl_specs->specs[(int) ds_short];
10660 type = short_integer_type_node;
10664 decl_specs->explicit_int_p = true;
10665 type = integer_type_node;
10669 ++decl_specs->specs[(int) ds_long];
10670 type = long_integer_type_node;
10674 ++decl_specs->specs[(int) ds_signed];
10675 type = integer_type_node;
10679 ++decl_specs->specs[(int) ds_unsigned];
10680 type = unsigned_type_node;
10683 type = float_type_node;
10686 type = double_type_node;
10689 type = void_type_node;
10693 /* Parse the `decltype' type. */
10694 type = cp_parser_decltype (parser);
10697 cp_parser_set_decl_spec_type (decl_specs, type,
10698 /*user_defined_p=*/true);
10703 /* Consume the `typeof' token. */
10704 cp_lexer_consume_token (parser->lexer);
10705 /* Parse the operand to `typeof'. */
10706 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10707 /* If it is not already a TYPE, take its type. */
10708 if (!TYPE_P (type))
10709 type = finish_typeof (type);
10712 cp_parser_set_decl_spec_type (decl_specs, type,
10713 /*user_defined_p=*/true);
10721 /* If the type-specifier was for a built-in type, we're done. */
10726 /* Record the type. */
10728 && (token->keyword != RID_SIGNED
10729 && token->keyword != RID_UNSIGNED
10730 && token->keyword != RID_SHORT
10731 && token->keyword != RID_LONG))
10732 cp_parser_set_decl_spec_type (decl_specs,
10734 /*user_defined=*/false);
10736 decl_specs->any_specifiers_p = true;
10738 /* Consume the token. */
10739 id = cp_lexer_consume_token (parser->lexer)->u.value;
10741 /* There is no valid C++ program where a non-template type is
10742 followed by a "<". That usually indicates that the user thought
10743 that the type was a template. */
10744 cp_parser_check_for_invalid_template_id (parser, type);
10746 return TYPE_NAME (type);
10749 /* The type-specifier must be a user-defined type. */
10750 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10755 /* Don't gobble tokens or issue error messages if this is an
10756 optional type-specifier. */
10757 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10758 cp_parser_parse_tentatively (parser);
10760 /* Look for the optional `::' operator. */
10762 = (cp_parser_global_scope_opt (parser,
10763 /*current_scope_valid_p=*/false)
10765 /* Look for the nested-name specifier. */
10767 = (cp_parser_nested_name_specifier_opt (parser,
10768 /*typename_keyword_p=*/false,
10769 /*check_dependency_p=*/true,
10771 /*is_declaration=*/false)
10773 /* If we have seen a nested-name-specifier, and the next token
10774 is `template', then we are using the template-id production. */
10776 && cp_parser_optional_template_keyword (parser))
10778 /* Look for the template-id. */
10779 type = cp_parser_template_id (parser,
10780 /*template_keyword_p=*/true,
10781 /*check_dependency_p=*/true,
10782 /*is_declaration=*/false);
10783 /* If the template-id did not name a type, we are out of
10785 if (TREE_CODE (type) != TYPE_DECL)
10787 cp_parser_error (parser, "expected template-id for type");
10791 /* Otherwise, look for a type-name. */
10793 type = cp_parser_type_name (parser);
10794 /* Keep track of all name-lookups performed in class scopes. */
10798 && TREE_CODE (type) == TYPE_DECL
10799 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10800 maybe_note_name_used_in_class (DECL_NAME (type), type);
10801 /* If it didn't work out, we don't have a TYPE. */
10802 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10803 && !cp_parser_parse_definitely (parser))
10805 if (type && decl_specs)
10806 cp_parser_set_decl_spec_type (decl_specs, type,
10807 /*user_defined=*/true);
10810 /* If we didn't get a type-name, issue an error message. */
10811 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10813 cp_parser_error (parser, "expected type-name");
10814 return error_mark_node;
10817 /* There is no valid C++ program where a non-template type is
10818 followed by a "<". That usually indicates that the user thought
10819 that the type was a template. */
10820 if (type && type != error_mark_node)
10822 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10823 If it is, then the '<'...'>' enclose protocol names rather than
10824 template arguments, and so everything is fine. */
10825 if (c_dialect_objc ()
10826 && (objc_is_id (type) || objc_is_class_name (type)))
10828 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10829 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10831 /* Clobber the "unqualified" type previously entered into
10832 DECL_SPECS with the new, improved protocol-qualified version. */
10834 decl_specs->type = qual_type;
10839 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10845 /* Parse a type-name.
10858 Returns a TYPE_DECL for the type. */
10861 cp_parser_type_name (cp_parser* parser)
10866 /* We can't know yet whether it is a class-name or not. */
10867 cp_parser_parse_tentatively (parser);
10868 /* Try a class-name. */
10869 type_decl = cp_parser_class_name (parser,
10870 /*typename_keyword_p=*/false,
10871 /*template_keyword_p=*/false,
10873 /*check_dependency_p=*/true,
10874 /*class_head_p=*/false,
10875 /*is_declaration=*/false);
10876 /* If it's not a class-name, keep looking. */
10877 if (!cp_parser_parse_definitely (parser))
10879 /* It must be a typedef-name or an enum-name. */
10880 identifier = cp_parser_identifier (parser);
10881 if (identifier == error_mark_node)
10882 return error_mark_node;
10884 /* Look up the type-name. */
10885 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10887 if (TREE_CODE (type_decl) != TYPE_DECL
10888 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10890 /* See if this is an Objective-C type. */
10891 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10892 tree type = objc_get_protocol_qualified_type (identifier, protos);
10894 type_decl = TYPE_NAME (type);
10897 /* Issue an error if we did not find a type-name. */
10898 if (TREE_CODE (type_decl) != TYPE_DECL)
10900 if (!cp_parser_simulate_error (parser))
10901 cp_parser_name_lookup_error (parser, identifier, type_decl,
10903 type_decl = error_mark_node;
10905 /* Remember that the name was used in the definition of the
10906 current class so that we can check later to see if the
10907 meaning would have been different after the class was
10908 entirely defined. */
10909 else if (type_decl != error_mark_node
10911 maybe_note_name_used_in_class (identifier, type_decl);
10918 /* Parse an elaborated-type-specifier. Note that the grammar given
10919 here incorporates the resolution to DR68.
10921 elaborated-type-specifier:
10922 class-key :: [opt] nested-name-specifier [opt] identifier
10923 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10924 enum :: [opt] nested-name-specifier [opt] identifier
10925 typename :: [opt] nested-name-specifier identifier
10926 typename :: [opt] nested-name-specifier template [opt]
10931 elaborated-type-specifier:
10932 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10933 class-key attributes :: [opt] nested-name-specifier [opt]
10934 template [opt] template-id
10935 enum attributes :: [opt] nested-name-specifier [opt] identifier
10937 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10938 declared `friend'. If IS_DECLARATION is TRUE, then this
10939 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10940 something is being declared.
10942 Returns the TYPE specified. */
10945 cp_parser_elaborated_type_specifier (cp_parser* parser,
10947 bool is_declaration)
10949 enum tag_types tag_type;
10951 tree type = NULL_TREE;
10952 tree attributes = NULL_TREE;
10954 /* See if we're looking at the `enum' keyword. */
10955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10957 /* Consume the `enum' token. */
10958 cp_lexer_consume_token (parser->lexer);
10959 /* Remember that it's an enumeration type. */
10960 tag_type = enum_type;
10961 /* Parse the attributes. */
10962 attributes = cp_parser_attributes_opt (parser);
10964 /* Or, it might be `typename'. */
10965 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10968 /* Consume the `typename' token. */
10969 cp_lexer_consume_token (parser->lexer);
10970 /* Remember that it's a `typename' type. */
10971 tag_type = typename_type;
10972 /* The `typename' keyword is only allowed in templates. */
10973 if (!processing_template_decl)
10974 pedwarn ("using %<typename%> outside of template");
10976 /* Otherwise it must be a class-key. */
10979 tag_type = cp_parser_class_key (parser);
10980 if (tag_type == none_type)
10981 return error_mark_node;
10982 /* Parse the attributes. */
10983 attributes = cp_parser_attributes_opt (parser);
10986 /* Look for the `::' operator. */
10987 cp_parser_global_scope_opt (parser,
10988 /*current_scope_valid_p=*/false);
10989 /* Look for the nested-name-specifier. */
10990 if (tag_type == typename_type)
10992 if (!cp_parser_nested_name_specifier (parser,
10993 /*typename_keyword_p=*/true,
10994 /*check_dependency_p=*/true,
10997 return error_mark_node;
11000 /* Even though `typename' is not present, the proposed resolution
11001 to Core Issue 180 says that in `class A<T>::B', `B' should be
11002 considered a type-name, even if `A<T>' is dependent. */
11003 cp_parser_nested_name_specifier_opt (parser,
11004 /*typename_keyword_p=*/true,
11005 /*check_dependency_p=*/true,
11008 /* For everything but enumeration types, consider a template-id.
11009 For an enumeration type, consider only a plain identifier. */
11010 if (tag_type != enum_type)
11012 bool template_p = false;
11015 /* Allow the `template' keyword. */
11016 template_p = cp_parser_optional_template_keyword (parser);
11017 /* If we didn't see `template', we don't know if there's a
11018 template-id or not. */
11020 cp_parser_parse_tentatively (parser);
11021 /* Parse the template-id. */
11022 decl = cp_parser_template_id (parser, template_p,
11023 /*check_dependency_p=*/true,
11025 /* If we didn't find a template-id, look for an ordinary
11027 if (!template_p && !cp_parser_parse_definitely (parser))
11029 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11030 in effect, then we must assume that, upon instantiation, the
11031 template will correspond to a class. */
11032 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11033 && tag_type == typename_type)
11034 type = make_typename_type (parser->scope, decl,
11036 /*complain=*/tf_error);
11038 type = TREE_TYPE (decl);
11043 identifier = cp_parser_identifier (parser);
11045 if (identifier == error_mark_node)
11047 parser->scope = NULL_TREE;
11048 return error_mark_node;
11051 /* For a `typename', we needn't call xref_tag. */
11052 if (tag_type == typename_type
11053 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11054 return cp_parser_make_typename_type (parser, parser->scope,
11056 /* Look up a qualified name in the usual way. */
11060 tree ambiguous_decls;
11062 decl = cp_parser_lookup_name (parser, identifier,
11064 /*is_template=*/false,
11065 /*is_namespace=*/false,
11066 /*check_dependency=*/true,
11069 /* If the lookup was ambiguous, an error will already have been
11071 if (ambiguous_decls)
11072 return error_mark_node;
11074 /* If we are parsing friend declaration, DECL may be a
11075 TEMPLATE_DECL tree node here. However, we need to check
11076 whether this TEMPLATE_DECL results in valid code. Consider
11077 the following example:
11080 template <class T> class C {};
11083 template <class T> friend class N::C; // #1, valid code
11085 template <class T> class Y {
11086 friend class N::C; // #2, invalid code
11089 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11090 name lookup of `N::C'. We see that friend declaration must
11091 be template for the code to be valid. Note that
11092 processing_template_decl does not work here since it is
11093 always 1 for the above two cases. */
11095 decl = (cp_parser_maybe_treat_template_as_class
11096 (decl, /*tag_name_p=*/is_friend
11097 && parser->num_template_parameter_lists));
11099 if (TREE_CODE (decl) != TYPE_DECL)
11101 cp_parser_diagnose_invalid_type_name (parser,
11104 return error_mark_node;
11107 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11109 bool allow_template = (parser->num_template_parameter_lists
11110 || DECL_SELF_REFERENCE_P (decl));
11111 type = check_elaborated_type_specifier (tag_type, decl,
11114 if (type == error_mark_node)
11115 return error_mark_node;
11118 /* Forward declarations of nested types, such as
11123 are invalid unless all components preceding the final '::'
11124 are complete. If all enclosing types are complete, these
11125 declarations become merely pointless.
11127 Invalid forward declarations of nested types are errors
11128 caught elsewhere in parsing. Those that are pointless arrive
11131 if (cp_parser_declares_only_class_p (parser)
11132 && !is_friend && !processing_explicit_instantiation)
11133 warning (0, "declaration %qD does not declare anything", decl);
11135 type = TREE_TYPE (decl);
11139 /* An elaborated-type-specifier sometimes introduces a new type and
11140 sometimes names an existing type. Normally, the rule is that it
11141 introduces a new type only if there is not an existing type of
11142 the same name already in scope. For example, given:
11145 void f() { struct S s; }
11147 the `struct S' in the body of `f' is the same `struct S' as in
11148 the global scope; the existing definition is used. However, if
11149 there were no global declaration, this would introduce a new
11150 local class named `S'.
11152 An exception to this rule applies to the following code:
11154 namespace N { struct S; }
11156 Here, the elaborated-type-specifier names a new type
11157 unconditionally; even if there is already an `S' in the
11158 containing scope this declaration names a new type.
11159 This exception only applies if the elaborated-type-specifier
11160 forms the complete declaration:
11164 A declaration consisting solely of `class-key identifier ;' is
11165 either a redeclaration of the name in the current scope or a
11166 forward declaration of the identifier as a class name. It
11167 introduces the name into the current scope.
11169 We are in this situation precisely when the next token is a `;'.
11171 An exception to the exception is that a `friend' declaration does
11172 *not* name a new type; i.e., given:
11174 struct S { friend struct T; };
11176 `T' is not a new type in the scope of `S'.
11178 Also, `new struct S' or `sizeof (struct S)' never results in the
11179 definition of a new type; a new type can only be declared in a
11180 declaration context. */
11186 /* Friends have special name lookup rules. */
11187 ts = ts_within_enclosing_non_class;
11188 else if (is_declaration
11189 && cp_lexer_next_token_is (parser->lexer,
11191 /* This is a `class-key identifier ;' */
11197 (parser->num_template_parameter_lists
11198 && (cp_parser_next_token_starts_class_definition_p (parser)
11199 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11200 /* An unqualified name was used to reference this type, so
11201 there were no qualifying templates. */
11202 if (!cp_parser_check_template_parameters (parser,
11203 /*num_templates=*/0))
11204 return error_mark_node;
11205 type = xref_tag (tag_type, identifier, ts, template_p);
11209 if (type == error_mark_node)
11210 return error_mark_node;
11212 /* Allow attributes on forward declarations of classes. */
11215 if (TREE_CODE (type) == TYPENAME_TYPE)
11216 warning (OPT_Wattributes,
11217 "attributes ignored on uninstantiated type");
11218 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11219 && ! processing_explicit_instantiation)
11220 warning (OPT_Wattributes,
11221 "attributes ignored on template instantiation");
11222 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11223 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11225 warning (OPT_Wattributes,
11226 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11229 if (tag_type != enum_type)
11230 cp_parser_check_class_key (tag_type, type);
11232 /* A "<" cannot follow an elaborated type specifier. If that
11233 happens, the user was probably trying to form a template-id. */
11234 cp_parser_check_for_invalid_template_id (parser, type);
11239 /* Parse an enum-specifier.
11242 enum identifier [opt] { enumerator-list [opt] }
11245 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11248 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11249 if the token stream isn't an enum-specifier after all. */
11252 cp_parser_enum_specifier (cp_parser* parser)
11258 /* Parse tentatively so that we can back up if we don't find a
11260 cp_parser_parse_tentatively (parser);
11262 /* Caller guarantees that the current token is 'enum', an identifier
11263 possibly follows, and the token after that is an opening brace.
11264 If we don't have an identifier, fabricate an anonymous name for
11265 the enumeration being defined. */
11266 cp_lexer_consume_token (parser->lexer);
11268 attributes = cp_parser_attributes_opt (parser);
11270 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11271 identifier = cp_parser_identifier (parser);
11273 identifier = make_anon_name ();
11275 /* Look for the `{' but don't consume it yet. */
11276 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11277 cp_parser_simulate_error (parser);
11279 if (!cp_parser_parse_definitely (parser))
11282 /* Issue an error message if type-definitions are forbidden here. */
11283 if (!cp_parser_check_type_definition (parser))
11284 type = error_mark_node;
11286 /* Create the new type. We do this before consuming the opening
11287 brace so the enum will be recorded as being on the line of its
11288 tag (or the 'enum' keyword, if there is no tag). */
11289 type = start_enum (identifier);
11291 /* Consume the opening brace. */
11292 cp_lexer_consume_token (parser->lexer);
11294 if (type == error_mark_node)
11296 cp_parser_skip_to_end_of_block_or_statement (parser);
11297 return error_mark_node;
11300 /* If the next token is not '}', then there are some enumerators. */
11301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11302 cp_parser_enumerator_list (parser, type);
11304 /* Consume the final '}'. */
11305 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11307 /* Look for trailing attributes to apply to this enumeration, and
11308 apply them if appropriate. */
11309 if (cp_parser_allow_gnu_extensions_p (parser))
11311 tree trailing_attr = cp_parser_attributes_opt (parser);
11312 cplus_decl_attributes (&type,
11314 (int) ATTR_FLAG_TYPE_IN_PLACE);
11317 /* Finish up the enumeration. */
11318 finish_enum (type);
11323 /* Parse an enumerator-list. The enumerators all have the indicated
11327 enumerator-definition
11328 enumerator-list , enumerator-definition */
11331 cp_parser_enumerator_list (cp_parser* parser, tree type)
11335 /* Parse an enumerator-definition. */
11336 cp_parser_enumerator_definition (parser, type);
11338 /* If the next token is not a ',', we've reached the end of
11340 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11342 /* Otherwise, consume the `,' and keep going. */
11343 cp_lexer_consume_token (parser->lexer);
11344 /* If the next token is a `}', there is a trailing comma. */
11345 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11347 if (pedantic && !in_system_header)
11348 pedwarn ("comma at end of enumerator list");
11354 /* Parse an enumerator-definition. The enumerator has the indicated
11357 enumerator-definition:
11359 enumerator = constant-expression
11365 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11370 /* Look for the identifier. */
11371 identifier = cp_parser_identifier (parser);
11372 if (identifier == error_mark_node)
11375 /* If the next token is an '=', then there is an explicit value. */
11376 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11378 /* Consume the `=' token. */
11379 cp_lexer_consume_token (parser->lexer);
11380 /* Parse the value. */
11381 value = cp_parser_constant_expression (parser,
11382 /*allow_non_constant_p=*/false,
11388 /* Create the enumerator. */
11389 build_enumerator (identifier, value, type);
11392 /* Parse a namespace-name.
11395 original-namespace-name
11398 Returns the NAMESPACE_DECL for the namespace. */
11401 cp_parser_namespace_name (cp_parser* parser)
11404 tree namespace_decl;
11406 /* Get the name of the namespace. */
11407 identifier = cp_parser_identifier (parser);
11408 if (identifier == error_mark_node)
11409 return error_mark_node;
11411 /* Look up the identifier in the currently active scope. Look only
11412 for namespaces, due to:
11414 [basic.lookup.udir]
11416 When looking up a namespace-name in a using-directive or alias
11417 definition, only namespace names are considered.
11421 [basic.lookup.qual]
11423 During the lookup of a name preceding the :: scope resolution
11424 operator, object, function, and enumerator names are ignored.
11426 (Note that cp_parser_class_or_namespace_name only calls this
11427 function if the token after the name is the scope resolution
11429 namespace_decl = cp_parser_lookup_name (parser, identifier,
11431 /*is_template=*/false,
11432 /*is_namespace=*/true,
11433 /*check_dependency=*/true,
11434 /*ambiguous_decls=*/NULL);
11435 /* If it's not a namespace, issue an error. */
11436 if (namespace_decl == error_mark_node
11437 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11439 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11440 error ("%qD is not a namespace-name", identifier);
11441 cp_parser_error (parser, "expected namespace-name");
11442 namespace_decl = error_mark_node;
11445 return namespace_decl;
11448 /* Parse a namespace-definition.
11450 namespace-definition:
11451 named-namespace-definition
11452 unnamed-namespace-definition
11454 named-namespace-definition:
11455 original-namespace-definition
11456 extension-namespace-definition
11458 original-namespace-definition:
11459 namespace identifier { namespace-body }
11461 extension-namespace-definition:
11462 namespace original-namespace-name { namespace-body }
11464 unnamed-namespace-definition:
11465 namespace { namespace-body } */
11468 cp_parser_namespace_definition (cp_parser* parser)
11470 tree identifier, attribs;
11472 /* Look for the `namespace' keyword. */
11473 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11475 /* Get the name of the namespace. We do not attempt to distinguish
11476 between an original-namespace-definition and an
11477 extension-namespace-definition at this point. The semantic
11478 analysis routines are responsible for that. */
11479 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11480 identifier = cp_parser_identifier (parser);
11482 identifier = NULL_TREE;
11484 /* Parse any specified attributes. */
11485 attribs = cp_parser_attributes_opt (parser);
11487 /* Look for the `{' to start the namespace. */
11488 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11489 /* Start the namespace. */
11490 push_namespace_with_attribs (identifier, attribs);
11491 /* Parse the body of the namespace. */
11492 cp_parser_namespace_body (parser);
11493 /* Finish the namespace. */
11495 /* Look for the final `}'. */
11496 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11499 /* Parse a namespace-body.
11502 declaration-seq [opt] */
11505 cp_parser_namespace_body (cp_parser* parser)
11507 cp_parser_declaration_seq_opt (parser);
11510 /* Parse a namespace-alias-definition.
11512 namespace-alias-definition:
11513 namespace identifier = qualified-namespace-specifier ; */
11516 cp_parser_namespace_alias_definition (cp_parser* parser)
11519 tree namespace_specifier;
11521 /* Look for the `namespace' keyword. */
11522 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11523 /* Look for the identifier. */
11524 identifier = cp_parser_identifier (parser);
11525 if (identifier == error_mark_node)
11527 /* Look for the `=' token. */
11528 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11529 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11531 error ("%<namespace%> definition is not allowed here");
11532 /* Skip the definition. */
11533 cp_lexer_consume_token (parser->lexer);
11534 if (cp_parser_skip_to_closing_brace (parser))
11535 cp_lexer_consume_token (parser->lexer);
11538 cp_parser_require (parser, CPP_EQ, "`='");
11539 /* Look for the qualified-namespace-specifier. */
11540 namespace_specifier
11541 = cp_parser_qualified_namespace_specifier (parser);
11542 /* Look for the `;' token. */
11543 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11545 /* Register the alias in the symbol table. */
11546 do_namespace_alias (identifier, namespace_specifier);
11549 /* Parse a qualified-namespace-specifier.
11551 qualified-namespace-specifier:
11552 :: [opt] nested-name-specifier [opt] namespace-name
11554 Returns a NAMESPACE_DECL corresponding to the specified
11558 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11560 /* Look for the optional `::'. */
11561 cp_parser_global_scope_opt (parser,
11562 /*current_scope_valid_p=*/false);
11564 /* Look for the optional nested-name-specifier. */
11565 cp_parser_nested_name_specifier_opt (parser,
11566 /*typename_keyword_p=*/false,
11567 /*check_dependency_p=*/true,
11569 /*is_declaration=*/true);
11571 return cp_parser_namespace_name (parser);
11574 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11575 access declaration.
11578 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11579 using :: unqualified-id ;
11581 access-declaration:
11587 cp_parser_using_declaration (cp_parser* parser,
11588 bool access_declaration_p)
11591 bool typename_p = false;
11592 bool global_scope_p;
11597 if (access_declaration_p)
11598 cp_parser_parse_tentatively (parser);
11601 /* Look for the `using' keyword. */
11602 cp_parser_require_keyword (parser, RID_USING, "`using'");
11604 /* Peek at the next token. */
11605 token = cp_lexer_peek_token (parser->lexer);
11606 /* See if it's `typename'. */
11607 if (token->keyword == RID_TYPENAME)
11609 /* Remember that we've seen it. */
11611 /* Consume the `typename' token. */
11612 cp_lexer_consume_token (parser->lexer);
11616 /* Look for the optional global scope qualification. */
11618 = (cp_parser_global_scope_opt (parser,
11619 /*current_scope_valid_p=*/false)
11622 /* If we saw `typename', or didn't see `::', then there must be a
11623 nested-name-specifier present. */
11624 if (typename_p || !global_scope_p)
11625 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11626 /*check_dependency_p=*/true,
11628 /*is_declaration=*/true);
11629 /* Otherwise, we could be in either of the two productions. In that
11630 case, treat the nested-name-specifier as optional. */
11632 qscope = cp_parser_nested_name_specifier_opt (parser,
11633 /*typename_keyword_p=*/false,
11634 /*check_dependency_p=*/true,
11636 /*is_declaration=*/true);
11638 qscope = global_namespace;
11640 if (access_declaration_p && cp_parser_error_occurred (parser))
11641 /* Something has already gone wrong; there's no need to parse
11642 further. Since an error has occurred, the return value of
11643 cp_parser_parse_definitely will be false, as required. */
11644 return cp_parser_parse_definitely (parser);
11646 /* Parse the unqualified-id. */
11647 identifier = cp_parser_unqualified_id (parser,
11648 /*template_keyword_p=*/false,
11649 /*check_dependency_p=*/true,
11650 /*declarator_p=*/true,
11651 /*optional_p=*/false);
11653 if (access_declaration_p)
11655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11656 cp_parser_simulate_error (parser);
11657 if (!cp_parser_parse_definitely (parser))
11661 /* The function we call to handle a using-declaration is different
11662 depending on what scope we are in. */
11663 if (qscope == error_mark_node || identifier == error_mark_node)
11665 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11666 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11667 /* [namespace.udecl]
11669 A using declaration shall not name a template-id. */
11670 error ("a template-id may not appear in a using-declaration");
11673 if (at_class_scope_p ())
11675 /* Create the USING_DECL. */
11676 decl = do_class_using_decl (parser->scope, identifier);
11677 /* Add it to the list of members in this class. */
11678 finish_member_declaration (decl);
11682 decl = cp_parser_lookup_name_simple (parser, identifier);
11683 if (decl == error_mark_node)
11684 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11685 else if (!at_namespace_scope_p ())
11686 do_local_using_decl (decl, qscope, identifier);
11688 do_toplevel_using_decl (decl, qscope, identifier);
11692 /* Look for the final `;'. */
11693 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11698 /* Parse a using-directive.
11701 using namespace :: [opt] nested-name-specifier [opt]
11702 namespace-name ; */
11705 cp_parser_using_directive (cp_parser* parser)
11707 tree namespace_decl;
11710 /* Look for the `using' keyword. */
11711 cp_parser_require_keyword (parser, RID_USING, "`using'");
11712 /* And the `namespace' keyword. */
11713 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11714 /* Look for the optional `::' operator. */
11715 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11716 /* And the optional nested-name-specifier. */
11717 cp_parser_nested_name_specifier_opt (parser,
11718 /*typename_keyword_p=*/false,
11719 /*check_dependency_p=*/true,
11721 /*is_declaration=*/true);
11722 /* Get the namespace being used. */
11723 namespace_decl = cp_parser_namespace_name (parser);
11724 /* And any specified attributes. */
11725 attribs = cp_parser_attributes_opt (parser);
11726 /* Update the symbol table. */
11727 parse_using_directive (namespace_decl, attribs);
11728 /* Look for the final `;'. */
11729 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11732 /* Parse an asm-definition.
11735 asm ( string-literal ) ;
11740 asm volatile [opt] ( string-literal ) ;
11741 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11742 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11743 : asm-operand-list [opt] ) ;
11744 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11745 : asm-operand-list [opt]
11746 : asm-operand-list [opt] ) ; */
11749 cp_parser_asm_definition (cp_parser* parser)
11752 tree outputs = NULL_TREE;
11753 tree inputs = NULL_TREE;
11754 tree clobbers = NULL_TREE;
11756 bool volatile_p = false;
11757 bool extended_p = false;
11758 bool invalid_inputs_p = false;
11759 bool invalid_outputs_p = false;
11761 /* Look for the `asm' keyword. */
11762 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11763 /* See if the next token is `volatile'. */
11764 if (cp_parser_allow_gnu_extensions_p (parser)
11765 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11767 /* Remember that we saw the `volatile' keyword. */
11769 /* Consume the token. */
11770 cp_lexer_consume_token (parser->lexer);
11772 /* Look for the opening `('. */
11773 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11775 /* Look for the string. */
11776 string = cp_parser_string_literal (parser, false, false);
11777 if (string == error_mark_node)
11779 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11780 /*consume_paren=*/true);
11784 /* If we're allowing GNU extensions, check for the extended assembly
11785 syntax. Unfortunately, the `:' tokens need not be separated by
11786 a space in C, and so, for compatibility, we tolerate that here
11787 too. Doing that means that we have to treat the `::' operator as
11789 if (cp_parser_allow_gnu_extensions_p (parser)
11790 && parser->in_function_body
11791 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11792 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11794 bool inputs_p = false;
11795 bool clobbers_p = false;
11797 /* The extended syntax was used. */
11800 /* Look for outputs. */
11801 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11803 /* Consume the `:'. */
11804 cp_lexer_consume_token (parser->lexer);
11805 /* Parse the output-operands. */
11806 if (cp_lexer_next_token_is_not (parser->lexer,
11808 && cp_lexer_next_token_is_not (parser->lexer,
11810 && cp_lexer_next_token_is_not (parser->lexer,
11812 outputs = cp_parser_asm_operand_list (parser);
11814 if (outputs == error_mark_node)
11815 invalid_outputs_p = true;
11817 /* If the next token is `::', there are no outputs, and the
11818 next token is the beginning of the inputs. */
11819 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11820 /* The inputs are coming next. */
11823 /* Look for inputs. */
11825 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11827 /* Consume the `:' or `::'. */
11828 cp_lexer_consume_token (parser->lexer);
11829 /* Parse the output-operands. */
11830 if (cp_lexer_next_token_is_not (parser->lexer,
11832 && cp_lexer_next_token_is_not (parser->lexer,
11834 inputs = cp_parser_asm_operand_list (parser);
11836 if (inputs == error_mark_node)
11837 invalid_inputs_p = true;
11839 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11840 /* The clobbers are coming next. */
11843 /* Look for clobbers. */
11845 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11847 /* Consume the `:' or `::'. */
11848 cp_lexer_consume_token (parser->lexer);
11849 /* Parse the clobbers. */
11850 if (cp_lexer_next_token_is_not (parser->lexer,
11852 clobbers = cp_parser_asm_clobber_list (parser);
11855 /* Look for the closing `)'. */
11856 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11857 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11858 /*consume_paren=*/true);
11859 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11861 if (!invalid_inputs_p && !invalid_outputs_p)
11863 /* Create the ASM_EXPR. */
11864 if (parser->in_function_body)
11866 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11868 /* If the extended syntax was not used, mark the ASM_EXPR. */
11871 tree temp = asm_stmt;
11872 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11873 temp = TREE_OPERAND (temp, 0);
11875 ASM_INPUT_P (temp) = 1;
11879 cgraph_add_asm_node (string);
11883 /* Declarators [gram.dcl.decl] */
11885 /* Parse an init-declarator.
11888 declarator initializer [opt]
11893 declarator asm-specification [opt] attributes [opt] initializer [opt]
11895 function-definition:
11896 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11898 decl-specifier-seq [opt] declarator function-try-block
11902 function-definition:
11903 __extension__ function-definition
11905 The DECL_SPECIFIERS apply to this declarator. Returns a
11906 representation of the entity declared. If MEMBER_P is TRUE, then
11907 this declarator appears in a class scope. The new DECL created by
11908 this declarator is returned.
11910 The CHECKS are access checks that should be performed once we know
11911 what entity is being declared (and, therefore, what classes have
11914 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11915 for a function-definition here as well. If the declarator is a
11916 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11917 be TRUE upon return. By that point, the function-definition will
11918 have been completely parsed.
11920 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11924 cp_parser_init_declarator (cp_parser* parser,
11925 cp_decl_specifier_seq *decl_specifiers,
11926 VEC (deferred_access_check,gc)* checks,
11927 bool function_definition_allowed_p,
11929 int declares_class_or_enum,
11930 bool* function_definition_p)
11933 cp_declarator *declarator;
11934 tree prefix_attributes;
11936 tree asm_specification;
11938 tree decl = NULL_TREE;
11940 bool is_initialized;
11941 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11942 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11944 enum cpp_ttype initialization_kind;
11945 bool is_parenthesized_init = false;
11946 bool is_non_constant_init;
11947 int ctor_dtor_or_conv_p;
11949 tree pushed_scope = NULL;
11951 /* Gather the attributes that were provided with the
11952 decl-specifiers. */
11953 prefix_attributes = decl_specifiers->attributes;
11955 /* Assume that this is not the declarator for a function
11957 if (function_definition_p)
11958 *function_definition_p = false;
11960 /* Defer access checks while parsing the declarator; we cannot know
11961 what names are accessible until we know what is being
11963 resume_deferring_access_checks ();
11965 /* Parse the declarator. */
11967 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11968 &ctor_dtor_or_conv_p,
11969 /*parenthesized_p=*/NULL,
11970 /*member_p=*/false);
11971 /* Gather up the deferred checks. */
11972 stop_deferring_access_checks ();
11974 /* If the DECLARATOR was erroneous, there's no need to go
11976 if (declarator == cp_error_declarator)
11977 return error_mark_node;
11979 /* Check that the number of template-parameter-lists is OK. */
11980 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11981 return error_mark_node;
11983 if (declares_class_or_enum & 2)
11984 cp_parser_check_for_definition_in_return_type (declarator,
11985 decl_specifiers->type);
11987 /* Figure out what scope the entity declared by the DECLARATOR is
11988 located in. `grokdeclarator' sometimes changes the scope, so
11989 we compute it now. */
11990 scope = get_scope_of_declarator (declarator);
11992 /* If we're allowing GNU extensions, look for an asm-specification
11994 if (cp_parser_allow_gnu_extensions_p (parser))
11996 /* Look for an asm-specification. */
11997 asm_specification = cp_parser_asm_specification_opt (parser);
11998 /* And attributes. */
11999 attributes = cp_parser_attributes_opt (parser);
12003 asm_specification = NULL_TREE;
12004 attributes = NULL_TREE;
12007 /* Peek at the next token. */
12008 token = cp_lexer_peek_token (parser->lexer);
12009 /* Check to see if the token indicates the start of a
12010 function-definition. */
12011 if (cp_parser_token_starts_function_definition_p (token))
12013 if (!function_definition_allowed_p)
12015 /* If a function-definition should not appear here, issue an
12017 cp_parser_error (parser,
12018 "a function-definition is not allowed here");
12019 return error_mark_node;
12023 /* Neither attributes nor an asm-specification are allowed
12024 on a function-definition. */
12025 if (asm_specification)
12026 error ("an asm-specification is not allowed on a function-definition");
12028 error ("attributes are not allowed on a function-definition");
12029 /* This is a function-definition. */
12030 *function_definition_p = true;
12032 /* Parse the function definition. */
12034 decl = cp_parser_save_member_function_body (parser,
12037 prefix_attributes);
12040 = (cp_parser_function_definition_from_specifiers_and_declarator
12041 (parser, decl_specifiers, prefix_attributes, declarator));
12049 Only in function declarations for constructors, destructors, and
12050 type conversions can the decl-specifier-seq be omitted.
12052 We explicitly postpone this check past the point where we handle
12053 function-definitions because we tolerate function-definitions
12054 that are missing their return types in some modes. */
12055 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12057 cp_parser_error (parser,
12058 "expected constructor, destructor, or type conversion");
12059 return error_mark_node;
12062 /* An `=' or an `(' indicates an initializer. */
12063 if (token->type == CPP_EQ
12064 || token->type == CPP_OPEN_PAREN)
12066 is_initialized = true;
12067 initialization_kind = token->type;
12071 /* If the init-declarator isn't initialized and isn't followed by a
12072 `,' or `;', it's not a valid init-declarator. */
12073 if (token->type != CPP_COMMA
12074 && token->type != CPP_SEMICOLON)
12076 cp_parser_error (parser, "expected initializer");
12077 return error_mark_node;
12079 is_initialized = false;
12080 initialization_kind = CPP_EOF;
12083 /* Because start_decl has side-effects, we should only call it if we
12084 know we're going ahead. By this point, we know that we cannot
12085 possibly be looking at any other construct. */
12086 cp_parser_commit_to_tentative_parse (parser);
12088 /* If the decl specifiers were bad, issue an error now that we're
12089 sure this was intended to be a declarator. Then continue
12090 declaring the variable(s), as int, to try to cut down on further
12092 if (decl_specifiers->any_specifiers_p
12093 && decl_specifiers->type == error_mark_node)
12095 cp_parser_error (parser, "invalid type in declaration");
12096 decl_specifiers->type = integer_type_node;
12099 /* Check to see whether or not this declaration is a friend. */
12100 friend_p = cp_parser_friend_p (decl_specifiers);
12102 /* Enter the newly declared entry in the symbol table. If we're
12103 processing a declaration in a class-specifier, we wait until
12104 after processing the initializer. */
12107 if (parser->in_unbraced_linkage_specification_p)
12108 decl_specifiers->storage_class = sc_extern;
12109 decl = start_decl (declarator, decl_specifiers,
12110 is_initialized, attributes, prefix_attributes,
12114 /* Enter the SCOPE. That way unqualified names appearing in the
12115 initializer will be looked up in SCOPE. */
12116 pushed_scope = push_scope (scope);
12118 /* Perform deferred access control checks, now that we know in which
12119 SCOPE the declared entity resides. */
12120 if (!member_p && decl)
12122 tree saved_current_function_decl = NULL_TREE;
12124 /* If the entity being declared is a function, pretend that we
12125 are in its scope. If it is a `friend', it may have access to
12126 things that would not otherwise be accessible. */
12127 if (TREE_CODE (decl) == FUNCTION_DECL)
12129 saved_current_function_decl = current_function_decl;
12130 current_function_decl = decl;
12133 /* Perform access checks for template parameters. */
12134 cp_parser_perform_template_parameter_access_checks (checks);
12136 /* Perform the access control checks for the declarator and the
12137 the decl-specifiers. */
12138 perform_deferred_access_checks ();
12140 /* Restore the saved value. */
12141 if (TREE_CODE (decl) == FUNCTION_DECL)
12142 current_function_decl = saved_current_function_decl;
12145 /* Parse the initializer. */
12146 initializer = NULL_TREE;
12147 is_parenthesized_init = false;
12148 is_non_constant_init = true;
12149 if (is_initialized)
12151 if (function_declarator_p (declarator))
12153 if (initialization_kind == CPP_EQ)
12154 initializer = cp_parser_pure_specifier (parser);
12157 /* If the declaration was erroneous, we don't really
12158 know what the user intended, so just silently
12159 consume the initializer. */
12160 if (decl != error_mark_node)
12161 error ("initializer provided for function");
12162 cp_parser_skip_to_closing_parenthesis (parser,
12163 /*recovering=*/true,
12164 /*or_comma=*/false,
12165 /*consume_paren=*/true);
12169 initializer = cp_parser_initializer (parser,
12170 &is_parenthesized_init,
12171 &is_non_constant_init);
12174 /* The old parser allows attributes to appear after a parenthesized
12175 initializer. Mark Mitchell proposed removing this functionality
12176 on the GCC mailing lists on 2002-08-13. This parser accepts the
12177 attributes -- but ignores them. */
12178 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12179 if (cp_parser_attributes_opt (parser))
12180 warning (OPT_Wattributes,
12181 "attributes after parenthesized initializer ignored");
12183 /* For an in-class declaration, use `grokfield' to create the
12189 pop_scope (pushed_scope);
12190 pushed_scope = false;
12192 decl = grokfield (declarator, decl_specifiers,
12193 initializer, !is_non_constant_init,
12194 /*asmspec=*/NULL_TREE,
12195 prefix_attributes);
12196 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12197 cp_parser_save_default_args (parser, decl);
12200 /* Finish processing the declaration. But, skip friend
12202 if (!friend_p && decl && decl != error_mark_node)
12204 cp_finish_decl (decl,
12205 initializer, !is_non_constant_init,
12207 /* If the initializer is in parentheses, then this is
12208 a direct-initialization, which means that an
12209 `explicit' constructor is OK. Otherwise, an
12210 `explicit' constructor cannot be used. */
12211 ((is_parenthesized_init || !is_initialized)
12212 ? 0 : LOOKUP_ONLYCONVERTING));
12214 else if ((cxx_dialect != cxx98) && friend_p
12215 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12216 /* Core issue #226 (C++0x only): A default template-argument
12217 shall not be specified in a friend class template
12219 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12220 /*is_partial=*/0, /*is_friend_decl=*/1);
12222 if (!friend_p && pushed_scope)
12223 pop_scope (pushed_scope);
12228 /* Parse a declarator.
12232 ptr-operator declarator
12234 abstract-declarator:
12235 ptr-operator abstract-declarator [opt]
12236 direct-abstract-declarator
12241 attributes [opt] direct-declarator
12242 attributes [opt] ptr-operator declarator
12244 abstract-declarator:
12245 attributes [opt] ptr-operator abstract-declarator [opt]
12246 attributes [opt] direct-abstract-declarator
12248 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12249 detect constructor, destructor or conversion operators. It is set
12250 to -1 if the declarator is a name, and +1 if it is a
12251 function. Otherwise it is set to zero. Usually you just want to
12252 test for >0, but internally the negative value is used.
12254 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12255 a decl-specifier-seq unless it declares a constructor, destructor,
12256 or conversion. It might seem that we could check this condition in
12257 semantic analysis, rather than parsing, but that makes it difficult
12258 to handle something like `f()'. We want to notice that there are
12259 no decl-specifiers, and therefore realize that this is an
12260 expression, not a declaration.)
12262 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12263 the declarator is a direct-declarator of the form "(...)".
12265 MEMBER_P is true iff this declarator is a member-declarator. */
12267 static cp_declarator *
12268 cp_parser_declarator (cp_parser* parser,
12269 cp_parser_declarator_kind dcl_kind,
12270 int* ctor_dtor_or_conv_p,
12271 bool* parenthesized_p,
12275 cp_declarator *declarator;
12276 enum tree_code code;
12277 cp_cv_quals cv_quals;
12279 tree attributes = NULL_TREE;
12281 /* Assume this is not a constructor, destructor, or type-conversion
12283 if (ctor_dtor_or_conv_p)
12284 *ctor_dtor_or_conv_p = 0;
12286 if (cp_parser_allow_gnu_extensions_p (parser))
12287 attributes = cp_parser_attributes_opt (parser);
12289 /* Peek at the next token. */
12290 token = cp_lexer_peek_token (parser->lexer);
12292 /* Check for the ptr-operator production. */
12293 cp_parser_parse_tentatively (parser);
12294 /* Parse the ptr-operator. */
12295 code = cp_parser_ptr_operator (parser,
12298 /* If that worked, then we have a ptr-operator. */
12299 if (cp_parser_parse_definitely (parser))
12301 /* If a ptr-operator was found, then this declarator was not
12303 if (parenthesized_p)
12304 *parenthesized_p = true;
12305 /* The dependent declarator is optional if we are parsing an
12306 abstract-declarator. */
12307 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12308 cp_parser_parse_tentatively (parser);
12310 /* Parse the dependent declarator. */
12311 declarator = cp_parser_declarator (parser, dcl_kind,
12312 /*ctor_dtor_or_conv_p=*/NULL,
12313 /*parenthesized_p=*/NULL,
12314 /*member_p=*/false);
12316 /* If we are parsing an abstract-declarator, we must handle the
12317 case where the dependent declarator is absent. */
12318 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12319 && !cp_parser_parse_definitely (parser))
12322 declarator = cp_parser_make_indirect_declarator
12323 (code, class_type, cv_quals, declarator);
12325 /* Everything else is a direct-declarator. */
12328 if (parenthesized_p)
12329 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12331 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12332 ctor_dtor_or_conv_p,
12336 if (attributes && declarator && declarator != cp_error_declarator)
12337 declarator->attributes = attributes;
12342 /* Parse a direct-declarator or direct-abstract-declarator.
12346 direct-declarator ( parameter-declaration-clause )
12347 cv-qualifier-seq [opt]
12348 exception-specification [opt]
12349 direct-declarator [ constant-expression [opt] ]
12352 direct-abstract-declarator:
12353 direct-abstract-declarator [opt]
12354 ( parameter-declaration-clause )
12355 cv-qualifier-seq [opt]
12356 exception-specification [opt]
12357 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12358 ( abstract-declarator )
12360 Returns a representation of the declarator. DCL_KIND is
12361 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12362 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12363 we are parsing a direct-declarator. It is
12364 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12365 of ambiguity we prefer an abstract declarator, as per
12366 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12367 cp_parser_declarator. */
12369 static cp_declarator *
12370 cp_parser_direct_declarator (cp_parser* parser,
12371 cp_parser_declarator_kind dcl_kind,
12372 int* ctor_dtor_or_conv_p,
12376 cp_declarator *declarator = NULL;
12377 tree scope = NULL_TREE;
12378 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12379 bool saved_in_declarator_p = parser->in_declarator_p;
12381 tree pushed_scope = NULL_TREE;
12385 /* Peek at the next token. */
12386 token = cp_lexer_peek_token (parser->lexer);
12387 if (token->type == CPP_OPEN_PAREN)
12389 /* This is either a parameter-declaration-clause, or a
12390 parenthesized declarator. When we know we are parsing a
12391 named declarator, it must be a parenthesized declarator
12392 if FIRST is true. For instance, `(int)' is a
12393 parameter-declaration-clause, with an omitted
12394 direct-abstract-declarator. But `((*))', is a
12395 parenthesized abstract declarator. Finally, when T is a
12396 template parameter `(T)' is a
12397 parameter-declaration-clause, and not a parenthesized
12400 We first try and parse a parameter-declaration-clause,
12401 and then try a nested declarator (if FIRST is true).
12403 It is not an error for it not to be a
12404 parameter-declaration-clause, even when FIRST is
12410 The first is the declaration of a function while the
12411 second is a the definition of a variable, including its
12414 Having seen only the parenthesis, we cannot know which of
12415 these two alternatives should be selected. Even more
12416 complex are examples like:
12421 The former is a function-declaration; the latter is a
12422 variable initialization.
12424 Thus again, we try a parameter-declaration-clause, and if
12425 that fails, we back out and return. */
12427 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12429 cp_parameter_declarator *params;
12430 unsigned saved_num_template_parameter_lists;
12432 /* In a member-declarator, the only valid interpretation
12433 of a parenthesis is the start of a
12434 parameter-declaration-clause. (It is invalid to
12435 initialize a static data member with a parenthesized
12436 initializer; only the "=" form of initialization is
12439 cp_parser_parse_tentatively (parser);
12441 /* Consume the `('. */
12442 cp_lexer_consume_token (parser->lexer);
12445 /* If this is going to be an abstract declarator, we're
12446 in a declarator and we can't have default args. */
12447 parser->default_arg_ok_p = false;
12448 parser->in_declarator_p = true;
12451 /* Inside the function parameter list, surrounding
12452 template-parameter-lists do not apply. */
12453 saved_num_template_parameter_lists
12454 = parser->num_template_parameter_lists;
12455 parser->num_template_parameter_lists = 0;
12457 /* Parse the parameter-declaration-clause. */
12458 params = cp_parser_parameter_declaration_clause (parser);
12460 parser->num_template_parameter_lists
12461 = saved_num_template_parameter_lists;
12463 /* If all went well, parse the cv-qualifier-seq and the
12464 exception-specification. */
12465 if (member_p || cp_parser_parse_definitely (parser))
12467 cp_cv_quals cv_quals;
12468 tree exception_specification;
12470 if (ctor_dtor_or_conv_p)
12471 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12473 /* Consume the `)'. */
12474 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12476 /* Parse the cv-qualifier-seq. */
12477 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12478 /* And the exception-specification. */
12479 exception_specification
12480 = cp_parser_exception_specification_opt (parser);
12482 /* Create the function-declarator. */
12483 declarator = make_call_declarator (declarator,
12486 exception_specification);
12487 /* Any subsequent parameter lists are to do with
12488 return type, so are not those of the declared
12490 parser->default_arg_ok_p = false;
12492 /* Repeat the main loop. */
12497 /* If this is the first, we can try a parenthesized
12501 bool saved_in_type_id_in_expr_p;
12503 parser->default_arg_ok_p = saved_default_arg_ok_p;
12504 parser->in_declarator_p = saved_in_declarator_p;
12506 /* Consume the `('. */
12507 cp_lexer_consume_token (parser->lexer);
12508 /* Parse the nested declarator. */
12509 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12510 parser->in_type_id_in_expr_p = true;
12512 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12513 /*parenthesized_p=*/NULL,
12515 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12517 /* Expect a `)'. */
12518 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12519 declarator = cp_error_declarator;
12520 if (declarator == cp_error_declarator)
12523 goto handle_declarator;
12525 /* Otherwise, we must be done. */
12529 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12530 && token->type == CPP_OPEN_SQUARE)
12532 /* Parse an array-declarator. */
12535 if (ctor_dtor_or_conv_p)
12536 *ctor_dtor_or_conv_p = 0;
12539 parser->default_arg_ok_p = false;
12540 parser->in_declarator_p = true;
12541 /* Consume the `['. */
12542 cp_lexer_consume_token (parser->lexer);
12543 /* Peek at the next token. */
12544 token = cp_lexer_peek_token (parser->lexer);
12545 /* If the next token is `]', then there is no
12546 constant-expression. */
12547 if (token->type != CPP_CLOSE_SQUARE)
12549 bool non_constant_p;
12552 = cp_parser_constant_expression (parser,
12553 /*allow_non_constant=*/true,
12555 if (!non_constant_p)
12556 bounds = fold_non_dependent_expr (bounds);
12557 /* Normally, the array bound must be an integral constant
12558 expression. However, as an extension, we allow VLAs
12559 in function scopes. */
12560 else if (!parser->in_function_body)
12562 error ("array bound is not an integer constant");
12563 bounds = error_mark_node;
12567 bounds = NULL_TREE;
12568 /* Look for the closing `]'. */
12569 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12571 declarator = cp_error_declarator;
12575 declarator = make_array_declarator (declarator, bounds);
12577 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12579 tree qualifying_scope;
12580 tree unqualified_name;
12581 special_function_kind sfk;
12583 bool pack_expansion_p = false;
12585 /* Parse a declarator-id */
12586 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12589 cp_parser_parse_tentatively (parser);
12591 /* If we see an ellipsis, we should be looking at a
12593 if (token->type == CPP_ELLIPSIS)
12595 /* Consume the `...' */
12596 cp_lexer_consume_token (parser->lexer);
12598 pack_expansion_p = true;
12603 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12604 qualifying_scope = parser->scope;
12609 if (!unqualified_name && pack_expansion_p)
12611 /* Check whether an error occurred. */
12612 okay = !cp_parser_error_occurred (parser);
12614 /* We already consumed the ellipsis to mark a
12615 parameter pack, but we have no way to report it,
12616 so abort the tentative parse. We will be exiting
12617 immediately anyway. */
12618 cp_parser_abort_tentative_parse (parser);
12621 okay = cp_parser_parse_definitely (parser);
12624 unqualified_name = error_mark_node;
12625 else if (unqualified_name
12626 && (qualifying_scope
12627 || (TREE_CODE (unqualified_name)
12628 != IDENTIFIER_NODE)))
12630 cp_parser_error (parser, "expected unqualified-id");
12631 unqualified_name = error_mark_node;
12635 if (!unqualified_name)
12637 if (unqualified_name == error_mark_node)
12639 declarator = cp_error_declarator;
12640 pack_expansion_p = false;
12641 declarator->parameter_pack_p = false;
12645 if (qualifying_scope && at_namespace_scope_p ()
12646 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12648 /* In the declaration of a member of a template class
12649 outside of the class itself, the SCOPE will sometimes
12650 be a TYPENAME_TYPE. For example, given:
12652 template <typename T>
12653 int S<T>::R::i = 3;
12655 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12656 this context, we must resolve S<T>::R to an ordinary
12657 type, rather than a typename type.
12659 The reason we normally avoid resolving TYPENAME_TYPEs
12660 is that a specialization of `S' might render
12661 `S<T>::R' not a type. However, if `S' is
12662 specialized, then this `i' will not be used, so there
12663 is no harm in resolving the types here. */
12666 /* Resolve the TYPENAME_TYPE. */
12667 type = resolve_typename_type (qualifying_scope,
12668 /*only_current_p=*/false);
12669 /* If that failed, the declarator is invalid. */
12670 if (TREE_CODE (type) == TYPENAME_TYPE)
12671 error ("%<%T::%E%> is not a type",
12672 TYPE_CONTEXT (qualifying_scope),
12673 TYPE_IDENTIFIER (qualifying_scope));
12674 qualifying_scope = type;
12679 if (unqualified_name)
12683 if (qualifying_scope
12684 && CLASS_TYPE_P (qualifying_scope))
12685 class_type = qualifying_scope;
12687 class_type = current_class_type;
12689 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12691 tree name_type = TREE_TYPE (unqualified_name);
12692 if (class_type && same_type_p (name_type, class_type))
12694 if (qualifying_scope
12695 && CLASSTYPE_USE_TEMPLATE (name_type))
12697 error ("invalid use of constructor as a template");
12698 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12699 "name the constructor in a qualified name",
12701 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12702 class_type, name_type);
12703 declarator = cp_error_declarator;
12707 unqualified_name = constructor_name (class_type);
12711 /* We do not attempt to print the declarator
12712 here because we do not have enough
12713 information about its original syntactic
12715 cp_parser_error (parser, "invalid declarator");
12716 declarator = cp_error_declarator;
12723 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12724 sfk = sfk_destructor;
12725 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12726 sfk = sfk_conversion;
12727 else if (/* There's no way to declare a constructor
12728 for an anonymous type, even if the type
12729 got a name for linkage purposes. */
12730 !TYPE_WAS_ANONYMOUS (class_type)
12731 && constructor_name_p (unqualified_name,
12734 unqualified_name = constructor_name (class_type);
12735 sfk = sfk_constructor;
12738 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12739 *ctor_dtor_or_conv_p = -1;
12742 declarator = make_id_declarator (qualifying_scope,
12745 declarator->id_loc = token->location;
12746 declarator->parameter_pack_p = pack_expansion_p;
12748 if (pack_expansion_p)
12749 maybe_warn_variadic_templates ();
12751 handle_declarator:;
12752 scope = get_scope_of_declarator (declarator);
12754 /* Any names that appear after the declarator-id for a
12755 member are looked up in the containing scope. */
12756 pushed_scope = push_scope (scope);
12757 parser->in_declarator_p = true;
12758 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12759 || (declarator && declarator->kind == cdk_id))
12760 /* Default args are only allowed on function
12762 parser->default_arg_ok_p = saved_default_arg_ok_p;
12764 parser->default_arg_ok_p = false;
12773 /* For an abstract declarator, we might wind up with nothing at this
12774 point. That's an error; the declarator is not optional. */
12776 cp_parser_error (parser, "expected declarator");
12778 /* If we entered a scope, we must exit it now. */
12780 pop_scope (pushed_scope);
12782 parser->default_arg_ok_p = saved_default_arg_ok_p;
12783 parser->in_declarator_p = saved_in_declarator_p;
12788 /* Parse a ptr-operator.
12791 * cv-qualifier-seq [opt]
12793 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12798 & cv-qualifier-seq [opt]
12800 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12801 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12802 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12803 filled in with the TYPE containing the member. *CV_QUALS is
12804 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12805 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12806 Note that the tree codes returned by this function have nothing
12807 to do with the types of trees that will be eventually be created
12808 to represent the pointer or reference type being parsed. They are
12809 just constants with suggestive names. */
12810 static enum tree_code
12811 cp_parser_ptr_operator (cp_parser* parser,
12813 cp_cv_quals *cv_quals)
12815 enum tree_code code = ERROR_MARK;
12818 /* Assume that it's not a pointer-to-member. */
12820 /* And that there are no cv-qualifiers. */
12821 *cv_quals = TYPE_UNQUALIFIED;
12823 /* Peek at the next token. */
12824 token = cp_lexer_peek_token (parser->lexer);
12826 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12827 if (token->type == CPP_MULT)
12828 code = INDIRECT_REF;
12829 else if (token->type == CPP_AND)
12831 else if ((cxx_dialect != cxx98) &&
12832 token->type == CPP_AND_AND) /* C++0x only */
12833 code = NON_LVALUE_EXPR;
12835 if (code != ERROR_MARK)
12837 /* Consume the `*', `&' or `&&'. */
12838 cp_lexer_consume_token (parser->lexer);
12840 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12841 `&', if we are allowing GNU extensions. (The only qualifier
12842 that can legally appear after `&' is `restrict', but that is
12843 enforced during semantic analysis. */
12844 if (code == INDIRECT_REF
12845 || cp_parser_allow_gnu_extensions_p (parser))
12846 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12850 /* Try the pointer-to-member case. */
12851 cp_parser_parse_tentatively (parser);
12852 /* Look for the optional `::' operator. */
12853 cp_parser_global_scope_opt (parser,
12854 /*current_scope_valid_p=*/false);
12855 /* Look for the nested-name specifier. */
12856 cp_parser_nested_name_specifier (parser,
12857 /*typename_keyword_p=*/false,
12858 /*check_dependency_p=*/true,
12860 /*is_declaration=*/false);
12861 /* If we found it, and the next token is a `*', then we are
12862 indeed looking at a pointer-to-member operator. */
12863 if (!cp_parser_error_occurred (parser)
12864 && cp_parser_require (parser, CPP_MULT, "`*'"))
12866 /* Indicate that the `*' operator was used. */
12867 code = INDIRECT_REF;
12869 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12870 error ("%qD is a namespace", parser->scope);
12873 /* The type of which the member is a member is given by the
12875 *type = parser->scope;
12876 /* The next name will not be qualified. */
12877 parser->scope = NULL_TREE;
12878 parser->qualifying_scope = NULL_TREE;
12879 parser->object_scope = NULL_TREE;
12880 /* Look for the optional cv-qualifier-seq. */
12881 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12884 /* If that didn't work we don't have a ptr-operator. */
12885 if (!cp_parser_parse_definitely (parser))
12886 cp_parser_error (parser, "expected ptr-operator");
12892 /* Parse an (optional) cv-qualifier-seq.
12895 cv-qualifier cv-qualifier-seq [opt]
12906 Returns a bitmask representing the cv-qualifiers. */
12909 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12911 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12916 cp_cv_quals cv_qualifier;
12918 /* Peek at the next token. */
12919 token = cp_lexer_peek_token (parser->lexer);
12920 /* See if it's a cv-qualifier. */
12921 switch (token->keyword)
12924 cv_qualifier = TYPE_QUAL_CONST;
12928 cv_qualifier = TYPE_QUAL_VOLATILE;
12932 cv_qualifier = TYPE_QUAL_RESTRICT;
12936 cv_qualifier = TYPE_UNQUALIFIED;
12943 if (cv_quals & cv_qualifier)
12945 error ("duplicate cv-qualifier");
12946 cp_lexer_purge_token (parser->lexer);
12950 cp_lexer_consume_token (parser->lexer);
12951 cv_quals |= cv_qualifier;
12958 /* Parse a declarator-id.
12962 :: [opt] nested-name-specifier [opt] type-name
12964 In the `id-expression' case, the value returned is as for
12965 cp_parser_id_expression if the id-expression was an unqualified-id.
12966 If the id-expression was a qualified-id, then a SCOPE_REF is
12967 returned. The first operand is the scope (either a NAMESPACE_DECL
12968 or TREE_TYPE), but the second is still just a representation of an
12972 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12975 /* The expression must be an id-expression. Assume that qualified
12976 names are the names of types so that:
12979 int S<T>::R::i = 3;
12981 will work; we must treat `S<T>::R' as the name of a type.
12982 Similarly, assume that qualified names are templates, where
12986 int S<T>::R<T>::i = 3;
12989 id = cp_parser_id_expression (parser,
12990 /*template_keyword_p=*/false,
12991 /*check_dependency_p=*/false,
12992 /*template_p=*/NULL,
12993 /*declarator_p=*/true,
12995 if (id && BASELINK_P (id))
12996 id = BASELINK_FUNCTIONS (id);
13000 /* Parse a type-id.
13003 type-specifier-seq abstract-declarator [opt]
13005 Returns the TYPE specified. */
13008 cp_parser_type_id (cp_parser* parser)
13010 cp_decl_specifier_seq type_specifier_seq;
13011 cp_declarator *abstract_declarator;
13013 /* Parse the type-specifier-seq. */
13014 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13015 &type_specifier_seq);
13016 if (type_specifier_seq.type == error_mark_node)
13017 return error_mark_node;
13019 /* There might or might not be an abstract declarator. */
13020 cp_parser_parse_tentatively (parser);
13021 /* Look for the declarator. */
13022 abstract_declarator
13023 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13024 /*parenthesized_p=*/NULL,
13025 /*member_p=*/false);
13026 /* Check to see if there really was a declarator. */
13027 if (!cp_parser_parse_definitely (parser))
13028 abstract_declarator = NULL;
13030 return groktypename (&type_specifier_seq, abstract_declarator);
13033 /* Parse a type-specifier-seq.
13035 type-specifier-seq:
13036 type-specifier type-specifier-seq [opt]
13040 type-specifier-seq:
13041 attributes type-specifier-seq [opt]
13043 If IS_CONDITION is true, we are at the start of a "condition",
13044 e.g., we've just seen "if (".
13046 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13049 cp_parser_type_specifier_seq (cp_parser* parser,
13051 cp_decl_specifier_seq *type_specifier_seq)
13053 bool seen_type_specifier = false;
13054 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13056 /* Clear the TYPE_SPECIFIER_SEQ. */
13057 clear_decl_specs (type_specifier_seq);
13059 /* Parse the type-specifiers and attributes. */
13062 tree type_specifier;
13063 bool is_cv_qualifier;
13065 /* Check for attributes first. */
13066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13068 type_specifier_seq->attributes =
13069 chainon (type_specifier_seq->attributes,
13070 cp_parser_attributes_opt (parser));
13074 /* Look for the type-specifier. */
13075 type_specifier = cp_parser_type_specifier (parser,
13077 type_specifier_seq,
13078 /*is_declaration=*/false,
13081 if (!type_specifier)
13083 /* If the first type-specifier could not be found, this is not a
13084 type-specifier-seq at all. */
13085 if (!seen_type_specifier)
13087 cp_parser_error (parser, "expected type-specifier");
13088 type_specifier_seq->type = error_mark_node;
13091 /* If subsequent type-specifiers could not be found, the
13092 type-specifier-seq is complete. */
13096 seen_type_specifier = true;
13097 /* The standard says that a condition can be:
13099 type-specifier-seq declarator = assignment-expression
13106 we should treat the "S" as a declarator, not as a
13107 type-specifier. The standard doesn't say that explicitly for
13108 type-specifier-seq, but it does say that for
13109 decl-specifier-seq in an ordinary declaration. Perhaps it
13110 would be clearer just to allow a decl-specifier-seq here, and
13111 then add a semantic restriction that if any decl-specifiers
13112 that are not type-specifiers appear, the program is invalid. */
13113 if (is_condition && !is_cv_qualifier)
13114 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13117 cp_parser_check_decl_spec (type_specifier_seq);
13120 /* Parse a parameter-declaration-clause.
13122 parameter-declaration-clause:
13123 parameter-declaration-list [opt] ... [opt]
13124 parameter-declaration-list , ...
13126 Returns a representation for the parameter declarations. A return
13127 value of NULL indicates a parameter-declaration-clause consisting
13128 only of an ellipsis. */
13130 static cp_parameter_declarator *
13131 cp_parser_parameter_declaration_clause (cp_parser* parser)
13133 cp_parameter_declarator *parameters;
13138 /* Peek at the next token. */
13139 token = cp_lexer_peek_token (parser->lexer);
13140 /* Check for trivial parameter-declaration-clauses. */
13141 if (token->type == CPP_ELLIPSIS)
13143 /* Consume the `...' token. */
13144 cp_lexer_consume_token (parser->lexer);
13147 else if (token->type == CPP_CLOSE_PAREN)
13148 /* There are no parameters. */
13150 #ifndef NO_IMPLICIT_EXTERN_C
13151 if (in_system_header && current_class_type == NULL
13152 && current_lang_name == lang_name_c)
13156 return no_parameters;
13158 /* Check for `(void)', too, which is a special case. */
13159 else if (token->keyword == RID_VOID
13160 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13161 == CPP_CLOSE_PAREN))
13163 /* Consume the `void' token. */
13164 cp_lexer_consume_token (parser->lexer);
13165 /* There are no parameters. */
13166 return no_parameters;
13169 /* Parse the parameter-declaration-list. */
13170 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13171 /* If a parse error occurred while parsing the
13172 parameter-declaration-list, then the entire
13173 parameter-declaration-clause is erroneous. */
13177 /* Peek at the next token. */
13178 token = cp_lexer_peek_token (parser->lexer);
13179 /* If it's a `,', the clause should terminate with an ellipsis. */
13180 if (token->type == CPP_COMMA)
13182 /* Consume the `,'. */
13183 cp_lexer_consume_token (parser->lexer);
13184 /* Expect an ellipsis. */
13186 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13188 /* It might also be `...' if the optional trailing `,' was
13190 else if (token->type == CPP_ELLIPSIS)
13192 /* Consume the `...' token. */
13193 cp_lexer_consume_token (parser->lexer);
13194 /* And remember that we saw it. */
13198 ellipsis_p = false;
13200 /* Finish the parameter list. */
13201 if (parameters && ellipsis_p)
13202 parameters->ellipsis_p = true;
13207 /* Parse a parameter-declaration-list.
13209 parameter-declaration-list:
13210 parameter-declaration
13211 parameter-declaration-list , parameter-declaration
13213 Returns a representation of the parameter-declaration-list, as for
13214 cp_parser_parameter_declaration_clause. However, the
13215 `void_list_node' is never appended to the list. Upon return,
13216 *IS_ERROR will be true iff an error occurred. */
13218 static cp_parameter_declarator *
13219 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13221 cp_parameter_declarator *parameters = NULL;
13222 cp_parameter_declarator **tail = ¶meters;
13223 bool saved_in_unbraced_linkage_specification_p;
13225 /* Assume all will go well. */
13227 /* The special considerations that apply to a function within an
13228 unbraced linkage specifications do not apply to the parameters
13229 to the function. */
13230 saved_in_unbraced_linkage_specification_p
13231 = parser->in_unbraced_linkage_specification_p;
13232 parser->in_unbraced_linkage_specification_p = false;
13234 /* Look for more parameters. */
13237 cp_parameter_declarator *parameter;
13238 bool parenthesized_p;
13239 /* Parse the parameter. */
13241 = cp_parser_parameter_declaration (parser,
13242 /*template_parm_p=*/false,
13245 /* If a parse error occurred parsing the parameter declaration,
13246 then the entire parameter-declaration-list is erroneous. */
13253 /* Add the new parameter to the list. */
13255 tail = ¶meter->next;
13257 /* Peek at the next token. */
13258 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13259 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13260 /* These are for Objective-C++ */
13261 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13262 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13263 /* The parameter-declaration-list is complete. */
13265 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13269 /* Peek at the next token. */
13270 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13271 /* If it's an ellipsis, then the list is complete. */
13272 if (token->type == CPP_ELLIPSIS)
13274 /* Otherwise, there must be more parameters. Consume the
13276 cp_lexer_consume_token (parser->lexer);
13277 /* When parsing something like:
13279 int i(float f, double d)
13281 we can tell after seeing the declaration for "f" that we
13282 are not looking at an initialization of a variable "i",
13283 but rather at the declaration of a function "i".
13285 Due to the fact that the parsing of template arguments
13286 (as specified to a template-id) requires backtracking we
13287 cannot use this technique when inside a template argument
13289 if (!parser->in_template_argument_list_p
13290 && !parser->in_type_id_in_expr_p
13291 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13292 /* However, a parameter-declaration of the form
13293 "foat(f)" (which is a valid declaration of a
13294 parameter "f") can also be interpreted as an
13295 expression (the conversion of "f" to "float"). */
13296 && !parenthesized_p)
13297 cp_parser_commit_to_tentative_parse (parser);
13301 cp_parser_error (parser, "expected %<,%> or %<...%>");
13302 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13303 cp_parser_skip_to_closing_parenthesis (parser,
13304 /*recovering=*/true,
13305 /*or_comma=*/false,
13306 /*consume_paren=*/false);
13311 parser->in_unbraced_linkage_specification_p
13312 = saved_in_unbraced_linkage_specification_p;
13317 /* Parse a parameter declaration.
13319 parameter-declaration:
13320 decl-specifier-seq ... [opt] declarator
13321 decl-specifier-seq declarator = assignment-expression
13322 decl-specifier-seq ... [opt] abstract-declarator [opt]
13323 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13325 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13326 declares a template parameter. (In that case, a non-nested `>'
13327 token encountered during the parsing of the assignment-expression
13328 is not interpreted as a greater-than operator.)
13330 Returns a representation of the parameter, or NULL if an error
13331 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13332 true iff the declarator is of the form "(p)". */
13334 static cp_parameter_declarator *
13335 cp_parser_parameter_declaration (cp_parser *parser,
13336 bool template_parm_p,
13337 bool *parenthesized_p)
13339 int declares_class_or_enum;
13340 bool greater_than_is_operator_p;
13341 cp_decl_specifier_seq decl_specifiers;
13342 cp_declarator *declarator;
13343 tree default_argument;
13345 const char *saved_message;
13347 /* In a template parameter, `>' is not an operator.
13351 When parsing a default template-argument for a non-type
13352 template-parameter, the first non-nested `>' is taken as the end
13353 of the template parameter-list rather than a greater-than
13355 greater_than_is_operator_p = !template_parm_p;
13357 /* Type definitions may not appear in parameter types. */
13358 saved_message = parser->type_definition_forbidden_message;
13359 parser->type_definition_forbidden_message
13360 = "types may not be defined in parameter types";
13362 /* Parse the declaration-specifiers. */
13363 cp_parser_decl_specifier_seq (parser,
13364 CP_PARSER_FLAGS_NONE,
13366 &declares_class_or_enum);
13367 /* If an error occurred, there's no reason to attempt to parse the
13368 rest of the declaration. */
13369 if (cp_parser_error_occurred (parser))
13371 parser->type_definition_forbidden_message = saved_message;
13375 /* Peek at the next token. */
13376 token = cp_lexer_peek_token (parser->lexer);
13378 /* If the next token is a `)', `,', `=', `>', or `...', then there
13379 is no declarator. However, when variadic templates are enabled,
13380 there may be a declarator following `...'. */
13381 if (token->type == CPP_CLOSE_PAREN
13382 || token->type == CPP_COMMA
13383 || token->type == CPP_EQ
13384 || token->type == CPP_GREATER)
13387 if (parenthesized_p)
13388 *parenthesized_p = false;
13390 /* Otherwise, there should be a declarator. */
13393 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13394 parser->default_arg_ok_p = false;
13396 /* After seeing a decl-specifier-seq, if the next token is not a
13397 "(", there is no possibility that the code is a valid
13398 expression. Therefore, if parsing tentatively, we commit at
13400 if (!parser->in_template_argument_list_p
13401 /* In an expression context, having seen:
13405 we cannot be sure whether we are looking at a
13406 function-type (taking a "char" as a parameter) or a cast
13407 of some object of type "char" to "int". */
13408 && !parser->in_type_id_in_expr_p
13409 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13410 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13411 cp_parser_commit_to_tentative_parse (parser);
13412 /* Parse the declarator. */
13413 declarator = cp_parser_declarator (parser,
13414 CP_PARSER_DECLARATOR_EITHER,
13415 /*ctor_dtor_or_conv_p=*/NULL,
13417 /*member_p=*/false);
13418 parser->default_arg_ok_p = saved_default_arg_ok_p;
13419 /* After the declarator, allow more attributes. */
13420 decl_specifiers.attributes
13421 = chainon (decl_specifiers.attributes,
13422 cp_parser_attributes_opt (parser));
13425 /* If the next token is an ellipsis, and we have not seen a
13426 declarator name, and the type of the declarator contains parameter
13427 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13428 a parameter pack expansion expression. Otherwise, leave the
13429 ellipsis for a C-style variadic function. */
13430 token = cp_lexer_peek_token (parser->lexer);
13431 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13433 tree type = decl_specifiers.type;
13435 if (type && DECL_P (type))
13436 type = TREE_TYPE (type);
13439 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13440 && declarator_can_be_parameter_pack (declarator)
13441 && (!declarator || !declarator->parameter_pack_p)
13442 && uses_parameter_packs (type))
13444 /* Consume the `...'. */
13445 cp_lexer_consume_token (parser->lexer);
13446 maybe_warn_variadic_templates ();
13448 /* Build a pack expansion type */
13450 declarator->parameter_pack_p = true;
13452 decl_specifiers.type = make_pack_expansion (type);
13456 /* The restriction on defining new types applies only to the type
13457 of the parameter, not to the default argument. */
13458 parser->type_definition_forbidden_message = saved_message;
13460 /* If the next token is `=', then process a default argument. */
13461 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13463 bool saved_greater_than_is_operator_p;
13464 /* Consume the `='. */
13465 cp_lexer_consume_token (parser->lexer);
13467 /* If we are defining a class, then the tokens that make up the
13468 default argument must be saved and processed later. */
13469 if (!template_parm_p && at_class_scope_p ()
13470 && TYPE_BEING_DEFINED (current_class_type))
13472 unsigned depth = 0;
13473 cp_token *first_token;
13476 /* Add tokens until we have processed the entire default
13477 argument. We add the range [first_token, token). */
13478 first_token = cp_lexer_peek_token (parser->lexer);
13483 /* Peek at the next token. */
13484 token = cp_lexer_peek_token (parser->lexer);
13485 /* What we do depends on what token we have. */
13486 switch (token->type)
13488 /* In valid code, a default argument must be
13489 immediately followed by a `,' `)', or `...'. */
13491 case CPP_CLOSE_PAREN:
13493 /* If we run into a non-nested `;', `}', or `]',
13494 then the code is invalid -- but the default
13495 argument is certainly over. */
13496 case CPP_SEMICOLON:
13497 case CPP_CLOSE_BRACE:
13498 case CPP_CLOSE_SQUARE:
13501 /* Update DEPTH, if necessary. */
13502 else if (token->type == CPP_CLOSE_PAREN
13503 || token->type == CPP_CLOSE_BRACE
13504 || token->type == CPP_CLOSE_SQUARE)
13508 case CPP_OPEN_PAREN:
13509 case CPP_OPEN_SQUARE:
13510 case CPP_OPEN_BRACE:
13515 if (cxx_dialect == cxx98)
13517 /* Fall through for C++0x, which treats the `>>'
13518 operator like two `>' tokens in certain
13522 /* If we see a non-nested `>', and `>' is not an
13523 operator, then it marks the end of the default
13525 if (!depth && !greater_than_is_operator_p)
13529 /* If we run out of tokens, issue an error message. */
13531 case CPP_PRAGMA_EOL:
13532 error ("file ends in default argument");
13538 /* In these cases, we should look for template-ids.
13539 For example, if the default argument is
13540 `X<int, double>()', we need to do name lookup to
13541 figure out whether or not `X' is a template; if
13542 so, the `,' does not end the default argument.
13544 That is not yet done. */
13551 /* If we've reached the end, stop. */
13555 /* Add the token to the token block. */
13556 token = cp_lexer_consume_token (parser->lexer);
13559 /* Create a DEFAULT_ARG to represented the unparsed default
13561 default_argument = make_node (DEFAULT_ARG);
13562 DEFARG_TOKENS (default_argument)
13563 = cp_token_cache_new (first_token, token);
13564 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13566 /* Outside of a class definition, we can just parse the
13567 assignment-expression. */
13570 bool saved_local_variables_forbidden_p;
13572 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13574 saved_greater_than_is_operator_p
13575 = parser->greater_than_is_operator_p;
13576 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13577 /* Local variable names (and the `this' keyword) may not
13578 appear in a default argument. */
13579 saved_local_variables_forbidden_p
13580 = parser->local_variables_forbidden_p;
13581 parser->local_variables_forbidden_p = true;
13582 /* The default argument expression may cause implicitly
13583 defined member functions to be synthesized, which will
13584 result in garbage collection. We must treat this
13585 situation as if we were within the body of function so as
13586 to avoid collecting live data on the stack. */
13588 /* Parse the assignment-expression. */
13589 if (template_parm_p)
13590 push_deferring_access_checks (dk_no_deferred);
13592 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13593 if (template_parm_p)
13594 pop_deferring_access_checks ();
13595 /* Restore saved state. */
13597 parser->greater_than_is_operator_p
13598 = saved_greater_than_is_operator_p;
13599 parser->local_variables_forbidden_p
13600 = saved_local_variables_forbidden_p;
13602 if (!parser->default_arg_ok_p)
13604 if (!flag_pedantic_errors)
13605 warning (0, "deprecated use of default argument for parameter of non-function");
13608 error ("default arguments are only permitted for function parameters");
13609 default_argument = NULL_TREE;
13614 default_argument = NULL_TREE;
13616 return make_parameter_declarator (&decl_specifiers,
13621 /* Parse a function-body.
13624 compound_statement */
13627 cp_parser_function_body (cp_parser *parser)
13629 cp_parser_compound_statement (parser, NULL, false);
13632 /* Parse a ctor-initializer-opt followed by a function-body. Return
13633 true if a ctor-initializer was present. */
13636 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13639 bool ctor_initializer_p;
13641 /* Begin the function body. */
13642 body = begin_function_body ();
13643 /* Parse the optional ctor-initializer. */
13644 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13645 /* Parse the function-body. */
13646 cp_parser_function_body (parser);
13647 /* Finish the function body. */
13648 finish_function_body (body);
13650 return ctor_initializer_p;
13653 /* Parse an initializer.
13656 = initializer-clause
13657 ( expression-list )
13659 Returns an expression representing the initializer. If no
13660 initializer is present, NULL_TREE is returned.
13662 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13663 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13664 set to FALSE if there is no initializer present. If there is an
13665 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13666 is set to true; otherwise it is set to false. */
13669 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13670 bool* non_constant_p)
13675 /* Peek at the next token. */
13676 token = cp_lexer_peek_token (parser->lexer);
13678 /* Let our caller know whether or not this initializer was
13680 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13681 /* Assume that the initializer is constant. */
13682 *non_constant_p = false;
13684 if (token->type == CPP_EQ)
13686 /* Consume the `='. */
13687 cp_lexer_consume_token (parser->lexer);
13688 /* Parse the initializer-clause. */
13689 init = cp_parser_initializer_clause (parser, non_constant_p);
13691 else if (token->type == CPP_OPEN_PAREN)
13692 init = cp_parser_parenthesized_expression_list (parser, false,
13694 /*allow_expansion_p=*/true,
13698 /* Anything else is an error. */
13699 cp_parser_error (parser, "expected initializer");
13700 init = error_mark_node;
13706 /* Parse an initializer-clause.
13708 initializer-clause:
13709 assignment-expression
13710 { initializer-list , [opt] }
13713 Returns an expression representing the initializer.
13715 If the `assignment-expression' production is used the value
13716 returned is simply a representation for the expression.
13718 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13719 the elements of the initializer-list (or NULL, if the last
13720 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13721 NULL_TREE. There is no way to detect whether or not the optional
13722 trailing `,' was provided. NON_CONSTANT_P is as for
13723 cp_parser_initializer. */
13726 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13730 /* Assume the expression is constant. */
13731 *non_constant_p = false;
13733 /* If it is not a `{', then we are looking at an
13734 assignment-expression. */
13735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13738 = cp_parser_constant_expression (parser,
13739 /*allow_non_constant_p=*/true,
13741 if (!*non_constant_p)
13742 initializer = fold_non_dependent_expr (initializer);
13746 /* Consume the `{' token. */
13747 cp_lexer_consume_token (parser->lexer);
13748 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13749 initializer = make_node (CONSTRUCTOR);
13750 /* If it's not a `}', then there is a non-trivial initializer. */
13751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13753 /* Parse the initializer list. */
13754 CONSTRUCTOR_ELTS (initializer)
13755 = cp_parser_initializer_list (parser, non_constant_p);
13756 /* A trailing `,' token is allowed. */
13757 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13758 cp_lexer_consume_token (parser->lexer);
13760 /* Now, there should be a trailing `}'. */
13761 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13764 return initializer;
13767 /* Parse an initializer-list.
13770 initializer-clause ... [opt]
13771 initializer-list , initializer-clause ... [opt]
13776 identifier : initializer-clause
13777 initializer-list, identifier : initializer-clause
13779 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13780 for the initializer. If the INDEX of the elt is non-NULL, it is the
13781 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13782 as for cp_parser_initializer. */
13784 static VEC(constructor_elt,gc) *
13785 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13787 VEC(constructor_elt,gc) *v = NULL;
13789 /* Assume all of the expressions are constant. */
13790 *non_constant_p = false;
13792 /* Parse the rest of the list. */
13798 bool clause_non_constant_p;
13800 /* If the next token is an identifier and the following one is a
13801 colon, we are looking at the GNU designated-initializer
13803 if (cp_parser_allow_gnu_extensions_p (parser)
13804 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13805 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13807 /* Warn the user that they are using an extension. */
13809 pedwarn ("ISO C++ does not allow designated initializers");
13810 /* Consume the identifier. */
13811 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13812 /* Consume the `:'. */
13813 cp_lexer_consume_token (parser->lexer);
13816 identifier = NULL_TREE;
13818 /* Parse the initializer. */
13819 initializer = cp_parser_initializer_clause (parser,
13820 &clause_non_constant_p);
13821 /* If any clause is non-constant, so is the entire initializer. */
13822 if (clause_non_constant_p)
13823 *non_constant_p = true;
13825 /* If we have an ellipsis, this is an initializer pack
13827 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13829 /* Consume the `...'. */
13830 cp_lexer_consume_token (parser->lexer);
13832 /* Turn the initializer into an initializer expansion. */
13833 initializer = make_pack_expansion (initializer);
13836 /* Add it to the vector. */
13837 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13839 /* If the next token is not a comma, we have reached the end of
13841 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13844 /* Peek at the next token. */
13845 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13846 /* If the next token is a `}', then we're still done. An
13847 initializer-clause can have a trailing `,' after the
13848 initializer-list and before the closing `}'. */
13849 if (token->type == CPP_CLOSE_BRACE)
13852 /* Consume the `,' token. */
13853 cp_lexer_consume_token (parser->lexer);
13859 /* Classes [gram.class] */
13861 /* Parse a class-name.
13867 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13868 to indicate that names looked up in dependent types should be
13869 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13870 keyword has been used to indicate that the name that appears next
13871 is a template. TAG_TYPE indicates the explicit tag given before
13872 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13873 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13874 is the class being defined in a class-head.
13876 Returns the TYPE_DECL representing the class. */
13879 cp_parser_class_name (cp_parser *parser,
13880 bool typename_keyword_p,
13881 bool template_keyword_p,
13882 enum tag_types tag_type,
13883 bool check_dependency_p,
13885 bool is_declaration)
13892 /* All class-names start with an identifier. */
13893 token = cp_lexer_peek_token (parser->lexer);
13894 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13896 cp_parser_error (parser, "expected class-name");
13897 return error_mark_node;
13900 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13901 to a template-id, so we save it here. */
13902 scope = parser->scope;
13903 if (scope == error_mark_node)
13904 return error_mark_node;
13906 /* Any name names a type if we're following the `typename' keyword
13907 in a qualified name where the enclosing scope is type-dependent. */
13908 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13909 && dependent_type_p (scope));
13910 /* Handle the common case (an identifier, but not a template-id)
13912 if (token->type == CPP_NAME
13913 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13915 cp_token *identifier_token;
13919 /* Look for the identifier. */
13920 identifier_token = cp_lexer_peek_token (parser->lexer);
13921 ambiguous_p = identifier_token->ambiguous_p;
13922 identifier = cp_parser_identifier (parser);
13923 /* If the next token isn't an identifier, we are certainly not
13924 looking at a class-name. */
13925 if (identifier == error_mark_node)
13926 decl = error_mark_node;
13927 /* If we know this is a type-name, there's no need to look it
13929 else if (typename_p)
13933 tree ambiguous_decls;
13934 /* If we already know that this lookup is ambiguous, then
13935 we've already issued an error message; there's no reason
13939 cp_parser_simulate_error (parser);
13940 return error_mark_node;
13942 /* If the next token is a `::', then the name must be a type
13945 [basic.lookup.qual]
13947 During the lookup for a name preceding the :: scope
13948 resolution operator, object, function, and enumerator
13949 names are ignored. */
13950 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13951 tag_type = typename_type;
13952 /* Look up the name. */
13953 decl = cp_parser_lookup_name (parser, identifier,
13955 /*is_template=*/false,
13956 /*is_namespace=*/false,
13957 check_dependency_p,
13959 if (ambiguous_decls)
13961 error ("reference to %qD is ambiguous", identifier);
13962 print_candidates (ambiguous_decls);
13963 if (cp_parser_parsing_tentatively (parser))
13965 identifier_token->ambiguous_p = true;
13966 cp_parser_simulate_error (parser);
13968 return error_mark_node;
13974 /* Try a template-id. */
13975 decl = cp_parser_template_id (parser, template_keyword_p,
13976 check_dependency_p,
13978 if (decl == error_mark_node)
13979 return error_mark_node;
13982 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13984 /* If this is a typename, create a TYPENAME_TYPE. */
13985 if (typename_p && decl != error_mark_node)
13987 decl = make_typename_type (scope, decl, typename_type,
13988 /*complain=*/tf_error);
13989 if (decl != error_mark_node)
13990 decl = TYPE_NAME (decl);
13993 /* Check to see that it is really the name of a class. */
13994 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13995 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13996 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13997 /* Situations like this:
13999 template <typename T> struct A {
14000 typename T::template X<int>::I i;
14003 are problematic. Is `T::template X<int>' a class-name? The
14004 standard does not seem to be definitive, but there is no other
14005 valid interpretation of the following `::'. Therefore, those
14006 names are considered class-names. */
14008 decl = make_typename_type (scope, decl, tag_type, tf_error);
14009 if (decl != error_mark_node)
14010 decl = TYPE_NAME (decl);
14012 else if (TREE_CODE (decl) != TYPE_DECL
14013 || TREE_TYPE (decl) == error_mark_node
14014 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14015 decl = error_mark_node;
14017 if (decl == error_mark_node)
14018 cp_parser_error (parser, "expected class-name");
14023 /* Parse a class-specifier.
14026 class-head { member-specification [opt] }
14028 Returns the TREE_TYPE representing the class. */
14031 cp_parser_class_specifier (cp_parser* parser)
14035 tree attributes = NULL_TREE;
14036 int has_trailing_semicolon;
14037 bool nested_name_specifier_p;
14038 unsigned saved_num_template_parameter_lists;
14039 bool saved_in_function_body;
14040 tree old_scope = NULL_TREE;
14041 tree scope = NULL_TREE;
14044 push_deferring_access_checks (dk_no_deferred);
14046 /* Parse the class-head. */
14047 type = cp_parser_class_head (parser,
14048 &nested_name_specifier_p,
14051 /* If the class-head was a semantic disaster, skip the entire body
14055 cp_parser_skip_to_end_of_block_or_statement (parser);
14056 pop_deferring_access_checks ();
14057 return error_mark_node;
14060 /* Look for the `{'. */
14061 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14063 pop_deferring_access_checks ();
14064 return error_mark_node;
14067 /* Process the base classes. If they're invalid, skip the
14068 entire class body. */
14069 if (!xref_basetypes (type, bases))
14071 /* Consuming the closing brace yields better error messages
14073 if (cp_parser_skip_to_closing_brace (parser))
14074 cp_lexer_consume_token (parser->lexer);
14075 pop_deferring_access_checks ();
14076 return error_mark_node;
14079 /* Issue an error message if type-definitions are forbidden here. */
14080 cp_parser_check_type_definition (parser);
14081 /* Remember that we are defining one more class. */
14082 ++parser->num_classes_being_defined;
14083 /* Inside the class, surrounding template-parameter-lists do not
14085 saved_num_template_parameter_lists
14086 = parser->num_template_parameter_lists;
14087 parser->num_template_parameter_lists = 0;
14088 /* We are not in a function body. */
14089 saved_in_function_body = parser->in_function_body;
14090 parser->in_function_body = false;
14092 /* Start the class. */
14093 if (nested_name_specifier_p)
14095 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14096 old_scope = push_inner_scope (scope);
14098 type = begin_class_definition (type, attributes);
14100 if (type == error_mark_node)
14101 /* If the type is erroneous, skip the entire body of the class. */
14102 cp_parser_skip_to_closing_brace (parser);
14104 /* Parse the member-specification. */
14105 cp_parser_member_specification_opt (parser);
14107 /* Look for the trailing `}'. */
14108 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14109 /* We get better error messages by noticing a common problem: a
14110 missing trailing `;'. */
14111 token = cp_lexer_peek_token (parser->lexer);
14112 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14113 /* Look for trailing attributes to apply to this class. */
14114 if (cp_parser_allow_gnu_extensions_p (parser))
14115 attributes = cp_parser_attributes_opt (parser);
14116 if (type != error_mark_node)
14117 type = finish_struct (type, attributes);
14118 if (nested_name_specifier_p)
14119 pop_inner_scope (old_scope, scope);
14120 /* If this class is not itself within the scope of another class,
14121 then we need to parse the bodies of all of the queued function
14122 definitions. Note that the queued functions defined in a class
14123 are not always processed immediately following the
14124 class-specifier for that class. Consider:
14127 struct B { void f() { sizeof (A); } };
14130 If `f' were processed before the processing of `A' were
14131 completed, there would be no way to compute the size of `A'.
14132 Note that the nesting we are interested in here is lexical --
14133 not the semantic nesting given by TYPE_CONTEXT. In particular,
14136 struct A { struct B; };
14137 struct A::B { void f() { } };
14139 there is no need to delay the parsing of `A::B::f'. */
14140 if (--parser->num_classes_being_defined == 0)
14144 tree class_type = NULL_TREE;
14145 tree pushed_scope = NULL_TREE;
14147 /* In a first pass, parse default arguments to the functions.
14148 Then, in a second pass, parse the bodies of the functions.
14149 This two-phased approach handles cases like:
14157 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14158 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14159 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14160 TREE_PURPOSE (parser->unparsed_functions_queues)
14161 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14163 fn = TREE_VALUE (queue_entry);
14164 /* If there are default arguments that have not yet been processed,
14165 take care of them now. */
14166 if (class_type != TREE_PURPOSE (queue_entry))
14169 pop_scope (pushed_scope);
14170 class_type = TREE_PURPOSE (queue_entry);
14171 pushed_scope = push_scope (class_type);
14173 /* Make sure that any template parameters are in scope. */
14174 maybe_begin_member_template_processing (fn);
14175 /* Parse the default argument expressions. */
14176 cp_parser_late_parsing_default_args (parser, fn);
14177 /* Remove any template parameters from the symbol table. */
14178 maybe_end_member_template_processing ();
14181 pop_scope (pushed_scope);
14182 /* Now parse the body of the functions. */
14183 for (TREE_VALUE (parser->unparsed_functions_queues)
14184 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14185 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14186 TREE_VALUE (parser->unparsed_functions_queues)
14187 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14189 /* Figure out which function we need to process. */
14190 fn = TREE_VALUE (queue_entry);
14191 /* Parse the function. */
14192 cp_parser_late_parsing_for_member (parser, fn);
14196 /* Put back any saved access checks. */
14197 pop_deferring_access_checks ();
14199 /* Restore saved state. */
14200 parser->in_function_body = saved_in_function_body;
14201 parser->num_template_parameter_lists
14202 = saved_num_template_parameter_lists;
14207 /* Parse a class-head.
14210 class-key identifier [opt] base-clause [opt]
14211 class-key nested-name-specifier identifier base-clause [opt]
14212 class-key nested-name-specifier [opt] template-id
14216 class-key attributes identifier [opt] base-clause [opt]
14217 class-key attributes nested-name-specifier identifier base-clause [opt]
14218 class-key attributes nested-name-specifier [opt] template-id
14221 Upon return BASES is initialized to the list of base classes (or
14222 NULL, if there are none) in the same form returned by
14223 cp_parser_base_clause.
14225 Returns the TYPE of the indicated class. Sets
14226 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14227 involving a nested-name-specifier was used, and FALSE otherwise.
14229 Returns error_mark_node if this is not a class-head.
14231 Returns NULL_TREE if the class-head is syntactically valid, but
14232 semantically invalid in a way that means we should skip the entire
14233 body of the class. */
14236 cp_parser_class_head (cp_parser* parser,
14237 bool* nested_name_specifier_p,
14238 tree *attributes_p,
14241 tree nested_name_specifier;
14242 enum tag_types class_key;
14243 tree id = NULL_TREE;
14244 tree type = NULL_TREE;
14246 bool template_id_p = false;
14247 bool qualified_p = false;
14248 bool invalid_nested_name_p = false;
14249 bool invalid_explicit_specialization_p = false;
14250 tree pushed_scope = NULL_TREE;
14251 unsigned num_templates;
14253 /* Assume no nested-name-specifier will be present. */
14254 *nested_name_specifier_p = false;
14255 /* Assume no template parameter lists will be used in defining the
14259 *bases = NULL_TREE;
14261 /* Look for the class-key. */
14262 class_key = cp_parser_class_key (parser);
14263 if (class_key == none_type)
14264 return error_mark_node;
14266 /* Parse the attributes. */
14267 attributes = cp_parser_attributes_opt (parser);
14269 /* If the next token is `::', that is invalid -- but sometimes
14270 people do try to write:
14274 Handle this gracefully by accepting the extra qualifier, and then
14275 issuing an error about it later if this really is a
14276 class-head. If it turns out just to be an elaborated type
14277 specifier, remain silent. */
14278 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14279 qualified_p = true;
14281 push_deferring_access_checks (dk_no_check);
14283 /* Determine the name of the class. Begin by looking for an
14284 optional nested-name-specifier. */
14285 nested_name_specifier
14286 = cp_parser_nested_name_specifier_opt (parser,
14287 /*typename_keyword_p=*/false,
14288 /*check_dependency_p=*/false,
14290 /*is_declaration=*/false);
14291 /* If there was a nested-name-specifier, then there *must* be an
14293 if (nested_name_specifier)
14295 /* Although the grammar says `identifier', it really means
14296 `class-name' or `template-name'. You are only allowed to
14297 define a class that has already been declared with this
14300 The proposed resolution for Core Issue 180 says that wherever
14301 you see `class T::X' you should treat `X' as a type-name.
14303 It is OK to define an inaccessible class; for example:
14305 class A { class B; };
14308 We do not know if we will see a class-name, or a
14309 template-name. We look for a class-name first, in case the
14310 class-name is a template-id; if we looked for the
14311 template-name first we would stop after the template-name. */
14312 cp_parser_parse_tentatively (parser);
14313 type = cp_parser_class_name (parser,
14314 /*typename_keyword_p=*/false,
14315 /*template_keyword_p=*/false,
14317 /*check_dependency_p=*/false,
14318 /*class_head_p=*/true,
14319 /*is_declaration=*/false);
14320 /* If that didn't work, ignore the nested-name-specifier. */
14321 if (!cp_parser_parse_definitely (parser))
14323 invalid_nested_name_p = true;
14324 id = cp_parser_identifier (parser);
14325 if (id == error_mark_node)
14328 /* If we could not find a corresponding TYPE, treat this
14329 declaration like an unqualified declaration. */
14330 if (type == error_mark_node)
14331 nested_name_specifier = NULL_TREE;
14332 /* Otherwise, count the number of templates used in TYPE and its
14333 containing scopes. */
14338 for (scope = TREE_TYPE (type);
14339 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14340 scope = (TYPE_P (scope)
14341 ? TYPE_CONTEXT (scope)
14342 : DECL_CONTEXT (scope)))
14344 && CLASS_TYPE_P (scope)
14345 && CLASSTYPE_TEMPLATE_INFO (scope)
14346 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14347 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14351 /* Otherwise, the identifier is optional. */
14354 /* We don't know whether what comes next is a template-id,
14355 an identifier, or nothing at all. */
14356 cp_parser_parse_tentatively (parser);
14357 /* Check for a template-id. */
14358 id = cp_parser_template_id (parser,
14359 /*template_keyword_p=*/false,
14360 /*check_dependency_p=*/true,
14361 /*is_declaration=*/true);
14362 /* If that didn't work, it could still be an identifier. */
14363 if (!cp_parser_parse_definitely (parser))
14365 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14366 id = cp_parser_identifier (parser);
14372 template_id_p = true;
14377 pop_deferring_access_checks ();
14380 cp_parser_check_for_invalid_template_id (parser, id);
14382 /* If it's not a `:' or a `{' then we can't really be looking at a
14383 class-head, since a class-head only appears as part of a
14384 class-specifier. We have to detect this situation before calling
14385 xref_tag, since that has irreversible side-effects. */
14386 if (!cp_parser_next_token_starts_class_definition_p (parser))
14388 cp_parser_error (parser, "expected %<{%> or %<:%>");
14389 return error_mark_node;
14392 /* At this point, we're going ahead with the class-specifier, even
14393 if some other problem occurs. */
14394 cp_parser_commit_to_tentative_parse (parser);
14395 /* Issue the error about the overly-qualified name now. */
14397 cp_parser_error (parser,
14398 "global qualification of class name is invalid");
14399 else if (invalid_nested_name_p)
14400 cp_parser_error (parser,
14401 "qualified name does not name a class");
14402 else if (nested_name_specifier)
14406 /* Reject typedef-names in class heads. */
14407 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14409 error ("invalid class name in declaration of %qD", type);
14414 /* Figure out in what scope the declaration is being placed. */
14415 scope = current_scope ();
14416 /* If that scope does not contain the scope in which the
14417 class was originally declared, the program is invalid. */
14418 if (scope && !is_ancestor (scope, nested_name_specifier))
14420 error ("declaration of %qD in %qD which does not enclose %qD",
14421 type, scope, nested_name_specifier);
14427 A declarator-id shall not be qualified exception of the
14428 definition of a ... nested class outside of its class
14429 ... [or] a the definition or explicit instantiation of a
14430 class member of a namespace outside of its namespace. */
14431 if (scope == nested_name_specifier)
14433 pedwarn ("extra qualification ignored");
14434 nested_name_specifier = NULL_TREE;
14438 /* An explicit-specialization must be preceded by "template <>". If
14439 it is not, try to recover gracefully. */
14440 if (at_namespace_scope_p ()
14441 && parser->num_template_parameter_lists == 0
14444 error ("an explicit specialization must be preceded by %<template <>%>");
14445 invalid_explicit_specialization_p = true;
14446 /* Take the same action that would have been taken by
14447 cp_parser_explicit_specialization. */
14448 ++parser->num_template_parameter_lists;
14449 begin_specialization ();
14451 /* There must be no "return" statements between this point and the
14452 end of this function; set "type "to the correct return value and
14453 use "goto done;" to return. */
14454 /* Make sure that the right number of template parameters were
14456 if (!cp_parser_check_template_parameters (parser, num_templates))
14458 /* If something went wrong, there is no point in even trying to
14459 process the class-definition. */
14464 /* Look up the type. */
14467 type = TREE_TYPE (id);
14468 type = maybe_process_partial_specialization (type);
14469 if (nested_name_specifier)
14470 pushed_scope = push_scope (nested_name_specifier);
14472 else if (nested_name_specifier)
14478 template <typename T> struct S { struct T };
14479 template <typename T> struct S<T>::T { };
14481 we will get a TYPENAME_TYPE when processing the definition of
14482 `S::T'. We need to resolve it to the actual type before we
14483 try to define it. */
14484 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14486 class_type = resolve_typename_type (TREE_TYPE (type),
14487 /*only_current_p=*/false);
14488 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14489 type = TYPE_NAME (class_type);
14492 cp_parser_error (parser, "could not resolve typename type");
14493 type = error_mark_node;
14497 maybe_process_partial_specialization (TREE_TYPE (type));
14498 class_type = current_class_type;
14499 /* Enter the scope indicated by the nested-name-specifier. */
14500 pushed_scope = push_scope (nested_name_specifier);
14501 /* Get the canonical version of this type. */
14502 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14503 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14504 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14506 type = push_template_decl (type);
14507 if (type == error_mark_node)
14514 type = TREE_TYPE (type);
14515 *nested_name_specifier_p = true;
14517 else /* The name is not a nested name. */
14519 /* If the class was unnamed, create a dummy name. */
14521 id = make_anon_name ();
14522 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14523 parser->num_template_parameter_lists);
14526 /* Indicate whether this class was declared as a `class' or as a
14528 if (TREE_CODE (type) == RECORD_TYPE)
14529 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14530 cp_parser_check_class_key (class_key, type);
14532 /* If this type was already complete, and we see another definition,
14533 that's an error. */
14534 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14536 error ("redefinition of %q#T", type);
14537 error ("previous definition of %q+#T", type);
14541 else if (type == error_mark_node)
14544 /* We will have entered the scope containing the class; the names of
14545 base classes should be looked up in that context. For example:
14547 struct A { struct B {}; struct C; };
14548 struct A::C : B {};
14552 /* Get the list of base-classes, if there is one. */
14553 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14554 *bases = cp_parser_base_clause (parser);
14557 /* Leave the scope given by the nested-name-specifier. We will
14558 enter the class scope itself while processing the members. */
14560 pop_scope (pushed_scope);
14562 if (invalid_explicit_specialization_p)
14564 end_specialization ();
14565 --parser->num_template_parameter_lists;
14567 *attributes_p = attributes;
14571 /* Parse a class-key.
14578 Returns the kind of class-key specified, or none_type to indicate
14581 static enum tag_types
14582 cp_parser_class_key (cp_parser* parser)
14585 enum tag_types tag_type;
14587 /* Look for the class-key. */
14588 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14592 /* Check to see if the TOKEN is a class-key. */
14593 tag_type = cp_parser_token_is_class_key (token);
14595 cp_parser_error (parser, "expected class-key");
14599 /* Parse an (optional) member-specification.
14601 member-specification:
14602 member-declaration member-specification [opt]
14603 access-specifier : member-specification [opt] */
14606 cp_parser_member_specification_opt (cp_parser* parser)
14613 /* Peek at the next token. */
14614 token = cp_lexer_peek_token (parser->lexer);
14615 /* If it's a `}', or EOF then we've seen all the members. */
14616 if (token->type == CPP_CLOSE_BRACE
14617 || token->type == CPP_EOF
14618 || token->type == CPP_PRAGMA_EOL)
14621 /* See if this token is a keyword. */
14622 keyword = token->keyword;
14626 case RID_PROTECTED:
14628 /* Consume the access-specifier. */
14629 cp_lexer_consume_token (parser->lexer);
14630 /* Remember which access-specifier is active. */
14631 current_access_specifier = token->u.value;
14632 /* Look for the `:'. */
14633 cp_parser_require (parser, CPP_COLON, "`:'");
14637 /* Accept #pragmas at class scope. */
14638 if (token->type == CPP_PRAGMA)
14640 cp_parser_pragma (parser, pragma_external);
14644 /* Otherwise, the next construction must be a
14645 member-declaration. */
14646 cp_parser_member_declaration (parser);
14651 /* Parse a member-declaration.
14653 member-declaration:
14654 decl-specifier-seq [opt] member-declarator-list [opt] ;
14655 function-definition ; [opt]
14656 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14658 template-declaration
14660 member-declarator-list:
14662 member-declarator-list , member-declarator
14665 declarator pure-specifier [opt]
14666 declarator constant-initializer [opt]
14667 identifier [opt] : constant-expression
14671 member-declaration:
14672 __extension__ member-declaration
14675 declarator attributes [opt] pure-specifier [opt]
14676 declarator attributes [opt] constant-initializer [opt]
14677 identifier [opt] attributes [opt] : constant-expression
14681 member-declaration:
14682 static_assert-declaration */
14685 cp_parser_member_declaration (cp_parser* parser)
14687 cp_decl_specifier_seq decl_specifiers;
14688 tree prefix_attributes;
14690 int declares_class_or_enum;
14693 int saved_pedantic;
14695 /* Check for the `__extension__' keyword. */
14696 if (cp_parser_extension_opt (parser, &saved_pedantic))
14699 cp_parser_member_declaration (parser);
14700 /* Restore the old value of the PEDANTIC flag. */
14701 pedantic = saved_pedantic;
14706 /* Check for a template-declaration. */
14707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14709 /* An explicit specialization here is an error condition, and we
14710 expect the specialization handler to detect and report this. */
14711 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14712 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14713 cp_parser_explicit_specialization (parser);
14715 cp_parser_template_declaration (parser, /*member_p=*/true);
14720 /* Check for a using-declaration. */
14721 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14723 /* Parse the using-declaration. */
14724 cp_parser_using_declaration (parser,
14725 /*access_declaration_p=*/false);
14729 /* Check for @defs. */
14730 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14733 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14734 ivar = ivar_chains;
14738 ivar = TREE_CHAIN (member);
14739 TREE_CHAIN (member) = NULL_TREE;
14740 finish_member_declaration (member);
14745 /* If the next token is `static_assert' we have a static assertion. */
14746 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14748 cp_parser_static_assert (parser, /*member_p=*/true);
14752 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14755 /* Parse the decl-specifier-seq. */
14756 cp_parser_decl_specifier_seq (parser,
14757 CP_PARSER_FLAGS_OPTIONAL,
14759 &declares_class_or_enum);
14760 prefix_attributes = decl_specifiers.attributes;
14761 decl_specifiers.attributes = NULL_TREE;
14762 /* Check for an invalid type-name. */
14763 if (!decl_specifiers.type
14764 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14766 /* If there is no declarator, then the decl-specifier-seq should
14768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14770 /* If there was no decl-specifier-seq, and the next token is a
14771 `;', then we have something like:
14777 Each member-declaration shall declare at least one member
14778 name of the class. */
14779 if (!decl_specifiers.any_specifiers_p)
14781 cp_token *token = cp_lexer_peek_token (parser->lexer);
14782 if (pedantic && !token->in_system_header)
14783 pedwarn ("%Hextra %<;%>", &token->location);
14789 /* See if this declaration is a friend. */
14790 friend_p = cp_parser_friend_p (&decl_specifiers);
14791 /* If there were decl-specifiers, check to see if there was
14792 a class-declaration. */
14793 type = check_tag_decl (&decl_specifiers);
14794 /* Nested classes have already been added to the class, but
14795 a `friend' needs to be explicitly registered. */
14798 /* If the `friend' keyword was present, the friend must
14799 be introduced with a class-key. */
14800 if (!declares_class_or_enum)
14801 error ("a class-key must be used when declaring a friend");
14804 template <typename T> struct A {
14805 friend struct A<T>::B;
14808 A<T>::B will be represented by a TYPENAME_TYPE, and
14809 therefore not recognized by check_tag_decl. */
14811 && decl_specifiers.type
14812 && TYPE_P (decl_specifiers.type))
14813 type = decl_specifiers.type;
14814 if (!type || !TYPE_P (type))
14815 error ("friend declaration does not name a class or "
14818 make_friend_class (current_class_type, type,
14819 /*complain=*/true);
14821 /* If there is no TYPE, an error message will already have
14823 else if (!type || type == error_mark_node)
14825 /* An anonymous aggregate has to be handled specially; such
14826 a declaration really declares a data member (with a
14827 particular type), as opposed to a nested class. */
14828 else if (ANON_AGGR_TYPE_P (type))
14830 /* Remove constructors and such from TYPE, now that we
14831 know it is an anonymous aggregate. */
14832 fixup_anonymous_aggr (type);
14833 /* And make the corresponding data member. */
14834 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14835 /* Add it to the class. */
14836 finish_member_declaration (decl);
14839 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14844 /* See if these declarations will be friends. */
14845 friend_p = cp_parser_friend_p (&decl_specifiers);
14847 /* Keep going until we hit the `;' at the end of the
14849 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14851 tree attributes = NULL_TREE;
14852 tree first_attribute;
14854 /* Peek at the next token. */
14855 token = cp_lexer_peek_token (parser->lexer);
14857 /* Check for a bitfield declaration. */
14858 if (token->type == CPP_COLON
14859 || (token->type == CPP_NAME
14860 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14866 /* Get the name of the bitfield. Note that we cannot just
14867 check TOKEN here because it may have been invalidated by
14868 the call to cp_lexer_peek_nth_token above. */
14869 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14870 identifier = cp_parser_identifier (parser);
14872 identifier = NULL_TREE;
14874 /* Consume the `:' token. */
14875 cp_lexer_consume_token (parser->lexer);
14876 /* Get the width of the bitfield. */
14878 = cp_parser_constant_expression (parser,
14879 /*allow_non_constant=*/false,
14882 /* Look for attributes that apply to the bitfield. */
14883 attributes = cp_parser_attributes_opt (parser);
14884 /* Remember which attributes are prefix attributes and
14886 first_attribute = attributes;
14887 /* Combine the attributes. */
14888 attributes = chainon (prefix_attributes, attributes);
14890 /* Create the bitfield declaration. */
14891 decl = grokbitfield (identifier
14892 ? make_id_declarator (NULL_TREE,
14898 /* Apply the attributes. */
14899 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14903 cp_declarator *declarator;
14905 tree asm_specification;
14906 int ctor_dtor_or_conv_p;
14908 /* Parse the declarator. */
14910 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14911 &ctor_dtor_or_conv_p,
14912 /*parenthesized_p=*/NULL,
14913 /*member_p=*/true);
14915 /* If something went wrong parsing the declarator, make sure
14916 that we at least consume some tokens. */
14917 if (declarator == cp_error_declarator)
14919 /* Skip to the end of the statement. */
14920 cp_parser_skip_to_end_of_statement (parser);
14921 /* If the next token is not a semicolon, that is
14922 probably because we just skipped over the body of
14923 a function. So, we consume a semicolon if
14924 present, but do not issue an error message if it
14926 if (cp_lexer_next_token_is (parser->lexer,
14928 cp_lexer_consume_token (parser->lexer);
14932 if (declares_class_or_enum & 2)
14933 cp_parser_check_for_definition_in_return_type
14934 (declarator, decl_specifiers.type);
14936 /* Look for an asm-specification. */
14937 asm_specification = cp_parser_asm_specification_opt (parser);
14938 /* Look for attributes that apply to the declaration. */
14939 attributes = cp_parser_attributes_opt (parser);
14940 /* Remember which attributes are prefix attributes and
14942 first_attribute = attributes;
14943 /* Combine the attributes. */
14944 attributes = chainon (prefix_attributes, attributes);
14946 /* If it's an `=', then we have a constant-initializer or a
14947 pure-specifier. It is not correct to parse the
14948 initializer before registering the member declaration
14949 since the member declaration should be in scope while
14950 its initializer is processed. However, the rest of the
14951 front end does not yet provide an interface that allows
14952 us to handle this correctly. */
14953 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14957 A pure-specifier shall be used only in the declaration of
14958 a virtual function.
14960 A member-declarator can contain a constant-initializer
14961 only if it declares a static member of integral or
14964 Therefore, if the DECLARATOR is for a function, we look
14965 for a pure-specifier; otherwise, we look for a
14966 constant-initializer. When we call `grokfield', it will
14967 perform more stringent semantics checks. */
14968 if (function_declarator_p (declarator))
14969 initializer = cp_parser_pure_specifier (parser);
14971 /* Parse the initializer. */
14972 initializer = cp_parser_constant_initializer (parser);
14974 /* Otherwise, there is no initializer. */
14976 initializer = NULL_TREE;
14978 /* See if we are probably looking at a function
14979 definition. We are certainly not looking at a
14980 member-declarator. Calling `grokfield' has
14981 side-effects, so we must not do it unless we are sure
14982 that we are looking at a member-declarator. */
14983 if (cp_parser_token_starts_function_definition_p
14984 (cp_lexer_peek_token (parser->lexer)))
14986 /* The grammar does not allow a pure-specifier to be
14987 used when a member function is defined. (It is
14988 possible that this fact is an oversight in the
14989 standard, since a pure function may be defined
14990 outside of the class-specifier. */
14992 error ("pure-specifier on function-definition");
14993 decl = cp_parser_save_member_function_body (parser,
14997 /* If the member was not a friend, declare it here. */
14999 finish_member_declaration (decl);
15000 /* Peek at the next token. */
15001 token = cp_lexer_peek_token (parser->lexer);
15002 /* If the next token is a semicolon, consume it. */
15003 if (token->type == CPP_SEMICOLON)
15004 cp_lexer_consume_token (parser->lexer);
15008 /* Create the declaration. */
15009 decl = grokfield (declarator, &decl_specifiers,
15010 initializer, /*init_const_expr_p=*/true,
15015 /* Reset PREFIX_ATTRIBUTES. */
15016 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15017 attributes = TREE_CHAIN (attributes);
15019 TREE_CHAIN (attributes) = NULL_TREE;
15021 /* If there is any qualification still in effect, clear it
15022 now; we will be starting fresh with the next declarator. */
15023 parser->scope = NULL_TREE;
15024 parser->qualifying_scope = NULL_TREE;
15025 parser->object_scope = NULL_TREE;
15026 /* If it's a `,', then there are more declarators. */
15027 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15028 cp_lexer_consume_token (parser->lexer);
15029 /* If the next token isn't a `;', then we have a parse error. */
15030 else if (cp_lexer_next_token_is_not (parser->lexer,
15033 cp_parser_error (parser, "expected %<;%>");
15034 /* Skip tokens until we find a `;'. */
15035 cp_parser_skip_to_end_of_statement (parser);
15042 /* Add DECL to the list of members. */
15044 finish_member_declaration (decl);
15046 if (TREE_CODE (decl) == FUNCTION_DECL)
15047 cp_parser_save_default_args (parser, decl);
15052 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15055 /* Parse a pure-specifier.
15060 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15061 Otherwise, ERROR_MARK_NODE is returned. */
15064 cp_parser_pure_specifier (cp_parser* parser)
15068 /* Look for the `=' token. */
15069 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15070 return error_mark_node;
15071 /* Look for the `0' token. */
15072 token = cp_lexer_consume_token (parser->lexer);
15073 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15074 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15076 cp_parser_error (parser,
15077 "invalid pure specifier (only `= 0' is allowed)");
15078 cp_parser_skip_to_end_of_statement (parser);
15079 return error_mark_node;
15081 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15083 error ("templates may not be %<virtual%>");
15084 return error_mark_node;
15087 return integer_zero_node;
15090 /* Parse a constant-initializer.
15092 constant-initializer:
15093 = constant-expression
15095 Returns a representation of the constant-expression. */
15098 cp_parser_constant_initializer (cp_parser* parser)
15100 /* Look for the `=' token. */
15101 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15102 return error_mark_node;
15104 /* It is invalid to write:
15106 struct S { static const int i = { 7 }; };
15109 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15111 cp_parser_error (parser,
15112 "a brace-enclosed initializer is not allowed here");
15113 /* Consume the opening brace. */
15114 cp_lexer_consume_token (parser->lexer);
15115 /* Skip the initializer. */
15116 cp_parser_skip_to_closing_brace (parser);
15117 /* Look for the trailing `}'. */
15118 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15120 return error_mark_node;
15123 return cp_parser_constant_expression (parser,
15124 /*allow_non_constant=*/false,
15128 /* Derived classes [gram.class.derived] */
15130 /* Parse a base-clause.
15133 : base-specifier-list
15135 base-specifier-list:
15136 base-specifier ... [opt]
15137 base-specifier-list , base-specifier ... [opt]
15139 Returns a TREE_LIST representing the base-classes, in the order in
15140 which they were declared. The representation of each node is as
15141 described by cp_parser_base_specifier.
15143 In the case that no bases are specified, this function will return
15144 NULL_TREE, not ERROR_MARK_NODE. */
15147 cp_parser_base_clause (cp_parser* parser)
15149 tree bases = NULL_TREE;
15151 /* Look for the `:' that begins the list. */
15152 cp_parser_require (parser, CPP_COLON, "`:'");
15154 /* Scan the base-specifier-list. */
15159 bool pack_expansion_p = false;
15161 /* Look for the base-specifier. */
15162 base = cp_parser_base_specifier (parser);
15163 /* Look for the (optional) ellipsis. */
15164 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15166 /* Consume the `...'. */
15167 cp_lexer_consume_token (parser->lexer);
15169 pack_expansion_p = true;
15172 /* Add BASE to the front of the list. */
15173 if (base != error_mark_node)
15175 if (pack_expansion_p)
15176 /* Make this a pack expansion type. */
15177 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15179 check_for_bare_parameter_packs (TREE_VALUE (base));
15181 TREE_CHAIN (base) = bases;
15184 /* Peek at the next token. */
15185 token = cp_lexer_peek_token (parser->lexer);
15186 /* If it's not a comma, then the list is complete. */
15187 if (token->type != CPP_COMMA)
15189 /* Consume the `,'. */
15190 cp_lexer_consume_token (parser->lexer);
15193 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15194 base class had a qualified name. However, the next name that
15195 appears is certainly not qualified. */
15196 parser->scope = NULL_TREE;
15197 parser->qualifying_scope = NULL_TREE;
15198 parser->object_scope = NULL_TREE;
15200 return nreverse (bases);
15203 /* Parse a base-specifier.
15206 :: [opt] nested-name-specifier [opt] class-name
15207 virtual access-specifier [opt] :: [opt] nested-name-specifier
15209 access-specifier virtual [opt] :: [opt] nested-name-specifier
15212 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15213 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15214 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15215 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15218 cp_parser_base_specifier (cp_parser* parser)
15222 bool virtual_p = false;
15223 bool duplicate_virtual_error_issued_p = false;
15224 bool duplicate_access_error_issued_p = false;
15225 bool class_scope_p, template_p;
15226 tree access = access_default_node;
15229 /* Process the optional `virtual' and `access-specifier'. */
15232 /* Peek at the next token. */
15233 token = cp_lexer_peek_token (parser->lexer);
15234 /* Process `virtual'. */
15235 switch (token->keyword)
15238 /* If `virtual' appears more than once, issue an error. */
15239 if (virtual_p && !duplicate_virtual_error_issued_p)
15241 cp_parser_error (parser,
15242 "%<virtual%> specified more than once in base-specified");
15243 duplicate_virtual_error_issued_p = true;
15248 /* Consume the `virtual' token. */
15249 cp_lexer_consume_token (parser->lexer);
15254 case RID_PROTECTED:
15256 /* If more than one access specifier appears, issue an
15258 if (access != access_default_node
15259 && !duplicate_access_error_issued_p)
15261 cp_parser_error (parser,
15262 "more than one access specifier in base-specified");
15263 duplicate_access_error_issued_p = true;
15266 access = ridpointers[(int) token->keyword];
15268 /* Consume the access-specifier. */
15269 cp_lexer_consume_token (parser->lexer);
15278 /* It is not uncommon to see programs mechanically, erroneously, use
15279 the 'typename' keyword to denote (dependent) qualified types
15280 as base classes. */
15281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15283 if (!processing_template_decl)
15284 error ("keyword %<typename%> not allowed outside of templates");
15286 error ("keyword %<typename%> not allowed in this context "
15287 "(the base class is implicitly a type)");
15288 cp_lexer_consume_token (parser->lexer);
15291 /* Look for the optional `::' operator. */
15292 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15293 /* Look for the nested-name-specifier. The simplest way to
15298 The keyword `typename' is not permitted in a base-specifier or
15299 mem-initializer; in these contexts a qualified name that
15300 depends on a template-parameter is implicitly assumed to be a
15303 is to pretend that we have seen the `typename' keyword at this
15305 cp_parser_nested_name_specifier_opt (parser,
15306 /*typename_keyword_p=*/true,
15307 /*check_dependency_p=*/true,
15309 /*is_declaration=*/true);
15310 /* If the base class is given by a qualified name, assume that names
15311 we see are type names or templates, as appropriate. */
15312 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15313 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15315 /* Finally, look for the class-name. */
15316 type = cp_parser_class_name (parser,
15320 /*check_dependency_p=*/true,
15321 /*class_head_p=*/false,
15322 /*is_declaration=*/true);
15324 if (type == error_mark_node)
15325 return error_mark_node;
15327 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15330 /* Exception handling [gram.exception] */
15332 /* Parse an (optional) exception-specification.
15334 exception-specification:
15335 throw ( type-id-list [opt] )
15337 Returns a TREE_LIST representing the exception-specification. The
15338 TREE_VALUE of each node is a type. */
15341 cp_parser_exception_specification_opt (cp_parser* parser)
15346 /* Peek at the next token. */
15347 token = cp_lexer_peek_token (parser->lexer);
15348 /* If it's not `throw', then there's no exception-specification. */
15349 if (!cp_parser_is_keyword (token, RID_THROW))
15352 /* Consume the `throw'. */
15353 cp_lexer_consume_token (parser->lexer);
15355 /* Look for the `('. */
15356 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15358 /* Peek at the next token. */
15359 token = cp_lexer_peek_token (parser->lexer);
15360 /* If it's not a `)', then there is a type-id-list. */
15361 if (token->type != CPP_CLOSE_PAREN)
15363 const char *saved_message;
15365 /* Types may not be defined in an exception-specification. */
15366 saved_message = parser->type_definition_forbidden_message;
15367 parser->type_definition_forbidden_message
15368 = "types may not be defined in an exception-specification";
15369 /* Parse the type-id-list. */
15370 type_id_list = cp_parser_type_id_list (parser);
15371 /* Restore the saved message. */
15372 parser->type_definition_forbidden_message = saved_message;
15375 type_id_list = empty_except_spec;
15377 /* Look for the `)'. */
15378 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15380 return type_id_list;
15383 /* Parse an (optional) type-id-list.
15387 type-id-list , type-id ... [opt]
15389 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15390 in the order that the types were presented. */
15393 cp_parser_type_id_list (cp_parser* parser)
15395 tree types = NULL_TREE;
15402 /* Get the next type-id. */
15403 type = cp_parser_type_id (parser);
15404 /* Parse the optional ellipsis. */
15405 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15407 /* Consume the `...'. */
15408 cp_lexer_consume_token (parser->lexer);
15410 /* Turn the type into a pack expansion expression. */
15411 type = make_pack_expansion (type);
15413 /* Add it to the list. */
15414 types = add_exception_specifier (types, type, /*complain=*/1);
15415 /* Peek at the next token. */
15416 token = cp_lexer_peek_token (parser->lexer);
15417 /* If it is not a `,', we are done. */
15418 if (token->type != CPP_COMMA)
15420 /* Consume the `,'. */
15421 cp_lexer_consume_token (parser->lexer);
15424 return nreverse (types);
15427 /* Parse a try-block.
15430 try compound-statement handler-seq */
15433 cp_parser_try_block (cp_parser* parser)
15437 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15438 try_block = begin_try_block ();
15439 cp_parser_compound_statement (parser, NULL, true);
15440 finish_try_block (try_block);
15441 cp_parser_handler_seq (parser);
15442 finish_handler_sequence (try_block);
15447 /* Parse a function-try-block.
15449 function-try-block:
15450 try ctor-initializer [opt] function-body handler-seq */
15453 cp_parser_function_try_block (cp_parser* parser)
15455 tree compound_stmt;
15457 bool ctor_initializer_p;
15459 /* Look for the `try' keyword. */
15460 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15462 /* Let the rest of the front end know where we are. */
15463 try_block = begin_function_try_block (&compound_stmt);
15464 /* Parse the function-body. */
15466 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15467 /* We're done with the `try' part. */
15468 finish_function_try_block (try_block);
15469 /* Parse the handlers. */
15470 cp_parser_handler_seq (parser);
15471 /* We're done with the handlers. */
15472 finish_function_handler_sequence (try_block, compound_stmt);
15474 return ctor_initializer_p;
15477 /* Parse a handler-seq.
15480 handler handler-seq [opt] */
15483 cp_parser_handler_seq (cp_parser* parser)
15489 /* Parse the handler. */
15490 cp_parser_handler (parser);
15491 /* Peek at the next token. */
15492 token = cp_lexer_peek_token (parser->lexer);
15493 /* If it's not `catch' then there are no more handlers. */
15494 if (!cp_parser_is_keyword (token, RID_CATCH))
15499 /* Parse a handler.
15502 catch ( exception-declaration ) compound-statement */
15505 cp_parser_handler (cp_parser* parser)
15510 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15511 handler = begin_handler ();
15512 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15513 declaration = cp_parser_exception_declaration (parser);
15514 finish_handler_parms (declaration, handler);
15515 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15516 cp_parser_compound_statement (parser, NULL, false);
15517 finish_handler (handler);
15520 /* Parse an exception-declaration.
15522 exception-declaration:
15523 type-specifier-seq declarator
15524 type-specifier-seq abstract-declarator
15528 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15529 ellipsis variant is used. */
15532 cp_parser_exception_declaration (cp_parser* parser)
15534 cp_decl_specifier_seq type_specifiers;
15535 cp_declarator *declarator;
15536 const char *saved_message;
15538 /* If it's an ellipsis, it's easy to handle. */
15539 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15541 /* Consume the `...' token. */
15542 cp_lexer_consume_token (parser->lexer);
15546 /* Types may not be defined in exception-declarations. */
15547 saved_message = parser->type_definition_forbidden_message;
15548 parser->type_definition_forbidden_message
15549 = "types may not be defined in exception-declarations";
15551 /* Parse the type-specifier-seq. */
15552 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15554 /* If it's a `)', then there is no declarator. */
15555 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15558 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15559 /*ctor_dtor_or_conv_p=*/NULL,
15560 /*parenthesized_p=*/NULL,
15561 /*member_p=*/false);
15563 /* Restore the saved message. */
15564 parser->type_definition_forbidden_message = saved_message;
15566 if (!type_specifiers.any_specifiers_p)
15567 return error_mark_node;
15569 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15572 /* Parse a throw-expression.
15575 throw assignment-expression [opt]
15577 Returns a THROW_EXPR representing the throw-expression. */
15580 cp_parser_throw_expression (cp_parser* parser)
15585 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15586 token = cp_lexer_peek_token (parser->lexer);
15587 /* Figure out whether or not there is an assignment-expression
15588 following the "throw" keyword. */
15589 if (token->type == CPP_COMMA
15590 || token->type == CPP_SEMICOLON
15591 || token->type == CPP_CLOSE_PAREN
15592 || token->type == CPP_CLOSE_SQUARE
15593 || token->type == CPP_CLOSE_BRACE
15594 || token->type == CPP_COLON)
15595 expression = NULL_TREE;
15597 expression = cp_parser_assignment_expression (parser,
15600 return build_throw (expression);
15603 /* GNU Extensions */
15605 /* Parse an (optional) asm-specification.
15608 asm ( string-literal )
15610 If the asm-specification is present, returns a STRING_CST
15611 corresponding to the string-literal. Otherwise, returns
15615 cp_parser_asm_specification_opt (cp_parser* parser)
15618 tree asm_specification;
15620 /* Peek at the next token. */
15621 token = cp_lexer_peek_token (parser->lexer);
15622 /* If the next token isn't the `asm' keyword, then there's no
15623 asm-specification. */
15624 if (!cp_parser_is_keyword (token, RID_ASM))
15627 /* Consume the `asm' token. */
15628 cp_lexer_consume_token (parser->lexer);
15629 /* Look for the `('. */
15630 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15632 /* Look for the string-literal. */
15633 asm_specification = cp_parser_string_literal (parser, false, false);
15635 /* Look for the `)'. */
15636 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15638 return asm_specification;
15641 /* Parse an asm-operand-list.
15645 asm-operand-list , asm-operand
15648 string-literal ( expression )
15649 [ string-literal ] string-literal ( expression )
15651 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15652 each node is the expression. The TREE_PURPOSE is itself a
15653 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15654 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15655 is a STRING_CST for the string literal before the parenthesis. Returns
15656 ERROR_MARK_NODE if any of the operands are invalid. */
15659 cp_parser_asm_operand_list (cp_parser* parser)
15661 tree asm_operands = NULL_TREE;
15662 bool invalid_operands = false;
15666 tree string_literal;
15670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15672 /* Consume the `[' token. */
15673 cp_lexer_consume_token (parser->lexer);
15674 /* Read the operand name. */
15675 name = cp_parser_identifier (parser);
15676 if (name != error_mark_node)
15677 name = build_string (IDENTIFIER_LENGTH (name),
15678 IDENTIFIER_POINTER (name));
15679 /* Look for the closing `]'. */
15680 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15684 /* Look for the string-literal. */
15685 string_literal = cp_parser_string_literal (parser, false, false);
15687 /* Look for the `('. */
15688 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15689 /* Parse the expression. */
15690 expression = cp_parser_expression (parser, /*cast_p=*/false);
15691 /* Look for the `)'. */
15692 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15694 if (name == error_mark_node
15695 || string_literal == error_mark_node
15696 || expression == error_mark_node)
15697 invalid_operands = true;
15699 /* Add this operand to the list. */
15700 asm_operands = tree_cons (build_tree_list (name, string_literal),
15703 /* If the next token is not a `,', there are no more
15705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15707 /* Consume the `,'. */
15708 cp_lexer_consume_token (parser->lexer);
15711 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15714 /* Parse an asm-clobber-list.
15718 asm-clobber-list , string-literal
15720 Returns a TREE_LIST, indicating the clobbers in the order that they
15721 appeared. The TREE_VALUE of each node is a STRING_CST. */
15724 cp_parser_asm_clobber_list (cp_parser* parser)
15726 tree clobbers = NULL_TREE;
15730 tree string_literal;
15732 /* Look for the string literal. */
15733 string_literal = cp_parser_string_literal (parser, false, false);
15734 /* Add it to the list. */
15735 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15736 /* If the next token is not a `,', then the list is
15738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15740 /* Consume the `,' token. */
15741 cp_lexer_consume_token (parser->lexer);
15747 /* Parse an (optional) series of attributes.
15750 attributes attribute
15753 __attribute__ (( attribute-list [opt] ))
15755 The return value is as for cp_parser_attribute_list. */
15758 cp_parser_attributes_opt (cp_parser* parser)
15760 tree attributes = NULL_TREE;
15765 tree attribute_list;
15767 /* Peek at the next token. */
15768 token = cp_lexer_peek_token (parser->lexer);
15769 /* If it's not `__attribute__', then we're done. */
15770 if (token->keyword != RID_ATTRIBUTE)
15773 /* Consume the `__attribute__' keyword. */
15774 cp_lexer_consume_token (parser->lexer);
15775 /* Look for the two `(' tokens. */
15776 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15777 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15779 /* Peek at the next token. */
15780 token = cp_lexer_peek_token (parser->lexer);
15781 if (token->type != CPP_CLOSE_PAREN)
15782 /* Parse the attribute-list. */
15783 attribute_list = cp_parser_attribute_list (parser);
15785 /* If the next token is a `)', then there is no attribute
15787 attribute_list = NULL;
15789 /* Look for the two `)' tokens. */
15790 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15791 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15793 /* Add these new attributes to the list. */
15794 attributes = chainon (attributes, attribute_list);
15800 /* Parse an attribute-list.
15804 attribute-list , attribute
15808 identifier ( identifier )
15809 identifier ( identifier , expression-list )
15810 identifier ( expression-list )
15812 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15813 to an attribute. The TREE_PURPOSE of each node is the identifier
15814 indicating which attribute is in use. The TREE_VALUE represents
15815 the arguments, if any. */
15818 cp_parser_attribute_list (cp_parser* parser)
15820 tree attribute_list = NULL_TREE;
15821 bool save_translate_strings_p = parser->translate_strings_p;
15823 parser->translate_strings_p = false;
15830 /* Look for the identifier. We also allow keywords here; for
15831 example `__attribute__ ((const))' is legal. */
15832 token = cp_lexer_peek_token (parser->lexer);
15833 if (token->type == CPP_NAME
15834 || token->type == CPP_KEYWORD)
15836 tree arguments = NULL_TREE;
15838 /* Consume the token. */
15839 token = cp_lexer_consume_token (parser->lexer);
15841 /* Save away the identifier that indicates which attribute
15843 identifier = token->u.value;
15844 attribute = build_tree_list (identifier, NULL_TREE);
15846 /* Peek at the next token. */
15847 token = cp_lexer_peek_token (parser->lexer);
15848 /* If it's an `(', then parse the attribute arguments. */
15849 if (token->type == CPP_OPEN_PAREN)
15851 arguments = cp_parser_parenthesized_expression_list
15852 (parser, true, /*cast_p=*/false,
15853 /*allow_expansion_p=*/false,
15854 /*non_constant_p=*/NULL);
15855 /* Save the arguments away. */
15856 TREE_VALUE (attribute) = arguments;
15859 if (arguments != error_mark_node)
15861 /* Add this attribute to the list. */
15862 TREE_CHAIN (attribute) = attribute_list;
15863 attribute_list = attribute;
15866 token = cp_lexer_peek_token (parser->lexer);
15868 /* Now, look for more attributes. If the next token isn't a
15869 `,', we're done. */
15870 if (token->type != CPP_COMMA)
15873 /* Consume the comma and keep going. */
15874 cp_lexer_consume_token (parser->lexer);
15876 parser->translate_strings_p = save_translate_strings_p;
15878 /* We built up the list in reverse order. */
15879 return nreverse (attribute_list);
15882 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15883 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15884 current value of the PEDANTIC flag, regardless of whether or not
15885 the `__extension__' keyword is present. The caller is responsible
15886 for restoring the value of the PEDANTIC flag. */
15889 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15891 /* Save the old value of the PEDANTIC flag. */
15892 *saved_pedantic = pedantic;
15894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15896 /* Consume the `__extension__' token. */
15897 cp_lexer_consume_token (parser->lexer);
15898 /* We're not being pedantic while the `__extension__' keyword is
15908 /* Parse a label declaration.
15911 __label__ label-declarator-seq ;
15913 label-declarator-seq:
15914 identifier , label-declarator-seq
15918 cp_parser_label_declaration (cp_parser* parser)
15920 /* Look for the `__label__' keyword. */
15921 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15927 /* Look for an identifier. */
15928 identifier = cp_parser_identifier (parser);
15929 /* If we failed, stop. */
15930 if (identifier == error_mark_node)
15932 /* Declare it as a label. */
15933 finish_label_decl (identifier);
15934 /* If the next token is a `;', stop. */
15935 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15937 /* Look for the `,' separating the label declarations. */
15938 cp_parser_require (parser, CPP_COMMA, "`,'");
15941 /* Look for the final `;'. */
15942 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15945 /* Support Functions */
15947 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15948 NAME should have one of the representations used for an
15949 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15950 is returned. If PARSER->SCOPE is a dependent type, then a
15951 SCOPE_REF is returned.
15953 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15954 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15955 was formed. Abstractly, such entities should not be passed to this
15956 function, because they do not need to be looked up, but it is
15957 simpler to check for this special case here, rather than at the
15960 In cases not explicitly covered above, this function returns a
15961 DECL, OVERLOAD, or baselink representing the result of the lookup.
15962 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15965 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15966 (e.g., "struct") that was used. In that case bindings that do not
15967 refer to types are ignored.
15969 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15972 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15975 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15978 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15979 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15980 NULL_TREE otherwise. */
15983 cp_parser_lookup_name (cp_parser *parser, tree name,
15984 enum tag_types tag_type,
15987 bool check_dependency,
15988 tree *ambiguous_decls)
15992 tree object_type = parser->context->object_type;
15994 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15995 flags |= LOOKUP_COMPLAIN;
15997 /* Assume that the lookup will be unambiguous. */
15998 if (ambiguous_decls)
15999 *ambiguous_decls = NULL_TREE;
16001 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16002 no longer valid. Note that if we are parsing tentatively, and
16003 the parse fails, OBJECT_TYPE will be automatically restored. */
16004 parser->context->object_type = NULL_TREE;
16006 if (name == error_mark_node)
16007 return error_mark_node;
16009 /* A template-id has already been resolved; there is no lookup to
16011 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16013 if (BASELINK_P (name))
16015 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16016 == TEMPLATE_ID_EXPR);
16020 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16021 it should already have been checked to make sure that the name
16022 used matches the type being destroyed. */
16023 if (TREE_CODE (name) == BIT_NOT_EXPR)
16027 /* Figure out to which type this destructor applies. */
16029 type = parser->scope;
16030 else if (object_type)
16031 type = object_type;
16033 type = current_class_type;
16034 /* If that's not a class type, there is no destructor. */
16035 if (!type || !CLASS_TYPE_P (type))
16036 return error_mark_node;
16037 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16038 lazily_declare_fn (sfk_destructor, type);
16039 if (!CLASSTYPE_DESTRUCTORS (type))
16040 return error_mark_node;
16041 /* If it was a class type, return the destructor. */
16042 return CLASSTYPE_DESTRUCTORS (type);
16045 /* By this point, the NAME should be an ordinary identifier. If
16046 the id-expression was a qualified name, the qualifying scope is
16047 stored in PARSER->SCOPE at this point. */
16048 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16050 /* Perform the lookup. */
16055 if (parser->scope == error_mark_node)
16056 return error_mark_node;
16058 /* If the SCOPE is dependent, the lookup must be deferred until
16059 the template is instantiated -- unless we are explicitly
16060 looking up names in uninstantiated templates. Even then, we
16061 cannot look up the name if the scope is not a class type; it
16062 might, for example, be a template type parameter. */
16063 dependent_p = (TYPE_P (parser->scope)
16064 && !(parser->in_declarator_p
16065 && currently_open_class (parser->scope))
16066 && dependent_type_p (parser->scope));
16067 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16074 /* The resolution to Core Issue 180 says that `struct
16075 A::B' should be considered a type-name, even if `A'
16077 type = make_typename_type (parser->scope, name, tag_type,
16078 /*complain=*/tf_error);
16079 decl = TYPE_NAME (type);
16081 else if (is_template
16082 && (cp_parser_next_token_ends_template_argument_p (parser)
16083 || cp_lexer_next_token_is (parser->lexer,
16085 decl = make_unbound_class_template (parser->scope,
16087 /*complain=*/tf_error);
16089 decl = build_qualified_name (/*type=*/NULL_TREE,
16090 parser->scope, name,
16095 tree pushed_scope = NULL_TREE;
16097 /* If PARSER->SCOPE is a dependent type, then it must be a
16098 class type, and we must not be checking dependencies;
16099 otherwise, we would have processed this lookup above. So
16100 that PARSER->SCOPE is not considered a dependent base by
16101 lookup_member, we must enter the scope here. */
16103 pushed_scope = push_scope (parser->scope);
16104 /* If the PARSER->SCOPE is a template specialization, it
16105 may be instantiated during name lookup. In that case,
16106 errors may be issued. Even if we rollback the current
16107 tentative parse, those errors are valid. */
16108 decl = lookup_qualified_name (parser->scope, name,
16109 tag_type != none_type,
16110 /*complain=*/true);
16112 pop_scope (pushed_scope);
16114 parser->qualifying_scope = parser->scope;
16115 parser->object_scope = NULL_TREE;
16117 else if (object_type)
16119 tree object_decl = NULL_TREE;
16120 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16121 OBJECT_TYPE is not a class. */
16122 if (CLASS_TYPE_P (object_type))
16123 /* If the OBJECT_TYPE is a template specialization, it may
16124 be instantiated during name lookup. In that case, errors
16125 may be issued. Even if we rollback the current tentative
16126 parse, those errors are valid. */
16127 object_decl = lookup_member (object_type,
16130 tag_type != none_type);
16131 /* Look it up in the enclosing context, too. */
16132 decl = lookup_name_real (name, tag_type != none_type,
16134 /*block_p=*/true, is_namespace, flags);
16135 parser->object_scope = object_type;
16136 parser->qualifying_scope = NULL_TREE;
16138 decl = object_decl;
16142 decl = lookup_name_real (name, tag_type != none_type,
16144 /*block_p=*/true, is_namespace, flags);
16145 parser->qualifying_scope = NULL_TREE;
16146 parser->object_scope = NULL_TREE;
16149 /* If the lookup failed, let our caller know. */
16150 if (!decl || decl == error_mark_node)
16151 return error_mark_node;
16153 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16154 if (TREE_CODE (decl) == TREE_LIST)
16156 if (ambiguous_decls)
16157 *ambiguous_decls = decl;
16158 /* The error message we have to print is too complicated for
16159 cp_parser_error, so we incorporate its actions directly. */
16160 if (!cp_parser_simulate_error (parser))
16162 error ("reference to %qD is ambiguous", name);
16163 print_candidates (decl);
16165 return error_mark_node;
16168 gcc_assert (DECL_P (decl)
16169 || TREE_CODE (decl) == OVERLOAD
16170 || TREE_CODE (decl) == SCOPE_REF
16171 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16172 || BASELINK_P (decl));
16174 /* If we have resolved the name of a member declaration, check to
16175 see if the declaration is accessible. When the name resolves to
16176 set of overloaded functions, accessibility is checked when
16177 overload resolution is done.
16179 During an explicit instantiation, access is not checked at all,
16180 as per [temp.explicit]. */
16182 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16187 /* Like cp_parser_lookup_name, but for use in the typical case where
16188 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16189 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16192 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16194 return cp_parser_lookup_name (parser, name,
16196 /*is_template=*/false,
16197 /*is_namespace=*/false,
16198 /*check_dependency=*/true,
16199 /*ambiguous_decls=*/NULL);
16202 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16203 the current context, return the TYPE_DECL. If TAG_NAME_P is
16204 true, the DECL indicates the class being defined in a class-head,
16205 or declared in an elaborated-type-specifier.
16207 Otherwise, return DECL. */
16210 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16212 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16213 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16216 template <typename T> struct B;
16219 template <typename T> struct A::B {};
16221 Similarly, in an elaborated-type-specifier:
16223 namespace N { struct X{}; }
16226 template <typename T> friend struct N::X;
16229 However, if the DECL refers to a class type, and we are in
16230 the scope of the class, then the name lookup automatically
16231 finds the TYPE_DECL created by build_self_reference rather
16232 than a TEMPLATE_DECL. For example, in:
16234 template <class T> struct S {
16238 there is no need to handle such case. */
16240 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16241 return DECL_TEMPLATE_RESULT (decl);
16246 /* If too many, or too few, template-parameter lists apply to the
16247 declarator, issue an error message. Returns TRUE if all went well,
16248 and FALSE otherwise. */
16251 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16252 cp_declarator *declarator)
16254 unsigned num_templates;
16256 /* We haven't seen any classes that involve template parameters yet. */
16259 switch (declarator->kind)
16262 if (declarator->u.id.qualifying_scope)
16267 scope = declarator->u.id.qualifying_scope;
16268 member = declarator->u.id.unqualified_name;
16270 while (scope && CLASS_TYPE_P (scope))
16272 /* You're supposed to have one `template <...>'
16273 for every template class, but you don't need one
16274 for a full specialization. For example:
16276 template <class T> struct S{};
16277 template <> struct S<int> { void f(); };
16278 void S<int>::f () {}
16280 is correct; there shouldn't be a `template <>' for
16281 the definition of `S<int>::f'. */
16282 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16283 /* If SCOPE does not have template information of any
16284 kind, then it is not a template, nor is it nested
16285 within a template. */
16287 if (explicit_class_specialization_p (scope))
16289 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16292 scope = TYPE_CONTEXT (scope);
16295 else if (TREE_CODE (declarator->u.id.unqualified_name)
16296 == TEMPLATE_ID_EXPR)
16297 /* If the DECLARATOR has the form `X<y>' then it uses one
16298 additional level of template parameters. */
16301 return cp_parser_check_template_parameters (parser,
16307 case cdk_reference:
16309 return (cp_parser_check_declarator_template_parameters
16310 (parser, declarator->declarator));
16316 gcc_unreachable ();
16321 /* NUM_TEMPLATES were used in the current declaration. If that is
16322 invalid, return FALSE and issue an error messages. Otherwise,
16326 cp_parser_check_template_parameters (cp_parser* parser,
16327 unsigned num_templates)
16329 /* If there are more template classes than parameter lists, we have
16332 template <class T> void S<T>::R<T>::f (); */
16333 if (parser->num_template_parameter_lists < num_templates)
16335 error ("too few template-parameter-lists");
16338 /* If there are the same number of template classes and parameter
16339 lists, that's OK. */
16340 if (parser->num_template_parameter_lists == num_templates)
16342 /* If there are more, but only one more, then we are referring to a
16343 member template. That's OK too. */
16344 if (parser->num_template_parameter_lists == num_templates + 1)
16346 /* Otherwise, there are too many template parameter lists. We have
16349 template <class T> template <class U> void S::f(); */
16350 error ("too many template-parameter-lists");
16354 /* Parse an optional `::' token indicating that the following name is
16355 from the global namespace. If so, PARSER->SCOPE is set to the
16356 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16357 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16358 Returns the new value of PARSER->SCOPE, if the `::' token is
16359 present, and NULL_TREE otherwise. */
16362 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16366 /* Peek at the next token. */
16367 token = cp_lexer_peek_token (parser->lexer);
16368 /* If we're looking at a `::' token then we're starting from the
16369 global namespace, not our current location. */
16370 if (token->type == CPP_SCOPE)
16372 /* Consume the `::' token. */
16373 cp_lexer_consume_token (parser->lexer);
16374 /* Set the SCOPE so that we know where to start the lookup. */
16375 parser->scope = global_namespace;
16376 parser->qualifying_scope = global_namespace;
16377 parser->object_scope = NULL_TREE;
16379 return parser->scope;
16381 else if (!current_scope_valid_p)
16383 parser->scope = NULL_TREE;
16384 parser->qualifying_scope = NULL_TREE;
16385 parser->object_scope = NULL_TREE;
16391 /* Returns TRUE if the upcoming token sequence is the start of a
16392 constructor declarator. If FRIEND_P is true, the declarator is
16393 preceded by the `friend' specifier. */
16396 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16398 bool constructor_p;
16399 tree type_decl = NULL_TREE;
16400 bool nested_name_p;
16401 cp_token *next_token;
16403 /* The common case is that this is not a constructor declarator, so
16404 try to avoid doing lots of work if at all possible. It's not
16405 valid declare a constructor at function scope. */
16406 if (parser->in_function_body)
16408 /* And only certain tokens can begin a constructor declarator. */
16409 next_token = cp_lexer_peek_token (parser->lexer);
16410 if (next_token->type != CPP_NAME
16411 && next_token->type != CPP_SCOPE
16412 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16413 && next_token->type != CPP_TEMPLATE_ID)
16416 /* Parse tentatively; we are going to roll back all of the tokens
16418 cp_parser_parse_tentatively (parser);
16419 /* Assume that we are looking at a constructor declarator. */
16420 constructor_p = true;
16422 /* Look for the optional `::' operator. */
16423 cp_parser_global_scope_opt (parser,
16424 /*current_scope_valid_p=*/false);
16425 /* Look for the nested-name-specifier. */
16427 = (cp_parser_nested_name_specifier_opt (parser,
16428 /*typename_keyword_p=*/false,
16429 /*check_dependency_p=*/false,
16431 /*is_declaration=*/false)
16433 /* Outside of a class-specifier, there must be a
16434 nested-name-specifier. */
16435 if (!nested_name_p &&
16436 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16438 constructor_p = false;
16439 /* If we still think that this might be a constructor-declarator,
16440 look for a class-name. */
16445 template <typename T> struct S { S(); };
16446 template <typename T> S<T>::S ();
16448 we must recognize that the nested `S' names a class.
16451 template <typename T> S<T>::S<T> ();
16453 we must recognize that the nested `S' names a template. */
16454 type_decl = cp_parser_class_name (parser,
16455 /*typename_keyword_p=*/false,
16456 /*template_keyword_p=*/false,
16458 /*check_dependency_p=*/false,
16459 /*class_head_p=*/false,
16460 /*is_declaration=*/false);
16461 /* If there was no class-name, then this is not a constructor. */
16462 constructor_p = !cp_parser_error_occurred (parser);
16465 /* If we're still considering a constructor, we have to see a `(',
16466 to begin the parameter-declaration-clause, followed by either a
16467 `)', an `...', or a decl-specifier. We need to check for a
16468 type-specifier to avoid being fooled into thinking that:
16472 is a constructor. (It is actually a function named `f' that
16473 takes one parameter (of type `int') and returns a value of type
16476 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16479 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16480 /* A parameter declaration begins with a decl-specifier,
16481 which is either the "attribute" keyword, a storage class
16482 specifier, or (usually) a type-specifier. */
16483 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16486 tree pushed_scope = NULL_TREE;
16487 unsigned saved_num_template_parameter_lists;
16489 /* Names appearing in the type-specifier should be looked up
16490 in the scope of the class. */
16491 if (current_class_type)
16495 type = TREE_TYPE (type_decl);
16496 if (TREE_CODE (type) == TYPENAME_TYPE)
16498 type = resolve_typename_type (type,
16499 /*only_current_p=*/false);
16500 if (TREE_CODE (type) == TYPENAME_TYPE)
16502 cp_parser_abort_tentative_parse (parser);
16506 pushed_scope = push_scope (type);
16509 /* Inside the constructor parameter list, surrounding
16510 template-parameter-lists do not apply. */
16511 saved_num_template_parameter_lists
16512 = parser->num_template_parameter_lists;
16513 parser->num_template_parameter_lists = 0;
16515 /* Look for the type-specifier. */
16516 cp_parser_type_specifier (parser,
16517 CP_PARSER_FLAGS_NONE,
16518 /*decl_specs=*/NULL,
16519 /*is_declarator=*/true,
16520 /*declares_class_or_enum=*/NULL,
16521 /*is_cv_qualifier=*/NULL);
16523 parser->num_template_parameter_lists
16524 = saved_num_template_parameter_lists;
16526 /* Leave the scope of the class. */
16528 pop_scope (pushed_scope);
16530 constructor_p = !cp_parser_error_occurred (parser);
16534 constructor_p = false;
16535 /* We did not really want to consume any tokens. */
16536 cp_parser_abort_tentative_parse (parser);
16538 return constructor_p;
16541 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16542 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16543 they must be performed once we are in the scope of the function.
16545 Returns the function defined. */
16548 cp_parser_function_definition_from_specifiers_and_declarator
16549 (cp_parser* parser,
16550 cp_decl_specifier_seq *decl_specifiers,
16552 const cp_declarator *declarator)
16557 /* Begin the function-definition. */
16558 success_p = start_function (decl_specifiers, declarator, attributes);
16560 /* The things we're about to see are not directly qualified by any
16561 template headers we've seen thus far. */
16562 reset_specialization ();
16564 /* If there were names looked up in the decl-specifier-seq that we
16565 did not check, check them now. We must wait until we are in the
16566 scope of the function to perform the checks, since the function
16567 might be a friend. */
16568 perform_deferred_access_checks ();
16572 /* Skip the entire function. */
16573 cp_parser_skip_to_end_of_block_or_statement (parser);
16574 fn = error_mark_node;
16576 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16578 /* Seen already, skip it. An error message has already been output. */
16579 cp_parser_skip_to_end_of_block_or_statement (parser);
16580 fn = current_function_decl;
16581 current_function_decl = NULL_TREE;
16582 /* If this is a function from a class, pop the nested class. */
16583 if (current_class_name)
16584 pop_nested_class ();
16587 fn = cp_parser_function_definition_after_declarator (parser,
16588 /*inline_p=*/false);
16593 /* Parse the part of a function-definition that follows the
16594 declarator. INLINE_P is TRUE iff this function is an inline
16595 function defined with a class-specifier.
16597 Returns the function defined. */
16600 cp_parser_function_definition_after_declarator (cp_parser* parser,
16604 bool ctor_initializer_p = false;
16605 bool saved_in_unbraced_linkage_specification_p;
16606 bool saved_in_function_body;
16607 unsigned saved_num_template_parameter_lists;
16609 saved_in_function_body = parser->in_function_body;
16610 parser->in_function_body = true;
16611 /* If the next token is `return', then the code may be trying to
16612 make use of the "named return value" extension that G++ used to
16614 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16616 /* Consume the `return' keyword. */
16617 cp_lexer_consume_token (parser->lexer);
16618 /* Look for the identifier that indicates what value is to be
16620 cp_parser_identifier (parser);
16621 /* Issue an error message. */
16622 error ("named return values are no longer supported");
16623 /* Skip tokens until we reach the start of the function body. */
16626 cp_token *token = cp_lexer_peek_token (parser->lexer);
16627 if (token->type == CPP_OPEN_BRACE
16628 || token->type == CPP_EOF
16629 || token->type == CPP_PRAGMA_EOL)
16631 cp_lexer_consume_token (parser->lexer);
16634 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16635 anything declared inside `f'. */
16636 saved_in_unbraced_linkage_specification_p
16637 = parser->in_unbraced_linkage_specification_p;
16638 parser->in_unbraced_linkage_specification_p = false;
16639 /* Inside the function, surrounding template-parameter-lists do not
16641 saved_num_template_parameter_lists
16642 = parser->num_template_parameter_lists;
16643 parser->num_template_parameter_lists = 0;
16644 /* If the next token is `try', then we are looking at a
16645 function-try-block. */
16646 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16647 ctor_initializer_p = cp_parser_function_try_block (parser);
16648 /* A function-try-block includes the function-body, so we only do
16649 this next part if we're not processing a function-try-block. */
16652 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16654 /* Finish the function. */
16655 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16656 (inline_p ? 2 : 0));
16657 /* Generate code for it, if necessary. */
16658 expand_or_defer_fn (fn);
16659 /* Restore the saved values. */
16660 parser->in_unbraced_linkage_specification_p
16661 = saved_in_unbraced_linkage_specification_p;
16662 parser->num_template_parameter_lists
16663 = saved_num_template_parameter_lists;
16664 parser->in_function_body = saved_in_function_body;
16669 /* Parse a template-declaration, assuming that the `export' (and
16670 `extern') keywords, if present, has already been scanned. MEMBER_P
16671 is as for cp_parser_template_declaration. */
16674 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16676 tree decl = NULL_TREE;
16677 VEC (deferred_access_check,gc) *checks;
16678 tree parameter_list;
16679 bool friend_p = false;
16680 bool need_lang_pop;
16682 /* Look for the `template' keyword. */
16683 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16687 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16689 if (at_class_scope_p () && current_function_decl)
16691 /* 14.5.2.2 [temp.mem]
16693 A local class shall not have member templates. */
16694 error ("invalid declaration of member template in local class");
16695 cp_parser_skip_to_end_of_block_or_statement (parser);
16700 A template ... shall not have C linkage. */
16701 if (current_lang_name == lang_name_c)
16703 error ("template with C linkage");
16704 /* Give it C++ linkage to avoid confusing other parts of the
16706 push_lang_context (lang_name_cplusplus);
16707 need_lang_pop = true;
16710 need_lang_pop = false;
16712 /* We cannot perform access checks on the template parameter
16713 declarations until we know what is being declared, just as we
16714 cannot check the decl-specifier list. */
16715 push_deferring_access_checks (dk_deferred);
16717 /* If the next token is `>', then we have an invalid
16718 specialization. Rather than complain about an invalid template
16719 parameter, issue an error message here. */
16720 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16722 cp_parser_error (parser, "invalid explicit specialization");
16723 begin_specialization ();
16724 parameter_list = NULL_TREE;
16727 /* Parse the template parameters. */
16728 parameter_list = cp_parser_template_parameter_list (parser);
16730 /* Get the deferred access checks from the parameter list. These
16731 will be checked once we know what is being declared, as for a
16732 member template the checks must be performed in the scope of the
16733 class containing the member. */
16734 checks = get_deferred_access_checks ();
16736 /* Look for the `>'. */
16737 cp_parser_skip_to_end_of_template_parameter_list (parser);
16738 /* We just processed one more parameter list. */
16739 ++parser->num_template_parameter_lists;
16740 /* If the next token is `template', there are more template
16742 if (cp_lexer_next_token_is_keyword (parser->lexer,
16744 cp_parser_template_declaration_after_export (parser, member_p);
16747 /* There are no access checks when parsing a template, as we do not
16748 know if a specialization will be a friend. */
16749 push_deferring_access_checks (dk_no_check);
16750 decl = cp_parser_single_declaration (parser,
16753 /*explicit_specialization_p=*/false,
16755 pop_deferring_access_checks ();
16757 /* If this is a member template declaration, let the front
16759 if (member_p && !friend_p && decl)
16761 if (TREE_CODE (decl) == TYPE_DECL)
16762 cp_parser_check_access_in_redeclaration (decl);
16764 decl = finish_member_template_decl (decl);
16766 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16767 make_friend_class (current_class_type, TREE_TYPE (decl),
16768 /*complain=*/true);
16770 /* We are done with the current parameter list. */
16771 --parser->num_template_parameter_lists;
16773 pop_deferring_access_checks ();
16776 finish_template_decl (parameter_list);
16778 /* Register member declarations. */
16779 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16780 finish_member_declaration (decl);
16781 /* For the erroneous case of a template with C linkage, we pushed an
16782 implicit C++ linkage scope; exit that scope now. */
16784 pop_lang_context ();
16785 /* If DECL is a function template, we must return to parse it later.
16786 (Even though there is no definition, there might be default
16787 arguments that need handling.) */
16788 if (member_p && decl
16789 && (TREE_CODE (decl) == FUNCTION_DECL
16790 || DECL_FUNCTION_TEMPLATE_P (decl)))
16791 TREE_VALUE (parser->unparsed_functions_queues)
16792 = tree_cons (NULL_TREE, decl,
16793 TREE_VALUE (parser->unparsed_functions_queues));
16796 /* Perform the deferred access checks from a template-parameter-list.
16797 CHECKS is a TREE_LIST of access checks, as returned by
16798 get_deferred_access_checks. */
16801 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16803 ++processing_template_parmlist;
16804 perform_access_checks (checks);
16805 --processing_template_parmlist;
16808 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16809 `function-definition' sequence. MEMBER_P is true, this declaration
16810 appears in a class scope.
16812 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16813 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16816 cp_parser_single_declaration (cp_parser* parser,
16817 VEC (deferred_access_check,gc)* checks,
16819 bool explicit_specialization_p,
16822 int declares_class_or_enum;
16823 tree decl = NULL_TREE;
16824 cp_decl_specifier_seq decl_specifiers;
16825 bool function_definition_p = false;
16827 /* This function is only used when processing a template
16829 gcc_assert (innermost_scope_kind () == sk_template_parms
16830 || innermost_scope_kind () == sk_template_spec);
16832 /* Defer access checks until we know what is being declared. */
16833 push_deferring_access_checks (dk_deferred);
16835 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16837 cp_parser_decl_specifier_seq (parser,
16838 CP_PARSER_FLAGS_OPTIONAL,
16840 &declares_class_or_enum);
16842 *friend_p = cp_parser_friend_p (&decl_specifiers);
16844 /* There are no template typedefs. */
16845 if (decl_specifiers.specs[(int) ds_typedef])
16847 error ("template declaration of %qs", "typedef");
16848 decl = error_mark_node;
16851 /* Gather up the access checks that occurred the
16852 decl-specifier-seq. */
16853 stop_deferring_access_checks ();
16855 /* Check for the declaration of a template class. */
16856 if (declares_class_or_enum)
16858 if (cp_parser_declares_only_class_p (parser))
16860 decl = shadow_tag (&decl_specifiers);
16865 friend template <typename T> struct A<T>::B;
16868 A<T>::B will be represented by a TYPENAME_TYPE, and
16869 therefore not recognized by shadow_tag. */
16870 if (friend_p && *friend_p
16872 && decl_specifiers.type
16873 && TYPE_P (decl_specifiers.type))
16874 decl = decl_specifiers.type;
16876 if (decl && decl != error_mark_node)
16877 decl = TYPE_NAME (decl);
16879 decl = error_mark_node;
16881 /* Perform access checks for template parameters. */
16882 cp_parser_perform_template_parameter_access_checks (checks);
16885 /* If it's not a template class, try for a template function. If
16886 the next token is a `;', then this declaration does not declare
16887 anything. But, if there were errors in the decl-specifiers, then
16888 the error might well have come from an attempted class-specifier.
16889 In that case, there's no need to warn about a missing declarator. */
16891 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16892 || decl_specifiers.type != error_mark_node))
16894 decl = cp_parser_init_declarator (parser,
16897 /*function_definition_allowed_p=*/true,
16899 declares_class_or_enum,
16900 &function_definition_p);
16902 /* 7.1.1-1 [dcl.stc]
16904 A storage-class-specifier shall not be specified in an explicit
16905 specialization... */
16907 && explicit_specialization_p
16908 && decl_specifiers.storage_class != sc_none)
16910 error ("explicit template specialization cannot have a storage class");
16911 decl = error_mark_node;
16915 pop_deferring_access_checks ();
16917 /* Clear any current qualification; whatever comes next is the start
16918 of something new. */
16919 parser->scope = NULL_TREE;
16920 parser->qualifying_scope = NULL_TREE;
16921 parser->object_scope = NULL_TREE;
16922 /* Look for a trailing `;' after the declaration. */
16923 if (!function_definition_p
16924 && (decl == error_mark_node
16925 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16926 cp_parser_skip_to_end_of_block_or_statement (parser);
16931 /* Parse a cast-expression that is not the operand of a unary "&". */
16934 cp_parser_simple_cast_expression (cp_parser *parser)
16936 return cp_parser_cast_expression (parser, /*address_p=*/false,
16940 /* Parse a functional cast to TYPE. Returns an expression
16941 representing the cast. */
16944 cp_parser_functional_cast (cp_parser* parser, tree type)
16946 tree expression_list;
16950 = cp_parser_parenthesized_expression_list (parser, false,
16952 /*allow_expansion_p=*/true,
16953 /*non_constant_p=*/NULL);
16955 cast = build_functional_cast (type, expression_list);
16956 /* [expr.const]/1: In an integral constant expression "only type
16957 conversions to integral or enumeration type can be used". */
16958 if (TREE_CODE (type) == TYPE_DECL)
16959 type = TREE_TYPE (type);
16960 if (cast != error_mark_node
16961 && !cast_valid_in_integral_constant_expression_p (type)
16962 && (cp_parser_non_integral_constant_expression
16963 (parser, "a call to a constructor")))
16964 return error_mark_node;
16968 /* Save the tokens that make up the body of a member function defined
16969 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16970 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16971 specifiers applied to the declaration. Returns the FUNCTION_DECL
16972 for the member function. */
16975 cp_parser_save_member_function_body (cp_parser* parser,
16976 cp_decl_specifier_seq *decl_specifiers,
16977 cp_declarator *declarator,
16984 /* Create the function-declaration. */
16985 fn = start_method (decl_specifiers, declarator, attributes);
16986 /* If something went badly wrong, bail out now. */
16987 if (fn == error_mark_node)
16989 /* If there's a function-body, skip it. */
16990 if (cp_parser_token_starts_function_definition_p
16991 (cp_lexer_peek_token (parser->lexer)))
16992 cp_parser_skip_to_end_of_block_or_statement (parser);
16993 return error_mark_node;
16996 /* Remember it, if there default args to post process. */
16997 cp_parser_save_default_args (parser, fn);
16999 /* Save away the tokens that make up the body of the
17001 first = parser->lexer->next_token;
17002 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17003 /* Handle function try blocks. */
17004 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17005 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17006 last = parser->lexer->next_token;
17008 /* Save away the inline definition; we will process it when the
17009 class is complete. */
17010 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17011 DECL_PENDING_INLINE_P (fn) = 1;
17013 /* We need to know that this was defined in the class, so that
17014 friend templates are handled correctly. */
17015 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17017 /* We're done with the inline definition. */
17018 finish_method (fn);
17020 /* Add FN to the queue of functions to be parsed later. */
17021 TREE_VALUE (parser->unparsed_functions_queues)
17022 = tree_cons (NULL_TREE, fn,
17023 TREE_VALUE (parser->unparsed_functions_queues));
17028 /* Parse a template-argument-list, as well as the trailing ">" (but
17029 not the opening ">"). See cp_parser_template_argument_list for the
17033 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17037 tree saved_qualifying_scope;
17038 tree saved_object_scope;
17039 bool saved_greater_than_is_operator_p;
17040 bool saved_skip_evaluation;
17044 When parsing a template-id, the first non-nested `>' is taken as
17045 the end of the template-argument-list rather than a greater-than
17047 saved_greater_than_is_operator_p
17048 = parser->greater_than_is_operator_p;
17049 parser->greater_than_is_operator_p = false;
17050 /* Parsing the argument list may modify SCOPE, so we save it
17052 saved_scope = parser->scope;
17053 saved_qualifying_scope = parser->qualifying_scope;
17054 saved_object_scope = parser->object_scope;
17055 /* We need to evaluate the template arguments, even though this
17056 template-id may be nested within a "sizeof". */
17057 saved_skip_evaluation = skip_evaluation;
17058 skip_evaluation = false;
17059 /* Parse the template-argument-list itself. */
17060 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17061 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17062 arguments = NULL_TREE;
17064 arguments = cp_parser_template_argument_list (parser);
17065 /* Look for the `>' that ends the template-argument-list. If we find
17066 a '>>' instead, it's probably just a typo. */
17067 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17069 if (cxx_dialect != cxx98)
17071 /* In C++0x, a `>>' in a template argument list or cast
17072 expression is considered to be two separate `>'
17073 tokens. So, change the current token to a `>', but don't
17074 consume it: it will be consumed later when the outer
17075 template argument list (or cast expression) is parsed.
17076 Note that this replacement of `>' for `>>' is necessary
17077 even if we are parsing tentatively: in the tentative
17078 case, after calling
17079 cp_parser_enclosed_template_argument_list we will always
17080 throw away all of the template arguments and the first
17081 closing `>', either because the template argument list
17082 was erroneous or because we are replacing those tokens
17083 with a CPP_TEMPLATE_ID token. The second `>' (which will
17084 not have been thrown away) is needed either to close an
17085 outer template argument list or to complete a new-style
17087 cp_token *token = cp_lexer_peek_token (parser->lexer);
17088 token->type = CPP_GREATER;
17090 else if (!saved_greater_than_is_operator_p)
17092 /* If we're in a nested template argument list, the '>>' has
17093 to be a typo for '> >'. We emit the error message, but we
17094 continue parsing and we push a '>' as next token, so that
17095 the argument list will be parsed correctly. Note that the
17096 global source location is still on the token before the
17097 '>>', so we need to say explicitly where we want it. */
17098 cp_token *token = cp_lexer_peek_token (parser->lexer);
17099 error ("%H%<>>%> should be %<> >%> "
17100 "within a nested template argument list",
17103 token->type = CPP_GREATER;
17107 /* If this is not a nested template argument list, the '>>'
17108 is a typo for '>'. Emit an error message and continue.
17109 Same deal about the token location, but here we can get it
17110 right by consuming the '>>' before issuing the diagnostic. */
17111 cp_lexer_consume_token (parser->lexer);
17112 error ("spurious %<>>%>, use %<>%> to terminate "
17113 "a template argument list");
17117 cp_parser_skip_to_end_of_template_parameter_list (parser);
17118 /* The `>' token might be a greater-than operator again now. */
17119 parser->greater_than_is_operator_p
17120 = saved_greater_than_is_operator_p;
17121 /* Restore the SAVED_SCOPE. */
17122 parser->scope = saved_scope;
17123 parser->qualifying_scope = saved_qualifying_scope;
17124 parser->object_scope = saved_object_scope;
17125 skip_evaluation = saved_skip_evaluation;
17130 /* MEMBER_FUNCTION is a member function, or a friend. If default
17131 arguments, or the body of the function have not yet been parsed,
17135 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17137 /* If this member is a template, get the underlying
17139 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17140 member_function = DECL_TEMPLATE_RESULT (member_function);
17142 /* There should not be any class definitions in progress at this
17143 point; the bodies of members are only parsed outside of all class
17145 gcc_assert (parser->num_classes_being_defined == 0);
17146 /* While we're parsing the member functions we might encounter more
17147 classes. We want to handle them right away, but we don't want
17148 them getting mixed up with functions that are currently in the
17150 parser->unparsed_functions_queues
17151 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17153 /* Make sure that any template parameters are in scope. */
17154 maybe_begin_member_template_processing (member_function);
17156 /* If the body of the function has not yet been parsed, parse it
17158 if (DECL_PENDING_INLINE_P (member_function))
17160 tree function_scope;
17161 cp_token_cache *tokens;
17163 /* The function is no longer pending; we are processing it. */
17164 tokens = DECL_PENDING_INLINE_INFO (member_function);
17165 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17166 DECL_PENDING_INLINE_P (member_function) = 0;
17168 /* If this is a local class, enter the scope of the containing
17170 function_scope = current_function_decl;
17171 if (function_scope)
17172 push_function_context_to (function_scope);
17175 /* Push the body of the function onto the lexer stack. */
17176 cp_parser_push_lexer_for_tokens (parser, tokens);
17178 /* Let the front end know that we going to be defining this
17180 start_preparsed_function (member_function, NULL_TREE,
17181 SF_PRE_PARSED | SF_INCLASS_INLINE);
17183 /* Don't do access checking if it is a templated function. */
17184 if (processing_template_decl)
17185 push_deferring_access_checks (dk_no_check);
17187 /* Now, parse the body of the function. */
17188 cp_parser_function_definition_after_declarator (parser,
17189 /*inline_p=*/true);
17191 if (processing_template_decl)
17192 pop_deferring_access_checks ();
17194 /* Leave the scope of the containing function. */
17195 if (function_scope)
17196 pop_function_context_from (function_scope);
17197 cp_parser_pop_lexer (parser);
17200 /* Remove any template parameters from the symbol table. */
17201 maybe_end_member_template_processing ();
17203 /* Restore the queue. */
17204 parser->unparsed_functions_queues
17205 = TREE_CHAIN (parser->unparsed_functions_queues);
17208 /* If DECL contains any default args, remember it on the unparsed
17209 functions queue. */
17212 cp_parser_save_default_args (cp_parser* parser, tree decl)
17216 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17218 probe = TREE_CHAIN (probe))
17219 if (TREE_PURPOSE (probe))
17221 TREE_PURPOSE (parser->unparsed_functions_queues)
17222 = tree_cons (current_class_type, decl,
17223 TREE_PURPOSE (parser->unparsed_functions_queues));
17228 /* FN is a FUNCTION_DECL which may contains a parameter with an
17229 unparsed DEFAULT_ARG. Parse the default args now. This function
17230 assumes that the current scope is the scope in which the default
17231 argument should be processed. */
17234 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17236 bool saved_local_variables_forbidden_p;
17239 /* While we're parsing the default args, we might (due to the
17240 statement expression extension) encounter more classes. We want
17241 to handle them right away, but we don't want them getting mixed
17242 up with default args that are currently in the queue. */
17243 parser->unparsed_functions_queues
17244 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17246 /* Local variable names (and the `this' keyword) may not appear
17247 in a default argument. */
17248 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17249 parser->local_variables_forbidden_p = true;
17251 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17253 parm = TREE_CHAIN (parm))
17255 cp_token_cache *tokens;
17256 tree default_arg = TREE_PURPOSE (parm);
17258 VEC(tree,gc) *insts;
17265 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17266 /* This can happen for a friend declaration for a function
17267 already declared with default arguments. */
17270 /* Push the saved tokens for the default argument onto the parser's
17272 tokens = DEFARG_TOKENS (default_arg);
17273 cp_parser_push_lexer_for_tokens (parser, tokens);
17275 /* Parse the assignment-expression. */
17276 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17278 if (!processing_template_decl)
17279 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17281 TREE_PURPOSE (parm) = parsed_arg;
17283 /* Update any instantiations we've already created. */
17284 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17285 VEC_iterate (tree, insts, ix, copy); ix++)
17286 TREE_PURPOSE (copy) = parsed_arg;
17288 /* If the token stream has not been completely used up, then
17289 there was extra junk after the end of the default
17291 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17292 cp_parser_error (parser, "expected %<,%>");
17294 /* Revert to the main lexer. */
17295 cp_parser_pop_lexer (parser);
17298 /* Make sure no default arg is missing. */
17299 check_default_args (fn);
17301 /* Restore the state of local_variables_forbidden_p. */
17302 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17304 /* Restore the queue. */
17305 parser->unparsed_functions_queues
17306 = TREE_CHAIN (parser->unparsed_functions_queues);
17309 /* Parse the operand of `sizeof' (or a similar operator). Returns
17310 either a TYPE or an expression, depending on the form of the
17311 input. The KEYWORD indicates which kind of expression we have
17315 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17317 static const char *format;
17318 tree expr = NULL_TREE;
17319 const char *saved_message;
17321 bool saved_integral_constant_expression_p;
17322 bool saved_non_integral_constant_expression_p;
17323 bool pack_expansion_p = false;
17325 /* Initialize FORMAT the first time we get here. */
17327 format = "types may not be defined in '%s' expressions";
17329 /* Types cannot be defined in a `sizeof' expression. Save away the
17331 saved_message = parser->type_definition_forbidden_message;
17332 /* And create the new one. */
17333 parser->type_definition_forbidden_message = tmp
17334 = XNEWVEC (char, strlen (format)
17335 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17337 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17339 /* The restrictions on constant-expressions do not apply inside
17340 sizeof expressions. */
17341 saved_integral_constant_expression_p
17342 = parser->integral_constant_expression_p;
17343 saved_non_integral_constant_expression_p
17344 = parser->non_integral_constant_expression_p;
17345 parser->integral_constant_expression_p = false;
17347 /* If it's a `...', then we are computing the length of a parameter
17349 if (keyword == RID_SIZEOF
17350 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17352 /* Consume the `...'. */
17353 cp_lexer_consume_token (parser->lexer);
17354 maybe_warn_variadic_templates ();
17356 /* Note that this is an expansion. */
17357 pack_expansion_p = true;
17360 /* Do not actually evaluate the expression. */
17362 /* If it's a `(', then we might be looking at the type-id
17364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17367 bool saved_in_type_id_in_expr_p;
17369 /* We can't be sure yet whether we're looking at a type-id or an
17371 cp_parser_parse_tentatively (parser);
17372 /* Consume the `('. */
17373 cp_lexer_consume_token (parser->lexer);
17374 /* Parse the type-id. */
17375 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17376 parser->in_type_id_in_expr_p = true;
17377 type = cp_parser_type_id (parser);
17378 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17379 /* Now, look for the trailing `)'. */
17380 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17381 /* If all went well, then we're done. */
17382 if (cp_parser_parse_definitely (parser))
17384 cp_decl_specifier_seq decl_specs;
17386 /* Build a trivial decl-specifier-seq. */
17387 clear_decl_specs (&decl_specs);
17388 decl_specs.type = type;
17390 /* Call grokdeclarator to figure out what type this is. */
17391 expr = grokdeclarator (NULL,
17395 /*attrlist=*/NULL);
17399 /* If the type-id production did not work out, then we must be
17400 looking at the unary-expression production. */
17402 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17405 if (pack_expansion_p)
17406 /* Build a pack expansion. */
17407 expr = make_pack_expansion (expr);
17409 /* Go back to evaluating expressions. */
17412 /* Free the message we created. */
17414 /* And restore the old one. */
17415 parser->type_definition_forbidden_message = saved_message;
17416 parser->integral_constant_expression_p
17417 = saved_integral_constant_expression_p;
17418 parser->non_integral_constant_expression_p
17419 = saved_non_integral_constant_expression_p;
17424 /* If the current declaration has no declarator, return true. */
17427 cp_parser_declares_only_class_p (cp_parser *parser)
17429 /* If the next token is a `;' or a `,' then there is no
17431 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17432 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17435 /* Update the DECL_SPECS to reflect the storage class indicated by
17439 cp_parser_set_storage_class (cp_parser *parser,
17440 cp_decl_specifier_seq *decl_specs,
17443 cp_storage_class storage_class;
17445 if (parser->in_unbraced_linkage_specification_p)
17447 error ("invalid use of %qD in linkage specification",
17448 ridpointers[keyword]);
17451 else if (decl_specs->storage_class != sc_none)
17453 decl_specs->conflicting_specifiers_p = true;
17457 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17458 && decl_specs->specs[(int) ds_thread])
17460 error ("%<__thread%> before %qD", ridpointers[keyword]);
17461 decl_specs->specs[(int) ds_thread] = 0;
17467 storage_class = sc_auto;
17470 storage_class = sc_register;
17473 storage_class = sc_static;
17476 storage_class = sc_extern;
17479 storage_class = sc_mutable;
17482 gcc_unreachable ();
17484 decl_specs->storage_class = storage_class;
17486 /* A storage class specifier cannot be applied alongside a typedef
17487 specifier. If there is a typedef specifier present then set
17488 conflicting_specifiers_p which will trigger an error later
17489 on in grokdeclarator. */
17490 if (decl_specs->specs[(int)ds_typedef])
17491 decl_specs->conflicting_specifiers_p = true;
17494 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17495 is true, the type is a user-defined type; otherwise it is a
17496 built-in type specified by a keyword. */
17499 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17501 bool user_defined_p)
17503 decl_specs->any_specifiers_p = true;
17505 /* If the user tries to redeclare bool or wchar_t (with, for
17506 example, in "typedef int wchar_t;") we remember that this is what
17507 happened. In system headers, we ignore these declarations so
17508 that G++ can work with system headers that are not C++-safe. */
17509 if (decl_specs->specs[(int) ds_typedef]
17511 && (type_spec == boolean_type_node
17512 || type_spec == wchar_type_node)
17513 && (decl_specs->type
17514 || decl_specs->specs[(int) ds_long]
17515 || decl_specs->specs[(int) ds_short]
17516 || decl_specs->specs[(int) ds_unsigned]
17517 || decl_specs->specs[(int) ds_signed]))
17519 decl_specs->redefined_builtin_type = type_spec;
17520 if (!decl_specs->type)
17522 decl_specs->type = type_spec;
17523 decl_specs->user_defined_type_p = false;
17526 else if (decl_specs->type)
17527 decl_specs->multiple_types_p = true;
17530 decl_specs->type = type_spec;
17531 decl_specs->user_defined_type_p = user_defined_p;
17532 decl_specs->redefined_builtin_type = NULL_TREE;
17536 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17537 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17540 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17542 return decl_specifiers->specs[(int) ds_friend] != 0;
17545 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17546 issue an error message indicating that TOKEN_DESC was expected.
17548 Returns the token consumed, if the token had the appropriate type.
17549 Otherwise, returns NULL. */
17552 cp_parser_require (cp_parser* parser,
17553 enum cpp_ttype type,
17554 const char* token_desc)
17556 if (cp_lexer_next_token_is (parser->lexer, type))
17557 return cp_lexer_consume_token (parser->lexer);
17560 /* Output the MESSAGE -- unless we're parsing tentatively. */
17561 if (!cp_parser_simulate_error (parser))
17563 char *message = concat ("expected ", token_desc, NULL);
17564 cp_parser_error (parser, message);
17571 /* An error message is produced if the next token is not '>'.
17572 All further tokens are skipped until the desired token is
17573 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17576 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17578 /* Current level of '< ... >'. */
17579 unsigned level = 0;
17580 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17581 unsigned nesting_depth = 0;
17583 /* Are we ready, yet? If not, issue error message. */
17584 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17587 /* Skip tokens until the desired token is found. */
17590 /* Peek at the next token. */
17591 switch (cp_lexer_peek_token (parser->lexer)->type)
17594 if (!nesting_depth)
17599 if (cxx_dialect == cxx98)
17600 /* C++0x views the `>>' operator as two `>' tokens, but
17603 else if (!nesting_depth && level-- == 0)
17605 /* We've hit a `>>' where the first `>' closes the
17606 template argument list, and the second `>' is
17607 spurious. Just consume the `>>' and stop; we've
17608 already produced at least one error. */
17609 cp_lexer_consume_token (parser->lexer);
17612 /* Fall through for C++0x, so we handle the second `>' in
17616 if (!nesting_depth && level-- == 0)
17618 /* We've reached the token we want, consume it and stop. */
17619 cp_lexer_consume_token (parser->lexer);
17624 case CPP_OPEN_PAREN:
17625 case CPP_OPEN_SQUARE:
17629 case CPP_CLOSE_PAREN:
17630 case CPP_CLOSE_SQUARE:
17631 if (nesting_depth-- == 0)
17636 case CPP_PRAGMA_EOL:
17637 case CPP_SEMICOLON:
17638 case CPP_OPEN_BRACE:
17639 case CPP_CLOSE_BRACE:
17640 /* The '>' was probably forgotten, don't look further. */
17647 /* Consume this token. */
17648 cp_lexer_consume_token (parser->lexer);
17652 /* If the next token is the indicated keyword, consume it. Otherwise,
17653 issue an error message indicating that TOKEN_DESC was expected.
17655 Returns the token consumed, if the token had the appropriate type.
17656 Otherwise, returns NULL. */
17659 cp_parser_require_keyword (cp_parser* parser,
17661 const char* token_desc)
17663 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17665 if (token && token->keyword != keyword)
17667 dyn_string_t error_msg;
17669 /* Format the error message. */
17670 error_msg = dyn_string_new (0);
17671 dyn_string_append_cstr (error_msg, "expected ");
17672 dyn_string_append_cstr (error_msg, token_desc);
17673 cp_parser_error (parser, error_msg->s);
17674 dyn_string_delete (error_msg);
17681 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17682 function-definition. */
17685 cp_parser_token_starts_function_definition_p (cp_token* token)
17687 return (/* An ordinary function-body begins with an `{'. */
17688 token->type == CPP_OPEN_BRACE
17689 /* A ctor-initializer begins with a `:'. */
17690 || token->type == CPP_COLON
17691 /* A function-try-block begins with `try'. */
17692 || token->keyword == RID_TRY
17693 /* The named return value extension begins with `return'. */
17694 || token->keyword == RID_RETURN);
17697 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17701 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17705 token = cp_lexer_peek_token (parser->lexer);
17706 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17709 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17710 C++0x) ending a template-argument. */
17713 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17717 token = cp_lexer_peek_token (parser->lexer);
17718 return (token->type == CPP_COMMA
17719 || token->type == CPP_GREATER
17720 || token->type == CPP_ELLIPSIS
17721 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17724 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17725 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17728 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17733 token = cp_lexer_peek_nth_token (parser->lexer, n);
17734 if (token->type == CPP_LESS)
17736 /* Check for the sequence `<::' in the original code. It would be lexed as
17737 `[:', where `[' is a digraph, and there is no whitespace before
17739 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17742 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17743 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17749 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17750 or none_type otherwise. */
17752 static enum tag_types
17753 cp_parser_token_is_class_key (cp_token* token)
17755 switch (token->keyword)
17760 return record_type;
17769 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17772 cp_parser_check_class_key (enum tag_types class_key, tree type)
17774 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17775 pedwarn ("%qs tag used in naming %q#T",
17776 class_key == union_type ? "union"
17777 : class_key == record_type ? "struct" : "class",
17781 /* Issue an error message if DECL is redeclared with different
17782 access than its original declaration [class.access.spec/3].
17783 This applies to nested classes and nested class templates.
17787 cp_parser_check_access_in_redeclaration (tree decl)
17789 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17792 if ((TREE_PRIVATE (decl)
17793 != (current_access_specifier == access_private_node))
17794 || (TREE_PROTECTED (decl)
17795 != (current_access_specifier == access_protected_node)))
17796 error ("%qD redeclared with different access", decl);
17799 /* Look for the `template' keyword, as a syntactic disambiguator.
17800 Return TRUE iff it is present, in which case it will be
17804 cp_parser_optional_template_keyword (cp_parser *parser)
17806 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17808 /* The `template' keyword can only be used within templates;
17809 outside templates the parser can always figure out what is a
17810 template and what is not. */
17811 if (!processing_template_decl)
17813 error ("%<template%> (as a disambiguator) is only allowed "
17814 "within templates");
17815 /* If this part of the token stream is rescanned, the same
17816 error message would be generated. So, we purge the token
17817 from the stream. */
17818 cp_lexer_purge_token (parser->lexer);
17823 /* Consume the `template' keyword. */
17824 cp_lexer_consume_token (parser->lexer);
17832 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17833 set PARSER->SCOPE, and perform other related actions. */
17836 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17839 struct tree_check *check_value;
17840 deferred_access_check *chk;
17841 VEC (deferred_access_check,gc) *checks;
17843 /* Get the stored value. */
17844 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17845 /* Perform any access checks that were deferred. */
17846 checks = check_value->checks;
17850 VEC_iterate (deferred_access_check, checks, i, chk) ;
17853 perform_or_defer_access_check (chk->binfo,
17858 /* Set the scope from the stored value. */
17859 parser->scope = check_value->value;
17860 parser->qualifying_scope = check_value->qualifying_scope;
17861 parser->object_scope = NULL_TREE;
17864 /* Consume tokens up through a non-nested END token. */
17867 cp_parser_cache_group (cp_parser *parser,
17868 enum cpp_ttype end,
17875 /* Abort a parenthesized expression if we encounter a brace. */
17876 if ((end == CPP_CLOSE_PAREN || depth == 0)
17877 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17879 /* If we've reached the end of the file, stop. */
17880 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17881 || (end != CPP_PRAGMA_EOL
17882 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17884 /* Consume the next token. */
17885 token = cp_lexer_consume_token (parser->lexer);
17886 /* See if it starts a new group. */
17887 if (token->type == CPP_OPEN_BRACE)
17889 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17893 else if (token->type == CPP_OPEN_PAREN)
17894 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17895 else if (token->type == CPP_PRAGMA)
17896 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17897 else if (token->type == end)
17902 /* Begin parsing tentatively. We always save tokens while parsing
17903 tentatively so that if the tentative parsing fails we can restore the
17907 cp_parser_parse_tentatively (cp_parser* parser)
17909 /* Enter a new parsing context. */
17910 parser->context = cp_parser_context_new (parser->context);
17911 /* Begin saving tokens. */
17912 cp_lexer_save_tokens (parser->lexer);
17913 /* In order to avoid repetitive access control error messages,
17914 access checks are queued up until we are no longer parsing
17916 push_deferring_access_checks (dk_deferred);
17919 /* Commit to the currently active tentative parse. */
17922 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17924 cp_parser_context *context;
17927 /* Mark all of the levels as committed. */
17928 lexer = parser->lexer;
17929 for (context = parser->context; context->next; context = context->next)
17931 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17933 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17934 while (!cp_lexer_saving_tokens (lexer))
17935 lexer = lexer->next;
17936 cp_lexer_commit_tokens (lexer);
17940 /* Abort the currently active tentative parse. All consumed tokens
17941 will be rolled back, and no diagnostics will be issued. */
17944 cp_parser_abort_tentative_parse (cp_parser* parser)
17946 cp_parser_simulate_error (parser);
17947 /* Now, pretend that we want to see if the construct was
17948 successfully parsed. */
17949 cp_parser_parse_definitely (parser);
17952 /* Stop parsing tentatively. If a parse error has occurred, restore the
17953 token stream. Otherwise, commit to the tokens we have consumed.
17954 Returns true if no error occurred; false otherwise. */
17957 cp_parser_parse_definitely (cp_parser* parser)
17959 bool error_occurred;
17960 cp_parser_context *context;
17962 /* Remember whether or not an error occurred, since we are about to
17963 destroy that information. */
17964 error_occurred = cp_parser_error_occurred (parser);
17965 /* Remove the topmost context from the stack. */
17966 context = parser->context;
17967 parser->context = context->next;
17968 /* If no parse errors occurred, commit to the tentative parse. */
17969 if (!error_occurred)
17971 /* Commit to the tokens read tentatively, unless that was
17973 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17974 cp_lexer_commit_tokens (parser->lexer);
17976 pop_to_parent_deferring_access_checks ();
17978 /* Otherwise, if errors occurred, roll back our state so that things
17979 are just as they were before we began the tentative parse. */
17982 cp_lexer_rollback_tokens (parser->lexer);
17983 pop_deferring_access_checks ();
17985 /* Add the context to the front of the free list. */
17986 context->next = cp_parser_context_free_list;
17987 cp_parser_context_free_list = context;
17989 return !error_occurred;
17992 /* Returns true if we are parsing tentatively and are not committed to
17993 this tentative parse. */
17996 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17998 return (cp_parser_parsing_tentatively (parser)
17999 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18002 /* Returns nonzero iff an error has occurred during the most recent
18003 tentative parse. */
18006 cp_parser_error_occurred (cp_parser* parser)
18008 return (cp_parser_parsing_tentatively (parser)
18009 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18012 /* Returns nonzero if GNU extensions are allowed. */
18015 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18017 return parser->allow_gnu_extensions_p;
18020 /* Objective-C++ Productions */
18023 /* Parse an Objective-C expression, which feeds into a primary-expression
18027 objc-message-expression
18028 objc-string-literal
18029 objc-encode-expression
18030 objc-protocol-expression
18031 objc-selector-expression
18033 Returns a tree representation of the expression. */
18036 cp_parser_objc_expression (cp_parser* parser)
18038 /* Try to figure out what kind of declaration is present. */
18039 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18043 case CPP_OPEN_SQUARE:
18044 return cp_parser_objc_message_expression (parser);
18046 case CPP_OBJC_STRING:
18047 kwd = cp_lexer_consume_token (parser->lexer);
18048 return objc_build_string_object (kwd->u.value);
18051 switch (kwd->keyword)
18053 case RID_AT_ENCODE:
18054 return cp_parser_objc_encode_expression (parser);
18056 case RID_AT_PROTOCOL:
18057 return cp_parser_objc_protocol_expression (parser);
18059 case RID_AT_SELECTOR:
18060 return cp_parser_objc_selector_expression (parser);
18066 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18067 cp_parser_skip_to_end_of_block_or_statement (parser);
18070 return error_mark_node;
18073 /* Parse an Objective-C message expression.
18075 objc-message-expression:
18076 [ objc-message-receiver objc-message-args ]
18078 Returns a representation of an Objective-C message. */
18081 cp_parser_objc_message_expression (cp_parser* parser)
18083 tree receiver, messageargs;
18085 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18086 receiver = cp_parser_objc_message_receiver (parser);
18087 messageargs = cp_parser_objc_message_args (parser);
18088 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18090 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18093 /* Parse an objc-message-receiver.
18095 objc-message-receiver:
18097 simple-type-specifier
18099 Returns a representation of the type or expression. */
18102 cp_parser_objc_message_receiver (cp_parser* parser)
18106 /* An Objective-C message receiver may be either (1) a type
18107 or (2) an expression. */
18108 cp_parser_parse_tentatively (parser);
18109 rcv = cp_parser_expression (parser, false);
18111 if (cp_parser_parse_definitely (parser))
18114 rcv = cp_parser_simple_type_specifier (parser,
18115 /*decl_specs=*/NULL,
18116 CP_PARSER_FLAGS_NONE);
18118 return objc_get_class_reference (rcv);
18121 /* Parse the arguments and selectors comprising an Objective-C message.
18126 objc-selector-args , objc-comma-args
18128 objc-selector-args:
18129 objc-selector [opt] : assignment-expression
18130 objc-selector-args objc-selector [opt] : assignment-expression
18133 assignment-expression
18134 objc-comma-args , assignment-expression
18136 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18137 selector arguments and TREE_VALUE containing a list of comma
18141 cp_parser_objc_message_args (cp_parser* parser)
18143 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18144 bool maybe_unary_selector_p = true;
18145 cp_token *token = cp_lexer_peek_token (parser->lexer);
18147 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18149 tree selector = NULL_TREE, arg;
18151 if (token->type != CPP_COLON)
18152 selector = cp_parser_objc_selector (parser);
18154 /* Detect if we have a unary selector. */
18155 if (maybe_unary_selector_p
18156 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18157 return build_tree_list (selector, NULL_TREE);
18159 maybe_unary_selector_p = false;
18160 cp_parser_require (parser, CPP_COLON, "`:'");
18161 arg = cp_parser_assignment_expression (parser, false);
18164 = chainon (sel_args,
18165 build_tree_list (selector, arg));
18167 token = cp_lexer_peek_token (parser->lexer);
18170 /* Handle non-selector arguments, if any. */
18171 while (token->type == CPP_COMMA)
18175 cp_lexer_consume_token (parser->lexer);
18176 arg = cp_parser_assignment_expression (parser, false);
18179 = chainon (addl_args,
18180 build_tree_list (NULL_TREE, arg));
18182 token = cp_lexer_peek_token (parser->lexer);
18185 return build_tree_list (sel_args, addl_args);
18188 /* Parse an Objective-C encode expression.
18190 objc-encode-expression:
18191 @encode objc-typename
18193 Returns an encoded representation of the type argument. */
18196 cp_parser_objc_encode_expression (cp_parser* parser)
18200 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18201 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18202 type = complete_type (cp_parser_type_id (parser));
18203 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18207 error ("%<@encode%> must specify a type as an argument");
18208 return error_mark_node;
18211 return objc_build_encode_expr (type);
18214 /* Parse an Objective-C @defs expression. */
18217 cp_parser_objc_defs_expression (cp_parser *parser)
18221 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18222 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18223 name = cp_parser_identifier (parser);
18224 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18226 return objc_get_class_ivars (name);
18229 /* Parse an Objective-C protocol expression.
18231 objc-protocol-expression:
18232 @protocol ( identifier )
18234 Returns a representation of the protocol expression. */
18237 cp_parser_objc_protocol_expression (cp_parser* parser)
18241 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18242 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18243 proto = cp_parser_identifier (parser);
18244 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18246 return objc_build_protocol_expr (proto);
18249 /* Parse an Objective-C selector expression.
18251 objc-selector-expression:
18252 @selector ( objc-method-signature )
18254 objc-method-signature:
18260 objc-selector-seq objc-selector :
18262 Returns a representation of the method selector. */
18265 cp_parser_objc_selector_expression (cp_parser* parser)
18267 tree sel_seq = NULL_TREE;
18268 bool maybe_unary_selector_p = true;
18271 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18272 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18273 token = cp_lexer_peek_token (parser->lexer);
18275 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18276 || token->type == CPP_SCOPE)
18278 tree selector = NULL_TREE;
18280 if (token->type != CPP_COLON
18281 || token->type == CPP_SCOPE)
18282 selector = cp_parser_objc_selector (parser);
18284 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18285 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18287 /* Detect if we have a unary selector. */
18288 if (maybe_unary_selector_p)
18290 sel_seq = selector;
18291 goto finish_selector;
18295 cp_parser_error (parser, "expected %<:%>");
18298 maybe_unary_selector_p = false;
18299 token = cp_lexer_consume_token (parser->lexer);
18301 if (token->type == CPP_SCOPE)
18304 = chainon (sel_seq,
18305 build_tree_list (selector, NULL_TREE));
18307 = chainon (sel_seq,
18308 build_tree_list (NULL_TREE, NULL_TREE));
18312 = chainon (sel_seq,
18313 build_tree_list (selector, NULL_TREE));
18315 token = cp_lexer_peek_token (parser->lexer);
18319 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18321 return objc_build_selector_expr (sel_seq);
18324 /* Parse a list of identifiers.
18326 objc-identifier-list:
18328 objc-identifier-list , identifier
18330 Returns a TREE_LIST of identifier nodes. */
18333 cp_parser_objc_identifier_list (cp_parser* parser)
18335 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18336 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18338 while (sep->type == CPP_COMMA)
18340 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18341 list = chainon (list,
18342 build_tree_list (NULL_TREE,
18343 cp_parser_identifier (parser)));
18344 sep = cp_lexer_peek_token (parser->lexer);
18350 /* Parse an Objective-C alias declaration.
18352 objc-alias-declaration:
18353 @compatibility_alias identifier identifier ;
18355 This function registers the alias mapping with the Objective-C front end.
18356 It returns nothing. */
18359 cp_parser_objc_alias_declaration (cp_parser* parser)
18363 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18364 alias = cp_parser_identifier (parser);
18365 orig = cp_parser_identifier (parser);
18366 objc_declare_alias (alias, orig);
18367 cp_parser_consume_semicolon_at_end_of_statement (parser);
18370 /* Parse an Objective-C class forward-declaration.
18372 objc-class-declaration:
18373 @class objc-identifier-list ;
18375 The function registers the forward declarations with the Objective-C
18376 front end. It returns nothing. */
18379 cp_parser_objc_class_declaration (cp_parser* parser)
18381 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18382 objc_declare_class (cp_parser_objc_identifier_list (parser));
18383 cp_parser_consume_semicolon_at_end_of_statement (parser);
18386 /* Parse a list of Objective-C protocol references.
18388 objc-protocol-refs-opt:
18389 objc-protocol-refs [opt]
18391 objc-protocol-refs:
18392 < objc-identifier-list >
18394 Returns a TREE_LIST of identifiers, if any. */
18397 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18399 tree protorefs = NULL_TREE;
18401 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18403 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18404 protorefs = cp_parser_objc_identifier_list (parser);
18405 cp_parser_require (parser, CPP_GREATER, "`>'");
18411 /* Parse a Objective-C visibility specification. */
18414 cp_parser_objc_visibility_spec (cp_parser* parser)
18416 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18418 switch (vis->keyword)
18420 case RID_AT_PRIVATE:
18421 objc_set_visibility (2);
18423 case RID_AT_PROTECTED:
18424 objc_set_visibility (0);
18426 case RID_AT_PUBLIC:
18427 objc_set_visibility (1);
18433 /* Eat '@private'/'@protected'/'@public'. */
18434 cp_lexer_consume_token (parser->lexer);
18437 /* Parse an Objective-C method type. */
18440 cp_parser_objc_method_type (cp_parser* parser)
18442 objc_set_method_type
18443 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18448 /* Parse an Objective-C protocol qualifier. */
18451 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18453 tree quals = NULL_TREE, node;
18454 cp_token *token = cp_lexer_peek_token (parser->lexer);
18456 node = token->u.value;
18458 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18459 && (node == ridpointers [(int) RID_IN]
18460 || node == ridpointers [(int) RID_OUT]
18461 || node == ridpointers [(int) RID_INOUT]
18462 || node == ridpointers [(int) RID_BYCOPY]
18463 || node == ridpointers [(int) RID_BYREF]
18464 || node == ridpointers [(int) RID_ONEWAY]))
18466 quals = tree_cons (NULL_TREE, node, quals);
18467 cp_lexer_consume_token (parser->lexer);
18468 token = cp_lexer_peek_token (parser->lexer);
18469 node = token->u.value;
18475 /* Parse an Objective-C typename. */
18478 cp_parser_objc_typename (cp_parser* parser)
18480 tree typename = NULL_TREE;
18482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18484 tree proto_quals, cp_type = NULL_TREE;
18486 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18487 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18489 /* An ObjC type name may consist of just protocol qualifiers, in which
18490 case the type shall default to 'id'. */
18491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18492 cp_type = cp_parser_type_id (parser);
18494 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18495 typename = build_tree_list (proto_quals, cp_type);
18501 /* Check to see if TYPE refers to an Objective-C selector name. */
18504 cp_parser_objc_selector_p (enum cpp_ttype type)
18506 return (type == CPP_NAME || type == CPP_KEYWORD
18507 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18508 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18509 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18510 || type == CPP_XOR || type == CPP_XOR_EQ);
18513 /* Parse an Objective-C selector. */
18516 cp_parser_objc_selector (cp_parser* parser)
18518 cp_token *token = cp_lexer_consume_token (parser->lexer);
18520 if (!cp_parser_objc_selector_p (token->type))
18522 error ("invalid Objective-C++ selector name");
18523 return error_mark_node;
18526 /* C++ operator names are allowed to appear in ObjC selectors. */
18527 switch (token->type)
18529 case CPP_AND_AND: return get_identifier ("and");
18530 case CPP_AND_EQ: return get_identifier ("and_eq");
18531 case CPP_AND: return get_identifier ("bitand");
18532 case CPP_OR: return get_identifier ("bitor");
18533 case CPP_COMPL: return get_identifier ("compl");
18534 case CPP_NOT: return get_identifier ("not");
18535 case CPP_NOT_EQ: return get_identifier ("not_eq");
18536 case CPP_OR_OR: return get_identifier ("or");
18537 case CPP_OR_EQ: return get_identifier ("or_eq");
18538 case CPP_XOR: return get_identifier ("xor");
18539 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18540 default: return token->u.value;
18544 /* Parse an Objective-C params list. */
18547 cp_parser_objc_method_keyword_params (cp_parser* parser)
18549 tree params = NULL_TREE;
18550 bool maybe_unary_selector_p = true;
18551 cp_token *token = cp_lexer_peek_token (parser->lexer);
18553 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18555 tree selector = NULL_TREE, typename, identifier;
18557 if (token->type != CPP_COLON)
18558 selector = cp_parser_objc_selector (parser);
18560 /* Detect if we have a unary selector. */
18561 if (maybe_unary_selector_p
18562 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18565 maybe_unary_selector_p = false;
18566 cp_parser_require (parser, CPP_COLON, "`:'");
18567 typename = cp_parser_objc_typename (parser);
18568 identifier = cp_parser_identifier (parser);
18572 objc_build_keyword_decl (selector,
18576 token = cp_lexer_peek_token (parser->lexer);
18582 /* Parse the non-keyword Objective-C params. */
18585 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18587 tree params = make_node (TREE_LIST);
18588 cp_token *token = cp_lexer_peek_token (parser->lexer);
18589 *ellipsisp = false; /* Initially, assume no ellipsis. */
18591 while (token->type == CPP_COMMA)
18593 cp_parameter_declarator *parmdecl;
18596 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18597 token = cp_lexer_peek_token (parser->lexer);
18599 if (token->type == CPP_ELLIPSIS)
18601 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18606 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18607 parm = grokdeclarator (parmdecl->declarator,
18608 &parmdecl->decl_specifiers,
18609 PARM, /*initialized=*/0,
18610 /*attrlist=*/NULL);
18612 chainon (params, build_tree_list (NULL_TREE, parm));
18613 token = cp_lexer_peek_token (parser->lexer);
18619 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18622 cp_parser_objc_interstitial_code (cp_parser* parser)
18624 cp_token *token = cp_lexer_peek_token (parser->lexer);
18626 /* If the next token is `extern' and the following token is a string
18627 literal, then we have a linkage specification. */
18628 if (token->keyword == RID_EXTERN
18629 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18630 cp_parser_linkage_specification (parser);
18631 /* Handle #pragma, if any. */
18632 else if (token->type == CPP_PRAGMA)
18633 cp_parser_pragma (parser, pragma_external);
18634 /* Allow stray semicolons. */
18635 else if (token->type == CPP_SEMICOLON)
18636 cp_lexer_consume_token (parser->lexer);
18637 /* Finally, try to parse a block-declaration, or a function-definition. */
18639 cp_parser_block_declaration (parser, /*statement_p=*/false);
18642 /* Parse a method signature. */
18645 cp_parser_objc_method_signature (cp_parser* parser)
18647 tree rettype, kwdparms, optparms;
18648 bool ellipsis = false;
18650 cp_parser_objc_method_type (parser);
18651 rettype = cp_parser_objc_typename (parser);
18652 kwdparms = cp_parser_objc_method_keyword_params (parser);
18653 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18655 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18658 /* Pars an Objective-C method prototype list. */
18661 cp_parser_objc_method_prototype_list (cp_parser* parser)
18663 cp_token *token = cp_lexer_peek_token (parser->lexer);
18665 while (token->keyword != RID_AT_END)
18667 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18669 objc_add_method_declaration
18670 (cp_parser_objc_method_signature (parser));
18671 cp_parser_consume_semicolon_at_end_of_statement (parser);
18674 /* Allow for interspersed non-ObjC++ code. */
18675 cp_parser_objc_interstitial_code (parser);
18677 token = cp_lexer_peek_token (parser->lexer);
18680 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18681 objc_finish_interface ();
18684 /* Parse an Objective-C method definition list. */
18687 cp_parser_objc_method_definition_list (cp_parser* parser)
18689 cp_token *token = cp_lexer_peek_token (parser->lexer);
18691 while (token->keyword != RID_AT_END)
18695 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18697 push_deferring_access_checks (dk_deferred);
18698 objc_start_method_definition
18699 (cp_parser_objc_method_signature (parser));
18701 /* For historical reasons, we accept an optional semicolon. */
18702 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18703 cp_lexer_consume_token (parser->lexer);
18705 perform_deferred_access_checks ();
18706 stop_deferring_access_checks ();
18707 meth = cp_parser_function_definition_after_declarator (parser,
18709 pop_deferring_access_checks ();
18710 objc_finish_method_definition (meth);
18713 /* Allow for interspersed non-ObjC++ code. */
18714 cp_parser_objc_interstitial_code (parser);
18716 token = cp_lexer_peek_token (parser->lexer);
18719 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18720 objc_finish_implementation ();
18723 /* Parse Objective-C ivars. */
18726 cp_parser_objc_class_ivars (cp_parser* parser)
18728 cp_token *token = cp_lexer_peek_token (parser->lexer);
18730 if (token->type != CPP_OPEN_BRACE)
18731 return; /* No ivars specified. */
18733 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18734 token = cp_lexer_peek_token (parser->lexer);
18736 while (token->type != CPP_CLOSE_BRACE)
18738 cp_decl_specifier_seq declspecs;
18739 int decl_class_or_enum_p;
18740 tree prefix_attributes;
18742 cp_parser_objc_visibility_spec (parser);
18744 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18747 cp_parser_decl_specifier_seq (parser,
18748 CP_PARSER_FLAGS_OPTIONAL,
18750 &decl_class_or_enum_p);
18751 prefix_attributes = declspecs.attributes;
18752 declspecs.attributes = NULL_TREE;
18754 /* Keep going until we hit the `;' at the end of the
18756 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18758 tree width = NULL_TREE, attributes, first_attribute, decl;
18759 cp_declarator *declarator = NULL;
18760 int ctor_dtor_or_conv_p;
18762 /* Check for a (possibly unnamed) bitfield declaration. */
18763 token = cp_lexer_peek_token (parser->lexer);
18764 if (token->type == CPP_COLON)
18767 if (token->type == CPP_NAME
18768 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18771 /* Get the name of the bitfield. */
18772 declarator = make_id_declarator (NULL_TREE,
18773 cp_parser_identifier (parser),
18777 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18778 /* Get the width of the bitfield. */
18780 = cp_parser_constant_expression (parser,
18781 /*allow_non_constant=*/false,
18786 /* Parse the declarator. */
18788 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18789 &ctor_dtor_or_conv_p,
18790 /*parenthesized_p=*/NULL,
18791 /*member_p=*/false);
18794 /* Look for attributes that apply to the ivar. */
18795 attributes = cp_parser_attributes_opt (parser);
18796 /* Remember which attributes are prefix attributes and
18798 first_attribute = attributes;
18799 /* Combine the attributes. */
18800 attributes = chainon (prefix_attributes, attributes);
18804 /* Create the bitfield declaration. */
18805 decl = grokbitfield (declarator, &declspecs, width);
18806 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18809 decl = grokfield (declarator, &declspecs,
18810 NULL_TREE, /*init_const_expr_p=*/false,
18811 NULL_TREE, attributes);
18813 /* Add the instance variable. */
18814 objc_add_instance_variable (decl);
18816 /* Reset PREFIX_ATTRIBUTES. */
18817 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18818 attributes = TREE_CHAIN (attributes);
18820 TREE_CHAIN (attributes) = NULL_TREE;
18822 token = cp_lexer_peek_token (parser->lexer);
18824 if (token->type == CPP_COMMA)
18826 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18832 cp_parser_consume_semicolon_at_end_of_statement (parser);
18833 token = cp_lexer_peek_token (parser->lexer);
18836 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18837 /* For historical reasons, we accept an optional semicolon. */
18838 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18839 cp_lexer_consume_token (parser->lexer);
18842 /* Parse an Objective-C protocol declaration. */
18845 cp_parser_objc_protocol_declaration (cp_parser* parser)
18847 tree proto, protorefs;
18850 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18853 error ("identifier expected after %<@protocol%>");
18857 /* See if we have a forward declaration or a definition. */
18858 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18860 /* Try a forward declaration first. */
18861 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18863 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18865 cp_parser_consume_semicolon_at_end_of_statement (parser);
18868 /* Ok, we got a full-fledged definition (or at least should). */
18871 proto = cp_parser_identifier (parser);
18872 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18873 objc_start_protocol (proto, protorefs);
18874 cp_parser_objc_method_prototype_list (parser);
18878 /* Parse an Objective-C superclass or category. */
18881 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18884 cp_token *next = cp_lexer_peek_token (parser->lexer);
18886 *super = *categ = NULL_TREE;
18887 if (next->type == CPP_COLON)
18889 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18890 *super = cp_parser_identifier (parser);
18892 else if (next->type == CPP_OPEN_PAREN)
18894 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18895 *categ = cp_parser_identifier (parser);
18896 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18900 /* Parse an Objective-C class interface. */
18903 cp_parser_objc_class_interface (cp_parser* parser)
18905 tree name, super, categ, protos;
18907 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18908 name = cp_parser_identifier (parser);
18909 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18910 protos = cp_parser_objc_protocol_refs_opt (parser);
18912 /* We have either a class or a category on our hands. */
18914 objc_start_category_interface (name, categ, protos);
18917 objc_start_class_interface (name, super, protos);
18918 /* Handle instance variable declarations, if any. */
18919 cp_parser_objc_class_ivars (parser);
18920 objc_continue_interface ();
18923 cp_parser_objc_method_prototype_list (parser);
18926 /* Parse an Objective-C class implementation. */
18929 cp_parser_objc_class_implementation (cp_parser* parser)
18931 tree name, super, categ;
18933 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18934 name = cp_parser_identifier (parser);
18935 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18937 /* We have either a class or a category on our hands. */
18939 objc_start_category_implementation (name, categ);
18942 objc_start_class_implementation (name, super);
18943 /* Handle instance variable declarations, if any. */
18944 cp_parser_objc_class_ivars (parser);
18945 objc_continue_implementation ();
18948 cp_parser_objc_method_definition_list (parser);
18951 /* Consume the @end token and finish off the implementation. */
18954 cp_parser_objc_end_implementation (cp_parser* parser)
18956 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18957 objc_finish_implementation ();
18960 /* Parse an Objective-C declaration. */
18963 cp_parser_objc_declaration (cp_parser* parser)
18965 /* Try to figure out what kind of declaration is present. */
18966 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18968 switch (kwd->keyword)
18971 cp_parser_objc_alias_declaration (parser);
18974 cp_parser_objc_class_declaration (parser);
18976 case RID_AT_PROTOCOL:
18977 cp_parser_objc_protocol_declaration (parser);
18979 case RID_AT_INTERFACE:
18980 cp_parser_objc_class_interface (parser);
18982 case RID_AT_IMPLEMENTATION:
18983 cp_parser_objc_class_implementation (parser);
18986 cp_parser_objc_end_implementation (parser);
18989 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18990 cp_parser_skip_to_end_of_block_or_statement (parser);
18994 /* Parse an Objective-C try-catch-finally statement.
18996 objc-try-catch-finally-stmt:
18997 @try compound-statement objc-catch-clause-seq [opt]
18998 objc-finally-clause [opt]
19000 objc-catch-clause-seq:
19001 objc-catch-clause objc-catch-clause-seq [opt]
19004 @catch ( exception-declaration ) compound-statement
19006 objc-finally-clause
19007 @finally compound-statement
19009 Returns NULL_TREE. */
19012 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19013 location_t location;
19016 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19017 location = cp_lexer_peek_token (parser->lexer)->location;
19018 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19019 node, lest it get absorbed into the surrounding block. */
19020 stmt = push_stmt_list ();
19021 cp_parser_compound_statement (parser, NULL, false);
19022 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19024 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19026 cp_parameter_declarator *parmdecl;
19029 cp_lexer_consume_token (parser->lexer);
19030 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19031 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19032 parm = grokdeclarator (parmdecl->declarator,
19033 &parmdecl->decl_specifiers,
19034 PARM, /*initialized=*/0,
19035 /*attrlist=*/NULL);
19036 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19037 objc_begin_catch_clause (parm);
19038 cp_parser_compound_statement (parser, NULL, false);
19039 objc_finish_catch_clause ();
19042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19044 cp_lexer_consume_token (parser->lexer);
19045 location = cp_lexer_peek_token (parser->lexer)->location;
19046 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19047 node, lest it get absorbed into the surrounding block. */
19048 stmt = push_stmt_list ();
19049 cp_parser_compound_statement (parser, NULL, false);
19050 objc_build_finally_clause (location, pop_stmt_list (stmt));
19053 return objc_finish_try_stmt ();
19056 /* Parse an Objective-C synchronized statement.
19058 objc-synchronized-stmt:
19059 @synchronized ( expression ) compound-statement
19061 Returns NULL_TREE. */
19064 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19065 location_t location;
19068 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19070 location = cp_lexer_peek_token (parser->lexer)->location;
19071 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19072 lock = cp_parser_expression (parser, false);
19073 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19075 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19076 node, lest it get absorbed into the surrounding block. */
19077 stmt = push_stmt_list ();
19078 cp_parser_compound_statement (parser, NULL, false);
19080 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19083 /* Parse an Objective-C throw statement.
19086 @throw assignment-expression [opt] ;
19088 Returns a constructed '@throw' statement. */
19091 cp_parser_objc_throw_statement (cp_parser *parser) {
19092 tree expr = NULL_TREE;
19094 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19097 expr = cp_parser_assignment_expression (parser, false);
19099 cp_parser_consume_semicolon_at_end_of_statement (parser);
19101 return objc_build_throw_stmt (expr);
19104 /* Parse an Objective-C statement. */
19107 cp_parser_objc_statement (cp_parser * parser) {
19108 /* Try to figure out what kind of declaration is present. */
19109 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19111 switch (kwd->keyword)
19114 return cp_parser_objc_try_catch_finally_statement (parser);
19115 case RID_AT_SYNCHRONIZED:
19116 return cp_parser_objc_synchronized_statement (parser);
19118 return cp_parser_objc_throw_statement (parser);
19120 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19121 cp_parser_skip_to_end_of_block_or_statement (parser);
19124 return error_mark_node;
19127 /* OpenMP 2.5 parsing routines. */
19129 /* Returns name of the next clause.
19130 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19131 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19132 returned and the token is consumed. */
19134 static pragma_omp_clause
19135 cp_parser_omp_clause_name (cp_parser *parser)
19137 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19140 result = PRAGMA_OMP_CLAUSE_IF;
19141 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19142 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19143 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19144 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19145 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19147 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19148 const char *p = IDENTIFIER_POINTER (id);
19153 if (!strcmp ("copyin", p))
19154 result = PRAGMA_OMP_CLAUSE_COPYIN;
19155 else if (!strcmp ("copyprivate", p))
19156 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19159 if (!strcmp ("firstprivate", p))
19160 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19163 if (!strcmp ("lastprivate", p))
19164 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19167 if (!strcmp ("nowait", p))
19168 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19169 else if (!strcmp ("num_threads", p))
19170 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19173 if (!strcmp ("ordered", p))
19174 result = PRAGMA_OMP_CLAUSE_ORDERED;
19177 if (!strcmp ("reduction", p))
19178 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19181 if (!strcmp ("schedule", p))
19182 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19183 else if (!strcmp ("shared", p))
19184 result = PRAGMA_OMP_CLAUSE_SHARED;
19189 if (result != PRAGMA_OMP_CLAUSE_NONE)
19190 cp_lexer_consume_token (parser->lexer);
19195 /* Validate that a clause of the given type does not already exist. */
19198 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19202 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19203 if (OMP_CLAUSE_CODE (c) == code)
19205 error ("too many %qs clauses", name);
19213 variable-list , identifier
19215 In addition, we match a closing parenthesis. An opening parenthesis
19216 will have been consumed by the caller.
19218 If KIND is nonzero, create the appropriate node and install the decl
19219 in OMP_CLAUSE_DECL and add the node to the head of the list.
19221 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19222 return the list created. */
19225 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19232 name = cp_parser_id_expression (parser, /*template_p=*/false,
19233 /*check_dependency_p=*/true,
19234 /*template_p=*/NULL,
19235 /*declarator_p=*/false,
19236 /*optional_p=*/false);
19237 if (name == error_mark_node)
19240 decl = cp_parser_lookup_name_simple (parser, name);
19241 if (decl == error_mark_node)
19242 cp_parser_name_lookup_error (parser, name, decl, NULL);
19243 else if (kind != 0)
19245 tree u = build_omp_clause (kind);
19246 OMP_CLAUSE_DECL (u) = decl;
19247 OMP_CLAUSE_CHAIN (u) = list;
19251 list = tree_cons (decl, NULL_TREE, list);
19254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19256 cp_lexer_consume_token (parser->lexer);
19259 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19263 /* Try to resync to an unnested comma. Copied from
19264 cp_parser_parenthesized_expression_list. */
19266 ending = cp_parser_skip_to_closing_parenthesis (parser,
19267 /*recovering=*/true,
19269 /*consume_paren=*/true);
19277 /* Similarly, but expect leading and trailing parenthesis. This is a very
19278 common case for omp clauses. */
19281 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19283 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19284 return cp_parser_omp_var_list_no_open (parser, kind, list);
19289 default ( shared | none ) */
19292 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19294 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19297 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19299 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19301 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19302 const char *p = IDENTIFIER_POINTER (id);
19307 if (strcmp ("none", p) != 0)
19309 kind = OMP_CLAUSE_DEFAULT_NONE;
19313 if (strcmp ("shared", p) != 0)
19315 kind = OMP_CLAUSE_DEFAULT_SHARED;
19322 cp_lexer_consume_token (parser->lexer);
19327 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19330 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19331 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19332 /*or_comma=*/false,
19333 /*consume_paren=*/true);
19335 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19338 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19339 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19340 OMP_CLAUSE_CHAIN (c) = list;
19341 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19347 if ( expression ) */
19350 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19354 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19357 t = cp_parser_condition (parser);
19359 if (t == error_mark_node
19360 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19361 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19362 /*or_comma=*/false,
19363 /*consume_paren=*/true);
19365 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19367 c = build_omp_clause (OMP_CLAUSE_IF);
19368 OMP_CLAUSE_IF_EXPR (c) = t;
19369 OMP_CLAUSE_CHAIN (c) = list;
19378 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19382 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19384 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19385 OMP_CLAUSE_CHAIN (c) = list;
19390 num_threads ( expression ) */
19393 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19400 t = cp_parser_expression (parser, false);
19402 if (t == error_mark_node
19403 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19405 /*or_comma=*/false,
19406 /*consume_paren=*/true);
19408 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19410 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19411 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19412 OMP_CLAUSE_CHAIN (c) = list;
19421 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19425 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19427 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19428 OMP_CLAUSE_CHAIN (c) = list;
19433 reduction ( reduction-operator : variable-list )
19435 reduction-operator:
19436 One of: + * - & ^ | && || */
19439 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19441 enum tree_code code;
19444 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19447 switch (cp_lexer_peek_token (parser->lexer)->type)
19459 code = BIT_AND_EXPR;
19462 code = BIT_XOR_EXPR;
19465 code = BIT_IOR_EXPR;
19468 code = TRUTH_ANDIF_EXPR;
19471 code = TRUTH_ORIF_EXPR;
19474 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19476 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19477 /*or_comma=*/false,
19478 /*consume_paren=*/true);
19481 cp_lexer_consume_token (parser->lexer);
19483 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19486 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19487 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19488 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19494 schedule ( schedule-kind )
19495 schedule ( schedule-kind , expression )
19498 static | dynamic | guided | runtime */
19501 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19505 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19508 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19510 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19512 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19513 const char *p = IDENTIFIER_POINTER (id);
19518 if (strcmp ("dynamic", p) != 0)
19520 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19524 if (strcmp ("guided", p) != 0)
19526 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19530 if (strcmp ("runtime", p) != 0)
19532 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19539 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19540 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19543 cp_lexer_consume_token (parser->lexer);
19545 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19547 cp_lexer_consume_token (parser->lexer);
19549 t = cp_parser_assignment_expression (parser, false);
19551 if (t == error_mark_node)
19553 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19554 error ("schedule %<runtime%> does not take "
19555 "a %<chunk_size%> parameter");
19557 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19559 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19562 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19565 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19566 OMP_CLAUSE_CHAIN (c) = list;
19570 cp_parser_error (parser, "invalid schedule kind");
19572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19573 /*or_comma=*/false,
19574 /*consume_paren=*/true);
19578 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19579 is a bitmask in MASK. Return the list of clauses found; the result
19580 of clause default goes in *pdefault. */
19583 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19584 const char *where, cp_token *pragma_tok)
19586 tree clauses = NULL;
19588 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19590 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19591 const char *c_name;
19592 tree prev = clauses;
19596 case PRAGMA_OMP_CLAUSE_COPYIN:
19597 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19600 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19601 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19603 c_name = "copyprivate";
19605 case PRAGMA_OMP_CLAUSE_DEFAULT:
19606 clauses = cp_parser_omp_clause_default (parser, clauses);
19607 c_name = "default";
19609 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19610 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19612 c_name = "firstprivate";
19614 case PRAGMA_OMP_CLAUSE_IF:
19615 clauses = cp_parser_omp_clause_if (parser, clauses);
19618 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19619 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19621 c_name = "lastprivate";
19623 case PRAGMA_OMP_CLAUSE_NOWAIT:
19624 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19627 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19628 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19629 c_name = "num_threads";
19631 case PRAGMA_OMP_CLAUSE_ORDERED:
19632 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19633 c_name = "ordered";
19635 case PRAGMA_OMP_CLAUSE_PRIVATE:
19636 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19638 c_name = "private";
19640 case PRAGMA_OMP_CLAUSE_REDUCTION:
19641 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19642 c_name = "reduction";
19644 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19645 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19646 c_name = "schedule";
19648 case PRAGMA_OMP_CLAUSE_SHARED:
19649 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19654 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19658 if (((mask >> c_kind) & 1) == 0)
19660 /* Remove the invalid clause(s) from the list to avoid
19661 confusing the rest of the compiler. */
19663 error ("%qs is not valid for %qs", c_name, where);
19667 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19668 return finish_omp_clauses (clauses);
19675 In practice, we're also interested in adding the statement to an
19676 outer node. So it is convenient if we work around the fact that
19677 cp_parser_statement calls add_stmt. */
19680 cp_parser_begin_omp_structured_block (cp_parser *parser)
19682 unsigned save = parser->in_statement;
19684 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19685 This preserves the "not within loop or switch" style error messages
19686 for nonsense cases like
19692 if (parser->in_statement)
19693 parser->in_statement = IN_OMP_BLOCK;
19699 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19701 parser->in_statement = save;
19705 cp_parser_omp_structured_block (cp_parser *parser)
19707 tree stmt = begin_omp_structured_block ();
19708 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19710 cp_parser_statement (parser, NULL_TREE, false, NULL);
19712 cp_parser_end_omp_structured_block (parser, save);
19713 return finish_omp_structured_block (stmt);
19717 # pragma omp atomic new-line
19721 x binop= expr | x++ | ++x | x-- | --x
19723 +, *, -, /, &, ^, |, <<, >>
19725 where x is an lvalue expression with scalar type. */
19728 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19731 enum tree_code code;
19733 cp_parser_require_pragma_eol (parser, pragma_tok);
19735 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19737 switch (TREE_CODE (lhs))
19742 case PREINCREMENT_EXPR:
19743 case POSTINCREMENT_EXPR:
19744 lhs = TREE_OPERAND (lhs, 0);
19746 rhs = integer_one_node;
19749 case PREDECREMENT_EXPR:
19750 case POSTDECREMENT_EXPR:
19751 lhs = TREE_OPERAND (lhs, 0);
19753 rhs = integer_one_node;
19757 switch (cp_lexer_peek_token (parser->lexer)->type)
19763 code = TRUNC_DIV_EXPR;
19771 case CPP_LSHIFT_EQ:
19772 code = LSHIFT_EXPR;
19774 case CPP_RSHIFT_EQ:
19775 code = RSHIFT_EXPR;
19778 code = BIT_AND_EXPR;
19781 code = BIT_IOR_EXPR;
19784 code = BIT_XOR_EXPR;
19787 cp_parser_error (parser,
19788 "invalid operator for %<#pragma omp atomic%>");
19791 cp_lexer_consume_token (parser->lexer);
19793 rhs = cp_parser_expression (parser, false);
19794 if (rhs == error_mark_node)
19798 finish_omp_atomic (code, lhs, rhs);
19799 cp_parser_consume_semicolon_at_end_of_statement (parser);
19803 cp_parser_skip_to_end_of_block_or_statement (parser);
19808 # pragma omp barrier new-line */
19811 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19813 cp_parser_require_pragma_eol (parser, pragma_tok);
19814 finish_omp_barrier ();
19818 # pragma omp critical [(name)] new-line
19819 structured-block */
19822 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19824 tree stmt, name = NULL;
19826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19828 cp_lexer_consume_token (parser->lexer);
19830 name = cp_parser_identifier (parser);
19832 if (name == error_mark_node
19833 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19835 /*or_comma=*/false,
19836 /*consume_paren=*/true);
19837 if (name == error_mark_node)
19840 cp_parser_require_pragma_eol (parser, pragma_tok);
19842 stmt = cp_parser_omp_structured_block (parser);
19843 return c_finish_omp_critical (stmt, name);
19847 # pragma omp flush flush-vars[opt] new-line
19850 ( variable-list ) */
19853 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19856 (void) cp_parser_omp_var_list (parser, 0, NULL);
19857 cp_parser_require_pragma_eol (parser, pragma_tok);
19859 finish_omp_flush ();
19862 /* Parse the restricted form of the for statment allowed by OpenMP. */
19865 cp_parser_omp_for_loop (cp_parser *parser)
19867 tree init, cond, incr, body, decl, pre_body;
19870 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19872 cp_parser_error (parser, "for statement expected");
19875 loc = cp_lexer_consume_token (parser->lexer)->location;
19876 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19879 init = decl = NULL;
19880 pre_body = push_stmt_list ();
19881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19883 cp_decl_specifier_seq type_specifiers;
19885 /* First, try to parse as an initialized declaration. See
19886 cp_parser_condition, from whence the bulk of this is copied. */
19888 cp_parser_parse_tentatively (parser);
19889 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19891 if (!cp_parser_error_occurred (parser))
19893 tree asm_specification, attributes;
19894 cp_declarator *declarator;
19896 declarator = cp_parser_declarator (parser,
19897 CP_PARSER_DECLARATOR_NAMED,
19898 /*ctor_dtor_or_conv_p=*/NULL,
19899 /*parenthesized_p=*/NULL,
19900 /*member_p=*/false);
19901 attributes = cp_parser_attributes_opt (parser);
19902 asm_specification = cp_parser_asm_specification_opt (parser);
19904 cp_parser_require (parser, CPP_EQ, "`='");
19905 if (cp_parser_parse_definitely (parser))
19909 decl = start_decl (declarator, &type_specifiers,
19910 /*initialized_p=*/false, attributes,
19911 /*prefix_attributes=*/NULL_TREE,
19914 init = cp_parser_assignment_expression (parser, false);
19916 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19917 asm_specification, LOOKUP_ONLYCONVERTING);
19920 pop_scope (pushed_scope);
19924 cp_parser_abort_tentative_parse (parser);
19926 /* If parsing as an initialized declaration failed, try again as
19927 a simple expression. */
19929 init = cp_parser_expression (parser, false);
19931 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19932 pre_body = pop_stmt_list (pre_body);
19935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19936 cond = cp_parser_condition (parser);
19937 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19940 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19941 incr = cp_parser_expression (parser, false);
19943 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19944 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19945 /*or_comma=*/false,
19946 /*consume_paren=*/true);
19948 /* Note that we saved the original contents of this flag when we entered
19949 the structured block, and so we don't need to re-save it here. */
19950 parser->in_statement = IN_OMP_FOR;
19952 /* Note that the grammar doesn't call for a structured block here,
19953 though the loop as a whole is a structured block. */
19954 body = push_stmt_list ();
19955 cp_parser_statement (parser, NULL_TREE, false, NULL);
19956 body = pop_stmt_list (body);
19958 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19962 #pragma omp for for-clause[optseq] new-line
19965 #define OMP_FOR_CLAUSE_MASK \
19966 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19967 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19968 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19969 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19970 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19971 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19972 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19975 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19977 tree clauses, sb, ret;
19980 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19981 "#pragma omp for", pragma_tok);
19983 sb = begin_omp_structured_block ();
19984 save = cp_parser_begin_omp_structured_block (parser);
19986 ret = cp_parser_omp_for_loop (parser);
19988 OMP_FOR_CLAUSES (ret) = clauses;
19990 cp_parser_end_omp_structured_block (parser, save);
19991 add_stmt (finish_omp_structured_block (sb));
19997 # pragma omp master new-line
19998 structured-block */
20001 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20003 cp_parser_require_pragma_eol (parser, pragma_tok);
20004 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20008 # pragma omp ordered new-line
20009 structured-block */
20012 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20014 cp_parser_require_pragma_eol (parser, pragma_tok);
20015 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20021 { section-sequence }
20024 section-directive[opt] structured-block
20025 section-sequence section-directive structured-block */
20028 cp_parser_omp_sections_scope (cp_parser *parser)
20030 tree stmt, substmt;
20031 bool error_suppress = false;
20034 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20037 stmt = push_stmt_list ();
20039 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20043 substmt = begin_omp_structured_block ();
20044 save = cp_parser_begin_omp_structured_block (parser);
20048 cp_parser_statement (parser, NULL_TREE, false, NULL);
20050 tok = cp_lexer_peek_token (parser->lexer);
20051 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20053 if (tok->type == CPP_CLOSE_BRACE)
20055 if (tok->type == CPP_EOF)
20059 cp_parser_end_omp_structured_block (parser, save);
20060 substmt = finish_omp_structured_block (substmt);
20061 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20062 add_stmt (substmt);
20067 tok = cp_lexer_peek_token (parser->lexer);
20068 if (tok->type == CPP_CLOSE_BRACE)
20070 if (tok->type == CPP_EOF)
20073 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20075 cp_lexer_consume_token (parser->lexer);
20076 cp_parser_require_pragma_eol (parser, tok);
20077 error_suppress = false;
20079 else if (!error_suppress)
20081 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20082 error_suppress = true;
20085 substmt = cp_parser_omp_structured_block (parser);
20086 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20087 add_stmt (substmt);
20089 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20091 substmt = pop_stmt_list (stmt);
20093 stmt = make_node (OMP_SECTIONS);
20094 TREE_TYPE (stmt) = void_type_node;
20095 OMP_SECTIONS_BODY (stmt) = substmt;
20102 # pragma omp sections sections-clause[optseq] newline
20105 #define OMP_SECTIONS_CLAUSE_MASK \
20106 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20107 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20108 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20109 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20110 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20113 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20117 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20118 "#pragma omp sections", pragma_tok);
20120 ret = cp_parser_omp_sections_scope (parser);
20122 OMP_SECTIONS_CLAUSES (ret) = clauses;
20128 # pragma parallel parallel-clause new-line
20129 # pragma parallel for parallel-for-clause new-line
20130 # pragma parallel sections parallel-sections-clause new-line */
20132 #define OMP_PARALLEL_CLAUSE_MASK \
20133 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20134 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20135 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20136 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20137 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20138 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20139 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20140 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20143 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20145 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20146 const char *p_name = "#pragma omp parallel";
20147 tree stmt, clauses, par_clause, ws_clause, block;
20148 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20153 cp_lexer_consume_token (parser->lexer);
20154 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20155 p_name = "#pragma omp parallel for";
20156 mask |= OMP_FOR_CLAUSE_MASK;
20157 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20159 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20161 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20162 const char *p = IDENTIFIER_POINTER (id);
20163 if (strcmp (p, "sections") == 0)
20165 cp_lexer_consume_token (parser->lexer);
20166 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20167 p_name = "#pragma omp parallel sections";
20168 mask |= OMP_SECTIONS_CLAUSE_MASK;
20169 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20173 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20174 block = begin_omp_parallel ();
20175 save = cp_parser_begin_omp_structured_block (parser);
20179 case PRAGMA_OMP_PARALLEL:
20180 cp_parser_already_scoped_statement (parser);
20181 par_clause = clauses;
20184 case PRAGMA_OMP_PARALLEL_FOR:
20185 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20186 stmt = cp_parser_omp_for_loop (parser);
20188 OMP_FOR_CLAUSES (stmt) = ws_clause;
20191 case PRAGMA_OMP_PARALLEL_SECTIONS:
20192 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20193 stmt = cp_parser_omp_sections_scope (parser);
20195 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20199 gcc_unreachable ();
20202 cp_parser_end_omp_structured_block (parser, save);
20203 stmt = finish_omp_parallel (par_clause, block);
20204 if (p_kind != PRAGMA_OMP_PARALLEL)
20205 OMP_PARALLEL_COMBINED (stmt) = 1;
20210 # pragma omp single single-clause[optseq] new-line
20211 structured-block */
20213 #define OMP_SINGLE_CLAUSE_MASK \
20214 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20215 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20216 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20217 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20220 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20222 tree stmt = make_node (OMP_SINGLE);
20223 TREE_TYPE (stmt) = void_type_node;
20225 OMP_SINGLE_CLAUSES (stmt)
20226 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20227 "#pragma omp single", pragma_tok);
20228 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20230 return add_stmt (stmt);
20234 # pragma omp threadprivate (variable-list) */
20237 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20241 vars = cp_parser_omp_var_list (parser, 0, NULL);
20242 cp_parser_require_pragma_eol (parser, pragma_tok);
20244 finish_omp_threadprivate (vars);
20247 /* Main entry point to OpenMP statement pragmas. */
20250 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20254 switch (pragma_tok->pragma_kind)
20256 case PRAGMA_OMP_ATOMIC:
20257 cp_parser_omp_atomic (parser, pragma_tok);
20259 case PRAGMA_OMP_CRITICAL:
20260 stmt = cp_parser_omp_critical (parser, pragma_tok);
20262 case PRAGMA_OMP_FOR:
20263 stmt = cp_parser_omp_for (parser, pragma_tok);
20265 case PRAGMA_OMP_MASTER:
20266 stmt = cp_parser_omp_master (parser, pragma_tok);
20268 case PRAGMA_OMP_ORDERED:
20269 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20271 case PRAGMA_OMP_PARALLEL:
20272 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20274 case PRAGMA_OMP_SECTIONS:
20275 stmt = cp_parser_omp_sections (parser, pragma_tok);
20277 case PRAGMA_OMP_SINGLE:
20278 stmt = cp_parser_omp_single (parser, pragma_tok);
20281 gcc_unreachable ();
20285 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20290 static GTY (()) cp_parser *the_parser;
20293 /* Special handling for the first token or line in the file. The first
20294 thing in the file might be #pragma GCC pch_preprocess, which loads a
20295 PCH file, which is a GC collection point. So we need to handle this
20296 first pragma without benefit of an existing lexer structure.
20298 Always returns one token to the caller in *FIRST_TOKEN. This is
20299 either the true first token of the file, or the first token after
20300 the initial pragma. */
20303 cp_parser_initial_pragma (cp_token *first_token)
20307 cp_lexer_get_preprocessor_token (NULL, first_token);
20308 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20311 cp_lexer_get_preprocessor_token (NULL, first_token);
20312 if (first_token->type == CPP_STRING)
20314 name = first_token->u.value;
20316 cp_lexer_get_preprocessor_token (NULL, first_token);
20317 if (first_token->type != CPP_PRAGMA_EOL)
20318 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20321 error ("expected string literal");
20323 /* Skip to the end of the pragma. */
20324 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20325 cp_lexer_get_preprocessor_token (NULL, first_token);
20327 /* Now actually load the PCH file. */
20329 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20331 /* Read one more token to return to our caller. We have to do this
20332 after reading the PCH file in, since its pointers have to be
20334 cp_lexer_get_preprocessor_token (NULL, first_token);
20337 /* Normal parsing of a pragma token. Here we can (and must) use the
20341 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20343 cp_token *pragma_tok;
20346 pragma_tok = cp_lexer_consume_token (parser->lexer);
20347 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20348 parser->lexer->in_pragma = true;
20350 id = pragma_tok->pragma_kind;
20353 case PRAGMA_GCC_PCH_PREPROCESS:
20354 error ("%<#pragma GCC pch_preprocess%> must be first");
20357 case PRAGMA_OMP_BARRIER:
20360 case pragma_compound:
20361 cp_parser_omp_barrier (parser, pragma_tok);
20364 error ("%<#pragma omp barrier%> may only be "
20365 "used in compound statements");
20372 case PRAGMA_OMP_FLUSH:
20375 case pragma_compound:
20376 cp_parser_omp_flush (parser, pragma_tok);
20379 error ("%<#pragma omp flush%> may only be "
20380 "used in compound statements");
20387 case PRAGMA_OMP_THREADPRIVATE:
20388 cp_parser_omp_threadprivate (parser, pragma_tok);
20391 case PRAGMA_OMP_ATOMIC:
20392 case PRAGMA_OMP_CRITICAL:
20393 case PRAGMA_OMP_FOR:
20394 case PRAGMA_OMP_MASTER:
20395 case PRAGMA_OMP_ORDERED:
20396 case PRAGMA_OMP_PARALLEL:
20397 case PRAGMA_OMP_SECTIONS:
20398 case PRAGMA_OMP_SINGLE:
20399 if (context == pragma_external)
20401 cp_parser_omp_construct (parser, pragma_tok);
20404 case PRAGMA_OMP_SECTION:
20405 error ("%<#pragma omp section%> may only be used in "
20406 "%<#pragma omp sections%> construct");
20410 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20411 c_invoke_pragma_handler (id);
20415 cp_parser_error (parser, "expected declaration specifiers");
20419 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20423 /* The interface the pragma parsers have to the lexer. */
20426 pragma_lex (tree *value)
20429 enum cpp_ttype ret;
20431 tok = cp_lexer_peek_token (the_parser->lexer);
20434 *value = tok->u.value;
20436 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20438 else if (ret == CPP_STRING)
20439 *value = cp_parser_string_literal (the_parser, false, false);
20442 cp_lexer_consume_token (the_parser->lexer);
20443 if (ret == CPP_KEYWORD)
20451 /* External interface. */
20453 /* Parse one entire translation unit. */
20456 c_parse_file (void)
20458 bool error_occurred;
20459 static bool already_called = false;
20461 if (already_called)
20463 sorry ("inter-module optimizations not implemented for C++");
20466 already_called = true;
20468 the_parser = cp_parser_new ();
20469 push_deferring_access_checks (flag_access_control
20470 ? dk_no_deferred : dk_no_check);
20471 error_occurred = cp_parser_translation_unit (the_parser);
20475 #include "gt-cp-parser.h"