2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 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"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct GTY(()) tree_check {
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 GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
154 static cp_lexer *cp_lexer_new_main
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
160 static int cp_lexer_saving_tokens
162 static cp_token_position cp_lexer_token_position
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
180 static void cp_lexer_purge_token
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
186 static void cp_lexer_commit_tokens
188 static void cp_lexer_rollback_tokens
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Create a new main C++ lexer, the lexer that gets tokens from the
254 cp_lexer_new_main (void)
256 cp_token first_token;
263 /* It's possible that parsing the first pragma will load a PCH file,
264 which is a GC collection point. So we have to do that before
265 allocating any memory. */
266 cp_parser_initial_pragma (&first_token);
268 c_common_no_more_pch ();
270 /* Allocate the memory. */
271 lexer = GGC_CNEW (cp_lexer);
273 #ifdef ENABLE_CHECKING
274 /* Initially we are not debugging. */
275 lexer->debugging_p = false;
276 #endif /* ENABLE_CHECKING */
277 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
278 CP_SAVED_TOKEN_STACK);
280 /* Create the buffer. */
281 alloc = CP_LEXER_BUFFER_SIZE;
282 buffer = GGC_NEWVEC (cp_token, alloc);
284 /* Put the first token in the buffer. */
289 /* Get the remaining tokens from the preprocessor. */
290 while (pos->type != CPP_EOF)
297 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
298 pos = buffer + space;
300 cp_lexer_get_preprocessor_token (lexer, pos);
302 lexer->buffer = buffer;
303 lexer->buffer_length = alloc - space;
304 lexer->last_token = pos;
305 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
307 /* Subsequent preprocessor diagnostics should use compiler
308 diagnostic functions to get the compiler source location. */
311 gcc_assert (lexer->next_token->type != CPP_PURGED);
315 /* Create a new lexer whose token stream is primed with the tokens in
316 CACHE. When these tokens are exhausted, no new tokens will be read. */
319 cp_lexer_new_from_tokens (cp_token_cache *cache)
321 cp_token *first = cache->first;
322 cp_token *last = cache->last;
323 cp_lexer *lexer = GGC_CNEW (cp_lexer);
325 /* We do not own the buffer. */
326 lexer->buffer = NULL;
327 lexer->buffer_length = 0;
328 lexer->next_token = first == last ? &eof_token : first;
329 lexer->last_token = last;
331 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
332 CP_SAVED_TOKEN_STACK);
334 #ifdef ENABLE_CHECKING
335 /* Initially we are not debugging. */
336 lexer->debugging_p = false;
339 gcc_assert (lexer->next_token->type != CPP_PURGED);
343 /* Frees all resources associated with LEXER. */
346 cp_lexer_destroy (cp_lexer *lexer)
349 ggc_free (lexer->buffer);
350 VEC_free (cp_token_position, heap, lexer->saved_tokens);
354 /* Returns nonzero if debugging information should be output. */
356 #ifdef ENABLE_CHECKING
359 cp_lexer_debugging_p (cp_lexer *lexer)
361 return lexer->debugging_p;
364 #endif /* ENABLE_CHECKING */
366 static inline cp_token_position
367 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
369 gcc_assert (!previous_p || lexer->next_token != &eof_token);
371 return lexer->next_token - previous_p;
374 static inline cp_token *
375 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
380 /* nonzero if we are presently saving tokens. */
383 cp_lexer_saving_tokens (const cp_lexer* lexer)
385 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
388 /* Store the next token from the preprocessor in *TOKEN. Return true
389 if we reach EOF. If LEXER is NULL, assume we are handling an
390 initial #pragma pch_preprocess, and thus want the lexer to return
391 processed strings. */
394 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
396 static int is_extern_c = 0;
398 /* Get a new token from the preprocessor. */
400 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
401 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
402 token->keyword = RID_MAX;
403 token->pragma_kind = PRAGMA_NONE;
405 /* On some systems, some header files are surrounded by an
406 implicit extern "C" block. Set a flag in the token if it
407 comes from such a header. */
408 is_extern_c += pending_lang_change;
409 pending_lang_change = 0;
410 token->implicit_extern_c = is_extern_c > 0;
412 /* Check to see if this token is a keyword. */
413 if (token->type == CPP_NAME)
415 if (C_IS_RESERVED_WORD (token->u.value))
417 /* Mark this token as a keyword. */
418 token->type = CPP_KEYWORD;
419 /* Record which keyword. */
420 token->keyword = C_RID_CODE (token->u.value);
424 if (warn_cxx0x_compat
425 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
426 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
428 /* Warn about the C++0x keyword (but still treat it as
430 warning (OPT_Wc__0x_compat,
431 "identifier %qE will become a keyword in C++0x",
434 /* Clear out the C_RID_CODE so we don't warn about this
435 particular identifier-turned-keyword again. */
436 C_SET_RID_CODE (token->u.value, RID_MAX);
439 token->ambiguous_p = false;
440 token->keyword = RID_MAX;
443 /* Handle Objective-C++ keywords. */
444 else if (token->type == CPP_AT_NAME)
446 token->type = CPP_KEYWORD;
447 switch (C_RID_CODE (token->u.value))
449 /* Map 'class' to '@class', 'private' to '@private', etc. */
450 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
451 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
452 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
453 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
454 case RID_THROW: token->keyword = RID_AT_THROW; break;
455 case RID_TRY: token->keyword = RID_AT_TRY; break;
456 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
457 default: token->keyword = C_RID_CODE (token->u.value);
460 else if (token->type == CPP_PRAGMA)
462 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
463 token->pragma_kind = ((enum pragma_kind)
464 TREE_INT_CST_LOW (token->u.value));
465 token->u.value = NULL_TREE;
469 /* Update the globals input_location and the input file stack from TOKEN. */
471 cp_lexer_set_source_position_from_token (cp_token *token)
473 if (token->type != CPP_EOF)
475 input_location = token->location;
479 /* Return a pointer to the next token in the token stream, but do not
482 static inline cp_token *
483 cp_lexer_peek_token (cp_lexer *lexer)
485 if (cp_lexer_debugging_p (lexer))
487 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
488 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
489 putc ('\n', cp_lexer_debug_stream);
491 return lexer->next_token;
494 /* Return true if the next token has the indicated TYPE. */
497 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
499 return cp_lexer_peek_token (lexer)->type == type;
502 /* Return true if the next token does not have the indicated TYPE. */
505 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
507 return !cp_lexer_next_token_is (lexer, type);
510 /* Return true if the next token is the indicated KEYWORD. */
513 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
515 return cp_lexer_peek_token (lexer)->keyword == keyword;
518 /* Return true if the next token is not the indicated KEYWORD. */
521 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
523 return cp_lexer_peek_token (lexer)->keyword != keyword;
526 /* Return true if the next token is a keyword for a decl-specifier. */
529 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
533 token = cp_lexer_peek_token (lexer);
534 switch (token->keyword)
536 /* auto specifier: storage-class-specifier in C++,
537 simple-type-specifier in C++0x. */
539 /* Storage classes. */
545 /* Elaborated type specifiers. */
551 /* Simple type specifiers. */
565 /* GNU extensions. */
568 /* C++0x extensions. */
577 /* Return a pointer to the Nth token in the token stream. If N is 1,
578 then this is precisely equivalent to cp_lexer_peek_token (except
579 that it is not inline). One would like to disallow that case, but
580 there is one case (cp_parser_nth_token_starts_template_id) where
581 the caller passes a variable for N and it might be 1. */
584 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
588 /* N is 1-based, not zero-based. */
591 if (cp_lexer_debugging_p (lexer))
592 fprintf (cp_lexer_debug_stream,
593 "cp_lexer: peeking ahead %ld at token: ", (long)n);
596 token = lexer->next_token;
597 gcc_assert (!n || token != &eof_token);
601 if (token == lexer->last_token)
607 if (token->type != CPP_PURGED)
611 if (cp_lexer_debugging_p (lexer))
613 cp_lexer_print_token (cp_lexer_debug_stream, token);
614 putc ('\n', cp_lexer_debug_stream);
620 /* Return the next token, and advance the lexer's next_token pointer
621 to point to the next non-purged token. */
624 cp_lexer_consume_token (cp_lexer* lexer)
626 cp_token *token = lexer->next_token;
628 gcc_assert (token != &eof_token);
629 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634 if (lexer->next_token == lexer->last_token)
636 lexer->next_token = &eof_token;
641 while (lexer->next_token->type == CPP_PURGED);
643 cp_lexer_set_source_position_from_token (token);
645 /* Provide debugging output. */
646 if (cp_lexer_debugging_p (lexer))
648 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
649 cp_lexer_print_token (cp_lexer_debug_stream, token);
650 putc ('\n', cp_lexer_debug_stream);
656 /* Permanently remove the next token from the token stream, and
657 advance the next_token pointer to refer to the next non-purged
661 cp_lexer_purge_token (cp_lexer *lexer)
663 cp_token *tok = lexer->next_token;
665 gcc_assert (tok != &eof_token);
666 tok->type = CPP_PURGED;
667 tok->location = UNKNOWN_LOCATION;
668 tok->u.value = NULL_TREE;
669 tok->keyword = RID_MAX;
674 if (tok == lexer->last_token)
680 while (tok->type == CPP_PURGED);
681 lexer->next_token = tok;
684 /* Permanently remove all tokens after TOK, up to, but not
685 including, the token that will be returned next by
686 cp_lexer_peek_token. */
689 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
691 cp_token *peek = lexer->next_token;
693 if (peek == &eof_token)
694 peek = lexer->last_token;
696 gcc_assert (tok < peek);
698 for ( tok += 1; tok != peek; tok += 1)
700 tok->type = CPP_PURGED;
701 tok->location = UNKNOWN_LOCATION;
702 tok->u.value = NULL_TREE;
703 tok->keyword = RID_MAX;
707 /* Begin saving tokens. All tokens consumed after this point will be
711 cp_lexer_save_tokens (cp_lexer* lexer)
713 /* Provide debugging output. */
714 if (cp_lexer_debugging_p (lexer))
715 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
717 VEC_safe_push (cp_token_position, heap,
718 lexer->saved_tokens, lexer->next_token);
721 /* Commit to the portion of the token stream most recently saved. */
724 cp_lexer_commit_tokens (cp_lexer* lexer)
726 /* Provide debugging output. */
727 if (cp_lexer_debugging_p (lexer))
728 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
730 VEC_pop (cp_token_position, lexer->saved_tokens);
733 /* Return all tokens saved since the last call to cp_lexer_save_tokens
734 to the token stream. Stop saving tokens. */
737 cp_lexer_rollback_tokens (cp_lexer* lexer)
739 /* Provide debugging output. */
740 if (cp_lexer_debugging_p (lexer))
741 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
743 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
746 /* Print a representation of the TOKEN on the STREAM. */
748 #ifdef ENABLE_CHECKING
751 cp_lexer_print_token (FILE * stream, cp_token *token)
753 /* We don't use cpp_type2name here because the parser defines
754 a few tokens of its own. */
755 static const char *const token_names[] = {
756 /* cpplib-defined token types */
762 /* C++ parser token types - see "Manifest constants", above. */
765 "NESTED_NAME_SPECIFIER",
769 /* If we have a name for the token, print it out. Otherwise, we
770 simply give the numeric code. */
771 gcc_assert (token->type < ARRAY_SIZE(token_names));
772 fputs (token_names[token->type], stream);
774 /* For some tokens, print the associated data. */
778 /* Some keywords have a value that is not an IDENTIFIER_NODE.
779 For example, `struct' is mapped to an INTEGER_CST. */
780 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
782 /* else fall through */
784 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 *, tree, cp_cv_quals, tree, 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,
1008 cp_cv_quals cv_qualifiers,
1009 tree exception_specification,
1010 tree late_return_type)
1012 cp_declarator *declarator;
1014 declarator = make_declarator (cdk_function);
1015 declarator->declarator = target;
1016 declarator->u.function.parameters = parms;
1017 declarator->u.function.qualifiers = cv_qualifiers;
1018 declarator->u.function.exception_specification = exception_specification;
1019 declarator->u.function.late_return_type = late_return_type;
1022 declarator->parameter_pack_p = target->parameter_pack_p;
1023 target->parameter_pack_p = false;
1026 declarator->parameter_pack_p = false;
1031 /* Make a declarator for an array of BOUNDS elements, each of which is
1032 defined by ELEMENT. */
1035 make_array_declarator (cp_declarator *element, tree bounds)
1037 cp_declarator *declarator;
1039 declarator = make_declarator (cdk_array);
1040 declarator->declarator = element;
1041 declarator->u.array.bounds = bounds;
1044 declarator->parameter_pack_p = element->parameter_pack_p;
1045 element->parameter_pack_p = false;
1048 declarator->parameter_pack_p = false;
1053 /* Determine whether the declarator we've seen so far can be a
1054 parameter pack, when followed by an ellipsis. */
1056 declarator_can_be_parameter_pack (cp_declarator *declarator)
1058 /* Search for a declarator name, or any other declarator that goes
1059 after the point where the ellipsis could appear in a parameter
1060 pack. If we find any of these, then this declarator can not be
1061 made into a parameter pack. */
1063 while (declarator && !found)
1065 switch ((int)declarator->kind)
1076 declarator = declarator->declarator;
1084 cp_parameter_declarator *no_parameters;
1086 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1087 DECLARATOR and DEFAULT_ARGUMENT. */
1089 cp_parameter_declarator *
1090 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1091 cp_declarator *declarator,
1092 tree default_argument)
1094 cp_parameter_declarator *parameter;
1096 parameter = ((cp_parameter_declarator *)
1097 alloc_declarator (sizeof (cp_parameter_declarator)));
1098 parameter->next = NULL;
1099 if (decl_specifiers)
1100 parameter->decl_specifiers = *decl_specifiers;
1102 clear_decl_specs (¶meter->decl_specifiers);
1103 parameter->declarator = declarator;
1104 parameter->default_argument = default_argument;
1105 parameter->ellipsis_p = false;
1110 /* Returns true iff DECLARATOR is a declaration for a function. */
1113 function_declarator_p (const cp_declarator *declarator)
1117 if (declarator->kind == cdk_function
1118 && declarator->declarator->kind == cdk_id)
1120 if (declarator->kind == cdk_id
1121 || declarator->kind == cdk_error)
1123 declarator = declarator->declarator;
1133 A cp_parser parses the token stream as specified by the C++
1134 grammar. Its job is purely parsing, not semantic analysis. For
1135 example, the parser breaks the token stream into declarators,
1136 expressions, statements, and other similar syntactic constructs.
1137 It does not check that the types of the expressions on either side
1138 of an assignment-statement are compatible, or that a function is
1139 not declared with a parameter of type `void'.
1141 The parser invokes routines elsewhere in the compiler to perform
1142 semantic analysis and to build up the abstract syntax tree for the
1145 The parser (and the template instantiation code, which is, in a
1146 way, a close relative of parsing) are the only parts of the
1147 compiler that should be calling push_scope and pop_scope, or
1148 related functions. The parser (and template instantiation code)
1149 keeps track of what scope is presently active; everything else
1150 should simply honor that. (The code that generates static
1151 initializers may also need to set the scope, in order to check
1152 access control correctly when emitting the initializers.)
1157 The parser is of the standard recursive-descent variety. Upcoming
1158 tokens in the token stream are examined in order to determine which
1159 production to use when parsing a non-terminal. Some C++ constructs
1160 require arbitrary look ahead to disambiguate. For example, it is
1161 impossible, in the general case, to tell whether a statement is an
1162 expression or declaration without scanning the entire statement.
1163 Therefore, the parser is capable of "parsing tentatively." When the
1164 parser is not sure what construct comes next, it enters this mode.
1165 Then, while we attempt to parse the construct, the parser queues up
1166 error messages, rather than issuing them immediately, and saves the
1167 tokens it consumes. If the construct is parsed successfully, the
1168 parser "commits", i.e., it issues any queued error messages and
1169 the tokens that were being preserved are permanently discarded.
1170 If, however, the construct is not parsed successfully, the parser
1171 rolls back its state completely so that it can resume parsing using
1172 a different alternative.
1177 The performance of the parser could probably be improved substantially.
1178 We could often eliminate the need to parse tentatively by looking ahead
1179 a little bit. In some places, this approach might not entirely eliminate
1180 the need to parse tentatively, but it might still speed up the average
1183 /* Flags that are passed to some parsing functions. These values can
1184 be bitwise-ored together. */
1186 enum cp_parser_flags
1189 CP_PARSER_FLAGS_NONE = 0x0,
1190 /* The construct is optional. If it is not present, then no error
1191 should be issued. */
1192 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1193 /* When parsing a type-specifier, do not allow user-defined types. */
1194 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1197 /* This type is used for parameters and variables which hold
1198 combinations of the flags in enum cp_parser_flags. */
1199 typedef int cp_parser_flags;
1201 /* The different kinds of declarators we want to parse. */
1203 typedef enum cp_parser_declarator_kind
1205 /* We want an abstract declarator. */
1206 CP_PARSER_DECLARATOR_ABSTRACT,
1207 /* We want a named declarator. */
1208 CP_PARSER_DECLARATOR_NAMED,
1209 /* We don't mind, but the name must be an unqualified-id. */
1210 CP_PARSER_DECLARATOR_EITHER
1211 } cp_parser_declarator_kind;
1213 /* The precedence values used to parse binary expressions. The minimum value
1214 of PREC must be 1, because zero is reserved to quickly discriminate
1215 binary operators from other tokens. */
1220 PREC_LOGICAL_OR_EXPRESSION,
1221 PREC_LOGICAL_AND_EXPRESSION,
1222 PREC_INCLUSIVE_OR_EXPRESSION,
1223 PREC_EXCLUSIVE_OR_EXPRESSION,
1224 PREC_AND_EXPRESSION,
1225 PREC_EQUALITY_EXPRESSION,
1226 PREC_RELATIONAL_EXPRESSION,
1227 PREC_SHIFT_EXPRESSION,
1228 PREC_ADDITIVE_EXPRESSION,
1229 PREC_MULTIPLICATIVE_EXPRESSION,
1231 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1234 /* A mapping from a token type to a corresponding tree node type, with a
1235 precedence value. */
1237 typedef struct cp_parser_binary_operations_map_node
1239 /* The token type. */
1240 enum cpp_ttype token_type;
1241 /* The corresponding tree code. */
1242 enum tree_code tree_type;
1243 /* The precedence of this operator. */
1244 enum cp_parser_prec prec;
1245 } cp_parser_binary_operations_map_node;
1247 /* The status of a tentative parse. */
1249 typedef enum cp_parser_status_kind
1251 /* No errors have occurred. */
1252 CP_PARSER_STATUS_KIND_NO_ERROR,
1253 /* An error has occurred. */
1254 CP_PARSER_STATUS_KIND_ERROR,
1255 /* We are committed to this tentative parse, whether or not an error
1257 CP_PARSER_STATUS_KIND_COMMITTED
1258 } cp_parser_status_kind;
1260 typedef struct cp_parser_expression_stack_entry
1262 /* Left hand side of the binary operation we are currently
1265 /* Original tree code for left hand side, if it was a binary
1266 expression itself (used for -Wparentheses). */
1267 enum tree_code lhs_type;
1268 /* Tree code for the binary operation we are parsing. */
1269 enum tree_code tree_type;
1270 /* Precedence of the binary operation we are parsing. */
1271 enum cp_parser_prec prec;
1272 } cp_parser_expression_stack_entry;
1274 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1275 entries because precedence levels on the stack are monotonically
1277 typedef struct cp_parser_expression_stack_entry
1278 cp_parser_expression_stack[NUM_PREC_VALUES];
1280 /* Context that is saved and restored when parsing tentatively. */
1281 typedef struct GTY (()) cp_parser_context {
1282 /* If this is a tentative parsing context, the status of the
1284 enum cp_parser_status_kind status;
1285 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1286 that are looked up in this context must be looked up both in the
1287 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1288 the context of the containing expression. */
1291 /* The next parsing context in the stack. */
1292 struct cp_parser_context *next;
1293 } cp_parser_context;
1297 /* Constructors and destructors. */
1299 static cp_parser_context *cp_parser_context_new
1300 (cp_parser_context *);
1302 /* Class variables. */
1304 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1306 /* The operator-precedence table used by cp_parser_binary_expression.
1307 Transformed into an associative array (binops_by_token) by
1310 static const cp_parser_binary_operations_map_node binops[] = {
1311 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1312 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1314 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1315 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1319 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1321 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1322 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1324 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1325 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1327 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1330 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1332 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1334 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1336 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1338 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1340 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1343 /* The same as binops, but initialized by cp_parser_new so that
1344 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1346 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1348 /* Constructors and destructors. */
1350 /* Construct a new context. The context below this one on the stack
1351 is given by NEXT. */
1353 static cp_parser_context *
1354 cp_parser_context_new (cp_parser_context* next)
1356 cp_parser_context *context;
1358 /* Allocate the storage. */
1359 if (cp_parser_context_free_list != NULL)
1361 /* Pull the first entry from the free list. */
1362 context = cp_parser_context_free_list;
1363 cp_parser_context_free_list = context->next;
1364 memset (context, 0, sizeof (*context));
1367 context = GGC_CNEW (cp_parser_context);
1369 /* No errors have occurred yet in this context. */
1370 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1371 /* If this is not the bottommost context, copy information that we
1372 need from the previous context. */
1375 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1376 expression, then we are parsing one in this context, too. */
1377 context->object_type = next->object_type;
1378 /* Thread the stack. */
1379 context->next = next;
1385 /* The cp_parser structure represents the C++ parser. */
1387 typedef struct GTY(()) cp_parser {
1388 /* The lexer from which we are obtaining tokens. */
1391 /* The scope in which names should be looked up. If NULL_TREE, then
1392 we look up names in the scope that is currently open in the
1393 source program. If non-NULL, this is either a TYPE or
1394 NAMESPACE_DECL for the scope in which we should look. It can
1395 also be ERROR_MARK, when we've parsed a bogus scope.
1397 This value is not cleared automatically after a name is looked
1398 up, so we must be careful to clear it before starting a new look
1399 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1400 will look up `Z' in the scope of `X', rather than the current
1401 scope.) Unfortunately, it is difficult to tell when name lookup
1402 is complete, because we sometimes peek at a token, look it up,
1403 and then decide not to consume it. */
1406 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1407 last lookup took place. OBJECT_SCOPE is used if an expression
1408 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1409 respectively. QUALIFYING_SCOPE is used for an expression of the
1410 form "X::Y"; it refers to X. */
1412 tree qualifying_scope;
1414 /* A stack of parsing contexts. All but the bottom entry on the
1415 stack will be tentative contexts.
1417 We parse tentatively in order to determine which construct is in
1418 use in some situations. For example, in order to determine
1419 whether a statement is an expression-statement or a
1420 declaration-statement we parse it tentatively as a
1421 declaration-statement. If that fails, we then reparse the same
1422 token stream as an expression-statement. */
1423 cp_parser_context *context;
1425 /* True if we are parsing GNU C++. If this flag is not set, then
1426 GNU extensions are not recognized. */
1427 bool allow_gnu_extensions_p;
1429 /* TRUE if the `>' token should be interpreted as the greater-than
1430 operator. FALSE if it is the end of a template-id or
1431 template-parameter-list. In C++0x mode, this flag also applies to
1432 `>>' tokens, which are viewed as two consecutive `>' tokens when
1433 this flag is FALSE. */
1434 bool greater_than_is_operator_p;
1436 /* TRUE if default arguments are allowed within a parameter list
1437 that starts at this point. FALSE if only a gnu extension makes
1438 them permissible. */
1439 bool default_arg_ok_p;
1441 /* TRUE if we are parsing an integral constant-expression. See
1442 [expr.const] for a precise definition. */
1443 bool integral_constant_expression_p;
1445 /* TRUE if we are parsing an integral constant-expression -- but a
1446 non-constant expression should be permitted as well. This flag
1447 is used when parsing an array bound so that GNU variable-length
1448 arrays are tolerated. */
1449 bool allow_non_integral_constant_expression_p;
1451 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1452 been seen that makes the expression non-constant. */
1453 bool non_integral_constant_expression_p;
1455 /* TRUE if local variable names and `this' are forbidden in the
1457 bool local_variables_forbidden_p;
1459 /* TRUE if the declaration we are parsing is part of a
1460 linkage-specification of the form `extern string-literal
1462 bool in_unbraced_linkage_specification_p;
1464 /* TRUE if we are presently parsing a declarator, after the
1465 direct-declarator. */
1466 bool in_declarator_p;
1468 /* TRUE if we are presently parsing a template-argument-list. */
1469 bool in_template_argument_list_p;
1471 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1472 to IN_OMP_BLOCK if parsing OpenMP structured block and
1473 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1474 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1475 iteration-statement, OpenMP block or loop within that switch. */
1476 #define IN_SWITCH_STMT 1
1477 #define IN_ITERATION_STMT 2
1478 #define IN_OMP_BLOCK 4
1479 #define IN_OMP_FOR 8
1480 #define IN_IF_STMT 16
1481 unsigned char in_statement;
1483 /* TRUE if we are presently parsing the body of a switch statement.
1484 Note that this doesn't quite overlap with in_statement above.
1485 The difference relates to giving the right sets of error messages:
1486 "case not in switch" vs "break statement used with OpenMP...". */
1487 bool in_switch_statement_p;
1489 /* TRUE if we are parsing a type-id in an expression context. In
1490 such a situation, both "type (expr)" and "type (type)" are valid
1492 bool in_type_id_in_expr_p;
1494 /* TRUE if we are currently in a header file where declarations are
1495 implicitly extern "C". */
1496 bool implicit_extern_c;
1498 /* TRUE if strings in expressions should be translated to the execution
1500 bool translate_strings_p;
1502 /* TRUE if we are presently parsing the body of a function, but not
1504 bool in_function_body;
1506 /* If non-NULL, then we are parsing a construct where new type
1507 definitions are not permitted. The string stored here will be
1508 issued as an error message if a type is defined. */
1509 const char *type_definition_forbidden_message;
1511 /* A list of lists. The outer list is a stack, used for member
1512 functions of local classes. At each level there are two sub-list,
1513 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1514 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1515 TREE_VALUE's. The functions are chained in reverse declaration
1518 The TREE_PURPOSE sublist contains those functions with default
1519 arguments that need post processing, and the TREE_VALUE sublist
1520 contains those functions with definitions that need post
1523 These lists can only be processed once the outermost class being
1524 defined is complete. */
1525 tree unparsed_functions_queues;
1527 /* The number of classes whose definitions are currently in
1529 unsigned num_classes_being_defined;
1531 /* The number of template parameter lists that apply directly to the
1532 current declaration. */
1533 unsigned num_template_parameter_lists;
1538 /* Constructors and destructors. */
1540 static cp_parser *cp_parser_new
1543 /* Routines to parse various constructs.
1545 Those that return `tree' will return the error_mark_node (rather
1546 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1547 Sometimes, they will return an ordinary node if error-recovery was
1548 attempted, even though a parse error occurred. So, to check
1549 whether or not a parse error occurred, you should always use
1550 cp_parser_error_occurred. If the construct is optional (indicated
1551 either by an `_opt' in the name of the function that does the
1552 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1553 the construct is not present. */
1555 /* Lexical conventions [gram.lex] */
1557 static tree cp_parser_identifier
1559 static tree cp_parser_string_literal
1560 (cp_parser *, bool, bool);
1562 /* Basic concepts [gram.basic] */
1564 static bool cp_parser_translation_unit
1567 /* Expressions [gram.expr] */
1569 static tree cp_parser_primary_expression
1570 (cp_parser *, bool, bool, bool, cp_id_kind *);
1571 static tree cp_parser_id_expression
1572 (cp_parser *, bool, bool, bool *, bool, bool);
1573 static tree cp_parser_unqualified_id
1574 (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_nested_name_specifier_opt
1576 (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_qualifying_entity
1580 (cp_parser *, bool, bool, bool, bool, bool);
1581 static tree cp_parser_postfix_expression
1582 (cp_parser *, bool, bool, bool, cp_id_kind *);
1583 static tree cp_parser_postfix_open_square_expression
1584 (cp_parser *, tree, bool);
1585 static tree cp_parser_postfix_dot_deref_expression
1586 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1587 static tree cp_parser_parenthesized_expression_list
1588 (cp_parser *, bool, bool, bool, bool *);
1589 static void cp_parser_pseudo_destructor_name
1590 (cp_parser *, tree *, tree *);
1591 static tree cp_parser_unary_expression
1592 (cp_parser *, bool, bool, cp_id_kind *);
1593 static enum tree_code cp_parser_unary_operator
1595 static tree cp_parser_new_expression
1597 static tree cp_parser_new_placement
1599 static tree cp_parser_new_type_id
1600 (cp_parser *, tree *);
1601 static cp_declarator *cp_parser_new_declarator_opt
1603 static cp_declarator *cp_parser_direct_new_declarator
1605 static tree cp_parser_new_initializer
1607 static tree cp_parser_delete_expression
1609 static tree cp_parser_cast_expression
1610 (cp_parser *, bool, bool, cp_id_kind *);
1611 static tree cp_parser_binary_expression
1612 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1613 static tree cp_parser_question_colon_clause
1614 (cp_parser *, tree);
1615 static tree cp_parser_assignment_expression
1616 (cp_parser *, bool, cp_id_kind *);
1617 static enum tree_code cp_parser_assignment_operator_opt
1619 static tree cp_parser_expression
1620 (cp_parser *, bool, cp_id_kind *);
1621 static tree cp_parser_constant_expression
1622 (cp_parser *, bool, bool *);
1623 static tree cp_parser_builtin_offsetof
1626 /* Statements [gram.stmt.stmt] */
1628 static void cp_parser_statement
1629 (cp_parser *, tree, bool, bool *);
1630 static void cp_parser_label_for_labeled_statement
1632 static tree cp_parser_expression_statement
1633 (cp_parser *, tree);
1634 static tree cp_parser_compound_statement
1635 (cp_parser *, tree, bool);
1636 static void cp_parser_statement_seq_opt
1637 (cp_parser *, tree);
1638 static tree cp_parser_selection_statement
1639 (cp_parser *, bool *);
1640 static tree cp_parser_condition
1642 static tree cp_parser_iteration_statement
1644 static void cp_parser_for_init_statement
1646 static tree cp_parser_jump_statement
1648 static void cp_parser_declaration_statement
1651 static tree cp_parser_implicitly_scoped_statement
1652 (cp_parser *, bool *);
1653 static void cp_parser_already_scoped_statement
1656 /* Declarations [gram.dcl.dcl] */
1658 static void cp_parser_declaration_seq_opt
1660 static void cp_parser_declaration
1662 static void cp_parser_block_declaration
1663 (cp_parser *, bool);
1664 static void cp_parser_simple_declaration
1665 (cp_parser *, bool);
1666 static void cp_parser_decl_specifier_seq
1667 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1668 static tree cp_parser_storage_class_specifier_opt
1670 static tree cp_parser_function_specifier_opt
1671 (cp_parser *, cp_decl_specifier_seq *);
1672 static tree cp_parser_type_specifier
1673 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1675 static tree cp_parser_simple_type_specifier
1676 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1677 static tree cp_parser_type_name
1679 static tree cp_parser_nonclass_name
1680 (cp_parser* parser);
1681 static tree cp_parser_elaborated_type_specifier
1682 (cp_parser *, bool, bool);
1683 static tree cp_parser_enum_specifier
1685 static void cp_parser_enumerator_list
1686 (cp_parser *, tree);
1687 static void cp_parser_enumerator_definition
1688 (cp_parser *, tree);
1689 static tree cp_parser_namespace_name
1691 static void cp_parser_namespace_definition
1693 static void cp_parser_namespace_body
1695 static tree cp_parser_qualified_namespace_specifier
1697 static void cp_parser_namespace_alias_definition
1699 static bool cp_parser_using_declaration
1700 (cp_parser *, bool);
1701 static void cp_parser_using_directive
1703 static void cp_parser_asm_definition
1705 static void cp_parser_linkage_specification
1707 static void cp_parser_static_assert
1708 (cp_parser *, bool);
1709 static tree cp_parser_decltype
1712 /* Declarators [gram.dcl.decl] */
1714 static tree cp_parser_init_declarator
1715 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1716 static cp_declarator *cp_parser_declarator
1717 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1718 static cp_declarator *cp_parser_direct_declarator
1719 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1720 static enum tree_code cp_parser_ptr_operator
1721 (cp_parser *, tree *, cp_cv_quals *);
1722 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1724 static tree cp_parser_late_return_type_opt
1726 static tree cp_parser_declarator_id
1727 (cp_parser *, bool);
1728 static tree cp_parser_type_id
1730 static tree cp_parser_template_type_arg
1732 static tree cp_parser_type_id_1
1733 (cp_parser *, bool);
1734 static void cp_parser_type_specifier_seq
1735 (cp_parser *, bool, cp_decl_specifier_seq *);
1736 static tree cp_parser_parameter_declaration_clause
1738 static tree cp_parser_parameter_declaration_list
1739 (cp_parser *, bool *);
1740 static cp_parameter_declarator *cp_parser_parameter_declaration
1741 (cp_parser *, bool, bool *);
1742 static tree cp_parser_default_argument
1743 (cp_parser *, bool);
1744 static void cp_parser_function_body
1746 static tree cp_parser_initializer
1747 (cp_parser *, bool *, bool *);
1748 static tree cp_parser_initializer_clause
1749 (cp_parser *, bool *);
1750 static tree cp_parser_braced_list
1751 (cp_parser*, bool*);
1752 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1753 (cp_parser *, bool *);
1755 static bool cp_parser_ctor_initializer_opt_and_function_body
1758 /* Classes [gram.class] */
1760 static tree cp_parser_class_name
1761 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1762 static tree cp_parser_class_specifier
1764 static tree cp_parser_class_head
1765 (cp_parser *, bool *, tree *, tree *);
1766 static enum tag_types cp_parser_class_key
1768 static void cp_parser_member_specification_opt
1770 static void cp_parser_member_declaration
1772 static tree cp_parser_pure_specifier
1774 static tree cp_parser_constant_initializer
1777 /* Derived classes [gram.class.derived] */
1779 static tree cp_parser_base_clause
1781 static tree cp_parser_base_specifier
1784 /* Special member functions [gram.special] */
1786 static tree cp_parser_conversion_function_id
1788 static tree cp_parser_conversion_type_id
1790 static cp_declarator *cp_parser_conversion_declarator_opt
1792 static bool cp_parser_ctor_initializer_opt
1794 static void cp_parser_mem_initializer_list
1796 static tree cp_parser_mem_initializer
1798 static tree cp_parser_mem_initializer_id
1801 /* Overloading [gram.over] */
1803 static tree cp_parser_operator_function_id
1805 static tree cp_parser_operator
1808 /* Templates [gram.temp] */
1810 static void cp_parser_template_declaration
1811 (cp_parser *, bool);
1812 static tree cp_parser_template_parameter_list
1814 static tree cp_parser_template_parameter
1815 (cp_parser *, bool *, bool *);
1816 static tree cp_parser_type_parameter
1817 (cp_parser *, bool *);
1818 static tree cp_parser_template_id
1819 (cp_parser *, bool, bool, bool);
1820 static tree cp_parser_template_name
1821 (cp_parser *, bool, bool, bool, bool *);
1822 static tree cp_parser_template_argument_list
1824 static tree cp_parser_template_argument
1826 static void cp_parser_explicit_instantiation
1828 static void cp_parser_explicit_specialization
1831 /* Exception handling [gram.exception] */
1833 static tree cp_parser_try_block
1835 static bool cp_parser_function_try_block
1837 static void cp_parser_handler_seq
1839 static void cp_parser_handler
1841 static tree cp_parser_exception_declaration
1843 static tree cp_parser_throw_expression
1845 static tree cp_parser_exception_specification_opt
1847 static tree cp_parser_type_id_list
1850 /* GNU Extensions */
1852 static tree cp_parser_asm_specification_opt
1854 static tree cp_parser_asm_operand_list
1856 static tree cp_parser_asm_clobber_list
1858 static tree cp_parser_attributes_opt
1860 static tree cp_parser_attribute_list
1862 static bool cp_parser_extension_opt
1863 (cp_parser *, int *);
1864 static void cp_parser_label_declaration
1867 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1868 static bool cp_parser_pragma
1869 (cp_parser *, enum pragma_context);
1871 /* Objective-C++ Productions */
1873 static tree cp_parser_objc_message_receiver
1875 static tree cp_parser_objc_message_args
1877 static tree cp_parser_objc_message_expression
1879 static tree cp_parser_objc_encode_expression
1881 static tree cp_parser_objc_defs_expression
1883 static tree cp_parser_objc_protocol_expression
1885 static tree cp_parser_objc_selector_expression
1887 static tree cp_parser_objc_expression
1889 static bool cp_parser_objc_selector_p
1891 static tree cp_parser_objc_selector
1893 static tree cp_parser_objc_protocol_refs_opt
1895 static void cp_parser_objc_declaration
1897 static tree cp_parser_objc_statement
1900 /* Utility Routines */
1902 static tree cp_parser_lookup_name
1903 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1904 static tree cp_parser_lookup_name_simple
1905 (cp_parser *, tree, location_t);
1906 static tree cp_parser_maybe_treat_template_as_class
1908 static bool cp_parser_check_declarator_template_parameters
1909 (cp_parser *, cp_declarator *, location_t);
1910 static bool cp_parser_check_template_parameters
1911 (cp_parser *, unsigned, location_t, cp_declarator *);
1912 static tree cp_parser_simple_cast_expression
1914 static tree cp_parser_global_scope_opt
1915 (cp_parser *, bool);
1916 static bool cp_parser_constructor_declarator_p
1917 (cp_parser *, bool);
1918 static tree cp_parser_function_definition_from_specifiers_and_declarator
1919 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1920 static tree cp_parser_function_definition_after_declarator
1921 (cp_parser *, bool);
1922 static void cp_parser_template_declaration_after_export
1923 (cp_parser *, bool);
1924 static void cp_parser_perform_template_parameter_access_checks
1925 (VEC (deferred_access_check,gc)*);
1926 static tree cp_parser_single_declaration
1927 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1928 static tree cp_parser_functional_cast
1929 (cp_parser *, tree);
1930 static tree cp_parser_save_member_function_body
1931 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1932 static tree cp_parser_enclosed_template_argument_list
1934 static void cp_parser_save_default_args
1935 (cp_parser *, tree);
1936 static void cp_parser_late_parsing_for_member
1937 (cp_parser *, tree);
1938 static void cp_parser_late_parsing_default_args
1939 (cp_parser *, tree);
1940 static tree cp_parser_sizeof_operand
1941 (cp_parser *, enum rid);
1942 static tree cp_parser_trait_expr
1943 (cp_parser *, enum rid);
1944 static bool cp_parser_declares_only_class_p
1946 static void cp_parser_set_storage_class
1947 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1948 static void cp_parser_set_decl_spec_type
1949 (cp_decl_specifier_seq *, tree, location_t, bool);
1950 static bool cp_parser_friend_p
1951 (const cp_decl_specifier_seq *);
1952 static cp_token *cp_parser_require
1953 (cp_parser *, enum cpp_ttype, const char *);
1954 static cp_token *cp_parser_require_keyword
1955 (cp_parser *, enum rid, const char *);
1956 static bool cp_parser_token_starts_function_definition_p
1958 static bool cp_parser_next_token_starts_class_definition_p
1960 static bool cp_parser_next_token_ends_template_argument_p
1962 static bool cp_parser_nth_token_starts_template_argument_list_p
1963 (cp_parser *, size_t);
1964 static enum tag_types cp_parser_token_is_class_key
1966 static void cp_parser_check_class_key
1967 (enum tag_types, tree type);
1968 static void cp_parser_check_access_in_redeclaration
1969 (tree type, location_t location);
1970 static bool cp_parser_optional_template_keyword
1972 static void cp_parser_pre_parsed_nested_name_specifier
1974 static bool cp_parser_cache_group
1975 (cp_parser *, enum cpp_ttype, unsigned);
1976 static void cp_parser_parse_tentatively
1978 static void cp_parser_commit_to_tentative_parse
1980 static void cp_parser_abort_tentative_parse
1982 static bool cp_parser_parse_definitely
1984 static inline bool cp_parser_parsing_tentatively
1986 static bool cp_parser_uncommitted_to_tentative_parse_p
1988 static void cp_parser_error
1989 (cp_parser *, const char *);
1990 static void cp_parser_name_lookup_error
1991 (cp_parser *, tree, tree, const char *, location_t);
1992 static bool cp_parser_simulate_error
1994 static bool cp_parser_check_type_definition
1996 static void cp_parser_check_for_definition_in_return_type
1997 (cp_declarator *, tree, location_t type_location);
1998 static void cp_parser_check_for_invalid_template_id
1999 (cp_parser *, tree, location_t location);
2000 static bool cp_parser_non_integral_constant_expression
2001 (cp_parser *, const char *);
2002 static void cp_parser_diagnose_invalid_type_name
2003 (cp_parser *, tree, tree, location_t);
2004 static bool cp_parser_parse_and_diagnose_invalid_type_name
2006 static int cp_parser_skip_to_closing_parenthesis
2007 (cp_parser *, bool, bool, bool);
2008 static void cp_parser_skip_to_end_of_statement
2010 static void cp_parser_consume_semicolon_at_end_of_statement
2012 static void cp_parser_skip_to_end_of_block_or_statement
2014 static bool cp_parser_skip_to_closing_brace
2016 static void cp_parser_skip_to_end_of_template_parameter_list
2018 static void cp_parser_skip_to_pragma_eol
2019 (cp_parser*, cp_token *);
2020 static bool cp_parser_error_occurred
2022 static bool cp_parser_allow_gnu_extensions_p
2024 static bool cp_parser_is_string_literal
2026 static bool cp_parser_is_keyword
2027 (cp_token *, enum rid);
2028 static tree cp_parser_make_typename_type
2029 (cp_parser *, tree, tree, location_t location);
2030 static cp_declarator * cp_parser_make_indirect_declarator
2031 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2033 /* Returns nonzero if we are parsing tentatively. */
2036 cp_parser_parsing_tentatively (cp_parser* parser)
2038 return parser->context->next != NULL;
2041 /* Returns nonzero if TOKEN is a string literal. */
2044 cp_parser_is_string_literal (cp_token* token)
2046 return (token->type == CPP_STRING ||
2047 token->type == CPP_STRING16 ||
2048 token->type == CPP_STRING32 ||
2049 token->type == CPP_WSTRING);
2052 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2055 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2057 return token->keyword == keyword;
2060 /* If not parsing tentatively, issue a diagnostic of the form
2061 FILE:LINE: MESSAGE before TOKEN
2062 where TOKEN is the next token in the input stream. MESSAGE
2063 (specified by the caller) is usually of the form "expected
2067 cp_parser_error (cp_parser* parser, const char* message)
2069 if (!cp_parser_simulate_error (parser))
2071 cp_token *token = cp_lexer_peek_token (parser->lexer);
2072 /* This diagnostic makes more sense if it is tagged to the line
2073 of the token we just peeked at. */
2074 cp_lexer_set_source_position_from_token (token);
2076 if (token->type == CPP_PRAGMA)
2078 error ("%H%<#pragma%> is not allowed here", &token->location);
2079 cp_parser_skip_to_pragma_eol (parser, token);
2083 c_parse_error (message,
2084 /* Because c_parser_error does not understand
2085 CPP_KEYWORD, keywords are treated like
2087 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2088 token->u.value, token->flags);
2092 /* Issue an error about name-lookup failing. NAME is the
2093 IDENTIFIER_NODE DECL is the result of
2094 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2095 the thing that we hoped to find. */
2098 cp_parser_name_lookup_error (cp_parser* parser,
2101 const char* desired,
2102 location_t location)
2104 /* If name lookup completely failed, tell the user that NAME was not
2106 if (decl == error_mark_node)
2108 if (parser->scope && parser->scope != global_namespace)
2109 error ("%H%<%E::%E%> has not been declared",
2110 &location, parser->scope, name);
2111 else if (parser->scope == global_namespace)
2112 error ("%H%<::%E%> has not been declared", &location, name);
2113 else if (parser->object_scope
2114 && !CLASS_TYPE_P (parser->object_scope))
2115 error ("%Hrequest for member %qE in non-class type %qT",
2116 &location, name, parser->object_scope);
2117 else if (parser->object_scope)
2118 error ("%H%<%T::%E%> has not been declared",
2119 &location, parser->object_scope, name);
2121 error ("%H%qE has not been declared", &location, name);
2123 else if (parser->scope && parser->scope != global_namespace)
2124 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2125 else if (parser->scope == global_namespace)
2126 error ("%H%<::%E%> %s", &location, name, desired);
2128 error ("%H%qE %s", &location, name, desired);
2131 /* If we are parsing tentatively, remember that an error has occurred
2132 during this tentative parse. Returns true if the error was
2133 simulated; false if a message should be issued by the caller. */
2136 cp_parser_simulate_error (cp_parser* parser)
2138 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2140 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2146 /* Check for repeated decl-specifiers. */
2149 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2150 location_t location)
2154 for (ds = ds_first; ds != ds_last; ++ds)
2156 unsigned count = decl_specs->specs[(int)ds];
2159 /* The "long" specifier is a special case because of "long long". */
2163 error ("%H%<long long long%> is too long for GCC", &location);
2165 pedwarn_cxx98 (location, OPT_Wlong_long,
2166 "ISO C++ 1998 does not support %<long long%>");
2170 static const char *const decl_spec_names[] = {
2186 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191 /* This function is called when a type is defined. If type
2192 definitions are forbidden at this point, an error message is
2196 cp_parser_check_type_definition (cp_parser* parser)
2198 /* If types are forbidden here, issue a message. */
2199 if (parser->type_definition_forbidden_message)
2201 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2202 in the message need to be interpreted. */
2203 error (parser->type_definition_forbidden_message);
2209 /* This function is called when the DECLARATOR is processed. The TYPE
2210 was a type defined in the decl-specifiers. If it is invalid to
2211 define a type in the decl-specifiers for DECLARATOR, an error is
2212 issued. TYPE_LOCATION is the location of TYPE and is used
2213 for error reporting. */
2216 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2217 tree type, location_t type_location)
2219 /* [dcl.fct] forbids type definitions in return types.
2220 Unfortunately, it's not easy to know whether or not we are
2221 processing a return type until after the fact. */
2223 && (declarator->kind == cdk_pointer
2224 || declarator->kind == cdk_reference
2225 || declarator->kind == cdk_ptrmem))
2226 declarator = declarator->declarator;
2228 && declarator->kind == cdk_function)
2230 error ("%Hnew types may not be defined in a return type", &type_location);
2231 inform (type_location,
2232 "(perhaps a semicolon is missing after the definition of %qT)",
2237 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2238 "<" in any valid C++ program. If the next token is indeed "<",
2239 issue a message warning the user about what appears to be an
2240 invalid attempt to form a template-id. LOCATION is the location
2241 of the type-specifier (TYPE) */
2244 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2245 tree type, location_t location)
2247 cp_token_position start = 0;
2249 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2252 error ("%H%qT is not a template", &location, type);
2253 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2254 error ("%H%qE is not a template", &location, type);
2256 error ("%Hinvalid template-id", &location);
2257 /* Remember the location of the invalid "<". */
2258 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2259 start = cp_lexer_token_position (parser->lexer, true);
2260 /* Consume the "<". */
2261 cp_lexer_consume_token (parser->lexer);
2262 /* Parse the template arguments. */
2263 cp_parser_enclosed_template_argument_list (parser);
2264 /* Permanently remove the invalid template arguments so that
2265 this error message is not issued again. */
2267 cp_lexer_purge_tokens_after (parser->lexer, start);
2271 /* If parsing an integral constant-expression, issue an error message
2272 about the fact that THING appeared and return true. Otherwise,
2273 return false. In either case, set
2274 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2277 cp_parser_non_integral_constant_expression (cp_parser *parser,
2280 parser->non_integral_constant_expression_p = true;
2281 if (parser->integral_constant_expression_p)
2283 if (!parser->allow_non_integral_constant_expression_p)
2285 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286 in the message need to be interpreted. */
2287 char *message = concat (thing,
2288 " cannot appear in a constant-expression",
2298 /* Emit a diagnostic for an invalid type name. SCOPE is the
2299 qualifying scope (or NULL, if none) for ID. This function commits
2300 to the current active tentative parse, if any. (Otherwise, the
2301 problematic construct might be encountered again later, resulting
2302 in duplicate error messages.) LOCATION is the location of ID. */
2305 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306 tree scope, tree id,
2307 location_t location)
2309 tree decl, old_scope;
2310 /* Try to lookup the identifier. */
2311 old_scope = parser->scope;
2312 parser->scope = scope;
2313 decl = cp_parser_lookup_name_simple (parser, id, location);
2314 parser->scope = old_scope;
2315 /* If the lookup found a template-name, it means that the user forgot
2316 to specify an argument list. Emit a useful error message. */
2317 if (TREE_CODE (decl) == TEMPLATE_DECL)
2318 error ("%Hinvalid use of template-name %qE without an argument list",
2320 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2321 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2322 else if (TREE_CODE (decl) == TYPE_DECL)
2323 /* Something like 'unsigned A a;' */
2324 error ("%Hinvalid combination of multiple type-specifiers",
2326 else if (!parser->scope)
2328 /* Issue an error message. */
2329 error ("%H%qE does not name a type", &location, id);
2330 /* If we're in a template class, it's possible that the user was
2331 referring to a type from a base class. For example:
2333 template <typename T> struct A { typedef T X; };
2334 template <typename T> struct B : public A<T> { X x; };
2336 The user should have said "typename A<T>::X". */
2337 if (processing_template_decl && current_class_type
2338 && TYPE_BINFO (current_class_type))
2342 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2346 tree base_type = BINFO_TYPE (b);
2347 if (CLASS_TYPE_P (base_type)
2348 && dependent_type_p (base_type))
2351 /* Go from a particular instantiation of the
2352 template (which will have an empty TYPE_FIELDs),
2353 to the main version. */
2354 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2355 for (field = TYPE_FIELDS (base_type);
2357 field = TREE_CHAIN (field))
2358 if (TREE_CODE (field) == TYPE_DECL
2359 && DECL_NAME (field) == id)
2362 "(perhaps %<typename %T::%E%> was intended)",
2363 BINFO_TYPE (b), id);
2372 /* Here we diagnose qualified-ids where the scope is actually correct,
2373 but the identifier does not resolve to a valid type name. */
2374 else if (parser->scope != error_mark_node)
2376 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2377 error ("%H%qE in namespace %qE does not name a type",
2378 &location, id, parser->scope);
2379 else if (TYPE_P (parser->scope))
2380 error ("%H%qE in class %qT does not name a type",
2381 &location, id, parser->scope);
2385 cp_parser_commit_to_tentative_parse (parser);
2388 /* Check for a common situation where a type-name should be present,
2389 but is not, and issue a sensible error message. Returns true if an
2390 invalid type-name was detected.
2392 The situation handled by this function are variable declarations of the
2393 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2394 Usually, `ID' should name a type, but if we got here it means that it
2395 does not. We try to emit the best possible error message depending on
2396 how exactly the id-expression looks like. */
2399 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2402 cp_token *token = cp_lexer_peek_token (parser->lexer);
2404 cp_parser_parse_tentatively (parser);
2405 id = cp_parser_id_expression (parser,
2406 /*template_keyword_p=*/false,
2407 /*check_dependency_p=*/true,
2408 /*template_p=*/NULL,
2409 /*declarator_p=*/true,
2410 /*optional_p=*/false);
2411 /* After the id-expression, there should be a plain identifier,
2412 otherwise this is not a simple variable declaration. Also, if
2413 the scope is dependent, we cannot do much. */
2414 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2415 || (parser->scope && TYPE_P (parser->scope)
2416 && dependent_type_p (parser->scope))
2417 || TREE_CODE (id) == TYPE_DECL)
2419 cp_parser_abort_tentative_parse (parser);
2422 if (!cp_parser_parse_definitely (parser))
2425 /* Emit a diagnostic for the invalid type. */
2426 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2427 id, token->location);
2428 /* Skip to the end of the declaration; there's no point in
2429 trying to process it. */
2430 cp_parser_skip_to_end_of_block_or_statement (parser);
2434 /* Consume tokens up to, and including, the next non-nested closing `)'.
2435 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2436 are doing error recovery. Returns -1 if OR_COMMA is true and we
2437 found an unnested comma. */
2440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445 unsigned paren_depth = 0;
2446 unsigned brace_depth = 0;
2448 if (recovering && !or_comma
2449 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454 cp_token * token = cp_lexer_peek_token (parser->lexer);
2456 switch (token->type)
2459 case CPP_PRAGMA_EOL:
2460 /* If we've run out of tokens, then there is no closing `)'. */
2464 /* This matches the processing in skip_to_end_of_statement. */
2469 case CPP_OPEN_BRACE:
2472 case CPP_CLOSE_BRACE:
2478 if (recovering && or_comma && !brace_depth && !paren_depth)
2482 case CPP_OPEN_PAREN:
2487 case CPP_CLOSE_PAREN:
2488 if (!brace_depth && !paren_depth--)
2491 cp_lexer_consume_token (parser->lexer);
2500 /* Consume the token. */
2501 cp_lexer_consume_token (parser->lexer);
2505 /* Consume tokens until we reach the end of the current statement.
2506 Normally, that will be just before consuming a `;'. However, if a
2507 non-nested `}' comes first, then we stop before consuming that. */
2510 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2512 unsigned nesting_depth = 0;
2516 cp_token *token = cp_lexer_peek_token (parser->lexer);
2518 switch (token->type)
2521 case CPP_PRAGMA_EOL:
2522 /* If we've run out of tokens, stop. */
2526 /* If the next token is a `;', we have reached the end of the
2532 case CPP_CLOSE_BRACE:
2533 /* If this is a non-nested '}', stop before consuming it.
2534 That way, when confronted with something like:
2538 we stop before consuming the closing '}', even though we
2539 have not yet reached a `;'. */
2540 if (nesting_depth == 0)
2543 /* If it is the closing '}' for a block that we have
2544 scanned, stop -- but only after consuming the token.
2550 we will stop after the body of the erroneously declared
2551 function, but before consuming the following `typedef'
2553 if (--nesting_depth == 0)
2555 cp_lexer_consume_token (parser->lexer);
2559 case CPP_OPEN_BRACE:
2567 /* Consume the token. */
2568 cp_lexer_consume_token (parser->lexer);
2572 /* This function is called at the end of a statement or declaration.
2573 If the next token is a semicolon, it is consumed; otherwise, error
2574 recovery is attempted. */
2577 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2579 /* Look for the trailing `;'. */
2580 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2582 /* If there is additional (erroneous) input, skip to the end of
2584 cp_parser_skip_to_end_of_statement (parser);
2585 /* If the next token is now a `;', consume it. */
2586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2587 cp_lexer_consume_token (parser->lexer);
2591 /* Skip tokens until we have consumed an entire block, or until we
2592 have consumed a non-nested `;'. */
2595 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2597 int nesting_depth = 0;
2599 while (nesting_depth >= 0)
2601 cp_token *token = cp_lexer_peek_token (parser->lexer);
2603 switch (token->type)
2606 case CPP_PRAGMA_EOL:
2607 /* If we've run out of tokens, stop. */
2611 /* Stop if this is an unnested ';'. */
2616 case CPP_CLOSE_BRACE:
2617 /* Stop if this is an unnested '}', or closes the outermost
2620 if (nesting_depth < 0)
2626 case CPP_OPEN_BRACE:
2635 /* Consume the token. */
2636 cp_lexer_consume_token (parser->lexer);
2640 /* Skip tokens until a non-nested closing curly brace is the next
2641 token, or there are no more tokens. Return true in the first case,
2645 cp_parser_skip_to_closing_brace (cp_parser *parser)
2647 unsigned nesting_depth = 0;
2651 cp_token *token = cp_lexer_peek_token (parser->lexer);
2653 switch (token->type)
2656 case CPP_PRAGMA_EOL:
2657 /* If we've run out of tokens, stop. */
2660 case CPP_CLOSE_BRACE:
2661 /* If the next token is a non-nested `}', then we have reached
2662 the end of the current block. */
2663 if (nesting_depth-- == 0)
2667 case CPP_OPEN_BRACE:
2668 /* If it the next token is a `{', then we are entering a new
2669 block. Consume the entire block. */
2677 /* Consume the token. */
2678 cp_lexer_consume_token (parser->lexer);
2682 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2683 parameter is the PRAGMA token, allowing us to purge the entire pragma
2687 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2691 parser->lexer->in_pragma = false;
2694 token = cp_lexer_consume_token (parser->lexer);
2695 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2697 /* Ensure that the pragma is not parsed again. */
2698 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 /* Require pragma end of line, resyncing with it as necessary. The
2702 arguments are as for cp_parser_skip_to_pragma_eol. */
2705 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2707 parser->lexer->in_pragma = false;
2708 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2709 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 /* This is a simple wrapper around make_typename_type. When the id is
2713 an unresolved identifier node, we can provide a superior diagnostic
2714 using cp_parser_diagnose_invalid_type_name. */
2717 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2718 tree id, location_t id_location)
2721 if (TREE_CODE (id) == IDENTIFIER_NODE)
2723 result = make_typename_type (scope, id, typename_type,
2724 /*complain=*/tf_none);
2725 if (result == error_mark_node)
2726 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729 return make_typename_type (scope, id, typename_type, tf_error);
2732 /* This is a wrapper around the
2733 make_{pointer,ptrmem,reference}_declarator functions that decides
2734 which one to call based on the CODE and CLASS_TYPE arguments. The
2735 CODE argument should be one of the values returned by
2736 cp_parser_ptr_operator. */
2737 static cp_declarator *
2738 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2739 cp_cv_quals cv_qualifiers,
2740 cp_declarator *target)
2742 if (code == ERROR_MARK)
2743 return cp_error_declarator;
2745 if (code == INDIRECT_REF)
2746 if (class_type == NULL_TREE)
2747 return make_pointer_declarator (cv_qualifiers, target);
2749 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2750 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2751 return make_reference_declarator (cv_qualifiers, target, false);
2752 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, true);
2757 /* Create a new C++ parser. */
2760 cp_parser_new (void)
2766 /* cp_lexer_new_main is called before calling ggc_alloc because
2767 cp_lexer_new_main might load a PCH file. */
2768 lexer = cp_lexer_new_main ();
2770 /* Initialize the binops_by_token so that we can get the tree
2771 directly from the token. */
2772 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2773 binops_by_token[binops[i].token_type] = binops[i];
2775 parser = GGC_CNEW (cp_parser);
2776 parser->lexer = lexer;
2777 parser->context = cp_parser_context_new (NULL);
2779 /* For now, we always accept GNU extensions. */
2780 parser->allow_gnu_extensions_p = 1;
2782 /* The `>' token is a greater-than operator, not the end of a
2784 parser->greater_than_is_operator_p = true;
2786 parser->default_arg_ok_p = true;
2788 /* We are not parsing a constant-expression. */
2789 parser->integral_constant_expression_p = false;
2790 parser->allow_non_integral_constant_expression_p = false;
2791 parser->non_integral_constant_expression_p = false;
2793 /* Local variable names are not forbidden. */
2794 parser->local_variables_forbidden_p = false;
2796 /* We are not processing an `extern "C"' declaration. */
2797 parser->in_unbraced_linkage_specification_p = false;
2799 /* We are not processing a declarator. */
2800 parser->in_declarator_p = false;
2802 /* We are not processing a template-argument-list. */
2803 parser->in_template_argument_list_p = false;
2805 /* We are not in an iteration statement. */
2806 parser->in_statement = 0;
2808 /* We are not in a switch statement. */
2809 parser->in_switch_statement_p = false;
2811 /* We are not parsing a type-id inside an expression. */
2812 parser->in_type_id_in_expr_p = false;
2814 /* Declarations aren't implicitly extern "C". */
2815 parser->implicit_extern_c = false;
2817 /* String literals should be translated to the execution character set. */
2818 parser->translate_strings_p = true;
2820 /* We are not parsing a function body. */
2821 parser->in_function_body = false;
2823 /* The unparsed function queue is empty. */
2824 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2826 /* There are no classes being defined. */
2827 parser->num_classes_being_defined = 0;
2829 /* No template parameters apply. */
2830 parser->num_template_parameter_lists = 0;
2835 /* Create a cp_lexer structure which will emit the tokens in CACHE
2836 and push it onto the parser's lexer stack. This is used for delayed
2837 parsing of in-class method bodies and default arguments, and should
2838 not be confused with tentative parsing. */
2840 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2842 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2843 lexer->next = parser->lexer;
2844 parser->lexer = lexer;
2846 /* Move the current source position to that of the first token in the
2848 cp_lexer_set_source_position_from_token (lexer->next_token);
2851 /* Pop the top lexer off the parser stack. This is never used for the
2852 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2854 cp_parser_pop_lexer (cp_parser *parser)
2856 cp_lexer *lexer = parser->lexer;
2857 parser->lexer = lexer->next;
2858 cp_lexer_destroy (lexer);
2860 /* Put the current source position back where it was before this
2861 lexer was pushed. */
2862 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 /* Lexical conventions [gram.lex] */
2867 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2871 cp_parser_identifier (cp_parser* parser)
2875 /* Look for the identifier. */
2876 token = cp_parser_require (parser, CPP_NAME, "identifier");
2877 /* Return the value. */
2878 return token ? token->u.value : error_mark_node;
2881 /* Parse a sequence of adjacent string constants. Returns a
2882 TREE_STRING representing the combined, nul-terminated string
2883 constant. If TRANSLATE is true, translate the string to the
2884 execution character set. If WIDE_OK is true, a wide string is
2887 C++98 [lex.string] says that if a narrow string literal token is
2888 adjacent to a wide string literal token, the behavior is undefined.
2889 However, C99 6.4.5p4 says that this results in a wide string literal.
2890 We follow C99 here, for consistency with the C front end.
2892 This code is largely lifted from lex_string() in c-lex.c.
2894 FUTURE: ObjC++ will need to handle @-strings here. */
2896 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2900 struct obstack str_ob;
2901 cpp_string str, istr, *strs;
2903 enum cpp_ttype type;
2905 tok = cp_lexer_peek_token (parser->lexer);
2906 if (!cp_parser_is_string_literal (tok))
2908 cp_parser_error (parser, "expected string-literal");
2909 return error_mark_node;
2914 /* Try to avoid the overhead of creating and destroying an obstack
2915 for the common case of just one string. */
2916 if (!cp_parser_is_string_literal
2917 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2919 cp_lexer_consume_token (parser->lexer);
2921 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2922 str.len = TREE_STRING_LENGTH (tok->u.value);
2929 gcc_obstack_init (&str_ob);
2934 cp_lexer_consume_token (parser->lexer);
2936 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2937 str.len = TREE_STRING_LENGTH (tok->u.value);
2939 if (type != tok->type)
2941 if (type == CPP_STRING)
2943 else if (tok->type != CPP_STRING)
2944 error ("%Hunsupported non-standard concatenation "
2945 "of string literals", &tok->location);
2948 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2950 tok = cp_lexer_peek_token (parser->lexer);
2952 while (cp_parser_is_string_literal (tok));
2954 strs = (cpp_string *) obstack_finish (&str_ob);
2957 if (type != CPP_STRING && !wide_ok)
2959 cp_parser_error (parser, "a wide string is invalid in this context");
2963 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2964 (parse_in, strs, count, &istr, type))
2966 value = build_string (istr.len, (const char *)istr.text);
2967 free (CONST_CAST (unsigned char *, istr.text));
2973 TREE_TYPE (value) = char_array_type_node;
2976 TREE_TYPE (value) = char16_array_type_node;
2979 TREE_TYPE (value) = char32_array_type_node;
2982 TREE_TYPE (value) = wchar_array_type_node;
2986 value = fix_string_type (value);
2989 /* cpp_interpret_string has issued an error. */
2990 value = error_mark_node;
2993 obstack_free (&str_ob, 0);
2999 /* Basic concepts [gram.basic] */
3001 /* Parse a translation-unit.
3004 declaration-seq [opt]
3006 Returns TRUE if all went well. */
3009 cp_parser_translation_unit (cp_parser* parser)
3011 /* The address of the first non-permanent object on the declarator
3013 static void *declarator_obstack_base;
3017 /* Create the declarator obstack, if necessary. */
3018 if (!cp_error_declarator)
3020 gcc_obstack_init (&declarator_obstack);
3021 /* Create the error declarator. */
3022 cp_error_declarator = make_declarator (cdk_error);
3023 /* Create the empty parameter list. */
3024 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3025 /* Remember where the base of the declarator obstack lies. */
3026 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029 cp_parser_declaration_seq_opt (parser);
3031 /* If there are no tokens left then all went well. */
3032 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3034 /* Get rid of the token array; we don't need it any more. */
3035 cp_lexer_destroy (parser->lexer);
3036 parser->lexer = NULL;
3038 /* This file might have been a context that's implicitly extern
3039 "C". If so, pop the lang context. (Only relevant for PCH.) */
3040 if (parser->implicit_extern_c)
3042 pop_lang_context ();
3043 parser->implicit_extern_c = false;
3047 finish_translation_unit ();
3053 cp_parser_error (parser, "expected declaration");
3057 /* Make sure the declarator obstack was fully cleaned up. */
3058 gcc_assert (obstack_next_free (&declarator_obstack)
3059 == declarator_obstack_base);
3061 /* All went well. */
3065 /* Expressions [gram.expr] */
3067 /* Parse a primary-expression.
3078 ( compound-statement )
3079 __builtin_va_arg ( assignment-expression , type-id )
3080 __builtin_offsetof ( type-id , offsetof-expression )
3083 __has_nothrow_assign ( type-id )
3084 __has_nothrow_constructor ( type-id )
3085 __has_nothrow_copy ( type-id )
3086 __has_trivial_assign ( type-id )
3087 __has_trivial_constructor ( type-id )
3088 __has_trivial_copy ( type-id )
3089 __has_trivial_destructor ( type-id )
3090 __has_virtual_destructor ( type-id )
3091 __is_abstract ( type-id )
3092 __is_base_of ( type-id , type-id )
3093 __is_class ( type-id )
3094 __is_convertible_to ( type-id , type-id )
3095 __is_empty ( type-id )
3096 __is_enum ( type-id )
3097 __is_pod ( type-id )
3098 __is_polymorphic ( type-id )
3099 __is_union ( type-id )
3101 Objective-C++ Extension:
3109 ADDRESS_P is true iff this expression was immediately preceded by
3110 "&" and therefore might denote a pointer-to-member. CAST_P is true
3111 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3112 true iff this expression is a template argument.
3114 Returns a representation of the expression. Upon return, *IDK
3115 indicates what kind of id-expression (if any) was present. */
3118 cp_parser_primary_expression (cp_parser *parser,
3121 bool template_arg_p,
3124 cp_token *token = NULL;
3126 /* Assume the primary expression is not an id-expression. */
3127 *idk = CP_ID_KIND_NONE;
3129 /* Peek at the next token. */
3130 token = cp_lexer_peek_token (parser->lexer);
3131 switch (token->type)
3144 token = cp_lexer_consume_token (parser->lexer);
3145 if (TREE_CODE (token->u.value) == FIXED_CST)
3147 error ("%Hfixed-point types not supported in C++",
3149 return error_mark_node;
3151 /* Floating-point literals are only allowed in an integral
3152 constant expression if they are cast to an integral or
3153 enumeration type. */
3154 if (TREE_CODE (token->u.value) == REAL_CST
3155 && parser->integral_constant_expression_p
3158 /* CAST_P will be set even in invalid code like "int(2.7 +
3159 ...)". Therefore, we have to check that the next token
3160 is sure to end the cast. */
3163 cp_token *next_token;
3165 next_token = cp_lexer_peek_token (parser->lexer);
3166 if (/* The comma at the end of an
3167 enumerator-definition. */
3168 next_token->type != CPP_COMMA
3169 /* The curly brace at the end of an enum-specifier. */
3170 && next_token->type != CPP_CLOSE_BRACE
3171 /* The end of a statement. */
3172 && next_token->type != CPP_SEMICOLON
3173 /* The end of the cast-expression. */
3174 && next_token->type != CPP_CLOSE_PAREN
3175 /* The end of an array bound. */
3176 && next_token->type != CPP_CLOSE_SQUARE
3177 /* The closing ">" in a template-argument-list. */
3178 && (next_token->type != CPP_GREATER
3179 || parser->greater_than_is_operator_p)
3180 /* C++0x only: A ">>" treated like two ">" tokens,
3181 in a template-argument-list. */
3182 && (next_token->type != CPP_RSHIFT
3183 || (cxx_dialect == cxx98)
3184 || parser->greater_than_is_operator_p))
3188 /* If we are within a cast, then the constraint that the
3189 cast is to an integral or enumeration type will be
3190 checked at that point. If we are not within a cast, then
3191 this code is invalid. */
3193 cp_parser_non_integral_constant_expression
3194 (parser, "floating-point literal");
3196 return token->u.value;
3202 /* ??? Should wide strings be allowed when parser->translate_strings_p
3203 is false (i.e. in attributes)? If not, we can kill the third
3204 argument to cp_parser_string_literal. */
3205 return cp_parser_string_literal (parser,
3206 parser->translate_strings_p,
3209 case CPP_OPEN_PAREN:
3212 bool saved_greater_than_is_operator_p;
3214 /* Consume the `('. */
3215 cp_lexer_consume_token (parser->lexer);
3216 /* Within a parenthesized expression, a `>' token is always
3217 the greater-than operator. */
3218 saved_greater_than_is_operator_p
3219 = parser->greater_than_is_operator_p;
3220 parser->greater_than_is_operator_p = true;
3221 /* If we see `( { ' then we are looking at the beginning of
3222 a GNU statement-expression. */
3223 if (cp_parser_allow_gnu_extensions_p (parser)
3224 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3226 /* Statement-expressions are not allowed by the standard. */
3227 pedwarn (token->location, OPT_pedantic,
3228 "ISO C++ forbids braced-groups within expressions");
3230 /* And they're not allowed outside of a function-body; you
3231 cannot, for example, write:
3233 int i = ({ int j = 3; j + 1; });
3235 at class or namespace scope. */
3236 if (!parser->in_function_body
3237 || parser->in_template_argument_list_p)
3239 error ("%Hstatement-expressions are not allowed outside "
3240 "functions nor in template-argument lists",
3242 cp_parser_skip_to_end_of_block_or_statement (parser);
3243 expr = error_mark_node;
3247 /* Start the statement-expression. */
3248 expr = begin_stmt_expr ();
3249 /* Parse the compound-statement. */
3250 cp_parser_compound_statement (parser, expr, false);
3252 expr = finish_stmt_expr (expr, false);
3257 /* Parse the parenthesized expression. */
3258 expr = cp_parser_expression (parser, cast_p, idk);
3259 /* Let the front end know that this expression was
3260 enclosed in parentheses. This matters in case, for
3261 example, the expression is of the form `A::B', since
3262 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3264 finish_parenthesized_expr (expr);
3266 /* The `>' token might be the end of a template-id or
3267 template-parameter-list now. */
3268 parser->greater_than_is_operator_p
3269 = saved_greater_than_is_operator_p;
3270 /* Consume the `)'. */
3271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3272 cp_parser_skip_to_end_of_statement (parser);
3278 switch (token->keyword)
3280 /* These two are the boolean literals. */
3282 cp_lexer_consume_token (parser->lexer);
3283 return boolean_true_node;
3285 cp_lexer_consume_token (parser->lexer);
3286 return boolean_false_node;
3288 /* The `__null' literal. */
3290 cp_lexer_consume_token (parser->lexer);
3293 /* Recognize the `this' keyword. */
3295 cp_lexer_consume_token (parser->lexer);
3296 if (parser->local_variables_forbidden_p)
3298 error ("%H%<this%> may not be used in this context",
3300 return error_mark_node;
3302 /* Pointers cannot appear in constant-expressions. */
3303 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3304 return error_mark_node;
3305 return finish_this_expr ();
3307 /* The `operator' keyword can be the beginning of an
3312 case RID_FUNCTION_NAME:
3313 case RID_PRETTY_FUNCTION_NAME:
3314 case RID_C99_FUNCTION_NAME:
3318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3319 __func__ are the names of variables -- but they are
3320 treated specially. Therefore, they are handled here,
3321 rather than relying on the generic id-expression logic
3322 below. Grammatically, these names are id-expressions.
3324 Consume the token. */
3325 token = cp_lexer_consume_token (parser->lexer);
3327 switch (token->keyword)
3329 case RID_FUNCTION_NAME:
3330 name = "%<__FUNCTION__%>";
3332 case RID_PRETTY_FUNCTION_NAME:
3333 name = "%<__PRETTY_FUNCTION__%>";
3335 case RID_C99_FUNCTION_NAME:
3336 name = "%<__func__%>";
3342 if (cp_parser_non_integral_constant_expression (parser, name))
3343 return error_mark_node;
3345 /* Look up the name. */
3346 return finish_fname (token->u.value);
3354 /* The `__builtin_va_arg' construct is used to handle
3355 `va_arg'. Consume the `__builtin_va_arg' token. */
3356 cp_lexer_consume_token (parser->lexer);
3357 /* Look for the opening `('. */
3358 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3359 /* Now, parse the assignment-expression. */
3360 expression = cp_parser_assignment_expression (parser,
3361 /*cast_p=*/false, NULL);
3362 /* Look for the `,'. */
3363 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3364 /* Parse the type-id. */
3365 type = cp_parser_type_id (parser);
3366 /* Look for the closing `)'. */
3367 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3368 /* Using `va_arg' in a constant-expression is not
3370 if (cp_parser_non_integral_constant_expression (parser,
3372 return error_mark_node;
3373 return build_x_va_arg (expression, type);
3377 return cp_parser_builtin_offsetof (parser);
3379 case RID_HAS_NOTHROW_ASSIGN:
3380 case RID_HAS_NOTHROW_CONSTRUCTOR:
3381 case RID_HAS_NOTHROW_COPY:
3382 case RID_HAS_TRIVIAL_ASSIGN:
3383 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3384 case RID_HAS_TRIVIAL_COPY:
3385 case RID_HAS_TRIVIAL_DESTRUCTOR:
3386 case RID_HAS_VIRTUAL_DESTRUCTOR:
3387 case RID_IS_ABSTRACT:
3388 case RID_IS_BASE_OF:
3390 case RID_IS_CONVERTIBLE_TO:
3394 case RID_IS_POLYMORPHIC:
3396 return cp_parser_trait_expr (parser, token->keyword);
3398 /* Objective-C++ expressions. */
3400 case RID_AT_PROTOCOL:
3401 case RID_AT_SELECTOR:
3402 return cp_parser_objc_expression (parser);
3405 cp_parser_error (parser, "expected primary-expression");
3406 return error_mark_node;
3409 /* An id-expression can start with either an identifier, a
3410 `::' as the beginning of a qualified-id, or the "operator"
3414 case CPP_TEMPLATE_ID:
3415 case CPP_NESTED_NAME_SPECIFIER:
3419 const char *error_msg;
3422 cp_token *id_expr_token;
3425 /* Parse the id-expression. */
3427 = cp_parser_id_expression (parser,
3428 /*template_keyword_p=*/false,
3429 /*check_dependency_p=*/true,
3431 /*declarator_p=*/false,
3432 /*optional_p=*/false);
3433 if (id_expression == error_mark_node)
3434 return error_mark_node;
3435 id_expr_token = token;
3436 token = cp_lexer_peek_token (parser->lexer);
3437 done = (token->type != CPP_OPEN_SQUARE
3438 && token->type != CPP_OPEN_PAREN
3439 && token->type != CPP_DOT
3440 && token->type != CPP_DEREF
3441 && token->type != CPP_PLUS_PLUS
3442 && token->type != CPP_MINUS_MINUS);
3443 /* If we have a template-id, then no further lookup is
3444 required. If the template-id was for a template-class, we
3445 will sometimes have a TYPE_DECL at this point. */
3446 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3447 || TREE_CODE (id_expression) == TYPE_DECL)
3448 decl = id_expression;
3449 /* Look up the name. */
3452 tree ambiguous_decls;
3454 decl = cp_parser_lookup_name (parser, id_expression,
3457 /*is_namespace=*/false,
3458 /*check_dependency=*/true,
3460 id_expr_token->location);
3461 /* If the lookup was ambiguous, an error will already have
3463 if (ambiguous_decls)
3464 return error_mark_node;
3466 /* In Objective-C++, an instance variable (ivar) may be preferred
3467 to whatever cp_parser_lookup_name() found. */
3468 decl = objc_lookup_ivar (decl, id_expression);
3470 /* If name lookup gives us a SCOPE_REF, then the
3471 qualifying scope was dependent. */
3472 if (TREE_CODE (decl) == SCOPE_REF)
3474 /* At this point, we do not know if DECL is a valid
3475 integral constant expression. We assume that it is
3476 in fact such an expression, so that code like:
3478 template <int N> struct A {
3482 is accepted. At template-instantiation time, we
3483 will check that B<N>::i is actually a constant. */
3486 /* Check to see if DECL is a local variable in a context
3487 where that is forbidden. */
3488 if (parser->local_variables_forbidden_p
3489 && local_variable_p (decl))
3491 /* It might be that we only found DECL because we are
3492 trying to be generous with pre-ISO scoping rules.
3493 For example, consider:
3497 for (int i = 0; i < 10; ++i) {}
3498 extern void f(int j = i);
3501 Here, name look up will originally find the out
3502 of scope `i'. We need to issue a warning message,
3503 but then use the global `i'. */
3504 decl = check_for_out_of_scope_variable (decl);
3505 if (local_variable_p (decl))
3507 error ("%Hlocal variable %qD may not appear in this context",
3508 &id_expr_token->location, decl);
3509 return error_mark_node;
3514 decl = (finish_id_expression
3515 (id_expression, decl, parser->scope,
3517 parser->integral_constant_expression_p,
3518 parser->allow_non_integral_constant_expression_p,
3519 &parser->non_integral_constant_expression_p,
3520 template_p, done, address_p,
3523 id_expr_token->location));
3525 cp_parser_error (parser, error_msg);
3529 /* Anything else is an error. */
3531 /* ...unless we have an Objective-C++ message or string literal,
3533 if (c_dialect_objc ()
3534 && (token->type == CPP_OPEN_SQUARE
3535 || token->type == CPP_OBJC_STRING))
3536 return cp_parser_objc_expression (parser);
3538 cp_parser_error (parser, "expected primary-expression");
3539 return error_mark_node;
3543 /* Parse an id-expression.
3550 :: [opt] nested-name-specifier template [opt] unqualified-id
3552 :: operator-function-id
3555 Return a representation of the unqualified portion of the
3556 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3557 a `::' or nested-name-specifier.
3559 Often, if the id-expression was a qualified-id, the caller will
3560 want to make a SCOPE_REF to represent the qualified-id. This
3561 function does not do this in order to avoid wastefully creating
3562 SCOPE_REFs when they are not required.
3564 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3567 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3568 uninstantiated templates.
3570 If *TEMPLATE_P is non-NULL, it is set to true iff the
3571 `template' keyword is used to explicitly indicate that the entity
3572 named is a template.
3574 If DECLARATOR_P is true, the id-expression is appearing as part of
3575 a declarator, rather than as part of an expression. */
3578 cp_parser_id_expression (cp_parser *parser,
3579 bool template_keyword_p,
3580 bool check_dependency_p,
3585 bool global_scope_p;
3586 bool nested_name_specifier_p;
3588 /* Assume the `template' keyword was not used. */
3590 *template_p = template_keyword_p;
3592 /* Look for the optional `::' operator. */
3594 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3596 /* Look for the optional nested-name-specifier. */
3597 nested_name_specifier_p
3598 = (cp_parser_nested_name_specifier_opt (parser,
3599 /*typename_keyword_p=*/false,
3604 /* If there is a nested-name-specifier, then we are looking at
3605 the first qualified-id production. */
3606 if (nested_name_specifier_p)
3609 tree saved_object_scope;
3610 tree saved_qualifying_scope;
3611 tree unqualified_id;
3614 /* See if the next token is the `template' keyword. */
3616 template_p = &is_template;
3617 *template_p = cp_parser_optional_template_keyword (parser);
3618 /* Name lookup we do during the processing of the
3619 unqualified-id might obliterate SCOPE. */
3620 saved_scope = parser->scope;
3621 saved_object_scope = parser->object_scope;
3622 saved_qualifying_scope = parser->qualifying_scope;
3623 /* Process the final unqualified-id. */
3624 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3627 /*optional_p=*/false);
3628 /* Restore the SAVED_SCOPE for our caller. */
3629 parser->scope = saved_scope;
3630 parser->object_scope = saved_object_scope;
3631 parser->qualifying_scope = saved_qualifying_scope;
3633 return unqualified_id;
3635 /* Otherwise, if we are in global scope, then we are looking at one
3636 of the other qualified-id productions. */
3637 else if (global_scope_p)
3642 /* Peek at the next token. */
3643 token = cp_lexer_peek_token (parser->lexer);
3645 /* If it's an identifier, and the next token is not a "<", then
3646 we can avoid the template-id case. This is an optimization
3647 for this common case. */
3648 if (token->type == CPP_NAME
3649 && !cp_parser_nth_token_starts_template_argument_list_p
3651 return cp_parser_identifier (parser);
3653 cp_parser_parse_tentatively (parser);
3654 /* Try a template-id. */
3655 id = cp_parser_template_id (parser,
3656 /*template_keyword_p=*/false,
3657 /*check_dependency_p=*/true,
3659 /* If that worked, we're done. */
3660 if (cp_parser_parse_definitely (parser))
3663 /* Peek at the next token. (Changes in the token buffer may
3664 have invalidated the pointer obtained above.) */
3665 token = cp_lexer_peek_token (parser->lexer);
3667 switch (token->type)
3670 return cp_parser_identifier (parser);
3673 if (token->keyword == RID_OPERATOR)
3674 return cp_parser_operator_function_id (parser);
3678 cp_parser_error (parser, "expected id-expression");
3679 return error_mark_node;
3683 return cp_parser_unqualified_id (parser, template_keyword_p,
3684 /*check_dependency_p=*/true,
3689 /* Parse an unqualified-id.
3693 operator-function-id
3694 conversion-function-id
3698 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3699 keyword, in a construct like `A::template ...'.
3701 Returns a representation of unqualified-id. For the `identifier'
3702 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3703 production a BIT_NOT_EXPR is returned; the operand of the
3704 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3705 other productions, see the documentation accompanying the
3706 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3707 names are looked up in uninstantiated templates. If DECLARATOR_P
3708 is true, the unqualified-id is appearing as part of a declarator,
3709 rather than as part of an expression. */
3712 cp_parser_unqualified_id (cp_parser* parser,
3713 bool template_keyword_p,
3714 bool check_dependency_p,
3720 /* Peek at the next token. */
3721 token = cp_lexer_peek_token (parser->lexer);
3723 switch (token->type)
3729 /* We don't know yet whether or not this will be a
3731 cp_parser_parse_tentatively (parser);
3732 /* Try a template-id. */
3733 id = cp_parser_template_id (parser, template_keyword_p,
3736 /* If it worked, we're done. */
3737 if (cp_parser_parse_definitely (parser))
3739 /* Otherwise, it's an ordinary identifier. */
3740 return cp_parser_identifier (parser);
3743 case CPP_TEMPLATE_ID:
3744 return cp_parser_template_id (parser, template_keyword_p,
3751 tree qualifying_scope;
3756 /* Consume the `~' token. */
3757 cp_lexer_consume_token (parser->lexer);
3758 /* Parse the class-name. The standard, as written, seems to
3761 template <typename T> struct S { ~S (); };
3762 template <typename T> S<T>::~S() {}
3764 is invalid, since `~' must be followed by a class-name, but
3765 `S<T>' is dependent, and so not known to be a class.
3766 That's not right; we need to look in uninstantiated
3767 templates. A further complication arises from:
3769 template <typename T> void f(T t) {
3773 Here, it is not possible to look up `T' in the scope of `T'
3774 itself. We must look in both the current scope, and the
3775 scope of the containing complete expression.
3777 Yet another issue is:
3786 The standard does not seem to say that the `S' in `~S'
3787 should refer to the type `S' and not the data member
3790 /* DR 244 says that we look up the name after the "~" in the
3791 same scope as we looked up the qualifying name. That idea
3792 isn't fully worked out; it's more complicated than that. */
3793 scope = parser->scope;
3794 object_scope = parser->object_scope;
3795 qualifying_scope = parser->qualifying_scope;
3797 /* Check for invalid scopes. */
3798 if (scope == error_mark_node)
3800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3801 cp_lexer_consume_token (parser->lexer);
3802 return error_mark_node;
3804 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3806 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3807 error ("%Hscope %qT before %<~%> is not a class-name",
3808 &token->location, scope);
3809 cp_parser_simulate_error (parser);
3810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3811 cp_lexer_consume_token (parser->lexer);
3812 return error_mark_node;
3814 gcc_assert (!scope || TYPE_P (scope));
3816 /* If the name is of the form "X::~X" it's OK. */
3817 token = cp_lexer_peek_token (parser->lexer);
3819 && token->type == CPP_NAME
3820 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3822 && constructor_name_p (token->u.value, scope))
3824 cp_lexer_consume_token (parser->lexer);
3825 return build_nt (BIT_NOT_EXPR, scope);
3828 /* If there was an explicit qualification (S::~T), first look
3829 in the scope given by the qualification (i.e., S). */
3831 type_decl = NULL_TREE;
3834 cp_parser_parse_tentatively (parser);
3835 type_decl = cp_parser_class_name (parser,
3836 /*typename_keyword_p=*/false,
3837 /*template_keyword_p=*/false,
3839 /*check_dependency=*/false,
3840 /*class_head_p=*/false,
3842 if (cp_parser_parse_definitely (parser))
3845 /* In "N::S::~S", look in "N" as well. */
3846 if (!done && scope && qualifying_scope)
3848 cp_parser_parse_tentatively (parser);
3849 parser->scope = qualifying_scope;
3850 parser->object_scope = NULL_TREE;
3851 parser->qualifying_scope = NULL_TREE;
3853 = cp_parser_class_name (parser,
3854 /*typename_keyword_p=*/false,
3855 /*template_keyword_p=*/false,
3857 /*check_dependency=*/false,
3858 /*class_head_p=*/false,
3860 if (cp_parser_parse_definitely (parser))
3863 /* In "p->S::~T", look in the scope given by "*p" as well. */
3864 else if (!done && object_scope)
3866 cp_parser_parse_tentatively (parser);
3867 parser->scope = object_scope;
3868 parser->object_scope = NULL_TREE;
3869 parser->qualifying_scope = NULL_TREE;
3871 = cp_parser_class_name (parser,
3872 /*typename_keyword_p=*/false,
3873 /*template_keyword_p=*/false,
3875 /*check_dependency=*/false,
3876 /*class_head_p=*/false,
3878 if (cp_parser_parse_definitely (parser))
3881 /* Look in the surrounding context. */
3884 parser->scope = NULL_TREE;
3885 parser->object_scope = NULL_TREE;
3886 parser->qualifying_scope = NULL_TREE;
3887 if (processing_template_decl)
3888 cp_parser_parse_tentatively (parser);
3890 = cp_parser_class_name (parser,
3891 /*typename_keyword_p=*/false,
3892 /*template_keyword_p=*/false,
3894 /*check_dependency=*/false,
3895 /*class_head_p=*/false,
3897 if (processing_template_decl
3898 && ! cp_parser_parse_definitely (parser))
3900 /* We couldn't find a type with this name, so just accept
3901 it and check for a match at instantiation time. */
3902 type_decl = cp_parser_identifier (parser);
3903 if (type_decl != error_mark_node)
3904 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3908 /* If an error occurred, assume that the name of the
3909 destructor is the same as the name of the qualifying
3910 class. That allows us to keep parsing after running
3911 into ill-formed destructor names. */
3912 if (type_decl == error_mark_node && scope)
3913 return build_nt (BIT_NOT_EXPR, scope);
3914 else if (type_decl == error_mark_node)
3915 return error_mark_node;
3917 /* Check that destructor name and scope match. */
3918 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3921 error ("%Hdeclaration of %<~%T%> as member of %qT",
3922 &token->location, type_decl, scope);
3923 cp_parser_simulate_error (parser);
3924 return error_mark_node;
3929 A typedef-name that names a class shall not be used as the
3930 identifier in the declarator for a destructor declaration. */
3932 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3933 && !DECL_SELF_REFERENCE_P (type_decl)
3934 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3935 error ("%Htypedef-name %qD used as destructor declarator",
3936 &token->location, type_decl);
3938 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3942 if (token->keyword == RID_OPERATOR)
3946 /* This could be a template-id, so we try that first. */
3947 cp_parser_parse_tentatively (parser);
3948 /* Try a template-id. */
3949 id = cp_parser_template_id (parser, template_keyword_p,
3950 /*check_dependency_p=*/true,
3952 /* If that worked, we're done. */
3953 if (cp_parser_parse_definitely (parser))
3955 /* We still don't know whether we're looking at an
3956 operator-function-id or a conversion-function-id. */
3957 cp_parser_parse_tentatively (parser);
3958 /* Try an operator-function-id. */
3959 id = cp_parser_operator_function_id (parser);
3960 /* If that didn't work, try a conversion-function-id. */
3961 if (!cp_parser_parse_definitely (parser))
3962 id = cp_parser_conversion_function_id (parser);
3971 cp_parser_error (parser, "expected unqualified-id");
3972 return error_mark_node;
3976 /* Parse an (optional) nested-name-specifier.
3978 nested-name-specifier: [C++98]
3979 class-or-namespace-name :: nested-name-specifier [opt]
3980 class-or-namespace-name :: template nested-name-specifier [opt]
3982 nested-name-specifier: [C++0x]
3985 nested-name-specifier identifier ::
3986 nested-name-specifier template [opt] simple-template-id ::
3988 PARSER->SCOPE should be set appropriately before this function is
3989 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3990 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3993 Sets PARSER->SCOPE to the class (TYPE) or namespace
3994 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3995 it unchanged if there is no nested-name-specifier. Returns the new
3996 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3998 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3999 part of a declaration and/or decl-specifier. */
4002 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4003 bool typename_keyword_p,
4004 bool check_dependency_p,
4006 bool is_declaration)
4008 bool success = false;
4009 cp_token_position start = 0;
4012 /* Remember where the nested-name-specifier starts. */
4013 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4015 start = cp_lexer_token_position (parser->lexer, false);
4016 push_deferring_access_checks (dk_deferred);
4023 tree saved_qualifying_scope;
4024 bool template_keyword_p;
4026 /* Spot cases that cannot be the beginning of a
4027 nested-name-specifier. */
4028 token = cp_lexer_peek_token (parser->lexer);
4030 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4031 the already parsed nested-name-specifier. */
4032 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4034 /* Grab the nested-name-specifier and continue the loop. */
4035 cp_parser_pre_parsed_nested_name_specifier (parser);
4036 /* If we originally encountered this nested-name-specifier
4037 with IS_DECLARATION set to false, we will not have
4038 resolved TYPENAME_TYPEs, so we must do so here. */
4040 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4042 new_scope = resolve_typename_type (parser->scope,
4043 /*only_current_p=*/false);
4044 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4045 parser->scope = new_scope;
4051 /* Spot cases that cannot be the beginning of a
4052 nested-name-specifier. On the second and subsequent times
4053 through the loop, we look for the `template' keyword. */
4054 if (success && token->keyword == RID_TEMPLATE)
4056 /* A template-id can start a nested-name-specifier. */
4057 else if (token->type == CPP_TEMPLATE_ID)
4061 /* If the next token is not an identifier, then it is
4062 definitely not a type-name or namespace-name. */
4063 if (token->type != CPP_NAME)
4065 /* If the following token is neither a `<' (to begin a
4066 template-id), nor a `::', then we are not looking at a
4067 nested-name-specifier. */
4068 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4069 if (token->type != CPP_SCOPE
4070 && !cp_parser_nth_token_starts_template_argument_list_p
4075 /* The nested-name-specifier is optional, so we parse
4077 cp_parser_parse_tentatively (parser);
4079 /* Look for the optional `template' keyword, if this isn't the
4080 first time through the loop. */
4082 template_keyword_p = cp_parser_optional_template_keyword (parser);
4084 template_keyword_p = false;
4086 /* Save the old scope since the name lookup we are about to do
4087 might destroy it. */
4088 old_scope = parser->scope;
4089 saved_qualifying_scope = parser->qualifying_scope;
4090 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4091 look up names in "X<T>::I" in order to determine that "Y" is
4092 a template. So, if we have a typename at this point, we make
4093 an effort to look through it. */
4095 && !typename_keyword_p
4097 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4098 parser->scope = resolve_typename_type (parser->scope,
4099 /*only_current_p=*/false);
4100 /* Parse the qualifying entity. */
4102 = cp_parser_qualifying_entity (parser,
4108 /* Look for the `::' token. */
4109 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4111 /* If we found what we wanted, we keep going; otherwise, we're
4113 if (!cp_parser_parse_definitely (parser))
4115 bool error_p = false;
4117 /* Restore the OLD_SCOPE since it was valid before the
4118 failed attempt at finding the last
4119 class-or-namespace-name. */
4120 parser->scope = old_scope;
4121 parser->qualifying_scope = saved_qualifying_scope;
4122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4124 /* If the next token is an identifier, and the one after
4125 that is a `::', then any valid interpretation would have
4126 found a class-or-namespace-name. */
4127 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4128 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4130 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4133 token = cp_lexer_consume_token (parser->lexer);
4136 if (!token->ambiguous_p)
4139 tree ambiguous_decls;
4141 decl = cp_parser_lookup_name (parser, token->u.value,
4143 /*is_template=*/false,
4144 /*is_namespace=*/false,
4145 /*check_dependency=*/true,
4148 if (TREE_CODE (decl) == TEMPLATE_DECL)
4149 error ("%H%qD used without template parameters",
4150 &token->location, decl);
4151 else if (ambiguous_decls)
4153 error ("%Hreference to %qD is ambiguous",
4154 &token->location, token->u.value);
4155 print_candidates (ambiguous_decls);
4156 decl = error_mark_node;
4160 const char* msg = "is not a class or namespace";
4161 if (cxx_dialect != cxx98)
4162 msg = "is not a class, namespace, or enumeration";
4163 cp_parser_name_lookup_error
4164 (parser, token->u.value, decl, msg,
4168 parser->scope = error_mark_node;
4170 /* Treat this as a successful nested-name-specifier
4175 If the name found is not a class-name (clause
4176 _class_) or namespace-name (_namespace.def_), the
4177 program is ill-formed. */
4180 cp_lexer_consume_token (parser->lexer);
4184 /* We've found one valid nested-name-specifier. */
4186 /* Name lookup always gives us a DECL. */
4187 if (TREE_CODE (new_scope) == TYPE_DECL)
4188 new_scope = TREE_TYPE (new_scope);
4189 /* Uses of "template" must be followed by actual templates. */
4190 if (template_keyword_p
4191 && !(CLASS_TYPE_P (new_scope)
4192 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4193 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4194 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4195 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4196 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4197 == TEMPLATE_ID_EXPR)))
4198 permerror (input_location, TYPE_P (new_scope)
4199 ? "%qT is not a template"
4200 : "%qD is not a template",
4202 /* If it is a class scope, try to complete it; we are about to
4203 be looking up names inside the class. */
4204 if (TYPE_P (new_scope)
4205 /* Since checking types for dependency can be expensive,
4206 avoid doing it if the type is already complete. */
4207 && !COMPLETE_TYPE_P (new_scope)
4208 /* Do not try to complete dependent types. */
4209 && !dependent_type_p (new_scope))
4211 new_scope = complete_type (new_scope);
4212 /* If it is a typedef to current class, use the current
4213 class instead, as the typedef won't have any names inside
4215 if (!COMPLETE_TYPE_P (new_scope)
4216 && currently_open_class (new_scope))
4217 new_scope = TYPE_MAIN_VARIANT (new_scope);
4219 /* Make sure we look in the right scope the next time through
4221 parser->scope = new_scope;
4224 /* If parsing tentatively, replace the sequence of tokens that makes
4225 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4226 token. That way, should we re-parse the token stream, we will
4227 not have to repeat the effort required to do the parse, nor will
4228 we issue duplicate error messages. */
4229 if (success && start)
4233 token = cp_lexer_token_at (parser->lexer, start);
4234 /* Reset the contents of the START token. */
4235 token->type = CPP_NESTED_NAME_SPECIFIER;
4236 /* Retrieve any deferred checks. Do not pop this access checks yet
4237 so the memory will not be reclaimed during token replacing below. */
4238 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4239 token->u.tree_check_value->value = parser->scope;
4240 token->u.tree_check_value->checks = get_deferred_access_checks ();
4241 token->u.tree_check_value->qualifying_scope =
4242 parser->qualifying_scope;
4243 token->keyword = RID_MAX;
4245 /* Purge all subsequent tokens. */
4246 cp_lexer_purge_tokens_after (parser->lexer, start);
4250 pop_to_parent_deferring_access_checks ();
4252 return success ? parser->scope : NULL_TREE;
4255 /* Parse a nested-name-specifier. See
4256 cp_parser_nested_name_specifier_opt for details. This function
4257 behaves identically, except that it will an issue an error if no
4258 nested-name-specifier is present. */
4261 cp_parser_nested_name_specifier (cp_parser *parser,
4262 bool typename_keyword_p,
4263 bool check_dependency_p,
4265 bool is_declaration)
4269 /* Look for the nested-name-specifier. */
4270 scope = cp_parser_nested_name_specifier_opt (parser,
4275 /* If it was not present, issue an error message. */
4278 cp_parser_error (parser, "expected nested-name-specifier");
4279 parser->scope = NULL_TREE;
4285 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4286 this is either a class-name or a namespace-name (which corresponds
4287 to the class-or-namespace-name production in the grammar). For
4288 C++0x, it can also be a type-name that refers to an enumeration
4291 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4292 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4293 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4294 TYPE_P is TRUE iff the next name should be taken as a class-name,
4295 even the same name is declared to be another entity in the same
4298 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4299 specified by the class-or-namespace-name. If neither is found the
4300 ERROR_MARK_NODE is returned. */
4303 cp_parser_qualifying_entity (cp_parser *parser,
4304 bool typename_keyword_p,
4305 bool template_keyword_p,
4306 bool check_dependency_p,
4308 bool is_declaration)
4311 tree saved_qualifying_scope;
4312 tree saved_object_scope;
4315 bool successful_parse_p;
4317 /* Before we try to parse the class-name, we must save away the
4318 current PARSER->SCOPE since cp_parser_class_name will destroy
4320 saved_scope = parser->scope;
4321 saved_qualifying_scope = parser->qualifying_scope;
4322 saved_object_scope = parser->object_scope;
4323 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4324 there is no need to look for a namespace-name. */
4325 only_class_p = template_keyword_p
4326 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4328 cp_parser_parse_tentatively (parser);
4329 scope = cp_parser_class_name (parser,
4332 type_p ? class_type : none_type,
4334 /*class_head_p=*/false,
4336 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4337 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4339 && cxx_dialect != cxx98
4340 && !successful_parse_p)
4342 /* Restore the saved scope. */
4343 parser->scope = saved_scope;
4344 parser->qualifying_scope = saved_qualifying_scope;
4345 parser->object_scope = saved_object_scope;
4347 /* Parse tentatively. */
4348 cp_parser_parse_tentatively (parser);
4350 /* Parse a typedef-name or enum-name. */
4351 scope = cp_parser_nonclass_name (parser);
4352 successful_parse_p = cp_parser_parse_definitely (parser);
4354 /* If that didn't work, try for a namespace-name. */
4355 if (!only_class_p && !successful_parse_p)
4357 /* Restore the saved scope. */
4358 parser->scope = saved_scope;
4359 parser->qualifying_scope = saved_qualifying_scope;
4360 parser->object_scope = saved_object_scope;
4361 /* If we are not looking at an identifier followed by the scope
4362 resolution operator, then this is not part of a
4363 nested-name-specifier. (Note that this function is only used
4364 to parse the components of a nested-name-specifier.) */
4365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4366 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4367 return error_mark_node;
4368 scope = cp_parser_namespace_name (parser);
4374 /* Parse a postfix-expression.
4378 postfix-expression [ expression ]
4379 postfix-expression ( expression-list [opt] )
4380 simple-type-specifier ( expression-list [opt] )
4381 typename :: [opt] nested-name-specifier identifier
4382 ( expression-list [opt] )
4383 typename :: [opt] nested-name-specifier template [opt] template-id
4384 ( expression-list [opt] )
4385 postfix-expression . template [opt] id-expression
4386 postfix-expression -> template [opt] id-expression
4387 postfix-expression . pseudo-destructor-name
4388 postfix-expression -> pseudo-destructor-name
4389 postfix-expression ++
4390 postfix-expression --
4391 dynamic_cast < type-id > ( expression )
4392 static_cast < type-id > ( expression )
4393 reinterpret_cast < type-id > ( expression )
4394 const_cast < type-id > ( expression )
4395 typeid ( expression )
4401 ( type-id ) { initializer-list , [opt] }
4403 This extension is a GNU version of the C99 compound-literal
4404 construct. (The C99 grammar uses `type-name' instead of `type-id',
4405 but they are essentially the same concept.)
4407 If ADDRESS_P is true, the postfix expression is the operand of the
4408 `&' operator. CAST_P is true if this expression is the target of a
4411 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4412 class member access expressions [expr.ref].
4414 Returns a representation of the expression. */
4417 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4418 bool member_access_only_p,
4419 cp_id_kind * pidk_return)
4423 cp_id_kind idk = CP_ID_KIND_NONE;
4424 tree postfix_expression = NULL_TREE;
4425 bool is_member_access = false;
4427 /* Peek at the next token. */
4428 token = cp_lexer_peek_token (parser->lexer);
4429 /* Some of the productions are determined by keywords. */
4430 keyword = token->keyword;
4440 const char *saved_message;
4442 /* All of these can be handled in the same way from the point
4443 of view of parsing. Begin by consuming the token
4444 identifying the cast. */
4445 cp_lexer_consume_token (parser->lexer);
4447 /* New types cannot be defined in the cast. */
4448 saved_message = parser->type_definition_forbidden_message;
4449 parser->type_definition_forbidden_message
4450 = "types may not be defined in casts";
4452 /* Look for the opening `<'. */
4453 cp_parser_require (parser, CPP_LESS, "%<<%>");
4454 /* Parse the type to which we are casting. */
4455 type = cp_parser_type_id (parser);
4456 /* Look for the closing `>'. */
4457 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4458 /* Restore the old message. */
4459 parser->type_definition_forbidden_message = saved_message;
4461 /* And the expression which is being cast. */
4462 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4463 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4464 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4466 /* Only type conversions to integral or enumeration types
4467 can be used in constant-expressions. */
4468 if (!cast_valid_in_integral_constant_expression_p (type)
4469 && (cp_parser_non_integral_constant_expression
4471 "a cast to a type other than an integral or "
4472 "enumeration type")))
4473 return error_mark_node;
4479 = build_dynamic_cast (type, expression, tf_warning_or_error);
4483 = build_static_cast (type, expression, tf_warning_or_error);
4487 = build_reinterpret_cast (type, expression,
4488 tf_warning_or_error);
4492 = build_const_cast (type, expression, tf_warning_or_error);
4503 const char *saved_message;
4504 bool saved_in_type_id_in_expr_p;
4506 /* Consume the `typeid' token. */
4507 cp_lexer_consume_token (parser->lexer);
4508 /* Look for the `(' token. */
4509 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4510 /* Types cannot be defined in a `typeid' expression. */
4511 saved_message = parser->type_definition_forbidden_message;
4512 parser->type_definition_forbidden_message
4513 = "types may not be defined in a %<typeid%> expression";
4514 /* We can't be sure yet whether we're looking at a type-id or an
4516 cp_parser_parse_tentatively (parser);
4517 /* Try a type-id first. */
4518 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4519 parser->in_type_id_in_expr_p = true;
4520 type = cp_parser_type_id (parser);
4521 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4522 /* Look for the `)' token. Otherwise, we can't be sure that
4523 we're not looking at an expression: consider `typeid (int
4524 (3))', for example. */
4525 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4526 /* If all went well, simply lookup the type-id. */
4527 if (cp_parser_parse_definitely (parser))
4528 postfix_expression = get_typeid (type);
4529 /* Otherwise, fall back to the expression variant. */
4534 /* Look for an expression. */
4535 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4536 /* Compute its typeid. */
4537 postfix_expression = build_typeid (expression);
4538 /* Look for the `)' token. */
4539 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4541 /* Restore the saved message. */
4542 parser->type_definition_forbidden_message = saved_message;
4543 /* `typeid' may not appear in an integral constant expression. */
4544 if (cp_parser_non_integral_constant_expression(parser,
4545 "%<typeid%> operator"))
4546 return error_mark_node;
4553 /* The syntax permitted here is the same permitted for an
4554 elaborated-type-specifier. */
4555 type = cp_parser_elaborated_type_specifier (parser,
4556 /*is_friend=*/false,
4557 /*is_declaration=*/false);
4558 postfix_expression = cp_parser_functional_cast (parser, type);
4566 /* If the next thing is a simple-type-specifier, we may be
4567 looking at a functional cast. We could also be looking at
4568 an id-expression. So, we try the functional cast, and if
4569 that doesn't work we fall back to the primary-expression. */
4570 cp_parser_parse_tentatively (parser);
4571 /* Look for the simple-type-specifier. */
4572 type = cp_parser_simple_type_specifier (parser,
4573 /*decl_specs=*/NULL,
4574 CP_PARSER_FLAGS_NONE);
4575 /* Parse the cast itself. */
4576 if (!cp_parser_error_occurred (parser))
4578 = cp_parser_functional_cast (parser, type);
4579 /* If that worked, we're done. */
4580 if (cp_parser_parse_definitely (parser))
4583 /* If the functional-cast didn't work out, try a
4584 compound-literal. */
4585 if (cp_parser_allow_gnu_extensions_p (parser)
4586 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4588 VEC(constructor_elt,gc) *initializer_list = NULL;
4589 bool saved_in_type_id_in_expr_p;
4591 cp_parser_parse_tentatively (parser);
4592 /* Consume the `('. */
4593 cp_lexer_consume_token (parser->lexer);
4594 /* Parse the type. */
4595 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4596 parser->in_type_id_in_expr_p = true;
4597 type = cp_parser_type_id (parser);
4598 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4599 /* Look for the `)'. */
4600 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4601 /* Look for the `{'. */
4602 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4603 /* If things aren't going well, there's no need to
4605 if (!cp_parser_error_occurred (parser))
4607 bool non_constant_p;
4608 /* Parse the initializer-list. */
4610 = cp_parser_initializer_list (parser, &non_constant_p);
4611 /* Allow a trailing `,'. */
4612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4613 cp_lexer_consume_token (parser->lexer);
4614 /* Look for the final `}'. */
4615 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4617 /* If that worked, we're definitely looking at a
4618 compound-literal expression. */
4619 if (cp_parser_parse_definitely (parser))
4621 /* Warn the user that a compound literal is not
4622 allowed in standard C++. */
4623 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4624 /* For simplicity, we disallow compound literals in
4625 constant-expressions. We could
4626 allow compound literals of integer type, whose
4627 initializer was a constant, in constant
4628 expressions. Permitting that usage, as a further
4629 extension, would not change the meaning of any
4630 currently accepted programs. (Of course, as
4631 compound literals are not part of ISO C++, the
4632 standard has nothing to say.) */
4633 if (cp_parser_non_integral_constant_expression
4634 (parser, "non-constant compound literals"))
4636 postfix_expression = error_mark_node;
4639 /* Form the representation of the compound-literal. */
4641 = (finish_compound_literal
4642 (type, build_constructor (init_list_type_node,
4643 initializer_list)));
4648 /* It must be a primary-expression. */
4650 = cp_parser_primary_expression (parser, address_p, cast_p,
4651 /*template_arg_p=*/false,
4657 /* Keep looping until the postfix-expression is complete. */
4660 if (idk == CP_ID_KIND_UNQUALIFIED
4661 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4662 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4663 /* It is not a Koenig lookup function call. */
4665 = unqualified_name_lookup_error (postfix_expression);
4667 /* Peek at the next token. */
4668 token = cp_lexer_peek_token (parser->lexer);
4670 switch (token->type)
4672 case CPP_OPEN_SQUARE:
4674 = cp_parser_postfix_open_square_expression (parser,
4677 idk = CP_ID_KIND_NONE;
4678 is_member_access = false;
4681 case CPP_OPEN_PAREN:
4682 /* postfix-expression ( expression-list [opt] ) */
4685 bool is_builtin_constant_p;
4686 bool saved_integral_constant_expression_p = false;
4687 bool saved_non_integral_constant_expression_p = false;
4690 is_member_access = false;
4692 is_builtin_constant_p
4693 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4694 if (is_builtin_constant_p)
4696 /* The whole point of __builtin_constant_p is to allow
4697 non-constant expressions to appear as arguments. */
4698 saved_integral_constant_expression_p
4699 = parser->integral_constant_expression_p;
4700 saved_non_integral_constant_expression_p
4701 = parser->non_integral_constant_expression_p;
4702 parser->integral_constant_expression_p = false;
4704 args = (cp_parser_parenthesized_expression_list
4705 (parser, /*is_attribute_list=*/false,
4706 /*cast_p=*/false, /*allow_expansion_p=*/true,
4707 /*non_constant_p=*/NULL));
4708 if (is_builtin_constant_p)
4710 parser->integral_constant_expression_p
4711 = saved_integral_constant_expression_p;
4712 parser->non_integral_constant_expression_p
4713 = saved_non_integral_constant_expression_p;
4716 if (args == error_mark_node)
4718 postfix_expression = error_mark_node;
4722 /* Function calls are not permitted in
4723 constant-expressions. */
4724 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4725 && cp_parser_non_integral_constant_expression (parser,
4728 postfix_expression = error_mark_node;
4733 if (idk == CP_ID_KIND_UNQUALIFIED
4734 || idk == CP_ID_KIND_TEMPLATE_ID)
4736 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4741 if (!any_type_dependent_arguments_p (args))
4743 = perform_koenig_lookup (postfix_expression, args);
4747 = unqualified_fn_lookup_error (postfix_expression);
4749 /* We do not perform argument-dependent lookup if
4750 normal lookup finds a non-function, in accordance
4751 with the expected resolution of DR 218. */
4752 else if (args && is_overloaded_fn (postfix_expression))
4754 tree fn = get_first_fn (postfix_expression);
4756 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4757 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4759 /* Only do argument dependent lookup if regular
4760 lookup does not find a set of member functions.
4761 [basic.lookup.koenig]/2a */
4762 if (!DECL_FUNCTION_MEMBER_P (fn))
4765 if (!any_type_dependent_arguments_p (args))
4767 = perform_koenig_lookup (postfix_expression, args);
4772 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4774 tree instance = TREE_OPERAND (postfix_expression, 0);
4775 tree fn = TREE_OPERAND (postfix_expression, 1);
4777 if (processing_template_decl
4778 && (type_dependent_expression_p (instance)
4779 || (!BASELINK_P (fn)
4780 && TREE_CODE (fn) != FIELD_DECL)
4781 || type_dependent_expression_p (fn)
4782 || any_type_dependent_arguments_p (args)))
4785 = build_nt_call_list (postfix_expression, args);
4789 if (BASELINK_P (fn))
4792 = (build_new_method_call
4793 (instance, fn, args, NULL_TREE,
4794 (idk == CP_ID_KIND_QUALIFIED
4795 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4797 tf_warning_or_error));
4801 = finish_call_expr (postfix_expression, args,
4802 /*disallow_virtual=*/false,
4804 tf_warning_or_error);
4806 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4807 || TREE_CODE (postfix_expression) == MEMBER_REF
4808 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4809 postfix_expression = (build_offset_ref_call_from_tree
4810 (postfix_expression, args));
4811 else if (idk == CP_ID_KIND_QUALIFIED)
4812 /* A call to a static class member, or a namespace-scope
4815 = finish_call_expr (postfix_expression, args,
4816 /*disallow_virtual=*/true,
4818 tf_warning_or_error);
4820 /* All other function calls. */
4822 = finish_call_expr (postfix_expression, args,
4823 /*disallow_virtual=*/false,
4825 tf_warning_or_error);
4827 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4828 idk = CP_ID_KIND_NONE;
4834 /* postfix-expression . template [opt] id-expression
4835 postfix-expression . pseudo-destructor-name
4836 postfix-expression -> template [opt] id-expression
4837 postfix-expression -> pseudo-destructor-name */
4839 /* Consume the `.' or `->' operator. */
4840 cp_lexer_consume_token (parser->lexer);
4843 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4848 is_member_access = true;
4852 /* postfix-expression ++ */
4853 /* Consume the `++' token. */
4854 cp_lexer_consume_token (parser->lexer);
4855 /* Generate a representation for the complete expression. */
4857 = finish_increment_expr (postfix_expression,
4858 POSTINCREMENT_EXPR);
4859 /* Increments may not appear in constant-expressions. */
4860 if (cp_parser_non_integral_constant_expression (parser,
4862 postfix_expression = error_mark_node;
4863 idk = CP_ID_KIND_NONE;
4864 is_member_access = false;
4867 case CPP_MINUS_MINUS:
4868 /* postfix-expression -- */
4869 /* Consume the `--' token. */
4870 cp_lexer_consume_token (parser->lexer);
4871 /* Generate a representation for the complete expression. */
4873 = finish_increment_expr (postfix_expression,
4874 POSTDECREMENT_EXPR);
4875 /* Decrements may not appear in constant-expressions. */
4876 if (cp_parser_non_integral_constant_expression (parser,
4878 postfix_expression = error_mark_node;
4879 idk = CP_ID_KIND_NONE;
4880 is_member_access = false;
4884 if (pidk_return != NULL)
4885 * pidk_return = idk;
4886 if (member_access_only_p)
4887 return is_member_access? postfix_expression : error_mark_node;
4889 return postfix_expression;
4893 /* We should never get here. */
4895 return error_mark_node;
4898 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4899 by cp_parser_builtin_offsetof. We're looking for
4901 postfix-expression [ expression ]
4903 FOR_OFFSETOF is set if we're being called in that context, which
4904 changes how we deal with integer constant expressions. */
4907 cp_parser_postfix_open_square_expression (cp_parser *parser,
4908 tree postfix_expression,
4913 /* Consume the `[' token. */
4914 cp_lexer_consume_token (parser->lexer);
4916 /* Parse the index expression. */
4917 /* ??? For offsetof, there is a question of what to allow here. If
4918 offsetof is not being used in an integral constant expression context,
4919 then we *could* get the right answer by computing the value at runtime.
4920 If we are in an integral constant expression context, then we might
4921 could accept any constant expression; hard to say without analysis.
4922 Rather than open the barn door too wide right away, allow only integer
4923 constant expressions here. */
4925 index = cp_parser_constant_expression (parser, false, NULL);
4927 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4929 /* Look for the closing `]'. */
4930 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4932 /* Build the ARRAY_REF. */
4933 postfix_expression = grok_array_decl (postfix_expression, index);
4935 /* When not doing offsetof, array references are not permitted in
4936 constant-expressions. */
4938 && (cp_parser_non_integral_constant_expression
4939 (parser, "an array reference")))
4940 postfix_expression = error_mark_node;
4942 return postfix_expression;
4945 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4946 by cp_parser_builtin_offsetof. We're looking for
4948 postfix-expression . template [opt] id-expression
4949 postfix-expression . pseudo-destructor-name
4950 postfix-expression -> template [opt] id-expression
4951 postfix-expression -> pseudo-destructor-name
4953 FOR_OFFSETOF is set if we're being called in that context. That sorta
4954 limits what of the above we'll actually accept, but nevermind.
4955 TOKEN_TYPE is the "." or "->" token, which will already have been
4956 removed from the stream. */
4959 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4960 enum cpp_ttype token_type,
4961 tree postfix_expression,
4962 bool for_offsetof, cp_id_kind *idk,
4963 location_t location)
4967 bool pseudo_destructor_p;
4968 tree scope = NULL_TREE;
4970 /* If this is a `->' operator, dereference the pointer. */
4971 if (token_type == CPP_DEREF)
4972 postfix_expression = build_x_arrow (postfix_expression);
4973 /* Check to see whether or not the expression is type-dependent. */
4974 dependent_p = type_dependent_expression_p (postfix_expression);
4975 /* The identifier following the `->' or `.' is not qualified. */
4976 parser->scope = NULL_TREE;
4977 parser->qualifying_scope = NULL_TREE;
4978 parser->object_scope = NULL_TREE;
4979 *idk = CP_ID_KIND_NONE;
4981 /* Enter the scope corresponding to the type of the object
4982 given by the POSTFIX_EXPRESSION. */
4983 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4985 scope = TREE_TYPE (postfix_expression);
4986 /* According to the standard, no expression should ever have
4987 reference type. Unfortunately, we do not currently match
4988 the standard in this respect in that our internal representation
4989 of an expression may have reference type even when the standard
4990 says it does not. Therefore, we have to manually obtain the
4991 underlying type here. */
4992 scope = non_reference (scope);
4993 /* The type of the POSTFIX_EXPRESSION must be complete. */
4994 if (scope == unknown_type_node)
4996 error ("%H%qE does not have class type", &location, postfix_expression);
5000 scope = complete_type_or_else (scope, NULL_TREE);
5001 /* Let the name lookup machinery know that we are processing a
5002 class member access expression. */
5003 parser->context->object_type = scope;
5004 /* If something went wrong, we want to be able to discern that case,
5005 as opposed to the case where there was no SCOPE due to the type
5006 of expression being dependent. */
5008 scope = error_mark_node;
5009 /* If the SCOPE was erroneous, make the various semantic analysis
5010 functions exit quickly -- and without issuing additional error
5012 if (scope == error_mark_node)
5013 postfix_expression = error_mark_node;
5016 /* Assume this expression is not a pseudo-destructor access. */
5017 pseudo_destructor_p = false;
5019 /* If the SCOPE is a scalar type, then, if this is a valid program,
5020 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5021 is type dependent, it can be pseudo-destructor-name or something else.
5022 Try to parse it as pseudo-destructor-name first. */
5023 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5028 cp_parser_parse_tentatively (parser);
5029 /* Parse the pseudo-destructor-name. */
5031 cp_parser_pseudo_destructor_name (parser, &s, &type);
5033 && (cp_parser_error_occurred (parser)
5034 || TREE_CODE (type) != TYPE_DECL
5035 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5036 cp_parser_abort_tentative_parse (parser);
5037 else if (cp_parser_parse_definitely (parser))
5039 pseudo_destructor_p = true;
5041 = finish_pseudo_destructor_expr (postfix_expression,
5042 s, TREE_TYPE (type));
5046 if (!pseudo_destructor_p)
5048 /* If the SCOPE is not a scalar type, we are looking at an
5049 ordinary class member access expression, rather than a
5050 pseudo-destructor-name. */
5052 cp_token *token = cp_lexer_peek_token (parser->lexer);
5053 /* Parse the id-expression. */
5054 name = (cp_parser_id_expression
5056 cp_parser_optional_template_keyword (parser),
5057 /*check_dependency_p=*/true,
5059 /*declarator_p=*/false,
5060 /*optional_p=*/false));
5061 /* In general, build a SCOPE_REF if the member name is qualified.
5062 However, if the name was not dependent and has already been
5063 resolved; there is no need to build the SCOPE_REF. For example;
5065 struct X { void f(); };
5066 template <typename T> void f(T* t) { t->X::f(); }
5068 Even though "t" is dependent, "X::f" is not and has been resolved
5069 to a BASELINK; there is no need to include scope information. */
5071 /* But we do need to remember that there was an explicit scope for
5072 virtual function calls. */
5074 *idk = CP_ID_KIND_QUALIFIED;
5076 /* If the name is a template-id that names a type, we will get a
5077 TYPE_DECL here. That is invalid code. */
5078 if (TREE_CODE (name) == TYPE_DECL)
5080 error ("%Hinvalid use of %qD", &token->location, name);
5081 postfix_expression = error_mark_node;
5085 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5087 name = build_qualified_name (/*type=*/NULL_TREE,
5091 parser->scope = NULL_TREE;
5092 parser->qualifying_scope = NULL_TREE;
5093 parser->object_scope = NULL_TREE;
5095 if (scope && name && BASELINK_P (name))
5096 adjust_result_of_qualified_name_lookup
5097 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5099 = finish_class_member_access_expr (postfix_expression, name,
5101 tf_warning_or_error);
5105 /* We no longer need to look up names in the scope of the object on
5106 the left-hand side of the `.' or `->' operator. */
5107 parser->context->object_type = NULL_TREE;
5109 /* Outside of offsetof, these operators may not appear in
5110 constant-expressions. */
5112 && (cp_parser_non_integral_constant_expression
5113 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5114 postfix_expression = error_mark_node;
5116 return postfix_expression;
5119 /* Parse a parenthesized expression-list.
5122 assignment-expression
5123 expression-list, assignment-expression
5128 identifier, expression-list
5130 CAST_P is true if this expression is the target of a cast.
5132 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5135 Returns a TREE_LIST. The TREE_VALUE of each node is a
5136 representation of an assignment-expression. Note that a TREE_LIST
5137 is returned even if there is only a single expression in the list.
5138 error_mark_node is returned if the ( and or ) are
5139 missing. NULL_TREE is returned on no expressions. The parentheses
5140 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5141 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5142 indicates whether or not all of the expressions in the list were
5146 cp_parser_parenthesized_expression_list (cp_parser* parser,
5147 bool is_attribute_list,
5149 bool allow_expansion_p,
5150 bool *non_constant_p)
5152 tree expression_list = NULL_TREE;
5153 bool fold_expr_p = is_attribute_list;
5154 tree identifier = NULL_TREE;
5155 bool saved_greater_than_is_operator_p;
5157 /* Assume all the expressions will be constant. */
5159 *non_constant_p = false;
5161 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5162 return error_mark_node;
5164 /* Within a parenthesized expression, a `>' token is always
5165 the greater-than operator. */
5166 saved_greater_than_is_operator_p
5167 = parser->greater_than_is_operator_p;
5168 parser->greater_than_is_operator_p = true;
5170 /* Consume expressions until there are no more. */
5171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5176 /* At the beginning of attribute lists, check to see if the
5177 next token is an identifier. */
5178 if (is_attribute_list
5179 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5183 /* Consume the identifier. */
5184 token = cp_lexer_consume_token (parser->lexer);
5185 /* Save the identifier. */
5186 identifier = token->u.value;
5190 bool expr_non_constant_p;
5192 /* Parse the next assignment-expression. */
5193 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5195 /* A braced-init-list. */
5196 maybe_warn_cpp0x ("extended initializer lists");
5197 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5198 if (non_constant_p && expr_non_constant_p)
5199 *non_constant_p = true;
5201 else if (non_constant_p)
5203 expr = (cp_parser_constant_expression
5204 (parser, /*allow_non_constant_p=*/true,
5205 &expr_non_constant_p));
5206 if (expr_non_constant_p)
5207 *non_constant_p = true;
5210 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5213 expr = fold_non_dependent_expr (expr);
5215 /* If we have an ellipsis, then this is an expression
5217 if (allow_expansion_p
5218 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5220 /* Consume the `...'. */
5221 cp_lexer_consume_token (parser->lexer);
5223 /* Build the argument pack. */
5224 expr = make_pack_expansion (expr);
5227 /* Add it to the list. We add error_mark_node
5228 expressions to the list, so that we can still tell if
5229 the correct form for a parenthesized expression-list
5230 is found. That gives better errors. */
5231 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5233 if (expr == error_mark_node)
5237 /* After the first item, attribute lists look the same as
5238 expression lists. */
5239 is_attribute_list = false;
5242 /* If the next token isn't a `,', then we are done. */
5243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5246 /* Otherwise, consume the `,' and keep going. */
5247 cp_lexer_consume_token (parser->lexer);
5250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5255 /* We try and resync to an unnested comma, as that will give the
5256 user better diagnostics. */
5257 ending = cp_parser_skip_to_closing_parenthesis (parser,
5258 /*recovering=*/true,
5260 /*consume_paren=*/true);
5265 parser->greater_than_is_operator_p
5266 = saved_greater_than_is_operator_p;
5267 return error_mark_node;
5271 parser->greater_than_is_operator_p
5272 = saved_greater_than_is_operator_p;
5274 /* We built up the list in reverse order so we must reverse it now. */
5275 expression_list = nreverse (expression_list);
5277 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5279 return expression_list;
5282 /* Parse a pseudo-destructor-name.
5284 pseudo-destructor-name:
5285 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5286 :: [opt] nested-name-specifier template template-id :: ~ type-name
5287 :: [opt] nested-name-specifier [opt] ~ type-name
5289 If either of the first two productions is used, sets *SCOPE to the
5290 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5291 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5292 or ERROR_MARK_NODE if the parse fails. */
5295 cp_parser_pseudo_destructor_name (cp_parser* parser,
5299 bool nested_name_specifier_p;
5301 /* Assume that things will not work out. */
5302 *type = error_mark_node;
5304 /* Look for the optional `::' operator. */
5305 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5306 /* Look for the optional nested-name-specifier. */
5307 nested_name_specifier_p
5308 = (cp_parser_nested_name_specifier_opt (parser,
5309 /*typename_keyword_p=*/false,
5310 /*check_dependency_p=*/true,
5312 /*is_declaration=*/false)
5314 /* Now, if we saw a nested-name-specifier, we might be doing the
5315 second production. */
5316 if (nested_name_specifier_p
5317 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5319 /* Consume the `template' keyword. */
5320 cp_lexer_consume_token (parser->lexer);
5321 /* Parse the template-id. */
5322 cp_parser_template_id (parser,
5323 /*template_keyword_p=*/true,
5324 /*check_dependency_p=*/false,
5325 /*is_declaration=*/true);
5326 /* Look for the `::' token. */
5327 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5329 /* If the next token is not a `~', then there might be some
5330 additional qualification. */
5331 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5333 /* At this point, we're looking for "type-name :: ~". The type-name
5334 must not be a class-name, since this is a pseudo-destructor. So,
5335 it must be either an enum-name, or a typedef-name -- both of which
5336 are just identifiers. So, we peek ahead to check that the "::"
5337 and "~" tokens are present; if they are not, then we can avoid
5338 calling type_name. */
5339 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5340 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5341 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5343 cp_parser_error (parser, "non-scalar type");
5347 /* Look for the type-name. */
5348 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5349 if (*scope == error_mark_node)
5352 /* Look for the `::' token. */
5353 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5358 /* Look for the `~'. */
5359 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5360 /* Look for the type-name again. We are not responsible for
5361 checking that it matches the first type-name. */
5362 *type = cp_parser_nonclass_name (parser);
5365 /* Parse a unary-expression.
5371 unary-operator cast-expression
5372 sizeof unary-expression
5380 __extension__ cast-expression
5381 __alignof__ unary-expression
5382 __alignof__ ( type-id )
5383 __real__ cast-expression
5384 __imag__ cast-expression
5387 ADDRESS_P is true iff the unary-expression is appearing as the
5388 operand of the `&' operator. CAST_P is true if this expression is
5389 the target of a cast.
5391 Returns a representation of the expression. */
5394 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5398 enum tree_code unary_operator;
5400 /* Peek at the next token. */
5401 token = cp_lexer_peek_token (parser->lexer);
5402 /* Some keywords give away the kind of expression. */
5403 if (token->type == CPP_KEYWORD)
5405 enum rid keyword = token->keyword;
5415 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5416 /* Consume the token. */
5417 cp_lexer_consume_token (parser->lexer);
5418 /* Parse the operand. */
5419 operand = cp_parser_sizeof_operand (parser, keyword);
5421 if (TYPE_P (operand))
5422 return cxx_sizeof_or_alignof_type (operand, op, true);
5424 return cxx_sizeof_or_alignof_expr (operand, op, true);
5428 return cp_parser_new_expression (parser);
5431 return cp_parser_delete_expression (parser);
5435 /* The saved value of the PEDANTIC flag. */
5439 /* Save away the PEDANTIC flag. */
5440 cp_parser_extension_opt (parser, &saved_pedantic);
5441 /* Parse the cast-expression. */
5442 expr = cp_parser_simple_cast_expression (parser);
5443 /* Restore the PEDANTIC flag. */
5444 pedantic = saved_pedantic;
5454 /* Consume the `__real__' or `__imag__' token. */
5455 cp_lexer_consume_token (parser->lexer);
5456 /* Parse the cast-expression. */
5457 expression = cp_parser_simple_cast_expression (parser);
5458 /* Create the complete representation. */
5459 return build_x_unary_op ((keyword == RID_REALPART
5460 ? REALPART_EXPR : IMAGPART_EXPR),
5462 tf_warning_or_error);
5471 /* Look for the `:: new' and `:: delete', which also signal the
5472 beginning of a new-expression, or delete-expression,
5473 respectively. If the next token is `::', then it might be one of
5475 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5479 /* See if the token after the `::' is one of the keywords in
5480 which we're interested. */
5481 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5482 /* If it's `new', we have a new-expression. */
5483 if (keyword == RID_NEW)
5484 return cp_parser_new_expression (parser);
5485 /* Similarly, for `delete'. */
5486 else if (keyword == RID_DELETE)
5487 return cp_parser_delete_expression (parser);
5490 /* Look for a unary operator. */
5491 unary_operator = cp_parser_unary_operator (token);
5492 /* The `++' and `--' operators can be handled similarly, even though
5493 they are not technically unary-operators in the grammar. */
5494 if (unary_operator == ERROR_MARK)
5496 if (token->type == CPP_PLUS_PLUS)
5497 unary_operator = PREINCREMENT_EXPR;
5498 else if (token->type == CPP_MINUS_MINUS)
5499 unary_operator = PREDECREMENT_EXPR;
5500 /* Handle the GNU address-of-label extension. */
5501 else if (cp_parser_allow_gnu_extensions_p (parser)
5502 && token->type == CPP_AND_AND)
5506 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5508 /* Consume the '&&' token. */
5509 cp_lexer_consume_token (parser->lexer);
5510 /* Look for the identifier. */
5511 identifier = cp_parser_identifier (parser);
5512 /* Create an expression representing the address. */
5513 expression = finish_label_address_expr (identifier, loc);
5514 if (cp_parser_non_integral_constant_expression (parser,
5515 "the address of a label"))
5516 expression = error_mark_node;
5520 if (unary_operator != ERROR_MARK)
5522 tree cast_expression;
5523 tree expression = error_mark_node;
5524 const char *non_constant_p = NULL;
5526 /* Consume the operator token. */
5527 token = cp_lexer_consume_token (parser->lexer);
5528 /* Parse the cast-expression. */
5530 = cp_parser_cast_expression (parser,
5531 unary_operator == ADDR_EXPR,
5532 /*cast_p=*/false, pidk);
5533 /* Now, build an appropriate representation. */
5534 switch (unary_operator)
5537 non_constant_p = "%<*%>";
5538 expression = build_x_indirect_ref (cast_expression, "unary *",
5539 tf_warning_or_error);
5543 non_constant_p = "%<&%>";
5546 expression = build_x_unary_op (unary_operator, cast_expression,
5547 tf_warning_or_error);
5550 case PREINCREMENT_EXPR:
5551 case PREDECREMENT_EXPR:
5552 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5553 ? "%<++%>" : "%<--%>");
5555 case UNARY_PLUS_EXPR:
5557 case TRUTH_NOT_EXPR:
5558 expression = finish_unary_op_expr (unary_operator, cast_expression);
5566 && cp_parser_non_integral_constant_expression (parser,
5568 expression = error_mark_node;
5573 return cp_parser_postfix_expression (parser, address_p, cast_p,
5574 /*member_access_only_p=*/false,
5578 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5579 unary-operator, the corresponding tree code is returned. */
5581 static enum tree_code
5582 cp_parser_unary_operator (cp_token* token)
5584 switch (token->type)
5587 return INDIRECT_REF;
5593 return UNARY_PLUS_EXPR;
5599 return TRUTH_NOT_EXPR;
5602 return BIT_NOT_EXPR;
5609 /* Parse a new-expression.
5612 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5613 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5615 Returns a representation of the expression. */
5618 cp_parser_new_expression (cp_parser* parser)
5620 bool global_scope_p;
5626 /* Look for the optional `::' operator. */
5628 = (cp_parser_global_scope_opt (parser,
5629 /*current_scope_valid_p=*/false)
5631 /* Look for the `new' operator. */
5632 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5633 /* There's no easy way to tell a new-placement from the
5634 `( type-id )' construct. */
5635 cp_parser_parse_tentatively (parser);
5636 /* Look for a new-placement. */
5637 placement = cp_parser_new_placement (parser);
5638 /* If that didn't work out, there's no new-placement. */
5639 if (!cp_parser_parse_definitely (parser))
5640 placement = NULL_TREE;
5642 /* If the next token is a `(', then we have a parenthesized
5644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5647 /* Consume the `('. */
5648 cp_lexer_consume_token (parser->lexer);
5649 /* Parse the type-id. */
5650 type = cp_parser_type_id (parser);
5651 /* Look for the closing `)'. */
5652 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5653 token = cp_lexer_peek_token (parser->lexer);
5654 /* There should not be a direct-new-declarator in this production,
5655 but GCC used to allowed this, so we check and emit a sensible error
5656 message for this case. */
5657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5659 error ("%Harray bound forbidden after parenthesized type-id",
5661 inform (token->location,
5662 "try removing the parentheses around the type-id");
5663 cp_parser_direct_new_declarator (parser);
5667 /* Otherwise, there must be a new-type-id. */
5669 type = cp_parser_new_type_id (parser, &nelts);
5671 /* If the next token is a `(' or '{', then we have a new-initializer. */
5672 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5673 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5674 initializer = cp_parser_new_initializer (parser);
5676 initializer = NULL_TREE;
5678 /* A new-expression may not appear in an integral constant
5680 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5681 return error_mark_node;
5683 /* Create a representation of the new-expression. */
5684 return build_new (placement, type, nelts, initializer, global_scope_p,
5685 tf_warning_or_error);
5688 /* Parse a new-placement.
5693 Returns the same representation as for an expression-list. */
5696 cp_parser_new_placement (cp_parser* parser)
5698 tree expression_list;
5700 /* Parse the expression-list. */
5701 expression_list = (cp_parser_parenthesized_expression_list
5702 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5703 /*non_constant_p=*/NULL));
5705 return expression_list;
5708 /* Parse a new-type-id.
5711 type-specifier-seq new-declarator [opt]
5713 Returns the TYPE allocated. If the new-type-id indicates an array
5714 type, *NELTS is set to the number of elements in the last array
5715 bound; the TYPE will not include the last array bound. */
5718 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5720 cp_decl_specifier_seq type_specifier_seq;
5721 cp_declarator *new_declarator;
5722 cp_declarator *declarator;
5723 cp_declarator *outer_declarator;
5724 const char *saved_message;
5727 /* The type-specifier sequence must not contain type definitions.
5728 (It cannot contain declarations of new types either, but if they
5729 are not definitions we will catch that because they are not
5731 saved_message = parser->type_definition_forbidden_message;
5732 parser->type_definition_forbidden_message
5733 = "types may not be defined in a new-type-id";
5734 /* Parse the type-specifier-seq. */
5735 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5736 &type_specifier_seq);
5737 /* Restore the old message. */
5738 parser->type_definition_forbidden_message = saved_message;
5739 /* Parse the new-declarator. */
5740 new_declarator = cp_parser_new_declarator_opt (parser);
5742 /* Determine the number of elements in the last array dimension, if
5745 /* Skip down to the last array dimension. */
5746 declarator = new_declarator;
5747 outer_declarator = NULL;
5748 while (declarator && (declarator->kind == cdk_pointer
5749 || declarator->kind == cdk_ptrmem))
5751 outer_declarator = declarator;
5752 declarator = declarator->declarator;
5755 && declarator->kind == cdk_array
5756 && declarator->declarator
5757 && declarator->declarator->kind == cdk_array)
5759 outer_declarator = declarator;
5760 declarator = declarator->declarator;
5763 if (declarator && declarator->kind == cdk_array)
5765 *nelts = declarator->u.array.bounds;
5766 if (*nelts == error_mark_node)
5767 *nelts = integer_one_node;
5769 if (outer_declarator)
5770 outer_declarator->declarator = declarator->declarator;
5772 new_declarator = NULL;
5775 type = groktypename (&type_specifier_seq, new_declarator, false);
5779 /* Parse an (optional) new-declarator.
5782 ptr-operator new-declarator [opt]
5783 direct-new-declarator
5785 Returns the declarator. */
5787 static cp_declarator *
5788 cp_parser_new_declarator_opt (cp_parser* parser)
5790 enum tree_code code;
5792 cp_cv_quals cv_quals;
5794 /* We don't know if there's a ptr-operator next, or not. */
5795 cp_parser_parse_tentatively (parser);
5796 /* Look for a ptr-operator. */
5797 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5798 /* If that worked, look for more new-declarators. */
5799 if (cp_parser_parse_definitely (parser))
5801 cp_declarator *declarator;
5803 /* Parse another optional declarator. */
5804 declarator = cp_parser_new_declarator_opt (parser);
5806 return cp_parser_make_indirect_declarator
5807 (code, type, cv_quals, declarator);
5810 /* If the next token is a `[', there is a direct-new-declarator. */
5811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5812 return cp_parser_direct_new_declarator (parser);
5817 /* Parse a direct-new-declarator.
5819 direct-new-declarator:
5821 direct-new-declarator [constant-expression]
5825 static cp_declarator *
5826 cp_parser_direct_new_declarator (cp_parser* parser)
5828 cp_declarator *declarator = NULL;
5834 /* Look for the opening `['. */
5835 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5836 /* The first expression is not required to be constant. */
5839 cp_token *token = cp_lexer_peek_token (parser->lexer);
5840 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5841 /* The standard requires that the expression have integral
5842 type. DR 74 adds enumeration types. We believe that the
5843 real intent is that these expressions be handled like the
5844 expression in a `switch' condition, which also allows
5845 classes with a single conversion to integral or
5846 enumeration type. */
5847 if (!processing_template_decl)
5850 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5855 error ("%Hexpression in new-declarator must have integral "
5856 "or enumeration type", &token->location);
5857 expression = error_mark_node;
5861 /* But all the other expressions must be. */
5864 = cp_parser_constant_expression (parser,
5865 /*allow_non_constant=*/false,
5867 /* Look for the closing `]'. */
5868 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5870 /* Add this bound to the declarator. */
5871 declarator = make_array_declarator (declarator, expression);
5873 /* If the next token is not a `[', then there are no more
5875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5882 /* Parse a new-initializer.
5885 ( expression-list [opt] )
5888 Returns a representation of the expression-list. If there is no
5889 expression-list, VOID_ZERO_NODE is returned. */
5892 cp_parser_new_initializer (cp_parser* parser)
5894 tree expression_list;
5896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5898 bool expr_non_constant_p;
5899 maybe_warn_cpp0x ("extended initializer lists");
5900 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5901 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5902 expression_list = build_tree_list (NULL_TREE, expression_list);
5905 expression_list = (cp_parser_parenthesized_expression_list
5906 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5907 /*non_constant_p=*/NULL));
5908 if (!expression_list)
5909 expression_list = void_zero_node;
5911 return expression_list;
5914 /* Parse a delete-expression.
5917 :: [opt] delete cast-expression
5918 :: [opt] delete [ ] cast-expression
5920 Returns a representation of the expression. */
5923 cp_parser_delete_expression (cp_parser* parser)
5925 bool global_scope_p;
5929 /* Look for the optional `::' operator. */
5931 = (cp_parser_global_scope_opt (parser,
5932 /*current_scope_valid_p=*/false)
5934 /* Look for the `delete' keyword. */
5935 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5936 /* See if the array syntax is in use. */
5937 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5939 /* Consume the `[' token. */
5940 cp_lexer_consume_token (parser->lexer);
5941 /* Look for the `]' token. */
5942 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5943 /* Remember that this is the `[]' construct. */
5949 /* Parse the cast-expression. */
5950 expression = cp_parser_simple_cast_expression (parser);
5952 /* A delete-expression may not appear in an integral constant
5954 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5955 return error_mark_node;
5957 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5960 /* Returns true if TOKEN may start a cast-expression and false
5964 cp_parser_token_starts_cast_expression (cp_token *token)
5966 switch (token->type)
5972 case CPP_CLOSE_SQUARE:
5973 case CPP_CLOSE_PAREN:
5974 case CPP_CLOSE_BRACE:
5978 case CPP_DEREF_STAR:
5986 case CPP_GREATER_EQ:
6006 /* '[' may start a primary-expression in obj-c++. */
6007 case CPP_OPEN_SQUARE:
6008 return c_dialect_objc ();
6015 /* Parse a cast-expression.
6019 ( type-id ) cast-expression
6021 ADDRESS_P is true iff the unary-expression is appearing as the
6022 operand of the `&' operator. CAST_P is true if this expression is
6023 the target of a cast.
6025 Returns a representation of the expression. */
6028 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6031 /* If it's a `(', then we might be looking at a cast. */
6032 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6034 tree type = NULL_TREE;
6035 tree expr = NULL_TREE;
6036 bool compound_literal_p;
6037 const char *saved_message;
6039 /* There's no way to know yet whether or not this is a cast.
6040 For example, `(int (3))' is a unary-expression, while `(int)
6041 3' is a cast. So, we resort to parsing tentatively. */
6042 cp_parser_parse_tentatively (parser);
6043 /* Types may not be defined in a cast. */
6044 saved_message = parser->type_definition_forbidden_message;
6045 parser->type_definition_forbidden_message
6046 = "types may not be defined in casts";
6047 /* Consume the `('. */
6048 cp_lexer_consume_token (parser->lexer);
6049 /* A very tricky bit is that `(struct S) { 3 }' is a
6050 compound-literal (which we permit in C++ as an extension).
6051 But, that construct is not a cast-expression -- it is a
6052 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6053 is legal; if the compound-literal were a cast-expression,
6054 you'd need an extra set of parentheses.) But, if we parse
6055 the type-id, and it happens to be a class-specifier, then we
6056 will commit to the parse at that point, because we cannot
6057 undo the action that is done when creating a new class. So,
6058 then we cannot back up and do a postfix-expression.
6060 Therefore, we scan ahead to the closing `)', and check to see
6061 if the token after the `)' is a `{'. If so, we are not
6062 looking at a cast-expression.
6064 Save tokens so that we can put them back. */
6065 cp_lexer_save_tokens (parser->lexer);
6066 /* Skip tokens until the next token is a closing parenthesis.
6067 If we find the closing `)', and the next token is a `{', then
6068 we are looking at a compound-literal. */
6070 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6071 /*consume_paren=*/true)
6072 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6073 /* Roll back the tokens we skipped. */
6074 cp_lexer_rollback_tokens (parser->lexer);
6075 /* If we were looking at a compound-literal, simulate an error
6076 so that the call to cp_parser_parse_definitely below will
6078 if (compound_literal_p)
6079 cp_parser_simulate_error (parser);
6082 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6083 parser->in_type_id_in_expr_p = true;
6084 /* Look for the type-id. */
6085 type = cp_parser_type_id (parser);
6086 /* Look for the closing `)'. */
6087 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6088 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6091 /* Restore the saved message. */
6092 parser->type_definition_forbidden_message = saved_message;
6094 /* At this point this can only be either a cast or a
6095 parenthesized ctor such as `(T ())' that looks like a cast to
6096 function returning T. */
6097 if (!cp_parser_error_occurred (parser)
6098 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6101 cp_parser_parse_definitely (parser);
6102 expr = cp_parser_cast_expression (parser,
6103 /*address_p=*/false,
6104 /*cast_p=*/true, pidk);
6106 /* Warn about old-style casts, if so requested. */
6107 if (warn_old_style_cast
6108 && !in_system_header
6109 && !VOID_TYPE_P (type)
6110 && current_lang_name != lang_name_c)
6111 warning (OPT_Wold_style_cast, "use of old-style cast");
6113 /* Only type conversions to integral or enumeration types
6114 can be used in constant-expressions. */
6115 if (!cast_valid_in_integral_constant_expression_p (type)
6116 && (cp_parser_non_integral_constant_expression
6118 "a cast to a type other than an integral or "
6119 "enumeration type")))
6120 return error_mark_node;
6122 /* Perform the cast. */
6123 expr = build_c_cast (type, expr);
6127 cp_parser_abort_tentative_parse (parser);
6130 /* If we get here, then it's not a cast, so it must be a
6131 unary-expression. */
6132 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6135 /* Parse a binary expression of the general form:
6139 pm-expression .* cast-expression
6140 pm-expression ->* cast-expression
6142 multiplicative-expression:
6144 multiplicative-expression * pm-expression
6145 multiplicative-expression / pm-expression
6146 multiplicative-expression % pm-expression
6148 additive-expression:
6149 multiplicative-expression
6150 additive-expression + multiplicative-expression
6151 additive-expression - multiplicative-expression
6155 shift-expression << additive-expression
6156 shift-expression >> additive-expression
6158 relational-expression:
6160 relational-expression < shift-expression
6161 relational-expression > shift-expression
6162 relational-expression <= shift-expression
6163 relational-expression >= shift-expression
6167 relational-expression:
6168 relational-expression <? shift-expression
6169 relational-expression >? shift-expression
6171 equality-expression:
6172 relational-expression
6173 equality-expression == relational-expression
6174 equality-expression != relational-expression
6178 and-expression & equality-expression
6180 exclusive-or-expression:
6182 exclusive-or-expression ^ and-expression
6184 inclusive-or-expression:
6185 exclusive-or-expression
6186 inclusive-or-expression | exclusive-or-expression
6188 logical-and-expression:
6189 inclusive-or-expression
6190 logical-and-expression && inclusive-or-expression
6192 logical-or-expression:
6193 logical-and-expression
6194 logical-or-expression || logical-and-expression
6196 All these are implemented with a single function like:
6199 simple-cast-expression
6200 binary-expression <token> binary-expression
6202 CAST_P is true if this expression is the target of a cast.
6204 The binops_by_token map is used to get the tree codes for each <token> type.
6205 binary-expressions are associated according to a precedence table. */
6207 #define TOKEN_PRECEDENCE(token) \
6208 (((token->type == CPP_GREATER \
6209 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6210 && !parser->greater_than_is_operator_p) \
6211 ? PREC_NOT_OPERATOR \
6212 : binops_by_token[token->type].prec)
6215 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6216 bool no_toplevel_fold_p,
6217 enum cp_parser_prec prec,
6220 cp_parser_expression_stack stack;
6221 cp_parser_expression_stack_entry *sp = &stack[0];
6224 enum tree_code tree_type, lhs_type, rhs_type;
6225 enum cp_parser_prec new_prec, lookahead_prec;
6228 /* Parse the first expression. */
6229 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6230 lhs_type = ERROR_MARK;
6234 /* Get an operator token. */
6235 token = cp_lexer_peek_token (parser->lexer);
6237 if (warn_cxx0x_compat
6238 && token->type == CPP_RSHIFT
6239 && !parser->greater_than_is_operator_p)
6241 warning (OPT_Wc__0x_compat,
6242 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6244 warning (OPT_Wc__0x_compat,
6245 "suggest parentheses around %<>>%> expression");
6248 new_prec = TOKEN_PRECEDENCE (token);
6250 /* Popping an entry off the stack means we completed a subexpression:
6251 - either we found a token which is not an operator (`>' where it is not
6252 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6253 will happen repeatedly;
6254 - or, we found an operator which has lower priority. This is the case
6255 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6257 if (new_prec <= prec)
6266 tree_type = binops_by_token[token->type].tree_type;
6268 /* We used the operator token. */
6269 cp_lexer_consume_token (parser->lexer);
6271 /* Extract another operand. It may be the RHS of this expression
6272 or the LHS of a new, higher priority expression. */
6273 rhs = cp_parser_simple_cast_expression (parser);
6274 rhs_type = ERROR_MARK;
6276 /* Get another operator token. Look up its precedence to avoid
6277 building a useless (immediately popped) stack entry for common
6278 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6279 token = cp_lexer_peek_token (parser->lexer);
6280 lookahead_prec = TOKEN_PRECEDENCE (token);
6281 if (lookahead_prec > new_prec)
6283 /* ... and prepare to parse the RHS of the new, higher priority
6284 expression. Since precedence levels on the stack are
6285 monotonically increasing, we do not have to care about
6288 sp->tree_type = tree_type;
6290 sp->lhs_type = lhs_type;
6293 lhs_type = rhs_type;
6295 new_prec = lookahead_prec;
6299 lookahead_prec = new_prec;
6300 /* If the stack is not empty, we have parsed into LHS the right side
6301 (`4' in the example above) of an expression we had suspended.
6302 We can use the information on the stack to recover the LHS (`3')
6303 from the stack together with the tree code (`MULT_EXPR'), and
6304 the precedence of the higher level subexpression
6305 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6306 which will be used to actually build the additive expression. */
6309 tree_type = sp->tree_type;
6311 rhs_type = lhs_type;
6313 lhs_type = sp->lhs_type;
6316 overloaded_p = false;
6317 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6318 ERROR_MARK for everything that is not a binary expression.
6319 This makes warn_about_parentheses miss some warnings that
6320 involve unary operators. For unary expressions we should
6321 pass the correct tree_code unless the unary expression was
6322 surrounded by parentheses.
6324 if (no_toplevel_fold_p
6325 && lookahead_prec <= prec
6327 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6328 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6330 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6331 &overloaded_p, tf_warning_or_error);
6332 lhs_type = tree_type;
6334 /* If the binary operator required the use of an overloaded operator,
6335 then this expression cannot be an integral constant-expression.
6336 An overloaded operator can be used even if both operands are
6337 otherwise permissible in an integral constant-expression if at
6338 least one of the operands is of enumeration type. */
6341 && (cp_parser_non_integral_constant_expression
6342 (parser, "calls to overloaded operators")))
6343 return error_mark_node;
6350 /* Parse the `? expression : assignment-expression' part of a
6351 conditional-expression. The LOGICAL_OR_EXPR is the
6352 logical-or-expression that started the conditional-expression.
6353 Returns a representation of the entire conditional-expression.
6355 This routine is used by cp_parser_assignment_expression.
6357 ? expression : assignment-expression
6361 ? : assignment-expression */
6364 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6367 tree assignment_expr;
6369 /* Consume the `?' token. */
6370 cp_lexer_consume_token (parser->lexer);
6371 if (cp_parser_allow_gnu_extensions_p (parser)
6372 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6373 /* Implicit true clause. */
6376 /* Parse the expression. */
6377 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6379 /* The next token should be a `:'. */
6380 cp_parser_require (parser, CPP_COLON, "%<:%>");
6381 /* Parse the assignment-expression. */
6382 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6384 /* Build the conditional-expression. */
6385 return build_x_conditional_expr (logical_or_expr,
6388 tf_warning_or_error);
6391 /* Parse an assignment-expression.
6393 assignment-expression:
6394 conditional-expression
6395 logical-or-expression assignment-operator assignment_expression
6398 CAST_P is true if this expression is the target of a cast.
6400 Returns a representation for the expression. */
6403 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6408 /* If the next token is the `throw' keyword, then we're looking at
6409 a throw-expression. */
6410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6411 expr = cp_parser_throw_expression (parser);
6412 /* Otherwise, it must be that we are looking at a
6413 logical-or-expression. */
6416 /* Parse the binary expressions (logical-or-expression). */
6417 expr = cp_parser_binary_expression (parser, cast_p, false,
6418 PREC_NOT_OPERATOR, pidk);
6419 /* If the next token is a `?' then we're actually looking at a
6420 conditional-expression. */
6421 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6422 return cp_parser_question_colon_clause (parser, expr);
6425 enum tree_code assignment_operator;
6427 /* If it's an assignment-operator, we're using the second
6430 = cp_parser_assignment_operator_opt (parser);
6431 if (assignment_operator != ERROR_MARK)
6433 bool non_constant_p;
6435 /* Parse the right-hand side of the assignment. */
6436 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6438 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6439 maybe_warn_cpp0x ("extended initializer lists");
6441 /* An assignment may not appear in a
6442 constant-expression. */
6443 if (cp_parser_non_integral_constant_expression (parser,
6445 return error_mark_node;
6446 /* Build the assignment expression. */
6447 expr = build_x_modify_expr (expr,
6448 assignment_operator,
6450 tf_warning_or_error);
6458 /* Parse an (optional) assignment-operator.
6460 assignment-operator: one of
6461 = *= /= %= += -= >>= <<= &= ^= |=
6465 assignment-operator: one of
6468 If the next token is an assignment operator, the corresponding tree
6469 code is returned, and the token is consumed. For example, for
6470 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6471 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6472 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6473 operator, ERROR_MARK is returned. */
6475 static enum tree_code
6476 cp_parser_assignment_operator_opt (cp_parser* parser)
6481 /* Peek at the next token. */
6482 token = cp_lexer_peek_token (parser->lexer);
6484 switch (token->type)
6495 op = TRUNC_DIV_EXPR;
6499 op = TRUNC_MOD_EXPR;
6531 /* Nothing else is an assignment operator. */
6535 /* If it was an assignment operator, consume it. */
6536 if (op != ERROR_MARK)
6537 cp_lexer_consume_token (parser->lexer);
6542 /* Parse an expression.
6545 assignment-expression
6546 expression , assignment-expression
6548 CAST_P is true if this expression is the target of a cast.
6550 Returns a representation of the expression. */
6553 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6555 tree expression = NULL_TREE;
6559 tree assignment_expression;
6561 /* Parse the next assignment-expression. */
6562 assignment_expression
6563 = cp_parser_assignment_expression (parser, cast_p, pidk);
6564 /* If this is the first assignment-expression, we can just
6567 expression = assignment_expression;
6569 expression = build_x_compound_expr (expression,
6570 assignment_expression,
6571 tf_warning_or_error);
6572 /* If the next token is not a comma, then we are done with the
6574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6576 /* Consume the `,'. */
6577 cp_lexer_consume_token (parser->lexer);
6578 /* A comma operator cannot appear in a constant-expression. */
6579 if (cp_parser_non_integral_constant_expression (parser,
6580 "a comma operator"))
6581 expression = error_mark_node;
6587 /* Parse a constant-expression.
6589 constant-expression:
6590 conditional-expression
6592 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6593 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6594 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6595 is false, NON_CONSTANT_P should be NULL. */
6598 cp_parser_constant_expression (cp_parser* parser,
6599 bool allow_non_constant_p,
6600 bool *non_constant_p)
6602 bool saved_integral_constant_expression_p;
6603 bool saved_allow_non_integral_constant_expression_p;
6604 bool saved_non_integral_constant_expression_p;
6607 /* It might seem that we could simply parse the
6608 conditional-expression, and then check to see if it were
6609 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6610 one that the compiler can figure out is constant, possibly after
6611 doing some simplifications or optimizations. The standard has a
6612 precise definition of constant-expression, and we must honor
6613 that, even though it is somewhat more restrictive.
6619 is not a legal declaration, because `(2, 3)' is not a
6620 constant-expression. The `,' operator is forbidden in a
6621 constant-expression. However, GCC's constant-folding machinery
6622 will fold this operation to an INTEGER_CST for `3'. */
6624 /* Save the old settings. */
6625 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6626 saved_allow_non_integral_constant_expression_p
6627 = parser->allow_non_integral_constant_expression_p;
6628 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6629 /* We are now parsing a constant-expression. */
6630 parser->integral_constant_expression_p = true;
6631 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6632 parser->non_integral_constant_expression_p = false;
6633 /* Although the grammar says "conditional-expression", we parse an
6634 "assignment-expression", which also permits "throw-expression"
6635 and the use of assignment operators. In the case that
6636 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6637 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6638 actually essential that we look for an assignment-expression.
6639 For example, cp_parser_initializer_clauses uses this function to
6640 determine whether a particular assignment-expression is in fact
6642 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6643 /* Restore the old settings. */
6644 parser->integral_constant_expression_p
6645 = saved_integral_constant_expression_p;
6646 parser->allow_non_integral_constant_expression_p
6647 = saved_allow_non_integral_constant_expression_p;
6648 if (allow_non_constant_p)
6649 *non_constant_p = parser->non_integral_constant_expression_p;
6650 else if (parser->non_integral_constant_expression_p)
6651 expression = error_mark_node;
6652 parser->non_integral_constant_expression_p
6653 = saved_non_integral_constant_expression_p;
6658 /* Parse __builtin_offsetof.
6660 offsetof-expression:
6661 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6663 offsetof-member-designator:
6665 | offsetof-member-designator "." id-expression
6666 | offsetof-member-designator "[" expression "]"
6667 | offsetof-member-designator "->" id-expression */
6670 cp_parser_builtin_offsetof (cp_parser *parser)
6672 int save_ice_p, save_non_ice_p;
6677 /* We're about to accept non-integral-constant things, but will
6678 definitely yield an integral constant expression. Save and
6679 restore these values around our local parsing. */
6680 save_ice_p = parser->integral_constant_expression_p;
6681 save_non_ice_p = parser->non_integral_constant_expression_p;
6683 /* Consume the "__builtin_offsetof" token. */
6684 cp_lexer_consume_token (parser->lexer);
6685 /* Consume the opening `('. */
6686 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6687 /* Parse the type-id. */
6688 type = cp_parser_type_id (parser);
6689 /* Look for the `,'. */
6690 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6691 token = cp_lexer_peek_token (parser->lexer);
6693 /* Build the (type *)null that begins the traditional offsetof macro. */
6694 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6695 tf_warning_or_error);
6697 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6698 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6699 true, &dummy, token->location);
6702 token = cp_lexer_peek_token (parser->lexer);
6703 switch (token->type)
6705 case CPP_OPEN_SQUARE:
6706 /* offsetof-member-designator "[" expression "]" */
6707 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6711 /* offsetof-member-designator "->" identifier */
6712 expr = grok_array_decl (expr, integer_zero_node);
6716 /* offsetof-member-designator "." identifier */
6717 cp_lexer_consume_token (parser->lexer);
6718 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6723 case CPP_CLOSE_PAREN:
6724 /* Consume the ")" token. */
6725 cp_lexer_consume_token (parser->lexer);
6729 /* Error. We know the following require will fail, but
6730 that gives the proper error message. */
6731 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6732 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6733 expr = error_mark_node;
6739 /* If we're processing a template, we can't finish the semantics yet.
6740 Otherwise we can fold the entire expression now. */
6741 if (processing_template_decl)
6742 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6744 expr = finish_offsetof (expr);
6747 parser->integral_constant_expression_p = save_ice_p;
6748 parser->non_integral_constant_expression_p = save_non_ice_p;
6753 /* Parse a trait expression. */
6756 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6759 tree type1, type2 = NULL_TREE;
6760 bool binary = false;
6761 cp_decl_specifier_seq decl_specs;
6765 case RID_HAS_NOTHROW_ASSIGN:
6766 kind = CPTK_HAS_NOTHROW_ASSIGN;
6768 case RID_HAS_NOTHROW_CONSTRUCTOR:
6769 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6771 case RID_HAS_NOTHROW_COPY:
6772 kind = CPTK_HAS_NOTHROW_COPY;
6774 case RID_HAS_TRIVIAL_ASSIGN:
6775 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6777 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6778 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6780 case RID_HAS_TRIVIAL_COPY:
6781 kind = CPTK_HAS_TRIVIAL_COPY;
6783 case RID_HAS_TRIVIAL_DESTRUCTOR:
6784 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6786 case RID_HAS_VIRTUAL_DESTRUCTOR:
6787 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6789 case RID_IS_ABSTRACT:
6790 kind = CPTK_IS_ABSTRACT;
6792 case RID_IS_BASE_OF:
6793 kind = CPTK_IS_BASE_OF;
6797 kind = CPTK_IS_CLASS;
6799 case RID_IS_CONVERTIBLE_TO:
6800 kind = CPTK_IS_CONVERTIBLE_TO;
6804 kind = CPTK_IS_EMPTY;
6807 kind = CPTK_IS_ENUM;
6812 case RID_IS_POLYMORPHIC:
6813 kind = CPTK_IS_POLYMORPHIC;
6816 kind = CPTK_IS_UNION;
6822 /* Consume the token. */
6823 cp_lexer_consume_token (parser->lexer);
6825 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6827 type1 = cp_parser_type_id (parser);
6829 if (type1 == error_mark_node)
6830 return error_mark_node;
6832 /* Build a trivial decl-specifier-seq. */
6833 clear_decl_specs (&decl_specs);
6834 decl_specs.type = type1;
6836 /* Call grokdeclarator to figure out what type this is. */
6837 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6838 /*initialized=*/0, /*attrlist=*/NULL);
6842 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6844 type2 = cp_parser_type_id (parser);
6846 if (type2 == error_mark_node)
6847 return error_mark_node;
6849 /* Build a trivial decl-specifier-seq. */
6850 clear_decl_specs (&decl_specs);
6851 decl_specs.type = type2;
6853 /* Call grokdeclarator to figure out what type this is. */
6854 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6855 /*initialized=*/0, /*attrlist=*/NULL);
6858 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6860 /* Complete the trait expression, which may mean either processing
6861 the trait expr now or saving it for template instantiation. */
6862 return finish_trait_expr (kind, type1, type2);
6865 /* Statements [gram.stmt.stmt] */
6867 /* Parse a statement.
6871 expression-statement
6876 declaration-statement
6879 IN_COMPOUND is true when the statement is nested inside a
6880 cp_parser_compound_statement; this matters for certain pragmas.
6882 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6883 is a (possibly labeled) if statement which is not enclosed in braces
6884 and has an else clause. This is used to implement -Wparentheses. */
6887 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6888 bool in_compound, bool *if_p)
6892 location_t statement_location;
6897 /* There is no statement yet. */
6898 statement = NULL_TREE;
6899 /* Peek at the next token. */
6900 token = cp_lexer_peek_token (parser->lexer);
6901 /* Remember the location of the first token in the statement. */
6902 statement_location = token->location;
6903 /* If this is a keyword, then that will often determine what kind of
6904 statement we have. */
6905 if (token->type == CPP_KEYWORD)
6907 enum rid keyword = token->keyword;
6913 /* Looks like a labeled-statement with a case label.
6914 Parse the label, and then use tail recursion to parse
6916 cp_parser_label_for_labeled_statement (parser);
6921 statement = cp_parser_selection_statement (parser, if_p);
6927 statement = cp_parser_iteration_statement (parser);
6934 statement = cp_parser_jump_statement (parser);
6937 /* Objective-C++ exception-handling constructs. */
6940 case RID_AT_FINALLY:
6941 case RID_AT_SYNCHRONIZED:
6943 statement = cp_parser_objc_statement (parser);
6947 statement = cp_parser_try_block (parser);
6951 /* This must be a namespace alias definition. */
6952 cp_parser_declaration_statement (parser);
6956 /* It might be a keyword like `int' that can start a
6957 declaration-statement. */
6961 else if (token->type == CPP_NAME)
6963 /* If the next token is a `:', then we are looking at a
6964 labeled-statement. */
6965 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6966 if (token->type == CPP_COLON)
6968 /* Looks like a labeled-statement with an ordinary label.
6969 Parse the label, and then use tail recursion to parse
6971 cp_parser_label_for_labeled_statement (parser);
6975 /* Anything that starts with a `{' must be a compound-statement. */
6976 else if (token->type == CPP_OPEN_BRACE)
6977 statement = cp_parser_compound_statement (parser, NULL, false);
6978 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6979 a statement all its own. */
6980 else if (token->type == CPP_PRAGMA)
6982 /* Only certain OpenMP pragmas are attached to statements, and thus
6983 are considered statements themselves. All others are not. In
6984 the context of a compound, accept the pragma as a "statement" and
6985 return so that we can check for a close brace. Otherwise we
6986 require a real statement and must go back and read one. */
6988 cp_parser_pragma (parser, pragma_compound);
6989 else if (!cp_parser_pragma (parser, pragma_stmt))
6993 else if (token->type == CPP_EOF)
6995 cp_parser_error (parser, "expected statement");
6999 /* Everything else must be a declaration-statement or an
7000 expression-statement. Try for the declaration-statement
7001 first, unless we are looking at a `;', in which case we know that
7002 we have an expression-statement. */
7005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7007 cp_parser_parse_tentatively (parser);
7008 /* Try to parse the declaration-statement. */
7009 cp_parser_declaration_statement (parser);
7010 /* If that worked, we're done. */
7011 if (cp_parser_parse_definitely (parser))
7014 /* Look for an expression-statement instead. */
7015 statement = cp_parser_expression_statement (parser, in_statement_expr);
7018 /* Set the line number for the statement. */
7019 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7020 SET_EXPR_LOCATION (statement, statement_location);
7023 /* Parse the label for a labeled-statement, i.e.
7026 case constant-expression :
7030 case constant-expression ... constant-expression : statement
7032 When a label is parsed without errors, the label is added to the
7033 parse tree by the finish_* functions, so this function doesn't
7034 have to return the label. */
7037 cp_parser_label_for_labeled_statement (cp_parser* parser)
7041 /* The next token should be an identifier. */
7042 token = cp_lexer_peek_token (parser->lexer);
7043 if (token->type != CPP_NAME
7044 && token->type != CPP_KEYWORD)
7046 cp_parser_error (parser, "expected labeled-statement");
7050 switch (token->keyword)
7057 /* Consume the `case' token. */
7058 cp_lexer_consume_token (parser->lexer);
7059 /* Parse the constant-expression. */
7060 expr = cp_parser_constant_expression (parser,
7061 /*allow_non_constant_p=*/false,
7064 ellipsis = cp_lexer_peek_token (parser->lexer);
7065 if (ellipsis->type == CPP_ELLIPSIS)
7067 /* Consume the `...' token. */
7068 cp_lexer_consume_token (parser->lexer);
7070 cp_parser_constant_expression (parser,
7071 /*allow_non_constant_p=*/false,
7073 /* We don't need to emit warnings here, as the common code
7074 will do this for us. */
7077 expr_hi = NULL_TREE;
7079 if (parser->in_switch_statement_p)
7080 finish_case_label (expr, expr_hi);
7082 error ("%Hcase label %qE not within a switch statement",
7083 &token->location, expr);
7088 /* Consume the `default' token. */
7089 cp_lexer_consume_token (parser->lexer);
7091 if (parser->in_switch_statement_p)
7092 finish_case_label (NULL_TREE, NULL_TREE);
7094 error ("%Hcase label not within a switch statement", &token->location);
7098 /* Anything else must be an ordinary label. */
7099 finish_label_stmt (cp_parser_identifier (parser));
7103 /* Require the `:' token. */
7104 cp_parser_require (parser, CPP_COLON, "%<:%>");
7107 /* Parse an expression-statement.
7109 expression-statement:
7112 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7113 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7114 indicates whether this expression-statement is part of an
7115 expression statement. */
7118 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7120 tree statement = NULL_TREE;
7122 /* If the next token is a ';', then there is no expression
7124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7125 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7127 /* Consume the final `;'. */
7128 cp_parser_consume_semicolon_at_end_of_statement (parser);
7130 if (in_statement_expr
7131 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7132 /* This is the final expression statement of a statement
7134 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7136 statement = finish_expr_stmt (statement);
7143 /* Parse a compound-statement.
7146 { statement-seq [opt] }
7151 { label-declaration-seq [opt] statement-seq [opt] }
7153 label-declaration-seq:
7155 label-declaration-seq label-declaration
7157 Returns a tree representing the statement. */
7160 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7165 /* Consume the `{'. */
7166 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7167 return error_mark_node;
7168 /* Begin the compound-statement. */
7169 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7170 /* If the next keyword is `__label__' we have a label declaration. */
7171 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7172 cp_parser_label_declaration (parser);
7173 /* Parse an (optional) statement-seq. */
7174 cp_parser_statement_seq_opt (parser, in_statement_expr);
7175 /* Finish the compound-statement. */
7176 finish_compound_stmt (compound_stmt);
7177 /* Consume the `}'. */
7178 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7180 return compound_stmt;
7183 /* Parse an (optional) statement-seq.
7187 statement-seq [opt] statement */
7190 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7192 /* Scan statements until there aren't any more. */
7195 cp_token *token = cp_lexer_peek_token (parser->lexer);
7197 /* If we're looking at a `}', then we've run out of statements. */
7198 if (token->type == CPP_CLOSE_BRACE
7199 || token->type == CPP_EOF
7200 || token->type == CPP_PRAGMA_EOL)
7203 /* If we are in a compound statement and find 'else' then
7204 something went wrong. */
7205 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7207 if (parser->in_statement & IN_IF_STMT)
7211 token = cp_lexer_consume_token (parser->lexer);
7212 error ("%H%<else%> without a previous %<if%>", &token->location);
7216 /* Parse the statement. */
7217 cp_parser_statement (parser, in_statement_expr, true, NULL);
7221 /* Parse a selection-statement.
7223 selection-statement:
7224 if ( condition ) statement
7225 if ( condition ) statement else statement
7226 switch ( condition ) statement
7228 Returns the new IF_STMT or SWITCH_STMT.
7230 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7231 is a (possibly labeled) if statement which is not enclosed in
7232 braces and has an else clause. This is used to implement
7236 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7244 /* Peek at the next token. */
7245 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7247 /* See what kind of keyword it is. */
7248 keyword = token->keyword;
7257 /* Look for the `('. */
7258 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7260 cp_parser_skip_to_end_of_statement (parser);
7261 return error_mark_node;
7264 /* Begin the selection-statement. */
7265 if (keyword == RID_IF)
7266 statement = begin_if_stmt ();
7268 statement = begin_switch_stmt ();
7270 /* Parse the condition. */
7271 condition = cp_parser_condition (parser);
7272 /* Look for the `)'. */
7273 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7274 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7275 /*consume_paren=*/true);
7277 if (keyword == RID_IF)
7280 unsigned char in_statement;
7282 /* Add the condition. */
7283 finish_if_stmt_cond (condition, statement);
7285 /* Parse the then-clause. */
7286 in_statement = parser->in_statement;
7287 parser->in_statement |= IN_IF_STMT;
7288 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7290 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7291 add_stmt (build_empty_stmt ());
7292 cp_lexer_consume_token (parser->lexer);
7293 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7294 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7295 "empty body in an %<if%> statement");
7299 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7300 parser->in_statement = in_statement;
7302 finish_then_clause (statement);
7304 /* If the next token is `else', parse the else-clause. */
7305 if (cp_lexer_next_token_is_keyword (parser->lexer,
7308 /* Consume the `else' keyword. */
7309 cp_lexer_consume_token (parser->lexer);
7310 begin_else_clause (statement);
7311 /* Parse the else-clause. */
7312 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7314 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7315 OPT_Wempty_body, "suggest braces around "
7316 "empty body in an %<else%> statement");
7317 add_stmt (build_empty_stmt ());
7318 cp_lexer_consume_token (parser->lexer);
7321 cp_parser_implicitly_scoped_statement (parser, NULL);
7323 finish_else_clause (statement);
7325 /* If we are currently parsing a then-clause, then
7326 IF_P will not be NULL. We set it to true to
7327 indicate that this if statement has an else clause.
7328 This may trigger the Wparentheses warning below
7329 when we get back up to the parent if statement. */
7335 /* This if statement does not have an else clause. If
7336 NESTED_IF is true, then the then-clause is an if
7337 statement which does have an else clause. We warn
7338 about the potential ambiguity. */
7340 warning (OPT_Wparentheses,
7341 ("%Hsuggest explicit braces "
7342 "to avoid ambiguous %<else%>"),
7343 EXPR_LOCUS (statement));
7346 /* Now we're all done with the if-statement. */
7347 finish_if_stmt (statement);
7351 bool in_switch_statement_p;
7352 unsigned char in_statement;
7354 /* Add the condition. */
7355 finish_switch_cond (condition, statement);
7357 /* Parse the body of the switch-statement. */
7358 in_switch_statement_p = parser->in_switch_statement_p;
7359 in_statement = parser->in_statement;
7360 parser->in_switch_statement_p = true;
7361 parser->in_statement |= IN_SWITCH_STMT;
7362 cp_parser_implicitly_scoped_statement (parser, NULL);
7363 parser->in_switch_statement_p = in_switch_statement_p;
7364 parser->in_statement = in_statement;
7366 /* Now we're all done with the switch-statement. */
7367 finish_switch_stmt (statement);
7375 cp_parser_error (parser, "expected selection-statement");
7376 return error_mark_node;
7380 /* Parse a condition.
7384 type-specifier-seq declarator = initializer-clause
7385 type-specifier-seq declarator braced-init-list
7390 type-specifier-seq declarator asm-specification [opt]
7391 attributes [opt] = assignment-expression
7393 Returns the expression that should be tested. */
7396 cp_parser_condition (cp_parser* parser)
7398 cp_decl_specifier_seq type_specifiers;
7399 const char *saved_message;
7401 /* Try the declaration first. */
7402 cp_parser_parse_tentatively (parser);
7403 /* New types are not allowed in the type-specifier-seq for a
7405 saved_message = parser->type_definition_forbidden_message;
7406 parser->type_definition_forbidden_message
7407 = "types may not be defined in conditions";
7408 /* Parse the type-specifier-seq. */
7409 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7411 /* Restore the saved message. */
7412 parser->type_definition_forbidden_message = saved_message;
7413 /* If all is well, we might be looking at a declaration. */
7414 if (!cp_parser_error_occurred (parser))
7417 tree asm_specification;
7419 cp_declarator *declarator;
7420 tree initializer = NULL_TREE;
7422 /* Parse the declarator. */
7423 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7424 /*ctor_dtor_or_conv_p=*/NULL,
7425 /*parenthesized_p=*/NULL,
7426 /*member_p=*/false);
7427 /* Parse the attributes. */
7428 attributes = cp_parser_attributes_opt (parser);
7429 /* Parse the asm-specification. */
7430 asm_specification = cp_parser_asm_specification_opt (parser);
7431 /* If the next token is not an `=' or '{', then we might still be
7432 looking at an expression. For example:
7436 looks like a decl-specifier-seq and a declarator -- but then
7437 there is no `=', so this is an expression. */
7438 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7439 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7440 cp_parser_simulate_error (parser);
7442 /* If we did see an `=' or '{', then we are looking at a declaration
7444 if (cp_parser_parse_definitely (parser))
7447 bool non_constant_p;
7448 bool flags = LOOKUP_ONLYCONVERTING;
7450 /* Create the declaration. */
7451 decl = start_decl (declarator, &type_specifiers,
7452 /*initialized_p=*/true,
7453 attributes, /*prefix_attributes=*/NULL_TREE,
7456 /* Parse the initializer. */
7457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7459 initializer = cp_parser_braced_list (parser, &non_constant_p);
7460 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7465 /* Consume the `='. */
7466 cp_parser_require (parser, CPP_EQ, "%<=%>");
7467 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7469 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7470 maybe_warn_cpp0x ("extended initializer lists");
7472 if (!non_constant_p)
7473 initializer = fold_non_dependent_expr (initializer);
7475 /* Process the initializer. */
7476 cp_finish_decl (decl,
7477 initializer, !non_constant_p,
7482 pop_scope (pushed_scope);
7484 return convert_from_reference (decl);
7487 /* If we didn't even get past the declarator successfully, we are
7488 definitely not looking at a declaration. */
7490 cp_parser_abort_tentative_parse (parser);
7492 /* Otherwise, we are looking at an expression. */
7493 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7496 /* Parse an iteration-statement.
7498 iteration-statement:
7499 while ( condition ) statement
7500 do statement while ( expression ) ;
7501 for ( for-init-statement condition [opt] ; expression [opt] )
7504 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7507 cp_parser_iteration_statement (cp_parser* parser)
7512 unsigned char in_statement;
7514 /* Peek at the next token. */
7515 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7517 return error_mark_node;
7519 /* Remember whether or not we are already within an iteration
7521 in_statement = parser->in_statement;
7523 /* See what kind of keyword it is. */
7524 keyword = token->keyword;
7531 /* Begin the while-statement. */
7532 statement = begin_while_stmt ();
7533 /* Look for the `('. */
7534 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7535 /* Parse the condition. */
7536 condition = cp_parser_condition (parser);
7537 finish_while_stmt_cond (condition, statement);
7538 /* Look for the `)'. */
7539 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7540 /* Parse the dependent statement. */
7541 parser->in_statement = IN_ITERATION_STMT;
7542 cp_parser_already_scoped_statement (parser);
7543 parser->in_statement = in_statement;
7544 /* We're done with the while-statement. */
7545 finish_while_stmt (statement);
7553 /* Begin the do-statement. */
7554 statement = begin_do_stmt ();
7555 /* Parse the body of the do-statement. */
7556 parser->in_statement = IN_ITERATION_STMT;
7557 cp_parser_implicitly_scoped_statement (parser, NULL);
7558 parser->in_statement = in_statement;
7559 finish_do_body (statement);
7560 /* Look for the `while' keyword. */
7561 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7562 /* Look for the `('. */
7563 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7564 /* Parse the expression. */
7565 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7566 /* We're done with the do-statement. */
7567 finish_do_stmt (expression, statement);
7568 /* Look for the `)'. */
7569 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7570 /* Look for the `;'. */
7571 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7577 tree condition = NULL_TREE;
7578 tree expression = NULL_TREE;
7580 /* Begin the for-statement. */
7581 statement = begin_for_stmt ();
7582 /* Look for the `('. */
7583 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7584 /* Parse the initialization. */
7585 cp_parser_for_init_statement (parser);
7586 finish_for_init_stmt (statement);
7588 /* If there's a condition, process it. */
7589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7590 condition = cp_parser_condition (parser);
7591 finish_for_cond (condition, statement);
7592 /* Look for the `;'. */
7593 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7595 /* If there's an expression, process it. */
7596 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7597 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7598 finish_for_expr (expression, statement);
7599 /* Look for the `)'. */
7600 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7602 /* Parse the body of the for-statement. */
7603 parser->in_statement = IN_ITERATION_STMT;
7604 cp_parser_already_scoped_statement (parser);
7605 parser->in_statement = in_statement;
7607 /* We're done with the for-statement. */
7608 finish_for_stmt (statement);
7613 cp_parser_error (parser, "expected iteration-statement");
7614 statement = error_mark_node;
7621 /* Parse a for-init-statement.
7624 expression-statement
7625 simple-declaration */
7628 cp_parser_for_init_statement (cp_parser* parser)
7630 /* If the next token is a `;', then we have an empty
7631 expression-statement. Grammatically, this is also a
7632 simple-declaration, but an invalid one, because it does not
7633 declare anything. Therefore, if we did not handle this case
7634 specially, we would issue an error message about an invalid
7636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7638 /* We're going to speculatively look for a declaration, falling back
7639 to an expression, if necessary. */
7640 cp_parser_parse_tentatively (parser);
7641 /* Parse the declaration. */
7642 cp_parser_simple_declaration (parser,
7643 /*function_definition_allowed_p=*/false);
7644 /* If the tentative parse failed, then we shall need to look for an
7645 expression-statement. */
7646 if (cp_parser_parse_definitely (parser))
7650 cp_parser_expression_statement (parser, false);
7653 /* Parse a jump-statement.
7658 return expression [opt] ;
7659 return braced-init-list ;
7667 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7670 cp_parser_jump_statement (cp_parser* parser)
7672 tree statement = error_mark_node;
7675 unsigned char in_statement;
7677 /* Peek at the next token. */
7678 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7680 return error_mark_node;
7682 /* See what kind of keyword it is. */
7683 keyword = token->keyword;
7687 in_statement = parser->in_statement & ~IN_IF_STMT;
7688 switch (in_statement)
7691 error ("%Hbreak statement not within loop or switch", &token->location);
7694 gcc_assert ((in_statement & IN_SWITCH_STMT)
7695 || in_statement == IN_ITERATION_STMT);
7696 statement = finish_break_stmt ();
7699 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7702 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7705 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7709 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7712 error ("%Hcontinue statement not within a loop", &token->location);
7714 case IN_ITERATION_STMT:
7716 statement = finish_continue_stmt ();
7719 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7724 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7730 bool expr_non_constant_p;
7732 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7734 maybe_warn_cpp0x ("extended initializer lists");
7735 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7737 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7738 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7740 /* If the next token is a `;', then there is no
7743 /* Build the return-statement. */
7744 statement = finish_return_stmt (expr);
7745 /* Look for the final `;'. */
7746 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7751 /* Create the goto-statement. */
7752 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7754 /* Issue a warning about this use of a GNU extension. */
7755 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7756 /* Consume the '*' token. */
7757 cp_lexer_consume_token (parser->lexer);
7758 /* Parse the dependent expression. */
7759 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7762 finish_goto_stmt (cp_parser_identifier (parser));
7763 /* Look for the final `;'. */
7764 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7768 cp_parser_error (parser, "expected jump-statement");
7775 /* Parse a declaration-statement.
7777 declaration-statement:
7778 block-declaration */
7781 cp_parser_declaration_statement (cp_parser* parser)
7785 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7786 p = obstack_alloc (&declarator_obstack, 0);
7788 /* Parse the block-declaration. */
7789 cp_parser_block_declaration (parser, /*statement_p=*/true);
7791 /* Free any declarators allocated. */
7792 obstack_free (&declarator_obstack, p);
7794 /* Finish off the statement. */
7798 /* Some dependent statements (like `if (cond) statement'), are
7799 implicitly in their own scope. In other words, if the statement is
7800 a single statement (as opposed to a compound-statement), it is
7801 none-the-less treated as if it were enclosed in braces. Any
7802 declarations appearing in the dependent statement are out of scope
7803 after control passes that point. This function parses a statement,
7804 but ensures that is in its own scope, even if it is not a
7807 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7808 is a (possibly labeled) if statement which is not enclosed in
7809 braces and has an else clause. This is used to implement
7812 Returns the new statement. */
7815 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7822 /* Mark if () ; with a special NOP_EXPR. */
7823 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7825 cp_lexer_consume_token (parser->lexer);
7826 statement = add_stmt (build_empty_stmt ());
7828 /* if a compound is opened, we simply parse the statement directly. */
7829 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7830 statement = cp_parser_compound_statement (parser, NULL, false);
7831 /* If the token is not a `{', then we must take special action. */
7834 /* Create a compound-statement. */
7835 statement = begin_compound_stmt (0);
7836 /* Parse the dependent-statement. */
7837 cp_parser_statement (parser, NULL_TREE, false, if_p);
7838 /* Finish the dummy compound-statement. */
7839 finish_compound_stmt (statement);
7842 /* Return the statement. */
7846 /* For some dependent statements (like `while (cond) statement'), we
7847 have already created a scope. Therefore, even if the dependent
7848 statement is a compound-statement, we do not want to create another
7852 cp_parser_already_scoped_statement (cp_parser* parser)
7854 /* If the token is a `{', then we must take special action. */
7855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7856 cp_parser_statement (parser, NULL_TREE, false, NULL);
7859 /* Avoid calling cp_parser_compound_statement, so that we
7860 don't create a new scope. Do everything else by hand. */
7861 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7862 /* If the next keyword is `__label__' we have a label declaration. */
7863 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7864 cp_parser_label_declaration (parser);
7865 /* Parse an (optional) statement-seq. */
7866 cp_parser_statement_seq_opt (parser, NULL_TREE);
7867 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7871 /* Declarations [gram.dcl.dcl] */
7873 /* Parse an optional declaration-sequence.
7877 declaration-seq declaration */
7880 cp_parser_declaration_seq_opt (cp_parser* parser)
7886 token = cp_lexer_peek_token (parser->lexer);
7888 if (token->type == CPP_CLOSE_BRACE
7889 || token->type == CPP_EOF
7890 || token->type == CPP_PRAGMA_EOL)
7893 if (token->type == CPP_SEMICOLON)
7895 /* A declaration consisting of a single semicolon is
7896 invalid. Allow it unless we're being pedantic. */
7897 cp_lexer_consume_token (parser->lexer);
7898 if (!in_system_header)
7899 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7903 /* If we're entering or exiting a region that's implicitly
7904 extern "C", modify the lang context appropriately. */
7905 if (!parser->implicit_extern_c && token->implicit_extern_c)
7907 push_lang_context (lang_name_c);
7908 parser->implicit_extern_c = true;
7910 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7912 pop_lang_context ();
7913 parser->implicit_extern_c = false;
7916 if (token->type == CPP_PRAGMA)
7918 /* A top-level declaration can consist solely of a #pragma.
7919 A nested declaration cannot, so this is done here and not
7920 in cp_parser_declaration. (A #pragma at block scope is
7921 handled in cp_parser_statement.) */
7922 cp_parser_pragma (parser, pragma_external);
7926 /* Parse the declaration itself. */
7927 cp_parser_declaration (parser);
7931 /* Parse a declaration.
7936 template-declaration
7937 explicit-instantiation
7938 explicit-specialization
7939 linkage-specification
7940 namespace-definition
7945 __extension__ declaration */
7948 cp_parser_declaration (cp_parser* parser)
7955 /* Check for the `__extension__' keyword. */
7956 if (cp_parser_extension_opt (parser, &saved_pedantic))
7958 /* Parse the qualified declaration. */
7959 cp_parser_declaration (parser);
7960 /* Restore the PEDANTIC flag. */
7961 pedantic = saved_pedantic;
7966 /* Try to figure out what kind of declaration is present. */
7967 token1 = *cp_lexer_peek_token (parser->lexer);
7969 if (token1.type != CPP_EOF)
7970 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7973 token2.type = CPP_EOF;
7974 token2.keyword = RID_MAX;
7977 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7978 p = obstack_alloc (&declarator_obstack, 0);
7980 /* If the next token is `extern' and the following token is a string
7981 literal, then we have a linkage specification. */
7982 if (token1.keyword == RID_EXTERN
7983 && cp_parser_is_string_literal (&token2))
7984 cp_parser_linkage_specification (parser);
7985 /* If the next token is `template', then we have either a template
7986 declaration, an explicit instantiation, or an explicit
7988 else if (token1.keyword == RID_TEMPLATE)
7990 /* `template <>' indicates a template specialization. */
7991 if (token2.type == CPP_LESS
7992 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7993 cp_parser_explicit_specialization (parser);
7994 /* `template <' indicates a template declaration. */
7995 else if (token2.type == CPP_LESS)
7996 cp_parser_template_declaration (parser, /*member_p=*/false);
7997 /* Anything else must be an explicit instantiation. */
7999 cp_parser_explicit_instantiation (parser);
8001 /* If the next token is `export', then we have a template
8003 else if (token1.keyword == RID_EXPORT)
8004 cp_parser_template_declaration (parser, /*member_p=*/false);
8005 /* If the next token is `extern', 'static' or 'inline' and the one
8006 after that is `template', we have a GNU extended explicit
8007 instantiation directive. */
8008 else if (cp_parser_allow_gnu_extensions_p (parser)
8009 && (token1.keyword == RID_EXTERN
8010 || token1.keyword == RID_STATIC
8011 || token1.keyword == RID_INLINE)
8012 && token2.keyword == RID_TEMPLATE)
8013 cp_parser_explicit_instantiation (parser);
8014 /* If the next token is `namespace', check for a named or unnamed
8015 namespace definition. */
8016 else if (token1.keyword == RID_NAMESPACE
8017 && (/* A named namespace definition. */
8018 (token2.type == CPP_NAME
8019 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8021 /* An unnamed namespace definition. */
8022 || token2.type == CPP_OPEN_BRACE
8023 || token2.keyword == RID_ATTRIBUTE))
8024 cp_parser_namespace_definition (parser);
8025 /* An inline (associated) namespace definition. */
8026 else if (token1.keyword == RID_INLINE
8027 && token2.keyword == RID_NAMESPACE)
8028 cp_parser_namespace_definition (parser);
8029 /* Objective-C++ declaration/definition. */
8030 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8031 cp_parser_objc_declaration (parser);
8032 /* We must have either a block declaration or a function
8035 /* Try to parse a block-declaration, or a function-definition. */
8036 cp_parser_block_declaration (parser, /*statement_p=*/false);
8038 /* Free any declarators allocated. */
8039 obstack_free (&declarator_obstack, p);
8042 /* Parse a block-declaration.
8047 namespace-alias-definition
8054 __extension__ block-declaration
8059 static_assert-declaration
8061 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8062 part of a declaration-statement. */
8065 cp_parser_block_declaration (cp_parser *parser,
8071 /* Check for the `__extension__' keyword. */
8072 if (cp_parser_extension_opt (parser, &saved_pedantic))
8074 /* Parse the qualified declaration. */
8075 cp_parser_block_declaration (parser, statement_p);
8076 /* Restore the PEDANTIC flag. */
8077 pedantic = saved_pedantic;
8082 /* Peek at the next token to figure out which kind of declaration is
8084 token1 = cp_lexer_peek_token (parser->lexer);
8086 /* If the next keyword is `asm', we have an asm-definition. */
8087 if (token1->keyword == RID_ASM)
8090 cp_parser_commit_to_tentative_parse (parser);
8091 cp_parser_asm_definition (parser);
8093 /* If the next keyword is `namespace', we have a
8094 namespace-alias-definition. */
8095 else if (token1->keyword == RID_NAMESPACE)
8096 cp_parser_namespace_alias_definition (parser);
8097 /* If the next keyword is `using', we have either a
8098 using-declaration or a using-directive. */
8099 else if (token1->keyword == RID_USING)
8104 cp_parser_commit_to_tentative_parse (parser);
8105 /* If the token after `using' is `namespace', then we have a
8107 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8108 if (token2->keyword == RID_NAMESPACE)
8109 cp_parser_using_directive (parser);
8110 /* Otherwise, it's a using-declaration. */
8112 cp_parser_using_declaration (parser,
8113 /*access_declaration_p=*/false);
8115 /* If the next keyword is `__label__' we have a misplaced label
8117 else if (token1->keyword == RID_LABEL)
8119 cp_lexer_consume_token (parser->lexer);
8120 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8121 cp_parser_skip_to_end_of_statement (parser);
8122 /* If the next token is now a `;', consume it. */
8123 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8124 cp_lexer_consume_token (parser->lexer);
8126 /* If the next token is `static_assert' we have a static assertion. */
8127 else if (token1->keyword == RID_STATIC_ASSERT)
8128 cp_parser_static_assert (parser, /*member_p=*/false);
8129 /* Anything else must be a simple-declaration. */
8131 cp_parser_simple_declaration (parser, !statement_p);
8134 /* Parse a simple-declaration.
8137 decl-specifier-seq [opt] init-declarator-list [opt] ;
8139 init-declarator-list:
8141 init-declarator-list , init-declarator
8143 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8144 function-definition as a simple-declaration. */
8147 cp_parser_simple_declaration (cp_parser* parser,
8148 bool function_definition_allowed_p)
8150 cp_decl_specifier_seq decl_specifiers;
8151 int declares_class_or_enum;
8152 bool saw_declarator;
8154 /* Defer access checks until we know what is being declared; the
8155 checks for names appearing in the decl-specifier-seq should be
8156 done as if we were in the scope of the thing being declared. */
8157 push_deferring_access_checks (dk_deferred);
8159 /* Parse the decl-specifier-seq. We have to keep track of whether
8160 or not the decl-specifier-seq declares a named class or
8161 enumeration type, since that is the only case in which the
8162 init-declarator-list is allowed to be empty.
8166 In a simple-declaration, the optional init-declarator-list can be
8167 omitted only when declaring a class or enumeration, that is when
8168 the decl-specifier-seq contains either a class-specifier, an
8169 elaborated-type-specifier, or an enum-specifier. */
8170 cp_parser_decl_specifier_seq (parser,
8171 CP_PARSER_FLAGS_OPTIONAL,
8173 &declares_class_or_enum);
8174 /* We no longer need to defer access checks. */
8175 stop_deferring_access_checks ();
8177 /* In a block scope, a valid declaration must always have a
8178 decl-specifier-seq. By not trying to parse declarators, we can
8179 resolve the declaration/expression ambiguity more quickly. */
8180 if (!function_definition_allowed_p
8181 && !decl_specifiers.any_specifiers_p)
8183 cp_parser_error (parser, "expected declaration");
8187 /* If the next two tokens are both identifiers, the code is
8188 erroneous. The usual cause of this situation is code like:
8192 where "T" should name a type -- but does not. */
8193 if (!decl_specifiers.type
8194 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8196 /* If parsing tentatively, we should commit; we really are
8197 looking at a declaration. */
8198 cp_parser_commit_to_tentative_parse (parser);
8203 /* If we have seen at least one decl-specifier, and the next token
8204 is not a parenthesis, then we must be looking at a declaration.
8205 (After "int (" we might be looking at a functional cast.) */
8206 if (decl_specifiers.any_specifiers_p
8207 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8208 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8209 && !cp_parser_error_occurred (parser))
8210 cp_parser_commit_to_tentative_parse (parser);
8212 /* Keep going until we hit the `;' at the end of the simple
8214 saw_declarator = false;
8215 while (cp_lexer_next_token_is_not (parser->lexer,
8219 bool function_definition_p;
8224 /* If we are processing next declarator, coma is expected */
8225 token = cp_lexer_peek_token (parser->lexer);
8226 gcc_assert (token->type == CPP_COMMA);
8227 cp_lexer_consume_token (parser->lexer);
8230 saw_declarator = true;
8232 /* Parse the init-declarator. */
8233 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8235 function_definition_allowed_p,
8237 declares_class_or_enum,
8238 &function_definition_p);
8239 /* If an error occurred while parsing tentatively, exit quickly.
8240 (That usually happens when in the body of a function; each
8241 statement is treated as a declaration-statement until proven
8243 if (cp_parser_error_occurred (parser))
8245 /* Handle function definitions specially. */
8246 if (function_definition_p)
8248 /* If the next token is a `,', then we are probably
8249 processing something like:
8253 which is erroneous. */
8254 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8256 cp_token *token = cp_lexer_peek_token (parser->lexer);
8257 error ("%Hmixing declarations and function-definitions is forbidden",
8260 /* Otherwise, we're done with the list of declarators. */
8263 pop_deferring_access_checks ();
8267 /* The next token should be either a `,' or a `;'. */
8268 token = cp_lexer_peek_token (parser->lexer);
8269 /* If it's a `,', there are more declarators to come. */
8270 if (token->type == CPP_COMMA)
8271 /* will be consumed next time around */;
8272 /* If it's a `;', we are done. */
8273 else if (token->type == CPP_SEMICOLON)
8275 /* Anything else is an error. */
8278 /* If we have already issued an error message we don't need
8279 to issue another one. */
8280 if (decl != error_mark_node
8281 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8282 cp_parser_error (parser, "expected %<,%> or %<;%>");
8283 /* Skip tokens until we reach the end of the statement. */
8284 cp_parser_skip_to_end_of_statement (parser);
8285 /* If the next token is now a `;', consume it. */
8286 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8287 cp_lexer_consume_token (parser->lexer);
8290 /* After the first time around, a function-definition is not
8291 allowed -- even if it was OK at first. For example:
8296 function_definition_allowed_p = false;
8299 /* Issue an error message if no declarators are present, and the
8300 decl-specifier-seq does not itself declare a class or
8302 if (!saw_declarator)
8304 if (cp_parser_declares_only_class_p (parser))
8305 shadow_tag (&decl_specifiers);
8306 /* Perform any deferred access checks. */
8307 perform_deferred_access_checks ();
8310 /* Consume the `;'. */
8311 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8314 pop_deferring_access_checks ();
8317 /* Parse a decl-specifier-seq.
8320 decl-specifier-seq [opt] decl-specifier
8323 storage-class-specifier
8334 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8336 The parser flags FLAGS is used to control type-specifier parsing.
8338 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8341 1: one of the decl-specifiers is an elaborated-type-specifier
8342 (i.e., a type declaration)
8343 2: one of the decl-specifiers is an enum-specifier or a
8344 class-specifier (i.e., a type definition)
8349 cp_parser_decl_specifier_seq (cp_parser* parser,
8350 cp_parser_flags flags,
8351 cp_decl_specifier_seq *decl_specs,
8352 int* declares_class_or_enum)
8354 bool constructor_possible_p = !parser->in_declarator_p;
8355 cp_token *start_token = NULL;
8357 /* Clear DECL_SPECS. */
8358 clear_decl_specs (decl_specs);
8360 /* Assume no class or enumeration type is declared. */
8361 *declares_class_or_enum = 0;
8363 /* Keep reading specifiers until there are no more to read. */
8367 bool found_decl_spec;
8370 /* Peek at the next token. */
8371 token = cp_lexer_peek_token (parser->lexer);
8373 /* Save the first token of the decl spec list for error
8376 start_token = token;
8377 /* Handle attributes. */
8378 if (token->keyword == RID_ATTRIBUTE)
8380 /* Parse the attributes. */
8381 decl_specs->attributes
8382 = chainon (decl_specs->attributes,
8383 cp_parser_attributes_opt (parser));
8386 /* Assume we will find a decl-specifier keyword. */
8387 found_decl_spec = true;
8388 /* If the next token is an appropriate keyword, we can simply
8389 add it to the list. */
8390 switch (token->keyword)
8395 if (!at_class_scope_p ())
8397 error ("%H%<friend%> used outside of class", &token->location);
8398 cp_lexer_purge_token (parser->lexer);
8402 ++decl_specs->specs[(int) ds_friend];
8403 /* Consume the token. */
8404 cp_lexer_consume_token (parser->lexer);
8408 /* function-specifier:
8415 cp_parser_function_specifier_opt (parser, decl_specs);
8421 ++decl_specs->specs[(int) ds_typedef];
8422 /* Consume the token. */
8423 cp_lexer_consume_token (parser->lexer);
8424 /* A constructor declarator cannot appear in a typedef. */
8425 constructor_possible_p = false;
8426 /* The "typedef" keyword can only occur in a declaration; we
8427 may as well commit at this point. */
8428 cp_parser_commit_to_tentative_parse (parser);
8430 if (decl_specs->storage_class != sc_none)
8431 decl_specs->conflicting_specifiers_p = true;
8434 /* storage-class-specifier:
8444 if (cxx_dialect == cxx98)
8446 /* Consume the token. */
8447 cp_lexer_consume_token (parser->lexer);
8449 /* Complain about `auto' as a storage specifier, if
8450 we're complaining about C++0x compatibility. */
8453 "%H%<auto%> will change meaning in C++0x; please remove it",
8456 /* Set the storage class anyway. */
8457 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8461 /* C++0x auto type-specifier. */
8462 found_decl_spec = false;
8469 /* Consume the token. */
8470 cp_lexer_consume_token (parser->lexer);
8471 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8475 /* Consume the token. */
8476 cp_lexer_consume_token (parser->lexer);
8477 ++decl_specs->specs[(int) ds_thread];
8481 /* We did not yet find a decl-specifier yet. */
8482 found_decl_spec = false;
8486 /* Constructors are a special case. The `S' in `S()' is not a
8487 decl-specifier; it is the beginning of the declarator. */
8490 && constructor_possible_p
8491 && (cp_parser_constructor_declarator_p
8492 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8494 /* If we don't have a DECL_SPEC yet, then we must be looking at
8495 a type-specifier. */
8496 if (!found_decl_spec && !constructor_p)
8498 int decl_spec_declares_class_or_enum;
8499 bool is_cv_qualifier;
8503 = cp_parser_type_specifier (parser, flags,
8505 /*is_declaration=*/true,
8506 &decl_spec_declares_class_or_enum,
8508 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8510 /* If this type-specifier referenced a user-defined type
8511 (a typedef, class-name, etc.), then we can't allow any
8512 more such type-specifiers henceforth.
8516 The longest sequence of decl-specifiers that could
8517 possibly be a type name is taken as the
8518 decl-specifier-seq of a declaration. The sequence shall
8519 be self-consistent as described below.
8523 As a general rule, at most one type-specifier is allowed
8524 in the complete decl-specifier-seq of a declaration. The
8525 only exceptions are the following:
8527 -- const or volatile can be combined with any other
8530 -- signed or unsigned can be combined with char, long,
8538 void g (const int Pc);
8540 Here, Pc is *not* part of the decl-specifier seq; it's
8541 the declarator. Therefore, once we see a type-specifier
8542 (other than a cv-qualifier), we forbid any additional
8543 user-defined types. We *do* still allow things like `int
8544 int' to be considered a decl-specifier-seq, and issue the
8545 error message later. */
8546 if (type_spec && !is_cv_qualifier)
8547 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8548 /* A constructor declarator cannot follow a type-specifier. */
8551 constructor_possible_p = false;
8552 found_decl_spec = true;
8556 /* If we still do not have a DECL_SPEC, then there are no more
8558 if (!found_decl_spec)
8561 decl_specs->any_specifiers_p = true;
8562 /* After we see one decl-specifier, further decl-specifiers are
8564 flags |= CP_PARSER_FLAGS_OPTIONAL;
8567 cp_parser_check_decl_spec (decl_specs, start_token->location);
8569 /* Don't allow a friend specifier with a class definition. */
8570 if (decl_specs->specs[(int) ds_friend] != 0
8571 && (*declares_class_or_enum & 2))
8572 error ("%Hclass definition may not be declared a friend",
8573 &start_token->location);
8576 /* Parse an (optional) storage-class-specifier.
8578 storage-class-specifier:
8587 storage-class-specifier:
8590 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8593 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8595 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8598 if (cxx_dialect != cxx98)
8600 /* Fall through for C++98. */
8607 /* Consume the token. */
8608 return cp_lexer_consume_token (parser->lexer)->u.value;
8615 /* Parse an (optional) function-specifier.
8622 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8623 Updates DECL_SPECS, if it is non-NULL. */
8626 cp_parser_function_specifier_opt (cp_parser* parser,
8627 cp_decl_specifier_seq *decl_specs)
8629 cp_token *token = cp_lexer_peek_token (parser->lexer);
8630 switch (token->keyword)
8634 ++decl_specs->specs[(int) ds_inline];
8638 /* 14.5.2.3 [temp.mem]
8640 A member function template shall not be virtual. */
8641 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8642 error ("%Htemplates may not be %<virtual%>", &token->location);
8643 else if (decl_specs)
8644 ++decl_specs->specs[(int) ds_virtual];
8649 ++decl_specs->specs[(int) ds_explicit];
8656 /* Consume the token. */
8657 return cp_lexer_consume_token (parser->lexer)->u.value;
8660 /* Parse a linkage-specification.
8662 linkage-specification:
8663 extern string-literal { declaration-seq [opt] }
8664 extern string-literal declaration */
8667 cp_parser_linkage_specification (cp_parser* parser)
8671 /* Look for the `extern' keyword. */
8672 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8674 /* Look for the string-literal. */
8675 linkage = cp_parser_string_literal (parser, false, false);
8677 /* Transform the literal into an identifier. If the literal is a
8678 wide-character string, or contains embedded NULs, then we can't
8679 handle it as the user wants. */
8680 if (strlen (TREE_STRING_POINTER (linkage))
8681 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8683 cp_parser_error (parser, "invalid linkage-specification");
8684 /* Assume C++ linkage. */
8685 linkage = lang_name_cplusplus;
8688 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8690 /* We're now using the new linkage. */
8691 push_lang_context (linkage);
8693 /* If the next token is a `{', then we're using the first
8695 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8697 /* Consume the `{' token. */
8698 cp_lexer_consume_token (parser->lexer);
8699 /* Parse the declarations. */
8700 cp_parser_declaration_seq_opt (parser);
8701 /* Look for the closing `}'. */
8702 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8704 /* Otherwise, there's just one declaration. */
8707 bool saved_in_unbraced_linkage_specification_p;
8709 saved_in_unbraced_linkage_specification_p
8710 = parser->in_unbraced_linkage_specification_p;
8711 parser->in_unbraced_linkage_specification_p = true;
8712 cp_parser_declaration (parser);
8713 parser->in_unbraced_linkage_specification_p
8714 = saved_in_unbraced_linkage_specification_p;
8717 /* We're done with the linkage-specification. */
8718 pop_lang_context ();
8721 /* Parse a static_assert-declaration.
8723 static_assert-declaration:
8724 static_assert ( constant-expression , string-literal ) ;
8726 If MEMBER_P, this static_assert is a class member. */
8729 cp_parser_static_assert(cp_parser *parser, bool member_p)
8734 location_t saved_loc;
8736 /* Peek at the `static_assert' token so we can keep track of exactly
8737 where the static assertion started. */
8738 token = cp_lexer_peek_token (parser->lexer);
8739 saved_loc = token->location;
8741 /* Look for the `static_assert' keyword. */
8742 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8743 "%<static_assert%>"))
8746 /* We know we are in a static assertion; commit to any tentative
8748 if (cp_parser_parsing_tentatively (parser))
8749 cp_parser_commit_to_tentative_parse (parser);
8751 /* Parse the `(' starting the static assertion condition. */
8752 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8754 /* Parse the constant-expression. */
8756 cp_parser_constant_expression (parser,
8757 /*allow_non_constant_p=*/false,
8758 /*non_constant_p=*/NULL);
8760 /* Parse the separating `,'. */
8761 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8763 /* Parse the string-literal message. */
8764 message = cp_parser_string_literal (parser,
8765 /*translate=*/false,
8768 /* A `)' completes the static assertion. */
8769 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8770 cp_parser_skip_to_closing_parenthesis (parser,
8771 /*recovering=*/true,
8773 /*consume_paren=*/true);
8775 /* A semicolon terminates the declaration. */
8776 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8778 /* Complete the static assertion, which may mean either processing
8779 the static assert now or saving it for template instantiation. */
8780 finish_static_assert (condition, message, saved_loc, member_p);
8783 /* Parse a `decltype' type. Returns the type.
8785 simple-type-specifier:
8786 decltype ( expression ) */
8789 cp_parser_decltype (cp_parser *parser)
8792 bool id_expression_or_member_access_p = false;
8793 const char *saved_message;
8794 bool saved_integral_constant_expression_p;
8795 bool saved_non_integral_constant_expression_p;
8796 cp_token *id_expr_start_token;
8798 /* Look for the `decltype' token. */
8799 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8800 return error_mark_node;
8802 /* Types cannot be defined in a `decltype' expression. Save away the
8804 saved_message = parser->type_definition_forbidden_message;
8806 /* And create the new one. */
8807 parser->type_definition_forbidden_message
8808 = "types may not be defined in %<decltype%> expressions";
8810 /* The restrictions on constant-expressions do not apply inside
8811 decltype expressions. */
8812 saved_integral_constant_expression_p
8813 = parser->integral_constant_expression_p;
8814 saved_non_integral_constant_expression_p
8815 = parser->non_integral_constant_expression_p;
8816 parser->integral_constant_expression_p = false;
8818 /* Do not actually evaluate the expression. */
8821 /* Parse the opening `('. */
8822 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8823 return error_mark_node;
8825 /* First, try parsing an id-expression. */
8826 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8827 cp_parser_parse_tentatively (parser);
8828 expr = cp_parser_id_expression (parser,
8829 /*template_keyword_p=*/false,
8830 /*check_dependency_p=*/true,
8831 /*template_p=*/NULL,
8832 /*declarator_p=*/false,
8833 /*optional_p=*/false);
8835 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8837 bool non_integral_constant_expression_p = false;
8838 tree id_expression = expr;
8840 const char *error_msg;
8842 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8843 /* Lookup the name we got back from the id-expression. */
8844 expr = cp_parser_lookup_name (parser, expr,
8846 /*is_template=*/false,
8847 /*is_namespace=*/false,
8848 /*check_dependency=*/true,
8849 /*ambiguous_decls=*/NULL,
8850 id_expr_start_token->location);
8853 && expr != error_mark_node
8854 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8855 && TREE_CODE (expr) != TYPE_DECL
8856 && (TREE_CODE (expr) != BIT_NOT_EXPR
8857 || !TYPE_P (TREE_OPERAND (expr, 0)))
8858 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8860 /* Complete lookup of the id-expression. */
8861 expr = (finish_id_expression
8862 (id_expression, expr, parser->scope, &idk,
8863 /*integral_constant_expression_p=*/false,
8864 /*allow_non_integral_constant_expression_p=*/true,
8865 &non_integral_constant_expression_p,
8866 /*template_p=*/false,
8868 /*address_p=*/false,
8869 /*template_arg_p=*/false,
8871 id_expr_start_token->location));
8873 if (expr == error_mark_node)
8874 /* We found an id-expression, but it was something that we
8875 should not have found. This is an error, not something
8876 we can recover from, so note that we found an
8877 id-expression and we'll recover as gracefully as
8879 id_expression_or_member_access_p = true;
8883 && expr != error_mark_node
8884 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8885 /* We have an id-expression. */
8886 id_expression_or_member_access_p = true;
8889 if (!id_expression_or_member_access_p)
8891 /* Abort the id-expression parse. */
8892 cp_parser_abort_tentative_parse (parser);
8894 /* Parsing tentatively, again. */
8895 cp_parser_parse_tentatively (parser);
8897 /* Parse a class member access. */
8898 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8900 /*member_access_only_p=*/true, NULL);
8903 && expr != error_mark_node
8904 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8905 /* We have an id-expression. */
8906 id_expression_or_member_access_p = true;
8909 if (id_expression_or_member_access_p)
8910 /* We have parsed the complete id-expression or member access. */
8911 cp_parser_parse_definitely (parser);
8914 /* Abort our attempt to parse an id-expression or member access
8916 cp_parser_abort_tentative_parse (parser);
8918 /* Parse a full expression. */
8919 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8922 /* Go back to evaluating expressions. */
8925 /* Restore the old message and the integral constant expression
8927 parser->type_definition_forbidden_message = saved_message;
8928 parser->integral_constant_expression_p
8929 = saved_integral_constant_expression_p;
8930 parser->non_integral_constant_expression_p
8931 = saved_non_integral_constant_expression_p;
8933 if (expr == error_mark_node)
8935 /* Skip everything up to the closing `)'. */
8936 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8937 /*consume_paren=*/true);
8938 return error_mark_node;
8941 /* Parse to the closing `)'. */
8942 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8944 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8945 /*consume_paren=*/true);
8946 return error_mark_node;
8949 return finish_decltype_type (expr, id_expression_or_member_access_p);
8952 /* Special member functions [gram.special] */
8954 /* Parse a conversion-function-id.
8956 conversion-function-id:
8957 operator conversion-type-id
8959 Returns an IDENTIFIER_NODE representing the operator. */
8962 cp_parser_conversion_function_id (cp_parser* parser)
8966 tree saved_qualifying_scope;
8967 tree saved_object_scope;
8968 tree pushed_scope = NULL_TREE;
8970 /* Look for the `operator' token. */
8971 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8972 return error_mark_node;
8973 /* When we parse the conversion-type-id, the current scope will be
8974 reset. However, we need that information in able to look up the
8975 conversion function later, so we save it here. */
8976 saved_scope = parser->scope;
8977 saved_qualifying_scope = parser->qualifying_scope;
8978 saved_object_scope = parser->object_scope;
8979 /* We must enter the scope of the class so that the names of
8980 entities declared within the class are available in the
8981 conversion-type-id. For example, consider:
8988 S::operator I() { ... }
8990 In order to see that `I' is a type-name in the definition, we
8991 must be in the scope of `S'. */
8993 pushed_scope = push_scope (saved_scope);
8994 /* Parse the conversion-type-id. */
8995 type = cp_parser_conversion_type_id (parser);
8996 /* Leave the scope of the class, if any. */
8998 pop_scope (pushed_scope);
8999 /* Restore the saved scope. */
9000 parser->scope = saved_scope;
9001 parser->qualifying_scope = saved_qualifying_scope;
9002 parser->object_scope = saved_object_scope;
9003 /* If the TYPE is invalid, indicate failure. */
9004 if (type == error_mark_node)
9005 return error_mark_node;
9006 return mangle_conv_op_name_for_type (type);
9009 /* Parse a conversion-type-id:
9012 type-specifier-seq conversion-declarator [opt]
9014 Returns the TYPE specified. */
9017 cp_parser_conversion_type_id (cp_parser* parser)
9020 cp_decl_specifier_seq type_specifiers;
9021 cp_declarator *declarator;
9022 tree type_specified;
9024 /* Parse the attributes. */
9025 attributes = cp_parser_attributes_opt (parser);
9026 /* Parse the type-specifiers. */
9027 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9029 /* If that didn't work, stop. */
9030 if (type_specifiers.type == error_mark_node)
9031 return error_mark_node;
9032 /* Parse the conversion-declarator. */
9033 declarator = cp_parser_conversion_declarator_opt (parser);
9035 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9036 /*initialized=*/0, &attributes);
9038 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9040 /* Don't give this error when parsing tentatively. This happens to
9041 work because we always parse this definitively once. */
9042 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9043 && type_uses_auto (type_specified))
9045 error ("invalid use of %<auto%> in conversion operator");
9046 return error_mark_node;
9049 return type_specified;
9052 /* Parse an (optional) conversion-declarator.
9054 conversion-declarator:
9055 ptr-operator conversion-declarator [opt]
9059 static cp_declarator *
9060 cp_parser_conversion_declarator_opt (cp_parser* parser)
9062 enum tree_code code;
9064 cp_cv_quals cv_quals;
9066 /* We don't know if there's a ptr-operator next, or not. */
9067 cp_parser_parse_tentatively (parser);
9068 /* Try the ptr-operator. */
9069 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9070 /* If it worked, look for more conversion-declarators. */
9071 if (cp_parser_parse_definitely (parser))
9073 cp_declarator *declarator;
9075 /* Parse another optional declarator. */
9076 declarator = cp_parser_conversion_declarator_opt (parser);
9078 return cp_parser_make_indirect_declarator
9079 (code, class_type, cv_quals, declarator);
9085 /* Parse an (optional) ctor-initializer.
9088 : mem-initializer-list
9090 Returns TRUE iff the ctor-initializer was actually present. */
9093 cp_parser_ctor_initializer_opt (cp_parser* parser)
9095 /* If the next token is not a `:', then there is no
9096 ctor-initializer. */
9097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9099 /* Do default initialization of any bases and members. */
9100 if (DECL_CONSTRUCTOR_P (current_function_decl))
9101 finish_mem_initializers (NULL_TREE);
9106 /* Consume the `:' token. */
9107 cp_lexer_consume_token (parser->lexer);
9108 /* And the mem-initializer-list. */
9109 cp_parser_mem_initializer_list (parser);
9114 /* Parse a mem-initializer-list.
9116 mem-initializer-list:
9117 mem-initializer ... [opt]
9118 mem-initializer ... [opt] , mem-initializer-list */
9121 cp_parser_mem_initializer_list (cp_parser* parser)
9123 tree mem_initializer_list = NULL_TREE;
9124 cp_token *token = cp_lexer_peek_token (parser->lexer);
9126 /* Let the semantic analysis code know that we are starting the
9127 mem-initializer-list. */
9128 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9129 error ("%Honly constructors take base initializers",
9132 /* Loop through the list. */
9135 tree mem_initializer;
9137 token = cp_lexer_peek_token (parser->lexer);
9138 /* Parse the mem-initializer. */
9139 mem_initializer = cp_parser_mem_initializer (parser);
9140 /* If the next token is a `...', we're expanding member initializers. */
9141 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9143 /* Consume the `...'. */
9144 cp_lexer_consume_token (parser->lexer);
9146 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9147 can be expanded but members cannot. */
9148 if (mem_initializer != error_mark_node
9149 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9151 error ("%Hcannot expand initializer for member %<%D%>",
9152 &token->location, TREE_PURPOSE (mem_initializer));
9153 mem_initializer = error_mark_node;
9156 /* Construct the pack expansion type. */
9157 if (mem_initializer != error_mark_node)
9158 mem_initializer = make_pack_expansion (mem_initializer);
9160 /* Add it to the list, unless it was erroneous. */
9161 if (mem_initializer != error_mark_node)
9163 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9164 mem_initializer_list = mem_initializer;
9166 /* If the next token is not a `,', we're done. */
9167 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9169 /* Consume the `,' token. */
9170 cp_lexer_consume_token (parser->lexer);
9173 /* Perform semantic analysis. */
9174 if (DECL_CONSTRUCTOR_P (current_function_decl))
9175 finish_mem_initializers (mem_initializer_list);
9178 /* Parse a mem-initializer.
9181 mem-initializer-id ( expression-list [opt] )
9182 mem-initializer-id braced-init-list
9187 ( expression-list [opt] )
9189 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9190 class) or FIELD_DECL (for a non-static data member) to initialize;
9191 the TREE_VALUE is the expression-list. An empty initialization
9192 list is represented by void_list_node. */
9195 cp_parser_mem_initializer (cp_parser* parser)
9197 tree mem_initializer_id;
9198 tree expression_list;
9200 cp_token *token = cp_lexer_peek_token (parser->lexer);
9202 /* Find out what is being initialized. */
9203 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9205 permerror (token->location,
9206 "anachronistic old-style base class initializer");
9207 mem_initializer_id = NULL_TREE;
9211 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9212 if (mem_initializer_id == error_mark_node)
9213 return mem_initializer_id;
9215 member = expand_member_init (mem_initializer_id);
9216 if (member && !DECL_P (member))
9217 in_base_initializer = 1;
9219 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9221 bool expr_non_constant_p;
9222 maybe_warn_cpp0x ("extended initializer lists");
9223 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9224 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9225 expression_list = build_tree_list (NULL_TREE, expression_list);
9229 = cp_parser_parenthesized_expression_list (parser, false,
9231 /*allow_expansion_p=*/true,
9232 /*non_constant_p=*/NULL);
9233 if (expression_list == error_mark_node)
9234 return error_mark_node;
9235 if (!expression_list)
9236 expression_list = void_type_node;
9238 in_base_initializer = 0;
9240 return member ? build_tree_list (member, expression_list) : error_mark_node;
9243 /* Parse a mem-initializer-id.
9246 :: [opt] nested-name-specifier [opt] class-name
9249 Returns a TYPE indicating the class to be initializer for the first
9250 production. Returns an IDENTIFIER_NODE indicating the data member
9251 to be initialized for the second production. */
9254 cp_parser_mem_initializer_id (cp_parser* parser)
9256 bool global_scope_p;
9257 bool nested_name_specifier_p;
9258 bool template_p = false;
9261 cp_token *token = cp_lexer_peek_token (parser->lexer);
9263 /* `typename' is not allowed in this context ([temp.res]). */
9264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9266 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9267 "member initializer is implicitly a type)",
9269 cp_lexer_consume_token (parser->lexer);
9271 /* Look for the optional `::' operator. */
9273 = (cp_parser_global_scope_opt (parser,
9274 /*current_scope_valid_p=*/false)
9276 /* Look for the optional nested-name-specifier. The simplest way to
9281 The keyword `typename' is not permitted in a base-specifier or
9282 mem-initializer; in these contexts a qualified name that
9283 depends on a template-parameter is implicitly assumed to be a
9286 is to assume that we have seen the `typename' keyword at this
9288 nested_name_specifier_p
9289 = (cp_parser_nested_name_specifier_opt (parser,
9290 /*typename_keyword_p=*/true,
9291 /*check_dependency_p=*/true,
9293 /*is_declaration=*/true)
9295 if (nested_name_specifier_p)
9296 template_p = cp_parser_optional_template_keyword (parser);
9297 /* If there is a `::' operator or a nested-name-specifier, then we
9298 are definitely looking for a class-name. */
9299 if (global_scope_p || nested_name_specifier_p)
9300 return cp_parser_class_name (parser,
9301 /*typename_keyword_p=*/true,
9302 /*template_keyword_p=*/template_p,
9304 /*check_dependency_p=*/true,
9305 /*class_head_p=*/false,
9306 /*is_declaration=*/true);
9307 /* Otherwise, we could also be looking for an ordinary identifier. */
9308 cp_parser_parse_tentatively (parser);
9309 /* Try a class-name. */
9310 id = cp_parser_class_name (parser,
9311 /*typename_keyword_p=*/true,
9312 /*template_keyword_p=*/false,
9314 /*check_dependency_p=*/true,
9315 /*class_head_p=*/false,
9316 /*is_declaration=*/true);
9317 /* If we found one, we're done. */
9318 if (cp_parser_parse_definitely (parser))
9320 /* Otherwise, look for an ordinary identifier. */
9321 return cp_parser_identifier (parser);
9324 /* Overloading [gram.over] */
9326 /* Parse an operator-function-id.
9328 operator-function-id:
9331 Returns an IDENTIFIER_NODE for the operator which is a
9332 human-readable spelling of the identifier, e.g., `operator +'. */
9335 cp_parser_operator_function_id (cp_parser* parser)
9337 /* Look for the `operator' keyword. */
9338 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9339 return error_mark_node;
9340 /* And then the name of the operator itself. */
9341 return cp_parser_operator (parser);
9344 /* Parse an operator.
9347 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9348 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9349 || ++ -- , ->* -> () []
9356 Returns an IDENTIFIER_NODE for the operator which is a
9357 human-readable spelling of the identifier, e.g., `operator +'. */
9360 cp_parser_operator (cp_parser* parser)
9362 tree id = NULL_TREE;
9365 /* Peek at the next token. */
9366 token = cp_lexer_peek_token (parser->lexer);
9367 /* Figure out which operator we have. */
9368 switch (token->type)
9374 /* The keyword should be either `new' or `delete'. */
9375 if (token->keyword == RID_NEW)
9377 else if (token->keyword == RID_DELETE)
9382 /* Consume the `new' or `delete' token. */
9383 cp_lexer_consume_token (parser->lexer);
9385 /* Peek at the next token. */
9386 token = cp_lexer_peek_token (parser->lexer);
9387 /* If it's a `[' token then this is the array variant of the
9389 if (token->type == CPP_OPEN_SQUARE)
9391 /* Consume the `[' token. */
9392 cp_lexer_consume_token (parser->lexer);
9393 /* Look for the `]' token. */
9394 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9395 id = ansi_opname (op == NEW_EXPR
9396 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9398 /* Otherwise, we have the non-array variant. */
9400 id = ansi_opname (op);
9406 id = ansi_opname (PLUS_EXPR);
9410 id = ansi_opname (MINUS_EXPR);
9414 id = ansi_opname (MULT_EXPR);
9418 id = ansi_opname (TRUNC_DIV_EXPR);
9422 id = ansi_opname (TRUNC_MOD_EXPR);
9426 id = ansi_opname (BIT_XOR_EXPR);
9430 id = ansi_opname (BIT_AND_EXPR);
9434 id = ansi_opname (BIT_IOR_EXPR);
9438 id = ansi_opname (BIT_NOT_EXPR);
9442 id = ansi_opname (TRUTH_NOT_EXPR);
9446 id = ansi_assopname (NOP_EXPR);
9450 id = ansi_opname (LT_EXPR);
9454 id = ansi_opname (GT_EXPR);
9458 id = ansi_assopname (PLUS_EXPR);
9462 id = ansi_assopname (MINUS_EXPR);
9466 id = ansi_assopname (MULT_EXPR);
9470 id = ansi_assopname (TRUNC_DIV_EXPR);
9474 id = ansi_assopname (TRUNC_MOD_EXPR);
9478 id = ansi_assopname (BIT_XOR_EXPR);
9482 id = ansi_assopname (BIT_AND_EXPR);
9486 id = ansi_assopname (BIT_IOR_EXPR);
9490 id = ansi_opname (LSHIFT_EXPR);
9494 id = ansi_opname (RSHIFT_EXPR);
9498 id = ansi_assopname (LSHIFT_EXPR);
9502 id = ansi_assopname (RSHIFT_EXPR);
9506 id = ansi_opname (EQ_EXPR);
9510 id = ansi_opname (NE_EXPR);
9514 id = ansi_opname (LE_EXPR);
9517 case CPP_GREATER_EQ:
9518 id = ansi_opname (GE_EXPR);
9522 id = ansi_opname (TRUTH_ANDIF_EXPR);
9526 id = ansi_opname (TRUTH_ORIF_EXPR);
9530 id = ansi_opname (POSTINCREMENT_EXPR);
9533 case CPP_MINUS_MINUS:
9534 id = ansi_opname (PREDECREMENT_EXPR);
9538 id = ansi_opname (COMPOUND_EXPR);
9541 case CPP_DEREF_STAR:
9542 id = ansi_opname (MEMBER_REF);
9546 id = ansi_opname (COMPONENT_REF);
9549 case CPP_OPEN_PAREN:
9550 /* Consume the `('. */
9551 cp_lexer_consume_token (parser->lexer);
9552 /* Look for the matching `)'. */
9553 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9554 return ansi_opname (CALL_EXPR);
9556 case CPP_OPEN_SQUARE:
9557 /* Consume the `['. */
9558 cp_lexer_consume_token (parser->lexer);
9559 /* Look for the matching `]'. */
9560 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9561 return ansi_opname (ARRAY_REF);
9564 /* Anything else is an error. */
9568 /* If we have selected an identifier, we need to consume the
9571 cp_lexer_consume_token (parser->lexer);
9572 /* Otherwise, no valid operator name was present. */
9575 cp_parser_error (parser, "expected operator");
9576 id = error_mark_node;
9582 /* Parse a template-declaration.
9584 template-declaration:
9585 export [opt] template < template-parameter-list > declaration
9587 If MEMBER_P is TRUE, this template-declaration occurs within a
9590 The grammar rule given by the standard isn't correct. What
9593 template-declaration:
9594 export [opt] template-parameter-list-seq
9595 decl-specifier-seq [opt] init-declarator [opt] ;
9596 export [opt] template-parameter-list-seq
9599 template-parameter-list-seq:
9600 template-parameter-list-seq [opt]
9601 template < template-parameter-list > */
9604 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9606 /* Check for `export'. */
9607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9609 /* Consume the `export' token. */
9610 cp_lexer_consume_token (parser->lexer);
9611 /* Warn that we do not support `export'. */
9612 warning (0, "keyword %<export%> not implemented, and will be ignored");
9615 cp_parser_template_declaration_after_export (parser, member_p);
9618 /* Parse a template-parameter-list.
9620 template-parameter-list:
9622 template-parameter-list , template-parameter
9624 Returns a TREE_LIST. Each node represents a template parameter.
9625 The nodes are connected via their TREE_CHAINs. */
9628 cp_parser_template_parameter_list (cp_parser* parser)
9630 tree parameter_list = NULL_TREE;
9632 begin_template_parm_list ();
9637 bool is_parameter_pack;
9639 /* Parse the template-parameter. */
9640 parameter = cp_parser_template_parameter (parser,
9642 &is_parameter_pack);
9643 /* Add it to the list. */
9644 if (parameter != error_mark_node)
9645 parameter_list = process_template_parm (parameter_list,
9651 tree err_parm = build_tree_list (parameter, parameter);
9652 TREE_VALUE (err_parm) = error_mark_node;
9653 parameter_list = chainon (parameter_list, err_parm);
9656 /* If the next token is not a `,', we're done. */
9657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9659 /* Otherwise, consume the `,' token. */
9660 cp_lexer_consume_token (parser->lexer);
9663 return end_template_parm_list (parameter_list);
9666 /* Parse a template-parameter.
9670 parameter-declaration
9672 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9673 the parameter. The TREE_PURPOSE is the default value, if any.
9674 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9675 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9676 set to true iff this parameter is a parameter pack. */
9679 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9680 bool *is_parameter_pack)
9683 cp_parameter_declarator *parameter_declarator;
9684 cp_declarator *id_declarator;
9687 /* Assume it is a type parameter or a template parameter. */
9688 *is_non_type = false;
9689 /* Assume it not a parameter pack. */
9690 *is_parameter_pack = false;
9691 /* Peek at the next token. */
9692 token = cp_lexer_peek_token (parser->lexer);
9693 /* If it is `class' or `template', we have a type-parameter. */
9694 if (token->keyword == RID_TEMPLATE)
9695 return cp_parser_type_parameter (parser, is_parameter_pack);
9696 /* If it is `class' or `typename' we do not know yet whether it is a
9697 type parameter or a non-type parameter. Consider:
9699 template <typename T, typename T::X X> ...
9703 template <class C, class D*> ...
9705 Here, the first parameter is a type parameter, and the second is
9706 a non-type parameter. We can tell by looking at the token after
9707 the identifier -- if it is a `,', `=', or `>' then we have a type
9709 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9711 /* Peek at the token after `class' or `typename'. */
9712 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9713 /* If it's an ellipsis, we have a template type parameter
9715 if (token->type == CPP_ELLIPSIS)
9716 return cp_parser_type_parameter (parser, is_parameter_pack);
9717 /* If it's an identifier, skip it. */
9718 if (token->type == CPP_NAME)
9719 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9720 /* Now, see if the token looks like the end of a template
9722 if (token->type == CPP_COMMA
9723 || token->type == CPP_EQ
9724 || token->type == CPP_GREATER)
9725 return cp_parser_type_parameter (parser, is_parameter_pack);
9728 /* Otherwise, it is a non-type parameter.
9732 When parsing a default template-argument for a non-type
9733 template-parameter, the first non-nested `>' is taken as the end
9734 of the template parameter-list rather than a greater-than
9736 *is_non_type = true;
9737 parameter_declarator
9738 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9739 /*parenthesized_p=*/NULL);
9741 /* If the parameter declaration is marked as a parameter pack, set
9742 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9743 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9745 if (parameter_declarator
9746 && parameter_declarator->declarator
9747 && parameter_declarator->declarator->parameter_pack_p)
9749 *is_parameter_pack = true;
9750 parameter_declarator->declarator->parameter_pack_p = false;
9753 /* If the next token is an ellipsis, and we don't already have it
9754 marked as a parameter pack, then we have a parameter pack (that
9755 has no declarator). */
9756 if (!*is_parameter_pack
9757 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9758 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9760 /* Consume the `...'. */
9761 cp_lexer_consume_token (parser->lexer);
9762 maybe_warn_variadic_templates ();
9764 *is_parameter_pack = true;
9766 /* We might end up with a pack expansion as the type of the non-type
9767 template parameter, in which case this is a non-type template
9769 else if (parameter_declarator
9770 && parameter_declarator->decl_specifiers.type
9771 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9773 *is_parameter_pack = true;
9774 parameter_declarator->decl_specifiers.type =
9775 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9778 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9780 /* Parameter packs cannot have default arguments. However, a
9781 user may try to do so, so we'll parse them and give an
9782 appropriate diagnostic here. */
9784 /* Consume the `='. */
9785 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9786 cp_lexer_consume_token (parser->lexer);
9788 /* Find the name of the parameter pack. */
9789 id_declarator = parameter_declarator->declarator;
9790 while (id_declarator && id_declarator->kind != cdk_id)
9791 id_declarator = id_declarator->declarator;
9793 if (id_declarator && id_declarator->kind == cdk_id)
9794 error ("%Htemplate parameter pack %qD cannot have a default argument",
9795 &start_token->location, id_declarator->u.id.unqualified_name);
9797 error ("%Htemplate parameter pack cannot have a default argument",
9798 &start_token->location);
9800 /* Parse the default argument, but throw away the result. */
9801 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9804 parm = grokdeclarator (parameter_declarator->declarator,
9805 ¶meter_declarator->decl_specifiers,
9806 PARM, /*initialized=*/0,
9808 if (parm == error_mark_node)
9809 return error_mark_node;
9811 return build_tree_list (parameter_declarator->default_argument, parm);
9814 /* Parse a type-parameter.
9817 class identifier [opt]
9818 class identifier [opt] = type-id
9819 typename identifier [opt]
9820 typename identifier [opt] = type-id
9821 template < template-parameter-list > class identifier [opt]
9822 template < template-parameter-list > class identifier [opt]
9825 GNU Extension (variadic templates):
9828 class ... identifier [opt]
9829 typename ... identifier [opt]
9831 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9832 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9833 the declaration of the parameter.
9835 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9838 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9843 /* Look for a keyword to tell us what kind of parameter this is. */
9844 token = cp_parser_require (parser, CPP_KEYWORD,
9845 "%<class%>, %<typename%>, or %<template%>");
9847 return error_mark_node;
9849 switch (token->keyword)
9855 tree default_argument;
9857 /* If the next token is an ellipsis, we have a template
9859 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9861 /* Consume the `...' token. */
9862 cp_lexer_consume_token (parser->lexer);
9863 maybe_warn_variadic_templates ();
9865 *is_parameter_pack = true;
9868 /* If the next token is an identifier, then it names the
9870 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9871 identifier = cp_parser_identifier (parser);
9873 identifier = NULL_TREE;
9875 /* Create the parameter. */
9876 parameter = finish_template_type_parm (class_type_node, identifier);
9878 /* If the next token is an `=', we have a default argument. */
9879 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9881 /* Consume the `=' token. */
9882 cp_lexer_consume_token (parser->lexer);
9883 /* Parse the default-argument. */
9884 push_deferring_access_checks (dk_no_deferred);
9885 default_argument = cp_parser_type_id (parser);
9887 /* Template parameter packs cannot have default
9889 if (*is_parameter_pack)
9892 error ("%Htemplate parameter pack %qD cannot have a "
9893 "default argument", &token->location, identifier);
9895 error ("%Htemplate parameter packs cannot have "
9896 "default arguments", &token->location);
9897 default_argument = NULL_TREE;
9899 pop_deferring_access_checks ();
9902 default_argument = NULL_TREE;
9904 /* Create the combined representation of the parameter and the
9905 default argument. */
9906 parameter = build_tree_list (default_argument, parameter);
9912 tree parameter_list;
9914 tree default_argument;
9916 /* Look for the `<'. */
9917 cp_parser_require (parser, CPP_LESS, "%<<%>");
9918 /* Parse the template-parameter-list. */
9919 parameter_list = cp_parser_template_parameter_list (parser);
9920 /* Look for the `>'. */
9921 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9922 /* Look for the `class' keyword. */
9923 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9924 /* If the next token is an ellipsis, we have a template
9926 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9928 /* Consume the `...' token. */
9929 cp_lexer_consume_token (parser->lexer);
9930 maybe_warn_variadic_templates ();
9932 *is_parameter_pack = true;
9934 /* If the next token is an `=', then there is a
9935 default-argument. If the next token is a `>', we are at
9936 the end of the parameter-list. If the next token is a `,',
9937 then we are at the end of this parameter. */
9938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9939 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9940 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9942 identifier = cp_parser_identifier (parser);
9943 /* Treat invalid names as if the parameter were nameless. */
9944 if (identifier == error_mark_node)
9945 identifier = NULL_TREE;
9948 identifier = NULL_TREE;
9950 /* Create the template parameter. */
9951 parameter = finish_template_template_parm (class_type_node,
9954 /* If the next token is an `=', then there is a
9955 default-argument. */
9956 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9960 /* Consume the `='. */
9961 cp_lexer_consume_token (parser->lexer);
9962 /* Parse the id-expression. */
9963 push_deferring_access_checks (dk_no_deferred);
9964 /* save token before parsing the id-expression, for error
9966 token = cp_lexer_peek_token (parser->lexer);
9968 = cp_parser_id_expression (parser,
9969 /*template_keyword_p=*/false,
9970 /*check_dependency_p=*/true,
9971 /*template_p=*/&is_template,
9972 /*declarator_p=*/false,
9973 /*optional_p=*/false);
9974 if (TREE_CODE (default_argument) == TYPE_DECL)
9975 /* If the id-expression was a template-id that refers to
9976 a template-class, we already have the declaration here,
9977 so no further lookup is needed. */
9980 /* Look up the name. */
9982 = cp_parser_lookup_name (parser, default_argument,
9984 /*is_template=*/is_template,
9985 /*is_namespace=*/false,
9986 /*check_dependency=*/true,
9987 /*ambiguous_decls=*/NULL,
9989 /* See if the default argument is valid. */
9991 = check_template_template_default_arg (default_argument);
9993 /* Template parameter packs cannot have default
9995 if (*is_parameter_pack)
9998 error ("%Htemplate parameter pack %qD cannot "
9999 "have a default argument",
10000 &token->location, identifier);
10002 error ("%Htemplate parameter packs cannot "
10003 "have default arguments",
10005 default_argument = NULL_TREE;
10007 pop_deferring_access_checks ();
10010 default_argument = NULL_TREE;
10012 /* Create the combined representation of the parameter and the
10013 default argument. */
10014 parameter = build_tree_list (default_argument, parameter);
10019 gcc_unreachable ();
10026 /* Parse a template-id.
10029 template-name < template-argument-list [opt] >
10031 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10032 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10033 returned. Otherwise, if the template-name names a function, or set
10034 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10035 names a class, returns a TYPE_DECL for the specialization.
10037 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10038 uninstantiated templates. */
10041 cp_parser_template_id (cp_parser *parser,
10042 bool template_keyword_p,
10043 bool check_dependency_p,
10044 bool is_declaration)
10050 cp_token_position start_of_id = 0;
10051 deferred_access_check *chk;
10052 VEC (deferred_access_check,gc) *access_check;
10053 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10054 bool is_identifier;
10056 /* If the next token corresponds to a template-id, there is no need
10058 next_token = cp_lexer_peek_token (parser->lexer);
10059 if (next_token->type == CPP_TEMPLATE_ID)
10061 struct tree_check *check_value;
10063 /* Get the stored value. */
10064 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10065 /* Perform any access checks that were deferred. */
10066 access_check = check_value->checks;
10070 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10073 perform_or_defer_access_check (chk->binfo,
10078 /* Return the stored value. */
10079 return check_value->value;
10082 /* Avoid performing name lookup if there is no possibility of
10083 finding a template-id. */
10084 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10085 || (next_token->type == CPP_NAME
10086 && !cp_parser_nth_token_starts_template_argument_list_p
10089 cp_parser_error (parser, "expected template-id");
10090 return error_mark_node;
10093 /* Remember where the template-id starts. */
10094 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10095 start_of_id = cp_lexer_token_position (parser->lexer, false);
10097 push_deferring_access_checks (dk_deferred);
10099 /* Parse the template-name. */
10100 is_identifier = false;
10101 token = cp_lexer_peek_token (parser->lexer);
10102 templ = cp_parser_template_name (parser, template_keyword_p,
10103 check_dependency_p,
10106 if (templ == error_mark_node || is_identifier)
10108 pop_deferring_access_checks ();
10112 /* If we find the sequence `[:' after a template-name, it's probably
10113 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10114 parse correctly the argument list. */
10115 next_token = cp_lexer_peek_token (parser->lexer);
10116 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10117 if (next_token->type == CPP_OPEN_SQUARE
10118 && next_token->flags & DIGRAPH
10119 && next_token_2->type == CPP_COLON
10120 && !(next_token_2->flags & PREV_WHITE))
10122 cp_parser_parse_tentatively (parser);
10123 /* Change `:' into `::'. */
10124 next_token_2->type = CPP_SCOPE;
10125 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10127 cp_lexer_consume_token (parser->lexer);
10129 /* Parse the arguments. */
10130 arguments = cp_parser_enclosed_template_argument_list (parser);
10131 if (!cp_parser_parse_definitely (parser))
10133 /* If we couldn't parse an argument list, then we revert our changes
10134 and return simply an error. Maybe this is not a template-id
10136 next_token_2->type = CPP_COLON;
10137 cp_parser_error (parser, "expected %<<%>");
10138 pop_deferring_access_checks ();
10139 return error_mark_node;
10141 /* Otherwise, emit an error about the invalid digraph, but continue
10142 parsing because we got our argument list. */
10143 if (permerror (next_token->location,
10144 "%<<::%> cannot begin a template-argument list"))
10146 static bool hint = false;
10147 inform (next_token->location,
10148 "%<<:%> is an alternate spelling for %<[%>."
10149 " Insert whitespace between %<<%> and %<::%>");
10150 if (!hint && !flag_permissive)
10152 inform (next_token->location, "(if you use %<-fpermissive%>"
10153 " G++ will accept your code)");
10160 /* Look for the `<' that starts the template-argument-list. */
10161 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10163 pop_deferring_access_checks ();
10164 return error_mark_node;
10166 /* Parse the arguments. */
10167 arguments = cp_parser_enclosed_template_argument_list (parser);
10170 /* Build a representation of the specialization. */
10171 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10172 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10173 else if (DECL_CLASS_TEMPLATE_P (templ)
10174 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10176 bool entering_scope;
10177 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10178 template (rather than some instantiation thereof) only if
10179 is not nested within some other construct. For example, in
10180 "template <typename T> void f(T) { A<T>::", A<T> is just an
10181 instantiation of A. */
10182 entering_scope = (template_parm_scope_p ()
10183 && cp_lexer_next_token_is (parser->lexer,
10186 = finish_template_type (templ, arguments, entering_scope);
10190 /* If it's not a class-template or a template-template, it should be
10191 a function-template. */
10192 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10193 || TREE_CODE (templ) == OVERLOAD
10194 || BASELINK_P (templ)));
10196 template_id = lookup_template_function (templ, arguments);
10199 /* If parsing tentatively, replace the sequence of tokens that makes
10200 up the template-id with a CPP_TEMPLATE_ID token. That way,
10201 should we re-parse the token stream, we will not have to repeat
10202 the effort required to do the parse, nor will we issue duplicate
10203 error messages about problems during instantiation of the
10207 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10209 /* Reset the contents of the START_OF_ID token. */
10210 token->type = CPP_TEMPLATE_ID;
10211 /* Retrieve any deferred checks. Do not pop this access checks yet
10212 so the memory will not be reclaimed during token replacing below. */
10213 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10214 token->u.tree_check_value->value = template_id;
10215 token->u.tree_check_value->checks = get_deferred_access_checks ();
10216 token->keyword = RID_MAX;
10218 /* Purge all subsequent tokens. */
10219 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10221 /* ??? Can we actually assume that, if template_id ==
10222 error_mark_node, we will have issued a diagnostic to the
10223 user, as opposed to simply marking the tentative parse as
10225 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10226 error ("%Hparse error in template argument list",
10230 pop_deferring_access_checks ();
10231 return template_id;
10234 /* Parse a template-name.
10239 The standard should actually say:
10243 operator-function-id
10245 A defect report has been filed about this issue.
10247 A conversion-function-id cannot be a template name because they cannot
10248 be part of a template-id. In fact, looking at this code:
10250 a.operator K<int>()
10252 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10253 It is impossible to call a templated conversion-function-id with an
10254 explicit argument list, since the only allowed template parameter is
10255 the type to which it is converting.
10257 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10258 `template' keyword, in a construction like:
10262 In that case `f' is taken to be a template-name, even though there
10263 is no way of knowing for sure.
10265 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10266 name refers to a set of overloaded functions, at least one of which
10267 is a template, or an IDENTIFIER_NODE with the name of the template,
10268 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10269 names are looked up inside uninstantiated templates. */
10272 cp_parser_template_name (cp_parser* parser,
10273 bool template_keyword_p,
10274 bool check_dependency_p,
10275 bool is_declaration,
10276 bool *is_identifier)
10281 cp_token *token = cp_lexer_peek_token (parser->lexer);
10283 /* If the next token is `operator', then we have either an
10284 operator-function-id or a conversion-function-id. */
10285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10287 /* We don't know whether we're looking at an
10288 operator-function-id or a conversion-function-id. */
10289 cp_parser_parse_tentatively (parser);
10290 /* Try an operator-function-id. */
10291 identifier = cp_parser_operator_function_id (parser);
10292 /* If that didn't work, try a conversion-function-id. */
10293 if (!cp_parser_parse_definitely (parser))
10295 cp_parser_error (parser, "expected template-name");
10296 return error_mark_node;
10299 /* Look for the identifier. */
10301 identifier = cp_parser_identifier (parser);
10303 /* If we didn't find an identifier, we don't have a template-id. */
10304 if (identifier == error_mark_node)
10305 return error_mark_node;
10307 /* If the name immediately followed the `template' keyword, then it
10308 is a template-name. However, if the next token is not `<', then
10309 we do not treat it as a template-name, since it is not being used
10310 as part of a template-id. This enables us to handle constructs
10313 template <typename T> struct S { S(); };
10314 template <typename T> S<T>::S();
10316 correctly. We would treat `S' as a template -- if it were `S<T>'
10317 -- but we do not if there is no `<'. */
10319 if (processing_template_decl
10320 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10322 /* In a declaration, in a dependent context, we pretend that the
10323 "template" keyword was present in order to improve error
10324 recovery. For example, given:
10326 template <typename T> void f(T::X<int>);
10328 we want to treat "X<int>" as a template-id. */
10330 && !template_keyword_p
10331 && parser->scope && TYPE_P (parser->scope)
10332 && check_dependency_p
10333 && dependent_scope_p (parser->scope)
10334 /* Do not do this for dtors (or ctors), since they never
10335 need the template keyword before their name. */
10336 && !constructor_name_p (identifier, parser->scope))
10338 cp_token_position start = 0;
10340 /* Explain what went wrong. */
10341 error ("%Hnon-template %qD used as template",
10342 &token->location, identifier);
10343 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10344 parser->scope, identifier);
10345 /* If parsing tentatively, find the location of the "<" token. */
10346 if (cp_parser_simulate_error (parser))
10347 start = cp_lexer_token_position (parser->lexer, true);
10348 /* Parse the template arguments so that we can issue error
10349 messages about them. */
10350 cp_lexer_consume_token (parser->lexer);
10351 cp_parser_enclosed_template_argument_list (parser);
10352 /* Skip tokens until we find a good place from which to
10353 continue parsing. */
10354 cp_parser_skip_to_closing_parenthesis (parser,
10355 /*recovering=*/true,
10357 /*consume_paren=*/false);
10358 /* If parsing tentatively, permanently remove the
10359 template argument list. That will prevent duplicate
10360 error messages from being issued about the missing
10361 "template" keyword. */
10363 cp_lexer_purge_tokens_after (parser->lexer, start);
10365 *is_identifier = true;
10369 /* If the "template" keyword is present, then there is generally
10370 no point in doing name-lookup, so we just return IDENTIFIER.
10371 But, if the qualifying scope is non-dependent then we can
10372 (and must) do name-lookup normally. */
10373 if (template_keyword_p
10375 || (TYPE_P (parser->scope)
10376 && dependent_type_p (parser->scope))))
10380 /* Look up the name. */
10381 decl = cp_parser_lookup_name (parser, identifier,
10383 /*is_template=*/false,
10384 /*is_namespace=*/false,
10385 check_dependency_p,
10386 /*ambiguous_decls=*/NULL,
10388 decl = maybe_get_template_decl_from_type_decl (decl);
10390 /* If DECL is a template, then the name was a template-name. */
10391 if (TREE_CODE (decl) == TEMPLATE_DECL)
10395 tree fn = NULL_TREE;
10397 /* The standard does not explicitly indicate whether a name that
10398 names a set of overloaded declarations, some of which are
10399 templates, is a template-name. However, such a name should
10400 be a template-name; otherwise, there is no way to form a
10401 template-id for the overloaded templates. */
10402 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10403 if (TREE_CODE (fns) == OVERLOAD)
10404 for (fn = fns; fn; fn = OVL_NEXT (fn))
10405 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10410 /* The name does not name a template. */
10411 cp_parser_error (parser, "expected template-name");
10412 return error_mark_node;
10416 /* If DECL is dependent, and refers to a function, then just return
10417 its name; we will look it up again during template instantiation. */
10418 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10420 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10421 if (TYPE_P (scope) && dependent_type_p (scope))
10428 /* Parse a template-argument-list.
10430 template-argument-list:
10431 template-argument ... [opt]
10432 template-argument-list , template-argument ... [opt]
10434 Returns a TREE_VEC containing the arguments. */
10437 cp_parser_template_argument_list (cp_parser* parser)
10439 tree fixed_args[10];
10440 unsigned n_args = 0;
10441 unsigned alloced = 10;
10442 tree *arg_ary = fixed_args;
10444 bool saved_in_template_argument_list_p;
10446 bool saved_non_ice_p;
10448 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10449 parser->in_template_argument_list_p = true;
10450 /* Even if the template-id appears in an integral
10451 constant-expression, the contents of the argument list do
10453 saved_ice_p = parser->integral_constant_expression_p;
10454 parser->integral_constant_expression_p = false;
10455 saved_non_ice_p = parser->non_integral_constant_expression_p;
10456 parser->non_integral_constant_expression_p = false;
10457 /* Parse the arguments. */
10463 /* Consume the comma. */
10464 cp_lexer_consume_token (parser->lexer);
10466 /* Parse the template-argument. */
10467 argument = cp_parser_template_argument (parser);
10469 /* If the next token is an ellipsis, we're expanding a template
10471 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10473 if (argument == error_mark_node)
10475 cp_token *token = cp_lexer_peek_token (parser->lexer);
10476 error ("%Hexpected parameter pack before %<...%>",
10479 /* Consume the `...' token. */
10480 cp_lexer_consume_token (parser->lexer);
10482 /* Make the argument into a TYPE_PACK_EXPANSION or
10483 EXPR_PACK_EXPANSION. */
10484 argument = make_pack_expansion (argument);
10487 if (n_args == alloced)
10491 if (arg_ary == fixed_args)
10493 arg_ary = XNEWVEC (tree, alloced);
10494 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10497 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10499 arg_ary[n_args++] = argument;
10501 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10503 vec = make_tree_vec (n_args);
10506 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10508 if (arg_ary != fixed_args)
10510 parser->non_integral_constant_expression_p = saved_non_ice_p;
10511 parser->integral_constant_expression_p = saved_ice_p;
10512 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10516 /* Parse a template-argument.
10519 assignment-expression
10523 The representation is that of an assignment-expression, type-id, or
10524 id-expression -- except that the qualified id-expression is
10525 evaluated, so that the value returned is either a DECL or an
10528 Although the standard says "assignment-expression", it forbids
10529 throw-expressions or assignments in the template argument.
10530 Therefore, we use "conditional-expression" instead. */
10533 cp_parser_template_argument (cp_parser* parser)
10538 bool maybe_type_id = false;
10539 cp_token *token = NULL, *argument_start_token = NULL;
10542 /* There's really no way to know what we're looking at, so we just
10543 try each alternative in order.
10547 In a template-argument, an ambiguity between a type-id and an
10548 expression is resolved to a type-id, regardless of the form of
10549 the corresponding template-parameter.
10551 Therefore, we try a type-id first. */
10552 cp_parser_parse_tentatively (parser);
10553 argument = cp_parser_template_type_arg (parser);
10554 /* If there was no error parsing the type-id but the next token is a
10555 '>>', our behavior depends on which dialect of C++ we're
10556 parsing. In C++98, we probably found a typo for '> >'. But there
10557 are type-id which are also valid expressions. For instance:
10559 struct X { int operator >> (int); };
10560 template <int V> struct Foo {};
10563 Here 'X()' is a valid type-id of a function type, but the user just
10564 wanted to write the expression "X() >> 5". Thus, we remember that we
10565 found a valid type-id, but we still try to parse the argument as an
10566 expression to see what happens.
10568 In C++0x, the '>>' will be considered two separate '>'
10570 if (!cp_parser_error_occurred (parser)
10571 && cxx_dialect == cxx98
10572 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10574 maybe_type_id = true;
10575 cp_parser_abort_tentative_parse (parser);
10579 /* If the next token isn't a `,' or a `>', then this argument wasn't
10580 really finished. This means that the argument is not a valid
10582 if (!cp_parser_next_token_ends_template_argument_p (parser))
10583 cp_parser_error (parser, "expected template-argument");
10584 /* If that worked, we're done. */
10585 if (cp_parser_parse_definitely (parser))
10588 /* We're still not sure what the argument will be. */
10589 cp_parser_parse_tentatively (parser);
10590 /* Try a template. */
10591 argument_start_token = cp_lexer_peek_token (parser->lexer);
10592 argument = cp_parser_id_expression (parser,
10593 /*template_keyword_p=*/false,
10594 /*check_dependency_p=*/true,
10596 /*declarator_p=*/false,
10597 /*optional_p=*/false);
10598 /* If the next token isn't a `,' or a `>', then this argument wasn't
10599 really finished. */
10600 if (!cp_parser_next_token_ends_template_argument_p (parser))
10601 cp_parser_error (parser, "expected template-argument");
10602 if (!cp_parser_error_occurred (parser))
10604 /* Figure out what is being referred to. If the id-expression
10605 was for a class template specialization, then we will have a
10606 TYPE_DECL at this point. There is no need to do name lookup
10607 at this point in that case. */
10608 if (TREE_CODE (argument) != TYPE_DECL)
10609 argument = cp_parser_lookup_name (parser, argument,
10611 /*is_template=*/template_p,
10612 /*is_namespace=*/false,
10613 /*check_dependency=*/true,
10614 /*ambiguous_decls=*/NULL,
10615 argument_start_token->location);
10616 if (TREE_CODE (argument) != TEMPLATE_DECL
10617 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10618 cp_parser_error (parser, "expected template-name");
10620 if (cp_parser_parse_definitely (parser))
10622 /* It must be a non-type argument. There permitted cases are given
10623 in [temp.arg.nontype]:
10625 -- an integral constant-expression of integral or enumeration
10628 -- the name of a non-type template-parameter; or
10630 -- the name of an object or function with external linkage...
10632 -- the address of an object or function with external linkage...
10634 -- a pointer to member... */
10635 /* Look for a non-type template parameter. */
10636 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10638 cp_parser_parse_tentatively (parser);
10639 argument = cp_parser_primary_expression (parser,
10640 /*address_p=*/false,
10642 /*template_arg_p=*/true,
10644 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10645 || !cp_parser_next_token_ends_template_argument_p (parser))
10646 cp_parser_simulate_error (parser);
10647 if (cp_parser_parse_definitely (parser))
10651 /* If the next token is "&", the argument must be the address of an
10652 object or function with external linkage. */
10653 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10655 cp_lexer_consume_token (parser->lexer);
10656 /* See if we might have an id-expression. */
10657 token = cp_lexer_peek_token (parser->lexer);
10658 if (token->type == CPP_NAME
10659 || token->keyword == RID_OPERATOR
10660 || token->type == CPP_SCOPE
10661 || token->type == CPP_TEMPLATE_ID
10662 || token->type == CPP_NESTED_NAME_SPECIFIER)
10664 cp_parser_parse_tentatively (parser);
10665 argument = cp_parser_primary_expression (parser,
10668 /*template_arg_p=*/true,
10670 if (cp_parser_error_occurred (parser)
10671 || !cp_parser_next_token_ends_template_argument_p (parser))
10672 cp_parser_abort_tentative_parse (parser);
10675 if (TREE_CODE (argument) == INDIRECT_REF)
10677 gcc_assert (REFERENCE_REF_P (argument));
10678 argument = TREE_OPERAND (argument, 0);
10681 if (TREE_CODE (argument) == VAR_DECL)
10683 /* A variable without external linkage might still be a
10684 valid constant-expression, so no error is issued here
10685 if the external-linkage check fails. */
10686 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10687 cp_parser_simulate_error (parser);
10689 else if (is_overloaded_fn (argument))
10690 /* All overloaded functions are allowed; if the external
10691 linkage test does not pass, an error will be issued
10695 && (TREE_CODE (argument) == OFFSET_REF
10696 || TREE_CODE (argument) == SCOPE_REF))
10697 /* A pointer-to-member. */
10699 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10702 cp_parser_simulate_error (parser);
10704 if (cp_parser_parse_definitely (parser))
10707 argument = build_x_unary_op (ADDR_EXPR, argument,
10708 tf_warning_or_error);
10713 /* If the argument started with "&", there are no other valid
10714 alternatives at this point. */
10717 cp_parser_error (parser, "invalid non-type template argument");
10718 return error_mark_node;
10721 /* If the argument wasn't successfully parsed as a type-id followed
10722 by '>>', the argument can only be a constant expression now.
10723 Otherwise, we try parsing the constant-expression tentatively,
10724 because the argument could really be a type-id. */
10726 cp_parser_parse_tentatively (parser);
10727 argument = cp_parser_constant_expression (parser,
10728 /*allow_non_constant_p=*/false,
10729 /*non_constant_p=*/NULL);
10730 argument = fold_non_dependent_expr (argument);
10731 if (!maybe_type_id)
10733 if (!cp_parser_next_token_ends_template_argument_p (parser))
10734 cp_parser_error (parser, "expected template-argument");
10735 if (cp_parser_parse_definitely (parser))
10737 /* We did our best to parse the argument as a non type-id, but that
10738 was the only alternative that matched (albeit with a '>' after
10739 it). We can assume it's just a typo from the user, and a
10740 diagnostic will then be issued. */
10741 return cp_parser_template_type_arg (parser);
10744 /* Parse an explicit-instantiation.
10746 explicit-instantiation:
10747 template declaration
10749 Although the standard says `declaration', what it really means is:
10751 explicit-instantiation:
10752 template decl-specifier-seq [opt] declarator [opt] ;
10754 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10755 supposed to be allowed. A defect report has been filed about this
10760 explicit-instantiation:
10761 storage-class-specifier template
10762 decl-specifier-seq [opt] declarator [opt] ;
10763 function-specifier template
10764 decl-specifier-seq [opt] declarator [opt] ; */
10767 cp_parser_explicit_instantiation (cp_parser* parser)
10769 int declares_class_or_enum;
10770 cp_decl_specifier_seq decl_specifiers;
10771 tree extension_specifier = NULL_TREE;
10774 /* Look for an (optional) storage-class-specifier or
10775 function-specifier. */
10776 if (cp_parser_allow_gnu_extensions_p (parser))
10778 extension_specifier
10779 = cp_parser_storage_class_specifier_opt (parser);
10780 if (!extension_specifier)
10781 extension_specifier
10782 = cp_parser_function_specifier_opt (parser,
10783 /*decl_specs=*/NULL);
10786 /* Look for the `template' keyword. */
10787 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10788 /* Let the front end know that we are processing an explicit
10790 begin_explicit_instantiation ();
10791 /* [temp.explicit] says that we are supposed to ignore access
10792 control while processing explicit instantiation directives. */
10793 push_deferring_access_checks (dk_no_check);
10794 /* Parse a decl-specifier-seq. */
10795 token = cp_lexer_peek_token (parser->lexer);
10796 cp_parser_decl_specifier_seq (parser,
10797 CP_PARSER_FLAGS_OPTIONAL,
10799 &declares_class_or_enum);
10800 /* If there was exactly one decl-specifier, and it declared a class,
10801 and there's no declarator, then we have an explicit type
10803 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10807 type = check_tag_decl (&decl_specifiers);
10808 /* Turn access control back on for names used during
10809 template instantiation. */
10810 pop_deferring_access_checks ();
10812 do_type_instantiation (type, extension_specifier,
10813 /*complain=*/tf_error);
10817 cp_declarator *declarator;
10820 /* Parse the declarator. */
10822 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10823 /*ctor_dtor_or_conv_p=*/NULL,
10824 /*parenthesized_p=*/NULL,
10825 /*member_p=*/false);
10826 if (declares_class_or_enum & 2)
10827 cp_parser_check_for_definition_in_return_type (declarator,
10828 decl_specifiers.type,
10829 decl_specifiers.type_location);
10830 if (declarator != cp_error_declarator)
10832 decl = grokdeclarator (declarator, &decl_specifiers,
10833 NORMAL, 0, &decl_specifiers.attributes);
10834 /* Turn access control back on for names used during
10835 template instantiation. */
10836 pop_deferring_access_checks ();
10837 /* Do the explicit instantiation. */
10838 do_decl_instantiation (decl, extension_specifier);
10842 pop_deferring_access_checks ();
10843 /* Skip the body of the explicit instantiation. */
10844 cp_parser_skip_to_end_of_statement (parser);
10847 /* We're done with the instantiation. */
10848 end_explicit_instantiation ();
10850 cp_parser_consume_semicolon_at_end_of_statement (parser);
10853 /* Parse an explicit-specialization.
10855 explicit-specialization:
10856 template < > declaration
10858 Although the standard says `declaration', what it really means is:
10860 explicit-specialization:
10861 template <> decl-specifier [opt] init-declarator [opt] ;
10862 template <> function-definition
10863 template <> explicit-specialization
10864 template <> template-declaration */
10867 cp_parser_explicit_specialization (cp_parser* parser)
10869 bool need_lang_pop;
10870 cp_token *token = cp_lexer_peek_token (parser->lexer);
10872 /* Look for the `template' keyword. */
10873 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10874 /* Look for the `<'. */
10875 cp_parser_require (parser, CPP_LESS, "%<<%>");
10876 /* Look for the `>'. */
10877 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10878 /* We have processed another parameter list. */
10879 ++parser->num_template_parameter_lists;
10882 A template ... explicit specialization ... shall not have C
10884 if (current_lang_name == lang_name_c)
10886 error ("%Htemplate specialization with C linkage", &token->location);
10887 /* Give it C++ linkage to avoid confusing other parts of the
10889 push_lang_context (lang_name_cplusplus);
10890 need_lang_pop = true;
10893 need_lang_pop = false;
10894 /* Let the front end know that we are beginning a specialization. */
10895 if (!begin_specialization ())
10897 end_specialization ();
10901 /* If the next keyword is `template', we need to figure out whether
10902 or not we're looking a template-declaration. */
10903 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10905 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10906 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10907 cp_parser_template_declaration_after_export (parser,
10908 /*member_p=*/false);
10910 cp_parser_explicit_specialization (parser);
10913 /* Parse the dependent declaration. */
10914 cp_parser_single_declaration (parser,
10916 /*member_p=*/false,
10917 /*explicit_specialization_p=*/true,
10918 /*friend_p=*/NULL);
10919 /* We're done with the specialization. */
10920 end_specialization ();
10921 /* For the erroneous case of a template with C linkage, we pushed an
10922 implicit C++ linkage scope; exit that scope now. */
10924 pop_lang_context ();
10925 /* We're done with this parameter list. */
10926 --parser->num_template_parameter_lists;
10929 /* Parse a type-specifier.
10932 simple-type-specifier
10935 elaborated-type-specifier
10943 Returns a representation of the type-specifier. For a
10944 class-specifier, enum-specifier, or elaborated-type-specifier, a
10945 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10947 The parser flags FLAGS is used to control type-specifier parsing.
10949 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10950 in a decl-specifier-seq.
10952 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10953 class-specifier, enum-specifier, or elaborated-type-specifier, then
10954 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10955 if a type is declared; 2 if it is defined. Otherwise, it is set to
10958 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10959 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10960 is set to FALSE. */
10963 cp_parser_type_specifier (cp_parser* parser,
10964 cp_parser_flags flags,
10965 cp_decl_specifier_seq *decl_specs,
10966 bool is_declaration,
10967 int* declares_class_or_enum,
10968 bool* is_cv_qualifier)
10970 tree type_spec = NULL_TREE;
10973 cp_decl_spec ds = ds_last;
10975 /* Assume this type-specifier does not declare a new type. */
10976 if (declares_class_or_enum)
10977 *declares_class_or_enum = 0;
10978 /* And that it does not specify a cv-qualifier. */
10979 if (is_cv_qualifier)
10980 *is_cv_qualifier = false;
10981 /* Peek at the next token. */
10982 token = cp_lexer_peek_token (parser->lexer);
10984 /* If we're looking at a keyword, we can use that to guide the
10985 production we choose. */
10986 keyword = token->keyword;
10990 /* Look for the enum-specifier. */
10991 type_spec = cp_parser_enum_specifier (parser);
10992 /* If that worked, we're done. */
10995 if (declares_class_or_enum)
10996 *declares_class_or_enum = 2;
10998 cp_parser_set_decl_spec_type (decl_specs,
11001 /*user_defined_p=*/true);
11005 goto elaborated_type_specifier;
11007 /* Any of these indicate either a class-specifier, or an
11008 elaborated-type-specifier. */
11012 /* Parse tentatively so that we can back up if we don't find a
11013 class-specifier. */
11014 cp_parser_parse_tentatively (parser);
11015 /* Look for the class-specifier. */
11016 type_spec = cp_parser_class_specifier (parser);
11017 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11018 /* If that worked, we're done. */
11019 if (cp_parser_parse_definitely (parser))
11021 if (declares_class_or_enum)
11022 *declares_class_or_enum = 2;
11024 cp_parser_set_decl_spec_type (decl_specs,
11027 /*user_defined_p=*/true);
11031 /* Fall through. */
11032 elaborated_type_specifier:
11033 /* We're declaring (not defining) a class or enum. */
11034 if (declares_class_or_enum)
11035 *declares_class_or_enum = 1;
11037 /* Fall through. */
11039 /* Look for an elaborated-type-specifier. */
11041 = (cp_parser_elaborated_type_specifier
11043 decl_specs && decl_specs->specs[(int) ds_friend],
11046 cp_parser_set_decl_spec_type (decl_specs,
11049 /*user_defined_p=*/true);
11054 if (is_cv_qualifier)
11055 *is_cv_qualifier = true;
11060 if (is_cv_qualifier)
11061 *is_cv_qualifier = true;
11066 if (is_cv_qualifier)
11067 *is_cv_qualifier = true;
11071 /* The `__complex__' keyword is a GNU extension. */
11079 /* Handle simple keywords. */
11084 ++decl_specs->specs[(int)ds];
11085 decl_specs->any_specifiers_p = true;
11087 return cp_lexer_consume_token (parser->lexer)->u.value;
11090 /* If we do not already have a type-specifier, assume we are looking
11091 at a simple-type-specifier. */
11092 type_spec = cp_parser_simple_type_specifier (parser,
11096 /* If we didn't find a type-specifier, and a type-specifier was not
11097 optional in this context, issue an error message. */
11098 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11100 cp_parser_error (parser, "expected type specifier");
11101 return error_mark_node;
11107 /* Parse a simple-type-specifier.
11109 simple-type-specifier:
11110 :: [opt] nested-name-specifier [opt] type-name
11111 :: [opt] nested-name-specifier template template-id
11126 simple-type-specifier:
11128 decltype ( expression )
11134 simple-type-specifier:
11135 __typeof__ unary-expression
11136 __typeof__ ( type-id )
11138 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11139 appropriately updated. */
11142 cp_parser_simple_type_specifier (cp_parser* parser,
11143 cp_decl_specifier_seq *decl_specs,
11144 cp_parser_flags flags)
11146 tree type = NULL_TREE;
11149 /* Peek at the next token. */
11150 token = cp_lexer_peek_token (parser->lexer);
11152 /* If we're looking at a keyword, things are easy. */
11153 switch (token->keyword)
11157 decl_specs->explicit_char_p = true;
11158 type = char_type_node;
11161 type = char16_type_node;
11164 type = char32_type_node;
11167 type = wchar_type_node;
11170 type = boolean_type_node;
11174 ++decl_specs->specs[(int) ds_short];
11175 type = short_integer_type_node;
11179 decl_specs->explicit_int_p = true;
11180 type = integer_type_node;
11184 ++decl_specs->specs[(int) ds_long];
11185 type = long_integer_type_node;
11189 ++decl_specs->specs[(int) ds_signed];
11190 type = integer_type_node;
11194 ++decl_specs->specs[(int) ds_unsigned];
11195 type = unsigned_type_node;
11198 type = float_type_node;
11201 type = double_type_node;
11204 type = void_type_node;
11208 maybe_warn_cpp0x ("C++0x auto");
11209 type = make_auto ();
11213 /* Parse the `decltype' type. */
11214 type = cp_parser_decltype (parser);
11217 cp_parser_set_decl_spec_type (decl_specs, type,
11219 /*user_defined_p=*/true);
11224 /* Consume the `typeof' token. */
11225 cp_lexer_consume_token (parser->lexer);
11226 /* Parse the operand to `typeof'. */
11227 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11228 /* If it is not already a TYPE, take its type. */
11229 if (!TYPE_P (type))
11230 type = finish_typeof (type);
11233 cp_parser_set_decl_spec_type (decl_specs, type,
11235 /*user_defined_p=*/true);
11243 /* If the type-specifier was for a built-in type, we're done. */
11248 /* Record the type. */
11250 && (token->keyword != RID_SIGNED
11251 && token->keyword != RID_UNSIGNED
11252 && token->keyword != RID_SHORT
11253 && token->keyword != RID_LONG))
11254 cp_parser_set_decl_spec_type (decl_specs,
11257 /*user_defined=*/false);
11259 decl_specs->any_specifiers_p = true;
11261 /* Consume the token. */
11262 id = cp_lexer_consume_token (parser->lexer)->u.value;
11264 /* There is no valid C++ program where a non-template type is
11265 followed by a "<". That usually indicates that the user thought
11266 that the type was a template. */
11267 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11269 return TYPE_NAME (type);
11272 /* The type-specifier must be a user-defined type. */
11273 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11278 /* Don't gobble tokens or issue error messages if this is an
11279 optional type-specifier. */
11280 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11281 cp_parser_parse_tentatively (parser);
11283 /* Look for the optional `::' operator. */
11285 = (cp_parser_global_scope_opt (parser,
11286 /*current_scope_valid_p=*/false)
11288 /* Look for the nested-name specifier. */
11290 = (cp_parser_nested_name_specifier_opt (parser,
11291 /*typename_keyword_p=*/false,
11292 /*check_dependency_p=*/true,
11294 /*is_declaration=*/false)
11296 token = cp_lexer_peek_token (parser->lexer);
11297 /* If we have seen a nested-name-specifier, and the next token
11298 is `template', then we are using the template-id production. */
11300 && cp_parser_optional_template_keyword (parser))
11302 /* Look for the template-id. */
11303 type = cp_parser_template_id (parser,
11304 /*template_keyword_p=*/true,
11305 /*check_dependency_p=*/true,
11306 /*is_declaration=*/false);
11307 /* If the template-id did not name a type, we are out of
11309 if (TREE_CODE (type) != TYPE_DECL)
11311 cp_parser_error (parser, "expected template-id for type");
11315 /* Otherwise, look for a type-name. */
11317 type = cp_parser_type_name (parser);
11318 /* Keep track of all name-lookups performed in class scopes. */
11322 && TREE_CODE (type) == TYPE_DECL
11323 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11324 maybe_note_name_used_in_class (DECL_NAME (type), type);
11325 /* If it didn't work out, we don't have a TYPE. */
11326 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11327 && !cp_parser_parse_definitely (parser))
11329 if (type && decl_specs)
11330 cp_parser_set_decl_spec_type (decl_specs, type,
11332 /*user_defined=*/true);
11335 /* If we didn't get a type-name, issue an error message. */
11336 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11338 cp_parser_error (parser, "expected type-name");
11339 return error_mark_node;
11342 /* There is no valid C++ program where a non-template type is
11343 followed by a "<". That usually indicates that the user thought
11344 that the type was a template. */
11345 if (type && type != error_mark_node)
11347 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11348 If it is, then the '<'...'>' enclose protocol names rather than
11349 template arguments, and so everything is fine. */
11350 if (c_dialect_objc ()
11351 && (objc_is_id (type) || objc_is_class_name (type)))
11353 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11354 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11356 /* Clobber the "unqualified" type previously entered into
11357 DECL_SPECS with the new, improved protocol-qualified version. */
11359 decl_specs->type = qual_type;
11364 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11371 /* Parse a type-name.
11384 Returns a TYPE_DECL for the type. */
11387 cp_parser_type_name (cp_parser* parser)
11391 /* We can't know yet whether it is a class-name or not. */
11392 cp_parser_parse_tentatively (parser);
11393 /* Try a class-name. */
11394 type_decl = cp_parser_class_name (parser,
11395 /*typename_keyword_p=*/false,
11396 /*template_keyword_p=*/false,
11398 /*check_dependency_p=*/true,
11399 /*class_head_p=*/false,
11400 /*is_declaration=*/false);
11401 /* If it's not a class-name, keep looking. */
11402 if (!cp_parser_parse_definitely (parser))
11404 /* It must be a typedef-name or an enum-name. */
11405 return cp_parser_nonclass_name (parser);
11411 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11419 Returns a TYPE_DECL for the type. */
11422 cp_parser_nonclass_name (cp_parser* parser)
11427 cp_token *token = cp_lexer_peek_token (parser->lexer);
11428 identifier = cp_parser_identifier (parser);
11429 if (identifier == error_mark_node)
11430 return error_mark_node;
11432 /* Look up the type-name. */
11433 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11435 if (TREE_CODE (type_decl) != TYPE_DECL
11436 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11438 /* See if this is an Objective-C type. */
11439 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11440 tree type = objc_get_protocol_qualified_type (identifier, protos);
11442 type_decl = TYPE_NAME (type);
11445 /* Issue an error if we did not find a type-name. */
11446 if (TREE_CODE (type_decl) != TYPE_DECL)
11448 if (!cp_parser_simulate_error (parser))
11449 cp_parser_name_lookup_error (parser, identifier, type_decl,
11450 "is not a type", token->location);
11451 return error_mark_node;
11453 /* Remember that the name was used in the definition of the
11454 current class so that we can check later to see if the
11455 meaning would have been different after the class was
11456 entirely defined. */
11457 else if (type_decl != error_mark_node
11459 maybe_note_name_used_in_class (identifier, type_decl);
11464 /* Parse an elaborated-type-specifier. Note that the grammar given
11465 here incorporates the resolution to DR68.
11467 elaborated-type-specifier:
11468 class-key :: [opt] nested-name-specifier [opt] identifier
11469 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11470 enum-key :: [opt] nested-name-specifier [opt] identifier
11471 typename :: [opt] nested-name-specifier identifier
11472 typename :: [opt] nested-name-specifier template [opt]
11477 elaborated-type-specifier:
11478 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11479 class-key attributes :: [opt] nested-name-specifier [opt]
11480 template [opt] template-id
11481 enum attributes :: [opt] nested-name-specifier [opt] identifier
11483 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11484 declared `friend'. If IS_DECLARATION is TRUE, then this
11485 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11486 something is being declared.
11488 Returns the TYPE specified. */
11491 cp_parser_elaborated_type_specifier (cp_parser* parser,
11493 bool is_declaration)
11495 enum tag_types tag_type;
11497 tree type = NULL_TREE;
11498 tree attributes = NULL_TREE;
11499 cp_token *token = NULL;
11501 /* See if we're looking at the `enum' keyword. */
11502 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11504 /* Consume the `enum' token. */
11505 cp_lexer_consume_token (parser->lexer);
11506 /* Remember that it's an enumeration type. */
11507 tag_type = enum_type;
11508 /* Parse the optional `struct' or `class' key (for C++0x scoped
11510 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11511 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11513 if (cxx_dialect == cxx98)
11514 maybe_warn_cpp0x ("scoped enums");
11516 /* Consume the `struct' or `class'. */
11517 cp_lexer_consume_token (parser->lexer);
11519 /* Parse the attributes. */
11520 attributes = cp_parser_attributes_opt (parser);
11522 /* Or, it might be `typename'. */
11523 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11526 /* Consume the `typename' token. */
11527 cp_lexer_consume_token (parser->lexer);
11528 /* Remember that it's a `typename' type. */
11529 tag_type = typename_type;
11530 /* The `typename' keyword is only allowed in templates. */
11531 if (!processing_template_decl)
11532 permerror (input_location, "using %<typename%> outside of template");
11534 /* Otherwise it must be a class-key. */
11537 tag_type = cp_parser_class_key (parser);
11538 if (tag_type == none_type)
11539 return error_mark_node;
11540 /* Parse the attributes. */
11541 attributes = cp_parser_attributes_opt (parser);
11544 /* Look for the `::' operator. */
11545 cp_parser_global_scope_opt (parser,
11546 /*current_scope_valid_p=*/false);
11547 /* Look for the nested-name-specifier. */
11548 if (tag_type == typename_type)
11550 if (!cp_parser_nested_name_specifier (parser,
11551 /*typename_keyword_p=*/true,
11552 /*check_dependency_p=*/true,
11555 return error_mark_node;
11558 /* Even though `typename' is not present, the proposed resolution
11559 to Core Issue 180 says that in `class A<T>::B', `B' should be
11560 considered a type-name, even if `A<T>' is dependent. */
11561 cp_parser_nested_name_specifier_opt (parser,
11562 /*typename_keyword_p=*/true,
11563 /*check_dependency_p=*/true,
11566 /* For everything but enumeration types, consider a template-id.
11567 For an enumeration type, consider only a plain identifier. */
11568 if (tag_type != enum_type)
11570 bool template_p = false;
11573 /* Allow the `template' keyword. */
11574 template_p = cp_parser_optional_template_keyword (parser);
11575 /* If we didn't see `template', we don't know if there's a
11576 template-id or not. */
11578 cp_parser_parse_tentatively (parser);
11579 /* Parse the template-id. */
11580 token = cp_lexer_peek_token (parser->lexer);
11581 decl = cp_parser_template_id (parser, template_p,
11582 /*check_dependency_p=*/true,
11584 /* If we didn't find a template-id, look for an ordinary
11586 if (!template_p && !cp_parser_parse_definitely (parser))
11588 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11589 in effect, then we must assume that, upon instantiation, the
11590 template will correspond to a class. */
11591 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11592 && tag_type == typename_type)
11593 type = make_typename_type (parser->scope, decl,
11595 /*complain=*/tf_error);
11596 /* If the `typename' keyword is in effect and DECL is not a type
11597 decl. Then type is non existant. */
11598 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11601 type = TREE_TYPE (decl);
11606 token = cp_lexer_peek_token (parser->lexer);
11607 identifier = cp_parser_identifier (parser);
11609 if (identifier == error_mark_node)
11611 parser->scope = NULL_TREE;
11612 return error_mark_node;
11615 /* For a `typename', we needn't call xref_tag. */
11616 if (tag_type == typename_type
11617 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11618 return cp_parser_make_typename_type (parser, parser->scope,
11621 /* Look up a qualified name in the usual way. */
11625 tree ambiguous_decls;
11627 decl = cp_parser_lookup_name (parser, identifier,
11629 /*is_template=*/false,
11630 /*is_namespace=*/false,
11631 /*check_dependency=*/true,
11635 /* If the lookup was ambiguous, an error will already have been
11637 if (ambiguous_decls)
11638 return error_mark_node;
11640 /* If we are parsing friend declaration, DECL may be a
11641 TEMPLATE_DECL tree node here. However, we need to check
11642 whether this TEMPLATE_DECL results in valid code. Consider
11643 the following example:
11646 template <class T> class C {};
11649 template <class T> friend class N::C; // #1, valid code
11651 template <class T> class Y {
11652 friend class N::C; // #2, invalid code
11655 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11656 name lookup of `N::C'. We see that friend declaration must
11657 be template for the code to be valid. Note that
11658 processing_template_decl does not work here since it is
11659 always 1 for the above two cases. */
11661 decl = (cp_parser_maybe_treat_template_as_class
11662 (decl, /*tag_name_p=*/is_friend
11663 && parser->num_template_parameter_lists));
11665 if (TREE_CODE (decl) != TYPE_DECL)
11667 cp_parser_diagnose_invalid_type_name (parser,
11671 return error_mark_node;
11674 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11676 bool allow_template = (parser->num_template_parameter_lists
11677 || DECL_SELF_REFERENCE_P (decl));
11678 type = check_elaborated_type_specifier (tag_type, decl,
11681 if (type == error_mark_node)
11682 return error_mark_node;
11685 /* Forward declarations of nested types, such as
11690 are invalid unless all components preceding the final '::'
11691 are complete. If all enclosing types are complete, these
11692 declarations become merely pointless.
11694 Invalid forward declarations of nested types are errors
11695 caught elsewhere in parsing. Those that are pointless arrive
11698 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11699 && !is_friend && !processing_explicit_instantiation)
11700 warning (0, "declaration %qD does not declare anything", decl);
11702 type = TREE_TYPE (decl);
11706 /* An elaborated-type-specifier sometimes introduces a new type and
11707 sometimes names an existing type. Normally, the rule is that it
11708 introduces a new type only if there is not an existing type of
11709 the same name already in scope. For example, given:
11712 void f() { struct S s; }
11714 the `struct S' in the body of `f' is the same `struct S' as in
11715 the global scope; the existing definition is used. However, if
11716 there were no global declaration, this would introduce a new
11717 local class named `S'.
11719 An exception to this rule applies to the following code:
11721 namespace N { struct S; }
11723 Here, the elaborated-type-specifier names a new type
11724 unconditionally; even if there is already an `S' in the
11725 containing scope this declaration names a new type.
11726 This exception only applies if the elaborated-type-specifier
11727 forms the complete declaration:
11731 A declaration consisting solely of `class-key identifier ;' is
11732 either a redeclaration of the name in the current scope or a
11733 forward declaration of the identifier as a class name. It
11734 introduces the name into the current scope.
11736 We are in this situation precisely when the next token is a `;'.
11738 An exception to the exception is that a `friend' declaration does
11739 *not* name a new type; i.e., given:
11741 struct S { friend struct T; };
11743 `T' is not a new type in the scope of `S'.
11745 Also, `new struct S' or `sizeof (struct S)' never results in the
11746 definition of a new type; a new type can only be declared in a
11747 declaration context. */
11753 /* Friends have special name lookup rules. */
11754 ts = ts_within_enclosing_non_class;
11755 else if (is_declaration
11756 && cp_lexer_next_token_is (parser->lexer,
11758 /* This is a `class-key identifier ;' */
11764 (parser->num_template_parameter_lists
11765 && (cp_parser_next_token_starts_class_definition_p (parser)
11766 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11767 /* An unqualified name was used to reference this type, so
11768 there were no qualifying templates. */
11769 if (!cp_parser_check_template_parameters (parser,
11770 /*num_templates=*/0,
11772 /*declarator=*/NULL))
11773 return error_mark_node;
11774 type = xref_tag (tag_type, identifier, ts, template_p);
11778 if (type == error_mark_node)
11779 return error_mark_node;
11781 /* Allow attributes on forward declarations of classes. */
11784 if (TREE_CODE (type) == TYPENAME_TYPE)
11785 warning (OPT_Wattributes,
11786 "attributes ignored on uninstantiated type");
11787 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11788 && ! processing_explicit_instantiation)
11789 warning (OPT_Wattributes,
11790 "attributes ignored on template instantiation");
11791 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11792 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11794 warning (OPT_Wattributes,
11795 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11798 if (tag_type != enum_type)
11799 cp_parser_check_class_key (tag_type, type);
11801 /* A "<" cannot follow an elaborated type specifier. If that
11802 happens, the user was probably trying to form a template-id. */
11803 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11808 /* Parse an enum-specifier.
11811 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11816 enum struct [C++0x]
11819 : type-specifier-seq
11822 enum-key attributes[opt] identifier [opt] enum-base [opt]
11823 { enumerator-list [opt] }attributes[opt]
11825 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11826 if the token stream isn't an enum-specifier after all. */
11829 cp_parser_enum_specifier (cp_parser* parser)
11834 bool scoped_enum_p = false;
11835 bool has_underlying_type = false;
11836 tree underlying_type = NULL_TREE;
11838 /* Parse tentatively so that we can back up if we don't find a
11840 cp_parser_parse_tentatively (parser);
11842 /* Caller guarantees that the current token is 'enum', an identifier
11843 possibly follows, and the token after that is an opening brace.
11844 If we don't have an identifier, fabricate an anonymous name for
11845 the enumeration being defined. */
11846 cp_lexer_consume_token (parser->lexer);
11848 /* Parse the "class" or "struct", which indicates a scoped
11849 enumeration type in C++0x. */
11850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11851 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11853 if (cxx_dialect == cxx98)
11854 maybe_warn_cpp0x ("scoped enums");
11856 /* Consume the `struct' or `class' token. */
11857 cp_lexer_consume_token (parser->lexer);
11859 scoped_enum_p = true;
11862 attributes = cp_parser_attributes_opt (parser);
11864 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11865 identifier = cp_parser_identifier (parser);
11867 identifier = make_anon_name ();
11869 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11870 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11872 cp_decl_specifier_seq type_specifiers;
11874 /* At this point this is surely not elaborated type specifier. */
11875 if (!cp_parser_parse_definitely (parser))
11878 if (cxx_dialect == cxx98)
11879 maybe_warn_cpp0x ("scoped enums");
11881 /* Consume the `:'. */
11882 cp_lexer_consume_token (parser->lexer);
11884 has_underlying_type = true;
11886 /* Parse the type-specifier-seq. */
11887 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11890 /* If that didn't work, stop. */
11891 if (type_specifiers.type != error_mark_node)
11893 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11894 /*initialized=*/0, NULL);
11895 if (underlying_type == error_mark_node)
11896 underlying_type = NULL_TREE;
11900 /* Look for the `{' but don't consume it yet. */
11901 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11903 cp_parser_error (parser, "expected %<{%>");
11904 if (has_underlying_type)
11908 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11911 /* Issue an error message if type-definitions are forbidden here. */
11912 if (!cp_parser_check_type_definition (parser))
11913 type = error_mark_node;
11915 /* Create the new type. We do this before consuming the opening
11916 brace so the enum will be recorded as being on the line of its
11917 tag (or the 'enum' keyword, if there is no tag). */
11918 type = start_enum (identifier, underlying_type, scoped_enum_p);
11920 /* Consume the opening brace. */
11921 cp_lexer_consume_token (parser->lexer);
11923 if (type == error_mark_node)
11925 cp_parser_skip_to_end_of_block_or_statement (parser);
11926 return error_mark_node;
11929 /* If the next token is not '}', then there are some enumerators. */
11930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11931 cp_parser_enumerator_list (parser, type);
11933 /* Consume the final '}'. */
11934 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11936 /* Look for trailing attributes to apply to this enumeration, and
11937 apply them if appropriate. */
11938 if (cp_parser_allow_gnu_extensions_p (parser))
11940 tree trailing_attr = cp_parser_attributes_opt (parser);
11941 trailing_attr = chainon (trailing_attr, attributes);
11942 cplus_decl_attributes (&type,
11944 (int) ATTR_FLAG_TYPE_IN_PLACE);
11947 /* Finish up the enumeration. */
11948 finish_enum (type);
11953 /* Parse an enumerator-list. The enumerators all have the indicated
11957 enumerator-definition
11958 enumerator-list , enumerator-definition */
11961 cp_parser_enumerator_list (cp_parser* parser, tree type)
11965 /* Parse an enumerator-definition. */
11966 cp_parser_enumerator_definition (parser, type);
11968 /* If the next token is not a ',', we've reached the end of
11970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11972 /* Otherwise, consume the `,' and keep going. */
11973 cp_lexer_consume_token (parser->lexer);
11974 /* If the next token is a `}', there is a trailing comma. */
11975 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11977 if (!in_system_header)
11978 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11984 /* Parse an enumerator-definition. The enumerator has the indicated
11987 enumerator-definition:
11989 enumerator = constant-expression
11995 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12000 /* Look for the identifier. */
12001 identifier = cp_parser_identifier (parser);
12002 if (identifier == error_mark_node)
12005 /* If the next token is an '=', then there is an explicit value. */
12006 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12008 /* Consume the `=' token. */
12009 cp_lexer_consume_token (parser->lexer);
12010 /* Parse the value. */
12011 value = cp_parser_constant_expression (parser,
12012 /*allow_non_constant_p=*/false,
12018 /* If we are processing a template, make sure the initializer of the
12019 enumerator doesn't contain any bare template parameter pack. */
12020 if (check_for_bare_parameter_packs (value))
12021 value = error_mark_node;
12023 /* Create the enumerator. */
12024 build_enumerator (identifier, value, type);
12027 /* Parse a namespace-name.
12030 original-namespace-name
12033 Returns the NAMESPACE_DECL for the namespace. */
12036 cp_parser_namespace_name (cp_parser* parser)
12039 tree namespace_decl;
12041 cp_token *token = cp_lexer_peek_token (parser->lexer);
12043 /* Get the name of the namespace. */
12044 identifier = cp_parser_identifier (parser);
12045 if (identifier == error_mark_node)
12046 return error_mark_node;
12048 /* Look up the identifier in the currently active scope. Look only
12049 for namespaces, due to:
12051 [basic.lookup.udir]
12053 When looking up a namespace-name in a using-directive or alias
12054 definition, only namespace names are considered.
12058 [basic.lookup.qual]
12060 During the lookup of a name preceding the :: scope resolution
12061 operator, object, function, and enumerator names are ignored.
12063 (Note that cp_parser_qualifying_entity only calls this
12064 function if the token after the name is the scope resolution
12066 namespace_decl = cp_parser_lookup_name (parser, identifier,
12068 /*is_template=*/false,
12069 /*is_namespace=*/true,
12070 /*check_dependency=*/true,
12071 /*ambiguous_decls=*/NULL,
12073 /* If it's not a namespace, issue an error. */
12074 if (namespace_decl == error_mark_node
12075 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12077 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12078 error ("%H%qD is not a namespace-name", &token->location, identifier);
12079 cp_parser_error (parser, "expected namespace-name");
12080 namespace_decl = error_mark_node;
12083 return namespace_decl;
12086 /* Parse a namespace-definition.
12088 namespace-definition:
12089 named-namespace-definition
12090 unnamed-namespace-definition
12092 named-namespace-definition:
12093 original-namespace-definition
12094 extension-namespace-definition
12096 original-namespace-definition:
12097 namespace identifier { namespace-body }
12099 extension-namespace-definition:
12100 namespace original-namespace-name { namespace-body }
12102 unnamed-namespace-definition:
12103 namespace { namespace-body } */
12106 cp_parser_namespace_definition (cp_parser* parser)
12108 tree identifier, attribs;
12109 bool has_visibility;
12112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12115 cp_lexer_consume_token (parser->lexer);
12120 /* Look for the `namespace' keyword. */
12121 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12123 /* Get the name of the namespace. We do not attempt to distinguish
12124 between an original-namespace-definition and an
12125 extension-namespace-definition at this point. The semantic
12126 analysis routines are responsible for that. */
12127 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12128 identifier = cp_parser_identifier (parser);
12130 identifier = NULL_TREE;
12132 /* Parse any specified attributes. */
12133 attribs = cp_parser_attributes_opt (parser);
12135 /* Look for the `{' to start the namespace. */
12136 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12137 /* Start the namespace. */
12138 push_namespace (identifier);
12140 /* "inline namespace" is equivalent to a stub namespace definition
12141 followed by a strong using directive. */
12144 tree name_space = current_namespace;
12145 /* Set up namespace association. */
12146 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12147 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12148 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12149 /* Import the contents of the inline namespace. */
12151 do_using_directive (name_space);
12152 push_namespace (identifier);
12155 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12157 /* Parse the body of the namespace. */
12158 cp_parser_namespace_body (parser);
12160 #ifdef HANDLE_PRAGMA_VISIBILITY
12161 if (has_visibility)
12165 /* Finish the namespace. */
12167 /* Look for the final `}'. */
12168 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12171 /* Parse a namespace-body.
12174 declaration-seq [opt] */
12177 cp_parser_namespace_body (cp_parser* parser)
12179 cp_parser_declaration_seq_opt (parser);
12182 /* Parse a namespace-alias-definition.
12184 namespace-alias-definition:
12185 namespace identifier = qualified-namespace-specifier ; */
12188 cp_parser_namespace_alias_definition (cp_parser* parser)
12191 tree namespace_specifier;
12193 cp_token *token = cp_lexer_peek_token (parser->lexer);
12195 /* Look for the `namespace' keyword. */
12196 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12197 /* Look for the identifier. */
12198 identifier = cp_parser_identifier (parser);
12199 if (identifier == error_mark_node)
12201 /* Look for the `=' token. */
12202 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12203 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12205 error ("%H%<namespace%> definition is not allowed here", &token->location);
12206 /* Skip the definition. */
12207 cp_lexer_consume_token (parser->lexer);
12208 if (cp_parser_skip_to_closing_brace (parser))
12209 cp_lexer_consume_token (parser->lexer);
12212 cp_parser_require (parser, CPP_EQ, "%<=%>");
12213 /* Look for the qualified-namespace-specifier. */
12214 namespace_specifier
12215 = cp_parser_qualified_namespace_specifier (parser);
12216 /* Look for the `;' token. */
12217 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12219 /* Register the alias in the symbol table. */
12220 do_namespace_alias (identifier, namespace_specifier);
12223 /* Parse a qualified-namespace-specifier.
12225 qualified-namespace-specifier:
12226 :: [opt] nested-name-specifier [opt] namespace-name
12228 Returns a NAMESPACE_DECL corresponding to the specified
12232 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12234 /* Look for the optional `::'. */
12235 cp_parser_global_scope_opt (parser,
12236 /*current_scope_valid_p=*/false);
12238 /* Look for the optional nested-name-specifier. */
12239 cp_parser_nested_name_specifier_opt (parser,
12240 /*typename_keyword_p=*/false,
12241 /*check_dependency_p=*/true,
12243 /*is_declaration=*/true);
12245 return cp_parser_namespace_name (parser);
12248 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12249 access declaration.
12252 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12253 using :: unqualified-id ;
12255 access-declaration:
12261 cp_parser_using_declaration (cp_parser* parser,
12262 bool access_declaration_p)
12265 bool typename_p = false;
12266 bool global_scope_p;
12271 if (access_declaration_p)
12272 cp_parser_parse_tentatively (parser);
12275 /* Look for the `using' keyword. */
12276 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12278 /* Peek at the next token. */
12279 token = cp_lexer_peek_token (parser->lexer);
12280 /* See if it's `typename'. */
12281 if (token->keyword == RID_TYPENAME)
12283 /* Remember that we've seen it. */
12285 /* Consume the `typename' token. */
12286 cp_lexer_consume_token (parser->lexer);
12290 /* Look for the optional global scope qualification. */
12292 = (cp_parser_global_scope_opt (parser,
12293 /*current_scope_valid_p=*/false)
12296 /* If we saw `typename', or didn't see `::', then there must be a
12297 nested-name-specifier present. */
12298 if (typename_p || !global_scope_p)
12299 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12300 /*check_dependency_p=*/true,
12302 /*is_declaration=*/true);
12303 /* Otherwise, we could be in either of the two productions. In that
12304 case, treat the nested-name-specifier as optional. */
12306 qscope = cp_parser_nested_name_specifier_opt (parser,
12307 /*typename_keyword_p=*/false,
12308 /*check_dependency_p=*/true,
12310 /*is_declaration=*/true);
12312 qscope = global_namespace;
12314 if (access_declaration_p && cp_parser_error_occurred (parser))
12315 /* Something has already gone wrong; there's no need to parse
12316 further. Since an error has occurred, the return value of
12317 cp_parser_parse_definitely will be false, as required. */
12318 return cp_parser_parse_definitely (parser);
12320 token = cp_lexer_peek_token (parser->lexer);
12321 /* Parse the unqualified-id. */
12322 identifier = cp_parser_unqualified_id (parser,
12323 /*template_keyword_p=*/false,
12324 /*check_dependency_p=*/true,
12325 /*declarator_p=*/true,
12326 /*optional_p=*/false);
12328 if (access_declaration_p)
12330 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12331 cp_parser_simulate_error (parser);
12332 if (!cp_parser_parse_definitely (parser))
12336 /* The function we call to handle a using-declaration is different
12337 depending on what scope we are in. */
12338 if (qscope == error_mark_node || identifier == error_mark_node)
12340 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12341 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12342 /* [namespace.udecl]
12344 A using declaration shall not name a template-id. */
12345 error ("%Ha template-id may not appear in a using-declaration",
12349 if (at_class_scope_p ())
12351 /* Create the USING_DECL. */
12352 decl = do_class_using_decl (parser->scope, identifier);
12354 if (check_for_bare_parameter_packs (decl))
12357 /* Add it to the list of members in this class. */
12358 finish_member_declaration (decl);
12362 decl = cp_parser_lookup_name_simple (parser,
12365 if (decl == error_mark_node)
12366 cp_parser_name_lookup_error (parser, identifier,
12369 else if (check_for_bare_parameter_packs (decl))
12371 else if (!at_namespace_scope_p ())
12372 do_local_using_decl (decl, qscope, identifier);
12374 do_toplevel_using_decl (decl, qscope, identifier);
12378 /* Look for the final `;'. */
12379 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12384 /* Parse a using-directive.
12387 using namespace :: [opt] nested-name-specifier [opt]
12388 namespace-name ; */
12391 cp_parser_using_directive (cp_parser* parser)
12393 tree namespace_decl;
12396 /* Look for the `using' keyword. */
12397 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12398 /* And the `namespace' keyword. */
12399 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12400 /* Look for the optional `::' operator. */
12401 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12402 /* And the optional nested-name-specifier. */
12403 cp_parser_nested_name_specifier_opt (parser,
12404 /*typename_keyword_p=*/false,
12405 /*check_dependency_p=*/true,
12407 /*is_declaration=*/true);
12408 /* Get the namespace being used. */
12409 namespace_decl = cp_parser_namespace_name (parser);
12410 /* And any specified attributes. */
12411 attribs = cp_parser_attributes_opt (parser);
12412 /* Update the symbol table. */
12413 parse_using_directive (namespace_decl, attribs);
12414 /* Look for the final `;'. */
12415 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12418 /* Parse an asm-definition.
12421 asm ( string-literal ) ;
12426 asm volatile [opt] ( string-literal ) ;
12427 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12428 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12429 : asm-operand-list [opt] ) ;
12430 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12431 : asm-operand-list [opt]
12432 : asm-operand-list [opt] ) ; */
12435 cp_parser_asm_definition (cp_parser* parser)
12438 tree outputs = NULL_TREE;
12439 tree inputs = NULL_TREE;
12440 tree clobbers = NULL_TREE;
12442 bool volatile_p = false;
12443 bool extended_p = false;
12444 bool invalid_inputs_p = false;
12445 bool invalid_outputs_p = false;
12447 /* Look for the `asm' keyword. */
12448 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12449 /* See if the next token is `volatile'. */
12450 if (cp_parser_allow_gnu_extensions_p (parser)
12451 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12453 /* Remember that we saw the `volatile' keyword. */
12455 /* Consume the token. */
12456 cp_lexer_consume_token (parser->lexer);
12458 /* Look for the opening `('. */
12459 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12461 /* Look for the string. */
12462 string = cp_parser_string_literal (parser, false, false);
12463 if (string == error_mark_node)
12465 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12466 /*consume_paren=*/true);
12470 /* If we're allowing GNU extensions, check for the extended assembly
12471 syntax. Unfortunately, the `:' tokens need not be separated by
12472 a space in C, and so, for compatibility, we tolerate that here
12473 too. Doing that means that we have to treat the `::' operator as
12475 if (cp_parser_allow_gnu_extensions_p (parser)
12476 && parser->in_function_body
12477 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12478 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12480 bool inputs_p = false;
12481 bool clobbers_p = false;
12483 /* The extended syntax was used. */
12486 /* Look for outputs. */
12487 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12489 /* Consume the `:'. */
12490 cp_lexer_consume_token (parser->lexer);
12491 /* Parse the output-operands. */
12492 if (cp_lexer_next_token_is_not (parser->lexer,
12494 && cp_lexer_next_token_is_not (parser->lexer,
12496 && cp_lexer_next_token_is_not (parser->lexer,
12498 outputs = cp_parser_asm_operand_list (parser);
12500 if (outputs == error_mark_node)
12501 invalid_outputs_p = true;
12503 /* If the next token is `::', there are no outputs, and the
12504 next token is the beginning of the inputs. */
12505 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12506 /* The inputs are coming next. */
12509 /* Look for inputs. */
12511 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12513 /* Consume the `:' or `::'. */
12514 cp_lexer_consume_token (parser->lexer);
12515 /* Parse the output-operands. */
12516 if (cp_lexer_next_token_is_not (parser->lexer,
12518 && cp_lexer_next_token_is_not (parser->lexer,
12520 inputs = cp_parser_asm_operand_list (parser);
12522 if (inputs == error_mark_node)
12523 invalid_inputs_p = true;
12525 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12526 /* The clobbers are coming next. */
12529 /* Look for clobbers. */
12531 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12533 /* Consume the `:' or `::'. */
12534 cp_lexer_consume_token (parser->lexer);
12535 /* Parse the clobbers. */
12536 if (cp_lexer_next_token_is_not (parser->lexer,
12538 clobbers = cp_parser_asm_clobber_list (parser);
12541 /* Look for the closing `)'. */
12542 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12543 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12544 /*consume_paren=*/true);
12545 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12547 if (!invalid_inputs_p && !invalid_outputs_p)
12549 /* Create the ASM_EXPR. */
12550 if (parser->in_function_body)
12552 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12554 /* If the extended syntax was not used, mark the ASM_EXPR. */
12557 tree temp = asm_stmt;
12558 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12559 temp = TREE_OPERAND (temp, 0);
12561 ASM_INPUT_P (temp) = 1;
12565 cgraph_add_asm_node (string);
12569 /* Declarators [gram.dcl.decl] */
12571 /* Parse an init-declarator.
12574 declarator initializer [opt]
12579 declarator asm-specification [opt] attributes [opt] initializer [opt]
12581 function-definition:
12582 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12584 decl-specifier-seq [opt] declarator function-try-block
12588 function-definition:
12589 __extension__ function-definition
12591 The DECL_SPECIFIERS apply to this declarator. Returns a
12592 representation of the entity declared. If MEMBER_P is TRUE, then
12593 this declarator appears in a class scope. The new DECL created by
12594 this declarator is returned.
12596 The CHECKS are access checks that should be performed once we know
12597 what entity is being declared (and, therefore, what classes have
12600 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12601 for a function-definition here as well. If the declarator is a
12602 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12603 be TRUE upon return. By that point, the function-definition will
12604 have been completely parsed.
12606 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12610 cp_parser_init_declarator (cp_parser* parser,
12611 cp_decl_specifier_seq *decl_specifiers,
12612 VEC (deferred_access_check,gc)* checks,
12613 bool function_definition_allowed_p,
12615 int declares_class_or_enum,
12616 bool* function_definition_p)
12618 cp_token *token = NULL, *asm_spec_start_token = NULL,
12619 *attributes_start_token = NULL;
12620 cp_declarator *declarator;
12621 tree prefix_attributes;
12623 tree asm_specification;
12625 tree decl = NULL_TREE;
12627 int is_initialized;
12628 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12629 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12631 enum cpp_ttype initialization_kind;
12632 bool is_direct_init = false;
12633 bool is_non_constant_init;
12634 int ctor_dtor_or_conv_p;
12636 tree pushed_scope = NULL;
12638 /* Gather the attributes that were provided with the
12639 decl-specifiers. */
12640 prefix_attributes = decl_specifiers->attributes;
12642 /* Assume that this is not the declarator for a function
12644 if (function_definition_p)
12645 *function_definition_p = false;
12647 /* Defer access checks while parsing the declarator; we cannot know
12648 what names are accessible until we know what is being
12650 resume_deferring_access_checks ();
12652 /* Parse the declarator. */
12653 token = cp_lexer_peek_token (parser->lexer);
12655 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12656 &ctor_dtor_or_conv_p,
12657 /*parenthesized_p=*/NULL,
12658 /*member_p=*/false);
12659 /* Gather up the deferred checks. */
12660 stop_deferring_access_checks ();
12662 /* If the DECLARATOR was erroneous, there's no need to go
12664 if (declarator == cp_error_declarator)
12665 return error_mark_node;
12667 /* Check that the number of template-parameter-lists is OK. */
12668 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12670 return error_mark_node;
12672 if (declares_class_or_enum & 2)
12673 cp_parser_check_for_definition_in_return_type (declarator,
12674 decl_specifiers->type,
12675 decl_specifiers->type_location);
12677 /* Figure out what scope the entity declared by the DECLARATOR is
12678 located in. `grokdeclarator' sometimes changes the scope, so
12679 we compute it now. */
12680 scope = get_scope_of_declarator (declarator);
12682 /* If we're allowing GNU extensions, look for an asm-specification
12684 if (cp_parser_allow_gnu_extensions_p (parser))
12686 /* Look for an asm-specification. */
12687 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12688 asm_specification = cp_parser_asm_specification_opt (parser);
12689 /* And attributes. */
12690 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12691 attributes = cp_parser_attributes_opt (parser);
12695 asm_specification = NULL_TREE;
12696 attributes = NULL_TREE;
12699 /* Peek at the next token. */
12700 token = cp_lexer_peek_token (parser->lexer);
12701 /* Check to see if the token indicates the start of a
12702 function-definition. */
12703 if (function_declarator_p (declarator)
12704 && cp_parser_token_starts_function_definition_p (token))
12706 if (!function_definition_allowed_p)
12708 /* If a function-definition should not appear here, issue an
12710 cp_parser_error (parser,
12711 "a function-definition is not allowed here");
12712 return error_mark_node;
12716 location_t func_brace_location
12717 = cp_lexer_peek_token (parser->lexer)->location;
12719 /* Neither attributes nor an asm-specification are allowed
12720 on a function-definition. */
12721 if (asm_specification)
12722 error ("%Han asm-specification is not allowed "
12723 "on a function-definition",
12724 &asm_spec_start_token->location);
12726 error ("%Hattributes are not allowed on a function-definition",
12727 &attributes_start_token->location);
12728 /* This is a function-definition. */
12729 *function_definition_p = true;
12731 /* Parse the function definition. */
12733 decl = cp_parser_save_member_function_body (parser,
12736 prefix_attributes);
12739 = (cp_parser_function_definition_from_specifiers_and_declarator
12740 (parser, decl_specifiers, prefix_attributes, declarator));
12742 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12744 /* This is where the prologue starts... */
12745 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12746 = func_brace_location;
12755 Only in function declarations for constructors, destructors, and
12756 type conversions can the decl-specifier-seq be omitted.
12758 We explicitly postpone this check past the point where we handle
12759 function-definitions because we tolerate function-definitions
12760 that are missing their return types in some modes. */
12761 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12763 cp_parser_error (parser,
12764 "expected constructor, destructor, or type conversion");
12765 return error_mark_node;
12768 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12769 if (token->type == CPP_EQ
12770 || token->type == CPP_OPEN_PAREN
12771 || token->type == CPP_OPEN_BRACE)
12773 is_initialized = SD_INITIALIZED;
12774 initialization_kind = token->type;
12776 if (token->type == CPP_EQ
12777 && function_declarator_p (declarator))
12779 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12780 if (t2->keyword == RID_DEFAULT)
12781 is_initialized = SD_DEFAULTED;
12782 else if (t2->keyword == RID_DELETE)
12783 is_initialized = SD_DELETED;
12788 /* If the init-declarator isn't initialized and isn't followed by a
12789 `,' or `;', it's not a valid init-declarator. */
12790 if (token->type != CPP_COMMA
12791 && token->type != CPP_SEMICOLON)
12793 cp_parser_error (parser, "expected initializer");
12794 return error_mark_node;
12796 is_initialized = SD_UNINITIALIZED;
12797 initialization_kind = CPP_EOF;
12800 /* Because start_decl has side-effects, we should only call it if we
12801 know we're going ahead. By this point, we know that we cannot
12802 possibly be looking at any other construct. */
12803 cp_parser_commit_to_tentative_parse (parser);
12805 /* If the decl specifiers were bad, issue an error now that we're
12806 sure this was intended to be a declarator. Then continue
12807 declaring the variable(s), as int, to try to cut down on further
12809 if (decl_specifiers->any_specifiers_p
12810 && decl_specifiers->type == error_mark_node)
12812 cp_parser_error (parser, "invalid type in declaration");
12813 decl_specifiers->type = integer_type_node;
12816 /* Check to see whether or not this declaration is a friend. */
12817 friend_p = cp_parser_friend_p (decl_specifiers);
12819 /* Enter the newly declared entry in the symbol table. If we're
12820 processing a declaration in a class-specifier, we wait until
12821 after processing the initializer. */
12824 if (parser->in_unbraced_linkage_specification_p)
12825 decl_specifiers->storage_class = sc_extern;
12826 decl = start_decl (declarator, decl_specifiers,
12827 is_initialized, attributes, prefix_attributes,
12831 /* Enter the SCOPE. That way unqualified names appearing in the
12832 initializer will be looked up in SCOPE. */
12833 pushed_scope = push_scope (scope);
12835 /* Perform deferred access control checks, now that we know in which
12836 SCOPE the declared entity resides. */
12837 if (!member_p && decl)
12839 tree saved_current_function_decl = NULL_TREE;
12841 /* If the entity being declared is a function, pretend that we
12842 are in its scope. If it is a `friend', it may have access to
12843 things that would not otherwise be accessible. */
12844 if (TREE_CODE (decl) == FUNCTION_DECL)
12846 saved_current_function_decl = current_function_decl;
12847 current_function_decl = decl;
12850 /* Perform access checks for template parameters. */
12851 cp_parser_perform_template_parameter_access_checks (checks);
12853 /* Perform the access control checks for the declarator and the
12854 decl-specifiers. */
12855 perform_deferred_access_checks ();
12857 /* Restore the saved value. */
12858 if (TREE_CODE (decl) == FUNCTION_DECL)
12859 current_function_decl = saved_current_function_decl;
12862 /* Parse the initializer. */
12863 initializer = NULL_TREE;
12864 is_direct_init = false;
12865 is_non_constant_init = true;
12866 if (is_initialized)
12868 if (function_declarator_p (declarator))
12870 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12871 if (initialization_kind == CPP_EQ)
12872 initializer = cp_parser_pure_specifier (parser);
12875 /* If the declaration was erroneous, we don't really
12876 know what the user intended, so just silently
12877 consume the initializer. */
12878 if (decl != error_mark_node)
12879 error ("%Hinitializer provided for function",
12880 &initializer_start_token->location);
12881 cp_parser_skip_to_closing_parenthesis (parser,
12882 /*recovering=*/true,
12883 /*or_comma=*/false,
12884 /*consume_paren=*/true);
12888 initializer = cp_parser_initializer (parser,
12890 &is_non_constant_init);
12893 /* The old parser allows attributes to appear after a parenthesized
12894 initializer. Mark Mitchell proposed removing this functionality
12895 on the GCC mailing lists on 2002-08-13. This parser accepts the
12896 attributes -- but ignores them. */
12897 if (cp_parser_allow_gnu_extensions_p (parser)
12898 && initialization_kind == CPP_OPEN_PAREN)
12899 if (cp_parser_attributes_opt (parser))
12900 warning (OPT_Wattributes,
12901 "attributes after parenthesized initializer ignored");
12903 /* For an in-class declaration, use `grokfield' to create the
12909 pop_scope (pushed_scope);
12910 pushed_scope = false;
12912 decl = grokfield (declarator, decl_specifiers,
12913 initializer, !is_non_constant_init,
12914 /*asmspec=*/NULL_TREE,
12915 prefix_attributes);
12916 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12917 cp_parser_save_default_args (parser, decl);
12920 /* Finish processing the declaration. But, skip friend
12922 if (!friend_p && decl && decl != error_mark_node)
12924 cp_finish_decl (decl,
12925 initializer, !is_non_constant_init,
12927 /* If the initializer is in parentheses, then this is
12928 a direct-initialization, which means that an
12929 `explicit' constructor is OK. Otherwise, an
12930 `explicit' constructor cannot be used. */
12931 ((is_direct_init || !is_initialized)
12932 ? 0 : LOOKUP_ONLYCONVERTING));
12934 else if ((cxx_dialect != cxx98) && friend_p
12935 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12936 /* Core issue #226 (C++0x only): A default template-argument
12937 shall not be specified in a friend class template
12939 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12940 /*is_partial=*/0, /*is_friend_decl=*/1);
12942 if (!friend_p && pushed_scope)
12943 pop_scope (pushed_scope);
12948 /* Parse a declarator.
12952 ptr-operator declarator
12954 abstract-declarator:
12955 ptr-operator abstract-declarator [opt]
12956 direct-abstract-declarator
12961 attributes [opt] direct-declarator
12962 attributes [opt] ptr-operator declarator
12964 abstract-declarator:
12965 attributes [opt] ptr-operator abstract-declarator [opt]
12966 attributes [opt] direct-abstract-declarator
12968 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12969 detect constructor, destructor or conversion operators. It is set
12970 to -1 if the declarator is a name, and +1 if it is a
12971 function. Otherwise it is set to zero. Usually you just want to
12972 test for >0, but internally the negative value is used.
12974 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12975 a decl-specifier-seq unless it declares a constructor, destructor,
12976 or conversion. It might seem that we could check this condition in
12977 semantic analysis, rather than parsing, but that makes it difficult
12978 to handle something like `f()'. We want to notice that there are
12979 no decl-specifiers, and therefore realize that this is an
12980 expression, not a declaration.)
12982 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12983 the declarator is a direct-declarator of the form "(...)".
12985 MEMBER_P is true iff this declarator is a member-declarator. */
12987 static cp_declarator *
12988 cp_parser_declarator (cp_parser* parser,
12989 cp_parser_declarator_kind dcl_kind,
12990 int* ctor_dtor_or_conv_p,
12991 bool* parenthesized_p,
12995 cp_declarator *declarator;
12996 enum tree_code code;
12997 cp_cv_quals cv_quals;
12999 tree attributes = NULL_TREE;
13001 /* Assume this is not a constructor, destructor, or type-conversion
13003 if (ctor_dtor_or_conv_p)
13004 *ctor_dtor_or_conv_p = 0;
13006 if (cp_parser_allow_gnu_extensions_p (parser))
13007 attributes = cp_parser_attributes_opt (parser);
13009 /* Peek at the next token. */
13010 token = cp_lexer_peek_token (parser->lexer);
13012 /* Check for the ptr-operator production. */
13013 cp_parser_parse_tentatively (parser);
13014 /* Parse the ptr-operator. */
13015 code = cp_parser_ptr_operator (parser,
13018 /* If that worked, then we have a ptr-operator. */
13019 if (cp_parser_parse_definitely (parser))
13021 /* If a ptr-operator was found, then this declarator was not
13023 if (parenthesized_p)
13024 *parenthesized_p = true;
13025 /* The dependent declarator is optional if we are parsing an
13026 abstract-declarator. */
13027 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13028 cp_parser_parse_tentatively (parser);
13030 /* Parse the dependent declarator. */
13031 declarator = cp_parser_declarator (parser, dcl_kind,
13032 /*ctor_dtor_or_conv_p=*/NULL,
13033 /*parenthesized_p=*/NULL,
13034 /*member_p=*/false);
13036 /* If we are parsing an abstract-declarator, we must handle the
13037 case where the dependent declarator is absent. */
13038 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13039 && !cp_parser_parse_definitely (parser))
13042 declarator = cp_parser_make_indirect_declarator
13043 (code, class_type, cv_quals, declarator);
13045 /* Everything else is a direct-declarator. */
13048 if (parenthesized_p)
13049 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13051 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13052 ctor_dtor_or_conv_p,
13056 if (attributes && declarator && declarator != cp_error_declarator)
13057 declarator->attributes = attributes;
13062 /* Parse a direct-declarator or direct-abstract-declarator.
13066 direct-declarator ( parameter-declaration-clause )
13067 cv-qualifier-seq [opt]
13068 exception-specification [opt]
13069 direct-declarator [ constant-expression [opt] ]
13072 direct-abstract-declarator:
13073 direct-abstract-declarator [opt]
13074 ( parameter-declaration-clause )
13075 cv-qualifier-seq [opt]
13076 exception-specification [opt]
13077 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13078 ( abstract-declarator )
13080 Returns a representation of the declarator. DCL_KIND is
13081 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13082 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13083 we are parsing a direct-declarator. It is
13084 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13085 of ambiguity we prefer an abstract declarator, as per
13086 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13087 cp_parser_declarator. */
13089 static cp_declarator *
13090 cp_parser_direct_declarator (cp_parser* parser,
13091 cp_parser_declarator_kind dcl_kind,
13092 int* ctor_dtor_or_conv_p,
13096 cp_declarator *declarator = NULL;
13097 tree scope = NULL_TREE;
13098 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13099 bool saved_in_declarator_p = parser->in_declarator_p;
13101 tree pushed_scope = NULL_TREE;
13105 /* Peek at the next token. */
13106 token = cp_lexer_peek_token (parser->lexer);
13107 if (token->type == CPP_OPEN_PAREN)
13109 /* This is either a parameter-declaration-clause, or a
13110 parenthesized declarator. When we know we are parsing a
13111 named declarator, it must be a parenthesized declarator
13112 if FIRST is true. For instance, `(int)' is a
13113 parameter-declaration-clause, with an omitted
13114 direct-abstract-declarator. But `((*))', is a
13115 parenthesized abstract declarator. Finally, when T is a
13116 template parameter `(T)' is a
13117 parameter-declaration-clause, and not a parenthesized
13120 We first try and parse a parameter-declaration-clause,
13121 and then try a nested declarator (if FIRST is true).
13123 It is not an error for it not to be a
13124 parameter-declaration-clause, even when FIRST is
13130 The first is the declaration of a function while the
13131 second is the definition of a variable, including its
13134 Having seen only the parenthesis, we cannot know which of
13135 these two alternatives should be selected. Even more
13136 complex are examples like:
13141 The former is a function-declaration; the latter is a
13142 variable initialization.
13144 Thus again, we try a parameter-declaration-clause, and if
13145 that fails, we back out and return. */
13147 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13150 unsigned saved_num_template_parameter_lists;
13151 bool is_declarator = false;
13154 /* In a member-declarator, the only valid interpretation
13155 of a parenthesis is the start of a
13156 parameter-declaration-clause. (It is invalid to
13157 initialize a static data member with a parenthesized
13158 initializer; only the "=" form of initialization is
13161 cp_parser_parse_tentatively (parser);
13163 /* Consume the `('. */
13164 cp_lexer_consume_token (parser->lexer);
13167 /* If this is going to be an abstract declarator, we're
13168 in a declarator and we can't have default args. */
13169 parser->default_arg_ok_p = false;
13170 parser->in_declarator_p = true;
13173 /* Inside the function parameter list, surrounding
13174 template-parameter-lists do not apply. */
13175 saved_num_template_parameter_lists
13176 = parser->num_template_parameter_lists;
13177 parser->num_template_parameter_lists = 0;
13179 begin_scope (sk_function_parms, NULL_TREE);
13181 /* Parse the parameter-declaration-clause. */
13182 params = cp_parser_parameter_declaration_clause (parser);
13184 parser->num_template_parameter_lists
13185 = saved_num_template_parameter_lists;
13187 /* If all went well, parse the cv-qualifier-seq and the
13188 exception-specification. */
13189 if (member_p || cp_parser_parse_definitely (parser))
13191 cp_cv_quals cv_quals;
13192 tree exception_specification;
13195 is_declarator = true;
13197 if (ctor_dtor_or_conv_p)
13198 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13200 /* Consume the `)'. */
13201 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13203 /* Parse the cv-qualifier-seq. */
13204 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13205 /* And the exception-specification. */
13206 exception_specification
13207 = cp_parser_exception_specification_opt (parser);
13210 = cp_parser_late_return_type_opt (parser);
13212 /* Create the function-declarator. */
13213 declarator = make_call_declarator (declarator,
13216 exception_specification,
13218 /* Any subsequent parameter lists are to do with
13219 return type, so are not those of the declared
13221 parser->default_arg_ok_p = false;
13224 /* Remove the function parms from scope. */
13225 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13226 pop_binding (DECL_NAME (t), t);
13230 /* Repeat the main loop. */
13234 /* If this is the first, we can try a parenthesized
13238 bool saved_in_type_id_in_expr_p;
13240 parser->default_arg_ok_p = saved_default_arg_ok_p;
13241 parser->in_declarator_p = saved_in_declarator_p;
13243 /* Consume the `('. */
13244 cp_lexer_consume_token (parser->lexer);
13245 /* Parse the nested declarator. */
13246 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13247 parser->in_type_id_in_expr_p = true;
13249 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13250 /*parenthesized_p=*/NULL,
13252 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13254 /* Expect a `)'. */
13255 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13256 declarator = cp_error_declarator;
13257 if (declarator == cp_error_declarator)
13260 goto handle_declarator;
13262 /* Otherwise, we must be done. */
13266 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13267 && token->type == CPP_OPEN_SQUARE)
13269 /* Parse an array-declarator. */
13272 if (ctor_dtor_or_conv_p)
13273 *ctor_dtor_or_conv_p = 0;
13276 parser->default_arg_ok_p = false;
13277 parser->in_declarator_p = true;
13278 /* Consume the `['. */
13279 cp_lexer_consume_token (parser->lexer);
13280 /* Peek at the next token. */
13281 token = cp_lexer_peek_token (parser->lexer);
13282 /* If the next token is `]', then there is no
13283 constant-expression. */
13284 if (token->type != CPP_CLOSE_SQUARE)
13286 bool non_constant_p;
13289 = cp_parser_constant_expression (parser,
13290 /*allow_non_constant=*/true,
13292 if (!non_constant_p)
13293 bounds = fold_non_dependent_expr (bounds);
13294 else if (processing_template_decl)
13296 /* Remember this wasn't a constant-expression. */
13297 bounds = build_nop (TREE_TYPE (bounds), bounds);
13298 TREE_SIDE_EFFECTS (bounds) = 1;
13301 /* Normally, the array bound must be an integral constant
13302 expression. However, as an extension, we allow VLAs
13303 in function scopes. */
13304 else if (!parser->in_function_body)
13306 error ("%Harray bound is not an integer constant",
13308 bounds = error_mark_node;
13312 bounds = NULL_TREE;
13313 /* Look for the closing `]'. */
13314 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13316 declarator = cp_error_declarator;
13320 declarator = make_array_declarator (declarator, bounds);
13322 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13324 tree qualifying_scope;
13325 tree unqualified_name;
13326 special_function_kind sfk;
13328 bool pack_expansion_p = false;
13329 cp_token *declarator_id_start_token;
13331 /* Parse a declarator-id */
13332 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13335 cp_parser_parse_tentatively (parser);
13337 /* If we see an ellipsis, we should be looking at a
13339 if (token->type == CPP_ELLIPSIS)
13341 /* Consume the `...' */
13342 cp_lexer_consume_token (parser->lexer);
13344 pack_expansion_p = true;
13348 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13350 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13351 qualifying_scope = parser->scope;
13356 if (!unqualified_name && pack_expansion_p)
13358 /* Check whether an error occurred. */
13359 okay = !cp_parser_error_occurred (parser);
13361 /* We already consumed the ellipsis to mark a
13362 parameter pack, but we have no way to report it,
13363 so abort the tentative parse. We will be exiting
13364 immediately anyway. */
13365 cp_parser_abort_tentative_parse (parser);
13368 okay = cp_parser_parse_definitely (parser);
13371 unqualified_name = error_mark_node;
13372 else if (unqualified_name
13373 && (qualifying_scope
13374 || (TREE_CODE (unqualified_name)
13375 != IDENTIFIER_NODE)))
13377 cp_parser_error (parser, "expected unqualified-id");
13378 unqualified_name = error_mark_node;
13382 if (!unqualified_name)
13384 if (unqualified_name == error_mark_node)
13386 declarator = cp_error_declarator;
13387 pack_expansion_p = false;
13388 declarator->parameter_pack_p = false;
13392 if (qualifying_scope && at_namespace_scope_p ()
13393 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13395 /* In the declaration of a member of a template class
13396 outside of the class itself, the SCOPE will sometimes
13397 be a TYPENAME_TYPE. For example, given:
13399 template <typename T>
13400 int S<T>::R::i = 3;
13402 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13403 this context, we must resolve S<T>::R to an ordinary
13404 type, rather than a typename type.
13406 The reason we normally avoid resolving TYPENAME_TYPEs
13407 is that a specialization of `S' might render
13408 `S<T>::R' not a type. However, if `S' is
13409 specialized, then this `i' will not be used, so there
13410 is no harm in resolving the types here. */
13413 /* Resolve the TYPENAME_TYPE. */
13414 type = resolve_typename_type (qualifying_scope,
13415 /*only_current_p=*/false);
13416 /* If that failed, the declarator is invalid. */
13417 if (TREE_CODE (type) == TYPENAME_TYPE)
13418 error ("%H%<%T::%E%> is not a type",
13419 &declarator_id_start_token->location,
13420 TYPE_CONTEXT (qualifying_scope),
13421 TYPE_IDENTIFIER (qualifying_scope));
13422 qualifying_scope = type;
13427 if (unqualified_name)
13431 if (qualifying_scope
13432 && CLASS_TYPE_P (qualifying_scope))
13433 class_type = qualifying_scope;
13435 class_type = current_class_type;
13437 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13439 tree name_type = TREE_TYPE (unqualified_name);
13440 if (class_type && same_type_p (name_type, class_type))
13442 if (qualifying_scope
13443 && CLASSTYPE_USE_TEMPLATE (name_type))
13445 error ("%Hinvalid use of constructor as a template",
13446 &declarator_id_start_token->location);
13447 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13448 "name the constructor in a qualified name",
13450 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13451 class_type, name_type);
13452 declarator = cp_error_declarator;
13456 unqualified_name = constructor_name (class_type);
13460 /* We do not attempt to print the declarator
13461 here because we do not have enough
13462 information about its original syntactic
13464 cp_parser_error (parser, "invalid declarator");
13465 declarator = cp_error_declarator;
13472 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13473 sfk = sfk_destructor;
13474 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13475 sfk = sfk_conversion;
13476 else if (/* There's no way to declare a constructor
13477 for an anonymous type, even if the type
13478 got a name for linkage purposes. */
13479 !TYPE_WAS_ANONYMOUS (class_type)
13480 && constructor_name_p (unqualified_name,
13483 unqualified_name = constructor_name (class_type);
13484 sfk = sfk_constructor;
13487 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13488 *ctor_dtor_or_conv_p = -1;
13491 declarator = make_id_declarator (qualifying_scope,
13494 declarator->id_loc = token->location;
13495 declarator->parameter_pack_p = pack_expansion_p;
13497 if (pack_expansion_p)
13498 maybe_warn_variadic_templates ();
13500 handle_declarator:;
13501 scope = get_scope_of_declarator (declarator);
13503 /* Any names that appear after the declarator-id for a
13504 member are looked up in the containing scope. */
13505 pushed_scope = push_scope (scope);
13506 parser->in_declarator_p = true;
13507 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13508 || (declarator && declarator->kind == cdk_id))
13509 /* Default args are only allowed on function
13511 parser->default_arg_ok_p = saved_default_arg_ok_p;
13513 parser->default_arg_ok_p = false;
13522 /* For an abstract declarator, we might wind up with nothing at this
13523 point. That's an error; the declarator is not optional. */
13525 cp_parser_error (parser, "expected declarator");
13527 /* If we entered a scope, we must exit it now. */
13529 pop_scope (pushed_scope);
13531 parser->default_arg_ok_p = saved_default_arg_ok_p;
13532 parser->in_declarator_p = saved_in_declarator_p;
13537 /* Parse a ptr-operator.
13540 * cv-qualifier-seq [opt]
13542 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13547 & cv-qualifier-seq [opt]
13549 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13550 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13551 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13552 filled in with the TYPE containing the member. *CV_QUALS is
13553 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13554 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13555 Note that the tree codes returned by this function have nothing
13556 to do with the types of trees that will be eventually be created
13557 to represent the pointer or reference type being parsed. They are
13558 just constants with suggestive names. */
13559 static enum tree_code
13560 cp_parser_ptr_operator (cp_parser* parser,
13562 cp_cv_quals *cv_quals)
13564 enum tree_code code = ERROR_MARK;
13567 /* Assume that it's not a pointer-to-member. */
13569 /* And that there are no cv-qualifiers. */
13570 *cv_quals = TYPE_UNQUALIFIED;
13572 /* Peek at the next token. */
13573 token = cp_lexer_peek_token (parser->lexer);
13575 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13576 if (token->type == CPP_MULT)
13577 code = INDIRECT_REF;
13578 else if (token->type == CPP_AND)
13580 else if ((cxx_dialect != cxx98) &&
13581 token->type == CPP_AND_AND) /* C++0x only */
13582 code = NON_LVALUE_EXPR;
13584 if (code != ERROR_MARK)
13586 /* Consume the `*', `&' or `&&'. */
13587 cp_lexer_consume_token (parser->lexer);
13589 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13590 `&', if we are allowing GNU extensions. (The only qualifier
13591 that can legally appear after `&' is `restrict', but that is
13592 enforced during semantic analysis. */
13593 if (code == INDIRECT_REF
13594 || cp_parser_allow_gnu_extensions_p (parser))
13595 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13599 /* Try the pointer-to-member case. */
13600 cp_parser_parse_tentatively (parser);
13601 /* Look for the optional `::' operator. */
13602 cp_parser_global_scope_opt (parser,
13603 /*current_scope_valid_p=*/false);
13604 /* Look for the nested-name specifier. */
13605 token = cp_lexer_peek_token (parser->lexer);
13606 cp_parser_nested_name_specifier (parser,
13607 /*typename_keyword_p=*/false,
13608 /*check_dependency_p=*/true,
13610 /*is_declaration=*/false);
13611 /* If we found it, and the next token is a `*', then we are
13612 indeed looking at a pointer-to-member operator. */
13613 if (!cp_parser_error_occurred (parser)
13614 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13616 /* Indicate that the `*' operator was used. */
13617 code = INDIRECT_REF;
13619 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13620 error ("%H%qD is a namespace", &token->location, parser->scope);
13623 /* The type of which the member is a member is given by the
13625 *type = parser->scope;
13626 /* The next name will not be qualified. */
13627 parser->scope = NULL_TREE;
13628 parser->qualifying_scope = NULL_TREE;
13629 parser->object_scope = NULL_TREE;
13630 /* Look for the optional cv-qualifier-seq. */
13631 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13634 /* If that didn't work we don't have a ptr-operator. */
13635 if (!cp_parser_parse_definitely (parser))
13636 cp_parser_error (parser, "expected ptr-operator");
13642 /* Parse an (optional) cv-qualifier-seq.
13645 cv-qualifier cv-qualifier-seq [opt]
13656 Returns a bitmask representing the cv-qualifiers. */
13659 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13661 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13666 cp_cv_quals cv_qualifier;
13668 /* Peek at the next token. */
13669 token = cp_lexer_peek_token (parser->lexer);
13670 /* See if it's a cv-qualifier. */
13671 switch (token->keyword)
13674 cv_qualifier = TYPE_QUAL_CONST;
13678 cv_qualifier = TYPE_QUAL_VOLATILE;
13682 cv_qualifier = TYPE_QUAL_RESTRICT;
13686 cv_qualifier = TYPE_UNQUALIFIED;
13693 if (cv_quals & cv_qualifier)
13695 error ("%Hduplicate cv-qualifier", &token->location);
13696 cp_lexer_purge_token (parser->lexer);
13700 cp_lexer_consume_token (parser->lexer);
13701 cv_quals |= cv_qualifier;
13708 /* Parse a late-specified return type, if any. This is not a separate
13709 non-terminal, but part of a function declarator, which looks like
13713 Returns the type indicated by the type-id. */
13716 cp_parser_late_return_type_opt (cp_parser* parser)
13720 /* Peek at the next token. */
13721 token = cp_lexer_peek_token (parser->lexer);
13722 /* A late-specified return type is indicated by an initial '->'. */
13723 if (token->type != CPP_DEREF)
13726 /* Consume the ->. */
13727 cp_lexer_consume_token (parser->lexer);
13729 return cp_parser_type_id (parser);
13732 /* Parse a declarator-id.
13736 :: [opt] nested-name-specifier [opt] type-name
13738 In the `id-expression' case, the value returned is as for
13739 cp_parser_id_expression if the id-expression was an unqualified-id.
13740 If the id-expression was a qualified-id, then a SCOPE_REF is
13741 returned. The first operand is the scope (either a NAMESPACE_DECL
13742 or TREE_TYPE), but the second is still just a representation of an
13746 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13749 /* The expression must be an id-expression. Assume that qualified
13750 names are the names of types so that:
13753 int S<T>::R::i = 3;
13755 will work; we must treat `S<T>::R' as the name of a type.
13756 Similarly, assume that qualified names are templates, where
13760 int S<T>::R<T>::i = 3;
13763 id = cp_parser_id_expression (parser,
13764 /*template_keyword_p=*/false,
13765 /*check_dependency_p=*/false,
13766 /*template_p=*/NULL,
13767 /*declarator_p=*/true,
13769 if (id && BASELINK_P (id))
13770 id = BASELINK_FUNCTIONS (id);
13774 /* Parse a type-id.
13777 type-specifier-seq abstract-declarator [opt]
13779 Returns the TYPE specified. */
13782 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13784 cp_decl_specifier_seq type_specifier_seq;
13785 cp_declarator *abstract_declarator;
13787 /* Parse the type-specifier-seq. */
13788 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13789 &type_specifier_seq);
13790 if (type_specifier_seq.type == error_mark_node)
13791 return error_mark_node;
13793 /* There might or might not be an abstract declarator. */
13794 cp_parser_parse_tentatively (parser);
13795 /* Look for the declarator. */
13796 abstract_declarator
13797 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13798 /*parenthesized_p=*/NULL,
13799 /*member_p=*/false);
13800 /* Check to see if there really was a declarator. */
13801 if (!cp_parser_parse_definitely (parser))
13802 abstract_declarator = NULL;
13804 if (type_specifier_seq.type
13805 && type_uses_auto (type_specifier_seq.type))
13807 error ("invalid use of %<auto%>");
13808 return error_mark_node;
13811 return groktypename (&type_specifier_seq, abstract_declarator,
13815 static tree cp_parser_type_id (cp_parser *parser)
13817 return cp_parser_type_id_1 (parser, false);
13820 static tree cp_parser_template_type_arg (cp_parser *parser)
13822 return cp_parser_type_id_1 (parser, true);
13825 /* Parse a type-specifier-seq.
13827 type-specifier-seq:
13828 type-specifier type-specifier-seq [opt]
13832 type-specifier-seq:
13833 attributes type-specifier-seq [opt]
13835 If IS_CONDITION is true, we are at the start of a "condition",
13836 e.g., we've just seen "if (".
13838 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13841 cp_parser_type_specifier_seq (cp_parser* parser,
13843 cp_decl_specifier_seq *type_specifier_seq)
13845 bool seen_type_specifier = false;
13846 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13847 cp_token *start_token = NULL;
13849 /* Clear the TYPE_SPECIFIER_SEQ. */
13850 clear_decl_specs (type_specifier_seq);
13852 /* Parse the type-specifiers and attributes. */
13855 tree type_specifier;
13856 bool is_cv_qualifier;
13858 /* Check for attributes first. */
13859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13861 type_specifier_seq->attributes =
13862 chainon (type_specifier_seq->attributes,
13863 cp_parser_attributes_opt (parser));
13867 /* record the token of the beginning of the type specifier seq,
13868 for error reporting purposes*/
13870 start_token = cp_lexer_peek_token (parser->lexer);
13872 /* Look for the type-specifier. */
13873 type_specifier = cp_parser_type_specifier (parser,
13875 type_specifier_seq,
13876 /*is_declaration=*/false,
13879 if (!type_specifier)
13881 /* If the first type-specifier could not be found, this is not a
13882 type-specifier-seq at all. */
13883 if (!seen_type_specifier)
13885 cp_parser_error (parser, "expected type-specifier");
13886 type_specifier_seq->type = error_mark_node;
13889 /* If subsequent type-specifiers could not be found, the
13890 type-specifier-seq is complete. */
13894 seen_type_specifier = true;
13895 /* The standard says that a condition can be:
13897 type-specifier-seq declarator = assignment-expression
13904 we should treat the "S" as a declarator, not as a
13905 type-specifier. The standard doesn't say that explicitly for
13906 type-specifier-seq, but it does say that for
13907 decl-specifier-seq in an ordinary declaration. Perhaps it
13908 would be clearer just to allow a decl-specifier-seq here, and
13909 then add a semantic restriction that if any decl-specifiers
13910 that are not type-specifiers appear, the program is invalid. */
13911 if (is_condition && !is_cv_qualifier)
13912 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13915 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13918 /* Parse a parameter-declaration-clause.
13920 parameter-declaration-clause:
13921 parameter-declaration-list [opt] ... [opt]
13922 parameter-declaration-list , ...
13924 Returns a representation for the parameter declarations. A return
13925 value of NULL indicates a parameter-declaration-clause consisting
13926 only of an ellipsis. */
13929 cp_parser_parameter_declaration_clause (cp_parser* parser)
13936 /* Peek at the next token. */
13937 token = cp_lexer_peek_token (parser->lexer);
13938 /* Check for trivial parameter-declaration-clauses. */
13939 if (token->type == CPP_ELLIPSIS)
13941 /* Consume the `...' token. */
13942 cp_lexer_consume_token (parser->lexer);
13945 else if (token->type == CPP_CLOSE_PAREN)
13946 /* There are no parameters. */
13948 #ifndef NO_IMPLICIT_EXTERN_C
13949 if (in_system_header && current_class_type == NULL
13950 && current_lang_name == lang_name_c)
13954 return void_list_node;
13956 /* Check for `(void)', too, which is a special case. */
13957 else if (token->keyword == RID_VOID
13958 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13959 == CPP_CLOSE_PAREN))
13961 /* Consume the `void' token. */
13962 cp_lexer_consume_token (parser->lexer);
13963 /* There are no parameters. */
13964 return void_list_node;
13967 /* Parse the parameter-declaration-list. */
13968 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13969 /* If a parse error occurred while parsing the
13970 parameter-declaration-list, then the entire
13971 parameter-declaration-clause is erroneous. */
13975 /* Peek at the next token. */
13976 token = cp_lexer_peek_token (parser->lexer);
13977 /* If it's a `,', the clause should terminate with an ellipsis. */
13978 if (token->type == CPP_COMMA)
13980 /* Consume the `,'. */
13981 cp_lexer_consume_token (parser->lexer);
13982 /* Expect an ellipsis. */
13984 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13986 /* It might also be `...' if the optional trailing `,' was
13988 else if (token->type == CPP_ELLIPSIS)
13990 /* Consume the `...' token. */
13991 cp_lexer_consume_token (parser->lexer);
13992 /* And remember that we saw it. */
13996 ellipsis_p = false;
13998 /* Finish the parameter list. */
14000 parameters = chainon (parameters, void_list_node);
14005 /* Parse a parameter-declaration-list.
14007 parameter-declaration-list:
14008 parameter-declaration
14009 parameter-declaration-list , parameter-declaration
14011 Returns a representation of the parameter-declaration-list, as for
14012 cp_parser_parameter_declaration_clause. However, the
14013 `void_list_node' is never appended to the list. Upon return,
14014 *IS_ERROR will be true iff an error occurred. */
14017 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14019 tree parameters = NULL_TREE;
14020 tree *tail = ¶meters;
14021 bool saved_in_unbraced_linkage_specification_p;
14023 /* Assume all will go well. */
14025 /* The special considerations that apply to a function within an
14026 unbraced linkage specifications do not apply to the parameters
14027 to the function. */
14028 saved_in_unbraced_linkage_specification_p
14029 = parser->in_unbraced_linkage_specification_p;
14030 parser->in_unbraced_linkage_specification_p = false;
14032 /* Look for more parameters. */
14035 cp_parameter_declarator *parameter;
14036 tree decl = error_mark_node;
14037 bool parenthesized_p;
14038 /* Parse the parameter. */
14040 = cp_parser_parameter_declaration (parser,
14041 /*template_parm_p=*/false,
14044 /* We don't know yet if the enclosing context is deprecated, so wait
14045 and warn in grokparms if appropriate. */
14046 deprecated_state = DEPRECATED_SUPPRESS;
14049 decl = grokdeclarator (parameter->declarator,
14050 ¶meter->decl_specifiers,
14052 parameter->default_argument != NULL_TREE,
14053 ¶meter->decl_specifiers.attributes);
14055 deprecated_state = DEPRECATED_NORMAL;
14057 /* If a parse error occurred parsing the parameter declaration,
14058 then the entire parameter-declaration-list is erroneous. */
14059 if (decl == error_mark_node)
14062 parameters = error_mark_node;
14066 if (parameter->decl_specifiers.attributes)
14067 cplus_decl_attributes (&decl,
14068 parameter->decl_specifiers.attributes,
14070 if (DECL_NAME (decl))
14071 decl = pushdecl (decl);
14073 /* Add the new parameter to the list. */
14074 *tail = build_tree_list (parameter->default_argument, decl);
14075 tail = &TREE_CHAIN (*tail);
14077 /* Peek at the next token. */
14078 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14079 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14080 /* These are for Objective-C++ */
14081 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14082 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14083 /* The parameter-declaration-list is complete. */
14085 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14089 /* Peek at the next token. */
14090 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14091 /* If it's an ellipsis, then the list is complete. */
14092 if (token->type == CPP_ELLIPSIS)
14094 /* Otherwise, there must be more parameters. Consume the
14096 cp_lexer_consume_token (parser->lexer);
14097 /* When parsing something like:
14099 int i(float f, double d)
14101 we can tell after seeing the declaration for "f" that we
14102 are not looking at an initialization of a variable "i",
14103 but rather at the declaration of a function "i".
14105 Due to the fact that the parsing of template arguments
14106 (as specified to a template-id) requires backtracking we
14107 cannot use this technique when inside a template argument
14109 if (!parser->in_template_argument_list_p
14110 && !parser->in_type_id_in_expr_p
14111 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14112 /* However, a parameter-declaration of the form
14113 "foat(f)" (which is a valid declaration of a
14114 parameter "f") can also be interpreted as an
14115 expression (the conversion of "f" to "float"). */
14116 && !parenthesized_p)
14117 cp_parser_commit_to_tentative_parse (parser);
14121 cp_parser_error (parser, "expected %<,%> or %<...%>");
14122 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14123 cp_parser_skip_to_closing_parenthesis (parser,
14124 /*recovering=*/true,
14125 /*or_comma=*/false,
14126 /*consume_paren=*/false);
14131 parser->in_unbraced_linkage_specification_p
14132 = saved_in_unbraced_linkage_specification_p;
14137 /* Parse a parameter declaration.
14139 parameter-declaration:
14140 decl-specifier-seq ... [opt] declarator
14141 decl-specifier-seq declarator = assignment-expression
14142 decl-specifier-seq ... [opt] abstract-declarator [opt]
14143 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14145 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14146 declares a template parameter. (In that case, a non-nested `>'
14147 token encountered during the parsing of the assignment-expression
14148 is not interpreted as a greater-than operator.)
14150 Returns a representation of the parameter, or NULL if an error
14151 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14152 true iff the declarator is of the form "(p)". */
14154 static cp_parameter_declarator *
14155 cp_parser_parameter_declaration (cp_parser *parser,
14156 bool template_parm_p,
14157 bool *parenthesized_p)
14159 int declares_class_or_enum;
14160 bool greater_than_is_operator_p;
14161 cp_decl_specifier_seq decl_specifiers;
14162 cp_declarator *declarator;
14163 tree default_argument;
14164 cp_token *token = NULL, *declarator_token_start = NULL;
14165 const char *saved_message;
14167 /* In a template parameter, `>' is not an operator.
14171 When parsing a default template-argument for a non-type
14172 template-parameter, the first non-nested `>' is taken as the end
14173 of the template parameter-list rather than a greater-than
14175 greater_than_is_operator_p = !template_parm_p;
14177 /* Type definitions may not appear in parameter types. */
14178 saved_message = parser->type_definition_forbidden_message;
14179 parser->type_definition_forbidden_message
14180 = "types may not be defined in parameter types";
14182 /* Parse the declaration-specifiers. */
14183 cp_parser_decl_specifier_seq (parser,
14184 CP_PARSER_FLAGS_NONE,
14186 &declares_class_or_enum);
14187 /* If an error occurred, there's no reason to attempt to parse the
14188 rest of the declaration. */
14189 if (cp_parser_error_occurred (parser))
14191 parser->type_definition_forbidden_message = saved_message;
14195 /* Peek at the next token. */
14196 token = cp_lexer_peek_token (parser->lexer);
14198 /* If the next token is a `)', `,', `=', `>', or `...', then there
14199 is no declarator. However, when variadic templates are enabled,
14200 there may be a declarator following `...'. */
14201 if (token->type == CPP_CLOSE_PAREN
14202 || token->type == CPP_COMMA
14203 || token->type == CPP_EQ
14204 || token->type == CPP_GREATER)
14207 if (parenthesized_p)
14208 *parenthesized_p = false;
14210 /* Otherwise, there should be a declarator. */
14213 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14214 parser->default_arg_ok_p = false;
14216 /* After seeing a decl-specifier-seq, if the next token is not a
14217 "(", there is no possibility that the code is a valid
14218 expression. Therefore, if parsing tentatively, we commit at
14220 if (!parser->in_template_argument_list_p
14221 /* In an expression context, having seen:
14225 we cannot be sure whether we are looking at a
14226 function-type (taking a "char" as a parameter) or a cast
14227 of some object of type "char" to "int". */
14228 && !parser->in_type_id_in_expr_p
14229 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14230 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14231 cp_parser_commit_to_tentative_parse (parser);
14232 /* Parse the declarator. */
14233 declarator_token_start = token;
14234 declarator = cp_parser_declarator (parser,
14235 CP_PARSER_DECLARATOR_EITHER,
14236 /*ctor_dtor_or_conv_p=*/NULL,
14238 /*member_p=*/false);
14239 parser->default_arg_ok_p = saved_default_arg_ok_p;
14240 /* After the declarator, allow more attributes. */
14241 decl_specifiers.attributes
14242 = chainon (decl_specifiers.attributes,
14243 cp_parser_attributes_opt (parser));
14246 /* If the next token is an ellipsis, and we have not seen a
14247 declarator name, and the type of the declarator contains parameter
14248 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14249 a parameter pack expansion expression. Otherwise, leave the
14250 ellipsis for a C-style variadic function. */
14251 token = cp_lexer_peek_token (parser->lexer);
14252 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14254 tree type = decl_specifiers.type;
14256 if (type && DECL_P (type))
14257 type = TREE_TYPE (type);
14260 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14261 && declarator_can_be_parameter_pack (declarator)
14262 && (!declarator || !declarator->parameter_pack_p)
14263 && uses_parameter_packs (type))
14265 /* Consume the `...'. */
14266 cp_lexer_consume_token (parser->lexer);
14267 maybe_warn_variadic_templates ();
14269 /* Build a pack expansion type */
14271 declarator->parameter_pack_p = true;
14273 decl_specifiers.type = make_pack_expansion (type);
14277 /* The restriction on defining new types applies only to the type
14278 of the parameter, not to the default argument. */
14279 parser->type_definition_forbidden_message = saved_message;
14281 /* If the next token is `=', then process a default argument. */
14282 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14284 /* Consume the `='. */
14285 cp_lexer_consume_token (parser->lexer);
14287 /* If we are defining a class, then the tokens that make up the
14288 default argument must be saved and processed later. */
14289 if (!template_parm_p && at_class_scope_p ()
14290 && TYPE_BEING_DEFINED (current_class_type))
14292 unsigned depth = 0;
14293 int maybe_template_id = 0;
14294 cp_token *first_token;
14297 /* Add tokens until we have processed the entire default
14298 argument. We add the range [first_token, token). */
14299 first_token = cp_lexer_peek_token (parser->lexer);
14304 /* Peek at the next token. */
14305 token = cp_lexer_peek_token (parser->lexer);
14306 /* What we do depends on what token we have. */
14307 switch (token->type)
14309 /* In valid code, a default argument must be
14310 immediately followed by a `,' `)', or `...'. */
14312 if (depth == 0 && maybe_template_id)
14314 /* If we've seen a '<', we might be in a
14315 template-argument-list. Until Core issue 325 is
14316 resolved, we don't know how this situation ought
14317 to be handled, so try to DTRT. We check whether
14318 what comes after the comma is a valid parameter
14319 declaration list. If it is, then the comma ends
14320 the default argument; otherwise the default
14321 argument continues. */
14322 bool error = false;
14324 /* Set ITALP so cp_parser_parameter_declaration_list
14325 doesn't decide to commit to this parse. */
14326 bool saved_italp = parser->in_template_argument_list_p;
14327 parser->in_template_argument_list_p = true;
14329 cp_parser_parse_tentatively (parser);
14330 cp_lexer_consume_token (parser->lexer);
14331 cp_parser_parameter_declaration_list (parser, &error);
14332 if (!cp_parser_error_occurred (parser) && !error)
14334 cp_parser_abort_tentative_parse (parser);
14336 parser->in_template_argument_list_p = saved_italp;
14339 case CPP_CLOSE_PAREN:
14341 /* If we run into a non-nested `;', `}', or `]',
14342 then the code is invalid -- but the default
14343 argument is certainly over. */
14344 case CPP_SEMICOLON:
14345 case CPP_CLOSE_BRACE:
14346 case CPP_CLOSE_SQUARE:
14349 /* Update DEPTH, if necessary. */
14350 else if (token->type == CPP_CLOSE_PAREN
14351 || token->type == CPP_CLOSE_BRACE
14352 || token->type == CPP_CLOSE_SQUARE)
14356 case CPP_OPEN_PAREN:
14357 case CPP_OPEN_SQUARE:
14358 case CPP_OPEN_BRACE:
14364 /* This might be the comparison operator, or it might
14365 start a template argument list. */
14366 ++maybe_template_id;
14370 if (cxx_dialect == cxx98)
14372 /* Fall through for C++0x, which treats the `>>'
14373 operator like two `>' tokens in certain
14379 /* This might be an operator, or it might close a
14380 template argument list. But if a previous '<'
14381 started a template argument list, this will have
14382 closed it, so we can't be in one anymore. */
14383 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14384 if (maybe_template_id < 0)
14385 maybe_template_id = 0;
14389 /* If we run out of tokens, issue an error message. */
14391 case CPP_PRAGMA_EOL:
14392 error ("%Hfile ends in default argument", &token->location);
14398 /* In these cases, we should look for template-ids.
14399 For example, if the default argument is
14400 `X<int, double>()', we need to do name lookup to
14401 figure out whether or not `X' is a template; if
14402 so, the `,' does not end the default argument.
14404 That is not yet done. */
14411 /* If we've reached the end, stop. */
14415 /* Add the token to the token block. */
14416 token = cp_lexer_consume_token (parser->lexer);
14419 /* Create a DEFAULT_ARG to represent the unparsed default
14421 default_argument = make_node (DEFAULT_ARG);
14422 DEFARG_TOKENS (default_argument)
14423 = cp_token_cache_new (first_token, token);
14424 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14426 /* Outside of a class definition, we can just parse the
14427 assignment-expression. */
14430 token = cp_lexer_peek_token (parser->lexer);
14432 = cp_parser_default_argument (parser, template_parm_p);
14435 if (!parser->default_arg_ok_p)
14437 if (flag_permissive)
14438 warning (0, "deprecated use of default argument for parameter of non-function");
14441 error ("%Hdefault arguments are only "
14442 "permitted for function parameters",
14444 default_argument = NULL_TREE;
14447 else if ((declarator && declarator->parameter_pack_p)
14448 || (decl_specifiers.type
14449 && PACK_EXPANSION_P (decl_specifiers.type)))
14451 const char* kind = template_parm_p? "template " : "";
14453 /* Find the name of the parameter pack. */
14454 cp_declarator *id_declarator = declarator;
14455 while (id_declarator && id_declarator->kind != cdk_id)
14456 id_declarator = id_declarator->declarator;
14458 if (id_declarator && id_declarator->kind == cdk_id)
14459 error ("%H%sparameter pack %qD cannot have a default argument",
14460 &declarator_token_start->location,
14461 kind, id_declarator->u.id.unqualified_name);
14463 error ("%H%sparameter pack cannot have a default argument",
14464 &declarator_token_start->location, kind);
14466 default_argument = NULL_TREE;
14470 default_argument = NULL_TREE;
14472 return make_parameter_declarator (&decl_specifiers,
14477 /* Parse a default argument and return it.
14479 TEMPLATE_PARM_P is true if this is a default argument for a
14480 non-type template parameter. */
14482 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14484 tree default_argument = NULL_TREE;
14485 bool saved_greater_than_is_operator_p;
14486 bool saved_local_variables_forbidden_p;
14488 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14490 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14491 parser->greater_than_is_operator_p = !template_parm_p;
14492 /* Local variable names (and the `this' keyword) may not
14493 appear in a default argument. */
14494 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14495 parser->local_variables_forbidden_p = true;
14496 /* The default argument expression may cause implicitly
14497 defined member functions to be synthesized, which will
14498 result in garbage collection. We must treat this
14499 situation as if we were within the body of function so as
14500 to avoid collecting live data on the stack. */
14502 /* Parse the assignment-expression. */
14503 if (template_parm_p)
14504 push_deferring_access_checks (dk_no_deferred);
14506 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14507 if (template_parm_p)
14508 pop_deferring_access_checks ();
14509 /* Restore saved state. */
14511 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14512 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14514 return default_argument;
14517 /* Parse a function-body.
14520 compound_statement */
14523 cp_parser_function_body (cp_parser *parser)
14525 cp_parser_compound_statement (parser, NULL, false);
14528 /* Parse a ctor-initializer-opt followed by a function-body. Return
14529 true if a ctor-initializer was present. */
14532 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14535 bool ctor_initializer_p;
14537 /* Begin the function body. */
14538 body = begin_function_body ();
14539 /* Parse the optional ctor-initializer. */
14540 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14541 /* Parse the function-body. */
14542 cp_parser_function_body (parser);
14543 /* Finish the function body. */
14544 finish_function_body (body);
14546 return ctor_initializer_p;
14549 /* Parse an initializer.
14552 = initializer-clause
14553 ( expression-list )
14555 Returns an expression representing the initializer. If no
14556 initializer is present, NULL_TREE is returned.
14558 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14559 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14560 set to TRUE if there is no initializer present. If there is an
14561 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14562 is set to true; otherwise it is set to false. */
14565 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14566 bool* non_constant_p)
14571 /* Peek at the next token. */
14572 token = cp_lexer_peek_token (parser->lexer);
14574 /* Let our caller know whether or not this initializer was
14576 *is_direct_init = (token->type != CPP_EQ);
14577 /* Assume that the initializer is constant. */
14578 *non_constant_p = false;
14580 if (token->type == CPP_EQ)
14582 /* Consume the `='. */
14583 cp_lexer_consume_token (parser->lexer);
14584 /* Parse the initializer-clause. */
14585 init = cp_parser_initializer_clause (parser, non_constant_p);
14587 else if (token->type == CPP_OPEN_PAREN)
14588 init = cp_parser_parenthesized_expression_list (parser, false,
14590 /*allow_expansion_p=*/true,
14592 else if (token->type == CPP_OPEN_BRACE)
14594 maybe_warn_cpp0x ("extended initializer lists");
14595 init = cp_parser_braced_list (parser, non_constant_p);
14596 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14600 /* Anything else is an error. */
14601 cp_parser_error (parser, "expected initializer");
14602 init = error_mark_node;
14608 /* Parse an initializer-clause.
14610 initializer-clause:
14611 assignment-expression
14614 Returns an expression representing the initializer.
14616 If the `assignment-expression' production is used the value
14617 returned is simply a representation for the expression.
14619 Otherwise, calls cp_parser_braced_list. */
14622 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14626 /* Assume the expression is constant. */
14627 *non_constant_p = false;
14629 /* If it is not a `{', then we are looking at an
14630 assignment-expression. */
14631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14634 = cp_parser_constant_expression (parser,
14635 /*allow_non_constant_p=*/true,
14637 if (!*non_constant_p)
14638 initializer = fold_non_dependent_expr (initializer);
14641 initializer = cp_parser_braced_list (parser, non_constant_p);
14643 return initializer;
14646 /* Parse a brace-enclosed initializer list.
14649 { initializer-list , [opt] }
14652 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14653 the elements of the initializer-list (or NULL, if the last
14654 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14655 NULL_TREE. There is no way to detect whether or not the optional
14656 trailing `,' was provided. NON_CONSTANT_P is as for
14657 cp_parser_initializer. */
14660 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14664 /* Consume the `{' token. */
14665 cp_lexer_consume_token (parser->lexer);
14666 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14667 initializer = make_node (CONSTRUCTOR);
14668 /* If it's not a `}', then there is a non-trivial initializer. */
14669 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14671 /* Parse the initializer list. */
14672 CONSTRUCTOR_ELTS (initializer)
14673 = cp_parser_initializer_list (parser, non_constant_p);
14674 /* A trailing `,' token is allowed. */
14675 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14676 cp_lexer_consume_token (parser->lexer);
14678 /* Now, there should be a trailing `}'. */
14679 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14680 TREE_TYPE (initializer) = init_list_type_node;
14681 return initializer;
14684 /* Parse an initializer-list.
14687 initializer-clause ... [opt]
14688 initializer-list , initializer-clause ... [opt]
14693 identifier : initializer-clause
14694 initializer-list, identifier : initializer-clause
14696 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14697 for the initializer. If the INDEX of the elt is non-NULL, it is the
14698 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14699 as for cp_parser_initializer. */
14701 static VEC(constructor_elt,gc) *
14702 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14704 VEC(constructor_elt,gc) *v = NULL;
14706 /* Assume all of the expressions are constant. */
14707 *non_constant_p = false;
14709 /* Parse the rest of the list. */
14715 bool clause_non_constant_p;
14717 /* If the next token is an identifier and the following one is a
14718 colon, we are looking at the GNU designated-initializer
14720 if (cp_parser_allow_gnu_extensions_p (parser)
14721 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14722 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14724 /* Warn the user that they are using an extension. */
14725 pedwarn (input_location, OPT_pedantic,
14726 "ISO C++ does not allow designated initializers");
14727 /* Consume the identifier. */
14728 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14729 /* Consume the `:'. */
14730 cp_lexer_consume_token (parser->lexer);
14733 identifier = NULL_TREE;
14735 /* Parse the initializer. */
14736 initializer = cp_parser_initializer_clause (parser,
14737 &clause_non_constant_p);
14738 /* If any clause is non-constant, so is the entire initializer. */
14739 if (clause_non_constant_p)
14740 *non_constant_p = true;
14742 /* If we have an ellipsis, this is an initializer pack
14744 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14746 /* Consume the `...'. */
14747 cp_lexer_consume_token (parser->lexer);
14749 /* Turn the initializer into an initializer expansion. */
14750 initializer = make_pack_expansion (initializer);
14753 /* Add it to the vector. */
14754 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14756 /* If the next token is not a comma, we have reached the end of
14758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14761 /* Peek at the next token. */
14762 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14763 /* If the next token is a `}', then we're still done. An
14764 initializer-clause can have a trailing `,' after the
14765 initializer-list and before the closing `}'. */
14766 if (token->type == CPP_CLOSE_BRACE)
14769 /* Consume the `,' token. */
14770 cp_lexer_consume_token (parser->lexer);
14776 /* Classes [gram.class] */
14778 /* Parse a class-name.
14784 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14785 to indicate that names looked up in dependent types should be
14786 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14787 keyword has been used to indicate that the name that appears next
14788 is a template. TAG_TYPE indicates the explicit tag given before
14789 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14790 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14791 is the class being defined in a class-head.
14793 Returns the TYPE_DECL representing the class. */
14796 cp_parser_class_name (cp_parser *parser,
14797 bool typename_keyword_p,
14798 bool template_keyword_p,
14799 enum tag_types tag_type,
14800 bool check_dependency_p,
14802 bool is_declaration)
14808 tree identifier = NULL_TREE;
14810 /* All class-names start with an identifier. */
14811 token = cp_lexer_peek_token (parser->lexer);
14812 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14814 cp_parser_error (parser, "expected class-name");
14815 return error_mark_node;
14818 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14819 to a template-id, so we save it here. */
14820 scope = parser->scope;
14821 if (scope == error_mark_node)
14822 return error_mark_node;
14824 /* Any name names a type if we're following the `typename' keyword
14825 in a qualified name where the enclosing scope is type-dependent. */
14826 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14827 && dependent_type_p (scope));
14828 /* Handle the common case (an identifier, but not a template-id)
14830 if (token->type == CPP_NAME
14831 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14833 cp_token *identifier_token;
14836 /* Look for the identifier. */
14837 identifier_token = cp_lexer_peek_token (parser->lexer);
14838 ambiguous_p = identifier_token->ambiguous_p;
14839 identifier = cp_parser_identifier (parser);
14840 /* If the next token isn't an identifier, we are certainly not
14841 looking at a class-name. */
14842 if (identifier == error_mark_node)
14843 decl = error_mark_node;
14844 /* If we know this is a type-name, there's no need to look it
14846 else if (typename_p)
14850 tree ambiguous_decls;
14851 /* If we already know that this lookup is ambiguous, then
14852 we've already issued an error message; there's no reason
14856 cp_parser_simulate_error (parser);
14857 return error_mark_node;
14859 /* If the next token is a `::', then the name must be a type
14862 [basic.lookup.qual]
14864 During the lookup for a name preceding the :: scope
14865 resolution operator, object, function, and enumerator
14866 names are ignored. */
14867 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14868 tag_type = typename_type;
14869 /* Look up the name. */
14870 decl = cp_parser_lookup_name (parser, identifier,
14872 /*is_template=*/false,
14873 /*is_namespace=*/false,
14874 check_dependency_p,
14876 identifier_token->location);
14877 if (ambiguous_decls)
14879 error ("%Hreference to %qD is ambiguous",
14880 &identifier_token->location, identifier);
14881 print_candidates (ambiguous_decls);
14882 if (cp_parser_parsing_tentatively (parser))
14884 identifier_token->ambiguous_p = true;
14885 cp_parser_simulate_error (parser);
14887 return error_mark_node;
14893 /* Try a template-id. */
14894 decl = cp_parser_template_id (parser, template_keyword_p,
14895 check_dependency_p,
14897 if (decl == error_mark_node)
14898 return error_mark_node;
14901 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14903 /* If this is a typename, create a TYPENAME_TYPE. */
14904 if (typename_p && decl != error_mark_node)
14906 decl = make_typename_type (scope, decl, typename_type,
14907 /*complain=*/tf_error);
14908 if (decl != error_mark_node)
14909 decl = TYPE_NAME (decl);
14912 /* Check to see that it is really the name of a class. */
14913 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14914 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14915 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14916 /* Situations like this:
14918 template <typename T> struct A {
14919 typename T::template X<int>::I i;
14922 are problematic. Is `T::template X<int>' a class-name? The
14923 standard does not seem to be definitive, but there is no other
14924 valid interpretation of the following `::'. Therefore, those
14925 names are considered class-names. */
14927 decl = make_typename_type (scope, decl, tag_type, tf_error);
14928 if (decl != error_mark_node)
14929 decl = TYPE_NAME (decl);
14931 else if (TREE_CODE (decl) != TYPE_DECL
14932 || TREE_TYPE (decl) == error_mark_node
14933 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14934 decl = error_mark_node;
14936 if (decl == error_mark_node)
14937 cp_parser_error (parser, "expected class-name");
14938 else if (identifier && !parser->scope)
14939 maybe_note_name_used_in_class (identifier, decl);
14944 /* Parse a class-specifier.
14947 class-head { member-specification [opt] }
14949 Returns the TREE_TYPE representing the class. */
14952 cp_parser_class_specifier (cp_parser* parser)
14955 tree attributes = NULL_TREE;
14956 bool nested_name_specifier_p;
14957 unsigned saved_num_template_parameter_lists;
14958 bool saved_in_function_body;
14959 bool saved_in_unbraced_linkage_specification_p;
14960 tree old_scope = NULL_TREE;
14961 tree scope = NULL_TREE;
14964 push_deferring_access_checks (dk_no_deferred);
14966 /* Parse the class-head. */
14967 type = cp_parser_class_head (parser,
14968 &nested_name_specifier_p,
14971 /* If the class-head was a semantic disaster, skip the entire body
14975 cp_parser_skip_to_end_of_block_or_statement (parser);
14976 pop_deferring_access_checks ();
14977 return error_mark_node;
14980 /* Look for the `{'. */
14981 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14983 pop_deferring_access_checks ();
14984 return error_mark_node;
14987 /* Process the base classes. If they're invalid, skip the
14988 entire class body. */
14989 if (!xref_basetypes (type, bases))
14991 /* Consuming the closing brace yields better error messages
14993 if (cp_parser_skip_to_closing_brace (parser))
14994 cp_lexer_consume_token (parser->lexer);
14995 pop_deferring_access_checks ();
14996 return error_mark_node;
14999 /* Issue an error message if type-definitions are forbidden here. */
15000 cp_parser_check_type_definition (parser);
15001 /* Remember that we are defining one more class. */
15002 ++parser->num_classes_being_defined;
15003 /* Inside the class, surrounding template-parameter-lists do not
15005 saved_num_template_parameter_lists
15006 = parser->num_template_parameter_lists;
15007 parser->num_template_parameter_lists = 0;
15008 /* We are not in a function body. */
15009 saved_in_function_body = parser->in_function_body;
15010 parser->in_function_body = false;
15011 /* We are not immediately inside an extern "lang" block. */
15012 saved_in_unbraced_linkage_specification_p
15013 = parser->in_unbraced_linkage_specification_p;
15014 parser->in_unbraced_linkage_specification_p = false;
15016 /* Start the class. */
15017 if (nested_name_specifier_p)
15019 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15020 old_scope = push_inner_scope (scope);
15022 type = begin_class_definition (type, attributes);
15024 if (type == error_mark_node)
15025 /* If the type is erroneous, skip the entire body of the class. */
15026 cp_parser_skip_to_closing_brace (parser);
15028 /* Parse the member-specification. */
15029 cp_parser_member_specification_opt (parser);
15031 /* Look for the trailing `}'. */
15032 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15033 /* Look for trailing attributes to apply to this class. */
15034 if (cp_parser_allow_gnu_extensions_p (parser))
15035 attributes = cp_parser_attributes_opt (parser);
15036 if (type != error_mark_node)
15037 type = finish_struct (type, attributes);
15038 if (nested_name_specifier_p)
15039 pop_inner_scope (old_scope, scope);
15040 /* If this class is not itself within the scope of another class,
15041 then we need to parse the bodies of all of the queued function
15042 definitions. Note that the queued functions defined in a class
15043 are not always processed immediately following the
15044 class-specifier for that class. Consider:
15047 struct B { void f() { sizeof (A); } };
15050 If `f' were processed before the processing of `A' were
15051 completed, there would be no way to compute the size of `A'.
15052 Note that the nesting we are interested in here is lexical --
15053 not the semantic nesting given by TYPE_CONTEXT. In particular,
15056 struct A { struct B; };
15057 struct A::B { void f() { } };
15059 there is no need to delay the parsing of `A::B::f'. */
15060 if (--parser->num_classes_being_defined == 0)
15064 tree class_type = NULL_TREE;
15065 tree pushed_scope = NULL_TREE;
15067 /* In a first pass, parse default arguments to the functions.
15068 Then, in a second pass, parse the bodies of the functions.
15069 This two-phased approach handles cases like:
15077 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15078 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15079 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15080 TREE_PURPOSE (parser->unparsed_functions_queues)
15081 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15083 fn = TREE_VALUE (queue_entry);
15084 /* If there are default arguments that have not yet been processed,
15085 take care of them now. */
15086 if (class_type != TREE_PURPOSE (queue_entry))
15089 pop_scope (pushed_scope);
15090 class_type = TREE_PURPOSE (queue_entry);
15091 pushed_scope = push_scope (class_type);
15093 /* Make sure that any template parameters are in scope. */
15094 maybe_begin_member_template_processing (fn);
15095 /* Parse the default argument expressions. */
15096 cp_parser_late_parsing_default_args (parser, fn);
15097 /* Remove any template parameters from the symbol table. */
15098 maybe_end_member_template_processing ();
15101 pop_scope (pushed_scope);
15102 /* Now parse the body of the functions. */
15103 for (TREE_VALUE (parser->unparsed_functions_queues)
15104 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15105 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15106 TREE_VALUE (parser->unparsed_functions_queues)
15107 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15109 /* Figure out which function we need to process. */
15110 fn = TREE_VALUE (queue_entry);
15111 /* Parse the function. */
15112 cp_parser_late_parsing_for_member (parser, fn);
15116 /* Put back any saved access checks. */
15117 pop_deferring_access_checks ();
15119 /* Restore saved state. */
15120 parser->in_function_body = saved_in_function_body;
15121 parser->num_template_parameter_lists
15122 = saved_num_template_parameter_lists;
15123 parser->in_unbraced_linkage_specification_p
15124 = saved_in_unbraced_linkage_specification_p;
15129 /* Parse a class-head.
15132 class-key identifier [opt] base-clause [opt]
15133 class-key nested-name-specifier identifier base-clause [opt]
15134 class-key nested-name-specifier [opt] template-id
15138 class-key attributes identifier [opt] base-clause [opt]
15139 class-key attributes nested-name-specifier identifier base-clause [opt]
15140 class-key attributes nested-name-specifier [opt] template-id
15143 Upon return BASES is initialized to the list of base classes (or
15144 NULL, if there are none) in the same form returned by
15145 cp_parser_base_clause.
15147 Returns the TYPE of the indicated class. Sets
15148 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15149 involving a nested-name-specifier was used, and FALSE otherwise.
15151 Returns error_mark_node if this is not a class-head.
15153 Returns NULL_TREE if the class-head is syntactically valid, but
15154 semantically invalid in a way that means we should skip the entire
15155 body of the class. */
15158 cp_parser_class_head (cp_parser* parser,
15159 bool* nested_name_specifier_p,
15160 tree *attributes_p,
15163 tree nested_name_specifier;
15164 enum tag_types class_key;
15165 tree id = NULL_TREE;
15166 tree type = NULL_TREE;
15168 bool template_id_p = false;
15169 bool qualified_p = false;
15170 bool invalid_nested_name_p = false;
15171 bool invalid_explicit_specialization_p = false;
15172 tree pushed_scope = NULL_TREE;
15173 unsigned num_templates;
15174 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15175 /* Assume no nested-name-specifier will be present. */
15176 *nested_name_specifier_p = false;
15177 /* Assume no template parameter lists will be used in defining the
15181 *bases = NULL_TREE;
15183 /* Look for the class-key. */
15184 class_key = cp_parser_class_key (parser);
15185 if (class_key == none_type)
15186 return error_mark_node;
15188 /* Parse the attributes. */
15189 attributes = cp_parser_attributes_opt (parser);
15191 /* If the next token is `::', that is invalid -- but sometimes
15192 people do try to write:
15196 Handle this gracefully by accepting the extra qualifier, and then
15197 issuing an error about it later if this really is a
15198 class-head. If it turns out just to be an elaborated type
15199 specifier, remain silent. */
15200 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15201 qualified_p = true;
15203 push_deferring_access_checks (dk_no_check);
15205 /* Determine the name of the class. Begin by looking for an
15206 optional nested-name-specifier. */
15207 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15208 nested_name_specifier
15209 = cp_parser_nested_name_specifier_opt (parser,
15210 /*typename_keyword_p=*/false,
15211 /*check_dependency_p=*/false,
15213 /*is_declaration=*/false);
15214 /* If there was a nested-name-specifier, then there *must* be an
15216 if (nested_name_specifier)
15218 type_start_token = cp_lexer_peek_token (parser->lexer);
15219 /* Although the grammar says `identifier', it really means
15220 `class-name' or `template-name'. You are only allowed to
15221 define a class that has already been declared with this
15224 The proposed resolution for Core Issue 180 says that wherever
15225 you see `class T::X' you should treat `X' as a type-name.
15227 It is OK to define an inaccessible class; for example:
15229 class A { class B; };
15232 We do not know if we will see a class-name, or a
15233 template-name. We look for a class-name first, in case the
15234 class-name is a template-id; if we looked for the
15235 template-name first we would stop after the template-name. */
15236 cp_parser_parse_tentatively (parser);
15237 type = cp_parser_class_name (parser,
15238 /*typename_keyword_p=*/false,
15239 /*template_keyword_p=*/false,
15241 /*check_dependency_p=*/false,
15242 /*class_head_p=*/true,
15243 /*is_declaration=*/false);
15244 /* If that didn't work, ignore the nested-name-specifier. */
15245 if (!cp_parser_parse_definitely (parser))
15247 invalid_nested_name_p = true;
15248 type_start_token = cp_lexer_peek_token (parser->lexer);
15249 id = cp_parser_identifier (parser);
15250 if (id == error_mark_node)
15253 /* If we could not find a corresponding TYPE, treat this
15254 declaration like an unqualified declaration. */
15255 if (type == error_mark_node)
15256 nested_name_specifier = NULL_TREE;
15257 /* Otherwise, count the number of templates used in TYPE and its
15258 containing scopes. */
15263 for (scope = TREE_TYPE (type);
15264 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15265 scope = (TYPE_P (scope)
15266 ? TYPE_CONTEXT (scope)
15267 : DECL_CONTEXT (scope)))
15269 && CLASS_TYPE_P (scope)
15270 && CLASSTYPE_TEMPLATE_INFO (scope)
15271 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15272 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15276 /* Otherwise, the identifier is optional. */
15279 /* We don't know whether what comes next is a template-id,
15280 an identifier, or nothing at all. */
15281 cp_parser_parse_tentatively (parser);
15282 /* Check for a template-id. */
15283 type_start_token = cp_lexer_peek_token (parser->lexer);
15284 id = cp_parser_template_id (parser,
15285 /*template_keyword_p=*/false,
15286 /*check_dependency_p=*/true,
15287 /*is_declaration=*/true);
15288 /* If that didn't work, it could still be an identifier. */
15289 if (!cp_parser_parse_definitely (parser))
15291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15293 type_start_token = cp_lexer_peek_token (parser->lexer);
15294 id = cp_parser_identifier (parser);
15301 template_id_p = true;
15306 pop_deferring_access_checks ();
15309 cp_parser_check_for_invalid_template_id (parser, id,
15310 type_start_token->location);
15312 /* If it's not a `:' or a `{' then we can't really be looking at a
15313 class-head, since a class-head only appears as part of a
15314 class-specifier. We have to detect this situation before calling
15315 xref_tag, since that has irreversible side-effects. */
15316 if (!cp_parser_next_token_starts_class_definition_p (parser))
15318 cp_parser_error (parser, "expected %<{%> or %<:%>");
15319 return error_mark_node;
15322 /* At this point, we're going ahead with the class-specifier, even
15323 if some other problem occurs. */
15324 cp_parser_commit_to_tentative_parse (parser);
15325 /* Issue the error about the overly-qualified name now. */
15328 cp_parser_error (parser,
15329 "global qualification of class name is invalid");
15330 return error_mark_node;
15332 else if (invalid_nested_name_p)
15334 cp_parser_error (parser,
15335 "qualified name does not name a class");
15336 return error_mark_node;
15338 else if (nested_name_specifier)
15342 /* Reject typedef-names in class heads. */
15343 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15345 error ("%Hinvalid class name in declaration of %qD",
15346 &type_start_token->location, type);
15351 /* Figure out in what scope the declaration is being placed. */
15352 scope = current_scope ();
15353 /* If that scope does not contain the scope in which the
15354 class was originally declared, the program is invalid. */
15355 if (scope && !is_ancestor (scope, nested_name_specifier))
15357 if (at_namespace_scope_p ())
15358 error ("%Hdeclaration of %qD in namespace %qD which does not "
15360 &type_start_token->location,
15361 type, scope, nested_name_specifier);
15363 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15364 &type_start_token->location,
15365 type, scope, nested_name_specifier);
15371 A declarator-id shall not be qualified except for the
15372 definition of a ... nested class outside of its class
15373 ... [or] the definition or explicit instantiation of a
15374 class member of a namespace outside of its namespace. */
15375 if (scope == nested_name_specifier)
15377 permerror (input_location, "%Hextra qualification not allowed",
15378 &nested_name_specifier_token_start->location);
15379 nested_name_specifier = NULL_TREE;
15383 /* An explicit-specialization must be preceded by "template <>". If
15384 it is not, try to recover gracefully. */
15385 if (at_namespace_scope_p ()
15386 && parser->num_template_parameter_lists == 0
15389 error ("%Han explicit specialization must be preceded by %<template <>%>",
15390 &type_start_token->location);
15391 invalid_explicit_specialization_p = true;
15392 /* Take the same action that would have been taken by
15393 cp_parser_explicit_specialization. */
15394 ++parser->num_template_parameter_lists;
15395 begin_specialization ();
15397 /* There must be no "return" statements between this point and the
15398 end of this function; set "type "to the correct return value and
15399 use "goto done;" to return. */
15400 /* Make sure that the right number of template parameters were
15402 if (!cp_parser_check_template_parameters (parser, num_templates,
15403 type_start_token->location,
15404 /*declarator=*/NULL))
15406 /* If something went wrong, there is no point in even trying to
15407 process the class-definition. */
15412 /* Look up the type. */
15415 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15416 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15417 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15419 error ("%Hfunction template %qD redeclared as a class template",
15420 &type_start_token->location, id);
15421 type = error_mark_node;
15425 type = TREE_TYPE (id);
15426 type = maybe_process_partial_specialization (type);
15428 if (nested_name_specifier)
15429 pushed_scope = push_scope (nested_name_specifier);
15431 else if (nested_name_specifier)
15437 template <typename T> struct S { struct T };
15438 template <typename T> struct S<T>::T { };
15440 we will get a TYPENAME_TYPE when processing the definition of
15441 `S::T'. We need to resolve it to the actual type before we
15442 try to define it. */
15443 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15445 class_type = resolve_typename_type (TREE_TYPE (type),
15446 /*only_current_p=*/false);
15447 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15448 type = TYPE_NAME (class_type);
15451 cp_parser_error (parser, "could not resolve typename type");
15452 type = error_mark_node;
15456 if (maybe_process_partial_specialization (TREE_TYPE (type))
15457 == error_mark_node)
15463 class_type = current_class_type;
15464 /* Enter the scope indicated by the nested-name-specifier. */
15465 pushed_scope = push_scope (nested_name_specifier);
15466 /* Get the canonical version of this type. */
15467 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15468 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15469 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15471 type = push_template_decl (type);
15472 if (type == error_mark_node)
15479 type = TREE_TYPE (type);
15480 *nested_name_specifier_p = true;
15482 else /* The name is not a nested name. */
15484 /* If the class was unnamed, create a dummy name. */
15486 id = make_anon_name ();
15487 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15488 parser->num_template_parameter_lists);
15491 /* Indicate whether this class was declared as a `class' or as a
15493 if (TREE_CODE (type) == RECORD_TYPE)
15494 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15495 cp_parser_check_class_key (class_key, type);
15497 /* If this type was already complete, and we see another definition,
15498 that's an error. */
15499 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15501 error ("%Hredefinition of %q#T",
15502 &type_start_token->location, type);
15503 error ("%Hprevious definition of %q+#T",
15504 &type_start_token->location, type);
15508 else if (type == error_mark_node)
15511 /* We will have entered the scope containing the class; the names of
15512 base classes should be looked up in that context. For example:
15514 struct A { struct B {}; struct C; };
15515 struct A::C : B {};
15519 /* Get the list of base-classes, if there is one. */
15520 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15521 *bases = cp_parser_base_clause (parser);
15524 /* Leave the scope given by the nested-name-specifier. We will
15525 enter the class scope itself while processing the members. */
15527 pop_scope (pushed_scope);
15529 if (invalid_explicit_specialization_p)
15531 end_specialization ();
15532 --parser->num_template_parameter_lists;
15534 *attributes_p = attributes;
15538 /* Parse a class-key.
15545 Returns the kind of class-key specified, or none_type to indicate
15548 static enum tag_types
15549 cp_parser_class_key (cp_parser* parser)
15552 enum tag_types tag_type;
15554 /* Look for the class-key. */
15555 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15559 /* Check to see if the TOKEN is a class-key. */
15560 tag_type = cp_parser_token_is_class_key (token);
15562 cp_parser_error (parser, "expected class-key");
15566 /* Parse an (optional) member-specification.
15568 member-specification:
15569 member-declaration member-specification [opt]
15570 access-specifier : member-specification [opt] */
15573 cp_parser_member_specification_opt (cp_parser* parser)
15580 /* Peek at the next token. */
15581 token = cp_lexer_peek_token (parser->lexer);
15582 /* If it's a `}', or EOF then we've seen all the members. */
15583 if (token->type == CPP_CLOSE_BRACE
15584 || token->type == CPP_EOF
15585 || token->type == CPP_PRAGMA_EOL)
15588 /* See if this token is a keyword. */
15589 keyword = token->keyword;
15593 case RID_PROTECTED:
15595 /* Consume the access-specifier. */
15596 cp_lexer_consume_token (parser->lexer);
15597 /* Remember which access-specifier is active. */
15598 current_access_specifier = token->u.value;
15599 /* Look for the `:'. */
15600 cp_parser_require (parser, CPP_COLON, "%<:%>");
15604 /* Accept #pragmas at class scope. */
15605 if (token->type == CPP_PRAGMA)
15607 cp_parser_pragma (parser, pragma_external);
15611 /* Otherwise, the next construction must be a
15612 member-declaration. */
15613 cp_parser_member_declaration (parser);
15618 /* Parse a member-declaration.
15620 member-declaration:
15621 decl-specifier-seq [opt] member-declarator-list [opt] ;
15622 function-definition ; [opt]
15623 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15625 template-declaration
15627 member-declarator-list:
15629 member-declarator-list , member-declarator
15632 declarator pure-specifier [opt]
15633 declarator constant-initializer [opt]
15634 identifier [opt] : constant-expression
15638 member-declaration:
15639 __extension__ member-declaration
15642 declarator attributes [opt] pure-specifier [opt]
15643 declarator attributes [opt] constant-initializer [opt]
15644 identifier [opt] attributes [opt] : constant-expression
15648 member-declaration:
15649 static_assert-declaration */
15652 cp_parser_member_declaration (cp_parser* parser)
15654 cp_decl_specifier_seq decl_specifiers;
15655 tree prefix_attributes;
15657 int declares_class_or_enum;
15659 cp_token *token = NULL;
15660 cp_token *decl_spec_token_start = NULL;
15661 cp_token *initializer_token_start = NULL;
15662 int saved_pedantic;
15664 /* Check for the `__extension__' keyword. */
15665 if (cp_parser_extension_opt (parser, &saved_pedantic))
15668 cp_parser_member_declaration (parser);
15669 /* Restore the old value of the PEDANTIC flag. */
15670 pedantic = saved_pedantic;
15675 /* Check for a template-declaration. */
15676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15678 /* An explicit specialization here is an error condition, and we
15679 expect the specialization handler to detect and report this. */
15680 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15681 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15682 cp_parser_explicit_specialization (parser);
15684 cp_parser_template_declaration (parser, /*member_p=*/true);
15689 /* Check for a using-declaration. */
15690 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15692 /* Parse the using-declaration. */
15693 cp_parser_using_declaration (parser,
15694 /*access_declaration_p=*/false);
15698 /* Check for @defs. */
15699 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15702 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15703 ivar = ivar_chains;
15707 ivar = TREE_CHAIN (member);
15708 TREE_CHAIN (member) = NULL_TREE;
15709 finish_member_declaration (member);
15714 /* If the next token is `static_assert' we have a static assertion. */
15715 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15717 cp_parser_static_assert (parser, /*member_p=*/true);
15721 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15724 /* Parse the decl-specifier-seq. */
15725 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15726 cp_parser_decl_specifier_seq (parser,
15727 CP_PARSER_FLAGS_OPTIONAL,
15729 &declares_class_or_enum);
15730 prefix_attributes = decl_specifiers.attributes;
15731 decl_specifiers.attributes = NULL_TREE;
15732 /* Check for an invalid type-name. */
15733 if (!decl_specifiers.type
15734 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15736 /* If there is no declarator, then the decl-specifier-seq should
15738 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15740 /* If there was no decl-specifier-seq, and the next token is a
15741 `;', then we have something like:
15747 Each member-declaration shall declare at least one member
15748 name of the class. */
15749 if (!decl_specifiers.any_specifiers_p)
15751 cp_token *token = cp_lexer_peek_token (parser->lexer);
15752 if (!in_system_header_at (token->location))
15753 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15759 /* See if this declaration is a friend. */
15760 friend_p = cp_parser_friend_p (&decl_specifiers);
15761 /* If there were decl-specifiers, check to see if there was
15762 a class-declaration. */
15763 type = check_tag_decl (&decl_specifiers);
15764 /* Nested classes have already been added to the class, but
15765 a `friend' needs to be explicitly registered. */
15768 /* If the `friend' keyword was present, the friend must
15769 be introduced with a class-key. */
15770 if (!declares_class_or_enum)
15771 error ("%Ha class-key must be used when declaring a friend",
15772 &decl_spec_token_start->location);
15775 template <typename T> struct A {
15776 friend struct A<T>::B;
15779 A<T>::B will be represented by a TYPENAME_TYPE, and
15780 therefore not recognized by check_tag_decl. */
15782 && decl_specifiers.type
15783 && TYPE_P (decl_specifiers.type))
15784 type = decl_specifiers.type;
15785 if (!type || !TYPE_P (type))
15786 error ("%Hfriend declaration does not name a class or "
15787 "function", &decl_spec_token_start->location);
15789 make_friend_class (current_class_type, type,
15790 /*complain=*/true);
15792 /* If there is no TYPE, an error message will already have
15794 else if (!type || type == error_mark_node)
15796 /* An anonymous aggregate has to be handled specially; such
15797 a declaration really declares a data member (with a
15798 particular type), as opposed to a nested class. */
15799 else if (ANON_AGGR_TYPE_P (type))
15801 /* Remove constructors and such from TYPE, now that we
15802 know it is an anonymous aggregate. */
15803 fixup_anonymous_aggr (type);
15804 /* And make the corresponding data member. */
15805 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15806 /* Add it to the class. */
15807 finish_member_declaration (decl);
15810 cp_parser_check_access_in_redeclaration
15812 decl_spec_token_start->location);
15817 /* See if these declarations will be friends. */
15818 friend_p = cp_parser_friend_p (&decl_specifiers);
15820 /* Keep going until we hit the `;' at the end of the
15822 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15824 tree attributes = NULL_TREE;
15825 tree first_attribute;
15827 /* Peek at the next token. */
15828 token = cp_lexer_peek_token (parser->lexer);
15830 /* Check for a bitfield declaration. */
15831 if (token->type == CPP_COLON
15832 || (token->type == CPP_NAME
15833 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15839 /* Get the name of the bitfield. Note that we cannot just
15840 check TOKEN here because it may have been invalidated by
15841 the call to cp_lexer_peek_nth_token above. */
15842 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15843 identifier = cp_parser_identifier (parser);
15845 identifier = NULL_TREE;
15847 /* Consume the `:' token. */
15848 cp_lexer_consume_token (parser->lexer);
15849 /* Get the width of the bitfield. */
15851 = cp_parser_constant_expression (parser,
15852 /*allow_non_constant=*/false,
15855 /* Look for attributes that apply to the bitfield. */
15856 attributes = cp_parser_attributes_opt (parser);
15857 /* Remember which attributes are prefix attributes and
15859 first_attribute = attributes;
15860 /* Combine the attributes. */
15861 attributes = chainon (prefix_attributes, attributes);
15863 /* Create the bitfield declaration. */
15864 decl = grokbitfield (identifier
15865 ? make_id_declarator (NULL_TREE,
15875 cp_declarator *declarator;
15877 tree asm_specification;
15878 int ctor_dtor_or_conv_p;
15880 /* Parse the declarator. */
15882 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15883 &ctor_dtor_or_conv_p,
15884 /*parenthesized_p=*/NULL,
15885 /*member_p=*/true);
15887 /* If something went wrong parsing the declarator, make sure
15888 that we at least consume some tokens. */
15889 if (declarator == cp_error_declarator)
15891 /* Skip to the end of the statement. */
15892 cp_parser_skip_to_end_of_statement (parser);
15893 /* If the next token is not a semicolon, that is
15894 probably because we just skipped over the body of
15895 a function. So, we consume a semicolon if
15896 present, but do not issue an error message if it
15898 if (cp_lexer_next_token_is (parser->lexer,
15900 cp_lexer_consume_token (parser->lexer);
15904 if (declares_class_or_enum & 2)
15905 cp_parser_check_for_definition_in_return_type
15906 (declarator, decl_specifiers.type,
15907 decl_specifiers.type_location);
15909 /* Look for an asm-specification. */
15910 asm_specification = cp_parser_asm_specification_opt (parser);
15911 /* Look for attributes that apply to the declaration. */
15912 attributes = cp_parser_attributes_opt (parser);
15913 /* Remember which attributes are prefix attributes and
15915 first_attribute = attributes;
15916 /* Combine the attributes. */
15917 attributes = chainon (prefix_attributes, attributes);
15919 /* If it's an `=', then we have a constant-initializer or a
15920 pure-specifier. It is not correct to parse the
15921 initializer before registering the member declaration
15922 since the member declaration should be in scope while
15923 its initializer is processed. However, the rest of the
15924 front end does not yet provide an interface that allows
15925 us to handle this correctly. */
15926 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15930 A pure-specifier shall be used only in the declaration of
15931 a virtual function.
15933 A member-declarator can contain a constant-initializer
15934 only if it declares a static member of integral or
15937 Therefore, if the DECLARATOR is for a function, we look
15938 for a pure-specifier; otherwise, we look for a
15939 constant-initializer. When we call `grokfield', it will
15940 perform more stringent semantics checks. */
15941 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15942 if (function_declarator_p (declarator))
15943 initializer = cp_parser_pure_specifier (parser);
15945 /* Parse the initializer. */
15946 initializer = cp_parser_constant_initializer (parser);
15948 /* Otherwise, there is no initializer. */
15950 initializer = NULL_TREE;
15952 /* See if we are probably looking at a function
15953 definition. We are certainly not looking at a
15954 member-declarator. Calling `grokfield' has
15955 side-effects, so we must not do it unless we are sure
15956 that we are looking at a member-declarator. */
15957 if (cp_parser_token_starts_function_definition_p
15958 (cp_lexer_peek_token (parser->lexer)))
15960 /* The grammar does not allow a pure-specifier to be
15961 used when a member function is defined. (It is
15962 possible that this fact is an oversight in the
15963 standard, since a pure function may be defined
15964 outside of the class-specifier. */
15966 error ("%Hpure-specifier on function-definition",
15967 &initializer_token_start->location);
15968 decl = cp_parser_save_member_function_body (parser,
15972 /* If the member was not a friend, declare it here. */
15974 finish_member_declaration (decl);
15975 /* Peek at the next token. */
15976 token = cp_lexer_peek_token (parser->lexer);
15977 /* If the next token is a semicolon, consume it. */
15978 if (token->type == CPP_SEMICOLON)
15979 cp_lexer_consume_token (parser->lexer);
15983 if (declarator->kind == cdk_function)
15984 declarator->id_loc = token->location;
15985 /* Create the declaration. */
15986 decl = grokfield (declarator, &decl_specifiers,
15987 initializer, /*init_const_expr_p=*/true,
15992 /* Reset PREFIX_ATTRIBUTES. */
15993 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15994 attributes = TREE_CHAIN (attributes);
15996 TREE_CHAIN (attributes) = NULL_TREE;
15998 /* If there is any qualification still in effect, clear it
15999 now; we will be starting fresh with the next declarator. */
16000 parser->scope = NULL_TREE;
16001 parser->qualifying_scope = NULL_TREE;
16002 parser->object_scope = NULL_TREE;
16003 /* If it's a `,', then there are more declarators. */
16004 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16005 cp_lexer_consume_token (parser->lexer);
16006 /* If the next token isn't a `;', then we have a parse error. */
16007 else if (cp_lexer_next_token_is_not (parser->lexer,
16010 cp_parser_error (parser, "expected %<;%>");
16011 /* Skip tokens until we find a `;'. */
16012 cp_parser_skip_to_end_of_statement (parser);
16019 /* Add DECL to the list of members. */
16021 finish_member_declaration (decl);
16023 if (TREE_CODE (decl) == FUNCTION_DECL)
16024 cp_parser_save_default_args (parser, decl);
16029 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16032 /* Parse a pure-specifier.
16037 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16038 Otherwise, ERROR_MARK_NODE is returned. */
16041 cp_parser_pure_specifier (cp_parser* parser)
16045 /* Look for the `=' token. */
16046 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16047 return error_mark_node;
16048 /* Look for the `0' token. */
16049 token = cp_lexer_peek_token (parser->lexer);
16051 if (token->type == CPP_EOF
16052 || token->type == CPP_PRAGMA_EOL)
16053 return error_mark_node;
16055 cp_lexer_consume_token (parser->lexer);
16057 /* Accept = default or = delete in c++0x mode. */
16058 if (token->keyword == RID_DEFAULT
16059 || token->keyword == RID_DELETE)
16061 maybe_warn_cpp0x ("defaulted and deleted functions");
16062 return token->u.value;
16065 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16066 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16068 cp_parser_error (parser,
16069 "invalid pure specifier (only %<= 0%> is allowed)");
16070 cp_parser_skip_to_end_of_statement (parser);
16071 return error_mark_node;
16073 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16075 error ("%Htemplates may not be %<virtual%>", &token->location);
16076 return error_mark_node;
16079 return integer_zero_node;
16082 /* Parse a constant-initializer.
16084 constant-initializer:
16085 = constant-expression
16087 Returns a representation of the constant-expression. */
16090 cp_parser_constant_initializer (cp_parser* parser)
16092 /* Look for the `=' token. */
16093 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16094 return error_mark_node;
16096 /* It is invalid to write:
16098 struct S { static const int i = { 7 }; };
16101 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16103 cp_parser_error (parser,
16104 "a brace-enclosed initializer is not allowed here");
16105 /* Consume the opening brace. */
16106 cp_lexer_consume_token (parser->lexer);
16107 /* Skip the initializer. */
16108 cp_parser_skip_to_closing_brace (parser);
16109 /* Look for the trailing `}'. */
16110 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16112 return error_mark_node;
16115 return cp_parser_constant_expression (parser,
16116 /*allow_non_constant=*/false,
16120 /* Derived classes [gram.class.derived] */
16122 /* Parse a base-clause.
16125 : base-specifier-list
16127 base-specifier-list:
16128 base-specifier ... [opt]
16129 base-specifier-list , base-specifier ... [opt]
16131 Returns a TREE_LIST representing the base-classes, in the order in
16132 which they were declared. The representation of each node is as
16133 described by cp_parser_base_specifier.
16135 In the case that no bases are specified, this function will return
16136 NULL_TREE, not ERROR_MARK_NODE. */
16139 cp_parser_base_clause (cp_parser* parser)
16141 tree bases = NULL_TREE;
16143 /* Look for the `:' that begins the list. */
16144 cp_parser_require (parser, CPP_COLON, "%<:%>");
16146 /* Scan the base-specifier-list. */
16151 bool pack_expansion_p = false;
16153 /* Look for the base-specifier. */
16154 base = cp_parser_base_specifier (parser);
16155 /* Look for the (optional) ellipsis. */
16156 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16158 /* Consume the `...'. */
16159 cp_lexer_consume_token (parser->lexer);
16161 pack_expansion_p = true;
16164 /* Add BASE to the front of the list. */
16165 if (base != error_mark_node)
16167 if (pack_expansion_p)
16168 /* Make this a pack expansion type. */
16169 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16172 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16174 TREE_CHAIN (base) = bases;
16178 /* Peek at the next token. */
16179 token = cp_lexer_peek_token (parser->lexer);
16180 /* If it's not a comma, then the list is complete. */
16181 if (token->type != CPP_COMMA)
16183 /* Consume the `,'. */
16184 cp_lexer_consume_token (parser->lexer);
16187 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16188 base class had a qualified name. However, the next name that
16189 appears is certainly not qualified. */
16190 parser->scope = NULL_TREE;
16191 parser->qualifying_scope = NULL_TREE;
16192 parser->object_scope = NULL_TREE;
16194 return nreverse (bases);
16197 /* Parse a base-specifier.
16200 :: [opt] nested-name-specifier [opt] class-name
16201 virtual access-specifier [opt] :: [opt] nested-name-specifier
16203 access-specifier virtual [opt] :: [opt] nested-name-specifier
16206 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16207 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16208 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16209 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16212 cp_parser_base_specifier (cp_parser* parser)
16216 bool virtual_p = false;
16217 bool duplicate_virtual_error_issued_p = false;
16218 bool duplicate_access_error_issued_p = false;
16219 bool class_scope_p, template_p;
16220 tree access = access_default_node;
16223 /* Process the optional `virtual' and `access-specifier'. */
16226 /* Peek at the next token. */
16227 token = cp_lexer_peek_token (parser->lexer);
16228 /* Process `virtual'. */
16229 switch (token->keyword)
16232 /* If `virtual' appears more than once, issue an error. */
16233 if (virtual_p && !duplicate_virtual_error_issued_p)
16235 cp_parser_error (parser,
16236 "%<virtual%> specified more than once in base-specified");
16237 duplicate_virtual_error_issued_p = true;
16242 /* Consume the `virtual' token. */
16243 cp_lexer_consume_token (parser->lexer);
16248 case RID_PROTECTED:
16250 /* If more than one access specifier appears, issue an
16252 if (access != access_default_node
16253 && !duplicate_access_error_issued_p)
16255 cp_parser_error (parser,
16256 "more than one access specifier in base-specified");
16257 duplicate_access_error_issued_p = true;
16260 access = ridpointers[(int) token->keyword];
16262 /* Consume the access-specifier. */
16263 cp_lexer_consume_token (parser->lexer);
16272 /* It is not uncommon to see programs mechanically, erroneously, use
16273 the 'typename' keyword to denote (dependent) qualified types
16274 as base classes. */
16275 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16277 token = cp_lexer_peek_token (parser->lexer);
16278 if (!processing_template_decl)
16279 error ("%Hkeyword %<typename%> not allowed outside of templates",
16282 error ("%Hkeyword %<typename%> not allowed in this context "
16283 "(the base class is implicitly a type)",
16285 cp_lexer_consume_token (parser->lexer);
16288 /* Look for the optional `::' operator. */
16289 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16290 /* Look for the nested-name-specifier. The simplest way to
16295 The keyword `typename' is not permitted in a base-specifier or
16296 mem-initializer; in these contexts a qualified name that
16297 depends on a template-parameter is implicitly assumed to be a
16300 is to pretend that we have seen the `typename' keyword at this
16302 cp_parser_nested_name_specifier_opt (parser,
16303 /*typename_keyword_p=*/true,
16304 /*check_dependency_p=*/true,
16306 /*is_declaration=*/true);
16307 /* If the base class is given by a qualified name, assume that names
16308 we see are type names or templates, as appropriate. */
16309 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16310 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16312 /* Finally, look for the class-name. */
16313 type = cp_parser_class_name (parser,
16317 /*check_dependency_p=*/true,
16318 /*class_head_p=*/false,
16319 /*is_declaration=*/true);
16321 if (type == error_mark_node)
16322 return error_mark_node;
16324 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16327 /* Exception handling [gram.exception] */
16329 /* Parse an (optional) exception-specification.
16331 exception-specification:
16332 throw ( type-id-list [opt] )
16334 Returns a TREE_LIST representing the exception-specification. The
16335 TREE_VALUE of each node is a type. */
16338 cp_parser_exception_specification_opt (cp_parser* parser)
16343 /* Peek at the next token. */
16344 token = cp_lexer_peek_token (parser->lexer);
16345 /* If it's not `throw', then there's no exception-specification. */
16346 if (!cp_parser_is_keyword (token, RID_THROW))
16349 /* Consume the `throw'. */
16350 cp_lexer_consume_token (parser->lexer);
16352 /* Look for the `('. */
16353 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16355 /* Peek at the next token. */
16356 token = cp_lexer_peek_token (parser->lexer);
16357 /* If it's not a `)', then there is a type-id-list. */
16358 if (token->type != CPP_CLOSE_PAREN)
16360 const char *saved_message;
16362 /* Types may not be defined in an exception-specification. */
16363 saved_message = parser->type_definition_forbidden_message;
16364 parser->type_definition_forbidden_message
16365 = "types may not be defined in an exception-specification";
16366 /* Parse the type-id-list. */
16367 type_id_list = cp_parser_type_id_list (parser);
16368 /* Restore the saved message. */
16369 parser->type_definition_forbidden_message = saved_message;
16372 type_id_list = empty_except_spec;
16374 /* Look for the `)'. */
16375 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16377 return type_id_list;
16380 /* Parse an (optional) type-id-list.
16384 type-id-list , type-id ... [opt]
16386 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16387 in the order that the types were presented. */
16390 cp_parser_type_id_list (cp_parser* parser)
16392 tree types = NULL_TREE;
16399 /* Get the next type-id. */
16400 type = cp_parser_type_id (parser);
16401 /* Parse the optional ellipsis. */
16402 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16404 /* Consume the `...'. */
16405 cp_lexer_consume_token (parser->lexer);
16407 /* Turn the type into a pack expansion expression. */
16408 type = make_pack_expansion (type);
16410 /* Add it to the list. */
16411 types = add_exception_specifier (types, type, /*complain=*/1);
16412 /* Peek at the next token. */
16413 token = cp_lexer_peek_token (parser->lexer);
16414 /* If it is not a `,', we are done. */
16415 if (token->type != CPP_COMMA)
16417 /* Consume the `,'. */
16418 cp_lexer_consume_token (parser->lexer);
16421 return nreverse (types);
16424 /* Parse a try-block.
16427 try compound-statement handler-seq */
16430 cp_parser_try_block (cp_parser* parser)
16434 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16435 try_block = begin_try_block ();
16436 cp_parser_compound_statement (parser, NULL, true);
16437 finish_try_block (try_block);
16438 cp_parser_handler_seq (parser);
16439 finish_handler_sequence (try_block);
16444 /* Parse a function-try-block.
16446 function-try-block:
16447 try ctor-initializer [opt] function-body handler-seq */
16450 cp_parser_function_try_block (cp_parser* parser)
16452 tree compound_stmt;
16454 bool ctor_initializer_p;
16456 /* Look for the `try' keyword. */
16457 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16459 /* Let the rest of the front end know where we are. */
16460 try_block = begin_function_try_block (&compound_stmt);
16461 /* Parse the function-body. */
16463 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16464 /* We're done with the `try' part. */
16465 finish_function_try_block (try_block);
16466 /* Parse the handlers. */
16467 cp_parser_handler_seq (parser);
16468 /* We're done with the handlers. */
16469 finish_function_handler_sequence (try_block, compound_stmt);
16471 return ctor_initializer_p;
16474 /* Parse a handler-seq.
16477 handler handler-seq [opt] */
16480 cp_parser_handler_seq (cp_parser* parser)
16486 /* Parse the handler. */
16487 cp_parser_handler (parser);
16488 /* Peek at the next token. */
16489 token = cp_lexer_peek_token (parser->lexer);
16490 /* If it's not `catch' then there are no more handlers. */
16491 if (!cp_parser_is_keyword (token, RID_CATCH))
16496 /* Parse a handler.
16499 catch ( exception-declaration ) compound-statement */
16502 cp_parser_handler (cp_parser* parser)
16507 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16508 handler = begin_handler ();
16509 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16510 declaration = cp_parser_exception_declaration (parser);
16511 finish_handler_parms (declaration, handler);
16512 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16513 cp_parser_compound_statement (parser, NULL, false);
16514 finish_handler (handler);
16517 /* Parse an exception-declaration.
16519 exception-declaration:
16520 type-specifier-seq declarator
16521 type-specifier-seq abstract-declarator
16525 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16526 ellipsis variant is used. */
16529 cp_parser_exception_declaration (cp_parser* parser)
16531 cp_decl_specifier_seq type_specifiers;
16532 cp_declarator *declarator;
16533 const char *saved_message;
16535 /* If it's an ellipsis, it's easy to handle. */
16536 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16538 /* Consume the `...' token. */
16539 cp_lexer_consume_token (parser->lexer);
16543 /* Types may not be defined in exception-declarations. */
16544 saved_message = parser->type_definition_forbidden_message;
16545 parser->type_definition_forbidden_message
16546 = "types may not be defined in exception-declarations";
16548 /* Parse the type-specifier-seq. */
16549 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16551 /* If it's a `)', then there is no declarator. */
16552 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16555 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16556 /*ctor_dtor_or_conv_p=*/NULL,
16557 /*parenthesized_p=*/NULL,
16558 /*member_p=*/false);
16560 /* Restore the saved message. */
16561 parser->type_definition_forbidden_message = saved_message;
16563 if (!type_specifiers.any_specifiers_p)
16564 return error_mark_node;
16566 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16569 /* Parse a throw-expression.
16572 throw assignment-expression [opt]
16574 Returns a THROW_EXPR representing the throw-expression. */
16577 cp_parser_throw_expression (cp_parser* parser)
16582 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16583 token = cp_lexer_peek_token (parser->lexer);
16584 /* Figure out whether or not there is an assignment-expression
16585 following the "throw" keyword. */
16586 if (token->type == CPP_COMMA
16587 || token->type == CPP_SEMICOLON
16588 || token->type == CPP_CLOSE_PAREN
16589 || token->type == CPP_CLOSE_SQUARE
16590 || token->type == CPP_CLOSE_BRACE
16591 || token->type == CPP_COLON)
16592 expression = NULL_TREE;
16594 expression = cp_parser_assignment_expression (parser,
16595 /*cast_p=*/false, NULL);
16597 return build_throw (expression);
16600 /* GNU Extensions */
16602 /* Parse an (optional) asm-specification.
16605 asm ( string-literal )
16607 If the asm-specification is present, returns a STRING_CST
16608 corresponding to the string-literal. Otherwise, returns
16612 cp_parser_asm_specification_opt (cp_parser* parser)
16615 tree asm_specification;
16617 /* Peek at the next token. */
16618 token = cp_lexer_peek_token (parser->lexer);
16619 /* If the next token isn't the `asm' keyword, then there's no
16620 asm-specification. */
16621 if (!cp_parser_is_keyword (token, RID_ASM))
16624 /* Consume the `asm' token. */
16625 cp_lexer_consume_token (parser->lexer);
16626 /* Look for the `('. */
16627 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16629 /* Look for the string-literal. */
16630 asm_specification = cp_parser_string_literal (parser, false, false);
16632 /* Look for the `)'. */
16633 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16635 return asm_specification;
16638 /* Parse an asm-operand-list.
16642 asm-operand-list , asm-operand
16645 string-literal ( expression )
16646 [ string-literal ] string-literal ( expression )
16648 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16649 each node is the expression. The TREE_PURPOSE is itself a
16650 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16651 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16652 is a STRING_CST for the string literal before the parenthesis. Returns
16653 ERROR_MARK_NODE if any of the operands are invalid. */
16656 cp_parser_asm_operand_list (cp_parser* parser)
16658 tree asm_operands = NULL_TREE;
16659 bool invalid_operands = false;
16663 tree string_literal;
16667 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16669 /* Consume the `[' token. */
16670 cp_lexer_consume_token (parser->lexer);
16671 /* Read the operand name. */
16672 name = cp_parser_identifier (parser);
16673 if (name != error_mark_node)
16674 name = build_string (IDENTIFIER_LENGTH (name),
16675 IDENTIFIER_POINTER (name));
16676 /* Look for the closing `]'. */
16677 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16681 /* Look for the string-literal. */
16682 string_literal = cp_parser_string_literal (parser, false, false);
16684 /* Look for the `('. */
16685 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16686 /* Parse the expression. */
16687 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16688 /* Look for the `)'. */
16689 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16691 if (name == error_mark_node
16692 || string_literal == error_mark_node
16693 || expression == error_mark_node)
16694 invalid_operands = true;
16696 /* Add this operand to the list. */
16697 asm_operands = tree_cons (build_tree_list (name, string_literal),
16700 /* If the next token is not a `,', there are no more
16702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16704 /* Consume the `,'. */
16705 cp_lexer_consume_token (parser->lexer);
16708 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16711 /* Parse an asm-clobber-list.
16715 asm-clobber-list , string-literal
16717 Returns a TREE_LIST, indicating the clobbers in the order that they
16718 appeared. The TREE_VALUE of each node is a STRING_CST. */
16721 cp_parser_asm_clobber_list (cp_parser* parser)
16723 tree clobbers = NULL_TREE;
16727 tree string_literal;
16729 /* Look for the string literal. */
16730 string_literal = cp_parser_string_literal (parser, false, false);
16731 /* Add it to the list. */
16732 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16733 /* If the next token is not a `,', then the list is
16735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16737 /* Consume the `,' token. */
16738 cp_lexer_consume_token (parser->lexer);
16744 /* Parse an (optional) series of attributes.
16747 attributes attribute
16750 __attribute__ (( attribute-list [opt] ))
16752 The return value is as for cp_parser_attribute_list. */
16755 cp_parser_attributes_opt (cp_parser* parser)
16757 tree attributes = NULL_TREE;
16762 tree attribute_list;
16764 /* Peek at the next token. */
16765 token = cp_lexer_peek_token (parser->lexer);
16766 /* If it's not `__attribute__', then we're done. */
16767 if (token->keyword != RID_ATTRIBUTE)
16770 /* Consume the `__attribute__' keyword. */
16771 cp_lexer_consume_token (parser->lexer);
16772 /* Look for the two `(' tokens. */
16773 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16774 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16776 /* Peek at the next token. */
16777 token = cp_lexer_peek_token (parser->lexer);
16778 if (token->type != CPP_CLOSE_PAREN)
16779 /* Parse the attribute-list. */
16780 attribute_list = cp_parser_attribute_list (parser);
16782 /* If the next token is a `)', then there is no attribute
16784 attribute_list = NULL;
16786 /* Look for the two `)' tokens. */
16787 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16788 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16790 /* Add these new attributes to the list. */
16791 attributes = chainon (attributes, attribute_list);
16797 /* Parse an attribute-list.
16801 attribute-list , attribute
16805 identifier ( identifier )
16806 identifier ( identifier , expression-list )
16807 identifier ( expression-list )
16809 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16810 to an attribute. The TREE_PURPOSE of each node is the identifier
16811 indicating which attribute is in use. The TREE_VALUE represents
16812 the arguments, if any. */
16815 cp_parser_attribute_list (cp_parser* parser)
16817 tree attribute_list = NULL_TREE;
16818 bool save_translate_strings_p = parser->translate_strings_p;
16820 parser->translate_strings_p = false;
16827 /* Look for the identifier. We also allow keywords here; for
16828 example `__attribute__ ((const))' is legal. */
16829 token = cp_lexer_peek_token (parser->lexer);
16830 if (token->type == CPP_NAME
16831 || token->type == CPP_KEYWORD)
16833 tree arguments = NULL_TREE;
16835 /* Consume the token. */
16836 token = cp_lexer_consume_token (parser->lexer);
16838 /* Save away the identifier that indicates which attribute
16840 identifier = (token->type == CPP_KEYWORD)
16841 /* For keywords, use the canonical spelling, not the
16842 parsed identifier. */
16843 ? ridpointers[(int) token->keyword]
16846 attribute = build_tree_list (identifier, NULL_TREE);
16848 /* Peek at the next token. */
16849 token = cp_lexer_peek_token (parser->lexer);
16850 /* If it's an `(', then parse the attribute arguments. */
16851 if (token->type == CPP_OPEN_PAREN)
16853 arguments = cp_parser_parenthesized_expression_list
16854 (parser, true, /*cast_p=*/false,
16855 /*allow_expansion_p=*/false,
16856 /*non_constant_p=*/NULL);
16857 /* Save the arguments away. */
16858 TREE_VALUE (attribute) = arguments;
16861 if (arguments != error_mark_node)
16863 /* Add this attribute to the list. */
16864 TREE_CHAIN (attribute) = attribute_list;
16865 attribute_list = attribute;
16868 token = cp_lexer_peek_token (parser->lexer);
16870 /* Now, look for more attributes. If the next token isn't a
16871 `,', we're done. */
16872 if (token->type != CPP_COMMA)
16875 /* Consume the comma and keep going. */
16876 cp_lexer_consume_token (parser->lexer);
16878 parser->translate_strings_p = save_translate_strings_p;
16880 /* We built up the list in reverse order. */
16881 return nreverse (attribute_list);
16884 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16885 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16886 current value of the PEDANTIC flag, regardless of whether or not
16887 the `__extension__' keyword is present. The caller is responsible
16888 for restoring the value of the PEDANTIC flag. */
16891 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16893 /* Save the old value of the PEDANTIC flag. */
16894 *saved_pedantic = pedantic;
16896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16898 /* Consume the `__extension__' token. */
16899 cp_lexer_consume_token (parser->lexer);
16900 /* We're not being pedantic while the `__extension__' keyword is
16910 /* Parse a label declaration.
16913 __label__ label-declarator-seq ;
16915 label-declarator-seq:
16916 identifier , label-declarator-seq
16920 cp_parser_label_declaration (cp_parser* parser)
16922 /* Look for the `__label__' keyword. */
16923 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16929 /* Look for an identifier. */
16930 identifier = cp_parser_identifier (parser);
16931 /* If we failed, stop. */
16932 if (identifier == error_mark_node)
16934 /* Declare it as a label. */
16935 finish_label_decl (identifier);
16936 /* If the next token is a `;', stop. */
16937 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16939 /* Look for the `,' separating the label declarations. */
16940 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16943 /* Look for the final `;'. */
16944 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16947 /* Support Functions */
16949 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16950 NAME should have one of the representations used for an
16951 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16952 is returned. If PARSER->SCOPE is a dependent type, then a
16953 SCOPE_REF is returned.
16955 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16956 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16957 was formed. Abstractly, such entities should not be passed to this
16958 function, because they do not need to be looked up, but it is
16959 simpler to check for this special case here, rather than at the
16962 In cases not explicitly covered above, this function returns a
16963 DECL, OVERLOAD, or baselink representing the result of the lookup.
16964 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16967 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16968 (e.g., "struct") that was used. In that case bindings that do not
16969 refer to types are ignored.
16971 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16974 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16977 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16980 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16981 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16982 NULL_TREE otherwise. */
16985 cp_parser_lookup_name (cp_parser *parser, tree name,
16986 enum tag_types tag_type,
16989 bool check_dependency,
16990 tree *ambiguous_decls,
16991 location_t name_location)
16995 tree object_type = parser->context->object_type;
16997 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16998 flags |= LOOKUP_COMPLAIN;
17000 /* Assume that the lookup will be unambiguous. */
17001 if (ambiguous_decls)
17002 *ambiguous_decls = NULL_TREE;
17004 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17005 no longer valid. Note that if we are parsing tentatively, and
17006 the parse fails, OBJECT_TYPE will be automatically restored. */
17007 parser->context->object_type = NULL_TREE;
17009 if (name == error_mark_node)
17010 return error_mark_node;
17012 /* A template-id has already been resolved; there is no lookup to
17014 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17016 if (BASELINK_P (name))
17018 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17019 == TEMPLATE_ID_EXPR);
17023 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17024 it should already have been checked to make sure that the name
17025 used matches the type being destroyed. */
17026 if (TREE_CODE (name) == BIT_NOT_EXPR)
17030 /* Figure out to which type this destructor applies. */
17032 type = parser->scope;
17033 else if (object_type)
17034 type = object_type;
17036 type = current_class_type;
17037 /* If that's not a class type, there is no destructor. */
17038 if (!type || !CLASS_TYPE_P (type))
17039 return error_mark_node;
17040 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17041 lazily_declare_fn (sfk_destructor, type);
17042 if (!CLASSTYPE_DESTRUCTORS (type))
17043 return error_mark_node;
17044 /* If it was a class type, return the destructor. */
17045 return CLASSTYPE_DESTRUCTORS (type);
17048 /* By this point, the NAME should be an ordinary identifier. If
17049 the id-expression was a qualified name, the qualifying scope is
17050 stored in PARSER->SCOPE at this point. */
17051 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17053 /* Perform the lookup. */
17058 if (parser->scope == error_mark_node)
17059 return error_mark_node;
17061 /* If the SCOPE is dependent, the lookup must be deferred until
17062 the template is instantiated -- unless we are explicitly
17063 looking up names in uninstantiated templates. Even then, we
17064 cannot look up the name if the scope is not a class type; it
17065 might, for example, be a template type parameter. */
17066 dependent_p = (TYPE_P (parser->scope)
17067 && dependent_scope_p (parser->scope));
17068 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17070 /* Defer lookup. */
17071 decl = error_mark_node;
17074 tree pushed_scope = NULL_TREE;
17076 /* If PARSER->SCOPE is a dependent type, then it must be a
17077 class type, and we must not be checking dependencies;
17078 otherwise, we would have processed this lookup above. So
17079 that PARSER->SCOPE is not considered a dependent base by
17080 lookup_member, we must enter the scope here. */
17082 pushed_scope = push_scope (parser->scope);
17083 /* If the PARSER->SCOPE is a template specialization, it
17084 may be instantiated during name lookup. In that case,
17085 errors may be issued. Even if we rollback the current
17086 tentative parse, those errors are valid. */
17087 decl = lookup_qualified_name (parser->scope, name,
17088 tag_type != none_type,
17089 /*complain=*/true);
17091 /* If we have a single function from a using decl, pull it out. */
17092 if (TREE_CODE (decl) == OVERLOAD
17093 && !really_overloaded_fn (decl))
17094 decl = OVL_FUNCTION (decl);
17097 pop_scope (pushed_scope);
17100 /* If the scope is a dependent type and either we deferred lookup or
17101 we did lookup but didn't find the name, rememeber the name. */
17102 if (decl == error_mark_node && TYPE_P (parser->scope)
17103 && dependent_type_p (parser->scope))
17109 /* The resolution to Core Issue 180 says that `struct
17110 A::B' should be considered a type-name, even if `A'
17112 type = make_typename_type (parser->scope, name, tag_type,
17113 /*complain=*/tf_error);
17114 decl = TYPE_NAME (type);
17116 else if (is_template
17117 && (cp_parser_next_token_ends_template_argument_p (parser)
17118 || cp_lexer_next_token_is (parser->lexer,
17120 decl = make_unbound_class_template (parser->scope,
17122 /*complain=*/tf_error);
17124 decl = build_qualified_name (/*type=*/NULL_TREE,
17125 parser->scope, name,
17128 parser->qualifying_scope = parser->scope;
17129 parser->object_scope = NULL_TREE;
17131 else if (object_type)
17133 tree object_decl = NULL_TREE;
17134 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17135 OBJECT_TYPE is not a class. */
17136 if (CLASS_TYPE_P (object_type))
17137 /* If the OBJECT_TYPE is a template specialization, it may
17138 be instantiated during name lookup. In that case, errors
17139 may be issued. Even if we rollback the current tentative
17140 parse, those errors are valid. */
17141 object_decl = lookup_member (object_type,
17144 tag_type != none_type);
17145 /* Look it up in the enclosing context, too. */
17146 decl = lookup_name_real (name, tag_type != none_type,
17148 /*block_p=*/true, is_namespace, flags);
17149 parser->object_scope = object_type;
17150 parser->qualifying_scope = NULL_TREE;
17152 decl = object_decl;
17156 decl = lookup_name_real (name, tag_type != none_type,
17158 /*block_p=*/true, is_namespace, flags);
17159 parser->qualifying_scope = NULL_TREE;
17160 parser->object_scope = NULL_TREE;
17163 /* If the lookup failed, let our caller know. */
17164 if (!decl || decl == error_mark_node)
17165 return error_mark_node;
17167 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17168 if (TREE_CODE (decl) == TREE_LIST)
17170 if (ambiguous_decls)
17171 *ambiguous_decls = decl;
17172 /* The error message we have to print is too complicated for
17173 cp_parser_error, so we incorporate its actions directly. */
17174 if (!cp_parser_simulate_error (parser))
17176 error ("%Hreference to %qD is ambiguous",
17177 &name_location, name);
17178 print_candidates (decl);
17180 return error_mark_node;
17183 gcc_assert (DECL_P (decl)
17184 || TREE_CODE (decl) == OVERLOAD
17185 || TREE_CODE (decl) == SCOPE_REF
17186 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17187 || BASELINK_P (decl));
17189 /* If we have resolved the name of a member declaration, check to
17190 see if the declaration is accessible. When the name resolves to
17191 set of overloaded functions, accessibility is checked when
17192 overload resolution is done.
17194 During an explicit instantiation, access is not checked at all,
17195 as per [temp.explicit]. */
17197 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17202 /* Like cp_parser_lookup_name, but for use in the typical case where
17203 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17204 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17207 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17209 return cp_parser_lookup_name (parser, name,
17211 /*is_template=*/false,
17212 /*is_namespace=*/false,
17213 /*check_dependency=*/true,
17214 /*ambiguous_decls=*/NULL,
17218 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17219 the current context, return the TYPE_DECL. If TAG_NAME_P is
17220 true, the DECL indicates the class being defined in a class-head,
17221 or declared in an elaborated-type-specifier.
17223 Otherwise, return DECL. */
17226 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17228 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17229 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17232 template <typename T> struct B;
17235 template <typename T> struct A::B {};
17237 Similarly, in an elaborated-type-specifier:
17239 namespace N { struct X{}; }
17242 template <typename T> friend struct N::X;
17245 However, if the DECL refers to a class type, and we are in
17246 the scope of the class, then the name lookup automatically
17247 finds the TYPE_DECL created by build_self_reference rather
17248 than a TEMPLATE_DECL. For example, in:
17250 template <class T> struct S {
17254 there is no need to handle such case. */
17256 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17257 return DECL_TEMPLATE_RESULT (decl);
17262 /* If too many, or too few, template-parameter lists apply to the
17263 declarator, issue an error message. Returns TRUE if all went well,
17264 and FALSE otherwise. */
17267 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17268 cp_declarator *declarator,
17269 location_t declarator_location)
17271 unsigned num_templates;
17273 /* We haven't seen any classes that involve template parameters yet. */
17276 switch (declarator->kind)
17279 if (declarator->u.id.qualifying_scope)
17284 scope = declarator->u.id.qualifying_scope;
17285 member = declarator->u.id.unqualified_name;
17287 while (scope && CLASS_TYPE_P (scope))
17289 /* You're supposed to have one `template <...>'
17290 for every template class, but you don't need one
17291 for a full specialization. For example:
17293 template <class T> struct S{};
17294 template <> struct S<int> { void f(); };
17295 void S<int>::f () {}
17297 is correct; there shouldn't be a `template <>' for
17298 the definition of `S<int>::f'. */
17299 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17300 /* If SCOPE does not have template information of any
17301 kind, then it is not a template, nor is it nested
17302 within a template. */
17304 if (explicit_class_specialization_p (scope))
17306 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17309 scope = TYPE_CONTEXT (scope);
17312 else if (TREE_CODE (declarator->u.id.unqualified_name)
17313 == TEMPLATE_ID_EXPR)
17314 /* If the DECLARATOR has the form `X<y>' then it uses one
17315 additional level of template parameters. */
17318 return cp_parser_check_template_parameters
17319 (parser, num_templates, declarator_location, declarator);
17325 case cdk_reference:
17327 return (cp_parser_check_declarator_template_parameters
17328 (parser, declarator->declarator, declarator_location));
17334 gcc_unreachable ();
17339 /* NUM_TEMPLATES were used in the current declaration. If that is
17340 invalid, return FALSE and issue an error messages. Otherwise,
17341 return TRUE. If DECLARATOR is non-NULL, then we are checking a
17342 declarator and we can print more accurate diagnostics. */
17345 cp_parser_check_template_parameters (cp_parser* parser,
17346 unsigned num_templates,
17347 location_t location,
17348 cp_declarator *declarator)
17350 /* If there are the same number of template classes and parameter
17351 lists, that's OK. */
17352 if (parser->num_template_parameter_lists == num_templates)
17354 /* If there are more, but only one more, then we are referring to a
17355 member template. That's OK too. */
17356 if (parser->num_template_parameter_lists == num_templates + 1)
17358 /* If there are more template classes than parameter lists, we have
17361 template <class T> void S<T>::R<T>::f (); */
17362 if (parser->num_template_parameter_lists < num_templates)
17365 error_at (location, "specializing member %<%T::%E%> "
17366 "requires %<template<>%> syntax",
17367 declarator->u.id.qualifying_scope,
17368 declarator->u.id.unqualified_name);
17370 error_at (location, "too few template-parameter-lists");
17373 /* Otherwise, there are too many template parameter lists. We have
17376 template <class T> template <class U> void S::f(); */
17377 error ("%Htoo many template-parameter-lists", &location);
17381 /* Parse an optional `::' token indicating that the following name is
17382 from the global namespace. If so, PARSER->SCOPE is set to the
17383 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17384 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17385 Returns the new value of PARSER->SCOPE, if the `::' token is
17386 present, and NULL_TREE otherwise. */
17389 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17393 /* Peek at the next token. */
17394 token = cp_lexer_peek_token (parser->lexer);
17395 /* If we're looking at a `::' token then we're starting from the
17396 global namespace, not our current location. */
17397 if (token->type == CPP_SCOPE)
17399 /* Consume the `::' token. */
17400 cp_lexer_consume_token (parser->lexer);
17401 /* Set the SCOPE so that we know where to start the lookup. */
17402 parser->scope = global_namespace;
17403 parser->qualifying_scope = global_namespace;
17404 parser->object_scope = NULL_TREE;
17406 return parser->scope;
17408 else if (!current_scope_valid_p)
17410 parser->scope = NULL_TREE;
17411 parser->qualifying_scope = NULL_TREE;
17412 parser->object_scope = NULL_TREE;
17418 /* Returns TRUE if the upcoming token sequence is the start of a
17419 constructor declarator. If FRIEND_P is true, the declarator is
17420 preceded by the `friend' specifier. */
17423 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17425 bool constructor_p;
17426 tree type_decl = NULL_TREE;
17427 bool nested_name_p;
17428 cp_token *next_token;
17430 /* The common case is that this is not a constructor declarator, so
17431 try to avoid doing lots of work if at all possible. It's not
17432 valid declare a constructor at function scope. */
17433 if (parser->in_function_body)
17435 /* And only certain tokens can begin a constructor declarator. */
17436 next_token = cp_lexer_peek_token (parser->lexer);
17437 if (next_token->type != CPP_NAME
17438 && next_token->type != CPP_SCOPE
17439 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17440 && next_token->type != CPP_TEMPLATE_ID)
17443 /* Parse tentatively; we are going to roll back all of the tokens
17445 cp_parser_parse_tentatively (parser);
17446 /* Assume that we are looking at a constructor declarator. */
17447 constructor_p = true;
17449 /* Look for the optional `::' operator. */
17450 cp_parser_global_scope_opt (parser,
17451 /*current_scope_valid_p=*/false);
17452 /* Look for the nested-name-specifier. */
17454 = (cp_parser_nested_name_specifier_opt (parser,
17455 /*typename_keyword_p=*/false,
17456 /*check_dependency_p=*/false,
17458 /*is_declaration=*/false)
17460 /* Outside of a class-specifier, there must be a
17461 nested-name-specifier. */
17462 if (!nested_name_p &&
17463 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17465 constructor_p = false;
17466 /* If we still think that this might be a constructor-declarator,
17467 look for a class-name. */
17472 template <typename T> struct S { S(); };
17473 template <typename T> S<T>::S ();
17475 we must recognize that the nested `S' names a class.
17478 template <typename T> S<T>::S<T> ();
17480 we must recognize that the nested `S' names a template. */
17481 type_decl = cp_parser_class_name (parser,
17482 /*typename_keyword_p=*/false,
17483 /*template_keyword_p=*/false,
17485 /*check_dependency_p=*/false,
17486 /*class_head_p=*/false,
17487 /*is_declaration=*/false);
17488 /* If there was no class-name, then this is not a constructor. */
17489 constructor_p = !cp_parser_error_occurred (parser);
17492 /* If we're still considering a constructor, we have to see a `(',
17493 to begin the parameter-declaration-clause, followed by either a
17494 `)', an `...', or a decl-specifier. We need to check for a
17495 type-specifier to avoid being fooled into thinking that:
17499 is a constructor. (It is actually a function named `f' that
17500 takes one parameter (of type `int') and returns a value of type
17503 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17506 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17507 /* A parameter declaration begins with a decl-specifier,
17508 which is either the "attribute" keyword, a storage class
17509 specifier, or (usually) a type-specifier. */
17510 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17513 tree pushed_scope = NULL_TREE;
17514 unsigned saved_num_template_parameter_lists;
17516 /* Names appearing in the type-specifier should be looked up
17517 in the scope of the class. */
17518 if (current_class_type)
17522 type = TREE_TYPE (type_decl);
17523 if (TREE_CODE (type) == TYPENAME_TYPE)
17525 type = resolve_typename_type (type,
17526 /*only_current_p=*/false);
17527 if (TREE_CODE (type) == TYPENAME_TYPE)
17529 cp_parser_abort_tentative_parse (parser);
17533 pushed_scope = push_scope (type);
17536 /* Inside the constructor parameter list, surrounding
17537 template-parameter-lists do not apply. */
17538 saved_num_template_parameter_lists
17539 = parser->num_template_parameter_lists;
17540 parser->num_template_parameter_lists = 0;
17542 /* Look for the type-specifier. */
17543 cp_parser_type_specifier (parser,
17544 CP_PARSER_FLAGS_NONE,
17545 /*decl_specs=*/NULL,
17546 /*is_declarator=*/true,
17547 /*declares_class_or_enum=*/NULL,
17548 /*is_cv_qualifier=*/NULL);
17550 parser->num_template_parameter_lists
17551 = saved_num_template_parameter_lists;
17553 /* Leave the scope of the class. */
17555 pop_scope (pushed_scope);
17557 constructor_p = !cp_parser_error_occurred (parser);
17561 constructor_p = false;
17562 /* We did not really want to consume any tokens. */
17563 cp_parser_abort_tentative_parse (parser);
17565 return constructor_p;
17568 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17569 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17570 they must be performed once we are in the scope of the function.
17572 Returns the function defined. */
17575 cp_parser_function_definition_from_specifiers_and_declarator
17576 (cp_parser* parser,
17577 cp_decl_specifier_seq *decl_specifiers,
17579 const cp_declarator *declarator)
17584 /* Begin the function-definition. */
17585 success_p = start_function (decl_specifiers, declarator, attributes);
17587 /* The things we're about to see are not directly qualified by any
17588 template headers we've seen thus far. */
17589 reset_specialization ();
17591 /* If there were names looked up in the decl-specifier-seq that we
17592 did not check, check them now. We must wait until we are in the
17593 scope of the function to perform the checks, since the function
17594 might be a friend. */
17595 perform_deferred_access_checks ();
17599 /* Skip the entire function. */
17600 cp_parser_skip_to_end_of_block_or_statement (parser);
17601 fn = error_mark_node;
17603 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17605 /* Seen already, skip it. An error message has already been output. */
17606 cp_parser_skip_to_end_of_block_or_statement (parser);
17607 fn = current_function_decl;
17608 current_function_decl = NULL_TREE;
17609 /* If this is a function from a class, pop the nested class. */
17610 if (current_class_name)
17611 pop_nested_class ();
17614 fn = cp_parser_function_definition_after_declarator (parser,
17615 /*inline_p=*/false);
17620 /* Parse the part of a function-definition that follows the
17621 declarator. INLINE_P is TRUE iff this function is an inline
17622 function defined with a class-specifier.
17624 Returns the function defined. */
17627 cp_parser_function_definition_after_declarator (cp_parser* parser,
17631 bool ctor_initializer_p = false;
17632 bool saved_in_unbraced_linkage_specification_p;
17633 bool saved_in_function_body;
17634 unsigned saved_num_template_parameter_lists;
17637 saved_in_function_body = parser->in_function_body;
17638 parser->in_function_body = true;
17639 /* If the next token is `return', then the code may be trying to
17640 make use of the "named return value" extension that G++ used to
17642 token = cp_lexer_peek_token (parser->lexer);
17643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17645 /* Consume the `return' keyword. */
17646 cp_lexer_consume_token (parser->lexer);
17647 /* Look for the identifier that indicates what value is to be
17649 cp_parser_identifier (parser);
17650 /* Issue an error message. */
17651 error ("%Hnamed return values are no longer supported",
17653 /* Skip tokens until we reach the start of the function body. */
17656 cp_token *token = cp_lexer_peek_token (parser->lexer);
17657 if (token->type == CPP_OPEN_BRACE
17658 || token->type == CPP_EOF
17659 || token->type == CPP_PRAGMA_EOL)
17661 cp_lexer_consume_token (parser->lexer);
17664 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17665 anything declared inside `f'. */
17666 saved_in_unbraced_linkage_specification_p
17667 = parser->in_unbraced_linkage_specification_p;
17668 parser->in_unbraced_linkage_specification_p = false;
17669 /* Inside the function, surrounding template-parameter-lists do not
17671 saved_num_template_parameter_lists
17672 = parser->num_template_parameter_lists;
17673 parser->num_template_parameter_lists = 0;
17674 /* If the next token is `try', then we are looking at a
17675 function-try-block. */
17676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17677 ctor_initializer_p = cp_parser_function_try_block (parser);
17678 /* A function-try-block includes the function-body, so we only do
17679 this next part if we're not processing a function-try-block. */
17682 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17684 /* Finish the function. */
17685 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17686 (inline_p ? 2 : 0));
17687 /* Generate code for it, if necessary. */
17688 expand_or_defer_fn (fn);
17689 /* Restore the saved values. */
17690 parser->in_unbraced_linkage_specification_p
17691 = saved_in_unbraced_linkage_specification_p;
17692 parser->num_template_parameter_lists
17693 = saved_num_template_parameter_lists;
17694 parser->in_function_body = saved_in_function_body;
17699 /* Parse a template-declaration, assuming that the `export' (and
17700 `extern') keywords, if present, has already been scanned. MEMBER_P
17701 is as for cp_parser_template_declaration. */
17704 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17706 tree decl = NULL_TREE;
17707 VEC (deferred_access_check,gc) *checks;
17708 tree parameter_list;
17709 bool friend_p = false;
17710 bool need_lang_pop;
17713 /* Look for the `template' keyword. */
17714 token = cp_lexer_peek_token (parser->lexer);
17715 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17719 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17721 if (at_class_scope_p () && current_function_decl)
17723 /* 14.5.2.2 [temp.mem]
17725 A local class shall not have member templates. */
17726 error ("%Hinvalid declaration of member template in local class",
17728 cp_parser_skip_to_end_of_block_or_statement (parser);
17733 A template ... shall not have C linkage. */
17734 if (current_lang_name == lang_name_c)
17736 error ("%Htemplate with C linkage", &token->location);
17737 /* Give it C++ linkage to avoid confusing other parts of the
17739 push_lang_context (lang_name_cplusplus);
17740 need_lang_pop = true;
17743 need_lang_pop = false;
17745 /* We cannot perform access checks on the template parameter
17746 declarations until we know what is being declared, just as we
17747 cannot check the decl-specifier list. */
17748 push_deferring_access_checks (dk_deferred);
17750 /* If the next token is `>', then we have an invalid
17751 specialization. Rather than complain about an invalid template
17752 parameter, issue an error message here. */
17753 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17755 cp_parser_error (parser, "invalid explicit specialization");
17756 begin_specialization ();
17757 parameter_list = NULL_TREE;
17760 /* Parse the template parameters. */
17761 parameter_list = cp_parser_template_parameter_list (parser);
17763 /* Get the deferred access checks from the parameter list. These
17764 will be checked once we know what is being declared, as for a
17765 member template the checks must be performed in the scope of the
17766 class containing the member. */
17767 checks = get_deferred_access_checks ();
17769 /* Look for the `>'. */
17770 cp_parser_skip_to_end_of_template_parameter_list (parser);
17771 /* We just processed one more parameter list. */
17772 ++parser->num_template_parameter_lists;
17773 /* If the next token is `template', there are more template
17775 if (cp_lexer_next_token_is_keyword (parser->lexer,
17777 cp_parser_template_declaration_after_export (parser, member_p);
17780 /* There are no access checks when parsing a template, as we do not
17781 know if a specialization will be a friend. */
17782 push_deferring_access_checks (dk_no_check);
17783 token = cp_lexer_peek_token (parser->lexer);
17784 decl = cp_parser_single_declaration (parser,
17787 /*explicit_specialization_p=*/false,
17789 pop_deferring_access_checks ();
17791 /* If this is a member template declaration, let the front
17793 if (member_p && !friend_p && decl)
17795 if (TREE_CODE (decl) == TYPE_DECL)
17796 cp_parser_check_access_in_redeclaration (decl, token->location);
17798 decl = finish_member_template_decl (decl);
17800 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17801 make_friend_class (current_class_type, TREE_TYPE (decl),
17802 /*complain=*/true);
17804 /* We are done with the current parameter list. */
17805 --parser->num_template_parameter_lists;
17807 pop_deferring_access_checks ();
17810 finish_template_decl (parameter_list);
17812 /* Register member declarations. */
17813 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17814 finish_member_declaration (decl);
17815 /* For the erroneous case of a template with C linkage, we pushed an
17816 implicit C++ linkage scope; exit that scope now. */
17818 pop_lang_context ();
17819 /* If DECL is a function template, we must return to parse it later.
17820 (Even though there is no definition, there might be default
17821 arguments that need handling.) */
17822 if (member_p && decl
17823 && (TREE_CODE (decl) == FUNCTION_DECL
17824 || DECL_FUNCTION_TEMPLATE_P (decl)))
17825 TREE_VALUE (parser->unparsed_functions_queues)
17826 = tree_cons (NULL_TREE, decl,
17827 TREE_VALUE (parser->unparsed_functions_queues));
17830 /* Perform the deferred access checks from a template-parameter-list.
17831 CHECKS is a TREE_LIST of access checks, as returned by
17832 get_deferred_access_checks. */
17835 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17837 ++processing_template_parmlist;
17838 perform_access_checks (checks);
17839 --processing_template_parmlist;
17842 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17843 `function-definition' sequence. MEMBER_P is true, this declaration
17844 appears in a class scope.
17846 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17847 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17850 cp_parser_single_declaration (cp_parser* parser,
17851 VEC (deferred_access_check,gc)* checks,
17853 bool explicit_specialization_p,
17856 int declares_class_or_enum;
17857 tree decl = NULL_TREE;
17858 cp_decl_specifier_seq decl_specifiers;
17859 bool function_definition_p = false;
17860 cp_token *decl_spec_token_start;
17862 /* This function is only used when processing a template
17864 gcc_assert (innermost_scope_kind () == sk_template_parms
17865 || innermost_scope_kind () == sk_template_spec);
17867 /* Defer access checks until we know what is being declared. */
17868 push_deferring_access_checks (dk_deferred);
17870 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17872 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17873 cp_parser_decl_specifier_seq (parser,
17874 CP_PARSER_FLAGS_OPTIONAL,
17876 &declares_class_or_enum);
17878 *friend_p = cp_parser_friend_p (&decl_specifiers);
17880 /* There are no template typedefs. */
17881 if (decl_specifiers.specs[(int) ds_typedef])
17883 error ("%Htemplate declaration of %qs",
17884 &decl_spec_token_start->location, "typedef");
17885 decl = error_mark_node;
17888 /* Gather up the access checks that occurred the
17889 decl-specifier-seq. */
17890 stop_deferring_access_checks ();
17892 /* Check for the declaration of a template class. */
17893 if (declares_class_or_enum)
17895 if (cp_parser_declares_only_class_p (parser))
17897 decl = shadow_tag (&decl_specifiers);
17902 friend template <typename T> struct A<T>::B;
17905 A<T>::B will be represented by a TYPENAME_TYPE, and
17906 therefore not recognized by shadow_tag. */
17907 if (friend_p && *friend_p
17909 && decl_specifiers.type
17910 && TYPE_P (decl_specifiers.type))
17911 decl = decl_specifiers.type;
17913 if (decl && decl != error_mark_node)
17914 decl = TYPE_NAME (decl);
17916 decl = error_mark_node;
17918 /* Perform access checks for template parameters. */
17919 cp_parser_perform_template_parameter_access_checks (checks);
17922 /* If it's not a template class, try for a template function. If
17923 the next token is a `;', then this declaration does not declare
17924 anything. But, if there were errors in the decl-specifiers, then
17925 the error might well have come from an attempted class-specifier.
17926 In that case, there's no need to warn about a missing declarator. */
17928 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17929 || decl_specifiers.type != error_mark_node))
17931 decl = cp_parser_init_declarator (parser,
17934 /*function_definition_allowed_p=*/true,
17936 declares_class_or_enum,
17937 &function_definition_p);
17939 /* 7.1.1-1 [dcl.stc]
17941 A storage-class-specifier shall not be specified in an explicit
17942 specialization... */
17944 && explicit_specialization_p
17945 && decl_specifiers.storage_class != sc_none)
17947 error ("%Hexplicit template specialization cannot have a storage class",
17948 &decl_spec_token_start->location);
17949 decl = error_mark_node;
17953 pop_deferring_access_checks ();
17955 /* Clear any current qualification; whatever comes next is the start
17956 of something new. */
17957 parser->scope = NULL_TREE;
17958 parser->qualifying_scope = NULL_TREE;
17959 parser->object_scope = NULL_TREE;
17960 /* Look for a trailing `;' after the declaration. */
17961 if (!function_definition_p
17962 && (decl == error_mark_node
17963 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17964 cp_parser_skip_to_end_of_block_or_statement (parser);
17969 /* Parse a cast-expression that is not the operand of a unary "&". */
17972 cp_parser_simple_cast_expression (cp_parser *parser)
17974 return cp_parser_cast_expression (parser, /*address_p=*/false,
17975 /*cast_p=*/false, NULL);
17978 /* Parse a functional cast to TYPE. Returns an expression
17979 representing the cast. */
17982 cp_parser_functional_cast (cp_parser* parser, tree type)
17984 tree expression_list;
17988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17990 maybe_warn_cpp0x ("extended initializer lists");
17991 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17992 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17993 if (TREE_CODE (type) == TYPE_DECL)
17994 type = TREE_TYPE (type);
17995 return finish_compound_literal (type, expression_list);
17999 = cp_parser_parenthesized_expression_list (parser, false,
18001 /*allow_expansion_p=*/true,
18002 /*non_constant_p=*/NULL);
18004 cast = build_functional_cast (type, expression_list,
18005 tf_warning_or_error);
18006 /* [expr.const]/1: In an integral constant expression "only type
18007 conversions to integral or enumeration type can be used". */
18008 if (TREE_CODE (type) == TYPE_DECL)
18009 type = TREE_TYPE (type);
18010 if (cast != error_mark_node
18011 && !cast_valid_in_integral_constant_expression_p (type)
18012 && (cp_parser_non_integral_constant_expression
18013 (parser, "a call to a constructor")))
18014 return error_mark_node;
18018 /* Save the tokens that make up the body of a member function defined
18019 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18020 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18021 specifiers applied to the declaration. Returns the FUNCTION_DECL
18022 for the member function. */
18025 cp_parser_save_member_function_body (cp_parser* parser,
18026 cp_decl_specifier_seq *decl_specifiers,
18027 cp_declarator *declarator,
18034 /* Create the function-declaration. */
18035 fn = start_method (decl_specifiers, declarator, attributes);
18036 /* If something went badly wrong, bail out now. */
18037 if (fn == error_mark_node)
18039 /* If there's a function-body, skip it. */
18040 if (cp_parser_token_starts_function_definition_p
18041 (cp_lexer_peek_token (parser->lexer)))
18042 cp_parser_skip_to_end_of_block_or_statement (parser);
18043 return error_mark_node;
18046 /* Remember it, if there default args to post process. */
18047 cp_parser_save_default_args (parser, fn);
18049 /* Save away the tokens that make up the body of the
18051 first = parser->lexer->next_token;
18052 /* We can have braced-init-list mem-initializers before the fn body. */
18053 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18055 cp_lexer_consume_token (parser->lexer);
18056 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18057 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18059 /* cache_group will stop after an un-nested { } pair, too. */
18060 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18063 /* variadic mem-inits have ... after the ')'. */
18064 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18065 cp_lexer_consume_token (parser->lexer);
18068 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18069 /* Handle function try blocks. */
18070 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18071 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18072 last = parser->lexer->next_token;
18074 /* Save away the inline definition; we will process it when the
18075 class is complete. */
18076 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18077 DECL_PENDING_INLINE_P (fn) = 1;
18079 /* We need to know that this was defined in the class, so that
18080 friend templates are handled correctly. */
18081 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18083 /* We're done with the inline definition. */
18084 finish_method (fn);
18086 /* Add FN to the queue of functions to be parsed later. */
18087 TREE_VALUE (parser->unparsed_functions_queues)
18088 = tree_cons (NULL_TREE, fn,
18089 TREE_VALUE (parser->unparsed_functions_queues));
18094 /* Parse a template-argument-list, as well as the trailing ">" (but
18095 not the opening ">"). See cp_parser_template_argument_list for the
18099 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18103 tree saved_qualifying_scope;
18104 tree saved_object_scope;
18105 bool saved_greater_than_is_operator_p;
18106 bool saved_skip_evaluation;
18110 When parsing a template-id, the first non-nested `>' is taken as
18111 the end of the template-argument-list rather than a greater-than
18113 saved_greater_than_is_operator_p
18114 = parser->greater_than_is_operator_p;
18115 parser->greater_than_is_operator_p = false;
18116 /* Parsing the argument list may modify SCOPE, so we save it
18118 saved_scope = parser->scope;
18119 saved_qualifying_scope = parser->qualifying_scope;
18120 saved_object_scope = parser->object_scope;
18121 /* We need to evaluate the template arguments, even though this
18122 template-id may be nested within a "sizeof". */
18123 saved_skip_evaluation = skip_evaluation;
18124 skip_evaluation = false;
18125 /* Parse the template-argument-list itself. */
18126 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18127 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18128 arguments = NULL_TREE;
18130 arguments = cp_parser_template_argument_list (parser);
18131 /* Look for the `>' that ends the template-argument-list. If we find
18132 a '>>' instead, it's probably just a typo. */
18133 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18135 if (cxx_dialect != cxx98)
18137 /* In C++0x, a `>>' in a template argument list or cast
18138 expression is considered to be two separate `>'
18139 tokens. So, change the current token to a `>', but don't
18140 consume it: it will be consumed later when the outer
18141 template argument list (or cast expression) is parsed.
18142 Note that this replacement of `>' for `>>' is necessary
18143 even if we are parsing tentatively: in the tentative
18144 case, after calling
18145 cp_parser_enclosed_template_argument_list we will always
18146 throw away all of the template arguments and the first
18147 closing `>', either because the template argument list
18148 was erroneous or because we are replacing those tokens
18149 with a CPP_TEMPLATE_ID token. The second `>' (which will
18150 not have been thrown away) is needed either to close an
18151 outer template argument list or to complete a new-style
18153 cp_token *token = cp_lexer_peek_token (parser->lexer);
18154 token->type = CPP_GREATER;
18156 else if (!saved_greater_than_is_operator_p)
18158 /* If we're in a nested template argument list, the '>>' has
18159 to be a typo for '> >'. We emit the error message, but we
18160 continue parsing and we push a '>' as next token, so that
18161 the argument list will be parsed correctly. Note that the
18162 global source location is still on the token before the
18163 '>>', so we need to say explicitly where we want it. */
18164 cp_token *token = cp_lexer_peek_token (parser->lexer);
18165 error ("%H%<>>%> should be %<> >%> "
18166 "within a nested template argument list",
18169 token->type = CPP_GREATER;
18173 /* If this is not a nested template argument list, the '>>'
18174 is a typo for '>'. Emit an error message and continue.
18175 Same deal about the token location, but here we can get it
18176 right by consuming the '>>' before issuing the diagnostic. */
18177 cp_token *token = cp_lexer_consume_token (parser->lexer);
18178 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18179 "a template argument list", &token->location);
18183 cp_parser_skip_to_end_of_template_parameter_list (parser);
18184 /* The `>' token might be a greater-than operator again now. */
18185 parser->greater_than_is_operator_p
18186 = saved_greater_than_is_operator_p;
18187 /* Restore the SAVED_SCOPE. */
18188 parser->scope = saved_scope;
18189 parser->qualifying_scope = saved_qualifying_scope;
18190 parser->object_scope = saved_object_scope;
18191 skip_evaluation = saved_skip_evaluation;
18196 /* MEMBER_FUNCTION is a member function, or a friend. If default
18197 arguments, or the body of the function have not yet been parsed,
18201 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18203 /* If this member is a template, get the underlying
18205 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18206 member_function = DECL_TEMPLATE_RESULT (member_function);
18208 /* There should not be any class definitions in progress at this
18209 point; the bodies of members are only parsed outside of all class
18211 gcc_assert (parser->num_classes_being_defined == 0);
18212 /* While we're parsing the member functions we might encounter more
18213 classes. We want to handle them right away, but we don't want
18214 them getting mixed up with functions that are currently in the
18216 parser->unparsed_functions_queues
18217 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18219 /* Make sure that any template parameters are in scope. */
18220 maybe_begin_member_template_processing (member_function);
18222 /* If the body of the function has not yet been parsed, parse it
18224 if (DECL_PENDING_INLINE_P (member_function))
18226 tree function_scope;
18227 cp_token_cache *tokens;
18229 /* The function is no longer pending; we are processing it. */
18230 tokens = DECL_PENDING_INLINE_INFO (member_function);
18231 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18232 DECL_PENDING_INLINE_P (member_function) = 0;
18234 /* If this is a local class, enter the scope of the containing
18236 function_scope = current_function_decl;
18237 if (function_scope)
18238 push_function_context ();
18240 /* Push the body of the function onto the lexer stack. */
18241 cp_parser_push_lexer_for_tokens (parser, tokens);
18243 /* Let the front end know that we going to be defining this
18245 start_preparsed_function (member_function, NULL_TREE,
18246 SF_PRE_PARSED | SF_INCLASS_INLINE);
18248 /* Don't do access checking if it is a templated function. */
18249 if (processing_template_decl)
18250 push_deferring_access_checks (dk_no_check);
18252 /* Now, parse the body of the function. */
18253 cp_parser_function_definition_after_declarator (parser,
18254 /*inline_p=*/true);
18256 if (processing_template_decl)
18257 pop_deferring_access_checks ();
18259 /* Leave the scope of the containing function. */
18260 if (function_scope)
18261 pop_function_context ();
18262 cp_parser_pop_lexer (parser);
18265 /* Remove any template parameters from the symbol table. */
18266 maybe_end_member_template_processing ();
18268 /* Restore the queue. */
18269 parser->unparsed_functions_queues
18270 = TREE_CHAIN (parser->unparsed_functions_queues);
18273 /* If DECL contains any default args, remember it on the unparsed
18274 functions queue. */
18277 cp_parser_save_default_args (cp_parser* parser, tree decl)
18281 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18283 probe = TREE_CHAIN (probe))
18284 if (TREE_PURPOSE (probe))
18286 TREE_PURPOSE (parser->unparsed_functions_queues)
18287 = tree_cons (current_class_type, decl,
18288 TREE_PURPOSE (parser->unparsed_functions_queues));
18293 /* FN is a FUNCTION_DECL which may contains a parameter with an
18294 unparsed DEFAULT_ARG. Parse the default args now. This function
18295 assumes that the current scope is the scope in which the default
18296 argument should be processed. */
18299 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18301 bool saved_local_variables_forbidden_p;
18304 /* While we're parsing the default args, we might (due to the
18305 statement expression extension) encounter more classes. We want
18306 to handle them right away, but we don't want them getting mixed
18307 up with default args that are currently in the queue. */
18308 parser->unparsed_functions_queues
18309 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18311 /* Local variable names (and the `this' keyword) may not appear
18312 in a default argument. */
18313 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18314 parser->local_variables_forbidden_p = true;
18316 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18318 parm = TREE_CHAIN (parm))
18320 cp_token_cache *tokens;
18321 tree default_arg = TREE_PURPOSE (parm);
18323 VEC(tree,gc) *insts;
18330 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18331 /* This can happen for a friend declaration for a function
18332 already declared with default arguments. */
18335 /* Push the saved tokens for the default argument onto the parser's
18337 tokens = DEFARG_TOKENS (default_arg);
18338 cp_parser_push_lexer_for_tokens (parser, tokens);
18340 /* Parse the assignment-expression. */
18341 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18342 if (parsed_arg == error_mark_node)
18344 cp_parser_pop_lexer (parser);
18348 if (!processing_template_decl)
18349 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18351 TREE_PURPOSE (parm) = parsed_arg;
18353 /* Update any instantiations we've already created. */
18354 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18355 VEC_iterate (tree, insts, ix, copy); ix++)
18356 TREE_PURPOSE (copy) = parsed_arg;
18358 /* If the token stream has not been completely used up, then
18359 there was extra junk after the end of the default
18361 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18362 cp_parser_error (parser, "expected %<,%>");
18364 /* Revert to the main lexer. */
18365 cp_parser_pop_lexer (parser);
18368 /* Make sure no default arg is missing. */
18369 check_default_args (fn);
18371 /* Restore the state of local_variables_forbidden_p. */
18372 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18374 /* Restore the queue. */
18375 parser->unparsed_functions_queues
18376 = TREE_CHAIN (parser->unparsed_functions_queues);
18379 /* Parse the operand of `sizeof' (or a similar operator). Returns
18380 either a TYPE or an expression, depending on the form of the
18381 input. The KEYWORD indicates which kind of expression we have
18385 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18387 tree expr = NULL_TREE;
18388 const char *saved_message;
18390 bool saved_integral_constant_expression_p;
18391 bool saved_non_integral_constant_expression_p;
18392 bool pack_expansion_p = false;
18394 /* Types cannot be defined in a `sizeof' expression. Save away the
18396 saved_message = parser->type_definition_forbidden_message;
18397 /* And create the new one. */
18398 tmp = concat ("types may not be defined in %<",
18399 IDENTIFIER_POINTER (ridpointers[keyword]),
18400 "%> expressions", NULL);
18401 parser->type_definition_forbidden_message = tmp;
18403 /* The restrictions on constant-expressions do not apply inside
18404 sizeof expressions. */
18405 saved_integral_constant_expression_p
18406 = parser->integral_constant_expression_p;
18407 saved_non_integral_constant_expression_p
18408 = parser->non_integral_constant_expression_p;
18409 parser->integral_constant_expression_p = false;
18411 /* If it's a `...', then we are computing the length of a parameter
18413 if (keyword == RID_SIZEOF
18414 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18416 /* Consume the `...'. */
18417 cp_lexer_consume_token (parser->lexer);
18418 maybe_warn_variadic_templates ();
18420 /* Note that this is an expansion. */
18421 pack_expansion_p = true;
18424 /* Do not actually evaluate the expression. */
18426 /* If it's a `(', then we might be looking at the type-id
18428 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18431 bool saved_in_type_id_in_expr_p;
18433 /* We can't be sure yet whether we're looking at a type-id or an
18435 cp_parser_parse_tentatively (parser);
18436 /* Consume the `('. */
18437 cp_lexer_consume_token (parser->lexer);
18438 /* Parse the type-id. */
18439 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18440 parser->in_type_id_in_expr_p = true;
18441 type = cp_parser_type_id (parser);
18442 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18443 /* Now, look for the trailing `)'. */
18444 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18445 /* If all went well, then we're done. */
18446 if (cp_parser_parse_definitely (parser))
18448 cp_decl_specifier_seq decl_specs;
18450 /* Build a trivial decl-specifier-seq. */
18451 clear_decl_specs (&decl_specs);
18452 decl_specs.type = type;
18454 /* Call grokdeclarator to figure out what type this is. */
18455 expr = grokdeclarator (NULL,
18459 /*attrlist=*/NULL);
18463 /* If the type-id production did not work out, then we must be
18464 looking at the unary-expression production. */
18466 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18467 /*cast_p=*/false, NULL);
18469 if (pack_expansion_p)
18470 /* Build a pack expansion. */
18471 expr = make_pack_expansion (expr);
18473 /* Go back to evaluating expressions. */
18476 /* Free the message we created. */
18478 /* And restore the old one. */
18479 parser->type_definition_forbidden_message = saved_message;
18480 parser->integral_constant_expression_p
18481 = saved_integral_constant_expression_p;
18482 parser->non_integral_constant_expression_p
18483 = saved_non_integral_constant_expression_p;
18488 /* If the current declaration has no declarator, return true. */
18491 cp_parser_declares_only_class_p (cp_parser *parser)
18493 /* If the next token is a `;' or a `,' then there is no
18495 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18496 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18499 /* Update the DECL_SPECS to reflect the storage class indicated by
18503 cp_parser_set_storage_class (cp_parser *parser,
18504 cp_decl_specifier_seq *decl_specs,
18506 location_t location)
18508 cp_storage_class storage_class;
18510 if (parser->in_unbraced_linkage_specification_p)
18512 error ("%Hinvalid use of %qD in linkage specification",
18513 &location, ridpointers[keyword]);
18516 else if (decl_specs->storage_class != sc_none)
18518 decl_specs->conflicting_specifiers_p = true;
18522 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18523 && decl_specs->specs[(int) ds_thread])
18525 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18526 decl_specs->specs[(int) ds_thread] = 0;
18532 storage_class = sc_auto;
18535 storage_class = sc_register;
18538 storage_class = sc_static;
18541 storage_class = sc_extern;
18544 storage_class = sc_mutable;
18547 gcc_unreachable ();
18549 decl_specs->storage_class = storage_class;
18551 /* A storage class specifier cannot be applied alongside a typedef
18552 specifier. If there is a typedef specifier present then set
18553 conflicting_specifiers_p which will trigger an error later
18554 on in grokdeclarator. */
18555 if (decl_specs->specs[(int)ds_typedef])
18556 decl_specs->conflicting_specifiers_p = true;
18559 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18560 is true, the type is a user-defined type; otherwise it is a
18561 built-in type specified by a keyword. */
18564 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18566 location_t location,
18567 bool user_defined_p)
18569 decl_specs->any_specifiers_p = true;
18571 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18572 (with, for example, in "typedef int wchar_t;") we remember that
18573 this is what happened. In system headers, we ignore these
18574 declarations so that G++ can work with system headers that are not
18576 if (decl_specs->specs[(int) ds_typedef]
18578 && (type_spec == boolean_type_node
18579 || type_spec == char16_type_node
18580 || type_spec == char32_type_node
18581 || type_spec == wchar_type_node)
18582 && (decl_specs->type
18583 || decl_specs->specs[(int) ds_long]
18584 || decl_specs->specs[(int) ds_short]
18585 || decl_specs->specs[(int) ds_unsigned]
18586 || decl_specs->specs[(int) ds_signed]))
18588 decl_specs->redefined_builtin_type = type_spec;
18589 if (!decl_specs->type)
18591 decl_specs->type = type_spec;
18592 decl_specs->user_defined_type_p = false;
18593 decl_specs->type_location = location;
18596 else if (decl_specs->type)
18597 decl_specs->multiple_types_p = true;
18600 decl_specs->type = type_spec;
18601 decl_specs->user_defined_type_p = user_defined_p;
18602 decl_specs->redefined_builtin_type = NULL_TREE;
18603 decl_specs->type_location = location;
18607 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18608 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18611 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18613 return decl_specifiers->specs[(int) ds_friend] != 0;
18616 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18617 issue an error message indicating that TOKEN_DESC was expected.
18619 Returns the token consumed, if the token had the appropriate type.
18620 Otherwise, returns NULL. */
18623 cp_parser_require (cp_parser* parser,
18624 enum cpp_ttype type,
18625 const char* token_desc)
18627 if (cp_lexer_next_token_is (parser->lexer, type))
18628 return cp_lexer_consume_token (parser->lexer);
18631 /* Output the MESSAGE -- unless we're parsing tentatively. */
18632 if (!cp_parser_simulate_error (parser))
18634 char *message = concat ("expected ", token_desc, NULL);
18635 cp_parser_error (parser, message);
18642 /* An error message is produced if the next token is not '>'.
18643 All further tokens are skipped until the desired token is
18644 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18647 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18649 /* Current level of '< ... >'. */
18650 unsigned level = 0;
18651 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18652 unsigned nesting_depth = 0;
18654 /* Are we ready, yet? If not, issue error message. */
18655 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18658 /* Skip tokens until the desired token is found. */
18661 /* Peek at the next token. */
18662 switch (cp_lexer_peek_token (parser->lexer)->type)
18665 if (!nesting_depth)
18670 if (cxx_dialect == cxx98)
18671 /* C++0x views the `>>' operator as two `>' tokens, but
18674 else if (!nesting_depth && level-- == 0)
18676 /* We've hit a `>>' where the first `>' closes the
18677 template argument list, and the second `>' is
18678 spurious. Just consume the `>>' and stop; we've
18679 already produced at least one error. */
18680 cp_lexer_consume_token (parser->lexer);
18683 /* Fall through for C++0x, so we handle the second `>' in
18687 if (!nesting_depth && level-- == 0)
18689 /* We've reached the token we want, consume it and stop. */
18690 cp_lexer_consume_token (parser->lexer);
18695 case CPP_OPEN_PAREN:
18696 case CPP_OPEN_SQUARE:
18700 case CPP_CLOSE_PAREN:
18701 case CPP_CLOSE_SQUARE:
18702 if (nesting_depth-- == 0)
18707 case CPP_PRAGMA_EOL:
18708 case CPP_SEMICOLON:
18709 case CPP_OPEN_BRACE:
18710 case CPP_CLOSE_BRACE:
18711 /* The '>' was probably forgotten, don't look further. */
18718 /* Consume this token. */
18719 cp_lexer_consume_token (parser->lexer);
18723 /* If the next token is the indicated keyword, consume it. Otherwise,
18724 issue an error message indicating that TOKEN_DESC was expected.
18726 Returns the token consumed, if the token had the appropriate type.
18727 Otherwise, returns NULL. */
18730 cp_parser_require_keyword (cp_parser* parser,
18732 const char* token_desc)
18734 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18736 if (token && token->keyword != keyword)
18738 dyn_string_t error_msg;
18740 /* Format the error message. */
18741 error_msg = dyn_string_new (0);
18742 dyn_string_append_cstr (error_msg, "expected ");
18743 dyn_string_append_cstr (error_msg, token_desc);
18744 cp_parser_error (parser, error_msg->s);
18745 dyn_string_delete (error_msg);
18752 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18753 function-definition. */
18756 cp_parser_token_starts_function_definition_p (cp_token* token)
18758 return (/* An ordinary function-body begins with an `{'. */
18759 token->type == CPP_OPEN_BRACE
18760 /* A ctor-initializer begins with a `:'. */
18761 || token->type == CPP_COLON
18762 /* A function-try-block begins with `try'. */
18763 || token->keyword == RID_TRY
18764 /* The named return value extension begins with `return'. */
18765 || token->keyword == RID_RETURN);
18768 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18772 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18776 token = cp_lexer_peek_token (parser->lexer);
18777 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18780 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18781 C++0x) ending a template-argument. */
18784 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18788 token = cp_lexer_peek_token (parser->lexer);
18789 return (token->type == CPP_COMMA
18790 || token->type == CPP_GREATER
18791 || token->type == CPP_ELLIPSIS
18792 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18795 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18796 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18799 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18804 token = cp_lexer_peek_nth_token (parser->lexer, n);
18805 if (token->type == CPP_LESS)
18807 /* Check for the sequence `<::' in the original code. It would be lexed as
18808 `[:', where `[' is a digraph, and there is no whitespace before
18810 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18813 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18814 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18820 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18821 or none_type otherwise. */
18823 static enum tag_types
18824 cp_parser_token_is_class_key (cp_token* token)
18826 switch (token->keyword)
18831 return record_type;
18840 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18843 cp_parser_check_class_key (enum tag_types class_key, tree type)
18845 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18846 permerror (input_location, "%qs tag used in naming %q#T",
18847 class_key == union_type ? "union"
18848 : class_key == record_type ? "struct" : "class",
18852 /* Issue an error message if DECL is redeclared with different
18853 access than its original declaration [class.access.spec/3].
18854 This applies to nested classes and nested class templates.
18858 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18860 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18863 if ((TREE_PRIVATE (decl)
18864 != (current_access_specifier == access_private_node))
18865 || (TREE_PROTECTED (decl)
18866 != (current_access_specifier == access_protected_node)))
18867 error ("%H%qD redeclared with different access", &location, decl);
18870 /* Look for the `template' keyword, as a syntactic disambiguator.
18871 Return TRUE iff it is present, in which case it will be
18875 cp_parser_optional_template_keyword (cp_parser *parser)
18877 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18879 /* The `template' keyword can only be used within templates;
18880 outside templates the parser can always figure out what is a
18881 template and what is not. */
18882 if (!processing_template_decl)
18884 cp_token *token = cp_lexer_peek_token (parser->lexer);
18885 error ("%H%<template%> (as a disambiguator) is only allowed "
18886 "within templates", &token->location);
18887 /* If this part of the token stream is rescanned, the same
18888 error message would be generated. So, we purge the token
18889 from the stream. */
18890 cp_lexer_purge_token (parser->lexer);
18895 /* Consume the `template' keyword. */
18896 cp_lexer_consume_token (parser->lexer);
18904 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18905 set PARSER->SCOPE, and perform other related actions. */
18908 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18911 struct tree_check *check_value;
18912 deferred_access_check *chk;
18913 VEC (deferred_access_check,gc) *checks;
18915 /* Get the stored value. */
18916 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18917 /* Perform any access checks that were deferred. */
18918 checks = check_value->checks;
18922 VEC_iterate (deferred_access_check, checks, i, chk) ;
18925 perform_or_defer_access_check (chk->binfo,
18930 /* Set the scope from the stored value. */
18931 parser->scope = check_value->value;
18932 parser->qualifying_scope = check_value->qualifying_scope;
18933 parser->object_scope = NULL_TREE;
18936 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18937 encounter the end of a block before what we were looking for. */
18940 cp_parser_cache_group (cp_parser *parser,
18941 enum cpp_ttype end,
18946 cp_token *token = cp_lexer_peek_token (parser->lexer);
18948 /* Abort a parenthesized expression if we encounter a semicolon. */
18949 if ((end == CPP_CLOSE_PAREN || depth == 0)
18950 && token->type == CPP_SEMICOLON)
18952 /* If we've reached the end of the file, stop. */
18953 if (token->type == CPP_EOF
18954 || (end != CPP_PRAGMA_EOL
18955 && token->type == CPP_PRAGMA_EOL))
18957 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18958 /* We've hit the end of an enclosing block, so there's been some
18959 kind of syntax error. */
18962 /* Consume the token. */
18963 cp_lexer_consume_token (parser->lexer);
18964 /* See if it starts a new group. */
18965 if (token->type == CPP_OPEN_BRACE)
18967 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18968 /* In theory this should probably check end == '}', but
18969 cp_parser_save_member_function_body needs it to exit
18970 after either '}' or ')' when called with ')'. */
18974 else if (token->type == CPP_OPEN_PAREN)
18976 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18977 if (depth == 0 && end == CPP_CLOSE_PAREN)
18980 else if (token->type == CPP_PRAGMA)
18981 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18982 else if (token->type == end)
18987 /* Begin parsing tentatively. We always save tokens while parsing
18988 tentatively so that if the tentative parsing fails we can restore the
18992 cp_parser_parse_tentatively (cp_parser* parser)
18994 /* Enter a new parsing context. */
18995 parser->context = cp_parser_context_new (parser->context);
18996 /* Begin saving tokens. */
18997 cp_lexer_save_tokens (parser->lexer);
18998 /* In order to avoid repetitive access control error messages,
18999 access checks are queued up until we are no longer parsing
19001 push_deferring_access_checks (dk_deferred);
19004 /* Commit to the currently active tentative parse. */
19007 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19009 cp_parser_context *context;
19012 /* Mark all of the levels as committed. */
19013 lexer = parser->lexer;
19014 for (context = parser->context; context->next; context = context->next)
19016 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19018 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19019 while (!cp_lexer_saving_tokens (lexer))
19020 lexer = lexer->next;
19021 cp_lexer_commit_tokens (lexer);
19025 /* Abort the currently active tentative parse. All consumed tokens
19026 will be rolled back, and no diagnostics will be issued. */
19029 cp_parser_abort_tentative_parse (cp_parser* parser)
19031 cp_parser_simulate_error (parser);
19032 /* Now, pretend that we want to see if the construct was
19033 successfully parsed. */
19034 cp_parser_parse_definitely (parser);
19037 /* Stop parsing tentatively. If a parse error has occurred, restore the
19038 token stream. Otherwise, commit to the tokens we have consumed.
19039 Returns true if no error occurred; false otherwise. */
19042 cp_parser_parse_definitely (cp_parser* parser)
19044 bool error_occurred;
19045 cp_parser_context *context;
19047 /* Remember whether or not an error occurred, since we are about to
19048 destroy that information. */
19049 error_occurred = cp_parser_error_occurred (parser);
19050 /* Remove the topmost context from the stack. */
19051 context = parser->context;
19052 parser->context = context->next;
19053 /* If no parse errors occurred, commit to the tentative parse. */
19054 if (!error_occurred)
19056 /* Commit to the tokens read tentatively, unless that was
19058 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19059 cp_lexer_commit_tokens (parser->lexer);
19061 pop_to_parent_deferring_access_checks ();
19063 /* Otherwise, if errors occurred, roll back our state so that things
19064 are just as they were before we began the tentative parse. */
19067 cp_lexer_rollback_tokens (parser->lexer);
19068 pop_deferring_access_checks ();
19070 /* Add the context to the front of the free list. */
19071 context->next = cp_parser_context_free_list;
19072 cp_parser_context_free_list = context;
19074 return !error_occurred;
19077 /* Returns true if we are parsing tentatively and are not committed to
19078 this tentative parse. */
19081 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19083 return (cp_parser_parsing_tentatively (parser)
19084 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19087 /* Returns nonzero iff an error has occurred during the most recent
19088 tentative parse. */
19091 cp_parser_error_occurred (cp_parser* parser)
19093 return (cp_parser_parsing_tentatively (parser)
19094 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19097 /* Returns nonzero if GNU extensions are allowed. */
19100 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19102 return parser->allow_gnu_extensions_p;
19105 /* Objective-C++ Productions */
19108 /* Parse an Objective-C expression, which feeds into a primary-expression
19112 objc-message-expression
19113 objc-string-literal
19114 objc-encode-expression
19115 objc-protocol-expression
19116 objc-selector-expression
19118 Returns a tree representation of the expression. */
19121 cp_parser_objc_expression (cp_parser* parser)
19123 /* Try to figure out what kind of declaration is present. */
19124 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19128 case CPP_OPEN_SQUARE:
19129 return cp_parser_objc_message_expression (parser);
19131 case CPP_OBJC_STRING:
19132 kwd = cp_lexer_consume_token (parser->lexer);
19133 return objc_build_string_object (kwd->u.value);
19136 switch (kwd->keyword)
19138 case RID_AT_ENCODE:
19139 return cp_parser_objc_encode_expression (parser);
19141 case RID_AT_PROTOCOL:
19142 return cp_parser_objc_protocol_expression (parser);
19144 case RID_AT_SELECTOR:
19145 return cp_parser_objc_selector_expression (parser);
19151 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19152 &kwd->location, kwd->u.value);
19153 cp_parser_skip_to_end_of_block_or_statement (parser);
19156 return error_mark_node;
19159 /* Parse an Objective-C message expression.
19161 objc-message-expression:
19162 [ objc-message-receiver objc-message-args ]
19164 Returns a representation of an Objective-C message. */
19167 cp_parser_objc_message_expression (cp_parser* parser)
19169 tree receiver, messageargs;
19171 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19172 receiver = cp_parser_objc_message_receiver (parser);
19173 messageargs = cp_parser_objc_message_args (parser);
19174 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19176 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19179 /* Parse an objc-message-receiver.
19181 objc-message-receiver:
19183 simple-type-specifier
19185 Returns a representation of the type or expression. */
19188 cp_parser_objc_message_receiver (cp_parser* parser)
19192 /* An Objective-C message receiver may be either (1) a type
19193 or (2) an expression. */
19194 cp_parser_parse_tentatively (parser);
19195 rcv = cp_parser_expression (parser, false, NULL);
19197 if (cp_parser_parse_definitely (parser))
19200 rcv = cp_parser_simple_type_specifier (parser,
19201 /*decl_specs=*/NULL,
19202 CP_PARSER_FLAGS_NONE);
19204 return objc_get_class_reference (rcv);
19207 /* Parse the arguments and selectors comprising an Objective-C message.
19212 objc-selector-args , objc-comma-args
19214 objc-selector-args:
19215 objc-selector [opt] : assignment-expression
19216 objc-selector-args objc-selector [opt] : assignment-expression
19219 assignment-expression
19220 objc-comma-args , assignment-expression
19222 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19223 selector arguments and TREE_VALUE containing a list of comma
19227 cp_parser_objc_message_args (cp_parser* parser)
19229 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19230 bool maybe_unary_selector_p = true;
19231 cp_token *token = cp_lexer_peek_token (parser->lexer);
19233 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19235 tree selector = NULL_TREE, arg;
19237 if (token->type != CPP_COLON)
19238 selector = cp_parser_objc_selector (parser);
19240 /* Detect if we have a unary selector. */
19241 if (maybe_unary_selector_p
19242 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19243 return build_tree_list (selector, NULL_TREE);
19245 maybe_unary_selector_p = false;
19246 cp_parser_require (parser, CPP_COLON, "%<:%>");
19247 arg = cp_parser_assignment_expression (parser, false, NULL);
19250 = chainon (sel_args,
19251 build_tree_list (selector, arg));
19253 token = cp_lexer_peek_token (parser->lexer);
19256 /* Handle non-selector arguments, if any. */
19257 while (token->type == CPP_COMMA)
19261 cp_lexer_consume_token (parser->lexer);
19262 arg = cp_parser_assignment_expression (parser, false, NULL);
19265 = chainon (addl_args,
19266 build_tree_list (NULL_TREE, arg));
19268 token = cp_lexer_peek_token (parser->lexer);
19271 return build_tree_list (sel_args, addl_args);
19274 /* Parse an Objective-C encode expression.
19276 objc-encode-expression:
19277 @encode objc-typename
19279 Returns an encoded representation of the type argument. */
19282 cp_parser_objc_encode_expression (cp_parser* parser)
19287 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19288 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19289 token = cp_lexer_peek_token (parser->lexer);
19290 type = complete_type (cp_parser_type_id (parser));
19291 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19295 error ("%H%<@encode%> must specify a type as an argument",
19297 return error_mark_node;
19300 return objc_build_encode_expr (type);
19303 /* Parse an Objective-C @defs expression. */
19306 cp_parser_objc_defs_expression (cp_parser *parser)
19310 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19311 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19312 name = cp_parser_identifier (parser);
19313 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19315 return objc_get_class_ivars (name);
19318 /* Parse an Objective-C protocol expression.
19320 objc-protocol-expression:
19321 @protocol ( identifier )
19323 Returns a representation of the protocol expression. */
19326 cp_parser_objc_protocol_expression (cp_parser* parser)
19330 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19331 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19332 proto = cp_parser_identifier (parser);
19333 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19335 return objc_build_protocol_expr (proto);
19338 /* Parse an Objective-C selector expression.
19340 objc-selector-expression:
19341 @selector ( objc-method-signature )
19343 objc-method-signature:
19349 objc-selector-seq objc-selector :
19351 Returns a representation of the method selector. */
19354 cp_parser_objc_selector_expression (cp_parser* parser)
19356 tree sel_seq = NULL_TREE;
19357 bool maybe_unary_selector_p = true;
19360 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19361 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19362 token = cp_lexer_peek_token (parser->lexer);
19364 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19365 || token->type == CPP_SCOPE)
19367 tree selector = NULL_TREE;
19369 if (token->type != CPP_COLON
19370 || token->type == CPP_SCOPE)
19371 selector = cp_parser_objc_selector (parser);
19373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19374 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19376 /* Detect if we have a unary selector. */
19377 if (maybe_unary_selector_p)
19379 sel_seq = selector;
19380 goto finish_selector;
19384 cp_parser_error (parser, "expected %<:%>");
19387 maybe_unary_selector_p = false;
19388 token = cp_lexer_consume_token (parser->lexer);
19390 if (token->type == CPP_SCOPE)
19393 = chainon (sel_seq,
19394 build_tree_list (selector, NULL_TREE));
19396 = chainon (sel_seq,
19397 build_tree_list (NULL_TREE, NULL_TREE));
19401 = chainon (sel_seq,
19402 build_tree_list (selector, NULL_TREE));
19404 token = cp_lexer_peek_token (parser->lexer);
19408 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19410 return objc_build_selector_expr (sel_seq);
19413 /* Parse a list of identifiers.
19415 objc-identifier-list:
19417 objc-identifier-list , identifier
19419 Returns a TREE_LIST of identifier nodes. */
19422 cp_parser_objc_identifier_list (cp_parser* parser)
19424 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19425 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19427 while (sep->type == CPP_COMMA)
19429 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19430 list = chainon (list,
19431 build_tree_list (NULL_TREE,
19432 cp_parser_identifier (parser)));
19433 sep = cp_lexer_peek_token (parser->lexer);
19439 /* Parse an Objective-C alias declaration.
19441 objc-alias-declaration:
19442 @compatibility_alias identifier identifier ;
19444 This function registers the alias mapping with the Objective-C front end.
19445 It returns nothing. */
19448 cp_parser_objc_alias_declaration (cp_parser* parser)
19452 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19453 alias = cp_parser_identifier (parser);
19454 orig = cp_parser_identifier (parser);
19455 objc_declare_alias (alias, orig);
19456 cp_parser_consume_semicolon_at_end_of_statement (parser);
19459 /* Parse an Objective-C class forward-declaration.
19461 objc-class-declaration:
19462 @class objc-identifier-list ;
19464 The function registers the forward declarations with the Objective-C
19465 front end. It returns nothing. */
19468 cp_parser_objc_class_declaration (cp_parser* parser)
19470 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19471 objc_declare_class (cp_parser_objc_identifier_list (parser));
19472 cp_parser_consume_semicolon_at_end_of_statement (parser);
19475 /* Parse a list of Objective-C protocol references.
19477 objc-protocol-refs-opt:
19478 objc-protocol-refs [opt]
19480 objc-protocol-refs:
19481 < objc-identifier-list >
19483 Returns a TREE_LIST of identifiers, if any. */
19486 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19488 tree protorefs = NULL_TREE;
19490 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19492 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19493 protorefs = cp_parser_objc_identifier_list (parser);
19494 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19500 /* Parse a Objective-C visibility specification. */
19503 cp_parser_objc_visibility_spec (cp_parser* parser)
19505 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19507 switch (vis->keyword)
19509 case RID_AT_PRIVATE:
19510 objc_set_visibility (2);
19512 case RID_AT_PROTECTED:
19513 objc_set_visibility (0);
19515 case RID_AT_PUBLIC:
19516 objc_set_visibility (1);
19522 /* Eat '@private'/'@protected'/'@public'. */
19523 cp_lexer_consume_token (parser->lexer);
19526 /* Parse an Objective-C method type. */
19529 cp_parser_objc_method_type (cp_parser* parser)
19531 objc_set_method_type
19532 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19537 /* Parse an Objective-C protocol qualifier. */
19540 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19542 tree quals = NULL_TREE, node;
19543 cp_token *token = cp_lexer_peek_token (parser->lexer);
19545 node = token->u.value;
19547 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19548 && (node == ridpointers [(int) RID_IN]
19549 || node == ridpointers [(int) RID_OUT]
19550 || node == ridpointers [(int) RID_INOUT]
19551 || node == ridpointers [(int) RID_BYCOPY]
19552 || node == ridpointers [(int) RID_BYREF]
19553 || node == ridpointers [(int) RID_ONEWAY]))
19555 quals = tree_cons (NULL_TREE, node, quals);
19556 cp_lexer_consume_token (parser->lexer);
19557 token = cp_lexer_peek_token (parser->lexer);
19558 node = token->u.value;
19564 /* Parse an Objective-C typename. */
19567 cp_parser_objc_typename (cp_parser* parser)
19569 tree type_name = NULL_TREE;
19571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19573 tree proto_quals, cp_type = NULL_TREE;
19575 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19576 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19578 /* An ObjC type name may consist of just protocol qualifiers, in which
19579 case the type shall default to 'id'. */
19580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19581 cp_type = cp_parser_type_id (parser);
19583 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19584 type_name = build_tree_list (proto_quals, cp_type);
19590 /* Check to see if TYPE refers to an Objective-C selector name. */
19593 cp_parser_objc_selector_p (enum cpp_ttype type)
19595 return (type == CPP_NAME || type == CPP_KEYWORD
19596 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19597 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19598 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19599 || type == CPP_XOR || type == CPP_XOR_EQ);
19602 /* Parse an Objective-C selector. */
19605 cp_parser_objc_selector (cp_parser* parser)
19607 cp_token *token = cp_lexer_consume_token (parser->lexer);
19609 if (!cp_parser_objc_selector_p (token->type))
19611 error ("%Hinvalid Objective-C++ selector name", &token->location);
19612 return error_mark_node;
19615 /* C++ operator names are allowed to appear in ObjC selectors. */
19616 switch (token->type)
19618 case CPP_AND_AND: return get_identifier ("and");
19619 case CPP_AND_EQ: return get_identifier ("and_eq");
19620 case CPP_AND: return get_identifier ("bitand");
19621 case CPP_OR: return get_identifier ("bitor");
19622 case CPP_COMPL: return get_identifier ("compl");
19623 case CPP_NOT: return get_identifier ("not");
19624 case CPP_NOT_EQ: return get_identifier ("not_eq");
19625 case CPP_OR_OR: return get_identifier ("or");
19626 case CPP_OR_EQ: return get_identifier ("or_eq");
19627 case CPP_XOR: return get_identifier ("xor");
19628 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19629 default: return token->u.value;
19633 /* Parse an Objective-C params list. */
19636 cp_parser_objc_method_keyword_params (cp_parser* parser)
19638 tree params = NULL_TREE;
19639 bool maybe_unary_selector_p = true;
19640 cp_token *token = cp_lexer_peek_token (parser->lexer);
19642 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19644 tree selector = NULL_TREE, type_name, identifier;
19646 if (token->type != CPP_COLON)
19647 selector = cp_parser_objc_selector (parser);
19649 /* Detect if we have a unary selector. */
19650 if (maybe_unary_selector_p
19651 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19654 maybe_unary_selector_p = false;
19655 cp_parser_require (parser, CPP_COLON, "%<:%>");
19656 type_name = cp_parser_objc_typename (parser);
19657 identifier = cp_parser_identifier (parser);
19661 objc_build_keyword_decl (selector,
19665 token = cp_lexer_peek_token (parser->lexer);
19671 /* Parse the non-keyword Objective-C params. */
19674 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19676 tree params = make_node (TREE_LIST);
19677 cp_token *token = cp_lexer_peek_token (parser->lexer);
19678 *ellipsisp = false; /* Initially, assume no ellipsis. */
19680 while (token->type == CPP_COMMA)
19682 cp_parameter_declarator *parmdecl;
19685 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19686 token = cp_lexer_peek_token (parser->lexer);
19688 if (token->type == CPP_ELLIPSIS)
19690 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19695 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19696 parm = grokdeclarator (parmdecl->declarator,
19697 &parmdecl->decl_specifiers,
19698 PARM, /*initialized=*/0,
19699 /*attrlist=*/NULL);
19701 chainon (params, build_tree_list (NULL_TREE, parm));
19702 token = cp_lexer_peek_token (parser->lexer);
19708 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19711 cp_parser_objc_interstitial_code (cp_parser* parser)
19713 cp_token *token = cp_lexer_peek_token (parser->lexer);
19715 /* If the next token is `extern' and the following token is a string
19716 literal, then we have a linkage specification. */
19717 if (token->keyword == RID_EXTERN
19718 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19719 cp_parser_linkage_specification (parser);
19720 /* Handle #pragma, if any. */
19721 else if (token->type == CPP_PRAGMA)
19722 cp_parser_pragma (parser, pragma_external);
19723 /* Allow stray semicolons. */
19724 else if (token->type == CPP_SEMICOLON)
19725 cp_lexer_consume_token (parser->lexer);
19726 /* Finally, try to parse a block-declaration, or a function-definition. */
19728 cp_parser_block_declaration (parser, /*statement_p=*/false);
19731 /* Parse a method signature. */
19734 cp_parser_objc_method_signature (cp_parser* parser)
19736 tree rettype, kwdparms, optparms;
19737 bool ellipsis = false;
19739 cp_parser_objc_method_type (parser);
19740 rettype = cp_parser_objc_typename (parser);
19741 kwdparms = cp_parser_objc_method_keyword_params (parser);
19742 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19744 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19747 /* Pars an Objective-C method prototype list. */
19750 cp_parser_objc_method_prototype_list (cp_parser* parser)
19752 cp_token *token = cp_lexer_peek_token (parser->lexer);
19754 while (token->keyword != RID_AT_END)
19756 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19758 objc_add_method_declaration
19759 (cp_parser_objc_method_signature (parser));
19760 cp_parser_consume_semicolon_at_end_of_statement (parser);
19763 /* Allow for interspersed non-ObjC++ code. */
19764 cp_parser_objc_interstitial_code (parser);
19766 token = cp_lexer_peek_token (parser->lexer);
19769 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19770 objc_finish_interface ();
19773 /* Parse an Objective-C method definition list. */
19776 cp_parser_objc_method_definition_list (cp_parser* parser)
19778 cp_token *token = cp_lexer_peek_token (parser->lexer);
19780 while (token->keyword != RID_AT_END)
19784 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19786 push_deferring_access_checks (dk_deferred);
19787 objc_start_method_definition
19788 (cp_parser_objc_method_signature (parser));
19790 /* For historical reasons, we accept an optional semicolon. */
19791 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19792 cp_lexer_consume_token (parser->lexer);
19794 perform_deferred_access_checks ();
19795 stop_deferring_access_checks ();
19796 meth = cp_parser_function_definition_after_declarator (parser,
19798 pop_deferring_access_checks ();
19799 objc_finish_method_definition (meth);
19802 /* Allow for interspersed non-ObjC++ code. */
19803 cp_parser_objc_interstitial_code (parser);
19805 token = cp_lexer_peek_token (parser->lexer);
19808 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19809 objc_finish_implementation ();
19812 /* Parse Objective-C ivars. */
19815 cp_parser_objc_class_ivars (cp_parser* parser)
19817 cp_token *token = cp_lexer_peek_token (parser->lexer);
19819 if (token->type != CPP_OPEN_BRACE)
19820 return; /* No ivars specified. */
19822 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19823 token = cp_lexer_peek_token (parser->lexer);
19825 while (token->type != CPP_CLOSE_BRACE)
19827 cp_decl_specifier_seq declspecs;
19828 int decl_class_or_enum_p;
19829 tree prefix_attributes;
19831 cp_parser_objc_visibility_spec (parser);
19833 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19836 cp_parser_decl_specifier_seq (parser,
19837 CP_PARSER_FLAGS_OPTIONAL,
19839 &decl_class_or_enum_p);
19840 prefix_attributes = declspecs.attributes;
19841 declspecs.attributes = NULL_TREE;
19843 /* Keep going until we hit the `;' at the end of the
19845 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19847 tree width = NULL_TREE, attributes, first_attribute, decl;
19848 cp_declarator *declarator = NULL;
19849 int ctor_dtor_or_conv_p;
19851 /* Check for a (possibly unnamed) bitfield declaration. */
19852 token = cp_lexer_peek_token (parser->lexer);
19853 if (token->type == CPP_COLON)
19856 if (token->type == CPP_NAME
19857 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19860 /* Get the name of the bitfield. */
19861 declarator = make_id_declarator (NULL_TREE,
19862 cp_parser_identifier (parser),
19866 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19867 /* Get the width of the bitfield. */
19869 = cp_parser_constant_expression (parser,
19870 /*allow_non_constant=*/false,
19875 /* Parse the declarator. */
19877 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19878 &ctor_dtor_or_conv_p,
19879 /*parenthesized_p=*/NULL,
19880 /*member_p=*/false);
19883 /* Look for attributes that apply to the ivar. */
19884 attributes = cp_parser_attributes_opt (parser);
19885 /* Remember which attributes are prefix attributes and
19887 first_attribute = attributes;
19888 /* Combine the attributes. */
19889 attributes = chainon (prefix_attributes, attributes);
19892 /* Create the bitfield declaration. */
19893 decl = grokbitfield (declarator, &declspecs,
19897 decl = grokfield (declarator, &declspecs,
19898 NULL_TREE, /*init_const_expr_p=*/false,
19899 NULL_TREE, attributes);
19901 /* Add the instance variable. */
19902 objc_add_instance_variable (decl);
19904 /* Reset PREFIX_ATTRIBUTES. */
19905 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19906 attributes = TREE_CHAIN (attributes);
19908 TREE_CHAIN (attributes) = NULL_TREE;
19910 token = cp_lexer_peek_token (parser->lexer);
19912 if (token->type == CPP_COMMA)
19914 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19920 cp_parser_consume_semicolon_at_end_of_statement (parser);
19921 token = cp_lexer_peek_token (parser->lexer);
19924 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19925 /* For historical reasons, we accept an optional semicolon. */
19926 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19927 cp_lexer_consume_token (parser->lexer);
19930 /* Parse an Objective-C protocol declaration. */
19933 cp_parser_objc_protocol_declaration (cp_parser* parser)
19935 tree proto, protorefs;
19938 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19941 tok = cp_lexer_peek_token (parser->lexer);
19942 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19946 /* See if we have a forward declaration or a definition. */
19947 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19949 /* Try a forward declaration first. */
19950 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19952 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19954 cp_parser_consume_semicolon_at_end_of_statement (parser);
19957 /* Ok, we got a full-fledged definition (or at least should). */
19960 proto = cp_parser_identifier (parser);
19961 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19962 objc_start_protocol (proto, protorefs);
19963 cp_parser_objc_method_prototype_list (parser);
19967 /* Parse an Objective-C superclass or category. */
19970 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19973 cp_token *next = cp_lexer_peek_token (parser->lexer);
19975 *super = *categ = NULL_TREE;
19976 if (next->type == CPP_COLON)
19978 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19979 *super = cp_parser_identifier (parser);
19981 else if (next->type == CPP_OPEN_PAREN)
19983 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19984 *categ = cp_parser_identifier (parser);
19985 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19989 /* Parse an Objective-C class interface. */
19992 cp_parser_objc_class_interface (cp_parser* parser)
19994 tree name, super, categ, protos;
19996 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19997 name = cp_parser_identifier (parser);
19998 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19999 protos = cp_parser_objc_protocol_refs_opt (parser);
20001 /* We have either a class or a category on our hands. */
20003 objc_start_category_interface (name, categ, protos);
20006 objc_start_class_interface (name, super, protos);
20007 /* Handle instance variable declarations, if any. */
20008 cp_parser_objc_class_ivars (parser);
20009 objc_continue_interface ();
20012 cp_parser_objc_method_prototype_list (parser);
20015 /* Parse an Objective-C class implementation. */
20018 cp_parser_objc_class_implementation (cp_parser* parser)
20020 tree name, super, categ;
20022 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20023 name = cp_parser_identifier (parser);
20024 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20026 /* We have either a class or a category on our hands. */
20028 objc_start_category_implementation (name, categ);
20031 objc_start_class_implementation (name, super);
20032 /* Handle instance variable declarations, if any. */
20033 cp_parser_objc_class_ivars (parser);
20034 objc_continue_implementation ();
20037 cp_parser_objc_method_definition_list (parser);
20040 /* Consume the @end token and finish off the implementation. */
20043 cp_parser_objc_end_implementation (cp_parser* parser)
20045 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20046 objc_finish_implementation ();
20049 /* Parse an Objective-C declaration. */
20052 cp_parser_objc_declaration (cp_parser* parser)
20054 /* Try to figure out what kind of declaration is present. */
20055 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20057 switch (kwd->keyword)
20060 cp_parser_objc_alias_declaration (parser);
20063 cp_parser_objc_class_declaration (parser);
20065 case RID_AT_PROTOCOL:
20066 cp_parser_objc_protocol_declaration (parser);
20068 case RID_AT_INTERFACE:
20069 cp_parser_objc_class_interface (parser);
20071 case RID_AT_IMPLEMENTATION:
20072 cp_parser_objc_class_implementation (parser);
20075 cp_parser_objc_end_implementation (parser);
20078 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20079 &kwd->location, kwd->u.value);
20080 cp_parser_skip_to_end_of_block_or_statement (parser);
20084 /* Parse an Objective-C try-catch-finally statement.
20086 objc-try-catch-finally-stmt:
20087 @try compound-statement objc-catch-clause-seq [opt]
20088 objc-finally-clause [opt]
20090 objc-catch-clause-seq:
20091 objc-catch-clause objc-catch-clause-seq [opt]
20094 @catch ( exception-declaration ) compound-statement
20096 objc-finally-clause
20097 @finally compound-statement
20099 Returns NULL_TREE. */
20102 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20103 location_t location;
20106 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20107 location = cp_lexer_peek_token (parser->lexer)->location;
20108 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20109 node, lest it get absorbed into the surrounding block. */
20110 stmt = push_stmt_list ();
20111 cp_parser_compound_statement (parser, NULL, false);
20112 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20114 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20116 cp_parameter_declarator *parmdecl;
20119 cp_lexer_consume_token (parser->lexer);
20120 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20121 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20122 parm = grokdeclarator (parmdecl->declarator,
20123 &parmdecl->decl_specifiers,
20124 PARM, /*initialized=*/0,
20125 /*attrlist=*/NULL);
20126 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20127 objc_begin_catch_clause (parm);
20128 cp_parser_compound_statement (parser, NULL, false);
20129 objc_finish_catch_clause ();
20132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20134 cp_lexer_consume_token (parser->lexer);
20135 location = cp_lexer_peek_token (parser->lexer)->location;
20136 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20137 node, lest it get absorbed into the surrounding block. */
20138 stmt = push_stmt_list ();
20139 cp_parser_compound_statement (parser, NULL, false);
20140 objc_build_finally_clause (location, pop_stmt_list (stmt));
20143 return objc_finish_try_stmt ();
20146 /* Parse an Objective-C synchronized statement.
20148 objc-synchronized-stmt:
20149 @synchronized ( expression ) compound-statement
20151 Returns NULL_TREE. */
20154 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20155 location_t location;
20158 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20160 location = cp_lexer_peek_token (parser->lexer)->location;
20161 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20162 lock = cp_parser_expression (parser, false, NULL);
20163 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20165 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20166 node, lest it get absorbed into the surrounding block. */
20167 stmt = push_stmt_list ();
20168 cp_parser_compound_statement (parser, NULL, false);
20170 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20173 /* Parse an Objective-C throw statement.
20176 @throw assignment-expression [opt] ;
20178 Returns a constructed '@throw' statement. */
20181 cp_parser_objc_throw_statement (cp_parser *parser) {
20182 tree expr = NULL_TREE;
20184 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20187 expr = cp_parser_assignment_expression (parser, false, NULL);
20189 cp_parser_consume_semicolon_at_end_of_statement (parser);
20191 return objc_build_throw_stmt (expr);
20194 /* Parse an Objective-C statement. */
20197 cp_parser_objc_statement (cp_parser * parser) {
20198 /* Try to figure out what kind of declaration is present. */
20199 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20201 switch (kwd->keyword)
20204 return cp_parser_objc_try_catch_finally_statement (parser);
20205 case RID_AT_SYNCHRONIZED:
20206 return cp_parser_objc_synchronized_statement (parser);
20208 return cp_parser_objc_throw_statement (parser);
20210 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20211 &kwd->location, kwd->u.value);
20212 cp_parser_skip_to_end_of_block_or_statement (parser);
20215 return error_mark_node;
20218 /* OpenMP 2.5 parsing routines. */
20220 /* Returns name of the next clause.
20221 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20222 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20223 returned and the token is consumed. */
20225 static pragma_omp_clause
20226 cp_parser_omp_clause_name (cp_parser *parser)
20228 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20230 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20231 result = PRAGMA_OMP_CLAUSE_IF;
20232 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20233 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20234 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20235 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20236 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20238 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20239 const char *p = IDENTIFIER_POINTER (id);
20244 if (!strcmp ("collapse", p))
20245 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20246 else if (!strcmp ("copyin", p))
20247 result = PRAGMA_OMP_CLAUSE_COPYIN;
20248 else if (!strcmp ("copyprivate", p))
20249 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20252 if (!strcmp ("firstprivate", p))
20253 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20256 if (!strcmp ("lastprivate", p))
20257 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20260 if (!strcmp ("nowait", p))
20261 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20262 else if (!strcmp ("num_threads", p))
20263 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20266 if (!strcmp ("ordered", p))
20267 result = PRAGMA_OMP_CLAUSE_ORDERED;
20270 if (!strcmp ("reduction", p))
20271 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20274 if (!strcmp ("schedule", p))
20275 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20276 else if (!strcmp ("shared", p))
20277 result = PRAGMA_OMP_CLAUSE_SHARED;
20280 if (!strcmp ("untied", p))
20281 result = PRAGMA_OMP_CLAUSE_UNTIED;
20286 if (result != PRAGMA_OMP_CLAUSE_NONE)
20287 cp_lexer_consume_token (parser->lexer);
20292 /* Validate that a clause of the given type does not already exist. */
20295 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20296 const char *name, location_t location)
20300 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20301 if (OMP_CLAUSE_CODE (c) == code)
20303 error ("%Htoo many %qs clauses", &location, name);
20311 variable-list , identifier
20313 In addition, we match a closing parenthesis. An opening parenthesis
20314 will have been consumed by the caller.
20316 If KIND is nonzero, create the appropriate node and install the decl
20317 in OMP_CLAUSE_DECL and add the node to the head of the list.
20319 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20320 return the list created. */
20323 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20331 token = cp_lexer_peek_token (parser->lexer);
20332 name = cp_parser_id_expression (parser, /*template_p=*/false,
20333 /*check_dependency_p=*/true,
20334 /*template_p=*/NULL,
20335 /*declarator_p=*/false,
20336 /*optional_p=*/false);
20337 if (name == error_mark_node)
20340 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20341 if (decl == error_mark_node)
20342 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20343 else if (kind != 0)
20345 tree u = build_omp_clause (kind);
20346 OMP_CLAUSE_DECL (u) = decl;
20347 OMP_CLAUSE_CHAIN (u) = list;
20351 list = tree_cons (decl, NULL_TREE, list);
20354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20356 cp_lexer_consume_token (parser->lexer);
20359 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20363 /* Try to resync to an unnested comma. Copied from
20364 cp_parser_parenthesized_expression_list. */
20366 ending = cp_parser_skip_to_closing_parenthesis (parser,
20367 /*recovering=*/true,
20369 /*consume_paren=*/true);
20377 /* Similarly, but expect leading and trailing parenthesis. This is a very
20378 common case for omp clauses. */
20381 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20383 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20384 return cp_parser_omp_var_list_no_open (parser, kind, list);
20389 collapse ( constant-expression ) */
20392 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20398 loc = cp_lexer_peek_token (parser->lexer)->location;
20399 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20402 num = cp_parser_constant_expression (parser, false, NULL);
20404 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20406 /*or_comma=*/false,
20407 /*consume_paren=*/true);
20409 if (num == error_mark_node)
20411 num = fold_non_dependent_expr (num);
20412 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20413 || !host_integerp (num, 0)
20414 || (n = tree_low_cst (num, 0)) <= 0
20417 error ("%Hcollapse argument needs positive constant integer expression",
20422 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20423 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20424 OMP_CLAUSE_CHAIN (c) = list;
20425 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20431 default ( shared | none ) */
20434 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20436 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20439 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20441 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20443 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20444 const char *p = IDENTIFIER_POINTER (id);
20449 if (strcmp ("none", p) != 0)
20451 kind = OMP_CLAUSE_DEFAULT_NONE;
20455 if (strcmp ("shared", p) != 0)
20457 kind = OMP_CLAUSE_DEFAULT_SHARED;
20464 cp_lexer_consume_token (parser->lexer);
20469 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20472 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20474 /*or_comma=*/false,
20475 /*consume_paren=*/true);
20477 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20480 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20481 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20482 OMP_CLAUSE_CHAIN (c) = list;
20483 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20489 if ( expression ) */
20492 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20499 t = cp_parser_condition (parser);
20501 if (t == error_mark_node
20502 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20504 /*or_comma=*/false,
20505 /*consume_paren=*/true);
20507 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20509 c = build_omp_clause (OMP_CLAUSE_IF);
20510 OMP_CLAUSE_IF_EXPR (c) = t;
20511 OMP_CLAUSE_CHAIN (c) = list;
20520 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20521 tree list, location_t location)
20525 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20527 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20528 OMP_CLAUSE_CHAIN (c) = list;
20533 num_threads ( expression ) */
20536 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20537 location_t location)
20541 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20544 t = cp_parser_expression (parser, false, NULL);
20546 if (t == error_mark_node
20547 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20548 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20549 /*or_comma=*/false,
20550 /*consume_paren=*/true);
20552 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20553 "num_threads", location);
20555 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20556 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20557 OMP_CLAUSE_CHAIN (c) = list;
20566 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20567 tree list, location_t location)
20571 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20572 "ordered", location);
20574 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20575 OMP_CLAUSE_CHAIN (c) = list;
20580 reduction ( reduction-operator : variable-list )
20582 reduction-operator:
20583 One of: + * - & ^ | && || */
20586 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20588 enum tree_code code;
20591 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20594 switch (cp_lexer_peek_token (parser->lexer)->type)
20606 code = BIT_AND_EXPR;
20609 code = BIT_XOR_EXPR;
20612 code = BIT_IOR_EXPR;
20615 code = TRUTH_ANDIF_EXPR;
20618 code = TRUTH_ORIF_EXPR;
20621 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20622 "%<|%>, %<&&%>, or %<||%>");
20624 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20625 /*or_comma=*/false,
20626 /*consume_paren=*/true);
20629 cp_lexer_consume_token (parser->lexer);
20631 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20634 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20635 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20636 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20642 schedule ( schedule-kind )
20643 schedule ( schedule-kind , expression )
20646 static | dynamic | guided | runtime | auto */
20649 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20653 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20656 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20658 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20660 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20661 const char *p = IDENTIFIER_POINTER (id);
20666 if (strcmp ("dynamic", p) != 0)
20668 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20672 if (strcmp ("guided", p) != 0)
20674 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20678 if (strcmp ("runtime", p) != 0)
20680 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20687 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20688 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20689 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20690 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20693 cp_lexer_consume_token (parser->lexer);
20695 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20698 cp_lexer_consume_token (parser->lexer);
20700 token = cp_lexer_peek_token (parser->lexer);
20701 t = cp_parser_assignment_expression (parser, false, NULL);
20703 if (t == error_mark_node)
20705 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20706 error ("%Hschedule %<runtime%> does not take "
20707 "a %<chunk_size%> parameter", &token->location);
20708 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20709 error ("%Hschedule %<auto%> does not take "
20710 "a %<chunk_size%> parameter", &token->location);
20712 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20714 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20717 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20720 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20721 OMP_CLAUSE_CHAIN (c) = list;
20725 cp_parser_error (parser, "invalid schedule kind");
20727 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20728 /*or_comma=*/false,
20729 /*consume_paren=*/true);
20737 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20738 tree list, location_t location)
20742 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20744 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20745 OMP_CLAUSE_CHAIN (c) = list;
20749 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20750 is a bitmask in MASK. Return the list of clauses found; the result
20751 of clause default goes in *pdefault. */
20754 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20755 const char *where, cp_token *pragma_tok)
20757 tree clauses = NULL;
20759 cp_token *token = NULL;
20761 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20763 pragma_omp_clause c_kind;
20764 const char *c_name;
20765 tree prev = clauses;
20767 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20768 cp_lexer_consume_token (parser->lexer);
20770 token = cp_lexer_peek_token (parser->lexer);
20771 c_kind = cp_parser_omp_clause_name (parser);
20776 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20777 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20779 c_name = "collapse";
20781 case PRAGMA_OMP_CLAUSE_COPYIN:
20782 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20785 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20786 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20788 c_name = "copyprivate";
20790 case PRAGMA_OMP_CLAUSE_DEFAULT:
20791 clauses = cp_parser_omp_clause_default (parser, clauses,
20793 c_name = "default";
20795 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20796 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20798 c_name = "firstprivate";
20800 case PRAGMA_OMP_CLAUSE_IF:
20801 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20804 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20805 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20807 c_name = "lastprivate";
20809 case PRAGMA_OMP_CLAUSE_NOWAIT:
20810 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20813 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20814 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20816 c_name = "num_threads";
20818 case PRAGMA_OMP_CLAUSE_ORDERED:
20819 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20821 c_name = "ordered";
20823 case PRAGMA_OMP_CLAUSE_PRIVATE:
20824 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20826 c_name = "private";
20828 case PRAGMA_OMP_CLAUSE_REDUCTION:
20829 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20830 c_name = "reduction";
20832 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20833 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20835 c_name = "schedule";
20837 case PRAGMA_OMP_CLAUSE_SHARED:
20838 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20842 case PRAGMA_OMP_CLAUSE_UNTIED:
20843 clauses = cp_parser_omp_clause_untied (parser, clauses,
20848 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20852 if (((mask >> c_kind) & 1) == 0)
20854 /* Remove the invalid clause(s) from the list to avoid
20855 confusing the rest of the compiler. */
20857 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20861 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20862 return finish_omp_clauses (clauses);
20869 In practice, we're also interested in adding the statement to an
20870 outer node. So it is convenient if we work around the fact that
20871 cp_parser_statement calls add_stmt. */
20874 cp_parser_begin_omp_structured_block (cp_parser *parser)
20876 unsigned save = parser->in_statement;
20878 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20879 This preserves the "not within loop or switch" style error messages
20880 for nonsense cases like
20886 if (parser->in_statement)
20887 parser->in_statement = IN_OMP_BLOCK;
20893 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20895 parser->in_statement = save;
20899 cp_parser_omp_structured_block (cp_parser *parser)
20901 tree stmt = begin_omp_structured_block ();
20902 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20904 cp_parser_statement (parser, NULL_TREE, false, NULL);
20906 cp_parser_end_omp_structured_block (parser, save);
20907 return finish_omp_structured_block (stmt);
20911 # pragma omp atomic new-line
20915 x binop= expr | x++ | ++x | x-- | --x
20917 +, *, -, /, &, ^, |, <<, >>
20919 where x is an lvalue expression with scalar type. */
20922 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20925 enum tree_code code;
20927 cp_parser_require_pragma_eol (parser, pragma_tok);
20929 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20930 /*cast_p=*/false, NULL);
20931 switch (TREE_CODE (lhs))
20936 case PREINCREMENT_EXPR:
20937 case POSTINCREMENT_EXPR:
20938 lhs = TREE_OPERAND (lhs, 0);
20940 rhs = integer_one_node;
20943 case PREDECREMENT_EXPR:
20944 case POSTDECREMENT_EXPR:
20945 lhs = TREE_OPERAND (lhs, 0);
20947 rhs = integer_one_node;
20951 switch (cp_lexer_peek_token (parser->lexer)->type)
20957 code = TRUNC_DIV_EXPR;
20965 case CPP_LSHIFT_EQ:
20966 code = LSHIFT_EXPR;
20968 case CPP_RSHIFT_EQ:
20969 code = RSHIFT_EXPR;
20972 code = BIT_AND_EXPR;
20975 code = BIT_IOR_EXPR;
20978 code = BIT_XOR_EXPR;
20981 cp_parser_error (parser,
20982 "invalid operator for %<#pragma omp atomic%>");
20985 cp_lexer_consume_token (parser->lexer);
20987 rhs = cp_parser_expression (parser, false, NULL);
20988 if (rhs == error_mark_node)
20992 finish_omp_atomic (code, lhs, rhs);
20993 cp_parser_consume_semicolon_at_end_of_statement (parser);
20997 cp_parser_skip_to_end_of_block_or_statement (parser);
21002 # pragma omp barrier new-line */
21005 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21007 cp_parser_require_pragma_eol (parser, pragma_tok);
21008 finish_omp_barrier ();
21012 # pragma omp critical [(name)] new-line
21013 structured-block */
21016 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21018 tree stmt, name = NULL;
21020 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21022 cp_lexer_consume_token (parser->lexer);
21024 name = cp_parser_identifier (parser);
21026 if (name == error_mark_node
21027 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21028 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21029 /*or_comma=*/false,
21030 /*consume_paren=*/true);
21031 if (name == error_mark_node)
21034 cp_parser_require_pragma_eol (parser, pragma_tok);
21036 stmt = cp_parser_omp_structured_block (parser);
21037 return c_finish_omp_critical (stmt, name);
21041 # pragma omp flush flush-vars[opt] new-line
21044 ( variable-list ) */
21047 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21050 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21051 cp_parser_require_pragma_eol (parser, pragma_tok);
21053 finish_omp_flush ();
21056 /* Helper function, to parse omp for increment expression. */
21059 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21061 tree cond = cp_parser_binary_expression (parser, false, true,
21062 PREC_NOT_OPERATOR, NULL);
21065 if (cond == error_mark_node
21066 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21068 cp_parser_skip_to_end_of_statement (parser);
21069 return error_mark_node;
21072 switch (TREE_CODE (cond))
21080 return error_mark_node;
21083 /* If decl is an iterator, preserve LHS and RHS of the relational
21084 expr until finish_omp_for. */
21086 && (type_dependent_expression_p (decl)
21087 || CLASS_TYPE_P (TREE_TYPE (decl))))
21090 return build_x_binary_op (TREE_CODE (cond),
21091 TREE_OPERAND (cond, 0), ERROR_MARK,
21092 TREE_OPERAND (cond, 1), ERROR_MARK,
21093 &overloaded_p, tf_warning_or_error);
21096 /* Helper function, to parse omp for increment expression. */
21099 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21101 cp_token *token = cp_lexer_peek_token (parser->lexer);
21107 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21109 op = (token->type == CPP_PLUS_PLUS
21110 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21111 cp_lexer_consume_token (parser->lexer);
21112 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21114 return error_mark_node;
21115 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21118 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21120 return error_mark_node;
21122 token = cp_lexer_peek_token (parser->lexer);
21123 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21125 op = (token->type == CPP_PLUS_PLUS
21126 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21127 cp_lexer_consume_token (parser->lexer);
21128 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21131 op = cp_parser_assignment_operator_opt (parser);
21132 if (op == ERROR_MARK)
21133 return error_mark_node;
21135 if (op != NOP_EXPR)
21137 rhs = cp_parser_assignment_expression (parser, false, NULL);
21138 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21139 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21142 lhs = cp_parser_binary_expression (parser, false, false,
21143 PREC_ADDITIVE_EXPRESSION, NULL);
21144 token = cp_lexer_peek_token (parser->lexer);
21145 decl_first = lhs == decl;
21148 if (token->type != CPP_PLUS
21149 && token->type != CPP_MINUS)
21150 return error_mark_node;
21154 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21155 cp_lexer_consume_token (parser->lexer);
21156 rhs = cp_parser_binary_expression (parser, false, false,
21157 PREC_ADDITIVE_EXPRESSION, NULL);
21158 token = cp_lexer_peek_token (parser->lexer);
21159 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21161 if (lhs == NULL_TREE)
21163 if (op == PLUS_EXPR)
21166 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21169 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21170 NULL, tf_warning_or_error);
21173 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21177 if (rhs != decl || op == MINUS_EXPR)
21178 return error_mark_node;
21179 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21182 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21184 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21187 /* Parse the restricted form of the for statement allowed by OpenMP. */
21190 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21192 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21193 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21194 tree this_pre_body, cl;
21195 location_t loc_first;
21196 bool collapse_err = false;
21197 int i, collapse = 1, nbraces = 0;
21199 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21200 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21201 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21203 gcc_assert (collapse >= 1);
21205 declv = make_tree_vec (collapse);
21206 initv = make_tree_vec (collapse);
21207 condv = make_tree_vec (collapse);
21208 incrv = make_tree_vec (collapse);
21210 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21212 for (i = 0; i < collapse; i++)
21214 int bracecount = 0;
21215 bool add_private_clause = false;
21218 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21220 cp_parser_error (parser, "for statement expected");
21223 loc = cp_lexer_consume_token (parser->lexer)->location;
21225 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21228 init = decl = real_decl = NULL;
21229 this_pre_body = push_stmt_list ();
21230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21232 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21236 integer-type var = lb
21237 random-access-iterator-type var = lb
21238 pointer-type var = lb
21240 cp_decl_specifier_seq type_specifiers;
21242 /* First, try to parse as an initialized declaration. See
21243 cp_parser_condition, from whence the bulk of this is copied. */
21245 cp_parser_parse_tentatively (parser);
21246 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21248 if (cp_parser_parse_definitely (parser))
21250 /* If parsing a type specifier seq succeeded, then this
21251 MUST be a initialized declaration. */
21252 tree asm_specification, attributes;
21253 cp_declarator *declarator;
21255 declarator = cp_parser_declarator (parser,
21256 CP_PARSER_DECLARATOR_NAMED,
21257 /*ctor_dtor_or_conv_p=*/NULL,
21258 /*parenthesized_p=*/NULL,
21259 /*member_p=*/false);
21260 attributes = cp_parser_attributes_opt (parser);
21261 asm_specification = cp_parser_asm_specification_opt (parser);
21263 if (declarator == cp_error_declarator)
21264 cp_parser_skip_to_end_of_statement (parser);
21268 tree pushed_scope, auto_node;
21270 decl = start_decl (declarator, &type_specifiers,
21271 SD_INITIALIZED, attributes,
21272 /*prefix_attributes=*/NULL_TREE,
21275 auto_node = type_uses_auto (TREE_TYPE (decl));
21276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21278 if (cp_lexer_next_token_is (parser->lexer,
21280 error ("parenthesized initialization is not allowed in "
21281 "OpenMP %<for%> loop");
21283 /* Trigger an error. */
21284 cp_parser_require (parser, CPP_EQ, "%<=%>");
21286 init = error_mark_node;
21287 cp_parser_skip_to_end_of_statement (parser);
21289 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21290 || type_dependent_expression_p (decl)
21293 bool is_direct_init, is_non_constant_init;
21295 init = cp_parser_initializer (parser,
21297 &is_non_constant_init);
21299 if (auto_node && describable_type (init))
21302 = do_auto_deduction (TREE_TYPE (decl), init,
21305 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21306 && !type_dependent_expression_p (decl))
21310 cp_finish_decl (decl, init, !is_non_constant_init,
21312 LOOKUP_ONLYCONVERTING);
21313 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21316 = tree_cons (NULL, this_pre_body, for_block);
21320 init = pop_stmt_list (this_pre_body);
21321 this_pre_body = NULL_TREE;
21326 cp_lexer_consume_token (parser->lexer);
21327 init = cp_parser_assignment_expression (parser, false, NULL);
21330 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21331 init = error_mark_node;
21333 cp_finish_decl (decl, NULL_TREE,
21334 /*init_const_expr_p=*/false,
21336 LOOKUP_ONLYCONVERTING);
21340 pop_scope (pushed_scope);
21346 /* If parsing a type specifier sequence failed, then
21347 this MUST be a simple expression. */
21348 cp_parser_parse_tentatively (parser);
21349 decl = cp_parser_primary_expression (parser, false, false,
21351 if (!cp_parser_error_occurred (parser)
21354 && CLASS_TYPE_P (TREE_TYPE (decl)))
21358 cp_parser_parse_definitely (parser);
21359 cp_parser_require (parser, CPP_EQ, "%<=%>");
21360 rhs = cp_parser_assignment_expression (parser, false, NULL);
21361 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21363 tf_warning_or_error));
21364 add_private_clause = true;
21369 cp_parser_abort_tentative_parse (parser);
21370 init = cp_parser_expression (parser, false, NULL);
21373 if (TREE_CODE (init) == MODIFY_EXPR
21374 || TREE_CODE (init) == MODOP_EXPR)
21375 real_decl = TREE_OPERAND (init, 0);
21380 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21383 this_pre_body = pop_stmt_list (this_pre_body);
21387 pre_body = push_stmt_list ();
21389 add_stmt (this_pre_body);
21390 pre_body = pop_stmt_list (pre_body);
21393 pre_body = this_pre_body;
21398 if (par_clauses != NULL && real_decl != NULL_TREE)
21401 for (c = par_clauses; *c ; )
21402 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21403 && OMP_CLAUSE_DECL (*c) == real_decl)
21405 error ("%Hiteration variable %qD should not be firstprivate",
21407 *c = OMP_CLAUSE_CHAIN (*c);
21409 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21410 && OMP_CLAUSE_DECL (*c) == real_decl)
21412 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21413 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21414 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21415 OMP_CLAUSE_DECL (l) = real_decl;
21416 OMP_CLAUSE_CHAIN (l) = clauses;
21417 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21419 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21420 CP_OMP_CLAUSE_INFO (*c) = NULL;
21421 add_private_clause = false;
21425 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21426 && OMP_CLAUSE_DECL (*c) == real_decl)
21427 add_private_clause = false;
21428 c = &OMP_CLAUSE_CHAIN (*c);
21432 if (add_private_clause)
21435 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21437 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21438 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21439 && OMP_CLAUSE_DECL (c) == decl)
21441 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21442 && OMP_CLAUSE_DECL (c) == decl)
21443 error ("%Hiteration variable %qD should not be firstprivate",
21445 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21446 && OMP_CLAUSE_DECL (c) == decl)
21447 error ("%Hiteration variable %qD should not be reduction",
21452 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21453 OMP_CLAUSE_DECL (c) = decl;
21454 c = finish_omp_clauses (c);
21457 OMP_CLAUSE_CHAIN (c) = clauses;
21464 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21465 cond = cp_parser_omp_for_cond (parser, decl);
21466 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21471 /* If decl is an iterator, preserve the operator on decl
21472 until finish_omp_for. */
21474 && (type_dependent_expression_p (decl)
21475 || CLASS_TYPE_P (TREE_TYPE (decl))))
21476 incr = cp_parser_omp_for_incr (parser, decl);
21478 incr = cp_parser_expression (parser, false, NULL);
21481 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21482 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21483 /*or_comma=*/false,
21484 /*consume_paren=*/true);
21486 TREE_VEC_ELT (declv, i) = decl;
21487 TREE_VEC_ELT (initv, i) = init;
21488 TREE_VEC_ELT (condv, i) = cond;
21489 TREE_VEC_ELT (incrv, i) = incr;
21491 if (i == collapse - 1)
21494 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21495 in between the collapsed for loops to be still considered perfectly
21496 nested. Hopefully the final version clarifies this.
21497 For now handle (multiple) {'s and empty statements. */
21498 cp_parser_parse_tentatively (parser);
21501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21503 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21505 cp_lexer_consume_token (parser->lexer);
21508 else if (bracecount
21509 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21510 cp_lexer_consume_token (parser->lexer);
21513 loc = cp_lexer_peek_token (parser->lexer)->location;
21514 error ("%Hnot enough collapsed for loops", &loc);
21515 collapse_err = true;
21516 cp_parser_abort_tentative_parse (parser);
21525 cp_parser_parse_definitely (parser);
21526 nbraces += bracecount;
21530 /* Note that we saved the original contents of this flag when we entered
21531 the structured block, and so we don't need to re-save it here. */
21532 parser->in_statement = IN_OMP_FOR;
21534 /* Note that the grammar doesn't call for a structured block here,
21535 though the loop as a whole is a structured block. */
21536 body = push_stmt_list ();
21537 cp_parser_statement (parser, NULL_TREE, false, NULL);
21538 body = pop_stmt_list (body);
21540 if (declv == NULL_TREE)
21543 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21544 pre_body, clauses);
21548 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21550 cp_lexer_consume_token (parser->lexer);
21553 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21554 cp_lexer_consume_token (parser->lexer);
21559 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21560 error ("%Hcollapsed loops not perfectly nested", &loc);
21562 collapse_err = true;
21563 cp_parser_statement_seq_opt (parser, NULL);
21564 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21570 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21571 for_block = TREE_CHAIN (for_block);
21578 #pragma omp for for-clause[optseq] new-line
21581 #define OMP_FOR_CLAUSE_MASK \
21582 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21583 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21584 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21585 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21586 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21587 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21588 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21589 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21592 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21594 tree clauses, sb, ret;
21597 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21598 "#pragma omp for", pragma_tok);
21600 sb = begin_omp_structured_block ();
21601 save = cp_parser_begin_omp_structured_block (parser);
21603 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21605 cp_parser_end_omp_structured_block (parser, save);
21606 add_stmt (finish_omp_structured_block (sb));
21612 # pragma omp master new-line
21613 structured-block */
21616 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21618 cp_parser_require_pragma_eol (parser, pragma_tok);
21619 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21623 # pragma omp ordered new-line
21624 structured-block */
21627 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21629 cp_parser_require_pragma_eol (parser, pragma_tok);
21630 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21636 { section-sequence }
21639 section-directive[opt] structured-block
21640 section-sequence section-directive structured-block */
21643 cp_parser_omp_sections_scope (cp_parser *parser)
21645 tree stmt, substmt;
21646 bool error_suppress = false;
21649 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21652 stmt = push_stmt_list ();
21654 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21658 substmt = begin_omp_structured_block ();
21659 save = cp_parser_begin_omp_structured_block (parser);
21663 cp_parser_statement (parser, NULL_TREE, false, NULL);
21665 tok = cp_lexer_peek_token (parser->lexer);
21666 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21668 if (tok->type == CPP_CLOSE_BRACE)
21670 if (tok->type == CPP_EOF)
21674 cp_parser_end_omp_structured_block (parser, save);
21675 substmt = finish_omp_structured_block (substmt);
21676 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21677 add_stmt (substmt);
21682 tok = cp_lexer_peek_token (parser->lexer);
21683 if (tok->type == CPP_CLOSE_BRACE)
21685 if (tok->type == CPP_EOF)
21688 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21690 cp_lexer_consume_token (parser->lexer);
21691 cp_parser_require_pragma_eol (parser, tok);
21692 error_suppress = false;
21694 else if (!error_suppress)
21696 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21697 error_suppress = true;
21700 substmt = cp_parser_omp_structured_block (parser);
21701 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21702 add_stmt (substmt);
21704 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21706 substmt = pop_stmt_list (stmt);
21708 stmt = make_node (OMP_SECTIONS);
21709 TREE_TYPE (stmt) = void_type_node;
21710 OMP_SECTIONS_BODY (stmt) = substmt;
21717 # pragma omp sections sections-clause[optseq] newline
21720 #define OMP_SECTIONS_CLAUSE_MASK \
21721 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21722 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21723 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21724 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21725 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21728 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21732 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21733 "#pragma omp sections", pragma_tok);
21735 ret = cp_parser_omp_sections_scope (parser);
21737 OMP_SECTIONS_CLAUSES (ret) = clauses;
21743 # pragma parallel parallel-clause new-line
21744 # pragma parallel for parallel-for-clause new-line
21745 # pragma parallel sections parallel-sections-clause new-line */
21747 #define OMP_PARALLEL_CLAUSE_MASK \
21748 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21749 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21750 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21751 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21752 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21753 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21754 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21755 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21758 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21760 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21761 const char *p_name = "#pragma omp parallel";
21762 tree stmt, clauses, par_clause, ws_clause, block;
21763 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21768 cp_lexer_consume_token (parser->lexer);
21769 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21770 p_name = "#pragma omp parallel for";
21771 mask |= OMP_FOR_CLAUSE_MASK;
21772 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21774 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21776 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21777 const char *p = IDENTIFIER_POINTER (id);
21778 if (strcmp (p, "sections") == 0)
21780 cp_lexer_consume_token (parser->lexer);
21781 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21782 p_name = "#pragma omp parallel sections";
21783 mask |= OMP_SECTIONS_CLAUSE_MASK;
21784 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21788 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21789 block = begin_omp_parallel ();
21790 save = cp_parser_begin_omp_structured_block (parser);
21794 case PRAGMA_OMP_PARALLEL:
21795 cp_parser_statement (parser, NULL_TREE, false, NULL);
21796 par_clause = clauses;
21799 case PRAGMA_OMP_PARALLEL_FOR:
21800 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21801 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21804 case PRAGMA_OMP_PARALLEL_SECTIONS:
21805 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21806 stmt = cp_parser_omp_sections_scope (parser);
21808 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21812 gcc_unreachable ();
21815 cp_parser_end_omp_structured_block (parser, save);
21816 stmt = finish_omp_parallel (par_clause, block);
21817 if (p_kind != PRAGMA_OMP_PARALLEL)
21818 OMP_PARALLEL_COMBINED (stmt) = 1;
21823 # pragma omp single single-clause[optseq] new-line
21824 structured-block */
21826 #define OMP_SINGLE_CLAUSE_MASK \
21827 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21828 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21829 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21830 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21833 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21835 tree stmt = make_node (OMP_SINGLE);
21836 TREE_TYPE (stmt) = void_type_node;
21838 OMP_SINGLE_CLAUSES (stmt)
21839 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21840 "#pragma omp single", pragma_tok);
21841 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21843 return add_stmt (stmt);
21847 # pragma omp task task-clause[optseq] new-line
21848 structured-block */
21850 #define OMP_TASK_CLAUSE_MASK \
21851 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21852 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21853 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21854 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21855 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21856 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21859 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21861 tree clauses, block;
21864 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21865 "#pragma omp task", pragma_tok);
21866 block = begin_omp_task ();
21867 save = cp_parser_begin_omp_structured_block (parser);
21868 cp_parser_statement (parser, NULL_TREE, false, NULL);
21869 cp_parser_end_omp_structured_block (parser, save);
21870 return finish_omp_task (clauses, block);
21874 # pragma omp taskwait new-line */
21877 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21879 cp_parser_require_pragma_eol (parser, pragma_tok);
21880 finish_omp_taskwait ();
21884 # pragma omp threadprivate (variable-list) */
21887 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21891 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21892 cp_parser_require_pragma_eol (parser, pragma_tok);
21894 finish_omp_threadprivate (vars);
21897 /* Main entry point to OpenMP statement pragmas. */
21900 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21904 switch (pragma_tok->pragma_kind)
21906 case PRAGMA_OMP_ATOMIC:
21907 cp_parser_omp_atomic (parser, pragma_tok);
21909 case PRAGMA_OMP_CRITICAL:
21910 stmt = cp_parser_omp_critical (parser, pragma_tok);
21912 case PRAGMA_OMP_FOR:
21913 stmt = cp_parser_omp_for (parser, pragma_tok);
21915 case PRAGMA_OMP_MASTER:
21916 stmt = cp_parser_omp_master (parser, pragma_tok);
21918 case PRAGMA_OMP_ORDERED:
21919 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21921 case PRAGMA_OMP_PARALLEL:
21922 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21924 case PRAGMA_OMP_SECTIONS:
21925 stmt = cp_parser_omp_sections (parser, pragma_tok);
21927 case PRAGMA_OMP_SINGLE:
21928 stmt = cp_parser_omp_single (parser, pragma_tok);
21930 case PRAGMA_OMP_TASK:
21931 stmt = cp_parser_omp_task (parser, pragma_tok);
21934 gcc_unreachable ();
21938 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21943 static GTY (()) cp_parser *the_parser;
21946 /* Special handling for the first token or line in the file. The first
21947 thing in the file might be #pragma GCC pch_preprocess, which loads a
21948 PCH file, which is a GC collection point. So we need to handle this
21949 first pragma without benefit of an existing lexer structure.
21951 Always returns one token to the caller in *FIRST_TOKEN. This is
21952 either the true first token of the file, or the first token after
21953 the initial pragma. */
21956 cp_parser_initial_pragma (cp_token *first_token)
21960 cp_lexer_get_preprocessor_token (NULL, first_token);
21961 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21964 cp_lexer_get_preprocessor_token (NULL, first_token);
21965 if (first_token->type == CPP_STRING)
21967 name = first_token->u.value;
21969 cp_lexer_get_preprocessor_token (NULL, first_token);
21970 if (first_token->type != CPP_PRAGMA_EOL)
21971 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21972 &first_token->location);
21975 error ("%Hexpected string literal", &first_token->location);
21977 /* Skip to the end of the pragma. */
21978 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21979 cp_lexer_get_preprocessor_token (NULL, first_token);
21981 /* Now actually load the PCH file. */
21983 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21985 /* Read one more token to return to our caller. We have to do this
21986 after reading the PCH file in, since its pointers have to be
21988 cp_lexer_get_preprocessor_token (NULL, first_token);
21991 /* Normal parsing of a pragma token. Here we can (and must) use the
21995 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21997 cp_token *pragma_tok;
22000 pragma_tok = cp_lexer_consume_token (parser->lexer);
22001 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22002 parser->lexer->in_pragma = true;
22004 id = pragma_tok->pragma_kind;
22007 case PRAGMA_GCC_PCH_PREPROCESS:
22008 error ("%H%<#pragma GCC pch_preprocess%> must be first",
22009 &pragma_tok->location);
22012 case PRAGMA_OMP_BARRIER:
22015 case pragma_compound:
22016 cp_parser_omp_barrier (parser, pragma_tok);
22019 error ("%H%<#pragma omp barrier%> may only be "
22020 "used in compound statements", &pragma_tok->location);
22027 case PRAGMA_OMP_FLUSH:
22030 case pragma_compound:
22031 cp_parser_omp_flush (parser, pragma_tok);
22034 error ("%H%<#pragma omp flush%> may only be "
22035 "used in compound statements", &pragma_tok->location);
22042 case PRAGMA_OMP_TASKWAIT:
22045 case pragma_compound:
22046 cp_parser_omp_taskwait (parser, pragma_tok);
22049 error ("%H%<#pragma omp taskwait%> may only be "
22050 "used in compound statements",
22051 &pragma_tok->location);
22058 case PRAGMA_OMP_THREADPRIVATE:
22059 cp_parser_omp_threadprivate (parser, pragma_tok);
22062 case PRAGMA_OMP_ATOMIC:
22063 case PRAGMA_OMP_CRITICAL:
22064 case PRAGMA_OMP_FOR:
22065 case PRAGMA_OMP_MASTER:
22066 case PRAGMA_OMP_ORDERED:
22067 case PRAGMA_OMP_PARALLEL:
22068 case PRAGMA_OMP_SECTIONS:
22069 case PRAGMA_OMP_SINGLE:
22070 case PRAGMA_OMP_TASK:
22071 if (context == pragma_external)
22073 cp_parser_omp_construct (parser, pragma_tok);
22076 case PRAGMA_OMP_SECTION:
22077 error ("%H%<#pragma omp section%> may only be used in "
22078 "%<#pragma omp sections%> construct", &pragma_tok->location);
22082 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22083 c_invoke_pragma_handler (id);
22087 cp_parser_error (parser, "expected declaration specifiers");
22091 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22095 /* The interface the pragma parsers have to the lexer. */
22098 pragma_lex (tree *value)
22101 enum cpp_ttype ret;
22103 tok = cp_lexer_peek_token (the_parser->lexer);
22106 *value = tok->u.value;
22108 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22110 else if (ret == CPP_STRING)
22111 *value = cp_parser_string_literal (the_parser, false, false);
22114 cp_lexer_consume_token (the_parser->lexer);
22115 if (ret == CPP_KEYWORD)
22123 /* External interface. */
22125 /* Parse one entire translation unit. */
22128 c_parse_file (void)
22130 bool error_occurred;
22131 static bool already_called = false;
22133 if (already_called)
22135 sorry ("inter-module optimizations not implemented for C++");
22138 already_called = true;
22140 the_parser = cp_parser_new ();
22141 push_deferring_access_checks (flag_access_control
22142 ? dk_no_deferred : dk_no_check);
22143 error_occurred = cp_parser_translation_unit (the_parser);
22147 #include "gt-cp-parser.h"