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. */
314 gcc_assert (lexer->next_token->type != CPP_PURGED);
318 /* Create a new lexer whose token stream is primed with the tokens in
319 CACHE. When these tokens are exhausted, no new tokens will be read. */
322 cp_lexer_new_from_tokens (cp_token_cache *cache)
324 cp_token *first = cache->first;
325 cp_token *last = cache->last;
326 cp_lexer *lexer = GGC_CNEW (cp_lexer);
328 /* We do not own the buffer. */
329 lexer->buffer = NULL;
330 lexer->buffer_length = 0;
331 lexer->next_token = first == last ? &eof_token : first;
332 lexer->last_token = last;
334 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
335 CP_SAVED_TOKEN_STACK);
337 #ifdef ENABLE_CHECKING
338 /* Initially we are not debugging. */
339 lexer->debugging_p = false;
342 gcc_assert (lexer->next_token->type != CPP_PURGED);
346 /* Frees all resources associated with LEXER. */
349 cp_lexer_destroy (cp_lexer *lexer)
352 ggc_free (lexer->buffer);
353 VEC_free (cp_token_position, heap, lexer->saved_tokens);
357 /* Returns nonzero if debugging information should be output. */
359 #ifdef ENABLE_CHECKING
362 cp_lexer_debugging_p (cp_lexer *lexer)
364 return lexer->debugging_p;
367 #endif /* ENABLE_CHECKING */
369 static inline cp_token_position
370 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
372 gcc_assert (!previous_p || lexer->next_token != &eof_token);
374 return lexer->next_token - previous_p;
377 static inline cp_token *
378 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
383 /* nonzero if we are presently saving tokens. */
386 cp_lexer_saving_tokens (const cp_lexer* lexer)
388 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 /* Store the next token from the preprocessor in *TOKEN. Return true
392 if we reach EOF. If LEXER is NULL, assume we are handling an
393 initial #pragma pch_preprocess, and thus want the lexer to return
394 processed strings. */
397 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
399 static int is_extern_c = 0;
401 /* Get a new token from the preprocessor. */
403 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
404 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
405 token->keyword = RID_MAX;
406 token->pragma_kind = PRAGMA_NONE;
408 /* On some systems, some header files are surrounded by an
409 implicit extern "C" block. Set a flag in the token if it
410 comes from such a header. */
411 is_extern_c += pending_lang_change;
412 pending_lang_change = 0;
413 token->implicit_extern_c = is_extern_c > 0;
415 /* Check to see if this token is a keyword. */
416 if (token->type == CPP_NAME)
418 if (C_IS_RESERVED_WORD (token->u.value))
420 /* Mark this token as a keyword. */
421 token->type = CPP_KEYWORD;
422 /* Record which keyword. */
423 token->keyword = C_RID_CODE (token->u.value);
424 /* Update the value. Some keywords are mapped to particular
425 entities, rather than simply having the value of the
426 corresponding IDENTIFIER_NODE. For example, `__const' is
427 mapped to `const'. */
428 token->u.value = ridpointers[token->keyword];
432 if (warn_cxx0x_compat
433 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
434 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
436 /* Warn about the C++0x keyword (but still treat it as
438 warning (OPT_Wc__0x_compat,
439 "identifier %<%s%> will become a keyword in C++0x",
440 IDENTIFIER_POINTER (token->u.value));
442 /* Clear out the C_RID_CODE so we don't warn about this
443 particular identifier-turned-keyword again. */
444 C_SET_RID_CODE (token->u.value, RID_MAX);
447 token->ambiguous_p = false;
448 token->keyword = RID_MAX;
451 /* Handle Objective-C++ keywords. */
452 else if (token->type == CPP_AT_NAME)
454 token->type = CPP_KEYWORD;
455 switch (C_RID_CODE (token->u.value))
457 /* Map 'class' to '@class', 'private' to '@private', etc. */
458 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
459 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
460 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
461 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
462 case RID_THROW: token->keyword = RID_AT_THROW; break;
463 case RID_TRY: token->keyword = RID_AT_TRY; break;
464 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
465 default: token->keyword = C_RID_CODE (token->u.value);
468 else if (token->type == CPP_PRAGMA)
470 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
471 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
472 token->u.value = NULL_TREE;
476 /* Update the globals input_location and the input file stack from TOKEN. */
478 cp_lexer_set_source_position_from_token (cp_token *token)
480 if (token->type != CPP_EOF)
482 input_location = token->location;
486 /* Return a pointer to the next token in the token stream, but do not
489 static inline cp_token *
490 cp_lexer_peek_token (cp_lexer *lexer)
492 if (cp_lexer_debugging_p (lexer))
494 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
495 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
496 putc ('\n', cp_lexer_debug_stream);
498 return lexer->next_token;
501 /* Return true if the next token has the indicated TYPE. */
504 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
506 return cp_lexer_peek_token (lexer)->type == type;
509 /* Return true if the next token does not have the indicated TYPE. */
512 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
514 return !cp_lexer_next_token_is (lexer, type);
517 /* Return true if the next token is the indicated KEYWORD. */
520 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
522 return cp_lexer_peek_token (lexer)->keyword == keyword;
525 /* Return true if the next token is not the indicated KEYWORD. */
528 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
530 return cp_lexer_peek_token (lexer)->keyword != keyword;
533 /* Return true if the next token is a keyword for a decl-specifier. */
536 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
540 token = cp_lexer_peek_token (lexer);
541 switch (token->keyword)
543 /* auto specifier: storage-class-specifier in C++,
544 simple-type-specifier in C++0x. */
546 /* Storage classes. */
552 /* Elaborated type specifiers. */
558 /* Simple type specifiers. */
572 /* GNU extensions. */
575 /* C++0x extensions. */
584 /* Return a pointer to the Nth token in the token stream. If N is 1,
585 then this is precisely equivalent to cp_lexer_peek_token (except
586 that it is not inline). One would like to disallow that case, but
587 there is one case (cp_parser_nth_token_starts_template_id) where
588 the caller passes a variable for N and it might be 1. */
591 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
595 /* N is 1-based, not zero-based. */
598 if (cp_lexer_debugging_p (lexer))
599 fprintf (cp_lexer_debug_stream,
600 "cp_lexer: peeking ahead %ld at token: ", (long)n);
603 token = lexer->next_token;
604 gcc_assert (!n || token != &eof_token);
608 if (token == lexer->last_token)
614 if (token->type != CPP_PURGED)
618 if (cp_lexer_debugging_p (lexer))
620 cp_lexer_print_token (cp_lexer_debug_stream, token);
621 putc ('\n', cp_lexer_debug_stream);
627 /* Return the next token, and advance the lexer's next_token pointer
628 to point to the next non-purged token. */
631 cp_lexer_consume_token (cp_lexer* lexer)
633 cp_token *token = lexer->next_token;
635 gcc_assert (token != &eof_token);
636 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
641 if (lexer->next_token == lexer->last_token)
643 lexer->next_token = &eof_token;
648 while (lexer->next_token->type == CPP_PURGED);
650 cp_lexer_set_source_position_from_token (token);
652 /* Provide debugging output. */
653 if (cp_lexer_debugging_p (lexer))
655 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
656 cp_lexer_print_token (cp_lexer_debug_stream, token);
657 putc ('\n', cp_lexer_debug_stream);
663 /* Permanently remove the next token from the token stream, and
664 advance the next_token pointer to refer to the next non-purged
668 cp_lexer_purge_token (cp_lexer *lexer)
670 cp_token *tok = lexer->next_token;
672 gcc_assert (tok != &eof_token);
673 tok->type = CPP_PURGED;
674 tok->location = UNKNOWN_LOCATION;
675 tok->u.value = NULL_TREE;
676 tok->keyword = RID_MAX;
681 if (tok == lexer->last_token)
687 while (tok->type == CPP_PURGED);
688 lexer->next_token = tok;
691 /* Permanently remove all tokens after TOK, up to, but not
692 including, the token that will be returned next by
693 cp_lexer_peek_token. */
696 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
698 cp_token *peek = lexer->next_token;
700 if (peek == &eof_token)
701 peek = lexer->last_token;
703 gcc_assert (tok < peek);
705 for ( tok += 1; tok != peek; tok += 1)
707 tok->type = CPP_PURGED;
708 tok->location = UNKNOWN_LOCATION;
709 tok->u.value = NULL_TREE;
710 tok->keyword = RID_MAX;
714 /* Begin saving tokens. All tokens consumed after this point will be
718 cp_lexer_save_tokens (cp_lexer* lexer)
720 /* Provide debugging output. */
721 if (cp_lexer_debugging_p (lexer))
722 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
724 VEC_safe_push (cp_token_position, heap,
725 lexer->saved_tokens, lexer->next_token);
728 /* Commit to the portion of the token stream most recently saved. */
731 cp_lexer_commit_tokens (cp_lexer* lexer)
733 /* Provide debugging output. */
734 if (cp_lexer_debugging_p (lexer))
735 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
737 VEC_pop (cp_token_position, lexer->saved_tokens);
740 /* Return all tokens saved since the last call to cp_lexer_save_tokens
741 to the token stream. Stop saving tokens. */
744 cp_lexer_rollback_tokens (cp_lexer* lexer)
746 /* Provide debugging output. */
747 if (cp_lexer_debugging_p (lexer))
748 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
750 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 /* Print a representation of the TOKEN on the STREAM. */
755 #ifdef ENABLE_CHECKING
758 cp_lexer_print_token (FILE * stream, cp_token *token)
760 /* We don't use cpp_type2name here because the parser defines
761 a few tokens of its own. */
762 static const char *const token_names[] = {
763 /* cpplib-defined token types */
769 /* C++ parser token types - see "Manifest constants", above. */
772 "NESTED_NAME_SPECIFIER",
776 /* If we have a name for the token, print it out. Otherwise, we
777 simply give the numeric code. */
778 gcc_assert (token->type < ARRAY_SIZE(token_names));
779 fputs (token_names[token->type], stream);
781 /* For some tokens, print the associated data. */
785 /* Some keywords have a value that is not an IDENTIFIER_NODE.
786 For example, `struct' is mapped to an INTEGER_CST. */
787 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
789 /* else fall through */
791 fputs (IDENTIFIER_POINTER (token->u.value), stream);
798 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
806 /* Start emitting debugging information. */
809 cp_lexer_start_debugging (cp_lexer* lexer)
811 lexer->debugging_p = true;
814 /* Stop emitting debugging information. */
817 cp_lexer_stop_debugging (cp_lexer* lexer)
819 lexer->debugging_p = false;
822 #endif /* ENABLE_CHECKING */
824 /* Create a new cp_token_cache, representing a range of tokens. */
826 static cp_token_cache *
827 cp_token_cache_new (cp_token *first, cp_token *last)
829 cp_token_cache *cache = GGC_NEW (cp_token_cache);
830 cache->first = first;
836 /* Decl-specifiers. */
838 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
841 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
843 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
848 /* Nothing other than the parser should be creating declarators;
849 declarators are a semi-syntactic representation of C++ entities.
850 Other parts of the front end that need to create entities (like
851 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
853 static cp_declarator *make_call_declarator
854 (cp_declarator *, tree, cp_cv_quals, tree, tree);
855 static cp_declarator *make_array_declarator
856 (cp_declarator *, tree);
857 static cp_declarator *make_pointer_declarator
858 (cp_cv_quals, cp_declarator *);
859 static cp_declarator *make_reference_declarator
860 (cp_cv_quals, cp_declarator *, bool);
861 static cp_parameter_declarator *make_parameter_declarator
862 (cp_decl_specifier_seq *, cp_declarator *, tree);
863 static cp_declarator *make_ptrmem_declarator
864 (cp_cv_quals, tree, cp_declarator *);
866 /* An erroneous declarator. */
867 static cp_declarator *cp_error_declarator;
869 /* The obstack on which declarators and related data structures are
871 static struct obstack declarator_obstack;
873 /* Alloc BYTES from the declarator memory pool. */
876 alloc_declarator (size_t bytes)
878 return obstack_alloc (&declarator_obstack, bytes);
881 /* Allocate a declarator of the indicated KIND. Clear fields that are
882 common to all declarators. */
884 static cp_declarator *
885 make_declarator (cp_declarator_kind kind)
887 cp_declarator *declarator;
889 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
890 declarator->kind = kind;
891 declarator->attributes = NULL_TREE;
892 declarator->declarator = NULL;
893 declarator->parameter_pack_p = false;
898 /* Make a declarator for a generalized identifier. If
899 QUALIFYING_SCOPE is non-NULL, the identifier is
900 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
901 UNQUALIFIED_NAME. SFK indicates the kind of special function this
904 static cp_declarator *
905 make_id_declarator (tree qualifying_scope, tree unqualified_name,
906 special_function_kind sfk)
908 cp_declarator *declarator;
910 /* It is valid to write:
912 class C { void f(); };
916 The standard is not clear about whether `typedef const C D' is
917 legal; as of 2002-09-15 the committee is considering that
918 question. EDG 3.0 allows that syntax. Therefore, we do as
920 if (qualifying_scope && TYPE_P (qualifying_scope))
921 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
923 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
924 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
925 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
927 declarator = make_declarator (cdk_id);
928 declarator->u.id.qualifying_scope = qualifying_scope;
929 declarator->u.id.unqualified_name = unqualified_name;
930 declarator->u.id.sfk = sfk;
935 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
936 of modifiers such as const or volatile to apply to the pointer
937 type, represented as identifiers. */
940 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
942 cp_declarator *declarator;
944 declarator = make_declarator (cdk_pointer);
945 declarator->declarator = target;
946 declarator->u.pointer.qualifiers = cv_qualifiers;
947 declarator->u.pointer.class_type = NULL_TREE;
950 declarator->parameter_pack_p = target->parameter_pack_p;
951 target->parameter_pack_p = false;
954 declarator->parameter_pack_p = false;
959 /* Like make_pointer_declarator -- but for references. */
962 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965 cp_declarator *declarator;
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.reference.qualifiers = cv_qualifiers;
970 declarator->u.reference.rvalue_ref = rvalue_ref;
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
977 declarator->parameter_pack_p = false;
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
989 cp_declarator *declarator;
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1002 declarator->parameter_pack_p = false;
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1013 make_call_declarator (cp_declarator *target,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification,
1017 tree late_return_type)
1019 cp_declarator *declarator;
1021 declarator = make_declarator (cdk_function);
1022 declarator->declarator = target;
1023 declarator->u.function.parameters = parms;
1024 declarator->u.function.qualifiers = cv_qualifiers;
1025 declarator->u.function.exception_specification = exception_specification;
1026 declarator->u.function.late_return_type = late_return_type;
1029 declarator->parameter_pack_p = target->parameter_pack_p;
1030 target->parameter_pack_p = false;
1033 declarator->parameter_pack_p = false;
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039 defined by ELEMENT. */
1042 make_array_declarator (cp_declarator *element, tree bounds)
1044 cp_declarator *declarator;
1046 declarator = make_declarator (cdk_array);
1047 declarator->declarator = element;
1048 declarator->u.array.bounds = bounds;
1051 declarator->parameter_pack_p = element->parameter_pack_p;
1052 element->parameter_pack_p = false;
1055 declarator->parameter_pack_p = false;
1060 /* Determine whether the declarator we've seen so far can be a
1061 parameter pack, when followed by an ellipsis. */
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1065 /* Search for a declarator name, or any other declarator that goes
1066 after the point where the ellipsis could appear in a parameter
1067 pack. If we find any of these, then this declarator can not be
1068 made into a parameter pack. */
1070 while (declarator && !found)
1072 switch ((int)declarator->kind)
1083 declarator = declarator->declarator;
1091 cp_parameter_declarator *no_parameters;
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094 DECLARATOR and DEFAULT_ARGUMENT. */
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098 cp_declarator *declarator,
1099 tree default_argument)
1101 cp_parameter_declarator *parameter;
1103 parameter = ((cp_parameter_declarator *)
1104 alloc_declarator (sizeof (cp_parameter_declarator)));
1105 parameter->next = NULL;
1106 if (decl_specifiers)
1107 parameter->decl_specifiers = *decl_specifiers;
1109 clear_decl_specs (¶meter->decl_specifiers);
1110 parameter->declarator = declarator;
1111 parameter->default_argument = default_argument;
1112 parameter->ellipsis_p = false;
1117 /* Returns true iff DECLARATOR is a declaration for a function. */
1120 function_declarator_p (const cp_declarator *declarator)
1124 if (declarator->kind == cdk_function
1125 && declarator->declarator->kind == cdk_id)
1127 if (declarator->kind == cdk_id
1128 || declarator->kind == cdk_error)
1130 declarator = declarator->declarator;
1140 A cp_parser parses the token stream as specified by the C++
1141 grammar. Its job is purely parsing, not semantic analysis. For
1142 example, the parser breaks the token stream into declarators,
1143 expressions, statements, and other similar syntactic constructs.
1144 It does not check that the types of the expressions on either side
1145 of an assignment-statement are compatible, or that a function is
1146 not declared with a parameter of type `void'.
1148 The parser invokes routines elsewhere in the compiler to perform
1149 semantic analysis and to build up the abstract syntax tree for the
1152 The parser (and the template instantiation code, which is, in a
1153 way, a close relative of parsing) are the only parts of the
1154 compiler that should be calling push_scope and pop_scope, or
1155 related functions. The parser (and template instantiation code)
1156 keeps track of what scope is presently active; everything else
1157 should simply honor that. (The code that generates static
1158 initializers may also need to set the scope, in order to check
1159 access control correctly when emitting the initializers.)
1164 The parser is of the standard recursive-descent variety. Upcoming
1165 tokens in the token stream are examined in order to determine which
1166 production to use when parsing a non-terminal. Some C++ constructs
1167 require arbitrary look ahead to disambiguate. For example, it is
1168 impossible, in the general case, to tell whether a statement is an
1169 expression or declaration without scanning the entire statement.
1170 Therefore, the parser is capable of "parsing tentatively." When the
1171 parser is not sure what construct comes next, it enters this mode.
1172 Then, while we attempt to parse the construct, the parser queues up
1173 error messages, rather than issuing them immediately, and saves the
1174 tokens it consumes. If the construct is parsed successfully, the
1175 parser "commits", i.e., it issues any queued error messages and
1176 the tokens that were being preserved are permanently discarded.
1177 If, however, the construct is not parsed successfully, the parser
1178 rolls back its state completely so that it can resume parsing using
1179 a different alternative.
1184 The performance of the parser could probably be improved substantially.
1185 We could often eliminate the need to parse tentatively by looking ahead
1186 a little bit. In some places, this approach might not entirely eliminate
1187 the need to parse tentatively, but it might still speed up the average
1190 /* Flags that are passed to some parsing functions. These values can
1191 be bitwise-ored together. */
1193 typedef enum cp_parser_flags
1196 CP_PARSER_FLAGS_NONE = 0x0,
1197 /* The construct is optional. If it is not present, then no error
1198 should be issued. */
1199 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200 /* When parsing a type-specifier, do not allow user-defined types. */
1201 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 /* The different kinds of declarators we want to parse. */
1206 typedef enum cp_parser_declarator_kind
1208 /* We want an abstract declarator. */
1209 CP_PARSER_DECLARATOR_ABSTRACT,
1210 /* We want a named declarator. */
1211 CP_PARSER_DECLARATOR_NAMED,
1212 /* We don't mind, but the name must be an unqualified-id. */
1213 CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1216 /* The precedence values used to parse binary expressions. The minimum value
1217 of PREC must be 1, because zero is reserved to quickly discriminate
1218 binary operators from other tokens. */
1223 PREC_LOGICAL_OR_EXPRESSION,
1224 PREC_LOGICAL_AND_EXPRESSION,
1225 PREC_INCLUSIVE_OR_EXPRESSION,
1226 PREC_EXCLUSIVE_OR_EXPRESSION,
1227 PREC_AND_EXPRESSION,
1228 PREC_EQUALITY_EXPRESSION,
1229 PREC_RELATIONAL_EXPRESSION,
1230 PREC_SHIFT_EXPRESSION,
1231 PREC_ADDITIVE_EXPRESSION,
1232 PREC_MULTIPLICATIVE_EXPRESSION,
1234 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238 precedence value. */
1240 typedef struct cp_parser_binary_operations_map_node
1242 /* The token type. */
1243 enum cpp_ttype token_type;
1244 /* The corresponding tree code. */
1245 enum tree_code tree_type;
1246 /* The precedence of this operator. */
1247 enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1250 /* The status of a tentative parse. */
1252 typedef enum cp_parser_status_kind
1254 /* No errors have occurred. */
1255 CP_PARSER_STATUS_KIND_NO_ERROR,
1256 /* An error has occurred. */
1257 CP_PARSER_STATUS_KIND_ERROR,
1258 /* We are committed to this tentative parse, whether or not an error
1260 CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1263 typedef struct cp_parser_expression_stack_entry
1265 /* Left hand side of the binary operation we are currently
1268 /* Original tree code for left hand side, if it was a binary
1269 expression itself (used for -Wparentheses). */
1270 enum tree_code lhs_type;
1271 /* Tree code for the binary operation we are parsing. */
1272 enum tree_code tree_type;
1273 /* Precedence of the binary operation we are parsing. */
1275 } cp_parser_expression_stack_entry;
1277 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1278 entries because precedence levels on the stack are monotonically
1280 typedef struct cp_parser_expression_stack_entry
1281 cp_parser_expression_stack[NUM_PREC_VALUES];
1283 /* Context that is saved and restored when parsing tentatively. */
1284 typedef struct cp_parser_context GTY (())
1286 /* If this is a tentative parsing context, the status of the
1288 enum cp_parser_status_kind status;
1289 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1290 that are looked up in this context must be looked up both in the
1291 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292 the context of the containing expression. */
1295 /* The next parsing context in the stack. */
1296 struct cp_parser_context *next;
1297 } cp_parser_context;
1301 /* Constructors and destructors. */
1303 static cp_parser_context *cp_parser_context_new
1304 (cp_parser_context *);
1306 /* Class variables. */
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311 Transformed into an associative array (binops_by_token) by
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1318 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1338 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1340 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1342 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1344 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 /* The same as binops, but initialized by cp_parser_new so that
1348 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1352 /* Constructors and destructors. */
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1360 cp_parser_context *context;
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1371 context = GGC_CNEW (cp_parser_context);
1373 /* No errors have occurred yet in this context. */
1374 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375 /* If this is not the bottommost context, copy information that we
1376 need from the previous context. */
1379 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380 expression, then we are parsing one in this context, too. */
1381 context->object_type = next->object_type;
1382 /* Thread the stack. */
1383 context->next = next;
1389 /* The cp_parser structure represents the C++ parser. */
1391 typedef struct cp_parser GTY(())
1393 /* The lexer from which we are obtaining tokens. */
1396 /* The scope in which names should be looked up. If NULL_TREE, then
1397 we look up names in the scope that is currently open in the
1398 source program. If non-NULL, this is either a TYPE or
1399 NAMESPACE_DECL for the scope in which we should look. It can
1400 also be ERROR_MARK, when we've parsed a bogus scope.
1402 This value is not cleared automatically after a name is looked
1403 up, so we must be careful to clear it before starting a new look
1404 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1405 will look up `Z' in the scope of `X', rather than the current
1406 scope.) Unfortunately, it is difficult to tell when name lookup
1407 is complete, because we sometimes peek at a token, look it up,
1408 and then decide not to consume it. */
1411 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412 last lookup took place. OBJECT_SCOPE is used if an expression
1413 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414 respectively. QUALIFYING_SCOPE is used for an expression of the
1415 form "X::Y"; it refers to X. */
1417 tree qualifying_scope;
1419 /* A stack of parsing contexts. All but the bottom entry on the
1420 stack will be tentative contexts.
1422 We parse tentatively in order to determine which construct is in
1423 use in some situations. For example, in order to determine
1424 whether a statement is an expression-statement or a
1425 declaration-statement we parse it tentatively as a
1426 declaration-statement. If that fails, we then reparse the same
1427 token stream as an expression-statement. */
1428 cp_parser_context *context;
1430 /* True if we are parsing GNU C++. If this flag is not set, then
1431 GNU extensions are not recognized. */
1432 bool allow_gnu_extensions_p;
1434 /* TRUE if the `>' token should be interpreted as the greater-than
1435 operator. FALSE if it is the end of a template-id or
1436 template-parameter-list. In C++0x mode, this flag also applies to
1437 `>>' tokens, which are viewed as two consecutive `>' tokens when
1438 this flag is FALSE. */
1439 bool greater_than_is_operator_p;
1441 /* TRUE if default arguments are allowed within a parameter list
1442 that starts at this point. FALSE if only a gnu extension makes
1443 them permissible. */
1444 bool default_arg_ok_p;
1446 /* TRUE if we are parsing an integral constant-expression. See
1447 [expr.const] for a precise definition. */
1448 bool integral_constant_expression_p;
1450 /* TRUE if we are parsing an integral constant-expression -- but a
1451 non-constant expression should be permitted as well. This flag
1452 is used when parsing an array bound so that GNU variable-length
1453 arrays are tolerated. */
1454 bool allow_non_integral_constant_expression_p;
1456 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457 been seen that makes the expression non-constant. */
1458 bool non_integral_constant_expression_p;
1460 /* TRUE if local variable names and `this' are forbidden in the
1462 bool local_variables_forbidden_p;
1464 /* TRUE if the declaration we are parsing is part of a
1465 linkage-specification of the form `extern string-literal
1467 bool in_unbraced_linkage_specification_p;
1469 /* TRUE if we are presently parsing a declarator, after the
1470 direct-declarator. */
1471 bool in_declarator_p;
1473 /* TRUE if we are presently parsing a template-argument-list. */
1474 bool in_template_argument_list_p;
1476 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477 to IN_OMP_BLOCK if parsing OpenMP structured block and
1478 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1479 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480 iteration-statement, OpenMP block or loop within that switch. */
1481 #define IN_SWITCH_STMT 1
1482 #define IN_ITERATION_STMT 2
1483 #define IN_OMP_BLOCK 4
1484 #define IN_OMP_FOR 8
1485 #define IN_IF_STMT 16
1486 unsigned char in_statement;
1488 /* TRUE if we are presently parsing the body of a switch statement.
1489 Note that this doesn't quite overlap with in_statement above.
1490 The difference relates to giving the right sets of error messages:
1491 "case not in switch" vs "break statement used with OpenMP...". */
1492 bool in_switch_statement_p;
1494 /* TRUE if we are parsing a type-id in an expression context. In
1495 such a situation, both "type (expr)" and "type (type)" are valid
1497 bool in_type_id_in_expr_p;
1499 /* TRUE if we are currently in a header file where declarations are
1500 implicitly extern "C". */
1501 bool implicit_extern_c;
1503 /* TRUE if strings in expressions should be translated to the execution
1505 bool translate_strings_p;
1507 /* TRUE if we are presently parsing the body of a function, but not
1509 bool in_function_body;
1511 /* If non-NULL, then we are parsing a construct where new type
1512 definitions are not permitted. The string stored here will be
1513 issued as an error message if a type is defined. */
1514 const char *type_definition_forbidden_message;
1516 /* A list of lists. The outer list is a stack, used for member
1517 functions of local classes. At each level there are two sub-list,
1518 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520 TREE_VALUE's. The functions are chained in reverse declaration
1523 The TREE_PURPOSE sublist contains those functions with default
1524 arguments that need post processing, and the TREE_VALUE sublist
1525 contains those functions with definitions that need post
1528 These lists can only be processed once the outermost class being
1529 defined is complete. */
1530 tree unparsed_functions_queues;
1532 /* The number of classes whose definitions are currently in
1534 unsigned num_classes_being_defined;
1536 /* The number of template parameter lists that apply directly to the
1537 current declaration. */
1538 unsigned num_template_parameter_lists;
1543 /* Constructors and destructors. */
1545 static cp_parser *cp_parser_new
1548 /* Routines to parse various constructs.
1550 Those that return `tree' will return the error_mark_node (rather
1551 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552 Sometimes, they will return an ordinary node if error-recovery was
1553 attempted, even though a parse error occurred. So, to check
1554 whether or not a parse error occurred, you should always use
1555 cp_parser_error_occurred. If the construct is optional (indicated
1556 either by an `_opt' in the name of the function that does the
1557 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558 the construct is not present. */
1560 /* Lexical conventions [gram.lex] */
1562 static tree cp_parser_identifier
1564 static tree cp_parser_string_literal
1565 (cp_parser *, bool, bool);
1567 /* Basic concepts [gram.basic] */
1569 static bool cp_parser_translation_unit
1572 /* Expressions [gram.expr] */
1574 static tree cp_parser_primary_expression
1575 (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577 (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579 (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_qualifying_entity
1585 (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587 (cp_parser *, bool, bool, bool, cp_id_kind *);
1588 static tree cp_parser_postfix_open_square_expression
1589 (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593 (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595 (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597 (cp_parser *, bool, bool, cp_id_kind *);
1598 static enum tree_code cp_parser_unary_operator
1600 static tree cp_parser_new_expression
1602 static tree cp_parser_new_placement
1604 static tree cp_parser_new_type_id
1605 (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1608 static cp_declarator *cp_parser_direct_new_declarator
1610 static tree cp_parser_new_initializer
1612 static tree cp_parser_delete_expression
1614 static tree cp_parser_cast_expression
1615 (cp_parser *, bool, bool, cp_id_kind *);
1616 static tree cp_parser_binary_expression
1617 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1618 static tree cp_parser_question_colon_clause
1619 (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621 (cp_parser *, bool, cp_id_kind *);
1622 static enum tree_code cp_parser_assignment_operator_opt
1624 static tree cp_parser_expression
1625 (cp_parser *, bool, cp_id_kind *);
1626 static tree cp_parser_constant_expression
1627 (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1631 /* Statements [gram.stmt.stmt] */
1633 static void cp_parser_statement
1634 (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1637 static tree cp_parser_expression_statement
1638 (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640 (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642 (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644 (cp_parser *, bool *);
1645 static tree cp_parser_condition
1647 static tree cp_parser_iteration_statement
1649 static void cp_parser_for_init_statement
1651 static tree cp_parser_jump_statement
1653 static void cp_parser_declaration_statement
1656 static tree cp_parser_implicitly_scoped_statement
1657 (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1661 /* Declarations [gram.dcl.dcl] */
1663 static void cp_parser_declaration_seq_opt
1665 static void cp_parser_declaration
1667 static void cp_parser_block_declaration
1668 (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1675 static tree cp_parser_function_specifier_opt
1676 (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1680 static tree cp_parser_simple_type_specifier
1681 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1684 static tree cp_parser_nonclass_name
1685 (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687 (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1690 static void cp_parser_enumerator_list
1691 (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693 (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1696 static void cp_parser_namespace_definition
1698 static void cp_parser_namespace_body
1700 static tree cp_parser_qualified_namespace_specifier
1702 static void cp_parser_namespace_alias_definition
1704 static bool cp_parser_using_declaration
1705 (cp_parser *, bool);
1706 static void cp_parser_using_directive
1708 static void cp_parser_asm_definition
1710 static void cp_parser_linkage_specification
1712 static void cp_parser_static_assert
1713 (cp_parser *, bool);
1714 static tree cp_parser_decltype
1717 /* Declarators [gram.dcl.decl] */
1719 static tree cp_parser_init_declarator
1720 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726 (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1729 static tree cp_parser_late_return_type_opt
1731 static tree cp_parser_declarator_id
1732 (cp_parser *, bool);
1733 static tree cp_parser_type_id
1735 static void cp_parser_type_specifier_seq
1736 (cp_parser *, bool, cp_decl_specifier_seq *);
1737 static tree cp_parser_parameter_declaration_clause
1739 static tree cp_parser_parameter_declaration_list
1740 (cp_parser *, bool *);
1741 static cp_parameter_declarator *cp_parser_parameter_declaration
1742 (cp_parser *, bool, bool *);
1743 static tree cp_parser_default_argument
1744 (cp_parser *, bool);
1745 static void cp_parser_function_body
1747 static tree cp_parser_initializer
1748 (cp_parser *, bool *, bool *);
1749 static tree cp_parser_initializer_clause
1750 (cp_parser *, bool *);
1751 static tree cp_parser_braced_list
1752 (cp_parser*, bool*);
1753 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1754 (cp_parser *, bool *);
1756 static bool cp_parser_ctor_initializer_opt_and_function_body
1759 /* Classes [gram.class] */
1761 static tree cp_parser_class_name
1762 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1763 static tree cp_parser_class_specifier
1765 static tree cp_parser_class_head
1766 (cp_parser *, bool *, tree *, tree *);
1767 static enum tag_types cp_parser_class_key
1769 static void cp_parser_member_specification_opt
1771 static void cp_parser_member_declaration
1773 static tree cp_parser_pure_specifier
1775 static tree cp_parser_constant_initializer
1778 /* Derived classes [gram.class.derived] */
1780 static tree cp_parser_base_clause
1782 static tree cp_parser_base_specifier
1785 /* Special member functions [gram.special] */
1787 static tree cp_parser_conversion_function_id
1789 static tree cp_parser_conversion_type_id
1791 static cp_declarator *cp_parser_conversion_declarator_opt
1793 static bool cp_parser_ctor_initializer_opt
1795 static void cp_parser_mem_initializer_list
1797 static tree cp_parser_mem_initializer
1799 static tree cp_parser_mem_initializer_id
1802 /* Overloading [gram.over] */
1804 static tree cp_parser_operator_function_id
1806 static tree cp_parser_operator
1809 /* Templates [gram.temp] */
1811 static void cp_parser_template_declaration
1812 (cp_parser *, bool);
1813 static tree cp_parser_template_parameter_list
1815 static tree cp_parser_template_parameter
1816 (cp_parser *, bool *, bool *);
1817 static tree cp_parser_type_parameter
1818 (cp_parser *, bool *);
1819 static tree cp_parser_template_id
1820 (cp_parser *, bool, bool, bool);
1821 static tree cp_parser_template_name
1822 (cp_parser *, bool, bool, bool, bool *);
1823 static tree cp_parser_template_argument_list
1825 static tree cp_parser_template_argument
1827 static void cp_parser_explicit_instantiation
1829 static void cp_parser_explicit_specialization
1832 /* Exception handling [gram.exception] */
1834 static tree cp_parser_try_block
1836 static bool cp_parser_function_try_block
1838 static void cp_parser_handler_seq
1840 static void cp_parser_handler
1842 static tree cp_parser_exception_declaration
1844 static tree cp_parser_throw_expression
1846 static tree cp_parser_exception_specification_opt
1848 static tree cp_parser_type_id_list
1851 /* GNU Extensions */
1853 static tree cp_parser_asm_specification_opt
1855 static tree cp_parser_asm_operand_list
1857 static tree cp_parser_asm_clobber_list
1859 static tree cp_parser_attributes_opt
1861 static tree cp_parser_attribute_list
1863 static bool cp_parser_extension_opt
1864 (cp_parser *, int *);
1865 static void cp_parser_label_declaration
1868 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1869 static bool cp_parser_pragma
1870 (cp_parser *, enum pragma_context);
1872 /* Objective-C++ Productions */
1874 static tree cp_parser_objc_message_receiver
1876 static tree cp_parser_objc_message_args
1878 static tree cp_parser_objc_message_expression
1880 static tree cp_parser_objc_encode_expression
1882 static tree cp_parser_objc_defs_expression
1884 static tree cp_parser_objc_protocol_expression
1886 static tree cp_parser_objc_selector_expression
1888 static tree cp_parser_objc_expression
1890 static bool cp_parser_objc_selector_p
1892 static tree cp_parser_objc_selector
1894 static tree cp_parser_objc_protocol_refs_opt
1896 static void cp_parser_objc_declaration
1898 static tree cp_parser_objc_statement
1901 /* Utility Routines */
1903 static tree cp_parser_lookup_name
1904 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1905 static tree cp_parser_lookup_name_simple
1906 (cp_parser *, tree, location_t);
1907 static tree cp_parser_maybe_treat_template_as_class
1909 static bool cp_parser_check_declarator_template_parameters
1910 (cp_parser *, cp_declarator *, location_t);
1911 static bool cp_parser_check_template_parameters
1912 (cp_parser *, unsigned, location_t);
1913 static tree cp_parser_simple_cast_expression
1915 static tree cp_parser_global_scope_opt
1916 (cp_parser *, bool);
1917 static bool cp_parser_constructor_declarator_p
1918 (cp_parser *, bool);
1919 static tree cp_parser_function_definition_from_specifiers_and_declarator
1920 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1921 static tree cp_parser_function_definition_after_declarator
1922 (cp_parser *, bool);
1923 static void cp_parser_template_declaration_after_export
1924 (cp_parser *, bool);
1925 static void cp_parser_perform_template_parameter_access_checks
1926 (VEC (deferred_access_check,gc)*);
1927 static tree cp_parser_single_declaration
1928 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1929 static tree cp_parser_functional_cast
1930 (cp_parser *, tree);
1931 static tree cp_parser_save_member_function_body
1932 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1933 static tree cp_parser_enclosed_template_argument_list
1935 static void cp_parser_save_default_args
1936 (cp_parser *, tree);
1937 static void cp_parser_late_parsing_for_member
1938 (cp_parser *, tree);
1939 static void cp_parser_late_parsing_default_args
1940 (cp_parser *, tree);
1941 static tree cp_parser_sizeof_operand
1942 (cp_parser *, enum rid);
1943 static tree cp_parser_trait_expr
1944 (cp_parser *, enum rid);
1945 static bool cp_parser_declares_only_class_p
1947 static void cp_parser_set_storage_class
1948 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1949 static void cp_parser_set_decl_spec_type
1950 (cp_decl_specifier_seq *, tree, location_t, bool);
1951 static bool cp_parser_friend_p
1952 (const cp_decl_specifier_seq *);
1953 static cp_token *cp_parser_require
1954 (cp_parser *, enum cpp_ttype, const char *);
1955 static cp_token *cp_parser_require_keyword
1956 (cp_parser *, enum rid, const char *);
1957 static bool cp_parser_token_starts_function_definition_p
1959 static bool cp_parser_next_token_starts_class_definition_p
1961 static bool cp_parser_next_token_ends_template_argument_p
1963 static bool cp_parser_nth_token_starts_template_argument_list_p
1964 (cp_parser *, size_t);
1965 static enum tag_types cp_parser_token_is_class_key
1967 static void cp_parser_check_class_key
1968 (enum tag_types, tree type);
1969 static void cp_parser_check_access_in_redeclaration
1970 (tree type, location_t location);
1971 static bool cp_parser_optional_template_keyword
1973 static void cp_parser_pre_parsed_nested_name_specifier
1975 static bool cp_parser_cache_group
1976 (cp_parser *, enum cpp_ttype, unsigned);
1977 static void cp_parser_parse_tentatively
1979 static void cp_parser_commit_to_tentative_parse
1981 static void cp_parser_abort_tentative_parse
1983 static bool cp_parser_parse_definitely
1985 static inline bool cp_parser_parsing_tentatively
1987 static bool cp_parser_uncommitted_to_tentative_parse_p
1989 static void cp_parser_error
1990 (cp_parser *, const char *);
1991 static void cp_parser_name_lookup_error
1992 (cp_parser *, tree, tree, const char *, location_t);
1993 static bool cp_parser_simulate_error
1995 static bool cp_parser_check_type_definition
1997 static void cp_parser_check_for_definition_in_return_type
1998 (cp_declarator *, tree, location_t type_location);
1999 static void cp_parser_check_for_invalid_template_id
2000 (cp_parser *, tree, location_t location);
2001 static bool cp_parser_non_integral_constant_expression
2002 (cp_parser *, const char *);
2003 static void cp_parser_diagnose_invalid_type_name
2004 (cp_parser *, tree, tree, location_t);
2005 static bool cp_parser_parse_and_diagnose_invalid_type_name
2007 static int cp_parser_skip_to_closing_parenthesis
2008 (cp_parser *, bool, bool, bool);
2009 static void cp_parser_skip_to_end_of_statement
2011 static void cp_parser_consume_semicolon_at_end_of_statement
2013 static void cp_parser_skip_to_end_of_block_or_statement
2015 static bool cp_parser_skip_to_closing_brace
2017 static void cp_parser_skip_to_end_of_template_parameter_list
2019 static void cp_parser_skip_to_pragma_eol
2020 (cp_parser*, cp_token *);
2021 static bool cp_parser_error_occurred
2023 static bool cp_parser_allow_gnu_extensions_p
2025 static bool cp_parser_is_string_literal
2027 static bool cp_parser_is_keyword
2028 (cp_token *, enum rid);
2029 static tree cp_parser_make_typename_type
2030 (cp_parser *, tree, tree, location_t location);
2031 static cp_declarator * cp_parser_make_indirect_declarator
2032 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2034 /* Returns nonzero if we are parsing tentatively. */
2037 cp_parser_parsing_tentatively (cp_parser* parser)
2039 return parser->context->next != NULL;
2042 /* Returns nonzero if TOKEN is a string literal. */
2045 cp_parser_is_string_literal (cp_token* token)
2047 return (token->type == CPP_STRING ||
2048 token->type == CPP_STRING16 ||
2049 token->type == CPP_STRING32 ||
2050 token->type == CPP_WSTRING);
2053 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2056 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2058 return token->keyword == keyword;
2061 /* If not parsing tentatively, issue a diagnostic of the form
2062 FILE:LINE: MESSAGE before TOKEN
2063 where TOKEN is the next token in the input stream. MESSAGE
2064 (specified by the caller) is usually of the form "expected
2068 cp_parser_error (cp_parser* parser, const char* message)
2070 if (!cp_parser_simulate_error (parser))
2072 cp_token *token = cp_lexer_peek_token (parser->lexer);
2073 /* This diagnostic makes more sense if it is tagged to the line
2074 of the token we just peeked at. */
2075 cp_lexer_set_source_position_from_token (token);
2077 if (token->type == CPP_PRAGMA)
2079 error ("%H%<#pragma%> is not allowed here", &token->location);
2080 cp_parser_skip_to_pragma_eol (parser, token);
2084 c_parse_error (message,
2085 /* Because c_parser_error does not understand
2086 CPP_KEYWORD, keywords are treated like
2088 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2093 /* Issue an error about name-lookup failing. NAME is the
2094 IDENTIFIER_NODE DECL is the result of
2095 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2096 the thing that we hoped to find. */
2099 cp_parser_name_lookup_error (cp_parser* parser,
2102 const char* desired,
2103 location_t location)
2105 /* If name lookup completely failed, tell the user that NAME was not
2107 if (decl == error_mark_node)
2109 if (parser->scope && parser->scope != global_namespace)
2110 error ("%H%<%E::%E%> has not been declared",
2111 &location, parser->scope, name);
2112 else if (parser->scope == global_namespace)
2113 error ("%H%<::%E%> has not been declared", &location, name);
2114 else if (parser->object_scope
2115 && !CLASS_TYPE_P (parser->object_scope))
2116 error ("%Hrequest for member %qE in non-class type %qT",
2117 &location, name, parser->object_scope);
2118 else if (parser->object_scope)
2119 error ("%H%<%T::%E%> has not been declared",
2120 &location, parser->object_scope, name);
2122 error ("%H%qE has not been declared", &location, name);
2124 else if (parser->scope && parser->scope != global_namespace)
2125 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2126 else if (parser->scope == global_namespace)
2127 error ("%H%<::%E%> %s", &location, name, desired);
2129 error ("%H%qE %s", &location, name, desired);
2132 /* If we are parsing tentatively, remember that an error has occurred
2133 during this tentative parse. Returns true if the error was
2134 simulated; false if a message should be issued by the caller. */
2137 cp_parser_simulate_error (cp_parser* parser)
2139 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2141 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2147 /* Check for repeated decl-specifiers. */
2150 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2151 location_t location)
2155 for (ds = ds_first; ds != ds_last; ++ds)
2157 unsigned count = decl_specs->specs[(int)ds];
2160 /* The "long" specifier is a special case because of "long long". */
2164 error ("%H%<long long long%> is too long for GCC", &location);
2165 else if (pedantic && !in_system_header && warn_long_long
2166 && cxx_dialect == cxx98)
2167 pedwarn (location, OPT_Wlong_long,
2168 "ISO C++ 1998 does not support %<long long%>");
2172 static const char *const decl_spec_names[] = {
2188 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2193 /* This function is called when a type is defined. If type
2194 definitions are forbidden at this point, an error message is
2198 cp_parser_check_type_definition (cp_parser* parser)
2200 /* If types are forbidden here, issue a message. */
2201 if (parser->type_definition_forbidden_message)
2203 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2204 in the message need to be interpreted. */
2205 error (parser->type_definition_forbidden_message);
2211 /* This function is called when the DECLARATOR is processed. The TYPE
2212 was a type defined in the decl-specifiers. If it is invalid to
2213 define a type in the decl-specifiers for DECLARATOR, an error is
2214 issued. TYPE_LOCATION is the location of TYPE and is used
2215 for error reporting. */
2218 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2219 tree type, location_t type_location)
2221 /* [dcl.fct] forbids type definitions in return types.
2222 Unfortunately, it's not easy to know whether or not we are
2223 processing a return type until after the fact. */
2225 && (declarator->kind == cdk_pointer
2226 || declarator->kind == cdk_reference
2227 || declarator->kind == cdk_ptrmem))
2228 declarator = declarator->declarator;
2230 && declarator->kind == cdk_function)
2232 error ("%Hnew types may not be defined in a return type", &type_location);
2233 inform (type_location,
2234 "(perhaps a semicolon is missing after the definition of %qT)",
2239 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2240 "<" in any valid C++ program. If the next token is indeed "<",
2241 issue a message warning the user about what appears to be an
2242 invalid attempt to form a template-id. LOCATION is the location
2243 of the type-specifier (TYPE) */
2246 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2247 tree type, location_t location)
2249 cp_token_position start = 0;
2251 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254 error ("%H%qT is not a template", &location, type);
2255 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2256 error ("%H%qE is not a template", &location, type);
2258 error ("%Hinvalid template-id", &location);
2259 /* Remember the location of the invalid "<". */
2260 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2261 start = cp_lexer_token_position (parser->lexer, true);
2262 /* Consume the "<". */
2263 cp_lexer_consume_token (parser->lexer);
2264 /* Parse the template arguments. */
2265 cp_parser_enclosed_template_argument_list (parser);
2266 /* Permanently remove the invalid template arguments so that
2267 this error message is not issued again. */
2269 cp_lexer_purge_tokens_after (parser->lexer, start);
2273 /* If parsing an integral constant-expression, issue an error message
2274 about the fact that THING appeared and return true. Otherwise,
2275 return false. In either case, set
2276 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2279 cp_parser_non_integral_constant_expression (cp_parser *parser,
2282 parser->non_integral_constant_expression_p = true;
2283 if (parser->integral_constant_expression_p)
2285 if (!parser->allow_non_integral_constant_expression_p)
2287 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2288 in the message need to be interpreted. */
2289 char *message = concat (thing,
2290 " cannot appear in a constant-expression",
2300 /* Emit a diagnostic for an invalid type name. SCOPE is the
2301 qualifying scope (or NULL, if none) for ID. This function commits
2302 to the current active tentative parse, if any. (Otherwise, the
2303 problematic construct might be encountered again later, resulting
2304 in duplicate error messages.) LOCATION is the location of ID. */
2307 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2308 tree scope, tree id,
2309 location_t location)
2311 tree decl, old_scope;
2312 /* Try to lookup the identifier. */
2313 old_scope = parser->scope;
2314 parser->scope = scope;
2315 decl = cp_parser_lookup_name_simple (parser, id, location);
2316 parser->scope = old_scope;
2317 /* If the lookup found a template-name, it means that the user forgot
2318 to specify an argument list. Emit a useful error message. */
2319 if (TREE_CODE (decl) == TEMPLATE_DECL)
2320 error ("%Hinvalid use of template-name %qE without an argument list",
2322 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2323 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2324 else if (TREE_CODE (decl) == TYPE_DECL)
2325 /* Something like 'unsigned A a;' */
2326 error ("%Hinvalid combination of multiple type-specifiers",
2328 else if (!parser->scope)
2330 /* Issue an error message. */
2331 error ("%H%qE does not name a type", &location, id);
2332 /* If we're in a template class, it's possible that the user was
2333 referring to a type from a base class. For example:
2335 template <typename T> struct A { typedef T X; };
2336 template <typename T> struct B : public A<T> { X x; };
2338 The user should have said "typename A<T>::X". */
2339 if (processing_template_decl && current_class_type
2340 && TYPE_BINFO (current_class_type))
2344 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2348 tree base_type = BINFO_TYPE (b);
2349 if (CLASS_TYPE_P (base_type)
2350 && dependent_type_p (base_type))
2353 /* Go from a particular instantiation of the
2354 template (which will have an empty TYPE_FIELDs),
2355 to the main version. */
2356 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2357 for (field = TYPE_FIELDS (base_type);
2359 field = TREE_CHAIN (field))
2360 if (TREE_CODE (field) == TYPE_DECL
2361 && DECL_NAME (field) == id)
2364 "(perhaps %<typename %T::%E%> was intended)",
2365 BINFO_TYPE (b), id);
2374 /* Here we diagnose qualified-ids where the scope is actually correct,
2375 but the identifier does not resolve to a valid type name. */
2376 else if (parser->scope != error_mark_node)
2378 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2379 error ("%H%qE in namespace %qE does not name a type",
2380 &location, id, parser->scope);
2381 else if (TYPE_P (parser->scope))
2382 error ("%H%qE in class %qT does not name a type",
2383 &location, id, parser->scope);
2387 cp_parser_commit_to_tentative_parse (parser);
2390 /* Check for a common situation where a type-name should be present,
2391 but is not, and issue a sensible error message. Returns true if an
2392 invalid type-name was detected.
2394 The situation handled by this function are variable declarations of the
2395 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2396 Usually, `ID' should name a type, but if we got here it means that it
2397 does not. We try to emit the best possible error message depending on
2398 how exactly the id-expression looks like. */
2401 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 cp_token *token = cp_lexer_peek_token (parser->lexer);
2406 cp_parser_parse_tentatively (parser);
2407 id = cp_parser_id_expression (parser,
2408 /*template_keyword_p=*/false,
2409 /*check_dependency_p=*/true,
2410 /*template_p=*/NULL,
2411 /*declarator_p=*/true,
2412 /*optional_p=*/false);
2413 /* After the id-expression, there should be a plain identifier,
2414 otherwise this is not a simple variable declaration. Also, if
2415 the scope is dependent, we cannot do much. */
2416 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2417 || (parser->scope && TYPE_P (parser->scope)
2418 && dependent_type_p (parser->scope))
2419 || TREE_CODE (id) == TYPE_DECL)
2421 cp_parser_abort_tentative_parse (parser);
2424 if (!cp_parser_parse_definitely (parser))
2427 /* Emit a diagnostic for the invalid type. */
2428 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2429 id, token->location);
2430 /* Skip to the end of the declaration; there's no point in
2431 trying to process it. */
2432 cp_parser_skip_to_end_of_block_or_statement (parser);
2436 /* Consume tokens up to, and including, the next non-nested closing `)'.
2437 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2438 are doing error recovery. Returns -1 if OR_COMMA is true and we
2439 found an unnested comma. */
2442 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2447 unsigned paren_depth = 0;
2448 unsigned brace_depth = 0;
2450 if (recovering && !or_comma
2451 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2456 cp_token * token = cp_lexer_peek_token (parser->lexer);
2458 switch (token->type)
2461 case CPP_PRAGMA_EOL:
2462 /* If we've run out of tokens, then there is no closing `)'. */
2466 /* This matches the processing in skip_to_end_of_statement. */
2471 case CPP_OPEN_BRACE:
2474 case CPP_CLOSE_BRACE:
2480 if (recovering && or_comma && !brace_depth && !paren_depth)
2484 case CPP_OPEN_PAREN:
2489 case CPP_CLOSE_PAREN:
2490 if (!brace_depth && !paren_depth--)
2493 cp_lexer_consume_token (parser->lexer);
2502 /* Consume the token. */
2503 cp_lexer_consume_token (parser->lexer);
2507 /* Consume tokens until we reach the end of the current statement.
2508 Normally, that will be just before consuming a `;'. However, if a
2509 non-nested `}' comes first, then we stop before consuming that. */
2512 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2514 unsigned nesting_depth = 0;
2518 cp_token *token = cp_lexer_peek_token (parser->lexer);
2520 switch (token->type)
2523 case CPP_PRAGMA_EOL:
2524 /* If we've run out of tokens, stop. */
2528 /* If the next token is a `;', we have reached the end of the
2534 case CPP_CLOSE_BRACE:
2535 /* If this is a non-nested '}', stop before consuming it.
2536 That way, when confronted with something like:
2540 we stop before consuming the closing '}', even though we
2541 have not yet reached a `;'. */
2542 if (nesting_depth == 0)
2545 /* If it is the closing '}' for a block that we have
2546 scanned, stop -- but only after consuming the token.
2552 we will stop after the body of the erroneously declared
2553 function, but before consuming the following `typedef'
2555 if (--nesting_depth == 0)
2557 cp_lexer_consume_token (parser->lexer);
2561 case CPP_OPEN_BRACE:
2569 /* Consume the token. */
2570 cp_lexer_consume_token (parser->lexer);
2574 /* This function is called at the end of a statement or declaration.
2575 If the next token is a semicolon, it is consumed; otherwise, error
2576 recovery is attempted. */
2579 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2581 /* Look for the trailing `;'. */
2582 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2584 /* If there is additional (erroneous) input, skip to the end of
2586 cp_parser_skip_to_end_of_statement (parser);
2587 /* If the next token is now a `;', consume it. */
2588 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2589 cp_lexer_consume_token (parser->lexer);
2593 /* Skip tokens until we have consumed an entire block, or until we
2594 have consumed a non-nested `;'. */
2597 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2599 int nesting_depth = 0;
2601 while (nesting_depth >= 0)
2603 cp_token *token = cp_lexer_peek_token (parser->lexer);
2605 switch (token->type)
2608 case CPP_PRAGMA_EOL:
2609 /* If we've run out of tokens, stop. */
2613 /* Stop if this is an unnested ';'. */
2618 case CPP_CLOSE_BRACE:
2619 /* Stop if this is an unnested '}', or closes the outermost
2626 case CPP_OPEN_BRACE:
2635 /* Consume the token. */
2636 cp_lexer_consume_token (parser->lexer);
2640 /* Skip tokens until a non-nested closing curly brace is the next
2641 token, or there are no more tokens. Return true in the first case,
2645 cp_parser_skip_to_closing_brace (cp_parser *parser)
2647 unsigned nesting_depth = 0;
2651 cp_token *token = cp_lexer_peek_token (parser->lexer);
2653 switch (token->type)
2656 case CPP_PRAGMA_EOL:
2657 /* If we've run out of tokens, stop. */
2660 case CPP_CLOSE_BRACE:
2661 /* If the next token is a non-nested `}', then we have reached
2662 the end of the current block. */
2663 if (nesting_depth-- == 0)
2667 case CPP_OPEN_BRACE:
2668 /* If it the next token is a `{', then we are entering a new
2669 block. Consume the entire block. */
2677 /* Consume the token. */
2678 cp_lexer_consume_token (parser->lexer);
2682 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2683 parameter is the PRAGMA token, allowing us to purge the entire pragma
2687 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2691 parser->lexer->in_pragma = false;
2694 token = cp_lexer_consume_token (parser->lexer);
2695 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2697 /* Ensure that the pragma is not parsed again. */
2698 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 /* Require pragma end of line, resyncing with it as necessary. The
2702 arguments are as for cp_parser_skip_to_pragma_eol. */
2705 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2707 parser->lexer->in_pragma = false;
2708 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2709 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 /* This is a simple wrapper around make_typename_type. When the id is
2713 an unresolved identifier node, we can provide a superior diagnostic
2714 using cp_parser_diagnose_invalid_type_name. */
2717 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2718 tree id, location_t id_location)
2721 if (TREE_CODE (id) == IDENTIFIER_NODE)
2723 result = make_typename_type (scope, id, typename_type,
2724 /*complain=*/tf_none);
2725 if (result == error_mark_node)
2726 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729 return make_typename_type (scope, id, typename_type, tf_error);
2732 /* This is a wrapper around the
2733 make_{pointer,ptrmem,reference}_declarator functions that decides
2734 which one to call based on the CODE and CLASS_TYPE arguments. The
2735 CODE argument should be one of the values returned by
2736 cp_parser_ptr_operator. */
2737 static cp_declarator *
2738 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2739 cp_cv_quals cv_qualifiers,
2740 cp_declarator *target)
2742 if (code == ERROR_MARK)
2743 return cp_error_declarator;
2745 if (code == INDIRECT_REF)
2746 if (class_type == NULL_TREE)
2747 return make_pointer_declarator (cv_qualifiers, target);
2749 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2750 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2751 return make_reference_declarator (cv_qualifiers, target, false);
2752 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, true);
2757 /* Create a new C++ parser. */
2760 cp_parser_new (void)
2766 /* cp_lexer_new_main is called before calling ggc_alloc because
2767 cp_lexer_new_main might load a PCH file. */
2768 lexer = cp_lexer_new_main ();
2770 /* Initialize the binops_by_token so that we can get the tree
2771 directly from the token. */
2772 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2773 binops_by_token[binops[i].token_type] = binops[i];
2775 parser = GGC_CNEW (cp_parser);
2776 parser->lexer = lexer;
2777 parser->context = cp_parser_context_new (NULL);
2779 /* For now, we always accept GNU extensions. */
2780 parser->allow_gnu_extensions_p = 1;
2782 /* The `>' token is a greater-than operator, not the end of a
2784 parser->greater_than_is_operator_p = true;
2786 parser->default_arg_ok_p = true;
2788 /* We are not parsing a constant-expression. */
2789 parser->integral_constant_expression_p = false;
2790 parser->allow_non_integral_constant_expression_p = false;
2791 parser->non_integral_constant_expression_p = false;
2793 /* Local variable names are not forbidden. */
2794 parser->local_variables_forbidden_p = false;
2796 /* We are not processing an `extern "C"' declaration. */
2797 parser->in_unbraced_linkage_specification_p = false;
2799 /* We are not processing a declarator. */
2800 parser->in_declarator_p = false;
2802 /* We are not processing a template-argument-list. */
2803 parser->in_template_argument_list_p = false;
2805 /* We are not in an iteration statement. */
2806 parser->in_statement = 0;
2808 /* We are not in a switch statement. */
2809 parser->in_switch_statement_p = false;
2811 /* We are not parsing a type-id inside an expression. */
2812 parser->in_type_id_in_expr_p = false;
2814 /* Declarations aren't implicitly extern "C". */
2815 parser->implicit_extern_c = false;
2817 /* String literals should be translated to the execution character set. */
2818 parser->translate_strings_p = true;
2820 /* We are not parsing a function body. */
2821 parser->in_function_body = false;
2823 /* The unparsed function queue is empty. */
2824 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2826 /* There are no classes being defined. */
2827 parser->num_classes_being_defined = 0;
2829 /* No template parameters apply. */
2830 parser->num_template_parameter_lists = 0;
2835 /* Create a cp_lexer structure which will emit the tokens in CACHE
2836 and push it onto the parser's lexer stack. This is used for delayed
2837 parsing of in-class method bodies and default arguments, and should
2838 not be confused with tentative parsing. */
2840 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2842 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2843 lexer->next = parser->lexer;
2844 parser->lexer = lexer;
2846 /* Move the current source position to that of the first token in the
2848 cp_lexer_set_source_position_from_token (lexer->next_token);
2851 /* Pop the top lexer off the parser stack. This is never used for the
2852 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2854 cp_parser_pop_lexer (cp_parser *parser)
2856 cp_lexer *lexer = parser->lexer;
2857 parser->lexer = lexer->next;
2858 cp_lexer_destroy (lexer);
2860 /* Put the current source position back where it was before this
2861 lexer was pushed. */
2862 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 /* Lexical conventions [gram.lex] */
2867 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2871 cp_parser_identifier (cp_parser* parser)
2875 /* Look for the identifier. */
2876 token = cp_parser_require (parser, CPP_NAME, "identifier");
2877 /* Return the value. */
2878 return token ? token->u.value : error_mark_node;
2881 /* Parse a sequence of adjacent string constants. Returns a
2882 TREE_STRING representing the combined, nul-terminated string
2883 constant. If TRANSLATE is true, translate the string to the
2884 execution character set. If WIDE_OK is true, a wide string is
2887 C++98 [lex.string] says that if a narrow string literal token is
2888 adjacent to a wide string literal token, the behavior is undefined.
2889 However, C99 6.4.5p4 says that this results in a wide string literal.
2890 We follow C99 here, for consistency with the C front end.
2892 This code is largely lifted from lex_string() in c-lex.c.
2894 FUTURE: ObjC++ will need to handle @-strings here. */
2896 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2900 struct obstack str_ob;
2901 cpp_string str, istr, *strs;
2903 enum cpp_ttype type;
2905 tok = cp_lexer_peek_token (parser->lexer);
2906 if (!cp_parser_is_string_literal (tok))
2908 cp_parser_error (parser, "expected string-literal");
2909 return error_mark_node;
2914 /* Try to avoid the overhead of creating and destroying an obstack
2915 for the common case of just one string. */
2916 if (!cp_parser_is_string_literal
2917 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2919 cp_lexer_consume_token (parser->lexer);
2921 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2922 str.len = TREE_STRING_LENGTH (tok->u.value);
2929 gcc_obstack_init (&str_ob);
2934 cp_lexer_consume_token (parser->lexer);
2936 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2937 str.len = TREE_STRING_LENGTH (tok->u.value);
2939 if (type != tok->type)
2941 if (type == CPP_STRING)
2943 else if (tok->type != CPP_STRING)
2944 error ("%Hunsupported non-standard concatenation "
2945 "of string literals", &tok->location);
2948 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2950 tok = cp_lexer_peek_token (parser->lexer);
2952 while (cp_parser_is_string_literal (tok));
2954 strs = (cpp_string *) obstack_finish (&str_ob);
2957 if (type != CPP_STRING && !wide_ok)
2959 cp_parser_error (parser, "a wide string is invalid in this context");
2963 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2964 (parse_in, strs, count, &istr, type))
2966 value = build_string (istr.len, (const char *)istr.text);
2967 free (CONST_CAST (unsigned char *, istr.text));
2973 TREE_TYPE (value) = char_array_type_node;
2976 TREE_TYPE (value) = char16_array_type_node;
2979 TREE_TYPE (value) = char32_array_type_node;
2982 TREE_TYPE (value) = wchar_array_type_node;
2986 value = fix_string_type (value);
2989 /* cpp_interpret_string has issued an error. */
2990 value = error_mark_node;
2993 obstack_free (&str_ob, 0);
2999 /* Basic concepts [gram.basic] */
3001 /* Parse a translation-unit.
3004 declaration-seq [opt]
3006 Returns TRUE if all went well. */
3009 cp_parser_translation_unit (cp_parser* parser)
3011 /* The address of the first non-permanent object on the declarator
3013 static void *declarator_obstack_base;
3017 /* Create the declarator obstack, if necessary. */
3018 if (!cp_error_declarator)
3020 gcc_obstack_init (&declarator_obstack);
3021 /* Create the error declarator. */
3022 cp_error_declarator = make_declarator (cdk_error);
3023 /* Create the empty parameter list. */
3024 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3025 /* Remember where the base of the declarator obstack lies. */
3026 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029 cp_parser_declaration_seq_opt (parser);
3031 /* If there are no tokens left then all went well. */
3032 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3034 /* Get rid of the token array; we don't need it any more. */
3035 cp_lexer_destroy (parser->lexer);
3036 parser->lexer = NULL;
3038 /* This file might have been a context that's implicitly extern
3039 "C". If so, pop the lang context. (Only relevant for PCH.) */
3040 if (parser->implicit_extern_c)
3042 pop_lang_context ();
3043 parser->implicit_extern_c = false;
3047 finish_translation_unit ();
3053 cp_parser_error (parser, "expected declaration");
3057 /* Make sure the declarator obstack was fully cleaned up. */
3058 gcc_assert (obstack_next_free (&declarator_obstack)
3059 == declarator_obstack_base);
3061 /* All went well. */
3065 /* Expressions [gram.expr] */
3067 /* Parse a primary-expression.
3078 ( compound-statement )
3079 __builtin_va_arg ( assignment-expression , type-id )
3080 __builtin_offsetof ( type-id , offsetof-expression )
3083 __has_nothrow_assign ( type-id )
3084 __has_nothrow_constructor ( type-id )
3085 __has_nothrow_copy ( type-id )
3086 __has_trivial_assign ( type-id )
3087 __has_trivial_constructor ( type-id )
3088 __has_trivial_copy ( type-id )
3089 __has_trivial_destructor ( type-id )
3090 __has_virtual_destructor ( type-id )
3091 __is_abstract ( type-id )
3092 __is_base_of ( type-id , type-id )
3093 __is_class ( type-id )
3094 __is_convertible_to ( type-id , type-id )
3095 __is_empty ( type-id )
3096 __is_enum ( type-id )
3097 __is_pod ( type-id )
3098 __is_polymorphic ( type-id )
3099 __is_union ( type-id )
3101 Objective-C++ Extension:
3109 ADDRESS_P is true iff this expression was immediately preceded by
3110 "&" and therefore might denote a pointer-to-member. CAST_P is true
3111 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3112 true iff this expression is a template argument.
3114 Returns a representation of the expression. Upon return, *IDK
3115 indicates what kind of id-expression (if any) was present. */
3118 cp_parser_primary_expression (cp_parser *parser,
3121 bool template_arg_p,
3124 cp_token *token = NULL;
3126 /* Assume the primary expression is not an id-expression. */
3127 *idk = CP_ID_KIND_NONE;
3129 /* Peek at the next token. */
3130 token = cp_lexer_peek_token (parser->lexer);
3131 switch (token->type)
3144 token = cp_lexer_consume_token (parser->lexer);
3145 if (TREE_CODE (token->u.value) == FIXED_CST)
3147 error ("%Hfixed-point types not supported in C++",
3149 return error_mark_node;
3151 /* Floating-point literals are only allowed in an integral
3152 constant expression if they are cast to an integral or
3153 enumeration type. */
3154 if (TREE_CODE (token->u.value) == REAL_CST
3155 && parser->integral_constant_expression_p
3158 /* CAST_P will be set even in invalid code like "int(2.7 +
3159 ...)". Therefore, we have to check that the next token
3160 is sure to end the cast. */
3163 cp_token *next_token;
3165 next_token = cp_lexer_peek_token (parser->lexer);
3166 if (/* The comma at the end of an
3167 enumerator-definition. */
3168 next_token->type != CPP_COMMA
3169 /* The curly brace at the end of an enum-specifier. */
3170 && next_token->type != CPP_CLOSE_BRACE
3171 /* The end of a statement. */
3172 && next_token->type != CPP_SEMICOLON
3173 /* The end of the cast-expression. */
3174 && next_token->type != CPP_CLOSE_PAREN
3175 /* The end of an array bound. */
3176 && next_token->type != CPP_CLOSE_SQUARE
3177 /* The closing ">" in a template-argument-list. */
3178 && (next_token->type != CPP_GREATER
3179 || parser->greater_than_is_operator_p)
3180 /* C++0x only: A ">>" treated like two ">" tokens,
3181 in a template-argument-list. */
3182 && (next_token->type != CPP_RSHIFT
3183 || (cxx_dialect == cxx98)
3184 || parser->greater_than_is_operator_p))
3188 /* If we are within a cast, then the constraint that the
3189 cast is to an integral or enumeration type will be
3190 checked at that point. If we are not within a cast, then
3191 this code is invalid. */
3193 cp_parser_non_integral_constant_expression
3194 (parser, "floating-point literal");
3196 return token->u.value;
3202 /* ??? Should wide strings be allowed when parser->translate_strings_p
3203 is false (i.e. in attributes)? If not, we can kill the third
3204 argument to cp_parser_string_literal. */
3205 return cp_parser_string_literal (parser,
3206 parser->translate_strings_p,
3209 case CPP_OPEN_PAREN:
3212 bool saved_greater_than_is_operator_p;
3214 /* Consume the `('. */
3215 cp_lexer_consume_token (parser->lexer);
3216 /* Within a parenthesized expression, a `>' token is always
3217 the greater-than operator. */
3218 saved_greater_than_is_operator_p
3219 = parser->greater_than_is_operator_p;
3220 parser->greater_than_is_operator_p = true;
3221 /* If we see `( { ' then we are looking at the beginning of
3222 a GNU statement-expression. */
3223 if (cp_parser_allow_gnu_extensions_p (parser)
3224 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3226 /* Statement-expressions are not allowed by the standard. */
3227 pedwarn (token->location, OPT_pedantic,
3228 "ISO C++ forbids braced-groups within expressions");
3230 /* And they're not allowed outside of a function-body; you
3231 cannot, for example, write:
3233 int i = ({ int j = 3; j + 1; });
3235 at class or namespace scope. */
3236 if (!parser->in_function_body
3237 || parser->in_template_argument_list_p)
3239 error ("%Hstatement-expressions are not allowed outside "
3240 "functions nor in template-argument lists",
3242 cp_parser_skip_to_end_of_block_or_statement (parser);
3243 expr = error_mark_node;
3247 /* Start the statement-expression. */
3248 expr = begin_stmt_expr ();
3249 /* Parse the compound-statement. */
3250 cp_parser_compound_statement (parser, expr, false);
3252 expr = finish_stmt_expr (expr, false);
3257 /* Parse the parenthesized expression. */
3258 expr = cp_parser_expression (parser, cast_p, idk);
3259 /* Let the front end know that this expression was
3260 enclosed in parentheses. This matters in case, for
3261 example, the expression is of the form `A::B', since
3262 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3264 finish_parenthesized_expr (expr);
3266 /* The `>' token might be the end of a template-id or
3267 template-parameter-list now. */
3268 parser->greater_than_is_operator_p
3269 = saved_greater_than_is_operator_p;
3270 /* Consume the `)'. */
3271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3272 cp_parser_skip_to_end_of_statement (parser);
3278 switch (token->keyword)
3280 /* These two are the boolean literals. */
3282 cp_lexer_consume_token (parser->lexer);
3283 return boolean_true_node;
3285 cp_lexer_consume_token (parser->lexer);
3286 return boolean_false_node;
3288 /* The `__null' literal. */
3290 cp_lexer_consume_token (parser->lexer);
3293 /* Recognize the `this' keyword. */
3295 cp_lexer_consume_token (parser->lexer);
3296 if (parser->local_variables_forbidden_p)
3298 error ("%H%<this%> may not be used in this context",
3300 return error_mark_node;
3302 /* Pointers cannot appear in constant-expressions. */
3303 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3304 return error_mark_node;
3305 return finish_this_expr ();
3307 /* The `operator' keyword can be the beginning of an
3312 case RID_FUNCTION_NAME:
3313 case RID_PRETTY_FUNCTION_NAME:
3314 case RID_C99_FUNCTION_NAME:
3318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3319 __func__ are the names of variables -- but they are
3320 treated specially. Therefore, they are handled here,
3321 rather than relying on the generic id-expression logic
3322 below. Grammatically, these names are id-expressions.
3324 Consume the token. */
3325 token = cp_lexer_consume_token (parser->lexer);
3327 switch (token->keyword)
3329 case RID_FUNCTION_NAME:
3330 name = "%<__FUNCTION__%>";
3332 case RID_PRETTY_FUNCTION_NAME:
3333 name = "%<__PRETTY_FUNCTION__%>";
3335 case RID_C99_FUNCTION_NAME:
3336 name = "%<__func__%>";
3342 if (cp_parser_non_integral_constant_expression (parser, name))
3343 return error_mark_node;
3345 /* Look up the name. */
3346 return finish_fname (token->u.value);
3354 /* The `__builtin_va_arg' construct is used to handle
3355 `va_arg'. Consume the `__builtin_va_arg' token. */
3356 cp_lexer_consume_token (parser->lexer);
3357 /* Look for the opening `('. */
3358 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3359 /* Now, parse the assignment-expression. */
3360 expression = cp_parser_assignment_expression (parser,
3361 /*cast_p=*/false, NULL);
3362 /* Look for the `,'. */
3363 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3364 /* Parse the type-id. */
3365 type = cp_parser_type_id (parser);
3366 /* Look for the closing `)'. */
3367 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3368 /* Using `va_arg' in a constant-expression is not
3370 if (cp_parser_non_integral_constant_expression (parser,
3372 return error_mark_node;
3373 return build_x_va_arg (expression, type);
3377 return cp_parser_builtin_offsetof (parser);
3379 case RID_HAS_NOTHROW_ASSIGN:
3380 case RID_HAS_NOTHROW_CONSTRUCTOR:
3381 case RID_HAS_NOTHROW_COPY:
3382 case RID_HAS_TRIVIAL_ASSIGN:
3383 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3384 case RID_HAS_TRIVIAL_COPY:
3385 case RID_HAS_TRIVIAL_DESTRUCTOR:
3386 case RID_HAS_VIRTUAL_DESTRUCTOR:
3387 case RID_IS_ABSTRACT:
3388 case RID_IS_BASE_OF:
3390 case RID_IS_CONVERTIBLE_TO:
3394 case RID_IS_POLYMORPHIC:
3396 return cp_parser_trait_expr (parser, token->keyword);
3398 /* Objective-C++ expressions. */
3400 case RID_AT_PROTOCOL:
3401 case RID_AT_SELECTOR:
3402 return cp_parser_objc_expression (parser);
3405 cp_parser_error (parser, "expected primary-expression");
3406 return error_mark_node;
3409 /* An id-expression can start with either an identifier, a
3410 `::' as the beginning of a qualified-id, or the "operator"
3414 case CPP_TEMPLATE_ID:
3415 case CPP_NESTED_NAME_SPECIFIER:
3419 const char *error_msg;
3422 cp_token *id_expr_token;
3425 /* Parse the id-expression. */
3427 = cp_parser_id_expression (parser,
3428 /*template_keyword_p=*/false,
3429 /*check_dependency_p=*/true,
3431 /*declarator_p=*/false,
3432 /*optional_p=*/false);
3433 if (id_expression == error_mark_node)
3434 return error_mark_node;
3435 id_expr_token = token;
3436 token = cp_lexer_peek_token (parser->lexer);
3437 done = (token->type != CPP_OPEN_SQUARE
3438 && token->type != CPP_OPEN_PAREN
3439 && token->type != CPP_DOT
3440 && token->type != CPP_DEREF
3441 && token->type != CPP_PLUS_PLUS
3442 && token->type != CPP_MINUS_MINUS);
3443 /* If we have a template-id, then no further lookup is
3444 required. If the template-id was for a template-class, we
3445 will sometimes have a TYPE_DECL at this point. */
3446 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3447 || TREE_CODE (id_expression) == TYPE_DECL)
3448 decl = id_expression;
3449 /* Look up the name. */
3452 tree ambiguous_decls;
3454 decl = cp_parser_lookup_name (parser, id_expression,
3457 /*is_namespace=*/false,
3458 /*check_dependency=*/true,
3460 id_expr_token->location);
3461 /* If the lookup was ambiguous, an error will already have
3463 if (ambiguous_decls)
3464 return error_mark_node;
3466 /* In Objective-C++, an instance variable (ivar) may be preferred
3467 to whatever cp_parser_lookup_name() found. */
3468 decl = objc_lookup_ivar (decl, id_expression);
3470 /* If name lookup gives us a SCOPE_REF, then the
3471 qualifying scope was dependent. */
3472 if (TREE_CODE (decl) == SCOPE_REF)
3474 /* At this point, we do not know if DECL is a valid
3475 integral constant expression. We assume that it is
3476 in fact such an expression, so that code like:
3478 template <int N> struct A {
3482 is accepted. At template-instantiation time, we
3483 will check that B<N>::i is actually a constant. */
3486 /* Check to see if DECL is a local variable in a context
3487 where that is forbidden. */
3488 if (parser->local_variables_forbidden_p
3489 && local_variable_p (decl))
3491 /* It might be that we only found DECL because we are
3492 trying to be generous with pre-ISO scoping rules.
3493 For example, consider:
3497 for (int i = 0; i < 10; ++i) {}
3498 extern void f(int j = i);
3501 Here, name look up will originally find the out
3502 of scope `i'. We need to issue a warning message,
3503 but then use the global `i'. */
3504 decl = check_for_out_of_scope_variable (decl);
3505 if (local_variable_p (decl))
3507 error ("%Hlocal variable %qD may not appear in this context",
3508 &id_expr_token->location, decl);
3509 return error_mark_node;
3514 decl = (finish_id_expression
3515 (id_expression, decl, parser->scope,
3517 parser->integral_constant_expression_p,
3518 parser->allow_non_integral_constant_expression_p,
3519 &parser->non_integral_constant_expression_p,
3520 template_p, done, address_p,
3523 id_expr_token->location));
3525 cp_parser_error (parser, error_msg);
3529 /* Anything else is an error. */
3531 /* ...unless we have an Objective-C++ message or string literal,
3533 if (c_dialect_objc ()
3534 && (token->type == CPP_OPEN_SQUARE
3535 || token->type == CPP_OBJC_STRING))
3536 return cp_parser_objc_expression (parser);
3538 cp_parser_error (parser, "expected primary-expression");
3539 return error_mark_node;
3543 /* Parse an id-expression.
3550 :: [opt] nested-name-specifier template [opt] unqualified-id
3552 :: operator-function-id
3555 Return a representation of the unqualified portion of the
3556 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3557 a `::' or nested-name-specifier.
3559 Often, if the id-expression was a qualified-id, the caller will
3560 want to make a SCOPE_REF to represent the qualified-id. This
3561 function does not do this in order to avoid wastefully creating
3562 SCOPE_REFs when they are not required.
3564 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3567 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3568 uninstantiated templates.
3570 If *TEMPLATE_P is non-NULL, it is set to true iff the
3571 `template' keyword is used to explicitly indicate that the entity
3572 named is a template.
3574 If DECLARATOR_P is true, the id-expression is appearing as part of
3575 a declarator, rather than as part of an expression. */
3578 cp_parser_id_expression (cp_parser *parser,
3579 bool template_keyword_p,
3580 bool check_dependency_p,
3585 bool global_scope_p;
3586 bool nested_name_specifier_p;
3588 /* Assume the `template' keyword was not used. */
3590 *template_p = template_keyword_p;
3592 /* Look for the optional `::' operator. */
3594 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3596 /* Look for the optional nested-name-specifier. */
3597 nested_name_specifier_p
3598 = (cp_parser_nested_name_specifier_opt (parser,
3599 /*typename_keyword_p=*/false,
3604 /* If there is a nested-name-specifier, then we are looking at
3605 the first qualified-id production. */
3606 if (nested_name_specifier_p)
3609 tree saved_object_scope;
3610 tree saved_qualifying_scope;
3611 tree unqualified_id;
3614 /* See if the next token is the `template' keyword. */
3616 template_p = &is_template;
3617 *template_p = cp_parser_optional_template_keyword (parser);
3618 /* Name lookup we do during the processing of the
3619 unqualified-id might obliterate SCOPE. */
3620 saved_scope = parser->scope;
3621 saved_object_scope = parser->object_scope;
3622 saved_qualifying_scope = parser->qualifying_scope;
3623 /* Process the final unqualified-id. */
3624 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3627 /*optional_p=*/false);
3628 /* Restore the SAVED_SCOPE for our caller. */
3629 parser->scope = saved_scope;
3630 parser->object_scope = saved_object_scope;
3631 parser->qualifying_scope = saved_qualifying_scope;
3633 return unqualified_id;
3635 /* Otherwise, if we are in global scope, then we are looking at one
3636 of the other qualified-id productions. */
3637 else if (global_scope_p)
3642 /* Peek at the next token. */
3643 token = cp_lexer_peek_token (parser->lexer);
3645 /* If it's an identifier, and the next token is not a "<", then
3646 we can avoid the template-id case. This is an optimization
3647 for this common case. */
3648 if (token->type == CPP_NAME
3649 && !cp_parser_nth_token_starts_template_argument_list_p
3651 return cp_parser_identifier (parser);
3653 cp_parser_parse_tentatively (parser);
3654 /* Try a template-id. */
3655 id = cp_parser_template_id (parser,
3656 /*template_keyword_p=*/false,
3657 /*check_dependency_p=*/true,
3659 /* If that worked, we're done. */
3660 if (cp_parser_parse_definitely (parser))
3663 /* Peek at the next token. (Changes in the token buffer may
3664 have invalidated the pointer obtained above.) */
3665 token = cp_lexer_peek_token (parser->lexer);
3667 switch (token->type)
3670 return cp_parser_identifier (parser);
3673 if (token->keyword == RID_OPERATOR)
3674 return cp_parser_operator_function_id (parser);
3678 cp_parser_error (parser, "expected id-expression");
3679 return error_mark_node;
3683 return cp_parser_unqualified_id (parser, template_keyword_p,
3684 /*check_dependency_p=*/true,
3689 /* Parse an unqualified-id.
3693 operator-function-id
3694 conversion-function-id
3698 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3699 keyword, in a construct like `A::template ...'.
3701 Returns a representation of unqualified-id. For the `identifier'
3702 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3703 production a BIT_NOT_EXPR is returned; the operand of the
3704 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3705 other productions, see the documentation accompanying the
3706 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3707 names are looked up in uninstantiated templates. If DECLARATOR_P
3708 is true, the unqualified-id is appearing as part of a declarator,
3709 rather than as part of an expression. */
3712 cp_parser_unqualified_id (cp_parser* parser,
3713 bool template_keyword_p,
3714 bool check_dependency_p,
3720 /* Peek at the next token. */
3721 token = cp_lexer_peek_token (parser->lexer);
3723 switch (token->type)
3729 /* We don't know yet whether or not this will be a
3731 cp_parser_parse_tentatively (parser);
3732 /* Try a template-id. */
3733 id = cp_parser_template_id (parser, template_keyword_p,
3736 /* If it worked, we're done. */
3737 if (cp_parser_parse_definitely (parser))
3739 /* Otherwise, it's an ordinary identifier. */
3740 return cp_parser_identifier (parser);
3743 case CPP_TEMPLATE_ID:
3744 return cp_parser_template_id (parser, template_keyword_p,
3751 tree qualifying_scope;
3756 /* Consume the `~' token. */
3757 cp_lexer_consume_token (parser->lexer);
3758 /* Parse the class-name. The standard, as written, seems to
3761 template <typename T> struct S { ~S (); };
3762 template <typename T> S<T>::~S() {}
3764 is invalid, since `~' must be followed by a class-name, but
3765 `S<T>' is dependent, and so not known to be a class.
3766 That's not right; we need to look in uninstantiated
3767 templates. A further complication arises from:
3769 template <typename T> void f(T t) {
3773 Here, it is not possible to look up `T' in the scope of `T'
3774 itself. We must look in both the current scope, and the
3775 scope of the containing complete expression.
3777 Yet another issue is:
3786 The standard does not seem to say that the `S' in `~S'
3787 should refer to the type `S' and not the data member
3790 /* DR 244 says that we look up the name after the "~" in the
3791 same scope as we looked up the qualifying name. That idea
3792 isn't fully worked out; it's more complicated than that. */
3793 scope = parser->scope;
3794 object_scope = parser->object_scope;
3795 qualifying_scope = parser->qualifying_scope;
3797 /* Check for invalid scopes. */
3798 if (scope == error_mark_node)
3800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3801 cp_lexer_consume_token (parser->lexer);
3802 return error_mark_node;
3804 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3806 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3807 error ("%Hscope %qT before %<~%> is not a class-name",
3808 &token->location, scope);
3809 cp_parser_simulate_error (parser);
3810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3811 cp_lexer_consume_token (parser->lexer);
3812 return error_mark_node;
3814 gcc_assert (!scope || TYPE_P (scope));
3816 /* If the name is of the form "X::~X" it's OK. */
3817 token = cp_lexer_peek_token (parser->lexer);
3819 && token->type == CPP_NAME
3820 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3822 && constructor_name_p (token->u.value, scope))
3824 cp_lexer_consume_token (parser->lexer);
3825 return build_nt (BIT_NOT_EXPR, scope);
3828 /* If there was an explicit qualification (S::~T), first look
3829 in the scope given by the qualification (i.e., S). */
3831 type_decl = NULL_TREE;
3834 cp_parser_parse_tentatively (parser);
3835 type_decl = cp_parser_class_name (parser,
3836 /*typename_keyword_p=*/false,
3837 /*template_keyword_p=*/false,
3839 /*check_dependency=*/false,
3840 /*class_head_p=*/false,
3842 if (cp_parser_parse_definitely (parser))
3845 /* In "N::S::~S", look in "N" as well. */
3846 if (!done && scope && qualifying_scope)
3848 cp_parser_parse_tentatively (parser);
3849 parser->scope = qualifying_scope;
3850 parser->object_scope = NULL_TREE;
3851 parser->qualifying_scope = NULL_TREE;
3853 = cp_parser_class_name (parser,
3854 /*typename_keyword_p=*/false,
3855 /*template_keyword_p=*/false,
3857 /*check_dependency=*/false,
3858 /*class_head_p=*/false,
3860 if (cp_parser_parse_definitely (parser))
3863 /* In "p->S::~T", look in the scope given by "*p" as well. */
3864 else if (!done && object_scope)
3866 cp_parser_parse_tentatively (parser);
3867 parser->scope = object_scope;
3868 parser->object_scope = NULL_TREE;
3869 parser->qualifying_scope = NULL_TREE;
3871 = cp_parser_class_name (parser,
3872 /*typename_keyword_p=*/false,
3873 /*template_keyword_p=*/false,
3875 /*check_dependency=*/false,
3876 /*class_head_p=*/false,
3878 if (cp_parser_parse_definitely (parser))
3881 /* Look in the surrounding context. */
3884 parser->scope = NULL_TREE;
3885 parser->object_scope = NULL_TREE;
3886 parser->qualifying_scope = NULL_TREE;
3887 if (processing_template_decl)
3888 cp_parser_parse_tentatively (parser);
3890 = cp_parser_class_name (parser,
3891 /*typename_keyword_p=*/false,
3892 /*template_keyword_p=*/false,
3894 /*check_dependency=*/false,
3895 /*class_head_p=*/false,
3897 if (processing_template_decl
3898 && ! cp_parser_parse_definitely (parser))
3900 /* We couldn't find a type with this name, so just accept
3901 it and check for a match at instantiation time. */
3902 type_decl = cp_parser_identifier (parser);
3903 if (type_decl != error_mark_node)
3904 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3908 /* If an error occurred, assume that the name of the
3909 destructor is the same as the name of the qualifying
3910 class. That allows us to keep parsing after running
3911 into ill-formed destructor names. */
3912 if (type_decl == error_mark_node && scope)
3913 return build_nt (BIT_NOT_EXPR, scope);
3914 else if (type_decl == error_mark_node)
3915 return error_mark_node;
3917 /* Check that destructor name and scope match. */
3918 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3921 error ("%Hdeclaration of %<~%T%> as member of %qT",
3922 &token->location, type_decl, scope);
3923 cp_parser_simulate_error (parser);
3924 return error_mark_node;
3929 A typedef-name that names a class shall not be used as the
3930 identifier in the declarator for a destructor declaration. */
3932 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3933 && !DECL_SELF_REFERENCE_P (type_decl)
3934 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3935 error ("%Htypedef-name %qD used as destructor declarator",
3936 &token->location, type_decl);
3938 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3942 if (token->keyword == RID_OPERATOR)
3946 /* This could be a template-id, so we try that first. */
3947 cp_parser_parse_tentatively (parser);
3948 /* Try a template-id. */
3949 id = cp_parser_template_id (parser, template_keyword_p,
3950 /*check_dependency_p=*/true,
3952 /* If that worked, we're done. */
3953 if (cp_parser_parse_definitely (parser))
3955 /* We still don't know whether we're looking at an
3956 operator-function-id or a conversion-function-id. */
3957 cp_parser_parse_tentatively (parser);
3958 /* Try an operator-function-id. */
3959 id = cp_parser_operator_function_id (parser);
3960 /* If that didn't work, try a conversion-function-id. */
3961 if (!cp_parser_parse_definitely (parser))
3962 id = cp_parser_conversion_function_id (parser);
3971 cp_parser_error (parser, "expected unqualified-id");
3972 return error_mark_node;
3976 /* Parse an (optional) nested-name-specifier.
3978 nested-name-specifier: [C++98]
3979 class-or-namespace-name :: nested-name-specifier [opt]
3980 class-or-namespace-name :: template nested-name-specifier [opt]
3982 nested-name-specifier: [C++0x]
3985 nested-name-specifier identifier ::
3986 nested-name-specifier template [opt] simple-template-id ::
3988 PARSER->SCOPE should be set appropriately before this function is
3989 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3990 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3993 Sets PARSER->SCOPE to the class (TYPE) or namespace
3994 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3995 it unchanged if there is no nested-name-specifier. Returns the new
3996 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3998 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3999 part of a declaration and/or decl-specifier. */
4002 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4003 bool typename_keyword_p,
4004 bool check_dependency_p,
4006 bool is_declaration)
4008 bool success = false;
4009 cp_token_position start = 0;
4012 /* Remember where the nested-name-specifier starts. */
4013 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4015 start = cp_lexer_token_position (parser->lexer, false);
4016 push_deferring_access_checks (dk_deferred);
4023 tree saved_qualifying_scope;
4024 bool template_keyword_p;
4026 /* Spot cases that cannot be the beginning of a
4027 nested-name-specifier. */
4028 token = cp_lexer_peek_token (parser->lexer);
4030 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4031 the already parsed nested-name-specifier. */
4032 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4034 /* Grab the nested-name-specifier and continue the loop. */
4035 cp_parser_pre_parsed_nested_name_specifier (parser);
4036 /* If we originally encountered this nested-name-specifier
4037 with IS_DECLARATION set to false, we will not have
4038 resolved TYPENAME_TYPEs, so we must do so here. */
4040 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4042 new_scope = resolve_typename_type (parser->scope,
4043 /*only_current_p=*/false);
4044 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4045 parser->scope = new_scope;
4051 /* Spot cases that cannot be the beginning of a
4052 nested-name-specifier. On the second and subsequent times
4053 through the loop, we look for the `template' keyword. */
4054 if (success && token->keyword == RID_TEMPLATE)
4056 /* A template-id can start a nested-name-specifier. */
4057 else if (token->type == CPP_TEMPLATE_ID)
4061 /* If the next token is not an identifier, then it is
4062 definitely not a type-name or namespace-name. */
4063 if (token->type != CPP_NAME)
4065 /* If the following token is neither a `<' (to begin a
4066 template-id), nor a `::', then we are not looking at a
4067 nested-name-specifier. */
4068 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4069 if (token->type != CPP_SCOPE
4070 && !cp_parser_nth_token_starts_template_argument_list_p
4075 /* The nested-name-specifier is optional, so we parse
4077 cp_parser_parse_tentatively (parser);
4079 /* Look for the optional `template' keyword, if this isn't the
4080 first time through the loop. */
4082 template_keyword_p = cp_parser_optional_template_keyword (parser);
4084 template_keyword_p = false;
4086 /* Save the old scope since the name lookup we are about to do
4087 might destroy it. */
4088 old_scope = parser->scope;
4089 saved_qualifying_scope = parser->qualifying_scope;
4090 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4091 look up names in "X<T>::I" in order to determine that "Y" is
4092 a template. So, if we have a typename at this point, we make
4093 an effort to look through it. */
4095 && !typename_keyword_p
4097 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4098 parser->scope = resolve_typename_type (parser->scope,
4099 /*only_current_p=*/false);
4100 /* Parse the qualifying entity. */
4102 = cp_parser_qualifying_entity (parser,
4108 /* Look for the `::' token. */
4109 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4111 /* If we found what we wanted, we keep going; otherwise, we're
4113 if (!cp_parser_parse_definitely (parser))
4115 bool error_p = false;
4117 /* Restore the OLD_SCOPE since it was valid before the
4118 failed attempt at finding the last
4119 class-or-namespace-name. */
4120 parser->scope = old_scope;
4121 parser->qualifying_scope = saved_qualifying_scope;
4122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4124 /* If the next token is an identifier, and the one after
4125 that is a `::', then any valid interpretation would have
4126 found a class-or-namespace-name. */
4127 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4128 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4130 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4133 token = cp_lexer_consume_token (parser->lexer);
4136 if (!token->ambiguous_p)
4139 tree ambiguous_decls;
4141 decl = cp_parser_lookup_name (parser, token->u.value,
4143 /*is_template=*/false,
4144 /*is_namespace=*/false,
4145 /*check_dependency=*/true,
4148 if (TREE_CODE (decl) == TEMPLATE_DECL)
4149 error ("%H%qD used without template parameters",
4150 &token->location, decl);
4151 else if (ambiguous_decls)
4153 error ("%Hreference to %qD is ambiguous",
4154 &token->location, token->u.value);
4155 print_candidates (ambiguous_decls);
4156 decl = error_mark_node;
4160 const char* msg = "is not a class or namespace";
4161 if (cxx_dialect != cxx98)
4162 msg = "is not a class, namespace, or enumeration";
4163 cp_parser_name_lookup_error
4164 (parser, token->u.value, decl, msg,
4168 parser->scope = error_mark_node;
4170 /* Treat this as a successful nested-name-specifier
4175 If the name found is not a class-name (clause
4176 _class_) or namespace-name (_namespace.def_), the
4177 program is ill-formed. */
4180 cp_lexer_consume_token (parser->lexer);
4184 /* We've found one valid nested-name-specifier. */
4186 /* Name lookup always gives us a DECL. */
4187 if (TREE_CODE (new_scope) == TYPE_DECL)
4188 new_scope = TREE_TYPE (new_scope);
4189 /* Uses of "template" must be followed by actual templates. */
4190 if (template_keyword_p
4191 && !(CLASS_TYPE_P (new_scope)
4192 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4193 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4194 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4195 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4196 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4197 == TEMPLATE_ID_EXPR)))
4198 permerror (input_location, TYPE_P (new_scope)
4199 ? "%qT is not a template"
4200 : "%qD is not a template",
4202 /* If it is a class scope, try to complete it; we are about to
4203 be looking up names inside the class. */
4204 if (TYPE_P (new_scope)
4205 /* Since checking types for dependency can be expensive,
4206 avoid doing it if the type is already complete. */
4207 && !COMPLETE_TYPE_P (new_scope)
4208 /* Do not try to complete dependent types. */
4209 && !dependent_type_p (new_scope))
4211 new_scope = complete_type (new_scope);
4212 /* If it is a typedef to current class, use the current
4213 class instead, as the typedef won't have any names inside
4215 if (!COMPLETE_TYPE_P (new_scope)
4216 && currently_open_class (new_scope))
4217 new_scope = TYPE_MAIN_VARIANT (new_scope);
4219 /* Make sure we look in the right scope the next time through
4221 parser->scope = new_scope;
4224 /* If parsing tentatively, replace the sequence of tokens that makes
4225 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4226 token. That way, should we re-parse the token stream, we will
4227 not have to repeat the effort required to do the parse, nor will
4228 we issue duplicate error messages. */
4229 if (success && start)
4233 token = cp_lexer_token_at (parser->lexer, start);
4234 /* Reset the contents of the START token. */
4235 token->type = CPP_NESTED_NAME_SPECIFIER;
4236 /* Retrieve any deferred checks. Do not pop this access checks yet
4237 so the memory will not be reclaimed during token replacing below. */
4238 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4239 token->u.tree_check_value->value = parser->scope;
4240 token->u.tree_check_value->checks = get_deferred_access_checks ();
4241 token->u.tree_check_value->qualifying_scope =
4242 parser->qualifying_scope;
4243 token->keyword = RID_MAX;
4245 /* Purge all subsequent tokens. */
4246 cp_lexer_purge_tokens_after (parser->lexer, start);
4250 pop_to_parent_deferring_access_checks ();
4252 return success ? parser->scope : NULL_TREE;
4255 /* Parse a nested-name-specifier. See
4256 cp_parser_nested_name_specifier_opt for details. This function
4257 behaves identically, except that it will an issue an error if no
4258 nested-name-specifier is present. */
4261 cp_parser_nested_name_specifier (cp_parser *parser,
4262 bool typename_keyword_p,
4263 bool check_dependency_p,
4265 bool is_declaration)
4269 /* Look for the nested-name-specifier. */
4270 scope = cp_parser_nested_name_specifier_opt (parser,
4275 /* If it was not present, issue an error message. */
4278 cp_parser_error (parser, "expected nested-name-specifier");
4279 parser->scope = NULL_TREE;
4285 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4286 this is either a class-name or a namespace-name (which corresponds
4287 to the class-or-namespace-name production in the grammar). For
4288 C++0x, it can also be a type-name that refers to an enumeration
4291 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4292 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4293 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4294 TYPE_P is TRUE iff the next name should be taken as a class-name,
4295 even the same name is declared to be another entity in the same
4298 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4299 specified by the class-or-namespace-name. If neither is found the
4300 ERROR_MARK_NODE is returned. */
4303 cp_parser_qualifying_entity (cp_parser *parser,
4304 bool typename_keyword_p,
4305 bool template_keyword_p,
4306 bool check_dependency_p,
4308 bool is_declaration)
4311 tree saved_qualifying_scope;
4312 tree saved_object_scope;
4315 bool successful_parse_p;
4317 /* Before we try to parse the class-name, we must save away the
4318 current PARSER->SCOPE since cp_parser_class_name will destroy
4320 saved_scope = parser->scope;
4321 saved_qualifying_scope = parser->qualifying_scope;
4322 saved_object_scope = parser->object_scope;
4323 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4324 there is no need to look for a namespace-name. */
4325 only_class_p = template_keyword_p
4326 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4328 cp_parser_parse_tentatively (parser);
4329 scope = cp_parser_class_name (parser,
4332 type_p ? class_type : none_type,
4334 /*class_head_p=*/false,
4336 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4337 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4339 && cxx_dialect != cxx98
4340 && !successful_parse_p)
4342 /* Restore the saved scope. */
4343 parser->scope = saved_scope;
4344 parser->qualifying_scope = saved_qualifying_scope;
4345 parser->object_scope = saved_object_scope;
4347 /* Parse tentatively. */
4348 cp_parser_parse_tentatively (parser);
4350 /* Parse a typedef-name or enum-name. */
4351 scope = cp_parser_nonclass_name (parser);
4352 successful_parse_p = cp_parser_parse_definitely (parser);
4354 /* If that didn't work, try for a namespace-name. */
4355 if (!only_class_p && !successful_parse_p)
4357 /* Restore the saved scope. */
4358 parser->scope = saved_scope;
4359 parser->qualifying_scope = saved_qualifying_scope;
4360 parser->object_scope = saved_object_scope;
4361 /* If we are not looking at an identifier followed by the scope
4362 resolution operator, then this is not part of a
4363 nested-name-specifier. (Note that this function is only used
4364 to parse the components of a nested-name-specifier.) */
4365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4366 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4367 return error_mark_node;
4368 scope = cp_parser_namespace_name (parser);
4374 /* Parse a postfix-expression.
4378 postfix-expression [ expression ]
4379 postfix-expression ( expression-list [opt] )
4380 simple-type-specifier ( expression-list [opt] )
4381 typename :: [opt] nested-name-specifier identifier
4382 ( expression-list [opt] )
4383 typename :: [opt] nested-name-specifier template [opt] template-id
4384 ( expression-list [opt] )
4385 postfix-expression . template [opt] id-expression
4386 postfix-expression -> template [opt] id-expression
4387 postfix-expression . pseudo-destructor-name
4388 postfix-expression -> pseudo-destructor-name
4389 postfix-expression ++
4390 postfix-expression --
4391 dynamic_cast < type-id > ( expression )
4392 static_cast < type-id > ( expression )
4393 reinterpret_cast < type-id > ( expression )
4394 const_cast < type-id > ( expression )
4395 typeid ( expression )
4401 ( type-id ) { initializer-list , [opt] }
4403 This extension is a GNU version of the C99 compound-literal
4404 construct. (The C99 grammar uses `type-name' instead of `type-id',
4405 but they are essentially the same concept.)
4407 If ADDRESS_P is true, the postfix expression is the operand of the
4408 `&' operator. CAST_P is true if this expression is the target of a
4411 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4412 class member access expressions [expr.ref].
4414 Returns a representation of the expression. */
4417 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4418 bool member_access_only_p,
4419 cp_id_kind * pidk_return)
4423 cp_id_kind idk = CP_ID_KIND_NONE;
4424 tree postfix_expression = NULL_TREE;
4425 bool is_member_access = false;
4427 /* Peek at the next token. */
4428 token = cp_lexer_peek_token (parser->lexer);
4429 /* Some of the productions are determined by keywords. */
4430 keyword = token->keyword;
4440 const char *saved_message;
4442 /* All of these can be handled in the same way from the point
4443 of view of parsing. Begin by consuming the token
4444 identifying the cast. */
4445 cp_lexer_consume_token (parser->lexer);
4447 /* New types cannot be defined in the cast. */
4448 saved_message = parser->type_definition_forbidden_message;
4449 parser->type_definition_forbidden_message
4450 = "types may not be defined in casts";
4452 /* Look for the opening `<'. */
4453 cp_parser_require (parser, CPP_LESS, "%<<%>");
4454 /* Parse the type to which we are casting. */
4455 type = cp_parser_type_id (parser);
4456 /* Look for the closing `>'. */
4457 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4458 /* Restore the old message. */
4459 parser->type_definition_forbidden_message = saved_message;
4461 /* And the expression which is being cast. */
4462 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4463 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4464 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4466 /* Only type conversions to integral or enumeration types
4467 can be used in constant-expressions. */
4468 if (!cast_valid_in_integral_constant_expression_p (type)
4469 && (cp_parser_non_integral_constant_expression
4471 "a cast to a type other than an integral or "
4472 "enumeration type")))
4473 return error_mark_node;
4479 = build_dynamic_cast (type, expression, tf_warning_or_error);
4483 = build_static_cast (type, expression, tf_warning_or_error);
4487 = build_reinterpret_cast (type, expression,
4488 tf_warning_or_error);
4492 = build_const_cast (type, expression, tf_warning_or_error);
4503 const char *saved_message;
4504 bool saved_in_type_id_in_expr_p;
4506 /* Consume the `typeid' token. */
4507 cp_lexer_consume_token (parser->lexer);
4508 /* Look for the `(' token. */
4509 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4510 /* Types cannot be defined in a `typeid' expression. */
4511 saved_message = parser->type_definition_forbidden_message;
4512 parser->type_definition_forbidden_message
4513 = "types may not be defined in a %<typeid%> expression";
4514 /* We can't be sure yet whether we're looking at a type-id or an
4516 cp_parser_parse_tentatively (parser);
4517 /* Try a type-id first. */
4518 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4519 parser->in_type_id_in_expr_p = true;
4520 type = cp_parser_type_id (parser);
4521 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4522 /* Look for the `)' token. Otherwise, we can't be sure that
4523 we're not looking at an expression: consider `typeid (int
4524 (3))', for example. */
4525 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4526 /* If all went well, simply lookup the type-id. */
4527 if (cp_parser_parse_definitely (parser))
4528 postfix_expression = get_typeid (type);
4529 /* Otherwise, fall back to the expression variant. */
4534 /* Look for an expression. */
4535 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4536 /* Compute its typeid. */
4537 postfix_expression = build_typeid (expression);
4538 /* Look for the `)' token. */
4539 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4541 /* Restore the saved message. */
4542 parser->type_definition_forbidden_message = saved_message;
4543 /* `typeid' may not appear in an integral constant expression. */
4544 if (cp_parser_non_integral_constant_expression(parser,
4545 "%<typeid%> operator"))
4546 return error_mark_node;
4553 /* The syntax permitted here is the same permitted for an
4554 elaborated-type-specifier. */
4555 type = cp_parser_elaborated_type_specifier (parser,
4556 /*is_friend=*/false,
4557 /*is_declaration=*/false);
4558 postfix_expression = cp_parser_functional_cast (parser, type);
4566 /* If the next thing is a simple-type-specifier, we may be
4567 looking at a functional cast. We could also be looking at
4568 an id-expression. So, we try the functional cast, and if
4569 that doesn't work we fall back to the primary-expression. */
4570 cp_parser_parse_tentatively (parser);
4571 /* Look for the simple-type-specifier. */
4572 type = cp_parser_simple_type_specifier (parser,
4573 /*decl_specs=*/NULL,
4574 CP_PARSER_FLAGS_NONE);
4575 /* Parse the cast itself. */
4576 if (!cp_parser_error_occurred (parser))
4578 = cp_parser_functional_cast (parser, type);
4579 /* If that worked, we're done. */
4580 if (cp_parser_parse_definitely (parser))
4583 /* If the functional-cast didn't work out, try a
4584 compound-literal. */
4585 if (cp_parser_allow_gnu_extensions_p (parser)
4586 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4588 VEC(constructor_elt,gc) *initializer_list = NULL;
4589 bool saved_in_type_id_in_expr_p;
4591 cp_parser_parse_tentatively (parser);
4592 /* Consume the `('. */
4593 cp_lexer_consume_token (parser->lexer);
4594 /* Parse the type. */
4595 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4596 parser->in_type_id_in_expr_p = true;
4597 type = cp_parser_type_id (parser);
4598 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4599 /* Look for the `)'. */
4600 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4601 /* Look for the `{'. */
4602 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4603 /* If things aren't going well, there's no need to
4605 if (!cp_parser_error_occurred (parser))
4607 bool non_constant_p;
4608 /* Parse the initializer-list. */
4610 = cp_parser_initializer_list (parser, &non_constant_p);
4611 /* Allow a trailing `,'. */
4612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4613 cp_lexer_consume_token (parser->lexer);
4614 /* Look for the final `}'. */
4615 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4617 /* If that worked, we're definitely looking at a
4618 compound-literal expression. */
4619 if (cp_parser_parse_definitely (parser))
4621 /* Warn the user that a compound literal is not
4622 allowed in standard C++. */
4623 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4624 /* For simplicity, we disallow compound literals in
4625 constant-expressions. We could
4626 allow compound literals of integer type, whose
4627 initializer was a constant, in constant
4628 expressions. Permitting that usage, as a further
4629 extension, would not change the meaning of any
4630 currently accepted programs. (Of course, as
4631 compound literals are not part of ISO C++, the
4632 standard has nothing to say.) */
4633 if (cp_parser_non_integral_constant_expression
4634 (parser, "non-constant compound literals"))
4636 postfix_expression = error_mark_node;
4639 /* Form the representation of the compound-literal. */
4641 = (finish_compound_literal
4642 (type, build_constructor (init_list_type_node,
4643 initializer_list)));
4648 /* It must be a primary-expression. */
4650 = cp_parser_primary_expression (parser, address_p, cast_p,
4651 /*template_arg_p=*/false,
4657 /* Keep looping until the postfix-expression is complete. */
4660 if (idk == CP_ID_KIND_UNQUALIFIED
4661 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4662 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4663 /* It is not a Koenig lookup function call. */
4665 = unqualified_name_lookup_error (postfix_expression);
4667 /* Peek at the next token. */
4668 token = cp_lexer_peek_token (parser->lexer);
4670 switch (token->type)
4672 case CPP_OPEN_SQUARE:
4674 = cp_parser_postfix_open_square_expression (parser,
4677 idk = CP_ID_KIND_NONE;
4678 is_member_access = false;
4681 case CPP_OPEN_PAREN:
4682 /* postfix-expression ( expression-list [opt] ) */
4685 bool is_builtin_constant_p;
4686 bool saved_integral_constant_expression_p = false;
4687 bool saved_non_integral_constant_expression_p = false;
4690 is_member_access = false;
4692 is_builtin_constant_p
4693 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4694 if (is_builtin_constant_p)
4696 /* The whole point of __builtin_constant_p is to allow
4697 non-constant expressions to appear as arguments. */
4698 saved_integral_constant_expression_p
4699 = parser->integral_constant_expression_p;
4700 saved_non_integral_constant_expression_p
4701 = parser->non_integral_constant_expression_p;
4702 parser->integral_constant_expression_p = false;
4704 args = (cp_parser_parenthesized_expression_list
4705 (parser, /*is_attribute_list=*/false,
4706 /*cast_p=*/false, /*allow_expansion_p=*/true,
4707 /*non_constant_p=*/NULL));
4708 if (is_builtin_constant_p)
4710 parser->integral_constant_expression_p
4711 = saved_integral_constant_expression_p;
4712 parser->non_integral_constant_expression_p
4713 = saved_non_integral_constant_expression_p;
4716 if (args == error_mark_node)
4718 postfix_expression = error_mark_node;
4722 /* Function calls are not permitted in
4723 constant-expressions. */
4724 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4725 && cp_parser_non_integral_constant_expression (parser,
4728 postfix_expression = error_mark_node;
4733 if (idk == CP_ID_KIND_UNQUALIFIED
4734 || idk == CP_ID_KIND_TEMPLATE_ID)
4736 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4742 = perform_koenig_lookup (postfix_expression, args);
4746 = unqualified_fn_lookup_error (postfix_expression);
4748 /* We do not perform argument-dependent lookup if
4749 normal lookup finds a non-function, in accordance
4750 with the expected resolution of DR 218. */
4751 else if (args && is_overloaded_fn (postfix_expression))
4753 tree fn = get_first_fn (postfix_expression);
4755 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4756 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4758 /* Only do argument dependent lookup if regular
4759 lookup does not find a set of member functions.
4760 [basic.lookup.koenig]/2a */
4761 if (!DECL_FUNCTION_MEMBER_P (fn))
4765 = perform_koenig_lookup (postfix_expression, args);
4770 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4772 tree instance = TREE_OPERAND (postfix_expression, 0);
4773 tree fn = TREE_OPERAND (postfix_expression, 1);
4775 if (processing_template_decl
4776 && (type_dependent_expression_p (instance)
4777 || (!BASELINK_P (fn)
4778 && TREE_CODE (fn) != FIELD_DECL)
4779 || type_dependent_expression_p (fn)
4780 || any_type_dependent_arguments_p (args)))
4783 = build_nt_call_list (postfix_expression, args);
4787 if (BASELINK_P (fn))
4790 = (build_new_method_call
4791 (instance, fn, args, NULL_TREE,
4792 (idk == CP_ID_KIND_QUALIFIED
4793 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4795 tf_warning_or_error));
4799 = finish_call_expr (postfix_expression, args,
4800 /*disallow_virtual=*/false,
4802 tf_warning_or_error);
4804 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4805 || TREE_CODE (postfix_expression) == MEMBER_REF
4806 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4807 postfix_expression = (build_offset_ref_call_from_tree
4808 (postfix_expression, args));
4809 else if (idk == CP_ID_KIND_QUALIFIED)
4810 /* A call to a static class member, or a namespace-scope
4813 = finish_call_expr (postfix_expression, args,
4814 /*disallow_virtual=*/true,
4816 tf_warning_or_error);
4818 /* All other function calls. */
4820 = finish_call_expr (postfix_expression, args,
4821 /*disallow_virtual=*/false,
4823 tf_warning_or_error);
4825 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4826 idk = CP_ID_KIND_NONE;
4832 /* postfix-expression . template [opt] id-expression
4833 postfix-expression . pseudo-destructor-name
4834 postfix-expression -> template [opt] id-expression
4835 postfix-expression -> pseudo-destructor-name */
4837 /* Consume the `.' or `->' operator. */
4838 cp_lexer_consume_token (parser->lexer);
4841 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4846 is_member_access = true;
4850 /* postfix-expression ++ */
4851 /* Consume the `++' token. */
4852 cp_lexer_consume_token (parser->lexer);
4853 /* Generate a representation for the complete expression. */
4855 = finish_increment_expr (postfix_expression,
4856 POSTINCREMENT_EXPR);
4857 /* Increments may not appear in constant-expressions. */
4858 if (cp_parser_non_integral_constant_expression (parser,
4860 postfix_expression = error_mark_node;
4861 idk = CP_ID_KIND_NONE;
4862 is_member_access = false;
4865 case CPP_MINUS_MINUS:
4866 /* postfix-expression -- */
4867 /* Consume the `--' token. */
4868 cp_lexer_consume_token (parser->lexer);
4869 /* Generate a representation for the complete expression. */
4871 = finish_increment_expr (postfix_expression,
4872 POSTDECREMENT_EXPR);
4873 /* Decrements may not appear in constant-expressions. */
4874 if (cp_parser_non_integral_constant_expression (parser,
4876 postfix_expression = error_mark_node;
4877 idk = CP_ID_KIND_NONE;
4878 is_member_access = false;
4882 if (pidk_return != NULL)
4883 * pidk_return = idk;
4884 if (member_access_only_p)
4885 return is_member_access? postfix_expression : error_mark_node;
4887 return postfix_expression;
4891 /* We should never get here. */
4893 return error_mark_node;
4896 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4897 by cp_parser_builtin_offsetof. We're looking for
4899 postfix-expression [ expression ]
4901 FOR_OFFSETOF is set if we're being called in that context, which
4902 changes how we deal with integer constant expressions. */
4905 cp_parser_postfix_open_square_expression (cp_parser *parser,
4906 tree postfix_expression,
4911 /* Consume the `[' token. */
4912 cp_lexer_consume_token (parser->lexer);
4914 /* Parse the index expression. */
4915 /* ??? For offsetof, there is a question of what to allow here. If
4916 offsetof is not being used in an integral constant expression context,
4917 then we *could* get the right answer by computing the value at runtime.
4918 If we are in an integral constant expression context, then we might
4919 could accept any constant expression; hard to say without analysis.
4920 Rather than open the barn door too wide right away, allow only integer
4921 constant expressions here. */
4923 index = cp_parser_constant_expression (parser, false, NULL);
4925 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4927 /* Look for the closing `]'. */
4928 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4930 /* Build the ARRAY_REF. */
4931 postfix_expression = grok_array_decl (postfix_expression, index);
4933 /* When not doing offsetof, array references are not permitted in
4934 constant-expressions. */
4936 && (cp_parser_non_integral_constant_expression
4937 (parser, "an array reference")))
4938 postfix_expression = error_mark_node;
4940 return postfix_expression;
4943 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4944 by cp_parser_builtin_offsetof. We're looking for
4946 postfix-expression . template [opt] id-expression
4947 postfix-expression . pseudo-destructor-name
4948 postfix-expression -> template [opt] id-expression
4949 postfix-expression -> pseudo-destructor-name
4951 FOR_OFFSETOF is set if we're being called in that context. That sorta
4952 limits what of the above we'll actually accept, but nevermind.
4953 TOKEN_TYPE is the "." or "->" token, which will already have been
4954 removed from the stream. */
4957 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4958 enum cpp_ttype token_type,
4959 tree postfix_expression,
4960 bool for_offsetof, cp_id_kind *idk,
4961 location_t location)
4965 bool pseudo_destructor_p;
4966 tree scope = NULL_TREE;
4968 /* If this is a `->' operator, dereference the pointer. */
4969 if (token_type == CPP_DEREF)
4970 postfix_expression = build_x_arrow (postfix_expression);
4971 /* Check to see whether or not the expression is type-dependent. */
4972 dependent_p = type_dependent_expression_p (postfix_expression);
4973 /* The identifier following the `->' or `.' is not qualified. */
4974 parser->scope = NULL_TREE;
4975 parser->qualifying_scope = NULL_TREE;
4976 parser->object_scope = NULL_TREE;
4977 *idk = CP_ID_KIND_NONE;
4979 /* Enter the scope corresponding to the type of the object
4980 given by the POSTFIX_EXPRESSION. */
4981 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4983 scope = TREE_TYPE (postfix_expression);
4984 /* According to the standard, no expression should ever have
4985 reference type. Unfortunately, we do not currently match
4986 the standard in this respect in that our internal representation
4987 of an expression may have reference type even when the standard
4988 says it does not. Therefore, we have to manually obtain the
4989 underlying type here. */
4990 scope = non_reference (scope);
4991 /* The type of the POSTFIX_EXPRESSION must be complete. */
4992 if (scope == unknown_type_node)
4994 error ("%H%qE does not have class type", &location, postfix_expression);
4998 scope = complete_type_or_else (scope, NULL_TREE);
4999 /* Let the name lookup machinery know that we are processing a
5000 class member access expression. */
5001 parser->context->object_type = scope;
5002 /* If something went wrong, we want to be able to discern that case,
5003 as opposed to the case where there was no SCOPE due to the type
5004 of expression being dependent. */
5006 scope = error_mark_node;
5007 /* If the SCOPE was erroneous, make the various semantic analysis
5008 functions exit quickly -- and without issuing additional error
5010 if (scope == error_mark_node)
5011 postfix_expression = error_mark_node;
5014 /* Assume this expression is not a pseudo-destructor access. */
5015 pseudo_destructor_p = false;
5017 /* If the SCOPE is a scalar type, then, if this is a valid program,
5018 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5019 is type dependent, it can be pseudo-destructor-name or something else.
5020 Try to parse it as pseudo-destructor-name first. */
5021 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5026 cp_parser_parse_tentatively (parser);
5027 /* Parse the pseudo-destructor-name. */
5029 cp_parser_pseudo_destructor_name (parser, &s, &type);
5031 && (cp_parser_error_occurred (parser)
5032 || TREE_CODE (type) != TYPE_DECL
5033 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5034 cp_parser_abort_tentative_parse (parser);
5035 else if (cp_parser_parse_definitely (parser))
5037 pseudo_destructor_p = true;
5039 = finish_pseudo_destructor_expr (postfix_expression,
5040 s, TREE_TYPE (type));
5044 if (!pseudo_destructor_p)
5046 /* If the SCOPE is not a scalar type, we are looking at an
5047 ordinary class member access expression, rather than a
5048 pseudo-destructor-name. */
5050 cp_token *token = cp_lexer_peek_token (parser->lexer);
5051 /* Parse the id-expression. */
5052 name = (cp_parser_id_expression
5054 cp_parser_optional_template_keyword (parser),
5055 /*check_dependency_p=*/true,
5057 /*declarator_p=*/false,
5058 /*optional_p=*/false));
5059 /* In general, build a SCOPE_REF if the member name is qualified.
5060 However, if the name was not dependent and has already been
5061 resolved; there is no need to build the SCOPE_REF. For example;
5063 struct X { void f(); };
5064 template <typename T> void f(T* t) { t->X::f(); }
5066 Even though "t" is dependent, "X::f" is not and has been resolved
5067 to a BASELINK; there is no need to include scope information. */
5069 /* But we do need to remember that there was an explicit scope for
5070 virtual function calls. */
5072 *idk = CP_ID_KIND_QUALIFIED;
5074 /* If the name is a template-id that names a type, we will get a
5075 TYPE_DECL here. That is invalid code. */
5076 if (TREE_CODE (name) == TYPE_DECL)
5078 error ("%Hinvalid use of %qD", &token->location, name);
5079 postfix_expression = error_mark_node;
5083 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5085 name = build_qualified_name (/*type=*/NULL_TREE,
5089 parser->scope = NULL_TREE;
5090 parser->qualifying_scope = NULL_TREE;
5091 parser->object_scope = NULL_TREE;
5093 if (scope && name && BASELINK_P (name))
5094 adjust_result_of_qualified_name_lookup
5095 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5097 = finish_class_member_access_expr (postfix_expression, name,
5099 tf_warning_or_error);
5103 /* We no longer need to look up names in the scope of the object on
5104 the left-hand side of the `.' or `->' operator. */
5105 parser->context->object_type = NULL_TREE;
5107 /* Outside of offsetof, these operators may not appear in
5108 constant-expressions. */
5110 && (cp_parser_non_integral_constant_expression
5111 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5112 postfix_expression = error_mark_node;
5114 return postfix_expression;
5117 /* Parse a parenthesized expression-list.
5120 assignment-expression
5121 expression-list, assignment-expression
5126 identifier, expression-list
5128 CAST_P is true if this expression is the target of a cast.
5130 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5133 Returns a TREE_LIST. The TREE_VALUE of each node is a
5134 representation of an assignment-expression. Note that a TREE_LIST
5135 is returned even if there is only a single expression in the list.
5136 error_mark_node is returned if the ( and or ) are
5137 missing. NULL_TREE is returned on no expressions. The parentheses
5138 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5139 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5140 indicates whether or not all of the expressions in the list were
5144 cp_parser_parenthesized_expression_list (cp_parser* parser,
5145 bool is_attribute_list,
5147 bool allow_expansion_p,
5148 bool *non_constant_p)
5150 tree expression_list = NULL_TREE;
5151 bool fold_expr_p = is_attribute_list;
5152 tree identifier = NULL_TREE;
5153 bool saved_greater_than_is_operator_p;
5155 /* Assume all the expressions will be constant. */
5157 *non_constant_p = false;
5159 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5160 return error_mark_node;
5162 /* Within a parenthesized expression, a `>' token is always
5163 the greater-than operator. */
5164 saved_greater_than_is_operator_p
5165 = parser->greater_than_is_operator_p;
5166 parser->greater_than_is_operator_p = true;
5168 /* Consume expressions until there are no more. */
5169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5174 /* At the beginning of attribute lists, check to see if the
5175 next token is an identifier. */
5176 if (is_attribute_list
5177 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5181 /* Consume the identifier. */
5182 token = cp_lexer_consume_token (parser->lexer);
5183 /* Save the identifier. */
5184 identifier = token->u.value;
5188 bool expr_non_constant_p;
5190 /* Parse the next assignment-expression. */
5191 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5193 /* A braced-init-list. */
5194 maybe_warn_cpp0x ("extended initializer lists");
5195 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5196 if (non_constant_p && expr_non_constant_p)
5197 *non_constant_p = true;
5199 else if (non_constant_p)
5201 expr = (cp_parser_constant_expression
5202 (parser, /*allow_non_constant_p=*/true,
5203 &expr_non_constant_p));
5204 if (expr_non_constant_p)
5205 *non_constant_p = true;
5208 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5211 expr = fold_non_dependent_expr (expr);
5213 /* If we have an ellipsis, then this is an expression
5215 if (allow_expansion_p
5216 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5218 /* Consume the `...'. */
5219 cp_lexer_consume_token (parser->lexer);
5221 /* Build the argument pack. */
5222 expr = make_pack_expansion (expr);
5225 /* Add it to the list. We add error_mark_node
5226 expressions to the list, so that we can still tell if
5227 the correct form for a parenthesized expression-list
5228 is found. That gives better errors. */
5229 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5231 if (expr == error_mark_node)
5235 /* After the first item, attribute lists look the same as
5236 expression lists. */
5237 is_attribute_list = false;
5240 /* If the next token isn't a `,', then we are done. */
5241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5244 /* Otherwise, consume the `,' and keep going. */
5245 cp_lexer_consume_token (parser->lexer);
5248 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5253 /* We try and resync to an unnested comma, as that will give the
5254 user better diagnostics. */
5255 ending = cp_parser_skip_to_closing_parenthesis (parser,
5256 /*recovering=*/true,
5258 /*consume_paren=*/true);
5263 parser->greater_than_is_operator_p
5264 = saved_greater_than_is_operator_p;
5265 return error_mark_node;
5269 parser->greater_than_is_operator_p
5270 = saved_greater_than_is_operator_p;
5272 /* We built up the list in reverse order so we must reverse it now. */
5273 expression_list = nreverse (expression_list);
5275 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5277 return expression_list;
5280 /* Parse a pseudo-destructor-name.
5282 pseudo-destructor-name:
5283 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5284 :: [opt] nested-name-specifier template template-id :: ~ type-name
5285 :: [opt] nested-name-specifier [opt] ~ type-name
5287 If either of the first two productions is used, sets *SCOPE to the
5288 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5289 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5290 or ERROR_MARK_NODE if the parse fails. */
5293 cp_parser_pseudo_destructor_name (cp_parser* parser,
5297 bool nested_name_specifier_p;
5299 /* Assume that things will not work out. */
5300 *type = error_mark_node;
5302 /* Look for the optional `::' operator. */
5303 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5304 /* Look for the optional nested-name-specifier. */
5305 nested_name_specifier_p
5306 = (cp_parser_nested_name_specifier_opt (parser,
5307 /*typename_keyword_p=*/false,
5308 /*check_dependency_p=*/true,
5310 /*is_declaration=*/false)
5312 /* Now, if we saw a nested-name-specifier, we might be doing the
5313 second production. */
5314 if (nested_name_specifier_p
5315 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5317 /* Consume the `template' keyword. */
5318 cp_lexer_consume_token (parser->lexer);
5319 /* Parse the template-id. */
5320 cp_parser_template_id (parser,
5321 /*template_keyword_p=*/true,
5322 /*check_dependency_p=*/false,
5323 /*is_declaration=*/true);
5324 /* Look for the `::' token. */
5325 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5327 /* If the next token is not a `~', then there might be some
5328 additional qualification. */
5329 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5331 /* At this point, we're looking for "type-name :: ~". The type-name
5332 must not be a class-name, since this is a pseudo-destructor. So,
5333 it must be either an enum-name, or a typedef-name -- both of which
5334 are just identifiers. So, we peek ahead to check that the "::"
5335 and "~" tokens are present; if they are not, then we can avoid
5336 calling type_name. */
5337 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5338 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5339 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5341 cp_parser_error (parser, "non-scalar type");
5345 /* Look for the type-name. */
5346 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5347 if (*scope == error_mark_node)
5350 /* Look for the `::' token. */
5351 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5356 /* Look for the `~'. */
5357 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5358 /* Look for the type-name again. We are not responsible for
5359 checking that it matches the first type-name. */
5360 *type = cp_parser_nonclass_name (parser);
5363 /* Parse a unary-expression.
5369 unary-operator cast-expression
5370 sizeof unary-expression
5378 __extension__ cast-expression
5379 __alignof__ unary-expression
5380 __alignof__ ( type-id )
5381 __real__ cast-expression
5382 __imag__ cast-expression
5385 ADDRESS_P is true iff the unary-expression is appearing as the
5386 operand of the `&' operator. CAST_P is true if this expression is
5387 the target of a cast.
5389 Returns a representation of the expression. */
5392 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5396 enum tree_code unary_operator;
5398 /* Peek at the next token. */
5399 token = cp_lexer_peek_token (parser->lexer);
5400 /* Some keywords give away the kind of expression. */
5401 if (token->type == CPP_KEYWORD)
5403 enum rid keyword = token->keyword;
5413 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5414 /* Consume the token. */
5415 cp_lexer_consume_token (parser->lexer);
5416 /* Parse the operand. */
5417 operand = cp_parser_sizeof_operand (parser, keyword);
5419 if (TYPE_P (operand))
5420 return cxx_sizeof_or_alignof_type (operand, op, true);
5422 return cxx_sizeof_or_alignof_expr (operand, op, true);
5426 return cp_parser_new_expression (parser);
5429 return cp_parser_delete_expression (parser);
5433 /* The saved value of the PEDANTIC flag. */
5437 /* Save away the PEDANTIC flag. */
5438 cp_parser_extension_opt (parser, &saved_pedantic);
5439 /* Parse the cast-expression. */
5440 expr = cp_parser_simple_cast_expression (parser);
5441 /* Restore the PEDANTIC flag. */
5442 pedantic = saved_pedantic;
5452 /* Consume the `__real__' or `__imag__' token. */
5453 cp_lexer_consume_token (parser->lexer);
5454 /* Parse the cast-expression. */
5455 expression = cp_parser_simple_cast_expression (parser);
5456 /* Create the complete representation. */
5457 return build_x_unary_op ((keyword == RID_REALPART
5458 ? REALPART_EXPR : IMAGPART_EXPR),
5460 tf_warning_or_error);
5469 /* Look for the `:: new' and `:: delete', which also signal the
5470 beginning of a new-expression, or delete-expression,
5471 respectively. If the next token is `::', then it might be one of
5473 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5477 /* See if the token after the `::' is one of the keywords in
5478 which we're interested. */
5479 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5480 /* If it's `new', we have a new-expression. */
5481 if (keyword == RID_NEW)
5482 return cp_parser_new_expression (parser);
5483 /* Similarly, for `delete'. */
5484 else if (keyword == RID_DELETE)
5485 return cp_parser_delete_expression (parser);
5488 /* Look for a unary operator. */
5489 unary_operator = cp_parser_unary_operator (token);
5490 /* The `++' and `--' operators can be handled similarly, even though
5491 they are not technically unary-operators in the grammar. */
5492 if (unary_operator == ERROR_MARK)
5494 if (token->type == CPP_PLUS_PLUS)
5495 unary_operator = PREINCREMENT_EXPR;
5496 else if (token->type == CPP_MINUS_MINUS)
5497 unary_operator = PREDECREMENT_EXPR;
5498 /* Handle the GNU address-of-label extension. */
5499 else if (cp_parser_allow_gnu_extensions_p (parser)
5500 && token->type == CPP_AND_AND)
5504 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5506 /* Consume the '&&' token. */
5507 cp_lexer_consume_token (parser->lexer);
5508 /* Look for the identifier. */
5509 identifier = cp_parser_identifier (parser);
5510 /* Create an expression representing the address. */
5511 expression = finish_label_address_expr (identifier, loc);
5512 if (cp_parser_non_integral_constant_expression (parser,
5513 "the address of a label"))
5514 expression = error_mark_node;
5518 if (unary_operator != ERROR_MARK)
5520 tree cast_expression;
5521 tree expression = error_mark_node;
5522 const char *non_constant_p = NULL;
5524 /* Consume the operator token. */
5525 token = cp_lexer_consume_token (parser->lexer);
5526 /* Parse the cast-expression. */
5528 = cp_parser_cast_expression (parser,
5529 unary_operator == ADDR_EXPR,
5530 /*cast_p=*/false, pidk);
5531 /* Now, build an appropriate representation. */
5532 switch (unary_operator)
5535 non_constant_p = "%<*%>";
5536 expression = build_x_indirect_ref (cast_expression, "unary *",
5537 tf_warning_or_error);
5541 non_constant_p = "%<&%>";
5544 expression = build_x_unary_op (unary_operator, cast_expression,
5545 tf_warning_or_error);
5548 case PREINCREMENT_EXPR:
5549 case PREDECREMENT_EXPR:
5550 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5551 ? "%<++%>" : "%<--%>");
5553 case UNARY_PLUS_EXPR:
5555 case TRUTH_NOT_EXPR:
5556 expression = finish_unary_op_expr (unary_operator, cast_expression);
5564 && cp_parser_non_integral_constant_expression (parser,
5566 expression = error_mark_node;
5571 return cp_parser_postfix_expression (parser, address_p, cast_p,
5572 /*member_access_only_p=*/false,
5576 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5577 unary-operator, the corresponding tree code is returned. */
5579 static enum tree_code
5580 cp_parser_unary_operator (cp_token* token)
5582 switch (token->type)
5585 return INDIRECT_REF;
5591 return UNARY_PLUS_EXPR;
5597 return TRUTH_NOT_EXPR;
5600 return BIT_NOT_EXPR;
5607 /* Parse a new-expression.
5610 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5611 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5613 Returns a representation of the expression. */
5616 cp_parser_new_expression (cp_parser* parser)
5618 bool global_scope_p;
5624 /* Look for the optional `::' operator. */
5626 = (cp_parser_global_scope_opt (parser,
5627 /*current_scope_valid_p=*/false)
5629 /* Look for the `new' operator. */
5630 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5631 /* There's no easy way to tell a new-placement from the
5632 `( type-id )' construct. */
5633 cp_parser_parse_tentatively (parser);
5634 /* Look for a new-placement. */
5635 placement = cp_parser_new_placement (parser);
5636 /* If that didn't work out, there's no new-placement. */
5637 if (!cp_parser_parse_definitely (parser))
5638 placement = NULL_TREE;
5640 /* If the next token is a `(', then we have a parenthesized
5642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5645 /* Consume the `('. */
5646 cp_lexer_consume_token (parser->lexer);
5647 /* Parse the type-id. */
5648 type = cp_parser_type_id (parser);
5649 /* Look for the closing `)'. */
5650 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5651 token = cp_lexer_peek_token (parser->lexer);
5652 /* There should not be a direct-new-declarator in this production,
5653 but GCC used to allowed this, so we check and emit a sensible error
5654 message for this case. */
5655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5657 error ("%Harray bound forbidden after parenthesized type-id",
5659 inform (token->location,
5660 "try removing the parentheses around the type-id");
5661 cp_parser_direct_new_declarator (parser);
5665 /* Otherwise, there must be a new-type-id. */
5667 type = cp_parser_new_type_id (parser, &nelts);
5669 /* If the next token is a `(' or '{', then we have a new-initializer. */
5670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5671 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5672 initializer = cp_parser_new_initializer (parser);
5674 initializer = NULL_TREE;
5676 /* A new-expression may not appear in an integral constant
5678 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5679 return error_mark_node;
5681 /* Create a representation of the new-expression. */
5682 return build_new (placement, type, nelts, initializer, global_scope_p,
5683 tf_warning_or_error);
5686 /* Parse a new-placement.
5691 Returns the same representation as for an expression-list. */
5694 cp_parser_new_placement (cp_parser* parser)
5696 tree expression_list;
5698 /* Parse the expression-list. */
5699 expression_list = (cp_parser_parenthesized_expression_list
5700 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5701 /*non_constant_p=*/NULL));
5703 return expression_list;
5706 /* Parse a new-type-id.
5709 type-specifier-seq new-declarator [opt]
5711 Returns the TYPE allocated. If the new-type-id indicates an array
5712 type, *NELTS is set to the number of elements in the last array
5713 bound; the TYPE will not include the last array bound. */
5716 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5718 cp_decl_specifier_seq type_specifier_seq;
5719 cp_declarator *new_declarator;
5720 cp_declarator *declarator;
5721 cp_declarator *outer_declarator;
5722 const char *saved_message;
5725 /* The type-specifier sequence must not contain type definitions.
5726 (It cannot contain declarations of new types either, but if they
5727 are not definitions we will catch that because they are not
5729 saved_message = parser->type_definition_forbidden_message;
5730 parser->type_definition_forbidden_message
5731 = "types may not be defined in a new-type-id";
5732 /* Parse the type-specifier-seq. */
5733 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5734 &type_specifier_seq);
5735 /* Restore the old message. */
5736 parser->type_definition_forbidden_message = saved_message;
5737 /* Parse the new-declarator. */
5738 new_declarator = cp_parser_new_declarator_opt (parser);
5740 /* Determine the number of elements in the last array dimension, if
5743 /* Skip down to the last array dimension. */
5744 declarator = new_declarator;
5745 outer_declarator = NULL;
5746 while (declarator && (declarator->kind == cdk_pointer
5747 || declarator->kind == cdk_ptrmem))
5749 outer_declarator = declarator;
5750 declarator = declarator->declarator;
5753 && declarator->kind == cdk_array
5754 && declarator->declarator
5755 && declarator->declarator->kind == cdk_array)
5757 outer_declarator = declarator;
5758 declarator = declarator->declarator;
5761 if (declarator && declarator->kind == cdk_array)
5763 *nelts = declarator->u.array.bounds;
5764 if (*nelts == error_mark_node)
5765 *nelts = integer_one_node;
5767 if (outer_declarator)
5768 outer_declarator->declarator = declarator->declarator;
5770 new_declarator = NULL;
5773 type = groktypename (&type_specifier_seq, new_declarator);
5777 /* Parse an (optional) new-declarator.
5780 ptr-operator new-declarator [opt]
5781 direct-new-declarator
5783 Returns the declarator. */
5785 static cp_declarator *
5786 cp_parser_new_declarator_opt (cp_parser* parser)
5788 enum tree_code code;
5790 cp_cv_quals cv_quals;
5792 /* We don't know if there's a ptr-operator next, or not. */
5793 cp_parser_parse_tentatively (parser);
5794 /* Look for a ptr-operator. */
5795 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5796 /* If that worked, look for more new-declarators. */
5797 if (cp_parser_parse_definitely (parser))
5799 cp_declarator *declarator;
5801 /* Parse another optional declarator. */
5802 declarator = cp_parser_new_declarator_opt (parser);
5804 return cp_parser_make_indirect_declarator
5805 (code, type, cv_quals, declarator);
5808 /* If the next token is a `[', there is a direct-new-declarator. */
5809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5810 return cp_parser_direct_new_declarator (parser);
5815 /* Parse a direct-new-declarator.
5817 direct-new-declarator:
5819 direct-new-declarator [constant-expression]
5823 static cp_declarator *
5824 cp_parser_direct_new_declarator (cp_parser* parser)
5826 cp_declarator *declarator = NULL;
5832 /* Look for the opening `['. */
5833 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5834 /* The first expression is not required to be constant. */
5837 cp_token *token = cp_lexer_peek_token (parser->lexer);
5838 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5839 /* The standard requires that the expression have integral
5840 type. DR 74 adds enumeration types. We believe that the
5841 real intent is that these expressions be handled like the
5842 expression in a `switch' condition, which also allows
5843 classes with a single conversion to integral or
5844 enumeration type. */
5845 if (!processing_template_decl)
5848 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5853 error ("%Hexpression in new-declarator must have integral "
5854 "or enumeration type", &token->location);
5855 expression = error_mark_node;
5859 /* But all the other expressions must be. */
5862 = cp_parser_constant_expression (parser,
5863 /*allow_non_constant=*/false,
5865 /* Look for the closing `]'. */
5866 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5868 /* Add this bound to the declarator. */
5869 declarator = make_array_declarator (declarator, expression);
5871 /* If the next token is not a `[', then there are no more
5873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5880 /* Parse a new-initializer.
5883 ( expression-list [opt] )
5886 Returns a representation of the expression-list. If there is no
5887 expression-list, VOID_ZERO_NODE is returned. */
5890 cp_parser_new_initializer (cp_parser* parser)
5892 tree expression_list;
5894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5896 bool expr_non_constant_p;
5897 maybe_warn_cpp0x ("extended initializer lists");
5898 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5899 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5900 expression_list = build_tree_list (NULL_TREE, expression_list);
5903 expression_list = (cp_parser_parenthesized_expression_list
5904 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5905 /*non_constant_p=*/NULL));
5906 if (!expression_list)
5907 expression_list = void_zero_node;
5909 return expression_list;
5912 /* Parse a delete-expression.
5915 :: [opt] delete cast-expression
5916 :: [opt] delete [ ] cast-expression
5918 Returns a representation of the expression. */
5921 cp_parser_delete_expression (cp_parser* parser)
5923 bool global_scope_p;
5927 /* Look for the optional `::' operator. */
5929 = (cp_parser_global_scope_opt (parser,
5930 /*current_scope_valid_p=*/false)
5932 /* Look for the `delete' keyword. */
5933 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5934 /* See if the array syntax is in use. */
5935 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5937 /* Consume the `[' token. */
5938 cp_lexer_consume_token (parser->lexer);
5939 /* Look for the `]' token. */
5940 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5941 /* Remember that this is the `[]' construct. */
5947 /* Parse the cast-expression. */
5948 expression = cp_parser_simple_cast_expression (parser);
5950 /* A delete-expression may not appear in an integral constant
5952 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5953 return error_mark_node;
5955 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5958 /* Returns true if TOKEN may start a cast-expression and false
5962 cp_parser_token_starts_cast_expression (cp_token *token)
5964 switch (token->type)
5970 case CPP_CLOSE_SQUARE:
5971 case CPP_CLOSE_PAREN:
5972 case CPP_CLOSE_BRACE:
5976 case CPP_DEREF_STAR:
5984 case CPP_GREATER_EQ:
6004 /* '[' may start a primary-expression in obj-c++. */
6005 case CPP_OPEN_SQUARE:
6006 return c_dialect_objc ();
6013 /* Parse a cast-expression.
6017 ( type-id ) cast-expression
6019 ADDRESS_P is true iff the unary-expression is appearing as the
6020 operand of the `&' operator. CAST_P is true if this expression is
6021 the target of a cast.
6023 Returns a representation of the expression. */
6026 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6029 /* If it's a `(', then we might be looking at a cast. */
6030 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6032 tree type = NULL_TREE;
6033 tree expr = NULL_TREE;
6034 bool compound_literal_p;
6035 const char *saved_message;
6037 /* There's no way to know yet whether or not this is a cast.
6038 For example, `(int (3))' is a unary-expression, while `(int)
6039 3' is a cast. So, we resort to parsing tentatively. */
6040 cp_parser_parse_tentatively (parser);
6041 /* Types may not be defined in a cast. */
6042 saved_message = parser->type_definition_forbidden_message;
6043 parser->type_definition_forbidden_message
6044 = "types may not be defined in casts";
6045 /* Consume the `('. */
6046 cp_lexer_consume_token (parser->lexer);
6047 /* A very tricky bit is that `(struct S) { 3 }' is a
6048 compound-literal (which we permit in C++ as an extension).
6049 But, that construct is not a cast-expression -- it is a
6050 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6051 is legal; if the compound-literal were a cast-expression,
6052 you'd need an extra set of parentheses.) But, if we parse
6053 the type-id, and it happens to be a class-specifier, then we
6054 will commit to the parse at that point, because we cannot
6055 undo the action that is done when creating a new class. So,
6056 then we cannot back up and do a postfix-expression.
6058 Therefore, we scan ahead to the closing `)', and check to see
6059 if the token after the `)' is a `{'. If so, we are not
6060 looking at a cast-expression.
6062 Save tokens so that we can put them back. */
6063 cp_lexer_save_tokens (parser->lexer);
6064 /* Skip tokens until the next token is a closing parenthesis.
6065 If we find the closing `)', and the next token is a `{', then
6066 we are looking at a compound-literal. */
6068 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6069 /*consume_paren=*/true)
6070 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6071 /* Roll back the tokens we skipped. */
6072 cp_lexer_rollback_tokens (parser->lexer);
6073 /* If we were looking at a compound-literal, simulate an error
6074 so that the call to cp_parser_parse_definitely below will
6076 if (compound_literal_p)
6077 cp_parser_simulate_error (parser);
6080 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6081 parser->in_type_id_in_expr_p = true;
6082 /* Look for the type-id. */
6083 type = cp_parser_type_id (parser);
6084 /* Look for the closing `)'. */
6085 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6086 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6089 /* Restore the saved message. */
6090 parser->type_definition_forbidden_message = saved_message;
6092 /* At this point this can only be either a cast or a
6093 parenthesized ctor such as `(T ())' that looks like a cast to
6094 function returning T. */
6095 if (!cp_parser_error_occurred (parser)
6096 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6099 cp_parser_parse_definitely (parser);
6100 expr = cp_parser_cast_expression (parser,
6101 /*address_p=*/false,
6102 /*cast_p=*/true, pidk);
6104 /* Warn about old-style casts, if so requested. */
6105 if (warn_old_style_cast
6106 && !in_system_header
6107 && !VOID_TYPE_P (type)
6108 && current_lang_name != lang_name_c)
6109 warning (OPT_Wold_style_cast, "use of old-style cast");
6111 /* Only type conversions to integral or enumeration types
6112 can be used in constant-expressions. */
6113 if (!cast_valid_in_integral_constant_expression_p (type)
6114 && (cp_parser_non_integral_constant_expression
6116 "a cast to a type other than an integral or "
6117 "enumeration type")))
6118 return error_mark_node;
6120 /* Perform the cast. */
6121 expr = build_c_cast (type, expr);
6125 cp_parser_abort_tentative_parse (parser);
6128 /* If we get here, then it's not a cast, so it must be a
6129 unary-expression. */
6130 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6133 /* Parse a binary expression of the general form:
6137 pm-expression .* cast-expression
6138 pm-expression ->* cast-expression
6140 multiplicative-expression:
6142 multiplicative-expression * pm-expression
6143 multiplicative-expression / pm-expression
6144 multiplicative-expression % pm-expression
6146 additive-expression:
6147 multiplicative-expression
6148 additive-expression + multiplicative-expression
6149 additive-expression - multiplicative-expression
6153 shift-expression << additive-expression
6154 shift-expression >> additive-expression
6156 relational-expression:
6158 relational-expression < shift-expression
6159 relational-expression > shift-expression
6160 relational-expression <= shift-expression
6161 relational-expression >= shift-expression
6165 relational-expression:
6166 relational-expression <? shift-expression
6167 relational-expression >? shift-expression
6169 equality-expression:
6170 relational-expression
6171 equality-expression == relational-expression
6172 equality-expression != relational-expression
6176 and-expression & equality-expression
6178 exclusive-or-expression:
6180 exclusive-or-expression ^ and-expression
6182 inclusive-or-expression:
6183 exclusive-or-expression
6184 inclusive-or-expression | exclusive-or-expression
6186 logical-and-expression:
6187 inclusive-or-expression
6188 logical-and-expression && inclusive-or-expression
6190 logical-or-expression:
6191 logical-and-expression
6192 logical-or-expression || logical-and-expression
6194 All these are implemented with a single function like:
6197 simple-cast-expression
6198 binary-expression <token> binary-expression
6200 CAST_P is true if this expression is the target of a cast.
6202 The binops_by_token map is used to get the tree codes for each <token> type.
6203 binary-expressions are associated according to a precedence table. */
6205 #define TOKEN_PRECEDENCE(token) \
6206 (((token->type == CPP_GREATER \
6207 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6208 && !parser->greater_than_is_operator_p) \
6209 ? PREC_NOT_OPERATOR \
6210 : binops_by_token[token->type].prec)
6213 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6214 bool no_toplevel_fold_p,
6215 enum cp_parser_prec prec,
6218 cp_parser_expression_stack stack;
6219 cp_parser_expression_stack_entry *sp = &stack[0];
6222 enum tree_code tree_type, lhs_type, rhs_type;
6223 enum cp_parser_prec new_prec, lookahead_prec;
6226 /* Parse the first expression. */
6227 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6228 lhs_type = ERROR_MARK;
6232 /* Get an operator token. */
6233 token = cp_lexer_peek_token (parser->lexer);
6235 if (warn_cxx0x_compat
6236 && token->type == CPP_RSHIFT
6237 && !parser->greater_than_is_operator_p)
6239 warning (OPT_Wc__0x_compat,
6240 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6242 warning (OPT_Wc__0x_compat,
6243 "suggest parentheses around %<>>%> expression");
6246 new_prec = TOKEN_PRECEDENCE (token);
6248 /* Popping an entry off the stack means we completed a subexpression:
6249 - either we found a token which is not an operator (`>' where it is not
6250 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6251 will happen repeatedly;
6252 - or, we found an operator which has lower priority. This is the case
6253 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6255 if (new_prec <= prec)
6264 tree_type = binops_by_token[token->type].tree_type;
6266 /* We used the operator token. */
6267 cp_lexer_consume_token (parser->lexer);
6269 /* Extract another operand. It may be the RHS of this expression
6270 or the LHS of a new, higher priority expression. */
6271 rhs = cp_parser_simple_cast_expression (parser);
6272 rhs_type = ERROR_MARK;
6274 /* Get another operator token. Look up its precedence to avoid
6275 building a useless (immediately popped) stack entry for common
6276 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6277 token = cp_lexer_peek_token (parser->lexer);
6278 lookahead_prec = TOKEN_PRECEDENCE (token);
6279 if (lookahead_prec > new_prec)
6281 /* ... and prepare to parse the RHS of the new, higher priority
6282 expression. Since precedence levels on the stack are
6283 monotonically increasing, we do not have to care about
6286 sp->tree_type = tree_type;
6288 sp->lhs_type = lhs_type;
6291 lhs_type = rhs_type;
6293 new_prec = lookahead_prec;
6297 lookahead_prec = new_prec;
6298 /* If the stack is not empty, we have parsed into LHS the right side
6299 (`4' in the example above) of an expression we had suspended.
6300 We can use the information on the stack to recover the LHS (`3')
6301 from the stack together with the tree code (`MULT_EXPR'), and
6302 the precedence of the higher level subexpression
6303 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6304 which will be used to actually build the additive expression. */
6307 tree_type = sp->tree_type;
6309 rhs_type = lhs_type;
6311 lhs_type = sp->lhs_type;
6314 overloaded_p = false;
6315 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6316 ERROR_MARK for everything that is not a binary expression.
6317 This makes warn_about_parentheses miss some warnings that
6318 involve unary operators. For unary expressions we should
6319 pass the correct tree_code unless the unary expression was
6320 surrounded by parentheses.
6322 if (no_toplevel_fold_p
6323 && lookahead_prec <= prec
6325 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6326 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6328 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6329 &overloaded_p, tf_warning_or_error);
6330 lhs_type = tree_type;
6332 /* If the binary operator required the use of an overloaded operator,
6333 then this expression cannot be an integral constant-expression.
6334 An overloaded operator can be used even if both operands are
6335 otherwise permissible in an integral constant-expression if at
6336 least one of the operands is of enumeration type. */
6339 && (cp_parser_non_integral_constant_expression
6340 (parser, "calls to overloaded operators")))
6341 return error_mark_node;
6348 /* Parse the `? expression : assignment-expression' part of a
6349 conditional-expression. The LOGICAL_OR_EXPR is the
6350 logical-or-expression that started the conditional-expression.
6351 Returns a representation of the entire conditional-expression.
6353 This routine is used by cp_parser_assignment_expression.
6355 ? expression : assignment-expression
6359 ? : assignment-expression */
6362 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6365 tree assignment_expr;
6367 /* Consume the `?' token. */
6368 cp_lexer_consume_token (parser->lexer);
6369 if (cp_parser_allow_gnu_extensions_p (parser)
6370 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6371 /* Implicit true clause. */
6374 /* Parse the expression. */
6375 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6377 /* The next token should be a `:'. */
6378 cp_parser_require (parser, CPP_COLON, "%<:%>");
6379 /* Parse the assignment-expression. */
6380 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6382 /* Build the conditional-expression. */
6383 return build_x_conditional_expr (logical_or_expr,
6386 tf_warning_or_error);
6389 /* Parse an assignment-expression.
6391 assignment-expression:
6392 conditional-expression
6393 logical-or-expression assignment-operator assignment_expression
6396 CAST_P is true if this expression is the target of a cast.
6398 Returns a representation for the expression. */
6401 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6406 /* If the next token is the `throw' keyword, then we're looking at
6407 a throw-expression. */
6408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6409 expr = cp_parser_throw_expression (parser);
6410 /* Otherwise, it must be that we are looking at a
6411 logical-or-expression. */
6414 /* Parse the binary expressions (logical-or-expression). */
6415 expr = cp_parser_binary_expression (parser, cast_p, false,
6416 PREC_NOT_OPERATOR, pidk);
6417 /* If the next token is a `?' then we're actually looking at a
6418 conditional-expression. */
6419 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6420 return cp_parser_question_colon_clause (parser, expr);
6423 enum tree_code assignment_operator;
6425 /* If it's an assignment-operator, we're using the second
6428 = cp_parser_assignment_operator_opt (parser);
6429 if (assignment_operator != ERROR_MARK)
6431 bool non_constant_p;
6433 /* Parse the right-hand side of the assignment. */
6434 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6436 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6437 maybe_warn_cpp0x ("extended initializer lists");
6439 /* An assignment may not appear in a
6440 constant-expression. */
6441 if (cp_parser_non_integral_constant_expression (parser,
6443 return error_mark_node;
6444 /* Build the assignment expression. */
6445 expr = build_x_modify_expr (expr,
6446 assignment_operator,
6448 tf_warning_or_error);
6456 /* Parse an (optional) assignment-operator.
6458 assignment-operator: one of
6459 = *= /= %= += -= >>= <<= &= ^= |=
6463 assignment-operator: one of
6466 If the next token is an assignment operator, the corresponding tree
6467 code is returned, and the token is consumed. For example, for
6468 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6469 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6470 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6471 operator, ERROR_MARK is returned. */
6473 static enum tree_code
6474 cp_parser_assignment_operator_opt (cp_parser* parser)
6479 /* Peek at the next token. */
6480 token = cp_lexer_peek_token (parser->lexer);
6482 switch (token->type)
6493 op = TRUNC_DIV_EXPR;
6497 op = TRUNC_MOD_EXPR;
6529 /* Nothing else is an assignment operator. */
6533 /* If it was an assignment operator, consume it. */
6534 if (op != ERROR_MARK)
6535 cp_lexer_consume_token (parser->lexer);
6540 /* Parse an expression.
6543 assignment-expression
6544 expression , assignment-expression
6546 CAST_P is true if this expression is the target of a cast.
6548 Returns a representation of the expression. */
6551 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6553 tree expression = NULL_TREE;
6557 tree assignment_expression;
6559 /* Parse the next assignment-expression. */
6560 assignment_expression
6561 = cp_parser_assignment_expression (parser, cast_p, pidk);
6562 /* If this is the first assignment-expression, we can just
6565 expression = assignment_expression;
6567 expression = build_x_compound_expr (expression,
6568 assignment_expression,
6569 tf_warning_or_error);
6570 /* If the next token is not a comma, then we are done with the
6572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6574 /* Consume the `,'. */
6575 cp_lexer_consume_token (parser->lexer);
6576 /* A comma operator cannot appear in a constant-expression. */
6577 if (cp_parser_non_integral_constant_expression (parser,
6578 "a comma operator"))
6579 expression = error_mark_node;
6585 /* Parse a constant-expression.
6587 constant-expression:
6588 conditional-expression
6590 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6591 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6592 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6593 is false, NON_CONSTANT_P should be NULL. */
6596 cp_parser_constant_expression (cp_parser* parser,
6597 bool allow_non_constant_p,
6598 bool *non_constant_p)
6600 bool saved_integral_constant_expression_p;
6601 bool saved_allow_non_integral_constant_expression_p;
6602 bool saved_non_integral_constant_expression_p;
6605 /* It might seem that we could simply parse the
6606 conditional-expression, and then check to see if it were
6607 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6608 one that the compiler can figure out is constant, possibly after
6609 doing some simplifications or optimizations. The standard has a
6610 precise definition of constant-expression, and we must honor
6611 that, even though it is somewhat more restrictive.
6617 is not a legal declaration, because `(2, 3)' is not a
6618 constant-expression. The `,' operator is forbidden in a
6619 constant-expression. However, GCC's constant-folding machinery
6620 will fold this operation to an INTEGER_CST for `3'. */
6622 /* Save the old settings. */
6623 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6624 saved_allow_non_integral_constant_expression_p
6625 = parser->allow_non_integral_constant_expression_p;
6626 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6627 /* We are now parsing a constant-expression. */
6628 parser->integral_constant_expression_p = true;
6629 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6630 parser->non_integral_constant_expression_p = false;
6631 /* Although the grammar says "conditional-expression", we parse an
6632 "assignment-expression", which also permits "throw-expression"
6633 and the use of assignment operators. In the case that
6634 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6635 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6636 actually essential that we look for an assignment-expression.
6637 For example, cp_parser_initializer_clauses uses this function to
6638 determine whether a particular assignment-expression is in fact
6640 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6641 /* Restore the old settings. */
6642 parser->integral_constant_expression_p
6643 = saved_integral_constant_expression_p;
6644 parser->allow_non_integral_constant_expression_p
6645 = saved_allow_non_integral_constant_expression_p;
6646 if (allow_non_constant_p)
6647 *non_constant_p = parser->non_integral_constant_expression_p;
6648 else if (parser->non_integral_constant_expression_p)
6649 expression = error_mark_node;
6650 parser->non_integral_constant_expression_p
6651 = saved_non_integral_constant_expression_p;
6656 /* Parse __builtin_offsetof.
6658 offsetof-expression:
6659 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6661 offsetof-member-designator:
6663 | offsetof-member-designator "." id-expression
6664 | offsetof-member-designator "[" expression "]"
6665 | offsetof-member-designator "->" id-expression */
6668 cp_parser_builtin_offsetof (cp_parser *parser)
6670 int save_ice_p, save_non_ice_p;
6675 /* We're about to accept non-integral-constant things, but will
6676 definitely yield an integral constant expression. Save and
6677 restore these values around our local parsing. */
6678 save_ice_p = parser->integral_constant_expression_p;
6679 save_non_ice_p = parser->non_integral_constant_expression_p;
6681 /* Consume the "__builtin_offsetof" token. */
6682 cp_lexer_consume_token (parser->lexer);
6683 /* Consume the opening `('. */
6684 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6685 /* Parse the type-id. */
6686 type = cp_parser_type_id (parser);
6687 /* Look for the `,'. */
6688 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6689 token = cp_lexer_peek_token (parser->lexer);
6691 /* Build the (type *)null that begins the traditional offsetof macro. */
6692 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6693 tf_warning_or_error);
6695 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6696 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6697 true, &dummy, token->location);
6700 token = cp_lexer_peek_token (parser->lexer);
6701 switch (token->type)
6703 case CPP_OPEN_SQUARE:
6704 /* offsetof-member-designator "[" expression "]" */
6705 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6709 /* offsetof-member-designator "->" identifier */
6710 expr = grok_array_decl (expr, integer_zero_node);
6714 /* offsetof-member-designator "." identifier */
6715 cp_lexer_consume_token (parser->lexer);
6716 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6721 case CPP_CLOSE_PAREN:
6722 /* Consume the ")" token. */
6723 cp_lexer_consume_token (parser->lexer);
6727 /* Error. We know the following require will fail, but
6728 that gives the proper error message. */
6729 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6730 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6731 expr = error_mark_node;
6737 /* If we're processing a template, we can't finish the semantics yet.
6738 Otherwise we can fold the entire expression now. */
6739 if (processing_template_decl)
6740 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6742 expr = finish_offsetof (expr);
6745 parser->integral_constant_expression_p = save_ice_p;
6746 parser->non_integral_constant_expression_p = save_non_ice_p;
6751 /* Parse a trait expression. */
6754 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6757 tree type1, type2 = NULL_TREE;
6758 bool binary = false;
6759 cp_decl_specifier_seq decl_specs;
6763 case RID_HAS_NOTHROW_ASSIGN:
6764 kind = CPTK_HAS_NOTHROW_ASSIGN;
6766 case RID_HAS_NOTHROW_CONSTRUCTOR:
6767 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6769 case RID_HAS_NOTHROW_COPY:
6770 kind = CPTK_HAS_NOTHROW_COPY;
6772 case RID_HAS_TRIVIAL_ASSIGN:
6773 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6775 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6776 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6778 case RID_HAS_TRIVIAL_COPY:
6779 kind = CPTK_HAS_TRIVIAL_COPY;
6781 case RID_HAS_TRIVIAL_DESTRUCTOR:
6782 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6784 case RID_HAS_VIRTUAL_DESTRUCTOR:
6785 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6787 case RID_IS_ABSTRACT:
6788 kind = CPTK_IS_ABSTRACT;
6790 case RID_IS_BASE_OF:
6791 kind = CPTK_IS_BASE_OF;
6795 kind = CPTK_IS_CLASS;
6797 case RID_IS_CONVERTIBLE_TO:
6798 kind = CPTK_IS_CONVERTIBLE_TO;
6802 kind = CPTK_IS_EMPTY;
6805 kind = CPTK_IS_ENUM;
6810 case RID_IS_POLYMORPHIC:
6811 kind = CPTK_IS_POLYMORPHIC;
6814 kind = CPTK_IS_UNION;
6820 /* Consume the token. */
6821 cp_lexer_consume_token (parser->lexer);
6823 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6825 type1 = cp_parser_type_id (parser);
6827 if (type1 == error_mark_node)
6828 return error_mark_node;
6830 /* Build a trivial decl-specifier-seq. */
6831 clear_decl_specs (&decl_specs);
6832 decl_specs.type = type1;
6834 /* Call grokdeclarator to figure out what type this is. */
6835 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6836 /*initialized=*/0, /*attrlist=*/NULL);
6840 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6842 type2 = cp_parser_type_id (parser);
6844 if (type2 == error_mark_node)
6845 return error_mark_node;
6847 /* Build a trivial decl-specifier-seq. */
6848 clear_decl_specs (&decl_specs);
6849 decl_specs.type = type2;
6851 /* Call grokdeclarator to figure out what type this is. */
6852 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6853 /*initialized=*/0, /*attrlist=*/NULL);
6856 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6858 /* Complete the trait expression, which may mean either processing
6859 the trait expr now or saving it for template instantiation. */
6860 return finish_trait_expr (kind, type1, type2);
6863 /* Statements [gram.stmt.stmt] */
6865 /* Parse a statement.
6869 expression-statement
6874 declaration-statement
6877 IN_COMPOUND is true when the statement is nested inside a
6878 cp_parser_compound_statement; this matters for certain pragmas.
6880 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6881 is a (possibly labeled) if statement which is not enclosed in braces
6882 and has an else clause. This is used to implement -Wparentheses. */
6885 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6886 bool in_compound, bool *if_p)
6890 location_t statement_location;
6895 /* There is no statement yet. */
6896 statement = NULL_TREE;
6897 /* Peek at the next token. */
6898 token = cp_lexer_peek_token (parser->lexer);
6899 /* Remember the location of the first token in the statement. */
6900 statement_location = token->location;
6901 /* If this is a keyword, then that will often determine what kind of
6902 statement we have. */
6903 if (token->type == CPP_KEYWORD)
6905 enum rid keyword = token->keyword;
6911 /* Looks like a labeled-statement with a case label.
6912 Parse the label, and then use tail recursion to parse
6914 cp_parser_label_for_labeled_statement (parser);
6919 statement = cp_parser_selection_statement (parser, if_p);
6925 statement = cp_parser_iteration_statement (parser);
6932 statement = cp_parser_jump_statement (parser);
6935 /* Objective-C++ exception-handling constructs. */
6938 case RID_AT_FINALLY:
6939 case RID_AT_SYNCHRONIZED:
6941 statement = cp_parser_objc_statement (parser);
6945 statement = cp_parser_try_block (parser);
6949 /* This must be a namespace alias definition. */
6950 cp_parser_declaration_statement (parser);
6954 /* It might be a keyword like `int' that can start a
6955 declaration-statement. */
6959 else if (token->type == CPP_NAME)
6961 /* If the next token is a `:', then we are looking at a
6962 labeled-statement. */
6963 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6964 if (token->type == CPP_COLON)
6966 /* Looks like a labeled-statement with an ordinary label.
6967 Parse the label, and then use tail recursion to parse
6969 cp_parser_label_for_labeled_statement (parser);
6973 /* Anything that starts with a `{' must be a compound-statement. */
6974 else if (token->type == CPP_OPEN_BRACE)
6975 statement = cp_parser_compound_statement (parser, NULL, false);
6976 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6977 a statement all its own. */
6978 else if (token->type == CPP_PRAGMA)
6980 /* Only certain OpenMP pragmas are attached to statements, and thus
6981 are considered statements themselves. All others are not. In
6982 the context of a compound, accept the pragma as a "statement" and
6983 return so that we can check for a close brace. Otherwise we
6984 require a real statement and must go back and read one. */
6986 cp_parser_pragma (parser, pragma_compound);
6987 else if (!cp_parser_pragma (parser, pragma_stmt))
6991 else if (token->type == CPP_EOF)
6993 cp_parser_error (parser, "expected statement");
6997 /* Everything else must be a declaration-statement or an
6998 expression-statement. Try for the declaration-statement
6999 first, unless we are looking at a `;', in which case we know that
7000 we have an expression-statement. */
7003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7005 cp_parser_parse_tentatively (parser);
7006 /* Try to parse the declaration-statement. */
7007 cp_parser_declaration_statement (parser);
7008 /* If that worked, we're done. */
7009 if (cp_parser_parse_definitely (parser))
7012 /* Look for an expression-statement instead. */
7013 statement = cp_parser_expression_statement (parser, in_statement_expr);
7016 /* Set the line number for the statement. */
7017 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7018 SET_EXPR_LOCATION (statement, statement_location);
7021 /* Parse the label for a labeled-statement, i.e.
7024 case constant-expression :
7028 case constant-expression ... constant-expression : statement
7030 When a label is parsed without errors, the label is added to the
7031 parse tree by the finish_* functions, so this function doesn't
7032 have to return the label. */
7035 cp_parser_label_for_labeled_statement (cp_parser* parser)
7039 /* The next token should be an identifier. */
7040 token = cp_lexer_peek_token (parser->lexer);
7041 if (token->type != CPP_NAME
7042 && token->type != CPP_KEYWORD)
7044 cp_parser_error (parser, "expected labeled-statement");
7048 switch (token->keyword)
7055 /* Consume the `case' token. */
7056 cp_lexer_consume_token (parser->lexer);
7057 /* Parse the constant-expression. */
7058 expr = cp_parser_constant_expression (parser,
7059 /*allow_non_constant_p=*/false,
7062 ellipsis = cp_lexer_peek_token (parser->lexer);
7063 if (ellipsis->type == CPP_ELLIPSIS)
7065 /* Consume the `...' token. */
7066 cp_lexer_consume_token (parser->lexer);
7068 cp_parser_constant_expression (parser,
7069 /*allow_non_constant_p=*/false,
7071 /* We don't need to emit warnings here, as the common code
7072 will do this for us. */
7075 expr_hi = NULL_TREE;
7077 if (parser->in_switch_statement_p)
7078 finish_case_label (expr, expr_hi);
7080 error ("%Hcase label %qE not within a switch statement",
7081 &token->location, expr);
7086 /* Consume the `default' token. */
7087 cp_lexer_consume_token (parser->lexer);
7089 if (parser->in_switch_statement_p)
7090 finish_case_label (NULL_TREE, NULL_TREE);
7092 error ("%Hcase label not within a switch statement", &token->location);
7096 /* Anything else must be an ordinary label. */
7097 finish_label_stmt (cp_parser_identifier (parser));
7101 /* Require the `:' token. */
7102 cp_parser_require (parser, CPP_COLON, "%<:%>");
7105 /* Parse an expression-statement.
7107 expression-statement:
7110 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7111 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7112 indicates whether this expression-statement is part of an
7113 expression statement. */
7116 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7118 tree statement = NULL_TREE;
7120 /* If the next token is a ';', then there is no expression
7122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7123 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7125 /* Consume the final `;'. */
7126 cp_parser_consume_semicolon_at_end_of_statement (parser);
7128 if (in_statement_expr
7129 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7130 /* This is the final expression statement of a statement
7132 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7134 statement = finish_expr_stmt (statement);
7141 /* Parse a compound-statement.
7144 { statement-seq [opt] }
7149 { label-declaration-seq [opt] statement-seq [opt] }
7151 label-declaration-seq:
7153 label-declaration-seq label-declaration
7155 Returns a tree representing the statement. */
7158 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7163 /* Consume the `{'. */
7164 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7165 return error_mark_node;
7166 /* Begin the compound-statement. */
7167 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7168 /* If the next keyword is `__label__' we have a label declaration. */
7169 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7170 cp_parser_label_declaration (parser);
7171 /* Parse an (optional) statement-seq. */
7172 cp_parser_statement_seq_opt (parser, in_statement_expr);
7173 /* Finish the compound-statement. */
7174 finish_compound_stmt (compound_stmt);
7175 /* Consume the `}'. */
7176 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7178 return compound_stmt;
7181 /* Parse an (optional) statement-seq.
7185 statement-seq [opt] statement */
7188 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7190 /* Scan statements until there aren't any more. */
7193 cp_token *token = cp_lexer_peek_token (parser->lexer);
7195 /* If we're looking at a `}', then we've run out of statements. */
7196 if (token->type == CPP_CLOSE_BRACE
7197 || token->type == CPP_EOF
7198 || token->type == CPP_PRAGMA_EOL)
7201 /* If we are in a compound statement and find 'else' then
7202 something went wrong. */
7203 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7205 if (parser->in_statement & IN_IF_STMT)
7209 token = cp_lexer_consume_token (parser->lexer);
7210 error ("%H%<else%> without a previous %<if%>", &token->location);
7214 /* Parse the statement. */
7215 cp_parser_statement (parser, in_statement_expr, true, NULL);
7219 /* Parse a selection-statement.
7221 selection-statement:
7222 if ( condition ) statement
7223 if ( condition ) statement else statement
7224 switch ( condition ) statement
7226 Returns the new IF_STMT or SWITCH_STMT.
7228 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7229 is a (possibly labeled) if statement which is not enclosed in
7230 braces and has an else clause. This is used to implement
7234 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7242 /* Peek at the next token. */
7243 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7245 /* See what kind of keyword it is. */
7246 keyword = token->keyword;
7255 /* Look for the `('. */
7256 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7258 cp_parser_skip_to_end_of_statement (parser);
7259 return error_mark_node;
7262 /* Begin the selection-statement. */
7263 if (keyword == RID_IF)
7264 statement = begin_if_stmt ();
7266 statement = begin_switch_stmt ();
7268 /* Parse the condition. */
7269 condition = cp_parser_condition (parser);
7270 /* Look for the `)'. */
7271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7272 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7273 /*consume_paren=*/true);
7275 if (keyword == RID_IF)
7278 unsigned char in_statement;
7280 /* Add the condition. */
7281 finish_if_stmt_cond (condition, statement);
7283 /* Parse the then-clause. */
7284 in_statement = parser->in_statement;
7285 parser->in_statement |= IN_IF_STMT;
7286 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7288 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7289 add_stmt (build_empty_stmt ());
7290 cp_lexer_consume_token (parser->lexer);
7291 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7292 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7293 "empty body in an %<if%> statement");
7297 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7298 parser->in_statement = in_statement;
7300 finish_then_clause (statement);
7302 /* If the next token is `else', parse the else-clause. */
7303 if (cp_lexer_next_token_is_keyword (parser->lexer,
7306 /* Consume the `else' keyword. */
7307 cp_lexer_consume_token (parser->lexer);
7308 begin_else_clause (statement);
7309 /* Parse the else-clause. */
7310 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7312 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7313 OPT_Wempty_body, "suggest braces around "
7314 "empty body in an %<else%> statement");
7315 add_stmt (build_empty_stmt ());
7316 cp_lexer_consume_token (parser->lexer);
7319 cp_parser_implicitly_scoped_statement (parser, NULL);
7321 finish_else_clause (statement);
7323 /* If we are currently parsing a then-clause, then
7324 IF_P will not be NULL. We set it to true to
7325 indicate that this if statement has an else clause.
7326 This may trigger the Wparentheses warning below
7327 when we get back up to the parent if statement. */
7333 /* This if statement does not have an else clause. If
7334 NESTED_IF is true, then the then-clause is an if
7335 statement which does have an else clause. We warn
7336 about the potential ambiguity. */
7338 warning (OPT_Wparentheses,
7339 ("%Hsuggest explicit braces "
7340 "to avoid ambiguous %<else%>"),
7341 EXPR_LOCUS (statement));
7344 /* Now we're all done with the if-statement. */
7345 finish_if_stmt (statement);
7349 bool in_switch_statement_p;
7350 unsigned char in_statement;
7352 /* Add the condition. */
7353 finish_switch_cond (condition, statement);
7355 /* Parse the body of the switch-statement. */
7356 in_switch_statement_p = parser->in_switch_statement_p;
7357 in_statement = parser->in_statement;
7358 parser->in_switch_statement_p = true;
7359 parser->in_statement |= IN_SWITCH_STMT;
7360 cp_parser_implicitly_scoped_statement (parser, NULL);
7361 parser->in_switch_statement_p = in_switch_statement_p;
7362 parser->in_statement = in_statement;
7364 /* Now we're all done with the switch-statement. */
7365 finish_switch_stmt (statement);
7373 cp_parser_error (parser, "expected selection-statement");
7374 return error_mark_node;
7378 /* Parse a condition.
7382 type-specifier-seq declarator = initializer-clause
7383 type-specifier-seq declarator braced-init-list
7388 type-specifier-seq declarator asm-specification [opt]
7389 attributes [opt] = assignment-expression
7391 Returns the expression that should be tested. */
7394 cp_parser_condition (cp_parser* parser)
7396 cp_decl_specifier_seq type_specifiers;
7397 const char *saved_message;
7399 /* Try the declaration first. */
7400 cp_parser_parse_tentatively (parser);
7401 /* New types are not allowed in the type-specifier-seq for a
7403 saved_message = parser->type_definition_forbidden_message;
7404 parser->type_definition_forbidden_message
7405 = "types may not be defined in conditions";
7406 /* Parse the type-specifier-seq. */
7407 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7409 /* Restore the saved message. */
7410 parser->type_definition_forbidden_message = saved_message;
7411 /* If all is well, we might be looking at a declaration. */
7412 if (!cp_parser_error_occurred (parser))
7415 tree asm_specification;
7417 cp_declarator *declarator;
7418 tree initializer = NULL_TREE;
7420 /* Parse the declarator. */
7421 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7422 /*ctor_dtor_or_conv_p=*/NULL,
7423 /*parenthesized_p=*/NULL,
7424 /*member_p=*/false);
7425 /* Parse the attributes. */
7426 attributes = cp_parser_attributes_opt (parser);
7427 /* Parse the asm-specification. */
7428 asm_specification = cp_parser_asm_specification_opt (parser);
7429 /* If the next token is not an `=' or '{', then we might still be
7430 looking at an expression. For example:
7434 looks like a decl-specifier-seq and a declarator -- but then
7435 there is no `=', so this is an expression. */
7436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7437 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7438 cp_parser_simulate_error (parser);
7440 /* If we did see an `=' or '{', then we are looking at a declaration
7442 if (cp_parser_parse_definitely (parser))
7445 bool non_constant_p;
7446 bool flags = LOOKUP_ONLYCONVERTING;
7448 /* Create the declaration. */
7449 decl = start_decl (declarator, &type_specifiers,
7450 /*initialized_p=*/true,
7451 attributes, /*prefix_attributes=*/NULL_TREE,
7454 /* Parse the initializer. */
7455 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7457 initializer = cp_parser_braced_list (parser, &non_constant_p);
7458 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7463 /* Consume the `='. */
7464 cp_parser_require (parser, CPP_EQ, "%<=%>");
7465 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7467 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7468 maybe_warn_cpp0x ("extended initializer lists");
7470 if (!non_constant_p)
7471 initializer = fold_non_dependent_expr (initializer);
7473 /* Process the initializer. */
7474 cp_finish_decl (decl,
7475 initializer, !non_constant_p,
7480 pop_scope (pushed_scope);
7482 return convert_from_reference (decl);
7485 /* If we didn't even get past the declarator successfully, we are
7486 definitely not looking at a declaration. */
7488 cp_parser_abort_tentative_parse (parser);
7490 /* Otherwise, we are looking at an expression. */
7491 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7494 /* Parse an iteration-statement.
7496 iteration-statement:
7497 while ( condition ) statement
7498 do statement while ( expression ) ;
7499 for ( for-init-statement condition [opt] ; expression [opt] )
7502 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7505 cp_parser_iteration_statement (cp_parser* parser)
7510 unsigned char in_statement;
7512 /* Peek at the next token. */
7513 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7515 return error_mark_node;
7517 /* Remember whether or not we are already within an iteration
7519 in_statement = parser->in_statement;
7521 /* See what kind of keyword it is. */
7522 keyword = token->keyword;
7529 /* Begin the while-statement. */
7530 statement = begin_while_stmt ();
7531 /* Look for the `('. */
7532 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7533 /* Parse the condition. */
7534 condition = cp_parser_condition (parser);
7535 finish_while_stmt_cond (condition, statement);
7536 /* Look for the `)'. */
7537 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7538 /* Parse the dependent statement. */
7539 parser->in_statement = IN_ITERATION_STMT;
7540 cp_parser_already_scoped_statement (parser);
7541 parser->in_statement = in_statement;
7542 /* We're done with the while-statement. */
7543 finish_while_stmt (statement);
7551 /* Begin the do-statement. */
7552 statement = begin_do_stmt ();
7553 /* Parse the body of the do-statement. */
7554 parser->in_statement = IN_ITERATION_STMT;
7555 cp_parser_implicitly_scoped_statement (parser, NULL);
7556 parser->in_statement = in_statement;
7557 finish_do_body (statement);
7558 /* Look for the `while' keyword. */
7559 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7560 /* Look for the `('. */
7561 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7562 /* Parse the expression. */
7563 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7564 /* We're done with the do-statement. */
7565 finish_do_stmt (expression, statement);
7566 /* Look for the `)'. */
7567 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7568 /* Look for the `;'. */
7569 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7575 tree condition = NULL_TREE;
7576 tree expression = NULL_TREE;
7578 /* Begin the for-statement. */
7579 statement = begin_for_stmt ();
7580 /* Look for the `('. */
7581 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7582 /* Parse the initialization. */
7583 cp_parser_for_init_statement (parser);
7584 finish_for_init_stmt (statement);
7586 /* If there's a condition, process it. */
7587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7588 condition = cp_parser_condition (parser);
7589 finish_for_cond (condition, statement);
7590 /* Look for the `;'. */
7591 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7593 /* If there's an expression, process it. */
7594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7595 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7596 finish_for_expr (expression, statement);
7597 /* Look for the `)'. */
7598 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7600 /* Parse the body of the for-statement. */
7601 parser->in_statement = IN_ITERATION_STMT;
7602 cp_parser_already_scoped_statement (parser);
7603 parser->in_statement = in_statement;
7605 /* We're done with the for-statement. */
7606 finish_for_stmt (statement);
7611 cp_parser_error (parser, "expected iteration-statement");
7612 statement = error_mark_node;
7619 /* Parse a for-init-statement.
7622 expression-statement
7623 simple-declaration */
7626 cp_parser_for_init_statement (cp_parser* parser)
7628 /* If the next token is a `;', then we have an empty
7629 expression-statement. Grammatically, this is also a
7630 simple-declaration, but an invalid one, because it does not
7631 declare anything. Therefore, if we did not handle this case
7632 specially, we would issue an error message about an invalid
7634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7636 /* We're going to speculatively look for a declaration, falling back
7637 to an expression, if necessary. */
7638 cp_parser_parse_tentatively (parser);
7639 /* Parse the declaration. */
7640 cp_parser_simple_declaration (parser,
7641 /*function_definition_allowed_p=*/false);
7642 /* If the tentative parse failed, then we shall need to look for an
7643 expression-statement. */
7644 if (cp_parser_parse_definitely (parser))
7648 cp_parser_expression_statement (parser, false);
7651 /* Parse a jump-statement.
7656 return expression [opt] ;
7657 return braced-init-list ;
7665 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7668 cp_parser_jump_statement (cp_parser* parser)
7670 tree statement = error_mark_node;
7673 unsigned char in_statement;
7675 /* Peek at the next token. */
7676 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7678 return error_mark_node;
7680 /* See what kind of keyword it is. */
7681 keyword = token->keyword;
7685 in_statement = parser->in_statement & ~IN_IF_STMT;
7686 switch (in_statement)
7689 error ("%Hbreak statement not within loop or switch", &token->location);
7692 gcc_assert ((in_statement & IN_SWITCH_STMT)
7693 || in_statement == IN_ITERATION_STMT);
7694 statement = finish_break_stmt ();
7697 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7700 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7703 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7707 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7710 error ("%Hcontinue statement not within a loop", &token->location);
7712 case IN_ITERATION_STMT:
7714 statement = finish_continue_stmt ();
7717 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7722 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7728 bool expr_non_constant_p;
7730 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7732 maybe_warn_cpp0x ("extended initializer lists");
7733 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7735 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7736 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7738 /* If the next token is a `;', then there is no
7741 /* Build the return-statement. */
7742 statement = finish_return_stmt (expr);
7743 /* Look for the final `;'. */
7744 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7749 /* Create the goto-statement. */
7750 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7752 /* Issue a warning about this use of a GNU extension. */
7753 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7754 /* Consume the '*' token. */
7755 cp_lexer_consume_token (parser->lexer);
7756 /* Parse the dependent expression. */
7757 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7760 finish_goto_stmt (cp_parser_identifier (parser));
7761 /* Look for the final `;'. */
7762 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7766 cp_parser_error (parser, "expected jump-statement");
7773 /* Parse a declaration-statement.
7775 declaration-statement:
7776 block-declaration */
7779 cp_parser_declaration_statement (cp_parser* parser)
7783 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7784 p = obstack_alloc (&declarator_obstack, 0);
7786 /* Parse the block-declaration. */
7787 cp_parser_block_declaration (parser, /*statement_p=*/true);
7789 /* Free any declarators allocated. */
7790 obstack_free (&declarator_obstack, p);
7792 /* Finish off the statement. */
7796 /* Some dependent statements (like `if (cond) statement'), are
7797 implicitly in their own scope. In other words, if the statement is
7798 a single statement (as opposed to a compound-statement), it is
7799 none-the-less treated as if it were enclosed in braces. Any
7800 declarations appearing in the dependent statement are out of scope
7801 after control passes that point. This function parses a statement,
7802 but ensures that is in its own scope, even if it is not a
7805 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7806 is a (possibly labeled) if statement which is not enclosed in
7807 braces and has an else clause. This is used to implement
7810 Returns the new statement. */
7813 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7820 /* Mark if () ; with a special NOP_EXPR. */
7821 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7823 cp_lexer_consume_token (parser->lexer);
7824 statement = add_stmt (build_empty_stmt ());
7826 /* if a compound is opened, we simply parse the statement directly. */
7827 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7828 statement = cp_parser_compound_statement (parser, NULL, false);
7829 /* If the token is not a `{', then we must take special action. */
7832 /* Create a compound-statement. */
7833 statement = begin_compound_stmt (0);
7834 /* Parse the dependent-statement. */
7835 cp_parser_statement (parser, NULL_TREE, false, if_p);
7836 /* Finish the dummy compound-statement. */
7837 finish_compound_stmt (statement);
7840 /* Return the statement. */
7844 /* For some dependent statements (like `while (cond) statement'), we
7845 have already created a scope. Therefore, even if the dependent
7846 statement is a compound-statement, we do not want to create another
7850 cp_parser_already_scoped_statement (cp_parser* parser)
7852 /* If the token is a `{', then we must take special action. */
7853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7854 cp_parser_statement (parser, NULL_TREE, false, NULL);
7857 /* Avoid calling cp_parser_compound_statement, so that we
7858 don't create a new scope. Do everything else by hand. */
7859 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7860 /* If the next keyword is `__label__' we have a label declaration. */
7861 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7862 cp_parser_label_declaration (parser);
7863 /* Parse an (optional) statement-seq. */
7864 cp_parser_statement_seq_opt (parser, NULL_TREE);
7865 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7869 /* Declarations [gram.dcl.dcl] */
7871 /* Parse an optional declaration-sequence.
7875 declaration-seq declaration */
7878 cp_parser_declaration_seq_opt (cp_parser* parser)
7884 token = cp_lexer_peek_token (parser->lexer);
7886 if (token->type == CPP_CLOSE_BRACE
7887 || token->type == CPP_EOF
7888 || token->type == CPP_PRAGMA_EOL)
7891 if (token->type == CPP_SEMICOLON)
7893 /* A declaration consisting of a single semicolon is
7894 invalid. Allow it unless we're being pedantic. */
7895 cp_lexer_consume_token (parser->lexer);
7896 if (!in_system_header)
7897 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7901 /* If we're entering or exiting a region that's implicitly
7902 extern "C", modify the lang context appropriately. */
7903 if (!parser->implicit_extern_c && token->implicit_extern_c)
7905 push_lang_context (lang_name_c);
7906 parser->implicit_extern_c = true;
7908 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7910 pop_lang_context ();
7911 parser->implicit_extern_c = false;
7914 if (token->type == CPP_PRAGMA)
7916 /* A top-level declaration can consist solely of a #pragma.
7917 A nested declaration cannot, so this is done here and not
7918 in cp_parser_declaration. (A #pragma at block scope is
7919 handled in cp_parser_statement.) */
7920 cp_parser_pragma (parser, pragma_external);
7924 /* Parse the declaration itself. */
7925 cp_parser_declaration (parser);
7929 /* Parse a declaration.
7934 template-declaration
7935 explicit-instantiation
7936 explicit-specialization
7937 linkage-specification
7938 namespace-definition
7943 __extension__ declaration */
7946 cp_parser_declaration (cp_parser* parser)
7953 /* Check for the `__extension__' keyword. */
7954 if (cp_parser_extension_opt (parser, &saved_pedantic))
7956 /* Parse the qualified declaration. */
7957 cp_parser_declaration (parser);
7958 /* Restore the PEDANTIC flag. */
7959 pedantic = saved_pedantic;
7964 /* Try to figure out what kind of declaration is present. */
7965 token1 = *cp_lexer_peek_token (parser->lexer);
7967 if (token1.type != CPP_EOF)
7968 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7971 token2.type = CPP_EOF;
7972 token2.keyword = RID_MAX;
7975 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7976 p = obstack_alloc (&declarator_obstack, 0);
7978 /* If the next token is `extern' and the following token is a string
7979 literal, then we have a linkage specification. */
7980 if (token1.keyword == RID_EXTERN
7981 && cp_parser_is_string_literal (&token2))
7982 cp_parser_linkage_specification (parser);
7983 /* If the next token is `template', then we have either a template
7984 declaration, an explicit instantiation, or an explicit
7986 else if (token1.keyword == RID_TEMPLATE)
7988 /* `template <>' indicates a template specialization. */
7989 if (token2.type == CPP_LESS
7990 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7991 cp_parser_explicit_specialization (parser);
7992 /* `template <' indicates a template declaration. */
7993 else if (token2.type == CPP_LESS)
7994 cp_parser_template_declaration (parser, /*member_p=*/false);
7995 /* Anything else must be an explicit instantiation. */
7997 cp_parser_explicit_instantiation (parser);
7999 /* If the next token is `export', then we have a template
8001 else if (token1.keyword == RID_EXPORT)
8002 cp_parser_template_declaration (parser, /*member_p=*/false);
8003 /* If the next token is `extern', 'static' or 'inline' and the one
8004 after that is `template', we have a GNU extended explicit
8005 instantiation directive. */
8006 else if (cp_parser_allow_gnu_extensions_p (parser)
8007 && (token1.keyword == RID_EXTERN
8008 || token1.keyword == RID_STATIC
8009 || token1.keyword == RID_INLINE)
8010 && token2.keyword == RID_TEMPLATE)
8011 cp_parser_explicit_instantiation (parser);
8012 /* If the next token is `namespace', check for a named or unnamed
8013 namespace definition. */
8014 else if (token1.keyword == RID_NAMESPACE
8015 && (/* A named namespace definition. */
8016 (token2.type == CPP_NAME
8017 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8019 /* An unnamed namespace definition. */
8020 || token2.type == CPP_OPEN_BRACE
8021 || token2.keyword == RID_ATTRIBUTE))
8022 cp_parser_namespace_definition (parser);
8023 /* An inline (associated) namespace definition. */
8024 else if (token1.keyword == RID_INLINE
8025 && token2.keyword == RID_NAMESPACE)
8026 cp_parser_namespace_definition (parser);
8027 /* Objective-C++ declaration/definition. */
8028 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8029 cp_parser_objc_declaration (parser);
8030 /* We must have either a block declaration or a function
8033 /* Try to parse a block-declaration, or a function-definition. */
8034 cp_parser_block_declaration (parser, /*statement_p=*/false);
8036 /* Free any declarators allocated. */
8037 obstack_free (&declarator_obstack, p);
8040 /* Parse a block-declaration.
8045 namespace-alias-definition
8052 __extension__ block-declaration
8057 static_assert-declaration
8059 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8060 part of a declaration-statement. */
8063 cp_parser_block_declaration (cp_parser *parser,
8069 /* Check for the `__extension__' keyword. */
8070 if (cp_parser_extension_opt (parser, &saved_pedantic))
8072 /* Parse the qualified declaration. */
8073 cp_parser_block_declaration (parser, statement_p);
8074 /* Restore the PEDANTIC flag. */
8075 pedantic = saved_pedantic;
8080 /* Peek at the next token to figure out which kind of declaration is
8082 token1 = cp_lexer_peek_token (parser->lexer);
8084 /* If the next keyword is `asm', we have an asm-definition. */
8085 if (token1->keyword == RID_ASM)
8088 cp_parser_commit_to_tentative_parse (parser);
8089 cp_parser_asm_definition (parser);
8091 /* If the next keyword is `namespace', we have a
8092 namespace-alias-definition. */
8093 else if (token1->keyword == RID_NAMESPACE)
8094 cp_parser_namespace_alias_definition (parser);
8095 /* If the next keyword is `using', we have either a
8096 using-declaration or a using-directive. */
8097 else if (token1->keyword == RID_USING)
8102 cp_parser_commit_to_tentative_parse (parser);
8103 /* If the token after `using' is `namespace', then we have a
8105 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8106 if (token2->keyword == RID_NAMESPACE)
8107 cp_parser_using_directive (parser);
8108 /* Otherwise, it's a using-declaration. */
8110 cp_parser_using_declaration (parser,
8111 /*access_declaration_p=*/false);
8113 /* If the next keyword is `__label__' we have a misplaced label
8115 else if (token1->keyword == RID_LABEL)
8117 cp_lexer_consume_token (parser->lexer);
8118 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8119 cp_parser_skip_to_end_of_statement (parser);
8120 /* If the next token is now a `;', consume it. */
8121 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8122 cp_lexer_consume_token (parser->lexer);
8124 /* If the next token is `static_assert' we have a static assertion. */
8125 else if (token1->keyword == RID_STATIC_ASSERT)
8126 cp_parser_static_assert (parser, /*member_p=*/false);
8127 /* Anything else must be a simple-declaration. */
8129 cp_parser_simple_declaration (parser, !statement_p);
8132 /* Parse a simple-declaration.
8135 decl-specifier-seq [opt] init-declarator-list [opt] ;
8137 init-declarator-list:
8139 init-declarator-list , init-declarator
8141 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8142 function-definition as a simple-declaration. */
8145 cp_parser_simple_declaration (cp_parser* parser,
8146 bool function_definition_allowed_p)
8148 cp_decl_specifier_seq decl_specifiers;
8149 int declares_class_or_enum;
8150 bool saw_declarator;
8152 /* Defer access checks until we know what is being declared; the
8153 checks for names appearing in the decl-specifier-seq should be
8154 done as if we were in the scope of the thing being declared. */
8155 push_deferring_access_checks (dk_deferred);
8157 /* Parse the decl-specifier-seq. We have to keep track of whether
8158 or not the decl-specifier-seq declares a named class or
8159 enumeration type, since that is the only case in which the
8160 init-declarator-list is allowed to be empty.
8164 In a simple-declaration, the optional init-declarator-list can be
8165 omitted only when declaring a class or enumeration, that is when
8166 the decl-specifier-seq contains either a class-specifier, an
8167 elaborated-type-specifier, or an enum-specifier. */
8168 cp_parser_decl_specifier_seq (parser,
8169 CP_PARSER_FLAGS_OPTIONAL,
8171 &declares_class_or_enum);
8172 /* We no longer need to defer access checks. */
8173 stop_deferring_access_checks ();
8175 /* In a block scope, a valid declaration must always have a
8176 decl-specifier-seq. By not trying to parse declarators, we can
8177 resolve the declaration/expression ambiguity more quickly. */
8178 if (!function_definition_allowed_p
8179 && !decl_specifiers.any_specifiers_p)
8181 cp_parser_error (parser, "expected declaration");
8185 /* If the next two tokens are both identifiers, the code is
8186 erroneous. The usual cause of this situation is code like:
8190 where "T" should name a type -- but does not. */
8191 if (!decl_specifiers.type
8192 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8194 /* If parsing tentatively, we should commit; we really are
8195 looking at a declaration. */
8196 cp_parser_commit_to_tentative_parse (parser);
8201 /* If we have seen at least one decl-specifier, and the next token
8202 is not a parenthesis, then we must be looking at a declaration.
8203 (After "int (" we might be looking at a functional cast.) */
8204 if (decl_specifiers.any_specifiers_p
8205 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8206 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8207 && !cp_parser_error_occurred (parser))
8208 cp_parser_commit_to_tentative_parse (parser);
8210 /* Keep going until we hit the `;' at the end of the simple
8212 saw_declarator = false;
8213 while (cp_lexer_next_token_is_not (parser->lexer,
8217 bool function_definition_p;
8222 /* If we are processing next declarator, coma is expected */
8223 token = cp_lexer_peek_token (parser->lexer);
8224 gcc_assert (token->type == CPP_COMMA);
8225 cp_lexer_consume_token (parser->lexer);
8228 saw_declarator = true;
8230 /* Parse the init-declarator. */
8231 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8233 function_definition_allowed_p,
8235 declares_class_or_enum,
8236 &function_definition_p);
8237 /* If an error occurred while parsing tentatively, exit quickly.
8238 (That usually happens when in the body of a function; each
8239 statement is treated as a declaration-statement until proven
8241 if (cp_parser_error_occurred (parser))
8243 /* Handle function definitions specially. */
8244 if (function_definition_p)
8246 /* If the next token is a `,', then we are probably
8247 processing something like:
8251 which is erroneous. */
8252 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8254 cp_token *token = cp_lexer_peek_token (parser->lexer);
8255 error ("%Hmixing declarations and function-definitions is forbidden",
8258 /* Otherwise, we're done with the list of declarators. */
8261 pop_deferring_access_checks ();
8265 /* The next token should be either a `,' or a `;'. */
8266 token = cp_lexer_peek_token (parser->lexer);
8267 /* If it's a `,', there are more declarators to come. */
8268 if (token->type == CPP_COMMA)
8269 /* will be consumed next time around */;
8270 /* If it's a `;', we are done. */
8271 else if (token->type == CPP_SEMICOLON)
8273 /* Anything else is an error. */
8276 /* If we have already issued an error message we don't need
8277 to issue another one. */
8278 if (decl != error_mark_node
8279 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8280 cp_parser_error (parser, "expected %<,%> or %<;%>");
8281 /* Skip tokens until we reach the end of the statement. */
8282 cp_parser_skip_to_end_of_statement (parser);
8283 /* If the next token is now a `;', consume it. */
8284 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8285 cp_lexer_consume_token (parser->lexer);
8288 /* After the first time around, a function-definition is not
8289 allowed -- even if it was OK at first. For example:
8294 function_definition_allowed_p = false;
8297 /* Issue an error message if no declarators are present, and the
8298 decl-specifier-seq does not itself declare a class or
8300 if (!saw_declarator)
8302 if (cp_parser_declares_only_class_p (parser))
8303 shadow_tag (&decl_specifiers);
8304 /* Perform any deferred access checks. */
8305 perform_deferred_access_checks ();
8308 /* Consume the `;'. */
8309 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8312 pop_deferring_access_checks ();
8315 /* Parse a decl-specifier-seq.
8318 decl-specifier-seq [opt] decl-specifier
8321 storage-class-specifier
8332 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8334 The parser flags FLAGS is used to control type-specifier parsing.
8336 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8339 1: one of the decl-specifiers is an elaborated-type-specifier
8340 (i.e., a type declaration)
8341 2: one of the decl-specifiers is an enum-specifier or a
8342 class-specifier (i.e., a type definition)
8347 cp_parser_decl_specifier_seq (cp_parser* parser,
8348 cp_parser_flags flags,
8349 cp_decl_specifier_seq *decl_specs,
8350 int* declares_class_or_enum)
8352 bool constructor_possible_p = !parser->in_declarator_p;
8353 cp_token *start_token = NULL;
8355 /* Clear DECL_SPECS. */
8356 clear_decl_specs (decl_specs);
8358 /* Assume no class or enumeration type is declared. */
8359 *declares_class_or_enum = 0;
8361 /* Keep reading specifiers until there are no more to read. */
8365 bool found_decl_spec;
8368 /* Peek at the next token. */
8369 token = cp_lexer_peek_token (parser->lexer);
8371 /* Save the first token of the decl spec list for error
8374 start_token = token;
8375 /* Handle attributes. */
8376 if (token->keyword == RID_ATTRIBUTE)
8378 /* Parse the attributes. */
8379 decl_specs->attributes
8380 = chainon (decl_specs->attributes,
8381 cp_parser_attributes_opt (parser));
8384 /* Assume we will find a decl-specifier keyword. */
8385 found_decl_spec = true;
8386 /* If the next token is an appropriate keyword, we can simply
8387 add it to the list. */
8388 switch (token->keyword)
8393 if (!at_class_scope_p ())
8395 error ("%H%<friend%> used outside of class", &token->location);
8396 cp_lexer_purge_token (parser->lexer);
8400 ++decl_specs->specs[(int) ds_friend];
8401 /* Consume the token. */
8402 cp_lexer_consume_token (parser->lexer);
8406 /* function-specifier:
8413 cp_parser_function_specifier_opt (parser, decl_specs);
8419 ++decl_specs->specs[(int) ds_typedef];
8420 /* Consume the token. */
8421 cp_lexer_consume_token (parser->lexer);
8422 /* A constructor declarator cannot appear in a typedef. */
8423 constructor_possible_p = false;
8424 /* The "typedef" keyword can only occur in a declaration; we
8425 may as well commit at this point. */
8426 cp_parser_commit_to_tentative_parse (parser);
8428 if (decl_specs->storage_class != sc_none)
8429 decl_specs->conflicting_specifiers_p = true;
8432 /* storage-class-specifier:
8442 if (cxx_dialect == cxx98)
8444 /* Consume the token. */
8445 cp_lexer_consume_token (parser->lexer);
8447 /* Complain about `auto' as a storage specifier, if
8448 we're complaining about C++0x compatibility. */
8451 "%H%<auto%> will change meaning in C++0x; please remove it",
8454 /* Set the storage class anyway. */
8455 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8459 /* C++0x auto type-specifier. */
8460 found_decl_spec = false;
8467 /* Consume the token. */
8468 cp_lexer_consume_token (parser->lexer);
8469 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8473 /* Consume the token. */
8474 cp_lexer_consume_token (parser->lexer);
8475 ++decl_specs->specs[(int) ds_thread];
8479 /* We did not yet find a decl-specifier yet. */
8480 found_decl_spec = false;
8484 /* Constructors are a special case. The `S' in `S()' is not a
8485 decl-specifier; it is the beginning of the declarator. */
8488 && constructor_possible_p
8489 && (cp_parser_constructor_declarator_p
8490 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8492 /* If we don't have a DECL_SPEC yet, then we must be looking at
8493 a type-specifier. */
8494 if (!found_decl_spec && !constructor_p)
8496 int decl_spec_declares_class_or_enum;
8497 bool is_cv_qualifier;
8501 = cp_parser_type_specifier (parser, flags,
8503 /*is_declaration=*/true,
8504 &decl_spec_declares_class_or_enum,
8506 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8508 /* If this type-specifier referenced a user-defined type
8509 (a typedef, class-name, etc.), then we can't allow any
8510 more such type-specifiers henceforth.
8514 The longest sequence of decl-specifiers that could
8515 possibly be a type name is taken as the
8516 decl-specifier-seq of a declaration. The sequence shall
8517 be self-consistent as described below.
8521 As a general rule, at most one type-specifier is allowed
8522 in the complete decl-specifier-seq of a declaration. The
8523 only exceptions are the following:
8525 -- const or volatile can be combined with any other
8528 -- signed or unsigned can be combined with char, long,
8536 void g (const int Pc);
8538 Here, Pc is *not* part of the decl-specifier seq; it's
8539 the declarator. Therefore, once we see a type-specifier
8540 (other than a cv-qualifier), we forbid any additional
8541 user-defined types. We *do* still allow things like `int
8542 int' to be considered a decl-specifier-seq, and issue the
8543 error message later. */
8544 if (type_spec && !is_cv_qualifier)
8545 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8546 /* A constructor declarator cannot follow a type-specifier. */
8549 constructor_possible_p = false;
8550 found_decl_spec = true;
8554 /* If we still do not have a DECL_SPEC, then there are no more
8556 if (!found_decl_spec)
8559 decl_specs->any_specifiers_p = true;
8560 /* After we see one decl-specifier, further decl-specifiers are
8562 flags |= CP_PARSER_FLAGS_OPTIONAL;
8565 cp_parser_check_decl_spec (decl_specs, start_token->location);
8567 /* Don't allow a friend specifier with a class definition. */
8568 if (decl_specs->specs[(int) ds_friend] != 0
8569 && (*declares_class_or_enum & 2))
8570 error ("%Hclass definition may not be declared a friend",
8571 &start_token->location);
8574 /* Parse an (optional) storage-class-specifier.
8576 storage-class-specifier:
8585 storage-class-specifier:
8588 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8591 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8593 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8596 if (cxx_dialect != cxx98)
8598 /* Fall through for C++98. */
8605 /* Consume the token. */
8606 return cp_lexer_consume_token (parser->lexer)->u.value;
8613 /* Parse an (optional) function-specifier.
8620 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8621 Updates DECL_SPECS, if it is non-NULL. */
8624 cp_parser_function_specifier_opt (cp_parser* parser,
8625 cp_decl_specifier_seq *decl_specs)
8627 cp_token *token = cp_lexer_peek_token (parser->lexer);
8628 switch (token->keyword)
8632 ++decl_specs->specs[(int) ds_inline];
8636 /* 14.5.2.3 [temp.mem]
8638 A member function template shall not be virtual. */
8639 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8640 error ("%Htemplates may not be %<virtual%>", &token->location);
8641 else if (decl_specs)
8642 ++decl_specs->specs[(int) ds_virtual];
8647 ++decl_specs->specs[(int) ds_explicit];
8654 /* Consume the token. */
8655 return cp_lexer_consume_token (parser->lexer)->u.value;
8658 /* Parse a linkage-specification.
8660 linkage-specification:
8661 extern string-literal { declaration-seq [opt] }
8662 extern string-literal declaration */
8665 cp_parser_linkage_specification (cp_parser* parser)
8669 /* Look for the `extern' keyword. */
8670 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8672 /* Look for the string-literal. */
8673 linkage = cp_parser_string_literal (parser, false, false);
8675 /* Transform the literal into an identifier. If the literal is a
8676 wide-character string, or contains embedded NULs, then we can't
8677 handle it as the user wants. */
8678 if (strlen (TREE_STRING_POINTER (linkage))
8679 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8681 cp_parser_error (parser, "invalid linkage-specification");
8682 /* Assume C++ linkage. */
8683 linkage = lang_name_cplusplus;
8686 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8688 /* We're now using the new linkage. */
8689 push_lang_context (linkage);
8691 /* If the next token is a `{', then we're using the first
8693 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8695 /* Consume the `{' token. */
8696 cp_lexer_consume_token (parser->lexer);
8697 /* Parse the declarations. */
8698 cp_parser_declaration_seq_opt (parser);
8699 /* Look for the closing `}'. */
8700 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8702 /* Otherwise, there's just one declaration. */
8705 bool saved_in_unbraced_linkage_specification_p;
8707 saved_in_unbraced_linkage_specification_p
8708 = parser->in_unbraced_linkage_specification_p;
8709 parser->in_unbraced_linkage_specification_p = true;
8710 cp_parser_declaration (parser);
8711 parser->in_unbraced_linkage_specification_p
8712 = saved_in_unbraced_linkage_specification_p;
8715 /* We're done with the linkage-specification. */
8716 pop_lang_context ();
8719 /* Parse a static_assert-declaration.
8721 static_assert-declaration:
8722 static_assert ( constant-expression , string-literal ) ;
8724 If MEMBER_P, this static_assert is a class member. */
8727 cp_parser_static_assert(cp_parser *parser, bool member_p)
8732 location_t saved_loc;
8734 /* Peek at the `static_assert' token so we can keep track of exactly
8735 where the static assertion started. */
8736 token = cp_lexer_peek_token (parser->lexer);
8737 saved_loc = token->location;
8739 /* Look for the `static_assert' keyword. */
8740 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8741 "%<static_assert%>"))
8744 /* We know we are in a static assertion; commit to any tentative
8746 if (cp_parser_parsing_tentatively (parser))
8747 cp_parser_commit_to_tentative_parse (parser);
8749 /* Parse the `(' starting the static assertion condition. */
8750 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8752 /* Parse the constant-expression. */
8754 cp_parser_constant_expression (parser,
8755 /*allow_non_constant_p=*/false,
8756 /*non_constant_p=*/NULL);
8758 /* Parse the separating `,'. */
8759 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8761 /* Parse the string-literal message. */
8762 message = cp_parser_string_literal (parser,
8763 /*translate=*/false,
8766 /* A `)' completes the static assertion. */
8767 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8768 cp_parser_skip_to_closing_parenthesis (parser,
8769 /*recovering=*/true,
8771 /*consume_paren=*/true);
8773 /* A semicolon terminates the declaration. */
8774 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8776 /* Complete the static assertion, which may mean either processing
8777 the static assert now or saving it for template instantiation. */
8778 finish_static_assert (condition, message, saved_loc, member_p);
8781 /* Parse a `decltype' type. Returns the type.
8783 simple-type-specifier:
8784 decltype ( expression ) */
8787 cp_parser_decltype (cp_parser *parser)
8790 bool id_expression_or_member_access_p = false;
8791 const char *saved_message;
8792 bool saved_integral_constant_expression_p;
8793 bool saved_non_integral_constant_expression_p;
8794 cp_token *id_expr_start_token;
8796 /* Look for the `decltype' token. */
8797 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8798 return error_mark_node;
8800 /* Types cannot be defined in a `decltype' expression. Save away the
8802 saved_message = parser->type_definition_forbidden_message;
8804 /* And create the new one. */
8805 parser->type_definition_forbidden_message
8806 = "types may not be defined in %<decltype%> expressions";
8808 /* The restrictions on constant-expressions do not apply inside
8809 decltype expressions. */
8810 saved_integral_constant_expression_p
8811 = parser->integral_constant_expression_p;
8812 saved_non_integral_constant_expression_p
8813 = parser->non_integral_constant_expression_p;
8814 parser->integral_constant_expression_p = false;
8816 /* Do not actually evaluate the expression. */
8819 /* Parse the opening `('. */
8820 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8821 return error_mark_node;
8823 /* First, try parsing an id-expression. */
8824 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8825 cp_parser_parse_tentatively (parser);
8826 expr = cp_parser_id_expression (parser,
8827 /*template_keyword_p=*/false,
8828 /*check_dependency_p=*/true,
8829 /*template_p=*/NULL,
8830 /*declarator_p=*/false,
8831 /*optional_p=*/false);
8833 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8835 bool non_integral_constant_expression_p = false;
8836 tree id_expression = expr;
8838 const char *error_msg;
8840 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8841 /* Lookup the name we got back from the id-expression. */
8842 expr = cp_parser_lookup_name (parser, expr,
8844 /*is_template=*/false,
8845 /*is_namespace=*/false,
8846 /*check_dependency=*/true,
8847 /*ambiguous_decls=*/NULL,
8848 id_expr_start_token->location);
8851 && expr != error_mark_node
8852 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8853 && TREE_CODE (expr) != TYPE_DECL
8854 && (TREE_CODE (expr) != BIT_NOT_EXPR
8855 || !TYPE_P (TREE_OPERAND (expr, 0)))
8856 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8858 /* Complete lookup of the id-expression. */
8859 expr = (finish_id_expression
8860 (id_expression, expr, parser->scope, &idk,
8861 /*integral_constant_expression_p=*/false,
8862 /*allow_non_integral_constant_expression_p=*/true,
8863 &non_integral_constant_expression_p,
8864 /*template_p=*/false,
8866 /*address_p=*/false,
8867 /*template_arg_p=*/false,
8869 id_expr_start_token->location));
8871 if (expr == error_mark_node)
8872 /* We found an id-expression, but it was something that we
8873 should not have found. This is an error, not something
8874 we can recover from, so note that we found an
8875 id-expression and we'll recover as gracefully as
8877 id_expression_or_member_access_p = true;
8881 && expr != error_mark_node
8882 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8883 /* We have an id-expression. */
8884 id_expression_or_member_access_p = true;
8887 if (!id_expression_or_member_access_p)
8889 /* Abort the id-expression parse. */
8890 cp_parser_abort_tentative_parse (parser);
8892 /* Parsing tentatively, again. */
8893 cp_parser_parse_tentatively (parser);
8895 /* Parse a class member access. */
8896 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8898 /*member_access_only_p=*/true, NULL);
8901 && expr != error_mark_node
8902 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8903 /* We have an id-expression. */
8904 id_expression_or_member_access_p = true;
8907 if (id_expression_or_member_access_p)
8908 /* We have parsed the complete id-expression or member access. */
8909 cp_parser_parse_definitely (parser);
8912 /* Abort our attempt to parse an id-expression or member access
8914 cp_parser_abort_tentative_parse (parser);
8916 /* Parse a full expression. */
8917 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8920 /* Go back to evaluating expressions. */
8923 /* Restore the old message and the integral constant expression
8925 parser->type_definition_forbidden_message = saved_message;
8926 parser->integral_constant_expression_p
8927 = saved_integral_constant_expression_p;
8928 parser->non_integral_constant_expression_p
8929 = saved_non_integral_constant_expression_p;
8931 if (expr == error_mark_node)
8933 /* Skip everything up to the closing `)'. */
8934 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8935 /*consume_paren=*/true);
8936 return error_mark_node;
8939 /* Parse to the closing `)'. */
8940 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8942 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8943 /*consume_paren=*/true);
8944 return error_mark_node;
8947 return finish_decltype_type (expr, id_expression_or_member_access_p);
8950 /* Special member functions [gram.special] */
8952 /* Parse a conversion-function-id.
8954 conversion-function-id:
8955 operator conversion-type-id
8957 Returns an IDENTIFIER_NODE representing the operator. */
8960 cp_parser_conversion_function_id (cp_parser* parser)
8964 tree saved_qualifying_scope;
8965 tree saved_object_scope;
8966 tree pushed_scope = NULL_TREE;
8968 /* Look for the `operator' token. */
8969 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8970 return error_mark_node;
8971 /* When we parse the conversion-type-id, the current scope will be
8972 reset. However, we need that information in able to look up the
8973 conversion function later, so we save it here. */
8974 saved_scope = parser->scope;
8975 saved_qualifying_scope = parser->qualifying_scope;
8976 saved_object_scope = parser->object_scope;
8977 /* We must enter the scope of the class so that the names of
8978 entities declared within the class are available in the
8979 conversion-type-id. For example, consider:
8986 S::operator I() { ... }
8988 In order to see that `I' is a type-name in the definition, we
8989 must be in the scope of `S'. */
8991 pushed_scope = push_scope (saved_scope);
8992 /* Parse the conversion-type-id. */
8993 type = cp_parser_conversion_type_id (parser);
8994 /* Leave the scope of the class, if any. */
8996 pop_scope (pushed_scope);
8997 /* Restore the saved scope. */
8998 parser->scope = saved_scope;
8999 parser->qualifying_scope = saved_qualifying_scope;
9000 parser->object_scope = saved_object_scope;
9001 /* If the TYPE is invalid, indicate failure. */
9002 if (type == error_mark_node)
9003 return error_mark_node;
9004 return mangle_conv_op_name_for_type (type);
9007 /* Parse a conversion-type-id:
9010 type-specifier-seq conversion-declarator [opt]
9012 Returns the TYPE specified. */
9015 cp_parser_conversion_type_id (cp_parser* parser)
9018 cp_decl_specifier_seq type_specifiers;
9019 cp_declarator *declarator;
9020 tree type_specified;
9022 /* Parse the attributes. */
9023 attributes = cp_parser_attributes_opt (parser);
9024 /* Parse the type-specifiers. */
9025 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9027 /* If that didn't work, stop. */
9028 if (type_specifiers.type == error_mark_node)
9029 return error_mark_node;
9030 /* Parse the conversion-declarator. */
9031 declarator = cp_parser_conversion_declarator_opt (parser);
9033 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9034 /*initialized=*/0, &attributes);
9036 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9038 /* Don't give this error when parsing tentatively. This happens to
9039 work because we always parse this definitively once. */
9040 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9041 && type_uses_auto (type_specified))
9043 error ("invalid use of %<auto%> in conversion operator");
9044 return error_mark_node;
9047 return type_specified;
9050 /* Parse an (optional) conversion-declarator.
9052 conversion-declarator:
9053 ptr-operator conversion-declarator [opt]
9057 static cp_declarator *
9058 cp_parser_conversion_declarator_opt (cp_parser* parser)
9060 enum tree_code code;
9062 cp_cv_quals cv_quals;
9064 /* We don't know if there's a ptr-operator next, or not. */
9065 cp_parser_parse_tentatively (parser);
9066 /* Try the ptr-operator. */
9067 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9068 /* If it worked, look for more conversion-declarators. */
9069 if (cp_parser_parse_definitely (parser))
9071 cp_declarator *declarator;
9073 /* Parse another optional declarator. */
9074 declarator = cp_parser_conversion_declarator_opt (parser);
9076 return cp_parser_make_indirect_declarator
9077 (code, class_type, cv_quals, declarator);
9083 /* Parse an (optional) ctor-initializer.
9086 : mem-initializer-list
9088 Returns TRUE iff the ctor-initializer was actually present. */
9091 cp_parser_ctor_initializer_opt (cp_parser* parser)
9093 /* If the next token is not a `:', then there is no
9094 ctor-initializer. */
9095 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9097 /* Do default initialization of any bases and members. */
9098 if (DECL_CONSTRUCTOR_P (current_function_decl))
9099 finish_mem_initializers (NULL_TREE);
9104 /* Consume the `:' token. */
9105 cp_lexer_consume_token (parser->lexer);
9106 /* And the mem-initializer-list. */
9107 cp_parser_mem_initializer_list (parser);
9112 /* Parse a mem-initializer-list.
9114 mem-initializer-list:
9115 mem-initializer ... [opt]
9116 mem-initializer ... [opt] , mem-initializer-list */
9119 cp_parser_mem_initializer_list (cp_parser* parser)
9121 tree mem_initializer_list = NULL_TREE;
9122 cp_token *token = cp_lexer_peek_token (parser->lexer);
9124 /* Let the semantic analysis code know that we are starting the
9125 mem-initializer-list. */
9126 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9127 error ("%Honly constructors take base initializers",
9130 /* Loop through the list. */
9133 tree mem_initializer;
9135 token = cp_lexer_peek_token (parser->lexer);
9136 /* Parse the mem-initializer. */
9137 mem_initializer = cp_parser_mem_initializer (parser);
9138 /* If the next token is a `...', we're expanding member initializers. */
9139 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9141 /* Consume the `...'. */
9142 cp_lexer_consume_token (parser->lexer);
9144 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9145 can be expanded but members cannot. */
9146 if (mem_initializer != error_mark_node
9147 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9149 error ("%Hcannot expand initializer for member %<%D%>",
9150 &token->location, TREE_PURPOSE (mem_initializer));
9151 mem_initializer = error_mark_node;
9154 /* Construct the pack expansion type. */
9155 if (mem_initializer != error_mark_node)
9156 mem_initializer = make_pack_expansion (mem_initializer);
9158 /* Add it to the list, unless it was erroneous. */
9159 if (mem_initializer != error_mark_node)
9161 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9162 mem_initializer_list = mem_initializer;
9164 /* If the next token is not a `,', we're done. */
9165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9167 /* Consume the `,' token. */
9168 cp_lexer_consume_token (parser->lexer);
9171 /* Perform semantic analysis. */
9172 if (DECL_CONSTRUCTOR_P (current_function_decl))
9173 finish_mem_initializers (mem_initializer_list);
9176 /* Parse a mem-initializer.
9179 mem-initializer-id ( expression-list [opt] )
9180 mem-initializer-id braced-init-list
9185 ( expression-list [opt] )
9187 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9188 class) or FIELD_DECL (for a non-static data member) to initialize;
9189 the TREE_VALUE is the expression-list. An empty initialization
9190 list is represented by void_list_node. */
9193 cp_parser_mem_initializer (cp_parser* parser)
9195 tree mem_initializer_id;
9196 tree expression_list;
9198 cp_token *token = cp_lexer_peek_token (parser->lexer);
9200 /* Find out what is being initialized. */
9201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9203 permerror (token->location,
9204 "anachronistic old-style base class initializer");
9205 mem_initializer_id = NULL_TREE;
9209 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9210 if (mem_initializer_id == error_mark_node)
9211 return mem_initializer_id;
9213 member = expand_member_init (mem_initializer_id);
9214 if (member && !DECL_P (member))
9215 in_base_initializer = 1;
9217 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9219 bool expr_non_constant_p;
9220 maybe_warn_cpp0x ("extended initializer lists");
9221 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9222 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9223 expression_list = build_tree_list (NULL_TREE, expression_list);
9227 = cp_parser_parenthesized_expression_list (parser, false,
9229 /*allow_expansion_p=*/true,
9230 /*non_constant_p=*/NULL);
9231 if (expression_list == error_mark_node)
9232 return error_mark_node;
9233 if (!expression_list)
9234 expression_list = void_type_node;
9236 in_base_initializer = 0;
9238 return member ? build_tree_list (member, expression_list) : error_mark_node;
9241 /* Parse a mem-initializer-id.
9244 :: [opt] nested-name-specifier [opt] class-name
9247 Returns a TYPE indicating the class to be initializer for the first
9248 production. Returns an IDENTIFIER_NODE indicating the data member
9249 to be initialized for the second production. */
9252 cp_parser_mem_initializer_id (cp_parser* parser)
9254 bool global_scope_p;
9255 bool nested_name_specifier_p;
9256 bool template_p = false;
9259 cp_token *token = cp_lexer_peek_token (parser->lexer);
9261 /* `typename' is not allowed in this context ([temp.res]). */
9262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9264 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9265 "member initializer is implicitly a type)",
9267 cp_lexer_consume_token (parser->lexer);
9269 /* Look for the optional `::' operator. */
9271 = (cp_parser_global_scope_opt (parser,
9272 /*current_scope_valid_p=*/false)
9274 /* Look for the optional nested-name-specifier. The simplest way to
9279 The keyword `typename' is not permitted in a base-specifier or
9280 mem-initializer; in these contexts a qualified name that
9281 depends on a template-parameter is implicitly assumed to be a
9284 is to assume that we have seen the `typename' keyword at this
9286 nested_name_specifier_p
9287 = (cp_parser_nested_name_specifier_opt (parser,
9288 /*typename_keyword_p=*/true,
9289 /*check_dependency_p=*/true,
9291 /*is_declaration=*/true)
9293 if (nested_name_specifier_p)
9294 template_p = cp_parser_optional_template_keyword (parser);
9295 /* If there is a `::' operator or a nested-name-specifier, then we
9296 are definitely looking for a class-name. */
9297 if (global_scope_p || nested_name_specifier_p)
9298 return cp_parser_class_name (parser,
9299 /*typename_keyword_p=*/true,
9300 /*template_keyword_p=*/template_p,
9302 /*check_dependency_p=*/true,
9303 /*class_head_p=*/false,
9304 /*is_declaration=*/true);
9305 /* Otherwise, we could also be looking for an ordinary identifier. */
9306 cp_parser_parse_tentatively (parser);
9307 /* Try a class-name. */
9308 id = cp_parser_class_name (parser,
9309 /*typename_keyword_p=*/true,
9310 /*template_keyword_p=*/false,
9312 /*check_dependency_p=*/true,
9313 /*class_head_p=*/false,
9314 /*is_declaration=*/true);
9315 /* If we found one, we're done. */
9316 if (cp_parser_parse_definitely (parser))
9318 /* Otherwise, look for an ordinary identifier. */
9319 return cp_parser_identifier (parser);
9322 /* Overloading [gram.over] */
9324 /* Parse an operator-function-id.
9326 operator-function-id:
9329 Returns an IDENTIFIER_NODE for the operator which is a
9330 human-readable spelling of the identifier, e.g., `operator +'. */
9333 cp_parser_operator_function_id (cp_parser* parser)
9335 /* Look for the `operator' keyword. */
9336 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9337 return error_mark_node;
9338 /* And then the name of the operator itself. */
9339 return cp_parser_operator (parser);
9342 /* Parse an operator.
9345 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9346 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9347 || ++ -- , ->* -> () []
9354 Returns an IDENTIFIER_NODE for the operator which is a
9355 human-readable spelling of the identifier, e.g., `operator +'. */
9358 cp_parser_operator (cp_parser* parser)
9360 tree id = NULL_TREE;
9363 /* Peek at the next token. */
9364 token = cp_lexer_peek_token (parser->lexer);
9365 /* Figure out which operator we have. */
9366 switch (token->type)
9372 /* The keyword should be either `new' or `delete'. */
9373 if (token->keyword == RID_NEW)
9375 else if (token->keyword == RID_DELETE)
9380 /* Consume the `new' or `delete' token. */
9381 cp_lexer_consume_token (parser->lexer);
9383 /* Peek at the next token. */
9384 token = cp_lexer_peek_token (parser->lexer);
9385 /* If it's a `[' token then this is the array variant of the
9387 if (token->type == CPP_OPEN_SQUARE)
9389 /* Consume the `[' token. */
9390 cp_lexer_consume_token (parser->lexer);
9391 /* Look for the `]' token. */
9392 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9393 id = ansi_opname (op == NEW_EXPR
9394 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9396 /* Otherwise, we have the non-array variant. */
9398 id = ansi_opname (op);
9404 id = ansi_opname (PLUS_EXPR);
9408 id = ansi_opname (MINUS_EXPR);
9412 id = ansi_opname (MULT_EXPR);
9416 id = ansi_opname (TRUNC_DIV_EXPR);
9420 id = ansi_opname (TRUNC_MOD_EXPR);
9424 id = ansi_opname (BIT_XOR_EXPR);
9428 id = ansi_opname (BIT_AND_EXPR);
9432 id = ansi_opname (BIT_IOR_EXPR);
9436 id = ansi_opname (BIT_NOT_EXPR);
9440 id = ansi_opname (TRUTH_NOT_EXPR);
9444 id = ansi_assopname (NOP_EXPR);
9448 id = ansi_opname (LT_EXPR);
9452 id = ansi_opname (GT_EXPR);
9456 id = ansi_assopname (PLUS_EXPR);
9460 id = ansi_assopname (MINUS_EXPR);
9464 id = ansi_assopname (MULT_EXPR);
9468 id = ansi_assopname (TRUNC_DIV_EXPR);
9472 id = ansi_assopname (TRUNC_MOD_EXPR);
9476 id = ansi_assopname (BIT_XOR_EXPR);
9480 id = ansi_assopname (BIT_AND_EXPR);
9484 id = ansi_assopname (BIT_IOR_EXPR);
9488 id = ansi_opname (LSHIFT_EXPR);
9492 id = ansi_opname (RSHIFT_EXPR);
9496 id = ansi_assopname (LSHIFT_EXPR);
9500 id = ansi_assopname (RSHIFT_EXPR);
9504 id = ansi_opname (EQ_EXPR);
9508 id = ansi_opname (NE_EXPR);
9512 id = ansi_opname (LE_EXPR);
9515 case CPP_GREATER_EQ:
9516 id = ansi_opname (GE_EXPR);
9520 id = ansi_opname (TRUTH_ANDIF_EXPR);
9524 id = ansi_opname (TRUTH_ORIF_EXPR);
9528 id = ansi_opname (POSTINCREMENT_EXPR);
9531 case CPP_MINUS_MINUS:
9532 id = ansi_opname (PREDECREMENT_EXPR);
9536 id = ansi_opname (COMPOUND_EXPR);
9539 case CPP_DEREF_STAR:
9540 id = ansi_opname (MEMBER_REF);
9544 id = ansi_opname (COMPONENT_REF);
9547 case CPP_OPEN_PAREN:
9548 /* Consume the `('. */
9549 cp_lexer_consume_token (parser->lexer);
9550 /* Look for the matching `)'. */
9551 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9552 return ansi_opname (CALL_EXPR);
9554 case CPP_OPEN_SQUARE:
9555 /* Consume the `['. */
9556 cp_lexer_consume_token (parser->lexer);
9557 /* Look for the matching `]'. */
9558 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9559 return ansi_opname (ARRAY_REF);
9562 /* Anything else is an error. */
9566 /* If we have selected an identifier, we need to consume the
9569 cp_lexer_consume_token (parser->lexer);
9570 /* Otherwise, no valid operator name was present. */
9573 cp_parser_error (parser, "expected operator");
9574 id = error_mark_node;
9580 /* Parse a template-declaration.
9582 template-declaration:
9583 export [opt] template < template-parameter-list > declaration
9585 If MEMBER_P is TRUE, this template-declaration occurs within a
9588 The grammar rule given by the standard isn't correct. What
9591 template-declaration:
9592 export [opt] template-parameter-list-seq
9593 decl-specifier-seq [opt] init-declarator [opt] ;
9594 export [opt] template-parameter-list-seq
9597 template-parameter-list-seq:
9598 template-parameter-list-seq [opt]
9599 template < template-parameter-list > */
9602 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9604 /* Check for `export'. */
9605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9607 /* Consume the `export' token. */
9608 cp_lexer_consume_token (parser->lexer);
9609 /* Warn that we do not support `export'. */
9610 warning (0, "keyword %<export%> not implemented, and will be ignored");
9613 cp_parser_template_declaration_after_export (parser, member_p);
9616 /* Parse a template-parameter-list.
9618 template-parameter-list:
9620 template-parameter-list , template-parameter
9622 Returns a TREE_LIST. Each node represents a template parameter.
9623 The nodes are connected via their TREE_CHAINs. */
9626 cp_parser_template_parameter_list (cp_parser* parser)
9628 tree parameter_list = NULL_TREE;
9630 begin_template_parm_list ();
9635 bool is_parameter_pack;
9637 /* Parse the template-parameter. */
9638 parameter = cp_parser_template_parameter (parser,
9640 &is_parameter_pack);
9641 /* Add it to the list. */
9642 if (parameter != error_mark_node)
9643 parameter_list = process_template_parm (parameter_list,
9649 tree err_parm = build_tree_list (parameter, parameter);
9650 TREE_VALUE (err_parm) = error_mark_node;
9651 parameter_list = chainon (parameter_list, err_parm);
9654 /* If the next token is not a `,', we're done. */
9655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9657 /* Otherwise, consume the `,' token. */
9658 cp_lexer_consume_token (parser->lexer);
9661 return end_template_parm_list (parameter_list);
9664 /* Parse a template-parameter.
9668 parameter-declaration
9670 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9671 the parameter. The TREE_PURPOSE is the default value, if any.
9672 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9673 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9674 set to true iff this parameter is a parameter pack. */
9677 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9678 bool *is_parameter_pack)
9681 cp_parameter_declarator *parameter_declarator;
9682 cp_declarator *id_declarator;
9685 /* Assume it is a type parameter or a template parameter. */
9686 *is_non_type = false;
9687 /* Assume it not a parameter pack. */
9688 *is_parameter_pack = false;
9689 /* Peek at the next token. */
9690 token = cp_lexer_peek_token (parser->lexer);
9691 /* If it is `class' or `template', we have a type-parameter. */
9692 if (token->keyword == RID_TEMPLATE)
9693 return cp_parser_type_parameter (parser, is_parameter_pack);
9694 /* If it is `class' or `typename' we do not know yet whether it is a
9695 type parameter or a non-type parameter. Consider:
9697 template <typename T, typename T::X X> ...
9701 template <class C, class D*> ...
9703 Here, the first parameter is a type parameter, and the second is
9704 a non-type parameter. We can tell by looking at the token after
9705 the identifier -- if it is a `,', `=', or `>' then we have a type
9707 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9709 /* Peek at the token after `class' or `typename'. */
9710 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9711 /* If it's an ellipsis, we have a template type parameter
9713 if (token->type == CPP_ELLIPSIS)
9714 return cp_parser_type_parameter (parser, is_parameter_pack);
9715 /* If it's an identifier, skip it. */
9716 if (token->type == CPP_NAME)
9717 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9718 /* Now, see if the token looks like the end of a template
9720 if (token->type == CPP_COMMA
9721 || token->type == CPP_EQ
9722 || token->type == CPP_GREATER)
9723 return cp_parser_type_parameter (parser, is_parameter_pack);
9726 /* Otherwise, it is a non-type parameter.
9730 When parsing a default template-argument for a non-type
9731 template-parameter, the first non-nested `>' is taken as the end
9732 of the template parameter-list rather than a greater-than
9734 *is_non_type = true;
9735 parameter_declarator
9736 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9737 /*parenthesized_p=*/NULL);
9739 /* If the parameter declaration is marked as a parameter pack, set
9740 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9741 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9743 if (parameter_declarator
9744 && parameter_declarator->declarator
9745 && parameter_declarator->declarator->parameter_pack_p)
9747 *is_parameter_pack = true;
9748 parameter_declarator->declarator->parameter_pack_p = false;
9751 /* If the next token is an ellipsis, and we don't already have it
9752 marked as a parameter pack, then we have a parameter pack (that
9753 has no declarator). */
9754 if (!*is_parameter_pack
9755 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9756 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9758 /* Consume the `...'. */
9759 cp_lexer_consume_token (parser->lexer);
9760 maybe_warn_variadic_templates ();
9762 *is_parameter_pack = true;
9764 /* We might end up with a pack expansion as the type of the non-type
9765 template parameter, in which case this is a non-type template
9767 else if (parameter_declarator
9768 && parameter_declarator->decl_specifiers.type
9769 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9771 *is_parameter_pack = true;
9772 parameter_declarator->decl_specifiers.type =
9773 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9776 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9778 /* Parameter packs cannot have default arguments. However, a
9779 user may try to do so, so we'll parse them and give an
9780 appropriate diagnostic here. */
9782 /* Consume the `='. */
9783 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9784 cp_lexer_consume_token (parser->lexer);
9786 /* Find the name of the parameter pack. */
9787 id_declarator = parameter_declarator->declarator;
9788 while (id_declarator && id_declarator->kind != cdk_id)
9789 id_declarator = id_declarator->declarator;
9791 if (id_declarator && id_declarator->kind == cdk_id)
9792 error ("%Htemplate parameter pack %qD cannot have a default argument",
9793 &start_token->location, id_declarator->u.id.unqualified_name);
9795 error ("%Htemplate parameter pack cannot have a default argument",
9796 &start_token->location);
9798 /* Parse the default argument, but throw away the result. */
9799 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9802 parm = grokdeclarator (parameter_declarator->declarator,
9803 ¶meter_declarator->decl_specifiers,
9804 PARM, /*initialized=*/0,
9806 if (parm == error_mark_node)
9807 return error_mark_node;
9809 return build_tree_list (parameter_declarator->default_argument, parm);
9812 /* Parse a type-parameter.
9815 class identifier [opt]
9816 class identifier [opt] = type-id
9817 typename identifier [opt]
9818 typename identifier [opt] = type-id
9819 template < template-parameter-list > class identifier [opt]
9820 template < template-parameter-list > class identifier [opt]
9823 GNU Extension (variadic templates):
9826 class ... identifier [opt]
9827 typename ... identifier [opt]
9829 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9830 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9831 the declaration of the parameter.
9833 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9836 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9841 /* Look for a keyword to tell us what kind of parameter this is. */
9842 token = cp_parser_require (parser, CPP_KEYWORD,
9843 "%<class%>, %<typename%>, or %<template%>");
9845 return error_mark_node;
9847 switch (token->keyword)
9853 tree default_argument;
9855 /* If the next token is an ellipsis, we have a template
9857 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9859 /* Consume the `...' token. */
9860 cp_lexer_consume_token (parser->lexer);
9861 maybe_warn_variadic_templates ();
9863 *is_parameter_pack = true;
9866 /* If the next token is an identifier, then it names the
9868 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9869 identifier = cp_parser_identifier (parser);
9871 identifier = NULL_TREE;
9873 /* Create the parameter. */
9874 parameter = finish_template_type_parm (class_type_node, identifier);
9876 /* If the next token is an `=', we have a default argument. */
9877 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9879 /* Consume the `=' token. */
9880 cp_lexer_consume_token (parser->lexer);
9881 /* Parse the default-argument. */
9882 push_deferring_access_checks (dk_no_deferred);
9883 default_argument = cp_parser_type_id (parser);
9885 /* Template parameter packs cannot have default
9887 if (*is_parameter_pack)
9890 error ("%Htemplate parameter pack %qD cannot have a "
9891 "default argument", &token->location, identifier);
9893 error ("%Htemplate parameter packs cannot have "
9894 "default arguments", &token->location);
9895 default_argument = NULL_TREE;
9897 pop_deferring_access_checks ();
9900 default_argument = NULL_TREE;
9902 /* Create the combined representation of the parameter and the
9903 default argument. */
9904 parameter = build_tree_list (default_argument, parameter);
9910 tree parameter_list;
9912 tree default_argument;
9914 /* Look for the `<'. */
9915 cp_parser_require (parser, CPP_LESS, "%<<%>");
9916 /* Parse the template-parameter-list. */
9917 parameter_list = cp_parser_template_parameter_list (parser);
9918 /* Look for the `>'. */
9919 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9920 /* Look for the `class' keyword. */
9921 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9922 /* If the next token is an ellipsis, we have a template
9924 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9926 /* Consume the `...' token. */
9927 cp_lexer_consume_token (parser->lexer);
9928 maybe_warn_variadic_templates ();
9930 *is_parameter_pack = true;
9932 /* If the next token is an `=', then there is a
9933 default-argument. If the next token is a `>', we are at
9934 the end of the parameter-list. If the next token is a `,',
9935 then we are at the end of this parameter. */
9936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9937 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9938 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9940 identifier = cp_parser_identifier (parser);
9941 /* Treat invalid names as if the parameter were nameless. */
9942 if (identifier == error_mark_node)
9943 identifier = NULL_TREE;
9946 identifier = NULL_TREE;
9948 /* Create the template parameter. */
9949 parameter = finish_template_template_parm (class_type_node,
9952 /* If the next token is an `=', then there is a
9953 default-argument. */
9954 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9958 /* Consume the `='. */
9959 cp_lexer_consume_token (parser->lexer);
9960 /* Parse the id-expression. */
9961 push_deferring_access_checks (dk_no_deferred);
9962 /* save token before parsing the id-expression, for error
9964 token = cp_lexer_peek_token (parser->lexer);
9966 = cp_parser_id_expression (parser,
9967 /*template_keyword_p=*/false,
9968 /*check_dependency_p=*/true,
9969 /*template_p=*/&is_template,
9970 /*declarator_p=*/false,
9971 /*optional_p=*/false);
9972 if (TREE_CODE (default_argument) == TYPE_DECL)
9973 /* If the id-expression was a template-id that refers to
9974 a template-class, we already have the declaration here,
9975 so no further lookup is needed. */
9978 /* Look up the name. */
9980 = cp_parser_lookup_name (parser, default_argument,
9982 /*is_template=*/is_template,
9983 /*is_namespace=*/false,
9984 /*check_dependency=*/true,
9985 /*ambiguous_decls=*/NULL,
9987 /* See if the default argument is valid. */
9989 = check_template_template_default_arg (default_argument);
9991 /* Template parameter packs cannot have default
9993 if (*is_parameter_pack)
9996 error ("%Htemplate parameter pack %qD cannot "
9997 "have a default argument",
9998 &token->location, identifier);
10000 error ("%Htemplate parameter packs cannot "
10001 "have default arguments",
10003 default_argument = NULL_TREE;
10005 pop_deferring_access_checks ();
10008 default_argument = NULL_TREE;
10010 /* Create the combined representation of the parameter and the
10011 default argument. */
10012 parameter = build_tree_list (default_argument, parameter);
10017 gcc_unreachable ();
10024 /* Parse a template-id.
10027 template-name < template-argument-list [opt] >
10029 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10030 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10031 returned. Otherwise, if the template-name names a function, or set
10032 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10033 names a class, returns a TYPE_DECL for the specialization.
10035 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10036 uninstantiated templates. */
10039 cp_parser_template_id (cp_parser *parser,
10040 bool template_keyword_p,
10041 bool check_dependency_p,
10042 bool is_declaration)
10048 cp_token_position start_of_id = 0;
10049 deferred_access_check *chk;
10050 VEC (deferred_access_check,gc) *access_check;
10051 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10052 bool is_identifier;
10054 /* If the next token corresponds to a template-id, there is no need
10056 next_token = cp_lexer_peek_token (parser->lexer);
10057 if (next_token->type == CPP_TEMPLATE_ID)
10059 struct tree_check *check_value;
10061 /* Get the stored value. */
10062 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10063 /* Perform any access checks that were deferred. */
10064 access_check = check_value->checks;
10068 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10071 perform_or_defer_access_check (chk->binfo,
10076 /* Return the stored value. */
10077 return check_value->value;
10080 /* Avoid performing name lookup if there is no possibility of
10081 finding a template-id. */
10082 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10083 || (next_token->type == CPP_NAME
10084 && !cp_parser_nth_token_starts_template_argument_list_p
10087 cp_parser_error (parser, "expected template-id");
10088 return error_mark_node;
10091 /* Remember where the template-id starts. */
10092 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10093 start_of_id = cp_lexer_token_position (parser->lexer, false);
10095 push_deferring_access_checks (dk_deferred);
10097 /* Parse the template-name. */
10098 is_identifier = false;
10099 token = cp_lexer_peek_token (parser->lexer);
10100 templ = cp_parser_template_name (parser, template_keyword_p,
10101 check_dependency_p,
10104 if (templ == error_mark_node || is_identifier)
10106 pop_deferring_access_checks ();
10110 /* If we find the sequence `[:' after a template-name, it's probably
10111 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10112 parse correctly the argument list. */
10113 next_token = cp_lexer_peek_token (parser->lexer);
10114 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10115 if (next_token->type == CPP_OPEN_SQUARE
10116 && next_token->flags & DIGRAPH
10117 && next_token_2->type == CPP_COLON
10118 && !(next_token_2->flags & PREV_WHITE))
10120 cp_parser_parse_tentatively (parser);
10121 /* Change `:' into `::'. */
10122 next_token_2->type = CPP_SCOPE;
10123 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10125 cp_lexer_consume_token (parser->lexer);
10127 /* Parse the arguments. */
10128 arguments = cp_parser_enclosed_template_argument_list (parser);
10129 if (!cp_parser_parse_definitely (parser))
10131 /* If we couldn't parse an argument list, then we revert our changes
10132 and return simply an error. Maybe this is not a template-id
10134 next_token_2->type = CPP_COLON;
10135 cp_parser_error (parser, "expected %<<%>");
10136 pop_deferring_access_checks ();
10137 return error_mark_node;
10139 /* Otherwise, emit an error about the invalid digraph, but continue
10140 parsing because we got our argument list. */
10141 if (permerror (next_token->location,
10142 "%<<::%> cannot begin a template-argument list"))
10144 static bool hint = false;
10145 inform (next_token->location,
10146 "%<<:%> is an alternate spelling for %<[%>."
10147 " Insert whitespace between %<<%> and %<::%>");
10148 if (!hint && !flag_permissive)
10150 inform (next_token->location, "(if you use %<-fpermissive%>"
10151 " G++ will accept your code)");
10158 /* Look for the `<' that starts the template-argument-list. */
10159 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10161 pop_deferring_access_checks ();
10162 return error_mark_node;
10164 /* Parse the arguments. */
10165 arguments = cp_parser_enclosed_template_argument_list (parser);
10168 /* Build a representation of the specialization. */
10169 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10170 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10171 else if (DECL_CLASS_TEMPLATE_P (templ)
10172 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10174 bool entering_scope;
10175 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10176 template (rather than some instantiation thereof) only if
10177 is not nested within some other construct. For example, in
10178 "template <typename T> void f(T) { A<T>::", A<T> is just an
10179 instantiation of A. */
10180 entering_scope = (template_parm_scope_p ()
10181 && cp_lexer_next_token_is (parser->lexer,
10184 = finish_template_type (templ, arguments, entering_scope);
10188 /* If it's not a class-template or a template-template, it should be
10189 a function-template. */
10190 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10191 || TREE_CODE (templ) == OVERLOAD
10192 || BASELINK_P (templ)));
10194 template_id = lookup_template_function (templ, arguments);
10197 /* If parsing tentatively, replace the sequence of tokens that makes
10198 up the template-id with a CPP_TEMPLATE_ID token. That way,
10199 should we re-parse the token stream, we will not have to repeat
10200 the effort required to do the parse, nor will we issue duplicate
10201 error messages about problems during instantiation of the
10205 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10207 /* Reset the contents of the START_OF_ID token. */
10208 token->type = CPP_TEMPLATE_ID;
10209 /* Retrieve any deferred checks. Do not pop this access checks yet
10210 so the memory will not be reclaimed during token replacing below. */
10211 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10212 token->u.tree_check_value->value = template_id;
10213 token->u.tree_check_value->checks = get_deferred_access_checks ();
10214 token->keyword = RID_MAX;
10216 /* Purge all subsequent tokens. */
10217 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10219 /* ??? Can we actually assume that, if template_id ==
10220 error_mark_node, we will have issued a diagnostic to the
10221 user, as opposed to simply marking the tentative parse as
10223 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10224 error ("%Hparse error in template argument list",
10228 pop_deferring_access_checks ();
10229 return template_id;
10232 /* Parse a template-name.
10237 The standard should actually say:
10241 operator-function-id
10243 A defect report has been filed about this issue.
10245 A conversion-function-id cannot be a template name because they cannot
10246 be part of a template-id. In fact, looking at this code:
10248 a.operator K<int>()
10250 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10251 It is impossible to call a templated conversion-function-id with an
10252 explicit argument list, since the only allowed template parameter is
10253 the type to which it is converting.
10255 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10256 `template' keyword, in a construction like:
10260 In that case `f' is taken to be a template-name, even though there
10261 is no way of knowing for sure.
10263 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10264 name refers to a set of overloaded functions, at least one of which
10265 is a template, or an IDENTIFIER_NODE with the name of the template,
10266 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10267 names are looked up inside uninstantiated templates. */
10270 cp_parser_template_name (cp_parser* parser,
10271 bool template_keyword_p,
10272 bool check_dependency_p,
10273 bool is_declaration,
10274 bool *is_identifier)
10279 cp_token *token = cp_lexer_peek_token (parser->lexer);
10281 /* If the next token is `operator', then we have either an
10282 operator-function-id or a conversion-function-id. */
10283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10285 /* We don't know whether we're looking at an
10286 operator-function-id or a conversion-function-id. */
10287 cp_parser_parse_tentatively (parser);
10288 /* Try an operator-function-id. */
10289 identifier = cp_parser_operator_function_id (parser);
10290 /* If that didn't work, try a conversion-function-id. */
10291 if (!cp_parser_parse_definitely (parser))
10293 cp_parser_error (parser, "expected template-name");
10294 return error_mark_node;
10297 /* Look for the identifier. */
10299 identifier = cp_parser_identifier (parser);
10301 /* If we didn't find an identifier, we don't have a template-id. */
10302 if (identifier == error_mark_node)
10303 return error_mark_node;
10305 /* If the name immediately followed the `template' keyword, then it
10306 is a template-name. However, if the next token is not `<', then
10307 we do not treat it as a template-name, since it is not being used
10308 as part of a template-id. This enables us to handle constructs
10311 template <typename T> struct S { S(); };
10312 template <typename T> S<T>::S();
10314 correctly. We would treat `S' as a template -- if it were `S<T>'
10315 -- but we do not if there is no `<'. */
10317 if (processing_template_decl
10318 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10320 /* In a declaration, in a dependent context, we pretend that the
10321 "template" keyword was present in order to improve error
10322 recovery. For example, given:
10324 template <typename T> void f(T::X<int>);
10326 we want to treat "X<int>" as a template-id. */
10328 && !template_keyword_p
10329 && parser->scope && TYPE_P (parser->scope)
10330 && check_dependency_p
10331 && dependent_scope_p (parser->scope)
10332 /* Do not do this for dtors (or ctors), since they never
10333 need the template keyword before their name. */
10334 && !constructor_name_p (identifier, parser->scope))
10336 cp_token_position start = 0;
10338 /* Explain what went wrong. */
10339 error ("%Hnon-template %qD used as template",
10340 &token->location, identifier);
10341 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10342 parser->scope, identifier);
10343 /* If parsing tentatively, find the location of the "<" token. */
10344 if (cp_parser_simulate_error (parser))
10345 start = cp_lexer_token_position (parser->lexer, true);
10346 /* Parse the template arguments so that we can issue error
10347 messages about them. */
10348 cp_lexer_consume_token (parser->lexer);
10349 cp_parser_enclosed_template_argument_list (parser);
10350 /* Skip tokens until we find a good place from which to
10351 continue parsing. */
10352 cp_parser_skip_to_closing_parenthesis (parser,
10353 /*recovering=*/true,
10355 /*consume_paren=*/false);
10356 /* If parsing tentatively, permanently remove the
10357 template argument list. That will prevent duplicate
10358 error messages from being issued about the missing
10359 "template" keyword. */
10361 cp_lexer_purge_tokens_after (parser->lexer, start);
10363 *is_identifier = true;
10367 /* If the "template" keyword is present, then there is generally
10368 no point in doing name-lookup, so we just return IDENTIFIER.
10369 But, if the qualifying scope is non-dependent then we can
10370 (and must) do name-lookup normally. */
10371 if (template_keyword_p
10373 || (TYPE_P (parser->scope)
10374 && dependent_type_p (parser->scope))))
10378 /* Look up the name. */
10379 decl = cp_parser_lookup_name (parser, identifier,
10381 /*is_template=*/false,
10382 /*is_namespace=*/false,
10383 check_dependency_p,
10384 /*ambiguous_decls=*/NULL,
10386 decl = maybe_get_template_decl_from_type_decl (decl);
10388 /* If DECL is a template, then the name was a template-name. */
10389 if (TREE_CODE (decl) == TEMPLATE_DECL)
10393 tree fn = NULL_TREE;
10395 /* The standard does not explicitly indicate whether a name that
10396 names a set of overloaded declarations, some of which are
10397 templates, is a template-name. However, such a name should
10398 be a template-name; otherwise, there is no way to form a
10399 template-id for the overloaded templates. */
10400 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10401 if (TREE_CODE (fns) == OVERLOAD)
10402 for (fn = fns; fn; fn = OVL_NEXT (fn))
10403 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10408 /* The name does not name a template. */
10409 cp_parser_error (parser, "expected template-name");
10410 return error_mark_node;
10414 /* If DECL is dependent, and refers to a function, then just return
10415 its name; we will look it up again during template instantiation. */
10416 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10418 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10419 if (TYPE_P (scope) && dependent_type_p (scope))
10426 /* Parse a template-argument-list.
10428 template-argument-list:
10429 template-argument ... [opt]
10430 template-argument-list , template-argument ... [opt]
10432 Returns a TREE_VEC containing the arguments. */
10435 cp_parser_template_argument_list (cp_parser* parser)
10437 tree fixed_args[10];
10438 unsigned n_args = 0;
10439 unsigned alloced = 10;
10440 tree *arg_ary = fixed_args;
10442 bool saved_in_template_argument_list_p;
10444 bool saved_non_ice_p;
10446 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10447 parser->in_template_argument_list_p = true;
10448 /* Even if the template-id appears in an integral
10449 constant-expression, the contents of the argument list do
10451 saved_ice_p = parser->integral_constant_expression_p;
10452 parser->integral_constant_expression_p = false;
10453 saved_non_ice_p = parser->non_integral_constant_expression_p;
10454 parser->non_integral_constant_expression_p = false;
10455 /* Parse the arguments. */
10461 /* Consume the comma. */
10462 cp_lexer_consume_token (parser->lexer);
10464 /* Parse the template-argument. */
10465 argument = cp_parser_template_argument (parser);
10467 /* If the next token is an ellipsis, we're expanding a template
10469 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10471 /* Consume the `...' token. */
10472 cp_lexer_consume_token (parser->lexer);
10474 /* Make the argument into a TYPE_PACK_EXPANSION or
10475 EXPR_PACK_EXPANSION. */
10476 argument = make_pack_expansion (argument);
10479 if (n_args == alloced)
10483 if (arg_ary == fixed_args)
10485 arg_ary = XNEWVEC (tree, alloced);
10486 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10489 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10491 arg_ary[n_args++] = argument;
10493 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10495 vec = make_tree_vec (n_args);
10498 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10500 if (arg_ary != fixed_args)
10502 parser->non_integral_constant_expression_p = saved_non_ice_p;
10503 parser->integral_constant_expression_p = saved_ice_p;
10504 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10508 /* Parse a template-argument.
10511 assignment-expression
10515 The representation is that of an assignment-expression, type-id, or
10516 id-expression -- except that the qualified id-expression is
10517 evaluated, so that the value returned is either a DECL or an
10520 Although the standard says "assignment-expression", it forbids
10521 throw-expressions or assignments in the template argument.
10522 Therefore, we use "conditional-expression" instead. */
10525 cp_parser_template_argument (cp_parser* parser)
10530 bool maybe_type_id = false;
10531 cp_token *token = NULL, *argument_start_token = NULL;
10534 /* There's really no way to know what we're looking at, so we just
10535 try each alternative in order.
10539 In a template-argument, an ambiguity between a type-id and an
10540 expression is resolved to a type-id, regardless of the form of
10541 the corresponding template-parameter.
10543 Therefore, we try a type-id first. */
10544 cp_parser_parse_tentatively (parser);
10545 argument = cp_parser_type_id (parser);
10546 /* If there was no error parsing the type-id but the next token is a
10547 '>>', our behavior depends on which dialect of C++ we're
10548 parsing. In C++98, we probably found a typo for '> >'. But there
10549 are type-id which are also valid expressions. For instance:
10551 struct X { int operator >> (int); };
10552 template <int V> struct Foo {};
10555 Here 'X()' is a valid type-id of a function type, but the user just
10556 wanted to write the expression "X() >> 5". Thus, we remember that we
10557 found a valid type-id, but we still try to parse the argument as an
10558 expression to see what happens.
10560 In C++0x, the '>>' will be considered two separate '>'
10562 if (!cp_parser_error_occurred (parser)
10563 && cxx_dialect == cxx98
10564 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10566 maybe_type_id = true;
10567 cp_parser_abort_tentative_parse (parser);
10571 /* If the next token isn't a `,' or a `>', then this argument wasn't
10572 really finished. This means that the argument is not a valid
10574 if (!cp_parser_next_token_ends_template_argument_p (parser))
10575 cp_parser_error (parser, "expected template-argument");
10576 /* If that worked, we're done. */
10577 if (cp_parser_parse_definitely (parser))
10580 /* We're still not sure what the argument will be. */
10581 cp_parser_parse_tentatively (parser);
10582 /* Try a template. */
10583 argument_start_token = cp_lexer_peek_token (parser->lexer);
10584 argument = cp_parser_id_expression (parser,
10585 /*template_keyword_p=*/false,
10586 /*check_dependency_p=*/true,
10588 /*declarator_p=*/false,
10589 /*optional_p=*/false);
10590 /* If the next token isn't a `,' or a `>', then this argument wasn't
10591 really finished. */
10592 if (!cp_parser_next_token_ends_template_argument_p (parser))
10593 cp_parser_error (parser, "expected template-argument");
10594 if (!cp_parser_error_occurred (parser))
10596 /* Figure out what is being referred to. If the id-expression
10597 was for a class template specialization, then we will have a
10598 TYPE_DECL at this point. There is no need to do name lookup
10599 at this point in that case. */
10600 if (TREE_CODE (argument) != TYPE_DECL)
10601 argument = cp_parser_lookup_name (parser, argument,
10603 /*is_template=*/template_p,
10604 /*is_namespace=*/false,
10605 /*check_dependency=*/true,
10606 /*ambiguous_decls=*/NULL,
10607 argument_start_token->location);
10608 if (TREE_CODE (argument) != TEMPLATE_DECL
10609 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10610 cp_parser_error (parser, "expected template-name");
10612 if (cp_parser_parse_definitely (parser))
10614 /* It must be a non-type argument. There permitted cases are given
10615 in [temp.arg.nontype]:
10617 -- an integral constant-expression of integral or enumeration
10620 -- the name of a non-type template-parameter; or
10622 -- the name of an object or function with external linkage...
10624 -- the address of an object or function with external linkage...
10626 -- a pointer to member... */
10627 /* Look for a non-type template parameter. */
10628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10630 cp_parser_parse_tentatively (parser);
10631 argument = cp_parser_primary_expression (parser,
10632 /*address_p=*/false,
10634 /*template_arg_p=*/true,
10636 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10637 || !cp_parser_next_token_ends_template_argument_p (parser))
10638 cp_parser_simulate_error (parser);
10639 if (cp_parser_parse_definitely (parser))
10643 /* If the next token is "&", the argument must be the address of an
10644 object or function with external linkage. */
10645 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10647 cp_lexer_consume_token (parser->lexer);
10648 /* See if we might have an id-expression. */
10649 token = cp_lexer_peek_token (parser->lexer);
10650 if (token->type == CPP_NAME
10651 || token->keyword == RID_OPERATOR
10652 || token->type == CPP_SCOPE
10653 || token->type == CPP_TEMPLATE_ID
10654 || token->type == CPP_NESTED_NAME_SPECIFIER)
10656 cp_parser_parse_tentatively (parser);
10657 argument = cp_parser_primary_expression (parser,
10660 /*template_arg_p=*/true,
10662 if (cp_parser_error_occurred (parser)
10663 || !cp_parser_next_token_ends_template_argument_p (parser))
10664 cp_parser_abort_tentative_parse (parser);
10667 if (TREE_CODE (argument) == INDIRECT_REF)
10669 gcc_assert (REFERENCE_REF_P (argument));
10670 argument = TREE_OPERAND (argument, 0);
10673 if (TREE_CODE (argument) == VAR_DECL)
10675 /* A variable without external linkage might still be a
10676 valid constant-expression, so no error is issued here
10677 if the external-linkage check fails. */
10678 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10679 cp_parser_simulate_error (parser);
10681 else if (is_overloaded_fn (argument))
10682 /* All overloaded functions are allowed; if the external
10683 linkage test does not pass, an error will be issued
10687 && (TREE_CODE (argument) == OFFSET_REF
10688 || TREE_CODE (argument) == SCOPE_REF))
10689 /* A pointer-to-member. */
10691 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10694 cp_parser_simulate_error (parser);
10696 if (cp_parser_parse_definitely (parser))
10699 argument = build_x_unary_op (ADDR_EXPR, argument,
10700 tf_warning_or_error);
10705 /* If the argument started with "&", there are no other valid
10706 alternatives at this point. */
10709 cp_parser_error (parser, "invalid non-type template argument");
10710 return error_mark_node;
10713 /* If the argument wasn't successfully parsed as a type-id followed
10714 by '>>', the argument can only be a constant expression now.
10715 Otherwise, we try parsing the constant-expression tentatively,
10716 because the argument could really be a type-id. */
10718 cp_parser_parse_tentatively (parser);
10719 argument = cp_parser_constant_expression (parser,
10720 /*allow_non_constant_p=*/false,
10721 /*non_constant_p=*/NULL);
10722 argument = fold_non_dependent_expr (argument);
10723 if (!maybe_type_id)
10725 if (!cp_parser_next_token_ends_template_argument_p (parser))
10726 cp_parser_error (parser, "expected template-argument");
10727 if (cp_parser_parse_definitely (parser))
10729 /* We did our best to parse the argument as a non type-id, but that
10730 was the only alternative that matched (albeit with a '>' after
10731 it). We can assume it's just a typo from the user, and a
10732 diagnostic will then be issued. */
10733 return cp_parser_type_id (parser);
10736 /* Parse an explicit-instantiation.
10738 explicit-instantiation:
10739 template declaration
10741 Although the standard says `declaration', what it really means is:
10743 explicit-instantiation:
10744 template decl-specifier-seq [opt] declarator [opt] ;
10746 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10747 supposed to be allowed. A defect report has been filed about this
10752 explicit-instantiation:
10753 storage-class-specifier template
10754 decl-specifier-seq [opt] declarator [opt] ;
10755 function-specifier template
10756 decl-specifier-seq [opt] declarator [opt] ; */
10759 cp_parser_explicit_instantiation (cp_parser* parser)
10761 int declares_class_or_enum;
10762 cp_decl_specifier_seq decl_specifiers;
10763 tree extension_specifier = NULL_TREE;
10766 /* Look for an (optional) storage-class-specifier or
10767 function-specifier. */
10768 if (cp_parser_allow_gnu_extensions_p (parser))
10770 extension_specifier
10771 = cp_parser_storage_class_specifier_opt (parser);
10772 if (!extension_specifier)
10773 extension_specifier
10774 = cp_parser_function_specifier_opt (parser,
10775 /*decl_specs=*/NULL);
10778 /* Look for the `template' keyword. */
10779 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10780 /* Let the front end know that we are processing an explicit
10782 begin_explicit_instantiation ();
10783 /* [temp.explicit] says that we are supposed to ignore access
10784 control while processing explicit instantiation directives. */
10785 push_deferring_access_checks (dk_no_check);
10786 /* Parse a decl-specifier-seq. */
10787 token = cp_lexer_peek_token (parser->lexer);
10788 cp_parser_decl_specifier_seq (parser,
10789 CP_PARSER_FLAGS_OPTIONAL,
10791 &declares_class_or_enum);
10792 /* If there was exactly one decl-specifier, and it declared a class,
10793 and there's no declarator, then we have an explicit type
10795 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10799 type = check_tag_decl (&decl_specifiers);
10800 /* Turn access control back on for names used during
10801 template instantiation. */
10802 pop_deferring_access_checks ();
10804 do_type_instantiation (type, extension_specifier,
10805 /*complain=*/tf_error);
10809 cp_declarator *declarator;
10812 /* Parse the declarator. */
10814 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10815 /*ctor_dtor_or_conv_p=*/NULL,
10816 /*parenthesized_p=*/NULL,
10817 /*member_p=*/false);
10818 if (declares_class_or_enum & 2)
10819 cp_parser_check_for_definition_in_return_type (declarator,
10820 decl_specifiers.type,
10821 decl_specifiers.type_location);
10822 if (declarator != cp_error_declarator)
10824 decl = grokdeclarator (declarator, &decl_specifiers,
10825 NORMAL, 0, &decl_specifiers.attributes);
10826 /* Turn access control back on for names used during
10827 template instantiation. */
10828 pop_deferring_access_checks ();
10829 /* Do the explicit instantiation. */
10830 do_decl_instantiation (decl, extension_specifier);
10834 pop_deferring_access_checks ();
10835 /* Skip the body of the explicit instantiation. */
10836 cp_parser_skip_to_end_of_statement (parser);
10839 /* We're done with the instantiation. */
10840 end_explicit_instantiation ();
10842 cp_parser_consume_semicolon_at_end_of_statement (parser);
10845 /* Parse an explicit-specialization.
10847 explicit-specialization:
10848 template < > declaration
10850 Although the standard says `declaration', what it really means is:
10852 explicit-specialization:
10853 template <> decl-specifier [opt] init-declarator [opt] ;
10854 template <> function-definition
10855 template <> explicit-specialization
10856 template <> template-declaration */
10859 cp_parser_explicit_specialization (cp_parser* parser)
10861 bool need_lang_pop;
10862 cp_token *token = cp_lexer_peek_token (parser->lexer);
10864 /* Look for the `template' keyword. */
10865 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10866 /* Look for the `<'. */
10867 cp_parser_require (parser, CPP_LESS, "%<<%>");
10868 /* Look for the `>'. */
10869 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10870 /* We have processed another parameter list. */
10871 ++parser->num_template_parameter_lists;
10874 A template ... explicit specialization ... shall not have C
10876 if (current_lang_name == lang_name_c)
10878 error ("%Htemplate specialization with C linkage", &token->location);
10879 /* Give it C++ linkage to avoid confusing other parts of the
10881 push_lang_context (lang_name_cplusplus);
10882 need_lang_pop = true;
10885 need_lang_pop = false;
10886 /* Let the front end know that we are beginning a specialization. */
10887 if (!begin_specialization ())
10889 end_specialization ();
10893 /* If the next keyword is `template', we need to figure out whether
10894 or not we're looking a template-declaration. */
10895 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10897 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10898 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10899 cp_parser_template_declaration_after_export (parser,
10900 /*member_p=*/false);
10902 cp_parser_explicit_specialization (parser);
10905 /* Parse the dependent declaration. */
10906 cp_parser_single_declaration (parser,
10908 /*member_p=*/false,
10909 /*explicit_specialization_p=*/true,
10910 /*friend_p=*/NULL);
10911 /* We're done with the specialization. */
10912 end_specialization ();
10913 /* For the erroneous case of a template with C linkage, we pushed an
10914 implicit C++ linkage scope; exit that scope now. */
10916 pop_lang_context ();
10917 /* We're done with this parameter list. */
10918 --parser->num_template_parameter_lists;
10921 /* Parse a type-specifier.
10924 simple-type-specifier
10927 elaborated-type-specifier
10935 Returns a representation of the type-specifier. For a
10936 class-specifier, enum-specifier, or elaborated-type-specifier, a
10937 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10939 The parser flags FLAGS is used to control type-specifier parsing.
10941 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10942 in a decl-specifier-seq.
10944 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10945 class-specifier, enum-specifier, or elaborated-type-specifier, then
10946 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10947 if a type is declared; 2 if it is defined. Otherwise, it is set to
10950 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10951 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10952 is set to FALSE. */
10955 cp_parser_type_specifier (cp_parser* parser,
10956 cp_parser_flags flags,
10957 cp_decl_specifier_seq *decl_specs,
10958 bool is_declaration,
10959 int* declares_class_or_enum,
10960 bool* is_cv_qualifier)
10962 tree type_spec = NULL_TREE;
10965 cp_decl_spec ds = ds_last;
10967 /* Assume this type-specifier does not declare a new type. */
10968 if (declares_class_or_enum)
10969 *declares_class_or_enum = 0;
10970 /* And that it does not specify a cv-qualifier. */
10971 if (is_cv_qualifier)
10972 *is_cv_qualifier = false;
10973 /* Peek at the next token. */
10974 token = cp_lexer_peek_token (parser->lexer);
10976 /* If we're looking at a keyword, we can use that to guide the
10977 production we choose. */
10978 keyword = token->keyword;
10982 /* Look for the enum-specifier. */
10983 type_spec = cp_parser_enum_specifier (parser);
10984 /* If that worked, we're done. */
10987 if (declares_class_or_enum)
10988 *declares_class_or_enum = 2;
10990 cp_parser_set_decl_spec_type (decl_specs,
10993 /*user_defined_p=*/true);
10997 goto elaborated_type_specifier;
10999 /* Any of these indicate either a class-specifier, or an
11000 elaborated-type-specifier. */
11004 /* Parse tentatively so that we can back up if we don't find a
11005 class-specifier. */
11006 cp_parser_parse_tentatively (parser);
11007 /* Look for the class-specifier. */
11008 type_spec = cp_parser_class_specifier (parser);
11009 /* If that worked, we're done. */
11010 if (cp_parser_parse_definitely (parser))
11012 if (declares_class_or_enum)
11013 *declares_class_or_enum = 2;
11015 cp_parser_set_decl_spec_type (decl_specs,
11018 /*user_defined_p=*/true);
11022 /* Fall through. */
11023 elaborated_type_specifier:
11024 /* We're declaring (not defining) a class or enum. */
11025 if (declares_class_or_enum)
11026 *declares_class_or_enum = 1;
11028 /* Fall through. */
11030 /* Look for an elaborated-type-specifier. */
11032 = (cp_parser_elaborated_type_specifier
11034 decl_specs && decl_specs->specs[(int) ds_friend],
11037 cp_parser_set_decl_spec_type (decl_specs,
11040 /*user_defined_p=*/true);
11045 if (is_cv_qualifier)
11046 *is_cv_qualifier = true;
11051 if (is_cv_qualifier)
11052 *is_cv_qualifier = true;
11057 if (is_cv_qualifier)
11058 *is_cv_qualifier = true;
11062 /* The `__complex__' keyword is a GNU extension. */
11070 /* Handle simple keywords. */
11075 ++decl_specs->specs[(int)ds];
11076 decl_specs->any_specifiers_p = true;
11078 return cp_lexer_consume_token (parser->lexer)->u.value;
11081 /* If we do not already have a type-specifier, assume we are looking
11082 at a simple-type-specifier. */
11083 type_spec = cp_parser_simple_type_specifier (parser,
11087 /* If we didn't find a type-specifier, and a type-specifier was not
11088 optional in this context, issue an error message. */
11089 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11091 cp_parser_error (parser, "expected type specifier");
11092 return error_mark_node;
11098 /* Parse a simple-type-specifier.
11100 simple-type-specifier:
11101 :: [opt] nested-name-specifier [opt] type-name
11102 :: [opt] nested-name-specifier template template-id
11117 simple-type-specifier:
11119 decltype ( expression )
11125 simple-type-specifier:
11126 __typeof__ unary-expression
11127 __typeof__ ( type-id )
11129 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11130 appropriately updated. */
11133 cp_parser_simple_type_specifier (cp_parser* parser,
11134 cp_decl_specifier_seq *decl_specs,
11135 cp_parser_flags flags)
11137 tree type = NULL_TREE;
11140 /* Peek at the next token. */
11141 token = cp_lexer_peek_token (parser->lexer);
11143 /* If we're looking at a keyword, things are easy. */
11144 switch (token->keyword)
11148 decl_specs->explicit_char_p = true;
11149 type = char_type_node;
11152 type = char16_type_node;
11155 type = char32_type_node;
11158 type = wchar_type_node;
11161 type = boolean_type_node;
11165 ++decl_specs->specs[(int) ds_short];
11166 type = short_integer_type_node;
11170 decl_specs->explicit_int_p = true;
11171 type = integer_type_node;
11175 ++decl_specs->specs[(int) ds_long];
11176 type = long_integer_type_node;
11180 ++decl_specs->specs[(int) ds_signed];
11181 type = integer_type_node;
11185 ++decl_specs->specs[(int) ds_unsigned];
11186 type = unsigned_type_node;
11189 type = float_type_node;
11192 type = double_type_node;
11195 type = void_type_node;
11199 maybe_warn_cpp0x ("C++0x auto");
11200 type = make_auto ();
11204 /* Parse the `decltype' type. */
11205 type = cp_parser_decltype (parser);
11208 cp_parser_set_decl_spec_type (decl_specs, type,
11210 /*user_defined_p=*/true);
11215 /* Consume the `typeof' token. */
11216 cp_lexer_consume_token (parser->lexer);
11217 /* Parse the operand to `typeof'. */
11218 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11219 /* If it is not already a TYPE, take its type. */
11220 if (!TYPE_P (type))
11221 type = finish_typeof (type);
11224 cp_parser_set_decl_spec_type (decl_specs, type,
11226 /*user_defined_p=*/true);
11234 /* If the type-specifier was for a built-in type, we're done. */
11239 /* Record the type. */
11241 && (token->keyword != RID_SIGNED
11242 && token->keyword != RID_UNSIGNED
11243 && token->keyword != RID_SHORT
11244 && token->keyword != RID_LONG))
11245 cp_parser_set_decl_spec_type (decl_specs,
11248 /*user_defined=*/false);
11250 decl_specs->any_specifiers_p = true;
11252 /* Consume the token. */
11253 id = cp_lexer_consume_token (parser->lexer)->u.value;
11255 /* There is no valid C++ program where a non-template type is
11256 followed by a "<". That usually indicates that the user thought
11257 that the type was a template. */
11258 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11260 return TYPE_NAME (type);
11263 /* The type-specifier must be a user-defined type. */
11264 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11269 /* Don't gobble tokens or issue error messages if this is an
11270 optional type-specifier. */
11271 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11272 cp_parser_parse_tentatively (parser);
11274 /* Look for the optional `::' operator. */
11276 = (cp_parser_global_scope_opt (parser,
11277 /*current_scope_valid_p=*/false)
11279 /* Look for the nested-name specifier. */
11281 = (cp_parser_nested_name_specifier_opt (parser,
11282 /*typename_keyword_p=*/false,
11283 /*check_dependency_p=*/true,
11285 /*is_declaration=*/false)
11287 token = cp_lexer_peek_token (parser->lexer);
11288 /* If we have seen a nested-name-specifier, and the next token
11289 is `template', then we are using the template-id production. */
11291 && cp_parser_optional_template_keyword (parser))
11293 /* Look for the template-id. */
11294 type = cp_parser_template_id (parser,
11295 /*template_keyword_p=*/true,
11296 /*check_dependency_p=*/true,
11297 /*is_declaration=*/false);
11298 /* If the template-id did not name a type, we are out of
11300 if (TREE_CODE (type) != TYPE_DECL)
11302 cp_parser_error (parser, "expected template-id for type");
11306 /* Otherwise, look for a type-name. */
11308 type = cp_parser_type_name (parser);
11309 /* Keep track of all name-lookups performed in class scopes. */
11313 && TREE_CODE (type) == TYPE_DECL
11314 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11315 maybe_note_name_used_in_class (DECL_NAME (type), type);
11316 /* If it didn't work out, we don't have a TYPE. */
11317 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11318 && !cp_parser_parse_definitely (parser))
11320 if (type && decl_specs)
11321 cp_parser_set_decl_spec_type (decl_specs, type,
11323 /*user_defined=*/true);
11326 /* If we didn't get a type-name, issue an error message. */
11327 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11329 cp_parser_error (parser, "expected type-name");
11330 return error_mark_node;
11333 /* There is no valid C++ program where a non-template type is
11334 followed by a "<". That usually indicates that the user thought
11335 that the type was a template. */
11336 if (type && type != error_mark_node)
11338 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11339 If it is, then the '<'...'>' enclose protocol names rather than
11340 template arguments, and so everything is fine. */
11341 if (c_dialect_objc ()
11342 && (objc_is_id (type) || objc_is_class_name (type)))
11344 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11345 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11347 /* Clobber the "unqualified" type previously entered into
11348 DECL_SPECS with the new, improved protocol-qualified version. */
11350 decl_specs->type = qual_type;
11355 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11362 /* Parse a type-name.
11375 Returns a TYPE_DECL for the type. */
11378 cp_parser_type_name (cp_parser* parser)
11382 /* We can't know yet whether it is a class-name or not. */
11383 cp_parser_parse_tentatively (parser);
11384 /* Try a class-name. */
11385 type_decl = cp_parser_class_name (parser,
11386 /*typename_keyword_p=*/false,
11387 /*template_keyword_p=*/false,
11389 /*check_dependency_p=*/true,
11390 /*class_head_p=*/false,
11391 /*is_declaration=*/false);
11392 /* If it's not a class-name, keep looking. */
11393 if (!cp_parser_parse_definitely (parser))
11395 /* It must be a typedef-name or an enum-name. */
11396 return cp_parser_nonclass_name (parser);
11402 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11410 Returns a TYPE_DECL for the type. */
11413 cp_parser_nonclass_name (cp_parser* parser)
11418 cp_token *token = cp_lexer_peek_token (parser->lexer);
11419 identifier = cp_parser_identifier (parser);
11420 if (identifier == error_mark_node)
11421 return error_mark_node;
11423 /* Look up the type-name. */
11424 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11426 if (TREE_CODE (type_decl) != TYPE_DECL
11427 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11429 /* See if this is an Objective-C type. */
11430 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11431 tree type = objc_get_protocol_qualified_type (identifier, protos);
11433 type_decl = TYPE_NAME (type);
11436 /* Issue an error if we did not find a type-name. */
11437 if (TREE_CODE (type_decl) != TYPE_DECL)
11439 if (!cp_parser_simulate_error (parser))
11440 cp_parser_name_lookup_error (parser, identifier, type_decl,
11441 "is not a type", token->location);
11442 return error_mark_node;
11444 /* Remember that the name was used in the definition of the
11445 current class so that we can check later to see if the
11446 meaning would have been different after the class was
11447 entirely defined. */
11448 else if (type_decl != error_mark_node
11450 maybe_note_name_used_in_class (identifier, type_decl);
11455 /* Parse an elaborated-type-specifier. Note that the grammar given
11456 here incorporates the resolution to DR68.
11458 elaborated-type-specifier:
11459 class-key :: [opt] nested-name-specifier [opt] identifier
11460 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11461 enum-key :: [opt] nested-name-specifier [opt] identifier
11462 typename :: [opt] nested-name-specifier identifier
11463 typename :: [opt] nested-name-specifier template [opt]
11468 elaborated-type-specifier:
11469 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11470 class-key attributes :: [opt] nested-name-specifier [opt]
11471 template [opt] template-id
11472 enum attributes :: [opt] nested-name-specifier [opt] identifier
11474 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11475 declared `friend'. If IS_DECLARATION is TRUE, then this
11476 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11477 something is being declared.
11479 Returns the TYPE specified. */
11482 cp_parser_elaborated_type_specifier (cp_parser* parser,
11484 bool is_declaration)
11486 enum tag_types tag_type;
11488 tree type = NULL_TREE;
11489 tree attributes = NULL_TREE;
11490 cp_token *token = NULL;
11492 /* See if we're looking at the `enum' keyword. */
11493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11495 /* Consume the `enum' token. */
11496 cp_lexer_consume_token (parser->lexer);
11497 /* Remember that it's an enumeration type. */
11498 tag_type = enum_type;
11499 /* Parse the optional `struct' or `class' key (for C++0x scoped
11501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11502 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11504 if (cxx_dialect == cxx98)
11505 maybe_warn_cpp0x ("scoped enums");
11507 /* Consume the `struct' or `class'. */
11508 cp_lexer_consume_token (parser->lexer);
11510 /* Parse the attributes. */
11511 attributes = cp_parser_attributes_opt (parser);
11513 /* Or, it might be `typename'. */
11514 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11517 /* Consume the `typename' token. */
11518 cp_lexer_consume_token (parser->lexer);
11519 /* Remember that it's a `typename' type. */
11520 tag_type = typename_type;
11521 /* The `typename' keyword is only allowed in templates. */
11522 if (!processing_template_decl)
11523 permerror (input_location, "using %<typename%> outside of template");
11525 /* Otherwise it must be a class-key. */
11528 tag_type = cp_parser_class_key (parser);
11529 if (tag_type == none_type)
11530 return error_mark_node;
11531 /* Parse the attributes. */
11532 attributes = cp_parser_attributes_opt (parser);
11535 /* Look for the `::' operator. */
11536 cp_parser_global_scope_opt (parser,
11537 /*current_scope_valid_p=*/false);
11538 /* Look for the nested-name-specifier. */
11539 if (tag_type == typename_type)
11541 if (!cp_parser_nested_name_specifier (parser,
11542 /*typename_keyword_p=*/true,
11543 /*check_dependency_p=*/true,
11546 return error_mark_node;
11549 /* Even though `typename' is not present, the proposed resolution
11550 to Core Issue 180 says that in `class A<T>::B', `B' should be
11551 considered a type-name, even if `A<T>' is dependent. */
11552 cp_parser_nested_name_specifier_opt (parser,
11553 /*typename_keyword_p=*/true,
11554 /*check_dependency_p=*/true,
11557 /* For everything but enumeration types, consider a template-id.
11558 For an enumeration type, consider only a plain identifier. */
11559 if (tag_type != enum_type)
11561 bool template_p = false;
11564 /* Allow the `template' keyword. */
11565 template_p = cp_parser_optional_template_keyword (parser);
11566 /* If we didn't see `template', we don't know if there's a
11567 template-id or not. */
11569 cp_parser_parse_tentatively (parser);
11570 /* Parse the template-id. */
11571 token = cp_lexer_peek_token (parser->lexer);
11572 decl = cp_parser_template_id (parser, template_p,
11573 /*check_dependency_p=*/true,
11575 /* If we didn't find a template-id, look for an ordinary
11577 if (!template_p && !cp_parser_parse_definitely (parser))
11579 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11580 in effect, then we must assume that, upon instantiation, the
11581 template will correspond to a class. */
11582 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11583 && tag_type == typename_type)
11584 type = make_typename_type (parser->scope, decl,
11586 /*complain=*/tf_error);
11587 /* If the `typename' keyword is in effect and DECL is not a type
11588 decl. Then type is non existant. */
11589 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11592 type = TREE_TYPE (decl);
11597 token = cp_lexer_peek_token (parser->lexer);
11598 identifier = cp_parser_identifier (parser);
11600 if (identifier == error_mark_node)
11602 parser->scope = NULL_TREE;
11603 return error_mark_node;
11606 /* For a `typename', we needn't call xref_tag. */
11607 if (tag_type == typename_type
11608 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11609 return cp_parser_make_typename_type (parser, parser->scope,
11612 /* Look up a qualified name in the usual way. */
11616 tree ambiguous_decls;
11618 decl = cp_parser_lookup_name (parser, identifier,
11620 /*is_template=*/false,
11621 /*is_namespace=*/false,
11622 /*check_dependency=*/true,
11626 /* If the lookup was ambiguous, an error will already have been
11628 if (ambiguous_decls)
11629 return error_mark_node;
11631 /* If we are parsing friend declaration, DECL may be a
11632 TEMPLATE_DECL tree node here. However, we need to check
11633 whether this TEMPLATE_DECL results in valid code. Consider
11634 the following example:
11637 template <class T> class C {};
11640 template <class T> friend class N::C; // #1, valid code
11642 template <class T> class Y {
11643 friend class N::C; // #2, invalid code
11646 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11647 name lookup of `N::C'. We see that friend declaration must
11648 be template for the code to be valid. Note that
11649 processing_template_decl does not work here since it is
11650 always 1 for the above two cases. */
11652 decl = (cp_parser_maybe_treat_template_as_class
11653 (decl, /*tag_name_p=*/is_friend
11654 && parser->num_template_parameter_lists));
11656 if (TREE_CODE (decl) != TYPE_DECL)
11658 cp_parser_diagnose_invalid_type_name (parser,
11662 return error_mark_node;
11665 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11667 bool allow_template = (parser->num_template_parameter_lists
11668 || DECL_SELF_REFERENCE_P (decl));
11669 type = check_elaborated_type_specifier (tag_type, decl,
11672 if (type == error_mark_node)
11673 return error_mark_node;
11676 /* Forward declarations of nested types, such as
11681 are invalid unless all components preceding the final '::'
11682 are complete. If all enclosing types are complete, these
11683 declarations become merely pointless.
11685 Invalid forward declarations of nested types are errors
11686 caught elsewhere in parsing. Those that are pointless arrive
11689 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11690 && !is_friend && !processing_explicit_instantiation)
11691 warning (0, "declaration %qD does not declare anything", decl);
11693 type = TREE_TYPE (decl);
11697 /* An elaborated-type-specifier sometimes introduces a new type and
11698 sometimes names an existing type. Normally, the rule is that it
11699 introduces a new type only if there is not an existing type of
11700 the same name already in scope. For example, given:
11703 void f() { struct S s; }
11705 the `struct S' in the body of `f' is the same `struct S' as in
11706 the global scope; the existing definition is used. However, if
11707 there were no global declaration, this would introduce a new
11708 local class named `S'.
11710 An exception to this rule applies to the following code:
11712 namespace N { struct S; }
11714 Here, the elaborated-type-specifier names a new type
11715 unconditionally; even if there is already an `S' in the
11716 containing scope this declaration names a new type.
11717 This exception only applies if the elaborated-type-specifier
11718 forms the complete declaration:
11722 A declaration consisting solely of `class-key identifier ;' is
11723 either a redeclaration of the name in the current scope or a
11724 forward declaration of the identifier as a class name. It
11725 introduces the name into the current scope.
11727 We are in this situation precisely when the next token is a `;'.
11729 An exception to the exception is that a `friend' declaration does
11730 *not* name a new type; i.e., given:
11732 struct S { friend struct T; };
11734 `T' is not a new type in the scope of `S'.
11736 Also, `new struct S' or `sizeof (struct S)' never results in the
11737 definition of a new type; a new type can only be declared in a
11738 declaration context. */
11744 /* Friends have special name lookup rules. */
11745 ts = ts_within_enclosing_non_class;
11746 else if (is_declaration
11747 && cp_lexer_next_token_is (parser->lexer,
11749 /* This is a `class-key identifier ;' */
11755 (parser->num_template_parameter_lists
11756 && (cp_parser_next_token_starts_class_definition_p (parser)
11757 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11758 /* An unqualified name was used to reference this type, so
11759 there were no qualifying templates. */
11760 if (!cp_parser_check_template_parameters (parser,
11761 /*num_templates=*/0,
11763 return error_mark_node;
11764 type = xref_tag (tag_type, identifier, ts, template_p);
11768 if (type == error_mark_node)
11769 return error_mark_node;
11771 /* Allow attributes on forward declarations of classes. */
11774 if (TREE_CODE (type) == TYPENAME_TYPE)
11775 warning (OPT_Wattributes,
11776 "attributes ignored on uninstantiated type");
11777 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11778 && ! processing_explicit_instantiation)
11779 warning (OPT_Wattributes,
11780 "attributes ignored on template instantiation");
11781 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11782 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11784 warning (OPT_Wattributes,
11785 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11788 if (tag_type != enum_type)
11789 cp_parser_check_class_key (tag_type, type);
11791 /* A "<" cannot follow an elaborated type specifier. If that
11792 happens, the user was probably trying to form a template-id. */
11793 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11798 /* Parse an enum-specifier.
11801 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11806 enum struct [C++0x]
11809 : type-specifier-seq
11812 enum-key attributes[opt] identifier [opt] enum-base [opt]
11813 { enumerator-list [opt] }attributes[opt]
11815 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11816 if the token stream isn't an enum-specifier after all. */
11819 cp_parser_enum_specifier (cp_parser* parser)
11824 bool scoped_enum_p = false;
11825 bool has_underlying_type = false;
11826 tree underlying_type = NULL_TREE;
11828 /* Parse tentatively so that we can back up if we don't find a
11830 cp_parser_parse_tentatively (parser);
11832 /* Caller guarantees that the current token is 'enum', an identifier
11833 possibly follows, and the token after that is an opening brace.
11834 If we don't have an identifier, fabricate an anonymous name for
11835 the enumeration being defined. */
11836 cp_lexer_consume_token (parser->lexer);
11838 /* Parse the "class" or "struct", which indicates a scoped
11839 enumeration type in C++0x. */
11840 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11841 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11843 if (cxx_dialect == cxx98)
11844 maybe_warn_cpp0x ("scoped enums");
11846 /* Consume the `struct' or `class' token. */
11847 cp_lexer_consume_token (parser->lexer);
11849 scoped_enum_p = true;
11852 attributes = cp_parser_attributes_opt (parser);
11854 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11855 identifier = cp_parser_identifier (parser);
11857 identifier = make_anon_name ();
11859 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11860 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11862 cp_decl_specifier_seq type_specifiers;
11864 /* At this point this is surely not elaborated type specifier. */
11865 if (!cp_parser_parse_definitely (parser))
11868 if (cxx_dialect == cxx98)
11869 maybe_warn_cpp0x ("scoped enums");
11871 /* Consume the `:'. */
11872 cp_lexer_consume_token (parser->lexer);
11874 has_underlying_type = true;
11876 /* Parse the type-specifier-seq. */
11877 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11880 /* If that didn't work, stop. */
11881 if (type_specifiers.type != error_mark_node)
11883 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11884 /*initialized=*/0, NULL);
11885 if (underlying_type == error_mark_node)
11886 underlying_type = NULL_TREE;
11890 /* Look for the `{' but don't consume it yet. */
11891 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11893 cp_parser_error (parser, "expected %<{%>");
11894 if (has_underlying_type)
11898 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11901 /* Issue an error message if type-definitions are forbidden here. */
11902 if (!cp_parser_check_type_definition (parser))
11903 type = error_mark_node;
11905 /* Create the new type. We do this before consuming the opening
11906 brace so the enum will be recorded as being on the line of its
11907 tag (or the 'enum' keyword, if there is no tag). */
11908 type = start_enum (identifier, underlying_type, scoped_enum_p);
11910 /* Consume the opening brace. */
11911 cp_lexer_consume_token (parser->lexer);
11913 if (type == error_mark_node)
11915 cp_parser_skip_to_end_of_block_or_statement (parser);
11916 return error_mark_node;
11919 /* If the next token is not '}', then there are some enumerators. */
11920 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11921 cp_parser_enumerator_list (parser, type);
11923 /* Consume the final '}'. */
11924 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11926 /* Look for trailing attributes to apply to this enumeration, and
11927 apply them if appropriate. */
11928 if (cp_parser_allow_gnu_extensions_p (parser))
11930 tree trailing_attr = cp_parser_attributes_opt (parser);
11931 trailing_attr = chainon (trailing_attr, attributes);
11932 cplus_decl_attributes (&type,
11934 (int) ATTR_FLAG_TYPE_IN_PLACE);
11937 /* Finish up the enumeration. */
11938 finish_enum (type);
11943 /* Parse an enumerator-list. The enumerators all have the indicated
11947 enumerator-definition
11948 enumerator-list , enumerator-definition */
11951 cp_parser_enumerator_list (cp_parser* parser, tree type)
11955 /* Parse an enumerator-definition. */
11956 cp_parser_enumerator_definition (parser, type);
11958 /* If the next token is not a ',', we've reached the end of
11960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11962 /* Otherwise, consume the `,' and keep going. */
11963 cp_lexer_consume_token (parser->lexer);
11964 /* If the next token is a `}', there is a trailing comma. */
11965 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11967 if (!in_system_header)
11968 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11974 /* Parse an enumerator-definition. The enumerator has the indicated
11977 enumerator-definition:
11979 enumerator = constant-expression
11985 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11990 /* Look for the identifier. */
11991 identifier = cp_parser_identifier (parser);
11992 if (identifier == error_mark_node)
11995 /* If the next token is an '=', then there is an explicit value. */
11996 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11998 /* Consume the `=' token. */
11999 cp_lexer_consume_token (parser->lexer);
12000 /* Parse the value. */
12001 value = cp_parser_constant_expression (parser,
12002 /*allow_non_constant_p=*/false,
12008 /* Create the enumerator. */
12009 build_enumerator (identifier, value, type);
12012 /* Parse a namespace-name.
12015 original-namespace-name
12018 Returns the NAMESPACE_DECL for the namespace. */
12021 cp_parser_namespace_name (cp_parser* parser)
12024 tree namespace_decl;
12026 cp_token *token = cp_lexer_peek_token (parser->lexer);
12028 /* Get the name of the namespace. */
12029 identifier = cp_parser_identifier (parser);
12030 if (identifier == error_mark_node)
12031 return error_mark_node;
12033 /* Look up the identifier in the currently active scope. Look only
12034 for namespaces, due to:
12036 [basic.lookup.udir]
12038 When looking up a namespace-name in a using-directive or alias
12039 definition, only namespace names are considered.
12043 [basic.lookup.qual]
12045 During the lookup of a name preceding the :: scope resolution
12046 operator, object, function, and enumerator names are ignored.
12048 (Note that cp_parser_qualifying_entity only calls this
12049 function if the token after the name is the scope resolution
12051 namespace_decl = cp_parser_lookup_name (parser, identifier,
12053 /*is_template=*/false,
12054 /*is_namespace=*/true,
12055 /*check_dependency=*/true,
12056 /*ambiguous_decls=*/NULL,
12058 /* If it's not a namespace, issue an error. */
12059 if (namespace_decl == error_mark_node
12060 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12062 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12063 error ("%H%qD is not a namespace-name", &token->location, identifier);
12064 cp_parser_error (parser, "expected namespace-name");
12065 namespace_decl = error_mark_node;
12068 return namespace_decl;
12071 /* Parse a namespace-definition.
12073 namespace-definition:
12074 named-namespace-definition
12075 unnamed-namespace-definition
12077 named-namespace-definition:
12078 original-namespace-definition
12079 extension-namespace-definition
12081 original-namespace-definition:
12082 namespace identifier { namespace-body }
12084 extension-namespace-definition:
12085 namespace original-namespace-name { namespace-body }
12087 unnamed-namespace-definition:
12088 namespace { namespace-body } */
12091 cp_parser_namespace_definition (cp_parser* parser)
12093 tree identifier, attribs;
12094 bool has_visibility;
12097 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12100 cp_lexer_consume_token (parser->lexer);
12105 /* Look for the `namespace' keyword. */
12106 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12108 /* Get the name of the namespace. We do not attempt to distinguish
12109 between an original-namespace-definition and an
12110 extension-namespace-definition at this point. The semantic
12111 analysis routines are responsible for that. */
12112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12113 identifier = cp_parser_identifier (parser);
12115 identifier = NULL_TREE;
12117 /* Parse any specified attributes. */
12118 attribs = cp_parser_attributes_opt (parser);
12120 /* Look for the `{' to start the namespace. */
12121 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12122 /* Start the namespace. */
12123 push_namespace (identifier);
12125 /* "inline namespace" is equivalent to a stub namespace definition
12126 followed by a strong using directive. */
12129 tree name_space = current_namespace;
12130 /* Set up namespace association. */
12131 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12132 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12133 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12134 /* Import the contents of the inline namespace. */
12136 do_using_directive (name_space);
12137 push_namespace (identifier);
12140 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12142 /* Parse the body of the namespace. */
12143 cp_parser_namespace_body (parser);
12145 #ifdef HANDLE_PRAGMA_VISIBILITY
12146 if (has_visibility)
12150 /* Finish the namespace. */
12152 /* Look for the final `}'. */
12153 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12156 /* Parse a namespace-body.
12159 declaration-seq [opt] */
12162 cp_parser_namespace_body (cp_parser* parser)
12164 cp_parser_declaration_seq_opt (parser);
12167 /* Parse a namespace-alias-definition.
12169 namespace-alias-definition:
12170 namespace identifier = qualified-namespace-specifier ; */
12173 cp_parser_namespace_alias_definition (cp_parser* parser)
12176 tree namespace_specifier;
12178 cp_token *token = cp_lexer_peek_token (parser->lexer);
12180 /* Look for the `namespace' keyword. */
12181 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12182 /* Look for the identifier. */
12183 identifier = cp_parser_identifier (parser);
12184 if (identifier == error_mark_node)
12186 /* Look for the `=' token. */
12187 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12188 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12190 error ("%H%<namespace%> definition is not allowed here", &token->location);
12191 /* Skip the definition. */
12192 cp_lexer_consume_token (parser->lexer);
12193 if (cp_parser_skip_to_closing_brace (parser))
12194 cp_lexer_consume_token (parser->lexer);
12197 cp_parser_require (parser, CPP_EQ, "%<=%>");
12198 /* Look for the qualified-namespace-specifier. */
12199 namespace_specifier
12200 = cp_parser_qualified_namespace_specifier (parser);
12201 /* Look for the `;' token. */
12202 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12204 /* Register the alias in the symbol table. */
12205 do_namespace_alias (identifier, namespace_specifier);
12208 /* Parse a qualified-namespace-specifier.
12210 qualified-namespace-specifier:
12211 :: [opt] nested-name-specifier [opt] namespace-name
12213 Returns a NAMESPACE_DECL corresponding to the specified
12217 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12219 /* Look for the optional `::'. */
12220 cp_parser_global_scope_opt (parser,
12221 /*current_scope_valid_p=*/false);
12223 /* Look for the optional nested-name-specifier. */
12224 cp_parser_nested_name_specifier_opt (parser,
12225 /*typename_keyword_p=*/false,
12226 /*check_dependency_p=*/true,
12228 /*is_declaration=*/true);
12230 return cp_parser_namespace_name (parser);
12233 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12234 access declaration.
12237 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12238 using :: unqualified-id ;
12240 access-declaration:
12246 cp_parser_using_declaration (cp_parser* parser,
12247 bool access_declaration_p)
12250 bool typename_p = false;
12251 bool global_scope_p;
12256 if (access_declaration_p)
12257 cp_parser_parse_tentatively (parser);
12260 /* Look for the `using' keyword. */
12261 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12263 /* Peek at the next token. */
12264 token = cp_lexer_peek_token (parser->lexer);
12265 /* See if it's `typename'. */
12266 if (token->keyword == RID_TYPENAME)
12268 /* Remember that we've seen it. */
12270 /* Consume the `typename' token. */
12271 cp_lexer_consume_token (parser->lexer);
12275 /* Look for the optional global scope qualification. */
12277 = (cp_parser_global_scope_opt (parser,
12278 /*current_scope_valid_p=*/false)
12281 /* If we saw `typename', or didn't see `::', then there must be a
12282 nested-name-specifier present. */
12283 if (typename_p || !global_scope_p)
12284 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12285 /*check_dependency_p=*/true,
12287 /*is_declaration=*/true);
12288 /* Otherwise, we could be in either of the two productions. In that
12289 case, treat the nested-name-specifier as optional. */
12291 qscope = cp_parser_nested_name_specifier_opt (parser,
12292 /*typename_keyword_p=*/false,
12293 /*check_dependency_p=*/true,
12295 /*is_declaration=*/true);
12297 qscope = global_namespace;
12299 if (access_declaration_p && cp_parser_error_occurred (parser))
12300 /* Something has already gone wrong; there's no need to parse
12301 further. Since an error has occurred, the return value of
12302 cp_parser_parse_definitely will be false, as required. */
12303 return cp_parser_parse_definitely (parser);
12305 token = cp_lexer_peek_token (parser->lexer);
12306 /* Parse the unqualified-id. */
12307 identifier = cp_parser_unqualified_id (parser,
12308 /*template_keyword_p=*/false,
12309 /*check_dependency_p=*/true,
12310 /*declarator_p=*/true,
12311 /*optional_p=*/false);
12313 if (access_declaration_p)
12315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12316 cp_parser_simulate_error (parser);
12317 if (!cp_parser_parse_definitely (parser))
12321 /* The function we call to handle a using-declaration is different
12322 depending on what scope we are in. */
12323 if (qscope == error_mark_node || identifier == error_mark_node)
12325 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12326 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12327 /* [namespace.udecl]
12329 A using declaration shall not name a template-id. */
12330 error ("%Ha template-id may not appear in a using-declaration",
12334 if (at_class_scope_p ())
12336 /* Create the USING_DECL. */
12337 decl = do_class_using_decl (parser->scope, identifier);
12339 if (check_for_bare_parameter_packs (decl))
12342 /* Add it to the list of members in this class. */
12343 finish_member_declaration (decl);
12347 decl = cp_parser_lookup_name_simple (parser,
12350 if (decl == error_mark_node)
12351 cp_parser_name_lookup_error (parser, identifier,
12354 else if (check_for_bare_parameter_packs (decl))
12356 else if (!at_namespace_scope_p ())
12357 do_local_using_decl (decl, qscope, identifier);
12359 do_toplevel_using_decl (decl, qscope, identifier);
12363 /* Look for the final `;'. */
12364 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12369 /* Parse a using-directive.
12372 using namespace :: [opt] nested-name-specifier [opt]
12373 namespace-name ; */
12376 cp_parser_using_directive (cp_parser* parser)
12378 tree namespace_decl;
12381 /* Look for the `using' keyword. */
12382 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12383 /* And the `namespace' keyword. */
12384 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12385 /* Look for the optional `::' operator. */
12386 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12387 /* And the optional nested-name-specifier. */
12388 cp_parser_nested_name_specifier_opt (parser,
12389 /*typename_keyword_p=*/false,
12390 /*check_dependency_p=*/true,
12392 /*is_declaration=*/true);
12393 /* Get the namespace being used. */
12394 namespace_decl = cp_parser_namespace_name (parser);
12395 /* And any specified attributes. */
12396 attribs = cp_parser_attributes_opt (parser);
12397 /* Update the symbol table. */
12398 parse_using_directive (namespace_decl, attribs);
12399 /* Look for the final `;'. */
12400 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12403 /* Parse an asm-definition.
12406 asm ( string-literal ) ;
12411 asm volatile [opt] ( string-literal ) ;
12412 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12413 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12414 : asm-operand-list [opt] ) ;
12415 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12416 : asm-operand-list [opt]
12417 : asm-operand-list [opt] ) ; */
12420 cp_parser_asm_definition (cp_parser* parser)
12423 tree outputs = NULL_TREE;
12424 tree inputs = NULL_TREE;
12425 tree clobbers = NULL_TREE;
12427 bool volatile_p = false;
12428 bool extended_p = false;
12429 bool invalid_inputs_p = false;
12430 bool invalid_outputs_p = false;
12432 /* Look for the `asm' keyword. */
12433 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12434 /* See if the next token is `volatile'. */
12435 if (cp_parser_allow_gnu_extensions_p (parser)
12436 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12438 /* Remember that we saw the `volatile' keyword. */
12440 /* Consume the token. */
12441 cp_lexer_consume_token (parser->lexer);
12443 /* Look for the opening `('. */
12444 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12446 /* Look for the string. */
12447 string = cp_parser_string_literal (parser, false, false);
12448 if (string == error_mark_node)
12450 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12451 /*consume_paren=*/true);
12455 /* If we're allowing GNU extensions, check for the extended assembly
12456 syntax. Unfortunately, the `:' tokens need not be separated by
12457 a space in C, and so, for compatibility, we tolerate that here
12458 too. Doing that means that we have to treat the `::' operator as
12460 if (cp_parser_allow_gnu_extensions_p (parser)
12461 && parser->in_function_body
12462 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12463 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12465 bool inputs_p = false;
12466 bool clobbers_p = false;
12468 /* The extended syntax was used. */
12471 /* Look for outputs. */
12472 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12474 /* Consume the `:'. */
12475 cp_lexer_consume_token (parser->lexer);
12476 /* Parse the output-operands. */
12477 if (cp_lexer_next_token_is_not (parser->lexer,
12479 && cp_lexer_next_token_is_not (parser->lexer,
12481 && cp_lexer_next_token_is_not (parser->lexer,
12483 outputs = cp_parser_asm_operand_list (parser);
12485 if (outputs == error_mark_node)
12486 invalid_outputs_p = true;
12488 /* If the next token is `::', there are no outputs, and the
12489 next token is the beginning of the inputs. */
12490 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12491 /* The inputs are coming next. */
12494 /* Look for inputs. */
12496 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12498 /* Consume the `:' or `::'. */
12499 cp_lexer_consume_token (parser->lexer);
12500 /* Parse the output-operands. */
12501 if (cp_lexer_next_token_is_not (parser->lexer,
12503 && cp_lexer_next_token_is_not (parser->lexer,
12505 inputs = cp_parser_asm_operand_list (parser);
12507 if (inputs == error_mark_node)
12508 invalid_inputs_p = true;
12510 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12511 /* The clobbers are coming next. */
12514 /* Look for clobbers. */
12516 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12518 /* Consume the `:' or `::'. */
12519 cp_lexer_consume_token (parser->lexer);
12520 /* Parse the clobbers. */
12521 if (cp_lexer_next_token_is_not (parser->lexer,
12523 clobbers = cp_parser_asm_clobber_list (parser);
12526 /* Look for the closing `)'. */
12527 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12528 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12529 /*consume_paren=*/true);
12530 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12532 if (!invalid_inputs_p && !invalid_outputs_p)
12534 /* Create the ASM_EXPR. */
12535 if (parser->in_function_body)
12537 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12539 /* If the extended syntax was not used, mark the ASM_EXPR. */
12542 tree temp = asm_stmt;
12543 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12544 temp = TREE_OPERAND (temp, 0);
12546 ASM_INPUT_P (temp) = 1;
12550 cgraph_add_asm_node (string);
12554 /* Declarators [gram.dcl.decl] */
12556 /* Parse an init-declarator.
12559 declarator initializer [opt]
12564 declarator asm-specification [opt] attributes [opt] initializer [opt]
12566 function-definition:
12567 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12569 decl-specifier-seq [opt] declarator function-try-block
12573 function-definition:
12574 __extension__ function-definition
12576 The DECL_SPECIFIERS apply to this declarator. Returns a
12577 representation of the entity declared. If MEMBER_P is TRUE, then
12578 this declarator appears in a class scope. The new DECL created by
12579 this declarator is returned.
12581 The CHECKS are access checks that should be performed once we know
12582 what entity is being declared (and, therefore, what classes have
12585 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12586 for a function-definition here as well. If the declarator is a
12587 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12588 be TRUE upon return. By that point, the function-definition will
12589 have been completely parsed.
12591 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12595 cp_parser_init_declarator (cp_parser* parser,
12596 cp_decl_specifier_seq *decl_specifiers,
12597 VEC (deferred_access_check,gc)* checks,
12598 bool function_definition_allowed_p,
12600 int declares_class_or_enum,
12601 bool* function_definition_p)
12603 cp_token *token = NULL, *asm_spec_start_token = NULL,
12604 *attributes_start_token = NULL;
12605 cp_declarator *declarator;
12606 tree prefix_attributes;
12608 tree asm_specification;
12610 tree decl = NULL_TREE;
12612 int is_initialized;
12613 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12614 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12616 enum cpp_ttype initialization_kind;
12617 bool is_direct_init = false;
12618 bool is_non_constant_init;
12619 int ctor_dtor_or_conv_p;
12621 tree pushed_scope = NULL;
12623 /* Gather the attributes that were provided with the
12624 decl-specifiers. */
12625 prefix_attributes = decl_specifiers->attributes;
12627 /* Assume that this is not the declarator for a function
12629 if (function_definition_p)
12630 *function_definition_p = false;
12632 /* Defer access checks while parsing the declarator; we cannot know
12633 what names are accessible until we know what is being
12635 resume_deferring_access_checks ();
12637 /* Parse the declarator. */
12638 token = cp_lexer_peek_token (parser->lexer);
12640 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12641 &ctor_dtor_or_conv_p,
12642 /*parenthesized_p=*/NULL,
12643 /*member_p=*/false);
12644 /* Gather up the deferred checks. */
12645 stop_deferring_access_checks ();
12647 /* If the DECLARATOR was erroneous, there's no need to go
12649 if (declarator == cp_error_declarator)
12650 return error_mark_node;
12652 /* Check that the number of template-parameter-lists is OK. */
12653 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12655 return error_mark_node;
12657 if (declares_class_or_enum & 2)
12658 cp_parser_check_for_definition_in_return_type (declarator,
12659 decl_specifiers->type,
12660 decl_specifiers->type_location);
12662 /* Figure out what scope the entity declared by the DECLARATOR is
12663 located in. `grokdeclarator' sometimes changes the scope, so
12664 we compute it now. */
12665 scope = get_scope_of_declarator (declarator);
12667 /* If we're allowing GNU extensions, look for an asm-specification
12669 if (cp_parser_allow_gnu_extensions_p (parser))
12671 /* Look for an asm-specification. */
12672 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12673 asm_specification = cp_parser_asm_specification_opt (parser);
12674 /* And attributes. */
12675 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12676 attributes = cp_parser_attributes_opt (parser);
12680 asm_specification = NULL_TREE;
12681 attributes = NULL_TREE;
12684 /* Peek at the next token. */
12685 token = cp_lexer_peek_token (parser->lexer);
12686 /* Check to see if the token indicates the start of a
12687 function-definition. */
12688 if (function_declarator_p (declarator)
12689 && cp_parser_token_starts_function_definition_p (token))
12691 if (!function_definition_allowed_p)
12693 /* If a function-definition should not appear here, issue an
12695 cp_parser_error (parser,
12696 "a function-definition is not allowed here");
12697 return error_mark_node;
12701 location_t func_brace_location
12702 = cp_lexer_peek_token (parser->lexer)->location;
12704 /* Neither attributes nor an asm-specification are allowed
12705 on a function-definition. */
12706 if (asm_specification)
12707 error ("%Han asm-specification is not allowed "
12708 "on a function-definition",
12709 &asm_spec_start_token->location);
12711 error ("%Hattributes are not allowed on a function-definition",
12712 &attributes_start_token->location);
12713 /* This is a function-definition. */
12714 *function_definition_p = true;
12716 /* Parse the function definition. */
12718 decl = cp_parser_save_member_function_body (parser,
12721 prefix_attributes);
12724 = (cp_parser_function_definition_from_specifiers_and_declarator
12725 (parser, decl_specifiers, prefix_attributes, declarator));
12727 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12729 /* This is where the prologue starts... */
12730 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12731 = func_brace_location;
12740 Only in function declarations for constructors, destructors, and
12741 type conversions can the decl-specifier-seq be omitted.
12743 We explicitly postpone this check past the point where we handle
12744 function-definitions because we tolerate function-definitions
12745 that are missing their return types in some modes. */
12746 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12748 cp_parser_error (parser,
12749 "expected constructor, destructor, or type conversion");
12750 return error_mark_node;
12753 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12754 if (token->type == CPP_EQ
12755 || token->type == CPP_OPEN_PAREN
12756 || token->type == CPP_OPEN_BRACE)
12758 is_initialized = SD_INITIALIZED;
12759 initialization_kind = token->type;
12761 if (token->type == CPP_EQ
12762 && function_declarator_p (declarator))
12764 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12765 if (t2->keyword == RID_DEFAULT)
12766 is_initialized = SD_DEFAULTED;
12767 else if (t2->keyword == RID_DELETE)
12768 is_initialized = SD_DELETED;
12773 /* If the init-declarator isn't initialized and isn't followed by a
12774 `,' or `;', it's not a valid init-declarator. */
12775 if (token->type != CPP_COMMA
12776 && token->type != CPP_SEMICOLON)
12778 cp_parser_error (parser, "expected initializer");
12779 return error_mark_node;
12781 is_initialized = SD_UNINITIALIZED;
12782 initialization_kind = CPP_EOF;
12785 /* Because start_decl has side-effects, we should only call it if we
12786 know we're going ahead. By this point, we know that we cannot
12787 possibly be looking at any other construct. */
12788 cp_parser_commit_to_tentative_parse (parser);
12790 /* If the decl specifiers were bad, issue an error now that we're
12791 sure this was intended to be a declarator. Then continue
12792 declaring the variable(s), as int, to try to cut down on further
12794 if (decl_specifiers->any_specifiers_p
12795 && decl_specifiers->type == error_mark_node)
12797 cp_parser_error (parser, "invalid type in declaration");
12798 decl_specifiers->type = integer_type_node;
12801 /* Check to see whether or not this declaration is a friend. */
12802 friend_p = cp_parser_friend_p (decl_specifiers);
12804 /* Enter the newly declared entry in the symbol table. If we're
12805 processing a declaration in a class-specifier, we wait until
12806 after processing the initializer. */
12809 if (parser->in_unbraced_linkage_specification_p)
12810 decl_specifiers->storage_class = sc_extern;
12811 decl = start_decl (declarator, decl_specifiers,
12812 is_initialized, attributes, prefix_attributes,
12816 /* Enter the SCOPE. That way unqualified names appearing in the
12817 initializer will be looked up in SCOPE. */
12818 pushed_scope = push_scope (scope);
12820 /* Perform deferred access control checks, now that we know in which
12821 SCOPE the declared entity resides. */
12822 if (!member_p && decl)
12824 tree saved_current_function_decl = NULL_TREE;
12826 /* If the entity being declared is a function, pretend that we
12827 are in its scope. If it is a `friend', it may have access to
12828 things that would not otherwise be accessible. */
12829 if (TREE_CODE (decl) == FUNCTION_DECL)
12831 saved_current_function_decl = current_function_decl;
12832 current_function_decl = decl;
12835 /* Perform access checks for template parameters. */
12836 cp_parser_perform_template_parameter_access_checks (checks);
12838 /* Perform the access control checks for the declarator and the
12839 decl-specifiers. */
12840 perform_deferred_access_checks ();
12842 /* Restore the saved value. */
12843 if (TREE_CODE (decl) == FUNCTION_DECL)
12844 current_function_decl = saved_current_function_decl;
12847 /* Parse the initializer. */
12848 initializer = NULL_TREE;
12849 is_direct_init = false;
12850 is_non_constant_init = true;
12851 if (is_initialized)
12853 if (function_declarator_p (declarator))
12855 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12856 if (initialization_kind == CPP_EQ)
12857 initializer = cp_parser_pure_specifier (parser);
12860 /* If the declaration was erroneous, we don't really
12861 know what the user intended, so just silently
12862 consume the initializer. */
12863 if (decl != error_mark_node)
12864 error ("%Hinitializer provided for function",
12865 &initializer_start_token->location);
12866 cp_parser_skip_to_closing_parenthesis (parser,
12867 /*recovering=*/true,
12868 /*or_comma=*/false,
12869 /*consume_paren=*/true);
12873 initializer = cp_parser_initializer (parser,
12875 &is_non_constant_init);
12878 /* The old parser allows attributes to appear after a parenthesized
12879 initializer. Mark Mitchell proposed removing this functionality
12880 on the GCC mailing lists on 2002-08-13. This parser accepts the
12881 attributes -- but ignores them. */
12882 if (cp_parser_allow_gnu_extensions_p (parser)
12883 && initialization_kind == CPP_OPEN_PAREN)
12884 if (cp_parser_attributes_opt (parser))
12885 warning (OPT_Wattributes,
12886 "attributes after parenthesized initializer ignored");
12888 /* For an in-class declaration, use `grokfield' to create the
12894 pop_scope (pushed_scope);
12895 pushed_scope = false;
12897 decl = grokfield (declarator, decl_specifiers,
12898 initializer, !is_non_constant_init,
12899 /*asmspec=*/NULL_TREE,
12900 prefix_attributes);
12901 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12902 cp_parser_save_default_args (parser, decl);
12905 /* Finish processing the declaration. But, skip friend
12907 if (!friend_p && decl && decl != error_mark_node)
12909 cp_finish_decl (decl,
12910 initializer, !is_non_constant_init,
12912 /* If the initializer is in parentheses, then this is
12913 a direct-initialization, which means that an
12914 `explicit' constructor is OK. Otherwise, an
12915 `explicit' constructor cannot be used. */
12916 ((is_direct_init || !is_initialized)
12917 ? 0 : LOOKUP_ONLYCONVERTING));
12919 else if ((cxx_dialect != cxx98) && friend_p
12920 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12921 /* Core issue #226 (C++0x only): A default template-argument
12922 shall not be specified in a friend class template
12924 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12925 /*is_partial=*/0, /*is_friend_decl=*/1);
12927 if (!friend_p && pushed_scope)
12928 pop_scope (pushed_scope);
12933 /* Parse a declarator.
12937 ptr-operator declarator
12939 abstract-declarator:
12940 ptr-operator abstract-declarator [opt]
12941 direct-abstract-declarator
12946 attributes [opt] direct-declarator
12947 attributes [opt] ptr-operator declarator
12949 abstract-declarator:
12950 attributes [opt] ptr-operator abstract-declarator [opt]
12951 attributes [opt] direct-abstract-declarator
12953 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12954 detect constructor, destructor or conversion operators. It is set
12955 to -1 if the declarator is a name, and +1 if it is a
12956 function. Otherwise it is set to zero. Usually you just want to
12957 test for >0, but internally the negative value is used.
12959 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12960 a decl-specifier-seq unless it declares a constructor, destructor,
12961 or conversion. It might seem that we could check this condition in
12962 semantic analysis, rather than parsing, but that makes it difficult
12963 to handle something like `f()'. We want to notice that there are
12964 no decl-specifiers, and therefore realize that this is an
12965 expression, not a declaration.)
12967 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12968 the declarator is a direct-declarator of the form "(...)".
12970 MEMBER_P is true iff this declarator is a member-declarator. */
12972 static cp_declarator *
12973 cp_parser_declarator (cp_parser* parser,
12974 cp_parser_declarator_kind dcl_kind,
12975 int* ctor_dtor_or_conv_p,
12976 bool* parenthesized_p,
12980 cp_declarator *declarator;
12981 enum tree_code code;
12982 cp_cv_quals cv_quals;
12984 tree attributes = NULL_TREE;
12986 /* Assume this is not a constructor, destructor, or type-conversion
12988 if (ctor_dtor_or_conv_p)
12989 *ctor_dtor_or_conv_p = 0;
12991 if (cp_parser_allow_gnu_extensions_p (parser))
12992 attributes = cp_parser_attributes_opt (parser);
12994 /* Peek at the next token. */
12995 token = cp_lexer_peek_token (parser->lexer);
12997 /* Check for the ptr-operator production. */
12998 cp_parser_parse_tentatively (parser);
12999 /* Parse the ptr-operator. */
13000 code = cp_parser_ptr_operator (parser,
13003 /* If that worked, then we have a ptr-operator. */
13004 if (cp_parser_parse_definitely (parser))
13006 /* If a ptr-operator was found, then this declarator was not
13008 if (parenthesized_p)
13009 *parenthesized_p = true;
13010 /* The dependent declarator is optional if we are parsing an
13011 abstract-declarator. */
13012 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13013 cp_parser_parse_tentatively (parser);
13015 /* Parse the dependent declarator. */
13016 declarator = cp_parser_declarator (parser, dcl_kind,
13017 /*ctor_dtor_or_conv_p=*/NULL,
13018 /*parenthesized_p=*/NULL,
13019 /*member_p=*/false);
13021 /* If we are parsing an abstract-declarator, we must handle the
13022 case where the dependent declarator is absent. */
13023 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13024 && !cp_parser_parse_definitely (parser))
13027 declarator = cp_parser_make_indirect_declarator
13028 (code, class_type, cv_quals, declarator);
13030 /* Everything else is a direct-declarator. */
13033 if (parenthesized_p)
13034 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13036 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13037 ctor_dtor_or_conv_p,
13041 if (attributes && declarator && declarator != cp_error_declarator)
13042 declarator->attributes = attributes;
13047 /* Parse a direct-declarator or direct-abstract-declarator.
13051 direct-declarator ( parameter-declaration-clause )
13052 cv-qualifier-seq [opt]
13053 exception-specification [opt]
13054 direct-declarator [ constant-expression [opt] ]
13057 direct-abstract-declarator:
13058 direct-abstract-declarator [opt]
13059 ( parameter-declaration-clause )
13060 cv-qualifier-seq [opt]
13061 exception-specification [opt]
13062 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13063 ( abstract-declarator )
13065 Returns a representation of the declarator. DCL_KIND is
13066 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13067 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13068 we are parsing a direct-declarator. It is
13069 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13070 of ambiguity we prefer an abstract declarator, as per
13071 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13072 cp_parser_declarator. */
13074 static cp_declarator *
13075 cp_parser_direct_declarator (cp_parser* parser,
13076 cp_parser_declarator_kind dcl_kind,
13077 int* ctor_dtor_or_conv_p,
13081 cp_declarator *declarator = NULL;
13082 tree scope = NULL_TREE;
13083 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13084 bool saved_in_declarator_p = parser->in_declarator_p;
13086 tree pushed_scope = NULL_TREE;
13090 /* Peek at the next token. */
13091 token = cp_lexer_peek_token (parser->lexer);
13092 if (token->type == CPP_OPEN_PAREN)
13094 /* This is either a parameter-declaration-clause, or a
13095 parenthesized declarator. When we know we are parsing a
13096 named declarator, it must be a parenthesized declarator
13097 if FIRST is true. For instance, `(int)' is a
13098 parameter-declaration-clause, with an omitted
13099 direct-abstract-declarator. But `((*))', is a
13100 parenthesized abstract declarator. Finally, when T is a
13101 template parameter `(T)' is a
13102 parameter-declaration-clause, and not a parenthesized
13105 We first try and parse a parameter-declaration-clause,
13106 and then try a nested declarator (if FIRST is true).
13108 It is not an error for it not to be a
13109 parameter-declaration-clause, even when FIRST is
13115 The first is the declaration of a function while the
13116 second is the definition of a variable, including its
13119 Having seen only the parenthesis, we cannot know which of
13120 these two alternatives should be selected. Even more
13121 complex are examples like:
13126 The former is a function-declaration; the latter is a
13127 variable initialization.
13129 Thus again, we try a parameter-declaration-clause, and if
13130 that fails, we back out and return. */
13132 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13135 unsigned saved_num_template_parameter_lists;
13136 bool is_declarator = false;
13139 /* In a member-declarator, the only valid interpretation
13140 of a parenthesis is the start of a
13141 parameter-declaration-clause. (It is invalid to
13142 initialize a static data member with a parenthesized
13143 initializer; only the "=" form of initialization is
13146 cp_parser_parse_tentatively (parser);
13148 /* Consume the `('. */
13149 cp_lexer_consume_token (parser->lexer);
13152 /* If this is going to be an abstract declarator, we're
13153 in a declarator and we can't have default args. */
13154 parser->default_arg_ok_p = false;
13155 parser->in_declarator_p = true;
13158 /* Inside the function parameter list, surrounding
13159 template-parameter-lists do not apply. */
13160 saved_num_template_parameter_lists
13161 = parser->num_template_parameter_lists;
13162 parser->num_template_parameter_lists = 0;
13164 begin_scope (sk_function_parms, NULL_TREE);
13166 /* Parse the parameter-declaration-clause. */
13167 params = cp_parser_parameter_declaration_clause (parser);
13169 parser->num_template_parameter_lists
13170 = saved_num_template_parameter_lists;
13172 /* If all went well, parse the cv-qualifier-seq and the
13173 exception-specification. */
13174 if (member_p || cp_parser_parse_definitely (parser))
13176 cp_cv_quals cv_quals;
13177 tree exception_specification;
13180 is_declarator = true;
13182 if (ctor_dtor_or_conv_p)
13183 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13185 /* Consume the `)'. */
13186 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13188 /* Parse the cv-qualifier-seq. */
13189 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13190 /* And the exception-specification. */
13191 exception_specification
13192 = cp_parser_exception_specification_opt (parser);
13195 = cp_parser_late_return_type_opt (parser);
13197 /* Create the function-declarator. */
13198 declarator = make_call_declarator (declarator,
13201 exception_specification,
13203 /* Any subsequent parameter lists are to do with
13204 return type, so are not those of the declared
13206 parser->default_arg_ok_p = false;
13209 /* Remove the function parms from scope. */
13210 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13211 pop_binding (DECL_NAME (t), t);
13215 /* Repeat the main loop. */
13219 /* If this is the first, we can try a parenthesized
13223 bool saved_in_type_id_in_expr_p;
13225 parser->default_arg_ok_p = saved_default_arg_ok_p;
13226 parser->in_declarator_p = saved_in_declarator_p;
13228 /* Consume the `('. */
13229 cp_lexer_consume_token (parser->lexer);
13230 /* Parse the nested declarator. */
13231 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13232 parser->in_type_id_in_expr_p = true;
13234 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13235 /*parenthesized_p=*/NULL,
13237 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13239 /* Expect a `)'. */
13240 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13241 declarator = cp_error_declarator;
13242 if (declarator == cp_error_declarator)
13245 goto handle_declarator;
13247 /* Otherwise, we must be done. */
13251 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13252 && token->type == CPP_OPEN_SQUARE)
13254 /* Parse an array-declarator. */
13257 if (ctor_dtor_or_conv_p)
13258 *ctor_dtor_or_conv_p = 0;
13261 parser->default_arg_ok_p = false;
13262 parser->in_declarator_p = true;
13263 /* Consume the `['. */
13264 cp_lexer_consume_token (parser->lexer);
13265 /* Peek at the next token. */
13266 token = cp_lexer_peek_token (parser->lexer);
13267 /* If the next token is `]', then there is no
13268 constant-expression. */
13269 if (token->type != CPP_CLOSE_SQUARE)
13271 bool non_constant_p;
13274 = cp_parser_constant_expression (parser,
13275 /*allow_non_constant=*/true,
13277 if (!non_constant_p)
13278 bounds = fold_non_dependent_expr (bounds);
13279 else if (processing_template_decl)
13281 /* Remember this wasn't a constant-expression. */
13282 bounds = build_nop (TREE_TYPE (bounds), bounds);
13283 TREE_SIDE_EFFECTS (bounds) = 1;
13286 /* Normally, the array bound must be an integral constant
13287 expression. However, as an extension, we allow VLAs
13288 in function scopes. */
13289 else if (!parser->in_function_body)
13291 error ("%Harray bound is not an integer constant",
13293 bounds = error_mark_node;
13297 bounds = NULL_TREE;
13298 /* Look for the closing `]'. */
13299 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13301 declarator = cp_error_declarator;
13305 declarator = make_array_declarator (declarator, bounds);
13307 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13309 tree qualifying_scope;
13310 tree unqualified_name;
13311 special_function_kind sfk;
13313 bool pack_expansion_p = false;
13314 cp_token *declarator_id_start_token;
13316 /* Parse a declarator-id */
13317 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13320 cp_parser_parse_tentatively (parser);
13322 /* If we see an ellipsis, we should be looking at a
13324 if (token->type == CPP_ELLIPSIS)
13326 /* Consume the `...' */
13327 cp_lexer_consume_token (parser->lexer);
13329 pack_expansion_p = true;
13333 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13335 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13336 qualifying_scope = parser->scope;
13341 if (!unqualified_name && pack_expansion_p)
13343 /* Check whether an error occurred. */
13344 okay = !cp_parser_error_occurred (parser);
13346 /* We already consumed the ellipsis to mark a
13347 parameter pack, but we have no way to report it,
13348 so abort the tentative parse. We will be exiting
13349 immediately anyway. */
13350 cp_parser_abort_tentative_parse (parser);
13353 okay = cp_parser_parse_definitely (parser);
13356 unqualified_name = error_mark_node;
13357 else if (unqualified_name
13358 && (qualifying_scope
13359 || (TREE_CODE (unqualified_name)
13360 != IDENTIFIER_NODE)))
13362 cp_parser_error (parser, "expected unqualified-id");
13363 unqualified_name = error_mark_node;
13367 if (!unqualified_name)
13369 if (unqualified_name == error_mark_node)
13371 declarator = cp_error_declarator;
13372 pack_expansion_p = false;
13373 declarator->parameter_pack_p = false;
13377 if (qualifying_scope && at_namespace_scope_p ()
13378 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13380 /* In the declaration of a member of a template class
13381 outside of the class itself, the SCOPE will sometimes
13382 be a TYPENAME_TYPE. For example, given:
13384 template <typename T>
13385 int S<T>::R::i = 3;
13387 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13388 this context, we must resolve S<T>::R to an ordinary
13389 type, rather than a typename type.
13391 The reason we normally avoid resolving TYPENAME_TYPEs
13392 is that a specialization of `S' might render
13393 `S<T>::R' not a type. However, if `S' is
13394 specialized, then this `i' will not be used, so there
13395 is no harm in resolving the types here. */
13398 /* Resolve the TYPENAME_TYPE. */
13399 type = resolve_typename_type (qualifying_scope,
13400 /*only_current_p=*/false);
13401 /* If that failed, the declarator is invalid. */
13402 if (TREE_CODE (type) == TYPENAME_TYPE)
13403 error ("%H%<%T::%E%> is not a type",
13404 &declarator_id_start_token->location,
13405 TYPE_CONTEXT (qualifying_scope),
13406 TYPE_IDENTIFIER (qualifying_scope));
13407 qualifying_scope = type;
13412 if (unqualified_name)
13416 if (qualifying_scope
13417 && CLASS_TYPE_P (qualifying_scope))
13418 class_type = qualifying_scope;
13420 class_type = current_class_type;
13422 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13424 tree name_type = TREE_TYPE (unqualified_name);
13425 if (class_type && same_type_p (name_type, class_type))
13427 if (qualifying_scope
13428 && CLASSTYPE_USE_TEMPLATE (name_type))
13430 error ("%Hinvalid use of constructor as a template",
13431 &declarator_id_start_token->location);
13432 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13433 "name the constructor in a qualified name",
13435 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13436 class_type, name_type);
13437 declarator = cp_error_declarator;
13441 unqualified_name = constructor_name (class_type);
13445 /* We do not attempt to print the declarator
13446 here because we do not have enough
13447 information about its original syntactic
13449 cp_parser_error (parser, "invalid declarator");
13450 declarator = cp_error_declarator;
13457 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13458 sfk = sfk_destructor;
13459 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13460 sfk = sfk_conversion;
13461 else if (/* There's no way to declare a constructor
13462 for an anonymous type, even if the type
13463 got a name for linkage purposes. */
13464 !TYPE_WAS_ANONYMOUS (class_type)
13465 && constructor_name_p (unqualified_name,
13468 unqualified_name = constructor_name (class_type);
13469 sfk = sfk_constructor;
13472 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13473 *ctor_dtor_or_conv_p = -1;
13476 declarator = make_id_declarator (qualifying_scope,
13479 declarator->id_loc = token->location;
13480 declarator->parameter_pack_p = pack_expansion_p;
13482 if (pack_expansion_p)
13483 maybe_warn_variadic_templates ();
13485 handle_declarator:;
13486 scope = get_scope_of_declarator (declarator);
13488 /* Any names that appear after the declarator-id for a
13489 member are looked up in the containing scope. */
13490 pushed_scope = push_scope (scope);
13491 parser->in_declarator_p = true;
13492 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13493 || (declarator && declarator->kind == cdk_id))
13494 /* Default args are only allowed on function
13496 parser->default_arg_ok_p = saved_default_arg_ok_p;
13498 parser->default_arg_ok_p = false;
13507 /* For an abstract declarator, we might wind up with nothing at this
13508 point. That's an error; the declarator is not optional. */
13510 cp_parser_error (parser, "expected declarator");
13512 /* If we entered a scope, we must exit it now. */
13514 pop_scope (pushed_scope);
13516 parser->default_arg_ok_p = saved_default_arg_ok_p;
13517 parser->in_declarator_p = saved_in_declarator_p;
13522 /* Parse a ptr-operator.
13525 * cv-qualifier-seq [opt]
13527 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13532 & cv-qualifier-seq [opt]
13534 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13535 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13536 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13537 filled in with the TYPE containing the member. *CV_QUALS is
13538 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13539 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13540 Note that the tree codes returned by this function have nothing
13541 to do with the types of trees that will be eventually be created
13542 to represent the pointer or reference type being parsed. They are
13543 just constants with suggestive names. */
13544 static enum tree_code
13545 cp_parser_ptr_operator (cp_parser* parser,
13547 cp_cv_quals *cv_quals)
13549 enum tree_code code = ERROR_MARK;
13552 /* Assume that it's not a pointer-to-member. */
13554 /* And that there are no cv-qualifiers. */
13555 *cv_quals = TYPE_UNQUALIFIED;
13557 /* Peek at the next token. */
13558 token = cp_lexer_peek_token (parser->lexer);
13560 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13561 if (token->type == CPP_MULT)
13562 code = INDIRECT_REF;
13563 else if (token->type == CPP_AND)
13565 else if ((cxx_dialect != cxx98) &&
13566 token->type == CPP_AND_AND) /* C++0x only */
13567 code = NON_LVALUE_EXPR;
13569 if (code != ERROR_MARK)
13571 /* Consume the `*', `&' or `&&'. */
13572 cp_lexer_consume_token (parser->lexer);
13574 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13575 `&', if we are allowing GNU extensions. (The only qualifier
13576 that can legally appear after `&' is `restrict', but that is
13577 enforced during semantic analysis. */
13578 if (code == INDIRECT_REF
13579 || cp_parser_allow_gnu_extensions_p (parser))
13580 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13584 /* Try the pointer-to-member case. */
13585 cp_parser_parse_tentatively (parser);
13586 /* Look for the optional `::' operator. */
13587 cp_parser_global_scope_opt (parser,
13588 /*current_scope_valid_p=*/false);
13589 /* Look for the nested-name specifier. */
13590 token = cp_lexer_peek_token (parser->lexer);
13591 cp_parser_nested_name_specifier (parser,
13592 /*typename_keyword_p=*/false,
13593 /*check_dependency_p=*/true,
13595 /*is_declaration=*/false);
13596 /* If we found it, and the next token is a `*', then we are
13597 indeed looking at a pointer-to-member operator. */
13598 if (!cp_parser_error_occurred (parser)
13599 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13601 /* Indicate that the `*' operator was used. */
13602 code = INDIRECT_REF;
13604 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13605 error ("%H%qD is a namespace", &token->location, parser->scope);
13608 /* The type of which the member is a member is given by the
13610 *type = parser->scope;
13611 /* The next name will not be qualified. */
13612 parser->scope = NULL_TREE;
13613 parser->qualifying_scope = NULL_TREE;
13614 parser->object_scope = NULL_TREE;
13615 /* Look for the optional cv-qualifier-seq. */
13616 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13619 /* If that didn't work we don't have a ptr-operator. */
13620 if (!cp_parser_parse_definitely (parser))
13621 cp_parser_error (parser, "expected ptr-operator");
13627 /* Parse an (optional) cv-qualifier-seq.
13630 cv-qualifier cv-qualifier-seq [opt]
13641 Returns a bitmask representing the cv-qualifiers. */
13644 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13646 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13651 cp_cv_quals cv_qualifier;
13653 /* Peek at the next token. */
13654 token = cp_lexer_peek_token (parser->lexer);
13655 /* See if it's a cv-qualifier. */
13656 switch (token->keyword)
13659 cv_qualifier = TYPE_QUAL_CONST;
13663 cv_qualifier = TYPE_QUAL_VOLATILE;
13667 cv_qualifier = TYPE_QUAL_RESTRICT;
13671 cv_qualifier = TYPE_UNQUALIFIED;
13678 if (cv_quals & cv_qualifier)
13680 error ("%Hduplicate cv-qualifier", &token->location);
13681 cp_lexer_purge_token (parser->lexer);
13685 cp_lexer_consume_token (parser->lexer);
13686 cv_quals |= cv_qualifier;
13693 /* Parse a late-specified return type, if any. This is not a separate
13694 non-terminal, but part of a function declarator, which looks like
13698 Returns the type indicated by the type-id. */
13701 cp_parser_late_return_type_opt (cp_parser* parser)
13705 /* Peek at the next token. */
13706 token = cp_lexer_peek_token (parser->lexer);
13707 /* A late-specified return type is indicated by an initial '->'. */
13708 if (token->type != CPP_DEREF)
13711 /* Consume the ->. */
13712 cp_lexer_consume_token (parser->lexer);
13714 return cp_parser_type_id (parser);
13717 /* Parse a declarator-id.
13721 :: [opt] nested-name-specifier [opt] type-name
13723 In the `id-expression' case, the value returned is as for
13724 cp_parser_id_expression if the id-expression was an unqualified-id.
13725 If the id-expression was a qualified-id, then a SCOPE_REF is
13726 returned. The first operand is the scope (either a NAMESPACE_DECL
13727 or TREE_TYPE), but the second is still just a representation of an
13731 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13734 /* The expression must be an id-expression. Assume that qualified
13735 names are the names of types so that:
13738 int S<T>::R::i = 3;
13740 will work; we must treat `S<T>::R' as the name of a type.
13741 Similarly, assume that qualified names are templates, where
13745 int S<T>::R<T>::i = 3;
13748 id = cp_parser_id_expression (parser,
13749 /*template_keyword_p=*/false,
13750 /*check_dependency_p=*/false,
13751 /*template_p=*/NULL,
13752 /*declarator_p=*/true,
13754 if (id && BASELINK_P (id))
13755 id = BASELINK_FUNCTIONS (id);
13759 /* Parse a type-id.
13762 type-specifier-seq abstract-declarator [opt]
13764 Returns the TYPE specified. */
13767 cp_parser_type_id (cp_parser* parser)
13769 cp_decl_specifier_seq type_specifier_seq;
13770 cp_declarator *abstract_declarator;
13772 /* Parse the type-specifier-seq. */
13773 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13774 &type_specifier_seq);
13775 if (type_specifier_seq.type == error_mark_node)
13776 return error_mark_node;
13778 /* There might or might not be an abstract declarator. */
13779 cp_parser_parse_tentatively (parser);
13780 /* Look for the declarator. */
13781 abstract_declarator
13782 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13783 /*parenthesized_p=*/NULL,
13784 /*member_p=*/false);
13785 /* Check to see if there really was a declarator. */
13786 if (!cp_parser_parse_definitely (parser))
13787 abstract_declarator = NULL;
13789 if (type_specifier_seq.type
13790 && type_uses_auto (type_specifier_seq.type))
13792 error ("invalid use of %<auto%>");
13793 return error_mark_node;
13796 return groktypename (&type_specifier_seq, abstract_declarator);
13799 /* Parse a type-specifier-seq.
13801 type-specifier-seq:
13802 type-specifier type-specifier-seq [opt]
13806 type-specifier-seq:
13807 attributes type-specifier-seq [opt]
13809 If IS_CONDITION is true, we are at the start of a "condition",
13810 e.g., we've just seen "if (".
13812 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13815 cp_parser_type_specifier_seq (cp_parser* parser,
13817 cp_decl_specifier_seq *type_specifier_seq)
13819 bool seen_type_specifier = false;
13820 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13821 cp_token *start_token = NULL;
13823 /* Clear the TYPE_SPECIFIER_SEQ. */
13824 clear_decl_specs (type_specifier_seq);
13826 /* Parse the type-specifiers and attributes. */
13829 tree type_specifier;
13830 bool is_cv_qualifier;
13832 /* Check for attributes first. */
13833 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13835 type_specifier_seq->attributes =
13836 chainon (type_specifier_seq->attributes,
13837 cp_parser_attributes_opt (parser));
13841 /* record the token of the beginning of the type specifier seq,
13842 for error reporting purposes*/
13844 start_token = cp_lexer_peek_token (parser->lexer);
13846 /* Look for the type-specifier. */
13847 type_specifier = cp_parser_type_specifier (parser,
13849 type_specifier_seq,
13850 /*is_declaration=*/false,
13853 if (!type_specifier)
13855 /* If the first type-specifier could not be found, this is not a
13856 type-specifier-seq at all. */
13857 if (!seen_type_specifier)
13859 cp_parser_error (parser, "expected type-specifier");
13860 type_specifier_seq->type = error_mark_node;
13863 /* If subsequent type-specifiers could not be found, the
13864 type-specifier-seq is complete. */
13868 seen_type_specifier = true;
13869 /* The standard says that a condition can be:
13871 type-specifier-seq declarator = assignment-expression
13878 we should treat the "S" as a declarator, not as a
13879 type-specifier. The standard doesn't say that explicitly for
13880 type-specifier-seq, but it does say that for
13881 decl-specifier-seq in an ordinary declaration. Perhaps it
13882 would be clearer just to allow a decl-specifier-seq here, and
13883 then add a semantic restriction that if any decl-specifiers
13884 that are not type-specifiers appear, the program is invalid. */
13885 if (is_condition && !is_cv_qualifier)
13886 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13889 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13892 /* Parse a parameter-declaration-clause.
13894 parameter-declaration-clause:
13895 parameter-declaration-list [opt] ... [opt]
13896 parameter-declaration-list , ...
13898 Returns a representation for the parameter declarations. A return
13899 value of NULL indicates a parameter-declaration-clause consisting
13900 only of an ellipsis. */
13903 cp_parser_parameter_declaration_clause (cp_parser* parser)
13910 /* Peek at the next token. */
13911 token = cp_lexer_peek_token (parser->lexer);
13912 /* Check for trivial parameter-declaration-clauses. */
13913 if (token->type == CPP_ELLIPSIS)
13915 /* Consume the `...' token. */
13916 cp_lexer_consume_token (parser->lexer);
13919 else if (token->type == CPP_CLOSE_PAREN)
13920 /* There are no parameters. */
13922 #ifndef NO_IMPLICIT_EXTERN_C
13923 if (in_system_header && current_class_type == NULL
13924 && current_lang_name == lang_name_c)
13928 return void_list_node;
13930 /* Check for `(void)', too, which is a special case. */
13931 else if (token->keyword == RID_VOID
13932 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13933 == CPP_CLOSE_PAREN))
13935 /* Consume the `void' token. */
13936 cp_lexer_consume_token (parser->lexer);
13937 /* There are no parameters. */
13938 return void_list_node;
13941 /* Parse the parameter-declaration-list. */
13942 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13943 /* If a parse error occurred while parsing the
13944 parameter-declaration-list, then the entire
13945 parameter-declaration-clause is erroneous. */
13949 /* Peek at the next token. */
13950 token = cp_lexer_peek_token (parser->lexer);
13951 /* If it's a `,', the clause should terminate with an ellipsis. */
13952 if (token->type == CPP_COMMA)
13954 /* Consume the `,'. */
13955 cp_lexer_consume_token (parser->lexer);
13956 /* Expect an ellipsis. */
13958 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13960 /* It might also be `...' if the optional trailing `,' was
13962 else if (token->type == CPP_ELLIPSIS)
13964 /* Consume the `...' token. */
13965 cp_lexer_consume_token (parser->lexer);
13966 /* And remember that we saw it. */
13970 ellipsis_p = false;
13972 /* Finish the parameter list. */
13974 parameters = chainon (parameters, void_list_node);
13979 /* Parse a parameter-declaration-list.
13981 parameter-declaration-list:
13982 parameter-declaration
13983 parameter-declaration-list , parameter-declaration
13985 Returns a representation of the parameter-declaration-list, as for
13986 cp_parser_parameter_declaration_clause. However, the
13987 `void_list_node' is never appended to the list. Upon return,
13988 *IS_ERROR will be true iff an error occurred. */
13991 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13993 tree parameters = NULL_TREE;
13994 tree *tail = ¶meters;
13995 bool saved_in_unbraced_linkage_specification_p;
13997 /* Assume all will go well. */
13999 /* The special considerations that apply to a function within an
14000 unbraced linkage specifications do not apply to the parameters
14001 to the function. */
14002 saved_in_unbraced_linkage_specification_p
14003 = parser->in_unbraced_linkage_specification_p;
14004 parser->in_unbraced_linkage_specification_p = false;
14006 /* Look for more parameters. */
14009 cp_parameter_declarator *parameter;
14010 tree decl = error_mark_node;
14011 bool parenthesized_p;
14012 /* Parse the parameter. */
14014 = cp_parser_parameter_declaration (parser,
14015 /*template_parm_p=*/false,
14018 /* We don't know yet if the enclosing context is deprecated, so wait
14019 and warn in grokparms if appropriate. */
14020 deprecated_state = DEPRECATED_SUPPRESS;
14023 decl = grokdeclarator (parameter->declarator,
14024 ¶meter->decl_specifiers,
14026 parameter->default_argument != NULL_TREE,
14027 ¶meter->decl_specifiers.attributes);
14029 deprecated_state = DEPRECATED_NORMAL;
14031 /* If a parse error occurred parsing the parameter declaration,
14032 then the entire parameter-declaration-list is erroneous. */
14033 if (decl == error_mark_node)
14036 parameters = error_mark_node;
14040 if (parameter->decl_specifiers.attributes)
14041 cplus_decl_attributes (&decl,
14042 parameter->decl_specifiers.attributes,
14044 if (DECL_NAME (decl))
14045 decl = pushdecl (decl);
14047 /* Add the new parameter to the list. */
14048 *tail = build_tree_list (parameter->default_argument, decl);
14049 tail = &TREE_CHAIN (*tail);
14051 /* Peek at the next token. */
14052 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14053 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14054 /* These are for Objective-C++ */
14055 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14056 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14057 /* The parameter-declaration-list is complete. */
14059 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14063 /* Peek at the next token. */
14064 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14065 /* If it's an ellipsis, then the list is complete. */
14066 if (token->type == CPP_ELLIPSIS)
14068 /* Otherwise, there must be more parameters. Consume the
14070 cp_lexer_consume_token (parser->lexer);
14071 /* When parsing something like:
14073 int i(float f, double d)
14075 we can tell after seeing the declaration for "f" that we
14076 are not looking at an initialization of a variable "i",
14077 but rather at the declaration of a function "i".
14079 Due to the fact that the parsing of template arguments
14080 (as specified to a template-id) requires backtracking we
14081 cannot use this technique when inside a template argument
14083 if (!parser->in_template_argument_list_p
14084 && !parser->in_type_id_in_expr_p
14085 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14086 /* However, a parameter-declaration of the form
14087 "foat(f)" (which is a valid declaration of a
14088 parameter "f") can also be interpreted as an
14089 expression (the conversion of "f" to "float"). */
14090 && !parenthesized_p)
14091 cp_parser_commit_to_tentative_parse (parser);
14095 cp_parser_error (parser, "expected %<,%> or %<...%>");
14096 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14097 cp_parser_skip_to_closing_parenthesis (parser,
14098 /*recovering=*/true,
14099 /*or_comma=*/false,
14100 /*consume_paren=*/false);
14105 parser->in_unbraced_linkage_specification_p
14106 = saved_in_unbraced_linkage_specification_p;
14111 /* Parse a parameter declaration.
14113 parameter-declaration:
14114 decl-specifier-seq ... [opt] declarator
14115 decl-specifier-seq declarator = assignment-expression
14116 decl-specifier-seq ... [opt] abstract-declarator [opt]
14117 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14119 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14120 declares a template parameter. (In that case, a non-nested `>'
14121 token encountered during the parsing of the assignment-expression
14122 is not interpreted as a greater-than operator.)
14124 Returns a representation of the parameter, or NULL if an error
14125 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14126 true iff the declarator is of the form "(p)". */
14128 static cp_parameter_declarator *
14129 cp_parser_parameter_declaration (cp_parser *parser,
14130 bool template_parm_p,
14131 bool *parenthesized_p)
14133 int declares_class_or_enum;
14134 bool greater_than_is_operator_p;
14135 cp_decl_specifier_seq decl_specifiers;
14136 cp_declarator *declarator;
14137 tree default_argument;
14138 cp_token *token = NULL, *declarator_token_start = NULL;
14139 const char *saved_message;
14141 /* In a template parameter, `>' is not an operator.
14145 When parsing a default template-argument for a non-type
14146 template-parameter, the first non-nested `>' is taken as the end
14147 of the template parameter-list rather than a greater-than
14149 greater_than_is_operator_p = !template_parm_p;
14151 /* Type definitions may not appear in parameter types. */
14152 saved_message = parser->type_definition_forbidden_message;
14153 parser->type_definition_forbidden_message
14154 = "types may not be defined in parameter types";
14156 /* Parse the declaration-specifiers. */
14157 cp_parser_decl_specifier_seq (parser,
14158 CP_PARSER_FLAGS_NONE,
14160 &declares_class_or_enum);
14161 /* If an error occurred, there's no reason to attempt to parse the
14162 rest of the declaration. */
14163 if (cp_parser_error_occurred (parser))
14165 parser->type_definition_forbidden_message = saved_message;
14169 /* Peek at the next token. */
14170 token = cp_lexer_peek_token (parser->lexer);
14172 /* If the next token is a `)', `,', `=', `>', or `...', then there
14173 is no declarator. However, when variadic templates are enabled,
14174 there may be a declarator following `...'. */
14175 if (token->type == CPP_CLOSE_PAREN
14176 || token->type == CPP_COMMA
14177 || token->type == CPP_EQ
14178 || token->type == CPP_GREATER)
14181 if (parenthesized_p)
14182 *parenthesized_p = false;
14184 /* Otherwise, there should be a declarator. */
14187 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14188 parser->default_arg_ok_p = false;
14190 /* After seeing a decl-specifier-seq, if the next token is not a
14191 "(", there is no possibility that the code is a valid
14192 expression. Therefore, if parsing tentatively, we commit at
14194 if (!parser->in_template_argument_list_p
14195 /* In an expression context, having seen:
14199 we cannot be sure whether we are looking at a
14200 function-type (taking a "char" as a parameter) or a cast
14201 of some object of type "char" to "int". */
14202 && !parser->in_type_id_in_expr_p
14203 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14204 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14205 cp_parser_commit_to_tentative_parse (parser);
14206 /* Parse the declarator. */
14207 declarator_token_start = token;
14208 declarator = cp_parser_declarator (parser,
14209 CP_PARSER_DECLARATOR_EITHER,
14210 /*ctor_dtor_or_conv_p=*/NULL,
14212 /*member_p=*/false);
14213 parser->default_arg_ok_p = saved_default_arg_ok_p;
14214 /* After the declarator, allow more attributes. */
14215 decl_specifiers.attributes
14216 = chainon (decl_specifiers.attributes,
14217 cp_parser_attributes_opt (parser));
14220 /* If the next token is an ellipsis, and we have not seen a
14221 declarator name, and the type of the declarator contains parameter
14222 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14223 a parameter pack expansion expression. Otherwise, leave the
14224 ellipsis for a C-style variadic function. */
14225 token = cp_lexer_peek_token (parser->lexer);
14226 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14228 tree type = decl_specifiers.type;
14230 if (type && DECL_P (type))
14231 type = TREE_TYPE (type);
14234 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14235 && declarator_can_be_parameter_pack (declarator)
14236 && (!declarator || !declarator->parameter_pack_p)
14237 && uses_parameter_packs (type))
14239 /* Consume the `...'. */
14240 cp_lexer_consume_token (parser->lexer);
14241 maybe_warn_variadic_templates ();
14243 /* Build a pack expansion type */
14245 declarator->parameter_pack_p = true;
14247 decl_specifiers.type = make_pack_expansion (type);
14251 /* The restriction on defining new types applies only to the type
14252 of the parameter, not to the default argument. */
14253 parser->type_definition_forbidden_message = saved_message;
14255 /* If the next token is `=', then process a default argument. */
14256 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14258 /* Consume the `='. */
14259 cp_lexer_consume_token (parser->lexer);
14261 /* If we are defining a class, then the tokens that make up the
14262 default argument must be saved and processed later. */
14263 if (!template_parm_p && at_class_scope_p ()
14264 && TYPE_BEING_DEFINED (current_class_type))
14266 unsigned depth = 0;
14267 int maybe_template_id = 0;
14268 cp_token *first_token;
14271 /* Add tokens until we have processed the entire default
14272 argument. We add the range [first_token, token). */
14273 first_token = cp_lexer_peek_token (parser->lexer);
14278 /* Peek at the next token. */
14279 token = cp_lexer_peek_token (parser->lexer);
14280 /* What we do depends on what token we have. */
14281 switch (token->type)
14283 /* In valid code, a default argument must be
14284 immediately followed by a `,' `)', or `...'. */
14286 if (depth == 0 && maybe_template_id)
14288 /* If we've seen a '<', we might be in a
14289 template-argument-list. Until Core issue 325 is
14290 resolved, we don't know how this situation ought
14291 to be handled, so try to DTRT. We check whether
14292 what comes after the comma is a valid parameter
14293 declaration list. If it is, then the comma ends
14294 the default argument; otherwise the default
14295 argument continues. */
14296 bool error = false;
14298 /* Set ITALP so cp_parser_parameter_declaration_list
14299 doesn't decide to commit to this parse. */
14300 bool saved_italp = parser->in_template_argument_list_p;
14301 parser->in_template_argument_list_p = true;
14303 cp_parser_parse_tentatively (parser);
14304 cp_lexer_consume_token (parser->lexer);
14305 cp_parser_parameter_declaration_list (parser, &error);
14306 if (!cp_parser_error_occurred (parser) && !error)
14308 cp_parser_abort_tentative_parse (parser);
14310 parser->in_template_argument_list_p = saved_italp;
14313 case CPP_CLOSE_PAREN:
14315 /* If we run into a non-nested `;', `}', or `]',
14316 then the code is invalid -- but the default
14317 argument is certainly over. */
14318 case CPP_SEMICOLON:
14319 case CPP_CLOSE_BRACE:
14320 case CPP_CLOSE_SQUARE:
14323 /* Update DEPTH, if necessary. */
14324 else if (token->type == CPP_CLOSE_PAREN
14325 || token->type == CPP_CLOSE_BRACE
14326 || token->type == CPP_CLOSE_SQUARE)
14330 case CPP_OPEN_PAREN:
14331 case CPP_OPEN_SQUARE:
14332 case CPP_OPEN_BRACE:
14338 /* This might be the comparison operator, or it might
14339 start a template argument list. */
14340 ++maybe_template_id;
14344 if (cxx_dialect == cxx98)
14346 /* Fall through for C++0x, which treats the `>>'
14347 operator like two `>' tokens in certain
14353 /* This might be an operator, or it might close a
14354 template argument list. But if a previous '<'
14355 started a template argument list, this will have
14356 closed it, so we can't be in one anymore. */
14357 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14358 if (maybe_template_id < 0)
14359 maybe_template_id = 0;
14363 /* If we run out of tokens, issue an error message. */
14365 case CPP_PRAGMA_EOL:
14366 error ("%Hfile ends in default argument", &token->location);
14372 /* In these cases, we should look for template-ids.
14373 For example, if the default argument is
14374 `X<int, double>()', we need to do name lookup to
14375 figure out whether or not `X' is a template; if
14376 so, the `,' does not end the default argument.
14378 That is not yet done. */
14385 /* If we've reached the end, stop. */
14389 /* Add the token to the token block. */
14390 token = cp_lexer_consume_token (parser->lexer);
14393 /* Create a DEFAULT_ARG to represent the unparsed default
14395 default_argument = make_node (DEFAULT_ARG);
14396 DEFARG_TOKENS (default_argument)
14397 = cp_token_cache_new (first_token, token);
14398 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14400 /* Outside of a class definition, we can just parse the
14401 assignment-expression. */
14404 token = cp_lexer_peek_token (parser->lexer);
14406 = cp_parser_default_argument (parser, template_parm_p);
14409 if (!parser->default_arg_ok_p)
14411 if (flag_permissive)
14412 warning (0, "deprecated use of default argument for parameter of non-function");
14415 error ("%Hdefault arguments are only "
14416 "permitted for function parameters",
14418 default_argument = NULL_TREE;
14421 else if ((declarator && declarator->parameter_pack_p)
14422 || (decl_specifiers.type
14423 && PACK_EXPANSION_P (decl_specifiers.type)))
14425 const char* kind = template_parm_p? "template " : "";
14427 /* Find the name of the parameter pack. */
14428 cp_declarator *id_declarator = declarator;
14429 while (id_declarator && id_declarator->kind != cdk_id)
14430 id_declarator = id_declarator->declarator;
14432 if (id_declarator && id_declarator->kind == cdk_id)
14433 error ("%H%sparameter pack %qD cannot have a default argument",
14434 &declarator_token_start->location,
14435 kind, id_declarator->u.id.unqualified_name);
14437 error ("%H%sparameter pack cannot have a default argument",
14438 &declarator_token_start->location, kind);
14440 default_argument = NULL_TREE;
14444 default_argument = NULL_TREE;
14446 return make_parameter_declarator (&decl_specifiers,
14451 /* Parse a default argument and return it.
14453 TEMPLATE_PARM_P is true if this is a default argument for a
14454 non-type template parameter. */
14456 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14458 tree default_argument = NULL_TREE;
14459 bool saved_greater_than_is_operator_p;
14460 bool saved_local_variables_forbidden_p;
14462 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14464 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14465 parser->greater_than_is_operator_p = !template_parm_p;
14466 /* Local variable names (and the `this' keyword) may not
14467 appear in a default argument. */
14468 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14469 parser->local_variables_forbidden_p = true;
14470 /* The default argument expression may cause implicitly
14471 defined member functions to be synthesized, which will
14472 result in garbage collection. We must treat this
14473 situation as if we were within the body of function so as
14474 to avoid collecting live data on the stack. */
14476 /* Parse the assignment-expression. */
14477 if (template_parm_p)
14478 push_deferring_access_checks (dk_no_deferred);
14480 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14481 if (template_parm_p)
14482 pop_deferring_access_checks ();
14483 /* Restore saved state. */
14485 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14486 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14488 return default_argument;
14491 /* Parse a function-body.
14494 compound_statement */
14497 cp_parser_function_body (cp_parser *parser)
14499 cp_parser_compound_statement (parser, NULL, false);
14502 /* Parse a ctor-initializer-opt followed by a function-body. Return
14503 true if a ctor-initializer was present. */
14506 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14509 bool ctor_initializer_p;
14511 /* Begin the function body. */
14512 body = begin_function_body ();
14513 /* Parse the optional ctor-initializer. */
14514 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14515 /* Parse the function-body. */
14516 cp_parser_function_body (parser);
14517 /* Finish the function body. */
14518 finish_function_body (body);
14520 return ctor_initializer_p;
14523 /* Parse an initializer.
14526 = initializer-clause
14527 ( expression-list )
14529 Returns an expression representing the initializer. If no
14530 initializer is present, NULL_TREE is returned.
14532 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14533 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14534 set to TRUE if there is no initializer present. If there is an
14535 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14536 is set to true; otherwise it is set to false. */
14539 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14540 bool* non_constant_p)
14545 /* Peek at the next token. */
14546 token = cp_lexer_peek_token (parser->lexer);
14548 /* Let our caller know whether or not this initializer was
14550 *is_direct_init = (token->type != CPP_EQ);
14551 /* Assume that the initializer is constant. */
14552 *non_constant_p = false;
14554 if (token->type == CPP_EQ)
14556 /* Consume the `='. */
14557 cp_lexer_consume_token (parser->lexer);
14558 /* Parse the initializer-clause. */
14559 init = cp_parser_initializer_clause (parser, non_constant_p);
14561 else if (token->type == CPP_OPEN_PAREN)
14562 init = cp_parser_parenthesized_expression_list (parser, false,
14564 /*allow_expansion_p=*/true,
14566 else if (token->type == CPP_OPEN_BRACE)
14568 maybe_warn_cpp0x ("extended initializer lists");
14569 init = cp_parser_braced_list (parser, non_constant_p);
14570 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14574 /* Anything else is an error. */
14575 cp_parser_error (parser, "expected initializer");
14576 init = error_mark_node;
14582 /* Parse an initializer-clause.
14584 initializer-clause:
14585 assignment-expression
14588 Returns an expression representing the initializer.
14590 If the `assignment-expression' production is used the value
14591 returned is simply a representation for the expression.
14593 Otherwise, calls cp_parser_braced_list. */
14596 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14600 /* Assume the expression is constant. */
14601 *non_constant_p = false;
14603 /* If it is not a `{', then we are looking at an
14604 assignment-expression. */
14605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14608 = cp_parser_constant_expression (parser,
14609 /*allow_non_constant_p=*/true,
14611 if (!*non_constant_p)
14612 initializer = fold_non_dependent_expr (initializer);
14615 initializer = cp_parser_braced_list (parser, non_constant_p);
14617 return initializer;
14620 /* Parse a brace-enclosed initializer list.
14623 { initializer-list , [opt] }
14626 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14627 the elements of the initializer-list (or NULL, if the last
14628 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14629 NULL_TREE. There is no way to detect whether or not the optional
14630 trailing `,' was provided. NON_CONSTANT_P is as for
14631 cp_parser_initializer. */
14634 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14638 /* Consume the `{' token. */
14639 cp_lexer_consume_token (parser->lexer);
14640 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14641 initializer = make_node (CONSTRUCTOR);
14642 /* If it's not a `}', then there is a non-trivial initializer. */
14643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14645 /* Parse the initializer list. */
14646 CONSTRUCTOR_ELTS (initializer)
14647 = cp_parser_initializer_list (parser, non_constant_p);
14648 /* A trailing `,' token is allowed. */
14649 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14650 cp_lexer_consume_token (parser->lexer);
14652 /* Now, there should be a trailing `}'. */
14653 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14654 TREE_TYPE (initializer) = init_list_type_node;
14655 return initializer;
14658 /* Parse an initializer-list.
14661 initializer-clause ... [opt]
14662 initializer-list , initializer-clause ... [opt]
14667 identifier : initializer-clause
14668 initializer-list, identifier : initializer-clause
14670 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14671 for the initializer. If the INDEX of the elt is non-NULL, it is the
14672 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14673 as for cp_parser_initializer. */
14675 static VEC(constructor_elt,gc) *
14676 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14678 VEC(constructor_elt,gc) *v = NULL;
14680 /* Assume all of the expressions are constant. */
14681 *non_constant_p = false;
14683 /* Parse the rest of the list. */
14689 bool clause_non_constant_p;
14691 /* If the next token is an identifier and the following one is a
14692 colon, we are looking at the GNU designated-initializer
14694 if (cp_parser_allow_gnu_extensions_p (parser)
14695 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14696 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14698 /* Warn the user that they are using an extension. */
14699 pedwarn (input_location, OPT_pedantic,
14700 "ISO C++ does not allow designated initializers");
14701 /* Consume the identifier. */
14702 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14703 /* Consume the `:'. */
14704 cp_lexer_consume_token (parser->lexer);
14707 identifier = NULL_TREE;
14709 /* Parse the initializer. */
14710 initializer = cp_parser_initializer_clause (parser,
14711 &clause_non_constant_p);
14712 /* If any clause is non-constant, so is the entire initializer. */
14713 if (clause_non_constant_p)
14714 *non_constant_p = true;
14716 /* If we have an ellipsis, this is an initializer pack
14718 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14720 /* Consume the `...'. */
14721 cp_lexer_consume_token (parser->lexer);
14723 /* Turn the initializer into an initializer expansion. */
14724 initializer = make_pack_expansion (initializer);
14727 /* Add it to the vector. */
14728 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14730 /* If the next token is not a comma, we have reached the end of
14732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14735 /* Peek at the next token. */
14736 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14737 /* If the next token is a `}', then we're still done. An
14738 initializer-clause can have a trailing `,' after the
14739 initializer-list and before the closing `}'. */
14740 if (token->type == CPP_CLOSE_BRACE)
14743 /* Consume the `,' token. */
14744 cp_lexer_consume_token (parser->lexer);
14750 /* Classes [gram.class] */
14752 /* Parse a class-name.
14758 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14759 to indicate that names looked up in dependent types should be
14760 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14761 keyword has been used to indicate that the name that appears next
14762 is a template. TAG_TYPE indicates the explicit tag given before
14763 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14764 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14765 is the class being defined in a class-head.
14767 Returns the TYPE_DECL representing the class. */
14770 cp_parser_class_name (cp_parser *parser,
14771 bool typename_keyword_p,
14772 bool template_keyword_p,
14773 enum tag_types tag_type,
14774 bool check_dependency_p,
14776 bool is_declaration)
14782 tree identifier = NULL_TREE;
14784 /* All class-names start with an identifier. */
14785 token = cp_lexer_peek_token (parser->lexer);
14786 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14788 cp_parser_error (parser, "expected class-name");
14789 return error_mark_node;
14792 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14793 to a template-id, so we save it here. */
14794 scope = parser->scope;
14795 if (scope == error_mark_node)
14796 return error_mark_node;
14798 /* Any name names a type if we're following the `typename' keyword
14799 in a qualified name where the enclosing scope is type-dependent. */
14800 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14801 && dependent_type_p (scope));
14802 /* Handle the common case (an identifier, but not a template-id)
14804 if (token->type == CPP_NAME
14805 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14807 cp_token *identifier_token;
14810 /* Look for the identifier. */
14811 identifier_token = cp_lexer_peek_token (parser->lexer);
14812 ambiguous_p = identifier_token->ambiguous_p;
14813 identifier = cp_parser_identifier (parser);
14814 /* If the next token isn't an identifier, we are certainly not
14815 looking at a class-name. */
14816 if (identifier == error_mark_node)
14817 decl = error_mark_node;
14818 /* If we know this is a type-name, there's no need to look it
14820 else if (typename_p)
14824 tree ambiguous_decls;
14825 /* If we already know that this lookup is ambiguous, then
14826 we've already issued an error message; there's no reason
14830 cp_parser_simulate_error (parser);
14831 return error_mark_node;
14833 /* If the next token is a `::', then the name must be a type
14836 [basic.lookup.qual]
14838 During the lookup for a name preceding the :: scope
14839 resolution operator, object, function, and enumerator
14840 names are ignored. */
14841 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14842 tag_type = typename_type;
14843 /* Look up the name. */
14844 decl = cp_parser_lookup_name (parser, identifier,
14846 /*is_template=*/false,
14847 /*is_namespace=*/false,
14848 check_dependency_p,
14850 identifier_token->location);
14851 if (ambiguous_decls)
14853 error ("%Hreference to %qD is ambiguous",
14854 &identifier_token->location, identifier);
14855 print_candidates (ambiguous_decls);
14856 if (cp_parser_parsing_tentatively (parser))
14858 identifier_token->ambiguous_p = true;
14859 cp_parser_simulate_error (parser);
14861 return error_mark_node;
14867 /* Try a template-id. */
14868 decl = cp_parser_template_id (parser, template_keyword_p,
14869 check_dependency_p,
14871 if (decl == error_mark_node)
14872 return error_mark_node;
14875 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14877 /* If this is a typename, create a TYPENAME_TYPE. */
14878 if (typename_p && decl != error_mark_node)
14880 decl = make_typename_type (scope, decl, typename_type,
14881 /*complain=*/tf_error);
14882 if (decl != error_mark_node)
14883 decl = TYPE_NAME (decl);
14886 /* Check to see that it is really the name of a class. */
14887 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14888 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14889 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14890 /* Situations like this:
14892 template <typename T> struct A {
14893 typename T::template X<int>::I i;
14896 are problematic. Is `T::template X<int>' a class-name? The
14897 standard does not seem to be definitive, but there is no other
14898 valid interpretation of the following `::'. Therefore, those
14899 names are considered class-names. */
14901 decl = make_typename_type (scope, decl, tag_type, tf_error);
14902 if (decl != error_mark_node)
14903 decl = TYPE_NAME (decl);
14905 else if (TREE_CODE (decl) != TYPE_DECL
14906 || TREE_TYPE (decl) == error_mark_node
14907 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14908 decl = error_mark_node;
14910 if (decl == error_mark_node)
14911 cp_parser_error (parser, "expected class-name");
14912 else if (identifier && !parser->scope)
14913 maybe_note_name_used_in_class (identifier, decl);
14918 /* Parse a class-specifier.
14921 class-head { member-specification [opt] }
14923 Returns the TREE_TYPE representing the class. */
14926 cp_parser_class_specifier (cp_parser* parser)
14930 tree attributes = NULL_TREE;
14931 int has_trailing_semicolon;
14932 bool nested_name_specifier_p;
14933 unsigned saved_num_template_parameter_lists;
14934 bool saved_in_function_body;
14935 bool saved_in_unbraced_linkage_specification_p;
14936 tree old_scope = NULL_TREE;
14937 tree scope = NULL_TREE;
14940 push_deferring_access_checks (dk_no_deferred);
14942 /* Parse the class-head. */
14943 type = cp_parser_class_head (parser,
14944 &nested_name_specifier_p,
14947 /* If the class-head was a semantic disaster, skip the entire body
14951 cp_parser_skip_to_end_of_block_or_statement (parser);
14952 pop_deferring_access_checks ();
14953 return error_mark_node;
14956 /* Look for the `{'. */
14957 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14959 pop_deferring_access_checks ();
14960 return error_mark_node;
14963 /* Process the base classes. If they're invalid, skip the
14964 entire class body. */
14965 if (!xref_basetypes (type, bases))
14967 /* Consuming the closing brace yields better error messages
14969 if (cp_parser_skip_to_closing_brace (parser))
14970 cp_lexer_consume_token (parser->lexer);
14971 pop_deferring_access_checks ();
14972 return error_mark_node;
14975 /* Issue an error message if type-definitions are forbidden here. */
14976 cp_parser_check_type_definition (parser);
14977 /* Remember that we are defining one more class. */
14978 ++parser->num_classes_being_defined;
14979 /* Inside the class, surrounding template-parameter-lists do not
14981 saved_num_template_parameter_lists
14982 = parser->num_template_parameter_lists;
14983 parser->num_template_parameter_lists = 0;
14984 /* We are not in a function body. */
14985 saved_in_function_body = parser->in_function_body;
14986 parser->in_function_body = false;
14987 /* We are not immediately inside an extern "lang" block. */
14988 saved_in_unbraced_linkage_specification_p
14989 = parser->in_unbraced_linkage_specification_p;
14990 parser->in_unbraced_linkage_specification_p = false;
14992 /* Start the class. */
14993 if (nested_name_specifier_p)
14995 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14996 old_scope = push_inner_scope (scope);
14998 type = begin_class_definition (type, attributes);
15000 if (type == error_mark_node)
15001 /* If the type is erroneous, skip the entire body of the class. */
15002 cp_parser_skip_to_closing_brace (parser);
15004 /* Parse the member-specification. */
15005 cp_parser_member_specification_opt (parser);
15007 /* Look for the trailing `}'. */
15008 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15009 /* We get better error messages by noticing a common problem: a
15010 missing trailing `;'. */
15011 token = cp_lexer_peek_token (parser->lexer);
15012 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15013 /* Look for trailing attributes to apply to this class. */
15014 if (cp_parser_allow_gnu_extensions_p (parser))
15015 attributes = cp_parser_attributes_opt (parser);
15016 if (type != error_mark_node)
15017 type = finish_struct (type, attributes);
15018 if (nested_name_specifier_p)
15019 pop_inner_scope (old_scope, scope);
15020 /* If this class is not itself within the scope of another class,
15021 then we need to parse the bodies of all of the queued function
15022 definitions. Note that the queued functions defined in a class
15023 are not always processed immediately following the
15024 class-specifier for that class. Consider:
15027 struct B { void f() { sizeof (A); } };
15030 If `f' were processed before the processing of `A' were
15031 completed, there would be no way to compute the size of `A'.
15032 Note that the nesting we are interested in here is lexical --
15033 not the semantic nesting given by TYPE_CONTEXT. In particular,
15036 struct A { struct B; };
15037 struct A::B { void f() { } };
15039 there is no need to delay the parsing of `A::B::f'. */
15040 if (--parser->num_classes_being_defined == 0)
15044 tree class_type = NULL_TREE;
15045 tree pushed_scope = NULL_TREE;
15047 /* In a first pass, parse default arguments to the functions.
15048 Then, in a second pass, parse the bodies of the functions.
15049 This two-phased approach handles cases like:
15057 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15058 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15059 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15060 TREE_PURPOSE (parser->unparsed_functions_queues)
15061 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15063 fn = TREE_VALUE (queue_entry);
15064 /* If there are default arguments that have not yet been processed,
15065 take care of them now. */
15066 if (class_type != TREE_PURPOSE (queue_entry))
15069 pop_scope (pushed_scope);
15070 class_type = TREE_PURPOSE (queue_entry);
15071 pushed_scope = push_scope (class_type);
15073 /* Make sure that any template parameters are in scope. */
15074 maybe_begin_member_template_processing (fn);
15075 /* Parse the default argument expressions. */
15076 cp_parser_late_parsing_default_args (parser, fn);
15077 /* Remove any template parameters from the symbol table. */
15078 maybe_end_member_template_processing ();
15081 pop_scope (pushed_scope);
15082 /* Now parse the body of the functions. */
15083 for (TREE_VALUE (parser->unparsed_functions_queues)
15084 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15085 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15086 TREE_VALUE (parser->unparsed_functions_queues)
15087 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15089 /* Figure out which function we need to process. */
15090 fn = TREE_VALUE (queue_entry);
15091 /* Parse the function. */
15092 cp_parser_late_parsing_for_member (parser, fn);
15096 /* Put back any saved access checks. */
15097 pop_deferring_access_checks ();
15099 /* Restore saved state. */
15100 parser->in_function_body = saved_in_function_body;
15101 parser->num_template_parameter_lists
15102 = saved_num_template_parameter_lists;
15103 parser->in_unbraced_linkage_specification_p
15104 = saved_in_unbraced_linkage_specification_p;
15109 /* Parse a class-head.
15112 class-key identifier [opt] base-clause [opt]
15113 class-key nested-name-specifier identifier base-clause [opt]
15114 class-key nested-name-specifier [opt] template-id
15118 class-key attributes identifier [opt] base-clause [opt]
15119 class-key attributes nested-name-specifier identifier base-clause [opt]
15120 class-key attributes nested-name-specifier [opt] template-id
15123 Upon return BASES is initialized to the list of base classes (or
15124 NULL, if there are none) in the same form returned by
15125 cp_parser_base_clause.
15127 Returns the TYPE of the indicated class. Sets
15128 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15129 involving a nested-name-specifier was used, and FALSE otherwise.
15131 Returns error_mark_node if this is not a class-head.
15133 Returns NULL_TREE if the class-head is syntactically valid, but
15134 semantically invalid in a way that means we should skip the entire
15135 body of the class. */
15138 cp_parser_class_head (cp_parser* parser,
15139 bool* nested_name_specifier_p,
15140 tree *attributes_p,
15143 tree nested_name_specifier;
15144 enum tag_types class_key;
15145 tree id = NULL_TREE;
15146 tree type = NULL_TREE;
15148 bool template_id_p = false;
15149 bool qualified_p = false;
15150 bool invalid_nested_name_p = false;
15151 bool invalid_explicit_specialization_p = false;
15152 tree pushed_scope = NULL_TREE;
15153 unsigned num_templates;
15154 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15155 /* Assume no nested-name-specifier will be present. */
15156 *nested_name_specifier_p = false;
15157 /* Assume no template parameter lists will be used in defining the
15161 *bases = NULL_TREE;
15163 /* Look for the class-key. */
15164 class_key = cp_parser_class_key (parser);
15165 if (class_key == none_type)
15166 return error_mark_node;
15168 /* Parse the attributes. */
15169 attributes = cp_parser_attributes_opt (parser);
15171 /* If the next token is `::', that is invalid -- but sometimes
15172 people do try to write:
15176 Handle this gracefully by accepting the extra qualifier, and then
15177 issuing an error about it later if this really is a
15178 class-head. If it turns out just to be an elaborated type
15179 specifier, remain silent. */
15180 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15181 qualified_p = true;
15183 push_deferring_access_checks (dk_no_check);
15185 /* Determine the name of the class. Begin by looking for an
15186 optional nested-name-specifier. */
15187 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15188 nested_name_specifier
15189 = cp_parser_nested_name_specifier_opt (parser,
15190 /*typename_keyword_p=*/false,
15191 /*check_dependency_p=*/false,
15193 /*is_declaration=*/false);
15194 /* If there was a nested-name-specifier, then there *must* be an
15196 if (nested_name_specifier)
15198 type_start_token = cp_lexer_peek_token (parser->lexer);
15199 /* Although the grammar says `identifier', it really means
15200 `class-name' or `template-name'. You are only allowed to
15201 define a class that has already been declared with this
15204 The proposed resolution for Core Issue 180 says that wherever
15205 you see `class T::X' you should treat `X' as a type-name.
15207 It is OK to define an inaccessible class; for example:
15209 class A { class B; };
15212 We do not know if we will see a class-name, or a
15213 template-name. We look for a class-name first, in case the
15214 class-name is a template-id; if we looked for the
15215 template-name first we would stop after the template-name. */
15216 cp_parser_parse_tentatively (parser);
15217 type = cp_parser_class_name (parser,
15218 /*typename_keyword_p=*/false,
15219 /*template_keyword_p=*/false,
15221 /*check_dependency_p=*/false,
15222 /*class_head_p=*/true,
15223 /*is_declaration=*/false);
15224 /* If that didn't work, ignore the nested-name-specifier. */
15225 if (!cp_parser_parse_definitely (parser))
15227 invalid_nested_name_p = true;
15228 type_start_token = cp_lexer_peek_token (parser->lexer);
15229 id = cp_parser_identifier (parser);
15230 if (id == error_mark_node)
15233 /* If we could not find a corresponding TYPE, treat this
15234 declaration like an unqualified declaration. */
15235 if (type == error_mark_node)
15236 nested_name_specifier = NULL_TREE;
15237 /* Otherwise, count the number of templates used in TYPE and its
15238 containing scopes. */
15243 for (scope = TREE_TYPE (type);
15244 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15245 scope = (TYPE_P (scope)
15246 ? TYPE_CONTEXT (scope)
15247 : DECL_CONTEXT (scope)))
15249 && CLASS_TYPE_P (scope)
15250 && CLASSTYPE_TEMPLATE_INFO (scope)
15251 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15252 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15256 /* Otherwise, the identifier is optional. */
15259 /* We don't know whether what comes next is a template-id,
15260 an identifier, or nothing at all. */
15261 cp_parser_parse_tentatively (parser);
15262 /* Check for a template-id. */
15263 type_start_token = cp_lexer_peek_token (parser->lexer);
15264 id = cp_parser_template_id (parser,
15265 /*template_keyword_p=*/false,
15266 /*check_dependency_p=*/true,
15267 /*is_declaration=*/true);
15268 /* If that didn't work, it could still be an identifier. */
15269 if (!cp_parser_parse_definitely (parser))
15271 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15273 type_start_token = cp_lexer_peek_token (parser->lexer);
15274 id = cp_parser_identifier (parser);
15281 template_id_p = true;
15286 pop_deferring_access_checks ();
15289 cp_parser_check_for_invalid_template_id (parser, id,
15290 type_start_token->location);
15292 /* If it's not a `:' or a `{' then we can't really be looking at a
15293 class-head, since a class-head only appears as part of a
15294 class-specifier. We have to detect this situation before calling
15295 xref_tag, since that has irreversible side-effects. */
15296 if (!cp_parser_next_token_starts_class_definition_p (parser))
15298 cp_parser_error (parser, "expected %<{%> or %<:%>");
15299 return error_mark_node;
15302 /* At this point, we're going ahead with the class-specifier, even
15303 if some other problem occurs. */
15304 cp_parser_commit_to_tentative_parse (parser);
15305 /* Issue the error about the overly-qualified name now. */
15308 cp_parser_error (parser,
15309 "global qualification of class name is invalid");
15310 return error_mark_node;
15312 else if (invalid_nested_name_p)
15314 cp_parser_error (parser,
15315 "qualified name does not name a class");
15316 return error_mark_node;
15318 else if (nested_name_specifier)
15322 /* Reject typedef-names in class heads. */
15323 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15325 error ("%Hinvalid class name in declaration of %qD",
15326 &type_start_token->location, type);
15331 /* Figure out in what scope the declaration is being placed. */
15332 scope = current_scope ();
15333 /* If that scope does not contain the scope in which the
15334 class was originally declared, the program is invalid. */
15335 if (scope && !is_ancestor (scope, nested_name_specifier))
15337 if (at_namespace_scope_p ())
15338 error ("%Hdeclaration of %qD in namespace %qD which does not "
15340 &type_start_token->location,
15341 type, scope, nested_name_specifier);
15343 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15344 &type_start_token->location,
15345 type, scope, nested_name_specifier);
15351 A declarator-id shall not be qualified except for the
15352 definition of a ... nested class outside of its class
15353 ... [or] the definition or explicit instantiation of a
15354 class member of a namespace outside of its namespace. */
15355 if (scope == nested_name_specifier)
15357 permerror (input_location, "%Hextra qualification not allowed",
15358 &nested_name_specifier_token_start->location);
15359 nested_name_specifier = NULL_TREE;
15363 /* An explicit-specialization must be preceded by "template <>". If
15364 it is not, try to recover gracefully. */
15365 if (at_namespace_scope_p ()
15366 && parser->num_template_parameter_lists == 0
15369 error ("%Han explicit specialization must be preceded by %<template <>%>",
15370 &type_start_token->location);
15371 invalid_explicit_specialization_p = true;
15372 /* Take the same action that would have been taken by
15373 cp_parser_explicit_specialization. */
15374 ++parser->num_template_parameter_lists;
15375 begin_specialization ();
15377 /* There must be no "return" statements between this point and the
15378 end of this function; set "type "to the correct return value and
15379 use "goto done;" to return. */
15380 /* Make sure that the right number of template parameters were
15382 if (!cp_parser_check_template_parameters (parser, num_templates,
15383 type_start_token->location))
15385 /* If something went wrong, there is no point in even trying to
15386 process the class-definition. */
15391 /* Look up the type. */
15394 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15395 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15396 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15398 error ("%Hfunction template %qD redeclared as a class template",
15399 &type_start_token->location, id);
15400 type = error_mark_node;
15404 type = TREE_TYPE (id);
15405 type = maybe_process_partial_specialization (type);
15407 if (nested_name_specifier)
15408 pushed_scope = push_scope (nested_name_specifier);
15410 else if (nested_name_specifier)
15416 template <typename T> struct S { struct T };
15417 template <typename T> struct S<T>::T { };
15419 we will get a TYPENAME_TYPE when processing the definition of
15420 `S::T'. We need to resolve it to the actual type before we
15421 try to define it. */
15422 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15424 class_type = resolve_typename_type (TREE_TYPE (type),
15425 /*only_current_p=*/false);
15426 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15427 type = TYPE_NAME (class_type);
15430 cp_parser_error (parser, "could not resolve typename type");
15431 type = error_mark_node;
15435 if (maybe_process_partial_specialization (TREE_TYPE (type))
15436 == error_mark_node)
15442 class_type = current_class_type;
15443 /* Enter the scope indicated by the nested-name-specifier. */
15444 pushed_scope = push_scope (nested_name_specifier);
15445 /* Get the canonical version of this type. */
15446 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15447 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15448 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15450 type = push_template_decl (type);
15451 if (type == error_mark_node)
15458 type = TREE_TYPE (type);
15459 *nested_name_specifier_p = true;
15461 else /* The name is not a nested name. */
15463 /* If the class was unnamed, create a dummy name. */
15465 id = make_anon_name ();
15466 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15467 parser->num_template_parameter_lists);
15470 /* Indicate whether this class was declared as a `class' or as a
15472 if (TREE_CODE (type) == RECORD_TYPE)
15473 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15474 cp_parser_check_class_key (class_key, type);
15476 /* If this type was already complete, and we see another definition,
15477 that's an error. */
15478 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15480 error ("%Hredefinition of %q#T",
15481 &type_start_token->location, type);
15482 error ("%Hprevious definition of %q+#T",
15483 &type_start_token->location, type);
15487 else if (type == error_mark_node)
15490 /* We will have entered the scope containing the class; the names of
15491 base classes should be looked up in that context. For example:
15493 struct A { struct B {}; struct C; };
15494 struct A::C : B {};
15498 /* Get the list of base-classes, if there is one. */
15499 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15500 *bases = cp_parser_base_clause (parser);
15503 /* Leave the scope given by the nested-name-specifier. We will
15504 enter the class scope itself while processing the members. */
15506 pop_scope (pushed_scope);
15508 if (invalid_explicit_specialization_p)
15510 end_specialization ();
15511 --parser->num_template_parameter_lists;
15513 *attributes_p = attributes;
15517 /* Parse a class-key.
15524 Returns the kind of class-key specified, or none_type to indicate
15527 static enum tag_types
15528 cp_parser_class_key (cp_parser* parser)
15531 enum tag_types tag_type;
15533 /* Look for the class-key. */
15534 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15538 /* Check to see if the TOKEN is a class-key. */
15539 tag_type = cp_parser_token_is_class_key (token);
15541 cp_parser_error (parser, "expected class-key");
15545 /* Parse an (optional) member-specification.
15547 member-specification:
15548 member-declaration member-specification [opt]
15549 access-specifier : member-specification [opt] */
15552 cp_parser_member_specification_opt (cp_parser* parser)
15559 /* Peek at the next token. */
15560 token = cp_lexer_peek_token (parser->lexer);
15561 /* If it's a `}', or EOF then we've seen all the members. */
15562 if (token->type == CPP_CLOSE_BRACE
15563 || token->type == CPP_EOF
15564 || token->type == CPP_PRAGMA_EOL)
15567 /* See if this token is a keyword. */
15568 keyword = token->keyword;
15572 case RID_PROTECTED:
15574 /* Consume the access-specifier. */
15575 cp_lexer_consume_token (parser->lexer);
15576 /* Remember which access-specifier is active. */
15577 current_access_specifier = token->u.value;
15578 /* Look for the `:'. */
15579 cp_parser_require (parser, CPP_COLON, "%<:%>");
15583 /* Accept #pragmas at class scope. */
15584 if (token->type == CPP_PRAGMA)
15586 cp_parser_pragma (parser, pragma_external);
15590 /* Otherwise, the next construction must be a
15591 member-declaration. */
15592 cp_parser_member_declaration (parser);
15597 /* Parse a member-declaration.
15599 member-declaration:
15600 decl-specifier-seq [opt] member-declarator-list [opt] ;
15601 function-definition ; [opt]
15602 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15604 template-declaration
15606 member-declarator-list:
15608 member-declarator-list , member-declarator
15611 declarator pure-specifier [opt]
15612 declarator constant-initializer [opt]
15613 identifier [opt] : constant-expression
15617 member-declaration:
15618 __extension__ member-declaration
15621 declarator attributes [opt] pure-specifier [opt]
15622 declarator attributes [opt] constant-initializer [opt]
15623 identifier [opt] attributes [opt] : constant-expression
15627 member-declaration:
15628 static_assert-declaration */
15631 cp_parser_member_declaration (cp_parser* parser)
15633 cp_decl_specifier_seq decl_specifiers;
15634 tree prefix_attributes;
15636 int declares_class_or_enum;
15638 cp_token *token = NULL;
15639 cp_token *decl_spec_token_start = NULL;
15640 cp_token *initializer_token_start = NULL;
15641 int saved_pedantic;
15643 /* Check for the `__extension__' keyword. */
15644 if (cp_parser_extension_opt (parser, &saved_pedantic))
15647 cp_parser_member_declaration (parser);
15648 /* Restore the old value of the PEDANTIC flag. */
15649 pedantic = saved_pedantic;
15654 /* Check for a template-declaration. */
15655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15657 /* An explicit specialization here is an error condition, and we
15658 expect the specialization handler to detect and report this. */
15659 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15660 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15661 cp_parser_explicit_specialization (parser);
15663 cp_parser_template_declaration (parser, /*member_p=*/true);
15668 /* Check for a using-declaration. */
15669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15671 /* Parse the using-declaration. */
15672 cp_parser_using_declaration (parser,
15673 /*access_declaration_p=*/false);
15677 /* Check for @defs. */
15678 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15681 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15682 ivar = ivar_chains;
15686 ivar = TREE_CHAIN (member);
15687 TREE_CHAIN (member) = NULL_TREE;
15688 finish_member_declaration (member);
15693 /* If the next token is `static_assert' we have a static assertion. */
15694 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15696 cp_parser_static_assert (parser, /*member_p=*/true);
15700 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15703 /* Parse the decl-specifier-seq. */
15704 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15705 cp_parser_decl_specifier_seq (parser,
15706 CP_PARSER_FLAGS_OPTIONAL,
15708 &declares_class_or_enum);
15709 prefix_attributes = decl_specifiers.attributes;
15710 decl_specifiers.attributes = NULL_TREE;
15711 /* Check for an invalid type-name. */
15712 if (!decl_specifiers.type
15713 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15715 /* If there is no declarator, then the decl-specifier-seq should
15717 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15719 /* If there was no decl-specifier-seq, and the next token is a
15720 `;', then we have something like:
15726 Each member-declaration shall declare at least one member
15727 name of the class. */
15728 if (!decl_specifiers.any_specifiers_p)
15730 cp_token *token = cp_lexer_peek_token (parser->lexer);
15731 if (!in_system_header_at (token->location))
15732 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15738 /* See if this declaration is a friend. */
15739 friend_p = cp_parser_friend_p (&decl_specifiers);
15740 /* If there were decl-specifiers, check to see if there was
15741 a class-declaration. */
15742 type = check_tag_decl (&decl_specifiers);
15743 /* Nested classes have already been added to the class, but
15744 a `friend' needs to be explicitly registered. */
15747 /* If the `friend' keyword was present, the friend must
15748 be introduced with a class-key. */
15749 if (!declares_class_or_enum)
15750 error ("%Ha class-key must be used when declaring a friend",
15751 &decl_spec_token_start->location);
15754 template <typename T> struct A {
15755 friend struct A<T>::B;
15758 A<T>::B will be represented by a TYPENAME_TYPE, and
15759 therefore not recognized by check_tag_decl. */
15761 && decl_specifiers.type
15762 && TYPE_P (decl_specifiers.type))
15763 type = decl_specifiers.type;
15764 if (!type || !TYPE_P (type))
15765 error ("%Hfriend declaration does not name a class or "
15766 "function", &decl_spec_token_start->location);
15768 make_friend_class (current_class_type, type,
15769 /*complain=*/true);
15771 /* If there is no TYPE, an error message will already have
15773 else if (!type || type == error_mark_node)
15775 /* An anonymous aggregate has to be handled specially; such
15776 a declaration really declares a data member (with a
15777 particular type), as opposed to a nested class. */
15778 else if (ANON_AGGR_TYPE_P (type))
15780 /* Remove constructors and such from TYPE, now that we
15781 know it is an anonymous aggregate. */
15782 fixup_anonymous_aggr (type);
15783 /* And make the corresponding data member. */
15784 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15785 /* Add it to the class. */
15786 finish_member_declaration (decl);
15789 cp_parser_check_access_in_redeclaration
15791 decl_spec_token_start->location);
15796 /* See if these declarations will be friends. */
15797 friend_p = cp_parser_friend_p (&decl_specifiers);
15799 /* Keep going until we hit the `;' at the end of the
15801 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15803 tree attributes = NULL_TREE;
15804 tree first_attribute;
15806 /* Peek at the next token. */
15807 token = cp_lexer_peek_token (parser->lexer);
15809 /* Check for a bitfield declaration. */
15810 if (token->type == CPP_COLON
15811 || (token->type == CPP_NAME
15812 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15818 /* Get the name of the bitfield. Note that we cannot just
15819 check TOKEN here because it may have been invalidated by
15820 the call to cp_lexer_peek_nth_token above. */
15821 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15822 identifier = cp_parser_identifier (parser);
15824 identifier = NULL_TREE;
15826 /* Consume the `:' token. */
15827 cp_lexer_consume_token (parser->lexer);
15828 /* Get the width of the bitfield. */
15830 = cp_parser_constant_expression (parser,
15831 /*allow_non_constant=*/false,
15834 /* Look for attributes that apply to the bitfield. */
15835 attributes = cp_parser_attributes_opt (parser);
15836 /* Remember which attributes are prefix attributes and
15838 first_attribute = attributes;
15839 /* Combine the attributes. */
15840 attributes = chainon (prefix_attributes, attributes);
15842 /* Create the bitfield declaration. */
15843 decl = grokbitfield (identifier
15844 ? make_id_declarator (NULL_TREE,
15854 cp_declarator *declarator;
15856 tree asm_specification;
15857 int ctor_dtor_or_conv_p;
15859 /* Parse the declarator. */
15861 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15862 &ctor_dtor_or_conv_p,
15863 /*parenthesized_p=*/NULL,
15864 /*member_p=*/true);
15866 /* If something went wrong parsing the declarator, make sure
15867 that we at least consume some tokens. */
15868 if (declarator == cp_error_declarator)
15870 /* Skip to the end of the statement. */
15871 cp_parser_skip_to_end_of_statement (parser);
15872 /* If the next token is not a semicolon, that is
15873 probably because we just skipped over the body of
15874 a function. So, we consume a semicolon if
15875 present, but do not issue an error message if it
15877 if (cp_lexer_next_token_is (parser->lexer,
15879 cp_lexer_consume_token (parser->lexer);
15883 if (declares_class_or_enum & 2)
15884 cp_parser_check_for_definition_in_return_type
15885 (declarator, decl_specifiers.type,
15886 decl_specifiers.type_location);
15888 /* Look for an asm-specification. */
15889 asm_specification = cp_parser_asm_specification_opt (parser);
15890 /* Look for attributes that apply to the declaration. */
15891 attributes = cp_parser_attributes_opt (parser);
15892 /* Remember which attributes are prefix attributes and
15894 first_attribute = attributes;
15895 /* Combine the attributes. */
15896 attributes = chainon (prefix_attributes, attributes);
15898 /* If it's an `=', then we have a constant-initializer or a
15899 pure-specifier. It is not correct to parse the
15900 initializer before registering the member declaration
15901 since the member declaration should be in scope while
15902 its initializer is processed. However, the rest of the
15903 front end does not yet provide an interface that allows
15904 us to handle this correctly. */
15905 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15909 A pure-specifier shall be used only in the declaration of
15910 a virtual function.
15912 A member-declarator can contain a constant-initializer
15913 only if it declares a static member of integral or
15916 Therefore, if the DECLARATOR is for a function, we look
15917 for a pure-specifier; otherwise, we look for a
15918 constant-initializer. When we call `grokfield', it will
15919 perform more stringent semantics checks. */
15920 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15921 if (function_declarator_p (declarator))
15922 initializer = cp_parser_pure_specifier (parser);
15924 /* Parse the initializer. */
15925 initializer = cp_parser_constant_initializer (parser);
15927 /* Otherwise, there is no initializer. */
15929 initializer = NULL_TREE;
15931 /* See if we are probably looking at a function
15932 definition. We are certainly not looking at a
15933 member-declarator. Calling `grokfield' has
15934 side-effects, so we must not do it unless we are sure
15935 that we are looking at a member-declarator. */
15936 if (cp_parser_token_starts_function_definition_p
15937 (cp_lexer_peek_token (parser->lexer)))
15939 /* The grammar does not allow a pure-specifier to be
15940 used when a member function is defined. (It is
15941 possible that this fact is an oversight in the
15942 standard, since a pure function may be defined
15943 outside of the class-specifier. */
15945 error ("%Hpure-specifier on function-definition",
15946 &initializer_token_start->location);
15947 decl = cp_parser_save_member_function_body (parser,
15951 /* If the member was not a friend, declare it here. */
15953 finish_member_declaration (decl);
15954 /* Peek at the next token. */
15955 token = cp_lexer_peek_token (parser->lexer);
15956 /* If the next token is a semicolon, consume it. */
15957 if (token->type == CPP_SEMICOLON)
15958 cp_lexer_consume_token (parser->lexer);
15962 if (declarator->kind == cdk_function)
15963 declarator->id_loc = token->location;
15964 /* Create the declaration. */
15965 decl = grokfield (declarator, &decl_specifiers,
15966 initializer, /*init_const_expr_p=*/true,
15971 /* Reset PREFIX_ATTRIBUTES. */
15972 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15973 attributes = TREE_CHAIN (attributes);
15975 TREE_CHAIN (attributes) = NULL_TREE;
15977 /* If there is any qualification still in effect, clear it
15978 now; we will be starting fresh with the next declarator. */
15979 parser->scope = NULL_TREE;
15980 parser->qualifying_scope = NULL_TREE;
15981 parser->object_scope = NULL_TREE;
15982 /* If it's a `,', then there are more declarators. */
15983 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15984 cp_lexer_consume_token (parser->lexer);
15985 /* If the next token isn't a `;', then we have a parse error. */
15986 else if (cp_lexer_next_token_is_not (parser->lexer,
15989 cp_parser_error (parser, "expected %<;%>");
15990 /* Skip tokens until we find a `;'. */
15991 cp_parser_skip_to_end_of_statement (parser);
15998 /* Add DECL to the list of members. */
16000 finish_member_declaration (decl);
16002 if (TREE_CODE (decl) == FUNCTION_DECL)
16003 cp_parser_save_default_args (parser, decl);
16008 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16011 /* Parse a pure-specifier.
16016 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16017 Otherwise, ERROR_MARK_NODE is returned. */
16020 cp_parser_pure_specifier (cp_parser* parser)
16024 /* Look for the `=' token. */
16025 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16026 return error_mark_node;
16027 /* Look for the `0' token. */
16028 token = cp_lexer_peek_token (parser->lexer);
16030 if (token->type == CPP_EOF
16031 || token->type == CPP_PRAGMA_EOL)
16032 return error_mark_node;
16034 cp_lexer_consume_token (parser->lexer);
16036 /* Accept = default or = delete in c++0x mode. */
16037 if (token->keyword == RID_DEFAULT
16038 || token->keyword == RID_DELETE)
16040 maybe_warn_cpp0x ("defaulted and deleted functions");
16041 return token->u.value;
16044 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16045 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16047 cp_parser_error (parser,
16048 "invalid pure specifier (only %<= 0%> is allowed)");
16049 cp_parser_skip_to_end_of_statement (parser);
16050 return error_mark_node;
16052 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16054 error ("%Htemplates may not be %<virtual%>", &token->location);
16055 return error_mark_node;
16058 return integer_zero_node;
16061 /* Parse a constant-initializer.
16063 constant-initializer:
16064 = constant-expression
16066 Returns a representation of the constant-expression. */
16069 cp_parser_constant_initializer (cp_parser* parser)
16071 /* Look for the `=' token. */
16072 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16073 return error_mark_node;
16075 /* It is invalid to write:
16077 struct S { static const int i = { 7 }; };
16080 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16082 cp_parser_error (parser,
16083 "a brace-enclosed initializer is not allowed here");
16084 /* Consume the opening brace. */
16085 cp_lexer_consume_token (parser->lexer);
16086 /* Skip the initializer. */
16087 cp_parser_skip_to_closing_brace (parser);
16088 /* Look for the trailing `}'. */
16089 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16091 return error_mark_node;
16094 return cp_parser_constant_expression (parser,
16095 /*allow_non_constant=*/false,
16099 /* Derived classes [gram.class.derived] */
16101 /* Parse a base-clause.
16104 : base-specifier-list
16106 base-specifier-list:
16107 base-specifier ... [opt]
16108 base-specifier-list , base-specifier ... [opt]
16110 Returns a TREE_LIST representing the base-classes, in the order in
16111 which they were declared. The representation of each node is as
16112 described by cp_parser_base_specifier.
16114 In the case that no bases are specified, this function will return
16115 NULL_TREE, not ERROR_MARK_NODE. */
16118 cp_parser_base_clause (cp_parser* parser)
16120 tree bases = NULL_TREE;
16122 /* Look for the `:' that begins the list. */
16123 cp_parser_require (parser, CPP_COLON, "%<:%>");
16125 /* Scan the base-specifier-list. */
16130 bool pack_expansion_p = false;
16132 /* Look for the base-specifier. */
16133 base = cp_parser_base_specifier (parser);
16134 /* Look for the (optional) ellipsis. */
16135 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16137 /* Consume the `...'. */
16138 cp_lexer_consume_token (parser->lexer);
16140 pack_expansion_p = true;
16143 /* Add BASE to the front of the list. */
16144 if (base != error_mark_node)
16146 if (pack_expansion_p)
16147 /* Make this a pack expansion type. */
16148 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16151 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16153 TREE_CHAIN (base) = bases;
16157 /* Peek at the next token. */
16158 token = cp_lexer_peek_token (parser->lexer);
16159 /* If it's not a comma, then the list is complete. */
16160 if (token->type != CPP_COMMA)
16162 /* Consume the `,'. */
16163 cp_lexer_consume_token (parser->lexer);
16166 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16167 base class had a qualified name. However, the next name that
16168 appears is certainly not qualified. */
16169 parser->scope = NULL_TREE;
16170 parser->qualifying_scope = NULL_TREE;
16171 parser->object_scope = NULL_TREE;
16173 return nreverse (bases);
16176 /* Parse a base-specifier.
16179 :: [opt] nested-name-specifier [opt] class-name
16180 virtual access-specifier [opt] :: [opt] nested-name-specifier
16182 access-specifier virtual [opt] :: [opt] nested-name-specifier
16185 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16186 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16187 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16188 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16191 cp_parser_base_specifier (cp_parser* parser)
16195 bool virtual_p = false;
16196 bool duplicate_virtual_error_issued_p = false;
16197 bool duplicate_access_error_issued_p = false;
16198 bool class_scope_p, template_p;
16199 tree access = access_default_node;
16202 /* Process the optional `virtual' and `access-specifier'. */
16205 /* Peek at the next token. */
16206 token = cp_lexer_peek_token (parser->lexer);
16207 /* Process `virtual'. */
16208 switch (token->keyword)
16211 /* If `virtual' appears more than once, issue an error. */
16212 if (virtual_p && !duplicate_virtual_error_issued_p)
16214 cp_parser_error (parser,
16215 "%<virtual%> specified more than once in base-specified");
16216 duplicate_virtual_error_issued_p = true;
16221 /* Consume the `virtual' token. */
16222 cp_lexer_consume_token (parser->lexer);
16227 case RID_PROTECTED:
16229 /* If more than one access specifier appears, issue an
16231 if (access != access_default_node
16232 && !duplicate_access_error_issued_p)
16234 cp_parser_error (parser,
16235 "more than one access specifier in base-specified");
16236 duplicate_access_error_issued_p = true;
16239 access = ridpointers[(int) token->keyword];
16241 /* Consume the access-specifier. */
16242 cp_lexer_consume_token (parser->lexer);
16251 /* It is not uncommon to see programs mechanically, erroneously, use
16252 the 'typename' keyword to denote (dependent) qualified types
16253 as base classes. */
16254 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16256 token = cp_lexer_peek_token (parser->lexer);
16257 if (!processing_template_decl)
16258 error ("%Hkeyword %<typename%> not allowed outside of templates",
16261 error ("%Hkeyword %<typename%> not allowed in this context "
16262 "(the base class is implicitly a type)",
16264 cp_lexer_consume_token (parser->lexer);
16267 /* Look for the optional `::' operator. */
16268 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16269 /* Look for the nested-name-specifier. The simplest way to
16274 The keyword `typename' is not permitted in a base-specifier or
16275 mem-initializer; in these contexts a qualified name that
16276 depends on a template-parameter is implicitly assumed to be a
16279 is to pretend that we have seen the `typename' keyword at this
16281 cp_parser_nested_name_specifier_opt (parser,
16282 /*typename_keyword_p=*/true,
16283 /*check_dependency_p=*/true,
16285 /*is_declaration=*/true);
16286 /* If the base class is given by a qualified name, assume that names
16287 we see are type names or templates, as appropriate. */
16288 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16289 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16291 /* Finally, look for the class-name. */
16292 type = cp_parser_class_name (parser,
16296 /*check_dependency_p=*/true,
16297 /*class_head_p=*/false,
16298 /*is_declaration=*/true);
16300 if (type == error_mark_node)
16301 return error_mark_node;
16303 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16306 /* Exception handling [gram.exception] */
16308 /* Parse an (optional) exception-specification.
16310 exception-specification:
16311 throw ( type-id-list [opt] )
16313 Returns a TREE_LIST representing the exception-specification. The
16314 TREE_VALUE of each node is a type. */
16317 cp_parser_exception_specification_opt (cp_parser* parser)
16322 /* Peek at the next token. */
16323 token = cp_lexer_peek_token (parser->lexer);
16324 /* If it's not `throw', then there's no exception-specification. */
16325 if (!cp_parser_is_keyword (token, RID_THROW))
16328 /* Consume the `throw'. */
16329 cp_lexer_consume_token (parser->lexer);
16331 /* Look for the `('. */
16332 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16334 /* Peek at the next token. */
16335 token = cp_lexer_peek_token (parser->lexer);
16336 /* If it's not a `)', then there is a type-id-list. */
16337 if (token->type != CPP_CLOSE_PAREN)
16339 const char *saved_message;
16341 /* Types may not be defined in an exception-specification. */
16342 saved_message = parser->type_definition_forbidden_message;
16343 parser->type_definition_forbidden_message
16344 = "types may not be defined in an exception-specification";
16345 /* Parse the type-id-list. */
16346 type_id_list = cp_parser_type_id_list (parser);
16347 /* Restore the saved message. */
16348 parser->type_definition_forbidden_message = saved_message;
16351 type_id_list = empty_except_spec;
16353 /* Look for the `)'. */
16354 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16356 return type_id_list;
16359 /* Parse an (optional) type-id-list.
16363 type-id-list , type-id ... [opt]
16365 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16366 in the order that the types were presented. */
16369 cp_parser_type_id_list (cp_parser* parser)
16371 tree types = NULL_TREE;
16378 /* Get the next type-id. */
16379 type = cp_parser_type_id (parser);
16380 /* Parse the optional ellipsis. */
16381 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16383 /* Consume the `...'. */
16384 cp_lexer_consume_token (parser->lexer);
16386 /* Turn the type into a pack expansion expression. */
16387 type = make_pack_expansion (type);
16389 /* Add it to the list. */
16390 types = add_exception_specifier (types, type, /*complain=*/1);
16391 /* Peek at the next token. */
16392 token = cp_lexer_peek_token (parser->lexer);
16393 /* If it is not a `,', we are done. */
16394 if (token->type != CPP_COMMA)
16396 /* Consume the `,'. */
16397 cp_lexer_consume_token (parser->lexer);
16400 return nreverse (types);
16403 /* Parse a try-block.
16406 try compound-statement handler-seq */
16409 cp_parser_try_block (cp_parser* parser)
16413 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16414 try_block = begin_try_block ();
16415 cp_parser_compound_statement (parser, NULL, true);
16416 finish_try_block (try_block);
16417 cp_parser_handler_seq (parser);
16418 finish_handler_sequence (try_block);
16423 /* Parse a function-try-block.
16425 function-try-block:
16426 try ctor-initializer [opt] function-body handler-seq */
16429 cp_parser_function_try_block (cp_parser* parser)
16431 tree compound_stmt;
16433 bool ctor_initializer_p;
16435 /* Look for the `try' keyword. */
16436 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16438 /* Let the rest of the front end know where we are. */
16439 try_block = begin_function_try_block (&compound_stmt);
16440 /* Parse the function-body. */
16442 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16443 /* We're done with the `try' part. */
16444 finish_function_try_block (try_block);
16445 /* Parse the handlers. */
16446 cp_parser_handler_seq (parser);
16447 /* We're done with the handlers. */
16448 finish_function_handler_sequence (try_block, compound_stmt);
16450 return ctor_initializer_p;
16453 /* Parse a handler-seq.
16456 handler handler-seq [opt] */
16459 cp_parser_handler_seq (cp_parser* parser)
16465 /* Parse the handler. */
16466 cp_parser_handler (parser);
16467 /* Peek at the next token. */
16468 token = cp_lexer_peek_token (parser->lexer);
16469 /* If it's not `catch' then there are no more handlers. */
16470 if (!cp_parser_is_keyword (token, RID_CATCH))
16475 /* Parse a handler.
16478 catch ( exception-declaration ) compound-statement */
16481 cp_parser_handler (cp_parser* parser)
16486 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16487 handler = begin_handler ();
16488 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16489 declaration = cp_parser_exception_declaration (parser);
16490 finish_handler_parms (declaration, handler);
16491 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16492 cp_parser_compound_statement (parser, NULL, false);
16493 finish_handler (handler);
16496 /* Parse an exception-declaration.
16498 exception-declaration:
16499 type-specifier-seq declarator
16500 type-specifier-seq abstract-declarator
16504 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16505 ellipsis variant is used. */
16508 cp_parser_exception_declaration (cp_parser* parser)
16510 cp_decl_specifier_seq type_specifiers;
16511 cp_declarator *declarator;
16512 const char *saved_message;
16514 /* If it's an ellipsis, it's easy to handle. */
16515 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16517 /* Consume the `...' token. */
16518 cp_lexer_consume_token (parser->lexer);
16522 /* Types may not be defined in exception-declarations. */
16523 saved_message = parser->type_definition_forbidden_message;
16524 parser->type_definition_forbidden_message
16525 = "types may not be defined in exception-declarations";
16527 /* Parse the type-specifier-seq. */
16528 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16530 /* If it's a `)', then there is no declarator. */
16531 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16534 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16535 /*ctor_dtor_or_conv_p=*/NULL,
16536 /*parenthesized_p=*/NULL,
16537 /*member_p=*/false);
16539 /* Restore the saved message. */
16540 parser->type_definition_forbidden_message = saved_message;
16542 if (!type_specifiers.any_specifiers_p)
16543 return error_mark_node;
16545 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16548 /* Parse a throw-expression.
16551 throw assignment-expression [opt]
16553 Returns a THROW_EXPR representing the throw-expression. */
16556 cp_parser_throw_expression (cp_parser* parser)
16561 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16562 token = cp_lexer_peek_token (parser->lexer);
16563 /* Figure out whether or not there is an assignment-expression
16564 following the "throw" keyword. */
16565 if (token->type == CPP_COMMA
16566 || token->type == CPP_SEMICOLON
16567 || token->type == CPP_CLOSE_PAREN
16568 || token->type == CPP_CLOSE_SQUARE
16569 || token->type == CPP_CLOSE_BRACE
16570 || token->type == CPP_COLON)
16571 expression = NULL_TREE;
16573 expression = cp_parser_assignment_expression (parser,
16574 /*cast_p=*/false, NULL);
16576 return build_throw (expression);
16579 /* GNU Extensions */
16581 /* Parse an (optional) asm-specification.
16584 asm ( string-literal )
16586 If the asm-specification is present, returns a STRING_CST
16587 corresponding to the string-literal. Otherwise, returns
16591 cp_parser_asm_specification_opt (cp_parser* parser)
16594 tree asm_specification;
16596 /* Peek at the next token. */
16597 token = cp_lexer_peek_token (parser->lexer);
16598 /* If the next token isn't the `asm' keyword, then there's no
16599 asm-specification. */
16600 if (!cp_parser_is_keyword (token, RID_ASM))
16603 /* Consume the `asm' token. */
16604 cp_lexer_consume_token (parser->lexer);
16605 /* Look for the `('. */
16606 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16608 /* Look for the string-literal. */
16609 asm_specification = cp_parser_string_literal (parser, false, false);
16611 /* Look for the `)'. */
16612 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16614 return asm_specification;
16617 /* Parse an asm-operand-list.
16621 asm-operand-list , asm-operand
16624 string-literal ( expression )
16625 [ string-literal ] string-literal ( expression )
16627 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16628 each node is the expression. The TREE_PURPOSE is itself a
16629 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16630 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16631 is a STRING_CST for the string literal before the parenthesis. Returns
16632 ERROR_MARK_NODE if any of the operands are invalid. */
16635 cp_parser_asm_operand_list (cp_parser* parser)
16637 tree asm_operands = NULL_TREE;
16638 bool invalid_operands = false;
16642 tree string_literal;
16646 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16648 /* Consume the `[' token. */
16649 cp_lexer_consume_token (parser->lexer);
16650 /* Read the operand name. */
16651 name = cp_parser_identifier (parser);
16652 if (name != error_mark_node)
16653 name = build_string (IDENTIFIER_LENGTH (name),
16654 IDENTIFIER_POINTER (name));
16655 /* Look for the closing `]'. */
16656 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16660 /* Look for the string-literal. */
16661 string_literal = cp_parser_string_literal (parser, false, false);
16663 /* Look for the `('. */
16664 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16665 /* Parse the expression. */
16666 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16667 /* Look for the `)'. */
16668 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16670 if (name == error_mark_node
16671 || string_literal == error_mark_node
16672 || expression == error_mark_node)
16673 invalid_operands = true;
16675 /* Add this operand to the list. */
16676 asm_operands = tree_cons (build_tree_list (name, string_literal),
16679 /* If the next token is not a `,', there are no more
16681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16683 /* Consume the `,'. */
16684 cp_lexer_consume_token (parser->lexer);
16687 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16690 /* Parse an asm-clobber-list.
16694 asm-clobber-list , string-literal
16696 Returns a TREE_LIST, indicating the clobbers in the order that they
16697 appeared. The TREE_VALUE of each node is a STRING_CST. */
16700 cp_parser_asm_clobber_list (cp_parser* parser)
16702 tree clobbers = NULL_TREE;
16706 tree string_literal;
16708 /* Look for the string literal. */
16709 string_literal = cp_parser_string_literal (parser, false, false);
16710 /* Add it to the list. */
16711 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16712 /* If the next token is not a `,', then the list is
16714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16716 /* Consume the `,' token. */
16717 cp_lexer_consume_token (parser->lexer);
16723 /* Parse an (optional) series of attributes.
16726 attributes attribute
16729 __attribute__ (( attribute-list [opt] ))
16731 The return value is as for cp_parser_attribute_list. */
16734 cp_parser_attributes_opt (cp_parser* parser)
16736 tree attributes = NULL_TREE;
16741 tree attribute_list;
16743 /* Peek at the next token. */
16744 token = cp_lexer_peek_token (parser->lexer);
16745 /* If it's not `__attribute__', then we're done. */
16746 if (token->keyword != RID_ATTRIBUTE)
16749 /* Consume the `__attribute__' keyword. */
16750 cp_lexer_consume_token (parser->lexer);
16751 /* Look for the two `(' tokens. */
16752 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16753 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16755 /* Peek at the next token. */
16756 token = cp_lexer_peek_token (parser->lexer);
16757 if (token->type != CPP_CLOSE_PAREN)
16758 /* Parse the attribute-list. */
16759 attribute_list = cp_parser_attribute_list (parser);
16761 /* If the next token is a `)', then there is no attribute
16763 attribute_list = NULL;
16765 /* Look for the two `)' tokens. */
16766 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16767 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16769 /* Add these new attributes to the list. */
16770 attributes = chainon (attributes, attribute_list);
16776 /* Parse an attribute-list.
16780 attribute-list , attribute
16784 identifier ( identifier )
16785 identifier ( identifier , expression-list )
16786 identifier ( expression-list )
16788 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16789 to an attribute. The TREE_PURPOSE of each node is the identifier
16790 indicating which attribute is in use. The TREE_VALUE represents
16791 the arguments, if any. */
16794 cp_parser_attribute_list (cp_parser* parser)
16796 tree attribute_list = NULL_TREE;
16797 bool save_translate_strings_p = parser->translate_strings_p;
16799 parser->translate_strings_p = false;
16806 /* Look for the identifier. We also allow keywords here; for
16807 example `__attribute__ ((const))' is legal. */
16808 token = cp_lexer_peek_token (parser->lexer);
16809 if (token->type == CPP_NAME
16810 || token->type == CPP_KEYWORD)
16812 tree arguments = NULL_TREE;
16814 /* Consume the token. */
16815 token = cp_lexer_consume_token (parser->lexer);
16817 /* Save away the identifier that indicates which attribute
16819 identifier = token->u.value;
16820 attribute = build_tree_list (identifier, NULL_TREE);
16822 /* Peek at the next token. */
16823 token = cp_lexer_peek_token (parser->lexer);
16824 /* If it's an `(', then parse the attribute arguments. */
16825 if (token->type == CPP_OPEN_PAREN)
16827 arguments = cp_parser_parenthesized_expression_list
16828 (parser, true, /*cast_p=*/false,
16829 /*allow_expansion_p=*/false,
16830 /*non_constant_p=*/NULL);
16831 /* Save the arguments away. */
16832 TREE_VALUE (attribute) = arguments;
16835 if (arguments != error_mark_node)
16837 /* Add this attribute to the list. */
16838 TREE_CHAIN (attribute) = attribute_list;
16839 attribute_list = attribute;
16842 token = cp_lexer_peek_token (parser->lexer);
16844 /* Now, look for more attributes. If the next token isn't a
16845 `,', we're done. */
16846 if (token->type != CPP_COMMA)
16849 /* Consume the comma and keep going. */
16850 cp_lexer_consume_token (parser->lexer);
16852 parser->translate_strings_p = save_translate_strings_p;
16854 /* We built up the list in reverse order. */
16855 return nreverse (attribute_list);
16858 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16859 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16860 current value of the PEDANTIC flag, regardless of whether or not
16861 the `__extension__' keyword is present. The caller is responsible
16862 for restoring the value of the PEDANTIC flag. */
16865 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16867 /* Save the old value of the PEDANTIC flag. */
16868 *saved_pedantic = pedantic;
16870 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16872 /* Consume the `__extension__' token. */
16873 cp_lexer_consume_token (parser->lexer);
16874 /* We're not being pedantic while the `__extension__' keyword is
16884 /* Parse a label declaration.
16887 __label__ label-declarator-seq ;
16889 label-declarator-seq:
16890 identifier , label-declarator-seq
16894 cp_parser_label_declaration (cp_parser* parser)
16896 /* Look for the `__label__' keyword. */
16897 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16903 /* Look for an identifier. */
16904 identifier = cp_parser_identifier (parser);
16905 /* If we failed, stop. */
16906 if (identifier == error_mark_node)
16908 /* Declare it as a label. */
16909 finish_label_decl (identifier);
16910 /* If the next token is a `;', stop. */
16911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16913 /* Look for the `,' separating the label declarations. */
16914 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16917 /* Look for the final `;'. */
16918 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16921 /* Support Functions */
16923 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16924 NAME should have one of the representations used for an
16925 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16926 is returned. If PARSER->SCOPE is a dependent type, then a
16927 SCOPE_REF is returned.
16929 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16930 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16931 was formed. Abstractly, such entities should not be passed to this
16932 function, because they do not need to be looked up, but it is
16933 simpler to check for this special case here, rather than at the
16936 In cases not explicitly covered above, this function returns a
16937 DECL, OVERLOAD, or baselink representing the result of the lookup.
16938 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16941 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16942 (e.g., "struct") that was used. In that case bindings that do not
16943 refer to types are ignored.
16945 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16948 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16951 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16954 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16955 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16956 NULL_TREE otherwise. */
16959 cp_parser_lookup_name (cp_parser *parser, tree name,
16960 enum tag_types tag_type,
16963 bool check_dependency,
16964 tree *ambiguous_decls,
16965 location_t name_location)
16969 tree object_type = parser->context->object_type;
16971 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16972 flags |= LOOKUP_COMPLAIN;
16974 /* Assume that the lookup will be unambiguous. */
16975 if (ambiguous_decls)
16976 *ambiguous_decls = NULL_TREE;
16978 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16979 no longer valid. Note that if we are parsing tentatively, and
16980 the parse fails, OBJECT_TYPE will be automatically restored. */
16981 parser->context->object_type = NULL_TREE;
16983 if (name == error_mark_node)
16984 return error_mark_node;
16986 /* A template-id has already been resolved; there is no lookup to
16988 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16990 if (BASELINK_P (name))
16992 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16993 == TEMPLATE_ID_EXPR);
16997 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16998 it should already have been checked to make sure that the name
16999 used matches the type being destroyed. */
17000 if (TREE_CODE (name) == BIT_NOT_EXPR)
17004 /* Figure out to which type this destructor applies. */
17006 type = parser->scope;
17007 else if (object_type)
17008 type = object_type;
17010 type = current_class_type;
17011 /* If that's not a class type, there is no destructor. */
17012 if (!type || !CLASS_TYPE_P (type))
17013 return error_mark_node;
17014 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17015 lazily_declare_fn (sfk_destructor, type);
17016 if (!CLASSTYPE_DESTRUCTORS (type))
17017 return error_mark_node;
17018 /* If it was a class type, return the destructor. */
17019 return CLASSTYPE_DESTRUCTORS (type);
17022 /* By this point, the NAME should be an ordinary identifier. If
17023 the id-expression was a qualified name, the qualifying scope is
17024 stored in PARSER->SCOPE at this point. */
17025 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17027 /* Perform the lookup. */
17032 if (parser->scope == error_mark_node)
17033 return error_mark_node;
17035 /* If the SCOPE is dependent, the lookup must be deferred until
17036 the template is instantiated -- unless we are explicitly
17037 looking up names in uninstantiated templates. Even then, we
17038 cannot look up the name if the scope is not a class type; it
17039 might, for example, be a template type parameter. */
17040 dependent_p = (TYPE_P (parser->scope)
17041 && dependent_scope_p (parser->scope));
17042 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17044 /* Defer lookup. */
17045 decl = error_mark_node;
17048 tree pushed_scope = NULL_TREE;
17050 /* If PARSER->SCOPE is a dependent type, then it must be a
17051 class type, and we must not be checking dependencies;
17052 otherwise, we would have processed this lookup above. So
17053 that PARSER->SCOPE is not considered a dependent base by
17054 lookup_member, we must enter the scope here. */
17056 pushed_scope = push_scope (parser->scope);
17057 /* If the PARSER->SCOPE is a template specialization, it
17058 may be instantiated during name lookup. In that case,
17059 errors may be issued. Even if we rollback the current
17060 tentative parse, those errors are valid. */
17061 decl = lookup_qualified_name (parser->scope, name,
17062 tag_type != none_type,
17063 /*complain=*/true);
17065 /* If we have a single function from a using decl, pull it out. */
17066 if (TREE_CODE (decl) == OVERLOAD
17067 && !really_overloaded_fn (decl))
17068 decl = OVL_FUNCTION (decl);
17071 pop_scope (pushed_scope);
17074 /* If the scope is a dependent type and either we deferred lookup or
17075 we did lookup but didn't find the name, rememeber the name. */
17076 if (decl == error_mark_node && TYPE_P (parser->scope)
17077 && dependent_type_p (parser->scope))
17083 /* The resolution to Core Issue 180 says that `struct
17084 A::B' should be considered a type-name, even if `A'
17086 type = make_typename_type (parser->scope, name, tag_type,
17087 /*complain=*/tf_error);
17088 decl = TYPE_NAME (type);
17090 else if (is_template
17091 && (cp_parser_next_token_ends_template_argument_p (parser)
17092 || cp_lexer_next_token_is (parser->lexer,
17094 decl = make_unbound_class_template (parser->scope,
17096 /*complain=*/tf_error);
17098 decl = build_qualified_name (/*type=*/NULL_TREE,
17099 parser->scope, name,
17102 parser->qualifying_scope = parser->scope;
17103 parser->object_scope = NULL_TREE;
17105 else if (object_type)
17107 tree object_decl = NULL_TREE;
17108 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17109 OBJECT_TYPE is not a class. */
17110 if (CLASS_TYPE_P (object_type))
17111 /* If the OBJECT_TYPE is a template specialization, it may
17112 be instantiated during name lookup. In that case, errors
17113 may be issued. Even if we rollback the current tentative
17114 parse, those errors are valid. */
17115 object_decl = lookup_member (object_type,
17118 tag_type != none_type);
17119 /* Look it up in the enclosing context, too. */
17120 decl = lookup_name_real (name, tag_type != none_type,
17122 /*block_p=*/true, is_namespace, flags);
17123 parser->object_scope = object_type;
17124 parser->qualifying_scope = NULL_TREE;
17126 decl = object_decl;
17130 decl = lookup_name_real (name, tag_type != none_type,
17132 /*block_p=*/true, is_namespace, flags);
17133 parser->qualifying_scope = NULL_TREE;
17134 parser->object_scope = NULL_TREE;
17137 /* If the lookup failed, let our caller know. */
17138 if (!decl || decl == error_mark_node)
17139 return error_mark_node;
17141 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17142 if (TREE_CODE (decl) == TREE_LIST)
17144 if (ambiguous_decls)
17145 *ambiguous_decls = decl;
17146 /* The error message we have to print is too complicated for
17147 cp_parser_error, so we incorporate its actions directly. */
17148 if (!cp_parser_simulate_error (parser))
17150 error ("%Hreference to %qD is ambiguous",
17151 &name_location, name);
17152 print_candidates (decl);
17154 return error_mark_node;
17157 gcc_assert (DECL_P (decl)
17158 || TREE_CODE (decl) == OVERLOAD
17159 || TREE_CODE (decl) == SCOPE_REF
17160 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17161 || BASELINK_P (decl));
17163 /* If we have resolved the name of a member declaration, check to
17164 see if the declaration is accessible. When the name resolves to
17165 set of overloaded functions, accessibility is checked when
17166 overload resolution is done.
17168 During an explicit instantiation, access is not checked at all,
17169 as per [temp.explicit]. */
17171 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17176 /* Like cp_parser_lookup_name, but for use in the typical case where
17177 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17178 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17181 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17183 return cp_parser_lookup_name (parser, name,
17185 /*is_template=*/false,
17186 /*is_namespace=*/false,
17187 /*check_dependency=*/true,
17188 /*ambiguous_decls=*/NULL,
17192 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17193 the current context, return the TYPE_DECL. If TAG_NAME_P is
17194 true, the DECL indicates the class being defined in a class-head,
17195 or declared in an elaborated-type-specifier.
17197 Otherwise, return DECL. */
17200 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17202 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17203 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17206 template <typename T> struct B;
17209 template <typename T> struct A::B {};
17211 Similarly, in an elaborated-type-specifier:
17213 namespace N { struct X{}; }
17216 template <typename T> friend struct N::X;
17219 However, if the DECL refers to a class type, and we are in
17220 the scope of the class, then the name lookup automatically
17221 finds the TYPE_DECL created by build_self_reference rather
17222 than a TEMPLATE_DECL. For example, in:
17224 template <class T> struct S {
17228 there is no need to handle such case. */
17230 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17231 return DECL_TEMPLATE_RESULT (decl);
17236 /* If too many, or too few, template-parameter lists apply to the
17237 declarator, issue an error message. Returns TRUE if all went well,
17238 and FALSE otherwise. */
17241 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17242 cp_declarator *declarator,
17243 location_t declarator_location)
17245 unsigned num_templates;
17247 /* We haven't seen any classes that involve template parameters yet. */
17250 switch (declarator->kind)
17253 if (declarator->u.id.qualifying_scope)
17258 scope = declarator->u.id.qualifying_scope;
17259 member = declarator->u.id.unqualified_name;
17261 while (scope && CLASS_TYPE_P (scope))
17263 /* You're supposed to have one `template <...>'
17264 for every template class, but you don't need one
17265 for a full specialization. For example:
17267 template <class T> struct S{};
17268 template <> struct S<int> { void f(); };
17269 void S<int>::f () {}
17271 is correct; there shouldn't be a `template <>' for
17272 the definition of `S<int>::f'. */
17273 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17274 /* If SCOPE does not have template information of any
17275 kind, then it is not a template, nor is it nested
17276 within a template. */
17278 if (explicit_class_specialization_p (scope))
17280 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17283 scope = TYPE_CONTEXT (scope);
17286 else if (TREE_CODE (declarator->u.id.unqualified_name)
17287 == TEMPLATE_ID_EXPR)
17288 /* If the DECLARATOR has the form `X<y>' then it uses one
17289 additional level of template parameters. */
17292 return cp_parser_check_template_parameters (parser,
17294 declarator_location);
17299 case cdk_reference:
17301 return (cp_parser_check_declarator_template_parameters
17302 (parser, declarator->declarator, declarator_location));
17308 gcc_unreachable ();
17313 /* NUM_TEMPLATES were used in the current declaration. If that is
17314 invalid, return FALSE and issue an error messages. Otherwise,
17318 cp_parser_check_template_parameters (cp_parser* parser,
17319 unsigned num_templates,
17320 location_t location)
17322 /* If there are more template classes than parameter lists, we have
17325 template <class T> void S<T>::R<T>::f (); */
17326 if (parser->num_template_parameter_lists < num_templates)
17328 error ("%Htoo few template-parameter-lists", &location);
17331 /* If there are the same number of template classes and parameter
17332 lists, that's OK. */
17333 if (parser->num_template_parameter_lists == num_templates)
17335 /* If there are more, but only one more, then we are referring to a
17336 member template. That's OK too. */
17337 if (parser->num_template_parameter_lists == num_templates + 1)
17339 /* Otherwise, there are too many template parameter lists. We have
17342 template <class T> template <class U> void S::f(); */
17343 error ("%Htoo many template-parameter-lists", &location);
17347 /* Parse an optional `::' token indicating that the following name is
17348 from the global namespace. If so, PARSER->SCOPE is set to the
17349 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17350 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17351 Returns the new value of PARSER->SCOPE, if the `::' token is
17352 present, and NULL_TREE otherwise. */
17355 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17359 /* Peek at the next token. */
17360 token = cp_lexer_peek_token (parser->lexer);
17361 /* If we're looking at a `::' token then we're starting from the
17362 global namespace, not our current location. */
17363 if (token->type == CPP_SCOPE)
17365 /* Consume the `::' token. */
17366 cp_lexer_consume_token (parser->lexer);
17367 /* Set the SCOPE so that we know where to start the lookup. */
17368 parser->scope = global_namespace;
17369 parser->qualifying_scope = global_namespace;
17370 parser->object_scope = NULL_TREE;
17372 return parser->scope;
17374 else if (!current_scope_valid_p)
17376 parser->scope = NULL_TREE;
17377 parser->qualifying_scope = NULL_TREE;
17378 parser->object_scope = NULL_TREE;
17384 /* Returns TRUE if the upcoming token sequence is the start of a
17385 constructor declarator. If FRIEND_P is true, the declarator is
17386 preceded by the `friend' specifier. */
17389 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17391 bool constructor_p;
17392 tree type_decl = NULL_TREE;
17393 bool nested_name_p;
17394 cp_token *next_token;
17396 /* The common case is that this is not a constructor declarator, so
17397 try to avoid doing lots of work if at all possible. It's not
17398 valid declare a constructor at function scope. */
17399 if (parser->in_function_body)
17401 /* And only certain tokens can begin a constructor declarator. */
17402 next_token = cp_lexer_peek_token (parser->lexer);
17403 if (next_token->type != CPP_NAME
17404 && next_token->type != CPP_SCOPE
17405 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17406 && next_token->type != CPP_TEMPLATE_ID)
17409 /* Parse tentatively; we are going to roll back all of the tokens
17411 cp_parser_parse_tentatively (parser);
17412 /* Assume that we are looking at a constructor declarator. */
17413 constructor_p = true;
17415 /* Look for the optional `::' operator. */
17416 cp_parser_global_scope_opt (parser,
17417 /*current_scope_valid_p=*/false);
17418 /* Look for the nested-name-specifier. */
17420 = (cp_parser_nested_name_specifier_opt (parser,
17421 /*typename_keyword_p=*/false,
17422 /*check_dependency_p=*/false,
17424 /*is_declaration=*/false)
17426 /* Outside of a class-specifier, there must be a
17427 nested-name-specifier. */
17428 if (!nested_name_p &&
17429 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17431 constructor_p = false;
17432 /* If we still think that this might be a constructor-declarator,
17433 look for a class-name. */
17438 template <typename T> struct S { S(); };
17439 template <typename T> S<T>::S ();
17441 we must recognize that the nested `S' names a class.
17444 template <typename T> S<T>::S<T> ();
17446 we must recognize that the nested `S' names a template. */
17447 type_decl = cp_parser_class_name (parser,
17448 /*typename_keyword_p=*/false,
17449 /*template_keyword_p=*/false,
17451 /*check_dependency_p=*/false,
17452 /*class_head_p=*/false,
17453 /*is_declaration=*/false);
17454 /* If there was no class-name, then this is not a constructor. */
17455 constructor_p = !cp_parser_error_occurred (parser);
17458 /* If we're still considering a constructor, we have to see a `(',
17459 to begin the parameter-declaration-clause, followed by either a
17460 `)', an `...', or a decl-specifier. We need to check for a
17461 type-specifier to avoid being fooled into thinking that:
17465 is a constructor. (It is actually a function named `f' that
17466 takes one parameter (of type `int') and returns a value of type
17469 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17472 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17473 /* A parameter declaration begins with a decl-specifier,
17474 which is either the "attribute" keyword, a storage class
17475 specifier, or (usually) a type-specifier. */
17476 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17479 tree pushed_scope = NULL_TREE;
17480 unsigned saved_num_template_parameter_lists;
17482 /* Names appearing in the type-specifier should be looked up
17483 in the scope of the class. */
17484 if (current_class_type)
17488 type = TREE_TYPE (type_decl);
17489 if (TREE_CODE (type) == TYPENAME_TYPE)
17491 type = resolve_typename_type (type,
17492 /*only_current_p=*/false);
17493 if (TREE_CODE (type) == TYPENAME_TYPE)
17495 cp_parser_abort_tentative_parse (parser);
17499 pushed_scope = push_scope (type);
17502 /* Inside the constructor parameter list, surrounding
17503 template-parameter-lists do not apply. */
17504 saved_num_template_parameter_lists
17505 = parser->num_template_parameter_lists;
17506 parser->num_template_parameter_lists = 0;
17508 /* Look for the type-specifier. */
17509 cp_parser_type_specifier (parser,
17510 CP_PARSER_FLAGS_NONE,
17511 /*decl_specs=*/NULL,
17512 /*is_declarator=*/true,
17513 /*declares_class_or_enum=*/NULL,
17514 /*is_cv_qualifier=*/NULL);
17516 parser->num_template_parameter_lists
17517 = saved_num_template_parameter_lists;
17519 /* Leave the scope of the class. */
17521 pop_scope (pushed_scope);
17523 constructor_p = !cp_parser_error_occurred (parser);
17527 constructor_p = false;
17528 /* We did not really want to consume any tokens. */
17529 cp_parser_abort_tentative_parse (parser);
17531 return constructor_p;
17534 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17535 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17536 they must be performed once we are in the scope of the function.
17538 Returns the function defined. */
17541 cp_parser_function_definition_from_specifiers_and_declarator
17542 (cp_parser* parser,
17543 cp_decl_specifier_seq *decl_specifiers,
17545 const cp_declarator *declarator)
17550 /* Begin the function-definition. */
17551 success_p = start_function (decl_specifiers, declarator, attributes);
17553 /* The things we're about to see are not directly qualified by any
17554 template headers we've seen thus far. */
17555 reset_specialization ();
17557 /* If there were names looked up in the decl-specifier-seq that we
17558 did not check, check them now. We must wait until we are in the
17559 scope of the function to perform the checks, since the function
17560 might be a friend. */
17561 perform_deferred_access_checks ();
17565 /* Skip the entire function. */
17566 cp_parser_skip_to_end_of_block_or_statement (parser);
17567 fn = error_mark_node;
17569 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17571 /* Seen already, skip it. An error message has already been output. */
17572 cp_parser_skip_to_end_of_block_or_statement (parser);
17573 fn = current_function_decl;
17574 current_function_decl = NULL_TREE;
17575 /* If this is a function from a class, pop the nested class. */
17576 if (current_class_name)
17577 pop_nested_class ();
17580 fn = cp_parser_function_definition_after_declarator (parser,
17581 /*inline_p=*/false);
17586 /* Parse the part of a function-definition that follows the
17587 declarator. INLINE_P is TRUE iff this function is an inline
17588 function defined with a class-specifier.
17590 Returns the function defined. */
17593 cp_parser_function_definition_after_declarator (cp_parser* parser,
17597 bool ctor_initializer_p = false;
17598 bool saved_in_unbraced_linkage_specification_p;
17599 bool saved_in_function_body;
17600 unsigned saved_num_template_parameter_lists;
17603 saved_in_function_body = parser->in_function_body;
17604 parser->in_function_body = true;
17605 /* If the next token is `return', then the code may be trying to
17606 make use of the "named return value" extension that G++ used to
17608 token = cp_lexer_peek_token (parser->lexer);
17609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17611 /* Consume the `return' keyword. */
17612 cp_lexer_consume_token (parser->lexer);
17613 /* Look for the identifier that indicates what value is to be
17615 cp_parser_identifier (parser);
17616 /* Issue an error message. */
17617 error ("%Hnamed return values are no longer supported",
17619 /* Skip tokens until we reach the start of the function body. */
17622 cp_token *token = cp_lexer_peek_token (parser->lexer);
17623 if (token->type == CPP_OPEN_BRACE
17624 || token->type == CPP_EOF
17625 || token->type == CPP_PRAGMA_EOL)
17627 cp_lexer_consume_token (parser->lexer);
17630 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17631 anything declared inside `f'. */
17632 saved_in_unbraced_linkage_specification_p
17633 = parser->in_unbraced_linkage_specification_p;
17634 parser->in_unbraced_linkage_specification_p = false;
17635 /* Inside the function, surrounding template-parameter-lists do not
17637 saved_num_template_parameter_lists
17638 = parser->num_template_parameter_lists;
17639 parser->num_template_parameter_lists = 0;
17640 /* If the next token is `try', then we are looking at a
17641 function-try-block. */
17642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17643 ctor_initializer_p = cp_parser_function_try_block (parser);
17644 /* A function-try-block includes the function-body, so we only do
17645 this next part if we're not processing a function-try-block. */
17648 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17650 /* Finish the function. */
17651 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17652 (inline_p ? 2 : 0));
17653 /* Generate code for it, if necessary. */
17654 expand_or_defer_fn (fn);
17655 /* Restore the saved values. */
17656 parser->in_unbraced_linkage_specification_p
17657 = saved_in_unbraced_linkage_specification_p;
17658 parser->num_template_parameter_lists
17659 = saved_num_template_parameter_lists;
17660 parser->in_function_body = saved_in_function_body;
17665 /* Parse a template-declaration, assuming that the `export' (and
17666 `extern') keywords, if present, has already been scanned. MEMBER_P
17667 is as for cp_parser_template_declaration. */
17670 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17672 tree decl = NULL_TREE;
17673 VEC (deferred_access_check,gc) *checks;
17674 tree parameter_list;
17675 bool friend_p = false;
17676 bool need_lang_pop;
17679 /* Look for the `template' keyword. */
17680 token = cp_lexer_peek_token (parser->lexer);
17681 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17685 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17687 if (at_class_scope_p () && current_function_decl)
17689 /* 14.5.2.2 [temp.mem]
17691 A local class shall not have member templates. */
17692 error ("%Hinvalid declaration of member template in local class",
17694 cp_parser_skip_to_end_of_block_or_statement (parser);
17699 A template ... shall not have C linkage. */
17700 if (current_lang_name == lang_name_c)
17702 error ("%Htemplate with C linkage", &token->location);
17703 /* Give it C++ linkage to avoid confusing other parts of the
17705 push_lang_context (lang_name_cplusplus);
17706 need_lang_pop = true;
17709 need_lang_pop = false;
17711 /* We cannot perform access checks on the template parameter
17712 declarations until we know what is being declared, just as we
17713 cannot check the decl-specifier list. */
17714 push_deferring_access_checks (dk_deferred);
17716 /* If the next token is `>', then we have an invalid
17717 specialization. Rather than complain about an invalid template
17718 parameter, issue an error message here. */
17719 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17721 cp_parser_error (parser, "invalid explicit specialization");
17722 begin_specialization ();
17723 parameter_list = NULL_TREE;
17726 /* Parse the template parameters. */
17727 parameter_list = cp_parser_template_parameter_list (parser);
17729 /* Get the deferred access checks from the parameter list. These
17730 will be checked once we know what is being declared, as for a
17731 member template the checks must be performed in the scope of the
17732 class containing the member. */
17733 checks = get_deferred_access_checks ();
17735 /* Look for the `>'. */
17736 cp_parser_skip_to_end_of_template_parameter_list (parser);
17737 /* We just processed one more parameter list. */
17738 ++parser->num_template_parameter_lists;
17739 /* If the next token is `template', there are more template
17741 if (cp_lexer_next_token_is_keyword (parser->lexer,
17743 cp_parser_template_declaration_after_export (parser, member_p);
17746 /* There are no access checks when parsing a template, as we do not
17747 know if a specialization will be a friend. */
17748 push_deferring_access_checks (dk_no_check);
17749 token = cp_lexer_peek_token (parser->lexer);
17750 decl = cp_parser_single_declaration (parser,
17753 /*explicit_specialization_p=*/false,
17755 pop_deferring_access_checks ();
17757 /* If this is a member template declaration, let the front
17759 if (member_p && !friend_p && decl)
17761 if (TREE_CODE (decl) == TYPE_DECL)
17762 cp_parser_check_access_in_redeclaration (decl, token->location);
17764 decl = finish_member_template_decl (decl);
17766 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17767 make_friend_class (current_class_type, TREE_TYPE (decl),
17768 /*complain=*/true);
17770 /* We are done with the current parameter list. */
17771 --parser->num_template_parameter_lists;
17773 pop_deferring_access_checks ();
17776 finish_template_decl (parameter_list);
17778 /* Register member declarations. */
17779 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17780 finish_member_declaration (decl);
17781 /* For the erroneous case of a template with C linkage, we pushed an
17782 implicit C++ linkage scope; exit that scope now. */
17784 pop_lang_context ();
17785 /* If DECL is a function template, we must return to parse it later.
17786 (Even though there is no definition, there might be default
17787 arguments that need handling.) */
17788 if (member_p && decl
17789 && (TREE_CODE (decl) == FUNCTION_DECL
17790 || DECL_FUNCTION_TEMPLATE_P (decl)))
17791 TREE_VALUE (parser->unparsed_functions_queues)
17792 = tree_cons (NULL_TREE, decl,
17793 TREE_VALUE (parser->unparsed_functions_queues));
17796 /* Perform the deferred access checks from a template-parameter-list.
17797 CHECKS is a TREE_LIST of access checks, as returned by
17798 get_deferred_access_checks. */
17801 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17803 ++processing_template_parmlist;
17804 perform_access_checks (checks);
17805 --processing_template_parmlist;
17808 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17809 `function-definition' sequence. MEMBER_P is true, this declaration
17810 appears in a class scope.
17812 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17813 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17816 cp_parser_single_declaration (cp_parser* parser,
17817 VEC (deferred_access_check,gc)* checks,
17819 bool explicit_specialization_p,
17822 int declares_class_or_enum;
17823 tree decl = NULL_TREE;
17824 cp_decl_specifier_seq decl_specifiers;
17825 bool function_definition_p = false;
17826 cp_token *decl_spec_token_start;
17828 /* This function is only used when processing a template
17830 gcc_assert (innermost_scope_kind () == sk_template_parms
17831 || innermost_scope_kind () == sk_template_spec);
17833 /* Defer access checks until we know what is being declared. */
17834 push_deferring_access_checks (dk_deferred);
17836 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17838 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17839 cp_parser_decl_specifier_seq (parser,
17840 CP_PARSER_FLAGS_OPTIONAL,
17842 &declares_class_or_enum);
17844 *friend_p = cp_parser_friend_p (&decl_specifiers);
17846 /* There are no template typedefs. */
17847 if (decl_specifiers.specs[(int) ds_typedef])
17849 error ("%Htemplate declaration of %qs",
17850 &decl_spec_token_start->location, "typedef");
17851 decl = error_mark_node;
17854 /* Gather up the access checks that occurred the
17855 decl-specifier-seq. */
17856 stop_deferring_access_checks ();
17858 /* Check for the declaration of a template class. */
17859 if (declares_class_or_enum)
17861 if (cp_parser_declares_only_class_p (parser))
17863 decl = shadow_tag (&decl_specifiers);
17868 friend template <typename T> struct A<T>::B;
17871 A<T>::B will be represented by a TYPENAME_TYPE, and
17872 therefore not recognized by shadow_tag. */
17873 if (friend_p && *friend_p
17875 && decl_specifiers.type
17876 && TYPE_P (decl_specifiers.type))
17877 decl = decl_specifiers.type;
17879 if (decl && decl != error_mark_node)
17880 decl = TYPE_NAME (decl);
17882 decl = error_mark_node;
17884 /* Perform access checks for template parameters. */
17885 cp_parser_perform_template_parameter_access_checks (checks);
17888 /* If it's not a template class, try for a template function. If
17889 the next token is a `;', then this declaration does not declare
17890 anything. But, if there were errors in the decl-specifiers, then
17891 the error might well have come from an attempted class-specifier.
17892 In that case, there's no need to warn about a missing declarator. */
17894 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17895 || decl_specifiers.type != error_mark_node))
17897 decl = cp_parser_init_declarator (parser,
17900 /*function_definition_allowed_p=*/true,
17902 declares_class_or_enum,
17903 &function_definition_p);
17905 /* 7.1.1-1 [dcl.stc]
17907 A storage-class-specifier shall not be specified in an explicit
17908 specialization... */
17910 && explicit_specialization_p
17911 && decl_specifiers.storage_class != sc_none)
17913 error ("%Hexplicit template specialization cannot have a storage class",
17914 &decl_spec_token_start->location);
17915 decl = error_mark_node;
17919 pop_deferring_access_checks ();
17921 /* Clear any current qualification; whatever comes next is the start
17922 of something new. */
17923 parser->scope = NULL_TREE;
17924 parser->qualifying_scope = NULL_TREE;
17925 parser->object_scope = NULL_TREE;
17926 /* Look for a trailing `;' after the declaration. */
17927 if (!function_definition_p
17928 && (decl == error_mark_node
17929 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17930 cp_parser_skip_to_end_of_block_or_statement (parser);
17935 /* Parse a cast-expression that is not the operand of a unary "&". */
17938 cp_parser_simple_cast_expression (cp_parser *parser)
17940 return cp_parser_cast_expression (parser, /*address_p=*/false,
17941 /*cast_p=*/false, NULL);
17944 /* Parse a functional cast to TYPE. Returns an expression
17945 representing the cast. */
17948 cp_parser_functional_cast (cp_parser* parser, tree type)
17950 tree expression_list;
17954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17956 maybe_warn_cpp0x ("extended initializer lists");
17957 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17958 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17959 if (TREE_CODE (type) == TYPE_DECL)
17960 type = TREE_TYPE (type);
17961 return finish_compound_literal (type, expression_list);
17965 = cp_parser_parenthesized_expression_list (parser, false,
17967 /*allow_expansion_p=*/true,
17968 /*non_constant_p=*/NULL);
17970 cast = build_functional_cast (type, expression_list,
17971 tf_warning_or_error);
17972 /* [expr.const]/1: In an integral constant expression "only type
17973 conversions to integral or enumeration type can be used". */
17974 if (TREE_CODE (type) == TYPE_DECL)
17975 type = TREE_TYPE (type);
17976 if (cast != error_mark_node
17977 && !cast_valid_in_integral_constant_expression_p (type)
17978 && (cp_parser_non_integral_constant_expression
17979 (parser, "a call to a constructor")))
17980 return error_mark_node;
17984 /* Save the tokens that make up the body of a member function defined
17985 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17986 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17987 specifiers applied to the declaration. Returns the FUNCTION_DECL
17988 for the member function. */
17991 cp_parser_save_member_function_body (cp_parser* parser,
17992 cp_decl_specifier_seq *decl_specifiers,
17993 cp_declarator *declarator,
18000 /* Create the function-declaration. */
18001 fn = start_method (decl_specifiers, declarator, attributes);
18002 /* If something went badly wrong, bail out now. */
18003 if (fn == error_mark_node)
18005 /* If there's a function-body, skip it. */
18006 if (cp_parser_token_starts_function_definition_p
18007 (cp_lexer_peek_token (parser->lexer)))
18008 cp_parser_skip_to_end_of_block_or_statement (parser);
18009 return error_mark_node;
18012 /* Remember it, if there default args to post process. */
18013 cp_parser_save_default_args (parser, fn);
18015 /* Save away the tokens that make up the body of the
18017 first = parser->lexer->next_token;
18018 /* We can have braced-init-list mem-initializers before the fn body. */
18019 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18021 cp_lexer_consume_token (parser->lexer);
18022 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18023 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18025 /* cache_group will stop after an un-nested { } pair, too. */
18026 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18029 /* variadic mem-inits have ... after the ')'. */
18030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18031 cp_lexer_consume_token (parser->lexer);
18034 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18035 /* Handle function try blocks. */
18036 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18037 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18038 last = parser->lexer->next_token;
18040 /* Save away the inline definition; we will process it when the
18041 class is complete. */
18042 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18043 DECL_PENDING_INLINE_P (fn) = 1;
18045 /* We need to know that this was defined in the class, so that
18046 friend templates are handled correctly. */
18047 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18049 /* We're done with the inline definition. */
18050 finish_method (fn);
18052 /* Add FN to the queue of functions to be parsed later. */
18053 TREE_VALUE (parser->unparsed_functions_queues)
18054 = tree_cons (NULL_TREE, fn,
18055 TREE_VALUE (parser->unparsed_functions_queues));
18060 /* Parse a template-argument-list, as well as the trailing ">" (but
18061 not the opening ">"). See cp_parser_template_argument_list for the
18065 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18069 tree saved_qualifying_scope;
18070 tree saved_object_scope;
18071 bool saved_greater_than_is_operator_p;
18072 bool saved_skip_evaluation;
18076 When parsing a template-id, the first non-nested `>' is taken as
18077 the end of the template-argument-list rather than a greater-than
18079 saved_greater_than_is_operator_p
18080 = parser->greater_than_is_operator_p;
18081 parser->greater_than_is_operator_p = false;
18082 /* Parsing the argument list may modify SCOPE, so we save it
18084 saved_scope = parser->scope;
18085 saved_qualifying_scope = parser->qualifying_scope;
18086 saved_object_scope = parser->object_scope;
18087 /* We need to evaluate the template arguments, even though this
18088 template-id may be nested within a "sizeof". */
18089 saved_skip_evaluation = skip_evaluation;
18090 skip_evaluation = false;
18091 /* Parse the template-argument-list itself. */
18092 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18093 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18094 arguments = NULL_TREE;
18096 arguments = cp_parser_template_argument_list (parser);
18097 /* Look for the `>' that ends the template-argument-list. If we find
18098 a '>>' instead, it's probably just a typo. */
18099 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18101 if (cxx_dialect != cxx98)
18103 /* In C++0x, a `>>' in a template argument list or cast
18104 expression is considered to be two separate `>'
18105 tokens. So, change the current token to a `>', but don't
18106 consume it: it will be consumed later when the outer
18107 template argument list (or cast expression) is parsed.
18108 Note that this replacement of `>' for `>>' is necessary
18109 even if we are parsing tentatively: in the tentative
18110 case, after calling
18111 cp_parser_enclosed_template_argument_list we will always
18112 throw away all of the template arguments and the first
18113 closing `>', either because the template argument list
18114 was erroneous or because we are replacing those tokens
18115 with a CPP_TEMPLATE_ID token. The second `>' (which will
18116 not have been thrown away) is needed either to close an
18117 outer template argument list or to complete a new-style
18119 cp_token *token = cp_lexer_peek_token (parser->lexer);
18120 token->type = CPP_GREATER;
18122 else if (!saved_greater_than_is_operator_p)
18124 /* If we're in a nested template argument list, the '>>' has
18125 to be a typo for '> >'. We emit the error message, but we
18126 continue parsing and we push a '>' as next token, so that
18127 the argument list will be parsed correctly. Note that the
18128 global source location is still on the token before the
18129 '>>', so we need to say explicitly where we want it. */
18130 cp_token *token = cp_lexer_peek_token (parser->lexer);
18131 error ("%H%<>>%> should be %<> >%> "
18132 "within a nested template argument list",
18135 token->type = CPP_GREATER;
18139 /* If this is not a nested template argument list, the '>>'
18140 is a typo for '>'. Emit an error message and continue.
18141 Same deal about the token location, but here we can get it
18142 right by consuming the '>>' before issuing the diagnostic. */
18143 cp_token *token = cp_lexer_consume_token (parser->lexer);
18144 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18145 "a template argument list", &token->location);
18149 cp_parser_skip_to_end_of_template_parameter_list (parser);
18150 /* The `>' token might be a greater-than operator again now. */
18151 parser->greater_than_is_operator_p
18152 = saved_greater_than_is_operator_p;
18153 /* Restore the SAVED_SCOPE. */
18154 parser->scope = saved_scope;
18155 parser->qualifying_scope = saved_qualifying_scope;
18156 parser->object_scope = saved_object_scope;
18157 skip_evaluation = saved_skip_evaluation;
18162 /* MEMBER_FUNCTION is a member function, or a friend. If default
18163 arguments, or the body of the function have not yet been parsed,
18167 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18169 /* If this member is a template, get the underlying
18171 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18172 member_function = DECL_TEMPLATE_RESULT (member_function);
18174 /* There should not be any class definitions in progress at this
18175 point; the bodies of members are only parsed outside of all class
18177 gcc_assert (parser->num_classes_being_defined == 0);
18178 /* While we're parsing the member functions we might encounter more
18179 classes. We want to handle them right away, but we don't want
18180 them getting mixed up with functions that are currently in the
18182 parser->unparsed_functions_queues
18183 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18185 /* Make sure that any template parameters are in scope. */
18186 maybe_begin_member_template_processing (member_function);
18188 /* If the body of the function has not yet been parsed, parse it
18190 if (DECL_PENDING_INLINE_P (member_function))
18192 tree function_scope;
18193 cp_token_cache *tokens;
18195 /* The function is no longer pending; we are processing it. */
18196 tokens = DECL_PENDING_INLINE_INFO (member_function);
18197 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18198 DECL_PENDING_INLINE_P (member_function) = 0;
18200 /* If this is a local class, enter the scope of the containing
18202 function_scope = current_function_decl;
18203 if (function_scope)
18204 push_function_context ();
18206 /* Push the body of the function onto the lexer stack. */
18207 cp_parser_push_lexer_for_tokens (parser, tokens);
18209 /* Let the front end know that we going to be defining this
18211 start_preparsed_function (member_function, NULL_TREE,
18212 SF_PRE_PARSED | SF_INCLASS_INLINE);
18214 /* Don't do access checking if it is a templated function. */
18215 if (processing_template_decl)
18216 push_deferring_access_checks (dk_no_check);
18218 /* Now, parse the body of the function. */
18219 cp_parser_function_definition_after_declarator (parser,
18220 /*inline_p=*/true);
18222 if (processing_template_decl)
18223 pop_deferring_access_checks ();
18225 /* Leave the scope of the containing function. */
18226 if (function_scope)
18227 pop_function_context ();
18228 cp_parser_pop_lexer (parser);
18231 /* Remove any template parameters from the symbol table. */
18232 maybe_end_member_template_processing ();
18234 /* Restore the queue. */
18235 parser->unparsed_functions_queues
18236 = TREE_CHAIN (parser->unparsed_functions_queues);
18239 /* If DECL contains any default args, remember it on the unparsed
18240 functions queue. */
18243 cp_parser_save_default_args (cp_parser* parser, tree decl)
18247 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18249 probe = TREE_CHAIN (probe))
18250 if (TREE_PURPOSE (probe))
18252 TREE_PURPOSE (parser->unparsed_functions_queues)
18253 = tree_cons (current_class_type, decl,
18254 TREE_PURPOSE (parser->unparsed_functions_queues));
18259 /* FN is a FUNCTION_DECL which may contains a parameter with an
18260 unparsed DEFAULT_ARG. Parse the default args now. This function
18261 assumes that the current scope is the scope in which the default
18262 argument should be processed. */
18265 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18267 bool saved_local_variables_forbidden_p;
18270 /* While we're parsing the default args, we might (due to the
18271 statement expression extension) encounter more classes. We want
18272 to handle them right away, but we don't want them getting mixed
18273 up with default args that are currently in the queue. */
18274 parser->unparsed_functions_queues
18275 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18277 /* Local variable names (and the `this' keyword) may not appear
18278 in a default argument. */
18279 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18280 parser->local_variables_forbidden_p = true;
18282 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18284 parm = TREE_CHAIN (parm))
18286 cp_token_cache *tokens;
18287 tree default_arg = TREE_PURPOSE (parm);
18289 VEC(tree,gc) *insts;
18296 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18297 /* This can happen for a friend declaration for a function
18298 already declared with default arguments. */
18301 /* Push the saved tokens for the default argument onto the parser's
18303 tokens = DEFARG_TOKENS (default_arg);
18304 cp_parser_push_lexer_for_tokens (parser, tokens);
18306 /* Parse the assignment-expression. */
18307 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18308 if (parsed_arg == error_mark_node)
18310 cp_parser_pop_lexer (parser);
18314 if (!processing_template_decl)
18315 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18317 TREE_PURPOSE (parm) = parsed_arg;
18319 /* Update any instantiations we've already created. */
18320 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18321 VEC_iterate (tree, insts, ix, copy); ix++)
18322 TREE_PURPOSE (copy) = parsed_arg;
18324 /* If the token stream has not been completely used up, then
18325 there was extra junk after the end of the default
18327 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18328 cp_parser_error (parser, "expected %<,%>");
18330 /* Revert to the main lexer. */
18331 cp_parser_pop_lexer (parser);
18334 /* Make sure no default arg is missing. */
18335 check_default_args (fn);
18337 /* Restore the state of local_variables_forbidden_p. */
18338 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18340 /* Restore the queue. */
18341 parser->unparsed_functions_queues
18342 = TREE_CHAIN (parser->unparsed_functions_queues);
18345 /* Parse the operand of `sizeof' (or a similar operator). Returns
18346 either a TYPE or an expression, depending on the form of the
18347 input. The KEYWORD indicates which kind of expression we have
18351 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18353 tree expr = NULL_TREE;
18354 const char *saved_message;
18356 bool saved_integral_constant_expression_p;
18357 bool saved_non_integral_constant_expression_p;
18358 bool pack_expansion_p = false;
18360 /* Types cannot be defined in a `sizeof' expression. Save away the
18362 saved_message = parser->type_definition_forbidden_message;
18363 /* And create the new one. */
18364 tmp = concat ("types may not be defined in %<",
18365 IDENTIFIER_POINTER (ridpointers[keyword]),
18366 "%> expressions", NULL);
18367 parser->type_definition_forbidden_message = tmp;
18369 /* The restrictions on constant-expressions do not apply inside
18370 sizeof expressions. */
18371 saved_integral_constant_expression_p
18372 = parser->integral_constant_expression_p;
18373 saved_non_integral_constant_expression_p
18374 = parser->non_integral_constant_expression_p;
18375 parser->integral_constant_expression_p = false;
18377 /* If it's a `...', then we are computing the length of a parameter
18379 if (keyword == RID_SIZEOF
18380 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18382 /* Consume the `...'. */
18383 cp_lexer_consume_token (parser->lexer);
18384 maybe_warn_variadic_templates ();
18386 /* Note that this is an expansion. */
18387 pack_expansion_p = true;
18390 /* Do not actually evaluate the expression. */
18392 /* If it's a `(', then we might be looking at the type-id
18394 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18397 bool saved_in_type_id_in_expr_p;
18399 /* We can't be sure yet whether we're looking at a type-id or an
18401 cp_parser_parse_tentatively (parser);
18402 /* Consume the `('. */
18403 cp_lexer_consume_token (parser->lexer);
18404 /* Parse the type-id. */
18405 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18406 parser->in_type_id_in_expr_p = true;
18407 type = cp_parser_type_id (parser);
18408 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18409 /* Now, look for the trailing `)'. */
18410 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18411 /* If all went well, then we're done. */
18412 if (cp_parser_parse_definitely (parser))
18414 cp_decl_specifier_seq decl_specs;
18416 /* Build a trivial decl-specifier-seq. */
18417 clear_decl_specs (&decl_specs);
18418 decl_specs.type = type;
18420 /* Call grokdeclarator to figure out what type this is. */
18421 expr = grokdeclarator (NULL,
18425 /*attrlist=*/NULL);
18429 /* If the type-id production did not work out, then we must be
18430 looking at the unary-expression production. */
18432 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18433 /*cast_p=*/false, NULL);
18435 if (pack_expansion_p)
18436 /* Build a pack expansion. */
18437 expr = make_pack_expansion (expr);
18439 /* Go back to evaluating expressions. */
18442 /* Free the message we created. */
18444 /* And restore the old one. */
18445 parser->type_definition_forbidden_message = saved_message;
18446 parser->integral_constant_expression_p
18447 = saved_integral_constant_expression_p;
18448 parser->non_integral_constant_expression_p
18449 = saved_non_integral_constant_expression_p;
18454 /* If the current declaration has no declarator, return true. */
18457 cp_parser_declares_only_class_p (cp_parser *parser)
18459 /* If the next token is a `;' or a `,' then there is no
18461 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18462 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18465 /* Update the DECL_SPECS to reflect the storage class indicated by
18469 cp_parser_set_storage_class (cp_parser *parser,
18470 cp_decl_specifier_seq *decl_specs,
18472 location_t location)
18474 cp_storage_class storage_class;
18476 if (parser->in_unbraced_linkage_specification_p)
18478 error ("%Hinvalid use of %qD in linkage specification",
18479 &location, ridpointers[keyword]);
18482 else if (decl_specs->storage_class != sc_none)
18484 decl_specs->conflicting_specifiers_p = true;
18488 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18489 && decl_specs->specs[(int) ds_thread])
18491 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18492 decl_specs->specs[(int) ds_thread] = 0;
18498 storage_class = sc_auto;
18501 storage_class = sc_register;
18504 storage_class = sc_static;
18507 storage_class = sc_extern;
18510 storage_class = sc_mutable;
18513 gcc_unreachable ();
18515 decl_specs->storage_class = storage_class;
18517 /* A storage class specifier cannot be applied alongside a typedef
18518 specifier. If there is a typedef specifier present then set
18519 conflicting_specifiers_p which will trigger an error later
18520 on in grokdeclarator. */
18521 if (decl_specs->specs[(int)ds_typedef])
18522 decl_specs->conflicting_specifiers_p = true;
18525 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18526 is true, the type is a user-defined type; otherwise it is a
18527 built-in type specified by a keyword. */
18530 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18532 location_t location,
18533 bool user_defined_p)
18535 decl_specs->any_specifiers_p = true;
18537 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18538 (with, for example, in "typedef int wchar_t;") we remember that
18539 this is what happened. In system headers, we ignore these
18540 declarations so that G++ can work with system headers that are not
18542 if (decl_specs->specs[(int) ds_typedef]
18544 && (type_spec == boolean_type_node
18545 || type_spec == char16_type_node
18546 || type_spec == char32_type_node
18547 || type_spec == wchar_type_node)
18548 && (decl_specs->type
18549 || decl_specs->specs[(int) ds_long]
18550 || decl_specs->specs[(int) ds_short]
18551 || decl_specs->specs[(int) ds_unsigned]
18552 || decl_specs->specs[(int) ds_signed]))
18554 decl_specs->redefined_builtin_type = type_spec;
18555 if (!decl_specs->type)
18557 decl_specs->type = type_spec;
18558 decl_specs->user_defined_type_p = false;
18559 decl_specs->type_location = location;
18562 else if (decl_specs->type)
18563 decl_specs->multiple_types_p = true;
18566 decl_specs->type = type_spec;
18567 decl_specs->user_defined_type_p = user_defined_p;
18568 decl_specs->redefined_builtin_type = NULL_TREE;
18569 decl_specs->type_location = location;
18573 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18574 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18577 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18579 return decl_specifiers->specs[(int) ds_friend] != 0;
18582 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18583 issue an error message indicating that TOKEN_DESC was expected.
18585 Returns the token consumed, if the token had the appropriate type.
18586 Otherwise, returns NULL. */
18589 cp_parser_require (cp_parser* parser,
18590 enum cpp_ttype type,
18591 const char* token_desc)
18593 if (cp_lexer_next_token_is (parser->lexer, type))
18594 return cp_lexer_consume_token (parser->lexer);
18597 /* Output the MESSAGE -- unless we're parsing tentatively. */
18598 if (!cp_parser_simulate_error (parser))
18600 char *message = concat ("expected ", token_desc, NULL);
18601 cp_parser_error (parser, message);
18608 /* An error message is produced if the next token is not '>'.
18609 All further tokens are skipped until the desired token is
18610 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18613 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18615 /* Current level of '< ... >'. */
18616 unsigned level = 0;
18617 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18618 unsigned nesting_depth = 0;
18620 /* Are we ready, yet? If not, issue error message. */
18621 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18624 /* Skip tokens until the desired token is found. */
18627 /* Peek at the next token. */
18628 switch (cp_lexer_peek_token (parser->lexer)->type)
18631 if (!nesting_depth)
18636 if (cxx_dialect == cxx98)
18637 /* C++0x views the `>>' operator as two `>' tokens, but
18640 else if (!nesting_depth && level-- == 0)
18642 /* We've hit a `>>' where the first `>' closes the
18643 template argument list, and the second `>' is
18644 spurious. Just consume the `>>' and stop; we've
18645 already produced at least one error. */
18646 cp_lexer_consume_token (parser->lexer);
18649 /* Fall through for C++0x, so we handle the second `>' in
18653 if (!nesting_depth && level-- == 0)
18655 /* We've reached the token we want, consume it and stop. */
18656 cp_lexer_consume_token (parser->lexer);
18661 case CPP_OPEN_PAREN:
18662 case CPP_OPEN_SQUARE:
18666 case CPP_CLOSE_PAREN:
18667 case CPP_CLOSE_SQUARE:
18668 if (nesting_depth-- == 0)
18673 case CPP_PRAGMA_EOL:
18674 case CPP_SEMICOLON:
18675 case CPP_OPEN_BRACE:
18676 case CPP_CLOSE_BRACE:
18677 /* The '>' was probably forgotten, don't look further. */
18684 /* Consume this token. */
18685 cp_lexer_consume_token (parser->lexer);
18689 /* If the next token is the indicated keyword, 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_keyword (cp_parser* parser,
18698 const char* token_desc)
18700 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18702 if (token && token->keyword != keyword)
18704 dyn_string_t error_msg;
18706 /* Format the error message. */
18707 error_msg = dyn_string_new (0);
18708 dyn_string_append_cstr (error_msg, "expected ");
18709 dyn_string_append_cstr (error_msg, token_desc);
18710 cp_parser_error (parser, error_msg->s);
18711 dyn_string_delete (error_msg);
18718 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18719 function-definition. */
18722 cp_parser_token_starts_function_definition_p (cp_token* token)
18724 return (/* An ordinary function-body begins with an `{'. */
18725 token->type == CPP_OPEN_BRACE
18726 /* A ctor-initializer begins with a `:'. */
18727 || token->type == CPP_COLON
18728 /* A function-try-block begins with `try'. */
18729 || token->keyword == RID_TRY
18730 /* The named return value extension begins with `return'. */
18731 || token->keyword == RID_RETURN);
18734 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18738 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18742 token = cp_lexer_peek_token (parser->lexer);
18743 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18746 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18747 C++0x) ending a template-argument. */
18750 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18754 token = cp_lexer_peek_token (parser->lexer);
18755 return (token->type == CPP_COMMA
18756 || token->type == CPP_GREATER
18757 || token->type == CPP_ELLIPSIS
18758 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18761 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18762 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18765 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18770 token = cp_lexer_peek_nth_token (parser->lexer, n);
18771 if (token->type == CPP_LESS)
18773 /* Check for the sequence `<::' in the original code. It would be lexed as
18774 `[:', where `[' is a digraph, and there is no whitespace before
18776 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18779 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18780 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18786 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18787 or none_type otherwise. */
18789 static enum tag_types
18790 cp_parser_token_is_class_key (cp_token* token)
18792 switch (token->keyword)
18797 return record_type;
18806 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18809 cp_parser_check_class_key (enum tag_types class_key, tree type)
18811 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18812 permerror (input_location, "%qs tag used in naming %q#T",
18813 class_key == union_type ? "union"
18814 : class_key == record_type ? "struct" : "class",
18818 /* Issue an error message if DECL is redeclared with different
18819 access than its original declaration [class.access.spec/3].
18820 This applies to nested classes and nested class templates.
18824 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18826 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18829 if ((TREE_PRIVATE (decl)
18830 != (current_access_specifier == access_private_node))
18831 || (TREE_PROTECTED (decl)
18832 != (current_access_specifier == access_protected_node)))
18833 error ("%H%qD redeclared with different access", &location, decl);
18836 /* Look for the `template' keyword, as a syntactic disambiguator.
18837 Return TRUE iff it is present, in which case it will be
18841 cp_parser_optional_template_keyword (cp_parser *parser)
18843 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18845 /* The `template' keyword can only be used within templates;
18846 outside templates the parser can always figure out what is a
18847 template and what is not. */
18848 if (!processing_template_decl)
18850 cp_token *token = cp_lexer_peek_token (parser->lexer);
18851 error ("%H%<template%> (as a disambiguator) is only allowed "
18852 "within templates", &token->location);
18853 /* If this part of the token stream is rescanned, the same
18854 error message would be generated. So, we purge the token
18855 from the stream. */
18856 cp_lexer_purge_token (parser->lexer);
18861 /* Consume the `template' keyword. */
18862 cp_lexer_consume_token (parser->lexer);
18870 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18871 set PARSER->SCOPE, and perform other related actions. */
18874 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18877 struct tree_check *check_value;
18878 deferred_access_check *chk;
18879 VEC (deferred_access_check,gc) *checks;
18881 /* Get the stored value. */
18882 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18883 /* Perform any access checks that were deferred. */
18884 checks = check_value->checks;
18888 VEC_iterate (deferred_access_check, checks, i, chk) ;
18891 perform_or_defer_access_check (chk->binfo,
18896 /* Set the scope from the stored value. */
18897 parser->scope = check_value->value;
18898 parser->qualifying_scope = check_value->qualifying_scope;
18899 parser->object_scope = NULL_TREE;
18902 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18903 encounter the end of a block before what we were looking for. */
18906 cp_parser_cache_group (cp_parser *parser,
18907 enum cpp_ttype end,
18912 cp_token *token = cp_lexer_peek_token (parser->lexer);
18914 /* Abort a parenthesized expression if we encounter a semicolon. */
18915 if ((end == CPP_CLOSE_PAREN || depth == 0)
18916 && token->type == CPP_SEMICOLON)
18918 /* If we've reached the end of the file, stop. */
18919 if (token->type == CPP_EOF
18920 || (end != CPP_PRAGMA_EOL
18921 && token->type == CPP_PRAGMA_EOL))
18923 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18924 /* We've hit the end of an enclosing block, so there's been some
18925 kind of syntax error. */
18928 /* Consume the token. */
18929 cp_lexer_consume_token (parser->lexer);
18930 /* See if it starts a new group. */
18931 if (token->type == CPP_OPEN_BRACE)
18933 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18934 /* In theory this should probably check end == '}', but
18935 cp_parser_save_member_function_body needs it to exit
18936 after either '}' or ')' when called with ')'. */
18940 else if (token->type == CPP_OPEN_PAREN)
18942 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18943 if (depth == 0 && end == CPP_CLOSE_PAREN)
18946 else if (token->type == CPP_PRAGMA)
18947 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18948 else if (token->type == end)
18953 /* Begin parsing tentatively. We always save tokens while parsing
18954 tentatively so that if the tentative parsing fails we can restore the
18958 cp_parser_parse_tentatively (cp_parser* parser)
18960 /* Enter a new parsing context. */
18961 parser->context = cp_parser_context_new (parser->context);
18962 /* Begin saving tokens. */
18963 cp_lexer_save_tokens (parser->lexer);
18964 /* In order to avoid repetitive access control error messages,
18965 access checks are queued up until we are no longer parsing
18967 push_deferring_access_checks (dk_deferred);
18970 /* Commit to the currently active tentative parse. */
18973 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18975 cp_parser_context *context;
18978 /* Mark all of the levels as committed. */
18979 lexer = parser->lexer;
18980 for (context = parser->context; context->next; context = context->next)
18982 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18984 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18985 while (!cp_lexer_saving_tokens (lexer))
18986 lexer = lexer->next;
18987 cp_lexer_commit_tokens (lexer);
18991 /* Abort the currently active tentative parse. All consumed tokens
18992 will be rolled back, and no diagnostics will be issued. */
18995 cp_parser_abort_tentative_parse (cp_parser* parser)
18997 cp_parser_simulate_error (parser);
18998 /* Now, pretend that we want to see if the construct was
18999 successfully parsed. */
19000 cp_parser_parse_definitely (parser);
19003 /* Stop parsing tentatively. If a parse error has occurred, restore the
19004 token stream. Otherwise, commit to the tokens we have consumed.
19005 Returns true if no error occurred; false otherwise. */
19008 cp_parser_parse_definitely (cp_parser* parser)
19010 bool error_occurred;
19011 cp_parser_context *context;
19013 /* Remember whether or not an error occurred, since we are about to
19014 destroy that information. */
19015 error_occurred = cp_parser_error_occurred (parser);
19016 /* Remove the topmost context from the stack. */
19017 context = parser->context;
19018 parser->context = context->next;
19019 /* If no parse errors occurred, commit to the tentative parse. */
19020 if (!error_occurred)
19022 /* Commit to the tokens read tentatively, unless that was
19024 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19025 cp_lexer_commit_tokens (parser->lexer);
19027 pop_to_parent_deferring_access_checks ();
19029 /* Otherwise, if errors occurred, roll back our state so that things
19030 are just as they were before we began the tentative parse. */
19033 cp_lexer_rollback_tokens (parser->lexer);
19034 pop_deferring_access_checks ();
19036 /* Add the context to the front of the free list. */
19037 context->next = cp_parser_context_free_list;
19038 cp_parser_context_free_list = context;
19040 return !error_occurred;
19043 /* Returns true if we are parsing tentatively and are not committed to
19044 this tentative parse. */
19047 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19049 return (cp_parser_parsing_tentatively (parser)
19050 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19053 /* Returns nonzero iff an error has occurred during the most recent
19054 tentative parse. */
19057 cp_parser_error_occurred (cp_parser* parser)
19059 return (cp_parser_parsing_tentatively (parser)
19060 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19063 /* Returns nonzero if GNU extensions are allowed. */
19066 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19068 return parser->allow_gnu_extensions_p;
19071 /* Objective-C++ Productions */
19074 /* Parse an Objective-C expression, which feeds into a primary-expression
19078 objc-message-expression
19079 objc-string-literal
19080 objc-encode-expression
19081 objc-protocol-expression
19082 objc-selector-expression
19084 Returns a tree representation of the expression. */
19087 cp_parser_objc_expression (cp_parser* parser)
19089 /* Try to figure out what kind of declaration is present. */
19090 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19094 case CPP_OPEN_SQUARE:
19095 return cp_parser_objc_message_expression (parser);
19097 case CPP_OBJC_STRING:
19098 kwd = cp_lexer_consume_token (parser->lexer);
19099 return objc_build_string_object (kwd->u.value);
19102 switch (kwd->keyword)
19104 case RID_AT_ENCODE:
19105 return cp_parser_objc_encode_expression (parser);
19107 case RID_AT_PROTOCOL:
19108 return cp_parser_objc_protocol_expression (parser);
19110 case RID_AT_SELECTOR:
19111 return cp_parser_objc_selector_expression (parser);
19117 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19118 &kwd->location, kwd->u.value);
19119 cp_parser_skip_to_end_of_block_or_statement (parser);
19122 return error_mark_node;
19125 /* Parse an Objective-C message expression.
19127 objc-message-expression:
19128 [ objc-message-receiver objc-message-args ]
19130 Returns a representation of an Objective-C message. */
19133 cp_parser_objc_message_expression (cp_parser* parser)
19135 tree receiver, messageargs;
19137 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19138 receiver = cp_parser_objc_message_receiver (parser);
19139 messageargs = cp_parser_objc_message_args (parser);
19140 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19142 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19145 /* Parse an objc-message-receiver.
19147 objc-message-receiver:
19149 simple-type-specifier
19151 Returns a representation of the type or expression. */
19154 cp_parser_objc_message_receiver (cp_parser* parser)
19158 /* An Objective-C message receiver may be either (1) a type
19159 or (2) an expression. */
19160 cp_parser_parse_tentatively (parser);
19161 rcv = cp_parser_expression (parser, false, NULL);
19163 if (cp_parser_parse_definitely (parser))
19166 rcv = cp_parser_simple_type_specifier (parser,
19167 /*decl_specs=*/NULL,
19168 CP_PARSER_FLAGS_NONE);
19170 return objc_get_class_reference (rcv);
19173 /* Parse the arguments and selectors comprising an Objective-C message.
19178 objc-selector-args , objc-comma-args
19180 objc-selector-args:
19181 objc-selector [opt] : assignment-expression
19182 objc-selector-args objc-selector [opt] : assignment-expression
19185 assignment-expression
19186 objc-comma-args , assignment-expression
19188 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19189 selector arguments and TREE_VALUE containing a list of comma
19193 cp_parser_objc_message_args (cp_parser* parser)
19195 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19196 bool maybe_unary_selector_p = true;
19197 cp_token *token = cp_lexer_peek_token (parser->lexer);
19199 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19201 tree selector = NULL_TREE, arg;
19203 if (token->type != CPP_COLON)
19204 selector = cp_parser_objc_selector (parser);
19206 /* Detect if we have a unary selector. */
19207 if (maybe_unary_selector_p
19208 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19209 return build_tree_list (selector, NULL_TREE);
19211 maybe_unary_selector_p = false;
19212 cp_parser_require (parser, CPP_COLON, "%<:%>");
19213 arg = cp_parser_assignment_expression (parser, false, NULL);
19216 = chainon (sel_args,
19217 build_tree_list (selector, arg));
19219 token = cp_lexer_peek_token (parser->lexer);
19222 /* Handle non-selector arguments, if any. */
19223 while (token->type == CPP_COMMA)
19227 cp_lexer_consume_token (parser->lexer);
19228 arg = cp_parser_assignment_expression (parser, false, NULL);
19231 = chainon (addl_args,
19232 build_tree_list (NULL_TREE, arg));
19234 token = cp_lexer_peek_token (parser->lexer);
19237 return build_tree_list (sel_args, addl_args);
19240 /* Parse an Objective-C encode expression.
19242 objc-encode-expression:
19243 @encode objc-typename
19245 Returns an encoded representation of the type argument. */
19248 cp_parser_objc_encode_expression (cp_parser* parser)
19253 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19254 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19255 token = cp_lexer_peek_token (parser->lexer);
19256 type = complete_type (cp_parser_type_id (parser));
19257 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19261 error ("%H%<@encode%> must specify a type as an argument",
19263 return error_mark_node;
19266 return objc_build_encode_expr (type);
19269 /* Parse an Objective-C @defs expression. */
19272 cp_parser_objc_defs_expression (cp_parser *parser)
19276 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19277 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19278 name = cp_parser_identifier (parser);
19279 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19281 return objc_get_class_ivars (name);
19284 /* Parse an Objective-C protocol expression.
19286 objc-protocol-expression:
19287 @protocol ( identifier )
19289 Returns a representation of the protocol expression. */
19292 cp_parser_objc_protocol_expression (cp_parser* parser)
19296 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19297 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19298 proto = cp_parser_identifier (parser);
19299 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19301 return objc_build_protocol_expr (proto);
19304 /* Parse an Objective-C selector expression.
19306 objc-selector-expression:
19307 @selector ( objc-method-signature )
19309 objc-method-signature:
19315 objc-selector-seq objc-selector :
19317 Returns a representation of the method selector. */
19320 cp_parser_objc_selector_expression (cp_parser* parser)
19322 tree sel_seq = NULL_TREE;
19323 bool maybe_unary_selector_p = true;
19326 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19327 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19328 token = cp_lexer_peek_token (parser->lexer);
19330 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19331 || token->type == CPP_SCOPE)
19333 tree selector = NULL_TREE;
19335 if (token->type != CPP_COLON
19336 || token->type == CPP_SCOPE)
19337 selector = cp_parser_objc_selector (parser);
19339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19340 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19342 /* Detect if we have a unary selector. */
19343 if (maybe_unary_selector_p)
19345 sel_seq = selector;
19346 goto finish_selector;
19350 cp_parser_error (parser, "expected %<:%>");
19353 maybe_unary_selector_p = false;
19354 token = cp_lexer_consume_token (parser->lexer);
19356 if (token->type == CPP_SCOPE)
19359 = chainon (sel_seq,
19360 build_tree_list (selector, NULL_TREE));
19362 = chainon (sel_seq,
19363 build_tree_list (NULL_TREE, NULL_TREE));
19367 = chainon (sel_seq,
19368 build_tree_list (selector, NULL_TREE));
19370 token = cp_lexer_peek_token (parser->lexer);
19374 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19376 return objc_build_selector_expr (sel_seq);
19379 /* Parse a list of identifiers.
19381 objc-identifier-list:
19383 objc-identifier-list , identifier
19385 Returns a TREE_LIST of identifier nodes. */
19388 cp_parser_objc_identifier_list (cp_parser* parser)
19390 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19391 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19393 while (sep->type == CPP_COMMA)
19395 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19396 list = chainon (list,
19397 build_tree_list (NULL_TREE,
19398 cp_parser_identifier (parser)));
19399 sep = cp_lexer_peek_token (parser->lexer);
19405 /* Parse an Objective-C alias declaration.
19407 objc-alias-declaration:
19408 @compatibility_alias identifier identifier ;
19410 This function registers the alias mapping with the Objective-C front end.
19411 It returns nothing. */
19414 cp_parser_objc_alias_declaration (cp_parser* parser)
19418 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19419 alias = cp_parser_identifier (parser);
19420 orig = cp_parser_identifier (parser);
19421 objc_declare_alias (alias, orig);
19422 cp_parser_consume_semicolon_at_end_of_statement (parser);
19425 /* Parse an Objective-C class forward-declaration.
19427 objc-class-declaration:
19428 @class objc-identifier-list ;
19430 The function registers the forward declarations with the Objective-C
19431 front end. It returns nothing. */
19434 cp_parser_objc_class_declaration (cp_parser* parser)
19436 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19437 objc_declare_class (cp_parser_objc_identifier_list (parser));
19438 cp_parser_consume_semicolon_at_end_of_statement (parser);
19441 /* Parse a list of Objective-C protocol references.
19443 objc-protocol-refs-opt:
19444 objc-protocol-refs [opt]
19446 objc-protocol-refs:
19447 < objc-identifier-list >
19449 Returns a TREE_LIST of identifiers, if any. */
19452 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19454 tree protorefs = NULL_TREE;
19456 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19458 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19459 protorefs = cp_parser_objc_identifier_list (parser);
19460 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19466 /* Parse a Objective-C visibility specification. */
19469 cp_parser_objc_visibility_spec (cp_parser* parser)
19471 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19473 switch (vis->keyword)
19475 case RID_AT_PRIVATE:
19476 objc_set_visibility (2);
19478 case RID_AT_PROTECTED:
19479 objc_set_visibility (0);
19481 case RID_AT_PUBLIC:
19482 objc_set_visibility (1);
19488 /* Eat '@private'/'@protected'/'@public'. */
19489 cp_lexer_consume_token (parser->lexer);
19492 /* Parse an Objective-C method type. */
19495 cp_parser_objc_method_type (cp_parser* parser)
19497 objc_set_method_type
19498 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19503 /* Parse an Objective-C protocol qualifier. */
19506 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19508 tree quals = NULL_TREE, node;
19509 cp_token *token = cp_lexer_peek_token (parser->lexer);
19511 node = token->u.value;
19513 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19514 && (node == ridpointers [(int) RID_IN]
19515 || node == ridpointers [(int) RID_OUT]
19516 || node == ridpointers [(int) RID_INOUT]
19517 || node == ridpointers [(int) RID_BYCOPY]
19518 || node == ridpointers [(int) RID_BYREF]
19519 || node == ridpointers [(int) RID_ONEWAY]))
19521 quals = tree_cons (NULL_TREE, node, quals);
19522 cp_lexer_consume_token (parser->lexer);
19523 token = cp_lexer_peek_token (parser->lexer);
19524 node = token->u.value;
19530 /* Parse an Objective-C typename. */
19533 cp_parser_objc_typename (cp_parser* parser)
19535 tree type_name = NULL_TREE;
19537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19539 tree proto_quals, cp_type = NULL_TREE;
19541 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19542 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19544 /* An ObjC type name may consist of just protocol qualifiers, in which
19545 case the type shall default to 'id'. */
19546 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19547 cp_type = cp_parser_type_id (parser);
19549 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19550 type_name = build_tree_list (proto_quals, cp_type);
19556 /* Check to see if TYPE refers to an Objective-C selector name. */
19559 cp_parser_objc_selector_p (enum cpp_ttype type)
19561 return (type == CPP_NAME || type == CPP_KEYWORD
19562 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19563 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19564 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19565 || type == CPP_XOR || type == CPP_XOR_EQ);
19568 /* Parse an Objective-C selector. */
19571 cp_parser_objc_selector (cp_parser* parser)
19573 cp_token *token = cp_lexer_consume_token (parser->lexer);
19575 if (!cp_parser_objc_selector_p (token->type))
19577 error ("%Hinvalid Objective-C++ selector name", &token->location);
19578 return error_mark_node;
19581 /* C++ operator names are allowed to appear in ObjC selectors. */
19582 switch (token->type)
19584 case CPP_AND_AND: return get_identifier ("and");
19585 case CPP_AND_EQ: return get_identifier ("and_eq");
19586 case CPP_AND: return get_identifier ("bitand");
19587 case CPP_OR: return get_identifier ("bitor");
19588 case CPP_COMPL: return get_identifier ("compl");
19589 case CPP_NOT: return get_identifier ("not");
19590 case CPP_NOT_EQ: return get_identifier ("not_eq");
19591 case CPP_OR_OR: return get_identifier ("or");
19592 case CPP_OR_EQ: return get_identifier ("or_eq");
19593 case CPP_XOR: return get_identifier ("xor");
19594 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19595 default: return token->u.value;
19599 /* Parse an Objective-C params list. */
19602 cp_parser_objc_method_keyword_params (cp_parser* parser)
19604 tree params = NULL_TREE;
19605 bool maybe_unary_selector_p = true;
19606 cp_token *token = cp_lexer_peek_token (parser->lexer);
19608 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19610 tree selector = NULL_TREE, type_name, identifier;
19612 if (token->type != CPP_COLON)
19613 selector = cp_parser_objc_selector (parser);
19615 /* Detect if we have a unary selector. */
19616 if (maybe_unary_selector_p
19617 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19620 maybe_unary_selector_p = false;
19621 cp_parser_require (parser, CPP_COLON, "%<:%>");
19622 type_name = cp_parser_objc_typename (parser);
19623 identifier = cp_parser_identifier (parser);
19627 objc_build_keyword_decl (selector,
19631 token = cp_lexer_peek_token (parser->lexer);
19637 /* Parse the non-keyword Objective-C params. */
19640 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19642 tree params = make_node (TREE_LIST);
19643 cp_token *token = cp_lexer_peek_token (parser->lexer);
19644 *ellipsisp = false; /* Initially, assume no ellipsis. */
19646 while (token->type == CPP_COMMA)
19648 cp_parameter_declarator *parmdecl;
19651 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19652 token = cp_lexer_peek_token (parser->lexer);
19654 if (token->type == CPP_ELLIPSIS)
19656 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19661 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19662 parm = grokdeclarator (parmdecl->declarator,
19663 &parmdecl->decl_specifiers,
19664 PARM, /*initialized=*/0,
19665 /*attrlist=*/NULL);
19667 chainon (params, build_tree_list (NULL_TREE, parm));
19668 token = cp_lexer_peek_token (parser->lexer);
19674 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19677 cp_parser_objc_interstitial_code (cp_parser* parser)
19679 cp_token *token = cp_lexer_peek_token (parser->lexer);
19681 /* If the next token is `extern' and the following token is a string
19682 literal, then we have a linkage specification. */
19683 if (token->keyword == RID_EXTERN
19684 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19685 cp_parser_linkage_specification (parser);
19686 /* Handle #pragma, if any. */
19687 else if (token->type == CPP_PRAGMA)
19688 cp_parser_pragma (parser, pragma_external);
19689 /* Allow stray semicolons. */
19690 else if (token->type == CPP_SEMICOLON)
19691 cp_lexer_consume_token (parser->lexer);
19692 /* Finally, try to parse a block-declaration, or a function-definition. */
19694 cp_parser_block_declaration (parser, /*statement_p=*/false);
19697 /* Parse a method signature. */
19700 cp_parser_objc_method_signature (cp_parser* parser)
19702 tree rettype, kwdparms, optparms;
19703 bool ellipsis = false;
19705 cp_parser_objc_method_type (parser);
19706 rettype = cp_parser_objc_typename (parser);
19707 kwdparms = cp_parser_objc_method_keyword_params (parser);
19708 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19710 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19713 /* Pars an Objective-C method prototype list. */
19716 cp_parser_objc_method_prototype_list (cp_parser* parser)
19718 cp_token *token = cp_lexer_peek_token (parser->lexer);
19720 while (token->keyword != RID_AT_END)
19722 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19724 objc_add_method_declaration
19725 (cp_parser_objc_method_signature (parser));
19726 cp_parser_consume_semicolon_at_end_of_statement (parser);
19729 /* Allow for interspersed non-ObjC++ code. */
19730 cp_parser_objc_interstitial_code (parser);
19732 token = cp_lexer_peek_token (parser->lexer);
19735 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19736 objc_finish_interface ();
19739 /* Parse an Objective-C method definition list. */
19742 cp_parser_objc_method_definition_list (cp_parser* parser)
19744 cp_token *token = cp_lexer_peek_token (parser->lexer);
19746 while (token->keyword != RID_AT_END)
19750 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19752 push_deferring_access_checks (dk_deferred);
19753 objc_start_method_definition
19754 (cp_parser_objc_method_signature (parser));
19756 /* For historical reasons, we accept an optional semicolon. */
19757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19758 cp_lexer_consume_token (parser->lexer);
19760 perform_deferred_access_checks ();
19761 stop_deferring_access_checks ();
19762 meth = cp_parser_function_definition_after_declarator (parser,
19764 pop_deferring_access_checks ();
19765 objc_finish_method_definition (meth);
19768 /* Allow for interspersed non-ObjC++ code. */
19769 cp_parser_objc_interstitial_code (parser);
19771 token = cp_lexer_peek_token (parser->lexer);
19774 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19775 objc_finish_implementation ();
19778 /* Parse Objective-C ivars. */
19781 cp_parser_objc_class_ivars (cp_parser* parser)
19783 cp_token *token = cp_lexer_peek_token (parser->lexer);
19785 if (token->type != CPP_OPEN_BRACE)
19786 return; /* No ivars specified. */
19788 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19789 token = cp_lexer_peek_token (parser->lexer);
19791 while (token->type != CPP_CLOSE_BRACE)
19793 cp_decl_specifier_seq declspecs;
19794 int decl_class_or_enum_p;
19795 tree prefix_attributes;
19797 cp_parser_objc_visibility_spec (parser);
19799 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19802 cp_parser_decl_specifier_seq (parser,
19803 CP_PARSER_FLAGS_OPTIONAL,
19805 &decl_class_or_enum_p);
19806 prefix_attributes = declspecs.attributes;
19807 declspecs.attributes = NULL_TREE;
19809 /* Keep going until we hit the `;' at the end of the
19811 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19813 tree width = NULL_TREE, attributes, first_attribute, decl;
19814 cp_declarator *declarator = NULL;
19815 int ctor_dtor_or_conv_p;
19817 /* Check for a (possibly unnamed) bitfield declaration. */
19818 token = cp_lexer_peek_token (parser->lexer);
19819 if (token->type == CPP_COLON)
19822 if (token->type == CPP_NAME
19823 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19826 /* Get the name of the bitfield. */
19827 declarator = make_id_declarator (NULL_TREE,
19828 cp_parser_identifier (parser),
19832 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19833 /* Get the width of the bitfield. */
19835 = cp_parser_constant_expression (parser,
19836 /*allow_non_constant=*/false,
19841 /* Parse the declarator. */
19843 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19844 &ctor_dtor_or_conv_p,
19845 /*parenthesized_p=*/NULL,
19846 /*member_p=*/false);
19849 /* Look for attributes that apply to the ivar. */
19850 attributes = cp_parser_attributes_opt (parser);
19851 /* Remember which attributes are prefix attributes and
19853 first_attribute = attributes;
19854 /* Combine the attributes. */
19855 attributes = chainon (prefix_attributes, attributes);
19858 /* Create the bitfield declaration. */
19859 decl = grokbitfield (declarator, &declspecs,
19863 decl = grokfield (declarator, &declspecs,
19864 NULL_TREE, /*init_const_expr_p=*/false,
19865 NULL_TREE, attributes);
19867 /* Add the instance variable. */
19868 objc_add_instance_variable (decl);
19870 /* Reset PREFIX_ATTRIBUTES. */
19871 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19872 attributes = TREE_CHAIN (attributes);
19874 TREE_CHAIN (attributes) = NULL_TREE;
19876 token = cp_lexer_peek_token (parser->lexer);
19878 if (token->type == CPP_COMMA)
19880 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19886 cp_parser_consume_semicolon_at_end_of_statement (parser);
19887 token = cp_lexer_peek_token (parser->lexer);
19890 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19891 /* For historical reasons, we accept an optional semicolon. */
19892 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19893 cp_lexer_consume_token (parser->lexer);
19896 /* Parse an Objective-C protocol declaration. */
19899 cp_parser_objc_protocol_declaration (cp_parser* parser)
19901 tree proto, protorefs;
19904 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19907 tok = cp_lexer_peek_token (parser->lexer);
19908 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19912 /* See if we have a forward declaration or a definition. */
19913 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19915 /* Try a forward declaration first. */
19916 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19918 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19920 cp_parser_consume_semicolon_at_end_of_statement (parser);
19923 /* Ok, we got a full-fledged definition (or at least should). */
19926 proto = cp_parser_identifier (parser);
19927 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19928 objc_start_protocol (proto, protorefs);
19929 cp_parser_objc_method_prototype_list (parser);
19933 /* Parse an Objective-C superclass or category. */
19936 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19939 cp_token *next = cp_lexer_peek_token (parser->lexer);
19941 *super = *categ = NULL_TREE;
19942 if (next->type == CPP_COLON)
19944 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19945 *super = cp_parser_identifier (parser);
19947 else if (next->type == CPP_OPEN_PAREN)
19949 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19950 *categ = cp_parser_identifier (parser);
19951 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19955 /* Parse an Objective-C class interface. */
19958 cp_parser_objc_class_interface (cp_parser* parser)
19960 tree name, super, categ, protos;
19962 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19963 name = cp_parser_identifier (parser);
19964 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19965 protos = cp_parser_objc_protocol_refs_opt (parser);
19967 /* We have either a class or a category on our hands. */
19969 objc_start_category_interface (name, categ, protos);
19972 objc_start_class_interface (name, super, protos);
19973 /* Handle instance variable declarations, if any. */
19974 cp_parser_objc_class_ivars (parser);
19975 objc_continue_interface ();
19978 cp_parser_objc_method_prototype_list (parser);
19981 /* Parse an Objective-C class implementation. */
19984 cp_parser_objc_class_implementation (cp_parser* parser)
19986 tree name, super, categ;
19988 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19989 name = cp_parser_identifier (parser);
19990 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19992 /* We have either a class or a category on our hands. */
19994 objc_start_category_implementation (name, categ);
19997 objc_start_class_implementation (name, super);
19998 /* Handle instance variable declarations, if any. */
19999 cp_parser_objc_class_ivars (parser);
20000 objc_continue_implementation ();
20003 cp_parser_objc_method_definition_list (parser);
20006 /* Consume the @end token and finish off the implementation. */
20009 cp_parser_objc_end_implementation (cp_parser* parser)
20011 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20012 objc_finish_implementation ();
20015 /* Parse an Objective-C declaration. */
20018 cp_parser_objc_declaration (cp_parser* parser)
20020 /* Try to figure out what kind of declaration is present. */
20021 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20023 switch (kwd->keyword)
20026 cp_parser_objc_alias_declaration (parser);
20029 cp_parser_objc_class_declaration (parser);
20031 case RID_AT_PROTOCOL:
20032 cp_parser_objc_protocol_declaration (parser);
20034 case RID_AT_INTERFACE:
20035 cp_parser_objc_class_interface (parser);
20037 case RID_AT_IMPLEMENTATION:
20038 cp_parser_objc_class_implementation (parser);
20041 cp_parser_objc_end_implementation (parser);
20044 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20045 &kwd->location, kwd->u.value);
20046 cp_parser_skip_to_end_of_block_or_statement (parser);
20050 /* Parse an Objective-C try-catch-finally statement.
20052 objc-try-catch-finally-stmt:
20053 @try compound-statement objc-catch-clause-seq [opt]
20054 objc-finally-clause [opt]
20056 objc-catch-clause-seq:
20057 objc-catch-clause objc-catch-clause-seq [opt]
20060 @catch ( exception-declaration ) compound-statement
20062 objc-finally-clause
20063 @finally compound-statement
20065 Returns NULL_TREE. */
20068 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20069 location_t location;
20072 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20073 location = cp_lexer_peek_token (parser->lexer)->location;
20074 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20075 node, lest it get absorbed into the surrounding block. */
20076 stmt = push_stmt_list ();
20077 cp_parser_compound_statement (parser, NULL, false);
20078 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20080 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20082 cp_parameter_declarator *parmdecl;
20085 cp_lexer_consume_token (parser->lexer);
20086 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20087 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20088 parm = grokdeclarator (parmdecl->declarator,
20089 &parmdecl->decl_specifiers,
20090 PARM, /*initialized=*/0,
20091 /*attrlist=*/NULL);
20092 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20093 objc_begin_catch_clause (parm);
20094 cp_parser_compound_statement (parser, NULL, false);
20095 objc_finish_catch_clause ();
20098 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20100 cp_lexer_consume_token (parser->lexer);
20101 location = cp_lexer_peek_token (parser->lexer)->location;
20102 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20103 node, lest it get absorbed into the surrounding block. */
20104 stmt = push_stmt_list ();
20105 cp_parser_compound_statement (parser, NULL, false);
20106 objc_build_finally_clause (location, pop_stmt_list (stmt));
20109 return objc_finish_try_stmt ();
20112 /* Parse an Objective-C synchronized statement.
20114 objc-synchronized-stmt:
20115 @synchronized ( expression ) compound-statement
20117 Returns NULL_TREE. */
20120 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20121 location_t location;
20124 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20126 location = cp_lexer_peek_token (parser->lexer)->location;
20127 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20128 lock = cp_parser_expression (parser, false, NULL);
20129 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20131 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20132 node, lest it get absorbed into the surrounding block. */
20133 stmt = push_stmt_list ();
20134 cp_parser_compound_statement (parser, NULL, false);
20136 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20139 /* Parse an Objective-C throw statement.
20142 @throw assignment-expression [opt] ;
20144 Returns a constructed '@throw' statement. */
20147 cp_parser_objc_throw_statement (cp_parser *parser) {
20148 tree expr = NULL_TREE;
20150 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20153 expr = cp_parser_assignment_expression (parser, false, NULL);
20155 cp_parser_consume_semicolon_at_end_of_statement (parser);
20157 return objc_build_throw_stmt (expr);
20160 /* Parse an Objective-C statement. */
20163 cp_parser_objc_statement (cp_parser * parser) {
20164 /* Try to figure out what kind of declaration is present. */
20165 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20167 switch (kwd->keyword)
20170 return cp_parser_objc_try_catch_finally_statement (parser);
20171 case RID_AT_SYNCHRONIZED:
20172 return cp_parser_objc_synchronized_statement (parser);
20174 return cp_parser_objc_throw_statement (parser);
20176 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20177 &kwd->location, kwd->u.value);
20178 cp_parser_skip_to_end_of_block_or_statement (parser);
20181 return error_mark_node;
20184 /* OpenMP 2.5 parsing routines. */
20186 /* Returns name of the next clause.
20187 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20188 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20189 returned and the token is consumed. */
20191 static pragma_omp_clause
20192 cp_parser_omp_clause_name (cp_parser *parser)
20194 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20197 result = PRAGMA_OMP_CLAUSE_IF;
20198 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20199 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20200 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20201 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20202 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20204 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20205 const char *p = IDENTIFIER_POINTER (id);
20210 if (!strcmp ("collapse", p))
20211 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20212 else if (!strcmp ("copyin", p))
20213 result = PRAGMA_OMP_CLAUSE_COPYIN;
20214 else if (!strcmp ("copyprivate", p))
20215 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20218 if (!strcmp ("firstprivate", p))
20219 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20222 if (!strcmp ("lastprivate", p))
20223 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20226 if (!strcmp ("nowait", p))
20227 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20228 else if (!strcmp ("num_threads", p))
20229 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20232 if (!strcmp ("ordered", p))
20233 result = PRAGMA_OMP_CLAUSE_ORDERED;
20236 if (!strcmp ("reduction", p))
20237 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20240 if (!strcmp ("schedule", p))
20241 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20242 else if (!strcmp ("shared", p))
20243 result = PRAGMA_OMP_CLAUSE_SHARED;
20246 if (!strcmp ("untied", p))
20247 result = PRAGMA_OMP_CLAUSE_UNTIED;
20252 if (result != PRAGMA_OMP_CLAUSE_NONE)
20253 cp_lexer_consume_token (parser->lexer);
20258 /* Validate that a clause of the given type does not already exist. */
20261 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20262 const char *name, location_t location)
20266 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20267 if (OMP_CLAUSE_CODE (c) == code)
20269 error ("%Htoo many %qs clauses", &location, name);
20277 variable-list , identifier
20279 In addition, we match a closing parenthesis. An opening parenthesis
20280 will have been consumed by the caller.
20282 If KIND is nonzero, create the appropriate node and install the decl
20283 in OMP_CLAUSE_DECL and add the node to the head of the list.
20285 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20286 return the list created. */
20289 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20297 token = cp_lexer_peek_token (parser->lexer);
20298 name = cp_parser_id_expression (parser, /*template_p=*/false,
20299 /*check_dependency_p=*/true,
20300 /*template_p=*/NULL,
20301 /*declarator_p=*/false,
20302 /*optional_p=*/false);
20303 if (name == error_mark_node)
20306 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20307 if (decl == error_mark_node)
20308 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20309 else if (kind != 0)
20311 tree u = build_omp_clause (kind);
20312 OMP_CLAUSE_DECL (u) = decl;
20313 OMP_CLAUSE_CHAIN (u) = list;
20317 list = tree_cons (decl, NULL_TREE, list);
20320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20322 cp_lexer_consume_token (parser->lexer);
20325 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20329 /* Try to resync to an unnested comma. Copied from
20330 cp_parser_parenthesized_expression_list. */
20332 ending = cp_parser_skip_to_closing_parenthesis (parser,
20333 /*recovering=*/true,
20335 /*consume_paren=*/true);
20343 /* Similarly, but expect leading and trailing parenthesis. This is a very
20344 common case for omp clauses. */
20347 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20349 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20350 return cp_parser_omp_var_list_no_open (parser, kind, list);
20355 collapse ( constant-expression ) */
20358 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20364 loc = cp_lexer_peek_token (parser->lexer)->location;
20365 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20368 num = cp_parser_constant_expression (parser, false, NULL);
20370 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20372 /*or_comma=*/false,
20373 /*consume_paren=*/true);
20375 if (num == error_mark_node)
20377 num = fold_non_dependent_expr (num);
20378 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20379 || !host_integerp (num, 0)
20380 || (n = tree_low_cst (num, 0)) <= 0
20383 error ("%Hcollapse argument needs positive constant integer expression",
20388 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20389 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20390 OMP_CLAUSE_CHAIN (c) = list;
20391 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20397 default ( shared | none ) */
20400 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20402 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20407 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20409 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20410 const char *p = IDENTIFIER_POINTER (id);
20415 if (strcmp ("none", p) != 0)
20417 kind = OMP_CLAUSE_DEFAULT_NONE;
20421 if (strcmp ("shared", p) != 0)
20423 kind = OMP_CLAUSE_DEFAULT_SHARED;
20430 cp_lexer_consume_token (parser->lexer);
20435 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20439 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20440 /*or_comma=*/false,
20441 /*consume_paren=*/true);
20443 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20446 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20447 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20448 OMP_CLAUSE_CHAIN (c) = list;
20449 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20455 if ( expression ) */
20458 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20462 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20465 t = cp_parser_condition (parser);
20467 if (t == error_mark_node
20468 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20469 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20470 /*or_comma=*/false,
20471 /*consume_paren=*/true);
20473 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20475 c = build_omp_clause (OMP_CLAUSE_IF);
20476 OMP_CLAUSE_IF_EXPR (c) = t;
20477 OMP_CLAUSE_CHAIN (c) = list;
20486 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20487 tree list, location_t location)
20491 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20493 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20494 OMP_CLAUSE_CHAIN (c) = list;
20499 num_threads ( expression ) */
20502 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20503 location_t location)
20507 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20510 t = cp_parser_expression (parser, false, NULL);
20512 if (t == error_mark_node
20513 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20514 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20515 /*or_comma=*/false,
20516 /*consume_paren=*/true);
20518 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20519 "num_threads", location);
20521 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20522 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20523 OMP_CLAUSE_CHAIN (c) = list;
20532 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20533 tree list, location_t location)
20537 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20538 "ordered", location);
20540 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20541 OMP_CLAUSE_CHAIN (c) = list;
20546 reduction ( reduction-operator : variable-list )
20548 reduction-operator:
20549 One of: + * - & ^ | && || */
20552 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20554 enum tree_code code;
20557 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20560 switch (cp_lexer_peek_token (parser->lexer)->type)
20572 code = BIT_AND_EXPR;
20575 code = BIT_XOR_EXPR;
20578 code = BIT_IOR_EXPR;
20581 code = TRUTH_ANDIF_EXPR;
20584 code = TRUTH_ORIF_EXPR;
20587 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20588 "%<|%>, %<&&%>, or %<||%>");
20590 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20591 /*or_comma=*/false,
20592 /*consume_paren=*/true);
20595 cp_lexer_consume_token (parser->lexer);
20597 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20600 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20601 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20602 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20608 schedule ( schedule-kind )
20609 schedule ( schedule-kind , expression )
20612 static | dynamic | guided | runtime | auto */
20615 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20619 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20622 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20624 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20626 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20627 const char *p = IDENTIFIER_POINTER (id);
20632 if (strcmp ("dynamic", p) != 0)
20634 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20638 if (strcmp ("guided", p) != 0)
20640 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20644 if (strcmp ("runtime", p) != 0)
20646 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20653 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20654 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20655 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20656 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20659 cp_lexer_consume_token (parser->lexer);
20661 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20664 cp_lexer_consume_token (parser->lexer);
20666 token = cp_lexer_peek_token (parser->lexer);
20667 t = cp_parser_assignment_expression (parser, false, NULL);
20669 if (t == error_mark_node)
20671 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20672 error ("%Hschedule %<runtime%> does not take "
20673 "a %<chunk_size%> parameter", &token->location);
20674 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20675 error ("%Hschedule %<auto%> does not take "
20676 "a %<chunk_size%> parameter", &token->location);
20678 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20680 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20683 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20686 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20687 OMP_CLAUSE_CHAIN (c) = list;
20691 cp_parser_error (parser, "invalid schedule kind");
20693 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20694 /*or_comma=*/false,
20695 /*consume_paren=*/true);
20703 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20704 tree list, location_t location)
20708 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20710 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20711 OMP_CLAUSE_CHAIN (c) = list;
20715 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20716 is a bitmask in MASK. Return the list of clauses found; the result
20717 of clause default goes in *pdefault. */
20720 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20721 const char *where, cp_token *pragma_tok)
20723 tree clauses = NULL;
20725 cp_token *token = NULL;
20727 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20729 pragma_omp_clause c_kind;
20730 const char *c_name;
20731 tree prev = clauses;
20733 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20734 cp_lexer_consume_token (parser->lexer);
20736 token = cp_lexer_peek_token (parser->lexer);
20737 c_kind = cp_parser_omp_clause_name (parser);
20742 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20743 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20745 c_name = "collapse";
20747 case PRAGMA_OMP_CLAUSE_COPYIN:
20748 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20751 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20752 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20754 c_name = "copyprivate";
20756 case PRAGMA_OMP_CLAUSE_DEFAULT:
20757 clauses = cp_parser_omp_clause_default (parser, clauses,
20759 c_name = "default";
20761 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20762 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20764 c_name = "firstprivate";
20766 case PRAGMA_OMP_CLAUSE_IF:
20767 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20770 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20771 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20773 c_name = "lastprivate";
20775 case PRAGMA_OMP_CLAUSE_NOWAIT:
20776 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20779 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20780 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20782 c_name = "num_threads";
20784 case PRAGMA_OMP_CLAUSE_ORDERED:
20785 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20787 c_name = "ordered";
20789 case PRAGMA_OMP_CLAUSE_PRIVATE:
20790 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20792 c_name = "private";
20794 case PRAGMA_OMP_CLAUSE_REDUCTION:
20795 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20796 c_name = "reduction";
20798 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20799 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20801 c_name = "schedule";
20803 case PRAGMA_OMP_CLAUSE_SHARED:
20804 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20808 case PRAGMA_OMP_CLAUSE_UNTIED:
20809 clauses = cp_parser_omp_clause_untied (parser, clauses,
20814 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20818 if (((mask >> c_kind) & 1) == 0)
20820 /* Remove the invalid clause(s) from the list to avoid
20821 confusing the rest of the compiler. */
20823 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20827 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20828 return finish_omp_clauses (clauses);
20835 In practice, we're also interested in adding the statement to an
20836 outer node. So it is convenient if we work around the fact that
20837 cp_parser_statement calls add_stmt. */
20840 cp_parser_begin_omp_structured_block (cp_parser *parser)
20842 unsigned save = parser->in_statement;
20844 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20845 This preserves the "not within loop or switch" style error messages
20846 for nonsense cases like
20852 if (parser->in_statement)
20853 parser->in_statement = IN_OMP_BLOCK;
20859 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20861 parser->in_statement = save;
20865 cp_parser_omp_structured_block (cp_parser *parser)
20867 tree stmt = begin_omp_structured_block ();
20868 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20870 cp_parser_statement (parser, NULL_TREE, false, NULL);
20872 cp_parser_end_omp_structured_block (parser, save);
20873 return finish_omp_structured_block (stmt);
20877 # pragma omp atomic new-line
20881 x binop= expr | x++ | ++x | x-- | --x
20883 +, *, -, /, &, ^, |, <<, >>
20885 where x is an lvalue expression with scalar type. */
20888 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20891 enum tree_code code;
20893 cp_parser_require_pragma_eol (parser, pragma_tok);
20895 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20896 /*cast_p=*/false, NULL);
20897 switch (TREE_CODE (lhs))
20902 case PREINCREMENT_EXPR:
20903 case POSTINCREMENT_EXPR:
20904 lhs = TREE_OPERAND (lhs, 0);
20906 rhs = integer_one_node;
20909 case PREDECREMENT_EXPR:
20910 case POSTDECREMENT_EXPR:
20911 lhs = TREE_OPERAND (lhs, 0);
20913 rhs = integer_one_node;
20917 switch (cp_lexer_peek_token (parser->lexer)->type)
20923 code = TRUNC_DIV_EXPR;
20931 case CPP_LSHIFT_EQ:
20932 code = LSHIFT_EXPR;
20934 case CPP_RSHIFT_EQ:
20935 code = RSHIFT_EXPR;
20938 code = BIT_AND_EXPR;
20941 code = BIT_IOR_EXPR;
20944 code = BIT_XOR_EXPR;
20947 cp_parser_error (parser,
20948 "invalid operator for %<#pragma omp atomic%>");
20951 cp_lexer_consume_token (parser->lexer);
20953 rhs = cp_parser_expression (parser, false, NULL);
20954 if (rhs == error_mark_node)
20958 finish_omp_atomic (code, lhs, rhs);
20959 cp_parser_consume_semicolon_at_end_of_statement (parser);
20963 cp_parser_skip_to_end_of_block_or_statement (parser);
20968 # pragma omp barrier new-line */
20971 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20973 cp_parser_require_pragma_eol (parser, pragma_tok);
20974 finish_omp_barrier ();
20978 # pragma omp critical [(name)] new-line
20979 structured-block */
20982 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20984 tree stmt, name = NULL;
20986 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20988 cp_lexer_consume_token (parser->lexer);
20990 name = cp_parser_identifier (parser);
20992 if (name == error_mark_node
20993 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20994 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20995 /*or_comma=*/false,
20996 /*consume_paren=*/true);
20997 if (name == error_mark_node)
21000 cp_parser_require_pragma_eol (parser, pragma_tok);
21002 stmt = cp_parser_omp_structured_block (parser);
21003 return c_finish_omp_critical (stmt, name);
21007 # pragma omp flush flush-vars[opt] new-line
21010 ( variable-list ) */
21013 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21015 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21016 (void) cp_parser_omp_var_list (parser, 0, NULL);
21017 cp_parser_require_pragma_eol (parser, pragma_tok);
21019 finish_omp_flush ();
21022 /* Helper function, to parse omp for increment expression. */
21025 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21027 tree cond = cp_parser_binary_expression (parser, false, true,
21028 PREC_NOT_OPERATOR, NULL);
21031 if (cond == error_mark_node
21032 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21034 cp_parser_skip_to_end_of_statement (parser);
21035 return error_mark_node;
21038 switch (TREE_CODE (cond))
21046 return error_mark_node;
21049 /* If decl is an iterator, preserve LHS and RHS of the relational
21050 expr until finish_omp_for. */
21052 && (type_dependent_expression_p (decl)
21053 || CLASS_TYPE_P (TREE_TYPE (decl))))
21056 return build_x_binary_op (TREE_CODE (cond),
21057 TREE_OPERAND (cond, 0), ERROR_MARK,
21058 TREE_OPERAND (cond, 1), ERROR_MARK,
21059 &overloaded_p, tf_warning_or_error);
21062 /* Helper function, to parse omp for increment expression. */
21065 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21067 cp_token *token = cp_lexer_peek_token (parser->lexer);
21073 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21075 op = (token->type == CPP_PLUS_PLUS
21076 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21077 cp_lexer_consume_token (parser->lexer);
21078 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21080 return error_mark_node;
21081 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21084 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21086 return error_mark_node;
21088 token = cp_lexer_peek_token (parser->lexer);
21089 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21091 op = (token->type == CPP_PLUS_PLUS
21092 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21093 cp_lexer_consume_token (parser->lexer);
21094 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21097 op = cp_parser_assignment_operator_opt (parser);
21098 if (op == ERROR_MARK)
21099 return error_mark_node;
21101 if (op != NOP_EXPR)
21103 rhs = cp_parser_assignment_expression (parser, false, NULL);
21104 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21105 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21108 lhs = cp_parser_binary_expression (parser, false, false,
21109 PREC_ADDITIVE_EXPRESSION, NULL);
21110 token = cp_lexer_peek_token (parser->lexer);
21111 decl_first = lhs == decl;
21114 if (token->type != CPP_PLUS
21115 && token->type != CPP_MINUS)
21116 return error_mark_node;
21120 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21121 cp_lexer_consume_token (parser->lexer);
21122 rhs = cp_parser_binary_expression (parser, false, false,
21123 PREC_ADDITIVE_EXPRESSION, NULL);
21124 token = cp_lexer_peek_token (parser->lexer);
21125 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21127 if (lhs == NULL_TREE)
21129 if (op == PLUS_EXPR)
21132 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21135 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21136 NULL, tf_warning_or_error);
21139 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21143 if (rhs != decl || op == MINUS_EXPR)
21144 return error_mark_node;
21145 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21148 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21150 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21153 /* Parse the restricted form of the for statement allowed by OpenMP. */
21156 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21158 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21159 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21160 tree this_pre_body, cl;
21161 location_t loc_first;
21162 bool collapse_err = false;
21163 int i, collapse = 1, nbraces = 0;
21165 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21166 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21167 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21169 gcc_assert (collapse >= 1);
21171 declv = make_tree_vec (collapse);
21172 initv = make_tree_vec (collapse);
21173 condv = make_tree_vec (collapse);
21174 incrv = make_tree_vec (collapse);
21176 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21178 for (i = 0; i < collapse; i++)
21180 int bracecount = 0;
21181 bool add_private_clause = false;
21184 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21186 cp_parser_error (parser, "for statement expected");
21189 loc = cp_lexer_consume_token (parser->lexer)->location;
21191 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21194 init = decl = real_decl = NULL;
21195 this_pre_body = push_stmt_list ();
21196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21198 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21202 integer-type var = lb
21203 random-access-iterator-type var = lb
21204 pointer-type var = lb
21206 cp_decl_specifier_seq type_specifiers;
21208 /* First, try to parse as an initialized declaration. See
21209 cp_parser_condition, from whence the bulk of this is copied. */
21211 cp_parser_parse_tentatively (parser);
21212 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21214 if (cp_parser_parse_definitely (parser))
21216 /* If parsing a type specifier seq succeeded, then this
21217 MUST be a initialized declaration. */
21218 tree asm_specification, attributes;
21219 cp_declarator *declarator;
21221 declarator = cp_parser_declarator (parser,
21222 CP_PARSER_DECLARATOR_NAMED,
21223 /*ctor_dtor_or_conv_p=*/NULL,
21224 /*parenthesized_p=*/NULL,
21225 /*member_p=*/false);
21226 attributes = cp_parser_attributes_opt (parser);
21227 asm_specification = cp_parser_asm_specification_opt (parser);
21229 if (declarator == cp_error_declarator)
21230 cp_parser_skip_to_end_of_statement (parser);
21234 tree pushed_scope, auto_node;
21236 decl = start_decl (declarator, &type_specifiers,
21237 SD_INITIALIZED, attributes,
21238 /*prefix_attributes=*/NULL_TREE,
21241 auto_node = type_uses_auto (TREE_TYPE (decl));
21242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21244 if (cp_lexer_next_token_is (parser->lexer,
21246 error ("parenthesized initialization is not allowed in "
21247 "OpenMP %<for%> loop");
21249 /* Trigger an error. */
21250 cp_parser_require (parser, CPP_EQ, "%<=%>");
21252 init = error_mark_node;
21253 cp_parser_skip_to_end_of_statement (parser);
21255 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21256 || type_dependent_expression_p (decl)
21259 bool is_direct_init, is_non_constant_init;
21261 init = cp_parser_initializer (parser,
21263 &is_non_constant_init);
21265 if (auto_node && describable_type (init))
21268 = do_auto_deduction (TREE_TYPE (decl), init,
21271 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21272 && !type_dependent_expression_p (decl))
21276 cp_finish_decl (decl, init, !is_non_constant_init,
21278 LOOKUP_ONLYCONVERTING);
21279 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21282 = tree_cons (NULL, this_pre_body, for_block);
21286 init = pop_stmt_list (this_pre_body);
21287 this_pre_body = NULL_TREE;
21292 cp_lexer_consume_token (parser->lexer);
21293 init = cp_parser_assignment_expression (parser, false, NULL);
21296 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21297 init = error_mark_node;
21299 cp_finish_decl (decl, NULL_TREE,
21300 /*init_const_expr_p=*/false,
21302 LOOKUP_ONLYCONVERTING);
21306 pop_scope (pushed_scope);
21312 /* If parsing a type specifier sequence failed, then
21313 this MUST be a simple expression. */
21314 cp_parser_parse_tentatively (parser);
21315 decl = cp_parser_primary_expression (parser, false, false,
21317 if (!cp_parser_error_occurred (parser)
21320 && CLASS_TYPE_P (TREE_TYPE (decl)))
21324 cp_parser_parse_definitely (parser);
21325 cp_parser_require (parser, CPP_EQ, "%<=%>");
21326 rhs = cp_parser_assignment_expression (parser, false, NULL);
21327 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21329 tf_warning_or_error));
21330 add_private_clause = true;
21335 cp_parser_abort_tentative_parse (parser);
21336 init = cp_parser_expression (parser, false, NULL);
21339 if (TREE_CODE (init) == MODIFY_EXPR
21340 || TREE_CODE (init) == MODOP_EXPR)
21341 real_decl = TREE_OPERAND (init, 0);
21346 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21349 this_pre_body = pop_stmt_list (this_pre_body);
21353 pre_body = push_stmt_list ();
21355 add_stmt (this_pre_body);
21356 pre_body = pop_stmt_list (pre_body);
21359 pre_body = this_pre_body;
21364 if (par_clauses != NULL && real_decl != NULL_TREE)
21367 for (c = par_clauses; *c ; )
21368 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21369 && OMP_CLAUSE_DECL (*c) == real_decl)
21371 error ("%Hiteration variable %qD should not be firstprivate",
21373 *c = OMP_CLAUSE_CHAIN (*c);
21375 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21376 && OMP_CLAUSE_DECL (*c) == real_decl)
21378 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21379 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21380 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21381 OMP_CLAUSE_DECL (l) = real_decl;
21382 OMP_CLAUSE_CHAIN (l) = clauses;
21383 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21385 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21386 CP_OMP_CLAUSE_INFO (*c) = NULL;
21387 add_private_clause = false;
21391 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21392 && OMP_CLAUSE_DECL (*c) == real_decl)
21393 add_private_clause = false;
21394 c = &OMP_CLAUSE_CHAIN (*c);
21398 if (add_private_clause)
21401 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21403 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21404 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21405 && OMP_CLAUSE_DECL (c) == decl)
21407 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21408 && OMP_CLAUSE_DECL (c) == decl)
21409 error ("%Hiteration variable %qD should not be firstprivate",
21411 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21412 && OMP_CLAUSE_DECL (c) == decl)
21413 error ("%Hiteration variable %qD should not be reduction",
21418 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21419 OMP_CLAUSE_DECL (c) = decl;
21420 c = finish_omp_clauses (c);
21423 OMP_CLAUSE_CHAIN (c) = clauses;
21430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21431 cond = cp_parser_omp_for_cond (parser, decl);
21432 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21437 /* If decl is an iterator, preserve the operator on decl
21438 until finish_omp_for. */
21440 && (type_dependent_expression_p (decl)
21441 || CLASS_TYPE_P (TREE_TYPE (decl))))
21442 incr = cp_parser_omp_for_incr (parser, decl);
21444 incr = cp_parser_expression (parser, false, NULL);
21447 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21448 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21449 /*or_comma=*/false,
21450 /*consume_paren=*/true);
21452 TREE_VEC_ELT (declv, i) = decl;
21453 TREE_VEC_ELT (initv, i) = init;
21454 TREE_VEC_ELT (condv, i) = cond;
21455 TREE_VEC_ELT (incrv, i) = incr;
21457 if (i == collapse - 1)
21460 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21461 in between the collapsed for loops to be still considered perfectly
21462 nested. Hopefully the final version clarifies this.
21463 For now handle (multiple) {'s and empty statements. */
21464 cp_parser_parse_tentatively (parser);
21467 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21469 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21471 cp_lexer_consume_token (parser->lexer);
21474 else if (bracecount
21475 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21476 cp_lexer_consume_token (parser->lexer);
21479 loc = cp_lexer_peek_token (parser->lexer)->location;
21480 error ("%Hnot enough collapsed for loops", &loc);
21481 collapse_err = true;
21482 cp_parser_abort_tentative_parse (parser);
21491 cp_parser_parse_definitely (parser);
21492 nbraces += bracecount;
21496 /* Note that we saved the original contents of this flag when we entered
21497 the structured block, and so we don't need to re-save it here. */
21498 parser->in_statement = IN_OMP_FOR;
21500 /* Note that the grammar doesn't call for a structured block here,
21501 though the loop as a whole is a structured block. */
21502 body = push_stmt_list ();
21503 cp_parser_statement (parser, NULL_TREE, false, NULL);
21504 body = pop_stmt_list (body);
21506 if (declv == NULL_TREE)
21509 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21510 pre_body, clauses);
21514 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21516 cp_lexer_consume_token (parser->lexer);
21519 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21520 cp_lexer_consume_token (parser->lexer);
21525 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21526 error ("%Hcollapsed loops not perfectly nested", &loc);
21528 collapse_err = true;
21529 cp_parser_statement_seq_opt (parser, NULL);
21530 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21536 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21537 for_block = TREE_CHAIN (for_block);
21544 #pragma omp for for-clause[optseq] new-line
21547 #define OMP_FOR_CLAUSE_MASK \
21548 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21549 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21550 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21551 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21552 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21553 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21554 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21555 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21558 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21560 tree clauses, sb, ret;
21563 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21564 "#pragma omp for", pragma_tok);
21566 sb = begin_omp_structured_block ();
21567 save = cp_parser_begin_omp_structured_block (parser);
21569 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21571 cp_parser_end_omp_structured_block (parser, save);
21572 add_stmt (finish_omp_structured_block (sb));
21578 # pragma omp master new-line
21579 structured-block */
21582 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21584 cp_parser_require_pragma_eol (parser, pragma_tok);
21585 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21589 # pragma omp ordered new-line
21590 structured-block */
21593 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21595 cp_parser_require_pragma_eol (parser, pragma_tok);
21596 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21602 { section-sequence }
21605 section-directive[opt] structured-block
21606 section-sequence section-directive structured-block */
21609 cp_parser_omp_sections_scope (cp_parser *parser)
21611 tree stmt, substmt;
21612 bool error_suppress = false;
21615 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21618 stmt = push_stmt_list ();
21620 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21624 substmt = begin_omp_structured_block ();
21625 save = cp_parser_begin_omp_structured_block (parser);
21629 cp_parser_statement (parser, NULL_TREE, false, NULL);
21631 tok = cp_lexer_peek_token (parser->lexer);
21632 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21634 if (tok->type == CPP_CLOSE_BRACE)
21636 if (tok->type == CPP_EOF)
21640 cp_parser_end_omp_structured_block (parser, save);
21641 substmt = finish_omp_structured_block (substmt);
21642 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21643 add_stmt (substmt);
21648 tok = cp_lexer_peek_token (parser->lexer);
21649 if (tok->type == CPP_CLOSE_BRACE)
21651 if (tok->type == CPP_EOF)
21654 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21656 cp_lexer_consume_token (parser->lexer);
21657 cp_parser_require_pragma_eol (parser, tok);
21658 error_suppress = false;
21660 else if (!error_suppress)
21662 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21663 error_suppress = true;
21666 substmt = cp_parser_omp_structured_block (parser);
21667 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21668 add_stmt (substmt);
21670 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21672 substmt = pop_stmt_list (stmt);
21674 stmt = make_node (OMP_SECTIONS);
21675 TREE_TYPE (stmt) = void_type_node;
21676 OMP_SECTIONS_BODY (stmt) = substmt;
21683 # pragma omp sections sections-clause[optseq] newline
21686 #define OMP_SECTIONS_CLAUSE_MASK \
21687 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21688 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21689 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21690 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21691 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21694 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21698 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21699 "#pragma omp sections", pragma_tok);
21701 ret = cp_parser_omp_sections_scope (parser);
21703 OMP_SECTIONS_CLAUSES (ret) = clauses;
21709 # pragma parallel parallel-clause new-line
21710 # pragma parallel for parallel-for-clause new-line
21711 # pragma parallel sections parallel-sections-clause new-line */
21713 #define OMP_PARALLEL_CLAUSE_MASK \
21714 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21715 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21716 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21717 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21718 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21719 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21720 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21721 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21724 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21726 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21727 const char *p_name = "#pragma omp parallel";
21728 tree stmt, clauses, par_clause, ws_clause, block;
21729 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21732 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21734 cp_lexer_consume_token (parser->lexer);
21735 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21736 p_name = "#pragma omp parallel for";
21737 mask |= OMP_FOR_CLAUSE_MASK;
21738 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21740 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21742 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21743 const char *p = IDENTIFIER_POINTER (id);
21744 if (strcmp (p, "sections") == 0)
21746 cp_lexer_consume_token (parser->lexer);
21747 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21748 p_name = "#pragma omp parallel sections";
21749 mask |= OMP_SECTIONS_CLAUSE_MASK;
21750 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21754 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21755 block = begin_omp_parallel ();
21756 save = cp_parser_begin_omp_structured_block (parser);
21760 case PRAGMA_OMP_PARALLEL:
21761 cp_parser_statement (parser, NULL_TREE, false, NULL);
21762 par_clause = clauses;
21765 case PRAGMA_OMP_PARALLEL_FOR:
21766 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21767 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21770 case PRAGMA_OMP_PARALLEL_SECTIONS:
21771 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21772 stmt = cp_parser_omp_sections_scope (parser);
21774 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21778 gcc_unreachable ();
21781 cp_parser_end_omp_structured_block (parser, save);
21782 stmt = finish_omp_parallel (par_clause, block);
21783 if (p_kind != PRAGMA_OMP_PARALLEL)
21784 OMP_PARALLEL_COMBINED (stmt) = 1;
21789 # pragma omp single single-clause[optseq] new-line
21790 structured-block */
21792 #define OMP_SINGLE_CLAUSE_MASK \
21793 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21794 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21795 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21796 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21799 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21801 tree stmt = make_node (OMP_SINGLE);
21802 TREE_TYPE (stmt) = void_type_node;
21804 OMP_SINGLE_CLAUSES (stmt)
21805 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21806 "#pragma omp single", pragma_tok);
21807 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21809 return add_stmt (stmt);
21813 # pragma omp task task-clause[optseq] new-line
21814 structured-block */
21816 #define OMP_TASK_CLAUSE_MASK \
21817 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21818 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21819 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21820 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21821 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21822 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21825 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21827 tree clauses, block;
21830 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21831 "#pragma omp task", pragma_tok);
21832 block = begin_omp_task ();
21833 save = cp_parser_begin_omp_structured_block (parser);
21834 cp_parser_statement (parser, NULL_TREE, false, NULL);
21835 cp_parser_end_omp_structured_block (parser, save);
21836 return finish_omp_task (clauses, block);
21840 # pragma omp taskwait new-line */
21843 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21845 cp_parser_require_pragma_eol (parser, pragma_tok);
21846 finish_omp_taskwait ();
21850 # pragma omp threadprivate (variable-list) */
21853 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21857 vars = cp_parser_omp_var_list (parser, 0, NULL);
21858 cp_parser_require_pragma_eol (parser, pragma_tok);
21860 finish_omp_threadprivate (vars);
21863 /* Main entry point to OpenMP statement pragmas. */
21866 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21870 switch (pragma_tok->pragma_kind)
21872 case PRAGMA_OMP_ATOMIC:
21873 cp_parser_omp_atomic (parser, pragma_tok);
21875 case PRAGMA_OMP_CRITICAL:
21876 stmt = cp_parser_omp_critical (parser, pragma_tok);
21878 case PRAGMA_OMP_FOR:
21879 stmt = cp_parser_omp_for (parser, pragma_tok);
21881 case PRAGMA_OMP_MASTER:
21882 stmt = cp_parser_omp_master (parser, pragma_tok);
21884 case PRAGMA_OMP_ORDERED:
21885 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21887 case PRAGMA_OMP_PARALLEL:
21888 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21890 case PRAGMA_OMP_SECTIONS:
21891 stmt = cp_parser_omp_sections (parser, pragma_tok);
21893 case PRAGMA_OMP_SINGLE:
21894 stmt = cp_parser_omp_single (parser, pragma_tok);
21896 case PRAGMA_OMP_TASK:
21897 stmt = cp_parser_omp_task (parser, pragma_tok);
21900 gcc_unreachable ();
21904 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21909 static GTY (()) cp_parser *the_parser;
21912 /* Special handling for the first token or line in the file. The first
21913 thing in the file might be #pragma GCC pch_preprocess, which loads a
21914 PCH file, which is a GC collection point. So we need to handle this
21915 first pragma without benefit of an existing lexer structure.
21917 Always returns one token to the caller in *FIRST_TOKEN. This is
21918 either the true first token of the file, or the first token after
21919 the initial pragma. */
21922 cp_parser_initial_pragma (cp_token *first_token)
21926 cp_lexer_get_preprocessor_token (NULL, first_token);
21927 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21930 cp_lexer_get_preprocessor_token (NULL, first_token);
21931 if (first_token->type == CPP_STRING)
21933 name = first_token->u.value;
21935 cp_lexer_get_preprocessor_token (NULL, first_token);
21936 if (first_token->type != CPP_PRAGMA_EOL)
21937 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21938 &first_token->location);
21941 error ("%Hexpected string literal", &first_token->location);
21943 /* Skip to the end of the pragma. */
21944 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21945 cp_lexer_get_preprocessor_token (NULL, first_token);
21947 /* Now actually load the PCH file. */
21949 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21951 /* Read one more token to return to our caller. We have to do this
21952 after reading the PCH file in, since its pointers have to be
21954 cp_lexer_get_preprocessor_token (NULL, first_token);
21957 /* Normal parsing of a pragma token. Here we can (and must) use the
21961 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21963 cp_token *pragma_tok;
21966 pragma_tok = cp_lexer_consume_token (parser->lexer);
21967 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21968 parser->lexer->in_pragma = true;
21970 id = pragma_tok->pragma_kind;
21973 case PRAGMA_GCC_PCH_PREPROCESS:
21974 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21975 &pragma_tok->location);
21978 case PRAGMA_OMP_BARRIER:
21981 case pragma_compound:
21982 cp_parser_omp_barrier (parser, pragma_tok);
21985 error ("%H%<#pragma omp barrier%> may only be "
21986 "used in compound statements", &pragma_tok->location);
21993 case PRAGMA_OMP_FLUSH:
21996 case pragma_compound:
21997 cp_parser_omp_flush (parser, pragma_tok);
22000 error ("%H%<#pragma omp flush%> may only be "
22001 "used in compound statements", &pragma_tok->location);
22008 case PRAGMA_OMP_TASKWAIT:
22011 case pragma_compound:
22012 cp_parser_omp_taskwait (parser, pragma_tok);
22015 error ("%H%<#pragma omp taskwait%> may only be "
22016 "used in compound statements",
22017 &pragma_tok->location);
22024 case PRAGMA_OMP_THREADPRIVATE:
22025 cp_parser_omp_threadprivate (parser, pragma_tok);
22028 case PRAGMA_OMP_ATOMIC:
22029 case PRAGMA_OMP_CRITICAL:
22030 case PRAGMA_OMP_FOR:
22031 case PRAGMA_OMP_MASTER:
22032 case PRAGMA_OMP_ORDERED:
22033 case PRAGMA_OMP_PARALLEL:
22034 case PRAGMA_OMP_SECTIONS:
22035 case PRAGMA_OMP_SINGLE:
22036 case PRAGMA_OMP_TASK:
22037 if (context == pragma_external)
22039 cp_parser_omp_construct (parser, pragma_tok);
22042 case PRAGMA_OMP_SECTION:
22043 error ("%H%<#pragma omp section%> may only be used in "
22044 "%<#pragma omp sections%> construct", &pragma_tok->location);
22048 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22049 c_invoke_pragma_handler (id);
22053 cp_parser_error (parser, "expected declaration specifiers");
22057 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22061 /* The interface the pragma parsers have to the lexer. */
22064 pragma_lex (tree *value)
22067 enum cpp_ttype ret;
22069 tok = cp_lexer_peek_token (the_parser->lexer);
22072 *value = tok->u.value;
22074 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22076 else if (ret == CPP_STRING)
22077 *value = cp_parser_string_literal (the_parser, false, false);
22080 cp_lexer_consume_token (the_parser->lexer);
22081 if (ret == CPP_KEYWORD)
22089 /* External interface. */
22091 /* Parse one entire translation unit. */
22094 c_parse_file (void)
22096 bool error_occurred;
22097 static bool already_called = false;
22099 if (already_called)
22101 sorry ("inter-module optimizations not implemented for C++");
22104 already_called = true;
22106 the_parser = cp_parser_new ();
22107 push_deferring_access_checks (flag_access_control
22108 ? dk_no_deferred : dk_no_check);
22109 error_occurred = cp_parser_translation_unit (the_parser);
22113 #include "gt-cp-parser.h"