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 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
567 /* GNU extensions. */
570 /* C++0x extensions. */
579 /* Return a pointer to the Nth token in the token stream. If N is 1,
580 then this is precisely equivalent to cp_lexer_peek_token (except
581 that it is not inline). One would like to disallow that case, but
582 there is one case (cp_parser_nth_token_starts_template_id) where
583 the caller passes a variable for N and it might be 1. */
586 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
590 /* N is 1-based, not zero-based. */
593 if (cp_lexer_debugging_p (lexer))
594 fprintf (cp_lexer_debug_stream,
595 "cp_lexer: peeking ahead %ld at token: ", (long)n);
598 token = lexer->next_token;
599 gcc_assert (!n || token != &eof_token);
603 if (token == lexer->last_token)
609 if (token->type != CPP_PURGED)
613 if (cp_lexer_debugging_p (lexer))
615 cp_lexer_print_token (cp_lexer_debug_stream, token);
616 putc ('\n', cp_lexer_debug_stream);
622 /* Return the next token, and advance the lexer's next_token pointer
623 to point to the next non-purged token. */
626 cp_lexer_consume_token (cp_lexer* lexer)
628 cp_token *token = lexer->next_token;
630 gcc_assert (token != &eof_token);
631 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
636 if (lexer->next_token == lexer->last_token)
638 lexer->next_token = &eof_token;
643 while (lexer->next_token->type == CPP_PURGED);
645 cp_lexer_set_source_position_from_token (token);
647 /* Provide debugging output. */
648 if (cp_lexer_debugging_p (lexer))
650 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
651 cp_lexer_print_token (cp_lexer_debug_stream, token);
652 putc ('\n', cp_lexer_debug_stream);
658 /* Permanently remove the next token from the token stream, and
659 advance the next_token pointer to refer to the next non-purged
663 cp_lexer_purge_token (cp_lexer *lexer)
665 cp_token *tok = lexer->next_token;
667 gcc_assert (tok != &eof_token);
668 tok->type = CPP_PURGED;
669 tok->location = UNKNOWN_LOCATION;
670 tok->u.value = NULL_TREE;
671 tok->keyword = RID_MAX;
676 if (tok == lexer->last_token)
682 while (tok->type == CPP_PURGED);
683 lexer->next_token = tok;
686 /* Permanently remove all tokens after TOK, up to, but not
687 including, the token that will be returned next by
688 cp_lexer_peek_token. */
691 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
693 cp_token *peek = lexer->next_token;
695 if (peek == &eof_token)
696 peek = lexer->last_token;
698 gcc_assert (tok < peek);
700 for ( tok += 1; tok != peek; tok += 1)
702 tok->type = CPP_PURGED;
703 tok->location = UNKNOWN_LOCATION;
704 tok->u.value = NULL_TREE;
705 tok->keyword = RID_MAX;
709 /* Begin saving tokens. All tokens consumed after this point will be
713 cp_lexer_save_tokens (cp_lexer* lexer)
715 /* Provide debugging output. */
716 if (cp_lexer_debugging_p (lexer))
717 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
719 VEC_safe_push (cp_token_position, heap,
720 lexer->saved_tokens, lexer->next_token);
723 /* Commit to the portion of the token stream most recently saved. */
726 cp_lexer_commit_tokens (cp_lexer* lexer)
728 /* Provide debugging output. */
729 if (cp_lexer_debugging_p (lexer))
730 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
732 VEC_pop (cp_token_position, lexer->saved_tokens);
735 /* Return all tokens saved since the last call to cp_lexer_save_tokens
736 to the token stream. Stop saving tokens. */
739 cp_lexer_rollback_tokens (cp_lexer* lexer)
741 /* Provide debugging output. */
742 if (cp_lexer_debugging_p (lexer))
743 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
745 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 /* Print a representation of the TOKEN on the STREAM. */
750 #ifdef ENABLE_CHECKING
753 cp_lexer_print_token (FILE * stream, cp_token *token)
755 /* We don't use cpp_type2name here because the parser defines
756 a few tokens of its own. */
757 static const char *const token_names[] = {
758 /* cpplib-defined token types */
764 /* C++ parser token types - see "Manifest constants", above. */
767 "NESTED_NAME_SPECIFIER",
771 /* If we have a name for the token, print it out. Otherwise, we
772 simply give the numeric code. */
773 gcc_assert (token->type < ARRAY_SIZE(token_names));
774 fputs (token_names[token->type], stream);
776 /* For some tokens, print the associated data. */
780 /* Some keywords have a value that is not an IDENTIFIER_NODE.
781 For example, `struct' is mapped to an INTEGER_CST. */
782 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
784 /* else fall through */
786 fputs (IDENTIFIER_POINTER (token->u.value), stream);
791 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
799 /* Start emitting debugging information. */
802 cp_lexer_start_debugging (cp_lexer* lexer)
804 lexer->debugging_p = true;
807 /* Stop emitting debugging information. */
810 cp_lexer_stop_debugging (cp_lexer* lexer)
812 lexer->debugging_p = false;
815 #endif /* ENABLE_CHECKING */
817 /* Create a new cp_token_cache, representing a range of tokens. */
819 static cp_token_cache *
820 cp_token_cache_new (cp_token *first, cp_token *last)
822 cp_token_cache *cache = GGC_NEW (cp_token_cache);
823 cache->first = first;
829 /* Decl-specifiers. */
831 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
834 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
836 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 /* Nothing other than the parser should be creating declarators;
842 declarators are a semi-syntactic representation of C++ entities.
843 Other parts of the front end that need to create entities (like
844 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
846 static cp_declarator *make_call_declarator
847 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
848 static cp_declarator *make_array_declarator
849 (cp_declarator *, tree);
850 static cp_declarator *make_pointer_declarator
851 (cp_cv_quals, cp_declarator *);
852 static cp_declarator *make_reference_declarator
853 (cp_cv_quals, cp_declarator *, bool);
854 static cp_parameter_declarator *make_parameter_declarator
855 (cp_decl_specifier_seq *, cp_declarator *, tree);
856 static cp_declarator *make_ptrmem_declarator
857 (cp_cv_quals, tree, cp_declarator *);
859 /* An erroneous declarator. */
860 static cp_declarator *cp_error_declarator;
862 /* The obstack on which declarators and related data structures are
864 static struct obstack declarator_obstack;
866 /* Alloc BYTES from the declarator memory pool. */
869 alloc_declarator (size_t bytes)
871 return obstack_alloc (&declarator_obstack, bytes);
874 /* Allocate a declarator of the indicated KIND. Clear fields that are
875 common to all declarators. */
877 static cp_declarator *
878 make_declarator (cp_declarator_kind kind)
880 cp_declarator *declarator;
882 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
883 declarator->kind = kind;
884 declarator->attributes = NULL_TREE;
885 declarator->declarator = NULL;
886 declarator->parameter_pack_p = false;
891 /* Make a declarator for a generalized identifier. If
892 QUALIFYING_SCOPE is non-NULL, the identifier is
893 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
894 UNQUALIFIED_NAME. SFK indicates the kind of special function this
897 static cp_declarator *
898 make_id_declarator (tree qualifying_scope, tree unqualified_name,
899 special_function_kind sfk)
901 cp_declarator *declarator;
903 /* It is valid to write:
905 class C { void f(); };
909 The standard is not clear about whether `typedef const C D' is
910 legal; as of 2002-09-15 the committee is considering that
911 question. EDG 3.0 allows that syntax. Therefore, we do as
913 if (qualifying_scope && TYPE_P (qualifying_scope))
914 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
916 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
917 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
918 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
920 declarator = make_declarator (cdk_id);
921 declarator->u.id.qualifying_scope = qualifying_scope;
922 declarator->u.id.unqualified_name = unqualified_name;
923 declarator->u.id.sfk = sfk;
928 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
929 of modifiers such as const or volatile to apply to the pointer
930 type, represented as identifiers. */
933 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
935 cp_declarator *declarator;
937 declarator = make_declarator (cdk_pointer);
938 declarator->declarator = target;
939 declarator->u.pointer.qualifiers = cv_qualifiers;
940 declarator->u.pointer.class_type = NULL_TREE;
943 declarator->parameter_pack_p = target->parameter_pack_p;
944 target->parameter_pack_p = false;
947 declarator->parameter_pack_p = false;
952 /* Like make_pointer_declarator -- but for references. */
955 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
958 cp_declarator *declarator;
960 declarator = make_declarator (cdk_reference);
961 declarator->declarator = target;
962 declarator->u.reference.qualifiers = cv_qualifiers;
963 declarator->u.reference.rvalue_ref = rvalue_ref;
966 declarator->parameter_pack_p = target->parameter_pack_p;
967 target->parameter_pack_p = false;
970 declarator->parameter_pack_p = false;
975 /* Like make_pointer_declarator -- but for a pointer to a non-static
976 member of CLASS_TYPE. */
979 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
980 cp_declarator *pointee)
982 cp_declarator *declarator;
984 declarator = make_declarator (cdk_ptrmem);
985 declarator->declarator = pointee;
986 declarator->u.pointer.qualifiers = cv_qualifiers;
987 declarator->u.pointer.class_type = class_type;
991 declarator->parameter_pack_p = pointee->parameter_pack_p;
992 pointee->parameter_pack_p = false;
995 declarator->parameter_pack_p = false;
1000 /* Make a declarator for the function given by TARGET, with the
1001 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1002 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1003 indicates what exceptions can be thrown. */
1006 make_call_declarator (cp_declarator *target,
1007 cp_parameter_declarator *parms,
1008 cp_cv_quals cv_qualifiers,
1009 tree exception_specification)
1011 cp_declarator *declarator;
1013 declarator = make_declarator (cdk_function);
1014 declarator->declarator = target;
1015 declarator->u.function.parameters = parms;
1016 declarator->u.function.qualifiers = cv_qualifiers;
1017 declarator->u.function.exception_specification = exception_specification;
1020 declarator->parameter_pack_p = target->parameter_pack_p;
1021 target->parameter_pack_p = false;
1024 declarator->parameter_pack_p = false;
1029 /* Make a declarator for an array of BOUNDS elements, each of which is
1030 defined by ELEMENT. */
1033 make_array_declarator (cp_declarator *element, tree bounds)
1035 cp_declarator *declarator;
1037 declarator = make_declarator (cdk_array);
1038 declarator->declarator = element;
1039 declarator->u.array.bounds = bounds;
1042 declarator->parameter_pack_p = element->parameter_pack_p;
1043 element->parameter_pack_p = false;
1046 declarator->parameter_pack_p = false;
1051 /* Determine whether the declarator we've seen so far can be a
1052 parameter pack, when followed by an ellipsis. */
1054 declarator_can_be_parameter_pack (cp_declarator *declarator)
1056 /* Search for a declarator name, or any other declarator that goes
1057 after the point where the ellipsis could appear in a parameter
1058 pack. If we find any of these, then this declarator can not be
1059 made into a parameter pack. */
1061 while (declarator && !found)
1063 switch ((int)declarator->kind)
1074 declarator = declarator->declarator;
1082 cp_parameter_declarator *no_parameters;
1084 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1085 DECLARATOR and DEFAULT_ARGUMENT. */
1087 cp_parameter_declarator *
1088 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1089 cp_declarator *declarator,
1090 tree default_argument)
1092 cp_parameter_declarator *parameter;
1094 parameter = ((cp_parameter_declarator *)
1095 alloc_declarator (sizeof (cp_parameter_declarator)));
1096 parameter->next = NULL;
1097 if (decl_specifiers)
1098 parameter->decl_specifiers = *decl_specifiers;
1100 clear_decl_specs (¶meter->decl_specifiers);
1101 parameter->declarator = declarator;
1102 parameter->default_argument = default_argument;
1103 parameter->ellipsis_p = false;
1108 /* Returns true iff DECLARATOR is a declaration for a function. */
1111 function_declarator_p (const cp_declarator *declarator)
1115 if (declarator->kind == cdk_function
1116 && declarator->declarator->kind == cdk_id)
1118 if (declarator->kind == cdk_id
1119 || declarator->kind == cdk_error)
1121 declarator = declarator->declarator;
1131 A cp_parser parses the token stream as specified by the C++
1132 grammar. Its job is purely parsing, not semantic analysis. For
1133 example, the parser breaks the token stream into declarators,
1134 expressions, statements, and other similar syntactic constructs.
1135 It does not check that the types of the expressions on either side
1136 of an assignment-statement are compatible, or that a function is
1137 not declared with a parameter of type `void'.
1139 The parser invokes routines elsewhere in the compiler to perform
1140 semantic analysis and to build up the abstract syntax tree for the
1143 The parser (and the template instantiation code, which is, in a
1144 way, a close relative of parsing) are the only parts of the
1145 compiler that should be calling push_scope and pop_scope, or
1146 related functions. The parser (and template instantiation code)
1147 keeps track of what scope is presently active; everything else
1148 should simply honor that. (The code that generates static
1149 initializers may also need to set the scope, in order to check
1150 access control correctly when emitting the initializers.)
1155 The parser is of the standard recursive-descent variety. Upcoming
1156 tokens in the token stream are examined in order to determine which
1157 production to use when parsing a non-terminal. Some C++ constructs
1158 require arbitrary look ahead to disambiguate. For example, it is
1159 impossible, in the general case, to tell whether a statement is an
1160 expression or declaration without scanning the entire statement.
1161 Therefore, the parser is capable of "parsing tentatively." When the
1162 parser is not sure what construct comes next, it enters this mode.
1163 Then, while we attempt to parse the construct, the parser queues up
1164 error messages, rather than issuing them immediately, and saves the
1165 tokens it consumes. If the construct is parsed successfully, the
1166 parser "commits", i.e., it issues any queued error messages and
1167 the tokens that were being preserved are permanently discarded.
1168 If, however, the construct is not parsed successfully, the parser
1169 rolls back its state completely so that it can resume parsing using
1170 a different alternative.
1175 The performance of the parser could probably be improved substantially.
1176 We could often eliminate the need to parse tentatively by looking ahead
1177 a little bit. In some places, this approach might not entirely eliminate
1178 the need to parse tentatively, but it might still speed up the average
1181 /* Flags that are passed to some parsing functions. These values can
1182 be bitwise-ored together. */
1184 typedef enum cp_parser_flags
1187 CP_PARSER_FLAGS_NONE = 0x0,
1188 /* The construct is optional. If it is not present, then no error
1189 should be issued. */
1190 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1191 /* When parsing a type-specifier, do not allow user-defined types. */
1192 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1195 /* The different kinds of declarators we want to parse. */
1197 typedef enum cp_parser_declarator_kind
1199 /* We want an abstract declarator. */
1200 CP_PARSER_DECLARATOR_ABSTRACT,
1201 /* We want a named declarator. */
1202 CP_PARSER_DECLARATOR_NAMED,
1203 /* We don't mind, but the name must be an unqualified-id. */
1204 CP_PARSER_DECLARATOR_EITHER
1205 } cp_parser_declarator_kind;
1207 /* The precedence values used to parse binary expressions. The minimum value
1208 of PREC must be 1, because zero is reserved to quickly discriminate
1209 binary operators from other tokens. */
1214 PREC_LOGICAL_OR_EXPRESSION,
1215 PREC_LOGICAL_AND_EXPRESSION,
1216 PREC_INCLUSIVE_OR_EXPRESSION,
1217 PREC_EXCLUSIVE_OR_EXPRESSION,
1218 PREC_AND_EXPRESSION,
1219 PREC_EQUALITY_EXPRESSION,
1220 PREC_RELATIONAL_EXPRESSION,
1221 PREC_SHIFT_EXPRESSION,
1222 PREC_ADDITIVE_EXPRESSION,
1223 PREC_MULTIPLICATIVE_EXPRESSION,
1225 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1228 /* A mapping from a token type to a corresponding tree node type, with a
1229 precedence value. */
1231 typedef struct cp_parser_binary_operations_map_node
1233 /* The token type. */
1234 enum cpp_ttype token_type;
1235 /* The corresponding tree code. */
1236 enum tree_code tree_type;
1237 /* The precedence of this operator. */
1238 enum cp_parser_prec prec;
1239 } cp_parser_binary_operations_map_node;
1241 /* The status of a tentative parse. */
1243 typedef enum cp_parser_status_kind
1245 /* No errors have occurred. */
1246 CP_PARSER_STATUS_KIND_NO_ERROR,
1247 /* An error has occurred. */
1248 CP_PARSER_STATUS_KIND_ERROR,
1249 /* We are committed to this tentative parse, whether or not an error
1251 CP_PARSER_STATUS_KIND_COMMITTED
1252 } cp_parser_status_kind;
1254 typedef struct cp_parser_expression_stack_entry
1256 /* Left hand side of the binary operation we are currently
1259 /* Original tree code for left hand side, if it was a binary
1260 expression itself (used for -Wparentheses). */
1261 enum tree_code lhs_type;
1262 /* Tree code for the binary operation we are parsing. */
1263 enum tree_code tree_type;
1264 /* Precedence of the binary operation we are parsing. */
1266 } cp_parser_expression_stack_entry;
1268 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1269 entries because precedence levels on the stack are monotonically
1271 typedef struct cp_parser_expression_stack_entry
1272 cp_parser_expression_stack[NUM_PREC_VALUES];
1274 /* Context that is saved and restored when parsing tentatively. */
1275 typedef struct cp_parser_context GTY (())
1277 /* If this is a tentative parsing context, the status of the
1279 enum cp_parser_status_kind status;
1280 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1281 that are looked up in this context must be looked up both in the
1282 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1283 the context of the containing expression. */
1286 /* The next parsing context in the stack. */
1287 struct cp_parser_context *next;
1288 } cp_parser_context;
1292 /* Constructors and destructors. */
1294 static cp_parser_context *cp_parser_context_new
1295 (cp_parser_context *);
1297 /* Class variables. */
1299 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1301 /* The operator-precedence table used by cp_parser_binary_expression.
1302 Transformed into an associative array (binops_by_token) by
1305 static const cp_parser_binary_operations_map_node binops[] = {
1306 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1307 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1309 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1310 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1311 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1313 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1314 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1316 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1317 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1319 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1320 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1321 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1322 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1324 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1325 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1327 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1329 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1331 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1333 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1335 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1338 /* The same as binops, but initialized by cp_parser_new so that
1339 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1341 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1343 /* Constructors and destructors. */
1345 /* Construct a new context. The context below this one on the stack
1346 is given by NEXT. */
1348 static cp_parser_context *
1349 cp_parser_context_new (cp_parser_context* next)
1351 cp_parser_context *context;
1353 /* Allocate the storage. */
1354 if (cp_parser_context_free_list != NULL)
1356 /* Pull the first entry from the free list. */
1357 context = cp_parser_context_free_list;
1358 cp_parser_context_free_list = context->next;
1359 memset (context, 0, sizeof (*context));
1362 context = GGC_CNEW (cp_parser_context);
1364 /* No errors have occurred yet in this context. */
1365 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1366 /* If this is not the bottomost context, copy information that we
1367 need from the previous context. */
1370 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1371 expression, then we are parsing one in this context, too. */
1372 context->object_type = next->object_type;
1373 /* Thread the stack. */
1374 context->next = next;
1380 /* The cp_parser structure represents the C++ parser. */
1382 typedef struct cp_parser GTY(())
1384 /* The lexer from which we are obtaining tokens. */
1387 /* The scope in which names should be looked up. If NULL_TREE, then
1388 we look up names in the scope that is currently open in the
1389 source program. If non-NULL, this is either a TYPE or
1390 NAMESPACE_DECL for the scope in which we should look. It can
1391 also be ERROR_MARK, when we've parsed a bogus scope.
1393 This value is not cleared automatically after a name is looked
1394 up, so we must be careful to clear it before starting a new look
1395 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1396 will look up `Z' in the scope of `X', rather than the current
1397 scope.) Unfortunately, it is difficult to tell when name lookup
1398 is complete, because we sometimes peek at a token, look it up,
1399 and then decide not to consume it. */
1402 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1403 last lookup took place. OBJECT_SCOPE is used if an expression
1404 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1405 respectively. QUALIFYING_SCOPE is used for an expression of the
1406 form "X::Y"; it refers to X. */
1408 tree qualifying_scope;
1410 /* A stack of parsing contexts. All but the bottom entry on the
1411 stack will be tentative contexts.
1413 We parse tentatively in order to determine which construct is in
1414 use in some situations. For example, in order to determine
1415 whether a statement is an expression-statement or a
1416 declaration-statement we parse it tentatively as a
1417 declaration-statement. If that fails, we then reparse the same
1418 token stream as an expression-statement. */
1419 cp_parser_context *context;
1421 /* True if we are parsing GNU C++. If this flag is not set, then
1422 GNU extensions are not recognized. */
1423 bool allow_gnu_extensions_p;
1425 /* TRUE if the `>' token should be interpreted as the greater-than
1426 operator. FALSE if it is the end of a template-id or
1427 template-parameter-list. In C++0x mode, this flag also applies to
1428 `>>' tokens, which are viewed as two consecutive `>' tokens when
1429 this flag is FALSE. */
1430 bool greater_than_is_operator_p;
1432 /* TRUE if default arguments are allowed within a parameter list
1433 that starts at this point. FALSE if only a gnu extension makes
1434 them permissible. */
1435 bool default_arg_ok_p;
1437 /* TRUE if we are parsing an integral constant-expression. See
1438 [expr.const] for a precise definition. */
1439 bool integral_constant_expression_p;
1441 /* TRUE if we are parsing an integral constant-expression -- but a
1442 non-constant expression should be permitted as well. This flag
1443 is used when parsing an array bound so that GNU variable-length
1444 arrays are tolerated. */
1445 bool allow_non_integral_constant_expression_p;
1447 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1448 been seen that makes the expression non-constant. */
1449 bool non_integral_constant_expression_p;
1451 /* TRUE if local variable names and `this' are forbidden in the
1453 bool local_variables_forbidden_p;
1455 /* TRUE if the declaration we are parsing is part of a
1456 linkage-specification of the form `extern string-literal
1458 bool in_unbraced_linkage_specification_p;
1460 /* TRUE if we are presently parsing a declarator, after the
1461 direct-declarator. */
1462 bool in_declarator_p;
1464 /* TRUE if we are presently parsing a template-argument-list. */
1465 bool in_template_argument_list_p;
1467 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1468 to IN_OMP_BLOCK if parsing OpenMP structured block and
1469 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1470 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1471 iteration-statement, OpenMP block or loop within that switch. */
1472 #define IN_SWITCH_STMT 1
1473 #define IN_ITERATION_STMT 2
1474 #define IN_OMP_BLOCK 4
1475 #define IN_OMP_FOR 8
1476 #define IN_IF_STMT 16
1477 unsigned char in_statement;
1479 /* TRUE if we are presently parsing the body of a switch statement.
1480 Note that this doesn't quite overlap with in_statement above.
1481 The difference relates to giving the right sets of error messages:
1482 "case not in switch" vs "break statement used with OpenMP...". */
1483 bool in_switch_statement_p;
1485 /* TRUE if we are parsing a type-id in an expression context. In
1486 such a situation, both "type (expr)" and "type (type)" are valid
1488 bool in_type_id_in_expr_p;
1490 /* TRUE if we are currently in a header file where declarations are
1491 implicitly extern "C". */
1492 bool implicit_extern_c;
1494 /* TRUE if strings in expressions should be translated to the execution
1496 bool translate_strings_p;
1498 /* TRUE if we are presently parsing the body of a function, but not
1500 bool in_function_body;
1502 /* If non-NULL, then we are parsing a construct where new type
1503 definitions are not permitted. The string stored here will be
1504 issued as an error message if a type is defined. */
1505 const char *type_definition_forbidden_message;
1507 /* A list of lists. The outer list is a stack, used for member
1508 functions of local classes. At each level there are two sub-list,
1509 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1510 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1511 TREE_VALUE's. The functions are chained in reverse declaration
1514 The TREE_PURPOSE sublist contains those functions with default
1515 arguments that need post processing, and the TREE_VALUE sublist
1516 contains those functions with definitions that need post
1519 These lists can only be processed once the outermost class being
1520 defined is complete. */
1521 tree unparsed_functions_queues;
1523 /* The number of classes whose definitions are currently in
1525 unsigned num_classes_being_defined;
1527 /* The number of template parameter lists that apply directly to the
1528 current declaration. */
1529 unsigned num_template_parameter_lists;
1534 /* Constructors and destructors. */
1536 static cp_parser *cp_parser_new
1539 /* Routines to parse various constructs.
1541 Those that return `tree' will return the error_mark_node (rather
1542 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1543 Sometimes, they will return an ordinary node if error-recovery was
1544 attempted, even though a parse error occurred. So, to check
1545 whether or not a parse error occurred, you should always use
1546 cp_parser_error_occurred. If the construct is optional (indicated
1547 either by an `_opt' in the name of the function that does the
1548 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1549 the construct is not present. */
1551 /* Lexical conventions [gram.lex] */
1553 static tree cp_parser_identifier
1555 static tree cp_parser_string_literal
1556 (cp_parser *, bool, bool);
1558 /* Basic concepts [gram.basic] */
1560 static bool cp_parser_translation_unit
1563 /* Expressions [gram.expr] */
1565 static tree cp_parser_primary_expression
1566 (cp_parser *, bool, bool, bool, cp_id_kind *);
1567 static tree cp_parser_id_expression
1568 (cp_parser *, bool, bool, bool *, bool, bool);
1569 static tree cp_parser_unqualified_id
1570 (cp_parser *, bool, bool, bool, bool);
1571 static tree cp_parser_nested_name_specifier_opt
1572 (cp_parser *, bool, bool, bool, bool);
1573 static tree cp_parser_nested_name_specifier
1574 (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_class_or_namespace_name
1576 (cp_parser *, bool, bool, bool, bool, bool);
1577 static tree cp_parser_postfix_expression
1578 (cp_parser *, bool, bool, bool);
1579 static tree cp_parser_postfix_open_square_expression
1580 (cp_parser *, tree, bool);
1581 static tree cp_parser_postfix_dot_deref_expression
1582 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1583 static tree cp_parser_parenthesized_expression_list
1584 (cp_parser *, bool, bool, bool, bool *);
1585 static void cp_parser_pseudo_destructor_name
1586 (cp_parser *, tree *, tree *);
1587 static tree cp_parser_unary_expression
1588 (cp_parser *, bool, bool);
1589 static enum tree_code cp_parser_unary_operator
1591 static tree cp_parser_new_expression
1593 static tree cp_parser_new_placement
1595 static tree cp_parser_new_type_id
1596 (cp_parser *, tree *);
1597 static cp_declarator *cp_parser_new_declarator_opt
1599 static cp_declarator *cp_parser_direct_new_declarator
1601 static tree cp_parser_new_initializer
1603 static tree cp_parser_delete_expression
1605 static tree cp_parser_cast_expression
1606 (cp_parser *, bool, bool);
1607 static tree cp_parser_binary_expression
1608 (cp_parser *, bool);
1609 static tree cp_parser_question_colon_clause
1610 (cp_parser *, tree);
1611 static tree cp_parser_assignment_expression
1612 (cp_parser *, bool);
1613 static enum tree_code cp_parser_assignment_operator_opt
1615 static tree cp_parser_expression
1616 (cp_parser *, bool);
1617 static tree cp_parser_constant_expression
1618 (cp_parser *, bool, bool *);
1619 static tree cp_parser_builtin_offsetof
1622 /* Statements [gram.stmt.stmt] */
1624 static void cp_parser_statement
1625 (cp_parser *, tree, bool, bool *);
1626 static void cp_parser_label_for_labeled_statement
1628 static tree cp_parser_expression_statement
1629 (cp_parser *, tree);
1630 static tree cp_parser_compound_statement
1631 (cp_parser *, tree, bool);
1632 static void cp_parser_statement_seq_opt
1633 (cp_parser *, tree);
1634 static tree cp_parser_selection_statement
1635 (cp_parser *, bool *);
1636 static tree cp_parser_condition
1638 static tree cp_parser_iteration_statement
1640 static void cp_parser_for_init_statement
1642 static tree cp_parser_jump_statement
1644 static void cp_parser_declaration_statement
1647 static tree cp_parser_implicitly_scoped_statement
1648 (cp_parser *, bool *);
1649 static void cp_parser_already_scoped_statement
1652 /* Declarations [gram.dcl.dcl] */
1654 static void cp_parser_declaration_seq_opt
1656 static void cp_parser_declaration
1658 static void cp_parser_block_declaration
1659 (cp_parser *, bool);
1660 static void cp_parser_simple_declaration
1661 (cp_parser *, bool);
1662 static void cp_parser_decl_specifier_seq
1663 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1664 static tree cp_parser_storage_class_specifier_opt
1666 static tree cp_parser_function_specifier_opt
1667 (cp_parser *, cp_decl_specifier_seq *);
1668 static tree cp_parser_type_specifier
1669 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1671 static tree cp_parser_simple_type_specifier
1672 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1673 static tree cp_parser_type_name
1675 static tree cp_parser_elaborated_type_specifier
1676 (cp_parser *, bool, bool);
1677 static tree cp_parser_enum_specifier
1679 static void cp_parser_enumerator_list
1680 (cp_parser *, tree);
1681 static void cp_parser_enumerator_definition
1682 (cp_parser *, tree);
1683 static tree cp_parser_namespace_name
1685 static void cp_parser_namespace_definition
1687 static void cp_parser_namespace_body
1689 static tree cp_parser_qualified_namespace_specifier
1691 static void cp_parser_namespace_alias_definition
1693 static bool cp_parser_using_declaration
1694 (cp_parser *, bool);
1695 static void cp_parser_using_directive
1697 static void cp_parser_asm_definition
1699 static void cp_parser_linkage_specification
1701 static void cp_parser_static_assert
1702 (cp_parser *, bool);
1703 static tree cp_parser_decltype
1706 /* Declarators [gram.dcl.decl] */
1708 static tree cp_parser_init_declarator
1709 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1710 static cp_declarator *cp_parser_declarator
1711 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1712 static cp_declarator *cp_parser_direct_declarator
1713 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1714 static enum tree_code cp_parser_ptr_operator
1715 (cp_parser *, tree *, cp_cv_quals *);
1716 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1718 static tree cp_parser_declarator_id
1719 (cp_parser *, bool);
1720 static tree cp_parser_type_id
1722 static void cp_parser_type_specifier_seq
1723 (cp_parser *, bool, cp_decl_specifier_seq *);
1724 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1726 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1727 (cp_parser *, bool *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration
1729 (cp_parser *, bool, bool *);
1730 static tree cp_parser_default_argument
1731 (cp_parser *, bool);
1732 static void cp_parser_function_body
1734 static tree cp_parser_initializer
1735 (cp_parser *, bool *, bool *);
1736 static tree cp_parser_initializer_clause
1737 (cp_parser *, bool *);
1738 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1739 (cp_parser *, bool *);
1741 static bool cp_parser_ctor_initializer_opt_and_function_body
1744 /* Classes [gram.class] */
1746 static tree cp_parser_class_name
1747 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1748 static tree cp_parser_class_specifier
1750 static tree cp_parser_class_head
1751 (cp_parser *, bool *, tree *, tree *);
1752 static enum tag_types cp_parser_class_key
1754 static void cp_parser_member_specification_opt
1756 static void cp_parser_member_declaration
1758 static tree cp_parser_pure_specifier
1760 static tree cp_parser_constant_initializer
1763 /* Derived classes [gram.class.derived] */
1765 static tree cp_parser_base_clause
1767 static tree cp_parser_base_specifier
1770 /* Special member functions [gram.special] */
1772 static tree cp_parser_conversion_function_id
1774 static tree cp_parser_conversion_type_id
1776 static cp_declarator *cp_parser_conversion_declarator_opt
1778 static bool cp_parser_ctor_initializer_opt
1780 static void cp_parser_mem_initializer_list
1782 static tree cp_parser_mem_initializer
1784 static tree cp_parser_mem_initializer_id
1787 /* Overloading [gram.over] */
1789 static tree cp_parser_operator_function_id
1791 static tree cp_parser_operator
1794 /* Templates [gram.temp] */
1796 static void cp_parser_template_declaration
1797 (cp_parser *, bool);
1798 static tree cp_parser_template_parameter_list
1800 static tree cp_parser_template_parameter
1801 (cp_parser *, bool *, bool *);
1802 static tree cp_parser_type_parameter
1803 (cp_parser *, bool *);
1804 static tree cp_parser_template_id
1805 (cp_parser *, bool, bool, bool);
1806 static tree cp_parser_template_name
1807 (cp_parser *, bool, bool, bool, bool *);
1808 static tree cp_parser_template_argument_list
1810 static tree cp_parser_template_argument
1812 static void cp_parser_explicit_instantiation
1814 static void cp_parser_explicit_specialization
1817 /* Exception handling [gram.exception] */
1819 static tree cp_parser_try_block
1821 static bool cp_parser_function_try_block
1823 static void cp_parser_handler_seq
1825 static void cp_parser_handler
1827 static tree cp_parser_exception_declaration
1829 static tree cp_parser_throw_expression
1831 static tree cp_parser_exception_specification_opt
1833 static tree cp_parser_type_id_list
1836 /* GNU Extensions */
1838 static tree cp_parser_asm_specification_opt
1840 static tree cp_parser_asm_operand_list
1842 static tree cp_parser_asm_clobber_list
1844 static tree cp_parser_attributes_opt
1846 static tree cp_parser_attribute_list
1848 static bool cp_parser_extension_opt
1849 (cp_parser *, int *);
1850 static void cp_parser_label_declaration
1853 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1854 static bool cp_parser_pragma
1855 (cp_parser *, enum pragma_context);
1857 /* Objective-C++ Productions */
1859 static tree cp_parser_objc_message_receiver
1861 static tree cp_parser_objc_message_args
1863 static tree cp_parser_objc_message_expression
1865 static tree cp_parser_objc_encode_expression
1867 static tree cp_parser_objc_defs_expression
1869 static tree cp_parser_objc_protocol_expression
1871 static tree cp_parser_objc_selector_expression
1873 static tree cp_parser_objc_expression
1875 static bool cp_parser_objc_selector_p
1877 static tree cp_parser_objc_selector
1879 static tree cp_parser_objc_protocol_refs_opt
1881 static void cp_parser_objc_declaration
1883 static tree cp_parser_objc_statement
1886 /* Utility Routines */
1888 static tree cp_parser_lookup_name
1889 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1890 static tree cp_parser_lookup_name_simple
1891 (cp_parser *, tree);
1892 static tree cp_parser_maybe_treat_template_as_class
1894 static bool cp_parser_check_declarator_template_parameters
1895 (cp_parser *, cp_declarator *);
1896 static bool cp_parser_check_template_parameters
1897 (cp_parser *, unsigned);
1898 static tree cp_parser_simple_cast_expression
1900 static tree cp_parser_global_scope_opt
1901 (cp_parser *, bool);
1902 static bool cp_parser_constructor_declarator_p
1903 (cp_parser *, bool);
1904 static tree cp_parser_function_definition_from_specifiers_and_declarator
1905 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1906 static tree cp_parser_function_definition_after_declarator
1907 (cp_parser *, bool);
1908 static void cp_parser_template_declaration_after_export
1909 (cp_parser *, bool);
1910 static void cp_parser_perform_template_parameter_access_checks
1911 (VEC (deferred_access_check,gc)*);
1912 static tree cp_parser_single_declaration
1913 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1914 static tree cp_parser_functional_cast
1915 (cp_parser *, tree);
1916 static tree cp_parser_save_member_function_body
1917 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1918 static tree cp_parser_enclosed_template_argument_list
1920 static void cp_parser_save_default_args
1921 (cp_parser *, tree);
1922 static void cp_parser_late_parsing_for_member
1923 (cp_parser *, tree);
1924 static void cp_parser_late_parsing_default_args
1925 (cp_parser *, tree);
1926 static tree cp_parser_sizeof_operand
1927 (cp_parser *, enum rid);
1928 static tree cp_parser_trait_expr
1929 (cp_parser *, enum rid);
1930 static bool cp_parser_declares_only_class_p
1932 static void cp_parser_set_storage_class
1933 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1934 static void cp_parser_set_decl_spec_type
1935 (cp_decl_specifier_seq *, tree, bool);
1936 static bool cp_parser_friend_p
1937 (const cp_decl_specifier_seq *);
1938 static cp_token *cp_parser_require
1939 (cp_parser *, enum cpp_ttype, const char *);
1940 static cp_token *cp_parser_require_keyword
1941 (cp_parser *, enum rid, const char *);
1942 static bool cp_parser_token_starts_function_definition_p
1944 static bool cp_parser_next_token_starts_class_definition_p
1946 static bool cp_parser_next_token_ends_template_argument_p
1948 static bool cp_parser_nth_token_starts_template_argument_list_p
1949 (cp_parser *, size_t);
1950 static enum tag_types cp_parser_token_is_class_key
1952 static void cp_parser_check_class_key
1953 (enum tag_types, tree type);
1954 static void cp_parser_check_access_in_redeclaration
1956 static bool cp_parser_optional_template_keyword
1958 static void cp_parser_pre_parsed_nested_name_specifier
1960 static void cp_parser_cache_group
1961 (cp_parser *, enum cpp_ttype, unsigned);
1962 static void cp_parser_parse_tentatively
1964 static void cp_parser_commit_to_tentative_parse
1966 static void cp_parser_abort_tentative_parse
1968 static bool cp_parser_parse_definitely
1970 static inline bool cp_parser_parsing_tentatively
1972 static bool cp_parser_uncommitted_to_tentative_parse_p
1974 static void cp_parser_error
1975 (cp_parser *, const char *);
1976 static void cp_parser_name_lookup_error
1977 (cp_parser *, tree, tree, const char *);
1978 static bool cp_parser_simulate_error
1980 static bool cp_parser_check_type_definition
1982 static void cp_parser_check_for_definition_in_return_type
1983 (cp_declarator *, tree);
1984 static void cp_parser_check_for_invalid_template_id
1985 (cp_parser *, tree);
1986 static bool cp_parser_non_integral_constant_expression
1987 (cp_parser *, const char *);
1988 static void cp_parser_diagnose_invalid_type_name
1989 (cp_parser *, tree, tree);
1990 static bool cp_parser_parse_and_diagnose_invalid_type_name
1992 static int cp_parser_skip_to_closing_parenthesis
1993 (cp_parser *, bool, bool, bool);
1994 static void cp_parser_skip_to_end_of_statement
1996 static void cp_parser_consume_semicolon_at_end_of_statement
1998 static void cp_parser_skip_to_end_of_block_or_statement
2000 static bool cp_parser_skip_to_closing_brace
2002 static void cp_parser_skip_to_end_of_template_parameter_list
2004 static void cp_parser_skip_to_pragma_eol
2005 (cp_parser*, cp_token *);
2006 static bool cp_parser_error_occurred
2008 static bool cp_parser_allow_gnu_extensions_p
2010 static bool cp_parser_is_string_literal
2012 static bool cp_parser_is_keyword
2013 (cp_token *, enum rid);
2014 static tree cp_parser_make_typename_type
2015 (cp_parser *, tree, tree);
2016 static cp_declarator * cp_parser_make_indirect_declarator
2017 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2019 /* Returns nonzero if we are parsing tentatively. */
2022 cp_parser_parsing_tentatively (cp_parser* parser)
2024 return parser->context->next != NULL;
2027 /* Returns nonzero if TOKEN is a string literal. */
2030 cp_parser_is_string_literal (cp_token* token)
2032 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2035 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2038 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2040 return token->keyword == keyword;
2043 /* If not parsing tentatively, issue a diagnostic of the form
2044 FILE:LINE: MESSAGE before TOKEN
2045 where TOKEN is the next token in the input stream. MESSAGE
2046 (specified by the caller) is usually of the form "expected
2050 cp_parser_error (cp_parser* parser, const char* message)
2052 if (!cp_parser_simulate_error (parser))
2054 cp_token *token = cp_lexer_peek_token (parser->lexer);
2055 /* This diagnostic makes more sense if it is tagged to the line
2056 of the token we just peeked at. */
2057 cp_lexer_set_source_position_from_token (token);
2059 if (token->type == CPP_PRAGMA)
2061 error ("%<#pragma%> is not allowed here");
2062 cp_parser_skip_to_pragma_eol (parser, token);
2066 c_parse_error (message,
2067 /* Because c_parser_error does not understand
2068 CPP_KEYWORD, keywords are treated like
2070 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2075 /* Issue an error about name-lookup failing. NAME is the
2076 IDENTIFIER_NODE DECL is the result of
2077 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2078 the thing that we hoped to find. */
2081 cp_parser_name_lookup_error (cp_parser* parser,
2084 const char* desired)
2086 /* If name lookup completely failed, tell the user that NAME was not
2088 if (decl == error_mark_node)
2090 if (parser->scope && parser->scope != global_namespace)
2091 error ("%<%E::%E%> has not been declared",
2092 parser->scope, name);
2093 else if (parser->scope == global_namespace)
2094 error ("%<::%E%> has not been declared", name);
2095 else if (parser->object_scope
2096 && !CLASS_TYPE_P (parser->object_scope))
2097 error ("request for member %qE in non-class type %qT",
2098 name, parser->object_scope);
2099 else if (parser->object_scope)
2100 error ("%<%T::%E%> has not been declared",
2101 parser->object_scope, name);
2103 error ("%qE has not been declared", name);
2105 else if (parser->scope && parser->scope != global_namespace)
2106 error ("%<%E::%E%> %s", parser->scope, name, desired);
2107 else if (parser->scope == global_namespace)
2108 error ("%<::%E%> %s", name, desired);
2110 error ("%qE %s", name, desired);
2113 /* If we are parsing tentatively, remember that an error has occurred
2114 during this tentative parse. Returns true if the error was
2115 simulated; false if a message should be issued by the caller. */
2118 cp_parser_simulate_error (cp_parser* parser)
2120 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2122 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2128 /* Check for repeated decl-specifiers. */
2131 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2135 for (ds = ds_first; ds != ds_last; ++ds)
2137 unsigned count = decl_specs->specs[(int)ds];
2140 /* The "long" specifier is a special case because of "long long". */
2144 error ("%<long long long%> is too long for GCC");
2145 else if (pedantic && !in_system_header && warn_long_long
2146 && cxx_dialect == cxx98)
2147 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2151 static const char *const decl_spec_names[] = {
2167 error ("duplicate %qs", decl_spec_names[(int)ds]);
2172 /* This function is called when a type is defined. If type
2173 definitions are forbidden at this point, an error message is
2177 cp_parser_check_type_definition (cp_parser* parser)
2179 /* If types are forbidden here, issue a message. */
2180 if (parser->type_definition_forbidden_message)
2182 /* Use `%s' to print the string in case there are any escape
2183 characters in the message. */
2184 error ("%s", parser->type_definition_forbidden_message);
2190 /* This function is called when the DECLARATOR is processed. The TYPE
2191 was a type defined in the decl-specifiers. If it is invalid to
2192 define a type in the decl-specifiers for DECLARATOR, an error is
2196 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2199 /* [dcl.fct] forbids type definitions in return types.
2200 Unfortunately, it's not easy to know whether or not we are
2201 processing a return type until after the fact. */
2203 && (declarator->kind == cdk_pointer
2204 || declarator->kind == cdk_reference
2205 || declarator->kind == cdk_ptrmem))
2206 declarator = declarator->declarator;
2208 && declarator->kind == cdk_function)
2210 error ("new types may not be defined in a return type");
2211 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2216 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2217 "<" in any valid C++ program. If the next token is indeed "<",
2218 issue a message warning the user about what appears to be an
2219 invalid attempt to form a template-id. */
2222 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2225 cp_token_position start = 0;
2227 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2230 error ("%qT is not a template", type);
2231 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2232 error ("%qE is not a template", type);
2234 error ("invalid template-id");
2235 /* Remember the location of the invalid "<". */
2236 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2237 start = cp_lexer_token_position (parser->lexer, true);
2238 /* Consume the "<". */
2239 cp_lexer_consume_token (parser->lexer);
2240 /* Parse the template arguments. */
2241 cp_parser_enclosed_template_argument_list (parser);
2242 /* Permanently remove the invalid template arguments so that
2243 this error message is not issued again. */
2245 cp_lexer_purge_tokens_after (parser->lexer, start);
2249 /* If parsing an integral constant-expression, issue an error message
2250 about the fact that THING appeared and return true. Otherwise,
2251 return false. In either case, set
2252 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2255 cp_parser_non_integral_constant_expression (cp_parser *parser,
2258 parser->non_integral_constant_expression_p = true;
2259 if (parser->integral_constant_expression_p)
2261 if (!parser->allow_non_integral_constant_expression_p)
2263 error ("%s cannot appear in a constant-expression", thing);
2270 /* Emit a diagnostic for an invalid type name. SCOPE is the
2271 qualifying scope (or NULL, if none) for ID. This function commits
2272 to the current active tentative parse, if any. (Otherwise, the
2273 problematic construct might be encountered again later, resulting
2274 in duplicate error messages.) */
2277 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2279 tree decl, old_scope;
2280 /* Try to lookup the identifier. */
2281 old_scope = parser->scope;
2282 parser->scope = scope;
2283 decl = cp_parser_lookup_name_simple (parser, id);
2284 parser->scope = old_scope;
2285 /* If the lookup found a template-name, it means that the user forgot
2286 to specify an argument list. Emit a useful error message. */
2287 if (TREE_CODE (decl) == TEMPLATE_DECL)
2288 error ("invalid use of template-name %qE without an argument list", decl);
2289 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2290 error ("invalid use of destructor %qD as a type", id);
2291 else if (TREE_CODE (decl) == TYPE_DECL)
2292 /* Something like 'unsigned A a;' */
2293 error ("invalid combination of multiple type-specifiers");
2294 else if (!parser->scope)
2296 /* Issue an error message. */
2297 error ("%qE does not name a type", id);
2298 /* If we're in a template class, it's possible that the user was
2299 referring to a type from a base class. For example:
2301 template <typename T> struct A { typedef T X; };
2302 template <typename T> struct B : public A<T> { X x; };
2304 The user should have said "typename A<T>::X". */
2305 if (processing_template_decl && current_class_type
2306 && TYPE_BINFO (current_class_type))
2310 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2314 tree base_type = BINFO_TYPE (b);
2315 if (CLASS_TYPE_P (base_type)
2316 && dependent_type_p (base_type))
2319 /* Go from a particular instantiation of the
2320 template (which will have an empty TYPE_FIELDs),
2321 to the main version. */
2322 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2323 for (field = TYPE_FIELDS (base_type);
2325 field = TREE_CHAIN (field))
2326 if (TREE_CODE (field) == TYPE_DECL
2327 && DECL_NAME (field) == id)
2329 inform ("(perhaps %<typename %T::%E%> was intended)",
2330 BINFO_TYPE (b), id);
2339 /* Here we diagnose qualified-ids where the scope is actually correct,
2340 but the identifier does not resolve to a valid type name. */
2341 else if (parser->scope != error_mark_node)
2343 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2344 error ("%qE in namespace %qE does not name a type",
2346 else if (TYPE_P (parser->scope))
2347 error ("%qE in class %qT does not name a type", id, parser->scope);
2351 cp_parser_commit_to_tentative_parse (parser);
2354 /* Check for a common situation where a type-name should be present,
2355 but is not, and issue a sensible error message. Returns true if an
2356 invalid type-name was detected.
2358 The situation handled by this function are variable declarations of the
2359 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2360 Usually, `ID' should name a type, but if we got here it means that it
2361 does not. We try to emit the best possible error message depending on
2362 how exactly the id-expression looks like. */
2365 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2369 cp_parser_parse_tentatively (parser);
2370 id = cp_parser_id_expression (parser,
2371 /*template_keyword_p=*/false,
2372 /*check_dependency_p=*/true,
2373 /*template_p=*/NULL,
2374 /*declarator_p=*/true,
2375 /*optional_p=*/false);
2376 /* After the id-expression, there should be a plain identifier,
2377 otherwise this is not a simple variable declaration. Also, if
2378 the scope is dependent, we cannot do much. */
2379 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2380 || (parser->scope && TYPE_P (parser->scope)
2381 && dependent_type_p (parser->scope))
2382 || TREE_CODE (id) == TYPE_DECL)
2384 cp_parser_abort_tentative_parse (parser);
2387 if (!cp_parser_parse_definitely (parser))
2390 /* Emit a diagnostic for the invalid type. */
2391 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2392 /* Skip to the end of the declaration; there's no point in
2393 trying to process it. */
2394 cp_parser_skip_to_end_of_block_or_statement (parser);
2398 /* Consume tokens up to, and including, the next non-nested closing `)'.
2399 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2400 are doing error recovery. Returns -1 if OR_COMMA is true and we
2401 found an unnested comma. */
2404 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2409 unsigned paren_depth = 0;
2410 unsigned brace_depth = 0;
2412 if (recovering && !or_comma
2413 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2418 cp_token * token = cp_lexer_peek_token (parser->lexer);
2420 switch (token->type)
2423 case CPP_PRAGMA_EOL:
2424 /* If we've run out of tokens, then there is no closing `)'. */
2428 /* This matches the processing in skip_to_end_of_statement. */
2433 case CPP_OPEN_BRACE:
2436 case CPP_CLOSE_BRACE:
2442 if (recovering && or_comma && !brace_depth && !paren_depth)
2446 case CPP_OPEN_PAREN:
2451 case CPP_CLOSE_PAREN:
2452 if (!brace_depth && !paren_depth--)
2455 cp_lexer_consume_token (parser->lexer);
2464 /* Consume the token. */
2465 cp_lexer_consume_token (parser->lexer);
2469 /* Consume tokens until we reach the end of the current statement.
2470 Normally, that will be just before consuming a `;'. However, if a
2471 non-nested `}' comes first, then we stop before consuming that. */
2474 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2476 unsigned nesting_depth = 0;
2480 cp_token *token = cp_lexer_peek_token (parser->lexer);
2482 switch (token->type)
2485 case CPP_PRAGMA_EOL:
2486 /* If we've run out of tokens, stop. */
2490 /* If the next token is a `;', we have reached the end of the
2496 case CPP_CLOSE_BRACE:
2497 /* If this is a non-nested '}', stop before consuming it.
2498 That way, when confronted with something like:
2502 we stop before consuming the closing '}', even though we
2503 have not yet reached a `;'. */
2504 if (nesting_depth == 0)
2507 /* If it is the closing '}' for a block that we have
2508 scanned, stop -- but only after consuming the token.
2514 we will stop after the body of the erroneously declared
2515 function, but before consuming the following `typedef'
2517 if (--nesting_depth == 0)
2519 cp_lexer_consume_token (parser->lexer);
2523 case CPP_OPEN_BRACE:
2531 /* Consume the token. */
2532 cp_lexer_consume_token (parser->lexer);
2536 /* This function is called at the end of a statement or declaration.
2537 If the next token is a semicolon, it is consumed; otherwise, error
2538 recovery is attempted. */
2541 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2543 /* Look for the trailing `;'. */
2544 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2546 /* If there is additional (erroneous) input, skip to the end of
2548 cp_parser_skip_to_end_of_statement (parser);
2549 /* If the next token is now a `;', consume it. */
2550 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2551 cp_lexer_consume_token (parser->lexer);
2555 /* Skip tokens until we have consumed an entire block, or until we
2556 have consumed a non-nested `;'. */
2559 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2561 int nesting_depth = 0;
2563 while (nesting_depth >= 0)
2565 cp_token *token = cp_lexer_peek_token (parser->lexer);
2567 switch (token->type)
2570 case CPP_PRAGMA_EOL:
2571 /* If we've run out of tokens, stop. */
2575 /* Stop if this is an unnested ';'. */
2580 case CPP_CLOSE_BRACE:
2581 /* Stop if this is an unnested '}', or closes the outermost
2588 case CPP_OPEN_BRACE:
2597 /* Consume the token. */
2598 cp_lexer_consume_token (parser->lexer);
2602 /* Skip tokens until a non-nested closing curly brace is the next
2603 token, or there are no more tokens. Return true in the first case,
2607 cp_parser_skip_to_closing_brace (cp_parser *parser)
2609 unsigned nesting_depth = 0;
2613 cp_token *token = cp_lexer_peek_token (parser->lexer);
2615 switch (token->type)
2618 case CPP_PRAGMA_EOL:
2619 /* If we've run out of tokens, stop. */
2622 case CPP_CLOSE_BRACE:
2623 /* If the next token is a non-nested `}', then we have reached
2624 the end of the current block. */
2625 if (nesting_depth-- == 0)
2629 case CPP_OPEN_BRACE:
2630 /* If it the next token is a `{', then we are entering a new
2631 block. Consume the entire block. */
2639 /* Consume the token. */
2640 cp_lexer_consume_token (parser->lexer);
2644 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2645 parameter is the PRAGMA token, allowing us to purge the entire pragma
2649 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2653 parser->lexer->in_pragma = false;
2656 token = cp_lexer_consume_token (parser->lexer);
2657 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2659 /* Ensure that the pragma is not parsed again. */
2660 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2663 /* Require pragma end of line, resyncing with it as necessary. The
2664 arguments are as for cp_parser_skip_to_pragma_eol. */
2667 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2669 parser->lexer->in_pragma = false;
2670 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2671 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2674 /* This is a simple wrapper around make_typename_type. When the id is
2675 an unresolved identifier node, we can provide a superior diagnostic
2676 using cp_parser_diagnose_invalid_type_name. */
2679 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2682 if (TREE_CODE (id) == IDENTIFIER_NODE)
2684 result = make_typename_type (scope, id, typename_type,
2685 /*complain=*/tf_none);
2686 if (result == error_mark_node)
2687 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2690 return make_typename_type (scope, id, typename_type, tf_error);
2693 /* This is a wrapper around the
2694 make_{pointer,ptrmem,reference}_declarator functions that decides
2695 which one to call based on the CODE and CLASS_TYPE arguments. The
2696 CODE argument should be one of the values returned by
2697 cp_parser_ptr_operator. */
2698 static cp_declarator *
2699 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2700 cp_cv_quals cv_qualifiers,
2701 cp_declarator *target)
2703 if (code == ERROR_MARK)
2704 return cp_error_declarator;
2706 if (code == INDIRECT_REF)
2707 if (class_type == NULL_TREE)
2708 return make_pointer_declarator (cv_qualifiers, target);
2710 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2711 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2712 return make_reference_declarator (cv_qualifiers, target, false);
2713 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2714 return make_reference_declarator (cv_qualifiers, target, true);
2718 /* Create a new C++ parser. */
2721 cp_parser_new (void)
2727 /* cp_lexer_new_main is called before calling ggc_alloc because
2728 cp_lexer_new_main might load a PCH file. */
2729 lexer = cp_lexer_new_main ();
2731 /* Initialize the binops_by_token so that we can get the tree
2732 directly from the token. */
2733 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2734 binops_by_token[binops[i].token_type] = binops[i];
2736 parser = GGC_CNEW (cp_parser);
2737 parser->lexer = lexer;
2738 parser->context = cp_parser_context_new (NULL);
2740 /* For now, we always accept GNU extensions. */
2741 parser->allow_gnu_extensions_p = 1;
2743 /* The `>' token is a greater-than operator, not the end of a
2745 parser->greater_than_is_operator_p = true;
2747 parser->default_arg_ok_p = true;
2749 /* We are not parsing a constant-expression. */
2750 parser->integral_constant_expression_p = false;
2751 parser->allow_non_integral_constant_expression_p = false;
2752 parser->non_integral_constant_expression_p = false;
2754 /* Local variable names are not forbidden. */
2755 parser->local_variables_forbidden_p = false;
2757 /* We are not processing an `extern "C"' declaration. */
2758 parser->in_unbraced_linkage_specification_p = false;
2760 /* We are not processing a declarator. */
2761 parser->in_declarator_p = false;
2763 /* We are not processing a template-argument-list. */
2764 parser->in_template_argument_list_p = false;
2766 /* We are not in an iteration statement. */
2767 parser->in_statement = 0;
2769 /* We are not in a switch statement. */
2770 parser->in_switch_statement_p = false;
2772 /* We are not parsing a type-id inside an expression. */
2773 parser->in_type_id_in_expr_p = false;
2775 /* Declarations aren't implicitly extern "C". */
2776 parser->implicit_extern_c = false;
2778 /* String literals should be translated to the execution character set. */
2779 parser->translate_strings_p = true;
2781 /* We are not parsing a function body. */
2782 parser->in_function_body = false;
2784 /* The unparsed function queue is empty. */
2785 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2787 /* There are no classes being defined. */
2788 parser->num_classes_being_defined = 0;
2790 /* No template parameters apply. */
2791 parser->num_template_parameter_lists = 0;
2796 /* Create a cp_lexer structure which will emit the tokens in CACHE
2797 and push it onto the parser's lexer stack. This is used for delayed
2798 parsing of in-class method bodies and default arguments, and should
2799 not be confused with tentative parsing. */
2801 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2803 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2804 lexer->next = parser->lexer;
2805 parser->lexer = lexer;
2807 /* Move the current source position to that of the first token in the
2809 cp_lexer_set_source_position_from_token (lexer->next_token);
2812 /* Pop the top lexer off the parser stack. This is never used for the
2813 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2815 cp_parser_pop_lexer (cp_parser *parser)
2817 cp_lexer *lexer = parser->lexer;
2818 parser->lexer = lexer->next;
2819 cp_lexer_destroy (lexer);
2821 /* Put the current source position back where it was before this
2822 lexer was pushed. */
2823 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2826 /* Lexical conventions [gram.lex] */
2828 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2832 cp_parser_identifier (cp_parser* parser)
2836 /* Look for the identifier. */
2837 token = cp_parser_require (parser, CPP_NAME, "identifier");
2838 /* Return the value. */
2839 return token ? token->u.value : error_mark_node;
2842 /* Parse a sequence of adjacent string constants. Returns a
2843 TREE_STRING representing the combined, nul-terminated string
2844 constant. If TRANSLATE is true, translate the string to the
2845 execution character set. If WIDE_OK is true, a wide string is
2848 C++98 [lex.string] says that if a narrow string literal token is
2849 adjacent to a wide string literal token, the behavior is undefined.
2850 However, C99 6.4.5p4 says that this results in a wide string literal.
2851 We follow C99 here, for consistency with the C front end.
2853 This code is largely lifted from lex_string() in c-lex.c.
2855 FUTURE: ObjC++ will need to handle @-strings here. */
2857 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2862 struct obstack str_ob;
2863 cpp_string str, istr, *strs;
2866 tok = cp_lexer_peek_token (parser->lexer);
2867 if (!cp_parser_is_string_literal (tok))
2869 cp_parser_error (parser, "expected string-literal");
2870 return error_mark_node;
2873 /* Try to avoid the overhead of creating and destroying an obstack
2874 for the common case of just one string. */
2875 if (!cp_parser_is_string_literal
2876 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2878 cp_lexer_consume_token (parser->lexer);
2880 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2881 str.len = TREE_STRING_LENGTH (tok->u.value);
2883 if (tok->type == CPP_WSTRING)
2890 gcc_obstack_init (&str_ob);
2895 cp_lexer_consume_token (parser->lexer);
2897 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2898 str.len = TREE_STRING_LENGTH (tok->u.value);
2899 if (tok->type == CPP_WSTRING)
2902 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2904 tok = cp_lexer_peek_token (parser->lexer);
2906 while (cp_parser_is_string_literal (tok));
2908 strs = (cpp_string *) obstack_finish (&str_ob);
2911 if (wide && !wide_ok)
2913 cp_parser_error (parser, "a wide string is invalid in this context");
2917 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2918 (parse_in, strs, count, &istr, wide))
2920 value = build_string (istr.len, (const char *)istr.text);
2921 free (CONST_CAST (unsigned char *, istr.text));
2923 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2924 value = fix_string_type (value);
2927 /* cpp_interpret_string has issued an error. */
2928 value = error_mark_node;
2931 obstack_free (&str_ob, 0);
2937 /* Basic concepts [gram.basic] */
2939 /* Parse a translation-unit.
2942 declaration-seq [opt]
2944 Returns TRUE if all went well. */
2947 cp_parser_translation_unit (cp_parser* parser)
2949 /* The address of the first non-permanent object on the declarator
2951 static void *declarator_obstack_base;
2955 /* Create the declarator obstack, if necessary. */
2956 if (!cp_error_declarator)
2958 gcc_obstack_init (&declarator_obstack);
2959 /* Create the error declarator. */
2960 cp_error_declarator = make_declarator (cdk_error);
2961 /* Create the empty parameter list. */
2962 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2963 /* Remember where the base of the declarator obstack lies. */
2964 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2967 cp_parser_declaration_seq_opt (parser);
2969 /* If there are no tokens left then all went well. */
2970 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2972 /* Get rid of the token array; we don't need it any more. */
2973 cp_lexer_destroy (parser->lexer);
2974 parser->lexer = NULL;
2976 /* This file might have been a context that's implicitly extern
2977 "C". If so, pop the lang context. (Only relevant for PCH.) */
2978 if (parser->implicit_extern_c)
2980 pop_lang_context ();
2981 parser->implicit_extern_c = false;
2985 finish_translation_unit ();
2991 cp_parser_error (parser, "expected declaration");
2995 /* Make sure the declarator obstack was fully cleaned up. */
2996 gcc_assert (obstack_next_free (&declarator_obstack)
2997 == declarator_obstack_base);
2999 /* All went well. */
3003 /* Expressions [gram.expr] */
3005 /* Parse a primary-expression.
3016 ( compound-statement )
3017 __builtin_va_arg ( assignment-expression , type-id )
3018 __builtin_offsetof ( type-id , offsetof-expression )
3021 __has_nothrow_assign ( type-id )
3022 __has_nothrow_constructor ( type-id )
3023 __has_nothrow_copy ( type-id )
3024 __has_trivial_assign ( type-id )
3025 __has_trivial_constructor ( type-id )
3026 __has_trivial_copy ( type-id )
3027 __has_trivial_destructor ( type-id )
3028 __has_virtual_destructor ( type-id )
3029 __is_abstract ( type-id )
3030 __is_base_of ( type-id , type-id )
3031 __is_class ( type-id )
3032 __is_convertible_to ( type-id , type-id )
3033 __is_empty ( type-id )
3034 __is_enum ( type-id )
3035 __is_pod ( type-id )
3036 __is_polymorphic ( type-id )
3037 __is_union ( type-id )
3039 Objective-C++ Extension:
3047 ADDRESS_P is true iff this expression was immediately preceded by
3048 "&" and therefore might denote a pointer-to-member. CAST_P is true
3049 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3050 true iff this expression is a template argument.
3052 Returns a representation of the expression. Upon return, *IDK
3053 indicates what kind of id-expression (if any) was present. */
3056 cp_parser_primary_expression (cp_parser *parser,
3059 bool template_arg_p,
3064 /* Assume the primary expression is not an id-expression. */
3065 *idk = CP_ID_KIND_NONE;
3067 /* Peek at the next token. */
3068 token = cp_lexer_peek_token (parser->lexer);
3069 switch (token->type)
3080 token = cp_lexer_consume_token (parser->lexer);
3081 /* Floating-point literals are only allowed in an integral
3082 constant expression if they are cast to an integral or
3083 enumeration type. */
3084 if (TREE_CODE (token->u.value) == REAL_CST
3085 && parser->integral_constant_expression_p
3088 /* CAST_P will be set even in invalid code like "int(2.7 +
3089 ...)". Therefore, we have to check that the next token
3090 is sure to end the cast. */
3093 cp_token *next_token;
3095 next_token = cp_lexer_peek_token (parser->lexer);
3096 if (/* The comma at the end of an
3097 enumerator-definition. */
3098 next_token->type != CPP_COMMA
3099 /* The curly brace at the end of an enum-specifier. */
3100 && next_token->type != CPP_CLOSE_BRACE
3101 /* The end of a statement. */
3102 && next_token->type != CPP_SEMICOLON
3103 /* The end of the cast-expression. */
3104 && next_token->type != CPP_CLOSE_PAREN
3105 /* The end of an array bound. */
3106 && next_token->type != CPP_CLOSE_SQUARE
3107 /* The closing ">" in a template-argument-list. */
3108 && (next_token->type != CPP_GREATER
3109 || parser->greater_than_is_operator_p)
3110 /* C++0x only: A ">>" treated like two ">" tokens,
3111 in a template-argument-list. */
3112 && (next_token->type != CPP_RSHIFT
3113 || (cxx_dialect == cxx98)
3114 || parser->greater_than_is_operator_p))
3118 /* If we are within a cast, then the constraint that the
3119 cast is to an integral or enumeration type will be
3120 checked at that point. If we are not within a cast, then
3121 this code is invalid. */
3123 cp_parser_non_integral_constant_expression
3124 (parser, "floating-point literal");
3126 return token->u.value;
3130 /* ??? Should wide strings be allowed when parser->translate_strings_p
3131 is false (i.e. in attributes)? If not, we can kill the third
3132 argument to cp_parser_string_literal. */
3133 return cp_parser_string_literal (parser,
3134 parser->translate_strings_p,
3137 case CPP_OPEN_PAREN:
3140 bool saved_greater_than_is_operator_p;
3142 /* Consume the `('. */
3143 cp_lexer_consume_token (parser->lexer);
3144 /* Within a parenthesized expression, a `>' token is always
3145 the greater-than operator. */
3146 saved_greater_than_is_operator_p
3147 = parser->greater_than_is_operator_p;
3148 parser->greater_than_is_operator_p = true;
3149 /* If we see `( { ' then we are looking at the beginning of
3150 a GNU statement-expression. */
3151 if (cp_parser_allow_gnu_extensions_p (parser)
3152 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3154 /* Statement-expressions are not allowed by the standard. */
3156 pedwarn ("ISO C++ forbids braced-groups within expressions");
3158 /* And they're not allowed outside of a function-body; you
3159 cannot, for example, write:
3161 int i = ({ int j = 3; j + 1; });
3163 at class or namespace scope. */
3164 if (!parser->in_function_body
3165 || parser->in_template_argument_list_p)
3167 error ("statement-expressions are not allowed outside "
3168 "functions nor in template-argument lists");
3169 cp_parser_skip_to_end_of_block_or_statement (parser);
3170 expr = error_mark_node;
3174 /* Start the statement-expression. */
3175 expr = begin_stmt_expr ();
3176 /* Parse the compound-statement. */
3177 cp_parser_compound_statement (parser, expr, false);
3179 expr = finish_stmt_expr (expr, false);
3184 /* Parse the parenthesized expression. */
3185 expr = cp_parser_expression (parser, cast_p);
3186 /* Let the front end know that this expression was
3187 enclosed in parentheses. This matters in case, for
3188 example, the expression is of the form `A::B', since
3189 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3191 finish_parenthesized_expr (expr);
3193 /* The `>' token might be the end of a template-id or
3194 template-parameter-list now. */
3195 parser->greater_than_is_operator_p
3196 = saved_greater_than_is_operator_p;
3197 /* Consume the `)'. */
3198 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3199 cp_parser_skip_to_end_of_statement (parser);
3205 switch (token->keyword)
3207 /* These two are the boolean literals. */
3209 cp_lexer_consume_token (parser->lexer);
3210 return boolean_true_node;
3212 cp_lexer_consume_token (parser->lexer);
3213 return boolean_false_node;
3215 /* The `__null' literal. */
3217 cp_lexer_consume_token (parser->lexer);
3220 /* Recognize the `this' keyword. */
3222 cp_lexer_consume_token (parser->lexer);
3223 if (parser->local_variables_forbidden_p)
3225 error ("%<this%> may not be used in this context");
3226 return error_mark_node;
3228 /* Pointers cannot appear in constant-expressions. */
3229 if (cp_parser_non_integral_constant_expression (parser,
3231 return error_mark_node;
3232 return finish_this_expr ();
3234 /* The `operator' keyword can be the beginning of an
3239 case RID_FUNCTION_NAME:
3240 case RID_PRETTY_FUNCTION_NAME:
3241 case RID_C99_FUNCTION_NAME:
3242 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3243 __func__ are the names of variables -- but they are
3244 treated specially. Therefore, they are handled here,
3245 rather than relying on the generic id-expression logic
3246 below. Grammatically, these names are id-expressions.
3248 Consume the token. */
3249 token = cp_lexer_consume_token (parser->lexer);
3250 /* Look up the name. */
3251 return finish_fname (token->u.value);
3258 /* The `__builtin_va_arg' construct is used to handle
3259 `va_arg'. Consume the `__builtin_va_arg' token. */
3260 cp_lexer_consume_token (parser->lexer);
3261 /* Look for the opening `('. */
3262 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3263 /* Now, parse the assignment-expression. */
3264 expression = cp_parser_assignment_expression (parser,
3266 /* Look for the `,'. */
3267 cp_parser_require (parser, CPP_COMMA, "`,'");
3268 /* Parse the type-id. */
3269 type = cp_parser_type_id (parser);
3270 /* Look for the closing `)'. */
3271 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3272 /* Using `va_arg' in a constant-expression is not
3274 if (cp_parser_non_integral_constant_expression (parser,
3276 return error_mark_node;
3277 return build_x_va_arg (expression, type);
3281 return cp_parser_builtin_offsetof (parser);
3283 case RID_HAS_NOTHROW_ASSIGN:
3284 case RID_HAS_NOTHROW_CONSTRUCTOR:
3285 case RID_HAS_NOTHROW_COPY:
3286 case RID_HAS_TRIVIAL_ASSIGN:
3287 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3288 case RID_HAS_TRIVIAL_COPY:
3289 case RID_HAS_TRIVIAL_DESTRUCTOR:
3290 case RID_HAS_VIRTUAL_DESTRUCTOR:
3291 case RID_IS_ABSTRACT:
3292 case RID_IS_BASE_OF:
3294 case RID_IS_CONVERTIBLE_TO:
3298 case RID_IS_POLYMORPHIC:
3300 return cp_parser_trait_expr (parser, token->keyword);
3302 /* Objective-C++ expressions. */
3304 case RID_AT_PROTOCOL:
3305 case RID_AT_SELECTOR:
3306 return cp_parser_objc_expression (parser);
3309 cp_parser_error (parser, "expected primary-expression");
3310 return error_mark_node;
3313 /* An id-expression can start with either an identifier, a
3314 `::' as the beginning of a qualified-id, or the "operator"
3318 case CPP_TEMPLATE_ID:
3319 case CPP_NESTED_NAME_SPECIFIER:
3323 const char *error_msg;
3328 /* Parse the id-expression. */
3330 = cp_parser_id_expression (parser,
3331 /*template_keyword_p=*/false,
3332 /*check_dependency_p=*/true,
3334 /*declarator_p=*/false,
3335 /*optional_p=*/false);
3336 if (id_expression == error_mark_node)
3337 return error_mark_node;
3338 token = cp_lexer_peek_token (parser->lexer);
3339 done = (token->type != CPP_OPEN_SQUARE
3340 && token->type != CPP_OPEN_PAREN
3341 && token->type != CPP_DOT
3342 && token->type != CPP_DEREF
3343 && token->type != CPP_PLUS_PLUS
3344 && token->type != CPP_MINUS_MINUS);
3345 /* If we have a template-id, then no further lookup is
3346 required. If the template-id was for a template-class, we
3347 will sometimes have a TYPE_DECL at this point. */
3348 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3349 || TREE_CODE (id_expression) == TYPE_DECL)
3350 decl = id_expression;
3351 /* Look up the name. */
3354 tree ambiguous_decls;
3356 decl = cp_parser_lookup_name (parser, id_expression,
3359 /*is_namespace=*/false,
3360 /*check_dependency=*/true,
3362 /* If the lookup was ambiguous, an error will already have
3364 if (ambiguous_decls)
3365 return error_mark_node;
3367 /* In Objective-C++, an instance variable (ivar) may be preferred
3368 to whatever cp_parser_lookup_name() found. */
3369 decl = objc_lookup_ivar (decl, id_expression);
3371 /* If name lookup gives us a SCOPE_REF, then the
3372 qualifying scope was dependent. */
3373 if (TREE_CODE (decl) == SCOPE_REF)
3375 /* At this point, we do not know if DECL is a valid
3376 integral constant expression. We assume that it is
3377 in fact such an expression, so that code like:
3379 template <int N> struct A {
3383 is accepted. At template-instantiation time, we
3384 will check that B<N>::i is actually a constant. */
3387 /* Check to see if DECL is a local variable in a context
3388 where that is forbidden. */
3389 if (parser->local_variables_forbidden_p
3390 && local_variable_p (decl))
3392 /* It might be that we only found DECL because we are
3393 trying to be generous with pre-ISO scoping rules.
3394 For example, consider:
3398 for (int i = 0; i < 10; ++i) {}
3399 extern void f(int j = i);
3402 Here, name look up will originally find the out
3403 of scope `i'. We need to issue a warning message,
3404 but then use the global `i'. */
3405 decl = check_for_out_of_scope_variable (decl);
3406 if (local_variable_p (decl))
3408 error ("local variable %qD may not appear in this context",
3410 return error_mark_node;
3415 decl = (finish_id_expression
3416 (id_expression, decl, parser->scope,
3418 parser->integral_constant_expression_p,
3419 parser->allow_non_integral_constant_expression_p,
3420 &parser->non_integral_constant_expression_p,
3421 template_p, done, address_p,
3425 cp_parser_error (parser, error_msg);
3429 /* Anything else is an error. */
3431 /* ...unless we have an Objective-C++ message or string literal,
3433 if (c_dialect_objc ()
3434 && (token->type == CPP_OPEN_SQUARE
3435 || token->type == CPP_OBJC_STRING))
3436 return cp_parser_objc_expression (parser);
3438 cp_parser_error (parser, "expected primary-expression");
3439 return error_mark_node;
3443 /* Parse an id-expression.
3450 :: [opt] nested-name-specifier template [opt] unqualified-id
3452 :: operator-function-id
3455 Return a representation of the unqualified portion of the
3456 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3457 a `::' or nested-name-specifier.
3459 Often, if the id-expression was a qualified-id, the caller will
3460 want to make a SCOPE_REF to represent the qualified-id. This
3461 function does not do this in order to avoid wastefully creating
3462 SCOPE_REFs when they are not required.
3464 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3467 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3468 uninstantiated templates.
3470 If *TEMPLATE_P is non-NULL, it is set to true iff the
3471 `template' keyword is used to explicitly indicate that the entity
3472 named is a template.
3474 If DECLARATOR_P is true, the id-expression is appearing as part of
3475 a declarator, rather than as part of an expression. */
3478 cp_parser_id_expression (cp_parser *parser,
3479 bool template_keyword_p,
3480 bool check_dependency_p,
3485 bool global_scope_p;
3486 bool nested_name_specifier_p;
3488 /* Assume the `template' keyword was not used. */
3490 *template_p = template_keyword_p;
3492 /* Look for the optional `::' operator. */
3494 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3496 /* Look for the optional nested-name-specifier. */
3497 nested_name_specifier_p
3498 = (cp_parser_nested_name_specifier_opt (parser,
3499 /*typename_keyword_p=*/false,
3504 /* If there is a nested-name-specifier, then we are looking at
3505 the first qualified-id production. */
3506 if (nested_name_specifier_p)
3509 tree saved_object_scope;
3510 tree saved_qualifying_scope;
3511 tree unqualified_id;
3514 /* See if the next token is the `template' keyword. */
3516 template_p = &is_template;
3517 *template_p = cp_parser_optional_template_keyword (parser);
3518 /* Name lookup we do during the processing of the
3519 unqualified-id might obliterate SCOPE. */
3520 saved_scope = parser->scope;
3521 saved_object_scope = parser->object_scope;
3522 saved_qualifying_scope = parser->qualifying_scope;
3523 /* Process the final unqualified-id. */
3524 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3527 /*optional_p=*/false);
3528 /* Restore the SAVED_SCOPE for our caller. */
3529 parser->scope = saved_scope;
3530 parser->object_scope = saved_object_scope;
3531 parser->qualifying_scope = saved_qualifying_scope;
3533 return unqualified_id;
3535 /* Otherwise, if we are in global scope, then we are looking at one
3536 of the other qualified-id productions. */
3537 else if (global_scope_p)
3542 /* Peek at the next token. */
3543 token = cp_lexer_peek_token (parser->lexer);
3545 /* If it's an identifier, and the next token is not a "<", then
3546 we can avoid the template-id case. This is an optimization
3547 for this common case. */
3548 if (token->type == CPP_NAME
3549 && !cp_parser_nth_token_starts_template_argument_list_p
3551 return cp_parser_identifier (parser);
3553 cp_parser_parse_tentatively (parser);
3554 /* Try a template-id. */
3555 id = cp_parser_template_id (parser,
3556 /*template_keyword_p=*/false,
3557 /*check_dependency_p=*/true,
3559 /* If that worked, we're done. */
3560 if (cp_parser_parse_definitely (parser))
3563 /* Peek at the next token. (Changes in the token buffer may
3564 have invalidated the pointer obtained above.) */
3565 token = cp_lexer_peek_token (parser->lexer);
3567 switch (token->type)
3570 return cp_parser_identifier (parser);
3573 if (token->keyword == RID_OPERATOR)
3574 return cp_parser_operator_function_id (parser);
3578 cp_parser_error (parser, "expected id-expression");
3579 return error_mark_node;
3583 return cp_parser_unqualified_id (parser, template_keyword_p,
3584 /*check_dependency_p=*/true,
3589 /* Parse an unqualified-id.
3593 operator-function-id
3594 conversion-function-id
3598 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3599 keyword, in a construct like `A::template ...'.
3601 Returns a representation of unqualified-id. For the `identifier'
3602 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3603 production a BIT_NOT_EXPR is returned; the operand of the
3604 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3605 other productions, see the documentation accompanying the
3606 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3607 names are looked up in uninstantiated templates. If DECLARATOR_P
3608 is true, the unqualified-id is appearing as part of a declarator,
3609 rather than as part of an expression. */
3612 cp_parser_unqualified_id (cp_parser* parser,
3613 bool template_keyword_p,
3614 bool check_dependency_p,
3620 /* Peek at the next token. */
3621 token = cp_lexer_peek_token (parser->lexer);
3623 switch (token->type)
3629 /* We don't know yet whether or not this will be a
3631 cp_parser_parse_tentatively (parser);
3632 /* Try a template-id. */
3633 id = cp_parser_template_id (parser, template_keyword_p,
3636 /* If it worked, we're done. */
3637 if (cp_parser_parse_definitely (parser))
3639 /* Otherwise, it's an ordinary identifier. */
3640 return cp_parser_identifier (parser);
3643 case CPP_TEMPLATE_ID:
3644 return cp_parser_template_id (parser, template_keyword_p,
3651 tree qualifying_scope;
3656 /* Consume the `~' token. */
3657 cp_lexer_consume_token (parser->lexer);
3658 /* Parse the class-name. The standard, as written, seems to
3661 template <typename T> struct S { ~S (); };
3662 template <typename T> S<T>::~S() {}
3664 is invalid, since `~' must be followed by a class-name, but
3665 `S<T>' is dependent, and so not known to be a class.
3666 That's not right; we need to look in uninstantiated
3667 templates. A further complication arises from:
3669 template <typename T> void f(T t) {
3673 Here, it is not possible to look up `T' in the scope of `T'
3674 itself. We must look in both the current scope, and the
3675 scope of the containing complete expression.
3677 Yet another issue is:
3686 The standard does not seem to say that the `S' in `~S'
3687 should refer to the type `S' and not the data member
3690 /* DR 244 says that we look up the name after the "~" in the
3691 same scope as we looked up the qualifying name. That idea
3692 isn't fully worked out; it's more complicated than that. */
3693 scope = parser->scope;
3694 object_scope = parser->object_scope;
3695 qualifying_scope = parser->qualifying_scope;
3697 /* Check for invalid scopes. */
3698 if (scope == error_mark_node)
3700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3701 cp_lexer_consume_token (parser->lexer);
3702 return error_mark_node;
3704 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3706 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3707 error ("scope %qT before %<~%> is not a class-name", scope);
3708 cp_parser_simulate_error (parser);
3709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3710 cp_lexer_consume_token (parser->lexer);
3711 return error_mark_node;
3713 gcc_assert (!scope || TYPE_P (scope));
3715 /* If the name is of the form "X::~X" it's OK. */
3716 token = cp_lexer_peek_token (parser->lexer);
3718 && token->type == CPP_NAME
3719 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3721 && constructor_name_p (token->u.value, scope))
3723 cp_lexer_consume_token (parser->lexer);
3724 return build_nt (BIT_NOT_EXPR, scope);
3727 /* If there was an explicit qualification (S::~T), first look
3728 in the scope given by the qualification (i.e., S). */
3730 type_decl = NULL_TREE;
3733 cp_parser_parse_tentatively (parser);
3734 type_decl = cp_parser_class_name (parser,
3735 /*typename_keyword_p=*/false,
3736 /*template_keyword_p=*/false,
3738 /*check_dependency=*/false,
3739 /*class_head_p=*/false,
3741 if (cp_parser_parse_definitely (parser))
3744 /* In "N::S::~S", look in "N" as well. */
3745 if (!done && scope && qualifying_scope)
3747 cp_parser_parse_tentatively (parser);
3748 parser->scope = qualifying_scope;
3749 parser->object_scope = NULL_TREE;
3750 parser->qualifying_scope = NULL_TREE;
3752 = cp_parser_class_name (parser,
3753 /*typename_keyword_p=*/false,
3754 /*template_keyword_p=*/false,
3756 /*check_dependency=*/false,
3757 /*class_head_p=*/false,
3759 if (cp_parser_parse_definitely (parser))
3762 /* In "p->S::~T", look in the scope given by "*p" as well. */
3763 else if (!done && object_scope)
3765 cp_parser_parse_tentatively (parser);
3766 parser->scope = object_scope;
3767 parser->object_scope = NULL_TREE;
3768 parser->qualifying_scope = NULL_TREE;
3770 = cp_parser_class_name (parser,
3771 /*typename_keyword_p=*/false,
3772 /*template_keyword_p=*/false,
3774 /*check_dependency=*/false,
3775 /*class_head_p=*/false,
3777 if (cp_parser_parse_definitely (parser))
3780 /* Look in the surrounding context. */
3783 parser->scope = NULL_TREE;
3784 parser->object_scope = NULL_TREE;
3785 parser->qualifying_scope = NULL_TREE;
3787 = cp_parser_class_name (parser,
3788 /*typename_keyword_p=*/false,
3789 /*template_keyword_p=*/false,
3791 /*check_dependency=*/false,
3792 /*class_head_p=*/false,
3795 /* If an error occurred, assume that the name of the
3796 destructor is the same as the name of the qualifying
3797 class. That allows us to keep parsing after running
3798 into ill-formed destructor names. */
3799 if (type_decl == error_mark_node && scope)
3800 return build_nt (BIT_NOT_EXPR, scope);
3801 else if (type_decl == error_mark_node)
3802 return error_mark_node;
3804 /* Check that destructor name and scope match. */
3805 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3807 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3808 error ("declaration of %<~%T%> as member of %qT",
3810 cp_parser_simulate_error (parser);
3811 return error_mark_node;
3816 A typedef-name that names a class shall not be used as the
3817 identifier in the declarator for a destructor declaration. */
3819 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3820 && !DECL_SELF_REFERENCE_P (type_decl)
3821 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3822 error ("typedef-name %qD used as destructor declarator",
3825 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3829 if (token->keyword == RID_OPERATOR)
3833 /* This could be a template-id, so we try that first. */
3834 cp_parser_parse_tentatively (parser);
3835 /* Try a template-id. */
3836 id = cp_parser_template_id (parser, template_keyword_p,
3837 /*check_dependency_p=*/true,
3839 /* If that worked, we're done. */
3840 if (cp_parser_parse_definitely (parser))
3842 /* We still don't know whether we're looking at an
3843 operator-function-id or a conversion-function-id. */
3844 cp_parser_parse_tentatively (parser);
3845 /* Try an operator-function-id. */
3846 id = cp_parser_operator_function_id (parser);
3847 /* If that didn't work, try a conversion-function-id. */
3848 if (!cp_parser_parse_definitely (parser))
3849 id = cp_parser_conversion_function_id (parser);
3858 cp_parser_error (parser, "expected unqualified-id");
3859 return error_mark_node;
3863 /* Parse an (optional) nested-name-specifier.
3865 nested-name-specifier:
3866 class-or-namespace-name :: nested-name-specifier [opt]
3867 class-or-namespace-name :: template nested-name-specifier [opt]
3869 PARSER->SCOPE should be set appropriately before this function is
3870 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3871 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3874 Sets PARSER->SCOPE to the class (TYPE) or namespace
3875 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3876 it unchanged if there is no nested-name-specifier. Returns the new
3877 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3879 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3880 part of a declaration and/or decl-specifier. */
3883 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3884 bool typename_keyword_p,
3885 bool check_dependency_p,
3887 bool is_declaration)
3889 bool success = false;
3890 cp_token_position start = 0;
3893 /* Remember where the nested-name-specifier starts. */
3894 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3896 start = cp_lexer_token_position (parser->lexer, false);
3897 push_deferring_access_checks (dk_deferred);
3904 tree saved_qualifying_scope;
3905 bool template_keyword_p;
3907 /* Spot cases that cannot be the beginning of a
3908 nested-name-specifier. */
3909 token = cp_lexer_peek_token (parser->lexer);
3911 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3912 the already parsed nested-name-specifier. */
3913 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3915 /* Grab the nested-name-specifier and continue the loop. */
3916 cp_parser_pre_parsed_nested_name_specifier (parser);
3917 /* If we originally encountered this nested-name-specifier
3918 with IS_DECLARATION set to false, we will not have
3919 resolved TYPENAME_TYPEs, so we must do so here. */
3921 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3923 new_scope = resolve_typename_type (parser->scope,
3924 /*only_current_p=*/false);
3925 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3926 parser->scope = new_scope;
3932 /* Spot cases that cannot be the beginning of a
3933 nested-name-specifier. On the second and subsequent times
3934 through the loop, we look for the `template' keyword. */
3935 if (success && token->keyword == RID_TEMPLATE)
3937 /* A template-id can start a nested-name-specifier. */
3938 else if (token->type == CPP_TEMPLATE_ID)
3942 /* If the next token is not an identifier, then it is
3943 definitely not a class-or-namespace-name. */
3944 if (token->type != CPP_NAME)
3946 /* If the following token is neither a `<' (to begin a
3947 template-id), nor a `::', then we are not looking at a
3948 nested-name-specifier. */
3949 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3950 if (token->type != CPP_SCOPE
3951 && !cp_parser_nth_token_starts_template_argument_list_p
3956 /* The nested-name-specifier is optional, so we parse
3958 cp_parser_parse_tentatively (parser);
3960 /* Look for the optional `template' keyword, if this isn't the
3961 first time through the loop. */
3963 template_keyword_p = cp_parser_optional_template_keyword (parser);
3965 template_keyword_p = false;
3967 /* Save the old scope since the name lookup we are about to do
3968 might destroy it. */
3969 old_scope = parser->scope;
3970 saved_qualifying_scope = parser->qualifying_scope;
3971 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3972 look up names in "X<T>::I" in order to determine that "Y" is
3973 a template. So, if we have a typename at this point, we make
3974 an effort to look through it. */
3976 && !typename_keyword_p
3978 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3979 parser->scope = resolve_typename_type (parser->scope,
3980 /*only_current_p=*/false);
3981 /* Parse the qualifying entity. */
3983 = cp_parser_class_or_namespace_name (parser,
3989 /* Look for the `::' token. */
3990 cp_parser_require (parser, CPP_SCOPE, "`::'");
3992 /* If we found what we wanted, we keep going; otherwise, we're
3994 if (!cp_parser_parse_definitely (parser))
3996 bool error_p = false;
3998 /* Restore the OLD_SCOPE since it was valid before the
3999 failed attempt at finding the last
4000 class-or-namespace-name. */
4001 parser->scope = old_scope;
4002 parser->qualifying_scope = saved_qualifying_scope;
4003 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4005 /* If the next token is an identifier, and the one after
4006 that is a `::', then any valid interpretation would have
4007 found a class-or-namespace-name. */
4008 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4009 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4011 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4014 token = cp_lexer_consume_token (parser->lexer);
4017 if (!token->ambiguous_p)
4020 tree ambiguous_decls;
4022 decl = cp_parser_lookup_name (parser, token->u.value,
4024 /*is_template=*/false,
4025 /*is_namespace=*/false,
4026 /*check_dependency=*/true,
4028 if (TREE_CODE (decl) == TEMPLATE_DECL)
4029 error ("%qD used without template parameters", decl);
4030 else if (ambiguous_decls)
4032 error ("reference to %qD is ambiguous",
4034 print_candidates (ambiguous_decls);
4035 decl = error_mark_node;
4038 cp_parser_name_lookup_error
4039 (parser, token->u.value, decl,
4040 "is not a class or namespace");
4042 parser->scope = error_mark_node;
4044 /* Treat this as a successful nested-name-specifier
4049 If the name found is not a class-name (clause
4050 _class_) or namespace-name (_namespace.def_), the
4051 program is ill-formed. */
4054 cp_lexer_consume_token (parser->lexer);
4058 /* We've found one valid nested-name-specifier. */
4060 /* Name lookup always gives us a DECL. */
4061 if (TREE_CODE (new_scope) == TYPE_DECL)
4062 new_scope = TREE_TYPE (new_scope);
4063 /* Uses of "template" must be followed by actual templates. */
4064 if (template_keyword_p
4065 && !(CLASS_TYPE_P (new_scope)
4066 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4067 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4068 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4069 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4070 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4071 == TEMPLATE_ID_EXPR)))
4072 pedwarn (TYPE_P (new_scope)
4073 ? "%qT is not a template"
4074 : "%qD is not a template",
4076 /* If it is a class scope, try to complete it; we are about to
4077 be looking up names inside the class. */
4078 if (TYPE_P (new_scope)
4079 /* Since checking types for dependency can be expensive,
4080 avoid doing it if the type is already complete. */
4081 && !COMPLETE_TYPE_P (new_scope)
4082 /* Do not try to complete dependent types. */
4083 && !dependent_type_p (new_scope))
4085 new_scope = complete_type (new_scope);
4086 /* If it is a typedef to current class, use the current
4087 class instead, as the typedef won't have any names inside
4089 if (!COMPLETE_TYPE_P (new_scope)
4090 && currently_open_class (new_scope))
4091 new_scope = TYPE_MAIN_VARIANT (new_scope);
4093 /* Make sure we look in the right scope the next time through
4095 parser->scope = new_scope;
4098 /* If parsing tentatively, replace the sequence of tokens that makes
4099 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4100 token. That way, should we re-parse the token stream, we will
4101 not have to repeat the effort required to do the parse, nor will
4102 we issue duplicate error messages. */
4103 if (success && start)
4107 token = cp_lexer_token_at (parser->lexer, start);
4108 /* Reset the contents of the START token. */
4109 token->type = CPP_NESTED_NAME_SPECIFIER;
4110 /* Retrieve any deferred checks. Do not pop this access checks yet
4111 so the memory will not be reclaimed during token replacing below. */
4112 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4113 token->u.tree_check_value->value = parser->scope;
4114 token->u.tree_check_value->checks = get_deferred_access_checks ();
4115 token->u.tree_check_value->qualifying_scope =
4116 parser->qualifying_scope;
4117 token->keyword = RID_MAX;
4119 /* Purge all subsequent tokens. */
4120 cp_lexer_purge_tokens_after (parser->lexer, start);
4124 pop_to_parent_deferring_access_checks ();
4126 return success ? parser->scope : NULL_TREE;
4129 /* Parse a nested-name-specifier. See
4130 cp_parser_nested_name_specifier_opt for details. This function
4131 behaves identically, except that it will an issue an error if no
4132 nested-name-specifier is present. */
4135 cp_parser_nested_name_specifier (cp_parser *parser,
4136 bool typename_keyword_p,
4137 bool check_dependency_p,
4139 bool is_declaration)
4143 /* Look for the nested-name-specifier. */
4144 scope = cp_parser_nested_name_specifier_opt (parser,
4149 /* If it was not present, issue an error message. */
4152 cp_parser_error (parser, "expected nested-name-specifier");
4153 parser->scope = NULL_TREE;
4159 /* Parse a class-or-namespace-name.
4161 class-or-namespace-name:
4165 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4166 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4167 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4168 TYPE_P is TRUE iff the next name should be taken as a class-name,
4169 even the same name is declared to be another entity in the same
4172 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4173 specified by the class-or-namespace-name. If neither is found the
4174 ERROR_MARK_NODE is returned. */
4177 cp_parser_class_or_namespace_name (cp_parser *parser,
4178 bool typename_keyword_p,
4179 bool template_keyword_p,
4180 bool check_dependency_p,
4182 bool is_declaration)
4185 tree saved_qualifying_scope;
4186 tree saved_object_scope;
4190 /* Before we try to parse the class-name, we must save away the
4191 current PARSER->SCOPE since cp_parser_class_name will destroy
4193 saved_scope = parser->scope;
4194 saved_qualifying_scope = parser->qualifying_scope;
4195 saved_object_scope = parser->object_scope;
4196 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4197 there is no need to look for a namespace-name. */
4198 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4200 cp_parser_parse_tentatively (parser);
4201 scope = cp_parser_class_name (parser,
4204 type_p ? class_type : none_type,
4206 /*class_head_p=*/false,
4208 /* If that didn't work, try for a namespace-name. */
4209 if (!only_class_p && !cp_parser_parse_definitely (parser))
4211 /* Restore the saved scope. */
4212 parser->scope = saved_scope;
4213 parser->qualifying_scope = saved_qualifying_scope;
4214 parser->object_scope = saved_object_scope;
4215 /* If we are not looking at an identifier followed by the scope
4216 resolution operator, then this is not part of a
4217 nested-name-specifier. (Note that this function is only used
4218 to parse the components of a nested-name-specifier.) */
4219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4220 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4221 return error_mark_node;
4222 scope = cp_parser_namespace_name (parser);
4228 /* Parse a postfix-expression.
4232 postfix-expression [ expression ]
4233 postfix-expression ( expression-list [opt] )
4234 simple-type-specifier ( expression-list [opt] )
4235 typename :: [opt] nested-name-specifier identifier
4236 ( expression-list [opt] )
4237 typename :: [opt] nested-name-specifier template [opt] template-id
4238 ( expression-list [opt] )
4239 postfix-expression . template [opt] id-expression
4240 postfix-expression -> template [opt] id-expression
4241 postfix-expression . pseudo-destructor-name
4242 postfix-expression -> pseudo-destructor-name
4243 postfix-expression ++
4244 postfix-expression --
4245 dynamic_cast < type-id > ( expression )
4246 static_cast < type-id > ( expression )
4247 reinterpret_cast < type-id > ( expression )
4248 const_cast < type-id > ( expression )
4249 typeid ( expression )
4255 ( type-id ) { initializer-list , [opt] }
4257 This extension is a GNU version of the C99 compound-literal
4258 construct. (The C99 grammar uses `type-name' instead of `type-id',
4259 but they are essentially the same concept.)
4261 If ADDRESS_P is true, the postfix expression is the operand of the
4262 `&' operator. CAST_P is true if this expression is the target of a
4265 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4266 class member access expressions [expr.ref].
4268 Returns a representation of the expression. */
4271 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4272 bool member_access_only_p)
4276 cp_id_kind idk = CP_ID_KIND_NONE;
4277 tree postfix_expression = NULL_TREE;
4278 bool is_member_access = false;
4280 /* Peek at the next token. */
4281 token = cp_lexer_peek_token (parser->lexer);
4282 /* Some of the productions are determined by keywords. */
4283 keyword = token->keyword;
4293 const char *saved_message;
4295 /* All of these can be handled in the same way from the point
4296 of view of parsing. Begin by consuming the token
4297 identifying the cast. */
4298 cp_lexer_consume_token (parser->lexer);
4300 /* New types cannot be defined in the cast. */
4301 saved_message = parser->type_definition_forbidden_message;
4302 parser->type_definition_forbidden_message
4303 = "types may not be defined in casts";
4305 /* Look for the opening `<'. */
4306 cp_parser_require (parser, CPP_LESS, "`<'");
4307 /* Parse the type to which we are casting. */
4308 type = cp_parser_type_id (parser);
4309 /* Look for the closing `>'. */
4310 cp_parser_require (parser, CPP_GREATER, "`>'");
4311 /* Restore the old message. */
4312 parser->type_definition_forbidden_message = saved_message;
4314 /* And the expression which is being cast. */
4315 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4316 expression = cp_parser_expression (parser, /*cast_p=*/true);
4317 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4319 /* Only type conversions to integral or enumeration types
4320 can be used in constant-expressions. */
4321 if (!cast_valid_in_integral_constant_expression_p (type)
4322 && (cp_parser_non_integral_constant_expression
4324 "a cast to a type other than an integral or "
4325 "enumeration type")))
4326 return error_mark_node;
4332 = build_dynamic_cast (type, expression);
4336 = build_static_cast (type, expression);
4340 = build_reinterpret_cast (type, expression);
4344 = build_const_cast (type, expression);
4355 const char *saved_message;
4356 bool saved_in_type_id_in_expr_p;
4358 /* Consume the `typeid' token. */
4359 cp_lexer_consume_token (parser->lexer);
4360 /* Look for the `(' token. */
4361 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4362 /* Types cannot be defined in a `typeid' expression. */
4363 saved_message = parser->type_definition_forbidden_message;
4364 parser->type_definition_forbidden_message
4365 = "types may not be defined in a `typeid\' expression";
4366 /* We can't be sure yet whether we're looking at a type-id or an
4368 cp_parser_parse_tentatively (parser);
4369 /* Try a type-id first. */
4370 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4371 parser->in_type_id_in_expr_p = true;
4372 type = cp_parser_type_id (parser);
4373 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4374 /* Look for the `)' token. Otherwise, we can't be sure that
4375 we're not looking at an expression: consider `typeid (int
4376 (3))', for example. */
4377 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4378 /* If all went well, simply lookup the type-id. */
4379 if (cp_parser_parse_definitely (parser))
4380 postfix_expression = get_typeid (type);
4381 /* Otherwise, fall back to the expression variant. */
4386 /* Look for an expression. */
4387 expression = cp_parser_expression (parser, /*cast_p=*/false);
4388 /* Compute its typeid. */
4389 postfix_expression = build_typeid (expression);
4390 /* Look for the `)' token. */
4391 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4393 /* Restore the saved message. */
4394 parser->type_definition_forbidden_message = saved_message;
4395 /* `typeid' may not appear in an integral constant expression. */
4396 if (cp_parser_non_integral_constant_expression(parser,
4397 "`typeid' operator"))
4398 return error_mark_node;
4405 /* The syntax permitted here is the same permitted for an
4406 elaborated-type-specifier. */
4407 type = cp_parser_elaborated_type_specifier (parser,
4408 /*is_friend=*/false,
4409 /*is_declaration=*/false);
4410 postfix_expression = cp_parser_functional_cast (parser, type);
4418 /* If the next thing is a simple-type-specifier, we may be
4419 looking at a functional cast. We could also be looking at
4420 an id-expression. So, we try the functional cast, and if
4421 that doesn't work we fall back to the primary-expression. */
4422 cp_parser_parse_tentatively (parser);
4423 /* Look for the simple-type-specifier. */
4424 type = cp_parser_simple_type_specifier (parser,
4425 /*decl_specs=*/NULL,
4426 CP_PARSER_FLAGS_NONE);
4427 /* Parse the cast itself. */
4428 if (!cp_parser_error_occurred (parser))
4430 = cp_parser_functional_cast (parser, type);
4431 /* If that worked, we're done. */
4432 if (cp_parser_parse_definitely (parser))
4435 /* If the functional-cast didn't work out, try a
4436 compound-literal. */
4437 if (cp_parser_allow_gnu_extensions_p (parser)
4438 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4440 VEC(constructor_elt,gc) *initializer_list = NULL;
4441 bool saved_in_type_id_in_expr_p;
4443 cp_parser_parse_tentatively (parser);
4444 /* Consume the `('. */
4445 cp_lexer_consume_token (parser->lexer);
4446 /* Parse the type. */
4447 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4448 parser->in_type_id_in_expr_p = true;
4449 type = cp_parser_type_id (parser);
4450 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4451 /* Look for the `)'. */
4452 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4453 /* Look for the `{'. */
4454 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4455 /* If things aren't going well, there's no need to
4457 if (!cp_parser_error_occurred (parser))
4459 bool non_constant_p;
4460 /* Parse the initializer-list. */
4462 = cp_parser_initializer_list (parser, &non_constant_p);
4463 /* Allow a trailing `,'. */
4464 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4465 cp_lexer_consume_token (parser->lexer);
4466 /* Look for the final `}'. */
4467 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4469 /* If that worked, we're definitely looking at a
4470 compound-literal expression. */
4471 if (cp_parser_parse_definitely (parser))
4473 /* Warn the user that a compound literal is not
4474 allowed in standard C++. */
4476 pedwarn ("ISO C++ forbids compound-literals");
4477 /* For simplicity, we disallow compound literals in
4478 constant-expressions. We could
4479 allow compound literals of integer type, whose
4480 initializer was a constant, in constant
4481 expressions. Permitting that usage, as a further
4482 extension, would not change the meaning of any
4483 currently accepted programs. (Of course, as
4484 compound literals are not part of ISO C++, the
4485 standard has nothing to say.) */
4486 if (cp_parser_non_integral_constant_expression
4487 (parser, "non-constant compound literals"))
4489 postfix_expression = error_mark_node;
4492 /* Form the representation of the compound-literal. */
4494 = finish_compound_literal (type, initializer_list);
4499 /* It must be a primary-expression. */
4501 = cp_parser_primary_expression (parser, address_p, cast_p,
4502 /*template_arg_p=*/false,
4508 /* Keep looping until the postfix-expression is complete. */
4511 if (idk == CP_ID_KIND_UNQUALIFIED
4512 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4513 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4514 /* It is not a Koenig lookup function call. */
4516 = unqualified_name_lookup_error (postfix_expression);
4518 /* Peek at the next token. */
4519 token = cp_lexer_peek_token (parser->lexer);
4521 switch (token->type)
4523 case CPP_OPEN_SQUARE:
4525 = cp_parser_postfix_open_square_expression (parser,
4528 idk = CP_ID_KIND_NONE;
4529 is_member_access = false;
4532 case CPP_OPEN_PAREN:
4533 /* postfix-expression ( expression-list [opt] ) */
4536 bool is_builtin_constant_p;
4537 bool saved_integral_constant_expression_p = false;
4538 bool saved_non_integral_constant_expression_p = false;
4541 is_member_access = false;
4543 is_builtin_constant_p
4544 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4545 if (is_builtin_constant_p)
4547 /* The whole point of __builtin_constant_p is to allow
4548 non-constant expressions to appear as arguments. */
4549 saved_integral_constant_expression_p
4550 = parser->integral_constant_expression_p;
4551 saved_non_integral_constant_expression_p
4552 = parser->non_integral_constant_expression_p;
4553 parser->integral_constant_expression_p = false;
4555 args = (cp_parser_parenthesized_expression_list
4556 (parser, /*is_attribute_list=*/false,
4557 /*cast_p=*/false, /*allow_expansion_p=*/true,
4558 /*non_constant_p=*/NULL));
4559 if (is_builtin_constant_p)
4561 parser->integral_constant_expression_p
4562 = saved_integral_constant_expression_p;
4563 parser->non_integral_constant_expression_p
4564 = saved_non_integral_constant_expression_p;
4567 if (args == error_mark_node)
4569 postfix_expression = error_mark_node;
4573 /* Function calls are not permitted in
4574 constant-expressions. */
4575 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4576 && cp_parser_non_integral_constant_expression (parser,
4579 postfix_expression = error_mark_node;
4584 if (idk == CP_ID_KIND_UNQUALIFIED)
4586 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4592 = perform_koenig_lookup (postfix_expression, args);
4596 = unqualified_fn_lookup_error (postfix_expression);
4598 /* We do not perform argument-dependent lookup if
4599 normal lookup finds a non-function, in accordance
4600 with the expected resolution of DR 218. */
4601 else if (args && is_overloaded_fn (postfix_expression))
4603 tree fn = get_first_fn (postfix_expression);
4605 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4606 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4608 /* Only do argument dependent lookup if regular
4609 lookup does not find a set of member functions.
4610 [basic.lookup.koenig]/2a */
4611 if (!DECL_FUNCTION_MEMBER_P (fn))
4615 = perform_koenig_lookup (postfix_expression, args);
4620 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4622 tree instance = TREE_OPERAND (postfix_expression, 0);
4623 tree fn = TREE_OPERAND (postfix_expression, 1);
4625 if (processing_template_decl
4626 && (type_dependent_expression_p (instance)
4627 || (!BASELINK_P (fn)
4628 && TREE_CODE (fn) != FIELD_DECL)
4629 || type_dependent_expression_p (fn)
4630 || any_type_dependent_arguments_p (args)))
4633 = build_nt_call_list (postfix_expression, args);
4637 if (BASELINK_P (fn))
4639 = (build_new_method_call
4640 (instance, fn, args, NULL_TREE,
4641 (idk == CP_ID_KIND_QUALIFIED
4642 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4646 = finish_call_expr (postfix_expression, args,
4647 /*disallow_virtual=*/false,
4648 /*koenig_p=*/false);
4650 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4651 || TREE_CODE (postfix_expression) == MEMBER_REF
4652 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4653 postfix_expression = (build_offset_ref_call_from_tree
4654 (postfix_expression, args));
4655 else if (idk == CP_ID_KIND_QUALIFIED)
4656 /* A call to a static class member, or a namespace-scope
4659 = finish_call_expr (postfix_expression, args,
4660 /*disallow_virtual=*/true,
4663 /* All other function calls. */
4665 = finish_call_expr (postfix_expression, args,
4666 /*disallow_virtual=*/false,
4669 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4670 idk = CP_ID_KIND_NONE;
4676 /* postfix-expression . template [opt] id-expression
4677 postfix-expression . pseudo-destructor-name
4678 postfix-expression -> template [opt] id-expression
4679 postfix-expression -> pseudo-destructor-name */
4681 /* Consume the `.' or `->' operator. */
4682 cp_lexer_consume_token (parser->lexer);
4685 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4689 is_member_access = true;
4693 /* postfix-expression ++ */
4694 /* Consume the `++' token. */
4695 cp_lexer_consume_token (parser->lexer);
4696 /* Generate a representation for the complete expression. */
4698 = finish_increment_expr (postfix_expression,
4699 POSTINCREMENT_EXPR);
4700 /* Increments may not appear in constant-expressions. */
4701 if (cp_parser_non_integral_constant_expression (parser,
4703 postfix_expression = error_mark_node;
4704 idk = CP_ID_KIND_NONE;
4705 is_member_access = false;
4708 case CPP_MINUS_MINUS:
4709 /* postfix-expression -- */
4710 /* Consume the `--' token. */
4711 cp_lexer_consume_token (parser->lexer);
4712 /* Generate a representation for the complete expression. */
4714 = finish_increment_expr (postfix_expression,
4715 POSTDECREMENT_EXPR);
4716 /* Decrements may not appear in constant-expressions. */
4717 if (cp_parser_non_integral_constant_expression (parser,
4719 postfix_expression = error_mark_node;
4720 idk = CP_ID_KIND_NONE;
4721 is_member_access = false;
4725 if (member_access_only_p)
4726 return is_member_access? postfix_expression : error_mark_node;
4728 return postfix_expression;
4732 /* We should never get here. */
4734 return error_mark_node;
4737 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4738 by cp_parser_builtin_offsetof. We're looking for
4740 postfix-expression [ expression ]
4742 FOR_OFFSETOF is set if we're being called in that context, which
4743 changes how we deal with integer constant expressions. */
4746 cp_parser_postfix_open_square_expression (cp_parser *parser,
4747 tree postfix_expression,
4752 /* Consume the `[' token. */
4753 cp_lexer_consume_token (parser->lexer);
4755 /* Parse the index expression. */
4756 /* ??? For offsetof, there is a question of what to allow here. If
4757 offsetof is not being used in an integral constant expression context,
4758 then we *could* get the right answer by computing the value at runtime.
4759 If we are in an integral constant expression context, then we might
4760 could accept any constant expression; hard to say without analysis.
4761 Rather than open the barn door too wide right away, allow only integer
4762 constant expressions here. */
4764 index = cp_parser_constant_expression (parser, false, NULL);
4766 index = cp_parser_expression (parser, /*cast_p=*/false);
4768 /* Look for the closing `]'. */
4769 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4771 /* Build the ARRAY_REF. */
4772 postfix_expression = grok_array_decl (postfix_expression, index);
4774 /* When not doing offsetof, array references are not permitted in
4775 constant-expressions. */
4777 && (cp_parser_non_integral_constant_expression
4778 (parser, "an array reference")))
4779 postfix_expression = error_mark_node;
4781 return postfix_expression;
4784 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4785 by cp_parser_builtin_offsetof. We're looking for
4787 postfix-expression . template [opt] id-expression
4788 postfix-expression . pseudo-destructor-name
4789 postfix-expression -> template [opt] id-expression
4790 postfix-expression -> pseudo-destructor-name
4792 FOR_OFFSETOF is set if we're being called in that context. That sorta
4793 limits what of the above we'll actually accept, but nevermind.
4794 TOKEN_TYPE is the "." or "->" token, which will already have been
4795 removed from the stream. */
4798 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4799 enum cpp_ttype token_type,
4800 tree postfix_expression,
4801 bool for_offsetof, cp_id_kind *idk)
4805 bool pseudo_destructor_p;
4806 tree scope = NULL_TREE;
4808 /* If this is a `->' operator, dereference the pointer. */
4809 if (token_type == CPP_DEREF)
4810 postfix_expression = build_x_arrow (postfix_expression);
4811 /* Check to see whether or not the expression is type-dependent. */
4812 dependent_p = type_dependent_expression_p (postfix_expression);
4813 /* The identifier following the `->' or `.' is not qualified. */
4814 parser->scope = NULL_TREE;
4815 parser->qualifying_scope = NULL_TREE;
4816 parser->object_scope = NULL_TREE;
4817 *idk = CP_ID_KIND_NONE;
4818 /* Enter the scope corresponding to the type of the object
4819 given by the POSTFIX_EXPRESSION. */
4820 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4822 scope = TREE_TYPE (postfix_expression);
4823 /* According to the standard, no expression should ever have
4824 reference type. Unfortunately, we do not currently match
4825 the standard in this respect in that our internal representation
4826 of an expression may have reference type even when the standard
4827 says it does not. Therefore, we have to manually obtain the
4828 underlying type here. */
4829 scope = non_reference (scope);
4830 /* The type of the POSTFIX_EXPRESSION must be complete. */
4831 if (scope == unknown_type_node)
4833 error ("%qE does not have class type", postfix_expression);
4837 scope = complete_type_or_else (scope, NULL_TREE);
4838 /* Let the name lookup machinery know that we are processing a
4839 class member access expression. */
4840 parser->context->object_type = scope;
4841 /* If something went wrong, we want to be able to discern that case,
4842 as opposed to the case where there was no SCOPE due to the type
4843 of expression being dependent. */
4845 scope = error_mark_node;
4846 /* If the SCOPE was erroneous, make the various semantic analysis
4847 functions exit quickly -- and without issuing additional error
4849 if (scope == error_mark_node)
4850 postfix_expression = error_mark_node;
4853 /* Assume this expression is not a pseudo-destructor access. */
4854 pseudo_destructor_p = false;
4856 /* If the SCOPE is a scalar type, then, if this is a valid program,
4857 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4858 is type dependent, it can be pseudo-destructor-name or something else.
4859 Try to parse it as pseudo-destructor-name first. */
4860 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4865 cp_parser_parse_tentatively (parser);
4866 /* Parse the pseudo-destructor-name. */
4868 cp_parser_pseudo_destructor_name (parser, &s, &type);
4870 && (cp_parser_error_occurred (parser)
4871 || TREE_CODE (type) != TYPE_DECL
4872 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4873 cp_parser_abort_tentative_parse (parser);
4874 else if (cp_parser_parse_definitely (parser))
4876 pseudo_destructor_p = true;
4878 = finish_pseudo_destructor_expr (postfix_expression,
4879 s, TREE_TYPE (type));
4883 if (!pseudo_destructor_p)
4885 /* If the SCOPE is not a scalar type, we are looking at an
4886 ordinary class member access expression, rather than a
4887 pseudo-destructor-name. */
4889 /* Parse the id-expression. */
4890 name = (cp_parser_id_expression
4892 cp_parser_optional_template_keyword (parser),
4893 /*check_dependency_p=*/true,
4895 /*declarator_p=*/false,
4896 /*optional_p=*/false));
4897 /* In general, build a SCOPE_REF if the member name is qualified.
4898 However, if the name was not dependent and has already been
4899 resolved; there is no need to build the SCOPE_REF. For example;
4901 struct X { void f(); };
4902 template <typename T> void f(T* t) { t->X::f(); }
4904 Even though "t" is dependent, "X::f" is not and has been resolved
4905 to a BASELINK; there is no need to include scope information. */
4907 /* But we do need to remember that there was an explicit scope for
4908 virtual function calls. */
4910 *idk = CP_ID_KIND_QUALIFIED;
4912 /* If the name is a template-id that names a type, we will get a
4913 TYPE_DECL here. That is invalid code. */
4914 if (TREE_CODE (name) == TYPE_DECL)
4916 error ("invalid use of %qD", name);
4917 postfix_expression = error_mark_node;
4921 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4923 name = build_qualified_name (/*type=*/NULL_TREE,
4927 parser->scope = NULL_TREE;
4928 parser->qualifying_scope = NULL_TREE;
4929 parser->object_scope = NULL_TREE;
4931 if (scope && name && BASELINK_P (name))
4932 adjust_result_of_qualified_name_lookup
4933 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4935 = finish_class_member_access_expr (postfix_expression, name,
4940 /* We no longer need to look up names in the scope of the object on
4941 the left-hand side of the `.' or `->' operator. */
4942 parser->context->object_type = NULL_TREE;
4944 /* Outside of offsetof, these operators may not appear in
4945 constant-expressions. */
4947 && (cp_parser_non_integral_constant_expression
4948 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4949 postfix_expression = error_mark_node;
4951 return postfix_expression;
4954 /* Parse a parenthesized expression-list.
4957 assignment-expression
4958 expression-list, assignment-expression
4963 identifier, expression-list
4965 CAST_P is true if this expression is the target of a cast.
4967 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4970 Returns a TREE_LIST. The TREE_VALUE of each node is a
4971 representation of an assignment-expression. Note that a TREE_LIST
4972 is returned even if there is only a single expression in the list.
4973 error_mark_node is returned if the ( and or ) are
4974 missing. NULL_TREE is returned on no expressions. The parentheses
4975 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4976 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4977 indicates whether or not all of the expressions in the list were
4981 cp_parser_parenthesized_expression_list (cp_parser* parser,
4982 bool is_attribute_list,
4984 bool allow_expansion_p,
4985 bool *non_constant_p)
4987 tree expression_list = NULL_TREE;
4988 bool fold_expr_p = is_attribute_list;
4989 tree identifier = NULL_TREE;
4990 bool saved_greater_than_is_operator_p;
4992 /* Assume all the expressions will be constant. */
4994 *non_constant_p = false;
4996 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4997 return error_mark_node;
4999 /* Within a parenthesized expression, a `>' token is always
5000 the greater-than operator. */
5001 saved_greater_than_is_operator_p
5002 = parser->greater_than_is_operator_p;
5003 parser->greater_than_is_operator_p = true;
5005 /* Consume expressions until there are no more. */
5006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5011 /* At the beginning of attribute lists, check to see if the
5012 next token is an identifier. */
5013 if (is_attribute_list
5014 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5018 /* Consume the identifier. */
5019 token = cp_lexer_consume_token (parser->lexer);
5020 /* Save the identifier. */
5021 identifier = token->u.value;
5025 /* Parse the next assignment-expression. */
5028 bool expr_non_constant_p;
5029 expr = (cp_parser_constant_expression
5030 (parser, /*allow_non_constant_p=*/true,
5031 &expr_non_constant_p));
5032 if (expr_non_constant_p)
5033 *non_constant_p = true;
5036 expr = cp_parser_assignment_expression (parser, cast_p);
5039 expr = fold_non_dependent_expr (expr);
5041 /* If we have an ellipsis, then this is an expression
5043 if (allow_expansion_p
5044 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5046 /* Consume the `...'. */
5047 cp_lexer_consume_token (parser->lexer);
5049 /* Build the argument pack. */
5050 expr = make_pack_expansion (expr);
5053 /* Add it to the list. We add error_mark_node
5054 expressions to the list, so that we can still tell if
5055 the correct form for a parenthesized expression-list
5056 is found. That gives better errors. */
5057 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5059 if (expr == error_mark_node)
5063 /* After the first item, attribute lists look the same as
5064 expression lists. */
5065 is_attribute_list = false;
5068 /* If the next token isn't a `,', then we are done. */
5069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5072 /* Otherwise, consume the `,' and keep going. */
5073 cp_lexer_consume_token (parser->lexer);
5076 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5081 /* We try and resync to an unnested comma, as that will give the
5082 user better diagnostics. */
5083 ending = cp_parser_skip_to_closing_parenthesis (parser,
5084 /*recovering=*/true,
5086 /*consume_paren=*/true);
5091 parser->greater_than_is_operator_p
5092 = saved_greater_than_is_operator_p;
5093 return error_mark_node;
5097 parser->greater_than_is_operator_p
5098 = saved_greater_than_is_operator_p;
5100 /* We built up the list in reverse order so we must reverse it now. */
5101 expression_list = nreverse (expression_list);
5103 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5105 return expression_list;
5108 /* Parse a pseudo-destructor-name.
5110 pseudo-destructor-name:
5111 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5112 :: [opt] nested-name-specifier template template-id :: ~ type-name
5113 :: [opt] nested-name-specifier [opt] ~ type-name
5115 If either of the first two productions is used, sets *SCOPE to the
5116 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5117 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5118 or ERROR_MARK_NODE if the parse fails. */
5121 cp_parser_pseudo_destructor_name (cp_parser* parser,
5125 bool nested_name_specifier_p;
5127 /* Assume that things will not work out. */
5128 *type = error_mark_node;
5130 /* Look for the optional `::' operator. */
5131 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5132 /* Look for the optional nested-name-specifier. */
5133 nested_name_specifier_p
5134 = (cp_parser_nested_name_specifier_opt (parser,
5135 /*typename_keyword_p=*/false,
5136 /*check_dependency_p=*/true,
5138 /*is_declaration=*/true)
5140 /* Now, if we saw a nested-name-specifier, we might be doing the
5141 second production. */
5142 if (nested_name_specifier_p
5143 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5145 /* Consume the `template' keyword. */
5146 cp_lexer_consume_token (parser->lexer);
5147 /* Parse the template-id. */
5148 cp_parser_template_id (parser,
5149 /*template_keyword_p=*/true,
5150 /*check_dependency_p=*/false,
5151 /*is_declaration=*/true);
5152 /* Look for the `::' token. */
5153 cp_parser_require (parser, CPP_SCOPE, "`::'");
5155 /* If the next token is not a `~', then there might be some
5156 additional qualification. */
5157 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5159 /* At this point, we're looking for "type-name :: ~". The type-name
5160 must not be a class-name, since this is a pseudo-destructor. So,
5161 it must be either an enum-name, or a typedef-name -- both of which
5162 are just identifiers. So, we peek ahead to check that the "::"
5163 and "~" tokens are present; if they are not, then we can avoid
5164 calling type_name. */
5165 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5166 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5167 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5169 cp_parser_error (parser, "non-scalar type");
5173 /* Look for the type-name. */
5174 *scope = TREE_TYPE (cp_parser_type_name (parser));
5176 if (*scope == error_mark_node)
5179 /* Look for the `::' token. */
5180 cp_parser_require (parser, CPP_SCOPE, "`::'");
5185 /* Look for the `~'. */
5186 cp_parser_require (parser, CPP_COMPL, "`~'");
5187 /* Look for the type-name again. We are not responsible for
5188 checking that it matches the first type-name. */
5189 *type = cp_parser_type_name (parser);
5192 /* Parse a unary-expression.
5198 unary-operator cast-expression
5199 sizeof unary-expression
5207 __extension__ cast-expression
5208 __alignof__ unary-expression
5209 __alignof__ ( type-id )
5210 __real__ cast-expression
5211 __imag__ cast-expression
5214 ADDRESS_P is true iff the unary-expression is appearing as the
5215 operand of the `&' operator. CAST_P is true if this expression is
5216 the target of a cast.
5218 Returns a representation of the expression. */
5221 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5224 enum tree_code unary_operator;
5226 /* Peek at the next token. */
5227 token = cp_lexer_peek_token (parser->lexer);
5228 /* Some keywords give away the kind of expression. */
5229 if (token->type == CPP_KEYWORD)
5231 enum rid keyword = token->keyword;
5241 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5242 /* Consume the token. */
5243 cp_lexer_consume_token (parser->lexer);
5244 /* Parse the operand. */
5245 operand = cp_parser_sizeof_operand (parser, keyword);
5247 if (TYPE_P (operand))
5248 return cxx_sizeof_or_alignof_type (operand, op, true);
5250 return cxx_sizeof_or_alignof_expr (operand, op);
5254 return cp_parser_new_expression (parser);
5257 return cp_parser_delete_expression (parser);
5261 /* The saved value of the PEDANTIC flag. */
5265 /* Save away the PEDANTIC flag. */
5266 cp_parser_extension_opt (parser, &saved_pedantic);
5267 /* Parse the cast-expression. */
5268 expr = cp_parser_simple_cast_expression (parser);
5269 /* Restore the PEDANTIC flag. */
5270 pedantic = saved_pedantic;
5280 /* Consume the `__real__' or `__imag__' token. */
5281 cp_lexer_consume_token (parser->lexer);
5282 /* Parse the cast-expression. */
5283 expression = cp_parser_simple_cast_expression (parser);
5284 /* Create the complete representation. */
5285 return build_x_unary_op ((keyword == RID_REALPART
5286 ? REALPART_EXPR : IMAGPART_EXPR),
5296 /* Look for the `:: new' and `:: delete', which also signal the
5297 beginning of a new-expression, or delete-expression,
5298 respectively. If the next token is `::', then it might be one of
5300 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5304 /* See if the token after the `::' is one of the keywords in
5305 which we're interested. */
5306 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5307 /* If it's `new', we have a new-expression. */
5308 if (keyword == RID_NEW)
5309 return cp_parser_new_expression (parser);
5310 /* Similarly, for `delete'. */
5311 else if (keyword == RID_DELETE)
5312 return cp_parser_delete_expression (parser);
5315 /* Look for a unary operator. */
5316 unary_operator = cp_parser_unary_operator (token);
5317 /* The `++' and `--' operators can be handled similarly, even though
5318 they are not technically unary-operators in the grammar. */
5319 if (unary_operator == ERROR_MARK)
5321 if (token->type == CPP_PLUS_PLUS)
5322 unary_operator = PREINCREMENT_EXPR;
5323 else if (token->type == CPP_MINUS_MINUS)
5324 unary_operator = PREDECREMENT_EXPR;
5325 /* Handle the GNU address-of-label extension. */
5326 else if (cp_parser_allow_gnu_extensions_p (parser)
5327 && token->type == CPP_AND_AND)
5332 /* Consume the '&&' token. */
5333 cp_lexer_consume_token (parser->lexer);
5334 /* Look for the identifier. */
5335 identifier = cp_parser_identifier (parser);
5336 /* Create an expression representing the address. */
5337 expression = finish_label_address_expr (identifier);
5338 if (cp_parser_non_integral_constant_expression (parser,
5339 "the address of a label"))
5340 expression = error_mark_node;
5344 if (unary_operator != ERROR_MARK)
5346 tree cast_expression;
5347 tree expression = error_mark_node;
5348 const char *non_constant_p = NULL;
5350 /* Consume the operator token. */
5351 token = cp_lexer_consume_token (parser->lexer);
5352 /* Parse the cast-expression. */
5354 = cp_parser_cast_expression (parser,
5355 unary_operator == ADDR_EXPR,
5357 /* Now, build an appropriate representation. */
5358 switch (unary_operator)
5361 non_constant_p = "`*'";
5362 expression = build_x_indirect_ref (cast_expression, "unary *");
5366 non_constant_p = "`&'";
5369 expression = build_x_unary_op (unary_operator, cast_expression);
5372 case PREINCREMENT_EXPR:
5373 case PREDECREMENT_EXPR:
5374 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5377 case UNARY_PLUS_EXPR:
5379 case TRUTH_NOT_EXPR:
5380 expression = finish_unary_op_expr (unary_operator, cast_expression);
5388 && cp_parser_non_integral_constant_expression (parser,
5390 expression = error_mark_node;
5395 return cp_parser_postfix_expression (parser, address_p, cast_p,
5396 /*member_access_only_p=*/false);
5399 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5400 unary-operator, the corresponding tree code is returned. */
5402 static enum tree_code
5403 cp_parser_unary_operator (cp_token* token)
5405 switch (token->type)
5408 return INDIRECT_REF;
5414 return UNARY_PLUS_EXPR;
5420 return TRUTH_NOT_EXPR;
5423 return BIT_NOT_EXPR;
5430 /* Parse a new-expression.
5433 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5434 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5436 Returns a representation of the expression. */
5439 cp_parser_new_expression (cp_parser* parser)
5441 bool global_scope_p;
5447 /* Look for the optional `::' operator. */
5449 = (cp_parser_global_scope_opt (parser,
5450 /*current_scope_valid_p=*/false)
5452 /* Look for the `new' operator. */
5453 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5454 /* There's no easy way to tell a new-placement from the
5455 `( type-id )' construct. */
5456 cp_parser_parse_tentatively (parser);
5457 /* Look for a new-placement. */
5458 placement = cp_parser_new_placement (parser);
5459 /* If that didn't work out, there's no new-placement. */
5460 if (!cp_parser_parse_definitely (parser))
5461 placement = NULL_TREE;
5463 /* If the next token is a `(', then we have a parenthesized
5465 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5467 /* Consume the `('. */
5468 cp_lexer_consume_token (parser->lexer);
5469 /* Parse the type-id. */
5470 type = cp_parser_type_id (parser);
5471 /* Look for the closing `)'. */
5472 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5473 /* There should not be a direct-new-declarator in this production,
5474 but GCC used to allowed this, so we check and emit a sensible error
5475 message for this case. */
5476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5478 error ("array bound forbidden after parenthesized type-id");
5479 inform ("try removing the parentheses around the type-id");
5480 cp_parser_direct_new_declarator (parser);
5484 /* Otherwise, there must be a new-type-id. */
5486 type = cp_parser_new_type_id (parser, &nelts);
5488 /* If the next token is a `(', then we have a new-initializer. */
5489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5490 initializer = cp_parser_new_initializer (parser);
5492 initializer = NULL_TREE;
5494 /* A new-expression may not appear in an integral constant
5496 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5497 return error_mark_node;
5499 /* Create a representation of the new-expression. */
5500 return build_new (placement, type, nelts, initializer, global_scope_p);
5503 /* Parse a new-placement.
5508 Returns the same representation as for an expression-list. */
5511 cp_parser_new_placement (cp_parser* parser)
5513 tree expression_list;
5515 /* Parse the expression-list. */
5516 expression_list = (cp_parser_parenthesized_expression_list
5517 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5518 /*non_constant_p=*/NULL));
5520 return expression_list;
5523 /* Parse a new-type-id.
5526 type-specifier-seq new-declarator [opt]
5528 Returns the TYPE allocated. If the new-type-id indicates an array
5529 type, *NELTS is set to the number of elements in the last array
5530 bound; the TYPE will not include the last array bound. */
5533 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5535 cp_decl_specifier_seq type_specifier_seq;
5536 cp_declarator *new_declarator;
5537 cp_declarator *declarator;
5538 cp_declarator *outer_declarator;
5539 const char *saved_message;
5542 /* The type-specifier sequence must not contain type definitions.
5543 (It cannot contain declarations of new types either, but if they
5544 are not definitions we will catch that because they are not
5546 saved_message = parser->type_definition_forbidden_message;
5547 parser->type_definition_forbidden_message
5548 = "types may not be defined in a new-type-id";
5549 /* Parse the type-specifier-seq. */
5550 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5551 &type_specifier_seq);
5552 /* Restore the old message. */
5553 parser->type_definition_forbidden_message = saved_message;
5554 /* Parse the new-declarator. */
5555 new_declarator = cp_parser_new_declarator_opt (parser);
5557 /* Determine the number of elements in the last array dimension, if
5560 /* Skip down to the last array dimension. */
5561 declarator = new_declarator;
5562 outer_declarator = NULL;
5563 while (declarator && (declarator->kind == cdk_pointer
5564 || declarator->kind == cdk_ptrmem))
5566 outer_declarator = declarator;
5567 declarator = declarator->declarator;
5570 && declarator->kind == cdk_array
5571 && declarator->declarator
5572 && declarator->declarator->kind == cdk_array)
5574 outer_declarator = declarator;
5575 declarator = declarator->declarator;
5578 if (declarator && declarator->kind == cdk_array)
5580 *nelts = declarator->u.array.bounds;
5581 if (*nelts == error_mark_node)
5582 *nelts = integer_one_node;
5584 if (outer_declarator)
5585 outer_declarator->declarator = declarator->declarator;
5587 new_declarator = NULL;
5590 type = groktypename (&type_specifier_seq, new_declarator);
5594 /* Parse an (optional) new-declarator.
5597 ptr-operator new-declarator [opt]
5598 direct-new-declarator
5600 Returns the declarator. */
5602 static cp_declarator *
5603 cp_parser_new_declarator_opt (cp_parser* parser)
5605 enum tree_code code;
5607 cp_cv_quals cv_quals;
5609 /* We don't know if there's a ptr-operator next, or not. */
5610 cp_parser_parse_tentatively (parser);
5611 /* Look for a ptr-operator. */
5612 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5613 /* If that worked, look for more new-declarators. */
5614 if (cp_parser_parse_definitely (parser))
5616 cp_declarator *declarator;
5618 /* Parse another optional declarator. */
5619 declarator = cp_parser_new_declarator_opt (parser);
5621 return cp_parser_make_indirect_declarator
5622 (code, type, cv_quals, declarator);
5625 /* If the next token is a `[', there is a direct-new-declarator. */
5626 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5627 return cp_parser_direct_new_declarator (parser);
5632 /* Parse a direct-new-declarator.
5634 direct-new-declarator:
5636 direct-new-declarator [constant-expression]
5640 static cp_declarator *
5641 cp_parser_direct_new_declarator (cp_parser* parser)
5643 cp_declarator *declarator = NULL;
5649 /* Look for the opening `['. */
5650 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5651 /* The first expression is not required to be constant. */
5654 expression = cp_parser_expression (parser, /*cast_p=*/false);
5655 /* The standard requires that the expression have integral
5656 type. DR 74 adds enumeration types. We believe that the
5657 real intent is that these expressions be handled like the
5658 expression in a `switch' condition, which also allows
5659 classes with a single conversion to integral or
5660 enumeration type. */
5661 if (!processing_template_decl)
5664 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5669 error ("expression in new-declarator must have integral "
5670 "or enumeration type");
5671 expression = error_mark_node;
5675 /* But all the other expressions must be. */
5678 = cp_parser_constant_expression (parser,
5679 /*allow_non_constant=*/false,
5681 /* Look for the closing `]'. */
5682 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5684 /* Add this bound to the declarator. */
5685 declarator = make_array_declarator (declarator, expression);
5687 /* If the next token is not a `[', then there are no more
5689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5696 /* Parse a new-initializer.
5699 ( expression-list [opt] )
5701 Returns a representation of the expression-list. If there is no
5702 expression-list, VOID_ZERO_NODE is returned. */
5705 cp_parser_new_initializer (cp_parser* parser)
5707 tree expression_list;
5709 expression_list = (cp_parser_parenthesized_expression_list
5710 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5711 /*non_constant_p=*/NULL));
5712 if (!expression_list)
5713 expression_list = void_zero_node;
5715 return expression_list;
5718 /* Parse a delete-expression.
5721 :: [opt] delete cast-expression
5722 :: [opt] delete [ ] cast-expression
5724 Returns a representation of the expression. */
5727 cp_parser_delete_expression (cp_parser* parser)
5729 bool global_scope_p;
5733 /* Look for the optional `::' operator. */
5735 = (cp_parser_global_scope_opt (parser,
5736 /*current_scope_valid_p=*/false)
5738 /* Look for the `delete' keyword. */
5739 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5740 /* See if the array syntax is in use. */
5741 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5743 /* Consume the `[' token. */
5744 cp_lexer_consume_token (parser->lexer);
5745 /* Look for the `]' token. */
5746 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5747 /* Remember that this is the `[]' construct. */
5753 /* Parse the cast-expression. */
5754 expression = cp_parser_simple_cast_expression (parser);
5756 /* A delete-expression may not appear in an integral constant
5758 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5759 return error_mark_node;
5761 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5764 /* Parse a cast-expression.
5768 ( type-id ) cast-expression
5770 ADDRESS_P is true iff the unary-expression is appearing as the
5771 operand of the `&' operator. CAST_P is true if this expression is
5772 the target of a cast.
5774 Returns a representation of the expression. */
5777 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5779 /* If it's a `(', then we might be looking at a cast. */
5780 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5782 tree type = NULL_TREE;
5783 tree expr = NULL_TREE;
5784 bool compound_literal_p;
5785 const char *saved_message;
5787 /* There's no way to know yet whether or not this is a cast.
5788 For example, `(int (3))' is a unary-expression, while `(int)
5789 3' is a cast. So, we resort to parsing tentatively. */
5790 cp_parser_parse_tentatively (parser);
5791 /* Types may not be defined in a cast. */
5792 saved_message = parser->type_definition_forbidden_message;
5793 parser->type_definition_forbidden_message
5794 = "types may not be defined in casts";
5795 /* Consume the `('. */
5796 cp_lexer_consume_token (parser->lexer);
5797 /* A very tricky bit is that `(struct S) { 3 }' is a
5798 compound-literal (which we permit in C++ as an extension).
5799 But, that construct is not a cast-expression -- it is a
5800 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5801 is legal; if the compound-literal were a cast-expression,
5802 you'd need an extra set of parentheses.) But, if we parse
5803 the type-id, and it happens to be a class-specifier, then we
5804 will commit to the parse at that point, because we cannot
5805 undo the action that is done when creating a new class. So,
5806 then we cannot back up and do a postfix-expression.
5808 Therefore, we scan ahead to the closing `)', and check to see
5809 if the token after the `)' is a `{'. If so, we are not
5810 looking at a cast-expression.
5812 Save tokens so that we can put them back. */
5813 cp_lexer_save_tokens (parser->lexer);
5814 /* Skip tokens until the next token is a closing parenthesis.
5815 If we find the closing `)', and the next token is a `{', then
5816 we are looking at a compound-literal. */
5818 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5819 /*consume_paren=*/true)
5820 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5821 /* Roll back the tokens we skipped. */
5822 cp_lexer_rollback_tokens (parser->lexer);
5823 /* If we were looking at a compound-literal, simulate an error
5824 so that the call to cp_parser_parse_definitely below will
5826 if (compound_literal_p)
5827 cp_parser_simulate_error (parser);
5830 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5831 parser->in_type_id_in_expr_p = true;
5832 /* Look for the type-id. */
5833 type = cp_parser_type_id (parser);
5834 /* Look for the closing `)'. */
5835 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5836 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5839 /* Restore the saved message. */
5840 parser->type_definition_forbidden_message = saved_message;
5842 /* If ok so far, parse the dependent expression. We cannot be
5843 sure it is a cast. Consider `(T ())'. It is a parenthesized
5844 ctor of T, but looks like a cast to function returning T
5845 without a dependent expression. */
5846 if (!cp_parser_error_occurred (parser))
5847 expr = cp_parser_cast_expression (parser,
5848 /*address_p=*/false,
5851 if (cp_parser_parse_definitely (parser))
5853 /* Warn about old-style casts, if so requested. */
5854 if (warn_old_style_cast
5855 && !in_system_header
5856 && !VOID_TYPE_P (type)
5857 && current_lang_name != lang_name_c)
5858 warning (OPT_Wold_style_cast, "use of old-style cast");
5860 /* Only type conversions to integral or enumeration types
5861 can be used in constant-expressions. */
5862 if (!cast_valid_in_integral_constant_expression_p (type)
5863 && (cp_parser_non_integral_constant_expression
5865 "a cast to a type other than an integral or "
5866 "enumeration type")))
5867 return error_mark_node;
5869 /* Perform the cast. */
5870 expr = build_c_cast (type, expr);
5875 /* If we get here, then it's not a cast, so it must be a
5876 unary-expression. */
5877 return cp_parser_unary_expression (parser, address_p, cast_p);
5880 /* Parse a binary expression of the general form:
5884 pm-expression .* cast-expression
5885 pm-expression ->* cast-expression
5887 multiplicative-expression:
5889 multiplicative-expression * pm-expression
5890 multiplicative-expression / pm-expression
5891 multiplicative-expression % pm-expression
5893 additive-expression:
5894 multiplicative-expression
5895 additive-expression + multiplicative-expression
5896 additive-expression - multiplicative-expression
5900 shift-expression << additive-expression
5901 shift-expression >> additive-expression
5903 relational-expression:
5905 relational-expression < shift-expression
5906 relational-expression > shift-expression
5907 relational-expression <= shift-expression
5908 relational-expression >= shift-expression
5912 relational-expression:
5913 relational-expression <? shift-expression
5914 relational-expression >? shift-expression
5916 equality-expression:
5917 relational-expression
5918 equality-expression == relational-expression
5919 equality-expression != relational-expression
5923 and-expression & equality-expression
5925 exclusive-or-expression:
5927 exclusive-or-expression ^ and-expression
5929 inclusive-or-expression:
5930 exclusive-or-expression
5931 inclusive-or-expression | exclusive-or-expression
5933 logical-and-expression:
5934 inclusive-or-expression
5935 logical-and-expression && inclusive-or-expression
5937 logical-or-expression:
5938 logical-and-expression
5939 logical-or-expression || logical-and-expression
5941 All these are implemented with a single function like:
5944 simple-cast-expression
5945 binary-expression <token> binary-expression
5947 CAST_P is true if this expression is the target of a cast.
5949 The binops_by_token map is used to get the tree codes for each <token> type.
5950 binary-expressions are associated according to a precedence table. */
5952 #define TOKEN_PRECEDENCE(token) \
5953 (((token->type == CPP_GREATER \
5954 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5955 && !parser->greater_than_is_operator_p) \
5956 ? PREC_NOT_OPERATOR \
5957 : binops_by_token[token->type].prec)
5960 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5962 cp_parser_expression_stack stack;
5963 cp_parser_expression_stack_entry *sp = &stack[0];
5966 enum tree_code tree_type, lhs_type, rhs_type;
5967 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5970 /* Parse the first expression. */
5971 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5972 lhs_type = ERROR_MARK;
5976 /* Get an operator token. */
5977 token = cp_lexer_peek_token (parser->lexer);
5979 if (warn_cxx0x_compat
5980 && token->type == CPP_RSHIFT
5981 && !parser->greater_than_is_operator_p)
5983 warning (OPT_Wc__0x_compat,
5984 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5986 warning (OPT_Wc__0x_compat,
5987 "suggest parentheses around %<>>%> expression");
5990 new_prec = TOKEN_PRECEDENCE (token);
5992 /* Popping an entry off the stack means we completed a subexpression:
5993 - either we found a token which is not an operator (`>' where it is not
5994 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5995 will happen repeatedly;
5996 - or, we found an operator which has lower priority. This is the case
5997 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5999 if (new_prec <= prec)
6008 tree_type = binops_by_token[token->type].tree_type;
6010 /* We used the operator token. */
6011 cp_lexer_consume_token (parser->lexer);
6013 /* Extract another operand. It may be the RHS of this expression
6014 or the LHS of a new, higher priority expression. */
6015 rhs = cp_parser_simple_cast_expression (parser);
6016 rhs_type = ERROR_MARK;
6018 /* Get another operator token. Look up its precedence to avoid
6019 building a useless (immediately popped) stack entry for common
6020 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6021 token = cp_lexer_peek_token (parser->lexer);
6022 lookahead_prec = TOKEN_PRECEDENCE (token);
6023 if (lookahead_prec > new_prec)
6025 /* ... and prepare to parse the RHS of the new, higher priority
6026 expression. Since precedence levels on the stack are
6027 monotonically increasing, we do not have to care about
6030 sp->tree_type = tree_type;
6032 sp->lhs_type = lhs_type;
6035 lhs_type = rhs_type;
6037 new_prec = lookahead_prec;
6041 /* If the stack is not empty, we have parsed into LHS the right side
6042 (`4' in the example above) of an expression we had suspended.
6043 We can use the information on the stack to recover the LHS (`3')
6044 from the stack together with the tree code (`MULT_EXPR'), and
6045 the precedence of the higher level subexpression
6046 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6047 which will be used to actually build the additive expression. */
6050 tree_type = sp->tree_type;
6052 rhs_type = lhs_type;
6054 lhs_type = sp->lhs_type;
6057 overloaded_p = false;
6058 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6060 lhs_type = tree_type;
6062 /* If the binary operator required the use of an overloaded operator,
6063 then this expression cannot be an integral constant-expression.
6064 An overloaded operator can be used even if both operands are
6065 otherwise permissible in an integral constant-expression if at
6066 least one of the operands is of enumeration type. */
6069 && (cp_parser_non_integral_constant_expression
6070 (parser, "calls to overloaded operators")))
6071 return error_mark_node;
6078 /* Parse the `? expression : assignment-expression' part of a
6079 conditional-expression. The LOGICAL_OR_EXPR is the
6080 logical-or-expression that started the conditional-expression.
6081 Returns a representation of the entire conditional-expression.
6083 This routine is used by cp_parser_assignment_expression.
6085 ? expression : assignment-expression
6089 ? : assignment-expression */
6092 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6095 tree assignment_expr;
6097 /* Consume the `?' token. */
6098 cp_lexer_consume_token (parser->lexer);
6099 if (cp_parser_allow_gnu_extensions_p (parser)
6100 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6101 /* Implicit true clause. */
6104 /* Parse the expression. */
6105 expr = cp_parser_expression (parser, /*cast_p=*/false);
6107 /* The next token should be a `:'. */
6108 cp_parser_require (parser, CPP_COLON, "`:'");
6109 /* Parse the assignment-expression. */
6110 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6112 /* Build the conditional-expression. */
6113 return build_x_conditional_expr (logical_or_expr,
6118 /* Parse an assignment-expression.
6120 assignment-expression:
6121 conditional-expression
6122 logical-or-expression assignment-operator assignment_expression
6125 CAST_P is true if this expression is the target of a cast.
6127 Returns a representation for the expression. */
6130 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6134 /* If the next token is the `throw' keyword, then we're looking at
6135 a throw-expression. */
6136 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6137 expr = cp_parser_throw_expression (parser);
6138 /* Otherwise, it must be that we are looking at a
6139 logical-or-expression. */
6142 /* Parse the binary expressions (logical-or-expression). */
6143 expr = cp_parser_binary_expression (parser, cast_p);
6144 /* If the next token is a `?' then we're actually looking at a
6145 conditional-expression. */
6146 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6147 return cp_parser_question_colon_clause (parser, expr);
6150 enum tree_code assignment_operator;
6152 /* If it's an assignment-operator, we're using the second
6155 = cp_parser_assignment_operator_opt (parser);
6156 if (assignment_operator != ERROR_MARK)
6160 /* Parse the right-hand side of the assignment. */
6161 rhs = cp_parser_assignment_expression (parser, cast_p);
6162 /* An assignment may not appear in a
6163 constant-expression. */
6164 if (cp_parser_non_integral_constant_expression (parser,
6166 return error_mark_node;
6167 /* Build the assignment expression. */
6168 expr = build_x_modify_expr (expr,
6169 assignment_operator,
6178 /* Parse an (optional) assignment-operator.
6180 assignment-operator: one of
6181 = *= /= %= += -= >>= <<= &= ^= |=
6185 assignment-operator: one of
6188 If the next token is an assignment operator, the corresponding tree
6189 code is returned, and the token is consumed. For example, for
6190 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6191 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6192 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6193 operator, ERROR_MARK is returned. */
6195 static enum tree_code
6196 cp_parser_assignment_operator_opt (cp_parser* parser)
6201 /* Peek at the next toen. */
6202 token = cp_lexer_peek_token (parser->lexer);
6204 switch (token->type)
6215 op = TRUNC_DIV_EXPR;
6219 op = TRUNC_MOD_EXPR;
6251 /* Nothing else is an assignment operator. */
6255 /* If it was an assignment operator, consume it. */
6256 if (op != ERROR_MARK)
6257 cp_lexer_consume_token (parser->lexer);
6262 /* Parse an expression.
6265 assignment-expression
6266 expression , assignment-expression
6268 CAST_P is true if this expression is the target of a cast.
6270 Returns a representation of the expression. */
6273 cp_parser_expression (cp_parser* parser, bool cast_p)
6275 tree expression = NULL_TREE;
6279 tree assignment_expression;
6281 /* Parse the next assignment-expression. */
6282 assignment_expression
6283 = cp_parser_assignment_expression (parser, cast_p);
6284 /* If this is the first assignment-expression, we can just
6287 expression = assignment_expression;
6289 expression = build_x_compound_expr (expression,
6290 assignment_expression);
6291 /* If the next token is not a comma, then we are done with the
6293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6295 /* Consume the `,'. */
6296 cp_lexer_consume_token (parser->lexer);
6297 /* A comma operator cannot appear in a constant-expression. */
6298 if (cp_parser_non_integral_constant_expression (parser,
6299 "a comma operator"))
6300 expression = error_mark_node;
6306 /* Parse a constant-expression.
6308 constant-expression:
6309 conditional-expression
6311 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6312 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6313 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6314 is false, NON_CONSTANT_P should be NULL. */
6317 cp_parser_constant_expression (cp_parser* parser,
6318 bool allow_non_constant_p,
6319 bool *non_constant_p)
6321 bool saved_integral_constant_expression_p;
6322 bool saved_allow_non_integral_constant_expression_p;
6323 bool saved_non_integral_constant_expression_p;
6326 /* It might seem that we could simply parse the
6327 conditional-expression, and then check to see if it were
6328 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6329 one that the compiler can figure out is constant, possibly after
6330 doing some simplifications or optimizations. The standard has a
6331 precise definition of constant-expression, and we must honor
6332 that, even though it is somewhat more restrictive.
6338 is not a legal declaration, because `(2, 3)' is not a
6339 constant-expression. The `,' operator is forbidden in a
6340 constant-expression. However, GCC's constant-folding machinery
6341 will fold this operation to an INTEGER_CST for `3'. */
6343 /* Save the old settings. */
6344 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6345 saved_allow_non_integral_constant_expression_p
6346 = parser->allow_non_integral_constant_expression_p;
6347 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6348 /* We are now parsing a constant-expression. */
6349 parser->integral_constant_expression_p = true;
6350 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6351 parser->non_integral_constant_expression_p = false;
6352 /* Although the grammar says "conditional-expression", we parse an
6353 "assignment-expression", which also permits "throw-expression"
6354 and the use of assignment operators. In the case that
6355 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6356 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6357 actually essential that we look for an assignment-expression.
6358 For example, cp_parser_initializer_clauses uses this function to
6359 determine whether a particular assignment-expression is in fact
6361 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6362 /* Restore the old settings. */
6363 parser->integral_constant_expression_p
6364 = saved_integral_constant_expression_p;
6365 parser->allow_non_integral_constant_expression_p
6366 = saved_allow_non_integral_constant_expression_p;
6367 if (allow_non_constant_p)
6368 *non_constant_p = parser->non_integral_constant_expression_p;
6369 else if (parser->non_integral_constant_expression_p)
6370 expression = error_mark_node;
6371 parser->non_integral_constant_expression_p
6372 = saved_non_integral_constant_expression_p;
6377 /* Parse __builtin_offsetof.
6379 offsetof-expression:
6380 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6382 offsetof-member-designator:
6384 | offsetof-member-designator "." id-expression
6385 | offsetof-member-designator "[" expression "]" */
6388 cp_parser_builtin_offsetof (cp_parser *parser)
6390 int save_ice_p, save_non_ice_p;
6394 /* We're about to accept non-integral-constant things, but will
6395 definitely yield an integral constant expression. Save and
6396 restore these values around our local parsing. */
6397 save_ice_p = parser->integral_constant_expression_p;
6398 save_non_ice_p = parser->non_integral_constant_expression_p;
6400 /* Consume the "__builtin_offsetof" token. */
6401 cp_lexer_consume_token (parser->lexer);
6402 /* Consume the opening `('. */
6403 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6404 /* Parse the type-id. */
6405 type = cp_parser_type_id (parser);
6406 /* Look for the `,'. */
6407 cp_parser_require (parser, CPP_COMMA, "`,'");
6409 /* Build the (type *)null that begins the traditional offsetof macro. */
6410 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6412 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6413 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6417 cp_token *token = cp_lexer_peek_token (parser->lexer);
6418 switch (token->type)
6420 case CPP_OPEN_SQUARE:
6421 /* offsetof-member-designator "[" expression "]" */
6422 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6426 /* offsetof-member-designator "." identifier */
6427 cp_lexer_consume_token (parser->lexer);
6428 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6432 case CPP_CLOSE_PAREN:
6433 /* Consume the ")" token. */
6434 cp_lexer_consume_token (parser->lexer);
6438 /* Error. We know the following require will fail, but
6439 that gives the proper error message. */
6440 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6441 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6442 expr = error_mark_node;
6448 /* If we're processing a template, we can't finish the semantics yet.
6449 Otherwise we can fold the entire expression now. */
6450 if (processing_template_decl)
6451 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6453 expr = finish_offsetof (expr);
6456 parser->integral_constant_expression_p = save_ice_p;
6457 parser->non_integral_constant_expression_p = save_non_ice_p;
6462 /* Parse a trait expression. */
6465 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6468 tree type1, type2 = NULL_TREE;
6469 bool binary = false;
6470 cp_decl_specifier_seq decl_specs;
6474 case RID_HAS_NOTHROW_ASSIGN:
6475 kind = CPTK_HAS_NOTHROW_ASSIGN;
6477 case RID_HAS_NOTHROW_CONSTRUCTOR:
6478 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6480 case RID_HAS_NOTHROW_COPY:
6481 kind = CPTK_HAS_NOTHROW_COPY;
6483 case RID_HAS_TRIVIAL_ASSIGN:
6484 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6486 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6487 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6489 case RID_HAS_TRIVIAL_COPY:
6490 kind = CPTK_HAS_TRIVIAL_COPY;
6492 case RID_HAS_TRIVIAL_DESTRUCTOR:
6493 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6495 case RID_HAS_VIRTUAL_DESTRUCTOR:
6496 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6498 case RID_IS_ABSTRACT:
6499 kind = CPTK_IS_ABSTRACT;
6501 case RID_IS_BASE_OF:
6502 kind = CPTK_IS_BASE_OF;
6506 kind = CPTK_IS_CLASS;
6508 case RID_IS_CONVERTIBLE_TO:
6509 kind = CPTK_IS_CONVERTIBLE_TO;
6513 kind = CPTK_IS_EMPTY;
6516 kind = CPTK_IS_ENUM;
6521 case RID_IS_POLYMORPHIC:
6522 kind = CPTK_IS_POLYMORPHIC;
6525 kind = CPTK_IS_UNION;
6531 /* Consume the token. */
6532 cp_lexer_consume_token (parser->lexer);
6534 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6536 type1 = cp_parser_type_id (parser);
6538 if (type1 == error_mark_node)
6539 return error_mark_node;
6541 /* Build a trivial decl-specifier-seq. */
6542 clear_decl_specs (&decl_specs);
6543 decl_specs.type = type1;
6545 /* Call grokdeclarator to figure out what type this is. */
6546 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6547 /*initialized=*/0, /*attrlist=*/NULL);
6551 cp_parser_require (parser, CPP_COMMA, "`,'");
6553 type2 = cp_parser_type_id (parser);
6555 if (type2 == 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 = type2;
6562 /* Call grokdeclarator to figure out what type this is. */
6563 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6564 /*initialized=*/0, /*attrlist=*/NULL);
6567 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6569 /* Complete the trait expression, which may mean either processing
6570 the trait expr now or saving it for template instantiation. */
6571 return finish_trait_expr (kind, type1, type2);
6574 /* Statements [gram.stmt.stmt] */
6576 /* Parse a statement.
6580 expression-statement
6585 declaration-statement
6588 IN_COMPOUND is true when the statement is nested inside a
6589 cp_parser_compound_statement; this matters for certain pragmas.
6591 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6592 is a (possibly labeled) if statement which is not enclosed in braces
6593 and has an else clause. This is used to implement -Wparentheses. */
6596 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6597 bool in_compound, bool *if_p)
6601 location_t statement_location;
6606 /* There is no statement yet. */
6607 statement = NULL_TREE;
6608 /* Peek at the next token. */
6609 token = cp_lexer_peek_token (parser->lexer);
6610 /* Remember the location of the first token in the statement. */
6611 statement_location = token->location;
6612 /* If this is a keyword, then that will often determine what kind of
6613 statement we have. */
6614 if (token->type == CPP_KEYWORD)
6616 enum rid keyword = token->keyword;
6622 /* Looks like a labeled-statement with a case label.
6623 Parse the label, and then use tail recursion to parse
6625 cp_parser_label_for_labeled_statement (parser);
6630 statement = cp_parser_selection_statement (parser, if_p);
6636 statement = cp_parser_iteration_statement (parser);
6643 statement = cp_parser_jump_statement (parser);
6646 /* Objective-C++ exception-handling constructs. */
6649 case RID_AT_FINALLY:
6650 case RID_AT_SYNCHRONIZED:
6652 statement = cp_parser_objc_statement (parser);
6656 statement = cp_parser_try_block (parser);
6660 /* This must be a namespace alias definition. */
6661 cp_parser_declaration_statement (parser);
6665 /* It might be a keyword like `int' that can start a
6666 declaration-statement. */
6670 else if (token->type == CPP_NAME)
6672 /* If the next token is a `:', then we are looking at a
6673 labeled-statement. */
6674 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6675 if (token->type == CPP_COLON)
6677 /* Looks like a labeled-statement with an ordinary label.
6678 Parse the label, and then use tail recursion to parse
6680 cp_parser_label_for_labeled_statement (parser);
6684 /* Anything that starts with a `{' must be a compound-statement. */
6685 else if (token->type == CPP_OPEN_BRACE)
6686 statement = cp_parser_compound_statement (parser, NULL, false);
6687 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6688 a statement all its own. */
6689 else if (token->type == CPP_PRAGMA)
6691 /* Only certain OpenMP pragmas are attached to statements, and thus
6692 are considered statements themselves. All others are not. In
6693 the context of a compound, accept the pragma as a "statement" and
6694 return so that we can check for a close brace. Otherwise we
6695 require a real statement and must go back and read one. */
6697 cp_parser_pragma (parser, pragma_compound);
6698 else if (!cp_parser_pragma (parser, pragma_stmt))
6702 else if (token->type == CPP_EOF)
6704 cp_parser_error (parser, "expected statement");
6708 /* Everything else must be a declaration-statement or an
6709 expression-statement. Try for the declaration-statement
6710 first, unless we are looking at a `;', in which case we know that
6711 we have an expression-statement. */
6714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6716 cp_parser_parse_tentatively (parser);
6717 /* Try to parse the declaration-statement. */
6718 cp_parser_declaration_statement (parser);
6719 /* If that worked, we're done. */
6720 if (cp_parser_parse_definitely (parser))
6723 /* Look for an expression-statement instead. */
6724 statement = cp_parser_expression_statement (parser, in_statement_expr);
6727 /* Set the line number for the statement. */
6728 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6729 SET_EXPR_LOCATION (statement, statement_location);
6732 /* Parse the label for a labeled-statement, i.e.
6735 case constant-expression :
6739 case constant-expression ... constant-expression : statement
6741 When a label is parsed without errors, the label is added to the
6742 parse tree by the finish_* functions, so this function doesn't
6743 have to return the label. */
6746 cp_parser_label_for_labeled_statement (cp_parser* parser)
6750 /* The next token should be an identifier. */
6751 token = cp_lexer_peek_token (parser->lexer);
6752 if (token->type != CPP_NAME
6753 && token->type != CPP_KEYWORD)
6755 cp_parser_error (parser, "expected labeled-statement");
6759 switch (token->keyword)
6766 /* Consume the `case' token. */
6767 cp_lexer_consume_token (parser->lexer);
6768 /* Parse the constant-expression. */
6769 expr = cp_parser_constant_expression (parser,
6770 /*allow_non_constant_p=*/false,
6773 ellipsis = cp_lexer_peek_token (parser->lexer);
6774 if (ellipsis->type == CPP_ELLIPSIS)
6776 /* Consume the `...' token. */
6777 cp_lexer_consume_token (parser->lexer);
6779 cp_parser_constant_expression (parser,
6780 /*allow_non_constant_p=*/false,
6782 /* We don't need to emit warnings here, as the common code
6783 will do this for us. */
6786 expr_hi = NULL_TREE;
6788 if (parser->in_switch_statement_p)
6789 finish_case_label (expr, expr_hi);
6791 error ("case label %qE not within a switch statement", expr);
6796 /* Consume the `default' token. */
6797 cp_lexer_consume_token (parser->lexer);
6799 if (parser->in_switch_statement_p)
6800 finish_case_label (NULL_TREE, NULL_TREE);
6802 error ("case label not within a switch statement");
6806 /* Anything else must be an ordinary label. */
6807 finish_label_stmt (cp_parser_identifier (parser));
6811 /* Require the `:' token. */
6812 cp_parser_require (parser, CPP_COLON, "`:'");
6815 /* Parse an expression-statement.
6817 expression-statement:
6820 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6821 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6822 indicates whether this expression-statement is part of an
6823 expression statement. */
6826 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6828 tree statement = NULL_TREE;
6830 /* If the next token is a ';', then there is no expression
6832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6833 statement = cp_parser_expression (parser, /*cast_p=*/false);
6835 /* Consume the final `;'. */
6836 cp_parser_consume_semicolon_at_end_of_statement (parser);
6838 if (in_statement_expr
6839 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6840 /* This is the final expression statement of a statement
6842 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6844 statement = finish_expr_stmt (statement);
6851 /* Parse a compound-statement.
6854 { statement-seq [opt] }
6859 { label-declaration-seq [opt] statement-seq [opt] }
6861 label-declaration-seq:
6863 label-declaration-seq label-declaration
6865 Returns a tree representing the statement. */
6868 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6873 /* Consume the `{'. */
6874 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6875 return error_mark_node;
6876 /* Begin the compound-statement. */
6877 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6878 /* If the next keyword is `__label__' we have a label declaration. */
6879 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6880 cp_parser_label_declaration (parser);
6881 /* Parse an (optional) statement-seq. */
6882 cp_parser_statement_seq_opt (parser, in_statement_expr);
6883 /* Finish the compound-statement. */
6884 finish_compound_stmt (compound_stmt);
6885 /* Consume the `}'. */
6886 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6888 return compound_stmt;
6891 /* Parse an (optional) statement-seq.
6895 statement-seq [opt] statement */
6898 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6900 /* Scan statements until there aren't any more. */
6903 cp_token *token = cp_lexer_peek_token (parser->lexer);
6905 /* If we're looking at a `}', then we've run out of statements. */
6906 if (token->type == CPP_CLOSE_BRACE
6907 || token->type == CPP_EOF
6908 || token->type == CPP_PRAGMA_EOL)
6911 /* If we are in a compound statement and find 'else' then
6912 something went wrong. */
6913 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6915 if (parser->in_statement & IN_IF_STMT)
6919 token = cp_lexer_consume_token (parser->lexer);
6920 error ("%<else%> without a previous %<if%>");
6924 /* Parse the statement. */
6925 cp_parser_statement (parser, in_statement_expr, true, NULL);
6929 /* Parse a selection-statement.
6931 selection-statement:
6932 if ( condition ) statement
6933 if ( condition ) statement else statement
6934 switch ( condition ) statement
6936 Returns the new IF_STMT or SWITCH_STMT.
6938 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6939 is a (possibly labeled) if statement which is not enclosed in
6940 braces and has an else clause. This is used to implement
6944 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6952 /* Peek at the next token. */
6953 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6955 /* See what kind of keyword it is. */
6956 keyword = token->keyword;
6965 /* Look for the `('. */
6966 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6968 cp_parser_skip_to_end_of_statement (parser);
6969 return error_mark_node;
6972 /* Begin the selection-statement. */
6973 if (keyword == RID_IF)
6974 statement = begin_if_stmt ();
6976 statement = begin_switch_stmt ();
6978 /* Parse the condition. */
6979 condition = cp_parser_condition (parser);
6980 /* Look for the `)'. */
6981 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6982 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6983 /*consume_paren=*/true);
6985 if (keyword == RID_IF)
6988 unsigned char in_statement;
6990 /* Add the condition. */
6991 finish_if_stmt_cond (condition, statement);
6993 /* Parse the then-clause. */
6994 in_statement = parser->in_statement;
6995 parser->in_statement |= IN_IF_STMT;
6996 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6997 parser->in_statement = in_statement;
6999 finish_then_clause (statement);
7001 /* If the next token is `else', parse the else-clause. */
7002 if (cp_lexer_next_token_is_keyword (parser->lexer,
7005 /* Consume the `else' keyword. */
7006 cp_lexer_consume_token (parser->lexer);
7007 begin_else_clause (statement);
7008 /* Parse the else-clause. */
7009 cp_parser_implicitly_scoped_statement (parser, NULL);
7010 finish_else_clause (statement);
7012 /* If we are currently parsing a then-clause, then
7013 IF_P will not be NULL. We set it to true to
7014 indicate that this if statement has an else clause.
7015 This may trigger the Wparentheses warning below
7016 when we get back up to the parent if statement. */
7022 /* This if statement does not have an else clause. If
7023 NESTED_IF is true, then the then-clause is an if
7024 statement which does have an else clause. We warn
7025 about the potential ambiguity. */
7027 warning (OPT_Wparentheses,
7028 ("%Hsuggest explicit braces "
7029 "to avoid ambiguous %<else%>"),
7030 EXPR_LOCUS (statement));
7033 /* Now we're all done with the if-statement. */
7034 finish_if_stmt (statement);
7038 bool in_switch_statement_p;
7039 unsigned char in_statement;
7041 /* Add the condition. */
7042 finish_switch_cond (condition, statement);
7044 /* Parse the body of the switch-statement. */
7045 in_switch_statement_p = parser->in_switch_statement_p;
7046 in_statement = parser->in_statement;
7047 parser->in_switch_statement_p = true;
7048 parser->in_statement |= IN_SWITCH_STMT;
7049 cp_parser_implicitly_scoped_statement (parser, NULL);
7050 parser->in_switch_statement_p = in_switch_statement_p;
7051 parser->in_statement = in_statement;
7053 /* Now we're all done with the switch-statement. */
7054 finish_switch_stmt (statement);
7062 cp_parser_error (parser, "expected selection-statement");
7063 return error_mark_node;
7067 /* Parse a condition.
7071 type-specifier-seq declarator = assignment-expression
7076 type-specifier-seq declarator asm-specification [opt]
7077 attributes [opt] = assignment-expression
7079 Returns the expression that should be tested. */
7082 cp_parser_condition (cp_parser* parser)
7084 cp_decl_specifier_seq type_specifiers;
7085 const char *saved_message;
7087 /* Try the declaration first. */
7088 cp_parser_parse_tentatively (parser);
7089 /* New types are not allowed in the type-specifier-seq for a
7091 saved_message = parser->type_definition_forbidden_message;
7092 parser->type_definition_forbidden_message
7093 = "types may not be defined in conditions";
7094 /* Parse the type-specifier-seq. */
7095 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7097 /* Restore the saved message. */
7098 parser->type_definition_forbidden_message = saved_message;
7099 /* If all is well, we might be looking at a declaration. */
7100 if (!cp_parser_error_occurred (parser))
7103 tree asm_specification;
7105 cp_declarator *declarator;
7106 tree initializer = NULL_TREE;
7108 /* Parse the declarator. */
7109 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7110 /*ctor_dtor_or_conv_p=*/NULL,
7111 /*parenthesized_p=*/NULL,
7112 /*member_p=*/false);
7113 /* Parse the attributes. */
7114 attributes = cp_parser_attributes_opt (parser);
7115 /* Parse the asm-specification. */
7116 asm_specification = cp_parser_asm_specification_opt (parser);
7117 /* If the next token is not an `=', then we might still be
7118 looking at an expression. For example:
7122 looks like a decl-specifier-seq and a declarator -- but then
7123 there is no `=', so this is an expression. */
7124 cp_parser_require (parser, CPP_EQ, "`='");
7125 /* If we did see an `=', then we are looking at a declaration
7127 if (cp_parser_parse_definitely (parser))
7130 bool non_constant_p;
7132 /* Create the declaration. */
7133 decl = start_decl (declarator, &type_specifiers,
7134 /*initialized_p=*/true,
7135 attributes, /*prefix_attributes=*/NULL_TREE,
7137 /* Parse the assignment-expression. */
7139 = cp_parser_constant_expression (parser,
7140 /*allow_non_constant_p=*/true,
7142 if (!non_constant_p)
7143 initializer = fold_non_dependent_expr (initializer);
7145 /* Process the initializer. */
7146 cp_finish_decl (decl,
7147 initializer, !non_constant_p,
7149 LOOKUP_ONLYCONVERTING);
7152 pop_scope (pushed_scope);
7154 return convert_from_reference (decl);
7157 /* If we didn't even get past the declarator successfully, we are
7158 definitely not looking at a declaration. */
7160 cp_parser_abort_tentative_parse (parser);
7162 /* Otherwise, we are looking at an expression. */
7163 return cp_parser_expression (parser, /*cast_p=*/false);
7166 /* We check for a ) immediately followed by ; with no whitespacing
7167 between. This is used to issue a warning for:
7175 as the semicolon is probably extraneous.
7177 On parse errors, the next token might not be a ), so do nothing in
7181 check_empty_body (cp_parser* parser, const char* type)
7184 cp_token *close_paren;
7185 expanded_location close_loc;
7186 expanded_location semi_loc;
7188 close_paren = cp_lexer_peek_token (parser->lexer);
7189 if (close_paren->type != CPP_CLOSE_PAREN)
7192 close_loc = expand_location (close_paren->location);
7193 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7195 if (token->type != CPP_SEMICOLON
7196 || (token->flags & PREV_WHITE))
7199 semi_loc = expand_location (token->location);
7200 if (close_loc.line == semi_loc.line
7201 && close_loc.column+1 == semi_loc.column)
7202 warning (OPT_Wempty_body,
7203 "suggest a space before %<;%> or explicit braces around empty "
7204 "body in %<%s%> statement",
7208 /* Parse an iteration-statement.
7210 iteration-statement:
7211 while ( condition ) statement
7212 do statement while ( expression ) ;
7213 for ( for-init-statement condition [opt] ; expression [opt] )
7216 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7219 cp_parser_iteration_statement (cp_parser* parser)
7224 unsigned char in_statement;
7226 /* Peek at the next token. */
7227 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7229 return error_mark_node;
7231 /* Remember whether or not we are already within an iteration
7233 in_statement = parser->in_statement;
7235 /* See what kind of keyword it is. */
7236 keyword = token->keyword;
7243 /* Begin the while-statement. */
7244 statement = begin_while_stmt ();
7245 /* Look for the `('. */
7246 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7247 /* Parse the condition. */
7248 condition = cp_parser_condition (parser);
7249 finish_while_stmt_cond (condition, statement);
7250 check_empty_body (parser, "while");
7251 /* Look for the `)'. */
7252 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7253 /* Parse the dependent statement. */
7254 parser->in_statement = IN_ITERATION_STMT;
7255 cp_parser_already_scoped_statement (parser);
7256 parser->in_statement = in_statement;
7257 /* We're done with the while-statement. */
7258 finish_while_stmt (statement);
7266 /* Begin the do-statement. */
7267 statement = begin_do_stmt ();
7268 /* Parse the body of the do-statement. */
7269 parser->in_statement = IN_ITERATION_STMT;
7270 cp_parser_implicitly_scoped_statement (parser, NULL);
7271 parser->in_statement = in_statement;
7272 finish_do_body (statement);
7273 /* Look for the `while' keyword. */
7274 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7275 /* Look for the `('. */
7276 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7277 /* Parse the expression. */
7278 expression = cp_parser_expression (parser, /*cast_p=*/false);
7279 /* We're done with the do-statement. */
7280 finish_do_stmt (expression, statement);
7281 /* Look for the `)'. */
7282 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7283 /* Look for the `;'. */
7284 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7290 tree condition = NULL_TREE;
7291 tree expression = NULL_TREE;
7293 /* Begin the for-statement. */
7294 statement = begin_for_stmt ();
7295 /* Look for the `('. */
7296 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7297 /* Parse the initialization. */
7298 cp_parser_for_init_statement (parser);
7299 finish_for_init_stmt (statement);
7301 /* If there's a condition, process it. */
7302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7303 condition = cp_parser_condition (parser);
7304 finish_for_cond (condition, statement);
7305 /* Look for the `;'. */
7306 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7308 /* If there's an expression, process it. */
7309 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7310 expression = cp_parser_expression (parser, /*cast_p=*/false);
7311 finish_for_expr (expression, statement);
7312 check_empty_body (parser, "for");
7313 /* Look for the `)'. */
7314 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7316 /* Parse the body of the for-statement. */
7317 parser->in_statement = IN_ITERATION_STMT;
7318 cp_parser_already_scoped_statement (parser);
7319 parser->in_statement = in_statement;
7321 /* We're done with the for-statement. */
7322 finish_for_stmt (statement);
7327 cp_parser_error (parser, "expected iteration-statement");
7328 statement = error_mark_node;
7335 /* Parse a for-init-statement.
7338 expression-statement
7339 simple-declaration */
7342 cp_parser_for_init_statement (cp_parser* parser)
7344 /* If the next token is a `;', then we have an empty
7345 expression-statement. Grammatically, this is also a
7346 simple-declaration, but an invalid one, because it does not
7347 declare anything. Therefore, if we did not handle this case
7348 specially, we would issue an error message about an invalid
7350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7352 /* We're going to speculatively look for a declaration, falling back
7353 to an expression, if necessary. */
7354 cp_parser_parse_tentatively (parser);
7355 /* Parse the declaration. */
7356 cp_parser_simple_declaration (parser,
7357 /*function_definition_allowed_p=*/false);
7358 /* If the tentative parse failed, then we shall need to look for an
7359 expression-statement. */
7360 if (cp_parser_parse_definitely (parser))
7364 cp_parser_expression_statement (parser, false);
7367 /* Parse a jump-statement.
7372 return expression [opt] ;
7380 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7383 cp_parser_jump_statement (cp_parser* parser)
7385 tree statement = error_mark_node;
7388 unsigned char in_statement;
7390 /* Peek at the next token. */
7391 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7393 return error_mark_node;
7395 /* See what kind of keyword it is. */
7396 keyword = token->keyword;
7400 in_statement = parser->in_statement & ~IN_IF_STMT;
7401 switch (in_statement)
7404 error ("break statement not within loop or switch");
7407 gcc_assert ((in_statement & IN_SWITCH_STMT)
7408 || in_statement == IN_ITERATION_STMT);
7409 statement = finish_break_stmt ();
7412 error ("invalid exit from OpenMP structured block");
7415 error ("break statement used with OpenMP for loop");
7418 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7422 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7425 error ("continue statement not within a loop");
7427 case IN_ITERATION_STMT:
7429 statement = finish_continue_stmt ();
7432 error ("invalid exit from OpenMP structured block");
7437 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7444 /* If the next token is a `;', then there is no
7446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7447 expr = cp_parser_expression (parser, /*cast_p=*/false);
7450 /* Build the return-statement. */
7451 statement = finish_return_stmt (expr);
7452 /* Look for the final `;'. */
7453 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7458 /* Create the goto-statement. */
7459 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7461 /* Issue a warning about this use of a GNU extension. */
7463 pedwarn ("ISO C++ forbids computed gotos");
7464 /* Consume the '*' token. */
7465 cp_lexer_consume_token (parser->lexer);
7466 /* Parse the dependent expression. */
7467 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7470 finish_goto_stmt (cp_parser_identifier (parser));
7471 /* Look for the final `;'. */
7472 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7476 cp_parser_error (parser, "expected jump-statement");
7483 /* Parse a declaration-statement.
7485 declaration-statement:
7486 block-declaration */
7489 cp_parser_declaration_statement (cp_parser* parser)
7493 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7494 p = obstack_alloc (&declarator_obstack, 0);
7496 /* Parse the block-declaration. */
7497 cp_parser_block_declaration (parser, /*statement_p=*/true);
7499 /* Free any declarators allocated. */
7500 obstack_free (&declarator_obstack, p);
7502 /* Finish off the statement. */
7506 /* Some dependent statements (like `if (cond) statement'), are
7507 implicitly in their own scope. In other words, if the statement is
7508 a single statement (as opposed to a compound-statement), it is
7509 none-the-less treated as if it were enclosed in braces. Any
7510 declarations appearing in the dependent statement are out of scope
7511 after control passes that point. This function parses a statement,
7512 but ensures that is in its own scope, even if it is not a
7515 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7516 is a (possibly labeled) if statement which is not enclosed in
7517 braces and has an else clause. This is used to implement
7520 Returns the new statement. */
7523 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7530 /* Mark if () ; with a special NOP_EXPR. */
7531 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7533 cp_lexer_consume_token (parser->lexer);
7534 statement = add_stmt (build_empty_stmt ());
7536 /* if a compound is opened, we simply parse the statement directly. */
7537 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7538 statement = cp_parser_compound_statement (parser, NULL, false);
7539 /* If the token is not a `{', then we must take special action. */
7542 /* Create a compound-statement. */
7543 statement = begin_compound_stmt (0);
7544 /* Parse the dependent-statement. */
7545 cp_parser_statement (parser, NULL_TREE, false, if_p);
7546 /* Finish the dummy compound-statement. */
7547 finish_compound_stmt (statement);
7550 /* Return the statement. */
7554 /* For some dependent statements (like `while (cond) statement'), we
7555 have already created a scope. Therefore, even if the dependent
7556 statement is a compound-statement, we do not want to create another
7560 cp_parser_already_scoped_statement (cp_parser* parser)
7562 /* If the token is a `{', then we must take special action. */
7563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7564 cp_parser_statement (parser, NULL_TREE, false, NULL);
7567 /* Avoid calling cp_parser_compound_statement, so that we
7568 don't create a new scope. Do everything else by hand. */
7569 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7570 cp_parser_statement_seq_opt (parser, NULL_TREE);
7571 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7575 /* Declarations [gram.dcl.dcl] */
7577 /* Parse an optional declaration-sequence.
7581 declaration-seq declaration */
7584 cp_parser_declaration_seq_opt (cp_parser* parser)
7590 token = cp_lexer_peek_token (parser->lexer);
7592 if (token->type == CPP_CLOSE_BRACE
7593 || token->type == CPP_EOF
7594 || token->type == CPP_PRAGMA_EOL)
7597 if (token->type == CPP_SEMICOLON)
7599 /* A declaration consisting of a single semicolon is
7600 invalid. Allow it unless we're being pedantic. */
7601 cp_lexer_consume_token (parser->lexer);
7602 if (pedantic && !in_system_header)
7603 pedwarn ("extra %<;%>");
7607 /* If we're entering or exiting a region that's implicitly
7608 extern "C", modify the lang context appropriately. */
7609 if (!parser->implicit_extern_c && token->implicit_extern_c)
7611 push_lang_context (lang_name_c);
7612 parser->implicit_extern_c = true;
7614 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7616 pop_lang_context ();
7617 parser->implicit_extern_c = false;
7620 if (token->type == CPP_PRAGMA)
7622 /* A top-level declaration can consist solely of a #pragma.
7623 A nested declaration cannot, so this is done here and not
7624 in cp_parser_declaration. (A #pragma at block scope is
7625 handled in cp_parser_statement.) */
7626 cp_parser_pragma (parser, pragma_external);
7630 /* Parse the declaration itself. */
7631 cp_parser_declaration (parser);
7635 /* Parse a declaration.
7640 template-declaration
7641 explicit-instantiation
7642 explicit-specialization
7643 linkage-specification
7644 namespace-definition
7649 __extension__ declaration */
7652 cp_parser_declaration (cp_parser* parser)
7659 /* Check for the `__extension__' keyword. */
7660 if (cp_parser_extension_opt (parser, &saved_pedantic))
7662 /* Parse the qualified declaration. */
7663 cp_parser_declaration (parser);
7664 /* Restore the PEDANTIC flag. */
7665 pedantic = saved_pedantic;
7670 /* Try to figure out what kind of declaration is present. */
7671 token1 = *cp_lexer_peek_token (parser->lexer);
7673 if (token1.type != CPP_EOF)
7674 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7677 token2.type = CPP_EOF;
7678 token2.keyword = RID_MAX;
7681 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7682 p = obstack_alloc (&declarator_obstack, 0);
7684 /* If the next token is `extern' and the following token is a string
7685 literal, then we have a linkage specification. */
7686 if (token1.keyword == RID_EXTERN
7687 && cp_parser_is_string_literal (&token2))
7688 cp_parser_linkage_specification (parser);
7689 /* If the next token is `template', then we have either a template
7690 declaration, an explicit instantiation, or an explicit
7692 else if (token1.keyword == RID_TEMPLATE)
7694 /* `template <>' indicates a template specialization. */
7695 if (token2.type == CPP_LESS
7696 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7697 cp_parser_explicit_specialization (parser);
7698 /* `template <' indicates a template declaration. */
7699 else if (token2.type == CPP_LESS)
7700 cp_parser_template_declaration (parser, /*member_p=*/false);
7701 /* Anything else must be an explicit instantiation. */
7703 cp_parser_explicit_instantiation (parser);
7705 /* If the next token is `export', then we have a template
7707 else if (token1.keyword == RID_EXPORT)
7708 cp_parser_template_declaration (parser, /*member_p=*/false);
7709 /* If the next token is `extern', 'static' or 'inline' and the one
7710 after that is `template', we have a GNU extended explicit
7711 instantiation directive. */
7712 else if (cp_parser_allow_gnu_extensions_p (parser)
7713 && (token1.keyword == RID_EXTERN
7714 || token1.keyword == RID_STATIC
7715 || token1.keyword == RID_INLINE)
7716 && token2.keyword == RID_TEMPLATE)
7717 cp_parser_explicit_instantiation (parser);
7718 /* If the next token is `namespace', check for a named or unnamed
7719 namespace definition. */
7720 else if (token1.keyword == RID_NAMESPACE
7721 && (/* A named namespace definition. */
7722 (token2.type == CPP_NAME
7723 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7725 /* An unnamed namespace definition. */
7726 || token2.type == CPP_OPEN_BRACE
7727 || token2.keyword == RID_ATTRIBUTE))
7728 cp_parser_namespace_definition (parser);
7729 /* An inline (associated) namespace definition. */
7730 else if (token1.keyword == RID_INLINE
7731 && token2.keyword == RID_NAMESPACE)
7732 cp_parser_namespace_definition (parser);
7733 /* Objective-C++ declaration/definition. */
7734 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7735 cp_parser_objc_declaration (parser);
7736 /* We must have either a block declaration or a function
7739 /* Try to parse a block-declaration, or a function-definition. */
7740 cp_parser_block_declaration (parser, /*statement_p=*/false);
7742 /* Free any declarators allocated. */
7743 obstack_free (&declarator_obstack, p);
7746 /* Parse a block-declaration.
7751 namespace-alias-definition
7758 __extension__ block-declaration
7763 static_assert-declaration
7765 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7766 part of a declaration-statement. */
7769 cp_parser_block_declaration (cp_parser *parser,
7775 /* Check for the `__extension__' keyword. */
7776 if (cp_parser_extension_opt (parser, &saved_pedantic))
7778 /* Parse the qualified declaration. */
7779 cp_parser_block_declaration (parser, statement_p);
7780 /* Restore the PEDANTIC flag. */
7781 pedantic = saved_pedantic;
7786 /* Peek at the next token to figure out which kind of declaration is
7788 token1 = cp_lexer_peek_token (parser->lexer);
7790 /* If the next keyword is `asm', we have an asm-definition. */
7791 if (token1->keyword == RID_ASM)
7794 cp_parser_commit_to_tentative_parse (parser);
7795 cp_parser_asm_definition (parser);
7797 /* If the next keyword is `namespace', we have a
7798 namespace-alias-definition. */
7799 else if (token1->keyword == RID_NAMESPACE)
7800 cp_parser_namespace_alias_definition (parser);
7801 /* If the next keyword is `using', we have either a
7802 using-declaration or a using-directive. */
7803 else if (token1->keyword == RID_USING)
7808 cp_parser_commit_to_tentative_parse (parser);
7809 /* If the token after `using' is `namespace', then we have a
7811 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7812 if (token2->keyword == RID_NAMESPACE)
7813 cp_parser_using_directive (parser);
7814 /* Otherwise, it's a using-declaration. */
7816 cp_parser_using_declaration (parser,
7817 /*access_declaration_p=*/false);
7819 /* If the next keyword is `__label__' we have a misplaced label
7821 else if (token1->keyword == RID_LABEL)
7823 cp_lexer_consume_token (parser->lexer);
7824 error ("%<__label__%> not at the beginning of a block");
7825 cp_parser_skip_to_end_of_statement (parser);
7826 /* If the next token is now a `;', consume it. */
7827 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7828 cp_lexer_consume_token (parser->lexer);
7830 /* If the next token is `static_assert' we have a static assertion. */
7831 else if (token1->keyword == RID_STATIC_ASSERT)
7832 cp_parser_static_assert (parser, /*member_p=*/false);
7833 /* Anything else must be a simple-declaration. */
7835 cp_parser_simple_declaration (parser, !statement_p);
7838 /* Parse a simple-declaration.
7841 decl-specifier-seq [opt] init-declarator-list [opt] ;
7843 init-declarator-list:
7845 init-declarator-list , init-declarator
7847 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7848 function-definition as a simple-declaration. */
7851 cp_parser_simple_declaration (cp_parser* parser,
7852 bool function_definition_allowed_p)
7854 cp_decl_specifier_seq decl_specifiers;
7855 int declares_class_or_enum;
7856 bool saw_declarator;
7858 /* Defer access checks until we know what is being declared; the
7859 checks for names appearing in the decl-specifier-seq should be
7860 done as if we were in the scope of the thing being declared. */
7861 push_deferring_access_checks (dk_deferred);
7863 /* Parse the decl-specifier-seq. We have to keep track of whether
7864 or not the decl-specifier-seq declares a named class or
7865 enumeration type, since that is the only case in which the
7866 init-declarator-list is allowed to be empty.
7870 In a simple-declaration, the optional init-declarator-list can be
7871 omitted only when declaring a class or enumeration, that is when
7872 the decl-specifier-seq contains either a class-specifier, an
7873 elaborated-type-specifier, or an enum-specifier. */
7874 cp_parser_decl_specifier_seq (parser,
7875 CP_PARSER_FLAGS_OPTIONAL,
7877 &declares_class_or_enum);
7878 /* We no longer need to defer access checks. */
7879 stop_deferring_access_checks ();
7881 /* In a block scope, a valid declaration must always have a
7882 decl-specifier-seq. By not trying to parse declarators, we can
7883 resolve the declaration/expression ambiguity more quickly. */
7884 if (!function_definition_allowed_p
7885 && !decl_specifiers.any_specifiers_p)
7887 cp_parser_error (parser, "expected declaration");
7891 /* If the next two tokens are both identifiers, the code is
7892 erroneous. The usual cause of this situation is code like:
7896 where "T" should name a type -- but does not. */
7897 if (!decl_specifiers.type
7898 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7900 /* If parsing tentatively, we should commit; we really are
7901 looking at a declaration. */
7902 cp_parser_commit_to_tentative_parse (parser);
7907 /* If we have seen at least one decl-specifier, and the next token
7908 is not a parenthesis, then we must be looking at a declaration.
7909 (After "int (" we might be looking at a functional cast.) */
7910 if (decl_specifiers.any_specifiers_p
7911 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7912 cp_parser_commit_to_tentative_parse (parser);
7914 /* Keep going until we hit the `;' at the end of the simple
7916 saw_declarator = false;
7917 while (cp_lexer_next_token_is_not (parser->lexer,
7921 bool function_definition_p;
7926 /* If we are processing next declarator, coma is expected */
7927 token = cp_lexer_peek_token (parser->lexer);
7928 gcc_assert (token->type == CPP_COMMA);
7929 cp_lexer_consume_token (parser->lexer);
7932 saw_declarator = true;
7934 /* Parse the init-declarator. */
7935 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7937 function_definition_allowed_p,
7939 declares_class_or_enum,
7940 &function_definition_p);
7941 /* If an error occurred while parsing tentatively, exit quickly.
7942 (That usually happens when in the body of a function; each
7943 statement is treated as a declaration-statement until proven
7945 if (cp_parser_error_occurred (parser))
7947 /* Handle function definitions specially. */
7948 if (function_definition_p)
7950 /* If the next token is a `,', then we are probably
7951 processing something like:
7955 which is erroneous. */
7956 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7957 error ("mixing declarations and function-definitions is forbidden");
7958 /* Otherwise, we're done with the list of declarators. */
7961 pop_deferring_access_checks ();
7965 /* The next token should be either a `,' or a `;'. */
7966 token = cp_lexer_peek_token (parser->lexer);
7967 /* If it's a `,', there are more declarators to come. */
7968 if (token->type == CPP_COMMA)
7969 /* will be consumed next time around */;
7970 /* If it's a `;', we are done. */
7971 else if (token->type == CPP_SEMICOLON)
7973 /* Anything else is an error. */
7976 /* If we have already issued an error message we don't need
7977 to issue another one. */
7978 if (decl != error_mark_node
7979 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7980 cp_parser_error (parser, "expected %<,%> or %<;%>");
7981 /* Skip tokens until we reach the end of the statement. */
7982 cp_parser_skip_to_end_of_statement (parser);
7983 /* If the next token is now a `;', consume it. */
7984 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7985 cp_lexer_consume_token (parser->lexer);
7988 /* After the first time around, a function-definition is not
7989 allowed -- even if it was OK at first. For example:
7994 function_definition_allowed_p = false;
7997 /* Issue an error message if no declarators are present, and the
7998 decl-specifier-seq does not itself declare a class or
8000 if (!saw_declarator)
8002 if (cp_parser_declares_only_class_p (parser))
8003 shadow_tag (&decl_specifiers);
8004 /* Perform any deferred access checks. */
8005 perform_deferred_access_checks ();
8008 /* Consume the `;'. */
8009 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8012 pop_deferring_access_checks ();
8015 /* Parse a decl-specifier-seq.
8018 decl-specifier-seq [opt] decl-specifier
8021 storage-class-specifier
8032 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8034 The parser flags FLAGS is used to control type-specifier parsing.
8036 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8039 1: one of the decl-specifiers is an elaborated-type-specifier
8040 (i.e., a type declaration)
8041 2: one of the decl-specifiers is an enum-specifier or a
8042 class-specifier (i.e., a type definition)
8047 cp_parser_decl_specifier_seq (cp_parser* parser,
8048 cp_parser_flags flags,
8049 cp_decl_specifier_seq *decl_specs,
8050 int* declares_class_or_enum)
8052 bool constructor_possible_p = !parser->in_declarator_p;
8054 /* Clear DECL_SPECS. */
8055 clear_decl_specs (decl_specs);
8057 /* Assume no class or enumeration type is declared. */
8058 *declares_class_or_enum = 0;
8060 /* Keep reading specifiers until there are no more to read. */
8064 bool found_decl_spec;
8067 /* Peek at the next token. */
8068 token = cp_lexer_peek_token (parser->lexer);
8069 /* Handle attributes. */
8070 if (token->keyword == RID_ATTRIBUTE)
8072 /* Parse the attributes. */
8073 decl_specs->attributes
8074 = chainon (decl_specs->attributes,
8075 cp_parser_attributes_opt (parser));
8078 /* Assume we will find a decl-specifier keyword. */
8079 found_decl_spec = true;
8080 /* If the next token is an appropriate keyword, we can simply
8081 add it to the list. */
8082 switch (token->keyword)
8087 if (!at_class_scope_p ())
8089 error ("%<friend%> used outside of class");
8090 cp_lexer_purge_token (parser->lexer);
8094 ++decl_specs->specs[(int) ds_friend];
8095 /* Consume the token. */
8096 cp_lexer_consume_token (parser->lexer);
8100 /* function-specifier:
8107 cp_parser_function_specifier_opt (parser, decl_specs);
8113 ++decl_specs->specs[(int) ds_typedef];
8114 /* Consume the token. */
8115 cp_lexer_consume_token (parser->lexer);
8116 /* A constructor declarator cannot appear in a typedef. */
8117 constructor_possible_p = false;
8118 /* The "typedef" keyword can only occur in a declaration; we
8119 may as well commit at this point. */
8120 cp_parser_commit_to_tentative_parse (parser);
8122 if (decl_specs->storage_class != sc_none)
8123 decl_specs->conflicting_specifiers_p = true;
8126 /* storage-class-specifier:
8140 /* Consume the token. */
8141 cp_lexer_consume_token (parser->lexer);
8142 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8145 /* Consume the token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 ++decl_specs->specs[(int) ds_thread];
8151 /* We did not yet find a decl-specifier yet. */
8152 found_decl_spec = false;
8156 /* Constructors are a special case. The `S' in `S()' is not a
8157 decl-specifier; it is the beginning of the declarator. */
8160 && constructor_possible_p
8161 && (cp_parser_constructor_declarator_p
8162 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8164 /* If we don't have a DECL_SPEC yet, then we must be looking at
8165 a type-specifier. */
8166 if (!found_decl_spec && !constructor_p)
8168 int decl_spec_declares_class_or_enum;
8169 bool is_cv_qualifier;
8173 = cp_parser_type_specifier (parser, flags,
8175 /*is_declaration=*/true,
8176 &decl_spec_declares_class_or_enum,
8179 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8181 /* If this type-specifier referenced a user-defined type
8182 (a typedef, class-name, etc.), then we can't allow any
8183 more such type-specifiers henceforth.
8187 The longest sequence of decl-specifiers that could
8188 possibly be a type name is taken as the
8189 decl-specifier-seq of a declaration. The sequence shall
8190 be self-consistent as described below.
8194 As a general rule, at most one type-specifier is allowed
8195 in the complete decl-specifier-seq of a declaration. The
8196 only exceptions are the following:
8198 -- const or volatile can be combined with any other
8201 -- signed or unsigned can be combined with char, long,
8209 void g (const int Pc);
8211 Here, Pc is *not* part of the decl-specifier seq; it's
8212 the declarator. Therefore, once we see a type-specifier
8213 (other than a cv-qualifier), we forbid any additional
8214 user-defined types. We *do* still allow things like `int
8215 int' to be considered a decl-specifier-seq, and issue the
8216 error message later. */
8217 if (type_spec && !is_cv_qualifier)
8218 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8219 /* A constructor declarator cannot follow a type-specifier. */
8222 constructor_possible_p = false;
8223 found_decl_spec = true;
8227 /* If we still do not have a DECL_SPEC, then there are no more
8229 if (!found_decl_spec)
8232 decl_specs->any_specifiers_p = true;
8233 /* After we see one decl-specifier, further decl-specifiers are
8235 flags |= CP_PARSER_FLAGS_OPTIONAL;
8238 cp_parser_check_decl_spec (decl_specs);
8240 /* Don't allow a friend specifier with a class definition. */
8241 if (decl_specs->specs[(int) ds_friend] != 0
8242 && (*declares_class_or_enum & 2))
8243 error ("class definition may not be declared a friend");
8246 /* Parse an (optional) storage-class-specifier.
8248 storage-class-specifier:
8257 storage-class-specifier:
8260 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8263 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8265 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8273 /* Consume the token. */
8274 return cp_lexer_consume_token (parser->lexer)->u.value;
8281 /* Parse an (optional) function-specifier.
8288 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8289 Updates DECL_SPECS, if it is non-NULL. */
8292 cp_parser_function_specifier_opt (cp_parser* parser,
8293 cp_decl_specifier_seq *decl_specs)
8295 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8299 ++decl_specs->specs[(int) ds_inline];
8303 /* 14.5.2.3 [temp.mem]
8305 A member function template shall not be virtual. */
8306 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8307 error ("templates may not be %<virtual%>");
8308 else if (decl_specs)
8309 ++decl_specs->specs[(int) ds_virtual];
8314 ++decl_specs->specs[(int) ds_explicit];
8321 /* Consume the token. */
8322 return cp_lexer_consume_token (parser->lexer)->u.value;
8325 /* Parse a linkage-specification.
8327 linkage-specification:
8328 extern string-literal { declaration-seq [opt] }
8329 extern string-literal declaration */
8332 cp_parser_linkage_specification (cp_parser* parser)
8336 /* Look for the `extern' keyword. */
8337 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8339 /* Look for the string-literal. */
8340 linkage = cp_parser_string_literal (parser, false, false);
8342 /* Transform the literal into an identifier. If the literal is a
8343 wide-character string, or contains embedded NULs, then we can't
8344 handle it as the user wants. */
8345 if (strlen (TREE_STRING_POINTER (linkage))
8346 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8348 cp_parser_error (parser, "invalid linkage-specification");
8349 /* Assume C++ linkage. */
8350 linkage = lang_name_cplusplus;
8353 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8355 /* We're now using the new linkage. */
8356 push_lang_context (linkage);
8358 /* If the next token is a `{', then we're using the first
8360 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8362 /* Consume the `{' token. */
8363 cp_lexer_consume_token (parser->lexer);
8364 /* Parse the declarations. */
8365 cp_parser_declaration_seq_opt (parser);
8366 /* Look for the closing `}'. */
8367 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8369 /* Otherwise, there's just one declaration. */
8372 bool saved_in_unbraced_linkage_specification_p;
8374 saved_in_unbraced_linkage_specification_p
8375 = parser->in_unbraced_linkage_specification_p;
8376 parser->in_unbraced_linkage_specification_p = true;
8377 cp_parser_declaration (parser);
8378 parser->in_unbraced_linkage_specification_p
8379 = saved_in_unbraced_linkage_specification_p;
8382 /* We're done with the linkage-specification. */
8383 pop_lang_context ();
8386 /* Parse a static_assert-declaration.
8388 static_assert-declaration:
8389 static_assert ( constant-expression , string-literal ) ;
8391 If MEMBER_P, this static_assert is a class member. */
8394 cp_parser_static_assert(cp_parser *parser, bool member_p)
8399 location_t saved_loc;
8401 /* Peek at the `static_assert' token so we can keep track of exactly
8402 where the static assertion started. */
8403 token = cp_lexer_peek_token (parser->lexer);
8404 saved_loc = token->location;
8406 /* Look for the `static_assert' keyword. */
8407 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8411 /* We know we are in a static assertion; commit to any tentative
8413 if (cp_parser_parsing_tentatively (parser))
8414 cp_parser_commit_to_tentative_parse (parser);
8416 /* Parse the `(' starting the static assertion condition. */
8417 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8419 /* Parse the constant-expression. */
8421 cp_parser_constant_expression (parser,
8422 /*allow_non_constant_p=*/false,
8423 /*non_constant_p=*/NULL);
8425 /* Parse the separating `,'. */
8426 cp_parser_require (parser, CPP_COMMA, "`,'");
8428 /* Parse the string-literal message. */
8429 message = cp_parser_string_literal (parser,
8430 /*translate=*/false,
8433 /* A `)' completes the static assertion. */
8434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8435 cp_parser_skip_to_closing_parenthesis (parser,
8436 /*recovering=*/true,
8438 /*consume_paren=*/true);
8440 /* A semicolon terminates the declaration. */
8441 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8443 /* Complete the static assertion, which may mean either processing
8444 the static assert now or saving it for template instantiation. */
8445 finish_static_assert (condition, message, saved_loc, member_p);
8448 /* Parse a `decltype' type. Returns the type.
8450 simple-type-specifier:
8451 decltype ( expression ) */
8454 cp_parser_decltype (cp_parser *parser)
8457 bool id_expression_or_member_access_p = false;
8458 const char *saved_message;
8459 bool saved_integral_constant_expression_p;
8460 bool saved_non_integral_constant_expression_p;
8462 /* Look for the `decltype' token. */
8463 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8464 return error_mark_node;
8466 /* Types cannot be defined in a `decltype' expression. Save away the
8468 saved_message = parser->type_definition_forbidden_message;
8470 /* And create the new one. */
8471 parser->type_definition_forbidden_message
8472 = "types may not be defined in `decltype' expressions";
8474 /* The restrictions on constant-expressions do not apply inside
8475 decltype expressions. */
8476 saved_integral_constant_expression_p
8477 = parser->integral_constant_expression_p;
8478 saved_non_integral_constant_expression_p
8479 = parser->non_integral_constant_expression_p;
8480 parser->integral_constant_expression_p = false;
8482 /* Do not actually evaluate the expression. */
8485 /* Parse the opening `('. */
8486 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8487 return error_mark_node;
8489 /* First, try parsing an id-expression. */
8490 cp_parser_parse_tentatively (parser);
8491 expr = cp_parser_id_expression (parser,
8492 /*template_keyword_p=*/false,
8493 /*check_dependency_p=*/true,
8494 /*template_p=*/NULL,
8495 /*declarator_p=*/false,
8496 /*optional_p=*/false);
8498 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8500 bool non_integral_constant_expression_p = false;
8501 tree id_expression = expr;
8503 const char *error_msg;
8505 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8506 /* Lookup the name we got back from the id-expression. */
8507 expr = cp_parser_lookup_name (parser, expr,
8509 /*is_template=*/false,
8510 /*is_namespace=*/false,
8511 /*check_dependency=*/true,
8512 /*ambiguous_decls=*/NULL);
8515 && expr != error_mark_node
8516 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8517 && TREE_CODE (expr) != TYPE_DECL
8518 && (TREE_CODE (expr) != BIT_NOT_EXPR
8519 || !TYPE_P (TREE_OPERAND (expr, 0)))
8520 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8522 /* Complete lookup of the id-expression. */
8523 expr = (finish_id_expression
8524 (id_expression, expr, parser->scope, &idk,
8525 /*integral_constant_expression_p=*/false,
8526 /*allow_non_integral_constant_expression_p=*/true,
8527 &non_integral_constant_expression_p,
8528 /*template_p=*/false,
8530 /*address_p=*/false,
8531 /*template_arg_p=*/false,
8534 if (expr == error_mark_node)
8535 /* We found an id-expression, but it was something that we
8536 should not have found. This is an error, not something
8537 we can recover from, so note that we found an
8538 id-expression and we'll recover as gracefully as
8540 id_expression_or_member_access_p = true;
8544 && expr != error_mark_node
8545 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8546 /* We have an id-expression. */
8547 id_expression_or_member_access_p = true;
8550 if (!id_expression_or_member_access_p)
8552 /* Abort the id-expression parse. */
8553 cp_parser_abort_tentative_parse (parser);
8555 /* Parsing tentatively, again. */
8556 cp_parser_parse_tentatively (parser);
8558 /* Parse a class member access. */
8559 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8561 /*member_access_only_p=*/true);
8564 && expr != error_mark_node
8565 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8566 /* We have an id-expression. */
8567 id_expression_or_member_access_p = true;
8570 if (id_expression_or_member_access_p)
8571 /* We have parsed the complete id-expression or member access. */
8572 cp_parser_parse_definitely (parser);
8575 /* Abort our attempt to parse an id-expression or member access
8577 cp_parser_abort_tentative_parse (parser);
8579 /* Parse a full expression. */
8580 expr = cp_parser_expression (parser, /*cast_p=*/false);
8583 /* Go back to evaluating expressions. */
8586 /* Restore the old message and the integral constant expression
8588 parser->type_definition_forbidden_message = saved_message;
8589 parser->integral_constant_expression_p
8590 = saved_integral_constant_expression_p;
8591 parser->non_integral_constant_expression_p
8592 = saved_non_integral_constant_expression_p;
8594 if (expr == error_mark_node)
8596 /* Skip everything up to the closing `)'. */
8597 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8598 /*consume_paren=*/true);
8599 return error_mark_node;
8602 /* Parse to the closing `)'. */
8603 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8605 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8606 /*consume_paren=*/true);
8607 return error_mark_node;
8610 return finish_decltype_type (expr, id_expression_or_member_access_p);
8613 /* Special member functions [gram.special] */
8615 /* Parse a conversion-function-id.
8617 conversion-function-id:
8618 operator conversion-type-id
8620 Returns an IDENTIFIER_NODE representing the operator. */
8623 cp_parser_conversion_function_id (cp_parser* parser)
8627 tree saved_qualifying_scope;
8628 tree saved_object_scope;
8629 tree pushed_scope = NULL_TREE;
8631 /* Look for the `operator' token. */
8632 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8633 return error_mark_node;
8634 /* When we parse the conversion-type-id, the current scope will be
8635 reset. However, we need that information in able to look up the
8636 conversion function later, so we save it here. */
8637 saved_scope = parser->scope;
8638 saved_qualifying_scope = parser->qualifying_scope;
8639 saved_object_scope = parser->object_scope;
8640 /* We must enter the scope of the class so that the names of
8641 entities declared within the class are available in the
8642 conversion-type-id. For example, consider:
8649 S::operator I() { ... }
8651 In order to see that `I' is a type-name in the definition, we
8652 must be in the scope of `S'. */
8654 pushed_scope = push_scope (saved_scope);
8655 /* Parse the conversion-type-id. */
8656 type = cp_parser_conversion_type_id (parser);
8657 /* Leave the scope of the class, if any. */
8659 pop_scope (pushed_scope);
8660 /* Restore the saved scope. */
8661 parser->scope = saved_scope;
8662 parser->qualifying_scope = saved_qualifying_scope;
8663 parser->object_scope = saved_object_scope;
8664 /* If the TYPE is invalid, indicate failure. */
8665 if (type == error_mark_node)
8666 return error_mark_node;
8667 return mangle_conv_op_name_for_type (type);
8670 /* Parse a conversion-type-id:
8673 type-specifier-seq conversion-declarator [opt]
8675 Returns the TYPE specified. */
8678 cp_parser_conversion_type_id (cp_parser* parser)
8681 cp_decl_specifier_seq type_specifiers;
8682 cp_declarator *declarator;
8683 tree type_specified;
8685 /* Parse the attributes. */
8686 attributes = cp_parser_attributes_opt (parser);
8687 /* Parse the type-specifiers. */
8688 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8690 /* If that didn't work, stop. */
8691 if (type_specifiers.type == error_mark_node)
8692 return error_mark_node;
8693 /* Parse the conversion-declarator. */
8694 declarator = cp_parser_conversion_declarator_opt (parser);
8696 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8697 /*initialized=*/0, &attributes);
8699 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8700 return type_specified;
8703 /* Parse an (optional) conversion-declarator.
8705 conversion-declarator:
8706 ptr-operator conversion-declarator [opt]
8710 static cp_declarator *
8711 cp_parser_conversion_declarator_opt (cp_parser* parser)
8713 enum tree_code code;
8715 cp_cv_quals cv_quals;
8717 /* We don't know if there's a ptr-operator next, or not. */
8718 cp_parser_parse_tentatively (parser);
8719 /* Try the ptr-operator. */
8720 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8721 /* If it worked, look for more conversion-declarators. */
8722 if (cp_parser_parse_definitely (parser))
8724 cp_declarator *declarator;
8726 /* Parse another optional declarator. */
8727 declarator = cp_parser_conversion_declarator_opt (parser);
8729 return cp_parser_make_indirect_declarator
8730 (code, class_type, cv_quals, declarator);
8736 /* Parse an (optional) ctor-initializer.
8739 : mem-initializer-list
8741 Returns TRUE iff the ctor-initializer was actually present. */
8744 cp_parser_ctor_initializer_opt (cp_parser* parser)
8746 /* If the next token is not a `:', then there is no
8747 ctor-initializer. */
8748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8750 /* Do default initialization of any bases and members. */
8751 if (DECL_CONSTRUCTOR_P (current_function_decl))
8752 finish_mem_initializers (NULL_TREE);
8757 /* Consume the `:' token. */
8758 cp_lexer_consume_token (parser->lexer);
8759 /* And the mem-initializer-list. */
8760 cp_parser_mem_initializer_list (parser);
8765 /* Parse a mem-initializer-list.
8767 mem-initializer-list:
8768 mem-initializer ... [opt]
8769 mem-initializer ... [opt] , mem-initializer-list */
8772 cp_parser_mem_initializer_list (cp_parser* parser)
8774 tree mem_initializer_list = NULL_TREE;
8776 /* Let the semantic analysis code know that we are starting the
8777 mem-initializer-list. */
8778 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8779 error ("only constructors take base initializers");
8781 /* Loop through the list. */
8784 tree mem_initializer;
8786 /* Parse the mem-initializer. */
8787 mem_initializer = cp_parser_mem_initializer (parser);
8788 /* If the next token is a `...', we're expanding member initializers. */
8789 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8791 /* Consume the `...'. */
8792 cp_lexer_consume_token (parser->lexer);
8794 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8795 can be expanded but members cannot. */
8796 if (mem_initializer != error_mark_node
8797 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8799 error ("cannot expand initializer for member %<%D%>",
8800 TREE_PURPOSE (mem_initializer));
8801 mem_initializer = error_mark_node;
8804 /* Construct the pack expansion type. */
8805 if (mem_initializer != error_mark_node)
8806 mem_initializer = make_pack_expansion (mem_initializer);
8808 /* Add it to the list, unless it was erroneous. */
8809 if (mem_initializer != error_mark_node)
8811 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8812 mem_initializer_list = mem_initializer;
8814 /* If the next token is not a `,', we're done. */
8815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8817 /* Consume the `,' token. */
8818 cp_lexer_consume_token (parser->lexer);
8821 /* Perform semantic analysis. */
8822 if (DECL_CONSTRUCTOR_P (current_function_decl))
8823 finish_mem_initializers (mem_initializer_list);
8826 /* Parse a mem-initializer.
8829 mem-initializer-id ( expression-list [opt] )
8834 ( expression-list [opt] )
8836 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8837 class) or FIELD_DECL (for a non-static data member) to initialize;
8838 the TREE_VALUE is the expression-list. An empty initialization
8839 list is represented by void_list_node. */
8842 cp_parser_mem_initializer (cp_parser* parser)
8844 tree mem_initializer_id;
8845 tree expression_list;
8848 /* Find out what is being initialized. */
8849 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8851 pedwarn ("anachronistic old-style base class initializer");
8852 mem_initializer_id = NULL_TREE;
8855 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8856 member = expand_member_init (mem_initializer_id);
8857 if (member && !DECL_P (member))
8858 in_base_initializer = 1;
8861 = cp_parser_parenthesized_expression_list (parser, false,
8863 /*allow_expansion_p=*/true,
8864 /*non_constant_p=*/NULL);
8865 if (expression_list == error_mark_node)
8866 return error_mark_node;
8867 if (!expression_list)
8868 expression_list = void_type_node;
8870 in_base_initializer = 0;
8872 return member ? build_tree_list (member, expression_list) : error_mark_node;
8875 /* Parse a mem-initializer-id.
8878 :: [opt] nested-name-specifier [opt] class-name
8881 Returns a TYPE indicating the class to be initializer for the first
8882 production. Returns an IDENTIFIER_NODE indicating the data member
8883 to be initialized for the second production. */
8886 cp_parser_mem_initializer_id (cp_parser* parser)
8888 bool global_scope_p;
8889 bool nested_name_specifier_p;
8890 bool template_p = false;
8893 /* `typename' is not allowed in this context ([temp.res]). */
8894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8896 error ("keyword %<typename%> not allowed in this context (a qualified "
8897 "member initializer is implicitly a type)");
8898 cp_lexer_consume_token (parser->lexer);
8900 /* Look for the optional `::' operator. */
8902 = (cp_parser_global_scope_opt (parser,
8903 /*current_scope_valid_p=*/false)
8905 /* Look for the optional nested-name-specifier. The simplest way to
8910 The keyword `typename' is not permitted in a base-specifier or
8911 mem-initializer; in these contexts a qualified name that
8912 depends on a template-parameter is implicitly assumed to be a
8915 is to assume that we have seen the `typename' keyword at this
8917 nested_name_specifier_p
8918 = (cp_parser_nested_name_specifier_opt (parser,
8919 /*typename_keyword_p=*/true,
8920 /*check_dependency_p=*/true,
8922 /*is_declaration=*/true)
8924 if (nested_name_specifier_p)
8925 template_p = cp_parser_optional_template_keyword (parser);
8926 /* If there is a `::' operator or a nested-name-specifier, then we
8927 are definitely looking for a class-name. */
8928 if (global_scope_p || nested_name_specifier_p)
8929 return cp_parser_class_name (parser,
8930 /*typename_keyword_p=*/true,
8931 /*template_keyword_p=*/template_p,
8933 /*check_dependency_p=*/true,
8934 /*class_head_p=*/false,
8935 /*is_declaration=*/true);
8936 /* Otherwise, we could also be looking for an ordinary identifier. */
8937 cp_parser_parse_tentatively (parser);
8938 /* Try a class-name. */
8939 id = cp_parser_class_name (parser,
8940 /*typename_keyword_p=*/true,
8941 /*template_keyword_p=*/false,
8943 /*check_dependency_p=*/true,
8944 /*class_head_p=*/false,
8945 /*is_declaration=*/true);
8946 /* If we found one, we're done. */
8947 if (cp_parser_parse_definitely (parser))
8949 /* Otherwise, look for an ordinary identifier. */
8950 return cp_parser_identifier (parser);
8953 /* Overloading [gram.over] */
8955 /* Parse an operator-function-id.
8957 operator-function-id:
8960 Returns an IDENTIFIER_NODE for the operator which is a
8961 human-readable spelling of the identifier, e.g., `operator +'. */
8964 cp_parser_operator_function_id (cp_parser* parser)
8966 /* Look for the `operator' keyword. */
8967 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8968 return error_mark_node;
8969 /* And then the name of the operator itself. */
8970 return cp_parser_operator (parser);
8973 /* Parse an operator.
8976 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8977 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8978 || ++ -- , ->* -> () []
8985 Returns an IDENTIFIER_NODE for the operator which is a
8986 human-readable spelling of the identifier, e.g., `operator +'. */
8989 cp_parser_operator (cp_parser* parser)
8991 tree id = NULL_TREE;
8994 /* Peek at the next token. */
8995 token = cp_lexer_peek_token (parser->lexer);
8996 /* Figure out which operator we have. */
8997 switch (token->type)
9003 /* The keyword should be either `new' or `delete'. */
9004 if (token->keyword == RID_NEW)
9006 else if (token->keyword == RID_DELETE)
9011 /* Consume the `new' or `delete' token. */
9012 cp_lexer_consume_token (parser->lexer);
9014 /* Peek at the next token. */
9015 token = cp_lexer_peek_token (parser->lexer);
9016 /* If it's a `[' token then this is the array variant of the
9018 if (token->type == CPP_OPEN_SQUARE)
9020 /* Consume the `[' token. */
9021 cp_lexer_consume_token (parser->lexer);
9022 /* Look for the `]' token. */
9023 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9024 id = ansi_opname (op == NEW_EXPR
9025 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9027 /* Otherwise, we have the non-array variant. */
9029 id = ansi_opname (op);
9035 id = ansi_opname (PLUS_EXPR);
9039 id = ansi_opname (MINUS_EXPR);
9043 id = ansi_opname (MULT_EXPR);
9047 id = ansi_opname (TRUNC_DIV_EXPR);
9051 id = ansi_opname (TRUNC_MOD_EXPR);
9055 id = ansi_opname (BIT_XOR_EXPR);
9059 id = ansi_opname (BIT_AND_EXPR);
9063 id = ansi_opname (BIT_IOR_EXPR);
9067 id = ansi_opname (BIT_NOT_EXPR);
9071 id = ansi_opname (TRUTH_NOT_EXPR);
9075 id = ansi_assopname (NOP_EXPR);
9079 id = ansi_opname (LT_EXPR);
9083 id = ansi_opname (GT_EXPR);
9087 id = ansi_assopname (PLUS_EXPR);
9091 id = ansi_assopname (MINUS_EXPR);
9095 id = ansi_assopname (MULT_EXPR);
9099 id = ansi_assopname (TRUNC_DIV_EXPR);
9103 id = ansi_assopname (TRUNC_MOD_EXPR);
9107 id = ansi_assopname (BIT_XOR_EXPR);
9111 id = ansi_assopname (BIT_AND_EXPR);
9115 id = ansi_assopname (BIT_IOR_EXPR);
9119 id = ansi_opname (LSHIFT_EXPR);
9123 id = ansi_opname (RSHIFT_EXPR);
9127 id = ansi_assopname (LSHIFT_EXPR);
9131 id = ansi_assopname (RSHIFT_EXPR);
9135 id = ansi_opname (EQ_EXPR);
9139 id = ansi_opname (NE_EXPR);
9143 id = ansi_opname (LE_EXPR);
9146 case CPP_GREATER_EQ:
9147 id = ansi_opname (GE_EXPR);
9151 id = ansi_opname (TRUTH_ANDIF_EXPR);
9155 id = ansi_opname (TRUTH_ORIF_EXPR);
9159 id = ansi_opname (POSTINCREMENT_EXPR);
9162 case CPP_MINUS_MINUS:
9163 id = ansi_opname (PREDECREMENT_EXPR);
9167 id = ansi_opname (COMPOUND_EXPR);
9170 case CPP_DEREF_STAR:
9171 id = ansi_opname (MEMBER_REF);
9175 id = ansi_opname (COMPONENT_REF);
9178 case CPP_OPEN_PAREN:
9179 /* Consume the `('. */
9180 cp_lexer_consume_token (parser->lexer);
9181 /* Look for the matching `)'. */
9182 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9183 return ansi_opname (CALL_EXPR);
9185 case CPP_OPEN_SQUARE:
9186 /* Consume the `['. */
9187 cp_lexer_consume_token (parser->lexer);
9188 /* Look for the matching `]'. */
9189 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9190 return ansi_opname (ARRAY_REF);
9193 /* Anything else is an error. */
9197 /* If we have selected an identifier, we need to consume the
9200 cp_lexer_consume_token (parser->lexer);
9201 /* Otherwise, no valid operator name was present. */
9204 cp_parser_error (parser, "expected operator");
9205 id = error_mark_node;
9211 /* Parse a template-declaration.
9213 template-declaration:
9214 export [opt] template < template-parameter-list > declaration
9216 If MEMBER_P is TRUE, this template-declaration occurs within a
9219 The grammar rule given by the standard isn't correct. What
9222 template-declaration:
9223 export [opt] template-parameter-list-seq
9224 decl-specifier-seq [opt] init-declarator [opt] ;
9225 export [opt] template-parameter-list-seq
9228 template-parameter-list-seq:
9229 template-parameter-list-seq [opt]
9230 template < template-parameter-list > */
9233 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9235 /* Check for `export'. */
9236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9238 /* Consume the `export' token. */
9239 cp_lexer_consume_token (parser->lexer);
9240 /* Warn that we do not support `export'. */
9241 warning (0, "keyword %<export%> not implemented, and will be ignored");
9244 cp_parser_template_declaration_after_export (parser, member_p);
9247 /* Parse a template-parameter-list.
9249 template-parameter-list:
9251 template-parameter-list , template-parameter
9253 Returns a TREE_LIST. Each node represents a template parameter.
9254 The nodes are connected via their TREE_CHAINs. */
9257 cp_parser_template_parameter_list (cp_parser* parser)
9259 tree parameter_list = NULL_TREE;
9261 begin_template_parm_list ();
9267 bool is_parameter_pack;
9269 /* Parse the template-parameter. */
9270 parameter = cp_parser_template_parameter (parser,
9272 &is_parameter_pack);
9273 /* Add it to the list. */
9274 if (parameter != error_mark_node)
9275 parameter_list = process_template_parm (parameter_list,
9281 tree err_parm = build_tree_list (parameter, parameter);
9282 TREE_VALUE (err_parm) = error_mark_node;
9283 parameter_list = chainon (parameter_list, err_parm);
9286 /* Peek at the next token. */
9287 token = cp_lexer_peek_token (parser->lexer);
9288 /* If it's not a `,', we're done. */
9289 if (token->type != CPP_COMMA)
9291 /* Otherwise, consume the `,' token. */
9292 cp_lexer_consume_token (parser->lexer);
9295 return end_template_parm_list (parameter_list);
9298 /* Parse a template-parameter.
9302 parameter-declaration
9304 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9305 the parameter. The TREE_PURPOSE is the default value, if any.
9306 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9307 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9308 set to true iff this parameter is a parameter pack. */
9311 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9312 bool *is_parameter_pack)
9315 cp_parameter_declarator *parameter_declarator;
9316 cp_declarator *id_declarator;
9319 /* Assume it is a type parameter or a template parameter. */
9320 *is_non_type = false;
9321 /* Assume it not a parameter pack. */
9322 *is_parameter_pack = false;
9323 /* Peek at the next token. */
9324 token = cp_lexer_peek_token (parser->lexer);
9325 /* If it is `class' or `template', we have a type-parameter. */
9326 if (token->keyword == RID_TEMPLATE)
9327 return cp_parser_type_parameter (parser, is_parameter_pack);
9328 /* If it is `class' or `typename' we do not know yet whether it is a
9329 type parameter or a non-type parameter. Consider:
9331 template <typename T, typename T::X X> ...
9335 template <class C, class D*> ...
9337 Here, the first parameter is a type parameter, and the second is
9338 a non-type parameter. We can tell by looking at the token after
9339 the identifier -- if it is a `,', `=', or `>' then we have a type
9341 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9343 /* Peek at the token after `class' or `typename'. */
9344 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9345 /* If it's an ellipsis, we have a template type parameter
9347 if (token->type == CPP_ELLIPSIS)
9348 return cp_parser_type_parameter (parser, is_parameter_pack);
9349 /* If it's an identifier, skip it. */
9350 if (token->type == CPP_NAME)
9351 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9352 /* Now, see if the token looks like the end of a template
9354 if (token->type == CPP_COMMA
9355 || token->type == CPP_EQ
9356 || token->type == CPP_GREATER)
9357 return cp_parser_type_parameter (parser, is_parameter_pack);
9360 /* Otherwise, it is a non-type parameter.
9364 When parsing a default template-argument for a non-type
9365 template-parameter, the first non-nested `>' is taken as the end
9366 of the template parameter-list rather than a greater-than
9368 *is_non_type = true;
9369 parameter_declarator
9370 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9371 /*parenthesized_p=*/NULL);
9373 /* If the parameter declaration is marked as a parameter pack, set
9374 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9375 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9377 if (parameter_declarator
9378 && parameter_declarator->declarator
9379 && parameter_declarator->declarator->parameter_pack_p)
9381 *is_parameter_pack = true;
9382 parameter_declarator->declarator->parameter_pack_p = false;
9385 /* If the next token is an ellipsis, and we don't already have it
9386 marked as a parameter pack, then we have a parameter pack (that
9387 has no declarator). */
9388 if (!*is_parameter_pack
9389 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9390 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9392 /* Consume the `...'. */
9393 cp_lexer_consume_token (parser->lexer);
9394 maybe_warn_variadic_templates ();
9396 *is_parameter_pack = true;
9398 /* Parameter packs cannot have default arguments. However, a
9399 user may try to do so, so we'll parse them and give an
9400 appropriate diagnostic here. */
9401 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9403 /* Consume the `='. */
9404 cp_lexer_consume_token (parser->lexer);
9406 /* Find the name of the parameter pack. */
9407 id_declarator = parameter_declarator->declarator;
9408 while (id_declarator && id_declarator->kind != cdk_id)
9409 id_declarator = id_declarator->declarator;
9411 if (id_declarator && id_declarator->kind == cdk_id)
9412 error ("template parameter pack %qD cannot have a default argument",
9413 id_declarator->u.id.unqualified_name);
9415 error ("template parameter pack cannot have a default argument");
9417 /* Parse the default argument, but throw away the result. */
9418 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9422 parm = grokdeclarator (parameter_declarator->declarator,
9423 ¶meter_declarator->decl_specifiers,
9424 PARM, /*initialized=*/0,
9426 if (parm == error_mark_node)
9427 return error_mark_node;
9429 return build_tree_list (parameter_declarator->default_argument, parm);
9432 /* Parse a type-parameter.
9435 class identifier [opt]
9436 class identifier [opt] = type-id
9437 typename identifier [opt]
9438 typename identifier [opt] = type-id
9439 template < template-parameter-list > class identifier [opt]
9440 template < template-parameter-list > class identifier [opt]
9443 GNU Extension (variadic templates):
9446 class ... identifier [opt]
9447 typename ... identifier [opt]
9449 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9450 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9451 the declaration of the parameter.
9453 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9456 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9461 /* Look for a keyword to tell us what kind of parameter this is. */
9462 token = cp_parser_require (parser, CPP_KEYWORD,
9463 "`class', `typename', or `template'");
9465 return error_mark_node;
9467 switch (token->keyword)
9473 tree default_argument;
9475 /* If the next token is an ellipsis, we have a template
9477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9479 /* Consume the `...' token. */
9480 cp_lexer_consume_token (parser->lexer);
9481 maybe_warn_variadic_templates ();
9483 *is_parameter_pack = true;
9486 /* If the next token is an identifier, then it names the
9488 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9489 identifier = cp_parser_identifier (parser);
9491 identifier = NULL_TREE;
9493 /* Create the parameter. */
9494 parameter = finish_template_type_parm (class_type_node, identifier);
9496 /* If the next token is an `=', we have a default argument. */
9497 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9499 /* Consume the `=' token. */
9500 cp_lexer_consume_token (parser->lexer);
9501 /* Parse the default-argument. */
9502 push_deferring_access_checks (dk_no_deferred);
9503 default_argument = cp_parser_type_id (parser);
9505 /* Template parameter packs cannot have default
9507 if (*is_parameter_pack)
9510 error ("template parameter pack %qD cannot have a default argument",
9513 error ("template parameter packs cannot have default arguments");
9514 default_argument = NULL_TREE;
9516 pop_deferring_access_checks ();
9519 default_argument = NULL_TREE;
9521 /* Create the combined representation of the parameter and the
9522 default argument. */
9523 parameter = build_tree_list (default_argument, parameter);
9529 tree parameter_list;
9531 tree default_argument;
9533 /* Look for the `<'. */
9534 cp_parser_require (parser, CPP_LESS, "`<'");
9535 /* Parse the template-parameter-list. */
9536 parameter_list = cp_parser_template_parameter_list (parser);
9537 /* Look for the `>'. */
9538 cp_parser_require (parser, CPP_GREATER, "`>'");
9539 /* Look for the `class' keyword. */
9540 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9541 /* If the next token is an ellipsis, we have a template
9543 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9545 /* Consume the `...' token. */
9546 cp_lexer_consume_token (parser->lexer);
9547 maybe_warn_variadic_templates ();
9549 *is_parameter_pack = true;
9551 /* If the next token is an `=', then there is a
9552 default-argument. If the next token is a `>', we are at
9553 the end of the parameter-list. If the next token is a `,',
9554 then we are at the end of this parameter. */
9555 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9556 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9557 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9559 identifier = cp_parser_identifier (parser);
9560 /* Treat invalid names as if the parameter were nameless. */
9561 if (identifier == error_mark_node)
9562 identifier = NULL_TREE;
9565 identifier = NULL_TREE;
9567 /* Create the template parameter. */
9568 parameter = finish_template_template_parm (class_type_node,
9571 /* If the next token is an `=', then there is a
9572 default-argument. */
9573 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9577 /* Consume the `='. */
9578 cp_lexer_consume_token (parser->lexer);
9579 /* Parse the id-expression. */
9580 push_deferring_access_checks (dk_no_deferred);
9582 = cp_parser_id_expression (parser,
9583 /*template_keyword_p=*/false,
9584 /*check_dependency_p=*/true,
9585 /*template_p=*/&is_template,
9586 /*declarator_p=*/false,
9587 /*optional_p=*/false);
9588 if (TREE_CODE (default_argument) == TYPE_DECL)
9589 /* If the id-expression was a template-id that refers to
9590 a template-class, we already have the declaration here,
9591 so no further lookup is needed. */
9594 /* Look up the name. */
9596 = cp_parser_lookup_name (parser, default_argument,
9598 /*is_template=*/is_template,
9599 /*is_namespace=*/false,
9600 /*check_dependency=*/true,
9601 /*ambiguous_decls=*/NULL);
9602 /* See if the default argument is valid. */
9604 = check_template_template_default_arg (default_argument);
9606 /* Template parameter packs cannot have default
9608 if (*is_parameter_pack)
9611 error ("template parameter pack %qD cannot have a default argument",
9614 error ("template parameter packs cannot have default arguments");
9615 default_argument = NULL_TREE;
9617 pop_deferring_access_checks ();
9620 default_argument = NULL_TREE;
9622 /* Create the combined representation of the parameter and the
9623 default argument. */
9624 parameter = build_tree_list (default_argument, parameter);
9636 /* Parse a template-id.
9639 template-name < template-argument-list [opt] >
9641 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9642 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9643 returned. Otherwise, if the template-name names a function, or set
9644 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9645 names a class, returns a TYPE_DECL for the specialization.
9647 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9648 uninstantiated templates. */
9651 cp_parser_template_id (cp_parser *parser,
9652 bool template_keyword_p,
9653 bool check_dependency_p,
9654 bool is_declaration)
9660 cp_token_position start_of_id = 0;
9661 deferred_access_check *chk;
9662 VEC (deferred_access_check,gc) *access_check;
9663 cp_token *next_token, *next_token_2;
9666 /* If the next token corresponds to a template-id, there is no need
9668 next_token = cp_lexer_peek_token (parser->lexer);
9669 if (next_token->type == CPP_TEMPLATE_ID)
9671 struct tree_check *check_value;
9673 /* Get the stored value. */
9674 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9675 /* Perform any access checks that were deferred. */
9676 access_check = check_value->checks;
9680 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9683 perform_or_defer_access_check (chk->binfo,
9688 /* Return the stored value. */
9689 return check_value->value;
9692 /* Avoid performing name lookup if there is no possibility of
9693 finding a template-id. */
9694 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9695 || (next_token->type == CPP_NAME
9696 && !cp_parser_nth_token_starts_template_argument_list_p
9699 cp_parser_error (parser, "expected template-id");
9700 return error_mark_node;
9703 /* Remember where the template-id starts. */
9704 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9705 start_of_id = cp_lexer_token_position (parser->lexer, false);
9707 push_deferring_access_checks (dk_deferred);
9709 /* Parse the template-name. */
9710 is_identifier = false;
9711 template = cp_parser_template_name (parser, template_keyword_p,
9715 if (template == error_mark_node || is_identifier)
9717 pop_deferring_access_checks ();
9721 /* If we find the sequence `[:' after a template-name, it's probably
9722 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9723 parse correctly the argument list. */
9724 next_token = cp_lexer_peek_token (parser->lexer);
9725 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9726 if (next_token->type == CPP_OPEN_SQUARE
9727 && next_token->flags & DIGRAPH
9728 && next_token_2->type == CPP_COLON
9729 && !(next_token_2->flags & PREV_WHITE))
9731 cp_parser_parse_tentatively (parser);
9732 /* Change `:' into `::'. */
9733 next_token_2->type = CPP_SCOPE;
9734 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9736 cp_lexer_consume_token (parser->lexer);
9737 /* Parse the arguments. */
9738 arguments = cp_parser_enclosed_template_argument_list (parser);
9739 if (!cp_parser_parse_definitely (parser))
9741 /* If we couldn't parse an argument list, then we revert our changes
9742 and return simply an error. Maybe this is not a template-id
9744 next_token_2->type = CPP_COLON;
9745 cp_parser_error (parser, "expected %<<%>");
9746 pop_deferring_access_checks ();
9747 return error_mark_node;
9749 /* Otherwise, emit an error about the invalid digraph, but continue
9750 parsing because we got our argument list. */
9751 pedwarn ("%<<::%> cannot begin a template-argument list");
9752 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9753 "between %<<%> and %<::%>");
9754 if (!flag_permissive)
9759 inform ("(if you use -fpermissive G++ will accept your code)");
9766 /* Look for the `<' that starts the template-argument-list. */
9767 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9769 pop_deferring_access_checks ();
9770 return error_mark_node;
9772 /* Parse the arguments. */
9773 arguments = cp_parser_enclosed_template_argument_list (parser);
9776 /* Build a representation of the specialization. */
9777 if (TREE_CODE (template) == IDENTIFIER_NODE)
9778 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9779 else if (DECL_CLASS_TEMPLATE_P (template)
9780 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9782 bool entering_scope;
9783 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9784 template (rather than some instantiation thereof) only if
9785 is not nested within some other construct. For example, in
9786 "template <typename T> void f(T) { A<T>::", A<T> is just an
9787 instantiation of A. */
9788 entering_scope = (template_parm_scope_p ()
9789 && cp_lexer_next_token_is (parser->lexer,
9792 = finish_template_type (template, arguments, entering_scope);
9796 /* If it's not a class-template or a template-template, it should be
9797 a function-template. */
9798 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9799 || TREE_CODE (template) == OVERLOAD
9800 || BASELINK_P (template)));
9802 template_id = lookup_template_function (template, arguments);
9805 /* If parsing tentatively, replace the sequence of tokens that makes
9806 up the template-id with a CPP_TEMPLATE_ID token. That way,
9807 should we re-parse the token stream, we will not have to repeat
9808 the effort required to do the parse, nor will we issue duplicate
9809 error messages about problems during instantiation of the
9813 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9815 /* Reset the contents of the START_OF_ID token. */
9816 token->type = CPP_TEMPLATE_ID;
9817 /* Retrieve any deferred checks. Do not pop this access checks yet
9818 so the memory will not be reclaimed during token replacing below. */
9819 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9820 token->u.tree_check_value->value = template_id;
9821 token->u.tree_check_value->checks = get_deferred_access_checks ();
9822 token->keyword = RID_MAX;
9824 /* Purge all subsequent tokens. */
9825 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9827 /* ??? Can we actually assume that, if template_id ==
9828 error_mark_node, we will have issued a diagnostic to the
9829 user, as opposed to simply marking the tentative parse as
9831 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9832 error ("parse error in template argument list");
9835 pop_deferring_access_checks ();
9839 /* Parse a template-name.
9844 The standard should actually say:
9848 operator-function-id
9850 A defect report has been filed about this issue.
9852 A conversion-function-id cannot be a template name because they cannot
9853 be part of a template-id. In fact, looking at this code:
9857 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9858 It is impossible to call a templated conversion-function-id with an
9859 explicit argument list, since the only allowed template parameter is
9860 the type to which it is converting.
9862 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9863 `template' keyword, in a construction like:
9867 In that case `f' is taken to be a template-name, even though there
9868 is no way of knowing for sure.
9870 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9871 name refers to a set of overloaded functions, at least one of which
9872 is a template, or an IDENTIFIER_NODE with the name of the template,
9873 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9874 names are looked up inside uninstantiated templates. */
9877 cp_parser_template_name (cp_parser* parser,
9878 bool template_keyword_p,
9879 bool check_dependency_p,
9880 bool is_declaration,
9881 bool *is_identifier)
9887 /* If the next token is `operator', then we have either an
9888 operator-function-id or a conversion-function-id. */
9889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9891 /* We don't know whether we're looking at an
9892 operator-function-id or a conversion-function-id. */
9893 cp_parser_parse_tentatively (parser);
9894 /* Try an operator-function-id. */
9895 identifier = cp_parser_operator_function_id (parser);
9896 /* If that didn't work, try a conversion-function-id. */
9897 if (!cp_parser_parse_definitely (parser))
9899 cp_parser_error (parser, "expected template-name");
9900 return error_mark_node;
9903 /* Look for the identifier. */
9905 identifier = cp_parser_identifier (parser);
9907 /* If we didn't find an identifier, we don't have a template-id. */
9908 if (identifier == error_mark_node)
9909 return error_mark_node;
9911 /* If the name immediately followed the `template' keyword, then it
9912 is a template-name. However, if the next token is not `<', then
9913 we do not treat it as a template-name, since it is not being used
9914 as part of a template-id. This enables us to handle constructs
9917 template <typename T> struct S { S(); };
9918 template <typename T> S<T>::S();
9920 correctly. We would treat `S' as a template -- if it were `S<T>'
9921 -- but we do not if there is no `<'. */
9923 if (processing_template_decl
9924 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9926 /* In a declaration, in a dependent context, we pretend that the
9927 "template" keyword was present in order to improve error
9928 recovery. For example, given:
9930 template <typename T> void f(T::X<int>);
9932 we want to treat "X<int>" as a template-id. */
9934 && !template_keyword_p
9935 && parser->scope && TYPE_P (parser->scope)
9936 && check_dependency_p
9937 && dependent_type_p (parser->scope)
9938 /* Do not do this for dtors (or ctors), since they never
9939 need the template keyword before their name. */
9940 && !constructor_name_p (identifier, parser->scope))
9942 cp_token_position start = 0;
9944 /* Explain what went wrong. */
9945 error ("non-template %qD used as template", identifier);
9946 inform ("use %<%T::template %D%> to indicate that it is a template",
9947 parser->scope, identifier);
9948 /* If parsing tentatively, find the location of the "<" token. */
9949 if (cp_parser_simulate_error (parser))
9950 start = cp_lexer_token_position (parser->lexer, true);
9951 /* Parse the template arguments so that we can issue error
9952 messages about them. */
9953 cp_lexer_consume_token (parser->lexer);
9954 cp_parser_enclosed_template_argument_list (parser);
9955 /* Skip tokens until we find a good place from which to
9956 continue parsing. */
9957 cp_parser_skip_to_closing_parenthesis (parser,
9958 /*recovering=*/true,
9960 /*consume_paren=*/false);
9961 /* If parsing tentatively, permanently remove the
9962 template argument list. That will prevent duplicate
9963 error messages from being issued about the missing
9964 "template" keyword. */
9966 cp_lexer_purge_tokens_after (parser->lexer, start);
9968 *is_identifier = true;
9972 /* If the "template" keyword is present, then there is generally
9973 no point in doing name-lookup, so we just return IDENTIFIER.
9974 But, if the qualifying scope is non-dependent then we can
9975 (and must) do name-lookup normally. */
9976 if (template_keyword_p
9978 || (TYPE_P (parser->scope)
9979 && dependent_type_p (parser->scope))))
9983 /* Look up the name. */
9984 decl = cp_parser_lookup_name (parser, identifier,
9986 /*is_template=*/false,
9987 /*is_namespace=*/false,
9989 /*ambiguous_decls=*/NULL);
9990 decl = maybe_get_template_decl_from_type_decl (decl);
9992 /* If DECL is a template, then the name was a template-name. */
9993 if (TREE_CODE (decl) == TEMPLATE_DECL)
9997 tree fn = NULL_TREE;
9999 /* The standard does not explicitly indicate whether a name that
10000 names a set of overloaded declarations, some of which are
10001 templates, is a template-name. However, such a name should
10002 be a template-name; otherwise, there is no way to form a
10003 template-id for the overloaded templates. */
10004 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10005 if (TREE_CODE (fns) == OVERLOAD)
10006 for (fn = fns; fn; fn = OVL_NEXT (fn))
10007 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10012 /* The name does not name a template. */
10013 cp_parser_error (parser, "expected template-name");
10014 return error_mark_node;
10018 /* If DECL is dependent, and refers to a function, then just return
10019 its name; we will look it up again during template instantiation. */
10020 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10022 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10023 if (TYPE_P (scope) && dependent_type_p (scope))
10030 /* Parse a template-argument-list.
10032 template-argument-list:
10033 template-argument ... [opt]
10034 template-argument-list , template-argument ... [opt]
10036 Returns a TREE_VEC containing the arguments. */
10039 cp_parser_template_argument_list (cp_parser* parser)
10041 tree fixed_args[10];
10042 unsigned n_args = 0;
10043 unsigned alloced = 10;
10044 tree *arg_ary = fixed_args;
10046 bool saved_in_template_argument_list_p;
10048 bool saved_non_ice_p;
10050 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10051 parser->in_template_argument_list_p = true;
10052 /* Even if the template-id appears in an integral
10053 constant-expression, the contents of the argument list do
10055 saved_ice_p = parser->integral_constant_expression_p;
10056 parser->integral_constant_expression_p = false;
10057 saved_non_ice_p = parser->non_integral_constant_expression_p;
10058 parser->non_integral_constant_expression_p = false;
10059 /* Parse the arguments. */
10065 /* Consume the comma. */
10066 cp_lexer_consume_token (parser->lexer);
10068 /* Parse the template-argument. */
10069 argument = cp_parser_template_argument (parser);
10071 /* If the next token is an ellipsis, we're expanding a template
10073 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10075 /* Consume the `...' token. */
10076 cp_lexer_consume_token (parser->lexer);
10078 /* Make the argument into a TYPE_PACK_EXPANSION or
10079 EXPR_PACK_EXPANSION. */
10080 argument = make_pack_expansion (argument);
10083 if (n_args == alloced)
10087 if (arg_ary == fixed_args)
10089 arg_ary = XNEWVEC (tree, alloced);
10090 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10093 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10095 arg_ary[n_args++] = argument;
10097 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10099 vec = make_tree_vec (n_args);
10102 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10104 if (arg_ary != fixed_args)
10106 parser->non_integral_constant_expression_p = saved_non_ice_p;
10107 parser->integral_constant_expression_p = saved_ice_p;
10108 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10112 /* Parse a template-argument.
10115 assignment-expression
10119 The representation is that of an assignment-expression, type-id, or
10120 id-expression -- except that the qualified id-expression is
10121 evaluated, so that the value returned is either a DECL or an
10124 Although the standard says "assignment-expression", it forbids
10125 throw-expressions or assignments in the template argument.
10126 Therefore, we use "conditional-expression" instead. */
10129 cp_parser_template_argument (cp_parser* parser)
10134 bool maybe_type_id = false;
10138 /* There's really no way to know what we're looking at, so we just
10139 try each alternative in order.
10143 In a template-argument, an ambiguity between a type-id and an
10144 expression is resolved to a type-id, regardless of the form of
10145 the corresponding template-parameter.
10147 Therefore, we try a type-id first. */
10148 cp_parser_parse_tentatively (parser);
10149 argument = cp_parser_type_id (parser);
10150 /* If there was no error parsing the type-id but the next token is a '>>',
10151 we probably found a typo for '> >'. But there are type-id which are
10152 also valid expressions. For instance:
10154 struct X { int operator >> (int); };
10155 template <int V> struct Foo {};
10158 Here 'X()' is a valid type-id of a function type, but the user just
10159 wanted to write the expression "X() >> 5". Thus, we remember that we
10160 found a valid type-id, but we still try to parse the argument as an
10161 expression to see what happens. */
10162 if (!cp_parser_error_occurred (parser)
10163 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10165 maybe_type_id = true;
10166 cp_parser_abort_tentative_parse (parser);
10170 /* If the next token isn't a `,' or a `>', then this argument wasn't
10171 really finished. This means that the argument is not a valid
10173 if (!cp_parser_next_token_ends_template_argument_p (parser))
10174 cp_parser_error (parser, "expected template-argument");
10175 /* If that worked, we're done. */
10176 if (cp_parser_parse_definitely (parser))
10179 /* We're still not sure what the argument will be. */
10180 cp_parser_parse_tentatively (parser);
10181 /* Try a template. */
10182 argument = cp_parser_id_expression (parser,
10183 /*template_keyword_p=*/false,
10184 /*check_dependency_p=*/true,
10186 /*declarator_p=*/false,
10187 /*optional_p=*/false);
10188 /* If the next token isn't a `,' or a `>', then this argument wasn't
10189 really finished. */
10190 if (!cp_parser_next_token_ends_template_argument_p (parser))
10191 cp_parser_error (parser, "expected template-argument");
10192 if (!cp_parser_error_occurred (parser))
10194 /* Figure out what is being referred to. If the id-expression
10195 was for a class template specialization, then we will have a
10196 TYPE_DECL at this point. There is no need to do name lookup
10197 at this point in that case. */
10198 if (TREE_CODE (argument) != TYPE_DECL)
10199 argument = cp_parser_lookup_name (parser, argument,
10201 /*is_template=*/template_p,
10202 /*is_namespace=*/false,
10203 /*check_dependency=*/true,
10204 /*ambiguous_decls=*/NULL);
10205 if (TREE_CODE (argument) != TEMPLATE_DECL
10206 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10207 cp_parser_error (parser, "expected template-name");
10209 if (cp_parser_parse_definitely (parser))
10211 /* It must be a non-type argument. There permitted cases are given
10212 in [temp.arg.nontype]:
10214 -- an integral constant-expression of integral or enumeration
10217 -- the name of a non-type template-parameter; or
10219 -- the name of an object or function with external linkage...
10221 -- the address of an object or function with external linkage...
10223 -- a pointer to member... */
10224 /* Look for a non-type template parameter. */
10225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10227 cp_parser_parse_tentatively (parser);
10228 argument = cp_parser_primary_expression (parser,
10229 /*adress_p=*/false,
10231 /*template_arg_p=*/true,
10233 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10234 || !cp_parser_next_token_ends_template_argument_p (parser))
10235 cp_parser_simulate_error (parser);
10236 if (cp_parser_parse_definitely (parser))
10240 /* If the next token is "&", the argument must be the address of an
10241 object or function with external linkage. */
10242 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10244 cp_lexer_consume_token (parser->lexer);
10245 /* See if we might have an id-expression. */
10246 token = cp_lexer_peek_token (parser->lexer);
10247 if (token->type == CPP_NAME
10248 || token->keyword == RID_OPERATOR
10249 || token->type == CPP_SCOPE
10250 || token->type == CPP_TEMPLATE_ID
10251 || token->type == CPP_NESTED_NAME_SPECIFIER)
10253 cp_parser_parse_tentatively (parser);
10254 argument = cp_parser_primary_expression (parser,
10257 /*template_arg_p=*/true,
10259 if (cp_parser_error_occurred (parser)
10260 || !cp_parser_next_token_ends_template_argument_p (parser))
10261 cp_parser_abort_tentative_parse (parser);
10264 if (TREE_CODE (argument) == INDIRECT_REF)
10266 gcc_assert (REFERENCE_REF_P (argument));
10267 argument = TREE_OPERAND (argument, 0);
10270 if (TREE_CODE (argument) == VAR_DECL)
10272 /* A variable without external linkage might still be a
10273 valid constant-expression, so no error is issued here
10274 if the external-linkage check fails. */
10275 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10276 cp_parser_simulate_error (parser);
10278 else if (is_overloaded_fn (argument))
10279 /* All overloaded functions are allowed; if the external
10280 linkage test does not pass, an error will be issued
10284 && (TREE_CODE (argument) == OFFSET_REF
10285 || TREE_CODE (argument) == SCOPE_REF))
10286 /* A pointer-to-member. */
10288 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10291 cp_parser_simulate_error (parser);
10293 if (cp_parser_parse_definitely (parser))
10296 argument = build_x_unary_op (ADDR_EXPR, argument);
10301 /* If the argument started with "&", there are no other valid
10302 alternatives at this point. */
10305 cp_parser_error (parser, "invalid non-type template argument");
10306 return error_mark_node;
10309 /* If the argument wasn't successfully parsed as a type-id followed
10310 by '>>', the argument can only be a constant expression now.
10311 Otherwise, we try parsing the constant-expression tentatively,
10312 because the argument could really be a type-id. */
10314 cp_parser_parse_tentatively (parser);
10315 argument = cp_parser_constant_expression (parser,
10316 /*allow_non_constant_p=*/false,
10317 /*non_constant_p=*/NULL);
10318 argument = fold_non_dependent_expr (argument);
10319 if (!maybe_type_id)
10321 if (!cp_parser_next_token_ends_template_argument_p (parser))
10322 cp_parser_error (parser, "expected template-argument");
10323 if (cp_parser_parse_definitely (parser))
10325 /* We did our best to parse the argument as a non type-id, but that
10326 was the only alternative that matched (albeit with a '>' after
10327 it). We can assume it's just a typo from the user, and a
10328 diagnostic will then be issued. */
10329 return cp_parser_type_id (parser);
10332 /* Parse an explicit-instantiation.
10334 explicit-instantiation:
10335 template declaration
10337 Although the standard says `declaration', what it really means is:
10339 explicit-instantiation:
10340 template decl-specifier-seq [opt] declarator [opt] ;
10342 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10343 supposed to be allowed. A defect report has been filed about this
10348 explicit-instantiation:
10349 storage-class-specifier template
10350 decl-specifier-seq [opt] declarator [opt] ;
10351 function-specifier template
10352 decl-specifier-seq [opt] declarator [opt] ; */
10355 cp_parser_explicit_instantiation (cp_parser* parser)
10357 int declares_class_or_enum;
10358 cp_decl_specifier_seq decl_specifiers;
10359 tree extension_specifier = NULL_TREE;
10361 /* Look for an (optional) storage-class-specifier or
10362 function-specifier. */
10363 if (cp_parser_allow_gnu_extensions_p (parser))
10365 extension_specifier
10366 = cp_parser_storage_class_specifier_opt (parser);
10367 if (!extension_specifier)
10368 extension_specifier
10369 = cp_parser_function_specifier_opt (parser,
10370 /*decl_specs=*/NULL);
10373 /* Look for the `template' keyword. */
10374 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10375 /* Let the front end know that we are processing an explicit
10377 begin_explicit_instantiation ();
10378 /* [temp.explicit] says that we are supposed to ignore access
10379 control while processing explicit instantiation directives. */
10380 push_deferring_access_checks (dk_no_check);
10381 /* Parse a decl-specifier-seq. */
10382 cp_parser_decl_specifier_seq (parser,
10383 CP_PARSER_FLAGS_OPTIONAL,
10385 &declares_class_or_enum);
10386 /* If there was exactly one decl-specifier, and it declared a class,
10387 and there's no declarator, then we have an explicit type
10389 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10393 type = check_tag_decl (&decl_specifiers);
10394 /* Turn access control back on for names used during
10395 template instantiation. */
10396 pop_deferring_access_checks ();
10398 do_type_instantiation (type, extension_specifier,
10399 /*complain=*/tf_error);
10403 cp_declarator *declarator;
10406 /* Parse the declarator. */
10408 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10409 /*ctor_dtor_or_conv_p=*/NULL,
10410 /*parenthesized_p=*/NULL,
10411 /*member_p=*/false);
10412 if (declares_class_or_enum & 2)
10413 cp_parser_check_for_definition_in_return_type (declarator,
10414 decl_specifiers.type);
10415 if (declarator != cp_error_declarator)
10417 decl = grokdeclarator (declarator, &decl_specifiers,
10418 NORMAL, 0, &decl_specifiers.attributes);
10419 /* Turn access control back on for names used during
10420 template instantiation. */
10421 pop_deferring_access_checks ();
10422 /* Do the explicit instantiation. */
10423 do_decl_instantiation (decl, extension_specifier);
10427 pop_deferring_access_checks ();
10428 /* Skip the body of the explicit instantiation. */
10429 cp_parser_skip_to_end_of_statement (parser);
10432 /* We're done with the instantiation. */
10433 end_explicit_instantiation ();
10435 cp_parser_consume_semicolon_at_end_of_statement (parser);
10438 /* Parse an explicit-specialization.
10440 explicit-specialization:
10441 template < > declaration
10443 Although the standard says `declaration', what it really means is:
10445 explicit-specialization:
10446 template <> decl-specifier [opt] init-declarator [opt] ;
10447 template <> function-definition
10448 template <> explicit-specialization
10449 template <> template-declaration */
10452 cp_parser_explicit_specialization (cp_parser* parser)
10454 bool need_lang_pop;
10455 /* Look for the `template' keyword. */
10456 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10457 /* Look for the `<'. */
10458 cp_parser_require (parser, CPP_LESS, "`<'");
10459 /* Look for the `>'. */
10460 cp_parser_require (parser, CPP_GREATER, "`>'");
10461 /* We have processed another parameter list. */
10462 ++parser->num_template_parameter_lists;
10465 A template ... explicit specialization ... shall not have C
10467 if (current_lang_name == lang_name_c)
10469 error ("template specialization with C linkage");
10470 /* Give it C++ linkage to avoid confusing other parts of the
10472 push_lang_context (lang_name_cplusplus);
10473 need_lang_pop = true;
10476 need_lang_pop = false;
10477 /* Let the front end know that we are beginning a specialization. */
10478 if (!begin_specialization ())
10480 end_specialization ();
10481 cp_parser_skip_to_end_of_block_or_statement (parser);
10485 /* If the next keyword is `template', we need to figure out whether
10486 or not we're looking a template-declaration. */
10487 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10489 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10490 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10491 cp_parser_template_declaration_after_export (parser,
10492 /*member_p=*/false);
10494 cp_parser_explicit_specialization (parser);
10497 /* Parse the dependent declaration. */
10498 cp_parser_single_declaration (parser,
10500 /*member_p=*/false,
10501 /*explicit_specialization_p=*/true,
10502 /*friend_p=*/NULL);
10503 /* We're done with the specialization. */
10504 end_specialization ();
10505 /* For the erroneous case of a template with C linkage, we pushed an
10506 implicit C++ linkage scope; exit that scope now. */
10508 pop_lang_context ();
10509 /* We're done with this parameter list. */
10510 --parser->num_template_parameter_lists;
10513 /* Parse a type-specifier.
10516 simple-type-specifier
10519 elaborated-type-specifier
10527 Returns a representation of the type-specifier. For a
10528 class-specifier, enum-specifier, or elaborated-type-specifier, a
10529 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10531 The parser flags FLAGS is used to control type-specifier parsing.
10533 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10534 in a decl-specifier-seq.
10536 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10537 class-specifier, enum-specifier, or elaborated-type-specifier, then
10538 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10539 if a type is declared; 2 if it is defined. Otherwise, it is set to
10542 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10543 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10544 is set to FALSE. */
10547 cp_parser_type_specifier (cp_parser* parser,
10548 cp_parser_flags flags,
10549 cp_decl_specifier_seq *decl_specs,
10550 bool is_declaration,
10551 int* declares_class_or_enum,
10552 bool* is_cv_qualifier)
10554 tree type_spec = NULL_TREE;
10557 cp_decl_spec ds = ds_last;
10559 /* Assume this type-specifier does not declare a new type. */
10560 if (declares_class_or_enum)
10561 *declares_class_or_enum = 0;
10562 /* And that it does not specify a cv-qualifier. */
10563 if (is_cv_qualifier)
10564 *is_cv_qualifier = false;
10565 /* Peek at the next token. */
10566 token = cp_lexer_peek_token (parser->lexer);
10568 /* If we're looking at a keyword, we can use that to guide the
10569 production we choose. */
10570 keyword = token->keyword;
10574 /* Look for the enum-specifier. */
10575 type_spec = cp_parser_enum_specifier (parser);
10576 /* If that worked, we're done. */
10579 if (declares_class_or_enum)
10580 *declares_class_or_enum = 2;
10582 cp_parser_set_decl_spec_type (decl_specs,
10584 /*user_defined_p=*/true);
10588 goto elaborated_type_specifier;
10590 /* Any of these indicate either a class-specifier, or an
10591 elaborated-type-specifier. */
10595 /* Parse tentatively so that we can back up if we don't find a
10596 class-specifier. */
10597 cp_parser_parse_tentatively (parser);
10598 /* Look for the class-specifier. */
10599 type_spec = cp_parser_class_specifier (parser);
10600 /* If that worked, we're done. */
10601 if (cp_parser_parse_definitely (parser))
10603 if (declares_class_or_enum)
10604 *declares_class_or_enum = 2;
10606 cp_parser_set_decl_spec_type (decl_specs,
10608 /*user_defined_p=*/true);
10612 /* Fall through. */
10613 elaborated_type_specifier:
10614 /* We're declaring (not defining) a class or enum. */
10615 if (declares_class_or_enum)
10616 *declares_class_or_enum = 1;
10618 /* Fall through. */
10620 /* Look for an elaborated-type-specifier. */
10622 = (cp_parser_elaborated_type_specifier
10624 decl_specs && decl_specs->specs[(int) ds_friend],
10627 cp_parser_set_decl_spec_type (decl_specs,
10629 /*user_defined_p=*/true);
10634 if (is_cv_qualifier)
10635 *is_cv_qualifier = true;
10640 if (is_cv_qualifier)
10641 *is_cv_qualifier = true;
10646 if (is_cv_qualifier)
10647 *is_cv_qualifier = true;
10651 /* The `__complex__' keyword is a GNU extension. */
10659 /* Handle simple keywords. */
10664 ++decl_specs->specs[(int)ds];
10665 decl_specs->any_specifiers_p = true;
10667 return cp_lexer_consume_token (parser->lexer)->u.value;
10670 /* If we do not already have a type-specifier, assume we are looking
10671 at a simple-type-specifier. */
10672 type_spec = cp_parser_simple_type_specifier (parser,
10676 /* If we didn't find a type-specifier, and a type-specifier was not
10677 optional in this context, issue an error message. */
10678 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10680 cp_parser_error (parser, "expected type specifier");
10681 return error_mark_node;
10687 /* Parse a simple-type-specifier.
10689 simple-type-specifier:
10690 :: [opt] nested-name-specifier [opt] type-name
10691 :: [opt] nested-name-specifier template template-id
10706 simple-type-specifier:
10707 decltype ( expression )
10711 simple-type-specifier:
10712 __typeof__ unary-expression
10713 __typeof__ ( type-id )
10715 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10716 appropriately updated. */
10719 cp_parser_simple_type_specifier (cp_parser* parser,
10720 cp_decl_specifier_seq *decl_specs,
10721 cp_parser_flags flags)
10723 tree type = NULL_TREE;
10726 /* Peek at the next token. */
10727 token = cp_lexer_peek_token (parser->lexer);
10729 /* If we're looking at a keyword, things are easy. */
10730 switch (token->keyword)
10734 decl_specs->explicit_char_p = true;
10735 type = char_type_node;
10738 type = wchar_type_node;
10741 type = boolean_type_node;
10745 ++decl_specs->specs[(int) ds_short];
10746 type = short_integer_type_node;
10750 decl_specs->explicit_int_p = true;
10751 type = integer_type_node;
10755 ++decl_specs->specs[(int) ds_long];
10756 type = long_integer_type_node;
10760 ++decl_specs->specs[(int) ds_signed];
10761 type = integer_type_node;
10765 ++decl_specs->specs[(int) ds_unsigned];
10766 type = unsigned_type_node;
10769 type = float_type_node;
10772 type = double_type_node;
10775 type = void_type_node;
10779 /* Parse the `decltype' type. */
10780 type = cp_parser_decltype (parser);
10783 cp_parser_set_decl_spec_type (decl_specs, type,
10784 /*user_defined_p=*/true);
10789 /* Consume the `typeof' token. */
10790 cp_lexer_consume_token (parser->lexer);
10791 /* Parse the operand to `typeof'. */
10792 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10793 /* If it is not already a TYPE, take its type. */
10794 if (!TYPE_P (type))
10795 type = finish_typeof (type);
10798 cp_parser_set_decl_spec_type (decl_specs, type,
10799 /*user_defined_p=*/true);
10807 /* If the type-specifier was for a built-in type, we're done. */
10812 /* Record the type. */
10814 && (token->keyword != RID_SIGNED
10815 && token->keyword != RID_UNSIGNED
10816 && token->keyword != RID_SHORT
10817 && token->keyword != RID_LONG))
10818 cp_parser_set_decl_spec_type (decl_specs,
10820 /*user_defined=*/false);
10822 decl_specs->any_specifiers_p = true;
10824 /* Consume the token. */
10825 id = cp_lexer_consume_token (parser->lexer)->u.value;
10827 /* There is no valid C++ program where a non-template type is
10828 followed by a "<". That usually indicates that the user thought
10829 that the type was a template. */
10830 cp_parser_check_for_invalid_template_id (parser, type);
10832 return TYPE_NAME (type);
10835 /* The type-specifier must be a user-defined type. */
10836 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10841 /* Don't gobble tokens or issue error messages if this is an
10842 optional type-specifier. */
10843 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10844 cp_parser_parse_tentatively (parser);
10846 /* Look for the optional `::' operator. */
10848 = (cp_parser_global_scope_opt (parser,
10849 /*current_scope_valid_p=*/false)
10851 /* Look for the nested-name specifier. */
10853 = (cp_parser_nested_name_specifier_opt (parser,
10854 /*typename_keyword_p=*/false,
10855 /*check_dependency_p=*/true,
10857 /*is_declaration=*/false)
10859 /* If we have seen a nested-name-specifier, and the next token
10860 is `template', then we are using the template-id production. */
10862 && cp_parser_optional_template_keyword (parser))
10864 /* Look for the template-id. */
10865 type = cp_parser_template_id (parser,
10866 /*template_keyword_p=*/true,
10867 /*check_dependency_p=*/true,
10868 /*is_declaration=*/false);
10869 /* If the template-id did not name a type, we are out of
10871 if (TREE_CODE (type) != TYPE_DECL)
10873 cp_parser_error (parser, "expected template-id for type");
10877 /* Otherwise, look for a type-name. */
10879 type = cp_parser_type_name (parser);
10880 /* Keep track of all name-lookups performed in class scopes. */
10884 && TREE_CODE (type) == TYPE_DECL
10885 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10886 maybe_note_name_used_in_class (DECL_NAME (type), type);
10887 /* If it didn't work out, we don't have a TYPE. */
10888 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10889 && !cp_parser_parse_definitely (parser))
10891 if (type && decl_specs)
10892 cp_parser_set_decl_spec_type (decl_specs, type,
10893 /*user_defined=*/true);
10896 /* If we didn't get a type-name, issue an error message. */
10897 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10899 cp_parser_error (parser, "expected type-name");
10900 return error_mark_node;
10903 /* There is no valid C++ program where a non-template type is
10904 followed by a "<". That usually indicates that the user thought
10905 that the type was a template. */
10906 if (type && type != error_mark_node)
10908 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10909 If it is, then the '<'...'>' enclose protocol names rather than
10910 template arguments, and so everything is fine. */
10911 if (c_dialect_objc ()
10912 && (objc_is_id (type) || objc_is_class_name (type)))
10914 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10915 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10917 /* Clobber the "unqualified" type previously entered into
10918 DECL_SPECS with the new, improved protocol-qualified version. */
10920 decl_specs->type = qual_type;
10925 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10931 /* Parse a type-name.
10944 Returns a TYPE_DECL for the type. */
10947 cp_parser_type_name (cp_parser* parser)
10952 /* We can't know yet whether it is a class-name or not. */
10953 cp_parser_parse_tentatively (parser);
10954 /* Try a class-name. */
10955 type_decl = cp_parser_class_name (parser,
10956 /*typename_keyword_p=*/false,
10957 /*template_keyword_p=*/false,
10959 /*check_dependency_p=*/true,
10960 /*class_head_p=*/false,
10961 /*is_declaration=*/false);
10962 /* If it's not a class-name, keep looking. */
10963 if (!cp_parser_parse_definitely (parser))
10965 /* It must be a typedef-name or an enum-name. */
10966 identifier = cp_parser_identifier (parser);
10967 if (identifier == error_mark_node)
10968 return error_mark_node;
10970 /* Look up the type-name. */
10971 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10973 if (TREE_CODE (type_decl) != TYPE_DECL
10974 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10976 /* See if this is an Objective-C type. */
10977 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10978 tree type = objc_get_protocol_qualified_type (identifier, protos);
10980 type_decl = TYPE_NAME (type);
10983 /* Issue an error if we did not find a type-name. */
10984 if (TREE_CODE (type_decl) != TYPE_DECL)
10986 if (!cp_parser_simulate_error (parser))
10987 cp_parser_name_lookup_error (parser, identifier, type_decl,
10989 type_decl = error_mark_node;
10991 /* Remember that the name was used in the definition of the
10992 current class so that we can check later to see if the
10993 meaning would have been different after the class was
10994 entirely defined. */
10995 else if (type_decl != error_mark_node
10997 maybe_note_name_used_in_class (identifier, type_decl);
11004 /* Parse an elaborated-type-specifier. Note that the grammar given
11005 here incorporates the resolution to DR68.
11007 elaborated-type-specifier:
11008 class-key :: [opt] nested-name-specifier [opt] identifier
11009 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11010 enum :: [opt] nested-name-specifier [opt] identifier
11011 typename :: [opt] nested-name-specifier identifier
11012 typename :: [opt] nested-name-specifier template [opt]
11017 elaborated-type-specifier:
11018 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11019 class-key attributes :: [opt] nested-name-specifier [opt]
11020 template [opt] template-id
11021 enum attributes :: [opt] nested-name-specifier [opt] identifier
11023 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11024 declared `friend'. If IS_DECLARATION is TRUE, then this
11025 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11026 something is being declared.
11028 Returns the TYPE specified. */
11031 cp_parser_elaborated_type_specifier (cp_parser* parser,
11033 bool is_declaration)
11035 enum tag_types tag_type;
11037 tree type = NULL_TREE;
11038 tree attributes = NULL_TREE;
11040 /* See if we're looking at the `enum' keyword. */
11041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11043 /* Consume the `enum' token. */
11044 cp_lexer_consume_token (parser->lexer);
11045 /* Remember that it's an enumeration type. */
11046 tag_type = enum_type;
11047 /* Parse the attributes. */
11048 attributes = cp_parser_attributes_opt (parser);
11050 /* Or, it might be `typename'. */
11051 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11054 /* Consume the `typename' token. */
11055 cp_lexer_consume_token (parser->lexer);
11056 /* Remember that it's a `typename' type. */
11057 tag_type = typename_type;
11058 /* The `typename' keyword is only allowed in templates. */
11059 if (!processing_template_decl)
11060 pedwarn ("using %<typename%> outside of template");
11062 /* Otherwise it must be a class-key. */
11065 tag_type = cp_parser_class_key (parser);
11066 if (tag_type == none_type)
11067 return error_mark_node;
11068 /* Parse the attributes. */
11069 attributes = cp_parser_attributes_opt (parser);
11072 /* Look for the `::' operator. */
11073 cp_parser_global_scope_opt (parser,
11074 /*current_scope_valid_p=*/false);
11075 /* Look for the nested-name-specifier. */
11076 if (tag_type == typename_type)
11078 if (!cp_parser_nested_name_specifier (parser,
11079 /*typename_keyword_p=*/true,
11080 /*check_dependency_p=*/true,
11083 return error_mark_node;
11086 /* Even though `typename' is not present, the proposed resolution
11087 to Core Issue 180 says that in `class A<T>::B', `B' should be
11088 considered a type-name, even if `A<T>' is dependent. */
11089 cp_parser_nested_name_specifier_opt (parser,
11090 /*typename_keyword_p=*/true,
11091 /*check_dependency_p=*/true,
11094 /* For everything but enumeration types, consider a template-id.
11095 For an enumeration type, consider only a plain identifier. */
11096 if (tag_type != enum_type)
11098 bool template_p = false;
11101 /* Allow the `template' keyword. */
11102 template_p = cp_parser_optional_template_keyword (parser);
11103 /* If we didn't see `template', we don't know if there's a
11104 template-id or not. */
11106 cp_parser_parse_tentatively (parser);
11107 /* Parse the template-id. */
11108 decl = cp_parser_template_id (parser, template_p,
11109 /*check_dependency_p=*/true,
11111 /* If we didn't find a template-id, look for an ordinary
11113 if (!template_p && !cp_parser_parse_definitely (parser))
11115 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11116 in effect, then we must assume that, upon instantiation, the
11117 template will correspond to a class. */
11118 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11119 && tag_type == typename_type)
11120 type = make_typename_type (parser->scope, decl,
11122 /*complain=*/tf_error);
11124 type = TREE_TYPE (decl);
11129 identifier = cp_parser_identifier (parser);
11131 if (identifier == error_mark_node)
11133 parser->scope = NULL_TREE;
11134 return error_mark_node;
11137 /* For a `typename', we needn't call xref_tag. */
11138 if (tag_type == typename_type
11139 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11140 return cp_parser_make_typename_type (parser, parser->scope,
11142 /* Look up a qualified name in the usual way. */
11146 tree ambiguous_decls;
11148 decl = cp_parser_lookup_name (parser, identifier,
11150 /*is_template=*/false,
11151 /*is_namespace=*/false,
11152 /*check_dependency=*/true,
11155 /* If the lookup was ambiguous, an error will already have been
11157 if (ambiguous_decls)
11158 return error_mark_node;
11160 /* If we are parsing friend declaration, DECL may be a
11161 TEMPLATE_DECL tree node here. However, we need to check
11162 whether this TEMPLATE_DECL results in valid code. Consider
11163 the following example:
11166 template <class T> class C {};
11169 template <class T> friend class N::C; // #1, valid code
11171 template <class T> class Y {
11172 friend class N::C; // #2, invalid code
11175 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11176 name lookup of `N::C'. We see that friend declaration must
11177 be template for the code to be valid. Note that
11178 processing_template_decl does not work here since it is
11179 always 1 for the above two cases. */
11181 decl = (cp_parser_maybe_treat_template_as_class
11182 (decl, /*tag_name_p=*/is_friend
11183 && parser->num_template_parameter_lists));
11185 if (TREE_CODE (decl) != TYPE_DECL)
11187 cp_parser_diagnose_invalid_type_name (parser,
11190 return error_mark_node;
11193 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11195 bool allow_template = (parser->num_template_parameter_lists
11196 || DECL_SELF_REFERENCE_P (decl));
11197 type = check_elaborated_type_specifier (tag_type, decl,
11200 if (type == error_mark_node)
11201 return error_mark_node;
11204 /* Forward declarations of nested types, such as
11209 are invalid unless all components preceding the final '::'
11210 are complete. If all enclosing types are complete, these
11211 declarations become merely pointless.
11213 Invalid forward declarations of nested types are errors
11214 caught elsewhere in parsing. Those that are pointless arrive
11217 if (cp_parser_declares_only_class_p (parser)
11218 && !is_friend && !processing_explicit_instantiation)
11219 warning (0, "declaration %qD does not declare anything", decl);
11221 type = TREE_TYPE (decl);
11225 /* An elaborated-type-specifier sometimes introduces a new type and
11226 sometimes names an existing type. Normally, the rule is that it
11227 introduces a new type only if there is not an existing type of
11228 the same name already in scope. For example, given:
11231 void f() { struct S s; }
11233 the `struct S' in the body of `f' is the same `struct S' as in
11234 the global scope; the existing definition is used. However, if
11235 there were no global declaration, this would introduce a new
11236 local class named `S'.
11238 An exception to this rule applies to the following code:
11240 namespace N { struct S; }
11242 Here, the elaborated-type-specifier names a new type
11243 unconditionally; even if there is already an `S' in the
11244 containing scope this declaration names a new type.
11245 This exception only applies if the elaborated-type-specifier
11246 forms the complete declaration:
11250 A declaration consisting solely of `class-key identifier ;' is
11251 either a redeclaration of the name in the current scope or a
11252 forward declaration of the identifier as a class name. It
11253 introduces the name into the current scope.
11255 We are in this situation precisely when the next token is a `;'.
11257 An exception to the exception is that a `friend' declaration does
11258 *not* name a new type; i.e., given:
11260 struct S { friend struct T; };
11262 `T' is not a new type in the scope of `S'.
11264 Also, `new struct S' or `sizeof (struct S)' never results in the
11265 definition of a new type; a new type can only be declared in a
11266 declaration context. */
11272 /* Friends have special name lookup rules. */
11273 ts = ts_within_enclosing_non_class;
11274 else if (is_declaration
11275 && cp_lexer_next_token_is (parser->lexer,
11277 /* This is a `class-key identifier ;' */
11283 (parser->num_template_parameter_lists
11284 && (cp_parser_next_token_starts_class_definition_p (parser)
11285 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11286 /* An unqualified name was used to reference this type, so
11287 there were no qualifying templates. */
11288 if (!cp_parser_check_template_parameters (parser,
11289 /*num_templates=*/0))
11290 return error_mark_node;
11291 type = xref_tag (tag_type, identifier, ts, template_p);
11295 if (type == error_mark_node)
11296 return error_mark_node;
11298 /* Allow attributes on forward declarations of classes. */
11301 if (TREE_CODE (type) == TYPENAME_TYPE)
11302 warning (OPT_Wattributes,
11303 "attributes ignored on uninstantiated type");
11304 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11305 && ! processing_explicit_instantiation)
11306 warning (OPT_Wattributes,
11307 "attributes ignored on template instantiation");
11308 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11309 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11311 warning (OPT_Wattributes,
11312 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11315 if (tag_type != enum_type)
11316 cp_parser_check_class_key (tag_type, type);
11318 /* A "<" cannot follow an elaborated type specifier. If that
11319 happens, the user was probably trying to form a template-id. */
11320 cp_parser_check_for_invalid_template_id (parser, type);
11325 /* Parse an enum-specifier.
11328 enum identifier [opt] { enumerator-list [opt] }
11331 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11334 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11335 if the token stream isn't an enum-specifier after all. */
11338 cp_parser_enum_specifier (cp_parser* parser)
11344 /* Parse tentatively so that we can back up if we don't find a
11346 cp_parser_parse_tentatively (parser);
11348 /* Caller guarantees that the current token is 'enum', an identifier
11349 possibly follows, and the token after that is an opening brace.
11350 If we don't have an identifier, fabricate an anonymous name for
11351 the enumeration being defined. */
11352 cp_lexer_consume_token (parser->lexer);
11354 attributes = cp_parser_attributes_opt (parser);
11356 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11357 identifier = cp_parser_identifier (parser);
11359 identifier = make_anon_name ();
11361 /* Look for the `{' but don't consume it yet. */
11362 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11363 cp_parser_simulate_error (parser);
11365 if (!cp_parser_parse_definitely (parser))
11368 /* Issue an error message if type-definitions are forbidden here. */
11369 if (!cp_parser_check_type_definition (parser))
11370 type = error_mark_node;
11372 /* Create the new type. We do this before consuming the opening
11373 brace so the enum will be recorded as being on the line of its
11374 tag (or the 'enum' keyword, if there is no tag). */
11375 type = start_enum (identifier);
11377 /* Consume the opening brace. */
11378 cp_lexer_consume_token (parser->lexer);
11380 if (type == error_mark_node)
11382 cp_parser_skip_to_end_of_block_or_statement (parser);
11383 return error_mark_node;
11386 /* If the next token is not '}', then there are some enumerators. */
11387 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11388 cp_parser_enumerator_list (parser, type);
11390 /* Consume the final '}'. */
11391 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11393 /* Look for trailing attributes to apply to this enumeration, and
11394 apply them if appropriate. */
11395 if (cp_parser_allow_gnu_extensions_p (parser))
11397 tree trailing_attr = cp_parser_attributes_opt (parser);
11398 cplus_decl_attributes (&type,
11400 (int) ATTR_FLAG_TYPE_IN_PLACE);
11403 /* Finish up the enumeration. */
11404 finish_enum (type);
11409 /* Parse an enumerator-list. The enumerators all have the indicated
11413 enumerator-definition
11414 enumerator-list , enumerator-definition */
11417 cp_parser_enumerator_list (cp_parser* parser, tree type)
11421 /* Parse an enumerator-definition. */
11422 cp_parser_enumerator_definition (parser, type);
11424 /* If the next token is not a ',', we've reached the end of
11426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11428 /* Otherwise, consume the `,' and keep going. */
11429 cp_lexer_consume_token (parser->lexer);
11430 /* If the next token is a `}', there is a trailing comma. */
11431 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11433 if (pedantic && !in_system_header)
11434 pedwarn ("comma at end of enumerator list");
11440 /* Parse an enumerator-definition. The enumerator has the indicated
11443 enumerator-definition:
11445 enumerator = constant-expression
11451 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11456 /* Look for the identifier. */
11457 identifier = cp_parser_identifier (parser);
11458 if (identifier == error_mark_node)
11461 /* If the next token is an '=', then there is an explicit value. */
11462 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11464 /* Consume the `=' token. */
11465 cp_lexer_consume_token (parser->lexer);
11466 /* Parse the value. */
11467 value = cp_parser_constant_expression (parser,
11468 /*allow_non_constant_p=*/false,
11474 /* Create the enumerator. */
11475 build_enumerator (identifier, value, type);
11478 /* Parse a namespace-name.
11481 original-namespace-name
11484 Returns the NAMESPACE_DECL for the namespace. */
11487 cp_parser_namespace_name (cp_parser* parser)
11490 tree namespace_decl;
11492 /* Get the name of the namespace. */
11493 identifier = cp_parser_identifier (parser);
11494 if (identifier == error_mark_node)
11495 return error_mark_node;
11497 /* Look up the identifier in the currently active scope. Look only
11498 for namespaces, due to:
11500 [basic.lookup.udir]
11502 When looking up a namespace-name in a using-directive or alias
11503 definition, only namespace names are considered.
11507 [basic.lookup.qual]
11509 During the lookup of a name preceding the :: scope resolution
11510 operator, object, function, and enumerator names are ignored.
11512 (Note that cp_parser_class_or_namespace_name only calls this
11513 function if the token after the name is the scope resolution
11515 namespace_decl = cp_parser_lookup_name (parser, identifier,
11517 /*is_template=*/false,
11518 /*is_namespace=*/true,
11519 /*check_dependency=*/true,
11520 /*ambiguous_decls=*/NULL);
11521 /* If it's not a namespace, issue an error. */
11522 if (namespace_decl == error_mark_node
11523 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11525 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11526 error ("%qD is not a namespace-name", identifier);
11527 cp_parser_error (parser, "expected namespace-name");
11528 namespace_decl = error_mark_node;
11531 return namespace_decl;
11534 /* Parse a namespace-definition.
11536 namespace-definition:
11537 named-namespace-definition
11538 unnamed-namespace-definition
11540 named-namespace-definition:
11541 original-namespace-definition
11542 extension-namespace-definition
11544 original-namespace-definition:
11545 namespace identifier { namespace-body }
11547 extension-namespace-definition:
11548 namespace original-namespace-name { namespace-body }
11550 unnamed-namespace-definition:
11551 namespace { namespace-body } */
11554 cp_parser_namespace_definition (cp_parser* parser)
11556 tree identifier, attribs;
11557 bool has_visibility;
11560 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11563 cp_lexer_consume_token (parser->lexer);
11568 /* Look for the `namespace' keyword. */
11569 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11571 /* Get the name of the namespace. We do not attempt to distinguish
11572 between an original-namespace-definition and an
11573 extension-namespace-definition at this point. The semantic
11574 analysis routines are responsible for that. */
11575 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11576 identifier = cp_parser_identifier (parser);
11578 identifier = NULL_TREE;
11580 /* Parse any specified attributes. */
11581 attribs = cp_parser_attributes_opt (parser);
11583 /* Look for the `{' to start the namespace. */
11584 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11585 /* Start the namespace. */
11586 push_namespace (identifier);
11588 /* "inline namespace" is equivalent to a stub namespace definition
11589 followed by a strong using directive. */
11592 tree namespace = current_namespace;
11593 /* Set up namespace association. */
11594 DECL_NAMESPACE_ASSOCIATIONS (namespace)
11595 = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11596 DECL_NAMESPACE_ASSOCIATIONS (namespace));
11597 /* Import the contents of the inline namespace. */
11599 do_using_directive (namespace);
11600 push_namespace (identifier);
11603 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11605 /* Parse the body of the namespace. */
11606 cp_parser_namespace_body (parser);
11608 #ifdef HANDLE_PRAGMA_VISIBILITY
11609 if (has_visibility)
11613 /* Finish the namespace. */
11615 /* Look for the final `}'. */
11616 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11619 /* Parse a namespace-body.
11622 declaration-seq [opt] */
11625 cp_parser_namespace_body (cp_parser* parser)
11627 cp_parser_declaration_seq_opt (parser);
11630 /* Parse a namespace-alias-definition.
11632 namespace-alias-definition:
11633 namespace identifier = qualified-namespace-specifier ; */
11636 cp_parser_namespace_alias_definition (cp_parser* parser)
11639 tree namespace_specifier;
11641 /* Look for the `namespace' keyword. */
11642 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11643 /* Look for the identifier. */
11644 identifier = cp_parser_identifier (parser);
11645 if (identifier == error_mark_node)
11647 /* Look for the `=' token. */
11648 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11649 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11651 error ("%<namespace%> definition is not allowed here");
11652 /* Skip the definition. */
11653 cp_lexer_consume_token (parser->lexer);
11654 if (cp_parser_skip_to_closing_brace (parser))
11655 cp_lexer_consume_token (parser->lexer);
11658 cp_parser_require (parser, CPP_EQ, "`='");
11659 /* Look for the qualified-namespace-specifier. */
11660 namespace_specifier
11661 = cp_parser_qualified_namespace_specifier (parser);
11662 /* Look for the `;' token. */
11663 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11665 /* Register the alias in the symbol table. */
11666 do_namespace_alias (identifier, namespace_specifier);
11669 /* Parse a qualified-namespace-specifier.
11671 qualified-namespace-specifier:
11672 :: [opt] nested-name-specifier [opt] namespace-name
11674 Returns a NAMESPACE_DECL corresponding to the specified
11678 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11680 /* Look for the optional `::'. */
11681 cp_parser_global_scope_opt (parser,
11682 /*current_scope_valid_p=*/false);
11684 /* Look for the optional nested-name-specifier. */
11685 cp_parser_nested_name_specifier_opt (parser,
11686 /*typename_keyword_p=*/false,
11687 /*check_dependency_p=*/true,
11689 /*is_declaration=*/true);
11691 return cp_parser_namespace_name (parser);
11694 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11695 access declaration.
11698 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11699 using :: unqualified-id ;
11701 access-declaration:
11707 cp_parser_using_declaration (cp_parser* parser,
11708 bool access_declaration_p)
11711 bool typename_p = false;
11712 bool global_scope_p;
11717 if (access_declaration_p)
11718 cp_parser_parse_tentatively (parser);
11721 /* Look for the `using' keyword. */
11722 cp_parser_require_keyword (parser, RID_USING, "`using'");
11724 /* Peek at the next token. */
11725 token = cp_lexer_peek_token (parser->lexer);
11726 /* See if it's `typename'. */
11727 if (token->keyword == RID_TYPENAME)
11729 /* Remember that we've seen it. */
11731 /* Consume the `typename' token. */
11732 cp_lexer_consume_token (parser->lexer);
11736 /* Look for the optional global scope qualification. */
11738 = (cp_parser_global_scope_opt (parser,
11739 /*current_scope_valid_p=*/false)
11742 /* If we saw `typename', or didn't see `::', then there must be a
11743 nested-name-specifier present. */
11744 if (typename_p || !global_scope_p)
11745 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11746 /*check_dependency_p=*/true,
11748 /*is_declaration=*/true);
11749 /* Otherwise, we could be in either of the two productions. In that
11750 case, treat the nested-name-specifier as optional. */
11752 qscope = cp_parser_nested_name_specifier_opt (parser,
11753 /*typename_keyword_p=*/false,
11754 /*check_dependency_p=*/true,
11756 /*is_declaration=*/true);
11758 qscope = global_namespace;
11760 if (access_declaration_p && cp_parser_error_occurred (parser))
11761 /* Something has already gone wrong; there's no need to parse
11762 further. Since an error has occurred, the return value of
11763 cp_parser_parse_definitely will be false, as required. */
11764 return cp_parser_parse_definitely (parser);
11766 /* Parse the unqualified-id. */
11767 identifier = cp_parser_unqualified_id (parser,
11768 /*template_keyword_p=*/false,
11769 /*check_dependency_p=*/true,
11770 /*declarator_p=*/true,
11771 /*optional_p=*/false);
11773 if (access_declaration_p)
11775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11776 cp_parser_simulate_error (parser);
11777 if (!cp_parser_parse_definitely (parser))
11781 /* The function we call to handle a using-declaration is different
11782 depending on what scope we are in. */
11783 if (qscope == error_mark_node || identifier == error_mark_node)
11785 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11786 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11787 /* [namespace.udecl]
11789 A using declaration shall not name a template-id. */
11790 error ("a template-id may not appear in a using-declaration");
11793 if (at_class_scope_p ())
11795 /* Create the USING_DECL. */
11796 decl = do_class_using_decl (parser->scope, identifier);
11798 if (check_for_bare_parameter_packs (decl))
11801 /* Add it to the list of members in this class. */
11802 finish_member_declaration (decl);
11806 decl = cp_parser_lookup_name_simple (parser, identifier);
11807 if (decl == error_mark_node)
11808 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11809 else if (check_for_bare_parameter_packs (decl))
11811 else if (!at_namespace_scope_p ())
11812 do_local_using_decl (decl, qscope, identifier);
11814 do_toplevel_using_decl (decl, qscope, identifier);
11818 /* Look for the final `;'. */
11819 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11824 /* Parse a using-directive.
11827 using namespace :: [opt] nested-name-specifier [opt]
11828 namespace-name ; */
11831 cp_parser_using_directive (cp_parser* parser)
11833 tree namespace_decl;
11836 /* Look for the `using' keyword. */
11837 cp_parser_require_keyword (parser, RID_USING, "`using'");
11838 /* And the `namespace' keyword. */
11839 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11840 /* Look for the optional `::' operator. */
11841 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11842 /* And the optional nested-name-specifier. */
11843 cp_parser_nested_name_specifier_opt (parser,
11844 /*typename_keyword_p=*/false,
11845 /*check_dependency_p=*/true,
11847 /*is_declaration=*/true);
11848 /* Get the namespace being used. */
11849 namespace_decl = cp_parser_namespace_name (parser);
11850 /* And any specified attributes. */
11851 attribs = cp_parser_attributes_opt (parser);
11852 /* Update the symbol table. */
11853 parse_using_directive (namespace_decl, attribs);
11854 /* Look for the final `;'. */
11855 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11858 /* Parse an asm-definition.
11861 asm ( string-literal ) ;
11866 asm volatile [opt] ( string-literal ) ;
11867 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11868 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11869 : asm-operand-list [opt] ) ;
11870 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11871 : asm-operand-list [opt]
11872 : asm-operand-list [opt] ) ; */
11875 cp_parser_asm_definition (cp_parser* parser)
11878 tree outputs = NULL_TREE;
11879 tree inputs = NULL_TREE;
11880 tree clobbers = NULL_TREE;
11882 bool volatile_p = false;
11883 bool extended_p = false;
11884 bool invalid_inputs_p = false;
11885 bool invalid_outputs_p = false;
11887 /* Look for the `asm' keyword. */
11888 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11889 /* See if the next token is `volatile'. */
11890 if (cp_parser_allow_gnu_extensions_p (parser)
11891 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11893 /* Remember that we saw the `volatile' keyword. */
11895 /* Consume the token. */
11896 cp_lexer_consume_token (parser->lexer);
11898 /* Look for the opening `('. */
11899 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11901 /* Look for the string. */
11902 string = cp_parser_string_literal (parser, false, false);
11903 if (string == error_mark_node)
11905 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11906 /*consume_paren=*/true);
11910 /* If we're allowing GNU extensions, check for the extended assembly
11911 syntax. Unfortunately, the `:' tokens need not be separated by
11912 a space in C, and so, for compatibility, we tolerate that here
11913 too. Doing that means that we have to treat the `::' operator as
11915 if (cp_parser_allow_gnu_extensions_p (parser)
11916 && parser->in_function_body
11917 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11918 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11920 bool inputs_p = false;
11921 bool clobbers_p = false;
11923 /* The extended syntax was used. */
11926 /* Look for outputs. */
11927 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11929 /* Consume the `:'. */
11930 cp_lexer_consume_token (parser->lexer);
11931 /* Parse the output-operands. */
11932 if (cp_lexer_next_token_is_not (parser->lexer,
11934 && cp_lexer_next_token_is_not (parser->lexer,
11936 && cp_lexer_next_token_is_not (parser->lexer,
11938 outputs = cp_parser_asm_operand_list (parser);
11940 if (outputs == error_mark_node)
11941 invalid_outputs_p = true;
11943 /* If the next token is `::', there are no outputs, and the
11944 next token is the beginning of the inputs. */
11945 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11946 /* The inputs are coming next. */
11949 /* Look for inputs. */
11951 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11953 /* Consume the `:' or `::'. */
11954 cp_lexer_consume_token (parser->lexer);
11955 /* Parse the output-operands. */
11956 if (cp_lexer_next_token_is_not (parser->lexer,
11958 && cp_lexer_next_token_is_not (parser->lexer,
11960 inputs = cp_parser_asm_operand_list (parser);
11962 if (inputs == error_mark_node)
11963 invalid_inputs_p = true;
11965 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11966 /* The clobbers are coming next. */
11969 /* Look for clobbers. */
11971 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11973 /* Consume the `:' or `::'. */
11974 cp_lexer_consume_token (parser->lexer);
11975 /* Parse the clobbers. */
11976 if (cp_lexer_next_token_is_not (parser->lexer,
11978 clobbers = cp_parser_asm_clobber_list (parser);
11981 /* Look for the closing `)'. */
11982 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11983 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11984 /*consume_paren=*/true);
11985 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11987 if (!invalid_inputs_p && !invalid_outputs_p)
11989 /* Create the ASM_EXPR. */
11990 if (parser->in_function_body)
11992 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11994 /* If the extended syntax was not used, mark the ASM_EXPR. */
11997 tree temp = asm_stmt;
11998 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11999 temp = TREE_OPERAND (temp, 0);
12001 ASM_INPUT_P (temp) = 1;
12005 cgraph_add_asm_node (string);
12009 /* Declarators [gram.dcl.decl] */
12011 /* Parse an init-declarator.
12014 declarator initializer [opt]
12019 declarator asm-specification [opt] attributes [opt] initializer [opt]
12021 function-definition:
12022 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12024 decl-specifier-seq [opt] declarator function-try-block
12028 function-definition:
12029 __extension__ function-definition
12031 The DECL_SPECIFIERS apply to this declarator. Returns a
12032 representation of the entity declared. If MEMBER_P is TRUE, then
12033 this declarator appears in a class scope. The new DECL created by
12034 this declarator is returned.
12036 The CHECKS are access checks that should be performed once we know
12037 what entity is being declared (and, therefore, what classes have
12040 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12041 for a function-definition here as well. If the declarator is a
12042 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12043 be TRUE upon return. By that point, the function-definition will
12044 have been completely parsed.
12046 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12050 cp_parser_init_declarator (cp_parser* parser,
12051 cp_decl_specifier_seq *decl_specifiers,
12052 VEC (deferred_access_check,gc)* checks,
12053 bool function_definition_allowed_p,
12055 int declares_class_or_enum,
12056 bool* function_definition_p)
12059 cp_declarator *declarator;
12060 tree prefix_attributes;
12062 tree asm_specification;
12064 tree decl = NULL_TREE;
12066 bool is_initialized;
12067 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12068 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12070 enum cpp_ttype initialization_kind;
12071 bool is_parenthesized_init = false;
12072 bool is_non_constant_init;
12073 int ctor_dtor_or_conv_p;
12075 tree pushed_scope = NULL;
12077 /* Gather the attributes that were provided with the
12078 decl-specifiers. */
12079 prefix_attributes = decl_specifiers->attributes;
12081 /* Assume that this is not the declarator for a function
12083 if (function_definition_p)
12084 *function_definition_p = false;
12086 /* Defer access checks while parsing the declarator; we cannot know
12087 what names are accessible until we know what is being
12089 resume_deferring_access_checks ();
12091 /* Parse the declarator. */
12093 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12094 &ctor_dtor_or_conv_p,
12095 /*parenthesized_p=*/NULL,
12096 /*member_p=*/false);
12097 /* Gather up the deferred checks. */
12098 stop_deferring_access_checks ();
12100 /* If the DECLARATOR was erroneous, there's no need to go
12102 if (declarator == cp_error_declarator)
12103 return error_mark_node;
12105 /* Check that the number of template-parameter-lists is OK. */
12106 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12107 return error_mark_node;
12109 if (declares_class_or_enum & 2)
12110 cp_parser_check_for_definition_in_return_type (declarator,
12111 decl_specifiers->type);
12113 /* Figure out what scope the entity declared by the DECLARATOR is
12114 located in. `grokdeclarator' sometimes changes the scope, so
12115 we compute it now. */
12116 scope = get_scope_of_declarator (declarator);
12118 /* If we're allowing GNU extensions, look for an asm-specification
12120 if (cp_parser_allow_gnu_extensions_p (parser))
12122 /* Look for an asm-specification. */
12123 asm_specification = cp_parser_asm_specification_opt (parser);
12124 /* And attributes. */
12125 attributes = cp_parser_attributes_opt (parser);
12129 asm_specification = NULL_TREE;
12130 attributes = NULL_TREE;
12133 /* Peek at the next token. */
12134 token = cp_lexer_peek_token (parser->lexer);
12135 /* Check to see if the token indicates the start of a
12136 function-definition. */
12137 if (cp_parser_token_starts_function_definition_p (token))
12139 if (!function_definition_allowed_p)
12141 /* If a function-definition should not appear here, issue an
12143 cp_parser_error (parser,
12144 "a function-definition is not allowed here");
12145 return error_mark_node;
12149 /* Neither attributes nor an asm-specification are allowed
12150 on a function-definition. */
12151 if (asm_specification)
12152 error ("an asm-specification is not allowed on a function-definition");
12154 error ("attributes are not allowed on a function-definition");
12155 /* This is a function-definition. */
12156 *function_definition_p = true;
12158 /* Parse the function definition. */
12160 decl = cp_parser_save_member_function_body (parser,
12163 prefix_attributes);
12166 = (cp_parser_function_definition_from_specifiers_and_declarator
12167 (parser, decl_specifiers, prefix_attributes, declarator));
12175 Only in function declarations for constructors, destructors, and
12176 type conversions can the decl-specifier-seq be omitted.
12178 We explicitly postpone this check past the point where we handle
12179 function-definitions because we tolerate function-definitions
12180 that are missing their return types in some modes. */
12181 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12183 cp_parser_error (parser,
12184 "expected constructor, destructor, or type conversion");
12185 return error_mark_node;
12188 /* An `=' or an `(' indicates an initializer. */
12189 if (token->type == CPP_EQ
12190 || token->type == CPP_OPEN_PAREN)
12192 is_initialized = true;
12193 initialization_kind = token->type;
12197 /* If the init-declarator isn't initialized and isn't followed by a
12198 `,' or `;', it's not a valid init-declarator. */
12199 if (token->type != CPP_COMMA
12200 && token->type != CPP_SEMICOLON)
12202 cp_parser_error (parser, "expected initializer");
12203 return error_mark_node;
12205 is_initialized = false;
12206 initialization_kind = CPP_EOF;
12209 /* Because start_decl has side-effects, we should only call it if we
12210 know we're going ahead. By this point, we know that we cannot
12211 possibly be looking at any other construct. */
12212 cp_parser_commit_to_tentative_parse (parser);
12214 /* If the decl specifiers were bad, issue an error now that we're
12215 sure this was intended to be a declarator. Then continue
12216 declaring the variable(s), as int, to try to cut down on further
12218 if (decl_specifiers->any_specifiers_p
12219 && decl_specifiers->type == error_mark_node)
12221 cp_parser_error (parser, "invalid type in declaration");
12222 decl_specifiers->type = integer_type_node;
12225 /* Check to see whether or not this declaration is a friend. */
12226 friend_p = cp_parser_friend_p (decl_specifiers);
12228 /* Enter the newly declared entry in the symbol table. If we're
12229 processing a declaration in a class-specifier, we wait until
12230 after processing the initializer. */
12233 if (parser->in_unbraced_linkage_specification_p)
12234 decl_specifiers->storage_class = sc_extern;
12235 decl = start_decl (declarator, decl_specifiers,
12236 is_initialized, attributes, prefix_attributes,
12240 /* Enter the SCOPE. That way unqualified names appearing in the
12241 initializer will be looked up in SCOPE. */
12242 pushed_scope = push_scope (scope);
12244 /* Perform deferred access control checks, now that we know in which
12245 SCOPE the declared entity resides. */
12246 if (!member_p && decl)
12248 tree saved_current_function_decl = NULL_TREE;
12250 /* If the entity being declared is a function, pretend that we
12251 are in its scope. If it is a `friend', it may have access to
12252 things that would not otherwise be accessible. */
12253 if (TREE_CODE (decl) == FUNCTION_DECL)
12255 saved_current_function_decl = current_function_decl;
12256 current_function_decl = decl;
12259 /* Perform access checks for template parameters. */
12260 cp_parser_perform_template_parameter_access_checks (checks);
12262 /* Perform the access control checks for the declarator and the
12263 the decl-specifiers. */
12264 perform_deferred_access_checks ();
12266 /* Restore the saved value. */
12267 if (TREE_CODE (decl) == FUNCTION_DECL)
12268 current_function_decl = saved_current_function_decl;
12271 /* Parse the initializer. */
12272 initializer = NULL_TREE;
12273 is_parenthesized_init = false;
12274 is_non_constant_init = true;
12275 if (is_initialized)
12277 if (function_declarator_p (declarator))
12279 if (initialization_kind == CPP_EQ)
12280 initializer = cp_parser_pure_specifier (parser);
12283 /* If the declaration was erroneous, we don't really
12284 know what the user intended, so just silently
12285 consume the initializer. */
12286 if (decl != error_mark_node)
12287 error ("initializer provided for function");
12288 cp_parser_skip_to_closing_parenthesis (parser,
12289 /*recovering=*/true,
12290 /*or_comma=*/false,
12291 /*consume_paren=*/true);
12295 initializer = cp_parser_initializer (parser,
12296 &is_parenthesized_init,
12297 &is_non_constant_init);
12300 /* The old parser allows attributes to appear after a parenthesized
12301 initializer. Mark Mitchell proposed removing this functionality
12302 on the GCC mailing lists on 2002-08-13. This parser accepts the
12303 attributes -- but ignores them. */
12304 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12305 if (cp_parser_attributes_opt (parser))
12306 warning (OPT_Wattributes,
12307 "attributes after parenthesized initializer ignored");
12309 /* For an in-class declaration, use `grokfield' to create the
12315 pop_scope (pushed_scope);
12316 pushed_scope = false;
12318 decl = grokfield (declarator, decl_specifiers,
12319 initializer, !is_non_constant_init,
12320 /*asmspec=*/NULL_TREE,
12321 prefix_attributes);
12322 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12323 cp_parser_save_default_args (parser, decl);
12326 /* Finish processing the declaration. But, skip friend
12328 if (!friend_p && decl && decl != error_mark_node)
12330 cp_finish_decl (decl,
12331 initializer, !is_non_constant_init,
12333 /* If the initializer is in parentheses, then this is
12334 a direct-initialization, which means that an
12335 `explicit' constructor is OK. Otherwise, an
12336 `explicit' constructor cannot be used. */
12337 ((is_parenthesized_init || !is_initialized)
12338 ? 0 : LOOKUP_ONLYCONVERTING));
12340 else if ((cxx_dialect != cxx98) && friend_p
12341 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12342 /* Core issue #226 (C++0x only): A default template-argument
12343 shall not be specified in a friend class template
12345 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12346 /*is_partial=*/0, /*is_friend_decl=*/1);
12348 if (!friend_p && pushed_scope)
12349 pop_scope (pushed_scope);
12354 /* Parse a declarator.
12358 ptr-operator declarator
12360 abstract-declarator:
12361 ptr-operator abstract-declarator [opt]
12362 direct-abstract-declarator
12367 attributes [opt] direct-declarator
12368 attributes [opt] ptr-operator declarator
12370 abstract-declarator:
12371 attributes [opt] ptr-operator abstract-declarator [opt]
12372 attributes [opt] direct-abstract-declarator
12374 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12375 detect constructor, destructor or conversion operators. It is set
12376 to -1 if the declarator is a name, and +1 if it is a
12377 function. Otherwise it is set to zero. Usually you just want to
12378 test for >0, but internally the negative value is used.
12380 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12381 a decl-specifier-seq unless it declares a constructor, destructor,
12382 or conversion. It might seem that we could check this condition in
12383 semantic analysis, rather than parsing, but that makes it difficult
12384 to handle something like `f()'. We want to notice that there are
12385 no decl-specifiers, and therefore realize that this is an
12386 expression, not a declaration.)
12388 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12389 the declarator is a direct-declarator of the form "(...)".
12391 MEMBER_P is true iff this declarator is a member-declarator. */
12393 static cp_declarator *
12394 cp_parser_declarator (cp_parser* parser,
12395 cp_parser_declarator_kind dcl_kind,
12396 int* ctor_dtor_or_conv_p,
12397 bool* parenthesized_p,
12401 cp_declarator *declarator;
12402 enum tree_code code;
12403 cp_cv_quals cv_quals;
12405 tree attributes = NULL_TREE;
12407 /* Assume this is not a constructor, destructor, or type-conversion
12409 if (ctor_dtor_or_conv_p)
12410 *ctor_dtor_or_conv_p = 0;
12412 if (cp_parser_allow_gnu_extensions_p (parser))
12413 attributes = cp_parser_attributes_opt (parser);
12415 /* Peek at the next token. */
12416 token = cp_lexer_peek_token (parser->lexer);
12418 /* Check for the ptr-operator production. */
12419 cp_parser_parse_tentatively (parser);
12420 /* Parse the ptr-operator. */
12421 code = cp_parser_ptr_operator (parser,
12424 /* If that worked, then we have a ptr-operator. */
12425 if (cp_parser_parse_definitely (parser))
12427 /* If a ptr-operator was found, then this declarator was not
12429 if (parenthesized_p)
12430 *parenthesized_p = true;
12431 /* The dependent declarator is optional if we are parsing an
12432 abstract-declarator. */
12433 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12434 cp_parser_parse_tentatively (parser);
12436 /* Parse the dependent declarator. */
12437 declarator = cp_parser_declarator (parser, dcl_kind,
12438 /*ctor_dtor_or_conv_p=*/NULL,
12439 /*parenthesized_p=*/NULL,
12440 /*member_p=*/false);
12442 /* If we are parsing an abstract-declarator, we must handle the
12443 case where the dependent declarator is absent. */
12444 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12445 && !cp_parser_parse_definitely (parser))
12448 declarator = cp_parser_make_indirect_declarator
12449 (code, class_type, cv_quals, declarator);
12451 /* Everything else is a direct-declarator. */
12454 if (parenthesized_p)
12455 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12457 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12458 ctor_dtor_or_conv_p,
12462 if (attributes && declarator && declarator != cp_error_declarator)
12463 declarator->attributes = attributes;
12468 /* Parse a direct-declarator or direct-abstract-declarator.
12472 direct-declarator ( parameter-declaration-clause )
12473 cv-qualifier-seq [opt]
12474 exception-specification [opt]
12475 direct-declarator [ constant-expression [opt] ]
12478 direct-abstract-declarator:
12479 direct-abstract-declarator [opt]
12480 ( parameter-declaration-clause )
12481 cv-qualifier-seq [opt]
12482 exception-specification [opt]
12483 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12484 ( abstract-declarator )
12486 Returns a representation of the declarator. DCL_KIND is
12487 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12488 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12489 we are parsing a direct-declarator. It is
12490 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12491 of ambiguity we prefer an abstract declarator, as per
12492 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12493 cp_parser_declarator. */
12495 static cp_declarator *
12496 cp_parser_direct_declarator (cp_parser* parser,
12497 cp_parser_declarator_kind dcl_kind,
12498 int* ctor_dtor_or_conv_p,
12502 cp_declarator *declarator = NULL;
12503 tree scope = NULL_TREE;
12504 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12505 bool saved_in_declarator_p = parser->in_declarator_p;
12507 tree pushed_scope = NULL_TREE;
12511 /* Peek at the next token. */
12512 token = cp_lexer_peek_token (parser->lexer);
12513 if (token->type == CPP_OPEN_PAREN)
12515 /* This is either a parameter-declaration-clause, or a
12516 parenthesized declarator. When we know we are parsing a
12517 named declarator, it must be a parenthesized declarator
12518 if FIRST is true. For instance, `(int)' is a
12519 parameter-declaration-clause, with an omitted
12520 direct-abstract-declarator. But `((*))', is a
12521 parenthesized abstract declarator. Finally, when T is a
12522 template parameter `(T)' is a
12523 parameter-declaration-clause, and not a parenthesized
12526 We first try and parse a parameter-declaration-clause,
12527 and then try a nested declarator (if FIRST is true).
12529 It is not an error for it not to be a
12530 parameter-declaration-clause, even when FIRST is
12536 The first is the declaration of a function while the
12537 second is a the definition of a variable, including its
12540 Having seen only the parenthesis, we cannot know which of
12541 these two alternatives should be selected. Even more
12542 complex are examples like:
12547 The former is a function-declaration; the latter is a
12548 variable initialization.
12550 Thus again, we try a parameter-declaration-clause, and if
12551 that fails, we back out and return. */
12553 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12555 cp_parameter_declarator *params;
12556 unsigned saved_num_template_parameter_lists;
12558 /* In a member-declarator, the only valid interpretation
12559 of a parenthesis is the start of a
12560 parameter-declaration-clause. (It is invalid to
12561 initialize a static data member with a parenthesized
12562 initializer; only the "=" form of initialization is
12565 cp_parser_parse_tentatively (parser);
12567 /* Consume the `('. */
12568 cp_lexer_consume_token (parser->lexer);
12571 /* If this is going to be an abstract declarator, we're
12572 in a declarator and we can't have default args. */
12573 parser->default_arg_ok_p = false;
12574 parser->in_declarator_p = true;
12577 /* Inside the function parameter list, surrounding
12578 template-parameter-lists do not apply. */
12579 saved_num_template_parameter_lists
12580 = parser->num_template_parameter_lists;
12581 parser->num_template_parameter_lists = 0;
12583 /* Parse the parameter-declaration-clause. */
12584 params = cp_parser_parameter_declaration_clause (parser);
12586 parser->num_template_parameter_lists
12587 = saved_num_template_parameter_lists;
12589 /* If all went well, parse the cv-qualifier-seq and the
12590 exception-specification. */
12591 if (member_p || cp_parser_parse_definitely (parser))
12593 cp_cv_quals cv_quals;
12594 tree exception_specification;
12596 if (ctor_dtor_or_conv_p)
12597 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12599 /* Consume the `)'. */
12600 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12602 /* Parse the cv-qualifier-seq. */
12603 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12604 /* And the exception-specification. */
12605 exception_specification
12606 = cp_parser_exception_specification_opt (parser);
12608 /* Create the function-declarator. */
12609 declarator = make_call_declarator (declarator,
12612 exception_specification);
12613 /* Any subsequent parameter lists are to do with
12614 return type, so are not those of the declared
12616 parser->default_arg_ok_p = false;
12618 /* Repeat the main loop. */
12623 /* If this is the first, we can try a parenthesized
12627 bool saved_in_type_id_in_expr_p;
12629 parser->default_arg_ok_p = saved_default_arg_ok_p;
12630 parser->in_declarator_p = saved_in_declarator_p;
12632 /* Consume the `('. */
12633 cp_lexer_consume_token (parser->lexer);
12634 /* Parse the nested declarator. */
12635 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12636 parser->in_type_id_in_expr_p = true;
12638 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12639 /*parenthesized_p=*/NULL,
12641 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12643 /* Expect a `)'. */
12644 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12645 declarator = cp_error_declarator;
12646 if (declarator == cp_error_declarator)
12649 goto handle_declarator;
12651 /* Otherwise, we must be done. */
12655 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12656 && token->type == CPP_OPEN_SQUARE)
12658 /* Parse an array-declarator. */
12661 if (ctor_dtor_or_conv_p)
12662 *ctor_dtor_or_conv_p = 0;
12665 parser->default_arg_ok_p = false;
12666 parser->in_declarator_p = true;
12667 /* Consume the `['. */
12668 cp_lexer_consume_token (parser->lexer);
12669 /* Peek at the next token. */
12670 token = cp_lexer_peek_token (parser->lexer);
12671 /* If the next token is `]', then there is no
12672 constant-expression. */
12673 if (token->type != CPP_CLOSE_SQUARE)
12675 bool non_constant_p;
12678 = cp_parser_constant_expression (parser,
12679 /*allow_non_constant=*/true,
12681 if (!non_constant_p)
12682 bounds = fold_non_dependent_expr (bounds);
12683 /* Normally, the array bound must be an integral constant
12684 expression. However, as an extension, we allow VLAs
12685 in function scopes. */
12686 else if (!parser->in_function_body)
12688 error ("array bound is not an integer constant");
12689 bounds = error_mark_node;
12693 bounds = NULL_TREE;
12694 /* Look for the closing `]'. */
12695 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12697 declarator = cp_error_declarator;
12701 declarator = make_array_declarator (declarator, bounds);
12703 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12705 tree qualifying_scope;
12706 tree unqualified_name;
12707 special_function_kind sfk;
12709 bool pack_expansion_p = false;
12711 /* Parse a declarator-id */
12712 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12715 cp_parser_parse_tentatively (parser);
12717 /* If we see an ellipsis, we should be looking at a
12719 if (token->type == CPP_ELLIPSIS)
12721 /* Consume the `...' */
12722 cp_lexer_consume_token (parser->lexer);
12724 pack_expansion_p = true;
12729 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12730 qualifying_scope = parser->scope;
12735 if (!unqualified_name && pack_expansion_p)
12737 /* Check whether an error occurred. */
12738 okay = !cp_parser_error_occurred (parser);
12740 /* We already consumed the ellipsis to mark a
12741 parameter pack, but we have no way to report it,
12742 so abort the tentative parse. We will be exiting
12743 immediately anyway. */
12744 cp_parser_abort_tentative_parse (parser);
12747 okay = cp_parser_parse_definitely (parser);
12750 unqualified_name = error_mark_node;
12751 else if (unqualified_name
12752 && (qualifying_scope
12753 || (TREE_CODE (unqualified_name)
12754 != IDENTIFIER_NODE)))
12756 cp_parser_error (parser, "expected unqualified-id");
12757 unqualified_name = error_mark_node;
12761 if (!unqualified_name)
12763 if (unqualified_name == error_mark_node)
12765 declarator = cp_error_declarator;
12766 pack_expansion_p = false;
12767 declarator->parameter_pack_p = false;
12771 if (qualifying_scope && at_namespace_scope_p ()
12772 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12774 /* In the declaration of a member of a template class
12775 outside of the class itself, the SCOPE will sometimes
12776 be a TYPENAME_TYPE. For example, given:
12778 template <typename T>
12779 int S<T>::R::i = 3;
12781 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12782 this context, we must resolve S<T>::R to an ordinary
12783 type, rather than a typename type.
12785 The reason we normally avoid resolving TYPENAME_TYPEs
12786 is that a specialization of `S' might render
12787 `S<T>::R' not a type. However, if `S' is
12788 specialized, then this `i' will not be used, so there
12789 is no harm in resolving the types here. */
12792 /* Resolve the TYPENAME_TYPE. */
12793 type = resolve_typename_type (qualifying_scope,
12794 /*only_current_p=*/false);
12795 /* If that failed, the declarator is invalid. */
12796 if (TREE_CODE (type) == TYPENAME_TYPE)
12797 error ("%<%T::%E%> is not a type",
12798 TYPE_CONTEXT (qualifying_scope),
12799 TYPE_IDENTIFIER (qualifying_scope));
12800 qualifying_scope = type;
12805 if (unqualified_name)
12809 if (qualifying_scope
12810 && CLASS_TYPE_P (qualifying_scope))
12811 class_type = qualifying_scope;
12813 class_type = current_class_type;
12815 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12817 tree name_type = TREE_TYPE (unqualified_name);
12818 if (class_type && same_type_p (name_type, class_type))
12820 if (qualifying_scope
12821 && CLASSTYPE_USE_TEMPLATE (name_type))
12823 error ("invalid use of constructor as a template");
12824 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12825 "name the constructor in a qualified name",
12827 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12828 class_type, name_type);
12829 declarator = cp_error_declarator;
12833 unqualified_name = constructor_name (class_type);
12837 /* We do not attempt to print the declarator
12838 here because we do not have enough
12839 information about its original syntactic
12841 cp_parser_error (parser, "invalid declarator");
12842 declarator = cp_error_declarator;
12849 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12850 sfk = sfk_destructor;
12851 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12852 sfk = sfk_conversion;
12853 else if (/* There's no way to declare a constructor
12854 for an anonymous type, even if the type
12855 got a name for linkage purposes. */
12856 !TYPE_WAS_ANONYMOUS (class_type)
12857 && constructor_name_p (unqualified_name,
12860 unqualified_name = constructor_name (class_type);
12861 sfk = sfk_constructor;
12864 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12865 *ctor_dtor_or_conv_p = -1;
12868 declarator = make_id_declarator (qualifying_scope,
12871 declarator->id_loc = token->location;
12872 declarator->parameter_pack_p = pack_expansion_p;
12874 if (pack_expansion_p)
12875 maybe_warn_variadic_templates ();
12877 handle_declarator:;
12878 scope = get_scope_of_declarator (declarator);
12880 /* Any names that appear after the declarator-id for a
12881 member are looked up in the containing scope. */
12882 pushed_scope = push_scope (scope);
12883 parser->in_declarator_p = true;
12884 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12885 || (declarator && declarator->kind == cdk_id))
12886 /* Default args are only allowed on function
12888 parser->default_arg_ok_p = saved_default_arg_ok_p;
12890 parser->default_arg_ok_p = false;
12899 /* For an abstract declarator, we might wind up with nothing at this
12900 point. That's an error; the declarator is not optional. */
12902 cp_parser_error (parser, "expected declarator");
12904 /* If we entered a scope, we must exit it now. */
12906 pop_scope (pushed_scope);
12908 parser->default_arg_ok_p = saved_default_arg_ok_p;
12909 parser->in_declarator_p = saved_in_declarator_p;
12914 /* Parse a ptr-operator.
12917 * cv-qualifier-seq [opt]
12919 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12924 & cv-qualifier-seq [opt]
12926 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12927 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12928 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12929 filled in with the TYPE containing the member. *CV_QUALS is
12930 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12931 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12932 Note that the tree codes returned by this function have nothing
12933 to do with the types of trees that will be eventually be created
12934 to represent the pointer or reference type being parsed. They are
12935 just constants with suggestive names. */
12936 static enum tree_code
12937 cp_parser_ptr_operator (cp_parser* parser,
12939 cp_cv_quals *cv_quals)
12941 enum tree_code code = ERROR_MARK;
12944 /* Assume that it's not a pointer-to-member. */
12946 /* And that there are no cv-qualifiers. */
12947 *cv_quals = TYPE_UNQUALIFIED;
12949 /* Peek at the next token. */
12950 token = cp_lexer_peek_token (parser->lexer);
12952 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12953 if (token->type == CPP_MULT)
12954 code = INDIRECT_REF;
12955 else if (token->type == CPP_AND)
12957 else if ((cxx_dialect != cxx98) &&
12958 token->type == CPP_AND_AND) /* C++0x only */
12959 code = NON_LVALUE_EXPR;
12961 if (code != ERROR_MARK)
12963 /* Consume the `*', `&' or `&&'. */
12964 cp_lexer_consume_token (parser->lexer);
12966 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12967 `&', if we are allowing GNU extensions. (The only qualifier
12968 that can legally appear after `&' is `restrict', but that is
12969 enforced during semantic analysis. */
12970 if (code == INDIRECT_REF
12971 || cp_parser_allow_gnu_extensions_p (parser))
12972 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12976 /* Try the pointer-to-member case. */
12977 cp_parser_parse_tentatively (parser);
12978 /* Look for the optional `::' operator. */
12979 cp_parser_global_scope_opt (parser,
12980 /*current_scope_valid_p=*/false);
12981 /* Look for the nested-name specifier. */
12982 cp_parser_nested_name_specifier (parser,
12983 /*typename_keyword_p=*/false,
12984 /*check_dependency_p=*/true,
12986 /*is_declaration=*/false);
12987 /* If we found it, and the next token is a `*', then we are
12988 indeed looking at a pointer-to-member operator. */
12989 if (!cp_parser_error_occurred (parser)
12990 && cp_parser_require (parser, CPP_MULT, "`*'"))
12992 /* Indicate that the `*' operator was used. */
12993 code = INDIRECT_REF;
12995 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12996 error ("%qD is a namespace", parser->scope);
12999 /* The type of which the member is a member is given by the
13001 *type = parser->scope;
13002 /* The next name will not be qualified. */
13003 parser->scope = NULL_TREE;
13004 parser->qualifying_scope = NULL_TREE;
13005 parser->object_scope = NULL_TREE;
13006 /* Look for the optional cv-qualifier-seq. */
13007 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13010 /* If that didn't work we don't have a ptr-operator. */
13011 if (!cp_parser_parse_definitely (parser))
13012 cp_parser_error (parser, "expected ptr-operator");
13018 /* Parse an (optional) cv-qualifier-seq.
13021 cv-qualifier cv-qualifier-seq [opt]
13032 Returns a bitmask representing the cv-qualifiers. */
13035 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13037 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13042 cp_cv_quals cv_qualifier;
13044 /* Peek at the next token. */
13045 token = cp_lexer_peek_token (parser->lexer);
13046 /* See if it's a cv-qualifier. */
13047 switch (token->keyword)
13050 cv_qualifier = TYPE_QUAL_CONST;
13054 cv_qualifier = TYPE_QUAL_VOLATILE;
13058 cv_qualifier = TYPE_QUAL_RESTRICT;
13062 cv_qualifier = TYPE_UNQUALIFIED;
13069 if (cv_quals & cv_qualifier)
13071 error ("duplicate cv-qualifier");
13072 cp_lexer_purge_token (parser->lexer);
13076 cp_lexer_consume_token (parser->lexer);
13077 cv_quals |= cv_qualifier;
13084 /* Parse a declarator-id.
13088 :: [opt] nested-name-specifier [opt] type-name
13090 In the `id-expression' case, the value returned is as for
13091 cp_parser_id_expression if the id-expression was an unqualified-id.
13092 If the id-expression was a qualified-id, then a SCOPE_REF is
13093 returned. The first operand is the scope (either a NAMESPACE_DECL
13094 or TREE_TYPE), but the second is still just a representation of an
13098 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13101 /* The expression must be an id-expression. Assume that qualified
13102 names are the names of types so that:
13105 int S<T>::R::i = 3;
13107 will work; we must treat `S<T>::R' as the name of a type.
13108 Similarly, assume that qualified names are templates, where
13112 int S<T>::R<T>::i = 3;
13115 id = cp_parser_id_expression (parser,
13116 /*template_keyword_p=*/false,
13117 /*check_dependency_p=*/false,
13118 /*template_p=*/NULL,
13119 /*declarator_p=*/true,
13121 if (id && BASELINK_P (id))
13122 id = BASELINK_FUNCTIONS (id);
13126 /* Parse a type-id.
13129 type-specifier-seq abstract-declarator [opt]
13131 Returns the TYPE specified. */
13134 cp_parser_type_id (cp_parser* parser)
13136 cp_decl_specifier_seq type_specifier_seq;
13137 cp_declarator *abstract_declarator;
13139 /* Parse the type-specifier-seq. */
13140 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13141 &type_specifier_seq);
13142 if (type_specifier_seq.type == error_mark_node)
13143 return error_mark_node;
13145 /* There might or might not be an abstract declarator. */
13146 cp_parser_parse_tentatively (parser);
13147 /* Look for the declarator. */
13148 abstract_declarator
13149 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13150 /*parenthesized_p=*/NULL,
13151 /*member_p=*/false);
13152 /* Check to see if there really was a declarator. */
13153 if (!cp_parser_parse_definitely (parser))
13154 abstract_declarator = NULL;
13156 return groktypename (&type_specifier_seq, abstract_declarator);
13159 /* Parse a type-specifier-seq.
13161 type-specifier-seq:
13162 type-specifier type-specifier-seq [opt]
13166 type-specifier-seq:
13167 attributes type-specifier-seq [opt]
13169 If IS_CONDITION is true, we are at the start of a "condition",
13170 e.g., we've just seen "if (".
13172 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13175 cp_parser_type_specifier_seq (cp_parser* parser,
13177 cp_decl_specifier_seq *type_specifier_seq)
13179 bool seen_type_specifier = false;
13180 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13182 /* Clear the TYPE_SPECIFIER_SEQ. */
13183 clear_decl_specs (type_specifier_seq);
13185 /* Parse the type-specifiers and attributes. */
13188 tree type_specifier;
13189 bool is_cv_qualifier;
13191 /* Check for attributes first. */
13192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13194 type_specifier_seq->attributes =
13195 chainon (type_specifier_seq->attributes,
13196 cp_parser_attributes_opt (parser));
13200 /* Look for the type-specifier. */
13201 type_specifier = cp_parser_type_specifier (parser,
13203 type_specifier_seq,
13204 /*is_declaration=*/false,
13207 if (!type_specifier)
13209 /* If the first type-specifier could not be found, this is not a
13210 type-specifier-seq at all. */
13211 if (!seen_type_specifier)
13213 cp_parser_error (parser, "expected type-specifier");
13214 type_specifier_seq->type = error_mark_node;
13217 /* If subsequent type-specifiers could not be found, the
13218 type-specifier-seq is complete. */
13222 seen_type_specifier = true;
13223 /* The standard says that a condition can be:
13225 type-specifier-seq declarator = assignment-expression
13232 we should treat the "S" as a declarator, not as a
13233 type-specifier. The standard doesn't say that explicitly for
13234 type-specifier-seq, but it does say that for
13235 decl-specifier-seq in an ordinary declaration. Perhaps it
13236 would be clearer just to allow a decl-specifier-seq here, and
13237 then add a semantic restriction that if any decl-specifiers
13238 that are not type-specifiers appear, the program is invalid. */
13239 if (is_condition && !is_cv_qualifier)
13240 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13243 cp_parser_check_decl_spec (type_specifier_seq);
13246 /* Parse a parameter-declaration-clause.
13248 parameter-declaration-clause:
13249 parameter-declaration-list [opt] ... [opt]
13250 parameter-declaration-list , ...
13252 Returns a representation for the parameter declarations. A return
13253 value of NULL indicates a parameter-declaration-clause consisting
13254 only of an ellipsis. */
13256 static cp_parameter_declarator *
13257 cp_parser_parameter_declaration_clause (cp_parser* parser)
13259 cp_parameter_declarator *parameters;
13264 /* Peek at the next token. */
13265 token = cp_lexer_peek_token (parser->lexer);
13266 /* Check for trivial parameter-declaration-clauses. */
13267 if (token->type == CPP_ELLIPSIS)
13269 /* Consume the `...' token. */
13270 cp_lexer_consume_token (parser->lexer);
13273 else if (token->type == CPP_CLOSE_PAREN)
13274 /* There are no parameters. */
13276 #ifndef NO_IMPLICIT_EXTERN_C
13277 if (in_system_header && current_class_type == NULL
13278 && current_lang_name == lang_name_c)
13282 return no_parameters;
13284 /* Check for `(void)', too, which is a special case. */
13285 else if (token->keyword == RID_VOID
13286 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13287 == CPP_CLOSE_PAREN))
13289 /* Consume the `void' token. */
13290 cp_lexer_consume_token (parser->lexer);
13291 /* There are no parameters. */
13292 return no_parameters;
13295 /* Parse the parameter-declaration-list. */
13296 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13297 /* If a parse error occurred while parsing the
13298 parameter-declaration-list, then the entire
13299 parameter-declaration-clause is erroneous. */
13303 /* Peek at the next token. */
13304 token = cp_lexer_peek_token (parser->lexer);
13305 /* If it's a `,', the clause should terminate with an ellipsis. */
13306 if (token->type == CPP_COMMA)
13308 /* Consume the `,'. */
13309 cp_lexer_consume_token (parser->lexer);
13310 /* Expect an ellipsis. */
13312 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13314 /* It might also be `...' if the optional trailing `,' was
13316 else if (token->type == CPP_ELLIPSIS)
13318 /* Consume the `...' token. */
13319 cp_lexer_consume_token (parser->lexer);
13320 /* And remember that we saw it. */
13324 ellipsis_p = false;
13326 /* Finish the parameter list. */
13327 if (parameters && ellipsis_p)
13328 parameters->ellipsis_p = true;
13333 /* Parse a parameter-declaration-list.
13335 parameter-declaration-list:
13336 parameter-declaration
13337 parameter-declaration-list , parameter-declaration
13339 Returns a representation of the parameter-declaration-list, as for
13340 cp_parser_parameter_declaration_clause. However, the
13341 `void_list_node' is never appended to the list. Upon return,
13342 *IS_ERROR will be true iff an error occurred. */
13344 static cp_parameter_declarator *
13345 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13347 cp_parameter_declarator *parameters = NULL;
13348 cp_parameter_declarator **tail = ¶meters;
13349 bool saved_in_unbraced_linkage_specification_p;
13351 /* Assume all will go well. */
13353 /* The special considerations that apply to a function within an
13354 unbraced linkage specifications do not apply to the parameters
13355 to the function. */
13356 saved_in_unbraced_linkage_specification_p
13357 = parser->in_unbraced_linkage_specification_p;
13358 parser->in_unbraced_linkage_specification_p = false;
13360 /* Look for more parameters. */
13363 cp_parameter_declarator *parameter;
13364 bool parenthesized_p;
13365 /* Parse the parameter. */
13367 = cp_parser_parameter_declaration (parser,
13368 /*template_parm_p=*/false,
13371 /* If a parse error occurred parsing the parameter declaration,
13372 then the entire parameter-declaration-list is erroneous. */
13379 /* Add the new parameter to the list. */
13381 tail = ¶meter->next;
13383 /* Peek at the next token. */
13384 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13385 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13386 /* These are for Objective-C++ */
13387 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13388 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13389 /* The parameter-declaration-list is complete. */
13391 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13395 /* Peek at the next token. */
13396 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13397 /* If it's an ellipsis, then the list is complete. */
13398 if (token->type == CPP_ELLIPSIS)
13400 /* Otherwise, there must be more parameters. Consume the
13402 cp_lexer_consume_token (parser->lexer);
13403 /* When parsing something like:
13405 int i(float f, double d)
13407 we can tell after seeing the declaration for "f" that we
13408 are not looking at an initialization of a variable "i",
13409 but rather at the declaration of a function "i".
13411 Due to the fact that the parsing of template arguments
13412 (as specified to a template-id) requires backtracking we
13413 cannot use this technique when inside a template argument
13415 if (!parser->in_template_argument_list_p
13416 && !parser->in_type_id_in_expr_p
13417 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13418 /* However, a parameter-declaration of the form
13419 "foat(f)" (which is a valid declaration of a
13420 parameter "f") can also be interpreted as an
13421 expression (the conversion of "f" to "float"). */
13422 && !parenthesized_p)
13423 cp_parser_commit_to_tentative_parse (parser);
13427 cp_parser_error (parser, "expected %<,%> or %<...%>");
13428 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13429 cp_parser_skip_to_closing_parenthesis (parser,
13430 /*recovering=*/true,
13431 /*or_comma=*/false,
13432 /*consume_paren=*/false);
13437 parser->in_unbraced_linkage_specification_p
13438 = saved_in_unbraced_linkage_specification_p;
13443 /* Parse a parameter declaration.
13445 parameter-declaration:
13446 decl-specifier-seq ... [opt] declarator
13447 decl-specifier-seq declarator = assignment-expression
13448 decl-specifier-seq ... [opt] abstract-declarator [opt]
13449 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13451 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13452 declares a template parameter. (In that case, a non-nested `>'
13453 token encountered during the parsing of the assignment-expression
13454 is not interpreted as a greater-than operator.)
13456 Returns a representation of the parameter, or NULL if an error
13457 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13458 true iff the declarator is of the form "(p)". */
13460 static cp_parameter_declarator *
13461 cp_parser_parameter_declaration (cp_parser *parser,
13462 bool template_parm_p,
13463 bool *parenthesized_p)
13465 int declares_class_or_enum;
13466 bool greater_than_is_operator_p;
13467 cp_decl_specifier_seq decl_specifiers;
13468 cp_declarator *declarator;
13469 tree default_argument;
13471 const char *saved_message;
13473 /* In a template parameter, `>' is not an operator.
13477 When parsing a default template-argument for a non-type
13478 template-parameter, the first non-nested `>' is taken as the end
13479 of the template parameter-list rather than a greater-than
13481 greater_than_is_operator_p = !template_parm_p;
13483 /* Type definitions may not appear in parameter types. */
13484 saved_message = parser->type_definition_forbidden_message;
13485 parser->type_definition_forbidden_message
13486 = "types may not be defined in parameter types";
13488 /* Parse the declaration-specifiers. */
13489 cp_parser_decl_specifier_seq (parser,
13490 CP_PARSER_FLAGS_NONE,
13492 &declares_class_or_enum);
13493 /* If an error occurred, there's no reason to attempt to parse the
13494 rest of the declaration. */
13495 if (cp_parser_error_occurred (parser))
13497 parser->type_definition_forbidden_message = saved_message;
13501 /* Peek at the next token. */
13502 token = cp_lexer_peek_token (parser->lexer);
13504 /* If the next token is a `)', `,', `=', `>', or `...', then there
13505 is no declarator. However, when variadic templates are enabled,
13506 there may be a declarator following `...'. */
13507 if (token->type == CPP_CLOSE_PAREN
13508 || token->type == CPP_COMMA
13509 || token->type == CPP_EQ
13510 || token->type == CPP_GREATER)
13513 if (parenthesized_p)
13514 *parenthesized_p = false;
13516 /* Otherwise, there should be a declarator. */
13519 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13520 parser->default_arg_ok_p = false;
13522 /* After seeing a decl-specifier-seq, if the next token is not a
13523 "(", there is no possibility that the code is a valid
13524 expression. Therefore, if parsing tentatively, we commit at
13526 if (!parser->in_template_argument_list_p
13527 /* In an expression context, having seen:
13531 we cannot be sure whether we are looking at a
13532 function-type (taking a "char" as a parameter) or a cast
13533 of some object of type "char" to "int". */
13534 && !parser->in_type_id_in_expr_p
13535 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13536 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13537 cp_parser_commit_to_tentative_parse (parser);
13538 /* Parse the declarator. */
13539 declarator = cp_parser_declarator (parser,
13540 CP_PARSER_DECLARATOR_EITHER,
13541 /*ctor_dtor_or_conv_p=*/NULL,
13543 /*member_p=*/false);
13544 parser->default_arg_ok_p = saved_default_arg_ok_p;
13545 /* After the declarator, allow more attributes. */
13546 decl_specifiers.attributes
13547 = chainon (decl_specifiers.attributes,
13548 cp_parser_attributes_opt (parser));
13551 /* If the next token is an ellipsis, and we have not seen a
13552 declarator name, and the type of the declarator contains parameter
13553 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13554 a parameter pack expansion expression. Otherwise, leave the
13555 ellipsis for a C-style variadic function. */
13556 token = cp_lexer_peek_token (parser->lexer);
13557 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13559 tree type = decl_specifiers.type;
13561 if (type && DECL_P (type))
13562 type = TREE_TYPE (type);
13565 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13566 && declarator_can_be_parameter_pack (declarator)
13567 && (!declarator || !declarator->parameter_pack_p)
13568 && uses_parameter_packs (type))
13570 /* Consume the `...'. */
13571 cp_lexer_consume_token (parser->lexer);
13572 maybe_warn_variadic_templates ();
13574 /* Build a pack expansion type */
13576 declarator->parameter_pack_p = true;
13578 decl_specifiers.type = make_pack_expansion (type);
13582 /* The restriction on defining new types applies only to the type
13583 of the parameter, not to the default argument. */
13584 parser->type_definition_forbidden_message = saved_message;
13586 /* If the next token is `=', then process a default argument. */
13587 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13589 /* Consume the `='. */
13590 cp_lexer_consume_token (parser->lexer);
13592 /* If we are defining a class, then the tokens that make up the
13593 default argument must be saved and processed later. */
13594 if (!template_parm_p && at_class_scope_p ()
13595 && TYPE_BEING_DEFINED (current_class_type))
13597 unsigned depth = 0;
13598 cp_token *first_token;
13601 /* Add tokens until we have processed the entire default
13602 argument. We add the range [first_token, token). */
13603 first_token = cp_lexer_peek_token (parser->lexer);
13608 /* Peek at the next token. */
13609 token = cp_lexer_peek_token (parser->lexer);
13610 /* What we do depends on what token we have. */
13611 switch (token->type)
13613 /* In valid code, a default argument must be
13614 immediately followed by a `,' `)', or `...'. */
13616 case CPP_CLOSE_PAREN:
13618 /* If we run into a non-nested `;', `}', or `]',
13619 then the code is invalid -- but the default
13620 argument is certainly over. */
13621 case CPP_SEMICOLON:
13622 case CPP_CLOSE_BRACE:
13623 case CPP_CLOSE_SQUARE:
13626 /* Update DEPTH, if necessary. */
13627 else if (token->type == CPP_CLOSE_PAREN
13628 || token->type == CPP_CLOSE_BRACE
13629 || token->type == CPP_CLOSE_SQUARE)
13633 case CPP_OPEN_PAREN:
13634 case CPP_OPEN_SQUARE:
13635 case CPP_OPEN_BRACE:
13640 if (cxx_dialect == cxx98)
13642 /* Fall through for C++0x, which treats the `>>'
13643 operator like two `>' tokens in certain
13647 /* If we see a non-nested `>', and `>' is not an
13648 operator, then it marks the end of the default
13650 if (!depth && !greater_than_is_operator_p)
13654 /* If we run out of tokens, issue an error message. */
13656 case CPP_PRAGMA_EOL:
13657 error ("file ends in default argument");
13663 /* In these cases, we should look for template-ids.
13664 For example, if the default argument is
13665 `X<int, double>()', we need to do name lookup to
13666 figure out whether or not `X' is a template; if
13667 so, the `,' does not end the default argument.
13669 That is not yet done. */
13676 /* If we've reached the end, stop. */
13680 /* Add the token to the token block. */
13681 token = cp_lexer_consume_token (parser->lexer);
13684 /* Create a DEFAULT_ARG to represent the unparsed default
13686 default_argument = make_node (DEFAULT_ARG);
13687 DEFARG_TOKENS (default_argument)
13688 = cp_token_cache_new (first_token, token);
13689 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13691 /* Outside of a class definition, we can just parse the
13692 assignment-expression. */
13695 = cp_parser_default_argument (parser, template_parm_p);
13697 if (!parser->default_arg_ok_p)
13699 if (!flag_pedantic_errors)
13700 warning (0, "deprecated use of default argument for parameter of non-function");
13703 error ("default arguments are only permitted for function parameters");
13704 default_argument = NULL_TREE;
13707 else if ((declarator && declarator->parameter_pack_p)
13708 || (decl_specifiers.type
13709 && PACK_EXPANSION_P (decl_specifiers.type)))
13711 const char* kind = template_parm_p? "template " : "";
13713 /* Find the name of the parameter pack. */
13714 cp_declarator *id_declarator = declarator;
13715 while (id_declarator && id_declarator->kind != cdk_id)
13716 id_declarator = id_declarator->declarator;
13718 if (id_declarator && id_declarator->kind == cdk_id)
13719 error ("%sparameter pack %qD cannot have a default argument",
13720 kind, id_declarator->u.id.unqualified_name);
13722 error ("%sparameter pack cannot have a default argument",
13725 default_argument = NULL_TREE;
13729 default_argument = NULL_TREE;
13731 return make_parameter_declarator (&decl_specifiers,
13736 /* Parse a default argument and return it.
13738 TEMPLATE_PARM_P is true if this is a default argument for a
13739 non-type template parameter. */
13741 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13743 tree default_argument = NULL_TREE;
13744 bool saved_greater_than_is_operator_p;
13745 bool saved_local_variables_forbidden_p;
13747 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13749 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13750 parser->greater_than_is_operator_p = !template_parm_p;
13751 /* Local variable names (and the `this' keyword) may not
13752 appear in a default argument. */
13753 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13754 parser->local_variables_forbidden_p = true;
13755 /* The default argument expression may cause implicitly
13756 defined member functions to be synthesized, which will
13757 result in garbage collection. We must treat this
13758 situation as if we were within the body of function so as
13759 to avoid collecting live data on the stack. */
13761 /* Parse the assignment-expression. */
13762 if (template_parm_p)
13763 push_deferring_access_checks (dk_no_deferred);
13765 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13766 if (template_parm_p)
13767 pop_deferring_access_checks ();
13768 /* Restore saved state. */
13770 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13771 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13773 return default_argument;
13776 /* Parse a function-body.
13779 compound_statement */
13782 cp_parser_function_body (cp_parser *parser)
13784 cp_parser_compound_statement (parser, NULL, false);
13787 /* Parse a ctor-initializer-opt followed by a function-body. Return
13788 true if a ctor-initializer was present. */
13791 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13794 bool ctor_initializer_p;
13796 /* Begin the function body. */
13797 body = begin_function_body ();
13798 /* Parse the optional ctor-initializer. */
13799 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13800 /* Parse the function-body. */
13801 cp_parser_function_body (parser);
13802 /* Finish the function body. */
13803 finish_function_body (body);
13805 return ctor_initializer_p;
13808 /* Parse an initializer.
13811 = initializer-clause
13812 ( expression-list )
13814 Returns an expression representing the initializer. If no
13815 initializer is present, NULL_TREE is returned.
13817 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13818 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13819 set to FALSE if there is no initializer present. If there is an
13820 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13821 is set to true; otherwise it is set to false. */
13824 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13825 bool* non_constant_p)
13830 /* Peek at the next token. */
13831 token = cp_lexer_peek_token (parser->lexer);
13833 /* Let our caller know whether or not this initializer was
13835 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13836 /* Assume that the initializer is constant. */
13837 *non_constant_p = false;
13839 if (token->type == CPP_EQ)
13841 /* Consume the `='. */
13842 cp_lexer_consume_token (parser->lexer);
13843 /* Parse the initializer-clause. */
13844 init = cp_parser_initializer_clause (parser, non_constant_p);
13846 else if (token->type == CPP_OPEN_PAREN)
13847 init = cp_parser_parenthesized_expression_list (parser, false,
13849 /*allow_expansion_p=*/true,
13853 /* Anything else is an error. */
13854 cp_parser_error (parser, "expected initializer");
13855 init = error_mark_node;
13861 /* Parse an initializer-clause.
13863 initializer-clause:
13864 assignment-expression
13865 { initializer-list , [opt] }
13868 Returns an expression representing the initializer.
13870 If the `assignment-expression' production is used the value
13871 returned is simply a representation for the expression.
13873 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13874 the elements of the initializer-list (or NULL, if the last
13875 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13876 NULL_TREE. There is no way to detect whether or not the optional
13877 trailing `,' was provided. NON_CONSTANT_P is as for
13878 cp_parser_initializer. */
13881 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13885 /* Assume the expression is constant. */
13886 *non_constant_p = false;
13888 /* If it is not a `{', then we are looking at an
13889 assignment-expression. */
13890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13893 = cp_parser_constant_expression (parser,
13894 /*allow_non_constant_p=*/true,
13896 if (!*non_constant_p)
13897 initializer = fold_non_dependent_expr (initializer);
13901 /* Consume the `{' token. */
13902 cp_lexer_consume_token (parser->lexer);
13903 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13904 initializer = make_node (CONSTRUCTOR);
13905 /* If it's not a `}', then there is a non-trivial initializer. */
13906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13908 /* Parse the initializer list. */
13909 CONSTRUCTOR_ELTS (initializer)
13910 = cp_parser_initializer_list (parser, non_constant_p);
13911 /* A trailing `,' token is allowed. */
13912 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13913 cp_lexer_consume_token (parser->lexer);
13915 /* Now, there should be a trailing `}'. */
13916 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13919 return initializer;
13922 /* Parse an initializer-list.
13925 initializer-clause ... [opt]
13926 initializer-list , initializer-clause ... [opt]
13931 identifier : initializer-clause
13932 initializer-list, identifier : initializer-clause
13934 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13935 for the initializer. If the INDEX of the elt is non-NULL, it is the
13936 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13937 as for cp_parser_initializer. */
13939 static VEC(constructor_elt,gc) *
13940 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13942 VEC(constructor_elt,gc) *v = NULL;
13944 /* Assume all of the expressions are constant. */
13945 *non_constant_p = false;
13947 /* Parse the rest of the list. */
13953 bool clause_non_constant_p;
13955 /* If the next token is an identifier and the following one is a
13956 colon, we are looking at the GNU designated-initializer
13958 if (cp_parser_allow_gnu_extensions_p (parser)
13959 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13960 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13962 /* Warn the user that they are using an extension. */
13964 pedwarn ("ISO C++ does not allow designated initializers");
13965 /* Consume the identifier. */
13966 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13967 /* Consume the `:'. */
13968 cp_lexer_consume_token (parser->lexer);
13971 identifier = NULL_TREE;
13973 /* Parse the initializer. */
13974 initializer = cp_parser_initializer_clause (parser,
13975 &clause_non_constant_p);
13976 /* If any clause is non-constant, so is the entire initializer. */
13977 if (clause_non_constant_p)
13978 *non_constant_p = true;
13980 /* If we have an ellipsis, this is an initializer pack
13982 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13984 /* Consume the `...'. */
13985 cp_lexer_consume_token (parser->lexer);
13987 /* Turn the initializer into an initializer expansion. */
13988 initializer = make_pack_expansion (initializer);
13991 /* Add it to the vector. */
13992 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13994 /* If the next token is not a comma, we have reached the end of
13996 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13999 /* Peek at the next token. */
14000 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14001 /* If the next token is a `}', then we're still done. An
14002 initializer-clause can have a trailing `,' after the
14003 initializer-list and before the closing `}'. */
14004 if (token->type == CPP_CLOSE_BRACE)
14007 /* Consume the `,' token. */
14008 cp_lexer_consume_token (parser->lexer);
14014 /* Classes [gram.class] */
14016 /* Parse a class-name.
14022 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14023 to indicate that names looked up in dependent types should be
14024 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14025 keyword has been used to indicate that the name that appears next
14026 is a template. TAG_TYPE indicates the explicit tag given before
14027 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14028 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14029 is the class being defined in a class-head.
14031 Returns the TYPE_DECL representing the class. */
14034 cp_parser_class_name (cp_parser *parser,
14035 bool typename_keyword_p,
14036 bool template_keyword_p,
14037 enum tag_types tag_type,
14038 bool check_dependency_p,
14040 bool is_declaration)
14047 /* All class-names start with an identifier. */
14048 token = cp_lexer_peek_token (parser->lexer);
14049 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14051 cp_parser_error (parser, "expected class-name");
14052 return error_mark_node;
14055 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14056 to a template-id, so we save it here. */
14057 scope = parser->scope;
14058 if (scope == error_mark_node)
14059 return error_mark_node;
14061 /* Any name names a type if we're following the `typename' keyword
14062 in a qualified name where the enclosing scope is type-dependent. */
14063 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14064 && dependent_type_p (scope));
14065 /* Handle the common case (an identifier, but not a template-id)
14067 if (token->type == CPP_NAME
14068 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14070 cp_token *identifier_token;
14074 /* Look for the identifier. */
14075 identifier_token = cp_lexer_peek_token (parser->lexer);
14076 ambiguous_p = identifier_token->ambiguous_p;
14077 identifier = cp_parser_identifier (parser);
14078 /* If the next token isn't an identifier, we are certainly not
14079 looking at a class-name. */
14080 if (identifier == error_mark_node)
14081 decl = error_mark_node;
14082 /* If we know this is a type-name, there's no need to look it
14084 else if (typename_p)
14088 tree ambiguous_decls;
14089 /* If we already know that this lookup is ambiguous, then
14090 we've already issued an error message; there's no reason
14094 cp_parser_simulate_error (parser);
14095 return error_mark_node;
14097 /* If the next token is a `::', then the name must be a type
14100 [basic.lookup.qual]
14102 During the lookup for a name preceding the :: scope
14103 resolution operator, object, function, and enumerator
14104 names are ignored. */
14105 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14106 tag_type = typename_type;
14107 /* Look up the name. */
14108 decl = cp_parser_lookup_name (parser, identifier,
14110 /*is_template=*/false,
14111 /*is_namespace=*/false,
14112 check_dependency_p,
14114 if (ambiguous_decls)
14116 error ("reference to %qD is ambiguous", identifier);
14117 print_candidates (ambiguous_decls);
14118 if (cp_parser_parsing_tentatively (parser))
14120 identifier_token->ambiguous_p = true;
14121 cp_parser_simulate_error (parser);
14123 return error_mark_node;
14129 /* Try a template-id. */
14130 decl = cp_parser_template_id (parser, template_keyword_p,
14131 check_dependency_p,
14133 if (decl == error_mark_node)
14134 return error_mark_node;
14137 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14139 /* If this is a typename, create a TYPENAME_TYPE. */
14140 if (typename_p && decl != error_mark_node)
14142 decl = make_typename_type (scope, decl, typename_type,
14143 /*complain=*/tf_error);
14144 if (decl != error_mark_node)
14145 decl = TYPE_NAME (decl);
14148 /* Check to see that it is really the name of a class. */
14149 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14150 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14151 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14152 /* Situations like this:
14154 template <typename T> struct A {
14155 typename T::template X<int>::I i;
14158 are problematic. Is `T::template X<int>' a class-name? The
14159 standard does not seem to be definitive, but there is no other
14160 valid interpretation of the following `::'. Therefore, those
14161 names are considered class-names. */
14163 decl = make_typename_type (scope, decl, tag_type, tf_error);
14164 if (decl != error_mark_node)
14165 decl = TYPE_NAME (decl);
14167 else if (TREE_CODE (decl) != TYPE_DECL
14168 || TREE_TYPE (decl) == error_mark_node
14169 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14170 decl = error_mark_node;
14172 if (decl == error_mark_node)
14173 cp_parser_error (parser, "expected class-name");
14178 /* Parse a class-specifier.
14181 class-head { member-specification [opt] }
14183 Returns the TREE_TYPE representing the class. */
14186 cp_parser_class_specifier (cp_parser* parser)
14190 tree attributes = NULL_TREE;
14191 int has_trailing_semicolon;
14192 bool nested_name_specifier_p;
14193 unsigned saved_num_template_parameter_lists;
14194 bool saved_in_function_body;
14195 tree old_scope = NULL_TREE;
14196 tree scope = NULL_TREE;
14199 push_deferring_access_checks (dk_no_deferred);
14201 /* Parse the class-head. */
14202 type = cp_parser_class_head (parser,
14203 &nested_name_specifier_p,
14206 /* If the class-head was a semantic disaster, skip the entire body
14210 cp_parser_skip_to_end_of_block_or_statement (parser);
14211 pop_deferring_access_checks ();
14212 return error_mark_node;
14215 /* Look for the `{'. */
14216 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14218 pop_deferring_access_checks ();
14219 return error_mark_node;
14222 /* Process the base classes. If they're invalid, skip the
14223 entire class body. */
14224 if (!xref_basetypes (type, bases))
14226 /* Consuming the closing brace yields better error messages
14228 if (cp_parser_skip_to_closing_brace (parser))
14229 cp_lexer_consume_token (parser->lexer);
14230 pop_deferring_access_checks ();
14231 return error_mark_node;
14234 /* Issue an error message if type-definitions are forbidden here. */
14235 cp_parser_check_type_definition (parser);
14236 /* Remember that we are defining one more class. */
14237 ++parser->num_classes_being_defined;
14238 /* Inside the class, surrounding template-parameter-lists do not
14240 saved_num_template_parameter_lists
14241 = parser->num_template_parameter_lists;
14242 parser->num_template_parameter_lists = 0;
14243 /* We are not in a function body. */
14244 saved_in_function_body = parser->in_function_body;
14245 parser->in_function_body = false;
14247 /* Start the class. */
14248 if (nested_name_specifier_p)
14250 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14251 old_scope = push_inner_scope (scope);
14253 type = begin_class_definition (type, attributes);
14255 if (type == error_mark_node)
14256 /* If the type is erroneous, skip the entire body of the class. */
14257 cp_parser_skip_to_closing_brace (parser);
14259 /* Parse the member-specification. */
14260 cp_parser_member_specification_opt (parser);
14262 /* Look for the trailing `}'. */
14263 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14264 /* We get better error messages by noticing a common problem: a
14265 missing trailing `;'. */
14266 token = cp_lexer_peek_token (parser->lexer);
14267 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14268 /* Look for trailing attributes to apply to this class. */
14269 if (cp_parser_allow_gnu_extensions_p (parser))
14270 attributes = cp_parser_attributes_opt (parser);
14271 if (type != error_mark_node)
14272 type = finish_struct (type, attributes);
14273 if (nested_name_specifier_p)
14274 pop_inner_scope (old_scope, scope);
14275 /* If this class is not itself within the scope of another class,
14276 then we need to parse the bodies of all of the queued function
14277 definitions. Note that the queued functions defined in a class
14278 are not always processed immediately following the
14279 class-specifier for that class. Consider:
14282 struct B { void f() { sizeof (A); } };
14285 If `f' were processed before the processing of `A' were
14286 completed, there would be no way to compute the size of `A'.
14287 Note that the nesting we are interested in here is lexical --
14288 not the semantic nesting given by TYPE_CONTEXT. In particular,
14291 struct A { struct B; };
14292 struct A::B { void f() { } };
14294 there is no need to delay the parsing of `A::B::f'. */
14295 if (--parser->num_classes_being_defined == 0)
14299 tree class_type = NULL_TREE;
14300 tree pushed_scope = NULL_TREE;
14302 /* In a first pass, parse default arguments to the functions.
14303 Then, in a second pass, parse the bodies of the functions.
14304 This two-phased approach handles cases like:
14312 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14313 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14314 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14315 TREE_PURPOSE (parser->unparsed_functions_queues)
14316 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14318 fn = TREE_VALUE (queue_entry);
14319 /* If there are default arguments that have not yet been processed,
14320 take care of them now. */
14321 if (class_type != TREE_PURPOSE (queue_entry))
14324 pop_scope (pushed_scope);
14325 class_type = TREE_PURPOSE (queue_entry);
14326 pushed_scope = push_scope (class_type);
14328 /* Make sure that any template parameters are in scope. */
14329 maybe_begin_member_template_processing (fn);
14330 /* Parse the default argument expressions. */
14331 cp_parser_late_parsing_default_args (parser, fn);
14332 /* Remove any template parameters from the symbol table. */
14333 maybe_end_member_template_processing ();
14336 pop_scope (pushed_scope);
14337 /* Now parse the body of the functions. */
14338 for (TREE_VALUE (parser->unparsed_functions_queues)
14339 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14340 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14341 TREE_VALUE (parser->unparsed_functions_queues)
14342 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14344 /* Figure out which function we need to process. */
14345 fn = TREE_VALUE (queue_entry);
14346 /* Parse the function. */
14347 cp_parser_late_parsing_for_member (parser, fn);
14351 /* Put back any saved access checks. */
14352 pop_deferring_access_checks ();
14354 /* Restore saved state. */
14355 parser->in_function_body = saved_in_function_body;
14356 parser->num_template_parameter_lists
14357 = saved_num_template_parameter_lists;
14362 /* Parse a class-head.
14365 class-key identifier [opt] base-clause [opt]
14366 class-key nested-name-specifier identifier base-clause [opt]
14367 class-key nested-name-specifier [opt] template-id
14371 class-key attributes identifier [opt] base-clause [opt]
14372 class-key attributes nested-name-specifier identifier base-clause [opt]
14373 class-key attributes nested-name-specifier [opt] template-id
14376 Upon return BASES is initialized to the list of base classes (or
14377 NULL, if there are none) in the same form returned by
14378 cp_parser_base_clause.
14380 Returns the TYPE of the indicated class. Sets
14381 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14382 involving a nested-name-specifier was used, and FALSE otherwise.
14384 Returns error_mark_node if this is not a class-head.
14386 Returns NULL_TREE if the class-head is syntactically valid, but
14387 semantically invalid in a way that means we should skip the entire
14388 body of the class. */
14391 cp_parser_class_head (cp_parser* parser,
14392 bool* nested_name_specifier_p,
14393 tree *attributes_p,
14396 tree nested_name_specifier;
14397 enum tag_types class_key;
14398 tree id = NULL_TREE;
14399 tree type = NULL_TREE;
14401 bool template_id_p = false;
14402 bool qualified_p = false;
14403 bool invalid_nested_name_p = false;
14404 bool invalid_explicit_specialization_p = false;
14405 tree pushed_scope = NULL_TREE;
14406 unsigned num_templates;
14408 /* Assume no nested-name-specifier will be present. */
14409 *nested_name_specifier_p = false;
14410 /* Assume no template parameter lists will be used in defining the
14414 *bases = NULL_TREE;
14416 /* Look for the class-key. */
14417 class_key = cp_parser_class_key (parser);
14418 if (class_key == none_type)
14419 return error_mark_node;
14421 /* Parse the attributes. */
14422 attributes = cp_parser_attributes_opt (parser);
14424 /* If the next token is `::', that is invalid -- but sometimes
14425 people do try to write:
14429 Handle this gracefully by accepting the extra qualifier, and then
14430 issuing an error about it later if this really is a
14431 class-head. If it turns out just to be an elaborated type
14432 specifier, remain silent. */
14433 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14434 qualified_p = true;
14436 push_deferring_access_checks (dk_no_check);
14438 /* Determine the name of the class. Begin by looking for an
14439 optional nested-name-specifier. */
14440 nested_name_specifier
14441 = cp_parser_nested_name_specifier_opt (parser,
14442 /*typename_keyword_p=*/false,
14443 /*check_dependency_p=*/false,
14445 /*is_declaration=*/false);
14446 /* If there was a nested-name-specifier, then there *must* be an
14448 if (nested_name_specifier)
14450 /* Although the grammar says `identifier', it really means
14451 `class-name' or `template-name'. You are only allowed to
14452 define a class that has already been declared with this
14455 The proposed resolution for Core Issue 180 says that wherever
14456 you see `class T::X' you should treat `X' as a type-name.
14458 It is OK to define an inaccessible class; for example:
14460 class A { class B; };
14463 We do not know if we will see a class-name, or a
14464 template-name. We look for a class-name first, in case the
14465 class-name is a template-id; if we looked for the
14466 template-name first we would stop after the template-name. */
14467 cp_parser_parse_tentatively (parser);
14468 type = cp_parser_class_name (parser,
14469 /*typename_keyword_p=*/false,
14470 /*template_keyword_p=*/false,
14472 /*check_dependency_p=*/false,
14473 /*class_head_p=*/true,
14474 /*is_declaration=*/false);
14475 /* If that didn't work, ignore the nested-name-specifier. */
14476 if (!cp_parser_parse_definitely (parser))
14478 invalid_nested_name_p = true;
14479 id = cp_parser_identifier (parser);
14480 if (id == error_mark_node)
14483 /* If we could not find a corresponding TYPE, treat this
14484 declaration like an unqualified declaration. */
14485 if (type == error_mark_node)
14486 nested_name_specifier = NULL_TREE;
14487 /* Otherwise, count the number of templates used in TYPE and its
14488 containing scopes. */
14493 for (scope = TREE_TYPE (type);
14494 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14495 scope = (TYPE_P (scope)
14496 ? TYPE_CONTEXT (scope)
14497 : DECL_CONTEXT (scope)))
14499 && CLASS_TYPE_P (scope)
14500 && CLASSTYPE_TEMPLATE_INFO (scope)
14501 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14502 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14506 /* Otherwise, the identifier is optional. */
14509 /* We don't know whether what comes next is a template-id,
14510 an identifier, or nothing at all. */
14511 cp_parser_parse_tentatively (parser);
14512 /* Check for a template-id. */
14513 id = cp_parser_template_id (parser,
14514 /*template_keyword_p=*/false,
14515 /*check_dependency_p=*/true,
14516 /*is_declaration=*/true);
14517 /* If that didn't work, it could still be an identifier. */
14518 if (!cp_parser_parse_definitely (parser))
14520 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14521 id = cp_parser_identifier (parser);
14527 template_id_p = true;
14532 pop_deferring_access_checks ();
14535 cp_parser_check_for_invalid_template_id (parser, id);
14537 /* If it's not a `:' or a `{' then we can't really be looking at a
14538 class-head, since a class-head only appears as part of a
14539 class-specifier. We have to detect this situation before calling
14540 xref_tag, since that has irreversible side-effects. */
14541 if (!cp_parser_next_token_starts_class_definition_p (parser))
14543 cp_parser_error (parser, "expected %<{%> or %<:%>");
14544 return error_mark_node;
14547 /* At this point, we're going ahead with the class-specifier, even
14548 if some other problem occurs. */
14549 cp_parser_commit_to_tentative_parse (parser);
14550 /* Issue the error about the overly-qualified name now. */
14552 cp_parser_error (parser,
14553 "global qualification of class name is invalid");
14554 else if (invalid_nested_name_p)
14555 cp_parser_error (parser,
14556 "qualified name does not name a class");
14557 else if (nested_name_specifier)
14561 /* Reject typedef-names in class heads. */
14562 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14564 error ("invalid class name in declaration of %qD", type);
14569 /* Figure out in what scope the declaration is being placed. */
14570 scope = current_scope ();
14571 /* If that scope does not contain the scope in which the
14572 class was originally declared, the program is invalid. */
14573 if (scope && !is_ancestor (scope, nested_name_specifier))
14575 if (at_namespace_scope_p ())
14576 error ("declaration of %qD in namespace %qD which does not "
14577 "enclose %qD", type, scope, nested_name_specifier);
14579 error ("declaration of %qD in %qD which does not enclose %qD",
14580 type, scope, nested_name_specifier);
14586 A declarator-id shall not be qualified exception of the
14587 definition of a ... nested class outside of its class
14588 ... [or] a the definition or explicit instantiation of a
14589 class member of a namespace outside of its namespace. */
14590 if (scope == nested_name_specifier)
14592 pedwarn ("extra qualification ignored");
14593 nested_name_specifier = NULL_TREE;
14597 /* An explicit-specialization must be preceded by "template <>". If
14598 it is not, try to recover gracefully. */
14599 if (at_namespace_scope_p ()
14600 && parser->num_template_parameter_lists == 0
14603 error ("an explicit specialization must be preceded by %<template <>%>");
14604 invalid_explicit_specialization_p = true;
14605 /* Take the same action that would have been taken by
14606 cp_parser_explicit_specialization. */
14607 ++parser->num_template_parameter_lists;
14608 begin_specialization ();
14610 /* There must be no "return" statements between this point and the
14611 end of this function; set "type "to the correct return value and
14612 use "goto done;" to return. */
14613 /* Make sure that the right number of template parameters were
14615 if (!cp_parser_check_template_parameters (parser, num_templates))
14617 /* If something went wrong, there is no point in even trying to
14618 process the class-definition. */
14623 /* Look up the type. */
14626 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14627 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14628 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14630 error ("function template %qD redeclared as a class template", id);
14631 type = error_mark_node;
14635 type = TREE_TYPE (id);
14636 type = maybe_process_partial_specialization (type);
14638 if (nested_name_specifier)
14639 pushed_scope = push_scope (nested_name_specifier);
14641 else if (nested_name_specifier)
14647 template <typename T> struct S { struct T };
14648 template <typename T> struct S<T>::T { };
14650 we will get a TYPENAME_TYPE when processing the definition of
14651 `S::T'. We need to resolve it to the actual type before we
14652 try to define it. */
14653 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14655 class_type = resolve_typename_type (TREE_TYPE (type),
14656 /*only_current_p=*/false);
14657 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14658 type = TYPE_NAME (class_type);
14661 cp_parser_error (parser, "could not resolve typename type");
14662 type = error_mark_node;
14666 maybe_process_partial_specialization (TREE_TYPE (type));
14667 class_type = current_class_type;
14668 /* Enter the scope indicated by the nested-name-specifier. */
14669 pushed_scope = push_scope (nested_name_specifier);
14670 /* Get the canonical version of this type. */
14671 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14672 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14673 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14675 type = push_template_decl (type);
14676 if (type == error_mark_node)
14683 type = TREE_TYPE (type);
14684 *nested_name_specifier_p = true;
14686 else /* The name is not a nested name. */
14688 /* If the class was unnamed, create a dummy name. */
14690 id = make_anon_name ();
14691 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14692 parser->num_template_parameter_lists);
14695 /* Indicate whether this class was declared as a `class' or as a
14697 if (TREE_CODE (type) == RECORD_TYPE)
14698 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14699 cp_parser_check_class_key (class_key, type);
14701 /* If this type was already complete, and we see another definition,
14702 that's an error. */
14703 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14705 error ("redefinition of %q#T", type);
14706 error ("previous definition of %q+#T", type);
14710 else if (type == error_mark_node)
14713 /* We will have entered the scope containing the class; the names of
14714 base classes should be looked up in that context. For example:
14716 struct A { struct B {}; struct C; };
14717 struct A::C : B {};
14721 /* Get the list of base-classes, if there is one. */
14722 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14723 *bases = cp_parser_base_clause (parser);
14726 /* Leave the scope given by the nested-name-specifier. We will
14727 enter the class scope itself while processing the members. */
14729 pop_scope (pushed_scope);
14731 if (invalid_explicit_specialization_p)
14733 end_specialization ();
14734 --parser->num_template_parameter_lists;
14736 *attributes_p = attributes;
14740 /* Parse a class-key.
14747 Returns the kind of class-key specified, or none_type to indicate
14750 static enum tag_types
14751 cp_parser_class_key (cp_parser* parser)
14754 enum tag_types tag_type;
14756 /* Look for the class-key. */
14757 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14761 /* Check to see if the TOKEN is a class-key. */
14762 tag_type = cp_parser_token_is_class_key (token);
14764 cp_parser_error (parser, "expected class-key");
14768 /* Parse an (optional) member-specification.
14770 member-specification:
14771 member-declaration member-specification [opt]
14772 access-specifier : member-specification [opt] */
14775 cp_parser_member_specification_opt (cp_parser* parser)
14782 /* Peek at the next token. */
14783 token = cp_lexer_peek_token (parser->lexer);
14784 /* If it's a `}', or EOF then we've seen all the members. */
14785 if (token->type == CPP_CLOSE_BRACE
14786 || token->type == CPP_EOF
14787 || token->type == CPP_PRAGMA_EOL)
14790 /* See if this token is a keyword. */
14791 keyword = token->keyword;
14795 case RID_PROTECTED:
14797 /* Consume the access-specifier. */
14798 cp_lexer_consume_token (parser->lexer);
14799 /* Remember which access-specifier is active. */
14800 current_access_specifier = token->u.value;
14801 /* Look for the `:'. */
14802 cp_parser_require (parser, CPP_COLON, "`:'");
14806 /* Accept #pragmas at class scope. */
14807 if (token->type == CPP_PRAGMA)
14809 cp_parser_pragma (parser, pragma_external);
14813 /* Otherwise, the next construction must be a
14814 member-declaration. */
14815 cp_parser_member_declaration (parser);
14820 /* Parse a member-declaration.
14822 member-declaration:
14823 decl-specifier-seq [opt] member-declarator-list [opt] ;
14824 function-definition ; [opt]
14825 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14827 template-declaration
14829 member-declarator-list:
14831 member-declarator-list , member-declarator
14834 declarator pure-specifier [opt]
14835 declarator constant-initializer [opt]
14836 identifier [opt] : constant-expression
14840 member-declaration:
14841 __extension__ member-declaration
14844 declarator attributes [opt] pure-specifier [opt]
14845 declarator attributes [opt] constant-initializer [opt]
14846 identifier [opt] attributes [opt] : constant-expression
14850 member-declaration:
14851 static_assert-declaration */
14854 cp_parser_member_declaration (cp_parser* parser)
14856 cp_decl_specifier_seq decl_specifiers;
14857 tree prefix_attributes;
14859 int declares_class_or_enum;
14862 int saved_pedantic;
14864 /* Check for the `__extension__' keyword. */
14865 if (cp_parser_extension_opt (parser, &saved_pedantic))
14868 cp_parser_member_declaration (parser);
14869 /* Restore the old value of the PEDANTIC flag. */
14870 pedantic = saved_pedantic;
14875 /* Check for a template-declaration. */
14876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14878 /* An explicit specialization here is an error condition, and we
14879 expect the specialization handler to detect and report this. */
14880 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14881 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14882 cp_parser_explicit_specialization (parser);
14884 cp_parser_template_declaration (parser, /*member_p=*/true);
14889 /* Check for a using-declaration. */
14890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14892 /* Parse the using-declaration. */
14893 cp_parser_using_declaration (parser,
14894 /*access_declaration_p=*/false);
14898 /* Check for @defs. */
14899 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14902 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14903 ivar = ivar_chains;
14907 ivar = TREE_CHAIN (member);
14908 TREE_CHAIN (member) = NULL_TREE;
14909 finish_member_declaration (member);
14914 /* If the next token is `static_assert' we have a static assertion. */
14915 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14917 cp_parser_static_assert (parser, /*member_p=*/true);
14921 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14924 /* Parse the decl-specifier-seq. */
14925 cp_parser_decl_specifier_seq (parser,
14926 CP_PARSER_FLAGS_OPTIONAL,
14928 &declares_class_or_enum);
14929 prefix_attributes = decl_specifiers.attributes;
14930 decl_specifiers.attributes = NULL_TREE;
14931 /* Check for an invalid type-name. */
14932 if (!decl_specifiers.type
14933 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14935 /* If there is no declarator, then the decl-specifier-seq should
14937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14939 /* If there was no decl-specifier-seq, and the next token is a
14940 `;', then we have something like:
14946 Each member-declaration shall declare at least one member
14947 name of the class. */
14948 if (!decl_specifiers.any_specifiers_p)
14950 cp_token *token = cp_lexer_peek_token (parser->lexer);
14951 if (pedantic && !token->in_system_header)
14952 pedwarn ("%Hextra %<;%>", &token->location);
14958 /* See if this declaration is a friend. */
14959 friend_p = cp_parser_friend_p (&decl_specifiers);
14960 /* If there were decl-specifiers, check to see if there was
14961 a class-declaration. */
14962 type = check_tag_decl (&decl_specifiers);
14963 /* Nested classes have already been added to the class, but
14964 a `friend' needs to be explicitly registered. */
14967 /* If the `friend' keyword was present, the friend must
14968 be introduced with a class-key. */
14969 if (!declares_class_or_enum)
14970 error ("a class-key must be used when declaring a friend");
14973 template <typename T> struct A {
14974 friend struct A<T>::B;
14977 A<T>::B will be represented by a TYPENAME_TYPE, and
14978 therefore not recognized by check_tag_decl. */
14980 && decl_specifiers.type
14981 && TYPE_P (decl_specifiers.type))
14982 type = decl_specifiers.type;
14983 if (!type || !TYPE_P (type))
14984 error ("friend declaration does not name a class or "
14987 make_friend_class (current_class_type, type,
14988 /*complain=*/true);
14990 /* If there is no TYPE, an error message will already have
14992 else if (!type || type == error_mark_node)
14994 /* An anonymous aggregate has to be handled specially; such
14995 a declaration really declares a data member (with a
14996 particular type), as opposed to a nested class. */
14997 else if (ANON_AGGR_TYPE_P (type))
14999 /* Remove constructors and such from TYPE, now that we
15000 know it is an anonymous aggregate. */
15001 fixup_anonymous_aggr (type);
15002 /* And make the corresponding data member. */
15003 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15004 /* Add it to the class. */
15005 finish_member_declaration (decl);
15008 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15013 /* See if these declarations will be friends. */
15014 friend_p = cp_parser_friend_p (&decl_specifiers);
15016 /* Keep going until we hit the `;' at the end of the
15018 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15020 tree attributes = NULL_TREE;
15021 tree first_attribute;
15023 /* Peek at the next token. */
15024 token = cp_lexer_peek_token (parser->lexer);
15026 /* Check for a bitfield declaration. */
15027 if (token->type == CPP_COLON
15028 || (token->type == CPP_NAME
15029 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15035 /* Get the name of the bitfield. Note that we cannot just
15036 check TOKEN here because it may have been invalidated by
15037 the call to cp_lexer_peek_nth_token above. */
15038 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15039 identifier = cp_parser_identifier (parser);
15041 identifier = NULL_TREE;
15043 /* Consume the `:' token. */
15044 cp_lexer_consume_token (parser->lexer);
15045 /* Get the width of the bitfield. */
15047 = cp_parser_constant_expression (parser,
15048 /*allow_non_constant=*/false,
15051 /* Look for attributes that apply to the bitfield. */
15052 attributes = cp_parser_attributes_opt (parser);
15053 /* Remember which attributes are prefix attributes and
15055 first_attribute = attributes;
15056 /* Combine the attributes. */
15057 attributes = chainon (prefix_attributes, attributes);
15059 /* Create the bitfield declaration. */
15060 decl = grokbitfield (identifier
15061 ? make_id_declarator (NULL_TREE,
15067 /* Apply the attributes. */
15068 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15072 cp_declarator *declarator;
15074 tree asm_specification;
15075 int ctor_dtor_or_conv_p;
15077 /* Parse the declarator. */
15079 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15080 &ctor_dtor_or_conv_p,
15081 /*parenthesized_p=*/NULL,
15082 /*member_p=*/true);
15084 /* If something went wrong parsing the declarator, make sure
15085 that we at least consume some tokens. */
15086 if (declarator == cp_error_declarator)
15088 /* Skip to the end of the statement. */
15089 cp_parser_skip_to_end_of_statement (parser);
15090 /* If the next token is not a semicolon, that is
15091 probably because we just skipped over the body of
15092 a function. So, we consume a semicolon if
15093 present, but do not issue an error message if it
15095 if (cp_lexer_next_token_is (parser->lexer,
15097 cp_lexer_consume_token (parser->lexer);
15101 if (declares_class_or_enum & 2)
15102 cp_parser_check_for_definition_in_return_type
15103 (declarator, decl_specifiers.type);
15105 /* Look for an asm-specification. */
15106 asm_specification = cp_parser_asm_specification_opt (parser);
15107 /* Look for attributes that apply to the declaration. */
15108 attributes = cp_parser_attributes_opt (parser);
15109 /* Remember which attributes are prefix attributes and
15111 first_attribute = attributes;
15112 /* Combine the attributes. */
15113 attributes = chainon (prefix_attributes, attributes);
15115 /* If it's an `=', then we have a constant-initializer or a
15116 pure-specifier. It is not correct to parse the
15117 initializer before registering the member declaration
15118 since the member declaration should be in scope while
15119 its initializer is processed. However, the rest of the
15120 front end does not yet provide an interface that allows
15121 us to handle this correctly. */
15122 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15126 A pure-specifier shall be used only in the declaration of
15127 a virtual function.
15129 A member-declarator can contain a constant-initializer
15130 only if it declares a static member of integral or
15133 Therefore, if the DECLARATOR is for a function, we look
15134 for a pure-specifier; otherwise, we look for a
15135 constant-initializer. When we call `grokfield', it will
15136 perform more stringent semantics checks. */
15137 if (function_declarator_p (declarator))
15138 initializer = cp_parser_pure_specifier (parser);
15140 /* Parse the initializer. */
15141 initializer = cp_parser_constant_initializer (parser);
15143 /* Otherwise, there is no initializer. */
15145 initializer = NULL_TREE;
15147 /* See if we are probably looking at a function
15148 definition. We are certainly not looking at a
15149 member-declarator. Calling `grokfield' has
15150 side-effects, so we must not do it unless we are sure
15151 that we are looking at a member-declarator. */
15152 if (cp_parser_token_starts_function_definition_p
15153 (cp_lexer_peek_token (parser->lexer)))
15155 /* The grammar does not allow a pure-specifier to be
15156 used when a member function is defined. (It is
15157 possible that this fact is an oversight in the
15158 standard, since a pure function may be defined
15159 outside of the class-specifier. */
15161 error ("pure-specifier on function-definition");
15162 decl = cp_parser_save_member_function_body (parser,
15166 /* If the member was not a friend, declare it here. */
15168 finish_member_declaration (decl);
15169 /* Peek at the next token. */
15170 token = cp_lexer_peek_token (parser->lexer);
15171 /* If the next token is a semicolon, consume it. */
15172 if (token->type == CPP_SEMICOLON)
15173 cp_lexer_consume_token (parser->lexer);
15177 /* Create the declaration. */
15178 decl = grokfield (declarator, &decl_specifiers,
15179 initializer, /*init_const_expr_p=*/true,
15184 /* Reset PREFIX_ATTRIBUTES. */
15185 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15186 attributes = TREE_CHAIN (attributes);
15188 TREE_CHAIN (attributes) = NULL_TREE;
15190 /* If there is any qualification still in effect, clear it
15191 now; we will be starting fresh with the next declarator. */
15192 parser->scope = NULL_TREE;
15193 parser->qualifying_scope = NULL_TREE;
15194 parser->object_scope = NULL_TREE;
15195 /* If it's a `,', then there are more declarators. */
15196 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15197 cp_lexer_consume_token (parser->lexer);
15198 /* If the next token isn't a `;', then we have a parse error. */
15199 else if (cp_lexer_next_token_is_not (parser->lexer,
15202 cp_parser_error (parser, "expected %<;%>");
15203 /* Skip tokens until we find a `;'. */
15204 cp_parser_skip_to_end_of_statement (parser);
15211 /* Add DECL to the list of members. */
15213 finish_member_declaration (decl);
15215 if (TREE_CODE (decl) == FUNCTION_DECL)
15216 cp_parser_save_default_args (parser, decl);
15221 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15224 /* Parse a pure-specifier.
15229 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15230 Otherwise, ERROR_MARK_NODE is returned. */
15233 cp_parser_pure_specifier (cp_parser* parser)
15237 /* Look for the `=' token. */
15238 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15239 return error_mark_node;
15240 /* Look for the `0' token. */
15241 token = cp_lexer_consume_token (parser->lexer);
15242 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15243 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15245 cp_parser_error (parser,
15246 "invalid pure specifier (only `= 0' is allowed)");
15247 cp_parser_skip_to_end_of_statement (parser);
15248 return error_mark_node;
15250 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15252 error ("templates may not be %<virtual%>");
15253 return error_mark_node;
15256 return integer_zero_node;
15259 /* Parse a constant-initializer.
15261 constant-initializer:
15262 = constant-expression
15264 Returns a representation of the constant-expression. */
15267 cp_parser_constant_initializer (cp_parser* parser)
15269 /* Look for the `=' token. */
15270 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15271 return error_mark_node;
15273 /* It is invalid to write:
15275 struct S { static const int i = { 7 }; };
15278 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15280 cp_parser_error (parser,
15281 "a brace-enclosed initializer is not allowed here");
15282 /* Consume the opening brace. */
15283 cp_lexer_consume_token (parser->lexer);
15284 /* Skip the initializer. */
15285 cp_parser_skip_to_closing_brace (parser);
15286 /* Look for the trailing `}'. */
15287 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15289 return error_mark_node;
15292 return cp_parser_constant_expression (parser,
15293 /*allow_non_constant=*/false,
15297 /* Derived classes [gram.class.derived] */
15299 /* Parse a base-clause.
15302 : base-specifier-list
15304 base-specifier-list:
15305 base-specifier ... [opt]
15306 base-specifier-list , base-specifier ... [opt]
15308 Returns a TREE_LIST representing the base-classes, in the order in
15309 which they were declared. The representation of each node is as
15310 described by cp_parser_base_specifier.
15312 In the case that no bases are specified, this function will return
15313 NULL_TREE, not ERROR_MARK_NODE. */
15316 cp_parser_base_clause (cp_parser* parser)
15318 tree bases = NULL_TREE;
15320 /* Look for the `:' that begins the list. */
15321 cp_parser_require (parser, CPP_COLON, "`:'");
15323 /* Scan the base-specifier-list. */
15328 bool pack_expansion_p = false;
15330 /* Look for the base-specifier. */
15331 base = cp_parser_base_specifier (parser);
15332 /* Look for the (optional) ellipsis. */
15333 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15335 /* Consume the `...'. */
15336 cp_lexer_consume_token (parser->lexer);
15338 pack_expansion_p = true;
15341 /* Add BASE to the front of the list. */
15342 if (base != error_mark_node)
15344 if (pack_expansion_p)
15345 /* Make this a pack expansion type. */
15346 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15349 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15351 TREE_CHAIN (base) = bases;
15355 /* Peek at the next token. */
15356 token = cp_lexer_peek_token (parser->lexer);
15357 /* If it's not a comma, then the list is complete. */
15358 if (token->type != CPP_COMMA)
15360 /* Consume the `,'. */
15361 cp_lexer_consume_token (parser->lexer);
15364 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15365 base class had a qualified name. However, the next name that
15366 appears is certainly not qualified. */
15367 parser->scope = NULL_TREE;
15368 parser->qualifying_scope = NULL_TREE;
15369 parser->object_scope = NULL_TREE;
15371 return nreverse (bases);
15374 /* Parse a base-specifier.
15377 :: [opt] nested-name-specifier [opt] class-name
15378 virtual access-specifier [opt] :: [opt] nested-name-specifier
15380 access-specifier virtual [opt] :: [opt] nested-name-specifier
15383 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15384 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15385 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15386 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15389 cp_parser_base_specifier (cp_parser* parser)
15393 bool virtual_p = false;
15394 bool duplicate_virtual_error_issued_p = false;
15395 bool duplicate_access_error_issued_p = false;
15396 bool class_scope_p, template_p;
15397 tree access = access_default_node;
15400 /* Process the optional `virtual' and `access-specifier'. */
15403 /* Peek at the next token. */
15404 token = cp_lexer_peek_token (parser->lexer);
15405 /* Process `virtual'. */
15406 switch (token->keyword)
15409 /* If `virtual' appears more than once, issue an error. */
15410 if (virtual_p && !duplicate_virtual_error_issued_p)
15412 cp_parser_error (parser,
15413 "%<virtual%> specified more than once in base-specified");
15414 duplicate_virtual_error_issued_p = true;
15419 /* Consume the `virtual' token. */
15420 cp_lexer_consume_token (parser->lexer);
15425 case RID_PROTECTED:
15427 /* If more than one access specifier appears, issue an
15429 if (access != access_default_node
15430 && !duplicate_access_error_issued_p)
15432 cp_parser_error (parser,
15433 "more than one access specifier in base-specified");
15434 duplicate_access_error_issued_p = true;
15437 access = ridpointers[(int) token->keyword];
15439 /* Consume the access-specifier. */
15440 cp_lexer_consume_token (parser->lexer);
15449 /* It is not uncommon to see programs mechanically, erroneously, use
15450 the 'typename' keyword to denote (dependent) qualified types
15451 as base classes. */
15452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15454 if (!processing_template_decl)
15455 error ("keyword %<typename%> not allowed outside of templates");
15457 error ("keyword %<typename%> not allowed in this context "
15458 "(the base class is implicitly a type)");
15459 cp_lexer_consume_token (parser->lexer);
15462 /* Look for the optional `::' operator. */
15463 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15464 /* Look for the nested-name-specifier. The simplest way to
15469 The keyword `typename' is not permitted in a base-specifier or
15470 mem-initializer; in these contexts a qualified name that
15471 depends on a template-parameter is implicitly assumed to be a
15474 is to pretend that we have seen the `typename' keyword at this
15476 cp_parser_nested_name_specifier_opt (parser,
15477 /*typename_keyword_p=*/true,
15478 /*check_dependency_p=*/true,
15480 /*is_declaration=*/true);
15481 /* If the base class is given by a qualified name, assume that names
15482 we see are type names or templates, as appropriate. */
15483 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15484 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15486 /* Finally, look for the class-name. */
15487 type = cp_parser_class_name (parser,
15491 /*check_dependency_p=*/true,
15492 /*class_head_p=*/false,
15493 /*is_declaration=*/true);
15495 if (type == error_mark_node)
15496 return error_mark_node;
15498 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15501 /* Exception handling [gram.exception] */
15503 /* Parse an (optional) exception-specification.
15505 exception-specification:
15506 throw ( type-id-list [opt] )
15508 Returns a TREE_LIST representing the exception-specification. The
15509 TREE_VALUE of each node is a type. */
15512 cp_parser_exception_specification_opt (cp_parser* parser)
15517 /* Peek at the next token. */
15518 token = cp_lexer_peek_token (parser->lexer);
15519 /* If it's not `throw', then there's no exception-specification. */
15520 if (!cp_parser_is_keyword (token, RID_THROW))
15523 /* Consume the `throw'. */
15524 cp_lexer_consume_token (parser->lexer);
15526 /* Look for the `('. */
15527 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15529 /* Peek at the next token. */
15530 token = cp_lexer_peek_token (parser->lexer);
15531 /* If it's not a `)', then there is a type-id-list. */
15532 if (token->type != CPP_CLOSE_PAREN)
15534 const char *saved_message;
15536 /* Types may not be defined in an exception-specification. */
15537 saved_message = parser->type_definition_forbidden_message;
15538 parser->type_definition_forbidden_message
15539 = "types may not be defined in an exception-specification";
15540 /* Parse the type-id-list. */
15541 type_id_list = cp_parser_type_id_list (parser);
15542 /* Restore the saved message. */
15543 parser->type_definition_forbidden_message = saved_message;
15546 type_id_list = empty_except_spec;
15548 /* Look for the `)'. */
15549 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15551 return type_id_list;
15554 /* Parse an (optional) type-id-list.
15558 type-id-list , type-id ... [opt]
15560 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15561 in the order that the types were presented. */
15564 cp_parser_type_id_list (cp_parser* parser)
15566 tree types = NULL_TREE;
15573 /* Get the next type-id. */
15574 type = cp_parser_type_id (parser);
15575 /* Parse the optional ellipsis. */
15576 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15578 /* Consume the `...'. */
15579 cp_lexer_consume_token (parser->lexer);
15581 /* Turn the type into a pack expansion expression. */
15582 type = make_pack_expansion (type);
15584 /* Add it to the list. */
15585 types = add_exception_specifier (types, type, /*complain=*/1);
15586 /* Peek at the next token. */
15587 token = cp_lexer_peek_token (parser->lexer);
15588 /* If it is not a `,', we are done. */
15589 if (token->type != CPP_COMMA)
15591 /* Consume the `,'. */
15592 cp_lexer_consume_token (parser->lexer);
15595 return nreverse (types);
15598 /* Parse a try-block.
15601 try compound-statement handler-seq */
15604 cp_parser_try_block (cp_parser* parser)
15608 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15609 try_block = begin_try_block ();
15610 cp_parser_compound_statement (parser, NULL, true);
15611 finish_try_block (try_block);
15612 cp_parser_handler_seq (parser);
15613 finish_handler_sequence (try_block);
15618 /* Parse a function-try-block.
15620 function-try-block:
15621 try ctor-initializer [opt] function-body handler-seq */
15624 cp_parser_function_try_block (cp_parser* parser)
15626 tree compound_stmt;
15628 bool ctor_initializer_p;
15630 /* Look for the `try' keyword. */
15631 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15633 /* Let the rest of the front end know where we are. */
15634 try_block = begin_function_try_block (&compound_stmt);
15635 /* Parse the function-body. */
15637 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15638 /* We're done with the `try' part. */
15639 finish_function_try_block (try_block);
15640 /* Parse the handlers. */
15641 cp_parser_handler_seq (parser);
15642 /* We're done with the handlers. */
15643 finish_function_handler_sequence (try_block, compound_stmt);
15645 return ctor_initializer_p;
15648 /* Parse a handler-seq.
15651 handler handler-seq [opt] */
15654 cp_parser_handler_seq (cp_parser* parser)
15660 /* Parse the handler. */
15661 cp_parser_handler (parser);
15662 /* Peek at the next token. */
15663 token = cp_lexer_peek_token (parser->lexer);
15664 /* If it's not `catch' then there are no more handlers. */
15665 if (!cp_parser_is_keyword (token, RID_CATCH))
15670 /* Parse a handler.
15673 catch ( exception-declaration ) compound-statement */
15676 cp_parser_handler (cp_parser* parser)
15681 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15682 handler = begin_handler ();
15683 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15684 declaration = cp_parser_exception_declaration (parser);
15685 finish_handler_parms (declaration, handler);
15686 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15687 cp_parser_compound_statement (parser, NULL, false);
15688 finish_handler (handler);
15691 /* Parse an exception-declaration.
15693 exception-declaration:
15694 type-specifier-seq declarator
15695 type-specifier-seq abstract-declarator
15699 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15700 ellipsis variant is used. */
15703 cp_parser_exception_declaration (cp_parser* parser)
15705 cp_decl_specifier_seq type_specifiers;
15706 cp_declarator *declarator;
15707 const char *saved_message;
15709 /* If it's an ellipsis, it's easy to handle. */
15710 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15712 /* Consume the `...' token. */
15713 cp_lexer_consume_token (parser->lexer);
15717 /* Types may not be defined in exception-declarations. */
15718 saved_message = parser->type_definition_forbidden_message;
15719 parser->type_definition_forbidden_message
15720 = "types may not be defined in exception-declarations";
15722 /* Parse the type-specifier-seq. */
15723 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15725 /* If it's a `)', then there is no declarator. */
15726 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15729 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15730 /*ctor_dtor_or_conv_p=*/NULL,
15731 /*parenthesized_p=*/NULL,
15732 /*member_p=*/false);
15734 /* Restore the saved message. */
15735 parser->type_definition_forbidden_message = saved_message;
15737 if (!type_specifiers.any_specifiers_p)
15738 return error_mark_node;
15740 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15743 /* Parse a throw-expression.
15746 throw assignment-expression [opt]
15748 Returns a THROW_EXPR representing the throw-expression. */
15751 cp_parser_throw_expression (cp_parser* parser)
15756 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15757 token = cp_lexer_peek_token (parser->lexer);
15758 /* Figure out whether or not there is an assignment-expression
15759 following the "throw" keyword. */
15760 if (token->type == CPP_COMMA
15761 || token->type == CPP_SEMICOLON
15762 || token->type == CPP_CLOSE_PAREN
15763 || token->type == CPP_CLOSE_SQUARE
15764 || token->type == CPP_CLOSE_BRACE
15765 || token->type == CPP_COLON)
15766 expression = NULL_TREE;
15768 expression = cp_parser_assignment_expression (parser,
15771 return build_throw (expression);
15774 /* GNU Extensions */
15776 /* Parse an (optional) asm-specification.
15779 asm ( string-literal )
15781 If the asm-specification is present, returns a STRING_CST
15782 corresponding to the string-literal. Otherwise, returns
15786 cp_parser_asm_specification_opt (cp_parser* parser)
15789 tree asm_specification;
15791 /* Peek at the next token. */
15792 token = cp_lexer_peek_token (parser->lexer);
15793 /* If the next token isn't the `asm' keyword, then there's no
15794 asm-specification. */
15795 if (!cp_parser_is_keyword (token, RID_ASM))
15798 /* Consume the `asm' token. */
15799 cp_lexer_consume_token (parser->lexer);
15800 /* Look for the `('. */
15801 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15803 /* Look for the string-literal. */
15804 asm_specification = cp_parser_string_literal (parser, false, false);
15806 /* Look for the `)'. */
15807 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15809 return asm_specification;
15812 /* Parse an asm-operand-list.
15816 asm-operand-list , asm-operand
15819 string-literal ( expression )
15820 [ string-literal ] string-literal ( expression )
15822 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15823 each node is the expression. The TREE_PURPOSE is itself a
15824 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15825 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15826 is a STRING_CST for the string literal before the parenthesis. Returns
15827 ERROR_MARK_NODE if any of the operands are invalid. */
15830 cp_parser_asm_operand_list (cp_parser* parser)
15832 tree asm_operands = NULL_TREE;
15833 bool invalid_operands = false;
15837 tree string_literal;
15841 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15843 /* Consume the `[' token. */
15844 cp_lexer_consume_token (parser->lexer);
15845 /* Read the operand name. */
15846 name = cp_parser_identifier (parser);
15847 if (name != error_mark_node)
15848 name = build_string (IDENTIFIER_LENGTH (name),
15849 IDENTIFIER_POINTER (name));
15850 /* Look for the closing `]'. */
15851 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15855 /* Look for the string-literal. */
15856 string_literal = cp_parser_string_literal (parser, false, false);
15858 /* Look for the `('. */
15859 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15860 /* Parse the expression. */
15861 expression = cp_parser_expression (parser, /*cast_p=*/false);
15862 /* Look for the `)'. */
15863 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15865 if (name == error_mark_node
15866 || string_literal == error_mark_node
15867 || expression == error_mark_node)
15868 invalid_operands = true;
15870 /* Add this operand to the list. */
15871 asm_operands = tree_cons (build_tree_list (name, string_literal),
15874 /* If the next token is not a `,', there are no more
15876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15878 /* Consume the `,'. */
15879 cp_lexer_consume_token (parser->lexer);
15882 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15885 /* Parse an asm-clobber-list.
15889 asm-clobber-list , string-literal
15891 Returns a TREE_LIST, indicating the clobbers in the order that they
15892 appeared. The TREE_VALUE of each node is a STRING_CST. */
15895 cp_parser_asm_clobber_list (cp_parser* parser)
15897 tree clobbers = NULL_TREE;
15901 tree string_literal;
15903 /* Look for the string literal. */
15904 string_literal = cp_parser_string_literal (parser, false, false);
15905 /* Add it to the list. */
15906 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15907 /* If the next token is not a `,', then the list is
15909 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15911 /* Consume the `,' token. */
15912 cp_lexer_consume_token (parser->lexer);
15918 /* Parse an (optional) series of attributes.
15921 attributes attribute
15924 __attribute__ (( attribute-list [opt] ))
15926 The return value is as for cp_parser_attribute_list. */
15929 cp_parser_attributes_opt (cp_parser* parser)
15931 tree attributes = NULL_TREE;
15936 tree attribute_list;
15938 /* Peek at the next token. */
15939 token = cp_lexer_peek_token (parser->lexer);
15940 /* If it's not `__attribute__', then we're done. */
15941 if (token->keyword != RID_ATTRIBUTE)
15944 /* Consume the `__attribute__' keyword. */
15945 cp_lexer_consume_token (parser->lexer);
15946 /* Look for the two `(' tokens. */
15947 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15948 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15950 /* Peek at the next token. */
15951 token = cp_lexer_peek_token (parser->lexer);
15952 if (token->type != CPP_CLOSE_PAREN)
15953 /* Parse the attribute-list. */
15954 attribute_list = cp_parser_attribute_list (parser);
15956 /* If the next token is a `)', then there is no attribute
15958 attribute_list = NULL;
15960 /* Look for the two `)' tokens. */
15961 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15962 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15964 /* Add these new attributes to the list. */
15965 attributes = chainon (attributes, attribute_list);
15971 /* Parse an attribute-list.
15975 attribute-list , attribute
15979 identifier ( identifier )
15980 identifier ( identifier , expression-list )
15981 identifier ( expression-list )
15983 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15984 to an attribute. The TREE_PURPOSE of each node is the identifier
15985 indicating which attribute is in use. The TREE_VALUE represents
15986 the arguments, if any. */
15989 cp_parser_attribute_list (cp_parser* parser)
15991 tree attribute_list = NULL_TREE;
15992 bool save_translate_strings_p = parser->translate_strings_p;
15994 parser->translate_strings_p = false;
16001 /* Look for the identifier. We also allow keywords here; for
16002 example `__attribute__ ((const))' is legal. */
16003 token = cp_lexer_peek_token (parser->lexer);
16004 if (token->type == CPP_NAME
16005 || token->type == CPP_KEYWORD)
16007 tree arguments = NULL_TREE;
16009 /* Consume the token. */
16010 token = cp_lexer_consume_token (parser->lexer);
16012 /* Save away the identifier that indicates which attribute
16014 identifier = token->u.value;
16015 attribute = build_tree_list (identifier, NULL_TREE);
16017 /* Peek at the next token. */
16018 token = cp_lexer_peek_token (parser->lexer);
16019 /* If it's an `(', then parse the attribute arguments. */
16020 if (token->type == CPP_OPEN_PAREN)
16022 arguments = cp_parser_parenthesized_expression_list
16023 (parser, true, /*cast_p=*/false,
16024 /*allow_expansion_p=*/false,
16025 /*non_constant_p=*/NULL);
16026 /* Save the arguments away. */
16027 TREE_VALUE (attribute) = arguments;
16030 if (arguments != error_mark_node)
16032 /* Add this attribute to the list. */
16033 TREE_CHAIN (attribute) = attribute_list;
16034 attribute_list = attribute;
16037 token = cp_lexer_peek_token (parser->lexer);
16039 /* Now, look for more attributes. If the next token isn't a
16040 `,', we're done. */
16041 if (token->type != CPP_COMMA)
16044 /* Consume the comma and keep going. */
16045 cp_lexer_consume_token (parser->lexer);
16047 parser->translate_strings_p = save_translate_strings_p;
16049 /* We built up the list in reverse order. */
16050 return nreverse (attribute_list);
16053 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16054 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16055 current value of the PEDANTIC flag, regardless of whether or not
16056 the `__extension__' keyword is present. The caller is responsible
16057 for restoring the value of the PEDANTIC flag. */
16060 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16062 /* Save the old value of the PEDANTIC flag. */
16063 *saved_pedantic = pedantic;
16065 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16067 /* Consume the `__extension__' token. */
16068 cp_lexer_consume_token (parser->lexer);
16069 /* We're not being pedantic while the `__extension__' keyword is
16079 /* Parse a label declaration.
16082 __label__ label-declarator-seq ;
16084 label-declarator-seq:
16085 identifier , label-declarator-seq
16089 cp_parser_label_declaration (cp_parser* parser)
16091 /* Look for the `__label__' keyword. */
16092 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16098 /* Look for an identifier. */
16099 identifier = cp_parser_identifier (parser);
16100 /* If we failed, stop. */
16101 if (identifier == error_mark_node)
16103 /* Declare it as a label. */
16104 finish_label_decl (identifier);
16105 /* If the next token is a `;', stop. */
16106 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16108 /* Look for the `,' separating the label declarations. */
16109 cp_parser_require (parser, CPP_COMMA, "`,'");
16112 /* Look for the final `;'. */
16113 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16116 /* Support Functions */
16118 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16119 NAME should have one of the representations used for an
16120 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16121 is returned. If PARSER->SCOPE is a dependent type, then a
16122 SCOPE_REF is returned.
16124 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16125 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16126 was formed. Abstractly, such entities should not be passed to this
16127 function, because they do not need to be looked up, but it is
16128 simpler to check for this special case here, rather than at the
16131 In cases not explicitly covered above, this function returns a
16132 DECL, OVERLOAD, or baselink representing the result of the lookup.
16133 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16136 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16137 (e.g., "struct") that was used. In that case bindings that do not
16138 refer to types are ignored.
16140 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16143 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16146 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16149 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16150 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16151 NULL_TREE otherwise. */
16154 cp_parser_lookup_name (cp_parser *parser, tree name,
16155 enum tag_types tag_type,
16158 bool check_dependency,
16159 tree *ambiguous_decls)
16163 tree object_type = parser->context->object_type;
16165 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16166 flags |= LOOKUP_COMPLAIN;
16168 /* Assume that the lookup will be unambiguous. */
16169 if (ambiguous_decls)
16170 *ambiguous_decls = NULL_TREE;
16172 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16173 no longer valid. Note that if we are parsing tentatively, and
16174 the parse fails, OBJECT_TYPE will be automatically restored. */
16175 parser->context->object_type = NULL_TREE;
16177 if (name == error_mark_node)
16178 return error_mark_node;
16180 /* A template-id has already been resolved; there is no lookup to
16182 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16184 if (BASELINK_P (name))
16186 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16187 == TEMPLATE_ID_EXPR);
16191 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16192 it should already have been checked to make sure that the name
16193 used matches the type being destroyed. */
16194 if (TREE_CODE (name) == BIT_NOT_EXPR)
16198 /* Figure out to which type this destructor applies. */
16200 type = parser->scope;
16201 else if (object_type)
16202 type = object_type;
16204 type = current_class_type;
16205 /* If that's not a class type, there is no destructor. */
16206 if (!type || !CLASS_TYPE_P (type))
16207 return error_mark_node;
16208 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16209 lazily_declare_fn (sfk_destructor, type);
16210 if (!CLASSTYPE_DESTRUCTORS (type))
16211 return error_mark_node;
16212 /* If it was a class type, return the destructor. */
16213 return CLASSTYPE_DESTRUCTORS (type);
16216 /* By this point, the NAME should be an ordinary identifier. If
16217 the id-expression was a qualified name, the qualifying scope is
16218 stored in PARSER->SCOPE at this point. */
16219 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16221 /* Perform the lookup. */
16226 if (parser->scope == error_mark_node)
16227 return error_mark_node;
16229 /* If the SCOPE is dependent, the lookup must be deferred until
16230 the template is instantiated -- unless we are explicitly
16231 looking up names in uninstantiated templates. Even then, we
16232 cannot look up the name if the scope is not a class type; it
16233 might, for example, be a template type parameter. */
16234 dependent_p = (TYPE_P (parser->scope)
16235 && !(parser->in_declarator_p
16236 && currently_open_class (parser->scope))
16237 && dependent_type_p (parser->scope));
16238 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16245 /* The resolution to Core Issue 180 says that `struct
16246 A::B' should be considered a type-name, even if `A'
16248 type = make_typename_type (parser->scope, name, tag_type,
16249 /*complain=*/tf_error);
16250 decl = TYPE_NAME (type);
16252 else if (is_template
16253 && (cp_parser_next_token_ends_template_argument_p (parser)
16254 || cp_lexer_next_token_is (parser->lexer,
16256 decl = make_unbound_class_template (parser->scope,
16258 /*complain=*/tf_error);
16260 decl = build_qualified_name (/*type=*/NULL_TREE,
16261 parser->scope, name,
16266 tree pushed_scope = NULL_TREE;
16268 /* If PARSER->SCOPE is a dependent type, then it must be a
16269 class type, and we must not be checking dependencies;
16270 otherwise, we would have processed this lookup above. So
16271 that PARSER->SCOPE is not considered a dependent base by
16272 lookup_member, we must enter the scope here. */
16274 pushed_scope = push_scope (parser->scope);
16275 /* If the PARSER->SCOPE is a template specialization, it
16276 may be instantiated during name lookup. In that case,
16277 errors may be issued. Even if we rollback the current
16278 tentative parse, those errors are valid. */
16279 decl = lookup_qualified_name (parser->scope, name,
16280 tag_type != none_type,
16281 /*complain=*/true);
16283 pop_scope (pushed_scope);
16285 parser->qualifying_scope = parser->scope;
16286 parser->object_scope = NULL_TREE;
16288 else if (object_type)
16290 tree object_decl = NULL_TREE;
16291 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16292 OBJECT_TYPE is not a class. */
16293 if (CLASS_TYPE_P (object_type))
16294 /* If the OBJECT_TYPE is a template specialization, it may
16295 be instantiated during name lookup. In that case, errors
16296 may be issued. Even if we rollback the current tentative
16297 parse, those errors are valid. */
16298 object_decl = lookup_member (object_type,
16301 tag_type != none_type);
16302 /* Look it up in the enclosing context, too. */
16303 decl = lookup_name_real (name, tag_type != none_type,
16305 /*block_p=*/true, is_namespace, flags);
16306 parser->object_scope = object_type;
16307 parser->qualifying_scope = NULL_TREE;
16309 decl = object_decl;
16313 decl = lookup_name_real (name, tag_type != none_type,
16315 /*block_p=*/true, is_namespace, flags);
16316 parser->qualifying_scope = NULL_TREE;
16317 parser->object_scope = NULL_TREE;
16320 /* If the lookup failed, let our caller know. */
16321 if (!decl || decl == error_mark_node)
16322 return error_mark_node;
16324 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16325 if (TREE_CODE (decl) == TREE_LIST)
16327 if (ambiguous_decls)
16328 *ambiguous_decls = decl;
16329 /* The error message we have to print is too complicated for
16330 cp_parser_error, so we incorporate its actions directly. */
16331 if (!cp_parser_simulate_error (parser))
16333 error ("reference to %qD is ambiguous", name);
16334 print_candidates (decl);
16336 return error_mark_node;
16339 gcc_assert (DECL_P (decl)
16340 || TREE_CODE (decl) == OVERLOAD
16341 || TREE_CODE (decl) == SCOPE_REF
16342 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16343 || BASELINK_P (decl));
16345 /* If we have resolved the name of a member declaration, check to
16346 see if the declaration is accessible. When the name resolves to
16347 set of overloaded functions, accessibility is checked when
16348 overload resolution is done.
16350 During an explicit instantiation, access is not checked at all,
16351 as per [temp.explicit]. */
16353 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16358 /* Like cp_parser_lookup_name, but for use in the typical case where
16359 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16360 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16363 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16365 return cp_parser_lookup_name (parser, name,
16367 /*is_template=*/false,
16368 /*is_namespace=*/false,
16369 /*check_dependency=*/true,
16370 /*ambiguous_decls=*/NULL);
16373 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16374 the current context, return the TYPE_DECL. If TAG_NAME_P is
16375 true, the DECL indicates the class being defined in a class-head,
16376 or declared in an elaborated-type-specifier.
16378 Otherwise, return DECL. */
16381 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16383 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16384 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16387 template <typename T> struct B;
16390 template <typename T> struct A::B {};
16392 Similarly, in an elaborated-type-specifier:
16394 namespace N { struct X{}; }
16397 template <typename T> friend struct N::X;
16400 However, if the DECL refers to a class type, and we are in
16401 the scope of the class, then the name lookup automatically
16402 finds the TYPE_DECL created by build_self_reference rather
16403 than a TEMPLATE_DECL. For example, in:
16405 template <class T> struct S {
16409 there is no need to handle such case. */
16411 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16412 return DECL_TEMPLATE_RESULT (decl);
16417 /* If too many, or too few, template-parameter lists apply to the
16418 declarator, issue an error message. Returns TRUE if all went well,
16419 and FALSE otherwise. */
16422 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16423 cp_declarator *declarator)
16425 unsigned num_templates;
16427 /* We haven't seen any classes that involve template parameters yet. */
16430 switch (declarator->kind)
16433 if (declarator->u.id.qualifying_scope)
16438 scope = declarator->u.id.qualifying_scope;
16439 member = declarator->u.id.unqualified_name;
16441 while (scope && CLASS_TYPE_P (scope))
16443 /* You're supposed to have one `template <...>'
16444 for every template class, but you don't need one
16445 for a full specialization. For example:
16447 template <class T> struct S{};
16448 template <> struct S<int> { void f(); };
16449 void S<int>::f () {}
16451 is correct; there shouldn't be a `template <>' for
16452 the definition of `S<int>::f'. */
16453 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16454 /* If SCOPE does not have template information of any
16455 kind, then it is not a template, nor is it nested
16456 within a template. */
16458 if (explicit_class_specialization_p (scope))
16460 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16463 scope = TYPE_CONTEXT (scope);
16466 else if (TREE_CODE (declarator->u.id.unqualified_name)
16467 == TEMPLATE_ID_EXPR)
16468 /* If the DECLARATOR has the form `X<y>' then it uses one
16469 additional level of template parameters. */
16472 return cp_parser_check_template_parameters (parser,
16478 case cdk_reference:
16480 return (cp_parser_check_declarator_template_parameters
16481 (parser, declarator->declarator));
16487 gcc_unreachable ();
16492 /* NUM_TEMPLATES were used in the current declaration. If that is
16493 invalid, return FALSE and issue an error messages. Otherwise,
16497 cp_parser_check_template_parameters (cp_parser* parser,
16498 unsigned num_templates)
16500 /* If there are more template classes than parameter lists, we have
16503 template <class T> void S<T>::R<T>::f (); */
16504 if (parser->num_template_parameter_lists < num_templates)
16506 error ("too few template-parameter-lists");
16509 /* If there are the same number of template classes and parameter
16510 lists, that's OK. */
16511 if (parser->num_template_parameter_lists == num_templates)
16513 /* If there are more, but only one more, then we are referring to a
16514 member template. That's OK too. */
16515 if (parser->num_template_parameter_lists == num_templates + 1)
16517 /* Otherwise, there are too many template parameter lists. We have
16520 template <class T> template <class U> void S::f(); */
16521 error ("too many template-parameter-lists");
16525 /* Parse an optional `::' token indicating that the following name is
16526 from the global namespace. If so, PARSER->SCOPE is set to the
16527 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16528 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16529 Returns the new value of PARSER->SCOPE, if the `::' token is
16530 present, and NULL_TREE otherwise. */
16533 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16537 /* Peek at the next token. */
16538 token = cp_lexer_peek_token (parser->lexer);
16539 /* If we're looking at a `::' token then we're starting from the
16540 global namespace, not our current location. */
16541 if (token->type == CPP_SCOPE)
16543 /* Consume the `::' token. */
16544 cp_lexer_consume_token (parser->lexer);
16545 /* Set the SCOPE so that we know where to start the lookup. */
16546 parser->scope = global_namespace;
16547 parser->qualifying_scope = global_namespace;
16548 parser->object_scope = NULL_TREE;
16550 return parser->scope;
16552 else if (!current_scope_valid_p)
16554 parser->scope = NULL_TREE;
16555 parser->qualifying_scope = NULL_TREE;
16556 parser->object_scope = NULL_TREE;
16562 /* Returns TRUE if the upcoming token sequence is the start of a
16563 constructor declarator. If FRIEND_P is true, the declarator is
16564 preceded by the `friend' specifier. */
16567 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16569 bool constructor_p;
16570 tree type_decl = NULL_TREE;
16571 bool nested_name_p;
16572 cp_token *next_token;
16574 /* The common case is that this is not a constructor declarator, so
16575 try to avoid doing lots of work if at all possible. It's not
16576 valid declare a constructor at function scope. */
16577 if (parser->in_function_body)
16579 /* And only certain tokens can begin a constructor declarator. */
16580 next_token = cp_lexer_peek_token (parser->lexer);
16581 if (next_token->type != CPP_NAME
16582 && next_token->type != CPP_SCOPE
16583 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16584 && next_token->type != CPP_TEMPLATE_ID)
16587 /* Parse tentatively; we are going to roll back all of the tokens
16589 cp_parser_parse_tentatively (parser);
16590 /* Assume that we are looking at a constructor declarator. */
16591 constructor_p = true;
16593 /* Look for the optional `::' operator. */
16594 cp_parser_global_scope_opt (parser,
16595 /*current_scope_valid_p=*/false);
16596 /* Look for the nested-name-specifier. */
16598 = (cp_parser_nested_name_specifier_opt (parser,
16599 /*typename_keyword_p=*/false,
16600 /*check_dependency_p=*/false,
16602 /*is_declaration=*/false)
16604 /* Outside of a class-specifier, there must be a
16605 nested-name-specifier. */
16606 if (!nested_name_p &&
16607 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16609 constructor_p = false;
16610 /* If we still think that this might be a constructor-declarator,
16611 look for a class-name. */
16616 template <typename T> struct S { S(); };
16617 template <typename T> S<T>::S ();
16619 we must recognize that the nested `S' names a class.
16622 template <typename T> S<T>::S<T> ();
16624 we must recognize that the nested `S' names a template. */
16625 type_decl = cp_parser_class_name (parser,
16626 /*typename_keyword_p=*/false,
16627 /*template_keyword_p=*/false,
16629 /*check_dependency_p=*/false,
16630 /*class_head_p=*/false,
16631 /*is_declaration=*/false);
16632 /* If there was no class-name, then this is not a constructor. */
16633 constructor_p = !cp_parser_error_occurred (parser);
16636 /* If we're still considering a constructor, we have to see a `(',
16637 to begin the parameter-declaration-clause, followed by either a
16638 `)', an `...', or a decl-specifier. We need to check for a
16639 type-specifier to avoid being fooled into thinking that:
16643 is a constructor. (It is actually a function named `f' that
16644 takes one parameter (of type `int') and returns a value of type
16647 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16650 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16651 /* A parameter declaration begins with a decl-specifier,
16652 which is either the "attribute" keyword, a storage class
16653 specifier, or (usually) a type-specifier. */
16654 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16657 tree pushed_scope = NULL_TREE;
16658 unsigned saved_num_template_parameter_lists;
16660 /* Names appearing in the type-specifier should be looked up
16661 in the scope of the class. */
16662 if (current_class_type)
16666 type = TREE_TYPE (type_decl);
16667 if (TREE_CODE (type) == TYPENAME_TYPE)
16669 type = resolve_typename_type (type,
16670 /*only_current_p=*/false);
16671 if (TREE_CODE (type) == TYPENAME_TYPE)
16673 cp_parser_abort_tentative_parse (parser);
16677 pushed_scope = push_scope (type);
16680 /* Inside the constructor parameter list, surrounding
16681 template-parameter-lists do not apply. */
16682 saved_num_template_parameter_lists
16683 = parser->num_template_parameter_lists;
16684 parser->num_template_parameter_lists = 0;
16686 /* Look for the type-specifier. */
16687 cp_parser_type_specifier (parser,
16688 CP_PARSER_FLAGS_NONE,
16689 /*decl_specs=*/NULL,
16690 /*is_declarator=*/true,
16691 /*declares_class_or_enum=*/NULL,
16692 /*is_cv_qualifier=*/NULL);
16694 parser->num_template_parameter_lists
16695 = saved_num_template_parameter_lists;
16697 /* Leave the scope of the class. */
16699 pop_scope (pushed_scope);
16701 constructor_p = !cp_parser_error_occurred (parser);
16705 constructor_p = false;
16706 /* We did not really want to consume any tokens. */
16707 cp_parser_abort_tentative_parse (parser);
16709 return constructor_p;
16712 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16713 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16714 they must be performed once we are in the scope of the function.
16716 Returns the function defined. */
16719 cp_parser_function_definition_from_specifiers_and_declarator
16720 (cp_parser* parser,
16721 cp_decl_specifier_seq *decl_specifiers,
16723 const cp_declarator *declarator)
16728 /* Begin the function-definition. */
16729 success_p = start_function (decl_specifiers, declarator, attributes);
16731 /* The things we're about to see are not directly qualified by any
16732 template headers we've seen thus far. */
16733 reset_specialization ();
16735 /* If there were names looked up in the decl-specifier-seq that we
16736 did not check, check them now. We must wait until we are in the
16737 scope of the function to perform the checks, since the function
16738 might be a friend. */
16739 perform_deferred_access_checks ();
16743 /* Skip the entire function. */
16744 cp_parser_skip_to_end_of_block_or_statement (parser);
16745 fn = error_mark_node;
16747 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16749 /* Seen already, skip it. An error message has already been output. */
16750 cp_parser_skip_to_end_of_block_or_statement (parser);
16751 fn = current_function_decl;
16752 current_function_decl = NULL_TREE;
16753 /* If this is a function from a class, pop the nested class. */
16754 if (current_class_name)
16755 pop_nested_class ();
16758 fn = cp_parser_function_definition_after_declarator (parser,
16759 /*inline_p=*/false);
16764 /* Parse the part of a function-definition that follows the
16765 declarator. INLINE_P is TRUE iff this function is an inline
16766 function defined with a class-specifier.
16768 Returns the function defined. */
16771 cp_parser_function_definition_after_declarator (cp_parser* parser,
16775 bool ctor_initializer_p = false;
16776 bool saved_in_unbraced_linkage_specification_p;
16777 bool saved_in_function_body;
16778 unsigned saved_num_template_parameter_lists;
16780 saved_in_function_body = parser->in_function_body;
16781 parser->in_function_body = true;
16782 /* If the next token is `return', then the code may be trying to
16783 make use of the "named return value" extension that G++ used to
16785 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16787 /* Consume the `return' keyword. */
16788 cp_lexer_consume_token (parser->lexer);
16789 /* Look for the identifier that indicates what value is to be
16791 cp_parser_identifier (parser);
16792 /* Issue an error message. */
16793 error ("named return values are no longer supported");
16794 /* Skip tokens until we reach the start of the function body. */
16797 cp_token *token = cp_lexer_peek_token (parser->lexer);
16798 if (token->type == CPP_OPEN_BRACE
16799 || token->type == CPP_EOF
16800 || token->type == CPP_PRAGMA_EOL)
16802 cp_lexer_consume_token (parser->lexer);
16805 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16806 anything declared inside `f'. */
16807 saved_in_unbraced_linkage_specification_p
16808 = parser->in_unbraced_linkage_specification_p;
16809 parser->in_unbraced_linkage_specification_p = false;
16810 /* Inside the function, surrounding template-parameter-lists do not
16812 saved_num_template_parameter_lists
16813 = parser->num_template_parameter_lists;
16814 parser->num_template_parameter_lists = 0;
16815 /* If the next token is `try', then we are looking at a
16816 function-try-block. */
16817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16818 ctor_initializer_p = cp_parser_function_try_block (parser);
16819 /* A function-try-block includes the function-body, so we only do
16820 this next part if we're not processing a function-try-block. */
16823 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16825 /* Finish the function. */
16826 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16827 (inline_p ? 2 : 0));
16828 /* Generate code for it, if necessary. */
16829 expand_or_defer_fn (fn);
16830 /* Restore the saved values. */
16831 parser->in_unbraced_linkage_specification_p
16832 = saved_in_unbraced_linkage_specification_p;
16833 parser->num_template_parameter_lists
16834 = saved_num_template_parameter_lists;
16835 parser->in_function_body = saved_in_function_body;
16840 /* Parse a template-declaration, assuming that the `export' (and
16841 `extern') keywords, if present, has already been scanned. MEMBER_P
16842 is as for cp_parser_template_declaration. */
16845 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16847 tree decl = NULL_TREE;
16848 VEC (deferred_access_check,gc) *checks;
16849 tree parameter_list;
16850 bool friend_p = false;
16851 bool need_lang_pop;
16853 /* Look for the `template' keyword. */
16854 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16858 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16860 if (at_class_scope_p () && current_function_decl)
16862 /* 14.5.2.2 [temp.mem]
16864 A local class shall not have member templates. */
16865 error ("invalid declaration of member template in local class");
16866 cp_parser_skip_to_end_of_block_or_statement (parser);
16871 A template ... shall not have C linkage. */
16872 if (current_lang_name == lang_name_c)
16874 error ("template with C linkage");
16875 /* Give it C++ linkage to avoid confusing other parts of the
16877 push_lang_context (lang_name_cplusplus);
16878 need_lang_pop = true;
16881 need_lang_pop = false;
16883 /* We cannot perform access checks on the template parameter
16884 declarations until we know what is being declared, just as we
16885 cannot check the decl-specifier list. */
16886 push_deferring_access_checks (dk_deferred);
16888 /* If the next token is `>', then we have an invalid
16889 specialization. Rather than complain about an invalid template
16890 parameter, issue an error message here. */
16891 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16893 cp_parser_error (parser, "invalid explicit specialization");
16894 begin_specialization ();
16895 parameter_list = NULL_TREE;
16898 /* Parse the template parameters. */
16899 parameter_list = cp_parser_template_parameter_list (parser);
16901 /* Get the deferred access checks from the parameter list. These
16902 will be checked once we know what is being declared, as for a
16903 member template the checks must be performed in the scope of the
16904 class containing the member. */
16905 checks = get_deferred_access_checks ();
16907 /* Look for the `>'. */
16908 cp_parser_skip_to_end_of_template_parameter_list (parser);
16909 /* We just processed one more parameter list. */
16910 ++parser->num_template_parameter_lists;
16911 /* If the next token is `template', there are more template
16913 if (cp_lexer_next_token_is_keyword (parser->lexer,
16915 cp_parser_template_declaration_after_export (parser, member_p);
16918 /* There are no access checks when parsing a template, as we do not
16919 know if a specialization will be a friend. */
16920 push_deferring_access_checks (dk_no_check);
16921 decl = cp_parser_single_declaration (parser,
16924 /*explicit_specialization_p=*/false,
16926 pop_deferring_access_checks ();
16928 /* If this is a member template declaration, let the front
16930 if (member_p && !friend_p && decl)
16932 if (TREE_CODE (decl) == TYPE_DECL)
16933 cp_parser_check_access_in_redeclaration (decl);
16935 decl = finish_member_template_decl (decl);
16937 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16938 make_friend_class (current_class_type, TREE_TYPE (decl),
16939 /*complain=*/true);
16941 /* We are done with the current parameter list. */
16942 --parser->num_template_parameter_lists;
16944 pop_deferring_access_checks ();
16947 finish_template_decl (parameter_list);
16949 /* Register member declarations. */
16950 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16951 finish_member_declaration (decl);
16952 /* For the erroneous case of a template with C linkage, we pushed an
16953 implicit C++ linkage scope; exit that scope now. */
16955 pop_lang_context ();
16956 /* If DECL is a function template, we must return to parse it later.
16957 (Even though there is no definition, there might be default
16958 arguments that need handling.) */
16959 if (member_p && decl
16960 && (TREE_CODE (decl) == FUNCTION_DECL
16961 || DECL_FUNCTION_TEMPLATE_P (decl)))
16962 TREE_VALUE (parser->unparsed_functions_queues)
16963 = tree_cons (NULL_TREE, decl,
16964 TREE_VALUE (parser->unparsed_functions_queues));
16967 /* Perform the deferred access checks from a template-parameter-list.
16968 CHECKS is a TREE_LIST of access checks, as returned by
16969 get_deferred_access_checks. */
16972 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16974 ++processing_template_parmlist;
16975 perform_access_checks (checks);
16976 --processing_template_parmlist;
16979 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16980 `function-definition' sequence. MEMBER_P is true, this declaration
16981 appears in a class scope.
16983 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16984 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16987 cp_parser_single_declaration (cp_parser* parser,
16988 VEC (deferred_access_check,gc)* checks,
16990 bool explicit_specialization_p,
16993 int declares_class_or_enum;
16994 tree decl = NULL_TREE;
16995 cp_decl_specifier_seq decl_specifiers;
16996 bool function_definition_p = false;
16998 /* This function is only used when processing a template
17000 gcc_assert (innermost_scope_kind () == sk_template_parms
17001 || innermost_scope_kind () == sk_template_spec);
17003 /* Defer access checks until we know what is being declared. */
17004 push_deferring_access_checks (dk_deferred);
17006 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17008 cp_parser_decl_specifier_seq (parser,
17009 CP_PARSER_FLAGS_OPTIONAL,
17011 &declares_class_or_enum);
17013 *friend_p = cp_parser_friend_p (&decl_specifiers);
17015 /* There are no template typedefs. */
17016 if (decl_specifiers.specs[(int) ds_typedef])
17018 error ("template declaration of %qs", "typedef");
17019 decl = error_mark_node;
17022 /* Gather up the access checks that occurred the
17023 decl-specifier-seq. */
17024 stop_deferring_access_checks ();
17026 /* Check for the declaration of a template class. */
17027 if (declares_class_or_enum)
17029 if (cp_parser_declares_only_class_p (parser))
17031 decl = shadow_tag (&decl_specifiers);
17036 friend template <typename T> struct A<T>::B;
17039 A<T>::B will be represented by a TYPENAME_TYPE, and
17040 therefore not recognized by shadow_tag. */
17041 if (friend_p && *friend_p
17043 && decl_specifiers.type
17044 && TYPE_P (decl_specifiers.type))
17045 decl = decl_specifiers.type;
17047 if (decl && decl != error_mark_node)
17048 decl = TYPE_NAME (decl);
17050 decl = error_mark_node;
17052 /* Perform access checks for template parameters. */
17053 cp_parser_perform_template_parameter_access_checks (checks);
17056 /* If it's not a template class, try for a template function. If
17057 the next token is a `;', then this declaration does not declare
17058 anything. But, if there were errors in the decl-specifiers, then
17059 the error might well have come from an attempted class-specifier.
17060 In that case, there's no need to warn about a missing declarator. */
17062 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17063 || decl_specifiers.type != error_mark_node))
17065 decl = cp_parser_init_declarator (parser,
17068 /*function_definition_allowed_p=*/true,
17070 declares_class_or_enum,
17071 &function_definition_p);
17073 /* 7.1.1-1 [dcl.stc]
17075 A storage-class-specifier shall not be specified in an explicit
17076 specialization... */
17078 && explicit_specialization_p
17079 && decl_specifiers.storage_class != sc_none)
17081 error ("explicit template specialization cannot have a storage class");
17082 decl = error_mark_node;
17086 pop_deferring_access_checks ();
17088 /* Clear any current qualification; whatever comes next is the start
17089 of something new. */
17090 parser->scope = NULL_TREE;
17091 parser->qualifying_scope = NULL_TREE;
17092 parser->object_scope = NULL_TREE;
17093 /* Look for a trailing `;' after the declaration. */
17094 if (!function_definition_p
17095 && (decl == error_mark_node
17096 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17097 cp_parser_skip_to_end_of_block_or_statement (parser);
17102 /* Parse a cast-expression that is not the operand of a unary "&". */
17105 cp_parser_simple_cast_expression (cp_parser *parser)
17107 return cp_parser_cast_expression (parser, /*address_p=*/false,
17111 /* Parse a functional cast to TYPE. Returns an expression
17112 representing the cast. */
17115 cp_parser_functional_cast (cp_parser* parser, tree type)
17117 tree expression_list;
17121 = cp_parser_parenthesized_expression_list (parser, false,
17123 /*allow_expansion_p=*/true,
17124 /*non_constant_p=*/NULL);
17126 cast = build_functional_cast (type, expression_list);
17127 /* [expr.const]/1: In an integral constant expression "only type
17128 conversions to integral or enumeration type can be used". */
17129 if (TREE_CODE (type) == TYPE_DECL)
17130 type = TREE_TYPE (type);
17131 if (cast != error_mark_node
17132 && !cast_valid_in_integral_constant_expression_p (type)
17133 && (cp_parser_non_integral_constant_expression
17134 (parser, "a call to a constructor")))
17135 return error_mark_node;
17139 /* Save the tokens that make up the body of a member function defined
17140 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17141 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17142 specifiers applied to the declaration. Returns the FUNCTION_DECL
17143 for the member function. */
17146 cp_parser_save_member_function_body (cp_parser* parser,
17147 cp_decl_specifier_seq *decl_specifiers,
17148 cp_declarator *declarator,
17155 /* Create the function-declaration. */
17156 fn = start_method (decl_specifiers, declarator, attributes);
17157 /* If something went badly wrong, bail out now. */
17158 if (fn == error_mark_node)
17160 /* If there's a function-body, skip it. */
17161 if (cp_parser_token_starts_function_definition_p
17162 (cp_lexer_peek_token (parser->lexer)))
17163 cp_parser_skip_to_end_of_block_or_statement (parser);
17164 return error_mark_node;
17167 /* Remember it, if there default args to post process. */
17168 cp_parser_save_default_args (parser, fn);
17170 /* Save away the tokens that make up the body of the
17172 first = parser->lexer->next_token;
17173 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17174 /* Handle function try blocks. */
17175 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17176 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17177 last = parser->lexer->next_token;
17179 /* Save away the inline definition; we will process it when the
17180 class is complete. */
17181 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17182 DECL_PENDING_INLINE_P (fn) = 1;
17184 /* We need to know that this was defined in the class, so that
17185 friend templates are handled correctly. */
17186 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17188 /* We're done with the inline definition. */
17189 finish_method (fn);
17191 /* Add FN to the queue of functions to be parsed later. */
17192 TREE_VALUE (parser->unparsed_functions_queues)
17193 = tree_cons (NULL_TREE, fn,
17194 TREE_VALUE (parser->unparsed_functions_queues));
17199 /* Parse a template-argument-list, as well as the trailing ">" (but
17200 not the opening ">"). See cp_parser_template_argument_list for the
17204 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17208 tree saved_qualifying_scope;
17209 tree saved_object_scope;
17210 bool saved_greater_than_is_operator_p;
17211 bool saved_skip_evaluation;
17215 When parsing a template-id, the first non-nested `>' is taken as
17216 the end of the template-argument-list rather than a greater-than
17218 saved_greater_than_is_operator_p
17219 = parser->greater_than_is_operator_p;
17220 parser->greater_than_is_operator_p = false;
17221 /* Parsing the argument list may modify SCOPE, so we save it
17223 saved_scope = parser->scope;
17224 saved_qualifying_scope = parser->qualifying_scope;
17225 saved_object_scope = parser->object_scope;
17226 /* We need to evaluate the template arguments, even though this
17227 template-id may be nested within a "sizeof". */
17228 saved_skip_evaluation = skip_evaluation;
17229 skip_evaluation = false;
17230 /* Parse the template-argument-list itself. */
17231 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17232 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17233 arguments = NULL_TREE;
17235 arguments = cp_parser_template_argument_list (parser);
17236 /* Look for the `>' that ends the template-argument-list. If we find
17237 a '>>' instead, it's probably just a typo. */
17238 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17240 if (cxx_dialect != cxx98)
17242 /* In C++0x, a `>>' in a template argument list or cast
17243 expression is considered to be two separate `>'
17244 tokens. So, change the current token to a `>', but don't
17245 consume it: it will be consumed later when the outer
17246 template argument list (or cast expression) is parsed.
17247 Note that this replacement of `>' for `>>' is necessary
17248 even if we are parsing tentatively: in the tentative
17249 case, after calling
17250 cp_parser_enclosed_template_argument_list we will always
17251 throw away all of the template arguments and the first
17252 closing `>', either because the template argument list
17253 was erroneous or because we are replacing those tokens
17254 with a CPP_TEMPLATE_ID token. The second `>' (which will
17255 not have been thrown away) is needed either to close an
17256 outer template argument list or to complete a new-style
17258 cp_token *token = cp_lexer_peek_token (parser->lexer);
17259 token->type = CPP_GREATER;
17261 else if (!saved_greater_than_is_operator_p)
17263 /* If we're in a nested template argument list, the '>>' has
17264 to be a typo for '> >'. We emit the error message, but we
17265 continue parsing and we push a '>' as next token, so that
17266 the argument list will be parsed correctly. Note that the
17267 global source location is still on the token before the
17268 '>>', so we need to say explicitly where we want it. */
17269 cp_token *token = cp_lexer_peek_token (parser->lexer);
17270 error ("%H%<>>%> should be %<> >%> "
17271 "within a nested template argument list",
17274 token->type = CPP_GREATER;
17278 /* If this is not a nested template argument list, the '>>'
17279 is a typo for '>'. Emit an error message and continue.
17280 Same deal about the token location, but here we can get it
17281 right by consuming the '>>' before issuing the diagnostic. */
17282 cp_lexer_consume_token (parser->lexer);
17283 error ("spurious %<>>%>, use %<>%> to terminate "
17284 "a template argument list");
17288 cp_parser_skip_to_end_of_template_parameter_list (parser);
17289 /* The `>' token might be a greater-than operator again now. */
17290 parser->greater_than_is_operator_p
17291 = saved_greater_than_is_operator_p;
17292 /* Restore the SAVED_SCOPE. */
17293 parser->scope = saved_scope;
17294 parser->qualifying_scope = saved_qualifying_scope;
17295 parser->object_scope = saved_object_scope;
17296 skip_evaluation = saved_skip_evaluation;
17301 /* MEMBER_FUNCTION is a member function, or a friend. If default
17302 arguments, or the body of the function have not yet been parsed,
17306 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17308 /* If this member is a template, get the underlying
17310 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17311 member_function = DECL_TEMPLATE_RESULT (member_function);
17313 /* There should not be any class definitions in progress at this
17314 point; the bodies of members are only parsed outside of all class
17316 gcc_assert (parser->num_classes_being_defined == 0);
17317 /* While we're parsing the member functions we might encounter more
17318 classes. We want to handle them right away, but we don't want
17319 them getting mixed up with functions that are currently in the
17321 parser->unparsed_functions_queues
17322 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17324 /* Make sure that any template parameters are in scope. */
17325 maybe_begin_member_template_processing (member_function);
17327 /* If the body of the function has not yet been parsed, parse it
17329 if (DECL_PENDING_INLINE_P (member_function))
17331 tree function_scope;
17332 cp_token_cache *tokens;
17334 /* The function is no longer pending; we are processing it. */
17335 tokens = DECL_PENDING_INLINE_INFO (member_function);
17336 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17337 DECL_PENDING_INLINE_P (member_function) = 0;
17339 /* If this is a local class, enter the scope of the containing
17341 function_scope = current_function_decl;
17342 if (function_scope)
17343 push_function_context_to (function_scope);
17346 /* Push the body of the function onto the lexer stack. */
17347 cp_parser_push_lexer_for_tokens (parser, tokens);
17349 /* Let the front end know that we going to be defining this
17351 start_preparsed_function (member_function, NULL_TREE,
17352 SF_PRE_PARSED | SF_INCLASS_INLINE);
17354 /* Don't do access checking if it is a templated function. */
17355 if (processing_template_decl)
17356 push_deferring_access_checks (dk_no_check);
17358 /* Now, parse the body of the function. */
17359 cp_parser_function_definition_after_declarator (parser,
17360 /*inline_p=*/true);
17362 if (processing_template_decl)
17363 pop_deferring_access_checks ();
17365 /* Leave the scope of the containing function. */
17366 if (function_scope)
17367 pop_function_context_from (function_scope);
17368 cp_parser_pop_lexer (parser);
17371 /* Remove any template parameters from the symbol table. */
17372 maybe_end_member_template_processing ();
17374 /* Restore the queue. */
17375 parser->unparsed_functions_queues
17376 = TREE_CHAIN (parser->unparsed_functions_queues);
17379 /* If DECL contains any default args, remember it on the unparsed
17380 functions queue. */
17383 cp_parser_save_default_args (cp_parser* parser, tree decl)
17387 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17389 probe = TREE_CHAIN (probe))
17390 if (TREE_PURPOSE (probe))
17392 TREE_PURPOSE (parser->unparsed_functions_queues)
17393 = tree_cons (current_class_type, decl,
17394 TREE_PURPOSE (parser->unparsed_functions_queues));
17399 /* FN is a FUNCTION_DECL which may contains a parameter with an
17400 unparsed DEFAULT_ARG. Parse the default args now. This function
17401 assumes that the current scope is the scope in which the default
17402 argument should be processed. */
17405 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17407 bool saved_local_variables_forbidden_p;
17410 /* While we're parsing the default args, we might (due to the
17411 statement expression extension) encounter more classes. We want
17412 to handle them right away, but we don't want them getting mixed
17413 up with default args that are currently in the queue. */
17414 parser->unparsed_functions_queues
17415 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17417 /* Local variable names (and the `this' keyword) may not appear
17418 in a default argument. */
17419 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17420 parser->local_variables_forbidden_p = true;
17422 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17424 parm = TREE_CHAIN (parm))
17426 cp_token_cache *tokens;
17427 tree default_arg = TREE_PURPOSE (parm);
17429 VEC(tree,gc) *insts;
17436 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17437 /* This can happen for a friend declaration for a function
17438 already declared with default arguments. */
17441 /* Push the saved tokens for the default argument onto the parser's
17443 tokens = DEFARG_TOKENS (default_arg);
17444 cp_parser_push_lexer_for_tokens (parser, tokens);
17446 /* Parse the assignment-expression. */
17447 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17449 if (!processing_template_decl)
17450 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17452 TREE_PURPOSE (parm) = parsed_arg;
17454 /* Update any instantiations we've already created. */
17455 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17456 VEC_iterate (tree, insts, ix, copy); ix++)
17457 TREE_PURPOSE (copy) = parsed_arg;
17459 /* If the token stream has not been completely used up, then
17460 there was extra junk after the end of the default
17462 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17463 cp_parser_error (parser, "expected %<,%>");
17465 /* Revert to the main lexer. */
17466 cp_parser_pop_lexer (parser);
17469 /* Make sure no default arg is missing. */
17470 check_default_args (fn);
17472 /* Restore the state of local_variables_forbidden_p. */
17473 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17475 /* Restore the queue. */
17476 parser->unparsed_functions_queues
17477 = TREE_CHAIN (parser->unparsed_functions_queues);
17480 /* Parse the operand of `sizeof' (or a similar operator). Returns
17481 either a TYPE or an expression, depending on the form of the
17482 input. The KEYWORD indicates which kind of expression we have
17486 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17488 static const char *format;
17489 tree expr = NULL_TREE;
17490 const char *saved_message;
17492 bool saved_integral_constant_expression_p;
17493 bool saved_non_integral_constant_expression_p;
17494 bool pack_expansion_p = false;
17496 /* Initialize FORMAT the first time we get here. */
17498 format = "types may not be defined in '%s' expressions";
17500 /* Types cannot be defined in a `sizeof' expression. Save away the
17502 saved_message = parser->type_definition_forbidden_message;
17503 /* And create the new one. */
17504 parser->type_definition_forbidden_message = tmp
17505 = XNEWVEC (char, strlen (format)
17506 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17508 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17510 /* The restrictions on constant-expressions do not apply inside
17511 sizeof expressions. */
17512 saved_integral_constant_expression_p
17513 = parser->integral_constant_expression_p;
17514 saved_non_integral_constant_expression_p
17515 = parser->non_integral_constant_expression_p;
17516 parser->integral_constant_expression_p = false;
17518 /* If it's a `...', then we are computing the length of a parameter
17520 if (keyword == RID_SIZEOF
17521 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17523 /* Consume the `...'. */
17524 cp_lexer_consume_token (parser->lexer);
17525 maybe_warn_variadic_templates ();
17527 /* Note that this is an expansion. */
17528 pack_expansion_p = true;
17531 /* Do not actually evaluate the expression. */
17533 /* If it's a `(', then we might be looking at the type-id
17535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17538 bool saved_in_type_id_in_expr_p;
17540 /* We can't be sure yet whether we're looking at a type-id or an
17542 cp_parser_parse_tentatively (parser);
17543 /* Consume the `('. */
17544 cp_lexer_consume_token (parser->lexer);
17545 /* Parse the type-id. */
17546 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17547 parser->in_type_id_in_expr_p = true;
17548 type = cp_parser_type_id (parser);
17549 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17550 /* Now, look for the trailing `)'. */
17551 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17552 /* If all went well, then we're done. */
17553 if (cp_parser_parse_definitely (parser))
17555 cp_decl_specifier_seq decl_specs;
17557 /* Build a trivial decl-specifier-seq. */
17558 clear_decl_specs (&decl_specs);
17559 decl_specs.type = type;
17561 /* Call grokdeclarator to figure out what type this is. */
17562 expr = grokdeclarator (NULL,
17566 /*attrlist=*/NULL);
17570 /* If the type-id production did not work out, then we must be
17571 looking at the unary-expression production. */
17573 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17576 if (pack_expansion_p)
17577 /* Build a pack expansion. */
17578 expr = make_pack_expansion (expr);
17580 /* Go back to evaluating expressions. */
17583 /* Free the message we created. */
17585 /* And restore the old one. */
17586 parser->type_definition_forbidden_message = saved_message;
17587 parser->integral_constant_expression_p
17588 = saved_integral_constant_expression_p;
17589 parser->non_integral_constant_expression_p
17590 = saved_non_integral_constant_expression_p;
17595 /* If the current declaration has no declarator, return true. */
17598 cp_parser_declares_only_class_p (cp_parser *parser)
17600 /* If the next token is a `;' or a `,' then there is no
17602 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17603 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17606 /* Update the DECL_SPECS to reflect the storage class indicated by
17610 cp_parser_set_storage_class (cp_parser *parser,
17611 cp_decl_specifier_seq *decl_specs,
17614 cp_storage_class storage_class;
17616 if (parser->in_unbraced_linkage_specification_p)
17618 error ("invalid use of %qD in linkage specification",
17619 ridpointers[keyword]);
17622 else if (decl_specs->storage_class != sc_none)
17624 decl_specs->conflicting_specifiers_p = true;
17628 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17629 && decl_specs->specs[(int) ds_thread])
17631 error ("%<__thread%> before %qD", ridpointers[keyword]);
17632 decl_specs->specs[(int) ds_thread] = 0;
17638 storage_class = sc_auto;
17641 storage_class = sc_register;
17644 storage_class = sc_static;
17647 storage_class = sc_extern;
17650 storage_class = sc_mutable;
17653 gcc_unreachable ();
17655 decl_specs->storage_class = storage_class;
17657 /* A storage class specifier cannot be applied alongside a typedef
17658 specifier. If there is a typedef specifier present then set
17659 conflicting_specifiers_p which will trigger an error later
17660 on in grokdeclarator. */
17661 if (decl_specs->specs[(int)ds_typedef])
17662 decl_specs->conflicting_specifiers_p = true;
17665 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17666 is true, the type is a user-defined type; otherwise it is a
17667 built-in type specified by a keyword. */
17670 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17672 bool user_defined_p)
17674 decl_specs->any_specifiers_p = true;
17676 /* If the user tries to redeclare bool or wchar_t (with, for
17677 example, in "typedef int wchar_t;") we remember that this is what
17678 happened. In system headers, we ignore these declarations so
17679 that G++ can work with system headers that are not C++-safe. */
17680 if (decl_specs->specs[(int) ds_typedef]
17682 && (type_spec == boolean_type_node
17683 || type_spec == wchar_type_node)
17684 && (decl_specs->type
17685 || decl_specs->specs[(int) ds_long]
17686 || decl_specs->specs[(int) ds_short]
17687 || decl_specs->specs[(int) ds_unsigned]
17688 || decl_specs->specs[(int) ds_signed]))
17690 decl_specs->redefined_builtin_type = type_spec;
17691 if (!decl_specs->type)
17693 decl_specs->type = type_spec;
17694 decl_specs->user_defined_type_p = false;
17697 else if (decl_specs->type)
17698 decl_specs->multiple_types_p = true;
17701 decl_specs->type = type_spec;
17702 decl_specs->user_defined_type_p = user_defined_p;
17703 decl_specs->redefined_builtin_type = NULL_TREE;
17707 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17708 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17711 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17713 return decl_specifiers->specs[(int) ds_friend] != 0;
17716 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17717 issue an error message indicating that TOKEN_DESC was expected.
17719 Returns the token consumed, if the token had the appropriate type.
17720 Otherwise, returns NULL. */
17723 cp_parser_require (cp_parser* parser,
17724 enum cpp_ttype type,
17725 const char* token_desc)
17727 if (cp_lexer_next_token_is (parser->lexer, type))
17728 return cp_lexer_consume_token (parser->lexer);
17731 /* Output the MESSAGE -- unless we're parsing tentatively. */
17732 if (!cp_parser_simulate_error (parser))
17734 char *message = concat ("expected ", token_desc, NULL);
17735 cp_parser_error (parser, message);
17742 /* An error message is produced if the next token is not '>'.
17743 All further tokens are skipped until the desired token is
17744 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17747 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17749 /* Current level of '< ... >'. */
17750 unsigned level = 0;
17751 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17752 unsigned nesting_depth = 0;
17754 /* Are we ready, yet? If not, issue error message. */
17755 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17758 /* Skip tokens until the desired token is found. */
17761 /* Peek at the next token. */
17762 switch (cp_lexer_peek_token (parser->lexer)->type)
17765 if (!nesting_depth)
17770 if (cxx_dialect == cxx98)
17771 /* C++0x views the `>>' operator as two `>' tokens, but
17774 else if (!nesting_depth && level-- == 0)
17776 /* We've hit a `>>' where the first `>' closes the
17777 template argument list, and the second `>' is
17778 spurious. Just consume the `>>' and stop; we've
17779 already produced at least one error. */
17780 cp_lexer_consume_token (parser->lexer);
17783 /* Fall through for C++0x, so we handle the second `>' in
17787 if (!nesting_depth && level-- == 0)
17789 /* We've reached the token we want, consume it and stop. */
17790 cp_lexer_consume_token (parser->lexer);
17795 case CPP_OPEN_PAREN:
17796 case CPP_OPEN_SQUARE:
17800 case CPP_CLOSE_PAREN:
17801 case CPP_CLOSE_SQUARE:
17802 if (nesting_depth-- == 0)
17807 case CPP_PRAGMA_EOL:
17808 case CPP_SEMICOLON:
17809 case CPP_OPEN_BRACE:
17810 case CPP_CLOSE_BRACE:
17811 /* The '>' was probably forgotten, don't look further. */
17818 /* Consume this token. */
17819 cp_lexer_consume_token (parser->lexer);
17823 /* If the next token is the indicated keyword, consume it. Otherwise,
17824 issue an error message indicating that TOKEN_DESC was expected.
17826 Returns the token consumed, if the token had the appropriate type.
17827 Otherwise, returns NULL. */
17830 cp_parser_require_keyword (cp_parser* parser,
17832 const char* token_desc)
17834 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17836 if (token && token->keyword != keyword)
17838 dyn_string_t error_msg;
17840 /* Format the error message. */
17841 error_msg = dyn_string_new (0);
17842 dyn_string_append_cstr (error_msg, "expected ");
17843 dyn_string_append_cstr (error_msg, token_desc);
17844 cp_parser_error (parser, error_msg->s);
17845 dyn_string_delete (error_msg);
17852 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17853 function-definition. */
17856 cp_parser_token_starts_function_definition_p (cp_token* token)
17858 return (/* An ordinary function-body begins with an `{'. */
17859 token->type == CPP_OPEN_BRACE
17860 /* A ctor-initializer begins with a `:'. */
17861 || token->type == CPP_COLON
17862 /* A function-try-block begins with `try'. */
17863 || token->keyword == RID_TRY
17864 /* The named return value extension begins with `return'. */
17865 || token->keyword == RID_RETURN);
17868 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17872 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17876 token = cp_lexer_peek_token (parser->lexer);
17877 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17880 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17881 C++0x) ending a template-argument. */
17884 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17888 token = cp_lexer_peek_token (parser->lexer);
17889 return (token->type == CPP_COMMA
17890 || token->type == CPP_GREATER
17891 || token->type == CPP_ELLIPSIS
17892 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17895 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17896 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17899 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17904 token = cp_lexer_peek_nth_token (parser->lexer, n);
17905 if (token->type == CPP_LESS)
17907 /* Check for the sequence `<::' in the original code. It would be lexed as
17908 `[:', where `[' is a digraph, and there is no whitespace before
17910 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17913 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17914 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17920 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17921 or none_type otherwise. */
17923 static enum tag_types
17924 cp_parser_token_is_class_key (cp_token* token)
17926 switch (token->keyword)
17931 return record_type;
17940 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17943 cp_parser_check_class_key (enum tag_types class_key, tree type)
17945 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17946 pedwarn ("%qs tag used in naming %q#T",
17947 class_key == union_type ? "union"
17948 : class_key == record_type ? "struct" : "class",
17952 /* Issue an error message if DECL is redeclared with different
17953 access than its original declaration [class.access.spec/3].
17954 This applies to nested classes and nested class templates.
17958 cp_parser_check_access_in_redeclaration (tree decl)
17960 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17963 if ((TREE_PRIVATE (decl)
17964 != (current_access_specifier == access_private_node))
17965 || (TREE_PROTECTED (decl)
17966 != (current_access_specifier == access_protected_node)))
17967 error ("%qD redeclared with different access", decl);
17970 /* Look for the `template' keyword, as a syntactic disambiguator.
17971 Return TRUE iff it is present, in which case it will be
17975 cp_parser_optional_template_keyword (cp_parser *parser)
17977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17979 /* The `template' keyword can only be used within templates;
17980 outside templates the parser can always figure out what is a
17981 template and what is not. */
17982 if (!processing_template_decl)
17984 error ("%<template%> (as a disambiguator) is only allowed "
17985 "within templates");
17986 /* If this part of the token stream is rescanned, the same
17987 error message would be generated. So, we purge the token
17988 from the stream. */
17989 cp_lexer_purge_token (parser->lexer);
17994 /* Consume the `template' keyword. */
17995 cp_lexer_consume_token (parser->lexer);
18003 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18004 set PARSER->SCOPE, and perform other related actions. */
18007 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18010 struct tree_check *check_value;
18011 deferred_access_check *chk;
18012 VEC (deferred_access_check,gc) *checks;
18014 /* Get the stored value. */
18015 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18016 /* Perform any access checks that were deferred. */
18017 checks = check_value->checks;
18021 VEC_iterate (deferred_access_check, checks, i, chk) ;
18024 perform_or_defer_access_check (chk->binfo,
18029 /* Set the scope from the stored value. */
18030 parser->scope = check_value->value;
18031 parser->qualifying_scope = check_value->qualifying_scope;
18032 parser->object_scope = NULL_TREE;
18035 /* Consume tokens up through a non-nested END token. */
18038 cp_parser_cache_group (cp_parser *parser,
18039 enum cpp_ttype end,
18046 /* Abort a parenthesized expression if we encounter a brace. */
18047 if ((end == CPP_CLOSE_PAREN || depth == 0)
18048 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18050 /* If we've reached the end of the file, stop. */
18051 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18052 || (end != CPP_PRAGMA_EOL
18053 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18055 /* Consume the next token. */
18056 token = cp_lexer_consume_token (parser->lexer);
18057 /* See if it starts a new group. */
18058 if (token->type == CPP_OPEN_BRACE)
18060 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18064 else if (token->type == CPP_OPEN_PAREN)
18065 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18066 else if (token->type == CPP_PRAGMA)
18067 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18068 else if (token->type == end)
18073 /* Begin parsing tentatively. We always save tokens while parsing
18074 tentatively so that if the tentative parsing fails we can restore the
18078 cp_parser_parse_tentatively (cp_parser* parser)
18080 /* Enter a new parsing context. */
18081 parser->context = cp_parser_context_new (parser->context);
18082 /* Begin saving tokens. */
18083 cp_lexer_save_tokens (parser->lexer);
18084 /* In order to avoid repetitive access control error messages,
18085 access checks are queued up until we are no longer parsing
18087 push_deferring_access_checks (dk_deferred);
18090 /* Commit to the currently active tentative parse. */
18093 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18095 cp_parser_context *context;
18098 /* Mark all of the levels as committed. */
18099 lexer = parser->lexer;
18100 for (context = parser->context; context->next; context = context->next)
18102 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18104 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18105 while (!cp_lexer_saving_tokens (lexer))
18106 lexer = lexer->next;
18107 cp_lexer_commit_tokens (lexer);
18111 /* Abort the currently active tentative parse. All consumed tokens
18112 will be rolled back, and no diagnostics will be issued. */
18115 cp_parser_abort_tentative_parse (cp_parser* parser)
18117 cp_parser_simulate_error (parser);
18118 /* Now, pretend that we want to see if the construct was
18119 successfully parsed. */
18120 cp_parser_parse_definitely (parser);
18123 /* Stop parsing tentatively. If a parse error has occurred, restore the
18124 token stream. Otherwise, commit to the tokens we have consumed.
18125 Returns true if no error occurred; false otherwise. */
18128 cp_parser_parse_definitely (cp_parser* parser)
18130 bool error_occurred;
18131 cp_parser_context *context;
18133 /* Remember whether or not an error occurred, since we are about to
18134 destroy that information. */
18135 error_occurred = cp_parser_error_occurred (parser);
18136 /* Remove the topmost context from the stack. */
18137 context = parser->context;
18138 parser->context = context->next;
18139 /* If no parse errors occurred, commit to the tentative parse. */
18140 if (!error_occurred)
18142 /* Commit to the tokens read tentatively, unless that was
18144 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18145 cp_lexer_commit_tokens (parser->lexer);
18147 pop_to_parent_deferring_access_checks ();
18149 /* Otherwise, if errors occurred, roll back our state so that things
18150 are just as they were before we began the tentative parse. */
18153 cp_lexer_rollback_tokens (parser->lexer);
18154 pop_deferring_access_checks ();
18156 /* Add the context to the front of the free list. */
18157 context->next = cp_parser_context_free_list;
18158 cp_parser_context_free_list = context;
18160 return !error_occurred;
18163 /* Returns true if we are parsing tentatively and are not committed to
18164 this tentative parse. */
18167 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18169 return (cp_parser_parsing_tentatively (parser)
18170 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18173 /* Returns nonzero iff an error has occurred during the most recent
18174 tentative parse. */
18177 cp_parser_error_occurred (cp_parser* parser)
18179 return (cp_parser_parsing_tentatively (parser)
18180 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18183 /* Returns nonzero if GNU extensions are allowed. */
18186 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18188 return parser->allow_gnu_extensions_p;
18191 /* Objective-C++ Productions */
18194 /* Parse an Objective-C expression, which feeds into a primary-expression
18198 objc-message-expression
18199 objc-string-literal
18200 objc-encode-expression
18201 objc-protocol-expression
18202 objc-selector-expression
18204 Returns a tree representation of the expression. */
18207 cp_parser_objc_expression (cp_parser* parser)
18209 /* Try to figure out what kind of declaration is present. */
18210 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18214 case CPP_OPEN_SQUARE:
18215 return cp_parser_objc_message_expression (parser);
18217 case CPP_OBJC_STRING:
18218 kwd = cp_lexer_consume_token (parser->lexer);
18219 return objc_build_string_object (kwd->u.value);
18222 switch (kwd->keyword)
18224 case RID_AT_ENCODE:
18225 return cp_parser_objc_encode_expression (parser);
18227 case RID_AT_PROTOCOL:
18228 return cp_parser_objc_protocol_expression (parser);
18230 case RID_AT_SELECTOR:
18231 return cp_parser_objc_selector_expression (parser);
18237 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18238 cp_parser_skip_to_end_of_block_or_statement (parser);
18241 return error_mark_node;
18244 /* Parse an Objective-C message expression.
18246 objc-message-expression:
18247 [ objc-message-receiver objc-message-args ]
18249 Returns a representation of an Objective-C message. */
18252 cp_parser_objc_message_expression (cp_parser* parser)
18254 tree receiver, messageargs;
18256 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18257 receiver = cp_parser_objc_message_receiver (parser);
18258 messageargs = cp_parser_objc_message_args (parser);
18259 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18261 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18264 /* Parse an objc-message-receiver.
18266 objc-message-receiver:
18268 simple-type-specifier
18270 Returns a representation of the type or expression. */
18273 cp_parser_objc_message_receiver (cp_parser* parser)
18277 /* An Objective-C message receiver may be either (1) a type
18278 or (2) an expression. */
18279 cp_parser_parse_tentatively (parser);
18280 rcv = cp_parser_expression (parser, false);
18282 if (cp_parser_parse_definitely (parser))
18285 rcv = cp_parser_simple_type_specifier (parser,
18286 /*decl_specs=*/NULL,
18287 CP_PARSER_FLAGS_NONE);
18289 return objc_get_class_reference (rcv);
18292 /* Parse the arguments and selectors comprising an Objective-C message.
18297 objc-selector-args , objc-comma-args
18299 objc-selector-args:
18300 objc-selector [opt] : assignment-expression
18301 objc-selector-args objc-selector [opt] : assignment-expression
18304 assignment-expression
18305 objc-comma-args , assignment-expression
18307 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18308 selector arguments and TREE_VALUE containing a list of comma
18312 cp_parser_objc_message_args (cp_parser* parser)
18314 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18315 bool maybe_unary_selector_p = true;
18316 cp_token *token = cp_lexer_peek_token (parser->lexer);
18318 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18320 tree selector = NULL_TREE, arg;
18322 if (token->type != CPP_COLON)
18323 selector = cp_parser_objc_selector (parser);
18325 /* Detect if we have a unary selector. */
18326 if (maybe_unary_selector_p
18327 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18328 return build_tree_list (selector, NULL_TREE);
18330 maybe_unary_selector_p = false;
18331 cp_parser_require (parser, CPP_COLON, "`:'");
18332 arg = cp_parser_assignment_expression (parser, false);
18335 = chainon (sel_args,
18336 build_tree_list (selector, arg));
18338 token = cp_lexer_peek_token (parser->lexer);
18341 /* Handle non-selector arguments, if any. */
18342 while (token->type == CPP_COMMA)
18346 cp_lexer_consume_token (parser->lexer);
18347 arg = cp_parser_assignment_expression (parser, false);
18350 = chainon (addl_args,
18351 build_tree_list (NULL_TREE, arg));
18353 token = cp_lexer_peek_token (parser->lexer);
18356 return build_tree_list (sel_args, addl_args);
18359 /* Parse an Objective-C encode expression.
18361 objc-encode-expression:
18362 @encode objc-typename
18364 Returns an encoded representation of the type argument. */
18367 cp_parser_objc_encode_expression (cp_parser* parser)
18371 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18372 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18373 type = complete_type (cp_parser_type_id (parser));
18374 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18378 error ("%<@encode%> must specify a type as an argument");
18379 return error_mark_node;
18382 return objc_build_encode_expr (type);
18385 /* Parse an Objective-C @defs expression. */
18388 cp_parser_objc_defs_expression (cp_parser *parser)
18392 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18393 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18394 name = cp_parser_identifier (parser);
18395 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18397 return objc_get_class_ivars (name);
18400 /* Parse an Objective-C protocol expression.
18402 objc-protocol-expression:
18403 @protocol ( identifier )
18405 Returns a representation of the protocol expression. */
18408 cp_parser_objc_protocol_expression (cp_parser* parser)
18412 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18413 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18414 proto = cp_parser_identifier (parser);
18415 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18417 return objc_build_protocol_expr (proto);
18420 /* Parse an Objective-C selector expression.
18422 objc-selector-expression:
18423 @selector ( objc-method-signature )
18425 objc-method-signature:
18431 objc-selector-seq objc-selector :
18433 Returns a representation of the method selector. */
18436 cp_parser_objc_selector_expression (cp_parser* parser)
18438 tree sel_seq = NULL_TREE;
18439 bool maybe_unary_selector_p = true;
18442 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18443 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18444 token = cp_lexer_peek_token (parser->lexer);
18446 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18447 || token->type == CPP_SCOPE)
18449 tree selector = NULL_TREE;
18451 if (token->type != CPP_COLON
18452 || token->type == CPP_SCOPE)
18453 selector = cp_parser_objc_selector (parser);
18455 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18456 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18458 /* Detect if we have a unary selector. */
18459 if (maybe_unary_selector_p)
18461 sel_seq = selector;
18462 goto finish_selector;
18466 cp_parser_error (parser, "expected %<:%>");
18469 maybe_unary_selector_p = false;
18470 token = cp_lexer_consume_token (parser->lexer);
18472 if (token->type == CPP_SCOPE)
18475 = chainon (sel_seq,
18476 build_tree_list (selector, NULL_TREE));
18478 = chainon (sel_seq,
18479 build_tree_list (NULL_TREE, NULL_TREE));
18483 = chainon (sel_seq,
18484 build_tree_list (selector, NULL_TREE));
18486 token = cp_lexer_peek_token (parser->lexer);
18490 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18492 return objc_build_selector_expr (sel_seq);
18495 /* Parse a list of identifiers.
18497 objc-identifier-list:
18499 objc-identifier-list , identifier
18501 Returns a TREE_LIST of identifier nodes. */
18504 cp_parser_objc_identifier_list (cp_parser* parser)
18506 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18507 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18509 while (sep->type == CPP_COMMA)
18511 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18512 list = chainon (list,
18513 build_tree_list (NULL_TREE,
18514 cp_parser_identifier (parser)));
18515 sep = cp_lexer_peek_token (parser->lexer);
18521 /* Parse an Objective-C alias declaration.
18523 objc-alias-declaration:
18524 @compatibility_alias identifier identifier ;
18526 This function registers the alias mapping with the Objective-C front end.
18527 It returns nothing. */
18530 cp_parser_objc_alias_declaration (cp_parser* parser)
18534 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18535 alias = cp_parser_identifier (parser);
18536 orig = cp_parser_identifier (parser);
18537 objc_declare_alias (alias, orig);
18538 cp_parser_consume_semicolon_at_end_of_statement (parser);
18541 /* Parse an Objective-C class forward-declaration.
18543 objc-class-declaration:
18544 @class objc-identifier-list ;
18546 The function registers the forward declarations with the Objective-C
18547 front end. It returns nothing. */
18550 cp_parser_objc_class_declaration (cp_parser* parser)
18552 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18553 objc_declare_class (cp_parser_objc_identifier_list (parser));
18554 cp_parser_consume_semicolon_at_end_of_statement (parser);
18557 /* Parse a list of Objective-C protocol references.
18559 objc-protocol-refs-opt:
18560 objc-protocol-refs [opt]
18562 objc-protocol-refs:
18563 < objc-identifier-list >
18565 Returns a TREE_LIST of identifiers, if any. */
18568 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18570 tree protorefs = NULL_TREE;
18572 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18574 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18575 protorefs = cp_parser_objc_identifier_list (parser);
18576 cp_parser_require (parser, CPP_GREATER, "`>'");
18582 /* Parse a Objective-C visibility specification. */
18585 cp_parser_objc_visibility_spec (cp_parser* parser)
18587 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18589 switch (vis->keyword)
18591 case RID_AT_PRIVATE:
18592 objc_set_visibility (2);
18594 case RID_AT_PROTECTED:
18595 objc_set_visibility (0);
18597 case RID_AT_PUBLIC:
18598 objc_set_visibility (1);
18604 /* Eat '@private'/'@protected'/'@public'. */
18605 cp_lexer_consume_token (parser->lexer);
18608 /* Parse an Objective-C method type. */
18611 cp_parser_objc_method_type (cp_parser* parser)
18613 objc_set_method_type
18614 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18619 /* Parse an Objective-C protocol qualifier. */
18622 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18624 tree quals = NULL_TREE, node;
18625 cp_token *token = cp_lexer_peek_token (parser->lexer);
18627 node = token->u.value;
18629 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18630 && (node == ridpointers [(int) RID_IN]
18631 || node == ridpointers [(int) RID_OUT]
18632 || node == ridpointers [(int) RID_INOUT]
18633 || node == ridpointers [(int) RID_BYCOPY]
18634 || node == ridpointers [(int) RID_BYREF]
18635 || node == ridpointers [(int) RID_ONEWAY]))
18637 quals = tree_cons (NULL_TREE, node, quals);
18638 cp_lexer_consume_token (parser->lexer);
18639 token = cp_lexer_peek_token (parser->lexer);
18640 node = token->u.value;
18646 /* Parse an Objective-C typename. */
18649 cp_parser_objc_typename (cp_parser* parser)
18651 tree typename = NULL_TREE;
18653 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18655 tree proto_quals, cp_type = NULL_TREE;
18657 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18658 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18660 /* An ObjC type name may consist of just protocol qualifiers, in which
18661 case the type shall default to 'id'. */
18662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18663 cp_type = cp_parser_type_id (parser);
18665 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18666 typename = build_tree_list (proto_quals, cp_type);
18672 /* Check to see if TYPE refers to an Objective-C selector name. */
18675 cp_parser_objc_selector_p (enum cpp_ttype type)
18677 return (type == CPP_NAME || type == CPP_KEYWORD
18678 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18679 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18680 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18681 || type == CPP_XOR || type == CPP_XOR_EQ);
18684 /* Parse an Objective-C selector. */
18687 cp_parser_objc_selector (cp_parser* parser)
18689 cp_token *token = cp_lexer_consume_token (parser->lexer);
18691 if (!cp_parser_objc_selector_p (token->type))
18693 error ("invalid Objective-C++ selector name");
18694 return error_mark_node;
18697 /* C++ operator names are allowed to appear in ObjC selectors. */
18698 switch (token->type)
18700 case CPP_AND_AND: return get_identifier ("and");
18701 case CPP_AND_EQ: return get_identifier ("and_eq");
18702 case CPP_AND: return get_identifier ("bitand");
18703 case CPP_OR: return get_identifier ("bitor");
18704 case CPP_COMPL: return get_identifier ("compl");
18705 case CPP_NOT: return get_identifier ("not");
18706 case CPP_NOT_EQ: return get_identifier ("not_eq");
18707 case CPP_OR_OR: return get_identifier ("or");
18708 case CPP_OR_EQ: return get_identifier ("or_eq");
18709 case CPP_XOR: return get_identifier ("xor");
18710 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18711 default: return token->u.value;
18715 /* Parse an Objective-C params list. */
18718 cp_parser_objc_method_keyword_params (cp_parser* parser)
18720 tree params = NULL_TREE;
18721 bool maybe_unary_selector_p = true;
18722 cp_token *token = cp_lexer_peek_token (parser->lexer);
18724 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18726 tree selector = NULL_TREE, typename, identifier;
18728 if (token->type != CPP_COLON)
18729 selector = cp_parser_objc_selector (parser);
18731 /* Detect if we have a unary selector. */
18732 if (maybe_unary_selector_p
18733 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18736 maybe_unary_selector_p = false;
18737 cp_parser_require (parser, CPP_COLON, "`:'");
18738 typename = cp_parser_objc_typename (parser);
18739 identifier = cp_parser_identifier (parser);
18743 objc_build_keyword_decl (selector,
18747 token = cp_lexer_peek_token (parser->lexer);
18753 /* Parse the non-keyword Objective-C params. */
18756 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18758 tree params = make_node (TREE_LIST);
18759 cp_token *token = cp_lexer_peek_token (parser->lexer);
18760 *ellipsisp = false; /* Initially, assume no ellipsis. */
18762 while (token->type == CPP_COMMA)
18764 cp_parameter_declarator *parmdecl;
18767 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18768 token = cp_lexer_peek_token (parser->lexer);
18770 if (token->type == CPP_ELLIPSIS)
18772 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18777 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18778 parm = grokdeclarator (parmdecl->declarator,
18779 &parmdecl->decl_specifiers,
18780 PARM, /*initialized=*/0,
18781 /*attrlist=*/NULL);
18783 chainon (params, build_tree_list (NULL_TREE, parm));
18784 token = cp_lexer_peek_token (parser->lexer);
18790 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18793 cp_parser_objc_interstitial_code (cp_parser* parser)
18795 cp_token *token = cp_lexer_peek_token (parser->lexer);
18797 /* If the next token is `extern' and the following token is a string
18798 literal, then we have a linkage specification. */
18799 if (token->keyword == RID_EXTERN
18800 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18801 cp_parser_linkage_specification (parser);
18802 /* Handle #pragma, if any. */
18803 else if (token->type == CPP_PRAGMA)
18804 cp_parser_pragma (parser, pragma_external);
18805 /* Allow stray semicolons. */
18806 else if (token->type == CPP_SEMICOLON)
18807 cp_lexer_consume_token (parser->lexer);
18808 /* Finally, try to parse a block-declaration, or a function-definition. */
18810 cp_parser_block_declaration (parser, /*statement_p=*/false);
18813 /* Parse a method signature. */
18816 cp_parser_objc_method_signature (cp_parser* parser)
18818 tree rettype, kwdparms, optparms;
18819 bool ellipsis = false;
18821 cp_parser_objc_method_type (parser);
18822 rettype = cp_parser_objc_typename (parser);
18823 kwdparms = cp_parser_objc_method_keyword_params (parser);
18824 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18826 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18829 /* Pars an Objective-C method prototype list. */
18832 cp_parser_objc_method_prototype_list (cp_parser* parser)
18834 cp_token *token = cp_lexer_peek_token (parser->lexer);
18836 while (token->keyword != RID_AT_END)
18838 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18840 objc_add_method_declaration
18841 (cp_parser_objc_method_signature (parser));
18842 cp_parser_consume_semicolon_at_end_of_statement (parser);
18845 /* Allow for interspersed non-ObjC++ code. */
18846 cp_parser_objc_interstitial_code (parser);
18848 token = cp_lexer_peek_token (parser->lexer);
18851 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18852 objc_finish_interface ();
18855 /* Parse an Objective-C method definition list. */
18858 cp_parser_objc_method_definition_list (cp_parser* parser)
18860 cp_token *token = cp_lexer_peek_token (parser->lexer);
18862 while (token->keyword != RID_AT_END)
18866 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18868 push_deferring_access_checks (dk_deferred);
18869 objc_start_method_definition
18870 (cp_parser_objc_method_signature (parser));
18872 /* For historical reasons, we accept an optional semicolon. */
18873 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18874 cp_lexer_consume_token (parser->lexer);
18876 perform_deferred_access_checks ();
18877 stop_deferring_access_checks ();
18878 meth = cp_parser_function_definition_after_declarator (parser,
18880 pop_deferring_access_checks ();
18881 objc_finish_method_definition (meth);
18884 /* Allow for interspersed non-ObjC++ code. */
18885 cp_parser_objc_interstitial_code (parser);
18887 token = cp_lexer_peek_token (parser->lexer);
18890 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18891 objc_finish_implementation ();
18894 /* Parse Objective-C ivars. */
18897 cp_parser_objc_class_ivars (cp_parser* parser)
18899 cp_token *token = cp_lexer_peek_token (parser->lexer);
18901 if (token->type != CPP_OPEN_BRACE)
18902 return; /* No ivars specified. */
18904 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18905 token = cp_lexer_peek_token (parser->lexer);
18907 while (token->type != CPP_CLOSE_BRACE)
18909 cp_decl_specifier_seq declspecs;
18910 int decl_class_or_enum_p;
18911 tree prefix_attributes;
18913 cp_parser_objc_visibility_spec (parser);
18915 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18918 cp_parser_decl_specifier_seq (parser,
18919 CP_PARSER_FLAGS_OPTIONAL,
18921 &decl_class_or_enum_p);
18922 prefix_attributes = declspecs.attributes;
18923 declspecs.attributes = NULL_TREE;
18925 /* Keep going until we hit the `;' at the end of the
18927 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18929 tree width = NULL_TREE, attributes, first_attribute, decl;
18930 cp_declarator *declarator = NULL;
18931 int ctor_dtor_or_conv_p;
18933 /* Check for a (possibly unnamed) bitfield declaration. */
18934 token = cp_lexer_peek_token (parser->lexer);
18935 if (token->type == CPP_COLON)
18938 if (token->type == CPP_NAME
18939 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18942 /* Get the name of the bitfield. */
18943 declarator = make_id_declarator (NULL_TREE,
18944 cp_parser_identifier (parser),
18948 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18949 /* Get the width of the bitfield. */
18951 = cp_parser_constant_expression (parser,
18952 /*allow_non_constant=*/false,
18957 /* Parse the declarator. */
18959 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18960 &ctor_dtor_or_conv_p,
18961 /*parenthesized_p=*/NULL,
18962 /*member_p=*/false);
18965 /* Look for attributes that apply to the ivar. */
18966 attributes = cp_parser_attributes_opt (parser);
18967 /* Remember which attributes are prefix attributes and
18969 first_attribute = attributes;
18970 /* Combine the attributes. */
18971 attributes = chainon (prefix_attributes, attributes);
18975 /* Create the bitfield declaration. */
18976 decl = grokbitfield (declarator, &declspecs, width);
18977 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18980 decl = grokfield (declarator, &declspecs,
18981 NULL_TREE, /*init_const_expr_p=*/false,
18982 NULL_TREE, attributes);
18984 /* Add the instance variable. */
18985 objc_add_instance_variable (decl);
18987 /* Reset PREFIX_ATTRIBUTES. */
18988 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18989 attributes = TREE_CHAIN (attributes);
18991 TREE_CHAIN (attributes) = NULL_TREE;
18993 token = cp_lexer_peek_token (parser->lexer);
18995 if (token->type == CPP_COMMA)
18997 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19003 cp_parser_consume_semicolon_at_end_of_statement (parser);
19004 token = cp_lexer_peek_token (parser->lexer);
19007 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19008 /* For historical reasons, we accept an optional semicolon. */
19009 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19010 cp_lexer_consume_token (parser->lexer);
19013 /* Parse an Objective-C protocol declaration. */
19016 cp_parser_objc_protocol_declaration (cp_parser* parser)
19018 tree proto, protorefs;
19021 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19022 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19024 error ("identifier expected after %<@protocol%>");
19028 /* See if we have a forward declaration or a definition. */
19029 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19031 /* Try a forward declaration first. */
19032 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19034 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19036 cp_parser_consume_semicolon_at_end_of_statement (parser);
19039 /* Ok, we got a full-fledged definition (or at least should). */
19042 proto = cp_parser_identifier (parser);
19043 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19044 objc_start_protocol (proto, protorefs);
19045 cp_parser_objc_method_prototype_list (parser);
19049 /* Parse an Objective-C superclass or category. */
19052 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19055 cp_token *next = cp_lexer_peek_token (parser->lexer);
19057 *super = *categ = NULL_TREE;
19058 if (next->type == CPP_COLON)
19060 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19061 *super = cp_parser_identifier (parser);
19063 else if (next->type == CPP_OPEN_PAREN)
19065 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19066 *categ = cp_parser_identifier (parser);
19067 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19071 /* Parse an Objective-C class interface. */
19074 cp_parser_objc_class_interface (cp_parser* parser)
19076 tree name, super, categ, protos;
19078 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19079 name = cp_parser_identifier (parser);
19080 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19081 protos = cp_parser_objc_protocol_refs_opt (parser);
19083 /* We have either a class or a category on our hands. */
19085 objc_start_category_interface (name, categ, protos);
19088 objc_start_class_interface (name, super, protos);
19089 /* Handle instance variable declarations, if any. */
19090 cp_parser_objc_class_ivars (parser);
19091 objc_continue_interface ();
19094 cp_parser_objc_method_prototype_list (parser);
19097 /* Parse an Objective-C class implementation. */
19100 cp_parser_objc_class_implementation (cp_parser* parser)
19102 tree name, super, categ;
19104 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19105 name = cp_parser_identifier (parser);
19106 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19108 /* We have either a class or a category on our hands. */
19110 objc_start_category_implementation (name, categ);
19113 objc_start_class_implementation (name, super);
19114 /* Handle instance variable declarations, if any. */
19115 cp_parser_objc_class_ivars (parser);
19116 objc_continue_implementation ();
19119 cp_parser_objc_method_definition_list (parser);
19122 /* Consume the @end token and finish off the implementation. */
19125 cp_parser_objc_end_implementation (cp_parser* parser)
19127 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19128 objc_finish_implementation ();
19131 /* Parse an Objective-C declaration. */
19134 cp_parser_objc_declaration (cp_parser* parser)
19136 /* Try to figure out what kind of declaration is present. */
19137 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19139 switch (kwd->keyword)
19142 cp_parser_objc_alias_declaration (parser);
19145 cp_parser_objc_class_declaration (parser);
19147 case RID_AT_PROTOCOL:
19148 cp_parser_objc_protocol_declaration (parser);
19150 case RID_AT_INTERFACE:
19151 cp_parser_objc_class_interface (parser);
19153 case RID_AT_IMPLEMENTATION:
19154 cp_parser_objc_class_implementation (parser);
19157 cp_parser_objc_end_implementation (parser);
19160 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19161 cp_parser_skip_to_end_of_block_or_statement (parser);
19165 /* Parse an Objective-C try-catch-finally statement.
19167 objc-try-catch-finally-stmt:
19168 @try compound-statement objc-catch-clause-seq [opt]
19169 objc-finally-clause [opt]
19171 objc-catch-clause-seq:
19172 objc-catch-clause objc-catch-clause-seq [opt]
19175 @catch ( exception-declaration ) compound-statement
19177 objc-finally-clause
19178 @finally compound-statement
19180 Returns NULL_TREE. */
19183 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19184 location_t location;
19187 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19188 location = cp_lexer_peek_token (parser->lexer)->location;
19189 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19190 node, lest it get absorbed into the surrounding block. */
19191 stmt = push_stmt_list ();
19192 cp_parser_compound_statement (parser, NULL, false);
19193 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19195 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19197 cp_parameter_declarator *parmdecl;
19200 cp_lexer_consume_token (parser->lexer);
19201 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19202 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19203 parm = grokdeclarator (parmdecl->declarator,
19204 &parmdecl->decl_specifiers,
19205 PARM, /*initialized=*/0,
19206 /*attrlist=*/NULL);
19207 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19208 objc_begin_catch_clause (parm);
19209 cp_parser_compound_statement (parser, NULL, false);
19210 objc_finish_catch_clause ();
19213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19215 cp_lexer_consume_token (parser->lexer);
19216 location = cp_lexer_peek_token (parser->lexer)->location;
19217 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19218 node, lest it get absorbed into the surrounding block. */
19219 stmt = push_stmt_list ();
19220 cp_parser_compound_statement (parser, NULL, false);
19221 objc_build_finally_clause (location, pop_stmt_list (stmt));
19224 return objc_finish_try_stmt ();
19227 /* Parse an Objective-C synchronized statement.
19229 objc-synchronized-stmt:
19230 @synchronized ( expression ) compound-statement
19232 Returns NULL_TREE. */
19235 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19236 location_t location;
19239 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19241 location = cp_lexer_peek_token (parser->lexer)->location;
19242 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19243 lock = cp_parser_expression (parser, false);
19244 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19246 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19247 node, lest it get absorbed into the surrounding block. */
19248 stmt = push_stmt_list ();
19249 cp_parser_compound_statement (parser, NULL, false);
19251 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19254 /* Parse an Objective-C throw statement.
19257 @throw assignment-expression [opt] ;
19259 Returns a constructed '@throw' statement. */
19262 cp_parser_objc_throw_statement (cp_parser *parser) {
19263 tree expr = NULL_TREE;
19265 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19268 expr = cp_parser_assignment_expression (parser, false);
19270 cp_parser_consume_semicolon_at_end_of_statement (parser);
19272 return objc_build_throw_stmt (expr);
19275 /* Parse an Objective-C statement. */
19278 cp_parser_objc_statement (cp_parser * parser) {
19279 /* Try to figure out what kind of declaration is present. */
19280 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19282 switch (kwd->keyword)
19285 return cp_parser_objc_try_catch_finally_statement (parser);
19286 case RID_AT_SYNCHRONIZED:
19287 return cp_parser_objc_synchronized_statement (parser);
19289 return cp_parser_objc_throw_statement (parser);
19291 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19292 cp_parser_skip_to_end_of_block_or_statement (parser);
19295 return error_mark_node;
19298 /* OpenMP 2.5 parsing routines. */
19300 /* Returns name of the next clause.
19301 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19302 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19303 returned and the token is consumed. */
19305 static pragma_omp_clause
19306 cp_parser_omp_clause_name (cp_parser *parser)
19308 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19310 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19311 result = PRAGMA_OMP_CLAUSE_IF;
19312 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19313 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19314 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19315 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19316 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19318 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19319 const char *p = IDENTIFIER_POINTER (id);
19324 if (!strcmp ("copyin", p))
19325 result = PRAGMA_OMP_CLAUSE_COPYIN;
19326 else if (!strcmp ("copyprivate", p))
19327 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19330 if (!strcmp ("firstprivate", p))
19331 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19334 if (!strcmp ("lastprivate", p))
19335 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19338 if (!strcmp ("nowait", p))
19339 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19340 else if (!strcmp ("num_threads", p))
19341 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19344 if (!strcmp ("ordered", p))
19345 result = PRAGMA_OMP_CLAUSE_ORDERED;
19348 if (!strcmp ("reduction", p))
19349 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19352 if (!strcmp ("schedule", p))
19353 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19354 else if (!strcmp ("shared", p))
19355 result = PRAGMA_OMP_CLAUSE_SHARED;
19360 if (result != PRAGMA_OMP_CLAUSE_NONE)
19361 cp_lexer_consume_token (parser->lexer);
19366 /* Validate that a clause of the given type does not already exist. */
19369 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19373 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19374 if (OMP_CLAUSE_CODE (c) == code)
19376 error ("too many %qs clauses", name);
19384 variable-list , identifier
19386 In addition, we match a closing parenthesis. An opening parenthesis
19387 will have been consumed by the caller.
19389 If KIND is nonzero, create the appropriate node and install the decl
19390 in OMP_CLAUSE_DECL and add the node to the head of the list.
19392 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19393 return the list created. */
19396 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19403 name = cp_parser_id_expression (parser, /*template_p=*/false,
19404 /*check_dependency_p=*/true,
19405 /*template_p=*/NULL,
19406 /*declarator_p=*/false,
19407 /*optional_p=*/false);
19408 if (name == error_mark_node)
19411 decl = cp_parser_lookup_name_simple (parser, name);
19412 if (decl == error_mark_node)
19413 cp_parser_name_lookup_error (parser, name, decl, NULL);
19414 else if (kind != 0)
19416 tree u = build_omp_clause (kind);
19417 OMP_CLAUSE_DECL (u) = decl;
19418 OMP_CLAUSE_CHAIN (u) = list;
19422 list = tree_cons (decl, NULL_TREE, list);
19425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19427 cp_lexer_consume_token (parser->lexer);
19430 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19434 /* Try to resync to an unnested comma. Copied from
19435 cp_parser_parenthesized_expression_list. */
19437 ending = cp_parser_skip_to_closing_parenthesis (parser,
19438 /*recovering=*/true,
19440 /*consume_paren=*/true);
19448 /* Similarly, but expect leading and trailing parenthesis. This is a very
19449 common case for omp clauses. */
19452 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19454 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19455 return cp_parser_omp_var_list_no_open (parser, kind, list);
19460 default ( shared | none ) */
19463 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19465 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19468 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19470 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19472 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19473 const char *p = IDENTIFIER_POINTER (id);
19478 if (strcmp ("none", p) != 0)
19480 kind = OMP_CLAUSE_DEFAULT_NONE;
19484 if (strcmp ("shared", p) != 0)
19486 kind = OMP_CLAUSE_DEFAULT_SHARED;
19493 cp_lexer_consume_token (parser->lexer);
19498 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19501 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19502 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19503 /*or_comma=*/false,
19504 /*consume_paren=*/true);
19506 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19509 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19510 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19511 OMP_CLAUSE_CHAIN (c) = list;
19512 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19518 if ( expression ) */
19521 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19525 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19528 t = cp_parser_condition (parser);
19530 if (t == error_mark_node
19531 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19532 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19533 /*or_comma=*/false,
19534 /*consume_paren=*/true);
19536 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19538 c = build_omp_clause (OMP_CLAUSE_IF);
19539 OMP_CLAUSE_IF_EXPR (c) = t;
19540 OMP_CLAUSE_CHAIN (c) = list;
19549 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19553 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19555 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19556 OMP_CLAUSE_CHAIN (c) = list;
19561 num_threads ( expression ) */
19564 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19568 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19571 t = cp_parser_expression (parser, false);
19573 if (t == error_mark_node
19574 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19575 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19576 /*or_comma=*/false,
19577 /*consume_paren=*/true);
19579 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19581 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19582 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19583 OMP_CLAUSE_CHAIN (c) = list;
19592 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19596 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19598 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19599 OMP_CLAUSE_CHAIN (c) = list;
19604 reduction ( reduction-operator : variable-list )
19606 reduction-operator:
19607 One of: + * - & ^ | && || */
19610 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19612 enum tree_code code;
19615 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19618 switch (cp_lexer_peek_token (parser->lexer)->type)
19630 code = BIT_AND_EXPR;
19633 code = BIT_XOR_EXPR;
19636 code = BIT_IOR_EXPR;
19639 code = TRUTH_ANDIF_EXPR;
19642 code = TRUTH_ORIF_EXPR;
19645 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19647 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19648 /*or_comma=*/false,
19649 /*consume_paren=*/true);
19652 cp_lexer_consume_token (parser->lexer);
19654 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19657 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19658 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19659 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19665 schedule ( schedule-kind )
19666 schedule ( schedule-kind , expression )
19669 static | dynamic | guided | runtime */
19672 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19676 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19679 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19681 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19683 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19684 const char *p = IDENTIFIER_POINTER (id);
19689 if (strcmp ("dynamic", p) != 0)
19691 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19695 if (strcmp ("guided", p) != 0)
19697 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19701 if (strcmp ("runtime", p) != 0)
19703 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19710 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19711 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19714 cp_lexer_consume_token (parser->lexer);
19716 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19718 cp_lexer_consume_token (parser->lexer);
19720 t = cp_parser_assignment_expression (parser, false);
19722 if (t == error_mark_node)
19724 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19725 error ("schedule %<runtime%> does not take "
19726 "a %<chunk_size%> parameter");
19728 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19730 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19733 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19736 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19737 OMP_CLAUSE_CHAIN (c) = list;
19741 cp_parser_error (parser, "invalid schedule kind");
19743 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19744 /*or_comma=*/false,
19745 /*consume_paren=*/true);
19749 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19750 is a bitmask in MASK. Return the list of clauses found; the result
19751 of clause default goes in *pdefault. */
19754 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19755 const char *where, cp_token *pragma_tok)
19757 tree clauses = NULL;
19760 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19762 pragma_omp_clause c_kind;
19763 const char *c_name;
19764 tree prev = clauses;
19766 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19767 cp_lexer_consume_token (parser->lexer);
19769 c_kind = cp_parser_omp_clause_name (parser);
19774 case PRAGMA_OMP_CLAUSE_COPYIN:
19775 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19778 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19779 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19781 c_name = "copyprivate";
19783 case PRAGMA_OMP_CLAUSE_DEFAULT:
19784 clauses = cp_parser_omp_clause_default (parser, clauses);
19785 c_name = "default";
19787 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19788 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19790 c_name = "firstprivate";
19792 case PRAGMA_OMP_CLAUSE_IF:
19793 clauses = cp_parser_omp_clause_if (parser, clauses);
19796 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19797 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19799 c_name = "lastprivate";
19801 case PRAGMA_OMP_CLAUSE_NOWAIT:
19802 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19805 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19806 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19807 c_name = "num_threads";
19809 case PRAGMA_OMP_CLAUSE_ORDERED:
19810 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19811 c_name = "ordered";
19813 case PRAGMA_OMP_CLAUSE_PRIVATE:
19814 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19816 c_name = "private";
19818 case PRAGMA_OMP_CLAUSE_REDUCTION:
19819 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19820 c_name = "reduction";
19822 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19823 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19824 c_name = "schedule";
19826 case PRAGMA_OMP_CLAUSE_SHARED:
19827 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19832 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19836 if (((mask >> c_kind) & 1) == 0)
19838 /* Remove the invalid clause(s) from the list to avoid
19839 confusing the rest of the compiler. */
19841 error ("%qs is not valid for %qs", c_name, where);
19845 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19846 return finish_omp_clauses (clauses);
19853 In practice, we're also interested in adding the statement to an
19854 outer node. So it is convenient if we work around the fact that
19855 cp_parser_statement calls add_stmt. */
19858 cp_parser_begin_omp_structured_block (cp_parser *parser)
19860 unsigned save = parser->in_statement;
19862 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19863 This preserves the "not within loop or switch" style error messages
19864 for nonsense cases like
19870 if (parser->in_statement)
19871 parser->in_statement = IN_OMP_BLOCK;
19877 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19879 parser->in_statement = save;
19883 cp_parser_omp_structured_block (cp_parser *parser)
19885 tree stmt = begin_omp_structured_block ();
19886 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19888 cp_parser_statement (parser, NULL_TREE, false, NULL);
19890 cp_parser_end_omp_structured_block (parser, save);
19891 return finish_omp_structured_block (stmt);
19895 # pragma omp atomic new-line
19899 x binop= expr | x++ | ++x | x-- | --x
19901 +, *, -, /, &, ^, |, <<, >>
19903 where x is an lvalue expression with scalar type. */
19906 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19909 enum tree_code code;
19911 cp_parser_require_pragma_eol (parser, pragma_tok);
19913 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19915 switch (TREE_CODE (lhs))
19920 case PREINCREMENT_EXPR:
19921 case POSTINCREMENT_EXPR:
19922 lhs = TREE_OPERAND (lhs, 0);
19924 rhs = integer_one_node;
19927 case PREDECREMENT_EXPR:
19928 case POSTDECREMENT_EXPR:
19929 lhs = TREE_OPERAND (lhs, 0);
19931 rhs = integer_one_node;
19935 switch (cp_lexer_peek_token (parser->lexer)->type)
19941 code = TRUNC_DIV_EXPR;
19949 case CPP_LSHIFT_EQ:
19950 code = LSHIFT_EXPR;
19952 case CPP_RSHIFT_EQ:
19953 code = RSHIFT_EXPR;
19956 code = BIT_AND_EXPR;
19959 code = BIT_IOR_EXPR;
19962 code = BIT_XOR_EXPR;
19965 cp_parser_error (parser,
19966 "invalid operator for %<#pragma omp atomic%>");
19969 cp_lexer_consume_token (parser->lexer);
19971 rhs = cp_parser_expression (parser, false);
19972 if (rhs == error_mark_node)
19976 finish_omp_atomic (code, lhs, rhs);
19977 cp_parser_consume_semicolon_at_end_of_statement (parser);
19981 cp_parser_skip_to_end_of_block_or_statement (parser);
19986 # pragma omp barrier new-line */
19989 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19991 cp_parser_require_pragma_eol (parser, pragma_tok);
19992 finish_omp_barrier ();
19996 # pragma omp critical [(name)] new-line
19997 structured-block */
20000 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20002 tree stmt, name = NULL;
20004 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20006 cp_lexer_consume_token (parser->lexer);
20008 name = cp_parser_identifier (parser);
20010 if (name == error_mark_node
20011 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20012 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20013 /*or_comma=*/false,
20014 /*consume_paren=*/true);
20015 if (name == error_mark_node)
20018 cp_parser_require_pragma_eol (parser, pragma_tok);
20020 stmt = cp_parser_omp_structured_block (parser);
20021 return c_finish_omp_critical (stmt, name);
20025 # pragma omp flush flush-vars[opt] new-line
20028 ( variable-list ) */
20031 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20033 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20034 (void) cp_parser_omp_var_list (parser, 0, NULL);
20035 cp_parser_require_pragma_eol (parser, pragma_tok);
20037 finish_omp_flush ();
20040 /* Parse the restricted form of the for statment allowed by OpenMP. */
20043 cp_parser_omp_for_loop (cp_parser *parser)
20045 tree init, cond, incr, body, decl, pre_body;
20048 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20050 cp_parser_error (parser, "for statement expected");
20053 loc = cp_lexer_consume_token (parser->lexer)->location;
20054 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20057 init = decl = NULL;
20058 pre_body = push_stmt_list ();
20059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20061 cp_decl_specifier_seq type_specifiers;
20063 /* First, try to parse as an initialized declaration. See
20064 cp_parser_condition, from whence the bulk of this is copied. */
20066 cp_parser_parse_tentatively (parser);
20067 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20069 if (!cp_parser_error_occurred (parser))
20071 tree asm_specification, attributes;
20072 cp_declarator *declarator;
20074 declarator = cp_parser_declarator (parser,
20075 CP_PARSER_DECLARATOR_NAMED,
20076 /*ctor_dtor_or_conv_p=*/NULL,
20077 /*parenthesized_p=*/NULL,
20078 /*member_p=*/false);
20079 attributes = cp_parser_attributes_opt (parser);
20080 asm_specification = cp_parser_asm_specification_opt (parser);
20082 cp_parser_require (parser, CPP_EQ, "`='");
20083 if (cp_parser_parse_definitely (parser))
20087 decl = start_decl (declarator, &type_specifiers,
20088 /*initialized_p=*/false, attributes,
20089 /*prefix_attributes=*/NULL_TREE,
20092 init = cp_parser_assignment_expression (parser, false);
20094 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20095 init = error_mark_node;
20097 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20098 asm_specification, LOOKUP_ONLYCONVERTING);
20101 pop_scope (pushed_scope);
20105 cp_parser_abort_tentative_parse (parser);
20107 /* If parsing as an initialized declaration failed, try again as
20108 a simple expression. */
20110 init = cp_parser_expression (parser, false);
20112 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20113 pre_body = pop_stmt_list (pre_body);
20116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20117 cond = cp_parser_condition (parser);
20118 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20122 incr = cp_parser_expression (parser, false);
20124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20125 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20126 /*or_comma=*/false,
20127 /*consume_paren=*/true);
20129 /* Note that we saved the original contents of this flag when we entered
20130 the structured block, and so we don't need to re-save it here. */
20131 parser->in_statement = IN_OMP_FOR;
20133 /* Note that the grammar doesn't call for a structured block here,
20134 though the loop as a whole is a structured block. */
20135 body = push_stmt_list ();
20136 cp_parser_statement (parser, NULL_TREE, false, NULL);
20137 body = pop_stmt_list (body);
20139 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20143 #pragma omp for for-clause[optseq] new-line
20146 #define OMP_FOR_CLAUSE_MASK \
20147 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20148 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20149 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20150 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20151 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20152 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20153 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20156 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20158 tree clauses, sb, ret;
20161 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20162 "#pragma omp for", pragma_tok);
20164 sb = begin_omp_structured_block ();
20165 save = cp_parser_begin_omp_structured_block (parser);
20167 ret = cp_parser_omp_for_loop (parser);
20169 OMP_FOR_CLAUSES (ret) = clauses;
20171 cp_parser_end_omp_structured_block (parser, save);
20172 add_stmt (finish_omp_structured_block (sb));
20178 # pragma omp master new-line
20179 structured-block */
20182 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20184 cp_parser_require_pragma_eol (parser, pragma_tok);
20185 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20189 # pragma omp ordered new-line
20190 structured-block */
20193 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20195 cp_parser_require_pragma_eol (parser, pragma_tok);
20196 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20202 { section-sequence }
20205 section-directive[opt] structured-block
20206 section-sequence section-directive structured-block */
20209 cp_parser_omp_sections_scope (cp_parser *parser)
20211 tree stmt, substmt;
20212 bool error_suppress = false;
20215 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20218 stmt = push_stmt_list ();
20220 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20224 substmt = begin_omp_structured_block ();
20225 save = cp_parser_begin_omp_structured_block (parser);
20229 cp_parser_statement (parser, NULL_TREE, false, NULL);
20231 tok = cp_lexer_peek_token (parser->lexer);
20232 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20234 if (tok->type == CPP_CLOSE_BRACE)
20236 if (tok->type == CPP_EOF)
20240 cp_parser_end_omp_structured_block (parser, save);
20241 substmt = finish_omp_structured_block (substmt);
20242 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20243 add_stmt (substmt);
20248 tok = cp_lexer_peek_token (parser->lexer);
20249 if (tok->type == CPP_CLOSE_BRACE)
20251 if (tok->type == CPP_EOF)
20254 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20256 cp_lexer_consume_token (parser->lexer);
20257 cp_parser_require_pragma_eol (parser, tok);
20258 error_suppress = false;
20260 else if (!error_suppress)
20262 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20263 error_suppress = true;
20266 substmt = cp_parser_omp_structured_block (parser);
20267 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20268 add_stmt (substmt);
20270 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20272 substmt = pop_stmt_list (stmt);
20274 stmt = make_node (OMP_SECTIONS);
20275 TREE_TYPE (stmt) = void_type_node;
20276 OMP_SECTIONS_BODY (stmt) = substmt;
20283 # pragma omp sections sections-clause[optseq] newline
20286 #define OMP_SECTIONS_CLAUSE_MASK \
20287 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20288 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20289 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20290 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20291 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20294 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20298 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20299 "#pragma omp sections", pragma_tok);
20301 ret = cp_parser_omp_sections_scope (parser);
20303 OMP_SECTIONS_CLAUSES (ret) = clauses;
20309 # pragma parallel parallel-clause new-line
20310 # pragma parallel for parallel-for-clause new-line
20311 # pragma parallel sections parallel-sections-clause new-line */
20313 #define OMP_PARALLEL_CLAUSE_MASK \
20314 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20315 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20316 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20317 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20318 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20319 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20320 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20321 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20324 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20326 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20327 const char *p_name = "#pragma omp parallel";
20328 tree stmt, clauses, par_clause, ws_clause, block;
20329 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20332 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20334 cp_lexer_consume_token (parser->lexer);
20335 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20336 p_name = "#pragma omp parallel for";
20337 mask |= OMP_FOR_CLAUSE_MASK;
20338 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20340 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20342 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20343 const char *p = IDENTIFIER_POINTER (id);
20344 if (strcmp (p, "sections") == 0)
20346 cp_lexer_consume_token (parser->lexer);
20347 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20348 p_name = "#pragma omp parallel sections";
20349 mask |= OMP_SECTIONS_CLAUSE_MASK;
20350 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20354 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20355 block = begin_omp_parallel ();
20356 save = cp_parser_begin_omp_structured_block (parser);
20360 case PRAGMA_OMP_PARALLEL:
20361 cp_parser_statement (parser, NULL_TREE, false, NULL);
20362 par_clause = clauses;
20365 case PRAGMA_OMP_PARALLEL_FOR:
20366 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20367 stmt = cp_parser_omp_for_loop (parser);
20369 OMP_FOR_CLAUSES (stmt) = ws_clause;
20372 case PRAGMA_OMP_PARALLEL_SECTIONS:
20373 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20374 stmt = cp_parser_omp_sections_scope (parser);
20376 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20380 gcc_unreachable ();
20383 cp_parser_end_omp_structured_block (parser, save);
20384 stmt = finish_omp_parallel (par_clause, block);
20385 if (p_kind != PRAGMA_OMP_PARALLEL)
20386 OMP_PARALLEL_COMBINED (stmt) = 1;
20391 # pragma omp single single-clause[optseq] new-line
20392 structured-block */
20394 #define OMP_SINGLE_CLAUSE_MASK \
20395 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20396 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20397 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20398 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20401 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20403 tree stmt = make_node (OMP_SINGLE);
20404 TREE_TYPE (stmt) = void_type_node;
20406 OMP_SINGLE_CLAUSES (stmt)
20407 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20408 "#pragma omp single", pragma_tok);
20409 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20411 return add_stmt (stmt);
20415 # pragma omp threadprivate (variable-list) */
20418 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20422 vars = cp_parser_omp_var_list (parser, 0, NULL);
20423 cp_parser_require_pragma_eol (parser, pragma_tok);
20425 finish_omp_threadprivate (vars);
20428 /* Main entry point to OpenMP statement pragmas. */
20431 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20435 switch (pragma_tok->pragma_kind)
20437 case PRAGMA_OMP_ATOMIC:
20438 cp_parser_omp_atomic (parser, pragma_tok);
20440 case PRAGMA_OMP_CRITICAL:
20441 stmt = cp_parser_omp_critical (parser, pragma_tok);
20443 case PRAGMA_OMP_FOR:
20444 stmt = cp_parser_omp_for (parser, pragma_tok);
20446 case PRAGMA_OMP_MASTER:
20447 stmt = cp_parser_omp_master (parser, pragma_tok);
20449 case PRAGMA_OMP_ORDERED:
20450 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20452 case PRAGMA_OMP_PARALLEL:
20453 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20455 case PRAGMA_OMP_SECTIONS:
20456 stmt = cp_parser_omp_sections (parser, pragma_tok);
20458 case PRAGMA_OMP_SINGLE:
20459 stmt = cp_parser_omp_single (parser, pragma_tok);
20462 gcc_unreachable ();
20466 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20471 static GTY (()) cp_parser *the_parser;
20474 /* Special handling for the first token or line in the file. The first
20475 thing in the file might be #pragma GCC pch_preprocess, which loads a
20476 PCH file, which is a GC collection point. So we need to handle this
20477 first pragma without benefit of an existing lexer structure.
20479 Always returns one token to the caller in *FIRST_TOKEN. This is
20480 either the true first token of the file, or the first token after
20481 the initial pragma. */
20484 cp_parser_initial_pragma (cp_token *first_token)
20488 cp_lexer_get_preprocessor_token (NULL, first_token);
20489 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20492 cp_lexer_get_preprocessor_token (NULL, first_token);
20493 if (first_token->type == CPP_STRING)
20495 name = first_token->u.value;
20497 cp_lexer_get_preprocessor_token (NULL, first_token);
20498 if (first_token->type != CPP_PRAGMA_EOL)
20499 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20502 error ("expected string literal");
20504 /* Skip to the end of the pragma. */
20505 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20506 cp_lexer_get_preprocessor_token (NULL, first_token);
20508 /* Now actually load the PCH file. */
20510 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20512 /* Read one more token to return to our caller. We have to do this
20513 after reading the PCH file in, since its pointers have to be
20515 cp_lexer_get_preprocessor_token (NULL, first_token);
20518 /* Normal parsing of a pragma token. Here we can (and must) use the
20522 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20524 cp_token *pragma_tok;
20527 pragma_tok = cp_lexer_consume_token (parser->lexer);
20528 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20529 parser->lexer->in_pragma = true;
20531 id = pragma_tok->pragma_kind;
20534 case PRAGMA_GCC_PCH_PREPROCESS:
20535 error ("%<#pragma GCC pch_preprocess%> must be first");
20538 case PRAGMA_OMP_BARRIER:
20541 case pragma_compound:
20542 cp_parser_omp_barrier (parser, pragma_tok);
20545 error ("%<#pragma omp barrier%> may only be "
20546 "used in compound statements");
20553 case PRAGMA_OMP_FLUSH:
20556 case pragma_compound:
20557 cp_parser_omp_flush (parser, pragma_tok);
20560 error ("%<#pragma omp flush%> may only be "
20561 "used in compound statements");
20568 case PRAGMA_OMP_THREADPRIVATE:
20569 cp_parser_omp_threadprivate (parser, pragma_tok);
20572 case PRAGMA_OMP_ATOMIC:
20573 case PRAGMA_OMP_CRITICAL:
20574 case PRAGMA_OMP_FOR:
20575 case PRAGMA_OMP_MASTER:
20576 case PRAGMA_OMP_ORDERED:
20577 case PRAGMA_OMP_PARALLEL:
20578 case PRAGMA_OMP_SECTIONS:
20579 case PRAGMA_OMP_SINGLE:
20580 if (context == pragma_external)
20582 cp_parser_omp_construct (parser, pragma_tok);
20585 case PRAGMA_OMP_SECTION:
20586 error ("%<#pragma omp section%> may only be used in "
20587 "%<#pragma omp sections%> construct");
20591 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20592 c_invoke_pragma_handler (id);
20596 cp_parser_error (parser, "expected declaration specifiers");
20600 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20604 /* The interface the pragma parsers have to the lexer. */
20607 pragma_lex (tree *value)
20610 enum cpp_ttype ret;
20612 tok = cp_lexer_peek_token (the_parser->lexer);
20615 *value = tok->u.value;
20617 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20619 else if (ret == CPP_STRING)
20620 *value = cp_parser_string_literal (the_parser, false, false);
20623 cp_lexer_consume_token (the_parser->lexer);
20624 if (ret == CPP_KEYWORD)
20632 /* External interface. */
20634 /* Parse one entire translation unit. */
20637 c_parse_file (void)
20639 bool error_occurred;
20640 static bool already_called = false;
20642 if (already_called)
20644 sorry ("inter-module optimizations not implemented for C++");
20647 already_called = true;
20649 the_parser = cp_parser_new ();
20650 push_deferring_access_checks (flag_access_control
20651 ? dk_no_deferred : dk_no_check);
20652 error_occurred = cp_parser_translation_unit (the_parser);
20656 #include "gt-cp-parser.h"