2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 /* The location at which this token was found. */
93 /* We use a stack of token pointer for saving token sets. */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
98 static cp_token eof_token =
100 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
104 /* The cp_lexer structure represents the C++ lexer. It is responsible
105 for managing the token stream from the preprocessor and supplying
106 it to the parser. Tokens are never added to the cp_lexer after
109 typedef struct cp_lexer GTY (())
111 /* The memory allocated for the buffer. NULL if this lexer does not
112 own the token buffer. */
113 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114 /* If the lexer owns the buffer, this is the number of tokens in the
116 size_t buffer_length;
118 /* A pointer just past the last available token. The tokens
119 in this lexer are [buffer, last_token). */
120 cp_token_position GTY ((skip)) last_token;
122 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
123 no more available tokens. */
124 cp_token_position GTY ((skip)) next_token;
126 /* A stack indicating positions at which cp_lexer_save_tokens was
127 called. The top entry is the most recent position at which we
128 began saving tokens. If the stack is non-empty, we are saving
130 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
132 /* The next lexer in a linked list of lexers. */
133 struct cp_lexer *next;
135 /* True if we should output debugging information. */
138 /* True if we're in the context of parsing a pragma, and should not
139 increment past the end-of-line marker. */
143 /* cp_token_cache is a range of tokens. There is no need to represent
144 allocate heap memory for it, since tokens are never removed from the
145 lexer's array. There is also no need for the GC to walk through
146 a cp_token_cache, since everything in here is referenced through
149 typedef struct cp_token_cache GTY(())
151 /* The beginning of the token range. */
152 cp_token * GTY((skip)) first;
154 /* Points immediately after the last token in the range. */
155 cp_token * GTY ((skip)) last;
160 static cp_lexer *cp_lexer_new_main
162 static cp_lexer *cp_lexer_new_from_tokens
163 (cp_token_cache *tokens);
164 static void cp_lexer_destroy
166 static int cp_lexer_saving_tokens
168 static cp_token_position cp_lexer_token_position
170 static cp_token *cp_lexer_token_at
171 (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173 (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
176 static cp_token *cp_lexer_peek_nth_token
177 (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181 (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183 (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
186 static void cp_lexer_purge_token
188 static void cp_lexer_purge_tokens_after
189 (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
192 static void cp_lexer_commit_tokens
194 static void cp_lexer_rollback_tokens
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198 (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
201 static void cp_lexer_start_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204 (cp_lexer *) ATTRIBUTE_UNUSED;
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207 about passing NULL to functions that require non-NULL arguments
208 (fputs, fprintf). It will never be used, so all we need is a value
209 of the right type that's guaranteed not to be NULL. */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
215 static cp_token_cache *cp_token_cache_new
216 (cp_token *, cp_token *);
218 static void cp_parser_initial_pragma
221 /* Manifest constants. */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
225 /* A token type for keywords, as opposed to ordinary identifiers. */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
228 /* A token type for template-ids. If a template-id is processed while
229 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230 the value of the CPP_TEMPLATE_ID is whatever was returned by
231 cp_parser_template_id. */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
234 /* A token type for nested-name-specifiers. If a
235 nested-name-specifier is processed while parsing tentatively, it is
236 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238 cp_parser_nested_name_specifier_opt. */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
241 /* A token type for tokens that are not tokens at all; these are used
242 to represent slots in the array where there used to be a token
243 that has now been deleted. */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
246 /* The number of token types, including C++-specific ones. */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
260 cp_lexer_new_main (void)
262 cp_token first_token;
269 /* It's possible that parsing the first pragma will load a PCH file,
270 which is a GC collection point. So we have to do that before
271 allocating any memory. */
272 cp_parser_initial_pragma (&first_token);
274 c_common_no_more_pch ();
276 /* Allocate the memory. */
277 lexer = GGC_CNEW (cp_lexer);
279 #ifdef ENABLE_CHECKING
280 /* Initially we are not debugging. */
281 lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284 CP_SAVED_TOKEN_STACK);
286 /* Create the buffer. */
287 alloc = CP_LEXER_BUFFER_SIZE;
288 buffer = GGC_NEWVEC (cp_token, alloc);
290 /* Put the first token in the buffer. */
295 /* Get the remaining tokens from the preprocessor. */
296 while (pos->type != CPP_EOF)
303 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304 pos = buffer + space;
306 cp_lexer_get_preprocessor_token (lexer, pos);
308 lexer->buffer = buffer;
309 lexer->buffer_length = alloc - space;
310 lexer->last_token = pos;
311 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
313 /* Subsequent preprocessor diagnostics should use compiler
314 diagnostic functions to get the compiler source location. */
315 cpp_get_options (parse_in)->client_diagnostic = true;
316 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
318 gcc_assert (lexer->next_token->type != CPP_PURGED);
322 /* Create a new lexer whose token stream is primed with the tokens in
323 CACHE. When these tokens are exhausted, no new tokens will be read. */
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
328 cp_token *first = cache->first;
329 cp_token *last = cache->last;
330 cp_lexer *lexer = GGC_CNEW (cp_lexer);
332 /* We do not own the buffer. */
333 lexer->buffer = NULL;
334 lexer->buffer_length = 0;
335 lexer->next_token = first == last ? &eof_token : first;
336 lexer->last_token = last;
338 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339 CP_SAVED_TOKEN_STACK);
341 #ifdef ENABLE_CHECKING
342 /* Initially we are not debugging. */
343 lexer->debugging_p = false;
346 gcc_assert (lexer->next_token->type != CPP_PURGED);
350 /* Frees all resources associated with LEXER. */
353 cp_lexer_destroy (cp_lexer *lexer)
356 ggc_free (lexer->buffer);
357 VEC_free (cp_token_position, heap, lexer->saved_tokens);
361 /* Returns nonzero if debugging information should be output. */
363 #ifdef ENABLE_CHECKING
366 cp_lexer_debugging_p (cp_lexer *lexer)
368 return lexer->debugging_p;
371 #endif /* ENABLE_CHECKING */
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
376 gcc_assert (!previous_p || lexer->next_token != &eof_token);
378 return lexer->next_token - previous_p;
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
387 /* nonzero if we are presently saving tokens. */
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
392 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
395 /* Store the next token from the preprocessor in *TOKEN. Return true
396 if we reach EOF. If LEXER is NULL, assume we are handling an
397 initial #pragma pch_preprocess, and thus want the lexer to return
398 processed strings. */
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
403 static int is_extern_c = 0;
405 /* Get a new token from the preprocessor. */
407 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409 token->keyword = RID_MAX;
410 token->pragma_kind = PRAGMA_NONE;
411 token->in_system_header = in_system_header;
413 /* On some systems, some header files are surrounded by an
414 implicit extern "C" block. Set a flag in the token if it
415 comes from such a header. */
416 is_extern_c += pending_lang_change;
417 pending_lang_change = 0;
418 token->implicit_extern_c = is_extern_c > 0;
420 /* Check to see if this token is a keyword. */
421 if (token->type == CPP_NAME)
423 if (C_IS_RESERVED_WORD (token->u.value))
425 /* Mark this token as a keyword. */
426 token->type = CPP_KEYWORD;
427 /* Record which keyword. */
428 token->keyword = C_RID_CODE (token->u.value);
429 /* Update the value. Some keywords are mapped to particular
430 entities, rather than simply having the value of the
431 corresponding IDENTIFIER_NODE. For example, `__const' is
432 mapped to `const'. */
433 token->u.value = ridpointers[token->keyword];
437 if (warn_cxx0x_compat
438 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
441 /* Warn about the C++0x keyword (but still treat it as
443 warning (OPT_Wc__0x_compat,
444 "identifier %<%s%> will become a keyword in C++0x",
445 IDENTIFIER_POINTER (token->u.value));
447 /* Clear out the C_RID_CODE so we don't warn about this
448 particular identifier-turned-keyword again. */
449 C_RID_CODE (token->u.value) = RID_MAX;
452 token->ambiguous_p = false;
453 token->keyword = RID_MAX;
456 /* Handle Objective-C++ keywords. */
457 else if (token->type == CPP_AT_NAME)
459 token->type = CPP_KEYWORD;
460 switch (C_RID_CODE (token->u.value))
462 /* Map 'class' to '@class', 'private' to '@private', etc. */
463 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467 case RID_THROW: token->keyword = RID_AT_THROW; break;
468 case RID_TRY: token->keyword = RID_AT_TRY; break;
469 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470 default: token->keyword = C_RID_CODE (token->u.value);
473 else if (token->type == CPP_PRAGMA)
475 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
476 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477 token->u.value = NULL_TREE;
481 /* Update the globals input_location and in_system_header and the
482 input file stack from TOKEN. */
484 cp_lexer_set_source_position_from_token (cp_token *token)
486 if (token->type != CPP_EOF)
488 input_location = token->location;
489 in_system_header = token->in_system_header;
493 /* Return a pointer to the next token in the token stream, but do not
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
499 if (cp_lexer_debugging_p (lexer))
501 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503 putc ('\n', cp_lexer_debug_stream);
505 return lexer->next_token;
508 /* Return true if the next token has the indicated TYPE. */
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
513 return cp_lexer_peek_token (lexer)->type == type;
516 /* Return true if the next token does not have the indicated TYPE. */
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
521 return !cp_lexer_next_token_is (lexer, type);
524 /* Return true if the next token is the indicated KEYWORD. */
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
529 return cp_lexer_peek_token (lexer)->keyword == keyword;
532 /* Return true if the next token is a keyword for a decl-specifier. */
535 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 token = cp_lexer_peek_token (lexer);
540 switch (token->keyword)
542 /* auto specifier: storage-class-specifier in C++,
543 simple-type-specifier in C++0x. */
545 /* Storage classes. */
551 /* Elaborated type specifiers. */
557 /* Simple type specifiers. */
569 /* GNU extensions. */
572 /* C++0x extensions. */
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 /* N is 1-based, not zero-based. */
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
605 if (token == lexer->last_token)
611 if (token->type != CPP_PURGED)
615 if (cp_lexer_debugging_p (lexer))
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
628 cp_lexer_consume_token (cp_lexer* lexer)
630 cp_token *token = lexer->next_token;
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 if (lexer->next_token == lexer->last_token)
640 lexer->next_token = &eof_token;
645 while (lexer->next_token->type == CPP_PURGED);
647 cp_lexer_set_source_position_from_token (token);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
665 cp_lexer_purge_token (cp_lexer *lexer)
667 cp_token *tok = lexer->next_token;
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
678 if (tok == lexer->last_token)
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 cp_token *peek = lexer->next_token;
697 if (peek == &eof_token)
698 peek = lexer->last_token;
700 gcc_assert (tok < peek);
702 for ( tok += 1; tok != peek; tok += 1)
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
711 /* Begin saving tokens. All tokens consumed after this point will be
715 cp_lexer_save_tokens (cp_lexer* lexer)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
725 /* Commit to the portion of the token stream most recently saved. */
728 cp_lexer_commit_tokens (cp_lexer* lexer)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position, lexer->saved_tokens);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
755 cp_lexer_print_token (FILE * stream, cp_token *token)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
766 /* C++ parser token types - see "Manifest constants", above. */
769 "NESTED_NAME_SPECIFIER",
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
778 /* For some tokens, print the associated data. */
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 /* else fall through */
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
793 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801 /* Start emitting debugging information. */
804 cp_lexer_start_debugging (cp_lexer* lexer)
806 lexer->debugging_p = true;
809 /* Stop emitting debugging information. */
812 cp_lexer_stop_debugging (cp_lexer* lexer)
814 lexer->debugging_p = false;
817 #endif /* ENABLE_CHECKING */
819 /* Create a new cp_token_cache, representing a range of tokens. */
821 static cp_token_cache *
822 cp_token_cache_new (cp_token *first, cp_token *last)
824 cp_token_cache *cache = GGC_NEW (cp_token_cache);
825 cache->first = first;
831 /* Decl-specifiers. */
833 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
836 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
838 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
843 /* Nothing other than the parser should be creating declarators;
844 declarators are a semi-syntactic representation of C++ entities.
845 Other parts of the front end that need to create entities (like
846 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
848 static cp_declarator *make_call_declarator
849 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
850 static cp_declarator *make_array_declarator
851 (cp_declarator *, tree);
852 static cp_declarator *make_pointer_declarator
853 (cp_cv_quals, cp_declarator *);
854 static cp_declarator *make_reference_declarator
855 (cp_cv_quals, cp_declarator *, bool);
856 static cp_parameter_declarator *make_parameter_declarator
857 (cp_decl_specifier_seq *, cp_declarator *, tree);
858 static cp_declarator *make_ptrmem_declarator
859 (cp_cv_quals, tree, cp_declarator *);
861 /* An erroneous declarator. */
862 static cp_declarator *cp_error_declarator;
864 /* The obstack on which declarators and related data structures are
866 static struct obstack declarator_obstack;
868 /* Alloc BYTES from the declarator memory pool. */
871 alloc_declarator (size_t bytes)
873 return obstack_alloc (&declarator_obstack, bytes);
876 /* Allocate a declarator of the indicated KIND. Clear fields that are
877 common to all declarators. */
879 static cp_declarator *
880 make_declarator (cp_declarator_kind kind)
882 cp_declarator *declarator;
884 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
885 declarator->kind = kind;
886 declarator->attributes = NULL_TREE;
887 declarator->declarator = NULL;
888 declarator->parameter_pack_p = false;
893 /* Make a declarator for a generalized identifier. If
894 QUALIFYING_SCOPE is non-NULL, the identifier is
895 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
896 UNQUALIFIED_NAME. SFK indicates the kind of special function this
899 static cp_declarator *
900 make_id_declarator (tree qualifying_scope, tree unqualified_name,
901 special_function_kind sfk)
903 cp_declarator *declarator;
905 /* It is valid to write:
907 class C { void f(); };
911 The standard is not clear about whether `typedef const C D' is
912 legal; as of 2002-09-15 the committee is considering that
913 question. EDG 3.0 allows that syntax. Therefore, we do as
915 if (qualifying_scope && TYPE_P (qualifying_scope))
916 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
918 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
919 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
920 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
922 declarator = make_declarator (cdk_id);
923 declarator->u.id.qualifying_scope = qualifying_scope;
924 declarator->u.id.unqualified_name = unqualified_name;
925 declarator->u.id.sfk = sfk;
930 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
931 of modifiers such as const or volatile to apply to the pointer
932 type, represented as identifiers. */
935 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
937 cp_declarator *declarator;
939 declarator = make_declarator (cdk_pointer);
940 declarator->declarator = target;
941 declarator->u.pointer.qualifiers = cv_qualifiers;
942 declarator->u.pointer.class_type = NULL_TREE;
945 declarator->parameter_pack_p = target->parameter_pack_p;
946 target->parameter_pack_p = false;
949 declarator->parameter_pack_p = false;
954 /* Like make_pointer_declarator -- but for references. */
957 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
960 cp_declarator *declarator;
962 declarator = make_declarator (cdk_reference);
963 declarator->declarator = target;
964 declarator->u.reference.qualifiers = cv_qualifiers;
965 declarator->u.reference.rvalue_ref = rvalue_ref;
968 declarator->parameter_pack_p = target->parameter_pack_p;
969 target->parameter_pack_p = false;
972 declarator->parameter_pack_p = false;
977 /* Like make_pointer_declarator -- but for a pointer to a non-static
978 member of CLASS_TYPE. */
981 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
982 cp_declarator *pointee)
984 cp_declarator *declarator;
986 declarator = make_declarator (cdk_ptrmem);
987 declarator->declarator = pointee;
988 declarator->u.pointer.qualifiers = cv_qualifiers;
989 declarator->u.pointer.class_type = class_type;
993 declarator->parameter_pack_p = pointee->parameter_pack_p;
994 pointee->parameter_pack_p = false;
997 declarator->parameter_pack_p = false;
1002 /* Make a declarator for the function given by TARGET, with the
1003 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1004 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1005 indicates what exceptions can be thrown. */
1008 make_call_declarator (cp_declarator *target,
1009 cp_parameter_declarator *parms,
1010 cp_cv_quals cv_qualifiers,
1011 tree exception_specification)
1013 cp_declarator *declarator;
1015 declarator = make_declarator (cdk_function);
1016 declarator->declarator = target;
1017 declarator->u.function.parameters = parms;
1018 declarator->u.function.qualifiers = cv_qualifiers;
1019 declarator->u.function.exception_specification = exception_specification;
1022 declarator->parameter_pack_p = target->parameter_pack_p;
1023 target->parameter_pack_p = false;
1026 declarator->parameter_pack_p = false;
1031 /* Make a declarator for an array of BOUNDS elements, each of which is
1032 defined by ELEMENT. */
1035 make_array_declarator (cp_declarator *element, tree bounds)
1037 cp_declarator *declarator;
1039 declarator = make_declarator (cdk_array);
1040 declarator->declarator = element;
1041 declarator->u.array.bounds = bounds;
1044 declarator->parameter_pack_p = element->parameter_pack_p;
1045 element->parameter_pack_p = false;
1048 declarator->parameter_pack_p = false;
1053 /* Determine whether the declarator we've seen so far can be a
1054 parameter pack, when followed by an ellipsis. */
1056 declarator_can_be_parameter_pack (cp_declarator *declarator)
1058 /* Search for a declarator name, or any other declarator that goes
1059 after the point where the ellipsis could appear in a parameter
1060 pack. If we find any of these, then this declarator can not be
1061 made into a parameter pack. */
1063 while (declarator && !found)
1065 switch ((int)declarator->kind)
1076 declarator = declarator->declarator;
1084 cp_parameter_declarator *no_parameters;
1086 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1087 DECLARATOR and DEFAULT_ARGUMENT. */
1089 cp_parameter_declarator *
1090 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1091 cp_declarator *declarator,
1092 tree default_argument)
1094 cp_parameter_declarator *parameter;
1096 parameter = ((cp_parameter_declarator *)
1097 alloc_declarator (sizeof (cp_parameter_declarator)));
1098 parameter->next = NULL;
1099 if (decl_specifiers)
1100 parameter->decl_specifiers = *decl_specifiers;
1102 clear_decl_specs (¶meter->decl_specifiers);
1103 parameter->declarator = declarator;
1104 parameter->default_argument = default_argument;
1105 parameter->ellipsis_p = false;
1110 /* Returns true iff DECLARATOR is a declaration for a function. */
1113 function_declarator_p (const cp_declarator *declarator)
1117 if (declarator->kind == cdk_function
1118 && declarator->declarator->kind == cdk_id)
1120 if (declarator->kind == cdk_id
1121 || declarator->kind == cdk_error)
1123 declarator = declarator->declarator;
1133 A cp_parser parses the token stream as specified by the C++
1134 grammar. Its job is purely parsing, not semantic analysis. For
1135 example, the parser breaks the token stream into declarators,
1136 expressions, statements, and other similar syntactic constructs.
1137 It does not check that the types of the expressions on either side
1138 of an assignment-statement are compatible, or that a function is
1139 not declared with a parameter of type `void'.
1141 The parser invokes routines elsewhere in the compiler to perform
1142 semantic analysis and to build up the abstract syntax tree for the
1145 The parser (and the template instantiation code, which is, in a
1146 way, a close relative of parsing) are the only parts of the
1147 compiler that should be calling push_scope and pop_scope, or
1148 related functions. The parser (and template instantiation code)
1149 keeps track of what scope is presently active; everything else
1150 should simply honor that. (The code that generates static
1151 initializers may also need to set the scope, in order to check
1152 access control correctly when emitting the initializers.)
1157 The parser is of the standard recursive-descent variety. Upcoming
1158 tokens in the token stream are examined in order to determine which
1159 production to use when parsing a non-terminal. Some C++ constructs
1160 require arbitrary look ahead to disambiguate. For example, it is
1161 impossible, in the general case, to tell whether a statement is an
1162 expression or declaration without scanning the entire statement.
1163 Therefore, the parser is capable of "parsing tentatively." When the
1164 parser is not sure what construct comes next, it enters this mode.
1165 Then, while we attempt to parse the construct, the parser queues up
1166 error messages, rather than issuing them immediately, and saves the
1167 tokens it consumes. If the construct is parsed successfully, the
1168 parser "commits", i.e., it issues any queued error messages and
1169 the tokens that were being preserved are permanently discarded.
1170 If, however, the construct is not parsed successfully, the parser
1171 rolls back its state completely so that it can resume parsing using
1172 a different alternative.
1177 The performance of the parser could probably be improved substantially.
1178 We could often eliminate the need to parse tentatively by looking ahead
1179 a little bit. In some places, this approach might not entirely eliminate
1180 the need to parse tentatively, but it might still speed up the average
1183 /* Flags that are passed to some parsing functions. These values can
1184 be bitwise-ored together. */
1186 typedef enum cp_parser_flags
1189 CP_PARSER_FLAGS_NONE = 0x0,
1190 /* The construct is optional. If it is not present, then no error
1191 should be issued. */
1192 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1193 /* When parsing a type-specifier, do not allow user-defined types. */
1194 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1197 /* The different kinds of declarators we want to parse. */
1199 typedef enum cp_parser_declarator_kind
1201 /* We want an abstract declarator. */
1202 CP_PARSER_DECLARATOR_ABSTRACT,
1203 /* We want a named declarator. */
1204 CP_PARSER_DECLARATOR_NAMED,
1205 /* We don't mind, but the name must be an unqualified-id. */
1206 CP_PARSER_DECLARATOR_EITHER
1207 } cp_parser_declarator_kind;
1209 /* The precedence values used to parse binary expressions. The minimum value
1210 of PREC must be 1, because zero is reserved to quickly discriminate
1211 binary operators from other tokens. */
1216 PREC_LOGICAL_OR_EXPRESSION,
1217 PREC_LOGICAL_AND_EXPRESSION,
1218 PREC_INCLUSIVE_OR_EXPRESSION,
1219 PREC_EXCLUSIVE_OR_EXPRESSION,
1220 PREC_AND_EXPRESSION,
1221 PREC_EQUALITY_EXPRESSION,
1222 PREC_RELATIONAL_EXPRESSION,
1223 PREC_SHIFT_EXPRESSION,
1224 PREC_ADDITIVE_EXPRESSION,
1225 PREC_MULTIPLICATIVE_EXPRESSION,
1227 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1230 /* A mapping from a token type to a corresponding tree node type, with a
1231 precedence value. */
1233 typedef struct cp_parser_binary_operations_map_node
1235 /* The token type. */
1236 enum cpp_ttype token_type;
1237 /* The corresponding tree code. */
1238 enum tree_code tree_type;
1239 /* The precedence of this operator. */
1240 enum cp_parser_prec prec;
1241 } cp_parser_binary_operations_map_node;
1243 /* The status of a tentative parse. */
1245 typedef enum cp_parser_status_kind
1247 /* No errors have occurred. */
1248 CP_PARSER_STATUS_KIND_NO_ERROR,
1249 /* An error has occurred. */
1250 CP_PARSER_STATUS_KIND_ERROR,
1251 /* We are committed to this tentative parse, whether or not an error
1253 CP_PARSER_STATUS_KIND_COMMITTED
1254 } cp_parser_status_kind;
1256 typedef struct cp_parser_expression_stack_entry
1258 /* Left hand side of the binary operation we are currently
1261 /* Original tree code for left hand side, if it was a binary
1262 expression itself (used for -Wparentheses). */
1263 enum tree_code lhs_type;
1264 /* Tree code for the binary operation we are parsing. */
1265 enum tree_code tree_type;
1266 /* Precedence of the binary operation we are parsing. */
1268 } cp_parser_expression_stack_entry;
1270 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1271 entries because precedence levels on the stack are monotonically
1273 typedef struct cp_parser_expression_stack_entry
1274 cp_parser_expression_stack[NUM_PREC_VALUES];
1276 /* Context that is saved and restored when parsing tentatively. */
1277 typedef struct cp_parser_context GTY (())
1279 /* If this is a tentative parsing context, the status of the
1281 enum cp_parser_status_kind status;
1282 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1283 that are looked up in this context must be looked up both in the
1284 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1285 the context of the containing expression. */
1288 /* The next parsing context in the stack. */
1289 struct cp_parser_context *next;
1290 } cp_parser_context;
1294 /* Constructors and destructors. */
1296 static cp_parser_context *cp_parser_context_new
1297 (cp_parser_context *);
1299 /* Class variables. */
1301 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1303 /* The operator-precedence table used by cp_parser_binary_expression.
1304 Transformed into an associative array (binops_by_token) by
1307 static const cp_parser_binary_operations_map_node binops[] = {
1308 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1309 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1311 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1312 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1313 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1315 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1316 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1318 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1319 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1321 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1322 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1323 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1324 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1326 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1327 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1329 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1331 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1333 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1335 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1337 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1340 /* The same as binops, but initialized by cp_parser_new so that
1341 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1343 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1345 /* Constructors and destructors. */
1347 /* Construct a new context. The context below this one on the stack
1348 is given by NEXT. */
1350 static cp_parser_context *
1351 cp_parser_context_new (cp_parser_context* next)
1353 cp_parser_context *context;
1355 /* Allocate the storage. */
1356 if (cp_parser_context_free_list != NULL)
1358 /* Pull the first entry from the free list. */
1359 context = cp_parser_context_free_list;
1360 cp_parser_context_free_list = context->next;
1361 memset (context, 0, sizeof (*context));
1364 context = GGC_CNEW (cp_parser_context);
1366 /* No errors have occurred yet in this context. */
1367 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1368 /* If this is not the bottomost context, copy information that we
1369 need from the previous context. */
1372 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1373 expression, then we are parsing one in this context, too. */
1374 context->object_type = next->object_type;
1375 /* Thread the stack. */
1376 context->next = next;
1382 /* The cp_parser structure represents the C++ parser. */
1384 typedef struct cp_parser GTY(())
1386 /* The lexer from which we are obtaining tokens. */
1389 /* The scope in which names should be looked up. If NULL_TREE, then
1390 we look up names in the scope that is currently open in the
1391 source program. If non-NULL, this is either a TYPE or
1392 NAMESPACE_DECL for the scope in which we should look. It can
1393 also be ERROR_MARK, when we've parsed a bogus scope.
1395 This value is not cleared automatically after a name is looked
1396 up, so we must be careful to clear it before starting a new look
1397 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1398 will look up `Z' in the scope of `X', rather than the current
1399 scope.) Unfortunately, it is difficult to tell when name lookup
1400 is complete, because we sometimes peek at a token, look it up,
1401 and then decide not to consume it. */
1404 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1405 last lookup took place. OBJECT_SCOPE is used if an expression
1406 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1407 respectively. QUALIFYING_SCOPE is used for an expression of the
1408 form "X::Y"; it refers to X. */
1410 tree qualifying_scope;
1412 /* A stack of parsing contexts. All but the bottom entry on the
1413 stack will be tentative contexts.
1415 We parse tentatively in order to determine which construct is in
1416 use in some situations. For example, in order to determine
1417 whether a statement is an expression-statement or a
1418 declaration-statement we parse it tentatively as a
1419 declaration-statement. If that fails, we then reparse the same
1420 token stream as an expression-statement. */
1421 cp_parser_context *context;
1423 /* True if we are parsing GNU C++. If this flag is not set, then
1424 GNU extensions are not recognized. */
1425 bool allow_gnu_extensions_p;
1427 /* TRUE if the `>' token should be interpreted as the greater-than
1428 operator. FALSE if it is the end of a template-id or
1429 template-parameter-list. In C++0x mode, this flag also applies to
1430 `>>' tokens, which are viewed as two consecutive `>' tokens when
1431 this flag is FALSE. */
1432 bool greater_than_is_operator_p;
1434 /* TRUE if default arguments are allowed within a parameter list
1435 that starts at this point. FALSE if only a gnu extension makes
1436 them permissible. */
1437 bool default_arg_ok_p;
1439 /* TRUE if we are parsing an integral constant-expression. See
1440 [expr.const] for a precise definition. */
1441 bool integral_constant_expression_p;
1443 /* TRUE if we are parsing an integral constant-expression -- but a
1444 non-constant expression should be permitted as well. This flag
1445 is used when parsing an array bound so that GNU variable-length
1446 arrays are tolerated. */
1447 bool allow_non_integral_constant_expression_p;
1449 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1450 been seen that makes the expression non-constant. */
1451 bool non_integral_constant_expression_p;
1453 /* TRUE if local variable names and `this' are forbidden in the
1455 bool local_variables_forbidden_p;
1457 /* TRUE if the declaration we are parsing is part of a
1458 linkage-specification of the form `extern string-literal
1460 bool in_unbraced_linkage_specification_p;
1462 /* TRUE if we are presently parsing a declarator, after the
1463 direct-declarator. */
1464 bool in_declarator_p;
1466 /* TRUE if we are presently parsing a template-argument-list. */
1467 bool in_template_argument_list_p;
1469 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1470 to IN_OMP_BLOCK if parsing OpenMP structured block and
1471 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1472 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1473 iteration-statement, OpenMP block or loop within that switch. */
1474 #define IN_SWITCH_STMT 1
1475 #define IN_ITERATION_STMT 2
1476 #define IN_OMP_BLOCK 4
1477 #define IN_OMP_FOR 8
1478 #define IN_IF_STMT 16
1479 unsigned char in_statement;
1481 /* TRUE if we are presently parsing the body of a switch statement.
1482 Note that this doesn't quite overlap with in_statement above.
1483 The difference relates to giving the right sets of error messages:
1484 "case not in switch" vs "break statement used with OpenMP...". */
1485 bool in_switch_statement_p;
1487 /* TRUE if we are parsing a type-id in an expression context. In
1488 such a situation, both "type (expr)" and "type (type)" are valid
1490 bool in_type_id_in_expr_p;
1492 /* TRUE if we are currently in a header file where declarations are
1493 implicitly extern "C". */
1494 bool implicit_extern_c;
1496 /* TRUE if strings in expressions should be translated to the execution
1498 bool translate_strings_p;
1500 /* TRUE if we are presently parsing the body of a function, but not
1502 bool in_function_body;
1504 /* If non-NULL, then we are parsing a construct where new type
1505 definitions are not permitted. The string stored here will be
1506 issued as an error message if a type is defined. */
1507 const char *type_definition_forbidden_message;
1509 /* A list of lists. The outer list is a stack, used for member
1510 functions of local classes. At each level there are two sub-list,
1511 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1512 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1513 TREE_VALUE's. The functions are chained in reverse declaration
1516 The TREE_PURPOSE sublist contains those functions with default
1517 arguments that need post processing, and the TREE_VALUE sublist
1518 contains those functions with definitions that need post
1521 These lists can only be processed once the outermost class being
1522 defined is complete. */
1523 tree unparsed_functions_queues;
1525 /* The number of classes whose definitions are currently in
1527 unsigned num_classes_being_defined;
1529 /* The number of template parameter lists that apply directly to the
1530 current declaration. */
1531 unsigned num_template_parameter_lists;
1536 /* Constructors and destructors. */
1538 static cp_parser *cp_parser_new
1541 /* Routines to parse various constructs.
1543 Those that return `tree' will return the error_mark_node (rather
1544 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1545 Sometimes, they will return an ordinary node if error-recovery was
1546 attempted, even though a parse error occurred. So, to check
1547 whether or not a parse error occurred, you should always use
1548 cp_parser_error_occurred. If the construct is optional (indicated
1549 either by an `_opt' in the name of the function that does the
1550 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1551 the construct is not present. */
1553 /* Lexical conventions [gram.lex] */
1555 static tree cp_parser_identifier
1557 static tree cp_parser_string_literal
1558 (cp_parser *, bool, bool);
1560 /* Basic concepts [gram.basic] */
1562 static bool cp_parser_translation_unit
1565 /* Expressions [gram.expr] */
1567 static tree cp_parser_primary_expression
1568 (cp_parser *, bool, bool, bool, cp_id_kind *);
1569 static tree cp_parser_id_expression
1570 (cp_parser *, bool, bool, bool *, bool, bool);
1571 static tree cp_parser_unqualified_id
1572 (cp_parser *, bool, bool, bool, bool);
1573 static tree cp_parser_nested_name_specifier_opt
1574 (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_nested_name_specifier
1576 (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_class_or_namespace_name
1578 (cp_parser *, bool, bool, bool, bool, bool);
1579 static tree cp_parser_postfix_expression
1580 (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_postfix_open_square_expression
1582 (cp_parser *, tree, bool);
1583 static tree cp_parser_postfix_dot_deref_expression
1584 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1585 static tree cp_parser_parenthesized_expression_list
1586 (cp_parser *, bool, bool, bool, bool *);
1587 static void cp_parser_pseudo_destructor_name
1588 (cp_parser *, tree *, tree *);
1589 static tree cp_parser_unary_expression
1590 (cp_parser *, bool, bool);
1591 static enum tree_code cp_parser_unary_operator
1593 static tree cp_parser_new_expression
1595 static tree cp_parser_new_placement
1597 static tree cp_parser_new_type_id
1598 (cp_parser *, tree *);
1599 static cp_declarator *cp_parser_new_declarator_opt
1601 static cp_declarator *cp_parser_direct_new_declarator
1603 static tree cp_parser_new_initializer
1605 static tree cp_parser_delete_expression
1607 static tree cp_parser_cast_expression
1608 (cp_parser *, bool, bool);
1609 static tree cp_parser_binary_expression
1610 (cp_parser *, bool);
1611 static tree cp_parser_question_colon_clause
1612 (cp_parser *, tree);
1613 static tree cp_parser_assignment_expression
1614 (cp_parser *, bool);
1615 static enum tree_code cp_parser_assignment_operator_opt
1617 static tree cp_parser_expression
1618 (cp_parser *, bool);
1619 static tree cp_parser_constant_expression
1620 (cp_parser *, bool, bool *);
1621 static tree cp_parser_builtin_offsetof
1624 /* Statements [gram.stmt.stmt] */
1626 static void cp_parser_statement
1627 (cp_parser *, tree, bool, bool *);
1628 static void cp_parser_label_for_labeled_statement
1630 static tree cp_parser_expression_statement
1631 (cp_parser *, tree);
1632 static tree cp_parser_compound_statement
1633 (cp_parser *, tree, bool);
1634 static void cp_parser_statement_seq_opt
1635 (cp_parser *, tree);
1636 static tree cp_parser_selection_statement
1637 (cp_parser *, bool *);
1638 static tree cp_parser_condition
1640 static tree cp_parser_iteration_statement
1642 static void cp_parser_for_init_statement
1644 static tree cp_parser_jump_statement
1646 static void cp_parser_declaration_statement
1649 static tree cp_parser_implicitly_scoped_statement
1650 (cp_parser *, bool *);
1651 static void cp_parser_already_scoped_statement
1654 /* Declarations [gram.dcl.dcl] */
1656 static void cp_parser_declaration_seq_opt
1658 static void cp_parser_declaration
1660 static void cp_parser_block_declaration
1661 (cp_parser *, bool);
1662 static void cp_parser_simple_declaration
1663 (cp_parser *, bool);
1664 static void cp_parser_decl_specifier_seq
1665 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1666 static tree cp_parser_storage_class_specifier_opt
1668 static tree cp_parser_function_specifier_opt
1669 (cp_parser *, cp_decl_specifier_seq *);
1670 static tree cp_parser_type_specifier
1671 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1673 static tree cp_parser_simple_type_specifier
1674 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1675 static tree cp_parser_type_name
1677 static tree cp_parser_nonclass_name
1678 (cp_parser* parser);
1679 static tree cp_parser_elaborated_type_specifier
1680 (cp_parser *, bool, bool);
1681 static tree cp_parser_enum_specifier
1683 static void cp_parser_enumerator_list
1684 (cp_parser *, tree);
1685 static void cp_parser_enumerator_definition
1686 (cp_parser *, tree);
1687 static tree cp_parser_namespace_name
1689 static void cp_parser_namespace_definition
1691 static void cp_parser_namespace_body
1693 static tree cp_parser_qualified_namespace_specifier
1695 static void cp_parser_namespace_alias_definition
1697 static bool cp_parser_using_declaration
1698 (cp_parser *, bool);
1699 static void cp_parser_using_directive
1701 static void cp_parser_asm_definition
1703 static void cp_parser_linkage_specification
1705 static void cp_parser_static_assert
1706 (cp_parser *, bool);
1707 static tree cp_parser_decltype
1710 /* Declarators [gram.dcl.decl] */
1712 static tree cp_parser_init_declarator
1713 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1714 static cp_declarator *cp_parser_declarator
1715 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1716 static cp_declarator *cp_parser_direct_declarator
1717 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1718 static enum tree_code cp_parser_ptr_operator
1719 (cp_parser *, tree *, cp_cv_quals *);
1720 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1722 static tree cp_parser_declarator_id
1723 (cp_parser *, bool);
1724 static tree cp_parser_type_id
1726 static void cp_parser_type_specifier_seq
1727 (cp_parser *, bool, cp_decl_specifier_seq *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1730 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1731 (cp_parser *, bool *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration
1733 (cp_parser *, bool, bool *);
1734 static tree cp_parser_default_argument
1735 (cp_parser *, bool);
1736 static void cp_parser_function_body
1738 static tree cp_parser_initializer
1739 (cp_parser *, bool *, bool *);
1740 static tree cp_parser_initializer_clause
1741 (cp_parser *, bool *);
1742 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1743 (cp_parser *, bool *);
1745 static bool cp_parser_ctor_initializer_opt_and_function_body
1748 /* Classes [gram.class] */
1750 static tree cp_parser_class_name
1751 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1752 static tree cp_parser_class_specifier
1754 static tree cp_parser_class_head
1755 (cp_parser *, bool *, tree *, tree *);
1756 static enum tag_types cp_parser_class_key
1758 static void cp_parser_member_specification_opt
1760 static void cp_parser_member_declaration
1762 static tree cp_parser_pure_specifier
1764 static tree cp_parser_constant_initializer
1767 /* Derived classes [gram.class.derived] */
1769 static tree cp_parser_base_clause
1771 static tree cp_parser_base_specifier
1774 /* Special member functions [gram.special] */
1776 static tree cp_parser_conversion_function_id
1778 static tree cp_parser_conversion_type_id
1780 static cp_declarator *cp_parser_conversion_declarator_opt
1782 static bool cp_parser_ctor_initializer_opt
1784 static void cp_parser_mem_initializer_list
1786 static tree cp_parser_mem_initializer
1788 static tree cp_parser_mem_initializer_id
1791 /* Overloading [gram.over] */
1793 static tree cp_parser_operator_function_id
1795 static tree cp_parser_operator
1798 /* Templates [gram.temp] */
1800 static void cp_parser_template_declaration
1801 (cp_parser *, bool);
1802 static tree cp_parser_template_parameter_list
1804 static tree cp_parser_template_parameter
1805 (cp_parser *, bool *, bool *);
1806 static tree cp_parser_type_parameter
1807 (cp_parser *, bool *);
1808 static tree cp_parser_template_id
1809 (cp_parser *, bool, bool, bool);
1810 static tree cp_parser_template_name
1811 (cp_parser *, bool, bool, bool, bool *);
1812 static tree cp_parser_template_argument_list
1814 static tree cp_parser_template_argument
1816 static void cp_parser_explicit_instantiation
1818 static void cp_parser_explicit_specialization
1821 /* Exception handling [gram.exception] */
1823 static tree cp_parser_try_block
1825 static bool cp_parser_function_try_block
1827 static void cp_parser_handler_seq
1829 static void cp_parser_handler
1831 static tree cp_parser_exception_declaration
1833 static tree cp_parser_throw_expression
1835 static tree cp_parser_exception_specification_opt
1837 static tree cp_parser_type_id_list
1840 /* GNU Extensions */
1842 static tree cp_parser_asm_specification_opt
1844 static tree cp_parser_asm_operand_list
1846 static tree cp_parser_asm_clobber_list
1848 static tree cp_parser_attributes_opt
1850 static tree cp_parser_attribute_list
1852 static bool cp_parser_extension_opt
1853 (cp_parser *, int *);
1854 static void cp_parser_label_declaration
1857 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1858 static bool cp_parser_pragma
1859 (cp_parser *, enum pragma_context);
1861 /* Objective-C++ Productions */
1863 static tree cp_parser_objc_message_receiver
1865 static tree cp_parser_objc_message_args
1867 static tree cp_parser_objc_message_expression
1869 static tree cp_parser_objc_encode_expression
1871 static tree cp_parser_objc_defs_expression
1873 static tree cp_parser_objc_protocol_expression
1875 static tree cp_parser_objc_selector_expression
1877 static tree cp_parser_objc_expression
1879 static bool cp_parser_objc_selector_p
1881 static tree cp_parser_objc_selector
1883 static tree cp_parser_objc_protocol_refs_opt
1885 static void cp_parser_objc_declaration
1887 static tree cp_parser_objc_statement
1890 /* Utility Routines */
1892 static tree cp_parser_lookup_name
1893 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1894 static tree cp_parser_lookup_name_simple
1895 (cp_parser *, tree);
1896 static tree cp_parser_maybe_treat_template_as_class
1898 static bool cp_parser_check_declarator_template_parameters
1899 (cp_parser *, cp_declarator *);
1900 static bool cp_parser_check_template_parameters
1901 (cp_parser *, unsigned);
1902 static tree cp_parser_simple_cast_expression
1904 static tree cp_parser_global_scope_opt
1905 (cp_parser *, bool);
1906 static bool cp_parser_constructor_declarator_p
1907 (cp_parser *, bool);
1908 static tree cp_parser_function_definition_from_specifiers_and_declarator
1909 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1910 static tree cp_parser_function_definition_after_declarator
1911 (cp_parser *, bool);
1912 static void cp_parser_template_declaration_after_export
1913 (cp_parser *, bool);
1914 static void cp_parser_perform_template_parameter_access_checks
1915 (VEC (deferred_access_check,gc)*);
1916 static tree cp_parser_single_declaration
1917 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1918 static tree cp_parser_functional_cast
1919 (cp_parser *, tree);
1920 static tree cp_parser_save_member_function_body
1921 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1922 static tree cp_parser_enclosed_template_argument_list
1924 static void cp_parser_save_default_args
1925 (cp_parser *, tree);
1926 static void cp_parser_late_parsing_for_member
1927 (cp_parser *, tree);
1928 static void cp_parser_late_parsing_default_args
1929 (cp_parser *, tree);
1930 static tree cp_parser_sizeof_operand
1931 (cp_parser *, enum rid);
1932 static tree cp_parser_trait_expr
1933 (cp_parser *, enum rid);
1934 static bool cp_parser_declares_only_class_p
1936 static void cp_parser_set_storage_class
1937 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1938 static void cp_parser_set_decl_spec_type
1939 (cp_decl_specifier_seq *, tree, bool);
1940 static bool cp_parser_friend_p
1941 (const cp_decl_specifier_seq *);
1942 static cp_token *cp_parser_require
1943 (cp_parser *, enum cpp_ttype, const char *);
1944 static cp_token *cp_parser_require_keyword
1945 (cp_parser *, enum rid, const char *);
1946 static bool cp_parser_token_starts_function_definition_p
1948 static bool cp_parser_next_token_starts_class_definition_p
1950 static bool cp_parser_next_token_ends_template_argument_p
1952 static bool cp_parser_nth_token_starts_template_argument_list_p
1953 (cp_parser *, size_t);
1954 static enum tag_types cp_parser_token_is_class_key
1956 static void cp_parser_check_class_key
1957 (enum tag_types, tree type);
1958 static void cp_parser_check_access_in_redeclaration
1960 static bool cp_parser_optional_template_keyword
1962 static void cp_parser_pre_parsed_nested_name_specifier
1964 static void cp_parser_cache_group
1965 (cp_parser *, enum cpp_ttype, unsigned);
1966 static void cp_parser_parse_tentatively
1968 static void cp_parser_commit_to_tentative_parse
1970 static void cp_parser_abort_tentative_parse
1972 static bool cp_parser_parse_definitely
1974 static inline bool cp_parser_parsing_tentatively
1976 static bool cp_parser_uncommitted_to_tentative_parse_p
1978 static void cp_parser_error
1979 (cp_parser *, const char *);
1980 static void cp_parser_name_lookup_error
1981 (cp_parser *, tree, tree, const char *);
1982 static bool cp_parser_simulate_error
1984 static bool cp_parser_check_type_definition
1986 static void cp_parser_check_for_definition_in_return_type
1987 (cp_declarator *, tree);
1988 static void cp_parser_check_for_invalid_template_id
1989 (cp_parser *, tree);
1990 static bool cp_parser_non_integral_constant_expression
1991 (cp_parser *, const char *);
1992 static void cp_parser_diagnose_invalid_type_name
1993 (cp_parser *, tree, tree);
1994 static bool cp_parser_parse_and_diagnose_invalid_type_name
1996 static int cp_parser_skip_to_closing_parenthesis
1997 (cp_parser *, bool, bool, bool);
1998 static void cp_parser_skip_to_end_of_statement
2000 static void cp_parser_consume_semicolon_at_end_of_statement
2002 static void cp_parser_skip_to_end_of_block_or_statement
2004 static bool cp_parser_skip_to_closing_brace
2006 static void cp_parser_skip_to_end_of_template_parameter_list
2008 static void cp_parser_skip_to_pragma_eol
2009 (cp_parser*, cp_token *);
2010 static bool cp_parser_error_occurred
2012 static bool cp_parser_allow_gnu_extensions_p
2014 static bool cp_parser_is_string_literal
2016 static bool cp_parser_is_keyword
2017 (cp_token *, enum rid);
2018 static tree cp_parser_make_typename_type
2019 (cp_parser *, tree, tree);
2020 static cp_declarator * cp_parser_make_indirect_declarator
2021 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2023 /* Returns nonzero if we are parsing tentatively. */
2026 cp_parser_parsing_tentatively (cp_parser* parser)
2028 return parser->context->next != NULL;
2031 /* Returns nonzero if TOKEN is a string literal. */
2034 cp_parser_is_string_literal (cp_token* token)
2036 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2039 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2042 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2044 return token->keyword == keyword;
2047 /* If not parsing tentatively, issue a diagnostic of the form
2048 FILE:LINE: MESSAGE before TOKEN
2049 where TOKEN is the next token in the input stream. MESSAGE
2050 (specified by the caller) is usually of the form "expected
2054 cp_parser_error (cp_parser* parser, const char* message)
2056 if (!cp_parser_simulate_error (parser))
2058 cp_token *token = cp_lexer_peek_token (parser->lexer);
2059 /* This diagnostic makes more sense if it is tagged to the line
2060 of the token we just peeked at. */
2061 cp_lexer_set_source_position_from_token (token);
2063 if (token->type == CPP_PRAGMA)
2065 error ("%<#pragma%> is not allowed here");
2066 cp_parser_skip_to_pragma_eol (parser, token);
2070 c_parse_error (message,
2071 /* Because c_parser_error does not understand
2072 CPP_KEYWORD, keywords are treated like
2074 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079 /* Issue an error about name-lookup failing. NAME is the
2080 IDENTIFIER_NODE DECL is the result of
2081 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2082 the thing that we hoped to find. */
2085 cp_parser_name_lookup_error (cp_parser* parser,
2088 const char* desired)
2090 /* If name lookup completely failed, tell the user that NAME was not
2092 if (decl == error_mark_node)
2094 if (parser->scope && parser->scope != global_namespace)
2095 error ("%<%E::%E%> has not been declared",
2096 parser->scope, name);
2097 else if (parser->scope == global_namespace)
2098 error ("%<::%E%> has not been declared", name);
2099 else if (parser->object_scope
2100 && !CLASS_TYPE_P (parser->object_scope))
2101 error ("request for member %qE in non-class type %qT",
2102 name, parser->object_scope);
2103 else if (parser->object_scope)
2104 error ("%<%T::%E%> has not been declared",
2105 parser->object_scope, name);
2107 error ("%qE has not been declared", name);
2109 else if (parser->scope && parser->scope != global_namespace)
2110 error ("%<%E::%E%> %s", parser->scope, name, desired);
2111 else if (parser->scope == global_namespace)
2112 error ("%<::%E%> %s", name, desired);
2114 error ("%qE %s", name, desired);
2117 /* If we are parsing tentatively, remember that an error has occurred
2118 during this tentative parse. Returns true if the error was
2119 simulated; false if a message should be issued by the caller. */
2122 cp_parser_simulate_error (cp_parser* parser)
2124 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2126 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2132 /* Check for repeated decl-specifiers. */
2135 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2139 for (ds = ds_first; ds != ds_last; ++ds)
2141 unsigned count = decl_specs->specs[(int)ds];
2144 /* The "long" specifier is a special case because of "long long". */
2148 error ("%<long long long%> is too long for GCC");
2149 else if (pedantic && !in_system_header && warn_long_long
2150 && cxx_dialect == cxx98)
2151 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2155 static const char *const decl_spec_names[] = {
2171 error ("duplicate %qs", decl_spec_names[(int)ds]);
2176 /* This function is called when a type is defined. If type
2177 definitions are forbidden at this point, an error message is
2181 cp_parser_check_type_definition (cp_parser* parser)
2183 /* If types are forbidden here, issue a message. */
2184 if (parser->type_definition_forbidden_message)
2186 /* Use `%s' to print the string in case there are any escape
2187 characters in the message. */
2188 error ("%s", parser->type_definition_forbidden_message);
2194 /* This function is called when the DECLARATOR is processed. The TYPE
2195 was a type defined in the decl-specifiers. If it is invalid to
2196 define a type in the decl-specifiers for DECLARATOR, an error is
2200 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2203 /* [dcl.fct] forbids type definitions in return types.
2204 Unfortunately, it's not easy to know whether or not we are
2205 processing a return type until after the fact. */
2207 && (declarator->kind == cdk_pointer
2208 || declarator->kind == cdk_reference
2209 || declarator->kind == cdk_ptrmem))
2210 declarator = declarator->declarator;
2212 && declarator->kind == cdk_function)
2214 error ("new types may not be defined in a return type");
2215 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2220 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2221 "<" in any valid C++ program. If the next token is indeed "<",
2222 issue a message warning the user about what appears to be an
2223 invalid attempt to form a template-id. */
2226 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2229 cp_token_position start = 0;
2231 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2234 error ("%qT is not a template", type);
2235 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2236 error ("%qE is not a template", type);
2238 error ("invalid template-id");
2239 /* Remember the location of the invalid "<". */
2240 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2241 start = cp_lexer_token_position (parser->lexer, true);
2242 /* Consume the "<". */
2243 cp_lexer_consume_token (parser->lexer);
2244 /* Parse the template arguments. */
2245 cp_parser_enclosed_template_argument_list (parser);
2246 /* Permanently remove the invalid template arguments so that
2247 this error message is not issued again. */
2249 cp_lexer_purge_tokens_after (parser->lexer, start);
2253 /* If parsing an integral constant-expression, issue an error message
2254 about the fact that THING appeared and return true. Otherwise,
2255 return false. In either case, set
2256 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2259 cp_parser_non_integral_constant_expression (cp_parser *parser,
2262 parser->non_integral_constant_expression_p = true;
2263 if (parser->integral_constant_expression_p)
2265 if (!parser->allow_non_integral_constant_expression_p)
2267 error ("%s cannot appear in a constant-expression", thing);
2274 /* Emit a diagnostic for an invalid type name. SCOPE is the
2275 qualifying scope (or NULL, if none) for ID. This function commits
2276 to the current active tentative parse, if any. (Otherwise, the
2277 problematic construct might be encountered again later, resulting
2278 in duplicate error messages.) */
2281 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2283 tree decl, old_scope;
2284 /* Try to lookup the identifier. */
2285 old_scope = parser->scope;
2286 parser->scope = scope;
2287 decl = cp_parser_lookup_name_simple (parser, id);
2288 parser->scope = old_scope;
2289 /* If the lookup found a template-name, it means that the user forgot
2290 to specify an argument list. Emit a useful error message. */
2291 if (TREE_CODE (decl) == TEMPLATE_DECL)
2292 error ("invalid use of template-name %qE without an argument list", decl);
2293 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2294 error ("invalid use of destructor %qD as a type", id);
2295 else if (TREE_CODE (decl) == TYPE_DECL)
2296 /* Something like 'unsigned A a;' */
2297 error ("invalid combination of multiple type-specifiers");
2298 else if (!parser->scope)
2300 /* Issue an error message. */
2301 error ("%qE does not name a type", id);
2302 /* If we're in a template class, it's possible that the user was
2303 referring to a type from a base class. For example:
2305 template <typename T> struct A { typedef T X; };
2306 template <typename T> struct B : public A<T> { X x; };
2308 The user should have said "typename A<T>::X". */
2309 if (processing_template_decl && current_class_type
2310 && TYPE_BINFO (current_class_type))
2314 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2318 tree base_type = BINFO_TYPE (b);
2319 if (CLASS_TYPE_P (base_type)
2320 && dependent_type_p (base_type))
2323 /* Go from a particular instantiation of the
2324 template (which will have an empty TYPE_FIELDs),
2325 to the main version. */
2326 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2327 for (field = TYPE_FIELDS (base_type);
2329 field = TREE_CHAIN (field))
2330 if (TREE_CODE (field) == TYPE_DECL
2331 && DECL_NAME (field) == id)
2333 inform ("(perhaps %<typename %T::%E%> was intended)",
2334 BINFO_TYPE (b), id);
2343 /* Here we diagnose qualified-ids where the scope is actually correct,
2344 but the identifier does not resolve to a valid type name. */
2345 else if (parser->scope != error_mark_node)
2347 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2348 error ("%qE in namespace %qE does not name a type",
2350 else if (TYPE_P (parser->scope))
2351 error ("%qE in class %qT does not name a type", id, parser->scope);
2355 cp_parser_commit_to_tentative_parse (parser);
2358 /* Check for a common situation where a type-name should be present,
2359 but is not, and issue a sensible error message. Returns true if an
2360 invalid type-name was detected.
2362 The situation handled by this function are variable declarations of the
2363 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2364 Usually, `ID' should name a type, but if we got here it means that it
2365 does not. We try to emit the best possible error message depending on
2366 how exactly the id-expression looks like. */
2369 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2373 cp_parser_parse_tentatively (parser);
2374 id = cp_parser_id_expression (parser,
2375 /*template_keyword_p=*/false,
2376 /*check_dependency_p=*/true,
2377 /*template_p=*/NULL,
2378 /*declarator_p=*/true,
2379 /*optional_p=*/false);
2380 /* After the id-expression, there should be a plain identifier,
2381 otherwise this is not a simple variable declaration. Also, if
2382 the scope is dependent, we cannot do much. */
2383 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2384 || (parser->scope && TYPE_P (parser->scope)
2385 && dependent_type_p (parser->scope))
2386 || TREE_CODE (id) == TYPE_DECL)
2388 cp_parser_abort_tentative_parse (parser);
2391 if (!cp_parser_parse_definitely (parser))
2394 /* Emit a diagnostic for the invalid type. */
2395 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2396 /* Skip to the end of the declaration; there's no point in
2397 trying to process it. */
2398 cp_parser_skip_to_end_of_block_or_statement (parser);
2402 /* Consume tokens up to, and including, the next non-nested closing `)'.
2403 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2404 are doing error recovery. Returns -1 if OR_COMMA is true and we
2405 found an unnested comma. */
2408 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2413 unsigned paren_depth = 0;
2414 unsigned brace_depth = 0;
2416 if (recovering && !or_comma
2417 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2422 cp_token * token = cp_lexer_peek_token (parser->lexer);
2424 switch (token->type)
2427 case CPP_PRAGMA_EOL:
2428 /* If we've run out of tokens, then there is no closing `)'. */
2432 /* This matches the processing in skip_to_end_of_statement. */
2437 case CPP_OPEN_BRACE:
2440 case CPP_CLOSE_BRACE:
2446 if (recovering && or_comma && !brace_depth && !paren_depth)
2450 case CPP_OPEN_PAREN:
2455 case CPP_CLOSE_PAREN:
2456 if (!brace_depth && !paren_depth--)
2459 cp_lexer_consume_token (parser->lexer);
2468 /* Consume the token. */
2469 cp_lexer_consume_token (parser->lexer);
2473 /* Consume tokens until we reach the end of the current statement.
2474 Normally, that will be just before consuming a `;'. However, if a
2475 non-nested `}' comes first, then we stop before consuming that. */
2478 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2480 unsigned nesting_depth = 0;
2484 cp_token *token = cp_lexer_peek_token (parser->lexer);
2486 switch (token->type)
2489 case CPP_PRAGMA_EOL:
2490 /* If we've run out of tokens, stop. */
2494 /* If the next token is a `;', we have reached the end of the
2500 case CPP_CLOSE_BRACE:
2501 /* If this is a non-nested '}', stop before consuming it.
2502 That way, when confronted with something like:
2506 we stop before consuming the closing '}', even though we
2507 have not yet reached a `;'. */
2508 if (nesting_depth == 0)
2511 /* If it is the closing '}' for a block that we have
2512 scanned, stop -- but only after consuming the token.
2518 we will stop after the body of the erroneously declared
2519 function, but before consuming the following `typedef'
2521 if (--nesting_depth == 0)
2523 cp_lexer_consume_token (parser->lexer);
2527 case CPP_OPEN_BRACE:
2535 /* Consume the token. */
2536 cp_lexer_consume_token (parser->lexer);
2540 /* This function is called at the end of a statement or declaration.
2541 If the next token is a semicolon, it is consumed; otherwise, error
2542 recovery is attempted. */
2545 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2547 /* Look for the trailing `;'. */
2548 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2550 /* If there is additional (erroneous) input, skip to the end of
2552 cp_parser_skip_to_end_of_statement (parser);
2553 /* If the next token is now a `;', consume it. */
2554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2555 cp_lexer_consume_token (parser->lexer);
2559 /* Skip tokens until we have consumed an entire block, or until we
2560 have consumed a non-nested `;'. */
2563 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2565 int nesting_depth = 0;
2567 while (nesting_depth >= 0)
2569 cp_token *token = cp_lexer_peek_token (parser->lexer);
2571 switch (token->type)
2574 case CPP_PRAGMA_EOL:
2575 /* If we've run out of tokens, stop. */
2579 /* Stop if this is an unnested ';'. */
2584 case CPP_CLOSE_BRACE:
2585 /* Stop if this is an unnested '}', or closes the outermost
2592 case CPP_OPEN_BRACE:
2601 /* Consume the token. */
2602 cp_lexer_consume_token (parser->lexer);
2606 /* Skip tokens until a non-nested closing curly brace is the next
2607 token, or there are no more tokens. Return true in the first case,
2611 cp_parser_skip_to_closing_brace (cp_parser *parser)
2613 unsigned nesting_depth = 0;
2617 cp_token *token = cp_lexer_peek_token (parser->lexer);
2619 switch (token->type)
2622 case CPP_PRAGMA_EOL:
2623 /* If we've run out of tokens, stop. */
2626 case CPP_CLOSE_BRACE:
2627 /* If the next token is a non-nested `}', then we have reached
2628 the end of the current block. */
2629 if (nesting_depth-- == 0)
2633 case CPP_OPEN_BRACE:
2634 /* If it the next token is a `{', then we are entering a new
2635 block. Consume the entire block. */
2643 /* Consume the token. */
2644 cp_lexer_consume_token (parser->lexer);
2648 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2649 parameter is the PRAGMA token, allowing us to purge the entire pragma
2653 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2657 parser->lexer->in_pragma = false;
2660 token = cp_lexer_consume_token (parser->lexer);
2661 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2663 /* Ensure that the pragma is not parsed again. */
2664 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2667 /* Require pragma end of line, resyncing with it as necessary. The
2668 arguments are as for cp_parser_skip_to_pragma_eol. */
2671 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2673 parser->lexer->in_pragma = false;
2674 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2675 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2678 /* This is a simple wrapper around make_typename_type. When the id is
2679 an unresolved identifier node, we can provide a superior diagnostic
2680 using cp_parser_diagnose_invalid_type_name. */
2683 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2686 if (TREE_CODE (id) == IDENTIFIER_NODE)
2688 result = make_typename_type (scope, id, typename_type,
2689 /*complain=*/tf_none);
2690 if (result == error_mark_node)
2691 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2694 return make_typename_type (scope, id, typename_type, tf_error);
2697 /* This is a wrapper around the
2698 make_{pointer,ptrmem,reference}_declarator functions that decides
2699 which one to call based on the CODE and CLASS_TYPE arguments. The
2700 CODE argument should be one of the values returned by
2701 cp_parser_ptr_operator. */
2702 static cp_declarator *
2703 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2704 cp_cv_quals cv_qualifiers,
2705 cp_declarator *target)
2707 if (code == ERROR_MARK)
2708 return cp_error_declarator;
2710 if (code == INDIRECT_REF)
2711 if (class_type == NULL_TREE)
2712 return make_pointer_declarator (cv_qualifiers, target);
2714 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2715 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2716 return make_reference_declarator (cv_qualifiers, target, false);
2717 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2718 return make_reference_declarator (cv_qualifiers, target, true);
2722 /* Create a new C++ parser. */
2725 cp_parser_new (void)
2731 /* cp_lexer_new_main is called before calling ggc_alloc because
2732 cp_lexer_new_main might load a PCH file. */
2733 lexer = cp_lexer_new_main ();
2735 /* Initialize the binops_by_token so that we can get the tree
2736 directly from the token. */
2737 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2738 binops_by_token[binops[i].token_type] = binops[i];
2740 parser = GGC_CNEW (cp_parser);
2741 parser->lexer = lexer;
2742 parser->context = cp_parser_context_new (NULL);
2744 /* For now, we always accept GNU extensions. */
2745 parser->allow_gnu_extensions_p = 1;
2747 /* The `>' token is a greater-than operator, not the end of a
2749 parser->greater_than_is_operator_p = true;
2751 parser->default_arg_ok_p = true;
2753 /* We are not parsing a constant-expression. */
2754 parser->integral_constant_expression_p = false;
2755 parser->allow_non_integral_constant_expression_p = false;
2756 parser->non_integral_constant_expression_p = false;
2758 /* Local variable names are not forbidden. */
2759 parser->local_variables_forbidden_p = false;
2761 /* We are not processing an `extern "C"' declaration. */
2762 parser->in_unbraced_linkage_specification_p = false;
2764 /* We are not processing a declarator. */
2765 parser->in_declarator_p = false;
2767 /* We are not processing a template-argument-list. */
2768 parser->in_template_argument_list_p = false;
2770 /* We are not in an iteration statement. */
2771 parser->in_statement = 0;
2773 /* We are not in a switch statement. */
2774 parser->in_switch_statement_p = false;
2776 /* We are not parsing a type-id inside an expression. */
2777 parser->in_type_id_in_expr_p = false;
2779 /* Declarations aren't implicitly extern "C". */
2780 parser->implicit_extern_c = false;
2782 /* String literals should be translated to the execution character set. */
2783 parser->translate_strings_p = true;
2785 /* We are not parsing a function body. */
2786 parser->in_function_body = false;
2788 /* The unparsed function queue is empty. */
2789 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2791 /* There are no classes being defined. */
2792 parser->num_classes_being_defined = 0;
2794 /* No template parameters apply. */
2795 parser->num_template_parameter_lists = 0;
2800 /* Create a cp_lexer structure which will emit the tokens in CACHE
2801 and push it onto the parser's lexer stack. This is used for delayed
2802 parsing of in-class method bodies and default arguments, and should
2803 not be confused with tentative parsing. */
2805 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2807 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2808 lexer->next = parser->lexer;
2809 parser->lexer = lexer;
2811 /* Move the current source position to that of the first token in the
2813 cp_lexer_set_source_position_from_token (lexer->next_token);
2816 /* Pop the top lexer off the parser stack. This is never used for the
2817 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2819 cp_parser_pop_lexer (cp_parser *parser)
2821 cp_lexer *lexer = parser->lexer;
2822 parser->lexer = lexer->next;
2823 cp_lexer_destroy (lexer);
2825 /* Put the current source position back where it was before this
2826 lexer was pushed. */
2827 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2830 /* Lexical conventions [gram.lex] */
2832 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2836 cp_parser_identifier (cp_parser* parser)
2840 /* Look for the identifier. */
2841 token = cp_parser_require (parser, CPP_NAME, "identifier");
2842 /* Return the value. */
2843 return token ? token->u.value : error_mark_node;
2846 /* Parse a sequence of adjacent string constants. Returns a
2847 TREE_STRING representing the combined, nul-terminated string
2848 constant. If TRANSLATE is true, translate the string to the
2849 execution character set. If WIDE_OK is true, a wide string is
2852 C++98 [lex.string] says that if a narrow string literal token is
2853 adjacent to a wide string literal token, the behavior is undefined.
2854 However, C99 6.4.5p4 says that this results in a wide string literal.
2855 We follow C99 here, for consistency with the C front end.
2857 This code is largely lifted from lex_string() in c-lex.c.
2859 FUTURE: ObjC++ will need to handle @-strings here. */
2861 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2866 struct obstack str_ob;
2867 cpp_string str, istr, *strs;
2870 tok = cp_lexer_peek_token (parser->lexer);
2871 if (!cp_parser_is_string_literal (tok))
2873 cp_parser_error (parser, "expected string-literal");
2874 return error_mark_node;
2877 /* Try to avoid the overhead of creating and destroying an obstack
2878 for the common case of just one string. */
2879 if (!cp_parser_is_string_literal
2880 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2882 cp_lexer_consume_token (parser->lexer);
2884 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2885 str.len = TREE_STRING_LENGTH (tok->u.value);
2887 if (tok->type == CPP_WSTRING)
2894 gcc_obstack_init (&str_ob);
2899 cp_lexer_consume_token (parser->lexer);
2901 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2902 str.len = TREE_STRING_LENGTH (tok->u.value);
2903 if (tok->type == CPP_WSTRING)
2906 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2908 tok = cp_lexer_peek_token (parser->lexer);
2910 while (cp_parser_is_string_literal (tok));
2912 strs = (cpp_string *) obstack_finish (&str_ob);
2915 if (wide && !wide_ok)
2917 cp_parser_error (parser, "a wide string is invalid in this context");
2921 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2922 (parse_in, strs, count, &istr, wide))
2924 value = build_string (istr.len, (const char *)istr.text);
2925 free (CONST_CAST (unsigned char *, istr.text));
2927 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2928 value = fix_string_type (value);
2931 /* cpp_interpret_string has issued an error. */
2932 value = error_mark_node;
2935 obstack_free (&str_ob, 0);
2941 /* Basic concepts [gram.basic] */
2943 /* Parse a translation-unit.
2946 declaration-seq [opt]
2948 Returns TRUE if all went well. */
2951 cp_parser_translation_unit (cp_parser* parser)
2953 /* The address of the first non-permanent object on the declarator
2955 static void *declarator_obstack_base;
2959 /* Create the declarator obstack, if necessary. */
2960 if (!cp_error_declarator)
2962 gcc_obstack_init (&declarator_obstack);
2963 /* Create the error declarator. */
2964 cp_error_declarator = make_declarator (cdk_error);
2965 /* Create the empty parameter list. */
2966 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2967 /* Remember where the base of the declarator obstack lies. */
2968 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2971 cp_parser_declaration_seq_opt (parser);
2973 /* If there are no tokens left then all went well. */
2974 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2976 /* Get rid of the token array; we don't need it any more. */
2977 cp_lexer_destroy (parser->lexer);
2978 parser->lexer = NULL;
2980 /* This file might have been a context that's implicitly extern
2981 "C". If so, pop the lang context. (Only relevant for PCH.) */
2982 if (parser->implicit_extern_c)
2984 pop_lang_context ();
2985 parser->implicit_extern_c = false;
2989 finish_translation_unit ();
2995 cp_parser_error (parser, "expected declaration");
2999 /* Make sure the declarator obstack was fully cleaned up. */
3000 gcc_assert (obstack_next_free (&declarator_obstack)
3001 == declarator_obstack_base);
3003 /* All went well. */
3007 /* Expressions [gram.expr] */
3009 /* Parse a primary-expression.
3020 ( compound-statement )
3021 __builtin_va_arg ( assignment-expression , type-id )
3022 __builtin_offsetof ( type-id , offsetof-expression )
3025 __has_nothrow_assign ( type-id )
3026 __has_nothrow_constructor ( type-id )
3027 __has_nothrow_copy ( type-id )
3028 __has_trivial_assign ( type-id )
3029 __has_trivial_constructor ( type-id )
3030 __has_trivial_copy ( type-id )
3031 __has_trivial_destructor ( type-id )
3032 __has_virtual_destructor ( type-id )
3033 __is_abstract ( type-id )
3034 __is_base_of ( type-id , type-id )
3035 __is_class ( type-id )
3036 __is_convertible_to ( type-id , type-id )
3037 __is_empty ( type-id )
3038 __is_enum ( type-id )
3039 __is_pod ( type-id )
3040 __is_polymorphic ( type-id )
3041 __is_union ( type-id )
3043 Objective-C++ Extension:
3051 ADDRESS_P is true iff this expression was immediately preceded by
3052 "&" and therefore might denote a pointer-to-member. CAST_P is true
3053 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3054 true iff this expression is a template argument.
3056 Returns a representation of the expression. Upon return, *IDK
3057 indicates what kind of id-expression (if any) was present. */
3060 cp_parser_primary_expression (cp_parser *parser,
3063 bool template_arg_p,
3068 /* Assume the primary expression is not an id-expression. */
3069 *idk = CP_ID_KIND_NONE;
3071 /* Peek at the next token. */
3072 token = cp_lexer_peek_token (parser->lexer);
3073 switch (token->type)
3084 token = cp_lexer_consume_token (parser->lexer);
3085 /* Floating-point literals are only allowed in an integral
3086 constant expression if they are cast to an integral or
3087 enumeration type. */
3088 if (TREE_CODE (token->u.value) == REAL_CST
3089 && parser->integral_constant_expression_p
3092 /* CAST_P will be set even in invalid code like "int(2.7 +
3093 ...)". Therefore, we have to check that the next token
3094 is sure to end the cast. */
3097 cp_token *next_token;
3099 next_token = cp_lexer_peek_token (parser->lexer);
3100 if (/* The comma at the end of an
3101 enumerator-definition. */
3102 next_token->type != CPP_COMMA
3103 /* The curly brace at the end of an enum-specifier. */
3104 && next_token->type != CPP_CLOSE_BRACE
3105 /* The end of a statement. */
3106 && next_token->type != CPP_SEMICOLON
3107 /* The end of the cast-expression. */
3108 && next_token->type != CPP_CLOSE_PAREN
3109 /* The end of an array bound. */
3110 && next_token->type != CPP_CLOSE_SQUARE
3111 /* The closing ">" in a template-argument-list. */
3112 && (next_token->type != CPP_GREATER
3113 || parser->greater_than_is_operator_p)
3114 /* C++0x only: A ">>" treated like two ">" tokens,
3115 in a template-argument-list. */
3116 && (next_token->type != CPP_RSHIFT
3117 || (cxx_dialect == cxx98)
3118 || parser->greater_than_is_operator_p))
3122 /* If we are within a cast, then the constraint that the
3123 cast is to an integral or enumeration type will be
3124 checked at that point. If we are not within a cast, then
3125 this code is invalid. */
3127 cp_parser_non_integral_constant_expression
3128 (parser, "floating-point literal");
3130 return token->u.value;
3134 /* ??? Should wide strings be allowed when parser->translate_strings_p
3135 is false (i.e. in attributes)? If not, we can kill the third
3136 argument to cp_parser_string_literal. */
3137 return cp_parser_string_literal (parser,
3138 parser->translate_strings_p,
3141 case CPP_OPEN_PAREN:
3144 bool saved_greater_than_is_operator_p;
3146 /* Consume the `('. */
3147 cp_lexer_consume_token (parser->lexer);
3148 /* Within a parenthesized expression, a `>' token is always
3149 the greater-than operator. */
3150 saved_greater_than_is_operator_p
3151 = parser->greater_than_is_operator_p;
3152 parser->greater_than_is_operator_p = true;
3153 /* If we see `( { ' then we are looking at the beginning of
3154 a GNU statement-expression. */
3155 if (cp_parser_allow_gnu_extensions_p (parser)
3156 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3158 /* Statement-expressions are not allowed by the standard. */
3160 pedwarn ("ISO C++ forbids braced-groups within expressions");
3162 /* And they're not allowed outside of a function-body; you
3163 cannot, for example, write:
3165 int i = ({ int j = 3; j + 1; });
3167 at class or namespace scope. */
3168 if (!parser->in_function_body
3169 || parser->in_template_argument_list_p)
3171 error ("statement-expressions are not allowed outside "
3172 "functions nor in template-argument lists");
3173 cp_parser_skip_to_end_of_block_or_statement (parser);
3174 expr = error_mark_node;
3178 /* Start the statement-expression. */
3179 expr = begin_stmt_expr ();
3180 /* Parse the compound-statement. */
3181 cp_parser_compound_statement (parser, expr, false);
3183 expr = finish_stmt_expr (expr, false);
3188 /* Parse the parenthesized expression. */
3189 expr = cp_parser_expression (parser, cast_p);
3190 /* Let the front end know that this expression was
3191 enclosed in parentheses. This matters in case, for
3192 example, the expression is of the form `A::B', since
3193 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3195 finish_parenthesized_expr (expr);
3197 /* The `>' token might be the end of a template-id or
3198 template-parameter-list now. */
3199 parser->greater_than_is_operator_p
3200 = saved_greater_than_is_operator_p;
3201 /* Consume the `)'. */
3202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3203 cp_parser_skip_to_end_of_statement (parser);
3209 switch (token->keyword)
3211 /* These two are the boolean literals. */
3213 cp_lexer_consume_token (parser->lexer);
3214 return boolean_true_node;
3216 cp_lexer_consume_token (parser->lexer);
3217 return boolean_false_node;
3219 /* The `__null' literal. */
3221 cp_lexer_consume_token (parser->lexer);
3224 /* Recognize the `this' keyword. */
3226 cp_lexer_consume_token (parser->lexer);
3227 if (parser->local_variables_forbidden_p)
3229 error ("%<this%> may not be used in this context");
3230 return error_mark_node;
3232 /* Pointers cannot appear in constant-expressions. */
3233 if (cp_parser_non_integral_constant_expression (parser,
3235 return error_mark_node;
3236 return finish_this_expr ();
3238 /* The `operator' keyword can be the beginning of an
3243 case RID_FUNCTION_NAME:
3244 case RID_PRETTY_FUNCTION_NAME:
3245 case RID_C99_FUNCTION_NAME:
3246 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3247 __func__ are the names of variables -- but they are
3248 treated specially. Therefore, they are handled here,
3249 rather than relying on the generic id-expression logic
3250 below. Grammatically, these names are id-expressions.
3252 Consume the token. */
3253 token = cp_lexer_consume_token (parser->lexer);
3254 /* Look up the name. */
3255 return finish_fname (token->u.value);
3262 /* The `__builtin_va_arg' construct is used to handle
3263 `va_arg'. Consume the `__builtin_va_arg' token. */
3264 cp_lexer_consume_token (parser->lexer);
3265 /* Look for the opening `('. */
3266 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3267 /* Now, parse the assignment-expression. */
3268 expression = cp_parser_assignment_expression (parser,
3270 /* Look for the `,'. */
3271 cp_parser_require (parser, CPP_COMMA, "`,'");
3272 /* Parse the type-id. */
3273 type = cp_parser_type_id (parser);
3274 /* Look for the closing `)'. */
3275 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3276 /* Using `va_arg' in a constant-expression is not
3278 if (cp_parser_non_integral_constant_expression (parser,
3280 return error_mark_node;
3281 return build_x_va_arg (expression, type);
3285 return cp_parser_builtin_offsetof (parser);
3287 case RID_HAS_NOTHROW_ASSIGN:
3288 case RID_HAS_NOTHROW_CONSTRUCTOR:
3289 case RID_HAS_NOTHROW_COPY:
3290 case RID_HAS_TRIVIAL_ASSIGN:
3291 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3292 case RID_HAS_TRIVIAL_COPY:
3293 case RID_HAS_TRIVIAL_DESTRUCTOR:
3294 case RID_HAS_VIRTUAL_DESTRUCTOR:
3295 case RID_IS_ABSTRACT:
3296 case RID_IS_BASE_OF:
3298 case RID_IS_CONVERTIBLE_TO:
3302 case RID_IS_POLYMORPHIC:
3304 return cp_parser_trait_expr (parser, token->keyword);
3306 /* Objective-C++ expressions. */
3308 case RID_AT_PROTOCOL:
3309 case RID_AT_SELECTOR:
3310 return cp_parser_objc_expression (parser);
3313 cp_parser_error (parser, "expected primary-expression");
3314 return error_mark_node;
3317 /* An id-expression can start with either an identifier, a
3318 `::' as the beginning of a qualified-id, or the "operator"
3322 case CPP_TEMPLATE_ID:
3323 case CPP_NESTED_NAME_SPECIFIER:
3327 const char *error_msg;
3332 /* Parse the id-expression. */
3334 = cp_parser_id_expression (parser,
3335 /*template_keyword_p=*/false,
3336 /*check_dependency_p=*/true,
3338 /*declarator_p=*/false,
3339 /*optional_p=*/false);
3340 if (id_expression == error_mark_node)
3341 return error_mark_node;
3342 token = cp_lexer_peek_token (parser->lexer);
3343 done = (token->type != CPP_OPEN_SQUARE
3344 && token->type != CPP_OPEN_PAREN
3345 && token->type != CPP_DOT
3346 && token->type != CPP_DEREF
3347 && token->type != CPP_PLUS_PLUS
3348 && token->type != CPP_MINUS_MINUS);
3349 /* If we have a template-id, then no further lookup is
3350 required. If the template-id was for a template-class, we
3351 will sometimes have a TYPE_DECL at this point. */
3352 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3353 || TREE_CODE (id_expression) == TYPE_DECL)
3354 decl = id_expression;
3355 /* Look up the name. */
3358 tree ambiguous_decls;
3360 decl = cp_parser_lookup_name (parser, id_expression,
3363 /*is_namespace=*/false,
3364 /*check_dependency=*/true,
3366 /* If the lookup was ambiguous, an error will already have
3368 if (ambiguous_decls)
3369 return error_mark_node;
3371 /* In Objective-C++, an instance variable (ivar) may be preferred
3372 to whatever cp_parser_lookup_name() found. */
3373 decl = objc_lookup_ivar (decl, id_expression);
3375 /* If name lookup gives us a SCOPE_REF, then the
3376 qualifying scope was dependent. */
3377 if (TREE_CODE (decl) == SCOPE_REF)
3379 /* At this point, we do not know if DECL is a valid
3380 integral constant expression. We assume that it is
3381 in fact such an expression, so that code like:
3383 template <int N> struct A {
3387 is accepted. At template-instantiation time, we
3388 will check that B<N>::i is actually a constant. */
3391 /* Check to see if DECL is a local variable in a context
3392 where that is forbidden. */
3393 if (parser->local_variables_forbidden_p
3394 && local_variable_p (decl))
3396 /* It might be that we only found DECL because we are
3397 trying to be generous with pre-ISO scoping rules.
3398 For example, consider:
3402 for (int i = 0; i < 10; ++i) {}
3403 extern void f(int j = i);
3406 Here, name look up will originally find the out
3407 of scope `i'. We need to issue a warning message,
3408 but then use the global `i'. */
3409 decl = check_for_out_of_scope_variable (decl);
3410 if (local_variable_p (decl))
3412 error ("local variable %qD may not appear in this context",
3414 return error_mark_node;
3419 decl = (finish_id_expression
3420 (id_expression, decl, parser->scope,
3422 parser->integral_constant_expression_p,
3423 parser->allow_non_integral_constant_expression_p,
3424 &parser->non_integral_constant_expression_p,
3425 template_p, done, address_p,
3429 cp_parser_error (parser, error_msg);
3433 /* Anything else is an error. */
3435 /* ...unless we have an Objective-C++ message or string literal,
3437 if (c_dialect_objc ()
3438 && (token->type == CPP_OPEN_SQUARE
3439 || token->type == CPP_OBJC_STRING))
3440 return cp_parser_objc_expression (parser);
3442 cp_parser_error (parser, "expected primary-expression");
3443 return error_mark_node;
3447 /* Parse an id-expression.
3454 :: [opt] nested-name-specifier template [opt] unqualified-id
3456 :: operator-function-id
3459 Return a representation of the unqualified portion of the
3460 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3461 a `::' or nested-name-specifier.
3463 Often, if the id-expression was a qualified-id, the caller will
3464 want to make a SCOPE_REF to represent the qualified-id. This
3465 function does not do this in order to avoid wastefully creating
3466 SCOPE_REFs when they are not required.
3468 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3471 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3472 uninstantiated templates.
3474 If *TEMPLATE_P is non-NULL, it is set to true iff the
3475 `template' keyword is used to explicitly indicate that the entity
3476 named is a template.
3478 If DECLARATOR_P is true, the id-expression is appearing as part of
3479 a declarator, rather than as part of an expression. */
3482 cp_parser_id_expression (cp_parser *parser,
3483 bool template_keyword_p,
3484 bool check_dependency_p,
3489 bool global_scope_p;
3490 bool nested_name_specifier_p;
3492 /* Assume the `template' keyword was not used. */
3494 *template_p = template_keyword_p;
3496 /* Look for the optional `::' operator. */
3498 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3500 /* Look for the optional nested-name-specifier. */
3501 nested_name_specifier_p
3502 = (cp_parser_nested_name_specifier_opt (parser,
3503 /*typename_keyword_p=*/false,
3508 /* If there is a nested-name-specifier, then we are looking at
3509 the first qualified-id production. */
3510 if (nested_name_specifier_p)
3513 tree saved_object_scope;
3514 tree saved_qualifying_scope;
3515 tree unqualified_id;
3518 /* See if the next token is the `template' keyword. */
3520 template_p = &is_template;
3521 *template_p = cp_parser_optional_template_keyword (parser);
3522 /* Name lookup we do during the processing of the
3523 unqualified-id might obliterate SCOPE. */
3524 saved_scope = parser->scope;
3525 saved_object_scope = parser->object_scope;
3526 saved_qualifying_scope = parser->qualifying_scope;
3527 /* Process the final unqualified-id. */
3528 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3531 /*optional_p=*/false);
3532 /* Restore the SAVED_SCOPE for our caller. */
3533 parser->scope = saved_scope;
3534 parser->object_scope = saved_object_scope;
3535 parser->qualifying_scope = saved_qualifying_scope;
3537 return unqualified_id;
3539 /* Otherwise, if we are in global scope, then we are looking at one
3540 of the other qualified-id productions. */
3541 else if (global_scope_p)
3546 /* Peek at the next token. */
3547 token = cp_lexer_peek_token (parser->lexer);
3549 /* If it's an identifier, and the next token is not a "<", then
3550 we can avoid the template-id case. This is an optimization
3551 for this common case. */
3552 if (token->type == CPP_NAME
3553 && !cp_parser_nth_token_starts_template_argument_list_p
3555 return cp_parser_identifier (parser);
3557 cp_parser_parse_tentatively (parser);
3558 /* Try a template-id. */
3559 id = cp_parser_template_id (parser,
3560 /*template_keyword_p=*/false,
3561 /*check_dependency_p=*/true,
3563 /* If that worked, we're done. */
3564 if (cp_parser_parse_definitely (parser))
3567 /* Peek at the next token. (Changes in the token buffer may
3568 have invalidated the pointer obtained above.) */
3569 token = cp_lexer_peek_token (parser->lexer);
3571 switch (token->type)
3574 return cp_parser_identifier (parser);
3577 if (token->keyword == RID_OPERATOR)
3578 return cp_parser_operator_function_id (parser);
3582 cp_parser_error (parser, "expected id-expression");
3583 return error_mark_node;
3587 return cp_parser_unqualified_id (parser, template_keyword_p,
3588 /*check_dependency_p=*/true,
3593 /* Parse an unqualified-id.
3597 operator-function-id
3598 conversion-function-id
3602 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3603 keyword, in a construct like `A::template ...'.
3605 Returns a representation of unqualified-id. For the `identifier'
3606 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3607 production a BIT_NOT_EXPR is returned; the operand of the
3608 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3609 other productions, see the documentation accompanying the
3610 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3611 names are looked up in uninstantiated templates. If DECLARATOR_P
3612 is true, the unqualified-id is appearing as part of a declarator,
3613 rather than as part of an expression. */
3616 cp_parser_unqualified_id (cp_parser* parser,
3617 bool template_keyword_p,
3618 bool check_dependency_p,
3624 /* Peek at the next token. */
3625 token = cp_lexer_peek_token (parser->lexer);
3627 switch (token->type)
3633 /* We don't know yet whether or not this will be a
3635 cp_parser_parse_tentatively (parser);
3636 /* Try a template-id. */
3637 id = cp_parser_template_id (parser, template_keyword_p,
3640 /* If it worked, we're done. */
3641 if (cp_parser_parse_definitely (parser))
3643 /* Otherwise, it's an ordinary identifier. */
3644 return cp_parser_identifier (parser);
3647 case CPP_TEMPLATE_ID:
3648 return cp_parser_template_id (parser, template_keyword_p,
3655 tree qualifying_scope;
3660 /* Consume the `~' token. */
3661 cp_lexer_consume_token (parser->lexer);
3662 /* Parse the class-name. The standard, as written, seems to
3665 template <typename T> struct S { ~S (); };
3666 template <typename T> S<T>::~S() {}
3668 is invalid, since `~' must be followed by a class-name, but
3669 `S<T>' is dependent, and so not known to be a class.
3670 That's not right; we need to look in uninstantiated
3671 templates. A further complication arises from:
3673 template <typename T> void f(T t) {
3677 Here, it is not possible to look up `T' in the scope of `T'
3678 itself. We must look in both the current scope, and the
3679 scope of the containing complete expression.
3681 Yet another issue is:
3690 The standard does not seem to say that the `S' in `~S'
3691 should refer to the type `S' and not the data member
3694 /* DR 244 says that we look up the name after the "~" in the
3695 same scope as we looked up the qualifying name. That idea
3696 isn't fully worked out; it's more complicated than that. */
3697 scope = parser->scope;
3698 object_scope = parser->object_scope;
3699 qualifying_scope = parser->qualifying_scope;
3701 /* Check for invalid scopes. */
3702 if (scope == error_mark_node)
3704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3705 cp_lexer_consume_token (parser->lexer);
3706 return error_mark_node;
3708 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3710 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3711 error ("scope %qT before %<~%> is not a class-name", scope);
3712 cp_parser_simulate_error (parser);
3713 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3714 cp_lexer_consume_token (parser->lexer);
3715 return error_mark_node;
3717 gcc_assert (!scope || TYPE_P (scope));
3719 /* If the name is of the form "X::~X" it's OK. */
3720 token = cp_lexer_peek_token (parser->lexer);
3722 && token->type == CPP_NAME
3723 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3725 && constructor_name_p (token->u.value, scope))
3727 cp_lexer_consume_token (parser->lexer);
3728 return build_nt (BIT_NOT_EXPR, scope);
3731 /* If there was an explicit qualification (S::~T), first look
3732 in the scope given by the qualification (i.e., S). */
3734 type_decl = NULL_TREE;
3737 cp_parser_parse_tentatively (parser);
3738 type_decl = cp_parser_class_name (parser,
3739 /*typename_keyword_p=*/false,
3740 /*template_keyword_p=*/false,
3742 /*check_dependency=*/false,
3743 /*class_head_p=*/false,
3745 if (cp_parser_parse_definitely (parser))
3748 /* In "N::S::~S", look in "N" as well. */
3749 if (!done && scope && qualifying_scope)
3751 cp_parser_parse_tentatively (parser);
3752 parser->scope = qualifying_scope;
3753 parser->object_scope = NULL_TREE;
3754 parser->qualifying_scope = NULL_TREE;
3756 = cp_parser_class_name (parser,
3757 /*typename_keyword_p=*/false,
3758 /*template_keyword_p=*/false,
3760 /*check_dependency=*/false,
3761 /*class_head_p=*/false,
3763 if (cp_parser_parse_definitely (parser))
3766 /* In "p->S::~T", look in the scope given by "*p" as well. */
3767 else if (!done && object_scope)
3769 cp_parser_parse_tentatively (parser);
3770 parser->scope = object_scope;
3771 parser->object_scope = NULL_TREE;
3772 parser->qualifying_scope = NULL_TREE;
3774 = cp_parser_class_name (parser,
3775 /*typename_keyword_p=*/false,
3776 /*template_keyword_p=*/false,
3778 /*check_dependency=*/false,
3779 /*class_head_p=*/false,
3781 if (cp_parser_parse_definitely (parser))
3784 /* Look in the surrounding context. */
3787 parser->scope = NULL_TREE;
3788 parser->object_scope = NULL_TREE;
3789 parser->qualifying_scope = NULL_TREE;
3791 = cp_parser_class_name (parser,
3792 /*typename_keyword_p=*/false,
3793 /*template_keyword_p=*/false,
3795 /*check_dependency=*/false,
3796 /*class_head_p=*/false,
3799 /* If an error occurred, assume that the name of the
3800 destructor is the same as the name of the qualifying
3801 class. That allows us to keep parsing after running
3802 into ill-formed destructor names. */
3803 if (type_decl == error_mark_node && scope)
3804 return build_nt (BIT_NOT_EXPR, scope);
3805 else if (type_decl == error_mark_node)
3806 return error_mark_node;
3808 /* Check that destructor name and scope match. */
3809 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3811 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3812 error ("declaration of %<~%T%> as member of %qT",
3814 cp_parser_simulate_error (parser);
3815 return error_mark_node;
3820 A typedef-name that names a class shall not be used as the
3821 identifier in the declarator for a destructor declaration. */
3823 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3824 && !DECL_SELF_REFERENCE_P (type_decl)
3825 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3826 error ("typedef-name %qD used as destructor declarator",
3829 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3833 if (token->keyword == RID_OPERATOR)
3837 /* This could be a template-id, so we try that first. */
3838 cp_parser_parse_tentatively (parser);
3839 /* Try a template-id. */
3840 id = cp_parser_template_id (parser, template_keyword_p,
3841 /*check_dependency_p=*/true,
3843 /* If that worked, we're done. */
3844 if (cp_parser_parse_definitely (parser))
3846 /* We still don't know whether we're looking at an
3847 operator-function-id or a conversion-function-id. */
3848 cp_parser_parse_tentatively (parser);
3849 /* Try an operator-function-id. */
3850 id = cp_parser_operator_function_id (parser);
3851 /* If that didn't work, try a conversion-function-id. */
3852 if (!cp_parser_parse_definitely (parser))
3853 id = cp_parser_conversion_function_id (parser);
3862 cp_parser_error (parser, "expected unqualified-id");
3863 return error_mark_node;
3867 /* Parse an (optional) nested-name-specifier.
3869 nested-name-specifier:
3870 class-or-namespace-name :: nested-name-specifier [opt]
3871 class-or-namespace-name :: template nested-name-specifier [opt]
3873 PARSER->SCOPE should be set appropriately before this function is
3874 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3875 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3878 Sets PARSER->SCOPE to the class (TYPE) or namespace
3879 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3880 it unchanged if there is no nested-name-specifier. Returns the new
3881 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3883 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3884 part of a declaration and/or decl-specifier. */
3887 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3888 bool typename_keyword_p,
3889 bool check_dependency_p,
3891 bool is_declaration)
3893 bool success = false;
3894 cp_token_position start = 0;
3897 /* Remember where the nested-name-specifier starts. */
3898 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3900 start = cp_lexer_token_position (parser->lexer, false);
3901 push_deferring_access_checks (dk_deferred);
3908 tree saved_qualifying_scope;
3909 bool template_keyword_p;
3911 /* Spot cases that cannot be the beginning of a
3912 nested-name-specifier. */
3913 token = cp_lexer_peek_token (parser->lexer);
3915 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3916 the already parsed nested-name-specifier. */
3917 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3919 /* Grab the nested-name-specifier and continue the loop. */
3920 cp_parser_pre_parsed_nested_name_specifier (parser);
3921 /* If we originally encountered this nested-name-specifier
3922 with IS_DECLARATION set to false, we will not have
3923 resolved TYPENAME_TYPEs, so we must do so here. */
3925 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3927 new_scope = resolve_typename_type (parser->scope,
3928 /*only_current_p=*/false);
3929 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3930 parser->scope = new_scope;
3936 /* Spot cases that cannot be the beginning of a
3937 nested-name-specifier. On the second and subsequent times
3938 through the loop, we look for the `template' keyword. */
3939 if (success && token->keyword == RID_TEMPLATE)
3941 /* A template-id can start a nested-name-specifier. */
3942 else if (token->type == CPP_TEMPLATE_ID)
3946 /* If the next token is not an identifier, then it is
3947 definitely not a class-or-namespace-name. */
3948 if (token->type != CPP_NAME)
3950 /* If the following token is neither a `<' (to begin a
3951 template-id), nor a `::', then we are not looking at a
3952 nested-name-specifier. */
3953 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3954 if (token->type != CPP_SCOPE
3955 && !cp_parser_nth_token_starts_template_argument_list_p
3960 /* The nested-name-specifier is optional, so we parse
3962 cp_parser_parse_tentatively (parser);
3964 /* Look for the optional `template' keyword, if this isn't the
3965 first time through the loop. */
3967 template_keyword_p = cp_parser_optional_template_keyword (parser);
3969 template_keyword_p = false;
3971 /* Save the old scope since the name lookup we are about to do
3972 might destroy it. */
3973 old_scope = parser->scope;
3974 saved_qualifying_scope = parser->qualifying_scope;
3975 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3976 look up names in "X<T>::I" in order to determine that "Y" is
3977 a template. So, if we have a typename at this point, we make
3978 an effort to look through it. */
3980 && !typename_keyword_p
3982 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3983 parser->scope = resolve_typename_type (parser->scope,
3984 /*only_current_p=*/false);
3985 /* Parse the qualifying entity. */
3987 = cp_parser_class_or_namespace_name (parser,
3993 /* Look for the `::' token. */
3994 cp_parser_require (parser, CPP_SCOPE, "`::'");
3996 /* If we found what we wanted, we keep going; otherwise, we're
3998 if (!cp_parser_parse_definitely (parser))
4000 bool error_p = false;
4002 /* Restore the OLD_SCOPE since it was valid before the
4003 failed attempt at finding the last
4004 class-or-namespace-name. */
4005 parser->scope = old_scope;
4006 parser->qualifying_scope = saved_qualifying_scope;
4007 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4009 /* If the next token is an identifier, and the one after
4010 that is a `::', then any valid interpretation would have
4011 found a class-or-namespace-name. */
4012 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4013 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4015 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4018 token = cp_lexer_consume_token (parser->lexer);
4021 if (!token->ambiguous_p)
4024 tree ambiguous_decls;
4026 decl = cp_parser_lookup_name (parser, token->u.value,
4028 /*is_template=*/false,
4029 /*is_namespace=*/false,
4030 /*check_dependency=*/true,
4032 if (TREE_CODE (decl) == TEMPLATE_DECL)
4033 error ("%qD used without template parameters", decl);
4034 else if (ambiguous_decls)
4036 error ("reference to %qD is ambiguous",
4038 print_candidates (ambiguous_decls);
4039 decl = error_mark_node;
4042 cp_parser_name_lookup_error
4043 (parser, token->u.value, decl,
4044 "is not a class or namespace");
4046 parser->scope = error_mark_node;
4048 /* Treat this as a successful nested-name-specifier
4053 If the name found is not a class-name (clause
4054 _class_) or namespace-name (_namespace.def_), the
4055 program is ill-formed. */
4058 cp_lexer_consume_token (parser->lexer);
4062 /* We've found one valid nested-name-specifier. */
4064 /* Name lookup always gives us a DECL. */
4065 if (TREE_CODE (new_scope) == TYPE_DECL)
4066 new_scope = TREE_TYPE (new_scope);
4067 /* Uses of "template" must be followed by actual templates. */
4068 if (template_keyword_p
4069 && !(CLASS_TYPE_P (new_scope)
4070 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4071 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4072 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4073 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4074 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4075 == TEMPLATE_ID_EXPR)))
4076 pedwarn (TYPE_P (new_scope)
4077 ? "%qT is not a template"
4078 : "%qD is not a template",
4080 /* If it is a class scope, try to complete it; we are about to
4081 be looking up names inside the class. */
4082 if (TYPE_P (new_scope)
4083 /* Since checking types for dependency can be expensive,
4084 avoid doing it if the type is already complete. */
4085 && !COMPLETE_TYPE_P (new_scope)
4086 /* Do not try to complete dependent types. */
4087 && !dependent_type_p (new_scope))
4089 new_scope = complete_type (new_scope);
4090 /* If it is a typedef to current class, use the current
4091 class instead, as the typedef won't have any names inside
4093 if (!COMPLETE_TYPE_P (new_scope)
4094 && currently_open_class (new_scope))
4095 new_scope = TYPE_MAIN_VARIANT (new_scope);
4097 /* Make sure we look in the right scope the next time through
4099 parser->scope = new_scope;
4102 /* If parsing tentatively, replace the sequence of tokens that makes
4103 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4104 token. That way, should we re-parse the token stream, we will
4105 not have to repeat the effort required to do the parse, nor will
4106 we issue duplicate error messages. */
4107 if (success && start)
4111 token = cp_lexer_token_at (parser->lexer, start);
4112 /* Reset the contents of the START token. */
4113 token->type = CPP_NESTED_NAME_SPECIFIER;
4114 /* Retrieve any deferred checks. Do not pop this access checks yet
4115 so the memory will not be reclaimed during token replacing below. */
4116 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4117 token->u.tree_check_value->value = parser->scope;
4118 token->u.tree_check_value->checks = get_deferred_access_checks ();
4119 token->u.tree_check_value->qualifying_scope =
4120 parser->qualifying_scope;
4121 token->keyword = RID_MAX;
4123 /* Purge all subsequent tokens. */
4124 cp_lexer_purge_tokens_after (parser->lexer, start);
4128 pop_to_parent_deferring_access_checks ();
4130 return success ? parser->scope : NULL_TREE;
4133 /* Parse a nested-name-specifier. See
4134 cp_parser_nested_name_specifier_opt for details. This function
4135 behaves identically, except that it will an issue an error if no
4136 nested-name-specifier is present. */
4139 cp_parser_nested_name_specifier (cp_parser *parser,
4140 bool typename_keyword_p,
4141 bool check_dependency_p,
4143 bool is_declaration)
4147 /* Look for the nested-name-specifier. */
4148 scope = cp_parser_nested_name_specifier_opt (parser,
4153 /* If it was not present, issue an error message. */
4156 cp_parser_error (parser, "expected nested-name-specifier");
4157 parser->scope = NULL_TREE;
4163 /* Parse a class-or-namespace-name.
4165 class-or-namespace-name:
4169 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4170 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4171 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4172 TYPE_P is TRUE iff the next name should be taken as a class-name,
4173 even the same name is declared to be another entity in the same
4176 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4177 specified by the class-or-namespace-name. If neither is found the
4178 ERROR_MARK_NODE is returned. */
4181 cp_parser_class_or_namespace_name (cp_parser *parser,
4182 bool typename_keyword_p,
4183 bool template_keyword_p,
4184 bool check_dependency_p,
4186 bool is_declaration)
4189 tree saved_qualifying_scope;
4190 tree saved_object_scope;
4194 /* Before we try to parse the class-name, we must save away the
4195 current PARSER->SCOPE since cp_parser_class_name will destroy
4197 saved_scope = parser->scope;
4198 saved_qualifying_scope = parser->qualifying_scope;
4199 saved_object_scope = parser->object_scope;
4200 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4201 there is no need to look for a namespace-name. */
4202 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4204 cp_parser_parse_tentatively (parser);
4205 scope = cp_parser_class_name (parser,
4208 type_p ? class_type : none_type,
4210 /*class_head_p=*/false,
4212 /* If that didn't work, try for a namespace-name. */
4213 if (!only_class_p && !cp_parser_parse_definitely (parser))
4215 /* Restore the saved scope. */
4216 parser->scope = saved_scope;
4217 parser->qualifying_scope = saved_qualifying_scope;
4218 parser->object_scope = saved_object_scope;
4219 /* If we are not looking at an identifier followed by the scope
4220 resolution operator, then this is not part of a
4221 nested-name-specifier. (Note that this function is only used
4222 to parse the components of a nested-name-specifier.) */
4223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4224 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4225 return error_mark_node;
4226 scope = cp_parser_namespace_name (parser);
4232 /* Parse a postfix-expression.
4236 postfix-expression [ expression ]
4237 postfix-expression ( expression-list [opt] )
4238 simple-type-specifier ( expression-list [opt] )
4239 typename :: [opt] nested-name-specifier identifier
4240 ( expression-list [opt] )
4241 typename :: [opt] nested-name-specifier template [opt] template-id
4242 ( expression-list [opt] )
4243 postfix-expression . template [opt] id-expression
4244 postfix-expression -> template [opt] id-expression
4245 postfix-expression . pseudo-destructor-name
4246 postfix-expression -> pseudo-destructor-name
4247 postfix-expression ++
4248 postfix-expression --
4249 dynamic_cast < type-id > ( expression )
4250 static_cast < type-id > ( expression )
4251 reinterpret_cast < type-id > ( expression )
4252 const_cast < type-id > ( expression )
4253 typeid ( expression )
4259 ( type-id ) { initializer-list , [opt] }
4261 This extension is a GNU version of the C99 compound-literal
4262 construct. (The C99 grammar uses `type-name' instead of `type-id',
4263 but they are essentially the same concept.)
4265 If ADDRESS_P is true, the postfix expression is the operand of the
4266 `&' operator. CAST_P is true if this expression is the target of a
4269 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4270 class member access expressions [expr.ref].
4272 Returns a representation of the expression. */
4275 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4276 bool member_access_only_p)
4280 cp_id_kind idk = CP_ID_KIND_NONE;
4281 tree postfix_expression = NULL_TREE;
4282 bool is_member_access = false;
4284 /* Peek at the next token. */
4285 token = cp_lexer_peek_token (parser->lexer);
4286 /* Some of the productions are determined by keywords. */
4287 keyword = token->keyword;
4297 const char *saved_message;
4299 /* All of these can be handled in the same way from the point
4300 of view of parsing. Begin by consuming the token
4301 identifying the cast. */
4302 cp_lexer_consume_token (parser->lexer);
4304 /* New types cannot be defined in the cast. */
4305 saved_message = parser->type_definition_forbidden_message;
4306 parser->type_definition_forbidden_message
4307 = "types may not be defined in casts";
4309 /* Look for the opening `<'. */
4310 cp_parser_require (parser, CPP_LESS, "`<'");
4311 /* Parse the type to which we are casting. */
4312 type = cp_parser_type_id (parser);
4313 /* Look for the closing `>'. */
4314 cp_parser_require (parser, CPP_GREATER, "`>'");
4315 /* Restore the old message. */
4316 parser->type_definition_forbidden_message = saved_message;
4318 /* And the expression which is being cast. */
4319 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4320 expression = cp_parser_expression (parser, /*cast_p=*/true);
4321 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4323 /* Only type conversions to integral or enumeration types
4324 can be used in constant-expressions. */
4325 if (!cast_valid_in_integral_constant_expression_p (type)
4326 && (cp_parser_non_integral_constant_expression
4328 "a cast to a type other than an integral or "
4329 "enumeration type")))
4330 return error_mark_node;
4336 = build_dynamic_cast (type, expression, tf_warning_or_error);
4340 = build_static_cast (type, expression, tf_warning_or_error);
4344 = build_reinterpret_cast (type, expression,
4345 tf_warning_or_error);
4349 = build_const_cast (type, expression, tf_warning_or_error);
4360 const char *saved_message;
4361 bool saved_in_type_id_in_expr_p;
4363 /* Consume the `typeid' token. */
4364 cp_lexer_consume_token (parser->lexer);
4365 /* Look for the `(' token. */
4366 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4367 /* Types cannot be defined in a `typeid' expression. */
4368 saved_message = parser->type_definition_forbidden_message;
4369 parser->type_definition_forbidden_message
4370 = "types may not be defined in a `typeid\' expression";
4371 /* We can't be sure yet whether we're looking at a type-id or an
4373 cp_parser_parse_tentatively (parser);
4374 /* Try a type-id first. */
4375 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4376 parser->in_type_id_in_expr_p = true;
4377 type = cp_parser_type_id (parser);
4378 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4379 /* Look for the `)' token. Otherwise, we can't be sure that
4380 we're not looking at an expression: consider `typeid (int
4381 (3))', for example. */
4382 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4383 /* If all went well, simply lookup the type-id. */
4384 if (cp_parser_parse_definitely (parser))
4385 postfix_expression = get_typeid (type);
4386 /* Otherwise, fall back to the expression variant. */
4391 /* Look for an expression. */
4392 expression = cp_parser_expression (parser, /*cast_p=*/false);
4393 /* Compute its typeid. */
4394 postfix_expression = build_typeid (expression);
4395 /* Look for the `)' token. */
4396 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4398 /* Restore the saved message. */
4399 parser->type_definition_forbidden_message = saved_message;
4400 /* `typeid' may not appear in an integral constant expression. */
4401 if (cp_parser_non_integral_constant_expression(parser,
4402 "`typeid' operator"))
4403 return error_mark_node;
4410 /* The syntax permitted here is the same permitted for an
4411 elaborated-type-specifier. */
4412 type = cp_parser_elaborated_type_specifier (parser,
4413 /*is_friend=*/false,
4414 /*is_declaration=*/false);
4415 postfix_expression = cp_parser_functional_cast (parser, type);
4423 /* If the next thing is a simple-type-specifier, we may be
4424 looking at a functional cast. We could also be looking at
4425 an id-expression. So, we try the functional cast, and if
4426 that doesn't work we fall back to the primary-expression. */
4427 cp_parser_parse_tentatively (parser);
4428 /* Look for the simple-type-specifier. */
4429 type = cp_parser_simple_type_specifier (parser,
4430 /*decl_specs=*/NULL,
4431 CP_PARSER_FLAGS_NONE);
4432 /* Parse the cast itself. */
4433 if (!cp_parser_error_occurred (parser))
4435 = cp_parser_functional_cast (parser, type);
4436 /* If that worked, we're done. */
4437 if (cp_parser_parse_definitely (parser))
4440 /* If the functional-cast didn't work out, try a
4441 compound-literal. */
4442 if (cp_parser_allow_gnu_extensions_p (parser)
4443 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4445 VEC(constructor_elt,gc) *initializer_list = NULL;
4446 bool saved_in_type_id_in_expr_p;
4448 cp_parser_parse_tentatively (parser);
4449 /* Consume the `('. */
4450 cp_lexer_consume_token (parser->lexer);
4451 /* Parse the type. */
4452 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4453 parser->in_type_id_in_expr_p = true;
4454 type = cp_parser_type_id (parser);
4455 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4456 /* Look for the `)'. */
4457 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4458 /* Look for the `{'. */
4459 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4460 /* If things aren't going well, there's no need to
4462 if (!cp_parser_error_occurred (parser))
4464 bool non_constant_p;
4465 /* Parse the initializer-list. */
4467 = cp_parser_initializer_list (parser, &non_constant_p);
4468 /* Allow a trailing `,'. */
4469 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4470 cp_lexer_consume_token (parser->lexer);
4471 /* Look for the final `}'. */
4472 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4474 /* If that worked, we're definitely looking at a
4475 compound-literal expression. */
4476 if (cp_parser_parse_definitely (parser))
4478 /* Warn the user that a compound literal is not
4479 allowed in standard C++. */
4481 pedwarn ("ISO C++ forbids compound-literals");
4482 /* For simplicity, we disallow compound literals in
4483 constant-expressions. We could
4484 allow compound literals of integer type, whose
4485 initializer was a constant, in constant
4486 expressions. Permitting that usage, as a further
4487 extension, would not change the meaning of any
4488 currently accepted programs. (Of course, as
4489 compound literals are not part of ISO C++, the
4490 standard has nothing to say.) */
4491 if (cp_parser_non_integral_constant_expression
4492 (parser, "non-constant compound literals"))
4494 postfix_expression = error_mark_node;
4497 /* Form the representation of the compound-literal. */
4499 = finish_compound_literal (type, initializer_list);
4504 /* It must be a primary-expression. */
4506 = cp_parser_primary_expression (parser, address_p, cast_p,
4507 /*template_arg_p=*/false,
4513 /* Keep looping until the postfix-expression is complete. */
4516 if (idk == CP_ID_KIND_UNQUALIFIED
4517 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4518 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4519 /* It is not a Koenig lookup function call. */
4521 = unqualified_name_lookup_error (postfix_expression);
4523 /* Peek at the next token. */
4524 token = cp_lexer_peek_token (parser->lexer);
4526 switch (token->type)
4528 case CPP_OPEN_SQUARE:
4530 = cp_parser_postfix_open_square_expression (parser,
4533 idk = CP_ID_KIND_NONE;
4534 is_member_access = false;
4537 case CPP_OPEN_PAREN:
4538 /* postfix-expression ( expression-list [opt] ) */
4541 bool is_builtin_constant_p;
4542 bool saved_integral_constant_expression_p = false;
4543 bool saved_non_integral_constant_expression_p = false;
4546 is_member_access = false;
4548 is_builtin_constant_p
4549 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4550 if (is_builtin_constant_p)
4552 /* The whole point of __builtin_constant_p is to allow
4553 non-constant expressions to appear as arguments. */
4554 saved_integral_constant_expression_p
4555 = parser->integral_constant_expression_p;
4556 saved_non_integral_constant_expression_p
4557 = parser->non_integral_constant_expression_p;
4558 parser->integral_constant_expression_p = false;
4560 args = (cp_parser_parenthesized_expression_list
4561 (parser, /*is_attribute_list=*/false,
4562 /*cast_p=*/false, /*allow_expansion_p=*/true,
4563 /*non_constant_p=*/NULL));
4564 if (is_builtin_constant_p)
4566 parser->integral_constant_expression_p
4567 = saved_integral_constant_expression_p;
4568 parser->non_integral_constant_expression_p
4569 = saved_non_integral_constant_expression_p;
4572 if (args == error_mark_node)
4574 postfix_expression = error_mark_node;
4578 /* Function calls are not permitted in
4579 constant-expressions. */
4580 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4581 && cp_parser_non_integral_constant_expression (parser,
4584 postfix_expression = error_mark_node;
4589 if (idk == CP_ID_KIND_UNQUALIFIED)
4591 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4597 = perform_koenig_lookup (postfix_expression, args);
4601 = unqualified_fn_lookup_error (postfix_expression);
4603 /* We do not perform argument-dependent lookup if
4604 normal lookup finds a non-function, in accordance
4605 with the expected resolution of DR 218. */
4606 else if (args && is_overloaded_fn (postfix_expression))
4608 tree fn = get_first_fn (postfix_expression);
4610 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4611 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4613 /* Only do argument dependent lookup if regular
4614 lookup does not find a set of member functions.
4615 [basic.lookup.koenig]/2a */
4616 if (!DECL_FUNCTION_MEMBER_P (fn))
4620 = perform_koenig_lookup (postfix_expression, args);
4625 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4627 tree instance = TREE_OPERAND (postfix_expression, 0);
4628 tree fn = TREE_OPERAND (postfix_expression, 1);
4630 if (processing_template_decl
4631 && (type_dependent_expression_p (instance)
4632 || (!BASELINK_P (fn)
4633 && TREE_CODE (fn) != FIELD_DECL)
4634 || type_dependent_expression_p (fn)
4635 || any_type_dependent_arguments_p (args)))
4638 = build_nt_call_list (postfix_expression, args);
4642 if (BASELINK_P (fn))
4644 = (build_new_method_call
4645 (instance, fn, args, NULL_TREE,
4646 (idk == CP_ID_KIND_QUALIFIED
4647 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4649 tf_warning_or_error));
4652 = finish_call_expr (postfix_expression, args,
4653 /*disallow_virtual=*/false,
4655 tf_warning_or_error);
4657 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4658 || TREE_CODE (postfix_expression) == MEMBER_REF
4659 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4660 postfix_expression = (build_offset_ref_call_from_tree
4661 (postfix_expression, args));
4662 else if (idk == CP_ID_KIND_QUALIFIED)
4663 /* A call to a static class member, or a namespace-scope
4666 = finish_call_expr (postfix_expression, args,
4667 /*disallow_virtual=*/true,
4669 tf_warning_or_error);
4671 /* All other function calls. */
4673 = finish_call_expr (postfix_expression, args,
4674 /*disallow_virtual=*/false,
4676 tf_warning_or_error);
4678 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4679 idk = CP_ID_KIND_NONE;
4685 /* postfix-expression . template [opt] id-expression
4686 postfix-expression . pseudo-destructor-name
4687 postfix-expression -> template [opt] id-expression
4688 postfix-expression -> pseudo-destructor-name */
4690 /* Consume the `.' or `->' operator. */
4691 cp_lexer_consume_token (parser->lexer);
4694 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4698 is_member_access = true;
4702 /* postfix-expression ++ */
4703 /* Consume the `++' token. */
4704 cp_lexer_consume_token (parser->lexer);
4705 /* Generate a representation for the complete expression. */
4707 = finish_increment_expr (postfix_expression,
4708 POSTINCREMENT_EXPR);
4709 /* Increments may not appear in constant-expressions. */
4710 if (cp_parser_non_integral_constant_expression (parser,
4712 postfix_expression = error_mark_node;
4713 idk = CP_ID_KIND_NONE;
4714 is_member_access = false;
4717 case CPP_MINUS_MINUS:
4718 /* postfix-expression -- */
4719 /* Consume the `--' token. */
4720 cp_lexer_consume_token (parser->lexer);
4721 /* Generate a representation for the complete expression. */
4723 = finish_increment_expr (postfix_expression,
4724 POSTDECREMENT_EXPR);
4725 /* Decrements may not appear in constant-expressions. */
4726 if (cp_parser_non_integral_constant_expression (parser,
4728 postfix_expression = error_mark_node;
4729 idk = CP_ID_KIND_NONE;
4730 is_member_access = false;
4734 if (member_access_only_p)
4735 return is_member_access? postfix_expression : error_mark_node;
4737 return postfix_expression;
4741 /* We should never get here. */
4743 return error_mark_node;
4746 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4747 by cp_parser_builtin_offsetof. We're looking for
4749 postfix-expression [ expression ]
4751 FOR_OFFSETOF is set if we're being called in that context, which
4752 changes how we deal with integer constant expressions. */
4755 cp_parser_postfix_open_square_expression (cp_parser *parser,
4756 tree postfix_expression,
4761 /* Consume the `[' token. */
4762 cp_lexer_consume_token (parser->lexer);
4764 /* Parse the index expression. */
4765 /* ??? For offsetof, there is a question of what to allow here. If
4766 offsetof is not being used in an integral constant expression context,
4767 then we *could* get the right answer by computing the value at runtime.
4768 If we are in an integral constant expression context, then we might
4769 could accept any constant expression; hard to say without analysis.
4770 Rather than open the barn door too wide right away, allow only integer
4771 constant expressions here. */
4773 index = cp_parser_constant_expression (parser, false, NULL);
4775 index = cp_parser_expression (parser, /*cast_p=*/false);
4777 /* Look for the closing `]'. */
4778 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4780 /* Build the ARRAY_REF. */
4781 postfix_expression = grok_array_decl (postfix_expression, index);
4783 /* When not doing offsetof, array references are not permitted in
4784 constant-expressions. */
4786 && (cp_parser_non_integral_constant_expression
4787 (parser, "an array reference")))
4788 postfix_expression = error_mark_node;
4790 return postfix_expression;
4793 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4794 by cp_parser_builtin_offsetof. We're looking for
4796 postfix-expression . template [opt] id-expression
4797 postfix-expression . pseudo-destructor-name
4798 postfix-expression -> template [opt] id-expression
4799 postfix-expression -> pseudo-destructor-name
4801 FOR_OFFSETOF is set if we're being called in that context. That sorta
4802 limits what of the above we'll actually accept, but nevermind.
4803 TOKEN_TYPE is the "." or "->" token, which will already have been
4804 removed from the stream. */
4807 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4808 enum cpp_ttype token_type,
4809 tree postfix_expression,
4810 bool for_offsetof, cp_id_kind *idk)
4814 bool pseudo_destructor_p;
4815 tree scope = NULL_TREE;
4817 /* If this is a `->' operator, dereference the pointer. */
4818 if (token_type == CPP_DEREF)
4819 postfix_expression = build_x_arrow (postfix_expression);
4820 /* Check to see whether or not the expression is type-dependent. */
4821 dependent_p = type_dependent_expression_p (postfix_expression);
4822 /* The identifier following the `->' or `.' is not qualified. */
4823 parser->scope = NULL_TREE;
4824 parser->qualifying_scope = NULL_TREE;
4825 parser->object_scope = NULL_TREE;
4826 *idk = CP_ID_KIND_NONE;
4827 /* Enter the scope corresponding to the type of the object
4828 given by the POSTFIX_EXPRESSION. */
4829 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4831 scope = TREE_TYPE (postfix_expression);
4832 /* According to the standard, no expression should ever have
4833 reference type. Unfortunately, we do not currently match
4834 the standard in this respect in that our internal representation
4835 of an expression may have reference type even when the standard
4836 says it does not. Therefore, we have to manually obtain the
4837 underlying type here. */
4838 scope = non_reference (scope);
4839 /* The type of the POSTFIX_EXPRESSION must be complete. */
4840 if (scope == unknown_type_node)
4842 error ("%qE does not have class type", postfix_expression);
4846 scope = complete_type_or_else (scope, NULL_TREE);
4847 /* Let the name lookup machinery know that we are processing a
4848 class member access expression. */
4849 parser->context->object_type = scope;
4850 /* If something went wrong, we want to be able to discern that case,
4851 as opposed to the case where there was no SCOPE due to the type
4852 of expression being dependent. */
4854 scope = error_mark_node;
4855 /* If the SCOPE was erroneous, make the various semantic analysis
4856 functions exit quickly -- and without issuing additional error
4858 if (scope == error_mark_node)
4859 postfix_expression = error_mark_node;
4862 /* Assume this expression is not a pseudo-destructor access. */
4863 pseudo_destructor_p = false;
4865 /* If the SCOPE is a scalar type, then, if this is a valid program,
4866 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4867 is type dependent, it can be pseudo-destructor-name or something else.
4868 Try to parse it as pseudo-destructor-name first. */
4869 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4874 cp_parser_parse_tentatively (parser);
4875 /* Parse the pseudo-destructor-name. */
4877 cp_parser_pseudo_destructor_name (parser, &s, &type);
4879 && (cp_parser_error_occurred (parser)
4880 || TREE_CODE (type) != TYPE_DECL
4881 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4882 cp_parser_abort_tentative_parse (parser);
4883 else if (cp_parser_parse_definitely (parser))
4885 pseudo_destructor_p = true;
4887 = finish_pseudo_destructor_expr (postfix_expression,
4888 s, TREE_TYPE (type));
4892 if (!pseudo_destructor_p)
4894 /* If the SCOPE is not a scalar type, we are looking at an
4895 ordinary class member access expression, rather than a
4896 pseudo-destructor-name. */
4898 /* Parse the id-expression. */
4899 name = (cp_parser_id_expression
4901 cp_parser_optional_template_keyword (parser),
4902 /*check_dependency_p=*/true,
4904 /*declarator_p=*/false,
4905 /*optional_p=*/false));
4906 /* In general, build a SCOPE_REF if the member name is qualified.
4907 However, if the name was not dependent and has already been
4908 resolved; there is no need to build the SCOPE_REF. For example;
4910 struct X { void f(); };
4911 template <typename T> void f(T* t) { t->X::f(); }
4913 Even though "t" is dependent, "X::f" is not and has been resolved
4914 to a BASELINK; there is no need to include scope information. */
4916 /* But we do need to remember that there was an explicit scope for
4917 virtual function calls. */
4919 *idk = CP_ID_KIND_QUALIFIED;
4921 /* If the name is a template-id that names a type, we will get a
4922 TYPE_DECL here. That is invalid code. */
4923 if (TREE_CODE (name) == TYPE_DECL)
4925 error ("invalid use of %qD", name);
4926 postfix_expression = error_mark_node;
4930 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4932 name = build_qualified_name (/*type=*/NULL_TREE,
4936 parser->scope = NULL_TREE;
4937 parser->qualifying_scope = NULL_TREE;
4938 parser->object_scope = NULL_TREE;
4940 if (scope && name && BASELINK_P (name))
4941 adjust_result_of_qualified_name_lookup
4942 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4944 = finish_class_member_access_expr (postfix_expression, name,
4946 tf_warning_or_error);
4950 /* We no longer need to look up names in the scope of the object on
4951 the left-hand side of the `.' or `->' operator. */
4952 parser->context->object_type = NULL_TREE;
4954 /* Outside of offsetof, these operators may not appear in
4955 constant-expressions. */
4957 && (cp_parser_non_integral_constant_expression
4958 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4959 postfix_expression = error_mark_node;
4961 return postfix_expression;
4964 /* Parse a parenthesized expression-list.
4967 assignment-expression
4968 expression-list, assignment-expression
4973 identifier, expression-list
4975 CAST_P is true if this expression is the target of a cast.
4977 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4980 Returns a TREE_LIST. The TREE_VALUE of each node is a
4981 representation of an assignment-expression. Note that a TREE_LIST
4982 is returned even if there is only a single expression in the list.
4983 error_mark_node is returned if the ( and or ) are
4984 missing. NULL_TREE is returned on no expressions. The parentheses
4985 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4986 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4987 indicates whether or not all of the expressions in the list were
4991 cp_parser_parenthesized_expression_list (cp_parser* parser,
4992 bool is_attribute_list,
4994 bool allow_expansion_p,
4995 bool *non_constant_p)
4997 tree expression_list = NULL_TREE;
4998 bool fold_expr_p = is_attribute_list;
4999 tree identifier = NULL_TREE;
5000 bool saved_greater_than_is_operator_p;
5002 /* Assume all the expressions will be constant. */
5004 *non_constant_p = false;
5006 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5007 return error_mark_node;
5009 /* Within a parenthesized expression, a `>' token is always
5010 the greater-than operator. */
5011 saved_greater_than_is_operator_p
5012 = parser->greater_than_is_operator_p;
5013 parser->greater_than_is_operator_p = true;
5015 /* Consume expressions until there are no more. */
5016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5021 /* At the beginning of attribute lists, check to see if the
5022 next token is an identifier. */
5023 if (is_attribute_list
5024 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5028 /* Consume the identifier. */
5029 token = cp_lexer_consume_token (parser->lexer);
5030 /* Save the identifier. */
5031 identifier = token->u.value;
5035 /* Parse the next assignment-expression. */
5038 bool expr_non_constant_p;
5039 expr = (cp_parser_constant_expression
5040 (parser, /*allow_non_constant_p=*/true,
5041 &expr_non_constant_p));
5042 if (expr_non_constant_p)
5043 *non_constant_p = true;
5046 expr = cp_parser_assignment_expression (parser, cast_p);
5049 expr = fold_non_dependent_expr (expr);
5051 /* If we have an ellipsis, then this is an expression
5053 if (allow_expansion_p
5054 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5056 /* Consume the `...'. */
5057 cp_lexer_consume_token (parser->lexer);
5059 /* Build the argument pack. */
5060 expr = make_pack_expansion (expr);
5063 /* Add it to the list. We add error_mark_node
5064 expressions to the list, so that we can still tell if
5065 the correct form for a parenthesized expression-list
5066 is found. That gives better errors. */
5067 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5069 if (expr == error_mark_node)
5073 /* After the first item, attribute lists look the same as
5074 expression lists. */
5075 is_attribute_list = false;
5078 /* If the next token isn't a `,', then we are done. */
5079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5082 /* Otherwise, consume the `,' and keep going. */
5083 cp_lexer_consume_token (parser->lexer);
5086 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5091 /* We try and resync to an unnested comma, as that will give the
5092 user better diagnostics. */
5093 ending = cp_parser_skip_to_closing_parenthesis (parser,
5094 /*recovering=*/true,
5096 /*consume_paren=*/true);
5101 parser->greater_than_is_operator_p
5102 = saved_greater_than_is_operator_p;
5103 return error_mark_node;
5107 parser->greater_than_is_operator_p
5108 = saved_greater_than_is_operator_p;
5110 /* We built up the list in reverse order so we must reverse it now. */
5111 expression_list = nreverse (expression_list);
5113 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5115 return expression_list;
5118 /* Parse a pseudo-destructor-name.
5120 pseudo-destructor-name:
5121 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5122 :: [opt] nested-name-specifier template template-id :: ~ type-name
5123 :: [opt] nested-name-specifier [opt] ~ type-name
5125 If either of the first two productions is used, sets *SCOPE to the
5126 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5127 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5128 or ERROR_MARK_NODE if the parse fails. */
5131 cp_parser_pseudo_destructor_name (cp_parser* parser,
5135 bool nested_name_specifier_p;
5137 /* Assume that things will not work out. */
5138 *type = error_mark_node;
5140 /* Look for the optional `::' operator. */
5141 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5142 /* Look for the optional nested-name-specifier. */
5143 nested_name_specifier_p
5144 = (cp_parser_nested_name_specifier_opt (parser,
5145 /*typename_keyword_p=*/false,
5146 /*check_dependency_p=*/true,
5148 /*is_declaration=*/true)
5150 /* Now, if we saw a nested-name-specifier, we might be doing the
5151 second production. */
5152 if (nested_name_specifier_p
5153 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5155 /* Consume the `template' keyword. */
5156 cp_lexer_consume_token (parser->lexer);
5157 /* Parse the template-id. */
5158 cp_parser_template_id (parser,
5159 /*template_keyword_p=*/true,
5160 /*check_dependency_p=*/false,
5161 /*is_declaration=*/true);
5162 /* Look for the `::' token. */
5163 cp_parser_require (parser, CPP_SCOPE, "`::'");
5165 /* If the next token is not a `~', then there might be some
5166 additional qualification. */
5167 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5169 /* At this point, we're looking for "type-name :: ~". The type-name
5170 must not be a class-name, since this is a pseudo-destructor. So,
5171 it must be either an enum-name, or a typedef-name -- both of which
5172 are just identifiers. So, we peek ahead to check that the "::"
5173 and "~" tokens are present; if they are not, then we can avoid
5174 calling type_name. */
5175 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5176 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5177 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5179 cp_parser_error (parser, "non-scalar type");
5183 /* Look for the type-name. */
5184 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5185 if (*scope == error_mark_node)
5188 /* Look for the `::' token. */
5189 cp_parser_require (parser, CPP_SCOPE, "`::'");
5194 /* Look for the `~'. */
5195 cp_parser_require (parser, CPP_COMPL, "`~'");
5196 /* Look for the type-name again. We are not responsible for
5197 checking that it matches the first type-name. */
5198 *type = cp_parser_nonclass_name (parser);
5201 /* Parse a unary-expression.
5207 unary-operator cast-expression
5208 sizeof unary-expression
5216 __extension__ cast-expression
5217 __alignof__ unary-expression
5218 __alignof__ ( type-id )
5219 __real__ cast-expression
5220 __imag__ cast-expression
5223 ADDRESS_P is true iff the unary-expression is appearing as the
5224 operand of the `&' operator. CAST_P is true if this expression is
5225 the target of a cast.
5227 Returns a representation of the expression. */
5230 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5233 enum tree_code unary_operator;
5235 /* Peek at the next token. */
5236 token = cp_lexer_peek_token (parser->lexer);
5237 /* Some keywords give away the kind of expression. */
5238 if (token->type == CPP_KEYWORD)
5240 enum rid keyword = token->keyword;
5250 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5251 /* Consume the token. */
5252 cp_lexer_consume_token (parser->lexer);
5253 /* Parse the operand. */
5254 operand = cp_parser_sizeof_operand (parser, keyword);
5256 if (TYPE_P (operand))
5257 return cxx_sizeof_or_alignof_type (operand, op, true);
5259 return cxx_sizeof_or_alignof_expr (operand, op, true);
5263 return cp_parser_new_expression (parser);
5266 return cp_parser_delete_expression (parser);
5270 /* The saved value of the PEDANTIC flag. */
5274 /* Save away the PEDANTIC flag. */
5275 cp_parser_extension_opt (parser, &saved_pedantic);
5276 /* Parse the cast-expression. */
5277 expr = cp_parser_simple_cast_expression (parser);
5278 /* Restore the PEDANTIC flag. */
5279 pedantic = saved_pedantic;
5289 /* Consume the `__real__' or `__imag__' token. */
5290 cp_lexer_consume_token (parser->lexer);
5291 /* Parse the cast-expression. */
5292 expression = cp_parser_simple_cast_expression (parser);
5293 /* Create the complete representation. */
5294 return build_x_unary_op ((keyword == RID_REALPART
5295 ? REALPART_EXPR : IMAGPART_EXPR),
5297 tf_warning_or_error);
5306 /* Look for the `:: new' and `:: delete', which also signal the
5307 beginning of a new-expression, or delete-expression,
5308 respectively. If the next token is `::', then it might be one of
5310 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5314 /* See if the token after the `::' is one of the keywords in
5315 which we're interested. */
5316 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5317 /* If it's `new', we have a new-expression. */
5318 if (keyword == RID_NEW)
5319 return cp_parser_new_expression (parser);
5320 /* Similarly, for `delete'. */
5321 else if (keyword == RID_DELETE)
5322 return cp_parser_delete_expression (parser);
5325 /* Look for a unary operator. */
5326 unary_operator = cp_parser_unary_operator (token);
5327 /* The `++' and `--' operators can be handled similarly, even though
5328 they are not technically unary-operators in the grammar. */
5329 if (unary_operator == ERROR_MARK)
5331 if (token->type == CPP_PLUS_PLUS)
5332 unary_operator = PREINCREMENT_EXPR;
5333 else if (token->type == CPP_MINUS_MINUS)
5334 unary_operator = PREDECREMENT_EXPR;
5335 /* Handle the GNU address-of-label extension. */
5336 else if (cp_parser_allow_gnu_extensions_p (parser)
5337 && token->type == CPP_AND_AND)
5342 /* Consume the '&&' token. */
5343 cp_lexer_consume_token (parser->lexer);
5344 /* Look for the identifier. */
5345 identifier = cp_parser_identifier (parser);
5346 /* Create an expression representing the address. */
5347 expression = finish_label_address_expr (identifier);
5348 if (cp_parser_non_integral_constant_expression (parser,
5349 "the address of a label"))
5350 expression = error_mark_node;
5354 if (unary_operator != ERROR_MARK)
5356 tree cast_expression;
5357 tree expression = error_mark_node;
5358 const char *non_constant_p = NULL;
5360 /* Consume the operator token. */
5361 token = cp_lexer_consume_token (parser->lexer);
5362 /* Parse the cast-expression. */
5364 = cp_parser_cast_expression (parser,
5365 unary_operator == ADDR_EXPR,
5367 /* Now, build an appropriate representation. */
5368 switch (unary_operator)
5371 non_constant_p = "`*'";
5372 expression = build_x_indirect_ref (cast_expression, "unary *",
5373 tf_warning_or_error);
5377 non_constant_p = "`&'";
5380 expression = build_x_unary_op (unary_operator, cast_expression,
5381 tf_warning_or_error);
5384 case PREINCREMENT_EXPR:
5385 case PREDECREMENT_EXPR:
5386 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5389 case UNARY_PLUS_EXPR:
5391 case TRUTH_NOT_EXPR:
5392 expression = finish_unary_op_expr (unary_operator, cast_expression);
5400 && cp_parser_non_integral_constant_expression (parser,
5402 expression = error_mark_node;
5407 return cp_parser_postfix_expression (parser, address_p, cast_p,
5408 /*member_access_only_p=*/false);
5411 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5412 unary-operator, the corresponding tree code is returned. */
5414 static enum tree_code
5415 cp_parser_unary_operator (cp_token* token)
5417 switch (token->type)
5420 return INDIRECT_REF;
5426 return UNARY_PLUS_EXPR;
5432 return TRUTH_NOT_EXPR;
5435 return BIT_NOT_EXPR;
5442 /* Parse a new-expression.
5445 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5446 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5448 Returns a representation of the expression. */
5451 cp_parser_new_expression (cp_parser* parser)
5453 bool global_scope_p;
5459 /* Look for the optional `::' operator. */
5461 = (cp_parser_global_scope_opt (parser,
5462 /*current_scope_valid_p=*/false)
5464 /* Look for the `new' operator. */
5465 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5466 /* There's no easy way to tell a new-placement from the
5467 `( type-id )' construct. */
5468 cp_parser_parse_tentatively (parser);
5469 /* Look for a new-placement. */
5470 placement = cp_parser_new_placement (parser);
5471 /* If that didn't work out, there's no new-placement. */
5472 if (!cp_parser_parse_definitely (parser))
5473 placement = NULL_TREE;
5475 /* If the next token is a `(', then we have a parenthesized
5477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5479 /* Consume the `('. */
5480 cp_lexer_consume_token (parser->lexer);
5481 /* Parse the type-id. */
5482 type = cp_parser_type_id (parser);
5483 /* Look for the closing `)'. */
5484 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5485 /* There should not be a direct-new-declarator in this production,
5486 but GCC used to allowed this, so we check and emit a sensible error
5487 message for this case. */
5488 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5490 error ("array bound forbidden after parenthesized type-id");
5491 inform ("try removing the parentheses around the type-id");
5492 cp_parser_direct_new_declarator (parser);
5496 /* Otherwise, there must be a new-type-id. */
5498 type = cp_parser_new_type_id (parser, &nelts);
5500 /* If the next token is a `(', then we have a new-initializer. */
5501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5502 initializer = cp_parser_new_initializer (parser);
5504 initializer = NULL_TREE;
5506 /* A new-expression may not appear in an integral constant
5508 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5509 return error_mark_node;
5511 /* Create a representation of the new-expression. */
5512 return build_new (placement, type, nelts, initializer, global_scope_p,
5513 tf_warning_or_error);
5516 /* Parse a new-placement.
5521 Returns the same representation as for an expression-list. */
5524 cp_parser_new_placement (cp_parser* parser)
5526 tree expression_list;
5528 /* Parse the expression-list. */
5529 expression_list = (cp_parser_parenthesized_expression_list
5530 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5531 /*non_constant_p=*/NULL));
5533 return expression_list;
5536 /* Parse a new-type-id.
5539 type-specifier-seq new-declarator [opt]
5541 Returns the TYPE allocated. If the new-type-id indicates an array
5542 type, *NELTS is set to the number of elements in the last array
5543 bound; the TYPE will not include the last array bound. */
5546 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5548 cp_decl_specifier_seq type_specifier_seq;
5549 cp_declarator *new_declarator;
5550 cp_declarator *declarator;
5551 cp_declarator *outer_declarator;
5552 const char *saved_message;
5555 /* The type-specifier sequence must not contain type definitions.
5556 (It cannot contain declarations of new types either, but if they
5557 are not definitions we will catch that because they are not
5559 saved_message = parser->type_definition_forbidden_message;
5560 parser->type_definition_forbidden_message
5561 = "types may not be defined in a new-type-id";
5562 /* Parse the type-specifier-seq. */
5563 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5564 &type_specifier_seq);
5565 /* Restore the old message. */
5566 parser->type_definition_forbidden_message = saved_message;
5567 /* Parse the new-declarator. */
5568 new_declarator = cp_parser_new_declarator_opt (parser);
5570 /* Determine the number of elements in the last array dimension, if
5573 /* Skip down to the last array dimension. */
5574 declarator = new_declarator;
5575 outer_declarator = NULL;
5576 while (declarator && (declarator->kind == cdk_pointer
5577 || declarator->kind == cdk_ptrmem))
5579 outer_declarator = declarator;
5580 declarator = declarator->declarator;
5583 && declarator->kind == cdk_array
5584 && declarator->declarator
5585 && declarator->declarator->kind == cdk_array)
5587 outer_declarator = declarator;
5588 declarator = declarator->declarator;
5591 if (declarator && declarator->kind == cdk_array)
5593 *nelts = declarator->u.array.bounds;
5594 if (*nelts == error_mark_node)
5595 *nelts = integer_one_node;
5597 if (outer_declarator)
5598 outer_declarator->declarator = declarator->declarator;
5600 new_declarator = NULL;
5603 type = groktypename (&type_specifier_seq, new_declarator);
5607 /* Parse an (optional) new-declarator.
5610 ptr-operator new-declarator [opt]
5611 direct-new-declarator
5613 Returns the declarator. */
5615 static cp_declarator *
5616 cp_parser_new_declarator_opt (cp_parser* parser)
5618 enum tree_code code;
5620 cp_cv_quals cv_quals;
5622 /* We don't know if there's a ptr-operator next, or not. */
5623 cp_parser_parse_tentatively (parser);
5624 /* Look for a ptr-operator. */
5625 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5626 /* If that worked, look for more new-declarators. */
5627 if (cp_parser_parse_definitely (parser))
5629 cp_declarator *declarator;
5631 /* Parse another optional declarator. */
5632 declarator = cp_parser_new_declarator_opt (parser);
5634 return cp_parser_make_indirect_declarator
5635 (code, type, cv_quals, declarator);
5638 /* If the next token is a `[', there is a direct-new-declarator. */
5639 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5640 return cp_parser_direct_new_declarator (parser);
5645 /* Parse a direct-new-declarator.
5647 direct-new-declarator:
5649 direct-new-declarator [constant-expression]
5653 static cp_declarator *
5654 cp_parser_direct_new_declarator (cp_parser* parser)
5656 cp_declarator *declarator = NULL;
5662 /* Look for the opening `['. */
5663 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5664 /* The first expression is not required to be constant. */
5667 expression = cp_parser_expression (parser, /*cast_p=*/false);
5668 /* The standard requires that the expression have integral
5669 type. DR 74 adds enumeration types. We believe that the
5670 real intent is that these expressions be handled like the
5671 expression in a `switch' condition, which also allows
5672 classes with a single conversion to integral or
5673 enumeration type. */
5674 if (!processing_template_decl)
5677 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5682 error ("expression in new-declarator must have integral "
5683 "or enumeration type");
5684 expression = error_mark_node;
5688 /* But all the other expressions must be. */
5691 = cp_parser_constant_expression (parser,
5692 /*allow_non_constant=*/false,
5694 /* Look for the closing `]'. */
5695 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5697 /* Add this bound to the declarator. */
5698 declarator = make_array_declarator (declarator, expression);
5700 /* If the next token is not a `[', then there are no more
5702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5709 /* Parse a new-initializer.
5712 ( expression-list [opt] )
5714 Returns a representation of the expression-list. If there is no
5715 expression-list, VOID_ZERO_NODE is returned. */
5718 cp_parser_new_initializer (cp_parser* parser)
5720 tree expression_list;
5722 expression_list = (cp_parser_parenthesized_expression_list
5723 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5724 /*non_constant_p=*/NULL));
5725 if (!expression_list)
5726 expression_list = void_zero_node;
5728 return expression_list;
5731 /* Parse a delete-expression.
5734 :: [opt] delete cast-expression
5735 :: [opt] delete [ ] cast-expression
5737 Returns a representation of the expression. */
5740 cp_parser_delete_expression (cp_parser* parser)
5742 bool global_scope_p;
5746 /* Look for the optional `::' operator. */
5748 = (cp_parser_global_scope_opt (parser,
5749 /*current_scope_valid_p=*/false)
5751 /* Look for the `delete' keyword. */
5752 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5753 /* See if the array syntax is in use. */
5754 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5756 /* Consume the `[' token. */
5757 cp_lexer_consume_token (parser->lexer);
5758 /* Look for the `]' token. */
5759 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5760 /* Remember that this is the `[]' construct. */
5766 /* Parse the cast-expression. */
5767 expression = cp_parser_simple_cast_expression (parser);
5769 /* A delete-expression may not appear in an integral constant
5771 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5772 return error_mark_node;
5774 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5777 /* Parse a cast-expression.
5781 ( type-id ) cast-expression
5783 ADDRESS_P is true iff the unary-expression is appearing as the
5784 operand of the `&' operator. CAST_P is true if this expression is
5785 the target of a cast.
5787 Returns a representation of the expression. */
5790 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5792 /* If it's a `(', then we might be looking at a cast. */
5793 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5795 tree type = NULL_TREE;
5796 tree expr = NULL_TREE;
5797 bool compound_literal_p;
5798 const char *saved_message;
5800 /* There's no way to know yet whether or not this is a cast.
5801 For example, `(int (3))' is a unary-expression, while `(int)
5802 3' is a cast. So, we resort to parsing tentatively. */
5803 cp_parser_parse_tentatively (parser);
5804 /* Types may not be defined in a cast. */
5805 saved_message = parser->type_definition_forbidden_message;
5806 parser->type_definition_forbidden_message
5807 = "types may not be defined in casts";
5808 /* Consume the `('. */
5809 cp_lexer_consume_token (parser->lexer);
5810 /* A very tricky bit is that `(struct S) { 3 }' is a
5811 compound-literal (which we permit in C++ as an extension).
5812 But, that construct is not a cast-expression -- it is a
5813 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5814 is legal; if the compound-literal were a cast-expression,
5815 you'd need an extra set of parentheses.) But, if we parse
5816 the type-id, and it happens to be a class-specifier, then we
5817 will commit to the parse at that point, because we cannot
5818 undo the action that is done when creating a new class. So,
5819 then we cannot back up and do a postfix-expression.
5821 Therefore, we scan ahead to the closing `)', and check to see
5822 if the token after the `)' is a `{'. If so, we are not
5823 looking at a cast-expression.
5825 Save tokens so that we can put them back. */
5826 cp_lexer_save_tokens (parser->lexer);
5827 /* Skip tokens until the next token is a closing parenthesis.
5828 If we find the closing `)', and the next token is a `{', then
5829 we are looking at a compound-literal. */
5831 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5832 /*consume_paren=*/true)
5833 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5834 /* Roll back the tokens we skipped. */
5835 cp_lexer_rollback_tokens (parser->lexer);
5836 /* If we were looking at a compound-literal, simulate an error
5837 so that the call to cp_parser_parse_definitely below will
5839 if (compound_literal_p)
5840 cp_parser_simulate_error (parser);
5843 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5844 parser->in_type_id_in_expr_p = true;
5845 /* Look for the type-id. */
5846 type = cp_parser_type_id (parser);
5847 /* Look for the closing `)'. */
5848 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5849 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5852 /* Restore the saved message. */
5853 parser->type_definition_forbidden_message = saved_message;
5855 /* If ok so far, parse the dependent expression. We cannot be
5856 sure it is a cast. Consider `(T ())'. It is a parenthesized
5857 ctor of T, but looks like a cast to function returning T
5858 without a dependent expression. */
5859 if (!cp_parser_error_occurred (parser))
5860 expr = cp_parser_cast_expression (parser,
5861 /*address_p=*/false,
5864 if (cp_parser_parse_definitely (parser))
5866 /* Warn about old-style casts, if so requested. */
5867 if (warn_old_style_cast
5868 && !in_system_header
5869 && !VOID_TYPE_P (type)
5870 && current_lang_name != lang_name_c)
5871 warning (OPT_Wold_style_cast, "use of old-style cast");
5873 /* Only type conversions to integral or enumeration types
5874 can be used in constant-expressions. */
5875 if (!cast_valid_in_integral_constant_expression_p (type)
5876 && (cp_parser_non_integral_constant_expression
5878 "a cast to a type other than an integral or "
5879 "enumeration type")))
5880 return error_mark_node;
5882 /* Perform the cast. */
5883 expr = build_c_cast (type, expr);
5888 /* If we get here, then it's not a cast, so it must be a
5889 unary-expression. */
5890 return cp_parser_unary_expression (parser, address_p, cast_p);
5893 /* Parse a binary expression of the general form:
5897 pm-expression .* cast-expression
5898 pm-expression ->* cast-expression
5900 multiplicative-expression:
5902 multiplicative-expression * pm-expression
5903 multiplicative-expression / pm-expression
5904 multiplicative-expression % pm-expression
5906 additive-expression:
5907 multiplicative-expression
5908 additive-expression + multiplicative-expression
5909 additive-expression - multiplicative-expression
5913 shift-expression << additive-expression
5914 shift-expression >> additive-expression
5916 relational-expression:
5918 relational-expression < shift-expression
5919 relational-expression > shift-expression
5920 relational-expression <= shift-expression
5921 relational-expression >= shift-expression
5925 relational-expression:
5926 relational-expression <? shift-expression
5927 relational-expression >? shift-expression
5929 equality-expression:
5930 relational-expression
5931 equality-expression == relational-expression
5932 equality-expression != relational-expression
5936 and-expression & equality-expression
5938 exclusive-or-expression:
5940 exclusive-or-expression ^ and-expression
5942 inclusive-or-expression:
5943 exclusive-or-expression
5944 inclusive-or-expression | exclusive-or-expression
5946 logical-and-expression:
5947 inclusive-or-expression
5948 logical-and-expression && inclusive-or-expression
5950 logical-or-expression:
5951 logical-and-expression
5952 logical-or-expression || logical-and-expression
5954 All these are implemented with a single function like:
5957 simple-cast-expression
5958 binary-expression <token> binary-expression
5960 CAST_P is true if this expression is the target of a cast.
5962 The binops_by_token map is used to get the tree codes for each <token> type.
5963 binary-expressions are associated according to a precedence table. */
5965 #define TOKEN_PRECEDENCE(token) \
5966 (((token->type == CPP_GREATER \
5967 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5968 && !parser->greater_than_is_operator_p) \
5969 ? PREC_NOT_OPERATOR \
5970 : binops_by_token[token->type].prec)
5973 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5975 cp_parser_expression_stack stack;
5976 cp_parser_expression_stack_entry *sp = &stack[0];
5979 enum tree_code tree_type, lhs_type, rhs_type;
5980 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5983 /* Parse the first expression. */
5984 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5985 lhs_type = ERROR_MARK;
5989 /* Get an operator token. */
5990 token = cp_lexer_peek_token (parser->lexer);
5992 if (warn_cxx0x_compat
5993 && token->type == CPP_RSHIFT
5994 && !parser->greater_than_is_operator_p)
5996 warning (OPT_Wc__0x_compat,
5997 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5999 warning (OPT_Wc__0x_compat,
6000 "suggest parentheses around %<>>%> expression");
6003 new_prec = TOKEN_PRECEDENCE (token);
6005 /* Popping an entry off the stack means we completed a subexpression:
6006 - either we found a token which is not an operator (`>' where it is not
6007 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6008 will happen repeatedly;
6009 - or, we found an operator which has lower priority. This is the case
6010 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6012 if (new_prec <= prec)
6021 tree_type = binops_by_token[token->type].tree_type;
6023 /* We used the operator token. */
6024 cp_lexer_consume_token (parser->lexer);
6026 /* Extract another operand. It may be the RHS of this expression
6027 or the LHS of a new, higher priority expression. */
6028 rhs = cp_parser_simple_cast_expression (parser);
6029 rhs_type = ERROR_MARK;
6031 /* Get another operator token. Look up its precedence to avoid
6032 building a useless (immediately popped) stack entry for common
6033 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6034 token = cp_lexer_peek_token (parser->lexer);
6035 lookahead_prec = TOKEN_PRECEDENCE (token);
6036 if (lookahead_prec > new_prec)
6038 /* ... and prepare to parse the RHS of the new, higher priority
6039 expression. Since precedence levels on the stack are
6040 monotonically increasing, we do not have to care about
6043 sp->tree_type = tree_type;
6045 sp->lhs_type = lhs_type;
6048 lhs_type = rhs_type;
6050 new_prec = lookahead_prec;
6054 /* If the stack is not empty, we have parsed into LHS the right side
6055 (`4' in the example above) of an expression we had suspended.
6056 We can use the information on the stack to recover the LHS (`3')
6057 from the stack together with the tree code (`MULT_EXPR'), and
6058 the precedence of the higher level subexpression
6059 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6060 which will be used to actually build the additive expression. */
6063 tree_type = sp->tree_type;
6065 rhs_type = lhs_type;
6067 lhs_type = sp->lhs_type;
6070 overloaded_p = false;
6071 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6072 &overloaded_p, tf_warning_or_error);
6073 lhs_type = tree_type;
6075 /* If the binary operator required the use of an overloaded operator,
6076 then this expression cannot be an integral constant-expression.
6077 An overloaded operator can be used even if both operands are
6078 otherwise permissible in an integral constant-expression if at
6079 least one of the operands is of enumeration type. */
6082 && (cp_parser_non_integral_constant_expression
6083 (parser, "calls to overloaded operators")))
6084 return error_mark_node;
6091 /* Parse the `? expression : assignment-expression' part of a
6092 conditional-expression. The LOGICAL_OR_EXPR is the
6093 logical-or-expression that started the conditional-expression.
6094 Returns a representation of the entire conditional-expression.
6096 This routine is used by cp_parser_assignment_expression.
6098 ? expression : assignment-expression
6102 ? : assignment-expression */
6105 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6108 tree assignment_expr;
6110 /* Consume the `?' token. */
6111 cp_lexer_consume_token (parser->lexer);
6112 if (cp_parser_allow_gnu_extensions_p (parser)
6113 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6114 /* Implicit true clause. */
6117 /* Parse the expression. */
6118 expr = cp_parser_expression (parser, /*cast_p=*/false);
6120 /* The next token should be a `:'. */
6121 cp_parser_require (parser, CPP_COLON, "`:'");
6122 /* Parse the assignment-expression. */
6123 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6125 /* Build the conditional-expression. */
6126 return build_x_conditional_expr (logical_or_expr,
6129 tf_warning_or_error);
6132 /* Parse an assignment-expression.
6134 assignment-expression:
6135 conditional-expression
6136 logical-or-expression assignment-operator assignment_expression
6139 CAST_P is true if this expression is the target of a cast.
6141 Returns a representation for the expression. */
6144 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6148 /* If the next token is the `throw' keyword, then we're looking at
6149 a throw-expression. */
6150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6151 expr = cp_parser_throw_expression (parser);
6152 /* Otherwise, it must be that we are looking at a
6153 logical-or-expression. */
6156 /* Parse the binary expressions (logical-or-expression). */
6157 expr = cp_parser_binary_expression (parser, cast_p);
6158 /* If the next token is a `?' then we're actually looking at a
6159 conditional-expression. */
6160 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6161 return cp_parser_question_colon_clause (parser, expr);
6164 enum tree_code assignment_operator;
6166 /* If it's an assignment-operator, we're using the second
6169 = cp_parser_assignment_operator_opt (parser);
6170 if (assignment_operator != ERROR_MARK)
6174 /* Parse the right-hand side of the assignment. */
6175 rhs = cp_parser_assignment_expression (parser, cast_p);
6176 /* An assignment may not appear in a
6177 constant-expression. */
6178 if (cp_parser_non_integral_constant_expression (parser,
6180 return error_mark_node;
6181 /* Build the assignment expression. */
6182 expr = build_x_modify_expr (expr,
6183 assignment_operator,
6185 tf_warning_or_error);
6193 /* Parse an (optional) assignment-operator.
6195 assignment-operator: one of
6196 = *= /= %= += -= >>= <<= &= ^= |=
6200 assignment-operator: one of
6203 If the next token is an assignment operator, the corresponding tree
6204 code is returned, and the token is consumed. For example, for
6205 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6206 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6207 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6208 operator, ERROR_MARK is returned. */
6210 static enum tree_code
6211 cp_parser_assignment_operator_opt (cp_parser* parser)
6216 /* Peek at the next toen. */
6217 token = cp_lexer_peek_token (parser->lexer);
6219 switch (token->type)
6230 op = TRUNC_DIV_EXPR;
6234 op = TRUNC_MOD_EXPR;
6266 /* Nothing else is an assignment operator. */
6270 /* If it was an assignment operator, consume it. */
6271 if (op != ERROR_MARK)
6272 cp_lexer_consume_token (parser->lexer);
6277 /* Parse an expression.
6280 assignment-expression
6281 expression , assignment-expression
6283 CAST_P is true if this expression is the target of a cast.
6285 Returns a representation of the expression. */
6288 cp_parser_expression (cp_parser* parser, bool cast_p)
6290 tree expression = NULL_TREE;
6294 tree assignment_expression;
6296 /* Parse the next assignment-expression. */
6297 assignment_expression
6298 = cp_parser_assignment_expression (parser, cast_p);
6299 /* If this is the first assignment-expression, we can just
6302 expression = assignment_expression;
6304 expression = build_x_compound_expr (expression,
6305 assignment_expression,
6306 tf_warning_or_error);
6307 /* If the next token is not a comma, then we are done with the
6309 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6311 /* Consume the `,'. */
6312 cp_lexer_consume_token (parser->lexer);
6313 /* A comma operator cannot appear in a constant-expression. */
6314 if (cp_parser_non_integral_constant_expression (parser,
6315 "a comma operator"))
6316 expression = error_mark_node;
6322 /* Parse a constant-expression.
6324 constant-expression:
6325 conditional-expression
6327 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6328 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6329 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6330 is false, NON_CONSTANT_P should be NULL. */
6333 cp_parser_constant_expression (cp_parser* parser,
6334 bool allow_non_constant_p,
6335 bool *non_constant_p)
6337 bool saved_integral_constant_expression_p;
6338 bool saved_allow_non_integral_constant_expression_p;
6339 bool saved_non_integral_constant_expression_p;
6342 /* It might seem that we could simply parse the
6343 conditional-expression, and then check to see if it were
6344 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6345 one that the compiler can figure out is constant, possibly after
6346 doing some simplifications or optimizations. The standard has a
6347 precise definition of constant-expression, and we must honor
6348 that, even though it is somewhat more restrictive.
6354 is not a legal declaration, because `(2, 3)' is not a
6355 constant-expression. The `,' operator is forbidden in a
6356 constant-expression. However, GCC's constant-folding machinery
6357 will fold this operation to an INTEGER_CST for `3'. */
6359 /* Save the old settings. */
6360 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6361 saved_allow_non_integral_constant_expression_p
6362 = parser->allow_non_integral_constant_expression_p;
6363 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6364 /* We are now parsing a constant-expression. */
6365 parser->integral_constant_expression_p = true;
6366 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6367 parser->non_integral_constant_expression_p = false;
6368 /* Although the grammar says "conditional-expression", we parse an
6369 "assignment-expression", which also permits "throw-expression"
6370 and the use of assignment operators. In the case that
6371 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6372 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6373 actually essential that we look for an assignment-expression.
6374 For example, cp_parser_initializer_clauses uses this function to
6375 determine whether a particular assignment-expression is in fact
6377 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6378 /* Restore the old settings. */
6379 parser->integral_constant_expression_p
6380 = saved_integral_constant_expression_p;
6381 parser->allow_non_integral_constant_expression_p
6382 = saved_allow_non_integral_constant_expression_p;
6383 if (allow_non_constant_p)
6384 *non_constant_p = parser->non_integral_constant_expression_p;
6385 else if (parser->non_integral_constant_expression_p)
6386 expression = error_mark_node;
6387 parser->non_integral_constant_expression_p
6388 = saved_non_integral_constant_expression_p;
6393 /* Parse __builtin_offsetof.
6395 offsetof-expression:
6396 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6398 offsetof-member-designator:
6400 | offsetof-member-designator "." id-expression
6401 | offsetof-member-designator "[" expression "]" */
6404 cp_parser_builtin_offsetof (cp_parser *parser)
6406 int save_ice_p, save_non_ice_p;
6410 /* We're about to accept non-integral-constant things, but will
6411 definitely yield an integral constant expression. Save and
6412 restore these values around our local parsing. */
6413 save_ice_p = parser->integral_constant_expression_p;
6414 save_non_ice_p = parser->non_integral_constant_expression_p;
6416 /* Consume the "__builtin_offsetof" token. */
6417 cp_lexer_consume_token (parser->lexer);
6418 /* Consume the opening `('. */
6419 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6420 /* Parse the type-id. */
6421 type = cp_parser_type_id (parser);
6422 /* Look for the `,'. */
6423 cp_parser_require (parser, CPP_COMMA, "`,'");
6425 /* Build the (type *)null that begins the traditional offsetof macro. */
6426 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6427 tf_warning_or_error);
6429 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6430 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6434 cp_token *token = cp_lexer_peek_token (parser->lexer);
6435 switch (token->type)
6437 case CPP_OPEN_SQUARE:
6438 /* offsetof-member-designator "[" expression "]" */
6439 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6443 /* offsetof-member-designator "." identifier */
6444 cp_lexer_consume_token (parser->lexer);
6445 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6449 case CPP_CLOSE_PAREN:
6450 /* Consume the ")" token. */
6451 cp_lexer_consume_token (parser->lexer);
6455 /* Error. We know the following require will fail, but
6456 that gives the proper error message. */
6457 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6458 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6459 expr = error_mark_node;
6465 /* If we're processing a template, we can't finish the semantics yet.
6466 Otherwise we can fold the entire expression now. */
6467 if (processing_template_decl)
6468 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6470 expr = finish_offsetof (expr);
6473 parser->integral_constant_expression_p = save_ice_p;
6474 parser->non_integral_constant_expression_p = save_non_ice_p;
6479 /* Parse a trait expression. */
6482 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6485 tree type1, type2 = NULL_TREE;
6486 bool binary = false;
6487 cp_decl_specifier_seq decl_specs;
6491 case RID_HAS_NOTHROW_ASSIGN:
6492 kind = CPTK_HAS_NOTHROW_ASSIGN;
6494 case RID_HAS_NOTHROW_CONSTRUCTOR:
6495 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6497 case RID_HAS_NOTHROW_COPY:
6498 kind = CPTK_HAS_NOTHROW_COPY;
6500 case RID_HAS_TRIVIAL_ASSIGN:
6501 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6503 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6504 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6506 case RID_HAS_TRIVIAL_COPY:
6507 kind = CPTK_HAS_TRIVIAL_COPY;
6509 case RID_HAS_TRIVIAL_DESTRUCTOR:
6510 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6512 case RID_HAS_VIRTUAL_DESTRUCTOR:
6513 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6515 case RID_IS_ABSTRACT:
6516 kind = CPTK_IS_ABSTRACT;
6518 case RID_IS_BASE_OF:
6519 kind = CPTK_IS_BASE_OF;
6523 kind = CPTK_IS_CLASS;
6525 case RID_IS_CONVERTIBLE_TO:
6526 kind = CPTK_IS_CONVERTIBLE_TO;
6530 kind = CPTK_IS_EMPTY;
6533 kind = CPTK_IS_ENUM;
6538 case RID_IS_POLYMORPHIC:
6539 kind = CPTK_IS_POLYMORPHIC;
6542 kind = CPTK_IS_UNION;
6548 /* Consume the token. */
6549 cp_lexer_consume_token (parser->lexer);
6551 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6553 type1 = cp_parser_type_id (parser);
6555 if (type1 == error_mark_node)
6556 return error_mark_node;
6558 /* Build a trivial decl-specifier-seq. */
6559 clear_decl_specs (&decl_specs);
6560 decl_specs.type = type1;
6562 /* Call grokdeclarator to figure out what type this is. */
6563 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6564 /*initialized=*/0, /*attrlist=*/NULL);
6568 cp_parser_require (parser, CPP_COMMA, "`,'");
6570 type2 = cp_parser_type_id (parser);
6572 if (type2 == error_mark_node)
6573 return error_mark_node;
6575 /* Build a trivial decl-specifier-seq. */
6576 clear_decl_specs (&decl_specs);
6577 decl_specs.type = type2;
6579 /* Call grokdeclarator to figure out what type this is. */
6580 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6581 /*initialized=*/0, /*attrlist=*/NULL);
6584 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6586 /* Complete the trait expression, which may mean either processing
6587 the trait expr now or saving it for template instantiation. */
6588 return finish_trait_expr (kind, type1, type2);
6591 /* Statements [gram.stmt.stmt] */
6593 /* Parse a statement.
6597 expression-statement
6602 declaration-statement
6605 IN_COMPOUND is true when the statement is nested inside a
6606 cp_parser_compound_statement; this matters for certain pragmas.
6608 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6609 is a (possibly labeled) if statement which is not enclosed in braces
6610 and has an else clause. This is used to implement -Wparentheses. */
6613 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6614 bool in_compound, bool *if_p)
6618 location_t statement_location;
6623 /* There is no statement yet. */
6624 statement = NULL_TREE;
6625 /* Peek at the next token. */
6626 token = cp_lexer_peek_token (parser->lexer);
6627 /* Remember the location of the first token in the statement. */
6628 statement_location = token->location;
6629 /* If this is a keyword, then that will often determine what kind of
6630 statement we have. */
6631 if (token->type == CPP_KEYWORD)
6633 enum rid keyword = token->keyword;
6639 /* Looks like a labeled-statement with a case label.
6640 Parse the label, and then use tail recursion to parse
6642 cp_parser_label_for_labeled_statement (parser);
6647 statement = cp_parser_selection_statement (parser, if_p);
6653 statement = cp_parser_iteration_statement (parser);
6660 statement = cp_parser_jump_statement (parser);
6663 /* Objective-C++ exception-handling constructs. */
6666 case RID_AT_FINALLY:
6667 case RID_AT_SYNCHRONIZED:
6669 statement = cp_parser_objc_statement (parser);
6673 statement = cp_parser_try_block (parser);
6677 /* This must be a namespace alias definition. */
6678 cp_parser_declaration_statement (parser);
6682 /* It might be a keyword like `int' that can start a
6683 declaration-statement. */
6687 else if (token->type == CPP_NAME)
6689 /* If the next token is a `:', then we are looking at a
6690 labeled-statement. */
6691 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6692 if (token->type == CPP_COLON)
6694 /* Looks like a labeled-statement with an ordinary label.
6695 Parse the label, and then use tail recursion to parse
6697 cp_parser_label_for_labeled_statement (parser);
6701 /* Anything that starts with a `{' must be a compound-statement. */
6702 else if (token->type == CPP_OPEN_BRACE)
6703 statement = cp_parser_compound_statement (parser, NULL, false);
6704 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6705 a statement all its own. */
6706 else if (token->type == CPP_PRAGMA)
6708 /* Only certain OpenMP pragmas are attached to statements, and thus
6709 are considered statements themselves. All others are not. In
6710 the context of a compound, accept the pragma as a "statement" and
6711 return so that we can check for a close brace. Otherwise we
6712 require a real statement and must go back and read one. */
6714 cp_parser_pragma (parser, pragma_compound);
6715 else if (!cp_parser_pragma (parser, pragma_stmt))
6719 else if (token->type == CPP_EOF)
6721 cp_parser_error (parser, "expected statement");
6725 /* Everything else must be a declaration-statement or an
6726 expression-statement. Try for the declaration-statement
6727 first, unless we are looking at a `;', in which case we know that
6728 we have an expression-statement. */
6731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6733 cp_parser_parse_tentatively (parser);
6734 /* Try to parse the declaration-statement. */
6735 cp_parser_declaration_statement (parser);
6736 /* If that worked, we're done. */
6737 if (cp_parser_parse_definitely (parser))
6740 /* Look for an expression-statement instead. */
6741 statement = cp_parser_expression_statement (parser, in_statement_expr);
6744 /* Set the line number for the statement. */
6745 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6746 SET_EXPR_LOCATION (statement, statement_location);
6749 /* Parse the label for a labeled-statement, i.e.
6752 case constant-expression :
6756 case constant-expression ... constant-expression : statement
6758 When a label is parsed without errors, the label is added to the
6759 parse tree by the finish_* functions, so this function doesn't
6760 have to return the label. */
6763 cp_parser_label_for_labeled_statement (cp_parser* parser)
6767 /* The next token should be an identifier. */
6768 token = cp_lexer_peek_token (parser->lexer);
6769 if (token->type != CPP_NAME
6770 && token->type != CPP_KEYWORD)
6772 cp_parser_error (parser, "expected labeled-statement");
6776 switch (token->keyword)
6783 /* Consume the `case' token. */
6784 cp_lexer_consume_token (parser->lexer);
6785 /* Parse the constant-expression. */
6786 expr = cp_parser_constant_expression (parser,
6787 /*allow_non_constant_p=*/false,
6790 ellipsis = cp_lexer_peek_token (parser->lexer);
6791 if (ellipsis->type == CPP_ELLIPSIS)
6793 /* Consume the `...' token. */
6794 cp_lexer_consume_token (parser->lexer);
6796 cp_parser_constant_expression (parser,
6797 /*allow_non_constant_p=*/false,
6799 /* We don't need to emit warnings here, as the common code
6800 will do this for us. */
6803 expr_hi = NULL_TREE;
6805 if (parser->in_switch_statement_p)
6806 finish_case_label (expr, expr_hi);
6808 error ("case label %qE not within a switch statement", expr);
6813 /* Consume the `default' token. */
6814 cp_lexer_consume_token (parser->lexer);
6816 if (parser->in_switch_statement_p)
6817 finish_case_label (NULL_TREE, NULL_TREE);
6819 error ("case label not within a switch statement");
6823 /* Anything else must be an ordinary label. */
6824 finish_label_stmt (cp_parser_identifier (parser));
6828 /* Require the `:' token. */
6829 cp_parser_require (parser, CPP_COLON, "`:'");
6832 /* Parse an expression-statement.
6834 expression-statement:
6837 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6838 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6839 indicates whether this expression-statement is part of an
6840 expression statement. */
6843 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6845 tree statement = NULL_TREE;
6847 /* If the next token is a ';', then there is no expression
6849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6850 statement = cp_parser_expression (parser, /*cast_p=*/false);
6852 /* Consume the final `;'. */
6853 cp_parser_consume_semicolon_at_end_of_statement (parser);
6855 if (in_statement_expr
6856 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6857 /* This is the final expression statement of a statement
6859 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6861 statement = finish_expr_stmt (statement);
6868 /* Parse a compound-statement.
6871 { statement-seq [opt] }
6876 { label-declaration-seq [opt] statement-seq [opt] }
6878 label-declaration-seq:
6880 label-declaration-seq label-declaration
6882 Returns a tree representing the statement. */
6885 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6890 /* Consume the `{'. */
6891 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6892 return error_mark_node;
6893 /* Begin the compound-statement. */
6894 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6895 /* If the next keyword is `__label__' we have a label declaration. */
6896 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6897 cp_parser_label_declaration (parser);
6898 /* Parse an (optional) statement-seq. */
6899 cp_parser_statement_seq_opt (parser, in_statement_expr);
6900 /* Finish the compound-statement. */
6901 finish_compound_stmt (compound_stmt);
6902 /* Consume the `}'. */
6903 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6905 return compound_stmt;
6908 /* Parse an (optional) statement-seq.
6912 statement-seq [opt] statement */
6915 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6917 /* Scan statements until there aren't any more. */
6920 cp_token *token = cp_lexer_peek_token (parser->lexer);
6922 /* If we're looking at a `}', then we've run out of statements. */
6923 if (token->type == CPP_CLOSE_BRACE
6924 || token->type == CPP_EOF
6925 || token->type == CPP_PRAGMA_EOL)
6928 /* If we are in a compound statement and find 'else' then
6929 something went wrong. */
6930 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6932 if (parser->in_statement & IN_IF_STMT)
6936 token = cp_lexer_consume_token (parser->lexer);
6937 error ("%<else%> without a previous %<if%>");
6941 /* Parse the statement. */
6942 cp_parser_statement (parser, in_statement_expr, true, NULL);
6946 /* Parse a selection-statement.
6948 selection-statement:
6949 if ( condition ) statement
6950 if ( condition ) statement else statement
6951 switch ( condition ) statement
6953 Returns the new IF_STMT or SWITCH_STMT.
6955 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6956 is a (possibly labeled) if statement which is not enclosed in
6957 braces and has an else clause. This is used to implement
6961 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6969 /* Peek at the next token. */
6970 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6972 /* See what kind of keyword it is. */
6973 keyword = token->keyword;
6982 /* Look for the `('. */
6983 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6985 cp_parser_skip_to_end_of_statement (parser);
6986 return error_mark_node;
6989 /* Begin the selection-statement. */
6990 if (keyword == RID_IF)
6991 statement = begin_if_stmt ();
6993 statement = begin_switch_stmt ();
6995 /* Parse the condition. */
6996 condition = cp_parser_condition (parser);
6997 /* Look for the `)'. */
6998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6999 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7000 /*consume_paren=*/true);
7002 if (keyword == RID_IF)
7005 unsigned char in_statement;
7007 /* Add the condition. */
7008 finish_if_stmt_cond (condition, statement);
7010 /* Parse the then-clause. */
7011 in_statement = parser->in_statement;
7012 parser->in_statement |= IN_IF_STMT;
7013 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7014 parser->in_statement = in_statement;
7016 finish_then_clause (statement);
7018 /* If the next token is `else', parse the else-clause. */
7019 if (cp_lexer_next_token_is_keyword (parser->lexer,
7022 /* Consume the `else' keyword. */
7023 cp_lexer_consume_token (parser->lexer);
7024 begin_else_clause (statement);
7025 /* Parse the else-clause. */
7026 cp_parser_implicitly_scoped_statement (parser, NULL);
7027 finish_else_clause (statement);
7029 /* If we are currently parsing a then-clause, then
7030 IF_P will not be NULL. We set it to true to
7031 indicate that this if statement has an else clause.
7032 This may trigger the Wparentheses warning below
7033 when we get back up to the parent if statement. */
7039 /* This if statement does not have an else clause. If
7040 NESTED_IF is true, then the then-clause is an if
7041 statement which does have an else clause. We warn
7042 about the potential ambiguity. */
7044 warning (OPT_Wparentheses,
7045 ("%Hsuggest explicit braces "
7046 "to avoid ambiguous %<else%>"),
7047 EXPR_LOCUS (statement));
7050 /* Now we're all done with the if-statement. */
7051 finish_if_stmt (statement);
7055 bool in_switch_statement_p;
7056 unsigned char in_statement;
7058 /* Add the condition. */
7059 finish_switch_cond (condition, statement);
7061 /* Parse the body of the switch-statement. */
7062 in_switch_statement_p = parser->in_switch_statement_p;
7063 in_statement = parser->in_statement;
7064 parser->in_switch_statement_p = true;
7065 parser->in_statement |= IN_SWITCH_STMT;
7066 cp_parser_implicitly_scoped_statement (parser, NULL);
7067 parser->in_switch_statement_p = in_switch_statement_p;
7068 parser->in_statement = in_statement;
7070 /* Now we're all done with the switch-statement. */
7071 finish_switch_stmt (statement);
7079 cp_parser_error (parser, "expected selection-statement");
7080 return error_mark_node;
7084 /* Parse a condition.
7088 type-specifier-seq declarator = assignment-expression
7093 type-specifier-seq declarator asm-specification [opt]
7094 attributes [opt] = assignment-expression
7096 Returns the expression that should be tested. */
7099 cp_parser_condition (cp_parser* parser)
7101 cp_decl_specifier_seq type_specifiers;
7102 const char *saved_message;
7104 /* Try the declaration first. */
7105 cp_parser_parse_tentatively (parser);
7106 /* New types are not allowed in the type-specifier-seq for a
7108 saved_message = parser->type_definition_forbidden_message;
7109 parser->type_definition_forbidden_message
7110 = "types may not be defined in conditions";
7111 /* Parse the type-specifier-seq. */
7112 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7114 /* Restore the saved message. */
7115 parser->type_definition_forbidden_message = saved_message;
7116 /* If all is well, we might be looking at a declaration. */
7117 if (!cp_parser_error_occurred (parser))
7120 tree asm_specification;
7122 cp_declarator *declarator;
7123 tree initializer = NULL_TREE;
7125 /* Parse the declarator. */
7126 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7127 /*ctor_dtor_or_conv_p=*/NULL,
7128 /*parenthesized_p=*/NULL,
7129 /*member_p=*/false);
7130 /* Parse the attributes. */
7131 attributes = cp_parser_attributes_opt (parser);
7132 /* Parse the asm-specification. */
7133 asm_specification = cp_parser_asm_specification_opt (parser);
7134 /* If the next token is not an `=', then we might still be
7135 looking at an expression. For example:
7139 looks like a decl-specifier-seq and a declarator -- but then
7140 there is no `=', so this is an expression. */
7141 cp_parser_require (parser, CPP_EQ, "`='");
7142 /* If we did see an `=', then we are looking at a declaration
7144 if (cp_parser_parse_definitely (parser))
7147 bool non_constant_p;
7149 /* Create the declaration. */
7150 decl = start_decl (declarator, &type_specifiers,
7151 /*initialized_p=*/true,
7152 attributes, /*prefix_attributes=*/NULL_TREE,
7154 /* Parse the assignment-expression. */
7156 = cp_parser_constant_expression (parser,
7157 /*allow_non_constant_p=*/true,
7159 if (!non_constant_p)
7160 initializer = fold_non_dependent_expr (initializer);
7162 /* Process the initializer. */
7163 cp_finish_decl (decl,
7164 initializer, !non_constant_p,
7166 LOOKUP_ONLYCONVERTING);
7169 pop_scope (pushed_scope);
7171 return convert_from_reference (decl);
7174 /* If we didn't even get past the declarator successfully, we are
7175 definitely not looking at a declaration. */
7177 cp_parser_abort_tentative_parse (parser);
7179 /* Otherwise, we are looking at an expression. */
7180 return cp_parser_expression (parser, /*cast_p=*/false);
7183 /* We check for a ) immediately followed by ; with no whitespacing
7184 between. This is used to issue a warning for:
7192 as the semicolon is probably extraneous.
7194 On parse errors, the next token might not be a ), so do nothing in
7198 check_empty_body (cp_parser* parser, const char* type)
7201 cp_token *close_paren;
7202 expanded_location close_loc;
7203 expanded_location semi_loc;
7205 close_paren = cp_lexer_peek_token (parser->lexer);
7206 if (close_paren->type != CPP_CLOSE_PAREN)
7209 close_loc = expand_location (close_paren->location);
7210 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7212 if (token->type != CPP_SEMICOLON
7213 || (token->flags & PREV_WHITE))
7216 semi_loc = expand_location (token->location);
7217 if (close_loc.line == semi_loc.line
7218 && close_loc.column+1 == semi_loc.column)
7219 warning (OPT_Wempty_body,
7220 "suggest a space before %<;%> or explicit braces around empty "
7221 "body in %<%s%> statement",
7225 /* Parse an iteration-statement.
7227 iteration-statement:
7228 while ( condition ) statement
7229 do statement while ( expression ) ;
7230 for ( for-init-statement condition [opt] ; expression [opt] )
7233 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7236 cp_parser_iteration_statement (cp_parser* parser)
7241 unsigned char in_statement;
7243 /* Peek at the next token. */
7244 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7246 return error_mark_node;
7248 /* Remember whether or not we are already within an iteration
7250 in_statement = parser->in_statement;
7252 /* See what kind of keyword it is. */
7253 keyword = token->keyword;
7260 /* Begin the while-statement. */
7261 statement = begin_while_stmt ();
7262 /* Look for the `('. */
7263 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7264 /* Parse the condition. */
7265 condition = cp_parser_condition (parser);
7266 finish_while_stmt_cond (condition, statement);
7267 check_empty_body (parser, "while");
7268 /* Look for the `)'. */
7269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7270 /* Parse the dependent statement. */
7271 parser->in_statement = IN_ITERATION_STMT;
7272 cp_parser_already_scoped_statement (parser);
7273 parser->in_statement = in_statement;
7274 /* We're done with the while-statement. */
7275 finish_while_stmt (statement);
7283 /* Begin the do-statement. */
7284 statement = begin_do_stmt ();
7285 /* Parse the body of the do-statement. */
7286 parser->in_statement = IN_ITERATION_STMT;
7287 cp_parser_implicitly_scoped_statement (parser, NULL);
7288 parser->in_statement = in_statement;
7289 finish_do_body (statement);
7290 /* Look for the `while' keyword. */
7291 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7292 /* Look for the `('. */
7293 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7294 /* Parse the expression. */
7295 expression = cp_parser_expression (parser, /*cast_p=*/false);
7296 /* We're done with the do-statement. */
7297 finish_do_stmt (expression, statement);
7298 /* Look for the `)'. */
7299 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7300 /* Look for the `;'. */
7301 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7307 tree condition = NULL_TREE;
7308 tree expression = NULL_TREE;
7310 /* Begin the for-statement. */
7311 statement = begin_for_stmt ();
7312 /* Look for the `('. */
7313 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7314 /* Parse the initialization. */
7315 cp_parser_for_init_statement (parser);
7316 finish_for_init_stmt (statement);
7318 /* If there's a condition, process it. */
7319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7320 condition = cp_parser_condition (parser);
7321 finish_for_cond (condition, statement);
7322 /* Look for the `;'. */
7323 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7325 /* If there's an expression, process it. */
7326 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7327 expression = cp_parser_expression (parser, /*cast_p=*/false);
7328 finish_for_expr (expression, statement);
7329 check_empty_body (parser, "for");
7330 /* Look for the `)'. */
7331 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7333 /* Parse the body of the for-statement. */
7334 parser->in_statement = IN_ITERATION_STMT;
7335 cp_parser_already_scoped_statement (parser);
7336 parser->in_statement = in_statement;
7338 /* We're done with the for-statement. */
7339 finish_for_stmt (statement);
7344 cp_parser_error (parser, "expected iteration-statement");
7345 statement = error_mark_node;
7352 /* Parse a for-init-statement.
7355 expression-statement
7356 simple-declaration */
7359 cp_parser_for_init_statement (cp_parser* parser)
7361 /* If the next token is a `;', then we have an empty
7362 expression-statement. Grammatically, this is also a
7363 simple-declaration, but an invalid one, because it does not
7364 declare anything. Therefore, if we did not handle this case
7365 specially, we would issue an error message about an invalid
7367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7369 /* We're going to speculatively look for a declaration, falling back
7370 to an expression, if necessary. */
7371 cp_parser_parse_tentatively (parser);
7372 /* Parse the declaration. */
7373 cp_parser_simple_declaration (parser,
7374 /*function_definition_allowed_p=*/false);
7375 /* If the tentative parse failed, then we shall need to look for an
7376 expression-statement. */
7377 if (cp_parser_parse_definitely (parser))
7381 cp_parser_expression_statement (parser, false);
7384 /* Parse a jump-statement.
7389 return expression [opt] ;
7397 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7400 cp_parser_jump_statement (cp_parser* parser)
7402 tree statement = error_mark_node;
7405 unsigned char in_statement;
7407 /* Peek at the next token. */
7408 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7410 return error_mark_node;
7412 /* See what kind of keyword it is. */
7413 keyword = token->keyword;
7417 in_statement = parser->in_statement & ~IN_IF_STMT;
7418 switch (in_statement)
7421 error ("break statement not within loop or switch");
7424 gcc_assert ((in_statement & IN_SWITCH_STMT)
7425 || in_statement == IN_ITERATION_STMT);
7426 statement = finish_break_stmt ();
7429 error ("invalid exit from OpenMP structured block");
7432 error ("break statement used with OpenMP for loop");
7435 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7439 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7442 error ("continue statement not within a loop");
7444 case IN_ITERATION_STMT:
7446 statement = finish_continue_stmt ();
7449 error ("invalid exit from OpenMP structured block");
7454 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7461 /* If the next token is a `;', then there is no
7463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7464 expr = cp_parser_expression (parser, /*cast_p=*/false);
7467 /* Build the return-statement. */
7468 statement = finish_return_stmt (expr);
7469 /* Look for the final `;'. */
7470 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7475 /* Create the goto-statement. */
7476 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7478 /* Issue a warning about this use of a GNU extension. */
7480 pedwarn ("ISO C++ forbids computed gotos");
7481 /* Consume the '*' token. */
7482 cp_lexer_consume_token (parser->lexer);
7483 /* Parse the dependent expression. */
7484 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7487 finish_goto_stmt (cp_parser_identifier (parser));
7488 /* Look for the final `;'. */
7489 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7493 cp_parser_error (parser, "expected jump-statement");
7500 /* Parse a declaration-statement.
7502 declaration-statement:
7503 block-declaration */
7506 cp_parser_declaration_statement (cp_parser* parser)
7510 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7511 p = obstack_alloc (&declarator_obstack, 0);
7513 /* Parse the block-declaration. */
7514 cp_parser_block_declaration (parser, /*statement_p=*/true);
7516 /* Free any declarators allocated. */
7517 obstack_free (&declarator_obstack, p);
7519 /* Finish off the statement. */
7523 /* Some dependent statements (like `if (cond) statement'), are
7524 implicitly in their own scope. In other words, if the statement is
7525 a single statement (as opposed to a compound-statement), it is
7526 none-the-less treated as if it were enclosed in braces. Any
7527 declarations appearing in the dependent statement are out of scope
7528 after control passes that point. This function parses a statement,
7529 but ensures that is in its own scope, even if it is not a
7532 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7533 is a (possibly labeled) if statement which is not enclosed in
7534 braces and has an else clause. This is used to implement
7537 Returns the new statement. */
7540 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7547 /* Mark if () ; with a special NOP_EXPR. */
7548 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7550 cp_lexer_consume_token (parser->lexer);
7551 statement = add_stmt (build_empty_stmt ());
7553 /* if a compound is opened, we simply parse the statement directly. */
7554 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7555 statement = cp_parser_compound_statement (parser, NULL, false);
7556 /* If the token is not a `{', then we must take special action. */
7559 /* Create a compound-statement. */
7560 statement = begin_compound_stmt (0);
7561 /* Parse the dependent-statement. */
7562 cp_parser_statement (parser, NULL_TREE, false, if_p);
7563 /* Finish the dummy compound-statement. */
7564 finish_compound_stmt (statement);
7567 /* Return the statement. */
7571 /* For some dependent statements (like `while (cond) statement'), we
7572 have already created a scope. Therefore, even if the dependent
7573 statement is a compound-statement, we do not want to create another
7577 cp_parser_already_scoped_statement (cp_parser* parser)
7579 /* If the token is a `{', then we must take special action. */
7580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7581 cp_parser_statement (parser, NULL_TREE, false, NULL);
7584 /* Avoid calling cp_parser_compound_statement, so that we
7585 don't create a new scope. Do everything else by hand. */
7586 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7587 cp_parser_statement_seq_opt (parser, NULL_TREE);
7588 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7592 /* Declarations [gram.dcl.dcl] */
7594 /* Parse an optional declaration-sequence.
7598 declaration-seq declaration */
7601 cp_parser_declaration_seq_opt (cp_parser* parser)
7607 token = cp_lexer_peek_token (parser->lexer);
7609 if (token->type == CPP_CLOSE_BRACE
7610 || token->type == CPP_EOF
7611 || token->type == CPP_PRAGMA_EOL)
7614 if (token->type == CPP_SEMICOLON)
7616 /* A declaration consisting of a single semicolon is
7617 invalid. Allow it unless we're being pedantic. */
7618 cp_lexer_consume_token (parser->lexer);
7619 if (pedantic && !in_system_header)
7620 pedwarn ("extra %<;%>");
7624 /* If we're entering or exiting a region that's implicitly
7625 extern "C", modify the lang context appropriately. */
7626 if (!parser->implicit_extern_c && token->implicit_extern_c)
7628 push_lang_context (lang_name_c);
7629 parser->implicit_extern_c = true;
7631 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7633 pop_lang_context ();
7634 parser->implicit_extern_c = false;
7637 if (token->type == CPP_PRAGMA)
7639 /* A top-level declaration can consist solely of a #pragma.
7640 A nested declaration cannot, so this is done here and not
7641 in cp_parser_declaration. (A #pragma at block scope is
7642 handled in cp_parser_statement.) */
7643 cp_parser_pragma (parser, pragma_external);
7647 /* Parse the declaration itself. */
7648 cp_parser_declaration (parser);
7652 /* Parse a declaration.
7657 template-declaration
7658 explicit-instantiation
7659 explicit-specialization
7660 linkage-specification
7661 namespace-definition
7666 __extension__ declaration */
7669 cp_parser_declaration (cp_parser* parser)
7676 /* Check for the `__extension__' keyword. */
7677 if (cp_parser_extension_opt (parser, &saved_pedantic))
7679 /* Parse the qualified declaration. */
7680 cp_parser_declaration (parser);
7681 /* Restore the PEDANTIC flag. */
7682 pedantic = saved_pedantic;
7687 /* Try to figure out what kind of declaration is present. */
7688 token1 = *cp_lexer_peek_token (parser->lexer);
7690 if (token1.type != CPP_EOF)
7691 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7694 token2.type = CPP_EOF;
7695 token2.keyword = RID_MAX;
7698 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7699 p = obstack_alloc (&declarator_obstack, 0);
7701 /* If the next token is `extern' and the following token is a string
7702 literal, then we have a linkage specification. */
7703 if (token1.keyword == RID_EXTERN
7704 && cp_parser_is_string_literal (&token2))
7705 cp_parser_linkage_specification (parser);
7706 /* If the next token is `template', then we have either a template
7707 declaration, an explicit instantiation, or an explicit
7709 else if (token1.keyword == RID_TEMPLATE)
7711 /* `template <>' indicates a template specialization. */
7712 if (token2.type == CPP_LESS
7713 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7714 cp_parser_explicit_specialization (parser);
7715 /* `template <' indicates a template declaration. */
7716 else if (token2.type == CPP_LESS)
7717 cp_parser_template_declaration (parser, /*member_p=*/false);
7718 /* Anything else must be an explicit instantiation. */
7720 cp_parser_explicit_instantiation (parser);
7722 /* If the next token is `export', then we have a template
7724 else if (token1.keyword == RID_EXPORT)
7725 cp_parser_template_declaration (parser, /*member_p=*/false);
7726 /* If the next token is `extern', 'static' or 'inline' and the one
7727 after that is `template', we have a GNU extended explicit
7728 instantiation directive. */
7729 else if (cp_parser_allow_gnu_extensions_p (parser)
7730 && (token1.keyword == RID_EXTERN
7731 || token1.keyword == RID_STATIC
7732 || token1.keyword == RID_INLINE)
7733 && token2.keyword == RID_TEMPLATE)
7734 cp_parser_explicit_instantiation (parser);
7735 /* If the next token is `namespace', check for a named or unnamed
7736 namespace definition. */
7737 else if (token1.keyword == RID_NAMESPACE
7738 && (/* A named namespace definition. */
7739 (token2.type == CPP_NAME
7740 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7742 /* An unnamed namespace definition. */
7743 || token2.type == CPP_OPEN_BRACE
7744 || token2.keyword == RID_ATTRIBUTE))
7745 cp_parser_namespace_definition (parser);
7746 /* An inline (associated) namespace definition. */
7747 else if (token1.keyword == RID_INLINE
7748 && token2.keyword == RID_NAMESPACE)
7749 cp_parser_namespace_definition (parser);
7750 /* Objective-C++ declaration/definition. */
7751 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7752 cp_parser_objc_declaration (parser);
7753 /* We must have either a block declaration or a function
7756 /* Try to parse a block-declaration, or a function-definition. */
7757 cp_parser_block_declaration (parser, /*statement_p=*/false);
7759 /* Free any declarators allocated. */
7760 obstack_free (&declarator_obstack, p);
7763 /* Parse a block-declaration.
7768 namespace-alias-definition
7775 __extension__ block-declaration
7780 static_assert-declaration
7782 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7783 part of a declaration-statement. */
7786 cp_parser_block_declaration (cp_parser *parser,
7792 /* Check for the `__extension__' keyword. */
7793 if (cp_parser_extension_opt (parser, &saved_pedantic))
7795 /* Parse the qualified declaration. */
7796 cp_parser_block_declaration (parser, statement_p);
7797 /* Restore the PEDANTIC flag. */
7798 pedantic = saved_pedantic;
7803 /* Peek at the next token to figure out which kind of declaration is
7805 token1 = cp_lexer_peek_token (parser->lexer);
7807 /* If the next keyword is `asm', we have an asm-definition. */
7808 if (token1->keyword == RID_ASM)
7811 cp_parser_commit_to_tentative_parse (parser);
7812 cp_parser_asm_definition (parser);
7814 /* If the next keyword is `namespace', we have a
7815 namespace-alias-definition. */
7816 else if (token1->keyword == RID_NAMESPACE)
7817 cp_parser_namespace_alias_definition (parser);
7818 /* If the next keyword is `using', we have either a
7819 using-declaration or a using-directive. */
7820 else if (token1->keyword == RID_USING)
7825 cp_parser_commit_to_tentative_parse (parser);
7826 /* If the token after `using' is `namespace', then we have a
7828 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7829 if (token2->keyword == RID_NAMESPACE)
7830 cp_parser_using_directive (parser);
7831 /* Otherwise, it's a using-declaration. */
7833 cp_parser_using_declaration (parser,
7834 /*access_declaration_p=*/false);
7836 /* If the next keyword is `__label__' we have a misplaced label
7838 else if (token1->keyword == RID_LABEL)
7840 cp_lexer_consume_token (parser->lexer);
7841 error ("%<__label__%> not at the beginning of a block");
7842 cp_parser_skip_to_end_of_statement (parser);
7843 /* If the next token is now a `;', consume it. */
7844 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7845 cp_lexer_consume_token (parser->lexer);
7847 /* If the next token is `static_assert' we have a static assertion. */
7848 else if (token1->keyword == RID_STATIC_ASSERT)
7849 cp_parser_static_assert (parser, /*member_p=*/false);
7850 /* Anything else must be a simple-declaration. */
7852 cp_parser_simple_declaration (parser, !statement_p);
7855 /* Parse a simple-declaration.
7858 decl-specifier-seq [opt] init-declarator-list [opt] ;
7860 init-declarator-list:
7862 init-declarator-list , init-declarator
7864 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7865 function-definition as a simple-declaration. */
7868 cp_parser_simple_declaration (cp_parser* parser,
7869 bool function_definition_allowed_p)
7871 cp_decl_specifier_seq decl_specifiers;
7872 int declares_class_or_enum;
7873 bool saw_declarator;
7875 /* Defer access checks until we know what is being declared; the
7876 checks for names appearing in the decl-specifier-seq should be
7877 done as if we were in the scope of the thing being declared. */
7878 push_deferring_access_checks (dk_deferred);
7880 /* Parse the decl-specifier-seq. We have to keep track of whether
7881 or not the decl-specifier-seq declares a named class or
7882 enumeration type, since that is the only case in which the
7883 init-declarator-list is allowed to be empty.
7887 In a simple-declaration, the optional init-declarator-list can be
7888 omitted only when declaring a class or enumeration, that is when
7889 the decl-specifier-seq contains either a class-specifier, an
7890 elaborated-type-specifier, or an enum-specifier. */
7891 cp_parser_decl_specifier_seq (parser,
7892 CP_PARSER_FLAGS_OPTIONAL,
7894 &declares_class_or_enum);
7895 /* We no longer need to defer access checks. */
7896 stop_deferring_access_checks ();
7898 /* In a block scope, a valid declaration must always have a
7899 decl-specifier-seq. By not trying to parse declarators, we can
7900 resolve the declaration/expression ambiguity more quickly. */
7901 if (!function_definition_allowed_p
7902 && !decl_specifiers.any_specifiers_p)
7904 cp_parser_error (parser, "expected declaration");
7908 /* If the next two tokens are both identifiers, the code is
7909 erroneous. The usual cause of this situation is code like:
7913 where "T" should name a type -- but does not. */
7914 if (!decl_specifiers.type
7915 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7917 /* If parsing tentatively, we should commit; we really are
7918 looking at a declaration. */
7919 cp_parser_commit_to_tentative_parse (parser);
7924 /* If we have seen at least one decl-specifier, and the next token
7925 is not a parenthesis, then we must be looking at a declaration.
7926 (After "int (" we might be looking at a functional cast.) */
7927 if (decl_specifiers.any_specifiers_p
7928 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7929 cp_parser_commit_to_tentative_parse (parser);
7931 /* Keep going until we hit the `;' at the end of the simple
7933 saw_declarator = false;
7934 while (cp_lexer_next_token_is_not (parser->lexer,
7938 bool function_definition_p;
7943 /* If we are processing next declarator, coma is expected */
7944 token = cp_lexer_peek_token (parser->lexer);
7945 gcc_assert (token->type == CPP_COMMA);
7946 cp_lexer_consume_token (parser->lexer);
7949 saw_declarator = true;
7951 /* Parse the init-declarator. */
7952 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7954 function_definition_allowed_p,
7956 declares_class_or_enum,
7957 &function_definition_p);
7958 /* If an error occurred while parsing tentatively, exit quickly.
7959 (That usually happens when in the body of a function; each
7960 statement is treated as a declaration-statement until proven
7962 if (cp_parser_error_occurred (parser))
7964 /* Handle function definitions specially. */
7965 if (function_definition_p)
7967 /* If the next token is a `,', then we are probably
7968 processing something like:
7972 which is erroneous. */
7973 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7974 error ("mixing declarations and function-definitions is forbidden");
7975 /* Otherwise, we're done with the list of declarators. */
7978 pop_deferring_access_checks ();
7982 /* The next token should be either a `,' or a `;'. */
7983 token = cp_lexer_peek_token (parser->lexer);
7984 /* If it's a `,', there are more declarators to come. */
7985 if (token->type == CPP_COMMA)
7986 /* will be consumed next time around */;
7987 /* If it's a `;', we are done. */
7988 else if (token->type == CPP_SEMICOLON)
7990 /* Anything else is an error. */
7993 /* If we have already issued an error message we don't need
7994 to issue another one. */
7995 if (decl != error_mark_node
7996 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7997 cp_parser_error (parser, "expected %<,%> or %<;%>");
7998 /* Skip tokens until we reach the end of the statement. */
7999 cp_parser_skip_to_end_of_statement (parser);
8000 /* If the next token is now a `;', consume it. */
8001 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8002 cp_lexer_consume_token (parser->lexer);
8005 /* After the first time around, a function-definition is not
8006 allowed -- even if it was OK at first. For example:
8011 function_definition_allowed_p = false;
8014 /* Issue an error message if no declarators are present, and the
8015 decl-specifier-seq does not itself declare a class or
8017 if (!saw_declarator)
8019 if (cp_parser_declares_only_class_p (parser))
8020 shadow_tag (&decl_specifiers);
8021 /* Perform any deferred access checks. */
8022 perform_deferred_access_checks ();
8025 /* Consume the `;'. */
8026 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8029 pop_deferring_access_checks ();
8032 /* Parse a decl-specifier-seq.
8035 decl-specifier-seq [opt] decl-specifier
8038 storage-class-specifier
8049 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8051 The parser flags FLAGS is used to control type-specifier parsing.
8053 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8056 1: one of the decl-specifiers is an elaborated-type-specifier
8057 (i.e., a type declaration)
8058 2: one of the decl-specifiers is an enum-specifier or a
8059 class-specifier (i.e., a type definition)
8064 cp_parser_decl_specifier_seq (cp_parser* parser,
8065 cp_parser_flags flags,
8066 cp_decl_specifier_seq *decl_specs,
8067 int* declares_class_or_enum)
8069 bool constructor_possible_p = !parser->in_declarator_p;
8071 /* Clear DECL_SPECS. */
8072 clear_decl_specs (decl_specs);
8074 /* Assume no class or enumeration type is declared. */
8075 *declares_class_or_enum = 0;
8077 /* Keep reading specifiers until there are no more to read. */
8081 bool found_decl_spec;
8084 /* Peek at the next token. */
8085 token = cp_lexer_peek_token (parser->lexer);
8086 /* Handle attributes. */
8087 if (token->keyword == RID_ATTRIBUTE)
8089 /* Parse the attributes. */
8090 decl_specs->attributes
8091 = chainon (decl_specs->attributes,
8092 cp_parser_attributes_opt (parser));
8095 /* Assume we will find a decl-specifier keyword. */
8096 found_decl_spec = true;
8097 /* If the next token is an appropriate keyword, we can simply
8098 add it to the list. */
8099 switch (token->keyword)
8104 if (!at_class_scope_p ())
8106 error ("%H%<friend%> used outside of class", &token->location);
8107 cp_lexer_purge_token (parser->lexer);
8111 ++decl_specs->specs[(int) ds_friend];
8112 /* Consume the token. */
8113 cp_lexer_consume_token (parser->lexer);
8117 /* function-specifier:
8124 cp_parser_function_specifier_opt (parser, decl_specs);
8130 ++decl_specs->specs[(int) ds_typedef];
8131 /* Consume the token. */
8132 cp_lexer_consume_token (parser->lexer);
8133 /* A constructor declarator cannot appear in a typedef. */
8134 constructor_possible_p = false;
8135 /* The "typedef" keyword can only occur in a declaration; we
8136 may as well commit at this point. */
8137 cp_parser_commit_to_tentative_parse (parser);
8139 if (decl_specs->storage_class != sc_none)
8140 decl_specs->conflicting_specifiers_p = true;
8143 /* storage-class-specifier:
8153 /* Consume the token. */
8154 cp_lexer_consume_token (parser->lexer);
8156 if (cxx_dialect == cxx98)
8158 /* Complain about `auto' as a storage specifier, if
8159 we're complaining about C++0x compatibility. */
8162 "%<auto%> will change meaning in C++0x; please remove it");
8164 /* Set the storage class anyway. */
8165 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO);
8168 /* We do not yet support the use of `auto' as a
8170 error ("C++0x %<auto%> specifier not supported");
8177 /* Consume the token. */
8178 cp_lexer_consume_token (parser->lexer);
8179 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8182 /* Consume the token. */
8183 cp_lexer_consume_token (parser->lexer);
8184 ++decl_specs->specs[(int) ds_thread];
8188 /* We did not yet find a decl-specifier yet. */
8189 found_decl_spec = false;
8193 /* Constructors are a special case. The `S' in `S()' is not a
8194 decl-specifier; it is the beginning of the declarator. */
8197 && constructor_possible_p
8198 && (cp_parser_constructor_declarator_p
8199 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8201 /* If we don't have a DECL_SPEC yet, then we must be looking at
8202 a type-specifier. */
8203 if (!found_decl_spec && !constructor_p)
8205 int decl_spec_declares_class_or_enum;
8206 bool is_cv_qualifier;
8210 = cp_parser_type_specifier (parser, flags,
8212 /*is_declaration=*/true,
8213 &decl_spec_declares_class_or_enum,
8216 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8218 /* If this type-specifier referenced a user-defined type
8219 (a typedef, class-name, etc.), then we can't allow any
8220 more such type-specifiers henceforth.
8224 The longest sequence of decl-specifiers that could
8225 possibly be a type name is taken as the
8226 decl-specifier-seq of a declaration. The sequence shall
8227 be self-consistent as described below.
8231 As a general rule, at most one type-specifier is allowed
8232 in the complete decl-specifier-seq of a declaration. The
8233 only exceptions are the following:
8235 -- const or volatile can be combined with any other
8238 -- signed or unsigned can be combined with char, long,
8246 void g (const int Pc);
8248 Here, Pc is *not* part of the decl-specifier seq; it's
8249 the declarator. Therefore, once we see a type-specifier
8250 (other than a cv-qualifier), we forbid any additional
8251 user-defined types. We *do* still allow things like `int
8252 int' to be considered a decl-specifier-seq, and issue the
8253 error message later. */
8254 if (type_spec && !is_cv_qualifier)
8255 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8256 /* A constructor declarator cannot follow a type-specifier. */
8259 constructor_possible_p = false;
8260 found_decl_spec = true;
8264 /* If we still do not have a DECL_SPEC, then there are no more
8266 if (!found_decl_spec)
8269 decl_specs->any_specifiers_p = true;
8270 /* After we see one decl-specifier, further decl-specifiers are
8272 flags |= CP_PARSER_FLAGS_OPTIONAL;
8275 cp_parser_check_decl_spec (decl_specs);
8277 /* Don't allow a friend specifier with a class definition. */
8278 if (decl_specs->specs[(int) ds_friend] != 0
8279 && (*declares_class_or_enum & 2))
8280 error ("class definition may not be declared a friend");
8283 /* Parse an (optional) storage-class-specifier.
8285 storage-class-specifier:
8294 storage-class-specifier:
8297 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8300 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8302 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8305 if (cxx_dialect != cxx98)
8307 /* Fall through for C++98. */
8314 /* Consume the token. */
8315 return cp_lexer_consume_token (parser->lexer)->u.value;
8322 /* Parse an (optional) function-specifier.
8329 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8330 Updates DECL_SPECS, if it is non-NULL. */
8333 cp_parser_function_specifier_opt (cp_parser* parser,
8334 cp_decl_specifier_seq *decl_specs)
8336 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8340 ++decl_specs->specs[(int) ds_inline];
8344 /* 14.5.2.3 [temp.mem]
8346 A member function template shall not be virtual. */
8347 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8348 error ("templates may not be %<virtual%>");
8349 else if (decl_specs)
8350 ++decl_specs->specs[(int) ds_virtual];
8355 ++decl_specs->specs[(int) ds_explicit];
8362 /* Consume the token. */
8363 return cp_lexer_consume_token (parser->lexer)->u.value;
8366 /* Parse a linkage-specification.
8368 linkage-specification:
8369 extern string-literal { declaration-seq [opt] }
8370 extern string-literal declaration */
8373 cp_parser_linkage_specification (cp_parser* parser)
8377 /* Look for the `extern' keyword. */
8378 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8380 /* Look for the string-literal. */
8381 linkage = cp_parser_string_literal (parser, false, false);
8383 /* Transform the literal into an identifier. If the literal is a
8384 wide-character string, or contains embedded NULs, then we can't
8385 handle it as the user wants. */
8386 if (strlen (TREE_STRING_POINTER (linkage))
8387 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8389 cp_parser_error (parser, "invalid linkage-specification");
8390 /* Assume C++ linkage. */
8391 linkage = lang_name_cplusplus;
8394 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8396 /* We're now using the new linkage. */
8397 push_lang_context (linkage);
8399 /* If the next token is a `{', then we're using the first
8401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8403 /* Consume the `{' token. */
8404 cp_lexer_consume_token (parser->lexer);
8405 /* Parse the declarations. */
8406 cp_parser_declaration_seq_opt (parser);
8407 /* Look for the closing `}'. */
8408 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8410 /* Otherwise, there's just one declaration. */
8413 bool saved_in_unbraced_linkage_specification_p;
8415 saved_in_unbraced_linkage_specification_p
8416 = parser->in_unbraced_linkage_specification_p;
8417 parser->in_unbraced_linkage_specification_p = true;
8418 cp_parser_declaration (parser);
8419 parser->in_unbraced_linkage_specification_p
8420 = saved_in_unbraced_linkage_specification_p;
8423 /* We're done with the linkage-specification. */
8424 pop_lang_context ();
8427 /* Parse a static_assert-declaration.
8429 static_assert-declaration:
8430 static_assert ( constant-expression , string-literal ) ;
8432 If MEMBER_P, this static_assert is a class member. */
8435 cp_parser_static_assert(cp_parser *parser, bool member_p)
8440 location_t saved_loc;
8442 /* Peek at the `static_assert' token so we can keep track of exactly
8443 where the static assertion started. */
8444 token = cp_lexer_peek_token (parser->lexer);
8445 saved_loc = token->location;
8447 /* Look for the `static_assert' keyword. */
8448 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8452 /* We know we are in a static assertion; commit to any tentative
8454 if (cp_parser_parsing_tentatively (parser))
8455 cp_parser_commit_to_tentative_parse (parser);
8457 /* Parse the `(' starting the static assertion condition. */
8458 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8460 /* Parse the constant-expression. */
8462 cp_parser_constant_expression (parser,
8463 /*allow_non_constant_p=*/false,
8464 /*non_constant_p=*/NULL);
8466 /* Parse the separating `,'. */
8467 cp_parser_require (parser, CPP_COMMA, "`,'");
8469 /* Parse the string-literal message. */
8470 message = cp_parser_string_literal (parser,
8471 /*translate=*/false,
8474 /* A `)' completes the static assertion. */
8475 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8476 cp_parser_skip_to_closing_parenthesis (parser,
8477 /*recovering=*/true,
8479 /*consume_paren=*/true);
8481 /* A semicolon terminates the declaration. */
8482 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8484 /* Complete the static assertion, which may mean either processing
8485 the static assert now or saving it for template instantiation. */
8486 finish_static_assert (condition, message, saved_loc, member_p);
8489 /* Parse a `decltype' type. Returns the type.
8491 simple-type-specifier:
8492 decltype ( expression ) */
8495 cp_parser_decltype (cp_parser *parser)
8498 bool id_expression_or_member_access_p = false;
8499 const char *saved_message;
8500 bool saved_integral_constant_expression_p;
8501 bool saved_non_integral_constant_expression_p;
8503 /* Look for the `decltype' token. */
8504 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8505 return error_mark_node;
8507 /* Types cannot be defined in a `decltype' expression. Save away the
8509 saved_message = parser->type_definition_forbidden_message;
8511 /* And create the new one. */
8512 parser->type_definition_forbidden_message
8513 = "types may not be defined in `decltype' expressions";
8515 /* The restrictions on constant-expressions do not apply inside
8516 decltype expressions. */
8517 saved_integral_constant_expression_p
8518 = parser->integral_constant_expression_p;
8519 saved_non_integral_constant_expression_p
8520 = parser->non_integral_constant_expression_p;
8521 parser->integral_constant_expression_p = false;
8523 /* Do not actually evaluate the expression. */
8526 /* Parse the opening `('. */
8527 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8528 return error_mark_node;
8530 /* First, try parsing an id-expression. */
8531 cp_parser_parse_tentatively (parser);
8532 expr = cp_parser_id_expression (parser,
8533 /*template_keyword_p=*/false,
8534 /*check_dependency_p=*/true,
8535 /*template_p=*/NULL,
8536 /*declarator_p=*/false,
8537 /*optional_p=*/false);
8539 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8541 bool non_integral_constant_expression_p = false;
8542 tree id_expression = expr;
8544 const char *error_msg;
8546 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8547 /* Lookup the name we got back from the id-expression. */
8548 expr = cp_parser_lookup_name (parser, expr,
8550 /*is_template=*/false,
8551 /*is_namespace=*/false,
8552 /*check_dependency=*/true,
8553 /*ambiguous_decls=*/NULL);
8556 && expr != error_mark_node
8557 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8558 && TREE_CODE (expr) != TYPE_DECL
8559 && (TREE_CODE (expr) != BIT_NOT_EXPR
8560 || !TYPE_P (TREE_OPERAND (expr, 0)))
8561 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8563 /* Complete lookup of the id-expression. */
8564 expr = (finish_id_expression
8565 (id_expression, expr, parser->scope, &idk,
8566 /*integral_constant_expression_p=*/false,
8567 /*allow_non_integral_constant_expression_p=*/true,
8568 &non_integral_constant_expression_p,
8569 /*template_p=*/false,
8571 /*address_p=*/false,
8572 /*template_arg_p=*/false,
8575 if (expr == error_mark_node)
8576 /* We found an id-expression, but it was something that we
8577 should not have found. This is an error, not something
8578 we can recover from, so note that we found an
8579 id-expression and we'll recover as gracefully as
8581 id_expression_or_member_access_p = true;
8585 && expr != error_mark_node
8586 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8587 /* We have an id-expression. */
8588 id_expression_or_member_access_p = true;
8591 if (!id_expression_or_member_access_p)
8593 /* Abort the id-expression parse. */
8594 cp_parser_abort_tentative_parse (parser);
8596 /* Parsing tentatively, again. */
8597 cp_parser_parse_tentatively (parser);
8599 /* Parse a class member access. */
8600 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8602 /*member_access_only_p=*/true);
8605 && expr != error_mark_node
8606 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8607 /* We have an id-expression. */
8608 id_expression_or_member_access_p = true;
8611 if (id_expression_or_member_access_p)
8612 /* We have parsed the complete id-expression or member access. */
8613 cp_parser_parse_definitely (parser);
8616 /* Abort our attempt to parse an id-expression or member access
8618 cp_parser_abort_tentative_parse (parser);
8620 /* Parse a full expression. */
8621 expr = cp_parser_expression (parser, /*cast_p=*/false);
8624 /* Go back to evaluating expressions. */
8627 /* Restore the old message and the integral constant expression
8629 parser->type_definition_forbidden_message = saved_message;
8630 parser->integral_constant_expression_p
8631 = saved_integral_constant_expression_p;
8632 parser->non_integral_constant_expression_p
8633 = saved_non_integral_constant_expression_p;
8635 if (expr == error_mark_node)
8637 /* Skip everything up to the closing `)'. */
8638 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8639 /*consume_paren=*/true);
8640 return error_mark_node;
8643 /* Parse to the closing `)'. */
8644 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8646 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8647 /*consume_paren=*/true);
8648 return error_mark_node;
8651 return finish_decltype_type (expr, id_expression_or_member_access_p);
8654 /* Special member functions [gram.special] */
8656 /* Parse a conversion-function-id.
8658 conversion-function-id:
8659 operator conversion-type-id
8661 Returns an IDENTIFIER_NODE representing the operator. */
8664 cp_parser_conversion_function_id (cp_parser* parser)
8668 tree saved_qualifying_scope;
8669 tree saved_object_scope;
8670 tree pushed_scope = NULL_TREE;
8672 /* Look for the `operator' token. */
8673 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8674 return error_mark_node;
8675 /* When we parse the conversion-type-id, the current scope will be
8676 reset. However, we need that information in able to look up the
8677 conversion function later, so we save it here. */
8678 saved_scope = parser->scope;
8679 saved_qualifying_scope = parser->qualifying_scope;
8680 saved_object_scope = parser->object_scope;
8681 /* We must enter the scope of the class so that the names of
8682 entities declared within the class are available in the
8683 conversion-type-id. For example, consider:
8690 S::operator I() { ... }
8692 In order to see that `I' is a type-name in the definition, we
8693 must be in the scope of `S'. */
8695 pushed_scope = push_scope (saved_scope);
8696 /* Parse the conversion-type-id. */
8697 type = cp_parser_conversion_type_id (parser);
8698 /* Leave the scope of the class, if any. */
8700 pop_scope (pushed_scope);
8701 /* Restore the saved scope. */
8702 parser->scope = saved_scope;
8703 parser->qualifying_scope = saved_qualifying_scope;
8704 parser->object_scope = saved_object_scope;
8705 /* If the TYPE is invalid, indicate failure. */
8706 if (type == error_mark_node)
8707 return error_mark_node;
8708 return mangle_conv_op_name_for_type (type);
8711 /* Parse a conversion-type-id:
8714 type-specifier-seq conversion-declarator [opt]
8716 Returns the TYPE specified. */
8719 cp_parser_conversion_type_id (cp_parser* parser)
8722 cp_decl_specifier_seq type_specifiers;
8723 cp_declarator *declarator;
8724 tree type_specified;
8726 /* Parse the attributes. */
8727 attributes = cp_parser_attributes_opt (parser);
8728 /* Parse the type-specifiers. */
8729 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8731 /* If that didn't work, stop. */
8732 if (type_specifiers.type == error_mark_node)
8733 return error_mark_node;
8734 /* Parse the conversion-declarator. */
8735 declarator = cp_parser_conversion_declarator_opt (parser);
8737 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8738 /*initialized=*/0, &attributes);
8740 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8741 return type_specified;
8744 /* Parse an (optional) conversion-declarator.
8746 conversion-declarator:
8747 ptr-operator conversion-declarator [opt]
8751 static cp_declarator *
8752 cp_parser_conversion_declarator_opt (cp_parser* parser)
8754 enum tree_code code;
8756 cp_cv_quals cv_quals;
8758 /* We don't know if there's a ptr-operator next, or not. */
8759 cp_parser_parse_tentatively (parser);
8760 /* Try the ptr-operator. */
8761 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8762 /* If it worked, look for more conversion-declarators. */
8763 if (cp_parser_parse_definitely (parser))
8765 cp_declarator *declarator;
8767 /* Parse another optional declarator. */
8768 declarator = cp_parser_conversion_declarator_opt (parser);
8770 return cp_parser_make_indirect_declarator
8771 (code, class_type, cv_quals, declarator);
8777 /* Parse an (optional) ctor-initializer.
8780 : mem-initializer-list
8782 Returns TRUE iff the ctor-initializer was actually present. */
8785 cp_parser_ctor_initializer_opt (cp_parser* parser)
8787 /* If the next token is not a `:', then there is no
8788 ctor-initializer. */
8789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8791 /* Do default initialization of any bases and members. */
8792 if (DECL_CONSTRUCTOR_P (current_function_decl))
8793 finish_mem_initializers (NULL_TREE);
8798 /* Consume the `:' token. */
8799 cp_lexer_consume_token (parser->lexer);
8800 /* And the mem-initializer-list. */
8801 cp_parser_mem_initializer_list (parser);
8806 /* Parse a mem-initializer-list.
8808 mem-initializer-list:
8809 mem-initializer ... [opt]
8810 mem-initializer ... [opt] , mem-initializer-list */
8813 cp_parser_mem_initializer_list (cp_parser* parser)
8815 tree mem_initializer_list = NULL_TREE;
8817 /* Let the semantic analysis code know that we are starting the
8818 mem-initializer-list. */
8819 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8820 error ("only constructors take base initializers");
8822 /* Loop through the list. */
8825 tree mem_initializer;
8827 /* Parse the mem-initializer. */
8828 mem_initializer = cp_parser_mem_initializer (parser);
8829 /* If the next token is a `...', we're expanding member initializers. */
8830 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8832 /* Consume the `...'. */
8833 cp_lexer_consume_token (parser->lexer);
8835 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8836 can be expanded but members cannot. */
8837 if (mem_initializer != error_mark_node
8838 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8840 error ("cannot expand initializer for member %<%D%>",
8841 TREE_PURPOSE (mem_initializer));
8842 mem_initializer = error_mark_node;
8845 /* Construct the pack expansion type. */
8846 if (mem_initializer != error_mark_node)
8847 mem_initializer = make_pack_expansion (mem_initializer);
8849 /* Add it to the list, unless it was erroneous. */
8850 if (mem_initializer != error_mark_node)
8852 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8853 mem_initializer_list = mem_initializer;
8855 /* If the next token is not a `,', we're done. */
8856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8858 /* Consume the `,' token. */
8859 cp_lexer_consume_token (parser->lexer);
8862 /* Perform semantic analysis. */
8863 if (DECL_CONSTRUCTOR_P (current_function_decl))
8864 finish_mem_initializers (mem_initializer_list);
8867 /* Parse a mem-initializer.
8870 mem-initializer-id ( expression-list [opt] )
8875 ( expression-list [opt] )
8877 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8878 class) or FIELD_DECL (for a non-static data member) to initialize;
8879 the TREE_VALUE is the expression-list. An empty initialization
8880 list is represented by void_list_node. */
8883 cp_parser_mem_initializer (cp_parser* parser)
8885 tree mem_initializer_id;
8886 tree expression_list;
8889 /* Find out what is being initialized. */
8890 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8892 pedwarn ("anachronistic old-style base class initializer");
8893 mem_initializer_id = NULL_TREE;
8896 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8897 member = expand_member_init (mem_initializer_id);
8898 if (member && !DECL_P (member))
8899 in_base_initializer = 1;
8902 = cp_parser_parenthesized_expression_list (parser, false,
8904 /*allow_expansion_p=*/true,
8905 /*non_constant_p=*/NULL);
8906 if (expression_list == error_mark_node)
8907 return error_mark_node;
8908 if (!expression_list)
8909 expression_list = void_type_node;
8911 in_base_initializer = 0;
8913 return member ? build_tree_list (member, expression_list) : error_mark_node;
8916 /* Parse a mem-initializer-id.
8919 :: [opt] nested-name-specifier [opt] class-name
8922 Returns a TYPE indicating the class to be initializer for the first
8923 production. Returns an IDENTIFIER_NODE indicating the data member
8924 to be initialized for the second production. */
8927 cp_parser_mem_initializer_id (cp_parser* parser)
8929 bool global_scope_p;
8930 bool nested_name_specifier_p;
8931 bool template_p = false;
8934 /* `typename' is not allowed in this context ([temp.res]). */
8935 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8937 error ("keyword %<typename%> not allowed in this context (a qualified "
8938 "member initializer is implicitly a type)");
8939 cp_lexer_consume_token (parser->lexer);
8941 /* Look for the optional `::' operator. */
8943 = (cp_parser_global_scope_opt (parser,
8944 /*current_scope_valid_p=*/false)
8946 /* Look for the optional nested-name-specifier. The simplest way to
8951 The keyword `typename' is not permitted in a base-specifier or
8952 mem-initializer; in these contexts a qualified name that
8953 depends on a template-parameter is implicitly assumed to be a
8956 is to assume that we have seen the `typename' keyword at this
8958 nested_name_specifier_p
8959 = (cp_parser_nested_name_specifier_opt (parser,
8960 /*typename_keyword_p=*/true,
8961 /*check_dependency_p=*/true,
8963 /*is_declaration=*/true)
8965 if (nested_name_specifier_p)
8966 template_p = cp_parser_optional_template_keyword (parser);
8967 /* If there is a `::' operator or a nested-name-specifier, then we
8968 are definitely looking for a class-name. */
8969 if (global_scope_p || nested_name_specifier_p)
8970 return cp_parser_class_name (parser,
8971 /*typename_keyword_p=*/true,
8972 /*template_keyword_p=*/template_p,
8974 /*check_dependency_p=*/true,
8975 /*class_head_p=*/false,
8976 /*is_declaration=*/true);
8977 /* Otherwise, we could also be looking for an ordinary identifier. */
8978 cp_parser_parse_tentatively (parser);
8979 /* Try a class-name. */
8980 id = cp_parser_class_name (parser,
8981 /*typename_keyword_p=*/true,
8982 /*template_keyword_p=*/false,
8984 /*check_dependency_p=*/true,
8985 /*class_head_p=*/false,
8986 /*is_declaration=*/true);
8987 /* If we found one, we're done. */
8988 if (cp_parser_parse_definitely (parser))
8990 /* Otherwise, look for an ordinary identifier. */
8991 return cp_parser_identifier (parser);
8994 /* Overloading [gram.over] */
8996 /* Parse an operator-function-id.
8998 operator-function-id:
9001 Returns an IDENTIFIER_NODE for the operator which is a
9002 human-readable spelling of the identifier, e.g., `operator +'. */
9005 cp_parser_operator_function_id (cp_parser* parser)
9007 /* Look for the `operator' keyword. */
9008 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
9009 return error_mark_node;
9010 /* And then the name of the operator itself. */
9011 return cp_parser_operator (parser);
9014 /* Parse an operator.
9017 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9018 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9019 || ++ -- , ->* -> () []
9026 Returns an IDENTIFIER_NODE for the operator which is a
9027 human-readable spelling of the identifier, e.g., `operator +'. */
9030 cp_parser_operator (cp_parser* parser)
9032 tree id = NULL_TREE;
9035 /* Peek at the next token. */
9036 token = cp_lexer_peek_token (parser->lexer);
9037 /* Figure out which operator we have. */
9038 switch (token->type)
9044 /* The keyword should be either `new' or `delete'. */
9045 if (token->keyword == RID_NEW)
9047 else if (token->keyword == RID_DELETE)
9052 /* Consume the `new' or `delete' token. */
9053 cp_lexer_consume_token (parser->lexer);
9055 /* Peek at the next token. */
9056 token = cp_lexer_peek_token (parser->lexer);
9057 /* If it's a `[' token then this is the array variant of the
9059 if (token->type == CPP_OPEN_SQUARE)
9061 /* Consume the `[' token. */
9062 cp_lexer_consume_token (parser->lexer);
9063 /* Look for the `]' token. */
9064 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9065 id = ansi_opname (op == NEW_EXPR
9066 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9068 /* Otherwise, we have the non-array variant. */
9070 id = ansi_opname (op);
9076 id = ansi_opname (PLUS_EXPR);
9080 id = ansi_opname (MINUS_EXPR);
9084 id = ansi_opname (MULT_EXPR);
9088 id = ansi_opname (TRUNC_DIV_EXPR);
9092 id = ansi_opname (TRUNC_MOD_EXPR);
9096 id = ansi_opname (BIT_XOR_EXPR);
9100 id = ansi_opname (BIT_AND_EXPR);
9104 id = ansi_opname (BIT_IOR_EXPR);
9108 id = ansi_opname (BIT_NOT_EXPR);
9112 id = ansi_opname (TRUTH_NOT_EXPR);
9116 id = ansi_assopname (NOP_EXPR);
9120 id = ansi_opname (LT_EXPR);
9124 id = ansi_opname (GT_EXPR);
9128 id = ansi_assopname (PLUS_EXPR);
9132 id = ansi_assopname (MINUS_EXPR);
9136 id = ansi_assopname (MULT_EXPR);
9140 id = ansi_assopname (TRUNC_DIV_EXPR);
9144 id = ansi_assopname (TRUNC_MOD_EXPR);
9148 id = ansi_assopname (BIT_XOR_EXPR);
9152 id = ansi_assopname (BIT_AND_EXPR);
9156 id = ansi_assopname (BIT_IOR_EXPR);
9160 id = ansi_opname (LSHIFT_EXPR);
9164 id = ansi_opname (RSHIFT_EXPR);
9168 id = ansi_assopname (LSHIFT_EXPR);
9172 id = ansi_assopname (RSHIFT_EXPR);
9176 id = ansi_opname (EQ_EXPR);
9180 id = ansi_opname (NE_EXPR);
9184 id = ansi_opname (LE_EXPR);
9187 case CPP_GREATER_EQ:
9188 id = ansi_opname (GE_EXPR);
9192 id = ansi_opname (TRUTH_ANDIF_EXPR);
9196 id = ansi_opname (TRUTH_ORIF_EXPR);
9200 id = ansi_opname (POSTINCREMENT_EXPR);
9203 case CPP_MINUS_MINUS:
9204 id = ansi_opname (PREDECREMENT_EXPR);
9208 id = ansi_opname (COMPOUND_EXPR);
9211 case CPP_DEREF_STAR:
9212 id = ansi_opname (MEMBER_REF);
9216 id = ansi_opname (COMPONENT_REF);
9219 case CPP_OPEN_PAREN:
9220 /* Consume the `('. */
9221 cp_lexer_consume_token (parser->lexer);
9222 /* Look for the matching `)'. */
9223 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9224 return ansi_opname (CALL_EXPR);
9226 case CPP_OPEN_SQUARE:
9227 /* Consume the `['. */
9228 cp_lexer_consume_token (parser->lexer);
9229 /* Look for the matching `]'. */
9230 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9231 return ansi_opname (ARRAY_REF);
9234 /* Anything else is an error. */
9238 /* If we have selected an identifier, we need to consume the
9241 cp_lexer_consume_token (parser->lexer);
9242 /* Otherwise, no valid operator name was present. */
9245 cp_parser_error (parser, "expected operator");
9246 id = error_mark_node;
9252 /* Parse a template-declaration.
9254 template-declaration:
9255 export [opt] template < template-parameter-list > declaration
9257 If MEMBER_P is TRUE, this template-declaration occurs within a
9260 The grammar rule given by the standard isn't correct. What
9263 template-declaration:
9264 export [opt] template-parameter-list-seq
9265 decl-specifier-seq [opt] init-declarator [opt] ;
9266 export [opt] template-parameter-list-seq
9269 template-parameter-list-seq:
9270 template-parameter-list-seq [opt]
9271 template < template-parameter-list > */
9274 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9276 /* Check for `export'. */
9277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9279 /* Consume the `export' token. */
9280 cp_lexer_consume_token (parser->lexer);
9281 /* Warn that we do not support `export'. */
9282 warning (0, "keyword %<export%> not implemented, and will be ignored");
9285 cp_parser_template_declaration_after_export (parser, member_p);
9288 /* Parse a template-parameter-list.
9290 template-parameter-list:
9292 template-parameter-list , template-parameter
9294 Returns a TREE_LIST. Each node represents a template parameter.
9295 The nodes are connected via their TREE_CHAINs. */
9298 cp_parser_template_parameter_list (cp_parser* parser)
9300 tree parameter_list = NULL_TREE;
9302 begin_template_parm_list ();
9308 bool is_parameter_pack;
9310 /* Parse the template-parameter. */
9311 parameter = cp_parser_template_parameter (parser,
9313 &is_parameter_pack);
9314 /* Add it to the list. */
9315 if (parameter != error_mark_node)
9316 parameter_list = process_template_parm (parameter_list,
9322 tree err_parm = build_tree_list (parameter, parameter);
9323 TREE_VALUE (err_parm) = error_mark_node;
9324 parameter_list = chainon (parameter_list, err_parm);
9327 /* Peek at the next token. */
9328 token = cp_lexer_peek_token (parser->lexer);
9329 /* If it's not a `,', we're done. */
9330 if (token->type != CPP_COMMA)
9332 /* Otherwise, consume the `,' token. */
9333 cp_lexer_consume_token (parser->lexer);
9336 return end_template_parm_list (parameter_list);
9339 /* Parse a template-parameter.
9343 parameter-declaration
9345 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9346 the parameter. The TREE_PURPOSE is the default value, if any.
9347 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9348 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9349 set to true iff this parameter is a parameter pack. */
9352 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9353 bool *is_parameter_pack)
9356 cp_parameter_declarator *parameter_declarator;
9357 cp_declarator *id_declarator;
9360 /* Assume it is a type parameter or a template parameter. */
9361 *is_non_type = false;
9362 /* Assume it not a parameter pack. */
9363 *is_parameter_pack = false;
9364 /* Peek at the next token. */
9365 token = cp_lexer_peek_token (parser->lexer);
9366 /* If it is `class' or `template', we have a type-parameter. */
9367 if (token->keyword == RID_TEMPLATE)
9368 return cp_parser_type_parameter (parser, is_parameter_pack);
9369 /* If it is `class' or `typename' we do not know yet whether it is a
9370 type parameter or a non-type parameter. Consider:
9372 template <typename T, typename T::X X> ...
9376 template <class C, class D*> ...
9378 Here, the first parameter is a type parameter, and the second is
9379 a non-type parameter. We can tell by looking at the token after
9380 the identifier -- if it is a `,', `=', or `>' then we have a type
9382 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9384 /* Peek at the token after `class' or `typename'. */
9385 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9386 /* If it's an ellipsis, we have a template type parameter
9388 if (token->type == CPP_ELLIPSIS)
9389 return cp_parser_type_parameter (parser, is_parameter_pack);
9390 /* If it's an identifier, skip it. */
9391 if (token->type == CPP_NAME)
9392 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9393 /* Now, see if the token looks like the end of a template
9395 if (token->type == CPP_COMMA
9396 || token->type == CPP_EQ
9397 || token->type == CPP_GREATER)
9398 return cp_parser_type_parameter (parser, is_parameter_pack);
9401 /* Otherwise, it is a non-type parameter.
9405 When parsing a default template-argument for a non-type
9406 template-parameter, the first non-nested `>' is taken as the end
9407 of the template parameter-list rather than a greater-than
9409 *is_non_type = true;
9410 parameter_declarator
9411 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9412 /*parenthesized_p=*/NULL);
9414 /* If the parameter declaration is marked as a parameter pack, set
9415 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9416 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9418 if (parameter_declarator
9419 && parameter_declarator->declarator
9420 && parameter_declarator->declarator->parameter_pack_p)
9422 *is_parameter_pack = true;
9423 parameter_declarator->declarator->parameter_pack_p = false;
9426 /* If the next token is an ellipsis, and we don't already have it
9427 marked as a parameter pack, then we have a parameter pack (that
9428 has no declarator). */
9429 if (!*is_parameter_pack
9430 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9431 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9433 /* Consume the `...'. */
9434 cp_lexer_consume_token (parser->lexer);
9435 maybe_warn_variadic_templates ();
9437 *is_parameter_pack = true;
9439 /* We might end up with a pack expansion as the type of the non-type
9440 template parameter, in which case this is a non-type template
9442 else if (parameter_declarator
9443 && parameter_declarator->decl_specifiers.type
9444 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9446 *is_parameter_pack = true;
9447 parameter_declarator->decl_specifiers.type =
9448 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9451 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9453 /* Parameter packs cannot have default arguments. However, a
9454 user may try to do so, so we'll parse them and give an
9455 appropriate diagnostic here. */
9457 /* Consume the `='. */
9458 cp_lexer_consume_token (parser->lexer);
9460 /* Find the name of the parameter pack. */
9461 id_declarator = parameter_declarator->declarator;
9462 while (id_declarator && id_declarator->kind != cdk_id)
9463 id_declarator = id_declarator->declarator;
9465 if (id_declarator && id_declarator->kind == cdk_id)
9466 error ("template parameter pack %qD cannot have a default argument",
9467 id_declarator->u.id.unqualified_name);
9469 error ("template parameter pack cannot have a default argument");
9471 /* Parse the default argument, but throw away the result. */
9472 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9475 parm = grokdeclarator (parameter_declarator->declarator,
9476 ¶meter_declarator->decl_specifiers,
9477 PARM, /*initialized=*/0,
9479 if (parm == error_mark_node)
9480 return error_mark_node;
9482 return build_tree_list (parameter_declarator->default_argument, parm);
9485 /* Parse a type-parameter.
9488 class identifier [opt]
9489 class identifier [opt] = type-id
9490 typename identifier [opt]
9491 typename identifier [opt] = type-id
9492 template < template-parameter-list > class identifier [opt]
9493 template < template-parameter-list > class identifier [opt]
9496 GNU Extension (variadic templates):
9499 class ... identifier [opt]
9500 typename ... identifier [opt]
9502 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9503 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9504 the declaration of the parameter.
9506 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9509 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9514 /* Look for a keyword to tell us what kind of parameter this is. */
9515 token = cp_parser_require (parser, CPP_KEYWORD,
9516 "`class', `typename', or `template'");
9518 return error_mark_node;
9520 switch (token->keyword)
9526 tree default_argument;
9528 /* If the next token is an ellipsis, we have a template
9530 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9532 /* Consume the `...' token. */
9533 cp_lexer_consume_token (parser->lexer);
9534 maybe_warn_variadic_templates ();
9536 *is_parameter_pack = true;
9539 /* If the next token is an identifier, then it names the
9541 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9542 identifier = cp_parser_identifier (parser);
9544 identifier = NULL_TREE;
9546 /* Create the parameter. */
9547 parameter = finish_template_type_parm (class_type_node, identifier);
9549 /* If the next token is an `=', we have a default argument. */
9550 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9552 /* Consume the `=' token. */
9553 cp_lexer_consume_token (parser->lexer);
9554 /* Parse the default-argument. */
9555 push_deferring_access_checks (dk_no_deferred);
9556 default_argument = cp_parser_type_id (parser);
9558 /* Template parameter packs cannot have default
9560 if (*is_parameter_pack)
9563 error ("template parameter pack %qD cannot have a default argument",
9566 error ("template parameter packs cannot have default arguments");
9567 default_argument = NULL_TREE;
9569 pop_deferring_access_checks ();
9572 default_argument = NULL_TREE;
9574 /* Create the combined representation of the parameter and the
9575 default argument. */
9576 parameter = build_tree_list (default_argument, parameter);
9582 tree parameter_list;
9584 tree default_argument;
9586 /* Look for the `<'. */
9587 cp_parser_require (parser, CPP_LESS, "`<'");
9588 /* Parse the template-parameter-list. */
9589 parameter_list = cp_parser_template_parameter_list (parser);
9590 /* Look for the `>'. */
9591 cp_parser_require (parser, CPP_GREATER, "`>'");
9592 /* Look for the `class' keyword. */
9593 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9594 /* If the next token is an ellipsis, we have a template
9596 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9598 /* Consume the `...' token. */
9599 cp_lexer_consume_token (parser->lexer);
9600 maybe_warn_variadic_templates ();
9602 *is_parameter_pack = true;
9604 /* If the next token is an `=', then there is a
9605 default-argument. If the next token is a `>', we are at
9606 the end of the parameter-list. If the next token is a `,',
9607 then we are at the end of this parameter. */
9608 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9609 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9610 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9612 identifier = cp_parser_identifier (parser);
9613 /* Treat invalid names as if the parameter were nameless. */
9614 if (identifier == error_mark_node)
9615 identifier = NULL_TREE;
9618 identifier = NULL_TREE;
9620 /* Create the template parameter. */
9621 parameter = finish_template_template_parm (class_type_node,
9624 /* If the next token is an `=', then there is a
9625 default-argument. */
9626 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9630 /* Consume the `='. */
9631 cp_lexer_consume_token (parser->lexer);
9632 /* Parse the id-expression. */
9633 push_deferring_access_checks (dk_no_deferred);
9635 = cp_parser_id_expression (parser,
9636 /*template_keyword_p=*/false,
9637 /*check_dependency_p=*/true,
9638 /*template_p=*/&is_template,
9639 /*declarator_p=*/false,
9640 /*optional_p=*/false);
9641 if (TREE_CODE (default_argument) == TYPE_DECL)
9642 /* If the id-expression was a template-id that refers to
9643 a template-class, we already have the declaration here,
9644 so no further lookup is needed. */
9647 /* Look up the name. */
9649 = cp_parser_lookup_name (parser, default_argument,
9651 /*is_template=*/is_template,
9652 /*is_namespace=*/false,
9653 /*check_dependency=*/true,
9654 /*ambiguous_decls=*/NULL);
9655 /* See if the default argument is valid. */
9657 = check_template_template_default_arg (default_argument);
9659 /* Template parameter packs cannot have default
9661 if (*is_parameter_pack)
9664 error ("template parameter pack %qD cannot have a default argument",
9667 error ("template parameter packs cannot have default arguments");
9668 default_argument = NULL_TREE;
9670 pop_deferring_access_checks ();
9673 default_argument = NULL_TREE;
9675 /* Create the combined representation of the parameter and the
9676 default argument. */
9677 parameter = build_tree_list (default_argument, parameter);
9689 /* Parse a template-id.
9692 template-name < template-argument-list [opt] >
9694 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9695 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9696 returned. Otherwise, if the template-name names a function, or set
9697 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9698 names a class, returns a TYPE_DECL for the specialization.
9700 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9701 uninstantiated templates. */
9704 cp_parser_template_id (cp_parser *parser,
9705 bool template_keyword_p,
9706 bool check_dependency_p,
9707 bool is_declaration)
9713 cp_token_position start_of_id = 0;
9714 deferred_access_check *chk;
9715 VEC (deferred_access_check,gc) *access_check;
9716 cp_token *next_token, *next_token_2;
9719 /* If the next token corresponds to a template-id, there is no need
9721 next_token = cp_lexer_peek_token (parser->lexer);
9722 if (next_token->type == CPP_TEMPLATE_ID)
9724 struct tree_check *check_value;
9726 /* Get the stored value. */
9727 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9728 /* Perform any access checks that were deferred. */
9729 access_check = check_value->checks;
9733 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9736 perform_or_defer_access_check (chk->binfo,
9741 /* Return the stored value. */
9742 return check_value->value;
9745 /* Avoid performing name lookup if there is no possibility of
9746 finding a template-id. */
9747 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9748 || (next_token->type == CPP_NAME
9749 && !cp_parser_nth_token_starts_template_argument_list_p
9752 cp_parser_error (parser, "expected template-id");
9753 return error_mark_node;
9756 /* Remember where the template-id starts. */
9757 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9758 start_of_id = cp_lexer_token_position (parser->lexer, false);
9760 push_deferring_access_checks (dk_deferred);
9762 /* Parse the template-name. */
9763 is_identifier = false;
9764 template = cp_parser_template_name (parser, template_keyword_p,
9768 if (template == error_mark_node || is_identifier)
9770 pop_deferring_access_checks ();
9774 /* If we find the sequence `[:' after a template-name, it's probably
9775 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9776 parse correctly the argument list. */
9777 next_token = cp_lexer_peek_token (parser->lexer);
9778 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9779 if (next_token->type == CPP_OPEN_SQUARE
9780 && next_token->flags & DIGRAPH
9781 && next_token_2->type == CPP_COLON
9782 && !(next_token_2->flags & PREV_WHITE))
9784 cp_parser_parse_tentatively (parser);
9785 /* Change `:' into `::'. */
9786 next_token_2->type = CPP_SCOPE;
9787 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9789 cp_lexer_consume_token (parser->lexer);
9790 /* Parse the arguments. */
9791 arguments = cp_parser_enclosed_template_argument_list (parser);
9792 if (!cp_parser_parse_definitely (parser))
9794 /* If we couldn't parse an argument list, then we revert our changes
9795 and return simply an error. Maybe this is not a template-id
9797 next_token_2->type = CPP_COLON;
9798 cp_parser_error (parser, "expected %<<%>");
9799 pop_deferring_access_checks ();
9800 return error_mark_node;
9802 /* Otherwise, emit an error about the invalid digraph, but continue
9803 parsing because we got our argument list. */
9804 permerror ("%<<::%> cannot begin a template-argument list");
9805 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9806 "between %<<%> and %<::%>");
9807 if (!flag_permissive)
9812 inform ("(if you use -fpermissive G++ will accept your code)");
9819 /* Look for the `<' that starts the template-argument-list. */
9820 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9822 pop_deferring_access_checks ();
9823 return error_mark_node;
9825 /* Parse the arguments. */
9826 arguments = cp_parser_enclosed_template_argument_list (parser);
9829 /* Build a representation of the specialization. */
9830 if (TREE_CODE (template) == IDENTIFIER_NODE)
9831 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9832 else if (DECL_CLASS_TEMPLATE_P (template)
9833 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9835 bool entering_scope;
9836 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9837 template (rather than some instantiation thereof) only if
9838 is not nested within some other construct. For example, in
9839 "template <typename T> void f(T) { A<T>::", A<T> is just an
9840 instantiation of A. */
9841 entering_scope = (template_parm_scope_p ()
9842 && cp_lexer_next_token_is (parser->lexer,
9845 = finish_template_type (template, arguments, entering_scope);
9849 /* If it's not a class-template or a template-template, it should be
9850 a function-template. */
9851 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9852 || TREE_CODE (template) == OVERLOAD
9853 || BASELINK_P (template)));
9855 template_id = lookup_template_function (template, arguments);
9858 /* If parsing tentatively, replace the sequence of tokens that makes
9859 up the template-id with a CPP_TEMPLATE_ID token. That way,
9860 should we re-parse the token stream, we will not have to repeat
9861 the effort required to do the parse, nor will we issue duplicate
9862 error messages about problems during instantiation of the
9866 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9868 /* Reset the contents of the START_OF_ID token. */
9869 token->type = CPP_TEMPLATE_ID;
9870 /* Retrieve any deferred checks. Do not pop this access checks yet
9871 so the memory will not be reclaimed during token replacing below. */
9872 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9873 token->u.tree_check_value->value = template_id;
9874 token->u.tree_check_value->checks = get_deferred_access_checks ();
9875 token->keyword = RID_MAX;
9877 /* Purge all subsequent tokens. */
9878 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9880 /* ??? Can we actually assume that, if template_id ==
9881 error_mark_node, we will have issued a diagnostic to the
9882 user, as opposed to simply marking the tentative parse as
9884 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9885 error ("parse error in template argument list");
9888 pop_deferring_access_checks ();
9892 /* Parse a template-name.
9897 The standard should actually say:
9901 operator-function-id
9903 A defect report has been filed about this issue.
9905 A conversion-function-id cannot be a template name because they cannot
9906 be part of a template-id. In fact, looking at this code:
9910 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9911 It is impossible to call a templated conversion-function-id with an
9912 explicit argument list, since the only allowed template parameter is
9913 the type to which it is converting.
9915 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9916 `template' keyword, in a construction like:
9920 In that case `f' is taken to be a template-name, even though there
9921 is no way of knowing for sure.
9923 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9924 name refers to a set of overloaded functions, at least one of which
9925 is a template, or an IDENTIFIER_NODE with the name of the template,
9926 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9927 names are looked up inside uninstantiated templates. */
9930 cp_parser_template_name (cp_parser* parser,
9931 bool template_keyword_p,
9932 bool check_dependency_p,
9933 bool is_declaration,
9934 bool *is_identifier)
9940 /* If the next token is `operator', then we have either an
9941 operator-function-id or a conversion-function-id. */
9942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9944 /* We don't know whether we're looking at an
9945 operator-function-id or a conversion-function-id. */
9946 cp_parser_parse_tentatively (parser);
9947 /* Try an operator-function-id. */
9948 identifier = cp_parser_operator_function_id (parser);
9949 /* If that didn't work, try a conversion-function-id. */
9950 if (!cp_parser_parse_definitely (parser))
9952 cp_parser_error (parser, "expected template-name");
9953 return error_mark_node;
9956 /* Look for the identifier. */
9958 identifier = cp_parser_identifier (parser);
9960 /* If we didn't find an identifier, we don't have a template-id. */
9961 if (identifier == error_mark_node)
9962 return error_mark_node;
9964 /* If the name immediately followed the `template' keyword, then it
9965 is a template-name. However, if the next token is not `<', then
9966 we do not treat it as a template-name, since it is not being used
9967 as part of a template-id. This enables us to handle constructs
9970 template <typename T> struct S { S(); };
9971 template <typename T> S<T>::S();
9973 correctly. We would treat `S' as a template -- if it were `S<T>'
9974 -- but we do not if there is no `<'. */
9976 if (processing_template_decl
9977 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9979 /* In a declaration, in a dependent context, we pretend that the
9980 "template" keyword was present in order to improve error
9981 recovery. For example, given:
9983 template <typename T> void f(T::X<int>);
9985 we want to treat "X<int>" as a template-id. */
9987 && !template_keyword_p
9988 && parser->scope && TYPE_P (parser->scope)
9989 && check_dependency_p
9990 && dependent_type_p (parser->scope)
9991 /* Do not do this for dtors (or ctors), since they never
9992 need the template keyword before their name. */
9993 && !constructor_name_p (identifier, parser->scope))
9995 cp_token_position start = 0;
9997 /* Explain what went wrong. */
9998 error ("non-template %qD used as template", identifier);
9999 inform ("use %<%T::template %D%> to indicate that it is a template",
10000 parser->scope, identifier);
10001 /* If parsing tentatively, find the location of the "<" token. */
10002 if (cp_parser_simulate_error (parser))
10003 start = cp_lexer_token_position (parser->lexer, true);
10004 /* Parse the template arguments so that we can issue error
10005 messages about them. */
10006 cp_lexer_consume_token (parser->lexer);
10007 cp_parser_enclosed_template_argument_list (parser);
10008 /* Skip tokens until we find a good place from which to
10009 continue parsing. */
10010 cp_parser_skip_to_closing_parenthesis (parser,
10011 /*recovering=*/true,
10013 /*consume_paren=*/false);
10014 /* If parsing tentatively, permanently remove the
10015 template argument list. That will prevent duplicate
10016 error messages from being issued about the missing
10017 "template" keyword. */
10019 cp_lexer_purge_tokens_after (parser->lexer, start);
10021 *is_identifier = true;
10025 /* If the "template" keyword is present, then there is generally
10026 no point in doing name-lookup, so we just return IDENTIFIER.
10027 But, if the qualifying scope is non-dependent then we can
10028 (and must) do name-lookup normally. */
10029 if (template_keyword_p
10031 || (TYPE_P (parser->scope)
10032 && dependent_type_p (parser->scope))))
10036 /* Look up the name. */
10037 decl = cp_parser_lookup_name (parser, identifier,
10039 /*is_template=*/false,
10040 /*is_namespace=*/false,
10041 check_dependency_p,
10042 /*ambiguous_decls=*/NULL);
10043 decl = maybe_get_template_decl_from_type_decl (decl);
10045 /* If DECL is a template, then the name was a template-name. */
10046 if (TREE_CODE (decl) == TEMPLATE_DECL)
10050 tree fn = NULL_TREE;
10052 /* The standard does not explicitly indicate whether a name that
10053 names a set of overloaded declarations, some of which are
10054 templates, is a template-name. However, such a name should
10055 be a template-name; otherwise, there is no way to form a
10056 template-id for the overloaded templates. */
10057 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10058 if (TREE_CODE (fns) == OVERLOAD)
10059 for (fn = fns; fn; fn = OVL_NEXT (fn))
10060 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10065 /* The name does not name a template. */
10066 cp_parser_error (parser, "expected template-name");
10067 return error_mark_node;
10071 /* If DECL is dependent, and refers to a function, then just return
10072 its name; we will look it up again during template instantiation. */
10073 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10075 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10076 if (TYPE_P (scope) && dependent_type_p (scope))
10083 /* Parse a template-argument-list.
10085 template-argument-list:
10086 template-argument ... [opt]
10087 template-argument-list , template-argument ... [opt]
10089 Returns a TREE_VEC containing the arguments. */
10092 cp_parser_template_argument_list (cp_parser* parser)
10094 tree fixed_args[10];
10095 unsigned n_args = 0;
10096 unsigned alloced = 10;
10097 tree *arg_ary = fixed_args;
10099 bool saved_in_template_argument_list_p;
10101 bool saved_non_ice_p;
10103 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10104 parser->in_template_argument_list_p = true;
10105 /* Even if the template-id appears in an integral
10106 constant-expression, the contents of the argument list do
10108 saved_ice_p = parser->integral_constant_expression_p;
10109 parser->integral_constant_expression_p = false;
10110 saved_non_ice_p = parser->non_integral_constant_expression_p;
10111 parser->non_integral_constant_expression_p = false;
10112 /* Parse the arguments. */
10118 /* Consume the comma. */
10119 cp_lexer_consume_token (parser->lexer);
10121 /* Parse the template-argument. */
10122 argument = cp_parser_template_argument (parser);
10124 /* If the next token is an ellipsis, we're expanding a template
10126 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10128 /* Consume the `...' token. */
10129 cp_lexer_consume_token (parser->lexer);
10131 /* Make the argument into a TYPE_PACK_EXPANSION or
10132 EXPR_PACK_EXPANSION. */
10133 argument = make_pack_expansion (argument);
10136 if (n_args == alloced)
10140 if (arg_ary == fixed_args)
10142 arg_ary = XNEWVEC (tree, alloced);
10143 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10146 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10148 arg_ary[n_args++] = argument;
10150 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10152 vec = make_tree_vec (n_args);
10155 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10157 if (arg_ary != fixed_args)
10159 parser->non_integral_constant_expression_p = saved_non_ice_p;
10160 parser->integral_constant_expression_p = saved_ice_p;
10161 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10165 /* Parse a template-argument.
10168 assignment-expression
10172 The representation is that of an assignment-expression, type-id, or
10173 id-expression -- except that the qualified id-expression is
10174 evaluated, so that the value returned is either a DECL or an
10177 Although the standard says "assignment-expression", it forbids
10178 throw-expressions or assignments in the template argument.
10179 Therefore, we use "conditional-expression" instead. */
10182 cp_parser_template_argument (cp_parser* parser)
10187 bool maybe_type_id = false;
10191 /* There's really no way to know what we're looking at, so we just
10192 try each alternative in order.
10196 In a template-argument, an ambiguity between a type-id and an
10197 expression is resolved to a type-id, regardless of the form of
10198 the corresponding template-parameter.
10200 Therefore, we try a type-id first. */
10201 cp_parser_parse_tentatively (parser);
10202 argument = cp_parser_type_id (parser);
10203 /* If there was no error parsing the type-id but the next token is a '>>',
10204 we probably found a typo for '> >'. But there are type-id which are
10205 also valid expressions. For instance:
10207 struct X { int operator >> (int); };
10208 template <int V> struct Foo {};
10211 Here 'X()' is a valid type-id of a function type, but the user just
10212 wanted to write the expression "X() >> 5". Thus, we remember that we
10213 found a valid type-id, but we still try to parse the argument as an
10214 expression to see what happens. */
10215 if (!cp_parser_error_occurred (parser)
10216 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10218 maybe_type_id = true;
10219 cp_parser_abort_tentative_parse (parser);
10223 /* If the next token isn't a `,' or a `>', then this argument wasn't
10224 really finished. This means that the argument is not a valid
10226 if (!cp_parser_next_token_ends_template_argument_p (parser))
10227 cp_parser_error (parser, "expected template-argument");
10228 /* If that worked, we're done. */
10229 if (cp_parser_parse_definitely (parser))
10232 /* We're still not sure what the argument will be. */
10233 cp_parser_parse_tentatively (parser);
10234 /* Try a template. */
10235 argument = cp_parser_id_expression (parser,
10236 /*template_keyword_p=*/false,
10237 /*check_dependency_p=*/true,
10239 /*declarator_p=*/false,
10240 /*optional_p=*/false);
10241 /* If the next token isn't a `,' or a `>', then this argument wasn't
10242 really finished. */
10243 if (!cp_parser_next_token_ends_template_argument_p (parser))
10244 cp_parser_error (parser, "expected template-argument");
10245 if (!cp_parser_error_occurred (parser))
10247 /* Figure out what is being referred to. If the id-expression
10248 was for a class template specialization, then we will have a
10249 TYPE_DECL at this point. There is no need to do name lookup
10250 at this point in that case. */
10251 if (TREE_CODE (argument) != TYPE_DECL)
10252 argument = cp_parser_lookup_name (parser, argument,
10254 /*is_template=*/template_p,
10255 /*is_namespace=*/false,
10256 /*check_dependency=*/true,
10257 /*ambiguous_decls=*/NULL);
10258 if (TREE_CODE (argument) != TEMPLATE_DECL
10259 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10260 cp_parser_error (parser, "expected template-name");
10262 if (cp_parser_parse_definitely (parser))
10264 /* It must be a non-type argument. There permitted cases are given
10265 in [temp.arg.nontype]:
10267 -- an integral constant-expression of integral or enumeration
10270 -- the name of a non-type template-parameter; or
10272 -- the name of an object or function with external linkage...
10274 -- the address of an object or function with external linkage...
10276 -- a pointer to member... */
10277 /* Look for a non-type template parameter. */
10278 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10280 cp_parser_parse_tentatively (parser);
10281 argument = cp_parser_primary_expression (parser,
10282 /*adress_p=*/false,
10284 /*template_arg_p=*/true,
10286 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10287 || !cp_parser_next_token_ends_template_argument_p (parser))
10288 cp_parser_simulate_error (parser);
10289 if (cp_parser_parse_definitely (parser))
10293 /* If the next token is "&", the argument must be the address of an
10294 object or function with external linkage. */
10295 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10297 cp_lexer_consume_token (parser->lexer);
10298 /* See if we might have an id-expression. */
10299 token = cp_lexer_peek_token (parser->lexer);
10300 if (token->type == CPP_NAME
10301 || token->keyword == RID_OPERATOR
10302 || token->type == CPP_SCOPE
10303 || token->type == CPP_TEMPLATE_ID
10304 || token->type == CPP_NESTED_NAME_SPECIFIER)
10306 cp_parser_parse_tentatively (parser);
10307 argument = cp_parser_primary_expression (parser,
10310 /*template_arg_p=*/true,
10312 if (cp_parser_error_occurred (parser)
10313 || !cp_parser_next_token_ends_template_argument_p (parser))
10314 cp_parser_abort_tentative_parse (parser);
10317 if (TREE_CODE (argument) == INDIRECT_REF)
10319 gcc_assert (REFERENCE_REF_P (argument));
10320 argument = TREE_OPERAND (argument, 0);
10323 if (TREE_CODE (argument) == VAR_DECL)
10325 /* A variable without external linkage might still be a
10326 valid constant-expression, so no error is issued here
10327 if the external-linkage check fails. */
10328 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10329 cp_parser_simulate_error (parser);
10331 else if (is_overloaded_fn (argument))
10332 /* All overloaded functions are allowed; if the external
10333 linkage test does not pass, an error will be issued
10337 && (TREE_CODE (argument) == OFFSET_REF
10338 || TREE_CODE (argument) == SCOPE_REF))
10339 /* A pointer-to-member. */
10341 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10344 cp_parser_simulate_error (parser);
10346 if (cp_parser_parse_definitely (parser))
10349 argument = build_x_unary_op (ADDR_EXPR, argument,
10350 tf_warning_or_error);
10355 /* If the argument started with "&", there are no other valid
10356 alternatives at this point. */
10359 cp_parser_error (parser, "invalid non-type template argument");
10360 return error_mark_node;
10363 /* If the argument wasn't successfully parsed as a type-id followed
10364 by '>>', the argument can only be a constant expression now.
10365 Otherwise, we try parsing the constant-expression tentatively,
10366 because the argument could really be a type-id. */
10368 cp_parser_parse_tentatively (parser);
10369 argument = cp_parser_constant_expression (parser,
10370 /*allow_non_constant_p=*/false,
10371 /*non_constant_p=*/NULL);
10372 argument = fold_non_dependent_expr (argument);
10373 if (!maybe_type_id)
10375 if (!cp_parser_next_token_ends_template_argument_p (parser))
10376 cp_parser_error (parser, "expected template-argument");
10377 if (cp_parser_parse_definitely (parser))
10379 /* We did our best to parse the argument as a non type-id, but that
10380 was the only alternative that matched (albeit with a '>' after
10381 it). We can assume it's just a typo from the user, and a
10382 diagnostic will then be issued. */
10383 return cp_parser_type_id (parser);
10386 /* Parse an explicit-instantiation.
10388 explicit-instantiation:
10389 template declaration
10391 Although the standard says `declaration', what it really means is:
10393 explicit-instantiation:
10394 template decl-specifier-seq [opt] declarator [opt] ;
10396 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10397 supposed to be allowed. A defect report has been filed about this
10402 explicit-instantiation:
10403 storage-class-specifier template
10404 decl-specifier-seq [opt] declarator [opt] ;
10405 function-specifier template
10406 decl-specifier-seq [opt] declarator [opt] ; */
10409 cp_parser_explicit_instantiation (cp_parser* parser)
10411 int declares_class_or_enum;
10412 cp_decl_specifier_seq decl_specifiers;
10413 tree extension_specifier = NULL_TREE;
10415 /* Look for an (optional) storage-class-specifier or
10416 function-specifier. */
10417 if (cp_parser_allow_gnu_extensions_p (parser))
10419 extension_specifier
10420 = cp_parser_storage_class_specifier_opt (parser);
10421 if (!extension_specifier)
10422 extension_specifier
10423 = cp_parser_function_specifier_opt (parser,
10424 /*decl_specs=*/NULL);
10427 /* Look for the `template' keyword. */
10428 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10429 /* Let the front end know that we are processing an explicit
10431 begin_explicit_instantiation ();
10432 /* [temp.explicit] says that we are supposed to ignore access
10433 control while processing explicit instantiation directives. */
10434 push_deferring_access_checks (dk_no_check);
10435 /* Parse a decl-specifier-seq. */
10436 cp_parser_decl_specifier_seq (parser,
10437 CP_PARSER_FLAGS_OPTIONAL,
10439 &declares_class_or_enum);
10440 /* If there was exactly one decl-specifier, and it declared a class,
10441 and there's no declarator, then we have an explicit type
10443 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10447 type = check_tag_decl (&decl_specifiers);
10448 /* Turn access control back on for names used during
10449 template instantiation. */
10450 pop_deferring_access_checks ();
10452 do_type_instantiation (type, extension_specifier,
10453 /*complain=*/tf_error);
10457 cp_declarator *declarator;
10460 /* Parse the declarator. */
10462 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10463 /*ctor_dtor_or_conv_p=*/NULL,
10464 /*parenthesized_p=*/NULL,
10465 /*member_p=*/false);
10466 if (declares_class_or_enum & 2)
10467 cp_parser_check_for_definition_in_return_type (declarator,
10468 decl_specifiers.type);
10469 if (declarator != cp_error_declarator)
10471 decl = grokdeclarator (declarator, &decl_specifiers,
10472 NORMAL, 0, &decl_specifiers.attributes);
10473 /* Turn access control back on for names used during
10474 template instantiation. */
10475 pop_deferring_access_checks ();
10476 /* Do the explicit instantiation. */
10477 do_decl_instantiation (decl, extension_specifier);
10481 pop_deferring_access_checks ();
10482 /* Skip the body of the explicit instantiation. */
10483 cp_parser_skip_to_end_of_statement (parser);
10486 /* We're done with the instantiation. */
10487 end_explicit_instantiation ();
10489 cp_parser_consume_semicolon_at_end_of_statement (parser);
10492 /* Parse an explicit-specialization.
10494 explicit-specialization:
10495 template < > declaration
10497 Although the standard says `declaration', what it really means is:
10499 explicit-specialization:
10500 template <> decl-specifier [opt] init-declarator [opt] ;
10501 template <> function-definition
10502 template <> explicit-specialization
10503 template <> template-declaration */
10506 cp_parser_explicit_specialization (cp_parser* parser)
10508 bool need_lang_pop;
10509 /* Look for the `template' keyword. */
10510 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10511 /* Look for the `<'. */
10512 cp_parser_require (parser, CPP_LESS, "`<'");
10513 /* Look for the `>'. */
10514 cp_parser_require (parser, CPP_GREATER, "`>'");
10515 /* We have processed another parameter list. */
10516 ++parser->num_template_parameter_lists;
10519 A template ... explicit specialization ... shall not have C
10521 if (current_lang_name == lang_name_c)
10523 error ("template specialization with C linkage");
10524 /* Give it C++ linkage to avoid confusing other parts of the
10526 push_lang_context (lang_name_cplusplus);
10527 need_lang_pop = true;
10530 need_lang_pop = false;
10531 /* Let the front end know that we are beginning a specialization. */
10532 if (!begin_specialization ())
10534 end_specialization ();
10535 cp_parser_skip_to_end_of_block_or_statement (parser);
10539 /* If the next keyword is `template', we need to figure out whether
10540 or not we're looking a template-declaration. */
10541 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10543 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10544 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10545 cp_parser_template_declaration_after_export (parser,
10546 /*member_p=*/false);
10548 cp_parser_explicit_specialization (parser);
10551 /* Parse the dependent declaration. */
10552 cp_parser_single_declaration (parser,
10554 /*member_p=*/false,
10555 /*explicit_specialization_p=*/true,
10556 /*friend_p=*/NULL);
10557 /* We're done with the specialization. */
10558 end_specialization ();
10559 /* For the erroneous case of a template with C linkage, we pushed an
10560 implicit C++ linkage scope; exit that scope now. */
10562 pop_lang_context ();
10563 /* We're done with this parameter list. */
10564 --parser->num_template_parameter_lists;
10567 /* Parse a type-specifier.
10570 simple-type-specifier
10573 elaborated-type-specifier
10581 Returns a representation of the type-specifier. For a
10582 class-specifier, enum-specifier, or elaborated-type-specifier, a
10583 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10585 The parser flags FLAGS is used to control type-specifier parsing.
10587 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10588 in a decl-specifier-seq.
10590 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10591 class-specifier, enum-specifier, or elaborated-type-specifier, then
10592 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10593 if a type is declared; 2 if it is defined. Otherwise, it is set to
10596 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10597 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10598 is set to FALSE. */
10601 cp_parser_type_specifier (cp_parser* parser,
10602 cp_parser_flags flags,
10603 cp_decl_specifier_seq *decl_specs,
10604 bool is_declaration,
10605 int* declares_class_or_enum,
10606 bool* is_cv_qualifier)
10608 tree type_spec = NULL_TREE;
10611 cp_decl_spec ds = ds_last;
10613 /* Assume this type-specifier does not declare a new type. */
10614 if (declares_class_or_enum)
10615 *declares_class_or_enum = 0;
10616 /* And that it does not specify a cv-qualifier. */
10617 if (is_cv_qualifier)
10618 *is_cv_qualifier = false;
10619 /* Peek at the next token. */
10620 token = cp_lexer_peek_token (parser->lexer);
10622 /* If we're looking at a keyword, we can use that to guide the
10623 production we choose. */
10624 keyword = token->keyword;
10628 /* Look for the enum-specifier. */
10629 type_spec = cp_parser_enum_specifier (parser);
10630 /* If that worked, we're done. */
10633 if (declares_class_or_enum)
10634 *declares_class_or_enum = 2;
10636 cp_parser_set_decl_spec_type (decl_specs,
10638 /*user_defined_p=*/true);
10642 goto elaborated_type_specifier;
10644 /* Any of these indicate either a class-specifier, or an
10645 elaborated-type-specifier. */
10649 /* Parse tentatively so that we can back up if we don't find a
10650 class-specifier. */
10651 cp_parser_parse_tentatively (parser);
10652 /* Look for the class-specifier. */
10653 type_spec = cp_parser_class_specifier (parser);
10654 /* If that worked, we're done. */
10655 if (cp_parser_parse_definitely (parser))
10657 if (declares_class_or_enum)
10658 *declares_class_or_enum = 2;
10660 cp_parser_set_decl_spec_type (decl_specs,
10662 /*user_defined_p=*/true);
10666 /* Fall through. */
10667 elaborated_type_specifier:
10668 /* We're declaring (not defining) a class or enum. */
10669 if (declares_class_or_enum)
10670 *declares_class_or_enum = 1;
10672 /* Fall through. */
10674 /* Look for an elaborated-type-specifier. */
10676 = (cp_parser_elaborated_type_specifier
10678 decl_specs && decl_specs->specs[(int) ds_friend],
10681 cp_parser_set_decl_spec_type (decl_specs,
10683 /*user_defined_p=*/true);
10688 if (is_cv_qualifier)
10689 *is_cv_qualifier = true;
10694 if (is_cv_qualifier)
10695 *is_cv_qualifier = true;
10700 if (is_cv_qualifier)
10701 *is_cv_qualifier = true;
10705 /* The `__complex__' keyword is a GNU extension. */
10713 /* Handle simple keywords. */
10718 ++decl_specs->specs[(int)ds];
10719 decl_specs->any_specifiers_p = true;
10721 return cp_lexer_consume_token (parser->lexer)->u.value;
10724 /* If we do not already have a type-specifier, assume we are looking
10725 at a simple-type-specifier. */
10726 type_spec = cp_parser_simple_type_specifier (parser,
10730 /* If we didn't find a type-specifier, and a type-specifier was not
10731 optional in this context, issue an error message. */
10732 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10734 cp_parser_error (parser, "expected type specifier");
10735 return error_mark_node;
10741 /* Parse a simple-type-specifier.
10743 simple-type-specifier:
10744 :: [opt] nested-name-specifier [opt] type-name
10745 :: [opt] nested-name-specifier template template-id
10760 simple-type-specifier:
10762 decltype ( expression )
10766 simple-type-specifier:
10767 __typeof__ unary-expression
10768 __typeof__ ( type-id )
10770 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10771 appropriately updated. */
10774 cp_parser_simple_type_specifier (cp_parser* parser,
10775 cp_decl_specifier_seq *decl_specs,
10776 cp_parser_flags flags)
10778 tree type = NULL_TREE;
10781 /* Peek at the next token. */
10782 token = cp_lexer_peek_token (parser->lexer);
10784 /* If we're looking at a keyword, things are easy. */
10785 switch (token->keyword)
10789 decl_specs->explicit_char_p = true;
10790 type = char_type_node;
10793 type = wchar_type_node;
10796 type = boolean_type_node;
10800 ++decl_specs->specs[(int) ds_short];
10801 type = short_integer_type_node;
10805 decl_specs->explicit_int_p = true;
10806 type = integer_type_node;
10810 ++decl_specs->specs[(int) ds_long];
10811 type = long_integer_type_node;
10815 ++decl_specs->specs[(int) ds_signed];
10816 type = integer_type_node;
10820 ++decl_specs->specs[(int) ds_unsigned];
10821 type = unsigned_type_node;
10824 type = float_type_node;
10827 type = double_type_node;
10830 type = void_type_node;
10834 if (cxx_dialect != cxx98)
10836 /* Consume the token. */
10837 cp_lexer_consume_token (parser->lexer);
10838 /* We do not yet support the use of `auto' as a
10840 error ("C++0x %<auto%> specifier not supported");
10845 /* Parse the `decltype' type. */
10846 type = cp_parser_decltype (parser);
10849 cp_parser_set_decl_spec_type (decl_specs, type,
10850 /*user_defined_p=*/true);
10855 /* Consume the `typeof' token. */
10856 cp_lexer_consume_token (parser->lexer);
10857 /* Parse the operand to `typeof'. */
10858 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10859 /* If it is not already a TYPE, take its type. */
10860 if (!TYPE_P (type))
10861 type = finish_typeof (type);
10864 cp_parser_set_decl_spec_type (decl_specs, type,
10865 /*user_defined_p=*/true);
10873 /* If the type-specifier was for a built-in type, we're done. */
10878 /* Record the type. */
10880 && (token->keyword != RID_SIGNED
10881 && token->keyword != RID_UNSIGNED
10882 && token->keyword != RID_SHORT
10883 && token->keyword != RID_LONG))
10884 cp_parser_set_decl_spec_type (decl_specs,
10886 /*user_defined=*/false);
10888 decl_specs->any_specifiers_p = true;
10890 /* Consume the token. */
10891 id = cp_lexer_consume_token (parser->lexer)->u.value;
10893 /* There is no valid C++ program where a non-template type is
10894 followed by a "<". That usually indicates that the user thought
10895 that the type was a template. */
10896 cp_parser_check_for_invalid_template_id (parser, type);
10898 return TYPE_NAME (type);
10901 /* The type-specifier must be a user-defined type. */
10902 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10907 /* Don't gobble tokens or issue error messages if this is an
10908 optional type-specifier. */
10909 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10910 cp_parser_parse_tentatively (parser);
10912 /* Look for the optional `::' operator. */
10914 = (cp_parser_global_scope_opt (parser,
10915 /*current_scope_valid_p=*/false)
10917 /* Look for the nested-name specifier. */
10919 = (cp_parser_nested_name_specifier_opt (parser,
10920 /*typename_keyword_p=*/false,
10921 /*check_dependency_p=*/true,
10923 /*is_declaration=*/false)
10925 /* If we have seen a nested-name-specifier, and the next token
10926 is `template', then we are using the template-id production. */
10928 && cp_parser_optional_template_keyword (parser))
10930 /* Look for the template-id. */
10931 type = cp_parser_template_id (parser,
10932 /*template_keyword_p=*/true,
10933 /*check_dependency_p=*/true,
10934 /*is_declaration=*/false);
10935 /* If the template-id did not name a type, we are out of
10937 if (TREE_CODE (type) != TYPE_DECL)
10939 cp_parser_error (parser, "expected template-id for type");
10943 /* Otherwise, look for a type-name. */
10945 type = cp_parser_type_name (parser);
10946 /* Keep track of all name-lookups performed in class scopes. */
10950 && TREE_CODE (type) == TYPE_DECL
10951 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10952 maybe_note_name_used_in_class (DECL_NAME (type), type);
10953 /* If it didn't work out, we don't have a TYPE. */
10954 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10955 && !cp_parser_parse_definitely (parser))
10957 if (type && decl_specs)
10958 cp_parser_set_decl_spec_type (decl_specs, type,
10959 /*user_defined=*/true);
10962 /* If we didn't get a type-name, issue an error message. */
10963 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10965 cp_parser_error (parser, "expected type-name");
10966 return error_mark_node;
10969 /* There is no valid C++ program where a non-template type is
10970 followed by a "<". That usually indicates that the user thought
10971 that the type was a template. */
10972 if (type && type != error_mark_node)
10974 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10975 If it is, then the '<'...'>' enclose protocol names rather than
10976 template arguments, and so everything is fine. */
10977 if (c_dialect_objc ()
10978 && (objc_is_id (type) || objc_is_class_name (type)))
10980 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10981 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10983 /* Clobber the "unqualified" type previously entered into
10984 DECL_SPECS with the new, improved protocol-qualified version. */
10986 decl_specs->type = qual_type;
10991 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10997 /* Parse a type-name.
11010 Returns a TYPE_DECL for the type. */
11013 cp_parser_type_name (cp_parser* parser)
11017 /* We can't know yet whether it is a class-name or not. */
11018 cp_parser_parse_tentatively (parser);
11019 /* Try a class-name. */
11020 type_decl = cp_parser_class_name (parser,
11021 /*typename_keyword_p=*/false,
11022 /*template_keyword_p=*/false,
11024 /*check_dependency_p=*/true,
11025 /*class_head_p=*/false,
11026 /*is_declaration=*/false);
11027 /* If it's not a class-name, keep looking. */
11028 if (!cp_parser_parse_definitely (parser))
11030 /* It must be a typedef-name or an enum-name. */
11031 return cp_parser_nonclass_name (parser);
11037 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11045 Returns a TYPE_DECL for the type. */
11048 cp_parser_nonclass_name (cp_parser* parser)
11053 identifier = cp_parser_identifier (parser);
11054 if (identifier == error_mark_node)
11055 return error_mark_node;
11057 /* Look up the type-name. */
11058 type_decl = cp_parser_lookup_name_simple (parser, identifier);
11060 if (TREE_CODE (type_decl) != TYPE_DECL
11061 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11063 /* See if this is an Objective-C type. */
11064 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11065 tree type = objc_get_protocol_qualified_type (identifier, protos);
11067 type_decl = TYPE_NAME (type);
11070 /* Issue an error if we did not find a type-name. */
11071 if (TREE_CODE (type_decl) != TYPE_DECL)
11073 if (!cp_parser_simulate_error (parser))
11074 cp_parser_name_lookup_error (parser, identifier, type_decl,
11076 return error_mark_node;
11078 /* Remember that the name was used in the definition of the
11079 current class so that we can check later to see if the
11080 meaning would have been different after the class was
11081 entirely defined. */
11082 else if (type_decl != error_mark_node
11084 maybe_note_name_used_in_class (identifier, type_decl);
11089 /* Parse an elaborated-type-specifier. Note that the grammar given
11090 here incorporates the resolution to DR68.
11092 elaborated-type-specifier:
11093 class-key :: [opt] nested-name-specifier [opt] identifier
11094 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11095 enum :: [opt] nested-name-specifier [opt] identifier
11096 typename :: [opt] nested-name-specifier identifier
11097 typename :: [opt] nested-name-specifier template [opt]
11102 elaborated-type-specifier:
11103 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11104 class-key attributes :: [opt] nested-name-specifier [opt]
11105 template [opt] template-id
11106 enum attributes :: [opt] nested-name-specifier [opt] identifier
11108 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11109 declared `friend'. If IS_DECLARATION is TRUE, then this
11110 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11111 something is being declared.
11113 Returns the TYPE specified. */
11116 cp_parser_elaborated_type_specifier (cp_parser* parser,
11118 bool is_declaration)
11120 enum tag_types tag_type;
11122 tree type = NULL_TREE;
11123 tree attributes = NULL_TREE;
11125 /* See if we're looking at the `enum' keyword. */
11126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11128 /* Consume the `enum' token. */
11129 cp_lexer_consume_token (parser->lexer);
11130 /* Remember that it's an enumeration type. */
11131 tag_type = enum_type;
11132 /* Parse the attributes. */
11133 attributes = cp_parser_attributes_opt (parser);
11135 /* Or, it might be `typename'. */
11136 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11139 /* Consume the `typename' token. */
11140 cp_lexer_consume_token (parser->lexer);
11141 /* Remember that it's a `typename' type. */
11142 tag_type = typename_type;
11143 /* The `typename' keyword is only allowed in templates. */
11144 if (!processing_template_decl)
11145 pedwarn ("using %<typename%> outside of template");
11147 /* Otherwise it must be a class-key. */
11150 tag_type = cp_parser_class_key (parser);
11151 if (tag_type == none_type)
11152 return error_mark_node;
11153 /* Parse the attributes. */
11154 attributes = cp_parser_attributes_opt (parser);
11157 /* Look for the `::' operator. */
11158 cp_parser_global_scope_opt (parser,
11159 /*current_scope_valid_p=*/false);
11160 /* Look for the nested-name-specifier. */
11161 if (tag_type == typename_type)
11163 if (!cp_parser_nested_name_specifier (parser,
11164 /*typename_keyword_p=*/true,
11165 /*check_dependency_p=*/true,
11168 return error_mark_node;
11171 /* Even though `typename' is not present, the proposed resolution
11172 to Core Issue 180 says that in `class A<T>::B', `B' should be
11173 considered a type-name, even if `A<T>' is dependent. */
11174 cp_parser_nested_name_specifier_opt (parser,
11175 /*typename_keyword_p=*/true,
11176 /*check_dependency_p=*/true,
11179 /* For everything but enumeration types, consider a template-id.
11180 For an enumeration type, consider only a plain identifier. */
11181 if (tag_type != enum_type)
11183 bool template_p = false;
11186 /* Allow the `template' keyword. */
11187 template_p = cp_parser_optional_template_keyword (parser);
11188 /* If we didn't see `template', we don't know if there's a
11189 template-id or not. */
11191 cp_parser_parse_tentatively (parser);
11192 /* Parse the template-id. */
11193 decl = cp_parser_template_id (parser, template_p,
11194 /*check_dependency_p=*/true,
11196 /* If we didn't find a template-id, look for an ordinary
11198 if (!template_p && !cp_parser_parse_definitely (parser))
11200 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11201 in effect, then we must assume that, upon instantiation, the
11202 template will correspond to a class. */
11203 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11204 && tag_type == typename_type)
11205 type = make_typename_type (parser->scope, decl,
11207 /*complain=*/tf_error);
11209 type = TREE_TYPE (decl);
11214 identifier = cp_parser_identifier (parser);
11216 if (identifier == error_mark_node)
11218 parser->scope = NULL_TREE;
11219 return error_mark_node;
11222 /* For a `typename', we needn't call xref_tag. */
11223 if (tag_type == typename_type
11224 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11225 return cp_parser_make_typename_type (parser, parser->scope,
11227 /* Look up a qualified name in the usual way. */
11231 tree ambiguous_decls;
11233 decl = cp_parser_lookup_name (parser, identifier,
11235 /*is_template=*/false,
11236 /*is_namespace=*/false,
11237 /*check_dependency=*/true,
11240 /* If the lookup was ambiguous, an error will already have been
11242 if (ambiguous_decls)
11243 return error_mark_node;
11245 /* If we are parsing friend declaration, DECL may be a
11246 TEMPLATE_DECL tree node here. However, we need to check
11247 whether this TEMPLATE_DECL results in valid code. Consider
11248 the following example:
11251 template <class T> class C {};
11254 template <class T> friend class N::C; // #1, valid code
11256 template <class T> class Y {
11257 friend class N::C; // #2, invalid code
11260 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11261 name lookup of `N::C'. We see that friend declaration must
11262 be template for the code to be valid. Note that
11263 processing_template_decl does not work here since it is
11264 always 1 for the above two cases. */
11266 decl = (cp_parser_maybe_treat_template_as_class
11267 (decl, /*tag_name_p=*/is_friend
11268 && parser->num_template_parameter_lists));
11270 if (TREE_CODE (decl) != TYPE_DECL)
11272 cp_parser_diagnose_invalid_type_name (parser,
11275 return error_mark_node;
11278 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11280 bool allow_template = (parser->num_template_parameter_lists
11281 || DECL_SELF_REFERENCE_P (decl));
11282 type = check_elaborated_type_specifier (tag_type, decl,
11285 if (type == error_mark_node)
11286 return error_mark_node;
11289 /* Forward declarations of nested types, such as
11294 are invalid unless all components preceding the final '::'
11295 are complete. If all enclosing types are complete, these
11296 declarations become merely pointless.
11298 Invalid forward declarations of nested types are errors
11299 caught elsewhere in parsing. Those that are pointless arrive
11302 if (cp_parser_declares_only_class_p (parser)
11303 && !is_friend && !processing_explicit_instantiation)
11304 warning (0, "declaration %qD does not declare anything", decl);
11306 type = TREE_TYPE (decl);
11310 /* An elaborated-type-specifier sometimes introduces a new type and
11311 sometimes names an existing type. Normally, the rule is that it
11312 introduces a new type only if there is not an existing type of
11313 the same name already in scope. For example, given:
11316 void f() { struct S s; }
11318 the `struct S' in the body of `f' is the same `struct S' as in
11319 the global scope; the existing definition is used. However, if
11320 there were no global declaration, this would introduce a new
11321 local class named `S'.
11323 An exception to this rule applies to the following code:
11325 namespace N { struct S; }
11327 Here, the elaborated-type-specifier names a new type
11328 unconditionally; even if there is already an `S' in the
11329 containing scope this declaration names a new type.
11330 This exception only applies if the elaborated-type-specifier
11331 forms the complete declaration:
11335 A declaration consisting solely of `class-key identifier ;' is
11336 either a redeclaration of the name in the current scope or a
11337 forward declaration of the identifier as a class name. It
11338 introduces the name into the current scope.
11340 We are in this situation precisely when the next token is a `;'.
11342 An exception to the exception is that a `friend' declaration does
11343 *not* name a new type; i.e., given:
11345 struct S { friend struct T; };
11347 `T' is not a new type in the scope of `S'.
11349 Also, `new struct S' or `sizeof (struct S)' never results in the
11350 definition of a new type; a new type can only be declared in a
11351 declaration context. */
11357 /* Friends have special name lookup rules. */
11358 ts = ts_within_enclosing_non_class;
11359 else if (is_declaration
11360 && cp_lexer_next_token_is (parser->lexer,
11362 /* This is a `class-key identifier ;' */
11368 (parser->num_template_parameter_lists
11369 && (cp_parser_next_token_starts_class_definition_p (parser)
11370 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11371 /* An unqualified name was used to reference this type, so
11372 there were no qualifying templates. */
11373 if (!cp_parser_check_template_parameters (parser,
11374 /*num_templates=*/0))
11375 return error_mark_node;
11376 type = xref_tag (tag_type, identifier, ts, template_p);
11380 if (type == error_mark_node)
11381 return error_mark_node;
11383 /* Allow attributes on forward declarations of classes. */
11386 if (TREE_CODE (type) == TYPENAME_TYPE)
11387 warning (OPT_Wattributes,
11388 "attributes ignored on uninstantiated type");
11389 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11390 && ! processing_explicit_instantiation)
11391 warning (OPT_Wattributes,
11392 "attributes ignored on template instantiation");
11393 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11394 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11396 warning (OPT_Wattributes,
11397 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11400 if (tag_type != enum_type)
11401 cp_parser_check_class_key (tag_type, type);
11403 /* A "<" cannot follow an elaborated type specifier. If that
11404 happens, the user was probably trying to form a template-id. */
11405 cp_parser_check_for_invalid_template_id (parser, type);
11410 /* Parse an enum-specifier.
11413 enum identifier [opt] { enumerator-list [opt] }
11416 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11419 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11420 if the token stream isn't an enum-specifier after all. */
11423 cp_parser_enum_specifier (cp_parser* parser)
11429 /* Parse tentatively so that we can back up if we don't find a
11431 cp_parser_parse_tentatively (parser);
11433 /* Caller guarantees that the current token is 'enum', an identifier
11434 possibly follows, and the token after that is an opening brace.
11435 If we don't have an identifier, fabricate an anonymous name for
11436 the enumeration being defined. */
11437 cp_lexer_consume_token (parser->lexer);
11439 attributes = cp_parser_attributes_opt (parser);
11441 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11442 identifier = cp_parser_identifier (parser);
11444 identifier = make_anon_name ();
11446 /* Look for the `{' but don't consume it yet. */
11447 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11448 cp_parser_simulate_error (parser);
11450 if (!cp_parser_parse_definitely (parser))
11453 /* Issue an error message if type-definitions are forbidden here. */
11454 if (!cp_parser_check_type_definition (parser))
11455 type = error_mark_node;
11457 /* Create the new type. We do this before consuming the opening
11458 brace so the enum will be recorded as being on the line of its
11459 tag (or the 'enum' keyword, if there is no tag). */
11460 type = start_enum (identifier);
11462 /* Consume the opening brace. */
11463 cp_lexer_consume_token (parser->lexer);
11465 if (type == error_mark_node)
11467 cp_parser_skip_to_end_of_block_or_statement (parser);
11468 return error_mark_node;
11471 /* If the next token is not '}', then there are some enumerators. */
11472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11473 cp_parser_enumerator_list (parser, type);
11475 /* Consume the final '}'. */
11476 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11478 /* Look for trailing attributes to apply to this enumeration, and
11479 apply them if appropriate. */
11480 if (cp_parser_allow_gnu_extensions_p (parser))
11482 tree trailing_attr = cp_parser_attributes_opt (parser);
11483 cplus_decl_attributes (&type,
11485 (int) ATTR_FLAG_TYPE_IN_PLACE);
11488 /* Finish up the enumeration. */
11489 finish_enum (type);
11494 /* Parse an enumerator-list. The enumerators all have the indicated
11498 enumerator-definition
11499 enumerator-list , enumerator-definition */
11502 cp_parser_enumerator_list (cp_parser* parser, tree type)
11506 /* Parse an enumerator-definition. */
11507 cp_parser_enumerator_definition (parser, type);
11509 /* If the next token is not a ',', we've reached the end of
11511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11513 /* Otherwise, consume the `,' and keep going. */
11514 cp_lexer_consume_token (parser->lexer);
11515 /* If the next token is a `}', there is a trailing comma. */
11516 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11518 if (pedantic && !in_system_header)
11519 pedwarn ("comma at end of enumerator list");
11525 /* Parse an enumerator-definition. The enumerator has the indicated
11528 enumerator-definition:
11530 enumerator = constant-expression
11536 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11541 /* Look for the identifier. */
11542 identifier = cp_parser_identifier (parser);
11543 if (identifier == error_mark_node)
11546 /* If the next token is an '=', then there is an explicit value. */
11547 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11549 /* Consume the `=' token. */
11550 cp_lexer_consume_token (parser->lexer);
11551 /* Parse the value. */
11552 value = cp_parser_constant_expression (parser,
11553 /*allow_non_constant_p=*/false,
11559 /* Create the enumerator. */
11560 build_enumerator (identifier, value, type);
11563 /* Parse a namespace-name.
11566 original-namespace-name
11569 Returns the NAMESPACE_DECL for the namespace. */
11572 cp_parser_namespace_name (cp_parser* parser)
11575 tree namespace_decl;
11577 /* Get the name of the namespace. */
11578 identifier = cp_parser_identifier (parser);
11579 if (identifier == error_mark_node)
11580 return error_mark_node;
11582 /* Look up the identifier in the currently active scope. Look only
11583 for namespaces, due to:
11585 [basic.lookup.udir]
11587 When looking up a namespace-name in a using-directive or alias
11588 definition, only namespace names are considered.
11592 [basic.lookup.qual]
11594 During the lookup of a name preceding the :: scope resolution
11595 operator, object, function, and enumerator names are ignored.
11597 (Note that cp_parser_class_or_namespace_name only calls this
11598 function if the token after the name is the scope resolution
11600 namespace_decl = cp_parser_lookup_name (parser, identifier,
11602 /*is_template=*/false,
11603 /*is_namespace=*/true,
11604 /*check_dependency=*/true,
11605 /*ambiguous_decls=*/NULL);
11606 /* If it's not a namespace, issue an error. */
11607 if (namespace_decl == error_mark_node
11608 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11610 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11611 error ("%qD is not a namespace-name", identifier);
11612 cp_parser_error (parser, "expected namespace-name");
11613 namespace_decl = error_mark_node;
11616 return namespace_decl;
11619 /* Parse a namespace-definition.
11621 namespace-definition:
11622 named-namespace-definition
11623 unnamed-namespace-definition
11625 named-namespace-definition:
11626 original-namespace-definition
11627 extension-namespace-definition
11629 original-namespace-definition:
11630 namespace identifier { namespace-body }
11632 extension-namespace-definition:
11633 namespace original-namespace-name { namespace-body }
11635 unnamed-namespace-definition:
11636 namespace { namespace-body } */
11639 cp_parser_namespace_definition (cp_parser* parser)
11641 tree identifier, attribs;
11642 bool has_visibility;
11645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11648 cp_lexer_consume_token (parser->lexer);
11653 /* Look for the `namespace' keyword. */
11654 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11656 /* Get the name of the namespace. We do not attempt to distinguish
11657 between an original-namespace-definition and an
11658 extension-namespace-definition at this point. The semantic
11659 analysis routines are responsible for that. */
11660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11661 identifier = cp_parser_identifier (parser);
11663 identifier = NULL_TREE;
11665 /* Parse any specified attributes. */
11666 attribs = cp_parser_attributes_opt (parser);
11668 /* Look for the `{' to start the namespace. */
11669 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11670 /* Start the namespace. */
11671 push_namespace (identifier);
11673 /* "inline namespace" is equivalent to a stub namespace definition
11674 followed by a strong using directive. */
11677 tree namespace = current_namespace;
11678 /* Set up namespace association. */
11679 DECL_NAMESPACE_ASSOCIATIONS (namespace)
11680 = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11681 DECL_NAMESPACE_ASSOCIATIONS (namespace));
11682 /* Import the contents of the inline namespace. */
11684 do_using_directive (namespace);
11685 push_namespace (identifier);
11688 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11690 /* Parse the body of the namespace. */
11691 cp_parser_namespace_body (parser);
11693 #ifdef HANDLE_PRAGMA_VISIBILITY
11694 if (has_visibility)
11698 /* Finish the namespace. */
11700 /* Look for the final `}'. */
11701 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11704 /* Parse a namespace-body.
11707 declaration-seq [opt] */
11710 cp_parser_namespace_body (cp_parser* parser)
11712 cp_parser_declaration_seq_opt (parser);
11715 /* Parse a namespace-alias-definition.
11717 namespace-alias-definition:
11718 namespace identifier = qualified-namespace-specifier ; */
11721 cp_parser_namespace_alias_definition (cp_parser* parser)
11724 tree namespace_specifier;
11726 /* Look for the `namespace' keyword. */
11727 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11728 /* Look for the identifier. */
11729 identifier = cp_parser_identifier (parser);
11730 if (identifier == error_mark_node)
11732 /* Look for the `=' token. */
11733 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11734 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11736 error ("%<namespace%> definition is not allowed here");
11737 /* Skip the definition. */
11738 cp_lexer_consume_token (parser->lexer);
11739 if (cp_parser_skip_to_closing_brace (parser))
11740 cp_lexer_consume_token (parser->lexer);
11743 cp_parser_require (parser, CPP_EQ, "`='");
11744 /* Look for the qualified-namespace-specifier. */
11745 namespace_specifier
11746 = cp_parser_qualified_namespace_specifier (parser);
11747 /* Look for the `;' token. */
11748 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11750 /* Register the alias in the symbol table. */
11751 do_namespace_alias (identifier, namespace_specifier);
11754 /* Parse a qualified-namespace-specifier.
11756 qualified-namespace-specifier:
11757 :: [opt] nested-name-specifier [opt] namespace-name
11759 Returns a NAMESPACE_DECL corresponding to the specified
11763 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11765 /* Look for the optional `::'. */
11766 cp_parser_global_scope_opt (parser,
11767 /*current_scope_valid_p=*/false);
11769 /* Look for the optional nested-name-specifier. */
11770 cp_parser_nested_name_specifier_opt (parser,
11771 /*typename_keyword_p=*/false,
11772 /*check_dependency_p=*/true,
11774 /*is_declaration=*/true);
11776 return cp_parser_namespace_name (parser);
11779 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11780 access declaration.
11783 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11784 using :: unqualified-id ;
11786 access-declaration:
11792 cp_parser_using_declaration (cp_parser* parser,
11793 bool access_declaration_p)
11796 bool typename_p = false;
11797 bool global_scope_p;
11802 if (access_declaration_p)
11803 cp_parser_parse_tentatively (parser);
11806 /* Look for the `using' keyword. */
11807 cp_parser_require_keyword (parser, RID_USING, "`using'");
11809 /* Peek at the next token. */
11810 token = cp_lexer_peek_token (parser->lexer);
11811 /* See if it's `typename'. */
11812 if (token->keyword == RID_TYPENAME)
11814 /* Remember that we've seen it. */
11816 /* Consume the `typename' token. */
11817 cp_lexer_consume_token (parser->lexer);
11821 /* Look for the optional global scope qualification. */
11823 = (cp_parser_global_scope_opt (parser,
11824 /*current_scope_valid_p=*/false)
11827 /* If we saw `typename', or didn't see `::', then there must be a
11828 nested-name-specifier present. */
11829 if (typename_p || !global_scope_p)
11830 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11831 /*check_dependency_p=*/true,
11833 /*is_declaration=*/true);
11834 /* Otherwise, we could be in either of the two productions. In that
11835 case, treat the nested-name-specifier as optional. */
11837 qscope = cp_parser_nested_name_specifier_opt (parser,
11838 /*typename_keyword_p=*/false,
11839 /*check_dependency_p=*/true,
11841 /*is_declaration=*/true);
11843 qscope = global_namespace;
11845 if (access_declaration_p && cp_parser_error_occurred (parser))
11846 /* Something has already gone wrong; there's no need to parse
11847 further. Since an error has occurred, the return value of
11848 cp_parser_parse_definitely will be false, as required. */
11849 return cp_parser_parse_definitely (parser);
11851 /* Parse the unqualified-id. */
11852 identifier = cp_parser_unqualified_id (parser,
11853 /*template_keyword_p=*/false,
11854 /*check_dependency_p=*/true,
11855 /*declarator_p=*/true,
11856 /*optional_p=*/false);
11858 if (access_declaration_p)
11860 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11861 cp_parser_simulate_error (parser);
11862 if (!cp_parser_parse_definitely (parser))
11866 /* The function we call to handle a using-declaration is different
11867 depending on what scope we are in. */
11868 if (qscope == error_mark_node || identifier == error_mark_node)
11870 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11871 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11872 /* [namespace.udecl]
11874 A using declaration shall not name a template-id. */
11875 error ("a template-id may not appear in a using-declaration");
11878 if (at_class_scope_p ())
11880 /* Create the USING_DECL. */
11881 decl = do_class_using_decl (parser->scope, identifier);
11883 if (check_for_bare_parameter_packs (decl))
11886 /* Add it to the list of members in this class. */
11887 finish_member_declaration (decl);
11891 decl = cp_parser_lookup_name_simple (parser, identifier);
11892 if (decl == error_mark_node)
11893 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11894 else if (check_for_bare_parameter_packs (decl))
11896 else if (!at_namespace_scope_p ())
11897 do_local_using_decl (decl, qscope, identifier);
11899 do_toplevel_using_decl (decl, qscope, identifier);
11903 /* Look for the final `;'. */
11904 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11909 /* Parse a using-directive.
11912 using namespace :: [opt] nested-name-specifier [opt]
11913 namespace-name ; */
11916 cp_parser_using_directive (cp_parser* parser)
11918 tree namespace_decl;
11921 /* Look for the `using' keyword. */
11922 cp_parser_require_keyword (parser, RID_USING, "`using'");
11923 /* And the `namespace' keyword. */
11924 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11925 /* Look for the optional `::' operator. */
11926 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11927 /* And the optional nested-name-specifier. */
11928 cp_parser_nested_name_specifier_opt (parser,
11929 /*typename_keyword_p=*/false,
11930 /*check_dependency_p=*/true,
11932 /*is_declaration=*/true);
11933 /* Get the namespace being used. */
11934 namespace_decl = cp_parser_namespace_name (parser);
11935 /* And any specified attributes. */
11936 attribs = cp_parser_attributes_opt (parser);
11937 /* Update the symbol table. */
11938 parse_using_directive (namespace_decl, attribs);
11939 /* Look for the final `;'. */
11940 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11943 /* Parse an asm-definition.
11946 asm ( string-literal ) ;
11951 asm volatile [opt] ( string-literal ) ;
11952 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11953 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11954 : asm-operand-list [opt] ) ;
11955 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11956 : asm-operand-list [opt]
11957 : asm-operand-list [opt] ) ; */
11960 cp_parser_asm_definition (cp_parser* parser)
11963 tree outputs = NULL_TREE;
11964 tree inputs = NULL_TREE;
11965 tree clobbers = NULL_TREE;
11967 bool volatile_p = false;
11968 bool extended_p = false;
11969 bool invalid_inputs_p = false;
11970 bool invalid_outputs_p = false;
11972 /* Look for the `asm' keyword. */
11973 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11974 /* See if the next token is `volatile'. */
11975 if (cp_parser_allow_gnu_extensions_p (parser)
11976 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11978 /* Remember that we saw the `volatile' keyword. */
11980 /* Consume the token. */
11981 cp_lexer_consume_token (parser->lexer);
11983 /* Look for the opening `('. */
11984 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11986 /* Look for the string. */
11987 string = cp_parser_string_literal (parser, false, false);
11988 if (string == error_mark_node)
11990 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11991 /*consume_paren=*/true);
11995 /* If we're allowing GNU extensions, check for the extended assembly
11996 syntax. Unfortunately, the `:' tokens need not be separated by
11997 a space in C, and so, for compatibility, we tolerate that here
11998 too. Doing that means that we have to treat the `::' operator as
12000 if (cp_parser_allow_gnu_extensions_p (parser)
12001 && parser->in_function_body
12002 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12003 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12005 bool inputs_p = false;
12006 bool clobbers_p = false;
12008 /* The extended syntax was used. */
12011 /* Look for outputs. */
12012 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12014 /* Consume the `:'. */
12015 cp_lexer_consume_token (parser->lexer);
12016 /* Parse the output-operands. */
12017 if (cp_lexer_next_token_is_not (parser->lexer,
12019 && cp_lexer_next_token_is_not (parser->lexer,
12021 && cp_lexer_next_token_is_not (parser->lexer,
12023 outputs = cp_parser_asm_operand_list (parser);
12025 if (outputs == error_mark_node)
12026 invalid_outputs_p = true;
12028 /* If the next token is `::', there are no outputs, and the
12029 next token is the beginning of the inputs. */
12030 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12031 /* The inputs are coming next. */
12034 /* Look for inputs. */
12036 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12038 /* Consume the `:' or `::'. */
12039 cp_lexer_consume_token (parser->lexer);
12040 /* Parse the output-operands. */
12041 if (cp_lexer_next_token_is_not (parser->lexer,
12043 && cp_lexer_next_token_is_not (parser->lexer,
12045 inputs = cp_parser_asm_operand_list (parser);
12047 if (inputs == error_mark_node)
12048 invalid_inputs_p = true;
12050 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12051 /* The clobbers are coming next. */
12054 /* Look for clobbers. */
12056 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12058 /* Consume the `:' or `::'. */
12059 cp_lexer_consume_token (parser->lexer);
12060 /* Parse the clobbers. */
12061 if (cp_lexer_next_token_is_not (parser->lexer,
12063 clobbers = cp_parser_asm_clobber_list (parser);
12066 /* Look for the closing `)'. */
12067 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12068 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12069 /*consume_paren=*/true);
12070 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12072 if (!invalid_inputs_p && !invalid_outputs_p)
12074 /* Create the ASM_EXPR. */
12075 if (parser->in_function_body)
12077 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12079 /* If the extended syntax was not used, mark the ASM_EXPR. */
12082 tree temp = asm_stmt;
12083 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12084 temp = TREE_OPERAND (temp, 0);
12086 ASM_INPUT_P (temp) = 1;
12090 cgraph_add_asm_node (string);
12094 /* Declarators [gram.dcl.decl] */
12096 /* Parse an init-declarator.
12099 declarator initializer [opt]
12104 declarator asm-specification [opt] attributes [opt] initializer [opt]
12106 function-definition:
12107 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12109 decl-specifier-seq [opt] declarator function-try-block
12113 function-definition:
12114 __extension__ function-definition
12116 The DECL_SPECIFIERS apply to this declarator. Returns a
12117 representation of the entity declared. If MEMBER_P is TRUE, then
12118 this declarator appears in a class scope. The new DECL created by
12119 this declarator is returned.
12121 The CHECKS are access checks that should be performed once we know
12122 what entity is being declared (and, therefore, what classes have
12125 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12126 for a function-definition here as well. If the declarator is a
12127 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12128 be TRUE upon return. By that point, the function-definition will
12129 have been completely parsed.
12131 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12135 cp_parser_init_declarator (cp_parser* parser,
12136 cp_decl_specifier_seq *decl_specifiers,
12137 VEC (deferred_access_check,gc)* checks,
12138 bool function_definition_allowed_p,
12140 int declares_class_or_enum,
12141 bool* function_definition_p)
12144 cp_declarator *declarator;
12145 tree prefix_attributes;
12147 tree asm_specification;
12149 tree decl = NULL_TREE;
12151 bool is_initialized;
12152 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12153 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12155 enum cpp_ttype initialization_kind;
12156 bool is_parenthesized_init = false;
12157 bool is_non_constant_init;
12158 int ctor_dtor_or_conv_p;
12160 tree pushed_scope = NULL;
12162 /* Gather the attributes that were provided with the
12163 decl-specifiers. */
12164 prefix_attributes = decl_specifiers->attributes;
12166 /* Assume that this is not the declarator for a function
12168 if (function_definition_p)
12169 *function_definition_p = false;
12171 /* Defer access checks while parsing the declarator; we cannot know
12172 what names are accessible until we know what is being
12174 resume_deferring_access_checks ();
12176 /* Parse the declarator. */
12178 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12179 &ctor_dtor_or_conv_p,
12180 /*parenthesized_p=*/NULL,
12181 /*member_p=*/false);
12182 /* Gather up the deferred checks. */
12183 stop_deferring_access_checks ();
12185 /* If the DECLARATOR was erroneous, there's no need to go
12187 if (declarator == cp_error_declarator)
12188 return error_mark_node;
12190 /* Check that the number of template-parameter-lists is OK. */
12191 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12192 return error_mark_node;
12194 if (declares_class_or_enum & 2)
12195 cp_parser_check_for_definition_in_return_type (declarator,
12196 decl_specifiers->type);
12198 /* Figure out what scope the entity declared by the DECLARATOR is
12199 located in. `grokdeclarator' sometimes changes the scope, so
12200 we compute it now. */
12201 scope = get_scope_of_declarator (declarator);
12203 /* If we're allowing GNU extensions, look for an asm-specification
12205 if (cp_parser_allow_gnu_extensions_p (parser))
12207 /* Look for an asm-specification. */
12208 asm_specification = cp_parser_asm_specification_opt (parser);
12209 /* And attributes. */
12210 attributes = cp_parser_attributes_opt (parser);
12214 asm_specification = NULL_TREE;
12215 attributes = NULL_TREE;
12218 /* Peek at the next token. */
12219 token = cp_lexer_peek_token (parser->lexer);
12220 /* Check to see if the token indicates the start of a
12221 function-definition. */
12222 if (cp_parser_token_starts_function_definition_p (token))
12224 if (!function_definition_allowed_p)
12226 /* If a function-definition should not appear here, issue an
12228 cp_parser_error (parser,
12229 "a function-definition is not allowed here");
12230 return error_mark_node;
12234 /* Neither attributes nor an asm-specification are allowed
12235 on a function-definition. */
12236 if (asm_specification)
12237 error ("an asm-specification is not allowed on a function-definition");
12239 error ("attributes are not allowed on a function-definition");
12240 /* This is a function-definition. */
12241 *function_definition_p = true;
12243 /* Parse the function definition. */
12245 decl = cp_parser_save_member_function_body (parser,
12248 prefix_attributes);
12251 = (cp_parser_function_definition_from_specifiers_and_declarator
12252 (parser, decl_specifiers, prefix_attributes, declarator));
12260 Only in function declarations for constructors, destructors, and
12261 type conversions can the decl-specifier-seq be omitted.
12263 We explicitly postpone this check past the point where we handle
12264 function-definitions because we tolerate function-definitions
12265 that are missing their return types in some modes. */
12266 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12268 cp_parser_error (parser,
12269 "expected constructor, destructor, or type conversion");
12270 return error_mark_node;
12273 /* An `=' or an `(' indicates an initializer. */
12274 if (token->type == CPP_EQ
12275 || token->type == CPP_OPEN_PAREN)
12277 is_initialized = true;
12278 initialization_kind = token->type;
12282 /* If the init-declarator isn't initialized and isn't followed by a
12283 `,' or `;', it's not a valid init-declarator. */
12284 if (token->type != CPP_COMMA
12285 && token->type != CPP_SEMICOLON)
12287 cp_parser_error (parser, "expected initializer");
12288 return error_mark_node;
12290 is_initialized = false;
12291 initialization_kind = CPP_EOF;
12294 /* Because start_decl has side-effects, we should only call it if we
12295 know we're going ahead. By this point, we know that we cannot
12296 possibly be looking at any other construct. */
12297 cp_parser_commit_to_tentative_parse (parser);
12299 /* If the decl specifiers were bad, issue an error now that we're
12300 sure this was intended to be a declarator. Then continue
12301 declaring the variable(s), as int, to try to cut down on further
12303 if (decl_specifiers->any_specifiers_p
12304 && decl_specifiers->type == error_mark_node)
12306 cp_parser_error (parser, "invalid type in declaration");
12307 decl_specifiers->type = integer_type_node;
12310 /* Check to see whether or not this declaration is a friend. */
12311 friend_p = cp_parser_friend_p (decl_specifiers);
12313 /* Enter the newly declared entry in the symbol table. If we're
12314 processing a declaration in a class-specifier, we wait until
12315 after processing the initializer. */
12318 if (parser->in_unbraced_linkage_specification_p)
12319 decl_specifiers->storage_class = sc_extern;
12320 decl = start_decl (declarator, decl_specifiers,
12321 is_initialized, attributes, prefix_attributes,
12325 /* Enter the SCOPE. That way unqualified names appearing in the
12326 initializer will be looked up in SCOPE. */
12327 pushed_scope = push_scope (scope);
12329 /* Perform deferred access control checks, now that we know in which
12330 SCOPE the declared entity resides. */
12331 if (!member_p && decl)
12333 tree saved_current_function_decl = NULL_TREE;
12335 /* If the entity being declared is a function, pretend that we
12336 are in its scope. If it is a `friend', it may have access to
12337 things that would not otherwise be accessible. */
12338 if (TREE_CODE (decl) == FUNCTION_DECL)
12340 saved_current_function_decl = current_function_decl;
12341 current_function_decl = decl;
12344 /* Perform access checks for template parameters. */
12345 cp_parser_perform_template_parameter_access_checks (checks);
12347 /* Perform the access control checks for the declarator and the
12348 the decl-specifiers. */
12349 perform_deferred_access_checks ();
12351 /* Restore the saved value. */
12352 if (TREE_CODE (decl) == FUNCTION_DECL)
12353 current_function_decl = saved_current_function_decl;
12356 /* Parse the initializer. */
12357 initializer = NULL_TREE;
12358 is_parenthesized_init = false;
12359 is_non_constant_init = true;
12360 if (is_initialized)
12362 if (function_declarator_p (declarator))
12364 if (initialization_kind == CPP_EQ)
12365 initializer = cp_parser_pure_specifier (parser);
12368 /* If the declaration was erroneous, we don't really
12369 know what the user intended, so just silently
12370 consume the initializer. */
12371 if (decl != error_mark_node)
12372 error ("initializer provided for function");
12373 cp_parser_skip_to_closing_parenthesis (parser,
12374 /*recovering=*/true,
12375 /*or_comma=*/false,
12376 /*consume_paren=*/true);
12380 initializer = cp_parser_initializer (parser,
12381 &is_parenthesized_init,
12382 &is_non_constant_init);
12385 /* The old parser allows attributes to appear after a parenthesized
12386 initializer. Mark Mitchell proposed removing this functionality
12387 on the GCC mailing lists on 2002-08-13. This parser accepts the
12388 attributes -- but ignores them. */
12389 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12390 if (cp_parser_attributes_opt (parser))
12391 warning (OPT_Wattributes,
12392 "attributes after parenthesized initializer ignored");
12394 /* For an in-class declaration, use `grokfield' to create the
12400 pop_scope (pushed_scope);
12401 pushed_scope = false;
12403 decl = grokfield (declarator, decl_specifiers,
12404 initializer, !is_non_constant_init,
12405 /*asmspec=*/NULL_TREE,
12406 prefix_attributes);
12407 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12408 cp_parser_save_default_args (parser, decl);
12411 /* Finish processing the declaration. But, skip friend
12413 if (!friend_p && decl && decl != error_mark_node)
12415 cp_finish_decl (decl,
12416 initializer, !is_non_constant_init,
12418 /* If the initializer is in parentheses, then this is
12419 a direct-initialization, which means that an
12420 `explicit' constructor is OK. Otherwise, an
12421 `explicit' constructor cannot be used. */
12422 ((is_parenthesized_init || !is_initialized)
12423 ? 0 : LOOKUP_ONLYCONVERTING));
12425 else if ((cxx_dialect != cxx98) && friend_p
12426 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12427 /* Core issue #226 (C++0x only): A default template-argument
12428 shall not be specified in a friend class template
12430 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12431 /*is_partial=*/0, /*is_friend_decl=*/1);
12433 if (!friend_p && pushed_scope)
12434 pop_scope (pushed_scope);
12439 /* Parse a declarator.
12443 ptr-operator declarator
12445 abstract-declarator:
12446 ptr-operator abstract-declarator [opt]
12447 direct-abstract-declarator
12452 attributes [opt] direct-declarator
12453 attributes [opt] ptr-operator declarator
12455 abstract-declarator:
12456 attributes [opt] ptr-operator abstract-declarator [opt]
12457 attributes [opt] direct-abstract-declarator
12459 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12460 detect constructor, destructor or conversion operators. It is set
12461 to -1 if the declarator is a name, and +1 if it is a
12462 function. Otherwise it is set to zero. Usually you just want to
12463 test for >0, but internally the negative value is used.
12465 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12466 a decl-specifier-seq unless it declares a constructor, destructor,
12467 or conversion. It might seem that we could check this condition in
12468 semantic analysis, rather than parsing, but that makes it difficult
12469 to handle something like `f()'. We want to notice that there are
12470 no decl-specifiers, and therefore realize that this is an
12471 expression, not a declaration.)
12473 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12474 the declarator is a direct-declarator of the form "(...)".
12476 MEMBER_P is true iff this declarator is a member-declarator. */
12478 static cp_declarator *
12479 cp_parser_declarator (cp_parser* parser,
12480 cp_parser_declarator_kind dcl_kind,
12481 int* ctor_dtor_or_conv_p,
12482 bool* parenthesized_p,
12486 cp_declarator *declarator;
12487 enum tree_code code;
12488 cp_cv_quals cv_quals;
12490 tree attributes = NULL_TREE;
12492 /* Assume this is not a constructor, destructor, or type-conversion
12494 if (ctor_dtor_or_conv_p)
12495 *ctor_dtor_or_conv_p = 0;
12497 if (cp_parser_allow_gnu_extensions_p (parser))
12498 attributes = cp_parser_attributes_opt (parser);
12500 /* Peek at the next token. */
12501 token = cp_lexer_peek_token (parser->lexer);
12503 /* Check for the ptr-operator production. */
12504 cp_parser_parse_tentatively (parser);
12505 /* Parse the ptr-operator. */
12506 code = cp_parser_ptr_operator (parser,
12509 /* If that worked, then we have a ptr-operator. */
12510 if (cp_parser_parse_definitely (parser))
12512 /* If a ptr-operator was found, then this declarator was not
12514 if (parenthesized_p)
12515 *parenthesized_p = true;
12516 /* The dependent declarator is optional if we are parsing an
12517 abstract-declarator. */
12518 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12519 cp_parser_parse_tentatively (parser);
12521 /* Parse the dependent declarator. */
12522 declarator = cp_parser_declarator (parser, dcl_kind,
12523 /*ctor_dtor_or_conv_p=*/NULL,
12524 /*parenthesized_p=*/NULL,
12525 /*member_p=*/false);
12527 /* If we are parsing an abstract-declarator, we must handle the
12528 case where the dependent declarator is absent. */
12529 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12530 && !cp_parser_parse_definitely (parser))
12533 declarator = cp_parser_make_indirect_declarator
12534 (code, class_type, cv_quals, declarator);
12536 /* Everything else is a direct-declarator. */
12539 if (parenthesized_p)
12540 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12542 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12543 ctor_dtor_or_conv_p,
12547 if (attributes && declarator && declarator != cp_error_declarator)
12548 declarator->attributes = attributes;
12553 /* Parse a direct-declarator or direct-abstract-declarator.
12557 direct-declarator ( parameter-declaration-clause )
12558 cv-qualifier-seq [opt]
12559 exception-specification [opt]
12560 direct-declarator [ constant-expression [opt] ]
12563 direct-abstract-declarator:
12564 direct-abstract-declarator [opt]
12565 ( parameter-declaration-clause )
12566 cv-qualifier-seq [opt]
12567 exception-specification [opt]
12568 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12569 ( abstract-declarator )
12571 Returns a representation of the declarator. DCL_KIND is
12572 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12573 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12574 we are parsing a direct-declarator. It is
12575 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12576 of ambiguity we prefer an abstract declarator, as per
12577 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12578 cp_parser_declarator. */
12580 static cp_declarator *
12581 cp_parser_direct_declarator (cp_parser* parser,
12582 cp_parser_declarator_kind dcl_kind,
12583 int* ctor_dtor_or_conv_p,
12587 cp_declarator *declarator = NULL;
12588 tree scope = NULL_TREE;
12589 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12590 bool saved_in_declarator_p = parser->in_declarator_p;
12592 tree pushed_scope = NULL_TREE;
12596 /* Peek at the next token. */
12597 token = cp_lexer_peek_token (parser->lexer);
12598 if (token->type == CPP_OPEN_PAREN)
12600 /* This is either a parameter-declaration-clause, or a
12601 parenthesized declarator. When we know we are parsing a
12602 named declarator, it must be a parenthesized declarator
12603 if FIRST is true. For instance, `(int)' is a
12604 parameter-declaration-clause, with an omitted
12605 direct-abstract-declarator. But `((*))', is a
12606 parenthesized abstract declarator. Finally, when T is a
12607 template parameter `(T)' is a
12608 parameter-declaration-clause, and not a parenthesized
12611 We first try and parse a parameter-declaration-clause,
12612 and then try a nested declarator (if FIRST is true).
12614 It is not an error for it not to be a
12615 parameter-declaration-clause, even when FIRST is
12621 The first is the declaration of a function while the
12622 second is a the definition of a variable, including its
12625 Having seen only the parenthesis, we cannot know which of
12626 these two alternatives should be selected. Even more
12627 complex are examples like:
12632 The former is a function-declaration; the latter is a
12633 variable initialization.
12635 Thus again, we try a parameter-declaration-clause, and if
12636 that fails, we back out and return. */
12638 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12640 cp_parameter_declarator *params;
12641 unsigned saved_num_template_parameter_lists;
12643 /* In a member-declarator, the only valid interpretation
12644 of a parenthesis is the start of a
12645 parameter-declaration-clause. (It is invalid to
12646 initialize a static data member with a parenthesized
12647 initializer; only the "=" form of initialization is
12650 cp_parser_parse_tentatively (parser);
12652 /* Consume the `('. */
12653 cp_lexer_consume_token (parser->lexer);
12656 /* If this is going to be an abstract declarator, we're
12657 in a declarator and we can't have default args. */
12658 parser->default_arg_ok_p = false;
12659 parser->in_declarator_p = true;
12662 /* Inside the function parameter list, surrounding
12663 template-parameter-lists do not apply. */
12664 saved_num_template_parameter_lists
12665 = parser->num_template_parameter_lists;
12666 parser->num_template_parameter_lists = 0;
12668 /* Parse the parameter-declaration-clause. */
12669 params = cp_parser_parameter_declaration_clause (parser);
12671 parser->num_template_parameter_lists
12672 = saved_num_template_parameter_lists;
12674 /* If all went well, parse the cv-qualifier-seq and the
12675 exception-specification. */
12676 if (member_p || cp_parser_parse_definitely (parser))
12678 cp_cv_quals cv_quals;
12679 tree exception_specification;
12681 if (ctor_dtor_or_conv_p)
12682 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12684 /* Consume the `)'. */
12685 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12687 /* Parse the cv-qualifier-seq. */
12688 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12689 /* And the exception-specification. */
12690 exception_specification
12691 = cp_parser_exception_specification_opt (parser);
12693 /* Create the function-declarator. */
12694 declarator = make_call_declarator (declarator,
12697 exception_specification);
12698 /* Any subsequent parameter lists are to do with
12699 return type, so are not those of the declared
12701 parser->default_arg_ok_p = false;
12703 /* Repeat the main loop. */
12708 /* If this is the first, we can try a parenthesized
12712 bool saved_in_type_id_in_expr_p;
12714 parser->default_arg_ok_p = saved_default_arg_ok_p;
12715 parser->in_declarator_p = saved_in_declarator_p;
12717 /* Consume the `('. */
12718 cp_lexer_consume_token (parser->lexer);
12719 /* Parse the nested declarator. */
12720 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12721 parser->in_type_id_in_expr_p = true;
12723 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12724 /*parenthesized_p=*/NULL,
12726 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12728 /* Expect a `)'. */
12729 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12730 declarator = cp_error_declarator;
12731 if (declarator == cp_error_declarator)
12734 goto handle_declarator;
12736 /* Otherwise, we must be done. */
12740 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12741 && token->type == CPP_OPEN_SQUARE)
12743 /* Parse an array-declarator. */
12746 if (ctor_dtor_or_conv_p)
12747 *ctor_dtor_or_conv_p = 0;
12750 parser->default_arg_ok_p = false;
12751 parser->in_declarator_p = true;
12752 /* Consume the `['. */
12753 cp_lexer_consume_token (parser->lexer);
12754 /* Peek at the next token. */
12755 token = cp_lexer_peek_token (parser->lexer);
12756 /* If the next token is `]', then there is no
12757 constant-expression. */
12758 if (token->type != CPP_CLOSE_SQUARE)
12760 bool non_constant_p;
12763 = cp_parser_constant_expression (parser,
12764 /*allow_non_constant=*/true,
12766 if (!non_constant_p)
12767 bounds = fold_non_dependent_expr (bounds);
12768 /* Normally, the array bound must be an integral constant
12769 expression. However, as an extension, we allow VLAs
12770 in function scopes. */
12771 else if (!parser->in_function_body)
12773 error ("array bound is not an integer constant");
12774 bounds = error_mark_node;
12778 bounds = NULL_TREE;
12779 /* Look for the closing `]'. */
12780 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12782 declarator = cp_error_declarator;
12786 declarator = make_array_declarator (declarator, bounds);
12788 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12790 tree qualifying_scope;
12791 tree unqualified_name;
12792 special_function_kind sfk;
12794 bool pack_expansion_p = false;
12796 /* Parse a declarator-id */
12797 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12800 cp_parser_parse_tentatively (parser);
12802 /* If we see an ellipsis, we should be looking at a
12804 if (token->type == CPP_ELLIPSIS)
12806 /* Consume the `...' */
12807 cp_lexer_consume_token (parser->lexer);
12809 pack_expansion_p = true;
12814 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12815 qualifying_scope = parser->scope;
12820 if (!unqualified_name && pack_expansion_p)
12822 /* Check whether an error occurred. */
12823 okay = !cp_parser_error_occurred (parser);
12825 /* We already consumed the ellipsis to mark a
12826 parameter pack, but we have no way to report it,
12827 so abort the tentative parse. We will be exiting
12828 immediately anyway. */
12829 cp_parser_abort_tentative_parse (parser);
12832 okay = cp_parser_parse_definitely (parser);
12835 unqualified_name = error_mark_node;
12836 else if (unqualified_name
12837 && (qualifying_scope
12838 || (TREE_CODE (unqualified_name)
12839 != IDENTIFIER_NODE)))
12841 cp_parser_error (parser, "expected unqualified-id");
12842 unqualified_name = error_mark_node;
12846 if (!unqualified_name)
12848 if (unqualified_name == error_mark_node)
12850 declarator = cp_error_declarator;
12851 pack_expansion_p = false;
12852 declarator->parameter_pack_p = false;
12856 if (qualifying_scope && at_namespace_scope_p ()
12857 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12859 /* In the declaration of a member of a template class
12860 outside of the class itself, the SCOPE will sometimes
12861 be a TYPENAME_TYPE. For example, given:
12863 template <typename T>
12864 int S<T>::R::i = 3;
12866 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12867 this context, we must resolve S<T>::R to an ordinary
12868 type, rather than a typename type.
12870 The reason we normally avoid resolving TYPENAME_TYPEs
12871 is that a specialization of `S' might render
12872 `S<T>::R' not a type. However, if `S' is
12873 specialized, then this `i' will not be used, so there
12874 is no harm in resolving the types here. */
12877 /* Resolve the TYPENAME_TYPE. */
12878 type = resolve_typename_type (qualifying_scope,
12879 /*only_current_p=*/false);
12880 /* If that failed, the declarator is invalid. */
12881 if (TREE_CODE (type) == TYPENAME_TYPE)
12882 error ("%<%T::%E%> is not a type",
12883 TYPE_CONTEXT (qualifying_scope),
12884 TYPE_IDENTIFIER (qualifying_scope));
12885 qualifying_scope = type;
12890 if (unqualified_name)
12894 if (qualifying_scope
12895 && CLASS_TYPE_P (qualifying_scope))
12896 class_type = qualifying_scope;
12898 class_type = current_class_type;
12900 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12902 tree name_type = TREE_TYPE (unqualified_name);
12903 if (class_type && same_type_p (name_type, class_type))
12905 if (qualifying_scope
12906 && CLASSTYPE_USE_TEMPLATE (name_type))
12908 error ("invalid use of constructor as a template");
12909 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12910 "name the constructor in a qualified name",
12912 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12913 class_type, name_type);
12914 declarator = cp_error_declarator;
12918 unqualified_name = constructor_name (class_type);
12922 /* We do not attempt to print the declarator
12923 here because we do not have enough
12924 information about its original syntactic
12926 cp_parser_error (parser, "invalid declarator");
12927 declarator = cp_error_declarator;
12934 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12935 sfk = sfk_destructor;
12936 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12937 sfk = sfk_conversion;
12938 else if (/* There's no way to declare a constructor
12939 for an anonymous type, even if the type
12940 got a name for linkage purposes. */
12941 !TYPE_WAS_ANONYMOUS (class_type)
12942 && constructor_name_p (unqualified_name,
12945 unqualified_name = constructor_name (class_type);
12946 sfk = sfk_constructor;
12949 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12950 *ctor_dtor_or_conv_p = -1;
12953 declarator = make_id_declarator (qualifying_scope,
12956 declarator->id_loc = token->location;
12957 declarator->parameter_pack_p = pack_expansion_p;
12959 if (pack_expansion_p)
12960 maybe_warn_variadic_templates ();
12962 handle_declarator:;
12963 scope = get_scope_of_declarator (declarator);
12965 /* Any names that appear after the declarator-id for a
12966 member are looked up in the containing scope. */
12967 pushed_scope = push_scope (scope);
12968 parser->in_declarator_p = true;
12969 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12970 || (declarator && declarator->kind == cdk_id))
12971 /* Default args are only allowed on function
12973 parser->default_arg_ok_p = saved_default_arg_ok_p;
12975 parser->default_arg_ok_p = false;
12984 /* For an abstract declarator, we might wind up with nothing at this
12985 point. That's an error; the declarator is not optional. */
12987 cp_parser_error (parser, "expected declarator");
12989 /* If we entered a scope, we must exit it now. */
12991 pop_scope (pushed_scope);
12993 parser->default_arg_ok_p = saved_default_arg_ok_p;
12994 parser->in_declarator_p = saved_in_declarator_p;
12999 /* Parse a ptr-operator.
13002 * cv-qualifier-seq [opt]
13004 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13009 & cv-qualifier-seq [opt]
13011 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13012 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13013 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13014 filled in with the TYPE containing the member. *CV_QUALS is
13015 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13016 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13017 Note that the tree codes returned by this function have nothing
13018 to do with the types of trees that will be eventually be created
13019 to represent the pointer or reference type being parsed. They are
13020 just constants with suggestive names. */
13021 static enum tree_code
13022 cp_parser_ptr_operator (cp_parser* parser,
13024 cp_cv_quals *cv_quals)
13026 enum tree_code code = ERROR_MARK;
13029 /* Assume that it's not a pointer-to-member. */
13031 /* And that there are no cv-qualifiers. */
13032 *cv_quals = TYPE_UNQUALIFIED;
13034 /* Peek at the next token. */
13035 token = cp_lexer_peek_token (parser->lexer);
13037 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13038 if (token->type == CPP_MULT)
13039 code = INDIRECT_REF;
13040 else if (token->type == CPP_AND)
13042 else if ((cxx_dialect != cxx98) &&
13043 token->type == CPP_AND_AND) /* C++0x only */
13044 code = NON_LVALUE_EXPR;
13046 if (code != ERROR_MARK)
13048 /* Consume the `*', `&' or `&&'. */
13049 cp_lexer_consume_token (parser->lexer);
13051 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13052 `&', if we are allowing GNU extensions. (The only qualifier
13053 that can legally appear after `&' is `restrict', but that is
13054 enforced during semantic analysis. */
13055 if (code == INDIRECT_REF
13056 || cp_parser_allow_gnu_extensions_p (parser))
13057 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13061 /* Try the pointer-to-member case. */
13062 cp_parser_parse_tentatively (parser);
13063 /* Look for the optional `::' operator. */
13064 cp_parser_global_scope_opt (parser,
13065 /*current_scope_valid_p=*/false);
13066 /* Look for the nested-name specifier. */
13067 cp_parser_nested_name_specifier (parser,
13068 /*typename_keyword_p=*/false,
13069 /*check_dependency_p=*/true,
13071 /*is_declaration=*/false);
13072 /* If we found it, and the next token is a `*', then we are
13073 indeed looking at a pointer-to-member operator. */
13074 if (!cp_parser_error_occurred (parser)
13075 && cp_parser_require (parser, CPP_MULT, "`*'"))
13077 /* Indicate that the `*' operator was used. */
13078 code = INDIRECT_REF;
13080 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13081 error ("%qD is a namespace", parser->scope);
13084 /* The type of which the member is a member is given by the
13086 *type = parser->scope;
13087 /* The next name will not be qualified. */
13088 parser->scope = NULL_TREE;
13089 parser->qualifying_scope = NULL_TREE;
13090 parser->object_scope = NULL_TREE;
13091 /* Look for the optional cv-qualifier-seq. */
13092 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13095 /* If that didn't work we don't have a ptr-operator. */
13096 if (!cp_parser_parse_definitely (parser))
13097 cp_parser_error (parser, "expected ptr-operator");
13103 /* Parse an (optional) cv-qualifier-seq.
13106 cv-qualifier cv-qualifier-seq [opt]
13117 Returns a bitmask representing the cv-qualifiers. */
13120 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13122 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13127 cp_cv_quals cv_qualifier;
13129 /* Peek at the next token. */
13130 token = cp_lexer_peek_token (parser->lexer);
13131 /* See if it's a cv-qualifier. */
13132 switch (token->keyword)
13135 cv_qualifier = TYPE_QUAL_CONST;
13139 cv_qualifier = TYPE_QUAL_VOLATILE;
13143 cv_qualifier = TYPE_QUAL_RESTRICT;
13147 cv_qualifier = TYPE_UNQUALIFIED;
13154 if (cv_quals & cv_qualifier)
13156 error ("duplicate cv-qualifier");
13157 cp_lexer_purge_token (parser->lexer);
13161 cp_lexer_consume_token (parser->lexer);
13162 cv_quals |= cv_qualifier;
13169 /* Parse a declarator-id.
13173 :: [opt] nested-name-specifier [opt] type-name
13175 In the `id-expression' case, the value returned is as for
13176 cp_parser_id_expression if the id-expression was an unqualified-id.
13177 If the id-expression was a qualified-id, then a SCOPE_REF is
13178 returned. The first operand is the scope (either a NAMESPACE_DECL
13179 or TREE_TYPE), but the second is still just a representation of an
13183 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13186 /* The expression must be an id-expression. Assume that qualified
13187 names are the names of types so that:
13190 int S<T>::R::i = 3;
13192 will work; we must treat `S<T>::R' as the name of a type.
13193 Similarly, assume that qualified names are templates, where
13197 int S<T>::R<T>::i = 3;
13200 id = cp_parser_id_expression (parser,
13201 /*template_keyword_p=*/false,
13202 /*check_dependency_p=*/false,
13203 /*template_p=*/NULL,
13204 /*declarator_p=*/true,
13206 if (id && BASELINK_P (id))
13207 id = BASELINK_FUNCTIONS (id);
13211 /* Parse a type-id.
13214 type-specifier-seq abstract-declarator [opt]
13216 Returns the TYPE specified. */
13219 cp_parser_type_id (cp_parser* parser)
13221 cp_decl_specifier_seq type_specifier_seq;
13222 cp_declarator *abstract_declarator;
13224 /* Parse the type-specifier-seq. */
13225 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13226 &type_specifier_seq);
13227 if (type_specifier_seq.type == error_mark_node)
13228 return error_mark_node;
13230 /* There might or might not be an abstract declarator. */
13231 cp_parser_parse_tentatively (parser);
13232 /* Look for the declarator. */
13233 abstract_declarator
13234 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13235 /*parenthesized_p=*/NULL,
13236 /*member_p=*/false);
13237 /* Check to see if there really was a declarator. */
13238 if (!cp_parser_parse_definitely (parser))
13239 abstract_declarator = NULL;
13241 return groktypename (&type_specifier_seq, abstract_declarator);
13244 /* Parse a type-specifier-seq.
13246 type-specifier-seq:
13247 type-specifier type-specifier-seq [opt]
13251 type-specifier-seq:
13252 attributes type-specifier-seq [opt]
13254 If IS_CONDITION is true, we are at the start of a "condition",
13255 e.g., we've just seen "if (".
13257 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13260 cp_parser_type_specifier_seq (cp_parser* parser,
13262 cp_decl_specifier_seq *type_specifier_seq)
13264 bool seen_type_specifier = false;
13265 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13267 /* Clear the TYPE_SPECIFIER_SEQ. */
13268 clear_decl_specs (type_specifier_seq);
13270 /* Parse the type-specifiers and attributes. */
13273 tree type_specifier;
13274 bool is_cv_qualifier;
13276 /* Check for attributes first. */
13277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13279 type_specifier_seq->attributes =
13280 chainon (type_specifier_seq->attributes,
13281 cp_parser_attributes_opt (parser));
13285 /* Look for the type-specifier. */
13286 type_specifier = cp_parser_type_specifier (parser,
13288 type_specifier_seq,
13289 /*is_declaration=*/false,
13292 if (!type_specifier)
13294 /* If the first type-specifier could not be found, this is not a
13295 type-specifier-seq at all. */
13296 if (!seen_type_specifier)
13298 cp_parser_error (parser, "expected type-specifier");
13299 type_specifier_seq->type = error_mark_node;
13302 /* If subsequent type-specifiers could not be found, the
13303 type-specifier-seq is complete. */
13307 seen_type_specifier = true;
13308 /* The standard says that a condition can be:
13310 type-specifier-seq declarator = assignment-expression
13317 we should treat the "S" as a declarator, not as a
13318 type-specifier. The standard doesn't say that explicitly for
13319 type-specifier-seq, but it does say that for
13320 decl-specifier-seq in an ordinary declaration. Perhaps it
13321 would be clearer just to allow a decl-specifier-seq here, and
13322 then add a semantic restriction that if any decl-specifiers
13323 that are not type-specifiers appear, the program is invalid. */
13324 if (is_condition && !is_cv_qualifier)
13325 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13328 cp_parser_check_decl_spec (type_specifier_seq);
13331 /* Parse a parameter-declaration-clause.
13333 parameter-declaration-clause:
13334 parameter-declaration-list [opt] ... [opt]
13335 parameter-declaration-list , ...
13337 Returns a representation for the parameter declarations. A return
13338 value of NULL indicates a parameter-declaration-clause consisting
13339 only of an ellipsis. */
13341 static cp_parameter_declarator *
13342 cp_parser_parameter_declaration_clause (cp_parser* parser)
13344 cp_parameter_declarator *parameters;
13349 /* Peek at the next token. */
13350 token = cp_lexer_peek_token (parser->lexer);
13351 /* Check for trivial parameter-declaration-clauses. */
13352 if (token->type == CPP_ELLIPSIS)
13354 /* Consume the `...' token. */
13355 cp_lexer_consume_token (parser->lexer);
13358 else if (token->type == CPP_CLOSE_PAREN)
13359 /* There are no parameters. */
13361 #ifndef NO_IMPLICIT_EXTERN_C
13362 if (in_system_header && current_class_type == NULL
13363 && current_lang_name == lang_name_c)
13367 return no_parameters;
13369 /* Check for `(void)', too, which is a special case. */
13370 else if (token->keyword == RID_VOID
13371 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13372 == CPP_CLOSE_PAREN))
13374 /* Consume the `void' token. */
13375 cp_lexer_consume_token (parser->lexer);
13376 /* There are no parameters. */
13377 return no_parameters;
13380 /* Parse the parameter-declaration-list. */
13381 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13382 /* If a parse error occurred while parsing the
13383 parameter-declaration-list, then the entire
13384 parameter-declaration-clause is erroneous. */
13388 /* Peek at the next token. */
13389 token = cp_lexer_peek_token (parser->lexer);
13390 /* If it's a `,', the clause should terminate with an ellipsis. */
13391 if (token->type == CPP_COMMA)
13393 /* Consume the `,'. */
13394 cp_lexer_consume_token (parser->lexer);
13395 /* Expect an ellipsis. */
13397 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13399 /* It might also be `...' if the optional trailing `,' was
13401 else if (token->type == CPP_ELLIPSIS)
13403 /* Consume the `...' token. */
13404 cp_lexer_consume_token (parser->lexer);
13405 /* And remember that we saw it. */
13409 ellipsis_p = false;
13411 /* Finish the parameter list. */
13412 if (parameters && ellipsis_p)
13413 parameters->ellipsis_p = true;
13418 /* Parse a parameter-declaration-list.
13420 parameter-declaration-list:
13421 parameter-declaration
13422 parameter-declaration-list , parameter-declaration
13424 Returns a representation of the parameter-declaration-list, as for
13425 cp_parser_parameter_declaration_clause. However, the
13426 `void_list_node' is never appended to the list. Upon return,
13427 *IS_ERROR will be true iff an error occurred. */
13429 static cp_parameter_declarator *
13430 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13432 cp_parameter_declarator *parameters = NULL;
13433 cp_parameter_declarator **tail = ¶meters;
13434 bool saved_in_unbraced_linkage_specification_p;
13436 /* Assume all will go well. */
13438 /* The special considerations that apply to a function within an
13439 unbraced linkage specifications do not apply to the parameters
13440 to the function. */
13441 saved_in_unbraced_linkage_specification_p
13442 = parser->in_unbraced_linkage_specification_p;
13443 parser->in_unbraced_linkage_specification_p = false;
13445 /* Look for more parameters. */
13448 cp_parameter_declarator *parameter;
13449 bool parenthesized_p;
13450 /* Parse the parameter. */
13452 = cp_parser_parameter_declaration (parser,
13453 /*template_parm_p=*/false,
13456 /* If a parse error occurred parsing the parameter declaration,
13457 then the entire parameter-declaration-list is erroneous. */
13464 /* Add the new parameter to the list. */
13466 tail = ¶meter->next;
13468 /* Peek at the next token. */
13469 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13470 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13471 /* These are for Objective-C++ */
13472 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13473 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13474 /* The parameter-declaration-list is complete. */
13476 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13480 /* Peek at the next token. */
13481 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13482 /* If it's an ellipsis, then the list is complete. */
13483 if (token->type == CPP_ELLIPSIS)
13485 /* Otherwise, there must be more parameters. Consume the
13487 cp_lexer_consume_token (parser->lexer);
13488 /* When parsing something like:
13490 int i(float f, double d)
13492 we can tell after seeing the declaration for "f" that we
13493 are not looking at an initialization of a variable "i",
13494 but rather at the declaration of a function "i".
13496 Due to the fact that the parsing of template arguments
13497 (as specified to a template-id) requires backtracking we
13498 cannot use this technique when inside a template argument
13500 if (!parser->in_template_argument_list_p
13501 && !parser->in_type_id_in_expr_p
13502 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13503 /* However, a parameter-declaration of the form
13504 "foat(f)" (which is a valid declaration of a
13505 parameter "f") can also be interpreted as an
13506 expression (the conversion of "f" to "float"). */
13507 && !parenthesized_p)
13508 cp_parser_commit_to_tentative_parse (parser);
13512 cp_parser_error (parser, "expected %<,%> or %<...%>");
13513 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13514 cp_parser_skip_to_closing_parenthesis (parser,
13515 /*recovering=*/true,
13516 /*or_comma=*/false,
13517 /*consume_paren=*/false);
13522 parser->in_unbraced_linkage_specification_p
13523 = saved_in_unbraced_linkage_specification_p;
13528 /* Parse a parameter declaration.
13530 parameter-declaration:
13531 decl-specifier-seq ... [opt] declarator
13532 decl-specifier-seq declarator = assignment-expression
13533 decl-specifier-seq ... [opt] abstract-declarator [opt]
13534 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13536 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13537 declares a template parameter. (In that case, a non-nested `>'
13538 token encountered during the parsing of the assignment-expression
13539 is not interpreted as a greater-than operator.)
13541 Returns a representation of the parameter, or NULL if an error
13542 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13543 true iff the declarator is of the form "(p)". */
13545 static cp_parameter_declarator *
13546 cp_parser_parameter_declaration (cp_parser *parser,
13547 bool template_parm_p,
13548 bool *parenthesized_p)
13550 int declares_class_or_enum;
13551 bool greater_than_is_operator_p;
13552 cp_decl_specifier_seq decl_specifiers;
13553 cp_declarator *declarator;
13554 tree default_argument;
13556 const char *saved_message;
13558 /* In a template parameter, `>' is not an operator.
13562 When parsing a default template-argument for a non-type
13563 template-parameter, the first non-nested `>' is taken as the end
13564 of the template parameter-list rather than a greater-than
13566 greater_than_is_operator_p = !template_parm_p;
13568 /* Type definitions may not appear in parameter types. */
13569 saved_message = parser->type_definition_forbidden_message;
13570 parser->type_definition_forbidden_message
13571 = "types may not be defined in parameter types";
13573 /* Parse the declaration-specifiers. */
13574 cp_parser_decl_specifier_seq (parser,
13575 CP_PARSER_FLAGS_NONE,
13577 &declares_class_or_enum);
13578 /* If an error occurred, there's no reason to attempt to parse the
13579 rest of the declaration. */
13580 if (cp_parser_error_occurred (parser))
13582 parser->type_definition_forbidden_message = saved_message;
13586 /* Peek at the next token. */
13587 token = cp_lexer_peek_token (parser->lexer);
13589 /* If the next token is a `)', `,', `=', `>', or `...', then there
13590 is no declarator. However, when variadic templates are enabled,
13591 there may be a declarator following `...'. */
13592 if (token->type == CPP_CLOSE_PAREN
13593 || token->type == CPP_COMMA
13594 || token->type == CPP_EQ
13595 || token->type == CPP_GREATER)
13598 if (parenthesized_p)
13599 *parenthesized_p = false;
13601 /* Otherwise, there should be a declarator. */
13604 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13605 parser->default_arg_ok_p = false;
13607 /* After seeing a decl-specifier-seq, if the next token is not a
13608 "(", there is no possibility that the code is a valid
13609 expression. Therefore, if parsing tentatively, we commit at
13611 if (!parser->in_template_argument_list_p
13612 /* In an expression context, having seen:
13616 we cannot be sure whether we are looking at a
13617 function-type (taking a "char" as a parameter) or a cast
13618 of some object of type "char" to "int". */
13619 && !parser->in_type_id_in_expr_p
13620 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13621 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13622 cp_parser_commit_to_tentative_parse (parser);
13623 /* Parse the declarator. */
13624 declarator = cp_parser_declarator (parser,
13625 CP_PARSER_DECLARATOR_EITHER,
13626 /*ctor_dtor_or_conv_p=*/NULL,
13628 /*member_p=*/false);
13629 parser->default_arg_ok_p = saved_default_arg_ok_p;
13630 /* After the declarator, allow more attributes. */
13631 decl_specifiers.attributes
13632 = chainon (decl_specifiers.attributes,
13633 cp_parser_attributes_opt (parser));
13636 /* If the next token is an ellipsis, and we have not seen a
13637 declarator name, and the type of the declarator contains parameter
13638 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13639 a parameter pack expansion expression. Otherwise, leave the
13640 ellipsis for a C-style variadic function. */
13641 token = cp_lexer_peek_token (parser->lexer);
13642 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13644 tree type = decl_specifiers.type;
13646 if (type && DECL_P (type))
13647 type = TREE_TYPE (type);
13650 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13651 && declarator_can_be_parameter_pack (declarator)
13652 && (!declarator || !declarator->parameter_pack_p)
13653 && uses_parameter_packs (type))
13655 /* Consume the `...'. */
13656 cp_lexer_consume_token (parser->lexer);
13657 maybe_warn_variadic_templates ();
13659 /* Build a pack expansion type */
13661 declarator->parameter_pack_p = true;
13663 decl_specifiers.type = make_pack_expansion (type);
13667 /* The restriction on defining new types applies only to the type
13668 of the parameter, not to the default argument. */
13669 parser->type_definition_forbidden_message = saved_message;
13671 /* If the next token is `=', then process a default argument. */
13672 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13674 /* Consume the `='. */
13675 cp_lexer_consume_token (parser->lexer);
13677 /* If we are defining a class, then the tokens that make up the
13678 default argument must be saved and processed later. */
13679 if (!template_parm_p && at_class_scope_p ()
13680 && TYPE_BEING_DEFINED (current_class_type))
13682 unsigned depth = 0;
13683 cp_token *first_token;
13686 /* Add tokens until we have processed the entire default
13687 argument. We add the range [first_token, token). */
13688 first_token = cp_lexer_peek_token (parser->lexer);
13693 /* Peek at the next token. */
13694 token = cp_lexer_peek_token (parser->lexer);
13695 /* What we do depends on what token we have. */
13696 switch (token->type)
13698 /* In valid code, a default argument must be
13699 immediately followed by a `,' `)', or `...'. */
13701 case CPP_CLOSE_PAREN:
13703 /* If we run into a non-nested `;', `}', or `]',
13704 then the code is invalid -- but the default
13705 argument is certainly over. */
13706 case CPP_SEMICOLON:
13707 case CPP_CLOSE_BRACE:
13708 case CPP_CLOSE_SQUARE:
13711 /* Update DEPTH, if necessary. */
13712 else if (token->type == CPP_CLOSE_PAREN
13713 || token->type == CPP_CLOSE_BRACE
13714 || token->type == CPP_CLOSE_SQUARE)
13718 case CPP_OPEN_PAREN:
13719 case CPP_OPEN_SQUARE:
13720 case CPP_OPEN_BRACE:
13725 if (cxx_dialect == cxx98)
13727 /* Fall through for C++0x, which treats the `>>'
13728 operator like two `>' tokens in certain
13732 /* If we see a non-nested `>', and `>' is not an
13733 operator, then it marks the end of the default
13735 if (!depth && !greater_than_is_operator_p)
13739 /* If we run out of tokens, issue an error message. */
13741 case CPP_PRAGMA_EOL:
13742 error ("file ends in default argument");
13748 /* In these cases, we should look for template-ids.
13749 For example, if the default argument is
13750 `X<int, double>()', we need to do name lookup to
13751 figure out whether or not `X' is a template; if
13752 so, the `,' does not end the default argument.
13754 That is not yet done. */
13761 /* If we've reached the end, stop. */
13765 /* Add the token to the token block. */
13766 token = cp_lexer_consume_token (parser->lexer);
13769 /* Create a DEFAULT_ARG to represent the unparsed default
13771 default_argument = make_node (DEFAULT_ARG);
13772 DEFARG_TOKENS (default_argument)
13773 = cp_token_cache_new (first_token, token);
13774 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13776 /* Outside of a class definition, we can just parse the
13777 assignment-expression. */
13780 = cp_parser_default_argument (parser, template_parm_p);
13782 if (!parser->default_arg_ok_p)
13784 if (!flag_pedantic_errors)
13785 warning (0, "deprecated use of default argument for parameter of non-function");
13788 error ("default arguments are only permitted for function parameters");
13789 default_argument = NULL_TREE;
13792 else if ((declarator && declarator->parameter_pack_p)
13793 || (decl_specifiers.type
13794 && PACK_EXPANSION_P (decl_specifiers.type)))
13796 const char* kind = template_parm_p? "template " : "";
13798 /* Find the name of the parameter pack. */
13799 cp_declarator *id_declarator = declarator;
13800 while (id_declarator && id_declarator->kind != cdk_id)
13801 id_declarator = id_declarator->declarator;
13803 if (id_declarator && id_declarator->kind == cdk_id)
13804 error ("%sparameter pack %qD cannot have a default argument",
13805 kind, id_declarator->u.id.unqualified_name);
13807 error ("%sparameter pack cannot have a default argument",
13810 default_argument = NULL_TREE;
13814 default_argument = NULL_TREE;
13816 return make_parameter_declarator (&decl_specifiers,
13821 /* Parse a default argument and return it.
13823 TEMPLATE_PARM_P is true if this is a default argument for a
13824 non-type template parameter. */
13826 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13828 tree default_argument = NULL_TREE;
13829 bool saved_greater_than_is_operator_p;
13830 bool saved_local_variables_forbidden_p;
13832 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13834 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13835 parser->greater_than_is_operator_p = !template_parm_p;
13836 /* Local variable names (and the `this' keyword) may not
13837 appear in a default argument. */
13838 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13839 parser->local_variables_forbidden_p = true;
13840 /* The default argument expression may cause implicitly
13841 defined member functions to be synthesized, which will
13842 result in garbage collection. We must treat this
13843 situation as if we were within the body of function so as
13844 to avoid collecting live data on the stack. */
13846 /* Parse the assignment-expression. */
13847 if (template_parm_p)
13848 push_deferring_access_checks (dk_no_deferred);
13850 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13851 if (template_parm_p)
13852 pop_deferring_access_checks ();
13853 /* Restore saved state. */
13855 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13856 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13858 return default_argument;
13861 /* Parse a function-body.
13864 compound_statement */
13867 cp_parser_function_body (cp_parser *parser)
13869 cp_parser_compound_statement (parser, NULL, false);
13872 /* Parse a ctor-initializer-opt followed by a function-body. Return
13873 true if a ctor-initializer was present. */
13876 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13879 bool ctor_initializer_p;
13881 /* Begin the function body. */
13882 body = begin_function_body ();
13883 /* Parse the optional ctor-initializer. */
13884 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13885 /* Parse the function-body. */
13886 cp_parser_function_body (parser);
13887 /* Finish the function body. */
13888 finish_function_body (body);
13890 return ctor_initializer_p;
13893 /* Parse an initializer.
13896 = initializer-clause
13897 ( expression-list )
13899 Returns an expression representing the initializer. If no
13900 initializer is present, NULL_TREE is returned.
13902 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13903 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13904 set to FALSE if there is no initializer present. If there is an
13905 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13906 is set to true; otherwise it is set to false. */
13909 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13910 bool* non_constant_p)
13915 /* Peek at the next token. */
13916 token = cp_lexer_peek_token (parser->lexer);
13918 /* Let our caller know whether or not this initializer was
13920 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13921 /* Assume that the initializer is constant. */
13922 *non_constant_p = false;
13924 if (token->type == CPP_EQ)
13926 /* Consume the `='. */
13927 cp_lexer_consume_token (parser->lexer);
13928 /* Parse the initializer-clause. */
13929 init = cp_parser_initializer_clause (parser, non_constant_p);
13931 else if (token->type == CPP_OPEN_PAREN)
13932 init = cp_parser_parenthesized_expression_list (parser, false,
13934 /*allow_expansion_p=*/true,
13938 /* Anything else is an error. */
13939 cp_parser_error (parser, "expected initializer");
13940 init = error_mark_node;
13946 /* Parse an initializer-clause.
13948 initializer-clause:
13949 assignment-expression
13950 { initializer-list , [opt] }
13953 Returns an expression representing the initializer.
13955 If the `assignment-expression' production is used the value
13956 returned is simply a representation for the expression.
13958 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13959 the elements of the initializer-list (or NULL, if the last
13960 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13961 NULL_TREE. There is no way to detect whether or not the optional
13962 trailing `,' was provided. NON_CONSTANT_P is as for
13963 cp_parser_initializer. */
13966 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13970 /* Assume the expression is constant. */
13971 *non_constant_p = false;
13973 /* If it is not a `{', then we are looking at an
13974 assignment-expression. */
13975 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13978 = cp_parser_constant_expression (parser,
13979 /*allow_non_constant_p=*/true,
13981 if (!*non_constant_p)
13982 initializer = fold_non_dependent_expr (initializer);
13986 /* Consume the `{' token. */
13987 cp_lexer_consume_token (parser->lexer);
13988 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13989 initializer = make_node (CONSTRUCTOR);
13990 /* If it's not a `}', then there is a non-trivial initializer. */
13991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13993 /* Parse the initializer list. */
13994 CONSTRUCTOR_ELTS (initializer)
13995 = cp_parser_initializer_list (parser, non_constant_p);
13996 /* A trailing `,' token is allowed. */
13997 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13998 cp_lexer_consume_token (parser->lexer);
14000 /* Now, there should be a trailing `}'. */
14001 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14004 return initializer;
14007 /* Parse an initializer-list.
14010 initializer-clause ... [opt]
14011 initializer-list , initializer-clause ... [opt]
14016 identifier : initializer-clause
14017 initializer-list, identifier : initializer-clause
14019 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14020 for the initializer. If the INDEX of the elt is non-NULL, it is the
14021 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14022 as for cp_parser_initializer. */
14024 static VEC(constructor_elt,gc) *
14025 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14027 VEC(constructor_elt,gc) *v = NULL;
14029 /* Assume all of the expressions are constant. */
14030 *non_constant_p = false;
14032 /* Parse the rest of the list. */
14038 bool clause_non_constant_p;
14040 /* If the next token is an identifier and the following one is a
14041 colon, we are looking at the GNU designated-initializer
14043 if (cp_parser_allow_gnu_extensions_p (parser)
14044 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14045 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14047 /* Warn the user that they are using an extension. */
14049 pedwarn ("ISO C++ does not allow designated initializers");
14050 /* Consume the identifier. */
14051 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14052 /* Consume the `:'. */
14053 cp_lexer_consume_token (parser->lexer);
14056 identifier = NULL_TREE;
14058 /* Parse the initializer. */
14059 initializer = cp_parser_initializer_clause (parser,
14060 &clause_non_constant_p);
14061 /* If any clause is non-constant, so is the entire initializer. */
14062 if (clause_non_constant_p)
14063 *non_constant_p = true;
14065 /* If we have an ellipsis, this is an initializer pack
14067 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14069 /* Consume the `...'. */
14070 cp_lexer_consume_token (parser->lexer);
14072 /* Turn the initializer into an initializer expansion. */
14073 initializer = make_pack_expansion (initializer);
14076 /* Add it to the vector. */
14077 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14079 /* If the next token is not a comma, we have reached the end of
14081 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14084 /* Peek at the next token. */
14085 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14086 /* If the next token is a `}', then we're still done. An
14087 initializer-clause can have a trailing `,' after the
14088 initializer-list and before the closing `}'. */
14089 if (token->type == CPP_CLOSE_BRACE)
14092 /* Consume the `,' token. */
14093 cp_lexer_consume_token (parser->lexer);
14099 /* Classes [gram.class] */
14101 /* Parse a class-name.
14107 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14108 to indicate that names looked up in dependent types should be
14109 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14110 keyword has been used to indicate that the name that appears next
14111 is a template. TAG_TYPE indicates the explicit tag given before
14112 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14113 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14114 is the class being defined in a class-head.
14116 Returns the TYPE_DECL representing the class. */
14119 cp_parser_class_name (cp_parser *parser,
14120 bool typename_keyword_p,
14121 bool template_keyword_p,
14122 enum tag_types tag_type,
14123 bool check_dependency_p,
14125 bool is_declaration)
14132 /* All class-names start with an identifier. */
14133 token = cp_lexer_peek_token (parser->lexer);
14134 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14136 cp_parser_error (parser, "expected class-name");
14137 return error_mark_node;
14140 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14141 to a template-id, so we save it here. */
14142 scope = parser->scope;
14143 if (scope == error_mark_node)
14144 return error_mark_node;
14146 /* Any name names a type if we're following the `typename' keyword
14147 in a qualified name where the enclosing scope is type-dependent. */
14148 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14149 && dependent_type_p (scope));
14150 /* Handle the common case (an identifier, but not a template-id)
14152 if (token->type == CPP_NAME
14153 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14155 cp_token *identifier_token;
14159 /* Look for the identifier. */
14160 identifier_token = cp_lexer_peek_token (parser->lexer);
14161 ambiguous_p = identifier_token->ambiguous_p;
14162 identifier = cp_parser_identifier (parser);
14163 /* If the next token isn't an identifier, we are certainly not
14164 looking at a class-name. */
14165 if (identifier == error_mark_node)
14166 decl = error_mark_node;
14167 /* If we know this is a type-name, there's no need to look it
14169 else if (typename_p)
14173 tree ambiguous_decls;
14174 /* If we already know that this lookup is ambiguous, then
14175 we've already issued an error message; there's no reason
14179 cp_parser_simulate_error (parser);
14180 return error_mark_node;
14182 /* If the next token is a `::', then the name must be a type
14185 [basic.lookup.qual]
14187 During the lookup for a name preceding the :: scope
14188 resolution operator, object, function, and enumerator
14189 names are ignored. */
14190 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14191 tag_type = typename_type;
14192 /* Look up the name. */
14193 decl = cp_parser_lookup_name (parser, identifier,
14195 /*is_template=*/false,
14196 /*is_namespace=*/false,
14197 check_dependency_p,
14199 if (ambiguous_decls)
14201 error ("reference to %qD is ambiguous", identifier);
14202 print_candidates (ambiguous_decls);
14203 if (cp_parser_parsing_tentatively (parser))
14205 identifier_token->ambiguous_p = true;
14206 cp_parser_simulate_error (parser);
14208 return error_mark_node;
14214 /* Try a template-id. */
14215 decl = cp_parser_template_id (parser, template_keyword_p,
14216 check_dependency_p,
14218 if (decl == error_mark_node)
14219 return error_mark_node;
14222 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14224 /* If this is a typename, create a TYPENAME_TYPE. */
14225 if (typename_p && decl != error_mark_node)
14227 decl = make_typename_type (scope, decl, typename_type,
14228 /*complain=*/tf_error);
14229 if (decl != error_mark_node)
14230 decl = TYPE_NAME (decl);
14233 /* Check to see that it is really the name of a class. */
14234 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14235 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14236 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14237 /* Situations like this:
14239 template <typename T> struct A {
14240 typename T::template X<int>::I i;
14243 are problematic. Is `T::template X<int>' a class-name? The
14244 standard does not seem to be definitive, but there is no other
14245 valid interpretation of the following `::'. Therefore, those
14246 names are considered class-names. */
14248 decl = make_typename_type (scope, decl, tag_type, tf_error);
14249 if (decl != error_mark_node)
14250 decl = TYPE_NAME (decl);
14252 else if (TREE_CODE (decl) != TYPE_DECL
14253 || TREE_TYPE (decl) == error_mark_node
14254 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14255 decl = error_mark_node;
14257 if (decl == error_mark_node)
14258 cp_parser_error (parser, "expected class-name");
14263 /* Parse a class-specifier.
14266 class-head { member-specification [opt] }
14268 Returns the TREE_TYPE representing the class. */
14271 cp_parser_class_specifier (cp_parser* parser)
14275 tree attributes = NULL_TREE;
14276 int has_trailing_semicolon;
14277 bool nested_name_specifier_p;
14278 unsigned saved_num_template_parameter_lists;
14279 bool saved_in_function_body;
14280 tree old_scope = NULL_TREE;
14281 tree scope = NULL_TREE;
14284 push_deferring_access_checks (dk_no_deferred);
14286 /* Parse the class-head. */
14287 type = cp_parser_class_head (parser,
14288 &nested_name_specifier_p,
14291 /* If the class-head was a semantic disaster, skip the entire body
14295 cp_parser_skip_to_end_of_block_or_statement (parser);
14296 pop_deferring_access_checks ();
14297 return error_mark_node;
14300 /* Look for the `{'. */
14301 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14303 pop_deferring_access_checks ();
14304 return error_mark_node;
14307 /* Process the base classes. If they're invalid, skip the
14308 entire class body. */
14309 if (!xref_basetypes (type, bases))
14311 /* Consuming the closing brace yields better error messages
14313 if (cp_parser_skip_to_closing_brace (parser))
14314 cp_lexer_consume_token (parser->lexer);
14315 pop_deferring_access_checks ();
14316 return error_mark_node;
14319 /* Issue an error message if type-definitions are forbidden here. */
14320 cp_parser_check_type_definition (parser);
14321 /* Remember that we are defining one more class. */
14322 ++parser->num_classes_being_defined;
14323 /* Inside the class, surrounding template-parameter-lists do not
14325 saved_num_template_parameter_lists
14326 = parser->num_template_parameter_lists;
14327 parser->num_template_parameter_lists = 0;
14328 /* We are not in a function body. */
14329 saved_in_function_body = parser->in_function_body;
14330 parser->in_function_body = false;
14332 /* Start the class. */
14333 if (nested_name_specifier_p)
14335 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14336 old_scope = push_inner_scope (scope);
14338 type = begin_class_definition (type, attributes);
14340 if (type == error_mark_node)
14341 /* If the type is erroneous, skip the entire body of the class. */
14342 cp_parser_skip_to_closing_brace (parser);
14344 /* Parse the member-specification. */
14345 cp_parser_member_specification_opt (parser);
14347 /* Look for the trailing `}'. */
14348 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14349 /* We get better error messages by noticing a common problem: a
14350 missing trailing `;'. */
14351 token = cp_lexer_peek_token (parser->lexer);
14352 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14353 /* Look for trailing attributes to apply to this class. */
14354 if (cp_parser_allow_gnu_extensions_p (parser))
14355 attributes = cp_parser_attributes_opt (parser);
14356 if (type != error_mark_node)
14357 type = finish_struct (type, attributes);
14358 if (nested_name_specifier_p)
14359 pop_inner_scope (old_scope, scope);
14360 /* If this class is not itself within the scope of another class,
14361 then we need to parse the bodies of all of the queued function
14362 definitions. Note that the queued functions defined in a class
14363 are not always processed immediately following the
14364 class-specifier for that class. Consider:
14367 struct B { void f() { sizeof (A); } };
14370 If `f' were processed before the processing of `A' were
14371 completed, there would be no way to compute the size of `A'.
14372 Note that the nesting we are interested in here is lexical --
14373 not the semantic nesting given by TYPE_CONTEXT. In particular,
14376 struct A { struct B; };
14377 struct A::B { void f() { } };
14379 there is no need to delay the parsing of `A::B::f'. */
14380 if (--parser->num_classes_being_defined == 0)
14384 tree class_type = NULL_TREE;
14385 tree pushed_scope = NULL_TREE;
14387 /* In a first pass, parse default arguments to the functions.
14388 Then, in a second pass, parse the bodies of the functions.
14389 This two-phased approach handles cases like:
14397 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14398 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14399 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14400 TREE_PURPOSE (parser->unparsed_functions_queues)
14401 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14403 fn = TREE_VALUE (queue_entry);
14404 /* If there are default arguments that have not yet been processed,
14405 take care of them now. */
14406 if (class_type != TREE_PURPOSE (queue_entry))
14409 pop_scope (pushed_scope);
14410 class_type = TREE_PURPOSE (queue_entry);
14411 pushed_scope = push_scope (class_type);
14413 /* Make sure that any template parameters are in scope. */
14414 maybe_begin_member_template_processing (fn);
14415 /* Parse the default argument expressions. */
14416 cp_parser_late_parsing_default_args (parser, fn);
14417 /* Remove any template parameters from the symbol table. */
14418 maybe_end_member_template_processing ();
14421 pop_scope (pushed_scope);
14422 /* Now parse the body of the functions. */
14423 for (TREE_VALUE (parser->unparsed_functions_queues)
14424 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14425 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14426 TREE_VALUE (parser->unparsed_functions_queues)
14427 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14429 /* Figure out which function we need to process. */
14430 fn = TREE_VALUE (queue_entry);
14431 /* Parse the function. */
14432 cp_parser_late_parsing_for_member (parser, fn);
14436 /* Put back any saved access checks. */
14437 pop_deferring_access_checks ();
14439 /* Restore saved state. */
14440 parser->in_function_body = saved_in_function_body;
14441 parser->num_template_parameter_lists
14442 = saved_num_template_parameter_lists;
14447 /* Parse a class-head.
14450 class-key identifier [opt] base-clause [opt]
14451 class-key nested-name-specifier identifier base-clause [opt]
14452 class-key nested-name-specifier [opt] template-id
14456 class-key attributes identifier [opt] base-clause [opt]
14457 class-key attributes nested-name-specifier identifier base-clause [opt]
14458 class-key attributes nested-name-specifier [opt] template-id
14461 Upon return BASES is initialized to the list of base classes (or
14462 NULL, if there are none) in the same form returned by
14463 cp_parser_base_clause.
14465 Returns the TYPE of the indicated class. Sets
14466 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14467 involving a nested-name-specifier was used, and FALSE otherwise.
14469 Returns error_mark_node if this is not a class-head.
14471 Returns NULL_TREE if the class-head is syntactically valid, but
14472 semantically invalid in a way that means we should skip the entire
14473 body of the class. */
14476 cp_parser_class_head (cp_parser* parser,
14477 bool* nested_name_specifier_p,
14478 tree *attributes_p,
14481 tree nested_name_specifier;
14482 enum tag_types class_key;
14483 tree id = NULL_TREE;
14484 tree type = NULL_TREE;
14486 bool template_id_p = false;
14487 bool qualified_p = false;
14488 bool invalid_nested_name_p = false;
14489 bool invalid_explicit_specialization_p = false;
14490 tree pushed_scope = NULL_TREE;
14491 unsigned num_templates;
14493 /* Assume no nested-name-specifier will be present. */
14494 *nested_name_specifier_p = false;
14495 /* Assume no template parameter lists will be used in defining the
14499 *bases = NULL_TREE;
14501 /* Look for the class-key. */
14502 class_key = cp_parser_class_key (parser);
14503 if (class_key == none_type)
14504 return error_mark_node;
14506 /* Parse the attributes. */
14507 attributes = cp_parser_attributes_opt (parser);
14509 /* If the next token is `::', that is invalid -- but sometimes
14510 people do try to write:
14514 Handle this gracefully by accepting the extra qualifier, and then
14515 issuing an error about it later if this really is a
14516 class-head. If it turns out just to be an elaborated type
14517 specifier, remain silent. */
14518 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14519 qualified_p = true;
14521 push_deferring_access_checks (dk_no_check);
14523 /* Determine the name of the class. Begin by looking for an
14524 optional nested-name-specifier. */
14525 nested_name_specifier
14526 = cp_parser_nested_name_specifier_opt (parser,
14527 /*typename_keyword_p=*/false,
14528 /*check_dependency_p=*/false,
14530 /*is_declaration=*/false);
14531 /* If there was a nested-name-specifier, then there *must* be an
14533 if (nested_name_specifier)
14535 /* Although the grammar says `identifier', it really means
14536 `class-name' or `template-name'. You are only allowed to
14537 define a class that has already been declared with this
14540 The proposed resolution for Core Issue 180 says that wherever
14541 you see `class T::X' you should treat `X' as a type-name.
14543 It is OK to define an inaccessible class; for example:
14545 class A { class B; };
14548 We do not know if we will see a class-name, or a
14549 template-name. We look for a class-name first, in case the
14550 class-name is a template-id; if we looked for the
14551 template-name first we would stop after the template-name. */
14552 cp_parser_parse_tentatively (parser);
14553 type = cp_parser_class_name (parser,
14554 /*typename_keyword_p=*/false,
14555 /*template_keyword_p=*/false,
14557 /*check_dependency_p=*/false,
14558 /*class_head_p=*/true,
14559 /*is_declaration=*/false);
14560 /* If that didn't work, ignore the nested-name-specifier. */
14561 if (!cp_parser_parse_definitely (parser))
14563 invalid_nested_name_p = true;
14564 id = cp_parser_identifier (parser);
14565 if (id == error_mark_node)
14568 /* If we could not find a corresponding TYPE, treat this
14569 declaration like an unqualified declaration. */
14570 if (type == error_mark_node)
14571 nested_name_specifier = NULL_TREE;
14572 /* Otherwise, count the number of templates used in TYPE and its
14573 containing scopes. */
14578 for (scope = TREE_TYPE (type);
14579 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14580 scope = (TYPE_P (scope)
14581 ? TYPE_CONTEXT (scope)
14582 : DECL_CONTEXT (scope)))
14584 && CLASS_TYPE_P (scope)
14585 && CLASSTYPE_TEMPLATE_INFO (scope)
14586 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14587 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14591 /* Otherwise, the identifier is optional. */
14594 /* We don't know whether what comes next is a template-id,
14595 an identifier, or nothing at all. */
14596 cp_parser_parse_tentatively (parser);
14597 /* Check for a template-id. */
14598 id = cp_parser_template_id (parser,
14599 /*template_keyword_p=*/false,
14600 /*check_dependency_p=*/true,
14601 /*is_declaration=*/true);
14602 /* If that didn't work, it could still be an identifier. */
14603 if (!cp_parser_parse_definitely (parser))
14605 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14606 id = cp_parser_identifier (parser);
14612 template_id_p = true;
14617 pop_deferring_access_checks ();
14620 cp_parser_check_for_invalid_template_id (parser, id);
14622 /* If it's not a `:' or a `{' then we can't really be looking at a
14623 class-head, since a class-head only appears as part of a
14624 class-specifier. We have to detect this situation before calling
14625 xref_tag, since that has irreversible side-effects. */
14626 if (!cp_parser_next_token_starts_class_definition_p (parser))
14628 cp_parser_error (parser, "expected %<{%> or %<:%>");
14629 return error_mark_node;
14632 /* At this point, we're going ahead with the class-specifier, even
14633 if some other problem occurs. */
14634 cp_parser_commit_to_tentative_parse (parser);
14635 /* Issue the error about the overly-qualified name now. */
14637 cp_parser_error (parser,
14638 "global qualification of class name is invalid");
14639 else if (invalid_nested_name_p)
14640 cp_parser_error (parser,
14641 "qualified name does not name a class");
14642 else if (nested_name_specifier)
14646 /* Reject typedef-names in class heads. */
14647 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14649 error ("invalid class name in declaration of %qD", type);
14654 /* Figure out in what scope the declaration is being placed. */
14655 scope = current_scope ();
14656 /* If that scope does not contain the scope in which the
14657 class was originally declared, the program is invalid. */
14658 if (scope && !is_ancestor (scope, nested_name_specifier))
14660 if (at_namespace_scope_p ())
14661 error ("declaration of %qD in namespace %qD which does not "
14662 "enclose %qD", type, scope, nested_name_specifier);
14664 error ("declaration of %qD in %qD which does not enclose %qD",
14665 type, scope, nested_name_specifier);
14671 A declarator-id shall not be qualified exception of the
14672 definition of a ... nested class outside of its class
14673 ... [or] a the definition or explicit instantiation of a
14674 class member of a namespace outside of its namespace. */
14675 if (scope == nested_name_specifier)
14677 pedwarn ("extra qualification ignored");
14678 nested_name_specifier = NULL_TREE;
14682 /* An explicit-specialization must be preceded by "template <>". If
14683 it is not, try to recover gracefully. */
14684 if (at_namespace_scope_p ()
14685 && parser->num_template_parameter_lists == 0
14688 error ("an explicit specialization must be preceded by %<template <>%>");
14689 invalid_explicit_specialization_p = true;
14690 /* Take the same action that would have been taken by
14691 cp_parser_explicit_specialization. */
14692 ++parser->num_template_parameter_lists;
14693 begin_specialization ();
14695 /* There must be no "return" statements between this point and the
14696 end of this function; set "type "to the correct return value and
14697 use "goto done;" to return. */
14698 /* Make sure that the right number of template parameters were
14700 if (!cp_parser_check_template_parameters (parser, num_templates))
14702 /* If something went wrong, there is no point in even trying to
14703 process the class-definition. */
14708 /* Look up the type. */
14711 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14712 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14713 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14715 error ("function template %qD redeclared as a class template", id);
14716 type = error_mark_node;
14720 type = TREE_TYPE (id);
14721 type = maybe_process_partial_specialization (type);
14723 if (nested_name_specifier)
14724 pushed_scope = push_scope (nested_name_specifier);
14726 else if (nested_name_specifier)
14732 template <typename T> struct S { struct T };
14733 template <typename T> struct S<T>::T { };
14735 we will get a TYPENAME_TYPE when processing the definition of
14736 `S::T'. We need to resolve it to the actual type before we
14737 try to define it. */
14738 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14740 class_type = resolve_typename_type (TREE_TYPE (type),
14741 /*only_current_p=*/false);
14742 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14743 type = TYPE_NAME (class_type);
14746 cp_parser_error (parser, "could not resolve typename type");
14747 type = error_mark_node;
14751 maybe_process_partial_specialization (TREE_TYPE (type));
14752 class_type = current_class_type;
14753 /* Enter the scope indicated by the nested-name-specifier. */
14754 pushed_scope = push_scope (nested_name_specifier);
14755 /* Get the canonical version of this type. */
14756 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14757 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14758 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14760 type = push_template_decl (type);
14761 if (type == error_mark_node)
14768 type = TREE_TYPE (type);
14769 *nested_name_specifier_p = true;
14771 else /* The name is not a nested name. */
14773 /* If the class was unnamed, create a dummy name. */
14775 id = make_anon_name ();
14776 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14777 parser->num_template_parameter_lists);
14780 /* Indicate whether this class was declared as a `class' or as a
14782 if (TREE_CODE (type) == RECORD_TYPE)
14783 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14784 cp_parser_check_class_key (class_key, type);
14786 /* If this type was already complete, and we see another definition,
14787 that's an error. */
14788 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14790 error ("redefinition of %q#T", type);
14791 error ("previous definition of %q+#T", type);
14795 else if (type == error_mark_node)
14798 /* We will have entered the scope containing the class; the names of
14799 base classes should be looked up in that context. For example:
14801 struct A { struct B {}; struct C; };
14802 struct A::C : B {};
14806 /* Get the list of base-classes, if there is one. */
14807 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14808 *bases = cp_parser_base_clause (parser);
14811 /* Leave the scope given by the nested-name-specifier. We will
14812 enter the class scope itself while processing the members. */
14814 pop_scope (pushed_scope);
14816 if (invalid_explicit_specialization_p)
14818 end_specialization ();
14819 --parser->num_template_parameter_lists;
14821 *attributes_p = attributes;
14825 /* Parse a class-key.
14832 Returns the kind of class-key specified, or none_type to indicate
14835 static enum tag_types
14836 cp_parser_class_key (cp_parser* parser)
14839 enum tag_types tag_type;
14841 /* Look for the class-key. */
14842 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14846 /* Check to see if the TOKEN is a class-key. */
14847 tag_type = cp_parser_token_is_class_key (token);
14849 cp_parser_error (parser, "expected class-key");
14853 /* Parse an (optional) member-specification.
14855 member-specification:
14856 member-declaration member-specification [opt]
14857 access-specifier : member-specification [opt] */
14860 cp_parser_member_specification_opt (cp_parser* parser)
14867 /* Peek at the next token. */
14868 token = cp_lexer_peek_token (parser->lexer);
14869 /* If it's a `}', or EOF then we've seen all the members. */
14870 if (token->type == CPP_CLOSE_BRACE
14871 || token->type == CPP_EOF
14872 || token->type == CPP_PRAGMA_EOL)
14875 /* See if this token is a keyword. */
14876 keyword = token->keyword;
14880 case RID_PROTECTED:
14882 /* Consume the access-specifier. */
14883 cp_lexer_consume_token (parser->lexer);
14884 /* Remember which access-specifier is active. */
14885 current_access_specifier = token->u.value;
14886 /* Look for the `:'. */
14887 cp_parser_require (parser, CPP_COLON, "`:'");
14891 /* Accept #pragmas at class scope. */
14892 if (token->type == CPP_PRAGMA)
14894 cp_parser_pragma (parser, pragma_external);
14898 /* Otherwise, the next construction must be a
14899 member-declaration. */
14900 cp_parser_member_declaration (parser);
14905 /* Parse a member-declaration.
14907 member-declaration:
14908 decl-specifier-seq [opt] member-declarator-list [opt] ;
14909 function-definition ; [opt]
14910 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14912 template-declaration
14914 member-declarator-list:
14916 member-declarator-list , member-declarator
14919 declarator pure-specifier [opt]
14920 declarator constant-initializer [opt]
14921 identifier [opt] : constant-expression
14925 member-declaration:
14926 __extension__ member-declaration
14929 declarator attributes [opt] pure-specifier [opt]
14930 declarator attributes [opt] constant-initializer [opt]
14931 identifier [opt] attributes [opt] : constant-expression
14935 member-declaration:
14936 static_assert-declaration */
14939 cp_parser_member_declaration (cp_parser* parser)
14941 cp_decl_specifier_seq decl_specifiers;
14942 tree prefix_attributes;
14944 int declares_class_or_enum;
14947 int saved_pedantic;
14949 /* Check for the `__extension__' keyword. */
14950 if (cp_parser_extension_opt (parser, &saved_pedantic))
14953 cp_parser_member_declaration (parser);
14954 /* Restore the old value of the PEDANTIC flag. */
14955 pedantic = saved_pedantic;
14960 /* Check for a template-declaration. */
14961 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14963 /* An explicit specialization here is an error condition, and we
14964 expect the specialization handler to detect and report this. */
14965 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14966 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14967 cp_parser_explicit_specialization (parser);
14969 cp_parser_template_declaration (parser, /*member_p=*/true);
14974 /* Check for a using-declaration. */
14975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14977 /* Parse the using-declaration. */
14978 cp_parser_using_declaration (parser,
14979 /*access_declaration_p=*/false);
14983 /* Check for @defs. */
14984 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14987 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14988 ivar = ivar_chains;
14992 ivar = TREE_CHAIN (member);
14993 TREE_CHAIN (member) = NULL_TREE;
14994 finish_member_declaration (member);
14999 /* If the next token is `static_assert' we have a static assertion. */
15000 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15002 cp_parser_static_assert (parser, /*member_p=*/true);
15006 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15009 /* Parse the decl-specifier-seq. */
15010 cp_parser_decl_specifier_seq (parser,
15011 CP_PARSER_FLAGS_OPTIONAL,
15013 &declares_class_or_enum);
15014 prefix_attributes = decl_specifiers.attributes;
15015 decl_specifiers.attributes = NULL_TREE;
15016 /* Check for an invalid type-name. */
15017 if (!decl_specifiers.type
15018 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15020 /* If there is no declarator, then the decl-specifier-seq should
15022 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15024 /* If there was no decl-specifier-seq, and the next token is a
15025 `;', then we have something like:
15031 Each member-declaration shall declare at least one member
15032 name of the class. */
15033 if (!decl_specifiers.any_specifiers_p)
15035 cp_token *token = cp_lexer_peek_token (parser->lexer);
15036 if (pedantic && !token->in_system_header)
15037 pedwarn ("%Hextra %<;%>", &token->location);
15043 /* See if this declaration is a friend. */
15044 friend_p = cp_parser_friend_p (&decl_specifiers);
15045 /* If there were decl-specifiers, check to see if there was
15046 a class-declaration. */
15047 type = check_tag_decl (&decl_specifiers);
15048 /* Nested classes have already been added to the class, but
15049 a `friend' needs to be explicitly registered. */
15052 /* If the `friend' keyword was present, the friend must
15053 be introduced with a class-key. */
15054 if (!declares_class_or_enum)
15055 error ("a class-key must be used when declaring a friend");
15058 template <typename T> struct A {
15059 friend struct A<T>::B;
15062 A<T>::B will be represented by a TYPENAME_TYPE, and
15063 therefore not recognized by check_tag_decl. */
15065 && decl_specifiers.type
15066 && TYPE_P (decl_specifiers.type))
15067 type = decl_specifiers.type;
15068 if (!type || !TYPE_P (type))
15069 error ("friend declaration does not name a class or "
15072 make_friend_class (current_class_type, type,
15073 /*complain=*/true);
15075 /* If there is no TYPE, an error message will already have
15077 else if (!type || type == error_mark_node)
15079 /* An anonymous aggregate has to be handled specially; such
15080 a declaration really declares a data member (with a
15081 particular type), as opposed to a nested class. */
15082 else if (ANON_AGGR_TYPE_P (type))
15084 /* Remove constructors and such from TYPE, now that we
15085 know it is an anonymous aggregate. */
15086 fixup_anonymous_aggr (type);
15087 /* And make the corresponding data member. */
15088 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15089 /* Add it to the class. */
15090 finish_member_declaration (decl);
15093 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15098 /* See if these declarations will be friends. */
15099 friend_p = cp_parser_friend_p (&decl_specifiers);
15101 /* Keep going until we hit the `;' at the end of the
15103 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15105 tree attributes = NULL_TREE;
15106 tree first_attribute;
15108 /* Peek at the next token. */
15109 token = cp_lexer_peek_token (parser->lexer);
15111 /* Check for a bitfield declaration. */
15112 if (token->type == CPP_COLON
15113 || (token->type == CPP_NAME
15114 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15120 /* Get the name of the bitfield. Note that we cannot just
15121 check TOKEN here because it may have been invalidated by
15122 the call to cp_lexer_peek_nth_token above. */
15123 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15124 identifier = cp_parser_identifier (parser);
15126 identifier = NULL_TREE;
15128 /* Consume the `:' token. */
15129 cp_lexer_consume_token (parser->lexer);
15130 /* Get the width of the bitfield. */
15132 = cp_parser_constant_expression (parser,
15133 /*allow_non_constant=*/false,
15136 /* Look for attributes that apply to the bitfield. */
15137 attributes = cp_parser_attributes_opt (parser);
15138 /* Remember which attributes are prefix attributes and
15140 first_attribute = attributes;
15141 /* Combine the attributes. */
15142 attributes = chainon (prefix_attributes, attributes);
15144 /* Create the bitfield declaration. */
15145 decl = grokbitfield (identifier
15146 ? make_id_declarator (NULL_TREE,
15152 /* Apply the attributes. */
15153 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15157 cp_declarator *declarator;
15159 tree asm_specification;
15160 int ctor_dtor_or_conv_p;
15162 /* Parse the declarator. */
15164 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15165 &ctor_dtor_or_conv_p,
15166 /*parenthesized_p=*/NULL,
15167 /*member_p=*/true);
15169 /* If something went wrong parsing the declarator, make sure
15170 that we at least consume some tokens. */
15171 if (declarator == cp_error_declarator)
15173 /* Skip to the end of the statement. */
15174 cp_parser_skip_to_end_of_statement (parser);
15175 /* If the next token is not a semicolon, that is
15176 probably because we just skipped over the body of
15177 a function. So, we consume a semicolon if
15178 present, but do not issue an error message if it
15180 if (cp_lexer_next_token_is (parser->lexer,
15182 cp_lexer_consume_token (parser->lexer);
15186 if (declares_class_or_enum & 2)
15187 cp_parser_check_for_definition_in_return_type
15188 (declarator, decl_specifiers.type);
15190 /* Look for an asm-specification. */
15191 asm_specification = cp_parser_asm_specification_opt (parser);
15192 /* Look for attributes that apply to the declaration. */
15193 attributes = cp_parser_attributes_opt (parser);
15194 /* Remember which attributes are prefix attributes and
15196 first_attribute = attributes;
15197 /* Combine the attributes. */
15198 attributes = chainon (prefix_attributes, attributes);
15200 /* If it's an `=', then we have a constant-initializer or a
15201 pure-specifier. It is not correct to parse the
15202 initializer before registering the member declaration
15203 since the member declaration should be in scope while
15204 its initializer is processed. However, the rest of the
15205 front end does not yet provide an interface that allows
15206 us to handle this correctly. */
15207 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15211 A pure-specifier shall be used only in the declaration of
15212 a virtual function.
15214 A member-declarator can contain a constant-initializer
15215 only if it declares a static member of integral or
15218 Therefore, if the DECLARATOR is for a function, we look
15219 for a pure-specifier; otherwise, we look for a
15220 constant-initializer. When we call `grokfield', it will
15221 perform more stringent semantics checks. */
15222 if (function_declarator_p (declarator))
15223 initializer = cp_parser_pure_specifier (parser);
15225 /* Parse the initializer. */
15226 initializer = cp_parser_constant_initializer (parser);
15228 /* Otherwise, there is no initializer. */
15230 initializer = NULL_TREE;
15232 /* See if we are probably looking at a function
15233 definition. We are certainly not looking at a
15234 member-declarator. Calling `grokfield' has
15235 side-effects, so we must not do it unless we are sure
15236 that we are looking at a member-declarator. */
15237 if (cp_parser_token_starts_function_definition_p
15238 (cp_lexer_peek_token (parser->lexer)))
15240 /* The grammar does not allow a pure-specifier to be
15241 used when a member function is defined. (It is
15242 possible that this fact is an oversight in the
15243 standard, since a pure function may be defined
15244 outside of the class-specifier. */
15246 error ("pure-specifier on function-definition");
15247 decl = cp_parser_save_member_function_body (parser,
15251 /* If the member was not a friend, declare it here. */
15253 finish_member_declaration (decl);
15254 /* Peek at the next token. */
15255 token = cp_lexer_peek_token (parser->lexer);
15256 /* If the next token is a semicolon, consume it. */
15257 if (token->type == CPP_SEMICOLON)
15258 cp_lexer_consume_token (parser->lexer);
15262 /* Create the declaration. */
15263 decl = grokfield (declarator, &decl_specifiers,
15264 initializer, /*init_const_expr_p=*/true,
15269 /* Reset PREFIX_ATTRIBUTES. */
15270 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15271 attributes = TREE_CHAIN (attributes);
15273 TREE_CHAIN (attributes) = NULL_TREE;
15275 /* If there is any qualification still in effect, clear it
15276 now; we will be starting fresh with the next declarator. */
15277 parser->scope = NULL_TREE;
15278 parser->qualifying_scope = NULL_TREE;
15279 parser->object_scope = NULL_TREE;
15280 /* If it's a `,', then there are more declarators. */
15281 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15282 cp_lexer_consume_token (parser->lexer);
15283 /* If the next token isn't a `;', then we have a parse error. */
15284 else if (cp_lexer_next_token_is_not (parser->lexer,
15287 cp_parser_error (parser, "expected %<;%>");
15288 /* Skip tokens until we find a `;'. */
15289 cp_parser_skip_to_end_of_statement (parser);
15296 /* Add DECL to the list of members. */
15298 finish_member_declaration (decl);
15300 if (TREE_CODE (decl) == FUNCTION_DECL)
15301 cp_parser_save_default_args (parser, decl);
15306 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15309 /* Parse a pure-specifier.
15314 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15315 Otherwise, ERROR_MARK_NODE is returned. */
15318 cp_parser_pure_specifier (cp_parser* parser)
15322 /* Look for the `=' token. */
15323 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15324 return error_mark_node;
15325 /* Look for the `0' token. */
15326 token = cp_lexer_consume_token (parser->lexer);
15327 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15328 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15330 cp_parser_error (parser,
15331 "invalid pure specifier (only `= 0' is allowed)");
15332 cp_parser_skip_to_end_of_statement (parser);
15333 return error_mark_node;
15335 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15337 error ("templates may not be %<virtual%>");
15338 return error_mark_node;
15341 return integer_zero_node;
15344 /* Parse a constant-initializer.
15346 constant-initializer:
15347 = constant-expression
15349 Returns a representation of the constant-expression. */
15352 cp_parser_constant_initializer (cp_parser* parser)
15354 /* Look for the `=' token. */
15355 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15356 return error_mark_node;
15358 /* It is invalid to write:
15360 struct S { static const int i = { 7 }; };
15363 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15365 cp_parser_error (parser,
15366 "a brace-enclosed initializer is not allowed here");
15367 /* Consume the opening brace. */
15368 cp_lexer_consume_token (parser->lexer);
15369 /* Skip the initializer. */
15370 cp_parser_skip_to_closing_brace (parser);
15371 /* Look for the trailing `}'. */
15372 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15374 return error_mark_node;
15377 return cp_parser_constant_expression (parser,
15378 /*allow_non_constant=*/false,
15382 /* Derived classes [gram.class.derived] */
15384 /* Parse a base-clause.
15387 : base-specifier-list
15389 base-specifier-list:
15390 base-specifier ... [opt]
15391 base-specifier-list , base-specifier ... [opt]
15393 Returns a TREE_LIST representing the base-classes, in the order in
15394 which they were declared. The representation of each node is as
15395 described by cp_parser_base_specifier.
15397 In the case that no bases are specified, this function will return
15398 NULL_TREE, not ERROR_MARK_NODE. */
15401 cp_parser_base_clause (cp_parser* parser)
15403 tree bases = NULL_TREE;
15405 /* Look for the `:' that begins the list. */
15406 cp_parser_require (parser, CPP_COLON, "`:'");
15408 /* Scan the base-specifier-list. */
15413 bool pack_expansion_p = false;
15415 /* Look for the base-specifier. */
15416 base = cp_parser_base_specifier (parser);
15417 /* Look for the (optional) ellipsis. */
15418 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15420 /* Consume the `...'. */
15421 cp_lexer_consume_token (parser->lexer);
15423 pack_expansion_p = true;
15426 /* Add BASE to the front of the list. */
15427 if (base != error_mark_node)
15429 if (pack_expansion_p)
15430 /* Make this a pack expansion type. */
15431 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15434 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15436 TREE_CHAIN (base) = bases;
15440 /* Peek at the next token. */
15441 token = cp_lexer_peek_token (parser->lexer);
15442 /* If it's not a comma, then the list is complete. */
15443 if (token->type != CPP_COMMA)
15445 /* Consume the `,'. */
15446 cp_lexer_consume_token (parser->lexer);
15449 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15450 base class had a qualified name. However, the next name that
15451 appears is certainly not qualified. */
15452 parser->scope = NULL_TREE;
15453 parser->qualifying_scope = NULL_TREE;
15454 parser->object_scope = NULL_TREE;
15456 return nreverse (bases);
15459 /* Parse a base-specifier.
15462 :: [opt] nested-name-specifier [opt] class-name
15463 virtual access-specifier [opt] :: [opt] nested-name-specifier
15465 access-specifier virtual [opt] :: [opt] nested-name-specifier
15468 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15469 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15470 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15471 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15474 cp_parser_base_specifier (cp_parser* parser)
15478 bool virtual_p = false;
15479 bool duplicate_virtual_error_issued_p = false;
15480 bool duplicate_access_error_issued_p = false;
15481 bool class_scope_p, template_p;
15482 tree access = access_default_node;
15485 /* Process the optional `virtual' and `access-specifier'. */
15488 /* Peek at the next token. */
15489 token = cp_lexer_peek_token (parser->lexer);
15490 /* Process `virtual'. */
15491 switch (token->keyword)
15494 /* If `virtual' appears more than once, issue an error. */
15495 if (virtual_p && !duplicate_virtual_error_issued_p)
15497 cp_parser_error (parser,
15498 "%<virtual%> specified more than once in base-specified");
15499 duplicate_virtual_error_issued_p = true;
15504 /* Consume the `virtual' token. */
15505 cp_lexer_consume_token (parser->lexer);
15510 case RID_PROTECTED:
15512 /* If more than one access specifier appears, issue an
15514 if (access != access_default_node
15515 && !duplicate_access_error_issued_p)
15517 cp_parser_error (parser,
15518 "more than one access specifier in base-specified");
15519 duplicate_access_error_issued_p = true;
15522 access = ridpointers[(int) token->keyword];
15524 /* Consume the access-specifier. */
15525 cp_lexer_consume_token (parser->lexer);
15534 /* It is not uncommon to see programs mechanically, erroneously, use
15535 the 'typename' keyword to denote (dependent) qualified types
15536 as base classes. */
15537 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15539 if (!processing_template_decl)
15540 error ("keyword %<typename%> not allowed outside of templates");
15542 error ("keyword %<typename%> not allowed in this context "
15543 "(the base class is implicitly a type)");
15544 cp_lexer_consume_token (parser->lexer);
15547 /* Look for the optional `::' operator. */
15548 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15549 /* Look for the nested-name-specifier. The simplest way to
15554 The keyword `typename' is not permitted in a base-specifier or
15555 mem-initializer; in these contexts a qualified name that
15556 depends on a template-parameter is implicitly assumed to be a
15559 is to pretend that we have seen the `typename' keyword at this
15561 cp_parser_nested_name_specifier_opt (parser,
15562 /*typename_keyword_p=*/true,
15563 /*check_dependency_p=*/true,
15565 /*is_declaration=*/true);
15566 /* If the base class is given by a qualified name, assume that names
15567 we see are type names or templates, as appropriate. */
15568 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15569 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15571 /* Finally, look for the class-name. */
15572 type = cp_parser_class_name (parser,
15576 /*check_dependency_p=*/true,
15577 /*class_head_p=*/false,
15578 /*is_declaration=*/true);
15580 if (type == error_mark_node)
15581 return error_mark_node;
15583 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15586 /* Exception handling [gram.exception] */
15588 /* Parse an (optional) exception-specification.
15590 exception-specification:
15591 throw ( type-id-list [opt] )
15593 Returns a TREE_LIST representing the exception-specification. The
15594 TREE_VALUE of each node is a type. */
15597 cp_parser_exception_specification_opt (cp_parser* parser)
15602 /* Peek at the next token. */
15603 token = cp_lexer_peek_token (parser->lexer);
15604 /* If it's not `throw', then there's no exception-specification. */
15605 if (!cp_parser_is_keyword (token, RID_THROW))
15608 /* Consume the `throw'. */
15609 cp_lexer_consume_token (parser->lexer);
15611 /* Look for the `('. */
15612 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15614 /* Peek at the next token. */
15615 token = cp_lexer_peek_token (parser->lexer);
15616 /* If it's not a `)', then there is a type-id-list. */
15617 if (token->type != CPP_CLOSE_PAREN)
15619 const char *saved_message;
15621 /* Types may not be defined in an exception-specification. */
15622 saved_message = parser->type_definition_forbidden_message;
15623 parser->type_definition_forbidden_message
15624 = "types may not be defined in an exception-specification";
15625 /* Parse the type-id-list. */
15626 type_id_list = cp_parser_type_id_list (parser);
15627 /* Restore the saved message. */
15628 parser->type_definition_forbidden_message = saved_message;
15631 type_id_list = empty_except_spec;
15633 /* Look for the `)'. */
15634 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15636 return type_id_list;
15639 /* Parse an (optional) type-id-list.
15643 type-id-list , type-id ... [opt]
15645 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15646 in the order that the types were presented. */
15649 cp_parser_type_id_list (cp_parser* parser)
15651 tree types = NULL_TREE;
15658 /* Get the next type-id. */
15659 type = cp_parser_type_id (parser);
15660 /* Parse the optional ellipsis. */
15661 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15663 /* Consume the `...'. */
15664 cp_lexer_consume_token (parser->lexer);
15666 /* Turn the type into a pack expansion expression. */
15667 type = make_pack_expansion (type);
15669 /* Add it to the list. */
15670 types = add_exception_specifier (types, type, /*complain=*/1);
15671 /* Peek at the next token. */
15672 token = cp_lexer_peek_token (parser->lexer);
15673 /* If it is not a `,', we are done. */
15674 if (token->type != CPP_COMMA)
15676 /* Consume the `,'. */
15677 cp_lexer_consume_token (parser->lexer);
15680 return nreverse (types);
15683 /* Parse a try-block.
15686 try compound-statement handler-seq */
15689 cp_parser_try_block (cp_parser* parser)
15693 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15694 try_block = begin_try_block ();
15695 cp_parser_compound_statement (parser, NULL, true);
15696 finish_try_block (try_block);
15697 cp_parser_handler_seq (parser);
15698 finish_handler_sequence (try_block);
15703 /* Parse a function-try-block.
15705 function-try-block:
15706 try ctor-initializer [opt] function-body handler-seq */
15709 cp_parser_function_try_block (cp_parser* parser)
15711 tree compound_stmt;
15713 bool ctor_initializer_p;
15715 /* Look for the `try' keyword. */
15716 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15718 /* Let the rest of the front end know where we are. */
15719 try_block = begin_function_try_block (&compound_stmt);
15720 /* Parse the function-body. */
15722 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15723 /* We're done with the `try' part. */
15724 finish_function_try_block (try_block);
15725 /* Parse the handlers. */
15726 cp_parser_handler_seq (parser);
15727 /* We're done with the handlers. */
15728 finish_function_handler_sequence (try_block, compound_stmt);
15730 return ctor_initializer_p;
15733 /* Parse a handler-seq.
15736 handler handler-seq [opt] */
15739 cp_parser_handler_seq (cp_parser* parser)
15745 /* Parse the handler. */
15746 cp_parser_handler (parser);
15747 /* Peek at the next token. */
15748 token = cp_lexer_peek_token (parser->lexer);
15749 /* If it's not `catch' then there are no more handlers. */
15750 if (!cp_parser_is_keyword (token, RID_CATCH))
15755 /* Parse a handler.
15758 catch ( exception-declaration ) compound-statement */
15761 cp_parser_handler (cp_parser* parser)
15766 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15767 handler = begin_handler ();
15768 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15769 declaration = cp_parser_exception_declaration (parser);
15770 finish_handler_parms (declaration, handler);
15771 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15772 cp_parser_compound_statement (parser, NULL, false);
15773 finish_handler (handler);
15776 /* Parse an exception-declaration.
15778 exception-declaration:
15779 type-specifier-seq declarator
15780 type-specifier-seq abstract-declarator
15784 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15785 ellipsis variant is used. */
15788 cp_parser_exception_declaration (cp_parser* parser)
15790 cp_decl_specifier_seq type_specifiers;
15791 cp_declarator *declarator;
15792 const char *saved_message;
15794 /* If it's an ellipsis, it's easy to handle. */
15795 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15797 /* Consume the `...' token. */
15798 cp_lexer_consume_token (parser->lexer);
15802 /* Types may not be defined in exception-declarations. */
15803 saved_message = parser->type_definition_forbidden_message;
15804 parser->type_definition_forbidden_message
15805 = "types may not be defined in exception-declarations";
15807 /* Parse the type-specifier-seq. */
15808 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15810 /* If it's a `)', then there is no declarator. */
15811 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15814 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15815 /*ctor_dtor_or_conv_p=*/NULL,
15816 /*parenthesized_p=*/NULL,
15817 /*member_p=*/false);
15819 /* Restore the saved message. */
15820 parser->type_definition_forbidden_message = saved_message;
15822 if (!type_specifiers.any_specifiers_p)
15823 return error_mark_node;
15825 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15828 /* Parse a throw-expression.
15831 throw assignment-expression [opt]
15833 Returns a THROW_EXPR representing the throw-expression. */
15836 cp_parser_throw_expression (cp_parser* parser)
15841 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15842 token = cp_lexer_peek_token (parser->lexer);
15843 /* Figure out whether or not there is an assignment-expression
15844 following the "throw" keyword. */
15845 if (token->type == CPP_COMMA
15846 || token->type == CPP_SEMICOLON
15847 || token->type == CPP_CLOSE_PAREN
15848 || token->type == CPP_CLOSE_SQUARE
15849 || token->type == CPP_CLOSE_BRACE
15850 || token->type == CPP_COLON)
15851 expression = NULL_TREE;
15853 expression = cp_parser_assignment_expression (parser,
15856 return build_throw (expression);
15859 /* GNU Extensions */
15861 /* Parse an (optional) asm-specification.
15864 asm ( string-literal )
15866 If the asm-specification is present, returns a STRING_CST
15867 corresponding to the string-literal. Otherwise, returns
15871 cp_parser_asm_specification_opt (cp_parser* parser)
15874 tree asm_specification;
15876 /* Peek at the next token. */
15877 token = cp_lexer_peek_token (parser->lexer);
15878 /* If the next token isn't the `asm' keyword, then there's no
15879 asm-specification. */
15880 if (!cp_parser_is_keyword (token, RID_ASM))
15883 /* Consume the `asm' token. */
15884 cp_lexer_consume_token (parser->lexer);
15885 /* Look for the `('. */
15886 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15888 /* Look for the string-literal. */
15889 asm_specification = cp_parser_string_literal (parser, false, false);
15891 /* Look for the `)'. */
15892 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15894 return asm_specification;
15897 /* Parse an asm-operand-list.
15901 asm-operand-list , asm-operand
15904 string-literal ( expression )
15905 [ string-literal ] string-literal ( expression )
15907 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15908 each node is the expression. The TREE_PURPOSE is itself a
15909 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15910 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15911 is a STRING_CST for the string literal before the parenthesis. Returns
15912 ERROR_MARK_NODE if any of the operands are invalid. */
15915 cp_parser_asm_operand_list (cp_parser* parser)
15917 tree asm_operands = NULL_TREE;
15918 bool invalid_operands = false;
15922 tree string_literal;
15926 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15928 /* Consume the `[' token. */
15929 cp_lexer_consume_token (parser->lexer);
15930 /* Read the operand name. */
15931 name = cp_parser_identifier (parser);
15932 if (name != error_mark_node)
15933 name = build_string (IDENTIFIER_LENGTH (name),
15934 IDENTIFIER_POINTER (name));
15935 /* Look for the closing `]'. */
15936 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15940 /* Look for the string-literal. */
15941 string_literal = cp_parser_string_literal (parser, false, false);
15943 /* Look for the `('. */
15944 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15945 /* Parse the expression. */
15946 expression = cp_parser_expression (parser, /*cast_p=*/false);
15947 /* Look for the `)'. */
15948 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15950 if (name == error_mark_node
15951 || string_literal == error_mark_node
15952 || expression == error_mark_node)
15953 invalid_operands = true;
15955 /* Add this operand to the list. */
15956 asm_operands = tree_cons (build_tree_list (name, string_literal),
15959 /* If the next token is not a `,', there are no more
15961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15963 /* Consume the `,'. */
15964 cp_lexer_consume_token (parser->lexer);
15967 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15970 /* Parse an asm-clobber-list.
15974 asm-clobber-list , string-literal
15976 Returns a TREE_LIST, indicating the clobbers in the order that they
15977 appeared. The TREE_VALUE of each node is a STRING_CST. */
15980 cp_parser_asm_clobber_list (cp_parser* parser)
15982 tree clobbers = NULL_TREE;
15986 tree string_literal;
15988 /* Look for the string literal. */
15989 string_literal = cp_parser_string_literal (parser, false, false);
15990 /* Add it to the list. */
15991 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15992 /* If the next token is not a `,', then the list is
15994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15996 /* Consume the `,' token. */
15997 cp_lexer_consume_token (parser->lexer);
16003 /* Parse an (optional) series of attributes.
16006 attributes attribute
16009 __attribute__ (( attribute-list [opt] ))
16011 The return value is as for cp_parser_attribute_list. */
16014 cp_parser_attributes_opt (cp_parser* parser)
16016 tree attributes = NULL_TREE;
16021 tree attribute_list;
16023 /* Peek at the next token. */
16024 token = cp_lexer_peek_token (parser->lexer);
16025 /* If it's not `__attribute__', then we're done. */
16026 if (token->keyword != RID_ATTRIBUTE)
16029 /* Consume the `__attribute__' keyword. */
16030 cp_lexer_consume_token (parser->lexer);
16031 /* Look for the two `(' tokens. */
16032 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16033 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16035 /* Peek at the next token. */
16036 token = cp_lexer_peek_token (parser->lexer);
16037 if (token->type != CPP_CLOSE_PAREN)
16038 /* Parse the attribute-list. */
16039 attribute_list = cp_parser_attribute_list (parser);
16041 /* If the next token is a `)', then there is no attribute
16043 attribute_list = NULL;
16045 /* Look for the two `)' tokens. */
16046 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16047 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16049 /* Add these new attributes to the list. */
16050 attributes = chainon (attributes, attribute_list);
16056 /* Parse an attribute-list.
16060 attribute-list , attribute
16064 identifier ( identifier )
16065 identifier ( identifier , expression-list )
16066 identifier ( expression-list )
16068 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16069 to an attribute. The TREE_PURPOSE of each node is the identifier
16070 indicating which attribute is in use. The TREE_VALUE represents
16071 the arguments, if any. */
16074 cp_parser_attribute_list (cp_parser* parser)
16076 tree attribute_list = NULL_TREE;
16077 bool save_translate_strings_p = parser->translate_strings_p;
16079 parser->translate_strings_p = false;
16086 /* Look for the identifier. We also allow keywords here; for
16087 example `__attribute__ ((const))' is legal. */
16088 token = cp_lexer_peek_token (parser->lexer);
16089 if (token->type == CPP_NAME
16090 || token->type == CPP_KEYWORD)
16092 tree arguments = NULL_TREE;
16094 /* Consume the token. */
16095 token = cp_lexer_consume_token (parser->lexer);
16097 /* Save away the identifier that indicates which attribute
16099 identifier = token->u.value;
16100 attribute = build_tree_list (identifier, NULL_TREE);
16102 /* Peek at the next token. */
16103 token = cp_lexer_peek_token (parser->lexer);
16104 /* If it's an `(', then parse the attribute arguments. */
16105 if (token->type == CPP_OPEN_PAREN)
16107 arguments = cp_parser_parenthesized_expression_list
16108 (parser, true, /*cast_p=*/false,
16109 /*allow_expansion_p=*/false,
16110 /*non_constant_p=*/NULL);
16111 /* Save the arguments away. */
16112 TREE_VALUE (attribute) = arguments;
16115 if (arguments != error_mark_node)
16117 /* Add this attribute to the list. */
16118 TREE_CHAIN (attribute) = attribute_list;
16119 attribute_list = attribute;
16122 token = cp_lexer_peek_token (parser->lexer);
16124 /* Now, look for more attributes. If the next token isn't a
16125 `,', we're done. */
16126 if (token->type != CPP_COMMA)
16129 /* Consume the comma and keep going. */
16130 cp_lexer_consume_token (parser->lexer);
16132 parser->translate_strings_p = save_translate_strings_p;
16134 /* We built up the list in reverse order. */
16135 return nreverse (attribute_list);
16138 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16139 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16140 current value of the PEDANTIC flag, regardless of whether or not
16141 the `__extension__' keyword is present. The caller is responsible
16142 for restoring the value of the PEDANTIC flag. */
16145 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16147 /* Save the old value of the PEDANTIC flag. */
16148 *saved_pedantic = pedantic;
16150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16152 /* Consume the `__extension__' token. */
16153 cp_lexer_consume_token (parser->lexer);
16154 /* We're not being pedantic while the `__extension__' keyword is
16164 /* Parse a label declaration.
16167 __label__ label-declarator-seq ;
16169 label-declarator-seq:
16170 identifier , label-declarator-seq
16174 cp_parser_label_declaration (cp_parser* parser)
16176 /* Look for the `__label__' keyword. */
16177 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16183 /* Look for an identifier. */
16184 identifier = cp_parser_identifier (parser);
16185 /* If we failed, stop. */
16186 if (identifier == error_mark_node)
16188 /* Declare it as a label. */
16189 finish_label_decl (identifier);
16190 /* If the next token is a `;', stop. */
16191 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16193 /* Look for the `,' separating the label declarations. */
16194 cp_parser_require (parser, CPP_COMMA, "`,'");
16197 /* Look for the final `;'. */
16198 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16201 /* Support Functions */
16203 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16204 NAME should have one of the representations used for an
16205 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16206 is returned. If PARSER->SCOPE is a dependent type, then a
16207 SCOPE_REF is returned.
16209 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16210 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16211 was formed. Abstractly, such entities should not be passed to this
16212 function, because they do not need to be looked up, but it is
16213 simpler to check for this special case here, rather than at the
16216 In cases not explicitly covered above, this function returns a
16217 DECL, OVERLOAD, or baselink representing the result of the lookup.
16218 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16221 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16222 (e.g., "struct") that was used. In that case bindings that do not
16223 refer to types are ignored.
16225 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16228 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16231 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16234 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16235 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16236 NULL_TREE otherwise. */
16239 cp_parser_lookup_name (cp_parser *parser, tree name,
16240 enum tag_types tag_type,
16243 bool check_dependency,
16244 tree *ambiguous_decls)
16248 tree object_type = parser->context->object_type;
16250 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16251 flags |= LOOKUP_COMPLAIN;
16253 /* Assume that the lookup will be unambiguous. */
16254 if (ambiguous_decls)
16255 *ambiguous_decls = NULL_TREE;
16257 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16258 no longer valid. Note that if we are parsing tentatively, and
16259 the parse fails, OBJECT_TYPE will be automatically restored. */
16260 parser->context->object_type = NULL_TREE;
16262 if (name == error_mark_node)
16263 return error_mark_node;
16265 /* A template-id has already been resolved; there is no lookup to
16267 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16269 if (BASELINK_P (name))
16271 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16272 == TEMPLATE_ID_EXPR);
16276 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16277 it should already have been checked to make sure that the name
16278 used matches the type being destroyed. */
16279 if (TREE_CODE (name) == BIT_NOT_EXPR)
16283 /* Figure out to which type this destructor applies. */
16285 type = parser->scope;
16286 else if (object_type)
16287 type = object_type;
16289 type = current_class_type;
16290 /* If that's not a class type, there is no destructor. */
16291 if (!type || !CLASS_TYPE_P (type))
16292 return error_mark_node;
16293 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16294 lazily_declare_fn (sfk_destructor, type);
16295 if (!CLASSTYPE_DESTRUCTORS (type))
16296 return error_mark_node;
16297 /* If it was a class type, return the destructor. */
16298 return CLASSTYPE_DESTRUCTORS (type);
16301 /* By this point, the NAME should be an ordinary identifier. If
16302 the id-expression was a qualified name, the qualifying scope is
16303 stored in PARSER->SCOPE at this point. */
16304 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16306 /* Perform the lookup. */
16311 if (parser->scope == error_mark_node)
16312 return error_mark_node;
16314 /* If the SCOPE is dependent, the lookup must be deferred until
16315 the template is instantiated -- unless we are explicitly
16316 looking up names in uninstantiated templates. Even then, we
16317 cannot look up the name if the scope is not a class type; it
16318 might, for example, be a template type parameter. */
16319 dependent_p = (TYPE_P (parser->scope)
16320 && !(parser->in_declarator_p
16321 && currently_open_class (parser->scope))
16322 && dependent_type_p (parser->scope));
16323 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16330 /* The resolution to Core Issue 180 says that `struct
16331 A::B' should be considered a type-name, even if `A'
16333 type = make_typename_type (parser->scope, name, tag_type,
16334 /*complain=*/tf_error);
16335 decl = TYPE_NAME (type);
16337 else if (is_template
16338 && (cp_parser_next_token_ends_template_argument_p (parser)
16339 || cp_lexer_next_token_is (parser->lexer,
16341 decl = make_unbound_class_template (parser->scope,
16343 /*complain=*/tf_error);
16345 decl = build_qualified_name (/*type=*/NULL_TREE,
16346 parser->scope, name,
16351 tree pushed_scope = NULL_TREE;
16353 /* If PARSER->SCOPE is a dependent type, then it must be a
16354 class type, and we must not be checking dependencies;
16355 otherwise, we would have processed this lookup above. So
16356 that PARSER->SCOPE is not considered a dependent base by
16357 lookup_member, we must enter the scope here. */
16359 pushed_scope = push_scope (parser->scope);
16360 /* If the PARSER->SCOPE is a template specialization, it
16361 may be instantiated during name lookup. In that case,
16362 errors may be issued. Even if we rollback the current
16363 tentative parse, those errors are valid. */
16364 decl = lookup_qualified_name (parser->scope, name,
16365 tag_type != none_type,
16366 /*complain=*/true);
16368 pop_scope (pushed_scope);
16370 parser->qualifying_scope = parser->scope;
16371 parser->object_scope = NULL_TREE;
16373 else if (object_type)
16375 tree object_decl = NULL_TREE;
16376 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16377 OBJECT_TYPE is not a class. */
16378 if (CLASS_TYPE_P (object_type))
16379 /* If the OBJECT_TYPE is a template specialization, it may
16380 be instantiated during name lookup. In that case, errors
16381 may be issued. Even if we rollback the current tentative
16382 parse, those errors are valid. */
16383 object_decl = lookup_member (object_type,
16386 tag_type != none_type);
16387 /* Look it up in the enclosing context, too. */
16388 decl = lookup_name_real (name, tag_type != none_type,
16390 /*block_p=*/true, is_namespace, flags);
16391 parser->object_scope = object_type;
16392 parser->qualifying_scope = NULL_TREE;
16394 decl = object_decl;
16398 decl = lookup_name_real (name, tag_type != none_type,
16400 /*block_p=*/true, is_namespace, flags);
16401 parser->qualifying_scope = NULL_TREE;
16402 parser->object_scope = NULL_TREE;
16405 /* If the lookup failed, let our caller know. */
16406 if (!decl || decl == error_mark_node)
16407 return error_mark_node;
16409 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16410 if (TREE_CODE (decl) == TREE_LIST)
16412 if (ambiguous_decls)
16413 *ambiguous_decls = decl;
16414 /* The error message we have to print is too complicated for
16415 cp_parser_error, so we incorporate its actions directly. */
16416 if (!cp_parser_simulate_error (parser))
16418 error ("reference to %qD is ambiguous", name);
16419 print_candidates (decl);
16421 return error_mark_node;
16424 gcc_assert (DECL_P (decl)
16425 || TREE_CODE (decl) == OVERLOAD
16426 || TREE_CODE (decl) == SCOPE_REF
16427 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16428 || BASELINK_P (decl));
16430 /* If we have resolved the name of a member declaration, check to
16431 see if the declaration is accessible. When the name resolves to
16432 set of overloaded functions, accessibility is checked when
16433 overload resolution is done.
16435 During an explicit instantiation, access is not checked at all,
16436 as per [temp.explicit]. */
16438 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16443 /* Like cp_parser_lookup_name, but for use in the typical case where
16444 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16445 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16448 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16450 return cp_parser_lookup_name (parser, name,
16452 /*is_template=*/false,
16453 /*is_namespace=*/false,
16454 /*check_dependency=*/true,
16455 /*ambiguous_decls=*/NULL);
16458 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16459 the current context, return the TYPE_DECL. If TAG_NAME_P is
16460 true, the DECL indicates the class being defined in a class-head,
16461 or declared in an elaborated-type-specifier.
16463 Otherwise, return DECL. */
16466 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16468 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16469 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16472 template <typename T> struct B;
16475 template <typename T> struct A::B {};
16477 Similarly, in an elaborated-type-specifier:
16479 namespace N { struct X{}; }
16482 template <typename T> friend struct N::X;
16485 However, if the DECL refers to a class type, and we are in
16486 the scope of the class, then the name lookup automatically
16487 finds the TYPE_DECL created by build_self_reference rather
16488 than a TEMPLATE_DECL. For example, in:
16490 template <class T> struct S {
16494 there is no need to handle such case. */
16496 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16497 return DECL_TEMPLATE_RESULT (decl);
16502 /* If too many, or too few, template-parameter lists apply to the
16503 declarator, issue an error message. Returns TRUE if all went well,
16504 and FALSE otherwise. */
16507 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16508 cp_declarator *declarator)
16510 unsigned num_templates;
16512 /* We haven't seen any classes that involve template parameters yet. */
16515 switch (declarator->kind)
16518 if (declarator->u.id.qualifying_scope)
16523 scope = declarator->u.id.qualifying_scope;
16524 member = declarator->u.id.unqualified_name;
16526 while (scope && CLASS_TYPE_P (scope))
16528 /* You're supposed to have one `template <...>'
16529 for every template class, but you don't need one
16530 for a full specialization. For example:
16532 template <class T> struct S{};
16533 template <> struct S<int> { void f(); };
16534 void S<int>::f () {}
16536 is correct; there shouldn't be a `template <>' for
16537 the definition of `S<int>::f'. */
16538 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16539 /* If SCOPE does not have template information of any
16540 kind, then it is not a template, nor is it nested
16541 within a template. */
16543 if (explicit_class_specialization_p (scope))
16545 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16548 scope = TYPE_CONTEXT (scope);
16551 else if (TREE_CODE (declarator->u.id.unqualified_name)
16552 == TEMPLATE_ID_EXPR)
16553 /* If the DECLARATOR has the form `X<y>' then it uses one
16554 additional level of template parameters. */
16557 return cp_parser_check_template_parameters (parser,
16563 case cdk_reference:
16565 return (cp_parser_check_declarator_template_parameters
16566 (parser, declarator->declarator));
16572 gcc_unreachable ();
16577 /* NUM_TEMPLATES were used in the current declaration. If that is
16578 invalid, return FALSE and issue an error messages. Otherwise,
16582 cp_parser_check_template_parameters (cp_parser* parser,
16583 unsigned num_templates)
16585 /* If there are more template classes than parameter lists, we have
16588 template <class T> void S<T>::R<T>::f (); */
16589 if (parser->num_template_parameter_lists < num_templates)
16591 error ("too few template-parameter-lists");
16594 /* If there are the same number of template classes and parameter
16595 lists, that's OK. */
16596 if (parser->num_template_parameter_lists == num_templates)
16598 /* If there are more, but only one more, then we are referring to a
16599 member template. That's OK too. */
16600 if (parser->num_template_parameter_lists == num_templates + 1)
16602 /* Otherwise, there are too many template parameter lists. We have
16605 template <class T> template <class U> void S::f(); */
16606 error ("too many template-parameter-lists");
16610 /* Parse an optional `::' token indicating that the following name is
16611 from the global namespace. If so, PARSER->SCOPE is set to the
16612 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16613 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16614 Returns the new value of PARSER->SCOPE, if the `::' token is
16615 present, and NULL_TREE otherwise. */
16618 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16622 /* Peek at the next token. */
16623 token = cp_lexer_peek_token (parser->lexer);
16624 /* If we're looking at a `::' token then we're starting from the
16625 global namespace, not our current location. */
16626 if (token->type == CPP_SCOPE)
16628 /* Consume the `::' token. */
16629 cp_lexer_consume_token (parser->lexer);
16630 /* Set the SCOPE so that we know where to start the lookup. */
16631 parser->scope = global_namespace;
16632 parser->qualifying_scope = global_namespace;
16633 parser->object_scope = NULL_TREE;
16635 return parser->scope;
16637 else if (!current_scope_valid_p)
16639 parser->scope = NULL_TREE;
16640 parser->qualifying_scope = NULL_TREE;
16641 parser->object_scope = NULL_TREE;
16647 /* Returns TRUE if the upcoming token sequence is the start of a
16648 constructor declarator. If FRIEND_P is true, the declarator is
16649 preceded by the `friend' specifier. */
16652 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16654 bool constructor_p;
16655 tree type_decl = NULL_TREE;
16656 bool nested_name_p;
16657 cp_token *next_token;
16659 /* The common case is that this is not a constructor declarator, so
16660 try to avoid doing lots of work if at all possible. It's not
16661 valid declare a constructor at function scope. */
16662 if (parser->in_function_body)
16664 /* And only certain tokens can begin a constructor declarator. */
16665 next_token = cp_lexer_peek_token (parser->lexer);
16666 if (next_token->type != CPP_NAME
16667 && next_token->type != CPP_SCOPE
16668 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16669 && next_token->type != CPP_TEMPLATE_ID)
16672 /* Parse tentatively; we are going to roll back all of the tokens
16674 cp_parser_parse_tentatively (parser);
16675 /* Assume that we are looking at a constructor declarator. */
16676 constructor_p = true;
16678 /* Look for the optional `::' operator. */
16679 cp_parser_global_scope_opt (parser,
16680 /*current_scope_valid_p=*/false);
16681 /* Look for the nested-name-specifier. */
16683 = (cp_parser_nested_name_specifier_opt (parser,
16684 /*typename_keyword_p=*/false,
16685 /*check_dependency_p=*/false,
16687 /*is_declaration=*/false)
16689 /* Outside of a class-specifier, there must be a
16690 nested-name-specifier. */
16691 if (!nested_name_p &&
16692 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16694 constructor_p = false;
16695 /* If we still think that this might be a constructor-declarator,
16696 look for a class-name. */
16701 template <typename T> struct S { S(); };
16702 template <typename T> S<T>::S ();
16704 we must recognize that the nested `S' names a class.
16707 template <typename T> S<T>::S<T> ();
16709 we must recognize that the nested `S' names a template. */
16710 type_decl = cp_parser_class_name (parser,
16711 /*typename_keyword_p=*/false,
16712 /*template_keyword_p=*/false,
16714 /*check_dependency_p=*/false,
16715 /*class_head_p=*/false,
16716 /*is_declaration=*/false);
16717 /* If there was no class-name, then this is not a constructor. */
16718 constructor_p = !cp_parser_error_occurred (parser);
16721 /* If we're still considering a constructor, we have to see a `(',
16722 to begin the parameter-declaration-clause, followed by either a
16723 `)', an `...', or a decl-specifier. We need to check for a
16724 type-specifier to avoid being fooled into thinking that:
16728 is a constructor. (It is actually a function named `f' that
16729 takes one parameter (of type `int') and returns a value of type
16732 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16735 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16736 /* A parameter declaration begins with a decl-specifier,
16737 which is either the "attribute" keyword, a storage class
16738 specifier, or (usually) a type-specifier. */
16739 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16742 tree pushed_scope = NULL_TREE;
16743 unsigned saved_num_template_parameter_lists;
16745 /* Names appearing in the type-specifier should be looked up
16746 in the scope of the class. */
16747 if (current_class_type)
16751 type = TREE_TYPE (type_decl);
16752 if (TREE_CODE (type) == TYPENAME_TYPE)
16754 type = resolve_typename_type (type,
16755 /*only_current_p=*/false);
16756 if (TREE_CODE (type) == TYPENAME_TYPE)
16758 cp_parser_abort_tentative_parse (parser);
16762 pushed_scope = push_scope (type);
16765 /* Inside the constructor parameter list, surrounding
16766 template-parameter-lists do not apply. */
16767 saved_num_template_parameter_lists
16768 = parser->num_template_parameter_lists;
16769 parser->num_template_parameter_lists = 0;
16771 /* Look for the type-specifier. */
16772 cp_parser_type_specifier (parser,
16773 CP_PARSER_FLAGS_NONE,
16774 /*decl_specs=*/NULL,
16775 /*is_declarator=*/true,
16776 /*declares_class_or_enum=*/NULL,
16777 /*is_cv_qualifier=*/NULL);
16779 parser->num_template_parameter_lists
16780 = saved_num_template_parameter_lists;
16782 /* Leave the scope of the class. */
16784 pop_scope (pushed_scope);
16786 constructor_p = !cp_parser_error_occurred (parser);
16790 constructor_p = false;
16791 /* We did not really want to consume any tokens. */
16792 cp_parser_abort_tentative_parse (parser);
16794 return constructor_p;
16797 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16798 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16799 they must be performed once we are in the scope of the function.
16801 Returns the function defined. */
16804 cp_parser_function_definition_from_specifiers_and_declarator
16805 (cp_parser* parser,
16806 cp_decl_specifier_seq *decl_specifiers,
16808 const cp_declarator *declarator)
16813 /* Begin the function-definition. */
16814 success_p = start_function (decl_specifiers, declarator, attributes);
16816 /* The things we're about to see are not directly qualified by any
16817 template headers we've seen thus far. */
16818 reset_specialization ();
16820 /* If there were names looked up in the decl-specifier-seq that we
16821 did not check, check them now. We must wait until we are in the
16822 scope of the function to perform the checks, since the function
16823 might be a friend. */
16824 perform_deferred_access_checks ();
16828 /* Skip the entire function. */
16829 cp_parser_skip_to_end_of_block_or_statement (parser);
16830 fn = error_mark_node;
16832 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16834 /* Seen already, skip it. An error message has already been output. */
16835 cp_parser_skip_to_end_of_block_or_statement (parser);
16836 fn = current_function_decl;
16837 current_function_decl = NULL_TREE;
16838 /* If this is a function from a class, pop the nested class. */
16839 if (current_class_name)
16840 pop_nested_class ();
16843 fn = cp_parser_function_definition_after_declarator (parser,
16844 /*inline_p=*/false);
16849 /* Parse the part of a function-definition that follows the
16850 declarator. INLINE_P is TRUE iff this function is an inline
16851 function defined with a class-specifier.
16853 Returns the function defined. */
16856 cp_parser_function_definition_after_declarator (cp_parser* parser,
16860 bool ctor_initializer_p = false;
16861 bool saved_in_unbraced_linkage_specification_p;
16862 bool saved_in_function_body;
16863 unsigned saved_num_template_parameter_lists;
16865 saved_in_function_body = parser->in_function_body;
16866 parser->in_function_body = true;
16867 /* If the next token is `return', then the code may be trying to
16868 make use of the "named return value" extension that G++ used to
16870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16872 /* Consume the `return' keyword. */
16873 cp_lexer_consume_token (parser->lexer);
16874 /* Look for the identifier that indicates what value is to be
16876 cp_parser_identifier (parser);
16877 /* Issue an error message. */
16878 error ("named return values are no longer supported");
16879 /* Skip tokens until we reach the start of the function body. */
16882 cp_token *token = cp_lexer_peek_token (parser->lexer);
16883 if (token->type == CPP_OPEN_BRACE
16884 || token->type == CPP_EOF
16885 || token->type == CPP_PRAGMA_EOL)
16887 cp_lexer_consume_token (parser->lexer);
16890 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16891 anything declared inside `f'. */
16892 saved_in_unbraced_linkage_specification_p
16893 = parser->in_unbraced_linkage_specification_p;
16894 parser->in_unbraced_linkage_specification_p = false;
16895 /* Inside the function, surrounding template-parameter-lists do not
16897 saved_num_template_parameter_lists
16898 = parser->num_template_parameter_lists;
16899 parser->num_template_parameter_lists = 0;
16900 /* If the next token is `try', then we are looking at a
16901 function-try-block. */
16902 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16903 ctor_initializer_p = cp_parser_function_try_block (parser);
16904 /* A function-try-block includes the function-body, so we only do
16905 this next part if we're not processing a function-try-block. */
16908 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16910 /* Finish the function. */
16911 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16912 (inline_p ? 2 : 0));
16913 /* Generate code for it, if necessary. */
16914 expand_or_defer_fn (fn);
16915 /* Restore the saved values. */
16916 parser->in_unbraced_linkage_specification_p
16917 = saved_in_unbraced_linkage_specification_p;
16918 parser->num_template_parameter_lists
16919 = saved_num_template_parameter_lists;
16920 parser->in_function_body = saved_in_function_body;
16925 /* Parse a template-declaration, assuming that the `export' (and
16926 `extern') keywords, if present, has already been scanned. MEMBER_P
16927 is as for cp_parser_template_declaration. */
16930 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16932 tree decl = NULL_TREE;
16933 VEC (deferred_access_check,gc) *checks;
16934 tree parameter_list;
16935 bool friend_p = false;
16936 bool need_lang_pop;
16938 /* Look for the `template' keyword. */
16939 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16943 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16945 if (at_class_scope_p () && current_function_decl)
16947 /* 14.5.2.2 [temp.mem]
16949 A local class shall not have member templates. */
16950 error ("invalid declaration of member template in local class");
16951 cp_parser_skip_to_end_of_block_or_statement (parser);
16956 A template ... shall not have C linkage. */
16957 if (current_lang_name == lang_name_c)
16959 error ("template with C linkage");
16960 /* Give it C++ linkage to avoid confusing other parts of the
16962 push_lang_context (lang_name_cplusplus);
16963 need_lang_pop = true;
16966 need_lang_pop = false;
16968 /* We cannot perform access checks on the template parameter
16969 declarations until we know what is being declared, just as we
16970 cannot check the decl-specifier list. */
16971 push_deferring_access_checks (dk_deferred);
16973 /* If the next token is `>', then we have an invalid
16974 specialization. Rather than complain about an invalid template
16975 parameter, issue an error message here. */
16976 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16978 cp_parser_error (parser, "invalid explicit specialization");
16979 begin_specialization ();
16980 parameter_list = NULL_TREE;
16983 /* Parse the template parameters. */
16984 parameter_list = cp_parser_template_parameter_list (parser);
16986 /* Get the deferred access checks from the parameter list. These
16987 will be checked once we know what is being declared, as for a
16988 member template the checks must be performed in the scope of the
16989 class containing the member. */
16990 checks = get_deferred_access_checks ();
16992 /* Look for the `>'. */
16993 cp_parser_skip_to_end_of_template_parameter_list (parser);
16994 /* We just processed one more parameter list. */
16995 ++parser->num_template_parameter_lists;
16996 /* If the next token is `template', there are more template
16998 if (cp_lexer_next_token_is_keyword (parser->lexer,
17000 cp_parser_template_declaration_after_export (parser, member_p);
17003 /* There are no access checks when parsing a template, as we do not
17004 know if a specialization will be a friend. */
17005 push_deferring_access_checks (dk_no_check);
17006 decl = cp_parser_single_declaration (parser,
17009 /*explicit_specialization_p=*/false,
17011 pop_deferring_access_checks ();
17013 /* If this is a member template declaration, let the front
17015 if (member_p && !friend_p && decl)
17017 if (TREE_CODE (decl) == TYPE_DECL)
17018 cp_parser_check_access_in_redeclaration (decl);
17020 decl = finish_member_template_decl (decl);
17022 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17023 make_friend_class (current_class_type, TREE_TYPE (decl),
17024 /*complain=*/true);
17026 /* We are done with the current parameter list. */
17027 --parser->num_template_parameter_lists;
17029 pop_deferring_access_checks ();
17032 finish_template_decl (parameter_list);
17034 /* Register member declarations. */
17035 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17036 finish_member_declaration (decl);
17037 /* For the erroneous case of a template with C linkage, we pushed an
17038 implicit C++ linkage scope; exit that scope now. */
17040 pop_lang_context ();
17041 /* If DECL is a function template, we must return to parse it later.
17042 (Even though there is no definition, there might be default
17043 arguments that need handling.) */
17044 if (member_p && decl
17045 && (TREE_CODE (decl) == FUNCTION_DECL
17046 || DECL_FUNCTION_TEMPLATE_P (decl)))
17047 TREE_VALUE (parser->unparsed_functions_queues)
17048 = tree_cons (NULL_TREE, decl,
17049 TREE_VALUE (parser->unparsed_functions_queues));
17052 /* Perform the deferred access checks from a template-parameter-list.
17053 CHECKS is a TREE_LIST of access checks, as returned by
17054 get_deferred_access_checks. */
17057 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17059 ++processing_template_parmlist;
17060 perform_access_checks (checks);
17061 --processing_template_parmlist;
17064 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17065 `function-definition' sequence. MEMBER_P is true, this declaration
17066 appears in a class scope.
17068 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17069 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17072 cp_parser_single_declaration (cp_parser* parser,
17073 VEC (deferred_access_check,gc)* checks,
17075 bool explicit_specialization_p,
17078 int declares_class_or_enum;
17079 tree decl = NULL_TREE;
17080 cp_decl_specifier_seq decl_specifiers;
17081 bool function_definition_p = false;
17083 /* This function is only used when processing a template
17085 gcc_assert (innermost_scope_kind () == sk_template_parms
17086 || innermost_scope_kind () == sk_template_spec);
17088 /* Defer access checks until we know what is being declared. */
17089 push_deferring_access_checks (dk_deferred);
17091 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17093 cp_parser_decl_specifier_seq (parser,
17094 CP_PARSER_FLAGS_OPTIONAL,
17096 &declares_class_or_enum);
17098 *friend_p = cp_parser_friend_p (&decl_specifiers);
17100 /* There are no template typedefs. */
17101 if (decl_specifiers.specs[(int) ds_typedef])
17103 error ("template declaration of %qs", "typedef");
17104 decl = error_mark_node;
17107 /* Gather up the access checks that occurred the
17108 decl-specifier-seq. */
17109 stop_deferring_access_checks ();
17111 /* Check for the declaration of a template class. */
17112 if (declares_class_or_enum)
17114 if (cp_parser_declares_only_class_p (parser))
17116 decl = shadow_tag (&decl_specifiers);
17121 friend template <typename T> struct A<T>::B;
17124 A<T>::B will be represented by a TYPENAME_TYPE, and
17125 therefore not recognized by shadow_tag. */
17126 if (friend_p && *friend_p
17128 && decl_specifiers.type
17129 && TYPE_P (decl_specifiers.type))
17130 decl = decl_specifiers.type;
17132 if (decl && decl != error_mark_node)
17133 decl = TYPE_NAME (decl);
17135 decl = error_mark_node;
17137 /* Perform access checks for template parameters. */
17138 cp_parser_perform_template_parameter_access_checks (checks);
17141 /* If it's not a template class, try for a template function. If
17142 the next token is a `;', then this declaration does not declare
17143 anything. But, if there were errors in the decl-specifiers, then
17144 the error might well have come from an attempted class-specifier.
17145 In that case, there's no need to warn about a missing declarator. */
17147 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17148 || decl_specifiers.type != error_mark_node))
17150 decl = cp_parser_init_declarator (parser,
17153 /*function_definition_allowed_p=*/true,
17155 declares_class_or_enum,
17156 &function_definition_p);
17158 /* 7.1.1-1 [dcl.stc]
17160 A storage-class-specifier shall not be specified in an explicit
17161 specialization... */
17163 && explicit_specialization_p
17164 && decl_specifiers.storage_class != sc_none)
17166 error ("explicit template specialization cannot have a storage class");
17167 decl = error_mark_node;
17171 pop_deferring_access_checks ();
17173 /* Clear any current qualification; whatever comes next is the start
17174 of something new. */
17175 parser->scope = NULL_TREE;
17176 parser->qualifying_scope = NULL_TREE;
17177 parser->object_scope = NULL_TREE;
17178 /* Look for a trailing `;' after the declaration. */
17179 if (!function_definition_p
17180 && (decl == error_mark_node
17181 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17182 cp_parser_skip_to_end_of_block_or_statement (parser);
17187 /* Parse a cast-expression that is not the operand of a unary "&". */
17190 cp_parser_simple_cast_expression (cp_parser *parser)
17192 return cp_parser_cast_expression (parser, /*address_p=*/false,
17196 /* Parse a functional cast to TYPE. Returns an expression
17197 representing the cast. */
17200 cp_parser_functional_cast (cp_parser* parser, tree type)
17202 tree expression_list;
17206 = cp_parser_parenthesized_expression_list (parser, false,
17208 /*allow_expansion_p=*/true,
17209 /*non_constant_p=*/NULL);
17211 cast = build_functional_cast (type, expression_list,
17212 tf_warning_or_error);
17213 /* [expr.const]/1: In an integral constant expression "only type
17214 conversions to integral or enumeration type can be used". */
17215 if (TREE_CODE (type) == TYPE_DECL)
17216 type = TREE_TYPE (type);
17217 if (cast != error_mark_node
17218 && !cast_valid_in_integral_constant_expression_p (type)
17219 && (cp_parser_non_integral_constant_expression
17220 (parser, "a call to a constructor")))
17221 return error_mark_node;
17225 /* Save the tokens that make up the body of a member function defined
17226 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17227 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17228 specifiers applied to the declaration. Returns the FUNCTION_DECL
17229 for the member function. */
17232 cp_parser_save_member_function_body (cp_parser* parser,
17233 cp_decl_specifier_seq *decl_specifiers,
17234 cp_declarator *declarator,
17241 /* Create the function-declaration. */
17242 fn = start_method (decl_specifiers, declarator, attributes);
17243 /* If something went badly wrong, bail out now. */
17244 if (fn == error_mark_node)
17246 /* If there's a function-body, skip it. */
17247 if (cp_parser_token_starts_function_definition_p
17248 (cp_lexer_peek_token (parser->lexer)))
17249 cp_parser_skip_to_end_of_block_or_statement (parser);
17250 return error_mark_node;
17253 /* Remember it, if there default args to post process. */
17254 cp_parser_save_default_args (parser, fn);
17256 /* Save away the tokens that make up the body of the
17258 first = parser->lexer->next_token;
17259 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17260 /* Handle function try blocks. */
17261 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17262 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17263 last = parser->lexer->next_token;
17265 /* Save away the inline definition; we will process it when the
17266 class is complete. */
17267 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17268 DECL_PENDING_INLINE_P (fn) = 1;
17270 /* We need to know that this was defined in the class, so that
17271 friend templates are handled correctly. */
17272 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17274 /* We're done with the inline definition. */
17275 finish_method (fn);
17277 /* Add FN to the queue of functions to be parsed later. */
17278 TREE_VALUE (parser->unparsed_functions_queues)
17279 = tree_cons (NULL_TREE, fn,
17280 TREE_VALUE (parser->unparsed_functions_queues));
17285 /* Parse a template-argument-list, as well as the trailing ">" (but
17286 not the opening ">"). See cp_parser_template_argument_list for the
17290 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17294 tree saved_qualifying_scope;
17295 tree saved_object_scope;
17296 bool saved_greater_than_is_operator_p;
17297 bool saved_skip_evaluation;
17301 When parsing a template-id, the first non-nested `>' is taken as
17302 the end of the template-argument-list rather than a greater-than
17304 saved_greater_than_is_operator_p
17305 = parser->greater_than_is_operator_p;
17306 parser->greater_than_is_operator_p = false;
17307 /* Parsing the argument list may modify SCOPE, so we save it
17309 saved_scope = parser->scope;
17310 saved_qualifying_scope = parser->qualifying_scope;
17311 saved_object_scope = parser->object_scope;
17312 /* We need to evaluate the template arguments, even though this
17313 template-id may be nested within a "sizeof". */
17314 saved_skip_evaluation = skip_evaluation;
17315 skip_evaluation = false;
17316 /* Parse the template-argument-list itself. */
17317 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17318 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17319 arguments = NULL_TREE;
17321 arguments = cp_parser_template_argument_list (parser);
17322 /* Look for the `>' that ends the template-argument-list. If we find
17323 a '>>' instead, it's probably just a typo. */
17324 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17326 if (cxx_dialect != cxx98)
17328 /* In C++0x, a `>>' in a template argument list or cast
17329 expression is considered to be two separate `>'
17330 tokens. So, change the current token to a `>', but don't
17331 consume it: it will be consumed later when the outer
17332 template argument list (or cast expression) is parsed.
17333 Note that this replacement of `>' for `>>' is necessary
17334 even if we are parsing tentatively: in the tentative
17335 case, after calling
17336 cp_parser_enclosed_template_argument_list we will always
17337 throw away all of the template arguments and the first
17338 closing `>', either because the template argument list
17339 was erroneous or because we are replacing those tokens
17340 with a CPP_TEMPLATE_ID token. The second `>' (which will
17341 not have been thrown away) is needed either to close an
17342 outer template argument list or to complete a new-style
17344 cp_token *token = cp_lexer_peek_token (parser->lexer);
17345 token->type = CPP_GREATER;
17347 else if (!saved_greater_than_is_operator_p)
17349 /* If we're in a nested template argument list, the '>>' has
17350 to be a typo for '> >'. We emit the error message, but we
17351 continue parsing and we push a '>' as next token, so that
17352 the argument list will be parsed correctly. Note that the
17353 global source location is still on the token before the
17354 '>>', so we need to say explicitly where we want it. */
17355 cp_token *token = cp_lexer_peek_token (parser->lexer);
17356 error ("%H%<>>%> should be %<> >%> "
17357 "within a nested template argument list",
17360 token->type = CPP_GREATER;
17364 /* If this is not a nested template argument list, the '>>'
17365 is a typo for '>'. Emit an error message and continue.
17366 Same deal about the token location, but here we can get it
17367 right by consuming the '>>' before issuing the diagnostic. */
17368 cp_lexer_consume_token (parser->lexer);
17369 error ("spurious %<>>%>, use %<>%> to terminate "
17370 "a template argument list");
17374 cp_parser_skip_to_end_of_template_parameter_list (parser);
17375 /* The `>' token might be a greater-than operator again now. */
17376 parser->greater_than_is_operator_p
17377 = saved_greater_than_is_operator_p;
17378 /* Restore the SAVED_SCOPE. */
17379 parser->scope = saved_scope;
17380 parser->qualifying_scope = saved_qualifying_scope;
17381 parser->object_scope = saved_object_scope;
17382 skip_evaluation = saved_skip_evaluation;
17387 /* MEMBER_FUNCTION is a member function, or a friend. If default
17388 arguments, or the body of the function have not yet been parsed,
17392 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17394 /* If this member is a template, get the underlying
17396 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17397 member_function = DECL_TEMPLATE_RESULT (member_function);
17399 /* There should not be any class definitions in progress at this
17400 point; the bodies of members are only parsed outside of all class
17402 gcc_assert (parser->num_classes_being_defined == 0);
17403 /* While we're parsing the member functions we might encounter more
17404 classes. We want to handle them right away, but we don't want
17405 them getting mixed up with functions that are currently in the
17407 parser->unparsed_functions_queues
17408 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17410 /* Make sure that any template parameters are in scope. */
17411 maybe_begin_member_template_processing (member_function);
17413 /* If the body of the function has not yet been parsed, parse it
17415 if (DECL_PENDING_INLINE_P (member_function))
17417 tree function_scope;
17418 cp_token_cache *tokens;
17420 /* The function is no longer pending; we are processing it. */
17421 tokens = DECL_PENDING_INLINE_INFO (member_function);
17422 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17423 DECL_PENDING_INLINE_P (member_function) = 0;
17425 /* If this is a local class, enter the scope of the containing
17427 function_scope = current_function_decl;
17428 if (function_scope)
17429 push_function_context_to (function_scope);
17432 /* Push the body of the function onto the lexer stack. */
17433 cp_parser_push_lexer_for_tokens (parser, tokens);
17435 /* Let the front end know that we going to be defining this
17437 start_preparsed_function (member_function, NULL_TREE,
17438 SF_PRE_PARSED | SF_INCLASS_INLINE);
17440 /* Don't do access checking if it is a templated function. */
17441 if (processing_template_decl)
17442 push_deferring_access_checks (dk_no_check);
17444 /* Now, parse the body of the function. */
17445 cp_parser_function_definition_after_declarator (parser,
17446 /*inline_p=*/true);
17448 if (processing_template_decl)
17449 pop_deferring_access_checks ();
17451 /* Leave the scope of the containing function. */
17452 if (function_scope)
17453 pop_function_context_from (function_scope);
17454 cp_parser_pop_lexer (parser);
17457 /* Remove any template parameters from the symbol table. */
17458 maybe_end_member_template_processing ();
17460 /* Restore the queue. */
17461 parser->unparsed_functions_queues
17462 = TREE_CHAIN (parser->unparsed_functions_queues);
17465 /* If DECL contains any default args, remember it on the unparsed
17466 functions queue. */
17469 cp_parser_save_default_args (cp_parser* parser, tree decl)
17473 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17475 probe = TREE_CHAIN (probe))
17476 if (TREE_PURPOSE (probe))
17478 TREE_PURPOSE (parser->unparsed_functions_queues)
17479 = tree_cons (current_class_type, decl,
17480 TREE_PURPOSE (parser->unparsed_functions_queues));
17485 /* FN is a FUNCTION_DECL which may contains a parameter with an
17486 unparsed DEFAULT_ARG. Parse the default args now. This function
17487 assumes that the current scope is the scope in which the default
17488 argument should be processed. */
17491 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17493 bool saved_local_variables_forbidden_p;
17496 /* While we're parsing the default args, we might (due to the
17497 statement expression extension) encounter more classes. We want
17498 to handle them right away, but we don't want them getting mixed
17499 up with default args that are currently in the queue. */
17500 parser->unparsed_functions_queues
17501 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17503 /* Local variable names (and the `this' keyword) may not appear
17504 in a default argument. */
17505 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17506 parser->local_variables_forbidden_p = true;
17508 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17510 parm = TREE_CHAIN (parm))
17512 cp_token_cache *tokens;
17513 tree default_arg = TREE_PURPOSE (parm);
17515 VEC(tree,gc) *insts;
17522 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17523 /* This can happen for a friend declaration for a function
17524 already declared with default arguments. */
17527 /* Push the saved tokens for the default argument onto the parser's
17529 tokens = DEFARG_TOKENS (default_arg);
17530 cp_parser_push_lexer_for_tokens (parser, tokens);
17532 /* Parse the assignment-expression. */
17533 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17535 if (!processing_template_decl)
17536 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17538 TREE_PURPOSE (parm) = parsed_arg;
17540 /* Update any instantiations we've already created. */
17541 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17542 VEC_iterate (tree, insts, ix, copy); ix++)
17543 TREE_PURPOSE (copy) = parsed_arg;
17545 /* If the token stream has not been completely used up, then
17546 there was extra junk after the end of the default
17548 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17549 cp_parser_error (parser, "expected %<,%>");
17551 /* Revert to the main lexer. */
17552 cp_parser_pop_lexer (parser);
17555 /* Make sure no default arg is missing. */
17556 check_default_args (fn);
17558 /* Restore the state of local_variables_forbidden_p. */
17559 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17561 /* Restore the queue. */
17562 parser->unparsed_functions_queues
17563 = TREE_CHAIN (parser->unparsed_functions_queues);
17566 /* Parse the operand of `sizeof' (or a similar operator). Returns
17567 either a TYPE or an expression, depending on the form of the
17568 input. The KEYWORD indicates which kind of expression we have
17572 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17574 static const char *format;
17575 tree expr = NULL_TREE;
17576 const char *saved_message;
17578 bool saved_integral_constant_expression_p;
17579 bool saved_non_integral_constant_expression_p;
17580 bool pack_expansion_p = false;
17582 /* Initialize FORMAT the first time we get here. */
17584 format = "types may not be defined in '%s' expressions";
17586 /* Types cannot be defined in a `sizeof' expression. Save away the
17588 saved_message = parser->type_definition_forbidden_message;
17589 /* And create the new one. */
17590 parser->type_definition_forbidden_message = tmp
17591 = XNEWVEC (char, strlen (format)
17592 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17594 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17596 /* The restrictions on constant-expressions do not apply inside
17597 sizeof expressions. */
17598 saved_integral_constant_expression_p
17599 = parser->integral_constant_expression_p;
17600 saved_non_integral_constant_expression_p
17601 = parser->non_integral_constant_expression_p;
17602 parser->integral_constant_expression_p = false;
17604 /* If it's a `...', then we are computing the length of a parameter
17606 if (keyword == RID_SIZEOF
17607 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17609 /* Consume the `...'. */
17610 cp_lexer_consume_token (parser->lexer);
17611 maybe_warn_variadic_templates ();
17613 /* Note that this is an expansion. */
17614 pack_expansion_p = true;
17617 /* Do not actually evaluate the expression. */
17619 /* If it's a `(', then we might be looking at the type-id
17621 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17624 bool saved_in_type_id_in_expr_p;
17626 /* We can't be sure yet whether we're looking at a type-id or an
17628 cp_parser_parse_tentatively (parser);
17629 /* Consume the `('. */
17630 cp_lexer_consume_token (parser->lexer);
17631 /* Parse the type-id. */
17632 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17633 parser->in_type_id_in_expr_p = true;
17634 type = cp_parser_type_id (parser);
17635 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17636 /* Now, look for the trailing `)'. */
17637 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17638 /* If all went well, then we're done. */
17639 if (cp_parser_parse_definitely (parser))
17641 cp_decl_specifier_seq decl_specs;
17643 /* Build a trivial decl-specifier-seq. */
17644 clear_decl_specs (&decl_specs);
17645 decl_specs.type = type;
17647 /* Call grokdeclarator to figure out what type this is. */
17648 expr = grokdeclarator (NULL,
17652 /*attrlist=*/NULL);
17656 /* If the type-id production did not work out, then we must be
17657 looking at the unary-expression production. */
17659 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17662 if (pack_expansion_p)
17663 /* Build a pack expansion. */
17664 expr = make_pack_expansion (expr);
17666 /* Go back to evaluating expressions. */
17669 /* Free the message we created. */
17671 /* And restore the old one. */
17672 parser->type_definition_forbidden_message = saved_message;
17673 parser->integral_constant_expression_p
17674 = saved_integral_constant_expression_p;
17675 parser->non_integral_constant_expression_p
17676 = saved_non_integral_constant_expression_p;
17681 /* If the current declaration has no declarator, return true. */
17684 cp_parser_declares_only_class_p (cp_parser *parser)
17686 /* If the next token is a `;' or a `,' then there is no
17688 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17689 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17692 /* Update the DECL_SPECS to reflect the storage class indicated by
17696 cp_parser_set_storage_class (cp_parser *parser,
17697 cp_decl_specifier_seq *decl_specs,
17700 cp_storage_class storage_class;
17702 if (parser->in_unbraced_linkage_specification_p)
17704 error ("invalid use of %qD in linkage specification",
17705 ridpointers[keyword]);
17708 else if (decl_specs->storage_class != sc_none)
17710 decl_specs->conflicting_specifiers_p = true;
17714 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17715 && decl_specs->specs[(int) ds_thread])
17717 error ("%<__thread%> before %qD", ridpointers[keyword]);
17718 decl_specs->specs[(int) ds_thread] = 0;
17724 storage_class = sc_auto;
17727 storage_class = sc_register;
17730 storage_class = sc_static;
17733 storage_class = sc_extern;
17736 storage_class = sc_mutable;
17739 gcc_unreachable ();
17741 decl_specs->storage_class = storage_class;
17743 /* A storage class specifier cannot be applied alongside a typedef
17744 specifier. If there is a typedef specifier present then set
17745 conflicting_specifiers_p which will trigger an error later
17746 on in grokdeclarator. */
17747 if (decl_specs->specs[(int)ds_typedef])
17748 decl_specs->conflicting_specifiers_p = true;
17751 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17752 is true, the type is a user-defined type; otherwise it is a
17753 built-in type specified by a keyword. */
17756 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17758 bool user_defined_p)
17760 decl_specs->any_specifiers_p = true;
17762 /* If the user tries to redeclare bool or wchar_t (with, for
17763 example, in "typedef int wchar_t;") we remember that this is what
17764 happened. In system headers, we ignore these declarations so
17765 that G++ can work with system headers that are not C++-safe. */
17766 if (decl_specs->specs[(int) ds_typedef]
17768 && (type_spec == boolean_type_node
17769 || type_spec == wchar_type_node)
17770 && (decl_specs->type
17771 || decl_specs->specs[(int) ds_long]
17772 || decl_specs->specs[(int) ds_short]
17773 || decl_specs->specs[(int) ds_unsigned]
17774 || decl_specs->specs[(int) ds_signed]))
17776 decl_specs->redefined_builtin_type = type_spec;
17777 if (!decl_specs->type)
17779 decl_specs->type = type_spec;
17780 decl_specs->user_defined_type_p = false;
17783 else if (decl_specs->type)
17784 decl_specs->multiple_types_p = true;
17787 decl_specs->type = type_spec;
17788 decl_specs->user_defined_type_p = user_defined_p;
17789 decl_specs->redefined_builtin_type = NULL_TREE;
17793 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17794 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17797 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17799 return decl_specifiers->specs[(int) ds_friend] != 0;
17802 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17803 issue an error message indicating that TOKEN_DESC was expected.
17805 Returns the token consumed, if the token had the appropriate type.
17806 Otherwise, returns NULL. */
17809 cp_parser_require (cp_parser* parser,
17810 enum cpp_ttype type,
17811 const char* token_desc)
17813 if (cp_lexer_next_token_is (parser->lexer, type))
17814 return cp_lexer_consume_token (parser->lexer);
17817 /* Output the MESSAGE -- unless we're parsing tentatively. */
17818 if (!cp_parser_simulate_error (parser))
17820 char *message = concat ("expected ", token_desc, NULL);
17821 cp_parser_error (parser, message);
17828 /* An error message is produced if the next token is not '>'.
17829 All further tokens are skipped until the desired token is
17830 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17833 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17835 /* Current level of '< ... >'. */
17836 unsigned level = 0;
17837 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17838 unsigned nesting_depth = 0;
17840 /* Are we ready, yet? If not, issue error message. */
17841 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17844 /* Skip tokens until the desired token is found. */
17847 /* Peek at the next token. */
17848 switch (cp_lexer_peek_token (parser->lexer)->type)
17851 if (!nesting_depth)
17856 if (cxx_dialect == cxx98)
17857 /* C++0x views the `>>' operator as two `>' tokens, but
17860 else if (!nesting_depth && level-- == 0)
17862 /* We've hit a `>>' where the first `>' closes the
17863 template argument list, and the second `>' is
17864 spurious. Just consume the `>>' and stop; we've
17865 already produced at least one error. */
17866 cp_lexer_consume_token (parser->lexer);
17869 /* Fall through for C++0x, so we handle the second `>' in
17873 if (!nesting_depth && level-- == 0)
17875 /* We've reached the token we want, consume it and stop. */
17876 cp_lexer_consume_token (parser->lexer);
17881 case CPP_OPEN_PAREN:
17882 case CPP_OPEN_SQUARE:
17886 case CPP_CLOSE_PAREN:
17887 case CPP_CLOSE_SQUARE:
17888 if (nesting_depth-- == 0)
17893 case CPP_PRAGMA_EOL:
17894 case CPP_SEMICOLON:
17895 case CPP_OPEN_BRACE:
17896 case CPP_CLOSE_BRACE:
17897 /* The '>' was probably forgotten, don't look further. */
17904 /* Consume this token. */
17905 cp_lexer_consume_token (parser->lexer);
17909 /* If the next token is the indicated keyword, consume it. Otherwise,
17910 issue an error message indicating that TOKEN_DESC was expected.
17912 Returns the token consumed, if the token had the appropriate type.
17913 Otherwise, returns NULL. */
17916 cp_parser_require_keyword (cp_parser* parser,
17918 const char* token_desc)
17920 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17922 if (token && token->keyword != keyword)
17924 dyn_string_t error_msg;
17926 /* Format the error message. */
17927 error_msg = dyn_string_new (0);
17928 dyn_string_append_cstr (error_msg, "expected ");
17929 dyn_string_append_cstr (error_msg, token_desc);
17930 cp_parser_error (parser, error_msg->s);
17931 dyn_string_delete (error_msg);
17938 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17939 function-definition. */
17942 cp_parser_token_starts_function_definition_p (cp_token* token)
17944 return (/* An ordinary function-body begins with an `{'. */
17945 token->type == CPP_OPEN_BRACE
17946 /* A ctor-initializer begins with a `:'. */
17947 || token->type == CPP_COLON
17948 /* A function-try-block begins with `try'. */
17949 || token->keyword == RID_TRY
17950 /* The named return value extension begins with `return'. */
17951 || token->keyword == RID_RETURN);
17954 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17958 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17962 token = cp_lexer_peek_token (parser->lexer);
17963 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17966 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17967 C++0x) ending a template-argument. */
17970 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17974 token = cp_lexer_peek_token (parser->lexer);
17975 return (token->type == CPP_COMMA
17976 || token->type == CPP_GREATER
17977 || token->type == CPP_ELLIPSIS
17978 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17981 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17982 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17985 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17990 token = cp_lexer_peek_nth_token (parser->lexer, n);
17991 if (token->type == CPP_LESS)
17993 /* Check for the sequence `<::' in the original code. It would be lexed as
17994 `[:', where `[' is a digraph, and there is no whitespace before
17996 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17999 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18000 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18006 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18007 or none_type otherwise. */
18009 static enum tag_types
18010 cp_parser_token_is_class_key (cp_token* token)
18012 switch (token->keyword)
18017 return record_type;
18026 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18029 cp_parser_check_class_key (enum tag_types class_key, tree type)
18031 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18032 pedwarn ("%qs tag used in naming %q#T",
18033 class_key == union_type ? "union"
18034 : class_key == record_type ? "struct" : "class",
18038 /* Issue an error message if DECL is redeclared with different
18039 access than its original declaration [class.access.spec/3].
18040 This applies to nested classes and nested class templates.
18044 cp_parser_check_access_in_redeclaration (tree decl)
18046 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18049 if ((TREE_PRIVATE (decl)
18050 != (current_access_specifier == access_private_node))
18051 || (TREE_PROTECTED (decl)
18052 != (current_access_specifier == access_protected_node)))
18053 error ("%qD redeclared with different access", decl);
18056 /* Look for the `template' keyword, as a syntactic disambiguator.
18057 Return TRUE iff it is present, in which case it will be
18061 cp_parser_optional_template_keyword (cp_parser *parser)
18063 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18065 /* The `template' keyword can only be used within templates;
18066 outside templates the parser can always figure out what is a
18067 template and what is not. */
18068 if (!processing_template_decl)
18070 error ("%<template%> (as a disambiguator) is only allowed "
18071 "within templates");
18072 /* If this part of the token stream is rescanned, the same
18073 error message would be generated. So, we purge the token
18074 from the stream. */
18075 cp_lexer_purge_token (parser->lexer);
18080 /* Consume the `template' keyword. */
18081 cp_lexer_consume_token (parser->lexer);
18089 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18090 set PARSER->SCOPE, and perform other related actions. */
18093 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18096 struct tree_check *check_value;
18097 deferred_access_check *chk;
18098 VEC (deferred_access_check,gc) *checks;
18100 /* Get the stored value. */
18101 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18102 /* Perform any access checks that were deferred. */
18103 checks = check_value->checks;
18107 VEC_iterate (deferred_access_check, checks, i, chk) ;
18110 perform_or_defer_access_check (chk->binfo,
18115 /* Set the scope from the stored value. */
18116 parser->scope = check_value->value;
18117 parser->qualifying_scope = check_value->qualifying_scope;
18118 parser->object_scope = NULL_TREE;
18121 /* Consume tokens up through a non-nested END token. */
18124 cp_parser_cache_group (cp_parser *parser,
18125 enum cpp_ttype end,
18132 /* Abort a parenthesized expression if we encounter a brace. */
18133 if ((end == CPP_CLOSE_PAREN || depth == 0)
18134 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18136 /* If we've reached the end of the file, stop. */
18137 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18138 || (end != CPP_PRAGMA_EOL
18139 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18141 /* Consume the next token. */
18142 token = cp_lexer_consume_token (parser->lexer);
18143 /* See if it starts a new group. */
18144 if (token->type == CPP_OPEN_BRACE)
18146 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18150 else if (token->type == CPP_OPEN_PAREN)
18151 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18152 else if (token->type == CPP_PRAGMA)
18153 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18154 else if (token->type == end)
18159 /* Begin parsing tentatively. We always save tokens while parsing
18160 tentatively so that if the tentative parsing fails we can restore the
18164 cp_parser_parse_tentatively (cp_parser* parser)
18166 /* Enter a new parsing context. */
18167 parser->context = cp_parser_context_new (parser->context);
18168 /* Begin saving tokens. */
18169 cp_lexer_save_tokens (parser->lexer);
18170 /* In order to avoid repetitive access control error messages,
18171 access checks are queued up until we are no longer parsing
18173 push_deferring_access_checks (dk_deferred);
18176 /* Commit to the currently active tentative parse. */
18179 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18181 cp_parser_context *context;
18184 /* Mark all of the levels as committed. */
18185 lexer = parser->lexer;
18186 for (context = parser->context; context->next; context = context->next)
18188 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18190 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18191 while (!cp_lexer_saving_tokens (lexer))
18192 lexer = lexer->next;
18193 cp_lexer_commit_tokens (lexer);
18197 /* Abort the currently active tentative parse. All consumed tokens
18198 will be rolled back, and no diagnostics will be issued. */
18201 cp_parser_abort_tentative_parse (cp_parser* parser)
18203 cp_parser_simulate_error (parser);
18204 /* Now, pretend that we want to see if the construct was
18205 successfully parsed. */
18206 cp_parser_parse_definitely (parser);
18209 /* Stop parsing tentatively. If a parse error has occurred, restore the
18210 token stream. Otherwise, commit to the tokens we have consumed.
18211 Returns true if no error occurred; false otherwise. */
18214 cp_parser_parse_definitely (cp_parser* parser)
18216 bool error_occurred;
18217 cp_parser_context *context;
18219 /* Remember whether or not an error occurred, since we are about to
18220 destroy that information. */
18221 error_occurred = cp_parser_error_occurred (parser);
18222 /* Remove the topmost context from the stack. */
18223 context = parser->context;
18224 parser->context = context->next;
18225 /* If no parse errors occurred, commit to the tentative parse. */
18226 if (!error_occurred)
18228 /* Commit to the tokens read tentatively, unless that was
18230 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18231 cp_lexer_commit_tokens (parser->lexer);
18233 pop_to_parent_deferring_access_checks ();
18235 /* Otherwise, if errors occurred, roll back our state so that things
18236 are just as they were before we began the tentative parse. */
18239 cp_lexer_rollback_tokens (parser->lexer);
18240 pop_deferring_access_checks ();
18242 /* Add the context to the front of the free list. */
18243 context->next = cp_parser_context_free_list;
18244 cp_parser_context_free_list = context;
18246 return !error_occurred;
18249 /* Returns true if we are parsing tentatively and are not committed to
18250 this tentative parse. */
18253 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18255 return (cp_parser_parsing_tentatively (parser)
18256 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18259 /* Returns nonzero iff an error has occurred during the most recent
18260 tentative parse. */
18263 cp_parser_error_occurred (cp_parser* parser)
18265 return (cp_parser_parsing_tentatively (parser)
18266 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18269 /* Returns nonzero if GNU extensions are allowed. */
18272 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18274 return parser->allow_gnu_extensions_p;
18277 /* Objective-C++ Productions */
18280 /* Parse an Objective-C expression, which feeds into a primary-expression
18284 objc-message-expression
18285 objc-string-literal
18286 objc-encode-expression
18287 objc-protocol-expression
18288 objc-selector-expression
18290 Returns a tree representation of the expression. */
18293 cp_parser_objc_expression (cp_parser* parser)
18295 /* Try to figure out what kind of declaration is present. */
18296 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18300 case CPP_OPEN_SQUARE:
18301 return cp_parser_objc_message_expression (parser);
18303 case CPP_OBJC_STRING:
18304 kwd = cp_lexer_consume_token (parser->lexer);
18305 return objc_build_string_object (kwd->u.value);
18308 switch (kwd->keyword)
18310 case RID_AT_ENCODE:
18311 return cp_parser_objc_encode_expression (parser);
18313 case RID_AT_PROTOCOL:
18314 return cp_parser_objc_protocol_expression (parser);
18316 case RID_AT_SELECTOR:
18317 return cp_parser_objc_selector_expression (parser);
18323 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18324 cp_parser_skip_to_end_of_block_or_statement (parser);
18327 return error_mark_node;
18330 /* Parse an Objective-C message expression.
18332 objc-message-expression:
18333 [ objc-message-receiver objc-message-args ]
18335 Returns a representation of an Objective-C message. */
18338 cp_parser_objc_message_expression (cp_parser* parser)
18340 tree receiver, messageargs;
18342 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18343 receiver = cp_parser_objc_message_receiver (parser);
18344 messageargs = cp_parser_objc_message_args (parser);
18345 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18347 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18350 /* Parse an objc-message-receiver.
18352 objc-message-receiver:
18354 simple-type-specifier
18356 Returns a representation of the type or expression. */
18359 cp_parser_objc_message_receiver (cp_parser* parser)
18363 /* An Objective-C message receiver may be either (1) a type
18364 or (2) an expression. */
18365 cp_parser_parse_tentatively (parser);
18366 rcv = cp_parser_expression (parser, false);
18368 if (cp_parser_parse_definitely (parser))
18371 rcv = cp_parser_simple_type_specifier (parser,
18372 /*decl_specs=*/NULL,
18373 CP_PARSER_FLAGS_NONE);
18375 return objc_get_class_reference (rcv);
18378 /* Parse the arguments and selectors comprising an Objective-C message.
18383 objc-selector-args , objc-comma-args
18385 objc-selector-args:
18386 objc-selector [opt] : assignment-expression
18387 objc-selector-args objc-selector [opt] : assignment-expression
18390 assignment-expression
18391 objc-comma-args , assignment-expression
18393 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18394 selector arguments and TREE_VALUE containing a list of comma
18398 cp_parser_objc_message_args (cp_parser* parser)
18400 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18401 bool maybe_unary_selector_p = true;
18402 cp_token *token = cp_lexer_peek_token (parser->lexer);
18404 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18406 tree selector = NULL_TREE, arg;
18408 if (token->type != CPP_COLON)
18409 selector = cp_parser_objc_selector (parser);
18411 /* Detect if we have a unary selector. */
18412 if (maybe_unary_selector_p
18413 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18414 return build_tree_list (selector, NULL_TREE);
18416 maybe_unary_selector_p = false;
18417 cp_parser_require (parser, CPP_COLON, "`:'");
18418 arg = cp_parser_assignment_expression (parser, false);
18421 = chainon (sel_args,
18422 build_tree_list (selector, arg));
18424 token = cp_lexer_peek_token (parser->lexer);
18427 /* Handle non-selector arguments, if any. */
18428 while (token->type == CPP_COMMA)
18432 cp_lexer_consume_token (parser->lexer);
18433 arg = cp_parser_assignment_expression (parser, false);
18436 = chainon (addl_args,
18437 build_tree_list (NULL_TREE, arg));
18439 token = cp_lexer_peek_token (parser->lexer);
18442 return build_tree_list (sel_args, addl_args);
18445 /* Parse an Objective-C encode expression.
18447 objc-encode-expression:
18448 @encode objc-typename
18450 Returns an encoded representation of the type argument. */
18453 cp_parser_objc_encode_expression (cp_parser* parser)
18457 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18458 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18459 type = complete_type (cp_parser_type_id (parser));
18460 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18464 error ("%<@encode%> must specify a type as an argument");
18465 return error_mark_node;
18468 return objc_build_encode_expr (type);
18471 /* Parse an Objective-C @defs expression. */
18474 cp_parser_objc_defs_expression (cp_parser *parser)
18478 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18479 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18480 name = cp_parser_identifier (parser);
18481 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18483 return objc_get_class_ivars (name);
18486 /* Parse an Objective-C protocol expression.
18488 objc-protocol-expression:
18489 @protocol ( identifier )
18491 Returns a representation of the protocol expression. */
18494 cp_parser_objc_protocol_expression (cp_parser* parser)
18498 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18499 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18500 proto = cp_parser_identifier (parser);
18501 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18503 return objc_build_protocol_expr (proto);
18506 /* Parse an Objective-C selector expression.
18508 objc-selector-expression:
18509 @selector ( objc-method-signature )
18511 objc-method-signature:
18517 objc-selector-seq objc-selector :
18519 Returns a representation of the method selector. */
18522 cp_parser_objc_selector_expression (cp_parser* parser)
18524 tree sel_seq = NULL_TREE;
18525 bool maybe_unary_selector_p = true;
18528 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18529 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18530 token = cp_lexer_peek_token (parser->lexer);
18532 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18533 || token->type == CPP_SCOPE)
18535 tree selector = NULL_TREE;
18537 if (token->type != CPP_COLON
18538 || token->type == CPP_SCOPE)
18539 selector = cp_parser_objc_selector (parser);
18541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18542 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18544 /* Detect if we have a unary selector. */
18545 if (maybe_unary_selector_p)
18547 sel_seq = selector;
18548 goto finish_selector;
18552 cp_parser_error (parser, "expected %<:%>");
18555 maybe_unary_selector_p = false;
18556 token = cp_lexer_consume_token (parser->lexer);
18558 if (token->type == CPP_SCOPE)
18561 = chainon (sel_seq,
18562 build_tree_list (selector, NULL_TREE));
18564 = chainon (sel_seq,
18565 build_tree_list (NULL_TREE, NULL_TREE));
18569 = chainon (sel_seq,
18570 build_tree_list (selector, NULL_TREE));
18572 token = cp_lexer_peek_token (parser->lexer);
18576 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18578 return objc_build_selector_expr (sel_seq);
18581 /* Parse a list of identifiers.
18583 objc-identifier-list:
18585 objc-identifier-list , identifier
18587 Returns a TREE_LIST of identifier nodes. */
18590 cp_parser_objc_identifier_list (cp_parser* parser)
18592 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18593 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18595 while (sep->type == CPP_COMMA)
18597 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18598 list = chainon (list,
18599 build_tree_list (NULL_TREE,
18600 cp_parser_identifier (parser)));
18601 sep = cp_lexer_peek_token (parser->lexer);
18607 /* Parse an Objective-C alias declaration.
18609 objc-alias-declaration:
18610 @compatibility_alias identifier identifier ;
18612 This function registers the alias mapping with the Objective-C front end.
18613 It returns nothing. */
18616 cp_parser_objc_alias_declaration (cp_parser* parser)
18620 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18621 alias = cp_parser_identifier (parser);
18622 orig = cp_parser_identifier (parser);
18623 objc_declare_alias (alias, orig);
18624 cp_parser_consume_semicolon_at_end_of_statement (parser);
18627 /* Parse an Objective-C class forward-declaration.
18629 objc-class-declaration:
18630 @class objc-identifier-list ;
18632 The function registers the forward declarations with the Objective-C
18633 front end. It returns nothing. */
18636 cp_parser_objc_class_declaration (cp_parser* parser)
18638 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18639 objc_declare_class (cp_parser_objc_identifier_list (parser));
18640 cp_parser_consume_semicolon_at_end_of_statement (parser);
18643 /* Parse a list of Objective-C protocol references.
18645 objc-protocol-refs-opt:
18646 objc-protocol-refs [opt]
18648 objc-protocol-refs:
18649 < objc-identifier-list >
18651 Returns a TREE_LIST of identifiers, if any. */
18654 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18656 tree protorefs = NULL_TREE;
18658 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18660 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18661 protorefs = cp_parser_objc_identifier_list (parser);
18662 cp_parser_require (parser, CPP_GREATER, "`>'");
18668 /* Parse a Objective-C visibility specification. */
18671 cp_parser_objc_visibility_spec (cp_parser* parser)
18673 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18675 switch (vis->keyword)
18677 case RID_AT_PRIVATE:
18678 objc_set_visibility (2);
18680 case RID_AT_PROTECTED:
18681 objc_set_visibility (0);
18683 case RID_AT_PUBLIC:
18684 objc_set_visibility (1);
18690 /* Eat '@private'/'@protected'/'@public'. */
18691 cp_lexer_consume_token (parser->lexer);
18694 /* Parse an Objective-C method type. */
18697 cp_parser_objc_method_type (cp_parser* parser)
18699 objc_set_method_type
18700 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18705 /* Parse an Objective-C protocol qualifier. */
18708 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18710 tree quals = NULL_TREE, node;
18711 cp_token *token = cp_lexer_peek_token (parser->lexer);
18713 node = token->u.value;
18715 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18716 && (node == ridpointers [(int) RID_IN]
18717 || node == ridpointers [(int) RID_OUT]
18718 || node == ridpointers [(int) RID_INOUT]
18719 || node == ridpointers [(int) RID_BYCOPY]
18720 || node == ridpointers [(int) RID_BYREF]
18721 || node == ridpointers [(int) RID_ONEWAY]))
18723 quals = tree_cons (NULL_TREE, node, quals);
18724 cp_lexer_consume_token (parser->lexer);
18725 token = cp_lexer_peek_token (parser->lexer);
18726 node = token->u.value;
18732 /* Parse an Objective-C typename. */
18735 cp_parser_objc_typename (cp_parser* parser)
18737 tree typename = NULL_TREE;
18739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18741 tree proto_quals, cp_type = NULL_TREE;
18743 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18744 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18746 /* An ObjC type name may consist of just protocol qualifiers, in which
18747 case the type shall default to 'id'. */
18748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18749 cp_type = cp_parser_type_id (parser);
18751 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18752 typename = build_tree_list (proto_quals, cp_type);
18758 /* Check to see if TYPE refers to an Objective-C selector name. */
18761 cp_parser_objc_selector_p (enum cpp_ttype type)
18763 return (type == CPP_NAME || type == CPP_KEYWORD
18764 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18765 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18766 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18767 || type == CPP_XOR || type == CPP_XOR_EQ);
18770 /* Parse an Objective-C selector. */
18773 cp_parser_objc_selector (cp_parser* parser)
18775 cp_token *token = cp_lexer_consume_token (parser->lexer);
18777 if (!cp_parser_objc_selector_p (token->type))
18779 error ("invalid Objective-C++ selector name");
18780 return error_mark_node;
18783 /* C++ operator names are allowed to appear in ObjC selectors. */
18784 switch (token->type)
18786 case CPP_AND_AND: return get_identifier ("and");
18787 case CPP_AND_EQ: return get_identifier ("and_eq");
18788 case CPP_AND: return get_identifier ("bitand");
18789 case CPP_OR: return get_identifier ("bitor");
18790 case CPP_COMPL: return get_identifier ("compl");
18791 case CPP_NOT: return get_identifier ("not");
18792 case CPP_NOT_EQ: return get_identifier ("not_eq");
18793 case CPP_OR_OR: return get_identifier ("or");
18794 case CPP_OR_EQ: return get_identifier ("or_eq");
18795 case CPP_XOR: return get_identifier ("xor");
18796 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18797 default: return token->u.value;
18801 /* Parse an Objective-C params list. */
18804 cp_parser_objc_method_keyword_params (cp_parser* parser)
18806 tree params = NULL_TREE;
18807 bool maybe_unary_selector_p = true;
18808 cp_token *token = cp_lexer_peek_token (parser->lexer);
18810 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18812 tree selector = NULL_TREE, typename, identifier;
18814 if (token->type != CPP_COLON)
18815 selector = cp_parser_objc_selector (parser);
18817 /* Detect if we have a unary selector. */
18818 if (maybe_unary_selector_p
18819 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18822 maybe_unary_selector_p = false;
18823 cp_parser_require (parser, CPP_COLON, "`:'");
18824 typename = cp_parser_objc_typename (parser);
18825 identifier = cp_parser_identifier (parser);
18829 objc_build_keyword_decl (selector,
18833 token = cp_lexer_peek_token (parser->lexer);
18839 /* Parse the non-keyword Objective-C params. */
18842 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18844 tree params = make_node (TREE_LIST);
18845 cp_token *token = cp_lexer_peek_token (parser->lexer);
18846 *ellipsisp = false; /* Initially, assume no ellipsis. */
18848 while (token->type == CPP_COMMA)
18850 cp_parameter_declarator *parmdecl;
18853 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18854 token = cp_lexer_peek_token (parser->lexer);
18856 if (token->type == CPP_ELLIPSIS)
18858 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18863 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18864 parm = grokdeclarator (parmdecl->declarator,
18865 &parmdecl->decl_specifiers,
18866 PARM, /*initialized=*/0,
18867 /*attrlist=*/NULL);
18869 chainon (params, build_tree_list (NULL_TREE, parm));
18870 token = cp_lexer_peek_token (parser->lexer);
18876 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18879 cp_parser_objc_interstitial_code (cp_parser* parser)
18881 cp_token *token = cp_lexer_peek_token (parser->lexer);
18883 /* If the next token is `extern' and the following token is a string
18884 literal, then we have a linkage specification. */
18885 if (token->keyword == RID_EXTERN
18886 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18887 cp_parser_linkage_specification (parser);
18888 /* Handle #pragma, if any. */
18889 else if (token->type == CPP_PRAGMA)
18890 cp_parser_pragma (parser, pragma_external);
18891 /* Allow stray semicolons. */
18892 else if (token->type == CPP_SEMICOLON)
18893 cp_lexer_consume_token (parser->lexer);
18894 /* Finally, try to parse a block-declaration, or a function-definition. */
18896 cp_parser_block_declaration (parser, /*statement_p=*/false);
18899 /* Parse a method signature. */
18902 cp_parser_objc_method_signature (cp_parser* parser)
18904 tree rettype, kwdparms, optparms;
18905 bool ellipsis = false;
18907 cp_parser_objc_method_type (parser);
18908 rettype = cp_parser_objc_typename (parser);
18909 kwdparms = cp_parser_objc_method_keyword_params (parser);
18910 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18912 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18915 /* Pars an Objective-C method prototype list. */
18918 cp_parser_objc_method_prototype_list (cp_parser* parser)
18920 cp_token *token = cp_lexer_peek_token (parser->lexer);
18922 while (token->keyword != RID_AT_END)
18924 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18926 objc_add_method_declaration
18927 (cp_parser_objc_method_signature (parser));
18928 cp_parser_consume_semicolon_at_end_of_statement (parser);
18931 /* Allow for interspersed non-ObjC++ code. */
18932 cp_parser_objc_interstitial_code (parser);
18934 token = cp_lexer_peek_token (parser->lexer);
18937 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18938 objc_finish_interface ();
18941 /* Parse an Objective-C method definition list. */
18944 cp_parser_objc_method_definition_list (cp_parser* parser)
18946 cp_token *token = cp_lexer_peek_token (parser->lexer);
18948 while (token->keyword != RID_AT_END)
18952 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18954 push_deferring_access_checks (dk_deferred);
18955 objc_start_method_definition
18956 (cp_parser_objc_method_signature (parser));
18958 /* For historical reasons, we accept an optional semicolon. */
18959 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18960 cp_lexer_consume_token (parser->lexer);
18962 perform_deferred_access_checks ();
18963 stop_deferring_access_checks ();
18964 meth = cp_parser_function_definition_after_declarator (parser,
18966 pop_deferring_access_checks ();
18967 objc_finish_method_definition (meth);
18970 /* Allow for interspersed non-ObjC++ code. */
18971 cp_parser_objc_interstitial_code (parser);
18973 token = cp_lexer_peek_token (parser->lexer);
18976 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18977 objc_finish_implementation ();
18980 /* Parse Objective-C ivars. */
18983 cp_parser_objc_class_ivars (cp_parser* parser)
18985 cp_token *token = cp_lexer_peek_token (parser->lexer);
18987 if (token->type != CPP_OPEN_BRACE)
18988 return; /* No ivars specified. */
18990 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18991 token = cp_lexer_peek_token (parser->lexer);
18993 while (token->type != CPP_CLOSE_BRACE)
18995 cp_decl_specifier_seq declspecs;
18996 int decl_class_or_enum_p;
18997 tree prefix_attributes;
18999 cp_parser_objc_visibility_spec (parser);
19001 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19004 cp_parser_decl_specifier_seq (parser,
19005 CP_PARSER_FLAGS_OPTIONAL,
19007 &decl_class_or_enum_p);
19008 prefix_attributes = declspecs.attributes;
19009 declspecs.attributes = NULL_TREE;
19011 /* Keep going until we hit the `;' at the end of the
19013 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19015 tree width = NULL_TREE, attributes, first_attribute, decl;
19016 cp_declarator *declarator = NULL;
19017 int ctor_dtor_or_conv_p;
19019 /* Check for a (possibly unnamed) bitfield declaration. */
19020 token = cp_lexer_peek_token (parser->lexer);
19021 if (token->type == CPP_COLON)
19024 if (token->type == CPP_NAME
19025 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19028 /* Get the name of the bitfield. */
19029 declarator = make_id_declarator (NULL_TREE,
19030 cp_parser_identifier (parser),
19034 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19035 /* Get the width of the bitfield. */
19037 = cp_parser_constant_expression (parser,
19038 /*allow_non_constant=*/false,
19043 /* Parse the declarator. */
19045 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19046 &ctor_dtor_or_conv_p,
19047 /*parenthesized_p=*/NULL,
19048 /*member_p=*/false);
19051 /* Look for attributes that apply to the ivar. */
19052 attributes = cp_parser_attributes_opt (parser);
19053 /* Remember which attributes are prefix attributes and
19055 first_attribute = attributes;
19056 /* Combine the attributes. */
19057 attributes = chainon (prefix_attributes, attributes);
19061 /* Create the bitfield declaration. */
19062 decl = grokbitfield (declarator, &declspecs, width);
19063 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
19066 decl = grokfield (declarator, &declspecs,
19067 NULL_TREE, /*init_const_expr_p=*/false,
19068 NULL_TREE, attributes);
19070 /* Add the instance variable. */
19071 objc_add_instance_variable (decl);
19073 /* Reset PREFIX_ATTRIBUTES. */
19074 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19075 attributes = TREE_CHAIN (attributes);
19077 TREE_CHAIN (attributes) = NULL_TREE;
19079 token = cp_lexer_peek_token (parser->lexer);
19081 if (token->type == CPP_COMMA)
19083 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19089 cp_parser_consume_semicolon_at_end_of_statement (parser);
19090 token = cp_lexer_peek_token (parser->lexer);
19093 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19094 /* For historical reasons, we accept an optional semicolon. */
19095 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19096 cp_lexer_consume_token (parser->lexer);
19099 /* Parse an Objective-C protocol declaration. */
19102 cp_parser_objc_protocol_declaration (cp_parser* parser)
19104 tree proto, protorefs;
19107 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19110 error ("identifier expected after %<@protocol%>");
19114 /* See if we have a forward declaration or a definition. */
19115 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19117 /* Try a forward declaration first. */
19118 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19120 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19122 cp_parser_consume_semicolon_at_end_of_statement (parser);
19125 /* Ok, we got a full-fledged definition (or at least should). */
19128 proto = cp_parser_identifier (parser);
19129 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19130 objc_start_protocol (proto, protorefs);
19131 cp_parser_objc_method_prototype_list (parser);
19135 /* Parse an Objective-C superclass or category. */
19138 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19141 cp_token *next = cp_lexer_peek_token (parser->lexer);
19143 *super = *categ = NULL_TREE;
19144 if (next->type == CPP_COLON)
19146 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19147 *super = cp_parser_identifier (parser);
19149 else if (next->type == CPP_OPEN_PAREN)
19151 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19152 *categ = cp_parser_identifier (parser);
19153 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19157 /* Parse an Objective-C class interface. */
19160 cp_parser_objc_class_interface (cp_parser* parser)
19162 tree name, super, categ, protos;
19164 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19165 name = cp_parser_identifier (parser);
19166 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19167 protos = cp_parser_objc_protocol_refs_opt (parser);
19169 /* We have either a class or a category on our hands. */
19171 objc_start_category_interface (name, categ, protos);
19174 objc_start_class_interface (name, super, protos);
19175 /* Handle instance variable declarations, if any. */
19176 cp_parser_objc_class_ivars (parser);
19177 objc_continue_interface ();
19180 cp_parser_objc_method_prototype_list (parser);
19183 /* Parse an Objective-C class implementation. */
19186 cp_parser_objc_class_implementation (cp_parser* parser)
19188 tree name, super, categ;
19190 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19191 name = cp_parser_identifier (parser);
19192 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19194 /* We have either a class or a category on our hands. */
19196 objc_start_category_implementation (name, categ);
19199 objc_start_class_implementation (name, super);
19200 /* Handle instance variable declarations, if any. */
19201 cp_parser_objc_class_ivars (parser);
19202 objc_continue_implementation ();
19205 cp_parser_objc_method_definition_list (parser);
19208 /* Consume the @end token and finish off the implementation. */
19211 cp_parser_objc_end_implementation (cp_parser* parser)
19213 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19214 objc_finish_implementation ();
19217 /* Parse an Objective-C declaration. */
19220 cp_parser_objc_declaration (cp_parser* parser)
19222 /* Try to figure out what kind of declaration is present. */
19223 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19225 switch (kwd->keyword)
19228 cp_parser_objc_alias_declaration (parser);
19231 cp_parser_objc_class_declaration (parser);
19233 case RID_AT_PROTOCOL:
19234 cp_parser_objc_protocol_declaration (parser);
19236 case RID_AT_INTERFACE:
19237 cp_parser_objc_class_interface (parser);
19239 case RID_AT_IMPLEMENTATION:
19240 cp_parser_objc_class_implementation (parser);
19243 cp_parser_objc_end_implementation (parser);
19246 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19247 cp_parser_skip_to_end_of_block_or_statement (parser);
19251 /* Parse an Objective-C try-catch-finally statement.
19253 objc-try-catch-finally-stmt:
19254 @try compound-statement objc-catch-clause-seq [opt]
19255 objc-finally-clause [opt]
19257 objc-catch-clause-seq:
19258 objc-catch-clause objc-catch-clause-seq [opt]
19261 @catch ( exception-declaration ) compound-statement
19263 objc-finally-clause
19264 @finally compound-statement
19266 Returns NULL_TREE. */
19269 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19270 location_t location;
19273 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19274 location = cp_lexer_peek_token (parser->lexer)->location;
19275 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19276 node, lest it get absorbed into the surrounding block. */
19277 stmt = push_stmt_list ();
19278 cp_parser_compound_statement (parser, NULL, false);
19279 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19281 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19283 cp_parameter_declarator *parmdecl;
19286 cp_lexer_consume_token (parser->lexer);
19287 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19288 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19289 parm = grokdeclarator (parmdecl->declarator,
19290 &parmdecl->decl_specifiers,
19291 PARM, /*initialized=*/0,
19292 /*attrlist=*/NULL);
19293 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19294 objc_begin_catch_clause (parm);
19295 cp_parser_compound_statement (parser, NULL, false);
19296 objc_finish_catch_clause ();
19299 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19301 cp_lexer_consume_token (parser->lexer);
19302 location = cp_lexer_peek_token (parser->lexer)->location;
19303 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19304 node, lest it get absorbed into the surrounding block. */
19305 stmt = push_stmt_list ();
19306 cp_parser_compound_statement (parser, NULL, false);
19307 objc_build_finally_clause (location, pop_stmt_list (stmt));
19310 return objc_finish_try_stmt ();
19313 /* Parse an Objective-C synchronized statement.
19315 objc-synchronized-stmt:
19316 @synchronized ( expression ) compound-statement
19318 Returns NULL_TREE. */
19321 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19322 location_t location;
19325 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19327 location = cp_lexer_peek_token (parser->lexer)->location;
19328 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19329 lock = cp_parser_expression (parser, false);
19330 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19332 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19333 node, lest it get absorbed into the surrounding block. */
19334 stmt = push_stmt_list ();
19335 cp_parser_compound_statement (parser, NULL, false);
19337 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19340 /* Parse an Objective-C throw statement.
19343 @throw assignment-expression [opt] ;
19345 Returns a constructed '@throw' statement. */
19348 cp_parser_objc_throw_statement (cp_parser *parser) {
19349 tree expr = NULL_TREE;
19351 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19353 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19354 expr = cp_parser_assignment_expression (parser, false);
19356 cp_parser_consume_semicolon_at_end_of_statement (parser);
19358 return objc_build_throw_stmt (expr);
19361 /* Parse an Objective-C statement. */
19364 cp_parser_objc_statement (cp_parser * parser) {
19365 /* Try to figure out what kind of declaration is present. */
19366 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19368 switch (kwd->keyword)
19371 return cp_parser_objc_try_catch_finally_statement (parser);
19372 case RID_AT_SYNCHRONIZED:
19373 return cp_parser_objc_synchronized_statement (parser);
19375 return cp_parser_objc_throw_statement (parser);
19377 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19378 cp_parser_skip_to_end_of_block_or_statement (parser);
19381 return error_mark_node;
19384 /* OpenMP 2.5 parsing routines. */
19386 /* Returns name of the next clause.
19387 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19388 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19389 returned and the token is consumed. */
19391 static pragma_omp_clause
19392 cp_parser_omp_clause_name (cp_parser *parser)
19394 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19396 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19397 result = PRAGMA_OMP_CLAUSE_IF;
19398 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19399 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19400 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19401 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19402 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19404 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19405 const char *p = IDENTIFIER_POINTER (id);
19410 if (!strcmp ("copyin", p))
19411 result = PRAGMA_OMP_CLAUSE_COPYIN;
19412 else if (!strcmp ("copyprivate", p))
19413 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19416 if (!strcmp ("firstprivate", p))
19417 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19420 if (!strcmp ("lastprivate", p))
19421 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19424 if (!strcmp ("nowait", p))
19425 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19426 else if (!strcmp ("num_threads", p))
19427 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19430 if (!strcmp ("ordered", p))
19431 result = PRAGMA_OMP_CLAUSE_ORDERED;
19434 if (!strcmp ("reduction", p))
19435 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19438 if (!strcmp ("schedule", p))
19439 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19440 else if (!strcmp ("shared", p))
19441 result = PRAGMA_OMP_CLAUSE_SHARED;
19446 if (result != PRAGMA_OMP_CLAUSE_NONE)
19447 cp_lexer_consume_token (parser->lexer);
19452 /* Validate that a clause of the given type does not already exist. */
19455 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19459 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19460 if (OMP_CLAUSE_CODE (c) == code)
19462 error ("too many %qs clauses", name);
19470 variable-list , identifier
19472 In addition, we match a closing parenthesis. An opening parenthesis
19473 will have been consumed by the caller.
19475 If KIND is nonzero, create the appropriate node and install the decl
19476 in OMP_CLAUSE_DECL and add the node to the head of the list.
19478 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19479 return the list created. */
19482 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19489 name = cp_parser_id_expression (parser, /*template_p=*/false,
19490 /*check_dependency_p=*/true,
19491 /*template_p=*/NULL,
19492 /*declarator_p=*/false,
19493 /*optional_p=*/false);
19494 if (name == error_mark_node)
19497 decl = cp_parser_lookup_name_simple (parser, name);
19498 if (decl == error_mark_node)
19499 cp_parser_name_lookup_error (parser, name, decl, NULL);
19500 else if (kind != 0)
19502 tree u = build_omp_clause (kind);
19503 OMP_CLAUSE_DECL (u) = decl;
19504 OMP_CLAUSE_CHAIN (u) = list;
19508 list = tree_cons (decl, NULL_TREE, list);
19511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19513 cp_lexer_consume_token (parser->lexer);
19516 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19520 /* Try to resync to an unnested comma. Copied from
19521 cp_parser_parenthesized_expression_list. */
19523 ending = cp_parser_skip_to_closing_parenthesis (parser,
19524 /*recovering=*/true,
19526 /*consume_paren=*/true);
19534 /* Similarly, but expect leading and trailing parenthesis. This is a very
19535 common case for omp clauses. */
19538 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19540 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19541 return cp_parser_omp_var_list_no_open (parser, kind, list);
19546 default ( shared | none ) */
19549 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19551 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19554 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19559 const char *p = IDENTIFIER_POINTER (id);
19564 if (strcmp ("none", p) != 0)
19566 kind = OMP_CLAUSE_DEFAULT_NONE;
19570 if (strcmp ("shared", p) != 0)
19572 kind = OMP_CLAUSE_DEFAULT_SHARED;
19579 cp_lexer_consume_token (parser->lexer);
19584 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19587 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19588 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19589 /*or_comma=*/false,
19590 /*consume_paren=*/true);
19592 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19595 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19596 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19597 OMP_CLAUSE_CHAIN (c) = list;
19598 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19604 if ( expression ) */
19607 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19611 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19614 t = cp_parser_condition (parser);
19616 if (t == error_mark_node
19617 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19618 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19619 /*or_comma=*/false,
19620 /*consume_paren=*/true);
19622 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19624 c = build_omp_clause (OMP_CLAUSE_IF);
19625 OMP_CLAUSE_IF_EXPR (c) = t;
19626 OMP_CLAUSE_CHAIN (c) = list;
19635 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19639 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19641 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19642 OMP_CLAUSE_CHAIN (c) = list;
19647 num_threads ( expression ) */
19650 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19654 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19657 t = cp_parser_expression (parser, false);
19659 if (t == error_mark_node
19660 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19661 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19662 /*or_comma=*/false,
19663 /*consume_paren=*/true);
19665 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19667 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19668 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19669 OMP_CLAUSE_CHAIN (c) = list;
19678 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19682 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19684 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19685 OMP_CLAUSE_CHAIN (c) = list;
19690 reduction ( reduction-operator : variable-list )
19692 reduction-operator:
19693 One of: + * - & ^ | && || */
19696 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19698 enum tree_code code;
19701 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19704 switch (cp_lexer_peek_token (parser->lexer)->type)
19716 code = BIT_AND_EXPR;
19719 code = BIT_XOR_EXPR;
19722 code = BIT_IOR_EXPR;
19725 code = TRUTH_ANDIF_EXPR;
19728 code = TRUTH_ORIF_EXPR;
19731 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19733 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19734 /*or_comma=*/false,
19735 /*consume_paren=*/true);
19738 cp_lexer_consume_token (parser->lexer);
19740 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19743 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19744 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19745 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19751 schedule ( schedule-kind )
19752 schedule ( schedule-kind , expression )
19755 static | dynamic | guided | runtime */
19758 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19762 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19765 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19767 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19769 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19770 const char *p = IDENTIFIER_POINTER (id);
19775 if (strcmp ("dynamic", p) != 0)
19777 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19781 if (strcmp ("guided", p) != 0)
19783 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19787 if (strcmp ("runtime", p) != 0)
19789 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19796 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19797 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19800 cp_lexer_consume_token (parser->lexer);
19802 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19804 cp_lexer_consume_token (parser->lexer);
19806 t = cp_parser_assignment_expression (parser, false);
19808 if (t == error_mark_node)
19810 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19811 error ("schedule %<runtime%> does not take "
19812 "a %<chunk_size%> parameter");
19814 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19816 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19819 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19822 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19823 OMP_CLAUSE_CHAIN (c) = list;
19827 cp_parser_error (parser, "invalid schedule kind");
19829 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19830 /*or_comma=*/false,
19831 /*consume_paren=*/true);
19835 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19836 is a bitmask in MASK. Return the list of clauses found; the result
19837 of clause default goes in *pdefault. */
19840 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19841 const char *where, cp_token *pragma_tok)
19843 tree clauses = NULL;
19846 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19848 pragma_omp_clause c_kind;
19849 const char *c_name;
19850 tree prev = clauses;
19852 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19853 cp_lexer_consume_token (parser->lexer);
19855 c_kind = cp_parser_omp_clause_name (parser);
19860 case PRAGMA_OMP_CLAUSE_COPYIN:
19861 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19864 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19865 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19867 c_name = "copyprivate";
19869 case PRAGMA_OMP_CLAUSE_DEFAULT:
19870 clauses = cp_parser_omp_clause_default (parser, clauses);
19871 c_name = "default";
19873 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19874 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19876 c_name = "firstprivate";
19878 case PRAGMA_OMP_CLAUSE_IF:
19879 clauses = cp_parser_omp_clause_if (parser, clauses);
19882 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19883 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19885 c_name = "lastprivate";
19887 case PRAGMA_OMP_CLAUSE_NOWAIT:
19888 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19891 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19892 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19893 c_name = "num_threads";
19895 case PRAGMA_OMP_CLAUSE_ORDERED:
19896 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19897 c_name = "ordered";
19899 case PRAGMA_OMP_CLAUSE_PRIVATE:
19900 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19902 c_name = "private";
19904 case PRAGMA_OMP_CLAUSE_REDUCTION:
19905 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19906 c_name = "reduction";
19908 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19909 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19910 c_name = "schedule";
19912 case PRAGMA_OMP_CLAUSE_SHARED:
19913 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19918 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19922 if (((mask >> c_kind) & 1) == 0)
19924 /* Remove the invalid clause(s) from the list to avoid
19925 confusing the rest of the compiler. */
19927 error ("%qs is not valid for %qs", c_name, where);
19931 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19932 return finish_omp_clauses (clauses);
19939 In practice, we're also interested in adding the statement to an
19940 outer node. So it is convenient if we work around the fact that
19941 cp_parser_statement calls add_stmt. */
19944 cp_parser_begin_omp_structured_block (cp_parser *parser)
19946 unsigned save = parser->in_statement;
19948 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19949 This preserves the "not within loop or switch" style error messages
19950 for nonsense cases like
19956 if (parser->in_statement)
19957 parser->in_statement = IN_OMP_BLOCK;
19963 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19965 parser->in_statement = save;
19969 cp_parser_omp_structured_block (cp_parser *parser)
19971 tree stmt = begin_omp_structured_block ();
19972 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19974 cp_parser_statement (parser, NULL_TREE, false, NULL);
19976 cp_parser_end_omp_structured_block (parser, save);
19977 return finish_omp_structured_block (stmt);
19981 # pragma omp atomic new-line
19985 x binop= expr | x++ | ++x | x-- | --x
19987 +, *, -, /, &, ^, |, <<, >>
19989 where x is an lvalue expression with scalar type. */
19992 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19995 enum tree_code code;
19997 cp_parser_require_pragma_eol (parser, pragma_tok);
19999 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20001 switch (TREE_CODE (lhs))
20006 case PREINCREMENT_EXPR:
20007 case POSTINCREMENT_EXPR:
20008 lhs = TREE_OPERAND (lhs, 0);
20010 rhs = integer_one_node;
20013 case PREDECREMENT_EXPR:
20014 case POSTDECREMENT_EXPR:
20015 lhs = TREE_OPERAND (lhs, 0);
20017 rhs = integer_one_node;
20021 switch (cp_lexer_peek_token (parser->lexer)->type)
20027 code = TRUNC_DIV_EXPR;
20035 case CPP_LSHIFT_EQ:
20036 code = LSHIFT_EXPR;
20038 case CPP_RSHIFT_EQ:
20039 code = RSHIFT_EXPR;
20042 code = BIT_AND_EXPR;
20045 code = BIT_IOR_EXPR;
20048 code = BIT_XOR_EXPR;
20051 cp_parser_error (parser,
20052 "invalid operator for %<#pragma omp atomic%>");
20055 cp_lexer_consume_token (parser->lexer);
20057 rhs = cp_parser_expression (parser, false);
20058 if (rhs == error_mark_node)
20062 finish_omp_atomic (code, lhs, rhs);
20063 cp_parser_consume_semicolon_at_end_of_statement (parser);
20067 cp_parser_skip_to_end_of_block_or_statement (parser);
20072 # pragma omp barrier new-line */
20075 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20077 cp_parser_require_pragma_eol (parser, pragma_tok);
20078 finish_omp_barrier ();
20082 # pragma omp critical [(name)] new-line
20083 structured-block */
20086 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20088 tree stmt, name = NULL;
20090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20092 cp_lexer_consume_token (parser->lexer);
20094 name = cp_parser_identifier (parser);
20096 if (name == error_mark_node
20097 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20098 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20099 /*or_comma=*/false,
20100 /*consume_paren=*/true);
20101 if (name == error_mark_node)
20104 cp_parser_require_pragma_eol (parser, pragma_tok);
20106 stmt = cp_parser_omp_structured_block (parser);
20107 return c_finish_omp_critical (stmt, name);
20111 # pragma omp flush flush-vars[opt] new-line
20114 ( variable-list ) */
20117 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20120 (void) cp_parser_omp_var_list (parser, 0, NULL);
20121 cp_parser_require_pragma_eol (parser, pragma_tok);
20123 finish_omp_flush ();
20126 /* Parse the restricted form of the for statment allowed by OpenMP. */
20129 cp_parser_omp_for_loop (cp_parser *parser)
20131 tree init, cond, incr, body, decl, pre_body;
20134 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20136 cp_parser_error (parser, "for statement expected");
20139 loc = cp_lexer_consume_token (parser->lexer)->location;
20140 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20143 init = decl = NULL;
20144 pre_body = push_stmt_list ();
20145 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20147 cp_decl_specifier_seq type_specifiers;
20149 /* First, try to parse as an initialized declaration. See
20150 cp_parser_condition, from whence the bulk of this is copied. */
20152 cp_parser_parse_tentatively (parser);
20153 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20155 if (!cp_parser_error_occurred (parser))
20157 tree asm_specification, attributes;
20158 cp_declarator *declarator;
20160 declarator = cp_parser_declarator (parser,
20161 CP_PARSER_DECLARATOR_NAMED,
20162 /*ctor_dtor_or_conv_p=*/NULL,
20163 /*parenthesized_p=*/NULL,
20164 /*member_p=*/false);
20165 attributes = cp_parser_attributes_opt (parser);
20166 asm_specification = cp_parser_asm_specification_opt (parser);
20168 cp_parser_require (parser, CPP_EQ, "`='");
20169 if (cp_parser_parse_definitely (parser))
20173 decl = start_decl (declarator, &type_specifiers,
20174 /*initialized_p=*/false, attributes,
20175 /*prefix_attributes=*/NULL_TREE,
20178 init = cp_parser_assignment_expression (parser, false);
20180 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20181 init = error_mark_node;
20183 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20184 asm_specification, LOOKUP_ONLYCONVERTING);
20187 pop_scope (pushed_scope);
20191 cp_parser_abort_tentative_parse (parser);
20193 /* If parsing as an initialized declaration failed, try again as
20194 a simple expression. */
20196 init = cp_parser_expression (parser, false);
20198 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20199 pre_body = pop_stmt_list (pre_body);
20202 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20203 cond = cp_parser_condition (parser);
20204 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20208 incr = cp_parser_expression (parser, false);
20210 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20211 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20212 /*or_comma=*/false,
20213 /*consume_paren=*/true);
20215 /* Note that we saved the original contents of this flag when we entered
20216 the structured block, and so we don't need to re-save it here. */
20217 parser->in_statement = IN_OMP_FOR;
20219 /* Note that the grammar doesn't call for a structured block here,
20220 though the loop as a whole is a structured block. */
20221 body = push_stmt_list ();
20222 cp_parser_statement (parser, NULL_TREE, false, NULL);
20223 body = pop_stmt_list (body);
20225 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20229 #pragma omp for for-clause[optseq] new-line
20232 #define OMP_FOR_CLAUSE_MASK \
20233 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20234 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20235 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20236 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20237 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20238 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20239 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20242 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20244 tree clauses, sb, ret;
20247 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20248 "#pragma omp for", pragma_tok);
20250 sb = begin_omp_structured_block ();
20251 save = cp_parser_begin_omp_structured_block (parser);
20253 ret = cp_parser_omp_for_loop (parser);
20255 OMP_FOR_CLAUSES (ret) = clauses;
20257 cp_parser_end_omp_structured_block (parser, save);
20258 add_stmt (finish_omp_structured_block (sb));
20264 # pragma omp master new-line
20265 structured-block */
20268 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20270 cp_parser_require_pragma_eol (parser, pragma_tok);
20271 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20275 # pragma omp ordered new-line
20276 structured-block */
20279 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20281 cp_parser_require_pragma_eol (parser, pragma_tok);
20282 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20288 { section-sequence }
20291 section-directive[opt] structured-block
20292 section-sequence section-directive structured-block */
20295 cp_parser_omp_sections_scope (cp_parser *parser)
20297 tree stmt, substmt;
20298 bool error_suppress = false;
20301 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20304 stmt = push_stmt_list ();
20306 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20310 substmt = begin_omp_structured_block ();
20311 save = cp_parser_begin_omp_structured_block (parser);
20315 cp_parser_statement (parser, NULL_TREE, false, NULL);
20317 tok = cp_lexer_peek_token (parser->lexer);
20318 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20320 if (tok->type == CPP_CLOSE_BRACE)
20322 if (tok->type == CPP_EOF)
20326 cp_parser_end_omp_structured_block (parser, save);
20327 substmt = finish_omp_structured_block (substmt);
20328 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20329 add_stmt (substmt);
20334 tok = cp_lexer_peek_token (parser->lexer);
20335 if (tok->type == CPP_CLOSE_BRACE)
20337 if (tok->type == CPP_EOF)
20340 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20342 cp_lexer_consume_token (parser->lexer);
20343 cp_parser_require_pragma_eol (parser, tok);
20344 error_suppress = false;
20346 else if (!error_suppress)
20348 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20349 error_suppress = true;
20352 substmt = cp_parser_omp_structured_block (parser);
20353 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20354 add_stmt (substmt);
20356 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20358 substmt = pop_stmt_list (stmt);
20360 stmt = make_node (OMP_SECTIONS);
20361 TREE_TYPE (stmt) = void_type_node;
20362 OMP_SECTIONS_BODY (stmt) = substmt;
20369 # pragma omp sections sections-clause[optseq] newline
20372 #define OMP_SECTIONS_CLAUSE_MASK \
20373 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20374 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20375 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20376 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20377 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20380 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20384 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20385 "#pragma omp sections", pragma_tok);
20387 ret = cp_parser_omp_sections_scope (parser);
20389 OMP_SECTIONS_CLAUSES (ret) = clauses;
20395 # pragma parallel parallel-clause new-line
20396 # pragma parallel for parallel-for-clause new-line
20397 # pragma parallel sections parallel-sections-clause new-line */
20399 #define OMP_PARALLEL_CLAUSE_MASK \
20400 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20401 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20402 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20403 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20404 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20405 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20406 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20407 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20410 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20412 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20413 const char *p_name = "#pragma omp parallel";
20414 tree stmt, clauses, par_clause, ws_clause, block;
20415 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20420 cp_lexer_consume_token (parser->lexer);
20421 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20422 p_name = "#pragma omp parallel for";
20423 mask |= OMP_FOR_CLAUSE_MASK;
20424 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20426 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20428 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20429 const char *p = IDENTIFIER_POINTER (id);
20430 if (strcmp (p, "sections") == 0)
20432 cp_lexer_consume_token (parser->lexer);
20433 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20434 p_name = "#pragma omp parallel sections";
20435 mask |= OMP_SECTIONS_CLAUSE_MASK;
20436 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20440 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20441 block = begin_omp_parallel ();
20442 save = cp_parser_begin_omp_structured_block (parser);
20446 case PRAGMA_OMP_PARALLEL:
20447 cp_parser_statement (parser, NULL_TREE, false, NULL);
20448 par_clause = clauses;
20451 case PRAGMA_OMP_PARALLEL_FOR:
20452 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20453 stmt = cp_parser_omp_for_loop (parser);
20455 OMP_FOR_CLAUSES (stmt) = ws_clause;
20458 case PRAGMA_OMP_PARALLEL_SECTIONS:
20459 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20460 stmt = cp_parser_omp_sections_scope (parser);
20462 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20466 gcc_unreachable ();
20469 cp_parser_end_omp_structured_block (parser, save);
20470 stmt = finish_omp_parallel (par_clause, block);
20471 if (p_kind != PRAGMA_OMP_PARALLEL)
20472 OMP_PARALLEL_COMBINED (stmt) = 1;
20477 # pragma omp single single-clause[optseq] new-line
20478 structured-block */
20480 #define OMP_SINGLE_CLAUSE_MASK \
20481 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20482 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20483 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20484 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20487 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20489 tree stmt = make_node (OMP_SINGLE);
20490 TREE_TYPE (stmt) = void_type_node;
20492 OMP_SINGLE_CLAUSES (stmt)
20493 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20494 "#pragma omp single", pragma_tok);
20495 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20497 return add_stmt (stmt);
20501 # pragma omp threadprivate (variable-list) */
20504 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20508 vars = cp_parser_omp_var_list (parser, 0, NULL);
20509 cp_parser_require_pragma_eol (parser, pragma_tok);
20511 finish_omp_threadprivate (vars);
20514 /* Main entry point to OpenMP statement pragmas. */
20517 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20521 switch (pragma_tok->pragma_kind)
20523 case PRAGMA_OMP_ATOMIC:
20524 cp_parser_omp_atomic (parser, pragma_tok);
20526 case PRAGMA_OMP_CRITICAL:
20527 stmt = cp_parser_omp_critical (parser, pragma_tok);
20529 case PRAGMA_OMP_FOR:
20530 stmt = cp_parser_omp_for (parser, pragma_tok);
20532 case PRAGMA_OMP_MASTER:
20533 stmt = cp_parser_omp_master (parser, pragma_tok);
20535 case PRAGMA_OMP_ORDERED:
20536 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20538 case PRAGMA_OMP_PARALLEL:
20539 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20541 case PRAGMA_OMP_SECTIONS:
20542 stmt = cp_parser_omp_sections (parser, pragma_tok);
20544 case PRAGMA_OMP_SINGLE:
20545 stmt = cp_parser_omp_single (parser, pragma_tok);
20548 gcc_unreachable ();
20552 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20557 static GTY (()) cp_parser *the_parser;
20560 /* Special handling for the first token or line in the file. The first
20561 thing in the file might be #pragma GCC pch_preprocess, which loads a
20562 PCH file, which is a GC collection point. So we need to handle this
20563 first pragma without benefit of an existing lexer structure.
20565 Always returns one token to the caller in *FIRST_TOKEN. This is
20566 either the true first token of the file, or the first token after
20567 the initial pragma. */
20570 cp_parser_initial_pragma (cp_token *first_token)
20574 cp_lexer_get_preprocessor_token (NULL, first_token);
20575 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20578 cp_lexer_get_preprocessor_token (NULL, first_token);
20579 if (first_token->type == CPP_STRING)
20581 name = first_token->u.value;
20583 cp_lexer_get_preprocessor_token (NULL, first_token);
20584 if (first_token->type != CPP_PRAGMA_EOL)
20585 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20588 error ("expected string literal");
20590 /* Skip to the end of the pragma. */
20591 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20592 cp_lexer_get_preprocessor_token (NULL, first_token);
20594 /* Now actually load the PCH file. */
20596 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20598 /* Read one more token to return to our caller. We have to do this
20599 after reading the PCH file in, since its pointers have to be
20601 cp_lexer_get_preprocessor_token (NULL, first_token);
20604 /* Normal parsing of a pragma token. Here we can (and must) use the
20608 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20610 cp_token *pragma_tok;
20613 pragma_tok = cp_lexer_consume_token (parser->lexer);
20614 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20615 parser->lexer->in_pragma = true;
20617 id = pragma_tok->pragma_kind;
20620 case PRAGMA_GCC_PCH_PREPROCESS:
20621 error ("%<#pragma GCC pch_preprocess%> must be first");
20624 case PRAGMA_OMP_BARRIER:
20627 case pragma_compound:
20628 cp_parser_omp_barrier (parser, pragma_tok);
20631 error ("%<#pragma omp barrier%> may only be "
20632 "used in compound statements");
20639 case PRAGMA_OMP_FLUSH:
20642 case pragma_compound:
20643 cp_parser_omp_flush (parser, pragma_tok);
20646 error ("%<#pragma omp flush%> may only be "
20647 "used in compound statements");
20654 case PRAGMA_OMP_THREADPRIVATE:
20655 cp_parser_omp_threadprivate (parser, pragma_tok);
20658 case PRAGMA_OMP_ATOMIC:
20659 case PRAGMA_OMP_CRITICAL:
20660 case PRAGMA_OMP_FOR:
20661 case PRAGMA_OMP_MASTER:
20662 case PRAGMA_OMP_ORDERED:
20663 case PRAGMA_OMP_PARALLEL:
20664 case PRAGMA_OMP_SECTIONS:
20665 case PRAGMA_OMP_SINGLE:
20666 if (context == pragma_external)
20668 cp_parser_omp_construct (parser, pragma_tok);
20671 case PRAGMA_OMP_SECTION:
20672 error ("%<#pragma omp section%> may only be used in "
20673 "%<#pragma omp sections%> construct");
20677 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20678 c_invoke_pragma_handler (id);
20682 cp_parser_error (parser, "expected declaration specifiers");
20686 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20690 /* The interface the pragma parsers have to the lexer. */
20693 pragma_lex (tree *value)
20696 enum cpp_ttype ret;
20698 tok = cp_lexer_peek_token (the_parser->lexer);
20701 *value = tok->u.value;
20703 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20705 else if (ret == CPP_STRING)
20706 *value = cp_parser_string_literal (the_parser, false, false);
20709 cp_lexer_consume_token (the_parser->lexer);
20710 if (ret == CPP_KEYWORD)
20718 /* External interface. */
20720 /* Parse one entire translation unit. */
20723 c_parse_file (void)
20725 bool error_occurred;
20726 static bool already_called = false;
20728 if (already_called)
20730 sorry ("inter-module optimizations not implemented for C++");
20733 already_called = true;
20735 the_parser = cp_parser_new ();
20736 push_deferring_access_checks (flag_access_control
20737 ? dk_no_deferred : dk_no_check);
20738 error_occurred = cp_parser_translation_unit (the_parser);
20742 #include "gt-cp-parser.h"