2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
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 context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The location at which this token was found. */
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;
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
101 /* The cp_lexer structure represents the C++ lexer. It is responsible
102 for managing the token stream from the preprocessor and supplying
103 it to the parser. Tokens are never added to the cp_lexer after
106 typedef struct cp_lexer GTY (())
108 /* The memory allocated for the buffer. NULL if this lexer does not
109 own the token buffer. */
110 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
111 /* If the lexer owns the buffer, this is the number of tokens in the
113 size_t buffer_length;
115 /* A pointer just past the last available token. The tokens
116 in this lexer are [buffer, last_token). */
117 cp_token_position GTY ((skip)) last_token;
119 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
120 no more available tokens. */
121 cp_token_position GTY ((skip)) next_token;
123 /* A stack indicating positions at which cp_lexer_save_tokens was
124 called. The top entry is the most recent position at which we
125 began saving tokens. If the stack is non-empty, we are saving
127 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129 /* The next lexer in a linked list of lexers. */
130 struct cp_lexer *next;
132 /* True if we should output debugging information. */
135 /* True if we're in the context of parsing a pragma, and should not
136 increment past the end-of-line marker. */
140 /* cp_token_cache is a range of tokens. There is no need to represent
141 allocate heap memory for it, since tokens are never removed from the
142 lexer's array. There is also no need for the GC to walk through
143 a cp_token_cache, since everything in here is referenced through
146 typedef struct cp_token_cache GTY(())
148 /* The beginning of the token range. */
149 cp_token * GTY((skip)) first;
151 /* Points immediately after the last token in the range. */
152 cp_token * GTY ((skip)) last;
157 static cp_lexer *cp_lexer_new_main
159 static cp_lexer *cp_lexer_new_from_tokens
160 (cp_token_cache *tokens);
161 static void cp_lexer_destroy
163 static int cp_lexer_saving_tokens
165 static cp_token_position cp_lexer_token_position
167 static cp_token *cp_lexer_token_at
168 (cp_lexer *, cp_token_position);
169 static void cp_lexer_get_preprocessor_token
170 (cp_lexer *, cp_token *);
171 static inline cp_token *cp_lexer_peek_token
173 static cp_token *cp_lexer_peek_nth_token
174 (cp_lexer *, size_t);
175 static inline bool cp_lexer_next_token_is
176 (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_not
178 (cp_lexer *, enum cpp_ttype);
179 static bool cp_lexer_next_token_is_keyword
180 (cp_lexer *, enum rid);
181 static cp_token *cp_lexer_consume_token
183 static void cp_lexer_purge_token
185 static void cp_lexer_purge_tokens_after
186 (cp_lexer *, cp_token_position);
187 static void cp_lexer_save_tokens
189 static void cp_lexer_commit_tokens
191 static void cp_lexer_rollback_tokens
193 #ifdef ENABLE_CHECKING
194 static void cp_lexer_print_token
195 (FILE *, cp_token *);
196 static inline bool cp_lexer_debugging_p
198 static void cp_lexer_start_debugging
199 (cp_lexer *) ATTRIBUTE_UNUSED;
200 static void cp_lexer_stop_debugging
201 (cp_lexer *) ATTRIBUTE_UNUSED;
203 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
204 about passing NULL to functions that require non-NULL arguments
205 (fputs, fprintf). It will never be used, so all we need is a value
206 of the right type that's guaranteed not to be NULL. */
207 #define cp_lexer_debug_stream stdout
208 #define cp_lexer_print_token(str, tok) (void) 0
209 #define cp_lexer_debugging_p(lexer) 0
210 #endif /* ENABLE_CHECKING */
212 static cp_token_cache *cp_token_cache_new
213 (cp_token *, cp_token *);
215 static void cp_parser_initial_pragma
218 /* Manifest constants. */
219 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
220 #define CP_SAVED_TOKEN_STACK 5
222 /* A token type for keywords, as opposed to ordinary identifiers. */
223 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225 /* A token type for template-ids. If a template-id is processed while
226 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
227 the value of the CPP_TEMPLATE_ID is whatever was returned by
228 cp_parser_template_id. */
229 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231 /* A token type for nested-name-specifiers. If a
232 nested-name-specifier is processed while parsing tentatively, it is
233 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
234 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
235 cp_parser_nested_name_specifier_opt. */
236 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238 /* A token type for tokens that are not tokens at all; these are used
239 to represent slots in the array where there used to be a token
240 that has now been deleted. */
241 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243 /* The number of token types, including C++-specific ones. */
244 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
248 #ifdef ENABLE_CHECKING
249 /* The stream to which debugging output should be written. */
250 static FILE *cp_lexer_debug_stream;
251 #endif /* ENABLE_CHECKING */
253 /* Create a new main C++ lexer, the lexer that gets tokens from the
257 cp_lexer_new_main (void)
259 cp_token first_token;
266 /* It's possible that parsing the first pragma will load a PCH file,
267 which is a GC collection point. So we have to do that before
268 allocating any memory. */
269 cp_parser_initial_pragma (&first_token);
271 c_common_no_more_pch ();
273 /* Allocate the memory. */
274 lexer = GGC_CNEW (cp_lexer);
276 #ifdef ENABLE_CHECKING
277 /* Initially we are not debugging. */
278 lexer->debugging_p = false;
279 #endif /* ENABLE_CHECKING */
280 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
281 CP_SAVED_TOKEN_STACK);
283 /* Create the buffer. */
284 alloc = CP_LEXER_BUFFER_SIZE;
285 buffer = GGC_NEWVEC (cp_token, alloc);
287 /* Put the first token in the buffer. */
292 /* Get the remaining tokens from the preprocessor. */
293 while (pos->type != CPP_EOF)
300 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
301 pos = buffer + space;
303 cp_lexer_get_preprocessor_token (lexer, pos);
305 lexer->buffer = buffer;
306 lexer->buffer_length = alloc - space;
307 lexer->last_token = pos;
308 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310 /* Subsequent preprocessor diagnostics should use compiler
311 diagnostic functions to get the compiler source location. */
312 cpp_get_options (parse_in)->client_diagnostic = true;
313 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 /* Frees all resources associated with LEXER. */
350 cp_lexer_destroy (cp_lexer *lexer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
384 /* nonzero if we are presently saving tokens. */
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
425 /* Update the value. Some keywords are mapped to particular
426 entities, rather than simply having the value of the
427 corresponding IDENTIFIER_NODE. For example, `__const' is
428 mapped to `const'. */
429 token->u.value = ridpointers[token->keyword];
433 if (warn_cxx0x_compat
434 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
435 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437 /* Warn about the C++0x keyword (but still treat it as
439 warning (OPT_Wc__0x_compat,
440 "identifier %<%s%> will become a keyword in C++0x",
441 IDENTIFIER_POINTER (token->u.value));
443 /* Clear out the C_RID_CODE so we don't warn about this
444 particular identifier-turned-keyword again. */
445 C_SET_RID_CODE (token->u.value, RID_MAX);
448 token->ambiguous_p = false;
449 token->keyword = RID_MAX;
452 /* Handle Objective-C++ keywords. */
453 else if (token->type == CPP_AT_NAME)
455 token->type = CPP_KEYWORD;
456 switch (C_RID_CODE (token->u.value))
458 /* Map 'class' to '@class', 'private' to '@private', etc. */
459 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
460 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
461 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
462 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
463 case RID_THROW: token->keyword = RID_AT_THROW; break;
464 case RID_TRY: token->keyword = RID_AT_TRY; break;
465 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
466 default: token->keyword = C_RID_CODE (token->u.value);
469 else if (token->type == CPP_PRAGMA)
471 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
472 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
473 token->u.value = NULL_TREE;
477 /* Update the globals input_location and the input file stack from TOKEN. */
479 cp_lexer_set_source_position_from_token (cp_token *token)
481 if (token->type != CPP_EOF)
483 input_location = token->location;
487 /* Return a pointer to the next token in the token stream, but do not
490 static inline cp_token *
491 cp_lexer_peek_token (cp_lexer *lexer)
493 if (cp_lexer_debugging_p (lexer))
495 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
496 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
497 putc ('\n', cp_lexer_debug_stream);
499 return lexer->next_token;
502 /* Return true if the next token has the indicated TYPE. */
505 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 return cp_lexer_peek_token (lexer)->type == type;
510 /* Return true if the next token does not have the indicated TYPE. */
513 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 return !cp_lexer_next_token_is (lexer, type);
518 /* Return true if the next token is the indicated KEYWORD. */
521 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 return cp_lexer_peek_token (lexer)->keyword == keyword;
526 /* Return true if the next token is not the indicated KEYWORD. */
529 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 return cp_lexer_peek_token (lexer)->keyword != keyword;
534 /* Return true if the next token is a keyword for a decl-specifier. */
537 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
541 token = cp_lexer_peek_token (lexer);
542 switch (token->keyword)
544 /* auto specifier: storage-class-specifier in C++,
545 simple-type-specifier in C++0x. */
547 /* Storage classes. */
553 /* Elaborated type specifiers. */
559 /* Simple type specifiers. */
573 /* GNU extensions. */
576 /* C++0x extensions. */
585 /* Return a pointer to the Nth token in the token stream. If N is 1,
586 then this is precisely equivalent to cp_lexer_peek_token (except
587 that it is not inline). One would like to disallow that case, but
588 there is one case (cp_parser_nth_token_starts_template_id) where
589 the caller passes a variable for N and it might be 1. */
592 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
596 /* N is 1-based, not zero-based. */
599 if (cp_lexer_debugging_p (lexer))
600 fprintf (cp_lexer_debug_stream,
601 "cp_lexer: peeking ahead %ld at token: ", (long)n);
604 token = lexer->next_token;
605 gcc_assert (!n || token != &eof_token);
609 if (token == lexer->last_token)
615 if (token->type != CPP_PURGED)
619 if (cp_lexer_debugging_p (lexer))
621 cp_lexer_print_token (cp_lexer_debug_stream, token);
622 putc ('\n', cp_lexer_debug_stream);
628 /* Return the next token, and advance the lexer's next_token pointer
629 to point to the next non-purged token. */
632 cp_lexer_consume_token (cp_lexer* lexer)
634 cp_token *token = lexer->next_token;
636 gcc_assert (token != &eof_token);
637 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
642 if (lexer->next_token == lexer->last_token)
644 lexer->next_token = &eof_token;
649 while (lexer->next_token->type == CPP_PURGED);
651 cp_lexer_set_source_position_from_token (token);
653 /* Provide debugging output. */
654 if (cp_lexer_debugging_p (lexer))
656 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
657 cp_lexer_print_token (cp_lexer_debug_stream, token);
658 putc ('\n', cp_lexer_debug_stream);
664 /* Permanently remove the next token from the token stream, and
665 advance the next_token pointer to refer to the next non-purged
669 cp_lexer_purge_token (cp_lexer *lexer)
671 cp_token *tok = lexer->next_token;
673 gcc_assert (tok != &eof_token);
674 tok->type = CPP_PURGED;
675 tok->location = UNKNOWN_LOCATION;
676 tok->u.value = NULL_TREE;
677 tok->keyword = RID_MAX;
682 if (tok == lexer->last_token)
688 while (tok->type == CPP_PURGED);
689 lexer->next_token = tok;
692 /* Permanently remove all tokens after TOK, up to, but not
693 including, the token that will be returned next by
694 cp_lexer_peek_token. */
697 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 cp_token *peek = lexer->next_token;
701 if (peek == &eof_token)
702 peek = lexer->last_token;
704 gcc_assert (tok < peek);
706 for ( tok += 1; tok != peek; tok += 1)
708 tok->type = CPP_PURGED;
709 tok->location = UNKNOWN_LOCATION;
710 tok->u.value = NULL_TREE;
711 tok->keyword = RID_MAX;
715 /* Begin saving tokens. All tokens consumed after this point will be
719 cp_lexer_save_tokens (cp_lexer* lexer)
721 /* Provide debugging output. */
722 if (cp_lexer_debugging_p (lexer))
723 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725 VEC_safe_push (cp_token_position, heap,
726 lexer->saved_tokens, lexer->next_token);
729 /* Commit to the portion of the token stream most recently saved. */
732 cp_lexer_commit_tokens (cp_lexer* lexer)
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
736 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738 VEC_pop (cp_token_position, lexer->saved_tokens);
741 /* Return all tokens saved since the last call to cp_lexer_save_tokens
742 to the token stream. Stop saving tokens. */
745 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 /* Provide debugging output. */
748 if (cp_lexer_debugging_p (lexer))
749 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
754 /* Print a representation of the TOKEN on the STREAM. */
756 #ifdef ENABLE_CHECKING
759 cp_lexer_print_token (FILE * stream, cp_token *token)
761 /* We don't use cpp_type2name here because the parser defines
762 a few tokens of its own. */
763 static const char *const token_names[] = {
764 /* cpplib-defined token types */
770 /* C++ parser token types - see "Manifest constants", above. */
773 "NESTED_NAME_SPECIFIER",
777 /* If we have a name for the token, print it out. Otherwise, we
778 simply give the numeric code. */
779 gcc_assert (token->type < ARRAY_SIZE(token_names));
780 fputs (token_names[token->type], stream);
782 /* For some tokens, print the associated data. */
786 /* Some keywords have a value that is not an IDENTIFIER_NODE.
787 For example, `struct' is mapped to an INTEGER_CST. */
788 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790 /* else fall through */
792 fputs (IDENTIFIER_POINTER (token->u.value), stream);
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
807 /* Start emitting debugging information. */
810 cp_lexer_start_debugging (cp_lexer* lexer)
812 lexer->debugging_p = true;
815 /* Stop emitting debugging information. */
818 cp_lexer_stop_debugging (cp_lexer* lexer)
820 lexer->debugging_p = false;
823 #endif /* ENABLE_CHECKING */
825 /* Create a new cp_token_cache, representing a range of tokens. */
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
837 /* Decl-specifiers. */
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, tree, cp_cv_quals, tree, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *, bool);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
870 /* The obstack on which declarators and related data structures are
872 static struct obstack declarator_obstack;
874 /* Alloc BYTES from the declarator memory pool. */
877 alloc_declarator (size_t bytes)
879 return obstack_alloc (&declarator_obstack, bytes);
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
888 cp_declarator *declarator;
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
894 declarator->parameter_pack_p = false;
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907 special_function_kind sfk)
909 cp_declarator *declarator;
911 /* It is valid to write:
913 class C { void f(); };
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
921 if (qualifying_scope && TYPE_P (qualifying_scope))
922 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928 declarator = make_declarator (cdk_id);
929 declarator->u.id.qualifying_scope = qualifying_scope;
930 declarator->u.id.unqualified_name = unqualified_name;
931 declarator->u.id.sfk = sfk;
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 cp_declarator *declarator;
945 declarator = make_declarator (cdk_pointer);
946 declarator->declarator = target;
947 declarator->u.pointer.qualifiers = cv_qualifiers;
948 declarator->u.pointer.class_type = NULL_TREE;
951 declarator->parameter_pack_p = target->parameter_pack_p;
952 target->parameter_pack_p = false;
955 declarator->parameter_pack_p = false;
960 /* Like make_pointer_declarator -- but for references. */
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
966 cp_declarator *declarator;
968 declarator = make_declarator (cdk_reference);
969 declarator->declarator = target;
970 declarator->u.reference.qualifiers = cv_qualifiers;
971 declarator->u.reference.rvalue_ref = rvalue_ref;
974 declarator->parameter_pack_p = target->parameter_pack_p;
975 target->parameter_pack_p = false;
978 declarator->parameter_pack_p = false;
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984 member of CLASS_TYPE. */
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988 cp_declarator *pointee)
990 cp_declarator *declarator;
992 declarator = make_declarator (cdk_ptrmem);
993 declarator->declarator = pointee;
994 declarator->u.pointer.qualifiers = cv_qualifiers;
995 declarator->u.pointer.class_type = class_type;
999 declarator->parameter_pack_p = pointee->parameter_pack_p;
1000 pointee->parameter_pack_p = false;
1003 declarator->parameter_pack_p = false;
1008 /* Make a declarator for the function given by TARGET, with the
1009 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1010 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1011 indicates what exceptions can be thrown. */
1014 make_call_declarator (cp_declarator *target,
1016 cp_cv_quals cv_qualifiers,
1017 tree exception_specification,
1018 tree late_return_type)
1020 cp_declarator *declarator;
1022 declarator = make_declarator (cdk_function);
1023 declarator->declarator = target;
1024 declarator->u.function.parameters = parms;
1025 declarator->u.function.qualifiers = cv_qualifiers;
1026 declarator->u.function.exception_specification = exception_specification;
1027 declarator->u.function.late_return_type = late_return_type;
1030 declarator->parameter_pack_p = target->parameter_pack_p;
1031 target->parameter_pack_p = false;
1034 declarator->parameter_pack_p = false;
1039 /* Make a declarator for an array of BOUNDS elements, each of which is
1040 defined by ELEMENT. */
1043 make_array_declarator (cp_declarator *element, tree bounds)
1045 cp_declarator *declarator;
1047 declarator = make_declarator (cdk_array);
1048 declarator->declarator = element;
1049 declarator->u.array.bounds = bounds;
1052 declarator->parameter_pack_p = element->parameter_pack_p;
1053 element->parameter_pack_p = false;
1056 declarator->parameter_pack_p = false;
1061 /* Determine whether the declarator we've seen so far can be a
1062 parameter pack, when followed by an ellipsis. */
1064 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 /* Search for a declarator name, or any other declarator that goes
1067 after the point where the ellipsis could appear in a parameter
1068 pack. If we find any of these, then this declarator can not be
1069 made into a parameter pack. */
1071 while (declarator && !found)
1073 switch ((int)declarator->kind)
1084 declarator = declarator->declarator;
1092 cp_parameter_declarator *no_parameters;
1094 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1095 DECLARATOR and DEFAULT_ARGUMENT. */
1097 cp_parameter_declarator *
1098 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1099 cp_declarator *declarator,
1100 tree default_argument)
1102 cp_parameter_declarator *parameter;
1104 parameter = ((cp_parameter_declarator *)
1105 alloc_declarator (sizeof (cp_parameter_declarator)));
1106 parameter->next = NULL;
1107 if (decl_specifiers)
1108 parameter->decl_specifiers = *decl_specifiers;
1110 clear_decl_specs (¶meter->decl_specifiers);
1111 parameter->declarator = declarator;
1112 parameter->default_argument = default_argument;
1113 parameter->ellipsis_p = false;
1118 /* Returns true iff DECLARATOR is a declaration for a function. */
1121 function_declarator_p (const cp_declarator *declarator)
1125 if (declarator->kind == cdk_function
1126 && declarator->declarator->kind == cdk_id)
1128 if (declarator->kind == cdk_id
1129 || declarator->kind == cdk_error)
1131 declarator = declarator->declarator;
1141 A cp_parser parses the token stream as specified by the C++
1142 grammar. Its job is purely parsing, not semantic analysis. For
1143 example, the parser breaks the token stream into declarators,
1144 expressions, statements, and other similar syntactic constructs.
1145 It does not check that the types of the expressions on either side
1146 of an assignment-statement are compatible, or that a function is
1147 not declared with a parameter of type `void'.
1149 The parser invokes routines elsewhere in the compiler to perform
1150 semantic analysis and to build up the abstract syntax tree for the
1153 The parser (and the template instantiation code, which is, in a
1154 way, a close relative of parsing) are the only parts of the
1155 compiler that should be calling push_scope and pop_scope, or
1156 related functions. The parser (and template instantiation code)
1157 keeps track of what scope is presently active; everything else
1158 should simply honor that. (The code that generates static
1159 initializers may also need to set the scope, in order to check
1160 access control correctly when emitting the initializers.)
1165 The parser is of the standard recursive-descent variety. Upcoming
1166 tokens in the token stream are examined in order to determine which
1167 production to use when parsing a non-terminal. Some C++ constructs
1168 require arbitrary look ahead to disambiguate. For example, it is
1169 impossible, in the general case, to tell whether a statement is an
1170 expression or declaration without scanning the entire statement.
1171 Therefore, the parser is capable of "parsing tentatively." When the
1172 parser is not sure what construct comes next, it enters this mode.
1173 Then, while we attempt to parse the construct, the parser queues up
1174 error messages, rather than issuing them immediately, and saves the
1175 tokens it consumes. If the construct is parsed successfully, the
1176 parser "commits", i.e., it issues any queued error messages and
1177 the tokens that were being preserved are permanently discarded.
1178 If, however, the construct is not parsed successfully, the parser
1179 rolls back its state completely so that it can resume parsing using
1180 a different alternative.
1185 The performance of the parser could probably be improved substantially.
1186 We could often eliminate the need to parse tentatively by looking ahead
1187 a little bit. In some places, this approach might not entirely eliminate
1188 the need to parse tentatively, but it might still speed up the average
1191 /* Flags that are passed to some parsing functions. These values can
1192 be bitwise-ored together. */
1194 typedef enum cp_parser_flags
1197 CP_PARSER_FLAGS_NONE = 0x0,
1198 /* The construct is optional. If it is not present, then no error
1199 should be issued. */
1200 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1201 /* When parsing a type-specifier, treat user-defined type-names
1202 as non-type identifiers. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1204 /* When parsing a type-specifier, do not try to parse a class-specifier
1205 or enum-specifier. */
1206 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
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 bottommost 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_qualifying_entity
1590 (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592 (cp_parser *, bool, bool, bool, cp_id_kind *);
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, cp_id_kind *);
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, cp_id_kind *);
1621 static tree cp_parser_binary_expression
1622 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1623 static tree cp_parser_question_colon_clause
1624 (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626 (cp_parser *, bool, cp_id_kind *);
1627 static enum tree_code cp_parser_assignment_operator_opt
1629 static tree cp_parser_expression
1630 (cp_parser *, bool, cp_id_kind *);
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_late_return_type_opt
1736 static tree cp_parser_declarator_id
1737 (cp_parser *, bool);
1738 static tree cp_parser_type_id
1740 static tree cp_parser_template_type_arg
1742 static tree cp_parser_trailing_type_id (cp_parser *);
1743 static tree cp_parser_type_id_1
1744 (cp_parser *, bool, bool);
1745 static void cp_parser_type_specifier_seq
1746 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1747 static tree cp_parser_parameter_declaration_clause
1749 static tree cp_parser_parameter_declaration_list
1750 (cp_parser *, bool *);
1751 static cp_parameter_declarator *cp_parser_parameter_declaration
1752 (cp_parser *, bool, bool *);
1753 static tree cp_parser_default_argument
1754 (cp_parser *, bool);
1755 static void cp_parser_function_body
1757 static tree cp_parser_initializer
1758 (cp_parser *, bool *, bool *);
1759 static tree cp_parser_initializer_clause
1760 (cp_parser *, bool *);
1761 static tree cp_parser_braced_list
1762 (cp_parser*, bool*);
1763 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1764 (cp_parser *, bool *);
1766 static bool cp_parser_ctor_initializer_opt_and_function_body
1769 /* Classes [gram.class] */
1771 static tree cp_parser_class_name
1772 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1773 static tree cp_parser_class_specifier
1775 static tree cp_parser_class_head
1776 (cp_parser *, bool *, tree *, tree *);
1777 static enum tag_types cp_parser_class_key
1779 static void cp_parser_member_specification_opt
1781 static void cp_parser_member_declaration
1783 static tree cp_parser_pure_specifier
1785 static tree cp_parser_constant_initializer
1788 /* Derived classes [gram.class.derived] */
1790 static tree cp_parser_base_clause
1792 static tree cp_parser_base_specifier
1795 /* Special member functions [gram.special] */
1797 static tree cp_parser_conversion_function_id
1799 static tree cp_parser_conversion_type_id
1801 static cp_declarator *cp_parser_conversion_declarator_opt
1803 static bool cp_parser_ctor_initializer_opt
1805 static void cp_parser_mem_initializer_list
1807 static tree cp_parser_mem_initializer
1809 static tree cp_parser_mem_initializer_id
1812 /* Overloading [gram.over] */
1814 static tree cp_parser_operator_function_id
1816 static tree cp_parser_operator
1819 /* Templates [gram.temp] */
1821 static void cp_parser_template_declaration
1822 (cp_parser *, bool);
1823 static tree cp_parser_template_parameter_list
1825 static tree cp_parser_template_parameter
1826 (cp_parser *, bool *, bool *);
1827 static tree cp_parser_type_parameter
1828 (cp_parser *, bool *);
1829 static tree cp_parser_template_id
1830 (cp_parser *, bool, bool, bool);
1831 static tree cp_parser_template_name
1832 (cp_parser *, bool, bool, bool, bool *);
1833 static tree cp_parser_template_argument_list
1835 static tree cp_parser_template_argument
1837 static void cp_parser_explicit_instantiation
1839 static void cp_parser_explicit_specialization
1842 /* Exception handling [gram.exception] */
1844 static tree cp_parser_try_block
1846 static bool cp_parser_function_try_block
1848 static void cp_parser_handler_seq
1850 static void cp_parser_handler
1852 static tree cp_parser_exception_declaration
1854 static tree cp_parser_throw_expression
1856 static tree cp_parser_exception_specification_opt
1858 static tree cp_parser_type_id_list
1861 /* GNU Extensions */
1863 static tree cp_parser_asm_specification_opt
1865 static tree cp_parser_asm_operand_list
1867 static tree cp_parser_asm_clobber_list
1869 static tree cp_parser_attributes_opt
1871 static tree cp_parser_attribute_list
1873 static bool cp_parser_extension_opt
1874 (cp_parser *, int *);
1875 static void cp_parser_label_declaration
1878 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1879 static bool cp_parser_pragma
1880 (cp_parser *, enum pragma_context);
1882 /* Objective-C++ Productions */
1884 static tree cp_parser_objc_message_receiver
1886 static tree cp_parser_objc_message_args
1888 static tree cp_parser_objc_message_expression
1890 static tree cp_parser_objc_encode_expression
1892 static tree cp_parser_objc_defs_expression
1894 static tree cp_parser_objc_protocol_expression
1896 static tree cp_parser_objc_selector_expression
1898 static tree cp_parser_objc_expression
1900 static bool cp_parser_objc_selector_p
1902 static tree cp_parser_objc_selector
1904 static tree cp_parser_objc_protocol_refs_opt
1906 static void cp_parser_objc_declaration
1908 static tree cp_parser_objc_statement
1911 /* Utility Routines */
1913 static tree cp_parser_lookup_name
1914 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1915 static tree cp_parser_lookup_name_simple
1916 (cp_parser *, tree, location_t);
1917 static tree cp_parser_maybe_treat_template_as_class
1919 static bool cp_parser_check_declarator_template_parameters
1920 (cp_parser *, cp_declarator *, location_t);
1921 static bool cp_parser_check_template_parameters
1922 (cp_parser *, unsigned, location_t);
1923 static tree cp_parser_simple_cast_expression
1925 static tree cp_parser_global_scope_opt
1926 (cp_parser *, bool);
1927 static bool cp_parser_constructor_declarator_p
1928 (cp_parser *, bool);
1929 static tree cp_parser_function_definition_from_specifiers_and_declarator
1930 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1931 static tree cp_parser_function_definition_after_declarator
1932 (cp_parser *, bool);
1933 static void cp_parser_template_declaration_after_export
1934 (cp_parser *, bool);
1935 static void cp_parser_perform_template_parameter_access_checks
1936 (VEC (deferred_access_check,gc)*);
1937 static tree cp_parser_single_declaration
1938 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1939 static tree cp_parser_functional_cast
1940 (cp_parser *, tree);
1941 static tree cp_parser_save_member_function_body
1942 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1943 static tree cp_parser_enclosed_template_argument_list
1945 static void cp_parser_save_default_args
1946 (cp_parser *, tree);
1947 static void cp_parser_late_parsing_for_member
1948 (cp_parser *, tree);
1949 static void cp_parser_late_parsing_default_args
1950 (cp_parser *, tree);
1951 static tree cp_parser_sizeof_operand
1952 (cp_parser *, enum rid);
1953 static tree cp_parser_trait_expr
1954 (cp_parser *, enum rid);
1955 static bool cp_parser_declares_only_class_p
1957 static void cp_parser_set_storage_class
1958 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1959 static void cp_parser_set_decl_spec_type
1960 (cp_decl_specifier_seq *, tree, location_t, bool);
1961 static bool cp_parser_friend_p
1962 (const cp_decl_specifier_seq *);
1963 static cp_token *cp_parser_require
1964 (cp_parser *, enum cpp_ttype, const char *);
1965 static cp_token *cp_parser_require_keyword
1966 (cp_parser *, enum rid, const char *);
1967 static bool cp_parser_token_starts_function_definition_p
1969 static bool cp_parser_next_token_starts_class_definition_p
1971 static bool cp_parser_next_token_ends_template_argument_p
1973 static bool cp_parser_nth_token_starts_template_argument_list_p
1974 (cp_parser *, size_t);
1975 static enum tag_types cp_parser_token_is_class_key
1977 static void cp_parser_check_class_key
1978 (enum tag_types, tree type);
1979 static void cp_parser_check_access_in_redeclaration
1980 (tree type, location_t location);
1981 static bool cp_parser_optional_template_keyword
1983 static void cp_parser_pre_parsed_nested_name_specifier
1985 static bool cp_parser_cache_group
1986 (cp_parser *, enum cpp_ttype, unsigned);
1987 static void cp_parser_parse_tentatively
1989 static void cp_parser_commit_to_tentative_parse
1991 static void cp_parser_abort_tentative_parse
1993 static bool cp_parser_parse_definitely
1995 static inline bool cp_parser_parsing_tentatively
1997 static bool cp_parser_uncommitted_to_tentative_parse_p
1999 static void cp_parser_error
2000 (cp_parser *, const char *);
2001 static void cp_parser_name_lookup_error
2002 (cp_parser *, tree, tree, const char *, location_t);
2003 static bool cp_parser_simulate_error
2005 static bool cp_parser_check_type_definition
2007 static void cp_parser_check_for_definition_in_return_type
2008 (cp_declarator *, tree, location_t type_location);
2009 static void cp_parser_check_for_invalid_template_id
2010 (cp_parser *, tree, location_t location);
2011 static bool cp_parser_non_integral_constant_expression
2012 (cp_parser *, const char *);
2013 static void cp_parser_diagnose_invalid_type_name
2014 (cp_parser *, tree, tree, location_t);
2015 static bool cp_parser_parse_and_diagnose_invalid_type_name
2017 static int cp_parser_skip_to_closing_parenthesis
2018 (cp_parser *, bool, bool, bool);
2019 static void cp_parser_skip_to_end_of_statement
2021 static void cp_parser_consume_semicolon_at_end_of_statement
2023 static void cp_parser_skip_to_end_of_block_or_statement
2025 static bool cp_parser_skip_to_closing_brace
2027 static void cp_parser_skip_to_end_of_template_parameter_list
2029 static void cp_parser_skip_to_pragma_eol
2030 (cp_parser*, cp_token *);
2031 static bool cp_parser_error_occurred
2033 static bool cp_parser_allow_gnu_extensions_p
2035 static bool cp_parser_is_string_literal
2037 static bool cp_parser_is_keyword
2038 (cp_token *, enum rid);
2039 static tree cp_parser_make_typename_type
2040 (cp_parser *, tree, tree, location_t location);
2041 static cp_declarator * cp_parser_make_indirect_declarator
2042 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2044 /* Returns nonzero if we are parsing tentatively. */
2047 cp_parser_parsing_tentatively (cp_parser* parser)
2049 return parser->context->next != NULL;
2052 /* Returns nonzero if TOKEN is a string literal. */
2055 cp_parser_is_string_literal (cp_token* token)
2057 return (token->type == CPP_STRING ||
2058 token->type == CPP_STRING16 ||
2059 token->type == CPP_STRING32 ||
2060 token->type == CPP_WSTRING);
2063 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2066 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2068 return token->keyword == keyword;
2071 /* If not parsing tentatively, issue a diagnostic of the form
2072 FILE:LINE: MESSAGE before TOKEN
2073 where TOKEN is the next token in the input stream. MESSAGE
2074 (specified by the caller) is usually of the form "expected
2078 cp_parser_error (cp_parser* parser, const char* message)
2080 if (!cp_parser_simulate_error (parser))
2082 cp_token *token = cp_lexer_peek_token (parser->lexer);
2083 /* This diagnostic makes more sense if it is tagged to the line
2084 of the token we just peeked at. */
2085 cp_lexer_set_source_position_from_token (token);
2087 if (token->type == CPP_PRAGMA)
2089 error ("%H%<#pragma%> is not allowed here", &token->location);
2090 cp_parser_skip_to_pragma_eol (parser, token);
2094 c_parse_error (message,
2095 /* Because c_parser_error does not understand
2096 CPP_KEYWORD, keywords are treated like
2098 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2103 /* Issue an error about name-lookup failing. NAME is the
2104 IDENTIFIER_NODE DECL is the result of
2105 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2106 the thing that we hoped to find. */
2109 cp_parser_name_lookup_error (cp_parser* parser,
2112 const char* desired,
2113 location_t location)
2115 /* If name lookup completely failed, tell the user that NAME was not
2117 if (decl == error_mark_node)
2119 if (parser->scope && parser->scope != global_namespace)
2120 error ("%H%<%E::%E%> has not been declared",
2121 &location, parser->scope, name);
2122 else if (parser->scope == global_namespace)
2123 error ("%H%<::%E%> has not been declared", &location, name);
2124 else if (parser->object_scope
2125 && !CLASS_TYPE_P (parser->object_scope))
2126 error ("%Hrequest for member %qE in non-class type %qT",
2127 &location, name, parser->object_scope);
2128 else if (parser->object_scope)
2129 error ("%H%<%T::%E%> has not been declared",
2130 &location, parser->object_scope, name);
2132 error ("%H%qE has not been declared", &location, name);
2134 else if (parser->scope && parser->scope != global_namespace)
2135 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2136 else if (parser->scope == global_namespace)
2137 error ("%H%<::%E%> %s", &location, name, desired);
2139 error ("%H%qE %s", &location, name, desired);
2142 /* If we are parsing tentatively, remember that an error has occurred
2143 during this tentative parse. Returns true if the error was
2144 simulated; false if a message should be issued by the caller. */
2147 cp_parser_simulate_error (cp_parser* parser)
2149 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2151 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2157 /* Check for repeated decl-specifiers. */
2160 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2161 location_t location)
2165 for (ds = ds_first; ds != ds_last; ++ds)
2167 unsigned count = decl_specs->specs[(int)ds];
2170 /* The "long" specifier is a special case because of "long long". */
2174 error ("%H%<long long long%> is too long for GCC", &location);
2175 else if (pedantic && !in_system_header && warn_long_long
2176 && cxx_dialect == cxx98)
2177 pedwarn (location, OPT_Wlong_long,
2178 "ISO C++ 1998 does not support %<long long%>");
2182 static const char *const decl_spec_names[] = {
2198 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2203 /* This function is called when a type is defined. If type
2204 definitions are forbidden at this point, an error message is
2208 cp_parser_check_type_definition (cp_parser* parser)
2210 /* If types are forbidden here, issue a message. */
2211 if (parser->type_definition_forbidden_message)
2213 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2214 in the message need to be interpreted. */
2215 error (parser->type_definition_forbidden_message);
2221 /* This function is called when the DECLARATOR is processed. The TYPE
2222 was a type defined in the decl-specifiers. If it is invalid to
2223 define a type in the decl-specifiers for DECLARATOR, an error is
2224 issued. TYPE_LOCATION is the location of TYPE and is used
2225 for error reporting. */
2228 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2229 tree type, location_t type_location)
2231 /* [dcl.fct] forbids type definitions in return types.
2232 Unfortunately, it's not easy to know whether or not we are
2233 processing a return type until after the fact. */
2235 && (declarator->kind == cdk_pointer
2236 || declarator->kind == cdk_reference
2237 || declarator->kind == cdk_ptrmem))
2238 declarator = declarator->declarator;
2240 && declarator->kind == cdk_function)
2242 error ("%Hnew types may not be defined in a return type", &type_location);
2243 inform (type_location,
2244 "(perhaps a semicolon is missing after the definition of %qT)",
2249 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2250 "<" in any valid C++ program. If the next token is indeed "<",
2251 issue a message warning the user about what appears to be an
2252 invalid attempt to form a template-id. LOCATION is the location
2253 of the type-specifier (TYPE) */
2256 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2257 tree type, location_t location)
2259 cp_token_position start = 0;
2261 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2264 error ("%H%qT is not a template", &location, type);
2265 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2266 error ("%H%qE is not a template", &location, type);
2268 error ("%Hinvalid template-id", &location);
2269 /* Remember the location of the invalid "<". */
2270 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2271 start = cp_lexer_token_position (parser->lexer, true);
2272 /* Consume the "<". */
2273 cp_lexer_consume_token (parser->lexer);
2274 /* Parse the template arguments. */
2275 cp_parser_enclosed_template_argument_list (parser);
2276 /* Permanently remove the invalid template arguments so that
2277 this error message is not issued again. */
2279 cp_lexer_purge_tokens_after (parser->lexer, start);
2283 /* If parsing an integral constant-expression, issue an error message
2284 about the fact that THING appeared and return true. Otherwise,
2285 return false. In either case, set
2286 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2289 cp_parser_non_integral_constant_expression (cp_parser *parser,
2292 parser->non_integral_constant_expression_p = true;
2293 if (parser->integral_constant_expression_p)
2295 if (!parser->allow_non_integral_constant_expression_p)
2297 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2298 in the message need to be interpreted. */
2299 char *message = concat (thing,
2300 " cannot appear in a constant-expression",
2310 /* Emit a diagnostic for an invalid type name. SCOPE is the
2311 qualifying scope (or NULL, if none) for ID. This function commits
2312 to the current active tentative parse, if any. (Otherwise, the
2313 problematic construct might be encountered again later, resulting
2314 in duplicate error messages.) LOCATION is the location of ID. */
2317 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2318 tree scope, tree id,
2319 location_t location)
2321 tree decl, old_scope;
2322 /* Try to lookup the identifier. */
2323 old_scope = parser->scope;
2324 parser->scope = scope;
2325 decl = cp_parser_lookup_name_simple (parser, id, location);
2326 parser->scope = old_scope;
2327 /* If the lookup found a template-name, it means that the user forgot
2328 to specify an argument list. Emit a useful error message. */
2329 if (TREE_CODE (decl) == TEMPLATE_DECL)
2330 error ("%Hinvalid use of template-name %qE without an argument list",
2332 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2333 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2334 else if (TREE_CODE (decl) == TYPE_DECL)
2335 /* Something like 'unsigned A a;' */
2336 error ("%Hinvalid combination of multiple type-specifiers",
2338 else if (!parser->scope)
2340 /* Issue an error message. */
2341 error ("%H%qE does not name a type", &location, id);
2342 /* If we're in a template class, it's possible that the user was
2343 referring to a type from a base class. For example:
2345 template <typename T> struct A { typedef T X; };
2346 template <typename T> struct B : public A<T> { X x; };
2348 The user should have said "typename A<T>::X". */
2349 if (processing_template_decl && current_class_type
2350 && TYPE_BINFO (current_class_type))
2354 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2358 tree base_type = BINFO_TYPE (b);
2359 if (CLASS_TYPE_P (base_type)
2360 && dependent_type_p (base_type))
2363 /* Go from a particular instantiation of the
2364 template (which will have an empty TYPE_FIELDs),
2365 to the main version. */
2366 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2367 for (field = TYPE_FIELDS (base_type);
2369 field = TREE_CHAIN (field))
2370 if (TREE_CODE (field) == TYPE_DECL
2371 && DECL_NAME (field) == id)
2374 "(perhaps %<typename %T::%E%> was intended)",
2375 BINFO_TYPE (b), id);
2384 /* Here we diagnose qualified-ids where the scope is actually correct,
2385 but the identifier does not resolve to a valid type name. */
2386 else if (parser->scope != error_mark_node)
2388 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2389 error ("%H%qE in namespace %qE does not name a type",
2390 &location, id, parser->scope);
2391 else if (TYPE_P (parser->scope))
2392 error ("%H%qE in class %qT does not name a type",
2393 &location, id, parser->scope);
2397 cp_parser_commit_to_tentative_parse (parser);
2400 /* Check for a common situation where a type-name should be present,
2401 but is not, and issue a sensible error message. Returns true if an
2402 invalid type-name was detected.
2404 The situation handled by this function are variable declarations of the
2405 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2406 Usually, `ID' should name a type, but if we got here it means that it
2407 does not. We try to emit the best possible error message depending on
2408 how exactly the id-expression looks like. */
2411 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2414 cp_token *token = cp_lexer_peek_token (parser->lexer);
2416 cp_parser_parse_tentatively (parser);
2417 id = cp_parser_id_expression (parser,
2418 /*template_keyword_p=*/false,
2419 /*check_dependency_p=*/true,
2420 /*template_p=*/NULL,
2421 /*declarator_p=*/true,
2422 /*optional_p=*/false);
2423 /* After the id-expression, there should be a plain identifier,
2424 otherwise this is not a simple variable declaration. Also, if
2425 the scope is dependent, we cannot do much. */
2426 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2427 || (parser->scope && TYPE_P (parser->scope)
2428 && dependent_type_p (parser->scope))
2429 || TREE_CODE (id) == TYPE_DECL)
2431 cp_parser_abort_tentative_parse (parser);
2434 if (!cp_parser_parse_definitely (parser))
2437 /* Emit a diagnostic for the invalid type. */
2438 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2439 id, token->location);
2440 /* Skip to the end of the declaration; there's no point in
2441 trying to process it. */
2442 cp_parser_skip_to_end_of_block_or_statement (parser);
2446 /* Consume tokens up to, and including, the next non-nested closing `)'.
2447 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2448 are doing error recovery. Returns -1 if OR_COMMA is true and we
2449 found an unnested comma. */
2452 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2457 unsigned paren_depth = 0;
2458 unsigned brace_depth = 0;
2460 if (recovering && !or_comma
2461 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2466 cp_token * token = cp_lexer_peek_token (parser->lexer);
2468 switch (token->type)
2471 case CPP_PRAGMA_EOL:
2472 /* If we've run out of tokens, then there is no closing `)'. */
2476 /* This matches the processing in skip_to_end_of_statement. */
2481 case CPP_OPEN_BRACE:
2484 case CPP_CLOSE_BRACE:
2490 if (recovering && or_comma && !brace_depth && !paren_depth)
2494 case CPP_OPEN_PAREN:
2499 case CPP_CLOSE_PAREN:
2500 if (!brace_depth && !paren_depth--)
2503 cp_lexer_consume_token (parser->lexer);
2512 /* Consume the token. */
2513 cp_lexer_consume_token (parser->lexer);
2517 /* Consume tokens until we reach the end of the current statement.
2518 Normally, that will be just before consuming a `;'. However, if a
2519 non-nested `}' comes first, then we stop before consuming that. */
2522 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2524 unsigned nesting_depth = 0;
2528 cp_token *token = cp_lexer_peek_token (parser->lexer);
2530 switch (token->type)
2533 case CPP_PRAGMA_EOL:
2534 /* If we've run out of tokens, stop. */
2538 /* If the next token is a `;', we have reached the end of the
2544 case CPP_CLOSE_BRACE:
2545 /* If this is a non-nested '}', stop before consuming it.
2546 That way, when confronted with something like:
2550 we stop before consuming the closing '}', even though we
2551 have not yet reached a `;'. */
2552 if (nesting_depth == 0)
2555 /* If it is the closing '}' for a block that we have
2556 scanned, stop -- but only after consuming the token.
2562 we will stop after the body of the erroneously declared
2563 function, but before consuming the following `typedef'
2565 if (--nesting_depth == 0)
2567 cp_lexer_consume_token (parser->lexer);
2571 case CPP_OPEN_BRACE:
2579 /* Consume the token. */
2580 cp_lexer_consume_token (parser->lexer);
2584 /* This function is called at the end of a statement or declaration.
2585 If the next token is a semicolon, it is consumed; otherwise, error
2586 recovery is attempted. */
2589 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2591 /* Look for the trailing `;'. */
2592 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2594 /* If there is additional (erroneous) input, skip to the end of
2596 cp_parser_skip_to_end_of_statement (parser);
2597 /* If the next token is now a `;', consume it. */
2598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2599 cp_lexer_consume_token (parser->lexer);
2603 /* Skip tokens until we have consumed an entire block, or until we
2604 have consumed a non-nested `;'. */
2607 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2609 int nesting_depth = 0;
2611 while (nesting_depth >= 0)
2613 cp_token *token = cp_lexer_peek_token (parser->lexer);
2615 switch (token->type)
2618 case CPP_PRAGMA_EOL:
2619 /* If we've run out of tokens, stop. */
2623 /* Stop if this is an unnested ';'. */
2628 case CPP_CLOSE_BRACE:
2629 /* Stop if this is an unnested '}', or closes the outermost
2632 if (nesting_depth < 0)
2638 case CPP_OPEN_BRACE:
2647 /* Consume the token. */
2648 cp_lexer_consume_token (parser->lexer);
2652 /* Skip tokens until a non-nested closing curly brace is the next
2653 token, or there are no more tokens. Return true in the first case,
2657 cp_parser_skip_to_closing_brace (cp_parser *parser)
2659 unsigned nesting_depth = 0;
2663 cp_token *token = cp_lexer_peek_token (parser->lexer);
2665 switch (token->type)
2668 case CPP_PRAGMA_EOL:
2669 /* If we've run out of tokens, stop. */
2672 case CPP_CLOSE_BRACE:
2673 /* If the next token is a non-nested `}', then we have reached
2674 the end of the current block. */
2675 if (nesting_depth-- == 0)
2679 case CPP_OPEN_BRACE:
2680 /* If it the next token is a `{', then we are entering a new
2681 block. Consume the entire block. */
2689 /* Consume the token. */
2690 cp_lexer_consume_token (parser->lexer);
2694 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2695 parameter is the PRAGMA token, allowing us to purge the entire pragma
2699 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2703 parser->lexer->in_pragma = false;
2706 token = cp_lexer_consume_token (parser->lexer);
2707 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2709 /* Ensure that the pragma is not parsed again. */
2710 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2713 /* Require pragma end of line, resyncing with it as necessary. The
2714 arguments are as for cp_parser_skip_to_pragma_eol. */
2717 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2719 parser->lexer->in_pragma = false;
2720 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2721 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2724 /* This is a simple wrapper around make_typename_type. When the id is
2725 an unresolved identifier node, we can provide a superior diagnostic
2726 using cp_parser_diagnose_invalid_type_name. */
2729 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2730 tree id, location_t id_location)
2733 if (TREE_CODE (id) == IDENTIFIER_NODE)
2735 result = make_typename_type (scope, id, typename_type,
2736 /*complain=*/tf_none);
2737 if (result == error_mark_node)
2738 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2741 return make_typename_type (scope, id, typename_type, tf_error);
2744 /* This is a wrapper around the
2745 make_{pointer,ptrmem,reference}_declarator functions that decides
2746 which one to call based on the CODE and CLASS_TYPE arguments. The
2747 CODE argument should be one of the values returned by
2748 cp_parser_ptr_operator. */
2749 static cp_declarator *
2750 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2751 cp_cv_quals cv_qualifiers,
2752 cp_declarator *target)
2754 if (code == ERROR_MARK)
2755 return cp_error_declarator;
2757 if (code == INDIRECT_REF)
2758 if (class_type == NULL_TREE)
2759 return make_pointer_declarator (cv_qualifiers, target);
2761 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2762 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2763 return make_reference_declarator (cv_qualifiers, target, false);
2764 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2765 return make_reference_declarator (cv_qualifiers, target, true);
2769 /* Create a new C++ parser. */
2772 cp_parser_new (void)
2778 /* cp_lexer_new_main is called before calling ggc_alloc because
2779 cp_lexer_new_main might load a PCH file. */
2780 lexer = cp_lexer_new_main ();
2782 /* Initialize the binops_by_token so that we can get the tree
2783 directly from the token. */
2784 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2785 binops_by_token[binops[i].token_type] = binops[i];
2787 parser = GGC_CNEW (cp_parser);
2788 parser->lexer = lexer;
2789 parser->context = cp_parser_context_new (NULL);
2791 /* For now, we always accept GNU extensions. */
2792 parser->allow_gnu_extensions_p = 1;
2794 /* The `>' token is a greater-than operator, not the end of a
2796 parser->greater_than_is_operator_p = true;
2798 parser->default_arg_ok_p = true;
2800 /* We are not parsing a constant-expression. */
2801 parser->integral_constant_expression_p = false;
2802 parser->allow_non_integral_constant_expression_p = false;
2803 parser->non_integral_constant_expression_p = false;
2805 /* Local variable names are not forbidden. */
2806 parser->local_variables_forbidden_p = false;
2808 /* We are not processing an `extern "C"' declaration. */
2809 parser->in_unbraced_linkage_specification_p = false;
2811 /* We are not processing a declarator. */
2812 parser->in_declarator_p = false;
2814 /* We are not processing a template-argument-list. */
2815 parser->in_template_argument_list_p = false;
2817 /* We are not in an iteration statement. */
2818 parser->in_statement = 0;
2820 /* We are not in a switch statement. */
2821 parser->in_switch_statement_p = false;
2823 /* We are not parsing a type-id inside an expression. */
2824 parser->in_type_id_in_expr_p = false;
2826 /* Declarations aren't implicitly extern "C". */
2827 parser->implicit_extern_c = false;
2829 /* String literals should be translated to the execution character set. */
2830 parser->translate_strings_p = true;
2832 /* We are not parsing a function body. */
2833 parser->in_function_body = false;
2835 /* The unparsed function queue is empty. */
2836 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2838 /* There are no classes being defined. */
2839 parser->num_classes_being_defined = 0;
2841 /* No template parameters apply. */
2842 parser->num_template_parameter_lists = 0;
2847 /* Create a cp_lexer structure which will emit the tokens in CACHE
2848 and push it onto the parser's lexer stack. This is used for delayed
2849 parsing of in-class method bodies and default arguments, and should
2850 not be confused with tentative parsing. */
2852 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2854 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2855 lexer->next = parser->lexer;
2856 parser->lexer = lexer;
2858 /* Move the current source position to that of the first token in the
2860 cp_lexer_set_source_position_from_token (lexer->next_token);
2863 /* Pop the top lexer off the parser stack. This is never used for the
2864 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2866 cp_parser_pop_lexer (cp_parser *parser)
2868 cp_lexer *lexer = parser->lexer;
2869 parser->lexer = lexer->next;
2870 cp_lexer_destroy (lexer);
2872 /* Put the current source position back where it was before this
2873 lexer was pushed. */
2874 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2877 /* Lexical conventions [gram.lex] */
2879 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2883 cp_parser_identifier (cp_parser* parser)
2887 /* Look for the identifier. */
2888 token = cp_parser_require (parser, CPP_NAME, "identifier");
2889 /* Return the value. */
2890 return token ? token->u.value : error_mark_node;
2893 /* Parse a sequence of adjacent string constants. Returns a
2894 TREE_STRING representing the combined, nul-terminated string
2895 constant. If TRANSLATE is true, translate the string to the
2896 execution character set. If WIDE_OK is true, a wide string is
2899 C++98 [lex.string] says that if a narrow string literal token is
2900 adjacent to a wide string literal token, the behavior is undefined.
2901 However, C99 6.4.5p4 says that this results in a wide string literal.
2902 We follow C99 here, for consistency with the C front end.
2904 This code is largely lifted from lex_string() in c-lex.c.
2906 FUTURE: ObjC++ will need to handle @-strings here. */
2908 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2912 struct obstack str_ob;
2913 cpp_string str, istr, *strs;
2915 enum cpp_ttype type;
2917 tok = cp_lexer_peek_token (parser->lexer);
2918 if (!cp_parser_is_string_literal (tok))
2920 cp_parser_error (parser, "expected string-literal");
2921 return error_mark_node;
2926 /* Try to avoid the overhead of creating and destroying an obstack
2927 for the common case of just one string. */
2928 if (!cp_parser_is_string_literal
2929 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2931 cp_lexer_consume_token (parser->lexer);
2933 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2934 str.len = TREE_STRING_LENGTH (tok->u.value);
2941 gcc_obstack_init (&str_ob);
2946 cp_lexer_consume_token (parser->lexer);
2948 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2949 str.len = TREE_STRING_LENGTH (tok->u.value);
2951 if (type != tok->type)
2953 if (type == CPP_STRING)
2955 else if (tok->type != CPP_STRING)
2956 error ("%Hunsupported non-standard concatenation "
2957 "of string literals", &tok->location);
2960 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2962 tok = cp_lexer_peek_token (parser->lexer);
2964 while (cp_parser_is_string_literal (tok));
2966 strs = (cpp_string *) obstack_finish (&str_ob);
2969 if (type != CPP_STRING && !wide_ok)
2971 cp_parser_error (parser, "a wide string is invalid in this context");
2975 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2976 (parse_in, strs, count, &istr, type))
2978 value = build_string (istr.len, (const char *)istr.text);
2979 free (CONST_CAST (unsigned char *, istr.text));
2985 TREE_TYPE (value) = char_array_type_node;
2988 TREE_TYPE (value) = char16_array_type_node;
2991 TREE_TYPE (value) = char32_array_type_node;
2994 TREE_TYPE (value) = wchar_array_type_node;
2998 value = fix_string_type (value);
3001 /* cpp_interpret_string has issued an error. */
3002 value = error_mark_node;
3005 obstack_free (&str_ob, 0);
3011 /* Basic concepts [gram.basic] */
3013 /* Parse a translation-unit.
3016 declaration-seq [opt]
3018 Returns TRUE if all went well. */
3021 cp_parser_translation_unit (cp_parser* parser)
3023 /* The address of the first non-permanent object on the declarator
3025 static void *declarator_obstack_base;
3029 /* Create the declarator obstack, if necessary. */
3030 if (!cp_error_declarator)
3032 gcc_obstack_init (&declarator_obstack);
3033 /* Create the error declarator. */
3034 cp_error_declarator = make_declarator (cdk_error);
3035 /* Create the empty parameter list. */
3036 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3037 /* Remember where the base of the declarator obstack lies. */
3038 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3041 cp_parser_declaration_seq_opt (parser);
3043 /* If there are no tokens left then all went well. */
3044 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3046 /* Get rid of the token array; we don't need it any more. */
3047 cp_lexer_destroy (parser->lexer);
3048 parser->lexer = NULL;
3050 /* This file might have been a context that's implicitly extern
3051 "C". If so, pop the lang context. (Only relevant for PCH.) */
3052 if (parser->implicit_extern_c)
3054 pop_lang_context ();
3055 parser->implicit_extern_c = false;
3059 finish_translation_unit ();
3065 cp_parser_error (parser, "expected declaration");
3069 /* Make sure the declarator obstack was fully cleaned up. */
3070 gcc_assert (obstack_next_free (&declarator_obstack)
3071 == declarator_obstack_base);
3073 /* All went well. */
3077 /* Expressions [gram.expr] */
3079 /* Parse a primary-expression.
3090 ( compound-statement )
3091 __builtin_va_arg ( assignment-expression , type-id )
3092 __builtin_offsetof ( type-id , offsetof-expression )
3095 __has_nothrow_assign ( type-id )
3096 __has_nothrow_constructor ( type-id )
3097 __has_nothrow_copy ( type-id )
3098 __has_trivial_assign ( type-id )
3099 __has_trivial_constructor ( type-id )
3100 __has_trivial_copy ( type-id )
3101 __has_trivial_destructor ( type-id )
3102 __has_virtual_destructor ( type-id )
3103 __is_abstract ( type-id )
3104 __is_base_of ( type-id , type-id )
3105 __is_class ( type-id )
3106 __is_convertible_to ( type-id , type-id )
3107 __is_empty ( type-id )
3108 __is_enum ( type-id )
3109 __is_pod ( type-id )
3110 __is_polymorphic ( type-id )
3111 __is_union ( type-id )
3113 Objective-C++ Extension:
3121 ADDRESS_P is true iff this expression was immediately preceded by
3122 "&" and therefore might denote a pointer-to-member. CAST_P is true
3123 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3124 true iff this expression is a template argument.
3126 Returns a representation of the expression. Upon return, *IDK
3127 indicates what kind of id-expression (if any) was present. */
3130 cp_parser_primary_expression (cp_parser *parser,
3133 bool template_arg_p,
3136 cp_token *token = NULL;
3138 /* Assume the primary expression is not an id-expression. */
3139 *idk = CP_ID_KIND_NONE;
3141 /* Peek at the next token. */
3142 token = cp_lexer_peek_token (parser->lexer);
3143 switch (token->type)
3156 token = cp_lexer_consume_token (parser->lexer);
3157 if (TREE_CODE (token->u.value) == FIXED_CST)
3159 error ("%Hfixed-point types not supported in C++",
3161 return error_mark_node;
3163 /* Floating-point literals are only allowed in an integral
3164 constant expression if they are cast to an integral or
3165 enumeration type. */
3166 if (TREE_CODE (token->u.value) == REAL_CST
3167 && parser->integral_constant_expression_p
3170 /* CAST_P will be set even in invalid code like "int(2.7 +
3171 ...)". Therefore, we have to check that the next token
3172 is sure to end the cast. */
3175 cp_token *next_token;
3177 next_token = cp_lexer_peek_token (parser->lexer);
3178 if (/* The comma at the end of an
3179 enumerator-definition. */
3180 next_token->type != CPP_COMMA
3181 /* The curly brace at the end of an enum-specifier. */
3182 && next_token->type != CPP_CLOSE_BRACE
3183 /* The end of a statement. */
3184 && next_token->type != CPP_SEMICOLON
3185 /* The end of the cast-expression. */
3186 && next_token->type != CPP_CLOSE_PAREN
3187 /* The end of an array bound. */
3188 && next_token->type != CPP_CLOSE_SQUARE
3189 /* The closing ">" in a template-argument-list. */
3190 && (next_token->type != CPP_GREATER
3191 || parser->greater_than_is_operator_p)
3192 /* C++0x only: A ">>" treated like two ">" tokens,
3193 in a template-argument-list. */
3194 && (next_token->type != CPP_RSHIFT
3195 || (cxx_dialect == cxx98)
3196 || parser->greater_than_is_operator_p))
3200 /* If we are within a cast, then the constraint that the
3201 cast is to an integral or enumeration type will be
3202 checked at that point. If we are not within a cast, then
3203 this code is invalid. */
3205 cp_parser_non_integral_constant_expression
3206 (parser, "floating-point literal");
3208 return token->u.value;
3214 /* ??? Should wide strings be allowed when parser->translate_strings_p
3215 is false (i.e. in attributes)? If not, we can kill the third
3216 argument to cp_parser_string_literal. */
3217 return cp_parser_string_literal (parser,
3218 parser->translate_strings_p,
3221 case CPP_OPEN_PAREN:
3224 bool saved_greater_than_is_operator_p;
3226 /* Consume the `('. */
3227 cp_lexer_consume_token (parser->lexer);
3228 /* Within a parenthesized expression, a `>' token is always
3229 the greater-than operator. */
3230 saved_greater_than_is_operator_p
3231 = parser->greater_than_is_operator_p;
3232 parser->greater_than_is_operator_p = true;
3233 /* If we see `( { ' then we are looking at the beginning of
3234 a GNU statement-expression. */
3235 if (cp_parser_allow_gnu_extensions_p (parser)
3236 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3238 /* Statement-expressions are not allowed by the standard. */
3239 pedwarn (token->location, OPT_pedantic,
3240 "ISO C++ forbids braced-groups within expressions");
3242 /* And they're not allowed outside of a function-body; you
3243 cannot, for example, write:
3245 int i = ({ int j = 3; j + 1; });
3247 at class or namespace scope. */
3248 if (!parser->in_function_body
3249 || parser->in_template_argument_list_p)
3251 error ("%Hstatement-expressions are not allowed outside "
3252 "functions nor in template-argument lists",
3254 cp_parser_skip_to_end_of_block_or_statement (parser);
3255 expr = error_mark_node;
3259 /* Start the statement-expression. */
3260 expr = begin_stmt_expr ();
3261 /* Parse the compound-statement. */
3262 cp_parser_compound_statement (parser, expr, false);
3264 expr = finish_stmt_expr (expr, false);
3269 /* Parse the parenthesized expression. */
3270 expr = cp_parser_expression (parser, cast_p, idk);
3271 /* Let the front end know that this expression was
3272 enclosed in parentheses. This matters in case, for
3273 example, the expression is of the form `A::B', since
3274 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3276 finish_parenthesized_expr (expr);
3277 /* DR 705: Wrapping an unqualified name in parentheses
3278 suppresses arg-dependent lookup. We want to pass back
3279 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3280 (c++/37862), but none of the others. */
3281 if (*idk != CP_ID_KIND_QUALIFIED)
3282 *idk = CP_ID_KIND_NONE;
3284 /* The `>' token might be the end of a template-id or
3285 template-parameter-list now. */
3286 parser->greater_than_is_operator_p
3287 = saved_greater_than_is_operator_p;
3288 /* Consume the `)'. */
3289 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3290 cp_parser_skip_to_end_of_statement (parser);
3296 switch (token->keyword)
3298 /* These two are the boolean literals. */
3300 cp_lexer_consume_token (parser->lexer);
3301 return boolean_true_node;
3303 cp_lexer_consume_token (parser->lexer);
3304 return boolean_false_node;
3306 /* The `__null' literal. */
3308 cp_lexer_consume_token (parser->lexer);
3311 /* Recognize the `this' keyword. */
3313 cp_lexer_consume_token (parser->lexer);
3314 if (parser->local_variables_forbidden_p)
3316 error ("%H%<this%> may not be used in this context",
3318 return error_mark_node;
3320 /* Pointers cannot appear in constant-expressions. */
3321 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3322 return error_mark_node;
3323 return finish_this_expr ();
3325 /* The `operator' keyword can be the beginning of an
3330 case RID_FUNCTION_NAME:
3331 case RID_PRETTY_FUNCTION_NAME:
3332 case RID_C99_FUNCTION_NAME:
3336 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3337 __func__ are the names of variables -- but they are
3338 treated specially. Therefore, they are handled here,
3339 rather than relying on the generic id-expression logic
3340 below. Grammatically, these names are id-expressions.
3342 Consume the token. */
3343 token = cp_lexer_consume_token (parser->lexer);
3345 switch (token->keyword)
3347 case RID_FUNCTION_NAME:
3348 name = "%<__FUNCTION__%>";
3350 case RID_PRETTY_FUNCTION_NAME:
3351 name = "%<__PRETTY_FUNCTION__%>";
3353 case RID_C99_FUNCTION_NAME:
3354 name = "%<__func__%>";
3360 if (cp_parser_non_integral_constant_expression (parser, name))
3361 return error_mark_node;
3363 /* Look up the name. */
3364 return finish_fname (token->u.value);
3372 /* The `__builtin_va_arg' construct is used to handle
3373 `va_arg'. Consume the `__builtin_va_arg' token. */
3374 cp_lexer_consume_token (parser->lexer);
3375 /* Look for the opening `('. */
3376 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3377 /* Now, parse the assignment-expression. */
3378 expression = cp_parser_assignment_expression (parser,
3379 /*cast_p=*/false, NULL);
3380 /* Look for the `,'. */
3381 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3382 /* Parse the type-id. */
3383 type = cp_parser_type_id (parser);
3384 /* Look for the closing `)'. */
3385 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3386 /* Using `va_arg' in a constant-expression is not
3388 if (cp_parser_non_integral_constant_expression (parser,
3390 return error_mark_node;
3391 return build_x_va_arg (expression, type);
3395 return cp_parser_builtin_offsetof (parser);
3397 case RID_HAS_NOTHROW_ASSIGN:
3398 case RID_HAS_NOTHROW_CONSTRUCTOR:
3399 case RID_HAS_NOTHROW_COPY:
3400 case RID_HAS_TRIVIAL_ASSIGN:
3401 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3402 case RID_HAS_TRIVIAL_COPY:
3403 case RID_HAS_TRIVIAL_DESTRUCTOR:
3404 case RID_HAS_VIRTUAL_DESTRUCTOR:
3405 case RID_IS_ABSTRACT:
3406 case RID_IS_BASE_OF:
3408 case RID_IS_CONVERTIBLE_TO:
3412 case RID_IS_POLYMORPHIC:
3414 return cp_parser_trait_expr (parser, token->keyword);
3416 /* Objective-C++ expressions. */
3418 case RID_AT_PROTOCOL:
3419 case RID_AT_SELECTOR:
3420 return cp_parser_objc_expression (parser);
3423 cp_parser_error (parser, "expected primary-expression");
3424 return error_mark_node;
3427 /* An id-expression can start with either an identifier, a
3428 `::' as the beginning of a qualified-id, or the "operator"
3432 case CPP_TEMPLATE_ID:
3433 case CPP_NESTED_NAME_SPECIFIER:
3437 const char *error_msg;
3440 cp_token *id_expr_token;
3443 /* Parse the id-expression. */
3445 = cp_parser_id_expression (parser,
3446 /*template_keyword_p=*/false,
3447 /*check_dependency_p=*/true,
3449 /*declarator_p=*/false,
3450 /*optional_p=*/false);
3451 if (id_expression == error_mark_node)
3452 return error_mark_node;
3453 id_expr_token = token;
3454 token = cp_lexer_peek_token (parser->lexer);
3455 done = (token->type != CPP_OPEN_SQUARE
3456 && token->type != CPP_OPEN_PAREN
3457 && token->type != CPP_DOT
3458 && token->type != CPP_DEREF
3459 && token->type != CPP_PLUS_PLUS
3460 && token->type != CPP_MINUS_MINUS);
3461 /* If we have a template-id, then no further lookup is
3462 required. If the template-id was for a template-class, we
3463 will sometimes have a TYPE_DECL at this point. */
3464 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3465 || TREE_CODE (id_expression) == TYPE_DECL)
3466 decl = id_expression;
3467 /* Look up the name. */
3470 tree ambiguous_decls;
3472 decl = cp_parser_lookup_name (parser, id_expression,
3475 /*is_namespace=*/false,
3476 /*check_dependency=*/true,
3478 id_expr_token->location);
3479 /* If the lookup was ambiguous, an error will already have
3481 if (ambiguous_decls)
3482 return error_mark_node;
3484 /* In Objective-C++, an instance variable (ivar) may be preferred
3485 to whatever cp_parser_lookup_name() found. */
3486 decl = objc_lookup_ivar (decl, id_expression);
3488 /* If name lookup gives us a SCOPE_REF, then the
3489 qualifying scope was dependent. */
3490 if (TREE_CODE (decl) == SCOPE_REF)
3492 /* At this point, we do not know if DECL is a valid
3493 integral constant expression. We assume that it is
3494 in fact such an expression, so that code like:
3496 template <int N> struct A {
3500 is accepted. At template-instantiation time, we
3501 will check that B<N>::i is actually a constant. */
3504 /* Check to see if DECL is a local variable in a context
3505 where that is forbidden. */
3506 if (parser->local_variables_forbidden_p
3507 && local_variable_p (decl))
3509 /* It might be that we only found DECL because we are
3510 trying to be generous with pre-ISO scoping rules.
3511 For example, consider:
3515 for (int i = 0; i < 10; ++i) {}
3516 extern void f(int j = i);
3519 Here, name look up will originally find the out
3520 of scope `i'. We need to issue a warning message,
3521 but then use the global `i'. */
3522 decl = check_for_out_of_scope_variable (decl);
3523 if (local_variable_p (decl))
3525 error ("%Hlocal variable %qD may not appear in this context",
3526 &id_expr_token->location, decl);
3527 return error_mark_node;
3532 decl = (finish_id_expression
3533 (id_expression, decl, parser->scope,
3535 parser->integral_constant_expression_p,
3536 parser->allow_non_integral_constant_expression_p,
3537 &parser->non_integral_constant_expression_p,
3538 template_p, done, address_p,
3541 id_expr_token->location));
3543 cp_parser_error (parser, error_msg);
3547 /* Anything else is an error. */
3549 /* ...unless we have an Objective-C++ message or string literal,
3551 if (c_dialect_objc ()
3552 && (token->type == CPP_OPEN_SQUARE
3553 || token->type == CPP_OBJC_STRING))
3554 return cp_parser_objc_expression (parser);
3556 cp_parser_error (parser, "expected primary-expression");
3557 return error_mark_node;
3561 /* Parse an id-expression.
3568 :: [opt] nested-name-specifier template [opt] unqualified-id
3570 :: operator-function-id
3573 Return a representation of the unqualified portion of the
3574 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3575 a `::' or nested-name-specifier.
3577 Often, if the id-expression was a qualified-id, the caller will
3578 want to make a SCOPE_REF to represent the qualified-id. This
3579 function does not do this in order to avoid wastefully creating
3580 SCOPE_REFs when they are not required.
3582 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3585 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3586 uninstantiated templates.
3588 If *TEMPLATE_P is non-NULL, it is set to true iff the
3589 `template' keyword is used to explicitly indicate that the entity
3590 named is a template.
3592 If DECLARATOR_P is true, the id-expression is appearing as part of
3593 a declarator, rather than as part of an expression. */
3596 cp_parser_id_expression (cp_parser *parser,
3597 bool template_keyword_p,
3598 bool check_dependency_p,
3603 bool global_scope_p;
3604 bool nested_name_specifier_p;
3606 /* Assume the `template' keyword was not used. */
3608 *template_p = template_keyword_p;
3610 /* Look for the optional `::' operator. */
3612 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3614 /* Look for the optional nested-name-specifier. */
3615 nested_name_specifier_p
3616 = (cp_parser_nested_name_specifier_opt (parser,
3617 /*typename_keyword_p=*/false,
3622 /* If there is a nested-name-specifier, then we are looking at
3623 the first qualified-id production. */
3624 if (nested_name_specifier_p)
3627 tree saved_object_scope;
3628 tree saved_qualifying_scope;
3629 tree unqualified_id;
3632 /* See if the next token is the `template' keyword. */
3634 template_p = &is_template;
3635 *template_p = cp_parser_optional_template_keyword (parser);
3636 /* Name lookup we do during the processing of the
3637 unqualified-id might obliterate SCOPE. */
3638 saved_scope = parser->scope;
3639 saved_object_scope = parser->object_scope;
3640 saved_qualifying_scope = parser->qualifying_scope;
3641 /* Process the final unqualified-id. */
3642 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3645 /*optional_p=*/false);
3646 /* Restore the SAVED_SCOPE for our caller. */
3647 parser->scope = saved_scope;
3648 parser->object_scope = saved_object_scope;
3649 parser->qualifying_scope = saved_qualifying_scope;
3651 return unqualified_id;
3653 /* Otherwise, if we are in global scope, then we are looking at one
3654 of the other qualified-id productions. */
3655 else if (global_scope_p)
3660 /* Peek at the next token. */
3661 token = cp_lexer_peek_token (parser->lexer);
3663 /* If it's an identifier, and the next token is not a "<", then
3664 we can avoid the template-id case. This is an optimization
3665 for this common case. */
3666 if (token->type == CPP_NAME
3667 && !cp_parser_nth_token_starts_template_argument_list_p
3669 return cp_parser_identifier (parser);
3671 cp_parser_parse_tentatively (parser);
3672 /* Try a template-id. */
3673 id = cp_parser_template_id (parser,
3674 /*template_keyword_p=*/false,
3675 /*check_dependency_p=*/true,
3677 /* If that worked, we're done. */
3678 if (cp_parser_parse_definitely (parser))
3681 /* Peek at the next token. (Changes in the token buffer may
3682 have invalidated the pointer obtained above.) */
3683 token = cp_lexer_peek_token (parser->lexer);
3685 switch (token->type)
3688 return cp_parser_identifier (parser);
3691 if (token->keyword == RID_OPERATOR)
3692 return cp_parser_operator_function_id (parser);
3696 cp_parser_error (parser, "expected id-expression");
3697 return error_mark_node;
3701 return cp_parser_unqualified_id (parser, template_keyword_p,
3702 /*check_dependency_p=*/true,
3707 /* Parse an unqualified-id.
3711 operator-function-id
3712 conversion-function-id
3716 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3717 keyword, in a construct like `A::template ...'.
3719 Returns a representation of unqualified-id. For the `identifier'
3720 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3721 production a BIT_NOT_EXPR is returned; the operand of the
3722 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3723 other productions, see the documentation accompanying the
3724 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3725 names are looked up in uninstantiated templates. If DECLARATOR_P
3726 is true, the unqualified-id is appearing as part of a declarator,
3727 rather than as part of an expression. */
3730 cp_parser_unqualified_id (cp_parser* parser,
3731 bool template_keyword_p,
3732 bool check_dependency_p,
3738 /* Peek at the next token. */
3739 token = cp_lexer_peek_token (parser->lexer);
3741 switch (token->type)
3747 /* We don't know yet whether or not this will be a
3749 cp_parser_parse_tentatively (parser);
3750 /* Try a template-id. */
3751 id = cp_parser_template_id (parser, template_keyword_p,
3754 /* If it worked, we're done. */
3755 if (cp_parser_parse_definitely (parser))
3757 /* Otherwise, it's an ordinary identifier. */
3758 return cp_parser_identifier (parser);
3761 case CPP_TEMPLATE_ID:
3762 return cp_parser_template_id (parser, template_keyword_p,
3769 tree qualifying_scope;
3774 /* Consume the `~' token. */
3775 cp_lexer_consume_token (parser->lexer);
3776 /* Parse the class-name. The standard, as written, seems to
3779 template <typename T> struct S { ~S (); };
3780 template <typename T> S<T>::~S() {}
3782 is invalid, since `~' must be followed by a class-name, but
3783 `S<T>' is dependent, and so not known to be a class.
3784 That's not right; we need to look in uninstantiated
3785 templates. A further complication arises from:
3787 template <typename T> void f(T t) {
3791 Here, it is not possible to look up `T' in the scope of `T'
3792 itself. We must look in both the current scope, and the
3793 scope of the containing complete expression.
3795 Yet another issue is:
3804 The standard does not seem to say that the `S' in `~S'
3805 should refer to the type `S' and not the data member
3808 /* DR 244 says that we look up the name after the "~" in the
3809 same scope as we looked up the qualifying name. That idea
3810 isn't fully worked out; it's more complicated than that. */
3811 scope = parser->scope;
3812 object_scope = parser->object_scope;
3813 qualifying_scope = parser->qualifying_scope;
3815 /* Check for invalid scopes. */
3816 if (scope == error_mark_node)
3818 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3819 cp_lexer_consume_token (parser->lexer);
3820 return error_mark_node;
3822 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3824 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3825 error ("%Hscope %qT before %<~%> is not a class-name",
3826 &token->location, scope);
3827 cp_parser_simulate_error (parser);
3828 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3829 cp_lexer_consume_token (parser->lexer);
3830 return error_mark_node;
3832 gcc_assert (!scope || TYPE_P (scope));
3834 /* If the name is of the form "X::~X" it's OK. */
3835 token = cp_lexer_peek_token (parser->lexer);
3837 && token->type == CPP_NAME
3838 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3840 && constructor_name_p (token->u.value, scope))
3842 cp_lexer_consume_token (parser->lexer);
3843 return build_nt (BIT_NOT_EXPR, scope);
3846 /* If there was an explicit qualification (S::~T), first look
3847 in the scope given by the qualification (i.e., S). */
3849 type_decl = NULL_TREE;
3852 cp_parser_parse_tentatively (parser);
3853 type_decl = cp_parser_class_name (parser,
3854 /*typename_keyword_p=*/false,
3855 /*template_keyword_p=*/false,
3857 /*check_dependency=*/false,
3858 /*class_head_p=*/false,
3860 if (cp_parser_parse_definitely (parser))
3863 /* In "N::S::~S", look in "N" as well. */
3864 if (!done && scope && qualifying_scope)
3866 cp_parser_parse_tentatively (parser);
3867 parser->scope = qualifying_scope;
3868 parser->object_scope = NULL_TREE;
3869 parser->qualifying_scope = NULL_TREE;
3871 = cp_parser_class_name (parser,
3872 /*typename_keyword_p=*/false,
3873 /*template_keyword_p=*/false,
3875 /*check_dependency=*/false,
3876 /*class_head_p=*/false,
3878 if (cp_parser_parse_definitely (parser))
3881 /* In "p->S::~T", look in the scope given by "*p" as well. */
3882 else if (!done && object_scope)
3884 cp_parser_parse_tentatively (parser);
3885 parser->scope = object_scope;
3886 parser->object_scope = NULL_TREE;
3887 parser->qualifying_scope = NULL_TREE;
3889 = cp_parser_class_name (parser,
3890 /*typename_keyword_p=*/false,
3891 /*template_keyword_p=*/false,
3893 /*check_dependency=*/false,
3894 /*class_head_p=*/false,
3896 if (cp_parser_parse_definitely (parser))
3899 /* Look in the surrounding context. */
3902 parser->scope = NULL_TREE;
3903 parser->object_scope = NULL_TREE;
3904 parser->qualifying_scope = NULL_TREE;
3905 if (processing_template_decl)
3906 cp_parser_parse_tentatively (parser);
3908 = cp_parser_class_name (parser,
3909 /*typename_keyword_p=*/false,
3910 /*template_keyword_p=*/false,
3912 /*check_dependency=*/false,
3913 /*class_head_p=*/false,
3915 if (processing_template_decl
3916 && ! cp_parser_parse_definitely (parser))
3918 /* We couldn't find a type with this name, so just accept
3919 it and check for a match at instantiation time. */
3920 type_decl = cp_parser_identifier (parser);
3921 if (type_decl != error_mark_node)
3922 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3926 /* If an error occurred, assume that the name of the
3927 destructor is the same as the name of the qualifying
3928 class. That allows us to keep parsing after running
3929 into ill-formed destructor names. */
3930 if (type_decl == error_mark_node && scope)
3931 return build_nt (BIT_NOT_EXPR, scope);
3932 else if (type_decl == error_mark_node)
3933 return error_mark_node;
3935 /* Check that destructor name and scope match. */
3936 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3938 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3939 error ("%Hdeclaration of %<~%T%> as member of %qT",
3940 &token->location, type_decl, scope);
3941 cp_parser_simulate_error (parser);
3942 return error_mark_node;
3947 A typedef-name that names a class shall not be used as the
3948 identifier in the declarator for a destructor declaration. */
3950 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3951 && !DECL_SELF_REFERENCE_P (type_decl)
3952 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3953 error ("%Htypedef-name %qD used as destructor declarator",
3954 &token->location, type_decl);
3956 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3960 if (token->keyword == RID_OPERATOR)
3964 /* This could be a template-id, so we try that first. */
3965 cp_parser_parse_tentatively (parser);
3966 /* Try a template-id. */
3967 id = cp_parser_template_id (parser, template_keyword_p,
3968 /*check_dependency_p=*/true,
3970 /* If that worked, we're done. */
3971 if (cp_parser_parse_definitely (parser))
3973 /* We still don't know whether we're looking at an
3974 operator-function-id or a conversion-function-id. */
3975 cp_parser_parse_tentatively (parser);
3976 /* Try an operator-function-id. */
3977 id = cp_parser_operator_function_id (parser);
3978 /* If that didn't work, try a conversion-function-id. */
3979 if (!cp_parser_parse_definitely (parser))
3980 id = cp_parser_conversion_function_id (parser);
3989 cp_parser_error (parser, "expected unqualified-id");
3990 return error_mark_node;
3994 /* Parse an (optional) nested-name-specifier.
3996 nested-name-specifier: [C++98]
3997 class-or-namespace-name :: nested-name-specifier [opt]
3998 class-or-namespace-name :: template nested-name-specifier [opt]
4000 nested-name-specifier: [C++0x]
4003 nested-name-specifier identifier ::
4004 nested-name-specifier template [opt] simple-template-id ::
4006 PARSER->SCOPE should be set appropriately before this function is
4007 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4008 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4011 Sets PARSER->SCOPE to the class (TYPE) or namespace
4012 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4013 it unchanged if there is no nested-name-specifier. Returns the new
4014 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4016 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4017 part of a declaration and/or decl-specifier. */
4020 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4021 bool typename_keyword_p,
4022 bool check_dependency_p,
4024 bool is_declaration)
4026 bool success = false;
4027 cp_token_position start = 0;
4030 /* Remember where the nested-name-specifier starts. */
4031 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4033 start = cp_lexer_token_position (parser->lexer, false);
4034 push_deferring_access_checks (dk_deferred);
4041 tree saved_qualifying_scope;
4042 bool template_keyword_p;
4044 /* Spot cases that cannot be the beginning of a
4045 nested-name-specifier. */
4046 token = cp_lexer_peek_token (parser->lexer);
4048 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4049 the already parsed nested-name-specifier. */
4050 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4052 /* Grab the nested-name-specifier and continue the loop. */
4053 cp_parser_pre_parsed_nested_name_specifier (parser);
4054 /* If we originally encountered this nested-name-specifier
4055 with IS_DECLARATION set to false, we will not have
4056 resolved TYPENAME_TYPEs, so we must do so here. */
4058 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4060 new_scope = resolve_typename_type (parser->scope,
4061 /*only_current_p=*/false);
4062 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4063 parser->scope = new_scope;
4069 /* Spot cases that cannot be the beginning of a
4070 nested-name-specifier. On the second and subsequent times
4071 through the loop, we look for the `template' keyword. */
4072 if (success && token->keyword == RID_TEMPLATE)
4074 /* A template-id can start a nested-name-specifier. */
4075 else if (token->type == CPP_TEMPLATE_ID)
4079 /* If the next token is not an identifier, then it is
4080 definitely not a type-name or namespace-name. */
4081 if (token->type != CPP_NAME)
4083 /* If the following token is neither a `<' (to begin a
4084 template-id), nor a `::', then we are not looking at a
4085 nested-name-specifier. */
4086 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4087 if (token->type != CPP_SCOPE
4088 && !cp_parser_nth_token_starts_template_argument_list_p
4093 /* The nested-name-specifier is optional, so we parse
4095 cp_parser_parse_tentatively (parser);
4097 /* Look for the optional `template' keyword, if this isn't the
4098 first time through the loop. */
4100 template_keyword_p = cp_parser_optional_template_keyword (parser);
4102 template_keyword_p = false;
4104 /* Save the old scope since the name lookup we are about to do
4105 might destroy it. */
4106 old_scope = parser->scope;
4107 saved_qualifying_scope = parser->qualifying_scope;
4108 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4109 look up names in "X<T>::I" in order to determine that "Y" is
4110 a template. So, if we have a typename at this point, we make
4111 an effort to look through it. */
4113 && !typename_keyword_p
4115 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4116 parser->scope = resolve_typename_type (parser->scope,
4117 /*only_current_p=*/false);
4118 /* Parse the qualifying entity. */
4120 = cp_parser_qualifying_entity (parser,
4126 /* Look for the `::' token. */
4127 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4129 /* If we found what we wanted, we keep going; otherwise, we're
4131 if (!cp_parser_parse_definitely (parser))
4133 bool error_p = false;
4135 /* Restore the OLD_SCOPE since it was valid before the
4136 failed attempt at finding the last
4137 class-or-namespace-name. */
4138 parser->scope = old_scope;
4139 parser->qualifying_scope = saved_qualifying_scope;
4140 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4142 /* If the next token is an identifier, and the one after
4143 that is a `::', then any valid interpretation would have
4144 found a class-or-namespace-name. */
4145 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4146 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4148 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4151 token = cp_lexer_consume_token (parser->lexer);
4154 if (!token->ambiguous_p)
4157 tree ambiguous_decls;
4159 decl = cp_parser_lookup_name (parser, token->u.value,
4161 /*is_template=*/false,
4162 /*is_namespace=*/false,
4163 /*check_dependency=*/true,
4166 if (TREE_CODE (decl) == TEMPLATE_DECL)
4167 error ("%H%qD used without template parameters",
4168 &token->location, decl);
4169 else if (ambiguous_decls)
4171 error ("%Hreference to %qD is ambiguous",
4172 &token->location, token->u.value);
4173 print_candidates (ambiguous_decls);
4174 decl = error_mark_node;
4178 const char* msg = "is not a class or namespace";
4179 if (cxx_dialect != cxx98)
4180 msg = "is not a class, namespace, or enumeration";
4181 cp_parser_name_lookup_error
4182 (parser, token->u.value, decl, msg,
4186 parser->scope = error_mark_node;
4188 /* Treat this as a successful nested-name-specifier
4193 If the name found is not a class-name (clause
4194 _class_) or namespace-name (_namespace.def_), the
4195 program is ill-formed. */
4198 cp_lexer_consume_token (parser->lexer);
4202 /* We've found one valid nested-name-specifier. */
4204 /* Name lookup always gives us a DECL. */
4205 if (TREE_CODE (new_scope) == TYPE_DECL)
4206 new_scope = TREE_TYPE (new_scope);
4207 /* Uses of "template" must be followed by actual templates. */
4208 if (template_keyword_p
4209 && !(CLASS_TYPE_P (new_scope)
4210 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4211 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4212 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4213 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4214 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4215 == TEMPLATE_ID_EXPR)))
4216 permerror (input_location, TYPE_P (new_scope)
4217 ? "%qT is not a template"
4218 : "%qD is not a template",
4220 /* If it is a class scope, try to complete it; we are about to
4221 be looking up names inside the class. */
4222 if (TYPE_P (new_scope)
4223 /* Since checking types for dependency can be expensive,
4224 avoid doing it if the type is already complete. */
4225 && !COMPLETE_TYPE_P (new_scope)
4226 /* Do not try to complete dependent types. */
4227 && !dependent_type_p (new_scope))
4229 new_scope = complete_type (new_scope);
4230 /* If it is a typedef to current class, use the current
4231 class instead, as the typedef won't have any names inside
4233 if (!COMPLETE_TYPE_P (new_scope)
4234 && currently_open_class (new_scope))
4235 new_scope = TYPE_MAIN_VARIANT (new_scope);
4237 /* Make sure we look in the right scope the next time through
4239 parser->scope = new_scope;
4242 /* If parsing tentatively, replace the sequence of tokens that makes
4243 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4244 token. That way, should we re-parse the token stream, we will
4245 not have to repeat the effort required to do the parse, nor will
4246 we issue duplicate error messages. */
4247 if (success && start)
4251 token = cp_lexer_token_at (parser->lexer, start);
4252 /* Reset the contents of the START token. */
4253 token->type = CPP_NESTED_NAME_SPECIFIER;
4254 /* Retrieve any deferred checks. Do not pop this access checks yet
4255 so the memory will not be reclaimed during token replacing below. */
4256 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4257 token->u.tree_check_value->value = parser->scope;
4258 token->u.tree_check_value->checks = get_deferred_access_checks ();
4259 token->u.tree_check_value->qualifying_scope =
4260 parser->qualifying_scope;
4261 token->keyword = RID_MAX;
4263 /* Purge all subsequent tokens. */
4264 cp_lexer_purge_tokens_after (parser->lexer, start);
4268 pop_to_parent_deferring_access_checks ();
4270 return success ? parser->scope : NULL_TREE;
4273 /* Parse a nested-name-specifier. See
4274 cp_parser_nested_name_specifier_opt for details. This function
4275 behaves identically, except that it will an issue an error if no
4276 nested-name-specifier is present. */
4279 cp_parser_nested_name_specifier (cp_parser *parser,
4280 bool typename_keyword_p,
4281 bool check_dependency_p,
4283 bool is_declaration)
4287 /* Look for the nested-name-specifier. */
4288 scope = cp_parser_nested_name_specifier_opt (parser,
4293 /* If it was not present, issue an error message. */
4296 cp_parser_error (parser, "expected nested-name-specifier");
4297 parser->scope = NULL_TREE;
4303 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4304 this is either a class-name or a namespace-name (which corresponds
4305 to the class-or-namespace-name production in the grammar). For
4306 C++0x, it can also be a type-name that refers to an enumeration
4309 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4310 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4311 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4312 TYPE_P is TRUE iff the next name should be taken as a class-name,
4313 even the same name is declared to be another entity in the same
4316 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4317 specified by the class-or-namespace-name. If neither is found the
4318 ERROR_MARK_NODE is returned. */
4321 cp_parser_qualifying_entity (cp_parser *parser,
4322 bool typename_keyword_p,
4323 bool template_keyword_p,
4324 bool check_dependency_p,
4326 bool is_declaration)
4329 tree saved_qualifying_scope;
4330 tree saved_object_scope;
4333 bool successful_parse_p;
4335 /* Before we try to parse the class-name, we must save away the
4336 current PARSER->SCOPE since cp_parser_class_name will destroy
4338 saved_scope = parser->scope;
4339 saved_qualifying_scope = parser->qualifying_scope;
4340 saved_object_scope = parser->object_scope;
4341 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4342 there is no need to look for a namespace-name. */
4343 only_class_p = template_keyword_p
4344 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4346 cp_parser_parse_tentatively (parser);
4347 scope = cp_parser_class_name (parser,
4350 type_p ? class_type : none_type,
4352 /*class_head_p=*/false,
4354 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4355 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4357 && cxx_dialect != cxx98
4358 && !successful_parse_p)
4360 /* Restore the saved scope. */
4361 parser->scope = saved_scope;
4362 parser->qualifying_scope = saved_qualifying_scope;
4363 parser->object_scope = saved_object_scope;
4365 /* Parse tentatively. */
4366 cp_parser_parse_tentatively (parser);
4368 /* Parse a typedef-name or enum-name. */
4369 scope = cp_parser_nonclass_name (parser);
4370 successful_parse_p = cp_parser_parse_definitely (parser);
4372 /* If that didn't work, try for a namespace-name. */
4373 if (!only_class_p && !successful_parse_p)
4375 /* Restore the saved scope. */
4376 parser->scope = saved_scope;
4377 parser->qualifying_scope = saved_qualifying_scope;
4378 parser->object_scope = saved_object_scope;
4379 /* If we are not looking at an identifier followed by the scope
4380 resolution operator, then this is not part of a
4381 nested-name-specifier. (Note that this function is only used
4382 to parse the components of a nested-name-specifier.) */
4383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4384 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4385 return error_mark_node;
4386 scope = cp_parser_namespace_name (parser);
4392 /* Parse a postfix-expression.
4396 postfix-expression [ expression ]
4397 postfix-expression ( expression-list [opt] )
4398 simple-type-specifier ( expression-list [opt] )
4399 typename :: [opt] nested-name-specifier identifier
4400 ( expression-list [opt] )
4401 typename :: [opt] nested-name-specifier template [opt] template-id
4402 ( expression-list [opt] )
4403 postfix-expression . template [opt] id-expression
4404 postfix-expression -> template [opt] id-expression
4405 postfix-expression . pseudo-destructor-name
4406 postfix-expression -> pseudo-destructor-name
4407 postfix-expression ++
4408 postfix-expression --
4409 dynamic_cast < type-id > ( expression )
4410 static_cast < type-id > ( expression )
4411 reinterpret_cast < type-id > ( expression )
4412 const_cast < type-id > ( expression )
4413 typeid ( expression )
4419 ( type-id ) { initializer-list , [opt] }
4421 This extension is a GNU version of the C99 compound-literal
4422 construct. (The C99 grammar uses `type-name' instead of `type-id',
4423 but they are essentially the same concept.)
4425 If ADDRESS_P is true, the postfix expression is the operand of the
4426 `&' operator. CAST_P is true if this expression is the target of a
4429 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4430 class member access expressions [expr.ref].
4432 Returns a representation of the expression. */
4435 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4436 bool member_access_only_p,
4437 cp_id_kind * pidk_return)
4441 cp_id_kind idk = CP_ID_KIND_NONE;
4442 tree postfix_expression = NULL_TREE;
4443 bool is_member_access = false;
4445 /* Peek at the next token. */
4446 token = cp_lexer_peek_token (parser->lexer);
4447 /* Some of the productions are determined by keywords. */
4448 keyword = token->keyword;
4458 const char *saved_message;
4460 /* All of these can be handled in the same way from the point
4461 of view of parsing. Begin by consuming the token
4462 identifying the cast. */
4463 cp_lexer_consume_token (parser->lexer);
4465 /* New types cannot be defined in the cast. */
4466 saved_message = parser->type_definition_forbidden_message;
4467 parser->type_definition_forbidden_message
4468 = "types may not be defined in casts";
4470 /* Look for the opening `<'. */
4471 cp_parser_require (parser, CPP_LESS, "%<<%>");
4472 /* Parse the type to which we are casting. */
4473 type = cp_parser_type_id (parser);
4474 /* Look for the closing `>'. */
4475 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4476 /* Restore the old message. */
4477 parser->type_definition_forbidden_message = saved_message;
4479 /* And the expression which is being cast. */
4480 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4481 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4482 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4484 /* Only type conversions to integral or enumeration types
4485 can be used in constant-expressions. */
4486 if (!cast_valid_in_integral_constant_expression_p (type)
4487 && (cp_parser_non_integral_constant_expression
4489 "a cast to a type other than an integral or "
4490 "enumeration type")))
4491 return error_mark_node;
4497 = build_dynamic_cast (type, expression, tf_warning_or_error);
4501 = build_static_cast (type, expression, tf_warning_or_error);
4505 = build_reinterpret_cast (type, expression,
4506 tf_warning_or_error);
4510 = build_const_cast (type, expression, tf_warning_or_error);
4521 const char *saved_message;
4522 bool saved_in_type_id_in_expr_p;
4524 /* Consume the `typeid' token. */
4525 cp_lexer_consume_token (parser->lexer);
4526 /* Look for the `(' token. */
4527 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4528 /* Types cannot be defined in a `typeid' expression. */
4529 saved_message = parser->type_definition_forbidden_message;
4530 parser->type_definition_forbidden_message
4531 = "types may not be defined in a %<typeid%> expression";
4532 /* We can't be sure yet whether we're looking at a type-id or an
4534 cp_parser_parse_tentatively (parser);
4535 /* Try a type-id first. */
4536 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4537 parser->in_type_id_in_expr_p = true;
4538 type = cp_parser_type_id (parser);
4539 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4540 /* Look for the `)' token. Otherwise, we can't be sure that
4541 we're not looking at an expression: consider `typeid (int
4542 (3))', for example. */
4543 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4544 /* If all went well, simply lookup the type-id. */
4545 if (cp_parser_parse_definitely (parser))
4546 postfix_expression = get_typeid (type);
4547 /* Otherwise, fall back to the expression variant. */
4552 /* Look for an expression. */
4553 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4554 /* Compute its typeid. */
4555 postfix_expression = build_typeid (expression);
4556 /* Look for the `)' token. */
4557 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4559 /* Restore the saved message. */
4560 parser->type_definition_forbidden_message = saved_message;
4561 /* `typeid' may not appear in an integral constant expression. */
4562 if (cp_parser_non_integral_constant_expression(parser,
4563 "%<typeid%> operator"))
4564 return error_mark_node;
4571 /* The syntax permitted here is the same permitted for an
4572 elaborated-type-specifier. */
4573 type = cp_parser_elaborated_type_specifier (parser,
4574 /*is_friend=*/false,
4575 /*is_declaration=*/false);
4576 postfix_expression = cp_parser_functional_cast (parser, type);
4584 /* If the next thing is a simple-type-specifier, we may be
4585 looking at a functional cast. We could also be looking at
4586 an id-expression. So, we try the functional cast, and if
4587 that doesn't work we fall back to the primary-expression. */
4588 cp_parser_parse_tentatively (parser);
4589 /* Look for the simple-type-specifier. */
4590 type = cp_parser_simple_type_specifier (parser,
4591 /*decl_specs=*/NULL,
4592 CP_PARSER_FLAGS_NONE);
4593 /* Parse the cast itself. */
4594 if (!cp_parser_error_occurred (parser))
4596 = cp_parser_functional_cast (parser, type);
4597 /* If that worked, we're done. */
4598 if (cp_parser_parse_definitely (parser))
4601 /* If the functional-cast didn't work out, try a
4602 compound-literal. */
4603 if (cp_parser_allow_gnu_extensions_p (parser)
4604 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4606 VEC(constructor_elt,gc) *initializer_list = NULL;
4607 bool saved_in_type_id_in_expr_p;
4609 cp_parser_parse_tentatively (parser);
4610 /* Consume the `('. */
4611 cp_lexer_consume_token (parser->lexer);
4612 /* Parse the type. */
4613 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4614 parser->in_type_id_in_expr_p = true;
4615 type = cp_parser_type_id (parser);
4616 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4617 /* Look for the `)'. */
4618 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4619 /* Look for the `{'. */
4620 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4621 /* If things aren't going well, there's no need to
4623 if (!cp_parser_error_occurred (parser))
4625 bool non_constant_p;
4626 /* Parse the initializer-list. */
4628 = cp_parser_initializer_list (parser, &non_constant_p);
4629 /* Allow a trailing `,'. */
4630 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4631 cp_lexer_consume_token (parser->lexer);
4632 /* Look for the final `}'. */
4633 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4635 /* If that worked, we're definitely looking at a
4636 compound-literal expression. */
4637 if (cp_parser_parse_definitely (parser))
4639 /* Warn the user that a compound literal is not
4640 allowed in standard C++. */
4641 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4642 /* For simplicity, we disallow compound literals in
4643 constant-expressions. We could
4644 allow compound literals of integer type, whose
4645 initializer was a constant, in constant
4646 expressions. Permitting that usage, as a further
4647 extension, would not change the meaning of any
4648 currently accepted programs. (Of course, as
4649 compound literals are not part of ISO C++, the
4650 standard has nothing to say.) */
4651 if (cp_parser_non_integral_constant_expression
4652 (parser, "non-constant compound literals"))
4654 postfix_expression = error_mark_node;
4657 /* Form the representation of the compound-literal. */
4659 = (finish_compound_literal
4660 (type, build_constructor (init_list_type_node,
4661 initializer_list)));
4666 /* It must be a primary-expression. */
4668 = cp_parser_primary_expression (parser, address_p, cast_p,
4669 /*template_arg_p=*/false,
4675 /* Keep looping until the postfix-expression is complete. */
4678 if (idk == CP_ID_KIND_UNQUALIFIED
4679 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4680 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4681 /* It is not a Koenig lookup function call. */
4683 = unqualified_name_lookup_error (postfix_expression);
4685 /* Peek at the next token. */
4686 token = cp_lexer_peek_token (parser->lexer);
4688 switch (token->type)
4690 case CPP_OPEN_SQUARE:
4692 = cp_parser_postfix_open_square_expression (parser,
4695 idk = CP_ID_KIND_NONE;
4696 is_member_access = false;
4699 case CPP_OPEN_PAREN:
4700 /* postfix-expression ( expression-list [opt] ) */
4703 bool is_builtin_constant_p;
4704 bool saved_integral_constant_expression_p = false;
4705 bool saved_non_integral_constant_expression_p = false;
4708 is_member_access = false;
4710 is_builtin_constant_p
4711 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4712 if (is_builtin_constant_p)
4714 /* The whole point of __builtin_constant_p is to allow
4715 non-constant expressions to appear as arguments. */
4716 saved_integral_constant_expression_p
4717 = parser->integral_constant_expression_p;
4718 saved_non_integral_constant_expression_p
4719 = parser->non_integral_constant_expression_p;
4720 parser->integral_constant_expression_p = false;
4722 args = (cp_parser_parenthesized_expression_list
4723 (parser, /*is_attribute_list=*/false,
4724 /*cast_p=*/false, /*allow_expansion_p=*/true,
4725 /*non_constant_p=*/NULL));
4726 if (is_builtin_constant_p)
4728 parser->integral_constant_expression_p
4729 = saved_integral_constant_expression_p;
4730 parser->non_integral_constant_expression_p
4731 = saved_non_integral_constant_expression_p;
4734 if (args == error_mark_node)
4736 postfix_expression = error_mark_node;
4740 /* Function calls are not permitted in
4741 constant-expressions. */
4742 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4743 && cp_parser_non_integral_constant_expression (parser,
4746 postfix_expression = error_mark_node;
4751 if (idk == CP_ID_KIND_UNQUALIFIED
4752 || idk == CP_ID_KIND_TEMPLATE_ID)
4754 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4759 if (!any_type_dependent_arguments_p (args))
4761 = perform_koenig_lookup (postfix_expression, args);
4765 = unqualified_fn_lookup_error (postfix_expression);
4767 /* We do not perform argument-dependent lookup if
4768 normal lookup finds a non-function, in accordance
4769 with the expected resolution of DR 218. */
4770 else if (args && is_overloaded_fn (postfix_expression))
4772 tree fn = get_first_fn (postfix_expression);
4774 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4775 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4777 /* Only do argument dependent lookup if regular
4778 lookup does not find a set of member functions.
4779 [basic.lookup.koenig]/2a */
4780 if (!DECL_FUNCTION_MEMBER_P (fn))
4783 if (!any_type_dependent_arguments_p (args))
4785 = perform_koenig_lookup (postfix_expression, args);
4790 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4792 tree instance = TREE_OPERAND (postfix_expression, 0);
4793 tree fn = TREE_OPERAND (postfix_expression, 1);
4795 if (processing_template_decl
4796 && (type_dependent_expression_p (instance)
4797 || (!BASELINK_P (fn)
4798 && TREE_CODE (fn) != FIELD_DECL)
4799 || type_dependent_expression_p (fn)
4800 || any_type_dependent_arguments_p (args)))
4803 = build_nt_call_list (postfix_expression, args);
4807 if (BASELINK_P (fn))
4810 = (build_new_method_call
4811 (instance, fn, args, NULL_TREE,
4812 (idk == CP_ID_KIND_QUALIFIED
4813 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4815 tf_warning_or_error));
4819 = finish_call_expr (postfix_expression, args,
4820 /*disallow_virtual=*/false,
4822 tf_warning_or_error);
4824 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4825 || TREE_CODE (postfix_expression) == MEMBER_REF
4826 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4827 postfix_expression = (build_offset_ref_call_from_tree
4828 (postfix_expression, args));
4829 else if (idk == CP_ID_KIND_QUALIFIED)
4830 /* A call to a static class member, or a namespace-scope
4833 = finish_call_expr (postfix_expression, args,
4834 /*disallow_virtual=*/true,
4836 tf_warning_or_error);
4838 /* All other function calls. */
4840 = finish_call_expr (postfix_expression, args,
4841 /*disallow_virtual=*/false,
4843 tf_warning_or_error);
4845 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4846 idk = CP_ID_KIND_NONE;
4852 /* postfix-expression . template [opt] id-expression
4853 postfix-expression . pseudo-destructor-name
4854 postfix-expression -> template [opt] id-expression
4855 postfix-expression -> pseudo-destructor-name */
4857 /* Consume the `.' or `->' operator. */
4858 cp_lexer_consume_token (parser->lexer);
4861 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4866 is_member_access = true;
4870 /* postfix-expression ++ */
4871 /* Consume the `++' token. */
4872 cp_lexer_consume_token (parser->lexer);
4873 /* Generate a representation for the complete expression. */
4875 = finish_increment_expr (postfix_expression,
4876 POSTINCREMENT_EXPR);
4877 /* Increments may not appear in constant-expressions. */
4878 if (cp_parser_non_integral_constant_expression (parser,
4880 postfix_expression = error_mark_node;
4881 idk = CP_ID_KIND_NONE;
4882 is_member_access = false;
4885 case CPP_MINUS_MINUS:
4886 /* postfix-expression -- */
4887 /* Consume the `--' token. */
4888 cp_lexer_consume_token (parser->lexer);
4889 /* Generate a representation for the complete expression. */
4891 = finish_increment_expr (postfix_expression,
4892 POSTDECREMENT_EXPR);
4893 /* Decrements may not appear in constant-expressions. */
4894 if (cp_parser_non_integral_constant_expression (parser,
4896 postfix_expression = error_mark_node;
4897 idk = CP_ID_KIND_NONE;
4898 is_member_access = false;
4902 if (pidk_return != NULL)
4903 * pidk_return = idk;
4904 if (member_access_only_p)
4905 return is_member_access? postfix_expression : error_mark_node;
4907 return postfix_expression;
4911 /* We should never get here. */
4913 return error_mark_node;
4916 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4917 by cp_parser_builtin_offsetof. We're looking for
4919 postfix-expression [ expression ]
4921 FOR_OFFSETOF is set if we're being called in that context, which
4922 changes how we deal with integer constant expressions. */
4925 cp_parser_postfix_open_square_expression (cp_parser *parser,
4926 tree postfix_expression,
4931 /* Consume the `[' token. */
4932 cp_lexer_consume_token (parser->lexer);
4934 /* Parse the index expression. */
4935 /* ??? For offsetof, there is a question of what to allow here. If
4936 offsetof is not being used in an integral constant expression context,
4937 then we *could* get the right answer by computing the value at runtime.
4938 If we are in an integral constant expression context, then we might
4939 could accept any constant expression; hard to say without analysis.
4940 Rather than open the barn door too wide right away, allow only integer
4941 constant expressions here. */
4943 index = cp_parser_constant_expression (parser, false, NULL);
4945 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4947 /* Look for the closing `]'. */
4948 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4950 /* Build the ARRAY_REF. */
4951 postfix_expression = grok_array_decl (postfix_expression, index);
4953 /* When not doing offsetof, array references are not permitted in
4954 constant-expressions. */
4956 && (cp_parser_non_integral_constant_expression
4957 (parser, "an array reference")))
4958 postfix_expression = error_mark_node;
4960 return postfix_expression;
4963 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4964 by cp_parser_builtin_offsetof. We're looking for
4966 postfix-expression . template [opt] id-expression
4967 postfix-expression . pseudo-destructor-name
4968 postfix-expression -> template [opt] id-expression
4969 postfix-expression -> pseudo-destructor-name
4971 FOR_OFFSETOF is set if we're being called in that context. That sorta
4972 limits what of the above we'll actually accept, but nevermind.
4973 TOKEN_TYPE is the "." or "->" token, which will already have been
4974 removed from the stream. */
4977 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4978 enum cpp_ttype token_type,
4979 tree postfix_expression,
4980 bool for_offsetof, cp_id_kind *idk,
4981 location_t location)
4985 bool pseudo_destructor_p;
4986 tree scope = NULL_TREE;
4988 /* If this is a `->' operator, dereference the pointer. */
4989 if (token_type == CPP_DEREF)
4990 postfix_expression = build_x_arrow (postfix_expression);
4991 /* Check to see whether or not the expression is type-dependent. */
4992 dependent_p = type_dependent_expression_p (postfix_expression);
4993 /* The identifier following the `->' or `.' is not qualified. */
4994 parser->scope = NULL_TREE;
4995 parser->qualifying_scope = NULL_TREE;
4996 parser->object_scope = NULL_TREE;
4997 *idk = CP_ID_KIND_NONE;
4999 /* Enter the scope corresponding to the type of the object
5000 given by the POSTFIX_EXPRESSION. */
5001 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5003 scope = TREE_TYPE (postfix_expression);
5004 /* According to the standard, no expression should ever have
5005 reference type. Unfortunately, we do not currently match
5006 the standard in this respect in that our internal representation
5007 of an expression may have reference type even when the standard
5008 says it does not. Therefore, we have to manually obtain the
5009 underlying type here. */
5010 scope = non_reference (scope);
5011 /* The type of the POSTFIX_EXPRESSION must be complete. */
5012 if (scope == unknown_type_node)
5014 error ("%H%qE does not have class type", &location, postfix_expression);
5018 scope = complete_type_or_else (scope, NULL_TREE);
5019 /* Let the name lookup machinery know that we are processing a
5020 class member access expression. */
5021 parser->context->object_type = scope;
5022 /* If something went wrong, we want to be able to discern that case,
5023 as opposed to the case where there was no SCOPE due to the type
5024 of expression being dependent. */
5026 scope = error_mark_node;
5027 /* If the SCOPE was erroneous, make the various semantic analysis
5028 functions exit quickly -- and without issuing additional error
5030 if (scope == error_mark_node)
5031 postfix_expression = error_mark_node;
5034 /* Assume this expression is not a pseudo-destructor access. */
5035 pseudo_destructor_p = false;
5037 /* If the SCOPE is a scalar type, then, if this is a valid program,
5038 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5039 is type dependent, it can be pseudo-destructor-name or something else.
5040 Try to parse it as pseudo-destructor-name first. */
5041 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5046 cp_parser_parse_tentatively (parser);
5047 /* Parse the pseudo-destructor-name. */
5049 cp_parser_pseudo_destructor_name (parser, &s, &type);
5051 && (cp_parser_error_occurred (parser)
5052 || TREE_CODE (type) != TYPE_DECL
5053 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5054 cp_parser_abort_tentative_parse (parser);
5055 else if (cp_parser_parse_definitely (parser))
5057 pseudo_destructor_p = true;
5059 = finish_pseudo_destructor_expr (postfix_expression,
5060 s, TREE_TYPE (type));
5064 if (!pseudo_destructor_p)
5066 /* If the SCOPE is not a scalar type, we are looking at an
5067 ordinary class member access expression, rather than a
5068 pseudo-destructor-name. */
5070 cp_token *token = cp_lexer_peek_token (parser->lexer);
5071 /* Parse the id-expression. */
5072 name = (cp_parser_id_expression
5074 cp_parser_optional_template_keyword (parser),
5075 /*check_dependency_p=*/true,
5077 /*declarator_p=*/false,
5078 /*optional_p=*/false));
5079 /* In general, build a SCOPE_REF if the member name is qualified.
5080 However, if the name was not dependent and has already been
5081 resolved; there is no need to build the SCOPE_REF. For example;
5083 struct X { void f(); };
5084 template <typename T> void f(T* t) { t->X::f(); }
5086 Even though "t" is dependent, "X::f" is not and has been resolved
5087 to a BASELINK; there is no need to include scope information. */
5089 /* But we do need to remember that there was an explicit scope for
5090 virtual function calls. */
5092 *idk = CP_ID_KIND_QUALIFIED;
5094 /* If the name is a template-id that names a type, we will get a
5095 TYPE_DECL here. That is invalid code. */
5096 if (TREE_CODE (name) == TYPE_DECL)
5098 error ("%Hinvalid use of %qD", &token->location, name);
5099 postfix_expression = error_mark_node;
5103 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5105 name = build_qualified_name (/*type=*/NULL_TREE,
5109 parser->scope = NULL_TREE;
5110 parser->qualifying_scope = NULL_TREE;
5111 parser->object_scope = NULL_TREE;
5113 if (scope && name && BASELINK_P (name))
5114 adjust_result_of_qualified_name_lookup
5115 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5117 = finish_class_member_access_expr (postfix_expression, name,
5119 tf_warning_or_error);
5123 /* We no longer need to look up names in the scope of the object on
5124 the left-hand side of the `.' or `->' operator. */
5125 parser->context->object_type = NULL_TREE;
5127 /* Outside of offsetof, these operators may not appear in
5128 constant-expressions. */
5130 && (cp_parser_non_integral_constant_expression
5131 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5132 postfix_expression = error_mark_node;
5134 return postfix_expression;
5137 /* Parse a parenthesized expression-list.
5140 assignment-expression
5141 expression-list, assignment-expression
5146 identifier, expression-list
5148 CAST_P is true if this expression is the target of a cast.
5150 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5153 Returns a TREE_LIST. The TREE_VALUE of each node is a
5154 representation of an assignment-expression. Note that a TREE_LIST
5155 is returned even if there is only a single expression in the list.
5156 error_mark_node is returned if the ( and or ) are
5157 missing. NULL_TREE is returned on no expressions. The parentheses
5158 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5159 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5160 indicates whether or not all of the expressions in the list were
5164 cp_parser_parenthesized_expression_list (cp_parser* parser,
5165 bool is_attribute_list,
5167 bool allow_expansion_p,
5168 bool *non_constant_p)
5170 tree expression_list = NULL_TREE;
5171 bool fold_expr_p = is_attribute_list;
5172 tree identifier = NULL_TREE;
5173 bool saved_greater_than_is_operator_p;
5175 /* Assume all the expressions will be constant. */
5177 *non_constant_p = false;
5179 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5180 return error_mark_node;
5182 /* Within a parenthesized expression, a `>' token is always
5183 the greater-than operator. */
5184 saved_greater_than_is_operator_p
5185 = parser->greater_than_is_operator_p;
5186 parser->greater_than_is_operator_p = true;
5188 /* Consume expressions until there are no more. */
5189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5194 /* At the beginning of attribute lists, check to see if the
5195 next token is an identifier. */
5196 if (is_attribute_list
5197 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5201 /* Consume the identifier. */
5202 token = cp_lexer_consume_token (parser->lexer);
5203 /* Save the identifier. */
5204 identifier = token->u.value;
5208 bool expr_non_constant_p;
5210 /* Parse the next assignment-expression. */
5211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5213 /* A braced-init-list. */
5214 maybe_warn_cpp0x ("extended initializer lists");
5215 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5216 if (non_constant_p && expr_non_constant_p)
5217 *non_constant_p = true;
5219 else if (non_constant_p)
5221 expr = (cp_parser_constant_expression
5222 (parser, /*allow_non_constant_p=*/true,
5223 &expr_non_constant_p));
5224 if (expr_non_constant_p)
5225 *non_constant_p = true;
5228 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5231 expr = fold_non_dependent_expr (expr);
5233 /* If we have an ellipsis, then this is an expression
5235 if (allow_expansion_p
5236 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5238 /* Consume the `...'. */
5239 cp_lexer_consume_token (parser->lexer);
5241 /* Build the argument pack. */
5242 expr = make_pack_expansion (expr);
5245 /* Add it to the list. We add error_mark_node
5246 expressions to the list, so that we can still tell if
5247 the correct form for a parenthesized expression-list
5248 is found. That gives better errors. */
5249 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5251 if (expr == error_mark_node)
5255 /* After the first item, attribute lists look the same as
5256 expression lists. */
5257 is_attribute_list = false;
5260 /* If the next token isn't a `,', then we are done. */
5261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5264 /* Otherwise, consume the `,' and keep going. */
5265 cp_lexer_consume_token (parser->lexer);
5268 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5273 /* We try and resync to an unnested comma, as that will give the
5274 user better diagnostics. */
5275 ending = cp_parser_skip_to_closing_parenthesis (parser,
5276 /*recovering=*/true,
5278 /*consume_paren=*/true);
5283 parser->greater_than_is_operator_p
5284 = saved_greater_than_is_operator_p;
5285 return error_mark_node;
5289 parser->greater_than_is_operator_p
5290 = saved_greater_than_is_operator_p;
5292 /* We built up the list in reverse order so we must reverse it now. */
5293 expression_list = nreverse (expression_list);
5295 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5297 return expression_list;
5300 /* Parse a pseudo-destructor-name.
5302 pseudo-destructor-name:
5303 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5304 :: [opt] nested-name-specifier template template-id :: ~ type-name
5305 :: [opt] nested-name-specifier [opt] ~ type-name
5307 If either of the first two productions is used, sets *SCOPE to the
5308 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5309 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5310 or ERROR_MARK_NODE if the parse fails. */
5313 cp_parser_pseudo_destructor_name (cp_parser* parser,
5317 bool nested_name_specifier_p;
5319 /* Assume that things will not work out. */
5320 *type = error_mark_node;
5322 /* Look for the optional `::' operator. */
5323 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5324 /* Look for the optional nested-name-specifier. */
5325 nested_name_specifier_p
5326 = (cp_parser_nested_name_specifier_opt (parser,
5327 /*typename_keyword_p=*/false,
5328 /*check_dependency_p=*/true,
5330 /*is_declaration=*/false)
5332 /* Now, if we saw a nested-name-specifier, we might be doing the
5333 second production. */
5334 if (nested_name_specifier_p
5335 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5337 /* Consume the `template' keyword. */
5338 cp_lexer_consume_token (parser->lexer);
5339 /* Parse the template-id. */
5340 cp_parser_template_id (parser,
5341 /*template_keyword_p=*/true,
5342 /*check_dependency_p=*/false,
5343 /*is_declaration=*/true);
5344 /* Look for the `::' token. */
5345 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5347 /* If the next token is not a `~', then there might be some
5348 additional qualification. */
5349 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5351 /* At this point, we're looking for "type-name :: ~". The type-name
5352 must not be a class-name, since this is a pseudo-destructor. So,
5353 it must be either an enum-name, or a typedef-name -- both of which
5354 are just identifiers. So, we peek ahead to check that the "::"
5355 and "~" tokens are present; if they are not, then we can avoid
5356 calling type_name. */
5357 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5358 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5359 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5361 cp_parser_error (parser, "non-scalar type");
5365 /* Look for the type-name. */
5366 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5367 if (*scope == error_mark_node)
5370 /* Look for the `::' token. */
5371 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5376 /* Look for the `~'. */
5377 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5378 /* Look for the type-name again. We are not responsible for
5379 checking that it matches the first type-name. */
5380 *type = cp_parser_nonclass_name (parser);
5383 /* Parse a unary-expression.
5389 unary-operator cast-expression
5390 sizeof unary-expression
5398 __extension__ cast-expression
5399 __alignof__ unary-expression
5400 __alignof__ ( type-id )
5401 __real__ cast-expression
5402 __imag__ cast-expression
5405 ADDRESS_P is true iff the unary-expression is appearing as the
5406 operand of the `&' operator. CAST_P is true if this expression is
5407 the target of a cast.
5409 Returns a representation of the expression. */
5412 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5416 enum tree_code unary_operator;
5418 /* Peek at the next token. */
5419 token = cp_lexer_peek_token (parser->lexer);
5420 /* Some keywords give away the kind of expression. */
5421 if (token->type == CPP_KEYWORD)
5423 enum rid keyword = token->keyword;
5433 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5434 /* Consume the token. */
5435 cp_lexer_consume_token (parser->lexer);
5436 /* Parse the operand. */
5437 operand = cp_parser_sizeof_operand (parser, keyword);
5439 if (TYPE_P (operand))
5440 return cxx_sizeof_or_alignof_type (operand, op, true);
5442 return cxx_sizeof_or_alignof_expr (operand, op, true);
5446 return cp_parser_new_expression (parser);
5449 return cp_parser_delete_expression (parser);
5453 /* The saved value of the PEDANTIC flag. */
5457 /* Save away the PEDANTIC flag. */
5458 cp_parser_extension_opt (parser, &saved_pedantic);
5459 /* Parse the cast-expression. */
5460 expr = cp_parser_simple_cast_expression (parser);
5461 /* Restore the PEDANTIC flag. */
5462 pedantic = saved_pedantic;
5472 /* Consume the `__real__' or `__imag__' token. */
5473 cp_lexer_consume_token (parser->lexer);
5474 /* Parse the cast-expression. */
5475 expression = cp_parser_simple_cast_expression (parser);
5476 /* Create the complete representation. */
5477 return build_x_unary_op ((keyword == RID_REALPART
5478 ? REALPART_EXPR : IMAGPART_EXPR),
5480 tf_warning_or_error);
5489 /* Look for the `:: new' and `:: delete', which also signal the
5490 beginning of a new-expression, or delete-expression,
5491 respectively. If the next token is `::', then it might be one of
5493 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5497 /* See if the token after the `::' is one of the keywords in
5498 which we're interested. */
5499 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5500 /* If it's `new', we have a new-expression. */
5501 if (keyword == RID_NEW)
5502 return cp_parser_new_expression (parser);
5503 /* Similarly, for `delete'. */
5504 else if (keyword == RID_DELETE)
5505 return cp_parser_delete_expression (parser);
5508 /* Look for a unary operator. */
5509 unary_operator = cp_parser_unary_operator (token);
5510 /* The `++' and `--' operators can be handled similarly, even though
5511 they are not technically unary-operators in the grammar. */
5512 if (unary_operator == ERROR_MARK)
5514 if (token->type == CPP_PLUS_PLUS)
5515 unary_operator = PREINCREMENT_EXPR;
5516 else if (token->type == CPP_MINUS_MINUS)
5517 unary_operator = PREDECREMENT_EXPR;
5518 /* Handle the GNU address-of-label extension. */
5519 else if (cp_parser_allow_gnu_extensions_p (parser)
5520 && token->type == CPP_AND_AND)
5524 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5526 /* Consume the '&&' token. */
5527 cp_lexer_consume_token (parser->lexer);
5528 /* Look for the identifier. */
5529 identifier = cp_parser_identifier (parser);
5530 /* Create an expression representing the address. */
5531 expression = finish_label_address_expr (identifier, loc);
5532 if (cp_parser_non_integral_constant_expression (parser,
5533 "the address of a label"))
5534 expression = error_mark_node;
5538 if (unary_operator != ERROR_MARK)
5540 tree cast_expression;
5541 tree expression = error_mark_node;
5542 const char *non_constant_p = NULL;
5544 /* Consume the operator token. */
5545 token = cp_lexer_consume_token (parser->lexer);
5546 /* Parse the cast-expression. */
5548 = cp_parser_cast_expression (parser,
5549 unary_operator == ADDR_EXPR,
5550 /*cast_p=*/false, pidk);
5551 /* Now, build an appropriate representation. */
5552 switch (unary_operator)
5555 non_constant_p = "%<*%>";
5556 expression = build_x_indirect_ref (cast_expression, "unary *",
5557 tf_warning_or_error);
5561 non_constant_p = "%<&%>";
5564 expression = build_x_unary_op (unary_operator, cast_expression,
5565 tf_warning_or_error);
5568 case PREINCREMENT_EXPR:
5569 case PREDECREMENT_EXPR:
5570 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5571 ? "%<++%>" : "%<--%>");
5573 case UNARY_PLUS_EXPR:
5575 case TRUTH_NOT_EXPR:
5576 expression = finish_unary_op_expr (unary_operator, cast_expression);
5584 && cp_parser_non_integral_constant_expression (parser,
5586 expression = error_mark_node;
5591 return cp_parser_postfix_expression (parser, address_p, cast_p,
5592 /*member_access_only_p=*/false,
5596 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5597 unary-operator, the corresponding tree code is returned. */
5599 static enum tree_code
5600 cp_parser_unary_operator (cp_token* token)
5602 switch (token->type)
5605 return INDIRECT_REF;
5611 return UNARY_PLUS_EXPR;
5617 return TRUTH_NOT_EXPR;
5620 return BIT_NOT_EXPR;
5627 /* Parse a new-expression.
5630 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5631 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5633 Returns a representation of the expression. */
5636 cp_parser_new_expression (cp_parser* parser)
5638 bool global_scope_p;
5644 /* Look for the optional `::' operator. */
5646 = (cp_parser_global_scope_opt (parser,
5647 /*current_scope_valid_p=*/false)
5649 /* Look for the `new' operator. */
5650 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5651 /* There's no easy way to tell a new-placement from the
5652 `( type-id )' construct. */
5653 cp_parser_parse_tentatively (parser);
5654 /* Look for a new-placement. */
5655 placement = cp_parser_new_placement (parser);
5656 /* If that didn't work out, there's no new-placement. */
5657 if (!cp_parser_parse_definitely (parser))
5658 placement = NULL_TREE;
5660 /* If the next token is a `(', then we have a parenthesized
5662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5665 /* Consume the `('. */
5666 cp_lexer_consume_token (parser->lexer);
5667 /* Parse the type-id. */
5668 type = cp_parser_type_id (parser);
5669 /* Look for the closing `)'. */
5670 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5671 token = cp_lexer_peek_token (parser->lexer);
5672 /* There should not be a direct-new-declarator in this production,
5673 but GCC used to allowed this, so we check and emit a sensible error
5674 message for this case. */
5675 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5677 error ("%Harray bound forbidden after parenthesized type-id",
5679 inform (token->location,
5680 "try removing the parentheses around the type-id");
5681 cp_parser_direct_new_declarator (parser);
5685 /* Otherwise, there must be a new-type-id. */
5687 type = cp_parser_new_type_id (parser, &nelts);
5689 /* If the next token is a `(' or '{', then we have a new-initializer. */
5690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5691 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5692 initializer = cp_parser_new_initializer (parser);
5694 initializer = NULL_TREE;
5696 /* A new-expression may not appear in an integral constant
5698 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5699 return error_mark_node;
5701 /* Create a representation of the new-expression. */
5702 return build_new (placement, type, nelts, initializer, global_scope_p,
5703 tf_warning_or_error);
5706 /* Parse a new-placement.
5711 Returns the same representation as for an expression-list. */
5714 cp_parser_new_placement (cp_parser* parser)
5716 tree expression_list;
5718 /* Parse the expression-list. */
5719 expression_list = (cp_parser_parenthesized_expression_list
5720 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5721 /*non_constant_p=*/NULL));
5723 return expression_list;
5726 /* Parse a new-type-id.
5729 type-specifier-seq new-declarator [opt]
5731 Returns the TYPE allocated. If the new-type-id indicates an array
5732 type, *NELTS is set to the number of elements in the last array
5733 bound; the TYPE will not include the last array bound. */
5736 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5738 cp_decl_specifier_seq type_specifier_seq;
5739 cp_declarator *new_declarator;
5740 cp_declarator *declarator;
5741 cp_declarator *outer_declarator;
5742 const char *saved_message;
5745 /* The type-specifier sequence must not contain type definitions.
5746 (It cannot contain declarations of new types either, but if they
5747 are not definitions we will catch that because they are not
5749 saved_message = parser->type_definition_forbidden_message;
5750 parser->type_definition_forbidden_message
5751 = "types may not be defined in a new-type-id";
5752 /* Parse the type-specifier-seq. */
5753 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5754 /*is_trailing_return=*/false,
5755 &type_specifier_seq);
5756 /* Restore the old message. */
5757 parser->type_definition_forbidden_message = saved_message;
5758 /* Parse the new-declarator. */
5759 new_declarator = cp_parser_new_declarator_opt (parser);
5761 /* Determine the number of elements in the last array dimension, if
5764 /* Skip down to the last array dimension. */
5765 declarator = new_declarator;
5766 outer_declarator = NULL;
5767 while (declarator && (declarator->kind == cdk_pointer
5768 || declarator->kind == cdk_ptrmem))
5770 outer_declarator = declarator;
5771 declarator = declarator->declarator;
5774 && declarator->kind == cdk_array
5775 && declarator->declarator
5776 && declarator->declarator->kind == cdk_array)
5778 outer_declarator = declarator;
5779 declarator = declarator->declarator;
5782 if (declarator && declarator->kind == cdk_array)
5784 *nelts = declarator->u.array.bounds;
5785 if (*nelts == error_mark_node)
5786 *nelts = integer_one_node;
5788 if (outer_declarator)
5789 outer_declarator->declarator = declarator->declarator;
5791 new_declarator = NULL;
5794 type = groktypename (&type_specifier_seq, new_declarator, false);
5798 /* Parse an (optional) new-declarator.
5801 ptr-operator new-declarator [opt]
5802 direct-new-declarator
5804 Returns the declarator. */
5806 static cp_declarator *
5807 cp_parser_new_declarator_opt (cp_parser* parser)
5809 enum tree_code code;
5811 cp_cv_quals cv_quals;
5813 /* We don't know if there's a ptr-operator next, or not. */
5814 cp_parser_parse_tentatively (parser);
5815 /* Look for a ptr-operator. */
5816 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5817 /* If that worked, look for more new-declarators. */
5818 if (cp_parser_parse_definitely (parser))
5820 cp_declarator *declarator;
5822 /* Parse another optional declarator. */
5823 declarator = cp_parser_new_declarator_opt (parser);
5825 return cp_parser_make_indirect_declarator
5826 (code, type, cv_quals, declarator);
5829 /* If the next token is a `[', there is a direct-new-declarator. */
5830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5831 return cp_parser_direct_new_declarator (parser);
5836 /* Parse a direct-new-declarator.
5838 direct-new-declarator:
5840 direct-new-declarator [constant-expression]
5844 static cp_declarator *
5845 cp_parser_direct_new_declarator (cp_parser* parser)
5847 cp_declarator *declarator = NULL;
5853 /* Look for the opening `['. */
5854 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5855 /* The first expression is not required to be constant. */
5858 cp_token *token = cp_lexer_peek_token (parser->lexer);
5859 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5860 /* The standard requires that the expression have integral
5861 type. DR 74 adds enumeration types. We believe that the
5862 real intent is that these expressions be handled like the
5863 expression in a `switch' condition, which also allows
5864 classes with a single conversion to integral or
5865 enumeration type. */
5866 if (!processing_template_decl)
5869 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5874 error ("%Hexpression in new-declarator must have integral "
5875 "or enumeration type", &token->location);
5876 expression = error_mark_node;
5880 /* But all the other expressions must be. */
5883 = cp_parser_constant_expression (parser,
5884 /*allow_non_constant=*/false,
5886 /* Look for the closing `]'. */
5887 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5889 /* Add this bound to the declarator. */
5890 declarator = make_array_declarator (declarator, expression);
5892 /* If the next token is not a `[', then there are no more
5894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5901 /* Parse a new-initializer.
5904 ( expression-list [opt] )
5907 Returns a representation of the expression-list. If there is no
5908 expression-list, VOID_ZERO_NODE is returned. */
5911 cp_parser_new_initializer (cp_parser* parser)
5913 tree expression_list;
5915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5917 bool expr_non_constant_p;
5918 maybe_warn_cpp0x ("extended initializer lists");
5919 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5920 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5921 expression_list = build_tree_list (NULL_TREE, expression_list);
5924 expression_list = (cp_parser_parenthesized_expression_list
5925 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5926 /*non_constant_p=*/NULL));
5927 if (!expression_list)
5928 expression_list = void_zero_node;
5930 return expression_list;
5933 /* Parse a delete-expression.
5936 :: [opt] delete cast-expression
5937 :: [opt] delete [ ] cast-expression
5939 Returns a representation of the expression. */
5942 cp_parser_delete_expression (cp_parser* parser)
5944 bool global_scope_p;
5948 /* Look for the optional `::' operator. */
5950 = (cp_parser_global_scope_opt (parser,
5951 /*current_scope_valid_p=*/false)
5953 /* Look for the `delete' keyword. */
5954 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5955 /* See if the array syntax is in use. */
5956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5958 /* Consume the `[' token. */
5959 cp_lexer_consume_token (parser->lexer);
5960 /* Look for the `]' token. */
5961 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5962 /* Remember that this is the `[]' construct. */
5968 /* Parse the cast-expression. */
5969 expression = cp_parser_simple_cast_expression (parser);
5971 /* A delete-expression may not appear in an integral constant
5973 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5974 return error_mark_node;
5976 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5979 /* Returns true if TOKEN may start a cast-expression and false
5983 cp_parser_token_starts_cast_expression (cp_token *token)
5985 switch (token->type)
5991 case CPP_CLOSE_SQUARE:
5992 case CPP_CLOSE_PAREN:
5993 case CPP_CLOSE_BRACE:
5997 case CPP_DEREF_STAR:
6005 case CPP_GREATER_EQ:
6025 /* '[' may start a primary-expression in obj-c++. */
6026 case CPP_OPEN_SQUARE:
6027 return c_dialect_objc ();
6034 /* Parse a cast-expression.
6038 ( type-id ) cast-expression
6040 ADDRESS_P is true iff the unary-expression is appearing as the
6041 operand of the `&' operator. CAST_P is true if this expression is
6042 the target of a cast.
6044 Returns a representation of the expression. */
6047 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6050 /* If it's a `(', then we might be looking at a cast. */
6051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6053 tree type = NULL_TREE;
6054 tree expr = NULL_TREE;
6055 bool compound_literal_p;
6056 const char *saved_message;
6058 /* There's no way to know yet whether or not this is a cast.
6059 For example, `(int (3))' is a unary-expression, while `(int)
6060 3' is a cast. So, we resort to parsing tentatively. */
6061 cp_parser_parse_tentatively (parser);
6062 /* Types may not be defined in a cast. */
6063 saved_message = parser->type_definition_forbidden_message;
6064 parser->type_definition_forbidden_message
6065 = "types may not be defined in casts";
6066 /* Consume the `('. */
6067 cp_lexer_consume_token (parser->lexer);
6068 /* A very tricky bit is that `(struct S) { 3 }' is a
6069 compound-literal (which we permit in C++ as an extension).
6070 But, that construct is not a cast-expression -- it is a
6071 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6072 is legal; if the compound-literal were a cast-expression,
6073 you'd need an extra set of parentheses.) But, if we parse
6074 the type-id, and it happens to be a class-specifier, then we
6075 will commit to the parse at that point, because we cannot
6076 undo the action that is done when creating a new class. So,
6077 then we cannot back up and do a postfix-expression.
6079 Therefore, we scan ahead to the closing `)', and check to see
6080 if the token after the `)' is a `{'. If so, we are not
6081 looking at a cast-expression.
6083 Save tokens so that we can put them back. */
6084 cp_lexer_save_tokens (parser->lexer);
6085 /* Skip tokens until the next token is a closing parenthesis.
6086 If we find the closing `)', and the next token is a `{', then
6087 we are looking at a compound-literal. */
6089 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6090 /*consume_paren=*/true)
6091 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6092 /* Roll back the tokens we skipped. */
6093 cp_lexer_rollback_tokens (parser->lexer);
6094 /* If we were looking at a compound-literal, simulate an error
6095 so that the call to cp_parser_parse_definitely below will
6097 if (compound_literal_p)
6098 cp_parser_simulate_error (parser);
6101 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6102 parser->in_type_id_in_expr_p = true;
6103 /* Look for the type-id. */
6104 type = cp_parser_type_id (parser);
6105 /* Look for the closing `)'. */
6106 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6107 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6110 /* Restore the saved message. */
6111 parser->type_definition_forbidden_message = saved_message;
6113 /* At this point this can only be either a cast or a
6114 parenthesized ctor such as `(T ())' that looks like a cast to
6115 function returning T. */
6116 if (!cp_parser_error_occurred (parser)
6117 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6120 cp_parser_parse_definitely (parser);
6121 expr = cp_parser_cast_expression (parser,
6122 /*address_p=*/false,
6123 /*cast_p=*/true, pidk);
6125 /* Warn about old-style casts, if so requested. */
6126 if (warn_old_style_cast
6127 && !in_system_header
6128 && !VOID_TYPE_P (type)
6129 && current_lang_name != lang_name_c)
6130 warning (OPT_Wold_style_cast, "use of old-style cast");
6132 /* Only type conversions to integral or enumeration types
6133 can be used in constant-expressions. */
6134 if (!cast_valid_in_integral_constant_expression_p (type)
6135 && (cp_parser_non_integral_constant_expression
6137 "a cast to a type other than an integral or "
6138 "enumeration type")))
6139 return error_mark_node;
6141 /* Perform the cast. */
6142 expr = build_c_cast (type, expr);
6146 cp_parser_abort_tentative_parse (parser);
6149 /* If we get here, then it's not a cast, so it must be a
6150 unary-expression. */
6151 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6154 /* Parse a binary expression of the general form:
6158 pm-expression .* cast-expression
6159 pm-expression ->* cast-expression
6161 multiplicative-expression:
6163 multiplicative-expression * pm-expression
6164 multiplicative-expression / pm-expression
6165 multiplicative-expression % pm-expression
6167 additive-expression:
6168 multiplicative-expression
6169 additive-expression + multiplicative-expression
6170 additive-expression - multiplicative-expression
6174 shift-expression << additive-expression
6175 shift-expression >> additive-expression
6177 relational-expression:
6179 relational-expression < shift-expression
6180 relational-expression > shift-expression
6181 relational-expression <= shift-expression
6182 relational-expression >= shift-expression
6186 relational-expression:
6187 relational-expression <? shift-expression
6188 relational-expression >? shift-expression
6190 equality-expression:
6191 relational-expression
6192 equality-expression == relational-expression
6193 equality-expression != relational-expression
6197 and-expression & equality-expression
6199 exclusive-or-expression:
6201 exclusive-or-expression ^ and-expression
6203 inclusive-or-expression:
6204 exclusive-or-expression
6205 inclusive-or-expression | exclusive-or-expression
6207 logical-and-expression:
6208 inclusive-or-expression
6209 logical-and-expression && inclusive-or-expression
6211 logical-or-expression:
6212 logical-and-expression
6213 logical-or-expression || logical-and-expression
6215 All these are implemented with a single function like:
6218 simple-cast-expression
6219 binary-expression <token> binary-expression
6221 CAST_P is true if this expression is the target of a cast.
6223 The binops_by_token map is used to get the tree codes for each <token> type.
6224 binary-expressions are associated according to a precedence table. */
6226 #define TOKEN_PRECEDENCE(token) \
6227 (((token->type == CPP_GREATER \
6228 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6229 && !parser->greater_than_is_operator_p) \
6230 ? PREC_NOT_OPERATOR \
6231 : binops_by_token[token->type].prec)
6234 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6235 bool no_toplevel_fold_p,
6236 enum cp_parser_prec prec,
6239 cp_parser_expression_stack stack;
6240 cp_parser_expression_stack_entry *sp = &stack[0];
6243 enum tree_code tree_type, lhs_type, rhs_type;
6244 enum cp_parser_prec new_prec, lookahead_prec;
6247 /* Parse the first expression. */
6248 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6249 lhs_type = ERROR_MARK;
6253 /* Get an operator token. */
6254 token = cp_lexer_peek_token (parser->lexer);
6256 if (warn_cxx0x_compat
6257 && token->type == CPP_RSHIFT
6258 && !parser->greater_than_is_operator_p)
6260 warning (OPT_Wc__0x_compat,
6261 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6263 warning (OPT_Wc__0x_compat,
6264 "suggest parentheses around %<>>%> expression");
6267 new_prec = TOKEN_PRECEDENCE (token);
6269 /* Popping an entry off the stack means we completed a subexpression:
6270 - either we found a token which is not an operator (`>' where it is not
6271 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6272 will happen repeatedly;
6273 - or, we found an operator which has lower priority. This is the case
6274 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6276 if (new_prec <= prec)
6285 tree_type = binops_by_token[token->type].tree_type;
6287 /* We used the operator token. */
6288 cp_lexer_consume_token (parser->lexer);
6290 /* Extract another operand. It may be the RHS of this expression
6291 or the LHS of a new, higher priority expression. */
6292 rhs = cp_parser_simple_cast_expression (parser);
6293 rhs_type = ERROR_MARK;
6295 /* Get another operator token. Look up its precedence to avoid
6296 building a useless (immediately popped) stack entry for common
6297 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6298 token = cp_lexer_peek_token (parser->lexer);
6299 lookahead_prec = TOKEN_PRECEDENCE (token);
6300 if (lookahead_prec > new_prec)
6302 /* ... and prepare to parse the RHS of the new, higher priority
6303 expression. Since precedence levels on the stack are
6304 monotonically increasing, we do not have to care about
6307 sp->tree_type = tree_type;
6309 sp->lhs_type = lhs_type;
6312 lhs_type = rhs_type;
6314 new_prec = lookahead_prec;
6318 lookahead_prec = new_prec;
6319 /* If the stack is not empty, we have parsed into LHS the right side
6320 (`4' in the example above) of an expression we had suspended.
6321 We can use the information on the stack to recover the LHS (`3')
6322 from the stack together with the tree code (`MULT_EXPR'), and
6323 the precedence of the higher level subexpression
6324 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6325 which will be used to actually build the additive expression. */
6328 tree_type = sp->tree_type;
6330 rhs_type = lhs_type;
6332 lhs_type = sp->lhs_type;
6335 overloaded_p = false;
6336 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6337 ERROR_MARK for everything that is not a binary expression.
6338 This makes warn_about_parentheses miss some warnings that
6339 involve unary operators. For unary expressions we should
6340 pass the correct tree_code unless the unary expression was
6341 surrounded by parentheses.
6343 if (no_toplevel_fold_p
6344 && lookahead_prec <= prec
6346 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6347 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6349 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6350 &overloaded_p, tf_warning_or_error);
6351 lhs_type = tree_type;
6353 /* If the binary operator required the use of an overloaded operator,
6354 then this expression cannot be an integral constant-expression.
6355 An overloaded operator can be used even if both operands are
6356 otherwise permissible in an integral constant-expression if at
6357 least one of the operands is of enumeration type. */
6360 && (cp_parser_non_integral_constant_expression
6361 (parser, "calls to overloaded operators")))
6362 return error_mark_node;
6369 /* Parse the `? expression : assignment-expression' part of a
6370 conditional-expression. The LOGICAL_OR_EXPR is the
6371 logical-or-expression that started the conditional-expression.
6372 Returns a representation of the entire conditional-expression.
6374 This routine is used by cp_parser_assignment_expression.
6376 ? expression : assignment-expression
6380 ? : assignment-expression */
6383 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6386 tree assignment_expr;
6388 /* Consume the `?' token. */
6389 cp_lexer_consume_token (parser->lexer);
6390 if (cp_parser_allow_gnu_extensions_p (parser)
6391 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6392 /* Implicit true clause. */
6395 /* Parse the expression. */
6396 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6398 /* The next token should be a `:'. */
6399 cp_parser_require (parser, CPP_COLON, "%<:%>");
6400 /* Parse the assignment-expression. */
6401 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6403 /* Build the conditional-expression. */
6404 return build_x_conditional_expr (logical_or_expr,
6407 tf_warning_or_error);
6410 /* Parse an assignment-expression.
6412 assignment-expression:
6413 conditional-expression
6414 logical-or-expression assignment-operator assignment_expression
6417 CAST_P is true if this expression is the target of a cast.
6419 Returns a representation for the expression. */
6422 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6427 /* If the next token is the `throw' keyword, then we're looking at
6428 a throw-expression. */
6429 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6430 expr = cp_parser_throw_expression (parser);
6431 /* Otherwise, it must be that we are looking at a
6432 logical-or-expression. */
6435 /* Parse the binary expressions (logical-or-expression). */
6436 expr = cp_parser_binary_expression (parser, cast_p, false,
6437 PREC_NOT_OPERATOR, pidk);
6438 /* If the next token is a `?' then we're actually looking at a
6439 conditional-expression. */
6440 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6441 return cp_parser_question_colon_clause (parser, expr);
6444 enum tree_code assignment_operator;
6446 /* If it's an assignment-operator, we're using the second
6449 = cp_parser_assignment_operator_opt (parser);
6450 if (assignment_operator != ERROR_MARK)
6452 bool non_constant_p;
6454 /* Parse the right-hand side of the assignment. */
6455 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6457 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6458 maybe_warn_cpp0x ("extended initializer lists");
6460 /* An assignment may not appear in a
6461 constant-expression. */
6462 if (cp_parser_non_integral_constant_expression (parser,
6464 return error_mark_node;
6465 /* Build the assignment expression. */
6466 expr = build_x_modify_expr (expr,
6467 assignment_operator,
6469 tf_warning_or_error);
6477 /* Parse an (optional) assignment-operator.
6479 assignment-operator: one of
6480 = *= /= %= += -= >>= <<= &= ^= |=
6484 assignment-operator: one of
6487 If the next token is an assignment operator, the corresponding tree
6488 code is returned, and the token is consumed. For example, for
6489 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6490 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6491 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6492 operator, ERROR_MARK is returned. */
6494 static enum tree_code
6495 cp_parser_assignment_operator_opt (cp_parser* parser)
6500 /* Peek at the next token. */
6501 token = cp_lexer_peek_token (parser->lexer);
6503 switch (token->type)
6514 op = TRUNC_DIV_EXPR;
6518 op = TRUNC_MOD_EXPR;
6550 /* Nothing else is an assignment operator. */
6554 /* If it was an assignment operator, consume it. */
6555 if (op != ERROR_MARK)
6556 cp_lexer_consume_token (parser->lexer);
6561 /* Parse an expression.
6564 assignment-expression
6565 expression , assignment-expression
6567 CAST_P is true if this expression is the target of a cast.
6569 Returns a representation of the expression. */
6572 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6574 tree expression = NULL_TREE;
6578 tree assignment_expression;
6580 /* Parse the next assignment-expression. */
6581 assignment_expression
6582 = cp_parser_assignment_expression (parser, cast_p, pidk);
6583 /* If this is the first assignment-expression, we can just
6586 expression = assignment_expression;
6588 expression = build_x_compound_expr (expression,
6589 assignment_expression,
6590 tf_warning_or_error);
6591 /* If the next token is not a comma, then we are done with the
6593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6595 /* Consume the `,'. */
6596 cp_lexer_consume_token (parser->lexer);
6597 /* A comma operator cannot appear in a constant-expression. */
6598 if (cp_parser_non_integral_constant_expression (parser,
6599 "a comma operator"))
6600 expression = error_mark_node;
6606 /* Parse a constant-expression.
6608 constant-expression:
6609 conditional-expression
6611 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6612 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6613 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6614 is false, NON_CONSTANT_P should be NULL. */
6617 cp_parser_constant_expression (cp_parser* parser,
6618 bool allow_non_constant_p,
6619 bool *non_constant_p)
6621 bool saved_integral_constant_expression_p;
6622 bool saved_allow_non_integral_constant_expression_p;
6623 bool saved_non_integral_constant_expression_p;
6626 /* It might seem that we could simply parse the
6627 conditional-expression, and then check to see if it were
6628 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6629 one that the compiler can figure out is constant, possibly after
6630 doing some simplifications or optimizations. The standard has a
6631 precise definition of constant-expression, and we must honor
6632 that, even though it is somewhat more restrictive.
6638 is not a legal declaration, because `(2, 3)' is not a
6639 constant-expression. The `,' operator is forbidden in a
6640 constant-expression. However, GCC's constant-folding machinery
6641 will fold this operation to an INTEGER_CST for `3'. */
6643 /* Save the old settings. */
6644 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6645 saved_allow_non_integral_constant_expression_p
6646 = parser->allow_non_integral_constant_expression_p;
6647 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6648 /* We are now parsing a constant-expression. */
6649 parser->integral_constant_expression_p = true;
6650 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6651 parser->non_integral_constant_expression_p = false;
6652 /* Although the grammar says "conditional-expression", we parse an
6653 "assignment-expression", which also permits "throw-expression"
6654 and the use of assignment operators. In the case that
6655 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6656 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6657 actually essential that we look for an assignment-expression.
6658 For example, cp_parser_initializer_clauses uses this function to
6659 determine whether a particular assignment-expression is in fact
6661 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6662 /* Restore the old settings. */
6663 parser->integral_constant_expression_p
6664 = saved_integral_constant_expression_p;
6665 parser->allow_non_integral_constant_expression_p
6666 = saved_allow_non_integral_constant_expression_p;
6667 if (allow_non_constant_p)
6668 *non_constant_p = parser->non_integral_constant_expression_p;
6669 else if (parser->non_integral_constant_expression_p)
6670 expression = error_mark_node;
6671 parser->non_integral_constant_expression_p
6672 = saved_non_integral_constant_expression_p;
6677 /* Parse __builtin_offsetof.
6679 offsetof-expression:
6680 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6682 offsetof-member-designator:
6684 | offsetof-member-designator "." id-expression
6685 | offsetof-member-designator "[" expression "]"
6686 | offsetof-member-designator "->" id-expression */
6689 cp_parser_builtin_offsetof (cp_parser *parser)
6691 int save_ice_p, save_non_ice_p;
6696 /* We're about to accept non-integral-constant things, but will
6697 definitely yield an integral constant expression. Save and
6698 restore these values around our local parsing. */
6699 save_ice_p = parser->integral_constant_expression_p;
6700 save_non_ice_p = parser->non_integral_constant_expression_p;
6702 /* Consume the "__builtin_offsetof" token. */
6703 cp_lexer_consume_token (parser->lexer);
6704 /* Consume the opening `('. */
6705 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6706 /* Parse the type-id. */
6707 type = cp_parser_type_id (parser);
6708 /* Look for the `,'. */
6709 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6710 token = cp_lexer_peek_token (parser->lexer);
6712 /* Build the (type *)null that begins the traditional offsetof macro. */
6713 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6714 tf_warning_or_error);
6716 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6717 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6718 true, &dummy, token->location);
6721 token = cp_lexer_peek_token (parser->lexer);
6722 switch (token->type)
6724 case CPP_OPEN_SQUARE:
6725 /* offsetof-member-designator "[" expression "]" */
6726 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6730 /* offsetof-member-designator "->" identifier */
6731 expr = grok_array_decl (expr, integer_zero_node);
6735 /* offsetof-member-designator "." identifier */
6736 cp_lexer_consume_token (parser->lexer);
6737 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6742 case CPP_CLOSE_PAREN:
6743 /* Consume the ")" token. */
6744 cp_lexer_consume_token (parser->lexer);
6748 /* Error. We know the following require will fail, but
6749 that gives the proper error message. */
6750 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6751 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6752 expr = error_mark_node;
6758 /* If we're processing a template, we can't finish the semantics yet.
6759 Otherwise we can fold the entire expression now. */
6760 if (processing_template_decl)
6761 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6763 expr = finish_offsetof (expr);
6766 parser->integral_constant_expression_p = save_ice_p;
6767 parser->non_integral_constant_expression_p = save_non_ice_p;
6772 /* Parse a trait expression. */
6775 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6778 tree type1, type2 = NULL_TREE;
6779 bool binary = false;
6780 cp_decl_specifier_seq decl_specs;
6784 case RID_HAS_NOTHROW_ASSIGN:
6785 kind = CPTK_HAS_NOTHROW_ASSIGN;
6787 case RID_HAS_NOTHROW_CONSTRUCTOR:
6788 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6790 case RID_HAS_NOTHROW_COPY:
6791 kind = CPTK_HAS_NOTHROW_COPY;
6793 case RID_HAS_TRIVIAL_ASSIGN:
6794 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6796 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6797 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6799 case RID_HAS_TRIVIAL_COPY:
6800 kind = CPTK_HAS_TRIVIAL_COPY;
6802 case RID_HAS_TRIVIAL_DESTRUCTOR:
6803 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6805 case RID_HAS_VIRTUAL_DESTRUCTOR:
6806 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6808 case RID_IS_ABSTRACT:
6809 kind = CPTK_IS_ABSTRACT;
6811 case RID_IS_BASE_OF:
6812 kind = CPTK_IS_BASE_OF;
6816 kind = CPTK_IS_CLASS;
6818 case RID_IS_CONVERTIBLE_TO:
6819 kind = CPTK_IS_CONVERTIBLE_TO;
6823 kind = CPTK_IS_EMPTY;
6826 kind = CPTK_IS_ENUM;
6831 case RID_IS_POLYMORPHIC:
6832 kind = CPTK_IS_POLYMORPHIC;
6835 kind = CPTK_IS_UNION;
6841 /* Consume the token. */
6842 cp_lexer_consume_token (parser->lexer);
6844 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6846 type1 = cp_parser_type_id (parser);
6848 if (type1 == error_mark_node)
6849 return error_mark_node;
6851 /* Build a trivial decl-specifier-seq. */
6852 clear_decl_specs (&decl_specs);
6853 decl_specs.type = type1;
6855 /* Call grokdeclarator to figure out what type this is. */
6856 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6857 /*initialized=*/0, /*attrlist=*/NULL);
6861 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6863 type2 = cp_parser_type_id (parser);
6865 if (type2 == error_mark_node)
6866 return error_mark_node;
6868 /* Build a trivial decl-specifier-seq. */
6869 clear_decl_specs (&decl_specs);
6870 decl_specs.type = type2;
6872 /* Call grokdeclarator to figure out what type this is. */
6873 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6874 /*initialized=*/0, /*attrlist=*/NULL);
6877 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6879 /* Complete the trait expression, which may mean either processing
6880 the trait expr now or saving it for template instantiation. */
6881 return finish_trait_expr (kind, type1, type2);
6884 /* Statements [gram.stmt.stmt] */
6886 /* Parse a statement.
6890 expression-statement
6895 declaration-statement
6898 IN_COMPOUND is true when the statement is nested inside a
6899 cp_parser_compound_statement; this matters for certain pragmas.
6901 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6902 is a (possibly labeled) if statement which is not enclosed in braces
6903 and has an else clause. This is used to implement -Wparentheses. */
6906 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6907 bool in_compound, bool *if_p)
6911 location_t statement_location;
6916 /* There is no statement yet. */
6917 statement = NULL_TREE;
6918 /* Peek at the next token. */
6919 token = cp_lexer_peek_token (parser->lexer);
6920 /* Remember the location of the first token in the statement. */
6921 statement_location = token->location;
6922 /* If this is a keyword, then that will often determine what kind of
6923 statement we have. */
6924 if (token->type == CPP_KEYWORD)
6926 enum rid keyword = token->keyword;
6932 /* Looks like a labeled-statement with a case label.
6933 Parse the label, and then use tail recursion to parse
6935 cp_parser_label_for_labeled_statement (parser);
6940 statement = cp_parser_selection_statement (parser, if_p);
6946 statement = cp_parser_iteration_statement (parser);
6953 statement = cp_parser_jump_statement (parser);
6956 /* Objective-C++ exception-handling constructs. */
6959 case RID_AT_FINALLY:
6960 case RID_AT_SYNCHRONIZED:
6962 statement = cp_parser_objc_statement (parser);
6966 statement = cp_parser_try_block (parser);
6970 /* This must be a namespace alias definition. */
6971 cp_parser_declaration_statement (parser);
6975 /* It might be a keyword like `int' that can start a
6976 declaration-statement. */
6980 else if (token->type == CPP_NAME)
6982 /* If the next token is a `:', then we are looking at a
6983 labeled-statement. */
6984 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6985 if (token->type == CPP_COLON)
6987 /* Looks like a labeled-statement with an ordinary label.
6988 Parse the label, and then use tail recursion to parse
6990 cp_parser_label_for_labeled_statement (parser);
6994 /* Anything that starts with a `{' must be a compound-statement. */
6995 else if (token->type == CPP_OPEN_BRACE)
6996 statement = cp_parser_compound_statement (parser, NULL, false);
6997 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6998 a statement all its own. */
6999 else if (token->type == CPP_PRAGMA)
7001 /* Only certain OpenMP pragmas are attached to statements, and thus
7002 are considered statements themselves. All others are not. In
7003 the context of a compound, accept the pragma as a "statement" and
7004 return so that we can check for a close brace. Otherwise we
7005 require a real statement and must go back and read one. */
7007 cp_parser_pragma (parser, pragma_compound);
7008 else if (!cp_parser_pragma (parser, pragma_stmt))
7012 else if (token->type == CPP_EOF)
7014 cp_parser_error (parser, "expected statement");
7018 /* Everything else must be a declaration-statement or an
7019 expression-statement. Try for the declaration-statement
7020 first, unless we are looking at a `;', in which case we know that
7021 we have an expression-statement. */
7024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7026 cp_parser_parse_tentatively (parser);
7027 /* Try to parse the declaration-statement. */
7028 cp_parser_declaration_statement (parser);
7029 /* If that worked, we're done. */
7030 if (cp_parser_parse_definitely (parser))
7033 /* Look for an expression-statement instead. */
7034 statement = cp_parser_expression_statement (parser, in_statement_expr);
7037 /* Set the line number for the statement. */
7038 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7039 SET_EXPR_LOCATION (statement, statement_location);
7042 /* Parse the label for a labeled-statement, i.e.
7045 case constant-expression :
7049 case constant-expression ... constant-expression : statement
7051 When a label is parsed without errors, the label is added to the
7052 parse tree by the finish_* functions, so this function doesn't
7053 have to return the label. */
7056 cp_parser_label_for_labeled_statement (cp_parser* parser)
7060 /* The next token should be an identifier. */
7061 token = cp_lexer_peek_token (parser->lexer);
7062 if (token->type != CPP_NAME
7063 && token->type != CPP_KEYWORD)
7065 cp_parser_error (parser, "expected labeled-statement");
7069 switch (token->keyword)
7076 /* Consume the `case' token. */
7077 cp_lexer_consume_token (parser->lexer);
7078 /* Parse the constant-expression. */
7079 expr = cp_parser_constant_expression (parser,
7080 /*allow_non_constant_p=*/false,
7083 ellipsis = cp_lexer_peek_token (parser->lexer);
7084 if (ellipsis->type == CPP_ELLIPSIS)
7086 /* Consume the `...' token. */
7087 cp_lexer_consume_token (parser->lexer);
7089 cp_parser_constant_expression (parser,
7090 /*allow_non_constant_p=*/false,
7092 /* We don't need to emit warnings here, as the common code
7093 will do this for us. */
7096 expr_hi = NULL_TREE;
7098 if (parser->in_switch_statement_p)
7099 finish_case_label (expr, expr_hi);
7101 error ("%Hcase label %qE not within a switch statement",
7102 &token->location, expr);
7107 /* Consume the `default' token. */
7108 cp_lexer_consume_token (parser->lexer);
7110 if (parser->in_switch_statement_p)
7111 finish_case_label (NULL_TREE, NULL_TREE);
7113 error ("%Hcase label not within a switch statement", &token->location);
7117 /* Anything else must be an ordinary label. */
7118 finish_label_stmt (cp_parser_identifier (parser));
7122 /* Require the `:' token. */
7123 cp_parser_require (parser, CPP_COLON, "%<:%>");
7126 /* Parse an expression-statement.
7128 expression-statement:
7131 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7132 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7133 indicates whether this expression-statement is part of an
7134 expression statement. */
7137 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7139 tree statement = NULL_TREE;
7141 /* If the next token is a ';', then there is no expression
7143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7144 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7146 /* Consume the final `;'. */
7147 cp_parser_consume_semicolon_at_end_of_statement (parser);
7149 if (in_statement_expr
7150 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7151 /* This is the final expression statement of a statement
7153 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7155 statement = finish_expr_stmt (statement);
7162 /* Parse a compound-statement.
7165 { statement-seq [opt] }
7170 { label-declaration-seq [opt] statement-seq [opt] }
7172 label-declaration-seq:
7174 label-declaration-seq label-declaration
7176 Returns a tree representing the statement. */
7179 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7184 /* Consume the `{'. */
7185 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7186 return error_mark_node;
7187 /* Begin the compound-statement. */
7188 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7189 /* If the next keyword is `__label__' we have a label declaration. */
7190 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7191 cp_parser_label_declaration (parser);
7192 /* Parse an (optional) statement-seq. */
7193 cp_parser_statement_seq_opt (parser, in_statement_expr);
7194 /* Finish the compound-statement. */
7195 finish_compound_stmt (compound_stmt);
7196 /* Consume the `}'. */
7197 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7199 return compound_stmt;
7202 /* Parse an (optional) statement-seq.
7206 statement-seq [opt] statement */
7209 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7211 /* Scan statements until there aren't any more. */
7214 cp_token *token = cp_lexer_peek_token (parser->lexer);
7216 /* If we're looking at a `}', then we've run out of statements. */
7217 if (token->type == CPP_CLOSE_BRACE
7218 || token->type == CPP_EOF
7219 || token->type == CPP_PRAGMA_EOL)
7222 /* If we are in a compound statement and find 'else' then
7223 something went wrong. */
7224 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7226 if (parser->in_statement & IN_IF_STMT)
7230 token = cp_lexer_consume_token (parser->lexer);
7231 error ("%H%<else%> without a previous %<if%>", &token->location);
7235 /* Parse the statement. */
7236 cp_parser_statement (parser, in_statement_expr, true, NULL);
7240 /* Parse a selection-statement.
7242 selection-statement:
7243 if ( condition ) statement
7244 if ( condition ) statement else statement
7245 switch ( condition ) statement
7247 Returns the new IF_STMT or SWITCH_STMT.
7249 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7250 is a (possibly labeled) if statement which is not enclosed in
7251 braces and has an else clause. This is used to implement
7255 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7263 /* Peek at the next token. */
7264 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7266 /* See what kind of keyword it is. */
7267 keyword = token->keyword;
7276 /* Look for the `('. */
7277 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7279 cp_parser_skip_to_end_of_statement (parser);
7280 return error_mark_node;
7283 /* Begin the selection-statement. */
7284 if (keyword == RID_IF)
7285 statement = begin_if_stmt ();
7287 statement = begin_switch_stmt ();
7289 /* Parse the condition. */
7290 condition = cp_parser_condition (parser);
7291 /* Look for the `)'. */
7292 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7293 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7294 /*consume_paren=*/true);
7296 if (keyword == RID_IF)
7299 unsigned char in_statement;
7301 /* Add the condition. */
7302 finish_if_stmt_cond (condition, statement);
7304 /* Parse the then-clause. */
7305 in_statement = parser->in_statement;
7306 parser->in_statement |= IN_IF_STMT;
7307 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7309 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7310 add_stmt (build_empty_stmt ());
7311 cp_lexer_consume_token (parser->lexer);
7312 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7313 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7314 "empty body in an %<if%> statement");
7318 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7319 parser->in_statement = in_statement;
7321 finish_then_clause (statement);
7323 /* If the next token is `else', parse the else-clause. */
7324 if (cp_lexer_next_token_is_keyword (parser->lexer,
7327 /* Consume the `else' keyword. */
7328 cp_lexer_consume_token (parser->lexer);
7329 begin_else_clause (statement);
7330 /* Parse the else-clause. */
7331 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7333 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7334 OPT_Wempty_body, "suggest braces around "
7335 "empty body in an %<else%> statement");
7336 add_stmt (build_empty_stmt ());
7337 cp_lexer_consume_token (parser->lexer);
7340 cp_parser_implicitly_scoped_statement (parser, NULL);
7342 finish_else_clause (statement);
7344 /* If we are currently parsing a then-clause, then
7345 IF_P will not be NULL. We set it to true to
7346 indicate that this if statement has an else clause.
7347 This may trigger the Wparentheses warning below
7348 when we get back up to the parent if statement. */
7354 /* This if statement does not have an else clause. If
7355 NESTED_IF is true, then the then-clause is an if
7356 statement which does have an else clause. We warn
7357 about the potential ambiguity. */
7359 warning (OPT_Wparentheses,
7360 ("%Hsuggest explicit braces "
7361 "to avoid ambiguous %<else%>"),
7362 EXPR_LOCUS (statement));
7365 /* Now we're all done with the if-statement. */
7366 finish_if_stmt (statement);
7370 bool in_switch_statement_p;
7371 unsigned char in_statement;
7373 /* Add the condition. */
7374 finish_switch_cond (condition, statement);
7376 /* Parse the body of the switch-statement. */
7377 in_switch_statement_p = parser->in_switch_statement_p;
7378 in_statement = parser->in_statement;
7379 parser->in_switch_statement_p = true;
7380 parser->in_statement |= IN_SWITCH_STMT;
7381 cp_parser_implicitly_scoped_statement (parser, NULL);
7382 parser->in_switch_statement_p = in_switch_statement_p;
7383 parser->in_statement = in_statement;
7385 /* Now we're all done with the switch-statement. */
7386 finish_switch_stmt (statement);
7394 cp_parser_error (parser, "expected selection-statement");
7395 return error_mark_node;
7399 /* Parse a condition.
7403 type-specifier-seq declarator = initializer-clause
7404 type-specifier-seq declarator braced-init-list
7409 type-specifier-seq declarator asm-specification [opt]
7410 attributes [opt] = assignment-expression
7412 Returns the expression that should be tested. */
7415 cp_parser_condition (cp_parser* parser)
7417 cp_decl_specifier_seq type_specifiers;
7418 const char *saved_message;
7420 /* Try the declaration first. */
7421 cp_parser_parse_tentatively (parser);
7422 /* New types are not allowed in the type-specifier-seq for a
7424 saved_message = parser->type_definition_forbidden_message;
7425 parser->type_definition_forbidden_message
7426 = "types may not be defined in conditions";
7427 /* Parse the type-specifier-seq. */
7428 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7429 /*is_trailing_return=*/false,
7431 /* Restore the saved message. */
7432 parser->type_definition_forbidden_message = saved_message;
7433 /* If all is well, we might be looking at a declaration. */
7434 if (!cp_parser_error_occurred (parser))
7437 tree asm_specification;
7439 cp_declarator *declarator;
7440 tree initializer = NULL_TREE;
7442 /* Parse the declarator. */
7443 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7444 /*ctor_dtor_or_conv_p=*/NULL,
7445 /*parenthesized_p=*/NULL,
7446 /*member_p=*/false);
7447 /* Parse the attributes. */
7448 attributes = cp_parser_attributes_opt (parser);
7449 /* Parse the asm-specification. */
7450 asm_specification = cp_parser_asm_specification_opt (parser);
7451 /* If the next token is not an `=' or '{', then we might still be
7452 looking at an expression. For example:
7456 looks like a decl-specifier-seq and a declarator -- but then
7457 there is no `=', so this is an expression. */
7458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7459 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7460 cp_parser_simulate_error (parser);
7462 /* If we did see an `=' or '{', then we are looking at a declaration
7464 if (cp_parser_parse_definitely (parser))
7467 bool non_constant_p;
7468 bool flags = LOOKUP_ONLYCONVERTING;
7470 /* Create the declaration. */
7471 decl = start_decl (declarator, &type_specifiers,
7472 /*initialized_p=*/true,
7473 attributes, /*prefix_attributes=*/NULL_TREE,
7476 /* Parse the initializer. */
7477 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7479 initializer = cp_parser_braced_list (parser, &non_constant_p);
7480 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7485 /* Consume the `='. */
7486 cp_parser_require (parser, CPP_EQ, "%<=%>");
7487 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7489 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7490 maybe_warn_cpp0x ("extended initializer lists");
7492 if (!non_constant_p)
7493 initializer = fold_non_dependent_expr (initializer);
7495 /* Process the initializer. */
7496 cp_finish_decl (decl,
7497 initializer, !non_constant_p,
7502 pop_scope (pushed_scope);
7504 return convert_from_reference (decl);
7507 /* If we didn't even get past the declarator successfully, we are
7508 definitely not looking at a declaration. */
7510 cp_parser_abort_tentative_parse (parser);
7512 /* Otherwise, we are looking at an expression. */
7513 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7516 /* Parse an iteration-statement.
7518 iteration-statement:
7519 while ( condition ) statement
7520 do statement while ( expression ) ;
7521 for ( for-init-statement condition [opt] ; expression [opt] )
7524 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7527 cp_parser_iteration_statement (cp_parser* parser)
7532 unsigned char in_statement;
7534 /* Peek at the next token. */
7535 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7537 return error_mark_node;
7539 /* Remember whether or not we are already within an iteration
7541 in_statement = parser->in_statement;
7543 /* See what kind of keyword it is. */
7544 keyword = token->keyword;
7551 /* Begin the while-statement. */
7552 statement = begin_while_stmt ();
7553 /* Look for the `('. */
7554 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7555 /* Parse the condition. */
7556 condition = cp_parser_condition (parser);
7557 finish_while_stmt_cond (condition, statement);
7558 /* Look for the `)'. */
7559 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7560 /* Parse the dependent statement. */
7561 parser->in_statement = IN_ITERATION_STMT;
7562 cp_parser_already_scoped_statement (parser);
7563 parser->in_statement = in_statement;
7564 /* We're done with the while-statement. */
7565 finish_while_stmt (statement);
7573 /* Begin the do-statement. */
7574 statement = begin_do_stmt ();
7575 /* Parse the body of the do-statement. */
7576 parser->in_statement = IN_ITERATION_STMT;
7577 cp_parser_implicitly_scoped_statement (parser, NULL);
7578 parser->in_statement = in_statement;
7579 finish_do_body (statement);
7580 /* Look for the `while' keyword. */
7581 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7582 /* Look for the `('. */
7583 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7584 /* Parse the expression. */
7585 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7586 /* We're done with the do-statement. */
7587 finish_do_stmt (expression, statement);
7588 /* Look for the `)'. */
7589 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7590 /* Look for the `;'. */
7591 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7597 tree condition = NULL_TREE;
7598 tree expression = NULL_TREE;
7600 /* Begin the for-statement. */
7601 statement = begin_for_stmt ();
7602 /* Look for the `('. */
7603 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7604 /* Parse the initialization. */
7605 cp_parser_for_init_statement (parser);
7606 finish_for_init_stmt (statement);
7608 /* If there's a condition, process it. */
7609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7610 condition = cp_parser_condition (parser);
7611 finish_for_cond (condition, statement);
7612 /* Look for the `;'. */
7613 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7615 /* If there's an expression, process it. */
7616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7617 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7618 finish_for_expr (expression, statement);
7619 /* Look for the `)'. */
7620 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7622 /* Parse the body of the for-statement. */
7623 parser->in_statement = IN_ITERATION_STMT;
7624 cp_parser_already_scoped_statement (parser);
7625 parser->in_statement = in_statement;
7627 /* We're done with the for-statement. */
7628 finish_for_stmt (statement);
7633 cp_parser_error (parser, "expected iteration-statement");
7634 statement = error_mark_node;
7641 /* Parse a for-init-statement.
7644 expression-statement
7645 simple-declaration */
7648 cp_parser_for_init_statement (cp_parser* parser)
7650 /* If the next token is a `;', then we have an empty
7651 expression-statement. Grammatically, this is also a
7652 simple-declaration, but an invalid one, because it does not
7653 declare anything. Therefore, if we did not handle this case
7654 specially, we would issue an error message about an invalid
7656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7658 /* We're going to speculatively look for a declaration, falling back
7659 to an expression, if necessary. */
7660 cp_parser_parse_tentatively (parser);
7661 /* Parse the declaration. */
7662 cp_parser_simple_declaration (parser,
7663 /*function_definition_allowed_p=*/false);
7664 /* If the tentative parse failed, then we shall need to look for an
7665 expression-statement. */
7666 if (cp_parser_parse_definitely (parser))
7670 cp_parser_expression_statement (parser, false);
7673 /* Parse a jump-statement.
7678 return expression [opt] ;
7679 return braced-init-list ;
7687 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7690 cp_parser_jump_statement (cp_parser* parser)
7692 tree statement = error_mark_node;
7695 unsigned char in_statement;
7697 /* Peek at the next token. */
7698 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7700 return error_mark_node;
7702 /* See what kind of keyword it is. */
7703 keyword = token->keyword;
7707 in_statement = parser->in_statement & ~IN_IF_STMT;
7708 switch (in_statement)
7711 error ("%Hbreak statement not within loop or switch", &token->location);
7714 gcc_assert ((in_statement & IN_SWITCH_STMT)
7715 || in_statement == IN_ITERATION_STMT);
7716 statement = finish_break_stmt ();
7719 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7722 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7725 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7729 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7732 error ("%Hcontinue statement not within a loop", &token->location);
7734 case IN_ITERATION_STMT:
7736 statement = finish_continue_stmt ();
7739 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7744 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7750 bool expr_non_constant_p;
7752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7754 maybe_warn_cpp0x ("extended initializer lists");
7755 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7757 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7758 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7760 /* If the next token is a `;', then there is no
7763 /* Build the return-statement. */
7764 statement = finish_return_stmt (expr);
7765 /* Look for the final `;'. */
7766 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7771 /* Create the goto-statement. */
7772 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7774 /* Issue a warning about this use of a GNU extension. */
7775 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7776 /* Consume the '*' token. */
7777 cp_lexer_consume_token (parser->lexer);
7778 /* Parse the dependent expression. */
7779 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7782 finish_goto_stmt (cp_parser_identifier (parser));
7783 /* Look for the final `;'. */
7784 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7788 cp_parser_error (parser, "expected jump-statement");
7795 /* Parse a declaration-statement.
7797 declaration-statement:
7798 block-declaration */
7801 cp_parser_declaration_statement (cp_parser* parser)
7805 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7806 p = obstack_alloc (&declarator_obstack, 0);
7808 /* Parse the block-declaration. */
7809 cp_parser_block_declaration (parser, /*statement_p=*/true);
7811 /* Free any declarators allocated. */
7812 obstack_free (&declarator_obstack, p);
7814 /* Finish off the statement. */
7818 /* Some dependent statements (like `if (cond) statement'), are
7819 implicitly in their own scope. In other words, if the statement is
7820 a single statement (as opposed to a compound-statement), it is
7821 none-the-less treated as if it were enclosed in braces. Any
7822 declarations appearing in the dependent statement are out of scope
7823 after control passes that point. This function parses a statement,
7824 but ensures that is in its own scope, even if it is not a
7827 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7828 is a (possibly labeled) if statement which is not enclosed in
7829 braces and has an else clause. This is used to implement
7832 Returns the new statement. */
7835 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7842 /* Mark if () ; with a special NOP_EXPR. */
7843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7845 cp_lexer_consume_token (parser->lexer);
7846 statement = add_stmt (build_empty_stmt ());
7848 /* if a compound is opened, we simply parse the statement directly. */
7849 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7850 statement = cp_parser_compound_statement (parser, NULL, false);
7851 /* If the token is not a `{', then we must take special action. */
7854 /* Create a compound-statement. */
7855 statement = begin_compound_stmt (0);
7856 /* Parse the dependent-statement. */
7857 cp_parser_statement (parser, NULL_TREE, false, if_p);
7858 /* Finish the dummy compound-statement. */
7859 finish_compound_stmt (statement);
7862 /* Return the statement. */
7866 /* For some dependent statements (like `while (cond) statement'), we
7867 have already created a scope. Therefore, even if the dependent
7868 statement is a compound-statement, we do not want to create another
7872 cp_parser_already_scoped_statement (cp_parser* parser)
7874 /* If the token is a `{', then we must take special action. */
7875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7876 cp_parser_statement (parser, NULL_TREE, false, NULL);
7879 /* Avoid calling cp_parser_compound_statement, so that we
7880 don't create a new scope. Do everything else by hand. */
7881 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7882 /* If the next keyword is `__label__' we have a label declaration. */
7883 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7884 cp_parser_label_declaration (parser);
7885 /* Parse an (optional) statement-seq. */
7886 cp_parser_statement_seq_opt (parser, NULL_TREE);
7887 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7891 /* Declarations [gram.dcl.dcl] */
7893 /* Parse an optional declaration-sequence.
7897 declaration-seq declaration */
7900 cp_parser_declaration_seq_opt (cp_parser* parser)
7906 token = cp_lexer_peek_token (parser->lexer);
7908 if (token->type == CPP_CLOSE_BRACE
7909 || token->type == CPP_EOF
7910 || token->type == CPP_PRAGMA_EOL)
7913 if (token->type == CPP_SEMICOLON)
7915 /* A declaration consisting of a single semicolon is
7916 invalid. Allow it unless we're being pedantic. */
7917 cp_lexer_consume_token (parser->lexer);
7918 if (!in_system_header)
7919 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7923 /* If we're entering or exiting a region that's implicitly
7924 extern "C", modify the lang context appropriately. */
7925 if (!parser->implicit_extern_c && token->implicit_extern_c)
7927 push_lang_context (lang_name_c);
7928 parser->implicit_extern_c = true;
7930 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7932 pop_lang_context ();
7933 parser->implicit_extern_c = false;
7936 if (token->type == CPP_PRAGMA)
7938 /* A top-level declaration can consist solely of a #pragma.
7939 A nested declaration cannot, so this is done here and not
7940 in cp_parser_declaration. (A #pragma at block scope is
7941 handled in cp_parser_statement.) */
7942 cp_parser_pragma (parser, pragma_external);
7946 /* Parse the declaration itself. */
7947 cp_parser_declaration (parser);
7951 /* Parse a declaration.
7956 template-declaration
7957 explicit-instantiation
7958 explicit-specialization
7959 linkage-specification
7960 namespace-definition
7965 __extension__ declaration */
7968 cp_parser_declaration (cp_parser* parser)
7975 /* Check for the `__extension__' keyword. */
7976 if (cp_parser_extension_opt (parser, &saved_pedantic))
7978 /* Parse the qualified declaration. */
7979 cp_parser_declaration (parser);
7980 /* Restore the PEDANTIC flag. */
7981 pedantic = saved_pedantic;
7986 /* Try to figure out what kind of declaration is present. */
7987 token1 = *cp_lexer_peek_token (parser->lexer);
7989 if (token1.type != CPP_EOF)
7990 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7993 token2.type = CPP_EOF;
7994 token2.keyword = RID_MAX;
7997 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7998 p = obstack_alloc (&declarator_obstack, 0);
8000 /* If the next token is `extern' and the following token is a string
8001 literal, then we have a linkage specification. */
8002 if (token1.keyword == RID_EXTERN
8003 && cp_parser_is_string_literal (&token2))
8004 cp_parser_linkage_specification (parser);
8005 /* If the next token is `template', then we have either a template
8006 declaration, an explicit instantiation, or an explicit
8008 else if (token1.keyword == RID_TEMPLATE)
8010 /* `template <>' indicates a template specialization. */
8011 if (token2.type == CPP_LESS
8012 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8013 cp_parser_explicit_specialization (parser);
8014 /* `template <' indicates a template declaration. */
8015 else if (token2.type == CPP_LESS)
8016 cp_parser_template_declaration (parser, /*member_p=*/false);
8017 /* Anything else must be an explicit instantiation. */
8019 cp_parser_explicit_instantiation (parser);
8021 /* If the next token is `export', then we have a template
8023 else if (token1.keyword == RID_EXPORT)
8024 cp_parser_template_declaration (parser, /*member_p=*/false);
8025 /* If the next token is `extern', 'static' or 'inline' and the one
8026 after that is `template', we have a GNU extended explicit
8027 instantiation directive. */
8028 else if (cp_parser_allow_gnu_extensions_p (parser)
8029 && (token1.keyword == RID_EXTERN
8030 || token1.keyword == RID_STATIC
8031 || token1.keyword == RID_INLINE)
8032 && token2.keyword == RID_TEMPLATE)
8033 cp_parser_explicit_instantiation (parser);
8034 /* If the next token is `namespace', check for a named or unnamed
8035 namespace definition. */
8036 else if (token1.keyword == RID_NAMESPACE
8037 && (/* A named namespace definition. */
8038 (token2.type == CPP_NAME
8039 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8041 /* An unnamed namespace definition. */
8042 || token2.type == CPP_OPEN_BRACE
8043 || token2.keyword == RID_ATTRIBUTE))
8044 cp_parser_namespace_definition (parser);
8045 /* An inline (associated) namespace definition. */
8046 else if (token1.keyword == RID_INLINE
8047 && token2.keyword == RID_NAMESPACE)
8048 cp_parser_namespace_definition (parser);
8049 /* Objective-C++ declaration/definition. */
8050 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8051 cp_parser_objc_declaration (parser);
8052 /* We must have either a block declaration or a function
8055 /* Try to parse a block-declaration, or a function-definition. */
8056 cp_parser_block_declaration (parser, /*statement_p=*/false);
8058 /* Free any declarators allocated. */
8059 obstack_free (&declarator_obstack, p);
8062 /* Parse a block-declaration.
8067 namespace-alias-definition
8074 __extension__ block-declaration
8079 static_assert-declaration
8081 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8082 part of a declaration-statement. */
8085 cp_parser_block_declaration (cp_parser *parser,
8091 /* Check for the `__extension__' keyword. */
8092 if (cp_parser_extension_opt (parser, &saved_pedantic))
8094 /* Parse the qualified declaration. */
8095 cp_parser_block_declaration (parser, statement_p);
8096 /* Restore the PEDANTIC flag. */
8097 pedantic = saved_pedantic;
8102 /* Peek at the next token to figure out which kind of declaration is
8104 token1 = cp_lexer_peek_token (parser->lexer);
8106 /* If the next keyword is `asm', we have an asm-definition. */
8107 if (token1->keyword == RID_ASM)
8110 cp_parser_commit_to_tentative_parse (parser);
8111 cp_parser_asm_definition (parser);
8113 /* If the next keyword is `namespace', we have a
8114 namespace-alias-definition. */
8115 else if (token1->keyword == RID_NAMESPACE)
8116 cp_parser_namespace_alias_definition (parser);
8117 /* If the next keyword is `using', we have either a
8118 using-declaration or a using-directive. */
8119 else if (token1->keyword == RID_USING)
8124 cp_parser_commit_to_tentative_parse (parser);
8125 /* If the token after `using' is `namespace', then we have a
8127 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8128 if (token2->keyword == RID_NAMESPACE)
8129 cp_parser_using_directive (parser);
8130 /* Otherwise, it's a using-declaration. */
8132 cp_parser_using_declaration (parser,
8133 /*access_declaration_p=*/false);
8135 /* If the next keyword is `__label__' we have a misplaced label
8137 else if (token1->keyword == RID_LABEL)
8139 cp_lexer_consume_token (parser->lexer);
8140 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8141 cp_parser_skip_to_end_of_statement (parser);
8142 /* If the next token is now a `;', consume it. */
8143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8144 cp_lexer_consume_token (parser->lexer);
8146 /* If the next token is `static_assert' we have a static assertion. */
8147 else if (token1->keyword == RID_STATIC_ASSERT)
8148 cp_parser_static_assert (parser, /*member_p=*/false);
8149 /* Anything else must be a simple-declaration. */
8151 cp_parser_simple_declaration (parser, !statement_p);
8154 /* Parse a simple-declaration.
8157 decl-specifier-seq [opt] init-declarator-list [opt] ;
8159 init-declarator-list:
8161 init-declarator-list , init-declarator
8163 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8164 function-definition as a simple-declaration. */
8167 cp_parser_simple_declaration (cp_parser* parser,
8168 bool function_definition_allowed_p)
8170 cp_decl_specifier_seq decl_specifiers;
8171 int declares_class_or_enum;
8172 bool saw_declarator;
8174 /* Defer access checks until we know what is being declared; the
8175 checks for names appearing in the decl-specifier-seq should be
8176 done as if we were in the scope of the thing being declared. */
8177 push_deferring_access_checks (dk_deferred);
8179 /* Parse the decl-specifier-seq. We have to keep track of whether
8180 or not the decl-specifier-seq declares a named class or
8181 enumeration type, since that is the only case in which the
8182 init-declarator-list is allowed to be empty.
8186 In a simple-declaration, the optional init-declarator-list can be
8187 omitted only when declaring a class or enumeration, that is when
8188 the decl-specifier-seq contains either a class-specifier, an
8189 elaborated-type-specifier, or an enum-specifier. */
8190 cp_parser_decl_specifier_seq (parser,
8191 CP_PARSER_FLAGS_OPTIONAL,
8193 &declares_class_or_enum);
8194 /* We no longer need to defer access checks. */
8195 stop_deferring_access_checks ();
8197 /* In a block scope, a valid declaration must always have a
8198 decl-specifier-seq. By not trying to parse declarators, we can
8199 resolve the declaration/expression ambiguity more quickly. */
8200 if (!function_definition_allowed_p
8201 && !decl_specifiers.any_specifiers_p)
8203 cp_parser_error (parser, "expected declaration");
8207 /* If the next two tokens are both identifiers, the code is
8208 erroneous. The usual cause of this situation is code like:
8212 where "T" should name a type -- but does not. */
8213 if (!decl_specifiers.type
8214 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8216 /* If parsing tentatively, we should commit; we really are
8217 looking at a declaration. */
8218 cp_parser_commit_to_tentative_parse (parser);
8223 /* If we have seen at least one decl-specifier, and the next token
8224 is not a parenthesis, then we must be looking at a declaration.
8225 (After "int (" we might be looking at a functional cast.) */
8226 if (decl_specifiers.any_specifiers_p
8227 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8228 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8229 && !cp_parser_error_occurred (parser))
8230 cp_parser_commit_to_tentative_parse (parser);
8232 /* Keep going until we hit the `;' at the end of the simple
8234 saw_declarator = false;
8235 while (cp_lexer_next_token_is_not (parser->lexer,
8239 bool function_definition_p;
8244 /* If we are processing next declarator, coma is expected */
8245 token = cp_lexer_peek_token (parser->lexer);
8246 gcc_assert (token->type == CPP_COMMA);
8247 cp_lexer_consume_token (parser->lexer);
8250 saw_declarator = true;
8252 /* Parse the init-declarator. */
8253 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8255 function_definition_allowed_p,
8257 declares_class_or_enum,
8258 &function_definition_p);
8259 /* If an error occurred while parsing tentatively, exit quickly.
8260 (That usually happens when in the body of a function; each
8261 statement is treated as a declaration-statement until proven
8263 if (cp_parser_error_occurred (parser))
8265 /* Handle function definitions specially. */
8266 if (function_definition_p)
8268 /* If the next token is a `,', then we are probably
8269 processing something like:
8273 which is erroneous. */
8274 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8276 cp_token *token = cp_lexer_peek_token (parser->lexer);
8277 error ("%Hmixing declarations and function-definitions is forbidden",
8280 /* Otherwise, we're done with the list of declarators. */
8283 pop_deferring_access_checks ();
8287 /* The next token should be either a `,' or a `;'. */
8288 token = cp_lexer_peek_token (parser->lexer);
8289 /* If it's a `,', there are more declarators to come. */
8290 if (token->type == CPP_COMMA)
8291 /* will be consumed next time around */;
8292 /* If it's a `;', we are done. */
8293 else if (token->type == CPP_SEMICOLON)
8295 /* Anything else is an error. */
8298 /* If we have already issued an error message we don't need
8299 to issue another one. */
8300 if (decl != error_mark_node
8301 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8302 cp_parser_error (parser, "expected %<,%> or %<;%>");
8303 /* Skip tokens until we reach the end of the statement. */
8304 cp_parser_skip_to_end_of_statement (parser);
8305 /* If the next token is now a `;', consume it. */
8306 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8307 cp_lexer_consume_token (parser->lexer);
8310 /* After the first time around, a function-definition is not
8311 allowed -- even if it was OK at first. For example:
8316 function_definition_allowed_p = false;
8319 /* Issue an error message if no declarators are present, and the
8320 decl-specifier-seq does not itself declare a class or
8322 if (!saw_declarator)
8324 if (cp_parser_declares_only_class_p (parser))
8325 shadow_tag (&decl_specifiers);
8326 /* Perform any deferred access checks. */
8327 perform_deferred_access_checks ();
8330 /* Consume the `;'. */
8331 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8334 pop_deferring_access_checks ();
8337 /* Parse a decl-specifier-seq.
8340 decl-specifier-seq [opt] decl-specifier
8343 storage-class-specifier
8354 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8356 The parser flags FLAGS is used to control type-specifier parsing.
8358 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8361 1: one of the decl-specifiers is an elaborated-type-specifier
8362 (i.e., a type declaration)
8363 2: one of the decl-specifiers is an enum-specifier or a
8364 class-specifier (i.e., a type definition)
8369 cp_parser_decl_specifier_seq (cp_parser* parser,
8370 cp_parser_flags flags,
8371 cp_decl_specifier_seq *decl_specs,
8372 int* declares_class_or_enum)
8374 bool constructor_possible_p = !parser->in_declarator_p;
8375 cp_token *start_token = NULL;
8377 /* Clear DECL_SPECS. */
8378 clear_decl_specs (decl_specs);
8380 /* Assume no class or enumeration type is declared. */
8381 *declares_class_or_enum = 0;
8383 /* Keep reading specifiers until there are no more to read. */
8387 bool found_decl_spec;
8390 /* Peek at the next token. */
8391 token = cp_lexer_peek_token (parser->lexer);
8393 /* Save the first token of the decl spec list for error
8396 start_token = token;
8397 /* Handle attributes. */
8398 if (token->keyword == RID_ATTRIBUTE)
8400 /* Parse the attributes. */
8401 decl_specs->attributes
8402 = chainon (decl_specs->attributes,
8403 cp_parser_attributes_opt (parser));
8406 /* Assume we will find a decl-specifier keyword. */
8407 found_decl_spec = true;
8408 /* If the next token is an appropriate keyword, we can simply
8409 add it to the list. */
8410 switch (token->keyword)
8415 if (!at_class_scope_p ())
8417 error ("%H%<friend%> used outside of class", &token->location);
8418 cp_lexer_purge_token (parser->lexer);
8422 ++decl_specs->specs[(int) ds_friend];
8423 /* Consume the token. */
8424 cp_lexer_consume_token (parser->lexer);
8428 /* function-specifier:
8435 cp_parser_function_specifier_opt (parser, decl_specs);
8441 ++decl_specs->specs[(int) ds_typedef];
8442 /* Consume the token. */
8443 cp_lexer_consume_token (parser->lexer);
8444 /* A constructor declarator cannot appear in a typedef. */
8445 constructor_possible_p = false;
8446 /* The "typedef" keyword can only occur in a declaration; we
8447 may as well commit at this point. */
8448 cp_parser_commit_to_tentative_parse (parser);
8450 if (decl_specs->storage_class != sc_none)
8451 decl_specs->conflicting_specifiers_p = true;
8454 /* storage-class-specifier:
8464 if (cxx_dialect == cxx98)
8466 /* Consume the token. */
8467 cp_lexer_consume_token (parser->lexer);
8469 /* Complain about `auto' as a storage specifier, if
8470 we're complaining about C++0x compatibility. */
8473 "%H%<auto%> will change meaning in C++0x; please remove it",
8476 /* Set the storage class anyway. */
8477 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8481 /* C++0x auto type-specifier. */
8482 found_decl_spec = false;
8489 /* Consume the token. */
8490 cp_lexer_consume_token (parser->lexer);
8491 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8495 /* Consume the token. */
8496 cp_lexer_consume_token (parser->lexer);
8497 ++decl_specs->specs[(int) ds_thread];
8501 /* We did not yet find a decl-specifier yet. */
8502 found_decl_spec = false;
8506 /* Constructors are a special case. The `S' in `S()' is not a
8507 decl-specifier; it is the beginning of the declarator. */
8510 && constructor_possible_p
8511 && (cp_parser_constructor_declarator_p
8512 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8514 /* If we don't have a DECL_SPEC yet, then we must be looking at
8515 a type-specifier. */
8516 if (!found_decl_spec && !constructor_p)
8518 int decl_spec_declares_class_or_enum;
8519 bool is_cv_qualifier;
8523 = cp_parser_type_specifier (parser, flags,
8525 /*is_declaration=*/true,
8526 &decl_spec_declares_class_or_enum,
8528 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8530 /* If this type-specifier referenced a user-defined type
8531 (a typedef, class-name, etc.), then we can't allow any
8532 more such type-specifiers henceforth.
8536 The longest sequence of decl-specifiers that could
8537 possibly be a type name is taken as the
8538 decl-specifier-seq of a declaration. The sequence shall
8539 be self-consistent as described below.
8543 As a general rule, at most one type-specifier is allowed
8544 in the complete decl-specifier-seq of a declaration. The
8545 only exceptions are the following:
8547 -- const or volatile can be combined with any other
8550 -- signed or unsigned can be combined with char, long,
8558 void g (const int Pc);
8560 Here, Pc is *not* part of the decl-specifier seq; it's
8561 the declarator. Therefore, once we see a type-specifier
8562 (other than a cv-qualifier), we forbid any additional
8563 user-defined types. We *do* still allow things like `int
8564 int' to be considered a decl-specifier-seq, and issue the
8565 error message later. */
8566 if (type_spec && !is_cv_qualifier)
8567 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8568 /* A constructor declarator cannot follow a type-specifier. */
8571 constructor_possible_p = false;
8572 found_decl_spec = true;
8576 /* If we still do not have a DECL_SPEC, then there are no more
8578 if (!found_decl_spec)
8581 decl_specs->any_specifiers_p = true;
8582 /* After we see one decl-specifier, further decl-specifiers are
8584 flags |= CP_PARSER_FLAGS_OPTIONAL;
8587 cp_parser_check_decl_spec (decl_specs, start_token->location);
8589 /* Don't allow a friend specifier with a class definition. */
8590 if (decl_specs->specs[(int) ds_friend] != 0
8591 && (*declares_class_or_enum & 2))
8592 error ("%Hclass definition may not be declared a friend",
8593 &start_token->location);
8596 /* Parse an (optional) storage-class-specifier.
8598 storage-class-specifier:
8607 storage-class-specifier:
8610 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8613 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8615 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8618 if (cxx_dialect != cxx98)
8620 /* Fall through for C++98. */
8627 /* Consume the token. */
8628 return cp_lexer_consume_token (parser->lexer)->u.value;
8635 /* Parse an (optional) function-specifier.
8642 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8643 Updates DECL_SPECS, if it is non-NULL. */
8646 cp_parser_function_specifier_opt (cp_parser* parser,
8647 cp_decl_specifier_seq *decl_specs)
8649 cp_token *token = cp_lexer_peek_token (parser->lexer);
8650 switch (token->keyword)
8654 ++decl_specs->specs[(int) ds_inline];
8658 /* 14.5.2.3 [temp.mem]
8660 A member function template shall not be virtual. */
8661 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8662 error ("%Htemplates may not be %<virtual%>", &token->location);
8663 else if (decl_specs)
8664 ++decl_specs->specs[(int) ds_virtual];
8669 ++decl_specs->specs[(int) ds_explicit];
8676 /* Consume the token. */
8677 return cp_lexer_consume_token (parser->lexer)->u.value;
8680 /* Parse a linkage-specification.
8682 linkage-specification:
8683 extern string-literal { declaration-seq [opt] }
8684 extern string-literal declaration */
8687 cp_parser_linkage_specification (cp_parser* parser)
8691 /* Look for the `extern' keyword. */
8692 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8694 /* Look for the string-literal. */
8695 linkage = cp_parser_string_literal (parser, false, false);
8697 /* Transform the literal into an identifier. If the literal is a
8698 wide-character string, or contains embedded NULs, then we can't
8699 handle it as the user wants. */
8700 if (strlen (TREE_STRING_POINTER (linkage))
8701 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8703 cp_parser_error (parser, "invalid linkage-specification");
8704 /* Assume C++ linkage. */
8705 linkage = lang_name_cplusplus;
8708 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8710 /* We're now using the new linkage. */
8711 push_lang_context (linkage);
8713 /* If the next token is a `{', then we're using the first
8715 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8717 /* Consume the `{' token. */
8718 cp_lexer_consume_token (parser->lexer);
8719 /* Parse the declarations. */
8720 cp_parser_declaration_seq_opt (parser);
8721 /* Look for the closing `}'. */
8722 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8724 /* Otherwise, there's just one declaration. */
8727 bool saved_in_unbraced_linkage_specification_p;
8729 saved_in_unbraced_linkage_specification_p
8730 = parser->in_unbraced_linkage_specification_p;
8731 parser->in_unbraced_linkage_specification_p = true;
8732 cp_parser_declaration (parser);
8733 parser->in_unbraced_linkage_specification_p
8734 = saved_in_unbraced_linkage_specification_p;
8737 /* We're done with the linkage-specification. */
8738 pop_lang_context ();
8741 /* Parse a static_assert-declaration.
8743 static_assert-declaration:
8744 static_assert ( constant-expression , string-literal ) ;
8746 If MEMBER_P, this static_assert is a class member. */
8749 cp_parser_static_assert(cp_parser *parser, bool member_p)
8754 location_t saved_loc;
8756 /* Peek at the `static_assert' token so we can keep track of exactly
8757 where the static assertion started. */
8758 token = cp_lexer_peek_token (parser->lexer);
8759 saved_loc = token->location;
8761 /* Look for the `static_assert' keyword. */
8762 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8763 "%<static_assert%>"))
8766 /* We know we are in a static assertion; commit to any tentative
8768 if (cp_parser_parsing_tentatively (parser))
8769 cp_parser_commit_to_tentative_parse (parser);
8771 /* Parse the `(' starting the static assertion condition. */
8772 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8774 /* Parse the constant-expression. */
8776 cp_parser_constant_expression (parser,
8777 /*allow_non_constant_p=*/false,
8778 /*non_constant_p=*/NULL);
8780 /* Parse the separating `,'. */
8781 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8783 /* Parse the string-literal message. */
8784 message = cp_parser_string_literal (parser,
8785 /*translate=*/false,
8788 /* A `)' completes the static assertion. */
8789 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8790 cp_parser_skip_to_closing_parenthesis (parser,
8791 /*recovering=*/true,
8793 /*consume_paren=*/true);
8795 /* A semicolon terminates the declaration. */
8796 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8798 /* Complete the static assertion, which may mean either processing
8799 the static assert now or saving it for template instantiation. */
8800 finish_static_assert (condition, message, saved_loc, member_p);
8803 /* Parse a `decltype' type. Returns the type.
8805 simple-type-specifier:
8806 decltype ( expression ) */
8809 cp_parser_decltype (cp_parser *parser)
8812 bool id_expression_or_member_access_p = false;
8813 const char *saved_message;
8814 bool saved_integral_constant_expression_p;
8815 bool saved_non_integral_constant_expression_p;
8816 cp_token *id_expr_start_token;
8818 /* Look for the `decltype' token. */
8819 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8820 return error_mark_node;
8822 /* Types cannot be defined in a `decltype' expression. Save away the
8824 saved_message = parser->type_definition_forbidden_message;
8826 /* And create the new one. */
8827 parser->type_definition_forbidden_message
8828 = "types may not be defined in %<decltype%> expressions";
8830 /* The restrictions on constant-expressions do not apply inside
8831 decltype expressions. */
8832 saved_integral_constant_expression_p
8833 = parser->integral_constant_expression_p;
8834 saved_non_integral_constant_expression_p
8835 = parser->non_integral_constant_expression_p;
8836 parser->integral_constant_expression_p = false;
8838 /* Do not actually evaluate the expression. */
8841 /* Parse the opening `('. */
8842 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8843 return error_mark_node;
8845 /* First, try parsing an id-expression. */
8846 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8847 cp_parser_parse_tentatively (parser);
8848 expr = cp_parser_id_expression (parser,
8849 /*template_keyword_p=*/false,
8850 /*check_dependency_p=*/true,
8851 /*template_p=*/NULL,
8852 /*declarator_p=*/false,
8853 /*optional_p=*/false);
8855 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8857 bool non_integral_constant_expression_p = false;
8858 tree id_expression = expr;
8860 const char *error_msg;
8862 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8863 /* Lookup the name we got back from the id-expression. */
8864 expr = cp_parser_lookup_name (parser, expr,
8866 /*is_template=*/false,
8867 /*is_namespace=*/false,
8868 /*check_dependency=*/true,
8869 /*ambiguous_decls=*/NULL,
8870 id_expr_start_token->location);
8873 && expr != error_mark_node
8874 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8875 && TREE_CODE (expr) != TYPE_DECL
8876 && (TREE_CODE (expr) != BIT_NOT_EXPR
8877 || !TYPE_P (TREE_OPERAND (expr, 0)))
8878 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8880 /* Complete lookup of the id-expression. */
8881 expr = (finish_id_expression
8882 (id_expression, expr, parser->scope, &idk,
8883 /*integral_constant_expression_p=*/false,
8884 /*allow_non_integral_constant_expression_p=*/true,
8885 &non_integral_constant_expression_p,
8886 /*template_p=*/false,
8888 /*address_p=*/false,
8889 /*template_arg_p=*/false,
8891 id_expr_start_token->location));
8893 if (expr == error_mark_node)
8894 /* We found an id-expression, but it was something that we
8895 should not have found. This is an error, not something
8896 we can recover from, so note that we found an
8897 id-expression and we'll recover as gracefully as
8899 id_expression_or_member_access_p = true;
8903 && expr != error_mark_node
8904 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8905 /* We have an id-expression. */
8906 id_expression_or_member_access_p = true;
8909 if (!id_expression_or_member_access_p)
8911 /* Abort the id-expression parse. */
8912 cp_parser_abort_tentative_parse (parser);
8914 /* Parsing tentatively, again. */
8915 cp_parser_parse_tentatively (parser);
8917 /* Parse a class member access. */
8918 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8920 /*member_access_only_p=*/true, NULL);
8923 && expr != error_mark_node
8924 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8925 /* We have an id-expression. */
8926 id_expression_or_member_access_p = true;
8929 if (id_expression_or_member_access_p)
8930 /* We have parsed the complete id-expression or member access. */
8931 cp_parser_parse_definitely (parser);
8934 bool saved_greater_than_is_operator_p;
8936 /* Abort our attempt to parse an id-expression or member access
8938 cp_parser_abort_tentative_parse (parser);
8940 /* Within a parenthesized expression, a `>' token is always
8941 the greater-than operator. */
8942 saved_greater_than_is_operator_p
8943 = parser->greater_than_is_operator_p;
8944 parser->greater_than_is_operator_p = true;
8946 /* Parse a full expression. */
8947 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8949 /* The `>' token might be the end of a template-id or
8950 template-parameter-list now. */
8951 parser->greater_than_is_operator_p
8952 = saved_greater_than_is_operator_p;
8955 /* Go back to evaluating expressions. */
8958 /* Restore the old message and the integral constant expression
8960 parser->type_definition_forbidden_message = saved_message;
8961 parser->integral_constant_expression_p
8962 = saved_integral_constant_expression_p;
8963 parser->non_integral_constant_expression_p
8964 = saved_non_integral_constant_expression_p;
8966 if (expr == error_mark_node)
8968 /* Skip everything up to the closing `)'. */
8969 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8970 /*consume_paren=*/true);
8971 return error_mark_node;
8974 /* Parse to the closing `)'. */
8975 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8977 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8978 /*consume_paren=*/true);
8979 return error_mark_node;
8982 return finish_decltype_type (expr, id_expression_or_member_access_p);
8985 /* Special member functions [gram.special] */
8987 /* Parse a conversion-function-id.
8989 conversion-function-id:
8990 operator conversion-type-id
8992 Returns an IDENTIFIER_NODE representing the operator. */
8995 cp_parser_conversion_function_id (cp_parser* parser)
8999 tree saved_qualifying_scope;
9000 tree saved_object_scope;
9001 tree pushed_scope = NULL_TREE;
9003 /* Look for the `operator' token. */
9004 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9005 return error_mark_node;
9006 /* When we parse the conversion-type-id, the current scope will be
9007 reset. However, we need that information in able to look up the
9008 conversion function later, so we save it here. */
9009 saved_scope = parser->scope;
9010 saved_qualifying_scope = parser->qualifying_scope;
9011 saved_object_scope = parser->object_scope;
9012 /* We must enter the scope of the class so that the names of
9013 entities declared within the class are available in the
9014 conversion-type-id. For example, consider:
9021 S::operator I() { ... }
9023 In order to see that `I' is a type-name in the definition, we
9024 must be in the scope of `S'. */
9026 pushed_scope = push_scope (saved_scope);
9027 /* Parse the conversion-type-id. */
9028 type = cp_parser_conversion_type_id (parser);
9029 /* Leave the scope of the class, if any. */
9031 pop_scope (pushed_scope);
9032 /* Restore the saved scope. */
9033 parser->scope = saved_scope;
9034 parser->qualifying_scope = saved_qualifying_scope;
9035 parser->object_scope = saved_object_scope;
9036 /* If the TYPE is invalid, indicate failure. */
9037 if (type == error_mark_node)
9038 return error_mark_node;
9039 return mangle_conv_op_name_for_type (type);
9042 /* Parse a conversion-type-id:
9045 type-specifier-seq conversion-declarator [opt]
9047 Returns the TYPE specified. */
9050 cp_parser_conversion_type_id (cp_parser* parser)
9053 cp_decl_specifier_seq type_specifiers;
9054 cp_declarator *declarator;
9055 tree type_specified;
9057 /* Parse the attributes. */
9058 attributes = cp_parser_attributes_opt (parser);
9059 /* Parse the type-specifiers. */
9060 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9061 /*is_trailing_return=*/false,
9063 /* If that didn't work, stop. */
9064 if (type_specifiers.type == error_mark_node)
9065 return error_mark_node;
9066 /* Parse the conversion-declarator. */
9067 declarator = cp_parser_conversion_declarator_opt (parser);
9069 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9070 /*initialized=*/0, &attributes);
9072 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9074 /* Don't give this error when parsing tentatively. This happens to
9075 work because we always parse this definitively once. */
9076 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9077 && type_uses_auto (type_specified))
9079 error ("invalid use of %<auto%> in conversion operator");
9080 return error_mark_node;
9083 return type_specified;
9086 /* Parse an (optional) conversion-declarator.
9088 conversion-declarator:
9089 ptr-operator conversion-declarator [opt]
9093 static cp_declarator *
9094 cp_parser_conversion_declarator_opt (cp_parser* parser)
9096 enum tree_code code;
9098 cp_cv_quals cv_quals;
9100 /* We don't know if there's a ptr-operator next, or not. */
9101 cp_parser_parse_tentatively (parser);
9102 /* Try the ptr-operator. */
9103 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9104 /* If it worked, look for more conversion-declarators. */
9105 if (cp_parser_parse_definitely (parser))
9107 cp_declarator *declarator;
9109 /* Parse another optional declarator. */
9110 declarator = cp_parser_conversion_declarator_opt (parser);
9112 return cp_parser_make_indirect_declarator
9113 (code, class_type, cv_quals, declarator);
9119 /* Parse an (optional) ctor-initializer.
9122 : mem-initializer-list
9124 Returns TRUE iff the ctor-initializer was actually present. */
9127 cp_parser_ctor_initializer_opt (cp_parser* parser)
9129 /* If the next token is not a `:', then there is no
9130 ctor-initializer. */
9131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9133 /* Do default initialization of any bases and members. */
9134 if (DECL_CONSTRUCTOR_P (current_function_decl))
9135 finish_mem_initializers (NULL_TREE);
9140 /* Consume the `:' token. */
9141 cp_lexer_consume_token (parser->lexer);
9142 /* And the mem-initializer-list. */
9143 cp_parser_mem_initializer_list (parser);
9148 /* Parse a mem-initializer-list.
9150 mem-initializer-list:
9151 mem-initializer ... [opt]
9152 mem-initializer ... [opt] , mem-initializer-list */
9155 cp_parser_mem_initializer_list (cp_parser* parser)
9157 tree mem_initializer_list = NULL_TREE;
9158 cp_token *token = cp_lexer_peek_token (parser->lexer);
9160 /* Let the semantic analysis code know that we are starting the
9161 mem-initializer-list. */
9162 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9163 error ("%Honly constructors take base initializers",
9166 /* Loop through the list. */
9169 tree mem_initializer;
9171 token = cp_lexer_peek_token (parser->lexer);
9172 /* Parse the mem-initializer. */
9173 mem_initializer = cp_parser_mem_initializer (parser);
9174 /* If the next token is a `...', we're expanding member initializers. */
9175 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9177 /* Consume the `...'. */
9178 cp_lexer_consume_token (parser->lexer);
9180 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9181 can be expanded but members cannot. */
9182 if (mem_initializer != error_mark_node
9183 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9185 error ("%Hcannot expand initializer for member %<%D%>",
9186 &token->location, TREE_PURPOSE (mem_initializer));
9187 mem_initializer = error_mark_node;
9190 /* Construct the pack expansion type. */
9191 if (mem_initializer != error_mark_node)
9192 mem_initializer = make_pack_expansion (mem_initializer);
9194 /* Add it to the list, unless it was erroneous. */
9195 if (mem_initializer != error_mark_node)
9197 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9198 mem_initializer_list = mem_initializer;
9200 /* If the next token is not a `,', we're done. */
9201 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9203 /* Consume the `,' token. */
9204 cp_lexer_consume_token (parser->lexer);
9207 /* Perform semantic analysis. */
9208 if (DECL_CONSTRUCTOR_P (current_function_decl))
9209 finish_mem_initializers (mem_initializer_list);
9212 /* Parse a mem-initializer.
9215 mem-initializer-id ( expression-list [opt] )
9216 mem-initializer-id braced-init-list
9221 ( expression-list [opt] )
9223 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9224 class) or FIELD_DECL (for a non-static data member) to initialize;
9225 the TREE_VALUE is the expression-list. An empty initialization
9226 list is represented by void_list_node. */
9229 cp_parser_mem_initializer (cp_parser* parser)
9231 tree mem_initializer_id;
9232 tree expression_list;
9234 cp_token *token = cp_lexer_peek_token (parser->lexer);
9236 /* Find out what is being initialized. */
9237 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9239 permerror (token->location,
9240 "anachronistic old-style base class initializer");
9241 mem_initializer_id = NULL_TREE;
9245 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9246 if (mem_initializer_id == error_mark_node)
9247 return mem_initializer_id;
9249 member = expand_member_init (mem_initializer_id);
9250 if (member && !DECL_P (member))
9251 in_base_initializer = 1;
9253 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9255 bool expr_non_constant_p;
9256 maybe_warn_cpp0x ("extended initializer lists");
9257 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9258 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9259 expression_list = build_tree_list (NULL_TREE, expression_list);
9263 = cp_parser_parenthesized_expression_list (parser, false,
9265 /*allow_expansion_p=*/true,
9266 /*non_constant_p=*/NULL);
9267 if (expression_list == error_mark_node)
9268 return error_mark_node;
9269 if (!expression_list)
9270 expression_list = void_type_node;
9272 in_base_initializer = 0;
9274 return member ? build_tree_list (member, expression_list) : error_mark_node;
9277 /* Parse a mem-initializer-id.
9280 :: [opt] nested-name-specifier [opt] class-name
9283 Returns a TYPE indicating the class to be initializer for the first
9284 production. Returns an IDENTIFIER_NODE indicating the data member
9285 to be initialized for the second production. */
9288 cp_parser_mem_initializer_id (cp_parser* parser)
9290 bool global_scope_p;
9291 bool nested_name_specifier_p;
9292 bool template_p = false;
9295 cp_token *token = cp_lexer_peek_token (parser->lexer);
9297 /* `typename' is not allowed in this context ([temp.res]). */
9298 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9300 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9301 "member initializer is implicitly a type)",
9303 cp_lexer_consume_token (parser->lexer);
9305 /* Look for the optional `::' operator. */
9307 = (cp_parser_global_scope_opt (parser,
9308 /*current_scope_valid_p=*/false)
9310 /* Look for the optional nested-name-specifier. The simplest way to
9315 The keyword `typename' is not permitted in a base-specifier or
9316 mem-initializer; in these contexts a qualified name that
9317 depends on a template-parameter is implicitly assumed to be a
9320 is to assume that we have seen the `typename' keyword at this
9322 nested_name_specifier_p
9323 = (cp_parser_nested_name_specifier_opt (parser,
9324 /*typename_keyword_p=*/true,
9325 /*check_dependency_p=*/true,
9327 /*is_declaration=*/true)
9329 if (nested_name_specifier_p)
9330 template_p = cp_parser_optional_template_keyword (parser);
9331 /* If there is a `::' operator or a nested-name-specifier, then we
9332 are definitely looking for a class-name. */
9333 if (global_scope_p || nested_name_specifier_p)
9334 return cp_parser_class_name (parser,
9335 /*typename_keyword_p=*/true,
9336 /*template_keyword_p=*/template_p,
9338 /*check_dependency_p=*/true,
9339 /*class_head_p=*/false,
9340 /*is_declaration=*/true);
9341 /* Otherwise, we could also be looking for an ordinary identifier. */
9342 cp_parser_parse_tentatively (parser);
9343 /* Try a class-name. */
9344 id = cp_parser_class_name (parser,
9345 /*typename_keyword_p=*/true,
9346 /*template_keyword_p=*/false,
9348 /*check_dependency_p=*/true,
9349 /*class_head_p=*/false,
9350 /*is_declaration=*/true);
9351 /* If we found one, we're done. */
9352 if (cp_parser_parse_definitely (parser))
9354 /* Otherwise, look for an ordinary identifier. */
9355 return cp_parser_identifier (parser);
9358 /* Overloading [gram.over] */
9360 /* Parse an operator-function-id.
9362 operator-function-id:
9365 Returns an IDENTIFIER_NODE for the operator which is a
9366 human-readable spelling of the identifier, e.g., `operator +'. */
9369 cp_parser_operator_function_id (cp_parser* parser)
9371 /* Look for the `operator' keyword. */
9372 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9373 return error_mark_node;
9374 /* And then the name of the operator itself. */
9375 return cp_parser_operator (parser);
9378 /* Parse an operator.
9381 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9382 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9383 || ++ -- , ->* -> () []
9390 Returns an IDENTIFIER_NODE for the operator which is a
9391 human-readable spelling of the identifier, e.g., `operator +'. */
9394 cp_parser_operator (cp_parser* parser)
9396 tree id = NULL_TREE;
9399 /* Peek at the next token. */
9400 token = cp_lexer_peek_token (parser->lexer);
9401 /* Figure out which operator we have. */
9402 switch (token->type)
9408 /* The keyword should be either `new' or `delete'. */
9409 if (token->keyword == RID_NEW)
9411 else if (token->keyword == RID_DELETE)
9416 /* Consume the `new' or `delete' token. */
9417 cp_lexer_consume_token (parser->lexer);
9419 /* Peek at the next token. */
9420 token = cp_lexer_peek_token (parser->lexer);
9421 /* If it's a `[' token then this is the array variant of the
9423 if (token->type == CPP_OPEN_SQUARE)
9425 /* Consume the `[' token. */
9426 cp_lexer_consume_token (parser->lexer);
9427 /* Look for the `]' token. */
9428 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9429 id = ansi_opname (op == NEW_EXPR
9430 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9432 /* Otherwise, we have the non-array variant. */
9434 id = ansi_opname (op);
9440 id = ansi_opname (PLUS_EXPR);
9444 id = ansi_opname (MINUS_EXPR);
9448 id = ansi_opname (MULT_EXPR);
9452 id = ansi_opname (TRUNC_DIV_EXPR);
9456 id = ansi_opname (TRUNC_MOD_EXPR);
9460 id = ansi_opname (BIT_XOR_EXPR);
9464 id = ansi_opname (BIT_AND_EXPR);
9468 id = ansi_opname (BIT_IOR_EXPR);
9472 id = ansi_opname (BIT_NOT_EXPR);
9476 id = ansi_opname (TRUTH_NOT_EXPR);
9480 id = ansi_assopname (NOP_EXPR);
9484 id = ansi_opname (LT_EXPR);
9488 id = ansi_opname (GT_EXPR);
9492 id = ansi_assopname (PLUS_EXPR);
9496 id = ansi_assopname (MINUS_EXPR);
9500 id = ansi_assopname (MULT_EXPR);
9504 id = ansi_assopname (TRUNC_DIV_EXPR);
9508 id = ansi_assopname (TRUNC_MOD_EXPR);
9512 id = ansi_assopname (BIT_XOR_EXPR);
9516 id = ansi_assopname (BIT_AND_EXPR);
9520 id = ansi_assopname (BIT_IOR_EXPR);
9524 id = ansi_opname (LSHIFT_EXPR);
9528 id = ansi_opname (RSHIFT_EXPR);
9532 id = ansi_assopname (LSHIFT_EXPR);
9536 id = ansi_assopname (RSHIFT_EXPR);
9540 id = ansi_opname (EQ_EXPR);
9544 id = ansi_opname (NE_EXPR);
9548 id = ansi_opname (LE_EXPR);
9551 case CPP_GREATER_EQ:
9552 id = ansi_opname (GE_EXPR);
9556 id = ansi_opname (TRUTH_ANDIF_EXPR);
9560 id = ansi_opname (TRUTH_ORIF_EXPR);
9564 id = ansi_opname (POSTINCREMENT_EXPR);
9567 case CPP_MINUS_MINUS:
9568 id = ansi_opname (PREDECREMENT_EXPR);
9572 id = ansi_opname (COMPOUND_EXPR);
9575 case CPP_DEREF_STAR:
9576 id = ansi_opname (MEMBER_REF);
9580 id = ansi_opname (COMPONENT_REF);
9583 case CPP_OPEN_PAREN:
9584 /* Consume the `('. */
9585 cp_lexer_consume_token (parser->lexer);
9586 /* Look for the matching `)'. */
9587 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9588 return ansi_opname (CALL_EXPR);
9590 case CPP_OPEN_SQUARE:
9591 /* Consume the `['. */
9592 cp_lexer_consume_token (parser->lexer);
9593 /* Look for the matching `]'. */
9594 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9595 return ansi_opname (ARRAY_REF);
9598 /* Anything else is an error. */
9602 /* If we have selected an identifier, we need to consume the
9605 cp_lexer_consume_token (parser->lexer);
9606 /* Otherwise, no valid operator name was present. */
9609 cp_parser_error (parser, "expected operator");
9610 id = error_mark_node;
9616 /* Parse a template-declaration.
9618 template-declaration:
9619 export [opt] template < template-parameter-list > declaration
9621 If MEMBER_P is TRUE, this template-declaration occurs within a
9624 The grammar rule given by the standard isn't correct. What
9627 template-declaration:
9628 export [opt] template-parameter-list-seq
9629 decl-specifier-seq [opt] init-declarator [opt] ;
9630 export [opt] template-parameter-list-seq
9633 template-parameter-list-seq:
9634 template-parameter-list-seq [opt]
9635 template < template-parameter-list > */
9638 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9640 /* Check for `export'. */
9641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9643 /* Consume the `export' token. */
9644 cp_lexer_consume_token (parser->lexer);
9645 /* Warn that we do not support `export'. */
9646 warning (0, "keyword %<export%> not implemented, and will be ignored");
9649 cp_parser_template_declaration_after_export (parser, member_p);
9652 /* Parse a template-parameter-list.
9654 template-parameter-list:
9656 template-parameter-list , template-parameter
9658 Returns a TREE_LIST. Each node represents a template parameter.
9659 The nodes are connected via their TREE_CHAINs. */
9662 cp_parser_template_parameter_list (cp_parser* parser)
9664 tree parameter_list = NULL_TREE;
9666 begin_template_parm_list ();
9671 bool is_parameter_pack;
9673 /* Parse the template-parameter. */
9674 parameter = cp_parser_template_parameter (parser,
9676 &is_parameter_pack);
9677 /* Add it to the list. */
9678 if (parameter != error_mark_node)
9679 parameter_list = process_template_parm (parameter_list,
9685 tree err_parm = build_tree_list (parameter, parameter);
9686 TREE_VALUE (err_parm) = error_mark_node;
9687 parameter_list = chainon (parameter_list, err_parm);
9690 /* If the next token is not a `,', we're done. */
9691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9693 /* Otherwise, consume the `,' token. */
9694 cp_lexer_consume_token (parser->lexer);
9697 return end_template_parm_list (parameter_list);
9700 /* Parse a template-parameter.
9704 parameter-declaration
9706 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9707 the parameter. The TREE_PURPOSE is the default value, if any.
9708 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9709 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9710 set to true iff this parameter is a parameter pack. */
9713 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9714 bool *is_parameter_pack)
9717 cp_parameter_declarator *parameter_declarator;
9718 cp_declarator *id_declarator;
9721 /* Assume it is a type parameter or a template parameter. */
9722 *is_non_type = false;
9723 /* Assume it not a parameter pack. */
9724 *is_parameter_pack = false;
9725 /* Peek at the next token. */
9726 token = cp_lexer_peek_token (parser->lexer);
9727 /* If it is `class' or `template', we have a type-parameter. */
9728 if (token->keyword == RID_TEMPLATE)
9729 return cp_parser_type_parameter (parser, is_parameter_pack);
9730 /* If it is `class' or `typename' we do not know yet whether it is a
9731 type parameter or a non-type parameter. Consider:
9733 template <typename T, typename T::X X> ...
9737 template <class C, class D*> ...
9739 Here, the first parameter is a type parameter, and the second is
9740 a non-type parameter. We can tell by looking at the token after
9741 the identifier -- if it is a `,', `=', or `>' then we have a type
9743 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9745 /* Peek at the token after `class' or `typename'. */
9746 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9747 /* If it's an ellipsis, we have a template type parameter
9749 if (token->type == CPP_ELLIPSIS)
9750 return cp_parser_type_parameter (parser, is_parameter_pack);
9751 /* If it's an identifier, skip it. */
9752 if (token->type == CPP_NAME)
9753 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9754 /* Now, see if the token looks like the end of a template
9756 if (token->type == CPP_COMMA
9757 || token->type == CPP_EQ
9758 || token->type == CPP_GREATER)
9759 return cp_parser_type_parameter (parser, is_parameter_pack);
9762 /* Otherwise, it is a non-type parameter.
9766 When parsing a default template-argument for a non-type
9767 template-parameter, the first non-nested `>' is taken as the end
9768 of the template parameter-list rather than a greater-than
9770 *is_non_type = true;
9771 parameter_declarator
9772 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9773 /*parenthesized_p=*/NULL);
9775 /* If the parameter declaration is marked as a parameter pack, set
9776 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9777 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9779 if (parameter_declarator
9780 && parameter_declarator->declarator
9781 && parameter_declarator->declarator->parameter_pack_p)
9783 *is_parameter_pack = true;
9784 parameter_declarator->declarator->parameter_pack_p = false;
9787 /* If the next token is an ellipsis, and we don't already have it
9788 marked as a parameter pack, then we have a parameter pack (that
9789 has no declarator). */
9790 if (!*is_parameter_pack
9791 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9792 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9794 /* Consume the `...'. */
9795 cp_lexer_consume_token (parser->lexer);
9796 maybe_warn_variadic_templates ();
9798 *is_parameter_pack = true;
9800 /* We might end up with a pack expansion as the type of the non-type
9801 template parameter, in which case this is a non-type template
9803 else if (parameter_declarator
9804 && parameter_declarator->decl_specifiers.type
9805 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9807 *is_parameter_pack = true;
9808 parameter_declarator->decl_specifiers.type =
9809 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9812 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9814 /* Parameter packs cannot have default arguments. However, a
9815 user may try to do so, so we'll parse them and give an
9816 appropriate diagnostic here. */
9818 /* Consume the `='. */
9819 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9820 cp_lexer_consume_token (parser->lexer);
9822 /* Find the name of the parameter pack. */
9823 id_declarator = parameter_declarator->declarator;
9824 while (id_declarator && id_declarator->kind != cdk_id)
9825 id_declarator = id_declarator->declarator;
9827 if (id_declarator && id_declarator->kind == cdk_id)
9828 error ("%Htemplate parameter pack %qD cannot have a default argument",
9829 &start_token->location, id_declarator->u.id.unqualified_name);
9831 error ("%Htemplate parameter pack cannot have a default argument",
9832 &start_token->location);
9834 /* Parse the default argument, but throw away the result. */
9835 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9838 parm = grokdeclarator (parameter_declarator->declarator,
9839 ¶meter_declarator->decl_specifiers,
9840 PARM, /*initialized=*/0,
9842 if (parm == error_mark_node)
9843 return error_mark_node;
9845 return build_tree_list (parameter_declarator->default_argument, parm);
9848 /* Parse a type-parameter.
9851 class identifier [opt]
9852 class identifier [opt] = type-id
9853 typename identifier [opt]
9854 typename identifier [opt] = type-id
9855 template < template-parameter-list > class identifier [opt]
9856 template < template-parameter-list > class identifier [opt]
9859 GNU Extension (variadic templates):
9862 class ... identifier [opt]
9863 typename ... identifier [opt]
9865 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9866 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9867 the declaration of the parameter.
9869 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9872 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9877 /* Look for a keyword to tell us what kind of parameter this is. */
9878 token = cp_parser_require (parser, CPP_KEYWORD,
9879 "%<class%>, %<typename%>, or %<template%>");
9881 return error_mark_node;
9883 switch (token->keyword)
9889 tree default_argument;
9891 /* If the next token is an ellipsis, we have a template
9893 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9895 /* Consume the `...' token. */
9896 cp_lexer_consume_token (parser->lexer);
9897 maybe_warn_variadic_templates ();
9899 *is_parameter_pack = true;
9902 /* If the next token is an identifier, then it names the
9904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9905 identifier = cp_parser_identifier (parser);
9907 identifier = NULL_TREE;
9909 /* Create the parameter. */
9910 parameter = finish_template_type_parm (class_type_node, identifier);
9912 /* If the next token is an `=', we have a default argument. */
9913 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9915 /* Consume the `=' token. */
9916 cp_lexer_consume_token (parser->lexer);
9917 /* Parse the default-argument. */
9918 push_deferring_access_checks (dk_no_deferred);
9919 default_argument = cp_parser_type_id (parser);
9921 /* Template parameter packs cannot have default
9923 if (*is_parameter_pack)
9926 error ("%Htemplate parameter pack %qD cannot have a "
9927 "default argument", &token->location, identifier);
9929 error ("%Htemplate parameter packs cannot have "
9930 "default arguments", &token->location);
9931 default_argument = NULL_TREE;
9933 pop_deferring_access_checks ();
9936 default_argument = NULL_TREE;
9938 /* Create the combined representation of the parameter and the
9939 default argument. */
9940 parameter = build_tree_list (default_argument, parameter);
9946 tree parameter_list;
9948 tree default_argument;
9950 /* Look for the `<'. */
9951 cp_parser_require (parser, CPP_LESS, "%<<%>");
9952 /* Parse the template-parameter-list. */
9953 parameter_list = cp_parser_template_parameter_list (parser);
9954 /* Look for the `>'. */
9955 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9956 /* Look for the `class' keyword. */
9957 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9958 /* If the next token is an ellipsis, we have a template
9960 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9962 /* Consume the `...' token. */
9963 cp_lexer_consume_token (parser->lexer);
9964 maybe_warn_variadic_templates ();
9966 *is_parameter_pack = true;
9968 /* If the next token is an `=', then there is a
9969 default-argument. If the next token is a `>', we are at
9970 the end of the parameter-list. If the next token is a `,',
9971 then we are at the end of this parameter. */
9972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9973 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9974 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9976 identifier = cp_parser_identifier (parser);
9977 /* Treat invalid names as if the parameter were nameless. */
9978 if (identifier == error_mark_node)
9979 identifier = NULL_TREE;
9982 identifier = NULL_TREE;
9984 /* Create the template parameter. */
9985 parameter = finish_template_template_parm (class_type_node,
9988 /* If the next token is an `=', then there is a
9989 default-argument. */
9990 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9994 /* Consume the `='. */
9995 cp_lexer_consume_token (parser->lexer);
9996 /* Parse the id-expression. */
9997 push_deferring_access_checks (dk_no_deferred);
9998 /* save token before parsing the id-expression, for error
10000 token = cp_lexer_peek_token (parser->lexer);
10002 = cp_parser_id_expression (parser,
10003 /*template_keyword_p=*/false,
10004 /*check_dependency_p=*/true,
10005 /*template_p=*/&is_template,
10006 /*declarator_p=*/false,
10007 /*optional_p=*/false);
10008 if (TREE_CODE (default_argument) == TYPE_DECL)
10009 /* If the id-expression was a template-id that refers to
10010 a template-class, we already have the declaration here,
10011 so no further lookup is needed. */
10014 /* Look up the name. */
10016 = cp_parser_lookup_name (parser, default_argument,
10018 /*is_template=*/is_template,
10019 /*is_namespace=*/false,
10020 /*check_dependency=*/true,
10021 /*ambiguous_decls=*/NULL,
10023 /* See if the default argument is valid. */
10025 = check_template_template_default_arg (default_argument);
10027 /* Template parameter packs cannot have default
10029 if (*is_parameter_pack)
10032 error ("%Htemplate parameter pack %qD cannot "
10033 "have a default argument",
10034 &token->location, identifier);
10036 error ("%Htemplate parameter packs cannot "
10037 "have default arguments",
10039 default_argument = NULL_TREE;
10041 pop_deferring_access_checks ();
10044 default_argument = NULL_TREE;
10046 /* Create the combined representation of the parameter and the
10047 default argument. */
10048 parameter = build_tree_list (default_argument, parameter);
10053 gcc_unreachable ();
10060 /* Parse a template-id.
10063 template-name < template-argument-list [opt] >
10065 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10066 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10067 returned. Otherwise, if the template-name names a function, or set
10068 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10069 names a class, returns a TYPE_DECL for the specialization.
10071 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10072 uninstantiated templates. */
10075 cp_parser_template_id (cp_parser *parser,
10076 bool template_keyword_p,
10077 bool check_dependency_p,
10078 bool is_declaration)
10084 cp_token_position start_of_id = 0;
10085 deferred_access_check *chk;
10086 VEC (deferred_access_check,gc) *access_check;
10087 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10088 bool is_identifier;
10090 /* If the next token corresponds to a template-id, there is no need
10092 next_token = cp_lexer_peek_token (parser->lexer);
10093 if (next_token->type == CPP_TEMPLATE_ID)
10095 struct tree_check *check_value;
10097 /* Get the stored value. */
10098 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10099 /* Perform any access checks that were deferred. */
10100 access_check = check_value->checks;
10104 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10107 perform_or_defer_access_check (chk->binfo,
10112 /* Return the stored value. */
10113 return check_value->value;
10116 /* Avoid performing name lookup if there is no possibility of
10117 finding a template-id. */
10118 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10119 || (next_token->type == CPP_NAME
10120 && !cp_parser_nth_token_starts_template_argument_list_p
10123 cp_parser_error (parser, "expected template-id");
10124 return error_mark_node;
10127 /* Remember where the template-id starts. */
10128 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10129 start_of_id = cp_lexer_token_position (parser->lexer, false);
10131 push_deferring_access_checks (dk_deferred);
10133 /* Parse the template-name. */
10134 is_identifier = false;
10135 token = cp_lexer_peek_token (parser->lexer);
10136 templ = cp_parser_template_name (parser, template_keyword_p,
10137 check_dependency_p,
10140 if (templ == error_mark_node || is_identifier)
10142 pop_deferring_access_checks ();
10146 /* If we find the sequence `[:' after a template-name, it's probably
10147 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10148 parse correctly the argument list. */
10149 next_token = cp_lexer_peek_token (parser->lexer);
10150 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10151 if (next_token->type == CPP_OPEN_SQUARE
10152 && next_token->flags & DIGRAPH
10153 && next_token_2->type == CPP_COLON
10154 && !(next_token_2->flags & PREV_WHITE))
10156 cp_parser_parse_tentatively (parser);
10157 /* Change `:' into `::'. */
10158 next_token_2->type = CPP_SCOPE;
10159 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10161 cp_lexer_consume_token (parser->lexer);
10163 /* Parse the arguments. */
10164 arguments = cp_parser_enclosed_template_argument_list (parser);
10165 if (!cp_parser_parse_definitely (parser))
10167 /* If we couldn't parse an argument list, then we revert our changes
10168 and return simply an error. Maybe this is not a template-id
10170 next_token_2->type = CPP_COLON;
10171 cp_parser_error (parser, "expected %<<%>");
10172 pop_deferring_access_checks ();
10173 return error_mark_node;
10175 /* Otherwise, emit an error about the invalid digraph, but continue
10176 parsing because we got our argument list. */
10177 if (permerror (next_token->location,
10178 "%<<::%> cannot begin a template-argument list"))
10180 static bool hint = false;
10181 inform (next_token->location,
10182 "%<<:%> is an alternate spelling for %<[%>."
10183 " Insert whitespace between %<<%> and %<::%>");
10184 if (!hint && !flag_permissive)
10186 inform (next_token->location, "(if you use %<-fpermissive%>"
10187 " G++ will accept your code)");
10194 /* Look for the `<' that starts the template-argument-list. */
10195 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10197 pop_deferring_access_checks ();
10198 return error_mark_node;
10200 /* Parse the arguments. */
10201 arguments = cp_parser_enclosed_template_argument_list (parser);
10204 /* Build a representation of the specialization. */
10205 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10206 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10207 else if (DECL_CLASS_TEMPLATE_P (templ)
10208 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10210 bool entering_scope;
10211 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10212 template (rather than some instantiation thereof) only if
10213 is not nested within some other construct. For example, in
10214 "template <typename T> void f(T) { A<T>::", A<T> is just an
10215 instantiation of A. */
10216 entering_scope = (template_parm_scope_p ()
10217 && cp_lexer_next_token_is (parser->lexer,
10220 = finish_template_type (templ, arguments, entering_scope);
10224 /* If it's not a class-template or a template-template, it should be
10225 a function-template. */
10226 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10227 || TREE_CODE (templ) == OVERLOAD
10228 || BASELINK_P (templ)));
10230 template_id = lookup_template_function (templ, arguments);
10233 /* If parsing tentatively, replace the sequence of tokens that makes
10234 up the template-id with a CPP_TEMPLATE_ID token. That way,
10235 should we re-parse the token stream, we will not have to repeat
10236 the effort required to do the parse, nor will we issue duplicate
10237 error messages about problems during instantiation of the
10241 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10243 /* Reset the contents of the START_OF_ID token. */
10244 token->type = CPP_TEMPLATE_ID;
10245 /* Retrieve any deferred checks. Do not pop this access checks yet
10246 so the memory will not be reclaimed during token replacing below. */
10247 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10248 token->u.tree_check_value->value = template_id;
10249 token->u.tree_check_value->checks = get_deferred_access_checks ();
10250 token->keyword = RID_MAX;
10252 /* Purge all subsequent tokens. */
10253 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10255 /* ??? Can we actually assume that, if template_id ==
10256 error_mark_node, we will have issued a diagnostic to the
10257 user, as opposed to simply marking the tentative parse as
10259 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10260 error ("%Hparse error in template argument list",
10264 pop_deferring_access_checks ();
10265 return template_id;
10268 /* Parse a template-name.
10273 The standard should actually say:
10277 operator-function-id
10279 A defect report has been filed about this issue.
10281 A conversion-function-id cannot be a template name because they cannot
10282 be part of a template-id. In fact, looking at this code:
10284 a.operator K<int>()
10286 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10287 It is impossible to call a templated conversion-function-id with an
10288 explicit argument list, since the only allowed template parameter is
10289 the type to which it is converting.
10291 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10292 `template' keyword, in a construction like:
10296 In that case `f' is taken to be a template-name, even though there
10297 is no way of knowing for sure.
10299 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10300 name refers to a set of overloaded functions, at least one of which
10301 is a template, or an IDENTIFIER_NODE with the name of the template,
10302 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10303 names are looked up inside uninstantiated templates. */
10306 cp_parser_template_name (cp_parser* parser,
10307 bool template_keyword_p,
10308 bool check_dependency_p,
10309 bool is_declaration,
10310 bool *is_identifier)
10315 cp_token *token = cp_lexer_peek_token (parser->lexer);
10317 /* If the next token is `operator', then we have either an
10318 operator-function-id or a conversion-function-id. */
10319 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10321 /* We don't know whether we're looking at an
10322 operator-function-id or a conversion-function-id. */
10323 cp_parser_parse_tentatively (parser);
10324 /* Try an operator-function-id. */
10325 identifier = cp_parser_operator_function_id (parser);
10326 /* If that didn't work, try a conversion-function-id. */
10327 if (!cp_parser_parse_definitely (parser))
10329 cp_parser_error (parser, "expected template-name");
10330 return error_mark_node;
10333 /* Look for the identifier. */
10335 identifier = cp_parser_identifier (parser);
10337 /* If we didn't find an identifier, we don't have a template-id. */
10338 if (identifier == error_mark_node)
10339 return error_mark_node;
10341 /* If the name immediately followed the `template' keyword, then it
10342 is a template-name. However, if the next token is not `<', then
10343 we do not treat it as a template-name, since it is not being used
10344 as part of a template-id. This enables us to handle constructs
10347 template <typename T> struct S { S(); };
10348 template <typename T> S<T>::S();
10350 correctly. We would treat `S' as a template -- if it were `S<T>'
10351 -- but we do not if there is no `<'. */
10353 if (processing_template_decl
10354 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10356 /* In a declaration, in a dependent context, we pretend that the
10357 "template" keyword was present in order to improve error
10358 recovery. For example, given:
10360 template <typename T> void f(T::X<int>);
10362 we want to treat "X<int>" as a template-id. */
10364 && !template_keyword_p
10365 && parser->scope && TYPE_P (parser->scope)
10366 && check_dependency_p
10367 && dependent_scope_p (parser->scope)
10368 /* Do not do this for dtors (or ctors), since they never
10369 need the template keyword before their name. */
10370 && !constructor_name_p (identifier, parser->scope))
10372 cp_token_position start = 0;
10374 /* Explain what went wrong. */
10375 error ("%Hnon-template %qD used as template",
10376 &token->location, identifier);
10377 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10378 parser->scope, identifier);
10379 /* If parsing tentatively, find the location of the "<" token. */
10380 if (cp_parser_simulate_error (parser))
10381 start = cp_lexer_token_position (parser->lexer, true);
10382 /* Parse the template arguments so that we can issue error
10383 messages about them. */
10384 cp_lexer_consume_token (parser->lexer);
10385 cp_parser_enclosed_template_argument_list (parser);
10386 /* Skip tokens until we find a good place from which to
10387 continue parsing. */
10388 cp_parser_skip_to_closing_parenthesis (parser,
10389 /*recovering=*/true,
10391 /*consume_paren=*/false);
10392 /* If parsing tentatively, permanently remove the
10393 template argument list. That will prevent duplicate
10394 error messages from being issued about the missing
10395 "template" keyword. */
10397 cp_lexer_purge_tokens_after (parser->lexer, start);
10399 *is_identifier = true;
10403 /* If the "template" keyword is present, then there is generally
10404 no point in doing name-lookup, so we just return IDENTIFIER.
10405 But, if the qualifying scope is non-dependent then we can
10406 (and must) do name-lookup normally. */
10407 if (template_keyword_p
10409 || (TYPE_P (parser->scope)
10410 && dependent_type_p (parser->scope))))
10414 /* Look up the name. */
10415 decl = cp_parser_lookup_name (parser, identifier,
10417 /*is_template=*/false,
10418 /*is_namespace=*/false,
10419 check_dependency_p,
10420 /*ambiguous_decls=*/NULL,
10422 decl = maybe_get_template_decl_from_type_decl (decl);
10424 /* If DECL is a template, then the name was a template-name. */
10425 if (TREE_CODE (decl) == TEMPLATE_DECL)
10429 tree fn = NULL_TREE;
10431 /* The standard does not explicitly indicate whether a name that
10432 names a set of overloaded declarations, some of which are
10433 templates, is a template-name. However, such a name should
10434 be a template-name; otherwise, there is no way to form a
10435 template-id for the overloaded templates. */
10436 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10437 if (TREE_CODE (fns) == OVERLOAD)
10438 for (fn = fns; fn; fn = OVL_NEXT (fn))
10439 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10444 /* The name does not name a template. */
10445 cp_parser_error (parser, "expected template-name");
10446 return error_mark_node;
10450 /* If DECL is dependent, and refers to a function, then just return
10451 its name; we will look it up again during template instantiation. */
10452 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10454 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10455 if (TYPE_P (scope) && dependent_type_p (scope))
10462 /* Parse a template-argument-list.
10464 template-argument-list:
10465 template-argument ... [opt]
10466 template-argument-list , template-argument ... [opt]
10468 Returns a TREE_VEC containing the arguments. */
10471 cp_parser_template_argument_list (cp_parser* parser)
10473 tree fixed_args[10];
10474 unsigned n_args = 0;
10475 unsigned alloced = 10;
10476 tree *arg_ary = fixed_args;
10478 bool saved_in_template_argument_list_p;
10480 bool saved_non_ice_p;
10482 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10483 parser->in_template_argument_list_p = true;
10484 /* Even if the template-id appears in an integral
10485 constant-expression, the contents of the argument list do
10487 saved_ice_p = parser->integral_constant_expression_p;
10488 parser->integral_constant_expression_p = false;
10489 saved_non_ice_p = parser->non_integral_constant_expression_p;
10490 parser->non_integral_constant_expression_p = false;
10491 /* Parse the arguments. */
10497 /* Consume the comma. */
10498 cp_lexer_consume_token (parser->lexer);
10500 /* Parse the template-argument. */
10501 argument = cp_parser_template_argument (parser);
10503 /* If the next token is an ellipsis, we're expanding a template
10505 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10507 if (argument == error_mark_node)
10509 cp_token *token = cp_lexer_peek_token (parser->lexer);
10510 error ("%Hexpected parameter pack before %<...%>",
10513 /* Consume the `...' token. */
10514 cp_lexer_consume_token (parser->lexer);
10516 /* Make the argument into a TYPE_PACK_EXPANSION or
10517 EXPR_PACK_EXPANSION. */
10518 argument = make_pack_expansion (argument);
10521 if (n_args == alloced)
10525 if (arg_ary == fixed_args)
10527 arg_ary = XNEWVEC (tree, alloced);
10528 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10531 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10533 arg_ary[n_args++] = argument;
10535 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10537 vec = make_tree_vec (n_args);
10540 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10542 if (arg_ary != fixed_args)
10544 parser->non_integral_constant_expression_p = saved_non_ice_p;
10545 parser->integral_constant_expression_p = saved_ice_p;
10546 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10550 /* Parse a template-argument.
10553 assignment-expression
10557 The representation is that of an assignment-expression, type-id, or
10558 id-expression -- except that the qualified id-expression is
10559 evaluated, so that the value returned is either a DECL or an
10562 Although the standard says "assignment-expression", it forbids
10563 throw-expressions or assignments in the template argument.
10564 Therefore, we use "conditional-expression" instead. */
10567 cp_parser_template_argument (cp_parser* parser)
10572 bool maybe_type_id = false;
10573 cp_token *token = NULL, *argument_start_token = NULL;
10576 /* There's really no way to know what we're looking at, so we just
10577 try each alternative in order.
10581 In a template-argument, an ambiguity between a type-id and an
10582 expression is resolved to a type-id, regardless of the form of
10583 the corresponding template-parameter.
10585 Therefore, we try a type-id first. */
10586 cp_parser_parse_tentatively (parser);
10587 argument = cp_parser_template_type_arg (parser);
10588 /* If there was no error parsing the type-id but the next token is a
10589 '>>', our behavior depends on which dialect of C++ we're
10590 parsing. In C++98, we probably found a typo for '> >'. But there
10591 are type-id which are also valid expressions. For instance:
10593 struct X { int operator >> (int); };
10594 template <int V> struct Foo {};
10597 Here 'X()' is a valid type-id of a function type, but the user just
10598 wanted to write the expression "X() >> 5". Thus, we remember that we
10599 found a valid type-id, but we still try to parse the argument as an
10600 expression to see what happens.
10602 In C++0x, the '>>' will be considered two separate '>'
10604 if (!cp_parser_error_occurred (parser)
10605 && cxx_dialect == cxx98
10606 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10608 maybe_type_id = true;
10609 cp_parser_abort_tentative_parse (parser);
10613 /* If the next token isn't a `,' or a `>', then this argument wasn't
10614 really finished. This means that the argument is not a valid
10616 if (!cp_parser_next_token_ends_template_argument_p (parser))
10617 cp_parser_error (parser, "expected template-argument");
10618 /* If that worked, we're done. */
10619 if (cp_parser_parse_definitely (parser))
10622 /* We're still not sure what the argument will be. */
10623 cp_parser_parse_tentatively (parser);
10624 /* Try a template. */
10625 argument_start_token = cp_lexer_peek_token (parser->lexer);
10626 argument = cp_parser_id_expression (parser,
10627 /*template_keyword_p=*/false,
10628 /*check_dependency_p=*/true,
10630 /*declarator_p=*/false,
10631 /*optional_p=*/false);
10632 /* If the next token isn't a `,' or a `>', then this argument wasn't
10633 really finished. */
10634 if (!cp_parser_next_token_ends_template_argument_p (parser))
10635 cp_parser_error (parser, "expected template-argument");
10636 if (!cp_parser_error_occurred (parser))
10638 /* Figure out what is being referred to. If the id-expression
10639 was for a class template specialization, then we will have a
10640 TYPE_DECL at this point. There is no need to do name lookup
10641 at this point in that case. */
10642 if (TREE_CODE (argument) != TYPE_DECL)
10643 argument = cp_parser_lookup_name (parser, argument,
10645 /*is_template=*/template_p,
10646 /*is_namespace=*/false,
10647 /*check_dependency=*/true,
10648 /*ambiguous_decls=*/NULL,
10649 argument_start_token->location);
10650 if (TREE_CODE (argument) != TEMPLATE_DECL
10651 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10652 cp_parser_error (parser, "expected template-name");
10654 if (cp_parser_parse_definitely (parser))
10656 /* It must be a non-type argument. There permitted cases are given
10657 in [temp.arg.nontype]:
10659 -- an integral constant-expression of integral or enumeration
10662 -- the name of a non-type template-parameter; or
10664 -- the name of an object or function with external linkage...
10666 -- the address of an object or function with external linkage...
10668 -- a pointer to member... */
10669 /* Look for a non-type template parameter. */
10670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10672 cp_parser_parse_tentatively (parser);
10673 argument = cp_parser_primary_expression (parser,
10674 /*address_p=*/false,
10676 /*template_arg_p=*/true,
10678 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10679 || !cp_parser_next_token_ends_template_argument_p (parser))
10680 cp_parser_simulate_error (parser);
10681 if (cp_parser_parse_definitely (parser))
10685 /* If the next token is "&", the argument must be the address of an
10686 object or function with external linkage. */
10687 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10689 cp_lexer_consume_token (parser->lexer);
10690 /* See if we might have an id-expression. */
10691 token = cp_lexer_peek_token (parser->lexer);
10692 if (token->type == CPP_NAME
10693 || token->keyword == RID_OPERATOR
10694 || token->type == CPP_SCOPE
10695 || token->type == CPP_TEMPLATE_ID
10696 || token->type == CPP_NESTED_NAME_SPECIFIER)
10698 cp_parser_parse_tentatively (parser);
10699 argument = cp_parser_primary_expression (parser,
10702 /*template_arg_p=*/true,
10704 if (cp_parser_error_occurred (parser)
10705 || !cp_parser_next_token_ends_template_argument_p (parser))
10706 cp_parser_abort_tentative_parse (parser);
10711 if (TREE_CODE (argument) == INDIRECT_REF)
10713 gcc_assert (REFERENCE_REF_P (argument));
10714 argument = TREE_OPERAND (argument, 0);
10717 /* If we're in a template, we represent a qualified-id referring
10718 to a static data member as a SCOPE_REF even if the scope isn't
10719 dependent so that we can check access control later. */
10721 if (TREE_CODE (probe) == SCOPE_REF)
10722 probe = TREE_OPERAND (probe, 1);
10723 if (TREE_CODE (probe) == VAR_DECL)
10725 /* A variable without external linkage might still be a
10726 valid constant-expression, so no error is issued here
10727 if the external-linkage check fails. */
10728 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
10729 cp_parser_simulate_error (parser);
10731 else if (is_overloaded_fn (argument))
10732 /* All overloaded functions are allowed; if the external
10733 linkage test does not pass, an error will be issued
10737 && (TREE_CODE (argument) == OFFSET_REF
10738 || TREE_CODE (argument) == SCOPE_REF))
10739 /* A pointer-to-member. */
10741 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10744 cp_parser_simulate_error (parser);
10746 if (cp_parser_parse_definitely (parser))
10749 argument = build_x_unary_op (ADDR_EXPR, argument,
10750 tf_warning_or_error);
10755 /* If the argument started with "&", there are no other valid
10756 alternatives at this point. */
10759 cp_parser_error (parser, "invalid non-type template argument");
10760 return error_mark_node;
10763 /* If the argument wasn't successfully parsed as a type-id followed
10764 by '>>', the argument can only be a constant expression now.
10765 Otherwise, we try parsing the constant-expression tentatively,
10766 because the argument could really be a type-id. */
10768 cp_parser_parse_tentatively (parser);
10769 argument = cp_parser_constant_expression (parser,
10770 /*allow_non_constant_p=*/false,
10771 /*non_constant_p=*/NULL);
10772 argument = fold_non_dependent_expr (argument);
10773 if (!maybe_type_id)
10775 if (!cp_parser_next_token_ends_template_argument_p (parser))
10776 cp_parser_error (parser, "expected template-argument");
10777 if (cp_parser_parse_definitely (parser))
10779 /* We did our best to parse the argument as a non type-id, but that
10780 was the only alternative that matched (albeit with a '>' after
10781 it). We can assume it's just a typo from the user, and a
10782 diagnostic will then be issued. */
10783 return cp_parser_template_type_arg (parser);
10786 /* Parse an explicit-instantiation.
10788 explicit-instantiation:
10789 template declaration
10791 Although the standard says `declaration', what it really means is:
10793 explicit-instantiation:
10794 template decl-specifier-seq [opt] declarator [opt] ;
10796 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10797 supposed to be allowed. A defect report has been filed about this
10802 explicit-instantiation:
10803 storage-class-specifier template
10804 decl-specifier-seq [opt] declarator [opt] ;
10805 function-specifier template
10806 decl-specifier-seq [opt] declarator [opt] ; */
10809 cp_parser_explicit_instantiation (cp_parser* parser)
10811 int declares_class_or_enum;
10812 cp_decl_specifier_seq decl_specifiers;
10813 tree extension_specifier = NULL_TREE;
10816 /* Look for an (optional) storage-class-specifier or
10817 function-specifier. */
10818 if (cp_parser_allow_gnu_extensions_p (parser))
10820 extension_specifier
10821 = cp_parser_storage_class_specifier_opt (parser);
10822 if (!extension_specifier)
10823 extension_specifier
10824 = cp_parser_function_specifier_opt (parser,
10825 /*decl_specs=*/NULL);
10828 /* Look for the `template' keyword. */
10829 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10830 /* Let the front end know that we are processing an explicit
10832 begin_explicit_instantiation ();
10833 /* [temp.explicit] says that we are supposed to ignore access
10834 control while processing explicit instantiation directives. */
10835 push_deferring_access_checks (dk_no_check);
10836 /* Parse a decl-specifier-seq. */
10837 token = cp_lexer_peek_token (parser->lexer);
10838 cp_parser_decl_specifier_seq (parser,
10839 CP_PARSER_FLAGS_OPTIONAL,
10841 &declares_class_or_enum);
10842 /* If there was exactly one decl-specifier, and it declared a class,
10843 and there's no declarator, then we have an explicit type
10845 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10849 type = check_tag_decl (&decl_specifiers);
10850 /* Turn access control back on for names used during
10851 template instantiation. */
10852 pop_deferring_access_checks ();
10854 do_type_instantiation (type, extension_specifier,
10855 /*complain=*/tf_error);
10859 cp_declarator *declarator;
10862 /* Parse the declarator. */
10864 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10865 /*ctor_dtor_or_conv_p=*/NULL,
10866 /*parenthesized_p=*/NULL,
10867 /*member_p=*/false);
10868 if (declares_class_or_enum & 2)
10869 cp_parser_check_for_definition_in_return_type (declarator,
10870 decl_specifiers.type,
10871 decl_specifiers.type_location);
10872 if (declarator != cp_error_declarator)
10874 decl = grokdeclarator (declarator, &decl_specifiers,
10875 NORMAL, 0, &decl_specifiers.attributes);
10876 /* Turn access control back on for names used during
10877 template instantiation. */
10878 pop_deferring_access_checks ();
10879 /* Do the explicit instantiation. */
10880 do_decl_instantiation (decl, extension_specifier);
10884 pop_deferring_access_checks ();
10885 /* Skip the body of the explicit instantiation. */
10886 cp_parser_skip_to_end_of_statement (parser);
10889 /* We're done with the instantiation. */
10890 end_explicit_instantiation ();
10892 cp_parser_consume_semicolon_at_end_of_statement (parser);
10895 /* Parse an explicit-specialization.
10897 explicit-specialization:
10898 template < > declaration
10900 Although the standard says `declaration', what it really means is:
10902 explicit-specialization:
10903 template <> decl-specifier [opt] init-declarator [opt] ;
10904 template <> function-definition
10905 template <> explicit-specialization
10906 template <> template-declaration */
10909 cp_parser_explicit_specialization (cp_parser* parser)
10911 bool need_lang_pop;
10912 cp_token *token = cp_lexer_peek_token (parser->lexer);
10914 /* Look for the `template' keyword. */
10915 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10916 /* Look for the `<'. */
10917 cp_parser_require (parser, CPP_LESS, "%<<%>");
10918 /* Look for the `>'. */
10919 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10920 /* We have processed another parameter list. */
10921 ++parser->num_template_parameter_lists;
10924 A template ... explicit specialization ... shall not have C
10926 if (current_lang_name == lang_name_c)
10928 error ("%Htemplate specialization with C linkage", &token->location);
10929 /* Give it C++ linkage to avoid confusing other parts of the
10931 push_lang_context (lang_name_cplusplus);
10932 need_lang_pop = true;
10935 need_lang_pop = false;
10936 /* Let the front end know that we are beginning a specialization. */
10937 if (!begin_specialization ())
10939 end_specialization ();
10943 /* If the next keyword is `template', we need to figure out whether
10944 or not we're looking a template-declaration. */
10945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10947 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10948 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10949 cp_parser_template_declaration_after_export (parser,
10950 /*member_p=*/false);
10952 cp_parser_explicit_specialization (parser);
10955 /* Parse the dependent declaration. */
10956 cp_parser_single_declaration (parser,
10958 /*member_p=*/false,
10959 /*explicit_specialization_p=*/true,
10960 /*friend_p=*/NULL);
10961 /* We're done with the specialization. */
10962 end_specialization ();
10963 /* For the erroneous case of a template with C linkage, we pushed an
10964 implicit C++ linkage scope; exit that scope now. */
10966 pop_lang_context ();
10967 /* We're done with this parameter list. */
10968 --parser->num_template_parameter_lists;
10971 /* Parse a type-specifier.
10974 simple-type-specifier
10977 elaborated-type-specifier
10985 Returns a representation of the type-specifier. For a
10986 class-specifier, enum-specifier, or elaborated-type-specifier, a
10987 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10989 The parser flags FLAGS is used to control type-specifier parsing.
10991 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10992 in a decl-specifier-seq.
10994 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10995 class-specifier, enum-specifier, or elaborated-type-specifier, then
10996 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10997 if a type is declared; 2 if it is defined. Otherwise, it is set to
11000 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11001 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11002 is set to FALSE. */
11005 cp_parser_type_specifier (cp_parser* parser,
11006 cp_parser_flags flags,
11007 cp_decl_specifier_seq *decl_specs,
11008 bool is_declaration,
11009 int* declares_class_or_enum,
11010 bool* is_cv_qualifier)
11012 tree type_spec = NULL_TREE;
11015 cp_decl_spec ds = ds_last;
11017 /* Assume this type-specifier does not declare a new type. */
11018 if (declares_class_or_enum)
11019 *declares_class_or_enum = 0;
11020 /* And that it does not specify a cv-qualifier. */
11021 if (is_cv_qualifier)
11022 *is_cv_qualifier = false;
11023 /* Peek at the next token. */
11024 token = cp_lexer_peek_token (parser->lexer);
11026 /* If we're looking at a keyword, we can use that to guide the
11027 production we choose. */
11028 keyword = token->keyword;
11032 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11033 goto elaborated_type_specifier;
11035 /* Look for the enum-specifier. */
11036 type_spec = cp_parser_enum_specifier (parser);
11037 /* If that worked, we're done. */
11040 if (declares_class_or_enum)
11041 *declares_class_or_enum = 2;
11043 cp_parser_set_decl_spec_type (decl_specs,
11046 /*user_defined_p=*/true);
11050 goto elaborated_type_specifier;
11052 /* Any of these indicate either a class-specifier, or an
11053 elaborated-type-specifier. */
11057 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11058 goto elaborated_type_specifier;
11060 /* Parse tentatively so that we can back up if we don't find a
11061 class-specifier. */
11062 cp_parser_parse_tentatively (parser);
11063 /* Look for the class-specifier. */
11064 type_spec = cp_parser_class_specifier (parser);
11065 /* If that worked, we're done. */
11066 if (cp_parser_parse_definitely (parser))
11068 if (declares_class_or_enum)
11069 *declares_class_or_enum = 2;
11071 cp_parser_set_decl_spec_type (decl_specs,
11074 /*user_defined_p=*/true);
11078 /* Fall through. */
11079 elaborated_type_specifier:
11080 /* We're declaring (not defining) a class or enum. */
11081 if (declares_class_or_enum)
11082 *declares_class_or_enum = 1;
11084 /* Fall through. */
11086 /* Look for an elaborated-type-specifier. */
11088 = (cp_parser_elaborated_type_specifier
11090 decl_specs && decl_specs->specs[(int) ds_friend],
11093 cp_parser_set_decl_spec_type (decl_specs,
11096 /*user_defined_p=*/true);
11101 if (is_cv_qualifier)
11102 *is_cv_qualifier = true;
11107 if (is_cv_qualifier)
11108 *is_cv_qualifier = true;
11113 if (is_cv_qualifier)
11114 *is_cv_qualifier = true;
11118 /* The `__complex__' keyword is a GNU extension. */
11126 /* Handle simple keywords. */
11131 ++decl_specs->specs[(int)ds];
11132 decl_specs->any_specifiers_p = true;
11134 return cp_lexer_consume_token (parser->lexer)->u.value;
11137 /* If we do not already have a type-specifier, assume we are looking
11138 at a simple-type-specifier. */
11139 type_spec = cp_parser_simple_type_specifier (parser,
11143 /* If we didn't find a type-specifier, and a type-specifier was not
11144 optional in this context, issue an error message. */
11145 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11147 cp_parser_error (parser, "expected type specifier");
11148 return error_mark_node;
11154 /* Parse a simple-type-specifier.
11156 simple-type-specifier:
11157 :: [opt] nested-name-specifier [opt] type-name
11158 :: [opt] nested-name-specifier template template-id
11173 simple-type-specifier:
11175 decltype ( expression )
11181 simple-type-specifier:
11182 __typeof__ unary-expression
11183 __typeof__ ( type-id )
11185 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11186 appropriately updated. */
11189 cp_parser_simple_type_specifier (cp_parser* parser,
11190 cp_decl_specifier_seq *decl_specs,
11191 cp_parser_flags flags)
11193 tree type = NULL_TREE;
11196 /* Peek at the next token. */
11197 token = cp_lexer_peek_token (parser->lexer);
11199 /* If we're looking at a keyword, things are easy. */
11200 switch (token->keyword)
11204 decl_specs->explicit_char_p = true;
11205 type = char_type_node;
11208 type = char16_type_node;
11211 type = char32_type_node;
11214 type = wchar_type_node;
11217 type = boolean_type_node;
11221 ++decl_specs->specs[(int) ds_short];
11222 type = short_integer_type_node;
11226 decl_specs->explicit_int_p = true;
11227 type = integer_type_node;
11231 ++decl_specs->specs[(int) ds_long];
11232 type = long_integer_type_node;
11236 ++decl_specs->specs[(int) ds_signed];
11237 type = integer_type_node;
11241 ++decl_specs->specs[(int) ds_unsigned];
11242 type = unsigned_type_node;
11245 type = float_type_node;
11248 type = double_type_node;
11251 type = void_type_node;
11255 maybe_warn_cpp0x ("C++0x auto");
11256 type = make_auto ();
11260 /* Parse the `decltype' type. */
11261 type = cp_parser_decltype (parser);
11264 cp_parser_set_decl_spec_type (decl_specs, type,
11266 /*user_defined_p=*/true);
11271 /* Consume the `typeof' token. */
11272 cp_lexer_consume_token (parser->lexer);
11273 /* Parse the operand to `typeof'. */
11274 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11275 /* If it is not already a TYPE, take its type. */
11276 if (!TYPE_P (type))
11277 type = finish_typeof (type);
11280 cp_parser_set_decl_spec_type (decl_specs, type,
11282 /*user_defined_p=*/true);
11290 /* If the type-specifier was for a built-in type, we're done. */
11295 /* Record the type. */
11297 && (token->keyword != RID_SIGNED
11298 && token->keyword != RID_UNSIGNED
11299 && token->keyword != RID_SHORT
11300 && token->keyword != RID_LONG))
11301 cp_parser_set_decl_spec_type (decl_specs,
11304 /*user_defined=*/false);
11306 decl_specs->any_specifiers_p = true;
11308 /* Consume the token. */
11309 id = cp_lexer_consume_token (parser->lexer)->u.value;
11311 /* There is no valid C++ program where a non-template type is
11312 followed by a "<". That usually indicates that the user thought
11313 that the type was a template. */
11314 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11316 return TYPE_NAME (type);
11319 /* The type-specifier must be a user-defined type. */
11320 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11325 /* Don't gobble tokens or issue error messages if this is an
11326 optional type-specifier. */
11327 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11328 cp_parser_parse_tentatively (parser);
11330 /* Look for the optional `::' operator. */
11332 = (cp_parser_global_scope_opt (parser,
11333 /*current_scope_valid_p=*/false)
11335 /* Look for the nested-name specifier. */
11337 = (cp_parser_nested_name_specifier_opt (parser,
11338 /*typename_keyword_p=*/false,
11339 /*check_dependency_p=*/true,
11341 /*is_declaration=*/false)
11343 token = cp_lexer_peek_token (parser->lexer);
11344 /* If we have seen a nested-name-specifier, and the next token
11345 is `template', then we are using the template-id production. */
11347 && cp_parser_optional_template_keyword (parser))
11349 /* Look for the template-id. */
11350 type = cp_parser_template_id (parser,
11351 /*template_keyword_p=*/true,
11352 /*check_dependency_p=*/true,
11353 /*is_declaration=*/false);
11354 /* If the template-id did not name a type, we are out of
11356 if (TREE_CODE (type) != TYPE_DECL)
11358 cp_parser_error (parser, "expected template-id for type");
11362 /* Otherwise, look for a type-name. */
11364 type = cp_parser_type_name (parser);
11365 /* Keep track of all name-lookups performed in class scopes. */
11369 && TREE_CODE (type) == TYPE_DECL
11370 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11371 maybe_note_name_used_in_class (DECL_NAME (type), type);
11372 /* If it didn't work out, we don't have a TYPE. */
11373 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11374 && !cp_parser_parse_definitely (parser))
11376 if (type && decl_specs)
11377 cp_parser_set_decl_spec_type (decl_specs, type,
11379 /*user_defined=*/true);
11382 /* If we didn't get a type-name, issue an error message. */
11383 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11385 cp_parser_error (parser, "expected type-name");
11386 return error_mark_node;
11389 /* There is no valid C++ program where a non-template type is
11390 followed by a "<". That usually indicates that the user thought
11391 that the type was a template. */
11392 if (type && type != error_mark_node)
11394 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11395 If it is, then the '<'...'>' enclose protocol names rather than
11396 template arguments, and so everything is fine. */
11397 if (c_dialect_objc ()
11398 && (objc_is_id (type) || objc_is_class_name (type)))
11400 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11401 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11403 /* Clobber the "unqualified" type previously entered into
11404 DECL_SPECS with the new, improved protocol-qualified version. */
11406 decl_specs->type = qual_type;
11411 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11418 /* Parse a type-name.
11431 Returns a TYPE_DECL for the type. */
11434 cp_parser_type_name (cp_parser* parser)
11438 /* We can't know yet whether it is a class-name or not. */
11439 cp_parser_parse_tentatively (parser);
11440 /* Try a class-name. */
11441 type_decl = cp_parser_class_name (parser,
11442 /*typename_keyword_p=*/false,
11443 /*template_keyword_p=*/false,
11445 /*check_dependency_p=*/true,
11446 /*class_head_p=*/false,
11447 /*is_declaration=*/false);
11448 /* If it's not a class-name, keep looking. */
11449 if (!cp_parser_parse_definitely (parser))
11451 /* It must be a typedef-name or an enum-name. */
11452 return cp_parser_nonclass_name (parser);
11458 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11466 Returns a TYPE_DECL for the type. */
11469 cp_parser_nonclass_name (cp_parser* parser)
11474 cp_token *token = cp_lexer_peek_token (parser->lexer);
11475 identifier = cp_parser_identifier (parser);
11476 if (identifier == error_mark_node)
11477 return error_mark_node;
11479 /* Look up the type-name. */
11480 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11482 if (TREE_CODE (type_decl) != TYPE_DECL
11483 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11485 /* See if this is an Objective-C type. */
11486 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11487 tree type = objc_get_protocol_qualified_type (identifier, protos);
11489 type_decl = TYPE_NAME (type);
11492 /* Issue an error if we did not find a type-name. */
11493 if (TREE_CODE (type_decl) != TYPE_DECL)
11495 if (!cp_parser_simulate_error (parser))
11496 cp_parser_name_lookup_error (parser, identifier, type_decl,
11497 "is not a type", token->location);
11498 return error_mark_node;
11500 /* Remember that the name was used in the definition of the
11501 current class so that we can check later to see if the
11502 meaning would have been different after the class was
11503 entirely defined. */
11504 else if (type_decl != error_mark_node
11506 maybe_note_name_used_in_class (identifier, type_decl);
11511 /* Parse an elaborated-type-specifier. Note that the grammar given
11512 here incorporates the resolution to DR68.
11514 elaborated-type-specifier:
11515 class-key :: [opt] nested-name-specifier [opt] identifier
11516 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11517 enum-key :: [opt] nested-name-specifier [opt] identifier
11518 typename :: [opt] nested-name-specifier identifier
11519 typename :: [opt] nested-name-specifier template [opt]
11524 elaborated-type-specifier:
11525 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11526 class-key attributes :: [opt] nested-name-specifier [opt]
11527 template [opt] template-id
11528 enum attributes :: [opt] nested-name-specifier [opt] identifier
11530 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11531 declared `friend'. If IS_DECLARATION is TRUE, then this
11532 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11533 something is being declared.
11535 Returns the TYPE specified. */
11538 cp_parser_elaborated_type_specifier (cp_parser* parser,
11540 bool is_declaration)
11542 enum tag_types tag_type;
11544 tree type = NULL_TREE;
11545 tree attributes = NULL_TREE;
11546 cp_token *token = NULL;
11548 /* See if we're looking at the `enum' keyword. */
11549 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11551 /* Consume the `enum' token. */
11552 cp_lexer_consume_token (parser->lexer);
11553 /* Remember that it's an enumeration type. */
11554 tag_type = enum_type;
11555 /* Parse the optional `struct' or `class' key (for C++0x scoped
11557 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11558 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11560 if (cxx_dialect == cxx98)
11561 maybe_warn_cpp0x ("scoped enums");
11563 /* Consume the `struct' or `class'. */
11564 cp_lexer_consume_token (parser->lexer);
11566 /* Parse the attributes. */
11567 attributes = cp_parser_attributes_opt (parser);
11569 /* Or, it might be `typename'. */
11570 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11573 /* Consume the `typename' token. */
11574 cp_lexer_consume_token (parser->lexer);
11575 /* Remember that it's a `typename' type. */
11576 tag_type = typename_type;
11577 /* The `typename' keyword is only allowed in templates. */
11578 if (!processing_template_decl)
11579 permerror (input_location, "using %<typename%> outside of template");
11581 /* Otherwise it must be a class-key. */
11584 tag_type = cp_parser_class_key (parser);
11585 if (tag_type == none_type)
11586 return error_mark_node;
11587 /* Parse the attributes. */
11588 attributes = cp_parser_attributes_opt (parser);
11591 /* Look for the `::' operator. */
11592 cp_parser_global_scope_opt (parser,
11593 /*current_scope_valid_p=*/false);
11594 /* Look for the nested-name-specifier. */
11595 if (tag_type == typename_type)
11597 if (!cp_parser_nested_name_specifier (parser,
11598 /*typename_keyword_p=*/true,
11599 /*check_dependency_p=*/true,
11602 return error_mark_node;
11605 /* Even though `typename' is not present, the proposed resolution
11606 to Core Issue 180 says that in `class A<T>::B', `B' should be
11607 considered a type-name, even if `A<T>' is dependent. */
11608 cp_parser_nested_name_specifier_opt (parser,
11609 /*typename_keyword_p=*/true,
11610 /*check_dependency_p=*/true,
11613 /* For everything but enumeration types, consider a template-id.
11614 For an enumeration type, consider only a plain identifier. */
11615 if (tag_type != enum_type)
11617 bool template_p = false;
11620 /* Allow the `template' keyword. */
11621 template_p = cp_parser_optional_template_keyword (parser);
11622 /* If we didn't see `template', we don't know if there's a
11623 template-id or not. */
11625 cp_parser_parse_tentatively (parser);
11626 /* Parse the template-id. */
11627 token = cp_lexer_peek_token (parser->lexer);
11628 decl = cp_parser_template_id (parser, template_p,
11629 /*check_dependency_p=*/true,
11631 /* If we didn't find a template-id, look for an ordinary
11633 if (!template_p && !cp_parser_parse_definitely (parser))
11635 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11636 in effect, then we must assume that, upon instantiation, the
11637 template will correspond to a class. */
11638 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11639 && tag_type == typename_type)
11640 type = make_typename_type (parser->scope, decl,
11642 /*complain=*/tf_error);
11643 /* If the `typename' keyword is in effect and DECL is not a type
11644 decl. Then type is non existant. */
11645 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11648 type = TREE_TYPE (decl);
11653 token = cp_lexer_peek_token (parser->lexer);
11654 identifier = cp_parser_identifier (parser);
11656 if (identifier == error_mark_node)
11658 parser->scope = NULL_TREE;
11659 return error_mark_node;
11662 /* For a `typename', we needn't call xref_tag. */
11663 if (tag_type == typename_type
11664 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11665 return cp_parser_make_typename_type (parser, parser->scope,
11668 /* Look up a qualified name in the usual way. */
11672 tree ambiguous_decls;
11674 decl = cp_parser_lookup_name (parser, identifier,
11676 /*is_template=*/false,
11677 /*is_namespace=*/false,
11678 /*check_dependency=*/true,
11682 /* If the lookup was ambiguous, an error will already have been
11684 if (ambiguous_decls)
11685 return error_mark_node;
11687 /* If we are parsing friend declaration, DECL may be a
11688 TEMPLATE_DECL tree node here. However, we need to check
11689 whether this TEMPLATE_DECL results in valid code. Consider
11690 the following example:
11693 template <class T> class C {};
11696 template <class T> friend class N::C; // #1, valid code
11698 template <class T> class Y {
11699 friend class N::C; // #2, invalid code
11702 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11703 name lookup of `N::C'. We see that friend declaration must
11704 be template for the code to be valid. Note that
11705 processing_template_decl does not work here since it is
11706 always 1 for the above two cases. */
11708 decl = (cp_parser_maybe_treat_template_as_class
11709 (decl, /*tag_name_p=*/is_friend
11710 && parser->num_template_parameter_lists));
11712 if (TREE_CODE (decl) != TYPE_DECL)
11714 cp_parser_diagnose_invalid_type_name (parser,
11718 return error_mark_node;
11721 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11723 bool allow_template = (parser->num_template_parameter_lists
11724 || DECL_SELF_REFERENCE_P (decl));
11725 type = check_elaborated_type_specifier (tag_type, decl,
11728 if (type == error_mark_node)
11729 return error_mark_node;
11732 /* Forward declarations of nested types, such as
11737 are invalid unless all components preceding the final '::'
11738 are complete. If all enclosing types are complete, these
11739 declarations become merely pointless.
11741 Invalid forward declarations of nested types are errors
11742 caught elsewhere in parsing. Those that are pointless arrive
11745 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11746 && !is_friend && !processing_explicit_instantiation)
11747 warning (0, "declaration %qD does not declare anything", decl);
11749 type = TREE_TYPE (decl);
11753 /* An elaborated-type-specifier sometimes introduces a new type and
11754 sometimes names an existing type. Normally, the rule is that it
11755 introduces a new type only if there is not an existing type of
11756 the same name already in scope. For example, given:
11759 void f() { struct S s; }
11761 the `struct S' in the body of `f' is the same `struct S' as in
11762 the global scope; the existing definition is used. However, if
11763 there were no global declaration, this would introduce a new
11764 local class named `S'.
11766 An exception to this rule applies to the following code:
11768 namespace N { struct S; }
11770 Here, the elaborated-type-specifier names a new type
11771 unconditionally; even if there is already an `S' in the
11772 containing scope this declaration names a new type.
11773 This exception only applies if the elaborated-type-specifier
11774 forms the complete declaration:
11778 A declaration consisting solely of `class-key identifier ;' is
11779 either a redeclaration of the name in the current scope or a
11780 forward declaration of the identifier as a class name. It
11781 introduces the name into the current scope.
11783 We are in this situation precisely when the next token is a `;'.
11785 An exception to the exception is that a `friend' declaration does
11786 *not* name a new type; i.e., given:
11788 struct S { friend struct T; };
11790 `T' is not a new type in the scope of `S'.
11792 Also, `new struct S' or `sizeof (struct S)' never results in the
11793 definition of a new type; a new type can only be declared in a
11794 declaration context. */
11800 /* Friends have special name lookup rules. */
11801 ts = ts_within_enclosing_non_class;
11802 else if (is_declaration
11803 && cp_lexer_next_token_is (parser->lexer,
11805 /* This is a `class-key identifier ;' */
11811 (parser->num_template_parameter_lists
11812 && (cp_parser_next_token_starts_class_definition_p (parser)
11813 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11814 /* An unqualified name was used to reference this type, so
11815 there were no qualifying templates. */
11816 if (!cp_parser_check_template_parameters (parser,
11817 /*num_templates=*/0,
11819 return error_mark_node;
11820 type = xref_tag (tag_type, identifier, ts, template_p);
11824 if (type == error_mark_node)
11825 return error_mark_node;
11827 /* Allow attributes on forward declarations of classes. */
11830 if (TREE_CODE (type) == TYPENAME_TYPE)
11831 warning (OPT_Wattributes,
11832 "attributes ignored on uninstantiated type");
11833 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11834 && ! processing_explicit_instantiation)
11835 warning (OPT_Wattributes,
11836 "attributes ignored on template instantiation");
11837 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11838 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11840 warning (OPT_Wattributes,
11841 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11844 if (tag_type != enum_type)
11845 cp_parser_check_class_key (tag_type, type);
11847 /* A "<" cannot follow an elaborated type specifier. If that
11848 happens, the user was probably trying to form a template-id. */
11849 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11854 /* Parse an enum-specifier.
11857 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11862 enum struct [C++0x]
11865 : type-specifier-seq
11868 enum-key attributes[opt] identifier [opt] enum-base [opt]
11869 { enumerator-list [opt] }attributes[opt]
11871 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11872 if the token stream isn't an enum-specifier after all. */
11875 cp_parser_enum_specifier (cp_parser* parser)
11880 bool scoped_enum_p = false;
11881 bool has_underlying_type = false;
11882 tree underlying_type = NULL_TREE;
11884 /* Parse tentatively so that we can back up if we don't find a
11886 cp_parser_parse_tentatively (parser);
11888 /* Caller guarantees that the current token is 'enum', an identifier
11889 possibly follows, and the token after that is an opening brace.
11890 If we don't have an identifier, fabricate an anonymous name for
11891 the enumeration being defined. */
11892 cp_lexer_consume_token (parser->lexer);
11894 /* Parse the "class" or "struct", which indicates a scoped
11895 enumeration type in C++0x. */
11896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11897 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11899 if (cxx_dialect == cxx98)
11900 maybe_warn_cpp0x ("scoped enums");
11902 /* Consume the `struct' or `class' token. */
11903 cp_lexer_consume_token (parser->lexer);
11905 scoped_enum_p = true;
11908 attributes = cp_parser_attributes_opt (parser);
11910 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11911 identifier = cp_parser_identifier (parser);
11913 identifier = make_anon_name ();
11915 /* Check for the `:' that denotes a specified underlying type in C++0x.
11916 Note that a ':' could also indicate a bitfield width, however. */
11917 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11919 cp_decl_specifier_seq type_specifiers;
11921 /* Consume the `:'. */
11922 cp_lexer_consume_token (parser->lexer);
11924 /* Parse the type-specifier-seq. */
11925 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11926 /*is_trailing_return=*/false,
11929 /* At this point this is surely not elaborated type specifier. */
11930 if (!cp_parser_parse_definitely (parser))
11933 if (cxx_dialect == cxx98)
11934 maybe_warn_cpp0x ("scoped enums");
11936 has_underlying_type = true;
11938 /* If that didn't work, stop. */
11939 if (type_specifiers.type != error_mark_node)
11941 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11942 /*initialized=*/0, NULL);
11943 if (underlying_type == error_mark_node)
11944 underlying_type = NULL_TREE;
11948 /* Look for the `{' but don't consume it yet. */
11949 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11951 cp_parser_error (parser, "expected %<{%>");
11952 if (has_underlying_type)
11956 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11959 /* Issue an error message if type-definitions are forbidden here. */
11960 if (!cp_parser_check_type_definition (parser))
11961 type = error_mark_node;
11963 /* Create the new type. We do this before consuming the opening
11964 brace so the enum will be recorded as being on the line of its
11965 tag (or the 'enum' keyword, if there is no tag). */
11966 type = start_enum (identifier, underlying_type, scoped_enum_p);
11968 /* Consume the opening brace. */
11969 cp_lexer_consume_token (parser->lexer);
11971 if (type == error_mark_node)
11973 cp_parser_skip_to_end_of_block_or_statement (parser);
11974 return error_mark_node;
11977 /* If the next token is not '}', then there are some enumerators. */
11978 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11979 cp_parser_enumerator_list (parser, type);
11981 /* Consume the final '}'. */
11982 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11984 /* Look for trailing attributes to apply to this enumeration, and
11985 apply them if appropriate. */
11986 if (cp_parser_allow_gnu_extensions_p (parser))
11988 tree trailing_attr = cp_parser_attributes_opt (parser);
11989 trailing_attr = chainon (trailing_attr, attributes);
11990 cplus_decl_attributes (&type,
11992 (int) ATTR_FLAG_TYPE_IN_PLACE);
11995 /* Finish up the enumeration. */
11996 finish_enum (type);
12001 /* Parse an enumerator-list. The enumerators all have the indicated
12005 enumerator-definition
12006 enumerator-list , enumerator-definition */
12009 cp_parser_enumerator_list (cp_parser* parser, tree type)
12013 /* Parse an enumerator-definition. */
12014 cp_parser_enumerator_definition (parser, type);
12016 /* If the next token is not a ',', we've reached the end of
12018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12020 /* Otherwise, consume the `,' and keep going. */
12021 cp_lexer_consume_token (parser->lexer);
12022 /* If the next token is a `}', there is a trailing comma. */
12023 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12025 if (!in_system_header)
12026 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12032 /* Parse an enumerator-definition. The enumerator has the indicated
12035 enumerator-definition:
12037 enumerator = constant-expression
12043 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12048 /* Look for the identifier. */
12049 identifier = cp_parser_identifier (parser);
12050 if (identifier == error_mark_node)
12053 /* If the next token is an '=', then there is an explicit value. */
12054 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12056 /* Consume the `=' token. */
12057 cp_lexer_consume_token (parser->lexer);
12058 /* Parse the value. */
12059 value = cp_parser_constant_expression (parser,
12060 /*allow_non_constant_p=*/false,
12066 /* If we are processing a template, make sure the initializer of the
12067 enumerator doesn't contain any bare template parameter pack. */
12068 if (check_for_bare_parameter_packs (value))
12069 value = error_mark_node;
12071 /* Create the enumerator. */
12072 build_enumerator (identifier, value, type);
12075 /* Parse a namespace-name.
12078 original-namespace-name
12081 Returns the NAMESPACE_DECL for the namespace. */
12084 cp_parser_namespace_name (cp_parser* parser)
12087 tree namespace_decl;
12089 cp_token *token = cp_lexer_peek_token (parser->lexer);
12091 /* Get the name of the namespace. */
12092 identifier = cp_parser_identifier (parser);
12093 if (identifier == error_mark_node)
12094 return error_mark_node;
12096 /* Look up the identifier in the currently active scope. Look only
12097 for namespaces, due to:
12099 [basic.lookup.udir]
12101 When looking up a namespace-name in a using-directive or alias
12102 definition, only namespace names are considered.
12106 [basic.lookup.qual]
12108 During the lookup of a name preceding the :: scope resolution
12109 operator, object, function, and enumerator names are ignored.
12111 (Note that cp_parser_qualifying_entity only calls this
12112 function if the token after the name is the scope resolution
12114 namespace_decl = cp_parser_lookup_name (parser, identifier,
12116 /*is_template=*/false,
12117 /*is_namespace=*/true,
12118 /*check_dependency=*/true,
12119 /*ambiguous_decls=*/NULL,
12121 /* If it's not a namespace, issue an error. */
12122 if (namespace_decl == error_mark_node
12123 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12125 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12126 error ("%H%qD is not a namespace-name", &token->location, identifier);
12127 cp_parser_error (parser, "expected namespace-name");
12128 namespace_decl = error_mark_node;
12131 return namespace_decl;
12134 /* Parse a namespace-definition.
12136 namespace-definition:
12137 named-namespace-definition
12138 unnamed-namespace-definition
12140 named-namespace-definition:
12141 original-namespace-definition
12142 extension-namespace-definition
12144 original-namespace-definition:
12145 namespace identifier { namespace-body }
12147 extension-namespace-definition:
12148 namespace original-namespace-name { namespace-body }
12150 unnamed-namespace-definition:
12151 namespace { namespace-body } */
12154 cp_parser_namespace_definition (cp_parser* parser)
12156 tree identifier, attribs;
12157 bool has_visibility;
12160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12163 cp_lexer_consume_token (parser->lexer);
12168 /* Look for the `namespace' keyword. */
12169 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12171 /* Get the name of the namespace. We do not attempt to distinguish
12172 between an original-namespace-definition and an
12173 extension-namespace-definition at this point. The semantic
12174 analysis routines are responsible for that. */
12175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12176 identifier = cp_parser_identifier (parser);
12178 identifier = NULL_TREE;
12180 /* Parse any specified attributes. */
12181 attribs = cp_parser_attributes_opt (parser);
12183 /* Look for the `{' to start the namespace. */
12184 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12185 /* Start the namespace. */
12186 push_namespace (identifier);
12188 /* "inline namespace" is equivalent to a stub namespace definition
12189 followed by a strong using directive. */
12192 tree name_space = current_namespace;
12193 /* Set up namespace association. */
12194 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12195 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12196 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12197 /* Import the contents of the inline namespace. */
12199 do_using_directive (name_space);
12200 push_namespace (identifier);
12203 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12205 /* Parse the body of the namespace. */
12206 cp_parser_namespace_body (parser);
12208 #ifdef HANDLE_PRAGMA_VISIBILITY
12209 if (has_visibility)
12213 /* Finish the namespace. */
12215 /* Look for the final `}'. */
12216 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12219 /* Parse a namespace-body.
12222 declaration-seq [opt] */
12225 cp_parser_namespace_body (cp_parser* parser)
12227 cp_parser_declaration_seq_opt (parser);
12230 /* Parse a namespace-alias-definition.
12232 namespace-alias-definition:
12233 namespace identifier = qualified-namespace-specifier ; */
12236 cp_parser_namespace_alias_definition (cp_parser* parser)
12239 tree namespace_specifier;
12241 cp_token *token = cp_lexer_peek_token (parser->lexer);
12243 /* Look for the `namespace' keyword. */
12244 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12245 /* Look for the identifier. */
12246 identifier = cp_parser_identifier (parser);
12247 if (identifier == error_mark_node)
12249 /* Look for the `=' token. */
12250 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12251 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12253 error ("%H%<namespace%> definition is not allowed here", &token->location);
12254 /* Skip the definition. */
12255 cp_lexer_consume_token (parser->lexer);
12256 if (cp_parser_skip_to_closing_brace (parser))
12257 cp_lexer_consume_token (parser->lexer);
12260 cp_parser_require (parser, CPP_EQ, "%<=%>");
12261 /* Look for the qualified-namespace-specifier. */
12262 namespace_specifier
12263 = cp_parser_qualified_namespace_specifier (parser);
12264 /* Look for the `;' token. */
12265 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12267 /* Register the alias in the symbol table. */
12268 do_namespace_alias (identifier, namespace_specifier);
12271 /* Parse a qualified-namespace-specifier.
12273 qualified-namespace-specifier:
12274 :: [opt] nested-name-specifier [opt] namespace-name
12276 Returns a NAMESPACE_DECL corresponding to the specified
12280 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12282 /* Look for the optional `::'. */
12283 cp_parser_global_scope_opt (parser,
12284 /*current_scope_valid_p=*/false);
12286 /* Look for the optional nested-name-specifier. */
12287 cp_parser_nested_name_specifier_opt (parser,
12288 /*typename_keyword_p=*/false,
12289 /*check_dependency_p=*/true,
12291 /*is_declaration=*/true);
12293 return cp_parser_namespace_name (parser);
12296 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12297 access declaration.
12300 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12301 using :: unqualified-id ;
12303 access-declaration:
12309 cp_parser_using_declaration (cp_parser* parser,
12310 bool access_declaration_p)
12313 bool typename_p = false;
12314 bool global_scope_p;
12319 if (access_declaration_p)
12320 cp_parser_parse_tentatively (parser);
12323 /* Look for the `using' keyword. */
12324 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12326 /* Peek at the next token. */
12327 token = cp_lexer_peek_token (parser->lexer);
12328 /* See if it's `typename'. */
12329 if (token->keyword == RID_TYPENAME)
12331 /* Remember that we've seen it. */
12333 /* Consume the `typename' token. */
12334 cp_lexer_consume_token (parser->lexer);
12338 /* Look for the optional global scope qualification. */
12340 = (cp_parser_global_scope_opt (parser,
12341 /*current_scope_valid_p=*/false)
12344 /* If we saw `typename', or didn't see `::', then there must be a
12345 nested-name-specifier present. */
12346 if (typename_p || !global_scope_p)
12347 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12348 /*check_dependency_p=*/true,
12350 /*is_declaration=*/true);
12351 /* Otherwise, we could be in either of the two productions. In that
12352 case, treat the nested-name-specifier as optional. */
12354 qscope = cp_parser_nested_name_specifier_opt (parser,
12355 /*typename_keyword_p=*/false,
12356 /*check_dependency_p=*/true,
12358 /*is_declaration=*/true);
12360 qscope = global_namespace;
12362 if (access_declaration_p && cp_parser_error_occurred (parser))
12363 /* Something has already gone wrong; there's no need to parse
12364 further. Since an error has occurred, the return value of
12365 cp_parser_parse_definitely will be false, as required. */
12366 return cp_parser_parse_definitely (parser);
12368 token = cp_lexer_peek_token (parser->lexer);
12369 /* Parse the unqualified-id. */
12370 identifier = cp_parser_unqualified_id (parser,
12371 /*template_keyword_p=*/false,
12372 /*check_dependency_p=*/true,
12373 /*declarator_p=*/true,
12374 /*optional_p=*/false);
12376 if (access_declaration_p)
12378 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12379 cp_parser_simulate_error (parser);
12380 if (!cp_parser_parse_definitely (parser))
12384 /* The function we call to handle a using-declaration is different
12385 depending on what scope we are in. */
12386 if (qscope == error_mark_node || identifier == error_mark_node)
12388 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12389 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12390 /* [namespace.udecl]
12392 A using declaration shall not name a template-id. */
12393 error ("%Ha template-id may not appear in a using-declaration",
12397 if (at_class_scope_p ())
12399 /* Create the USING_DECL. */
12400 decl = do_class_using_decl (parser->scope, identifier);
12402 if (check_for_bare_parameter_packs (decl))
12405 /* Add it to the list of members in this class. */
12406 finish_member_declaration (decl);
12410 decl = cp_parser_lookup_name_simple (parser,
12413 if (decl == error_mark_node)
12414 cp_parser_name_lookup_error (parser, identifier,
12417 else if (check_for_bare_parameter_packs (decl))
12419 else if (!at_namespace_scope_p ())
12420 do_local_using_decl (decl, qscope, identifier);
12422 do_toplevel_using_decl (decl, qscope, identifier);
12426 /* Look for the final `;'. */
12427 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12432 /* Parse a using-directive.
12435 using namespace :: [opt] nested-name-specifier [opt]
12436 namespace-name ; */
12439 cp_parser_using_directive (cp_parser* parser)
12441 tree namespace_decl;
12444 /* Look for the `using' keyword. */
12445 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12446 /* And the `namespace' keyword. */
12447 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12448 /* Look for the optional `::' operator. */
12449 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12450 /* And the optional nested-name-specifier. */
12451 cp_parser_nested_name_specifier_opt (parser,
12452 /*typename_keyword_p=*/false,
12453 /*check_dependency_p=*/true,
12455 /*is_declaration=*/true);
12456 /* Get the namespace being used. */
12457 namespace_decl = cp_parser_namespace_name (parser);
12458 /* And any specified attributes. */
12459 attribs = cp_parser_attributes_opt (parser);
12460 /* Update the symbol table. */
12461 parse_using_directive (namespace_decl, attribs);
12462 /* Look for the final `;'. */
12463 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12466 /* Parse an asm-definition.
12469 asm ( string-literal ) ;
12474 asm volatile [opt] ( string-literal ) ;
12475 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12476 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12477 : asm-operand-list [opt] ) ;
12478 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12479 : asm-operand-list [opt]
12480 : asm-operand-list [opt] ) ; */
12483 cp_parser_asm_definition (cp_parser* parser)
12486 tree outputs = NULL_TREE;
12487 tree inputs = NULL_TREE;
12488 tree clobbers = NULL_TREE;
12490 bool volatile_p = false;
12491 bool extended_p = false;
12492 bool invalid_inputs_p = false;
12493 bool invalid_outputs_p = false;
12495 /* Look for the `asm' keyword. */
12496 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12497 /* See if the next token is `volatile'. */
12498 if (cp_parser_allow_gnu_extensions_p (parser)
12499 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12501 /* Remember that we saw the `volatile' keyword. */
12503 /* Consume the token. */
12504 cp_lexer_consume_token (parser->lexer);
12506 /* Look for the opening `('. */
12507 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12509 /* Look for the string. */
12510 string = cp_parser_string_literal (parser, false, false);
12511 if (string == error_mark_node)
12513 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12514 /*consume_paren=*/true);
12518 /* If we're allowing GNU extensions, check for the extended assembly
12519 syntax. Unfortunately, the `:' tokens need not be separated by
12520 a space in C, and so, for compatibility, we tolerate that here
12521 too. Doing that means that we have to treat the `::' operator as
12523 if (cp_parser_allow_gnu_extensions_p (parser)
12524 && parser->in_function_body
12525 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12526 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12528 bool inputs_p = false;
12529 bool clobbers_p = false;
12531 /* The extended syntax was used. */
12534 /* Look for outputs. */
12535 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12537 /* Consume the `:'. */
12538 cp_lexer_consume_token (parser->lexer);
12539 /* Parse the output-operands. */
12540 if (cp_lexer_next_token_is_not (parser->lexer,
12542 && cp_lexer_next_token_is_not (parser->lexer,
12544 && cp_lexer_next_token_is_not (parser->lexer,
12546 outputs = cp_parser_asm_operand_list (parser);
12548 if (outputs == error_mark_node)
12549 invalid_outputs_p = true;
12551 /* If the next token is `::', there are no outputs, and the
12552 next token is the beginning of the inputs. */
12553 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12554 /* The inputs are coming next. */
12557 /* Look for inputs. */
12559 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12561 /* Consume the `:' or `::'. */
12562 cp_lexer_consume_token (parser->lexer);
12563 /* Parse the output-operands. */
12564 if (cp_lexer_next_token_is_not (parser->lexer,
12566 && cp_lexer_next_token_is_not (parser->lexer,
12568 inputs = cp_parser_asm_operand_list (parser);
12570 if (inputs == error_mark_node)
12571 invalid_inputs_p = true;
12573 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12574 /* The clobbers are coming next. */
12577 /* Look for clobbers. */
12579 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12581 /* Consume the `:' or `::'. */
12582 cp_lexer_consume_token (parser->lexer);
12583 /* Parse the clobbers. */
12584 if (cp_lexer_next_token_is_not (parser->lexer,
12586 clobbers = cp_parser_asm_clobber_list (parser);
12589 /* Look for the closing `)'. */
12590 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12591 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12592 /*consume_paren=*/true);
12593 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12595 if (!invalid_inputs_p && !invalid_outputs_p)
12597 /* Create the ASM_EXPR. */
12598 if (parser->in_function_body)
12600 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12602 /* If the extended syntax was not used, mark the ASM_EXPR. */
12605 tree temp = asm_stmt;
12606 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12607 temp = TREE_OPERAND (temp, 0);
12609 ASM_INPUT_P (temp) = 1;
12613 cgraph_add_asm_node (string);
12617 /* Declarators [gram.dcl.decl] */
12619 /* Parse an init-declarator.
12622 declarator initializer [opt]
12627 declarator asm-specification [opt] attributes [opt] initializer [opt]
12629 function-definition:
12630 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12632 decl-specifier-seq [opt] declarator function-try-block
12636 function-definition:
12637 __extension__ function-definition
12639 The DECL_SPECIFIERS apply to this declarator. Returns a
12640 representation of the entity declared. If MEMBER_P is TRUE, then
12641 this declarator appears in a class scope. The new DECL created by
12642 this declarator is returned.
12644 The CHECKS are access checks that should be performed once we know
12645 what entity is being declared (and, therefore, what classes have
12648 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12649 for a function-definition here as well. If the declarator is a
12650 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12651 be TRUE upon return. By that point, the function-definition will
12652 have been completely parsed.
12654 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12658 cp_parser_init_declarator (cp_parser* parser,
12659 cp_decl_specifier_seq *decl_specifiers,
12660 VEC (deferred_access_check,gc)* checks,
12661 bool function_definition_allowed_p,
12663 int declares_class_or_enum,
12664 bool* function_definition_p)
12666 cp_token *token = NULL, *asm_spec_start_token = NULL,
12667 *attributes_start_token = NULL;
12668 cp_declarator *declarator;
12669 tree prefix_attributes;
12671 tree asm_specification;
12673 tree decl = NULL_TREE;
12675 int is_initialized;
12676 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12677 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12679 enum cpp_ttype initialization_kind;
12680 bool is_direct_init = false;
12681 bool is_non_constant_init;
12682 int ctor_dtor_or_conv_p;
12684 tree pushed_scope = NULL;
12686 /* Gather the attributes that were provided with the
12687 decl-specifiers. */
12688 prefix_attributes = decl_specifiers->attributes;
12690 /* Assume that this is not the declarator for a function
12692 if (function_definition_p)
12693 *function_definition_p = false;
12695 /* Defer access checks while parsing the declarator; we cannot know
12696 what names are accessible until we know what is being
12698 resume_deferring_access_checks ();
12700 /* Parse the declarator. */
12701 token = cp_lexer_peek_token (parser->lexer);
12703 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12704 &ctor_dtor_or_conv_p,
12705 /*parenthesized_p=*/NULL,
12706 /*member_p=*/false);
12707 /* Gather up the deferred checks. */
12708 stop_deferring_access_checks ();
12710 /* If the DECLARATOR was erroneous, there's no need to go
12712 if (declarator == cp_error_declarator)
12713 return error_mark_node;
12715 /* Check that the number of template-parameter-lists is OK. */
12716 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12718 return error_mark_node;
12720 if (declares_class_or_enum & 2)
12721 cp_parser_check_for_definition_in_return_type (declarator,
12722 decl_specifiers->type,
12723 decl_specifiers->type_location);
12725 /* Figure out what scope the entity declared by the DECLARATOR is
12726 located in. `grokdeclarator' sometimes changes the scope, so
12727 we compute it now. */
12728 scope = get_scope_of_declarator (declarator);
12730 /* If we're allowing GNU extensions, look for an asm-specification
12732 if (cp_parser_allow_gnu_extensions_p (parser))
12734 /* Look for an asm-specification. */
12735 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12736 asm_specification = cp_parser_asm_specification_opt (parser);
12737 /* And attributes. */
12738 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12739 attributes = cp_parser_attributes_opt (parser);
12743 asm_specification = NULL_TREE;
12744 attributes = NULL_TREE;
12747 /* Peek at the next token. */
12748 token = cp_lexer_peek_token (parser->lexer);
12749 /* Check to see if the token indicates the start of a
12750 function-definition. */
12751 if (function_declarator_p (declarator)
12752 && cp_parser_token_starts_function_definition_p (token))
12754 if (!function_definition_allowed_p)
12756 /* If a function-definition should not appear here, issue an
12758 cp_parser_error (parser,
12759 "a function-definition is not allowed here");
12760 return error_mark_node;
12764 location_t func_brace_location
12765 = cp_lexer_peek_token (parser->lexer)->location;
12767 /* Neither attributes nor an asm-specification are allowed
12768 on a function-definition. */
12769 if (asm_specification)
12770 error ("%Han asm-specification is not allowed "
12771 "on a function-definition",
12772 &asm_spec_start_token->location);
12774 error ("%Hattributes are not allowed on a function-definition",
12775 &attributes_start_token->location);
12776 /* This is a function-definition. */
12777 *function_definition_p = true;
12779 /* Parse the function definition. */
12781 decl = cp_parser_save_member_function_body (parser,
12784 prefix_attributes);
12787 = (cp_parser_function_definition_from_specifiers_and_declarator
12788 (parser, decl_specifiers, prefix_attributes, declarator));
12790 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12792 /* This is where the prologue starts... */
12793 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12794 = func_brace_location;
12803 Only in function declarations for constructors, destructors, and
12804 type conversions can the decl-specifier-seq be omitted.
12806 We explicitly postpone this check past the point where we handle
12807 function-definitions because we tolerate function-definitions
12808 that are missing their return types in some modes. */
12809 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12811 cp_parser_error (parser,
12812 "expected constructor, destructor, or type conversion");
12813 return error_mark_node;
12816 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12817 if (token->type == CPP_EQ
12818 || token->type == CPP_OPEN_PAREN
12819 || token->type == CPP_OPEN_BRACE)
12821 is_initialized = SD_INITIALIZED;
12822 initialization_kind = token->type;
12824 if (token->type == CPP_EQ
12825 && function_declarator_p (declarator))
12827 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12828 if (t2->keyword == RID_DEFAULT)
12829 is_initialized = SD_DEFAULTED;
12830 else if (t2->keyword == RID_DELETE)
12831 is_initialized = SD_DELETED;
12836 /* If the init-declarator isn't initialized and isn't followed by a
12837 `,' or `;', it's not a valid init-declarator. */
12838 if (token->type != CPP_COMMA
12839 && token->type != CPP_SEMICOLON)
12841 cp_parser_error (parser, "expected initializer");
12842 return error_mark_node;
12844 is_initialized = SD_UNINITIALIZED;
12845 initialization_kind = CPP_EOF;
12848 /* Because start_decl has side-effects, we should only call it if we
12849 know we're going ahead. By this point, we know that we cannot
12850 possibly be looking at any other construct. */
12851 cp_parser_commit_to_tentative_parse (parser);
12853 /* If the decl specifiers were bad, issue an error now that we're
12854 sure this was intended to be a declarator. Then continue
12855 declaring the variable(s), as int, to try to cut down on further
12857 if (decl_specifiers->any_specifiers_p
12858 && decl_specifiers->type == error_mark_node)
12860 cp_parser_error (parser, "invalid type in declaration");
12861 decl_specifiers->type = integer_type_node;
12864 /* Check to see whether or not this declaration is a friend. */
12865 friend_p = cp_parser_friend_p (decl_specifiers);
12867 /* Enter the newly declared entry in the symbol table. If we're
12868 processing a declaration in a class-specifier, we wait until
12869 after processing the initializer. */
12872 if (parser->in_unbraced_linkage_specification_p)
12873 decl_specifiers->storage_class = sc_extern;
12874 decl = start_decl (declarator, decl_specifiers,
12875 is_initialized, attributes, prefix_attributes,
12879 /* Enter the SCOPE. That way unqualified names appearing in the
12880 initializer will be looked up in SCOPE. */
12881 pushed_scope = push_scope (scope);
12883 /* Perform deferred access control checks, now that we know in which
12884 SCOPE the declared entity resides. */
12885 if (!member_p && decl)
12887 tree saved_current_function_decl = NULL_TREE;
12889 /* If the entity being declared is a function, pretend that we
12890 are in its scope. If it is a `friend', it may have access to
12891 things that would not otherwise be accessible. */
12892 if (TREE_CODE (decl) == FUNCTION_DECL)
12894 saved_current_function_decl = current_function_decl;
12895 current_function_decl = decl;
12898 /* Perform access checks for template parameters. */
12899 cp_parser_perform_template_parameter_access_checks (checks);
12901 /* Perform the access control checks for the declarator and the
12902 decl-specifiers. */
12903 perform_deferred_access_checks ();
12905 /* Restore the saved value. */
12906 if (TREE_CODE (decl) == FUNCTION_DECL)
12907 current_function_decl = saved_current_function_decl;
12910 /* Parse the initializer. */
12911 initializer = NULL_TREE;
12912 is_direct_init = false;
12913 is_non_constant_init = true;
12914 if (is_initialized)
12916 if (function_declarator_p (declarator))
12918 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12919 if (initialization_kind == CPP_EQ)
12920 initializer = cp_parser_pure_specifier (parser);
12923 /* If the declaration was erroneous, we don't really
12924 know what the user intended, so just silently
12925 consume the initializer. */
12926 if (decl != error_mark_node)
12927 error ("%Hinitializer provided for function",
12928 &initializer_start_token->location);
12929 cp_parser_skip_to_closing_parenthesis (parser,
12930 /*recovering=*/true,
12931 /*or_comma=*/false,
12932 /*consume_paren=*/true);
12936 initializer = cp_parser_initializer (parser,
12938 &is_non_constant_init);
12941 /* The old parser allows attributes to appear after a parenthesized
12942 initializer. Mark Mitchell proposed removing this functionality
12943 on the GCC mailing lists on 2002-08-13. This parser accepts the
12944 attributes -- but ignores them. */
12945 if (cp_parser_allow_gnu_extensions_p (parser)
12946 && initialization_kind == CPP_OPEN_PAREN)
12947 if (cp_parser_attributes_opt (parser))
12948 warning (OPT_Wattributes,
12949 "attributes after parenthesized initializer ignored");
12951 /* For an in-class declaration, use `grokfield' to create the
12957 pop_scope (pushed_scope);
12958 pushed_scope = false;
12960 decl = grokfield (declarator, decl_specifiers,
12961 initializer, !is_non_constant_init,
12962 /*asmspec=*/NULL_TREE,
12963 prefix_attributes);
12964 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12965 cp_parser_save_default_args (parser, decl);
12968 /* Finish processing the declaration. But, skip friend
12970 if (!friend_p && decl && decl != error_mark_node)
12972 cp_finish_decl (decl,
12973 initializer, !is_non_constant_init,
12975 /* If the initializer is in parentheses, then this is
12976 a direct-initialization, which means that an
12977 `explicit' constructor is OK. Otherwise, an
12978 `explicit' constructor cannot be used. */
12979 ((is_direct_init || !is_initialized)
12980 ? 0 : LOOKUP_ONLYCONVERTING));
12982 else if ((cxx_dialect != cxx98) && friend_p
12983 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12984 /* Core issue #226 (C++0x only): A default template-argument
12985 shall not be specified in a friend class template
12987 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12988 /*is_partial=*/0, /*is_friend_decl=*/1);
12990 if (!friend_p && pushed_scope)
12991 pop_scope (pushed_scope);
12996 /* Parse a declarator.
13000 ptr-operator declarator
13002 abstract-declarator:
13003 ptr-operator abstract-declarator [opt]
13004 direct-abstract-declarator
13009 attributes [opt] direct-declarator
13010 attributes [opt] ptr-operator declarator
13012 abstract-declarator:
13013 attributes [opt] ptr-operator abstract-declarator [opt]
13014 attributes [opt] direct-abstract-declarator
13016 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13017 detect constructor, destructor or conversion operators. It is set
13018 to -1 if the declarator is a name, and +1 if it is a
13019 function. Otherwise it is set to zero. Usually you just want to
13020 test for >0, but internally the negative value is used.
13022 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13023 a decl-specifier-seq unless it declares a constructor, destructor,
13024 or conversion. It might seem that we could check this condition in
13025 semantic analysis, rather than parsing, but that makes it difficult
13026 to handle something like `f()'. We want to notice that there are
13027 no decl-specifiers, and therefore realize that this is an
13028 expression, not a declaration.)
13030 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13031 the declarator is a direct-declarator of the form "(...)".
13033 MEMBER_P is true iff this declarator is a member-declarator. */
13035 static cp_declarator *
13036 cp_parser_declarator (cp_parser* parser,
13037 cp_parser_declarator_kind dcl_kind,
13038 int* ctor_dtor_or_conv_p,
13039 bool* parenthesized_p,
13043 cp_declarator *declarator;
13044 enum tree_code code;
13045 cp_cv_quals cv_quals;
13047 tree attributes = NULL_TREE;
13049 /* Assume this is not a constructor, destructor, or type-conversion
13051 if (ctor_dtor_or_conv_p)
13052 *ctor_dtor_or_conv_p = 0;
13054 if (cp_parser_allow_gnu_extensions_p (parser))
13055 attributes = cp_parser_attributes_opt (parser);
13057 /* Peek at the next token. */
13058 token = cp_lexer_peek_token (parser->lexer);
13060 /* Check for the ptr-operator production. */
13061 cp_parser_parse_tentatively (parser);
13062 /* Parse the ptr-operator. */
13063 code = cp_parser_ptr_operator (parser,
13066 /* If that worked, then we have a ptr-operator. */
13067 if (cp_parser_parse_definitely (parser))
13069 /* If a ptr-operator was found, then this declarator was not
13071 if (parenthesized_p)
13072 *parenthesized_p = true;
13073 /* The dependent declarator is optional if we are parsing an
13074 abstract-declarator. */
13075 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13076 cp_parser_parse_tentatively (parser);
13078 /* Parse the dependent declarator. */
13079 declarator = cp_parser_declarator (parser, dcl_kind,
13080 /*ctor_dtor_or_conv_p=*/NULL,
13081 /*parenthesized_p=*/NULL,
13082 /*member_p=*/false);
13084 /* If we are parsing an abstract-declarator, we must handle the
13085 case where the dependent declarator is absent. */
13086 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13087 && !cp_parser_parse_definitely (parser))
13090 declarator = cp_parser_make_indirect_declarator
13091 (code, class_type, cv_quals, declarator);
13093 /* Everything else is a direct-declarator. */
13096 if (parenthesized_p)
13097 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13099 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13100 ctor_dtor_or_conv_p,
13104 if (attributes && declarator && declarator != cp_error_declarator)
13105 declarator->attributes = attributes;
13110 /* Parse a direct-declarator or direct-abstract-declarator.
13114 direct-declarator ( parameter-declaration-clause )
13115 cv-qualifier-seq [opt]
13116 exception-specification [opt]
13117 direct-declarator [ constant-expression [opt] ]
13120 direct-abstract-declarator:
13121 direct-abstract-declarator [opt]
13122 ( parameter-declaration-clause )
13123 cv-qualifier-seq [opt]
13124 exception-specification [opt]
13125 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13126 ( abstract-declarator )
13128 Returns a representation of the declarator. DCL_KIND is
13129 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13130 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13131 we are parsing a direct-declarator. It is
13132 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13133 of ambiguity we prefer an abstract declarator, as per
13134 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13135 cp_parser_declarator. */
13137 static cp_declarator *
13138 cp_parser_direct_declarator (cp_parser* parser,
13139 cp_parser_declarator_kind dcl_kind,
13140 int* ctor_dtor_or_conv_p,
13144 cp_declarator *declarator = NULL;
13145 tree scope = NULL_TREE;
13146 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13147 bool saved_in_declarator_p = parser->in_declarator_p;
13149 tree pushed_scope = NULL_TREE;
13153 /* Peek at the next token. */
13154 token = cp_lexer_peek_token (parser->lexer);
13155 if (token->type == CPP_OPEN_PAREN)
13157 /* This is either a parameter-declaration-clause, or a
13158 parenthesized declarator. When we know we are parsing a
13159 named declarator, it must be a parenthesized declarator
13160 if FIRST is true. For instance, `(int)' is a
13161 parameter-declaration-clause, with an omitted
13162 direct-abstract-declarator. But `((*))', is a
13163 parenthesized abstract declarator. Finally, when T is a
13164 template parameter `(T)' is a
13165 parameter-declaration-clause, and not a parenthesized
13168 We first try and parse a parameter-declaration-clause,
13169 and then try a nested declarator (if FIRST is true).
13171 It is not an error for it not to be a
13172 parameter-declaration-clause, even when FIRST is
13178 The first is the declaration of a function while the
13179 second is the definition of a variable, including its
13182 Having seen only the parenthesis, we cannot know which of
13183 these two alternatives should be selected. Even more
13184 complex are examples like:
13189 The former is a function-declaration; the latter is a
13190 variable initialization.
13192 Thus again, we try a parameter-declaration-clause, and if
13193 that fails, we back out and return. */
13195 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13198 unsigned saved_num_template_parameter_lists;
13199 bool is_declarator = false;
13202 /* In a member-declarator, the only valid interpretation
13203 of a parenthesis is the start of a
13204 parameter-declaration-clause. (It is invalid to
13205 initialize a static data member with a parenthesized
13206 initializer; only the "=" form of initialization is
13209 cp_parser_parse_tentatively (parser);
13211 /* Consume the `('. */
13212 cp_lexer_consume_token (parser->lexer);
13215 /* If this is going to be an abstract declarator, we're
13216 in a declarator and we can't have default args. */
13217 parser->default_arg_ok_p = false;
13218 parser->in_declarator_p = true;
13221 /* Inside the function parameter list, surrounding
13222 template-parameter-lists do not apply. */
13223 saved_num_template_parameter_lists
13224 = parser->num_template_parameter_lists;
13225 parser->num_template_parameter_lists = 0;
13227 begin_scope (sk_function_parms, NULL_TREE);
13229 /* Parse the parameter-declaration-clause. */
13230 params = cp_parser_parameter_declaration_clause (parser);
13232 parser->num_template_parameter_lists
13233 = saved_num_template_parameter_lists;
13235 /* If all went well, parse the cv-qualifier-seq and the
13236 exception-specification. */
13237 if (member_p || cp_parser_parse_definitely (parser))
13239 cp_cv_quals cv_quals;
13240 tree exception_specification;
13243 is_declarator = true;
13245 if (ctor_dtor_or_conv_p)
13246 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13248 /* Consume the `)'. */
13249 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13251 /* Parse the cv-qualifier-seq. */
13252 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13253 /* And the exception-specification. */
13254 exception_specification
13255 = cp_parser_exception_specification_opt (parser);
13258 = cp_parser_late_return_type_opt (parser);
13260 /* Create the function-declarator. */
13261 declarator = make_call_declarator (declarator,
13264 exception_specification,
13266 /* Any subsequent parameter lists are to do with
13267 return type, so are not those of the declared
13269 parser->default_arg_ok_p = false;
13272 /* Remove the function parms from scope. */
13273 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13274 pop_binding (DECL_NAME (t), t);
13278 /* Repeat the main loop. */
13282 /* If this is the first, we can try a parenthesized
13286 bool saved_in_type_id_in_expr_p;
13288 parser->default_arg_ok_p = saved_default_arg_ok_p;
13289 parser->in_declarator_p = saved_in_declarator_p;
13291 /* Consume the `('. */
13292 cp_lexer_consume_token (parser->lexer);
13293 /* Parse the nested declarator. */
13294 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13295 parser->in_type_id_in_expr_p = true;
13297 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13298 /*parenthesized_p=*/NULL,
13300 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13302 /* Expect a `)'. */
13303 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13304 declarator = cp_error_declarator;
13305 if (declarator == cp_error_declarator)
13308 goto handle_declarator;
13310 /* Otherwise, we must be done. */
13314 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13315 && token->type == CPP_OPEN_SQUARE)
13317 /* Parse an array-declarator. */
13320 if (ctor_dtor_or_conv_p)
13321 *ctor_dtor_or_conv_p = 0;
13324 parser->default_arg_ok_p = false;
13325 parser->in_declarator_p = true;
13326 /* Consume the `['. */
13327 cp_lexer_consume_token (parser->lexer);
13328 /* Peek at the next token. */
13329 token = cp_lexer_peek_token (parser->lexer);
13330 /* If the next token is `]', then there is no
13331 constant-expression. */
13332 if (token->type != CPP_CLOSE_SQUARE)
13334 bool non_constant_p;
13337 = cp_parser_constant_expression (parser,
13338 /*allow_non_constant=*/true,
13340 if (!non_constant_p)
13341 bounds = fold_non_dependent_expr (bounds);
13342 /* Normally, the array bound must be an integral constant
13343 expression. However, as an extension, we allow VLAs
13344 in function scopes as long as they aren't part of a
13345 parameter declaration. */
13346 else if (!parser->in_function_body
13347 || current_binding_level->kind == sk_function_parms)
13349 cp_parser_error (parser,
13350 "array bound is not an integer constant");
13351 bounds = error_mark_node;
13353 else if (processing_template_decl && !error_operand_p (bounds))
13355 /* Remember this wasn't a constant-expression. */
13356 bounds = build_nop (TREE_TYPE (bounds), bounds);
13357 TREE_SIDE_EFFECTS (bounds) = 1;
13361 bounds = NULL_TREE;
13362 /* Look for the closing `]'. */
13363 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13365 declarator = cp_error_declarator;
13369 declarator = make_array_declarator (declarator, bounds);
13371 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13373 tree qualifying_scope;
13374 tree unqualified_name;
13375 special_function_kind sfk;
13377 bool pack_expansion_p = false;
13378 cp_token *declarator_id_start_token;
13380 /* Parse a declarator-id */
13381 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13384 cp_parser_parse_tentatively (parser);
13386 /* If we see an ellipsis, we should be looking at a
13388 if (token->type == CPP_ELLIPSIS)
13390 /* Consume the `...' */
13391 cp_lexer_consume_token (parser->lexer);
13393 pack_expansion_p = true;
13397 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13399 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13400 qualifying_scope = parser->scope;
13405 if (!unqualified_name && pack_expansion_p)
13407 /* Check whether an error occurred. */
13408 okay = !cp_parser_error_occurred (parser);
13410 /* We already consumed the ellipsis to mark a
13411 parameter pack, but we have no way to report it,
13412 so abort the tentative parse. We will be exiting
13413 immediately anyway. */
13414 cp_parser_abort_tentative_parse (parser);
13417 okay = cp_parser_parse_definitely (parser);
13420 unqualified_name = error_mark_node;
13421 else if (unqualified_name
13422 && (qualifying_scope
13423 || (TREE_CODE (unqualified_name)
13424 != IDENTIFIER_NODE)))
13426 cp_parser_error (parser, "expected unqualified-id");
13427 unqualified_name = error_mark_node;
13431 if (!unqualified_name)
13433 if (unqualified_name == error_mark_node)
13435 declarator = cp_error_declarator;
13436 pack_expansion_p = false;
13437 declarator->parameter_pack_p = false;
13441 if (qualifying_scope && at_namespace_scope_p ()
13442 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13444 /* In the declaration of a member of a template class
13445 outside of the class itself, the SCOPE will sometimes
13446 be a TYPENAME_TYPE. For example, given:
13448 template <typename T>
13449 int S<T>::R::i = 3;
13451 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13452 this context, we must resolve S<T>::R to an ordinary
13453 type, rather than a typename type.
13455 The reason we normally avoid resolving TYPENAME_TYPEs
13456 is that a specialization of `S' might render
13457 `S<T>::R' not a type. However, if `S' is
13458 specialized, then this `i' will not be used, so there
13459 is no harm in resolving the types here. */
13462 /* Resolve the TYPENAME_TYPE. */
13463 type = resolve_typename_type (qualifying_scope,
13464 /*only_current_p=*/false);
13465 /* If that failed, the declarator is invalid. */
13466 if (TREE_CODE (type) == TYPENAME_TYPE)
13467 error ("%H%<%T::%E%> is not a type",
13468 &declarator_id_start_token->location,
13469 TYPE_CONTEXT (qualifying_scope),
13470 TYPE_IDENTIFIER (qualifying_scope));
13471 qualifying_scope = type;
13476 if (unqualified_name)
13480 if (qualifying_scope
13481 && CLASS_TYPE_P (qualifying_scope))
13482 class_type = qualifying_scope;
13484 class_type = current_class_type;
13486 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13488 tree name_type = TREE_TYPE (unqualified_name);
13489 if (class_type && same_type_p (name_type, class_type))
13491 if (qualifying_scope
13492 && CLASSTYPE_USE_TEMPLATE (name_type))
13494 error ("%Hinvalid use of constructor as a template",
13495 &declarator_id_start_token->location);
13496 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13497 "name the constructor in a qualified name",
13499 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13500 class_type, name_type);
13501 declarator = cp_error_declarator;
13505 unqualified_name = constructor_name (class_type);
13509 /* We do not attempt to print the declarator
13510 here because we do not have enough
13511 information about its original syntactic
13513 cp_parser_error (parser, "invalid declarator");
13514 declarator = cp_error_declarator;
13521 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13522 sfk = sfk_destructor;
13523 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13524 sfk = sfk_conversion;
13525 else if (/* There's no way to declare a constructor
13526 for an anonymous type, even if the type
13527 got a name for linkage purposes. */
13528 !TYPE_WAS_ANONYMOUS (class_type)
13529 && constructor_name_p (unqualified_name,
13532 unqualified_name = constructor_name (class_type);
13533 sfk = sfk_constructor;
13536 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13537 *ctor_dtor_or_conv_p = -1;
13540 declarator = make_id_declarator (qualifying_scope,
13543 declarator->id_loc = token->location;
13544 declarator->parameter_pack_p = pack_expansion_p;
13546 if (pack_expansion_p)
13547 maybe_warn_variadic_templates ();
13549 handle_declarator:;
13550 scope = get_scope_of_declarator (declarator);
13552 /* Any names that appear after the declarator-id for a
13553 member are looked up in the containing scope. */
13554 pushed_scope = push_scope (scope);
13555 parser->in_declarator_p = true;
13556 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13557 || (declarator && declarator->kind == cdk_id))
13558 /* Default args are only allowed on function
13560 parser->default_arg_ok_p = saved_default_arg_ok_p;
13562 parser->default_arg_ok_p = false;
13571 /* For an abstract declarator, we might wind up with nothing at this
13572 point. That's an error; the declarator is not optional. */
13574 cp_parser_error (parser, "expected declarator");
13576 /* If we entered a scope, we must exit it now. */
13578 pop_scope (pushed_scope);
13580 parser->default_arg_ok_p = saved_default_arg_ok_p;
13581 parser->in_declarator_p = saved_in_declarator_p;
13586 /* Parse a ptr-operator.
13589 * cv-qualifier-seq [opt]
13591 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13596 & cv-qualifier-seq [opt]
13598 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13599 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13600 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13601 filled in with the TYPE containing the member. *CV_QUALS is
13602 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13603 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13604 Note that the tree codes returned by this function have nothing
13605 to do with the types of trees that will be eventually be created
13606 to represent the pointer or reference type being parsed. They are
13607 just constants with suggestive names. */
13608 static enum tree_code
13609 cp_parser_ptr_operator (cp_parser* parser,
13611 cp_cv_quals *cv_quals)
13613 enum tree_code code = ERROR_MARK;
13616 /* Assume that it's not a pointer-to-member. */
13618 /* And that there are no cv-qualifiers. */
13619 *cv_quals = TYPE_UNQUALIFIED;
13621 /* Peek at the next token. */
13622 token = cp_lexer_peek_token (parser->lexer);
13624 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13625 if (token->type == CPP_MULT)
13626 code = INDIRECT_REF;
13627 else if (token->type == CPP_AND)
13629 else if ((cxx_dialect != cxx98) &&
13630 token->type == CPP_AND_AND) /* C++0x only */
13631 code = NON_LVALUE_EXPR;
13633 if (code != ERROR_MARK)
13635 /* Consume the `*', `&' or `&&'. */
13636 cp_lexer_consume_token (parser->lexer);
13638 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13639 `&', if we are allowing GNU extensions. (The only qualifier
13640 that can legally appear after `&' is `restrict', but that is
13641 enforced during semantic analysis. */
13642 if (code == INDIRECT_REF
13643 || cp_parser_allow_gnu_extensions_p (parser))
13644 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13648 /* Try the pointer-to-member case. */
13649 cp_parser_parse_tentatively (parser);
13650 /* Look for the optional `::' operator. */
13651 cp_parser_global_scope_opt (parser,
13652 /*current_scope_valid_p=*/false);
13653 /* Look for the nested-name specifier. */
13654 token = cp_lexer_peek_token (parser->lexer);
13655 cp_parser_nested_name_specifier (parser,
13656 /*typename_keyword_p=*/false,
13657 /*check_dependency_p=*/true,
13659 /*is_declaration=*/false);
13660 /* If we found it, and the next token is a `*', then we are
13661 indeed looking at a pointer-to-member operator. */
13662 if (!cp_parser_error_occurred (parser)
13663 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13665 /* Indicate that the `*' operator was used. */
13666 code = INDIRECT_REF;
13668 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13669 error ("%H%qD is a namespace", &token->location, parser->scope);
13672 /* The type of which the member is a member is given by the
13674 *type = parser->scope;
13675 /* The next name will not be qualified. */
13676 parser->scope = NULL_TREE;
13677 parser->qualifying_scope = NULL_TREE;
13678 parser->object_scope = NULL_TREE;
13679 /* Look for the optional cv-qualifier-seq. */
13680 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13683 /* If that didn't work we don't have a ptr-operator. */
13684 if (!cp_parser_parse_definitely (parser))
13685 cp_parser_error (parser, "expected ptr-operator");
13691 /* Parse an (optional) cv-qualifier-seq.
13694 cv-qualifier cv-qualifier-seq [opt]
13705 Returns a bitmask representing the cv-qualifiers. */
13708 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13710 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13715 cp_cv_quals cv_qualifier;
13717 /* Peek at the next token. */
13718 token = cp_lexer_peek_token (parser->lexer);
13719 /* See if it's a cv-qualifier. */
13720 switch (token->keyword)
13723 cv_qualifier = TYPE_QUAL_CONST;
13727 cv_qualifier = TYPE_QUAL_VOLATILE;
13731 cv_qualifier = TYPE_QUAL_RESTRICT;
13735 cv_qualifier = TYPE_UNQUALIFIED;
13742 if (cv_quals & cv_qualifier)
13744 error ("%Hduplicate cv-qualifier", &token->location);
13745 cp_lexer_purge_token (parser->lexer);
13749 cp_lexer_consume_token (parser->lexer);
13750 cv_quals |= cv_qualifier;
13757 /* Parse a late-specified return type, if any. This is not a separate
13758 non-terminal, but part of a function declarator, which looks like
13760 -> trailing-type-specifier-seq abstract-declarator(opt)
13762 Returns the type indicated by the type-id. */
13765 cp_parser_late_return_type_opt (cp_parser* parser)
13769 /* Peek at the next token. */
13770 token = cp_lexer_peek_token (parser->lexer);
13771 /* A late-specified return type is indicated by an initial '->'. */
13772 if (token->type != CPP_DEREF)
13775 /* Consume the ->. */
13776 cp_lexer_consume_token (parser->lexer);
13778 return cp_parser_trailing_type_id (parser);
13781 /* Parse a declarator-id.
13785 :: [opt] nested-name-specifier [opt] type-name
13787 In the `id-expression' case, the value returned is as for
13788 cp_parser_id_expression if the id-expression was an unqualified-id.
13789 If the id-expression was a qualified-id, then a SCOPE_REF is
13790 returned. The first operand is the scope (either a NAMESPACE_DECL
13791 or TREE_TYPE), but the second is still just a representation of an
13795 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13798 /* The expression must be an id-expression. Assume that qualified
13799 names are the names of types so that:
13802 int S<T>::R::i = 3;
13804 will work; we must treat `S<T>::R' as the name of a type.
13805 Similarly, assume that qualified names are templates, where
13809 int S<T>::R<T>::i = 3;
13812 id = cp_parser_id_expression (parser,
13813 /*template_keyword_p=*/false,
13814 /*check_dependency_p=*/false,
13815 /*template_p=*/NULL,
13816 /*declarator_p=*/true,
13818 if (id && BASELINK_P (id))
13819 id = BASELINK_FUNCTIONS (id);
13823 /* Parse a type-id.
13826 type-specifier-seq abstract-declarator [opt]
13828 Returns the TYPE specified. */
13831 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
13832 bool is_trailing_return)
13834 cp_decl_specifier_seq type_specifier_seq;
13835 cp_declarator *abstract_declarator;
13837 /* Parse the type-specifier-seq. */
13838 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13839 is_trailing_return,
13840 &type_specifier_seq);
13841 if (type_specifier_seq.type == error_mark_node)
13842 return error_mark_node;
13844 /* There might or might not be an abstract declarator. */
13845 cp_parser_parse_tentatively (parser);
13846 /* Look for the declarator. */
13847 abstract_declarator
13848 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13849 /*parenthesized_p=*/NULL,
13850 /*member_p=*/false);
13851 /* Check to see if there really was a declarator. */
13852 if (!cp_parser_parse_definitely (parser))
13853 abstract_declarator = NULL;
13855 if (type_specifier_seq.type
13856 && type_uses_auto (type_specifier_seq.type))
13858 /* A type-id with type 'auto' is only ok if the abstract declarator
13859 is a function declarator with a late-specified return type. */
13860 if (abstract_declarator
13861 && abstract_declarator->kind == cdk_function
13862 && abstract_declarator->u.function.late_return_type)
13866 error ("invalid use of %<auto%>");
13867 return error_mark_node;
13871 return groktypename (&type_specifier_seq, abstract_declarator,
13875 static tree cp_parser_type_id (cp_parser *parser)
13877 return cp_parser_type_id_1 (parser, false, false);
13880 static tree cp_parser_template_type_arg (cp_parser *parser)
13882 return cp_parser_type_id_1 (parser, true, false);
13885 static tree cp_parser_trailing_type_id (cp_parser *parser)
13887 return cp_parser_type_id_1 (parser, false, true);
13890 /* Parse a type-specifier-seq.
13892 type-specifier-seq:
13893 type-specifier type-specifier-seq [opt]
13897 type-specifier-seq:
13898 attributes type-specifier-seq [opt]
13900 If IS_CONDITION is true, we are at the start of a "condition",
13901 e.g., we've just seen "if (".
13903 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
13904 i.e. we've just seen "->".
13906 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13909 cp_parser_type_specifier_seq (cp_parser* parser,
13911 bool is_trailing_return,
13912 cp_decl_specifier_seq *type_specifier_seq)
13914 bool seen_type_specifier = false;
13915 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13916 cp_token *start_token = NULL;
13918 /* Clear the TYPE_SPECIFIER_SEQ. */
13919 clear_decl_specs (type_specifier_seq);
13921 /* In the context of a trailing return type, enum E { } is an
13922 elaborated-type-specifier followed by a function-body, not an
13924 if (is_trailing_return)
13925 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
13927 /* Parse the type-specifiers and attributes. */
13930 tree type_specifier;
13931 bool is_cv_qualifier;
13933 /* Check for attributes first. */
13934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13936 type_specifier_seq->attributes =
13937 chainon (type_specifier_seq->attributes,
13938 cp_parser_attributes_opt (parser));
13942 /* record the token of the beginning of the type specifier seq,
13943 for error reporting purposes*/
13945 start_token = cp_lexer_peek_token (parser->lexer);
13947 /* Look for the type-specifier. */
13948 type_specifier = cp_parser_type_specifier (parser,
13950 type_specifier_seq,
13951 /*is_declaration=*/false,
13954 if (!type_specifier)
13956 /* If the first type-specifier could not be found, this is not a
13957 type-specifier-seq at all. */
13958 if (!seen_type_specifier)
13960 cp_parser_error (parser, "expected type-specifier");
13961 type_specifier_seq->type = error_mark_node;
13964 /* If subsequent type-specifiers could not be found, the
13965 type-specifier-seq is complete. */
13969 seen_type_specifier = true;
13970 /* The standard says that a condition can be:
13972 type-specifier-seq declarator = assignment-expression
13979 we should treat the "S" as a declarator, not as a
13980 type-specifier. The standard doesn't say that explicitly for
13981 type-specifier-seq, but it does say that for
13982 decl-specifier-seq in an ordinary declaration. Perhaps it
13983 would be clearer just to allow a decl-specifier-seq here, and
13984 then add a semantic restriction that if any decl-specifiers
13985 that are not type-specifiers appear, the program is invalid. */
13986 if (is_condition && !is_cv_qualifier)
13987 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13990 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13993 /* Parse a parameter-declaration-clause.
13995 parameter-declaration-clause:
13996 parameter-declaration-list [opt] ... [opt]
13997 parameter-declaration-list , ...
13999 Returns a representation for the parameter declarations. A return
14000 value of NULL indicates a parameter-declaration-clause consisting
14001 only of an ellipsis. */
14004 cp_parser_parameter_declaration_clause (cp_parser* parser)
14011 /* Peek at the next token. */
14012 token = cp_lexer_peek_token (parser->lexer);
14013 /* Check for trivial parameter-declaration-clauses. */
14014 if (token->type == CPP_ELLIPSIS)
14016 /* Consume the `...' token. */
14017 cp_lexer_consume_token (parser->lexer);
14020 else if (token->type == CPP_CLOSE_PAREN)
14021 /* There are no parameters. */
14023 #ifndef NO_IMPLICIT_EXTERN_C
14024 if (in_system_header && current_class_type == NULL
14025 && current_lang_name == lang_name_c)
14029 return void_list_node;
14031 /* Check for `(void)', too, which is a special case. */
14032 else if (token->keyword == RID_VOID
14033 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14034 == CPP_CLOSE_PAREN))
14036 /* Consume the `void' token. */
14037 cp_lexer_consume_token (parser->lexer);
14038 /* There are no parameters. */
14039 return void_list_node;
14042 /* Parse the parameter-declaration-list. */
14043 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14044 /* If a parse error occurred while parsing the
14045 parameter-declaration-list, then the entire
14046 parameter-declaration-clause is erroneous. */
14050 /* Peek at the next token. */
14051 token = cp_lexer_peek_token (parser->lexer);
14052 /* If it's a `,', the clause should terminate with an ellipsis. */
14053 if (token->type == CPP_COMMA)
14055 /* Consume the `,'. */
14056 cp_lexer_consume_token (parser->lexer);
14057 /* Expect an ellipsis. */
14059 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14061 /* It might also be `...' if the optional trailing `,' was
14063 else if (token->type == CPP_ELLIPSIS)
14065 /* Consume the `...' token. */
14066 cp_lexer_consume_token (parser->lexer);
14067 /* And remember that we saw it. */
14071 ellipsis_p = false;
14073 /* Finish the parameter list. */
14075 parameters = chainon (parameters, void_list_node);
14080 /* Parse a parameter-declaration-list.
14082 parameter-declaration-list:
14083 parameter-declaration
14084 parameter-declaration-list , parameter-declaration
14086 Returns a representation of the parameter-declaration-list, as for
14087 cp_parser_parameter_declaration_clause. However, the
14088 `void_list_node' is never appended to the list. Upon return,
14089 *IS_ERROR will be true iff an error occurred. */
14092 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14094 tree parameters = NULL_TREE;
14095 tree *tail = ¶meters;
14096 bool saved_in_unbraced_linkage_specification_p;
14098 /* Assume all will go well. */
14100 /* The special considerations that apply to a function within an
14101 unbraced linkage specifications do not apply to the parameters
14102 to the function. */
14103 saved_in_unbraced_linkage_specification_p
14104 = parser->in_unbraced_linkage_specification_p;
14105 parser->in_unbraced_linkage_specification_p = false;
14107 /* Look for more parameters. */
14110 cp_parameter_declarator *parameter;
14111 tree decl = error_mark_node;
14112 bool parenthesized_p;
14113 /* Parse the parameter. */
14115 = cp_parser_parameter_declaration (parser,
14116 /*template_parm_p=*/false,
14119 /* We don't know yet if the enclosing context is deprecated, so wait
14120 and warn in grokparms if appropriate. */
14121 deprecated_state = DEPRECATED_SUPPRESS;
14124 decl = grokdeclarator (parameter->declarator,
14125 ¶meter->decl_specifiers,
14127 parameter->default_argument != NULL_TREE,
14128 ¶meter->decl_specifiers.attributes);
14130 deprecated_state = DEPRECATED_NORMAL;
14132 /* If a parse error occurred parsing the parameter declaration,
14133 then the entire parameter-declaration-list is erroneous. */
14134 if (decl == error_mark_node)
14137 parameters = error_mark_node;
14141 if (parameter->decl_specifiers.attributes)
14142 cplus_decl_attributes (&decl,
14143 parameter->decl_specifiers.attributes,
14145 if (DECL_NAME (decl))
14146 decl = pushdecl (decl);
14148 /* Add the new parameter to the list. */
14149 *tail = build_tree_list (parameter->default_argument, decl);
14150 tail = &TREE_CHAIN (*tail);
14152 /* Peek at the next token. */
14153 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14154 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14155 /* These are for Objective-C++ */
14156 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14157 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14158 /* The parameter-declaration-list is complete. */
14160 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14164 /* Peek at the next token. */
14165 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14166 /* If it's an ellipsis, then the list is complete. */
14167 if (token->type == CPP_ELLIPSIS)
14169 /* Otherwise, there must be more parameters. Consume the
14171 cp_lexer_consume_token (parser->lexer);
14172 /* When parsing something like:
14174 int i(float f, double d)
14176 we can tell after seeing the declaration for "f" that we
14177 are not looking at an initialization of a variable "i",
14178 but rather at the declaration of a function "i".
14180 Due to the fact that the parsing of template arguments
14181 (as specified to a template-id) requires backtracking we
14182 cannot use this technique when inside a template argument
14184 if (!parser->in_template_argument_list_p
14185 && !parser->in_type_id_in_expr_p
14186 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14187 /* However, a parameter-declaration of the form
14188 "foat(f)" (which is a valid declaration of a
14189 parameter "f") can also be interpreted as an
14190 expression (the conversion of "f" to "float"). */
14191 && !parenthesized_p)
14192 cp_parser_commit_to_tentative_parse (parser);
14196 cp_parser_error (parser, "expected %<,%> or %<...%>");
14197 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14198 cp_parser_skip_to_closing_parenthesis (parser,
14199 /*recovering=*/true,
14200 /*or_comma=*/false,
14201 /*consume_paren=*/false);
14206 parser->in_unbraced_linkage_specification_p
14207 = saved_in_unbraced_linkage_specification_p;
14212 /* Parse a parameter declaration.
14214 parameter-declaration:
14215 decl-specifier-seq ... [opt] declarator
14216 decl-specifier-seq declarator = assignment-expression
14217 decl-specifier-seq ... [opt] abstract-declarator [opt]
14218 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14220 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14221 declares a template parameter. (In that case, a non-nested `>'
14222 token encountered during the parsing of the assignment-expression
14223 is not interpreted as a greater-than operator.)
14225 Returns a representation of the parameter, or NULL if an error
14226 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14227 true iff the declarator is of the form "(p)". */
14229 static cp_parameter_declarator *
14230 cp_parser_parameter_declaration (cp_parser *parser,
14231 bool template_parm_p,
14232 bool *parenthesized_p)
14234 int declares_class_or_enum;
14235 bool greater_than_is_operator_p;
14236 cp_decl_specifier_seq decl_specifiers;
14237 cp_declarator *declarator;
14238 tree default_argument;
14239 cp_token *token = NULL, *declarator_token_start = NULL;
14240 const char *saved_message;
14242 /* In a template parameter, `>' is not an operator.
14246 When parsing a default template-argument for a non-type
14247 template-parameter, the first non-nested `>' is taken as the end
14248 of the template parameter-list rather than a greater-than
14250 greater_than_is_operator_p = !template_parm_p;
14252 /* Type definitions may not appear in parameter types. */
14253 saved_message = parser->type_definition_forbidden_message;
14254 parser->type_definition_forbidden_message
14255 = "types may not be defined in parameter types";
14257 /* Parse the declaration-specifiers. */
14258 cp_parser_decl_specifier_seq (parser,
14259 CP_PARSER_FLAGS_NONE,
14261 &declares_class_or_enum);
14262 /* If an error occurred, there's no reason to attempt to parse the
14263 rest of the declaration. */
14264 if (cp_parser_error_occurred (parser))
14266 parser->type_definition_forbidden_message = saved_message;
14270 /* Peek at the next token. */
14271 token = cp_lexer_peek_token (parser->lexer);
14273 /* If the next token is a `)', `,', `=', `>', or `...', then there
14274 is no declarator. However, when variadic templates are enabled,
14275 there may be a declarator following `...'. */
14276 if (token->type == CPP_CLOSE_PAREN
14277 || token->type == CPP_COMMA
14278 || token->type == CPP_EQ
14279 || token->type == CPP_GREATER)
14282 if (parenthesized_p)
14283 *parenthesized_p = false;
14285 /* Otherwise, there should be a declarator. */
14288 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14289 parser->default_arg_ok_p = false;
14291 /* After seeing a decl-specifier-seq, if the next token is not a
14292 "(", there is no possibility that the code is a valid
14293 expression. Therefore, if parsing tentatively, we commit at
14295 if (!parser->in_template_argument_list_p
14296 /* In an expression context, having seen:
14300 we cannot be sure whether we are looking at a
14301 function-type (taking a "char" as a parameter) or a cast
14302 of some object of type "char" to "int". */
14303 && !parser->in_type_id_in_expr_p
14304 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14305 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14306 cp_parser_commit_to_tentative_parse (parser);
14307 /* Parse the declarator. */
14308 declarator_token_start = token;
14309 declarator = cp_parser_declarator (parser,
14310 CP_PARSER_DECLARATOR_EITHER,
14311 /*ctor_dtor_or_conv_p=*/NULL,
14313 /*member_p=*/false);
14314 parser->default_arg_ok_p = saved_default_arg_ok_p;
14315 /* After the declarator, allow more attributes. */
14316 decl_specifiers.attributes
14317 = chainon (decl_specifiers.attributes,
14318 cp_parser_attributes_opt (parser));
14321 /* If the next token is an ellipsis, and we have not seen a
14322 declarator name, and the type of the declarator contains parameter
14323 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14324 a parameter pack expansion expression. Otherwise, leave the
14325 ellipsis for a C-style variadic function. */
14326 token = cp_lexer_peek_token (parser->lexer);
14327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14329 tree type = decl_specifiers.type;
14331 if (type && DECL_P (type))
14332 type = TREE_TYPE (type);
14335 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14336 && declarator_can_be_parameter_pack (declarator)
14337 && (!declarator || !declarator->parameter_pack_p)
14338 && uses_parameter_packs (type))
14340 /* Consume the `...'. */
14341 cp_lexer_consume_token (parser->lexer);
14342 maybe_warn_variadic_templates ();
14344 /* Build a pack expansion type */
14346 declarator->parameter_pack_p = true;
14348 decl_specifiers.type = make_pack_expansion (type);
14352 /* The restriction on defining new types applies only to the type
14353 of the parameter, not to the default argument. */
14354 parser->type_definition_forbidden_message = saved_message;
14356 /* If the next token is `=', then process a default argument. */
14357 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14359 /* Consume the `='. */
14360 cp_lexer_consume_token (parser->lexer);
14362 /* If we are defining a class, then the tokens that make up the
14363 default argument must be saved and processed later. */
14364 if (!template_parm_p && at_class_scope_p ()
14365 && TYPE_BEING_DEFINED (current_class_type))
14367 unsigned depth = 0;
14368 int maybe_template_id = 0;
14369 cp_token *first_token;
14372 /* Add tokens until we have processed the entire default
14373 argument. We add the range [first_token, token). */
14374 first_token = cp_lexer_peek_token (parser->lexer);
14379 /* Peek at the next token. */
14380 token = cp_lexer_peek_token (parser->lexer);
14381 /* What we do depends on what token we have. */
14382 switch (token->type)
14384 /* In valid code, a default argument must be
14385 immediately followed by a `,' `)', or `...'. */
14387 if (depth == 0 && maybe_template_id)
14389 /* If we've seen a '<', we might be in a
14390 template-argument-list. Until Core issue 325 is
14391 resolved, we don't know how this situation ought
14392 to be handled, so try to DTRT. We check whether
14393 what comes after the comma is a valid parameter
14394 declaration list. If it is, then the comma ends
14395 the default argument; otherwise the default
14396 argument continues. */
14397 bool error = false;
14400 /* Set ITALP so cp_parser_parameter_declaration_list
14401 doesn't decide to commit to this parse. */
14402 bool saved_italp = parser->in_template_argument_list_p;
14403 parser->in_template_argument_list_p = true;
14405 cp_parser_parse_tentatively (parser);
14406 cp_lexer_consume_token (parser->lexer);
14407 begin_scope (sk_function_parms, NULL_TREE);
14408 cp_parser_parameter_declaration_list (parser, &error);
14409 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14410 pop_binding (DECL_NAME (t), t);
14412 if (!cp_parser_error_occurred (parser) && !error)
14414 cp_parser_abort_tentative_parse (parser);
14416 parser->in_template_argument_list_p = saved_italp;
14419 case CPP_CLOSE_PAREN:
14421 /* If we run into a non-nested `;', `}', or `]',
14422 then the code is invalid -- but the default
14423 argument is certainly over. */
14424 case CPP_SEMICOLON:
14425 case CPP_CLOSE_BRACE:
14426 case CPP_CLOSE_SQUARE:
14429 /* Update DEPTH, if necessary. */
14430 else if (token->type == CPP_CLOSE_PAREN
14431 || token->type == CPP_CLOSE_BRACE
14432 || token->type == CPP_CLOSE_SQUARE)
14436 case CPP_OPEN_PAREN:
14437 case CPP_OPEN_SQUARE:
14438 case CPP_OPEN_BRACE:
14444 /* This might be the comparison operator, or it might
14445 start a template argument list. */
14446 ++maybe_template_id;
14450 if (cxx_dialect == cxx98)
14452 /* Fall through for C++0x, which treats the `>>'
14453 operator like two `>' tokens in certain
14459 /* This might be an operator, or it might close a
14460 template argument list. But if a previous '<'
14461 started a template argument list, this will have
14462 closed it, so we can't be in one anymore. */
14463 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14464 if (maybe_template_id < 0)
14465 maybe_template_id = 0;
14469 /* If we run out of tokens, issue an error message. */
14471 case CPP_PRAGMA_EOL:
14472 error ("%Hfile ends in default argument", &token->location);
14478 /* In these cases, we should look for template-ids.
14479 For example, if the default argument is
14480 `X<int, double>()', we need to do name lookup to
14481 figure out whether or not `X' is a template; if
14482 so, the `,' does not end the default argument.
14484 That is not yet done. */
14491 /* If we've reached the end, stop. */
14495 /* Add the token to the token block. */
14496 token = cp_lexer_consume_token (parser->lexer);
14499 /* Create a DEFAULT_ARG to represent the unparsed default
14501 default_argument = make_node (DEFAULT_ARG);
14502 DEFARG_TOKENS (default_argument)
14503 = cp_token_cache_new (first_token, token);
14504 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14506 /* Outside of a class definition, we can just parse the
14507 assignment-expression. */
14510 token = cp_lexer_peek_token (parser->lexer);
14512 = cp_parser_default_argument (parser, template_parm_p);
14515 if (!parser->default_arg_ok_p)
14517 if (flag_permissive)
14518 warning (0, "deprecated use of default argument for parameter of non-function");
14521 error ("%Hdefault arguments are only "
14522 "permitted for function parameters",
14524 default_argument = NULL_TREE;
14527 else if ((declarator && declarator->parameter_pack_p)
14528 || (decl_specifiers.type
14529 && PACK_EXPANSION_P (decl_specifiers.type)))
14531 const char* kind = template_parm_p? "template " : "";
14533 /* Find the name of the parameter pack. */
14534 cp_declarator *id_declarator = declarator;
14535 while (id_declarator && id_declarator->kind != cdk_id)
14536 id_declarator = id_declarator->declarator;
14538 if (id_declarator && id_declarator->kind == cdk_id)
14539 error ("%H%sparameter pack %qD cannot have a default argument",
14540 &declarator_token_start->location,
14541 kind, id_declarator->u.id.unqualified_name);
14543 error ("%H%sparameter pack cannot have a default argument",
14544 &declarator_token_start->location, kind);
14546 default_argument = NULL_TREE;
14550 default_argument = NULL_TREE;
14552 return make_parameter_declarator (&decl_specifiers,
14557 /* Parse a default argument and return it.
14559 TEMPLATE_PARM_P is true if this is a default argument for a
14560 non-type template parameter. */
14562 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14564 tree default_argument = NULL_TREE;
14565 bool saved_greater_than_is_operator_p;
14566 bool saved_local_variables_forbidden_p;
14568 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14570 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14571 parser->greater_than_is_operator_p = !template_parm_p;
14572 /* Local variable names (and the `this' keyword) may not
14573 appear in a default argument. */
14574 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14575 parser->local_variables_forbidden_p = true;
14576 /* The default argument expression may cause implicitly
14577 defined member functions to be synthesized, which will
14578 result in garbage collection. We must treat this
14579 situation as if we were within the body of function so as
14580 to avoid collecting live data on the stack. */
14582 /* Parse the assignment-expression. */
14583 if (template_parm_p)
14584 push_deferring_access_checks (dk_no_deferred);
14586 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14587 if (template_parm_p)
14588 pop_deferring_access_checks ();
14589 /* Restore saved state. */
14591 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14592 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14594 return default_argument;
14597 /* Parse a function-body.
14600 compound_statement */
14603 cp_parser_function_body (cp_parser *parser)
14605 cp_parser_compound_statement (parser, NULL, false);
14608 /* Parse a ctor-initializer-opt followed by a function-body. Return
14609 true if a ctor-initializer was present. */
14612 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14615 bool ctor_initializer_p;
14617 /* Begin the function body. */
14618 body = begin_function_body ();
14619 /* Parse the optional ctor-initializer. */
14620 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14621 /* Parse the function-body. */
14622 cp_parser_function_body (parser);
14623 /* Finish the function body. */
14624 finish_function_body (body);
14626 return ctor_initializer_p;
14629 /* Parse an initializer.
14632 = initializer-clause
14633 ( expression-list )
14635 Returns an expression representing the initializer. If no
14636 initializer is present, NULL_TREE is returned.
14638 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14639 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14640 set to TRUE if there is no initializer present. If there is an
14641 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14642 is set to true; otherwise it is set to false. */
14645 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14646 bool* non_constant_p)
14651 /* Peek at the next token. */
14652 token = cp_lexer_peek_token (parser->lexer);
14654 /* Let our caller know whether or not this initializer was
14656 *is_direct_init = (token->type != CPP_EQ);
14657 /* Assume that the initializer is constant. */
14658 *non_constant_p = false;
14660 if (token->type == CPP_EQ)
14662 /* Consume the `='. */
14663 cp_lexer_consume_token (parser->lexer);
14664 /* Parse the initializer-clause. */
14665 init = cp_parser_initializer_clause (parser, non_constant_p);
14667 else if (token->type == CPP_OPEN_PAREN)
14668 init = cp_parser_parenthesized_expression_list (parser, false,
14670 /*allow_expansion_p=*/true,
14672 else if (token->type == CPP_OPEN_BRACE)
14674 maybe_warn_cpp0x ("extended initializer lists");
14675 init = cp_parser_braced_list (parser, non_constant_p);
14676 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14680 /* Anything else is an error. */
14681 cp_parser_error (parser, "expected initializer");
14682 init = error_mark_node;
14688 /* Parse an initializer-clause.
14690 initializer-clause:
14691 assignment-expression
14694 Returns an expression representing the initializer.
14696 If the `assignment-expression' production is used the value
14697 returned is simply a representation for the expression.
14699 Otherwise, calls cp_parser_braced_list. */
14702 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14706 /* Assume the expression is constant. */
14707 *non_constant_p = false;
14709 /* If it is not a `{', then we are looking at an
14710 assignment-expression. */
14711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14714 = cp_parser_constant_expression (parser,
14715 /*allow_non_constant_p=*/true,
14717 if (!*non_constant_p)
14718 initializer = fold_non_dependent_expr (initializer);
14721 initializer = cp_parser_braced_list (parser, non_constant_p);
14723 return initializer;
14726 /* Parse a brace-enclosed initializer list.
14729 { initializer-list , [opt] }
14732 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14733 the elements of the initializer-list (or NULL, if the last
14734 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14735 NULL_TREE. There is no way to detect whether or not the optional
14736 trailing `,' was provided. NON_CONSTANT_P is as for
14737 cp_parser_initializer. */
14740 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14744 /* Consume the `{' token. */
14745 cp_lexer_consume_token (parser->lexer);
14746 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14747 initializer = make_node (CONSTRUCTOR);
14748 /* If it's not a `}', then there is a non-trivial initializer. */
14749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14751 /* Parse the initializer list. */
14752 CONSTRUCTOR_ELTS (initializer)
14753 = cp_parser_initializer_list (parser, non_constant_p);
14754 /* A trailing `,' token is allowed. */
14755 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14756 cp_lexer_consume_token (parser->lexer);
14758 /* Now, there should be a trailing `}'. */
14759 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14760 TREE_TYPE (initializer) = init_list_type_node;
14761 return initializer;
14764 /* Parse an initializer-list.
14767 initializer-clause ... [opt]
14768 initializer-list , initializer-clause ... [opt]
14773 identifier : initializer-clause
14774 initializer-list, identifier : initializer-clause
14776 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14777 for the initializer. If the INDEX of the elt is non-NULL, it is the
14778 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14779 as for cp_parser_initializer. */
14781 static VEC(constructor_elt,gc) *
14782 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14784 VEC(constructor_elt,gc) *v = NULL;
14786 /* Assume all of the expressions are constant. */
14787 *non_constant_p = false;
14789 /* Parse the rest of the list. */
14795 bool clause_non_constant_p;
14797 /* If the next token is an identifier and the following one is a
14798 colon, we are looking at the GNU designated-initializer
14800 if (cp_parser_allow_gnu_extensions_p (parser)
14801 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14802 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14804 /* Warn the user that they are using an extension. */
14805 pedwarn (input_location, OPT_pedantic,
14806 "ISO C++ does not allow designated initializers");
14807 /* Consume the identifier. */
14808 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14809 /* Consume the `:'. */
14810 cp_lexer_consume_token (parser->lexer);
14813 identifier = NULL_TREE;
14815 /* Parse the initializer. */
14816 initializer = cp_parser_initializer_clause (parser,
14817 &clause_non_constant_p);
14818 /* If any clause is non-constant, so is the entire initializer. */
14819 if (clause_non_constant_p)
14820 *non_constant_p = true;
14822 /* If we have an ellipsis, this is an initializer pack
14824 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14826 /* Consume the `...'. */
14827 cp_lexer_consume_token (parser->lexer);
14829 /* Turn the initializer into an initializer expansion. */
14830 initializer = make_pack_expansion (initializer);
14833 /* Add it to the vector. */
14834 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14836 /* If the next token is not a comma, we have reached the end of
14838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14841 /* Peek at the next token. */
14842 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14843 /* If the next token is a `}', then we're still done. An
14844 initializer-clause can have a trailing `,' after the
14845 initializer-list and before the closing `}'. */
14846 if (token->type == CPP_CLOSE_BRACE)
14849 /* Consume the `,' token. */
14850 cp_lexer_consume_token (parser->lexer);
14856 /* Classes [gram.class] */
14858 /* Parse a class-name.
14864 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14865 to indicate that names looked up in dependent types should be
14866 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14867 keyword has been used to indicate that the name that appears next
14868 is a template. TAG_TYPE indicates the explicit tag given before
14869 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14870 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14871 is the class being defined in a class-head.
14873 Returns the TYPE_DECL representing the class. */
14876 cp_parser_class_name (cp_parser *parser,
14877 bool typename_keyword_p,
14878 bool template_keyword_p,
14879 enum tag_types tag_type,
14880 bool check_dependency_p,
14882 bool is_declaration)
14888 tree identifier = NULL_TREE;
14890 /* All class-names start with an identifier. */
14891 token = cp_lexer_peek_token (parser->lexer);
14892 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14894 cp_parser_error (parser, "expected class-name");
14895 return error_mark_node;
14898 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14899 to a template-id, so we save it here. */
14900 scope = parser->scope;
14901 if (scope == error_mark_node)
14902 return error_mark_node;
14904 /* Any name names a type if we're following the `typename' keyword
14905 in a qualified name where the enclosing scope is type-dependent. */
14906 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14907 && dependent_type_p (scope));
14908 /* Handle the common case (an identifier, but not a template-id)
14910 if (token->type == CPP_NAME
14911 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14913 cp_token *identifier_token;
14916 /* Look for the identifier. */
14917 identifier_token = cp_lexer_peek_token (parser->lexer);
14918 ambiguous_p = identifier_token->ambiguous_p;
14919 identifier = cp_parser_identifier (parser);
14920 /* If the next token isn't an identifier, we are certainly not
14921 looking at a class-name. */
14922 if (identifier == error_mark_node)
14923 decl = error_mark_node;
14924 /* If we know this is a type-name, there's no need to look it
14926 else if (typename_p)
14930 tree ambiguous_decls;
14931 /* If we already know that this lookup is ambiguous, then
14932 we've already issued an error message; there's no reason
14936 cp_parser_simulate_error (parser);
14937 return error_mark_node;
14939 /* If the next token is a `::', then the name must be a type
14942 [basic.lookup.qual]
14944 During the lookup for a name preceding the :: scope
14945 resolution operator, object, function, and enumerator
14946 names are ignored. */
14947 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14948 tag_type = typename_type;
14949 /* Look up the name. */
14950 decl = cp_parser_lookup_name (parser, identifier,
14952 /*is_template=*/false,
14953 /*is_namespace=*/false,
14954 check_dependency_p,
14956 identifier_token->location);
14957 if (ambiguous_decls)
14959 error ("%Hreference to %qD is ambiguous",
14960 &identifier_token->location, identifier);
14961 print_candidates (ambiguous_decls);
14962 if (cp_parser_parsing_tentatively (parser))
14964 identifier_token->ambiguous_p = true;
14965 cp_parser_simulate_error (parser);
14967 return error_mark_node;
14973 /* Try a template-id. */
14974 decl = cp_parser_template_id (parser, template_keyword_p,
14975 check_dependency_p,
14977 if (decl == error_mark_node)
14978 return error_mark_node;
14981 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14983 /* If this is a typename, create a TYPENAME_TYPE. */
14984 if (typename_p && decl != error_mark_node)
14986 decl = make_typename_type (scope, decl, typename_type,
14987 /*complain=*/tf_error);
14988 if (decl != error_mark_node)
14989 decl = TYPE_NAME (decl);
14992 /* Check to see that it is really the name of a class. */
14993 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14994 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14995 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14996 /* Situations like this:
14998 template <typename T> struct A {
14999 typename T::template X<int>::I i;
15002 are problematic. Is `T::template X<int>' a class-name? The
15003 standard does not seem to be definitive, but there is no other
15004 valid interpretation of the following `::'. Therefore, those
15005 names are considered class-names. */
15007 decl = make_typename_type (scope, decl, tag_type, tf_error);
15008 if (decl != error_mark_node)
15009 decl = TYPE_NAME (decl);
15011 else if (TREE_CODE (decl) != TYPE_DECL
15012 || TREE_TYPE (decl) == error_mark_node
15013 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15014 decl = error_mark_node;
15016 if (decl == error_mark_node)
15017 cp_parser_error (parser, "expected class-name");
15018 else if (identifier && !parser->scope)
15019 maybe_note_name_used_in_class (identifier, decl);
15024 /* Parse a class-specifier.
15027 class-head { member-specification [opt] }
15029 Returns the TREE_TYPE representing the class. */
15032 cp_parser_class_specifier (cp_parser* parser)
15036 tree attributes = NULL_TREE;
15037 int has_trailing_semicolon;
15038 bool nested_name_specifier_p;
15039 unsigned saved_num_template_parameter_lists;
15040 bool saved_in_function_body;
15041 bool saved_in_unbraced_linkage_specification_p;
15042 tree old_scope = NULL_TREE;
15043 tree scope = NULL_TREE;
15046 push_deferring_access_checks (dk_no_deferred);
15048 /* Parse the class-head. */
15049 type = cp_parser_class_head (parser,
15050 &nested_name_specifier_p,
15053 /* If the class-head was a semantic disaster, skip the entire body
15057 cp_parser_skip_to_end_of_block_or_statement (parser);
15058 pop_deferring_access_checks ();
15059 return error_mark_node;
15062 /* Look for the `{'. */
15063 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15065 pop_deferring_access_checks ();
15066 return error_mark_node;
15069 /* Process the base classes. If they're invalid, skip the
15070 entire class body. */
15071 if (!xref_basetypes (type, bases))
15073 /* Consuming the closing brace yields better error messages
15075 if (cp_parser_skip_to_closing_brace (parser))
15076 cp_lexer_consume_token (parser->lexer);
15077 pop_deferring_access_checks ();
15078 return error_mark_node;
15081 /* Issue an error message if type-definitions are forbidden here. */
15082 cp_parser_check_type_definition (parser);
15083 /* Remember that we are defining one more class. */
15084 ++parser->num_classes_being_defined;
15085 /* Inside the class, surrounding template-parameter-lists do not
15087 saved_num_template_parameter_lists
15088 = parser->num_template_parameter_lists;
15089 parser->num_template_parameter_lists = 0;
15090 /* We are not in a function body. */
15091 saved_in_function_body = parser->in_function_body;
15092 parser->in_function_body = false;
15093 /* We are not immediately inside an extern "lang" block. */
15094 saved_in_unbraced_linkage_specification_p
15095 = parser->in_unbraced_linkage_specification_p;
15096 parser->in_unbraced_linkage_specification_p = false;
15098 /* Start the class. */
15099 if (nested_name_specifier_p)
15101 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15102 old_scope = push_inner_scope (scope);
15104 type = begin_class_definition (type, attributes);
15106 if (type == error_mark_node)
15107 /* If the type is erroneous, skip the entire body of the class. */
15108 cp_parser_skip_to_closing_brace (parser);
15110 /* Parse the member-specification. */
15111 cp_parser_member_specification_opt (parser);
15113 /* Look for the trailing `}'. */
15114 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15115 /* We get better error messages by noticing a common problem: a
15116 missing trailing `;'. */
15117 token = cp_lexer_peek_token (parser->lexer);
15118 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15119 /* Look for trailing attributes to apply to this class. */
15120 if (cp_parser_allow_gnu_extensions_p (parser))
15121 attributes = cp_parser_attributes_opt (parser);
15122 if (type != error_mark_node)
15123 type = finish_struct (type, attributes);
15124 if (nested_name_specifier_p)
15125 pop_inner_scope (old_scope, scope);
15126 /* If this class is not itself within the scope of another class,
15127 then we need to parse the bodies of all of the queued function
15128 definitions. Note that the queued functions defined in a class
15129 are not always processed immediately following the
15130 class-specifier for that class. Consider:
15133 struct B { void f() { sizeof (A); } };
15136 If `f' were processed before the processing of `A' were
15137 completed, there would be no way to compute the size of `A'.
15138 Note that the nesting we are interested in here is lexical --
15139 not the semantic nesting given by TYPE_CONTEXT. In particular,
15142 struct A { struct B; };
15143 struct A::B { void f() { } };
15145 there is no need to delay the parsing of `A::B::f'. */
15146 if (--parser->num_classes_being_defined == 0)
15150 tree class_type = NULL_TREE;
15151 tree pushed_scope = NULL_TREE;
15153 /* In a first pass, parse default arguments to the functions.
15154 Then, in a second pass, parse the bodies of the functions.
15155 This two-phased approach handles cases like:
15163 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15164 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15165 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15166 TREE_PURPOSE (parser->unparsed_functions_queues)
15167 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15169 fn = TREE_VALUE (queue_entry);
15170 /* If there are default arguments that have not yet been processed,
15171 take care of them now. */
15172 if (class_type != TREE_PURPOSE (queue_entry))
15175 pop_scope (pushed_scope);
15176 class_type = TREE_PURPOSE (queue_entry);
15177 pushed_scope = push_scope (class_type);
15179 /* Make sure that any template parameters are in scope. */
15180 maybe_begin_member_template_processing (fn);
15181 /* Parse the default argument expressions. */
15182 cp_parser_late_parsing_default_args (parser, fn);
15183 /* Remove any template parameters from the symbol table. */
15184 maybe_end_member_template_processing ();
15187 pop_scope (pushed_scope);
15188 /* Now parse the body of the functions. */
15189 for (TREE_VALUE (parser->unparsed_functions_queues)
15190 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15191 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15192 TREE_VALUE (parser->unparsed_functions_queues)
15193 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15195 /* Figure out which function we need to process. */
15196 fn = TREE_VALUE (queue_entry);
15197 /* Parse the function. */
15198 cp_parser_late_parsing_for_member (parser, fn);
15202 /* Put back any saved access checks. */
15203 pop_deferring_access_checks ();
15205 /* Restore saved state. */
15206 parser->in_function_body = saved_in_function_body;
15207 parser->num_template_parameter_lists
15208 = saved_num_template_parameter_lists;
15209 parser->in_unbraced_linkage_specification_p
15210 = saved_in_unbraced_linkage_specification_p;
15215 /* Parse a class-head.
15218 class-key identifier [opt] base-clause [opt]
15219 class-key nested-name-specifier identifier base-clause [opt]
15220 class-key nested-name-specifier [opt] template-id
15224 class-key attributes identifier [opt] base-clause [opt]
15225 class-key attributes nested-name-specifier identifier base-clause [opt]
15226 class-key attributes nested-name-specifier [opt] template-id
15229 Upon return BASES is initialized to the list of base classes (or
15230 NULL, if there are none) in the same form returned by
15231 cp_parser_base_clause.
15233 Returns the TYPE of the indicated class. Sets
15234 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15235 involving a nested-name-specifier was used, and FALSE otherwise.
15237 Returns error_mark_node if this is not a class-head.
15239 Returns NULL_TREE if the class-head is syntactically valid, but
15240 semantically invalid in a way that means we should skip the entire
15241 body of the class. */
15244 cp_parser_class_head (cp_parser* parser,
15245 bool* nested_name_specifier_p,
15246 tree *attributes_p,
15249 tree nested_name_specifier;
15250 enum tag_types class_key;
15251 tree id = NULL_TREE;
15252 tree type = NULL_TREE;
15254 bool template_id_p = false;
15255 bool qualified_p = false;
15256 bool invalid_nested_name_p = false;
15257 bool invalid_explicit_specialization_p = false;
15258 tree pushed_scope = NULL_TREE;
15259 unsigned num_templates;
15260 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15261 /* Assume no nested-name-specifier will be present. */
15262 *nested_name_specifier_p = false;
15263 /* Assume no template parameter lists will be used in defining the
15267 *bases = NULL_TREE;
15269 /* Look for the class-key. */
15270 class_key = cp_parser_class_key (parser);
15271 if (class_key == none_type)
15272 return error_mark_node;
15274 /* Parse the attributes. */
15275 attributes = cp_parser_attributes_opt (parser);
15277 /* If the next token is `::', that is invalid -- but sometimes
15278 people do try to write:
15282 Handle this gracefully by accepting the extra qualifier, and then
15283 issuing an error about it later if this really is a
15284 class-head. If it turns out just to be an elaborated type
15285 specifier, remain silent. */
15286 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15287 qualified_p = true;
15289 push_deferring_access_checks (dk_no_check);
15291 /* Determine the name of the class. Begin by looking for an
15292 optional nested-name-specifier. */
15293 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15294 nested_name_specifier
15295 = cp_parser_nested_name_specifier_opt (parser,
15296 /*typename_keyword_p=*/false,
15297 /*check_dependency_p=*/false,
15299 /*is_declaration=*/false);
15300 /* If there was a nested-name-specifier, then there *must* be an
15302 if (nested_name_specifier)
15304 type_start_token = cp_lexer_peek_token (parser->lexer);
15305 /* Although the grammar says `identifier', it really means
15306 `class-name' or `template-name'. You are only allowed to
15307 define a class that has already been declared with this
15310 The proposed resolution for Core Issue 180 says that wherever
15311 you see `class T::X' you should treat `X' as a type-name.
15313 It is OK to define an inaccessible class; for example:
15315 class A { class B; };
15318 We do not know if we will see a class-name, or a
15319 template-name. We look for a class-name first, in case the
15320 class-name is a template-id; if we looked for the
15321 template-name first we would stop after the template-name. */
15322 cp_parser_parse_tentatively (parser);
15323 type = cp_parser_class_name (parser,
15324 /*typename_keyword_p=*/false,
15325 /*template_keyword_p=*/false,
15327 /*check_dependency_p=*/false,
15328 /*class_head_p=*/true,
15329 /*is_declaration=*/false);
15330 /* If that didn't work, ignore the nested-name-specifier. */
15331 if (!cp_parser_parse_definitely (parser))
15333 invalid_nested_name_p = true;
15334 type_start_token = cp_lexer_peek_token (parser->lexer);
15335 id = cp_parser_identifier (parser);
15336 if (id == error_mark_node)
15339 /* If we could not find a corresponding TYPE, treat this
15340 declaration like an unqualified declaration. */
15341 if (type == error_mark_node)
15342 nested_name_specifier = NULL_TREE;
15343 /* Otherwise, count the number of templates used in TYPE and its
15344 containing scopes. */
15349 for (scope = TREE_TYPE (type);
15350 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15351 scope = (TYPE_P (scope)
15352 ? TYPE_CONTEXT (scope)
15353 : DECL_CONTEXT (scope)))
15355 && CLASS_TYPE_P (scope)
15356 && CLASSTYPE_TEMPLATE_INFO (scope)
15357 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15358 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15362 /* Otherwise, the identifier is optional. */
15365 /* We don't know whether what comes next is a template-id,
15366 an identifier, or nothing at all. */
15367 cp_parser_parse_tentatively (parser);
15368 /* Check for a template-id. */
15369 type_start_token = cp_lexer_peek_token (parser->lexer);
15370 id = cp_parser_template_id (parser,
15371 /*template_keyword_p=*/false,
15372 /*check_dependency_p=*/true,
15373 /*is_declaration=*/true);
15374 /* If that didn't work, it could still be an identifier. */
15375 if (!cp_parser_parse_definitely (parser))
15377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15379 type_start_token = cp_lexer_peek_token (parser->lexer);
15380 id = cp_parser_identifier (parser);
15387 template_id_p = true;
15392 pop_deferring_access_checks ();
15395 cp_parser_check_for_invalid_template_id (parser, id,
15396 type_start_token->location);
15398 /* If it's not a `:' or a `{' then we can't really be looking at a
15399 class-head, since a class-head only appears as part of a
15400 class-specifier. We have to detect this situation before calling
15401 xref_tag, since that has irreversible side-effects. */
15402 if (!cp_parser_next_token_starts_class_definition_p (parser))
15404 cp_parser_error (parser, "expected %<{%> or %<:%>");
15405 return error_mark_node;
15408 /* At this point, we're going ahead with the class-specifier, even
15409 if some other problem occurs. */
15410 cp_parser_commit_to_tentative_parse (parser);
15411 /* Issue the error about the overly-qualified name now. */
15414 cp_parser_error (parser,
15415 "global qualification of class name is invalid");
15416 return error_mark_node;
15418 else if (invalid_nested_name_p)
15420 cp_parser_error (parser,
15421 "qualified name does not name a class");
15422 return error_mark_node;
15424 else if (nested_name_specifier)
15428 /* Reject typedef-names in class heads. */
15429 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15431 error ("%Hinvalid class name in declaration of %qD",
15432 &type_start_token->location, type);
15437 /* Figure out in what scope the declaration is being placed. */
15438 scope = current_scope ();
15439 /* If that scope does not contain the scope in which the
15440 class was originally declared, the program is invalid. */
15441 if (scope && !is_ancestor (scope, nested_name_specifier))
15443 if (at_namespace_scope_p ())
15444 error ("%Hdeclaration of %qD in namespace %qD which does not "
15446 &type_start_token->location,
15447 type, scope, nested_name_specifier);
15449 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15450 &type_start_token->location,
15451 type, scope, nested_name_specifier);
15457 A declarator-id shall not be qualified except for the
15458 definition of a ... nested class outside of its class
15459 ... [or] the definition or explicit instantiation of a
15460 class member of a namespace outside of its namespace. */
15461 if (scope == nested_name_specifier)
15463 permerror (input_location, "%Hextra qualification not allowed",
15464 &nested_name_specifier_token_start->location);
15465 nested_name_specifier = NULL_TREE;
15469 /* An explicit-specialization must be preceded by "template <>". If
15470 it is not, try to recover gracefully. */
15471 if (at_namespace_scope_p ()
15472 && parser->num_template_parameter_lists == 0
15475 error ("%Han explicit specialization must be preceded by %<template <>%>",
15476 &type_start_token->location);
15477 invalid_explicit_specialization_p = true;
15478 /* Take the same action that would have been taken by
15479 cp_parser_explicit_specialization. */
15480 ++parser->num_template_parameter_lists;
15481 begin_specialization ();
15483 /* There must be no "return" statements between this point and the
15484 end of this function; set "type "to the correct return value and
15485 use "goto done;" to return. */
15486 /* Make sure that the right number of template parameters were
15488 if (!cp_parser_check_template_parameters (parser, num_templates,
15489 type_start_token->location))
15491 /* If something went wrong, there is no point in even trying to
15492 process the class-definition. */
15497 /* Look up the type. */
15500 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15501 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15502 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15504 error ("%Hfunction template %qD redeclared as a class template",
15505 &type_start_token->location, id);
15506 type = error_mark_node;
15510 type = TREE_TYPE (id);
15511 type = maybe_process_partial_specialization (type);
15513 if (nested_name_specifier)
15514 pushed_scope = push_scope (nested_name_specifier);
15516 else if (nested_name_specifier)
15522 template <typename T> struct S { struct T };
15523 template <typename T> struct S<T>::T { };
15525 we will get a TYPENAME_TYPE when processing the definition of
15526 `S::T'. We need to resolve it to the actual type before we
15527 try to define it. */
15528 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15530 class_type = resolve_typename_type (TREE_TYPE (type),
15531 /*only_current_p=*/false);
15532 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15533 type = TYPE_NAME (class_type);
15536 cp_parser_error (parser, "could not resolve typename type");
15537 type = error_mark_node;
15541 if (maybe_process_partial_specialization (TREE_TYPE (type))
15542 == error_mark_node)
15548 class_type = current_class_type;
15549 /* Enter the scope indicated by the nested-name-specifier. */
15550 pushed_scope = push_scope (nested_name_specifier);
15551 /* Get the canonical version of this type. */
15552 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15553 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15554 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15556 type = push_template_decl (type);
15557 if (type == error_mark_node)
15564 type = TREE_TYPE (type);
15565 *nested_name_specifier_p = true;
15567 else /* The name is not a nested name. */
15569 /* If the class was unnamed, create a dummy name. */
15571 id = make_anon_name ();
15572 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15573 parser->num_template_parameter_lists);
15576 /* Indicate whether this class was declared as a `class' or as a
15578 if (TREE_CODE (type) == RECORD_TYPE)
15579 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15580 cp_parser_check_class_key (class_key, type);
15582 /* If this type was already complete, and we see another definition,
15583 that's an error. */
15584 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15586 error ("%Hredefinition of %q#T",
15587 &type_start_token->location, type);
15588 error ("%Hprevious definition of %q+#T",
15589 &type_start_token->location, type);
15593 else if (type == error_mark_node)
15596 /* We will have entered the scope containing the class; the names of
15597 base classes should be looked up in that context. For example:
15599 struct A { struct B {}; struct C; };
15600 struct A::C : B {};
15604 /* Get the list of base-classes, if there is one. */
15605 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15606 *bases = cp_parser_base_clause (parser);
15609 /* Leave the scope given by the nested-name-specifier. We will
15610 enter the class scope itself while processing the members. */
15612 pop_scope (pushed_scope);
15614 if (invalid_explicit_specialization_p)
15616 end_specialization ();
15617 --parser->num_template_parameter_lists;
15619 *attributes_p = attributes;
15623 /* Parse a class-key.
15630 Returns the kind of class-key specified, or none_type to indicate
15633 static enum tag_types
15634 cp_parser_class_key (cp_parser* parser)
15637 enum tag_types tag_type;
15639 /* Look for the class-key. */
15640 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15644 /* Check to see if the TOKEN is a class-key. */
15645 tag_type = cp_parser_token_is_class_key (token);
15647 cp_parser_error (parser, "expected class-key");
15651 /* Parse an (optional) member-specification.
15653 member-specification:
15654 member-declaration member-specification [opt]
15655 access-specifier : member-specification [opt] */
15658 cp_parser_member_specification_opt (cp_parser* parser)
15665 /* Peek at the next token. */
15666 token = cp_lexer_peek_token (parser->lexer);
15667 /* If it's a `}', or EOF then we've seen all the members. */
15668 if (token->type == CPP_CLOSE_BRACE
15669 || token->type == CPP_EOF
15670 || token->type == CPP_PRAGMA_EOL)
15673 /* See if this token is a keyword. */
15674 keyword = token->keyword;
15678 case RID_PROTECTED:
15680 /* Consume the access-specifier. */
15681 cp_lexer_consume_token (parser->lexer);
15682 /* Remember which access-specifier is active. */
15683 current_access_specifier = token->u.value;
15684 /* Look for the `:'. */
15685 cp_parser_require (parser, CPP_COLON, "%<:%>");
15689 /* Accept #pragmas at class scope. */
15690 if (token->type == CPP_PRAGMA)
15692 cp_parser_pragma (parser, pragma_external);
15696 /* Otherwise, the next construction must be a
15697 member-declaration. */
15698 cp_parser_member_declaration (parser);
15703 /* Parse a member-declaration.
15705 member-declaration:
15706 decl-specifier-seq [opt] member-declarator-list [opt] ;
15707 function-definition ; [opt]
15708 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15710 template-declaration
15712 member-declarator-list:
15714 member-declarator-list , member-declarator
15717 declarator pure-specifier [opt]
15718 declarator constant-initializer [opt]
15719 identifier [opt] : constant-expression
15723 member-declaration:
15724 __extension__ member-declaration
15727 declarator attributes [opt] pure-specifier [opt]
15728 declarator attributes [opt] constant-initializer [opt]
15729 identifier [opt] attributes [opt] : constant-expression
15733 member-declaration:
15734 static_assert-declaration */
15737 cp_parser_member_declaration (cp_parser* parser)
15739 cp_decl_specifier_seq decl_specifiers;
15740 tree prefix_attributes;
15742 int declares_class_or_enum;
15744 cp_token *token = NULL;
15745 cp_token *decl_spec_token_start = NULL;
15746 cp_token *initializer_token_start = NULL;
15747 int saved_pedantic;
15749 /* Check for the `__extension__' keyword. */
15750 if (cp_parser_extension_opt (parser, &saved_pedantic))
15753 cp_parser_member_declaration (parser);
15754 /* Restore the old value of the PEDANTIC flag. */
15755 pedantic = saved_pedantic;
15760 /* Check for a template-declaration. */
15761 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15763 /* An explicit specialization here is an error condition, and we
15764 expect the specialization handler to detect and report this. */
15765 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15766 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15767 cp_parser_explicit_specialization (parser);
15769 cp_parser_template_declaration (parser, /*member_p=*/true);
15774 /* Check for a using-declaration. */
15775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15777 /* Parse the using-declaration. */
15778 cp_parser_using_declaration (parser,
15779 /*access_declaration_p=*/false);
15783 /* Check for @defs. */
15784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15787 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15788 ivar = ivar_chains;
15792 ivar = TREE_CHAIN (member);
15793 TREE_CHAIN (member) = NULL_TREE;
15794 finish_member_declaration (member);
15799 /* If the next token is `static_assert' we have a static assertion. */
15800 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15802 cp_parser_static_assert (parser, /*member_p=*/true);
15806 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15809 /* Parse the decl-specifier-seq. */
15810 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15811 cp_parser_decl_specifier_seq (parser,
15812 CP_PARSER_FLAGS_OPTIONAL,
15814 &declares_class_or_enum);
15815 prefix_attributes = decl_specifiers.attributes;
15816 decl_specifiers.attributes = NULL_TREE;
15817 /* Check for an invalid type-name. */
15818 if (!decl_specifiers.type
15819 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15821 /* If there is no declarator, then the decl-specifier-seq should
15823 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15825 /* If there was no decl-specifier-seq, and the next token is a
15826 `;', then we have something like:
15832 Each member-declaration shall declare at least one member
15833 name of the class. */
15834 if (!decl_specifiers.any_specifiers_p)
15836 cp_token *token = cp_lexer_peek_token (parser->lexer);
15837 if (!in_system_header_at (token->location))
15838 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15844 /* See if this declaration is a friend. */
15845 friend_p = cp_parser_friend_p (&decl_specifiers);
15846 /* If there were decl-specifiers, check to see if there was
15847 a class-declaration. */
15848 type = check_tag_decl (&decl_specifiers);
15849 /* Nested classes have already been added to the class, but
15850 a `friend' needs to be explicitly registered. */
15853 /* If the `friend' keyword was present, the friend must
15854 be introduced with a class-key. */
15855 if (!declares_class_or_enum)
15856 error ("%Ha class-key must be used when declaring a friend",
15857 &decl_spec_token_start->location);
15860 template <typename T> struct A {
15861 friend struct A<T>::B;
15864 A<T>::B will be represented by a TYPENAME_TYPE, and
15865 therefore not recognized by check_tag_decl. */
15867 && decl_specifiers.type
15868 && TYPE_P (decl_specifiers.type))
15869 type = decl_specifiers.type;
15870 if (!type || !TYPE_P (type))
15871 error ("%Hfriend declaration does not name a class or "
15872 "function", &decl_spec_token_start->location);
15874 make_friend_class (current_class_type, type,
15875 /*complain=*/true);
15877 /* If there is no TYPE, an error message will already have
15879 else if (!type || type == error_mark_node)
15881 /* An anonymous aggregate has to be handled specially; such
15882 a declaration really declares a data member (with a
15883 particular type), as opposed to a nested class. */
15884 else if (ANON_AGGR_TYPE_P (type))
15886 /* Remove constructors and such from TYPE, now that we
15887 know it is an anonymous aggregate. */
15888 fixup_anonymous_aggr (type);
15889 /* And make the corresponding data member. */
15890 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15891 /* Add it to the class. */
15892 finish_member_declaration (decl);
15895 cp_parser_check_access_in_redeclaration
15897 decl_spec_token_start->location);
15902 /* See if these declarations will be friends. */
15903 friend_p = cp_parser_friend_p (&decl_specifiers);
15905 /* Keep going until we hit the `;' at the end of the
15907 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15909 tree attributes = NULL_TREE;
15910 tree first_attribute;
15912 /* Peek at the next token. */
15913 token = cp_lexer_peek_token (parser->lexer);
15915 /* Check for a bitfield declaration. */
15916 if (token->type == CPP_COLON
15917 || (token->type == CPP_NAME
15918 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15924 /* Get the name of the bitfield. Note that we cannot just
15925 check TOKEN here because it may have been invalidated by
15926 the call to cp_lexer_peek_nth_token above. */
15927 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15928 identifier = cp_parser_identifier (parser);
15930 identifier = NULL_TREE;
15932 /* Consume the `:' token. */
15933 cp_lexer_consume_token (parser->lexer);
15934 /* Get the width of the bitfield. */
15936 = cp_parser_constant_expression (parser,
15937 /*allow_non_constant=*/false,
15940 /* Look for attributes that apply to the bitfield. */
15941 attributes = cp_parser_attributes_opt (parser);
15942 /* Remember which attributes are prefix attributes and
15944 first_attribute = attributes;
15945 /* Combine the attributes. */
15946 attributes = chainon (prefix_attributes, attributes);
15948 /* Create the bitfield declaration. */
15949 decl = grokbitfield (identifier
15950 ? make_id_declarator (NULL_TREE,
15960 cp_declarator *declarator;
15962 tree asm_specification;
15963 int ctor_dtor_or_conv_p;
15965 /* Parse the declarator. */
15967 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15968 &ctor_dtor_or_conv_p,
15969 /*parenthesized_p=*/NULL,
15970 /*member_p=*/true);
15972 /* If something went wrong parsing the declarator, make sure
15973 that we at least consume some tokens. */
15974 if (declarator == cp_error_declarator)
15976 /* Skip to the end of the statement. */
15977 cp_parser_skip_to_end_of_statement (parser);
15978 /* If the next token is not a semicolon, that is
15979 probably because we just skipped over the body of
15980 a function. So, we consume a semicolon if
15981 present, but do not issue an error message if it
15983 if (cp_lexer_next_token_is (parser->lexer,
15985 cp_lexer_consume_token (parser->lexer);
15989 if (declares_class_or_enum & 2)
15990 cp_parser_check_for_definition_in_return_type
15991 (declarator, decl_specifiers.type,
15992 decl_specifiers.type_location);
15994 /* Look for an asm-specification. */
15995 asm_specification = cp_parser_asm_specification_opt (parser);
15996 /* Look for attributes that apply to the declaration. */
15997 attributes = cp_parser_attributes_opt (parser);
15998 /* Remember which attributes are prefix attributes and
16000 first_attribute = attributes;
16001 /* Combine the attributes. */
16002 attributes = chainon (prefix_attributes, attributes);
16004 /* If it's an `=', then we have a constant-initializer or a
16005 pure-specifier. It is not correct to parse the
16006 initializer before registering the member declaration
16007 since the member declaration should be in scope while
16008 its initializer is processed. However, the rest of the
16009 front end does not yet provide an interface that allows
16010 us to handle this correctly. */
16011 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16015 A pure-specifier shall be used only in the declaration of
16016 a virtual function.
16018 A member-declarator can contain a constant-initializer
16019 only if it declares a static member of integral or
16022 Therefore, if the DECLARATOR is for a function, we look
16023 for a pure-specifier; otherwise, we look for a
16024 constant-initializer. When we call `grokfield', it will
16025 perform more stringent semantics checks. */
16026 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16027 if (function_declarator_p (declarator))
16028 initializer = cp_parser_pure_specifier (parser);
16030 /* Parse the initializer. */
16031 initializer = cp_parser_constant_initializer (parser);
16033 /* Otherwise, there is no initializer. */
16035 initializer = NULL_TREE;
16037 /* See if we are probably looking at a function
16038 definition. We are certainly not looking at a
16039 member-declarator. Calling `grokfield' has
16040 side-effects, so we must not do it unless we are sure
16041 that we are looking at a member-declarator. */
16042 if (cp_parser_token_starts_function_definition_p
16043 (cp_lexer_peek_token (parser->lexer)))
16045 /* The grammar does not allow a pure-specifier to be
16046 used when a member function is defined. (It is
16047 possible that this fact is an oversight in the
16048 standard, since a pure function may be defined
16049 outside of the class-specifier. */
16051 error ("%Hpure-specifier on function-definition",
16052 &initializer_token_start->location);
16053 decl = cp_parser_save_member_function_body (parser,
16057 /* If the member was not a friend, declare it here. */
16059 finish_member_declaration (decl);
16060 /* Peek at the next token. */
16061 token = cp_lexer_peek_token (parser->lexer);
16062 /* If the next token is a semicolon, consume it. */
16063 if (token->type == CPP_SEMICOLON)
16064 cp_lexer_consume_token (parser->lexer);
16068 if (declarator->kind == cdk_function)
16069 declarator->id_loc = token->location;
16070 /* Create the declaration. */
16071 decl = grokfield (declarator, &decl_specifiers,
16072 initializer, /*init_const_expr_p=*/true,
16077 /* Reset PREFIX_ATTRIBUTES. */
16078 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16079 attributes = TREE_CHAIN (attributes);
16081 TREE_CHAIN (attributes) = NULL_TREE;
16083 /* If there is any qualification still in effect, clear it
16084 now; we will be starting fresh with the next declarator. */
16085 parser->scope = NULL_TREE;
16086 parser->qualifying_scope = NULL_TREE;
16087 parser->object_scope = NULL_TREE;
16088 /* If it's a `,', then there are more declarators. */
16089 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16090 cp_lexer_consume_token (parser->lexer);
16091 /* If the next token isn't a `;', then we have a parse error. */
16092 else if (cp_lexer_next_token_is_not (parser->lexer,
16095 cp_parser_error (parser, "expected %<;%>");
16096 /* Skip tokens until we find a `;'. */
16097 cp_parser_skip_to_end_of_statement (parser);
16104 /* Add DECL to the list of members. */
16106 finish_member_declaration (decl);
16108 if (TREE_CODE (decl) == FUNCTION_DECL)
16109 cp_parser_save_default_args (parser, decl);
16114 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16117 /* Parse a pure-specifier.
16122 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16123 Otherwise, ERROR_MARK_NODE is returned. */
16126 cp_parser_pure_specifier (cp_parser* parser)
16130 /* Look for the `=' token. */
16131 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16132 return error_mark_node;
16133 /* Look for the `0' token. */
16134 token = cp_lexer_peek_token (parser->lexer);
16136 if (token->type == CPP_EOF
16137 || token->type == CPP_PRAGMA_EOL)
16138 return error_mark_node;
16140 cp_lexer_consume_token (parser->lexer);
16142 /* Accept = default or = delete in c++0x mode. */
16143 if (token->keyword == RID_DEFAULT
16144 || token->keyword == RID_DELETE)
16146 maybe_warn_cpp0x ("defaulted and deleted functions");
16147 return token->u.value;
16150 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16151 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16153 cp_parser_error (parser,
16154 "invalid pure specifier (only %<= 0%> is allowed)");
16155 cp_parser_skip_to_end_of_statement (parser);
16156 return error_mark_node;
16158 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16160 error ("%Htemplates may not be %<virtual%>", &token->location);
16161 return error_mark_node;
16164 return integer_zero_node;
16167 /* Parse a constant-initializer.
16169 constant-initializer:
16170 = constant-expression
16172 Returns a representation of the constant-expression. */
16175 cp_parser_constant_initializer (cp_parser* parser)
16177 /* Look for the `=' token. */
16178 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16179 return error_mark_node;
16181 /* It is invalid to write:
16183 struct S { static const int i = { 7 }; };
16186 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16188 cp_parser_error (parser,
16189 "a brace-enclosed initializer is not allowed here");
16190 /* Consume the opening brace. */
16191 cp_lexer_consume_token (parser->lexer);
16192 /* Skip the initializer. */
16193 cp_parser_skip_to_closing_brace (parser);
16194 /* Look for the trailing `}'. */
16195 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16197 return error_mark_node;
16200 return cp_parser_constant_expression (parser,
16201 /*allow_non_constant=*/false,
16205 /* Derived classes [gram.class.derived] */
16207 /* Parse a base-clause.
16210 : base-specifier-list
16212 base-specifier-list:
16213 base-specifier ... [opt]
16214 base-specifier-list , base-specifier ... [opt]
16216 Returns a TREE_LIST representing the base-classes, in the order in
16217 which they were declared. The representation of each node is as
16218 described by cp_parser_base_specifier.
16220 In the case that no bases are specified, this function will return
16221 NULL_TREE, not ERROR_MARK_NODE. */
16224 cp_parser_base_clause (cp_parser* parser)
16226 tree bases = NULL_TREE;
16228 /* Look for the `:' that begins the list. */
16229 cp_parser_require (parser, CPP_COLON, "%<:%>");
16231 /* Scan the base-specifier-list. */
16236 bool pack_expansion_p = false;
16238 /* Look for the base-specifier. */
16239 base = cp_parser_base_specifier (parser);
16240 /* Look for the (optional) ellipsis. */
16241 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16243 /* Consume the `...'. */
16244 cp_lexer_consume_token (parser->lexer);
16246 pack_expansion_p = true;
16249 /* Add BASE to the front of the list. */
16250 if (base != error_mark_node)
16252 if (pack_expansion_p)
16253 /* Make this a pack expansion type. */
16254 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16257 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16259 TREE_CHAIN (base) = bases;
16263 /* Peek at the next token. */
16264 token = cp_lexer_peek_token (parser->lexer);
16265 /* If it's not a comma, then the list is complete. */
16266 if (token->type != CPP_COMMA)
16268 /* Consume the `,'. */
16269 cp_lexer_consume_token (parser->lexer);
16272 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16273 base class had a qualified name. However, the next name that
16274 appears is certainly not qualified. */
16275 parser->scope = NULL_TREE;
16276 parser->qualifying_scope = NULL_TREE;
16277 parser->object_scope = NULL_TREE;
16279 return nreverse (bases);
16282 /* Parse a base-specifier.
16285 :: [opt] nested-name-specifier [opt] class-name
16286 virtual access-specifier [opt] :: [opt] nested-name-specifier
16288 access-specifier virtual [opt] :: [opt] nested-name-specifier
16291 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16292 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16293 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16294 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16297 cp_parser_base_specifier (cp_parser* parser)
16301 bool virtual_p = false;
16302 bool duplicate_virtual_error_issued_p = false;
16303 bool duplicate_access_error_issued_p = false;
16304 bool class_scope_p, template_p;
16305 tree access = access_default_node;
16308 /* Process the optional `virtual' and `access-specifier'. */
16311 /* Peek at the next token. */
16312 token = cp_lexer_peek_token (parser->lexer);
16313 /* Process `virtual'. */
16314 switch (token->keyword)
16317 /* If `virtual' appears more than once, issue an error. */
16318 if (virtual_p && !duplicate_virtual_error_issued_p)
16320 cp_parser_error (parser,
16321 "%<virtual%> specified more than once in base-specified");
16322 duplicate_virtual_error_issued_p = true;
16327 /* Consume the `virtual' token. */
16328 cp_lexer_consume_token (parser->lexer);
16333 case RID_PROTECTED:
16335 /* If more than one access specifier appears, issue an
16337 if (access != access_default_node
16338 && !duplicate_access_error_issued_p)
16340 cp_parser_error (parser,
16341 "more than one access specifier in base-specified");
16342 duplicate_access_error_issued_p = true;
16345 access = ridpointers[(int) token->keyword];
16347 /* Consume the access-specifier. */
16348 cp_lexer_consume_token (parser->lexer);
16357 /* It is not uncommon to see programs mechanically, erroneously, use
16358 the 'typename' keyword to denote (dependent) qualified types
16359 as base classes. */
16360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16362 token = cp_lexer_peek_token (parser->lexer);
16363 if (!processing_template_decl)
16364 error ("%Hkeyword %<typename%> not allowed outside of templates",
16367 error ("%Hkeyword %<typename%> not allowed in this context "
16368 "(the base class is implicitly a type)",
16370 cp_lexer_consume_token (parser->lexer);
16373 /* Look for the optional `::' operator. */
16374 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16375 /* Look for the nested-name-specifier. The simplest way to
16380 The keyword `typename' is not permitted in a base-specifier or
16381 mem-initializer; in these contexts a qualified name that
16382 depends on a template-parameter is implicitly assumed to be a
16385 is to pretend that we have seen the `typename' keyword at this
16387 cp_parser_nested_name_specifier_opt (parser,
16388 /*typename_keyword_p=*/true,
16389 /*check_dependency_p=*/true,
16391 /*is_declaration=*/true);
16392 /* If the base class is given by a qualified name, assume that names
16393 we see are type names or templates, as appropriate. */
16394 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16395 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16397 /* Finally, look for the class-name. */
16398 type = cp_parser_class_name (parser,
16402 /*check_dependency_p=*/true,
16403 /*class_head_p=*/false,
16404 /*is_declaration=*/true);
16406 if (type == error_mark_node)
16407 return error_mark_node;
16409 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16412 /* Exception handling [gram.exception] */
16414 /* Parse an (optional) exception-specification.
16416 exception-specification:
16417 throw ( type-id-list [opt] )
16419 Returns a TREE_LIST representing the exception-specification. The
16420 TREE_VALUE of each node is a type. */
16423 cp_parser_exception_specification_opt (cp_parser* parser)
16428 /* Peek at the next token. */
16429 token = cp_lexer_peek_token (parser->lexer);
16430 /* If it's not `throw', then there's no exception-specification. */
16431 if (!cp_parser_is_keyword (token, RID_THROW))
16434 /* Consume the `throw'. */
16435 cp_lexer_consume_token (parser->lexer);
16437 /* Look for the `('. */
16438 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16440 /* Peek at the next token. */
16441 token = cp_lexer_peek_token (parser->lexer);
16442 /* If it's not a `)', then there is a type-id-list. */
16443 if (token->type != CPP_CLOSE_PAREN)
16445 const char *saved_message;
16447 /* Types may not be defined in an exception-specification. */
16448 saved_message = parser->type_definition_forbidden_message;
16449 parser->type_definition_forbidden_message
16450 = "types may not be defined in an exception-specification";
16451 /* Parse the type-id-list. */
16452 type_id_list = cp_parser_type_id_list (parser);
16453 /* Restore the saved message. */
16454 parser->type_definition_forbidden_message = saved_message;
16457 type_id_list = empty_except_spec;
16459 /* Look for the `)'. */
16460 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16462 return type_id_list;
16465 /* Parse an (optional) type-id-list.
16469 type-id-list , type-id ... [opt]
16471 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16472 in the order that the types were presented. */
16475 cp_parser_type_id_list (cp_parser* parser)
16477 tree types = NULL_TREE;
16484 /* Get the next type-id. */
16485 type = cp_parser_type_id (parser);
16486 /* Parse the optional ellipsis. */
16487 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16489 /* Consume the `...'. */
16490 cp_lexer_consume_token (parser->lexer);
16492 /* Turn the type into a pack expansion expression. */
16493 type = make_pack_expansion (type);
16495 /* Add it to the list. */
16496 types = add_exception_specifier (types, type, /*complain=*/1);
16497 /* Peek at the next token. */
16498 token = cp_lexer_peek_token (parser->lexer);
16499 /* If it is not a `,', we are done. */
16500 if (token->type != CPP_COMMA)
16502 /* Consume the `,'. */
16503 cp_lexer_consume_token (parser->lexer);
16506 return nreverse (types);
16509 /* Parse a try-block.
16512 try compound-statement handler-seq */
16515 cp_parser_try_block (cp_parser* parser)
16519 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16520 try_block = begin_try_block ();
16521 cp_parser_compound_statement (parser, NULL, true);
16522 finish_try_block (try_block);
16523 cp_parser_handler_seq (parser);
16524 finish_handler_sequence (try_block);
16529 /* Parse a function-try-block.
16531 function-try-block:
16532 try ctor-initializer [opt] function-body handler-seq */
16535 cp_parser_function_try_block (cp_parser* parser)
16537 tree compound_stmt;
16539 bool ctor_initializer_p;
16541 /* Look for the `try' keyword. */
16542 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16544 /* Let the rest of the front end know where we are. */
16545 try_block = begin_function_try_block (&compound_stmt);
16546 /* Parse the function-body. */
16548 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16549 /* We're done with the `try' part. */
16550 finish_function_try_block (try_block);
16551 /* Parse the handlers. */
16552 cp_parser_handler_seq (parser);
16553 /* We're done with the handlers. */
16554 finish_function_handler_sequence (try_block, compound_stmt);
16556 return ctor_initializer_p;
16559 /* Parse a handler-seq.
16562 handler handler-seq [opt] */
16565 cp_parser_handler_seq (cp_parser* parser)
16571 /* Parse the handler. */
16572 cp_parser_handler (parser);
16573 /* Peek at the next token. */
16574 token = cp_lexer_peek_token (parser->lexer);
16575 /* If it's not `catch' then there are no more handlers. */
16576 if (!cp_parser_is_keyword (token, RID_CATCH))
16581 /* Parse a handler.
16584 catch ( exception-declaration ) compound-statement */
16587 cp_parser_handler (cp_parser* parser)
16592 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16593 handler = begin_handler ();
16594 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16595 declaration = cp_parser_exception_declaration (parser);
16596 finish_handler_parms (declaration, handler);
16597 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16598 cp_parser_compound_statement (parser, NULL, false);
16599 finish_handler (handler);
16602 /* Parse an exception-declaration.
16604 exception-declaration:
16605 type-specifier-seq declarator
16606 type-specifier-seq abstract-declarator
16610 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16611 ellipsis variant is used. */
16614 cp_parser_exception_declaration (cp_parser* parser)
16616 cp_decl_specifier_seq type_specifiers;
16617 cp_declarator *declarator;
16618 const char *saved_message;
16620 /* If it's an ellipsis, it's easy to handle. */
16621 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16623 /* Consume the `...' token. */
16624 cp_lexer_consume_token (parser->lexer);
16628 /* Types may not be defined in exception-declarations. */
16629 saved_message = parser->type_definition_forbidden_message;
16630 parser->type_definition_forbidden_message
16631 = "types may not be defined in exception-declarations";
16633 /* Parse the type-specifier-seq. */
16634 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
16635 /*is_trailing_return=*/false,
16637 /* If it's a `)', then there is no declarator. */
16638 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16641 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16642 /*ctor_dtor_or_conv_p=*/NULL,
16643 /*parenthesized_p=*/NULL,
16644 /*member_p=*/false);
16646 /* Restore the saved message. */
16647 parser->type_definition_forbidden_message = saved_message;
16649 if (!type_specifiers.any_specifiers_p)
16650 return error_mark_node;
16652 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16655 /* Parse a throw-expression.
16658 throw assignment-expression [opt]
16660 Returns a THROW_EXPR representing the throw-expression. */
16663 cp_parser_throw_expression (cp_parser* parser)
16668 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16669 token = cp_lexer_peek_token (parser->lexer);
16670 /* Figure out whether or not there is an assignment-expression
16671 following the "throw" keyword. */
16672 if (token->type == CPP_COMMA
16673 || token->type == CPP_SEMICOLON
16674 || token->type == CPP_CLOSE_PAREN
16675 || token->type == CPP_CLOSE_SQUARE
16676 || token->type == CPP_CLOSE_BRACE
16677 || token->type == CPP_COLON)
16678 expression = NULL_TREE;
16680 expression = cp_parser_assignment_expression (parser,
16681 /*cast_p=*/false, NULL);
16683 return build_throw (expression);
16686 /* GNU Extensions */
16688 /* Parse an (optional) asm-specification.
16691 asm ( string-literal )
16693 If the asm-specification is present, returns a STRING_CST
16694 corresponding to the string-literal. Otherwise, returns
16698 cp_parser_asm_specification_opt (cp_parser* parser)
16701 tree asm_specification;
16703 /* Peek at the next token. */
16704 token = cp_lexer_peek_token (parser->lexer);
16705 /* If the next token isn't the `asm' keyword, then there's no
16706 asm-specification. */
16707 if (!cp_parser_is_keyword (token, RID_ASM))
16710 /* Consume the `asm' token. */
16711 cp_lexer_consume_token (parser->lexer);
16712 /* Look for the `('. */
16713 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16715 /* Look for the string-literal. */
16716 asm_specification = cp_parser_string_literal (parser, false, false);
16718 /* Look for the `)'. */
16719 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16721 return asm_specification;
16724 /* Parse an asm-operand-list.
16728 asm-operand-list , asm-operand
16731 string-literal ( expression )
16732 [ string-literal ] string-literal ( expression )
16734 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16735 each node is the expression. The TREE_PURPOSE is itself a
16736 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16737 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16738 is a STRING_CST for the string literal before the parenthesis. Returns
16739 ERROR_MARK_NODE if any of the operands are invalid. */
16742 cp_parser_asm_operand_list (cp_parser* parser)
16744 tree asm_operands = NULL_TREE;
16745 bool invalid_operands = false;
16749 tree string_literal;
16753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16755 /* Consume the `[' token. */
16756 cp_lexer_consume_token (parser->lexer);
16757 /* Read the operand name. */
16758 name = cp_parser_identifier (parser);
16759 if (name != error_mark_node)
16760 name = build_string (IDENTIFIER_LENGTH (name),
16761 IDENTIFIER_POINTER (name));
16762 /* Look for the closing `]'. */
16763 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16767 /* Look for the string-literal. */
16768 string_literal = cp_parser_string_literal (parser, false, false);
16770 /* Look for the `('. */
16771 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16772 /* Parse the expression. */
16773 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16774 /* Look for the `)'. */
16775 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16777 if (name == error_mark_node
16778 || string_literal == error_mark_node
16779 || expression == error_mark_node)
16780 invalid_operands = true;
16782 /* Add this operand to the list. */
16783 asm_operands = tree_cons (build_tree_list (name, string_literal),
16786 /* If the next token is not a `,', there are no more
16788 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16790 /* Consume the `,'. */
16791 cp_lexer_consume_token (parser->lexer);
16794 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16797 /* Parse an asm-clobber-list.
16801 asm-clobber-list , string-literal
16803 Returns a TREE_LIST, indicating the clobbers in the order that they
16804 appeared. The TREE_VALUE of each node is a STRING_CST. */
16807 cp_parser_asm_clobber_list (cp_parser* parser)
16809 tree clobbers = NULL_TREE;
16813 tree string_literal;
16815 /* Look for the string literal. */
16816 string_literal = cp_parser_string_literal (parser, false, false);
16817 /* Add it to the list. */
16818 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16819 /* If the next token is not a `,', then the list is
16821 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16823 /* Consume the `,' token. */
16824 cp_lexer_consume_token (parser->lexer);
16830 /* Parse an (optional) series of attributes.
16833 attributes attribute
16836 __attribute__ (( attribute-list [opt] ))
16838 The return value is as for cp_parser_attribute_list. */
16841 cp_parser_attributes_opt (cp_parser* parser)
16843 tree attributes = NULL_TREE;
16848 tree attribute_list;
16850 /* Peek at the next token. */
16851 token = cp_lexer_peek_token (parser->lexer);
16852 /* If it's not `__attribute__', then we're done. */
16853 if (token->keyword != RID_ATTRIBUTE)
16856 /* Consume the `__attribute__' keyword. */
16857 cp_lexer_consume_token (parser->lexer);
16858 /* Look for the two `(' tokens. */
16859 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16860 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16862 /* Peek at the next token. */
16863 token = cp_lexer_peek_token (parser->lexer);
16864 if (token->type != CPP_CLOSE_PAREN)
16865 /* Parse the attribute-list. */
16866 attribute_list = cp_parser_attribute_list (parser);
16868 /* If the next token is a `)', then there is no attribute
16870 attribute_list = NULL;
16872 /* Look for the two `)' tokens. */
16873 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16874 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16876 /* Add these new attributes to the list. */
16877 attributes = chainon (attributes, attribute_list);
16883 /* Parse an attribute-list.
16887 attribute-list , attribute
16891 identifier ( identifier )
16892 identifier ( identifier , expression-list )
16893 identifier ( expression-list )
16895 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16896 to an attribute. The TREE_PURPOSE of each node is the identifier
16897 indicating which attribute is in use. The TREE_VALUE represents
16898 the arguments, if any. */
16901 cp_parser_attribute_list (cp_parser* parser)
16903 tree attribute_list = NULL_TREE;
16904 bool save_translate_strings_p = parser->translate_strings_p;
16906 parser->translate_strings_p = false;
16913 /* Look for the identifier. We also allow keywords here; for
16914 example `__attribute__ ((const))' is legal. */
16915 token = cp_lexer_peek_token (parser->lexer);
16916 if (token->type == CPP_NAME
16917 || token->type == CPP_KEYWORD)
16919 tree arguments = NULL_TREE;
16921 /* Consume the token. */
16922 token = cp_lexer_consume_token (parser->lexer);
16924 /* Save away the identifier that indicates which attribute
16926 identifier = token->u.value;
16927 attribute = build_tree_list (identifier, NULL_TREE);
16929 /* Peek at the next token. */
16930 token = cp_lexer_peek_token (parser->lexer);
16931 /* If it's an `(', then parse the attribute arguments. */
16932 if (token->type == CPP_OPEN_PAREN)
16934 arguments = cp_parser_parenthesized_expression_list
16935 (parser, true, /*cast_p=*/false,
16936 /*allow_expansion_p=*/false,
16937 /*non_constant_p=*/NULL);
16938 /* Save the arguments away. */
16939 TREE_VALUE (attribute) = arguments;
16942 if (arguments != error_mark_node)
16944 /* Add this attribute to the list. */
16945 TREE_CHAIN (attribute) = attribute_list;
16946 attribute_list = attribute;
16949 token = cp_lexer_peek_token (parser->lexer);
16951 /* Now, look for more attributes. If the next token isn't a
16952 `,', we're done. */
16953 if (token->type != CPP_COMMA)
16956 /* Consume the comma and keep going. */
16957 cp_lexer_consume_token (parser->lexer);
16959 parser->translate_strings_p = save_translate_strings_p;
16961 /* We built up the list in reverse order. */
16962 return nreverse (attribute_list);
16965 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16966 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16967 current value of the PEDANTIC flag, regardless of whether or not
16968 the `__extension__' keyword is present. The caller is responsible
16969 for restoring the value of the PEDANTIC flag. */
16972 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16974 /* Save the old value of the PEDANTIC flag. */
16975 *saved_pedantic = pedantic;
16977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16979 /* Consume the `__extension__' token. */
16980 cp_lexer_consume_token (parser->lexer);
16981 /* We're not being pedantic while the `__extension__' keyword is
16991 /* Parse a label declaration.
16994 __label__ label-declarator-seq ;
16996 label-declarator-seq:
16997 identifier , label-declarator-seq
17001 cp_parser_label_declaration (cp_parser* parser)
17003 /* Look for the `__label__' keyword. */
17004 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17010 /* Look for an identifier. */
17011 identifier = cp_parser_identifier (parser);
17012 /* If we failed, stop. */
17013 if (identifier == error_mark_node)
17015 /* Declare it as a label. */
17016 finish_label_decl (identifier);
17017 /* If the next token is a `;', stop. */
17018 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17020 /* Look for the `,' separating the label declarations. */
17021 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17024 /* Look for the final `;'. */
17025 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17028 /* Support Functions */
17030 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17031 NAME should have one of the representations used for an
17032 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17033 is returned. If PARSER->SCOPE is a dependent type, then a
17034 SCOPE_REF is returned.
17036 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17037 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17038 was formed. Abstractly, such entities should not be passed to this
17039 function, because they do not need to be looked up, but it is
17040 simpler to check for this special case here, rather than at the
17043 In cases not explicitly covered above, this function returns a
17044 DECL, OVERLOAD, or baselink representing the result of the lookup.
17045 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17048 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17049 (e.g., "struct") that was used. In that case bindings that do not
17050 refer to types are ignored.
17052 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17055 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17058 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17061 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17062 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17063 NULL_TREE otherwise. */
17066 cp_parser_lookup_name (cp_parser *parser, tree name,
17067 enum tag_types tag_type,
17070 bool check_dependency,
17071 tree *ambiguous_decls,
17072 location_t name_location)
17076 tree object_type = parser->context->object_type;
17078 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17079 flags |= LOOKUP_COMPLAIN;
17081 /* Assume that the lookup will be unambiguous. */
17082 if (ambiguous_decls)
17083 *ambiguous_decls = NULL_TREE;
17085 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17086 no longer valid. Note that if we are parsing tentatively, and
17087 the parse fails, OBJECT_TYPE will be automatically restored. */
17088 parser->context->object_type = NULL_TREE;
17090 if (name == error_mark_node)
17091 return error_mark_node;
17093 /* A template-id has already been resolved; there is no lookup to
17095 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17097 if (BASELINK_P (name))
17099 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17100 == TEMPLATE_ID_EXPR);
17104 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17105 it should already have been checked to make sure that the name
17106 used matches the type being destroyed. */
17107 if (TREE_CODE (name) == BIT_NOT_EXPR)
17111 /* Figure out to which type this destructor applies. */
17113 type = parser->scope;
17114 else if (object_type)
17115 type = object_type;
17117 type = current_class_type;
17118 /* If that's not a class type, there is no destructor. */
17119 if (!type || !CLASS_TYPE_P (type))
17120 return error_mark_node;
17121 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17122 lazily_declare_fn (sfk_destructor, type);
17123 if (!CLASSTYPE_DESTRUCTORS (type))
17124 return error_mark_node;
17125 /* If it was a class type, return the destructor. */
17126 return CLASSTYPE_DESTRUCTORS (type);
17129 /* By this point, the NAME should be an ordinary identifier. If
17130 the id-expression was a qualified name, the qualifying scope is
17131 stored in PARSER->SCOPE at this point. */
17132 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17134 /* Perform the lookup. */
17139 if (parser->scope == error_mark_node)
17140 return error_mark_node;
17142 /* If the SCOPE is dependent, the lookup must be deferred until
17143 the template is instantiated -- unless we are explicitly
17144 looking up names in uninstantiated templates. Even then, we
17145 cannot look up the name if the scope is not a class type; it
17146 might, for example, be a template type parameter. */
17147 dependent_p = (TYPE_P (parser->scope)
17148 && dependent_scope_p (parser->scope));
17149 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17151 /* Defer lookup. */
17152 decl = error_mark_node;
17155 tree pushed_scope = NULL_TREE;
17157 /* If PARSER->SCOPE is a dependent type, then it must be a
17158 class type, and we must not be checking dependencies;
17159 otherwise, we would have processed this lookup above. So
17160 that PARSER->SCOPE is not considered a dependent base by
17161 lookup_member, we must enter the scope here. */
17163 pushed_scope = push_scope (parser->scope);
17164 /* If the PARSER->SCOPE is a template specialization, it
17165 may be instantiated during name lookup. In that case,
17166 errors may be issued. Even if we rollback the current
17167 tentative parse, those errors are valid. */
17168 decl = lookup_qualified_name (parser->scope, name,
17169 tag_type != none_type,
17170 /*complain=*/true);
17172 /* If we have a single function from a using decl, pull it out. */
17173 if (TREE_CODE (decl) == OVERLOAD
17174 && !really_overloaded_fn (decl))
17175 decl = OVL_FUNCTION (decl);
17178 pop_scope (pushed_scope);
17181 /* If the scope is a dependent type and either we deferred lookup or
17182 we did lookup but didn't find the name, rememeber the name. */
17183 if (decl == error_mark_node && TYPE_P (parser->scope)
17184 && dependent_type_p (parser->scope))
17190 /* The resolution to Core Issue 180 says that `struct
17191 A::B' should be considered a type-name, even if `A'
17193 type = make_typename_type (parser->scope, name, tag_type,
17194 /*complain=*/tf_error);
17195 decl = TYPE_NAME (type);
17197 else if (is_template
17198 && (cp_parser_next_token_ends_template_argument_p (parser)
17199 || cp_lexer_next_token_is (parser->lexer,
17201 decl = make_unbound_class_template (parser->scope,
17203 /*complain=*/tf_error);
17205 decl = build_qualified_name (/*type=*/NULL_TREE,
17206 parser->scope, name,
17209 parser->qualifying_scope = parser->scope;
17210 parser->object_scope = NULL_TREE;
17212 else if (object_type)
17214 tree object_decl = NULL_TREE;
17215 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17216 OBJECT_TYPE is not a class. */
17217 if (CLASS_TYPE_P (object_type))
17218 /* If the OBJECT_TYPE is a template specialization, it may
17219 be instantiated during name lookup. In that case, errors
17220 may be issued. Even if we rollback the current tentative
17221 parse, those errors are valid. */
17222 object_decl = lookup_member (object_type,
17225 tag_type != none_type);
17226 /* Look it up in the enclosing context, too. */
17227 decl = lookup_name_real (name, tag_type != none_type,
17229 /*block_p=*/true, is_namespace, flags);
17230 parser->object_scope = object_type;
17231 parser->qualifying_scope = NULL_TREE;
17233 decl = object_decl;
17237 decl = lookup_name_real (name, tag_type != none_type,
17239 /*block_p=*/true, is_namespace, flags);
17240 parser->qualifying_scope = NULL_TREE;
17241 parser->object_scope = NULL_TREE;
17244 /* If the lookup failed, let our caller know. */
17245 if (!decl || decl == error_mark_node)
17246 return error_mark_node;
17248 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17249 if (TREE_CODE (decl) == TREE_LIST)
17251 if (ambiguous_decls)
17252 *ambiguous_decls = decl;
17253 /* The error message we have to print is too complicated for
17254 cp_parser_error, so we incorporate its actions directly. */
17255 if (!cp_parser_simulate_error (parser))
17257 error ("%Hreference to %qD is ambiguous",
17258 &name_location, name);
17259 print_candidates (decl);
17261 return error_mark_node;
17264 gcc_assert (DECL_P (decl)
17265 || TREE_CODE (decl) == OVERLOAD
17266 || TREE_CODE (decl) == SCOPE_REF
17267 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17268 || BASELINK_P (decl));
17270 /* If we have resolved the name of a member declaration, check to
17271 see if the declaration is accessible. When the name resolves to
17272 set of overloaded functions, accessibility is checked when
17273 overload resolution is done.
17275 During an explicit instantiation, access is not checked at all,
17276 as per [temp.explicit]. */
17278 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17283 /* Like cp_parser_lookup_name, but for use in the typical case where
17284 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17285 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17288 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17290 return cp_parser_lookup_name (parser, name,
17292 /*is_template=*/false,
17293 /*is_namespace=*/false,
17294 /*check_dependency=*/true,
17295 /*ambiguous_decls=*/NULL,
17299 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17300 the current context, return the TYPE_DECL. If TAG_NAME_P is
17301 true, the DECL indicates the class being defined in a class-head,
17302 or declared in an elaborated-type-specifier.
17304 Otherwise, return DECL. */
17307 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17309 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17310 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17313 template <typename T> struct B;
17316 template <typename T> struct A::B {};
17318 Similarly, in an elaborated-type-specifier:
17320 namespace N { struct X{}; }
17323 template <typename T> friend struct N::X;
17326 However, if the DECL refers to a class type, and we are in
17327 the scope of the class, then the name lookup automatically
17328 finds the TYPE_DECL created by build_self_reference rather
17329 than a TEMPLATE_DECL. For example, in:
17331 template <class T> struct S {
17335 there is no need to handle such case. */
17337 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17338 return DECL_TEMPLATE_RESULT (decl);
17343 /* If too many, or too few, template-parameter lists apply to the
17344 declarator, issue an error message. Returns TRUE if all went well,
17345 and FALSE otherwise. */
17348 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17349 cp_declarator *declarator,
17350 location_t declarator_location)
17352 unsigned num_templates;
17354 /* We haven't seen any classes that involve template parameters yet. */
17357 switch (declarator->kind)
17360 if (declarator->u.id.qualifying_scope)
17365 scope = declarator->u.id.qualifying_scope;
17366 member = declarator->u.id.unqualified_name;
17368 while (scope && CLASS_TYPE_P (scope))
17370 /* You're supposed to have one `template <...>'
17371 for every template class, but you don't need one
17372 for a full specialization. For example:
17374 template <class T> struct S{};
17375 template <> struct S<int> { void f(); };
17376 void S<int>::f () {}
17378 is correct; there shouldn't be a `template <>' for
17379 the definition of `S<int>::f'. */
17380 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17381 /* If SCOPE does not have template information of any
17382 kind, then it is not a template, nor is it nested
17383 within a template. */
17385 if (explicit_class_specialization_p (scope))
17387 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17390 scope = TYPE_CONTEXT (scope);
17393 else if (TREE_CODE (declarator->u.id.unqualified_name)
17394 == TEMPLATE_ID_EXPR)
17395 /* If the DECLARATOR has the form `X<y>' then it uses one
17396 additional level of template parameters. */
17399 return cp_parser_check_template_parameters (parser,
17401 declarator_location);
17406 case cdk_reference:
17408 return (cp_parser_check_declarator_template_parameters
17409 (parser, declarator->declarator, declarator_location));
17415 gcc_unreachable ();
17420 /* NUM_TEMPLATES were used in the current declaration. If that is
17421 invalid, return FALSE and issue an error messages. Otherwise,
17425 cp_parser_check_template_parameters (cp_parser* parser,
17426 unsigned num_templates,
17427 location_t location)
17429 /* If there are more template classes than parameter lists, we have
17432 template <class T> void S<T>::R<T>::f (); */
17433 if (parser->num_template_parameter_lists < num_templates)
17435 error ("%Htoo few template-parameter-lists", &location);
17438 /* If there are the same number of template classes and parameter
17439 lists, that's OK. */
17440 if (parser->num_template_parameter_lists == num_templates)
17442 /* If there are more, but only one more, then we are referring to a
17443 member template. That's OK too. */
17444 if (parser->num_template_parameter_lists == num_templates + 1)
17446 /* Otherwise, there are too many template parameter lists. We have
17449 template <class T> template <class U> void S::f(); */
17450 error ("%Htoo many template-parameter-lists", &location);
17454 /* Parse an optional `::' token indicating that the following name is
17455 from the global namespace. If so, PARSER->SCOPE is set to the
17456 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17457 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17458 Returns the new value of PARSER->SCOPE, if the `::' token is
17459 present, and NULL_TREE otherwise. */
17462 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17466 /* Peek at the next token. */
17467 token = cp_lexer_peek_token (parser->lexer);
17468 /* If we're looking at a `::' token then we're starting from the
17469 global namespace, not our current location. */
17470 if (token->type == CPP_SCOPE)
17472 /* Consume the `::' token. */
17473 cp_lexer_consume_token (parser->lexer);
17474 /* Set the SCOPE so that we know where to start the lookup. */
17475 parser->scope = global_namespace;
17476 parser->qualifying_scope = global_namespace;
17477 parser->object_scope = NULL_TREE;
17479 return parser->scope;
17481 else if (!current_scope_valid_p)
17483 parser->scope = NULL_TREE;
17484 parser->qualifying_scope = NULL_TREE;
17485 parser->object_scope = NULL_TREE;
17491 /* Returns TRUE if the upcoming token sequence is the start of a
17492 constructor declarator. If FRIEND_P is true, the declarator is
17493 preceded by the `friend' specifier. */
17496 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17498 bool constructor_p;
17499 tree type_decl = NULL_TREE;
17500 bool nested_name_p;
17501 cp_token *next_token;
17503 /* The common case is that this is not a constructor declarator, so
17504 try to avoid doing lots of work if at all possible. It's not
17505 valid declare a constructor at function scope. */
17506 if (parser->in_function_body)
17508 /* And only certain tokens can begin a constructor declarator. */
17509 next_token = cp_lexer_peek_token (parser->lexer);
17510 if (next_token->type != CPP_NAME
17511 && next_token->type != CPP_SCOPE
17512 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17513 && next_token->type != CPP_TEMPLATE_ID)
17516 /* Parse tentatively; we are going to roll back all of the tokens
17518 cp_parser_parse_tentatively (parser);
17519 /* Assume that we are looking at a constructor declarator. */
17520 constructor_p = true;
17522 /* Look for the optional `::' operator. */
17523 cp_parser_global_scope_opt (parser,
17524 /*current_scope_valid_p=*/false);
17525 /* Look for the nested-name-specifier. */
17527 = (cp_parser_nested_name_specifier_opt (parser,
17528 /*typename_keyword_p=*/false,
17529 /*check_dependency_p=*/false,
17531 /*is_declaration=*/false)
17533 /* Outside of a class-specifier, there must be a
17534 nested-name-specifier. */
17535 if (!nested_name_p &&
17536 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17538 constructor_p = false;
17539 /* If we still think that this might be a constructor-declarator,
17540 look for a class-name. */
17545 template <typename T> struct S { S(); };
17546 template <typename T> S<T>::S ();
17548 we must recognize that the nested `S' names a class.
17551 template <typename T> S<T>::S<T> ();
17553 we must recognize that the nested `S' names a template. */
17554 type_decl = cp_parser_class_name (parser,
17555 /*typename_keyword_p=*/false,
17556 /*template_keyword_p=*/false,
17558 /*check_dependency_p=*/false,
17559 /*class_head_p=*/false,
17560 /*is_declaration=*/false);
17561 /* If there was no class-name, then this is not a constructor. */
17562 constructor_p = !cp_parser_error_occurred (parser);
17565 /* If we're still considering a constructor, we have to see a `(',
17566 to begin the parameter-declaration-clause, followed by either a
17567 `)', an `...', or a decl-specifier. We need to check for a
17568 type-specifier to avoid being fooled into thinking that:
17572 is a constructor. (It is actually a function named `f' that
17573 takes one parameter (of type `int') and returns a value of type
17576 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17579 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17580 /* A parameter declaration begins with a decl-specifier,
17581 which is either the "attribute" keyword, a storage class
17582 specifier, or (usually) a type-specifier. */
17583 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17586 tree pushed_scope = NULL_TREE;
17587 unsigned saved_num_template_parameter_lists;
17589 /* Names appearing in the type-specifier should be looked up
17590 in the scope of the class. */
17591 if (current_class_type)
17595 type = TREE_TYPE (type_decl);
17596 if (TREE_CODE (type) == TYPENAME_TYPE)
17598 type = resolve_typename_type (type,
17599 /*only_current_p=*/false);
17600 if (TREE_CODE (type) == TYPENAME_TYPE)
17602 cp_parser_abort_tentative_parse (parser);
17606 pushed_scope = push_scope (type);
17609 /* Inside the constructor parameter list, surrounding
17610 template-parameter-lists do not apply. */
17611 saved_num_template_parameter_lists
17612 = parser->num_template_parameter_lists;
17613 parser->num_template_parameter_lists = 0;
17615 /* Look for the type-specifier. */
17616 cp_parser_type_specifier (parser,
17617 CP_PARSER_FLAGS_NONE,
17618 /*decl_specs=*/NULL,
17619 /*is_declarator=*/true,
17620 /*declares_class_or_enum=*/NULL,
17621 /*is_cv_qualifier=*/NULL);
17623 parser->num_template_parameter_lists
17624 = saved_num_template_parameter_lists;
17626 /* Leave the scope of the class. */
17628 pop_scope (pushed_scope);
17630 constructor_p = !cp_parser_error_occurred (parser);
17634 constructor_p = false;
17635 /* We did not really want to consume any tokens. */
17636 cp_parser_abort_tentative_parse (parser);
17638 return constructor_p;
17641 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17642 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17643 they must be performed once we are in the scope of the function.
17645 Returns the function defined. */
17648 cp_parser_function_definition_from_specifiers_and_declarator
17649 (cp_parser* parser,
17650 cp_decl_specifier_seq *decl_specifiers,
17652 const cp_declarator *declarator)
17657 /* Begin the function-definition. */
17658 success_p = start_function (decl_specifiers, declarator, attributes);
17660 /* The things we're about to see are not directly qualified by any
17661 template headers we've seen thus far. */
17662 reset_specialization ();
17664 /* If there were names looked up in the decl-specifier-seq that we
17665 did not check, check them now. We must wait until we are in the
17666 scope of the function to perform the checks, since the function
17667 might be a friend. */
17668 perform_deferred_access_checks ();
17672 /* Skip the entire function. */
17673 cp_parser_skip_to_end_of_block_or_statement (parser);
17674 fn = error_mark_node;
17676 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17678 /* Seen already, skip it. An error message has already been output. */
17679 cp_parser_skip_to_end_of_block_or_statement (parser);
17680 fn = current_function_decl;
17681 current_function_decl = NULL_TREE;
17682 /* If this is a function from a class, pop the nested class. */
17683 if (current_class_name)
17684 pop_nested_class ();
17687 fn = cp_parser_function_definition_after_declarator (parser,
17688 /*inline_p=*/false);
17693 /* Parse the part of a function-definition that follows the
17694 declarator. INLINE_P is TRUE iff this function is an inline
17695 function defined with a class-specifier.
17697 Returns the function defined. */
17700 cp_parser_function_definition_after_declarator (cp_parser* parser,
17704 bool ctor_initializer_p = false;
17705 bool saved_in_unbraced_linkage_specification_p;
17706 bool saved_in_function_body;
17707 unsigned saved_num_template_parameter_lists;
17710 saved_in_function_body = parser->in_function_body;
17711 parser->in_function_body = true;
17712 /* If the next token is `return', then the code may be trying to
17713 make use of the "named return value" extension that G++ used to
17715 token = cp_lexer_peek_token (parser->lexer);
17716 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17718 /* Consume the `return' keyword. */
17719 cp_lexer_consume_token (parser->lexer);
17720 /* Look for the identifier that indicates what value is to be
17722 cp_parser_identifier (parser);
17723 /* Issue an error message. */
17724 error ("%Hnamed return values are no longer supported",
17726 /* Skip tokens until we reach the start of the function body. */
17729 cp_token *token = cp_lexer_peek_token (parser->lexer);
17730 if (token->type == CPP_OPEN_BRACE
17731 || token->type == CPP_EOF
17732 || token->type == CPP_PRAGMA_EOL)
17734 cp_lexer_consume_token (parser->lexer);
17737 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17738 anything declared inside `f'. */
17739 saved_in_unbraced_linkage_specification_p
17740 = parser->in_unbraced_linkage_specification_p;
17741 parser->in_unbraced_linkage_specification_p = false;
17742 /* Inside the function, surrounding template-parameter-lists do not
17744 saved_num_template_parameter_lists
17745 = parser->num_template_parameter_lists;
17746 parser->num_template_parameter_lists = 0;
17747 /* If the next token is `try', then we are looking at a
17748 function-try-block. */
17749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17750 ctor_initializer_p = cp_parser_function_try_block (parser);
17751 /* A function-try-block includes the function-body, so we only do
17752 this next part if we're not processing a function-try-block. */
17755 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17757 /* Finish the function. */
17758 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17759 (inline_p ? 2 : 0));
17760 /* Generate code for it, if necessary. */
17761 expand_or_defer_fn (fn);
17762 /* Restore the saved values. */
17763 parser->in_unbraced_linkage_specification_p
17764 = saved_in_unbraced_linkage_specification_p;
17765 parser->num_template_parameter_lists
17766 = saved_num_template_parameter_lists;
17767 parser->in_function_body = saved_in_function_body;
17772 /* Parse a template-declaration, assuming that the `export' (and
17773 `extern') keywords, if present, has already been scanned. MEMBER_P
17774 is as for cp_parser_template_declaration. */
17777 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17779 tree decl = NULL_TREE;
17780 VEC (deferred_access_check,gc) *checks;
17781 tree parameter_list;
17782 bool friend_p = false;
17783 bool need_lang_pop;
17786 /* Look for the `template' keyword. */
17787 token = cp_lexer_peek_token (parser->lexer);
17788 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17792 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17794 if (at_class_scope_p () && current_function_decl)
17796 /* 14.5.2.2 [temp.mem]
17798 A local class shall not have member templates. */
17799 error ("%Hinvalid declaration of member template in local class",
17801 cp_parser_skip_to_end_of_block_or_statement (parser);
17806 A template ... shall not have C linkage. */
17807 if (current_lang_name == lang_name_c)
17809 error ("%Htemplate with C linkage", &token->location);
17810 /* Give it C++ linkage to avoid confusing other parts of the
17812 push_lang_context (lang_name_cplusplus);
17813 need_lang_pop = true;
17816 need_lang_pop = false;
17818 /* We cannot perform access checks on the template parameter
17819 declarations until we know what is being declared, just as we
17820 cannot check the decl-specifier list. */
17821 push_deferring_access_checks (dk_deferred);
17823 /* If the next token is `>', then we have an invalid
17824 specialization. Rather than complain about an invalid template
17825 parameter, issue an error message here. */
17826 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17828 cp_parser_error (parser, "invalid explicit specialization");
17829 begin_specialization ();
17830 parameter_list = NULL_TREE;
17833 /* Parse the template parameters. */
17834 parameter_list = cp_parser_template_parameter_list (parser);
17836 /* Get the deferred access checks from the parameter list. These
17837 will be checked once we know what is being declared, as for a
17838 member template the checks must be performed in the scope of the
17839 class containing the member. */
17840 checks = get_deferred_access_checks ();
17842 /* Look for the `>'. */
17843 cp_parser_skip_to_end_of_template_parameter_list (parser);
17844 /* We just processed one more parameter list. */
17845 ++parser->num_template_parameter_lists;
17846 /* If the next token is `template', there are more template
17848 if (cp_lexer_next_token_is_keyword (parser->lexer,
17850 cp_parser_template_declaration_after_export (parser, member_p);
17853 /* There are no access checks when parsing a template, as we do not
17854 know if a specialization will be a friend. */
17855 push_deferring_access_checks (dk_no_check);
17856 token = cp_lexer_peek_token (parser->lexer);
17857 decl = cp_parser_single_declaration (parser,
17860 /*explicit_specialization_p=*/false,
17862 pop_deferring_access_checks ();
17864 /* If this is a member template declaration, let the front
17866 if (member_p && !friend_p && decl)
17868 if (TREE_CODE (decl) == TYPE_DECL)
17869 cp_parser_check_access_in_redeclaration (decl, token->location);
17871 decl = finish_member_template_decl (decl);
17873 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17874 make_friend_class (current_class_type, TREE_TYPE (decl),
17875 /*complain=*/true);
17877 /* We are done with the current parameter list. */
17878 --parser->num_template_parameter_lists;
17880 pop_deferring_access_checks ();
17883 finish_template_decl (parameter_list);
17885 /* Register member declarations. */
17886 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17887 finish_member_declaration (decl);
17888 /* For the erroneous case of a template with C linkage, we pushed an
17889 implicit C++ linkage scope; exit that scope now. */
17891 pop_lang_context ();
17892 /* If DECL is a function template, we must return to parse it later.
17893 (Even though there is no definition, there might be default
17894 arguments that need handling.) */
17895 if (member_p && decl
17896 && (TREE_CODE (decl) == FUNCTION_DECL
17897 || DECL_FUNCTION_TEMPLATE_P (decl)))
17898 TREE_VALUE (parser->unparsed_functions_queues)
17899 = tree_cons (NULL_TREE, decl,
17900 TREE_VALUE (parser->unparsed_functions_queues));
17903 /* Perform the deferred access checks from a template-parameter-list.
17904 CHECKS is a TREE_LIST of access checks, as returned by
17905 get_deferred_access_checks. */
17908 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17910 ++processing_template_parmlist;
17911 perform_access_checks (checks);
17912 --processing_template_parmlist;
17915 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17916 `function-definition' sequence. MEMBER_P is true, this declaration
17917 appears in a class scope.
17919 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17920 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17923 cp_parser_single_declaration (cp_parser* parser,
17924 VEC (deferred_access_check,gc)* checks,
17926 bool explicit_specialization_p,
17929 int declares_class_or_enum;
17930 tree decl = NULL_TREE;
17931 cp_decl_specifier_seq decl_specifiers;
17932 bool function_definition_p = false;
17933 cp_token *decl_spec_token_start;
17935 /* This function is only used when processing a template
17937 gcc_assert (innermost_scope_kind () == sk_template_parms
17938 || innermost_scope_kind () == sk_template_spec);
17940 /* Defer access checks until we know what is being declared. */
17941 push_deferring_access_checks (dk_deferred);
17943 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17945 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17946 cp_parser_decl_specifier_seq (parser,
17947 CP_PARSER_FLAGS_OPTIONAL,
17949 &declares_class_or_enum);
17951 *friend_p = cp_parser_friend_p (&decl_specifiers);
17953 /* There are no template typedefs. */
17954 if (decl_specifiers.specs[(int) ds_typedef])
17956 error ("%Htemplate declaration of %qs",
17957 &decl_spec_token_start->location, "typedef");
17958 decl = error_mark_node;
17961 /* Gather up the access checks that occurred the
17962 decl-specifier-seq. */
17963 stop_deferring_access_checks ();
17965 /* Check for the declaration of a template class. */
17966 if (declares_class_or_enum)
17968 if (cp_parser_declares_only_class_p (parser))
17970 decl = shadow_tag (&decl_specifiers);
17975 friend template <typename T> struct A<T>::B;
17978 A<T>::B will be represented by a TYPENAME_TYPE, and
17979 therefore not recognized by shadow_tag. */
17980 if (friend_p && *friend_p
17982 && decl_specifiers.type
17983 && TYPE_P (decl_specifiers.type))
17984 decl = decl_specifiers.type;
17986 if (decl && decl != error_mark_node)
17987 decl = TYPE_NAME (decl);
17989 decl = error_mark_node;
17991 /* Perform access checks for template parameters. */
17992 cp_parser_perform_template_parameter_access_checks (checks);
17995 /* If it's not a template class, try for a template function. If
17996 the next token is a `;', then this declaration does not declare
17997 anything. But, if there were errors in the decl-specifiers, then
17998 the error might well have come from an attempted class-specifier.
17999 In that case, there's no need to warn about a missing declarator. */
18001 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18002 || decl_specifiers.type != error_mark_node))
18004 decl = cp_parser_init_declarator (parser,
18007 /*function_definition_allowed_p=*/true,
18009 declares_class_or_enum,
18010 &function_definition_p);
18012 /* 7.1.1-1 [dcl.stc]
18014 A storage-class-specifier shall not be specified in an explicit
18015 specialization... */
18017 && explicit_specialization_p
18018 && decl_specifiers.storage_class != sc_none)
18020 error ("%Hexplicit template specialization cannot have a storage class",
18021 &decl_spec_token_start->location);
18022 decl = error_mark_node;
18026 pop_deferring_access_checks ();
18028 /* Clear any current qualification; whatever comes next is the start
18029 of something new. */
18030 parser->scope = NULL_TREE;
18031 parser->qualifying_scope = NULL_TREE;
18032 parser->object_scope = NULL_TREE;
18033 /* Look for a trailing `;' after the declaration. */
18034 if (!function_definition_p
18035 && (decl == error_mark_node
18036 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18037 cp_parser_skip_to_end_of_block_or_statement (parser);
18042 /* Parse a cast-expression that is not the operand of a unary "&". */
18045 cp_parser_simple_cast_expression (cp_parser *parser)
18047 return cp_parser_cast_expression (parser, /*address_p=*/false,
18048 /*cast_p=*/false, NULL);
18051 /* Parse a functional cast to TYPE. Returns an expression
18052 representing the cast. */
18055 cp_parser_functional_cast (cp_parser* parser, tree type)
18057 tree expression_list;
18061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18063 maybe_warn_cpp0x ("extended initializer lists");
18064 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18065 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18066 if (TREE_CODE (type) == TYPE_DECL)
18067 type = TREE_TYPE (type);
18068 return finish_compound_literal (type, expression_list);
18072 = cp_parser_parenthesized_expression_list (parser, false,
18074 /*allow_expansion_p=*/true,
18075 /*non_constant_p=*/NULL);
18077 cast = build_functional_cast (type, expression_list,
18078 tf_warning_or_error);
18079 /* [expr.const]/1: In an integral constant expression "only type
18080 conversions to integral or enumeration type can be used". */
18081 if (TREE_CODE (type) == TYPE_DECL)
18082 type = TREE_TYPE (type);
18083 if (cast != error_mark_node
18084 && !cast_valid_in_integral_constant_expression_p (type)
18085 && (cp_parser_non_integral_constant_expression
18086 (parser, "a call to a constructor")))
18087 return error_mark_node;
18091 /* Save the tokens that make up the body of a member function defined
18092 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18093 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18094 specifiers applied to the declaration. Returns the FUNCTION_DECL
18095 for the member function. */
18098 cp_parser_save_member_function_body (cp_parser* parser,
18099 cp_decl_specifier_seq *decl_specifiers,
18100 cp_declarator *declarator,
18107 /* Create the function-declaration. */
18108 fn = start_method (decl_specifiers, declarator, attributes);
18109 /* If something went badly wrong, bail out now. */
18110 if (fn == error_mark_node)
18112 /* If there's a function-body, skip it. */
18113 if (cp_parser_token_starts_function_definition_p
18114 (cp_lexer_peek_token (parser->lexer)))
18115 cp_parser_skip_to_end_of_block_or_statement (parser);
18116 return error_mark_node;
18119 /* Remember it, if there default args to post process. */
18120 cp_parser_save_default_args (parser, fn);
18122 /* Save away the tokens that make up the body of the
18124 first = parser->lexer->next_token;
18125 /* We can have braced-init-list mem-initializers before the fn body. */
18126 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18128 cp_lexer_consume_token (parser->lexer);
18129 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18130 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18132 /* cache_group will stop after an un-nested { } pair, too. */
18133 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18136 /* variadic mem-inits have ... after the ')'. */
18137 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18138 cp_lexer_consume_token (parser->lexer);
18141 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18142 /* Handle function try blocks. */
18143 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18144 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18145 last = parser->lexer->next_token;
18147 /* Save away the inline definition; we will process it when the
18148 class is complete. */
18149 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18150 DECL_PENDING_INLINE_P (fn) = 1;
18152 /* We need to know that this was defined in the class, so that
18153 friend templates are handled correctly. */
18154 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18156 /* We're done with the inline definition. */
18157 finish_method (fn);
18159 /* Add FN to the queue of functions to be parsed later. */
18160 TREE_VALUE (parser->unparsed_functions_queues)
18161 = tree_cons (NULL_TREE, fn,
18162 TREE_VALUE (parser->unparsed_functions_queues));
18167 /* Parse a template-argument-list, as well as the trailing ">" (but
18168 not the opening ">"). See cp_parser_template_argument_list for the
18172 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18176 tree saved_qualifying_scope;
18177 tree saved_object_scope;
18178 bool saved_greater_than_is_operator_p;
18179 bool saved_skip_evaluation;
18183 When parsing a template-id, the first non-nested `>' is taken as
18184 the end of the template-argument-list rather than a greater-than
18186 saved_greater_than_is_operator_p
18187 = parser->greater_than_is_operator_p;
18188 parser->greater_than_is_operator_p = false;
18189 /* Parsing the argument list may modify SCOPE, so we save it
18191 saved_scope = parser->scope;
18192 saved_qualifying_scope = parser->qualifying_scope;
18193 saved_object_scope = parser->object_scope;
18194 /* We need to evaluate the template arguments, even though this
18195 template-id may be nested within a "sizeof". */
18196 saved_skip_evaluation = skip_evaluation;
18197 skip_evaluation = false;
18198 /* Parse the template-argument-list itself. */
18199 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18200 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18201 arguments = NULL_TREE;
18203 arguments = cp_parser_template_argument_list (parser);
18204 /* Look for the `>' that ends the template-argument-list. If we find
18205 a '>>' instead, it's probably just a typo. */
18206 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18208 if (cxx_dialect != cxx98)
18210 /* In C++0x, a `>>' in a template argument list or cast
18211 expression is considered to be two separate `>'
18212 tokens. So, change the current token to a `>', but don't
18213 consume it: it will be consumed later when the outer
18214 template argument list (or cast expression) is parsed.
18215 Note that this replacement of `>' for `>>' is necessary
18216 even if we are parsing tentatively: in the tentative
18217 case, after calling
18218 cp_parser_enclosed_template_argument_list we will always
18219 throw away all of the template arguments and the first
18220 closing `>', either because the template argument list
18221 was erroneous or because we are replacing those tokens
18222 with a CPP_TEMPLATE_ID token. The second `>' (which will
18223 not have been thrown away) is needed either to close an
18224 outer template argument list or to complete a new-style
18226 cp_token *token = cp_lexer_peek_token (parser->lexer);
18227 token->type = CPP_GREATER;
18229 else if (!saved_greater_than_is_operator_p)
18231 /* If we're in a nested template argument list, the '>>' has
18232 to be a typo for '> >'. We emit the error message, but we
18233 continue parsing and we push a '>' as next token, so that
18234 the argument list will be parsed correctly. Note that the
18235 global source location is still on the token before the
18236 '>>', so we need to say explicitly where we want it. */
18237 cp_token *token = cp_lexer_peek_token (parser->lexer);
18238 error ("%H%<>>%> should be %<> >%> "
18239 "within a nested template argument list",
18242 token->type = CPP_GREATER;
18246 /* If this is not a nested template argument list, the '>>'
18247 is a typo for '>'. Emit an error message and continue.
18248 Same deal about the token location, but here we can get it
18249 right by consuming the '>>' before issuing the diagnostic. */
18250 cp_token *token = cp_lexer_consume_token (parser->lexer);
18251 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18252 "a template argument list", &token->location);
18256 cp_parser_skip_to_end_of_template_parameter_list (parser);
18257 /* The `>' token might be a greater-than operator again now. */
18258 parser->greater_than_is_operator_p
18259 = saved_greater_than_is_operator_p;
18260 /* Restore the SAVED_SCOPE. */
18261 parser->scope = saved_scope;
18262 parser->qualifying_scope = saved_qualifying_scope;
18263 parser->object_scope = saved_object_scope;
18264 skip_evaluation = saved_skip_evaluation;
18269 /* MEMBER_FUNCTION is a member function, or a friend. If default
18270 arguments, or the body of the function have not yet been parsed,
18274 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18276 /* If this member is a template, get the underlying
18278 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18279 member_function = DECL_TEMPLATE_RESULT (member_function);
18281 /* There should not be any class definitions in progress at this
18282 point; the bodies of members are only parsed outside of all class
18284 gcc_assert (parser->num_classes_being_defined == 0);
18285 /* While we're parsing the member functions we might encounter more
18286 classes. We want to handle them right away, but we don't want
18287 them getting mixed up with functions that are currently in the
18289 parser->unparsed_functions_queues
18290 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18292 /* Make sure that any template parameters are in scope. */
18293 maybe_begin_member_template_processing (member_function);
18295 /* If the body of the function has not yet been parsed, parse it
18297 if (DECL_PENDING_INLINE_P (member_function))
18299 tree function_scope;
18300 cp_token_cache *tokens;
18302 /* The function is no longer pending; we are processing it. */
18303 tokens = DECL_PENDING_INLINE_INFO (member_function);
18304 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18305 DECL_PENDING_INLINE_P (member_function) = 0;
18307 /* If this is a local class, enter the scope of the containing
18309 function_scope = current_function_decl;
18310 if (function_scope)
18311 push_function_context ();
18313 /* Push the body of the function onto the lexer stack. */
18314 cp_parser_push_lexer_for_tokens (parser, tokens);
18316 /* Let the front end know that we going to be defining this
18318 start_preparsed_function (member_function, NULL_TREE,
18319 SF_PRE_PARSED | SF_INCLASS_INLINE);
18321 /* Don't do access checking if it is a templated function. */
18322 if (processing_template_decl)
18323 push_deferring_access_checks (dk_no_check);
18325 /* Now, parse the body of the function. */
18326 cp_parser_function_definition_after_declarator (parser,
18327 /*inline_p=*/true);
18329 if (processing_template_decl)
18330 pop_deferring_access_checks ();
18332 /* Leave the scope of the containing function. */
18333 if (function_scope)
18334 pop_function_context ();
18335 cp_parser_pop_lexer (parser);
18338 /* Remove any template parameters from the symbol table. */
18339 maybe_end_member_template_processing ();
18341 /* Restore the queue. */
18342 parser->unparsed_functions_queues
18343 = TREE_CHAIN (parser->unparsed_functions_queues);
18346 /* If DECL contains any default args, remember it on the unparsed
18347 functions queue. */
18350 cp_parser_save_default_args (cp_parser* parser, tree decl)
18354 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18356 probe = TREE_CHAIN (probe))
18357 if (TREE_PURPOSE (probe))
18359 TREE_PURPOSE (parser->unparsed_functions_queues)
18360 = tree_cons (current_class_type, decl,
18361 TREE_PURPOSE (parser->unparsed_functions_queues));
18366 /* FN is a FUNCTION_DECL which may contains a parameter with an
18367 unparsed DEFAULT_ARG. Parse the default args now. This function
18368 assumes that the current scope is the scope in which the default
18369 argument should be processed. */
18372 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18374 bool saved_local_variables_forbidden_p;
18377 /* While we're parsing the default args, we might (due to the
18378 statement expression extension) encounter more classes. We want
18379 to handle them right away, but we don't want them getting mixed
18380 up with default args that are currently in the queue. */
18381 parser->unparsed_functions_queues
18382 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18384 /* Local variable names (and the `this' keyword) may not appear
18385 in a default argument. */
18386 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18387 parser->local_variables_forbidden_p = true;
18389 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18391 parm = TREE_CHAIN (parm))
18393 cp_token_cache *tokens;
18394 tree default_arg = TREE_PURPOSE (parm);
18396 VEC(tree,gc) *insts;
18403 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18404 /* This can happen for a friend declaration for a function
18405 already declared with default arguments. */
18408 /* Push the saved tokens for the default argument onto the parser's
18410 tokens = DEFARG_TOKENS (default_arg);
18411 cp_parser_push_lexer_for_tokens (parser, tokens);
18413 /* Parse the assignment-expression. */
18414 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18415 if (parsed_arg == error_mark_node)
18417 cp_parser_pop_lexer (parser);
18421 if (!processing_template_decl)
18422 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18424 TREE_PURPOSE (parm) = parsed_arg;
18426 /* Update any instantiations we've already created. */
18427 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18428 VEC_iterate (tree, insts, ix, copy); ix++)
18429 TREE_PURPOSE (copy) = parsed_arg;
18431 /* If the token stream has not been completely used up, then
18432 there was extra junk after the end of the default
18434 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18435 cp_parser_error (parser, "expected %<,%>");
18437 /* Revert to the main lexer. */
18438 cp_parser_pop_lexer (parser);
18441 /* Make sure no default arg is missing. */
18442 check_default_args (fn);
18444 /* Restore the state of local_variables_forbidden_p. */
18445 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18447 /* Restore the queue. */
18448 parser->unparsed_functions_queues
18449 = TREE_CHAIN (parser->unparsed_functions_queues);
18452 /* Parse the operand of `sizeof' (or a similar operator). Returns
18453 either a TYPE or an expression, depending on the form of the
18454 input. The KEYWORD indicates which kind of expression we have
18458 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18460 tree expr = NULL_TREE;
18461 const char *saved_message;
18463 bool saved_integral_constant_expression_p;
18464 bool saved_non_integral_constant_expression_p;
18465 bool pack_expansion_p = false;
18467 /* Types cannot be defined in a `sizeof' expression. Save away the
18469 saved_message = parser->type_definition_forbidden_message;
18470 /* And create the new one. */
18471 tmp = concat ("types may not be defined in %<",
18472 IDENTIFIER_POINTER (ridpointers[keyword]),
18473 "%> expressions", NULL);
18474 parser->type_definition_forbidden_message = tmp;
18476 /* The restrictions on constant-expressions do not apply inside
18477 sizeof expressions. */
18478 saved_integral_constant_expression_p
18479 = parser->integral_constant_expression_p;
18480 saved_non_integral_constant_expression_p
18481 = parser->non_integral_constant_expression_p;
18482 parser->integral_constant_expression_p = false;
18484 /* If it's a `...', then we are computing the length of a parameter
18486 if (keyword == RID_SIZEOF
18487 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18489 /* Consume the `...'. */
18490 cp_lexer_consume_token (parser->lexer);
18491 maybe_warn_variadic_templates ();
18493 /* Note that this is an expansion. */
18494 pack_expansion_p = true;
18497 /* Do not actually evaluate the expression. */
18499 /* If it's a `(', then we might be looking at the type-id
18501 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18504 bool saved_in_type_id_in_expr_p;
18506 /* We can't be sure yet whether we're looking at a type-id or an
18508 cp_parser_parse_tentatively (parser);
18509 /* Consume the `('. */
18510 cp_lexer_consume_token (parser->lexer);
18511 /* Parse the type-id. */
18512 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18513 parser->in_type_id_in_expr_p = true;
18514 type = cp_parser_type_id (parser);
18515 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18516 /* Now, look for the trailing `)'. */
18517 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18518 /* If all went well, then we're done. */
18519 if (cp_parser_parse_definitely (parser))
18521 cp_decl_specifier_seq decl_specs;
18523 /* Build a trivial decl-specifier-seq. */
18524 clear_decl_specs (&decl_specs);
18525 decl_specs.type = type;
18527 /* Call grokdeclarator to figure out what type this is. */
18528 expr = grokdeclarator (NULL,
18532 /*attrlist=*/NULL);
18536 /* If the type-id production did not work out, then we must be
18537 looking at the unary-expression production. */
18539 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18540 /*cast_p=*/false, NULL);
18542 if (pack_expansion_p)
18543 /* Build a pack expansion. */
18544 expr = make_pack_expansion (expr);
18546 /* Go back to evaluating expressions. */
18549 /* Free the message we created. */
18551 /* And restore the old one. */
18552 parser->type_definition_forbidden_message = saved_message;
18553 parser->integral_constant_expression_p
18554 = saved_integral_constant_expression_p;
18555 parser->non_integral_constant_expression_p
18556 = saved_non_integral_constant_expression_p;
18561 /* If the current declaration has no declarator, return true. */
18564 cp_parser_declares_only_class_p (cp_parser *parser)
18566 /* If the next token is a `;' or a `,' then there is no
18568 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18569 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18572 /* Update the DECL_SPECS to reflect the storage class indicated by
18576 cp_parser_set_storage_class (cp_parser *parser,
18577 cp_decl_specifier_seq *decl_specs,
18579 location_t location)
18581 cp_storage_class storage_class;
18583 if (parser->in_unbraced_linkage_specification_p)
18585 error ("%Hinvalid use of %qD in linkage specification",
18586 &location, ridpointers[keyword]);
18589 else if (decl_specs->storage_class != sc_none)
18591 decl_specs->conflicting_specifiers_p = true;
18595 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18596 && decl_specs->specs[(int) ds_thread])
18598 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18599 decl_specs->specs[(int) ds_thread] = 0;
18605 storage_class = sc_auto;
18608 storage_class = sc_register;
18611 storage_class = sc_static;
18614 storage_class = sc_extern;
18617 storage_class = sc_mutable;
18620 gcc_unreachable ();
18622 decl_specs->storage_class = storage_class;
18624 /* A storage class specifier cannot be applied alongside a typedef
18625 specifier. If there is a typedef specifier present then set
18626 conflicting_specifiers_p which will trigger an error later
18627 on in grokdeclarator. */
18628 if (decl_specs->specs[(int)ds_typedef])
18629 decl_specs->conflicting_specifiers_p = true;
18632 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18633 is true, the type is a user-defined type; otherwise it is a
18634 built-in type specified by a keyword. */
18637 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18639 location_t location,
18640 bool user_defined_p)
18642 decl_specs->any_specifiers_p = true;
18644 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18645 (with, for example, in "typedef int wchar_t;") we remember that
18646 this is what happened. In system headers, we ignore these
18647 declarations so that G++ can work with system headers that are not
18649 if (decl_specs->specs[(int) ds_typedef]
18651 && (type_spec == boolean_type_node
18652 || type_spec == char16_type_node
18653 || type_spec == char32_type_node
18654 || type_spec == wchar_type_node)
18655 && (decl_specs->type
18656 || decl_specs->specs[(int) ds_long]
18657 || decl_specs->specs[(int) ds_short]
18658 || decl_specs->specs[(int) ds_unsigned]
18659 || decl_specs->specs[(int) ds_signed]))
18661 decl_specs->redefined_builtin_type = type_spec;
18662 if (!decl_specs->type)
18664 decl_specs->type = type_spec;
18665 decl_specs->user_defined_type_p = false;
18666 decl_specs->type_location = location;
18669 else if (decl_specs->type)
18670 decl_specs->multiple_types_p = true;
18673 decl_specs->type = type_spec;
18674 decl_specs->user_defined_type_p = user_defined_p;
18675 decl_specs->redefined_builtin_type = NULL_TREE;
18676 decl_specs->type_location = location;
18680 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18681 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18684 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18686 return decl_specifiers->specs[(int) ds_friend] != 0;
18689 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18690 issue an error message indicating that TOKEN_DESC was expected.
18692 Returns the token consumed, if the token had the appropriate type.
18693 Otherwise, returns NULL. */
18696 cp_parser_require (cp_parser* parser,
18697 enum cpp_ttype type,
18698 const char* token_desc)
18700 if (cp_lexer_next_token_is (parser->lexer, type))
18701 return cp_lexer_consume_token (parser->lexer);
18704 /* Output the MESSAGE -- unless we're parsing tentatively. */
18705 if (!cp_parser_simulate_error (parser))
18707 char *message = concat ("expected ", token_desc, NULL);
18708 cp_parser_error (parser, message);
18715 /* An error message is produced if the next token is not '>'.
18716 All further tokens are skipped until the desired token is
18717 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18720 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18722 /* Current level of '< ... >'. */
18723 unsigned level = 0;
18724 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18725 unsigned nesting_depth = 0;
18727 /* Are we ready, yet? If not, issue error message. */
18728 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18731 /* Skip tokens until the desired token is found. */
18734 /* Peek at the next token. */
18735 switch (cp_lexer_peek_token (parser->lexer)->type)
18738 if (!nesting_depth)
18743 if (cxx_dialect == cxx98)
18744 /* C++0x views the `>>' operator as two `>' tokens, but
18747 else if (!nesting_depth && level-- == 0)
18749 /* We've hit a `>>' where the first `>' closes the
18750 template argument list, and the second `>' is
18751 spurious. Just consume the `>>' and stop; we've
18752 already produced at least one error. */
18753 cp_lexer_consume_token (parser->lexer);
18756 /* Fall through for C++0x, so we handle the second `>' in
18760 if (!nesting_depth && level-- == 0)
18762 /* We've reached the token we want, consume it and stop. */
18763 cp_lexer_consume_token (parser->lexer);
18768 case CPP_OPEN_PAREN:
18769 case CPP_OPEN_SQUARE:
18773 case CPP_CLOSE_PAREN:
18774 case CPP_CLOSE_SQUARE:
18775 if (nesting_depth-- == 0)
18780 case CPP_PRAGMA_EOL:
18781 case CPP_SEMICOLON:
18782 case CPP_OPEN_BRACE:
18783 case CPP_CLOSE_BRACE:
18784 /* The '>' was probably forgotten, don't look further. */
18791 /* Consume this token. */
18792 cp_lexer_consume_token (parser->lexer);
18796 /* If the next token is the indicated keyword, consume it. Otherwise,
18797 issue an error message indicating that TOKEN_DESC was expected.
18799 Returns the token consumed, if the token had the appropriate type.
18800 Otherwise, returns NULL. */
18803 cp_parser_require_keyword (cp_parser* parser,
18805 const char* token_desc)
18807 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18809 if (token && token->keyword != keyword)
18811 dyn_string_t error_msg;
18813 /* Format the error message. */
18814 error_msg = dyn_string_new (0);
18815 dyn_string_append_cstr (error_msg, "expected ");
18816 dyn_string_append_cstr (error_msg, token_desc);
18817 cp_parser_error (parser, error_msg->s);
18818 dyn_string_delete (error_msg);
18825 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18826 function-definition. */
18829 cp_parser_token_starts_function_definition_p (cp_token* token)
18831 return (/* An ordinary function-body begins with an `{'. */
18832 token->type == CPP_OPEN_BRACE
18833 /* A ctor-initializer begins with a `:'. */
18834 || token->type == CPP_COLON
18835 /* A function-try-block begins with `try'. */
18836 || token->keyword == RID_TRY
18837 /* The named return value extension begins with `return'. */
18838 || token->keyword == RID_RETURN);
18841 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18845 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18849 token = cp_lexer_peek_token (parser->lexer);
18850 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18853 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18854 C++0x) ending a template-argument. */
18857 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18861 token = cp_lexer_peek_token (parser->lexer);
18862 return (token->type == CPP_COMMA
18863 || token->type == CPP_GREATER
18864 || token->type == CPP_ELLIPSIS
18865 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18868 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18869 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18872 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18877 token = cp_lexer_peek_nth_token (parser->lexer, n);
18878 if (token->type == CPP_LESS)
18880 /* Check for the sequence `<::' in the original code. It would be lexed as
18881 `[:', where `[' is a digraph, and there is no whitespace before
18883 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18886 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18887 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18893 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18894 or none_type otherwise. */
18896 static enum tag_types
18897 cp_parser_token_is_class_key (cp_token* token)
18899 switch (token->keyword)
18904 return record_type;
18913 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18916 cp_parser_check_class_key (enum tag_types class_key, tree type)
18918 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18919 permerror (input_location, "%qs tag used in naming %q#T",
18920 class_key == union_type ? "union"
18921 : class_key == record_type ? "struct" : "class",
18925 /* Issue an error message if DECL is redeclared with different
18926 access than its original declaration [class.access.spec/3].
18927 This applies to nested classes and nested class templates.
18931 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18933 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18936 if ((TREE_PRIVATE (decl)
18937 != (current_access_specifier == access_private_node))
18938 || (TREE_PROTECTED (decl)
18939 != (current_access_specifier == access_protected_node)))
18940 error ("%H%qD redeclared with different access", &location, decl);
18943 /* Look for the `template' keyword, as a syntactic disambiguator.
18944 Return TRUE iff it is present, in which case it will be
18948 cp_parser_optional_template_keyword (cp_parser *parser)
18950 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18952 /* The `template' keyword can only be used within templates;
18953 outside templates the parser can always figure out what is a
18954 template and what is not. */
18955 if (!processing_template_decl)
18957 cp_token *token = cp_lexer_peek_token (parser->lexer);
18958 error ("%H%<template%> (as a disambiguator) is only allowed "
18959 "within templates", &token->location);
18960 /* If this part of the token stream is rescanned, the same
18961 error message would be generated. So, we purge the token
18962 from the stream. */
18963 cp_lexer_purge_token (parser->lexer);
18968 /* Consume the `template' keyword. */
18969 cp_lexer_consume_token (parser->lexer);
18977 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18978 set PARSER->SCOPE, and perform other related actions. */
18981 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18984 struct tree_check *check_value;
18985 deferred_access_check *chk;
18986 VEC (deferred_access_check,gc) *checks;
18988 /* Get the stored value. */
18989 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18990 /* Perform any access checks that were deferred. */
18991 checks = check_value->checks;
18995 VEC_iterate (deferred_access_check, checks, i, chk) ;
18998 perform_or_defer_access_check (chk->binfo,
19003 /* Set the scope from the stored value. */
19004 parser->scope = check_value->value;
19005 parser->qualifying_scope = check_value->qualifying_scope;
19006 parser->object_scope = NULL_TREE;
19009 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19010 encounter the end of a block before what we were looking for. */
19013 cp_parser_cache_group (cp_parser *parser,
19014 enum cpp_ttype end,
19019 cp_token *token = cp_lexer_peek_token (parser->lexer);
19021 /* Abort a parenthesized expression if we encounter a semicolon. */
19022 if ((end == CPP_CLOSE_PAREN || depth == 0)
19023 && token->type == CPP_SEMICOLON)
19025 /* If we've reached the end of the file, stop. */
19026 if (token->type == CPP_EOF
19027 || (end != CPP_PRAGMA_EOL
19028 && token->type == CPP_PRAGMA_EOL))
19030 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19031 /* We've hit the end of an enclosing block, so there's been some
19032 kind of syntax error. */
19035 /* Consume the token. */
19036 cp_lexer_consume_token (parser->lexer);
19037 /* See if it starts a new group. */
19038 if (token->type == CPP_OPEN_BRACE)
19040 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19041 /* In theory this should probably check end == '}', but
19042 cp_parser_save_member_function_body needs it to exit
19043 after either '}' or ')' when called with ')'. */
19047 else if (token->type == CPP_OPEN_PAREN)
19049 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19050 if (depth == 0 && end == CPP_CLOSE_PAREN)
19053 else if (token->type == CPP_PRAGMA)
19054 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19055 else if (token->type == end)
19060 /* Begin parsing tentatively. We always save tokens while parsing
19061 tentatively so that if the tentative parsing fails we can restore the
19065 cp_parser_parse_tentatively (cp_parser* parser)
19067 /* Enter a new parsing context. */
19068 parser->context = cp_parser_context_new (parser->context);
19069 /* Begin saving tokens. */
19070 cp_lexer_save_tokens (parser->lexer);
19071 /* In order to avoid repetitive access control error messages,
19072 access checks are queued up until we are no longer parsing
19074 push_deferring_access_checks (dk_deferred);
19077 /* Commit to the currently active tentative parse. */
19080 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19082 cp_parser_context *context;
19085 /* Mark all of the levels as committed. */
19086 lexer = parser->lexer;
19087 for (context = parser->context; context->next; context = context->next)
19089 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19091 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19092 while (!cp_lexer_saving_tokens (lexer))
19093 lexer = lexer->next;
19094 cp_lexer_commit_tokens (lexer);
19098 /* Abort the currently active tentative parse. All consumed tokens
19099 will be rolled back, and no diagnostics will be issued. */
19102 cp_parser_abort_tentative_parse (cp_parser* parser)
19104 cp_parser_simulate_error (parser);
19105 /* Now, pretend that we want to see if the construct was
19106 successfully parsed. */
19107 cp_parser_parse_definitely (parser);
19110 /* Stop parsing tentatively. If a parse error has occurred, restore the
19111 token stream. Otherwise, commit to the tokens we have consumed.
19112 Returns true if no error occurred; false otherwise. */
19115 cp_parser_parse_definitely (cp_parser* parser)
19117 bool error_occurred;
19118 cp_parser_context *context;
19120 /* Remember whether or not an error occurred, since we are about to
19121 destroy that information. */
19122 error_occurred = cp_parser_error_occurred (parser);
19123 /* Remove the topmost context from the stack. */
19124 context = parser->context;
19125 parser->context = context->next;
19126 /* If no parse errors occurred, commit to the tentative parse. */
19127 if (!error_occurred)
19129 /* Commit to the tokens read tentatively, unless that was
19131 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19132 cp_lexer_commit_tokens (parser->lexer);
19134 pop_to_parent_deferring_access_checks ();
19136 /* Otherwise, if errors occurred, roll back our state so that things
19137 are just as they were before we began the tentative parse. */
19140 cp_lexer_rollback_tokens (parser->lexer);
19141 pop_deferring_access_checks ();
19143 /* Add the context to the front of the free list. */
19144 context->next = cp_parser_context_free_list;
19145 cp_parser_context_free_list = context;
19147 return !error_occurred;
19150 /* Returns true if we are parsing tentatively and are not committed to
19151 this tentative parse. */
19154 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19156 return (cp_parser_parsing_tentatively (parser)
19157 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19160 /* Returns nonzero iff an error has occurred during the most recent
19161 tentative parse. */
19164 cp_parser_error_occurred (cp_parser* parser)
19166 return (cp_parser_parsing_tentatively (parser)
19167 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19170 /* Returns nonzero if GNU extensions are allowed. */
19173 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19175 return parser->allow_gnu_extensions_p;
19178 /* Objective-C++ Productions */
19181 /* Parse an Objective-C expression, which feeds into a primary-expression
19185 objc-message-expression
19186 objc-string-literal
19187 objc-encode-expression
19188 objc-protocol-expression
19189 objc-selector-expression
19191 Returns a tree representation of the expression. */
19194 cp_parser_objc_expression (cp_parser* parser)
19196 /* Try to figure out what kind of declaration is present. */
19197 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19201 case CPP_OPEN_SQUARE:
19202 return cp_parser_objc_message_expression (parser);
19204 case CPP_OBJC_STRING:
19205 kwd = cp_lexer_consume_token (parser->lexer);
19206 return objc_build_string_object (kwd->u.value);
19209 switch (kwd->keyword)
19211 case RID_AT_ENCODE:
19212 return cp_parser_objc_encode_expression (parser);
19214 case RID_AT_PROTOCOL:
19215 return cp_parser_objc_protocol_expression (parser);
19217 case RID_AT_SELECTOR:
19218 return cp_parser_objc_selector_expression (parser);
19224 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19225 &kwd->location, kwd->u.value);
19226 cp_parser_skip_to_end_of_block_or_statement (parser);
19229 return error_mark_node;
19232 /* Parse an Objective-C message expression.
19234 objc-message-expression:
19235 [ objc-message-receiver objc-message-args ]
19237 Returns a representation of an Objective-C message. */
19240 cp_parser_objc_message_expression (cp_parser* parser)
19242 tree receiver, messageargs;
19244 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19245 receiver = cp_parser_objc_message_receiver (parser);
19246 messageargs = cp_parser_objc_message_args (parser);
19247 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19249 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19252 /* Parse an objc-message-receiver.
19254 objc-message-receiver:
19256 simple-type-specifier
19258 Returns a representation of the type or expression. */
19261 cp_parser_objc_message_receiver (cp_parser* parser)
19265 /* An Objective-C message receiver may be either (1) a type
19266 or (2) an expression. */
19267 cp_parser_parse_tentatively (parser);
19268 rcv = cp_parser_expression (parser, false, NULL);
19270 if (cp_parser_parse_definitely (parser))
19273 rcv = cp_parser_simple_type_specifier (parser,
19274 /*decl_specs=*/NULL,
19275 CP_PARSER_FLAGS_NONE);
19277 return objc_get_class_reference (rcv);
19280 /* Parse the arguments and selectors comprising an Objective-C message.
19285 objc-selector-args , objc-comma-args
19287 objc-selector-args:
19288 objc-selector [opt] : assignment-expression
19289 objc-selector-args objc-selector [opt] : assignment-expression
19292 assignment-expression
19293 objc-comma-args , assignment-expression
19295 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19296 selector arguments and TREE_VALUE containing a list of comma
19300 cp_parser_objc_message_args (cp_parser* parser)
19302 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19303 bool maybe_unary_selector_p = true;
19304 cp_token *token = cp_lexer_peek_token (parser->lexer);
19306 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19308 tree selector = NULL_TREE, arg;
19310 if (token->type != CPP_COLON)
19311 selector = cp_parser_objc_selector (parser);
19313 /* Detect if we have a unary selector. */
19314 if (maybe_unary_selector_p
19315 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19316 return build_tree_list (selector, NULL_TREE);
19318 maybe_unary_selector_p = false;
19319 cp_parser_require (parser, CPP_COLON, "%<:%>");
19320 arg = cp_parser_assignment_expression (parser, false, NULL);
19323 = chainon (sel_args,
19324 build_tree_list (selector, arg));
19326 token = cp_lexer_peek_token (parser->lexer);
19329 /* Handle non-selector arguments, if any. */
19330 while (token->type == CPP_COMMA)
19334 cp_lexer_consume_token (parser->lexer);
19335 arg = cp_parser_assignment_expression (parser, false, NULL);
19338 = chainon (addl_args,
19339 build_tree_list (NULL_TREE, arg));
19341 token = cp_lexer_peek_token (parser->lexer);
19344 return build_tree_list (sel_args, addl_args);
19347 /* Parse an Objective-C encode expression.
19349 objc-encode-expression:
19350 @encode objc-typename
19352 Returns an encoded representation of the type argument. */
19355 cp_parser_objc_encode_expression (cp_parser* parser)
19360 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19361 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19362 token = cp_lexer_peek_token (parser->lexer);
19363 type = complete_type (cp_parser_type_id (parser));
19364 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19368 error ("%H%<@encode%> must specify a type as an argument",
19370 return error_mark_node;
19373 return objc_build_encode_expr (type);
19376 /* Parse an Objective-C @defs expression. */
19379 cp_parser_objc_defs_expression (cp_parser *parser)
19383 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19384 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19385 name = cp_parser_identifier (parser);
19386 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19388 return objc_get_class_ivars (name);
19391 /* Parse an Objective-C protocol expression.
19393 objc-protocol-expression:
19394 @protocol ( identifier )
19396 Returns a representation of the protocol expression. */
19399 cp_parser_objc_protocol_expression (cp_parser* parser)
19403 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19404 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19405 proto = cp_parser_identifier (parser);
19406 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19408 return objc_build_protocol_expr (proto);
19411 /* Parse an Objective-C selector expression.
19413 objc-selector-expression:
19414 @selector ( objc-method-signature )
19416 objc-method-signature:
19422 objc-selector-seq objc-selector :
19424 Returns a representation of the method selector. */
19427 cp_parser_objc_selector_expression (cp_parser* parser)
19429 tree sel_seq = NULL_TREE;
19430 bool maybe_unary_selector_p = true;
19433 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19434 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19435 token = cp_lexer_peek_token (parser->lexer);
19437 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19438 || token->type == CPP_SCOPE)
19440 tree selector = NULL_TREE;
19442 if (token->type != CPP_COLON
19443 || token->type == CPP_SCOPE)
19444 selector = cp_parser_objc_selector (parser);
19446 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19447 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19449 /* Detect if we have a unary selector. */
19450 if (maybe_unary_selector_p)
19452 sel_seq = selector;
19453 goto finish_selector;
19457 cp_parser_error (parser, "expected %<:%>");
19460 maybe_unary_selector_p = false;
19461 token = cp_lexer_consume_token (parser->lexer);
19463 if (token->type == CPP_SCOPE)
19466 = chainon (sel_seq,
19467 build_tree_list (selector, NULL_TREE));
19469 = chainon (sel_seq,
19470 build_tree_list (NULL_TREE, NULL_TREE));
19474 = chainon (sel_seq,
19475 build_tree_list (selector, NULL_TREE));
19477 token = cp_lexer_peek_token (parser->lexer);
19481 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19483 return objc_build_selector_expr (sel_seq);
19486 /* Parse a list of identifiers.
19488 objc-identifier-list:
19490 objc-identifier-list , identifier
19492 Returns a TREE_LIST of identifier nodes. */
19495 cp_parser_objc_identifier_list (cp_parser* parser)
19497 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19498 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19500 while (sep->type == CPP_COMMA)
19502 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19503 list = chainon (list,
19504 build_tree_list (NULL_TREE,
19505 cp_parser_identifier (parser)));
19506 sep = cp_lexer_peek_token (parser->lexer);
19512 /* Parse an Objective-C alias declaration.
19514 objc-alias-declaration:
19515 @compatibility_alias identifier identifier ;
19517 This function registers the alias mapping with the Objective-C front end.
19518 It returns nothing. */
19521 cp_parser_objc_alias_declaration (cp_parser* parser)
19525 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19526 alias = cp_parser_identifier (parser);
19527 orig = cp_parser_identifier (parser);
19528 objc_declare_alias (alias, orig);
19529 cp_parser_consume_semicolon_at_end_of_statement (parser);
19532 /* Parse an Objective-C class forward-declaration.
19534 objc-class-declaration:
19535 @class objc-identifier-list ;
19537 The function registers the forward declarations with the Objective-C
19538 front end. It returns nothing. */
19541 cp_parser_objc_class_declaration (cp_parser* parser)
19543 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19544 objc_declare_class (cp_parser_objc_identifier_list (parser));
19545 cp_parser_consume_semicolon_at_end_of_statement (parser);
19548 /* Parse a list of Objective-C protocol references.
19550 objc-protocol-refs-opt:
19551 objc-protocol-refs [opt]
19553 objc-protocol-refs:
19554 < objc-identifier-list >
19556 Returns a TREE_LIST of identifiers, if any. */
19559 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19561 tree protorefs = NULL_TREE;
19563 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19565 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19566 protorefs = cp_parser_objc_identifier_list (parser);
19567 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19573 /* Parse a Objective-C visibility specification. */
19576 cp_parser_objc_visibility_spec (cp_parser* parser)
19578 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19580 switch (vis->keyword)
19582 case RID_AT_PRIVATE:
19583 objc_set_visibility (2);
19585 case RID_AT_PROTECTED:
19586 objc_set_visibility (0);
19588 case RID_AT_PUBLIC:
19589 objc_set_visibility (1);
19595 /* Eat '@private'/'@protected'/'@public'. */
19596 cp_lexer_consume_token (parser->lexer);
19599 /* Parse an Objective-C method type. */
19602 cp_parser_objc_method_type (cp_parser* parser)
19604 objc_set_method_type
19605 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19610 /* Parse an Objective-C protocol qualifier. */
19613 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19615 tree quals = NULL_TREE, node;
19616 cp_token *token = cp_lexer_peek_token (parser->lexer);
19618 node = token->u.value;
19620 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19621 && (node == ridpointers [(int) RID_IN]
19622 || node == ridpointers [(int) RID_OUT]
19623 || node == ridpointers [(int) RID_INOUT]
19624 || node == ridpointers [(int) RID_BYCOPY]
19625 || node == ridpointers [(int) RID_BYREF]
19626 || node == ridpointers [(int) RID_ONEWAY]))
19628 quals = tree_cons (NULL_TREE, node, quals);
19629 cp_lexer_consume_token (parser->lexer);
19630 token = cp_lexer_peek_token (parser->lexer);
19631 node = token->u.value;
19637 /* Parse an Objective-C typename. */
19640 cp_parser_objc_typename (cp_parser* parser)
19642 tree type_name = NULL_TREE;
19644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19646 tree proto_quals, cp_type = NULL_TREE;
19648 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19649 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19651 /* An ObjC type name may consist of just protocol qualifiers, in which
19652 case the type shall default to 'id'. */
19653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19654 cp_type = cp_parser_type_id (parser);
19656 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19657 type_name = build_tree_list (proto_quals, cp_type);
19663 /* Check to see if TYPE refers to an Objective-C selector name. */
19666 cp_parser_objc_selector_p (enum cpp_ttype type)
19668 return (type == CPP_NAME || type == CPP_KEYWORD
19669 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19670 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19671 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19672 || type == CPP_XOR || type == CPP_XOR_EQ);
19675 /* Parse an Objective-C selector. */
19678 cp_parser_objc_selector (cp_parser* parser)
19680 cp_token *token = cp_lexer_consume_token (parser->lexer);
19682 if (!cp_parser_objc_selector_p (token->type))
19684 error ("%Hinvalid Objective-C++ selector name", &token->location);
19685 return error_mark_node;
19688 /* C++ operator names are allowed to appear in ObjC selectors. */
19689 switch (token->type)
19691 case CPP_AND_AND: return get_identifier ("and");
19692 case CPP_AND_EQ: return get_identifier ("and_eq");
19693 case CPP_AND: return get_identifier ("bitand");
19694 case CPP_OR: return get_identifier ("bitor");
19695 case CPP_COMPL: return get_identifier ("compl");
19696 case CPP_NOT: return get_identifier ("not");
19697 case CPP_NOT_EQ: return get_identifier ("not_eq");
19698 case CPP_OR_OR: return get_identifier ("or");
19699 case CPP_OR_EQ: return get_identifier ("or_eq");
19700 case CPP_XOR: return get_identifier ("xor");
19701 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19702 default: return token->u.value;
19706 /* Parse an Objective-C params list. */
19709 cp_parser_objc_method_keyword_params (cp_parser* parser)
19711 tree params = NULL_TREE;
19712 bool maybe_unary_selector_p = true;
19713 cp_token *token = cp_lexer_peek_token (parser->lexer);
19715 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19717 tree selector = NULL_TREE, type_name, identifier;
19719 if (token->type != CPP_COLON)
19720 selector = cp_parser_objc_selector (parser);
19722 /* Detect if we have a unary selector. */
19723 if (maybe_unary_selector_p
19724 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19727 maybe_unary_selector_p = false;
19728 cp_parser_require (parser, CPP_COLON, "%<:%>");
19729 type_name = cp_parser_objc_typename (parser);
19730 identifier = cp_parser_identifier (parser);
19734 objc_build_keyword_decl (selector,
19738 token = cp_lexer_peek_token (parser->lexer);
19744 /* Parse the non-keyword Objective-C params. */
19747 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19749 tree params = make_node (TREE_LIST);
19750 cp_token *token = cp_lexer_peek_token (parser->lexer);
19751 *ellipsisp = false; /* Initially, assume no ellipsis. */
19753 while (token->type == CPP_COMMA)
19755 cp_parameter_declarator *parmdecl;
19758 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19759 token = cp_lexer_peek_token (parser->lexer);
19761 if (token->type == CPP_ELLIPSIS)
19763 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19768 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19769 parm = grokdeclarator (parmdecl->declarator,
19770 &parmdecl->decl_specifiers,
19771 PARM, /*initialized=*/0,
19772 /*attrlist=*/NULL);
19774 chainon (params, build_tree_list (NULL_TREE, parm));
19775 token = cp_lexer_peek_token (parser->lexer);
19781 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19784 cp_parser_objc_interstitial_code (cp_parser* parser)
19786 cp_token *token = cp_lexer_peek_token (parser->lexer);
19788 /* If the next token is `extern' and the following token is a string
19789 literal, then we have a linkage specification. */
19790 if (token->keyword == RID_EXTERN
19791 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19792 cp_parser_linkage_specification (parser);
19793 /* Handle #pragma, if any. */
19794 else if (token->type == CPP_PRAGMA)
19795 cp_parser_pragma (parser, pragma_external);
19796 /* Allow stray semicolons. */
19797 else if (token->type == CPP_SEMICOLON)
19798 cp_lexer_consume_token (parser->lexer);
19799 /* Finally, try to parse a block-declaration, or a function-definition. */
19801 cp_parser_block_declaration (parser, /*statement_p=*/false);
19804 /* Parse a method signature. */
19807 cp_parser_objc_method_signature (cp_parser* parser)
19809 tree rettype, kwdparms, optparms;
19810 bool ellipsis = false;
19812 cp_parser_objc_method_type (parser);
19813 rettype = cp_parser_objc_typename (parser);
19814 kwdparms = cp_parser_objc_method_keyword_params (parser);
19815 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19817 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19820 /* Pars an Objective-C method prototype list. */
19823 cp_parser_objc_method_prototype_list (cp_parser* parser)
19825 cp_token *token = cp_lexer_peek_token (parser->lexer);
19827 while (token->keyword != RID_AT_END)
19829 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19831 objc_add_method_declaration
19832 (cp_parser_objc_method_signature (parser));
19833 cp_parser_consume_semicolon_at_end_of_statement (parser);
19836 /* Allow for interspersed non-ObjC++ code. */
19837 cp_parser_objc_interstitial_code (parser);
19839 token = cp_lexer_peek_token (parser->lexer);
19842 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19843 objc_finish_interface ();
19846 /* Parse an Objective-C method definition list. */
19849 cp_parser_objc_method_definition_list (cp_parser* parser)
19851 cp_token *token = cp_lexer_peek_token (parser->lexer);
19853 while (token->keyword != RID_AT_END)
19857 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19859 push_deferring_access_checks (dk_deferred);
19860 objc_start_method_definition
19861 (cp_parser_objc_method_signature (parser));
19863 /* For historical reasons, we accept an optional semicolon. */
19864 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19865 cp_lexer_consume_token (parser->lexer);
19867 perform_deferred_access_checks ();
19868 stop_deferring_access_checks ();
19869 meth = cp_parser_function_definition_after_declarator (parser,
19871 pop_deferring_access_checks ();
19872 objc_finish_method_definition (meth);
19875 /* Allow for interspersed non-ObjC++ code. */
19876 cp_parser_objc_interstitial_code (parser);
19878 token = cp_lexer_peek_token (parser->lexer);
19881 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19882 objc_finish_implementation ();
19885 /* Parse Objective-C ivars. */
19888 cp_parser_objc_class_ivars (cp_parser* parser)
19890 cp_token *token = cp_lexer_peek_token (parser->lexer);
19892 if (token->type != CPP_OPEN_BRACE)
19893 return; /* No ivars specified. */
19895 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19896 token = cp_lexer_peek_token (parser->lexer);
19898 while (token->type != CPP_CLOSE_BRACE)
19900 cp_decl_specifier_seq declspecs;
19901 int decl_class_or_enum_p;
19902 tree prefix_attributes;
19904 cp_parser_objc_visibility_spec (parser);
19906 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19909 cp_parser_decl_specifier_seq (parser,
19910 CP_PARSER_FLAGS_OPTIONAL,
19912 &decl_class_or_enum_p);
19913 prefix_attributes = declspecs.attributes;
19914 declspecs.attributes = NULL_TREE;
19916 /* Keep going until we hit the `;' at the end of the
19918 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19920 tree width = NULL_TREE, attributes, first_attribute, decl;
19921 cp_declarator *declarator = NULL;
19922 int ctor_dtor_or_conv_p;
19924 /* Check for a (possibly unnamed) bitfield declaration. */
19925 token = cp_lexer_peek_token (parser->lexer);
19926 if (token->type == CPP_COLON)
19929 if (token->type == CPP_NAME
19930 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19933 /* Get the name of the bitfield. */
19934 declarator = make_id_declarator (NULL_TREE,
19935 cp_parser_identifier (parser),
19939 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19940 /* Get the width of the bitfield. */
19942 = cp_parser_constant_expression (parser,
19943 /*allow_non_constant=*/false,
19948 /* Parse the declarator. */
19950 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19951 &ctor_dtor_or_conv_p,
19952 /*parenthesized_p=*/NULL,
19953 /*member_p=*/false);
19956 /* Look for attributes that apply to the ivar. */
19957 attributes = cp_parser_attributes_opt (parser);
19958 /* Remember which attributes are prefix attributes and
19960 first_attribute = attributes;
19961 /* Combine the attributes. */
19962 attributes = chainon (prefix_attributes, attributes);
19965 /* Create the bitfield declaration. */
19966 decl = grokbitfield (declarator, &declspecs,
19970 decl = grokfield (declarator, &declspecs,
19971 NULL_TREE, /*init_const_expr_p=*/false,
19972 NULL_TREE, attributes);
19974 /* Add the instance variable. */
19975 objc_add_instance_variable (decl);
19977 /* Reset PREFIX_ATTRIBUTES. */
19978 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19979 attributes = TREE_CHAIN (attributes);
19981 TREE_CHAIN (attributes) = NULL_TREE;
19983 token = cp_lexer_peek_token (parser->lexer);
19985 if (token->type == CPP_COMMA)
19987 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19993 cp_parser_consume_semicolon_at_end_of_statement (parser);
19994 token = cp_lexer_peek_token (parser->lexer);
19997 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19998 /* For historical reasons, we accept an optional semicolon. */
19999 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20000 cp_lexer_consume_token (parser->lexer);
20003 /* Parse an Objective-C protocol declaration. */
20006 cp_parser_objc_protocol_declaration (cp_parser* parser)
20008 tree proto, protorefs;
20011 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20014 tok = cp_lexer_peek_token (parser->lexer);
20015 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
20019 /* See if we have a forward declaration or a definition. */
20020 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20022 /* Try a forward declaration first. */
20023 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20025 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20027 cp_parser_consume_semicolon_at_end_of_statement (parser);
20030 /* Ok, we got a full-fledged definition (or at least should). */
20033 proto = cp_parser_identifier (parser);
20034 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20035 objc_start_protocol (proto, protorefs);
20036 cp_parser_objc_method_prototype_list (parser);
20040 /* Parse an Objective-C superclass or category. */
20043 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20046 cp_token *next = cp_lexer_peek_token (parser->lexer);
20048 *super = *categ = NULL_TREE;
20049 if (next->type == CPP_COLON)
20051 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20052 *super = cp_parser_identifier (parser);
20054 else if (next->type == CPP_OPEN_PAREN)
20056 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20057 *categ = cp_parser_identifier (parser);
20058 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20062 /* Parse an Objective-C class interface. */
20065 cp_parser_objc_class_interface (cp_parser* parser)
20067 tree name, super, categ, protos;
20069 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20070 name = cp_parser_identifier (parser);
20071 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20072 protos = cp_parser_objc_protocol_refs_opt (parser);
20074 /* We have either a class or a category on our hands. */
20076 objc_start_category_interface (name, categ, protos);
20079 objc_start_class_interface (name, super, protos);
20080 /* Handle instance variable declarations, if any. */
20081 cp_parser_objc_class_ivars (parser);
20082 objc_continue_interface ();
20085 cp_parser_objc_method_prototype_list (parser);
20088 /* Parse an Objective-C class implementation. */
20091 cp_parser_objc_class_implementation (cp_parser* parser)
20093 tree name, super, categ;
20095 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20096 name = cp_parser_identifier (parser);
20097 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20099 /* We have either a class or a category on our hands. */
20101 objc_start_category_implementation (name, categ);
20104 objc_start_class_implementation (name, super);
20105 /* Handle instance variable declarations, if any. */
20106 cp_parser_objc_class_ivars (parser);
20107 objc_continue_implementation ();
20110 cp_parser_objc_method_definition_list (parser);
20113 /* Consume the @end token and finish off the implementation. */
20116 cp_parser_objc_end_implementation (cp_parser* parser)
20118 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20119 objc_finish_implementation ();
20122 /* Parse an Objective-C declaration. */
20125 cp_parser_objc_declaration (cp_parser* parser)
20127 /* Try to figure out what kind of declaration is present. */
20128 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20130 switch (kwd->keyword)
20133 cp_parser_objc_alias_declaration (parser);
20136 cp_parser_objc_class_declaration (parser);
20138 case RID_AT_PROTOCOL:
20139 cp_parser_objc_protocol_declaration (parser);
20141 case RID_AT_INTERFACE:
20142 cp_parser_objc_class_interface (parser);
20144 case RID_AT_IMPLEMENTATION:
20145 cp_parser_objc_class_implementation (parser);
20148 cp_parser_objc_end_implementation (parser);
20151 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20152 &kwd->location, kwd->u.value);
20153 cp_parser_skip_to_end_of_block_or_statement (parser);
20157 /* Parse an Objective-C try-catch-finally statement.
20159 objc-try-catch-finally-stmt:
20160 @try compound-statement objc-catch-clause-seq [opt]
20161 objc-finally-clause [opt]
20163 objc-catch-clause-seq:
20164 objc-catch-clause objc-catch-clause-seq [opt]
20167 @catch ( exception-declaration ) compound-statement
20169 objc-finally-clause
20170 @finally compound-statement
20172 Returns NULL_TREE. */
20175 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20176 location_t location;
20179 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20180 location = cp_lexer_peek_token (parser->lexer)->location;
20181 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20182 node, lest it get absorbed into the surrounding block. */
20183 stmt = push_stmt_list ();
20184 cp_parser_compound_statement (parser, NULL, false);
20185 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20187 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20189 cp_parameter_declarator *parmdecl;
20192 cp_lexer_consume_token (parser->lexer);
20193 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20194 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20195 parm = grokdeclarator (parmdecl->declarator,
20196 &parmdecl->decl_specifiers,
20197 PARM, /*initialized=*/0,
20198 /*attrlist=*/NULL);
20199 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20200 objc_begin_catch_clause (parm);
20201 cp_parser_compound_statement (parser, NULL, false);
20202 objc_finish_catch_clause ();
20205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20207 cp_lexer_consume_token (parser->lexer);
20208 location = cp_lexer_peek_token (parser->lexer)->location;
20209 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20210 node, lest it get absorbed into the surrounding block. */
20211 stmt = push_stmt_list ();
20212 cp_parser_compound_statement (parser, NULL, false);
20213 objc_build_finally_clause (location, pop_stmt_list (stmt));
20216 return objc_finish_try_stmt ();
20219 /* Parse an Objective-C synchronized statement.
20221 objc-synchronized-stmt:
20222 @synchronized ( expression ) compound-statement
20224 Returns NULL_TREE. */
20227 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20228 location_t location;
20231 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20233 location = cp_lexer_peek_token (parser->lexer)->location;
20234 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20235 lock = cp_parser_expression (parser, false, NULL);
20236 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20238 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20239 node, lest it get absorbed into the surrounding block. */
20240 stmt = push_stmt_list ();
20241 cp_parser_compound_statement (parser, NULL, false);
20243 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20246 /* Parse an Objective-C throw statement.
20249 @throw assignment-expression [opt] ;
20251 Returns a constructed '@throw' statement. */
20254 cp_parser_objc_throw_statement (cp_parser *parser) {
20255 tree expr = NULL_TREE;
20257 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20260 expr = cp_parser_assignment_expression (parser, false, NULL);
20262 cp_parser_consume_semicolon_at_end_of_statement (parser);
20264 return objc_build_throw_stmt (expr);
20267 /* Parse an Objective-C statement. */
20270 cp_parser_objc_statement (cp_parser * parser) {
20271 /* Try to figure out what kind of declaration is present. */
20272 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20274 switch (kwd->keyword)
20277 return cp_parser_objc_try_catch_finally_statement (parser);
20278 case RID_AT_SYNCHRONIZED:
20279 return cp_parser_objc_synchronized_statement (parser);
20281 return cp_parser_objc_throw_statement (parser);
20283 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20284 &kwd->location, kwd->u.value);
20285 cp_parser_skip_to_end_of_block_or_statement (parser);
20288 return error_mark_node;
20291 /* OpenMP 2.5 parsing routines. */
20293 /* Returns name of the next clause.
20294 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20295 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20296 returned and the token is consumed. */
20298 static pragma_omp_clause
20299 cp_parser_omp_clause_name (cp_parser *parser)
20301 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20304 result = PRAGMA_OMP_CLAUSE_IF;
20305 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20306 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20307 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20308 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20309 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20311 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20312 const char *p = IDENTIFIER_POINTER (id);
20317 if (!strcmp ("collapse", p))
20318 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20319 else if (!strcmp ("copyin", p))
20320 result = PRAGMA_OMP_CLAUSE_COPYIN;
20321 else if (!strcmp ("copyprivate", p))
20322 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20325 if (!strcmp ("firstprivate", p))
20326 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20329 if (!strcmp ("lastprivate", p))
20330 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20333 if (!strcmp ("nowait", p))
20334 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20335 else if (!strcmp ("num_threads", p))
20336 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20339 if (!strcmp ("ordered", p))
20340 result = PRAGMA_OMP_CLAUSE_ORDERED;
20343 if (!strcmp ("reduction", p))
20344 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20347 if (!strcmp ("schedule", p))
20348 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20349 else if (!strcmp ("shared", p))
20350 result = PRAGMA_OMP_CLAUSE_SHARED;
20353 if (!strcmp ("untied", p))
20354 result = PRAGMA_OMP_CLAUSE_UNTIED;
20359 if (result != PRAGMA_OMP_CLAUSE_NONE)
20360 cp_lexer_consume_token (parser->lexer);
20365 /* Validate that a clause of the given type does not already exist. */
20368 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20369 const char *name, location_t location)
20373 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20374 if (OMP_CLAUSE_CODE (c) == code)
20376 error ("%Htoo many %qs clauses", &location, name);
20384 variable-list , identifier
20386 In addition, we match a closing parenthesis. An opening parenthesis
20387 will have been consumed by the caller.
20389 If KIND is nonzero, create the appropriate node and install the decl
20390 in OMP_CLAUSE_DECL and add the node to the head of the list.
20392 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20393 return the list created. */
20396 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20404 token = cp_lexer_peek_token (parser->lexer);
20405 name = cp_parser_id_expression (parser, /*template_p=*/false,
20406 /*check_dependency_p=*/true,
20407 /*template_p=*/NULL,
20408 /*declarator_p=*/false,
20409 /*optional_p=*/false);
20410 if (name == error_mark_node)
20413 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20414 if (decl == error_mark_node)
20415 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20416 else if (kind != 0)
20418 tree u = build_omp_clause (kind);
20419 OMP_CLAUSE_DECL (u) = decl;
20420 OMP_CLAUSE_CHAIN (u) = list;
20424 list = tree_cons (decl, NULL_TREE, list);
20427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20429 cp_lexer_consume_token (parser->lexer);
20432 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20436 /* Try to resync to an unnested comma. Copied from
20437 cp_parser_parenthesized_expression_list. */
20439 ending = cp_parser_skip_to_closing_parenthesis (parser,
20440 /*recovering=*/true,
20442 /*consume_paren=*/true);
20450 /* Similarly, but expect leading and trailing parenthesis. This is a very
20451 common case for omp clauses. */
20454 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20456 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20457 return cp_parser_omp_var_list_no_open (parser, kind, list);
20462 collapse ( constant-expression ) */
20465 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20471 loc = cp_lexer_peek_token (parser->lexer)->location;
20472 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20475 num = cp_parser_constant_expression (parser, false, NULL);
20477 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20478 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20479 /*or_comma=*/false,
20480 /*consume_paren=*/true);
20482 if (num == error_mark_node)
20484 num = fold_non_dependent_expr (num);
20485 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20486 || !host_integerp (num, 0)
20487 || (n = tree_low_cst (num, 0)) <= 0
20490 error ("%Hcollapse argument needs positive constant integer expression",
20495 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20496 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20497 OMP_CLAUSE_CHAIN (c) = list;
20498 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20504 default ( shared | none ) */
20507 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20509 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20512 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20514 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20516 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20517 const char *p = IDENTIFIER_POINTER (id);
20522 if (strcmp ("none", p) != 0)
20524 kind = OMP_CLAUSE_DEFAULT_NONE;
20528 if (strcmp ("shared", p) != 0)
20530 kind = OMP_CLAUSE_DEFAULT_SHARED;
20537 cp_lexer_consume_token (parser->lexer);
20542 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20545 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20546 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20547 /*or_comma=*/false,
20548 /*consume_paren=*/true);
20550 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20553 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20554 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20555 OMP_CLAUSE_CHAIN (c) = list;
20556 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20562 if ( expression ) */
20565 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20569 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20572 t = cp_parser_condition (parser);
20574 if (t == error_mark_node
20575 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20577 /*or_comma=*/false,
20578 /*consume_paren=*/true);
20580 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20582 c = build_omp_clause (OMP_CLAUSE_IF);
20583 OMP_CLAUSE_IF_EXPR (c) = t;
20584 OMP_CLAUSE_CHAIN (c) = list;
20593 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20594 tree list, location_t location)
20598 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20600 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20601 OMP_CLAUSE_CHAIN (c) = list;
20606 num_threads ( expression ) */
20609 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20610 location_t location)
20614 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20617 t = cp_parser_expression (parser, false, NULL);
20619 if (t == error_mark_node
20620 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20621 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20622 /*or_comma=*/false,
20623 /*consume_paren=*/true);
20625 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20626 "num_threads", location);
20628 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20629 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20630 OMP_CLAUSE_CHAIN (c) = list;
20639 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20640 tree list, location_t location)
20644 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20645 "ordered", location);
20647 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20648 OMP_CLAUSE_CHAIN (c) = list;
20653 reduction ( reduction-operator : variable-list )
20655 reduction-operator:
20656 One of: + * - & ^ | && || */
20659 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20661 enum tree_code code;
20664 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20667 switch (cp_lexer_peek_token (parser->lexer)->type)
20679 code = BIT_AND_EXPR;
20682 code = BIT_XOR_EXPR;
20685 code = BIT_IOR_EXPR;
20688 code = TRUTH_ANDIF_EXPR;
20691 code = TRUTH_ORIF_EXPR;
20694 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20695 "%<|%>, %<&&%>, or %<||%>");
20697 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20698 /*or_comma=*/false,
20699 /*consume_paren=*/true);
20702 cp_lexer_consume_token (parser->lexer);
20704 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20707 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20708 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20709 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20715 schedule ( schedule-kind )
20716 schedule ( schedule-kind , expression )
20719 static | dynamic | guided | runtime | auto */
20722 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20726 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20729 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20731 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20733 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20734 const char *p = IDENTIFIER_POINTER (id);
20739 if (strcmp ("dynamic", p) != 0)
20741 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20745 if (strcmp ("guided", p) != 0)
20747 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20751 if (strcmp ("runtime", p) != 0)
20753 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20760 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20761 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20762 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20763 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20766 cp_lexer_consume_token (parser->lexer);
20768 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20771 cp_lexer_consume_token (parser->lexer);
20773 token = cp_lexer_peek_token (parser->lexer);
20774 t = cp_parser_assignment_expression (parser, false, NULL);
20776 if (t == error_mark_node)
20778 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20779 error ("%Hschedule %<runtime%> does not take "
20780 "a %<chunk_size%> parameter", &token->location);
20781 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20782 error ("%Hschedule %<auto%> does not take "
20783 "a %<chunk_size%> parameter", &token->location);
20785 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20787 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20790 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20793 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20794 OMP_CLAUSE_CHAIN (c) = list;
20798 cp_parser_error (parser, "invalid schedule kind");
20800 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20801 /*or_comma=*/false,
20802 /*consume_paren=*/true);
20810 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20811 tree list, location_t location)
20815 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20817 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20818 OMP_CLAUSE_CHAIN (c) = list;
20822 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20823 is a bitmask in MASK. Return the list of clauses found; the result
20824 of clause default goes in *pdefault. */
20827 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20828 const char *where, cp_token *pragma_tok)
20830 tree clauses = NULL;
20832 cp_token *token = NULL;
20834 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20836 pragma_omp_clause c_kind;
20837 const char *c_name;
20838 tree prev = clauses;
20840 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20841 cp_lexer_consume_token (parser->lexer);
20843 token = cp_lexer_peek_token (parser->lexer);
20844 c_kind = cp_parser_omp_clause_name (parser);
20849 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20850 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20852 c_name = "collapse";
20854 case PRAGMA_OMP_CLAUSE_COPYIN:
20855 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20858 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20859 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20861 c_name = "copyprivate";
20863 case PRAGMA_OMP_CLAUSE_DEFAULT:
20864 clauses = cp_parser_omp_clause_default (parser, clauses,
20866 c_name = "default";
20868 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20869 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20871 c_name = "firstprivate";
20873 case PRAGMA_OMP_CLAUSE_IF:
20874 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20877 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20878 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20880 c_name = "lastprivate";
20882 case PRAGMA_OMP_CLAUSE_NOWAIT:
20883 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20886 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20887 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20889 c_name = "num_threads";
20891 case PRAGMA_OMP_CLAUSE_ORDERED:
20892 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20894 c_name = "ordered";
20896 case PRAGMA_OMP_CLAUSE_PRIVATE:
20897 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20899 c_name = "private";
20901 case PRAGMA_OMP_CLAUSE_REDUCTION:
20902 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20903 c_name = "reduction";
20905 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20906 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20908 c_name = "schedule";
20910 case PRAGMA_OMP_CLAUSE_SHARED:
20911 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20915 case PRAGMA_OMP_CLAUSE_UNTIED:
20916 clauses = cp_parser_omp_clause_untied (parser, clauses,
20921 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20925 if (((mask >> c_kind) & 1) == 0)
20927 /* Remove the invalid clause(s) from the list to avoid
20928 confusing the rest of the compiler. */
20930 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20934 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20935 return finish_omp_clauses (clauses);
20942 In practice, we're also interested in adding the statement to an
20943 outer node. So it is convenient if we work around the fact that
20944 cp_parser_statement calls add_stmt. */
20947 cp_parser_begin_omp_structured_block (cp_parser *parser)
20949 unsigned save = parser->in_statement;
20951 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20952 This preserves the "not within loop or switch" style error messages
20953 for nonsense cases like
20959 if (parser->in_statement)
20960 parser->in_statement = IN_OMP_BLOCK;
20966 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20968 parser->in_statement = save;
20972 cp_parser_omp_structured_block (cp_parser *parser)
20974 tree stmt = begin_omp_structured_block ();
20975 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20977 cp_parser_statement (parser, NULL_TREE, false, NULL);
20979 cp_parser_end_omp_structured_block (parser, save);
20980 return finish_omp_structured_block (stmt);
20984 # pragma omp atomic new-line
20988 x binop= expr | x++ | ++x | x-- | --x
20990 +, *, -, /, &, ^, |, <<, >>
20992 where x is an lvalue expression with scalar type. */
20995 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20998 enum tree_code code;
21000 cp_parser_require_pragma_eol (parser, pragma_tok);
21002 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21003 /*cast_p=*/false, NULL);
21004 switch (TREE_CODE (lhs))
21009 case PREINCREMENT_EXPR:
21010 case POSTINCREMENT_EXPR:
21011 lhs = TREE_OPERAND (lhs, 0);
21013 rhs = integer_one_node;
21016 case PREDECREMENT_EXPR:
21017 case POSTDECREMENT_EXPR:
21018 lhs = TREE_OPERAND (lhs, 0);
21020 rhs = integer_one_node;
21023 case COMPOUND_EXPR:
21024 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
21025 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
21026 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
21027 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
21028 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
21029 (TREE_OPERAND (lhs, 1), 0), 0)))
21031 /* Undo effects of boolean_increment for post {in,de}crement. */
21032 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
21035 if (TREE_CODE (lhs) == MODIFY_EXPR
21036 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
21038 /* Undo effects of boolean_increment. */
21039 if (integer_onep (TREE_OPERAND (lhs, 1)))
21041 /* This is pre or post increment. */
21042 rhs = TREE_OPERAND (lhs, 1);
21043 lhs = TREE_OPERAND (lhs, 0);
21050 switch (cp_lexer_peek_token (parser->lexer)->type)
21056 code = TRUNC_DIV_EXPR;
21064 case CPP_LSHIFT_EQ:
21065 code = LSHIFT_EXPR;
21067 case CPP_RSHIFT_EQ:
21068 code = RSHIFT_EXPR;
21071 code = BIT_AND_EXPR;
21074 code = BIT_IOR_EXPR;
21077 code = BIT_XOR_EXPR;
21080 cp_parser_error (parser,
21081 "invalid operator for %<#pragma omp atomic%>");
21084 cp_lexer_consume_token (parser->lexer);
21086 rhs = cp_parser_expression (parser, false, NULL);
21087 if (rhs == error_mark_node)
21091 finish_omp_atomic (code, lhs, rhs);
21092 cp_parser_consume_semicolon_at_end_of_statement (parser);
21096 cp_parser_skip_to_end_of_block_or_statement (parser);
21101 # pragma omp barrier new-line */
21104 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21106 cp_parser_require_pragma_eol (parser, pragma_tok);
21107 finish_omp_barrier ();
21111 # pragma omp critical [(name)] new-line
21112 structured-block */
21115 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21117 tree stmt, name = NULL;
21119 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21121 cp_lexer_consume_token (parser->lexer);
21123 name = cp_parser_identifier (parser);
21125 if (name == error_mark_node
21126 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21127 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21128 /*or_comma=*/false,
21129 /*consume_paren=*/true);
21130 if (name == error_mark_node)
21133 cp_parser_require_pragma_eol (parser, pragma_tok);
21135 stmt = cp_parser_omp_structured_block (parser);
21136 return c_finish_omp_critical (stmt, name);
21140 # pragma omp flush flush-vars[opt] new-line
21143 ( variable-list ) */
21146 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21149 (void) cp_parser_omp_var_list (parser, 0, NULL);
21150 cp_parser_require_pragma_eol (parser, pragma_tok);
21152 finish_omp_flush ();
21155 /* Helper function, to parse omp for increment expression. */
21158 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21160 tree cond = cp_parser_binary_expression (parser, false, true,
21161 PREC_NOT_OPERATOR, NULL);
21164 if (cond == error_mark_node
21165 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21167 cp_parser_skip_to_end_of_statement (parser);
21168 return error_mark_node;
21171 switch (TREE_CODE (cond))
21179 return error_mark_node;
21182 /* If decl is an iterator, preserve LHS and RHS of the relational
21183 expr until finish_omp_for. */
21185 && (type_dependent_expression_p (decl)
21186 || CLASS_TYPE_P (TREE_TYPE (decl))))
21189 return build_x_binary_op (TREE_CODE (cond),
21190 TREE_OPERAND (cond, 0), ERROR_MARK,
21191 TREE_OPERAND (cond, 1), ERROR_MARK,
21192 &overloaded_p, tf_warning_or_error);
21195 /* Helper function, to parse omp for increment expression. */
21198 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21200 cp_token *token = cp_lexer_peek_token (parser->lexer);
21206 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21208 op = (token->type == CPP_PLUS_PLUS
21209 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21210 cp_lexer_consume_token (parser->lexer);
21211 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21213 return error_mark_node;
21214 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21217 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21219 return error_mark_node;
21221 token = cp_lexer_peek_token (parser->lexer);
21222 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21224 op = (token->type == CPP_PLUS_PLUS
21225 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21226 cp_lexer_consume_token (parser->lexer);
21227 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21230 op = cp_parser_assignment_operator_opt (parser);
21231 if (op == ERROR_MARK)
21232 return error_mark_node;
21234 if (op != NOP_EXPR)
21236 rhs = cp_parser_assignment_expression (parser, false, NULL);
21237 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21238 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21241 lhs = cp_parser_binary_expression (parser, false, false,
21242 PREC_ADDITIVE_EXPRESSION, NULL);
21243 token = cp_lexer_peek_token (parser->lexer);
21244 decl_first = lhs == decl;
21247 if (token->type != CPP_PLUS
21248 && token->type != CPP_MINUS)
21249 return error_mark_node;
21253 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21254 cp_lexer_consume_token (parser->lexer);
21255 rhs = cp_parser_binary_expression (parser, false, false,
21256 PREC_ADDITIVE_EXPRESSION, NULL);
21257 token = cp_lexer_peek_token (parser->lexer);
21258 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21260 if (lhs == NULL_TREE)
21262 if (op == PLUS_EXPR)
21265 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21268 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21269 NULL, tf_warning_or_error);
21272 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21276 if (rhs != decl || op == MINUS_EXPR)
21277 return error_mark_node;
21278 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21281 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21283 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21286 /* Parse the restricted form of the for statement allowed by OpenMP. */
21289 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21291 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21292 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21293 tree this_pre_body, cl;
21294 location_t loc_first;
21295 bool collapse_err = false;
21296 int i, collapse = 1, nbraces = 0;
21298 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21299 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21300 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21302 gcc_assert (collapse >= 1);
21304 declv = make_tree_vec (collapse);
21305 initv = make_tree_vec (collapse);
21306 condv = make_tree_vec (collapse);
21307 incrv = make_tree_vec (collapse);
21309 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21311 for (i = 0; i < collapse; i++)
21313 int bracecount = 0;
21314 bool add_private_clause = false;
21317 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21319 cp_parser_error (parser, "for statement expected");
21322 loc = cp_lexer_consume_token (parser->lexer)->location;
21324 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21327 init = decl = real_decl = NULL;
21328 this_pre_body = push_stmt_list ();
21329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21331 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21335 integer-type var = lb
21336 random-access-iterator-type var = lb
21337 pointer-type var = lb
21339 cp_decl_specifier_seq type_specifiers;
21341 /* First, try to parse as an initialized declaration. See
21342 cp_parser_condition, from whence the bulk of this is copied. */
21344 cp_parser_parse_tentatively (parser);
21345 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21346 /*is_trailing_return=*/false,
21348 if (cp_parser_parse_definitely (parser))
21350 /* If parsing a type specifier seq succeeded, then this
21351 MUST be a initialized declaration. */
21352 tree asm_specification, attributes;
21353 cp_declarator *declarator;
21355 declarator = cp_parser_declarator (parser,
21356 CP_PARSER_DECLARATOR_NAMED,
21357 /*ctor_dtor_or_conv_p=*/NULL,
21358 /*parenthesized_p=*/NULL,
21359 /*member_p=*/false);
21360 attributes = cp_parser_attributes_opt (parser);
21361 asm_specification = cp_parser_asm_specification_opt (parser);
21363 if (declarator == cp_error_declarator)
21364 cp_parser_skip_to_end_of_statement (parser);
21368 tree pushed_scope, auto_node;
21370 decl = start_decl (declarator, &type_specifiers,
21371 SD_INITIALIZED, attributes,
21372 /*prefix_attributes=*/NULL_TREE,
21375 auto_node = type_uses_auto (TREE_TYPE (decl));
21376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21378 if (cp_lexer_next_token_is (parser->lexer,
21380 error ("parenthesized initialization is not allowed in "
21381 "OpenMP %<for%> loop");
21383 /* Trigger an error. */
21384 cp_parser_require (parser, CPP_EQ, "%<=%>");
21386 init = error_mark_node;
21387 cp_parser_skip_to_end_of_statement (parser);
21389 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21390 || type_dependent_expression_p (decl)
21393 bool is_direct_init, is_non_constant_init;
21395 init = cp_parser_initializer (parser,
21397 &is_non_constant_init);
21399 if (auto_node && describable_type (init))
21402 = do_auto_deduction (TREE_TYPE (decl), init,
21405 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21406 && !type_dependent_expression_p (decl))
21410 cp_finish_decl (decl, init, !is_non_constant_init,
21412 LOOKUP_ONLYCONVERTING);
21413 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21416 = tree_cons (NULL, this_pre_body, for_block);
21420 init = pop_stmt_list (this_pre_body);
21421 this_pre_body = NULL_TREE;
21426 cp_lexer_consume_token (parser->lexer);
21427 init = cp_parser_assignment_expression (parser, false, NULL);
21430 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21431 init = error_mark_node;
21433 cp_finish_decl (decl, NULL_TREE,
21434 /*init_const_expr_p=*/false,
21436 LOOKUP_ONLYCONVERTING);
21440 pop_scope (pushed_scope);
21446 /* If parsing a type specifier sequence failed, then
21447 this MUST be a simple expression. */
21448 cp_parser_parse_tentatively (parser);
21449 decl = cp_parser_primary_expression (parser, false, false,
21451 if (!cp_parser_error_occurred (parser)
21454 && CLASS_TYPE_P (TREE_TYPE (decl)))
21458 cp_parser_parse_definitely (parser);
21459 cp_parser_require (parser, CPP_EQ, "%<=%>");
21460 rhs = cp_parser_assignment_expression (parser, false, NULL);
21461 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21463 tf_warning_or_error));
21464 add_private_clause = true;
21469 cp_parser_abort_tentative_parse (parser);
21470 init = cp_parser_expression (parser, false, NULL);
21473 if (TREE_CODE (init) == MODIFY_EXPR
21474 || TREE_CODE (init) == MODOP_EXPR)
21475 real_decl = TREE_OPERAND (init, 0);
21480 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21483 this_pre_body = pop_stmt_list (this_pre_body);
21487 pre_body = push_stmt_list ();
21489 add_stmt (this_pre_body);
21490 pre_body = pop_stmt_list (pre_body);
21493 pre_body = this_pre_body;
21498 if (par_clauses != NULL && real_decl != NULL_TREE)
21501 for (c = par_clauses; *c ; )
21502 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21503 && OMP_CLAUSE_DECL (*c) == real_decl)
21505 error ("%Hiteration variable %qD should not be firstprivate",
21507 *c = OMP_CLAUSE_CHAIN (*c);
21509 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21510 && OMP_CLAUSE_DECL (*c) == real_decl)
21512 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21513 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21514 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21515 OMP_CLAUSE_DECL (l) = real_decl;
21516 OMP_CLAUSE_CHAIN (l) = clauses;
21517 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21519 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21520 CP_OMP_CLAUSE_INFO (*c) = NULL;
21521 add_private_clause = false;
21525 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21526 && OMP_CLAUSE_DECL (*c) == real_decl)
21527 add_private_clause = false;
21528 c = &OMP_CLAUSE_CHAIN (*c);
21532 if (add_private_clause)
21535 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21537 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21538 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21539 && OMP_CLAUSE_DECL (c) == decl)
21541 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21542 && OMP_CLAUSE_DECL (c) == decl)
21543 error ("%Hiteration variable %qD should not be firstprivate",
21545 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21546 && OMP_CLAUSE_DECL (c) == decl)
21547 error ("%Hiteration variable %qD should not be reduction",
21552 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21553 OMP_CLAUSE_DECL (c) = decl;
21554 c = finish_omp_clauses (c);
21557 OMP_CLAUSE_CHAIN (c) = clauses;
21564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21565 cond = cp_parser_omp_for_cond (parser, decl);
21566 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21571 /* If decl is an iterator, preserve the operator on decl
21572 until finish_omp_for. */
21574 && (type_dependent_expression_p (decl)
21575 || CLASS_TYPE_P (TREE_TYPE (decl))))
21576 incr = cp_parser_omp_for_incr (parser, decl);
21578 incr = cp_parser_expression (parser, false, NULL);
21581 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21582 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21583 /*or_comma=*/false,
21584 /*consume_paren=*/true);
21586 TREE_VEC_ELT (declv, i) = decl;
21587 TREE_VEC_ELT (initv, i) = init;
21588 TREE_VEC_ELT (condv, i) = cond;
21589 TREE_VEC_ELT (incrv, i) = incr;
21591 if (i == collapse - 1)
21594 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21595 in between the collapsed for loops to be still considered perfectly
21596 nested. Hopefully the final version clarifies this.
21597 For now handle (multiple) {'s and empty statements. */
21598 cp_parser_parse_tentatively (parser);
21601 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21603 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21605 cp_lexer_consume_token (parser->lexer);
21608 else if (bracecount
21609 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21610 cp_lexer_consume_token (parser->lexer);
21613 loc = cp_lexer_peek_token (parser->lexer)->location;
21614 error ("%Hnot enough collapsed for loops", &loc);
21615 collapse_err = true;
21616 cp_parser_abort_tentative_parse (parser);
21625 cp_parser_parse_definitely (parser);
21626 nbraces += bracecount;
21630 /* Note that we saved the original contents of this flag when we entered
21631 the structured block, and so we don't need to re-save it here. */
21632 parser->in_statement = IN_OMP_FOR;
21634 /* Note that the grammar doesn't call for a structured block here,
21635 though the loop as a whole is a structured block. */
21636 body = push_stmt_list ();
21637 cp_parser_statement (parser, NULL_TREE, false, NULL);
21638 body = pop_stmt_list (body);
21640 if (declv == NULL_TREE)
21643 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21644 pre_body, clauses);
21648 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21650 cp_lexer_consume_token (parser->lexer);
21653 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21654 cp_lexer_consume_token (parser->lexer);
21659 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21660 error ("%Hcollapsed loops not perfectly nested", &loc);
21662 collapse_err = true;
21663 cp_parser_statement_seq_opt (parser, NULL);
21664 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21671 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21672 for_block = TREE_CHAIN (for_block);
21679 #pragma omp for for-clause[optseq] new-line
21682 #define OMP_FOR_CLAUSE_MASK \
21683 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21684 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21685 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21686 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21687 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21688 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21689 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21690 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21693 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21695 tree clauses, sb, ret;
21698 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21699 "#pragma omp for", pragma_tok);
21701 sb = begin_omp_structured_block ();
21702 save = cp_parser_begin_omp_structured_block (parser);
21704 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21706 cp_parser_end_omp_structured_block (parser, save);
21707 add_stmt (finish_omp_structured_block (sb));
21713 # pragma omp master new-line
21714 structured-block */
21717 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21719 cp_parser_require_pragma_eol (parser, pragma_tok);
21720 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21724 # pragma omp ordered new-line
21725 structured-block */
21728 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21730 cp_parser_require_pragma_eol (parser, pragma_tok);
21731 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21737 { section-sequence }
21740 section-directive[opt] structured-block
21741 section-sequence section-directive structured-block */
21744 cp_parser_omp_sections_scope (cp_parser *parser)
21746 tree stmt, substmt;
21747 bool error_suppress = false;
21750 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21753 stmt = push_stmt_list ();
21755 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21759 substmt = begin_omp_structured_block ();
21760 save = cp_parser_begin_omp_structured_block (parser);
21764 cp_parser_statement (parser, NULL_TREE, false, NULL);
21766 tok = cp_lexer_peek_token (parser->lexer);
21767 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21769 if (tok->type == CPP_CLOSE_BRACE)
21771 if (tok->type == CPP_EOF)
21775 cp_parser_end_omp_structured_block (parser, save);
21776 substmt = finish_omp_structured_block (substmt);
21777 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21778 add_stmt (substmt);
21783 tok = cp_lexer_peek_token (parser->lexer);
21784 if (tok->type == CPP_CLOSE_BRACE)
21786 if (tok->type == CPP_EOF)
21789 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21791 cp_lexer_consume_token (parser->lexer);
21792 cp_parser_require_pragma_eol (parser, tok);
21793 error_suppress = false;
21795 else if (!error_suppress)
21797 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21798 error_suppress = true;
21801 substmt = cp_parser_omp_structured_block (parser);
21802 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21803 add_stmt (substmt);
21805 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21807 substmt = pop_stmt_list (stmt);
21809 stmt = make_node (OMP_SECTIONS);
21810 TREE_TYPE (stmt) = void_type_node;
21811 OMP_SECTIONS_BODY (stmt) = substmt;
21818 # pragma omp sections sections-clause[optseq] newline
21821 #define OMP_SECTIONS_CLAUSE_MASK \
21822 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21823 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21824 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21825 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21826 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21829 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21833 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21834 "#pragma omp sections", pragma_tok);
21836 ret = cp_parser_omp_sections_scope (parser);
21838 OMP_SECTIONS_CLAUSES (ret) = clauses;
21844 # pragma parallel parallel-clause new-line
21845 # pragma parallel for parallel-for-clause new-line
21846 # pragma parallel sections parallel-sections-clause new-line */
21848 #define OMP_PARALLEL_CLAUSE_MASK \
21849 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21850 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21851 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21852 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21853 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21854 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21855 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21856 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21859 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21861 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21862 const char *p_name = "#pragma omp parallel";
21863 tree stmt, clauses, par_clause, ws_clause, block;
21864 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21869 cp_lexer_consume_token (parser->lexer);
21870 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21871 p_name = "#pragma omp parallel for";
21872 mask |= OMP_FOR_CLAUSE_MASK;
21873 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21875 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21877 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21878 const char *p = IDENTIFIER_POINTER (id);
21879 if (strcmp (p, "sections") == 0)
21881 cp_lexer_consume_token (parser->lexer);
21882 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21883 p_name = "#pragma omp parallel sections";
21884 mask |= OMP_SECTIONS_CLAUSE_MASK;
21885 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21889 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21890 block = begin_omp_parallel ();
21891 save = cp_parser_begin_omp_structured_block (parser);
21895 case PRAGMA_OMP_PARALLEL:
21896 cp_parser_statement (parser, NULL_TREE, false, NULL);
21897 par_clause = clauses;
21900 case PRAGMA_OMP_PARALLEL_FOR:
21901 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21902 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21905 case PRAGMA_OMP_PARALLEL_SECTIONS:
21906 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21907 stmt = cp_parser_omp_sections_scope (parser);
21909 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21913 gcc_unreachable ();
21916 cp_parser_end_omp_structured_block (parser, save);
21917 stmt = finish_omp_parallel (par_clause, block);
21918 if (p_kind != PRAGMA_OMP_PARALLEL)
21919 OMP_PARALLEL_COMBINED (stmt) = 1;
21924 # pragma omp single single-clause[optseq] new-line
21925 structured-block */
21927 #define OMP_SINGLE_CLAUSE_MASK \
21928 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21929 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21930 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21931 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21934 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21936 tree stmt = make_node (OMP_SINGLE);
21937 TREE_TYPE (stmt) = void_type_node;
21939 OMP_SINGLE_CLAUSES (stmt)
21940 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21941 "#pragma omp single", pragma_tok);
21942 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21944 return add_stmt (stmt);
21948 # pragma omp task task-clause[optseq] new-line
21949 structured-block */
21951 #define OMP_TASK_CLAUSE_MASK \
21952 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21953 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21954 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21955 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21956 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21957 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21960 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21962 tree clauses, block;
21965 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21966 "#pragma omp task", pragma_tok);
21967 block = begin_omp_task ();
21968 save = cp_parser_begin_omp_structured_block (parser);
21969 cp_parser_statement (parser, NULL_TREE, false, NULL);
21970 cp_parser_end_omp_structured_block (parser, save);
21971 return finish_omp_task (clauses, block);
21975 # pragma omp taskwait new-line */
21978 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21980 cp_parser_require_pragma_eol (parser, pragma_tok);
21981 finish_omp_taskwait ();
21985 # pragma omp threadprivate (variable-list) */
21988 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21992 vars = cp_parser_omp_var_list (parser, 0, NULL);
21993 cp_parser_require_pragma_eol (parser, pragma_tok);
21995 finish_omp_threadprivate (vars);
21998 /* Main entry point to OpenMP statement pragmas. */
22001 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22005 switch (pragma_tok->pragma_kind)
22007 case PRAGMA_OMP_ATOMIC:
22008 cp_parser_omp_atomic (parser, pragma_tok);
22010 case PRAGMA_OMP_CRITICAL:
22011 stmt = cp_parser_omp_critical (parser, pragma_tok);
22013 case PRAGMA_OMP_FOR:
22014 stmt = cp_parser_omp_for (parser, pragma_tok);
22016 case PRAGMA_OMP_MASTER:
22017 stmt = cp_parser_omp_master (parser, pragma_tok);
22019 case PRAGMA_OMP_ORDERED:
22020 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22022 case PRAGMA_OMP_PARALLEL:
22023 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22025 case PRAGMA_OMP_SECTIONS:
22026 stmt = cp_parser_omp_sections (parser, pragma_tok);
22028 case PRAGMA_OMP_SINGLE:
22029 stmt = cp_parser_omp_single (parser, pragma_tok);
22031 case PRAGMA_OMP_TASK:
22032 stmt = cp_parser_omp_task (parser, pragma_tok);
22035 gcc_unreachable ();
22039 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22044 static GTY (()) cp_parser *the_parser;
22047 /* Special handling for the first token or line in the file. The first
22048 thing in the file might be #pragma GCC pch_preprocess, which loads a
22049 PCH file, which is a GC collection point. So we need to handle this
22050 first pragma without benefit of an existing lexer structure.
22052 Always returns one token to the caller in *FIRST_TOKEN. This is
22053 either the true first token of the file, or the first token after
22054 the initial pragma. */
22057 cp_parser_initial_pragma (cp_token *first_token)
22061 cp_lexer_get_preprocessor_token (NULL, first_token);
22062 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22065 cp_lexer_get_preprocessor_token (NULL, first_token);
22066 if (first_token->type == CPP_STRING)
22068 name = first_token->u.value;
22070 cp_lexer_get_preprocessor_token (NULL, first_token);
22071 if (first_token->type != CPP_PRAGMA_EOL)
22072 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22073 &first_token->location);
22076 error ("%Hexpected string literal", &first_token->location);
22078 /* Skip to the end of the pragma. */
22079 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22080 cp_lexer_get_preprocessor_token (NULL, first_token);
22082 /* Now actually load the PCH file. */
22084 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22086 /* Read one more token to return to our caller. We have to do this
22087 after reading the PCH file in, since its pointers have to be
22089 cp_lexer_get_preprocessor_token (NULL, first_token);
22092 /* Normal parsing of a pragma token. Here we can (and must) use the
22096 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22098 cp_token *pragma_tok;
22101 pragma_tok = cp_lexer_consume_token (parser->lexer);
22102 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22103 parser->lexer->in_pragma = true;
22105 id = pragma_tok->pragma_kind;
22108 case PRAGMA_GCC_PCH_PREPROCESS:
22109 error ("%H%<#pragma GCC pch_preprocess%> must be first",
22110 &pragma_tok->location);
22113 case PRAGMA_OMP_BARRIER:
22116 case pragma_compound:
22117 cp_parser_omp_barrier (parser, pragma_tok);
22120 error ("%H%<#pragma omp barrier%> may only be "
22121 "used in compound statements", &pragma_tok->location);
22128 case PRAGMA_OMP_FLUSH:
22131 case pragma_compound:
22132 cp_parser_omp_flush (parser, pragma_tok);
22135 error ("%H%<#pragma omp flush%> may only be "
22136 "used in compound statements", &pragma_tok->location);
22143 case PRAGMA_OMP_TASKWAIT:
22146 case pragma_compound:
22147 cp_parser_omp_taskwait (parser, pragma_tok);
22150 error ("%H%<#pragma omp taskwait%> may only be "
22151 "used in compound statements",
22152 &pragma_tok->location);
22159 case PRAGMA_OMP_THREADPRIVATE:
22160 cp_parser_omp_threadprivate (parser, pragma_tok);
22163 case PRAGMA_OMP_ATOMIC:
22164 case PRAGMA_OMP_CRITICAL:
22165 case PRAGMA_OMP_FOR:
22166 case PRAGMA_OMP_MASTER:
22167 case PRAGMA_OMP_ORDERED:
22168 case PRAGMA_OMP_PARALLEL:
22169 case PRAGMA_OMP_SECTIONS:
22170 case PRAGMA_OMP_SINGLE:
22171 case PRAGMA_OMP_TASK:
22172 if (context == pragma_external)
22174 cp_parser_omp_construct (parser, pragma_tok);
22177 case PRAGMA_OMP_SECTION:
22178 error ("%H%<#pragma omp section%> may only be used in "
22179 "%<#pragma omp sections%> construct", &pragma_tok->location);
22183 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22184 c_invoke_pragma_handler (id);
22188 cp_parser_error (parser, "expected declaration specifiers");
22192 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22196 /* The interface the pragma parsers have to the lexer. */
22199 pragma_lex (tree *value)
22202 enum cpp_ttype ret;
22204 tok = cp_lexer_peek_token (the_parser->lexer);
22207 *value = tok->u.value;
22209 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22211 else if (ret == CPP_STRING)
22212 *value = cp_parser_string_literal (the_parser, false, false);
22215 cp_lexer_consume_token (the_parser->lexer);
22216 if (ret == CPP_KEYWORD)
22224 /* External interface. */
22226 /* Parse one entire translation unit. */
22229 c_parse_file (void)
22231 bool error_occurred;
22232 static bool already_called = false;
22234 if (already_called)
22236 sorry ("inter-module optimizations not implemented for C++");
22239 already_called = true;
22241 the_parser = cp_parser_new ();
22242 push_deferring_access_checks (flag_access_control
22243 ? dk_no_deferred : dk_no_check);
22244 error_occurred = cp_parser_translation_unit (the_parser);
22248 #include "gt-cp-parser.h"