2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
89 /* The location at which this token was found. */
93 /* We use a stack of token pointer for saving token sets. */
94 typedef struct cp_token *cp_token_position;
95 DEF_VEC_P (cp_token_position);
96 DEF_VEC_ALLOC_P (cp_token_position,heap);
98 static cp_token eof_token =
100 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
104 /* The cp_lexer structure represents the C++ lexer. It is responsible
105 for managing the token stream from the preprocessor and supplying
106 it to the parser. Tokens are never added to the cp_lexer after
109 typedef struct cp_lexer GTY (())
111 /* The memory allocated for the buffer. NULL if this lexer does not
112 own the token buffer. */
113 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
114 /* If the lexer owns the buffer, this is the number of tokens in the
116 size_t buffer_length;
118 /* A pointer just past the last available token. The tokens
119 in this lexer are [buffer, last_token). */
120 cp_token_position GTY ((skip)) last_token;
122 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
123 no more available tokens. */
124 cp_token_position GTY ((skip)) next_token;
126 /* A stack indicating positions at which cp_lexer_save_tokens was
127 called. The top entry is the most recent position at which we
128 began saving tokens. If the stack is non-empty, we are saving
130 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
132 /* The next lexer in a linked list of lexers. */
133 struct cp_lexer *next;
135 /* True if we should output debugging information. */
138 /* True if we're in the context of parsing a pragma, and should not
139 increment past the end-of-line marker. */
143 /* cp_token_cache is a range of tokens. There is no need to represent
144 allocate heap memory for it, since tokens are never removed from the
145 lexer's array. There is also no need for the GC to walk through
146 a cp_token_cache, since everything in here is referenced through
149 typedef struct cp_token_cache GTY(())
151 /* The beginning of the token range. */
152 cp_token * GTY((skip)) first;
154 /* Points immediately after the last token in the range. */
155 cp_token * GTY ((skip)) last;
160 static cp_lexer *cp_lexer_new_main
162 static cp_lexer *cp_lexer_new_from_tokens
163 (cp_token_cache *tokens);
164 static void cp_lexer_destroy
166 static int cp_lexer_saving_tokens
168 static cp_token_position cp_lexer_token_position
170 static cp_token *cp_lexer_token_at
171 (cp_lexer *, cp_token_position);
172 static void cp_lexer_get_preprocessor_token
173 (cp_lexer *, cp_token *);
174 static inline cp_token *cp_lexer_peek_token
176 static cp_token *cp_lexer_peek_nth_token
177 (cp_lexer *, size_t);
178 static inline bool cp_lexer_next_token_is
179 (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_not
181 (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_keyword
183 (cp_lexer *, enum rid);
184 static cp_token *cp_lexer_consume_token
186 static void cp_lexer_purge_token
188 static void cp_lexer_purge_tokens_after
189 (cp_lexer *, cp_token_position);
190 static void cp_lexer_save_tokens
192 static void cp_lexer_commit_tokens
194 static void cp_lexer_rollback_tokens
196 #ifdef ENABLE_CHECKING
197 static void cp_lexer_print_token
198 (FILE *, cp_token *);
199 static inline bool cp_lexer_debugging_p
201 static void cp_lexer_start_debugging
202 (cp_lexer *) ATTRIBUTE_UNUSED;
203 static void cp_lexer_stop_debugging
204 (cp_lexer *) ATTRIBUTE_UNUSED;
206 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
207 about passing NULL to functions that require non-NULL arguments
208 (fputs, fprintf). It will never be used, so all we need is a value
209 of the right type that's guaranteed not to be NULL. */
210 #define cp_lexer_debug_stream stdout
211 #define cp_lexer_print_token(str, tok) (void) 0
212 #define cp_lexer_debugging_p(lexer) 0
213 #endif /* ENABLE_CHECKING */
215 static cp_token_cache *cp_token_cache_new
216 (cp_token *, cp_token *);
218 static void cp_parser_initial_pragma
221 /* Manifest constants. */
222 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
223 #define CP_SAVED_TOKEN_STACK 5
225 /* A token type for keywords, as opposed to ordinary identifiers. */
226 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
228 /* A token type for template-ids. If a template-id is processed while
229 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
230 the value of the CPP_TEMPLATE_ID is whatever was returned by
231 cp_parser_template_id. */
232 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
234 /* A token type for nested-name-specifiers. If a
235 nested-name-specifier is processed while parsing tentatively, it is
236 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
237 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
238 cp_parser_nested_name_specifier_opt. */
239 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
241 /* A token type for tokens that are not tokens at all; these are used
242 to represent slots in the array where there used to be a token
243 that has now been deleted. */
244 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
246 /* The number of token types, including C++-specific ones. */
247 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
251 #ifdef ENABLE_CHECKING
252 /* The stream to which debugging output should be written. */
253 static FILE *cp_lexer_debug_stream;
254 #endif /* ENABLE_CHECKING */
256 /* Create a new main C++ lexer, the lexer that gets tokens from the
260 cp_lexer_new_main (void)
262 cp_token first_token;
269 /* It's possible that parsing the first pragma will load a PCH file,
270 which is a GC collection point. So we have to do that before
271 allocating any memory. */
272 cp_parser_initial_pragma (&first_token);
274 c_common_no_more_pch ();
276 /* Allocate the memory. */
277 lexer = GGC_CNEW (cp_lexer);
279 #ifdef ENABLE_CHECKING
280 /* Initially we are not debugging. */
281 lexer->debugging_p = false;
282 #endif /* ENABLE_CHECKING */
283 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
284 CP_SAVED_TOKEN_STACK);
286 /* Create the buffer. */
287 alloc = CP_LEXER_BUFFER_SIZE;
288 buffer = GGC_NEWVEC (cp_token, alloc);
290 /* Put the first token in the buffer. */
295 /* Get the remaining tokens from the preprocessor. */
296 while (pos->type != CPP_EOF)
303 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
304 pos = buffer + space;
306 cp_lexer_get_preprocessor_token (lexer, pos);
308 lexer->buffer = buffer;
309 lexer->buffer_length = alloc - space;
310 lexer->last_token = pos;
311 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
313 /* Subsequent preprocessor diagnostics should use compiler
314 diagnostic functions to get the compiler source location. */
315 cpp_get_options (parse_in)->client_diagnostic = true;
316 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
318 gcc_assert (lexer->next_token->type != CPP_PURGED);
322 /* Create a new lexer whose token stream is primed with the tokens in
323 CACHE. When these tokens are exhausted, no new tokens will be read. */
326 cp_lexer_new_from_tokens (cp_token_cache *cache)
328 cp_token *first = cache->first;
329 cp_token *last = cache->last;
330 cp_lexer *lexer = GGC_CNEW (cp_lexer);
332 /* We do not own the buffer. */
333 lexer->buffer = NULL;
334 lexer->buffer_length = 0;
335 lexer->next_token = first == last ? &eof_token : first;
336 lexer->last_token = last;
338 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
339 CP_SAVED_TOKEN_STACK);
341 #ifdef ENABLE_CHECKING
342 /* Initially we are not debugging. */
343 lexer->debugging_p = false;
346 gcc_assert (lexer->next_token->type != CPP_PURGED);
350 /* Frees all resources associated with LEXER. */
353 cp_lexer_destroy (cp_lexer *lexer)
356 ggc_free (lexer->buffer);
357 VEC_free (cp_token_position, heap, lexer->saved_tokens);
361 /* Returns nonzero if debugging information should be output. */
363 #ifdef ENABLE_CHECKING
366 cp_lexer_debugging_p (cp_lexer *lexer)
368 return lexer->debugging_p;
371 #endif /* ENABLE_CHECKING */
373 static inline cp_token_position
374 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
376 gcc_assert (!previous_p || lexer->next_token != &eof_token);
378 return lexer->next_token - previous_p;
381 static inline cp_token *
382 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
387 /* nonzero if we are presently saving tokens. */
390 cp_lexer_saving_tokens (const cp_lexer* lexer)
392 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
395 /* Store the next token from the preprocessor in *TOKEN. Return true
396 if we reach EOF. If LEXER is NULL, assume we are handling an
397 initial #pragma pch_preprocess, and thus want the lexer to return
398 processed strings. */
401 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
403 static int is_extern_c = 0;
405 /* Get a new token from the preprocessor. */
407 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
408 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
409 token->keyword = RID_MAX;
410 token->pragma_kind = PRAGMA_NONE;
411 token->in_system_header = in_system_header;
413 /* On some systems, some header files are surrounded by an
414 implicit extern "C" block. Set a flag in the token if it
415 comes from such a header. */
416 is_extern_c += pending_lang_change;
417 pending_lang_change = 0;
418 token->implicit_extern_c = is_extern_c > 0;
420 /* Check to see if this token is a keyword. */
421 if (token->type == CPP_NAME)
423 if (C_IS_RESERVED_WORD (token->u.value))
425 /* Mark this token as a keyword. */
426 token->type = CPP_KEYWORD;
427 /* Record which keyword. */
428 token->keyword = C_RID_CODE (token->u.value);
429 /* Update the value. Some keywords are mapped to particular
430 entities, rather than simply having the value of the
431 corresponding IDENTIFIER_NODE. For example, `__const' is
432 mapped to `const'. */
433 token->u.value = ridpointers[token->keyword];
437 if (warn_cxx0x_compat
438 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
439 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
441 /* Warn about the C++0x keyword (but still treat it as
443 warning (OPT_Wc__0x_compat,
444 "identifier %<%s%> will become a keyword in C++0x",
445 IDENTIFIER_POINTER (token->u.value));
447 /* Clear out the C_RID_CODE so we don't warn about this
448 particular identifier-turned-keyword again. */
449 C_SET_RID_CODE (token->u.value, RID_MAX);
452 token->ambiguous_p = false;
453 token->keyword = RID_MAX;
456 /* Handle Objective-C++ keywords. */
457 else if (token->type == CPP_AT_NAME)
459 token->type = CPP_KEYWORD;
460 switch (C_RID_CODE (token->u.value))
462 /* Map 'class' to '@class', 'private' to '@private', etc. */
463 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
464 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
465 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
466 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
467 case RID_THROW: token->keyword = RID_AT_THROW; break;
468 case RID_TRY: token->keyword = RID_AT_TRY; break;
469 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
470 default: token->keyword = C_RID_CODE (token->u.value);
473 else if (token->type == CPP_PRAGMA)
475 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
476 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
477 token->u.value = NULL_TREE;
481 /* Update the globals input_location and in_system_header and the
482 input file stack from TOKEN. */
484 cp_lexer_set_source_position_from_token (cp_token *token)
486 if (token->type != CPP_EOF)
488 input_location = token->location;
489 in_system_header = token->in_system_header;
493 /* Return a pointer to the next token in the token stream, but do not
496 static inline cp_token *
497 cp_lexer_peek_token (cp_lexer *lexer)
499 if (cp_lexer_debugging_p (lexer))
501 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
502 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
503 putc ('\n', cp_lexer_debug_stream);
505 return lexer->next_token;
508 /* Return true if the next token has the indicated TYPE. */
511 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
513 return cp_lexer_peek_token (lexer)->type == type;
516 /* Return true if the next token does not have the indicated TYPE. */
519 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
521 return !cp_lexer_next_token_is (lexer, type);
524 /* Return true if the next token is the indicated KEYWORD. */
527 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
529 return cp_lexer_peek_token (lexer)->keyword == keyword;
532 /* Return true if the next token is not the indicated KEYWORD. */
535 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
537 return cp_lexer_peek_token (lexer)->keyword != keyword;
540 /* Return true if the next token is a keyword for a decl-specifier. */
543 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
547 token = cp_lexer_peek_token (lexer);
548 switch (token->keyword)
550 /* auto specifier: storage-class-specifier in C++,
551 simple-type-specifier in C++0x. */
553 /* Storage classes. */
559 /* Elaborated type specifiers. */
565 /* Simple type specifiers. */
579 /* GNU extensions. */
582 /* C++0x extensions. */
591 /* Return a pointer to the Nth token in the token stream. If N is 1,
592 then this is precisely equivalent to cp_lexer_peek_token (except
593 that it is not inline). One would like to disallow that case, but
594 there is one case (cp_parser_nth_token_starts_template_id) where
595 the caller passes a variable for N and it might be 1. */
598 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
602 /* N is 1-based, not zero-based. */
605 if (cp_lexer_debugging_p (lexer))
606 fprintf (cp_lexer_debug_stream,
607 "cp_lexer: peeking ahead %ld at token: ", (long)n);
610 token = lexer->next_token;
611 gcc_assert (!n || token != &eof_token);
615 if (token == lexer->last_token)
621 if (token->type != CPP_PURGED)
625 if (cp_lexer_debugging_p (lexer))
627 cp_lexer_print_token (cp_lexer_debug_stream, token);
628 putc ('\n', cp_lexer_debug_stream);
634 /* Return the next token, and advance the lexer's next_token pointer
635 to point to the next non-purged token. */
638 cp_lexer_consume_token (cp_lexer* lexer)
640 cp_token *token = lexer->next_token;
642 gcc_assert (token != &eof_token);
643 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
648 if (lexer->next_token == lexer->last_token)
650 lexer->next_token = &eof_token;
655 while (lexer->next_token->type == CPP_PURGED);
657 cp_lexer_set_source_position_from_token (token);
659 /* Provide debugging output. */
660 if (cp_lexer_debugging_p (lexer))
662 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
663 cp_lexer_print_token (cp_lexer_debug_stream, token);
664 putc ('\n', cp_lexer_debug_stream);
670 /* Permanently remove the next token from the token stream, and
671 advance the next_token pointer to refer to the next non-purged
675 cp_lexer_purge_token (cp_lexer *lexer)
677 cp_token *tok = lexer->next_token;
679 gcc_assert (tok != &eof_token);
680 tok->type = CPP_PURGED;
681 tok->location = UNKNOWN_LOCATION;
682 tok->u.value = NULL_TREE;
683 tok->keyword = RID_MAX;
688 if (tok == lexer->last_token)
694 while (tok->type == CPP_PURGED);
695 lexer->next_token = tok;
698 /* Permanently remove all tokens after TOK, up to, but not
699 including, the token that will be returned next by
700 cp_lexer_peek_token. */
703 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
705 cp_token *peek = lexer->next_token;
707 if (peek == &eof_token)
708 peek = lexer->last_token;
710 gcc_assert (tok < peek);
712 for ( tok += 1; tok != peek; tok += 1)
714 tok->type = CPP_PURGED;
715 tok->location = UNKNOWN_LOCATION;
716 tok->u.value = NULL_TREE;
717 tok->keyword = RID_MAX;
721 /* Begin saving tokens. All tokens consumed after this point will be
725 cp_lexer_save_tokens (cp_lexer* lexer)
727 /* Provide debugging output. */
728 if (cp_lexer_debugging_p (lexer))
729 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
731 VEC_safe_push (cp_token_position, heap,
732 lexer->saved_tokens, lexer->next_token);
735 /* Commit to the portion of the token stream most recently saved. */
738 cp_lexer_commit_tokens (cp_lexer* lexer)
740 /* Provide debugging output. */
741 if (cp_lexer_debugging_p (lexer))
742 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
744 VEC_pop (cp_token_position, lexer->saved_tokens);
747 /* Return all tokens saved since the last call to cp_lexer_save_tokens
748 to the token stream. Stop saving tokens. */
751 cp_lexer_rollback_tokens (cp_lexer* lexer)
753 /* Provide debugging output. */
754 if (cp_lexer_debugging_p (lexer))
755 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
757 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
760 /* Print a representation of the TOKEN on the STREAM. */
762 #ifdef ENABLE_CHECKING
765 cp_lexer_print_token (FILE * stream, cp_token *token)
767 /* We don't use cpp_type2name here because the parser defines
768 a few tokens of its own. */
769 static const char *const token_names[] = {
770 /* cpplib-defined token types */
776 /* C++ parser token types - see "Manifest constants", above. */
779 "NESTED_NAME_SPECIFIER",
783 /* If we have a name for the token, print it out. Otherwise, we
784 simply give the numeric code. */
785 gcc_assert (token->type < ARRAY_SIZE(token_names));
786 fputs (token_names[token->type], stream);
788 /* For some tokens, print the associated data. */
792 /* Some keywords have a value that is not an IDENTIFIER_NODE.
793 For example, `struct' is mapped to an INTEGER_CST. */
794 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
796 /* else fall through */
798 fputs (IDENTIFIER_POINTER (token->u.value), stream);
805 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
813 /* Start emitting debugging information. */
816 cp_lexer_start_debugging (cp_lexer* lexer)
818 lexer->debugging_p = true;
821 /* Stop emitting debugging information. */
824 cp_lexer_stop_debugging (cp_lexer* lexer)
826 lexer->debugging_p = false;
829 #endif /* ENABLE_CHECKING */
831 /* Create a new cp_token_cache, representing a range of tokens. */
833 static cp_token_cache *
834 cp_token_cache_new (cp_token *first, cp_token *last)
836 cp_token_cache *cache = GGC_NEW (cp_token_cache);
837 cache->first = first;
843 /* Decl-specifiers. */
845 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
848 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
850 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
855 /* Nothing other than the parser should be creating declarators;
856 declarators are a semi-syntactic representation of C++ entities.
857 Other parts of the front end that need to create entities (like
858 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
860 static cp_declarator *make_call_declarator
861 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
862 static cp_declarator *make_array_declarator
863 (cp_declarator *, tree);
864 static cp_declarator *make_pointer_declarator
865 (cp_cv_quals, cp_declarator *);
866 static cp_declarator *make_reference_declarator
867 (cp_cv_quals, cp_declarator *, bool);
868 static cp_parameter_declarator *make_parameter_declarator
869 (cp_decl_specifier_seq *, cp_declarator *, tree);
870 static cp_declarator *make_ptrmem_declarator
871 (cp_cv_quals, tree, cp_declarator *);
873 /* An erroneous declarator. */
874 static cp_declarator *cp_error_declarator;
876 /* The obstack on which declarators and related data structures are
878 static struct obstack declarator_obstack;
880 /* Alloc BYTES from the declarator memory pool. */
883 alloc_declarator (size_t bytes)
885 return obstack_alloc (&declarator_obstack, bytes);
888 /* Allocate a declarator of the indicated KIND. Clear fields that are
889 common to all declarators. */
891 static cp_declarator *
892 make_declarator (cp_declarator_kind kind)
894 cp_declarator *declarator;
896 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
897 declarator->kind = kind;
898 declarator->attributes = NULL_TREE;
899 declarator->declarator = NULL;
900 declarator->parameter_pack_p = false;
905 /* Make a declarator for a generalized identifier. If
906 QUALIFYING_SCOPE is non-NULL, the identifier is
907 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
908 UNQUALIFIED_NAME. SFK indicates the kind of special function this
911 static cp_declarator *
912 make_id_declarator (tree qualifying_scope, tree unqualified_name,
913 special_function_kind sfk)
915 cp_declarator *declarator;
917 /* It is valid to write:
919 class C { void f(); };
923 The standard is not clear about whether `typedef const C D' is
924 legal; as of 2002-09-15 the committee is considering that
925 question. EDG 3.0 allows that syntax. Therefore, we do as
927 if (qualifying_scope && TYPE_P (qualifying_scope))
928 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
930 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
931 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
932 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
934 declarator = make_declarator (cdk_id);
935 declarator->u.id.qualifying_scope = qualifying_scope;
936 declarator->u.id.unqualified_name = unqualified_name;
937 declarator->u.id.sfk = sfk;
942 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
943 of modifiers such as const or volatile to apply to the pointer
944 type, represented as identifiers. */
947 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
949 cp_declarator *declarator;
951 declarator = make_declarator (cdk_pointer);
952 declarator->declarator = target;
953 declarator->u.pointer.qualifiers = cv_qualifiers;
954 declarator->u.pointer.class_type = NULL_TREE;
957 declarator->parameter_pack_p = target->parameter_pack_p;
958 target->parameter_pack_p = false;
961 declarator->parameter_pack_p = false;
966 /* Like make_pointer_declarator -- but for references. */
969 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
972 cp_declarator *declarator;
974 declarator = make_declarator (cdk_reference);
975 declarator->declarator = target;
976 declarator->u.reference.qualifiers = cv_qualifiers;
977 declarator->u.reference.rvalue_ref = rvalue_ref;
980 declarator->parameter_pack_p = target->parameter_pack_p;
981 target->parameter_pack_p = false;
984 declarator->parameter_pack_p = false;
989 /* Like make_pointer_declarator -- but for a pointer to a non-static
990 member of CLASS_TYPE. */
993 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
994 cp_declarator *pointee)
996 cp_declarator *declarator;
998 declarator = make_declarator (cdk_ptrmem);
999 declarator->declarator = pointee;
1000 declarator->u.pointer.qualifiers = cv_qualifiers;
1001 declarator->u.pointer.class_type = class_type;
1005 declarator->parameter_pack_p = pointee->parameter_pack_p;
1006 pointee->parameter_pack_p = false;
1009 declarator->parameter_pack_p = false;
1014 /* Make a declarator for the function given by TARGET, with the
1015 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1016 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1017 indicates what exceptions can be thrown. */
1020 make_call_declarator (cp_declarator *target,
1021 cp_parameter_declarator *parms,
1022 cp_cv_quals cv_qualifiers,
1023 tree exception_specification)
1025 cp_declarator *declarator;
1027 declarator = make_declarator (cdk_function);
1028 declarator->declarator = target;
1029 declarator->u.function.parameters = parms;
1030 declarator->u.function.qualifiers = cv_qualifiers;
1031 declarator->u.function.exception_specification = exception_specification;
1034 declarator->parameter_pack_p = target->parameter_pack_p;
1035 target->parameter_pack_p = false;
1038 declarator->parameter_pack_p = false;
1043 /* Make a declarator for an array of BOUNDS elements, each of which is
1044 defined by ELEMENT. */
1047 make_array_declarator (cp_declarator *element, tree bounds)
1049 cp_declarator *declarator;
1051 declarator = make_declarator (cdk_array);
1052 declarator->declarator = element;
1053 declarator->u.array.bounds = bounds;
1056 declarator->parameter_pack_p = element->parameter_pack_p;
1057 element->parameter_pack_p = false;
1060 declarator->parameter_pack_p = false;
1065 /* Determine whether the declarator we've seen so far can be a
1066 parameter pack, when followed by an ellipsis. */
1068 declarator_can_be_parameter_pack (cp_declarator *declarator)
1070 /* Search for a declarator name, or any other declarator that goes
1071 after the point where the ellipsis could appear in a parameter
1072 pack. If we find any of these, then this declarator can not be
1073 made into a parameter pack. */
1075 while (declarator && !found)
1077 switch ((int)declarator->kind)
1088 declarator = declarator->declarator;
1096 cp_parameter_declarator *no_parameters;
1098 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1099 DECLARATOR and DEFAULT_ARGUMENT. */
1101 cp_parameter_declarator *
1102 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1103 cp_declarator *declarator,
1104 tree default_argument)
1106 cp_parameter_declarator *parameter;
1108 parameter = ((cp_parameter_declarator *)
1109 alloc_declarator (sizeof (cp_parameter_declarator)));
1110 parameter->next = NULL;
1111 if (decl_specifiers)
1112 parameter->decl_specifiers = *decl_specifiers;
1114 clear_decl_specs (¶meter->decl_specifiers);
1115 parameter->declarator = declarator;
1116 parameter->default_argument = default_argument;
1117 parameter->ellipsis_p = false;
1122 /* Returns true iff DECLARATOR is a declaration for a function. */
1125 function_declarator_p (const cp_declarator *declarator)
1129 if (declarator->kind == cdk_function
1130 && declarator->declarator->kind == cdk_id)
1132 if (declarator->kind == cdk_id
1133 || declarator->kind == cdk_error)
1135 declarator = declarator->declarator;
1145 A cp_parser parses the token stream as specified by the C++
1146 grammar. Its job is purely parsing, not semantic analysis. For
1147 example, the parser breaks the token stream into declarators,
1148 expressions, statements, and other similar syntactic constructs.
1149 It does not check that the types of the expressions on either side
1150 of an assignment-statement are compatible, or that a function is
1151 not declared with a parameter of type `void'.
1153 The parser invokes routines elsewhere in the compiler to perform
1154 semantic analysis and to build up the abstract syntax tree for the
1157 The parser (and the template instantiation code, which is, in a
1158 way, a close relative of parsing) are the only parts of the
1159 compiler that should be calling push_scope and pop_scope, or
1160 related functions. The parser (and template instantiation code)
1161 keeps track of what scope is presently active; everything else
1162 should simply honor that. (The code that generates static
1163 initializers may also need to set the scope, in order to check
1164 access control correctly when emitting the initializers.)
1169 The parser is of the standard recursive-descent variety. Upcoming
1170 tokens in the token stream are examined in order to determine which
1171 production to use when parsing a non-terminal. Some C++ constructs
1172 require arbitrary look ahead to disambiguate. For example, it is
1173 impossible, in the general case, to tell whether a statement is an
1174 expression or declaration without scanning the entire statement.
1175 Therefore, the parser is capable of "parsing tentatively." When the
1176 parser is not sure what construct comes next, it enters this mode.
1177 Then, while we attempt to parse the construct, the parser queues up
1178 error messages, rather than issuing them immediately, and saves the
1179 tokens it consumes. If the construct is parsed successfully, the
1180 parser "commits", i.e., it issues any queued error messages and
1181 the tokens that were being preserved are permanently discarded.
1182 If, however, the construct is not parsed successfully, the parser
1183 rolls back its state completely so that it can resume parsing using
1184 a different alternative.
1189 The performance of the parser could probably be improved substantially.
1190 We could often eliminate the need to parse tentatively by looking ahead
1191 a little bit. In some places, this approach might not entirely eliminate
1192 the need to parse tentatively, but it might still speed up the average
1195 /* Flags that are passed to some parsing functions. These values can
1196 be bitwise-ored together. */
1198 typedef enum cp_parser_flags
1201 CP_PARSER_FLAGS_NONE = 0x0,
1202 /* The construct is optional. If it is not present, then no error
1203 should be issued. */
1204 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1205 /* When parsing a type-specifier, do not allow user-defined types. */
1206 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1209 /* The different kinds of declarators we want to parse. */
1211 typedef enum cp_parser_declarator_kind
1213 /* We want an abstract declarator. */
1214 CP_PARSER_DECLARATOR_ABSTRACT,
1215 /* We want a named declarator. */
1216 CP_PARSER_DECLARATOR_NAMED,
1217 /* We don't mind, but the name must be an unqualified-id. */
1218 CP_PARSER_DECLARATOR_EITHER
1219 } cp_parser_declarator_kind;
1221 /* The precedence values used to parse binary expressions. The minimum value
1222 of PREC must be 1, because zero is reserved to quickly discriminate
1223 binary operators from other tokens. */
1228 PREC_LOGICAL_OR_EXPRESSION,
1229 PREC_LOGICAL_AND_EXPRESSION,
1230 PREC_INCLUSIVE_OR_EXPRESSION,
1231 PREC_EXCLUSIVE_OR_EXPRESSION,
1232 PREC_AND_EXPRESSION,
1233 PREC_EQUALITY_EXPRESSION,
1234 PREC_RELATIONAL_EXPRESSION,
1235 PREC_SHIFT_EXPRESSION,
1236 PREC_ADDITIVE_EXPRESSION,
1237 PREC_MULTIPLICATIVE_EXPRESSION,
1239 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1242 /* A mapping from a token type to a corresponding tree node type, with a
1243 precedence value. */
1245 typedef struct cp_parser_binary_operations_map_node
1247 /* The token type. */
1248 enum cpp_ttype token_type;
1249 /* The corresponding tree code. */
1250 enum tree_code tree_type;
1251 /* The precedence of this operator. */
1252 enum cp_parser_prec prec;
1253 } cp_parser_binary_operations_map_node;
1255 /* The status of a tentative parse. */
1257 typedef enum cp_parser_status_kind
1259 /* No errors have occurred. */
1260 CP_PARSER_STATUS_KIND_NO_ERROR,
1261 /* An error has occurred. */
1262 CP_PARSER_STATUS_KIND_ERROR,
1263 /* We are committed to this tentative parse, whether or not an error
1265 CP_PARSER_STATUS_KIND_COMMITTED
1266 } cp_parser_status_kind;
1268 typedef struct cp_parser_expression_stack_entry
1270 /* Left hand side of the binary operation we are currently
1273 /* Original tree code for left hand side, if it was a binary
1274 expression itself (used for -Wparentheses). */
1275 enum tree_code lhs_type;
1276 /* Tree code for the binary operation we are parsing. */
1277 enum tree_code tree_type;
1278 /* Precedence of the binary operation we are parsing. */
1280 } cp_parser_expression_stack_entry;
1282 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1283 entries because precedence levels on the stack are monotonically
1285 typedef struct cp_parser_expression_stack_entry
1286 cp_parser_expression_stack[NUM_PREC_VALUES];
1288 /* Context that is saved and restored when parsing tentatively. */
1289 typedef struct cp_parser_context GTY (())
1291 /* If this is a tentative parsing context, the status of the
1293 enum cp_parser_status_kind status;
1294 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1295 that are looked up in this context must be looked up both in the
1296 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297 the context of the containing expression. */
1300 /* The next parsing context in the stack. */
1301 struct cp_parser_context *next;
1302 } cp_parser_context;
1306 /* Constructors and destructors. */
1308 static cp_parser_context *cp_parser_context_new
1309 (cp_parser_context *);
1311 /* Class variables. */
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316 Transformed into an associative array (binops_by_token) by
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1323 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1343 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1345 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1347 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1349 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352 /* The same as binops, but initialized by cp_parser_new so that
1353 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1357 /* Constructors and destructors. */
1359 /* Construct a new context. The context below this one on the stack
1360 is given by NEXT. */
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1365 cp_parser_context *context;
1367 /* Allocate the storage. */
1368 if (cp_parser_context_free_list != NULL)
1370 /* Pull the first entry from the free list. */
1371 context = cp_parser_context_free_list;
1372 cp_parser_context_free_list = context->next;
1373 memset (context, 0, sizeof (*context));
1376 context = GGC_CNEW (cp_parser_context);
1378 /* No errors have occurred yet in this context. */
1379 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380 /* If this is not the bottomost context, copy information that we
1381 need from the previous context. */
1384 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385 expression, then we are parsing one in this context, too. */
1386 context->object_type = next->object_type;
1387 /* Thread the stack. */
1388 context->next = next;
1394 /* The cp_parser structure represents the C++ parser. */
1396 typedef struct cp_parser GTY(())
1398 /* The lexer from which we are obtaining tokens. */
1401 /* The scope in which names should be looked up. If NULL_TREE, then
1402 we look up names in the scope that is currently open in the
1403 source program. If non-NULL, this is either a TYPE or
1404 NAMESPACE_DECL for the scope in which we should look. It can
1405 also be ERROR_MARK, when we've parsed a bogus scope.
1407 This value is not cleared automatically after a name is looked
1408 up, so we must be careful to clear it before starting a new look
1409 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1410 will look up `Z' in the scope of `X', rather than the current
1411 scope.) Unfortunately, it is difficult to tell when name lookup
1412 is complete, because we sometimes peek at a token, look it up,
1413 and then decide not to consume it. */
1416 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417 last lookup took place. OBJECT_SCOPE is used if an expression
1418 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419 respectively. QUALIFYING_SCOPE is used for an expression of the
1420 form "X::Y"; it refers to X. */
1422 tree qualifying_scope;
1424 /* A stack of parsing contexts. All but the bottom entry on the
1425 stack will be tentative contexts.
1427 We parse tentatively in order to determine which construct is in
1428 use in some situations. For example, in order to determine
1429 whether a statement is an expression-statement or a
1430 declaration-statement we parse it tentatively as a
1431 declaration-statement. If that fails, we then reparse the same
1432 token stream as an expression-statement. */
1433 cp_parser_context *context;
1435 /* True if we are parsing GNU C++. If this flag is not set, then
1436 GNU extensions are not recognized. */
1437 bool allow_gnu_extensions_p;
1439 /* TRUE if the `>' token should be interpreted as the greater-than
1440 operator. FALSE if it is the end of a template-id or
1441 template-parameter-list. In C++0x mode, this flag also applies to
1442 `>>' tokens, which are viewed as two consecutive `>' tokens when
1443 this flag is FALSE. */
1444 bool greater_than_is_operator_p;
1446 /* TRUE if default arguments are allowed within a parameter list
1447 that starts at this point. FALSE if only a gnu extension makes
1448 them permissible. */
1449 bool default_arg_ok_p;
1451 /* TRUE if we are parsing an integral constant-expression. See
1452 [expr.const] for a precise definition. */
1453 bool integral_constant_expression_p;
1455 /* TRUE if we are parsing an integral constant-expression -- but a
1456 non-constant expression should be permitted as well. This flag
1457 is used when parsing an array bound so that GNU variable-length
1458 arrays are tolerated. */
1459 bool allow_non_integral_constant_expression_p;
1461 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462 been seen that makes the expression non-constant. */
1463 bool non_integral_constant_expression_p;
1465 /* TRUE if local variable names and `this' are forbidden in the
1467 bool local_variables_forbidden_p;
1469 /* TRUE if the declaration we are parsing is part of a
1470 linkage-specification of the form `extern string-literal
1472 bool in_unbraced_linkage_specification_p;
1474 /* TRUE if we are presently parsing a declarator, after the
1475 direct-declarator. */
1476 bool in_declarator_p;
1478 /* TRUE if we are presently parsing a template-argument-list. */
1479 bool in_template_argument_list_p;
1481 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482 to IN_OMP_BLOCK if parsing OpenMP structured block and
1483 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1484 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485 iteration-statement, OpenMP block or loop within that switch. */
1486 #define IN_SWITCH_STMT 1
1487 #define IN_ITERATION_STMT 2
1488 #define IN_OMP_BLOCK 4
1489 #define IN_OMP_FOR 8
1490 #define IN_IF_STMT 16
1491 unsigned char in_statement;
1493 /* TRUE if we are presently parsing the body of a switch statement.
1494 Note that this doesn't quite overlap with in_statement above.
1495 The difference relates to giving the right sets of error messages:
1496 "case not in switch" vs "break statement used with OpenMP...". */
1497 bool in_switch_statement_p;
1499 /* TRUE if we are parsing a type-id in an expression context. In
1500 such a situation, both "type (expr)" and "type (type)" are valid
1502 bool in_type_id_in_expr_p;
1504 /* TRUE if we are currently in a header file where declarations are
1505 implicitly extern "C". */
1506 bool implicit_extern_c;
1508 /* TRUE if strings in expressions should be translated to the execution
1510 bool translate_strings_p;
1512 /* TRUE if we are presently parsing the body of a function, but not
1514 bool in_function_body;
1516 /* If non-NULL, then we are parsing a construct where new type
1517 definitions are not permitted. The string stored here will be
1518 issued as an error message if a type is defined. */
1519 const char *type_definition_forbidden_message;
1521 /* A list of lists. The outer list is a stack, used for member
1522 functions of local classes. At each level there are two sub-list,
1523 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525 TREE_VALUE's. The functions are chained in reverse declaration
1528 The TREE_PURPOSE sublist contains those functions with default
1529 arguments that need post processing, and the TREE_VALUE sublist
1530 contains those functions with definitions that need post
1533 These lists can only be processed once the outermost class being
1534 defined is complete. */
1535 tree unparsed_functions_queues;
1537 /* The number of classes whose definitions are currently in
1539 unsigned num_classes_being_defined;
1541 /* The number of template parameter lists that apply directly to the
1542 current declaration. */
1543 unsigned num_template_parameter_lists;
1548 /* Constructors and destructors. */
1550 static cp_parser *cp_parser_new
1553 /* Routines to parse various constructs.
1555 Those that return `tree' will return the error_mark_node (rather
1556 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557 Sometimes, they will return an ordinary node if error-recovery was
1558 attempted, even though a parse error occurred. So, to check
1559 whether or not a parse error occurred, you should always use
1560 cp_parser_error_occurred. If the construct is optional (indicated
1561 either by an `_opt' in the name of the function that does the
1562 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563 the construct is not present. */
1565 /* Lexical conventions [gram.lex] */
1567 static tree cp_parser_identifier
1569 static tree cp_parser_string_literal
1570 (cp_parser *, bool, bool);
1572 /* Basic concepts [gram.basic] */
1574 static bool cp_parser_translation_unit
1577 /* Expressions [gram.expr] */
1579 static tree cp_parser_primary_expression
1580 (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582 (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584 (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586 (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588 (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_class_or_namespace_name
1590 (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592 (cp_parser *, bool, bool, bool);
1593 static tree cp_parser_postfix_open_square_expression
1594 (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static tree cp_parser_parenthesized_expression_list
1598 (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600 (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602 (cp_parser *, bool, bool);
1603 static enum tree_code cp_parser_unary_operator
1605 static tree cp_parser_new_expression
1607 static tree cp_parser_new_placement
1609 static tree cp_parser_new_type_id
1610 (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1613 static cp_declarator *cp_parser_direct_new_declarator
1615 static tree cp_parser_new_initializer
1617 static tree cp_parser_delete_expression
1619 static tree cp_parser_cast_expression
1620 (cp_parser *, bool, bool);
1621 static tree cp_parser_binary_expression
1622 (cp_parser *, bool, enum cp_parser_prec);
1623 static tree cp_parser_question_colon_clause
1624 (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626 (cp_parser *, bool);
1627 static enum tree_code cp_parser_assignment_operator_opt
1629 static tree cp_parser_expression
1630 (cp_parser *, bool);
1631 static tree cp_parser_constant_expression
1632 (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1636 /* Statements [gram.stmt.stmt] */
1638 static void cp_parser_statement
1639 (cp_parser *, tree, bool, bool *);
1640 static void cp_parser_label_for_labeled_statement
1642 static tree cp_parser_expression_statement
1643 (cp_parser *, tree);
1644 static tree cp_parser_compound_statement
1645 (cp_parser *, tree, bool);
1646 static void cp_parser_statement_seq_opt
1647 (cp_parser *, tree);
1648 static tree cp_parser_selection_statement
1649 (cp_parser *, bool *);
1650 static tree cp_parser_condition
1652 static tree cp_parser_iteration_statement
1654 static void cp_parser_for_init_statement
1656 static tree cp_parser_jump_statement
1658 static void cp_parser_declaration_statement
1661 static tree cp_parser_implicitly_scoped_statement
1662 (cp_parser *, bool *);
1663 static void cp_parser_already_scoped_statement
1666 /* Declarations [gram.dcl.dcl] */
1668 static void cp_parser_declaration_seq_opt
1670 static void cp_parser_declaration
1672 static void cp_parser_block_declaration
1673 (cp_parser *, bool);
1674 static void cp_parser_simple_declaration
1675 (cp_parser *, bool);
1676 static void cp_parser_decl_specifier_seq
1677 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1678 static tree cp_parser_storage_class_specifier_opt
1680 static tree cp_parser_function_specifier_opt
1681 (cp_parser *, cp_decl_specifier_seq *);
1682 static tree cp_parser_type_specifier
1683 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1685 static tree cp_parser_simple_type_specifier
1686 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1687 static tree cp_parser_type_name
1689 static tree cp_parser_nonclass_name
1690 (cp_parser* parser);
1691 static tree cp_parser_elaborated_type_specifier
1692 (cp_parser *, bool, bool);
1693 static tree cp_parser_enum_specifier
1695 static void cp_parser_enumerator_list
1696 (cp_parser *, tree);
1697 static void cp_parser_enumerator_definition
1698 (cp_parser *, tree);
1699 static tree cp_parser_namespace_name
1701 static void cp_parser_namespace_definition
1703 static void cp_parser_namespace_body
1705 static tree cp_parser_qualified_namespace_specifier
1707 static void cp_parser_namespace_alias_definition
1709 static bool cp_parser_using_declaration
1710 (cp_parser *, bool);
1711 static void cp_parser_using_directive
1713 static void cp_parser_asm_definition
1715 static void cp_parser_linkage_specification
1717 static void cp_parser_static_assert
1718 (cp_parser *, bool);
1719 static tree cp_parser_decltype
1722 /* Declarators [gram.dcl.decl] */
1724 static tree cp_parser_init_declarator
1725 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1726 static cp_declarator *cp_parser_declarator
1727 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1728 static cp_declarator *cp_parser_direct_declarator
1729 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1730 static enum tree_code cp_parser_ptr_operator
1731 (cp_parser *, tree *, cp_cv_quals *);
1732 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1734 static tree cp_parser_declarator_id
1735 (cp_parser *, bool);
1736 static tree cp_parser_type_id
1738 static void cp_parser_type_specifier_seq
1739 (cp_parser *, bool, cp_decl_specifier_seq *);
1740 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1742 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1743 (cp_parser *, bool *);
1744 static cp_parameter_declarator *cp_parser_parameter_declaration
1745 (cp_parser *, bool, bool *);
1746 static tree cp_parser_default_argument
1747 (cp_parser *, bool);
1748 static void cp_parser_function_body
1750 static tree cp_parser_initializer
1751 (cp_parser *, bool *, bool *);
1752 static tree cp_parser_initializer_clause
1753 (cp_parser *, bool *);
1754 static tree cp_parser_braced_list
1755 (cp_parser*, bool*);
1756 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1757 (cp_parser *, bool *);
1759 static bool cp_parser_ctor_initializer_opt_and_function_body
1762 /* Classes [gram.class] */
1764 static tree cp_parser_class_name
1765 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1766 static tree cp_parser_class_specifier
1768 static tree cp_parser_class_head
1769 (cp_parser *, bool *, tree *, tree *);
1770 static enum tag_types cp_parser_class_key
1772 static void cp_parser_member_specification_opt
1774 static void cp_parser_member_declaration
1776 static tree cp_parser_pure_specifier
1778 static tree cp_parser_constant_initializer
1781 /* Derived classes [gram.class.derived] */
1783 static tree cp_parser_base_clause
1785 static tree cp_parser_base_specifier
1788 /* Special member functions [gram.special] */
1790 static tree cp_parser_conversion_function_id
1792 static tree cp_parser_conversion_type_id
1794 static cp_declarator *cp_parser_conversion_declarator_opt
1796 static bool cp_parser_ctor_initializer_opt
1798 static void cp_parser_mem_initializer_list
1800 static tree cp_parser_mem_initializer
1802 static tree cp_parser_mem_initializer_id
1805 /* Overloading [gram.over] */
1807 static tree cp_parser_operator_function_id
1809 static tree cp_parser_operator
1812 /* Templates [gram.temp] */
1814 static void cp_parser_template_declaration
1815 (cp_parser *, bool);
1816 static tree cp_parser_template_parameter_list
1818 static tree cp_parser_template_parameter
1819 (cp_parser *, bool *, bool *);
1820 static tree cp_parser_type_parameter
1821 (cp_parser *, bool *);
1822 static tree cp_parser_template_id
1823 (cp_parser *, bool, bool, bool);
1824 static tree cp_parser_template_name
1825 (cp_parser *, bool, bool, bool, bool *);
1826 static tree cp_parser_template_argument_list
1828 static tree cp_parser_template_argument
1830 static void cp_parser_explicit_instantiation
1832 static void cp_parser_explicit_specialization
1835 /* Exception handling [gram.exception] */
1837 static tree cp_parser_try_block
1839 static bool cp_parser_function_try_block
1841 static void cp_parser_handler_seq
1843 static void cp_parser_handler
1845 static tree cp_parser_exception_declaration
1847 static tree cp_parser_throw_expression
1849 static tree cp_parser_exception_specification_opt
1851 static tree cp_parser_type_id_list
1854 /* GNU Extensions */
1856 static tree cp_parser_asm_specification_opt
1858 static tree cp_parser_asm_operand_list
1860 static tree cp_parser_asm_clobber_list
1862 static tree cp_parser_attributes_opt
1864 static tree cp_parser_attribute_list
1866 static bool cp_parser_extension_opt
1867 (cp_parser *, int *);
1868 static void cp_parser_label_declaration
1871 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1872 static bool cp_parser_pragma
1873 (cp_parser *, enum pragma_context);
1875 /* Objective-C++ Productions */
1877 static tree cp_parser_objc_message_receiver
1879 static tree cp_parser_objc_message_args
1881 static tree cp_parser_objc_message_expression
1883 static tree cp_parser_objc_encode_expression
1885 static tree cp_parser_objc_defs_expression
1887 static tree cp_parser_objc_protocol_expression
1889 static tree cp_parser_objc_selector_expression
1891 static tree cp_parser_objc_expression
1893 static bool cp_parser_objc_selector_p
1895 static tree cp_parser_objc_selector
1897 static tree cp_parser_objc_protocol_refs_opt
1899 static void cp_parser_objc_declaration
1901 static tree cp_parser_objc_statement
1904 /* Utility Routines */
1906 static tree cp_parser_lookup_name
1907 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1908 static tree cp_parser_lookup_name_simple
1909 (cp_parser *, tree, location_t);
1910 static tree cp_parser_maybe_treat_template_as_class
1912 static bool cp_parser_check_declarator_template_parameters
1913 (cp_parser *, cp_declarator *, location_t);
1914 static bool cp_parser_check_template_parameters
1915 (cp_parser *, unsigned, location_t);
1916 static tree cp_parser_simple_cast_expression
1918 static tree cp_parser_global_scope_opt
1919 (cp_parser *, bool);
1920 static bool cp_parser_constructor_declarator_p
1921 (cp_parser *, bool);
1922 static tree cp_parser_function_definition_from_specifiers_and_declarator
1923 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1924 static tree cp_parser_function_definition_after_declarator
1925 (cp_parser *, bool);
1926 static void cp_parser_template_declaration_after_export
1927 (cp_parser *, bool);
1928 static void cp_parser_perform_template_parameter_access_checks
1929 (VEC (deferred_access_check,gc)*);
1930 static tree cp_parser_single_declaration
1931 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1932 static tree cp_parser_functional_cast
1933 (cp_parser *, tree);
1934 static tree cp_parser_save_member_function_body
1935 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1936 static tree cp_parser_enclosed_template_argument_list
1938 static void cp_parser_save_default_args
1939 (cp_parser *, tree);
1940 static void cp_parser_late_parsing_for_member
1941 (cp_parser *, tree);
1942 static void cp_parser_late_parsing_default_args
1943 (cp_parser *, tree);
1944 static tree cp_parser_sizeof_operand
1945 (cp_parser *, enum rid);
1946 static tree cp_parser_trait_expr
1947 (cp_parser *, enum rid);
1948 static bool cp_parser_declares_only_class_p
1950 static void cp_parser_set_storage_class
1951 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1952 static void cp_parser_set_decl_spec_type
1953 (cp_decl_specifier_seq *, tree, location_t, bool);
1954 static bool cp_parser_friend_p
1955 (const cp_decl_specifier_seq *);
1956 static cp_token *cp_parser_require
1957 (cp_parser *, enum cpp_ttype, const char *);
1958 static cp_token *cp_parser_require_keyword
1959 (cp_parser *, enum rid, const char *);
1960 static bool cp_parser_token_starts_function_definition_p
1962 static bool cp_parser_next_token_starts_class_definition_p
1964 static bool cp_parser_next_token_ends_template_argument_p
1966 static bool cp_parser_nth_token_starts_template_argument_list_p
1967 (cp_parser *, size_t);
1968 static enum tag_types cp_parser_token_is_class_key
1970 static void cp_parser_check_class_key
1971 (enum tag_types, tree type);
1972 static void cp_parser_check_access_in_redeclaration
1973 (tree type, location_t location);
1974 static bool cp_parser_optional_template_keyword
1976 static void cp_parser_pre_parsed_nested_name_specifier
1978 static bool cp_parser_cache_group
1979 (cp_parser *, enum cpp_ttype, unsigned);
1980 static void cp_parser_parse_tentatively
1982 static void cp_parser_commit_to_tentative_parse
1984 static void cp_parser_abort_tentative_parse
1986 static bool cp_parser_parse_definitely
1988 static inline bool cp_parser_parsing_tentatively
1990 static bool cp_parser_uncommitted_to_tentative_parse_p
1992 static void cp_parser_error
1993 (cp_parser *, const char *);
1994 static void cp_parser_name_lookup_error
1995 (cp_parser *, tree, tree, const char *, location_t);
1996 static bool cp_parser_simulate_error
1998 static bool cp_parser_check_type_definition
2000 static void cp_parser_check_for_definition_in_return_type
2001 (cp_declarator *, tree, location_t type_location);
2002 static void cp_parser_check_for_invalid_template_id
2003 (cp_parser *, tree, location_t location);
2004 static bool cp_parser_non_integral_constant_expression
2005 (cp_parser *, const char *);
2006 static void cp_parser_diagnose_invalid_type_name
2007 (cp_parser *, tree, tree, location_t);
2008 static bool cp_parser_parse_and_diagnose_invalid_type_name
2010 static int cp_parser_skip_to_closing_parenthesis
2011 (cp_parser *, bool, bool, bool);
2012 static void cp_parser_skip_to_end_of_statement
2014 static void cp_parser_consume_semicolon_at_end_of_statement
2016 static void cp_parser_skip_to_end_of_block_or_statement
2018 static bool cp_parser_skip_to_closing_brace
2020 static void cp_parser_skip_to_end_of_template_parameter_list
2022 static void cp_parser_skip_to_pragma_eol
2023 (cp_parser*, cp_token *);
2024 static bool cp_parser_error_occurred
2026 static bool cp_parser_allow_gnu_extensions_p
2028 static bool cp_parser_is_string_literal
2030 static bool cp_parser_is_keyword
2031 (cp_token *, enum rid);
2032 static tree cp_parser_make_typename_type
2033 (cp_parser *, tree, tree, location_t location);
2034 static cp_declarator * cp_parser_make_indirect_declarator
2035 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2037 /* Returns nonzero if we are parsing tentatively. */
2040 cp_parser_parsing_tentatively (cp_parser* parser)
2042 return parser->context->next != NULL;
2045 /* Returns nonzero if TOKEN is a string literal. */
2048 cp_parser_is_string_literal (cp_token* token)
2050 return (token->type == CPP_STRING ||
2051 token->type == CPP_STRING16 ||
2052 token->type == CPP_STRING32 ||
2053 token->type == CPP_WSTRING);
2056 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2059 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2061 return token->keyword == keyword;
2064 /* If not parsing tentatively, issue a diagnostic of the form
2065 FILE:LINE: MESSAGE before TOKEN
2066 where TOKEN is the next token in the input stream. MESSAGE
2067 (specified by the caller) is usually of the form "expected
2071 cp_parser_error (cp_parser* parser, const char* message)
2073 if (!cp_parser_simulate_error (parser))
2075 cp_token *token = cp_lexer_peek_token (parser->lexer);
2076 /* This diagnostic makes more sense if it is tagged to the line
2077 of the token we just peeked at. */
2078 cp_lexer_set_source_position_from_token (token);
2080 if (token->type == CPP_PRAGMA)
2082 error ("%H%<#pragma%> is not allowed here", &token->location);
2083 cp_parser_skip_to_pragma_eol (parser, token);
2087 c_parse_error (message,
2088 /* Because c_parser_error does not understand
2089 CPP_KEYWORD, keywords are treated like
2091 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2096 /* Issue an error about name-lookup failing. NAME is the
2097 IDENTIFIER_NODE DECL is the result of
2098 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2099 the thing that we hoped to find. */
2102 cp_parser_name_lookup_error (cp_parser* parser,
2105 const char* desired,
2106 location_t location)
2108 /* If name lookup completely failed, tell the user that NAME was not
2110 if (decl == error_mark_node)
2112 if (parser->scope && parser->scope != global_namespace)
2113 error ("%H%<%E::%E%> has not been declared",
2114 &location, parser->scope, name);
2115 else if (parser->scope == global_namespace)
2116 error ("%H%<::%E%> has not been declared", &location, name);
2117 else if (parser->object_scope
2118 && !CLASS_TYPE_P (parser->object_scope))
2119 error ("%Hrequest for member %qE in non-class type %qT",
2120 &location, name, parser->object_scope);
2121 else if (parser->object_scope)
2122 error ("%H%<%T::%E%> has not been declared",
2123 &location, parser->object_scope, name);
2125 error ("%H%qE has not been declared", &location, name);
2127 else if (parser->scope && parser->scope != global_namespace)
2128 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2129 else if (parser->scope == global_namespace)
2130 error ("%H%<::%E%> %s", &location, name, desired);
2132 error ("%H%qE %s", &location, name, desired);
2135 /* If we are parsing tentatively, remember that an error has occurred
2136 during this tentative parse. Returns true if the error was
2137 simulated; false if a message should be issued by the caller. */
2140 cp_parser_simulate_error (cp_parser* parser)
2142 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2144 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2150 /* Check for repeated decl-specifiers. */
2153 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2154 location_t location)
2158 for (ds = ds_first; ds != ds_last; ++ds)
2160 unsigned count = decl_specs->specs[(int)ds];
2163 /* The "long" specifier is a special case because of "long long". */
2167 error ("%H%<long long long%> is too long for GCC", &location);
2168 else if (pedantic && !in_system_header && warn_long_long
2169 && cxx_dialect == cxx98)
2170 pedwarn ("%HISO C++ 1998 does not support %<long long%>",
2175 static const char *const decl_spec_names[] = {
2191 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2196 /* This function is called when a type is defined. If type
2197 definitions are forbidden at this point, an error message is
2201 cp_parser_check_type_definition (cp_parser* parser)
2203 /* If types are forbidden here, issue a message. */
2204 if (parser->type_definition_forbidden_message)
2206 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2207 in the message need to be interpreted. */
2208 error (parser->type_definition_forbidden_message);
2214 /* This function is called when the DECLARATOR is processed. The TYPE
2215 was a type defined in the decl-specifiers. If it is invalid to
2216 define a type in the decl-specifiers for DECLARATOR, an error is
2217 issued. TYPE_LOCATION is the location of TYPE and is used
2218 for error reporting. */
2221 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2222 tree type, location_t type_location)
2224 /* [dcl.fct] forbids type definitions in return types.
2225 Unfortunately, it's not easy to know whether or not we are
2226 processing a return type until after the fact. */
2228 && (declarator->kind == cdk_pointer
2229 || declarator->kind == cdk_reference
2230 || declarator->kind == cdk_ptrmem))
2231 declarator = declarator->declarator;
2233 && declarator->kind == cdk_function)
2235 error ("%Hnew types may not be defined in a return type", &type_location);
2236 inform ("%H(perhaps a semicolon is missing after the definition of %qT)",
2237 &type_location, type);
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249 tree type, location_t location)
2251 cp_token_position start = 0;
2253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2256 error ("%H%qT is not a template", &location, type);
2257 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258 error ("%H%qE is not a template", &location, type);
2260 error ("%Hinvalid template-id", &location);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263 start = cp_lexer_token_position (parser->lexer, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser->lexer);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2271 cp_lexer_purge_tokens_after (parser->lexer, start);
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2281 cp_parser_non_integral_constant_expression (cp_parser *parser,
2284 parser->non_integral_constant_expression_p = true;
2285 if (parser->integral_constant_expression_p)
2287 if (!parser->allow_non_integral_constant_expression_p)
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message = concat (thing,
2292 " cannot appear in a constant-expression",
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310 tree scope, tree id,
2311 location_t location)
2313 tree decl, old_scope;
2314 /* Try to lookup the identifier. */
2315 old_scope = parser->scope;
2316 parser->scope = scope;
2317 decl = cp_parser_lookup_name_simple (parser, id, location);
2318 parser->scope = old_scope;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl) == TEMPLATE_DECL)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2324 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326 else if (TREE_CODE (decl) == TYPE_DECL)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2330 else if (!parser->scope)
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location, id);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl && current_class_type
2342 && TYPE_BINFO (current_class_type))
2346 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2350 tree base_type = BINFO_TYPE (b);
2351 if (CLASS_TYPE_P (base_type)
2352 && dependent_type_p (base_type))
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359 for (field = TYPE_FIELDS (base_type);
2361 field = TREE_CHAIN (field))
2362 if (TREE_CODE (field) == TYPE_DECL
2363 && DECL_NAME (field) == id)
2365 inform ("%H(perhaps %<typename %T::%E%> was intended)",
2366 &location, BINFO_TYPE (b), id);
2375 /* Here we diagnose qualified-ids where the scope is actually correct,
2376 but the identifier does not resolve to a valid type name. */
2377 else if (parser->scope != error_mark_node)
2379 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2380 error ("%H%qE in namespace %qE does not name a type",
2381 &location, id, parser->scope);
2382 else if (TYPE_P (parser->scope))
2383 error ("%H%qE in class %qT does not name a type",
2384 &location, id, parser->scope);
2388 cp_parser_commit_to_tentative_parse (parser);
2391 /* Check for a common situation where a type-name should be present,
2392 but is not, and issue a sensible error message. Returns true if an
2393 invalid type-name was detected.
2395 The situation handled by this function are variable declarations of the
2396 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2397 Usually, `ID' should name a type, but if we got here it means that it
2398 does not. We try to emit the best possible error message depending on
2399 how exactly the id-expression looks like. */
2402 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2405 cp_token *token = cp_lexer_peek_token (parser->lexer);
2407 cp_parser_parse_tentatively (parser);
2408 id = cp_parser_id_expression (parser,
2409 /*template_keyword_p=*/false,
2410 /*check_dependency_p=*/true,
2411 /*template_p=*/NULL,
2412 /*declarator_p=*/true,
2413 /*optional_p=*/false);
2414 /* After the id-expression, there should be a plain identifier,
2415 otherwise this is not a simple variable declaration. Also, if
2416 the scope is dependent, we cannot do much. */
2417 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2418 || (parser->scope && TYPE_P (parser->scope)
2419 && dependent_type_p (parser->scope))
2420 || TREE_CODE (id) == TYPE_DECL)
2422 cp_parser_abort_tentative_parse (parser);
2425 if (!cp_parser_parse_definitely (parser))
2428 /* Emit a diagnostic for the invalid type. */
2429 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2430 id, token->location);
2431 /* Skip to the end of the declaration; there's no point in
2432 trying to process it. */
2433 cp_parser_skip_to_end_of_block_or_statement (parser);
2437 /* Consume tokens up to, and including, the next non-nested closing `)'.
2438 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2439 are doing error recovery. Returns -1 if OR_COMMA is true and we
2440 found an unnested comma. */
2443 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2448 unsigned paren_depth = 0;
2449 unsigned brace_depth = 0;
2451 if (recovering && !or_comma
2452 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2457 cp_token * token = cp_lexer_peek_token (parser->lexer);
2459 switch (token->type)
2462 case CPP_PRAGMA_EOL:
2463 /* If we've run out of tokens, then there is no closing `)'. */
2467 /* This matches the processing in skip_to_end_of_statement. */
2472 case CPP_OPEN_BRACE:
2475 case CPP_CLOSE_BRACE:
2481 if (recovering && or_comma && !brace_depth && !paren_depth)
2485 case CPP_OPEN_PAREN:
2490 case CPP_CLOSE_PAREN:
2491 if (!brace_depth && !paren_depth--)
2494 cp_lexer_consume_token (parser->lexer);
2503 /* Consume the token. */
2504 cp_lexer_consume_token (parser->lexer);
2508 /* Consume tokens until we reach the end of the current statement.
2509 Normally, that will be just before consuming a `;'. However, if a
2510 non-nested `}' comes first, then we stop before consuming that. */
2513 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 unsigned nesting_depth = 0;
2519 cp_token *token = cp_lexer_peek_token (parser->lexer);
2521 switch (token->type)
2524 case CPP_PRAGMA_EOL:
2525 /* If we've run out of tokens, stop. */
2529 /* If the next token is a `;', we have reached the end of the
2535 case CPP_CLOSE_BRACE:
2536 /* If this is a non-nested '}', stop before consuming it.
2537 That way, when confronted with something like:
2541 we stop before consuming the closing '}', even though we
2542 have not yet reached a `;'. */
2543 if (nesting_depth == 0)
2546 /* If it is the closing '}' for a block that we have
2547 scanned, stop -- but only after consuming the token.
2553 we will stop after the body of the erroneously declared
2554 function, but before consuming the following `typedef'
2556 if (--nesting_depth == 0)
2558 cp_lexer_consume_token (parser->lexer);
2562 case CPP_OPEN_BRACE:
2570 /* Consume the token. */
2571 cp_lexer_consume_token (parser->lexer);
2575 /* This function is called at the end of a statement or declaration.
2576 If the next token is a semicolon, it is consumed; otherwise, error
2577 recovery is attempted. */
2580 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 /* Look for the trailing `;'. */
2583 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585 /* If there is additional (erroneous) input, skip to the end of
2587 cp_parser_skip_to_end_of_statement (parser);
2588 /* If the next token is now a `;', consume it. */
2589 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2590 cp_lexer_consume_token (parser->lexer);
2594 /* Skip tokens until we have consumed an entire block, or until we
2595 have consumed a non-nested `;'. */
2598 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 int nesting_depth = 0;
2602 while (nesting_depth >= 0)
2604 cp_token *token = cp_lexer_peek_token (parser->lexer);
2606 switch (token->type)
2609 case CPP_PRAGMA_EOL:
2610 /* If we've run out of tokens, stop. */
2614 /* Stop if this is an unnested ';'. */
2619 case CPP_CLOSE_BRACE:
2620 /* Stop if this is an unnested '}', or closes the outermost
2627 case CPP_OPEN_BRACE:
2636 /* Consume the token. */
2637 cp_lexer_consume_token (parser->lexer);
2641 /* Skip tokens until a non-nested closing curly brace is the next
2642 token, or there are no more tokens. Return true in the first case,
2646 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 unsigned nesting_depth = 0;
2652 cp_token *token = cp_lexer_peek_token (parser->lexer);
2654 switch (token->type)
2657 case CPP_PRAGMA_EOL:
2658 /* If we've run out of tokens, stop. */
2661 case CPP_CLOSE_BRACE:
2662 /* If the next token is a non-nested `}', then we have reached
2663 the end of the current block. */
2664 if (nesting_depth-- == 0)
2668 case CPP_OPEN_BRACE:
2669 /* If it the next token is a `{', then we are entering a new
2670 block. Consume the entire block. */
2678 /* Consume the token. */
2679 cp_lexer_consume_token (parser->lexer);
2683 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2684 parameter is the PRAGMA token, allowing us to purge the entire pragma
2688 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2692 parser->lexer->in_pragma = false;
2695 token = cp_lexer_consume_token (parser->lexer);
2696 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698 /* Ensure that the pragma is not parsed again. */
2699 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2702 /* Require pragma end of line, resyncing with it as necessary. The
2703 arguments are as for cp_parser_skip_to_pragma_eol. */
2706 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 parser->lexer->in_pragma = false;
2709 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2710 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2713 /* This is a simple wrapper around make_typename_type. When the id is
2714 an unresolved identifier node, we can provide a superior diagnostic
2715 using cp_parser_diagnose_invalid_type_name. */
2718 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2719 tree id, location_t id_location)
2722 if (TREE_CODE (id) == IDENTIFIER_NODE)
2724 result = make_typename_type (scope, id, typename_type,
2725 /*complain=*/tf_none);
2726 if (result == error_mark_node)
2727 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2730 return make_typename_type (scope, id, typename_type, tf_error);
2733 /* This is a wrapper around the
2734 make_{pointer,ptrmem,reference}_declarator functions that decides
2735 which one to call based on the CODE and CLASS_TYPE arguments. The
2736 CODE argument should be one of the values returned by
2737 cp_parser_ptr_operator. */
2738 static cp_declarator *
2739 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2740 cp_cv_quals cv_qualifiers,
2741 cp_declarator *target)
2743 if (code == ERROR_MARK)
2744 return cp_error_declarator;
2746 if (code == INDIRECT_REF)
2747 if (class_type == NULL_TREE)
2748 return make_pointer_declarator (cv_qualifiers, target);
2750 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2751 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2752 return make_reference_declarator (cv_qualifiers, target, false);
2753 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2754 return make_reference_declarator (cv_qualifiers, target, true);
2758 /* Create a new C++ parser. */
2761 cp_parser_new (void)
2767 /* cp_lexer_new_main is called before calling ggc_alloc because
2768 cp_lexer_new_main might load a PCH file. */
2769 lexer = cp_lexer_new_main ();
2771 /* Initialize the binops_by_token so that we can get the tree
2772 directly from the token. */
2773 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2774 binops_by_token[binops[i].token_type] = binops[i];
2776 parser = GGC_CNEW (cp_parser);
2777 parser->lexer = lexer;
2778 parser->context = cp_parser_context_new (NULL);
2780 /* For now, we always accept GNU extensions. */
2781 parser->allow_gnu_extensions_p = 1;
2783 /* The `>' token is a greater-than operator, not the end of a
2785 parser->greater_than_is_operator_p = true;
2787 parser->default_arg_ok_p = true;
2789 /* We are not parsing a constant-expression. */
2790 parser->integral_constant_expression_p = false;
2791 parser->allow_non_integral_constant_expression_p = false;
2792 parser->non_integral_constant_expression_p = false;
2794 /* Local variable names are not forbidden. */
2795 parser->local_variables_forbidden_p = false;
2797 /* We are not processing an `extern "C"' declaration. */
2798 parser->in_unbraced_linkage_specification_p = false;
2800 /* We are not processing a declarator. */
2801 parser->in_declarator_p = false;
2803 /* We are not processing a template-argument-list. */
2804 parser->in_template_argument_list_p = false;
2806 /* We are not in an iteration statement. */
2807 parser->in_statement = 0;
2809 /* We are not in a switch statement. */
2810 parser->in_switch_statement_p = false;
2812 /* We are not parsing a type-id inside an expression. */
2813 parser->in_type_id_in_expr_p = false;
2815 /* Declarations aren't implicitly extern "C". */
2816 parser->implicit_extern_c = false;
2818 /* String literals should be translated to the execution character set. */
2819 parser->translate_strings_p = true;
2821 /* We are not parsing a function body. */
2822 parser->in_function_body = false;
2824 /* The unparsed function queue is empty. */
2825 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827 /* There are no classes being defined. */
2828 parser->num_classes_being_defined = 0;
2830 /* No template parameters apply. */
2831 parser->num_template_parameter_lists = 0;
2836 /* Create a cp_lexer structure which will emit the tokens in CACHE
2837 and push it onto the parser's lexer stack. This is used for delayed
2838 parsing of in-class method bodies and default arguments, and should
2839 not be confused with tentative parsing. */
2841 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2844 lexer->next = parser->lexer;
2845 parser->lexer = lexer;
2847 /* Move the current source position to that of the first token in the
2849 cp_lexer_set_source_position_from_token (lexer->next_token);
2852 /* Pop the top lexer off the parser stack. This is never used for the
2853 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2855 cp_parser_pop_lexer (cp_parser *parser)
2857 cp_lexer *lexer = parser->lexer;
2858 parser->lexer = lexer->next;
2859 cp_lexer_destroy (lexer);
2861 /* Put the current source position back where it was before this
2862 lexer was pushed. */
2863 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2866 /* Lexical conventions [gram.lex] */
2868 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2872 cp_parser_identifier (cp_parser* parser)
2876 /* Look for the identifier. */
2877 token = cp_parser_require (parser, CPP_NAME, "identifier");
2878 /* Return the value. */
2879 return token ? token->u.value : error_mark_node;
2882 /* Parse a sequence of adjacent string constants. Returns a
2883 TREE_STRING representing the combined, nul-terminated string
2884 constant. If TRANSLATE is true, translate the string to the
2885 execution character set. If WIDE_OK is true, a wide string is
2888 C++98 [lex.string] says that if a narrow string literal token is
2889 adjacent to a wide string literal token, the behavior is undefined.
2890 However, C99 6.4.5p4 says that this results in a wide string literal.
2891 We follow C99 here, for consistency with the C front end.
2893 This code is largely lifted from lex_string() in c-lex.c.
2895 FUTURE: ObjC++ will need to handle @-strings here. */
2897 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2901 struct obstack str_ob;
2902 cpp_string str, istr, *strs;
2904 enum cpp_ttype type;
2906 tok = cp_lexer_peek_token (parser->lexer);
2907 if (!cp_parser_is_string_literal (tok))
2909 cp_parser_error (parser, "expected string-literal");
2910 return error_mark_node;
2915 /* Try to avoid the overhead of creating and destroying an obstack
2916 for the common case of just one string. */
2917 if (!cp_parser_is_string_literal
2918 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920 cp_lexer_consume_token (parser->lexer);
2922 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2923 str.len = TREE_STRING_LENGTH (tok->u.value);
2930 gcc_obstack_init (&str_ob);
2935 cp_lexer_consume_token (parser->lexer);
2937 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2938 str.len = TREE_STRING_LENGTH (tok->u.value);
2940 if (type != tok->type)
2942 if (type == CPP_STRING)
2944 else if (tok->type != CPP_STRING)
2945 error ("%Hunsupported non-standard concatenation "
2946 "of string literals", &tok->location);
2949 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951 tok = cp_lexer_peek_token (parser->lexer);
2953 while (cp_parser_is_string_literal (tok));
2955 strs = (cpp_string *) obstack_finish (&str_ob);
2958 if (type != CPP_STRING && !wide_ok)
2960 cp_parser_error (parser, "a wide string is invalid in this context");
2964 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2965 (parse_in, strs, count, &istr, type))
2967 value = build_string (istr.len, (const char *)istr.text);
2968 free (CONST_CAST (unsigned char *, istr.text));
2974 TREE_TYPE (value) = char_array_type_node;
2977 TREE_TYPE (value) = char16_array_type_node;
2980 TREE_TYPE (value) = char32_array_type_node;
2983 TREE_TYPE (value) = wchar_array_type_node;
2987 value = fix_string_type (value);
2990 /* cpp_interpret_string has issued an error. */
2991 value = error_mark_node;
2994 obstack_free (&str_ob, 0);
3000 /* Basic concepts [gram.basic] */
3002 /* Parse a translation-unit.
3005 declaration-seq [opt]
3007 Returns TRUE if all went well. */
3010 cp_parser_translation_unit (cp_parser* parser)
3012 /* The address of the first non-permanent object on the declarator
3014 static void *declarator_obstack_base;
3018 /* Create the declarator obstack, if necessary. */
3019 if (!cp_error_declarator)
3021 gcc_obstack_init (&declarator_obstack);
3022 /* Create the error declarator. */
3023 cp_error_declarator = make_declarator (cdk_error);
3024 /* Create the empty parameter list. */
3025 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3026 /* Remember where the base of the declarator obstack lies. */
3027 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3030 cp_parser_declaration_seq_opt (parser);
3032 /* If there are no tokens left then all went well. */
3033 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035 /* Get rid of the token array; we don't need it any more. */
3036 cp_lexer_destroy (parser->lexer);
3037 parser->lexer = NULL;
3039 /* This file might have been a context that's implicitly extern
3040 "C". If so, pop the lang context. (Only relevant for PCH.) */
3041 if (parser->implicit_extern_c)
3043 pop_lang_context ();
3044 parser->implicit_extern_c = false;
3048 finish_translation_unit ();
3054 cp_parser_error (parser, "expected declaration");
3058 /* Make sure the declarator obstack was fully cleaned up. */
3059 gcc_assert (obstack_next_free (&declarator_obstack)
3060 == declarator_obstack_base);
3062 /* All went well. */
3066 /* Expressions [gram.expr] */
3068 /* Parse a primary-expression.
3079 ( compound-statement )
3080 __builtin_va_arg ( assignment-expression , type-id )
3081 __builtin_offsetof ( type-id , offsetof-expression )
3084 __has_nothrow_assign ( type-id )
3085 __has_nothrow_constructor ( type-id )
3086 __has_nothrow_copy ( type-id )
3087 __has_trivial_assign ( type-id )
3088 __has_trivial_constructor ( type-id )
3089 __has_trivial_copy ( type-id )
3090 __has_trivial_destructor ( type-id )
3091 __has_virtual_destructor ( type-id )
3092 __is_abstract ( type-id )
3093 __is_base_of ( type-id , type-id )
3094 __is_class ( type-id )
3095 __is_convertible_to ( type-id , type-id )
3096 __is_empty ( type-id )
3097 __is_enum ( type-id )
3098 __is_pod ( type-id )
3099 __is_polymorphic ( type-id )
3100 __is_union ( type-id )
3102 Objective-C++ Extension:
3110 ADDRESS_P is true iff this expression was immediately preceded by
3111 "&" and therefore might denote a pointer-to-member. CAST_P is true
3112 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3113 true iff this expression is a template argument.
3115 Returns a representation of the expression. Upon return, *IDK
3116 indicates what kind of id-expression (if any) was present. */
3119 cp_parser_primary_expression (cp_parser *parser,
3122 bool template_arg_p,
3125 cp_token *token = NULL;
3127 /* Assume the primary expression is not an id-expression. */
3128 *idk = CP_ID_KIND_NONE;
3130 /* Peek at the next token. */
3131 token = cp_lexer_peek_token (parser->lexer);
3132 switch (token->type)
3145 token = cp_lexer_consume_token (parser->lexer);
3146 /* Floating-point literals are only allowed in an integral
3147 constant expression if they are cast to an integral or
3148 enumeration type. */
3149 if (TREE_CODE (token->u.value) == REAL_CST
3150 && parser->integral_constant_expression_p
3153 /* CAST_P will be set even in invalid code like "int(2.7 +
3154 ...)". Therefore, we have to check that the next token
3155 is sure to end the cast. */
3158 cp_token *next_token;
3160 next_token = cp_lexer_peek_token (parser->lexer);
3161 if (/* The comma at the end of an
3162 enumerator-definition. */
3163 next_token->type != CPP_COMMA
3164 /* The curly brace at the end of an enum-specifier. */
3165 && next_token->type != CPP_CLOSE_BRACE
3166 /* The end of a statement. */
3167 && next_token->type != CPP_SEMICOLON
3168 /* The end of the cast-expression. */
3169 && next_token->type != CPP_CLOSE_PAREN
3170 /* The end of an array bound. */
3171 && next_token->type != CPP_CLOSE_SQUARE
3172 /* The closing ">" in a template-argument-list. */
3173 && (next_token->type != CPP_GREATER
3174 || parser->greater_than_is_operator_p)
3175 /* C++0x only: A ">>" treated like two ">" tokens,
3176 in a template-argument-list. */
3177 && (next_token->type != CPP_RSHIFT
3178 || (cxx_dialect == cxx98)
3179 || parser->greater_than_is_operator_p))
3183 /* If we are within a cast, then the constraint that the
3184 cast is to an integral or enumeration type will be
3185 checked at that point. If we are not within a cast, then
3186 this code is invalid. */
3188 cp_parser_non_integral_constant_expression
3189 (parser, "floating-point literal");
3191 return token->u.value;
3197 /* ??? Should wide strings be allowed when parser->translate_strings_p
3198 is false (i.e. in attributes)? If not, we can kill the third
3199 argument to cp_parser_string_literal. */
3200 return cp_parser_string_literal (parser,
3201 parser->translate_strings_p,
3204 case CPP_OPEN_PAREN:
3207 bool saved_greater_than_is_operator_p;
3209 /* Consume the `('. */
3210 cp_lexer_consume_token (parser->lexer);
3211 /* Within a parenthesized expression, a `>' token is always
3212 the greater-than operator. */
3213 saved_greater_than_is_operator_p
3214 = parser->greater_than_is_operator_p;
3215 parser->greater_than_is_operator_p = true;
3216 /* If we see `( { ' then we are looking at the beginning of
3217 a GNU statement-expression. */
3218 if (cp_parser_allow_gnu_extensions_p (parser)
3219 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3221 /* Statement-expressions are not allowed by the standard. */
3223 pedwarn ("%HISO C++ forbids braced-groups within expressions",
3226 /* And they're not allowed outside of a function-body; you
3227 cannot, for example, write:
3229 int i = ({ int j = 3; j + 1; });
3231 at class or namespace scope. */
3232 if (!parser->in_function_body
3233 || parser->in_template_argument_list_p)
3235 error ("%Hstatement-expressions are not allowed outside "
3236 "functions nor in template-argument lists",
3238 cp_parser_skip_to_end_of_block_or_statement (parser);
3239 expr = error_mark_node;
3243 /* Start the statement-expression. */
3244 expr = begin_stmt_expr ();
3245 /* Parse the compound-statement. */
3246 cp_parser_compound_statement (parser, expr, false);
3248 expr = finish_stmt_expr (expr, false);
3253 /* Parse the parenthesized expression. */
3254 expr = cp_parser_expression (parser, cast_p);
3255 /* Let the front end know that this expression was
3256 enclosed in parentheses. This matters in case, for
3257 example, the expression is of the form `A::B', since
3258 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3260 finish_parenthesized_expr (expr);
3262 /* The `>' token might be the end of a template-id or
3263 template-parameter-list now. */
3264 parser->greater_than_is_operator_p
3265 = saved_greater_than_is_operator_p;
3266 /* Consume the `)'. */
3267 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268 cp_parser_skip_to_end_of_statement (parser);
3274 switch (token->keyword)
3276 /* These two are the boolean literals. */
3278 cp_lexer_consume_token (parser->lexer);
3279 return boolean_true_node;
3281 cp_lexer_consume_token (parser->lexer);
3282 return boolean_false_node;
3284 /* The `__null' literal. */
3286 cp_lexer_consume_token (parser->lexer);
3289 /* Recognize the `this' keyword. */
3291 cp_lexer_consume_token (parser->lexer);
3292 if (parser->local_variables_forbidden_p)
3294 error ("%H%<this%> may not be used in this context",
3296 return error_mark_node;
3298 /* Pointers cannot appear in constant-expressions. */
3299 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300 return error_mark_node;
3301 return finish_this_expr ();
3303 /* The `operator' keyword can be the beginning of an
3308 case RID_FUNCTION_NAME:
3309 case RID_PRETTY_FUNCTION_NAME:
3310 case RID_C99_FUNCTION_NAME:
3311 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3312 __func__ are the names of variables -- but they are
3313 treated specially. Therefore, they are handled here,
3314 rather than relying on the generic id-expression logic
3315 below. Grammatically, these names are id-expressions.
3317 Consume the token. */
3318 token = cp_lexer_consume_token (parser->lexer);
3319 /* Look up the name. */
3320 return finish_fname (token->u.value);
3327 /* The `__builtin_va_arg' construct is used to handle
3328 `va_arg'. Consume the `__builtin_va_arg' token. */
3329 cp_lexer_consume_token (parser->lexer);
3330 /* Look for the opening `('. */
3331 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3332 /* Now, parse the assignment-expression. */
3333 expression = cp_parser_assignment_expression (parser,
3335 /* Look for the `,'. */
3336 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3337 /* Parse the type-id. */
3338 type = cp_parser_type_id (parser);
3339 /* Look for the closing `)'. */
3340 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3341 /* Using `va_arg' in a constant-expression is not
3343 if (cp_parser_non_integral_constant_expression (parser,
3345 return error_mark_node;
3346 return build_x_va_arg (expression, type);
3350 return cp_parser_builtin_offsetof (parser);
3352 case RID_HAS_NOTHROW_ASSIGN:
3353 case RID_HAS_NOTHROW_CONSTRUCTOR:
3354 case RID_HAS_NOTHROW_COPY:
3355 case RID_HAS_TRIVIAL_ASSIGN:
3356 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3357 case RID_HAS_TRIVIAL_COPY:
3358 case RID_HAS_TRIVIAL_DESTRUCTOR:
3359 case RID_HAS_VIRTUAL_DESTRUCTOR:
3360 case RID_IS_ABSTRACT:
3361 case RID_IS_BASE_OF:
3363 case RID_IS_CONVERTIBLE_TO:
3367 case RID_IS_POLYMORPHIC:
3369 return cp_parser_trait_expr (parser, token->keyword);
3371 /* Objective-C++ expressions. */
3373 case RID_AT_PROTOCOL:
3374 case RID_AT_SELECTOR:
3375 return cp_parser_objc_expression (parser);
3378 cp_parser_error (parser, "expected primary-expression");
3379 return error_mark_node;
3382 /* An id-expression can start with either an identifier, a
3383 `::' as the beginning of a qualified-id, or the "operator"
3387 case CPP_TEMPLATE_ID:
3388 case CPP_NESTED_NAME_SPECIFIER:
3392 const char *error_msg;
3395 cp_token *id_expr_token;
3398 /* Parse the id-expression. */
3400 = cp_parser_id_expression (parser,
3401 /*template_keyword_p=*/false,
3402 /*check_dependency_p=*/true,
3404 /*declarator_p=*/false,
3405 /*optional_p=*/false);
3406 if (id_expression == error_mark_node)
3407 return error_mark_node;
3408 id_expr_token = token;
3409 token = cp_lexer_peek_token (parser->lexer);
3410 done = (token->type != CPP_OPEN_SQUARE
3411 && token->type != CPP_OPEN_PAREN
3412 && token->type != CPP_DOT
3413 && token->type != CPP_DEREF
3414 && token->type != CPP_PLUS_PLUS
3415 && token->type != CPP_MINUS_MINUS);
3416 /* If we have a template-id, then no further lookup is
3417 required. If the template-id was for a template-class, we
3418 will sometimes have a TYPE_DECL at this point. */
3419 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3420 || TREE_CODE (id_expression) == TYPE_DECL)
3421 decl = id_expression;
3422 /* Look up the name. */
3425 tree ambiguous_decls;
3427 decl = cp_parser_lookup_name (parser, id_expression,
3430 /*is_namespace=*/false,
3431 /*check_dependency=*/true,
3433 id_expr_token->location);
3434 /* If the lookup was ambiguous, an error will already have
3436 if (ambiguous_decls)
3437 return error_mark_node;
3439 /* In Objective-C++, an instance variable (ivar) may be preferred
3440 to whatever cp_parser_lookup_name() found. */
3441 decl = objc_lookup_ivar (decl, id_expression);
3443 /* If name lookup gives us a SCOPE_REF, then the
3444 qualifying scope was dependent. */
3445 if (TREE_CODE (decl) == SCOPE_REF)
3447 /* At this point, we do not know if DECL is a valid
3448 integral constant expression. We assume that it is
3449 in fact such an expression, so that code like:
3451 template <int N> struct A {
3455 is accepted. At template-instantiation time, we
3456 will check that B<N>::i is actually a constant. */
3459 /* Check to see if DECL is a local variable in a context
3460 where that is forbidden. */
3461 if (parser->local_variables_forbidden_p
3462 && local_variable_p (decl))
3464 /* It might be that we only found DECL because we are
3465 trying to be generous with pre-ISO scoping rules.
3466 For example, consider:
3470 for (int i = 0; i < 10; ++i) {}
3471 extern void f(int j = i);
3474 Here, name look up will originally find the out
3475 of scope `i'. We need to issue a warning message,
3476 but then use the global `i'. */
3477 decl = check_for_out_of_scope_variable (decl);
3478 if (local_variable_p (decl))
3480 error ("%Hlocal variable %qD may not appear in this context",
3481 &id_expr_token->location, decl);
3482 return error_mark_node;
3487 decl = (finish_id_expression
3488 (id_expression, decl, parser->scope,
3490 parser->integral_constant_expression_p,
3491 parser->allow_non_integral_constant_expression_p,
3492 &parser->non_integral_constant_expression_p,
3493 template_p, done, address_p,
3496 id_expr_token->location));
3498 cp_parser_error (parser, error_msg);
3502 /* Anything else is an error. */
3504 /* ...unless we have an Objective-C++ message or string literal,
3506 if (c_dialect_objc ()
3507 && (token->type == CPP_OPEN_SQUARE
3508 || token->type == CPP_OBJC_STRING))
3509 return cp_parser_objc_expression (parser);
3511 cp_parser_error (parser, "expected primary-expression");
3512 return error_mark_node;
3516 /* Parse an id-expression.
3523 :: [opt] nested-name-specifier template [opt] unqualified-id
3525 :: operator-function-id
3528 Return a representation of the unqualified portion of the
3529 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3530 a `::' or nested-name-specifier.
3532 Often, if the id-expression was a qualified-id, the caller will
3533 want to make a SCOPE_REF to represent the qualified-id. This
3534 function does not do this in order to avoid wastefully creating
3535 SCOPE_REFs when they are not required.
3537 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3540 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3541 uninstantiated templates.
3543 If *TEMPLATE_P is non-NULL, it is set to true iff the
3544 `template' keyword is used to explicitly indicate that the entity
3545 named is a template.
3547 If DECLARATOR_P is true, the id-expression is appearing as part of
3548 a declarator, rather than as part of an expression. */
3551 cp_parser_id_expression (cp_parser *parser,
3552 bool template_keyword_p,
3553 bool check_dependency_p,
3558 bool global_scope_p;
3559 bool nested_name_specifier_p;
3561 /* Assume the `template' keyword was not used. */
3563 *template_p = template_keyword_p;
3565 /* Look for the optional `::' operator. */
3567 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3569 /* Look for the optional nested-name-specifier. */
3570 nested_name_specifier_p
3571 = (cp_parser_nested_name_specifier_opt (parser,
3572 /*typename_keyword_p=*/false,
3577 /* If there is a nested-name-specifier, then we are looking at
3578 the first qualified-id production. */
3579 if (nested_name_specifier_p)
3582 tree saved_object_scope;
3583 tree saved_qualifying_scope;
3584 tree unqualified_id;
3587 /* See if the next token is the `template' keyword. */
3589 template_p = &is_template;
3590 *template_p = cp_parser_optional_template_keyword (parser);
3591 /* Name lookup we do during the processing of the
3592 unqualified-id might obliterate SCOPE. */
3593 saved_scope = parser->scope;
3594 saved_object_scope = parser->object_scope;
3595 saved_qualifying_scope = parser->qualifying_scope;
3596 /* Process the final unqualified-id. */
3597 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3600 /*optional_p=*/false);
3601 /* Restore the SAVED_SCOPE for our caller. */
3602 parser->scope = saved_scope;
3603 parser->object_scope = saved_object_scope;
3604 parser->qualifying_scope = saved_qualifying_scope;
3606 return unqualified_id;
3608 /* Otherwise, if we are in global scope, then we are looking at one
3609 of the other qualified-id productions. */
3610 else if (global_scope_p)
3615 /* Peek at the next token. */
3616 token = cp_lexer_peek_token (parser->lexer);
3618 /* If it's an identifier, and the next token is not a "<", then
3619 we can avoid the template-id case. This is an optimization
3620 for this common case. */
3621 if (token->type == CPP_NAME
3622 && !cp_parser_nth_token_starts_template_argument_list_p
3624 return cp_parser_identifier (parser);
3626 cp_parser_parse_tentatively (parser);
3627 /* Try a template-id. */
3628 id = cp_parser_template_id (parser,
3629 /*template_keyword_p=*/false,
3630 /*check_dependency_p=*/true,
3632 /* If that worked, we're done. */
3633 if (cp_parser_parse_definitely (parser))
3636 /* Peek at the next token. (Changes in the token buffer may
3637 have invalidated the pointer obtained above.) */
3638 token = cp_lexer_peek_token (parser->lexer);
3640 switch (token->type)
3643 return cp_parser_identifier (parser);
3646 if (token->keyword == RID_OPERATOR)
3647 return cp_parser_operator_function_id (parser);
3651 cp_parser_error (parser, "expected id-expression");
3652 return error_mark_node;
3656 return cp_parser_unqualified_id (parser, template_keyword_p,
3657 /*check_dependency_p=*/true,
3662 /* Parse an unqualified-id.
3666 operator-function-id
3667 conversion-function-id
3671 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3672 keyword, in a construct like `A::template ...'.
3674 Returns a representation of unqualified-id. For the `identifier'
3675 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3676 production a BIT_NOT_EXPR is returned; the operand of the
3677 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3678 other productions, see the documentation accompanying the
3679 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3680 names are looked up in uninstantiated templates. If DECLARATOR_P
3681 is true, the unqualified-id is appearing as part of a declarator,
3682 rather than as part of an expression. */
3685 cp_parser_unqualified_id (cp_parser* parser,
3686 bool template_keyword_p,
3687 bool check_dependency_p,
3693 /* Peek at the next token. */
3694 token = cp_lexer_peek_token (parser->lexer);
3696 switch (token->type)
3702 /* We don't know yet whether or not this will be a
3704 cp_parser_parse_tentatively (parser);
3705 /* Try a template-id. */
3706 id = cp_parser_template_id (parser, template_keyword_p,
3709 /* If it worked, we're done. */
3710 if (cp_parser_parse_definitely (parser))
3712 /* Otherwise, it's an ordinary identifier. */
3713 return cp_parser_identifier (parser);
3716 case CPP_TEMPLATE_ID:
3717 return cp_parser_template_id (parser, template_keyword_p,
3724 tree qualifying_scope;
3729 /* Consume the `~' token. */
3730 cp_lexer_consume_token (parser->lexer);
3731 /* Parse the class-name. The standard, as written, seems to
3734 template <typename T> struct S { ~S (); };
3735 template <typename T> S<T>::~S() {}
3737 is invalid, since `~' must be followed by a class-name, but
3738 `S<T>' is dependent, and so not known to be a class.
3739 That's not right; we need to look in uninstantiated
3740 templates. A further complication arises from:
3742 template <typename T> void f(T t) {
3746 Here, it is not possible to look up `T' in the scope of `T'
3747 itself. We must look in both the current scope, and the
3748 scope of the containing complete expression.
3750 Yet another issue is:
3759 The standard does not seem to say that the `S' in `~S'
3760 should refer to the type `S' and not the data member
3763 /* DR 244 says that we look up the name after the "~" in the
3764 same scope as we looked up the qualifying name. That idea
3765 isn't fully worked out; it's more complicated than that. */
3766 scope = parser->scope;
3767 object_scope = parser->object_scope;
3768 qualifying_scope = parser->qualifying_scope;
3770 /* Check for invalid scopes. */
3771 if (scope == error_mark_node)
3773 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3774 cp_lexer_consume_token (parser->lexer);
3775 return error_mark_node;
3777 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3779 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3780 error ("%Hscope %qT before %<~%> is not a class-name",
3781 &token->location, scope);
3782 cp_parser_simulate_error (parser);
3783 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3784 cp_lexer_consume_token (parser->lexer);
3785 return error_mark_node;
3787 gcc_assert (!scope || TYPE_P (scope));
3789 /* If the name is of the form "X::~X" it's OK. */
3790 token = cp_lexer_peek_token (parser->lexer);
3792 && token->type == CPP_NAME
3793 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3795 && constructor_name_p (token->u.value, scope))
3797 cp_lexer_consume_token (parser->lexer);
3798 return build_nt (BIT_NOT_EXPR, scope);
3801 /* If there was an explicit qualification (S::~T), first look
3802 in the scope given by the qualification (i.e., S). */
3804 type_decl = NULL_TREE;
3807 cp_parser_parse_tentatively (parser);
3808 type_decl = cp_parser_class_name (parser,
3809 /*typename_keyword_p=*/false,
3810 /*template_keyword_p=*/false,
3812 /*check_dependency=*/false,
3813 /*class_head_p=*/false,
3815 if (cp_parser_parse_definitely (parser))
3818 /* In "N::S::~S", look in "N" as well. */
3819 if (!done && scope && qualifying_scope)
3821 cp_parser_parse_tentatively (parser);
3822 parser->scope = qualifying_scope;
3823 parser->object_scope = NULL_TREE;
3824 parser->qualifying_scope = NULL_TREE;
3826 = cp_parser_class_name (parser,
3827 /*typename_keyword_p=*/false,
3828 /*template_keyword_p=*/false,
3830 /*check_dependency=*/false,
3831 /*class_head_p=*/false,
3833 if (cp_parser_parse_definitely (parser))
3836 /* In "p->S::~T", look in the scope given by "*p" as well. */
3837 else if (!done && object_scope)
3839 cp_parser_parse_tentatively (parser);
3840 parser->scope = object_scope;
3841 parser->object_scope = NULL_TREE;
3842 parser->qualifying_scope = NULL_TREE;
3844 = cp_parser_class_name (parser,
3845 /*typename_keyword_p=*/false,
3846 /*template_keyword_p=*/false,
3848 /*check_dependency=*/false,
3849 /*class_head_p=*/false,
3851 if (cp_parser_parse_definitely (parser))
3854 /* Look in the surrounding context. */
3857 parser->scope = NULL_TREE;
3858 parser->object_scope = NULL_TREE;
3859 parser->qualifying_scope = NULL_TREE;
3861 = cp_parser_class_name (parser,
3862 /*typename_keyword_p=*/false,
3863 /*template_keyword_p=*/false,
3865 /*check_dependency=*/false,
3866 /*class_head_p=*/false,
3869 /* If an error occurred, assume that the name of the
3870 destructor is the same as the name of the qualifying
3871 class. That allows us to keep parsing after running
3872 into ill-formed destructor names. */
3873 if (type_decl == error_mark_node && scope)
3874 return build_nt (BIT_NOT_EXPR, scope);
3875 else if (type_decl == error_mark_node)
3876 return error_mark_node;
3878 /* Check that destructor name and scope match. */
3879 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3881 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3882 error ("%Hdeclaration of %<~%T%> as member of %qT",
3883 &token->location, type_decl, scope);
3884 cp_parser_simulate_error (parser);
3885 return error_mark_node;
3890 A typedef-name that names a class shall not be used as the
3891 identifier in the declarator for a destructor declaration. */
3893 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3894 && !DECL_SELF_REFERENCE_P (type_decl)
3895 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3896 error ("%Htypedef-name %qD used as destructor declarator",
3897 &token->location, type_decl);
3899 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3903 if (token->keyword == RID_OPERATOR)
3907 /* This could be a template-id, so we try that first. */
3908 cp_parser_parse_tentatively (parser);
3909 /* Try a template-id. */
3910 id = cp_parser_template_id (parser, template_keyword_p,
3911 /*check_dependency_p=*/true,
3913 /* If that worked, we're done. */
3914 if (cp_parser_parse_definitely (parser))
3916 /* We still don't know whether we're looking at an
3917 operator-function-id or a conversion-function-id. */
3918 cp_parser_parse_tentatively (parser);
3919 /* Try an operator-function-id. */
3920 id = cp_parser_operator_function_id (parser);
3921 /* If that didn't work, try a conversion-function-id. */
3922 if (!cp_parser_parse_definitely (parser))
3923 id = cp_parser_conversion_function_id (parser);
3932 cp_parser_error (parser, "expected unqualified-id");
3933 return error_mark_node;
3937 /* Parse an (optional) nested-name-specifier.
3939 nested-name-specifier:
3940 class-or-namespace-name :: nested-name-specifier [opt]
3941 class-or-namespace-name :: template nested-name-specifier [opt]
3943 PARSER->SCOPE should be set appropriately before this function is
3944 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3945 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3948 Sets PARSER->SCOPE to the class (TYPE) or namespace
3949 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3950 it unchanged if there is no nested-name-specifier. Returns the new
3951 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3953 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3954 part of a declaration and/or decl-specifier. */
3957 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3958 bool typename_keyword_p,
3959 bool check_dependency_p,
3961 bool is_declaration)
3963 bool success = false;
3964 cp_token_position start = 0;
3967 /* Remember where the nested-name-specifier starts. */
3968 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3970 start = cp_lexer_token_position (parser->lexer, false);
3971 push_deferring_access_checks (dk_deferred);
3978 tree saved_qualifying_scope;
3979 bool template_keyword_p;
3981 /* Spot cases that cannot be the beginning of a
3982 nested-name-specifier. */
3983 token = cp_lexer_peek_token (parser->lexer);
3985 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3986 the already parsed nested-name-specifier. */
3987 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3989 /* Grab the nested-name-specifier and continue the loop. */
3990 cp_parser_pre_parsed_nested_name_specifier (parser);
3991 /* If we originally encountered this nested-name-specifier
3992 with IS_DECLARATION set to false, we will not have
3993 resolved TYPENAME_TYPEs, so we must do so here. */
3995 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3997 new_scope = resolve_typename_type (parser->scope,
3998 /*only_current_p=*/false);
3999 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4000 parser->scope = new_scope;
4006 /* Spot cases that cannot be the beginning of a
4007 nested-name-specifier. On the second and subsequent times
4008 through the loop, we look for the `template' keyword. */
4009 if (success && token->keyword == RID_TEMPLATE)
4011 /* A template-id can start a nested-name-specifier. */
4012 else if (token->type == CPP_TEMPLATE_ID)
4016 /* If the next token is not an identifier, then it is
4017 definitely not a class-or-namespace-name. */
4018 if (token->type != CPP_NAME)
4020 /* If the following token is neither a `<' (to begin a
4021 template-id), nor a `::', then we are not looking at a
4022 nested-name-specifier. */
4023 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4024 if (token->type != CPP_SCOPE
4025 && !cp_parser_nth_token_starts_template_argument_list_p
4030 /* The nested-name-specifier is optional, so we parse
4032 cp_parser_parse_tentatively (parser);
4034 /* Look for the optional `template' keyword, if this isn't the
4035 first time through the loop. */
4037 template_keyword_p = cp_parser_optional_template_keyword (parser);
4039 template_keyword_p = false;
4041 /* Save the old scope since the name lookup we are about to do
4042 might destroy it. */
4043 old_scope = parser->scope;
4044 saved_qualifying_scope = parser->qualifying_scope;
4045 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4046 look up names in "X<T>::I" in order to determine that "Y" is
4047 a template. So, if we have a typename at this point, we make
4048 an effort to look through it. */
4050 && !typename_keyword_p
4052 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4053 parser->scope = resolve_typename_type (parser->scope,
4054 /*only_current_p=*/false);
4055 /* Parse the qualifying entity. */
4057 = cp_parser_class_or_namespace_name (parser,
4063 /* Look for the `::' token. */
4064 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4066 /* If we found what we wanted, we keep going; otherwise, we're
4068 if (!cp_parser_parse_definitely (parser))
4070 bool error_p = false;
4072 /* Restore the OLD_SCOPE since it was valid before the
4073 failed attempt at finding the last
4074 class-or-namespace-name. */
4075 parser->scope = old_scope;
4076 parser->qualifying_scope = saved_qualifying_scope;
4077 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4079 /* If the next token is an identifier, and the one after
4080 that is a `::', then any valid interpretation would have
4081 found a class-or-namespace-name. */
4082 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4083 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4085 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4088 token = cp_lexer_consume_token (parser->lexer);
4091 if (!token->ambiguous_p)
4094 tree ambiguous_decls;
4096 decl = cp_parser_lookup_name (parser, token->u.value,
4098 /*is_template=*/false,
4099 /*is_namespace=*/false,
4100 /*check_dependency=*/true,
4103 if (TREE_CODE (decl) == TEMPLATE_DECL)
4104 error ("%H%qD used without template parameters",
4105 &token->location, decl);
4106 else if (ambiguous_decls)
4108 error ("%Hreference to %qD is ambiguous",
4109 &token->location, token->u.value);
4110 print_candidates (ambiguous_decls);
4111 decl = error_mark_node;
4114 cp_parser_name_lookup_error
4115 (parser, token->u.value, decl,
4116 "is not a class or namespace",
4119 parser->scope = error_mark_node;
4121 /* Treat this as a successful nested-name-specifier
4126 If the name found is not a class-name (clause
4127 _class_) or namespace-name (_namespace.def_), the
4128 program is ill-formed. */
4131 cp_lexer_consume_token (parser->lexer);
4135 /* We've found one valid nested-name-specifier. */
4137 /* Name lookup always gives us a DECL. */
4138 if (TREE_CODE (new_scope) == TYPE_DECL)
4139 new_scope = TREE_TYPE (new_scope);
4140 /* Uses of "template" must be followed by actual templates. */
4141 if (template_keyword_p
4142 && !(CLASS_TYPE_P (new_scope)
4143 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4144 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4145 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4146 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4147 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4148 == TEMPLATE_ID_EXPR)))
4149 permerror (TYPE_P (new_scope)
4150 ? "%qT is not a template"
4151 : "%qD is not a template",
4153 /* If it is a class scope, try to complete it; we are about to
4154 be looking up names inside the class. */
4155 if (TYPE_P (new_scope)
4156 /* Since checking types for dependency can be expensive,
4157 avoid doing it if the type is already complete. */
4158 && !COMPLETE_TYPE_P (new_scope)
4159 /* Do not try to complete dependent types. */
4160 && !dependent_type_p (new_scope))
4162 new_scope = complete_type (new_scope);
4163 /* If it is a typedef to current class, use the current
4164 class instead, as the typedef won't have any names inside
4166 if (!COMPLETE_TYPE_P (new_scope)
4167 && currently_open_class (new_scope))
4168 new_scope = TYPE_MAIN_VARIANT (new_scope);
4170 /* Make sure we look in the right scope the next time through
4172 parser->scope = new_scope;
4175 /* If parsing tentatively, replace the sequence of tokens that makes
4176 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4177 token. That way, should we re-parse the token stream, we will
4178 not have to repeat the effort required to do the parse, nor will
4179 we issue duplicate error messages. */
4180 if (success && start)
4184 token = cp_lexer_token_at (parser->lexer, start);
4185 /* Reset the contents of the START token. */
4186 token->type = CPP_NESTED_NAME_SPECIFIER;
4187 /* Retrieve any deferred checks. Do not pop this access checks yet
4188 so the memory will not be reclaimed during token replacing below. */
4189 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4190 token->u.tree_check_value->value = parser->scope;
4191 token->u.tree_check_value->checks = get_deferred_access_checks ();
4192 token->u.tree_check_value->qualifying_scope =
4193 parser->qualifying_scope;
4194 token->keyword = RID_MAX;
4196 /* Purge all subsequent tokens. */
4197 cp_lexer_purge_tokens_after (parser->lexer, start);
4201 pop_to_parent_deferring_access_checks ();
4203 return success ? parser->scope : NULL_TREE;
4206 /* Parse a nested-name-specifier. See
4207 cp_parser_nested_name_specifier_opt for details. This function
4208 behaves identically, except that it will an issue an error if no
4209 nested-name-specifier is present. */
4212 cp_parser_nested_name_specifier (cp_parser *parser,
4213 bool typename_keyword_p,
4214 bool check_dependency_p,
4216 bool is_declaration)
4220 /* Look for the nested-name-specifier. */
4221 scope = cp_parser_nested_name_specifier_opt (parser,
4226 /* If it was not present, issue an error message. */
4229 cp_parser_error (parser, "expected nested-name-specifier");
4230 parser->scope = NULL_TREE;
4236 /* Parse a class-or-namespace-name.
4238 class-or-namespace-name:
4242 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4243 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4244 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4245 TYPE_P is TRUE iff the next name should be taken as a class-name,
4246 even the same name is declared to be another entity in the same
4249 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4250 specified by the class-or-namespace-name. If neither is found the
4251 ERROR_MARK_NODE is returned. */
4254 cp_parser_class_or_namespace_name (cp_parser *parser,
4255 bool typename_keyword_p,
4256 bool template_keyword_p,
4257 bool check_dependency_p,
4259 bool is_declaration)
4262 tree saved_qualifying_scope;
4263 tree saved_object_scope;
4267 /* Before we try to parse the class-name, we must save away the
4268 current PARSER->SCOPE since cp_parser_class_name will destroy
4270 saved_scope = parser->scope;
4271 saved_qualifying_scope = parser->qualifying_scope;
4272 saved_object_scope = parser->object_scope;
4273 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4274 there is no need to look for a namespace-name. */
4275 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4277 cp_parser_parse_tentatively (parser);
4278 scope = cp_parser_class_name (parser,
4281 type_p ? class_type : none_type,
4283 /*class_head_p=*/false,
4285 /* If that didn't work, try for a namespace-name. */
4286 if (!only_class_p && !cp_parser_parse_definitely (parser))
4288 /* Restore the saved scope. */
4289 parser->scope = saved_scope;
4290 parser->qualifying_scope = saved_qualifying_scope;
4291 parser->object_scope = saved_object_scope;
4292 /* If we are not looking at an identifier followed by the scope
4293 resolution operator, then this is not part of a
4294 nested-name-specifier. (Note that this function is only used
4295 to parse the components of a nested-name-specifier.) */
4296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4297 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4298 return error_mark_node;
4299 scope = cp_parser_namespace_name (parser);
4305 /* Parse a postfix-expression.
4309 postfix-expression [ expression ]
4310 postfix-expression ( expression-list [opt] )
4311 simple-type-specifier ( expression-list [opt] )
4312 typename :: [opt] nested-name-specifier identifier
4313 ( expression-list [opt] )
4314 typename :: [opt] nested-name-specifier template [opt] template-id
4315 ( expression-list [opt] )
4316 postfix-expression . template [opt] id-expression
4317 postfix-expression -> template [opt] id-expression
4318 postfix-expression . pseudo-destructor-name
4319 postfix-expression -> pseudo-destructor-name
4320 postfix-expression ++
4321 postfix-expression --
4322 dynamic_cast < type-id > ( expression )
4323 static_cast < type-id > ( expression )
4324 reinterpret_cast < type-id > ( expression )
4325 const_cast < type-id > ( expression )
4326 typeid ( expression )
4332 ( type-id ) { initializer-list , [opt] }
4334 This extension is a GNU version of the C99 compound-literal
4335 construct. (The C99 grammar uses `type-name' instead of `type-id',
4336 but they are essentially the same concept.)
4338 If ADDRESS_P is true, the postfix expression is the operand of the
4339 `&' operator. CAST_P is true if this expression is the target of a
4342 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4343 class member access expressions [expr.ref].
4345 Returns a representation of the expression. */
4348 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4349 bool member_access_only_p)
4353 cp_id_kind idk = CP_ID_KIND_NONE;
4354 tree postfix_expression = NULL_TREE;
4355 bool is_member_access = false;
4357 /* Peek at the next token. */
4358 token = cp_lexer_peek_token (parser->lexer);
4359 /* Some of the productions are determined by keywords. */
4360 keyword = token->keyword;
4370 const char *saved_message;
4372 /* All of these can be handled in the same way from the point
4373 of view of parsing. Begin by consuming the token
4374 identifying the cast. */
4375 cp_lexer_consume_token (parser->lexer);
4377 /* New types cannot be defined in the cast. */
4378 saved_message = parser->type_definition_forbidden_message;
4379 parser->type_definition_forbidden_message
4380 = "types may not be defined in casts";
4382 /* Look for the opening `<'. */
4383 cp_parser_require (parser, CPP_LESS, "%<<%>");
4384 /* Parse the type to which we are casting. */
4385 type = cp_parser_type_id (parser);
4386 /* Look for the closing `>'. */
4387 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4388 /* Restore the old message. */
4389 parser->type_definition_forbidden_message = saved_message;
4391 /* And the expression which is being cast. */
4392 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4393 expression = cp_parser_expression (parser, /*cast_p=*/true);
4394 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4396 /* Only type conversions to integral or enumeration types
4397 can be used in constant-expressions. */
4398 if (!cast_valid_in_integral_constant_expression_p (type)
4399 && (cp_parser_non_integral_constant_expression
4401 "a cast to a type other than an integral or "
4402 "enumeration type")))
4403 return error_mark_node;
4409 = build_dynamic_cast (type, expression, tf_warning_or_error);
4413 = build_static_cast (type, expression, tf_warning_or_error);
4417 = build_reinterpret_cast (type, expression,
4418 tf_warning_or_error);
4422 = build_const_cast (type, expression, tf_warning_or_error);
4433 const char *saved_message;
4434 bool saved_in_type_id_in_expr_p;
4436 /* Consume the `typeid' token. */
4437 cp_lexer_consume_token (parser->lexer);
4438 /* Look for the `(' token. */
4439 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4440 /* Types cannot be defined in a `typeid' expression. */
4441 saved_message = parser->type_definition_forbidden_message;
4442 parser->type_definition_forbidden_message
4443 = "types may not be defined in a %<typeid%> expression";
4444 /* We can't be sure yet whether we're looking at a type-id or an
4446 cp_parser_parse_tentatively (parser);
4447 /* Try a type-id first. */
4448 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4449 parser->in_type_id_in_expr_p = true;
4450 type = cp_parser_type_id (parser);
4451 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4452 /* Look for the `)' token. Otherwise, we can't be sure that
4453 we're not looking at an expression: consider `typeid (int
4454 (3))', for example. */
4455 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4456 /* If all went well, simply lookup the type-id. */
4457 if (cp_parser_parse_definitely (parser))
4458 postfix_expression = get_typeid (type);
4459 /* Otherwise, fall back to the expression variant. */
4464 /* Look for an expression. */
4465 expression = cp_parser_expression (parser, /*cast_p=*/false);
4466 /* Compute its typeid. */
4467 postfix_expression = build_typeid (expression);
4468 /* Look for the `)' token. */
4469 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4471 /* Restore the saved message. */
4472 parser->type_definition_forbidden_message = saved_message;
4473 /* `typeid' may not appear in an integral constant expression. */
4474 if (cp_parser_non_integral_constant_expression(parser,
4475 "%<typeid%> operator"))
4476 return error_mark_node;
4483 /* The syntax permitted here is the same permitted for an
4484 elaborated-type-specifier. */
4485 type = cp_parser_elaborated_type_specifier (parser,
4486 /*is_friend=*/false,
4487 /*is_declaration=*/false);
4488 postfix_expression = cp_parser_functional_cast (parser, type);
4496 /* If the next thing is a simple-type-specifier, we may be
4497 looking at a functional cast. We could also be looking at
4498 an id-expression. So, we try the functional cast, and if
4499 that doesn't work we fall back to the primary-expression. */
4500 cp_parser_parse_tentatively (parser);
4501 /* Look for the simple-type-specifier. */
4502 type = cp_parser_simple_type_specifier (parser,
4503 /*decl_specs=*/NULL,
4504 CP_PARSER_FLAGS_NONE);
4505 /* Parse the cast itself. */
4506 if (!cp_parser_error_occurred (parser))
4508 = cp_parser_functional_cast (parser, type);
4509 /* If that worked, we're done. */
4510 if (cp_parser_parse_definitely (parser))
4513 /* If the functional-cast didn't work out, try a
4514 compound-literal. */
4515 if (cp_parser_allow_gnu_extensions_p (parser)
4516 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4518 VEC(constructor_elt,gc) *initializer_list = NULL;
4519 bool saved_in_type_id_in_expr_p;
4521 cp_parser_parse_tentatively (parser);
4522 /* Consume the `('. */
4523 cp_lexer_consume_token (parser->lexer);
4524 /* Parse the type. */
4525 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4526 parser->in_type_id_in_expr_p = true;
4527 type = cp_parser_type_id (parser);
4528 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4529 /* Look for the `)'. */
4530 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4531 /* Look for the `{'. */
4532 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4533 /* If things aren't going well, there's no need to
4535 if (!cp_parser_error_occurred (parser))
4537 bool non_constant_p;
4538 /* Parse the initializer-list. */
4540 = cp_parser_initializer_list (parser, &non_constant_p);
4541 /* Allow a trailing `,'. */
4542 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4543 cp_lexer_consume_token (parser->lexer);
4544 /* Look for the final `}'. */
4545 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4547 /* If that worked, we're definitely looking at a
4548 compound-literal expression. */
4549 if (cp_parser_parse_definitely (parser))
4551 /* Warn the user that a compound literal is not
4552 allowed in standard C++. */
4554 pedwarn ("ISO C++ forbids compound-literals");
4555 /* For simplicity, we disallow compound literals in
4556 constant-expressions. We could
4557 allow compound literals of integer type, whose
4558 initializer was a constant, in constant
4559 expressions. Permitting that usage, as a further
4560 extension, would not change the meaning of any
4561 currently accepted programs. (Of course, as
4562 compound literals are not part of ISO C++, the
4563 standard has nothing to say.) */
4564 if (cp_parser_non_integral_constant_expression
4565 (parser, "non-constant compound literals"))
4567 postfix_expression = error_mark_node;
4570 /* Form the representation of the compound-literal. */
4572 = (finish_compound_literal
4573 (type, build_constructor (init_list_type_node,
4574 initializer_list)));
4579 /* It must be a primary-expression. */
4581 = cp_parser_primary_expression (parser, address_p, cast_p,
4582 /*template_arg_p=*/false,
4588 /* Keep looping until the postfix-expression is complete. */
4591 if (idk == CP_ID_KIND_UNQUALIFIED
4592 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4593 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4594 /* It is not a Koenig lookup function call. */
4596 = unqualified_name_lookup_error (postfix_expression);
4598 /* Peek at the next token. */
4599 token = cp_lexer_peek_token (parser->lexer);
4601 switch (token->type)
4603 case CPP_OPEN_SQUARE:
4605 = cp_parser_postfix_open_square_expression (parser,
4608 idk = CP_ID_KIND_NONE;
4609 is_member_access = false;
4612 case CPP_OPEN_PAREN:
4613 /* postfix-expression ( expression-list [opt] ) */
4616 bool is_builtin_constant_p;
4617 bool saved_integral_constant_expression_p = false;
4618 bool saved_non_integral_constant_expression_p = false;
4621 is_member_access = false;
4623 is_builtin_constant_p
4624 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4625 if (is_builtin_constant_p)
4627 /* The whole point of __builtin_constant_p is to allow
4628 non-constant expressions to appear as arguments. */
4629 saved_integral_constant_expression_p
4630 = parser->integral_constant_expression_p;
4631 saved_non_integral_constant_expression_p
4632 = parser->non_integral_constant_expression_p;
4633 parser->integral_constant_expression_p = false;
4635 args = (cp_parser_parenthesized_expression_list
4636 (parser, /*is_attribute_list=*/false,
4637 /*cast_p=*/false, /*allow_expansion_p=*/true,
4638 /*non_constant_p=*/NULL));
4639 if (is_builtin_constant_p)
4641 parser->integral_constant_expression_p
4642 = saved_integral_constant_expression_p;
4643 parser->non_integral_constant_expression_p
4644 = saved_non_integral_constant_expression_p;
4647 if (args == error_mark_node)
4649 postfix_expression = error_mark_node;
4653 /* Function calls are not permitted in
4654 constant-expressions. */
4655 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4656 && cp_parser_non_integral_constant_expression (parser,
4659 postfix_expression = error_mark_node;
4664 if (idk == CP_ID_KIND_UNQUALIFIED)
4666 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4672 = perform_koenig_lookup (postfix_expression, args);
4676 = unqualified_fn_lookup_error (postfix_expression);
4678 /* We do not perform argument-dependent lookup if
4679 normal lookup finds a non-function, in accordance
4680 with the expected resolution of DR 218. */
4681 else if (args && is_overloaded_fn (postfix_expression))
4683 tree fn = get_first_fn (postfix_expression);
4685 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4686 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4688 /* Only do argument dependent lookup if regular
4689 lookup does not find a set of member functions.
4690 [basic.lookup.koenig]/2a */
4691 if (!DECL_FUNCTION_MEMBER_P (fn))
4695 = perform_koenig_lookup (postfix_expression, args);
4700 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4702 tree instance = TREE_OPERAND (postfix_expression, 0);
4703 tree fn = TREE_OPERAND (postfix_expression, 1);
4705 if (processing_template_decl
4706 && (type_dependent_expression_p (instance)
4707 || (!BASELINK_P (fn)
4708 && TREE_CODE (fn) != FIELD_DECL)
4709 || type_dependent_expression_p (fn)
4710 || any_type_dependent_arguments_p (args)))
4713 = build_nt_call_list (postfix_expression, args);
4717 if (BASELINK_P (fn))
4719 = (build_new_method_call
4720 (instance, fn, args, NULL_TREE,
4721 (idk == CP_ID_KIND_QUALIFIED
4722 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4724 tf_warning_or_error));
4727 = finish_call_expr (postfix_expression, args,
4728 /*disallow_virtual=*/false,
4730 tf_warning_or_error);
4732 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4733 || TREE_CODE (postfix_expression) == MEMBER_REF
4734 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4735 postfix_expression = (build_offset_ref_call_from_tree
4736 (postfix_expression, args));
4737 else if (idk == CP_ID_KIND_QUALIFIED)
4738 /* A call to a static class member, or a namespace-scope
4741 = finish_call_expr (postfix_expression, args,
4742 /*disallow_virtual=*/true,
4744 tf_warning_or_error);
4746 /* All other function calls. */
4748 = finish_call_expr (postfix_expression, args,
4749 /*disallow_virtual=*/false,
4751 tf_warning_or_error);
4753 if (warn_disallowed_functions)
4754 warn_if_disallowed_function_p (postfix_expression);
4756 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4757 idk = CP_ID_KIND_NONE;
4763 /* postfix-expression . template [opt] id-expression
4764 postfix-expression . pseudo-destructor-name
4765 postfix-expression -> template [opt] id-expression
4766 postfix-expression -> pseudo-destructor-name */
4768 /* Consume the `.' or `->' operator. */
4769 cp_lexer_consume_token (parser->lexer);
4772 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4777 is_member_access = true;
4781 /* postfix-expression ++ */
4782 /* Consume the `++' token. */
4783 cp_lexer_consume_token (parser->lexer);
4784 /* Generate a representation for the complete expression. */
4786 = finish_increment_expr (postfix_expression,
4787 POSTINCREMENT_EXPR);
4788 /* Increments may not appear in constant-expressions. */
4789 if (cp_parser_non_integral_constant_expression (parser,
4791 postfix_expression = error_mark_node;
4792 idk = CP_ID_KIND_NONE;
4793 is_member_access = false;
4796 case CPP_MINUS_MINUS:
4797 /* postfix-expression -- */
4798 /* Consume the `--' token. */
4799 cp_lexer_consume_token (parser->lexer);
4800 /* Generate a representation for the complete expression. */
4802 = finish_increment_expr (postfix_expression,
4803 POSTDECREMENT_EXPR);
4804 /* Decrements may not appear in constant-expressions. */
4805 if (cp_parser_non_integral_constant_expression (parser,
4807 postfix_expression = error_mark_node;
4808 idk = CP_ID_KIND_NONE;
4809 is_member_access = false;
4813 if (member_access_only_p)
4814 return is_member_access? postfix_expression : error_mark_node;
4816 return postfix_expression;
4820 /* We should never get here. */
4822 return error_mark_node;
4825 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4826 by cp_parser_builtin_offsetof. We're looking for
4828 postfix-expression [ expression ]
4830 FOR_OFFSETOF is set if we're being called in that context, which
4831 changes how we deal with integer constant expressions. */
4834 cp_parser_postfix_open_square_expression (cp_parser *parser,
4835 tree postfix_expression,
4840 /* Consume the `[' token. */
4841 cp_lexer_consume_token (parser->lexer);
4843 /* Parse the index expression. */
4844 /* ??? For offsetof, there is a question of what to allow here. If
4845 offsetof is not being used in an integral constant expression context,
4846 then we *could* get the right answer by computing the value at runtime.
4847 If we are in an integral constant expression context, then we might
4848 could accept any constant expression; hard to say without analysis.
4849 Rather than open the barn door too wide right away, allow only integer
4850 constant expressions here. */
4852 index = cp_parser_constant_expression (parser, false, NULL);
4854 index = cp_parser_expression (parser, /*cast_p=*/false);
4856 /* Look for the closing `]'. */
4857 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4859 /* Build the ARRAY_REF. */
4860 postfix_expression = grok_array_decl (postfix_expression, index);
4862 /* When not doing offsetof, array references are not permitted in
4863 constant-expressions. */
4865 && (cp_parser_non_integral_constant_expression
4866 (parser, "an array reference")))
4867 postfix_expression = error_mark_node;
4869 return postfix_expression;
4872 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4873 by cp_parser_builtin_offsetof. We're looking for
4875 postfix-expression . template [opt] id-expression
4876 postfix-expression . pseudo-destructor-name
4877 postfix-expression -> template [opt] id-expression
4878 postfix-expression -> pseudo-destructor-name
4880 FOR_OFFSETOF is set if we're being called in that context. That sorta
4881 limits what of the above we'll actually accept, but nevermind.
4882 TOKEN_TYPE is the "." or "->" token, which will already have been
4883 removed from the stream. */
4886 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4887 enum cpp_ttype token_type,
4888 tree postfix_expression,
4889 bool for_offsetof, cp_id_kind *idk,
4890 location_t location)
4894 bool pseudo_destructor_p;
4895 tree scope = NULL_TREE;
4897 /* If this is a `->' operator, dereference the pointer. */
4898 if (token_type == CPP_DEREF)
4899 postfix_expression = build_x_arrow (postfix_expression);
4900 /* Check to see whether or not the expression is type-dependent. */
4901 dependent_p = type_dependent_expression_p (postfix_expression);
4902 /* The identifier following the `->' or `.' is not qualified. */
4903 parser->scope = NULL_TREE;
4904 parser->qualifying_scope = NULL_TREE;
4905 parser->object_scope = NULL_TREE;
4906 *idk = CP_ID_KIND_NONE;
4907 /* Enter the scope corresponding to the type of the object
4908 given by the POSTFIX_EXPRESSION. */
4909 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4911 scope = TREE_TYPE (postfix_expression);
4912 /* According to the standard, no expression should ever have
4913 reference type. Unfortunately, we do not currently match
4914 the standard in this respect in that our internal representation
4915 of an expression may have reference type even when the standard
4916 says it does not. Therefore, we have to manually obtain the
4917 underlying type here. */
4918 scope = non_reference (scope);
4919 /* The type of the POSTFIX_EXPRESSION must be complete. */
4920 if (scope == unknown_type_node)
4922 error ("%H%qE does not have class type", &location, postfix_expression);
4926 scope = complete_type_or_else (scope, NULL_TREE);
4927 /* Let the name lookup machinery know that we are processing a
4928 class member access expression. */
4929 parser->context->object_type = scope;
4930 /* If something went wrong, we want to be able to discern that case,
4931 as opposed to the case where there was no SCOPE due to the type
4932 of expression being dependent. */
4934 scope = error_mark_node;
4935 /* If the SCOPE was erroneous, make the various semantic analysis
4936 functions exit quickly -- and without issuing additional error
4938 if (scope == error_mark_node)
4939 postfix_expression = error_mark_node;
4942 /* Assume this expression is not a pseudo-destructor access. */
4943 pseudo_destructor_p = false;
4945 /* If the SCOPE is a scalar type, then, if this is a valid program,
4946 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4947 is type dependent, it can be pseudo-destructor-name or something else.
4948 Try to parse it as pseudo-destructor-name first. */
4949 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4954 cp_parser_parse_tentatively (parser);
4955 /* Parse the pseudo-destructor-name. */
4957 cp_parser_pseudo_destructor_name (parser, &s, &type);
4959 && (cp_parser_error_occurred (parser)
4960 || TREE_CODE (type) != TYPE_DECL
4961 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4962 cp_parser_abort_tentative_parse (parser);
4963 else if (cp_parser_parse_definitely (parser))
4965 pseudo_destructor_p = true;
4967 = finish_pseudo_destructor_expr (postfix_expression,
4968 s, TREE_TYPE (type));
4972 if (!pseudo_destructor_p)
4974 /* If the SCOPE is not a scalar type, we are looking at an
4975 ordinary class member access expression, rather than a
4976 pseudo-destructor-name. */
4978 cp_token *token = cp_lexer_peek_token (parser->lexer);
4979 /* Parse the id-expression. */
4980 name = (cp_parser_id_expression
4982 cp_parser_optional_template_keyword (parser),
4983 /*check_dependency_p=*/true,
4985 /*declarator_p=*/false,
4986 /*optional_p=*/false));
4987 /* In general, build a SCOPE_REF if the member name is qualified.
4988 However, if the name was not dependent and has already been
4989 resolved; there is no need to build the SCOPE_REF. For example;
4991 struct X { void f(); };
4992 template <typename T> void f(T* t) { t->X::f(); }
4994 Even though "t" is dependent, "X::f" is not and has been resolved
4995 to a BASELINK; there is no need to include scope information. */
4997 /* But we do need to remember that there was an explicit scope for
4998 virtual function calls. */
5000 *idk = CP_ID_KIND_QUALIFIED;
5002 /* If the name is a template-id that names a type, we will get a
5003 TYPE_DECL here. That is invalid code. */
5004 if (TREE_CODE (name) == TYPE_DECL)
5006 error ("%Hinvalid use of %qD", &token->location, name);
5007 postfix_expression = error_mark_node;
5011 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5013 name = build_qualified_name (/*type=*/NULL_TREE,
5017 parser->scope = NULL_TREE;
5018 parser->qualifying_scope = NULL_TREE;
5019 parser->object_scope = NULL_TREE;
5021 if (scope && name && BASELINK_P (name))
5022 adjust_result_of_qualified_name_lookup
5023 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5025 = finish_class_member_access_expr (postfix_expression, name,
5027 tf_warning_or_error);
5031 /* We no longer need to look up names in the scope of the object on
5032 the left-hand side of the `.' or `->' operator. */
5033 parser->context->object_type = NULL_TREE;
5035 /* Outside of offsetof, these operators may not appear in
5036 constant-expressions. */
5038 && (cp_parser_non_integral_constant_expression
5039 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5040 postfix_expression = error_mark_node;
5042 return postfix_expression;
5045 /* Parse a parenthesized expression-list.
5048 assignment-expression
5049 expression-list, assignment-expression
5054 identifier, expression-list
5056 CAST_P is true if this expression is the target of a cast.
5058 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5061 Returns a TREE_LIST. The TREE_VALUE of each node is a
5062 representation of an assignment-expression. Note that a TREE_LIST
5063 is returned even if there is only a single expression in the list.
5064 error_mark_node is returned if the ( and or ) are
5065 missing. NULL_TREE is returned on no expressions. The parentheses
5066 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5067 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5068 indicates whether or not all of the expressions in the list were
5072 cp_parser_parenthesized_expression_list (cp_parser* parser,
5073 bool is_attribute_list,
5075 bool allow_expansion_p,
5076 bool *non_constant_p)
5078 tree expression_list = NULL_TREE;
5079 bool fold_expr_p = is_attribute_list;
5080 tree identifier = NULL_TREE;
5081 bool saved_greater_than_is_operator_p;
5083 /* Assume all the expressions will be constant. */
5085 *non_constant_p = false;
5087 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5088 return error_mark_node;
5090 /* Within a parenthesized expression, a `>' token is always
5091 the greater-than operator. */
5092 saved_greater_than_is_operator_p
5093 = parser->greater_than_is_operator_p;
5094 parser->greater_than_is_operator_p = true;
5096 /* Consume expressions until there are no more. */
5097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5102 /* At the beginning of attribute lists, check to see if the
5103 next token is an identifier. */
5104 if (is_attribute_list
5105 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5109 /* Consume the identifier. */
5110 token = cp_lexer_consume_token (parser->lexer);
5111 /* Save the identifier. */
5112 identifier = token->u.value;
5116 bool expr_non_constant_p;
5118 /* Parse the next assignment-expression. */
5119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5121 /* A braced-init-list. */
5122 maybe_warn_cpp0x ("extended initializer lists");
5123 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5124 if (non_constant_p && expr_non_constant_p)
5125 *non_constant_p = true;
5127 else if (non_constant_p)
5129 expr = (cp_parser_constant_expression
5130 (parser, /*allow_non_constant_p=*/true,
5131 &expr_non_constant_p));
5132 if (expr_non_constant_p)
5133 *non_constant_p = true;
5136 expr = cp_parser_assignment_expression (parser, cast_p);
5139 expr = fold_non_dependent_expr (expr);
5141 /* If we have an ellipsis, then this is an expression
5143 if (allow_expansion_p
5144 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5146 /* Consume the `...'. */
5147 cp_lexer_consume_token (parser->lexer);
5149 /* Build the argument pack. */
5150 expr = make_pack_expansion (expr);
5153 /* Add it to the list. We add error_mark_node
5154 expressions to the list, so that we can still tell if
5155 the correct form for a parenthesized expression-list
5156 is found. That gives better errors. */
5157 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5159 if (expr == error_mark_node)
5163 /* After the first item, attribute lists look the same as
5164 expression lists. */
5165 is_attribute_list = false;
5168 /* If the next token isn't a `,', then we are done. */
5169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5172 /* Otherwise, consume the `,' and keep going. */
5173 cp_lexer_consume_token (parser->lexer);
5176 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5181 /* We try and resync to an unnested comma, as that will give the
5182 user better diagnostics. */
5183 ending = cp_parser_skip_to_closing_parenthesis (parser,
5184 /*recovering=*/true,
5186 /*consume_paren=*/true);
5191 parser->greater_than_is_operator_p
5192 = saved_greater_than_is_operator_p;
5193 return error_mark_node;
5197 parser->greater_than_is_operator_p
5198 = saved_greater_than_is_operator_p;
5200 /* We built up the list in reverse order so we must reverse it now. */
5201 expression_list = nreverse (expression_list);
5203 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5205 return expression_list;
5208 /* Parse a pseudo-destructor-name.
5210 pseudo-destructor-name:
5211 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5212 :: [opt] nested-name-specifier template template-id :: ~ type-name
5213 :: [opt] nested-name-specifier [opt] ~ type-name
5215 If either of the first two productions is used, sets *SCOPE to the
5216 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5217 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5218 or ERROR_MARK_NODE if the parse fails. */
5221 cp_parser_pseudo_destructor_name (cp_parser* parser,
5225 bool nested_name_specifier_p;
5227 /* Assume that things will not work out. */
5228 *type = error_mark_node;
5230 /* Look for the optional `::' operator. */
5231 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5232 /* Look for the optional nested-name-specifier. */
5233 nested_name_specifier_p
5234 = (cp_parser_nested_name_specifier_opt (parser,
5235 /*typename_keyword_p=*/false,
5236 /*check_dependency_p=*/true,
5238 /*is_declaration=*/true)
5240 /* Now, if we saw a nested-name-specifier, we might be doing the
5241 second production. */
5242 if (nested_name_specifier_p
5243 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5245 /* Consume the `template' keyword. */
5246 cp_lexer_consume_token (parser->lexer);
5247 /* Parse the template-id. */
5248 cp_parser_template_id (parser,
5249 /*template_keyword_p=*/true,
5250 /*check_dependency_p=*/false,
5251 /*is_declaration=*/true);
5252 /* Look for the `::' token. */
5253 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5255 /* If the next token is not a `~', then there might be some
5256 additional qualification. */
5257 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5259 /* At this point, we're looking for "type-name :: ~". The type-name
5260 must not be a class-name, since this is a pseudo-destructor. So,
5261 it must be either an enum-name, or a typedef-name -- both of which
5262 are just identifiers. So, we peek ahead to check that the "::"
5263 and "~" tokens are present; if they are not, then we can avoid
5264 calling type_name. */
5265 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5266 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5267 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5269 cp_parser_error (parser, "non-scalar type");
5273 /* Look for the type-name. */
5274 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5275 if (*scope == error_mark_node)
5278 /* Look for the `::' token. */
5279 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5284 /* Look for the `~'. */
5285 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5286 /* Look for the type-name again. We are not responsible for
5287 checking that it matches the first type-name. */
5288 *type = cp_parser_nonclass_name (parser);
5291 /* Parse a unary-expression.
5297 unary-operator cast-expression
5298 sizeof unary-expression
5306 __extension__ cast-expression
5307 __alignof__ unary-expression
5308 __alignof__ ( type-id )
5309 __real__ cast-expression
5310 __imag__ cast-expression
5313 ADDRESS_P is true iff the unary-expression is appearing as the
5314 operand of the `&' operator. CAST_P is true if this expression is
5315 the target of a cast.
5317 Returns a representation of the expression. */
5320 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5323 enum tree_code unary_operator;
5325 /* Peek at the next token. */
5326 token = cp_lexer_peek_token (parser->lexer);
5327 /* Some keywords give away the kind of expression. */
5328 if (token->type == CPP_KEYWORD)
5330 enum rid keyword = token->keyword;
5340 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5341 /* Consume the token. */
5342 cp_lexer_consume_token (parser->lexer);
5343 /* Parse the operand. */
5344 operand = cp_parser_sizeof_operand (parser, keyword);
5346 if (TYPE_P (operand))
5347 return cxx_sizeof_or_alignof_type (operand, op, true);
5349 return cxx_sizeof_or_alignof_expr (operand, op, true);
5353 return cp_parser_new_expression (parser);
5356 return cp_parser_delete_expression (parser);
5360 /* The saved value of the PEDANTIC flag. */
5364 /* Save away the PEDANTIC flag. */
5365 cp_parser_extension_opt (parser, &saved_pedantic);
5366 /* Parse the cast-expression. */
5367 expr = cp_parser_simple_cast_expression (parser);
5368 /* Restore the PEDANTIC flag. */
5369 pedantic = saved_pedantic;
5379 /* Consume the `__real__' or `__imag__' token. */
5380 cp_lexer_consume_token (parser->lexer);
5381 /* Parse the cast-expression. */
5382 expression = cp_parser_simple_cast_expression (parser);
5383 /* Create the complete representation. */
5384 return build_x_unary_op ((keyword == RID_REALPART
5385 ? REALPART_EXPR : IMAGPART_EXPR),
5387 tf_warning_or_error);
5396 /* Look for the `:: new' and `:: delete', which also signal the
5397 beginning of a new-expression, or delete-expression,
5398 respectively. If the next token is `::', then it might be one of
5400 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5404 /* See if the token after the `::' is one of the keywords in
5405 which we're interested. */
5406 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5407 /* If it's `new', we have a new-expression. */
5408 if (keyword == RID_NEW)
5409 return cp_parser_new_expression (parser);
5410 /* Similarly, for `delete'. */
5411 else if (keyword == RID_DELETE)
5412 return cp_parser_delete_expression (parser);
5415 /* Look for a unary operator. */
5416 unary_operator = cp_parser_unary_operator (token);
5417 /* The `++' and `--' operators can be handled similarly, even though
5418 they are not technically unary-operators in the grammar. */
5419 if (unary_operator == ERROR_MARK)
5421 if (token->type == CPP_PLUS_PLUS)
5422 unary_operator = PREINCREMENT_EXPR;
5423 else if (token->type == CPP_MINUS_MINUS)
5424 unary_operator = PREDECREMENT_EXPR;
5425 /* Handle the GNU address-of-label extension. */
5426 else if (cp_parser_allow_gnu_extensions_p (parser)
5427 && token->type == CPP_AND_AND)
5432 /* Consume the '&&' token. */
5433 cp_lexer_consume_token (parser->lexer);
5434 /* Look for the identifier. */
5435 identifier = cp_parser_identifier (parser);
5436 /* Create an expression representing the address. */
5437 expression = finish_label_address_expr (identifier);
5438 if (cp_parser_non_integral_constant_expression (parser,
5439 "the address of a label"))
5440 expression = error_mark_node;
5444 if (unary_operator != ERROR_MARK)
5446 tree cast_expression;
5447 tree expression = error_mark_node;
5448 const char *non_constant_p = NULL;
5450 /* Consume the operator token. */
5451 token = cp_lexer_consume_token (parser->lexer);
5452 /* Parse the cast-expression. */
5454 = cp_parser_cast_expression (parser,
5455 unary_operator == ADDR_EXPR,
5457 /* Now, build an appropriate representation. */
5458 switch (unary_operator)
5461 non_constant_p = "%<*%>";
5462 expression = build_x_indirect_ref (cast_expression, "unary *",
5463 tf_warning_or_error);
5467 non_constant_p = "%<&%>";
5470 expression = build_x_unary_op (unary_operator, cast_expression,
5471 tf_warning_or_error);
5474 case PREINCREMENT_EXPR:
5475 case PREDECREMENT_EXPR:
5476 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5477 ? "%<++%>" : "%<--%>");
5479 case UNARY_PLUS_EXPR:
5481 case TRUTH_NOT_EXPR:
5482 expression = finish_unary_op_expr (unary_operator, cast_expression);
5490 && cp_parser_non_integral_constant_expression (parser,
5492 expression = error_mark_node;
5497 return cp_parser_postfix_expression (parser, address_p, cast_p,
5498 /*member_access_only_p=*/false);
5501 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5502 unary-operator, the corresponding tree code is returned. */
5504 static enum tree_code
5505 cp_parser_unary_operator (cp_token* token)
5507 switch (token->type)
5510 return INDIRECT_REF;
5516 return UNARY_PLUS_EXPR;
5522 return TRUTH_NOT_EXPR;
5525 return BIT_NOT_EXPR;
5532 /* Parse a new-expression.
5535 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5536 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5538 Returns a representation of the expression. */
5541 cp_parser_new_expression (cp_parser* parser)
5543 bool global_scope_p;
5549 /* Look for the optional `::' operator. */
5551 = (cp_parser_global_scope_opt (parser,
5552 /*current_scope_valid_p=*/false)
5554 /* Look for the `new' operator. */
5555 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5556 /* There's no easy way to tell a new-placement from the
5557 `( type-id )' construct. */
5558 cp_parser_parse_tentatively (parser);
5559 /* Look for a new-placement. */
5560 placement = cp_parser_new_placement (parser);
5561 /* If that didn't work out, there's no new-placement. */
5562 if (!cp_parser_parse_definitely (parser))
5563 placement = NULL_TREE;
5565 /* If the next token is a `(', then we have a parenthesized
5567 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5570 /* Consume the `('. */
5571 cp_lexer_consume_token (parser->lexer);
5572 /* Parse the type-id. */
5573 type = cp_parser_type_id (parser);
5574 /* Look for the closing `)'. */
5575 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5576 token = cp_lexer_peek_token (parser->lexer);
5577 /* There should not be a direct-new-declarator in this production,
5578 but GCC used to allowed this, so we check and emit a sensible error
5579 message for this case. */
5580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5582 error ("%Harray bound forbidden after parenthesized type-id",
5584 inform ("%Htry removing the parentheses around the type-id",
5586 cp_parser_direct_new_declarator (parser);
5590 /* Otherwise, there must be a new-type-id. */
5592 type = cp_parser_new_type_id (parser, &nelts);
5594 /* If the next token is a `(' or '{', then we have a new-initializer. */
5595 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5596 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5597 initializer = cp_parser_new_initializer (parser);
5599 initializer = NULL_TREE;
5601 /* A new-expression may not appear in an integral constant
5603 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5604 return error_mark_node;
5606 /* Create a representation of the new-expression. */
5607 return build_new (placement, type, nelts, initializer, global_scope_p,
5608 tf_warning_or_error);
5611 /* Parse a new-placement.
5616 Returns the same representation as for an expression-list. */
5619 cp_parser_new_placement (cp_parser* parser)
5621 tree expression_list;
5623 /* Parse the expression-list. */
5624 expression_list = (cp_parser_parenthesized_expression_list
5625 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5626 /*non_constant_p=*/NULL));
5628 return expression_list;
5631 /* Parse a new-type-id.
5634 type-specifier-seq new-declarator [opt]
5636 Returns the TYPE allocated. If the new-type-id indicates an array
5637 type, *NELTS is set to the number of elements in the last array
5638 bound; the TYPE will not include the last array bound. */
5641 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5643 cp_decl_specifier_seq type_specifier_seq;
5644 cp_declarator *new_declarator;
5645 cp_declarator *declarator;
5646 cp_declarator *outer_declarator;
5647 const char *saved_message;
5650 /* The type-specifier sequence must not contain type definitions.
5651 (It cannot contain declarations of new types either, but if they
5652 are not definitions we will catch that because they are not
5654 saved_message = parser->type_definition_forbidden_message;
5655 parser->type_definition_forbidden_message
5656 = "types may not be defined in a new-type-id";
5657 /* Parse the type-specifier-seq. */
5658 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5659 &type_specifier_seq);
5660 /* Restore the old message. */
5661 parser->type_definition_forbidden_message = saved_message;
5662 /* Parse the new-declarator. */
5663 new_declarator = cp_parser_new_declarator_opt (parser);
5665 /* Determine the number of elements in the last array dimension, if
5668 /* Skip down to the last array dimension. */
5669 declarator = new_declarator;
5670 outer_declarator = NULL;
5671 while (declarator && (declarator->kind == cdk_pointer
5672 || declarator->kind == cdk_ptrmem))
5674 outer_declarator = declarator;
5675 declarator = declarator->declarator;
5678 && declarator->kind == cdk_array
5679 && declarator->declarator
5680 && declarator->declarator->kind == cdk_array)
5682 outer_declarator = declarator;
5683 declarator = declarator->declarator;
5686 if (declarator && declarator->kind == cdk_array)
5688 *nelts = declarator->u.array.bounds;
5689 if (*nelts == error_mark_node)
5690 *nelts = integer_one_node;
5692 if (outer_declarator)
5693 outer_declarator->declarator = declarator->declarator;
5695 new_declarator = NULL;
5698 type = groktypename (&type_specifier_seq, new_declarator);
5702 /* Parse an (optional) new-declarator.
5705 ptr-operator new-declarator [opt]
5706 direct-new-declarator
5708 Returns the declarator. */
5710 static cp_declarator *
5711 cp_parser_new_declarator_opt (cp_parser* parser)
5713 enum tree_code code;
5715 cp_cv_quals cv_quals;
5717 /* We don't know if there's a ptr-operator next, or not. */
5718 cp_parser_parse_tentatively (parser);
5719 /* Look for a ptr-operator. */
5720 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5721 /* If that worked, look for more new-declarators. */
5722 if (cp_parser_parse_definitely (parser))
5724 cp_declarator *declarator;
5726 /* Parse another optional declarator. */
5727 declarator = cp_parser_new_declarator_opt (parser);
5729 return cp_parser_make_indirect_declarator
5730 (code, type, cv_quals, declarator);
5733 /* If the next token is a `[', there is a direct-new-declarator. */
5734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5735 return cp_parser_direct_new_declarator (parser);
5740 /* Parse a direct-new-declarator.
5742 direct-new-declarator:
5744 direct-new-declarator [constant-expression]
5748 static cp_declarator *
5749 cp_parser_direct_new_declarator (cp_parser* parser)
5751 cp_declarator *declarator = NULL;
5757 /* Look for the opening `['. */
5758 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5759 /* The first expression is not required to be constant. */
5762 cp_token *token = cp_lexer_peek_token (parser->lexer);
5763 expression = cp_parser_expression (parser, /*cast_p=*/false);
5764 /* The standard requires that the expression have integral
5765 type. DR 74 adds enumeration types. We believe that the
5766 real intent is that these expressions be handled like the
5767 expression in a `switch' condition, which also allows
5768 classes with a single conversion to integral or
5769 enumeration type. */
5770 if (!processing_template_decl)
5773 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5778 error ("%Hexpression in new-declarator must have integral "
5779 "or enumeration type", &token->location);
5780 expression = error_mark_node;
5784 /* But all the other expressions must be. */
5787 = cp_parser_constant_expression (parser,
5788 /*allow_non_constant=*/false,
5790 /* Look for the closing `]'. */
5791 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5793 /* Add this bound to the declarator. */
5794 declarator = make_array_declarator (declarator, expression);
5796 /* If the next token is not a `[', then there are no more
5798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5805 /* Parse a new-initializer.
5808 ( expression-list [opt] )
5811 Returns a representation of the expression-list. If there is no
5812 expression-list, VOID_ZERO_NODE is returned. */
5815 cp_parser_new_initializer (cp_parser* parser)
5817 tree expression_list;
5819 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5821 bool expr_non_constant_p;
5822 maybe_warn_cpp0x ("extended initializer lists");
5823 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5824 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5825 expression_list = build_tree_list (NULL_TREE, expression_list);
5828 expression_list = (cp_parser_parenthesized_expression_list
5829 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5830 /*non_constant_p=*/NULL));
5831 if (!expression_list)
5832 expression_list = void_zero_node;
5834 return expression_list;
5837 /* Parse a delete-expression.
5840 :: [opt] delete cast-expression
5841 :: [opt] delete [ ] cast-expression
5843 Returns a representation of the expression. */
5846 cp_parser_delete_expression (cp_parser* parser)
5848 bool global_scope_p;
5852 /* Look for the optional `::' operator. */
5854 = (cp_parser_global_scope_opt (parser,
5855 /*current_scope_valid_p=*/false)
5857 /* Look for the `delete' keyword. */
5858 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5859 /* See if the array syntax is in use. */
5860 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5862 /* Consume the `[' token. */
5863 cp_lexer_consume_token (parser->lexer);
5864 /* Look for the `]' token. */
5865 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5866 /* Remember that this is the `[]' construct. */
5872 /* Parse the cast-expression. */
5873 expression = cp_parser_simple_cast_expression (parser);
5875 /* A delete-expression may not appear in an integral constant
5877 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5878 return error_mark_node;
5880 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5883 /* Parse a cast-expression.
5887 ( type-id ) cast-expression
5889 ADDRESS_P is true iff the unary-expression is appearing as the
5890 operand of the `&' operator. CAST_P is true if this expression is
5891 the target of a cast.
5893 Returns a representation of the expression. */
5896 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5898 /* If it's a `(', then we might be looking at a cast. */
5899 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5901 tree type = NULL_TREE;
5902 tree expr = NULL_TREE;
5903 bool compound_literal_p;
5904 const char *saved_message;
5906 /* There's no way to know yet whether or not this is a cast.
5907 For example, `(int (3))' is a unary-expression, while `(int)
5908 3' is a cast. So, we resort to parsing tentatively. */
5909 cp_parser_parse_tentatively (parser);
5910 /* Types may not be defined in a cast. */
5911 saved_message = parser->type_definition_forbidden_message;
5912 parser->type_definition_forbidden_message
5913 = "types may not be defined in casts";
5914 /* Consume the `('. */
5915 cp_lexer_consume_token (parser->lexer);
5916 /* A very tricky bit is that `(struct S) { 3 }' is a
5917 compound-literal (which we permit in C++ as an extension).
5918 But, that construct is not a cast-expression -- it is a
5919 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5920 is legal; if the compound-literal were a cast-expression,
5921 you'd need an extra set of parentheses.) But, if we parse
5922 the type-id, and it happens to be a class-specifier, then we
5923 will commit to the parse at that point, because we cannot
5924 undo the action that is done when creating a new class. So,
5925 then we cannot back up and do a postfix-expression.
5927 Therefore, we scan ahead to the closing `)', and check to see
5928 if the token after the `)' is a `{'. If so, we are not
5929 looking at a cast-expression.
5931 Save tokens so that we can put them back. */
5932 cp_lexer_save_tokens (parser->lexer);
5933 /* Skip tokens until the next token is a closing parenthesis.
5934 If we find the closing `)', and the next token is a `{', then
5935 we are looking at a compound-literal. */
5937 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5938 /*consume_paren=*/true)
5939 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5940 /* Roll back the tokens we skipped. */
5941 cp_lexer_rollback_tokens (parser->lexer);
5942 /* If we were looking at a compound-literal, simulate an error
5943 so that the call to cp_parser_parse_definitely below will
5945 if (compound_literal_p)
5946 cp_parser_simulate_error (parser);
5949 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5950 parser->in_type_id_in_expr_p = true;
5951 /* Look for the type-id. */
5952 type = cp_parser_type_id (parser);
5953 /* Look for the closing `)'. */
5954 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5955 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5958 /* Restore the saved message. */
5959 parser->type_definition_forbidden_message = saved_message;
5961 /* If ok so far, parse the dependent expression. We cannot be
5962 sure it is a cast. Consider `(T ())'. It is a parenthesized
5963 ctor of T, but looks like a cast to function returning T
5964 without a dependent expression. */
5965 if (!cp_parser_error_occurred (parser))
5966 expr = cp_parser_cast_expression (parser,
5967 /*address_p=*/false,
5970 if (cp_parser_parse_definitely (parser))
5972 /* Warn about old-style casts, if so requested. */
5973 if (warn_old_style_cast
5974 && !in_system_header
5975 && !VOID_TYPE_P (type)
5976 && current_lang_name != lang_name_c)
5977 warning (OPT_Wold_style_cast, "use of old-style cast");
5979 /* Only type conversions to integral or enumeration types
5980 can be used in constant-expressions. */
5981 if (!cast_valid_in_integral_constant_expression_p (type)
5982 && (cp_parser_non_integral_constant_expression
5984 "a cast to a type other than an integral or "
5985 "enumeration type")))
5986 return error_mark_node;
5988 /* Perform the cast. */
5989 expr = build_c_cast (type, expr);
5994 /* If we get here, then it's not a cast, so it must be a
5995 unary-expression. */
5996 return cp_parser_unary_expression (parser, address_p, cast_p);
5999 /* Parse a binary expression of the general form:
6003 pm-expression .* cast-expression
6004 pm-expression ->* cast-expression
6006 multiplicative-expression:
6008 multiplicative-expression * pm-expression
6009 multiplicative-expression / pm-expression
6010 multiplicative-expression % pm-expression
6012 additive-expression:
6013 multiplicative-expression
6014 additive-expression + multiplicative-expression
6015 additive-expression - multiplicative-expression
6019 shift-expression << additive-expression
6020 shift-expression >> additive-expression
6022 relational-expression:
6024 relational-expression < shift-expression
6025 relational-expression > shift-expression
6026 relational-expression <= shift-expression
6027 relational-expression >= shift-expression
6031 relational-expression:
6032 relational-expression <? shift-expression
6033 relational-expression >? shift-expression
6035 equality-expression:
6036 relational-expression
6037 equality-expression == relational-expression
6038 equality-expression != relational-expression
6042 and-expression & equality-expression
6044 exclusive-or-expression:
6046 exclusive-or-expression ^ and-expression
6048 inclusive-or-expression:
6049 exclusive-or-expression
6050 inclusive-or-expression | exclusive-or-expression
6052 logical-and-expression:
6053 inclusive-or-expression
6054 logical-and-expression && inclusive-or-expression
6056 logical-or-expression:
6057 logical-and-expression
6058 logical-or-expression || logical-and-expression
6060 All these are implemented with a single function like:
6063 simple-cast-expression
6064 binary-expression <token> binary-expression
6066 CAST_P is true if this expression is the target of a cast.
6068 The binops_by_token map is used to get the tree codes for each <token> type.
6069 binary-expressions are associated according to a precedence table. */
6071 #define TOKEN_PRECEDENCE(token) \
6072 (((token->type == CPP_GREATER \
6073 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6074 && !parser->greater_than_is_operator_p) \
6075 ? PREC_NOT_OPERATOR \
6076 : binops_by_token[token->type].prec)
6079 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6080 enum cp_parser_prec prec)
6082 cp_parser_expression_stack stack;
6083 cp_parser_expression_stack_entry *sp = &stack[0];
6086 enum tree_code tree_type, lhs_type, rhs_type;
6087 enum cp_parser_prec new_prec, lookahead_prec;
6090 /* Parse the first expression. */
6091 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6092 lhs_type = ERROR_MARK;
6096 /* Get an operator token. */
6097 token = cp_lexer_peek_token (parser->lexer);
6099 if (warn_cxx0x_compat
6100 && token->type == CPP_RSHIFT
6101 && !parser->greater_than_is_operator_p)
6103 warning (OPT_Wc__0x_compat,
6104 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6106 warning (OPT_Wc__0x_compat,
6107 "suggest parentheses around %<>>%> expression");
6110 new_prec = TOKEN_PRECEDENCE (token);
6112 /* Popping an entry off the stack means we completed a subexpression:
6113 - either we found a token which is not an operator (`>' where it is not
6114 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6115 will happen repeatedly;
6116 - or, we found an operator which has lower priority. This is the case
6117 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6119 if (new_prec <= prec)
6128 tree_type = binops_by_token[token->type].tree_type;
6130 /* We used the operator token. */
6131 cp_lexer_consume_token (parser->lexer);
6133 /* Extract another operand. It may be the RHS of this expression
6134 or the LHS of a new, higher priority expression. */
6135 rhs = cp_parser_simple_cast_expression (parser);
6136 rhs_type = ERROR_MARK;
6138 /* Get another operator token. Look up its precedence to avoid
6139 building a useless (immediately popped) stack entry for common
6140 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6141 token = cp_lexer_peek_token (parser->lexer);
6142 lookahead_prec = TOKEN_PRECEDENCE (token);
6143 if (lookahead_prec > new_prec)
6145 /* ... and prepare to parse the RHS of the new, higher priority
6146 expression. Since precedence levels on the stack are
6147 monotonically increasing, we do not have to care about
6150 sp->tree_type = tree_type;
6152 sp->lhs_type = lhs_type;
6155 lhs_type = rhs_type;
6157 new_prec = lookahead_prec;
6161 /* If the stack is not empty, we have parsed into LHS the right side
6162 (`4' in the example above) of an expression we had suspended.
6163 We can use the information on the stack to recover the LHS (`3')
6164 from the stack together with the tree code (`MULT_EXPR'), and
6165 the precedence of the higher level subexpression
6166 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6167 which will be used to actually build the additive expression. */
6170 tree_type = sp->tree_type;
6172 rhs_type = lhs_type;
6174 lhs_type = sp->lhs_type;
6177 overloaded_p = false;
6178 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6179 &overloaded_p, tf_warning_or_error);
6180 lhs_type = tree_type;
6182 /* If the binary operator required the use of an overloaded operator,
6183 then this expression cannot be an integral constant-expression.
6184 An overloaded operator can be used even if both operands are
6185 otherwise permissible in an integral constant-expression if at
6186 least one of the operands is of enumeration type. */
6189 && (cp_parser_non_integral_constant_expression
6190 (parser, "calls to overloaded operators")))
6191 return error_mark_node;
6198 /* Parse the `? expression : assignment-expression' part of a
6199 conditional-expression. The LOGICAL_OR_EXPR is the
6200 logical-or-expression that started the conditional-expression.
6201 Returns a representation of the entire conditional-expression.
6203 This routine is used by cp_parser_assignment_expression.
6205 ? expression : assignment-expression
6209 ? : assignment-expression */
6212 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6215 tree assignment_expr;
6217 /* Consume the `?' token. */
6218 cp_lexer_consume_token (parser->lexer);
6219 if (cp_parser_allow_gnu_extensions_p (parser)
6220 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6221 /* Implicit true clause. */
6224 /* Parse the expression. */
6225 expr = cp_parser_expression (parser, /*cast_p=*/false);
6227 /* The next token should be a `:'. */
6228 cp_parser_require (parser, CPP_COLON, "%<:%>");
6229 /* Parse the assignment-expression. */
6230 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6232 /* Build the conditional-expression. */
6233 return build_x_conditional_expr (logical_or_expr,
6236 tf_warning_or_error);
6239 /* Parse an assignment-expression.
6241 assignment-expression:
6242 conditional-expression
6243 logical-or-expression assignment-operator assignment_expression
6246 CAST_P is true if this expression is the target of a cast.
6248 Returns a representation for the expression. */
6251 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6255 /* If the next token is the `throw' keyword, then we're looking at
6256 a throw-expression. */
6257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6258 expr = cp_parser_throw_expression (parser);
6259 /* Otherwise, it must be that we are looking at a
6260 logical-or-expression. */
6263 /* Parse the binary expressions (logical-or-expression). */
6264 expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6265 /* If the next token is a `?' then we're actually looking at a
6266 conditional-expression. */
6267 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6268 return cp_parser_question_colon_clause (parser, expr);
6271 enum tree_code assignment_operator;
6273 /* If it's an assignment-operator, we're using the second
6276 = cp_parser_assignment_operator_opt (parser);
6277 if (assignment_operator != ERROR_MARK)
6279 bool non_constant_p;
6281 /* Parse the right-hand side of the assignment. */
6282 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6284 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6285 maybe_warn_cpp0x ("extended initializer lists");
6287 /* An assignment may not appear in a
6288 constant-expression. */
6289 if (cp_parser_non_integral_constant_expression (parser,
6291 return error_mark_node;
6292 /* Build the assignment expression. */
6293 expr = build_x_modify_expr (expr,
6294 assignment_operator,
6296 tf_warning_or_error);
6304 /* Parse an (optional) assignment-operator.
6306 assignment-operator: one of
6307 = *= /= %= += -= >>= <<= &= ^= |=
6311 assignment-operator: one of
6314 If the next token is an assignment operator, the corresponding tree
6315 code is returned, and the token is consumed. For example, for
6316 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6317 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6318 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6319 operator, ERROR_MARK is returned. */
6321 static enum tree_code
6322 cp_parser_assignment_operator_opt (cp_parser* parser)
6327 /* Peek at the next toen. */
6328 token = cp_lexer_peek_token (parser->lexer);
6330 switch (token->type)
6341 op = TRUNC_DIV_EXPR;
6345 op = TRUNC_MOD_EXPR;
6377 /* Nothing else is an assignment operator. */
6381 /* If it was an assignment operator, consume it. */
6382 if (op != ERROR_MARK)
6383 cp_lexer_consume_token (parser->lexer);
6388 /* Parse an expression.
6391 assignment-expression
6392 expression , assignment-expression
6394 CAST_P is true if this expression is the target of a cast.
6396 Returns a representation of the expression. */
6399 cp_parser_expression (cp_parser* parser, bool cast_p)
6401 tree expression = NULL_TREE;
6405 tree assignment_expression;
6407 /* Parse the next assignment-expression. */
6408 assignment_expression
6409 = cp_parser_assignment_expression (parser, cast_p);
6410 /* If this is the first assignment-expression, we can just
6413 expression = assignment_expression;
6415 expression = build_x_compound_expr (expression,
6416 assignment_expression,
6417 tf_warning_or_error);
6418 /* If the next token is not a comma, then we are done with the
6420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6422 /* Consume the `,'. */
6423 cp_lexer_consume_token (parser->lexer);
6424 /* A comma operator cannot appear in a constant-expression. */
6425 if (cp_parser_non_integral_constant_expression (parser,
6426 "a comma operator"))
6427 expression = error_mark_node;
6433 /* Parse a constant-expression.
6435 constant-expression:
6436 conditional-expression
6438 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6439 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6440 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6441 is false, NON_CONSTANT_P should be NULL. */
6444 cp_parser_constant_expression (cp_parser* parser,
6445 bool allow_non_constant_p,
6446 bool *non_constant_p)
6448 bool saved_integral_constant_expression_p;
6449 bool saved_allow_non_integral_constant_expression_p;
6450 bool saved_non_integral_constant_expression_p;
6453 /* It might seem that we could simply parse the
6454 conditional-expression, and then check to see if it were
6455 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6456 one that the compiler can figure out is constant, possibly after
6457 doing some simplifications or optimizations. The standard has a
6458 precise definition of constant-expression, and we must honor
6459 that, even though it is somewhat more restrictive.
6465 is not a legal declaration, because `(2, 3)' is not a
6466 constant-expression. The `,' operator is forbidden in a
6467 constant-expression. However, GCC's constant-folding machinery
6468 will fold this operation to an INTEGER_CST for `3'. */
6470 /* Save the old settings. */
6471 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6472 saved_allow_non_integral_constant_expression_p
6473 = parser->allow_non_integral_constant_expression_p;
6474 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6475 /* We are now parsing a constant-expression. */
6476 parser->integral_constant_expression_p = true;
6477 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6478 parser->non_integral_constant_expression_p = false;
6479 /* Although the grammar says "conditional-expression", we parse an
6480 "assignment-expression", which also permits "throw-expression"
6481 and the use of assignment operators. In the case that
6482 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6483 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6484 actually essential that we look for an assignment-expression.
6485 For example, cp_parser_initializer_clauses uses this function to
6486 determine whether a particular assignment-expression is in fact
6488 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6489 /* Restore the old settings. */
6490 parser->integral_constant_expression_p
6491 = saved_integral_constant_expression_p;
6492 parser->allow_non_integral_constant_expression_p
6493 = saved_allow_non_integral_constant_expression_p;
6494 if (allow_non_constant_p)
6495 *non_constant_p = parser->non_integral_constant_expression_p;
6496 else if (parser->non_integral_constant_expression_p)
6497 expression = error_mark_node;
6498 parser->non_integral_constant_expression_p
6499 = saved_non_integral_constant_expression_p;
6504 /* Parse __builtin_offsetof.
6506 offsetof-expression:
6507 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6509 offsetof-member-designator:
6511 | offsetof-member-designator "." id-expression
6512 | offsetof-member-designator "[" expression "]" */
6515 cp_parser_builtin_offsetof (cp_parser *parser)
6517 int save_ice_p, save_non_ice_p;
6522 /* We're about to accept non-integral-constant things, but will
6523 definitely yield an integral constant expression. Save and
6524 restore these values around our local parsing. */
6525 save_ice_p = parser->integral_constant_expression_p;
6526 save_non_ice_p = parser->non_integral_constant_expression_p;
6528 /* Consume the "__builtin_offsetof" token. */
6529 cp_lexer_consume_token (parser->lexer);
6530 /* Consume the opening `('. */
6531 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6532 /* Parse the type-id. */
6533 type = cp_parser_type_id (parser);
6534 /* Look for the `,'. */
6535 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6536 token = cp_lexer_peek_token (parser->lexer);
6538 /* Build the (type *)null that begins the traditional offsetof macro. */
6539 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6540 tf_warning_or_error);
6542 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6543 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6544 true, &dummy, token->location);
6547 token = cp_lexer_peek_token (parser->lexer);
6548 switch (token->type)
6550 case CPP_OPEN_SQUARE:
6551 /* offsetof-member-designator "[" expression "]" */
6552 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6556 /* offsetof-member-designator "." identifier */
6557 cp_lexer_consume_token (parser->lexer);
6558 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6563 case CPP_CLOSE_PAREN:
6564 /* Consume the ")" token. */
6565 cp_lexer_consume_token (parser->lexer);
6569 /* Error. We know the following require will fail, but
6570 that gives the proper error message. */
6571 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6572 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6573 expr = error_mark_node;
6579 /* If we're processing a template, we can't finish the semantics yet.
6580 Otherwise we can fold the entire expression now. */
6581 if (processing_template_decl)
6582 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6584 expr = finish_offsetof (expr);
6587 parser->integral_constant_expression_p = save_ice_p;
6588 parser->non_integral_constant_expression_p = save_non_ice_p;
6593 /* Parse a trait expression. */
6596 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6599 tree type1, type2 = NULL_TREE;
6600 bool binary = false;
6601 cp_decl_specifier_seq decl_specs;
6605 case RID_HAS_NOTHROW_ASSIGN:
6606 kind = CPTK_HAS_NOTHROW_ASSIGN;
6608 case RID_HAS_NOTHROW_CONSTRUCTOR:
6609 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6611 case RID_HAS_NOTHROW_COPY:
6612 kind = CPTK_HAS_NOTHROW_COPY;
6614 case RID_HAS_TRIVIAL_ASSIGN:
6615 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6617 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6618 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6620 case RID_HAS_TRIVIAL_COPY:
6621 kind = CPTK_HAS_TRIVIAL_COPY;
6623 case RID_HAS_TRIVIAL_DESTRUCTOR:
6624 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6626 case RID_HAS_VIRTUAL_DESTRUCTOR:
6627 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6629 case RID_IS_ABSTRACT:
6630 kind = CPTK_IS_ABSTRACT;
6632 case RID_IS_BASE_OF:
6633 kind = CPTK_IS_BASE_OF;
6637 kind = CPTK_IS_CLASS;
6639 case RID_IS_CONVERTIBLE_TO:
6640 kind = CPTK_IS_CONVERTIBLE_TO;
6644 kind = CPTK_IS_EMPTY;
6647 kind = CPTK_IS_ENUM;
6652 case RID_IS_POLYMORPHIC:
6653 kind = CPTK_IS_POLYMORPHIC;
6656 kind = CPTK_IS_UNION;
6662 /* Consume the token. */
6663 cp_lexer_consume_token (parser->lexer);
6665 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6667 type1 = cp_parser_type_id (parser);
6669 if (type1 == error_mark_node)
6670 return error_mark_node;
6672 /* Build a trivial decl-specifier-seq. */
6673 clear_decl_specs (&decl_specs);
6674 decl_specs.type = type1;
6676 /* Call grokdeclarator to figure out what type this is. */
6677 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6678 /*initialized=*/0, /*attrlist=*/NULL);
6682 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6684 type2 = cp_parser_type_id (parser);
6686 if (type2 == error_mark_node)
6687 return error_mark_node;
6689 /* Build a trivial decl-specifier-seq. */
6690 clear_decl_specs (&decl_specs);
6691 decl_specs.type = type2;
6693 /* Call grokdeclarator to figure out what type this is. */
6694 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6695 /*initialized=*/0, /*attrlist=*/NULL);
6698 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6700 /* Complete the trait expression, which may mean either processing
6701 the trait expr now or saving it for template instantiation. */
6702 return finish_trait_expr (kind, type1, type2);
6705 /* Statements [gram.stmt.stmt] */
6707 /* Parse a statement.
6711 expression-statement
6716 declaration-statement
6719 IN_COMPOUND is true when the statement is nested inside a
6720 cp_parser_compound_statement; this matters for certain pragmas.
6722 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6723 is a (possibly labeled) if statement which is not enclosed in braces
6724 and has an else clause. This is used to implement -Wparentheses. */
6727 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6728 bool in_compound, bool *if_p)
6732 location_t statement_location;
6737 /* There is no statement yet. */
6738 statement = NULL_TREE;
6739 /* Peek at the next token. */
6740 token = cp_lexer_peek_token (parser->lexer);
6741 /* Remember the location of the first token in the statement. */
6742 statement_location = token->location;
6743 /* If this is a keyword, then that will often determine what kind of
6744 statement we have. */
6745 if (token->type == CPP_KEYWORD)
6747 enum rid keyword = token->keyword;
6753 /* Looks like a labeled-statement with a case label.
6754 Parse the label, and then use tail recursion to parse
6756 cp_parser_label_for_labeled_statement (parser);
6761 statement = cp_parser_selection_statement (parser, if_p);
6767 statement = cp_parser_iteration_statement (parser);
6774 statement = cp_parser_jump_statement (parser);
6777 /* Objective-C++ exception-handling constructs. */
6780 case RID_AT_FINALLY:
6781 case RID_AT_SYNCHRONIZED:
6783 statement = cp_parser_objc_statement (parser);
6787 statement = cp_parser_try_block (parser);
6791 /* This must be a namespace alias definition. */
6792 cp_parser_declaration_statement (parser);
6796 /* It might be a keyword like `int' that can start a
6797 declaration-statement. */
6801 else if (token->type == CPP_NAME)
6803 /* If the next token is a `:', then we are looking at a
6804 labeled-statement. */
6805 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6806 if (token->type == CPP_COLON)
6808 /* Looks like a labeled-statement with an ordinary label.
6809 Parse the label, and then use tail recursion to parse
6811 cp_parser_label_for_labeled_statement (parser);
6815 /* Anything that starts with a `{' must be a compound-statement. */
6816 else if (token->type == CPP_OPEN_BRACE)
6817 statement = cp_parser_compound_statement (parser, NULL, false);
6818 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6819 a statement all its own. */
6820 else if (token->type == CPP_PRAGMA)
6822 /* Only certain OpenMP pragmas are attached to statements, and thus
6823 are considered statements themselves. All others are not. In
6824 the context of a compound, accept the pragma as a "statement" and
6825 return so that we can check for a close brace. Otherwise we
6826 require a real statement and must go back and read one. */
6828 cp_parser_pragma (parser, pragma_compound);
6829 else if (!cp_parser_pragma (parser, pragma_stmt))
6833 else if (token->type == CPP_EOF)
6835 cp_parser_error (parser, "expected statement");
6839 /* Everything else must be a declaration-statement or an
6840 expression-statement. Try for the declaration-statement
6841 first, unless we are looking at a `;', in which case we know that
6842 we have an expression-statement. */
6845 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6847 cp_parser_parse_tentatively (parser);
6848 /* Try to parse the declaration-statement. */
6849 cp_parser_declaration_statement (parser);
6850 /* If that worked, we're done. */
6851 if (cp_parser_parse_definitely (parser))
6854 /* Look for an expression-statement instead. */
6855 statement = cp_parser_expression_statement (parser, in_statement_expr);
6858 /* Set the line number for the statement. */
6859 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6860 SET_EXPR_LOCATION (statement, statement_location);
6863 /* Parse the label for a labeled-statement, i.e.
6866 case constant-expression :
6870 case constant-expression ... constant-expression : statement
6872 When a label is parsed without errors, the label is added to the
6873 parse tree by the finish_* functions, so this function doesn't
6874 have to return the label. */
6877 cp_parser_label_for_labeled_statement (cp_parser* parser)
6881 /* The next token should be an identifier. */
6882 token = cp_lexer_peek_token (parser->lexer);
6883 if (token->type != CPP_NAME
6884 && token->type != CPP_KEYWORD)
6886 cp_parser_error (parser, "expected labeled-statement");
6890 switch (token->keyword)
6897 /* Consume the `case' token. */
6898 cp_lexer_consume_token (parser->lexer);
6899 /* Parse the constant-expression. */
6900 expr = cp_parser_constant_expression (parser,
6901 /*allow_non_constant_p=*/false,
6904 ellipsis = cp_lexer_peek_token (parser->lexer);
6905 if (ellipsis->type == CPP_ELLIPSIS)
6907 /* Consume the `...' token. */
6908 cp_lexer_consume_token (parser->lexer);
6910 cp_parser_constant_expression (parser,
6911 /*allow_non_constant_p=*/false,
6913 /* We don't need to emit warnings here, as the common code
6914 will do this for us. */
6917 expr_hi = NULL_TREE;
6919 if (parser->in_switch_statement_p)
6920 finish_case_label (expr, expr_hi);
6922 error ("%Hcase label %qE not within a switch statement",
6923 &token->location, expr);
6928 /* Consume the `default' token. */
6929 cp_lexer_consume_token (parser->lexer);
6931 if (parser->in_switch_statement_p)
6932 finish_case_label (NULL_TREE, NULL_TREE);
6934 error ("%Hcase label not within a switch statement", &token->location);
6938 /* Anything else must be an ordinary label. */
6939 finish_label_stmt (cp_parser_identifier (parser));
6943 /* Require the `:' token. */
6944 cp_parser_require (parser, CPP_COLON, "%<:%>");
6947 /* Parse an expression-statement.
6949 expression-statement:
6952 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6953 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6954 indicates whether this expression-statement is part of an
6955 expression statement. */
6958 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6960 tree statement = NULL_TREE;
6962 /* If the next token is a ';', then there is no expression
6964 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6965 statement = cp_parser_expression (parser, /*cast_p=*/false);
6967 /* Consume the final `;'. */
6968 cp_parser_consume_semicolon_at_end_of_statement (parser);
6970 if (in_statement_expr
6971 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6972 /* This is the final expression statement of a statement
6974 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6976 statement = finish_expr_stmt (statement);
6983 /* Parse a compound-statement.
6986 { statement-seq [opt] }
6991 { label-declaration-seq [opt] statement-seq [opt] }
6993 label-declaration-seq:
6995 label-declaration-seq label-declaration
6997 Returns a tree representing the statement. */
7000 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7005 /* Consume the `{'. */
7006 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7007 return error_mark_node;
7008 /* Begin the compound-statement. */
7009 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7010 /* If the next keyword is `__label__' we have a label declaration. */
7011 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7012 cp_parser_label_declaration (parser);
7013 /* Parse an (optional) statement-seq. */
7014 cp_parser_statement_seq_opt (parser, in_statement_expr);
7015 /* Finish the compound-statement. */
7016 finish_compound_stmt (compound_stmt);
7017 /* Consume the `}'. */
7018 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7020 return compound_stmt;
7023 /* Parse an (optional) statement-seq.
7027 statement-seq [opt] statement */
7030 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7032 /* Scan statements until there aren't any more. */
7035 cp_token *token = cp_lexer_peek_token (parser->lexer);
7037 /* If we're looking at a `}', then we've run out of statements. */
7038 if (token->type == CPP_CLOSE_BRACE
7039 || token->type == CPP_EOF
7040 || token->type == CPP_PRAGMA_EOL)
7043 /* If we are in a compound statement and find 'else' then
7044 something went wrong. */
7045 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7047 if (parser->in_statement & IN_IF_STMT)
7051 token = cp_lexer_consume_token (parser->lexer);
7052 error ("%H%<else%> without a previous %<if%>", &token->location);
7056 /* Parse the statement. */
7057 cp_parser_statement (parser, in_statement_expr, true, NULL);
7061 /* Parse a selection-statement.
7063 selection-statement:
7064 if ( condition ) statement
7065 if ( condition ) statement else statement
7066 switch ( condition ) statement
7068 Returns the new IF_STMT or SWITCH_STMT.
7070 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7071 is a (possibly labeled) if statement which is not enclosed in
7072 braces and has an else clause. This is used to implement
7076 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7084 /* Peek at the next token. */
7085 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7087 /* See what kind of keyword it is. */
7088 keyword = token->keyword;
7097 /* Look for the `('. */
7098 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7100 cp_parser_skip_to_end_of_statement (parser);
7101 return error_mark_node;
7104 /* Begin the selection-statement. */
7105 if (keyword == RID_IF)
7106 statement = begin_if_stmt ();
7108 statement = begin_switch_stmt ();
7110 /* Parse the condition. */
7111 condition = cp_parser_condition (parser);
7112 /* Look for the `)'. */
7113 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7114 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7115 /*consume_paren=*/true);
7117 if (keyword == RID_IF)
7120 unsigned char in_statement;
7122 /* Add the condition. */
7123 finish_if_stmt_cond (condition, statement);
7125 /* Parse the then-clause. */
7126 in_statement = parser->in_statement;
7127 parser->in_statement |= IN_IF_STMT;
7128 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7129 parser->in_statement = in_statement;
7131 finish_then_clause (statement);
7133 /* If the next token is `else', parse the else-clause. */
7134 if (cp_lexer_next_token_is_keyword (parser->lexer,
7137 /* Consume the `else' keyword. */
7138 cp_lexer_consume_token (parser->lexer);
7139 begin_else_clause (statement);
7140 /* Parse the else-clause. */
7141 cp_parser_implicitly_scoped_statement (parser, NULL);
7142 finish_else_clause (statement);
7144 /* If we are currently parsing a then-clause, then
7145 IF_P will not be NULL. We set it to true to
7146 indicate that this if statement has an else clause.
7147 This may trigger the Wparentheses warning below
7148 when we get back up to the parent if statement. */
7154 /* This if statement does not have an else clause. If
7155 NESTED_IF is true, then the then-clause is an if
7156 statement which does have an else clause. We warn
7157 about the potential ambiguity. */
7159 warning (OPT_Wparentheses,
7160 ("%Hsuggest explicit braces "
7161 "to avoid ambiguous %<else%>"),
7162 EXPR_LOCUS (statement));
7165 /* Now we're all done with the if-statement. */
7166 finish_if_stmt (statement);
7170 bool in_switch_statement_p;
7171 unsigned char in_statement;
7173 /* Add the condition. */
7174 finish_switch_cond (condition, statement);
7176 /* Parse the body of the switch-statement. */
7177 in_switch_statement_p = parser->in_switch_statement_p;
7178 in_statement = parser->in_statement;
7179 parser->in_switch_statement_p = true;
7180 parser->in_statement |= IN_SWITCH_STMT;
7181 cp_parser_implicitly_scoped_statement (parser, NULL);
7182 parser->in_switch_statement_p = in_switch_statement_p;
7183 parser->in_statement = in_statement;
7185 /* Now we're all done with the switch-statement. */
7186 finish_switch_stmt (statement);
7194 cp_parser_error (parser, "expected selection-statement");
7195 return error_mark_node;
7199 /* Parse a condition.
7203 type-specifier-seq declarator = initializer-clause
7204 type-specifier-seq declarator braced-init-list
7209 type-specifier-seq declarator asm-specification [opt]
7210 attributes [opt] = assignment-expression
7212 Returns the expression that should be tested. */
7215 cp_parser_condition (cp_parser* parser)
7217 cp_decl_specifier_seq type_specifiers;
7218 const char *saved_message;
7220 /* Try the declaration first. */
7221 cp_parser_parse_tentatively (parser);
7222 /* New types are not allowed in the type-specifier-seq for a
7224 saved_message = parser->type_definition_forbidden_message;
7225 parser->type_definition_forbidden_message
7226 = "types may not be defined in conditions";
7227 /* Parse the type-specifier-seq. */
7228 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7230 /* Restore the saved message. */
7231 parser->type_definition_forbidden_message = saved_message;
7232 /* If all is well, we might be looking at a declaration. */
7233 if (!cp_parser_error_occurred (parser))
7236 tree asm_specification;
7238 cp_declarator *declarator;
7239 tree initializer = NULL_TREE;
7241 /* Parse the declarator. */
7242 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7243 /*ctor_dtor_or_conv_p=*/NULL,
7244 /*parenthesized_p=*/NULL,
7245 /*member_p=*/false);
7246 /* Parse the attributes. */
7247 attributes = cp_parser_attributes_opt (parser);
7248 /* Parse the asm-specification. */
7249 asm_specification = cp_parser_asm_specification_opt (parser);
7250 /* If the next token is not an `=' or '{', then we might still be
7251 looking at an expression. For example:
7255 looks like a decl-specifier-seq and a declarator -- but then
7256 there is no `=', so this is an expression. */
7257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7258 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7259 cp_parser_simulate_error (parser);
7261 /* If we did see an `=' or '{', then we are looking at a declaration
7263 if (cp_parser_parse_definitely (parser))
7266 bool non_constant_p;
7267 bool flags = LOOKUP_ONLYCONVERTING;
7269 /* Create the declaration. */
7270 decl = start_decl (declarator, &type_specifiers,
7271 /*initialized_p=*/true,
7272 attributes, /*prefix_attributes=*/NULL_TREE,
7275 /* Parse the initializer. */
7276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7278 initializer = cp_parser_braced_list (parser, &non_constant_p);
7279 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7284 /* Consume the `='. */
7285 cp_lexer_consume_token (parser->lexer);
7286 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7288 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7289 maybe_warn_cpp0x ("extended initializer lists");
7291 if (!non_constant_p)
7292 initializer = fold_non_dependent_expr (initializer);
7294 /* Process the initializer. */
7295 cp_finish_decl (decl,
7296 initializer, !non_constant_p,
7301 pop_scope (pushed_scope);
7303 return convert_from_reference (decl);
7306 /* If we didn't even get past the declarator successfully, we are
7307 definitely not looking at a declaration. */
7309 cp_parser_abort_tentative_parse (parser);
7311 /* Otherwise, we are looking at an expression. */
7312 return cp_parser_expression (parser, /*cast_p=*/false);
7315 /* We check for a ) immediately followed by ; with no whitespacing
7316 between. This is used to issue a warning for:
7324 as the semicolon is probably extraneous.
7326 On parse errors, the next token might not be a ), so do nothing in
7330 check_empty_body (cp_parser* parser, const char* type)
7333 cp_token *close_paren;
7334 expanded_location close_loc;
7335 expanded_location semi_loc;
7337 close_paren = cp_lexer_peek_token (parser->lexer);
7338 if (close_paren->type != CPP_CLOSE_PAREN)
7341 close_loc = expand_location (close_paren->location);
7342 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7344 if (token->type != CPP_SEMICOLON
7345 || (token->flags & PREV_WHITE))
7348 semi_loc = expand_location (token->location);
7349 if (close_loc.line == semi_loc.line
7350 && close_loc.column+1 == semi_loc.column)
7351 warning (OPT_Wempty_body,
7352 "suggest a space before %<;%> or explicit braces around empty "
7353 "body in %<%s%> statement",
7357 /* Parse an iteration-statement.
7359 iteration-statement:
7360 while ( condition ) statement
7361 do statement while ( expression ) ;
7362 for ( for-init-statement condition [opt] ; expression [opt] )
7365 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7368 cp_parser_iteration_statement (cp_parser* parser)
7373 unsigned char in_statement;
7375 /* Peek at the next token. */
7376 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7378 return error_mark_node;
7380 /* Remember whether or not we are already within an iteration
7382 in_statement = parser->in_statement;
7384 /* See what kind of keyword it is. */
7385 keyword = token->keyword;
7392 /* Begin the while-statement. */
7393 statement = begin_while_stmt ();
7394 /* Look for the `('. */
7395 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7396 /* Parse the condition. */
7397 condition = cp_parser_condition (parser);
7398 finish_while_stmt_cond (condition, statement);
7399 check_empty_body (parser, "while");
7400 /* Look for the `)'. */
7401 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7402 /* Parse the dependent statement. */
7403 parser->in_statement = IN_ITERATION_STMT;
7404 cp_parser_already_scoped_statement (parser);
7405 parser->in_statement = in_statement;
7406 /* We're done with the while-statement. */
7407 finish_while_stmt (statement);
7415 /* Begin the do-statement. */
7416 statement = begin_do_stmt ();
7417 /* Parse the body of the do-statement. */
7418 parser->in_statement = IN_ITERATION_STMT;
7419 cp_parser_implicitly_scoped_statement (parser, NULL);
7420 parser->in_statement = in_statement;
7421 finish_do_body (statement);
7422 /* Look for the `while' keyword. */
7423 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7424 /* Look for the `('. */
7425 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7426 /* Parse the expression. */
7427 expression = cp_parser_expression (parser, /*cast_p=*/false);
7428 /* We're done with the do-statement. */
7429 finish_do_stmt (expression, statement);
7430 /* Look for the `)'. */
7431 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7432 /* Look for the `;'. */
7433 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7439 tree condition = NULL_TREE;
7440 tree expression = NULL_TREE;
7442 /* Begin the for-statement. */
7443 statement = begin_for_stmt ();
7444 /* Look for the `('. */
7445 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7446 /* Parse the initialization. */
7447 cp_parser_for_init_statement (parser);
7448 finish_for_init_stmt (statement);
7450 /* If there's a condition, process it. */
7451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7452 condition = cp_parser_condition (parser);
7453 finish_for_cond (condition, statement);
7454 /* Look for the `;'. */
7455 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7457 /* If there's an expression, process it. */
7458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7459 expression = cp_parser_expression (parser, /*cast_p=*/false);
7460 finish_for_expr (expression, statement);
7461 check_empty_body (parser, "for");
7462 /* Look for the `)'. */
7463 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7465 /* Parse the body of the for-statement. */
7466 parser->in_statement = IN_ITERATION_STMT;
7467 cp_parser_already_scoped_statement (parser);
7468 parser->in_statement = in_statement;
7470 /* We're done with the for-statement. */
7471 finish_for_stmt (statement);
7476 cp_parser_error (parser, "expected iteration-statement");
7477 statement = error_mark_node;
7484 /* Parse a for-init-statement.
7487 expression-statement
7488 simple-declaration */
7491 cp_parser_for_init_statement (cp_parser* parser)
7493 /* If the next token is a `;', then we have an empty
7494 expression-statement. Grammatically, this is also a
7495 simple-declaration, but an invalid one, because it does not
7496 declare anything. Therefore, if we did not handle this case
7497 specially, we would issue an error message about an invalid
7499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7501 /* We're going to speculatively look for a declaration, falling back
7502 to an expression, if necessary. */
7503 cp_parser_parse_tentatively (parser);
7504 /* Parse the declaration. */
7505 cp_parser_simple_declaration (parser,
7506 /*function_definition_allowed_p=*/false);
7507 /* If the tentative parse failed, then we shall need to look for an
7508 expression-statement. */
7509 if (cp_parser_parse_definitely (parser))
7513 cp_parser_expression_statement (parser, false);
7516 /* Parse a jump-statement.
7521 return expression [opt] ;
7522 return braced-init-list ;
7530 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7533 cp_parser_jump_statement (cp_parser* parser)
7535 tree statement = error_mark_node;
7538 unsigned char in_statement;
7540 /* Peek at the next token. */
7541 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7543 return error_mark_node;
7545 /* See what kind of keyword it is. */
7546 keyword = token->keyword;
7550 in_statement = parser->in_statement & ~IN_IF_STMT;
7551 switch (in_statement)
7554 error ("%Hbreak statement not within loop or switch", &token->location);
7557 gcc_assert ((in_statement & IN_SWITCH_STMT)
7558 || in_statement == IN_ITERATION_STMT);
7559 statement = finish_break_stmt ();
7562 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7565 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7568 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7572 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7575 error ("%Hcontinue statement not within a loop", &token->location);
7577 case IN_ITERATION_STMT:
7579 statement = finish_continue_stmt ();
7582 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7587 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7593 bool expr_non_constant_p;
7595 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7597 maybe_warn_cpp0x ("extended initializer lists");
7598 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7600 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7601 expr = cp_parser_expression (parser, /*cast_p=*/false);
7603 /* If the next token is a `;', then there is no
7606 /* Build the return-statement. */
7607 statement = finish_return_stmt (expr);
7608 /* Look for the final `;'. */
7609 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7614 /* Create the goto-statement. */
7615 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7617 /* Issue a warning about this use of a GNU extension. */
7619 pedwarn ("%HISO C++ forbids computed gotos", &token->location);
7620 /* Consume the '*' token. */
7621 cp_lexer_consume_token (parser->lexer);
7622 /* Parse the dependent expression. */
7623 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7626 finish_goto_stmt (cp_parser_identifier (parser));
7627 /* Look for the final `;'. */
7628 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7632 cp_parser_error (parser, "expected jump-statement");
7639 /* Parse a declaration-statement.
7641 declaration-statement:
7642 block-declaration */
7645 cp_parser_declaration_statement (cp_parser* parser)
7649 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7650 p = obstack_alloc (&declarator_obstack, 0);
7652 /* Parse the block-declaration. */
7653 cp_parser_block_declaration (parser, /*statement_p=*/true);
7655 /* Free any declarators allocated. */
7656 obstack_free (&declarator_obstack, p);
7658 /* Finish off the statement. */
7662 /* Some dependent statements (like `if (cond) statement'), are
7663 implicitly in their own scope. In other words, if the statement is
7664 a single statement (as opposed to a compound-statement), it is
7665 none-the-less treated as if it were enclosed in braces. Any
7666 declarations appearing in the dependent statement are out of scope
7667 after control passes that point. This function parses a statement,
7668 but ensures that is in its own scope, even if it is not a
7671 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7672 is a (possibly labeled) if statement which is not enclosed in
7673 braces and has an else clause. This is used to implement
7676 Returns the new statement. */
7679 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7686 /* Mark if () ; with a special NOP_EXPR. */
7687 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7689 cp_lexer_consume_token (parser->lexer);
7690 statement = add_stmt (build_empty_stmt ());
7692 /* if a compound is opened, we simply parse the statement directly. */
7693 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7694 statement = cp_parser_compound_statement (parser, NULL, false);
7695 /* If the token is not a `{', then we must take special action. */
7698 /* Create a compound-statement. */
7699 statement = begin_compound_stmt (0);
7700 /* Parse the dependent-statement. */
7701 cp_parser_statement (parser, NULL_TREE, false, if_p);
7702 /* Finish the dummy compound-statement. */
7703 finish_compound_stmt (statement);
7706 /* Return the statement. */
7710 /* For some dependent statements (like `while (cond) statement'), we
7711 have already created a scope. Therefore, even if the dependent
7712 statement is a compound-statement, we do not want to create another
7716 cp_parser_already_scoped_statement (cp_parser* parser)
7718 /* If the token is a `{', then we must take special action. */
7719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7720 cp_parser_statement (parser, NULL_TREE, false, NULL);
7723 /* Avoid calling cp_parser_compound_statement, so that we
7724 don't create a new scope. Do everything else by hand. */
7725 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7726 cp_parser_statement_seq_opt (parser, NULL_TREE);
7727 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7731 /* Declarations [gram.dcl.dcl] */
7733 /* Parse an optional declaration-sequence.
7737 declaration-seq declaration */
7740 cp_parser_declaration_seq_opt (cp_parser* parser)
7746 token = cp_lexer_peek_token (parser->lexer);
7748 if (token->type == CPP_CLOSE_BRACE
7749 || token->type == CPP_EOF
7750 || token->type == CPP_PRAGMA_EOL)
7753 if (token->type == CPP_SEMICOLON)
7755 /* A declaration consisting of a single semicolon is
7756 invalid. Allow it unless we're being pedantic. */
7757 cp_lexer_consume_token (parser->lexer);
7758 if (pedantic && !in_system_header)
7759 pedwarn ("extra %<;%>");
7763 /* If we're entering or exiting a region that's implicitly
7764 extern "C", modify the lang context appropriately. */
7765 if (!parser->implicit_extern_c && token->implicit_extern_c)
7767 push_lang_context (lang_name_c);
7768 parser->implicit_extern_c = true;
7770 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7772 pop_lang_context ();
7773 parser->implicit_extern_c = false;
7776 if (token->type == CPP_PRAGMA)
7778 /* A top-level declaration can consist solely of a #pragma.
7779 A nested declaration cannot, so this is done here and not
7780 in cp_parser_declaration. (A #pragma at block scope is
7781 handled in cp_parser_statement.) */
7782 cp_parser_pragma (parser, pragma_external);
7786 /* Parse the declaration itself. */
7787 cp_parser_declaration (parser);
7791 /* Parse a declaration.
7796 template-declaration
7797 explicit-instantiation
7798 explicit-specialization
7799 linkage-specification
7800 namespace-definition
7805 __extension__ declaration */
7808 cp_parser_declaration (cp_parser* parser)
7815 /* Check for the `__extension__' keyword. */
7816 if (cp_parser_extension_opt (parser, &saved_pedantic))
7818 /* Parse the qualified declaration. */
7819 cp_parser_declaration (parser);
7820 /* Restore the PEDANTIC flag. */
7821 pedantic = saved_pedantic;
7826 /* Try to figure out what kind of declaration is present. */
7827 token1 = *cp_lexer_peek_token (parser->lexer);
7829 if (token1.type != CPP_EOF)
7830 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7833 token2.type = CPP_EOF;
7834 token2.keyword = RID_MAX;
7837 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7838 p = obstack_alloc (&declarator_obstack, 0);
7840 /* If the next token is `extern' and the following token is a string
7841 literal, then we have a linkage specification. */
7842 if (token1.keyword == RID_EXTERN
7843 && cp_parser_is_string_literal (&token2))
7844 cp_parser_linkage_specification (parser);
7845 /* If the next token is `template', then we have either a template
7846 declaration, an explicit instantiation, or an explicit
7848 else if (token1.keyword == RID_TEMPLATE)
7850 /* `template <>' indicates a template specialization. */
7851 if (token2.type == CPP_LESS
7852 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7853 cp_parser_explicit_specialization (parser);
7854 /* `template <' indicates a template declaration. */
7855 else if (token2.type == CPP_LESS)
7856 cp_parser_template_declaration (parser, /*member_p=*/false);
7857 /* Anything else must be an explicit instantiation. */
7859 cp_parser_explicit_instantiation (parser);
7861 /* If the next token is `export', then we have a template
7863 else if (token1.keyword == RID_EXPORT)
7864 cp_parser_template_declaration (parser, /*member_p=*/false);
7865 /* If the next token is `extern', 'static' or 'inline' and the one
7866 after that is `template', we have a GNU extended explicit
7867 instantiation directive. */
7868 else if (cp_parser_allow_gnu_extensions_p (parser)
7869 && (token1.keyword == RID_EXTERN
7870 || token1.keyword == RID_STATIC
7871 || token1.keyword == RID_INLINE)
7872 && token2.keyword == RID_TEMPLATE)
7873 cp_parser_explicit_instantiation (parser);
7874 /* If the next token is `namespace', check for a named or unnamed
7875 namespace definition. */
7876 else if (token1.keyword == RID_NAMESPACE
7877 && (/* A named namespace definition. */
7878 (token2.type == CPP_NAME
7879 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7881 /* An unnamed namespace definition. */
7882 || token2.type == CPP_OPEN_BRACE
7883 || token2.keyword == RID_ATTRIBUTE))
7884 cp_parser_namespace_definition (parser);
7885 /* An inline (associated) namespace definition. */
7886 else if (token1.keyword == RID_INLINE
7887 && token2.keyword == RID_NAMESPACE)
7888 cp_parser_namespace_definition (parser);
7889 /* Objective-C++ declaration/definition. */
7890 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7891 cp_parser_objc_declaration (parser);
7892 /* We must have either a block declaration or a function
7895 /* Try to parse a block-declaration, or a function-definition. */
7896 cp_parser_block_declaration (parser, /*statement_p=*/false);
7898 /* Free any declarators allocated. */
7899 obstack_free (&declarator_obstack, p);
7902 /* Parse a block-declaration.
7907 namespace-alias-definition
7914 __extension__ block-declaration
7919 static_assert-declaration
7921 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7922 part of a declaration-statement. */
7925 cp_parser_block_declaration (cp_parser *parser,
7931 /* Check for the `__extension__' keyword. */
7932 if (cp_parser_extension_opt (parser, &saved_pedantic))
7934 /* Parse the qualified declaration. */
7935 cp_parser_block_declaration (parser, statement_p);
7936 /* Restore the PEDANTIC flag. */
7937 pedantic = saved_pedantic;
7942 /* Peek at the next token to figure out which kind of declaration is
7944 token1 = cp_lexer_peek_token (parser->lexer);
7946 /* If the next keyword is `asm', we have an asm-definition. */
7947 if (token1->keyword == RID_ASM)
7950 cp_parser_commit_to_tentative_parse (parser);
7951 cp_parser_asm_definition (parser);
7953 /* If the next keyword is `namespace', we have a
7954 namespace-alias-definition. */
7955 else if (token1->keyword == RID_NAMESPACE)
7956 cp_parser_namespace_alias_definition (parser);
7957 /* If the next keyword is `using', we have either a
7958 using-declaration or a using-directive. */
7959 else if (token1->keyword == RID_USING)
7964 cp_parser_commit_to_tentative_parse (parser);
7965 /* If the token after `using' is `namespace', then we have a
7967 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7968 if (token2->keyword == RID_NAMESPACE)
7969 cp_parser_using_directive (parser);
7970 /* Otherwise, it's a using-declaration. */
7972 cp_parser_using_declaration (parser,
7973 /*access_declaration_p=*/false);
7975 /* If the next keyword is `__label__' we have a misplaced label
7977 else if (token1->keyword == RID_LABEL)
7979 cp_lexer_consume_token (parser->lexer);
7980 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
7981 cp_parser_skip_to_end_of_statement (parser);
7982 /* If the next token is now a `;', consume it. */
7983 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7984 cp_lexer_consume_token (parser->lexer);
7986 /* If the next token is `static_assert' we have a static assertion. */
7987 else if (token1->keyword == RID_STATIC_ASSERT)
7988 cp_parser_static_assert (parser, /*member_p=*/false);
7989 /* Anything else must be a simple-declaration. */
7991 cp_parser_simple_declaration (parser, !statement_p);
7994 /* Parse a simple-declaration.
7997 decl-specifier-seq [opt] init-declarator-list [opt] ;
7999 init-declarator-list:
8001 init-declarator-list , init-declarator
8003 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8004 function-definition as a simple-declaration. */
8007 cp_parser_simple_declaration (cp_parser* parser,
8008 bool function_definition_allowed_p)
8010 cp_decl_specifier_seq decl_specifiers;
8011 int declares_class_or_enum;
8012 bool saw_declarator;
8014 /* Defer access checks until we know what is being declared; the
8015 checks for names appearing in the decl-specifier-seq should be
8016 done as if we were in the scope of the thing being declared. */
8017 push_deferring_access_checks (dk_deferred);
8019 /* Parse the decl-specifier-seq. We have to keep track of whether
8020 or not the decl-specifier-seq declares a named class or
8021 enumeration type, since that is the only case in which the
8022 init-declarator-list is allowed to be empty.
8026 In a simple-declaration, the optional init-declarator-list can be
8027 omitted only when declaring a class or enumeration, that is when
8028 the decl-specifier-seq contains either a class-specifier, an
8029 elaborated-type-specifier, or an enum-specifier. */
8030 cp_parser_decl_specifier_seq (parser,
8031 CP_PARSER_FLAGS_OPTIONAL,
8033 &declares_class_or_enum);
8034 /* We no longer need to defer access checks. */
8035 stop_deferring_access_checks ();
8037 /* In a block scope, a valid declaration must always have a
8038 decl-specifier-seq. By not trying to parse declarators, we can
8039 resolve the declaration/expression ambiguity more quickly. */
8040 if (!function_definition_allowed_p
8041 && !decl_specifiers.any_specifiers_p)
8043 cp_parser_error (parser, "expected declaration");
8047 /* If the next two tokens are both identifiers, the code is
8048 erroneous. The usual cause of this situation is code like:
8052 where "T" should name a type -- but does not. */
8053 if (!decl_specifiers.type
8054 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8056 /* If parsing tentatively, we should commit; we really are
8057 looking at a declaration. */
8058 cp_parser_commit_to_tentative_parse (parser);
8063 /* If we have seen at least one decl-specifier, and the next token
8064 is not a parenthesis, then we must be looking at a declaration.
8065 (After "int (" we might be looking at a functional cast.) */
8066 if (decl_specifiers.any_specifiers_p
8067 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8068 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8069 cp_parser_commit_to_tentative_parse (parser);
8071 /* Keep going until we hit the `;' at the end of the simple
8073 saw_declarator = false;
8074 while (cp_lexer_next_token_is_not (parser->lexer,
8078 bool function_definition_p;
8083 /* If we are processing next declarator, coma is expected */
8084 token = cp_lexer_peek_token (parser->lexer);
8085 gcc_assert (token->type == CPP_COMMA);
8086 cp_lexer_consume_token (parser->lexer);
8089 saw_declarator = true;
8091 /* Parse the init-declarator. */
8092 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8094 function_definition_allowed_p,
8096 declares_class_or_enum,
8097 &function_definition_p);
8098 /* If an error occurred while parsing tentatively, exit quickly.
8099 (That usually happens when in the body of a function; each
8100 statement is treated as a declaration-statement until proven
8102 if (cp_parser_error_occurred (parser))
8104 /* Handle function definitions specially. */
8105 if (function_definition_p)
8107 /* If the next token is a `,', then we are probably
8108 processing something like:
8112 which is erroneous. */
8113 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8115 cp_token *token = cp_lexer_peek_token (parser->lexer);
8116 error ("%Hmixing declarations and function-definitions is forbidden",
8119 /* Otherwise, we're done with the list of declarators. */
8122 pop_deferring_access_checks ();
8126 /* The next token should be either a `,' or a `;'. */
8127 token = cp_lexer_peek_token (parser->lexer);
8128 /* If it's a `,', there are more declarators to come. */
8129 if (token->type == CPP_COMMA)
8130 /* will be consumed next time around */;
8131 /* If it's a `;', we are done. */
8132 else if (token->type == CPP_SEMICOLON)
8134 /* Anything else is an error. */
8137 /* If we have already issued an error message we don't need
8138 to issue another one. */
8139 if (decl != error_mark_node
8140 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8141 cp_parser_error (parser, "expected %<,%> or %<;%>");
8142 /* Skip tokens until we reach the end of the statement. */
8143 cp_parser_skip_to_end_of_statement (parser);
8144 /* If the next token is now a `;', consume it. */
8145 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8146 cp_lexer_consume_token (parser->lexer);
8149 /* After the first time around, a function-definition is not
8150 allowed -- even if it was OK at first. For example:
8155 function_definition_allowed_p = false;
8158 /* Issue an error message if no declarators are present, and the
8159 decl-specifier-seq does not itself declare a class or
8161 if (!saw_declarator)
8163 if (cp_parser_declares_only_class_p (parser))
8164 shadow_tag (&decl_specifiers);
8165 /* Perform any deferred access checks. */
8166 perform_deferred_access_checks ();
8169 /* Consume the `;'. */
8170 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8173 pop_deferring_access_checks ();
8176 /* Parse a decl-specifier-seq.
8179 decl-specifier-seq [opt] decl-specifier
8182 storage-class-specifier
8193 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8195 The parser flags FLAGS is used to control type-specifier parsing.
8197 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8200 1: one of the decl-specifiers is an elaborated-type-specifier
8201 (i.e., a type declaration)
8202 2: one of the decl-specifiers is an enum-specifier or a
8203 class-specifier (i.e., a type definition)
8208 cp_parser_decl_specifier_seq (cp_parser* parser,
8209 cp_parser_flags flags,
8210 cp_decl_specifier_seq *decl_specs,
8211 int* declares_class_or_enum)
8213 bool constructor_possible_p = !parser->in_declarator_p;
8214 cp_token *start_token = NULL;
8216 /* Clear DECL_SPECS. */
8217 clear_decl_specs (decl_specs);
8219 /* Assume no class or enumeration type is declared. */
8220 *declares_class_or_enum = 0;
8222 /* Keep reading specifiers until there are no more to read. */
8226 bool found_decl_spec;
8229 /* Peek at the next token. */
8230 token = cp_lexer_peek_token (parser->lexer);
8232 /* Save the first token of the decl spec list for error
8235 start_token = token;
8236 /* Handle attributes. */
8237 if (token->keyword == RID_ATTRIBUTE)
8239 /* Parse the attributes. */
8240 decl_specs->attributes
8241 = chainon (decl_specs->attributes,
8242 cp_parser_attributes_opt (parser));
8245 /* Assume we will find a decl-specifier keyword. */
8246 found_decl_spec = true;
8247 /* If the next token is an appropriate keyword, we can simply
8248 add it to the list. */
8249 switch (token->keyword)
8254 if (!at_class_scope_p ())
8256 error ("%H%<friend%> used outside of class", &token->location);
8257 cp_lexer_purge_token (parser->lexer);
8261 ++decl_specs->specs[(int) ds_friend];
8262 /* Consume the token. */
8263 cp_lexer_consume_token (parser->lexer);
8267 /* function-specifier:
8274 cp_parser_function_specifier_opt (parser, decl_specs);
8280 ++decl_specs->specs[(int) ds_typedef];
8281 /* Consume the token. */
8282 cp_lexer_consume_token (parser->lexer);
8283 /* A constructor declarator cannot appear in a typedef. */
8284 constructor_possible_p = false;
8285 /* The "typedef" keyword can only occur in a declaration; we
8286 may as well commit at this point. */
8287 cp_parser_commit_to_tentative_parse (parser);
8289 if (decl_specs->storage_class != sc_none)
8290 decl_specs->conflicting_specifiers_p = true;
8293 /* storage-class-specifier:
8303 /* Consume the token. */
8304 cp_lexer_consume_token (parser->lexer);
8306 if (cxx_dialect == cxx98)
8308 /* Complain about `auto' as a storage specifier, if
8309 we're complaining about C++0x compatibility. */
8312 "%H%<auto%> will change meaning in C++0x; please remove it",
8315 /* Set the storage class anyway. */
8316 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8320 /* We do not yet support the use of `auto' as a
8322 error ("%HC++0x %<auto%> specifier not supported", &token->location);
8329 /* Consume the token. */
8330 cp_lexer_consume_token (parser->lexer);
8331 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8335 /* Consume the token. */
8336 cp_lexer_consume_token (parser->lexer);
8337 ++decl_specs->specs[(int) ds_thread];
8341 /* We did not yet find a decl-specifier yet. */
8342 found_decl_spec = false;
8346 /* Constructors are a special case. The `S' in `S()' is not a
8347 decl-specifier; it is the beginning of the declarator. */
8350 && constructor_possible_p
8351 && (cp_parser_constructor_declarator_p
8352 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8354 /* If we don't have a DECL_SPEC yet, then we must be looking at
8355 a type-specifier. */
8356 if (!found_decl_spec && !constructor_p)
8358 int decl_spec_declares_class_or_enum;
8359 bool is_cv_qualifier;
8363 = cp_parser_type_specifier (parser, flags,
8365 /*is_declaration=*/true,
8366 &decl_spec_declares_class_or_enum,
8368 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8370 /* If this type-specifier referenced a user-defined type
8371 (a typedef, class-name, etc.), then we can't allow any
8372 more such type-specifiers henceforth.
8376 The longest sequence of decl-specifiers that could
8377 possibly be a type name is taken as the
8378 decl-specifier-seq of a declaration. The sequence shall
8379 be self-consistent as described below.
8383 As a general rule, at most one type-specifier is allowed
8384 in the complete decl-specifier-seq of a declaration. The
8385 only exceptions are the following:
8387 -- const or volatile can be combined with any other
8390 -- signed or unsigned can be combined with char, long,
8398 void g (const int Pc);
8400 Here, Pc is *not* part of the decl-specifier seq; it's
8401 the declarator. Therefore, once we see a type-specifier
8402 (other than a cv-qualifier), we forbid any additional
8403 user-defined types. We *do* still allow things like `int
8404 int' to be considered a decl-specifier-seq, and issue the
8405 error message later. */
8406 if (type_spec && !is_cv_qualifier)
8407 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8408 /* A constructor declarator cannot follow a type-specifier. */
8411 constructor_possible_p = false;
8412 found_decl_spec = true;
8416 /* If we still do not have a DECL_SPEC, then there are no more
8418 if (!found_decl_spec)
8421 decl_specs->any_specifiers_p = true;
8422 /* After we see one decl-specifier, further decl-specifiers are
8424 flags |= CP_PARSER_FLAGS_OPTIONAL;
8427 cp_parser_check_decl_spec (decl_specs, start_token->location);
8429 /* Don't allow a friend specifier with a class definition. */
8430 if (decl_specs->specs[(int) ds_friend] != 0
8431 && (*declares_class_or_enum & 2))
8432 error ("%Hclass definition may not be declared a friend",
8433 &start_token->location);
8436 /* Parse an (optional) storage-class-specifier.
8438 storage-class-specifier:
8447 storage-class-specifier:
8450 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8453 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8455 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8458 if (cxx_dialect != cxx98)
8460 /* Fall through for C++98. */
8467 /* Consume the token. */
8468 return cp_lexer_consume_token (parser->lexer)->u.value;
8475 /* Parse an (optional) function-specifier.
8482 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8483 Updates DECL_SPECS, if it is non-NULL. */
8486 cp_parser_function_specifier_opt (cp_parser* parser,
8487 cp_decl_specifier_seq *decl_specs)
8489 cp_token *token = cp_lexer_peek_token (parser->lexer);
8490 switch (token->keyword)
8494 ++decl_specs->specs[(int) ds_inline];
8498 /* 14.5.2.3 [temp.mem]
8500 A member function template shall not be virtual. */
8501 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8502 error ("%Htemplates may not be %<virtual%>", &token->location);
8503 else if (decl_specs)
8504 ++decl_specs->specs[(int) ds_virtual];
8509 ++decl_specs->specs[(int) ds_explicit];
8516 /* Consume the token. */
8517 return cp_lexer_consume_token (parser->lexer)->u.value;
8520 /* Parse a linkage-specification.
8522 linkage-specification:
8523 extern string-literal { declaration-seq [opt] }
8524 extern string-literal declaration */
8527 cp_parser_linkage_specification (cp_parser* parser)
8531 /* Look for the `extern' keyword. */
8532 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8534 /* Look for the string-literal. */
8535 linkage = cp_parser_string_literal (parser, false, false);
8537 /* Transform the literal into an identifier. If the literal is a
8538 wide-character string, or contains embedded NULs, then we can't
8539 handle it as the user wants. */
8540 if (strlen (TREE_STRING_POINTER (linkage))
8541 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8543 cp_parser_error (parser, "invalid linkage-specification");
8544 /* Assume C++ linkage. */
8545 linkage = lang_name_cplusplus;
8548 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8550 /* We're now using the new linkage. */
8551 push_lang_context (linkage);
8553 /* If the next token is a `{', then we're using the first
8555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8557 /* Consume the `{' token. */
8558 cp_lexer_consume_token (parser->lexer);
8559 /* Parse the declarations. */
8560 cp_parser_declaration_seq_opt (parser);
8561 /* Look for the closing `}'. */
8562 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8564 /* Otherwise, there's just one declaration. */
8567 bool saved_in_unbraced_linkage_specification_p;
8569 saved_in_unbraced_linkage_specification_p
8570 = parser->in_unbraced_linkage_specification_p;
8571 parser->in_unbraced_linkage_specification_p = true;
8572 cp_parser_declaration (parser);
8573 parser->in_unbraced_linkage_specification_p
8574 = saved_in_unbraced_linkage_specification_p;
8577 /* We're done with the linkage-specification. */
8578 pop_lang_context ();
8581 /* Parse a static_assert-declaration.
8583 static_assert-declaration:
8584 static_assert ( constant-expression , string-literal ) ;
8586 If MEMBER_P, this static_assert is a class member. */
8589 cp_parser_static_assert(cp_parser *parser, bool member_p)
8594 location_t saved_loc;
8596 /* Peek at the `static_assert' token so we can keep track of exactly
8597 where the static assertion started. */
8598 token = cp_lexer_peek_token (parser->lexer);
8599 saved_loc = token->location;
8601 /* Look for the `static_assert' keyword. */
8602 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8603 "%<static_assert%>"))
8606 /* We know we are in a static assertion; commit to any tentative
8608 if (cp_parser_parsing_tentatively (parser))
8609 cp_parser_commit_to_tentative_parse (parser);
8611 /* Parse the `(' starting the static assertion condition. */
8612 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8614 /* Parse the constant-expression. */
8616 cp_parser_constant_expression (parser,
8617 /*allow_non_constant_p=*/false,
8618 /*non_constant_p=*/NULL);
8620 /* Parse the separating `,'. */
8621 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8623 /* Parse the string-literal message. */
8624 message = cp_parser_string_literal (parser,
8625 /*translate=*/false,
8628 /* A `)' completes the static assertion. */
8629 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8630 cp_parser_skip_to_closing_parenthesis (parser,
8631 /*recovering=*/true,
8633 /*consume_paren=*/true);
8635 /* A semicolon terminates the declaration. */
8636 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8638 /* Complete the static assertion, which may mean either processing
8639 the static assert now or saving it for template instantiation. */
8640 finish_static_assert (condition, message, saved_loc, member_p);
8643 /* Parse a `decltype' type. Returns the type.
8645 simple-type-specifier:
8646 decltype ( expression ) */
8649 cp_parser_decltype (cp_parser *parser)
8652 bool id_expression_or_member_access_p = false;
8653 const char *saved_message;
8654 bool saved_integral_constant_expression_p;
8655 bool saved_non_integral_constant_expression_p;
8656 cp_token *id_expr_start_token;
8658 /* Look for the `decltype' token. */
8659 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8660 return error_mark_node;
8662 /* Types cannot be defined in a `decltype' expression. Save away the
8664 saved_message = parser->type_definition_forbidden_message;
8666 /* And create the new one. */
8667 parser->type_definition_forbidden_message
8668 = "types may not be defined in %<decltype%> expressions";
8670 /* The restrictions on constant-expressions do not apply inside
8671 decltype expressions. */
8672 saved_integral_constant_expression_p
8673 = parser->integral_constant_expression_p;
8674 saved_non_integral_constant_expression_p
8675 = parser->non_integral_constant_expression_p;
8676 parser->integral_constant_expression_p = false;
8678 /* Do not actually evaluate the expression. */
8681 /* Parse the opening `('. */
8682 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8683 return error_mark_node;
8685 /* First, try parsing an id-expression. */
8686 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8687 cp_parser_parse_tentatively (parser);
8688 expr = cp_parser_id_expression (parser,
8689 /*template_keyword_p=*/false,
8690 /*check_dependency_p=*/true,
8691 /*template_p=*/NULL,
8692 /*declarator_p=*/false,
8693 /*optional_p=*/false);
8695 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8697 bool non_integral_constant_expression_p = false;
8698 tree id_expression = expr;
8700 const char *error_msg;
8702 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8703 /* Lookup the name we got back from the id-expression. */
8704 expr = cp_parser_lookup_name (parser, expr,
8706 /*is_template=*/false,
8707 /*is_namespace=*/false,
8708 /*check_dependency=*/true,
8709 /*ambiguous_decls=*/NULL,
8710 id_expr_start_token->location);
8713 && expr != error_mark_node
8714 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8715 && TREE_CODE (expr) != TYPE_DECL
8716 && (TREE_CODE (expr) != BIT_NOT_EXPR
8717 || !TYPE_P (TREE_OPERAND (expr, 0)))
8718 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8720 /* Complete lookup of the id-expression. */
8721 expr = (finish_id_expression
8722 (id_expression, expr, parser->scope, &idk,
8723 /*integral_constant_expression_p=*/false,
8724 /*allow_non_integral_constant_expression_p=*/true,
8725 &non_integral_constant_expression_p,
8726 /*template_p=*/false,
8728 /*address_p=*/false,
8729 /*template_arg_p=*/false,
8731 id_expr_start_token->location));
8733 if (expr == error_mark_node)
8734 /* We found an id-expression, but it was something that we
8735 should not have found. This is an error, not something
8736 we can recover from, so note that we found an
8737 id-expression and we'll recover as gracefully as
8739 id_expression_or_member_access_p = true;
8743 && expr != error_mark_node
8744 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8745 /* We have an id-expression. */
8746 id_expression_or_member_access_p = true;
8749 if (!id_expression_or_member_access_p)
8751 /* Abort the id-expression parse. */
8752 cp_parser_abort_tentative_parse (parser);
8754 /* Parsing tentatively, again. */
8755 cp_parser_parse_tentatively (parser);
8757 /* Parse a class member access. */
8758 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8760 /*member_access_only_p=*/true);
8763 && expr != error_mark_node
8764 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8765 /* We have an id-expression. */
8766 id_expression_or_member_access_p = true;
8769 if (id_expression_or_member_access_p)
8770 /* We have parsed the complete id-expression or member access. */
8771 cp_parser_parse_definitely (parser);
8774 /* Abort our attempt to parse an id-expression or member access
8776 cp_parser_abort_tentative_parse (parser);
8778 /* Parse a full expression. */
8779 expr = cp_parser_expression (parser, /*cast_p=*/false);
8782 /* Go back to evaluating expressions. */
8785 /* Restore the old message and the integral constant expression
8787 parser->type_definition_forbidden_message = saved_message;
8788 parser->integral_constant_expression_p
8789 = saved_integral_constant_expression_p;
8790 parser->non_integral_constant_expression_p
8791 = saved_non_integral_constant_expression_p;
8793 if (expr == error_mark_node)
8795 /* Skip everything up to the closing `)'. */
8796 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8797 /*consume_paren=*/true);
8798 return error_mark_node;
8801 /* Parse to the closing `)'. */
8802 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8804 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8805 /*consume_paren=*/true);
8806 return error_mark_node;
8809 return finish_decltype_type (expr, id_expression_or_member_access_p);
8812 /* Special member functions [gram.special] */
8814 /* Parse a conversion-function-id.
8816 conversion-function-id:
8817 operator conversion-type-id
8819 Returns an IDENTIFIER_NODE representing the operator. */
8822 cp_parser_conversion_function_id (cp_parser* parser)
8826 tree saved_qualifying_scope;
8827 tree saved_object_scope;
8828 tree pushed_scope = NULL_TREE;
8830 /* Look for the `operator' token. */
8831 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8832 return error_mark_node;
8833 /* When we parse the conversion-type-id, the current scope will be
8834 reset. However, we need that information in able to look up the
8835 conversion function later, so we save it here. */
8836 saved_scope = parser->scope;
8837 saved_qualifying_scope = parser->qualifying_scope;
8838 saved_object_scope = parser->object_scope;
8839 /* We must enter the scope of the class so that the names of
8840 entities declared within the class are available in the
8841 conversion-type-id. For example, consider:
8848 S::operator I() { ... }
8850 In order to see that `I' is a type-name in the definition, we
8851 must be in the scope of `S'. */
8853 pushed_scope = push_scope (saved_scope);
8854 /* Parse the conversion-type-id. */
8855 type = cp_parser_conversion_type_id (parser);
8856 /* Leave the scope of the class, if any. */
8858 pop_scope (pushed_scope);
8859 /* Restore the saved scope. */
8860 parser->scope = saved_scope;
8861 parser->qualifying_scope = saved_qualifying_scope;
8862 parser->object_scope = saved_object_scope;
8863 /* If the TYPE is invalid, indicate failure. */
8864 if (type == error_mark_node)
8865 return error_mark_node;
8866 return mangle_conv_op_name_for_type (type);
8869 /* Parse a conversion-type-id:
8872 type-specifier-seq conversion-declarator [opt]
8874 Returns the TYPE specified. */
8877 cp_parser_conversion_type_id (cp_parser* parser)
8880 cp_decl_specifier_seq type_specifiers;
8881 cp_declarator *declarator;
8882 tree type_specified;
8884 /* Parse the attributes. */
8885 attributes = cp_parser_attributes_opt (parser);
8886 /* Parse the type-specifiers. */
8887 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8889 /* If that didn't work, stop. */
8890 if (type_specifiers.type == error_mark_node)
8891 return error_mark_node;
8892 /* Parse the conversion-declarator. */
8893 declarator = cp_parser_conversion_declarator_opt (parser);
8895 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8896 /*initialized=*/0, &attributes);
8898 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8899 return type_specified;
8902 /* Parse an (optional) conversion-declarator.
8904 conversion-declarator:
8905 ptr-operator conversion-declarator [opt]
8909 static cp_declarator *
8910 cp_parser_conversion_declarator_opt (cp_parser* parser)
8912 enum tree_code code;
8914 cp_cv_quals cv_quals;
8916 /* We don't know if there's a ptr-operator next, or not. */
8917 cp_parser_parse_tentatively (parser);
8918 /* Try the ptr-operator. */
8919 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8920 /* If it worked, look for more conversion-declarators. */
8921 if (cp_parser_parse_definitely (parser))
8923 cp_declarator *declarator;
8925 /* Parse another optional declarator. */
8926 declarator = cp_parser_conversion_declarator_opt (parser);
8928 return cp_parser_make_indirect_declarator
8929 (code, class_type, cv_quals, declarator);
8935 /* Parse an (optional) ctor-initializer.
8938 : mem-initializer-list
8940 Returns TRUE iff the ctor-initializer was actually present. */
8943 cp_parser_ctor_initializer_opt (cp_parser* parser)
8945 /* If the next token is not a `:', then there is no
8946 ctor-initializer. */
8947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8949 /* Do default initialization of any bases and members. */
8950 if (DECL_CONSTRUCTOR_P (current_function_decl))
8951 finish_mem_initializers (NULL_TREE);
8956 /* Consume the `:' token. */
8957 cp_lexer_consume_token (parser->lexer);
8958 /* And the mem-initializer-list. */
8959 cp_parser_mem_initializer_list (parser);
8964 /* Parse a mem-initializer-list.
8966 mem-initializer-list:
8967 mem-initializer ... [opt]
8968 mem-initializer ... [opt] , mem-initializer-list */
8971 cp_parser_mem_initializer_list (cp_parser* parser)
8973 tree mem_initializer_list = NULL_TREE;
8974 cp_token *token = cp_lexer_peek_token (parser->lexer);
8976 /* Let the semantic analysis code know that we are starting the
8977 mem-initializer-list. */
8978 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8979 error ("%Honly constructors take base initializers",
8982 /* Loop through the list. */
8985 tree mem_initializer;
8987 token = cp_lexer_peek_token (parser->lexer);
8988 /* Parse the mem-initializer. */
8989 mem_initializer = cp_parser_mem_initializer (parser);
8990 /* If the next token is a `...', we're expanding member initializers. */
8991 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8993 /* Consume the `...'. */
8994 cp_lexer_consume_token (parser->lexer);
8996 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8997 can be expanded but members cannot. */
8998 if (mem_initializer != error_mark_node
8999 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9001 error ("%Hcannot expand initializer for member %<%D%>",
9002 &token->location, TREE_PURPOSE (mem_initializer));
9003 mem_initializer = error_mark_node;
9006 /* Construct the pack expansion type. */
9007 if (mem_initializer != error_mark_node)
9008 mem_initializer = make_pack_expansion (mem_initializer);
9010 /* Add it to the list, unless it was erroneous. */
9011 if (mem_initializer != error_mark_node)
9013 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9014 mem_initializer_list = mem_initializer;
9016 /* If the next token is not a `,', we're done. */
9017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9019 /* Consume the `,' token. */
9020 cp_lexer_consume_token (parser->lexer);
9023 /* Perform semantic analysis. */
9024 if (DECL_CONSTRUCTOR_P (current_function_decl))
9025 finish_mem_initializers (mem_initializer_list);
9028 /* Parse a mem-initializer.
9031 mem-initializer-id ( expression-list [opt] )
9032 mem-initializer-id braced-init-list
9037 ( expression-list [opt] )
9039 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9040 class) or FIELD_DECL (for a non-static data member) to initialize;
9041 the TREE_VALUE is the expression-list. An empty initialization
9042 list is represented by void_list_node. */
9045 cp_parser_mem_initializer (cp_parser* parser)
9047 tree mem_initializer_id;
9048 tree expression_list;
9050 cp_token *token = cp_lexer_peek_token (parser->lexer);
9052 /* Find out what is being initialized. */
9053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9055 permerror ("%Hanachronistic old-style base class initializer",
9057 mem_initializer_id = NULL_TREE;
9060 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9061 member = expand_member_init (mem_initializer_id);
9062 if (member && !DECL_P (member))
9063 in_base_initializer = 1;
9065 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9067 bool expr_non_constant_p;
9068 maybe_warn_cpp0x ("extended initializer lists");
9069 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9070 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9071 expression_list = build_tree_list (NULL_TREE, expression_list);
9075 = cp_parser_parenthesized_expression_list (parser, false,
9077 /*allow_expansion_p=*/true,
9078 /*non_constant_p=*/NULL);
9079 if (expression_list == error_mark_node)
9080 return error_mark_node;
9081 if (!expression_list)
9082 expression_list = void_type_node;
9084 in_base_initializer = 0;
9086 return member ? build_tree_list (member, expression_list) : error_mark_node;
9089 /* Parse a mem-initializer-id.
9092 :: [opt] nested-name-specifier [opt] class-name
9095 Returns a TYPE indicating the class to be initializer for the first
9096 production. Returns an IDENTIFIER_NODE indicating the data member
9097 to be initialized for the second production. */
9100 cp_parser_mem_initializer_id (cp_parser* parser)
9102 bool global_scope_p;
9103 bool nested_name_specifier_p;
9104 bool template_p = false;
9107 cp_token *token = cp_lexer_peek_token (parser->lexer);
9109 /* `typename' is not allowed in this context ([temp.res]). */
9110 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9112 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9113 "member initializer is implicitly a type)",
9115 cp_lexer_consume_token (parser->lexer);
9117 /* Look for the optional `::' operator. */
9119 = (cp_parser_global_scope_opt (parser,
9120 /*current_scope_valid_p=*/false)
9122 /* Look for the optional nested-name-specifier. The simplest way to
9127 The keyword `typename' is not permitted in a base-specifier or
9128 mem-initializer; in these contexts a qualified name that
9129 depends on a template-parameter is implicitly assumed to be a
9132 is to assume that we have seen the `typename' keyword at this
9134 nested_name_specifier_p
9135 = (cp_parser_nested_name_specifier_opt (parser,
9136 /*typename_keyword_p=*/true,
9137 /*check_dependency_p=*/true,
9139 /*is_declaration=*/true)
9141 if (nested_name_specifier_p)
9142 template_p = cp_parser_optional_template_keyword (parser);
9143 /* If there is a `::' operator or a nested-name-specifier, then we
9144 are definitely looking for a class-name. */
9145 if (global_scope_p || nested_name_specifier_p)
9146 return cp_parser_class_name (parser,
9147 /*typename_keyword_p=*/true,
9148 /*template_keyword_p=*/template_p,
9150 /*check_dependency_p=*/true,
9151 /*class_head_p=*/false,
9152 /*is_declaration=*/true);
9153 /* Otherwise, we could also be looking for an ordinary identifier. */
9154 cp_parser_parse_tentatively (parser);
9155 /* Try a class-name. */
9156 id = cp_parser_class_name (parser,
9157 /*typename_keyword_p=*/true,
9158 /*template_keyword_p=*/false,
9160 /*check_dependency_p=*/true,
9161 /*class_head_p=*/false,
9162 /*is_declaration=*/true);
9163 /* If we found one, we're done. */
9164 if (cp_parser_parse_definitely (parser))
9166 /* Otherwise, look for an ordinary identifier. */
9167 return cp_parser_identifier (parser);
9170 /* Overloading [gram.over] */
9172 /* Parse an operator-function-id.
9174 operator-function-id:
9177 Returns an IDENTIFIER_NODE for the operator which is a
9178 human-readable spelling of the identifier, e.g., `operator +'. */
9181 cp_parser_operator_function_id (cp_parser* parser)
9183 /* Look for the `operator' keyword. */
9184 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9185 return error_mark_node;
9186 /* And then the name of the operator itself. */
9187 return cp_parser_operator (parser);
9190 /* Parse an operator.
9193 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9194 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9195 || ++ -- , ->* -> () []
9202 Returns an IDENTIFIER_NODE for the operator which is a
9203 human-readable spelling of the identifier, e.g., `operator +'. */
9206 cp_parser_operator (cp_parser* parser)
9208 tree id = NULL_TREE;
9211 /* Peek at the next token. */
9212 token = cp_lexer_peek_token (parser->lexer);
9213 /* Figure out which operator we have. */
9214 switch (token->type)
9220 /* The keyword should be either `new' or `delete'. */
9221 if (token->keyword == RID_NEW)
9223 else if (token->keyword == RID_DELETE)
9228 /* Consume the `new' or `delete' token. */
9229 cp_lexer_consume_token (parser->lexer);
9231 /* Peek at the next token. */
9232 token = cp_lexer_peek_token (parser->lexer);
9233 /* If it's a `[' token then this is the array variant of the
9235 if (token->type == CPP_OPEN_SQUARE)
9237 /* Consume the `[' token. */
9238 cp_lexer_consume_token (parser->lexer);
9239 /* Look for the `]' token. */
9240 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9241 id = ansi_opname (op == NEW_EXPR
9242 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9244 /* Otherwise, we have the non-array variant. */
9246 id = ansi_opname (op);
9252 id = ansi_opname (PLUS_EXPR);
9256 id = ansi_opname (MINUS_EXPR);
9260 id = ansi_opname (MULT_EXPR);
9264 id = ansi_opname (TRUNC_DIV_EXPR);
9268 id = ansi_opname (TRUNC_MOD_EXPR);
9272 id = ansi_opname (BIT_XOR_EXPR);
9276 id = ansi_opname (BIT_AND_EXPR);
9280 id = ansi_opname (BIT_IOR_EXPR);
9284 id = ansi_opname (BIT_NOT_EXPR);
9288 id = ansi_opname (TRUTH_NOT_EXPR);
9292 id = ansi_assopname (NOP_EXPR);
9296 id = ansi_opname (LT_EXPR);
9300 id = ansi_opname (GT_EXPR);
9304 id = ansi_assopname (PLUS_EXPR);
9308 id = ansi_assopname (MINUS_EXPR);
9312 id = ansi_assopname (MULT_EXPR);
9316 id = ansi_assopname (TRUNC_DIV_EXPR);
9320 id = ansi_assopname (TRUNC_MOD_EXPR);
9324 id = ansi_assopname (BIT_XOR_EXPR);
9328 id = ansi_assopname (BIT_AND_EXPR);
9332 id = ansi_assopname (BIT_IOR_EXPR);
9336 id = ansi_opname (LSHIFT_EXPR);
9340 id = ansi_opname (RSHIFT_EXPR);
9344 id = ansi_assopname (LSHIFT_EXPR);
9348 id = ansi_assopname (RSHIFT_EXPR);
9352 id = ansi_opname (EQ_EXPR);
9356 id = ansi_opname (NE_EXPR);
9360 id = ansi_opname (LE_EXPR);
9363 case CPP_GREATER_EQ:
9364 id = ansi_opname (GE_EXPR);
9368 id = ansi_opname (TRUTH_ANDIF_EXPR);
9372 id = ansi_opname (TRUTH_ORIF_EXPR);
9376 id = ansi_opname (POSTINCREMENT_EXPR);
9379 case CPP_MINUS_MINUS:
9380 id = ansi_opname (PREDECREMENT_EXPR);
9384 id = ansi_opname (COMPOUND_EXPR);
9387 case CPP_DEREF_STAR:
9388 id = ansi_opname (MEMBER_REF);
9392 id = ansi_opname (COMPONENT_REF);
9395 case CPP_OPEN_PAREN:
9396 /* Consume the `('. */
9397 cp_lexer_consume_token (parser->lexer);
9398 /* Look for the matching `)'. */
9399 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9400 return ansi_opname (CALL_EXPR);
9402 case CPP_OPEN_SQUARE:
9403 /* Consume the `['. */
9404 cp_lexer_consume_token (parser->lexer);
9405 /* Look for the matching `]'. */
9406 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9407 return ansi_opname (ARRAY_REF);
9410 /* Anything else is an error. */
9414 /* If we have selected an identifier, we need to consume the
9417 cp_lexer_consume_token (parser->lexer);
9418 /* Otherwise, no valid operator name was present. */
9421 cp_parser_error (parser, "expected operator");
9422 id = error_mark_node;
9428 /* Parse a template-declaration.
9430 template-declaration:
9431 export [opt] template < template-parameter-list > declaration
9433 If MEMBER_P is TRUE, this template-declaration occurs within a
9436 The grammar rule given by the standard isn't correct. What
9439 template-declaration:
9440 export [opt] template-parameter-list-seq
9441 decl-specifier-seq [opt] init-declarator [opt] ;
9442 export [opt] template-parameter-list-seq
9445 template-parameter-list-seq:
9446 template-parameter-list-seq [opt]
9447 template < template-parameter-list > */
9450 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9452 /* Check for `export'. */
9453 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9455 /* Consume the `export' token. */
9456 cp_lexer_consume_token (parser->lexer);
9457 /* Warn that we do not support `export'. */
9458 warning (0, "keyword %<export%> not implemented, and will be ignored");
9461 cp_parser_template_declaration_after_export (parser, member_p);
9464 /* Parse a template-parameter-list.
9466 template-parameter-list:
9468 template-parameter-list , template-parameter
9470 Returns a TREE_LIST. Each node represents a template parameter.
9471 The nodes are connected via their TREE_CHAINs. */
9474 cp_parser_template_parameter_list (cp_parser* parser)
9476 tree parameter_list = NULL_TREE;
9478 begin_template_parm_list ();
9483 bool is_parameter_pack;
9485 /* Parse the template-parameter. */
9486 parameter = cp_parser_template_parameter (parser,
9488 &is_parameter_pack);
9489 /* Add it to the list. */
9490 if (parameter != error_mark_node)
9491 parameter_list = process_template_parm (parameter_list,
9497 tree err_parm = build_tree_list (parameter, parameter);
9498 TREE_VALUE (err_parm) = error_mark_node;
9499 parameter_list = chainon (parameter_list, err_parm);
9502 /* If the next token is not a `,', we're done. */
9503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9505 /* Otherwise, consume the `,' token. */
9506 cp_lexer_consume_token (parser->lexer);
9509 return end_template_parm_list (parameter_list);
9512 /* Parse a template-parameter.
9516 parameter-declaration
9518 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9519 the parameter. The TREE_PURPOSE is the default value, if any.
9520 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9521 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9522 set to true iff this parameter is a parameter pack. */
9525 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9526 bool *is_parameter_pack)
9529 cp_parameter_declarator *parameter_declarator;
9530 cp_declarator *id_declarator;
9533 /* Assume it is a type parameter or a template parameter. */
9534 *is_non_type = false;
9535 /* Assume it not a parameter pack. */
9536 *is_parameter_pack = false;
9537 /* Peek at the next token. */
9538 token = cp_lexer_peek_token (parser->lexer);
9539 /* If it is `class' or `template', we have a type-parameter. */
9540 if (token->keyword == RID_TEMPLATE)
9541 return cp_parser_type_parameter (parser, is_parameter_pack);
9542 /* If it is `class' or `typename' we do not know yet whether it is a
9543 type parameter or a non-type parameter. Consider:
9545 template <typename T, typename T::X X> ...
9549 template <class C, class D*> ...
9551 Here, the first parameter is a type parameter, and the second is
9552 a non-type parameter. We can tell by looking at the token after
9553 the identifier -- if it is a `,', `=', or `>' then we have a type
9555 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9557 /* Peek at the token after `class' or `typename'. */
9558 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9559 /* If it's an ellipsis, we have a template type parameter
9561 if (token->type == CPP_ELLIPSIS)
9562 return cp_parser_type_parameter (parser, is_parameter_pack);
9563 /* If it's an identifier, skip it. */
9564 if (token->type == CPP_NAME)
9565 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9566 /* Now, see if the token looks like the end of a template
9568 if (token->type == CPP_COMMA
9569 || token->type == CPP_EQ
9570 || token->type == CPP_GREATER)
9571 return cp_parser_type_parameter (parser, is_parameter_pack);
9574 /* Otherwise, it is a non-type parameter.
9578 When parsing a default template-argument for a non-type
9579 template-parameter, the first non-nested `>' is taken as the end
9580 of the template parameter-list rather than a greater-than
9582 *is_non_type = true;
9583 parameter_declarator
9584 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9585 /*parenthesized_p=*/NULL);
9587 /* If the parameter declaration is marked as a parameter pack, set
9588 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9589 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9591 if (parameter_declarator
9592 && parameter_declarator->declarator
9593 && parameter_declarator->declarator->parameter_pack_p)
9595 *is_parameter_pack = true;
9596 parameter_declarator->declarator->parameter_pack_p = false;
9599 /* If the next token is an ellipsis, and we don't already have it
9600 marked as a parameter pack, then we have a parameter pack (that
9601 has no declarator). */
9602 if (!*is_parameter_pack
9603 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9604 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9606 /* Consume the `...'. */
9607 cp_lexer_consume_token (parser->lexer);
9608 maybe_warn_variadic_templates ();
9610 *is_parameter_pack = true;
9612 /* We might end up with a pack expansion as the type of the non-type
9613 template parameter, in which case this is a non-type template
9615 else if (parameter_declarator
9616 && parameter_declarator->decl_specifiers.type
9617 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9619 *is_parameter_pack = true;
9620 parameter_declarator->decl_specifiers.type =
9621 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9624 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9626 /* Parameter packs cannot have default arguments. However, a
9627 user may try to do so, so we'll parse them and give an
9628 appropriate diagnostic here. */
9630 /* Consume the `='. */
9631 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9632 cp_lexer_consume_token (parser->lexer);
9634 /* Find the name of the parameter pack. */
9635 id_declarator = parameter_declarator->declarator;
9636 while (id_declarator && id_declarator->kind != cdk_id)
9637 id_declarator = id_declarator->declarator;
9639 if (id_declarator && id_declarator->kind == cdk_id)
9640 error ("%Htemplate parameter pack %qD cannot have a default argument",
9641 &start_token->location, id_declarator->u.id.unqualified_name);
9643 error ("%Htemplate parameter pack cannot have a default argument",
9644 &start_token->location);
9646 /* Parse the default argument, but throw away the result. */
9647 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9650 parm = grokdeclarator (parameter_declarator->declarator,
9651 ¶meter_declarator->decl_specifiers,
9652 PARM, /*initialized=*/0,
9654 if (parm == error_mark_node)
9655 return error_mark_node;
9657 return build_tree_list (parameter_declarator->default_argument, parm);
9660 /* Parse a type-parameter.
9663 class identifier [opt]
9664 class identifier [opt] = type-id
9665 typename identifier [opt]
9666 typename identifier [opt] = type-id
9667 template < template-parameter-list > class identifier [opt]
9668 template < template-parameter-list > class identifier [opt]
9671 GNU Extension (variadic templates):
9674 class ... identifier [opt]
9675 typename ... identifier [opt]
9677 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9678 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9679 the declaration of the parameter.
9681 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9684 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9689 /* Look for a keyword to tell us what kind of parameter this is. */
9690 token = cp_parser_require (parser, CPP_KEYWORD,
9691 "%<class%>, %<typename%>, or %<template%>");
9693 return error_mark_node;
9695 switch (token->keyword)
9701 tree default_argument;
9703 /* If the next token is an ellipsis, we have a template
9705 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9707 /* Consume the `...' token. */
9708 cp_lexer_consume_token (parser->lexer);
9709 maybe_warn_variadic_templates ();
9711 *is_parameter_pack = true;
9714 /* If the next token is an identifier, then it names the
9716 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9717 identifier = cp_parser_identifier (parser);
9719 identifier = NULL_TREE;
9721 /* Create the parameter. */
9722 parameter = finish_template_type_parm (class_type_node, identifier);
9724 /* If the next token is an `=', we have a default argument. */
9725 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9727 /* Consume the `=' token. */
9728 cp_lexer_consume_token (parser->lexer);
9729 /* Parse the default-argument. */
9730 push_deferring_access_checks (dk_no_deferred);
9731 default_argument = cp_parser_type_id (parser);
9733 /* Template parameter packs cannot have default
9735 if (*is_parameter_pack)
9738 error ("%Htemplate parameter pack %qD cannot have a "
9739 "default argument", &token->location, identifier);
9741 error ("%Htemplate parameter packs cannot have "
9742 "default arguments", &token->location);
9743 default_argument = NULL_TREE;
9745 pop_deferring_access_checks ();
9748 default_argument = NULL_TREE;
9750 /* Create the combined representation of the parameter and the
9751 default argument. */
9752 parameter = build_tree_list (default_argument, parameter);
9758 tree parameter_list;
9760 tree default_argument;
9762 /* Look for the `<'. */
9763 cp_parser_require (parser, CPP_LESS, "%<<%>");
9764 /* Parse the template-parameter-list. */
9765 parameter_list = cp_parser_template_parameter_list (parser);
9766 /* Look for the `>'. */
9767 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9768 /* Look for the `class' keyword. */
9769 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9770 /* If the next token is an ellipsis, we have a template
9772 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9774 /* Consume the `...' token. */
9775 cp_lexer_consume_token (parser->lexer);
9776 maybe_warn_variadic_templates ();
9778 *is_parameter_pack = true;
9780 /* If the next token is an `=', then there is a
9781 default-argument. If the next token is a `>', we are at
9782 the end of the parameter-list. If the next token is a `,',
9783 then we are at the end of this parameter. */
9784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9785 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9786 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9788 identifier = cp_parser_identifier (parser);
9789 /* Treat invalid names as if the parameter were nameless. */
9790 if (identifier == error_mark_node)
9791 identifier = NULL_TREE;
9794 identifier = NULL_TREE;
9796 /* Create the template parameter. */
9797 parameter = finish_template_template_parm (class_type_node,
9800 /* If the next token is an `=', then there is a
9801 default-argument. */
9802 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9806 /* Consume the `='. */
9807 cp_lexer_consume_token (parser->lexer);
9808 /* Parse the id-expression. */
9809 push_deferring_access_checks (dk_no_deferred);
9810 /* save token before parsing the id-expression, for error
9812 token = cp_lexer_peek_token (parser->lexer);
9814 = cp_parser_id_expression (parser,
9815 /*template_keyword_p=*/false,
9816 /*check_dependency_p=*/true,
9817 /*template_p=*/&is_template,
9818 /*declarator_p=*/false,
9819 /*optional_p=*/false);
9820 if (TREE_CODE (default_argument) == TYPE_DECL)
9821 /* If the id-expression was a template-id that refers to
9822 a template-class, we already have the declaration here,
9823 so no further lookup is needed. */
9826 /* Look up the name. */
9828 = cp_parser_lookup_name (parser, default_argument,
9830 /*is_template=*/is_template,
9831 /*is_namespace=*/false,
9832 /*check_dependency=*/true,
9833 /*ambiguous_decls=*/NULL,
9835 /* See if the default argument is valid. */
9837 = check_template_template_default_arg (default_argument);
9839 /* Template parameter packs cannot have default
9841 if (*is_parameter_pack)
9844 error ("%Htemplate parameter pack %qD cannot "
9845 "have a default argument",
9846 &token->location, identifier);
9848 error ("%Htemplate parameter packs cannot "
9849 "have default arguments",
9851 default_argument = NULL_TREE;
9853 pop_deferring_access_checks ();
9856 default_argument = NULL_TREE;
9858 /* Create the combined representation of the parameter and the
9859 default argument. */
9860 parameter = build_tree_list (default_argument, parameter);
9872 /* Parse a template-id.
9875 template-name < template-argument-list [opt] >
9877 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9878 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9879 returned. Otherwise, if the template-name names a function, or set
9880 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9881 names a class, returns a TYPE_DECL for the specialization.
9883 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9884 uninstantiated templates. */
9887 cp_parser_template_id (cp_parser *parser,
9888 bool template_keyword_p,
9889 bool check_dependency_p,
9890 bool is_declaration)
9896 cp_token_position start_of_id = 0;
9897 deferred_access_check *chk;
9898 VEC (deferred_access_check,gc) *access_check;
9899 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9902 /* If the next token corresponds to a template-id, there is no need
9904 next_token = cp_lexer_peek_token (parser->lexer);
9905 if (next_token->type == CPP_TEMPLATE_ID)
9907 struct tree_check *check_value;
9909 /* Get the stored value. */
9910 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9911 /* Perform any access checks that were deferred. */
9912 access_check = check_value->checks;
9916 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9919 perform_or_defer_access_check (chk->binfo,
9924 /* Return the stored value. */
9925 return check_value->value;
9928 /* Avoid performing name lookup if there is no possibility of
9929 finding a template-id. */
9930 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9931 || (next_token->type == CPP_NAME
9932 && !cp_parser_nth_token_starts_template_argument_list_p
9935 cp_parser_error (parser, "expected template-id");
9936 return error_mark_node;
9939 /* Remember where the template-id starts. */
9940 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9941 start_of_id = cp_lexer_token_position (parser->lexer, false);
9943 push_deferring_access_checks (dk_deferred);
9945 /* Parse the template-name. */
9946 is_identifier = false;
9947 token = cp_lexer_peek_token (parser->lexer);
9948 templ = cp_parser_template_name (parser, template_keyword_p,
9952 if (templ == error_mark_node || is_identifier)
9954 pop_deferring_access_checks ();
9958 /* If we find the sequence `[:' after a template-name, it's probably
9959 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9960 parse correctly the argument list. */
9961 next_token = cp_lexer_peek_token (parser->lexer);
9962 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9963 if (next_token->type == CPP_OPEN_SQUARE
9964 && next_token->flags & DIGRAPH
9965 && next_token_2->type == CPP_COLON
9966 && !(next_token_2->flags & PREV_WHITE))
9968 cp_parser_parse_tentatively (parser);
9969 /* Change `:' into `::'. */
9970 next_token_2->type = CPP_SCOPE;
9971 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9973 cp_lexer_consume_token (parser->lexer);
9975 /* Parse the arguments. */
9976 arguments = cp_parser_enclosed_template_argument_list (parser);
9977 if (!cp_parser_parse_definitely (parser))
9979 /* If we couldn't parse an argument list, then we revert our changes
9980 and return simply an error. Maybe this is not a template-id
9982 next_token_2->type = CPP_COLON;
9983 cp_parser_error (parser, "expected %<<%>");
9984 pop_deferring_access_checks ();
9985 return error_mark_node;
9987 /* Otherwise, emit an error about the invalid digraph, but continue
9988 parsing because we got our argument list. */
9989 permerror ("%H%<<::%> cannot begin a template-argument list",
9990 &next_token->location);
9991 inform ("%H%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9992 "between %<<%> and %<::%>",
9993 &next_token->location);
9994 if (!flag_permissive)
9999 inform ("%H(if you use %<-fpermissive%> G++ will accept your code)",
10000 &next_token->location);
10007 /* Look for the `<' that starts the template-argument-list. */
10008 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10010 pop_deferring_access_checks ();
10011 return error_mark_node;
10013 /* Parse the arguments. */
10014 arguments = cp_parser_enclosed_template_argument_list (parser);
10017 /* Build a representation of the specialization. */
10018 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10019 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10020 else if (DECL_CLASS_TEMPLATE_P (templ)
10021 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10023 bool entering_scope;
10024 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10025 template (rather than some instantiation thereof) only if
10026 is not nested within some other construct. For example, in
10027 "template <typename T> void f(T) { A<T>::", A<T> is just an
10028 instantiation of A. */
10029 entering_scope = (template_parm_scope_p ()
10030 && cp_lexer_next_token_is (parser->lexer,
10033 = finish_template_type (templ, arguments, entering_scope);
10037 /* If it's not a class-template or a template-template, it should be
10038 a function-template. */
10039 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10040 || TREE_CODE (templ) == OVERLOAD
10041 || BASELINK_P (templ)));
10043 template_id = lookup_template_function (templ, arguments);
10046 /* If parsing tentatively, replace the sequence of tokens that makes
10047 up the template-id with a CPP_TEMPLATE_ID token. That way,
10048 should we re-parse the token stream, we will not have to repeat
10049 the effort required to do the parse, nor will we issue duplicate
10050 error messages about problems during instantiation of the
10054 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10056 /* Reset the contents of the START_OF_ID token. */
10057 token->type = CPP_TEMPLATE_ID;
10058 /* Retrieve any deferred checks. Do not pop this access checks yet
10059 so the memory will not be reclaimed during token replacing below. */
10060 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10061 token->u.tree_check_value->value = template_id;
10062 token->u.tree_check_value->checks = get_deferred_access_checks ();
10063 token->keyword = RID_MAX;
10065 /* Purge all subsequent tokens. */
10066 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10068 /* ??? Can we actually assume that, if template_id ==
10069 error_mark_node, we will have issued a diagnostic to the
10070 user, as opposed to simply marking the tentative parse as
10072 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10073 error ("%Hparse error in template argument list",
10077 pop_deferring_access_checks ();
10078 return template_id;
10081 /* Parse a template-name.
10086 The standard should actually say:
10090 operator-function-id
10092 A defect report has been filed about this issue.
10094 A conversion-function-id cannot be a template name because they cannot
10095 be part of a template-id. In fact, looking at this code:
10097 a.operator K<int>()
10099 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10100 It is impossible to call a templated conversion-function-id with an
10101 explicit argument list, since the only allowed template parameter is
10102 the type to which it is converting.
10104 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10105 `template' keyword, in a construction like:
10109 In that case `f' is taken to be a template-name, even though there
10110 is no way of knowing for sure.
10112 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10113 name refers to a set of overloaded functions, at least one of which
10114 is a template, or an IDENTIFIER_NODE with the name of the template,
10115 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10116 names are looked up inside uninstantiated templates. */
10119 cp_parser_template_name (cp_parser* parser,
10120 bool template_keyword_p,
10121 bool check_dependency_p,
10122 bool is_declaration,
10123 bool *is_identifier)
10128 cp_token *token = cp_lexer_peek_token (parser->lexer);
10130 /* If the next token is `operator', then we have either an
10131 operator-function-id or a conversion-function-id. */
10132 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10134 /* We don't know whether we're looking at an
10135 operator-function-id or a conversion-function-id. */
10136 cp_parser_parse_tentatively (parser);
10137 /* Try an operator-function-id. */
10138 identifier = cp_parser_operator_function_id (parser);
10139 /* If that didn't work, try a conversion-function-id. */
10140 if (!cp_parser_parse_definitely (parser))
10142 cp_parser_error (parser, "expected template-name");
10143 return error_mark_node;
10146 /* Look for the identifier. */
10148 identifier = cp_parser_identifier (parser);
10150 /* If we didn't find an identifier, we don't have a template-id. */
10151 if (identifier == error_mark_node)
10152 return error_mark_node;
10154 /* If the name immediately followed the `template' keyword, then it
10155 is a template-name. However, if the next token is not `<', then
10156 we do not treat it as a template-name, since it is not being used
10157 as part of a template-id. This enables us to handle constructs
10160 template <typename T> struct S { S(); };
10161 template <typename T> S<T>::S();
10163 correctly. We would treat `S' as a template -- if it were `S<T>'
10164 -- but we do not if there is no `<'. */
10166 if (processing_template_decl
10167 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10169 /* In a declaration, in a dependent context, we pretend that the
10170 "template" keyword was present in order to improve error
10171 recovery. For example, given:
10173 template <typename T> void f(T::X<int>);
10175 we want to treat "X<int>" as a template-id. */
10177 && !template_keyword_p
10178 && parser->scope && TYPE_P (parser->scope)
10179 && check_dependency_p
10180 && dependent_type_p (parser->scope)
10181 /* Do not do this for dtors (or ctors), since they never
10182 need the template keyword before their name. */
10183 && !constructor_name_p (identifier, parser->scope))
10185 cp_token_position start = 0;
10187 /* Explain what went wrong. */
10188 error ("%Hnon-template %qD used as template",
10189 &token->location, identifier);
10190 inform ("use %<%T::template %D%> to indicate that it is a template",
10191 parser->scope, identifier);
10192 /* If parsing tentatively, find the location of the "<" token. */
10193 if (cp_parser_simulate_error (parser))
10194 start = cp_lexer_token_position (parser->lexer, true);
10195 /* Parse the template arguments so that we can issue error
10196 messages about them. */
10197 cp_lexer_consume_token (parser->lexer);
10198 cp_parser_enclosed_template_argument_list (parser);
10199 /* Skip tokens until we find a good place from which to
10200 continue parsing. */
10201 cp_parser_skip_to_closing_parenthesis (parser,
10202 /*recovering=*/true,
10204 /*consume_paren=*/false);
10205 /* If parsing tentatively, permanently remove the
10206 template argument list. That will prevent duplicate
10207 error messages from being issued about the missing
10208 "template" keyword. */
10210 cp_lexer_purge_tokens_after (parser->lexer, start);
10212 *is_identifier = true;
10216 /* If the "template" keyword is present, then there is generally
10217 no point in doing name-lookup, so we just return IDENTIFIER.
10218 But, if the qualifying scope is non-dependent then we can
10219 (and must) do name-lookup normally. */
10220 if (template_keyword_p
10222 || (TYPE_P (parser->scope)
10223 && dependent_type_p (parser->scope))))
10227 /* Look up the name. */
10228 decl = cp_parser_lookup_name (parser, identifier,
10230 /*is_template=*/false,
10231 /*is_namespace=*/false,
10232 check_dependency_p,
10233 /*ambiguous_decls=*/NULL,
10235 decl = maybe_get_template_decl_from_type_decl (decl);
10237 /* If DECL is a template, then the name was a template-name. */
10238 if (TREE_CODE (decl) == TEMPLATE_DECL)
10242 tree fn = NULL_TREE;
10244 /* The standard does not explicitly indicate whether a name that
10245 names a set of overloaded declarations, some of which are
10246 templates, is a template-name. However, such a name should
10247 be a template-name; otherwise, there is no way to form a
10248 template-id for the overloaded templates. */
10249 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10250 if (TREE_CODE (fns) == OVERLOAD)
10251 for (fn = fns; fn; fn = OVL_NEXT (fn))
10252 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10257 /* The name does not name a template. */
10258 cp_parser_error (parser, "expected template-name");
10259 return error_mark_node;
10263 /* If DECL is dependent, and refers to a function, then just return
10264 its name; we will look it up again during template instantiation. */
10265 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10267 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10268 if (TYPE_P (scope) && dependent_type_p (scope))
10275 /* Parse a template-argument-list.
10277 template-argument-list:
10278 template-argument ... [opt]
10279 template-argument-list , template-argument ... [opt]
10281 Returns a TREE_VEC containing the arguments. */
10284 cp_parser_template_argument_list (cp_parser* parser)
10286 tree fixed_args[10];
10287 unsigned n_args = 0;
10288 unsigned alloced = 10;
10289 tree *arg_ary = fixed_args;
10291 bool saved_in_template_argument_list_p;
10293 bool saved_non_ice_p;
10295 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10296 parser->in_template_argument_list_p = true;
10297 /* Even if the template-id appears in an integral
10298 constant-expression, the contents of the argument list do
10300 saved_ice_p = parser->integral_constant_expression_p;
10301 parser->integral_constant_expression_p = false;
10302 saved_non_ice_p = parser->non_integral_constant_expression_p;
10303 parser->non_integral_constant_expression_p = false;
10304 /* Parse the arguments. */
10310 /* Consume the comma. */
10311 cp_lexer_consume_token (parser->lexer);
10313 /* Parse the template-argument. */
10314 argument = cp_parser_template_argument (parser);
10316 /* If the next token is an ellipsis, we're expanding a template
10318 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10320 /* Consume the `...' token. */
10321 cp_lexer_consume_token (parser->lexer);
10323 /* Make the argument into a TYPE_PACK_EXPANSION or
10324 EXPR_PACK_EXPANSION. */
10325 argument = make_pack_expansion (argument);
10328 if (n_args == alloced)
10332 if (arg_ary == fixed_args)
10334 arg_ary = XNEWVEC (tree, alloced);
10335 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10338 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10340 arg_ary[n_args++] = argument;
10342 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10344 vec = make_tree_vec (n_args);
10347 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10349 if (arg_ary != fixed_args)
10351 parser->non_integral_constant_expression_p = saved_non_ice_p;
10352 parser->integral_constant_expression_p = saved_ice_p;
10353 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10357 /* Parse a template-argument.
10360 assignment-expression
10364 The representation is that of an assignment-expression, type-id, or
10365 id-expression -- except that the qualified id-expression is
10366 evaluated, so that the value returned is either a DECL or an
10369 Although the standard says "assignment-expression", it forbids
10370 throw-expressions or assignments in the template argument.
10371 Therefore, we use "conditional-expression" instead. */
10374 cp_parser_template_argument (cp_parser* parser)
10379 bool maybe_type_id = false;
10380 cp_token *token = NULL, *argument_start_token = NULL;
10383 /* There's really no way to know what we're looking at, so we just
10384 try each alternative in order.
10388 In a template-argument, an ambiguity between a type-id and an
10389 expression is resolved to a type-id, regardless of the form of
10390 the corresponding template-parameter.
10392 Therefore, we try a type-id first. */
10393 cp_parser_parse_tentatively (parser);
10394 argument = cp_parser_type_id (parser);
10395 /* If there was no error parsing the type-id but the next token is a '>>',
10396 we probably found a typo for '> >'. But there are type-id which are
10397 also valid expressions. For instance:
10399 struct X { int operator >> (int); };
10400 template <int V> struct Foo {};
10403 Here 'X()' is a valid type-id of a function type, but the user just
10404 wanted to write the expression "X() >> 5". Thus, we remember that we
10405 found a valid type-id, but we still try to parse the argument as an
10406 expression to see what happens. */
10407 if (!cp_parser_error_occurred (parser)
10408 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10410 maybe_type_id = true;
10411 cp_parser_abort_tentative_parse (parser);
10415 /* If the next token isn't a `,' or a `>', then this argument wasn't
10416 really finished. This means that the argument is not a valid
10418 if (!cp_parser_next_token_ends_template_argument_p (parser))
10419 cp_parser_error (parser, "expected template-argument");
10420 /* If that worked, we're done. */
10421 if (cp_parser_parse_definitely (parser))
10424 /* We're still not sure what the argument will be. */
10425 cp_parser_parse_tentatively (parser);
10426 /* Try a template. */
10427 argument_start_token = cp_lexer_peek_token (parser->lexer);
10428 argument = cp_parser_id_expression (parser,
10429 /*template_keyword_p=*/false,
10430 /*check_dependency_p=*/true,
10432 /*declarator_p=*/false,
10433 /*optional_p=*/false);
10434 /* If the next token isn't a `,' or a `>', then this argument wasn't
10435 really finished. */
10436 if (!cp_parser_next_token_ends_template_argument_p (parser))
10437 cp_parser_error (parser, "expected template-argument");
10438 if (!cp_parser_error_occurred (parser))
10440 /* Figure out what is being referred to. If the id-expression
10441 was for a class template specialization, then we will have a
10442 TYPE_DECL at this point. There is no need to do name lookup
10443 at this point in that case. */
10444 if (TREE_CODE (argument) != TYPE_DECL)
10445 argument = cp_parser_lookup_name (parser, argument,
10447 /*is_template=*/template_p,
10448 /*is_namespace=*/false,
10449 /*check_dependency=*/true,
10450 /*ambiguous_decls=*/NULL,
10451 argument_start_token->location);
10452 if (TREE_CODE (argument) != TEMPLATE_DECL
10453 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10454 cp_parser_error (parser, "expected template-name");
10456 if (cp_parser_parse_definitely (parser))
10458 /* It must be a non-type argument. There permitted cases are given
10459 in [temp.arg.nontype]:
10461 -- an integral constant-expression of integral or enumeration
10464 -- the name of a non-type template-parameter; or
10466 -- the name of an object or function with external linkage...
10468 -- the address of an object or function with external linkage...
10470 -- a pointer to member... */
10471 /* Look for a non-type template parameter. */
10472 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10474 cp_parser_parse_tentatively (parser);
10475 argument = cp_parser_primary_expression (parser,
10476 /*adress_p=*/false,
10478 /*template_arg_p=*/true,
10480 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10481 || !cp_parser_next_token_ends_template_argument_p (parser))
10482 cp_parser_simulate_error (parser);
10483 if (cp_parser_parse_definitely (parser))
10487 /* If the next token is "&", the argument must be the address of an
10488 object or function with external linkage. */
10489 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10491 cp_lexer_consume_token (parser->lexer);
10492 /* See if we might have an id-expression. */
10493 token = cp_lexer_peek_token (parser->lexer);
10494 if (token->type == CPP_NAME
10495 || token->keyword == RID_OPERATOR
10496 || token->type == CPP_SCOPE
10497 || token->type == CPP_TEMPLATE_ID
10498 || token->type == CPP_NESTED_NAME_SPECIFIER)
10500 cp_parser_parse_tentatively (parser);
10501 argument = cp_parser_primary_expression (parser,
10504 /*template_arg_p=*/true,
10506 if (cp_parser_error_occurred (parser)
10507 || !cp_parser_next_token_ends_template_argument_p (parser))
10508 cp_parser_abort_tentative_parse (parser);
10511 if (TREE_CODE (argument) == INDIRECT_REF)
10513 gcc_assert (REFERENCE_REF_P (argument));
10514 argument = TREE_OPERAND (argument, 0);
10517 if (TREE_CODE (argument) == VAR_DECL)
10519 /* A variable without external linkage might still be a
10520 valid constant-expression, so no error is issued here
10521 if the external-linkage check fails. */
10522 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10523 cp_parser_simulate_error (parser);
10525 else if (is_overloaded_fn (argument))
10526 /* All overloaded functions are allowed; if the external
10527 linkage test does not pass, an error will be issued
10531 && (TREE_CODE (argument) == OFFSET_REF
10532 || TREE_CODE (argument) == SCOPE_REF))
10533 /* A pointer-to-member. */
10535 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10538 cp_parser_simulate_error (parser);
10540 if (cp_parser_parse_definitely (parser))
10543 argument = build_x_unary_op (ADDR_EXPR, argument,
10544 tf_warning_or_error);
10549 /* If the argument started with "&", there are no other valid
10550 alternatives at this point. */
10553 cp_parser_error (parser, "invalid non-type template argument");
10554 return error_mark_node;
10557 /* If the argument wasn't successfully parsed as a type-id followed
10558 by '>>', the argument can only be a constant expression now.
10559 Otherwise, we try parsing the constant-expression tentatively,
10560 because the argument could really be a type-id. */
10562 cp_parser_parse_tentatively (parser);
10563 argument = cp_parser_constant_expression (parser,
10564 /*allow_non_constant_p=*/false,
10565 /*non_constant_p=*/NULL);
10566 argument = fold_non_dependent_expr (argument);
10567 if (!maybe_type_id)
10569 if (!cp_parser_next_token_ends_template_argument_p (parser))
10570 cp_parser_error (parser, "expected template-argument");
10571 if (cp_parser_parse_definitely (parser))
10573 /* We did our best to parse the argument as a non type-id, but that
10574 was the only alternative that matched (albeit with a '>' after
10575 it). We can assume it's just a typo from the user, and a
10576 diagnostic will then be issued. */
10577 return cp_parser_type_id (parser);
10580 /* Parse an explicit-instantiation.
10582 explicit-instantiation:
10583 template declaration
10585 Although the standard says `declaration', what it really means is:
10587 explicit-instantiation:
10588 template decl-specifier-seq [opt] declarator [opt] ;
10590 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10591 supposed to be allowed. A defect report has been filed about this
10596 explicit-instantiation:
10597 storage-class-specifier template
10598 decl-specifier-seq [opt] declarator [opt] ;
10599 function-specifier template
10600 decl-specifier-seq [opt] declarator [opt] ; */
10603 cp_parser_explicit_instantiation (cp_parser* parser)
10605 int declares_class_or_enum;
10606 cp_decl_specifier_seq decl_specifiers;
10607 tree extension_specifier = NULL_TREE;
10610 /* Look for an (optional) storage-class-specifier or
10611 function-specifier. */
10612 if (cp_parser_allow_gnu_extensions_p (parser))
10614 extension_specifier
10615 = cp_parser_storage_class_specifier_opt (parser);
10616 if (!extension_specifier)
10617 extension_specifier
10618 = cp_parser_function_specifier_opt (parser,
10619 /*decl_specs=*/NULL);
10622 /* Look for the `template' keyword. */
10623 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10624 /* Let the front end know that we are processing an explicit
10626 begin_explicit_instantiation ();
10627 /* [temp.explicit] says that we are supposed to ignore access
10628 control while processing explicit instantiation directives. */
10629 push_deferring_access_checks (dk_no_check);
10630 /* Parse a decl-specifier-seq. */
10631 token = cp_lexer_peek_token (parser->lexer);
10632 cp_parser_decl_specifier_seq (parser,
10633 CP_PARSER_FLAGS_OPTIONAL,
10635 &declares_class_or_enum);
10636 /* If there was exactly one decl-specifier, and it declared a class,
10637 and there's no declarator, then we have an explicit type
10639 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10643 type = check_tag_decl (&decl_specifiers);
10644 /* Turn access control back on for names used during
10645 template instantiation. */
10646 pop_deferring_access_checks ();
10648 do_type_instantiation (type, extension_specifier,
10649 /*complain=*/tf_error);
10653 cp_declarator *declarator;
10656 /* Parse the declarator. */
10658 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10659 /*ctor_dtor_or_conv_p=*/NULL,
10660 /*parenthesized_p=*/NULL,
10661 /*member_p=*/false);
10662 if (declares_class_or_enum & 2)
10663 cp_parser_check_for_definition_in_return_type (declarator,
10664 decl_specifiers.type,
10665 decl_specifiers.type_location);
10666 if (declarator != cp_error_declarator)
10668 decl = grokdeclarator (declarator, &decl_specifiers,
10669 NORMAL, 0, &decl_specifiers.attributes);
10670 /* Turn access control back on for names used during
10671 template instantiation. */
10672 pop_deferring_access_checks ();
10673 /* Do the explicit instantiation. */
10674 do_decl_instantiation (decl, extension_specifier);
10678 pop_deferring_access_checks ();
10679 /* Skip the body of the explicit instantiation. */
10680 cp_parser_skip_to_end_of_statement (parser);
10683 /* We're done with the instantiation. */
10684 end_explicit_instantiation ();
10686 cp_parser_consume_semicolon_at_end_of_statement (parser);
10689 /* Parse an explicit-specialization.
10691 explicit-specialization:
10692 template < > declaration
10694 Although the standard says `declaration', what it really means is:
10696 explicit-specialization:
10697 template <> decl-specifier [opt] init-declarator [opt] ;
10698 template <> function-definition
10699 template <> explicit-specialization
10700 template <> template-declaration */
10703 cp_parser_explicit_specialization (cp_parser* parser)
10705 bool need_lang_pop;
10706 cp_token *token = cp_lexer_peek_token (parser->lexer);
10708 /* Look for the `template' keyword. */
10709 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10710 /* Look for the `<'. */
10711 cp_parser_require (parser, CPP_LESS, "%<<%>");
10712 /* Look for the `>'. */
10713 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10714 /* We have processed another parameter list. */
10715 ++parser->num_template_parameter_lists;
10718 A template ... explicit specialization ... shall not have C
10720 if (current_lang_name == lang_name_c)
10722 error ("%Htemplate specialization with C linkage", &token->location);
10723 /* Give it C++ linkage to avoid confusing other parts of the
10725 push_lang_context (lang_name_cplusplus);
10726 need_lang_pop = true;
10729 need_lang_pop = false;
10730 /* Let the front end know that we are beginning a specialization. */
10731 if (!begin_specialization ())
10733 end_specialization ();
10734 cp_parser_skip_to_end_of_block_or_statement (parser);
10738 /* If the next keyword is `template', we need to figure out whether
10739 or not we're looking a template-declaration. */
10740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10742 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10743 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10744 cp_parser_template_declaration_after_export (parser,
10745 /*member_p=*/false);
10747 cp_parser_explicit_specialization (parser);
10750 /* Parse the dependent declaration. */
10751 cp_parser_single_declaration (parser,
10753 /*member_p=*/false,
10754 /*explicit_specialization_p=*/true,
10755 /*friend_p=*/NULL);
10756 /* We're done with the specialization. */
10757 end_specialization ();
10758 /* For the erroneous case of a template with C linkage, we pushed an
10759 implicit C++ linkage scope; exit that scope now. */
10761 pop_lang_context ();
10762 /* We're done with this parameter list. */
10763 --parser->num_template_parameter_lists;
10766 /* Parse a type-specifier.
10769 simple-type-specifier
10772 elaborated-type-specifier
10780 Returns a representation of the type-specifier. For a
10781 class-specifier, enum-specifier, or elaborated-type-specifier, a
10782 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10784 The parser flags FLAGS is used to control type-specifier parsing.
10786 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10787 in a decl-specifier-seq.
10789 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10790 class-specifier, enum-specifier, or elaborated-type-specifier, then
10791 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10792 if a type is declared; 2 if it is defined. Otherwise, it is set to
10795 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10796 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10797 is set to FALSE. */
10800 cp_parser_type_specifier (cp_parser* parser,
10801 cp_parser_flags flags,
10802 cp_decl_specifier_seq *decl_specs,
10803 bool is_declaration,
10804 int* declares_class_or_enum,
10805 bool* is_cv_qualifier)
10807 tree type_spec = NULL_TREE;
10810 cp_decl_spec ds = ds_last;
10812 /* Assume this type-specifier does not declare a new type. */
10813 if (declares_class_or_enum)
10814 *declares_class_or_enum = 0;
10815 /* And that it does not specify a cv-qualifier. */
10816 if (is_cv_qualifier)
10817 *is_cv_qualifier = false;
10818 /* Peek at the next token. */
10819 token = cp_lexer_peek_token (parser->lexer);
10821 /* If we're looking at a keyword, we can use that to guide the
10822 production we choose. */
10823 keyword = token->keyword;
10827 /* Look for the enum-specifier. */
10828 type_spec = cp_parser_enum_specifier (parser);
10829 /* If that worked, we're done. */
10832 if (declares_class_or_enum)
10833 *declares_class_or_enum = 2;
10835 cp_parser_set_decl_spec_type (decl_specs,
10838 /*user_defined_p=*/true);
10842 goto elaborated_type_specifier;
10844 /* Any of these indicate either a class-specifier, or an
10845 elaborated-type-specifier. */
10849 /* Parse tentatively so that we can back up if we don't find a
10850 class-specifier. */
10851 cp_parser_parse_tentatively (parser);
10852 /* Look for the class-specifier. */
10853 type_spec = cp_parser_class_specifier (parser);
10854 /* If that worked, we're done. */
10855 if (cp_parser_parse_definitely (parser))
10857 if (declares_class_or_enum)
10858 *declares_class_or_enum = 2;
10860 cp_parser_set_decl_spec_type (decl_specs,
10863 /*user_defined_p=*/true);
10867 /* Fall through. */
10868 elaborated_type_specifier:
10869 /* We're declaring (not defining) a class or enum. */
10870 if (declares_class_or_enum)
10871 *declares_class_or_enum = 1;
10873 /* Fall through. */
10875 /* Look for an elaborated-type-specifier. */
10877 = (cp_parser_elaborated_type_specifier
10879 decl_specs && decl_specs->specs[(int) ds_friend],
10882 cp_parser_set_decl_spec_type (decl_specs,
10885 /*user_defined_p=*/true);
10890 if (is_cv_qualifier)
10891 *is_cv_qualifier = true;
10896 if (is_cv_qualifier)
10897 *is_cv_qualifier = true;
10902 if (is_cv_qualifier)
10903 *is_cv_qualifier = true;
10907 /* The `__complex__' keyword is a GNU extension. */
10915 /* Handle simple keywords. */
10920 ++decl_specs->specs[(int)ds];
10921 decl_specs->any_specifiers_p = true;
10923 return cp_lexer_consume_token (parser->lexer)->u.value;
10926 /* If we do not already have a type-specifier, assume we are looking
10927 at a simple-type-specifier. */
10928 type_spec = cp_parser_simple_type_specifier (parser,
10932 /* If we didn't find a type-specifier, and a type-specifier was not
10933 optional in this context, issue an error message. */
10934 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10936 cp_parser_error (parser, "expected type specifier");
10937 return error_mark_node;
10943 /* Parse a simple-type-specifier.
10945 simple-type-specifier:
10946 :: [opt] nested-name-specifier [opt] type-name
10947 :: [opt] nested-name-specifier template template-id
10962 simple-type-specifier:
10964 decltype ( expression )
10970 simple-type-specifier:
10971 __typeof__ unary-expression
10972 __typeof__ ( type-id )
10974 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10975 appropriately updated. */
10978 cp_parser_simple_type_specifier (cp_parser* parser,
10979 cp_decl_specifier_seq *decl_specs,
10980 cp_parser_flags flags)
10982 tree type = NULL_TREE;
10985 /* Peek at the next token. */
10986 token = cp_lexer_peek_token (parser->lexer);
10988 /* If we're looking at a keyword, things are easy. */
10989 switch (token->keyword)
10993 decl_specs->explicit_char_p = true;
10994 type = char_type_node;
10997 type = char16_type_node;
11000 type = char32_type_node;
11003 type = wchar_type_node;
11006 type = boolean_type_node;
11010 ++decl_specs->specs[(int) ds_short];
11011 type = short_integer_type_node;
11015 decl_specs->explicit_int_p = true;
11016 type = integer_type_node;
11020 ++decl_specs->specs[(int) ds_long];
11021 type = long_integer_type_node;
11025 ++decl_specs->specs[(int) ds_signed];
11026 type = integer_type_node;
11030 ++decl_specs->specs[(int) ds_unsigned];
11031 type = unsigned_type_node;
11034 type = float_type_node;
11037 type = double_type_node;
11040 type = void_type_node;
11044 if (cxx_dialect != cxx98)
11046 /* Consume the token. */
11047 cp_lexer_consume_token (parser->lexer);
11048 /* We do not yet support the use of `auto' as a
11050 error ("%HC++0x %<auto%> specifier not supported", &token->location);
11055 /* Parse the `decltype' type. */
11056 type = cp_parser_decltype (parser);
11059 cp_parser_set_decl_spec_type (decl_specs, type,
11061 /*user_defined_p=*/true);
11066 /* Consume the `typeof' token. */
11067 cp_lexer_consume_token (parser->lexer);
11068 /* Parse the operand to `typeof'. */
11069 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11070 /* If it is not already a TYPE, take its type. */
11071 if (!TYPE_P (type))
11072 type = finish_typeof (type);
11075 cp_parser_set_decl_spec_type (decl_specs, type,
11077 /*user_defined_p=*/true);
11085 /* If the type-specifier was for a built-in type, we're done. */
11090 /* Record the type. */
11092 && (token->keyword != RID_SIGNED
11093 && token->keyword != RID_UNSIGNED
11094 && token->keyword != RID_SHORT
11095 && token->keyword != RID_LONG))
11096 cp_parser_set_decl_spec_type (decl_specs,
11099 /*user_defined=*/false);
11101 decl_specs->any_specifiers_p = true;
11103 /* Consume the token. */
11104 id = cp_lexer_consume_token (parser->lexer)->u.value;
11106 /* There is no valid C++ program where a non-template type is
11107 followed by a "<". That usually indicates that the user thought
11108 that the type was a template. */
11109 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11111 return TYPE_NAME (type);
11114 /* The type-specifier must be a user-defined type. */
11115 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11120 /* Don't gobble tokens or issue error messages if this is an
11121 optional type-specifier. */
11122 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11123 cp_parser_parse_tentatively (parser);
11125 /* Look for the optional `::' operator. */
11127 = (cp_parser_global_scope_opt (parser,
11128 /*current_scope_valid_p=*/false)
11130 /* Look for the nested-name specifier. */
11132 = (cp_parser_nested_name_specifier_opt (parser,
11133 /*typename_keyword_p=*/false,
11134 /*check_dependency_p=*/true,
11136 /*is_declaration=*/false)
11138 token = cp_lexer_peek_token (parser->lexer);
11139 /* If we have seen a nested-name-specifier, and the next token
11140 is `template', then we are using the template-id production. */
11142 && cp_parser_optional_template_keyword (parser))
11144 /* Look for the template-id. */
11145 type = cp_parser_template_id (parser,
11146 /*template_keyword_p=*/true,
11147 /*check_dependency_p=*/true,
11148 /*is_declaration=*/false);
11149 /* If the template-id did not name a type, we are out of
11151 if (TREE_CODE (type) != TYPE_DECL)
11153 cp_parser_error (parser, "expected template-id for type");
11157 /* Otherwise, look for a type-name. */
11159 type = cp_parser_type_name (parser);
11160 /* Keep track of all name-lookups performed in class scopes. */
11164 && TREE_CODE (type) == TYPE_DECL
11165 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11166 maybe_note_name_used_in_class (DECL_NAME (type), type);
11167 /* If it didn't work out, we don't have a TYPE. */
11168 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11169 && !cp_parser_parse_definitely (parser))
11171 if (type && decl_specs)
11172 cp_parser_set_decl_spec_type (decl_specs, type,
11174 /*user_defined=*/true);
11177 /* If we didn't get a type-name, issue an error message. */
11178 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11180 cp_parser_error (parser, "expected type-name");
11181 return error_mark_node;
11184 /* There is no valid C++ program where a non-template type is
11185 followed by a "<". That usually indicates that the user thought
11186 that the type was a template. */
11187 if (type && type != error_mark_node)
11189 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11190 If it is, then the '<'...'>' enclose protocol names rather than
11191 template arguments, and so everything is fine. */
11192 if (c_dialect_objc ()
11193 && (objc_is_id (type) || objc_is_class_name (type)))
11195 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11196 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11198 /* Clobber the "unqualified" type previously entered into
11199 DECL_SPECS with the new, improved protocol-qualified version. */
11201 decl_specs->type = qual_type;
11206 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11213 /* Parse a type-name.
11226 Returns a TYPE_DECL for the type. */
11229 cp_parser_type_name (cp_parser* parser)
11233 /* We can't know yet whether it is a class-name or not. */
11234 cp_parser_parse_tentatively (parser);
11235 /* Try a class-name. */
11236 type_decl = cp_parser_class_name (parser,
11237 /*typename_keyword_p=*/false,
11238 /*template_keyword_p=*/false,
11240 /*check_dependency_p=*/true,
11241 /*class_head_p=*/false,
11242 /*is_declaration=*/false);
11243 /* If it's not a class-name, keep looking. */
11244 if (!cp_parser_parse_definitely (parser))
11246 /* It must be a typedef-name or an enum-name. */
11247 return cp_parser_nonclass_name (parser);
11253 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11261 Returns a TYPE_DECL for the type. */
11264 cp_parser_nonclass_name (cp_parser* parser)
11269 cp_token *token = cp_lexer_peek_token (parser->lexer);
11270 identifier = cp_parser_identifier (parser);
11271 if (identifier == error_mark_node)
11272 return error_mark_node;
11274 /* Look up the type-name. */
11275 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11277 if (TREE_CODE (type_decl) != TYPE_DECL
11278 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11280 /* See if this is an Objective-C type. */
11281 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11282 tree type = objc_get_protocol_qualified_type (identifier, protos);
11284 type_decl = TYPE_NAME (type);
11287 /* Issue an error if we did not find a type-name. */
11288 if (TREE_CODE (type_decl) != TYPE_DECL)
11290 if (!cp_parser_simulate_error (parser))
11291 cp_parser_name_lookup_error (parser, identifier, type_decl,
11292 "is not a type", token->location);
11293 return error_mark_node;
11295 /* Remember that the name was used in the definition of the
11296 current class so that we can check later to see if the
11297 meaning would have been different after the class was
11298 entirely defined. */
11299 else if (type_decl != error_mark_node
11301 maybe_note_name_used_in_class (identifier, type_decl);
11306 /* Parse an elaborated-type-specifier. Note that the grammar given
11307 here incorporates the resolution to DR68.
11309 elaborated-type-specifier:
11310 class-key :: [opt] nested-name-specifier [opt] identifier
11311 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11312 enum :: [opt] nested-name-specifier [opt] identifier
11313 typename :: [opt] nested-name-specifier identifier
11314 typename :: [opt] nested-name-specifier template [opt]
11319 elaborated-type-specifier:
11320 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11321 class-key attributes :: [opt] nested-name-specifier [opt]
11322 template [opt] template-id
11323 enum attributes :: [opt] nested-name-specifier [opt] identifier
11325 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11326 declared `friend'. If IS_DECLARATION is TRUE, then this
11327 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11328 something is being declared.
11330 Returns the TYPE specified. */
11333 cp_parser_elaborated_type_specifier (cp_parser* parser,
11335 bool is_declaration)
11337 enum tag_types tag_type;
11339 tree type = NULL_TREE;
11340 tree attributes = NULL_TREE;
11341 cp_token *token = NULL;
11343 /* See if we're looking at the `enum' keyword. */
11344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11346 /* Consume the `enum' token. */
11347 cp_lexer_consume_token (parser->lexer);
11348 /* Remember that it's an enumeration type. */
11349 tag_type = enum_type;
11350 /* Parse the attributes. */
11351 attributes = cp_parser_attributes_opt (parser);
11353 /* Or, it might be `typename'. */
11354 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11357 /* Consume the `typename' token. */
11358 cp_lexer_consume_token (parser->lexer);
11359 /* Remember that it's a `typename' type. */
11360 tag_type = typename_type;
11361 /* The `typename' keyword is only allowed in templates. */
11362 if (!processing_template_decl)
11363 permerror ("using %<typename%> outside of template");
11365 /* Otherwise it must be a class-key. */
11368 tag_type = cp_parser_class_key (parser);
11369 if (tag_type == none_type)
11370 return error_mark_node;
11371 /* Parse the attributes. */
11372 attributes = cp_parser_attributes_opt (parser);
11375 /* Look for the `::' operator. */
11376 cp_parser_global_scope_opt (parser,
11377 /*current_scope_valid_p=*/false);
11378 /* Look for the nested-name-specifier. */
11379 if (tag_type == typename_type)
11381 if (!cp_parser_nested_name_specifier (parser,
11382 /*typename_keyword_p=*/true,
11383 /*check_dependency_p=*/true,
11386 return error_mark_node;
11389 /* Even though `typename' is not present, the proposed resolution
11390 to Core Issue 180 says that in `class A<T>::B', `B' should be
11391 considered a type-name, even if `A<T>' is dependent. */
11392 cp_parser_nested_name_specifier_opt (parser,
11393 /*typename_keyword_p=*/true,
11394 /*check_dependency_p=*/true,
11397 /* For everything but enumeration types, consider a template-id.
11398 For an enumeration type, consider only a plain identifier. */
11399 if (tag_type != enum_type)
11401 bool template_p = false;
11404 /* Allow the `template' keyword. */
11405 template_p = cp_parser_optional_template_keyword (parser);
11406 /* If we didn't see `template', we don't know if there's a
11407 template-id or not. */
11409 cp_parser_parse_tentatively (parser);
11410 /* Parse the template-id. */
11411 token = cp_lexer_peek_token (parser->lexer);
11412 decl = cp_parser_template_id (parser, template_p,
11413 /*check_dependency_p=*/true,
11415 /* If we didn't find a template-id, look for an ordinary
11417 if (!template_p && !cp_parser_parse_definitely (parser))
11419 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11420 in effect, then we must assume that, upon instantiation, the
11421 template will correspond to a class. */
11422 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11423 && tag_type == typename_type)
11424 type = make_typename_type (parser->scope, decl,
11426 /*complain=*/tf_error);
11428 type = TREE_TYPE (decl);
11433 token = cp_lexer_peek_token (parser->lexer);
11434 identifier = cp_parser_identifier (parser);
11436 if (identifier == error_mark_node)
11438 parser->scope = NULL_TREE;
11439 return error_mark_node;
11442 /* For a `typename', we needn't call xref_tag. */
11443 if (tag_type == typename_type
11444 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11445 return cp_parser_make_typename_type (parser, parser->scope,
11448 /* Look up a qualified name in the usual way. */
11452 tree ambiguous_decls;
11454 decl = cp_parser_lookup_name (parser, identifier,
11456 /*is_template=*/false,
11457 /*is_namespace=*/false,
11458 /*check_dependency=*/true,
11462 /* If the lookup was ambiguous, an error will already have been
11464 if (ambiguous_decls)
11465 return error_mark_node;
11467 /* If we are parsing friend declaration, DECL may be a
11468 TEMPLATE_DECL tree node here. However, we need to check
11469 whether this TEMPLATE_DECL results in valid code. Consider
11470 the following example:
11473 template <class T> class C {};
11476 template <class T> friend class N::C; // #1, valid code
11478 template <class T> class Y {
11479 friend class N::C; // #2, invalid code
11482 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11483 name lookup of `N::C'. We see that friend declaration must
11484 be template for the code to be valid. Note that
11485 processing_template_decl does not work here since it is
11486 always 1 for the above two cases. */
11488 decl = (cp_parser_maybe_treat_template_as_class
11489 (decl, /*tag_name_p=*/is_friend
11490 && parser->num_template_parameter_lists));
11492 if (TREE_CODE (decl) != TYPE_DECL)
11494 cp_parser_diagnose_invalid_type_name (parser,
11498 return error_mark_node;
11501 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11503 bool allow_template = (parser->num_template_parameter_lists
11504 || DECL_SELF_REFERENCE_P (decl));
11505 type = check_elaborated_type_specifier (tag_type, decl,
11508 if (type == error_mark_node)
11509 return error_mark_node;
11512 /* Forward declarations of nested types, such as
11517 are invalid unless all components preceding the final '::'
11518 are complete. If all enclosing types are complete, these
11519 declarations become merely pointless.
11521 Invalid forward declarations of nested types are errors
11522 caught elsewhere in parsing. Those that are pointless arrive
11525 if (cp_parser_declares_only_class_p (parser)
11526 && !is_friend && !processing_explicit_instantiation)
11527 warning (0, "declaration %qD does not declare anything", decl);
11529 type = TREE_TYPE (decl);
11533 /* An elaborated-type-specifier sometimes introduces a new type and
11534 sometimes names an existing type. Normally, the rule is that it
11535 introduces a new type only if there is not an existing type of
11536 the same name already in scope. For example, given:
11539 void f() { struct S s; }
11541 the `struct S' in the body of `f' is the same `struct S' as in
11542 the global scope; the existing definition is used. However, if
11543 there were no global declaration, this would introduce a new
11544 local class named `S'.
11546 An exception to this rule applies to the following code:
11548 namespace N { struct S; }
11550 Here, the elaborated-type-specifier names a new type
11551 unconditionally; even if there is already an `S' in the
11552 containing scope this declaration names a new type.
11553 This exception only applies if the elaborated-type-specifier
11554 forms the complete declaration:
11558 A declaration consisting solely of `class-key identifier ;' is
11559 either a redeclaration of the name in the current scope or a
11560 forward declaration of the identifier as a class name. It
11561 introduces the name into the current scope.
11563 We are in this situation precisely when the next token is a `;'.
11565 An exception to the exception is that a `friend' declaration does
11566 *not* name a new type; i.e., given:
11568 struct S { friend struct T; };
11570 `T' is not a new type in the scope of `S'.
11572 Also, `new struct S' or `sizeof (struct S)' never results in the
11573 definition of a new type; a new type can only be declared in a
11574 declaration context. */
11580 /* Friends have special name lookup rules. */
11581 ts = ts_within_enclosing_non_class;
11582 else if (is_declaration
11583 && cp_lexer_next_token_is (parser->lexer,
11585 /* This is a `class-key identifier ;' */
11591 (parser->num_template_parameter_lists
11592 && (cp_parser_next_token_starts_class_definition_p (parser)
11593 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11594 /* An unqualified name was used to reference this type, so
11595 there were no qualifying templates. */
11596 if (!cp_parser_check_template_parameters (parser,
11597 /*num_templates=*/0,
11599 return error_mark_node;
11600 type = xref_tag (tag_type, identifier, ts, template_p);
11604 if (type == error_mark_node)
11605 return error_mark_node;
11607 /* Allow attributes on forward declarations of classes. */
11610 if (TREE_CODE (type) == TYPENAME_TYPE)
11611 warning (OPT_Wattributes,
11612 "attributes ignored on uninstantiated type");
11613 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11614 && ! processing_explicit_instantiation)
11615 warning (OPT_Wattributes,
11616 "attributes ignored on template instantiation");
11617 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11618 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11620 warning (OPT_Wattributes,
11621 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11624 if (tag_type != enum_type)
11625 cp_parser_check_class_key (tag_type, type);
11627 /* A "<" cannot follow an elaborated type specifier. If that
11628 happens, the user was probably trying to form a template-id. */
11629 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11634 /* Parse an enum-specifier.
11637 enum identifier [opt] { enumerator-list [opt] }
11640 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11643 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11644 if the token stream isn't an enum-specifier after all. */
11647 cp_parser_enum_specifier (cp_parser* parser)
11653 /* Parse tentatively so that we can back up if we don't find a
11655 cp_parser_parse_tentatively (parser);
11657 /* Caller guarantees that the current token is 'enum', an identifier
11658 possibly follows, and the token after that is an opening brace.
11659 If we don't have an identifier, fabricate an anonymous name for
11660 the enumeration being defined. */
11661 cp_lexer_consume_token (parser->lexer);
11663 attributes = cp_parser_attributes_opt (parser);
11665 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11666 identifier = cp_parser_identifier (parser);
11668 identifier = make_anon_name ();
11670 /* Look for the `{' but don't consume it yet. */
11671 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11672 cp_parser_simulate_error (parser);
11674 if (!cp_parser_parse_definitely (parser))
11677 /* Issue an error message if type-definitions are forbidden here. */
11678 if (!cp_parser_check_type_definition (parser))
11679 type = error_mark_node;
11681 /* Create the new type. We do this before consuming the opening
11682 brace so the enum will be recorded as being on the line of its
11683 tag (or the 'enum' keyword, if there is no tag). */
11684 type = start_enum (identifier);
11686 /* Consume the opening brace. */
11687 cp_lexer_consume_token (parser->lexer);
11689 if (type == error_mark_node)
11691 cp_parser_skip_to_end_of_block_or_statement (parser);
11692 return error_mark_node;
11695 /* If the next token is not '}', then there are some enumerators. */
11696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11697 cp_parser_enumerator_list (parser, type);
11699 /* Consume the final '}'. */
11700 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11702 /* Look for trailing attributes to apply to this enumeration, and
11703 apply them if appropriate. */
11704 if (cp_parser_allow_gnu_extensions_p (parser))
11706 tree trailing_attr = cp_parser_attributes_opt (parser);
11707 cplus_decl_attributes (&type,
11709 (int) ATTR_FLAG_TYPE_IN_PLACE);
11712 /* Finish up the enumeration. */
11713 finish_enum (type);
11718 /* Parse an enumerator-list. The enumerators all have the indicated
11722 enumerator-definition
11723 enumerator-list , enumerator-definition */
11726 cp_parser_enumerator_list (cp_parser* parser, tree type)
11730 /* Parse an enumerator-definition. */
11731 cp_parser_enumerator_definition (parser, type);
11733 /* If the next token is not a ',', we've reached the end of
11735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11737 /* Otherwise, consume the `,' and keep going. */
11738 cp_lexer_consume_token (parser->lexer);
11739 /* If the next token is a `}', there is a trailing comma. */
11740 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11742 if (pedantic && !in_system_header)
11743 pedwarn ("comma at end of enumerator list");
11749 /* Parse an enumerator-definition. The enumerator has the indicated
11752 enumerator-definition:
11754 enumerator = constant-expression
11760 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11765 /* Look for the identifier. */
11766 identifier = cp_parser_identifier (parser);
11767 if (identifier == error_mark_node)
11770 /* If the next token is an '=', then there is an explicit value. */
11771 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11773 /* Consume the `=' token. */
11774 cp_lexer_consume_token (parser->lexer);
11775 /* Parse the value. */
11776 value = cp_parser_constant_expression (parser,
11777 /*allow_non_constant_p=*/false,
11783 /* Create the enumerator. */
11784 build_enumerator (identifier, value, type);
11787 /* Parse a namespace-name.
11790 original-namespace-name
11793 Returns the NAMESPACE_DECL for the namespace. */
11796 cp_parser_namespace_name (cp_parser* parser)
11799 tree namespace_decl;
11801 cp_token *token = cp_lexer_peek_token (parser->lexer);
11803 /* Get the name of the namespace. */
11804 identifier = cp_parser_identifier (parser);
11805 if (identifier == error_mark_node)
11806 return error_mark_node;
11808 /* Look up the identifier in the currently active scope. Look only
11809 for namespaces, due to:
11811 [basic.lookup.udir]
11813 When looking up a namespace-name in a using-directive or alias
11814 definition, only namespace names are considered.
11818 [basic.lookup.qual]
11820 During the lookup of a name preceding the :: scope resolution
11821 operator, object, function, and enumerator names are ignored.
11823 (Note that cp_parser_class_or_namespace_name only calls this
11824 function if the token after the name is the scope resolution
11826 namespace_decl = cp_parser_lookup_name (parser, identifier,
11828 /*is_template=*/false,
11829 /*is_namespace=*/true,
11830 /*check_dependency=*/true,
11831 /*ambiguous_decls=*/NULL,
11833 /* If it's not a namespace, issue an error. */
11834 if (namespace_decl == error_mark_node
11835 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11837 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11838 error ("%H%qD is not a namespace-name", &token->location, identifier);
11839 cp_parser_error (parser, "expected namespace-name");
11840 namespace_decl = error_mark_node;
11843 return namespace_decl;
11846 /* Parse a namespace-definition.
11848 namespace-definition:
11849 named-namespace-definition
11850 unnamed-namespace-definition
11852 named-namespace-definition:
11853 original-namespace-definition
11854 extension-namespace-definition
11856 original-namespace-definition:
11857 namespace identifier { namespace-body }
11859 extension-namespace-definition:
11860 namespace original-namespace-name { namespace-body }
11862 unnamed-namespace-definition:
11863 namespace { namespace-body } */
11866 cp_parser_namespace_definition (cp_parser* parser)
11868 tree identifier, attribs;
11869 bool has_visibility;
11872 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11875 cp_lexer_consume_token (parser->lexer);
11880 /* Look for the `namespace' keyword. */
11881 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11883 /* Get the name of the namespace. We do not attempt to distinguish
11884 between an original-namespace-definition and an
11885 extension-namespace-definition at this point. The semantic
11886 analysis routines are responsible for that. */
11887 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11888 identifier = cp_parser_identifier (parser);
11890 identifier = NULL_TREE;
11892 /* Parse any specified attributes. */
11893 attribs = cp_parser_attributes_opt (parser);
11895 /* Look for the `{' to start the namespace. */
11896 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11897 /* Start the namespace. */
11898 push_namespace (identifier);
11900 /* "inline namespace" is equivalent to a stub namespace definition
11901 followed by a strong using directive. */
11904 tree name_space = current_namespace;
11905 /* Set up namespace association. */
11906 DECL_NAMESPACE_ASSOCIATIONS (name_space)
11907 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11908 DECL_NAMESPACE_ASSOCIATIONS (name_space));
11909 /* Import the contents of the inline namespace. */
11911 do_using_directive (name_space);
11912 push_namespace (identifier);
11915 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11917 /* Parse the body of the namespace. */
11918 cp_parser_namespace_body (parser);
11920 #ifdef HANDLE_PRAGMA_VISIBILITY
11921 if (has_visibility)
11925 /* Finish the namespace. */
11927 /* Look for the final `}'. */
11928 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11931 /* Parse a namespace-body.
11934 declaration-seq [opt] */
11937 cp_parser_namespace_body (cp_parser* parser)
11939 cp_parser_declaration_seq_opt (parser);
11942 /* Parse a namespace-alias-definition.
11944 namespace-alias-definition:
11945 namespace identifier = qualified-namespace-specifier ; */
11948 cp_parser_namespace_alias_definition (cp_parser* parser)
11951 tree namespace_specifier;
11953 cp_token *token = cp_lexer_peek_token (parser->lexer);
11955 /* Look for the `namespace' keyword. */
11956 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11957 /* Look for the identifier. */
11958 identifier = cp_parser_identifier (parser);
11959 if (identifier == error_mark_node)
11961 /* Look for the `=' token. */
11962 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11963 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11965 error ("%H%<namespace%> definition is not allowed here", &token->location);
11966 /* Skip the definition. */
11967 cp_lexer_consume_token (parser->lexer);
11968 if (cp_parser_skip_to_closing_brace (parser))
11969 cp_lexer_consume_token (parser->lexer);
11972 cp_parser_require (parser, CPP_EQ, "%<=%>");
11973 /* Look for the qualified-namespace-specifier. */
11974 namespace_specifier
11975 = cp_parser_qualified_namespace_specifier (parser);
11976 /* Look for the `;' token. */
11977 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11979 /* Register the alias in the symbol table. */
11980 do_namespace_alias (identifier, namespace_specifier);
11983 /* Parse a qualified-namespace-specifier.
11985 qualified-namespace-specifier:
11986 :: [opt] nested-name-specifier [opt] namespace-name
11988 Returns a NAMESPACE_DECL corresponding to the specified
11992 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11994 /* Look for the optional `::'. */
11995 cp_parser_global_scope_opt (parser,
11996 /*current_scope_valid_p=*/false);
11998 /* Look for the optional nested-name-specifier. */
11999 cp_parser_nested_name_specifier_opt (parser,
12000 /*typename_keyword_p=*/false,
12001 /*check_dependency_p=*/true,
12003 /*is_declaration=*/true);
12005 return cp_parser_namespace_name (parser);
12008 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12009 access declaration.
12012 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12013 using :: unqualified-id ;
12015 access-declaration:
12021 cp_parser_using_declaration (cp_parser* parser,
12022 bool access_declaration_p)
12025 bool typename_p = false;
12026 bool global_scope_p;
12031 if (access_declaration_p)
12032 cp_parser_parse_tentatively (parser);
12035 /* Look for the `using' keyword. */
12036 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12038 /* Peek at the next token. */
12039 token = cp_lexer_peek_token (parser->lexer);
12040 /* See if it's `typename'. */
12041 if (token->keyword == RID_TYPENAME)
12043 /* Remember that we've seen it. */
12045 /* Consume the `typename' token. */
12046 cp_lexer_consume_token (parser->lexer);
12050 /* Look for the optional global scope qualification. */
12052 = (cp_parser_global_scope_opt (parser,
12053 /*current_scope_valid_p=*/false)
12056 /* If we saw `typename', or didn't see `::', then there must be a
12057 nested-name-specifier present. */
12058 if (typename_p || !global_scope_p)
12059 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12060 /*check_dependency_p=*/true,
12062 /*is_declaration=*/true);
12063 /* Otherwise, we could be in either of the two productions. In that
12064 case, treat the nested-name-specifier as optional. */
12066 qscope = cp_parser_nested_name_specifier_opt (parser,
12067 /*typename_keyword_p=*/false,
12068 /*check_dependency_p=*/true,
12070 /*is_declaration=*/true);
12072 qscope = global_namespace;
12074 if (access_declaration_p && cp_parser_error_occurred (parser))
12075 /* Something has already gone wrong; there's no need to parse
12076 further. Since an error has occurred, the return value of
12077 cp_parser_parse_definitely will be false, as required. */
12078 return cp_parser_parse_definitely (parser);
12080 token = cp_lexer_peek_token (parser->lexer);
12081 /* Parse the unqualified-id. */
12082 identifier = cp_parser_unqualified_id (parser,
12083 /*template_keyword_p=*/false,
12084 /*check_dependency_p=*/true,
12085 /*declarator_p=*/true,
12086 /*optional_p=*/false);
12088 if (access_declaration_p)
12090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12091 cp_parser_simulate_error (parser);
12092 if (!cp_parser_parse_definitely (parser))
12096 /* The function we call to handle a using-declaration is different
12097 depending on what scope we are in. */
12098 if (qscope == error_mark_node || identifier == error_mark_node)
12100 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12101 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12102 /* [namespace.udecl]
12104 A using declaration shall not name a template-id. */
12105 error ("%Ha template-id may not appear in a using-declaration",
12109 if (at_class_scope_p ())
12111 /* Create the USING_DECL. */
12112 decl = do_class_using_decl (parser->scope, identifier);
12114 if (check_for_bare_parameter_packs (decl))
12117 /* Add it to the list of members in this class. */
12118 finish_member_declaration (decl);
12122 decl = cp_parser_lookup_name_simple (parser,
12125 if (decl == error_mark_node)
12126 cp_parser_name_lookup_error (parser, identifier,
12129 else if (check_for_bare_parameter_packs (decl))
12131 else if (!at_namespace_scope_p ())
12132 do_local_using_decl (decl, qscope, identifier);
12134 do_toplevel_using_decl (decl, qscope, identifier);
12138 /* Look for the final `;'. */
12139 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12144 /* Parse a using-directive.
12147 using namespace :: [opt] nested-name-specifier [opt]
12148 namespace-name ; */
12151 cp_parser_using_directive (cp_parser* parser)
12153 tree namespace_decl;
12156 /* Look for the `using' keyword. */
12157 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12158 /* And the `namespace' keyword. */
12159 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12160 /* Look for the optional `::' operator. */
12161 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12162 /* And the optional nested-name-specifier. */
12163 cp_parser_nested_name_specifier_opt (parser,
12164 /*typename_keyword_p=*/false,
12165 /*check_dependency_p=*/true,
12167 /*is_declaration=*/true);
12168 /* Get the namespace being used. */
12169 namespace_decl = cp_parser_namespace_name (parser);
12170 /* And any specified attributes. */
12171 attribs = cp_parser_attributes_opt (parser);
12172 /* Update the symbol table. */
12173 parse_using_directive (namespace_decl, attribs);
12174 /* Look for the final `;'. */
12175 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12178 /* Parse an asm-definition.
12181 asm ( string-literal ) ;
12186 asm volatile [opt] ( string-literal ) ;
12187 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12188 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12189 : asm-operand-list [opt] ) ;
12190 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12191 : asm-operand-list [opt]
12192 : asm-operand-list [opt] ) ; */
12195 cp_parser_asm_definition (cp_parser* parser)
12198 tree outputs = NULL_TREE;
12199 tree inputs = NULL_TREE;
12200 tree clobbers = NULL_TREE;
12202 bool volatile_p = false;
12203 bool extended_p = false;
12204 bool invalid_inputs_p = false;
12205 bool invalid_outputs_p = false;
12207 /* Look for the `asm' keyword. */
12208 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12209 /* See if the next token is `volatile'. */
12210 if (cp_parser_allow_gnu_extensions_p (parser)
12211 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12213 /* Remember that we saw the `volatile' keyword. */
12215 /* Consume the token. */
12216 cp_lexer_consume_token (parser->lexer);
12218 /* Look for the opening `('. */
12219 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12221 /* Look for the string. */
12222 string = cp_parser_string_literal (parser, false, false);
12223 if (string == error_mark_node)
12225 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12226 /*consume_paren=*/true);
12230 /* If we're allowing GNU extensions, check for the extended assembly
12231 syntax. Unfortunately, the `:' tokens need not be separated by
12232 a space in C, and so, for compatibility, we tolerate that here
12233 too. Doing that means that we have to treat the `::' operator as
12235 if (cp_parser_allow_gnu_extensions_p (parser)
12236 && parser->in_function_body
12237 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12238 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12240 bool inputs_p = false;
12241 bool clobbers_p = false;
12243 /* The extended syntax was used. */
12246 /* Look for outputs. */
12247 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12249 /* Consume the `:'. */
12250 cp_lexer_consume_token (parser->lexer);
12251 /* Parse the output-operands. */
12252 if (cp_lexer_next_token_is_not (parser->lexer,
12254 && cp_lexer_next_token_is_not (parser->lexer,
12256 && cp_lexer_next_token_is_not (parser->lexer,
12258 outputs = cp_parser_asm_operand_list (parser);
12260 if (outputs == error_mark_node)
12261 invalid_outputs_p = true;
12263 /* If the next token is `::', there are no outputs, and the
12264 next token is the beginning of the inputs. */
12265 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12266 /* The inputs are coming next. */
12269 /* Look for inputs. */
12271 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12273 /* Consume the `:' or `::'. */
12274 cp_lexer_consume_token (parser->lexer);
12275 /* Parse the output-operands. */
12276 if (cp_lexer_next_token_is_not (parser->lexer,
12278 && cp_lexer_next_token_is_not (parser->lexer,
12280 inputs = cp_parser_asm_operand_list (parser);
12282 if (inputs == error_mark_node)
12283 invalid_inputs_p = true;
12285 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12286 /* The clobbers are coming next. */
12289 /* Look for clobbers. */
12291 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12293 /* Consume the `:' or `::'. */
12294 cp_lexer_consume_token (parser->lexer);
12295 /* Parse the clobbers. */
12296 if (cp_lexer_next_token_is_not (parser->lexer,
12298 clobbers = cp_parser_asm_clobber_list (parser);
12301 /* Look for the closing `)'. */
12302 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12303 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12304 /*consume_paren=*/true);
12305 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12307 if (!invalid_inputs_p && !invalid_outputs_p)
12309 /* Create the ASM_EXPR. */
12310 if (parser->in_function_body)
12312 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12314 /* If the extended syntax was not used, mark the ASM_EXPR. */
12317 tree temp = asm_stmt;
12318 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12319 temp = TREE_OPERAND (temp, 0);
12321 ASM_INPUT_P (temp) = 1;
12325 cgraph_add_asm_node (string);
12329 /* Declarators [gram.dcl.decl] */
12331 /* Parse an init-declarator.
12334 declarator initializer [opt]
12339 declarator asm-specification [opt] attributes [opt] initializer [opt]
12341 function-definition:
12342 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12344 decl-specifier-seq [opt] declarator function-try-block
12348 function-definition:
12349 __extension__ function-definition
12351 The DECL_SPECIFIERS apply to this declarator. Returns a
12352 representation of the entity declared. If MEMBER_P is TRUE, then
12353 this declarator appears in a class scope. The new DECL created by
12354 this declarator is returned.
12356 The CHECKS are access checks that should be performed once we know
12357 what entity is being declared (and, therefore, what classes have
12360 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12361 for a function-definition here as well. If the declarator is a
12362 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12363 be TRUE upon return. By that point, the function-definition will
12364 have been completely parsed.
12366 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12370 cp_parser_init_declarator (cp_parser* parser,
12371 cp_decl_specifier_seq *decl_specifiers,
12372 VEC (deferred_access_check,gc)* checks,
12373 bool function_definition_allowed_p,
12375 int declares_class_or_enum,
12376 bool* function_definition_p)
12378 cp_token *token = NULL, *asm_spec_start_token = NULL,
12379 *attributes_start_token = NULL;
12380 cp_declarator *declarator;
12381 tree prefix_attributes;
12383 tree asm_specification;
12385 tree decl = NULL_TREE;
12387 bool is_initialized;
12388 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12389 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12391 enum cpp_ttype initialization_kind;
12392 bool is_direct_init = false;
12393 bool is_non_constant_init;
12394 int ctor_dtor_or_conv_p;
12396 tree pushed_scope = NULL;
12398 /* Gather the attributes that were provided with the
12399 decl-specifiers. */
12400 prefix_attributes = decl_specifiers->attributes;
12402 /* Assume that this is not the declarator for a function
12404 if (function_definition_p)
12405 *function_definition_p = false;
12407 /* Defer access checks while parsing the declarator; we cannot know
12408 what names are accessible until we know what is being
12410 resume_deferring_access_checks ();
12412 /* Parse the declarator. */
12413 token = cp_lexer_peek_token (parser->lexer);
12415 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12416 &ctor_dtor_or_conv_p,
12417 /*parenthesized_p=*/NULL,
12418 /*member_p=*/false);
12419 /* Gather up the deferred checks. */
12420 stop_deferring_access_checks ();
12422 /* If the DECLARATOR was erroneous, there's no need to go
12424 if (declarator == cp_error_declarator)
12425 return error_mark_node;
12427 /* Check that the number of template-parameter-lists is OK. */
12428 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12430 return error_mark_node;
12432 if (declares_class_or_enum & 2)
12433 cp_parser_check_for_definition_in_return_type (declarator,
12434 decl_specifiers->type,
12435 decl_specifiers->type_location);
12437 /* Figure out what scope the entity declared by the DECLARATOR is
12438 located in. `grokdeclarator' sometimes changes the scope, so
12439 we compute it now. */
12440 scope = get_scope_of_declarator (declarator);
12442 /* If we're allowing GNU extensions, look for an asm-specification
12444 if (cp_parser_allow_gnu_extensions_p (parser))
12446 /* Look for an asm-specification. */
12447 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12448 asm_specification = cp_parser_asm_specification_opt (parser);
12449 /* And attributes. */
12450 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12451 attributes = cp_parser_attributes_opt (parser);
12455 asm_specification = NULL_TREE;
12456 attributes = NULL_TREE;
12459 /* Peek at the next token. */
12460 token = cp_lexer_peek_token (parser->lexer);
12461 /* Check to see if the token indicates the start of a
12462 function-definition. */
12463 if (function_declarator_p (declarator)
12464 && cp_parser_token_starts_function_definition_p (token))
12466 if (!function_definition_allowed_p)
12468 /* If a function-definition should not appear here, issue an
12470 cp_parser_error (parser,
12471 "a function-definition is not allowed here");
12472 return error_mark_node;
12476 /* Neither attributes nor an asm-specification are allowed
12477 on a function-definition. */
12478 if (asm_specification)
12479 error ("%Han asm-specification is not allowed "
12480 "on a function-definition",
12481 &asm_spec_start_token->location);
12483 error ("%Hattributes are not allowed on a function-definition",
12484 &attributes_start_token->location);
12485 /* This is a function-definition. */
12486 *function_definition_p = true;
12488 /* Parse the function definition. */
12490 decl = cp_parser_save_member_function_body (parser,
12493 prefix_attributes);
12496 = (cp_parser_function_definition_from_specifiers_and_declarator
12497 (parser, decl_specifiers, prefix_attributes, declarator));
12505 Only in function declarations for constructors, destructors, and
12506 type conversions can the decl-specifier-seq be omitted.
12508 We explicitly postpone this check past the point where we handle
12509 function-definitions because we tolerate function-definitions
12510 that are missing their return types in some modes. */
12511 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12513 cp_parser_error (parser,
12514 "expected constructor, destructor, or type conversion");
12515 return error_mark_node;
12518 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12519 if (token->type == CPP_EQ
12520 || token->type == CPP_OPEN_PAREN
12521 || token->type == CPP_OPEN_BRACE)
12523 is_initialized = true;
12524 initialization_kind = token->type;
12528 /* If the init-declarator isn't initialized and isn't followed by a
12529 `,' or `;', it's not a valid init-declarator. */
12530 if (token->type != CPP_COMMA
12531 && token->type != CPP_SEMICOLON)
12533 cp_parser_error (parser, "expected initializer");
12534 return error_mark_node;
12536 is_initialized = false;
12537 initialization_kind = CPP_EOF;
12540 /* Because start_decl has side-effects, we should only call it if we
12541 know we're going ahead. By this point, we know that we cannot
12542 possibly be looking at any other construct. */
12543 cp_parser_commit_to_tentative_parse (parser);
12545 /* If the decl specifiers were bad, issue an error now that we're
12546 sure this was intended to be a declarator. Then continue
12547 declaring the variable(s), as int, to try to cut down on further
12549 if (decl_specifiers->any_specifiers_p
12550 && decl_specifiers->type == error_mark_node)
12552 cp_parser_error (parser, "invalid type in declaration");
12553 decl_specifiers->type = integer_type_node;
12556 /* Check to see whether or not this declaration is a friend. */
12557 friend_p = cp_parser_friend_p (decl_specifiers);
12559 /* Enter the newly declared entry in the symbol table. If we're
12560 processing a declaration in a class-specifier, we wait until
12561 after processing the initializer. */
12564 if (parser->in_unbraced_linkage_specification_p)
12565 decl_specifiers->storage_class = sc_extern;
12566 decl = start_decl (declarator, decl_specifiers,
12567 is_initialized, attributes, prefix_attributes,
12571 /* Enter the SCOPE. That way unqualified names appearing in the
12572 initializer will be looked up in SCOPE. */
12573 pushed_scope = push_scope (scope);
12575 /* Perform deferred access control checks, now that we know in which
12576 SCOPE the declared entity resides. */
12577 if (!member_p && decl)
12579 tree saved_current_function_decl = NULL_TREE;
12581 /* If the entity being declared is a function, pretend that we
12582 are in its scope. If it is a `friend', it may have access to
12583 things that would not otherwise be accessible. */
12584 if (TREE_CODE (decl) == FUNCTION_DECL)
12586 saved_current_function_decl = current_function_decl;
12587 current_function_decl = decl;
12590 /* Perform access checks for template parameters. */
12591 cp_parser_perform_template_parameter_access_checks (checks);
12593 /* Perform the access control checks for the declarator and the
12594 the decl-specifiers. */
12595 perform_deferred_access_checks ();
12597 /* Restore the saved value. */
12598 if (TREE_CODE (decl) == FUNCTION_DECL)
12599 current_function_decl = saved_current_function_decl;
12602 /* Parse the initializer. */
12603 initializer = NULL_TREE;
12604 is_direct_init = false;
12605 is_non_constant_init = true;
12606 if (is_initialized)
12608 if (function_declarator_p (declarator))
12610 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12611 if (initialization_kind == CPP_EQ)
12612 initializer = cp_parser_pure_specifier (parser);
12615 /* If the declaration was erroneous, we don't really
12616 know what the user intended, so just silently
12617 consume the initializer. */
12618 if (decl != error_mark_node)
12619 error ("%Hinitializer provided for function",
12620 &initializer_start_token->location);
12621 cp_parser_skip_to_closing_parenthesis (parser,
12622 /*recovering=*/true,
12623 /*or_comma=*/false,
12624 /*consume_paren=*/true);
12628 initializer = cp_parser_initializer (parser,
12630 &is_non_constant_init);
12633 /* The old parser allows attributes to appear after a parenthesized
12634 initializer. Mark Mitchell proposed removing this functionality
12635 on the GCC mailing lists on 2002-08-13. This parser accepts the
12636 attributes -- but ignores them. */
12637 if (cp_parser_allow_gnu_extensions_p (parser)
12638 && initialization_kind == CPP_OPEN_PAREN)
12639 if (cp_parser_attributes_opt (parser))
12640 warning (OPT_Wattributes,
12641 "attributes after parenthesized initializer ignored");
12643 /* For an in-class declaration, use `grokfield' to create the
12649 pop_scope (pushed_scope);
12650 pushed_scope = false;
12652 decl = grokfield (declarator, decl_specifiers,
12653 initializer, !is_non_constant_init,
12654 /*asmspec=*/NULL_TREE,
12655 prefix_attributes);
12656 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12657 cp_parser_save_default_args (parser, decl);
12660 /* Finish processing the declaration. But, skip friend
12662 if (!friend_p && decl && decl != error_mark_node)
12664 cp_finish_decl (decl,
12665 initializer, !is_non_constant_init,
12667 /* If the initializer is in parentheses, then this is
12668 a direct-initialization, which means that an
12669 `explicit' constructor is OK. Otherwise, an
12670 `explicit' constructor cannot be used. */
12671 ((is_direct_init || !is_initialized)
12672 ? 0 : LOOKUP_ONLYCONVERTING));
12674 else if ((cxx_dialect != cxx98) && friend_p
12675 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12676 /* Core issue #226 (C++0x only): A default template-argument
12677 shall not be specified in a friend class template
12679 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12680 /*is_partial=*/0, /*is_friend_decl=*/1);
12682 if (!friend_p && pushed_scope)
12683 pop_scope (pushed_scope);
12688 /* Parse a declarator.
12692 ptr-operator declarator
12694 abstract-declarator:
12695 ptr-operator abstract-declarator [opt]
12696 direct-abstract-declarator
12701 attributes [opt] direct-declarator
12702 attributes [opt] ptr-operator declarator
12704 abstract-declarator:
12705 attributes [opt] ptr-operator abstract-declarator [opt]
12706 attributes [opt] direct-abstract-declarator
12708 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12709 detect constructor, destructor or conversion operators. It is set
12710 to -1 if the declarator is a name, and +1 if it is a
12711 function. Otherwise it is set to zero. Usually you just want to
12712 test for >0, but internally the negative value is used.
12714 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12715 a decl-specifier-seq unless it declares a constructor, destructor,
12716 or conversion. It might seem that we could check this condition in
12717 semantic analysis, rather than parsing, but that makes it difficult
12718 to handle something like `f()'. We want to notice that there are
12719 no decl-specifiers, and therefore realize that this is an
12720 expression, not a declaration.)
12722 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12723 the declarator is a direct-declarator of the form "(...)".
12725 MEMBER_P is true iff this declarator is a member-declarator. */
12727 static cp_declarator *
12728 cp_parser_declarator (cp_parser* parser,
12729 cp_parser_declarator_kind dcl_kind,
12730 int* ctor_dtor_or_conv_p,
12731 bool* parenthesized_p,
12735 cp_declarator *declarator;
12736 enum tree_code code;
12737 cp_cv_quals cv_quals;
12739 tree attributes = NULL_TREE;
12741 /* Assume this is not a constructor, destructor, or type-conversion
12743 if (ctor_dtor_or_conv_p)
12744 *ctor_dtor_or_conv_p = 0;
12746 if (cp_parser_allow_gnu_extensions_p (parser))
12747 attributes = cp_parser_attributes_opt (parser);
12749 /* Peek at the next token. */
12750 token = cp_lexer_peek_token (parser->lexer);
12752 /* Check for the ptr-operator production. */
12753 cp_parser_parse_tentatively (parser);
12754 /* Parse the ptr-operator. */
12755 code = cp_parser_ptr_operator (parser,
12758 /* If that worked, then we have a ptr-operator. */
12759 if (cp_parser_parse_definitely (parser))
12761 /* If a ptr-operator was found, then this declarator was not
12763 if (parenthesized_p)
12764 *parenthesized_p = true;
12765 /* The dependent declarator is optional if we are parsing an
12766 abstract-declarator. */
12767 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12768 cp_parser_parse_tentatively (parser);
12770 /* Parse the dependent declarator. */
12771 declarator = cp_parser_declarator (parser, dcl_kind,
12772 /*ctor_dtor_or_conv_p=*/NULL,
12773 /*parenthesized_p=*/NULL,
12774 /*member_p=*/false);
12776 /* If we are parsing an abstract-declarator, we must handle the
12777 case where the dependent declarator is absent. */
12778 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12779 && !cp_parser_parse_definitely (parser))
12782 declarator = cp_parser_make_indirect_declarator
12783 (code, class_type, cv_quals, declarator);
12785 /* Everything else is a direct-declarator. */
12788 if (parenthesized_p)
12789 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12791 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12792 ctor_dtor_or_conv_p,
12796 if (attributes && declarator && declarator != cp_error_declarator)
12797 declarator->attributes = attributes;
12802 /* Parse a direct-declarator or direct-abstract-declarator.
12806 direct-declarator ( parameter-declaration-clause )
12807 cv-qualifier-seq [opt]
12808 exception-specification [opt]
12809 direct-declarator [ constant-expression [opt] ]
12812 direct-abstract-declarator:
12813 direct-abstract-declarator [opt]
12814 ( parameter-declaration-clause )
12815 cv-qualifier-seq [opt]
12816 exception-specification [opt]
12817 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12818 ( abstract-declarator )
12820 Returns a representation of the declarator. DCL_KIND is
12821 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12822 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12823 we are parsing a direct-declarator. It is
12824 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12825 of ambiguity we prefer an abstract declarator, as per
12826 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12827 cp_parser_declarator. */
12829 static cp_declarator *
12830 cp_parser_direct_declarator (cp_parser* parser,
12831 cp_parser_declarator_kind dcl_kind,
12832 int* ctor_dtor_or_conv_p,
12836 cp_declarator *declarator = NULL;
12837 tree scope = NULL_TREE;
12838 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12839 bool saved_in_declarator_p = parser->in_declarator_p;
12841 tree pushed_scope = NULL_TREE;
12845 /* Peek at the next token. */
12846 token = cp_lexer_peek_token (parser->lexer);
12847 if (token->type == CPP_OPEN_PAREN)
12849 /* This is either a parameter-declaration-clause, or a
12850 parenthesized declarator. When we know we are parsing a
12851 named declarator, it must be a parenthesized declarator
12852 if FIRST is true. For instance, `(int)' is a
12853 parameter-declaration-clause, with an omitted
12854 direct-abstract-declarator. But `((*))', is a
12855 parenthesized abstract declarator. Finally, when T is a
12856 template parameter `(T)' is a
12857 parameter-declaration-clause, and not a parenthesized
12860 We first try and parse a parameter-declaration-clause,
12861 and then try a nested declarator (if FIRST is true).
12863 It is not an error for it not to be a
12864 parameter-declaration-clause, even when FIRST is
12870 The first is the declaration of a function while the
12871 second is a the definition of a variable, including its
12874 Having seen only the parenthesis, we cannot know which of
12875 these two alternatives should be selected. Even more
12876 complex are examples like:
12881 The former is a function-declaration; the latter is a
12882 variable initialization.
12884 Thus again, we try a parameter-declaration-clause, and if
12885 that fails, we back out and return. */
12887 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12889 cp_parameter_declarator *params;
12890 unsigned saved_num_template_parameter_lists;
12892 /* In a member-declarator, the only valid interpretation
12893 of a parenthesis is the start of a
12894 parameter-declaration-clause. (It is invalid to
12895 initialize a static data member with a parenthesized
12896 initializer; only the "=" form of initialization is
12899 cp_parser_parse_tentatively (parser);
12901 /* Consume the `('. */
12902 cp_lexer_consume_token (parser->lexer);
12905 /* If this is going to be an abstract declarator, we're
12906 in a declarator and we can't have default args. */
12907 parser->default_arg_ok_p = false;
12908 parser->in_declarator_p = true;
12911 /* Inside the function parameter list, surrounding
12912 template-parameter-lists do not apply. */
12913 saved_num_template_parameter_lists
12914 = parser->num_template_parameter_lists;
12915 parser->num_template_parameter_lists = 0;
12917 /* Parse the parameter-declaration-clause. */
12918 params = cp_parser_parameter_declaration_clause (parser);
12920 parser->num_template_parameter_lists
12921 = saved_num_template_parameter_lists;
12923 /* If all went well, parse the cv-qualifier-seq and the
12924 exception-specification. */
12925 if (member_p || cp_parser_parse_definitely (parser))
12927 cp_cv_quals cv_quals;
12928 tree exception_specification;
12930 if (ctor_dtor_or_conv_p)
12931 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12933 /* Consume the `)'. */
12934 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12936 /* Parse the cv-qualifier-seq. */
12937 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12938 /* And the exception-specification. */
12939 exception_specification
12940 = cp_parser_exception_specification_opt (parser);
12942 /* Create the function-declarator. */
12943 declarator = make_call_declarator (declarator,
12946 exception_specification);
12947 /* Any subsequent parameter lists are to do with
12948 return type, so are not those of the declared
12950 parser->default_arg_ok_p = false;
12952 /* Repeat the main loop. */
12957 /* If this is the first, we can try a parenthesized
12961 bool saved_in_type_id_in_expr_p;
12963 parser->default_arg_ok_p = saved_default_arg_ok_p;
12964 parser->in_declarator_p = saved_in_declarator_p;
12966 /* Consume the `('. */
12967 cp_lexer_consume_token (parser->lexer);
12968 /* Parse the nested declarator. */
12969 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12970 parser->in_type_id_in_expr_p = true;
12972 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12973 /*parenthesized_p=*/NULL,
12975 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12977 /* Expect a `)'. */
12978 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12979 declarator = cp_error_declarator;
12980 if (declarator == cp_error_declarator)
12983 goto handle_declarator;
12985 /* Otherwise, we must be done. */
12989 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12990 && token->type == CPP_OPEN_SQUARE)
12992 /* Parse an array-declarator. */
12995 if (ctor_dtor_or_conv_p)
12996 *ctor_dtor_or_conv_p = 0;
12999 parser->default_arg_ok_p = false;
13000 parser->in_declarator_p = true;
13001 /* Consume the `['. */
13002 cp_lexer_consume_token (parser->lexer);
13003 /* Peek at the next token. */
13004 token = cp_lexer_peek_token (parser->lexer);
13005 /* If the next token is `]', then there is no
13006 constant-expression. */
13007 if (token->type != CPP_CLOSE_SQUARE)
13009 bool non_constant_p;
13012 = cp_parser_constant_expression (parser,
13013 /*allow_non_constant=*/true,
13015 if (!non_constant_p)
13016 bounds = fold_non_dependent_expr (bounds);
13017 /* Normally, the array bound must be an integral constant
13018 expression. However, as an extension, we allow VLAs
13019 in function scopes. */
13020 else if (!parser->in_function_body)
13022 error ("%Harray bound is not an integer constant",
13024 bounds = error_mark_node;
13028 bounds = NULL_TREE;
13029 /* Look for the closing `]'. */
13030 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13032 declarator = cp_error_declarator;
13036 declarator = make_array_declarator (declarator, bounds);
13038 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13040 tree qualifying_scope;
13041 tree unqualified_name;
13042 special_function_kind sfk;
13044 bool pack_expansion_p = false;
13045 cp_token *declarator_id_start_token;
13047 /* Parse a declarator-id */
13048 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13051 cp_parser_parse_tentatively (parser);
13053 /* If we see an ellipsis, we should be looking at a
13055 if (token->type == CPP_ELLIPSIS)
13057 /* Consume the `...' */
13058 cp_lexer_consume_token (parser->lexer);
13060 pack_expansion_p = true;
13064 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13066 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13067 qualifying_scope = parser->scope;
13072 if (!unqualified_name && pack_expansion_p)
13074 /* Check whether an error occurred. */
13075 okay = !cp_parser_error_occurred (parser);
13077 /* We already consumed the ellipsis to mark a
13078 parameter pack, but we have no way to report it,
13079 so abort the tentative parse. We will be exiting
13080 immediately anyway. */
13081 cp_parser_abort_tentative_parse (parser);
13084 okay = cp_parser_parse_definitely (parser);
13087 unqualified_name = error_mark_node;
13088 else if (unqualified_name
13089 && (qualifying_scope
13090 || (TREE_CODE (unqualified_name)
13091 != IDENTIFIER_NODE)))
13093 cp_parser_error (parser, "expected unqualified-id");
13094 unqualified_name = error_mark_node;
13098 if (!unqualified_name)
13100 if (unqualified_name == error_mark_node)
13102 declarator = cp_error_declarator;
13103 pack_expansion_p = false;
13104 declarator->parameter_pack_p = false;
13108 if (qualifying_scope && at_namespace_scope_p ()
13109 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13111 /* In the declaration of a member of a template class
13112 outside of the class itself, the SCOPE will sometimes
13113 be a TYPENAME_TYPE. For example, given:
13115 template <typename T>
13116 int S<T>::R::i = 3;
13118 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13119 this context, we must resolve S<T>::R to an ordinary
13120 type, rather than a typename type.
13122 The reason we normally avoid resolving TYPENAME_TYPEs
13123 is that a specialization of `S' might render
13124 `S<T>::R' not a type. However, if `S' is
13125 specialized, then this `i' will not be used, so there
13126 is no harm in resolving the types here. */
13129 /* Resolve the TYPENAME_TYPE. */
13130 type = resolve_typename_type (qualifying_scope,
13131 /*only_current_p=*/false);
13132 /* If that failed, the declarator is invalid. */
13133 if (TREE_CODE (type) == TYPENAME_TYPE)
13134 error ("%H%<%T::%E%> is not a type",
13135 &declarator_id_start_token->location,
13136 TYPE_CONTEXT (qualifying_scope),
13137 TYPE_IDENTIFIER (qualifying_scope));
13138 qualifying_scope = type;
13143 if (unqualified_name)
13147 if (qualifying_scope
13148 && CLASS_TYPE_P (qualifying_scope))
13149 class_type = qualifying_scope;
13151 class_type = current_class_type;
13153 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13155 tree name_type = TREE_TYPE (unqualified_name);
13156 if (class_type && same_type_p (name_type, class_type))
13158 if (qualifying_scope
13159 && CLASSTYPE_USE_TEMPLATE (name_type))
13161 error ("%Hinvalid use of constructor as a template",
13162 &declarator_id_start_token->location);
13163 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
13164 "name the constructor in a qualified name",
13166 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13167 class_type, name_type);
13168 declarator = cp_error_declarator;
13172 unqualified_name = constructor_name (class_type);
13176 /* We do not attempt to print the declarator
13177 here because we do not have enough
13178 information about its original syntactic
13180 cp_parser_error (parser, "invalid declarator");
13181 declarator = cp_error_declarator;
13188 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13189 sfk = sfk_destructor;
13190 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13191 sfk = sfk_conversion;
13192 else if (/* There's no way to declare a constructor
13193 for an anonymous type, even if the type
13194 got a name for linkage purposes. */
13195 !TYPE_WAS_ANONYMOUS (class_type)
13196 && constructor_name_p (unqualified_name,
13199 unqualified_name = constructor_name (class_type);
13200 sfk = sfk_constructor;
13203 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13204 *ctor_dtor_or_conv_p = -1;
13207 declarator = make_id_declarator (qualifying_scope,
13210 declarator->id_loc = token->location;
13211 declarator->parameter_pack_p = pack_expansion_p;
13213 if (pack_expansion_p)
13214 maybe_warn_variadic_templates ();
13216 handle_declarator:;
13217 scope = get_scope_of_declarator (declarator);
13219 /* Any names that appear after the declarator-id for a
13220 member are looked up in the containing scope. */
13221 pushed_scope = push_scope (scope);
13222 parser->in_declarator_p = true;
13223 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13224 || (declarator && declarator->kind == cdk_id))
13225 /* Default args are only allowed on function
13227 parser->default_arg_ok_p = saved_default_arg_ok_p;
13229 parser->default_arg_ok_p = false;
13238 /* For an abstract declarator, we might wind up with nothing at this
13239 point. That's an error; the declarator is not optional. */
13241 cp_parser_error (parser, "expected declarator");
13243 /* If we entered a scope, we must exit it now. */
13245 pop_scope (pushed_scope);
13247 parser->default_arg_ok_p = saved_default_arg_ok_p;
13248 parser->in_declarator_p = saved_in_declarator_p;
13253 /* Parse a ptr-operator.
13256 * cv-qualifier-seq [opt]
13258 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13263 & cv-qualifier-seq [opt]
13265 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13266 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13267 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13268 filled in with the TYPE containing the member. *CV_QUALS is
13269 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13270 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13271 Note that the tree codes returned by this function have nothing
13272 to do with the types of trees that will be eventually be created
13273 to represent the pointer or reference type being parsed. They are
13274 just constants with suggestive names. */
13275 static enum tree_code
13276 cp_parser_ptr_operator (cp_parser* parser,
13278 cp_cv_quals *cv_quals)
13280 enum tree_code code = ERROR_MARK;
13283 /* Assume that it's not a pointer-to-member. */
13285 /* And that there are no cv-qualifiers. */
13286 *cv_quals = TYPE_UNQUALIFIED;
13288 /* Peek at the next token. */
13289 token = cp_lexer_peek_token (parser->lexer);
13291 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13292 if (token->type == CPP_MULT)
13293 code = INDIRECT_REF;
13294 else if (token->type == CPP_AND)
13296 else if ((cxx_dialect != cxx98) &&
13297 token->type == CPP_AND_AND) /* C++0x only */
13298 code = NON_LVALUE_EXPR;
13300 if (code != ERROR_MARK)
13302 /* Consume the `*', `&' or `&&'. */
13303 cp_lexer_consume_token (parser->lexer);
13305 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13306 `&', if we are allowing GNU extensions. (The only qualifier
13307 that can legally appear after `&' is `restrict', but that is
13308 enforced during semantic analysis. */
13309 if (code == INDIRECT_REF
13310 || cp_parser_allow_gnu_extensions_p (parser))
13311 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13315 /* Try the pointer-to-member case. */
13316 cp_parser_parse_tentatively (parser);
13317 /* Look for the optional `::' operator. */
13318 cp_parser_global_scope_opt (parser,
13319 /*current_scope_valid_p=*/false);
13320 /* Look for the nested-name specifier. */
13321 token = cp_lexer_peek_token (parser->lexer);
13322 cp_parser_nested_name_specifier (parser,
13323 /*typename_keyword_p=*/false,
13324 /*check_dependency_p=*/true,
13326 /*is_declaration=*/false);
13327 /* If we found it, and the next token is a `*', then we are
13328 indeed looking at a pointer-to-member operator. */
13329 if (!cp_parser_error_occurred (parser)
13330 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13332 /* Indicate that the `*' operator was used. */
13333 code = INDIRECT_REF;
13335 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13336 error ("%H%qD is a namespace", &token->location, parser->scope);
13339 /* The type of which the member is a member is given by the
13341 *type = parser->scope;
13342 /* The next name will not be qualified. */
13343 parser->scope = NULL_TREE;
13344 parser->qualifying_scope = NULL_TREE;
13345 parser->object_scope = NULL_TREE;
13346 /* Look for the optional cv-qualifier-seq. */
13347 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13350 /* If that didn't work we don't have a ptr-operator. */
13351 if (!cp_parser_parse_definitely (parser))
13352 cp_parser_error (parser, "expected ptr-operator");
13358 /* Parse an (optional) cv-qualifier-seq.
13361 cv-qualifier cv-qualifier-seq [opt]
13372 Returns a bitmask representing the cv-qualifiers. */
13375 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13377 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13382 cp_cv_quals cv_qualifier;
13384 /* Peek at the next token. */
13385 token = cp_lexer_peek_token (parser->lexer);
13386 /* See if it's a cv-qualifier. */
13387 switch (token->keyword)
13390 cv_qualifier = TYPE_QUAL_CONST;
13394 cv_qualifier = TYPE_QUAL_VOLATILE;
13398 cv_qualifier = TYPE_QUAL_RESTRICT;
13402 cv_qualifier = TYPE_UNQUALIFIED;
13409 if (cv_quals & cv_qualifier)
13411 error ("%Hduplicate cv-qualifier", &token->location);
13412 cp_lexer_purge_token (parser->lexer);
13416 cp_lexer_consume_token (parser->lexer);
13417 cv_quals |= cv_qualifier;
13424 /* Parse a declarator-id.
13428 :: [opt] nested-name-specifier [opt] type-name
13430 In the `id-expression' case, the value returned is as for
13431 cp_parser_id_expression if the id-expression was an unqualified-id.
13432 If the id-expression was a qualified-id, then a SCOPE_REF is
13433 returned. The first operand is the scope (either a NAMESPACE_DECL
13434 or TREE_TYPE), but the second is still just a representation of an
13438 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13441 /* The expression must be an id-expression. Assume that qualified
13442 names are the names of types so that:
13445 int S<T>::R::i = 3;
13447 will work; we must treat `S<T>::R' as the name of a type.
13448 Similarly, assume that qualified names are templates, where
13452 int S<T>::R<T>::i = 3;
13455 id = cp_parser_id_expression (parser,
13456 /*template_keyword_p=*/false,
13457 /*check_dependency_p=*/false,
13458 /*template_p=*/NULL,
13459 /*declarator_p=*/true,
13461 if (id && BASELINK_P (id))
13462 id = BASELINK_FUNCTIONS (id);
13466 /* Parse a type-id.
13469 type-specifier-seq abstract-declarator [opt]
13471 Returns the TYPE specified. */
13474 cp_parser_type_id (cp_parser* parser)
13476 cp_decl_specifier_seq type_specifier_seq;
13477 cp_declarator *abstract_declarator;
13479 /* Parse the type-specifier-seq. */
13480 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13481 &type_specifier_seq);
13482 if (type_specifier_seq.type == error_mark_node)
13483 return error_mark_node;
13485 /* There might or might not be an abstract declarator. */
13486 cp_parser_parse_tentatively (parser);
13487 /* Look for the declarator. */
13488 abstract_declarator
13489 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13490 /*parenthesized_p=*/NULL,
13491 /*member_p=*/false);
13492 /* Check to see if there really was a declarator. */
13493 if (!cp_parser_parse_definitely (parser))
13494 abstract_declarator = NULL;
13496 return groktypename (&type_specifier_seq, abstract_declarator);
13499 /* Parse a type-specifier-seq.
13501 type-specifier-seq:
13502 type-specifier type-specifier-seq [opt]
13506 type-specifier-seq:
13507 attributes type-specifier-seq [opt]
13509 If IS_CONDITION is true, we are at the start of a "condition",
13510 e.g., we've just seen "if (".
13512 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13515 cp_parser_type_specifier_seq (cp_parser* parser,
13517 cp_decl_specifier_seq *type_specifier_seq)
13519 bool seen_type_specifier = false;
13520 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13521 cp_token *start_token = NULL;
13523 /* Clear the TYPE_SPECIFIER_SEQ. */
13524 clear_decl_specs (type_specifier_seq);
13526 /* Parse the type-specifiers and attributes. */
13529 tree type_specifier;
13530 bool is_cv_qualifier;
13532 /* Check for attributes first. */
13533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13535 type_specifier_seq->attributes =
13536 chainon (type_specifier_seq->attributes,
13537 cp_parser_attributes_opt (parser));
13541 /* record the token of the beginning of the type specifier seq,
13542 for error reporting purposes*/
13544 start_token = cp_lexer_peek_token (parser->lexer);
13546 /* Look for the type-specifier. */
13547 type_specifier = cp_parser_type_specifier (parser,
13549 type_specifier_seq,
13550 /*is_declaration=*/false,
13553 if (!type_specifier)
13555 /* If the first type-specifier could not be found, this is not a
13556 type-specifier-seq at all. */
13557 if (!seen_type_specifier)
13559 cp_parser_error (parser, "expected type-specifier");
13560 type_specifier_seq->type = error_mark_node;
13563 /* If subsequent type-specifiers could not be found, the
13564 type-specifier-seq is complete. */
13568 seen_type_specifier = true;
13569 /* The standard says that a condition can be:
13571 type-specifier-seq declarator = assignment-expression
13578 we should treat the "S" as a declarator, not as a
13579 type-specifier. The standard doesn't say that explicitly for
13580 type-specifier-seq, but it does say that for
13581 decl-specifier-seq in an ordinary declaration. Perhaps it
13582 would be clearer just to allow a decl-specifier-seq here, and
13583 then add a semantic restriction that if any decl-specifiers
13584 that are not type-specifiers appear, the program is invalid. */
13585 if (is_condition && !is_cv_qualifier)
13586 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13589 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13592 /* Parse a parameter-declaration-clause.
13594 parameter-declaration-clause:
13595 parameter-declaration-list [opt] ... [opt]
13596 parameter-declaration-list , ...
13598 Returns a representation for the parameter declarations. A return
13599 value of NULL indicates a parameter-declaration-clause consisting
13600 only of an ellipsis. */
13602 static cp_parameter_declarator *
13603 cp_parser_parameter_declaration_clause (cp_parser* parser)
13605 cp_parameter_declarator *parameters;
13610 /* Peek at the next token. */
13611 token = cp_lexer_peek_token (parser->lexer);
13612 /* Check for trivial parameter-declaration-clauses. */
13613 if (token->type == CPP_ELLIPSIS)
13615 /* Consume the `...' token. */
13616 cp_lexer_consume_token (parser->lexer);
13619 else if (token->type == CPP_CLOSE_PAREN)
13620 /* There are no parameters. */
13622 #ifndef NO_IMPLICIT_EXTERN_C
13623 if (in_system_header && current_class_type == NULL
13624 && current_lang_name == lang_name_c)
13628 return no_parameters;
13630 /* Check for `(void)', too, which is a special case. */
13631 else if (token->keyword == RID_VOID
13632 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13633 == CPP_CLOSE_PAREN))
13635 /* Consume the `void' token. */
13636 cp_lexer_consume_token (parser->lexer);
13637 /* There are no parameters. */
13638 return no_parameters;
13641 /* Parse the parameter-declaration-list. */
13642 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13643 /* If a parse error occurred while parsing the
13644 parameter-declaration-list, then the entire
13645 parameter-declaration-clause is erroneous. */
13649 /* Peek at the next token. */
13650 token = cp_lexer_peek_token (parser->lexer);
13651 /* If it's a `,', the clause should terminate with an ellipsis. */
13652 if (token->type == CPP_COMMA)
13654 /* Consume the `,'. */
13655 cp_lexer_consume_token (parser->lexer);
13656 /* Expect an ellipsis. */
13658 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13660 /* It might also be `...' if the optional trailing `,' was
13662 else if (token->type == CPP_ELLIPSIS)
13664 /* Consume the `...' token. */
13665 cp_lexer_consume_token (parser->lexer);
13666 /* And remember that we saw it. */
13670 ellipsis_p = false;
13672 /* Finish the parameter list. */
13673 if (parameters && ellipsis_p)
13674 parameters->ellipsis_p = true;
13679 /* Parse a parameter-declaration-list.
13681 parameter-declaration-list:
13682 parameter-declaration
13683 parameter-declaration-list , parameter-declaration
13685 Returns a representation of the parameter-declaration-list, as for
13686 cp_parser_parameter_declaration_clause. However, the
13687 `void_list_node' is never appended to the list. Upon return,
13688 *IS_ERROR will be true iff an error occurred. */
13690 static cp_parameter_declarator *
13691 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13693 cp_parameter_declarator *parameters = NULL;
13694 cp_parameter_declarator **tail = ¶meters;
13695 bool saved_in_unbraced_linkage_specification_p;
13697 /* Assume all will go well. */
13699 /* The special considerations that apply to a function within an
13700 unbraced linkage specifications do not apply to the parameters
13701 to the function. */
13702 saved_in_unbraced_linkage_specification_p
13703 = parser->in_unbraced_linkage_specification_p;
13704 parser->in_unbraced_linkage_specification_p = false;
13706 /* Look for more parameters. */
13709 cp_parameter_declarator *parameter;
13710 bool parenthesized_p;
13711 /* Parse the parameter. */
13713 = cp_parser_parameter_declaration (parser,
13714 /*template_parm_p=*/false,
13717 /* If a parse error occurred parsing the parameter declaration,
13718 then the entire parameter-declaration-list is erroneous. */
13725 /* Add the new parameter to the list. */
13727 tail = ¶meter->next;
13729 /* Peek at the next token. */
13730 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13731 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13732 /* These are for Objective-C++ */
13733 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13734 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13735 /* The parameter-declaration-list is complete. */
13737 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13741 /* Peek at the next token. */
13742 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13743 /* If it's an ellipsis, then the list is complete. */
13744 if (token->type == CPP_ELLIPSIS)
13746 /* Otherwise, there must be more parameters. Consume the
13748 cp_lexer_consume_token (parser->lexer);
13749 /* When parsing something like:
13751 int i(float f, double d)
13753 we can tell after seeing the declaration for "f" that we
13754 are not looking at an initialization of a variable "i",
13755 but rather at the declaration of a function "i".
13757 Due to the fact that the parsing of template arguments
13758 (as specified to a template-id) requires backtracking we
13759 cannot use this technique when inside a template argument
13761 if (!parser->in_template_argument_list_p
13762 && !parser->in_type_id_in_expr_p
13763 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13764 /* However, a parameter-declaration of the form
13765 "foat(f)" (which is a valid declaration of a
13766 parameter "f") can also be interpreted as an
13767 expression (the conversion of "f" to "float"). */
13768 && !parenthesized_p)
13769 cp_parser_commit_to_tentative_parse (parser);
13773 cp_parser_error (parser, "expected %<,%> or %<...%>");
13774 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13775 cp_parser_skip_to_closing_parenthesis (parser,
13776 /*recovering=*/true,
13777 /*or_comma=*/false,
13778 /*consume_paren=*/false);
13783 parser->in_unbraced_linkage_specification_p
13784 = saved_in_unbraced_linkage_specification_p;
13789 /* Parse a parameter declaration.
13791 parameter-declaration:
13792 decl-specifier-seq ... [opt] declarator
13793 decl-specifier-seq declarator = assignment-expression
13794 decl-specifier-seq ... [opt] abstract-declarator [opt]
13795 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13797 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13798 declares a template parameter. (In that case, a non-nested `>'
13799 token encountered during the parsing of the assignment-expression
13800 is not interpreted as a greater-than operator.)
13802 Returns a representation of the parameter, or NULL if an error
13803 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13804 true iff the declarator is of the form "(p)". */
13806 static cp_parameter_declarator *
13807 cp_parser_parameter_declaration (cp_parser *parser,
13808 bool template_parm_p,
13809 bool *parenthesized_p)
13811 int declares_class_or_enum;
13812 bool greater_than_is_operator_p;
13813 cp_decl_specifier_seq decl_specifiers;
13814 cp_declarator *declarator;
13815 tree default_argument;
13816 cp_token *token = NULL, *declarator_token_start = NULL;
13817 const char *saved_message;
13819 /* In a template parameter, `>' is not an operator.
13823 When parsing a default template-argument for a non-type
13824 template-parameter, the first non-nested `>' is taken as the end
13825 of the template parameter-list rather than a greater-than
13827 greater_than_is_operator_p = !template_parm_p;
13829 /* Type definitions may not appear in parameter types. */
13830 saved_message = parser->type_definition_forbidden_message;
13831 parser->type_definition_forbidden_message
13832 = "types may not be defined in parameter types";
13834 /* Parse the declaration-specifiers. */
13835 cp_parser_decl_specifier_seq (parser,
13836 CP_PARSER_FLAGS_NONE,
13838 &declares_class_or_enum);
13839 /* If an error occurred, there's no reason to attempt to parse the
13840 rest of the declaration. */
13841 if (cp_parser_error_occurred (parser))
13843 parser->type_definition_forbidden_message = saved_message;
13847 /* Peek at the next token. */
13848 token = cp_lexer_peek_token (parser->lexer);
13850 /* If the next token is a `)', `,', `=', `>', or `...', then there
13851 is no declarator. However, when variadic templates are enabled,
13852 there may be a declarator following `...'. */
13853 if (token->type == CPP_CLOSE_PAREN
13854 || token->type == CPP_COMMA
13855 || token->type == CPP_EQ
13856 || token->type == CPP_GREATER)
13859 if (parenthesized_p)
13860 *parenthesized_p = false;
13862 /* Otherwise, there should be a declarator. */
13865 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13866 parser->default_arg_ok_p = false;
13868 /* After seeing a decl-specifier-seq, if the next token is not a
13869 "(", there is no possibility that the code is a valid
13870 expression. Therefore, if parsing tentatively, we commit at
13872 if (!parser->in_template_argument_list_p
13873 /* In an expression context, having seen:
13877 we cannot be sure whether we are looking at a
13878 function-type (taking a "char" as a parameter) or a cast
13879 of some object of type "char" to "int". */
13880 && !parser->in_type_id_in_expr_p
13881 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13882 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13883 cp_parser_commit_to_tentative_parse (parser);
13884 /* Parse the declarator. */
13885 declarator_token_start = token;
13886 declarator = cp_parser_declarator (parser,
13887 CP_PARSER_DECLARATOR_EITHER,
13888 /*ctor_dtor_or_conv_p=*/NULL,
13890 /*member_p=*/false);
13891 parser->default_arg_ok_p = saved_default_arg_ok_p;
13892 /* After the declarator, allow more attributes. */
13893 decl_specifiers.attributes
13894 = chainon (decl_specifiers.attributes,
13895 cp_parser_attributes_opt (parser));
13898 /* If the next token is an ellipsis, and we have not seen a
13899 declarator name, and the type of the declarator contains parameter
13900 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13901 a parameter pack expansion expression. Otherwise, leave the
13902 ellipsis for a C-style variadic function. */
13903 token = cp_lexer_peek_token (parser->lexer);
13904 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13906 tree type = decl_specifiers.type;
13908 if (type && DECL_P (type))
13909 type = TREE_TYPE (type);
13912 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13913 && declarator_can_be_parameter_pack (declarator)
13914 && (!declarator || !declarator->parameter_pack_p)
13915 && uses_parameter_packs (type))
13917 /* Consume the `...'. */
13918 cp_lexer_consume_token (parser->lexer);
13919 maybe_warn_variadic_templates ();
13921 /* Build a pack expansion type */
13923 declarator->parameter_pack_p = true;
13925 decl_specifiers.type = make_pack_expansion (type);
13929 /* The restriction on defining new types applies only to the type
13930 of the parameter, not to the default argument. */
13931 parser->type_definition_forbidden_message = saved_message;
13933 /* If the next token is `=', then process a default argument. */
13934 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13936 /* Consume the `='. */
13937 cp_lexer_consume_token (parser->lexer);
13939 /* If we are defining a class, then the tokens that make up the
13940 default argument must be saved and processed later. */
13941 if (!template_parm_p && at_class_scope_p ()
13942 && TYPE_BEING_DEFINED (current_class_type))
13944 unsigned depth = 0;
13945 int maybe_template_id = 0;
13946 cp_token *first_token;
13949 /* Add tokens until we have processed the entire default
13950 argument. We add the range [first_token, token). */
13951 first_token = cp_lexer_peek_token (parser->lexer);
13956 /* Peek at the next token. */
13957 token = cp_lexer_peek_token (parser->lexer);
13958 /* What we do depends on what token we have. */
13959 switch (token->type)
13961 /* In valid code, a default argument must be
13962 immediately followed by a `,' `)', or `...'. */
13964 if (depth == 0 && maybe_template_id)
13966 /* If we've seen a '<', we might be in a
13967 template-argument-list. Until Core issue 325 is
13968 resolved, we don't know how this situation ought
13969 to be handled, so try to DTRT. We check whether
13970 what comes after the comma is a valid parameter
13971 declaration list. If it is, then the comma ends
13972 the default argument; otherwise the default
13973 argument continues. */
13974 bool error = false;
13976 /* Set ITALP so cp_parser_parameter_declaration_list
13977 doesn't decide to commit to this parse. */
13978 bool saved_italp = parser->in_template_argument_list_p;
13979 parser->in_template_argument_list_p = true;
13981 cp_parser_parse_tentatively (parser);
13982 cp_lexer_consume_token (parser->lexer);
13983 cp_parser_parameter_declaration_list (parser, &error);
13984 if (!cp_parser_error_occurred (parser) && !error)
13986 cp_parser_abort_tentative_parse (parser);
13988 parser->in_template_argument_list_p = saved_italp;
13991 case CPP_CLOSE_PAREN:
13993 /* If we run into a non-nested `;', `}', or `]',
13994 then the code is invalid -- but the default
13995 argument is certainly over. */
13996 case CPP_SEMICOLON:
13997 case CPP_CLOSE_BRACE:
13998 case CPP_CLOSE_SQUARE:
14001 /* Update DEPTH, if necessary. */
14002 else if (token->type == CPP_CLOSE_PAREN
14003 || token->type == CPP_CLOSE_BRACE
14004 || token->type == CPP_CLOSE_SQUARE)
14008 case CPP_OPEN_PAREN:
14009 case CPP_OPEN_SQUARE:
14010 case CPP_OPEN_BRACE:
14016 /* This might be the comparison operator, or it might
14017 start a template argument list. */
14018 ++maybe_template_id;
14022 if (cxx_dialect == cxx98)
14024 /* Fall through for C++0x, which treats the `>>'
14025 operator like two `>' tokens in certain
14031 /* This might be an operator, or it might close a
14032 template argument list. But if a previous '<'
14033 started a template argument list, this will have
14034 closed it, so we can't be in one anymore. */
14035 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14036 if (maybe_template_id < 0)
14037 maybe_template_id = 0;
14041 /* If we run out of tokens, issue an error message. */
14043 case CPP_PRAGMA_EOL:
14044 error ("%Hfile ends in default argument", &token->location);
14050 /* In these cases, we should look for template-ids.
14051 For example, if the default argument is
14052 `X<int, double>()', we need to do name lookup to
14053 figure out whether or not `X' is a template; if
14054 so, the `,' does not end the default argument.
14056 That is not yet done. */
14063 /* If we've reached the end, stop. */
14067 /* Add the token to the token block. */
14068 token = cp_lexer_consume_token (parser->lexer);
14071 /* Create a DEFAULT_ARG to represent the unparsed default
14073 default_argument = make_node (DEFAULT_ARG);
14074 DEFARG_TOKENS (default_argument)
14075 = cp_token_cache_new (first_token, token);
14076 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14078 /* Outside of a class definition, we can just parse the
14079 assignment-expression. */
14082 token = cp_lexer_peek_token (parser->lexer);
14084 = cp_parser_default_argument (parser, template_parm_p);
14087 if (!parser->default_arg_ok_p)
14089 if (flag_permissive)
14090 warning (0, "deprecated use of default argument for parameter of non-function");
14093 error ("%Hdefault arguments are only "
14094 "permitted for function parameters",
14096 default_argument = NULL_TREE;
14099 else if ((declarator && declarator->parameter_pack_p)
14100 || (decl_specifiers.type
14101 && PACK_EXPANSION_P (decl_specifiers.type)))
14103 const char* kind = template_parm_p? "template " : "";
14105 /* Find the name of the parameter pack. */
14106 cp_declarator *id_declarator = declarator;
14107 while (id_declarator && id_declarator->kind != cdk_id)
14108 id_declarator = id_declarator->declarator;
14110 if (id_declarator && id_declarator->kind == cdk_id)
14111 error ("%H%sparameter pack %qD cannot have a default argument",
14112 &declarator_token_start->location,
14113 kind, id_declarator->u.id.unqualified_name);
14115 error ("%H%sparameter pack cannot have a default argument",
14116 &declarator_token_start->location, kind);
14118 default_argument = NULL_TREE;
14122 default_argument = NULL_TREE;
14124 return make_parameter_declarator (&decl_specifiers,
14129 /* Parse a default argument and return it.
14131 TEMPLATE_PARM_P is true if this is a default argument for a
14132 non-type template parameter. */
14134 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14136 tree default_argument = NULL_TREE;
14137 bool saved_greater_than_is_operator_p;
14138 bool saved_local_variables_forbidden_p;
14140 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14142 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14143 parser->greater_than_is_operator_p = !template_parm_p;
14144 /* Local variable names (and the `this' keyword) may not
14145 appear in a default argument. */
14146 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14147 parser->local_variables_forbidden_p = true;
14148 /* The default argument expression may cause implicitly
14149 defined member functions to be synthesized, which will
14150 result in garbage collection. We must treat this
14151 situation as if we were within the body of function so as
14152 to avoid collecting live data on the stack. */
14154 /* Parse the assignment-expression. */
14155 if (template_parm_p)
14156 push_deferring_access_checks (dk_no_deferred);
14158 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14159 if (template_parm_p)
14160 pop_deferring_access_checks ();
14161 /* Restore saved state. */
14163 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14164 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14166 return default_argument;
14169 /* Parse a function-body.
14172 compound_statement */
14175 cp_parser_function_body (cp_parser *parser)
14177 cp_parser_compound_statement (parser, NULL, false);
14180 /* Parse a ctor-initializer-opt followed by a function-body. Return
14181 true if a ctor-initializer was present. */
14184 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14187 bool ctor_initializer_p;
14189 /* Begin the function body. */
14190 body = begin_function_body ();
14191 /* Parse the optional ctor-initializer. */
14192 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14193 /* Parse the function-body. */
14194 cp_parser_function_body (parser);
14195 /* Finish the function body. */
14196 finish_function_body (body);
14198 return ctor_initializer_p;
14201 /* Parse an initializer.
14204 = initializer-clause
14205 ( expression-list )
14207 Returns an expression representing the initializer. If no
14208 initializer is present, NULL_TREE is returned.
14210 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14211 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14212 set to TRUE if there is no initializer present. If there is an
14213 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14214 is set to true; otherwise it is set to false. */
14217 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14218 bool* non_constant_p)
14223 /* Peek at the next token. */
14224 token = cp_lexer_peek_token (parser->lexer);
14226 /* Let our caller know whether or not this initializer was
14228 *is_direct_init = (token->type != CPP_EQ);
14229 /* Assume that the initializer is constant. */
14230 *non_constant_p = false;
14232 if (token->type == CPP_EQ)
14234 /* Consume the `='. */
14235 cp_lexer_consume_token (parser->lexer);
14236 /* Parse the initializer-clause. */
14237 init = cp_parser_initializer_clause (parser, non_constant_p);
14239 else if (token->type == CPP_OPEN_PAREN)
14240 init = cp_parser_parenthesized_expression_list (parser, false,
14242 /*allow_expansion_p=*/true,
14244 else if (token->type == CPP_OPEN_BRACE)
14246 maybe_warn_cpp0x ("extended initializer lists");
14247 init = cp_parser_braced_list (parser, non_constant_p);
14248 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14252 /* Anything else is an error. */
14253 cp_parser_error (parser, "expected initializer");
14254 init = error_mark_node;
14260 /* Parse an initializer-clause.
14262 initializer-clause:
14263 assignment-expression
14266 Returns an expression representing the initializer.
14268 If the `assignment-expression' production is used the value
14269 returned is simply a representation for the expression.
14271 Otherwise, calls cp_parser_braced_list. */
14274 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14278 /* Assume the expression is constant. */
14279 *non_constant_p = false;
14281 /* If it is not a `{', then we are looking at an
14282 assignment-expression. */
14283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14286 = cp_parser_constant_expression (parser,
14287 /*allow_non_constant_p=*/true,
14289 if (!*non_constant_p)
14290 initializer = fold_non_dependent_expr (initializer);
14293 initializer = cp_parser_braced_list (parser, non_constant_p);
14295 return initializer;
14298 /* Parse a brace-enclosed initializer list.
14301 { initializer-list , [opt] }
14304 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14305 the elements of the initializer-list (or NULL, if the last
14306 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14307 NULL_TREE. There is no way to detect whether or not the optional
14308 trailing `,' was provided. NON_CONSTANT_P is as for
14309 cp_parser_initializer. */
14312 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14316 /* Consume the `{' token. */
14317 cp_lexer_consume_token (parser->lexer);
14318 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14319 initializer = make_node (CONSTRUCTOR);
14320 /* If it's not a `}', then there is a non-trivial initializer. */
14321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14323 /* Parse the initializer list. */
14324 CONSTRUCTOR_ELTS (initializer)
14325 = cp_parser_initializer_list (parser, non_constant_p);
14326 /* A trailing `,' token is allowed. */
14327 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14328 cp_lexer_consume_token (parser->lexer);
14330 /* Now, there should be a trailing `}'. */
14331 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14332 TREE_TYPE (initializer) = init_list_type_node;
14333 return initializer;
14336 /* Parse an initializer-list.
14339 initializer-clause ... [opt]
14340 initializer-list , initializer-clause ... [opt]
14345 identifier : initializer-clause
14346 initializer-list, identifier : initializer-clause
14348 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14349 for the initializer. If the INDEX of the elt is non-NULL, it is the
14350 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14351 as for cp_parser_initializer. */
14353 static VEC(constructor_elt,gc) *
14354 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14356 VEC(constructor_elt,gc) *v = NULL;
14358 /* Assume all of the expressions are constant. */
14359 *non_constant_p = false;
14361 /* Parse the rest of the list. */
14367 bool clause_non_constant_p;
14369 /* If the next token is an identifier and the following one is a
14370 colon, we are looking at the GNU designated-initializer
14372 if (cp_parser_allow_gnu_extensions_p (parser)
14373 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14374 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14376 /* Warn the user that they are using an extension. */
14378 pedwarn ("ISO C++ does not allow designated initializers");
14379 /* Consume the identifier. */
14380 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14381 /* Consume the `:'. */
14382 cp_lexer_consume_token (parser->lexer);
14385 identifier = NULL_TREE;
14387 /* Parse the initializer. */
14388 initializer = cp_parser_initializer_clause (parser,
14389 &clause_non_constant_p);
14390 /* If any clause is non-constant, so is the entire initializer. */
14391 if (clause_non_constant_p)
14392 *non_constant_p = true;
14394 /* If we have an ellipsis, this is an initializer pack
14396 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14398 /* Consume the `...'. */
14399 cp_lexer_consume_token (parser->lexer);
14401 /* Turn the initializer into an initializer expansion. */
14402 initializer = make_pack_expansion (initializer);
14405 /* Add it to the vector. */
14406 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14408 /* If the next token is not a comma, we have reached the end of
14410 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14413 /* Peek at the next token. */
14414 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14415 /* If the next token is a `}', then we're still done. An
14416 initializer-clause can have a trailing `,' after the
14417 initializer-list and before the closing `}'. */
14418 if (token->type == CPP_CLOSE_BRACE)
14421 /* Consume the `,' token. */
14422 cp_lexer_consume_token (parser->lexer);
14428 /* Classes [gram.class] */
14430 /* Parse a class-name.
14436 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14437 to indicate that names looked up in dependent types should be
14438 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14439 keyword has been used to indicate that the name that appears next
14440 is a template. TAG_TYPE indicates the explicit tag given before
14441 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14442 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14443 is the class being defined in a class-head.
14445 Returns the TYPE_DECL representing the class. */
14448 cp_parser_class_name (cp_parser *parser,
14449 bool typename_keyword_p,
14450 bool template_keyword_p,
14451 enum tag_types tag_type,
14452 bool check_dependency_p,
14454 bool is_declaration)
14461 /* All class-names start with an identifier. */
14462 token = cp_lexer_peek_token (parser->lexer);
14463 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14465 cp_parser_error (parser, "expected class-name");
14466 return error_mark_node;
14469 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14470 to a template-id, so we save it here. */
14471 scope = parser->scope;
14472 if (scope == error_mark_node)
14473 return error_mark_node;
14475 /* Any name names a type if we're following the `typename' keyword
14476 in a qualified name where the enclosing scope is type-dependent. */
14477 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14478 && dependent_type_p (scope));
14479 /* Handle the common case (an identifier, but not a template-id)
14481 if (token->type == CPP_NAME
14482 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14484 cp_token *identifier_token;
14488 /* Look for the identifier. */
14489 identifier_token = cp_lexer_peek_token (parser->lexer);
14490 ambiguous_p = identifier_token->ambiguous_p;
14491 identifier = cp_parser_identifier (parser);
14492 /* If the next token isn't an identifier, we are certainly not
14493 looking at a class-name. */
14494 if (identifier == error_mark_node)
14495 decl = error_mark_node;
14496 /* If we know this is a type-name, there's no need to look it
14498 else if (typename_p)
14502 tree ambiguous_decls;
14503 /* If we already know that this lookup is ambiguous, then
14504 we've already issued an error message; there's no reason
14508 cp_parser_simulate_error (parser);
14509 return error_mark_node;
14511 /* If the next token is a `::', then the name must be a type
14514 [basic.lookup.qual]
14516 During the lookup for a name preceding the :: scope
14517 resolution operator, object, function, and enumerator
14518 names are ignored. */
14519 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14520 tag_type = typename_type;
14521 /* Look up the name. */
14522 decl = cp_parser_lookup_name (parser, identifier,
14524 /*is_template=*/false,
14525 /*is_namespace=*/false,
14526 check_dependency_p,
14528 identifier_token->location);
14529 if (ambiguous_decls)
14531 error ("%Hreference to %qD is ambiguous",
14532 &identifier_token->location, identifier);
14533 print_candidates (ambiguous_decls);
14534 if (cp_parser_parsing_tentatively (parser))
14536 identifier_token->ambiguous_p = true;
14537 cp_parser_simulate_error (parser);
14539 return error_mark_node;
14545 /* Try a template-id. */
14546 decl = cp_parser_template_id (parser, template_keyword_p,
14547 check_dependency_p,
14549 if (decl == error_mark_node)
14550 return error_mark_node;
14553 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14555 /* If this is a typename, create a TYPENAME_TYPE. */
14556 if (typename_p && decl != error_mark_node)
14558 decl = make_typename_type (scope, decl, typename_type,
14559 /*complain=*/tf_error);
14560 if (decl != error_mark_node)
14561 decl = TYPE_NAME (decl);
14564 /* Check to see that it is really the name of a class. */
14565 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14566 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14567 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14568 /* Situations like this:
14570 template <typename T> struct A {
14571 typename T::template X<int>::I i;
14574 are problematic. Is `T::template X<int>' a class-name? The
14575 standard does not seem to be definitive, but there is no other
14576 valid interpretation of the following `::'. Therefore, those
14577 names are considered class-names. */
14579 decl = make_typename_type (scope, decl, tag_type, tf_error);
14580 if (decl != error_mark_node)
14581 decl = TYPE_NAME (decl);
14583 else if (TREE_CODE (decl) != TYPE_DECL
14584 || TREE_TYPE (decl) == error_mark_node
14585 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14586 decl = error_mark_node;
14588 if (decl == error_mark_node)
14589 cp_parser_error (parser, "expected class-name");
14594 /* Parse a class-specifier.
14597 class-head { member-specification [opt] }
14599 Returns the TREE_TYPE representing the class. */
14602 cp_parser_class_specifier (cp_parser* parser)
14606 tree attributes = NULL_TREE;
14607 int has_trailing_semicolon;
14608 bool nested_name_specifier_p;
14609 unsigned saved_num_template_parameter_lists;
14610 bool saved_in_function_body;
14611 tree old_scope = NULL_TREE;
14612 tree scope = NULL_TREE;
14615 push_deferring_access_checks (dk_no_deferred);
14617 /* Parse the class-head. */
14618 type = cp_parser_class_head (parser,
14619 &nested_name_specifier_p,
14622 /* If the class-head was a semantic disaster, skip the entire body
14626 cp_parser_skip_to_end_of_block_or_statement (parser);
14627 pop_deferring_access_checks ();
14628 return error_mark_node;
14631 /* Look for the `{'. */
14632 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14634 pop_deferring_access_checks ();
14635 return error_mark_node;
14638 /* Process the base classes. If they're invalid, skip the
14639 entire class body. */
14640 if (!xref_basetypes (type, bases))
14642 /* Consuming the closing brace yields better error messages
14644 if (cp_parser_skip_to_closing_brace (parser))
14645 cp_lexer_consume_token (parser->lexer);
14646 pop_deferring_access_checks ();
14647 return error_mark_node;
14650 /* Issue an error message if type-definitions are forbidden here. */
14651 cp_parser_check_type_definition (parser);
14652 /* Remember that we are defining one more class. */
14653 ++parser->num_classes_being_defined;
14654 /* Inside the class, surrounding template-parameter-lists do not
14656 saved_num_template_parameter_lists
14657 = parser->num_template_parameter_lists;
14658 parser->num_template_parameter_lists = 0;
14659 /* We are not in a function body. */
14660 saved_in_function_body = parser->in_function_body;
14661 parser->in_function_body = false;
14663 /* Start the class. */
14664 if (nested_name_specifier_p)
14666 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14667 old_scope = push_inner_scope (scope);
14669 type = begin_class_definition (type, attributes);
14671 if (type == error_mark_node)
14672 /* If the type is erroneous, skip the entire body of the class. */
14673 cp_parser_skip_to_closing_brace (parser);
14675 /* Parse the member-specification. */
14676 cp_parser_member_specification_opt (parser);
14678 /* Look for the trailing `}'. */
14679 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14680 /* We get better error messages by noticing a common problem: a
14681 missing trailing `;'. */
14682 token = cp_lexer_peek_token (parser->lexer);
14683 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14684 /* Look for trailing attributes to apply to this class. */
14685 if (cp_parser_allow_gnu_extensions_p (parser))
14686 attributes = cp_parser_attributes_opt (parser);
14687 if (type != error_mark_node)
14688 type = finish_struct (type, attributes);
14689 if (nested_name_specifier_p)
14690 pop_inner_scope (old_scope, scope);
14691 /* If this class is not itself within the scope of another class,
14692 then we need to parse the bodies of all of the queued function
14693 definitions. Note that the queued functions defined in a class
14694 are not always processed immediately following the
14695 class-specifier for that class. Consider:
14698 struct B { void f() { sizeof (A); } };
14701 If `f' were processed before the processing of `A' were
14702 completed, there would be no way to compute the size of `A'.
14703 Note that the nesting we are interested in here is lexical --
14704 not the semantic nesting given by TYPE_CONTEXT. In particular,
14707 struct A { struct B; };
14708 struct A::B { void f() { } };
14710 there is no need to delay the parsing of `A::B::f'. */
14711 if (--parser->num_classes_being_defined == 0)
14715 tree class_type = NULL_TREE;
14716 tree pushed_scope = NULL_TREE;
14718 /* In a first pass, parse default arguments to the functions.
14719 Then, in a second pass, parse the bodies of the functions.
14720 This two-phased approach handles cases like:
14728 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14729 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14730 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14731 TREE_PURPOSE (parser->unparsed_functions_queues)
14732 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14734 fn = TREE_VALUE (queue_entry);
14735 /* If there are default arguments that have not yet been processed,
14736 take care of them now. */
14737 if (class_type != TREE_PURPOSE (queue_entry))
14740 pop_scope (pushed_scope);
14741 class_type = TREE_PURPOSE (queue_entry);
14742 pushed_scope = push_scope (class_type);
14744 /* Make sure that any template parameters are in scope. */
14745 maybe_begin_member_template_processing (fn);
14746 /* Parse the default argument expressions. */
14747 cp_parser_late_parsing_default_args (parser, fn);
14748 /* Remove any template parameters from the symbol table. */
14749 maybe_end_member_template_processing ();
14752 pop_scope (pushed_scope);
14753 /* Now parse the body of the functions. */
14754 for (TREE_VALUE (parser->unparsed_functions_queues)
14755 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14756 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14757 TREE_VALUE (parser->unparsed_functions_queues)
14758 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14760 /* Figure out which function we need to process. */
14761 fn = TREE_VALUE (queue_entry);
14762 /* Parse the function. */
14763 cp_parser_late_parsing_for_member (parser, fn);
14767 /* Put back any saved access checks. */
14768 pop_deferring_access_checks ();
14770 /* Restore saved state. */
14771 parser->in_function_body = saved_in_function_body;
14772 parser->num_template_parameter_lists
14773 = saved_num_template_parameter_lists;
14778 /* Parse a class-head.
14781 class-key identifier [opt] base-clause [opt]
14782 class-key nested-name-specifier identifier base-clause [opt]
14783 class-key nested-name-specifier [opt] template-id
14787 class-key attributes identifier [opt] base-clause [opt]
14788 class-key attributes nested-name-specifier identifier base-clause [opt]
14789 class-key attributes nested-name-specifier [opt] template-id
14792 Upon return BASES is initialized to the list of base classes (or
14793 NULL, if there are none) in the same form returned by
14794 cp_parser_base_clause.
14796 Returns the TYPE of the indicated class. Sets
14797 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14798 involving a nested-name-specifier was used, and FALSE otherwise.
14800 Returns error_mark_node if this is not a class-head.
14802 Returns NULL_TREE if the class-head is syntactically valid, but
14803 semantically invalid in a way that means we should skip the entire
14804 body of the class. */
14807 cp_parser_class_head (cp_parser* parser,
14808 bool* nested_name_specifier_p,
14809 tree *attributes_p,
14812 tree nested_name_specifier;
14813 enum tag_types class_key;
14814 tree id = NULL_TREE;
14815 tree type = NULL_TREE;
14817 bool template_id_p = false;
14818 bool qualified_p = false;
14819 bool invalid_nested_name_p = false;
14820 bool invalid_explicit_specialization_p = false;
14821 tree pushed_scope = NULL_TREE;
14822 unsigned num_templates;
14823 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14824 /* Assume no nested-name-specifier will be present. */
14825 *nested_name_specifier_p = false;
14826 /* Assume no template parameter lists will be used in defining the
14830 *bases = NULL_TREE;
14832 /* Look for the class-key. */
14833 class_key = cp_parser_class_key (parser);
14834 if (class_key == none_type)
14835 return error_mark_node;
14837 /* Parse the attributes. */
14838 attributes = cp_parser_attributes_opt (parser);
14840 /* If the next token is `::', that is invalid -- but sometimes
14841 people do try to write:
14845 Handle this gracefully by accepting the extra qualifier, and then
14846 issuing an error about it later if this really is a
14847 class-head. If it turns out just to be an elaborated type
14848 specifier, remain silent. */
14849 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14850 qualified_p = true;
14852 push_deferring_access_checks (dk_no_check);
14854 /* Determine the name of the class. Begin by looking for an
14855 optional nested-name-specifier. */
14856 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14857 nested_name_specifier
14858 = cp_parser_nested_name_specifier_opt (parser,
14859 /*typename_keyword_p=*/false,
14860 /*check_dependency_p=*/false,
14862 /*is_declaration=*/false);
14863 /* If there was a nested-name-specifier, then there *must* be an
14865 if (nested_name_specifier)
14867 type_start_token = cp_lexer_peek_token (parser->lexer);
14868 /* Although the grammar says `identifier', it really means
14869 `class-name' or `template-name'. You are only allowed to
14870 define a class that has already been declared with this
14873 The proposed resolution for Core Issue 180 says that wherever
14874 you see `class T::X' you should treat `X' as a type-name.
14876 It is OK to define an inaccessible class; for example:
14878 class A { class B; };
14881 We do not know if we will see a class-name, or a
14882 template-name. We look for a class-name first, in case the
14883 class-name is a template-id; if we looked for the
14884 template-name first we would stop after the template-name. */
14885 cp_parser_parse_tentatively (parser);
14886 type = cp_parser_class_name (parser,
14887 /*typename_keyword_p=*/false,
14888 /*template_keyword_p=*/false,
14890 /*check_dependency_p=*/false,
14891 /*class_head_p=*/true,
14892 /*is_declaration=*/false);
14893 /* If that didn't work, ignore the nested-name-specifier. */
14894 if (!cp_parser_parse_definitely (parser))
14896 invalid_nested_name_p = true;
14897 type_start_token = cp_lexer_peek_token (parser->lexer);
14898 id = cp_parser_identifier (parser);
14899 if (id == error_mark_node)
14902 /* If we could not find a corresponding TYPE, treat this
14903 declaration like an unqualified declaration. */
14904 if (type == error_mark_node)
14905 nested_name_specifier = NULL_TREE;
14906 /* Otherwise, count the number of templates used in TYPE and its
14907 containing scopes. */
14912 for (scope = TREE_TYPE (type);
14913 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14914 scope = (TYPE_P (scope)
14915 ? TYPE_CONTEXT (scope)
14916 : DECL_CONTEXT (scope)))
14918 && CLASS_TYPE_P (scope)
14919 && CLASSTYPE_TEMPLATE_INFO (scope)
14920 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14921 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14925 /* Otherwise, the identifier is optional. */
14928 /* We don't know whether what comes next is a template-id,
14929 an identifier, or nothing at all. */
14930 cp_parser_parse_tentatively (parser);
14931 /* Check for a template-id. */
14932 type_start_token = cp_lexer_peek_token (parser->lexer);
14933 id = cp_parser_template_id (parser,
14934 /*template_keyword_p=*/false,
14935 /*check_dependency_p=*/true,
14936 /*is_declaration=*/true);
14937 /* If that didn't work, it could still be an identifier. */
14938 if (!cp_parser_parse_definitely (parser))
14940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14942 type_start_token = cp_lexer_peek_token (parser->lexer);
14943 id = cp_parser_identifier (parser);
14950 template_id_p = true;
14955 pop_deferring_access_checks ();
14958 cp_parser_check_for_invalid_template_id (parser, id,
14959 type_start_token->location);
14961 /* If it's not a `:' or a `{' then we can't really be looking at a
14962 class-head, since a class-head only appears as part of a
14963 class-specifier. We have to detect this situation before calling
14964 xref_tag, since that has irreversible side-effects. */
14965 if (!cp_parser_next_token_starts_class_definition_p (parser))
14967 cp_parser_error (parser, "expected %<{%> or %<:%>");
14968 return error_mark_node;
14971 /* At this point, we're going ahead with the class-specifier, even
14972 if some other problem occurs. */
14973 cp_parser_commit_to_tentative_parse (parser);
14974 /* Issue the error about the overly-qualified name now. */
14976 cp_parser_error (parser,
14977 "global qualification of class name is invalid");
14978 else if (invalid_nested_name_p)
14979 cp_parser_error (parser,
14980 "qualified name does not name a class");
14981 else if (nested_name_specifier)
14985 /* Reject typedef-names in class heads. */
14986 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14988 error ("%Hinvalid class name in declaration of %qD",
14989 &type_start_token->location, type);
14994 /* Figure out in what scope the declaration is being placed. */
14995 scope = current_scope ();
14996 /* If that scope does not contain the scope in which the
14997 class was originally declared, the program is invalid. */
14998 if (scope && !is_ancestor (scope, nested_name_specifier))
15000 if (at_namespace_scope_p ())
15001 error ("%Hdeclaration of %qD in namespace %qD which does not "
15003 &type_start_token->location,
15004 type, scope, nested_name_specifier);
15006 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15007 &type_start_token->location,
15008 type, scope, nested_name_specifier);
15014 A declarator-id shall not be qualified exception of the
15015 definition of a ... nested class outside of its class
15016 ... [or] a the definition or explicit instantiation of a
15017 class member of a namespace outside of its namespace. */
15018 if (scope == nested_name_specifier)
15020 permerror ("%Hextra qualification not allowed",
15021 &nested_name_specifier_token_start->location);
15022 nested_name_specifier = NULL_TREE;
15026 /* An explicit-specialization must be preceded by "template <>". If
15027 it is not, try to recover gracefully. */
15028 if (at_namespace_scope_p ()
15029 && parser->num_template_parameter_lists == 0
15032 error ("%Han explicit specialization must be preceded by %<template <>%>",
15033 &type_start_token->location);
15034 invalid_explicit_specialization_p = true;
15035 /* Take the same action that would have been taken by
15036 cp_parser_explicit_specialization. */
15037 ++parser->num_template_parameter_lists;
15038 begin_specialization ();
15040 /* There must be no "return" statements between this point and the
15041 end of this function; set "type "to the correct return value and
15042 use "goto done;" to return. */
15043 /* Make sure that the right number of template parameters were
15045 if (!cp_parser_check_template_parameters (parser, num_templates,
15046 type_start_token->location))
15048 /* If something went wrong, there is no point in even trying to
15049 process the class-definition. */
15054 /* Look up the type. */
15057 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15058 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15059 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15061 error ("%Hfunction template %qD redeclared as a class template",
15062 &type_start_token->location, id);
15063 type = error_mark_node;
15067 type = TREE_TYPE (id);
15068 type = maybe_process_partial_specialization (type);
15070 if (nested_name_specifier)
15071 pushed_scope = push_scope (nested_name_specifier);
15073 else if (nested_name_specifier)
15079 template <typename T> struct S { struct T };
15080 template <typename T> struct S<T>::T { };
15082 we will get a TYPENAME_TYPE when processing the definition of
15083 `S::T'. We need to resolve it to the actual type before we
15084 try to define it. */
15085 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15087 class_type = resolve_typename_type (TREE_TYPE (type),
15088 /*only_current_p=*/false);
15089 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15090 type = TYPE_NAME (class_type);
15093 cp_parser_error (parser, "could not resolve typename type");
15094 type = error_mark_node;
15098 if (maybe_process_partial_specialization (TREE_TYPE (type))
15099 == error_mark_node)
15105 class_type = current_class_type;
15106 /* Enter the scope indicated by the nested-name-specifier. */
15107 pushed_scope = push_scope (nested_name_specifier);
15108 /* Get the canonical version of this type. */
15109 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15110 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15111 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15113 type = push_template_decl (type);
15114 if (type == error_mark_node)
15121 type = TREE_TYPE (type);
15122 *nested_name_specifier_p = true;
15124 else /* The name is not a nested name. */
15126 /* If the class was unnamed, create a dummy name. */
15128 id = make_anon_name ();
15129 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15130 parser->num_template_parameter_lists);
15133 /* Indicate whether this class was declared as a `class' or as a
15135 if (TREE_CODE (type) == RECORD_TYPE)
15136 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15137 cp_parser_check_class_key (class_key, type);
15139 /* If this type was already complete, and we see another definition,
15140 that's an error. */
15141 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15143 error ("%Hredefinition of %q#T",
15144 &type_start_token->location, type);
15145 error ("%Hprevious definition of %q+#T",
15146 &type_start_token->location, type);
15150 else if (type == error_mark_node)
15153 /* We will have entered the scope containing the class; the names of
15154 base classes should be looked up in that context. For example:
15156 struct A { struct B {}; struct C; };
15157 struct A::C : B {};
15161 /* Get the list of base-classes, if there is one. */
15162 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15163 *bases = cp_parser_base_clause (parser);
15166 /* Leave the scope given by the nested-name-specifier. We will
15167 enter the class scope itself while processing the members. */
15169 pop_scope (pushed_scope);
15171 if (invalid_explicit_specialization_p)
15173 end_specialization ();
15174 --parser->num_template_parameter_lists;
15176 *attributes_p = attributes;
15180 /* Parse a class-key.
15187 Returns the kind of class-key specified, or none_type to indicate
15190 static enum tag_types
15191 cp_parser_class_key (cp_parser* parser)
15194 enum tag_types tag_type;
15196 /* Look for the class-key. */
15197 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15201 /* Check to see if the TOKEN is a class-key. */
15202 tag_type = cp_parser_token_is_class_key (token);
15204 cp_parser_error (parser, "expected class-key");
15208 /* Parse an (optional) member-specification.
15210 member-specification:
15211 member-declaration member-specification [opt]
15212 access-specifier : member-specification [opt] */
15215 cp_parser_member_specification_opt (cp_parser* parser)
15222 /* Peek at the next token. */
15223 token = cp_lexer_peek_token (parser->lexer);
15224 /* If it's a `}', or EOF then we've seen all the members. */
15225 if (token->type == CPP_CLOSE_BRACE
15226 || token->type == CPP_EOF
15227 || token->type == CPP_PRAGMA_EOL)
15230 /* See if this token is a keyword. */
15231 keyword = token->keyword;
15235 case RID_PROTECTED:
15237 /* Consume the access-specifier. */
15238 cp_lexer_consume_token (parser->lexer);
15239 /* Remember which access-specifier is active. */
15240 current_access_specifier = token->u.value;
15241 /* Look for the `:'. */
15242 cp_parser_require (parser, CPP_COLON, "%<:%>");
15246 /* Accept #pragmas at class scope. */
15247 if (token->type == CPP_PRAGMA)
15249 cp_parser_pragma (parser, pragma_external);
15253 /* Otherwise, the next construction must be a
15254 member-declaration. */
15255 cp_parser_member_declaration (parser);
15260 /* Parse a member-declaration.
15262 member-declaration:
15263 decl-specifier-seq [opt] member-declarator-list [opt] ;
15264 function-definition ; [opt]
15265 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15267 template-declaration
15269 member-declarator-list:
15271 member-declarator-list , member-declarator
15274 declarator pure-specifier [opt]
15275 declarator constant-initializer [opt]
15276 identifier [opt] : constant-expression
15280 member-declaration:
15281 __extension__ member-declaration
15284 declarator attributes [opt] pure-specifier [opt]
15285 declarator attributes [opt] constant-initializer [opt]
15286 identifier [opt] attributes [opt] : constant-expression
15290 member-declaration:
15291 static_assert-declaration */
15294 cp_parser_member_declaration (cp_parser* parser)
15296 cp_decl_specifier_seq decl_specifiers;
15297 tree prefix_attributes;
15299 int declares_class_or_enum;
15301 cp_token *token = NULL;
15302 cp_token *decl_spec_token_start = NULL;
15303 cp_token *initializer_token_start = NULL;
15304 int saved_pedantic;
15306 /* Check for the `__extension__' keyword. */
15307 if (cp_parser_extension_opt (parser, &saved_pedantic))
15310 cp_parser_member_declaration (parser);
15311 /* Restore the old value of the PEDANTIC flag. */
15312 pedantic = saved_pedantic;
15317 /* Check for a template-declaration. */
15318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15320 /* An explicit specialization here is an error condition, and we
15321 expect the specialization handler to detect and report this. */
15322 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15323 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15324 cp_parser_explicit_specialization (parser);
15326 cp_parser_template_declaration (parser, /*member_p=*/true);
15331 /* Check for a using-declaration. */
15332 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15334 /* Parse the using-declaration. */
15335 cp_parser_using_declaration (parser,
15336 /*access_declaration_p=*/false);
15340 /* Check for @defs. */
15341 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15344 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15345 ivar = ivar_chains;
15349 ivar = TREE_CHAIN (member);
15350 TREE_CHAIN (member) = NULL_TREE;
15351 finish_member_declaration (member);
15356 /* If the next token is `static_assert' we have a static assertion. */
15357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15359 cp_parser_static_assert (parser, /*member_p=*/true);
15363 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15366 /* Parse the decl-specifier-seq. */
15367 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15368 cp_parser_decl_specifier_seq (parser,
15369 CP_PARSER_FLAGS_OPTIONAL,
15371 &declares_class_or_enum);
15372 prefix_attributes = decl_specifiers.attributes;
15373 decl_specifiers.attributes = NULL_TREE;
15374 /* Check for an invalid type-name. */
15375 if (!decl_specifiers.type
15376 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15378 /* If there is no declarator, then the decl-specifier-seq should
15380 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15382 /* If there was no decl-specifier-seq, and the next token is a
15383 `;', then we have something like:
15389 Each member-declaration shall declare at least one member
15390 name of the class. */
15391 if (!decl_specifiers.any_specifiers_p)
15393 cp_token *token = cp_lexer_peek_token (parser->lexer);
15394 if (pedantic && !token->in_system_header)
15395 pedwarn ("%Hextra %<;%>", &token->location);
15401 /* See if this declaration is a friend. */
15402 friend_p = cp_parser_friend_p (&decl_specifiers);
15403 /* If there were decl-specifiers, check to see if there was
15404 a class-declaration. */
15405 type = check_tag_decl (&decl_specifiers);
15406 /* Nested classes have already been added to the class, but
15407 a `friend' needs to be explicitly registered. */
15410 /* If the `friend' keyword was present, the friend must
15411 be introduced with a class-key. */
15412 if (!declares_class_or_enum)
15413 error ("%Ha class-key must be used when declaring a friend",
15414 &decl_spec_token_start->location);
15417 template <typename T> struct A {
15418 friend struct A<T>::B;
15421 A<T>::B will be represented by a TYPENAME_TYPE, and
15422 therefore not recognized by check_tag_decl. */
15424 && decl_specifiers.type
15425 && TYPE_P (decl_specifiers.type))
15426 type = decl_specifiers.type;
15427 if (!type || !TYPE_P (type))
15428 error ("%Hfriend declaration does not name a class or "
15429 "function", &decl_spec_token_start->location);
15431 make_friend_class (current_class_type, type,
15432 /*complain=*/true);
15434 /* If there is no TYPE, an error message will already have
15436 else if (!type || type == error_mark_node)
15438 /* An anonymous aggregate has to be handled specially; such
15439 a declaration really declares a data member (with a
15440 particular type), as opposed to a nested class. */
15441 else if (ANON_AGGR_TYPE_P (type))
15443 /* Remove constructors and such from TYPE, now that we
15444 know it is an anonymous aggregate. */
15445 fixup_anonymous_aggr (type);
15446 /* And make the corresponding data member. */
15447 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15448 /* Add it to the class. */
15449 finish_member_declaration (decl);
15452 cp_parser_check_access_in_redeclaration
15454 decl_spec_token_start->location);
15459 /* See if these declarations will be friends. */
15460 friend_p = cp_parser_friend_p (&decl_specifiers);
15462 /* Keep going until we hit the `;' at the end of the
15464 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15466 tree attributes = NULL_TREE;
15467 tree first_attribute;
15469 /* Peek at the next token. */
15470 token = cp_lexer_peek_token (parser->lexer);
15472 /* Check for a bitfield declaration. */
15473 if (token->type == CPP_COLON
15474 || (token->type == CPP_NAME
15475 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15481 /* Get the name of the bitfield. Note that we cannot just
15482 check TOKEN here because it may have been invalidated by
15483 the call to cp_lexer_peek_nth_token above. */
15484 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15485 identifier = cp_parser_identifier (parser);
15487 identifier = NULL_TREE;
15489 /* Consume the `:' token. */
15490 cp_lexer_consume_token (parser->lexer);
15491 /* Get the width of the bitfield. */
15493 = cp_parser_constant_expression (parser,
15494 /*allow_non_constant=*/false,
15497 /* Look for attributes that apply to the bitfield. */
15498 attributes = cp_parser_attributes_opt (parser);
15499 /* Remember which attributes are prefix attributes and
15501 first_attribute = attributes;
15502 /* Combine the attributes. */
15503 attributes = chainon (prefix_attributes, attributes);
15505 /* Create the bitfield declaration. */
15506 decl = grokbitfield (identifier
15507 ? make_id_declarator (NULL_TREE,
15517 cp_declarator *declarator;
15519 tree asm_specification;
15520 int ctor_dtor_or_conv_p;
15522 /* Parse the declarator. */
15524 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15525 &ctor_dtor_or_conv_p,
15526 /*parenthesized_p=*/NULL,
15527 /*member_p=*/true);
15529 /* If something went wrong parsing the declarator, make sure
15530 that we at least consume some tokens. */
15531 if (declarator == cp_error_declarator)
15533 /* Skip to the end of the statement. */
15534 cp_parser_skip_to_end_of_statement (parser);
15535 /* If the next token is not a semicolon, that is
15536 probably because we just skipped over the body of
15537 a function. So, we consume a semicolon if
15538 present, but do not issue an error message if it
15540 if (cp_lexer_next_token_is (parser->lexer,
15542 cp_lexer_consume_token (parser->lexer);
15546 if (declares_class_or_enum & 2)
15547 cp_parser_check_for_definition_in_return_type
15548 (declarator, decl_specifiers.type,
15549 decl_specifiers.type_location);
15551 /* Look for an asm-specification. */
15552 asm_specification = cp_parser_asm_specification_opt (parser);
15553 /* Look for attributes that apply to the declaration. */
15554 attributes = cp_parser_attributes_opt (parser);
15555 /* Remember which attributes are prefix attributes and
15557 first_attribute = attributes;
15558 /* Combine the attributes. */
15559 attributes = chainon (prefix_attributes, attributes);
15561 /* If it's an `=', then we have a constant-initializer or a
15562 pure-specifier. It is not correct to parse the
15563 initializer before registering the member declaration
15564 since the member declaration should be in scope while
15565 its initializer is processed. However, the rest of the
15566 front end does not yet provide an interface that allows
15567 us to handle this correctly. */
15568 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15572 A pure-specifier shall be used only in the declaration of
15573 a virtual function.
15575 A member-declarator can contain a constant-initializer
15576 only if it declares a static member of integral or
15579 Therefore, if the DECLARATOR is for a function, we look
15580 for a pure-specifier; otherwise, we look for a
15581 constant-initializer. When we call `grokfield', it will
15582 perform more stringent semantics checks. */
15583 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15584 if (function_declarator_p (declarator))
15585 initializer = cp_parser_pure_specifier (parser);
15587 /* Parse the initializer. */
15588 initializer = cp_parser_constant_initializer (parser);
15590 /* Otherwise, there is no initializer. */
15592 initializer = NULL_TREE;
15594 /* See if we are probably looking at a function
15595 definition. We are certainly not looking at a
15596 member-declarator. Calling `grokfield' has
15597 side-effects, so we must not do it unless we are sure
15598 that we are looking at a member-declarator. */
15599 if (cp_parser_token_starts_function_definition_p
15600 (cp_lexer_peek_token (parser->lexer)))
15602 /* The grammar does not allow a pure-specifier to be
15603 used when a member function is defined. (It is
15604 possible that this fact is an oversight in the
15605 standard, since a pure function may be defined
15606 outside of the class-specifier. */
15608 error ("%Hpure-specifier on function-definition",
15609 &initializer_token_start->location);
15610 decl = cp_parser_save_member_function_body (parser,
15614 /* If the member was not a friend, declare it here. */
15616 finish_member_declaration (decl);
15617 /* Peek at the next token. */
15618 token = cp_lexer_peek_token (parser->lexer);
15619 /* If the next token is a semicolon, consume it. */
15620 if (token->type == CPP_SEMICOLON)
15621 cp_lexer_consume_token (parser->lexer);
15625 /* Create the declaration. */
15626 decl = grokfield (declarator, &decl_specifiers,
15627 initializer, /*init_const_expr_p=*/true,
15632 /* Reset PREFIX_ATTRIBUTES. */
15633 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15634 attributes = TREE_CHAIN (attributes);
15636 TREE_CHAIN (attributes) = NULL_TREE;
15638 /* If there is any qualification still in effect, clear it
15639 now; we will be starting fresh with the next declarator. */
15640 parser->scope = NULL_TREE;
15641 parser->qualifying_scope = NULL_TREE;
15642 parser->object_scope = NULL_TREE;
15643 /* If it's a `,', then there are more declarators. */
15644 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15645 cp_lexer_consume_token (parser->lexer);
15646 /* If the next token isn't a `;', then we have a parse error. */
15647 else if (cp_lexer_next_token_is_not (parser->lexer,
15650 cp_parser_error (parser, "expected %<;%>");
15651 /* Skip tokens until we find a `;'. */
15652 cp_parser_skip_to_end_of_statement (parser);
15659 /* Add DECL to the list of members. */
15661 finish_member_declaration (decl);
15663 if (TREE_CODE (decl) == FUNCTION_DECL)
15664 cp_parser_save_default_args (parser, decl);
15669 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15672 /* Parse a pure-specifier.
15677 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15678 Otherwise, ERROR_MARK_NODE is returned. */
15681 cp_parser_pure_specifier (cp_parser* parser)
15685 /* Look for the `=' token. */
15686 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15687 return error_mark_node;
15688 /* Look for the `0' token. */
15689 token = cp_lexer_consume_token (parser->lexer);
15690 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15691 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15693 cp_parser_error (parser,
15694 "invalid pure specifier (only %<= 0%> is allowed)");
15695 cp_parser_skip_to_end_of_statement (parser);
15696 return error_mark_node;
15698 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15700 error ("%Htemplates may not be %<virtual%>", &token->location);
15701 return error_mark_node;
15704 return integer_zero_node;
15707 /* Parse a constant-initializer.
15709 constant-initializer:
15710 = constant-expression
15712 Returns a representation of the constant-expression. */
15715 cp_parser_constant_initializer (cp_parser* parser)
15717 /* Look for the `=' token. */
15718 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15719 return error_mark_node;
15721 /* It is invalid to write:
15723 struct S { static const int i = { 7 }; };
15726 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15728 cp_parser_error (parser,
15729 "a brace-enclosed initializer is not allowed here");
15730 /* Consume the opening brace. */
15731 cp_lexer_consume_token (parser->lexer);
15732 /* Skip the initializer. */
15733 cp_parser_skip_to_closing_brace (parser);
15734 /* Look for the trailing `}'. */
15735 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15737 return error_mark_node;
15740 return cp_parser_constant_expression (parser,
15741 /*allow_non_constant=*/false,
15745 /* Derived classes [gram.class.derived] */
15747 /* Parse a base-clause.
15750 : base-specifier-list
15752 base-specifier-list:
15753 base-specifier ... [opt]
15754 base-specifier-list , base-specifier ... [opt]
15756 Returns a TREE_LIST representing the base-classes, in the order in
15757 which they were declared. The representation of each node is as
15758 described by cp_parser_base_specifier.
15760 In the case that no bases are specified, this function will return
15761 NULL_TREE, not ERROR_MARK_NODE. */
15764 cp_parser_base_clause (cp_parser* parser)
15766 tree bases = NULL_TREE;
15768 /* Look for the `:' that begins the list. */
15769 cp_parser_require (parser, CPP_COLON, "%<:%>");
15771 /* Scan the base-specifier-list. */
15776 bool pack_expansion_p = false;
15778 /* Look for the base-specifier. */
15779 base = cp_parser_base_specifier (parser);
15780 /* Look for the (optional) ellipsis. */
15781 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15783 /* Consume the `...'. */
15784 cp_lexer_consume_token (parser->lexer);
15786 pack_expansion_p = true;
15789 /* Add BASE to the front of the list. */
15790 if (base != error_mark_node)
15792 if (pack_expansion_p)
15793 /* Make this a pack expansion type. */
15794 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15797 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15799 TREE_CHAIN (base) = bases;
15803 /* Peek at the next token. */
15804 token = cp_lexer_peek_token (parser->lexer);
15805 /* If it's not a comma, then the list is complete. */
15806 if (token->type != CPP_COMMA)
15808 /* Consume the `,'. */
15809 cp_lexer_consume_token (parser->lexer);
15812 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15813 base class had a qualified name. However, the next name that
15814 appears is certainly not qualified. */
15815 parser->scope = NULL_TREE;
15816 parser->qualifying_scope = NULL_TREE;
15817 parser->object_scope = NULL_TREE;
15819 return nreverse (bases);
15822 /* Parse a base-specifier.
15825 :: [opt] nested-name-specifier [opt] class-name
15826 virtual access-specifier [opt] :: [opt] nested-name-specifier
15828 access-specifier virtual [opt] :: [opt] nested-name-specifier
15831 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15832 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15833 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15834 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15837 cp_parser_base_specifier (cp_parser* parser)
15841 bool virtual_p = false;
15842 bool duplicate_virtual_error_issued_p = false;
15843 bool duplicate_access_error_issued_p = false;
15844 bool class_scope_p, template_p;
15845 tree access = access_default_node;
15848 /* Process the optional `virtual' and `access-specifier'. */
15851 /* Peek at the next token. */
15852 token = cp_lexer_peek_token (parser->lexer);
15853 /* Process `virtual'. */
15854 switch (token->keyword)
15857 /* If `virtual' appears more than once, issue an error. */
15858 if (virtual_p && !duplicate_virtual_error_issued_p)
15860 cp_parser_error (parser,
15861 "%<virtual%> specified more than once in base-specified");
15862 duplicate_virtual_error_issued_p = true;
15867 /* Consume the `virtual' token. */
15868 cp_lexer_consume_token (parser->lexer);
15873 case RID_PROTECTED:
15875 /* If more than one access specifier appears, issue an
15877 if (access != access_default_node
15878 && !duplicate_access_error_issued_p)
15880 cp_parser_error (parser,
15881 "more than one access specifier in base-specified");
15882 duplicate_access_error_issued_p = true;
15885 access = ridpointers[(int) token->keyword];
15887 /* Consume the access-specifier. */
15888 cp_lexer_consume_token (parser->lexer);
15897 /* It is not uncommon to see programs mechanically, erroneously, use
15898 the 'typename' keyword to denote (dependent) qualified types
15899 as base classes. */
15900 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15902 token = cp_lexer_peek_token (parser->lexer);
15903 if (!processing_template_decl)
15904 error ("%Hkeyword %<typename%> not allowed outside of templates",
15907 error ("%Hkeyword %<typename%> not allowed in this context "
15908 "(the base class is implicitly a type)",
15910 cp_lexer_consume_token (parser->lexer);
15913 /* Look for the optional `::' operator. */
15914 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15915 /* Look for the nested-name-specifier. The simplest way to
15920 The keyword `typename' is not permitted in a base-specifier or
15921 mem-initializer; in these contexts a qualified name that
15922 depends on a template-parameter is implicitly assumed to be a
15925 is to pretend that we have seen the `typename' keyword at this
15927 cp_parser_nested_name_specifier_opt (parser,
15928 /*typename_keyword_p=*/true,
15929 /*check_dependency_p=*/true,
15931 /*is_declaration=*/true);
15932 /* If the base class is given by a qualified name, assume that names
15933 we see are type names or templates, as appropriate. */
15934 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15935 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15937 /* Finally, look for the class-name. */
15938 type = cp_parser_class_name (parser,
15942 /*check_dependency_p=*/true,
15943 /*class_head_p=*/false,
15944 /*is_declaration=*/true);
15946 if (type == error_mark_node)
15947 return error_mark_node;
15949 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15952 /* Exception handling [gram.exception] */
15954 /* Parse an (optional) exception-specification.
15956 exception-specification:
15957 throw ( type-id-list [opt] )
15959 Returns a TREE_LIST representing the exception-specification. The
15960 TREE_VALUE of each node is a type. */
15963 cp_parser_exception_specification_opt (cp_parser* parser)
15968 /* Peek at the next token. */
15969 token = cp_lexer_peek_token (parser->lexer);
15970 /* If it's not `throw', then there's no exception-specification. */
15971 if (!cp_parser_is_keyword (token, RID_THROW))
15974 /* Consume the `throw'. */
15975 cp_lexer_consume_token (parser->lexer);
15977 /* Look for the `('. */
15978 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15980 /* Peek at the next token. */
15981 token = cp_lexer_peek_token (parser->lexer);
15982 /* If it's not a `)', then there is a type-id-list. */
15983 if (token->type != CPP_CLOSE_PAREN)
15985 const char *saved_message;
15987 /* Types may not be defined in an exception-specification. */
15988 saved_message = parser->type_definition_forbidden_message;
15989 parser->type_definition_forbidden_message
15990 = "types may not be defined in an exception-specification";
15991 /* Parse the type-id-list. */
15992 type_id_list = cp_parser_type_id_list (parser);
15993 /* Restore the saved message. */
15994 parser->type_definition_forbidden_message = saved_message;
15997 type_id_list = empty_except_spec;
15999 /* Look for the `)'. */
16000 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16002 return type_id_list;
16005 /* Parse an (optional) type-id-list.
16009 type-id-list , type-id ... [opt]
16011 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16012 in the order that the types were presented. */
16015 cp_parser_type_id_list (cp_parser* parser)
16017 tree types = NULL_TREE;
16024 /* Get the next type-id. */
16025 type = cp_parser_type_id (parser);
16026 /* Parse the optional ellipsis. */
16027 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16029 /* Consume the `...'. */
16030 cp_lexer_consume_token (parser->lexer);
16032 /* Turn the type into a pack expansion expression. */
16033 type = make_pack_expansion (type);
16035 /* Add it to the list. */
16036 types = add_exception_specifier (types, type, /*complain=*/1);
16037 /* Peek at the next token. */
16038 token = cp_lexer_peek_token (parser->lexer);
16039 /* If it is not a `,', we are done. */
16040 if (token->type != CPP_COMMA)
16042 /* Consume the `,'. */
16043 cp_lexer_consume_token (parser->lexer);
16046 return nreverse (types);
16049 /* Parse a try-block.
16052 try compound-statement handler-seq */
16055 cp_parser_try_block (cp_parser* parser)
16059 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16060 try_block = begin_try_block ();
16061 cp_parser_compound_statement (parser, NULL, true);
16062 finish_try_block (try_block);
16063 cp_parser_handler_seq (parser);
16064 finish_handler_sequence (try_block);
16069 /* Parse a function-try-block.
16071 function-try-block:
16072 try ctor-initializer [opt] function-body handler-seq */
16075 cp_parser_function_try_block (cp_parser* parser)
16077 tree compound_stmt;
16079 bool ctor_initializer_p;
16081 /* Look for the `try' keyword. */
16082 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16084 /* Let the rest of the front end know where we are. */
16085 try_block = begin_function_try_block (&compound_stmt);
16086 /* Parse the function-body. */
16088 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16089 /* We're done with the `try' part. */
16090 finish_function_try_block (try_block);
16091 /* Parse the handlers. */
16092 cp_parser_handler_seq (parser);
16093 /* We're done with the handlers. */
16094 finish_function_handler_sequence (try_block, compound_stmt);
16096 return ctor_initializer_p;
16099 /* Parse a handler-seq.
16102 handler handler-seq [opt] */
16105 cp_parser_handler_seq (cp_parser* parser)
16111 /* Parse the handler. */
16112 cp_parser_handler (parser);
16113 /* Peek at the next token. */
16114 token = cp_lexer_peek_token (parser->lexer);
16115 /* If it's not `catch' then there are no more handlers. */
16116 if (!cp_parser_is_keyword (token, RID_CATCH))
16121 /* Parse a handler.
16124 catch ( exception-declaration ) compound-statement */
16127 cp_parser_handler (cp_parser* parser)
16132 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16133 handler = begin_handler ();
16134 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16135 declaration = cp_parser_exception_declaration (parser);
16136 finish_handler_parms (declaration, handler);
16137 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16138 cp_parser_compound_statement (parser, NULL, false);
16139 finish_handler (handler);
16142 /* Parse an exception-declaration.
16144 exception-declaration:
16145 type-specifier-seq declarator
16146 type-specifier-seq abstract-declarator
16150 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16151 ellipsis variant is used. */
16154 cp_parser_exception_declaration (cp_parser* parser)
16156 cp_decl_specifier_seq type_specifiers;
16157 cp_declarator *declarator;
16158 const char *saved_message;
16160 /* If it's an ellipsis, it's easy to handle. */
16161 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16163 /* Consume the `...' token. */
16164 cp_lexer_consume_token (parser->lexer);
16168 /* Types may not be defined in exception-declarations. */
16169 saved_message = parser->type_definition_forbidden_message;
16170 parser->type_definition_forbidden_message
16171 = "types may not be defined in exception-declarations";
16173 /* Parse the type-specifier-seq. */
16174 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16176 /* If it's a `)', then there is no declarator. */
16177 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16180 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16181 /*ctor_dtor_or_conv_p=*/NULL,
16182 /*parenthesized_p=*/NULL,
16183 /*member_p=*/false);
16185 /* Restore the saved message. */
16186 parser->type_definition_forbidden_message = saved_message;
16188 if (!type_specifiers.any_specifiers_p)
16189 return error_mark_node;
16191 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16194 /* Parse a throw-expression.
16197 throw assignment-expression [opt]
16199 Returns a THROW_EXPR representing the throw-expression. */
16202 cp_parser_throw_expression (cp_parser* parser)
16207 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16208 token = cp_lexer_peek_token (parser->lexer);
16209 /* Figure out whether or not there is an assignment-expression
16210 following the "throw" keyword. */
16211 if (token->type == CPP_COMMA
16212 || token->type == CPP_SEMICOLON
16213 || token->type == CPP_CLOSE_PAREN
16214 || token->type == CPP_CLOSE_SQUARE
16215 || token->type == CPP_CLOSE_BRACE
16216 || token->type == CPP_COLON)
16217 expression = NULL_TREE;
16219 expression = cp_parser_assignment_expression (parser,
16222 return build_throw (expression);
16225 /* GNU Extensions */
16227 /* Parse an (optional) asm-specification.
16230 asm ( string-literal )
16232 If the asm-specification is present, returns a STRING_CST
16233 corresponding to the string-literal. Otherwise, returns
16237 cp_parser_asm_specification_opt (cp_parser* parser)
16240 tree asm_specification;
16242 /* Peek at the next token. */
16243 token = cp_lexer_peek_token (parser->lexer);
16244 /* If the next token isn't the `asm' keyword, then there's no
16245 asm-specification. */
16246 if (!cp_parser_is_keyword (token, RID_ASM))
16249 /* Consume the `asm' token. */
16250 cp_lexer_consume_token (parser->lexer);
16251 /* Look for the `('. */
16252 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16254 /* Look for the string-literal. */
16255 asm_specification = cp_parser_string_literal (parser, false, false);
16257 /* Look for the `)'. */
16258 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16260 return asm_specification;
16263 /* Parse an asm-operand-list.
16267 asm-operand-list , asm-operand
16270 string-literal ( expression )
16271 [ string-literal ] string-literal ( expression )
16273 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16274 each node is the expression. The TREE_PURPOSE is itself a
16275 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16276 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16277 is a STRING_CST for the string literal before the parenthesis. Returns
16278 ERROR_MARK_NODE if any of the operands are invalid. */
16281 cp_parser_asm_operand_list (cp_parser* parser)
16283 tree asm_operands = NULL_TREE;
16284 bool invalid_operands = false;
16288 tree string_literal;
16292 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16294 /* Consume the `[' token. */
16295 cp_lexer_consume_token (parser->lexer);
16296 /* Read the operand name. */
16297 name = cp_parser_identifier (parser);
16298 if (name != error_mark_node)
16299 name = build_string (IDENTIFIER_LENGTH (name),
16300 IDENTIFIER_POINTER (name));
16301 /* Look for the closing `]'. */
16302 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16306 /* Look for the string-literal. */
16307 string_literal = cp_parser_string_literal (parser, false, false);
16309 /* Look for the `('. */
16310 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16311 /* Parse the expression. */
16312 expression = cp_parser_expression (parser, /*cast_p=*/false);
16313 /* Look for the `)'. */
16314 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16316 if (name == error_mark_node
16317 || string_literal == error_mark_node
16318 || expression == error_mark_node)
16319 invalid_operands = true;
16321 /* Add this operand to the list. */
16322 asm_operands = tree_cons (build_tree_list (name, string_literal),
16325 /* If the next token is not a `,', there are no more
16327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16329 /* Consume the `,'. */
16330 cp_lexer_consume_token (parser->lexer);
16333 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16336 /* Parse an asm-clobber-list.
16340 asm-clobber-list , string-literal
16342 Returns a TREE_LIST, indicating the clobbers in the order that they
16343 appeared. The TREE_VALUE of each node is a STRING_CST. */
16346 cp_parser_asm_clobber_list (cp_parser* parser)
16348 tree clobbers = NULL_TREE;
16352 tree string_literal;
16354 /* Look for the string literal. */
16355 string_literal = cp_parser_string_literal (parser, false, false);
16356 /* Add it to the list. */
16357 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16358 /* If the next token is not a `,', then the list is
16360 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16362 /* Consume the `,' token. */
16363 cp_lexer_consume_token (parser->lexer);
16369 /* Parse an (optional) series of attributes.
16372 attributes attribute
16375 __attribute__ (( attribute-list [opt] ))
16377 The return value is as for cp_parser_attribute_list. */
16380 cp_parser_attributes_opt (cp_parser* parser)
16382 tree attributes = NULL_TREE;
16387 tree attribute_list;
16389 /* Peek at the next token. */
16390 token = cp_lexer_peek_token (parser->lexer);
16391 /* If it's not `__attribute__', then we're done. */
16392 if (token->keyword != RID_ATTRIBUTE)
16395 /* Consume the `__attribute__' keyword. */
16396 cp_lexer_consume_token (parser->lexer);
16397 /* Look for the two `(' tokens. */
16398 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16399 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16401 /* Peek at the next token. */
16402 token = cp_lexer_peek_token (parser->lexer);
16403 if (token->type != CPP_CLOSE_PAREN)
16404 /* Parse the attribute-list. */
16405 attribute_list = cp_parser_attribute_list (parser);
16407 /* If the next token is a `)', then there is no attribute
16409 attribute_list = NULL;
16411 /* Look for the two `)' tokens. */
16412 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16413 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16415 /* Add these new attributes to the list. */
16416 attributes = chainon (attributes, attribute_list);
16422 /* Parse an attribute-list.
16426 attribute-list , attribute
16430 identifier ( identifier )
16431 identifier ( identifier , expression-list )
16432 identifier ( expression-list )
16434 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16435 to an attribute. The TREE_PURPOSE of each node is the identifier
16436 indicating which attribute is in use. The TREE_VALUE represents
16437 the arguments, if any. */
16440 cp_parser_attribute_list (cp_parser* parser)
16442 tree attribute_list = NULL_TREE;
16443 bool save_translate_strings_p = parser->translate_strings_p;
16445 parser->translate_strings_p = false;
16452 /* Look for the identifier. We also allow keywords here; for
16453 example `__attribute__ ((const))' is legal. */
16454 token = cp_lexer_peek_token (parser->lexer);
16455 if (token->type == CPP_NAME
16456 || token->type == CPP_KEYWORD)
16458 tree arguments = NULL_TREE;
16460 /* Consume the token. */
16461 token = cp_lexer_consume_token (parser->lexer);
16463 /* Save away the identifier that indicates which attribute
16465 identifier = token->u.value;
16466 attribute = build_tree_list (identifier, NULL_TREE);
16468 /* Peek at the next token. */
16469 token = cp_lexer_peek_token (parser->lexer);
16470 /* If it's an `(', then parse the attribute arguments. */
16471 if (token->type == CPP_OPEN_PAREN)
16473 arguments = cp_parser_parenthesized_expression_list
16474 (parser, true, /*cast_p=*/false,
16475 /*allow_expansion_p=*/false,
16476 /*non_constant_p=*/NULL);
16477 /* Save the arguments away. */
16478 TREE_VALUE (attribute) = arguments;
16481 if (arguments != error_mark_node)
16483 /* Add this attribute to the list. */
16484 TREE_CHAIN (attribute) = attribute_list;
16485 attribute_list = attribute;
16488 token = cp_lexer_peek_token (parser->lexer);
16490 /* Now, look for more attributes. If the next token isn't a
16491 `,', we're done. */
16492 if (token->type != CPP_COMMA)
16495 /* Consume the comma and keep going. */
16496 cp_lexer_consume_token (parser->lexer);
16498 parser->translate_strings_p = save_translate_strings_p;
16500 /* We built up the list in reverse order. */
16501 return nreverse (attribute_list);
16504 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16505 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16506 current value of the PEDANTIC flag, regardless of whether or not
16507 the `__extension__' keyword is present. The caller is responsible
16508 for restoring the value of the PEDANTIC flag. */
16511 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16513 /* Save the old value of the PEDANTIC flag. */
16514 *saved_pedantic = pedantic;
16516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16518 /* Consume the `__extension__' token. */
16519 cp_lexer_consume_token (parser->lexer);
16520 /* We're not being pedantic while the `__extension__' keyword is
16530 /* Parse a label declaration.
16533 __label__ label-declarator-seq ;
16535 label-declarator-seq:
16536 identifier , label-declarator-seq
16540 cp_parser_label_declaration (cp_parser* parser)
16542 /* Look for the `__label__' keyword. */
16543 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16549 /* Look for an identifier. */
16550 identifier = cp_parser_identifier (parser);
16551 /* If we failed, stop. */
16552 if (identifier == error_mark_node)
16554 /* Declare it as a label. */
16555 finish_label_decl (identifier);
16556 /* If the next token is a `;', stop. */
16557 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16559 /* Look for the `,' separating the label declarations. */
16560 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16563 /* Look for the final `;'. */
16564 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16567 /* Support Functions */
16569 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16570 NAME should have one of the representations used for an
16571 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16572 is returned. If PARSER->SCOPE is a dependent type, then a
16573 SCOPE_REF is returned.
16575 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16576 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16577 was formed. Abstractly, such entities should not be passed to this
16578 function, because they do not need to be looked up, but it is
16579 simpler to check for this special case here, rather than at the
16582 In cases not explicitly covered above, this function returns a
16583 DECL, OVERLOAD, or baselink representing the result of the lookup.
16584 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16587 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16588 (e.g., "struct") that was used. In that case bindings that do not
16589 refer to types are ignored.
16591 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16594 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16597 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16600 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16601 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16602 NULL_TREE otherwise. */
16605 cp_parser_lookup_name (cp_parser *parser, tree name,
16606 enum tag_types tag_type,
16609 bool check_dependency,
16610 tree *ambiguous_decls,
16611 location_t name_location)
16615 tree object_type = parser->context->object_type;
16617 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16618 flags |= LOOKUP_COMPLAIN;
16620 /* Assume that the lookup will be unambiguous. */
16621 if (ambiguous_decls)
16622 *ambiguous_decls = NULL_TREE;
16624 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16625 no longer valid. Note that if we are parsing tentatively, and
16626 the parse fails, OBJECT_TYPE will be automatically restored. */
16627 parser->context->object_type = NULL_TREE;
16629 if (name == error_mark_node)
16630 return error_mark_node;
16632 /* A template-id has already been resolved; there is no lookup to
16634 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16636 if (BASELINK_P (name))
16638 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16639 == TEMPLATE_ID_EXPR);
16643 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16644 it should already have been checked to make sure that the name
16645 used matches the type being destroyed. */
16646 if (TREE_CODE (name) == BIT_NOT_EXPR)
16650 /* Figure out to which type this destructor applies. */
16652 type = parser->scope;
16653 else if (object_type)
16654 type = object_type;
16656 type = current_class_type;
16657 /* If that's not a class type, there is no destructor. */
16658 if (!type || !CLASS_TYPE_P (type))
16659 return error_mark_node;
16660 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16661 lazily_declare_fn (sfk_destructor, type);
16662 if (!CLASSTYPE_DESTRUCTORS (type))
16663 return error_mark_node;
16664 /* If it was a class type, return the destructor. */
16665 return CLASSTYPE_DESTRUCTORS (type);
16668 /* By this point, the NAME should be an ordinary identifier. If
16669 the id-expression was a qualified name, the qualifying scope is
16670 stored in PARSER->SCOPE at this point. */
16671 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16673 /* Perform the lookup. */
16678 if (parser->scope == error_mark_node)
16679 return error_mark_node;
16681 /* If the SCOPE is dependent, the lookup must be deferred until
16682 the template is instantiated -- unless we are explicitly
16683 looking up names in uninstantiated templates. Even then, we
16684 cannot look up the name if the scope is not a class type; it
16685 might, for example, be a template type parameter. */
16686 dependent_p = (TYPE_P (parser->scope)
16687 && !(parser->in_declarator_p
16688 && currently_open_class (parser->scope))
16689 && dependent_type_p (parser->scope));
16690 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16697 /* The resolution to Core Issue 180 says that `struct
16698 A::B' should be considered a type-name, even if `A'
16700 type = make_typename_type (parser->scope, name, tag_type,
16701 /*complain=*/tf_error);
16702 decl = TYPE_NAME (type);
16704 else if (is_template
16705 && (cp_parser_next_token_ends_template_argument_p (parser)
16706 || cp_lexer_next_token_is (parser->lexer,
16708 decl = make_unbound_class_template (parser->scope,
16710 /*complain=*/tf_error);
16712 decl = build_qualified_name (/*type=*/NULL_TREE,
16713 parser->scope, name,
16718 tree pushed_scope = NULL_TREE;
16720 /* If PARSER->SCOPE is a dependent type, then it must be a
16721 class type, and we must not be checking dependencies;
16722 otherwise, we would have processed this lookup above. So
16723 that PARSER->SCOPE is not considered a dependent base by
16724 lookup_member, we must enter the scope here. */
16726 pushed_scope = push_scope (parser->scope);
16727 /* If the PARSER->SCOPE is a template specialization, it
16728 may be instantiated during name lookup. In that case,
16729 errors may be issued. Even if we rollback the current
16730 tentative parse, those errors are valid. */
16731 decl = lookup_qualified_name (parser->scope, name,
16732 tag_type != none_type,
16733 /*complain=*/true);
16735 /* If we have a single function from a using decl, pull it out. */
16737 && TREE_CODE (decl) == OVERLOAD
16738 && !really_overloaded_fn (decl))
16739 decl = OVL_FUNCTION (decl);
16742 pop_scope (pushed_scope);
16744 parser->qualifying_scope = parser->scope;
16745 parser->object_scope = NULL_TREE;
16747 else if (object_type)
16749 tree object_decl = NULL_TREE;
16750 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16751 OBJECT_TYPE is not a class. */
16752 if (CLASS_TYPE_P (object_type))
16753 /* If the OBJECT_TYPE is a template specialization, it may
16754 be instantiated during name lookup. In that case, errors
16755 may be issued. Even if we rollback the current tentative
16756 parse, those errors are valid. */
16757 object_decl = lookup_member (object_type,
16760 tag_type != none_type);
16761 /* Look it up in the enclosing context, too. */
16762 decl = lookup_name_real (name, tag_type != none_type,
16764 /*block_p=*/true, is_namespace, flags);
16765 parser->object_scope = object_type;
16766 parser->qualifying_scope = NULL_TREE;
16768 decl = object_decl;
16772 decl = lookup_name_real (name, tag_type != none_type,
16774 /*block_p=*/true, is_namespace, flags);
16775 parser->qualifying_scope = NULL_TREE;
16776 parser->object_scope = NULL_TREE;
16779 /* If the lookup failed, let our caller know. */
16780 if (!decl || decl == error_mark_node)
16781 return error_mark_node;
16783 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16784 if (TREE_CODE (decl) == TREE_LIST)
16786 if (ambiguous_decls)
16787 *ambiguous_decls = decl;
16788 /* The error message we have to print is too complicated for
16789 cp_parser_error, so we incorporate its actions directly. */
16790 if (!cp_parser_simulate_error (parser))
16792 error ("%Hreference to %qD is ambiguous",
16793 &name_location, name);
16794 print_candidates (decl);
16796 return error_mark_node;
16799 gcc_assert (DECL_P (decl)
16800 || TREE_CODE (decl) == OVERLOAD
16801 || TREE_CODE (decl) == SCOPE_REF
16802 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16803 || BASELINK_P (decl));
16805 /* If we have resolved the name of a member declaration, check to
16806 see if the declaration is accessible. When the name resolves to
16807 set of overloaded functions, accessibility is checked when
16808 overload resolution is done.
16810 During an explicit instantiation, access is not checked at all,
16811 as per [temp.explicit]. */
16813 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16818 /* Like cp_parser_lookup_name, but for use in the typical case where
16819 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16820 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16823 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16825 return cp_parser_lookup_name (parser, name,
16827 /*is_template=*/false,
16828 /*is_namespace=*/false,
16829 /*check_dependency=*/true,
16830 /*ambiguous_decls=*/NULL,
16834 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16835 the current context, return the TYPE_DECL. If TAG_NAME_P is
16836 true, the DECL indicates the class being defined in a class-head,
16837 or declared in an elaborated-type-specifier.
16839 Otherwise, return DECL. */
16842 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16844 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16845 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16848 template <typename T> struct B;
16851 template <typename T> struct A::B {};
16853 Similarly, in an elaborated-type-specifier:
16855 namespace N { struct X{}; }
16858 template <typename T> friend struct N::X;
16861 However, if the DECL refers to a class type, and we are in
16862 the scope of the class, then the name lookup automatically
16863 finds the TYPE_DECL created by build_self_reference rather
16864 than a TEMPLATE_DECL. For example, in:
16866 template <class T> struct S {
16870 there is no need to handle such case. */
16872 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16873 return DECL_TEMPLATE_RESULT (decl);
16878 /* If too many, or too few, template-parameter lists apply to the
16879 declarator, issue an error message. Returns TRUE if all went well,
16880 and FALSE otherwise. */
16883 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16884 cp_declarator *declarator,
16885 location_t declarator_location)
16887 unsigned num_templates;
16889 /* We haven't seen any classes that involve template parameters yet. */
16892 switch (declarator->kind)
16895 if (declarator->u.id.qualifying_scope)
16900 scope = declarator->u.id.qualifying_scope;
16901 member = declarator->u.id.unqualified_name;
16903 while (scope && CLASS_TYPE_P (scope))
16905 /* You're supposed to have one `template <...>'
16906 for every template class, but you don't need one
16907 for a full specialization. For example:
16909 template <class T> struct S{};
16910 template <> struct S<int> { void f(); };
16911 void S<int>::f () {}
16913 is correct; there shouldn't be a `template <>' for
16914 the definition of `S<int>::f'. */
16915 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16916 /* If SCOPE does not have template information of any
16917 kind, then it is not a template, nor is it nested
16918 within a template. */
16920 if (explicit_class_specialization_p (scope))
16922 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16925 scope = TYPE_CONTEXT (scope);
16928 else if (TREE_CODE (declarator->u.id.unqualified_name)
16929 == TEMPLATE_ID_EXPR)
16930 /* If the DECLARATOR has the form `X<y>' then it uses one
16931 additional level of template parameters. */
16934 return cp_parser_check_template_parameters (parser,
16936 declarator_location);
16941 case cdk_reference:
16943 return (cp_parser_check_declarator_template_parameters
16944 (parser, declarator->declarator, declarator_location));
16950 gcc_unreachable ();
16955 /* NUM_TEMPLATES were used in the current declaration. If that is
16956 invalid, return FALSE and issue an error messages. Otherwise,
16960 cp_parser_check_template_parameters (cp_parser* parser,
16961 unsigned num_templates,
16962 location_t location)
16964 /* If there are more template classes than parameter lists, we have
16967 template <class T> void S<T>::R<T>::f (); */
16968 if (parser->num_template_parameter_lists < num_templates)
16970 error ("%Htoo few template-parameter-lists", &location);
16973 /* If there are the same number of template classes and parameter
16974 lists, that's OK. */
16975 if (parser->num_template_parameter_lists == num_templates)
16977 /* If there are more, but only one more, then we are referring to a
16978 member template. That's OK too. */
16979 if (parser->num_template_parameter_lists == num_templates + 1)
16981 /* Otherwise, there are too many template parameter lists. We have
16984 template <class T> template <class U> void S::f(); */
16985 error ("%Htoo many template-parameter-lists", &location);
16989 /* Parse an optional `::' token indicating that the following name is
16990 from the global namespace. If so, PARSER->SCOPE is set to the
16991 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16992 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16993 Returns the new value of PARSER->SCOPE, if the `::' token is
16994 present, and NULL_TREE otherwise. */
16997 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17001 /* Peek at the next token. */
17002 token = cp_lexer_peek_token (parser->lexer);
17003 /* If we're looking at a `::' token then we're starting from the
17004 global namespace, not our current location. */
17005 if (token->type == CPP_SCOPE)
17007 /* Consume the `::' token. */
17008 cp_lexer_consume_token (parser->lexer);
17009 /* Set the SCOPE so that we know where to start the lookup. */
17010 parser->scope = global_namespace;
17011 parser->qualifying_scope = global_namespace;
17012 parser->object_scope = NULL_TREE;
17014 return parser->scope;
17016 else if (!current_scope_valid_p)
17018 parser->scope = NULL_TREE;
17019 parser->qualifying_scope = NULL_TREE;
17020 parser->object_scope = NULL_TREE;
17026 /* Returns TRUE if the upcoming token sequence is the start of a
17027 constructor declarator. If FRIEND_P is true, the declarator is
17028 preceded by the `friend' specifier. */
17031 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17033 bool constructor_p;
17034 tree type_decl = NULL_TREE;
17035 bool nested_name_p;
17036 cp_token *next_token;
17038 /* The common case is that this is not a constructor declarator, so
17039 try to avoid doing lots of work if at all possible. It's not
17040 valid declare a constructor at function scope. */
17041 if (parser->in_function_body)
17043 /* And only certain tokens can begin a constructor declarator. */
17044 next_token = cp_lexer_peek_token (parser->lexer);
17045 if (next_token->type != CPP_NAME
17046 && next_token->type != CPP_SCOPE
17047 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17048 && next_token->type != CPP_TEMPLATE_ID)
17051 /* Parse tentatively; we are going to roll back all of the tokens
17053 cp_parser_parse_tentatively (parser);
17054 /* Assume that we are looking at a constructor declarator. */
17055 constructor_p = true;
17057 /* Look for the optional `::' operator. */
17058 cp_parser_global_scope_opt (parser,
17059 /*current_scope_valid_p=*/false);
17060 /* Look for the nested-name-specifier. */
17062 = (cp_parser_nested_name_specifier_opt (parser,
17063 /*typename_keyword_p=*/false,
17064 /*check_dependency_p=*/false,
17066 /*is_declaration=*/false)
17068 /* Outside of a class-specifier, there must be a
17069 nested-name-specifier. */
17070 if (!nested_name_p &&
17071 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17073 constructor_p = false;
17074 /* If we still think that this might be a constructor-declarator,
17075 look for a class-name. */
17080 template <typename T> struct S { S(); };
17081 template <typename T> S<T>::S ();
17083 we must recognize that the nested `S' names a class.
17086 template <typename T> S<T>::S<T> ();
17088 we must recognize that the nested `S' names a template. */
17089 type_decl = cp_parser_class_name (parser,
17090 /*typename_keyword_p=*/false,
17091 /*template_keyword_p=*/false,
17093 /*check_dependency_p=*/false,
17094 /*class_head_p=*/false,
17095 /*is_declaration=*/false);
17096 /* If there was no class-name, then this is not a constructor. */
17097 constructor_p = !cp_parser_error_occurred (parser);
17100 /* If we're still considering a constructor, we have to see a `(',
17101 to begin the parameter-declaration-clause, followed by either a
17102 `)', an `...', or a decl-specifier. We need to check for a
17103 type-specifier to avoid being fooled into thinking that:
17107 is a constructor. (It is actually a function named `f' that
17108 takes one parameter (of type `int') and returns a value of type
17111 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17114 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17115 /* A parameter declaration begins with a decl-specifier,
17116 which is either the "attribute" keyword, a storage class
17117 specifier, or (usually) a type-specifier. */
17118 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17121 tree pushed_scope = NULL_TREE;
17122 unsigned saved_num_template_parameter_lists;
17124 /* Names appearing in the type-specifier should be looked up
17125 in the scope of the class. */
17126 if (current_class_type)
17130 type = TREE_TYPE (type_decl);
17131 if (TREE_CODE (type) == TYPENAME_TYPE)
17133 type = resolve_typename_type (type,
17134 /*only_current_p=*/false);
17135 if (TREE_CODE (type) == TYPENAME_TYPE)
17137 cp_parser_abort_tentative_parse (parser);
17141 pushed_scope = push_scope (type);
17144 /* Inside the constructor parameter list, surrounding
17145 template-parameter-lists do not apply. */
17146 saved_num_template_parameter_lists
17147 = parser->num_template_parameter_lists;
17148 parser->num_template_parameter_lists = 0;
17150 /* Look for the type-specifier. */
17151 cp_parser_type_specifier (parser,
17152 CP_PARSER_FLAGS_NONE,
17153 /*decl_specs=*/NULL,
17154 /*is_declarator=*/true,
17155 /*declares_class_or_enum=*/NULL,
17156 /*is_cv_qualifier=*/NULL);
17158 parser->num_template_parameter_lists
17159 = saved_num_template_parameter_lists;
17161 /* Leave the scope of the class. */
17163 pop_scope (pushed_scope);
17165 constructor_p = !cp_parser_error_occurred (parser);
17169 constructor_p = false;
17170 /* We did not really want to consume any tokens. */
17171 cp_parser_abort_tentative_parse (parser);
17173 return constructor_p;
17176 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17177 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17178 they must be performed once we are in the scope of the function.
17180 Returns the function defined. */
17183 cp_parser_function_definition_from_specifiers_and_declarator
17184 (cp_parser* parser,
17185 cp_decl_specifier_seq *decl_specifiers,
17187 const cp_declarator *declarator)
17192 /* Begin the function-definition. */
17193 success_p = start_function (decl_specifiers, declarator, attributes);
17195 /* The things we're about to see are not directly qualified by any
17196 template headers we've seen thus far. */
17197 reset_specialization ();
17199 /* If there were names looked up in the decl-specifier-seq that we
17200 did not check, check them now. We must wait until we are in the
17201 scope of the function to perform the checks, since the function
17202 might be a friend. */
17203 perform_deferred_access_checks ();
17207 /* Skip the entire function. */
17208 cp_parser_skip_to_end_of_block_or_statement (parser);
17209 fn = error_mark_node;
17211 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17213 /* Seen already, skip it. An error message has already been output. */
17214 cp_parser_skip_to_end_of_block_or_statement (parser);
17215 fn = current_function_decl;
17216 current_function_decl = NULL_TREE;
17217 /* If this is a function from a class, pop the nested class. */
17218 if (current_class_name)
17219 pop_nested_class ();
17222 fn = cp_parser_function_definition_after_declarator (parser,
17223 /*inline_p=*/false);
17228 /* Parse the part of a function-definition that follows the
17229 declarator. INLINE_P is TRUE iff this function is an inline
17230 function defined with a class-specifier.
17232 Returns the function defined. */
17235 cp_parser_function_definition_after_declarator (cp_parser* parser,
17239 bool ctor_initializer_p = false;
17240 bool saved_in_unbraced_linkage_specification_p;
17241 bool saved_in_function_body;
17242 unsigned saved_num_template_parameter_lists;
17245 saved_in_function_body = parser->in_function_body;
17246 parser->in_function_body = true;
17247 /* If the next token is `return', then the code may be trying to
17248 make use of the "named return value" extension that G++ used to
17250 token = cp_lexer_peek_token (parser->lexer);
17251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17253 /* Consume the `return' keyword. */
17254 cp_lexer_consume_token (parser->lexer);
17255 /* Look for the identifier that indicates what value is to be
17257 cp_parser_identifier (parser);
17258 /* Issue an error message. */
17259 error ("%Hnamed return values are no longer supported",
17261 /* Skip tokens until we reach the start of the function body. */
17264 cp_token *token = cp_lexer_peek_token (parser->lexer);
17265 if (token->type == CPP_OPEN_BRACE
17266 || token->type == CPP_EOF
17267 || token->type == CPP_PRAGMA_EOL)
17269 cp_lexer_consume_token (parser->lexer);
17272 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17273 anything declared inside `f'. */
17274 saved_in_unbraced_linkage_specification_p
17275 = parser->in_unbraced_linkage_specification_p;
17276 parser->in_unbraced_linkage_specification_p = false;
17277 /* Inside the function, surrounding template-parameter-lists do not
17279 saved_num_template_parameter_lists
17280 = parser->num_template_parameter_lists;
17281 parser->num_template_parameter_lists = 0;
17282 /* If the next token is `try', then we are looking at a
17283 function-try-block. */
17284 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17285 ctor_initializer_p = cp_parser_function_try_block (parser);
17286 /* A function-try-block includes the function-body, so we only do
17287 this next part if we're not processing a function-try-block. */
17290 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17292 /* Finish the function. */
17293 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17294 (inline_p ? 2 : 0));
17295 /* Generate code for it, if necessary. */
17296 expand_or_defer_fn (fn);
17297 /* Restore the saved values. */
17298 parser->in_unbraced_linkage_specification_p
17299 = saved_in_unbraced_linkage_specification_p;
17300 parser->num_template_parameter_lists
17301 = saved_num_template_parameter_lists;
17302 parser->in_function_body = saved_in_function_body;
17307 /* Parse a template-declaration, assuming that the `export' (and
17308 `extern') keywords, if present, has already been scanned. MEMBER_P
17309 is as for cp_parser_template_declaration. */
17312 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17314 tree decl = NULL_TREE;
17315 VEC (deferred_access_check,gc) *checks;
17316 tree parameter_list;
17317 bool friend_p = false;
17318 bool need_lang_pop;
17321 /* Look for the `template' keyword. */
17322 token = cp_lexer_peek_token (parser->lexer);
17323 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17327 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17329 if (at_class_scope_p () && current_function_decl)
17331 /* 14.5.2.2 [temp.mem]
17333 A local class shall not have member templates. */
17334 error ("%Hinvalid declaration of member template in local class",
17336 cp_parser_skip_to_end_of_block_or_statement (parser);
17341 A template ... shall not have C linkage. */
17342 if (current_lang_name == lang_name_c)
17344 error ("%Htemplate with C linkage", &token->location);
17345 /* Give it C++ linkage to avoid confusing other parts of the
17347 push_lang_context (lang_name_cplusplus);
17348 need_lang_pop = true;
17351 need_lang_pop = false;
17353 /* We cannot perform access checks on the template parameter
17354 declarations until we know what is being declared, just as we
17355 cannot check the decl-specifier list. */
17356 push_deferring_access_checks (dk_deferred);
17358 /* If the next token is `>', then we have an invalid
17359 specialization. Rather than complain about an invalid template
17360 parameter, issue an error message here. */
17361 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17363 cp_parser_error (parser, "invalid explicit specialization");
17364 begin_specialization ();
17365 parameter_list = NULL_TREE;
17368 /* Parse the template parameters. */
17369 parameter_list = cp_parser_template_parameter_list (parser);
17371 /* Get the deferred access checks from the parameter list. These
17372 will be checked once we know what is being declared, as for a
17373 member template the checks must be performed in the scope of the
17374 class containing the member. */
17375 checks = get_deferred_access_checks ();
17377 /* Look for the `>'. */
17378 cp_parser_skip_to_end_of_template_parameter_list (parser);
17379 /* We just processed one more parameter list. */
17380 ++parser->num_template_parameter_lists;
17381 /* If the next token is `template', there are more template
17383 if (cp_lexer_next_token_is_keyword (parser->lexer,
17385 cp_parser_template_declaration_after_export (parser, member_p);
17388 /* There are no access checks when parsing a template, as we do not
17389 know if a specialization will be a friend. */
17390 push_deferring_access_checks (dk_no_check);
17391 token = cp_lexer_peek_token (parser->lexer);
17392 decl = cp_parser_single_declaration (parser,
17395 /*explicit_specialization_p=*/false,
17397 pop_deferring_access_checks ();
17399 /* If this is a member template declaration, let the front
17401 if (member_p && !friend_p && decl)
17403 if (TREE_CODE (decl) == TYPE_DECL)
17404 cp_parser_check_access_in_redeclaration (decl, token->location);
17406 decl = finish_member_template_decl (decl);
17408 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17409 make_friend_class (current_class_type, TREE_TYPE (decl),
17410 /*complain=*/true);
17412 /* We are done with the current parameter list. */
17413 --parser->num_template_parameter_lists;
17415 pop_deferring_access_checks ();
17418 finish_template_decl (parameter_list);
17420 /* Register member declarations. */
17421 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17422 finish_member_declaration (decl);
17423 /* For the erroneous case of a template with C linkage, we pushed an
17424 implicit C++ linkage scope; exit that scope now. */
17426 pop_lang_context ();
17427 /* If DECL is a function template, we must return to parse it later.
17428 (Even though there is no definition, there might be default
17429 arguments that need handling.) */
17430 if (member_p && decl
17431 && (TREE_CODE (decl) == FUNCTION_DECL
17432 || DECL_FUNCTION_TEMPLATE_P (decl)))
17433 TREE_VALUE (parser->unparsed_functions_queues)
17434 = tree_cons (NULL_TREE, decl,
17435 TREE_VALUE (parser->unparsed_functions_queues));
17438 /* Perform the deferred access checks from a template-parameter-list.
17439 CHECKS is a TREE_LIST of access checks, as returned by
17440 get_deferred_access_checks. */
17443 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17445 ++processing_template_parmlist;
17446 perform_access_checks (checks);
17447 --processing_template_parmlist;
17450 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17451 `function-definition' sequence. MEMBER_P is true, this declaration
17452 appears in a class scope.
17454 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17455 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17458 cp_parser_single_declaration (cp_parser* parser,
17459 VEC (deferred_access_check,gc)* checks,
17461 bool explicit_specialization_p,
17464 int declares_class_or_enum;
17465 tree decl = NULL_TREE;
17466 cp_decl_specifier_seq decl_specifiers;
17467 bool function_definition_p = false;
17468 cp_token *decl_spec_token_start;
17470 /* This function is only used when processing a template
17472 gcc_assert (innermost_scope_kind () == sk_template_parms
17473 || innermost_scope_kind () == sk_template_spec);
17475 /* Defer access checks until we know what is being declared. */
17476 push_deferring_access_checks (dk_deferred);
17478 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17480 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17481 cp_parser_decl_specifier_seq (parser,
17482 CP_PARSER_FLAGS_OPTIONAL,
17484 &declares_class_or_enum);
17486 *friend_p = cp_parser_friend_p (&decl_specifiers);
17488 /* There are no template typedefs. */
17489 if (decl_specifiers.specs[(int) ds_typedef])
17491 error ("%Htemplate declaration of %qs",
17492 &decl_spec_token_start->location, "typedef");
17493 decl = error_mark_node;
17496 /* Gather up the access checks that occurred the
17497 decl-specifier-seq. */
17498 stop_deferring_access_checks ();
17500 /* Check for the declaration of a template class. */
17501 if (declares_class_or_enum)
17503 if (cp_parser_declares_only_class_p (parser))
17505 decl = shadow_tag (&decl_specifiers);
17510 friend template <typename T> struct A<T>::B;
17513 A<T>::B will be represented by a TYPENAME_TYPE, and
17514 therefore not recognized by shadow_tag. */
17515 if (friend_p && *friend_p
17517 && decl_specifiers.type
17518 && TYPE_P (decl_specifiers.type))
17519 decl = decl_specifiers.type;
17521 if (decl && decl != error_mark_node)
17522 decl = TYPE_NAME (decl);
17524 decl = error_mark_node;
17526 /* Perform access checks for template parameters. */
17527 cp_parser_perform_template_parameter_access_checks (checks);
17530 /* If it's not a template class, try for a template function. If
17531 the next token is a `;', then this declaration does not declare
17532 anything. But, if there were errors in the decl-specifiers, then
17533 the error might well have come from an attempted class-specifier.
17534 In that case, there's no need to warn about a missing declarator. */
17536 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17537 || decl_specifiers.type != error_mark_node))
17539 decl = cp_parser_init_declarator (parser,
17542 /*function_definition_allowed_p=*/true,
17544 declares_class_or_enum,
17545 &function_definition_p);
17547 /* 7.1.1-1 [dcl.stc]
17549 A storage-class-specifier shall not be specified in an explicit
17550 specialization... */
17552 && explicit_specialization_p
17553 && decl_specifiers.storage_class != sc_none)
17555 error ("%Hexplicit template specialization cannot have a storage class",
17556 &decl_spec_token_start->location);
17557 decl = error_mark_node;
17561 pop_deferring_access_checks ();
17563 /* Clear any current qualification; whatever comes next is the start
17564 of something new. */
17565 parser->scope = NULL_TREE;
17566 parser->qualifying_scope = NULL_TREE;
17567 parser->object_scope = NULL_TREE;
17568 /* Look for a trailing `;' after the declaration. */
17569 if (!function_definition_p
17570 && (decl == error_mark_node
17571 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17572 cp_parser_skip_to_end_of_block_or_statement (parser);
17577 /* Parse a cast-expression that is not the operand of a unary "&". */
17580 cp_parser_simple_cast_expression (cp_parser *parser)
17582 return cp_parser_cast_expression (parser, /*address_p=*/false,
17586 /* Parse a functional cast to TYPE. Returns an expression
17587 representing the cast. */
17590 cp_parser_functional_cast (cp_parser* parser, tree type)
17592 tree expression_list;
17596 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17598 maybe_warn_cpp0x ("extended initializer lists");
17599 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17600 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17601 if (TREE_CODE (type) == TYPE_DECL)
17602 type = TREE_TYPE (type);
17603 return finish_compound_literal (type, expression_list);
17607 = cp_parser_parenthesized_expression_list (parser, false,
17609 /*allow_expansion_p=*/true,
17610 /*non_constant_p=*/NULL);
17612 cast = build_functional_cast (type, expression_list,
17613 tf_warning_or_error);
17614 /* [expr.const]/1: In an integral constant expression "only type
17615 conversions to integral or enumeration type can be used". */
17616 if (TREE_CODE (type) == TYPE_DECL)
17617 type = TREE_TYPE (type);
17618 if (cast != error_mark_node
17619 && !cast_valid_in_integral_constant_expression_p (type)
17620 && (cp_parser_non_integral_constant_expression
17621 (parser, "a call to a constructor")))
17622 return error_mark_node;
17626 /* Save the tokens that make up the body of a member function defined
17627 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17628 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17629 specifiers applied to the declaration. Returns the FUNCTION_DECL
17630 for the member function. */
17633 cp_parser_save_member_function_body (cp_parser* parser,
17634 cp_decl_specifier_seq *decl_specifiers,
17635 cp_declarator *declarator,
17642 /* Create the function-declaration. */
17643 fn = start_method (decl_specifiers, declarator, attributes);
17644 /* If something went badly wrong, bail out now. */
17645 if (fn == error_mark_node)
17647 /* If there's a function-body, skip it. */
17648 if (cp_parser_token_starts_function_definition_p
17649 (cp_lexer_peek_token (parser->lexer)))
17650 cp_parser_skip_to_end_of_block_or_statement (parser);
17651 return error_mark_node;
17654 /* Remember it, if there default args to post process. */
17655 cp_parser_save_default_args (parser, fn);
17657 /* Save away the tokens that make up the body of the
17659 first = parser->lexer->next_token;
17660 /* We can have braced-init-list mem-initializers before the fn body. */
17661 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17663 cp_lexer_consume_token (parser->lexer);
17664 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17665 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17667 /* cache_group will stop after an un-nested { } pair, too. */
17668 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17671 /* variadic mem-inits have ... after the ')'. */
17672 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17673 cp_lexer_consume_token (parser->lexer);
17676 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17677 /* Handle function try blocks. */
17678 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17679 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17680 last = parser->lexer->next_token;
17682 /* Save away the inline definition; we will process it when the
17683 class is complete. */
17684 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17685 DECL_PENDING_INLINE_P (fn) = 1;
17687 /* We need to know that this was defined in the class, so that
17688 friend templates are handled correctly. */
17689 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17691 /* We're done with the inline definition. */
17692 finish_method (fn);
17694 /* Add FN to the queue of functions to be parsed later. */
17695 TREE_VALUE (parser->unparsed_functions_queues)
17696 = tree_cons (NULL_TREE, fn,
17697 TREE_VALUE (parser->unparsed_functions_queues));
17702 /* Parse a template-argument-list, as well as the trailing ">" (but
17703 not the opening ">"). See cp_parser_template_argument_list for the
17707 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17711 tree saved_qualifying_scope;
17712 tree saved_object_scope;
17713 bool saved_greater_than_is_operator_p;
17714 bool saved_skip_evaluation;
17718 When parsing a template-id, the first non-nested `>' is taken as
17719 the end of the template-argument-list rather than a greater-than
17721 saved_greater_than_is_operator_p
17722 = parser->greater_than_is_operator_p;
17723 parser->greater_than_is_operator_p = false;
17724 /* Parsing the argument list may modify SCOPE, so we save it
17726 saved_scope = parser->scope;
17727 saved_qualifying_scope = parser->qualifying_scope;
17728 saved_object_scope = parser->object_scope;
17729 /* We need to evaluate the template arguments, even though this
17730 template-id may be nested within a "sizeof". */
17731 saved_skip_evaluation = skip_evaluation;
17732 skip_evaluation = false;
17733 /* Parse the template-argument-list itself. */
17734 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17735 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17736 arguments = NULL_TREE;
17738 arguments = cp_parser_template_argument_list (parser);
17739 /* Look for the `>' that ends the template-argument-list. If we find
17740 a '>>' instead, it's probably just a typo. */
17741 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17743 if (cxx_dialect != cxx98)
17745 /* In C++0x, a `>>' in a template argument list or cast
17746 expression is considered to be two separate `>'
17747 tokens. So, change the current token to a `>', but don't
17748 consume it: it will be consumed later when the outer
17749 template argument list (or cast expression) is parsed.
17750 Note that this replacement of `>' for `>>' is necessary
17751 even if we are parsing tentatively: in the tentative
17752 case, after calling
17753 cp_parser_enclosed_template_argument_list we will always
17754 throw away all of the template arguments and the first
17755 closing `>', either because the template argument list
17756 was erroneous or because we are replacing those tokens
17757 with a CPP_TEMPLATE_ID token. The second `>' (which will
17758 not have been thrown away) is needed either to close an
17759 outer template argument list or to complete a new-style
17761 cp_token *token = cp_lexer_peek_token (parser->lexer);
17762 token->type = CPP_GREATER;
17764 else if (!saved_greater_than_is_operator_p)
17766 /* If we're in a nested template argument list, the '>>' has
17767 to be a typo for '> >'. We emit the error message, but we
17768 continue parsing and we push a '>' as next token, so that
17769 the argument list will be parsed correctly. Note that the
17770 global source location is still on the token before the
17771 '>>', so we need to say explicitly where we want it. */
17772 cp_token *token = cp_lexer_peek_token (parser->lexer);
17773 error ("%H%<>>%> should be %<> >%> "
17774 "within a nested template argument list",
17777 token->type = CPP_GREATER;
17781 /* If this is not a nested template argument list, the '>>'
17782 is a typo for '>'. Emit an error message and continue.
17783 Same deal about the token location, but here we can get it
17784 right by consuming the '>>' before issuing the diagnostic. */
17785 cp_token *token = cp_lexer_consume_token (parser->lexer);
17786 error ("%Hspurious %<>>%>, use %<>%> to terminate "
17787 "a template argument list", &token->location);
17791 cp_parser_skip_to_end_of_template_parameter_list (parser);
17792 /* The `>' token might be a greater-than operator again now. */
17793 parser->greater_than_is_operator_p
17794 = saved_greater_than_is_operator_p;
17795 /* Restore the SAVED_SCOPE. */
17796 parser->scope = saved_scope;
17797 parser->qualifying_scope = saved_qualifying_scope;
17798 parser->object_scope = saved_object_scope;
17799 skip_evaluation = saved_skip_evaluation;
17804 /* MEMBER_FUNCTION is a member function, or a friend. If default
17805 arguments, or the body of the function have not yet been parsed,
17809 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17811 /* If this member is a template, get the underlying
17813 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17814 member_function = DECL_TEMPLATE_RESULT (member_function);
17816 /* There should not be any class definitions in progress at this
17817 point; the bodies of members are only parsed outside of all class
17819 gcc_assert (parser->num_classes_being_defined == 0);
17820 /* While we're parsing the member functions we might encounter more
17821 classes. We want to handle them right away, but we don't want
17822 them getting mixed up with functions that are currently in the
17824 parser->unparsed_functions_queues
17825 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17827 /* Make sure that any template parameters are in scope. */
17828 maybe_begin_member_template_processing (member_function);
17830 /* If the body of the function has not yet been parsed, parse it
17832 if (DECL_PENDING_INLINE_P (member_function))
17834 tree function_scope;
17835 cp_token_cache *tokens;
17837 /* The function is no longer pending; we are processing it. */
17838 tokens = DECL_PENDING_INLINE_INFO (member_function);
17839 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17840 DECL_PENDING_INLINE_P (member_function) = 0;
17842 /* If this is a local class, enter the scope of the containing
17844 function_scope = current_function_decl;
17845 if (function_scope)
17846 push_function_context ();
17848 /* Push the body of the function onto the lexer stack. */
17849 cp_parser_push_lexer_for_tokens (parser, tokens);
17851 /* Let the front end know that we going to be defining this
17853 start_preparsed_function (member_function, NULL_TREE,
17854 SF_PRE_PARSED | SF_INCLASS_INLINE);
17856 /* Don't do access checking if it is a templated function. */
17857 if (processing_template_decl)
17858 push_deferring_access_checks (dk_no_check);
17860 /* Now, parse the body of the function. */
17861 cp_parser_function_definition_after_declarator (parser,
17862 /*inline_p=*/true);
17864 if (processing_template_decl)
17865 pop_deferring_access_checks ();
17867 /* Leave the scope of the containing function. */
17868 if (function_scope)
17869 pop_function_context ();
17870 cp_parser_pop_lexer (parser);
17873 /* Remove any template parameters from the symbol table. */
17874 maybe_end_member_template_processing ();
17876 /* Restore the queue. */
17877 parser->unparsed_functions_queues
17878 = TREE_CHAIN (parser->unparsed_functions_queues);
17881 /* If DECL contains any default args, remember it on the unparsed
17882 functions queue. */
17885 cp_parser_save_default_args (cp_parser* parser, tree decl)
17889 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17891 probe = TREE_CHAIN (probe))
17892 if (TREE_PURPOSE (probe))
17894 TREE_PURPOSE (parser->unparsed_functions_queues)
17895 = tree_cons (current_class_type, decl,
17896 TREE_PURPOSE (parser->unparsed_functions_queues));
17901 /* FN is a FUNCTION_DECL which may contains a parameter with an
17902 unparsed DEFAULT_ARG. Parse the default args now. This function
17903 assumes that the current scope is the scope in which the default
17904 argument should be processed. */
17907 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17909 bool saved_local_variables_forbidden_p;
17912 /* While we're parsing the default args, we might (due to the
17913 statement expression extension) encounter more classes. We want
17914 to handle them right away, but we don't want them getting mixed
17915 up with default args that are currently in the queue. */
17916 parser->unparsed_functions_queues
17917 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17919 /* Local variable names (and the `this' keyword) may not appear
17920 in a default argument. */
17921 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17922 parser->local_variables_forbidden_p = true;
17924 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17926 parm = TREE_CHAIN (parm))
17928 cp_token_cache *tokens;
17929 tree default_arg = TREE_PURPOSE (parm);
17931 VEC(tree,gc) *insts;
17938 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17939 /* This can happen for a friend declaration for a function
17940 already declared with default arguments. */
17943 /* Push the saved tokens for the default argument onto the parser's
17945 tokens = DEFARG_TOKENS (default_arg);
17946 cp_parser_push_lexer_for_tokens (parser, tokens);
17948 /* Parse the assignment-expression. */
17949 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17951 if (!processing_template_decl)
17952 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17954 TREE_PURPOSE (parm) = parsed_arg;
17956 /* Update any instantiations we've already created. */
17957 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17958 VEC_iterate (tree, insts, ix, copy); ix++)
17959 TREE_PURPOSE (copy) = parsed_arg;
17961 /* If the token stream has not been completely used up, then
17962 there was extra junk after the end of the default
17964 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17965 cp_parser_error (parser, "expected %<,%>");
17967 /* Revert to the main lexer. */
17968 cp_parser_pop_lexer (parser);
17971 /* Make sure no default arg is missing. */
17972 check_default_args (fn);
17974 /* Restore the state of local_variables_forbidden_p. */
17975 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17977 /* Restore the queue. */
17978 parser->unparsed_functions_queues
17979 = TREE_CHAIN (parser->unparsed_functions_queues);
17982 /* Parse the operand of `sizeof' (or a similar operator). Returns
17983 either a TYPE or an expression, depending on the form of the
17984 input. The KEYWORD indicates which kind of expression we have
17988 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17990 tree expr = NULL_TREE;
17991 const char *saved_message;
17993 bool saved_integral_constant_expression_p;
17994 bool saved_non_integral_constant_expression_p;
17995 bool pack_expansion_p = false;
17997 /* Types cannot be defined in a `sizeof' expression. Save away the
17999 saved_message = parser->type_definition_forbidden_message;
18000 /* And create the new one. */
18001 tmp = concat ("types may not be defined in %<",
18002 IDENTIFIER_POINTER (ridpointers[keyword]),
18003 "%> expressions", NULL);
18004 parser->type_definition_forbidden_message = tmp;
18006 /* The restrictions on constant-expressions do not apply inside
18007 sizeof expressions. */
18008 saved_integral_constant_expression_p
18009 = parser->integral_constant_expression_p;
18010 saved_non_integral_constant_expression_p
18011 = parser->non_integral_constant_expression_p;
18012 parser->integral_constant_expression_p = false;
18014 /* If it's a `...', then we are computing the length of a parameter
18016 if (keyword == RID_SIZEOF
18017 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18019 /* Consume the `...'. */
18020 cp_lexer_consume_token (parser->lexer);
18021 maybe_warn_variadic_templates ();
18023 /* Note that this is an expansion. */
18024 pack_expansion_p = true;
18027 /* Do not actually evaluate the expression. */
18029 /* If it's a `(', then we might be looking at the type-id
18031 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18034 bool saved_in_type_id_in_expr_p;
18036 /* We can't be sure yet whether we're looking at a type-id or an
18038 cp_parser_parse_tentatively (parser);
18039 /* Consume the `('. */
18040 cp_lexer_consume_token (parser->lexer);
18041 /* Parse the type-id. */
18042 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18043 parser->in_type_id_in_expr_p = true;
18044 type = cp_parser_type_id (parser);
18045 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18046 /* Now, look for the trailing `)'. */
18047 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18048 /* If all went well, then we're done. */
18049 if (cp_parser_parse_definitely (parser))
18051 cp_decl_specifier_seq decl_specs;
18053 /* Build a trivial decl-specifier-seq. */
18054 clear_decl_specs (&decl_specs);
18055 decl_specs.type = type;
18057 /* Call grokdeclarator to figure out what type this is. */
18058 expr = grokdeclarator (NULL,
18062 /*attrlist=*/NULL);
18066 /* If the type-id production did not work out, then we must be
18067 looking at the unary-expression production. */
18069 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18072 if (pack_expansion_p)
18073 /* Build a pack expansion. */
18074 expr = make_pack_expansion (expr);
18076 /* Go back to evaluating expressions. */
18079 /* Free the message we created. */
18081 /* And restore the old one. */
18082 parser->type_definition_forbidden_message = saved_message;
18083 parser->integral_constant_expression_p
18084 = saved_integral_constant_expression_p;
18085 parser->non_integral_constant_expression_p
18086 = saved_non_integral_constant_expression_p;
18091 /* If the current declaration has no declarator, return true. */
18094 cp_parser_declares_only_class_p (cp_parser *parser)
18096 /* If the next token is a `;' or a `,' then there is no
18098 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18099 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18102 /* Update the DECL_SPECS to reflect the storage class indicated by
18106 cp_parser_set_storage_class (cp_parser *parser,
18107 cp_decl_specifier_seq *decl_specs,
18109 location_t location)
18111 cp_storage_class storage_class;
18113 if (parser->in_unbraced_linkage_specification_p)
18115 error ("%Hinvalid use of %qD in linkage specification",
18116 &location, ridpointers[keyword]);
18119 else if (decl_specs->storage_class != sc_none)
18121 decl_specs->conflicting_specifiers_p = true;
18125 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18126 && decl_specs->specs[(int) ds_thread])
18128 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18129 decl_specs->specs[(int) ds_thread] = 0;
18135 storage_class = sc_auto;
18138 storage_class = sc_register;
18141 storage_class = sc_static;
18144 storage_class = sc_extern;
18147 storage_class = sc_mutable;
18150 gcc_unreachable ();
18152 decl_specs->storage_class = storage_class;
18154 /* A storage class specifier cannot be applied alongside a typedef
18155 specifier. If there is a typedef specifier present then set
18156 conflicting_specifiers_p which will trigger an error later
18157 on in grokdeclarator. */
18158 if (decl_specs->specs[(int)ds_typedef])
18159 decl_specs->conflicting_specifiers_p = true;
18162 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18163 is true, the type is a user-defined type; otherwise it is a
18164 built-in type specified by a keyword. */
18167 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18169 location_t location,
18170 bool user_defined_p)
18172 decl_specs->any_specifiers_p = true;
18174 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18175 (with, for example, in "typedef int wchar_t;") we remember that
18176 this is what happened. In system headers, we ignore these
18177 declarations so that G++ can work with system headers that are not
18179 if (decl_specs->specs[(int) ds_typedef]
18181 && (type_spec == boolean_type_node
18182 || type_spec == char16_type_node
18183 || type_spec == char32_type_node
18184 || type_spec == wchar_type_node)
18185 && (decl_specs->type
18186 || decl_specs->specs[(int) ds_long]
18187 || decl_specs->specs[(int) ds_short]
18188 || decl_specs->specs[(int) ds_unsigned]
18189 || decl_specs->specs[(int) ds_signed]))
18191 decl_specs->redefined_builtin_type = type_spec;
18192 if (!decl_specs->type)
18194 decl_specs->type = type_spec;
18195 decl_specs->user_defined_type_p = false;
18196 decl_specs->type_location = location;
18199 else if (decl_specs->type)
18200 decl_specs->multiple_types_p = true;
18203 decl_specs->type = type_spec;
18204 decl_specs->user_defined_type_p = user_defined_p;
18205 decl_specs->redefined_builtin_type = NULL_TREE;
18206 decl_specs->type_location = location;
18210 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18211 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18214 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18216 return decl_specifiers->specs[(int) ds_friend] != 0;
18219 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18220 issue an error message indicating that TOKEN_DESC was expected.
18222 Returns the token consumed, if the token had the appropriate type.
18223 Otherwise, returns NULL. */
18226 cp_parser_require (cp_parser* parser,
18227 enum cpp_ttype type,
18228 const char* token_desc)
18230 if (cp_lexer_next_token_is (parser->lexer, type))
18231 return cp_lexer_consume_token (parser->lexer);
18234 /* Output the MESSAGE -- unless we're parsing tentatively. */
18235 if (!cp_parser_simulate_error (parser))
18237 char *message = concat ("expected ", token_desc, NULL);
18238 cp_parser_error (parser, message);
18245 /* An error message is produced if the next token is not '>'.
18246 All further tokens are skipped until the desired token is
18247 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18250 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18252 /* Current level of '< ... >'. */
18253 unsigned level = 0;
18254 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18255 unsigned nesting_depth = 0;
18257 /* Are we ready, yet? If not, issue error message. */
18258 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18261 /* Skip tokens until the desired token is found. */
18264 /* Peek at the next token. */
18265 switch (cp_lexer_peek_token (parser->lexer)->type)
18268 if (!nesting_depth)
18273 if (cxx_dialect == cxx98)
18274 /* C++0x views the `>>' operator as two `>' tokens, but
18277 else if (!nesting_depth && level-- == 0)
18279 /* We've hit a `>>' where the first `>' closes the
18280 template argument list, and the second `>' is
18281 spurious. Just consume the `>>' and stop; we've
18282 already produced at least one error. */
18283 cp_lexer_consume_token (parser->lexer);
18286 /* Fall through for C++0x, so we handle the second `>' in
18290 if (!nesting_depth && level-- == 0)
18292 /* We've reached the token we want, consume it and stop. */
18293 cp_lexer_consume_token (parser->lexer);
18298 case CPP_OPEN_PAREN:
18299 case CPP_OPEN_SQUARE:
18303 case CPP_CLOSE_PAREN:
18304 case CPP_CLOSE_SQUARE:
18305 if (nesting_depth-- == 0)
18310 case CPP_PRAGMA_EOL:
18311 case CPP_SEMICOLON:
18312 case CPP_OPEN_BRACE:
18313 case CPP_CLOSE_BRACE:
18314 /* The '>' was probably forgotten, don't look further. */
18321 /* Consume this token. */
18322 cp_lexer_consume_token (parser->lexer);
18326 /* If the next token is the indicated keyword, consume it. Otherwise,
18327 issue an error message indicating that TOKEN_DESC was expected.
18329 Returns the token consumed, if the token had the appropriate type.
18330 Otherwise, returns NULL. */
18333 cp_parser_require_keyword (cp_parser* parser,
18335 const char* token_desc)
18337 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18339 if (token && token->keyword != keyword)
18341 dyn_string_t error_msg;
18343 /* Format the error message. */
18344 error_msg = dyn_string_new (0);
18345 dyn_string_append_cstr (error_msg, "expected ");
18346 dyn_string_append_cstr (error_msg, token_desc);
18347 cp_parser_error (parser, error_msg->s);
18348 dyn_string_delete (error_msg);
18355 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18356 function-definition. */
18359 cp_parser_token_starts_function_definition_p (cp_token* token)
18361 return (/* An ordinary function-body begins with an `{'. */
18362 token->type == CPP_OPEN_BRACE
18363 /* A ctor-initializer begins with a `:'. */
18364 || token->type == CPP_COLON
18365 /* A function-try-block begins with `try'. */
18366 || token->keyword == RID_TRY
18367 /* The named return value extension begins with `return'. */
18368 || token->keyword == RID_RETURN);
18371 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18375 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18379 token = cp_lexer_peek_token (parser->lexer);
18380 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18383 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18384 C++0x) ending a template-argument. */
18387 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18391 token = cp_lexer_peek_token (parser->lexer);
18392 return (token->type == CPP_COMMA
18393 || token->type == CPP_GREATER
18394 || token->type == CPP_ELLIPSIS
18395 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18398 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18399 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18402 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18407 token = cp_lexer_peek_nth_token (parser->lexer, n);
18408 if (token->type == CPP_LESS)
18410 /* Check for the sequence `<::' in the original code. It would be lexed as
18411 `[:', where `[' is a digraph, and there is no whitespace before
18413 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18416 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18417 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18423 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18424 or none_type otherwise. */
18426 static enum tag_types
18427 cp_parser_token_is_class_key (cp_token* token)
18429 switch (token->keyword)
18434 return record_type;
18443 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18446 cp_parser_check_class_key (enum tag_types class_key, tree type)
18448 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18449 permerror ("%qs tag used in naming %q#T",
18450 class_key == union_type ? "union"
18451 : class_key == record_type ? "struct" : "class",
18455 /* Issue an error message if DECL is redeclared with different
18456 access than its original declaration [class.access.spec/3].
18457 This applies to nested classes and nested class templates.
18461 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18463 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18466 if ((TREE_PRIVATE (decl)
18467 != (current_access_specifier == access_private_node))
18468 || (TREE_PROTECTED (decl)
18469 != (current_access_specifier == access_protected_node)))
18470 error ("%H%qD redeclared with different access", &location, decl);
18473 /* Look for the `template' keyword, as a syntactic disambiguator.
18474 Return TRUE iff it is present, in which case it will be
18478 cp_parser_optional_template_keyword (cp_parser *parser)
18480 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18482 /* The `template' keyword can only be used within templates;
18483 outside templates the parser can always figure out what is a
18484 template and what is not. */
18485 if (!processing_template_decl)
18487 cp_token *token = cp_lexer_peek_token (parser->lexer);
18488 error ("%H%<template%> (as a disambiguator) is only allowed "
18489 "within templates", &token->location);
18490 /* If this part of the token stream is rescanned, the same
18491 error message would be generated. So, we purge the token
18492 from the stream. */
18493 cp_lexer_purge_token (parser->lexer);
18498 /* Consume the `template' keyword. */
18499 cp_lexer_consume_token (parser->lexer);
18507 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18508 set PARSER->SCOPE, and perform other related actions. */
18511 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18514 struct tree_check *check_value;
18515 deferred_access_check *chk;
18516 VEC (deferred_access_check,gc) *checks;
18518 /* Get the stored value. */
18519 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18520 /* Perform any access checks that were deferred. */
18521 checks = check_value->checks;
18525 VEC_iterate (deferred_access_check, checks, i, chk) ;
18528 perform_or_defer_access_check (chk->binfo,
18533 /* Set the scope from the stored value. */
18534 parser->scope = check_value->value;
18535 parser->qualifying_scope = check_value->qualifying_scope;
18536 parser->object_scope = NULL_TREE;
18539 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18540 encounter the end of a block before what we were looking for. */
18543 cp_parser_cache_group (cp_parser *parser,
18544 enum cpp_ttype end,
18549 cp_token *token = cp_lexer_peek_token (parser->lexer);
18551 /* Abort a parenthesized expression if we encounter a semicolon. */
18552 if ((end == CPP_CLOSE_PAREN || depth == 0)
18553 && token->type == CPP_SEMICOLON)
18555 /* If we've reached the end of the file, stop. */
18556 if (token->type == CPP_EOF
18557 || (end != CPP_PRAGMA_EOL
18558 && token->type == CPP_PRAGMA_EOL))
18560 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18561 /* We've hit the end of an enclosing block, so there's been some
18562 kind of syntax error. */
18565 /* Consume the token. */
18566 cp_lexer_consume_token (parser->lexer);
18567 /* See if it starts a new group. */
18568 if (token->type == CPP_OPEN_BRACE)
18570 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18571 /* In theory this should probably check end == '}', but
18572 cp_parser_save_member_function_body needs it to exit
18573 after either '}' or ')' when called with ')'. */
18577 else if (token->type == CPP_OPEN_PAREN)
18579 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18580 if (depth == 0 && end == CPP_CLOSE_PAREN)
18583 else if (token->type == CPP_PRAGMA)
18584 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18585 else if (token->type == end)
18590 /* Begin parsing tentatively. We always save tokens while parsing
18591 tentatively so that if the tentative parsing fails we can restore the
18595 cp_parser_parse_tentatively (cp_parser* parser)
18597 /* Enter a new parsing context. */
18598 parser->context = cp_parser_context_new (parser->context);
18599 /* Begin saving tokens. */
18600 cp_lexer_save_tokens (parser->lexer);
18601 /* In order to avoid repetitive access control error messages,
18602 access checks are queued up until we are no longer parsing
18604 push_deferring_access_checks (dk_deferred);
18607 /* Commit to the currently active tentative parse. */
18610 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18612 cp_parser_context *context;
18615 /* Mark all of the levels as committed. */
18616 lexer = parser->lexer;
18617 for (context = parser->context; context->next; context = context->next)
18619 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18621 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18622 while (!cp_lexer_saving_tokens (lexer))
18623 lexer = lexer->next;
18624 cp_lexer_commit_tokens (lexer);
18628 /* Abort the currently active tentative parse. All consumed tokens
18629 will be rolled back, and no diagnostics will be issued. */
18632 cp_parser_abort_tentative_parse (cp_parser* parser)
18634 cp_parser_simulate_error (parser);
18635 /* Now, pretend that we want to see if the construct was
18636 successfully parsed. */
18637 cp_parser_parse_definitely (parser);
18640 /* Stop parsing tentatively. If a parse error has occurred, restore the
18641 token stream. Otherwise, commit to the tokens we have consumed.
18642 Returns true if no error occurred; false otherwise. */
18645 cp_parser_parse_definitely (cp_parser* parser)
18647 bool error_occurred;
18648 cp_parser_context *context;
18650 /* Remember whether or not an error occurred, since we are about to
18651 destroy that information. */
18652 error_occurred = cp_parser_error_occurred (parser);
18653 /* Remove the topmost context from the stack. */
18654 context = parser->context;
18655 parser->context = context->next;
18656 /* If no parse errors occurred, commit to the tentative parse. */
18657 if (!error_occurred)
18659 /* Commit to the tokens read tentatively, unless that was
18661 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18662 cp_lexer_commit_tokens (parser->lexer);
18664 pop_to_parent_deferring_access_checks ();
18666 /* Otherwise, if errors occurred, roll back our state so that things
18667 are just as they were before we began the tentative parse. */
18670 cp_lexer_rollback_tokens (parser->lexer);
18671 pop_deferring_access_checks ();
18673 /* Add the context to the front of the free list. */
18674 context->next = cp_parser_context_free_list;
18675 cp_parser_context_free_list = context;
18677 return !error_occurred;
18680 /* Returns true if we are parsing tentatively and are not committed to
18681 this tentative parse. */
18684 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18686 return (cp_parser_parsing_tentatively (parser)
18687 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18690 /* Returns nonzero iff an error has occurred during the most recent
18691 tentative parse. */
18694 cp_parser_error_occurred (cp_parser* parser)
18696 return (cp_parser_parsing_tentatively (parser)
18697 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18700 /* Returns nonzero if GNU extensions are allowed. */
18703 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18705 return parser->allow_gnu_extensions_p;
18708 /* Objective-C++ Productions */
18711 /* Parse an Objective-C expression, which feeds into a primary-expression
18715 objc-message-expression
18716 objc-string-literal
18717 objc-encode-expression
18718 objc-protocol-expression
18719 objc-selector-expression
18721 Returns a tree representation of the expression. */
18724 cp_parser_objc_expression (cp_parser* parser)
18726 /* Try to figure out what kind of declaration is present. */
18727 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18731 case CPP_OPEN_SQUARE:
18732 return cp_parser_objc_message_expression (parser);
18734 case CPP_OBJC_STRING:
18735 kwd = cp_lexer_consume_token (parser->lexer);
18736 return objc_build_string_object (kwd->u.value);
18739 switch (kwd->keyword)
18741 case RID_AT_ENCODE:
18742 return cp_parser_objc_encode_expression (parser);
18744 case RID_AT_PROTOCOL:
18745 return cp_parser_objc_protocol_expression (parser);
18747 case RID_AT_SELECTOR:
18748 return cp_parser_objc_selector_expression (parser);
18754 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18755 &kwd->location, kwd->u.value);
18756 cp_parser_skip_to_end_of_block_or_statement (parser);
18759 return error_mark_node;
18762 /* Parse an Objective-C message expression.
18764 objc-message-expression:
18765 [ objc-message-receiver objc-message-args ]
18767 Returns a representation of an Objective-C message. */
18770 cp_parser_objc_message_expression (cp_parser* parser)
18772 tree receiver, messageargs;
18774 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18775 receiver = cp_parser_objc_message_receiver (parser);
18776 messageargs = cp_parser_objc_message_args (parser);
18777 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18779 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18782 /* Parse an objc-message-receiver.
18784 objc-message-receiver:
18786 simple-type-specifier
18788 Returns a representation of the type or expression. */
18791 cp_parser_objc_message_receiver (cp_parser* parser)
18795 /* An Objective-C message receiver may be either (1) a type
18796 or (2) an expression. */
18797 cp_parser_parse_tentatively (parser);
18798 rcv = cp_parser_expression (parser, false);
18800 if (cp_parser_parse_definitely (parser))
18803 rcv = cp_parser_simple_type_specifier (parser,
18804 /*decl_specs=*/NULL,
18805 CP_PARSER_FLAGS_NONE);
18807 return objc_get_class_reference (rcv);
18810 /* Parse the arguments and selectors comprising an Objective-C message.
18815 objc-selector-args , objc-comma-args
18817 objc-selector-args:
18818 objc-selector [opt] : assignment-expression
18819 objc-selector-args objc-selector [opt] : assignment-expression
18822 assignment-expression
18823 objc-comma-args , assignment-expression
18825 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18826 selector arguments and TREE_VALUE containing a list of comma
18830 cp_parser_objc_message_args (cp_parser* parser)
18832 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18833 bool maybe_unary_selector_p = true;
18834 cp_token *token = cp_lexer_peek_token (parser->lexer);
18836 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18838 tree selector = NULL_TREE, arg;
18840 if (token->type != CPP_COLON)
18841 selector = cp_parser_objc_selector (parser);
18843 /* Detect if we have a unary selector. */
18844 if (maybe_unary_selector_p
18845 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18846 return build_tree_list (selector, NULL_TREE);
18848 maybe_unary_selector_p = false;
18849 cp_parser_require (parser, CPP_COLON, "%<:%>");
18850 arg = cp_parser_assignment_expression (parser, false);
18853 = chainon (sel_args,
18854 build_tree_list (selector, arg));
18856 token = cp_lexer_peek_token (parser->lexer);
18859 /* Handle non-selector arguments, if any. */
18860 while (token->type == CPP_COMMA)
18864 cp_lexer_consume_token (parser->lexer);
18865 arg = cp_parser_assignment_expression (parser, false);
18868 = chainon (addl_args,
18869 build_tree_list (NULL_TREE, arg));
18871 token = cp_lexer_peek_token (parser->lexer);
18874 return build_tree_list (sel_args, addl_args);
18877 /* Parse an Objective-C encode expression.
18879 objc-encode-expression:
18880 @encode objc-typename
18882 Returns an encoded representation of the type argument. */
18885 cp_parser_objc_encode_expression (cp_parser* parser)
18890 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18891 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18892 token = cp_lexer_peek_token (parser->lexer);
18893 type = complete_type (cp_parser_type_id (parser));
18894 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18898 error ("%H%<@encode%> must specify a type as an argument",
18900 return error_mark_node;
18903 return objc_build_encode_expr (type);
18906 /* Parse an Objective-C @defs expression. */
18909 cp_parser_objc_defs_expression (cp_parser *parser)
18913 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18914 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18915 name = cp_parser_identifier (parser);
18916 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18918 return objc_get_class_ivars (name);
18921 /* Parse an Objective-C protocol expression.
18923 objc-protocol-expression:
18924 @protocol ( identifier )
18926 Returns a representation of the protocol expression. */
18929 cp_parser_objc_protocol_expression (cp_parser* parser)
18933 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18934 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18935 proto = cp_parser_identifier (parser);
18936 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18938 return objc_build_protocol_expr (proto);
18941 /* Parse an Objective-C selector expression.
18943 objc-selector-expression:
18944 @selector ( objc-method-signature )
18946 objc-method-signature:
18952 objc-selector-seq objc-selector :
18954 Returns a representation of the method selector. */
18957 cp_parser_objc_selector_expression (cp_parser* parser)
18959 tree sel_seq = NULL_TREE;
18960 bool maybe_unary_selector_p = true;
18963 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18964 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18965 token = cp_lexer_peek_token (parser->lexer);
18967 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18968 || token->type == CPP_SCOPE)
18970 tree selector = NULL_TREE;
18972 if (token->type != CPP_COLON
18973 || token->type == CPP_SCOPE)
18974 selector = cp_parser_objc_selector (parser);
18976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18977 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18979 /* Detect if we have a unary selector. */
18980 if (maybe_unary_selector_p)
18982 sel_seq = selector;
18983 goto finish_selector;
18987 cp_parser_error (parser, "expected %<:%>");
18990 maybe_unary_selector_p = false;
18991 token = cp_lexer_consume_token (parser->lexer);
18993 if (token->type == CPP_SCOPE)
18996 = chainon (sel_seq,
18997 build_tree_list (selector, NULL_TREE));
18999 = chainon (sel_seq,
19000 build_tree_list (NULL_TREE, NULL_TREE));
19004 = chainon (sel_seq,
19005 build_tree_list (selector, NULL_TREE));
19007 token = cp_lexer_peek_token (parser->lexer);
19011 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19013 return objc_build_selector_expr (sel_seq);
19016 /* Parse a list of identifiers.
19018 objc-identifier-list:
19020 objc-identifier-list , identifier
19022 Returns a TREE_LIST of identifier nodes. */
19025 cp_parser_objc_identifier_list (cp_parser* parser)
19027 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19028 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19030 while (sep->type == CPP_COMMA)
19032 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19033 list = chainon (list,
19034 build_tree_list (NULL_TREE,
19035 cp_parser_identifier (parser)));
19036 sep = cp_lexer_peek_token (parser->lexer);
19042 /* Parse an Objective-C alias declaration.
19044 objc-alias-declaration:
19045 @compatibility_alias identifier identifier ;
19047 This function registers the alias mapping with the Objective-C front end.
19048 It returns nothing. */
19051 cp_parser_objc_alias_declaration (cp_parser* parser)
19055 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19056 alias = cp_parser_identifier (parser);
19057 orig = cp_parser_identifier (parser);
19058 objc_declare_alias (alias, orig);
19059 cp_parser_consume_semicolon_at_end_of_statement (parser);
19062 /* Parse an Objective-C class forward-declaration.
19064 objc-class-declaration:
19065 @class objc-identifier-list ;
19067 The function registers the forward declarations with the Objective-C
19068 front end. It returns nothing. */
19071 cp_parser_objc_class_declaration (cp_parser* parser)
19073 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19074 objc_declare_class (cp_parser_objc_identifier_list (parser));
19075 cp_parser_consume_semicolon_at_end_of_statement (parser);
19078 /* Parse a list of Objective-C protocol references.
19080 objc-protocol-refs-opt:
19081 objc-protocol-refs [opt]
19083 objc-protocol-refs:
19084 < objc-identifier-list >
19086 Returns a TREE_LIST of identifiers, if any. */
19089 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19091 tree protorefs = NULL_TREE;
19093 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19095 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19096 protorefs = cp_parser_objc_identifier_list (parser);
19097 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19103 /* Parse a Objective-C visibility specification. */
19106 cp_parser_objc_visibility_spec (cp_parser* parser)
19108 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19110 switch (vis->keyword)
19112 case RID_AT_PRIVATE:
19113 objc_set_visibility (2);
19115 case RID_AT_PROTECTED:
19116 objc_set_visibility (0);
19118 case RID_AT_PUBLIC:
19119 objc_set_visibility (1);
19125 /* Eat '@private'/'@protected'/'@public'. */
19126 cp_lexer_consume_token (parser->lexer);
19129 /* Parse an Objective-C method type. */
19132 cp_parser_objc_method_type (cp_parser* parser)
19134 objc_set_method_type
19135 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19140 /* Parse an Objective-C protocol qualifier. */
19143 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19145 tree quals = NULL_TREE, node;
19146 cp_token *token = cp_lexer_peek_token (parser->lexer);
19148 node = token->u.value;
19150 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19151 && (node == ridpointers [(int) RID_IN]
19152 || node == ridpointers [(int) RID_OUT]
19153 || node == ridpointers [(int) RID_INOUT]
19154 || node == ridpointers [(int) RID_BYCOPY]
19155 || node == ridpointers [(int) RID_BYREF]
19156 || node == ridpointers [(int) RID_ONEWAY]))
19158 quals = tree_cons (NULL_TREE, node, quals);
19159 cp_lexer_consume_token (parser->lexer);
19160 token = cp_lexer_peek_token (parser->lexer);
19161 node = token->u.value;
19167 /* Parse an Objective-C typename. */
19170 cp_parser_objc_typename (cp_parser* parser)
19172 tree type_name = NULL_TREE;
19174 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19176 tree proto_quals, cp_type = NULL_TREE;
19178 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19179 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19181 /* An ObjC type name may consist of just protocol qualifiers, in which
19182 case the type shall default to 'id'. */
19183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19184 cp_type = cp_parser_type_id (parser);
19186 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19187 type_name = build_tree_list (proto_quals, cp_type);
19193 /* Check to see if TYPE refers to an Objective-C selector name. */
19196 cp_parser_objc_selector_p (enum cpp_ttype type)
19198 return (type == CPP_NAME || type == CPP_KEYWORD
19199 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19200 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19201 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19202 || type == CPP_XOR || type == CPP_XOR_EQ);
19205 /* Parse an Objective-C selector. */
19208 cp_parser_objc_selector (cp_parser* parser)
19210 cp_token *token = cp_lexer_consume_token (parser->lexer);
19212 if (!cp_parser_objc_selector_p (token->type))
19214 error ("%Hinvalid Objective-C++ selector name", &token->location);
19215 return error_mark_node;
19218 /* C++ operator names are allowed to appear in ObjC selectors. */
19219 switch (token->type)
19221 case CPP_AND_AND: return get_identifier ("and");
19222 case CPP_AND_EQ: return get_identifier ("and_eq");
19223 case CPP_AND: return get_identifier ("bitand");
19224 case CPP_OR: return get_identifier ("bitor");
19225 case CPP_COMPL: return get_identifier ("compl");
19226 case CPP_NOT: return get_identifier ("not");
19227 case CPP_NOT_EQ: return get_identifier ("not_eq");
19228 case CPP_OR_OR: return get_identifier ("or");
19229 case CPP_OR_EQ: return get_identifier ("or_eq");
19230 case CPP_XOR: return get_identifier ("xor");
19231 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19232 default: return token->u.value;
19236 /* Parse an Objective-C params list. */
19239 cp_parser_objc_method_keyword_params (cp_parser* parser)
19241 tree params = NULL_TREE;
19242 bool maybe_unary_selector_p = true;
19243 cp_token *token = cp_lexer_peek_token (parser->lexer);
19245 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19247 tree selector = NULL_TREE, type_name, identifier;
19249 if (token->type != CPP_COLON)
19250 selector = cp_parser_objc_selector (parser);
19252 /* Detect if we have a unary selector. */
19253 if (maybe_unary_selector_p
19254 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19257 maybe_unary_selector_p = false;
19258 cp_parser_require (parser, CPP_COLON, "%<:%>");
19259 type_name = cp_parser_objc_typename (parser);
19260 identifier = cp_parser_identifier (parser);
19264 objc_build_keyword_decl (selector,
19268 token = cp_lexer_peek_token (parser->lexer);
19274 /* Parse the non-keyword Objective-C params. */
19277 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19279 tree params = make_node (TREE_LIST);
19280 cp_token *token = cp_lexer_peek_token (parser->lexer);
19281 *ellipsisp = false; /* Initially, assume no ellipsis. */
19283 while (token->type == CPP_COMMA)
19285 cp_parameter_declarator *parmdecl;
19288 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19289 token = cp_lexer_peek_token (parser->lexer);
19291 if (token->type == CPP_ELLIPSIS)
19293 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19298 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19299 parm = grokdeclarator (parmdecl->declarator,
19300 &parmdecl->decl_specifiers,
19301 PARM, /*initialized=*/0,
19302 /*attrlist=*/NULL);
19304 chainon (params, build_tree_list (NULL_TREE, parm));
19305 token = cp_lexer_peek_token (parser->lexer);
19311 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19314 cp_parser_objc_interstitial_code (cp_parser* parser)
19316 cp_token *token = cp_lexer_peek_token (parser->lexer);
19318 /* If the next token is `extern' and the following token is a string
19319 literal, then we have a linkage specification. */
19320 if (token->keyword == RID_EXTERN
19321 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19322 cp_parser_linkage_specification (parser);
19323 /* Handle #pragma, if any. */
19324 else if (token->type == CPP_PRAGMA)
19325 cp_parser_pragma (parser, pragma_external);
19326 /* Allow stray semicolons. */
19327 else if (token->type == CPP_SEMICOLON)
19328 cp_lexer_consume_token (parser->lexer);
19329 /* Finally, try to parse a block-declaration, or a function-definition. */
19331 cp_parser_block_declaration (parser, /*statement_p=*/false);
19334 /* Parse a method signature. */
19337 cp_parser_objc_method_signature (cp_parser* parser)
19339 tree rettype, kwdparms, optparms;
19340 bool ellipsis = false;
19342 cp_parser_objc_method_type (parser);
19343 rettype = cp_parser_objc_typename (parser);
19344 kwdparms = cp_parser_objc_method_keyword_params (parser);
19345 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19347 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19350 /* Pars an Objective-C method prototype list. */
19353 cp_parser_objc_method_prototype_list (cp_parser* parser)
19355 cp_token *token = cp_lexer_peek_token (parser->lexer);
19357 while (token->keyword != RID_AT_END)
19359 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19361 objc_add_method_declaration
19362 (cp_parser_objc_method_signature (parser));
19363 cp_parser_consume_semicolon_at_end_of_statement (parser);
19366 /* Allow for interspersed non-ObjC++ code. */
19367 cp_parser_objc_interstitial_code (parser);
19369 token = cp_lexer_peek_token (parser->lexer);
19372 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19373 objc_finish_interface ();
19376 /* Parse an Objective-C method definition list. */
19379 cp_parser_objc_method_definition_list (cp_parser* parser)
19381 cp_token *token = cp_lexer_peek_token (parser->lexer);
19383 while (token->keyword != RID_AT_END)
19387 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19389 push_deferring_access_checks (dk_deferred);
19390 objc_start_method_definition
19391 (cp_parser_objc_method_signature (parser));
19393 /* For historical reasons, we accept an optional semicolon. */
19394 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19395 cp_lexer_consume_token (parser->lexer);
19397 perform_deferred_access_checks ();
19398 stop_deferring_access_checks ();
19399 meth = cp_parser_function_definition_after_declarator (parser,
19401 pop_deferring_access_checks ();
19402 objc_finish_method_definition (meth);
19405 /* Allow for interspersed non-ObjC++ code. */
19406 cp_parser_objc_interstitial_code (parser);
19408 token = cp_lexer_peek_token (parser->lexer);
19411 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19412 objc_finish_implementation ();
19415 /* Parse Objective-C ivars. */
19418 cp_parser_objc_class_ivars (cp_parser* parser)
19420 cp_token *token = cp_lexer_peek_token (parser->lexer);
19422 if (token->type != CPP_OPEN_BRACE)
19423 return; /* No ivars specified. */
19425 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19426 token = cp_lexer_peek_token (parser->lexer);
19428 while (token->type != CPP_CLOSE_BRACE)
19430 cp_decl_specifier_seq declspecs;
19431 int decl_class_or_enum_p;
19432 tree prefix_attributes;
19434 cp_parser_objc_visibility_spec (parser);
19436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19439 cp_parser_decl_specifier_seq (parser,
19440 CP_PARSER_FLAGS_OPTIONAL,
19442 &decl_class_or_enum_p);
19443 prefix_attributes = declspecs.attributes;
19444 declspecs.attributes = NULL_TREE;
19446 /* Keep going until we hit the `;' at the end of the
19448 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19450 tree width = NULL_TREE, attributes, first_attribute, decl;
19451 cp_declarator *declarator = NULL;
19452 int ctor_dtor_or_conv_p;
19454 /* Check for a (possibly unnamed) bitfield declaration. */
19455 token = cp_lexer_peek_token (parser->lexer);
19456 if (token->type == CPP_COLON)
19459 if (token->type == CPP_NAME
19460 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19463 /* Get the name of the bitfield. */
19464 declarator = make_id_declarator (NULL_TREE,
19465 cp_parser_identifier (parser),
19469 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19470 /* Get the width of the bitfield. */
19472 = cp_parser_constant_expression (parser,
19473 /*allow_non_constant=*/false,
19478 /* Parse the declarator. */
19480 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19481 &ctor_dtor_or_conv_p,
19482 /*parenthesized_p=*/NULL,
19483 /*member_p=*/false);
19486 /* Look for attributes that apply to the ivar. */
19487 attributes = cp_parser_attributes_opt (parser);
19488 /* Remember which attributes are prefix attributes and
19490 first_attribute = attributes;
19491 /* Combine the attributes. */
19492 attributes = chainon (prefix_attributes, attributes);
19495 /* Create the bitfield declaration. */
19496 decl = grokbitfield (declarator, &declspecs,
19500 decl = grokfield (declarator, &declspecs,
19501 NULL_TREE, /*init_const_expr_p=*/false,
19502 NULL_TREE, attributes);
19504 /* Add the instance variable. */
19505 objc_add_instance_variable (decl);
19507 /* Reset PREFIX_ATTRIBUTES. */
19508 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19509 attributes = TREE_CHAIN (attributes);
19511 TREE_CHAIN (attributes) = NULL_TREE;
19513 token = cp_lexer_peek_token (parser->lexer);
19515 if (token->type == CPP_COMMA)
19517 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19523 cp_parser_consume_semicolon_at_end_of_statement (parser);
19524 token = cp_lexer_peek_token (parser->lexer);
19527 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19528 /* For historical reasons, we accept an optional semicolon. */
19529 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19530 cp_lexer_consume_token (parser->lexer);
19533 /* Parse an Objective-C protocol declaration. */
19536 cp_parser_objc_protocol_declaration (cp_parser* parser)
19538 tree proto, protorefs;
19541 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19544 tok = cp_lexer_peek_token (parser->lexer);
19545 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19549 /* See if we have a forward declaration or a definition. */
19550 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19552 /* Try a forward declaration first. */
19553 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19555 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19557 cp_parser_consume_semicolon_at_end_of_statement (parser);
19560 /* Ok, we got a full-fledged definition (or at least should). */
19563 proto = cp_parser_identifier (parser);
19564 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19565 objc_start_protocol (proto, protorefs);
19566 cp_parser_objc_method_prototype_list (parser);
19570 /* Parse an Objective-C superclass or category. */
19573 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19576 cp_token *next = cp_lexer_peek_token (parser->lexer);
19578 *super = *categ = NULL_TREE;
19579 if (next->type == CPP_COLON)
19581 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19582 *super = cp_parser_identifier (parser);
19584 else if (next->type == CPP_OPEN_PAREN)
19586 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19587 *categ = cp_parser_identifier (parser);
19588 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19592 /* Parse an Objective-C class interface. */
19595 cp_parser_objc_class_interface (cp_parser* parser)
19597 tree name, super, categ, protos;
19599 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19600 name = cp_parser_identifier (parser);
19601 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19602 protos = cp_parser_objc_protocol_refs_opt (parser);
19604 /* We have either a class or a category on our hands. */
19606 objc_start_category_interface (name, categ, protos);
19609 objc_start_class_interface (name, super, protos);
19610 /* Handle instance variable declarations, if any. */
19611 cp_parser_objc_class_ivars (parser);
19612 objc_continue_interface ();
19615 cp_parser_objc_method_prototype_list (parser);
19618 /* Parse an Objective-C class implementation. */
19621 cp_parser_objc_class_implementation (cp_parser* parser)
19623 tree name, super, categ;
19625 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19626 name = cp_parser_identifier (parser);
19627 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19629 /* We have either a class or a category on our hands. */
19631 objc_start_category_implementation (name, categ);
19634 objc_start_class_implementation (name, super);
19635 /* Handle instance variable declarations, if any. */
19636 cp_parser_objc_class_ivars (parser);
19637 objc_continue_implementation ();
19640 cp_parser_objc_method_definition_list (parser);
19643 /* Consume the @end token and finish off the implementation. */
19646 cp_parser_objc_end_implementation (cp_parser* parser)
19648 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19649 objc_finish_implementation ();
19652 /* Parse an Objective-C declaration. */
19655 cp_parser_objc_declaration (cp_parser* parser)
19657 /* Try to figure out what kind of declaration is present. */
19658 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19660 switch (kwd->keyword)
19663 cp_parser_objc_alias_declaration (parser);
19666 cp_parser_objc_class_declaration (parser);
19668 case RID_AT_PROTOCOL:
19669 cp_parser_objc_protocol_declaration (parser);
19671 case RID_AT_INTERFACE:
19672 cp_parser_objc_class_interface (parser);
19674 case RID_AT_IMPLEMENTATION:
19675 cp_parser_objc_class_implementation (parser);
19678 cp_parser_objc_end_implementation (parser);
19681 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19682 &kwd->location, kwd->u.value);
19683 cp_parser_skip_to_end_of_block_or_statement (parser);
19687 /* Parse an Objective-C try-catch-finally statement.
19689 objc-try-catch-finally-stmt:
19690 @try compound-statement objc-catch-clause-seq [opt]
19691 objc-finally-clause [opt]
19693 objc-catch-clause-seq:
19694 objc-catch-clause objc-catch-clause-seq [opt]
19697 @catch ( exception-declaration ) compound-statement
19699 objc-finally-clause
19700 @finally compound-statement
19702 Returns NULL_TREE. */
19705 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19706 location_t location;
19709 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19710 location = cp_lexer_peek_token (parser->lexer)->location;
19711 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19712 node, lest it get absorbed into the surrounding block. */
19713 stmt = push_stmt_list ();
19714 cp_parser_compound_statement (parser, NULL, false);
19715 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19717 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19719 cp_parameter_declarator *parmdecl;
19722 cp_lexer_consume_token (parser->lexer);
19723 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19724 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19725 parm = grokdeclarator (parmdecl->declarator,
19726 &parmdecl->decl_specifiers,
19727 PARM, /*initialized=*/0,
19728 /*attrlist=*/NULL);
19729 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19730 objc_begin_catch_clause (parm);
19731 cp_parser_compound_statement (parser, NULL, false);
19732 objc_finish_catch_clause ();
19735 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19737 cp_lexer_consume_token (parser->lexer);
19738 location = cp_lexer_peek_token (parser->lexer)->location;
19739 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19740 node, lest it get absorbed into the surrounding block. */
19741 stmt = push_stmt_list ();
19742 cp_parser_compound_statement (parser, NULL, false);
19743 objc_build_finally_clause (location, pop_stmt_list (stmt));
19746 return objc_finish_try_stmt ();
19749 /* Parse an Objective-C synchronized statement.
19751 objc-synchronized-stmt:
19752 @synchronized ( expression ) compound-statement
19754 Returns NULL_TREE. */
19757 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19758 location_t location;
19761 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19763 location = cp_lexer_peek_token (parser->lexer)->location;
19764 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19765 lock = cp_parser_expression (parser, false);
19766 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19768 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19769 node, lest it get absorbed into the surrounding block. */
19770 stmt = push_stmt_list ();
19771 cp_parser_compound_statement (parser, NULL, false);
19773 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19776 /* Parse an Objective-C throw statement.
19779 @throw assignment-expression [opt] ;
19781 Returns a constructed '@throw' statement. */
19784 cp_parser_objc_throw_statement (cp_parser *parser) {
19785 tree expr = NULL_TREE;
19787 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19790 expr = cp_parser_assignment_expression (parser, false);
19792 cp_parser_consume_semicolon_at_end_of_statement (parser);
19794 return objc_build_throw_stmt (expr);
19797 /* Parse an Objective-C statement. */
19800 cp_parser_objc_statement (cp_parser * parser) {
19801 /* Try to figure out what kind of declaration is present. */
19802 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19804 switch (kwd->keyword)
19807 return cp_parser_objc_try_catch_finally_statement (parser);
19808 case RID_AT_SYNCHRONIZED:
19809 return cp_parser_objc_synchronized_statement (parser);
19811 return cp_parser_objc_throw_statement (parser);
19813 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19814 &kwd->location, kwd->u.value);
19815 cp_parser_skip_to_end_of_block_or_statement (parser);
19818 return error_mark_node;
19821 /* OpenMP 2.5 parsing routines. */
19823 /* Returns name of the next clause.
19824 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19825 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19826 returned and the token is consumed. */
19828 static pragma_omp_clause
19829 cp_parser_omp_clause_name (cp_parser *parser)
19831 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19834 result = PRAGMA_OMP_CLAUSE_IF;
19835 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19836 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19837 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19838 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19839 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19841 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19842 const char *p = IDENTIFIER_POINTER (id);
19847 if (!strcmp ("collapse", p))
19848 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19849 else if (!strcmp ("copyin", p))
19850 result = PRAGMA_OMP_CLAUSE_COPYIN;
19851 else if (!strcmp ("copyprivate", p))
19852 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19855 if (!strcmp ("firstprivate", p))
19856 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19859 if (!strcmp ("lastprivate", p))
19860 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19863 if (!strcmp ("nowait", p))
19864 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19865 else if (!strcmp ("num_threads", p))
19866 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19869 if (!strcmp ("ordered", p))
19870 result = PRAGMA_OMP_CLAUSE_ORDERED;
19873 if (!strcmp ("reduction", p))
19874 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19877 if (!strcmp ("schedule", p))
19878 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19879 else if (!strcmp ("shared", p))
19880 result = PRAGMA_OMP_CLAUSE_SHARED;
19883 if (!strcmp ("untied", p))
19884 result = PRAGMA_OMP_CLAUSE_UNTIED;
19889 if (result != PRAGMA_OMP_CLAUSE_NONE)
19890 cp_lexer_consume_token (parser->lexer);
19895 /* Validate that a clause of the given type does not already exist. */
19898 check_no_duplicate_clause (tree clauses, enum tree_code code,
19899 const char *name, location_t location)
19903 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19904 if (OMP_CLAUSE_CODE (c) == code)
19906 error ("%Htoo many %qs clauses", &location, name);
19914 variable-list , identifier
19916 In addition, we match a closing parenthesis. An opening parenthesis
19917 will have been consumed by the caller.
19919 If KIND is nonzero, create the appropriate node and install the decl
19920 in OMP_CLAUSE_DECL and add the node to the head of the list.
19922 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19923 return the list created. */
19926 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19934 token = cp_lexer_peek_token (parser->lexer);
19935 name = cp_parser_id_expression (parser, /*template_p=*/false,
19936 /*check_dependency_p=*/true,
19937 /*template_p=*/NULL,
19938 /*declarator_p=*/false,
19939 /*optional_p=*/false);
19940 if (name == error_mark_node)
19943 decl = cp_parser_lookup_name_simple (parser, name, token->location);
19944 if (decl == error_mark_node)
19945 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
19946 else if (kind != 0)
19948 tree u = build_omp_clause (kind);
19949 OMP_CLAUSE_DECL (u) = decl;
19950 OMP_CLAUSE_CHAIN (u) = list;
19954 list = tree_cons (decl, NULL_TREE, list);
19957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19959 cp_lexer_consume_token (parser->lexer);
19962 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19966 /* Try to resync to an unnested comma. Copied from
19967 cp_parser_parenthesized_expression_list. */
19969 ending = cp_parser_skip_to_closing_parenthesis (parser,
19970 /*recovering=*/true,
19972 /*consume_paren=*/true);
19980 /* Similarly, but expect leading and trailing parenthesis. This is a very
19981 common case for omp clauses. */
19984 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19986 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19987 return cp_parser_omp_var_list_no_open (parser, kind, list);
19992 collapse ( constant-expression ) */
19995 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20001 loc = cp_lexer_peek_token (parser->lexer)->location;
20002 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20005 num = cp_parser_constant_expression (parser, false, NULL);
20007 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20008 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20009 /*or_comma=*/false,
20010 /*consume_paren=*/true);
20012 if (num == error_mark_node)
20014 num = fold_non_dependent_expr (num);
20015 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20016 || !host_integerp (num, 0)
20017 || (n = tree_low_cst (num, 0)) <= 0
20020 error ("%Hcollapse argument needs positive constant integer expression",
20025 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20026 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20027 OMP_CLAUSE_CHAIN (c) = list;
20028 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20034 default ( shared | none ) */
20037 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20039 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20042 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20044 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20046 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20047 const char *p = IDENTIFIER_POINTER (id);
20052 if (strcmp ("none", p) != 0)
20054 kind = OMP_CLAUSE_DEFAULT_NONE;
20058 if (strcmp ("shared", p) != 0)
20060 kind = OMP_CLAUSE_DEFAULT_SHARED;
20067 cp_lexer_consume_token (parser->lexer);
20072 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20075 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20076 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20077 /*or_comma=*/false,
20078 /*consume_paren=*/true);
20080 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20083 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20084 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20085 OMP_CLAUSE_CHAIN (c) = list;
20086 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20092 if ( expression ) */
20095 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20102 t = cp_parser_condition (parser);
20104 if (t == error_mark_node
20105 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20106 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20107 /*or_comma=*/false,
20108 /*consume_paren=*/true);
20110 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20112 c = build_omp_clause (OMP_CLAUSE_IF);
20113 OMP_CLAUSE_IF_EXPR (c) = t;
20114 OMP_CLAUSE_CHAIN (c) = list;
20123 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20124 tree list, location_t location)
20128 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20130 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20131 OMP_CLAUSE_CHAIN (c) = list;
20136 num_threads ( expression ) */
20139 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20140 location_t location)
20144 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20147 t = cp_parser_expression (parser, false);
20149 if (t == error_mark_node
20150 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20151 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20152 /*or_comma=*/false,
20153 /*consume_paren=*/true);
20155 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20156 "num_threads", location);
20158 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20159 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20160 OMP_CLAUSE_CHAIN (c) = list;
20169 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20170 tree list, location_t location)
20174 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20175 "ordered", location);
20177 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20178 OMP_CLAUSE_CHAIN (c) = list;
20183 reduction ( reduction-operator : variable-list )
20185 reduction-operator:
20186 One of: + * - & ^ | && || */
20189 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20191 enum tree_code code;
20194 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20197 switch (cp_lexer_peek_token (parser->lexer)->type)
20209 code = BIT_AND_EXPR;
20212 code = BIT_XOR_EXPR;
20215 code = BIT_IOR_EXPR;
20218 code = TRUTH_ANDIF_EXPR;
20221 code = TRUTH_ORIF_EXPR;
20224 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20225 "%<|%>, %<&&%>, or %<||%>");
20227 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20228 /*or_comma=*/false,
20229 /*consume_paren=*/true);
20232 cp_lexer_consume_token (parser->lexer);
20234 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20237 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20238 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20239 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20245 schedule ( schedule-kind )
20246 schedule ( schedule-kind , expression )
20249 static | dynamic | guided | runtime | auto */
20252 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20256 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20259 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20261 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20263 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20264 const char *p = IDENTIFIER_POINTER (id);
20269 if (strcmp ("dynamic", p) != 0)
20271 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20275 if (strcmp ("guided", p) != 0)
20277 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20281 if (strcmp ("runtime", p) != 0)
20283 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20290 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20291 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20292 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20293 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20296 cp_lexer_consume_token (parser->lexer);
20298 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20301 cp_lexer_consume_token (parser->lexer);
20303 token = cp_lexer_peek_token (parser->lexer);
20304 t = cp_parser_assignment_expression (parser, false);
20306 if (t == error_mark_node)
20308 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20309 error ("%Hschedule %<runtime%> does not take "
20310 "a %<chunk_size%> parameter", &token->location);
20311 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20312 error ("%Hschedule %<auto%> does not take "
20313 "a %<chunk_size%> parameter", &token->location);
20315 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20317 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20320 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20323 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20324 OMP_CLAUSE_CHAIN (c) = list;
20328 cp_parser_error (parser, "invalid schedule kind");
20330 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20331 /*or_comma=*/false,
20332 /*consume_paren=*/true);
20340 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20341 tree list, location_t location)
20345 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20347 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20348 OMP_CLAUSE_CHAIN (c) = list;
20352 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20353 is a bitmask in MASK. Return the list of clauses found; the result
20354 of clause default goes in *pdefault. */
20357 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20358 const char *where, cp_token *pragma_tok)
20360 tree clauses = NULL;
20362 cp_token *token = NULL;
20364 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20366 pragma_omp_clause c_kind;
20367 const char *c_name;
20368 tree prev = clauses;
20370 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20371 cp_lexer_consume_token (parser->lexer);
20373 token = cp_lexer_peek_token (parser->lexer);
20374 c_kind = cp_parser_omp_clause_name (parser);
20379 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20380 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20382 c_name = "collapse";
20384 case PRAGMA_OMP_CLAUSE_COPYIN:
20385 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20388 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20389 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20391 c_name = "copyprivate";
20393 case PRAGMA_OMP_CLAUSE_DEFAULT:
20394 clauses = cp_parser_omp_clause_default (parser, clauses,
20396 c_name = "default";
20398 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20399 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20401 c_name = "firstprivate";
20403 case PRAGMA_OMP_CLAUSE_IF:
20404 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20407 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20408 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20410 c_name = "lastprivate";
20412 case PRAGMA_OMP_CLAUSE_NOWAIT:
20413 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20416 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20417 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20419 c_name = "num_threads";
20421 case PRAGMA_OMP_CLAUSE_ORDERED:
20422 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20424 c_name = "ordered";
20426 case PRAGMA_OMP_CLAUSE_PRIVATE:
20427 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20429 c_name = "private";
20431 case PRAGMA_OMP_CLAUSE_REDUCTION:
20432 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20433 c_name = "reduction";
20435 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20436 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20438 c_name = "schedule";
20440 case PRAGMA_OMP_CLAUSE_SHARED:
20441 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20445 case PRAGMA_OMP_CLAUSE_UNTIED:
20446 clauses = cp_parser_omp_clause_untied (parser, clauses,
20451 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20455 if (((mask >> c_kind) & 1) == 0)
20457 /* Remove the invalid clause(s) from the list to avoid
20458 confusing the rest of the compiler. */
20460 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20464 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20465 return finish_omp_clauses (clauses);
20472 In practice, we're also interested in adding the statement to an
20473 outer node. So it is convenient if we work around the fact that
20474 cp_parser_statement calls add_stmt. */
20477 cp_parser_begin_omp_structured_block (cp_parser *parser)
20479 unsigned save = parser->in_statement;
20481 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20482 This preserves the "not within loop or switch" style error messages
20483 for nonsense cases like
20489 if (parser->in_statement)
20490 parser->in_statement = IN_OMP_BLOCK;
20496 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20498 parser->in_statement = save;
20502 cp_parser_omp_structured_block (cp_parser *parser)
20504 tree stmt = begin_omp_structured_block ();
20505 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20507 cp_parser_statement (parser, NULL_TREE, false, NULL);
20509 cp_parser_end_omp_structured_block (parser, save);
20510 return finish_omp_structured_block (stmt);
20514 # pragma omp atomic new-line
20518 x binop= expr | x++ | ++x | x-- | --x
20520 +, *, -, /, &, ^, |, <<, >>
20522 where x is an lvalue expression with scalar type. */
20525 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20528 enum tree_code code;
20530 cp_parser_require_pragma_eol (parser, pragma_tok);
20532 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20534 switch (TREE_CODE (lhs))
20539 case PREINCREMENT_EXPR:
20540 case POSTINCREMENT_EXPR:
20541 lhs = TREE_OPERAND (lhs, 0);
20543 rhs = integer_one_node;
20546 case PREDECREMENT_EXPR:
20547 case POSTDECREMENT_EXPR:
20548 lhs = TREE_OPERAND (lhs, 0);
20550 rhs = integer_one_node;
20554 switch (cp_lexer_peek_token (parser->lexer)->type)
20560 code = TRUNC_DIV_EXPR;
20568 case CPP_LSHIFT_EQ:
20569 code = LSHIFT_EXPR;
20571 case CPP_RSHIFT_EQ:
20572 code = RSHIFT_EXPR;
20575 code = BIT_AND_EXPR;
20578 code = BIT_IOR_EXPR;
20581 code = BIT_XOR_EXPR;
20584 cp_parser_error (parser,
20585 "invalid operator for %<#pragma omp atomic%>");
20588 cp_lexer_consume_token (parser->lexer);
20590 rhs = cp_parser_expression (parser, false);
20591 if (rhs == error_mark_node)
20595 finish_omp_atomic (code, lhs, rhs);
20596 cp_parser_consume_semicolon_at_end_of_statement (parser);
20600 cp_parser_skip_to_end_of_block_or_statement (parser);
20605 # pragma omp barrier new-line */
20608 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20610 cp_parser_require_pragma_eol (parser, pragma_tok);
20611 finish_omp_barrier ();
20615 # pragma omp critical [(name)] new-line
20616 structured-block */
20619 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20621 tree stmt, name = NULL;
20623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20625 cp_lexer_consume_token (parser->lexer);
20627 name = cp_parser_identifier (parser);
20629 if (name == error_mark_node
20630 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20631 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20632 /*or_comma=*/false,
20633 /*consume_paren=*/true);
20634 if (name == error_mark_node)
20637 cp_parser_require_pragma_eol (parser, pragma_tok);
20639 stmt = cp_parser_omp_structured_block (parser);
20640 return c_finish_omp_critical (stmt, name);
20644 # pragma omp flush flush-vars[opt] new-line
20647 ( variable-list ) */
20650 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20653 (void) cp_parser_omp_var_list (parser, 0, NULL);
20654 cp_parser_require_pragma_eol (parser, pragma_tok);
20656 finish_omp_flush ();
20659 /* Helper function, to parse omp for increment expression. */
20662 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20664 tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20670 cp_parser_skip_to_end_of_statement (parser);
20671 return error_mark_node;
20674 token = cp_lexer_peek_token (parser->lexer);
20675 op = binops_by_token [token->type].tree_type;
20684 cp_parser_skip_to_end_of_statement (parser);
20685 return error_mark_node;
20688 cp_lexer_consume_token (parser->lexer);
20689 rhs = cp_parser_binary_expression (parser, false,
20690 PREC_RELATIONAL_EXPRESSION);
20691 if (rhs == error_mark_node
20692 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20694 cp_parser_skip_to_end_of_statement (parser);
20695 return error_mark_node;
20698 return build2 (op, boolean_type_node, lhs, rhs);
20701 /* Helper function, to parse omp for increment expression. */
20704 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20706 cp_token *token = cp_lexer_peek_token (parser->lexer);
20712 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20714 op = (token->type == CPP_PLUS_PLUS
20715 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20716 cp_lexer_consume_token (parser->lexer);
20717 lhs = cp_parser_cast_expression (parser, false, false);
20719 return error_mark_node;
20720 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20723 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20725 return error_mark_node;
20727 token = cp_lexer_peek_token (parser->lexer);
20728 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20730 op = (token->type == CPP_PLUS_PLUS
20731 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20732 cp_lexer_consume_token (parser->lexer);
20733 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20736 op = cp_parser_assignment_operator_opt (parser);
20737 if (op == ERROR_MARK)
20738 return error_mark_node;
20740 if (op != NOP_EXPR)
20742 rhs = cp_parser_assignment_expression (parser, false);
20743 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20744 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20747 lhs = cp_parser_binary_expression (parser, false,
20748 PREC_ADDITIVE_EXPRESSION);
20749 token = cp_lexer_peek_token (parser->lexer);
20750 decl_first = lhs == decl;
20753 if (token->type != CPP_PLUS
20754 && token->type != CPP_MINUS)
20755 return error_mark_node;
20759 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20760 cp_lexer_consume_token (parser->lexer);
20761 rhs = cp_parser_binary_expression (parser, false,
20762 PREC_ADDITIVE_EXPRESSION);
20763 token = cp_lexer_peek_token (parser->lexer);
20764 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20766 if (lhs == NULL_TREE)
20768 if (op == PLUS_EXPR)
20771 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20774 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20775 NULL, tf_warning_or_error);
20778 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20782 if (rhs != decl || op == MINUS_EXPR)
20783 return error_mark_node;
20784 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20787 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20789 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20792 /* Parse the restricted form of the for statement allowed by OpenMP. */
20795 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20797 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20798 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20799 tree this_pre_body, cl;
20800 location_t loc_first;
20801 bool collapse_err = false;
20802 int i, collapse = 1, nbraces = 0;
20804 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20805 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20806 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20808 gcc_assert (collapse >= 1);
20810 declv = make_tree_vec (collapse);
20811 initv = make_tree_vec (collapse);
20812 condv = make_tree_vec (collapse);
20813 incrv = make_tree_vec (collapse);
20815 loc_first = cp_lexer_peek_token (parser->lexer)->location;
20817 for (i = 0; i < collapse; i++)
20819 int bracecount = 0;
20820 bool add_private_clause = false;
20823 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20825 cp_parser_error (parser, "for statement expected");
20828 loc = cp_lexer_consume_token (parser->lexer)->location;
20830 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20833 init = decl = real_decl = NULL;
20834 this_pre_body = push_stmt_list ();
20835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20837 cp_decl_specifier_seq type_specifiers;
20839 /* First, try to parse as an initialized declaration. See
20840 cp_parser_condition, from whence the bulk of this is copied. */
20842 cp_parser_parse_tentatively (parser);
20843 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20845 if (!cp_parser_error_occurred (parser))
20847 tree asm_specification, attributes;
20848 cp_declarator *declarator;
20850 declarator = cp_parser_declarator (parser,
20851 CP_PARSER_DECLARATOR_NAMED,
20852 /*ctor_dtor_or_conv_p=*/NULL,
20853 /*parenthesized_p=*/NULL,
20854 /*member_p=*/false);
20855 attributes = cp_parser_attributes_opt (parser);
20856 asm_specification = cp_parser_asm_specification_opt (parser);
20858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20859 cp_parser_require (parser, CPP_EQ, "%<=%>");
20860 if (cp_parser_parse_definitely (parser))
20864 decl = start_decl (declarator, &type_specifiers,
20865 /*initialized_p=*/false, attributes,
20866 /*prefix_attributes=*/NULL_TREE,
20869 if (CLASS_TYPE_P (TREE_TYPE (decl))
20870 || type_dependent_expression_p (decl))
20872 bool is_direct_init, is_non_constant_init;
20874 init = cp_parser_initializer (parser,
20876 &is_non_constant_init);
20878 cp_finish_decl (decl, init, !is_non_constant_init,
20880 LOOKUP_ONLYCONVERTING);
20881 if (CLASS_TYPE_P (TREE_TYPE (decl)))
20884 = tree_cons (NULL, this_pre_body, for_block);
20888 init = pop_stmt_list (this_pre_body);
20889 this_pre_body = NULL_TREE;
20893 cp_parser_require (parser, CPP_EQ, "%<=%>");
20894 init = cp_parser_assignment_expression (parser, false);
20896 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20897 init = error_mark_node;
20899 cp_finish_decl (decl, NULL_TREE,
20900 /*init_const_expr_p=*/false,
20902 LOOKUP_ONLYCONVERTING);
20906 pop_scope (pushed_scope);
20910 cp_parser_abort_tentative_parse (parser);
20912 /* If parsing as an initialized declaration failed, try again as
20913 a simple expression. */
20917 cp_parser_parse_tentatively (parser);
20918 decl = cp_parser_primary_expression (parser, false, false,
20920 if (!cp_parser_error_occurred (parser)
20923 && CLASS_TYPE_P (TREE_TYPE (decl)))
20927 cp_parser_parse_definitely (parser);
20928 cp_parser_require (parser, CPP_EQ, "%<=%>");
20929 rhs = cp_parser_assignment_expression (parser, false);
20930 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
20932 tf_warning_or_error));
20933 add_private_clause = true;
20938 cp_parser_abort_tentative_parse (parser);
20939 init = cp_parser_expression (parser, false);
20942 if (TREE_CODE (init) == MODIFY_EXPR
20943 || TREE_CODE (init) == MODOP_EXPR)
20944 real_decl = TREE_OPERAND (init, 0);
20949 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20952 this_pre_body = pop_stmt_list (this_pre_body);
20956 pre_body = push_stmt_list ();
20958 add_stmt (this_pre_body);
20959 pre_body = pop_stmt_list (pre_body);
20962 pre_body = this_pre_body;
20967 if (par_clauses != NULL && real_decl != NULL_TREE)
20970 for (c = par_clauses; *c ; )
20971 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
20972 && OMP_CLAUSE_DECL (*c) == real_decl)
20974 error ("%Hiteration variable %qD should not be firstprivate",
20976 *c = OMP_CLAUSE_CHAIN (*c);
20978 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
20979 && OMP_CLAUSE_DECL (*c) == real_decl)
20981 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
20982 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
20983 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
20984 OMP_CLAUSE_DECL (l) = real_decl;
20985 OMP_CLAUSE_CHAIN (l) = clauses;
20986 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
20988 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
20989 CP_OMP_CLAUSE_INFO (*c) = NULL;
20990 add_private_clause = false;
20994 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
20995 && OMP_CLAUSE_DECL (*c) == real_decl)
20996 add_private_clause = false;
20997 c = &OMP_CLAUSE_CHAIN (*c);
21001 if (add_private_clause)
21004 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21006 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21007 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21008 && OMP_CLAUSE_DECL (c) == decl)
21010 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21011 && OMP_CLAUSE_DECL (c) == decl)
21012 error ("%Hiteration variable %qD should not be firstprivate",
21014 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21015 && OMP_CLAUSE_DECL (c) == decl)
21016 error ("%Hiteration variable %qD should not be reduction",
21021 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21022 OMP_CLAUSE_DECL (c) = decl;
21023 c = finish_omp_clauses (c);
21026 OMP_CLAUSE_CHAIN (c) = clauses;
21033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21035 /* If decl is an iterator, preserve LHS and RHS of the relational
21036 expr until finish_omp_for. */
21038 && (type_dependent_expression_p (decl)
21039 || CLASS_TYPE_P (TREE_TYPE (decl))))
21040 cond = cp_parser_omp_for_cond (parser, decl);
21042 cond = cp_parser_condition (parser);
21044 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21049 /* If decl is an iterator, preserve the operator on decl
21050 until finish_omp_for. */
21052 && (type_dependent_expression_p (decl)
21053 || CLASS_TYPE_P (TREE_TYPE (decl))))
21054 incr = cp_parser_omp_for_incr (parser, decl);
21056 incr = cp_parser_expression (parser, false);
21059 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21060 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21061 /*or_comma=*/false,
21062 /*consume_paren=*/true);
21064 TREE_VEC_ELT (declv, i) = decl;
21065 TREE_VEC_ELT (initv, i) = init;
21066 TREE_VEC_ELT (condv, i) = cond;
21067 TREE_VEC_ELT (incrv, i) = incr;
21069 if (i == collapse - 1)
21072 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21073 in between the collapsed for loops to be still considered perfectly
21074 nested. Hopefully the final version clarifies this.
21075 For now handle (multiple) {'s and empty statements. */
21076 cp_parser_parse_tentatively (parser);
21079 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21081 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21083 cp_lexer_consume_token (parser->lexer);
21086 else if (bracecount
21087 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21088 cp_lexer_consume_token (parser->lexer);
21091 loc = cp_lexer_peek_token (parser->lexer)->location;
21092 error ("%Hnot enough collapsed for loops", &loc);
21093 collapse_err = true;
21094 cp_parser_abort_tentative_parse (parser);
21103 cp_parser_parse_definitely (parser);
21104 nbraces += bracecount;
21108 /* Note that we saved the original contents of this flag when we entered
21109 the structured block, and so we don't need to re-save it here. */
21110 parser->in_statement = IN_OMP_FOR;
21112 /* Note that the grammar doesn't call for a structured block here,
21113 though the loop as a whole is a structured block. */
21114 body = push_stmt_list ();
21115 cp_parser_statement (parser, NULL_TREE, false, NULL);
21116 body = pop_stmt_list (body);
21118 if (declv == NULL_TREE)
21121 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21122 pre_body, clauses);
21126 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21128 cp_lexer_consume_token (parser->lexer);
21131 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21132 cp_lexer_consume_token (parser->lexer);
21137 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21138 error ("%Hcollapsed loops not perfectly nested", &loc);
21140 collapse_err = true;
21141 cp_parser_statement_seq_opt (parser, NULL);
21142 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21148 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21149 for_block = TREE_CHAIN (for_block);
21156 #pragma omp for for-clause[optseq] new-line
21159 #define OMP_FOR_CLAUSE_MASK \
21160 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21161 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21162 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21163 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21164 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21165 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21166 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21167 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21170 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21172 tree clauses, sb, ret;
21175 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21176 "#pragma omp for", pragma_tok);
21178 sb = begin_omp_structured_block ();
21179 save = cp_parser_begin_omp_structured_block (parser);
21181 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21183 cp_parser_end_omp_structured_block (parser, save);
21184 add_stmt (finish_omp_structured_block (sb));
21190 # pragma omp master new-line
21191 structured-block */
21194 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21196 cp_parser_require_pragma_eol (parser, pragma_tok);
21197 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21201 # pragma omp ordered new-line
21202 structured-block */
21205 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21207 cp_parser_require_pragma_eol (parser, pragma_tok);
21208 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21214 { section-sequence }
21217 section-directive[opt] structured-block
21218 section-sequence section-directive structured-block */
21221 cp_parser_omp_sections_scope (cp_parser *parser)
21223 tree stmt, substmt;
21224 bool error_suppress = false;
21227 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21230 stmt = push_stmt_list ();
21232 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21236 substmt = begin_omp_structured_block ();
21237 save = cp_parser_begin_omp_structured_block (parser);
21241 cp_parser_statement (parser, NULL_TREE, false, NULL);
21243 tok = cp_lexer_peek_token (parser->lexer);
21244 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21246 if (tok->type == CPP_CLOSE_BRACE)
21248 if (tok->type == CPP_EOF)
21252 cp_parser_end_omp_structured_block (parser, save);
21253 substmt = finish_omp_structured_block (substmt);
21254 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21255 add_stmt (substmt);
21260 tok = cp_lexer_peek_token (parser->lexer);
21261 if (tok->type == CPP_CLOSE_BRACE)
21263 if (tok->type == CPP_EOF)
21266 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21268 cp_lexer_consume_token (parser->lexer);
21269 cp_parser_require_pragma_eol (parser, tok);
21270 error_suppress = false;
21272 else if (!error_suppress)
21274 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21275 error_suppress = true;
21278 substmt = cp_parser_omp_structured_block (parser);
21279 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21280 add_stmt (substmt);
21282 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21284 substmt = pop_stmt_list (stmt);
21286 stmt = make_node (OMP_SECTIONS);
21287 TREE_TYPE (stmt) = void_type_node;
21288 OMP_SECTIONS_BODY (stmt) = substmt;
21295 # pragma omp sections sections-clause[optseq] newline
21298 #define OMP_SECTIONS_CLAUSE_MASK \
21299 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21300 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21301 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21302 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21303 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21306 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21310 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21311 "#pragma omp sections", pragma_tok);
21313 ret = cp_parser_omp_sections_scope (parser);
21315 OMP_SECTIONS_CLAUSES (ret) = clauses;
21321 # pragma parallel parallel-clause new-line
21322 # pragma parallel for parallel-for-clause new-line
21323 # pragma parallel sections parallel-sections-clause new-line */
21325 #define OMP_PARALLEL_CLAUSE_MASK \
21326 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21327 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21328 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21329 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21330 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21331 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21332 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21333 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21336 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21338 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21339 const char *p_name = "#pragma omp parallel";
21340 tree stmt, clauses, par_clause, ws_clause, block;
21341 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21346 cp_lexer_consume_token (parser->lexer);
21347 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21348 p_name = "#pragma omp parallel for";
21349 mask |= OMP_FOR_CLAUSE_MASK;
21350 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21352 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21354 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21355 const char *p = IDENTIFIER_POINTER (id);
21356 if (strcmp (p, "sections") == 0)
21358 cp_lexer_consume_token (parser->lexer);
21359 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21360 p_name = "#pragma omp parallel sections";
21361 mask |= OMP_SECTIONS_CLAUSE_MASK;
21362 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21366 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21367 block = begin_omp_parallel ();
21368 save = cp_parser_begin_omp_structured_block (parser);
21372 case PRAGMA_OMP_PARALLEL:
21373 cp_parser_statement (parser, NULL_TREE, false, NULL);
21374 par_clause = clauses;
21377 case PRAGMA_OMP_PARALLEL_FOR:
21378 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21379 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21382 case PRAGMA_OMP_PARALLEL_SECTIONS:
21383 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21384 stmt = cp_parser_omp_sections_scope (parser);
21386 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21390 gcc_unreachable ();
21393 cp_parser_end_omp_structured_block (parser, save);
21394 stmt = finish_omp_parallel (par_clause, block);
21395 if (p_kind != PRAGMA_OMP_PARALLEL)
21396 OMP_PARALLEL_COMBINED (stmt) = 1;
21401 # pragma omp single single-clause[optseq] new-line
21402 structured-block */
21404 #define OMP_SINGLE_CLAUSE_MASK \
21405 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21406 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21407 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21408 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21411 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21413 tree stmt = make_node (OMP_SINGLE);
21414 TREE_TYPE (stmt) = void_type_node;
21416 OMP_SINGLE_CLAUSES (stmt)
21417 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21418 "#pragma omp single", pragma_tok);
21419 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21421 return add_stmt (stmt);
21425 # pragma omp task task-clause[optseq] new-line
21426 structured-block */
21428 #define OMP_TASK_CLAUSE_MASK \
21429 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21430 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21431 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21432 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21433 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21434 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21437 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21439 tree clauses, block;
21442 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21443 "#pragma omp task", pragma_tok);
21444 block = begin_omp_task ();
21445 save = cp_parser_begin_omp_structured_block (parser);
21446 cp_parser_statement (parser, NULL_TREE, false, NULL);
21447 cp_parser_end_omp_structured_block (parser, save);
21448 return finish_omp_task (clauses, block);
21452 # pragma omp taskwait new-line */
21455 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21457 cp_parser_require_pragma_eol (parser, pragma_tok);
21458 finish_omp_taskwait ();
21462 # pragma omp threadprivate (variable-list) */
21465 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21469 vars = cp_parser_omp_var_list (parser, 0, NULL);
21470 cp_parser_require_pragma_eol (parser, pragma_tok);
21472 finish_omp_threadprivate (vars);
21475 /* Main entry point to OpenMP statement pragmas. */
21478 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21482 switch (pragma_tok->pragma_kind)
21484 case PRAGMA_OMP_ATOMIC:
21485 cp_parser_omp_atomic (parser, pragma_tok);
21487 case PRAGMA_OMP_CRITICAL:
21488 stmt = cp_parser_omp_critical (parser, pragma_tok);
21490 case PRAGMA_OMP_FOR:
21491 stmt = cp_parser_omp_for (parser, pragma_tok);
21493 case PRAGMA_OMP_MASTER:
21494 stmt = cp_parser_omp_master (parser, pragma_tok);
21496 case PRAGMA_OMP_ORDERED:
21497 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21499 case PRAGMA_OMP_PARALLEL:
21500 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21502 case PRAGMA_OMP_SECTIONS:
21503 stmt = cp_parser_omp_sections (parser, pragma_tok);
21505 case PRAGMA_OMP_SINGLE:
21506 stmt = cp_parser_omp_single (parser, pragma_tok);
21508 case PRAGMA_OMP_TASK:
21509 stmt = cp_parser_omp_task (parser, pragma_tok);
21512 gcc_unreachable ();
21516 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21521 static GTY (()) cp_parser *the_parser;
21524 /* Special handling for the first token or line in the file. The first
21525 thing in the file might be #pragma GCC pch_preprocess, which loads a
21526 PCH file, which is a GC collection point. So we need to handle this
21527 first pragma without benefit of an existing lexer structure.
21529 Always returns one token to the caller in *FIRST_TOKEN. This is
21530 either the true first token of the file, or the first token after
21531 the initial pragma. */
21534 cp_parser_initial_pragma (cp_token *first_token)
21538 cp_lexer_get_preprocessor_token (NULL, first_token);
21539 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21542 cp_lexer_get_preprocessor_token (NULL, first_token);
21543 if (first_token->type == CPP_STRING)
21545 name = first_token->u.value;
21547 cp_lexer_get_preprocessor_token (NULL, first_token);
21548 if (first_token->type != CPP_PRAGMA_EOL)
21549 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21550 &first_token->location);
21553 error ("%Hexpected string literal", &first_token->location);
21555 /* Skip to the end of the pragma. */
21556 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21557 cp_lexer_get_preprocessor_token (NULL, first_token);
21559 /* Now actually load the PCH file. */
21561 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21563 /* Read one more token to return to our caller. We have to do this
21564 after reading the PCH file in, since its pointers have to be
21566 cp_lexer_get_preprocessor_token (NULL, first_token);
21569 /* Normal parsing of a pragma token. Here we can (and must) use the
21573 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21575 cp_token *pragma_tok;
21578 pragma_tok = cp_lexer_consume_token (parser->lexer);
21579 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21580 parser->lexer->in_pragma = true;
21582 id = pragma_tok->pragma_kind;
21585 case PRAGMA_GCC_PCH_PREPROCESS:
21586 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21587 &pragma_tok->location);
21590 case PRAGMA_OMP_BARRIER:
21593 case pragma_compound:
21594 cp_parser_omp_barrier (parser, pragma_tok);
21597 error ("%H%<#pragma omp barrier%> may only be "
21598 "used in compound statements", &pragma_tok->location);
21605 case PRAGMA_OMP_FLUSH:
21608 case pragma_compound:
21609 cp_parser_omp_flush (parser, pragma_tok);
21612 error ("%H%<#pragma omp flush%> may only be "
21613 "used in compound statements", &pragma_tok->location);
21620 case PRAGMA_OMP_TASKWAIT:
21623 case pragma_compound:
21624 cp_parser_omp_taskwait (parser, pragma_tok);
21627 error ("%H%<#pragma omp taskwait%> may only be "
21628 "used in compound statements",
21629 &pragma_tok->location);
21636 case PRAGMA_OMP_THREADPRIVATE:
21637 cp_parser_omp_threadprivate (parser, pragma_tok);
21640 case PRAGMA_OMP_ATOMIC:
21641 case PRAGMA_OMP_CRITICAL:
21642 case PRAGMA_OMP_FOR:
21643 case PRAGMA_OMP_MASTER:
21644 case PRAGMA_OMP_ORDERED:
21645 case PRAGMA_OMP_PARALLEL:
21646 case PRAGMA_OMP_SECTIONS:
21647 case PRAGMA_OMP_SINGLE:
21648 case PRAGMA_OMP_TASK:
21649 if (context == pragma_external)
21651 cp_parser_omp_construct (parser, pragma_tok);
21654 case PRAGMA_OMP_SECTION:
21655 error ("%H%<#pragma omp section%> may only be used in "
21656 "%<#pragma omp sections%> construct", &pragma_tok->location);
21660 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21661 c_invoke_pragma_handler (id);
21665 cp_parser_error (parser, "expected declaration specifiers");
21669 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21673 /* The interface the pragma parsers have to the lexer. */
21676 pragma_lex (tree *value)
21679 enum cpp_ttype ret;
21681 tok = cp_lexer_peek_token (the_parser->lexer);
21684 *value = tok->u.value;
21686 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21688 else if (ret == CPP_STRING)
21689 *value = cp_parser_string_literal (the_parser, false, false);
21692 cp_lexer_consume_token (the_parser->lexer);
21693 if (ret == CPP_KEYWORD)
21701 /* External interface. */
21703 /* Parse one entire translation unit. */
21706 c_parse_file (void)
21708 bool error_occurred;
21709 static bool already_called = false;
21711 if (already_called)
21713 sorry ("inter-module optimizations not implemented for C++");
21716 already_called = true;
21718 the_parser = cp_parser_new ();
21719 push_deferring_access_checks (flag_access_control
21720 ? dk_no_deferred : dk_no_check);
21721 error_occurred = cp_parser_translation_unit (the_parser);
21725 #include "gt-cp-parser.h"