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 tree cp_parser_template_type_arg
1737 static tree cp_parser_type_id_1
1738 (cp_parser *, bool);
1739 static void cp_parser_type_specifier_seq
1740 (cp_parser *, bool, cp_decl_specifier_seq *);
1741 static tree cp_parser_parameter_declaration_clause
1743 static tree cp_parser_parameter_declaration_list
1744 (cp_parser *, bool *);
1745 static cp_parameter_declarator *cp_parser_parameter_declaration
1746 (cp_parser *, bool, bool *);
1747 static tree cp_parser_default_argument
1748 (cp_parser *, bool);
1749 static void cp_parser_function_body
1751 static tree cp_parser_initializer
1752 (cp_parser *, bool *, bool *);
1753 static tree cp_parser_initializer_clause
1754 (cp_parser *, bool *);
1755 static tree cp_parser_braced_list
1756 (cp_parser*, bool*);
1757 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1758 (cp_parser *, bool *);
1760 static bool cp_parser_ctor_initializer_opt_and_function_body
1763 /* Classes [gram.class] */
1765 static tree cp_parser_class_name
1766 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1767 static tree cp_parser_class_specifier
1769 static tree cp_parser_class_head
1770 (cp_parser *, bool *, tree *, tree *);
1771 static enum tag_types cp_parser_class_key
1773 static void cp_parser_member_specification_opt
1775 static void cp_parser_member_declaration
1777 static tree cp_parser_pure_specifier
1779 static tree cp_parser_constant_initializer
1782 /* Derived classes [gram.class.derived] */
1784 static tree cp_parser_base_clause
1786 static tree cp_parser_base_specifier
1789 /* Special member functions [gram.special] */
1791 static tree cp_parser_conversion_function_id
1793 static tree cp_parser_conversion_type_id
1795 static cp_declarator *cp_parser_conversion_declarator_opt
1797 static bool cp_parser_ctor_initializer_opt
1799 static void cp_parser_mem_initializer_list
1801 static tree cp_parser_mem_initializer
1803 static tree cp_parser_mem_initializer_id
1806 /* Overloading [gram.over] */
1808 static tree cp_parser_operator_function_id
1810 static tree cp_parser_operator
1813 /* Templates [gram.temp] */
1815 static void cp_parser_template_declaration
1816 (cp_parser *, bool);
1817 static tree cp_parser_template_parameter_list
1819 static tree cp_parser_template_parameter
1820 (cp_parser *, bool *, bool *);
1821 static tree cp_parser_type_parameter
1822 (cp_parser *, bool *);
1823 static tree cp_parser_template_id
1824 (cp_parser *, bool, bool, bool);
1825 static tree cp_parser_template_name
1826 (cp_parser *, bool, bool, bool, bool *);
1827 static tree cp_parser_template_argument_list
1829 static tree cp_parser_template_argument
1831 static void cp_parser_explicit_instantiation
1833 static void cp_parser_explicit_specialization
1836 /* Exception handling [gram.exception] */
1838 static tree cp_parser_try_block
1840 static bool cp_parser_function_try_block
1842 static void cp_parser_handler_seq
1844 static void cp_parser_handler
1846 static tree cp_parser_exception_declaration
1848 static tree cp_parser_throw_expression
1850 static tree cp_parser_exception_specification_opt
1852 static tree cp_parser_type_id_list
1855 /* GNU Extensions */
1857 static tree cp_parser_asm_specification_opt
1859 static tree cp_parser_asm_operand_list
1861 static tree cp_parser_asm_clobber_list
1863 static tree cp_parser_attributes_opt
1865 static tree cp_parser_attribute_list
1867 static bool cp_parser_extension_opt
1868 (cp_parser *, int *);
1869 static void cp_parser_label_declaration
1872 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1873 static bool cp_parser_pragma
1874 (cp_parser *, enum pragma_context);
1876 /* Objective-C++ Productions */
1878 static tree cp_parser_objc_message_receiver
1880 static tree cp_parser_objc_message_args
1882 static tree cp_parser_objc_message_expression
1884 static tree cp_parser_objc_encode_expression
1886 static tree cp_parser_objc_defs_expression
1888 static tree cp_parser_objc_protocol_expression
1890 static tree cp_parser_objc_selector_expression
1892 static tree cp_parser_objc_expression
1894 static bool cp_parser_objc_selector_p
1896 static tree cp_parser_objc_selector
1898 static tree cp_parser_objc_protocol_refs_opt
1900 static void cp_parser_objc_declaration
1902 static tree cp_parser_objc_statement
1905 /* Utility Routines */
1907 static tree cp_parser_lookup_name
1908 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1909 static tree cp_parser_lookup_name_simple
1910 (cp_parser *, tree, location_t);
1911 static tree cp_parser_maybe_treat_template_as_class
1913 static bool cp_parser_check_declarator_template_parameters
1914 (cp_parser *, cp_declarator *, location_t);
1915 static bool cp_parser_check_template_parameters
1916 (cp_parser *, unsigned, location_t);
1917 static tree cp_parser_simple_cast_expression
1919 static tree cp_parser_global_scope_opt
1920 (cp_parser *, bool);
1921 static bool cp_parser_constructor_declarator_p
1922 (cp_parser *, bool);
1923 static tree cp_parser_function_definition_from_specifiers_and_declarator
1924 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1925 static tree cp_parser_function_definition_after_declarator
1926 (cp_parser *, bool);
1927 static void cp_parser_template_declaration_after_export
1928 (cp_parser *, bool);
1929 static void cp_parser_perform_template_parameter_access_checks
1930 (VEC (deferred_access_check,gc)*);
1931 static tree cp_parser_single_declaration
1932 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1933 static tree cp_parser_functional_cast
1934 (cp_parser *, tree);
1935 static tree cp_parser_save_member_function_body
1936 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1937 static tree cp_parser_enclosed_template_argument_list
1939 static void cp_parser_save_default_args
1940 (cp_parser *, tree);
1941 static void cp_parser_late_parsing_for_member
1942 (cp_parser *, tree);
1943 static void cp_parser_late_parsing_default_args
1944 (cp_parser *, tree);
1945 static tree cp_parser_sizeof_operand
1946 (cp_parser *, enum rid);
1947 static tree cp_parser_trait_expr
1948 (cp_parser *, enum rid);
1949 static bool cp_parser_declares_only_class_p
1951 static void cp_parser_set_storage_class
1952 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1953 static void cp_parser_set_decl_spec_type
1954 (cp_decl_specifier_seq *, tree, location_t, bool);
1955 static bool cp_parser_friend_p
1956 (const cp_decl_specifier_seq *);
1957 static cp_token *cp_parser_require
1958 (cp_parser *, enum cpp_ttype, const char *);
1959 static cp_token *cp_parser_require_keyword
1960 (cp_parser *, enum rid, const char *);
1961 static bool cp_parser_token_starts_function_definition_p
1963 static bool cp_parser_next_token_starts_class_definition_p
1965 static bool cp_parser_next_token_ends_template_argument_p
1967 static bool cp_parser_nth_token_starts_template_argument_list_p
1968 (cp_parser *, size_t);
1969 static enum tag_types cp_parser_token_is_class_key
1971 static void cp_parser_check_class_key
1972 (enum tag_types, tree type);
1973 static void cp_parser_check_access_in_redeclaration
1974 (tree type, location_t location);
1975 static bool cp_parser_optional_template_keyword
1977 static void cp_parser_pre_parsed_nested_name_specifier
1979 static bool cp_parser_cache_group
1980 (cp_parser *, enum cpp_ttype, unsigned);
1981 static void cp_parser_parse_tentatively
1983 static void cp_parser_commit_to_tentative_parse
1985 static void cp_parser_abort_tentative_parse
1987 static bool cp_parser_parse_definitely
1989 static inline bool cp_parser_parsing_tentatively
1991 static bool cp_parser_uncommitted_to_tentative_parse_p
1993 static void cp_parser_error
1994 (cp_parser *, const char *);
1995 static void cp_parser_name_lookup_error
1996 (cp_parser *, tree, tree, const char *, location_t);
1997 static bool cp_parser_simulate_error
1999 static bool cp_parser_check_type_definition
2001 static void cp_parser_check_for_definition_in_return_type
2002 (cp_declarator *, tree, location_t type_location);
2003 static void cp_parser_check_for_invalid_template_id
2004 (cp_parser *, tree, location_t location);
2005 static bool cp_parser_non_integral_constant_expression
2006 (cp_parser *, const char *);
2007 static void cp_parser_diagnose_invalid_type_name
2008 (cp_parser *, tree, tree, location_t);
2009 static bool cp_parser_parse_and_diagnose_invalid_type_name
2011 static int cp_parser_skip_to_closing_parenthesis
2012 (cp_parser *, bool, bool, bool);
2013 static void cp_parser_skip_to_end_of_statement
2015 static void cp_parser_consume_semicolon_at_end_of_statement
2017 static void cp_parser_skip_to_end_of_block_or_statement
2019 static bool cp_parser_skip_to_closing_brace
2021 static void cp_parser_skip_to_end_of_template_parameter_list
2023 static void cp_parser_skip_to_pragma_eol
2024 (cp_parser*, cp_token *);
2025 static bool cp_parser_error_occurred
2027 static bool cp_parser_allow_gnu_extensions_p
2029 static bool cp_parser_is_string_literal
2031 static bool cp_parser_is_keyword
2032 (cp_token *, enum rid);
2033 static tree cp_parser_make_typename_type
2034 (cp_parser *, tree, tree, location_t location);
2035 static cp_declarator * cp_parser_make_indirect_declarator
2036 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2038 /* Returns nonzero if we are parsing tentatively. */
2041 cp_parser_parsing_tentatively (cp_parser* parser)
2043 return parser->context->next != NULL;
2046 /* Returns nonzero if TOKEN is a string literal. */
2049 cp_parser_is_string_literal (cp_token* token)
2051 return (token->type == CPP_STRING ||
2052 token->type == CPP_STRING16 ||
2053 token->type == CPP_STRING32 ||
2054 token->type == CPP_WSTRING);
2057 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2060 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2062 return token->keyword == keyword;
2065 /* If not parsing tentatively, issue a diagnostic of the form
2066 FILE:LINE: MESSAGE before TOKEN
2067 where TOKEN is the next token in the input stream. MESSAGE
2068 (specified by the caller) is usually of the form "expected
2072 cp_parser_error (cp_parser* parser, const char* message)
2074 if (!cp_parser_simulate_error (parser))
2076 cp_token *token = cp_lexer_peek_token (parser->lexer);
2077 /* This diagnostic makes more sense if it is tagged to the line
2078 of the token we just peeked at. */
2079 cp_lexer_set_source_position_from_token (token);
2081 if (token->type == CPP_PRAGMA)
2083 error ("%H%<#pragma%> is not allowed here", &token->location);
2084 cp_parser_skip_to_pragma_eol (parser, token);
2088 c_parse_error (message,
2089 /* Because c_parser_error does not understand
2090 CPP_KEYWORD, keywords are treated like
2092 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2097 /* Issue an error about name-lookup failing. NAME is the
2098 IDENTIFIER_NODE DECL is the result of
2099 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2100 the thing that we hoped to find. */
2103 cp_parser_name_lookup_error (cp_parser* parser,
2106 const char* desired,
2107 location_t location)
2109 /* If name lookup completely failed, tell the user that NAME was not
2111 if (decl == error_mark_node)
2113 if (parser->scope && parser->scope != global_namespace)
2114 error ("%H%<%E::%E%> has not been declared",
2115 &location, parser->scope, name);
2116 else if (parser->scope == global_namespace)
2117 error ("%H%<::%E%> has not been declared", &location, name);
2118 else if (parser->object_scope
2119 && !CLASS_TYPE_P (parser->object_scope))
2120 error ("%Hrequest for member %qE in non-class type %qT",
2121 &location, name, parser->object_scope);
2122 else if (parser->object_scope)
2123 error ("%H%<%T::%E%> has not been declared",
2124 &location, parser->object_scope, name);
2126 error ("%H%qE has not been declared", &location, name);
2128 else if (parser->scope && parser->scope != global_namespace)
2129 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2130 else if (parser->scope == global_namespace)
2131 error ("%H%<::%E%> %s", &location, name, desired);
2133 error ("%H%qE %s", &location, name, desired);
2136 /* If we are parsing tentatively, remember that an error has occurred
2137 during this tentative parse. Returns true if the error was
2138 simulated; false if a message should be issued by the caller. */
2141 cp_parser_simulate_error (cp_parser* parser)
2143 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2145 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2151 /* Check for repeated decl-specifiers. */
2154 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2155 location_t location)
2159 for (ds = ds_first; ds != ds_last; ++ds)
2161 unsigned count = decl_specs->specs[(int)ds];
2164 /* The "long" specifier is a special case because of "long long". */
2168 error ("%H%<long long long%> is too long for GCC", &location);
2169 else if (pedantic && !in_system_header && warn_long_long
2170 && cxx_dialect == cxx98)
2171 pedwarn (location, OPT_Wlong_long,
2172 "ISO C++ 1998 does not support %<long long%>");
2176 static const char *const decl_spec_names[] = {
2192 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2197 /* This function is called when a type is defined. If type
2198 definitions are forbidden at this point, an error message is
2202 cp_parser_check_type_definition (cp_parser* parser)
2204 /* If types are forbidden here, issue a message. */
2205 if (parser->type_definition_forbidden_message)
2207 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2208 in the message need to be interpreted. */
2209 error (parser->type_definition_forbidden_message);
2215 /* This function is called when the DECLARATOR is processed. The TYPE
2216 was a type defined in the decl-specifiers. If it is invalid to
2217 define a type in the decl-specifiers for DECLARATOR, an error is
2218 issued. TYPE_LOCATION is the location of TYPE and is used
2219 for error reporting. */
2222 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2223 tree type, location_t type_location)
2225 /* [dcl.fct] forbids type definitions in return types.
2226 Unfortunately, it's not easy to know whether or not we are
2227 processing a return type until after the fact. */
2229 && (declarator->kind == cdk_pointer
2230 || declarator->kind == cdk_reference
2231 || declarator->kind == cdk_ptrmem))
2232 declarator = declarator->declarator;
2234 && declarator->kind == cdk_function)
2236 error ("%Hnew types may not be defined in a return type", &type_location);
2237 inform (type_location,
2238 "(perhaps a semicolon is missing after the definition of %qT)",
2243 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2244 "<" in any valid C++ program. If the next token is indeed "<",
2245 issue a message warning the user about what appears to be an
2246 invalid attempt to form a template-id. LOCATION is the location
2247 of the type-specifier (TYPE) */
2250 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2251 tree type, location_t location)
2253 cp_token_position start = 0;
2255 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2258 error ("%H%qT is not a template", &location, type);
2259 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2260 error ("%H%qE is not a template", &location, type);
2262 error ("%Hinvalid template-id", &location);
2263 /* Remember the location of the invalid "<". */
2264 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2265 start = cp_lexer_token_position (parser->lexer, true);
2266 /* Consume the "<". */
2267 cp_lexer_consume_token (parser->lexer);
2268 /* Parse the template arguments. */
2269 cp_parser_enclosed_template_argument_list (parser);
2270 /* Permanently remove the invalid template arguments so that
2271 this error message is not issued again. */
2273 cp_lexer_purge_tokens_after (parser->lexer, start);
2277 /* If parsing an integral constant-expression, issue an error message
2278 about the fact that THING appeared and return true. Otherwise,
2279 return false. In either case, set
2280 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2283 cp_parser_non_integral_constant_expression (cp_parser *parser,
2286 parser->non_integral_constant_expression_p = true;
2287 if (parser->integral_constant_expression_p)
2289 if (!parser->allow_non_integral_constant_expression_p)
2291 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2292 in the message need to be interpreted. */
2293 char *message = concat (thing,
2294 " cannot appear in a constant-expression",
2304 /* Emit a diagnostic for an invalid type name. SCOPE is the
2305 qualifying scope (or NULL, if none) for ID. This function commits
2306 to the current active tentative parse, if any. (Otherwise, the
2307 problematic construct might be encountered again later, resulting
2308 in duplicate error messages.) LOCATION is the location of ID. */
2311 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2312 tree scope, tree id,
2313 location_t location)
2315 tree decl, old_scope;
2316 /* Try to lookup the identifier. */
2317 old_scope = parser->scope;
2318 parser->scope = scope;
2319 decl = cp_parser_lookup_name_simple (parser, id, location);
2320 parser->scope = old_scope;
2321 /* If the lookup found a template-name, it means that the user forgot
2322 to specify an argument list. Emit a useful error message. */
2323 if (TREE_CODE (decl) == TEMPLATE_DECL)
2324 error ("%Hinvalid use of template-name %qE without an argument list",
2326 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2327 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2328 else if (TREE_CODE (decl) == TYPE_DECL)
2329 /* Something like 'unsigned A a;' */
2330 error ("%Hinvalid combination of multiple type-specifiers",
2332 else if (!parser->scope)
2334 /* Issue an error message. */
2335 error ("%H%qE does not name a type", &location, id);
2336 /* If we're in a template class, it's possible that the user was
2337 referring to a type from a base class. For example:
2339 template <typename T> struct A { typedef T X; };
2340 template <typename T> struct B : public A<T> { X x; };
2342 The user should have said "typename A<T>::X". */
2343 if (processing_template_decl && current_class_type
2344 && TYPE_BINFO (current_class_type))
2348 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2352 tree base_type = BINFO_TYPE (b);
2353 if (CLASS_TYPE_P (base_type)
2354 && dependent_type_p (base_type))
2357 /* Go from a particular instantiation of the
2358 template (which will have an empty TYPE_FIELDs),
2359 to the main version. */
2360 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2361 for (field = TYPE_FIELDS (base_type);
2363 field = TREE_CHAIN (field))
2364 if (TREE_CODE (field) == TYPE_DECL
2365 && DECL_NAME (field) == id)
2368 "(perhaps %<typename %T::%E%> was intended)",
2369 BINFO_TYPE (b), id);
2378 /* Here we diagnose qualified-ids where the scope is actually correct,
2379 but the identifier does not resolve to a valid type name. */
2380 else if (parser->scope != error_mark_node)
2382 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2383 error ("%H%qE in namespace %qE does not name a type",
2384 &location, id, parser->scope);
2385 else if (TYPE_P (parser->scope))
2386 error ("%H%qE in class %qT does not name a type",
2387 &location, id, parser->scope);
2391 cp_parser_commit_to_tentative_parse (parser);
2394 /* Check for a common situation where a type-name should be present,
2395 but is not, and issue a sensible error message. Returns true if an
2396 invalid type-name was detected.
2398 The situation handled by this function are variable declarations of the
2399 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2400 Usually, `ID' should name a type, but if we got here it means that it
2401 does not. We try to emit the best possible error message depending on
2402 how exactly the id-expression looks like. */
2405 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2408 cp_token *token = cp_lexer_peek_token (parser->lexer);
2410 cp_parser_parse_tentatively (parser);
2411 id = cp_parser_id_expression (parser,
2412 /*template_keyword_p=*/false,
2413 /*check_dependency_p=*/true,
2414 /*template_p=*/NULL,
2415 /*declarator_p=*/true,
2416 /*optional_p=*/false);
2417 /* After the id-expression, there should be a plain identifier,
2418 otherwise this is not a simple variable declaration. Also, if
2419 the scope is dependent, we cannot do much. */
2420 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2421 || (parser->scope && TYPE_P (parser->scope)
2422 && dependent_type_p (parser->scope))
2423 || TREE_CODE (id) == TYPE_DECL)
2425 cp_parser_abort_tentative_parse (parser);
2428 if (!cp_parser_parse_definitely (parser))
2431 /* Emit a diagnostic for the invalid type. */
2432 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2433 id, token->location);
2434 /* Skip to the end of the declaration; there's no point in
2435 trying to process it. */
2436 cp_parser_skip_to_end_of_block_or_statement (parser);
2440 /* Consume tokens up to, and including, the next non-nested closing `)'.
2441 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2442 are doing error recovery. Returns -1 if OR_COMMA is true and we
2443 found an unnested comma. */
2446 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2451 unsigned paren_depth = 0;
2452 unsigned brace_depth = 0;
2454 if (recovering && !or_comma
2455 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2460 cp_token * token = cp_lexer_peek_token (parser->lexer);
2462 switch (token->type)
2465 case CPP_PRAGMA_EOL:
2466 /* If we've run out of tokens, then there is no closing `)'. */
2470 /* This matches the processing in skip_to_end_of_statement. */
2475 case CPP_OPEN_BRACE:
2478 case CPP_CLOSE_BRACE:
2484 if (recovering && or_comma && !brace_depth && !paren_depth)
2488 case CPP_OPEN_PAREN:
2493 case CPP_CLOSE_PAREN:
2494 if (!brace_depth && !paren_depth--)
2497 cp_lexer_consume_token (parser->lexer);
2506 /* Consume the token. */
2507 cp_lexer_consume_token (parser->lexer);
2511 /* Consume tokens until we reach the end of the current statement.
2512 Normally, that will be just before consuming a `;'. However, if a
2513 non-nested `}' comes first, then we stop before consuming that. */
2516 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2518 unsigned nesting_depth = 0;
2522 cp_token *token = cp_lexer_peek_token (parser->lexer);
2524 switch (token->type)
2527 case CPP_PRAGMA_EOL:
2528 /* If we've run out of tokens, stop. */
2532 /* If the next token is a `;', we have reached the end of the
2538 case CPP_CLOSE_BRACE:
2539 /* If this is a non-nested '}', stop before consuming it.
2540 That way, when confronted with something like:
2544 we stop before consuming the closing '}', even though we
2545 have not yet reached a `;'. */
2546 if (nesting_depth == 0)
2549 /* If it is the closing '}' for a block that we have
2550 scanned, stop -- but only after consuming the token.
2556 we will stop after the body of the erroneously declared
2557 function, but before consuming the following `typedef'
2559 if (--nesting_depth == 0)
2561 cp_lexer_consume_token (parser->lexer);
2565 case CPP_OPEN_BRACE:
2573 /* Consume the token. */
2574 cp_lexer_consume_token (parser->lexer);
2578 /* This function is called at the end of a statement or declaration.
2579 If the next token is a semicolon, it is consumed; otherwise, error
2580 recovery is attempted. */
2583 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2585 /* Look for the trailing `;'. */
2586 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2588 /* If there is additional (erroneous) input, skip to the end of
2590 cp_parser_skip_to_end_of_statement (parser);
2591 /* If the next token is now a `;', consume it. */
2592 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2593 cp_lexer_consume_token (parser->lexer);
2597 /* Skip tokens until we have consumed an entire block, or until we
2598 have consumed a non-nested `;'. */
2601 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2603 int nesting_depth = 0;
2605 while (nesting_depth >= 0)
2607 cp_token *token = cp_lexer_peek_token (parser->lexer);
2609 switch (token->type)
2612 case CPP_PRAGMA_EOL:
2613 /* If we've run out of tokens, stop. */
2617 /* Stop if this is an unnested ';'. */
2622 case CPP_CLOSE_BRACE:
2623 /* Stop if this is an unnested '}', or closes the outermost
2630 case CPP_OPEN_BRACE:
2639 /* Consume the token. */
2640 cp_lexer_consume_token (parser->lexer);
2644 /* Skip tokens until a non-nested closing curly brace is the next
2645 token, or there are no more tokens. Return true in the first case,
2649 cp_parser_skip_to_closing_brace (cp_parser *parser)
2651 unsigned nesting_depth = 0;
2655 cp_token *token = cp_lexer_peek_token (parser->lexer);
2657 switch (token->type)
2660 case CPP_PRAGMA_EOL:
2661 /* If we've run out of tokens, stop. */
2664 case CPP_CLOSE_BRACE:
2665 /* If the next token is a non-nested `}', then we have reached
2666 the end of the current block. */
2667 if (nesting_depth-- == 0)
2671 case CPP_OPEN_BRACE:
2672 /* If it the next token is a `{', then we are entering a new
2673 block. Consume the entire block. */
2681 /* Consume the token. */
2682 cp_lexer_consume_token (parser->lexer);
2686 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2687 parameter is the PRAGMA token, allowing us to purge the entire pragma
2691 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2695 parser->lexer->in_pragma = false;
2698 token = cp_lexer_consume_token (parser->lexer);
2699 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2701 /* Ensure that the pragma is not parsed again. */
2702 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2705 /* Require pragma end of line, resyncing with it as necessary. The
2706 arguments are as for cp_parser_skip_to_pragma_eol. */
2709 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2711 parser->lexer->in_pragma = false;
2712 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2713 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2716 /* This is a simple wrapper around make_typename_type. When the id is
2717 an unresolved identifier node, we can provide a superior diagnostic
2718 using cp_parser_diagnose_invalid_type_name. */
2721 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2722 tree id, location_t id_location)
2725 if (TREE_CODE (id) == IDENTIFIER_NODE)
2727 result = make_typename_type (scope, id, typename_type,
2728 /*complain=*/tf_none);
2729 if (result == error_mark_node)
2730 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2733 return make_typename_type (scope, id, typename_type, tf_error);
2736 /* This is a wrapper around the
2737 make_{pointer,ptrmem,reference}_declarator functions that decides
2738 which one to call based on the CODE and CLASS_TYPE arguments. The
2739 CODE argument should be one of the values returned by
2740 cp_parser_ptr_operator. */
2741 static cp_declarator *
2742 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2743 cp_cv_quals cv_qualifiers,
2744 cp_declarator *target)
2746 if (code == ERROR_MARK)
2747 return cp_error_declarator;
2749 if (code == INDIRECT_REF)
2750 if (class_type == NULL_TREE)
2751 return make_pointer_declarator (cv_qualifiers, target);
2753 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2754 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2755 return make_reference_declarator (cv_qualifiers, target, false);
2756 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2757 return make_reference_declarator (cv_qualifiers, target, true);
2761 /* Create a new C++ parser. */
2764 cp_parser_new (void)
2770 /* cp_lexer_new_main is called before calling ggc_alloc because
2771 cp_lexer_new_main might load a PCH file. */
2772 lexer = cp_lexer_new_main ();
2774 /* Initialize the binops_by_token so that we can get the tree
2775 directly from the token. */
2776 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2777 binops_by_token[binops[i].token_type] = binops[i];
2779 parser = GGC_CNEW (cp_parser);
2780 parser->lexer = lexer;
2781 parser->context = cp_parser_context_new (NULL);
2783 /* For now, we always accept GNU extensions. */
2784 parser->allow_gnu_extensions_p = 1;
2786 /* The `>' token is a greater-than operator, not the end of a
2788 parser->greater_than_is_operator_p = true;
2790 parser->default_arg_ok_p = true;
2792 /* We are not parsing a constant-expression. */
2793 parser->integral_constant_expression_p = false;
2794 parser->allow_non_integral_constant_expression_p = false;
2795 parser->non_integral_constant_expression_p = false;
2797 /* Local variable names are not forbidden. */
2798 parser->local_variables_forbidden_p = false;
2800 /* We are not processing an `extern "C"' declaration. */
2801 parser->in_unbraced_linkage_specification_p = false;
2803 /* We are not processing a declarator. */
2804 parser->in_declarator_p = false;
2806 /* We are not processing a template-argument-list. */
2807 parser->in_template_argument_list_p = false;
2809 /* We are not in an iteration statement. */
2810 parser->in_statement = 0;
2812 /* We are not in a switch statement. */
2813 parser->in_switch_statement_p = false;
2815 /* We are not parsing a type-id inside an expression. */
2816 parser->in_type_id_in_expr_p = false;
2818 /* Declarations aren't implicitly extern "C". */
2819 parser->implicit_extern_c = false;
2821 /* String literals should be translated to the execution character set. */
2822 parser->translate_strings_p = true;
2824 /* We are not parsing a function body. */
2825 parser->in_function_body = false;
2827 /* The unparsed function queue is empty. */
2828 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2830 /* There are no classes being defined. */
2831 parser->num_classes_being_defined = 0;
2833 /* No template parameters apply. */
2834 parser->num_template_parameter_lists = 0;
2839 /* Create a cp_lexer structure which will emit the tokens in CACHE
2840 and push it onto the parser's lexer stack. This is used for delayed
2841 parsing of in-class method bodies and default arguments, and should
2842 not be confused with tentative parsing. */
2844 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2846 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2847 lexer->next = parser->lexer;
2848 parser->lexer = lexer;
2850 /* Move the current source position to that of the first token in the
2852 cp_lexer_set_source_position_from_token (lexer->next_token);
2855 /* Pop the top lexer off the parser stack. This is never used for the
2856 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2858 cp_parser_pop_lexer (cp_parser *parser)
2860 cp_lexer *lexer = parser->lexer;
2861 parser->lexer = lexer->next;
2862 cp_lexer_destroy (lexer);
2864 /* Put the current source position back where it was before this
2865 lexer was pushed. */
2866 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2869 /* Lexical conventions [gram.lex] */
2871 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2875 cp_parser_identifier (cp_parser* parser)
2879 /* Look for the identifier. */
2880 token = cp_parser_require (parser, CPP_NAME, "identifier");
2881 /* Return the value. */
2882 return token ? token->u.value : error_mark_node;
2885 /* Parse a sequence of adjacent string constants. Returns a
2886 TREE_STRING representing the combined, nul-terminated string
2887 constant. If TRANSLATE is true, translate the string to the
2888 execution character set. If WIDE_OK is true, a wide string is
2891 C++98 [lex.string] says that if a narrow string literal token is
2892 adjacent to a wide string literal token, the behavior is undefined.
2893 However, C99 6.4.5p4 says that this results in a wide string literal.
2894 We follow C99 here, for consistency with the C front end.
2896 This code is largely lifted from lex_string() in c-lex.c.
2898 FUTURE: ObjC++ will need to handle @-strings here. */
2900 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2904 struct obstack str_ob;
2905 cpp_string str, istr, *strs;
2907 enum cpp_ttype type;
2909 tok = cp_lexer_peek_token (parser->lexer);
2910 if (!cp_parser_is_string_literal (tok))
2912 cp_parser_error (parser, "expected string-literal");
2913 return error_mark_node;
2918 /* Try to avoid the overhead of creating and destroying an obstack
2919 for the common case of just one string. */
2920 if (!cp_parser_is_string_literal
2921 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2923 cp_lexer_consume_token (parser->lexer);
2925 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2926 str.len = TREE_STRING_LENGTH (tok->u.value);
2933 gcc_obstack_init (&str_ob);
2938 cp_lexer_consume_token (parser->lexer);
2940 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2941 str.len = TREE_STRING_LENGTH (tok->u.value);
2943 if (type != tok->type)
2945 if (type == CPP_STRING)
2947 else if (tok->type != CPP_STRING)
2948 error ("%Hunsupported non-standard concatenation "
2949 "of string literals", &tok->location);
2952 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2954 tok = cp_lexer_peek_token (parser->lexer);
2956 while (cp_parser_is_string_literal (tok));
2958 strs = (cpp_string *) obstack_finish (&str_ob);
2961 if (type != CPP_STRING && !wide_ok)
2963 cp_parser_error (parser, "a wide string is invalid in this context");
2967 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2968 (parse_in, strs, count, &istr, type))
2970 value = build_string (istr.len, (const char *)istr.text);
2971 free (CONST_CAST (unsigned char *, istr.text));
2977 TREE_TYPE (value) = char_array_type_node;
2980 TREE_TYPE (value) = char16_array_type_node;
2983 TREE_TYPE (value) = char32_array_type_node;
2986 TREE_TYPE (value) = wchar_array_type_node;
2990 value = fix_string_type (value);
2993 /* cpp_interpret_string has issued an error. */
2994 value = error_mark_node;
2997 obstack_free (&str_ob, 0);
3003 /* Basic concepts [gram.basic] */
3005 /* Parse a translation-unit.
3008 declaration-seq [opt]
3010 Returns TRUE if all went well. */
3013 cp_parser_translation_unit (cp_parser* parser)
3015 /* The address of the first non-permanent object on the declarator
3017 static void *declarator_obstack_base;
3021 /* Create the declarator obstack, if necessary. */
3022 if (!cp_error_declarator)
3024 gcc_obstack_init (&declarator_obstack);
3025 /* Create the error declarator. */
3026 cp_error_declarator = make_declarator (cdk_error);
3027 /* Create the empty parameter list. */
3028 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3029 /* Remember where the base of the declarator obstack lies. */
3030 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3033 cp_parser_declaration_seq_opt (parser);
3035 /* If there are no tokens left then all went well. */
3036 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3038 /* Get rid of the token array; we don't need it any more. */
3039 cp_lexer_destroy (parser->lexer);
3040 parser->lexer = NULL;
3042 /* This file might have been a context that's implicitly extern
3043 "C". If so, pop the lang context. (Only relevant for PCH.) */
3044 if (parser->implicit_extern_c)
3046 pop_lang_context ();
3047 parser->implicit_extern_c = false;
3051 finish_translation_unit ();
3057 cp_parser_error (parser, "expected declaration");
3061 /* Make sure the declarator obstack was fully cleaned up. */
3062 gcc_assert (obstack_next_free (&declarator_obstack)
3063 == declarator_obstack_base);
3065 /* All went well. */
3069 /* Expressions [gram.expr] */
3071 /* Parse a primary-expression.
3082 ( compound-statement )
3083 __builtin_va_arg ( assignment-expression , type-id )
3084 __builtin_offsetof ( type-id , offsetof-expression )
3087 __has_nothrow_assign ( type-id )
3088 __has_nothrow_constructor ( type-id )
3089 __has_nothrow_copy ( type-id )
3090 __has_trivial_assign ( type-id )
3091 __has_trivial_constructor ( type-id )
3092 __has_trivial_copy ( type-id )
3093 __has_trivial_destructor ( type-id )
3094 __has_virtual_destructor ( type-id )
3095 __is_abstract ( type-id )
3096 __is_base_of ( type-id , type-id )
3097 __is_class ( type-id )
3098 __is_convertible_to ( type-id , type-id )
3099 __is_empty ( type-id )
3100 __is_enum ( type-id )
3101 __is_pod ( type-id )
3102 __is_polymorphic ( type-id )
3103 __is_union ( type-id )
3105 Objective-C++ Extension:
3113 ADDRESS_P is true iff this expression was immediately preceded by
3114 "&" and therefore might denote a pointer-to-member. CAST_P is true
3115 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3116 true iff this expression is a template argument.
3118 Returns a representation of the expression. Upon return, *IDK
3119 indicates what kind of id-expression (if any) was present. */
3122 cp_parser_primary_expression (cp_parser *parser,
3125 bool template_arg_p,
3128 cp_token *token = NULL;
3130 /* Assume the primary expression is not an id-expression. */
3131 *idk = CP_ID_KIND_NONE;
3133 /* Peek at the next token. */
3134 token = cp_lexer_peek_token (parser->lexer);
3135 switch (token->type)
3148 token = cp_lexer_consume_token (parser->lexer);
3149 if (TREE_CODE (token->u.value) == FIXED_CST)
3151 error ("%Hfixed-point types not supported in C++",
3153 return error_mark_node;
3155 /* Floating-point literals are only allowed in an integral
3156 constant expression if they are cast to an integral or
3157 enumeration type. */
3158 if (TREE_CODE (token->u.value) == REAL_CST
3159 && parser->integral_constant_expression_p
3162 /* CAST_P will be set even in invalid code like "int(2.7 +
3163 ...)". Therefore, we have to check that the next token
3164 is sure to end the cast. */
3167 cp_token *next_token;
3169 next_token = cp_lexer_peek_token (parser->lexer);
3170 if (/* The comma at the end of an
3171 enumerator-definition. */
3172 next_token->type != CPP_COMMA
3173 /* The curly brace at the end of an enum-specifier. */
3174 && next_token->type != CPP_CLOSE_BRACE
3175 /* The end of a statement. */
3176 && next_token->type != CPP_SEMICOLON
3177 /* The end of the cast-expression. */
3178 && next_token->type != CPP_CLOSE_PAREN
3179 /* The end of an array bound. */
3180 && next_token->type != CPP_CLOSE_SQUARE
3181 /* The closing ">" in a template-argument-list. */
3182 && (next_token->type != CPP_GREATER
3183 || parser->greater_than_is_operator_p)
3184 /* C++0x only: A ">>" treated like two ">" tokens,
3185 in a template-argument-list. */
3186 && (next_token->type != CPP_RSHIFT
3187 || (cxx_dialect == cxx98)
3188 || parser->greater_than_is_operator_p))
3192 /* If we are within a cast, then the constraint that the
3193 cast is to an integral or enumeration type will be
3194 checked at that point. If we are not within a cast, then
3195 this code is invalid. */
3197 cp_parser_non_integral_constant_expression
3198 (parser, "floating-point literal");
3200 return token->u.value;
3206 /* ??? Should wide strings be allowed when parser->translate_strings_p
3207 is false (i.e. in attributes)? If not, we can kill the third
3208 argument to cp_parser_string_literal. */
3209 return cp_parser_string_literal (parser,
3210 parser->translate_strings_p,
3213 case CPP_OPEN_PAREN:
3216 bool saved_greater_than_is_operator_p;
3218 /* Consume the `('. */
3219 cp_lexer_consume_token (parser->lexer);
3220 /* Within a parenthesized expression, a `>' token is always
3221 the greater-than operator. */
3222 saved_greater_than_is_operator_p
3223 = parser->greater_than_is_operator_p;
3224 parser->greater_than_is_operator_p = true;
3225 /* If we see `( { ' then we are looking at the beginning of
3226 a GNU statement-expression. */
3227 if (cp_parser_allow_gnu_extensions_p (parser)
3228 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3230 /* Statement-expressions are not allowed by the standard. */
3231 pedwarn (token->location, OPT_pedantic,
3232 "ISO C++ forbids braced-groups within expressions");
3234 /* And they're not allowed outside of a function-body; you
3235 cannot, for example, write:
3237 int i = ({ int j = 3; j + 1; });
3239 at class or namespace scope. */
3240 if (!parser->in_function_body
3241 || parser->in_template_argument_list_p)
3243 error ("%Hstatement-expressions are not allowed outside "
3244 "functions nor in template-argument lists",
3246 cp_parser_skip_to_end_of_block_or_statement (parser);
3247 expr = error_mark_node;
3251 /* Start the statement-expression. */
3252 expr = begin_stmt_expr ();
3253 /* Parse the compound-statement. */
3254 cp_parser_compound_statement (parser, expr, false);
3256 expr = finish_stmt_expr (expr, false);
3261 /* Parse the parenthesized expression. */
3262 expr = cp_parser_expression (parser, cast_p, idk);
3263 /* Let the front end know that this expression was
3264 enclosed in parentheses. This matters in case, for
3265 example, the expression is of the form `A::B', since
3266 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3268 finish_parenthesized_expr (expr);
3270 /* The `>' token might be the end of a template-id or
3271 template-parameter-list now. */
3272 parser->greater_than_is_operator_p
3273 = saved_greater_than_is_operator_p;
3274 /* Consume the `)'. */
3275 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3276 cp_parser_skip_to_end_of_statement (parser);
3282 switch (token->keyword)
3284 /* These two are the boolean literals. */
3286 cp_lexer_consume_token (parser->lexer);
3287 return boolean_true_node;
3289 cp_lexer_consume_token (parser->lexer);
3290 return boolean_false_node;
3292 /* The `__null' literal. */
3294 cp_lexer_consume_token (parser->lexer);
3297 /* Recognize the `this' keyword. */
3299 cp_lexer_consume_token (parser->lexer);
3300 if (parser->local_variables_forbidden_p)
3302 error ("%H%<this%> may not be used in this context",
3304 return error_mark_node;
3306 /* Pointers cannot appear in constant-expressions. */
3307 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3308 return error_mark_node;
3309 return finish_this_expr ();
3311 /* The `operator' keyword can be the beginning of an
3316 case RID_FUNCTION_NAME:
3317 case RID_PRETTY_FUNCTION_NAME:
3318 case RID_C99_FUNCTION_NAME:
3322 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3323 __func__ are the names of variables -- but they are
3324 treated specially. Therefore, they are handled here,
3325 rather than relying on the generic id-expression logic
3326 below. Grammatically, these names are id-expressions.
3328 Consume the token. */
3329 token = cp_lexer_consume_token (parser->lexer);
3331 switch (token->keyword)
3333 case RID_FUNCTION_NAME:
3334 name = "%<__FUNCTION__%>";
3336 case RID_PRETTY_FUNCTION_NAME:
3337 name = "%<__PRETTY_FUNCTION__%>";
3339 case RID_C99_FUNCTION_NAME:
3340 name = "%<__func__%>";
3346 if (cp_parser_non_integral_constant_expression (parser, name))
3347 return error_mark_node;
3349 /* Look up the name. */
3350 return finish_fname (token->u.value);
3358 /* The `__builtin_va_arg' construct is used to handle
3359 `va_arg'. Consume the `__builtin_va_arg' token. */
3360 cp_lexer_consume_token (parser->lexer);
3361 /* Look for the opening `('. */
3362 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3363 /* Now, parse the assignment-expression. */
3364 expression = cp_parser_assignment_expression (parser,
3365 /*cast_p=*/false, NULL);
3366 /* Look for the `,'. */
3367 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3368 /* Parse the type-id. */
3369 type = cp_parser_type_id (parser);
3370 /* Look for the closing `)'. */
3371 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3372 /* Using `va_arg' in a constant-expression is not
3374 if (cp_parser_non_integral_constant_expression (parser,
3376 return error_mark_node;
3377 return build_x_va_arg (expression, type);
3381 return cp_parser_builtin_offsetof (parser);
3383 case RID_HAS_NOTHROW_ASSIGN:
3384 case RID_HAS_NOTHROW_CONSTRUCTOR:
3385 case RID_HAS_NOTHROW_COPY:
3386 case RID_HAS_TRIVIAL_ASSIGN:
3387 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3388 case RID_HAS_TRIVIAL_COPY:
3389 case RID_HAS_TRIVIAL_DESTRUCTOR:
3390 case RID_HAS_VIRTUAL_DESTRUCTOR:
3391 case RID_IS_ABSTRACT:
3392 case RID_IS_BASE_OF:
3394 case RID_IS_CONVERTIBLE_TO:
3398 case RID_IS_POLYMORPHIC:
3400 return cp_parser_trait_expr (parser, token->keyword);
3402 /* Objective-C++ expressions. */
3404 case RID_AT_PROTOCOL:
3405 case RID_AT_SELECTOR:
3406 return cp_parser_objc_expression (parser);
3409 cp_parser_error (parser, "expected primary-expression");
3410 return error_mark_node;
3413 /* An id-expression can start with either an identifier, a
3414 `::' as the beginning of a qualified-id, or the "operator"
3418 case CPP_TEMPLATE_ID:
3419 case CPP_NESTED_NAME_SPECIFIER:
3423 const char *error_msg;
3426 cp_token *id_expr_token;
3429 /* Parse the id-expression. */
3431 = cp_parser_id_expression (parser,
3432 /*template_keyword_p=*/false,
3433 /*check_dependency_p=*/true,
3435 /*declarator_p=*/false,
3436 /*optional_p=*/false);
3437 if (id_expression == error_mark_node)
3438 return error_mark_node;
3439 id_expr_token = token;
3440 token = cp_lexer_peek_token (parser->lexer);
3441 done = (token->type != CPP_OPEN_SQUARE
3442 && token->type != CPP_OPEN_PAREN
3443 && token->type != CPP_DOT
3444 && token->type != CPP_DEREF
3445 && token->type != CPP_PLUS_PLUS
3446 && token->type != CPP_MINUS_MINUS);
3447 /* If we have a template-id, then no further lookup is
3448 required. If the template-id was for a template-class, we
3449 will sometimes have a TYPE_DECL at this point. */
3450 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3451 || TREE_CODE (id_expression) == TYPE_DECL)
3452 decl = id_expression;
3453 /* Look up the name. */
3456 tree ambiguous_decls;
3458 decl = cp_parser_lookup_name (parser, id_expression,
3461 /*is_namespace=*/false,
3462 /*check_dependency=*/true,
3464 id_expr_token->location);
3465 /* If the lookup was ambiguous, an error will already have
3467 if (ambiguous_decls)
3468 return error_mark_node;
3470 /* In Objective-C++, an instance variable (ivar) may be preferred
3471 to whatever cp_parser_lookup_name() found. */
3472 decl = objc_lookup_ivar (decl, id_expression);
3474 /* If name lookup gives us a SCOPE_REF, then the
3475 qualifying scope was dependent. */
3476 if (TREE_CODE (decl) == SCOPE_REF)
3478 /* At this point, we do not know if DECL is a valid
3479 integral constant expression. We assume that it is
3480 in fact such an expression, so that code like:
3482 template <int N> struct A {
3486 is accepted. At template-instantiation time, we
3487 will check that B<N>::i is actually a constant. */
3490 /* Check to see if DECL is a local variable in a context
3491 where that is forbidden. */
3492 if (parser->local_variables_forbidden_p
3493 && local_variable_p (decl))
3495 /* It might be that we only found DECL because we are
3496 trying to be generous with pre-ISO scoping rules.
3497 For example, consider:
3501 for (int i = 0; i < 10; ++i) {}
3502 extern void f(int j = i);
3505 Here, name look up will originally find the out
3506 of scope `i'. We need to issue a warning message,
3507 but then use the global `i'. */
3508 decl = check_for_out_of_scope_variable (decl);
3509 if (local_variable_p (decl))
3511 error ("%Hlocal variable %qD may not appear in this context",
3512 &id_expr_token->location, decl);
3513 return error_mark_node;
3518 decl = (finish_id_expression
3519 (id_expression, decl, parser->scope,
3521 parser->integral_constant_expression_p,
3522 parser->allow_non_integral_constant_expression_p,
3523 &parser->non_integral_constant_expression_p,
3524 template_p, done, address_p,
3527 id_expr_token->location));
3529 cp_parser_error (parser, error_msg);
3533 /* Anything else is an error. */
3535 /* ...unless we have an Objective-C++ message or string literal,
3537 if (c_dialect_objc ()
3538 && (token->type == CPP_OPEN_SQUARE
3539 || token->type == CPP_OBJC_STRING))
3540 return cp_parser_objc_expression (parser);
3542 cp_parser_error (parser, "expected primary-expression");
3543 return error_mark_node;
3547 /* Parse an id-expression.
3554 :: [opt] nested-name-specifier template [opt] unqualified-id
3556 :: operator-function-id
3559 Return a representation of the unqualified portion of the
3560 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3561 a `::' or nested-name-specifier.
3563 Often, if the id-expression was a qualified-id, the caller will
3564 want to make a SCOPE_REF to represent the qualified-id. This
3565 function does not do this in order to avoid wastefully creating
3566 SCOPE_REFs when they are not required.
3568 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3571 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3572 uninstantiated templates.
3574 If *TEMPLATE_P is non-NULL, it is set to true iff the
3575 `template' keyword is used to explicitly indicate that the entity
3576 named is a template.
3578 If DECLARATOR_P is true, the id-expression is appearing as part of
3579 a declarator, rather than as part of an expression. */
3582 cp_parser_id_expression (cp_parser *parser,
3583 bool template_keyword_p,
3584 bool check_dependency_p,
3589 bool global_scope_p;
3590 bool nested_name_specifier_p;
3592 /* Assume the `template' keyword was not used. */
3594 *template_p = template_keyword_p;
3596 /* Look for the optional `::' operator. */
3598 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3600 /* Look for the optional nested-name-specifier. */
3601 nested_name_specifier_p
3602 = (cp_parser_nested_name_specifier_opt (parser,
3603 /*typename_keyword_p=*/false,
3608 /* If there is a nested-name-specifier, then we are looking at
3609 the first qualified-id production. */
3610 if (nested_name_specifier_p)
3613 tree saved_object_scope;
3614 tree saved_qualifying_scope;
3615 tree unqualified_id;
3618 /* See if the next token is the `template' keyword. */
3620 template_p = &is_template;
3621 *template_p = cp_parser_optional_template_keyword (parser);
3622 /* Name lookup we do during the processing of the
3623 unqualified-id might obliterate SCOPE. */
3624 saved_scope = parser->scope;
3625 saved_object_scope = parser->object_scope;
3626 saved_qualifying_scope = parser->qualifying_scope;
3627 /* Process the final unqualified-id. */
3628 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3631 /*optional_p=*/false);
3632 /* Restore the SAVED_SCOPE for our caller. */
3633 parser->scope = saved_scope;
3634 parser->object_scope = saved_object_scope;
3635 parser->qualifying_scope = saved_qualifying_scope;
3637 return unqualified_id;
3639 /* Otherwise, if we are in global scope, then we are looking at one
3640 of the other qualified-id productions. */
3641 else if (global_scope_p)
3646 /* Peek at the next token. */
3647 token = cp_lexer_peek_token (parser->lexer);
3649 /* If it's an identifier, and the next token is not a "<", then
3650 we can avoid the template-id case. This is an optimization
3651 for this common case. */
3652 if (token->type == CPP_NAME
3653 && !cp_parser_nth_token_starts_template_argument_list_p
3655 return cp_parser_identifier (parser);
3657 cp_parser_parse_tentatively (parser);
3658 /* Try a template-id. */
3659 id = cp_parser_template_id (parser,
3660 /*template_keyword_p=*/false,
3661 /*check_dependency_p=*/true,
3663 /* If that worked, we're done. */
3664 if (cp_parser_parse_definitely (parser))
3667 /* Peek at the next token. (Changes in the token buffer may
3668 have invalidated the pointer obtained above.) */
3669 token = cp_lexer_peek_token (parser->lexer);
3671 switch (token->type)
3674 return cp_parser_identifier (parser);
3677 if (token->keyword == RID_OPERATOR)
3678 return cp_parser_operator_function_id (parser);
3682 cp_parser_error (parser, "expected id-expression");
3683 return error_mark_node;
3687 return cp_parser_unqualified_id (parser, template_keyword_p,
3688 /*check_dependency_p=*/true,
3693 /* Parse an unqualified-id.
3697 operator-function-id
3698 conversion-function-id
3702 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3703 keyword, in a construct like `A::template ...'.
3705 Returns a representation of unqualified-id. For the `identifier'
3706 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3707 production a BIT_NOT_EXPR is returned; the operand of the
3708 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3709 other productions, see the documentation accompanying the
3710 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3711 names are looked up in uninstantiated templates. If DECLARATOR_P
3712 is true, the unqualified-id is appearing as part of a declarator,
3713 rather than as part of an expression. */
3716 cp_parser_unqualified_id (cp_parser* parser,
3717 bool template_keyword_p,
3718 bool check_dependency_p,
3724 /* Peek at the next token. */
3725 token = cp_lexer_peek_token (parser->lexer);
3727 switch (token->type)
3733 /* We don't know yet whether or not this will be a
3735 cp_parser_parse_tentatively (parser);
3736 /* Try a template-id. */
3737 id = cp_parser_template_id (parser, template_keyword_p,
3740 /* If it worked, we're done. */
3741 if (cp_parser_parse_definitely (parser))
3743 /* Otherwise, it's an ordinary identifier. */
3744 return cp_parser_identifier (parser);
3747 case CPP_TEMPLATE_ID:
3748 return cp_parser_template_id (parser, template_keyword_p,
3755 tree qualifying_scope;
3760 /* Consume the `~' token. */
3761 cp_lexer_consume_token (parser->lexer);
3762 /* Parse the class-name. The standard, as written, seems to
3765 template <typename T> struct S { ~S (); };
3766 template <typename T> S<T>::~S() {}
3768 is invalid, since `~' must be followed by a class-name, but
3769 `S<T>' is dependent, and so not known to be a class.
3770 That's not right; we need to look in uninstantiated
3771 templates. A further complication arises from:
3773 template <typename T> void f(T t) {
3777 Here, it is not possible to look up `T' in the scope of `T'
3778 itself. We must look in both the current scope, and the
3779 scope of the containing complete expression.
3781 Yet another issue is:
3790 The standard does not seem to say that the `S' in `~S'
3791 should refer to the type `S' and not the data member
3794 /* DR 244 says that we look up the name after the "~" in the
3795 same scope as we looked up the qualifying name. That idea
3796 isn't fully worked out; it's more complicated than that. */
3797 scope = parser->scope;
3798 object_scope = parser->object_scope;
3799 qualifying_scope = parser->qualifying_scope;
3801 /* Check for invalid scopes. */
3802 if (scope == error_mark_node)
3804 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3805 cp_lexer_consume_token (parser->lexer);
3806 return error_mark_node;
3808 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3810 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3811 error ("%Hscope %qT before %<~%> is not a class-name",
3812 &token->location, scope);
3813 cp_parser_simulate_error (parser);
3814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3815 cp_lexer_consume_token (parser->lexer);
3816 return error_mark_node;
3818 gcc_assert (!scope || TYPE_P (scope));
3820 /* If the name is of the form "X::~X" it's OK. */
3821 token = cp_lexer_peek_token (parser->lexer);
3823 && token->type == CPP_NAME
3824 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3826 && constructor_name_p (token->u.value, scope))
3828 cp_lexer_consume_token (parser->lexer);
3829 return build_nt (BIT_NOT_EXPR, scope);
3832 /* If there was an explicit qualification (S::~T), first look
3833 in the scope given by the qualification (i.e., S). */
3835 type_decl = NULL_TREE;
3838 cp_parser_parse_tentatively (parser);
3839 type_decl = cp_parser_class_name (parser,
3840 /*typename_keyword_p=*/false,
3841 /*template_keyword_p=*/false,
3843 /*check_dependency=*/false,
3844 /*class_head_p=*/false,
3846 if (cp_parser_parse_definitely (parser))
3849 /* In "N::S::~S", look in "N" as well. */
3850 if (!done && scope && qualifying_scope)
3852 cp_parser_parse_tentatively (parser);
3853 parser->scope = qualifying_scope;
3854 parser->object_scope = NULL_TREE;
3855 parser->qualifying_scope = NULL_TREE;
3857 = cp_parser_class_name (parser,
3858 /*typename_keyword_p=*/false,
3859 /*template_keyword_p=*/false,
3861 /*check_dependency=*/false,
3862 /*class_head_p=*/false,
3864 if (cp_parser_parse_definitely (parser))
3867 /* In "p->S::~T", look in the scope given by "*p" as well. */
3868 else if (!done && object_scope)
3870 cp_parser_parse_tentatively (parser);
3871 parser->scope = object_scope;
3872 parser->object_scope = NULL_TREE;
3873 parser->qualifying_scope = NULL_TREE;
3875 = cp_parser_class_name (parser,
3876 /*typename_keyword_p=*/false,
3877 /*template_keyword_p=*/false,
3879 /*check_dependency=*/false,
3880 /*class_head_p=*/false,
3882 if (cp_parser_parse_definitely (parser))
3885 /* Look in the surrounding context. */
3888 parser->scope = NULL_TREE;
3889 parser->object_scope = NULL_TREE;
3890 parser->qualifying_scope = NULL_TREE;
3891 if (processing_template_decl)
3892 cp_parser_parse_tentatively (parser);
3894 = cp_parser_class_name (parser,
3895 /*typename_keyword_p=*/false,
3896 /*template_keyword_p=*/false,
3898 /*check_dependency=*/false,
3899 /*class_head_p=*/false,
3901 if (processing_template_decl
3902 && ! cp_parser_parse_definitely (parser))
3904 /* We couldn't find a type with this name, so just accept
3905 it and check for a match at instantiation time. */
3906 type_decl = cp_parser_identifier (parser);
3907 if (type_decl != error_mark_node)
3908 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3912 /* If an error occurred, assume that the name of the
3913 destructor is the same as the name of the qualifying
3914 class. That allows us to keep parsing after running
3915 into ill-formed destructor names. */
3916 if (type_decl == error_mark_node && scope)
3917 return build_nt (BIT_NOT_EXPR, scope);
3918 else if (type_decl == error_mark_node)
3919 return error_mark_node;
3921 /* Check that destructor name and scope match. */
3922 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3924 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3925 error ("%Hdeclaration of %<~%T%> as member of %qT",
3926 &token->location, type_decl, scope);
3927 cp_parser_simulate_error (parser);
3928 return error_mark_node;
3933 A typedef-name that names a class shall not be used as the
3934 identifier in the declarator for a destructor declaration. */
3936 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3937 && !DECL_SELF_REFERENCE_P (type_decl)
3938 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3939 error ("%Htypedef-name %qD used as destructor declarator",
3940 &token->location, type_decl);
3942 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3946 if (token->keyword == RID_OPERATOR)
3950 /* This could be a template-id, so we try that first. */
3951 cp_parser_parse_tentatively (parser);
3952 /* Try a template-id. */
3953 id = cp_parser_template_id (parser, template_keyword_p,
3954 /*check_dependency_p=*/true,
3956 /* If that worked, we're done. */
3957 if (cp_parser_parse_definitely (parser))
3959 /* We still don't know whether we're looking at an
3960 operator-function-id or a conversion-function-id. */
3961 cp_parser_parse_tentatively (parser);
3962 /* Try an operator-function-id. */
3963 id = cp_parser_operator_function_id (parser);
3964 /* If that didn't work, try a conversion-function-id. */
3965 if (!cp_parser_parse_definitely (parser))
3966 id = cp_parser_conversion_function_id (parser);
3975 cp_parser_error (parser, "expected unqualified-id");
3976 return error_mark_node;
3980 /* Parse an (optional) nested-name-specifier.
3982 nested-name-specifier: [C++98]
3983 class-or-namespace-name :: nested-name-specifier [opt]
3984 class-or-namespace-name :: template nested-name-specifier [opt]
3986 nested-name-specifier: [C++0x]
3989 nested-name-specifier identifier ::
3990 nested-name-specifier template [opt] simple-template-id ::
3992 PARSER->SCOPE should be set appropriately before this function is
3993 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3994 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3997 Sets PARSER->SCOPE to the class (TYPE) or namespace
3998 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3999 it unchanged if there is no nested-name-specifier. Returns the new
4000 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4002 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4003 part of a declaration and/or decl-specifier. */
4006 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4007 bool typename_keyword_p,
4008 bool check_dependency_p,
4010 bool is_declaration)
4012 bool success = false;
4013 cp_token_position start = 0;
4016 /* Remember where the nested-name-specifier starts. */
4017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4019 start = cp_lexer_token_position (parser->lexer, false);
4020 push_deferring_access_checks (dk_deferred);
4027 tree saved_qualifying_scope;
4028 bool template_keyword_p;
4030 /* Spot cases that cannot be the beginning of a
4031 nested-name-specifier. */
4032 token = cp_lexer_peek_token (parser->lexer);
4034 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4035 the already parsed nested-name-specifier. */
4036 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4038 /* Grab the nested-name-specifier and continue the loop. */
4039 cp_parser_pre_parsed_nested_name_specifier (parser);
4040 /* If we originally encountered this nested-name-specifier
4041 with IS_DECLARATION set to false, we will not have
4042 resolved TYPENAME_TYPEs, so we must do so here. */
4044 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4046 new_scope = resolve_typename_type (parser->scope,
4047 /*only_current_p=*/false);
4048 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4049 parser->scope = new_scope;
4055 /* Spot cases that cannot be the beginning of a
4056 nested-name-specifier. On the second and subsequent times
4057 through the loop, we look for the `template' keyword. */
4058 if (success && token->keyword == RID_TEMPLATE)
4060 /* A template-id can start a nested-name-specifier. */
4061 else if (token->type == CPP_TEMPLATE_ID)
4065 /* If the next token is not an identifier, then it is
4066 definitely not a type-name or namespace-name. */
4067 if (token->type != CPP_NAME)
4069 /* If the following token is neither a `<' (to begin a
4070 template-id), nor a `::', then we are not looking at a
4071 nested-name-specifier. */
4072 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4073 if (token->type != CPP_SCOPE
4074 && !cp_parser_nth_token_starts_template_argument_list_p
4079 /* The nested-name-specifier is optional, so we parse
4081 cp_parser_parse_tentatively (parser);
4083 /* Look for the optional `template' keyword, if this isn't the
4084 first time through the loop. */
4086 template_keyword_p = cp_parser_optional_template_keyword (parser);
4088 template_keyword_p = false;
4090 /* Save the old scope since the name lookup we are about to do
4091 might destroy it. */
4092 old_scope = parser->scope;
4093 saved_qualifying_scope = parser->qualifying_scope;
4094 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4095 look up names in "X<T>::I" in order to determine that "Y" is
4096 a template. So, if we have a typename at this point, we make
4097 an effort to look through it. */
4099 && !typename_keyword_p
4101 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4102 parser->scope = resolve_typename_type (parser->scope,
4103 /*only_current_p=*/false);
4104 /* Parse the qualifying entity. */
4106 = cp_parser_qualifying_entity (parser,
4112 /* Look for the `::' token. */
4113 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4115 /* If we found what we wanted, we keep going; otherwise, we're
4117 if (!cp_parser_parse_definitely (parser))
4119 bool error_p = false;
4121 /* Restore the OLD_SCOPE since it was valid before the
4122 failed attempt at finding the last
4123 class-or-namespace-name. */
4124 parser->scope = old_scope;
4125 parser->qualifying_scope = saved_qualifying_scope;
4126 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4128 /* If the next token is an identifier, and the one after
4129 that is a `::', then any valid interpretation would have
4130 found a class-or-namespace-name. */
4131 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4132 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4134 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4137 token = cp_lexer_consume_token (parser->lexer);
4140 if (!token->ambiguous_p)
4143 tree ambiguous_decls;
4145 decl = cp_parser_lookup_name (parser, token->u.value,
4147 /*is_template=*/false,
4148 /*is_namespace=*/false,
4149 /*check_dependency=*/true,
4152 if (TREE_CODE (decl) == TEMPLATE_DECL)
4153 error ("%H%qD used without template parameters",
4154 &token->location, decl);
4155 else if (ambiguous_decls)
4157 error ("%Hreference to %qD is ambiguous",
4158 &token->location, token->u.value);
4159 print_candidates (ambiguous_decls);
4160 decl = error_mark_node;
4164 const char* msg = "is not a class or namespace";
4165 if (cxx_dialect != cxx98)
4166 msg = "is not a class, namespace, or enumeration";
4167 cp_parser_name_lookup_error
4168 (parser, token->u.value, decl, msg,
4172 parser->scope = error_mark_node;
4174 /* Treat this as a successful nested-name-specifier
4179 If the name found is not a class-name (clause
4180 _class_) or namespace-name (_namespace.def_), the
4181 program is ill-formed. */
4184 cp_lexer_consume_token (parser->lexer);
4188 /* We've found one valid nested-name-specifier. */
4190 /* Name lookup always gives us a DECL. */
4191 if (TREE_CODE (new_scope) == TYPE_DECL)
4192 new_scope = TREE_TYPE (new_scope);
4193 /* Uses of "template" must be followed by actual templates. */
4194 if (template_keyword_p
4195 && !(CLASS_TYPE_P (new_scope)
4196 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4197 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4198 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4199 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4200 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4201 == TEMPLATE_ID_EXPR)))
4202 permerror (input_location, TYPE_P (new_scope)
4203 ? "%qT is not a template"
4204 : "%qD is not a template",
4206 /* If it is a class scope, try to complete it; we are about to
4207 be looking up names inside the class. */
4208 if (TYPE_P (new_scope)
4209 /* Since checking types for dependency can be expensive,
4210 avoid doing it if the type is already complete. */
4211 && !COMPLETE_TYPE_P (new_scope)
4212 /* Do not try to complete dependent types. */
4213 && !dependent_type_p (new_scope))
4215 new_scope = complete_type (new_scope);
4216 /* If it is a typedef to current class, use the current
4217 class instead, as the typedef won't have any names inside
4219 if (!COMPLETE_TYPE_P (new_scope)
4220 && currently_open_class (new_scope))
4221 new_scope = TYPE_MAIN_VARIANT (new_scope);
4223 /* Make sure we look in the right scope the next time through
4225 parser->scope = new_scope;
4228 /* If parsing tentatively, replace the sequence of tokens that makes
4229 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4230 token. That way, should we re-parse the token stream, we will
4231 not have to repeat the effort required to do the parse, nor will
4232 we issue duplicate error messages. */
4233 if (success && start)
4237 token = cp_lexer_token_at (parser->lexer, start);
4238 /* Reset the contents of the START token. */
4239 token->type = CPP_NESTED_NAME_SPECIFIER;
4240 /* Retrieve any deferred checks. Do not pop this access checks yet
4241 so the memory will not be reclaimed during token replacing below. */
4242 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4243 token->u.tree_check_value->value = parser->scope;
4244 token->u.tree_check_value->checks = get_deferred_access_checks ();
4245 token->u.tree_check_value->qualifying_scope =
4246 parser->qualifying_scope;
4247 token->keyword = RID_MAX;
4249 /* Purge all subsequent tokens. */
4250 cp_lexer_purge_tokens_after (parser->lexer, start);
4254 pop_to_parent_deferring_access_checks ();
4256 return success ? parser->scope : NULL_TREE;
4259 /* Parse a nested-name-specifier. See
4260 cp_parser_nested_name_specifier_opt for details. This function
4261 behaves identically, except that it will an issue an error if no
4262 nested-name-specifier is present. */
4265 cp_parser_nested_name_specifier (cp_parser *parser,
4266 bool typename_keyword_p,
4267 bool check_dependency_p,
4269 bool is_declaration)
4273 /* Look for the nested-name-specifier. */
4274 scope = cp_parser_nested_name_specifier_opt (parser,
4279 /* If it was not present, issue an error message. */
4282 cp_parser_error (parser, "expected nested-name-specifier");
4283 parser->scope = NULL_TREE;
4289 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4290 this is either a class-name or a namespace-name (which corresponds
4291 to the class-or-namespace-name production in the grammar). For
4292 C++0x, it can also be a type-name that refers to an enumeration
4295 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4296 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4297 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4298 TYPE_P is TRUE iff the next name should be taken as a class-name,
4299 even the same name is declared to be another entity in the same
4302 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4303 specified by the class-or-namespace-name. If neither is found the
4304 ERROR_MARK_NODE is returned. */
4307 cp_parser_qualifying_entity (cp_parser *parser,
4308 bool typename_keyword_p,
4309 bool template_keyword_p,
4310 bool check_dependency_p,
4312 bool is_declaration)
4315 tree saved_qualifying_scope;
4316 tree saved_object_scope;
4319 bool successful_parse_p;
4321 /* Before we try to parse the class-name, we must save away the
4322 current PARSER->SCOPE since cp_parser_class_name will destroy
4324 saved_scope = parser->scope;
4325 saved_qualifying_scope = parser->qualifying_scope;
4326 saved_object_scope = parser->object_scope;
4327 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4328 there is no need to look for a namespace-name. */
4329 only_class_p = template_keyword_p
4330 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4332 cp_parser_parse_tentatively (parser);
4333 scope = cp_parser_class_name (parser,
4336 type_p ? class_type : none_type,
4338 /*class_head_p=*/false,
4340 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4341 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4343 && cxx_dialect != cxx98
4344 && !successful_parse_p)
4346 /* Restore the saved scope. */
4347 parser->scope = saved_scope;
4348 parser->qualifying_scope = saved_qualifying_scope;
4349 parser->object_scope = saved_object_scope;
4351 /* Parse tentatively. */
4352 cp_parser_parse_tentatively (parser);
4354 /* Parse a typedef-name or enum-name. */
4355 scope = cp_parser_nonclass_name (parser);
4356 successful_parse_p = cp_parser_parse_definitely (parser);
4358 /* If that didn't work, try for a namespace-name. */
4359 if (!only_class_p && !successful_parse_p)
4361 /* Restore the saved scope. */
4362 parser->scope = saved_scope;
4363 parser->qualifying_scope = saved_qualifying_scope;
4364 parser->object_scope = saved_object_scope;
4365 /* If we are not looking at an identifier followed by the scope
4366 resolution operator, then this is not part of a
4367 nested-name-specifier. (Note that this function is only used
4368 to parse the components of a nested-name-specifier.) */
4369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4370 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4371 return error_mark_node;
4372 scope = cp_parser_namespace_name (parser);
4378 /* Parse a postfix-expression.
4382 postfix-expression [ expression ]
4383 postfix-expression ( expression-list [opt] )
4384 simple-type-specifier ( expression-list [opt] )
4385 typename :: [opt] nested-name-specifier identifier
4386 ( expression-list [opt] )
4387 typename :: [opt] nested-name-specifier template [opt] template-id
4388 ( expression-list [opt] )
4389 postfix-expression . template [opt] id-expression
4390 postfix-expression -> template [opt] id-expression
4391 postfix-expression . pseudo-destructor-name
4392 postfix-expression -> pseudo-destructor-name
4393 postfix-expression ++
4394 postfix-expression --
4395 dynamic_cast < type-id > ( expression )
4396 static_cast < type-id > ( expression )
4397 reinterpret_cast < type-id > ( expression )
4398 const_cast < type-id > ( expression )
4399 typeid ( expression )
4405 ( type-id ) { initializer-list , [opt] }
4407 This extension is a GNU version of the C99 compound-literal
4408 construct. (The C99 grammar uses `type-name' instead of `type-id',
4409 but they are essentially the same concept.)
4411 If ADDRESS_P is true, the postfix expression is the operand of the
4412 `&' operator. CAST_P is true if this expression is the target of a
4415 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4416 class member access expressions [expr.ref].
4418 Returns a representation of the expression. */
4421 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4422 bool member_access_only_p,
4423 cp_id_kind * pidk_return)
4427 cp_id_kind idk = CP_ID_KIND_NONE;
4428 tree postfix_expression = NULL_TREE;
4429 bool is_member_access = false;
4431 /* Peek at the next token. */
4432 token = cp_lexer_peek_token (parser->lexer);
4433 /* Some of the productions are determined by keywords. */
4434 keyword = token->keyword;
4444 const char *saved_message;
4446 /* All of these can be handled in the same way from the point
4447 of view of parsing. Begin by consuming the token
4448 identifying the cast. */
4449 cp_lexer_consume_token (parser->lexer);
4451 /* New types cannot be defined in the cast. */
4452 saved_message = parser->type_definition_forbidden_message;
4453 parser->type_definition_forbidden_message
4454 = "types may not be defined in casts";
4456 /* Look for the opening `<'. */
4457 cp_parser_require (parser, CPP_LESS, "%<<%>");
4458 /* Parse the type to which we are casting. */
4459 type = cp_parser_type_id (parser);
4460 /* Look for the closing `>'. */
4461 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4462 /* Restore the old message. */
4463 parser->type_definition_forbidden_message = saved_message;
4465 /* And the expression which is being cast. */
4466 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4467 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4468 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4470 /* Only type conversions to integral or enumeration types
4471 can be used in constant-expressions. */
4472 if (!cast_valid_in_integral_constant_expression_p (type)
4473 && (cp_parser_non_integral_constant_expression
4475 "a cast to a type other than an integral or "
4476 "enumeration type")))
4477 return error_mark_node;
4483 = build_dynamic_cast (type, expression, tf_warning_or_error);
4487 = build_static_cast (type, expression, tf_warning_or_error);
4491 = build_reinterpret_cast (type, expression,
4492 tf_warning_or_error);
4496 = build_const_cast (type, expression, tf_warning_or_error);
4507 const char *saved_message;
4508 bool saved_in_type_id_in_expr_p;
4510 /* Consume the `typeid' token. */
4511 cp_lexer_consume_token (parser->lexer);
4512 /* Look for the `(' token. */
4513 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4514 /* Types cannot be defined in a `typeid' expression. */
4515 saved_message = parser->type_definition_forbidden_message;
4516 parser->type_definition_forbidden_message
4517 = "types may not be defined in a %<typeid%> expression";
4518 /* We can't be sure yet whether we're looking at a type-id or an
4520 cp_parser_parse_tentatively (parser);
4521 /* Try a type-id first. */
4522 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4523 parser->in_type_id_in_expr_p = true;
4524 type = cp_parser_type_id (parser);
4525 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4526 /* Look for the `)' token. Otherwise, we can't be sure that
4527 we're not looking at an expression: consider `typeid (int
4528 (3))', for example. */
4529 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4530 /* If all went well, simply lookup the type-id. */
4531 if (cp_parser_parse_definitely (parser))
4532 postfix_expression = get_typeid (type);
4533 /* Otherwise, fall back to the expression variant. */
4538 /* Look for an expression. */
4539 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4540 /* Compute its typeid. */
4541 postfix_expression = build_typeid (expression);
4542 /* Look for the `)' token. */
4543 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4545 /* Restore the saved message. */
4546 parser->type_definition_forbidden_message = saved_message;
4547 /* `typeid' may not appear in an integral constant expression. */
4548 if (cp_parser_non_integral_constant_expression(parser,
4549 "%<typeid%> operator"))
4550 return error_mark_node;
4557 /* The syntax permitted here is the same permitted for an
4558 elaborated-type-specifier. */
4559 type = cp_parser_elaborated_type_specifier (parser,
4560 /*is_friend=*/false,
4561 /*is_declaration=*/false);
4562 postfix_expression = cp_parser_functional_cast (parser, type);
4570 /* If the next thing is a simple-type-specifier, we may be
4571 looking at a functional cast. We could also be looking at
4572 an id-expression. So, we try the functional cast, and if
4573 that doesn't work we fall back to the primary-expression. */
4574 cp_parser_parse_tentatively (parser);
4575 /* Look for the simple-type-specifier. */
4576 type = cp_parser_simple_type_specifier (parser,
4577 /*decl_specs=*/NULL,
4578 CP_PARSER_FLAGS_NONE);
4579 /* Parse the cast itself. */
4580 if (!cp_parser_error_occurred (parser))
4582 = cp_parser_functional_cast (parser, type);
4583 /* If that worked, we're done. */
4584 if (cp_parser_parse_definitely (parser))
4587 /* If the functional-cast didn't work out, try a
4588 compound-literal. */
4589 if (cp_parser_allow_gnu_extensions_p (parser)
4590 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4592 VEC(constructor_elt,gc) *initializer_list = NULL;
4593 bool saved_in_type_id_in_expr_p;
4595 cp_parser_parse_tentatively (parser);
4596 /* Consume the `('. */
4597 cp_lexer_consume_token (parser->lexer);
4598 /* Parse the type. */
4599 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4600 parser->in_type_id_in_expr_p = true;
4601 type = cp_parser_type_id (parser);
4602 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4603 /* Look for the `)'. */
4604 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4605 /* Look for the `{'. */
4606 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4607 /* If things aren't going well, there's no need to
4609 if (!cp_parser_error_occurred (parser))
4611 bool non_constant_p;
4612 /* Parse the initializer-list. */
4614 = cp_parser_initializer_list (parser, &non_constant_p);
4615 /* Allow a trailing `,'. */
4616 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4617 cp_lexer_consume_token (parser->lexer);
4618 /* Look for the final `}'. */
4619 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4621 /* If that worked, we're definitely looking at a
4622 compound-literal expression. */
4623 if (cp_parser_parse_definitely (parser))
4625 /* Warn the user that a compound literal is not
4626 allowed in standard C++. */
4627 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4628 /* For simplicity, we disallow compound literals in
4629 constant-expressions. We could
4630 allow compound literals of integer type, whose
4631 initializer was a constant, in constant
4632 expressions. Permitting that usage, as a further
4633 extension, would not change the meaning of any
4634 currently accepted programs. (Of course, as
4635 compound literals are not part of ISO C++, the
4636 standard has nothing to say.) */
4637 if (cp_parser_non_integral_constant_expression
4638 (parser, "non-constant compound literals"))
4640 postfix_expression = error_mark_node;
4643 /* Form the representation of the compound-literal. */
4645 = (finish_compound_literal
4646 (type, build_constructor (init_list_type_node,
4647 initializer_list)));
4652 /* It must be a primary-expression. */
4654 = cp_parser_primary_expression (parser, address_p, cast_p,
4655 /*template_arg_p=*/false,
4661 /* Keep looping until the postfix-expression is complete. */
4664 if (idk == CP_ID_KIND_UNQUALIFIED
4665 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4666 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4667 /* It is not a Koenig lookup function call. */
4669 = unqualified_name_lookup_error (postfix_expression);
4671 /* Peek at the next token. */
4672 token = cp_lexer_peek_token (parser->lexer);
4674 switch (token->type)
4676 case CPP_OPEN_SQUARE:
4678 = cp_parser_postfix_open_square_expression (parser,
4681 idk = CP_ID_KIND_NONE;
4682 is_member_access = false;
4685 case CPP_OPEN_PAREN:
4686 /* postfix-expression ( expression-list [opt] ) */
4689 bool is_builtin_constant_p;
4690 bool saved_integral_constant_expression_p = false;
4691 bool saved_non_integral_constant_expression_p = false;
4694 is_member_access = false;
4696 is_builtin_constant_p
4697 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4698 if (is_builtin_constant_p)
4700 /* The whole point of __builtin_constant_p is to allow
4701 non-constant expressions to appear as arguments. */
4702 saved_integral_constant_expression_p
4703 = parser->integral_constant_expression_p;
4704 saved_non_integral_constant_expression_p
4705 = parser->non_integral_constant_expression_p;
4706 parser->integral_constant_expression_p = false;
4708 args = (cp_parser_parenthesized_expression_list
4709 (parser, /*is_attribute_list=*/false,
4710 /*cast_p=*/false, /*allow_expansion_p=*/true,
4711 /*non_constant_p=*/NULL));
4712 if (is_builtin_constant_p)
4714 parser->integral_constant_expression_p
4715 = saved_integral_constant_expression_p;
4716 parser->non_integral_constant_expression_p
4717 = saved_non_integral_constant_expression_p;
4720 if (args == error_mark_node)
4722 postfix_expression = error_mark_node;
4726 /* Function calls are not permitted in
4727 constant-expressions. */
4728 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4729 && cp_parser_non_integral_constant_expression (parser,
4732 postfix_expression = error_mark_node;
4737 if (idk == CP_ID_KIND_UNQUALIFIED
4738 || idk == CP_ID_KIND_TEMPLATE_ID)
4740 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4745 if (!any_type_dependent_arguments_p (args))
4747 = perform_koenig_lookup (postfix_expression, args);
4751 = unqualified_fn_lookup_error (postfix_expression);
4753 /* We do not perform argument-dependent lookup if
4754 normal lookup finds a non-function, in accordance
4755 with the expected resolution of DR 218. */
4756 else if (args && is_overloaded_fn (postfix_expression))
4758 tree fn = get_first_fn (postfix_expression);
4760 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4761 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4763 /* Only do argument dependent lookup if regular
4764 lookup does not find a set of member functions.
4765 [basic.lookup.koenig]/2a */
4766 if (!DECL_FUNCTION_MEMBER_P (fn))
4769 if (!any_type_dependent_arguments_p (args))
4771 = perform_koenig_lookup (postfix_expression, args);
4776 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4778 tree instance = TREE_OPERAND (postfix_expression, 0);
4779 tree fn = TREE_OPERAND (postfix_expression, 1);
4781 if (processing_template_decl
4782 && (type_dependent_expression_p (instance)
4783 || (!BASELINK_P (fn)
4784 && TREE_CODE (fn) != FIELD_DECL)
4785 || type_dependent_expression_p (fn)
4786 || any_type_dependent_arguments_p (args)))
4789 = build_nt_call_list (postfix_expression, args);
4793 if (BASELINK_P (fn))
4796 = (build_new_method_call
4797 (instance, fn, args, NULL_TREE,
4798 (idk == CP_ID_KIND_QUALIFIED
4799 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4801 tf_warning_or_error));
4805 = finish_call_expr (postfix_expression, args,
4806 /*disallow_virtual=*/false,
4808 tf_warning_or_error);
4810 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4811 || TREE_CODE (postfix_expression) == MEMBER_REF
4812 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4813 postfix_expression = (build_offset_ref_call_from_tree
4814 (postfix_expression, args));
4815 else if (idk == CP_ID_KIND_QUALIFIED)
4816 /* A call to a static class member, or a namespace-scope
4819 = finish_call_expr (postfix_expression, args,
4820 /*disallow_virtual=*/true,
4822 tf_warning_or_error);
4824 /* All other function calls. */
4826 = finish_call_expr (postfix_expression, args,
4827 /*disallow_virtual=*/false,
4829 tf_warning_or_error);
4831 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4832 idk = CP_ID_KIND_NONE;
4838 /* postfix-expression . template [opt] id-expression
4839 postfix-expression . pseudo-destructor-name
4840 postfix-expression -> template [opt] id-expression
4841 postfix-expression -> pseudo-destructor-name */
4843 /* Consume the `.' or `->' operator. */
4844 cp_lexer_consume_token (parser->lexer);
4847 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4852 is_member_access = true;
4856 /* postfix-expression ++ */
4857 /* Consume the `++' token. */
4858 cp_lexer_consume_token (parser->lexer);
4859 /* Generate a representation for the complete expression. */
4861 = finish_increment_expr (postfix_expression,
4862 POSTINCREMENT_EXPR);
4863 /* Increments may not appear in constant-expressions. */
4864 if (cp_parser_non_integral_constant_expression (parser,
4866 postfix_expression = error_mark_node;
4867 idk = CP_ID_KIND_NONE;
4868 is_member_access = false;
4871 case CPP_MINUS_MINUS:
4872 /* postfix-expression -- */
4873 /* Consume the `--' token. */
4874 cp_lexer_consume_token (parser->lexer);
4875 /* Generate a representation for the complete expression. */
4877 = finish_increment_expr (postfix_expression,
4878 POSTDECREMENT_EXPR);
4879 /* Decrements may not appear in constant-expressions. */
4880 if (cp_parser_non_integral_constant_expression (parser,
4882 postfix_expression = error_mark_node;
4883 idk = CP_ID_KIND_NONE;
4884 is_member_access = false;
4888 if (pidk_return != NULL)
4889 * pidk_return = idk;
4890 if (member_access_only_p)
4891 return is_member_access? postfix_expression : error_mark_node;
4893 return postfix_expression;
4897 /* We should never get here. */
4899 return error_mark_node;
4902 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4903 by cp_parser_builtin_offsetof. We're looking for
4905 postfix-expression [ expression ]
4907 FOR_OFFSETOF is set if we're being called in that context, which
4908 changes how we deal with integer constant expressions. */
4911 cp_parser_postfix_open_square_expression (cp_parser *parser,
4912 tree postfix_expression,
4917 /* Consume the `[' token. */
4918 cp_lexer_consume_token (parser->lexer);
4920 /* Parse the index expression. */
4921 /* ??? For offsetof, there is a question of what to allow here. If
4922 offsetof is not being used in an integral constant expression context,
4923 then we *could* get the right answer by computing the value at runtime.
4924 If we are in an integral constant expression context, then we might
4925 could accept any constant expression; hard to say without analysis.
4926 Rather than open the barn door too wide right away, allow only integer
4927 constant expressions here. */
4929 index = cp_parser_constant_expression (parser, false, NULL);
4931 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4933 /* Look for the closing `]'. */
4934 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4936 /* Build the ARRAY_REF. */
4937 postfix_expression = grok_array_decl (postfix_expression, index);
4939 /* When not doing offsetof, array references are not permitted in
4940 constant-expressions. */
4942 && (cp_parser_non_integral_constant_expression
4943 (parser, "an array reference")))
4944 postfix_expression = error_mark_node;
4946 return postfix_expression;
4949 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4950 by cp_parser_builtin_offsetof. We're looking for
4952 postfix-expression . template [opt] id-expression
4953 postfix-expression . pseudo-destructor-name
4954 postfix-expression -> template [opt] id-expression
4955 postfix-expression -> pseudo-destructor-name
4957 FOR_OFFSETOF is set if we're being called in that context. That sorta
4958 limits what of the above we'll actually accept, but nevermind.
4959 TOKEN_TYPE is the "." or "->" token, which will already have been
4960 removed from the stream. */
4963 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4964 enum cpp_ttype token_type,
4965 tree postfix_expression,
4966 bool for_offsetof, cp_id_kind *idk,
4967 location_t location)
4971 bool pseudo_destructor_p;
4972 tree scope = NULL_TREE;
4974 /* If this is a `->' operator, dereference the pointer. */
4975 if (token_type == CPP_DEREF)
4976 postfix_expression = build_x_arrow (postfix_expression);
4977 /* Check to see whether or not the expression is type-dependent. */
4978 dependent_p = type_dependent_expression_p (postfix_expression);
4979 /* The identifier following the `->' or `.' is not qualified. */
4980 parser->scope = NULL_TREE;
4981 parser->qualifying_scope = NULL_TREE;
4982 parser->object_scope = NULL_TREE;
4983 *idk = CP_ID_KIND_NONE;
4985 /* Enter the scope corresponding to the type of the object
4986 given by the POSTFIX_EXPRESSION. */
4987 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4989 scope = TREE_TYPE (postfix_expression);
4990 /* According to the standard, no expression should ever have
4991 reference type. Unfortunately, we do not currently match
4992 the standard in this respect in that our internal representation
4993 of an expression may have reference type even when the standard
4994 says it does not. Therefore, we have to manually obtain the
4995 underlying type here. */
4996 scope = non_reference (scope);
4997 /* The type of the POSTFIX_EXPRESSION must be complete. */
4998 if (scope == unknown_type_node)
5000 error ("%H%qE does not have class type", &location, postfix_expression);
5004 scope = complete_type_or_else (scope, NULL_TREE);
5005 /* Let the name lookup machinery know that we are processing a
5006 class member access expression. */
5007 parser->context->object_type = scope;
5008 /* If something went wrong, we want to be able to discern that case,
5009 as opposed to the case where there was no SCOPE due to the type
5010 of expression being dependent. */
5012 scope = error_mark_node;
5013 /* If the SCOPE was erroneous, make the various semantic analysis
5014 functions exit quickly -- and without issuing additional error
5016 if (scope == error_mark_node)
5017 postfix_expression = error_mark_node;
5020 /* Assume this expression is not a pseudo-destructor access. */
5021 pseudo_destructor_p = false;
5023 /* If the SCOPE is a scalar type, then, if this is a valid program,
5024 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5025 is type dependent, it can be pseudo-destructor-name or something else.
5026 Try to parse it as pseudo-destructor-name first. */
5027 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5032 cp_parser_parse_tentatively (parser);
5033 /* Parse the pseudo-destructor-name. */
5035 cp_parser_pseudo_destructor_name (parser, &s, &type);
5037 && (cp_parser_error_occurred (parser)
5038 || TREE_CODE (type) != TYPE_DECL
5039 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5040 cp_parser_abort_tentative_parse (parser);
5041 else if (cp_parser_parse_definitely (parser))
5043 pseudo_destructor_p = true;
5045 = finish_pseudo_destructor_expr (postfix_expression,
5046 s, TREE_TYPE (type));
5050 if (!pseudo_destructor_p)
5052 /* If the SCOPE is not a scalar type, we are looking at an
5053 ordinary class member access expression, rather than a
5054 pseudo-destructor-name. */
5056 cp_token *token = cp_lexer_peek_token (parser->lexer);
5057 /* Parse the id-expression. */
5058 name = (cp_parser_id_expression
5060 cp_parser_optional_template_keyword (parser),
5061 /*check_dependency_p=*/true,
5063 /*declarator_p=*/false,
5064 /*optional_p=*/false));
5065 /* In general, build a SCOPE_REF if the member name is qualified.
5066 However, if the name was not dependent and has already been
5067 resolved; there is no need to build the SCOPE_REF. For example;
5069 struct X { void f(); };
5070 template <typename T> void f(T* t) { t->X::f(); }
5072 Even though "t" is dependent, "X::f" is not and has been resolved
5073 to a BASELINK; there is no need to include scope information. */
5075 /* But we do need to remember that there was an explicit scope for
5076 virtual function calls. */
5078 *idk = CP_ID_KIND_QUALIFIED;
5080 /* If the name is a template-id that names a type, we will get a
5081 TYPE_DECL here. That is invalid code. */
5082 if (TREE_CODE (name) == TYPE_DECL)
5084 error ("%Hinvalid use of %qD", &token->location, name);
5085 postfix_expression = error_mark_node;
5089 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5091 name = build_qualified_name (/*type=*/NULL_TREE,
5095 parser->scope = NULL_TREE;
5096 parser->qualifying_scope = NULL_TREE;
5097 parser->object_scope = NULL_TREE;
5099 if (scope && name && BASELINK_P (name))
5100 adjust_result_of_qualified_name_lookup
5101 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5103 = finish_class_member_access_expr (postfix_expression, name,
5105 tf_warning_or_error);
5109 /* We no longer need to look up names in the scope of the object on
5110 the left-hand side of the `.' or `->' operator. */
5111 parser->context->object_type = NULL_TREE;
5113 /* Outside of offsetof, these operators may not appear in
5114 constant-expressions. */
5116 && (cp_parser_non_integral_constant_expression
5117 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5118 postfix_expression = error_mark_node;
5120 return postfix_expression;
5123 /* Parse a parenthesized expression-list.
5126 assignment-expression
5127 expression-list, assignment-expression
5132 identifier, expression-list
5134 CAST_P is true if this expression is the target of a cast.
5136 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5139 Returns a TREE_LIST. The TREE_VALUE of each node is a
5140 representation of an assignment-expression. Note that a TREE_LIST
5141 is returned even if there is only a single expression in the list.
5142 error_mark_node is returned if the ( and or ) are
5143 missing. NULL_TREE is returned on no expressions. The parentheses
5144 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5145 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5146 indicates whether or not all of the expressions in the list were
5150 cp_parser_parenthesized_expression_list (cp_parser* parser,
5151 bool is_attribute_list,
5153 bool allow_expansion_p,
5154 bool *non_constant_p)
5156 tree expression_list = NULL_TREE;
5157 bool fold_expr_p = is_attribute_list;
5158 tree identifier = NULL_TREE;
5159 bool saved_greater_than_is_operator_p;
5161 /* Assume all the expressions will be constant. */
5163 *non_constant_p = false;
5165 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5166 return error_mark_node;
5168 /* Within a parenthesized expression, a `>' token is always
5169 the greater-than operator. */
5170 saved_greater_than_is_operator_p
5171 = parser->greater_than_is_operator_p;
5172 parser->greater_than_is_operator_p = true;
5174 /* Consume expressions until there are no more. */
5175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5180 /* At the beginning of attribute lists, check to see if the
5181 next token is an identifier. */
5182 if (is_attribute_list
5183 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5187 /* Consume the identifier. */
5188 token = cp_lexer_consume_token (parser->lexer);
5189 /* Save the identifier. */
5190 identifier = token->u.value;
5194 bool expr_non_constant_p;
5196 /* Parse the next assignment-expression. */
5197 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5199 /* A braced-init-list. */
5200 maybe_warn_cpp0x ("extended initializer lists");
5201 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5202 if (non_constant_p && expr_non_constant_p)
5203 *non_constant_p = true;
5205 else if (non_constant_p)
5207 expr = (cp_parser_constant_expression
5208 (parser, /*allow_non_constant_p=*/true,
5209 &expr_non_constant_p));
5210 if (expr_non_constant_p)
5211 *non_constant_p = true;
5214 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5217 expr = fold_non_dependent_expr (expr);
5219 /* If we have an ellipsis, then this is an expression
5221 if (allow_expansion_p
5222 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5224 /* Consume the `...'. */
5225 cp_lexer_consume_token (parser->lexer);
5227 /* Build the argument pack. */
5228 expr = make_pack_expansion (expr);
5231 /* Add it to the list. We add error_mark_node
5232 expressions to the list, so that we can still tell if
5233 the correct form for a parenthesized expression-list
5234 is found. That gives better errors. */
5235 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5237 if (expr == error_mark_node)
5241 /* After the first item, attribute lists look the same as
5242 expression lists. */
5243 is_attribute_list = false;
5246 /* If the next token isn't a `,', then we are done. */
5247 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5250 /* Otherwise, consume the `,' and keep going. */
5251 cp_lexer_consume_token (parser->lexer);
5254 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5259 /* We try and resync to an unnested comma, as that will give the
5260 user better diagnostics. */
5261 ending = cp_parser_skip_to_closing_parenthesis (parser,
5262 /*recovering=*/true,
5264 /*consume_paren=*/true);
5269 parser->greater_than_is_operator_p
5270 = saved_greater_than_is_operator_p;
5271 return error_mark_node;
5275 parser->greater_than_is_operator_p
5276 = saved_greater_than_is_operator_p;
5278 /* We built up the list in reverse order so we must reverse it now. */
5279 expression_list = nreverse (expression_list);
5281 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5283 return expression_list;
5286 /* Parse a pseudo-destructor-name.
5288 pseudo-destructor-name:
5289 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5290 :: [opt] nested-name-specifier template template-id :: ~ type-name
5291 :: [opt] nested-name-specifier [opt] ~ type-name
5293 If either of the first two productions is used, sets *SCOPE to the
5294 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5295 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5296 or ERROR_MARK_NODE if the parse fails. */
5299 cp_parser_pseudo_destructor_name (cp_parser* parser,
5303 bool nested_name_specifier_p;
5305 /* Assume that things will not work out. */
5306 *type = error_mark_node;
5308 /* Look for the optional `::' operator. */
5309 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5310 /* Look for the optional nested-name-specifier. */
5311 nested_name_specifier_p
5312 = (cp_parser_nested_name_specifier_opt (parser,
5313 /*typename_keyword_p=*/false,
5314 /*check_dependency_p=*/true,
5316 /*is_declaration=*/false)
5318 /* Now, if we saw a nested-name-specifier, we might be doing the
5319 second production. */
5320 if (nested_name_specifier_p
5321 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5323 /* Consume the `template' keyword. */
5324 cp_lexer_consume_token (parser->lexer);
5325 /* Parse the template-id. */
5326 cp_parser_template_id (parser,
5327 /*template_keyword_p=*/true,
5328 /*check_dependency_p=*/false,
5329 /*is_declaration=*/true);
5330 /* Look for the `::' token. */
5331 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5333 /* If the next token is not a `~', then there might be some
5334 additional qualification. */
5335 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5337 /* At this point, we're looking for "type-name :: ~". The type-name
5338 must not be a class-name, since this is a pseudo-destructor. So,
5339 it must be either an enum-name, or a typedef-name -- both of which
5340 are just identifiers. So, we peek ahead to check that the "::"
5341 and "~" tokens are present; if they are not, then we can avoid
5342 calling type_name. */
5343 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5344 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5345 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5347 cp_parser_error (parser, "non-scalar type");
5351 /* Look for the type-name. */
5352 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5353 if (*scope == error_mark_node)
5356 /* Look for the `::' token. */
5357 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5362 /* Look for the `~'. */
5363 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5364 /* Look for the type-name again. We are not responsible for
5365 checking that it matches the first type-name. */
5366 *type = cp_parser_nonclass_name (parser);
5369 /* Parse a unary-expression.
5375 unary-operator cast-expression
5376 sizeof unary-expression
5384 __extension__ cast-expression
5385 __alignof__ unary-expression
5386 __alignof__ ( type-id )
5387 __real__ cast-expression
5388 __imag__ cast-expression
5391 ADDRESS_P is true iff the unary-expression is appearing as the
5392 operand of the `&' operator. CAST_P is true if this expression is
5393 the target of a cast.
5395 Returns a representation of the expression. */
5398 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5402 enum tree_code unary_operator;
5404 /* Peek at the next token. */
5405 token = cp_lexer_peek_token (parser->lexer);
5406 /* Some keywords give away the kind of expression. */
5407 if (token->type == CPP_KEYWORD)
5409 enum rid keyword = token->keyword;
5419 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5420 /* Consume the token. */
5421 cp_lexer_consume_token (parser->lexer);
5422 /* Parse the operand. */
5423 operand = cp_parser_sizeof_operand (parser, keyword);
5425 if (TYPE_P (operand))
5426 return cxx_sizeof_or_alignof_type (operand, op, true);
5428 return cxx_sizeof_or_alignof_expr (operand, op, true);
5432 return cp_parser_new_expression (parser);
5435 return cp_parser_delete_expression (parser);
5439 /* The saved value of the PEDANTIC flag. */
5443 /* Save away the PEDANTIC flag. */
5444 cp_parser_extension_opt (parser, &saved_pedantic);
5445 /* Parse the cast-expression. */
5446 expr = cp_parser_simple_cast_expression (parser);
5447 /* Restore the PEDANTIC flag. */
5448 pedantic = saved_pedantic;
5458 /* Consume the `__real__' or `__imag__' token. */
5459 cp_lexer_consume_token (parser->lexer);
5460 /* Parse the cast-expression. */
5461 expression = cp_parser_simple_cast_expression (parser);
5462 /* Create the complete representation. */
5463 return build_x_unary_op ((keyword == RID_REALPART
5464 ? REALPART_EXPR : IMAGPART_EXPR),
5466 tf_warning_or_error);
5475 /* Look for the `:: new' and `:: delete', which also signal the
5476 beginning of a new-expression, or delete-expression,
5477 respectively. If the next token is `::', then it might be one of
5479 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5483 /* See if the token after the `::' is one of the keywords in
5484 which we're interested. */
5485 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5486 /* If it's `new', we have a new-expression. */
5487 if (keyword == RID_NEW)
5488 return cp_parser_new_expression (parser);
5489 /* Similarly, for `delete'. */
5490 else if (keyword == RID_DELETE)
5491 return cp_parser_delete_expression (parser);
5494 /* Look for a unary operator. */
5495 unary_operator = cp_parser_unary_operator (token);
5496 /* The `++' and `--' operators can be handled similarly, even though
5497 they are not technically unary-operators in the grammar. */
5498 if (unary_operator == ERROR_MARK)
5500 if (token->type == CPP_PLUS_PLUS)
5501 unary_operator = PREINCREMENT_EXPR;
5502 else if (token->type == CPP_MINUS_MINUS)
5503 unary_operator = PREDECREMENT_EXPR;
5504 /* Handle the GNU address-of-label extension. */
5505 else if (cp_parser_allow_gnu_extensions_p (parser)
5506 && token->type == CPP_AND_AND)
5510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5512 /* Consume the '&&' token. */
5513 cp_lexer_consume_token (parser->lexer);
5514 /* Look for the identifier. */
5515 identifier = cp_parser_identifier (parser);
5516 /* Create an expression representing the address. */
5517 expression = finish_label_address_expr (identifier, loc);
5518 if (cp_parser_non_integral_constant_expression (parser,
5519 "the address of a label"))
5520 expression = error_mark_node;
5524 if (unary_operator != ERROR_MARK)
5526 tree cast_expression;
5527 tree expression = error_mark_node;
5528 const char *non_constant_p = NULL;
5530 /* Consume the operator token. */
5531 token = cp_lexer_consume_token (parser->lexer);
5532 /* Parse the cast-expression. */
5534 = cp_parser_cast_expression (parser,
5535 unary_operator == ADDR_EXPR,
5536 /*cast_p=*/false, pidk);
5537 /* Now, build an appropriate representation. */
5538 switch (unary_operator)
5541 non_constant_p = "%<*%>";
5542 expression = build_x_indirect_ref (cast_expression, "unary *",
5543 tf_warning_or_error);
5547 non_constant_p = "%<&%>";
5550 expression = build_x_unary_op (unary_operator, cast_expression,
5551 tf_warning_or_error);
5554 case PREINCREMENT_EXPR:
5555 case PREDECREMENT_EXPR:
5556 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5557 ? "%<++%>" : "%<--%>");
5559 case UNARY_PLUS_EXPR:
5561 case TRUTH_NOT_EXPR:
5562 expression = finish_unary_op_expr (unary_operator, cast_expression);
5570 && cp_parser_non_integral_constant_expression (parser,
5572 expression = error_mark_node;
5577 return cp_parser_postfix_expression (parser, address_p, cast_p,
5578 /*member_access_only_p=*/false,
5582 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5583 unary-operator, the corresponding tree code is returned. */
5585 static enum tree_code
5586 cp_parser_unary_operator (cp_token* token)
5588 switch (token->type)
5591 return INDIRECT_REF;
5597 return UNARY_PLUS_EXPR;
5603 return TRUTH_NOT_EXPR;
5606 return BIT_NOT_EXPR;
5613 /* Parse a new-expression.
5616 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5617 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5619 Returns a representation of the expression. */
5622 cp_parser_new_expression (cp_parser* parser)
5624 bool global_scope_p;
5630 /* Look for the optional `::' operator. */
5632 = (cp_parser_global_scope_opt (parser,
5633 /*current_scope_valid_p=*/false)
5635 /* Look for the `new' operator. */
5636 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5637 /* There's no easy way to tell a new-placement from the
5638 `( type-id )' construct. */
5639 cp_parser_parse_tentatively (parser);
5640 /* Look for a new-placement. */
5641 placement = cp_parser_new_placement (parser);
5642 /* If that didn't work out, there's no new-placement. */
5643 if (!cp_parser_parse_definitely (parser))
5644 placement = NULL_TREE;
5646 /* If the next token is a `(', then we have a parenthesized
5648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5651 /* Consume the `('. */
5652 cp_lexer_consume_token (parser->lexer);
5653 /* Parse the type-id. */
5654 type = cp_parser_type_id (parser);
5655 /* Look for the closing `)'. */
5656 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5657 token = cp_lexer_peek_token (parser->lexer);
5658 /* There should not be a direct-new-declarator in this production,
5659 but GCC used to allowed this, so we check and emit a sensible error
5660 message for this case. */
5661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5663 error ("%Harray bound forbidden after parenthesized type-id",
5665 inform (token->location,
5666 "try removing the parentheses around the type-id");
5667 cp_parser_direct_new_declarator (parser);
5671 /* Otherwise, there must be a new-type-id. */
5673 type = cp_parser_new_type_id (parser, &nelts);
5675 /* If the next token is a `(' or '{', then we have a new-initializer. */
5676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5677 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5678 initializer = cp_parser_new_initializer (parser);
5680 initializer = NULL_TREE;
5682 /* A new-expression may not appear in an integral constant
5684 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5685 return error_mark_node;
5687 /* Create a representation of the new-expression. */
5688 return build_new (placement, type, nelts, initializer, global_scope_p,
5689 tf_warning_or_error);
5692 /* Parse a new-placement.
5697 Returns the same representation as for an expression-list. */
5700 cp_parser_new_placement (cp_parser* parser)
5702 tree expression_list;
5704 /* Parse the expression-list. */
5705 expression_list = (cp_parser_parenthesized_expression_list
5706 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5707 /*non_constant_p=*/NULL));
5709 return expression_list;
5712 /* Parse a new-type-id.
5715 type-specifier-seq new-declarator [opt]
5717 Returns the TYPE allocated. If the new-type-id indicates an array
5718 type, *NELTS is set to the number of elements in the last array
5719 bound; the TYPE will not include the last array bound. */
5722 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5724 cp_decl_specifier_seq type_specifier_seq;
5725 cp_declarator *new_declarator;
5726 cp_declarator *declarator;
5727 cp_declarator *outer_declarator;
5728 const char *saved_message;
5731 /* The type-specifier sequence must not contain type definitions.
5732 (It cannot contain declarations of new types either, but if they
5733 are not definitions we will catch that because they are not
5735 saved_message = parser->type_definition_forbidden_message;
5736 parser->type_definition_forbidden_message
5737 = "types may not be defined in a new-type-id";
5738 /* Parse the type-specifier-seq. */
5739 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5740 &type_specifier_seq);
5741 /* Restore the old message. */
5742 parser->type_definition_forbidden_message = saved_message;
5743 /* Parse the new-declarator. */
5744 new_declarator = cp_parser_new_declarator_opt (parser);
5746 /* Determine the number of elements in the last array dimension, if
5749 /* Skip down to the last array dimension. */
5750 declarator = new_declarator;
5751 outer_declarator = NULL;
5752 while (declarator && (declarator->kind == cdk_pointer
5753 || declarator->kind == cdk_ptrmem))
5755 outer_declarator = declarator;
5756 declarator = declarator->declarator;
5759 && declarator->kind == cdk_array
5760 && declarator->declarator
5761 && declarator->declarator->kind == cdk_array)
5763 outer_declarator = declarator;
5764 declarator = declarator->declarator;
5767 if (declarator && declarator->kind == cdk_array)
5769 *nelts = declarator->u.array.bounds;
5770 if (*nelts == error_mark_node)
5771 *nelts = integer_one_node;
5773 if (outer_declarator)
5774 outer_declarator->declarator = declarator->declarator;
5776 new_declarator = NULL;
5779 type = groktypename (&type_specifier_seq, new_declarator, false);
5783 /* Parse an (optional) new-declarator.
5786 ptr-operator new-declarator [opt]
5787 direct-new-declarator
5789 Returns the declarator. */
5791 static cp_declarator *
5792 cp_parser_new_declarator_opt (cp_parser* parser)
5794 enum tree_code code;
5796 cp_cv_quals cv_quals;
5798 /* We don't know if there's a ptr-operator next, or not. */
5799 cp_parser_parse_tentatively (parser);
5800 /* Look for a ptr-operator. */
5801 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5802 /* If that worked, look for more new-declarators. */
5803 if (cp_parser_parse_definitely (parser))
5805 cp_declarator *declarator;
5807 /* Parse another optional declarator. */
5808 declarator = cp_parser_new_declarator_opt (parser);
5810 return cp_parser_make_indirect_declarator
5811 (code, type, cv_quals, declarator);
5814 /* If the next token is a `[', there is a direct-new-declarator. */
5815 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5816 return cp_parser_direct_new_declarator (parser);
5821 /* Parse a direct-new-declarator.
5823 direct-new-declarator:
5825 direct-new-declarator [constant-expression]
5829 static cp_declarator *
5830 cp_parser_direct_new_declarator (cp_parser* parser)
5832 cp_declarator *declarator = NULL;
5838 /* Look for the opening `['. */
5839 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5840 /* The first expression is not required to be constant. */
5843 cp_token *token = cp_lexer_peek_token (parser->lexer);
5844 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5845 /* The standard requires that the expression have integral
5846 type. DR 74 adds enumeration types. We believe that the
5847 real intent is that these expressions be handled like the
5848 expression in a `switch' condition, which also allows
5849 classes with a single conversion to integral or
5850 enumeration type. */
5851 if (!processing_template_decl)
5854 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5859 error ("%Hexpression in new-declarator must have integral "
5860 "or enumeration type", &token->location);
5861 expression = error_mark_node;
5865 /* But all the other expressions must be. */
5868 = cp_parser_constant_expression (parser,
5869 /*allow_non_constant=*/false,
5871 /* Look for the closing `]'. */
5872 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5874 /* Add this bound to the declarator. */
5875 declarator = make_array_declarator (declarator, expression);
5877 /* If the next token is not a `[', then there are no more
5879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5886 /* Parse a new-initializer.
5889 ( expression-list [opt] )
5892 Returns a representation of the expression-list. If there is no
5893 expression-list, VOID_ZERO_NODE is returned. */
5896 cp_parser_new_initializer (cp_parser* parser)
5898 tree expression_list;
5900 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5902 bool expr_non_constant_p;
5903 maybe_warn_cpp0x ("extended initializer lists");
5904 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5905 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5906 expression_list = build_tree_list (NULL_TREE, expression_list);
5909 expression_list = (cp_parser_parenthesized_expression_list
5910 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5911 /*non_constant_p=*/NULL));
5912 if (!expression_list)
5913 expression_list = void_zero_node;
5915 return expression_list;
5918 /* Parse a delete-expression.
5921 :: [opt] delete cast-expression
5922 :: [opt] delete [ ] cast-expression
5924 Returns a representation of the expression. */
5927 cp_parser_delete_expression (cp_parser* parser)
5929 bool global_scope_p;
5933 /* Look for the optional `::' operator. */
5935 = (cp_parser_global_scope_opt (parser,
5936 /*current_scope_valid_p=*/false)
5938 /* Look for the `delete' keyword. */
5939 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5940 /* See if the array syntax is in use. */
5941 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5943 /* Consume the `[' token. */
5944 cp_lexer_consume_token (parser->lexer);
5945 /* Look for the `]' token. */
5946 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5947 /* Remember that this is the `[]' construct. */
5953 /* Parse the cast-expression. */
5954 expression = cp_parser_simple_cast_expression (parser);
5956 /* A delete-expression may not appear in an integral constant
5958 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5959 return error_mark_node;
5961 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5964 /* Returns true if TOKEN may start a cast-expression and false
5968 cp_parser_token_starts_cast_expression (cp_token *token)
5970 switch (token->type)
5976 case CPP_CLOSE_SQUARE:
5977 case CPP_CLOSE_PAREN:
5978 case CPP_CLOSE_BRACE:
5982 case CPP_DEREF_STAR:
5990 case CPP_GREATER_EQ:
6010 /* '[' may start a primary-expression in obj-c++. */
6011 case CPP_OPEN_SQUARE:
6012 return c_dialect_objc ();
6019 /* Parse a cast-expression.
6023 ( type-id ) cast-expression
6025 ADDRESS_P is true iff the unary-expression is appearing as the
6026 operand of the `&' operator. CAST_P is true if this expression is
6027 the target of a cast.
6029 Returns a representation of the expression. */
6032 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6035 /* If it's a `(', then we might be looking at a cast. */
6036 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6038 tree type = NULL_TREE;
6039 tree expr = NULL_TREE;
6040 bool compound_literal_p;
6041 const char *saved_message;
6043 /* There's no way to know yet whether or not this is a cast.
6044 For example, `(int (3))' is a unary-expression, while `(int)
6045 3' is a cast. So, we resort to parsing tentatively. */
6046 cp_parser_parse_tentatively (parser);
6047 /* Types may not be defined in a cast. */
6048 saved_message = parser->type_definition_forbidden_message;
6049 parser->type_definition_forbidden_message
6050 = "types may not be defined in casts";
6051 /* Consume the `('. */
6052 cp_lexer_consume_token (parser->lexer);
6053 /* A very tricky bit is that `(struct S) { 3 }' is a
6054 compound-literal (which we permit in C++ as an extension).
6055 But, that construct is not a cast-expression -- it is a
6056 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6057 is legal; if the compound-literal were a cast-expression,
6058 you'd need an extra set of parentheses.) But, if we parse
6059 the type-id, and it happens to be a class-specifier, then we
6060 will commit to the parse at that point, because we cannot
6061 undo the action that is done when creating a new class. So,
6062 then we cannot back up and do a postfix-expression.
6064 Therefore, we scan ahead to the closing `)', and check to see
6065 if the token after the `)' is a `{'. If so, we are not
6066 looking at a cast-expression.
6068 Save tokens so that we can put them back. */
6069 cp_lexer_save_tokens (parser->lexer);
6070 /* Skip tokens until the next token is a closing parenthesis.
6071 If we find the closing `)', and the next token is a `{', then
6072 we are looking at a compound-literal. */
6074 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6075 /*consume_paren=*/true)
6076 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6077 /* Roll back the tokens we skipped. */
6078 cp_lexer_rollback_tokens (parser->lexer);
6079 /* If we were looking at a compound-literal, simulate an error
6080 so that the call to cp_parser_parse_definitely below will
6082 if (compound_literal_p)
6083 cp_parser_simulate_error (parser);
6086 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6087 parser->in_type_id_in_expr_p = true;
6088 /* Look for the type-id. */
6089 type = cp_parser_type_id (parser);
6090 /* Look for the closing `)'. */
6091 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6092 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6095 /* Restore the saved message. */
6096 parser->type_definition_forbidden_message = saved_message;
6098 /* At this point this can only be either a cast or a
6099 parenthesized ctor such as `(T ())' that looks like a cast to
6100 function returning T. */
6101 if (!cp_parser_error_occurred (parser)
6102 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6105 cp_parser_parse_definitely (parser);
6106 expr = cp_parser_cast_expression (parser,
6107 /*address_p=*/false,
6108 /*cast_p=*/true, pidk);
6110 /* Warn about old-style casts, if so requested. */
6111 if (warn_old_style_cast
6112 && !in_system_header
6113 && !VOID_TYPE_P (type)
6114 && current_lang_name != lang_name_c)
6115 warning (OPT_Wold_style_cast, "use of old-style cast");
6117 /* Only type conversions to integral or enumeration types
6118 can be used in constant-expressions. */
6119 if (!cast_valid_in_integral_constant_expression_p (type)
6120 && (cp_parser_non_integral_constant_expression
6122 "a cast to a type other than an integral or "
6123 "enumeration type")))
6124 return error_mark_node;
6126 /* Perform the cast. */
6127 expr = build_c_cast (type, expr);
6131 cp_parser_abort_tentative_parse (parser);
6134 /* If we get here, then it's not a cast, so it must be a
6135 unary-expression. */
6136 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6139 /* Parse a binary expression of the general form:
6143 pm-expression .* cast-expression
6144 pm-expression ->* cast-expression
6146 multiplicative-expression:
6148 multiplicative-expression * pm-expression
6149 multiplicative-expression / pm-expression
6150 multiplicative-expression % pm-expression
6152 additive-expression:
6153 multiplicative-expression
6154 additive-expression + multiplicative-expression
6155 additive-expression - multiplicative-expression
6159 shift-expression << additive-expression
6160 shift-expression >> additive-expression
6162 relational-expression:
6164 relational-expression < shift-expression
6165 relational-expression > shift-expression
6166 relational-expression <= shift-expression
6167 relational-expression >= shift-expression
6171 relational-expression:
6172 relational-expression <? shift-expression
6173 relational-expression >? shift-expression
6175 equality-expression:
6176 relational-expression
6177 equality-expression == relational-expression
6178 equality-expression != relational-expression
6182 and-expression & equality-expression
6184 exclusive-or-expression:
6186 exclusive-or-expression ^ and-expression
6188 inclusive-or-expression:
6189 exclusive-or-expression
6190 inclusive-or-expression | exclusive-or-expression
6192 logical-and-expression:
6193 inclusive-or-expression
6194 logical-and-expression && inclusive-or-expression
6196 logical-or-expression:
6197 logical-and-expression
6198 logical-or-expression || logical-and-expression
6200 All these are implemented with a single function like:
6203 simple-cast-expression
6204 binary-expression <token> binary-expression
6206 CAST_P is true if this expression is the target of a cast.
6208 The binops_by_token map is used to get the tree codes for each <token> type.
6209 binary-expressions are associated according to a precedence table. */
6211 #define TOKEN_PRECEDENCE(token) \
6212 (((token->type == CPP_GREATER \
6213 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6214 && !parser->greater_than_is_operator_p) \
6215 ? PREC_NOT_OPERATOR \
6216 : binops_by_token[token->type].prec)
6219 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6220 bool no_toplevel_fold_p,
6221 enum cp_parser_prec prec,
6224 cp_parser_expression_stack stack;
6225 cp_parser_expression_stack_entry *sp = &stack[0];
6228 enum tree_code tree_type, lhs_type, rhs_type;
6229 enum cp_parser_prec new_prec, lookahead_prec;
6232 /* Parse the first expression. */
6233 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6234 lhs_type = ERROR_MARK;
6238 /* Get an operator token. */
6239 token = cp_lexer_peek_token (parser->lexer);
6241 if (warn_cxx0x_compat
6242 && token->type == CPP_RSHIFT
6243 && !parser->greater_than_is_operator_p)
6245 warning (OPT_Wc__0x_compat,
6246 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6248 warning (OPT_Wc__0x_compat,
6249 "suggest parentheses around %<>>%> expression");
6252 new_prec = TOKEN_PRECEDENCE (token);
6254 /* Popping an entry off the stack means we completed a subexpression:
6255 - either we found a token which is not an operator (`>' where it is not
6256 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6257 will happen repeatedly;
6258 - or, we found an operator which has lower priority. This is the case
6259 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6261 if (new_prec <= prec)
6270 tree_type = binops_by_token[token->type].tree_type;
6272 /* We used the operator token. */
6273 cp_lexer_consume_token (parser->lexer);
6275 /* Extract another operand. It may be the RHS of this expression
6276 or the LHS of a new, higher priority expression. */
6277 rhs = cp_parser_simple_cast_expression (parser);
6278 rhs_type = ERROR_MARK;
6280 /* Get another operator token. Look up its precedence to avoid
6281 building a useless (immediately popped) stack entry for common
6282 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6283 token = cp_lexer_peek_token (parser->lexer);
6284 lookahead_prec = TOKEN_PRECEDENCE (token);
6285 if (lookahead_prec > new_prec)
6287 /* ... and prepare to parse the RHS of the new, higher priority
6288 expression. Since precedence levels on the stack are
6289 monotonically increasing, we do not have to care about
6292 sp->tree_type = tree_type;
6294 sp->lhs_type = lhs_type;
6297 lhs_type = rhs_type;
6299 new_prec = lookahead_prec;
6303 lookahead_prec = new_prec;
6304 /* If the stack is not empty, we have parsed into LHS the right side
6305 (`4' in the example above) of an expression we had suspended.
6306 We can use the information on the stack to recover the LHS (`3')
6307 from the stack together with the tree code (`MULT_EXPR'), and
6308 the precedence of the higher level subexpression
6309 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6310 which will be used to actually build the additive expression. */
6313 tree_type = sp->tree_type;
6315 rhs_type = lhs_type;
6317 lhs_type = sp->lhs_type;
6320 overloaded_p = false;
6321 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6322 ERROR_MARK for everything that is not a binary expression.
6323 This makes warn_about_parentheses miss some warnings that
6324 involve unary operators. For unary expressions we should
6325 pass the correct tree_code unless the unary expression was
6326 surrounded by parentheses.
6328 if (no_toplevel_fold_p
6329 && lookahead_prec <= prec
6331 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6332 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6334 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6335 &overloaded_p, tf_warning_or_error);
6336 lhs_type = tree_type;
6338 /* If the binary operator required the use of an overloaded operator,
6339 then this expression cannot be an integral constant-expression.
6340 An overloaded operator can be used even if both operands are
6341 otherwise permissible in an integral constant-expression if at
6342 least one of the operands is of enumeration type. */
6345 && (cp_parser_non_integral_constant_expression
6346 (parser, "calls to overloaded operators")))
6347 return error_mark_node;
6354 /* Parse the `? expression : assignment-expression' part of a
6355 conditional-expression. The LOGICAL_OR_EXPR is the
6356 logical-or-expression that started the conditional-expression.
6357 Returns a representation of the entire conditional-expression.
6359 This routine is used by cp_parser_assignment_expression.
6361 ? expression : assignment-expression
6365 ? : assignment-expression */
6368 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6371 tree assignment_expr;
6373 /* Consume the `?' token. */
6374 cp_lexer_consume_token (parser->lexer);
6375 if (cp_parser_allow_gnu_extensions_p (parser)
6376 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6377 /* Implicit true clause. */
6380 /* Parse the expression. */
6381 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6383 /* The next token should be a `:'. */
6384 cp_parser_require (parser, CPP_COLON, "%<:%>");
6385 /* Parse the assignment-expression. */
6386 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6388 /* Build the conditional-expression. */
6389 return build_x_conditional_expr (logical_or_expr,
6392 tf_warning_or_error);
6395 /* Parse an assignment-expression.
6397 assignment-expression:
6398 conditional-expression
6399 logical-or-expression assignment-operator assignment_expression
6402 CAST_P is true if this expression is the target of a cast.
6404 Returns a representation for the expression. */
6407 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6412 /* If the next token is the `throw' keyword, then we're looking at
6413 a throw-expression. */
6414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6415 expr = cp_parser_throw_expression (parser);
6416 /* Otherwise, it must be that we are looking at a
6417 logical-or-expression. */
6420 /* Parse the binary expressions (logical-or-expression). */
6421 expr = cp_parser_binary_expression (parser, cast_p, false,
6422 PREC_NOT_OPERATOR, pidk);
6423 /* If the next token is a `?' then we're actually looking at a
6424 conditional-expression. */
6425 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6426 return cp_parser_question_colon_clause (parser, expr);
6429 enum tree_code assignment_operator;
6431 /* If it's an assignment-operator, we're using the second
6434 = cp_parser_assignment_operator_opt (parser);
6435 if (assignment_operator != ERROR_MARK)
6437 bool non_constant_p;
6439 /* Parse the right-hand side of the assignment. */
6440 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6442 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6443 maybe_warn_cpp0x ("extended initializer lists");
6445 /* An assignment may not appear in a
6446 constant-expression. */
6447 if (cp_parser_non_integral_constant_expression (parser,
6449 return error_mark_node;
6450 /* Build the assignment expression. */
6451 expr = build_x_modify_expr (expr,
6452 assignment_operator,
6454 tf_warning_or_error);
6462 /* Parse an (optional) assignment-operator.
6464 assignment-operator: one of
6465 = *= /= %= += -= >>= <<= &= ^= |=
6469 assignment-operator: one of
6472 If the next token is an assignment operator, the corresponding tree
6473 code is returned, and the token is consumed. For example, for
6474 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6475 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6476 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6477 operator, ERROR_MARK is returned. */
6479 static enum tree_code
6480 cp_parser_assignment_operator_opt (cp_parser* parser)
6485 /* Peek at the next token. */
6486 token = cp_lexer_peek_token (parser->lexer);
6488 switch (token->type)
6499 op = TRUNC_DIV_EXPR;
6503 op = TRUNC_MOD_EXPR;
6535 /* Nothing else is an assignment operator. */
6539 /* If it was an assignment operator, consume it. */
6540 if (op != ERROR_MARK)
6541 cp_lexer_consume_token (parser->lexer);
6546 /* Parse an expression.
6549 assignment-expression
6550 expression , assignment-expression
6552 CAST_P is true if this expression is the target of a cast.
6554 Returns a representation of the expression. */
6557 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6559 tree expression = NULL_TREE;
6563 tree assignment_expression;
6565 /* Parse the next assignment-expression. */
6566 assignment_expression
6567 = cp_parser_assignment_expression (parser, cast_p, pidk);
6568 /* If this is the first assignment-expression, we can just
6571 expression = assignment_expression;
6573 expression = build_x_compound_expr (expression,
6574 assignment_expression,
6575 tf_warning_or_error);
6576 /* If the next token is not a comma, then we are done with the
6578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6580 /* Consume the `,'. */
6581 cp_lexer_consume_token (parser->lexer);
6582 /* A comma operator cannot appear in a constant-expression. */
6583 if (cp_parser_non_integral_constant_expression (parser,
6584 "a comma operator"))
6585 expression = error_mark_node;
6591 /* Parse a constant-expression.
6593 constant-expression:
6594 conditional-expression
6596 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6597 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6598 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6599 is false, NON_CONSTANT_P should be NULL. */
6602 cp_parser_constant_expression (cp_parser* parser,
6603 bool allow_non_constant_p,
6604 bool *non_constant_p)
6606 bool saved_integral_constant_expression_p;
6607 bool saved_allow_non_integral_constant_expression_p;
6608 bool saved_non_integral_constant_expression_p;
6611 /* It might seem that we could simply parse the
6612 conditional-expression, and then check to see if it were
6613 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6614 one that the compiler can figure out is constant, possibly after
6615 doing some simplifications or optimizations. The standard has a
6616 precise definition of constant-expression, and we must honor
6617 that, even though it is somewhat more restrictive.
6623 is not a legal declaration, because `(2, 3)' is not a
6624 constant-expression. The `,' operator is forbidden in a
6625 constant-expression. However, GCC's constant-folding machinery
6626 will fold this operation to an INTEGER_CST for `3'. */
6628 /* Save the old settings. */
6629 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6630 saved_allow_non_integral_constant_expression_p
6631 = parser->allow_non_integral_constant_expression_p;
6632 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6633 /* We are now parsing a constant-expression. */
6634 parser->integral_constant_expression_p = true;
6635 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6636 parser->non_integral_constant_expression_p = false;
6637 /* Although the grammar says "conditional-expression", we parse an
6638 "assignment-expression", which also permits "throw-expression"
6639 and the use of assignment operators. In the case that
6640 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6641 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6642 actually essential that we look for an assignment-expression.
6643 For example, cp_parser_initializer_clauses uses this function to
6644 determine whether a particular assignment-expression is in fact
6646 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6647 /* Restore the old settings. */
6648 parser->integral_constant_expression_p
6649 = saved_integral_constant_expression_p;
6650 parser->allow_non_integral_constant_expression_p
6651 = saved_allow_non_integral_constant_expression_p;
6652 if (allow_non_constant_p)
6653 *non_constant_p = parser->non_integral_constant_expression_p;
6654 else if (parser->non_integral_constant_expression_p)
6655 expression = error_mark_node;
6656 parser->non_integral_constant_expression_p
6657 = saved_non_integral_constant_expression_p;
6662 /* Parse __builtin_offsetof.
6664 offsetof-expression:
6665 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6667 offsetof-member-designator:
6669 | offsetof-member-designator "." id-expression
6670 | offsetof-member-designator "[" expression "]"
6671 | offsetof-member-designator "->" id-expression */
6674 cp_parser_builtin_offsetof (cp_parser *parser)
6676 int save_ice_p, save_non_ice_p;
6681 /* We're about to accept non-integral-constant things, but will
6682 definitely yield an integral constant expression. Save and
6683 restore these values around our local parsing. */
6684 save_ice_p = parser->integral_constant_expression_p;
6685 save_non_ice_p = parser->non_integral_constant_expression_p;
6687 /* Consume the "__builtin_offsetof" token. */
6688 cp_lexer_consume_token (parser->lexer);
6689 /* Consume the opening `('. */
6690 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6691 /* Parse the type-id. */
6692 type = cp_parser_type_id (parser);
6693 /* Look for the `,'. */
6694 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6695 token = cp_lexer_peek_token (parser->lexer);
6697 /* Build the (type *)null that begins the traditional offsetof macro. */
6698 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6699 tf_warning_or_error);
6701 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6702 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6703 true, &dummy, token->location);
6706 token = cp_lexer_peek_token (parser->lexer);
6707 switch (token->type)
6709 case CPP_OPEN_SQUARE:
6710 /* offsetof-member-designator "[" expression "]" */
6711 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6715 /* offsetof-member-designator "->" identifier */
6716 expr = grok_array_decl (expr, integer_zero_node);
6720 /* offsetof-member-designator "." identifier */
6721 cp_lexer_consume_token (parser->lexer);
6722 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6727 case CPP_CLOSE_PAREN:
6728 /* Consume the ")" token. */
6729 cp_lexer_consume_token (parser->lexer);
6733 /* Error. We know the following require will fail, but
6734 that gives the proper error message. */
6735 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6736 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6737 expr = error_mark_node;
6743 /* If we're processing a template, we can't finish the semantics yet.
6744 Otherwise we can fold the entire expression now. */
6745 if (processing_template_decl)
6746 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6748 expr = finish_offsetof (expr);
6751 parser->integral_constant_expression_p = save_ice_p;
6752 parser->non_integral_constant_expression_p = save_non_ice_p;
6757 /* Parse a trait expression. */
6760 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6763 tree type1, type2 = NULL_TREE;
6764 bool binary = false;
6765 cp_decl_specifier_seq decl_specs;
6769 case RID_HAS_NOTHROW_ASSIGN:
6770 kind = CPTK_HAS_NOTHROW_ASSIGN;
6772 case RID_HAS_NOTHROW_CONSTRUCTOR:
6773 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6775 case RID_HAS_NOTHROW_COPY:
6776 kind = CPTK_HAS_NOTHROW_COPY;
6778 case RID_HAS_TRIVIAL_ASSIGN:
6779 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6781 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6782 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6784 case RID_HAS_TRIVIAL_COPY:
6785 kind = CPTK_HAS_TRIVIAL_COPY;
6787 case RID_HAS_TRIVIAL_DESTRUCTOR:
6788 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6790 case RID_HAS_VIRTUAL_DESTRUCTOR:
6791 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6793 case RID_IS_ABSTRACT:
6794 kind = CPTK_IS_ABSTRACT;
6796 case RID_IS_BASE_OF:
6797 kind = CPTK_IS_BASE_OF;
6801 kind = CPTK_IS_CLASS;
6803 case RID_IS_CONVERTIBLE_TO:
6804 kind = CPTK_IS_CONVERTIBLE_TO;
6808 kind = CPTK_IS_EMPTY;
6811 kind = CPTK_IS_ENUM;
6816 case RID_IS_POLYMORPHIC:
6817 kind = CPTK_IS_POLYMORPHIC;
6820 kind = CPTK_IS_UNION;
6826 /* Consume the token. */
6827 cp_lexer_consume_token (parser->lexer);
6829 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6831 type1 = cp_parser_type_id (parser);
6833 if (type1 == error_mark_node)
6834 return error_mark_node;
6836 /* Build a trivial decl-specifier-seq. */
6837 clear_decl_specs (&decl_specs);
6838 decl_specs.type = type1;
6840 /* Call grokdeclarator to figure out what type this is. */
6841 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6842 /*initialized=*/0, /*attrlist=*/NULL);
6846 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6848 type2 = cp_parser_type_id (parser);
6850 if (type2 == error_mark_node)
6851 return error_mark_node;
6853 /* Build a trivial decl-specifier-seq. */
6854 clear_decl_specs (&decl_specs);
6855 decl_specs.type = type2;
6857 /* Call grokdeclarator to figure out what type this is. */
6858 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6859 /*initialized=*/0, /*attrlist=*/NULL);
6862 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6864 /* Complete the trait expression, which may mean either processing
6865 the trait expr now or saving it for template instantiation. */
6866 return finish_trait_expr (kind, type1, type2);
6869 /* Statements [gram.stmt.stmt] */
6871 /* Parse a statement.
6875 expression-statement
6880 declaration-statement
6883 IN_COMPOUND is true when the statement is nested inside a
6884 cp_parser_compound_statement; this matters for certain pragmas.
6886 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6887 is a (possibly labeled) if statement which is not enclosed in braces
6888 and has an else clause. This is used to implement -Wparentheses. */
6891 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6892 bool in_compound, bool *if_p)
6896 location_t statement_location;
6901 /* There is no statement yet. */
6902 statement = NULL_TREE;
6903 /* Peek at the next token. */
6904 token = cp_lexer_peek_token (parser->lexer);
6905 /* Remember the location of the first token in the statement. */
6906 statement_location = token->location;
6907 /* If this is a keyword, then that will often determine what kind of
6908 statement we have. */
6909 if (token->type == CPP_KEYWORD)
6911 enum rid keyword = token->keyword;
6917 /* Looks like a labeled-statement with a case label.
6918 Parse the label, and then use tail recursion to parse
6920 cp_parser_label_for_labeled_statement (parser);
6925 statement = cp_parser_selection_statement (parser, if_p);
6931 statement = cp_parser_iteration_statement (parser);
6938 statement = cp_parser_jump_statement (parser);
6941 /* Objective-C++ exception-handling constructs. */
6944 case RID_AT_FINALLY:
6945 case RID_AT_SYNCHRONIZED:
6947 statement = cp_parser_objc_statement (parser);
6951 statement = cp_parser_try_block (parser);
6955 /* This must be a namespace alias definition. */
6956 cp_parser_declaration_statement (parser);
6960 /* It might be a keyword like `int' that can start a
6961 declaration-statement. */
6965 else if (token->type == CPP_NAME)
6967 /* If the next token is a `:', then we are looking at a
6968 labeled-statement. */
6969 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6970 if (token->type == CPP_COLON)
6972 /* Looks like a labeled-statement with an ordinary label.
6973 Parse the label, and then use tail recursion to parse
6975 cp_parser_label_for_labeled_statement (parser);
6979 /* Anything that starts with a `{' must be a compound-statement. */
6980 else if (token->type == CPP_OPEN_BRACE)
6981 statement = cp_parser_compound_statement (parser, NULL, false);
6982 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6983 a statement all its own. */
6984 else if (token->type == CPP_PRAGMA)
6986 /* Only certain OpenMP pragmas are attached to statements, and thus
6987 are considered statements themselves. All others are not. In
6988 the context of a compound, accept the pragma as a "statement" and
6989 return so that we can check for a close brace. Otherwise we
6990 require a real statement and must go back and read one. */
6992 cp_parser_pragma (parser, pragma_compound);
6993 else if (!cp_parser_pragma (parser, pragma_stmt))
6997 else if (token->type == CPP_EOF)
6999 cp_parser_error (parser, "expected statement");
7003 /* Everything else must be a declaration-statement or an
7004 expression-statement. Try for the declaration-statement
7005 first, unless we are looking at a `;', in which case we know that
7006 we have an expression-statement. */
7009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7011 cp_parser_parse_tentatively (parser);
7012 /* Try to parse the declaration-statement. */
7013 cp_parser_declaration_statement (parser);
7014 /* If that worked, we're done. */
7015 if (cp_parser_parse_definitely (parser))
7018 /* Look for an expression-statement instead. */
7019 statement = cp_parser_expression_statement (parser, in_statement_expr);
7022 /* Set the line number for the statement. */
7023 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7024 SET_EXPR_LOCATION (statement, statement_location);
7027 /* Parse the label for a labeled-statement, i.e.
7030 case constant-expression :
7034 case constant-expression ... constant-expression : statement
7036 When a label is parsed without errors, the label is added to the
7037 parse tree by the finish_* functions, so this function doesn't
7038 have to return the label. */
7041 cp_parser_label_for_labeled_statement (cp_parser* parser)
7045 /* The next token should be an identifier. */
7046 token = cp_lexer_peek_token (parser->lexer);
7047 if (token->type != CPP_NAME
7048 && token->type != CPP_KEYWORD)
7050 cp_parser_error (parser, "expected labeled-statement");
7054 switch (token->keyword)
7061 /* Consume the `case' token. */
7062 cp_lexer_consume_token (parser->lexer);
7063 /* Parse the constant-expression. */
7064 expr = cp_parser_constant_expression (parser,
7065 /*allow_non_constant_p=*/false,
7068 ellipsis = cp_lexer_peek_token (parser->lexer);
7069 if (ellipsis->type == CPP_ELLIPSIS)
7071 /* Consume the `...' token. */
7072 cp_lexer_consume_token (parser->lexer);
7074 cp_parser_constant_expression (parser,
7075 /*allow_non_constant_p=*/false,
7077 /* We don't need to emit warnings here, as the common code
7078 will do this for us. */
7081 expr_hi = NULL_TREE;
7083 if (parser->in_switch_statement_p)
7084 finish_case_label (expr, expr_hi);
7086 error ("%Hcase label %qE not within a switch statement",
7087 &token->location, expr);
7092 /* Consume the `default' token. */
7093 cp_lexer_consume_token (parser->lexer);
7095 if (parser->in_switch_statement_p)
7096 finish_case_label (NULL_TREE, NULL_TREE);
7098 error ("%Hcase label not within a switch statement", &token->location);
7102 /* Anything else must be an ordinary label. */
7103 finish_label_stmt (cp_parser_identifier (parser));
7107 /* Require the `:' token. */
7108 cp_parser_require (parser, CPP_COLON, "%<:%>");
7111 /* Parse an expression-statement.
7113 expression-statement:
7116 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7117 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7118 indicates whether this expression-statement is part of an
7119 expression statement. */
7122 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7124 tree statement = NULL_TREE;
7126 /* If the next token is a ';', then there is no expression
7128 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7129 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7131 /* Consume the final `;'. */
7132 cp_parser_consume_semicolon_at_end_of_statement (parser);
7134 if (in_statement_expr
7135 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7136 /* This is the final expression statement of a statement
7138 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7140 statement = finish_expr_stmt (statement);
7147 /* Parse a compound-statement.
7150 { statement-seq [opt] }
7155 { label-declaration-seq [opt] statement-seq [opt] }
7157 label-declaration-seq:
7159 label-declaration-seq label-declaration
7161 Returns a tree representing the statement. */
7164 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7169 /* Consume the `{'. */
7170 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7171 return error_mark_node;
7172 /* Begin the compound-statement. */
7173 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7174 /* If the next keyword is `__label__' we have a label declaration. */
7175 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7176 cp_parser_label_declaration (parser);
7177 /* Parse an (optional) statement-seq. */
7178 cp_parser_statement_seq_opt (parser, in_statement_expr);
7179 /* Finish the compound-statement. */
7180 finish_compound_stmt (compound_stmt);
7181 /* Consume the `}'. */
7182 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7184 return compound_stmt;
7187 /* Parse an (optional) statement-seq.
7191 statement-seq [opt] statement */
7194 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7196 /* Scan statements until there aren't any more. */
7199 cp_token *token = cp_lexer_peek_token (parser->lexer);
7201 /* If we're looking at a `}', then we've run out of statements. */
7202 if (token->type == CPP_CLOSE_BRACE
7203 || token->type == CPP_EOF
7204 || token->type == CPP_PRAGMA_EOL)
7207 /* If we are in a compound statement and find 'else' then
7208 something went wrong. */
7209 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7211 if (parser->in_statement & IN_IF_STMT)
7215 token = cp_lexer_consume_token (parser->lexer);
7216 error ("%H%<else%> without a previous %<if%>", &token->location);
7220 /* Parse the statement. */
7221 cp_parser_statement (parser, in_statement_expr, true, NULL);
7225 /* Parse a selection-statement.
7227 selection-statement:
7228 if ( condition ) statement
7229 if ( condition ) statement else statement
7230 switch ( condition ) statement
7232 Returns the new IF_STMT or SWITCH_STMT.
7234 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7235 is a (possibly labeled) if statement which is not enclosed in
7236 braces and has an else clause. This is used to implement
7240 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7248 /* Peek at the next token. */
7249 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7251 /* See what kind of keyword it is. */
7252 keyword = token->keyword;
7261 /* Look for the `('. */
7262 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7264 cp_parser_skip_to_end_of_statement (parser);
7265 return error_mark_node;
7268 /* Begin the selection-statement. */
7269 if (keyword == RID_IF)
7270 statement = begin_if_stmt ();
7272 statement = begin_switch_stmt ();
7274 /* Parse the condition. */
7275 condition = cp_parser_condition (parser);
7276 /* Look for the `)'. */
7277 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7278 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7279 /*consume_paren=*/true);
7281 if (keyword == RID_IF)
7284 unsigned char in_statement;
7286 /* Add the condition. */
7287 finish_if_stmt_cond (condition, statement);
7289 /* Parse the then-clause. */
7290 in_statement = parser->in_statement;
7291 parser->in_statement |= IN_IF_STMT;
7292 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7294 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7295 add_stmt (build_empty_stmt ());
7296 cp_lexer_consume_token (parser->lexer);
7297 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7298 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7299 "empty body in an %<if%> statement");
7303 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7304 parser->in_statement = in_statement;
7306 finish_then_clause (statement);
7308 /* If the next token is `else', parse the else-clause. */
7309 if (cp_lexer_next_token_is_keyword (parser->lexer,
7312 /* Consume the `else' keyword. */
7313 cp_lexer_consume_token (parser->lexer);
7314 begin_else_clause (statement);
7315 /* Parse the else-clause. */
7316 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7318 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7319 OPT_Wempty_body, "suggest braces around "
7320 "empty body in an %<else%> statement");
7321 add_stmt (build_empty_stmt ());
7322 cp_lexer_consume_token (parser->lexer);
7325 cp_parser_implicitly_scoped_statement (parser, NULL);
7327 finish_else_clause (statement);
7329 /* If we are currently parsing a then-clause, then
7330 IF_P will not be NULL. We set it to true to
7331 indicate that this if statement has an else clause.
7332 This may trigger the Wparentheses warning below
7333 when we get back up to the parent if statement. */
7339 /* This if statement does not have an else clause. If
7340 NESTED_IF is true, then the then-clause is an if
7341 statement which does have an else clause. We warn
7342 about the potential ambiguity. */
7344 warning (OPT_Wparentheses,
7345 ("%Hsuggest explicit braces "
7346 "to avoid ambiguous %<else%>"),
7347 EXPR_LOCUS (statement));
7350 /* Now we're all done with the if-statement. */
7351 finish_if_stmt (statement);
7355 bool in_switch_statement_p;
7356 unsigned char in_statement;
7358 /* Add the condition. */
7359 finish_switch_cond (condition, statement);
7361 /* Parse the body of the switch-statement. */
7362 in_switch_statement_p = parser->in_switch_statement_p;
7363 in_statement = parser->in_statement;
7364 parser->in_switch_statement_p = true;
7365 parser->in_statement |= IN_SWITCH_STMT;
7366 cp_parser_implicitly_scoped_statement (parser, NULL);
7367 parser->in_switch_statement_p = in_switch_statement_p;
7368 parser->in_statement = in_statement;
7370 /* Now we're all done with the switch-statement. */
7371 finish_switch_stmt (statement);
7379 cp_parser_error (parser, "expected selection-statement");
7380 return error_mark_node;
7384 /* Parse a condition.
7388 type-specifier-seq declarator = initializer-clause
7389 type-specifier-seq declarator braced-init-list
7394 type-specifier-seq declarator asm-specification [opt]
7395 attributes [opt] = assignment-expression
7397 Returns the expression that should be tested. */
7400 cp_parser_condition (cp_parser* parser)
7402 cp_decl_specifier_seq type_specifiers;
7403 const char *saved_message;
7405 /* Try the declaration first. */
7406 cp_parser_parse_tentatively (parser);
7407 /* New types are not allowed in the type-specifier-seq for a
7409 saved_message = parser->type_definition_forbidden_message;
7410 parser->type_definition_forbidden_message
7411 = "types may not be defined in conditions";
7412 /* Parse the type-specifier-seq. */
7413 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7415 /* Restore the saved message. */
7416 parser->type_definition_forbidden_message = saved_message;
7417 /* If all is well, we might be looking at a declaration. */
7418 if (!cp_parser_error_occurred (parser))
7421 tree asm_specification;
7423 cp_declarator *declarator;
7424 tree initializer = NULL_TREE;
7426 /* Parse the declarator. */
7427 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7428 /*ctor_dtor_or_conv_p=*/NULL,
7429 /*parenthesized_p=*/NULL,
7430 /*member_p=*/false);
7431 /* Parse the attributes. */
7432 attributes = cp_parser_attributes_opt (parser);
7433 /* Parse the asm-specification. */
7434 asm_specification = cp_parser_asm_specification_opt (parser);
7435 /* If the next token is not an `=' or '{', then we might still be
7436 looking at an expression. For example:
7440 looks like a decl-specifier-seq and a declarator -- but then
7441 there is no `=', so this is an expression. */
7442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7443 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7444 cp_parser_simulate_error (parser);
7446 /* If we did see an `=' or '{', then we are looking at a declaration
7448 if (cp_parser_parse_definitely (parser))
7451 bool non_constant_p;
7452 bool flags = LOOKUP_ONLYCONVERTING;
7454 /* Create the declaration. */
7455 decl = start_decl (declarator, &type_specifiers,
7456 /*initialized_p=*/true,
7457 attributes, /*prefix_attributes=*/NULL_TREE,
7460 /* Parse the initializer. */
7461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7463 initializer = cp_parser_braced_list (parser, &non_constant_p);
7464 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7469 /* Consume the `='. */
7470 cp_parser_require (parser, CPP_EQ, "%<=%>");
7471 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7473 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7474 maybe_warn_cpp0x ("extended initializer lists");
7476 if (!non_constant_p)
7477 initializer = fold_non_dependent_expr (initializer);
7479 /* Process the initializer. */
7480 cp_finish_decl (decl,
7481 initializer, !non_constant_p,
7486 pop_scope (pushed_scope);
7488 return convert_from_reference (decl);
7491 /* If we didn't even get past the declarator successfully, we are
7492 definitely not looking at a declaration. */
7494 cp_parser_abort_tentative_parse (parser);
7496 /* Otherwise, we are looking at an expression. */
7497 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7500 /* Parse an iteration-statement.
7502 iteration-statement:
7503 while ( condition ) statement
7504 do statement while ( expression ) ;
7505 for ( for-init-statement condition [opt] ; expression [opt] )
7508 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7511 cp_parser_iteration_statement (cp_parser* parser)
7516 unsigned char in_statement;
7518 /* Peek at the next token. */
7519 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7521 return error_mark_node;
7523 /* Remember whether or not we are already within an iteration
7525 in_statement = parser->in_statement;
7527 /* See what kind of keyword it is. */
7528 keyword = token->keyword;
7535 /* Begin the while-statement. */
7536 statement = begin_while_stmt ();
7537 /* Look for the `('. */
7538 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7539 /* Parse the condition. */
7540 condition = cp_parser_condition (parser);
7541 finish_while_stmt_cond (condition, statement);
7542 /* Look for the `)'. */
7543 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7544 /* Parse the dependent statement. */
7545 parser->in_statement = IN_ITERATION_STMT;
7546 cp_parser_already_scoped_statement (parser);
7547 parser->in_statement = in_statement;
7548 /* We're done with the while-statement. */
7549 finish_while_stmt (statement);
7557 /* Begin the do-statement. */
7558 statement = begin_do_stmt ();
7559 /* Parse the body of the do-statement. */
7560 parser->in_statement = IN_ITERATION_STMT;
7561 cp_parser_implicitly_scoped_statement (parser, NULL);
7562 parser->in_statement = in_statement;
7563 finish_do_body (statement);
7564 /* Look for the `while' keyword. */
7565 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7566 /* Look for the `('. */
7567 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7568 /* Parse the expression. */
7569 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7570 /* We're done with the do-statement. */
7571 finish_do_stmt (expression, statement);
7572 /* Look for the `)'. */
7573 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7574 /* Look for the `;'. */
7575 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7581 tree condition = NULL_TREE;
7582 tree expression = NULL_TREE;
7584 /* Begin the for-statement. */
7585 statement = begin_for_stmt ();
7586 /* Look for the `('. */
7587 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7588 /* Parse the initialization. */
7589 cp_parser_for_init_statement (parser);
7590 finish_for_init_stmt (statement);
7592 /* If there's a condition, process it. */
7593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7594 condition = cp_parser_condition (parser);
7595 finish_for_cond (condition, statement);
7596 /* Look for the `;'. */
7597 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7599 /* If there's an expression, process it. */
7600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7601 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7602 finish_for_expr (expression, statement);
7603 /* Look for the `)'. */
7604 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7606 /* Parse the body of the for-statement. */
7607 parser->in_statement = IN_ITERATION_STMT;
7608 cp_parser_already_scoped_statement (parser);
7609 parser->in_statement = in_statement;
7611 /* We're done with the for-statement. */
7612 finish_for_stmt (statement);
7617 cp_parser_error (parser, "expected iteration-statement");
7618 statement = error_mark_node;
7625 /* Parse a for-init-statement.
7628 expression-statement
7629 simple-declaration */
7632 cp_parser_for_init_statement (cp_parser* parser)
7634 /* If the next token is a `;', then we have an empty
7635 expression-statement. Grammatically, this is also a
7636 simple-declaration, but an invalid one, because it does not
7637 declare anything. Therefore, if we did not handle this case
7638 specially, we would issue an error message about an invalid
7640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7642 /* We're going to speculatively look for a declaration, falling back
7643 to an expression, if necessary. */
7644 cp_parser_parse_tentatively (parser);
7645 /* Parse the declaration. */
7646 cp_parser_simple_declaration (parser,
7647 /*function_definition_allowed_p=*/false);
7648 /* If the tentative parse failed, then we shall need to look for an
7649 expression-statement. */
7650 if (cp_parser_parse_definitely (parser))
7654 cp_parser_expression_statement (parser, false);
7657 /* Parse a jump-statement.
7662 return expression [opt] ;
7663 return braced-init-list ;
7671 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7674 cp_parser_jump_statement (cp_parser* parser)
7676 tree statement = error_mark_node;
7679 unsigned char in_statement;
7681 /* Peek at the next token. */
7682 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7684 return error_mark_node;
7686 /* See what kind of keyword it is. */
7687 keyword = token->keyword;
7691 in_statement = parser->in_statement & ~IN_IF_STMT;
7692 switch (in_statement)
7695 error ("%Hbreak statement not within loop or switch", &token->location);
7698 gcc_assert ((in_statement & IN_SWITCH_STMT)
7699 || in_statement == IN_ITERATION_STMT);
7700 statement = finish_break_stmt ();
7703 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7706 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7709 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7713 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7716 error ("%Hcontinue statement not within a loop", &token->location);
7718 case IN_ITERATION_STMT:
7720 statement = finish_continue_stmt ();
7723 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7728 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7734 bool expr_non_constant_p;
7736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7738 maybe_warn_cpp0x ("extended initializer lists");
7739 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7741 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7742 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7744 /* If the next token is a `;', then there is no
7747 /* Build the return-statement. */
7748 statement = finish_return_stmt (expr);
7749 /* Look for the final `;'. */
7750 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7755 /* Create the goto-statement. */
7756 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7758 /* Issue a warning about this use of a GNU extension. */
7759 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7760 /* Consume the '*' token. */
7761 cp_lexer_consume_token (parser->lexer);
7762 /* Parse the dependent expression. */
7763 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7766 finish_goto_stmt (cp_parser_identifier (parser));
7767 /* Look for the final `;'. */
7768 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7772 cp_parser_error (parser, "expected jump-statement");
7779 /* Parse a declaration-statement.
7781 declaration-statement:
7782 block-declaration */
7785 cp_parser_declaration_statement (cp_parser* parser)
7789 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7790 p = obstack_alloc (&declarator_obstack, 0);
7792 /* Parse the block-declaration. */
7793 cp_parser_block_declaration (parser, /*statement_p=*/true);
7795 /* Free any declarators allocated. */
7796 obstack_free (&declarator_obstack, p);
7798 /* Finish off the statement. */
7802 /* Some dependent statements (like `if (cond) statement'), are
7803 implicitly in their own scope. In other words, if the statement is
7804 a single statement (as opposed to a compound-statement), it is
7805 none-the-less treated as if it were enclosed in braces. Any
7806 declarations appearing in the dependent statement are out of scope
7807 after control passes that point. This function parses a statement,
7808 but ensures that is in its own scope, even if it is not a
7811 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7812 is a (possibly labeled) if statement which is not enclosed in
7813 braces and has an else clause. This is used to implement
7816 Returns the new statement. */
7819 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7826 /* Mark if () ; with a special NOP_EXPR. */
7827 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7829 cp_lexer_consume_token (parser->lexer);
7830 statement = add_stmt (build_empty_stmt ());
7832 /* if a compound is opened, we simply parse the statement directly. */
7833 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7834 statement = cp_parser_compound_statement (parser, NULL, false);
7835 /* If the token is not a `{', then we must take special action. */
7838 /* Create a compound-statement. */
7839 statement = begin_compound_stmt (0);
7840 /* Parse the dependent-statement. */
7841 cp_parser_statement (parser, NULL_TREE, false, if_p);
7842 /* Finish the dummy compound-statement. */
7843 finish_compound_stmt (statement);
7846 /* Return the statement. */
7850 /* For some dependent statements (like `while (cond) statement'), we
7851 have already created a scope. Therefore, even if the dependent
7852 statement is a compound-statement, we do not want to create another
7856 cp_parser_already_scoped_statement (cp_parser* parser)
7858 /* If the token is a `{', then we must take special action. */
7859 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7860 cp_parser_statement (parser, NULL_TREE, false, NULL);
7863 /* Avoid calling cp_parser_compound_statement, so that we
7864 don't create a new scope. Do everything else by hand. */
7865 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7866 /* If the next keyword is `__label__' we have a label declaration. */
7867 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7868 cp_parser_label_declaration (parser);
7869 /* Parse an (optional) statement-seq. */
7870 cp_parser_statement_seq_opt (parser, NULL_TREE);
7871 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7875 /* Declarations [gram.dcl.dcl] */
7877 /* Parse an optional declaration-sequence.
7881 declaration-seq declaration */
7884 cp_parser_declaration_seq_opt (cp_parser* parser)
7890 token = cp_lexer_peek_token (parser->lexer);
7892 if (token->type == CPP_CLOSE_BRACE
7893 || token->type == CPP_EOF
7894 || token->type == CPP_PRAGMA_EOL)
7897 if (token->type == CPP_SEMICOLON)
7899 /* A declaration consisting of a single semicolon is
7900 invalid. Allow it unless we're being pedantic. */
7901 cp_lexer_consume_token (parser->lexer);
7902 if (!in_system_header)
7903 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7907 /* If we're entering or exiting a region that's implicitly
7908 extern "C", modify the lang context appropriately. */
7909 if (!parser->implicit_extern_c && token->implicit_extern_c)
7911 push_lang_context (lang_name_c);
7912 parser->implicit_extern_c = true;
7914 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7916 pop_lang_context ();
7917 parser->implicit_extern_c = false;
7920 if (token->type == CPP_PRAGMA)
7922 /* A top-level declaration can consist solely of a #pragma.
7923 A nested declaration cannot, so this is done here and not
7924 in cp_parser_declaration. (A #pragma at block scope is
7925 handled in cp_parser_statement.) */
7926 cp_parser_pragma (parser, pragma_external);
7930 /* Parse the declaration itself. */
7931 cp_parser_declaration (parser);
7935 /* Parse a declaration.
7940 template-declaration
7941 explicit-instantiation
7942 explicit-specialization
7943 linkage-specification
7944 namespace-definition
7949 __extension__ declaration */
7952 cp_parser_declaration (cp_parser* parser)
7959 /* Check for the `__extension__' keyword. */
7960 if (cp_parser_extension_opt (parser, &saved_pedantic))
7962 /* Parse the qualified declaration. */
7963 cp_parser_declaration (parser);
7964 /* Restore the PEDANTIC flag. */
7965 pedantic = saved_pedantic;
7970 /* Try to figure out what kind of declaration is present. */
7971 token1 = *cp_lexer_peek_token (parser->lexer);
7973 if (token1.type != CPP_EOF)
7974 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7977 token2.type = CPP_EOF;
7978 token2.keyword = RID_MAX;
7981 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7982 p = obstack_alloc (&declarator_obstack, 0);
7984 /* If the next token is `extern' and the following token is a string
7985 literal, then we have a linkage specification. */
7986 if (token1.keyword == RID_EXTERN
7987 && cp_parser_is_string_literal (&token2))
7988 cp_parser_linkage_specification (parser);
7989 /* If the next token is `template', then we have either a template
7990 declaration, an explicit instantiation, or an explicit
7992 else if (token1.keyword == RID_TEMPLATE)
7994 /* `template <>' indicates a template specialization. */
7995 if (token2.type == CPP_LESS
7996 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7997 cp_parser_explicit_specialization (parser);
7998 /* `template <' indicates a template declaration. */
7999 else if (token2.type == CPP_LESS)
8000 cp_parser_template_declaration (parser, /*member_p=*/false);
8001 /* Anything else must be an explicit instantiation. */
8003 cp_parser_explicit_instantiation (parser);
8005 /* If the next token is `export', then we have a template
8007 else if (token1.keyword == RID_EXPORT)
8008 cp_parser_template_declaration (parser, /*member_p=*/false);
8009 /* If the next token is `extern', 'static' or 'inline' and the one
8010 after that is `template', we have a GNU extended explicit
8011 instantiation directive. */
8012 else if (cp_parser_allow_gnu_extensions_p (parser)
8013 && (token1.keyword == RID_EXTERN
8014 || token1.keyword == RID_STATIC
8015 || token1.keyword == RID_INLINE)
8016 && token2.keyword == RID_TEMPLATE)
8017 cp_parser_explicit_instantiation (parser);
8018 /* If the next token is `namespace', check for a named or unnamed
8019 namespace definition. */
8020 else if (token1.keyword == RID_NAMESPACE
8021 && (/* A named namespace definition. */
8022 (token2.type == CPP_NAME
8023 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8025 /* An unnamed namespace definition. */
8026 || token2.type == CPP_OPEN_BRACE
8027 || token2.keyword == RID_ATTRIBUTE))
8028 cp_parser_namespace_definition (parser);
8029 /* An inline (associated) namespace definition. */
8030 else if (token1.keyword == RID_INLINE
8031 && token2.keyword == RID_NAMESPACE)
8032 cp_parser_namespace_definition (parser);
8033 /* Objective-C++ declaration/definition. */
8034 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8035 cp_parser_objc_declaration (parser);
8036 /* We must have either a block declaration or a function
8039 /* Try to parse a block-declaration, or a function-definition. */
8040 cp_parser_block_declaration (parser, /*statement_p=*/false);
8042 /* Free any declarators allocated. */
8043 obstack_free (&declarator_obstack, p);
8046 /* Parse a block-declaration.
8051 namespace-alias-definition
8058 __extension__ block-declaration
8063 static_assert-declaration
8065 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8066 part of a declaration-statement. */
8069 cp_parser_block_declaration (cp_parser *parser,
8075 /* Check for the `__extension__' keyword. */
8076 if (cp_parser_extension_opt (parser, &saved_pedantic))
8078 /* Parse the qualified declaration. */
8079 cp_parser_block_declaration (parser, statement_p);
8080 /* Restore the PEDANTIC flag. */
8081 pedantic = saved_pedantic;
8086 /* Peek at the next token to figure out which kind of declaration is
8088 token1 = cp_lexer_peek_token (parser->lexer);
8090 /* If the next keyword is `asm', we have an asm-definition. */
8091 if (token1->keyword == RID_ASM)
8094 cp_parser_commit_to_tentative_parse (parser);
8095 cp_parser_asm_definition (parser);
8097 /* If the next keyword is `namespace', we have a
8098 namespace-alias-definition. */
8099 else if (token1->keyword == RID_NAMESPACE)
8100 cp_parser_namespace_alias_definition (parser);
8101 /* If the next keyword is `using', we have either a
8102 using-declaration or a using-directive. */
8103 else if (token1->keyword == RID_USING)
8108 cp_parser_commit_to_tentative_parse (parser);
8109 /* If the token after `using' is `namespace', then we have a
8111 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8112 if (token2->keyword == RID_NAMESPACE)
8113 cp_parser_using_directive (parser);
8114 /* Otherwise, it's a using-declaration. */
8116 cp_parser_using_declaration (parser,
8117 /*access_declaration_p=*/false);
8119 /* If the next keyword is `__label__' we have a misplaced label
8121 else if (token1->keyword == RID_LABEL)
8123 cp_lexer_consume_token (parser->lexer);
8124 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8125 cp_parser_skip_to_end_of_statement (parser);
8126 /* If the next token is now a `;', consume it. */
8127 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8128 cp_lexer_consume_token (parser->lexer);
8130 /* If the next token is `static_assert' we have a static assertion. */
8131 else if (token1->keyword == RID_STATIC_ASSERT)
8132 cp_parser_static_assert (parser, /*member_p=*/false);
8133 /* Anything else must be a simple-declaration. */
8135 cp_parser_simple_declaration (parser, !statement_p);
8138 /* Parse a simple-declaration.
8141 decl-specifier-seq [opt] init-declarator-list [opt] ;
8143 init-declarator-list:
8145 init-declarator-list , init-declarator
8147 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8148 function-definition as a simple-declaration. */
8151 cp_parser_simple_declaration (cp_parser* parser,
8152 bool function_definition_allowed_p)
8154 cp_decl_specifier_seq decl_specifiers;
8155 int declares_class_or_enum;
8156 bool saw_declarator;
8158 /* Defer access checks until we know what is being declared; the
8159 checks for names appearing in the decl-specifier-seq should be
8160 done as if we were in the scope of the thing being declared. */
8161 push_deferring_access_checks (dk_deferred);
8163 /* Parse the decl-specifier-seq. We have to keep track of whether
8164 or not the decl-specifier-seq declares a named class or
8165 enumeration type, since that is the only case in which the
8166 init-declarator-list is allowed to be empty.
8170 In a simple-declaration, the optional init-declarator-list can be
8171 omitted only when declaring a class or enumeration, that is when
8172 the decl-specifier-seq contains either a class-specifier, an
8173 elaborated-type-specifier, or an enum-specifier. */
8174 cp_parser_decl_specifier_seq (parser,
8175 CP_PARSER_FLAGS_OPTIONAL,
8177 &declares_class_or_enum);
8178 /* We no longer need to defer access checks. */
8179 stop_deferring_access_checks ();
8181 /* In a block scope, a valid declaration must always have a
8182 decl-specifier-seq. By not trying to parse declarators, we can
8183 resolve the declaration/expression ambiguity more quickly. */
8184 if (!function_definition_allowed_p
8185 && !decl_specifiers.any_specifiers_p)
8187 cp_parser_error (parser, "expected declaration");
8191 /* If the next two tokens are both identifiers, the code is
8192 erroneous. The usual cause of this situation is code like:
8196 where "T" should name a type -- but does not. */
8197 if (!decl_specifiers.type
8198 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8200 /* If parsing tentatively, we should commit; we really are
8201 looking at a declaration. */
8202 cp_parser_commit_to_tentative_parse (parser);
8207 /* If we have seen at least one decl-specifier, and the next token
8208 is not a parenthesis, then we must be looking at a declaration.
8209 (After "int (" we might be looking at a functional cast.) */
8210 if (decl_specifiers.any_specifiers_p
8211 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8212 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8213 && !cp_parser_error_occurred (parser))
8214 cp_parser_commit_to_tentative_parse (parser);
8216 /* Keep going until we hit the `;' at the end of the simple
8218 saw_declarator = false;
8219 while (cp_lexer_next_token_is_not (parser->lexer,
8223 bool function_definition_p;
8228 /* If we are processing next declarator, coma is expected */
8229 token = cp_lexer_peek_token (parser->lexer);
8230 gcc_assert (token->type == CPP_COMMA);
8231 cp_lexer_consume_token (parser->lexer);
8234 saw_declarator = true;
8236 /* Parse the init-declarator. */
8237 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8239 function_definition_allowed_p,
8241 declares_class_or_enum,
8242 &function_definition_p);
8243 /* If an error occurred while parsing tentatively, exit quickly.
8244 (That usually happens when in the body of a function; each
8245 statement is treated as a declaration-statement until proven
8247 if (cp_parser_error_occurred (parser))
8249 /* Handle function definitions specially. */
8250 if (function_definition_p)
8252 /* If the next token is a `,', then we are probably
8253 processing something like:
8257 which is erroneous. */
8258 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8260 cp_token *token = cp_lexer_peek_token (parser->lexer);
8261 error ("%Hmixing declarations and function-definitions is forbidden",
8264 /* Otherwise, we're done with the list of declarators. */
8267 pop_deferring_access_checks ();
8271 /* The next token should be either a `,' or a `;'. */
8272 token = cp_lexer_peek_token (parser->lexer);
8273 /* If it's a `,', there are more declarators to come. */
8274 if (token->type == CPP_COMMA)
8275 /* will be consumed next time around */;
8276 /* If it's a `;', we are done. */
8277 else if (token->type == CPP_SEMICOLON)
8279 /* Anything else is an error. */
8282 /* If we have already issued an error message we don't need
8283 to issue another one. */
8284 if (decl != error_mark_node
8285 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8286 cp_parser_error (parser, "expected %<,%> or %<;%>");
8287 /* Skip tokens until we reach the end of the statement. */
8288 cp_parser_skip_to_end_of_statement (parser);
8289 /* If the next token is now a `;', consume it. */
8290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8291 cp_lexer_consume_token (parser->lexer);
8294 /* After the first time around, a function-definition is not
8295 allowed -- even if it was OK at first. For example:
8300 function_definition_allowed_p = false;
8303 /* Issue an error message if no declarators are present, and the
8304 decl-specifier-seq does not itself declare a class or
8306 if (!saw_declarator)
8308 if (cp_parser_declares_only_class_p (parser))
8309 shadow_tag (&decl_specifiers);
8310 /* Perform any deferred access checks. */
8311 perform_deferred_access_checks ();
8314 /* Consume the `;'. */
8315 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8318 pop_deferring_access_checks ();
8321 /* Parse a decl-specifier-seq.
8324 decl-specifier-seq [opt] decl-specifier
8327 storage-class-specifier
8338 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8340 The parser flags FLAGS is used to control type-specifier parsing.
8342 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8345 1: one of the decl-specifiers is an elaborated-type-specifier
8346 (i.e., a type declaration)
8347 2: one of the decl-specifiers is an enum-specifier or a
8348 class-specifier (i.e., a type definition)
8353 cp_parser_decl_specifier_seq (cp_parser* parser,
8354 cp_parser_flags flags,
8355 cp_decl_specifier_seq *decl_specs,
8356 int* declares_class_or_enum)
8358 bool constructor_possible_p = !parser->in_declarator_p;
8359 cp_token *start_token = NULL;
8361 /* Clear DECL_SPECS. */
8362 clear_decl_specs (decl_specs);
8364 /* Assume no class or enumeration type is declared. */
8365 *declares_class_or_enum = 0;
8367 /* Keep reading specifiers until there are no more to read. */
8371 bool found_decl_spec;
8374 /* Peek at the next token. */
8375 token = cp_lexer_peek_token (parser->lexer);
8377 /* Save the first token of the decl spec list for error
8380 start_token = token;
8381 /* Handle attributes. */
8382 if (token->keyword == RID_ATTRIBUTE)
8384 /* Parse the attributes. */
8385 decl_specs->attributes
8386 = chainon (decl_specs->attributes,
8387 cp_parser_attributes_opt (parser));
8390 /* Assume we will find a decl-specifier keyword. */
8391 found_decl_spec = true;
8392 /* If the next token is an appropriate keyword, we can simply
8393 add it to the list. */
8394 switch (token->keyword)
8399 if (!at_class_scope_p ())
8401 error ("%H%<friend%> used outside of class", &token->location);
8402 cp_lexer_purge_token (parser->lexer);
8406 ++decl_specs->specs[(int) ds_friend];
8407 /* Consume the token. */
8408 cp_lexer_consume_token (parser->lexer);
8412 /* function-specifier:
8419 cp_parser_function_specifier_opt (parser, decl_specs);
8425 ++decl_specs->specs[(int) ds_typedef];
8426 /* Consume the token. */
8427 cp_lexer_consume_token (parser->lexer);
8428 /* A constructor declarator cannot appear in a typedef. */
8429 constructor_possible_p = false;
8430 /* The "typedef" keyword can only occur in a declaration; we
8431 may as well commit at this point. */
8432 cp_parser_commit_to_tentative_parse (parser);
8434 if (decl_specs->storage_class != sc_none)
8435 decl_specs->conflicting_specifiers_p = true;
8438 /* storage-class-specifier:
8448 if (cxx_dialect == cxx98)
8450 /* Consume the token. */
8451 cp_lexer_consume_token (parser->lexer);
8453 /* Complain about `auto' as a storage specifier, if
8454 we're complaining about C++0x compatibility. */
8457 "%H%<auto%> will change meaning in C++0x; please remove it",
8460 /* Set the storage class anyway. */
8461 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8465 /* C++0x auto type-specifier. */
8466 found_decl_spec = false;
8473 /* Consume the token. */
8474 cp_lexer_consume_token (parser->lexer);
8475 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8479 /* Consume the token. */
8480 cp_lexer_consume_token (parser->lexer);
8481 ++decl_specs->specs[(int) ds_thread];
8485 /* We did not yet find a decl-specifier yet. */
8486 found_decl_spec = false;
8490 /* Constructors are a special case. The `S' in `S()' is not a
8491 decl-specifier; it is the beginning of the declarator. */
8494 && constructor_possible_p
8495 && (cp_parser_constructor_declarator_p
8496 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8498 /* If we don't have a DECL_SPEC yet, then we must be looking at
8499 a type-specifier. */
8500 if (!found_decl_spec && !constructor_p)
8502 int decl_spec_declares_class_or_enum;
8503 bool is_cv_qualifier;
8507 = cp_parser_type_specifier (parser, flags,
8509 /*is_declaration=*/true,
8510 &decl_spec_declares_class_or_enum,
8512 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8514 /* If this type-specifier referenced a user-defined type
8515 (a typedef, class-name, etc.), then we can't allow any
8516 more such type-specifiers henceforth.
8520 The longest sequence of decl-specifiers that could
8521 possibly be a type name is taken as the
8522 decl-specifier-seq of a declaration. The sequence shall
8523 be self-consistent as described below.
8527 As a general rule, at most one type-specifier is allowed
8528 in the complete decl-specifier-seq of a declaration. The
8529 only exceptions are the following:
8531 -- const or volatile can be combined with any other
8534 -- signed or unsigned can be combined with char, long,
8542 void g (const int Pc);
8544 Here, Pc is *not* part of the decl-specifier seq; it's
8545 the declarator. Therefore, once we see a type-specifier
8546 (other than a cv-qualifier), we forbid any additional
8547 user-defined types. We *do* still allow things like `int
8548 int' to be considered a decl-specifier-seq, and issue the
8549 error message later. */
8550 if (type_spec && !is_cv_qualifier)
8551 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8552 /* A constructor declarator cannot follow a type-specifier. */
8555 constructor_possible_p = false;
8556 found_decl_spec = true;
8560 /* If we still do not have a DECL_SPEC, then there are no more
8562 if (!found_decl_spec)
8565 decl_specs->any_specifiers_p = true;
8566 /* After we see one decl-specifier, further decl-specifiers are
8568 flags |= CP_PARSER_FLAGS_OPTIONAL;
8571 cp_parser_check_decl_spec (decl_specs, start_token->location);
8573 /* Don't allow a friend specifier with a class definition. */
8574 if (decl_specs->specs[(int) ds_friend] != 0
8575 && (*declares_class_or_enum & 2))
8576 error ("%Hclass definition may not be declared a friend",
8577 &start_token->location);
8580 /* Parse an (optional) storage-class-specifier.
8582 storage-class-specifier:
8591 storage-class-specifier:
8594 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8597 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8599 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8602 if (cxx_dialect != cxx98)
8604 /* Fall through for C++98. */
8611 /* Consume the token. */
8612 return cp_lexer_consume_token (parser->lexer)->u.value;
8619 /* Parse an (optional) function-specifier.
8626 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8627 Updates DECL_SPECS, if it is non-NULL. */
8630 cp_parser_function_specifier_opt (cp_parser* parser,
8631 cp_decl_specifier_seq *decl_specs)
8633 cp_token *token = cp_lexer_peek_token (parser->lexer);
8634 switch (token->keyword)
8638 ++decl_specs->specs[(int) ds_inline];
8642 /* 14.5.2.3 [temp.mem]
8644 A member function template shall not be virtual. */
8645 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8646 error ("%Htemplates may not be %<virtual%>", &token->location);
8647 else if (decl_specs)
8648 ++decl_specs->specs[(int) ds_virtual];
8653 ++decl_specs->specs[(int) ds_explicit];
8660 /* Consume the token. */
8661 return cp_lexer_consume_token (parser->lexer)->u.value;
8664 /* Parse a linkage-specification.
8666 linkage-specification:
8667 extern string-literal { declaration-seq [opt] }
8668 extern string-literal declaration */
8671 cp_parser_linkage_specification (cp_parser* parser)
8675 /* Look for the `extern' keyword. */
8676 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8678 /* Look for the string-literal. */
8679 linkage = cp_parser_string_literal (parser, false, false);
8681 /* Transform the literal into an identifier. If the literal is a
8682 wide-character string, or contains embedded NULs, then we can't
8683 handle it as the user wants. */
8684 if (strlen (TREE_STRING_POINTER (linkage))
8685 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8687 cp_parser_error (parser, "invalid linkage-specification");
8688 /* Assume C++ linkage. */
8689 linkage = lang_name_cplusplus;
8692 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8694 /* We're now using the new linkage. */
8695 push_lang_context (linkage);
8697 /* If the next token is a `{', then we're using the first
8699 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8701 /* Consume the `{' token. */
8702 cp_lexer_consume_token (parser->lexer);
8703 /* Parse the declarations. */
8704 cp_parser_declaration_seq_opt (parser);
8705 /* Look for the closing `}'. */
8706 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8708 /* Otherwise, there's just one declaration. */
8711 bool saved_in_unbraced_linkage_specification_p;
8713 saved_in_unbraced_linkage_specification_p
8714 = parser->in_unbraced_linkage_specification_p;
8715 parser->in_unbraced_linkage_specification_p = true;
8716 cp_parser_declaration (parser);
8717 parser->in_unbraced_linkage_specification_p
8718 = saved_in_unbraced_linkage_specification_p;
8721 /* We're done with the linkage-specification. */
8722 pop_lang_context ();
8725 /* Parse a static_assert-declaration.
8727 static_assert-declaration:
8728 static_assert ( constant-expression , string-literal ) ;
8730 If MEMBER_P, this static_assert is a class member. */
8733 cp_parser_static_assert(cp_parser *parser, bool member_p)
8738 location_t saved_loc;
8740 /* Peek at the `static_assert' token so we can keep track of exactly
8741 where the static assertion started. */
8742 token = cp_lexer_peek_token (parser->lexer);
8743 saved_loc = token->location;
8745 /* Look for the `static_assert' keyword. */
8746 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8747 "%<static_assert%>"))
8750 /* We know we are in a static assertion; commit to any tentative
8752 if (cp_parser_parsing_tentatively (parser))
8753 cp_parser_commit_to_tentative_parse (parser);
8755 /* Parse the `(' starting the static assertion condition. */
8756 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8758 /* Parse the constant-expression. */
8760 cp_parser_constant_expression (parser,
8761 /*allow_non_constant_p=*/false,
8762 /*non_constant_p=*/NULL);
8764 /* Parse the separating `,'. */
8765 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8767 /* Parse the string-literal message. */
8768 message = cp_parser_string_literal (parser,
8769 /*translate=*/false,
8772 /* A `)' completes the static assertion. */
8773 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8774 cp_parser_skip_to_closing_parenthesis (parser,
8775 /*recovering=*/true,
8777 /*consume_paren=*/true);
8779 /* A semicolon terminates the declaration. */
8780 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8782 /* Complete the static assertion, which may mean either processing
8783 the static assert now or saving it for template instantiation. */
8784 finish_static_assert (condition, message, saved_loc, member_p);
8787 /* Parse a `decltype' type. Returns the type.
8789 simple-type-specifier:
8790 decltype ( expression ) */
8793 cp_parser_decltype (cp_parser *parser)
8796 bool id_expression_or_member_access_p = false;
8797 const char *saved_message;
8798 bool saved_integral_constant_expression_p;
8799 bool saved_non_integral_constant_expression_p;
8800 cp_token *id_expr_start_token;
8802 /* Look for the `decltype' token. */
8803 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8804 return error_mark_node;
8806 /* Types cannot be defined in a `decltype' expression. Save away the
8808 saved_message = parser->type_definition_forbidden_message;
8810 /* And create the new one. */
8811 parser->type_definition_forbidden_message
8812 = "types may not be defined in %<decltype%> expressions";
8814 /* The restrictions on constant-expressions do not apply inside
8815 decltype expressions. */
8816 saved_integral_constant_expression_p
8817 = parser->integral_constant_expression_p;
8818 saved_non_integral_constant_expression_p
8819 = parser->non_integral_constant_expression_p;
8820 parser->integral_constant_expression_p = false;
8822 /* Do not actually evaluate the expression. */
8825 /* Parse the opening `('. */
8826 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8827 return error_mark_node;
8829 /* First, try parsing an id-expression. */
8830 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8831 cp_parser_parse_tentatively (parser);
8832 expr = cp_parser_id_expression (parser,
8833 /*template_keyword_p=*/false,
8834 /*check_dependency_p=*/true,
8835 /*template_p=*/NULL,
8836 /*declarator_p=*/false,
8837 /*optional_p=*/false);
8839 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8841 bool non_integral_constant_expression_p = false;
8842 tree id_expression = expr;
8844 const char *error_msg;
8846 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8847 /* Lookup the name we got back from the id-expression. */
8848 expr = cp_parser_lookup_name (parser, expr,
8850 /*is_template=*/false,
8851 /*is_namespace=*/false,
8852 /*check_dependency=*/true,
8853 /*ambiguous_decls=*/NULL,
8854 id_expr_start_token->location);
8857 && expr != error_mark_node
8858 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8859 && TREE_CODE (expr) != TYPE_DECL
8860 && (TREE_CODE (expr) != BIT_NOT_EXPR
8861 || !TYPE_P (TREE_OPERAND (expr, 0)))
8862 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8864 /* Complete lookup of the id-expression. */
8865 expr = (finish_id_expression
8866 (id_expression, expr, parser->scope, &idk,
8867 /*integral_constant_expression_p=*/false,
8868 /*allow_non_integral_constant_expression_p=*/true,
8869 &non_integral_constant_expression_p,
8870 /*template_p=*/false,
8872 /*address_p=*/false,
8873 /*template_arg_p=*/false,
8875 id_expr_start_token->location));
8877 if (expr == error_mark_node)
8878 /* We found an id-expression, but it was something that we
8879 should not have found. This is an error, not something
8880 we can recover from, so note that we found an
8881 id-expression and we'll recover as gracefully as
8883 id_expression_or_member_access_p = true;
8887 && expr != error_mark_node
8888 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8889 /* We have an id-expression. */
8890 id_expression_or_member_access_p = true;
8893 if (!id_expression_or_member_access_p)
8895 /* Abort the id-expression parse. */
8896 cp_parser_abort_tentative_parse (parser);
8898 /* Parsing tentatively, again. */
8899 cp_parser_parse_tentatively (parser);
8901 /* Parse a class member access. */
8902 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8904 /*member_access_only_p=*/true, NULL);
8907 && expr != error_mark_node
8908 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8909 /* We have an id-expression. */
8910 id_expression_or_member_access_p = true;
8913 if (id_expression_or_member_access_p)
8914 /* We have parsed the complete id-expression or member access. */
8915 cp_parser_parse_definitely (parser);
8918 /* Abort our attempt to parse an id-expression or member access
8920 cp_parser_abort_tentative_parse (parser);
8922 /* Parse a full expression. */
8923 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8926 /* Go back to evaluating expressions. */
8929 /* Restore the old message and the integral constant expression
8931 parser->type_definition_forbidden_message = saved_message;
8932 parser->integral_constant_expression_p
8933 = saved_integral_constant_expression_p;
8934 parser->non_integral_constant_expression_p
8935 = saved_non_integral_constant_expression_p;
8937 if (expr == error_mark_node)
8939 /* Skip everything up to the closing `)'. */
8940 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8941 /*consume_paren=*/true);
8942 return error_mark_node;
8945 /* Parse to the closing `)'. */
8946 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8948 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8949 /*consume_paren=*/true);
8950 return error_mark_node;
8953 return finish_decltype_type (expr, id_expression_or_member_access_p);
8956 /* Special member functions [gram.special] */
8958 /* Parse a conversion-function-id.
8960 conversion-function-id:
8961 operator conversion-type-id
8963 Returns an IDENTIFIER_NODE representing the operator. */
8966 cp_parser_conversion_function_id (cp_parser* parser)
8970 tree saved_qualifying_scope;
8971 tree saved_object_scope;
8972 tree pushed_scope = NULL_TREE;
8974 /* Look for the `operator' token. */
8975 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8976 return error_mark_node;
8977 /* When we parse the conversion-type-id, the current scope will be
8978 reset. However, we need that information in able to look up the
8979 conversion function later, so we save it here. */
8980 saved_scope = parser->scope;
8981 saved_qualifying_scope = parser->qualifying_scope;
8982 saved_object_scope = parser->object_scope;
8983 /* We must enter the scope of the class so that the names of
8984 entities declared within the class are available in the
8985 conversion-type-id. For example, consider:
8992 S::operator I() { ... }
8994 In order to see that `I' is a type-name in the definition, we
8995 must be in the scope of `S'. */
8997 pushed_scope = push_scope (saved_scope);
8998 /* Parse the conversion-type-id. */
8999 type = cp_parser_conversion_type_id (parser);
9000 /* Leave the scope of the class, if any. */
9002 pop_scope (pushed_scope);
9003 /* Restore the saved scope. */
9004 parser->scope = saved_scope;
9005 parser->qualifying_scope = saved_qualifying_scope;
9006 parser->object_scope = saved_object_scope;
9007 /* If the TYPE is invalid, indicate failure. */
9008 if (type == error_mark_node)
9009 return error_mark_node;
9010 return mangle_conv_op_name_for_type (type);
9013 /* Parse a conversion-type-id:
9016 type-specifier-seq conversion-declarator [opt]
9018 Returns the TYPE specified. */
9021 cp_parser_conversion_type_id (cp_parser* parser)
9024 cp_decl_specifier_seq type_specifiers;
9025 cp_declarator *declarator;
9026 tree type_specified;
9028 /* Parse the attributes. */
9029 attributes = cp_parser_attributes_opt (parser);
9030 /* Parse the type-specifiers. */
9031 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9033 /* If that didn't work, stop. */
9034 if (type_specifiers.type == error_mark_node)
9035 return error_mark_node;
9036 /* Parse the conversion-declarator. */
9037 declarator = cp_parser_conversion_declarator_opt (parser);
9039 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9040 /*initialized=*/0, &attributes);
9042 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9044 /* Don't give this error when parsing tentatively. This happens to
9045 work because we always parse this definitively once. */
9046 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9047 && type_uses_auto (type_specified))
9049 error ("invalid use of %<auto%> in conversion operator");
9050 return error_mark_node;
9053 return type_specified;
9056 /* Parse an (optional) conversion-declarator.
9058 conversion-declarator:
9059 ptr-operator conversion-declarator [opt]
9063 static cp_declarator *
9064 cp_parser_conversion_declarator_opt (cp_parser* parser)
9066 enum tree_code code;
9068 cp_cv_quals cv_quals;
9070 /* We don't know if there's a ptr-operator next, or not. */
9071 cp_parser_parse_tentatively (parser);
9072 /* Try the ptr-operator. */
9073 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9074 /* If it worked, look for more conversion-declarators. */
9075 if (cp_parser_parse_definitely (parser))
9077 cp_declarator *declarator;
9079 /* Parse another optional declarator. */
9080 declarator = cp_parser_conversion_declarator_opt (parser);
9082 return cp_parser_make_indirect_declarator
9083 (code, class_type, cv_quals, declarator);
9089 /* Parse an (optional) ctor-initializer.
9092 : mem-initializer-list
9094 Returns TRUE iff the ctor-initializer was actually present. */
9097 cp_parser_ctor_initializer_opt (cp_parser* parser)
9099 /* If the next token is not a `:', then there is no
9100 ctor-initializer. */
9101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9103 /* Do default initialization of any bases and members. */
9104 if (DECL_CONSTRUCTOR_P (current_function_decl))
9105 finish_mem_initializers (NULL_TREE);
9110 /* Consume the `:' token. */
9111 cp_lexer_consume_token (parser->lexer);
9112 /* And the mem-initializer-list. */
9113 cp_parser_mem_initializer_list (parser);
9118 /* Parse a mem-initializer-list.
9120 mem-initializer-list:
9121 mem-initializer ... [opt]
9122 mem-initializer ... [opt] , mem-initializer-list */
9125 cp_parser_mem_initializer_list (cp_parser* parser)
9127 tree mem_initializer_list = NULL_TREE;
9128 cp_token *token = cp_lexer_peek_token (parser->lexer);
9130 /* Let the semantic analysis code know that we are starting the
9131 mem-initializer-list. */
9132 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9133 error ("%Honly constructors take base initializers",
9136 /* Loop through the list. */
9139 tree mem_initializer;
9141 token = cp_lexer_peek_token (parser->lexer);
9142 /* Parse the mem-initializer. */
9143 mem_initializer = cp_parser_mem_initializer (parser);
9144 /* If the next token is a `...', we're expanding member initializers. */
9145 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9147 /* Consume the `...'. */
9148 cp_lexer_consume_token (parser->lexer);
9150 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9151 can be expanded but members cannot. */
9152 if (mem_initializer != error_mark_node
9153 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9155 error ("%Hcannot expand initializer for member %<%D%>",
9156 &token->location, TREE_PURPOSE (mem_initializer));
9157 mem_initializer = error_mark_node;
9160 /* Construct the pack expansion type. */
9161 if (mem_initializer != error_mark_node)
9162 mem_initializer = make_pack_expansion (mem_initializer);
9164 /* Add it to the list, unless it was erroneous. */
9165 if (mem_initializer != error_mark_node)
9167 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9168 mem_initializer_list = mem_initializer;
9170 /* If the next token is not a `,', we're done. */
9171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9173 /* Consume the `,' token. */
9174 cp_lexer_consume_token (parser->lexer);
9177 /* Perform semantic analysis. */
9178 if (DECL_CONSTRUCTOR_P (current_function_decl))
9179 finish_mem_initializers (mem_initializer_list);
9182 /* Parse a mem-initializer.
9185 mem-initializer-id ( expression-list [opt] )
9186 mem-initializer-id braced-init-list
9191 ( expression-list [opt] )
9193 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9194 class) or FIELD_DECL (for a non-static data member) to initialize;
9195 the TREE_VALUE is the expression-list. An empty initialization
9196 list is represented by void_list_node. */
9199 cp_parser_mem_initializer (cp_parser* parser)
9201 tree mem_initializer_id;
9202 tree expression_list;
9204 cp_token *token = cp_lexer_peek_token (parser->lexer);
9206 /* Find out what is being initialized. */
9207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9209 permerror (token->location,
9210 "anachronistic old-style base class initializer");
9211 mem_initializer_id = NULL_TREE;
9215 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9216 if (mem_initializer_id == error_mark_node)
9217 return mem_initializer_id;
9219 member = expand_member_init (mem_initializer_id);
9220 if (member && !DECL_P (member))
9221 in_base_initializer = 1;
9223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9225 bool expr_non_constant_p;
9226 maybe_warn_cpp0x ("extended initializer lists");
9227 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9228 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9229 expression_list = build_tree_list (NULL_TREE, expression_list);
9233 = cp_parser_parenthesized_expression_list (parser, false,
9235 /*allow_expansion_p=*/true,
9236 /*non_constant_p=*/NULL);
9237 if (expression_list == error_mark_node)
9238 return error_mark_node;
9239 if (!expression_list)
9240 expression_list = void_type_node;
9242 in_base_initializer = 0;
9244 return member ? build_tree_list (member, expression_list) : error_mark_node;
9247 /* Parse a mem-initializer-id.
9250 :: [opt] nested-name-specifier [opt] class-name
9253 Returns a TYPE indicating the class to be initializer for the first
9254 production. Returns an IDENTIFIER_NODE indicating the data member
9255 to be initialized for the second production. */
9258 cp_parser_mem_initializer_id (cp_parser* parser)
9260 bool global_scope_p;
9261 bool nested_name_specifier_p;
9262 bool template_p = false;
9265 cp_token *token = cp_lexer_peek_token (parser->lexer);
9267 /* `typename' is not allowed in this context ([temp.res]). */
9268 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9270 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9271 "member initializer is implicitly a type)",
9273 cp_lexer_consume_token (parser->lexer);
9275 /* Look for the optional `::' operator. */
9277 = (cp_parser_global_scope_opt (parser,
9278 /*current_scope_valid_p=*/false)
9280 /* Look for the optional nested-name-specifier. The simplest way to
9285 The keyword `typename' is not permitted in a base-specifier or
9286 mem-initializer; in these contexts a qualified name that
9287 depends on a template-parameter is implicitly assumed to be a
9290 is to assume that we have seen the `typename' keyword at this
9292 nested_name_specifier_p
9293 = (cp_parser_nested_name_specifier_opt (parser,
9294 /*typename_keyword_p=*/true,
9295 /*check_dependency_p=*/true,
9297 /*is_declaration=*/true)
9299 if (nested_name_specifier_p)
9300 template_p = cp_parser_optional_template_keyword (parser);
9301 /* If there is a `::' operator or a nested-name-specifier, then we
9302 are definitely looking for a class-name. */
9303 if (global_scope_p || nested_name_specifier_p)
9304 return cp_parser_class_name (parser,
9305 /*typename_keyword_p=*/true,
9306 /*template_keyword_p=*/template_p,
9308 /*check_dependency_p=*/true,
9309 /*class_head_p=*/false,
9310 /*is_declaration=*/true);
9311 /* Otherwise, we could also be looking for an ordinary identifier. */
9312 cp_parser_parse_tentatively (parser);
9313 /* Try a class-name. */
9314 id = cp_parser_class_name (parser,
9315 /*typename_keyword_p=*/true,
9316 /*template_keyword_p=*/false,
9318 /*check_dependency_p=*/true,
9319 /*class_head_p=*/false,
9320 /*is_declaration=*/true);
9321 /* If we found one, we're done. */
9322 if (cp_parser_parse_definitely (parser))
9324 /* Otherwise, look for an ordinary identifier. */
9325 return cp_parser_identifier (parser);
9328 /* Overloading [gram.over] */
9330 /* Parse an operator-function-id.
9332 operator-function-id:
9335 Returns an IDENTIFIER_NODE for the operator which is a
9336 human-readable spelling of the identifier, e.g., `operator +'. */
9339 cp_parser_operator_function_id (cp_parser* parser)
9341 /* Look for the `operator' keyword. */
9342 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9343 return error_mark_node;
9344 /* And then the name of the operator itself. */
9345 return cp_parser_operator (parser);
9348 /* Parse an operator.
9351 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9352 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9353 || ++ -- , ->* -> () []
9360 Returns an IDENTIFIER_NODE for the operator which is a
9361 human-readable spelling of the identifier, e.g., `operator +'. */
9364 cp_parser_operator (cp_parser* parser)
9366 tree id = NULL_TREE;
9369 /* Peek at the next token. */
9370 token = cp_lexer_peek_token (parser->lexer);
9371 /* Figure out which operator we have. */
9372 switch (token->type)
9378 /* The keyword should be either `new' or `delete'. */
9379 if (token->keyword == RID_NEW)
9381 else if (token->keyword == RID_DELETE)
9386 /* Consume the `new' or `delete' token. */
9387 cp_lexer_consume_token (parser->lexer);
9389 /* Peek at the next token. */
9390 token = cp_lexer_peek_token (parser->lexer);
9391 /* If it's a `[' token then this is the array variant of the
9393 if (token->type == CPP_OPEN_SQUARE)
9395 /* Consume the `[' token. */
9396 cp_lexer_consume_token (parser->lexer);
9397 /* Look for the `]' token. */
9398 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9399 id = ansi_opname (op == NEW_EXPR
9400 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9402 /* Otherwise, we have the non-array variant. */
9404 id = ansi_opname (op);
9410 id = ansi_opname (PLUS_EXPR);
9414 id = ansi_opname (MINUS_EXPR);
9418 id = ansi_opname (MULT_EXPR);
9422 id = ansi_opname (TRUNC_DIV_EXPR);
9426 id = ansi_opname (TRUNC_MOD_EXPR);
9430 id = ansi_opname (BIT_XOR_EXPR);
9434 id = ansi_opname (BIT_AND_EXPR);
9438 id = ansi_opname (BIT_IOR_EXPR);
9442 id = ansi_opname (BIT_NOT_EXPR);
9446 id = ansi_opname (TRUTH_NOT_EXPR);
9450 id = ansi_assopname (NOP_EXPR);
9454 id = ansi_opname (LT_EXPR);
9458 id = ansi_opname (GT_EXPR);
9462 id = ansi_assopname (PLUS_EXPR);
9466 id = ansi_assopname (MINUS_EXPR);
9470 id = ansi_assopname (MULT_EXPR);
9474 id = ansi_assopname (TRUNC_DIV_EXPR);
9478 id = ansi_assopname (TRUNC_MOD_EXPR);
9482 id = ansi_assopname (BIT_XOR_EXPR);
9486 id = ansi_assopname (BIT_AND_EXPR);
9490 id = ansi_assopname (BIT_IOR_EXPR);
9494 id = ansi_opname (LSHIFT_EXPR);
9498 id = ansi_opname (RSHIFT_EXPR);
9502 id = ansi_assopname (LSHIFT_EXPR);
9506 id = ansi_assopname (RSHIFT_EXPR);
9510 id = ansi_opname (EQ_EXPR);
9514 id = ansi_opname (NE_EXPR);
9518 id = ansi_opname (LE_EXPR);
9521 case CPP_GREATER_EQ:
9522 id = ansi_opname (GE_EXPR);
9526 id = ansi_opname (TRUTH_ANDIF_EXPR);
9530 id = ansi_opname (TRUTH_ORIF_EXPR);
9534 id = ansi_opname (POSTINCREMENT_EXPR);
9537 case CPP_MINUS_MINUS:
9538 id = ansi_opname (PREDECREMENT_EXPR);
9542 id = ansi_opname (COMPOUND_EXPR);
9545 case CPP_DEREF_STAR:
9546 id = ansi_opname (MEMBER_REF);
9550 id = ansi_opname (COMPONENT_REF);
9553 case CPP_OPEN_PAREN:
9554 /* Consume the `('. */
9555 cp_lexer_consume_token (parser->lexer);
9556 /* Look for the matching `)'. */
9557 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9558 return ansi_opname (CALL_EXPR);
9560 case CPP_OPEN_SQUARE:
9561 /* Consume the `['. */
9562 cp_lexer_consume_token (parser->lexer);
9563 /* Look for the matching `]'. */
9564 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9565 return ansi_opname (ARRAY_REF);
9568 /* Anything else is an error. */
9572 /* If we have selected an identifier, we need to consume the
9575 cp_lexer_consume_token (parser->lexer);
9576 /* Otherwise, no valid operator name was present. */
9579 cp_parser_error (parser, "expected operator");
9580 id = error_mark_node;
9586 /* Parse a template-declaration.
9588 template-declaration:
9589 export [opt] template < template-parameter-list > declaration
9591 If MEMBER_P is TRUE, this template-declaration occurs within a
9594 The grammar rule given by the standard isn't correct. What
9597 template-declaration:
9598 export [opt] template-parameter-list-seq
9599 decl-specifier-seq [opt] init-declarator [opt] ;
9600 export [opt] template-parameter-list-seq
9603 template-parameter-list-seq:
9604 template-parameter-list-seq [opt]
9605 template < template-parameter-list > */
9608 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9610 /* Check for `export'. */
9611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9613 /* Consume the `export' token. */
9614 cp_lexer_consume_token (parser->lexer);
9615 /* Warn that we do not support `export'. */
9616 warning (0, "keyword %<export%> not implemented, and will be ignored");
9619 cp_parser_template_declaration_after_export (parser, member_p);
9622 /* Parse a template-parameter-list.
9624 template-parameter-list:
9626 template-parameter-list , template-parameter
9628 Returns a TREE_LIST. Each node represents a template parameter.
9629 The nodes are connected via their TREE_CHAINs. */
9632 cp_parser_template_parameter_list (cp_parser* parser)
9634 tree parameter_list = NULL_TREE;
9636 begin_template_parm_list ();
9641 bool is_parameter_pack;
9643 /* Parse the template-parameter. */
9644 parameter = cp_parser_template_parameter (parser,
9646 &is_parameter_pack);
9647 /* Add it to the list. */
9648 if (parameter != error_mark_node)
9649 parameter_list = process_template_parm (parameter_list,
9655 tree err_parm = build_tree_list (parameter, parameter);
9656 TREE_VALUE (err_parm) = error_mark_node;
9657 parameter_list = chainon (parameter_list, err_parm);
9660 /* If the next token is not a `,', we're done. */
9661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9663 /* Otherwise, consume the `,' token. */
9664 cp_lexer_consume_token (parser->lexer);
9667 return end_template_parm_list (parameter_list);
9670 /* Parse a template-parameter.
9674 parameter-declaration
9676 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9677 the parameter. The TREE_PURPOSE is the default value, if any.
9678 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9679 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9680 set to true iff this parameter is a parameter pack. */
9683 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9684 bool *is_parameter_pack)
9687 cp_parameter_declarator *parameter_declarator;
9688 cp_declarator *id_declarator;
9691 /* Assume it is a type parameter or a template parameter. */
9692 *is_non_type = false;
9693 /* Assume it not a parameter pack. */
9694 *is_parameter_pack = false;
9695 /* Peek at the next token. */
9696 token = cp_lexer_peek_token (parser->lexer);
9697 /* If it is `class' or `template', we have a type-parameter. */
9698 if (token->keyword == RID_TEMPLATE)
9699 return cp_parser_type_parameter (parser, is_parameter_pack);
9700 /* If it is `class' or `typename' we do not know yet whether it is a
9701 type parameter or a non-type parameter. Consider:
9703 template <typename T, typename T::X X> ...
9707 template <class C, class D*> ...
9709 Here, the first parameter is a type parameter, and the second is
9710 a non-type parameter. We can tell by looking at the token after
9711 the identifier -- if it is a `,', `=', or `>' then we have a type
9713 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9715 /* Peek at the token after `class' or `typename'. */
9716 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9717 /* If it's an ellipsis, we have a template type parameter
9719 if (token->type == CPP_ELLIPSIS)
9720 return cp_parser_type_parameter (parser, is_parameter_pack);
9721 /* If it's an identifier, skip it. */
9722 if (token->type == CPP_NAME)
9723 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9724 /* Now, see if the token looks like the end of a template
9726 if (token->type == CPP_COMMA
9727 || token->type == CPP_EQ
9728 || token->type == CPP_GREATER)
9729 return cp_parser_type_parameter (parser, is_parameter_pack);
9732 /* Otherwise, it is a non-type parameter.
9736 When parsing a default template-argument for a non-type
9737 template-parameter, the first non-nested `>' is taken as the end
9738 of the template parameter-list rather than a greater-than
9740 *is_non_type = true;
9741 parameter_declarator
9742 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9743 /*parenthesized_p=*/NULL);
9745 /* If the parameter declaration is marked as a parameter pack, set
9746 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9747 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9749 if (parameter_declarator
9750 && parameter_declarator->declarator
9751 && parameter_declarator->declarator->parameter_pack_p)
9753 *is_parameter_pack = true;
9754 parameter_declarator->declarator->parameter_pack_p = false;
9757 /* If the next token is an ellipsis, and we don't already have it
9758 marked as a parameter pack, then we have a parameter pack (that
9759 has no declarator). */
9760 if (!*is_parameter_pack
9761 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9762 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9764 /* Consume the `...'. */
9765 cp_lexer_consume_token (parser->lexer);
9766 maybe_warn_variadic_templates ();
9768 *is_parameter_pack = true;
9770 /* We might end up with a pack expansion as the type of the non-type
9771 template parameter, in which case this is a non-type template
9773 else if (parameter_declarator
9774 && parameter_declarator->decl_specifiers.type
9775 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9777 *is_parameter_pack = true;
9778 parameter_declarator->decl_specifiers.type =
9779 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9782 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9784 /* Parameter packs cannot have default arguments. However, a
9785 user may try to do so, so we'll parse them and give an
9786 appropriate diagnostic here. */
9788 /* Consume the `='. */
9789 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9790 cp_lexer_consume_token (parser->lexer);
9792 /* Find the name of the parameter pack. */
9793 id_declarator = parameter_declarator->declarator;
9794 while (id_declarator && id_declarator->kind != cdk_id)
9795 id_declarator = id_declarator->declarator;
9797 if (id_declarator && id_declarator->kind == cdk_id)
9798 error ("%Htemplate parameter pack %qD cannot have a default argument",
9799 &start_token->location, id_declarator->u.id.unqualified_name);
9801 error ("%Htemplate parameter pack cannot have a default argument",
9802 &start_token->location);
9804 /* Parse the default argument, but throw away the result. */
9805 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9808 parm = grokdeclarator (parameter_declarator->declarator,
9809 ¶meter_declarator->decl_specifiers,
9810 PARM, /*initialized=*/0,
9812 if (parm == error_mark_node)
9813 return error_mark_node;
9815 return build_tree_list (parameter_declarator->default_argument, parm);
9818 /* Parse a type-parameter.
9821 class identifier [opt]
9822 class identifier [opt] = type-id
9823 typename identifier [opt]
9824 typename identifier [opt] = type-id
9825 template < template-parameter-list > class identifier [opt]
9826 template < template-parameter-list > class identifier [opt]
9829 GNU Extension (variadic templates):
9832 class ... identifier [opt]
9833 typename ... identifier [opt]
9835 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9836 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9837 the declaration of the parameter.
9839 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9842 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9847 /* Look for a keyword to tell us what kind of parameter this is. */
9848 token = cp_parser_require (parser, CPP_KEYWORD,
9849 "%<class%>, %<typename%>, or %<template%>");
9851 return error_mark_node;
9853 switch (token->keyword)
9859 tree default_argument;
9861 /* If the next token is an ellipsis, we have a template
9863 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9865 /* Consume the `...' token. */
9866 cp_lexer_consume_token (parser->lexer);
9867 maybe_warn_variadic_templates ();
9869 *is_parameter_pack = true;
9872 /* If the next token is an identifier, then it names the
9874 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9875 identifier = cp_parser_identifier (parser);
9877 identifier = NULL_TREE;
9879 /* Create the parameter. */
9880 parameter = finish_template_type_parm (class_type_node, identifier);
9882 /* If the next token is an `=', we have a default argument. */
9883 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9885 /* Consume the `=' token. */
9886 cp_lexer_consume_token (parser->lexer);
9887 /* Parse the default-argument. */
9888 push_deferring_access_checks (dk_no_deferred);
9889 default_argument = cp_parser_type_id (parser);
9891 /* Template parameter packs cannot have default
9893 if (*is_parameter_pack)
9896 error ("%Htemplate parameter pack %qD cannot have a "
9897 "default argument", &token->location, identifier);
9899 error ("%Htemplate parameter packs cannot have "
9900 "default arguments", &token->location);
9901 default_argument = NULL_TREE;
9903 pop_deferring_access_checks ();
9906 default_argument = NULL_TREE;
9908 /* Create the combined representation of the parameter and the
9909 default argument. */
9910 parameter = build_tree_list (default_argument, parameter);
9916 tree parameter_list;
9918 tree default_argument;
9920 /* Look for the `<'. */
9921 cp_parser_require (parser, CPP_LESS, "%<<%>");
9922 /* Parse the template-parameter-list. */
9923 parameter_list = cp_parser_template_parameter_list (parser);
9924 /* Look for the `>'. */
9925 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9926 /* Look for the `class' keyword. */
9927 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9928 /* If the next token is an ellipsis, we have a template
9930 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9932 /* Consume the `...' token. */
9933 cp_lexer_consume_token (parser->lexer);
9934 maybe_warn_variadic_templates ();
9936 *is_parameter_pack = true;
9938 /* If the next token is an `=', then there is a
9939 default-argument. If the next token is a `>', we are at
9940 the end of the parameter-list. If the next token is a `,',
9941 then we are at the end of this parameter. */
9942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9943 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9944 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9946 identifier = cp_parser_identifier (parser);
9947 /* Treat invalid names as if the parameter were nameless. */
9948 if (identifier == error_mark_node)
9949 identifier = NULL_TREE;
9952 identifier = NULL_TREE;
9954 /* Create the template parameter. */
9955 parameter = finish_template_template_parm (class_type_node,
9958 /* If the next token is an `=', then there is a
9959 default-argument. */
9960 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9964 /* Consume the `='. */
9965 cp_lexer_consume_token (parser->lexer);
9966 /* Parse the id-expression. */
9967 push_deferring_access_checks (dk_no_deferred);
9968 /* save token before parsing the id-expression, for error
9970 token = cp_lexer_peek_token (parser->lexer);
9972 = cp_parser_id_expression (parser,
9973 /*template_keyword_p=*/false,
9974 /*check_dependency_p=*/true,
9975 /*template_p=*/&is_template,
9976 /*declarator_p=*/false,
9977 /*optional_p=*/false);
9978 if (TREE_CODE (default_argument) == TYPE_DECL)
9979 /* If the id-expression was a template-id that refers to
9980 a template-class, we already have the declaration here,
9981 so no further lookup is needed. */
9984 /* Look up the name. */
9986 = cp_parser_lookup_name (parser, default_argument,
9988 /*is_template=*/is_template,
9989 /*is_namespace=*/false,
9990 /*check_dependency=*/true,
9991 /*ambiguous_decls=*/NULL,
9993 /* See if the default argument is valid. */
9995 = check_template_template_default_arg (default_argument);
9997 /* Template parameter packs cannot have default
9999 if (*is_parameter_pack)
10002 error ("%Htemplate parameter pack %qD cannot "
10003 "have a default argument",
10004 &token->location, identifier);
10006 error ("%Htemplate parameter packs cannot "
10007 "have default arguments",
10009 default_argument = NULL_TREE;
10011 pop_deferring_access_checks ();
10014 default_argument = NULL_TREE;
10016 /* Create the combined representation of the parameter and the
10017 default argument. */
10018 parameter = build_tree_list (default_argument, parameter);
10023 gcc_unreachable ();
10030 /* Parse a template-id.
10033 template-name < template-argument-list [opt] >
10035 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10036 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10037 returned. Otherwise, if the template-name names a function, or set
10038 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10039 names a class, returns a TYPE_DECL for the specialization.
10041 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10042 uninstantiated templates. */
10045 cp_parser_template_id (cp_parser *parser,
10046 bool template_keyword_p,
10047 bool check_dependency_p,
10048 bool is_declaration)
10054 cp_token_position start_of_id = 0;
10055 deferred_access_check *chk;
10056 VEC (deferred_access_check,gc) *access_check;
10057 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10058 bool is_identifier;
10060 /* If the next token corresponds to a template-id, there is no need
10062 next_token = cp_lexer_peek_token (parser->lexer);
10063 if (next_token->type == CPP_TEMPLATE_ID)
10065 struct tree_check *check_value;
10067 /* Get the stored value. */
10068 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10069 /* Perform any access checks that were deferred. */
10070 access_check = check_value->checks;
10074 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10077 perform_or_defer_access_check (chk->binfo,
10082 /* Return the stored value. */
10083 return check_value->value;
10086 /* Avoid performing name lookup if there is no possibility of
10087 finding a template-id. */
10088 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10089 || (next_token->type == CPP_NAME
10090 && !cp_parser_nth_token_starts_template_argument_list_p
10093 cp_parser_error (parser, "expected template-id");
10094 return error_mark_node;
10097 /* Remember where the template-id starts. */
10098 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10099 start_of_id = cp_lexer_token_position (parser->lexer, false);
10101 push_deferring_access_checks (dk_deferred);
10103 /* Parse the template-name. */
10104 is_identifier = false;
10105 token = cp_lexer_peek_token (parser->lexer);
10106 templ = cp_parser_template_name (parser, template_keyword_p,
10107 check_dependency_p,
10110 if (templ == error_mark_node || is_identifier)
10112 pop_deferring_access_checks ();
10116 /* If we find the sequence `[:' after a template-name, it's probably
10117 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10118 parse correctly the argument list. */
10119 next_token = cp_lexer_peek_token (parser->lexer);
10120 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10121 if (next_token->type == CPP_OPEN_SQUARE
10122 && next_token->flags & DIGRAPH
10123 && next_token_2->type == CPP_COLON
10124 && !(next_token_2->flags & PREV_WHITE))
10126 cp_parser_parse_tentatively (parser);
10127 /* Change `:' into `::'. */
10128 next_token_2->type = CPP_SCOPE;
10129 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10131 cp_lexer_consume_token (parser->lexer);
10133 /* Parse the arguments. */
10134 arguments = cp_parser_enclosed_template_argument_list (parser);
10135 if (!cp_parser_parse_definitely (parser))
10137 /* If we couldn't parse an argument list, then we revert our changes
10138 and return simply an error. Maybe this is not a template-id
10140 next_token_2->type = CPP_COLON;
10141 cp_parser_error (parser, "expected %<<%>");
10142 pop_deferring_access_checks ();
10143 return error_mark_node;
10145 /* Otherwise, emit an error about the invalid digraph, but continue
10146 parsing because we got our argument list. */
10147 if (permerror (next_token->location,
10148 "%<<::%> cannot begin a template-argument list"))
10150 static bool hint = false;
10151 inform (next_token->location,
10152 "%<<:%> is an alternate spelling for %<[%>."
10153 " Insert whitespace between %<<%> and %<::%>");
10154 if (!hint && !flag_permissive)
10156 inform (next_token->location, "(if you use %<-fpermissive%>"
10157 " G++ will accept your code)");
10164 /* Look for the `<' that starts the template-argument-list. */
10165 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10167 pop_deferring_access_checks ();
10168 return error_mark_node;
10170 /* Parse the arguments. */
10171 arguments = cp_parser_enclosed_template_argument_list (parser);
10174 /* Build a representation of the specialization. */
10175 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10176 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10177 else if (DECL_CLASS_TEMPLATE_P (templ)
10178 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10180 bool entering_scope;
10181 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10182 template (rather than some instantiation thereof) only if
10183 is not nested within some other construct. For example, in
10184 "template <typename T> void f(T) { A<T>::", A<T> is just an
10185 instantiation of A. */
10186 entering_scope = (template_parm_scope_p ()
10187 && cp_lexer_next_token_is (parser->lexer,
10190 = finish_template_type (templ, arguments, entering_scope);
10194 /* If it's not a class-template or a template-template, it should be
10195 a function-template. */
10196 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10197 || TREE_CODE (templ) == OVERLOAD
10198 || BASELINK_P (templ)));
10200 template_id = lookup_template_function (templ, arguments);
10203 /* If parsing tentatively, replace the sequence of tokens that makes
10204 up the template-id with a CPP_TEMPLATE_ID token. That way,
10205 should we re-parse the token stream, we will not have to repeat
10206 the effort required to do the parse, nor will we issue duplicate
10207 error messages about problems during instantiation of the
10211 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10213 /* Reset the contents of the START_OF_ID token. */
10214 token->type = CPP_TEMPLATE_ID;
10215 /* Retrieve any deferred checks. Do not pop this access checks yet
10216 so the memory will not be reclaimed during token replacing below. */
10217 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10218 token->u.tree_check_value->value = template_id;
10219 token->u.tree_check_value->checks = get_deferred_access_checks ();
10220 token->keyword = RID_MAX;
10222 /* Purge all subsequent tokens. */
10223 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10225 /* ??? Can we actually assume that, if template_id ==
10226 error_mark_node, we will have issued a diagnostic to the
10227 user, as opposed to simply marking the tentative parse as
10229 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10230 error ("%Hparse error in template argument list",
10234 pop_deferring_access_checks ();
10235 return template_id;
10238 /* Parse a template-name.
10243 The standard should actually say:
10247 operator-function-id
10249 A defect report has been filed about this issue.
10251 A conversion-function-id cannot be a template name because they cannot
10252 be part of a template-id. In fact, looking at this code:
10254 a.operator K<int>()
10256 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10257 It is impossible to call a templated conversion-function-id with an
10258 explicit argument list, since the only allowed template parameter is
10259 the type to which it is converting.
10261 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10262 `template' keyword, in a construction like:
10266 In that case `f' is taken to be a template-name, even though there
10267 is no way of knowing for sure.
10269 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10270 name refers to a set of overloaded functions, at least one of which
10271 is a template, or an IDENTIFIER_NODE with the name of the template,
10272 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10273 names are looked up inside uninstantiated templates. */
10276 cp_parser_template_name (cp_parser* parser,
10277 bool template_keyword_p,
10278 bool check_dependency_p,
10279 bool is_declaration,
10280 bool *is_identifier)
10285 cp_token *token = cp_lexer_peek_token (parser->lexer);
10287 /* If the next token is `operator', then we have either an
10288 operator-function-id or a conversion-function-id. */
10289 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10291 /* We don't know whether we're looking at an
10292 operator-function-id or a conversion-function-id. */
10293 cp_parser_parse_tentatively (parser);
10294 /* Try an operator-function-id. */
10295 identifier = cp_parser_operator_function_id (parser);
10296 /* If that didn't work, try a conversion-function-id. */
10297 if (!cp_parser_parse_definitely (parser))
10299 cp_parser_error (parser, "expected template-name");
10300 return error_mark_node;
10303 /* Look for the identifier. */
10305 identifier = cp_parser_identifier (parser);
10307 /* If we didn't find an identifier, we don't have a template-id. */
10308 if (identifier == error_mark_node)
10309 return error_mark_node;
10311 /* If the name immediately followed the `template' keyword, then it
10312 is a template-name. However, if the next token is not `<', then
10313 we do not treat it as a template-name, since it is not being used
10314 as part of a template-id. This enables us to handle constructs
10317 template <typename T> struct S { S(); };
10318 template <typename T> S<T>::S();
10320 correctly. We would treat `S' as a template -- if it were `S<T>'
10321 -- but we do not if there is no `<'. */
10323 if (processing_template_decl
10324 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10326 /* In a declaration, in a dependent context, we pretend that the
10327 "template" keyword was present in order to improve error
10328 recovery. For example, given:
10330 template <typename T> void f(T::X<int>);
10332 we want to treat "X<int>" as a template-id. */
10334 && !template_keyword_p
10335 && parser->scope && TYPE_P (parser->scope)
10336 && check_dependency_p
10337 && dependent_scope_p (parser->scope)
10338 /* Do not do this for dtors (or ctors), since they never
10339 need the template keyword before their name. */
10340 && !constructor_name_p (identifier, parser->scope))
10342 cp_token_position start = 0;
10344 /* Explain what went wrong. */
10345 error ("%Hnon-template %qD used as template",
10346 &token->location, identifier);
10347 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10348 parser->scope, identifier);
10349 /* If parsing tentatively, find the location of the "<" token. */
10350 if (cp_parser_simulate_error (parser))
10351 start = cp_lexer_token_position (parser->lexer, true);
10352 /* Parse the template arguments so that we can issue error
10353 messages about them. */
10354 cp_lexer_consume_token (parser->lexer);
10355 cp_parser_enclosed_template_argument_list (parser);
10356 /* Skip tokens until we find a good place from which to
10357 continue parsing. */
10358 cp_parser_skip_to_closing_parenthesis (parser,
10359 /*recovering=*/true,
10361 /*consume_paren=*/false);
10362 /* If parsing tentatively, permanently remove the
10363 template argument list. That will prevent duplicate
10364 error messages from being issued about the missing
10365 "template" keyword. */
10367 cp_lexer_purge_tokens_after (parser->lexer, start);
10369 *is_identifier = true;
10373 /* If the "template" keyword is present, then there is generally
10374 no point in doing name-lookup, so we just return IDENTIFIER.
10375 But, if the qualifying scope is non-dependent then we can
10376 (and must) do name-lookup normally. */
10377 if (template_keyword_p
10379 || (TYPE_P (parser->scope)
10380 && dependent_type_p (parser->scope))))
10384 /* Look up the name. */
10385 decl = cp_parser_lookup_name (parser, identifier,
10387 /*is_template=*/false,
10388 /*is_namespace=*/false,
10389 check_dependency_p,
10390 /*ambiguous_decls=*/NULL,
10392 decl = maybe_get_template_decl_from_type_decl (decl);
10394 /* If DECL is a template, then the name was a template-name. */
10395 if (TREE_CODE (decl) == TEMPLATE_DECL)
10399 tree fn = NULL_TREE;
10401 /* The standard does not explicitly indicate whether a name that
10402 names a set of overloaded declarations, some of which are
10403 templates, is a template-name. However, such a name should
10404 be a template-name; otherwise, there is no way to form a
10405 template-id for the overloaded templates. */
10406 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10407 if (TREE_CODE (fns) == OVERLOAD)
10408 for (fn = fns; fn; fn = OVL_NEXT (fn))
10409 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10414 /* The name does not name a template. */
10415 cp_parser_error (parser, "expected template-name");
10416 return error_mark_node;
10420 /* If DECL is dependent, and refers to a function, then just return
10421 its name; we will look it up again during template instantiation. */
10422 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10424 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10425 if (TYPE_P (scope) && dependent_type_p (scope))
10432 /* Parse a template-argument-list.
10434 template-argument-list:
10435 template-argument ... [opt]
10436 template-argument-list , template-argument ... [opt]
10438 Returns a TREE_VEC containing the arguments. */
10441 cp_parser_template_argument_list (cp_parser* parser)
10443 tree fixed_args[10];
10444 unsigned n_args = 0;
10445 unsigned alloced = 10;
10446 tree *arg_ary = fixed_args;
10448 bool saved_in_template_argument_list_p;
10450 bool saved_non_ice_p;
10452 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10453 parser->in_template_argument_list_p = true;
10454 /* Even if the template-id appears in an integral
10455 constant-expression, the contents of the argument list do
10457 saved_ice_p = parser->integral_constant_expression_p;
10458 parser->integral_constant_expression_p = false;
10459 saved_non_ice_p = parser->non_integral_constant_expression_p;
10460 parser->non_integral_constant_expression_p = false;
10461 /* Parse the arguments. */
10467 /* Consume the comma. */
10468 cp_lexer_consume_token (parser->lexer);
10470 /* Parse the template-argument. */
10471 argument = cp_parser_template_argument (parser);
10473 /* If the next token is an ellipsis, we're expanding a template
10475 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10477 /* Consume the `...' token. */
10478 cp_lexer_consume_token (parser->lexer);
10480 /* Make the argument into a TYPE_PACK_EXPANSION or
10481 EXPR_PACK_EXPANSION. */
10482 argument = make_pack_expansion (argument);
10485 if (n_args == alloced)
10489 if (arg_ary == fixed_args)
10491 arg_ary = XNEWVEC (tree, alloced);
10492 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10495 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10497 arg_ary[n_args++] = argument;
10499 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10501 vec = make_tree_vec (n_args);
10504 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10506 if (arg_ary != fixed_args)
10508 parser->non_integral_constant_expression_p = saved_non_ice_p;
10509 parser->integral_constant_expression_p = saved_ice_p;
10510 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10514 /* Parse a template-argument.
10517 assignment-expression
10521 The representation is that of an assignment-expression, type-id, or
10522 id-expression -- except that the qualified id-expression is
10523 evaluated, so that the value returned is either a DECL or an
10526 Although the standard says "assignment-expression", it forbids
10527 throw-expressions or assignments in the template argument.
10528 Therefore, we use "conditional-expression" instead. */
10531 cp_parser_template_argument (cp_parser* parser)
10536 bool maybe_type_id = false;
10537 cp_token *token = NULL, *argument_start_token = NULL;
10540 /* There's really no way to know what we're looking at, so we just
10541 try each alternative in order.
10545 In a template-argument, an ambiguity between a type-id and an
10546 expression is resolved to a type-id, regardless of the form of
10547 the corresponding template-parameter.
10549 Therefore, we try a type-id first. */
10550 cp_parser_parse_tentatively (parser);
10551 argument = cp_parser_template_type_arg (parser);
10552 /* If there was no error parsing the type-id but the next token is a
10553 '>>', our behavior depends on which dialect of C++ we're
10554 parsing. In C++98, we probably found a typo for '> >'. But there
10555 are type-id which are also valid expressions. For instance:
10557 struct X { int operator >> (int); };
10558 template <int V> struct Foo {};
10561 Here 'X()' is a valid type-id of a function type, but the user just
10562 wanted to write the expression "X() >> 5". Thus, we remember that we
10563 found a valid type-id, but we still try to parse the argument as an
10564 expression to see what happens.
10566 In C++0x, the '>>' will be considered two separate '>'
10568 if (!cp_parser_error_occurred (parser)
10569 && cxx_dialect == cxx98
10570 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10572 maybe_type_id = true;
10573 cp_parser_abort_tentative_parse (parser);
10577 /* If the next token isn't a `,' or a `>', then this argument wasn't
10578 really finished. This means that the argument is not a valid
10580 if (!cp_parser_next_token_ends_template_argument_p (parser))
10581 cp_parser_error (parser, "expected template-argument");
10582 /* If that worked, we're done. */
10583 if (cp_parser_parse_definitely (parser))
10586 /* We're still not sure what the argument will be. */
10587 cp_parser_parse_tentatively (parser);
10588 /* Try a template. */
10589 argument_start_token = cp_lexer_peek_token (parser->lexer);
10590 argument = cp_parser_id_expression (parser,
10591 /*template_keyword_p=*/false,
10592 /*check_dependency_p=*/true,
10594 /*declarator_p=*/false,
10595 /*optional_p=*/false);
10596 /* If the next token isn't a `,' or a `>', then this argument wasn't
10597 really finished. */
10598 if (!cp_parser_next_token_ends_template_argument_p (parser))
10599 cp_parser_error (parser, "expected template-argument");
10600 if (!cp_parser_error_occurred (parser))
10602 /* Figure out what is being referred to. If the id-expression
10603 was for a class template specialization, then we will have a
10604 TYPE_DECL at this point. There is no need to do name lookup
10605 at this point in that case. */
10606 if (TREE_CODE (argument) != TYPE_DECL)
10607 argument = cp_parser_lookup_name (parser, argument,
10609 /*is_template=*/template_p,
10610 /*is_namespace=*/false,
10611 /*check_dependency=*/true,
10612 /*ambiguous_decls=*/NULL,
10613 argument_start_token->location);
10614 if (TREE_CODE (argument) != TEMPLATE_DECL
10615 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10616 cp_parser_error (parser, "expected template-name");
10618 if (cp_parser_parse_definitely (parser))
10620 /* It must be a non-type argument. There permitted cases are given
10621 in [temp.arg.nontype]:
10623 -- an integral constant-expression of integral or enumeration
10626 -- the name of a non-type template-parameter; or
10628 -- the name of an object or function with external linkage...
10630 -- the address of an object or function with external linkage...
10632 -- a pointer to member... */
10633 /* Look for a non-type template parameter. */
10634 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10636 cp_parser_parse_tentatively (parser);
10637 argument = cp_parser_primary_expression (parser,
10638 /*address_p=*/false,
10640 /*template_arg_p=*/true,
10642 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10643 || !cp_parser_next_token_ends_template_argument_p (parser))
10644 cp_parser_simulate_error (parser);
10645 if (cp_parser_parse_definitely (parser))
10649 /* If the next token is "&", the argument must be the address of an
10650 object or function with external linkage. */
10651 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10653 cp_lexer_consume_token (parser->lexer);
10654 /* See if we might have an id-expression. */
10655 token = cp_lexer_peek_token (parser->lexer);
10656 if (token->type == CPP_NAME
10657 || token->keyword == RID_OPERATOR
10658 || token->type == CPP_SCOPE
10659 || token->type == CPP_TEMPLATE_ID
10660 || token->type == CPP_NESTED_NAME_SPECIFIER)
10662 cp_parser_parse_tentatively (parser);
10663 argument = cp_parser_primary_expression (parser,
10666 /*template_arg_p=*/true,
10668 if (cp_parser_error_occurred (parser)
10669 || !cp_parser_next_token_ends_template_argument_p (parser))
10670 cp_parser_abort_tentative_parse (parser);
10673 if (TREE_CODE (argument) == INDIRECT_REF)
10675 gcc_assert (REFERENCE_REF_P (argument));
10676 argument = TREE_OPERAND (argument, 0);
10679 if (TREE_CODE (argument) == VAR_DECL)
10681 /* A variable without external linkage might still be a
10682 valid constant-expression, so no error is issued here
10683 if the external-linkage check fails. */
10684 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10685 cp_parser_simulate_error (parser);
10687 else if (is_overloaded_fn (argument))
10688 /* All overloaded functions are allowed; if the external
10689 linkage test does not pass, an error will be issued
10693 && (TREE_CODE (argument) == OFFSET_REF
10694 || TREE_CODE (argument) == SCOPE_REF))
10695 /* A pointer-to-member. */
10697 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10700 cp_parser_simulate_error (parser);
10702 if (cp_parser_parse_definitely (parser))
10705 argument = build_x_unary_op (ADDR_EXPR, argument,
10706 tf_warning_or_error);
10711 /* If the argument started with "&", there are no other valid
10712 alternatives at this point. */
10715 cp_parser_error (parser, "invalid non-type template argument");
10716 return error_mark_node;
10719 /* If the argument wasn't successfully parsed as a type-id followed
10720 by '>>', the argument can only be a constant expression now.
10721 Otherwise, we try parsing the constant-expression tentatively,
10722 because the argument could really be a type-id. */
10724 cp_parser_parse_tentatively (parser);
10725 argument = cp_parser_constant_expression (parser,
10726 /*allow_non_constant_p=*/false,
10727 /*non_constant_p=*/NULL);
10728 argument = fold_non_dependent_expr (argument);
10729 if (!maybe_type_id)
10731 if (!cp_parser_next_token_ends_template_argument_p (parser))
10732 cp_parser_error (parser, "expected template-argument");
10733 if (cp_parser_parse_definitely (parser))
10735 /* We did our best to parse the argument as a non type-id, but that
10736 was the only alternative that matched (albeit with a '>' after
10737 it). We can assume it's just a typo from the user, and a
10738 diagnostic will then be issued. */
10739 return cp_parser_template_type_arg (parser);
10742 /* Parse an explicit-instantiation.
10744 explicit-instantiation:
10745 template declaration
10747 Although the standard says `declaration', what it really means is:
10749 explicit-instantiation:
10750 template decl-specifier-seq [opt] declarator [opt] ;
10752 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10753 supposed to be allowed. A defect report has been filed about this
10758 explicit-instantiation:
10759 storage-class-specifier template
10760 decl-specifier-seq [opt] declarator [opt] ;
10761 function-specifier template
10762 decl-specifier-seq [opt] declarator [opt] ; */
10765 cp_parser_explicit_instantiation (cp_parser* parser)
10767 int declares_class_or_enum;
10768 cp_decl_specifier_seq decl_specifiers;
10769 tree extension_specifier = NULL_TREE;
10772 /* Look for an (optional) storage-class-specifier or
10773 function-specifier. */
10774 if (cp_parser_allow_gnu_extensions_p (parser))
10776 extension_specifier
10777 = cp_parser_storage_class_specifier_opt (parser);
10778 if (!extension_specifier)
10779 extension_specifier
10780 = cp_parser_function_specifier_opt (parser,
10781 /*decl_specs=*/NULL);
10784 /* Look for the `template' keyword. */
10785 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10786 /* Let the front end know that we are processing an explicit
10788 begin_explicit_instantiation ();
10789 /* [temp.explicit] says that we are supposed to ignore access
10790 control while processing explicit instantiation directives. */
10791 push_deferring_access_checks (dk_no_check);
10792 /* Parse a decl-specifier-seq. */
10793 token = cp_lexer_peek_token (parser->lexer);
10794 cp_parser_decl_specifier_seq (parser,
10795 CP_PARSER_FLAGS_OPTIONAL,
10797 &declares_class_or_enum);
10798 /* If there was exactly one decl-specifier, and it declared a class,
10799 and there's no declarator, then we have an explicit type
10801 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10805 type = check_tag_decl (&decl_specifiers);
10806 /* Turn access control back on for names used during
10807 template instantiation. */
10808 pop_deferring_access_checks ();
10810 do_type_instantiation (type, extension_specifier,
10811 /*complain=*/tf_error);
10815 cp_declarator *declarator;
10818 /* Parse the declarator. */
10820 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10821 /*ctor_dtor_or_conv_p=*/NULL,
10822 /*parenthesized_p=*/NULL,
10823 /*member_p=*/false);
10824 if (declares_class_or_enum & 2)
10825 cp_parser_check_for_definition_in_return_type (declarator,
10826 decl_specifiers.type,
10827 decl_specifiers.type_location);
10828 if (declarator != cp_error_declarator)
10830 decl = grokdeclarator (declarator, &decl_specifiers,
10831 NORMAL, 0, &decl_specifiers.attributes);
10832 /* Turn access control back on for names used during
10833 template instantiation. */
10834 pop_deferring_access_checks ();
10835 /* Do the explicit instantiation. */
10836 do_decl_instantiation (decl, extension_specifier);
10840 pop_deferring_access_checks ();
10841 /* Skip the body of the explicit instantiation. */
10842 cp_parser_skip_to_end_of_statement (parser);
10845 /* We're done with the instantiation. */
10846 end_explicit_instantiation ();
10848 cp_parser_consume_semicolon_at_end_of_statement (parser);
10851 /* Parse an explicit-specialization.
10853 explicit-specialization:
10854 template < > declaration
10856 Although the standard says `declaration', what it really means is:
10858 explicit-specialization:
10859 template <> decl-specifier [opt] init-declarator [opt] ;
10860 template <> function-definition
10861 template <> explicit-specialization
10862 template <> template-declaration */
10865 cp_parser_explicit_specialization (cp_parser* parser)
10867 bool need_lang_pop;
10868 cp_token *token = cp_lexer_peek_token (parser->lexer);
10870 /* Look for the `template' keyword. */
10871 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10872 /* Look for the `<'. */
10873 cp_parser_require (parser, CPP_LESS, "%<<%>");
10874 /* Look for the `>'. */
10875 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10876 /* We have processed another parameter list. */
10877 ++parser->num_template_parameter_lists;
10880 A template ... explicit specialization ... shall not have C
10882 if (current_lang_name == lang_name_c)
10884 error ("%Htemplate specialization with C linkage", &token->location);
10885 /* Give it C++ linkage to avoid confusing other parts of the
10887 push_lang_context (lang_name_cplusplus);
10888 need_lang_pop = true;
10891 need_lang_pop = false;
10892 /* Let the front end know that we are beginning a specialization. */
10893 if (!begin_specialization ())
10895 end_specialization ();
10899 /* If the next keyword is `template', we need to figure out whether
10900 or not we're looking a template-declaration. */
10901 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10903 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10904 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10905 cp_parser_template_declaration_after_export (parser,
10906 /*member_p=*/false);
10908 cp_parser_explicit_specialization (parser);
10911 /* Parse the dependent declaration. */
10912 cp_parser_single_declaration (parser,
10914 /*member_p=*/false,
10915 /*explicit_specialization_p=*/true,
10916 /*friend_p=*/NULL);
10917 /* We're done with the specialization. */
10918 end_specialization ();
10919 /* For the erroneous case of a template with C linkage, we pushed an
10920 implicit C++ linkage scope; exit that scope now. */
10922 pop_lang_context ();
10923 /* We're done with this parameter list. */
10924 --parser->num_template_parameter_lists;
10927 /* Parse a type-specifier.
10930 simple-type-specifier
10933 elaborated-type-specifier
10941 Returns a representation of the type-specifier. For a
10942 class-specifier, enum-specifier, or elaborated-type-specifier, a
10943 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10945 The parser flags FLAGS is used to control type-specifier parsing.
10947 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10948 in a decl-specifier-seq.
10950 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10951 class-specifier, enum-specifier, or elaborated-type-specifier, then
10952 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10953 if a type is declared; 2 if it is defined. Otherwise, it is set to
10956 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10957 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10958 is set to FALSE. */
10961 cp_parser_type_specifier (cp_parser* parser,
10962 cp_parser_flags flags,
10963 cp_decl_specifier_seq *decl_specs,
10964 bool is_declaration,
10965 int* declares_class_or_enum,
10966 bool* is_cv_qualifier)
10968 tree type_spec = NULL_TREE;
10971 cp_decl_spec ds = ds_last;
10973 /* Assume this type-specifier does not declare a new type. */
10974 if (declares_class_or_enum)
10975 *declares_class_or_enum = 0;
10976 /* And that it does not specify a cv-qualifier. */
10977 if (is_cv_qualifier)
10978 *is_cv_qualifier = false;
10979 /* Peek at the next token. */
10980 token = cp_lexer_peek_token (parser->lexer);
10982 /* If we're looking at a keyword, we can use that to guide the
10983 production we choose. */
10984 keyword = token->keyword;
10988 /* Look for the enum-specifier. */
10989 type_spec = cp_parser_enum_specifier (parser);
10990 /* If that worked, we're done. */
10993 if (declares_class_or_enum)
10994 *declares_class_or_enum = 2;
10996 cp_parser_set_decl_spec_type (decl_specs,
10999 /*user_defined_p=*/true);
11003 goto elaborated_type_specifier;
11005 /* Any of these indicate either a class-specifier, or an
11006 elaborated-type-specifier. */
11010 /* Parse tentatively so that we can back up if we don't find a
11011 class-specifier. */
11012 cp_parser_parse_tentatively (parser);
11013 /* Look for the class-specifier. */
11014 type_spec = cp_parser_class_specifier (parser);
11015 /* If that worked, we're done. */
11016 if (cp_parser_parse_definitely (parser))
11018 if (declares_class_or_enum)
11019 *declares_class_or_enum = 2;
11021 cp_parser_set_decl_spec_type (decl_specs,
11024 /*user_defined_p=*/true);
11028 /* Fall through. */
11029 elaborated_type_specifier:
11030 /* We're declaring (not defining) a class or enum. */
11031 if (declares_class_or_enum)
11032 *declares_class_or_enum = 1;
11034 /* Fall through. */
11036 /* Look for an elaborated-type-specifier. */
11038 = (cp_parser_elaborated_type_specifier
11040 decl_specs && decl_specs->specs[(int) ds_friend],
11043 cp_parser_set_decl_spec_type (decl_specs,
11046 /*user_defined_p=*/true);
11051 if (is_cv_qualifier)
11052 *is_cv_qualifier = true;
11057 if (is_cv_qualifier)
11058 *is_cv_qualifier = true;
11063 if (is_cv_qualifier)
11064 *is_cv_qualifier = true;
11068 /* The `__complex__' keyword is a GNU extension. */
11076 /* Handle simple keywords. */
11081 ++decl_specs->specs[(int)ds];
11082 decl_specs->any_specifiers_p = true;
11084 return cp_lexer_consume_token (parser->lexer)->u.value;
11087 /* If we do not already have a type-specifier, assume we are looking
11088 at a simple-type-specifier. */
11089 type_spec = cp_parser_simple_type_specifier (parser,
11093 /* If we didn't find a type-specifier, and a type-specifier was not
11094 optional in this context, issue an error message. */
11095 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11097 cp_parser_error (parser, "expected type specifier");
11098 return error_mark_node;
11104 /* Parse a simple-type-specifier.
11106 simple-type-specifier:
11107 :: [opt] nested-name-specifier [opt] type-name
11108 :: [opt] nested-name-specifier template template-id
11123 simple-type-specifier:
11125 decltype ( expression )
11131 simple-type-specifier:
11132 __typeof__ unary-expression
11133 __typeof__ ( type-id )
11135 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11136 appropriately updated. */
11139 cp_parser_simple_type_specifier (cp_parser* parser,
11140 cp_decl_specifier_seq *decl_specs,
11141 cp_parser_flags flags)
11143 tree type = NULL_TREE;
11146 /* Peek at the next token. */
11147 token = cp_lexer_peek_token (parser->lexer);
11149 /* If we're looking at a keyword, things are easy. */
11150 switch (token->keyword)
11154 decl_specs->explicit_char_p = true;
11155 type = char_type_node;
11158 type = char16_type_node;
11161 type = char32_type_node;
11164 type = wchar_type_node;
11167 type = boolean_type_node;
11171 ++decl_specs->specs[(int) ds_short];
11172 type = short_integer_type_node;
11176 decl_specs->explicit_int_p = true;
11177 type = integer_type_node;
11181 ++decl_specs->specs[(int) ds_long];
11182 type = long_integer_type_node;
11186 ++decl_specs->specs[(int) ds_signed];
11187 type = integer_type_node;
11191 ++decl_specs->specs[(int) ds_unsigned];
11192 type = unsigned_type_node;
11195 type = float_type_node;
11198 type = double_type_node;
11201 type = void_type_node;
11205 maybe_warn_cpp0x ("C++0x auto");
11206 type = make_auto ();
11210 /* Parse the `decltype' type. */
11211 type = cp_parser_decltype (parser);
11214 cp_parser_set_decl_spec_type (decl_specs, type,
11216 /*user_defined_p=*/true);
11221 /* Consume the `typeof' token. */
11222 cp_lexer_consume_token (parser->lexer);
11223 /* Parse the operand to `typeof'. */
11224 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11225 /* If it is not already a TYPE, take its type. */
11226 if (!TYPE_P (type))
11227 type = finish_typeof (type);
11230 cp_parser_set_decl_spec_type (decl_specs, type,
11232 /*user_defined_p=*/true);
11240 /* If the type-specifier was for a built-in type, we're done. */
11245 /* Record the type. */
11247 && (token->keyword != RID_SIGNED
11248 && token->keyword != RID_UNSIGNED
11249 && token->keyword != RID_SHORT
11250 && token->keyword != RID_LONG))
11251 cp_parser_set_decl_spec_type (decl_specs,
11254 /*user_defined=*/false);
11256 decl_specs->any_specifiers_p = true;
11258 /* Consume the token. */
11259 id = cp_lexer_consume_token (parser->lexer)->u.value;
11261 /* There is no valid C++ program where a non-template type is
11262 followed by a "<". That usually indicates that the user thought
11263 that the type was a template. */
11264 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11266 return TYPE_NAME (type);
11269 /* The type-specifier must be a user-defined type. */
11270 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11275 /* Don't gobble tokens or issue error messages if this is an
11276 optional type-specifier. */
11277 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11278 cp_parser_parse_tentatively (parser);
11280 /* Look for the optional `::' operator. */
11282 = (cp_parser_global_scope_opt (parser,
11283 /*current_scope_valid_p=*/false)
11285 /* Look for the nested-name specifier. */
11287 = (cp_parser_nested_name_specifier_opt (parser,
11288 /*typename_keyword_p=*/false,
11289 /*check_dependency_p=*/true,
11291 /*is_declaration=*/false)
11293 token = cp_lexer_peek_token (parser->lexer);
11294 /* If we have seen a nested-name-specifier, and the next token
11295 is `template', then we are using the template-id production. */
11297 && cp_parser_optional_template_keyword (parser))
11299 /* Look for the template-id. */
11300 type = cp_parser_template_id (parser,
11301 /*template_keyword_p=*/true,
11302 /*check_dependency_p=*/true,
11303 /*is_declaration=*/false);
11304 /* If the template-id did not name a type, we are out of
11306 if (TREE_CODE (type) != TYPE_DECL)
11308 cp_parser_error (parser, "expected template-id for type");
11312 /* Otherwise, look for a type-name. */
11314 type = cp_parser_type_name (parser);
11315 /* Keep track of all name-lookups performed in class scopes. */
11319 && TREE_CODE (type) == TYPE_DECL
11320 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11321 maybe_note_name_used_in_class (DECL_NAME (type), type);
11322 /* If it didn't work out, we don't have a TYPE. */
11323 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11324 && !cp_parser_parse_definitely (parser))
11326 if (type && decl_specs)
11327 cp_parser_set_decl_spec_type (decl_specs, type,
11329 /*user_defined=*/true);
11332 /* If we didn't get a type-name, issue an error message. */
11333 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11335 cp_parser_error (parser, "expected type-name");
11336 return error_mark_node;
11339 /* There is no valid C++ program where a non-template type is
11340 followed by a "<". That usually indicates that the user thought
11341 that the type was a template. */
11342 if (type && type != error_mark_node)
11344 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11345 If it is, then the '<'...'>' enclose protocol names rather than
11346 template arguments, and so everything is fine. */
11347 if (c_dialect_objc ()
11348 && (objc_is_id (type) || objc_is_class_name (type)))
11350 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11351 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11353 /* Clobber the "unqualified" type previously entered into
11354 DECL_SPECS with the new, improved protocol-qualified version. */
11356 decl_specs->type = qual_type;
11361 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11368 /* Parse a type-name.
11381 Returns a TYPE_DECL for the type. */
11384 cp_parser_type_name (cp_parser* parser)
11388 /* We can't know yet whether it is a class-name or not. */
11389 cp_parser_parse_tentatively (parser);
11390 /* Try a class-name. */
11391 type_decl = cp_parser_class_name (parser,
11392 /*typename_keyword_p=*/false,
11393 /*template_keyword_p=*/false,
11395 /*check_dependency_p=*/true,
11396 /*class_head_p=*/false,
11397 /*is_declaration=*/false);
11398 /* If it's not a class-name, keep looking. */
11399 if (!cp_parser_parse_definitely (parser))
11401 /* It must be a typedef-name or an enum-name. */
11402 return cp_parser_nonclass_name (parser);
11408 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11416 Returns a TYPE_DECL for the type. */
11419 cp_parser_nonclass_name (cp_parser* parser)
11424 cp_token *token = cp_lexer_peek_token (parser->lexer);
11425 identifier = cp_parser_identifier (parser);
11426 if (identifier == error_mark_node)
11427 return error_mark_node;
11429 /* Look up the type-name. */
11430 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11432 if (TREE_CODE (type_decl) != TYPE_DECL
11433 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11435 /* See if this is an Objective-C type. */
11436 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11437 tree type = objc_get_protocol_qualified_type (identifier, protos);
11439 type_decl = TYPE_NAME (type);
11442 /* Issue an error if we did not find a type-name. */
11443 if (TREE_CODE (type_decl) != TYPE_DECL)
11445 if (!cp_parser_simulate_error (parser))
11446 cp_parser_name_lookup_error (parser, identifier, type_decl,
11447 "is not a type", token->location);
11448 return error_mark_node;
11450 /* Remember that the name was used in the definition of the
11451 current class so that we can check later to see if the
11452 meaning would have been different after the class was
11453 entirely defined. */
11454 else if (type_decl != error_mark_node
11456 maybe_note_name_used_in_class (identifier, type_decl);
11461 /* Parse an elaborated-type-specifier. Note that the grammar given
11462 here incorporates the resolution to DR68.
11464 elaborated-type-specifier:
11465 class-key :: [opt] nested-name-specifier [opt] identifier
11466 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11467 enum-key :: [opt] nested-name-specifier [opt] identifier
11468 typename :: [opt] nested-name-specifier identifier
11469 typename :: [opt] nested-name-specifier template [opt]
11474 elaborated-type-specifier:
11475 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11476 class-key attributes :: [opt] nested-name-specifier [opt]
11477 template [opt] template-id
11478 enum attributes :: [opt] nested-name-specifier [opt] identifier
11480 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11481 declared `friend'. If IS_DECLARATION is TRUE, then this
11482 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11483 something is being declared.
11485 Returns the TYPE specified. */
11488 cp_parser_elaborated_type_specifier (cp_parser* parser,
11490 bool is_declaration)
11492 enum tag_types tag_type;
11494 tree type = NULL_TREE;
11495 tree attributes = NULL_TREE;
11496 cp_token *token = NULL;
11498 /* See if we're looking at the `enum' keyword. */
11499 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11501 /* Consume the `enum' token. */
11502 cp_lexer_consume_token (parser->lexer);
11503 /* Remember that it's an enumeration type. */
11504 tag_type = enum_type;
11505 /* Parse the optional `struct' or `class' key (for C++0x scoped
11507 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11508 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11510 if (cxx_dialect == cxx98)
11511 maybe_warn_cpp0x ("scoped enums");
11513 /* Consume the `struct' or `class'. */
11514 cp_lexer_consume_token (parser->lexer);
11516 /* Parse the attributes. */
11517 attributes = cp_parser_attributes_opt (parser);
11519 /* Or, it might be `typename'. */
11520 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11523 /* Consume the `typename' token. */
11524 cp_lexer_consume_token (parser->lexer);
11525 /* Remember that it's a `typename' type. */
11526 tag_type = typename_type;
11527 /* The `typename' keyword is only allowed in templates. */
11528 if (!processing_template_decl)
11529 permerror (input_location, "using %<typename%> outside of template");
11531 /* Otherwise it must be a class-key. */
11534 tag_type = cp_parser_class_key (parser);
11535 if (tag_type == none_type)
11536 return error_mark_node;
11537 /* Parse the attributes. */
11538 attributes = cp_parser_attributes_opt (parser);
11541 /* Look for the `::' operator. */
11542 cp_parser_global_scope_opt (parser,
11543 /*current_scope_valid_p=*/false);
11544 /* Look for the nested-name-specifier. */
11545 if (tag_type == typename_type)
11547 if (!cp_parser_nested_name_specifier (parser,
11548 /*typename_keyword_p=*/true,
11549 /*check_dependency_p=*/true,
11552 return error_mark_node;
11555 /* Even though `typename' is not present, the proposed resolution
11556 to Core Issue 180 says that in `class A<T>::B', `B' should be
11557 considered a type-name, even if `A<T>' is dependent. */
11558 cp_parser_nested_name_specifier_opt (parser,
11559 /*typename_keyword_p=*/true,
11560 /*check_dependency_p=*/true,
11563 /* For everything but enumeration types, consider a template-id.
11564 For an enumeration type, consider only a plain identifier. */
11565 if (tag_type != enum_type)
11567 bool template_p = false;
11570 /* Allow the `template' keyword. */
11571 template_p = cp_parser_optional_template_keyword (parser);
11572 /* If we didn't see `template', we don't know if there's a
11573 template-id or not. */
11575 cp_parser_parse_tentatively (parser);
11576 /* Parse the template-id. */
11577 token = cp_lexer_peek_token (parser->lexer);
11578 decl = cp_parser_template_id (parser, template_p,
11579 /*check_dependency_p=*/true,
11581 /* If we didn't find a template-id, look for an ordinary
11583 if (!template_p && !cp_parser_parse_definitely (parser))
11585 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11586 in effect, then we must assume that, upon instantiation, the
11587 template will correspond to a class. */
11588 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11589 && tag_type == typename_type)
11590 type = make_typename_type (parser->scope, decl,
11592 /*complain=*/tf_error);
11593 /* If the `typename' keyword is in effect and DECL is not a type
11594 decl. Then type is non existant. */
11595 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11598 type = TREE_TYPE (decl);
11603 token = cp_lexer_peek_token (parser->lexer);
11604 identifier = cp_parser_identifier (parser);
11606 if (identifier == error_mark_node)
11608 parser->scope = NULL_TREE;
11609 return error_mark_node;
11612 /* For a `typename', we needn't call xref_tag. */
11613 if (tag_type == typename_type
11614 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11615 return cp_parser_make_typename_type (parser, parser->scope,
11618 /* Look up a qualified name in the usual way. */
11622 tree ambiguous_decls;
11624 decl = cp_parser_lookup_name (parser, identifier,
11626 /*is_template=*/false,
11627 /*is_namespace=*/false,
11628 /*check_dependency=*/true,
11632 /* If the lookup was ambiguous, an error will already have been
11634 if (ambiguous_decls)
11635 return error_mark_node;
11637 /* If we are parsing friend declaration, DECL may be a
11638 TEMPLATE_DECL tree node here. However, we need to check
11639 whether this TEMPLATE_DECL results in valid code. Consider
11640 the following example:
11643 template <class T> class C {};
11646 template <class T> friend class N::C; // #1, valid code
11648 template <class T> class Y {
11649 friend class N::C; // #2, invalid code
11652 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11653 name lookup of `N::C'. We see that friend declaration must
11654 be template for the code to be valid. Note that
11655 processing_template_decl does not work here since it is
11656 always 1 for the above two cases. */
11658 decl = (cp_parser_maybe_treat_template_as_class
11659 (decl, /*tag_name_p=*/is_friend
11660 && parser->num_template_parameter_lists));
11662 if (TREE_CODE (decl) != TYPE_DECL)
11664 cp_parser_diagnose_invalid_type_name (parser,
11668 return error_mark_node;
11671 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11673 bool allow_template = (parser->num_template_parameter_lists
11674 || DECL_SELF_REFERENCE_P (decl));
11675 type = check_elaborated_type_specifier (tag_type, decl,
11678 if (type == error_mark_node)
11679 return error_mark_node;
11682 /* Forward declarations of nested types, such as
11687 are invalid unless all components preceding the final '::'
11688 are complete. If all enclosing types are complete, these
11689 declarations become merely pointless.
11691 Invalid forward declarations of nested types are errors
11692 caught elsewhere in parsing. Those that are pointless arrive
11695 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11696 && !is_friend && !processing_explicit_instantiation)
11697 warning (0, "declaration %qD does not declare anything", decl);
11699 type = TREE_TYPE (decl);
11703 /* An elaborated-type-specifier sometimes introduces a new type and
11704 sometimes names an existing type. Normally, the rule is that it
11705 introduces a new type only if there is not an existing type of
11706 the same name already in scope. For example, given:
11709 void f() { struct S s; }
11711 the `struct S' in the body of `f' is the same `struct S' as in
11712 the global scope; the existing definition is used. However, if
11713 there were no global declaration, this would introduce a new
11714 local class named `S'.
11716 An exception to this rule applies to the following code:
11718 namespace N { struct S; }
11720 Here, the elaborated-type-specifier names a new type
11721 unconditionally; even if there is already an `S' in the
11722 containing scope this declaration names a new type.
11723 This exception only applies if the elaborated-type-specifier
11724 forms the complete declaration:
11728 A declaration consisting solely of `class-key identifier ;' is
11729 either a redeclaration of the name in the current scope or a
11730 forward declaration of the identifier as a class name. It
11731 introduces the name into the current scope.
11733 We are in this situation precisely when the next token is a `;'.
11735 An exception to the exception is that a `friend' declaration does
11736 *not* name a new type; i.e., given:
11738 struct S { friend struct T; };
11740 `T' is not a new type in the scope of `S'.
11742 Also, `new struct S' or `sizeof (struct S)' never results in the
11743 definition of a new type; a new type can only be declared in a
11744 declaration context. */
11750 /* Friends have special name lookup rules. */
11751 ts = ts_within_enclosing_non_class;
11752 else if (is_declaration
11753 && cp_lexer_next_token_is (parser->lexer,
11755 /* This is a `class-key identifier ;' */
11761 (parser->num_template_parameter_lists
11762 && (cp_parser_next_token_starts_class_definition_p (parser)
11763 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11764 /* An unqualified name was used to reference this type, so
11765 there were no qualifying templates. */
11766 if (!cp_parser_check_template_parameters (parser,
11767 /*num_templates=*/0,
11769 return error_mark_node;
11770 type = xref_tag (tag_type, identifier, ts, template_p);
11774 if (type == error_mark_node)
11775 return error_mark_node;
11777 /* Allow attributes on forward declarations of classes. */
11780 if (TREE_CODE (type) == TYPENAME_TYPE)
11781 warning (OPT_Wattributes,
11782 "attributes ignored on uninstantiated type");
11783 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11784 && ! processing_explicit_instantiation)
11785 warning (OPT_Wattributes,
11786 "attributes ignored on template instantiation");
11787 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11788 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11790 warning (OPT_Wattributes,
11791 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11794 if (tag_type != enum_type)
11795 cp_parser_check_class_key (tag_type, type);
11797 /* A "<" cannot follow an elaborated type specifier. If that
11798 happens, the user was probably trying to form a template-id. */
11799 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11804 /* Parse an enum-specifier.
11807 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11812 enum struct [C++0x]
11815 : type-specifier-seq
11818 enum-key attributes[opt] identifier [opt] enum-base [opt]
11819 { enumerator-list [opt] }attributes[opt]
11821 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11822 if the token stream isn't an enum-specifier after all. */
11825 cp_parser_enum_specifier (cp_parser* parser)
11830 bool scoped_enum_p = false;
11831 bool has_underlying_type = false;
11832 tree underlying_type = NULL_TREE;
11834 /* Parse tentatively so that we can back up if we don't find a
11836 cp_parser_parse_tentatively (parser);
11838 /* Caller guarantees that the current token is 'enum', an identifier
11839 possibly follows, and the token after that is an opening brace.
11840 If we don't have an identifier, fabricate an anonymous name for
11841 the enumeration being defined. */
11842 cp_lexer_consume_token (parser->lexer);
11844 /* Parse the "class" or "struct", which indicates a scoped
11845 enumeration type in C++0x. */
11846 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11847 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11849 if (cxx_dialect == cxx98)
11850 maybe_warn_cpp0x ("scoped enums");
11852 /* Consume the `struct' or `class' token. */
11853 cp_lexer_consume_token (parser->lexer);
11855 scoped_enum_p = true;
11858 attributes = cp_parser_attributes_opt (parser);
11860 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11861 identifier = cp_parser_identifier (parser);
11863 identifier = make_anon_name ();
11865 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11866 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11868 cp_decl_specifier_seq type_specifiers;
11870 /* At this point this is surely not elaborated type specifier. */
11871 if (!cp_parser_parse_definitely (parser))
11874 if (cxx_dialect == cxx98)
11875 maybe_warn_cpp0x ("scoped enums");
11877 /* Consume the `:'. */
11878 cp_lexer_consume_token (parser->lexer);
11880 has_underlying_type = true;
11882 /* Parse the type-specifier-seq. */
11883 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11886 /* If that didn't work, stop. */
11887 if (type_specifiers.type != error_mark_node)
11889 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11890 /*initialized=*/0, NULL);
11891 if (underlying_type == error_mark_node)
11892 underlying_type = NULL_TREE;
11896 /* Look for the `{' but don't consume it yet. */
11897 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11899 cp_parser_error (parser, "expected %<{%>");
11900 if (has_underlying_type)
11904 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11907 /* Issue an error message if type-definitions are forbidden here. */
11908 if (!cp_parser_check_type_definition (parser))
11909 type = error_mark_node;
11911 /* Create the new type. We do this before consuming the opening
11912 brace so the enum will be recorded as being on the line of its
11913 tag (or the 'enum' keyword, if there is no tag). */
11914 type = start_enum (identifier, underlying_type, scoped_enum_p);
11916 /* Consume the opening brace. */
11917 cp_lexer_consume_token (parser->lexer);
11919 if (type == error_mark_node)
11921 cp_parser_skip_to_end_of_block_or_statement (parser);
11922 return error_mark_node;
11925 /* If the next token is not '}', then there are some enumerators. */
11926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11927 cp_parser_enumerator_list (parser, type);
11929 /* Consume the final '}'. */
11930 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11932 /* Look for trailing attributes to apply to this enumeration, and
11933 apply them if appropriate. */
11934 if (cp_parser_allow_gnu_extensions_p (parser))
11936 tree trailing_attr = cp_parser_attributes_opt (parser);
11937 trailing_attr = chainon (trailing_attr, attributes);
11938 cplus_decl_attributes (&type,
11940 (int) ATTR_FLAG_TYPE_IN_PLACE);
11943 /* Finish up the enumeration. */
11944 finish_enum (type);
11949 /* Parse an enumerator-list. The enumerators all have the indicated
11953 enumerator-definition
11954 enumerator-list , enumerator-definition */
11957 cp_parser_enumerator_list (cp_parser* parser, tree type)
11961 /* Parse an enumerator-definition. */
11962 cp_parser_enumerator_definition (parser, type);
11964 /* If the next token is not a ',', we've reached the end of
11966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11968 /* Otherwise, consume the `,' and keep going. */
11969 cp_lexer_consume_token (parser->lexer);
11970 /* If the next token is a `}', there is a trailing comma. */
11971 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11973 if (!in_system_header)
11974 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11980 /* Parse an enumerator-definition. The enumerator has the indicated
11983 enumerator-definition:
11985 enumerator = constant-expression
11991 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11996 /* Look for the identifier. */
11997 identifier = cp_parser_identifier (parser);
11998 if (identifier == error_mark_node)
12001 /* If the next token is an '=', then there is an explicit value. */
12002 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12004 /* Consume the `=' token. */
12005 cp_lexer_consume_token (parser->lexer);
12006 /* Parse the value. */
12007 value = cp_parser_constant_expression (parser,
12008 /*allow_non_constant_p=*/false,
12014 /* Create the enumerator. */
12015 build_enumerator (identifier, value, type);
12018 /* Parse a namespace-name.
12021 original-namespace-name
12024 Returns the NAMESPACE_DECL for the namespace. */
12027 cp_parser_namespace_name (cp_parser* parser)
12030 tree namespace_decl;
12032 cp_token *token = cp_lexer_peek_token (parser->lexer);
12034 /* Get the name of the namespace. */
12035 identifier = cp_parser_identifier (parser);
12036 if (identifier == error_mark_node)
12037 return error_mark_node;
12039 /* Look up the identifier in the currently active scope. Look only
12040 for namespaces, due to:
12042 [basic.lookup.udir]
12044 When looking up a namespace-name in a using-directive or alias
12045 definition, only namespace names are considered.
12049 [basic.lookup.qual]
12051 During the lookup of a name preceding the :: scope resolution
12052 operator, object, function, and enumerator names are ignored.
12054 (Note that cp_parser_qualifying_entity only calls this
12055 function if the token after the name is the scope resolution
12057 namespace_decl = cp_parser_lookup_name (parser, identifier,
12059 /*is_template=*/false,
12060 /*is_namespace=*/true,
12061 /*check_dependency=*/true,
12062 /*ambiguous_decls=*/NULL,
12064 /* If it's not a namespace, issue an error. */
12065 if (namespace_decl == error_mark_node
12066 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12068 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12069 error ("%H%qD is not a namespace-name", &token->location, identifier);
12070 cp_parser_error (parser, "expected namespace-name");
12071 namespace_decl = error_mark_node;
12074 return namespace_decl;
12077 /* Parse a namespace-definition.
12079 namespace-definition:
12080 named-namespace-definition
12081 unnamed-namespace-definition
12083 named-namespace-definition:
12084 original-namespace-definition
12085 extension-namespace-definition
12087 original-namespace-definition:
12088 namespace identifier { namespace-body }
12090 extension-namespace-definition:
12091 namespace original-namespace-name { namespace-body }
12093 unnamed-namespace-definition:
12094 namespace { namespace-body } */
12097 cp_parser_namespace_definition (cp_parser* parser)
12099 tree identifier, attribs;
12100 bool has_visibility;
12103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12106 cp_lexer_consume_token (parser->lexer);
12111 /* Look for the `namespace' keyword. */
12112 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12114 /* Get the name of the namespace. We do not attempt to distinguish
12115 between an original-namespace-definition and an
12116 extension-namespace-definition at this point. The semantic
12117 analysis routines are responsible for that. */
12118 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12119 identifier = cp_parser_identifier (parser);
12121 identifier = NULL_TREE;
12123 /* Parse any specified attributes. */
12124 attribs = cp_parser_attributes_opt (parser);
12126 /* Look for the `{' to start the namespace. */
12127 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12128 /* Start the namespace. */
12129 push_namespace (identifier);
12131 /* "inline namespace" is equivalent to a stub namespace definition
12132 followed by a strong using directive. */
12135 tree name_space = current_namespace;
12136 /* Set up namespace association. */
12137 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12138 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12139 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12140 /* Import the contents of the inline namespace. */
12142 do_using_directive (name_space);
12143 push_namespace (identifier);
12146 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12148 /* Parse the body of the namespace. */
12149 cp_parser_namespace_body (parser);
12151 #ifdef HANDLE_PRAGMA_VISIBILITY
12152 if (has_visibility)
12156 /* Finish the namespace. */
12158 /* Look for the final `}'. */
12159 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12162 /* Parse a namespace-body.
12165 declaration-seq [opt] */
12168 cp_parser_namespace_body (cp_parser* parser)
12170 cp_parser_declaration_seq_opt (parser);
12173 /* Parse a namespace-alias-definition.
12175 namespace-alias-definition:
12176 namespace identifier = qualified-namespace-specifier ; */
12179 cp_parser_namespace_alias_definition (cp_parser* parser)
12182 tree namespace_specifier;
12184 cp_token *token = cp_lexer_peek_token (parser->lexer);
12186 /* Look for the `namespace' keyword. */
12187 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12188 /* Look for the identifier. */
12189 identifier = cp_parser_identifier (parser);
12190 if (identifier == error_mark_node)
12192 /* Look for the `=' token. */
12193 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12194 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12196 error ("%H%<namespace%> definition is not allowed here", &token->location);
12197 /* Skip the definition. */
12198 cp_lexer_consume_token (parser->lexer);
12199 if (cp_parser_skip_to_closing_brace (parser))
12200 cp_lexer_consume_token (parser->lexer);
12203 cp_parser_require (parser, CPP_EQ, "%<=%>");
12204 /* Look for the qualified-namespace-specifier. */
12205 namespace_specifier
12206 = cp_parser_qualified_namespace_specifier (parser);
12207 /* Look for the `;' token. */
12208 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12210 /* Register the alias in the symbol table. */
12211 do_namespace_alias (identifier, namespace_specifier);
12214 /* Parse a qualified-namespace-specifier.
12216 qualified-namespace-specifier:
12217 :: [opt] nested-name-specifier [opt] namespace-name
12219 Returns a NAMESPACE_DECL corresponding to the specified
12223 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12225 /* Look for the optional `::'. */
12226 cp_parser_global_scope_opt (parser,
12227 /*current_scope_valid_p=*/false);
12229 /* Look for the optional nested-name-specifier. */
12230 cp_parser_nested_name_specifier_opt (parser,
12231 /*typename_keyword_p=*/false,
12232 /*check_dependency_p=*/true,
12234 /*is_declaration=*/true);
12236 return cp_parser_namespace_name (parser);
12239 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12240 access declaration.
12243 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12244 using :: unqualified-id ;
12246 access-declaration:
12252 cp_parser_using_declaration (cp_parser* parser,
12253 bool access_declaration_p)
12256 bool typename_p = false;
12257 bool global_scope_p;
12262 if (access_declaration_p)
12263 cp_parser_parse_tentatively (parser);
12266 /* Look for the `using' keyword. */
12267 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12269 /* Peek at the next token. */
12270 token = cp_lexer_peek_token (parser->lexer);
12271 /* See if it's `typename'. */
12272 if (token->keyword == RID_TYPENAME)
12274 /* Remember that we've seen it. */
12276 /* Consume the `typename' token. */
12277 cp_lexer_consume_token (parser->lexer);
12281 /* Look for the optional global scope qualification. */
12283 = (cp_parser_global_scope_opt (parser,
12284 /*current_scope_valid_p=*/false)
12287 /* If we saw `typename', or didn't see `::', then there must be a
12288 nested-name-specifier present. */
12289 if (typename_p || !global_scope_p)
12290 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12291 /*check_dependency_p=*/true,
12293 /*is_declaration=*/true);
12294 /* Otherwise, we could be in either of the two productions. In that
12295 case, treat the nested-name-specifier as optional. */
12297 qscope = cp_parser_nested_name_specifier_opt (parser,
12298 /*typename_keyword_p=*/false,
12299 /*check_dependency_p=*/true,
12301 /*is_declaration=*/true);
12303 qscope = global_namespace;
12305 if (access_declaration_p && cp_parser_error_occurred (parser))
12306 /* Something has already gone wrong; there's no need to parse
12307 further. Since an error has occurred, the return value of
12308 cp_parser_parse_definitely will be false, as required. */
12309 return cp_parser_parse_definitely (parser);
12311 token = cp_lexer_peek_token (parser->lexer);
12312 /* Parse the unqualified-id. */
12313 identifier = cp_parser_unqualified_id (parser,
12314 /*template_keyword_p=*/false,
12315 /*check_dependency_p=*/true,
12316 /*declarator_p=*/true,
12317 /*optional_p=*/false);
12319 if (access_declaration_p)
12321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12322 cp_parser_simulate_error (parser);
12323 if (!cp_parser_parse_definitely (parser))
12327 /* The function we call to handle a using-declaration is different
12328 depending on what scope we are in. */
12329 if (qscope == error_mark_node || identifier == error_mark_node)
12331 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12332 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12333 /* [namespace.udecl]
12335 A using declaration shall not name a template-id. */
12336 error ("%Ha template-id may not appear in a using-declaration",
12340 if (at_class_scope_p ())
12342 /* Create the USING_DECL. */
12343 decl = do_class_using_decl (parser->scope, identifier);
12345 if (check_for_bare_parameter_packs (decl))
12348 /* Add it to the list of members in this class. */
12349 finish_member_declaration (decl);
12353 decl = cp_parser_lookup_name_simple (parser,
12356 if (decl == error_mark_node)
12357 cp_parser_name_lookup_error (parser, identifier,
12360 else if (check_for_bare_parameter_packs (decl))
12362 else if (!at_namespace_scope_p ())
12363 do_local_using_decl (decl, qscope, identifier);
12365 do_toplevel_using_decl (decl, qscope, identifier);
12369 /* Look for the final `;'. */
12370 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12375 /* Parse a using-directive.
12378 using namespace :: [opt] nested-name-specifier [opt]
12379 namespace-name ; */
12382 cp_parser_using_directive (cp_parser* parser)
12384 tree namespace_decl;
12387 /* Look for the `using' keyword. */
12388 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12389 /* And the `namespace' keyword. */
12390 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12391 /* Look for the optional `::' operator. */
12392 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12393 /* And the optional nested-name-specifier. */
12394 cp_parser_nested_name_specifier_opt (parser,
12395 /*typename_keyword_p=*/false,
12396 /*check_dependency_p=*/true,
12398 /*is_declaration=*/true);
12399 /* Get the namespace being used. */
12400 namespace_decl = cp_parser_namespace_name (parser);
12401 /* And any specified attributes. */
12402 attribs = cp_parser_attributes_opt (parser);
12403 /* Update the symbol table. */
12404 parse_using_directive (namespace_decl, attribs);
12405 /* Look for the final `;'. */
12406 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12409 /* Parse an asm-definition.
12412 asm ( string-literal ) ;
12417 asm volatile [opt] ( string-literal ) ;
12418 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12419 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12420 : asm-operand-list [opt] ) ;
12421 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12422 : asm-operand-list [opt]
12423 : asm-operand-list [opt] ) ; */
12426 cp_parser_asm_definition (cp_parser* parser)
12429 tree outputs = NULL_TREE;
12430 tree inputs = NULL_TREE;
12431 tree clobbers = NULL_TREE;
12433 bool volatile_p = false;
12434 bool extended_p = false;
12435 bool invalid_inputs_p = false;
12436 bool invalid_outputs_p = false;
12438 /* Look for the `asm' keyword. */
12439 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12440 /* See if the next token is `volatile'. */
12441 if (cp_parser_allow_gnu_extensions_p (parser)
12442 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12444 /* Remember that we saw the `volatile' keyword. */
12446 /* Consume the token. */
12447 cp_lexer_consume_token (parser->lexer);
12449 /* Look for the opening `('. */
12450 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12452 /* Look for the string. */
12453 string = cp_parser_string_literal (parser, false, false);
12454 if (string == error_mark_node)
12456 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12457 /*consume_paren=*/true);
12461 /* If we're allowing GNU extensions, check for the extended assembly
12462 syntax. Unfortunately, the `:' tokens need not be separated by
12463 a space in C, and so, for compatibility, we tolerate that here
12464 too. Doing that means that we have to treat the `::' operator as
12466 if (cp_parser_allow_gnu_extensions_p (parser)
12467 && parser->in_function_body
12468 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12469 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12471 bool inputs_p = false;
12472 bool clobbers_p = false;
12474 /* The extended syntax was used. */
12477 /* Look for outputs. */
12478 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12480 /* Consume the `:'. */
12481 cp_lexer_consume_token (parser->lexer);
12482 /* Parse the output-operands. */
12483 if (cp_lexer_next_token_is_not (parser->lexer,
12485 && cp_lexer_next_token_is_not (parser->lexer,
12487 && cp_lexer_next_token_is_not (parser->lexer,
12489 outputs = cp_parser_asm_operand_list (parser);
12491 if (outputs == error_mark_node)
12492 invalid_outputs_p = true;
12494 /* If the next token is `::', there are no outputs, and the
12495 next token is the beginning of the inputs. */
12496 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12497 /* The inputs are coming next. */
12500 /* Look for inputs. */
12502 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12504 /* Consume the `:' or `::'. */
12505 cp_lexer_consume_token (parser->lexer);
12506 /* Parse the output-operands. */
12507 if (cp_lexer_next_token_is_not (parser->lexer,
12509 && cp_lexer_next_token_is_not (parser->lexer,
12511 inputs = cp_parser_asm_operand_list (parser);
12513 if (inputs == error_mark_node)
12514 invalid_inputs_p = true;
12516 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12517 /* The clobbers are coming next. */
12520 /* Look for clobbers. */
12522 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12524 /* Consume the `:' or `::'. */
12525 cp_lexer_consume_token (parser->lexer);
12526 /* Parse the clobbers. */
12527 if (cp_lexer_next_token_is_not (parser->lexer,
12529 clobbers = cp_parser_asm_clobber_list (parser);
12532 /* Look for the closing `)'. */
12533 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12534 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12535 /*consume_paren=*/true);
12536 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12538 if (!invalid_inputs_p && !invalid_outputs_p)
12540 /* Create the ASM_EXPR. */
12541 if (parser->in_function_body)
12543 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12545 /* If the extended syntax was not used, mark the ASM_EXPR. */
12548 tree temp = asm_stmt;
12549 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12550 temp = TREE_OPERAND (temp, 0);
12552 ASM_INPUT_P (temp) = 1;
12556 cgraph_add_asm_node (string);
12560 /* Declarators [gram.dcl.decl] */
12562 /* Parse an init-declarator.
12565 declarator initializer [opt]
12570 declarator asm-specification [opt] attributes [opt] initializer [opt]
12572 function-definition:
12573 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12575 decl-specifier-seq [opt] declarator function-try-block
12579 function-definition:
12580 __extension__ function-definition
12582 The DECL_SPECIFIERS apply to this declarator. Returns a
12583 representation of the entity declared. If MEMBER_P is TRUE, then
12584 this declarator appears in a class scope. The new DECL created by
12585 this declarator is returned.
12587 The CHECKS are access checks that should be performed once we know
12588 what entity is being declared (and, therefore, what classes have
12591 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12592 for a function-definition here as well. If the declarator is a
12593 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12594 be TRUE upon return. By that point, the function-definition will
12595 have been completely parsed.
12597 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12601 cp_parser_init_declarator (cp_parser* parser,
12602 cp_decl_specifier_seq *decl_specifiers,
12603 VEC (deferred_access_check,gc)* checks,
12604 bool function_definition_allowed_p,
12606 int declares_class_or_enum,
12607 bool* function_definition_p)
12609 cp_token *token = NULL, *asm_spec_start_token = NULL,
12610 *attributes_start_token = NULL;
12611 cp_declarator *declarator;
12612 tree prefix_attributes;
12614 tree asm_specification;
12616 tree decl = NULL_TREE;
12618 int is_initialized;
12619 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12620 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12622 enum cpp_ttype initialization_kind;
12623 bool is_direct_init = false;
12624 bool is_non_constant_init;
12625 int ctor_dtor_or_conv_p;
12627 tree pushed_scope = NULL;
12629 /* Gather the attributes that were provided with the
12630 decl-specifiers. */
12631 prefix_attributes = decl_specifiers->attributes;
12633 /* Assume that this is not the declarator for a function
12635 if (function_definition_p)
12636 *function_definition_p = false;
12638 /* Defer access checks while parsing the declarator; we cannot know
12639 what names are accessible until we know what is being
12641 resume_deferring_access_checks ();
12643 /* Parse the declarator. */
12644 token = cp_lexer_peek_token (parser->lexer);
12646 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12647 &ctor_dtor_or_conv_p,
12648 /*parenthesized_p=*/NULL,
12649 /*member_p=*/false);
12650 /* Gather up the deferred checks. */
12651 stop_deferring_access_checks ();
12653 /* If the DECLARATOR was erroneous, there's no need to go
12655 if (declarator == cp_error_declarator)
12656 return error_mark_node;
12658 /* Check that the number of template-parameter-lists is OK. */
12659 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12661 return error_mark_node;
12663 if (declares_class_or_enum & 2)
12664 cp_parser_check_for_definition_in_return_type (declarator,
12665 decl_specifiers->type,
12666 decl_specifiers->type_location);
12668 /* Figure out what scope the entity declared by the DECLARATOR is
12669 located in. `grokdeclarator' sometimes changes the scope, so
12670 we compute it now. */
12671 scope = get_scope_of_declarator (declarator);
12673 /* If we're allowing GNU extensions, look for an asm-specification
12675 if (cp_parser_allow_gnu_extensions_p (parser))
12677 /* Look for an asm-specification. */
12678 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12679 asm_specification = cp_parser_asm_specification_opt (parser);
12680 /* And attributes. */
12681 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12682 attributes = cp_parser_attributes_opt (parser);
12686 asm_specification = NULL_TREE;
12687 attributes = NULL_TREE;
12690 /* Peek at the next token. */
12691 token = cp_lexer_peek_token (parser->lexer);
12692 /* Check to see if the token indicates the start of a
12693 function-definition. */
12694 if (function_declarator_p (declarator)
12695 && cp_parser_token_starts_function_definition_p (token))
12697 if (!function_definition_allowed_p)
12699 /* If a function-definition should not appear here, issue an
12701 cp_parser_error (parser,
12702 "a function-definition is not allowed here");
12703 return error_mark_node;
12707 location_t func_brace_location
12708 = cp_lexer_peek_token (parser->lexer)->location;
12710 /* Neither attributes nor an asm-specification are allowed
12711 on a function-definition. */
12712 if (asm_specification)
12713 error ("%Han asm-specification is not allowed "
12714 "on a function-definition",
12715 &asm_spec_start_token->location);
12717 error ("%Hattributes are not allowed on a function-definition",
12718 &attributes_start_token->location);
12719 /* This is a function-definition. */
12720 *function_definition_p = true;
12722 /* Parse the function definition. */
12724 decl = cp_parser_save_member_function_body (parser,
12727 prefix_attributes);
12730 = (cp_parser_function_definition_from_specifiers_and_declarator
12731 (parser, decl_specifiers, prefix_attributes, declarator));
12733 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12735 /* This is where the prologue starts... */
12736 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12737 = func_brace_location;
12746 Only in function declarations for constructors, destructors, and
12747 type conversions can the decl-specifier-seq be omitted.
12749 We explicitly postpone this check past the point where we handle
12750 function-definitions because we tolerate function-definitions
12751 that are missing their return types in some modes. */
12752 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12754 cp_parser_error (parser,
12755 "expected constructor, destructor, or type conversion");
12756 return error_mark_node;
12759 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12760 if (token->type == CPP_EQ
12761 || token->type == CPP_OPEN_PAREN
12762 || token->type == CPP_OPEN_BRACE)
12764 is_initialized = SD_INITIALIZED;
12765 initialization_kind = token->type;
12767 if (token->type == CPP_EQ
12768 && function_declarator_p (declarator))
12770 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12771 if (t2->keyword == RID_DEFAULT)
12772 is_initialized = SD_DEFAULTED;
12773 else if (t2->keyword == RID_DELETE)
12774 is_initialized = SD_DELETED;
12779 /* If the init-declarator isn't initialized and isn't followed by a
12780 `,' or `;', it's not a valid init-declarator. */
12781 if (token->type != CPP_COMMA
12782 && token->type != CPP_SEMICOLON)
12784 cp_parser_error (parser, "expected initializer");
12785 return error_mark_node;
12787 is_initialized = SD_UNINITIALIZED;
12788 initialization_kind = CPP_EOF;
12791 /* Because start_decl has side-effects, we should only call it if we
12792 know we're going ahead. By this point, we know that we cannot
12793 possibly be looking at any other construct. */
12794 cp_parser_commit_to_tentative_parse (parser);
12796 /* If the decl specifiers were bad, issue an error now that we're
12797 sure this was intended to be a declarator. Then continue
12798 declaring the variable(s), as int, to try to cut down on further
12800 if (decl_specifiers->any_specifiers_p
12801 && decl_specifiers->type == error_mark_node)
12803 cp_parser_error (parser, "invalid type in declaration");
12804 decl_specifiers->type = integer_type_node;
12807 /* Check to see whether or not this declaration is a friend. */
12808 friend_p = cp_parser_friend_p (decl_specifiers);
12810 /* Enter the newly declared entry in the symbol table. If we're
12811 processing a declaration in a class-specifier, we wait until
12812 after processing the initializer. */
12815 if (parser->in_unbraced_linkage_specification_p)
12816 decl_specifiers->storage_class = sc_extern;
12817 decl = start_decl (declarator, decl_specifiers,
12818 is_initialized, attributes, prefix_attributes,
12822 /* Enter the SCOPE. That way unqualified names appearing in the
12823 initializer will be looked up in SCOPE. */
12824 pushed_scope = push_scope (scope);
12826 /* Perform deferred access control checks, now that we know in which
12827 SCOPE the declared entity resides. */
12828 if (!member_p && decl)
12830 tree saved_current_function_decl = NULL_TREE;
12832 /* If the entity being declared is a function, pretend that we
12833 are in its scope. If it is a `friend', it may have access to
12834 things that would not otherwise be accessible. */
12835 if (TREE_CODE (decl) == FUNCTION_DECL)
12837 saved_current_function_decl = current_function_decl;
12838 current_function_decl = decl;
12841 /* Perform access checks for template parameters. */
12842 cp_parser_perform_template_parameter_access_checks (checks);
12844 /* Perform the access control checks for the declarator and the
12845 decl-specifiers. */
12846 perform_deferred_access_checks ();
12848 /* Restore the saved value. */
12849 if (TREE_CODE (decl) == FUNCTION_DECL)
12850 current_function_decl = saved_current_function_decl;
12853 /* Parse the initializer. */
12854 initializer = NULL_TREE;
12855 is_direct_init = false;
12856 is_non_constant_init = true;
12857 if (is_initialized)
12859 if (function_declarator_p (declarator))
12861 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12862 if (initialization_kind == CPP_EQ)
12863 initializer = cp_parser_pure_specifier (parser);
12866 /* If the declaration was erroneous, we don't really
12867 know what the user intended, so just silently
12868 consume the initializer. */
12869 if (decl != error_mark_node)
12870 error ("%Hinitializer provided for function",
12871 &initializer_start_token->location);
12872 cp_parser_skip_to_closing_parenthesis (parser,
12873 /*recovering=*/true,
12874 /*or_comma=*/false,
12875 /*consume_paren=*/true);
12879 initializer = cp_parser_initializer (parser,
12881 &is_non_constant_init);
12884 /* The old parser allows attributes to appear after a parenthesized
12885 initializer. Mark Mitchell proposed removing this functionality
12886 on the GCC mailing lists on 2002-08-13. This parser accepts the
12887 attributes -- but ignores them. */
12888 if (cp_parser_allow_gnu_extensions_p (parser)
12889 && initialization_kind == CPP_OPEN_PAREN)
12890 if (cp_parser_attributes_opt (parser))
12891 warning (OPT_Wattributes,
12892 "attributes after parenthesized initializer ignored");
12894 /* For an in-class declaration, use `grokfield' to create the
12900 pop_scope (pushed_scope);
12901 pushed_scope = false;
12903 decl = grokfield (declarator, decl_specifiers,
12904 initializer, !is_non_constant_init,
12905 /*asmspec=*/NULL_TREE,
12906 prefix_attributes);
12907 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12908 cp_parser_save_default_args (parser, decl);
12911 /* Finish processing the declaration. But, skip friend
12913 if (!friend_p && decl && decl != error_mark_node)
12915 cp_finish_decl (decl,
12916 initializer, !is_non_constant_init,
12918 /* If the initializer is in parentheses, then this is
12919 a direct-initialization, which means that an
12920 `explicit' constructor is OK. Otherwise, an
12921 `explicit' constructor cannot be used. */
12922 ((is_direct_init || !is_initialized)
12923 ? 0 : LOOKUP_ONLYCONVERTING));
12925 else if ((cxx_dialect != cxx98) && friend_p
12926 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12927 /* Core issue #226 (C++0x only): A default template-argument
12928 shall not be specified in a friend class template
12930 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12931 /*is_partial=*/0, /*is_friend_decl=*/1);
12933 if (!friend_p && pushed_scope)
12934 pop_scope (pushed_scope);
12939 /* Parse a declarator.
12943 ptr-operator declarator
12945 abstract-declarator:
12946 ptr-operator abstract-declarator [opt]
12947 direct-abstract-declarator
12952 attributes [opt] direct-declarator
12953 attributes [opt] ptr-operator declarator
12955 abstract-declarator:
12956 attributes [opt] ptr-operator abstract-declarator [opt]
12957 attributes [opt] direct-abstract-declarator
12959 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12960 detect constructor, destructor or conversion operators. It is set
12961 to -1 if the declarator is a name, and +1 if it is a
12962 function. Otherwise it is set to zero. Usually you just want to
12963 test for >0, but internally the negative value is used.
12965 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12966 a decl-specifier-seq unless it declares a constructor, destructor,
12967 or conversion. It might seem that we could check this condition in
12968 semantic analysis, rather than parsing, but that makes it difficult
12969 to handle something like `f()'. We want to notice that there are
12970 no decl-specifiers, and therefore realize that this is an
12971 expression, not a declaration.)
12973 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12974 the declarator is a direct-declarator of the form "(...)".
12976 MEMBER_P is true iff this declarator is a member-declarator. */
12978 static cp_declarator *
12979 cp_parser_declarator (cp_parser* parser,
12980 cp_parser_declarator_kind dcl_kind,
12981 int* ctor_dtor_or_conv_p,
12982 bool* parenthesized_p,
12986 cp_declarator *declarator;
12987 enum tree_code code;
12988 cp_cv_quals cv_quals;
12990 tree attributes = NULL_TREE;
12992 /* Assume this is not a constructor, destructor, or type-conversion
12994 if (ctor_dtor_or_conv_p)
12995 *ctor_dtor_or_conv_p = 0;
12997 if (cp_parser_allow_gnu_extensions_p (parser))
12998 attributes = cp_parser_attributes_opt (parser);
13000 /* Peek at the next token. */
13001 token = cp_lexer_peek_token (parser->lexer);
13003 /* Check for the ptr-operator production. */
13004 cp_parser_parse_tentatively (parser);
13005 /* Parse the ptr-operator. */
13006 code = cp_parser_ptr_operator (parser,
13009 /* If that worked, then we have a ptr-operator. */
13010 if (cp_parser_parse_definitely (parser))
13012 /* If a ptr-operator was found, then this declarator was not
13014 if (parenthesized_p)
13015 *parenthesized_p = true;
13016 /* The dependent declarator is optional if we are parsing an
13017 abstract-declarator. */
13018 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13019 cp_parser_parse_tentatively (parser);
13021 /* Parse the dependent declarator. */
13022 declarator = cp_parser_declarator (parser, dcl_kind,
13023 /*ctor_dtor_or_conv_p=*/NULL,
13024 /*parenthesized_p=*/NULL,
13025 /*member_p=*/false);
13027 /* If we are parsing an abstract-declarator, we must handle the
13028 case where the dependent declarator is absent. */
13029 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13030 && !cp_parser_parse_definitely (parser))
13033 declarator = cp_parser_make_indirect_declarator
13034 (code, class_type, cv_quals, declarator);
13036 /* Everything else is a direct-declarator. */
13039 if (parenthesized_p)
13040 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13042 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13043 ctor_dtor_or_conv_p,
13047 if (attributes && declarator && declarator != cp_error_declarator)
13048 declarator->attributes = attributes;
13053 /* Parse a direct-declarator or direct-abstract-declarator.
13057 direct-declarator ( parameter-declaration-clause )
13058 cv-qualifier-seq [opt]
13059 exception-specification [opt]
13060 direct-declarator [ constant-expression [opt] ]
13063 direct-abstract-declarator:
13064 direct-abstract-declarator [opt]
13065 ( parameter-declaration-clause )
13066 cv-qualifier-seq [opt]
13067 exception-specification [opt]
13068 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13069 ( abstract-declarator )
13071 Returns a representation of the declarator. DCL_KIND is
13072 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13073 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13074 we are parsing a direct-declarator. It is
13075 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13076 of ambiguity we prefer an abstract declarator, as per
13077 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13078 cp_parser_declarator. */
13080 static cp_declarator *
13081 cp_parser_direct_declarator (cp_parser* parser,
13082 cp_parser_declarator_kind dcl_kind,
13083 int* ctor_dtor_or_conv_p,
13087 cp_declarator *declarator = NULL;
13088 tree scope = NULL_TREE;
13089 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13090 bool saved_in_declarator_p = parser->in_declarator_p;
13092 tree pushed_scope = NULL_TREE;
13096 /* Peek at the next token. */
13097 token = cp_lexer_peek_token (parser->lexer);
13098 if (token->type == CPP_OPEN_PAREN)
13100 /* This is either a parameter-declaration-clause, or a
13101 parenthesized declarator. When we know we are parsing a
13102 named declarator, it must be a parenthesized declarator
13103 if FIRST is true. For instance, `(int)' is a
13104 parameter-declaration-clause, with an omitted
13105 direct-abstract-declarator. But `((*))', is a
13106 parenthesized abstract declarator. Finally, when T is a
13107 template parameter `(T)' is a
13108 parameter-declaration-clause, and not a parenthesized
13111 We first try and parse a parameter-declaration-clause,
13112 and then try a nested declarator (if FIRST is true).
13114 It is not an error for it not to be a
13115 parameter-declaration-clause, even when FIRST is
13121 The first is the declaration of a function while the
13122 second is the definition of a variable, including its
13125 Having seen only the parenthesis, we cannot know which of
13126 these two alternatives should be selected. Even more
13127 complex are examples like:
13132 The former is a function-declaration; the latter is a
13133 variable initialization.
13135 Thus again, we try a parameter-declaration-clause, and if
13136 that fails, we back out and return. */
13138 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13141 unsigned saved_num_template_parameter_lists;
13142 bool is_declarator = false;
13145 /* In a member-declarator, the only valid interpretation
13146 of a parenthesis is the start of a
13147 parameter-declaration-clause. (It is invalid to
13148 initialize a static data member with a parenthesized
13149 initializer; only the "=" form of initialization is
13152 cp_parser_parse_tentatively (parser);
13154 /* Consume the `('. */
13155 cp_lexer_consume_token (parser->lexer);
13158 /* If this is going to be an abstract declarator, we're
13159 in a declarator and we can't have default args. */
13160 parser->default_arg_ok_p = false;
13161 parser->in_declarator_p = true;
13164 /* Inside the function parameter list, surrounding
13165 template-parameter-lists do not apply. */
13166 saved_num_template_parameter_lists
13167 = parser->num_template_parameter_lists;
13168 parser->num_template_parameter_lists = 0;
13170 begin_scope (sk_function_parms, NULL_TREE);
13172 /* Parse the parameter-declaration-clause. */
13173 params = cp_parser_parameter_declaration_clause (parser);
13175 parser->num_template_parameter_lists
13176 = saved_num_template_parameter_lists;
13178 /* If all went well, parse the cv-qualifier-seq and the
13179 exception-specification. */
13180 if (member_p || cp_parser_parse_definitely (parser))
13182 cp_cv_quals cv_quals;
13183 tree exception_specification;
13186 is_declarator = true;
13188 if (ctor_dtor_or_conv_p)
13189 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13191 /* Consume the `)'. */
13192 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13194 /* Parse the cv-qualifier-seq. */
13195 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13196 /* And the exception-specification. */
13197 exception_specification
13198 = cp_parser_exception_specification_opt (parser);
13201 = cp_parser_late_return_type_opt (parser);
13203 /* Create the function-declarator. */
13204 declarator = make_call_declarator (declarator,
13207 exception_specification,
13209 /* Any subsequent parameter lists are to do with
13210 return type, so are not those of the declared
13212 parser->default_arg_ok_p = false;
13215 /* Remove the function parms from scope. */
13216 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13217 pop_binding (DECL_NAME (t), t);
13221 /* Repeat the main loop. */
13225 /* If this is the first, we can try a parenthesized
13229 bool saved_in_type_id_in_expr_p;
13231 parser->default_arg_ok_p = saved_default_arg_ok_p;
13232 parser->in_declarator_p = saved_in_declarator_p;
13234 /* Consume the `('. */
13235 cp_lexer_consume_token (parser->lexer);
13236 /* Parse the nested declarator. */
13237 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13238 parser->in_type_id_in_expr_p = true;
13240 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13241 /*parenthesized_p=*/NULL,
13243 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13245 /* Expect a `)'. */
13246 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13247 declarator = cp_error_declarator;
13248 if (declarator == cp_error_declarator)
13251 goto handle_declarator;
13253 /* Otherwise, we must be done. */
13257 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13258 && token->type == CPP_OPEN_SQUARE)
13260 /* Parse an array-declarator. */
13263 if (ctor_dtor_or_conv_p)
13264 *ctor_dtor_or_conv_p = 0;
13267 parser->default_arg_ok_p = false;
13268 parser->in_declarator_p = true;
13269 /* Consume the `['. */
13270 cp_lexer_consume_token (parser->lexer);
13271 /* Peek at the next token. */
13272 token = cp_lexer_peek_token (parser->lexer);
13273 /* If the next token is `]', then there is no
13274 constant-expression. */
13275 if (token->type != CPP_CLOSE_SQUARE)
13277 bool non_constant_p;
13280 = cp_parser_constant_expression (parser,
13281 /*allow_non_constant=*/true,
13283 if (!non_constant_p)
13284 bounds = fold_non_dependent_expr (bounds);
13285 else if (processing_template_decl)
13287 /* Remember this wasn't a constant-expression. */
13288 bounds = build_nop (TREE_TYPE (bounds), bounds);
13289 TREE_SIDE_EFFECTS (bounds) = 1;
13292 /* Normally, the array bound must be an integral constant
13293 expression. However, as an extension, we allow VLAs
13294 in function scopes. */
13295 else if (!parser->in_function_body)
13297 error ("%Harray bound is not an integer constant",
13299 bounds = error_mark_node;
13303 bounds = NULL_TREE;
13304 /* Look for the closing `]'. */
13305 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13307 declarator = cp_error_declarator;
13311 declarator = make_array_declarator (declarator, bounds);
13313 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13315 tree qualifying_scope;
13316 tree unqualified_name;
13317 special_function_kind sfk;
13319 bool pack_expansion_p = false;
13320 cp_token *declarator_id_start_token;
13322 /* Parse a declarator-id */
13323 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13326 cp_parser_parse_tentatively (parser);
13328 /* If we see an ellipsis, we should be looking at a
13330 if (token->type == CPP_ELLIPSIS)
13332 /* Consume the `...' */
13333 cp_lexer_consume_token (parser->lexer);
13335 pack_expansion_p = true;
13339 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13341 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13342 qualifying_scope = parser->scope;
13347 if (!unqualified_name && pack_expansion_p)
13349 /* Check whether an error occurred. */
13350 okay = !cp_parser_error_occurred (parser);
13352 /* We already consumed the ellipsis to mark a
13353 parameter pack, but we have no way to report it,
13354 so abort the tentative parse. We will be exiting
13355 immediately anyway. */
13356 cp_parser_abort_tentative_parse (parser);
13359 okay = cp_parser_parse_definitely (parser);
13362 unqualified_name = error_mark_node;
13363 else if (unqualified_name
13364 && (qualifying_scope
13365 || (TREE_CODE (unqualified_name)
13366 != IDENTIFIER_NODE)))
13368 cp_parser_error (parser, "expected unqualified-id");
13369 unqualified_name = error_mark_node;
13373 if (!unqualified_name)
13375 if (unqualified_name == error_mark_node)
13377 declarator = cp_error_declarator;
13378 pack_expansion_p = false;
13379 declarator->parameter_pack_p = false;
13383 if (qualifying_scope && at_namespace_scope_p ()
13384 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13386 /* In the declaration of a member of a template class
13387 outside of the class itself, the SCOPE will sometimes
13388 be a TYPENAME_TYPE. For example, given:
13390 template <typename T>
13391 int S<T>::R::i = 3;
13393 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13394 this context, we must resolve S<T>::R to an ordinary
13395 type, rather than a typename type.
13397 The reason we normally avoid resolving TYPENAME_TYPEs
13398 is that a specialization of `S' might render
13399 `S<T>::R' not a type. However, if `S' is
13400 specialized, then this `i' will not be used, so there
13401 is no harm in resolving the types here. */
13404 /* Resolve the TYPENAME_TYPE. */
13405 type = resolve_typename_type (qualifying_scope,
13406 /*only_current_p=*/false);
13407 /* If that failed, the declarator is invalid. */
13408 if (TREE_CODE (type) == TYPENAME_TYPE)
13409 error ("%H%<%T::%E%> is not a type",
13410 &declarator_id_start_token->location,
13411 TYPE_CONTEXT (qualifying_scope),
13412 TYPE_IDENTIFIER (qualifying_scope));
13413 qualifying_scope = type;
13418 if (unqualified_name)
13422 if (qualifying_scope
13423 && CLASS_TYPE_P (qualifying_scope))
13424 class_type = qualifying_scope;
13426 class_type = current_class_type;
13428 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13430 tree name_type = TREE_TYPE (unqualified_name);
13431 if (class_type && same_type_p (name_type, class_type))
13433 if (qualifying_scope
13434 && CLASSTYPE_USE_TEMPLATE (name_type))
13436 error ("%Hinvalid use of constructor as a template",
13437 &declarator_id_start_token->location);
13438 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13439 "name the constructor in a qualified name",
13441 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13442 class_type, name_type);
13443 declarator = cp_error_declarator;
13447 unqualified_name = constructor_name (class_type);
13451 /* We do not attempt to print the declarator
13452 here because we do not have enough
13453 information about its original syntactic
13455 cp_parser_error (parser, "invalid declarator");
13456 declarator = cp_error_declarator;
13463 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13464 sfk = sfk_destructor;
13465 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13466 sfk = sfk_conversion;
13467 else if (/* There's no way to declare a constructor
13468 for an anonymous type, even if the type
13469 got a name for linkage purposes. */
13470 !TYPE_WAS_ANONYMOUS (class_type)
13471 && constructor_name_p (unqualified_name,
13474 unqualified_name = constructor_name (class_type);
13475 sfk = sfk_constructor;
13478 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13479 *ctor_dtor_or_conv_p = -1;
13482 declarator = make_id_declarator (qualifying_scope,
13485 declarator->id_loc = token->location;
13486 declarator->parameter_pack_p = pack_expansion_p;
13488 if (pack_expansion_p)
13489 maybe_warn_variadic_templates ();
13491 handle_declarator:;
13492 scope = get_scope_of_declarator (declarator);
13494 /* Any names that appear after the declarator-id for a
13495 member are looked up in the containing scope. */
13496 pushed_scope = push_scope (scope);
13497 parser->in_declarator_p = true;
13498 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13499 || (declarator && declarator->kind == cdk_id))
13500 /* Default args are only allowed on function
13502 parser->default_arg_ok_p = saved_default_arg_ok_p;
13504 parser->default_arg_ok_p = false;
13513 /* For an abstract declarator, we might wind up with nothing at this
13514 point. That's an error; the declarator is not optional. */
13516 cp_parser_error (parser, "expected declarator");
13518 /* If we entered a scope, we must exit it now. */
13520 pop_scope (pushed_scope);
13522 parser->default_arg_ok_p = saved_default_arg_ok_p;
13523 parser->in_declarator_p = saved_in_declarator_p;
13528 /* Parse a ptr-operator.
13531 * cv-qualifier-seq [opt]
13533 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13538 & cv-qualifier-seq [opt]
13540 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13541 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13542 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13543 filled in with the TYPE containing the member. *CV_QUALS is
13544 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13545 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13546 Note that the tree codes returned by this function have nothing
13547 to do with the types of trees that will be eventually be created
13548 to represent the pointer or reference type being parsed. They are
13549 just constants with suggestive names. */
13550 static enum tree_code
13551 cp_parser_ptr_operator (cp_parser* parser,
13553 cp_cv_quals *cv_quals)
13555 enum tree_code code = ERROR_MARK;
13558 /* Assume that it's not a pointer-to-member. */
13560 /* And that there are no cv-qualifiers. */
13561 *cv_quals = TYPE_UNQUALIFIED;
13563 /* Peek at the next token. */
13564 token = cp_lexer_peek_token (parser->lexer);
13566 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13567 if (token->type == CPP_MULT)
13568 code = INDIRECT_REF;
13569 else if (token->type == CPP_AND)
13571 else if ((cxx_dialect != cxx98) &&
13572 token->type == CPP_AND_AND) /* C++0x only */
13573 code = NON_LVALUE_EXPR;
13575 if (code != ERROR_MARK)
13577 /* Consume the `*', `&' or `&&'. */
13578 cp_lexer_consume_token (parser->lexer);
13580 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13581 `&', if we are allowing GNU extensions. (The only qualifier
13582 that can legally appear after `&' is `restrict', but that is
13583 enforced during semantic analysis. */
13584 if (code == INDIRECT_REF
13585 || cp_parser_allow_gnu_extensions_p (parser))
13586 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13590 /* Try the pointer-to-member case. */
13591 cp_parser_parse_tentatively (parser);
13592 /* Look for the optional `::' operator. */
13593 cp_parser_global_scope_opt (parser,
13594 /*current_scope_valid_p=*/false);
13595 /* Look for the nested-name specifier. */
13596 token = cp_lexer_peek_token (parser->lexer);
13597 cp_parser_nested_name_specifier (parser,
13598 /*typename_keyword_p=*/false,
13599 /*check_dependency_p=*/true,
13601 /*is_declaration=*/false);
13602 /* If we found it, and the next token is a `*', then we are
13603 indeed looking at a pointer-to-member operator. */
13604 if (!cp_parser_error_occurred (parser)
13605 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13607 /* Indicate that the `*' operator was used. */
13608 code = INDIRECT_REF;
13610 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13611 error ("%H%qD is a namespace", &token->location, parser->scope);
13614 /* The type of which the member is a member is given by the
13616 *type = parser->scope;
13617 /* The next name will not be qualified. */
13618 parser->scope = NULL_TREE;
13619 parser->qualifying_scope = NULL_TREE;
13620 parser->object_scope = NULL_TREE;
13621 /* Look for the optional cv-qualifier-seq. */
13622 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13625 /* If that didn't work we don't have a ptr-operator. */
13626 if (!cp_parser_parse_definitely (parser))
13627 cp_parser_error (parser, "expected ptr-operator");
13633 /* Parse an (optional) cv-qualifier-seq.
13636 cv-qualifier cv-qualifier-seq [opt]
13647 Returns a bitmask representing the cv-qualifiers. */
13650 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13652 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13657 cp_cv_quals cv_qualifier;
13659 /* Peek at the next token. */
13660 token = cp_lexer_peek_token (parser->lexer);
13661 /* See if it's a cv-qualifier. */
13662 switch (token->keyword)
13665 cv_qualifier = TYPE_QUAL_CONST;
13669 cv_qualifier = TYPE_QUAL_VOLATILE;
13673 cv_qualifier = TYPE_QUAL_RESTRICT;
13677 cv_qualifier = TYPE_UNQUALIFIED;
13684 if (cv_quals & cv_qualifier)
13686 error ("%Hduplicate cv-qualifier", &token->location);
13687 cp_lexer_purge_token (parser->lexer);
13691 cp_lexer_consume_token (parser->lexer);
13692 cv_quals |= cv_qualifier;
13699 /* Parse a late-specified return type, if any. This is not a separate
13700 non-terminal, but part of a function declarator, which looks like
13704 Returns the type indicated by the type-id. */
13707 cp_parser_late_return_type_opt (cp_parser* parser)
13711 /* Peek at the next token. */
13712 token = cp_lexer_peek_token (parser->lexer);
13713 /* A late-specified return type is indicated by an initial '->'. */
13714 if (token->type != CPP_DEREF)
13717 /* Consume the ->. */
13718 cp_lexer_consume_token (parser->lexer);
13720 return cp_parser_type_id (parser);
13723 /* Parse a declarator-id.
13727 :: [opt] nested-name-specifier [opt] type-name
13729 In the `id-expression' case, the value returned is as for
13730 cp_parser_id_expression if the id-expression was an unqualified-id.
13731 If the id-expression was a qualified-id, then a SCOPE_REF is
13732 returned. The first operand is the scope (either a NAMESPACE_DECL
13733 or TREE_TYPE), but the second is still just a representation of an
13737 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13740 /* The expression must be an id-expression. Assume that qualified
13741 names are the names of types so that:
13744 int S<T>::R::i = 3;
13746 will work; we must treat `S<T>::R' as the name of a type.
13747 Similarly, assume that qualified names are templates, where
13751 int S<T>::R<T>::i = 3;
13754 id = cp_parser_id_expression (parser,
13755 /*template_keyword_p=*/false,
13756 /*check_dependency_p=*/false,
13757 /*template_p=*/NULL,
13758 /*declarator_p=*/true,
13760 if (id && BASELINK_P (id))
13761 id = BASELINK_FUNCTIONS (id);
13765 /* Parse a type-id.
13768 type-specifier-seq abstract-declarator [opt]
13770 Returns the TYPE specified. */
13773 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13775 cp_decl_specifier_seq type_specifier_seq;
13776 cp_declarator *abstract_declarator;
13778 /* Parse the type-specifier-seq. */
13779 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13780 &type_specifier_seq);
13781 if (type_specifier_seq.type == error_mark_node)
13782 return error_mark_node;
13784 /* There might or might not be an abstract declarator. */
13785 cp_parser_parse_tentatively (parser);
13786 /* Look for the declarator. */
13787 abstract_declarator
13788 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13789 /*parenthesized_p=*/NULL,
13790 /*member_p=*/false);
13791 /* Check to see if there really was a declarator. */
13792 if (!cp_parser_parse_definitely (parser))
13793 abstract_declarator = NULL;
13795 if (type_specifier_seq.type
13796 && type_uses_auto (type_specifier_seq.type))
13798 error ("invalid use of %<auto%>");
13799 return error_mark_node;
13802 return groktypename (&type_specifier_seq, abstract_declarator,
13806 static tree cp_parser_type_id (cp_parser *parser)
13808 return cp_parser_type_id_1 (parser, false);
13811 static tree cp_parser_template_type_arg (cp_parser *parser)
13813 return cp_parser_type_id_1 (parser, true);
13816 /* Parse a type-specifier-seq.
13818 type-specifier-seq:
13819 type-specifier type-specifier-seq [opt]
13823 type-specifier-seq:
13824 attributes type-specifier-seq [opt]
13826 If IS_CONDITION is true, we are at the start of a "condition",
13827 e.g., we've just seen "if (".
13829 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13832 cp_parser_type_specifier_seq (cp_parser* parser,
13834 cp_decl_specifier_seq *type_specifier_seq)
13836 bool seen_type_specifier = false;
13837 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13838 cp_token *start_token = NULL;
13840 /* Clear the TYPE_SPECIFIER_SEQ. */
13841 clear_decl_specs (type_specifier_seq);
13843 /* Parse the type-specifiers and attributes. */
13846 tree type_specifier;
13847 bool is_cv_qualifier;
13849 /* Check for attributes first. */
13850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13852 type_specifier_seq->attributes =
13853 chainon (type_specifier_seq->attributes,
13854 cp_parser_attributes_opt (parser));
13858 /* record the token of the beginning of the type specifier seq,
13859 for error reporting purposes*/
13861 start_token = cp_lexer_peek_token (parser->lexer);
13863 /* Look for the type-specifier. */
13864 type_specifier = cp_parser_type_specifier (parser,
13866 type_specifier_seq,
13867 /*is_declaration=*/false,
13870 if (!type_specifier)
13872 /* If the first type-specifier could not be found, this is not a
13873 type-specifier-seq at all. */
13874 if (!seen_type_specifier)
13876 cp_parser_error (parser, "expected type-specifier");
13877 type_specifier_seq->type = error_mark_node;
13880 /* If subsequent type-specifiers could not be found, the
13881 type-specifier-seq is complete. */
13885 seen_type_specifier = true;
13886 /* The standard says that a condition can be:
13888 type-specifier-seq declarator = assignment-expression
13895 we should treat the "S" as a declarator, not as a
13896 type-specifier. The standard doesn't say that explicitly for
13897 type-specifier-seq, but it does say that for
13898 decl-specifier-seq in an ordinary declaration. Perhaps it
13899 would be clearer just to allow a decl-specifier-seq here, and
13900 then add a semantic restriction that if any decl-specifiers
13901 that are not type-specifiers appear, the program is invalid. */
13902 if (is_condition && !is_cv_qualifier)
13903 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13906 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13909 /* Parse a parameter-declaration-clause.
13911 parameter-declaration-clause:
13912 parameter-declaration-list [opt] ... [opt]
13913 parameter-declaration-list , ...
13915 Returns a representation for the parameter declarations. A return
13916 value of NULL indicates a parameter-declaration-clause consisting
13917 only of an ellipsis. */
13920 cp_parser_parameter_declaration_clause (cp_parser* parser)
13927 /* Peek at the next token. */
13928 token = cp_lexer_peek_token (parser->lexer);
13929 /* Check for trivial parameter-declaration-clauses. */
13930 if (token->type == CPP_ELLIPSIS)
13932 /* Consume the `...' token. */
13933 cp_lexer_consume_token (parser->lexer);
13936 else if (token->type == CPP_CLOSE_PAREN)
13937 /* There are no parameters. */
13939 #ifndef NO_IMPLICIT_EXTERN_C
13940 if (in_system_header && current_class_type == NULL
13941 && current_lang_name == lang_name_c)
13945 return void_list_node;
13947 /* Check for `(void)', too, which is a special case. */
13948 else if (token->keyword == RID_VOID
13949 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13950 == CPP_CLOSE_PAREN))
13952 /* Consume the `void' token. */
13953 cp_lexer_consume_token (parser->lexer);
13954 /* There are no parameters. */
13955 return void_list_node;
13958 /* Parse the parameter-declaration-list. */
13959 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13960 /* If a parse error occurred while parsing the
13961 parameter-declaration-list, then the entire
13962 parameter-declaration-clause is erroneous. */
13966 /* Peek at the next token. */
13967 token = cp_lexer_peek_token (parser->lexer);
13968 /* If it's a `,', the clause should terminate with an ellipsis. */
13969 if (token->type == CPP_COMMA)
13971 /* Consume the `,'. */
13972 cp_lexer_consume_token (parser->lexer);
13973 /* Expect an ellipsis. */
13975 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13977 /* It might also be `...' if the optional trailing `,' was
13979 else if (token->type == CPP_ELLIPSIS)
13981 /* Consume the `...' token. */
13982 cp_lexer_consume_token (parser->lexer);
13983 /* And remember that we saw it. */
13987 ellipsis_p = false;
13989 /* Finish the parameter list. */
13991 parameters = chainon (parameters, void_list_node);
13996 /* Parse a parameter-declaration-list.
13998 parameter-declaration-list:
13999 parameter-declaration
14000 parameter-declaration-list , parameter-declaration
14002 Returns a representation of the parameter-declaration-list, as for
14003 cp_parser_parameter_declaration_clause. However, the
14004 `void_list_node' is never appended to the list. Upon return,
14005 *IS_ERROR will be true iff an error occurred. */
14008 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14010 tree parameters = NULL_TREE;
14011 tree *tail = ¶meters;
14012 bool saved_in_unbraced_linkage_specification_p;
14014 /* Assume all will go well. */
14016 /* The special considerations that apply to a function within an
14017 unbraced linkage specifications do not apply to the parameters
14018 to the function. */
14019 saved_in_unbraced_linkage_specification_p
14020 = parser->in_unbraced_linkage_specification_p;
14021 parser->in_unbraced_linkage_specification_p = false;
14023 /* Look for more parameters. */
14026 cp_parameter_declarator *parameter;
14027 tree decl = error_mark_node;
14028 bool parenthesized_p;
14029 /* Parse the parameter. */
14031 = cp_parser_parameter_declaration (parser,
14032 /*template_parm_p=*/false,
14035 /* We don't know yet if the enclosing context is deprecated, so wait
14036 and warn in grokparms if appropriate. */
14037 deprecated_state = DEPRECATED_SUPPRESS;
14040 decl = grokdeclarator (parameter->declarator,
14041 ¶meter->decl_specifiers,
14043 parameter->default_argument != NULL_TREE,
14044 ¶meter->decl_specifiers.attributes);
14046 deprecated_state = DEPRECATED_NORMAL;
14048 /* If a parse error occurred parsing the parameter declaration,
14049 then the entire parameter-declaration-list is erroneous. */
14050 if (decl == error_mark_node)
14053 parameters = error_mark_node;
14057 if (parameter->decl_specifiers.attributes)
14058 cplus_decl_attributes (&decl,
14059 parameter->decl_specifiers.attributes,
14061 if (DECL_NAME (decl))
14062 decl = pushdecl (decl);
14064 /* Add the new parameter to the list. */
14065 *tail = build_tree_list (parameter->default_argument, decl);
14066 tail = &TREE_CHAIN (*tail);
14068 /* Peek at the next token. */
14069 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14070 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14071 /* These are for Objective-C++ */
14072 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14073 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14074 /* The parameter-declaration-list is complete. */
14076 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14080 /* Peek at the next token. */
14081 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14082 /* If it's an ellipsis, then the list is complete. */
14083 if (token->type == CPP_ELLIPSIS)
14085 /* Otherwise, there must be more parameters. Consume the
14087 cp_lexer_consume_token (parser->lexer);
14088 /* When parsing something like:
14090 int i(float f, double d)
14092 we can tell after seeing the declaration for "f" that we
14093 are not looking at an initialization of a variable "i",
14094 but rather at the declaration of a function "i".
14096 Due to the fact that the parsing of template arguments
14097 (as specified to a template-id) requires backtracking we
14098 cannot use this technique when inside a template argument
14100 if (!parser->in_template_argument_list_p
14101 && !parser->in_type_id_in_expr_p
14102 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14103 /* However, a parameter-declaration of the form
14104 "foat(f)" (which is a valid declaration of a
14105 parameter "f") can also be interpreted as an
14106 expression (the conversion of "f" to "float"). */
14107 && !parenthesized_p)
14108 cp_parser_commit_to_tentative_parse (parser);
14112 cp_parser_error (parser, "expected %<,%> or %<...%>");
14113 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14114 cp_parser_skip_to_closing_parenthesis (parser,
14115 /*recovering=*/true,
14116 /*or_comma=*/false,
14117 /*consume_paren=*/false);
14122 parser->in_unbraced_linkage_specification_p
14123 = saved_in_unbraced_linkage_specification_p;
14128 /* Parse a parameter declaration.
14130 parameter-declaration:
14131 decl-specifier-seq ... [opt] declarator
14132 decl-specifier-seq declarator = assignment-expression
14133 decl-specifier-seq ... [opt] abstract-declarator [opt]
14134 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14136 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14137 declares a template parameter. (In that case, a non-nested `>'
14138 token encountered during the parsing of the assignment-expression
14139 is not interpreted as a greater-than operator.)
14141 Returns a representation of the parameter, or NULL if an error
14142 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14143 true iff the declarator is of the form "(p)". */
14145 static cp_parameter_declarator *
14146 cp_parser_parameter_declaration (cp_parser *parser,
14147 bool template_parm_p,
14148 bool *parenthesized_p)
14150 int declares_class_or_enum;
14151 bool greater_than_is_operator_p;
14152 cp_decl_specifier_seq decl_specifiers;
14153 cp_declarator *declarator;
14154 tree default_argument;
14155 cp_token *token = NULL, *declarator_token_start = NULL;
14156 const char *saved_message;
14158 /* In a template parameter, `>' is not an operator.
14162 When parsing a default template-argument for a non-type
14163 template-parameter, the first non-nested `>' is taken as the end
14164 of the template parameter-list rather than a greater-than
14166 greater_than_is_operator_p = !template_parm_p;
14168 /* Type definitions may not appear in parameter types. */
14169 saved_message = parser->type_definition_forbidden_message;
14170 parser->type_definition_forbidden_message
14171 = "types may not be defined in parameter types";
14173 /* Parse the declaration-specifiers. */
14174 cp_parser_decl_specifier_seq (parser,
14175 CP_PARSER_FLAGS_NONE,
14177 &declares_class_or_enum);
14178 /* If an error occurred, there's no reason to attempt to parse the
14179 rest of the declaration. */
14180 if (cp_parser_error_occurred (parser))
14182 parser->type_definition_forbidden_message = saved_message;
14186 /* Peek at the next token. */
14187 token = cp_lexer_peek_token (parser->lexer);
14189 /* If the next token is a `)', `,', `=', `>', or `...', then there
14190 is no declarator. However, when variadic templates are enabled,
14191 there may be a declarator following `...'. */
14192 if (token->type == CPP_CLOSE_PAREN
14193 || token->type == CPP_COMMA
14194 || token->type == CPP_EQ
14195 || token->type == CPP_GREATER)
14198 if (parenthesized_p)
14199 *parenthesized_p = false;
14201 /* Otherwise, there should be a declarator. */
14204 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14205 parser->default_arg_ok_p = false;
14207 /* After seeing a decl-specifier-seq, if the next token is not a
14208 "(", there is no possibility that the code is a valid
14209 expression. Therefore, if parsing tentatively, we commit at
14211 if (!parser->in_template_argument_list_p
14212 /* In an expression context, having seen:
14216 we cannot be sure whether we are looking at a
14217 function-type (taking a "char" as a parameter) or a cast
14218 of some object of type "char" to "int". */
14219 && !parser->in_type_id_in_expr_p
14220 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14221 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14222 cp_parser_commit_to_tentative_parse (parser);
14223 /* Parse the declarator. */
14224 declarator_token_start = token;
14225 declarator = cp_parser_declarator (parser,
14226 CP_PARSER_DECLARATOR_EITHER,
14227 /*ctor_dtor_or_conv_p=*/NULL,
14229 /*member_p=*/false);
14230 parser->default_arg_ok_p = saved_default_arg_ok_p;
14231 /* After the declarator, allow more attributes. */
14232 decl_specifiers.attributes
14233 = chainon (decl_specifiers.attributes,
14234 cp_parser_attributes_opt (parser));
14237 /* If the next token is an ellipsis, and we have not seen a
14238 declarator name, and the type of the declarator contains parameter
14239 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14240 a parameter pack expansion expression. Otherwise, leave the
14241 ellipsis for a C-style variadic function. */
14242 token = cp_lexer_peek_token (parser->lexer);
14243 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14245 tree type = decl_specifiers.type;
14247 if (type && DECL_P (type))
14248 type = TREE_TYPE (type);
14251 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14252 && declarator_can_be_parameter_pack (declarator)
14253 && (!declarator || !declarator->parameter_pack_p)
14254 && uses_parameter_packs (type))
14256 /* Consume the `...'. */
14257 cp_lexer_consume_token (parser->lexer);
14258 maybe_warn_variadic_templates ();
14260 /* Build a pack expansion type */
14262 declarator->parameter_pack_p = true;
14264 decl_specifiers.type = make_pack_expansion (type);
14268 /* The restriction on defining new types applies only to the type
14269 of the parameter, not to the default argument. */
14270 parser->type_definition_forbidden_message = saved_message;
14272 /* If the next token is `=', then process a default argument. */
14273 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14275 /* Consume the `='. */
14276 cp_lexer_consume_token (parser->lexer);
14278 /* If we are defining a class, then the tokens that make up the
14279 default argument must be saved and processed later. */
14280 if (!template_parm_p && at_class_scope_p ()
14281 && TYPE_BEING_DEFINED (current_class_type))
14283 unsigned depth = 0;
14284 int maybe_template_id = 0;
14285 cp_token *first_token;
14288 /* Add tokens until we have processed the entire default
14289 argument. We add the range [first_token, token). */
14290 first_token = cp_lexer_peek_token (parser->lexer);
14295 /* Peek at the next token. */
14296 token = cp_lexer_peek_token (parser->lexer);
14297 /* What we do depends on what token we have. */
14298 switch (token->type)
14300 /* In valid code, a default argument must be
14301 immediately followed by a `,' `)', or `...'. */
14303 if (depth == 0 && maybe_template_id)
14305 /* If we've seen a '<', we might be in a
14306 template-argument-list. Until Core issue 325 is
14307 resolved, we don't know how this situation ought
14308 to be handled, so try to DTRT. We check whether
14309 what comes after the comma is a valid parameter
14310 declaration list. If it is, then the comma ends
14311 the default argument; otherwise the default
14312 argument continues. */
14313 bool error = false;
14315 /* Set ITALP so cp_parser_parameter_declaration_list
14316 doesn't decide to commit to this parse. */
14317 bool saved_italp = parser->in_template_argument_list_p;
14318 parser->in_template_argument_list_p = true;
14320 cp_parser_parse_tentatively (parser);
14321 cp_lexer_consume_token (parser->lexer);
14322 cp_parser_parameter_declaration_list (parser, &error);
14323 if (!cp_parser_error_occurred (parser) && !error)
14325 cp_parser_abort_tentative_parse (parser);
14327 parser->in_template_argument_list_p = saved_italp;
14330 case CPP_CLOSE_PAREN:
14332 /* If we run into a non-nested `;', `}', or `]',
14333 then the code is invalid -- but the default
14334 argument is certainly over. */
14335 case CPP_SEMICOLON:
14336 case CPP_CLOSE_BRACE:
14337 case CPP_CLOSE_SQUARE:
14340 /* Update DEPTH, if necessary. */
14341 else if (token->type == CPP_CLOSE_PAREN
14342 || token->type == CPP_CLOSE_BRACE
14343 || token->type == CPP_CLOSE_SQUARE)
14347 case CPP_OPEN_PAREN:
14348 case CPP_OPEN_SQUARE:
14349 case CPP_OPEN_BRACE:
14355 /* This might be the comparison operator, or it might
14356 start a template argument list. */
14357 ++maybe_template_id;
14361 if (cxx_dialect == cxx98)
14363 /* Fall through for C++0x, which treats the `>>'
14364 operator like two `>' tokens in certain
14370 /* This might be an operator, or it might close a
14371 template argument list. But if a previous '<'
14372 started a template argument list, this will have
14373 closed it, so we can't be in one anymore. */
14374 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14375 if (maybe_template_id < 0)
14376 maybe_template_id = 0;
14380 /* If we run out of tokens, issue an error message. */
14382 case CPP_PRAGMA_EOL:
14383 error ("%Hfile ends in default argument", &token->location);
14389 /* In these cases, we should look for template-ids.
14390 For example, if the default argument is
14391 `X<int, double>()', we need to do name lookup to
14392 figure out whether or not `X' is a template; if
14393 so, the `,' does not end the default argument.
14395 That is not yet done. */
14402 /* If we've reached the end, stop. */
14406 /* Add the token to the token block. */
14407 token = cp_lexer_consume_token (parser->lexer);
14410 /* Create a DEFAULT_ARG to represent the unparsed default
14412 default_argument = make_node (DEFAULT_ARG);
14413 DEFARG_TOKENS (default_argument)
14414 = cp_token_cache_new (first_token, token);
14415 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14417 /* Outside of a class definition, we can just parse the
14418 assignment-expression. */
14421 token = cp_lexer_peek_token (parser->lexer);
14423 = cp_parser_default_argument (parser, template_parm_p);
14426 if (!parser->default_arg_ok_p)
14428 if (flag_permissive)
14429 warning (0, "deprecated use of default argument for parameter of non-function");
14432 error ("%Hdefault arguments are only "
14433 "permitted for function parameters",
14435 default_argument = NULL_TREE;
14438 else if ((declarator && declarator->parameter_pack_p)
14439 || (decl_specifiers.type
14440 && PACK_EXPANSION_P (decl_specifiers.type)))
14442 const char* kind = template_parm_p? "template " : "";
14444 /* Find the name of the parameter pack. */
14445 cp_declarator *id_declarator = declarator;
14446 while (id_declarator && id_declarator->kind != cdk_id)
14447 id_declarator = id_declarator->declarator;
14449 if (id_declarator && id_declarator->kind == cdk_id)
14450 error ("%H%sparameter pack %qD cannot have a default argument",
14451 &declarator_token_start->location,
14452 kind, id_declarator->u.id.unqualified_name);
14454 error ("%H%sparameter pack cannot have a default argument",
14455 &declarator_token_start->location, kind);
14457 default_argument = NULL_TREE;
14461 default_argument = NULL_TREE;
14463 return make_parameter_declarator (&decl_specifiers,
14468 /* Parse a default argument and return it.
14470 TEMPLATE_PARM_P is true if this is a default argument for a
14471 non-type template parameter. */
14473 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14475 tree default_argument = NULL_TREE;
14476 bool saved_greater_than_is_operator_p;
14477 bool saved_local_variables_forbidden_p;
14479 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14481 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14482 parser->greater_than_is_operator_p = !template_parm_p;
14483 /* Local variable names (and the `this' keyword) may not
14484 appear in a default argument. */
14485 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14486 parser->local_variables_forbidden_p = true;
14487 /* The default argument expression may cause implicitly
14488 defined member functions to be synthesized, which will
14489 result in garbage collection. We must treat this
14490 situation as if we were within the body of function so as
14491 to avoid collecting live data on the stack. */
14493 /* Parse the assignment-expression. */
14494 if (template_parm_p)
14495 push_deferring_access_checks (dk_no_deferred);
14497 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14498 if (template_parm_p)
14499 pop_deferring_access_checks ();
14500 /* Restore saved state. */
14502 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14503 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14505 return default_argument;
14508 /* Parse a function-body.
14511 compound_statement */
14514 cp_parser_function_body (cp_parser *parser)
14516 cp_parser_compound_statement (parser, NULL, false);
14519 /* Parse a ctor-initializer-opt followed by a function-body. Return
14520 true if a ctor-initializer was present. */
14523 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14526 bool ctor_initializer_p;
14528 /* Begin the function body. */
14529 body = begin_function_body ();
14530 /* Parse the optional ctor-initializer. */
14531 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14532 /* Parse the function-body. */
14533 cp_parser_function_body (parser);
14534 /* Finish the function body. */
14535 finish_function_body (body);
14537 return ctor_initializer_p;
14540 /* Parse an initializer.
14543 = initializer-clause
14544 ( expression-list )
14546 Returns an expression representing the initializer. If no
14547 initializer is present, NULL_TREE is returned.
14549 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14550 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14551 set to TRUE if there is no initializer present. If there is an
14552 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14553 is set to true; otherwise it is set to false. */
14556 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14557 bool* non_constant_p)
14562 /* Peek at the next token. */
14563 token = cp_lexer_peek_token (parser->lexer);
14565 /* Let our caller know whether or not this initializer was
14567 *is_direct_init = (token->type != CPP_EQ);
14568 /* Assume that the initializer is constant. */
14569 *non_constant_p = false;
14571 if (token->type == CPP_EQ)
14573 /* Consume the `='. */
14574 cp_lexer_consume_token (parser->lexer);
14575 /* Parse the initializer-clause. */
14576 init = cp_parser_initializer_clause (parser, non_constant_p);
14578 else if (token->type == CPP_OPEN_PAREN)
14579 init = cp_parser_parenthesized_expression_list (parser, false,
14581 /*allow_expansion_p=*/true,
14583 else if (token->type == CPP_OPEN_BRACE)
14585 maybe_warn_cpp0x ("extended initializer lists");
14586 init = cp_parser_braced_list (parser, non_constant_p);
14587 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14591 /* Anything else is an error. */
14592 cp_parser_error (parser, "expected initializer");
14593 init = error_mark_node;
14599 /* Parse an initializer-clause.
14601 initializer-clause:
14602 assignment-expression
14605 Returns an expression representing the initializer.
14607 If the `assignment-expression' production is used the value
14608 returned is simply a representation for the expression.
14610 Otherwise, calls cp_parser_braced_list. */
14613 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14617 /* Assume the expression is constant. */
14618 *non_constant_p = false;
14620 /* If it is not a `{', then we are looking at an
14621 assignment-expression. */
14622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14625 = cp_parser_constant_expression (parser,
14626 /*allow_non_constant_p=*/true,
14628 if (!*non_constant_p)
14629 initializer = fold_non_dependent_expr (initializer);
14632 initializer = cp_parser_braced_list (parser, non_constant_p);
14634 return initializer;
14637 /* Parse a brace-enclosed initializer list.
14640 { initializer-list , [opt] }
14643 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14644 the elements of the initializer-list (or NULL, if the last
14645 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14646 NULL_TREE. There is no way to detect whether or not the optional
14647 trailing `,' was provided. NON_CONSTANT_P is as for
14648 cp_parser_initializer. */
14651 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14655 /* Consume the `{' token. */
14656 cp_lexer_consume_token (parser->lexer);
14657 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14658 initializer = make_node (CONSTRUCTOR);
14659 /* If it's not a `}', then there is a non-trivial initializer. */
14660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14662 /* Parse the initializer list. */
14663 CONSTRUCTOR_ELTS (initializer)
14664 = cp_parser_initializer_list (parser, non_constant_p);
14665 /* A trailing `,' token is allowed. */
14666 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14667 cp_lexer_consume_token (parser->lexer);
14669 /* Now, there should be a trailing `}'. */
14670 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14671 TREE_TYPE (initializer) = init_list_type_node;
14672 return initializer;
14675 /* Parse an initializer-list.
14678 initializer-clause ... [opt]
14679 initializer-list , initializer-clause ... [opt]
14684 identifier : initializer-clause
14685 initializer-list, identifier : initializer-clause
14687 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14688 for the initializer. If the INDEX of the elt is non-NULL, it is the
14689 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14690 as for cp_parser_initializer. */
14692 static VEC(constructor_elt,gc) *
14693 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14695 VEC(constructor_elt,gc) *v = NULL;
14697 /* Assume all of the expressions are constant. */
14698 *non_constant_p = false;
14700 /* Parse the rest of the list. */
14706 bool clause_non_constant_p;
14708 /* If the next token is an identifier and the following one is a
14709 colon, we are looking at the GNU designated-initializer
14711 if (cp_parser_allow_gnu_extensions_p (parser)
14712 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14713 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14715 /* Warn the user that they are using an extension. */
14716 pedwarn (input_location, OPT_pedantic,
14717 "ISO C++ does not allow designated initializers");
14718 /* Consume the identifier. */
14719 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14720 /* Consume the `:'. */
14721 cp_lexer_consume_token (parser->lexer);
14724 identifier = NULL_TREE;
14726 /* Parse the initializer. */
14727 initializer = cp_parser_initializer_clause (parser,
14728 &clause_non_constant_p);
14729 /* If any clause is non-constant, so is the entire initializer. */
14730 if (clause_non_constant_p)
14731 *non_constant_p = true;
14733 /* If we have an ellipsis, this is an initializer pack
14735 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14737 /* Consume the `...'. */
14738 cp_lexer_consume_token (parser->lexer);
14740 /* Turn the initializer into an initializer expansion. */
14741 initializer = make_pack_expansion (initializer);
14744 /* Add it to the vector. */
14745 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14747 /* If the next token is not a comma, we have reached the end of
14749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14752 /* Peek at the next token. */
14753 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14754 /* If the next token is a `}', then we're still done. An
14755 initializer-clause can have a trailing `,' after the
14756 initializer-list and before the closing `}'. */
14757 if (token->type == CPP_CLOSE_BRACE)
14760 /* Consume the `,' token. */
14761 cp_lexer_consume_token (parser->lexer);
14767 /* Classes [gram.class] */
14769 /* Parse a class-name.
14775 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14776 to indicate that names looked up in dependent types should be
14777 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14778 keyword has been used to indicate that the name that appears next
14779 is a template. TAG_TYPE indicates the explicit tag given before
14780 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14781 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14782 is the class being defined in a class-head.
14784 Returns the TYPE_DECL representing the class. */
14787 cp_parser_class_name (cp_parser *parser,
14788 bool typename_keyword_p,
14789 bool template_keyword_p,
14790 enum tag_types tag_type,
14791 bool check_dependency_p,
14793 bool is_declaration)
14799 tree identifier = NULL_TREE;
14801 /* All class-names start with an identifier. */
14802 token = cp_lexer_peek_token (parser->lexer);
14803 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14805 cp_parser_error (parser, "expected class-name");
14806 return error_mark_node;
14809 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14810 to a template-id, so we save it here. */
14811 scope = parser->scope;
14812 if (scope == error_mark_node)
14813 return error_mark_node;
14815 /* Any name names a type if we're following the `typename' keyword
14816 in a qualified name where the enclosing scope is type-dependent. */
14817 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14818 && dependent_type_p (scope));
14819 /* Handle the common case (an identifier, but not a template-id)
14821 if (token->type == CPP_NAME
14822 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14824 cp_token *identifier_token;
14827 /* Look for the identifier. */
14828 identifier_token = cp_lexer_peek_token (parser->lexer);
14829 ambiguous_p = identifier_token->ambiguous_p;
14830 identifier = cp_parser_identifier (parser);
14831 /* If the next token isn't an identifier, we are certainly not
14832 looking at a class-name. */
14833 if (identifier == error_mark_node)
14834 decl = error_mark_node;
14835 /* If we know this is a type-name, there's no need to look it
14837 else if (typename_p)
14841 tree ambiguous_decls;
14842 /* If we already know that this lookup is ambiguous, then
14843 we've already issued an error message; there's no reason
14847 cp_parser_simulate_error (parser);
14848 return error_mark_node;
14850 /* If the next token is a `::', then the name must be a type
14853 [basic.lookup.qual]
14855 During the lookup for a name preceding the :: scope
14856 resolution operator, object, function, and enumerator
14857 names are ignored. */
14858 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14859 tag_type = typename_type;
14860 /* Look up the name. */
14861 decl = cp_parser_lookup_name (parser, identifier,
14863 /*is_template=*/false,
14864 /*is_namespace=*/false,
14865 check_dependency_p,
14867 identifier_token->location);
14868 if (ambiguous_decls)
14870 error ("%Hreference to %qD is ambiguous",
14871 &identifier_token->location, identifier);
14872 print_candidates (ambiguous_decls);
14873 if (cp_parser_parsing_tentatively (parser))
14875 identifier_token->ambiguous_p = true;
14876 cp_parser_simulate_error (parser);
14878 return error_mark_node;
14884 /* Try a template-id. */
14885 decl = cp_parser_template_id (parser, template_keyword_p,
14886 check_dependency_p,
14888 if (decl == error_mark_node)
14889 return error_mark_node;
14892 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14894 /* If this is a typename, create a TYPENAME_TYPE. */
14895 if (typename_p && decl != error_mark_node)
14897 decl = make_typename_type (scope, decl, typename_type,
14898 /*complain=*/tf_error);
14899 if (decl != error_mark_node)
14900 decl = TYPE_NAME (decl);
14903 /* Check to see that it is really the name of a class. */
14904 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14905 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14906 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14907 /* Situations like this:
14909 template <typename T> struct A {
14910 typename T::template X<int>::I i;
14913 are problematic. Is `T::template X<int>' a class-name? The
14914 standard does not seem to be definitive, but there is no other
14915 valid interpretation of the following `::'. Therefore, those
14916 names are considered class-names. */
14918 decl = make_typename_type (scope, decl, tag_type, tf_error);
14919 if (decl != error_mark_node)
14920 decl = TYPE_NAME (decl);
14922 else if (TREE_CODE (decl) != TYPE_DECL
14923 || TREE_TYPE (decl) == error_mark_node
14924 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14925 decl = error_mark_node;
14927 if (decl == error_mark_node)
14928 cp_parser_error (parser, "expected class-name");
14929 else if (identifier && !parser->scope)
14930 maybe_note_name_used_in_class (identifier, decl);
14935 /* Parse a class-specifier.
14938 class-head { member-specification [opt] }
14940 Returns the TREE_TYPE representing the class. */
14943 cp_parser_class_specifier (cp_parser* parser)
14947 tree attributes = NULL_TREE;
14948 int has_trailing_semicolon;
14949 bool nested_name_specifier_p;
14950 unsigned saved_num_template_parameter_lists;
14951 bool saved_in_function_body;
14952 bool saved_in_unbraced_linkage_specification_p;
14953 tree old_scope = NULL_TREE;
14954 tree scope = NULL_TREE;
14957 push_deferring_access_checks (dk_no_deferred);
14959 /* Parse the class-head. */
14960 type = cp_parser_class_head (parser,
14961 &nested_name_specifier_p,
14964 /* If the class-head was a semantic disaster, skip the entire body
14968 cp_parser_skip_to_end_of_block_or_statement (parser);
14969 pop_deferring_access_checks ();
14970 return error_mark_node;
14973 /* Look for the `{'. */
14974 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14976 pop_deferring_access_checks ();
14977 return error_mark_node;
14980 /* Process the base classes. If they're invalid, skip the
14981 entire class body. */
14982 if (!xref_basetypes (type, bases))
14984 /* Consuming the closing brace yields better error messages
14986 if (cp_parser_skip_to_closing_brace (parser))
14987 cp_lexer_consume_token (parser->lexer);
14988 pop_deferring_access_checks ();
14989 return error_mark_node;
14992 /* Issue an error message if type-definitions are forbidden here. */
14993 cp_parser_check_type_definition (parser);
14994 /* Remember that we are defining one more class. */
14995 ++parser->num_classes_being_defined;
14996 /* Inside the class, surrounding template-parameter-lists do not
14998 saved_num_template_parameter_lists
14999 = parser->num_template_parameter_lists;
15000 parser->num_template_parameter_lists = 0;
15001 /* We are not in a function body. */
15002 saved_in_function_body = parser->in_function_body;
15003 parser->in_function_body = false;
15004 /* We are not immediately inside an extern "lang" block. */
15005 saved_in_unbraced_linkage_specification_p
15006 = parser->in_unbraced_linkage_specification_p;
15007 parser->in_unbraced_linkage_specification_p = false;
15009 /* Start the class. */
15010 if (nested_name_specifier_p)
15012 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15013 old_scope = push_inner_scope (scope);
15015 type = begin_class_definition (type, attributes);
15017 if (type == error_mark_node)
15018 /* If the type is erroneous, skip the entire body of the class. */
15019 cp_parser_skip_to_closing_brace (parser);
15021 /* Parse the member-specification. */
15022 cp_parser_member_specification_opt (parser);
15024 /* Look for the trailing `}'. */
15025 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15026 /* We get better error messages by noticing a common problem: a
15027 missing trailing `;'. */
15028 token = cp_lexer_peek_token (parser->lexer);
15029 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15030 /* Look for trailing attributes to apply to this class. */
15031 if (cp_parser_allow_gnu_extensions_p (parser))
15032 attributes = cp_parser_attributes_opt (parser);
15033 if (type != error_mark_node)
15034 type = finish_struct (type, attributes);
15035 if (nested_name_specifier_p)
15036 pop_inner_scope (old_scope, scope);
15037 /* If this class is not itself within the scope of another class,
15038 then we need to parse the bodies of all of the queued function
15039 definitions. Note that the queued functions defined in a class
15040 are not always processed immediately following the
15041 class-specifier for that class. Consider:
15044 struct B { void f() { sizeof (A); } };
15047 If `f' were processed before the processing of `A' were
15048 completed, there would be no way to compute the size of `A'.
15049 Note that the nesting we are interested in here is lexical --
15050 not the semantic nesting given by TYPE_CONTEXT. In particular,
15053 struct A { struct B; };
15054 struct A::B { void f() { } };
15056 there is no need to delay the parsing of `A::B::f'. */
15057 if (--parser->num_classes_being_defined == 0)
15061 tree class_type = NULL_TREE;
15062 tree pushed_scope = NULL_TREE;
15064 /* In a first pass, parse default arguments to the functions.
15065 Then, in a second pass, parse the bodies of the functions.
15066 This two-phased approach handles cases like:
15074 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15075 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15076 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15077 TREE_PURPOSE (parser->unparsed_functions_queues)
15078 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15080 fn = TREE_VALUE (queue_entry);
15081 /* If there are default arguments that have not yet been processed,
15082 take care of them now. */
15083 if (class_type != TREE_PURPOSE (queue_entry))
15086 pop_scope (pushed_scope);
15087 class_type = TREE_PURPOSE (queue_entry);
15088 pushed_scope = push_scope (class_type);
15090 /* Make sure that any template parameters are in scope. */
15091 maybe_begin_member_template_processing (fn);
15092 /* Parse the default argument expressions. */
15093 cp_parser_late_parsing_default_args (parser, fn);
15094 /* Remove any template parameters from the symbol table. */
15095 maybe_end_member_template_processing ();
15098 pop_scope (pushed_scope);
15099 /* Now parse the body of the functions. */
15100 for (TREE_VALUE (parser->unparsed_functions_queues)
15101 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15102 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15103 TREE_VALUE (parser->unparsed_functions_queues)
15104 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15106 /* Figure out which function we need to process. */
15107 fn = TREE_VALUE (queue_entry);
15108 /* Parse the function. */
15109 cp_parser_late_parsing_for_member (parser, fn);
15113 /* Put back any saved access checks. */
15114 pop_deferring_access_checks ();
15116 /* Restore saved state. */
15117 parser->in_function_body = saved_in_function_body;
15118 parser->num_template_parameter_lists
15119 = saved_num_template_parameter_lists;
15120 parser->in_unbraced_linkage_specification_p
15121 = saved_in_unbraced_linkage_specification_p;
15126 /* Parse a class-head.
15129 class-key identifier [opt] base-clause [opt]
15130 class-key nested-name-specifier identifier base-clause [opt]
15131 class-key nested-name-specifier [opt] template-id
15135 class-key attributes identifier [opt] base-clause [opt]
15136 class-key attributes nested-name-specifier identifier base-clause [opt]
15137 class-key attributes nested-name-specifier [opt] template-id
15140 Upon return BASES is initialized to the list of base classes (or
15141 NULL, if there are none) in the same form returned by
15142 cp_parser_base_clause.
15144 Returns the TYPE of the indicated class. Sets
15145 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15146 involving a nested-name-specifier was used, and FALSE otherwise.
15148 Returns error_mark_node if this is not a class-head.
15150 Returns NULL_TREE if the class-head is syntactically valid, but
15151 semantically invalid in a way that means we should skip the entire
15152 body of the class. */
15155 cp_parser_class_head (cp_parser* parser,
15156 bool* nested_name_specifier_p,
15157 tree *attributes_p,
15160 tree nested_name_specifier;
15161 enum tag_types class_key;
15162 tree id = NULL_TREE;
15163 tree type = NULL_TREE;
15165 bool template_id_p = false;
15166 bool qualified_p = false;
15167 bool invalid_nested_name_p = false;
15168 bool invalid_explicit_specialization_p = false;
15169 tree pushed_scope = NULL_TREE;
15170 unsigned num_templates;
15171 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15172 /* Assume no nested-name-specifier will be present. */
15173 *nested_name_specifier_p = false;
15174 /* Assume no template parameter lists will be used in defining the
15178 *bases = NULL_TREE;
15180 /* Look for the class-key. */
15181 class_key = cp_parser_class_key (parser);
15182 if (class_key == none_type)
15183 return error_mark_node;
15185 /* Parse the attributes. */
15186 attributes = cp_parser_attributes_opt (parser);
15188 /* If the next token is `::', that is invalid -- but sometimes
15189 people do try to write:
15193 Handle this gracefully by accepting the extra qualifier, and then
15194 issuing an error about it later if this really is a
15195 class-head. If it turns out just to be an elaborated type
15196 specifier, remain silent. */
15197 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15198 qualified_p = true;
15200 push_deferring_access_checks (dk_no_check);
15202 /* Determine the name of the class. Begin by looking for an
15203 optional nested-name-specifier. */
15204 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15205 nested_name_specifier
15206 = cp_parser_nested_name_specifier_opt (parser,
15207 /*typename_keyword_p=*/false,
15208 /*check_dependency_p=*/false,
15210 /*is_declaration=*/false);
15211 /* If there was a nested-name-specifier, then there *must* be an
15213 if (nested_name_specifier)
15215 type_start_token = cp_lexer_peek_token (parser->lexer);
15216 /* Although the grammar says `identifier', it really means
15217 `class-name' or `template-name'. You are only allowed to
15218 define a class that has already been declared with this
15221 The proposed resolution for Core Issue 180 says that wherever
15222 you see `class T::X' you should treat `X' as a type-name.
15224 It is OK to define an inaccessible class; for example:
15226 class A { class B; };
15229 We do not know if we will see a class-name, or a
15230 template-name. We look for a class-name first, in case the
15231 class-name is a template-id; if we looked for the
15232 template-name first we would stop after the template-name. */
15233 cp_parser_parse_tentatively (parser);
15234 type = cp_parser_class_name (parser,
15235 /*typename_keyword_p=*/false,
15236 /*template_keyword_p=*/false,
15238 /*check_dependency_p=*/false,
15239 /*class_head_p=*/true,
15240 /*is_declaration=*/false);
15241 /* If that didn't work, ignore the nested-name-specifier. */
15242 if (!cp_parser_parse_definitely (parser))
15244 invalid_nested_name_p = true;
15245 type_start_token = cp_lexer_peek_token (parser->lexer);
15246 id = cp_parser_identifier (parser);
15247 if (id == error_mark_node)
15250 /* If we could not find a corresponding TYPE, treat this
15251 declaration like an unqualified declaration. */
15252 if (type == error_mark_node)
15253 nested_name_specifier = NULL_TREE;
15254 /* Otherwise, count the number of templates used in TYPE and its
15255 containing scopes. */
15260 for (scope = TREE_TYPE (type);
15261 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15262 scope = (TYPE_P (scope)
15263 ? TYPE_CONTEXT (scope)
15264 : DECL_CONTEXT (scope)))
15266 && CLASS_TYPE_P (scope)
15267 && CLASSTYPE_TEMPLATE_INFO (scope)
15268 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15269 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15273 /* Otherwise, the identifier is optional. */
15276 /* We don't know whether what comes next is a template-id,
15277 an identifier, or nothing at all. */
15278 cp_parser_parse_tentatively (parser);
15279 /* Check for a template-id. */
15280 type_start_token = cp_lexer_peek_token (parser->lexer);
15281 id = cp_parser_template_id (parser,
15282 /*template_keyword_p=*/false,
15283 /*check_dependency_p=*/true,
15284 /*is_declaration=*/true);
15285 /* If that didn't work, it could still be an identifier. */
15286 if (!cp_parser_parse_definitely (parser))
15288 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15290 type_start_token = cp_lexer_peek_token (parser->lexer);
15291 id = cp_parser_identifier (parser);
15298 template_id_p = true;
15303 pop_deferring_access_checks ();
15306 cp_parser_check_for_invalid_template_id (parser, id,
15307 type_start_token->location);
15309 /* If it's not a `:' or a `{' then we can't really be looking at a
15310 class-head, since a class-head only appears as part of a
15311 class-specifier. We have to detect this situation before calling
15312 xref_tag, since that has irreversible side-effects. */
15313 if (!cp_parser_next_token_starts_class_definition_p (parser))
15315 cp_parser_error (parser, "expected %<{%> or %<:%>");
15316 return error_mark_node;
15319 /* At this point, we're going ahead with the class-specifier, even
15320 if some other problem occurs. */
15321 cp_parser_commit_to_tentative_parse (parser);
15322 /* Issue the error about the overly-qualified name now. */
15325 cp_parser_error (parser,
15326 "global qualification of class name is invalid");
15327 return error_mark_node;
15329 else if (invalid_nested_name_p)
15331 cp_parser_error (parser,
15332 "qualified name does not name a class");
15333 return error_mark_node;
15335 else if (nested_name_specifier)
15339 /* Reject typedef-names in class heads. */
15340 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15342 error ("%Hinvalid class name in declaration of %qD",
15343 &type_start_token->location, type);
15348 /* Figure out in what scope the declaration is being placed. */
15349 scope = current_scope ();
15350 /* If that scope does not contain the scope in which the
15351 class was originally declared, the program is invalid. */
15352 if (scope && !is_ancestor (scope, nested_name_specifier))
15354 if (at_namespace_scope_p ())
15355 error ("%Hdeclaration of %qD in namespace %qD which does not "
15357 &type_start_token->location,
15358 type, scope, nested_name_specifier);
15360 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15361 &type_start_token->location,
15362 type, scope, nested_name_specifier);
15368 A declarator-id shall not be qualified except for the
15369 definition of a ... nested class outside of its class
15370 ... [or] the definition or explicit instantiation of a
15371 class member of a namespace outside of its namespace. */
15372 if (scope == nested_name_specifier)
15374 permerror (input_location, "%Hextra qualification not allowed",
15375 &nested_name_specifier_token_start->location);
15376 nested_name_specifier = NULL_TREE;
15380 /* An explicit-specialization must be preceded by "template <>". If
15381 it is not, try to recover gracefully. */
15382 if (at_namespace_scope_p ()
15383 && parser->num_template_parameter_lists == 0
15386 error ("%Han explicit specialization must be preceded by %<template <>%>",
15387 &type_start_token->location);
15388 invalid_explicit_specialization_p = true;
15389 /* Take the same action that would have been taken by
15390 cp_parser_explicit_specialization. */
15391 ++parser->num_template_parameter_lists;
15392 begin_specialization ();
15394 /* There must be no "return" statements between this point and the
15395 end of this function; set "type "to the correct return value and
15396 use "goto done;" to return. */
15397 /* Make sure that the right number of template parameters were
15399 if (!cp_parser_check_template_parameters (parser, num_templates,
15400 type_start_token->location))
15402 /* If something went wrong, there is no point in even trying to
15403 process the class-definition. */
15408 /* Look up the type. */
15411 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15412 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15413 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15415 error ("%Hfunction template %qD redeclared as a class template",
15416 &type_start_token->location, id);
15417 type = error_mark_node;
15421 type = TREE_TYPE (id);
15422 type = maybe_process_partial_specialization (type);
15424 if (nested_name_specifier)
15425 pushed_scope = push_scope (nested_name_specifier);
15427 else if (nested_name_specifier)
15433 template <typename T> struct S { struct T };
15434 template <typename T> struct S<T>::T { };
15436 we will get a TYPENAME_TYPE when processing the definition of
15437 `S::T'. We need to resolve it to the actual type before we
15438 try to define it. */
15439 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15441 class_type = resolve_typename_type (TREE_TYPE (type),
15442 /*only_current_p=*/false);
15443 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15444 type = TYPE_NAME (class_type);
15447 cp_parser_error (parser, "could not resolve typename type");
15448 type = error_mark_node;
15452 if (maybe_process_partial_specialization (TREE_TYPE (type))
15453 == error_mark_node)
15459 class_type = current_class_type;
15460 /* Enter the scope indicated by the nested-name-specifier. */
15461 pushed_scope = push_scope (nested_name_specifier);
15462 /* Get the canonical version of this type. */
15463 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15464 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15465 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15467 type = push_template_decl (type);
15468 if (type == error_mark_node)
15475 type = TREE_TYPE (type);
15476 *nested_name_specifier_p = true;
15478 else /* The name is not a nested name. */
15480 /* If the class was unnamed, create a dummy name. */
15482 id = make_anon_name ();
15483 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15484 parser->num_template_parameter_lists);
15487 /* Indicate whether this class was declared as a `class' or as a
15489 if (TREE_CODE (type) == RECORD_TYPE)
15490 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15491 cp_parser_check_class_key (class_key, type);
15493 /* If this type was already complete, and we see another definition,
15494 that's an error. */
15495 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15497 error ("%Hredefinition of %q#T",
15498 &type_start_token->location, type);
15499 error ("%Hprevious definition of %q+#T",
15500 &type_start_token->location, type);
15504 else if (type == error_mark_node)
15507 /* We will have entered the scope containing the class; the names of
15508 base classes should be looked up in that context. For example:
15510 struct A { struct B {}; struct C; };
15511 struct A::C : B {};
15515 /* Get the list of base-classes, if there is one. */
15516 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15517 *bases = cp_parser_base_clause (parser);
15520 /* Leave the scope given by the nested-name-specifier. We will
15521 enter the class scope itself while processing the members. */
15523 pop_scope (pushed_scope);
15525 if (invalid_explicit_specialization_p)
15527 end_specialization ();
15528 --parser->num_template_parameter_lists;
15530 *attributes_p = attributes;
15534 /* Parse a class-key.
15541 Returns the kind of class-key specified, or none_type to indicate
15544 static enum tag_types
15545 cp_parser_class_key (cp_parser* parser)
15548 enum tag_types tag_type;
15550 /* Look for the class-key. */
15551 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15555 /* Check to see if the TOKEN is a class-key. */
15556 tag_type = cp_parser_token_is_class_key (token);
15558 cp_parser_error (parser, "expected class-key");
15562 /* Parse an (optional) member-specification.
15564 member-specification:
15565 member-declaration member-specification [opt]
15566 access-specifier : member-specification [opt] */
15569 cp_parser_member_specification_opt (cp_parser* parser)
15576 /* Peek at the next token. */
15577 token = cp_lexer_peek_token (parser->lexer);
15578 /* If it's a `}', or EOF then we've seen all the members. */
15579 if (token->type == CPP_CLOSE_BRACE
15580 || token->type == CPP_EOF
15581 || token->type == CPP_PRAGMA_EOL)
15584 /* See if this token is a keyword. */
15585 keyword = token->keyword;
15589 case RID_PROTECTED:
15591 /* Consume the access-specifier. */
15592 cp_lexer_consume_token (parser->lexer);
15593 /* Remember which access-specifier is active. */
15594 current_access_specifier = token->u.value;
15595 /* Look for the `:'. */
15596 cp_parser_require (parser, CPP_COLON, "%<:%>");
15600 /* Accept #pragmas at class scope. */
15601 if (token->type == CPP_PRAGMA)
15603 cp_parser_pragma (parser, pragma_external);
15607 /* Otherwise, the next construction must be a
15608 member-declaration. */
15609 cp_parser_member_declaration (parser);
15614 /* Parse a member-declaration.
15616 member-declaration:
15617 decl-specifier-seq [opt] member-declarator-list [opt] ;
15618 function-definition ; [opt]
15619 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15621 template-declaration
15623 member-declarator-list:
15625 member-declarator-list , member-declarator
15628 declarator pure-specifier [opt]
15629 declarator constant-initializer [opt]
15630 identifier [opt] : constant-expression
15634 member-declaration:
15635 __extension__ member-declaration
15638 declarator attributes [opt] pure-specifier [opt]
15639 declarator attributes [opt] constant-initializer [opt]
15640 identifier [opt] attributes [opt] : constant-expression
15644 member-declaration:
15645 static_assert-declaration */
15648 cp_parser_member_declaration (cp_parser* parser)
15650 cp_decl_specifier_seq decl_specifiers;
15651 tree prefix_attributes;
15653 int declares_class_or_enum;
15655 cp_token *token = NULL;
15656 cp_token *decl_spec_token_start = NULL;
15657 cp_token *initializer_token_start = NULL;
15658 int saved_pedantic;
15660 /* Check for the `__extension__' keyword. */
15661 if (cp_parser_extension_opt (parser, &saved_pedantic))
15664 cp_parser_member_declaration (parser);
15665 /* Restore the old value of the PEDANTIC flag. */
15666 pedantic = saved_pedantic;
15671 /* Check for a template-declaration. */
15672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15674 /* An explicit specialization here is an error condition, and we
15675 expect the specialization handler to detect and report this. */
15676 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15677 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15678 cp_parser_explicit_specialization (parser);
15680 cp_parser_template_declaration (parser, /*member_p=*/true);
15685 /* Check for a using-declaration. */
15686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15688 /* Parse the using-declaration. */
15689 cp_parser_using_declaration (parser,
15690 /*access_declaration_p=*/false);
15694 /* Check for @defs. */
15695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15698 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15699 ivar = ivar_chains;
15703 ivar = TREE_CHAIN (member);
15704 TREE_CHAIN (member) = NULL_TREE;
15705 finish_member_declaration (member);
15710 /* If the next token is `static_assert' we have a static assertion. */
15711 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15713 cp_parser_static_assert (parser, /*member_p=*/true);
15717 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15720 /* Parse the decl-specifier-seq. */
15721 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15722 cp_parser_decl_specifier_seq (parser,
15723 CP_PARSER_FLAGS_OPTIONAL,
15725 &declares_class_or_enum);
15726 prefix_attributes = decl_specifiers.attributes;
15727 decl_specifiers.attributes = NULL_TREE;
15728 /* Check for an invalid type-name. */
15729 if (!decl_specifiers.type
15730 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15732 /* If there is no declarator, then the decl-specifier-seq should
15734 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15736 /* If there was no decl-specifier-seq, and the next token is a
15737 `;', then we have something like:
15743 Each member-declaration shall declare at least one member
15744 name of the class. */
15745 if (!decl_specifiers.any_specifiers_p)
15747 cp_token *token = cp_lexer_peek_token (parser->lexer);
15748 if (!in_system_header_at (token->location))
15749 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15755 /* See if this declaration is a friend. */
15756 friend_p = cp_parser_friend_p (&decl_specifiers);
15757 /* If there were decl-specifiers, check to see if there was
15758 a class-declaration. */
15759 type = check_tag_decl (&decl_specifiers);
15760 /* Nested classes have already been added to the class, but
15761 a `friend' needs to be explicitly registered. */
15764 /* If the `friend' keyword was present, the friend must
15765 be introduced with a class-key. */
15766 if (!declares_class_or_enum)
15767 error ("%Ha class-key must be used when declaring a friend",
15768 &decl_spec_token_start->location);
15771 template <typename T> struct A {
15772 friend struct A<T>::B;
15775 A<T>::B will be represented by a TYPENAME_TYPE, and
15776 therefore not recognized by check_tag_decl. */
15778 && decl_specifiers.type
15779 && TYPE_P (decl_specifiers.type))
15780 type = decl_specifiers.type;
15781 if (!type || !TYPE_P (type))
15782 error ("%Hfriend declaration does not name a class or "
15783 "function", &decl_spec_token_start->location);
15785 make_friend_class (current_class_type, type,
15786 /*complain=*/true);
15788 /* If there is no TYPE, an error message will already have
15790 else if (!type || type == error_mark_node)
15792 /* An anonymous aggregate has to be handled specially; such
15793 a declaration really declares a data member (with a
15794 particular type), as opposed to a nested class. */
15795 else if (ANON_AGGR_TYPE_P (type))
15797 /* Remove constructors and such from TYPE, now that we
15798 know it is an anonymous aggregate. */
15799 fixup_anonymous_aggr (type);
15800 /* And make the corresponding data member. */
15801 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15802 /* Add it to the class. */
15803 finish_member_declaration (decl);
15806 cp_parser_check_access_in_redeclaration
15808 decl_spec_token_start->location);
15813 /* See if these declarations will be friends. */
15814 friend_p = cp_parser_friend_p (&decl_specifiers);
15816 /* Keep going until we hit the `;' at the end of the
15818 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15820 tree attributes = NULL_TREE;
15821 tree first_attribute;
15823 /* Peek at the next token. */
15824 token = cp_lexer_peek_token (parser->lexer);
15826 /* Check for a bitfield declaration. */
15827 if (token->type == CPP_COLON
15828 || (token->type == CPP_NAME
15829 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15835 /* Get the name of the bitfield. Note that we cannot just
15836 check TOKEN here because it may have been invalidated by
15837 the call to cp_lexer_peek_nth_token above. */
15838 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15839 identifier = cp_parser_identifier (parser);
15841 identifier = NULL_TREE;
15843 /* Consume the `:' token. */
15844 cp_lexer_consume_token (parser->lexer);
15845 /* Get the width of the bitfield. */
15847 = cp_parser_constant_expression (parser,
15848 /*allow_non_constant=*/false,
15851 /* Look for attributes that apply to the bitfield. */
15852 attributes = cp_parser_attributes_opt (parser);
15853 /* Remember which attributes are prefix attributes and
15855 first_attribute = attributes;
15856 /* Combine the attributes. */
15857 attributes = chainon (prefix_attributes, attributes);
15859 /* Create the bitfield declaration. */
15860 decl = grokbitfield (identifier
15861 ? make_id_declarator (NULL_TREE,
15871 cp_declarator *declarator;
15873 tree asm_specification;
15874 int ctor_dtor_or_conv_p;
15876 /* Parse the declarator. */
15878 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15879 &ctor_dtor_or_conv_p,
15880 /*parenthesized_p=*/NULL,
15881 /*member_p=*/true);
15883 /* If something went wrong parsing the declarator, make sure
15884 that we at least consume some tokens. */
15885 if (declarator == cp_error_declarator)
15887 /* Skip to the end of the statement. */
15888 cp_parser_skip_to_end_of_statement (parser);
15889 /* If the next token is not a semicolon, that is
15890 probably because we just skipped over the body of
15891 a function. So, we consume a semicolon if
15892 present, but do not issue an error message if it
15894 if (cp_lexer_next_token_is (parser->lexer,
15896 cp_lexer_consume_token (parser->lexer);
15900 if (declares_class_or_enum & 2)
15901 cp_parser_check_for_definition_in_return_type
15902 (declarator, decl_specifiers.type,
15903 decl_specifiers.type_location);
15905 /* Look for an asm-specification. */
15906 asm_specification = cp_parser_asm_specification_opt (parser);
15907 /* Look for attributes that apply to the declaration. */
15908 attributes = cp_parser_attributes_opt (parser);
15909 /* Remember which attributes are prefix attributes and
15911 first_attribute = attributes;
15912 /* Combine the attributes. */
15913 attributes = chainon (prefix_attributes, attributes);
15915 /* If it's an `=', then we have a constant-initializer or a
15916 pure-specifier. It is not correct to parse the
15917 initializer before registering the member declaration
15918 since the member declaration should be in scope while
15919 its initializer is processed. However, the rest of the
15920 front end does not yet provide an interface that allows
15921 us to handle this correctly. */
15922 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15926 A pure-specifier shall be used only in the declaration of
15927 a virtual function.
15929 A member-declarator can contain a constant-initializer
15930 only if it declares a static member of integral or
15933 Therefore, if the DECLARATOR is for a function, we look
15934 for a pure-specifier; otherwise, we look for a
15935 constant-initializer. When we call `grokfield', it will
15936 perform more stringent semantics checks. */
15937 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15938 if (function_declarator_p (declarator))
15939 initializer = cp_parser_pure_specifier (parser);
15941 /* Parse the initializer. */
15942 initializer = cp_parser_constant_initializer (parser);
15944 /* Otherwise, there is no initializer. */
15946 initializer = NULL_TREE;
15948 /* See if we are probably looking at a function
15949 definition. We are certainly not looking at a
15950 member-declarator. Calling `grokfield' has
15951 side-effects, so we must not do it unless we are sure
15952 that we are looking at a member-declarator. */
15953 if (cp_parser_token_starts_function_definition_p
15954 (cp_lexer_peek_token (parser->lexer)))
15956 /* The grammar does not allow a pure-specifier to be
15957 used when a member function is defined. (It is
15958 possible that this fact is an oversight in the
15959 standard, since a pure function may be defined
15960 outside of the class-specifier. */
15962 error ("%Hpure-specifier on function-definition",
15963 &initializer_token_start->location);
15964 decl = cp_parser_save_member_function_body (parser,
15968 /* If the member was not a friend, declare it here. */
15970 finish_member_declaration (decl);
15971 /* Peek at the next token. */
15972 token = cp_lexer_peek_token (parser->lexer);
15973 /* If the next token is a semicolon, consume it. */
15974 if (token->type == CPP_SEMICOLON)
15975 cp_lexer_consume_token (parser->lexer);
15979 if (declarator->kind == cdk_function)
15980 declarator->id_loc = token->location;
15981 /* Create the declaration. */
15982 decl = grokfield (declarator, &decl_specifiers,
15983 initializer, /*init_const_expr_p=*/true,
15988 /* Reset PREFIX_ATTRIBUTES. */
15989 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15990 attributes = TREE_CHAIN (attributes);
15992 TREE_CHAIN (attributes) = NULL_TREE;
15994 /* If there is any qualification still in effect, clear it
15995 now; we will be starting fresh with the next declarator. */
15996 parser->scope = NULL_TREE;
15997 parser->qualifying_scope = NULL_TREE;
15998 parser->object_scope = NULL_TREE;
15999 /* If it's a `,', then there are more declarators. */
16000 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16001 cp_lexer_consume_token (parser->lexer);
16002 /* If the next token isn't a `;', then we have a parse error. */
16003 else if (cp_lexer_next_token_is_not (parser->lexer,
16006 cp_parser_error (parser, "expected %<;%>");
16007 /* Skip tokens until we find a `;'. */
16008 cp_parser_skip_to_end_of_statement (parser);
16015 /* Add DECL to the list of members. */
16017 finish_member_declaration (decl);
16019 if (TREE_CODE (decl) == FUNCTION_DECL)
16020 cp_parser_save_default_args (parser, decl);
16025 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16028 /* Parse a pure-specifier.
16033 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16034 Otherwise, ERROR_MARK_NODE is returned. */
16037 cp_parser_pure_specifier (cp_parser* parser)
16041 /* Look for the `=' token. */
16042 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16043 return error_mark_node;
16044 /* Look for the `0' token. */
16045 token = cp_lexer_peek_token (parser->lexer);
16047 if (token->type == CPP_EOF
16048 || token->type == CPP_PRAGMA_EOL)
16049 return error_mark_node;
16051 cp_lexer_consume_token (parser->lexer);
16053 /* Accept = default or = delete in c++0x mode. */
16054 if (token->keyword == RID_DEFAULT
16055 || token->keyword == RID_DELETE)
16057 maybe_warn_cpp0x ("defaulted and deleted functions");
16058 return token->u.value;
16061 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16062 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16064 cp_parser_error (parser,
16065 "invalid pure specifier (only %<= 0%> is allowed)");
16066 cp_parser_skip_to_end_of_statement (parser);
16067 return error_mark_node;
16069 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16071 error ("%Htemplates may not be %<virtual%>", &token->location);
16072 return error_mark_node;
16075 return integer_zero_node;
16078 /* Parse a constant-initializer.
16080 constant-initializer:
16081 = constant-expression
16083 Returns a representation of the constant-expression. */
16086 cp_parser_constant_initializer (cp_parser* parser)
16088 /* Look for the `=' token. */
16089 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16090 return error_mark_node;
16092 /* It is invalid to write:
16094 struct S { static const int i = { 7 }; };
16097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16099 cp_parser_error (parser,
16100 "a brace-enclosed initializer is not allowed here");
16101 /* Consume the opening brace. */
16102 cp_lexer_consume_token (parser->lexer);
16103 /* Skip the initializer. */
16104 cp_parser_skip_to_closing_brace (parser);
16105 /* Look for the trailing `}'. */
16106 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16108 return error_mark_node;
16111 return cp_parser_constant_expression (parser,
16112 /*allow_non_constant=*/false,
16116 /* Derived classes [gram.class.derived] */
16118 /* Parse a base-clause.
16121 : base-specifier-list
16123 base-specifier-list:
16124 base-specifier ... [opt]
16125 base-specifier-list , base-specifier ... [opt]
16127 Returns a TREE_LIST representing the base-classes, in the order in
16128 which they were declared. The representation of each node is as
16129 described by cp_parser_base_specifier.
16131 In the case that no bases are specified, this function will return
16132 NULL_TREE, not ERROR_MARK_NODE. */
16135 cp_parser_base_clause (cp_parser* parser)
16137 tree bases = NULL_TREE;
16139 /* Look for the `:' that begins the list. */
16140 cp_parser_require (parser, CPP_COLON, "%<:%>");
16142 /* Scan the base-specifier-list. */
16147 bool pack_expansion_p = false;
16149 /* Look for the base-specifier. */
16150 base = cp_parser_base_specifier (parser);
16151 /* Look for the (optional) ellipsis. */
16152 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16154 /* Consume the `...'. */
16155 cp_lexer_consume_token (parser->lexer);
16157 pack_expansion_p = true;
16160 /* Add BASE to the front of the list. */
16161 if (base != error_mark_node)
16163 if (pack_expansion_p)
16164 /* Make this a pack expansion type. */
16165 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16168 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16170 TREE_CHAIN (base) = bases;
16174 /* Peek at the next token. */
16175 token = cp_lexer_peek_token (parser->lexer);
16176 /* If it's not a comma, then the list is complete. */
16177 if (token->type != CPP_COMMA)
16179 /* Consume the `,'. */
16180 cp_lexer_consume_token (parser->lexer);
16183 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16184 base class had a qualified name. However, the next name that
16185 appears is certainly not qualified. */
16186 parser->scope = NULL_TREE;
16187 parser->qualifying_scope = NULL_TREE;
16188 parser->object_scope = NULL_TREE;
16190 return nreverse (bases);
16193 /* Parse a base-specifier.
16196 :: [opt] nested-name-specifier [opt] class-name
16197 virtual access-specifier [opt] :: [opt] nested-name-specifier
16199 access-specifier virtual [opt] :: [opt] nested-name-specifier
16202 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16203 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16204 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16205 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16208 cp_parser_base_specifier (cp_parser* parser)
16212 bool virtual_p = false;
16213 bool duplicate_virtual_error_issued_p = false;
16214 bool duplicate_access_error_issued_p = false;
16215 bool class_scope_p, template_p;
16216 tree access = access_default_node;
16219 /* Process the optional `virtual' and `access-specifier'. */
16222 /* Peek at the next token. */
16223 token = cp_lexer_peek_token (parser->lexer);
16224 /* Process `virtual'. */
16225 switch (token->keyword)
16228 /* If `virtual' appears more than once, issue an error. */
16229 if (virtual_p && !duplicate_virtual_error_issued_p)
16231 cp_parser_error (parser,
16232 "%<virtual%> specified more than once in base-specified");
16233 duplicate_virtual_error_issued_p = true;
16238 /* Consume the `virtual' token. */
16239 cp_lexer_consume_token (parser->lexer);
16244 case RID_PROTECTED:
16246 /* If more than one access specifier appears, issue an
16248 if (access != access_default_node
16249 && !duplicate_access_error_issued_p)
16251 cp_parser_error (parser,
16252 "more than one access specifier in base-specified");
16253 duplicate_access_error_issued_p = true;
16256 access = ridpointers[(int) token->keyword];
16258 /* Consume the access-specifier. */
16259 cp_lexer_consume_token (parser->lexer);
16268 /* It is not uncommon to see programs mechanically, erroneously, use
16269 the 'typename' keyword to denote (dependent) qualified types
16270 as base classes. */
16271 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16273 token = cp_lexer_peek_token (parser->lexer);
16274 if (!processing_template_decl)
16275 error ("%Hkeyword %<typename%> not allowed outside of templates",
16278 error ("%Hkeyword %<typename%> not allowed in this context "
16279 "(the base class is implicitly a type)",
16281 cp_lexer_consume_token (parser->lexer);
16284 /* Look for the optional `::' operator. */
16285 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16286 /* Look for the nested-name-specifier. The simplest way to
16291 The keyword `typename' is not permitted in a base-specifier or
16292 mem-initializer; in these contexts a qualified name that
16293 depends on a template-parameter is implicitly assumed to be a
16296 is to pretend that we have seen the `typename' keyword at this
16298 cp_parser_nested_name_specifier_opt (parser,
16299 /*typename_keyword_p=*/true,
16300 /*check_dependency_p=*/true,
16302 /*is_declaration=*/true);
16303 /* If the base class is given by a qualified name, assume that names
16304 we see are type names or templates, as appropriate. */
16305 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16306 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16308 /* Finally, look for the class-name. */
16309 type = cp_parser_class_name (parser,
16313 /*check_dependency_p=*/true,
16314 /*class_head_p=*/false,
16315 /*is_declaration=*/true);
16317 if (type == error_mark_node)
16318 return error_mark_node;
16320 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16323 /* Exception handling [gram.exception] */
16325 /* Parse an (optional) exception-specification.
16327 exception-specification:
16328 throw ( type-id-list [opt] )
16330 Returns a TREE_LIST representing the exception-specification. The
16331 TREE_VALUE of each node is a type. */
16334 cp_parser_exception_specification_opt (cp_parser* parser)
16339 /* Peek at the next token. */
16340 token = cp_lexer_peek_token (parser->lexer);
16341 /* If it's not `throw', then there's no exception-specification. */
16342 if (!cp_parser_is_keyword (token, RID_THROW))
16345 /* Consume the `throw'. */
16346 cp_lexer_consume_token (parser->lexer);
16348 /* Look for the `('. */
16349 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16351 /* Peek at the next token. */
16352 token = cp_lexer_peek_token (parser->lexer);
16353 /* If it's not a `)', then there is a type-id-list. */
16354 if (token->type != CPP_CLOSE_PAREN)
16356 const char *saved_message;
16358 /* Types may not be defined in an exception-specification. */
16359 saved_message = parser->type_definition_forbidden_message;
16360 parser->type_definition_forbidden_message
16361 = "types may not be defined in an exception-specification";
16362 /* Parse the type-id-list. */
16363 type_id_list = cp_parser_type_id_list (parser);
16364 /* Restore the saved message. */
16365 parser->type_definition_forbidden_message = saved_message;
16368 type_id_list = empty_except_spec;
16370 /* Look for the `)'. */
16371 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16373 return type_id_list;
16376 /* Parse an (optional) type-id-list.
16380 type-id-list , type-id ... [opt]
16382 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16383 in the order that the types were presented. */
16386 cp_parser_type_id_list (cp_parser* parser)
16388 tree types = NULL_TREE;
16395 /* Get the next type-id. */
16396 type = cp_parser_type_id (parser);
16397 /* Parse the optional ellipsis. */
16398 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16400 /* Consume the `...'. */
16401 cp_lexer_consume_token (parser->lexer);
16403 /* Turn the type into a pack expansion expression. */
16404 type = make_pack_expansion (type);
16406 /* Add it to the list. */
16407 types = add_exception_specifier (types, type, /*complain=*/1);
16408 /* Peek at the next token. */
16409 token = cp_lexer_peek_token (parser->lexer);
16410 /* If it is not a `,', we are done. */
16411 if (token->type != CPP_COMMA)
16413 /* Consume the `,'. */
16414 cp_lexer_consume_token (parser->lexer);
16417 return nreverse (types);
16420 /* Parse a try-block.
16423 try compound-statement handler-seq */
16426 cp_parser_try_block (cp_parser* parser)
16430 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16431 try_block = begin_try_block ();
16432 cp_parser_compound_statement (parser, NULL, true);
16433 finish_try_block (try_block);
16434 cp_parser_handler_seq (parser);
16435 finish_handler_sequence (try_block);
16440 /* Parse a function-try-block.
16442 function-try-block:
16443 try ctor-initializer [opt] function-body handler-seq */
16446 cp_parser_function_try_block (cp_parser* parser)
16448 tree compound_stmt;
16450 bool ctor_initializer_p;
16452 /* Look for the `try' keyword. */
16453 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16455 /* Let the rest of the front end know where we are. */
16456 try_block = begin_function_try_block (&compound_stmt);
16457 /* Parse the function-body. */
16459 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16460 /* We're done with the `try' part. */
16461 finish_function_try_block (try_block);
16462 /* Parse the handlers. */
16463 cp_parser_handler_seq (parser);
16464 /* We're done with the handlers. */
16465 finish_function_handler_sequence (try_block, compound_stmt);
16467 return ctor_initializer_p;
16470 /* Parse a handler-seq.
16473 handler handler-seq [opt] */
16476 cp_parser_handler_seq (cp_parser* parser)
16482 /* Parse the handler. */
16483 cp_parser_handler (parser);
16484 /* Peek at the next token. */
16485 token = cp_lexer_peek_token (parser->lexer);
16486 /* If it's not `catch' then there are no more handlers. */
16487 if (!cp_parser_is_keyword (token, RID_CATCH))
16492 /* Parse a handler.
16495 catch ( exception-declaration ) compound-statement */
16498 cp_parser_handler (cp_parser* parser)
16503 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16504 handler = begin_handler ();
16505 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16506 declaration = cp_parser_exception_declaration (parser);
16507 finish_handler_parms (declaration, handler);
16508 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16509 cp_parser_compound_statement (parser, NULL, false);
16510 finish_handler (handler);
16513 /* Parse an exception-declaration.
16515 exception-declaration:
16516 type-specifier-seq declarator
16517 type-specifier-seq abstract-declarator
16521 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16522 ellipsis variant is used. */
16525 cp_parser_exception_declaration (cp_parser* parser)
16527 cp_decl_specifier_seq type_specifiers;
16528 cp_declarator *declarator;
16529 const char *saved_message;
16531 /* If it's an ellipsis, it's easy to handle. */
16532 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16534 /* Consume the `...' token. */
16535 cp_lexer_consume_token (parser->lexer);
16539 /* Types may not be defined in exception-declarations. */
16540 saved_message = parser->type_definition_forbidden_message;
16541 parser->type_definition_forbidden_message
16542 = "types may not be defined in exception-declarations";
16544 /* Parse the type-specifier-seq. */
16545 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16547 /* If it's a `)', then there is no declarator. */
16548 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16551 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16552 /*ctor_dtor_or_conv_p=*/NULL,
16553 /*parenthesized_p=*/NULL,
16554 /*member_p=*/false);
16556 /* Restore the saved message. */
16557 parser->type_definition_forbidden_message = saved_message;
16559 if (!type_specifiers.any_specifiers_p)
16560 return error_mark_node;
16562 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16565 /* Parse a throw-expression.
16568 throw assignment-expression [opt]
16570 Returns a THROW_EXPR representing the throw-expression. */
16573 cp_parser_throw_expression (cp_parser* parser)
16578 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16579 token = cp_lexer_peek_token (parser->lexer);
16580 /* Figure out whether or not there is an assignment-expression
16581 following the "throw" keyword. */
16582 if (token->type == CPP_COMMA
16583 || token->type == CPP_SEMICOLON
16584 || token->type == CPP_CLOSE_PAREN
16585 || token->type == CPP_CLOSE_SQUARE
16586 || token->type == CPP_CLOSE_BRACE
16587 || token->type == CPP_COLON)
16588 expression = NULL_TREE;
16590 expression = cp_parser_assignment_expression (parser,
16591 /*cast_p=*/false, NULL);
16593 return build_throw (expression);
16596 /* GNU Extensions */
16598 /* Parse an (optional) asm-specification.
16601 asm ( string-literal )
16603 If the asm-specification is present, returns a STRING_CST
16604 corresponding to the string-literal. Otherwise, returns
16608 cp_parser_asm_specification_opt (cp_parser* parser)
16611 tree asm_specification;
16613 /* Peek at the next token. */
16614 token = cp_lexer_peek_token (parser->lexer);
16615 /* If the next token isn't the `asm' keyword, then there's no
16616 asm-specification. */
16617 if (!cp_parser_is_keyword (token, RID_ASM))
16620 /* Consume the `asm' token. */
16621 cp_lexer_consume_token (parser->lexer);
16622 /* Look for the `('. */
16623 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16625 /* Look for the string-literal. */
16626 asm_specification = cp_parser_string_literal (parser, false, false);
16628 /* Look for the `)'. */
16629 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16631 return asm_specification;
16634 /* Parse an asm-operand-list.
16638 asm-operand-list , asm-operand
16641 string-literal ( expression )
16642 [ string-literal ] string-literal ( expression )
16644 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16645 each node is the expression. The TREE_PURPOSE is itself a
16646 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16647 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16648 is a STRING_CST for the string literal before the parenthesis. Returns
16649 ERROR_MARK_NODE if any of the operands are invalid. */
16652 cp_parser_asm_operand_list (cp_parser* parser)
16654 tree asm_operands = NULL_TREE;
16655 bool invalid_operands = false;
16659 tree string_literal;
16663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16665 /* Consume the `[' token. */
16666 cp_lexer_consume_token (parser->lexer);
16667 /* Read the operand name. */
16668 name = cp_parser_identifier (parser);
16669 if (name != error_mark_node)
16670 name = build_string (IDENTIFIER_LENGTH (name),
16671 IDENTIFIER_POINTER (name));
16672 /* Look for the closing `]'. */
16673 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16677 /* Look for the string-literal. */
16678 string_literal = cp_parser_string_literal (parser, false, false);
16680 /* Look for the `('. */
16681 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16682 /* Parse the expression. */
16683 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16684 /* Look for the `)'. */
16685 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16687 if (name == error_mark_node
16688 || string_literal == error_mark_node
16689 || expression == error_mark_node)
16690 invalid_operands = true;
16692 /* Add this operand to the list. */
16693 asm_operands = tree_cons (build_tree_list (name, string_literal),
16696 /* If the next token is not a `,', there are no more
16698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16700 /* Consume the `,'. */
16701 cp_lexer_consume_token (parser->lexer);
16704 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16707 /* Parse an asm-clobber-list.
16711 asm-clobber-list , string-literal
16713 Returns a TREE_LIST, indicating the clobbers in the order that they
16714 appeared. The TREE_VALUE of each node is a STRING_CST. */
16717 cp_parser_asm_clobber_list (cp_parser* parser)
16719 tree clobbers = NULL_TREE;
16723 tree string_literal;
16725 /* Look for the string literal. */
16726 string_literal = cp_parser_string_literal (parser, false, false);
16727 /* Add it to the list. */
16728 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16729 /* If the next token is not a `,', then the list is
16731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16733 /* Consume the `,' token. */
16734 cp_lexer_consume_token (parser->lexer);
16740 /* Parse an (optional) series of attributes.
16743 attributes attribute
16746 __attribute__ (( attribute-list [opt] ))
16748 The return value is as for cp_parser_attribute_list. */
16751 cp_parser_attributes_opt (cp_parser* parser)
16753 tree attributes = NULL_TREE;
16758 tree attribute_list;
16760 /* Peek at the next token. */
16761 token = cp_lexer_peek_token (parser->lexer);
16762 /* If it's not `__attribute__', then we're done. */
16763 if (token->keyword != RID_ATTRIBUTE)
16766 /* Consume the `__attribute__' keyword. */
16767 cp_lexer_consume_token (parser->lexer);
16768 /* Look for the two `(' tokens. */
16769 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16770 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16772 /* Peek at the next token. */
16773 token = cp_lexer_peek_token (parser->lexer);
16774 if (token->type != CPP_CLOSE_PAREN)
16775 /* Parse the attribute-list. */
16776 attribute_list = cp_parser_attribute_list (parser);
16778 /* If the next token is a `)', then there is no attribute
16780 attribute_list = NULL;
16782 /* Look for the two `)' tokens. */
16783 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16784 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16786 /* Add these new attributes to the list. */
16787 attributes = chainon (attributes, attribute_list);
16793 /* Parse an attribute-list.
16797 attribute-list , attribute
16801 identifier ( identifier )
16802 identifier ( identifier , expression-list )
16803 identifier ( expression-list )
16805 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16806 to an attribute. The TREE_PURPOSE of each node is the identifier
16807 indicating which attribute is in use. The TREE_VALUE represents
16808 the arguments, if any. */
16811 cp_parser_attribute_list (cp_parser* parser)
16813 tree attribute_list = NULL_TREE;
16814 bool save_translate_strings_p = parser->translate_strings_p;
16816 parser->translate_strings_p = false;
16823 /* Look for the identifier. We also allow keywords here; for
16824 example `__attribute__ ((const))' is legal. */
16825 token = cp_lexer_peek_token (parser->lexer);
16826 if (token->type == CPP_NAME
16827 || token->type == CPP_KEYWORD)
16829 tree arguments = NULL_TREE;
16831 /* Consume the token. */
16832 token = cp_lexer_consume_token (parser->lexer);
16834 /* Save away the identifier that indicates which attribute
16836 identifier = token->u.value;
16837 attribute = build_tree_list (identifier, NULL_TREE);
16839 /* Peek at the next token. */
16840 token = cp_lexer_peek_token (parser->lexer);
16841 /* If it's an `(', then parse the attribute arguments. */
16842 if (token->type == CPP_OPEN_PAREN)
16844 arguments = cp_parser_parenthesized_expression_list
16845 (parser, true, /*cast_p=*/false,
16846 /*allow_expansion_p=*/false,
16847 /*non_constant_p=*/NULL);
16848 /* Save the arguments away. */
16849 TREE_VALUE (attribute) = arguments;
16852 if (arguments != error_mark_node)
16854 /* Add this attribute to the list. */
16855 TREE_CHAIN (attribute) = attribute_list;
16856 attribute_list = attribute;
16859 token = cp_lexer_peek_token (parser->lexer);
16861 /* Now, look for more attributes. If the next token isn't a
16862 `,', we're done. */
16863 if (token->type != CPP_COMMA)
16866 /* Consume the comma and keep going. */
16867 cp_lexer_consume_token (parser->lexer);
16869 parser->translate_strings_p = save_translate_strings_p;
16871 /* We built up the list in reverse order. */
16872 return nreverse (attribute_list);
16875 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16876 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16877 current value of the PEDANTIC flag, regardless of whether or not
16878 the `__extension__' keyword is present. The caller is responsible
16879 for restoring the value of the PEDANTIC flag. */
16882 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16884 /* Save the old value of the PEDANTIC flag. */
16885 *saved_pedantic = pedantic;
16887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16889 /* Consume the `__extension__' token. */
16890 cp_lexer_consume_token (parser->lexer);
16891 /* We're not being pedantic while the `__extension__' keyword is
16901 /* Parse a label declaration.
16904 __label__ label-declarator-seq ;
16906 label-declarator-seq:
16907 identifier , label-declarator-seq
16911 cp_parser_label_declaration (cp_parser* parser)
16913 /* Look for the `__label__' keyword. */
16914 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16920 /* Look for an identifier. */
16921 identifier = cp_parser_identifier (parser);
16922 /* If we failed, stop. */
16923 if (identifier == error_mark_node)
16925 /* Declare it as a label. */
16926 finish_label_decl (identifier);
16927 /* If the next token is a `;', stop. */
16928 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16930 /* Look for the `,' separating the label declarations. */
16931 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16934 /* Look for the final `;'. */
16935 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16938 /* Support Functions */
16940 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16941 NAME should have one of the representations used for an
16942 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16943 is returned. If PARSER->SCOPE is a dependent type, then a
16944 SCOPE_REF is returned.
16946 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16947 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16948 was formed. Abstractly, such entities should not be passed to this
16949 function, because they do not need to be looked up, but it is
16950 simpler to check for this special case here, rather than at the
16953 In cases not explicitly covered above, this function returns a
16954 DECL, OVERLOAD, or baselink representing the result of the lookup.
16955 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16958 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16959 (e.g., "struct") that was used. In that case bindings that do not
16960 refer to types are ignored.
16962 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16965 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16968 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16971 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16972 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16973 NULL_TREE otherwise. */
16976 cp_parser_lookup_name (cp_parser *parser, tree name,
16977 enum tag_types tag_type,
16980 bool check_dependency,
16981 tree *ambiguous_decls,
16982 location_t name_location)
16986 tree object_type = parser->context->object_type;
16988 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16989 flags |= LOOKUP_COMPLAIN;
16991 /* Assume that the lookup will be unambiguous. */
16992 if (ambiguous_decls)
16993 *ambiguous_decls = NULL_TREE;
16995 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16996 no longer valid. Note that if we are parsing tentatively, and
16997 the parse fails, OBJECT_TYPE will be automatically restored. */
16998 parser->context->object_type = NULL_TREE;
17000 if (name == error_mark_node)
17001 return error_mark_node;
17003 /* A template-id has already been resolved; there is no lookup to
17005 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17007 if (BASELINK_P (name))
17009 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17010 == TEMPLATE_ID_EXPR);
17014 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17015 it should already have been checked to make sure that the name
17016 used matches the type being destroyed. */
17017 if (TREE_CODE (name) == BIT_NOT_EXPR)
17021 /* Figure out to which type this destructor applies. */
17023 type = parser->scope;
17024 else if (object_type)
17025 type = object_type;
17027 type = current_class_type;
17028 /* If that's not a class type, there is no destructor. */
17029 if (!type || !CLASS_TYPE_P (type))
17030 return error_mark_node;
17031 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17032 lazily_declare_fn (sfk_destructor, type);
17033 if (!CLASSTYPE_DESTRUCTORS (type))
17034 return error_mark_node;
17035 /* If it was a class type, return the destructor. */
17036 return CLASSTYPE_DESTRUCTORS (type);
17039 /* By this point, the NAME should be an ordinary identifier. If
17040 the id-expression was a qualified name, the qualifying scope is
17041 stored in PARSER->SCOPE at this point. */
17042 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17044 /* Perform the lookup. */
17049 if (parser->scope == error_mark_node)
17050 return error_mark_node;
17052 /* If the SCOPE is dependent, the lookup must be deferred until
17053 the template is instantiated -- unless we are explicitly
17054 looking up names in uninstantiated templates. Even then, we
17055 cannot look up the name if the scope is not a class type; it
17056 might, for example, be a template type parameter. */
17057 dependent_p = (TYPE_P (parser->scope)
17058 && dependent_scope_p (parser->scope));
17059 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17061 /* Defer lookup. */
17062 decl = error_mark_node;
17065 tree pushed_scope = NULL_TREE;
17067 /* If PARSER->SCOPE is a dependent type, then it must be a
17068 class type, and we must not be checking dependencies;
17069 otherwise, we would have processed this lookup above. So
17070 that PARSER->SCOPE is not considered a dependent base by
17071 lookup_member, we must enter the scope here. */
17073 pushed_scope = push_scope (parser->scope);
17074 /* If the PARSER->SCOPE is a template specialization, it
17075 may be instantiated during name lookup. In that case,
17076 errors may be issued. Even if we rollback the current
17077 tentative parse, those errors are valid. */
17078 decl = lookup_qualified_name (parser->scope, name,
17079 tag_type != none_type,
17080 /*complain=*/true);
17082 /* If we have a single function from a using decl, pull it out. */
17083 if (TREE_CODE (decl) == OVERLOAD
17084 && !really_overloaded_fn (decl))
17085 decl = OVL_FUNCTION (decl);
17088 pop_scope (pushed_scope);
17091 /* If the scope is a dependent type and either we deferred lookup or
17092 we did lookup but didn't find the name, rememeber the name. */
17093 if (decl == error_mark_node && TYPE_P (parser->scope)
17094 && dependent_type_p (parser->scope))
17100 /* The resolution to Core Issue 180 says that `struct
17101 A::B' should be considered a type-name, even if `A'
17103 type = make_typename_type (parser->scope, name, tag_type,
17104 /*complain=*/tf_error);
17105 decl = TYPE_NAME (type);
17107 else if (is_template
17108 && (cp_parser_next_token_ends_template_argument_p (parser)
17109 || cp_lexer_next_token_is (parser->lexer,
17111 decl = make_unbound_class_template (parser->scope,
17113 /*complain=*/tf_error);
17115 decl = build_qualified_name (/*type=*/NULL_TREE,
17116 parser->scope, name,
17119 parser->qualifying_scope = parser->scope;
17120 parser->object_scope = NULL_TREE;
17122 else if (object_type)
17124 tree object_decl = NULL_TREE;
17125 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17126 OBJECT_TYPE is not a class. */
17127 if (CLASS_TYPE_P (object_type))
17128 /* If the OBJECT_TYPE is a template specialization, it may
17129 be instantiated during name lookup. In that case, errors
17130 may be issued. Even if we rollback the current tentative
17131 parse, those errors are valid. */
17132 object_decl = lookup_member (object_type,
17135 tag_type != none_type);
17136 /* Look it up in the enclosing context, too. */
17137 decl = lookup_name_real (name, tag_type != none_type,
17139 /*block_p=*/true, is_namespace, flags);
17140 parser->object_scope = object_type;
17141 parser->qualifying_scope = NULL_TREE;
17143 decl = object_decl;
17147 decl = lookup_name_real (name, tag_type != none_type,
17149 /*block_p=*/true, is_namespace, flags);
17150 parser->qualifying_scope = NULL_TREE;
17151 parser->object_scope = NULL_TREE;
17154 /* If the lookup failed, let our caller know. */
17155 if (!decl || decl == error_mark_node)
17156 return error_mark_node;
17158 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17159 if (TREE_CODE (decl) == TREE_LIST)
17161 if (ambiguous_decls)
17162 *ambiguous_decls = decl;
17163 /* The error message we have to print is too complicated for
17164 cp_parser_error, so we incorporate its actions directly. */
17165 if (!cp_parser_simulate_error (parser))
17167 error ("%Hreference to %qD is ambiguous",
17168 &name_location, name);
17169 print_candidates (decl);
17171 return error_mark_node;
17174 gcc_assert (DECL_P (decl)
17175 || TREE_CODE (decl) == OVERLOAD
17176 || TREE_CODE (decl) == SCOPE_REF
17177 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17178 || BASELINK_P (decl));
17180 /* If we have resolved the name of a member declaration, check to
17181 see if the declaration is accessible. When the name resolves to
17182 set of overloaded functions, accessibility is checked when
17183 overload resolution is done.
17185 During an explicit instantiation, access is not checked at all,
17186 as per [temp.explicit]. */
17188 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17193 /* Like cp_parser_lookup_name, but for use in the typical case where
17194 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17195 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17198 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17200 return cp_parser_lookup_name (parser, name,
17202 /*is_template=*/false,
17203 /*is_namespace=*/false,
17204 /*check_dependency=*/true,
17205 /*ambiguous_decls=*/NULL,
17209 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17210 the current context, return the TYPE_DECL. If TAG_NAME_P is
17211 true, the DECL indicates the class being defined in a class-head,
17212 or declared in an elaborated-type-specifier.
17214 Otherwise, return DECL. */
17217 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17219 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17220 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17223 template <typename T> struct B;
17226 template <typename T> struct A::B {};
17228 Similarly, in an elaborated-type-specifier:
17230 namespace N { struct X{}; }
17233 template <typename T> friend struct N::X;
17236 However, if the DECL refers to a class type, and we are in
17237 the scope of the class, then the name lookup automatically
17238 finds the TYPE_DECL created by build_self_reference rather
17239 than a TEMPLATE_DECL. For example, in:
17241 template <class T> struct S {
17245 there is no need to handle such case. */
17247 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17248 return DECL_TEMPLATE_RESULT (decl);
17253 /* If too many, or too few, template-parameter lists apply to the
17254 declarator, issue an error message. Returns TRUE if all went well,
17255 and FALSE otherwise. */
17258 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17259 cp_declarator *declarator,
17260 location_t declarator_location)
17262 unsigned num_templates;
17264 /* We haven't seen any classes that involve template parameters yet. */
17267 switch (declarator->kind)
17270 if (declarator->u.id.qualifying_scope)
17275 scope = declarator->u.id.qualifying_scope;
17276 member = declarator->u.id.unqualified_name;
17278 while (scope && CLASS_TYPE_P (scope))
17280 /* You're supposed to have one `template <...>'
17281 for every template class, but you don't need one
17282 for a full specialization. For example:
17284 template <class T> struct S{};
17285 template <> struct S<int> { void f(); };
17286 void S<int>::f () {}
17288 is correct; there shouldn't be a `template <>' for
17289 the definition of `S<int>::f'. */
17290 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17291 /* If SCOPE does not have template information of any
17292 kind, then it is not a template, nor is it nested
17293 within a template. */
17295 if (explicit_class_specialization_p (scope))
17297 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17300 scope = TYPE_CONTEXT (scope);
17303 else if (TREE_CODE (declarator->u.id.unqualified_name)
17304 == TEMPLATE_ID_EXPR)
17305 /* If the DECLARATOR has the form `X<y>' then it uses one
17306 additional level of template parameters. */
17309 return cp_parser_check_template_parameters (parser,
17311 declarator_location);
17316 case cdk_reference:
17318 return (cp_parser_check_declarator_template_parameters
17319 (parser, declarator->declarator, declarator_location));
17325 gcc_unreachable ();
17330 /* NUM_TEMPLATES were used in the current declaration. If that is
17331 invalid, return FALSE and issue an error messages. Otherwise,
17335 cp_parser_check_template_parameters (cp_parser* parser,
17336 unsigned num_templates,
17337 location_t location)
17339 /* If there are more template classes than parameter lists, we have
17342 template <class T> void S<T>::R<T>::f (); */
17343 if (parser->num_template_parameter_lists < num_templates)
17345 error ("%Htoo few template-parameter-lists", &location);
17348 /* If there are the same number of template classes and parameter
17349 lists, that's OK. */
17350 if (parser->num_template_parameter_lists == num_templates)
17352 /* If there are more, but only one more, then we are referring to a
17353 member template. That's OK too. */
17354 if (parser->num_template_parameter_lists == num_templates + 1)
17356 /* Otherwise, there are too many template parameter lists. We have
17359 template <class T> template <class U> void S::f(); */
17360 error ("%Htoo many template-parameter-lists", &location);
17364 /* Parse an optional `::' token indicating that the following name is
17365 from the global namespace. If so, PARSER->SCOPE is set to the
17366 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17367 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17368 Returns the new value of PARSER->SCOPE, if the `::' token is
17369 present, and NULL_TREE otherwise. */
17372 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17376 /* Peek at the next token. */
17377 token = cp_lexer_peek_token (parser->lexer);
17378 /* If we're looking at a `::' token then we're starting from the
17379 global namespace, not our current location. */
17380 if (token->type == CPP_SCOPE)
17382 /* Consume the `::' token. */
17383 cp_lexer_consume_token (parser->lexer);
17384 /* Set the SCOPE so that we know where to start the lookup. */
17385 parser->scope = global_namespace;
17386 parser->qualifying_scope = global_namespace;
17387 parser->object_scope = NULL_TREE;
17389 return parser->scope;
17391 else if (!current_scope_valid_p)
17393 parser->scope = NULL_TREE;
17394 parser->qualifying_scope = NULL_TREE;
17395 parser->object_scope = NULL_TREE;
17401 /* Returns TRUE if the upcoming token sequence is the start of a
17402 constructor declarator. If FRIEND_P is true, the declarator is
17403 preceded by the `friend' specifier. */
17406 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17408 bool constructor_p;
17409 tree type_decl = NULL_TREE;
17410 bool nested_name_p;
17411 cp_token *next_token;
17413 /* The common case is that this is not a constructor declarator, so
17414 try to avoid doing lots of work if at all possible. It's not
17415 valid declare a constructor at function scope. */
17416 if (parser->in_function_body)
17418 /* And only certain tokens can begin a constructor declarator. */
17419 next_token = cp_lexer_peek_token (parser->lexer);
17420 if (next_token->type != CPP_NAME
17421 && next_token->type != CPP_SCOPE
17422 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17423 && next_token->type != CPP_TEMPLATE_ID)
17426 /* Parse tentatively; we are going to roll back all of the tokens
17428 cp_parser_parse_tentatively (parser);
17429 /* Assume that we are looking at a constructor declarator. */
17430 constructor_p = true;
17432 /* Look for the optional `::' operator. */
17433 cp_parser_global_scope_opt (parser,
17434 /*current_scope_valid_p=*/false);
17435 /* Look for the nested-name-specifier. */
17437 = (cp_parser_nested_name_specifier_opt (parser,
17438 /*typename_keyword_p=*/false,
17439 /*check_dependency_p=*/false,
17441 /*is_declaration=*/false)
17443 /* Outside of a class-specifier, there must be a
17444 nested-name-specifier. */
17445 if (!nested_name_p &&
17446 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17448 constructor_p = false;
17449 /* If we still think that this might be a constructor-declarator,
17450 look for a class-name. */
17455 template <typename T> struct S { S(); };
17456 template <typename T> S<T>::S ();
17458 we must recognize that the nested `S' names a class.
17461 template <typename T> S<T>::S<T> ();
17463 we must recognize that the nested `S' names a template. */
17464 type_decl = cp_parser_class_name (parser,
17465 /*typename_keyword_p=*/false,
17466 /*template_keyword_p=*/false,
17468 /*check_dependency_p=*/false,
17469 /*class_head_p=*/false,
17470 /*is_declaration=*/false);
17471 /* If there was no class-name, then this is not a constructor. */
17472 constructor_p = !cp_parser_error_occurred (parser);
17475 /* If we're still considering a constructor, we have to see a `(',
17476 to begin the parameter-declaration-clause, followed by either a
17477 `)', an `...', or a decl-specifier. We need to check for a
17478 type-specifier to avoid being fooled into thinking that:
17482 is a constructor. (It is actually a function named `f' that
17483 takes one parameter (of type `int') and returns a value of type
17486 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17489 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17490 /* A parameter declaration begins with a decl-specifier,
17491 which is either the "attribute" keyword, a storage class
17492 specifier, or (usually) a type-specifier. */
17493 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17496 tree pushed_scope = NULL_TREE;
17497 unsigned saved_num_template_parameter_lists;
17499 /* Names appearing in the type-specifier should be looked up
17500 in the scope of the class. */
17501 if (current_class_type)
17505 type = TREE_TYPE (type_decl);
17506 if (TREE_CODE (type) == TYPENAME_TYPE)
17508 type = resolve_typename_type (type,
17509 /*only_current_p=*/false);
17510 if (TREE_CODE (type) == TYPENAME_TYPE)
17512 cp_parser_abort_tentative_parse (parser);
17516 pushed_scope = push_scope (type);
17519 /* Inside the constructor parameter list, surrounding
17520 template-parameter-lists do not apply. */
17521 saved_num_template_parameter_lists
17522 = parser->num_template_parameter_lists;
17523 parser->num_template_parameter_lists = 0;
17525 /* Look for the type-specifier. */
17526 cp_parser_type_specifier (parser,
17527 CP_PARSER_FLAGS_NONE,
17528 /*decl_specs=*/NULL,
17529 /*is_declarator=*/true,
17530 /*declares_class_or_enum=*/NULL,
17531 /*is_cv_qualifier=*/NULL);
17533 parser->num_template_parameter_lists
17534 = saved_num_template_parameter_lists;
17536 /* Leave the scope of the class. */
17538 pop_scope (pushed_scope);
17540 constructor_p = !cp_parser_error_occurred (parser);
17544 constructor_p = false;
17545 /* We did not really want to consume any tokens. */
17546 cp_parser_abort_tentative_parse (parser);
17548 return constructor_p;
17551 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17552 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17553 they must be performed once we are in the scope of the function.
17555 Returns the function defined. */
17558 cp_parser_function_definition_from_specifiers_and_declarator
17559 (cp_parser* parser,
17560 cp_decl_specifier_seq *decl_specifiers,
17562 const cp_declarator *declarator)
17567 /* Begin the function-definition. */
17568 success_p = start_function (decl_specifiers, declarator, attributes);
17570 /* The things we're about to see are not directly qualified by any
17571 template headers we've seen thus far. */
17572 reset_specialization ();
17574 /* If there were names looked up in the decl-specifier-seq that we
17575 did not check, check them now. We must wait until we are in the
17576 scope of the function to perform the checks, since the function
17577 might be a friend. */
17578 perform_deferred_access_checks ();
17582 /* Skip the entire function. */
17583 cp_parser_skip_to_end_of_block_or_statement (parser);
17584 fn = error_mark_node;
17586 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17588 /* Seen already, skip it. An error message has already been output. */
17589 cp_parser_skip_to_end_of_block_or_statement (parser);
17590 fn = current_function_decl;
17591 current_function_decl = NULL_TREE;
17592 /* If this is a function from a class, pop the nested class. */
17593 if (current_class_name)
17594 pop_nested_class ();
17597 fn = cp_parser_function_definition_after_declarator (parser,
17598 /*inline_p=*/false);
17603 /* Parse the part of a function-definition that follows the
17604 declarator. INLINE_P is TRUE iff this function is an inline
17605 function defined with a class-specifier.
17607 Returns the function defined. */
17610 cp_parser_function_definition_after_declarator (cp_parser* parser,
17614 bool ctor_initializer_p = false;
17615 bool saved_in_unbraced_linkage_specification_p;
17616 bool saved_in_function_body;
17617 unsigned saved_num_template_parameter_lists;
17620 saved_in_function_body = parser->in_function_body;
17621 parser->in_function_body = true;
17622 /* If the next token is `return', then the code may be trying to
17623 make use of the "named return value" extension that G++ used to
17625 token = cp_lexer_peek_token (parser->lexer);
17626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17628 /* Consume the `return' keyword. */
17629 cp_lexer_consume_token (parser->lexer);
17630 /* Look for the identifier that indicates what value is to be
17632 cp_parser_identifier (parser);
17633 /* Issue an error message. */
17634 error ("%Hnamed return values are no longer supported",
17636 /* Skip tokens until we reach the start of the function body. */
17639 cp_token *token = cp_lexer_peek_token (parser->lexer);
17640 if (token->type == CPP_OPEN_BRACE
17641 || token->type == CPP_EOF
17642 || token->type == CPP_PRAGMA_EOL)
17644 cp_lexer_consume_token (parser->lexer);
17647 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17648 anything declared inside `f'. */
17649 saved_in_unbraced_linkage_specification_p
17650 = parser->in_unbraced_linkage_specification_p;
17651 parser->in_unbraced_linkage_specification_p = false;
17652 /* Inside the function, surrounding template-parameter-lists do not
17654 saved_num_template_parameter_lists
17655 = parser->num_template_parameter_lists;
17656 parser->num_template_parameter_lists = 0;
17657 /* If the next token is `try', then we are looking at a
17658 function-try-block. */
17659 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17660 ctor_initializer_p = cp_parser_function_try_block (parser);
17661 /* A function-try-block includes the function-body, so we only do
17662 this next part if we're not processing a function-try-block. */
17665 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17667 /* Finish the function. */
17668 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17669 (inline_p ? 2 : 0));
17670 /* Generate code for it, if necessary. */
17671 expand_or_defer_fn (fn);
17672 /* Restore the saved values. */
17673 parser->in_unbraced_linkage_specification_p
17674 = saved_in_unbraced_linkage_specification_p;
17675 parser->num_template_parameter_lists
17676 = saved_num_template_parameter_lists;
17677 parser->in_function_body = saved_in_function_body;
17682 /* Parse a template-declaration, assuming that the `export' (and
17683 `extern') keywords, if present, has already been scanned. MEMBER_P
17684 is as for cp_parser_template_declaration. */
17687 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17689 tree decl = NULL_TREE;
17690 VEC (deferred_access_check,gc) *checks;
17691 tree parameter_list;
17692 bool friend_p = false;
17693 bool need_lang_pop;
17696 /* Look for the `template' keyword. */
17697 token = cp_lexer_peek_token (parser->lexer);
17698 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17702 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17704 if (at_class_scope_p () && current_function_decl)
17706 /* 14.5.2.2 [temp.mem]
17708 A local class shall not have member templates. */
17709 error ("%Hinvalid declaration of member template in local class",
17711 cp_parser_skip_to_end_of_block_or_statement (parser);
17716 A template ... shall not have C linkage. */
17717 if (current_lang_name == lang_name_c)
17719 error ("%Htemplate with C linkage", &token->location);
17720 /* Give it C++ linkage to avoid confusing other parts of the
17722 push_lang_context (lang_name_cplusplus);
17723 need_lang_pop = true;
17726 need_lang_pop = false;
17728 /* We cannot perform access checks on the template parameter
17729 declarations until we know what is being declared, just as we
17730 cannot check the decl-specifier list. */
17731 push_deferring_access_checks (dk_deferred);
17733 /* If the next token is `>', then we have an invalid
17734 specialization. Rather than complain about an invalid template
17735 parameter, issue an error message here. */
17736 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17738 cp_parser_error (parser, "invalid explicit specialization");
17739 begin_specialization ();
17740 parameter_list = NULL_TREE;
17743 /* Parse the template parameters. */
17744 parameter_list = cp_parser_template_parameter_list (parser);
17746 /* Get the deferred access checks from the parameter list. These
17747 will be checked once we know what is being declared, as for a
17748 member template the checks must be performed in the scope of the
17749 class containing the member. */
17750 checks = get_deferred_access_checks ();
17752 /* Look for the `>'. */
17753 cp_parser_skip_to_end_of_template_parameter_list (parser);
17754 /* We just processed one more parameter list. */
17755 ++parser->num_template_parameter_lists;
17756 /* If the next token is `template', there are more template
17758 if (cp_lexer_next_token_is_keyword (parser->lexer,
17760 cp_parser_template_declaration_after_export (parser, member_p);
17763 /* There are no access checks when parsing a template, as we do not
17764 know if a specialization will be a friend. */
17765 push_deferring_access_checks (dk_no_check);
17766 token = cp_lexer_peek_token (parser->lexer);
17767 decl = cp_parser_single_declaration (parser,
17770 /*explicit_specialization_p=*/false,
17772 pop_deferring_access_checks ();
17774 /* If this is a member template declaration, let the front
17776 if (member_p && !friend_p && decl)
17778 if (TREE_CODE (decl) == TYPE_DECL)
17779 cp_parser_check_access_in_redeclaration (decl, token->location);
17781 decl = finish_member_template_decl (decl);
17783 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17784 make_friend_class (current_class_type, TREE_TYPE (decl),
17785 /*complain=*/true);
17787 /* We are done with the current parameter list. */
17788 --parser->num_template_parameter_lists;
17790 pop_deferring_access_checks ();
17793 finish_template_decl (parameter_list);
17795 /* Register member declarations. */
17796 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17797 finish_member_declaration (decl);
17798 /* For the erroneous case of a template with C linkage, we pushed an
17799 implicit C++ linkage scope; exit that scope now. */
17801 pop_lang_context ();
17802 /* If DECL is a function template, we must return to parse it later.
17803 (Even though there is no definition, there might be default
17804 arguments that need handling.) */
17805 if (member_p && decl
17806 && (TREE_CODE (decl) == FUNCTION_DECL
17807 || DECL_FUNCTION_TEMPLATE_P (decl)))
17808 TREE_VALUE (parser->unparsed_functions_queues)
17809 = tree_cons (NULL_TREE, decl,
17810 TREE_VALUE (parser->unparsed_functions_queues));
17813 /* Perform the deferred access checks from a template-parameter-list.
17814 CHECKS is a TREE_LIST of access checks, as returned by
17815 get_deferred_access_checks. */
17818 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17820 ++processing_template_parmlist;
17821 perform_access_checks (checks);
17822 --processing_template_parmlist;
17825 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17826 `function-definition' sequence. MEMBER_P is true, this declaration
17827 appears in a class scope.
17829 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17830 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17833 cp_parser_single_declaration (cp_parser* parser,
17834 VEC (deferred_access_check,gc)* checks,
17836 bool explicit_specialization_p,
17839 int declares_class_or_enum;
17840 tree decl = NULL_TREE;
17841 cp_decl_specifier_seq decl_specifiers;
17842 bool function_definition_p = false;
17843 cp_token *decl_spec_token_start;
17845 /* This function is only used when processing a template
17847 gcc_assert (innermost_scope_kind () == sk_template_parms
17848 || innermost_scope_kind () == sk_template_spec);
17850 /* Defer access checks until we know what is being declared. */
17851 push_deferring_access_checks (dk_deferred);
17853 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17855 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17856 cp_parser_decl_specifier_seq (parser,
17857 CP_PARSER_FLAGS_OPTIONAL,
17859 &declares_class_or_enum);
17861 *friend_p = cp_parser_friend_p (&decl_specifiers);
17863 /* There are no template typedefs. */
17864 if (decl_specifiers.specs[(int) ds_typedef])
17866 error ("%Htemplate declaration of %qs",
17867 &decl_spec_token_start->location, "typedef");
17868 decl = error_mark_node;
17871 /* Gather up the access checks that occurred the
17872 decl-specifier-seq. */
17873 stop_deferring_access_checks ();
17875 /* Check for the declaration of a template class. */
17876 if (declares_class_or_enum)
17878 if (cp_parser_declares_only_class_p (parser))
17880 decl = shadow_tag (&decl_specifiers);
17885 friend template <typename T> struct A<T>::B;
17888 A<T>::B will be represented by a TYPENAME_TYPE, and
17889 therefore not recognized by shadow_tag. */
17890 if (friend_p && *friend_p
17892 && decl_specifiers.type
17893 && TYPE_P (decl_specifiers.type))
17894 decl = decl_specifiers.type;
17896 if (decl && decl != error_mark_node)
17897 decl = TYPE_NAME (decl);
17899 decl = error_mark_node;
17901 /* Perform access checks for template parameters. */
17902 cp_parser_perform_template_parameter_access_checks (checks);
17905 /* If it's not a template class, try for a template function. If
17906 the next token is a `;', then this declaration does not declare
17907 anything. But, if there were errors in the decl-specifiers, then
17908 the error might well have come from an attempted class-specifier.
17909 In that case, there's no need to warn about a missing declarator. */
17911 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17912 || decl_specifiers.type != error_mark_node))
17914 decl = cp_parser_init_declarator (parser,
17917 /*function_definition_allowed_p=*/true,
17919 declares_class_or_enum,
17920 &function_definition_p);
17922 /* 7.1.1-1 [dcl.stc]
17924 A storage-class-specifier shall not be specified in an explicit
17925 specialization... */
17927 && explicit_specialization_p
17928 && decl_specifiers.storage_class != sc_none)
17930 error ("%Hexplicit template specialization cannot have a storage class",
17931 &decl_spec_token_start->location);
17932 decl = error_mark_node;
17936 pop_deferring_access_checks ();
17938 /* Clear any current qualification; whatever comes next is the start
17939 of something new. */
17940 parser->scope = NULL_TREE;
17941 parser->qualifying_scope = NULL_TREE;
17942 parser->object_scope = NULL_TREE;
17943 /* Look for a trailing `;' after the declaration. */
17944 if (!function_definition_p
17945 && (decl == error_mark_node
17946 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17947 cp_parser_skip_to_end_of_block_or_statement (parser);
17952 /* Parse a cast-expression that is not the operand of a unary "&". */
17955 cp_parser_simple_cast_expression (cp_parser *parser)
17957 return cp_parser_cast_expression (parser, /*address_p=*/false,
17958 /*cast_p=*/false, NULL);
17961 /* Parse a functional cast to TYPE. Returns an expression
17962 representing the cast. */
17965 cp_parser_functional_cast (cp_parser* parser, tree type)
17967 tree expression_list;
17971 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17973 maybe_warn_cpp0x ("extended initializer lists");
17974 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17975 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17976 if (TREE_CODE (type) == TYPE_DECL)
17977 type = TREE_TYPE (type);
17978 return finish_compound_literal (type, expression_list);
17982 = cp_parser_parenthesized_expression_list (parser, false,
17984 /*allow_expansion_p=*/true,
17985 /*non_constant_p=*/NULL);
17987 cast = build_functional_cast (type, expression_list,
17988 tf_warning_or_error);
17989 /* [expr.const]/1: In an integral constant expression "only type
17990 conversions to integral or enumeration type can be used". */
17991 if (TREE_CODE (type) == TYPE_DECL)
17992 type = TREE_TYPE (type);
17993 if (cast != error_mark_node
17994 && !cast_valid_in_integral_constant_expression_p (type)
17995 && (cp_parser_non_integral_constant_expression
17996 (parser, "a call to a constructor")))
17997 return error_mark_node;
18001 /* Save the tokens that make up the body of a member function defined
18002 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18003 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18004 specifiers applied to the declaration. Returns the FUNCTION_DECL
18005 for the member function. */
18008 cp_parser_save_member_function_body (cp_parser* parser,
18009 cp_decl_specifier_seq *decl_specifiers,
18010 cp_declarator *declarator,
18017 /* Create the function-declaration. */
18018 fn = start_method (decl_specifiers, declarator, attributes);
18019 /* If something went badly wrong, bail out now. */
18020 if (fn == error_mark_node)
18022 /* If there's a function-body, skip it. */
18023 if (cp_parser_token_starts_function_definition_p
18024 (cp_lexer_peek_token (parser->lexer)))
18025 cp_parser_skip_to_end_of_block_or_statement (parser);
18026 return error_mark_node;
18029 /* Remember it, if there default args to post process. */
18030 cp_parser_save_default_args (parser, fn);
18032 /* Save away the tokens that make up the body of the
18034 first = parser->lexer->next_token;
18035 /* We can have braced-init-list mem-initializers before the fn body. */
18036 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18038 cp_lexer_consume_token (parser->lexer);
18039 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18040 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18042 /* cache_group will stop after an un-nested { } pair, too. */
18043 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18046 /* variadic mem-inits have ... after the ')'. */
18047 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18048 cp_lexer_consume_token (parser->lexer);
18051 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18052 /* Handle function try blocks. */
18053 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18054 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18055 last = parser->lexer->next_token;
18057 /* Save away the inline definition; we will process it when the
18058 class is complete. */
18059 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18060 DECL_PENDING_INLINE_P (fn) = 1;
18062 /* We need to know that this was defined in the class, so that
18063 friend templates are handled correctly. */
18064 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18066 /* We're done with the inline definition. */
18067 finish_method (fn);
18069 /* Add FN to the queue of functions to be parsed later. */
18070 TREE_VALUE (parser->unparsed_functions_queues)
18071 = tree_cons (NULL_TREE, fn,
18072 TREE_VALUE (parser->unparsed_functions_queues));
18077 /* Parse a template-argument-list, as well as the trailing ">" (but
18078 not the opening ">"). See cp_parser_template_argument_list for the
18082 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18086 tree saved_qualifying_scope;
18087 tree saved_object_scope;
18088 bool saved_greater_than_is_operator_p;
18089 bool saved_skip_evaluation;
18093 When parsing a template-id, the first non-nested `>' is taken as
18094 the end of the template-argument-list rather than a greater-than
18096 saved_greater_than_is_operator_p
18097 = parser->greater_than_is_operator_p;
18098 parser->greater_than_is_operator_p = false;
18099 /* Parsing the argument list may modify SCOPE, so we save it
18101 saved_scope = parser->scope;
18102 saved_qualifying_scope = parser->qualifying_scope;
18103 saved_object_scope = parser->object_scope;
18104 /* We need to evaluate the template arguments, even though this
18105 template-id may be nested within a "sizeof". */
18106 saved_skip_evaluation = skip_evaluation;
18107 skip_evaluation = false;
18108 /* Parse the template-argument-list itself. */
18109 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18110 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18111 arguments = NULL_TREE;
18113 arguments = cp_parser_template_argument_list (parser);
18114 /* Look for the `>' that ends the template-argument-list. If we find
18115 a '>>' instead, it's probably just a typo. */
18116 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18118 if (cxx_dialect != cxx98)
18120 /* In C++0x, a `>>' in a template argument list or cast
18121 expression is considered to be two separate `>'
18122 tokens. So, change the current token to a `>', but don't
18123 consume it: it will be consumed later when the outer
18124 template argument list (or cast expression) is parsed.
18125 Note that this replacement of `>' for `>>' is necessary
18126 even if we are parsing tentatively: in the tentative
18127 case, after calling
18128 cp_parser_enclosed_template_argument_list we will always
18129 throw away all of the template arguments and the first
18130 closing `>', either because the template argument list
18131 was erroneous or because we are replacing those tokens
18132 with a CPP_TEMPLATE_ID token. The second `>' (which will
18133 not have been thrown away) is needed either to close an
18134 outer template argument list or to complete a new-style
18136 cp_token *token = cp_lexer_peek_token (parser->lexer);
18137 token->type = CPP_GREATER;
18139 else if (!saved_greater_than_is_operator_p)
18141 /* If we're in a nested template argument list, the '>>' has
18142 to be a typo for '> >'. We emit the error message, but we
18143 continue parsing and we push a '>' as next token, so that
18144 the argument list will be parsed correctly. Note that the
18145 global source location is still on the token before the
18146 '>>', so we need to say explicitly where we want it. */
18147 cp_token *token = cp_lexer_peek_token (parser->lexer);
18148 error ("%H%<>>%> should be %<> >%> "
18149 "within a nested template argument list",
18152 token->type = CPP_GREATER;
18156 /* If this is not a nested template argument list, the '>>'
18157 is a typo for '>'. Emit an error message and continue.
18158 Same deal about the token location, but here we can get it
18159 right by consuming the '>>' before issuing the diagnostic. */
18160 cp_token *token = cp_lexer_consume_token (parser->lexer);
18161 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18162 "a template argument list", &token->location);
18166 cp_parser_skip_to_end_of_template_parameter_list (parser);
18167 /* The `>' token might be a greater-than operator again now. */
18168 parser->greater_than_is_operator_p
18169 = saved_greater_than_is_operator_p;
18170 /* Restore the SAVED_SCOPE. */
18171 parser->scope = saved_scope;
18172 parser->qualifying_scope = saved_qualifying_scope;
18173 parser->object_scope = saved_object_scope;
18174 skip_evaluation = saved_skip_evaluation;
18179 /* MEMBER_FUNCTION is a member function, or a friend. If default
18180 arguments, or the body of the function have not yet been parsed,
18184 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18186 /* If this member is a template, get the underlying
18188 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18189 member_function = DECL_TEMPLATE_RESULT (member_function);
18191 /* There should not be any class definitions in progress at this
18192 point; the bodies of members are only parsed outside of all class
18194 gcc_assert (parser->num_classes_being_defined == 0);
18195 /* While we're parsing the member functions we might encounter more
18196 classes. We want to handle them right away, but we don't want
18197 them getting mixed up with functions that are currently in the
18199 parser->unparsed_functions_queues
18200 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18202 /* Make sure that any template parameters are in scope. */
18203 maybe_begin_member_template_processing (member_function);
18205 /* If the body of the function has not yet been parsed, parse it
18207 if (DECL_PENDING_INLINE_P (member_function))
18209 tree function_scope;
18210 cp_token_cache *tokens;
18212 /* The function is no longer pending; we are processing it. */
18213 tokens = DECL_PENDING_INLINE_INFO (member_function);
18214 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18215 DECL_PENDING_INLINE_P (member_function) = 0;
18217 /* If this is a local class, enter the scope of the containing
18219 function_scope = current_function_decl;
18220 if (function_scope)
18221 push_function_context ();
18223 /* Push the body of the function onto the lexer stack. */
18224 cp_parser_push_lexer_for_tokens (parser, tokens);
18226 /* Let the front end know that we going to be defining this
18228 start_preparsed_function (member_function, NULL_TREE,
18229 SF_PRE_PARSED | SF_INCLASS_INLINE);
18231 /* Don't do access checking if it is a templated function. */
18232 if (processing_template_decl)
18233 push_deferring_access_checks (dk_no_check);
18235 /* Now, parse the body of the function. */
18236 cp_parser_function_definition_after_declarator (parser,
18237 /*inline_p=*/true);
18239 if (processing_template_decl)
18240 pop_deferring_access_checks ();
18242 /* Leave the scope of the containing function. */
18243 if (function_scope)
18244 pop_function_context ();
18245 cp_parser_pop_lexer (parser);
18248 /* Remove any template parameters from the symbol table. */
18249 maybe_end_member_template_processing ();
18251 /* Restore the queue. */
18252 parser->unparsed_functions_queues
18253 = TREE_CHAIN (parser->unparsed_functions_queues);
18256 /* If DECL contains any default args, remember it on the unparsed
18257 functions queue. */
18260 cp_parser_save_default_args (cp_parser* parser, tree decl)
18264 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18266 probe = TREE_CHAIN (probe))
18267 if (TREE_PURPOSE (probe))
18269 TREE_PURPOSE (parser->unparsed_functions_queues)
18270 = tree_cons (current_class_type, decl,
18271 TREE_PURPOSE (parser->unparsed_functions_queues));
18276 /* FN is a FUNCTION_DECL which may contains a parameter with an
18277 unparsed DEFAULT_ARG. Parse the default args now. This function
18278 assumes that the current scope is the scope in which the default
18279 argument should be processed. */
18282 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18284 bool saved_local_variables_forbidden_p;
18287 /* While we're parsing the default args, we might (due to the
18288 statement expression extension) encounter more classes. We want
18289 to handle them right away, but we don't want them getting mixed
18290 up with default args that are currently in the queue. */
18291 parser->unparsed_functions_queues
18292 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18294 /* Local variable names (and the `this' keyword) may not appear
18295 in a default argument. */
18296 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18297 parser->local_variables_forbidden_p = true;
18299 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18301 parm = TREE_CHAIN (parm))
18303 cp_token_cache *tokens;
18304 tree default_arg = TREE_PURPOSE (parm);
18306 VEC(tree,gc) *insts;
18313 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18314 /* This can happen for a friend declaration for a function
18315 already declared with default arguments. */
18318 /* Push the saved tokens for the default argument onto the parser's
18320 tokens = DEFARG_TOKENS (default_arg);
18321 cp_parser_push_lexer_for_tokens (parser, tokens);
18323 /* Parse the assignment-expression. */
18324 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18325 if (parsed_arg == error_mark_node)
18327 cp_parser_pop_lexer (parser);
18331 if (!processing_template_decl)
18332 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18334 TREE_PURPOSE (parm) = parsed_arg;
18336 /* Update any instantiations we've already created. */
18337 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18338 VEC_iterate (tree, insts, ix, copy); ix++)
18339 TREE_PURPOSE (copy) = parsed_arg;
18341 /* If the token stream has not been completely used up, then
18342 there was extra junk after the end of the default
18344 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18345 cp_parser_error (parser, "expected %<,%>");
18347 /* Revert to the main lexer. */
18348 cp_parser_pop_lexer (parser);
18351 /* Make sure no default arg is missing. */
18352 check_default_args (fn);
18354 /* Restore the state of local_variables_forbidden_p. */
18355 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18357 /* Restore the queue. */
18358 parser->unparsed_functions_queues
18359 = TREE_CHAIN (parser->unparsed_functions_queues);
18362 /* Parse the operand of `sizeof' (or a similar operator). Returns
18363 either a TYPE or an expression, depending on the form of the
18364 input. The KEYWORD indicates which kind of expression we have
18368 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18370 tree expr = NULL_TREE;
18371 const char *saved_message;
18373 bool saved_integral_constant_expression_p;
18374 bool saved_non_integral_constant_expression_p;
18375 bool pack_expansion_p = false;
18377 /* Types cannot be defined in a `sizeof' expression. Save away the
18379 saved_message = parser->type_definition_forbidden_message;
18380 /* And create the new one. */
18381 tmp = concat ("types may not be defined in %<",
18382 IDENTIFIER_POINTER (ridpointers[keyword]),
18383 "%> expressions", NULL);
18384 parser->type_definition_forbidden_message = tmp;
18386 /* The restrictions on constant-expressions do not apply inside
18387 sizeof expressions. */
18388 saved_integral_constant_expression_p
18389 = parser->integral_constant_expression_p;
18390 saved_non_integral_constant_expression_p
18391 = parser->non_integral_constant_expression_p;
18392 parser->integral_constant_expression_p = false;
18394 /* If it's a `...', then we are computing the length of a parameter
18396 if (keyword == RID_SIZEOF
18397 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18399 /* Consume the `...'. */
18400 cp_lexer_consume_token (parser->lexer);
18401 maybe_warn_variadic_templates ();
18403 /* Note that this is an expansion. */
18404 pack_expansion_p = true;
18407 /* Do not actually evaluate the expression. */
18409 /* If it's a `(', then we might be looking at the type-id
18411 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18414 bool saved_in_type_id_in_expr_p;
18416 /* We can't be sure yet whether we're looking at a type-id or an
18418 cp_parser_parse_tentatively (parser);
18419 /* Consume the `('. */
18420 cp_lexer_consume_token (parser->lexer);
18421 /* Parse the type-id. */
18422 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18423 parser->in_type_id_in_expr_p = true;
18424 type = cp_parser_type_id (parser);
18425 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18426 /* Now, look for the trailing `)'. */
18427 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18428 /* If all went well, then we're done. */
18429 if (cp_parser_parse_definitely (parser))
18431 cp_decl_specifier_seq decl_specs;
18433 /* Build a trivial decl-specifier-seq. */
18434 clear_decl_specs (&decl_specs);
18435 decl_specs.type = type;
18437 /* Call grokdeclarator to figure out what type this is. */
18438 expr = grokdeclarator (NULL,
18442 /*attrlist=*/NULL);
18446 /* If the type-id production did not work out, then we must be
18447 looking at the unary-expression production. */
18449 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18450 /*cast_p=*/false, NULL);
18452 if (pack_expansion_p)
18453 /* Build a pack expansion. */
18454 expr = make_pack_expansion (expr);
18456 /* Go back to evaluating expressions. */
18459 /* Free the message we created. */
18461 /* And restore the old one. */
18462 parser->type_definition_forbidden_message = saved_message;
18463 parser->integral_constant_expression_p
18464 = saved_integral_constant_expression_p;
18465 parser->non_integral_constant_expression_p
18466 = saved_non_integral_constant_expression_p;
18471 /* If the current declaration has no declarator, return true. */
18474 cp_parser_declares_only_class_p (cp_parser *parser)
18476 /* If the next token is a `;' or a `,' then there is no
18478 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18479 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18482 /* Update the DECL_SPECS to reflect the storage class indicated by
18486 cp_parser_set_storage_class (cp_parser *parser,
18487 cp_decl_specifier_seq *decl_specs,
18489 location_t location)
18491 cp_storage_class storage_class;
18493 if (parser->in_unbraced_linkage_specification_p)
18495 error ("%Hinvalid use of %qD in linkage specification",
18496 &location, ridpointers[keyword]);
18499 else if (decl_specs->storage_class != sc_none)
18501 decl_specs->conflicting_specifiers_p = true;
18505 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18506 && decl_specs->specs[(int) ds_thread])
18508 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18509 decl_specs->specs[(int) ds_thread] = 0;
18515 storage_class = sc_auto;
18518 storage_class = sc_register;
18521 storage_class = sc_static;
18524 storage_class = sc_extern;
18527 storage_class = sc_mutable;
18530 gcc_unreachable ();
18532 decl_specs->storage_class = storage_class;
18534 /* A storage class specifier cannot be applied alongside a typedef
18535 specifier. If there is a typedef specifier present then set
18536 conflicting_specifiers_p which will trigger an error later
18537 on in grokdeclarator. */
18538 if (decl_specs->specs[(int)ds_typedef])
18539 decl_specs->conflicting_specifiers_p = true;
18542 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18543 is true, the type is a user-defined type; otherwise it is a
18544 built-in type specified by a keyword. */
18547 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18549 location_t location,
18550 bool user_defined_p)
18552 decl_specs->any_specifiers_p = true;
18554 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18555 (with, for example, in "typedef int wchar_t;") we remember that
18556 this is what happened. In system headers, we ignore these
18557 declarations so that G++ can work with system headers that are not
18559 if (decl_specs->specs[(int) ds_typedef]
18561 && (type_spec == boolean_type_node
18562 || type_spec == char16_type_node
18563 || type_spec == char32_type_node
18564 || type_spec == wchar_type_node)
18565 && (decl_specs->type
18566 || decl_specs->specs[(int) ds_long]
18567 || decl_specs->specs[(int) ds_short]
18568 || decl_specs->specs[(int) ds_unsigned]
18569 || decl_specs->specs[(int) ds_signed]))
18571 decl_specs->redefined_builtin_type = type_spec;
18572 if (!decl_specs->type)
18574 decl_specs->type = type_spec;
18575 decl_specs->user_defined_type_p = false;
18576 decl_specs->type_location = location;
18579 else if (decl_specs->type)
18580 decl_specs->multiple_types_p = true;
18583 decl_specs->type = type_spec;
18584 decl_specs->user_defined_type_p = user_defined_p;
18585 decl_specs->redefined_builtin_type = NULL_TREE;
18586 decl_specs->type_location = location;
18590 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18591 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18594 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18596 return decl_specifiers->specs[(int) ds_friend] != 0;
18599 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18600 issue an error message indicating that TOKEN_DESC was expected.
18602 Returns the token consumed, if the token had the appropriate type.
18603 Otherwise, returns NULL. */
18606 cp_parser_require (cp_parser* parser,
18607 enum cpp_ttype type,
18608 const char* token_desc)
18610 if (cp_lexer_next_token_is (parser->lexer, type))
18611 return cp_lexer_consume_token (parser->lexer);
18614 /* Output the MESSAGE -- unless we're parsing tentatively. */
18615 if (!cp_parser_simulate_error (parser))
18617 char *message = concat ("expected ", token_desc, NULL);
18618 cp_parser_error (parser, message);
18625 /* An error message is produced if the next token is not '>'.
18626 All further tokens are skipped until the desired token is
18627 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18630 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18632 /* Current level of '< ... >'. */
18633 unsigned level = 0;
18634 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18635 unsigned nesting_depth = 0;
18637 /* Are we ready, yet? If not, issue error message. */
18638 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18641 /* Skip tokens until the desired token is found. */
18644 /* Peek at the next token. */
18645 switch (cp_lexer_peek_token (parser->lexer)->type)
18648 if (!nesting_depth)
18653 if (cxx_dialect == cxx98)
18654 /* C++0x views the `>>' operator as two `>' tokens, but
18657 else if (!nesting_depth && level-- == 0)
18659 /* We've hit a `>>' where the first `>' closes the
18660 template argument list, and the second `>' is
18661 spurious. Just consume the `>>' and stop; we've
18662 already produced at least one error. */
18663 cp_lexer_consume_token (parser->lexer);
18666 /* Fall through for C++0x, so we handle the second `>' in
18670 if (!nesting_depth && level-- == 0)
18672 /* We've reached the token we want, consume it and stop. */
18673 cp_lexer_consume_token (parser->lexer);
18678 case CPP_OPEN_PAREN:
18679 case CPP_OPEN_SQUARE:
18683 case CPP_CLOSE_PAREN:
18684 case CPP_CLOSE_SQUARE:
18685 if (nesting_depth-- == 0)
18690 case CPP_PRAGMA_EOL:
18691 case CPP_SEMICOLON:
18692 case CPP_OPEN_BRACE:
18693 case CPP_CLOSE_BRACE:
18694 /* The '>' was probably forgotten, don't look further. */
18701 /* Consume this token. */
18702 cp_lexer_consume_token (parser->lexer);
18706 /* If the next token is the indicated keyword, consume it. Otherwise,
18707 issue an error message indicating that TOKEN_DESC was expected.
18709 Returns the token consumed, if the token had the appropriate type.
18710 Otherwise, returns NULL. */
18713 cp_parser_require_keyword (cp_parser* parser,
18715 const char* token_desc)
18717 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18719 if (token && token->keyword != keyword)
18721 dyn_string_t error_msg;
18723 /* Format the error message. */
18724 error_msg = dyn_string_new (0);
18725 dyn_string_append_cstr (error_msg, "expected ");
18726 dyn_string_append_cstr (error_msg, token_desc);
18727 cp_parser_error (parser, error_msg->s);
18728 dyn_string_delete (error_msg);
18735 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18736 function-definition. */
18739 cp_parser_token_starts_function_definition_p (cp_token* token)
18741 return (/* An ordinary function-body begins with an `{'. */
18742 token->type == CPP_OPEN_BRACE
18743 /* A ctor-initializer begins with a `:'. */
18744 || token->type == CPP_COLON
18745 /* A function-try-block begins with `try'. */
18746 || token->keyword == RID_TRY
18747 /* The named return value extension begins with `return'. */
18748 || token->keyword == RID_RETURN);
18751 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18755 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18759 token = cp_lexer_peek_token (parser->lexer);
18760 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18763 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18764 C++0x) ending a template-argument. */
18767 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18771 token = cp_lexer_peek_token (parser->lexer);
18772 return (token->type == CPP_COMMA
18773 || token->type == CPP_GREATER
18774 || token->type == CPP_ELLIPSIS
18775 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18778 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18779 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18782 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18787 token = cp_lexer_peek_nth_token (parser->lexer, n);
18788 if (token->type == CPP_LESS)
18790 /* Check for the sequence `<::' in the original code. It would be lexed as
18791 `[:', where `[' is a digraph, and there is no whitespace before
18793 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18796 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18797 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18803 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18804 or none_type otherwise. */
18806 static enum tag_types
18807 cp_parser_token_is_class_key (cp_token* token)
18809 switch (token->keyword)
18814 return record_type;
18823 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18826 cp_parser_check_class_key (enum tag_types class_key, tree type)
18828 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18829 permerror (input_location, "%qs tag used in naming %q#T",
18830 class_key == union_type ? "union"
18831 : class_key == record_type ? "struct" : "class",
18835 /* Issue an error message if DECL is redeclared with different
18836 access than its original declaration [class.access.spec/3].
18837 This applies to nested classes and nested class templates.
18841 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18843 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18846 if ((TREE_PRIVATE (decl)
18847 != (current_access_specifier == access_private_node))
18848 || (TREE_PROTECTED (decl)
18849 != (current_access_specifier == access_protected_node)))
18850 error ("%H%qD redeclared with different access", &location, decl);
18853 /* Look for the `template' keyword, as a syntactic disambiguator.
18854 Return TRUE iff it is present, in which case it will be
18858 cp_parser_optional_template_keyword (cp_parser *parser)
18860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18862 /* The `template' keyword can only be used within templates;
18863 outside templates the parser can always figure out what is a
18864 template and what is not. */
18865 if (!processing_template_decl)
18867 cp_token *token = cp_lexer_peek_token (parser->lexer);
18868 error ("%H%<template%> (as a disambiguator) is only allowed "
18869 "within templates", &token->location);
18870 /* If this part of the token stream is rescanned, the same
18871 error message would be generated. So, we purge the token
18872 from the stream. */
18873 cp_lexer_purge_token (parser->lexer);
18878 /* Consume the `template' keyword. */
18879 cp_lexer_consume_token (parser->lexer);
18887 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18888 set PARSER->SCOPE, and perform other related actions. */
18891 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18894 struct tree_check *check_value;
18895 deferred_access_check *chk;
18896 VEC (deferred_access_check,gc) *checks;
18898 /* Get the stored value. */
18899 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18900 /* Perform any access checks that were deferred. */
18901 checks = check_value->checks;
18905 VEC_iterate (deferred_access_check, checks, i, chk) ;
18908 perform_or_defer_access_check (chk->binfo,
18913 /* Set the scope from the stored value. */
18914 parser->scope = check_value->value;
18915 parser->qualifying_scope = check_value->qualifying_scope;
18916 parser->object_scope = NULL_TREE;
18919 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18920 encounter the end of a block before what we were looking for. */
18923 cp_parser_cache_group (cp_parser *parser,
18924 enum cpp_ttype end,
18929 cp_token *token = cp_lexer_peek_token (parser->lexer);
18931 /* Abort a parenthesized expression if we encounter a semicolon. */
18932 if ((end == CPP_CLOSE_PAREN || depth == 0)
18933 && token->type == CPP_SEMICOLON)
18935 /* If we've reached the end of the file, stop. */
18936 if (token->type == CPP_EOF
18937 || (end != CPP_PRAGMA_EOL
18938 && token->type == CPP_PRAGMA_EOL))
18940 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18941 /* We've hit the end of an enclosing block, so there's been some
18942 kind of syntax error. */
18945 /* Consume the token. */
18946 cp_lexer_consume_token (parser->lexer);
18947 /* See if it starts a new group. */
18948 if (token->type == CPP_OPEN_BRACE)
18950 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18951 /* In theory this should probably check end == '}', but
18952 cp_parser_save_member_function_body needs it to exit
18953 after either '}' or ')' when called with ')'. */
18957 else if (token->type == CPP_OPEN_PAREN)
18959 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18960 if (depth == 0 && end == CPP_CLOSE_PAREN)
18963 else if (token->type == CPP_PRAGMA)
18964 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18965 else if (token->type == end)
18970 /* Begin parsing tentatively. We always save tokens while parsing
18971 tentatively so that if the tentative parsing fails we can restore the
18975 cp_parser_parse_tentatively (cp_parser* parser)
18977 /* Enter a new parsing context. */
18978 parser->context = cp_parser_context_new (parser->context);
18979 /* Begin saving tokens. */
18980 cp_lexer_save_tokens (parser->lexer);
18981 /* In order to avoid repetitive access control error messages,
18982 access checks are queued up until we are no longer parsing
18984 push_deferring_access_checks (dk_deferred);
18987 /* Commit to the currently active tentative parse. */
18990 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18992 cp_parser_context *context;
18995 /* Mark all of the levels as committed. */
18996 lexer = parser->lexer;
18997 for (context = parser->context; context->next; context = context->next)
18999 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19001 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19002 while (!cp_lexer_saving_tokens (lexer))
19003 lexer = lexer->next;
19004 cp_lexer_commit_tokens (lexer);
19008 /* Abort the currently active tentative parse. All consumed tokens
19009 will be rolled back, and no diagnostics will be issued. */
19012 cp_parser_abort_tentative_parse (cp_parser* parser)
19014 cp_parser_simulate_error (parser);
19015 /* Now, pretend that we want to see if the construct was
19016 successfully parsed. */
19017 cp_parser_parse_definitely (parser);
19020 /* Stop parsing tentatively. If a parse error has occurred, restore the
19021 token stream. Otherwise, commit to the tokens we have consumed.
19022 Returns true if no error occurred; false otherwise. */
19025 cp_parser_parse_definitely (cp_parser* parser)
19027 bool error_occurred;
19028 cp_parser_context *context;
19030 /* Remember whether or not an error occurred, since we are about to
19031 destroy that information. */
19032 error_occurred = cp_parser_error_occurred (parser);
19033 /* Remove the topmost context from the stack. */
19034 context = parser->context;
19035 parser->context = context->next;
19036 /* If no parse errors occurred, commit to the tentative parse. */
19037 if (!error_occurred)
19039 /* Commit to the tokens read tentatively, unless that was
19041 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19042 cp_lexer_commit_tokens (parser->lexer);
19044 pop_to_parent_deferring_access_checks ();
19046 /* Otherwise, if errors occurred, roll back our state so that things
19047 are just as they were before we began the tentative parse. */
19050 cp_lexer_rollback_tokens (parser->lexer);
19051 pop_deferring_access_checks ();
19053 /* Add the context to the front of the free list. */
19054 context->next = cp_parser_context_free_list;
19055 cp_parser_context_free_list = context;
19057 return !error_occurred;
19060 /* Returns true if we are parsing tentatively and are not committed to
19061 this tentative parse. */
19064 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19066 return (cp_parser_parsing_tentatively (parser)
19067 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19070 /* Returns nonzero iff an error has occurred during the most recent
19071 tentative parse. */
19074 cp_parser_error_occurred (cp_parser* parser)
19076 return (cp_parser_parsing_tentatively (parser)
19077 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19080 /* Returns nonzero if GNU extensions are allowed. */
19083 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19085 return parser->allow_gnu_extensions_p;
19088 /* Objective-C++ Productions */
19091 /* Parse an Objective-C expression, which feeds into a primary-expression
19095 objc-message-expression
19096 objc-string-literal
19097 objc-encode-expression
19098 objc-protocol-expression
19099 objc-selector-expression
19101 Returns a tree representation of the expression. */
19104 cp_parser_objc_expression (cp_parser* parser)
19106 /* Try to figure out what kind of declaration is present. */
19107 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19111 case CPP_OPEN_SQUARE:
19112 return cp_parser_objc_message_expression (parser);
19114 case CPP_OBJC_STRING:
19115 kwd = cp_lexer_consume_token (parser->lexer);
19116 return objc_build_string_object (kwd->u.value);
19119 switch (kwd->keyword)
19121 case RID_AT_ENCODE:
19122 return cp_parser_objc_encode_expression (parser);
19124 case RID_AT_PROTOCOL:
19125 return cp_parser_objc_protocol_expression (parser);
19127 case RID_AT_SELECTOR:
19128 return cp_parser_objc_selector_expression (parser);
19134 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19135 &kwd->location, kwd->u.value);
19136 cp_parser_skip_to_end_of_block_or_statement (parser);
19139 return error_mark_node;
19142 /* Parse an Objective-C message expression.
19144 objc-message-expression:
19145 [ objc-message-receiver objc-message-args ]
19147 Returns a representation of an Objective-C message. */
19150 cp_parser_objc_message_expression (cp_parser* parser)
19152 tree receiver, messageargs;
19154 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19155 receiver = cp_parser_objc_message_receiver (parser);
19156 messageargs = cp_parser_objc_message_args (parser);
19157 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19159 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19162 /* Parse an objc-message-receiver.
19164 objc-message-receiver:
19166 simple-type-specifier
19168 Returns a representation of the type or expression. */
19171 cp_parser_objc_message_receiver (cp_parser* parser)
19175 /* An Objective-C message receiver may be either (1) a type
19176 or (2) an expression. */
19177 cp_parser_parse_tentatively (parser);
19178 rcv = cp_parser_expression (parser, false, NULL);
19180 if (cp_parser_parse_definitely (parser))
19183 rcv = cp_parser_simple_type_specifier (parser,
19184 /*decl_specs=*/NULL,
19185 CP_PARSER_FLAGS_NONE);
19187 return objc_get_class_reference (rcv);
19190 /* Parse the arguments and selectors comprising an Objective-C message.
19195 objc-selector-args , objc-comma-args
19197 objc-selector-args:
19198 objc-selector [opt] : assignment-expression
19199 objc-selector-args objc-selector [opt] : assignment-expression
19202 assignment-expression
19203 objc-comma-args , assignment-expression
19205 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19206 selector arguments and TREE_VALUE containing a list of comma
19210 cp_parser_objc_message_args (cp_parser* parser)
19212 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19213 bool maybe_unary_selector_p = true;
19214 cp_token *token = cp_lexer_peek_token (parser->lexer);
19216 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19218 tree selector = NULL_TREE, arg;
19220 if (token->type != CPP_COLON)
19221 selector = cp_parser_objc_selector (parser);
19223 /* Detect if we have a unary selector. */
19224 if (maybe_unary_selector_p
19225 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19226 return build_tree_list (selector, NULL_TREE);
19228 maybe_unary_selector_p = false;
19229 cp_parser_require (parser, CPP_COLON, "%<:%>");
19230 arg = cp_parser_assignment_expression (parser, false, NULL);
19233 = chainon (sel_args,
19234 build_tree_list (selector, arg));
19236 token = cp_lexer_peek_token (parser->lexer);
19239 /* Handle non-selector arguments, if any. */
19240 while (token->type == CPP_COMMA)
19244 cp_lexer_consume_token (parser->lexer);
19245 arg = cp_parser_assignment_expression (parser, false, NULL);
19248 = chainon (addl_args,
19249 build_tree_list (NULL_TREE, arg));
19251 token = cp_lexer_peek_token (parser->lexer);
19254 return build_tree_list (sel_args, addl_args);
19257 /* Parse an Objective-C encode expression.
19259 objc-encode-expression:
19260 @encode objc-typename
19262 Returns an encoded representation of the type argument. */
19265 cp_parser_objc_encode_expression (cp_parser* parser)
19270 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19271 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19272 token = cp_lexer_peek_token (parser->lexer);
19273 type = complete_type (cp_parser_type_id (parser));
19274 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19278 error ("%H%<@encode%> must specify a type as an argument",
19280 return error_mark_node;
19283 return objc_build_encode_expr (type);
19286 /* Parse an Objective-C @defs expression. */
19289 cp_parser_objc_defs_expression (cp_parser *parser)
19293 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19294 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19295 name = cp_parser_identifier (parser);
19296 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19298 return objc_get_class_ivars (name);
19301 /* Parse an Objective-C protocol expression.
19303 objc-protocol-expression:
19304 @protocol ( identifier )
19306 Returns a representation of the protocol expression. */
19309 cp_parser_objc_protocol_expression (cp_parser* parser)
19313 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19314 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19315 proto = cp_parser_identifier (parser);
19316 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19318 return objc_build_protocol_expr (proto);
19321 /* Parse an Objective-C selector expression.
19323 objc-selector-expression:
19324 @selector ( objc-method-signature )
19326 objc-method-signature:
19332 objc-selector-seq objc-selector :
19334 Returns a representation of the method selector. */
19337 cp_parser_objc_selector_expression (cp_parser* parser)
19339 tree sel_seq = NULL_TREE;
19340 bool maybe_unary_selector_p = true;
19343 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19344 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19345 token = cp_lexer_peek_token (parser->lexer);
19347 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19348 || token->type == CPP_SCOPE)
19350 tree selector = NULL_TREE;
19352 if (token->type != CPP_COLON
19353 || token->type == CPP_SCOPE)
19354 selector = cp_parser_objc_selector (parser);
19356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19357 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19359 /* Detect if we have a unary selector. */
19360 if (maybe_unary_selector_p)
19362 sel_seq = selector;
19363 goto finish_selector;
19367 cp_parser_error (parser, "expected %<:%>");
19370 maybe_unary_selector_p = false;
19371 token = cp_lexer_consume_token (parser->lexer);
19373 if (token->type == CPP_SCOPE)
19376 = chainon (sel_seq,
19377 build_tree_list (selector, NULL_TREE));
19379 = chainon (sel_seq,
19380 build_tree_list (NULL_TREE, NULL_TREE));
19384 = chainon (sel_seq,
19385 build_tree_list (selector, NULL_TREE));
19387 token = cp_lexer_peek_token (parser->lexer);
19391 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19393 return objc_build_selector_expr (sel_seq);
19396 /* Parse a list of identifiers.
19398 objc-identifier-list:
19400 objc-identifier-list , identifier
19402 Returns a TREE_LIST of identifier nodes. */
19405 cp_parser_objc_identifier_list (cp_parser* parser)
19407 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19408 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19410 while (sep->type == CPP_COMMA)
19412 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19413 list = chainon (list,
19414 build_tree_list (NULL_TREE,
19415 cp_parser_identifier (parser)));
19416 sep = cp_lexer_peek_token (parser->lexer);
19422 /* Parse an Objective-C alias declaration.
19424 objc-alias-declaration:
19425 @compatibility_alias identifier identifier ;
19427 This function registers the alias mapping with the Objective-C front end.
19428 It returns nothing. */
19431 cp_parser_objc_alias_declaration (cp_parser* parser)
19435 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19436 alias = cp_parser_identifier (parser);
19437 orig = cp_parser_identifier (parser);
19438 objc_declare_alias (alias, orig);
19439 cp_parser_consume_semicolon_at_end_of_statement (parser);
19442 /* Parse an Objective-C class forward-declaration.
19444 objc-class-declaration:
19445 @class objc-identifier-list ;
19447 The function registers the forward declarations with the Objective-C
19448 front end. It returns nothing. */
19451 cp_parser_objc_class_declaration (cp_parser* parser)
19453 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19454 objc_declare_class (cp_parser_objc_identifier_list (parser));
19455 cp_parser_consume_semicolon_at_end_of_statement (parser);
19458 /* Parse a list of Objective-C protocol references.
19460 objc-protocol-refs-opt:
19461 objc-protocol-refs [opt]
19463 objc-protocol-refs:
19464 < objc-identifier-list >
19466 Returns a TREE_LIST of identifiers, if any. */
19469 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19471 tree protorefs = NULL_TREE;
19473 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19475 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19476 protorefs = cp_parser_objc_identifier_list (parser);
19477 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19483 /* Parse a Objective-C visibility specification. */
19486 cp_parser_objc_visibility_spec (cp_parser* parser)
19488 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19490 switch (vis->keyword)
19492 case RID_AT_PRIVATE:
19493 objc_set_visibility (2);
19495 case RID_AT_PROTECTED:
19496 objc_set_visibility (0);
19498 case RID_AT_PUBLIC:
19499 objc_set_visibility (1);
19505 /* Eat '@private'/'@protected'/'@public'. */
19506 cp_lexer_consume_token (parser->lexer);
19509 /* Parse an Objective-C method type. */
19512 cp_parser_objc_method_type (cp_parser* parser)
19514 objc_set_method_type
19515 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19520 /* Parse an Objective-C protocol qualifier. */
19523 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19525 tree quals = NULL_TREE, node;
19526 cp_token *token = cp_lexer_peek_token (parser->lexer);
19528 node = token->u.value;
19530 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19531 && (node == ridpointers [(int) RID_IN]
19532 || node == ridpointers [(int) RID_OUT]
19533 || node == ridpointers [(int) RID_INOUT]
19534 || node == ridpointers [(int) RID_BYCOPY]
19535 || node == ridpointers [(int) RID_BYREF]
19536 || node == ridpointers [(int) RID_ONEWAY]))
19538 quals = tree_cons (NULL_TREE, node, quals);
19539 cp_lexer_consume_token (parser->lexer);
19540 token = cp_lexer_peek_token (parser->lexer);
19541 node = token->u.value;
19547 /* Parse an Objective-C typename. */
19550 cp_parser_objc_typename (cp_parser* parser)
19552 tree type_name = NULL_TREE;
19554 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19556 tree proto_quals, cp_type = NULL_TREE;
19558 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19559 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19561 /* An ObjC type name may consist of just protocol qualifiers, in which
19562 case the type shall default to 'id'. */
19563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19564 cp_type = cp_parser_type_id (parser);
19566 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19567 type_name = build_tree_list (proto_quals, cp_type);
19573 /* Check to see if TYPE refers to an Objective-C selector name. */
19576 cp_parser_objc_selector_p (enum cpp_ttype type)
19578 return (type == CPP_NAME || type == CPP_KEYWORD
19579 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19580 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19581 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19582 || type == CPP_XOR || type == CPP_XOR_EQ);
19585 /* Parse an Objective-C selector. */
19588 cp_parser_objc_selector (cp_parser* parser)
19590 cp_token *token = cp_lexer_consume_token (parser->lexer);
19592 if (!cp_parser_objc_selector_p (token->type))
19594 error ("%Hinvalid Objective-C++ selector name", &token->location);
19595 return error_mark_node;
19598 /* C++ operator names are allowed to appear in ObjC selectors. */
19599 switch (token->type)
19601 case CPP_AND_AND: return get_identifier ("and");
19602 case CPP_AND_EQ: return get_identifier ("and_eq");
19603 case CPP_AND: return get_identifier ("bitand");
19604 case CPP_OR: return get_identifier ("bitor");
19605 case CPP_COMPL: return get_identifier ("compl");
19606 case CPP_NOT: return get_identifier ("not");
19607 case CPP_NOT_EQ: return get_identifier ("not_eq");
19608 case CPP_OR_OR: return get_identifier ("or");
19609 case CPP_OR_EQ: return get_identifier ("or_eq");
19610 case CPP_XOR: return get_identifier ("xor");
19611 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19612 default: return token->u.value;
19616 /* Parse an Objective-C params list. */
19619 cp_parser_objc_method_keyword_params (cp_parser* parser)
19621 tree params = NULL_TREE;
19622 bool maybe_unary_selector_p = true;
19623 cp_token *token = cp_lexer_peek_token (parser->lexer);
19625 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19627 tree selector = NULL_TREE, type_name, identifier;
19629 if (token->type != CPP_COLON)
19630 selector = cp_parser_objc_selector (parser);
19632 /* Detect if we have a unary selector. */
19633 if (maybe_unary_selector_p
19634 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19637 maybe_unary_selector_p = false;
19638 cp_parser_require (parser, CPP_COLON, "%<:%>");
19639 type_name = cp_parser_objc_typename (parser);
19640 identifier = cp_parser_identifier (parser);
19644 objc_build_keyword_decl (selector,
19648 token = cp_lexer_peek_token (parser->lexer);
19654 /* Parse the non-keyword Objective-C params. */
19657 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19659 tree params = make_node (TREE_LIST);
19660 cp_token *token = cp_lexer_peek_token (parser->lexer);
19661 *ellipsisp = false; /* Initially, assume no ellipsis. */
19663 while (token->type == CPP_COMMA)
19665 cp_parameter_declarator *parmdecl;
19668 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19669 token = cp_lexer_peek_token (parser->lexer);
19671 if (token->type == CPP_ELLIPSIS)
19673 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19678 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19679 parm = grokdeclarator (parmdecl->declarator,
19680 &parmdecl->decl_specifiers,
19681 PARM, /*initialized=*/0,
19682 /*attrlist=*/NULL);
19684 chainon (params, build_tree_list (NULL_TREE, parm));
19685 token = cp_lexer_peek_token (parser->lexer);
19691 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19694 cp_parser_objc_interstitial_code (cp_parser* parser)
19696 cp_token *token = cp_lexer_peek_token (parser->lexer);
19698 /* If the next token is `extern' and the following token is a string
19699 literal, then we have a linkage specification. */
19700 if (token->keyword == RID_EXTERN
19701 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19702 cp_parser_linkage_specification (parser);
19703 /* Handle #pragma, if any. */
19704 else if (token->type == CPP_PRAGMA)
19705 cp_parser_pragma (parser, pragma_external);
19706 /* Allow stray semicolons. */
19707 else if (token->type == CPP_SEMICOLON)
19708 cp_lexer_consume_token (parser->lexer);
19709 /* Finally, try to parse a block-declaration, or a function-definition. */
19711 cp_parser_block_declaration (parser, /*statement_p=*/false);
19714 /* Parse a method signature. */
19717 cp_parser_objc_method_signature (cp_parser* parser)
19719 tree rettype, kwdparms, optparms;
19720 bool ellipsis = false;
19722 cp_parser_objc_method_type (parser);
19723 rettype = cp_parser_objc_typename (parser);
19724 kwdparms = cp_parser_objc_method_keyword_params (parser);
19725 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19727 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19730 /* Pars an Objective-C method prototype list. */
19733 cp_parser_objc_method_prototype_list (cp_parser* parser)
19735 cp_token *token = cp_lexer_peek_token (parser->lexer);
19737 while (token->keyword != RID_AT_END)
19739 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19741 objc_add_method_declaration
19742 (cp_parser_objc_method_signature (parser));
19743 cp_parser_consume_semicolon_at_end_of_statement (parser);
19746 /* Allow for interspersed non-ObjC++ code. */
19747 cp_parser_objc_interstitial_code (parser);
19749 token = cp_lexer_peek_token (parser->lexer);
19752 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19753 objc_finish_interface ();
19756 /* Parse an Objective-C method definition list. */
19759 cp_parser_objc_method_definition_list (cp_parser* parser)
19761 cp_token *token = cp_lexer_peek_token (parser->lexer);
19763 while (token->keyword != RID_AT_END)
19767 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19769 push_deferring_access_checks (dk_deferred);
19770 objc_start_method_definition
19771 (cp_parser_objc_method_signature (parser));
19773 /* For historical reasons, we accept an optional semicolon. */
19774 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19775 cp_lexer_consume_token (parser->lexer);
19777 perform_deferred_access_checks ();
19778 stop_deferring_access_checks ();
19779 meth = cp_parser_function_definition_after_declarator (parser,
19781 pop_deferring_access_checks ();
19782 objc_finish_method_definition (meth);
19785 /* Allow for interspersed non-ObjC++ code. */
19786 cp_parser_objc_interstitial_code (parser);
19788 token = cp_lexer_peek_token (parser->lexer);
19791 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19792 objc_finish_implementation ();
19795 /* Parse Objective-C ivars. */
19798 cp_parser_objc_class_ivars (cp_parser* parser)
19800 cp_token *token = cp_lexer_peek_token (parser->lexer);
19802 if (token->type != CPP_OPEN_BRACE)
19803 return; /* No ivars specified. */
19805 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19806 token = cp_lexer_peek_token (parser->lexer);
19808 while (token->type != CPP_CLOSE_BRACE)
19810 cp_decl_specifier_seq declspecs;
19811 int decl_class_or_enum_p;
19812 tree prefix_attributes;
19814 cp_parser_objc_visibility_spec (parser);
19816 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19819 cp_parser_decl_specifier_seq (parser,
19820 CP_PARSER_FLAGS_OPTIONAL,
19822 &decl_class_or_enum_p);
19823 prefix_attributes = declspecs.attributes;
19824 declspecs.attributes = NULL_TREE;
19826 /* Keep going until we hit the `;' at the end of the
19828 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19830 tree width = NULL_TREE, attributes, first_attribute, decl;
19831 cp_declarator *declarator = NULL;
19832 int ctor_dtor_or_conv_p;
19834 /* Check for a (possibly unnamed) bitfield declaration. */
19835 token = cp_lexer_peek_token (parser->lexer);
19836 if (token->type == CPP_COLON)
19839 if (token->type == CPP_NAME
19840 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19843 /* Get the name of the bitfield. */
19844 declarator = make_id_declarator (NULL_TREE,
19845 cp_parser_identifier (parser),
19849 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19850 /* Get the width of the bitfield. */
19852 = cp_parser_constant_expression (parser,
19853 /*allow_non_constant=*/false,
19858 /* Parse the declarator. */
19860 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19861 &ctor_dtor_or_conv_p,
19862 /*parenthesized_p=*/NULL,
19863 /*member_p=*/false);
19866 /* Look for attributes that apply to the ivar. */
19867 attributes = cp_parser_attributes_opt (parser);
19868 /* Remember which attributes are prefix attributes and
19870 first_attribute = attributes;
19871 /* Combine the attributes. */
19872 attributes = chainon (prefix_attributes, attributes);
19875 /* Create the bitfield declaration. */
19876 decl = grokbitfield (declarator, &declspecs,
19880 decl = grokfield (declarator, &declspecs,
19881 NULL_TREE, /*init_const_expr_p=*/false,
19882 NULL_TREE, attributes);
19884 /* Add the instance variable. */
19885 objc_add_instance_variable (decl);
19887 /* Reset PREFIX_ATTRIBUTES. */
19888 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19889 attributes = TREE_CHAIN (attributes);
19891 TREE_CHAIN (attributes) = NULL_TREE;
19893 token = cp_lexer_peek_token (parser->lexer);
19895 if (token->type == CPP_COMMA)
19897 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19903 cp_parser_consume_semicolon_at_end_of_statement (parser);
19904 token = cp_lexer_peek_token (parser->lexer);
19907 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19908 /* For historical reasons, we accept an optional semicolon. */
19909 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19910 cp_lexer_consume_token (parser->lexer);
19913 /* Parse an Objective-C protocol declaration. */
19916 cp_parser_objc_protocol_declaration (cp_parser* parser)
19918 tree proto, protorefs;
19921 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19922 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19924 tok = cp_lexer_peek_token (parser->lexer);
19925 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19929 /* See if we have a forward declaration or a definition. */
19930 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19932 /* Try a forward declaration first. */
19933 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19935 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19937 cp_parser_consume_semicolon_at_end_of_statement (parser);
19940 /* Ok, we got a full-fledged definition (or at least should). */
19943 proto = cp_parser_identifier (parser);
19944 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19945 objc_start_protocol (proto, protorefs);
19946 cp_parser_objc_method_prototype_list (parser);
19950 /* Parse an Objective-C superclass or category. */
19953 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19956 cp_token *next = cp_lexer_peek_token (parser->lexer);
19958 *super = *categ = NULL_TREE;
19959 if (next->type == CPP_COLON)
19961 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19962 *super = cp_parser_identifier (parser);
19964 else if (next->type == CPP_OPEN_PAREN)
19966 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19967 *categ = cp_parser_identifier (parser);
19968 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19972 /* Parse an Objective-C class interface. */
19975 cp_parser_objc_class_interface (cp_parser* parser)
19977 tree name, super, categ, protos;
19979 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19980 name = cp_parser_identifier (parser);
19981 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19982 protos = cp_parser_objc_protocol_refs_opt (parser);
19984 /* We have either a class or a category on our hands. */
19986 objc_start_category_interface (name, categ, protos);
19989 objc_start_class_interface (name, super, protos);
19990 /* Handle instance variable declarations, if any. */
19991 cp_parser_objc_class_ivars (parser);
19992 objc_continue_interface ();
19995 cp_parser_objc_method_prototype_list (parser);
19998 /* Parse an Objective-C class implementation. */
20001 cp_parser_objc_class_implementation (cp_parser* parser)
20003 tree name, super, categ;
20005 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20006 name = cp_parser_identifier (parser);
20007 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20009 /* We have either a class or a category on our hands. */
20011 objc_start_category_implementation (name, categ);
20014 objc_start_class_implementation (name, super);
20015 /* Handle instance variable declarations, if any. */
20016 cp_parser_objc_class_ivars (parser);
20017 objc_continue_implementation ();
20020 cp_parser_objc_method_definition_list (parser);
20023 /* Consume the @end token and finish off the implementation. */
20026 cp_parser_objc_end_implementation (cp_parser* parser)
20028 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20029 objc_finish_implementation ();
20032 /* Parse an Objective-C declaration. */
20035 cp_parser_objc_declaration (cp_parser* parser)
20037 /* Try to figure out what kind of declaration is present. */
20038 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20040 switch (kwd->keyword)
20043 cp_parser_objc_alias_declaration (parser);
20046 cp_parser_objc_class_declaration (parser);
20048 case RID_AT_PROTOCOL:
20049 cp_parser_objc_protocol_declaration (parser);
20051 case RID_AT_INTERFACE:
20052 cp_parser_objc_class_interface (parser);
20054 case RID_AT_IMPLEMENTATION:
20055 cp_parser_objc_class_implementation (parser);
20058 cp_parser_objc_end_implementation (parser);
20061 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20062 &kwd->location, kwd->u.value);
20063 cp_parser_skip_to_end_of_block_or_statement (parser);
20067 /* Parse an Objective-C try-catch-finally statement.
20069 objc-try-catch-finally-stmt:
20070 @try compound-statement objc-catch-clause-seq [opt]
20071 objc-finally-clause [opt]
20073 objc-catch-clause-seq:
20074 objc-catch-clause objc-catch-clause-seq [opt]
20077 @catch ( exception-declaration ) compound-statement
20079 objc-finally-clause
20080 @finally compound-statement
20082 Returns NULL_TREE. */
20085 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20086 location_t location;
20089 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20090 location = cp_lexer_peek_token (parser->lexer)->location;
20091 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20092 node, lest it get absorbed into the surrounding block. */
20093 stmt = push_stmt_list ();
20094 cp_parser_compound_statement (parser, NULL, false);
20095 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20097 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20099 cp_parameter_declarator *parmdecl;
20102 cp_lexer_consume_token (parser->lexer);
20103 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20104 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20105 parm = grokdeclarator (parmdecl->declarator,
20106 &parmdecl->decl_specifiers,
20107 PARM, /*initialized=*/0,
20108 /*attrlist=*/NULL);
20109 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20110 objc_begin_catch_clause (parm);
20111 cp_parser_compound_statement (parser, NULL, false);
20112 objc_finish_catch_clause ();
20115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20117 cp_lexer_consume_token (parser->lexer);
20118 location = cp_lexer_peek_token (parser->lexer)->location;
20119 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20120 node, lest it get absorbed into the surrounding block. */
20121 stmt = push_stmt_list ();
20122 cp_parser_compound_statement (parser, NULL, false);
20123 objc_build_finally_clause (location, pop_stmt_list (stmt));
20126 return objc_finish_try_stmt ();
20129 /* Parse an Objective-C synchronized statement.
20131 objc-synchronized-stmt:
20132 @synchronized ( expression ) compound-statement
20134 Returns NULL_TREE. */
20137 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20138 location_t location;
20141 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20143 location = cp_lexer_peek_token (parser->lexer)->location;
20144 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20145 lock = cp_parser_expression (parser, false, NULL);
20146 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20148 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20149 node, lest it get absorbed into the surrounding block. */
20150 stmt = push_stmt_list ();
20151 cp_parser_compound_statement (parser, NULL, false);
20153 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20156 /* Parse an Objective-C throw statement.
20159 @throw assignment-expression [opt] ;
20161 Returns a constructed '@throw' statement. */
20164 cp_parser_objc_throw_statement (cp_parser *parser) {
20165 tree expr = NULL_TREE;
20167 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20170 expr = cp_parser_assignment_expression (parser, false, NULL);
20172 cp_parser_consume_semicolon_at_end_of_statement (parser);
20174 return objc_build_throw_stmt (expr);
20177 /* Parse an Objective-C statement. */
20180 cp_parser_objc_statement (cp_parser * parser) {
20181 /* Try to figure out what kind of declaration is present. */
20182 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20184 switch (kwd->keyword)
20187 return cp_parser_objc_try_catch_finally_statement (parser);
20188 case RID_AT_SYNCHRONIZED:
20189 return cp_parser_objc_synchronized_statement (parser);
20191 return cp_parser_objc_throw_statement (parser);
20193 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20194 &kwd->location, kwd->u.value);
20195 cp_parser_skip_to_end_of_block_or_statement (parser);
20198 return error_mark_node;
20201 /* OpenMP 2.5 parsing routines. */
20203 /* Returns name of the next clause.
20204 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20205 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20206 returned and the token is consumed. */
20208 static pragma_omp_clause
20209 cp_parser_omp_clause_name (cp_parser *parser)
20211 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20214 result = PRAGMA_OMP_CLAUSE_IF;
20215 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20216 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20217 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20218 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20219 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20221 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20222 const char *p = IDENTIFIER_POINTER (id);
20227 if (!strcmp ("collapse", p))
20228 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20229 else if (!strcmp ("copyin", p))
20230 result = PRAGMA_OMP_CLAUSE_COPYIN;
20231 else if (!strcmp ("copyprivate", p))
20232 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20235 if (!strcmp ("firstprivate", p))
20236 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20239 if (!strcmp ("lastprivate", p))
20240 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20243 if (!strcmp ("nowait", p))
20244 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20245 else if (!strcmp ("num_threads", p))
20246 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20249 if (!strcmp ("ordered", p))
20250 result = PRAGMA_OMP_CLAUSE_ORDERED;
20253 if (!strcmp ("reduction", p))
20254 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20257 if (!strcmp ("schedule", p))
20258 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20259 else if (!strcmp ("shared", p))
20260 result = PRAGMA_OMP_CLAUSE_SHARED;
20263 if (!strcmp ("untied", p))
20264 result = PRAGMA_OMP_CLAUSE_UNTIED;
20269 if (result != PRAGMA_OMP_CLAUSE_NONE)
20270 cp_lexer_consume_token (parser->lexer);
20275 /* Validate that a clause of the given type does not already exist. */
20278 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20279 const char *name, location_t location)
20283 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20284 if (OMP_CLAUSE_CODE (c) == code)
20286 error ("%Htoo many %qs clauses", &location, name);
20294 variable-list , identifier
20296 In addition, we match a closing parenthesis. An opening parenthesis
20297 will have been consumed by the caller.
20299 If KIND is nonzero, create the appropriate node and install the decl
20300 in OMP_CLAUSE_DECL and add the node to the head of the list.
20302 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20303 return the list created. */
20306 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20314 token = cp_lexer_peek_token (parser->lexer);
20315 name = cp_parser_id_expression (parser, /*template_p=*/false,
20316 /*check_dependency_p=*/true,
20317 /*template_p=*/NULL,
20318 /*declarator_p=*/false,
20319 /*optional_p=*/false);
20320 if (name == error_mark_node)
20323 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20324 if (decl == error_mark_node)
20325 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20326 else if (kind != 0)
20328 tree u = build_omp_clause (kind);
20329 OMP_CLAUSE_DECL (u) = decl;
20330 OMP_CLAUSE_CHAIN (u) = list;
20334 list = tree_cons (decl, NULL_TREE, list);
20337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20339 cp_lexer_consume_token (parser->lexer);
20342 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20346 /* Try to resync to an unnested comma. Copied from
20347 cp_parser_parenthesized_expression_list. */
20349 ending = cp_parser_skip_to_closing_parenthesis (parser,
20350 /*recovering=*/true,
20352 /*consume_paren=*/true);
20360 /* Similarly, but expect leading and trailing parenthesis. This is a very
20361 common case for omp clauses. */
20364 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20366 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20367 return cp_parser_omp_var_list_no_open (parser, kind, list);
20372 collapse ( constant-expression ) */
20375 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20381 loc = cp_lexer_peek_token (parser->lexer)->location;
20382 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20385 num = cp_parser_constant_expression (parser, false, NULL);
20387 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20388 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20389 /*or_comma=*/false,
20390 /*consume_paren=*/true);
20392 if (num == error_mark_node)
20394 num = fold_non_dependent_expr (num);
20395 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20396 || !host_integerp (num, 0)
20397 || (n = tree_low_cst (num, 0)) <= 0
20400 error ("%Hcollapse argument needs positive constant integer expression",
20405 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20406 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20407 OMP_CLAUSE_CHAIN (c) = list;
20408 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20414 default ( shared | none ) */
20417 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20419 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20422 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20424 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20426 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20427 const char *p = IDENTIFIER_POINTER (id);
20432 if (strcmp ("none", p) != 0)
20434 kind = OMP_CLAUSE_DEFAULT_NONE;
20438 if (strcmp ("shared", p) != 0)
20440 kind = OMP_CLAUSE_DEFAULT_SHARED;
20447 cp_lexer_consume_token (parser->lexer);
20452 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20455 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20457 /*or_comma=*/false,
20458 /*consume_paren=*/true);
20460 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20463 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20464 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20465 OMP_CLAUSE_CHAIN (c) = list;
20466 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20472 if ( expression ) */
20475 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20479 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20482 t = cp_parser_condition (parser);
20484 if (t == error_mark_node
20485 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20486 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20487 /*or_comma=*/false,
20488 /*consume_paren=*/true);
20490 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20492 c = build_omp_clause (OMP_CLAUSE_IF);
20493 OMP_CLAUSE_IF_EXPR (c) = t;
20494 OMP_CLAUSE_CHAIN (c) = list;
20503 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20504 tree list, location_t location)
20508 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20510 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20511 OMP_CLAUSE_CHAIN (c) = list;
20516 num_threads ( expression ) */
20519 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20520 location_t location)
20524 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20527 t = cp_parser_expression (parser, false, NULL);
20529 if (t == error_mark_node
20530 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20532 /*or_comma=*/false,
20533 /*consume_paren=*/true);
20535 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20536 "num_threads", location);
20538 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20539 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20540 OMP_CLAUSE_CHAIN (c) = list;
20549 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20550 tree list, location_t location)
20554 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20555 "ordered", location);
20557 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20558 OMP_CLAUSE_CHAIN (c) = list;
20563 reduction ( reduction-operator : variable-list )
20565 reduction-operator:
20566 One of: + * - & ^ | && || */
20569 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20571 enum tree_code code;
20574 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20577 switch (cp_lexer_peek_token (parser->lexer)->type)
20589 code = BIT_AND_EXPR;
20592 code = BIT_XOR_EXPR;
20595 code = BIT_IOR_EXPR;
20598 code = TRUTH_ANDIF_EXPR;
20601 code = TRUTH_ORIF_EXPR;
20604 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20605 "%<|%>, %<&&%>, or %<||%>");
20607 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20608 /*or_comma=*/false,
20609 /*consume_paren=*/true);
20612 cp_lexer_consume_token (parser->lexer);
20614 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20617 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20618 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20619 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20625 schedule ( schedule-kind )
20626 schedule ( schedule-kind , expression )
20629 static | dynamic | guided | runtime | auto */
20632 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20636 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20639 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20644 const char *p = IDENTIFIER_POINTER (id);
20649 if (strcmp ("dynamic", p) != 0)
20651 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20655 if (strcmp ("guided", p) != 0)
20657 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20661 if (strcmp ("runtime", p) != 0)
20663 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20670 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20671 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20672 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20673 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20676 cp_lexer_consume_token (parser->lexer);
20678 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20681 cp_lexer_consume_token (parser->lexer);
20683 token = cp_lexer_peek_token (parser->lexer);
20684 t = cp_parser_assignment_expression (parser, false, NULL);
20686 if (t == error_mark_node)
20688 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20689 error ("%Hschedule %<runtime%> does not take "
20690 "a %<chunk_size%> parameter", &token->location);
20691 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20692 error ("%Hschedule %<auto%> does not take "
20693 "a %<chunk_size%> parameter", &token->location);
20695 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20697 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20700 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20703 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20704 OMP_CLAUSE_CHAIN (c) = list;
20708 cp_parser_error (parser, "invalid schedule kind");
20710 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20711 /*or_comma=*/false,
20712 /*consume_paren=*/true);
20720 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20721 tree list, location_t location)
20725 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20727 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20728 OMP_CLAUSE_CHAIN (c) = list;
20732 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20733 is a bitmask in MASK. Return the list of clauses found; the result
20734 of clause default goes in *pdefault. */
20737 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20738 const char *where, cp_token *pragma_tok)
20740 tree clauses = NULL;
20742 cp_token *token = NULL;
20744 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20746 pragma_omp_clause c_kind;
20747 const char *c_name;
20748 tree prev = clauses;
20750 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20751 cp_lexer_consume_token (parser->lexer);
20753 token = cp_lexer_peek_token (parser->lexer);
20754 c_kind = cp_parser_omp_clause_name (parser);
20759 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20760 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20762 c_name = "collapse";
20764 case PRAGMA_OMP_CLAUSE_COPYIN:
20765 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20768 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20769 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20771 c_name = "copyprivate";
20773 case PRAGMA_OMP_CLAUSE_DEFAULT:
20774 clauses = cp_parser_omp_clause_default (parser, clauses,
20776 c_name = "default";
20778 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20779 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20781 c_name = "firstprivate";
20783 case PRAGMA_OMP_CLAUSE_IF:
20784 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20787 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20788 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20790 c_name = "lastprivate";
20792 case PRAGMA_OMP_CLAUSE_NOWAIT:
20793 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20796 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20797 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20799 c_name = "num_threads";
20801 case PRAGMA_OMP_CLAUSE_ORDERED:
20802 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20804 c_name = "ordered";
20806 case PRAGMA_OMP_CLAUSE_PRIVATE:
20807 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20809 c_name = "private";
20811 case PRAGMA_OMP_CLAUSE_REDUCTION:
20812 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20813 c_name = "reduction";
20815 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20816 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20818 c_name = "schedule";
20820 case PRAGMA_OMP_CLAUSE_SHARED:
20821 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20825 case PRAGMA_OMP_CLAUSE_UNTIED:
20826 clauses = cp_parser_omp_clause_untied (parser, clauses,
20831 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20835 if (((mask >> c_kind) & 1) == 0)
20837 /* Remove the invalid clause(s) from the list to avoid
20838 confusing the rest of the compiler. */
20840 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20844 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20845 return finish_omp_clauses (clauses);
20852 In practice, we're also interested in adding the statement to an
20853 outer node. So it is convenient if we work around the fact that
20854 cp_parser_statement calls add_stmt. */
20857 cp_parser_begin_omp_structured_block (cp_parser *parser)
20859 unsigned save = parser->in_statement;
20861 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20862 This preserves the "not within loop or switch" style error messages
20863 for nonsense cases like
20869 if (parser->in_statement)
20870 parser->in_statement = IN_OMP_BLOCK;
20876 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20878 parser->in_statement = save;
20882 cp_parser_omp_structured_block (cp_parser *parser)
20884 tree stmt = begin_omp_structured_block ();
20885 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20887 cp_parser_statement (parser, NULL_TREE, false, NULL);
20889 cp_parser_end_omp_structured_block (parser, save);
20890 return finish_omp_structured_block (stmt);
20894 # pragma omp atomic new-line
20898 x binop= expr | x++ | ++x | x-- | --x
20900 +, *, -, /, &, ^, |, <<, >>
20902 where x is an lvalue expression with scalar type. */
20905 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20908 enum tree_code code;
20910 cp_parser_require_pragma_eol (parser, pragma_tok);
20912 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20913 /*cast_p=*/false, NULL);
20914 switch (TREE_CODE (lhs))
20919 case PREINCREMENT_EXPR:
20920 case POSTINCREMENT_EXPR:
20921 lhs = TREE_OPERAND (lhs, 0);
20923 rhs = integer_one_node;
20926 case PREDECREMENT_EXPR:
20927 case POSTDECREMENT_EXPR:
20928 lhs = TREE_OPERAND (lhs, 0);
20930 rhs = integer_one_node;
20934 switch (cp_lexer_peek_token (parser->lexer)->type)
20940 code = TRUNC_DIV_EXPR;
20948 case CPP_LSHIFT_EQ:
20949 code = LSHIFT_EXPR;
20951 case CPP_RSHIFT_EQ:
20952 code = RSHIFT_EXPR;
20955 code = BIT_AND_EXPR;
20958 code = BIT_IOR_EXPR;
20961 code = BIT_XOR_EXPR;
20964 cp_parser_error (parser,
20965 "invalid operator for %<#pragma omp atomic%>");
20968 cp_lexer_consume_token (parser->lexer);
20970 rhs = cp_parser_expression (parser, false, NULL);
20971 if (rhs == error_mark_node)
20975 finish_omp_atomic (code, lhs, rhs);
20976 cp_parser_consume_semicolon_at_end_of_statement (parser);
20980 cp_parser_skip_to_end_of_block_or_statement (parser);
20985 # pragma omp barrier new-line */
20988 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20990 cp_parser_require_pragma_eol (parser, pragma_tok);
20991 finish_omp_barrier ();
20995 # pragma omp critical [(name)] new-line
20996 structured-block */
20999 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21001 tree stmt, name = NULL;
21003 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21005 cp_lexer_consume_token (parser->lexer);
21007 name = cp_parser_identifier (parser);
21009 if (name == error_mark_node
21010 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21011 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21012 /*or_comma=*/false,
21013 /*consume_paren=*/true);
21014 if (name == error_mark_node)
21017 cp_parser_require_pragma_eol (parser, pragma_tok);
21019 stmt = cp_parser_omp_structured_block (parser);
21020 return c_finish_omp_critical (stmt, name);
21024 # pragma omp flush flush-vars[opt] new-line
21027 ( variable-list ) */
21030 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21032 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21033 (void) cp_parser_omp_var_list (parser, 0, NULL);
21034 cp_parser_require_pragma_eol (parser, pragma_tok);
21036 finish_omp_flush ();
21039 /* Helper function, to parse omp for increment expression. */
21042 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21044 tree cond = cp_parser_binary_expression (parser, false, true,
21045 PREC_NOT_OPERATOR, NULL);
21048 if (cond == error_mark_node
21049 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21051 cp_parser_skip_to_end_of_statement (parser);
21052 return error_mark_node;
21055 switch (TREE_CODE (cond))
21063 return error_mark_node;
21066 /* If decl is an iterator, preserve LHS and RHS of the relational
21067 expr until finish_omp_for. */
21069 && (type_dependent_expression_p (decl)
21070 || CLASS_TYPE_P (TREE_TYPE (decl))))
21073 return build_x_binary_op (TREE_CODE (cond),
21074 TREE_OPERAND (cond, 0), ERROR_MARK,
21075 TREE_OPERAND (cond, 1), ERROR_MARK,
21076 &overloaded_p, tf_warning_or_error);
21079 /* Helper function, to parse omp for increment expression. */
21082 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21084 cp_token *token = cp_lexer_peek_token (parser->lexer);
21090 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21092 op = (token->type == CPP_PLUS_PLUS
21093 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21094 cp_lexer_consume_token (parser->lexer);
21095 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21097 return error_mark_node;
21098 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21101 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21103 return error_mark_node;
21105 token = cp_lexer_peek_token (parser->lexer);
21106 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21108 op = (token->type == CPP_PLUS_PLUS
21109 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21110 cp_lexer_consume_token (parser->lexer);
21111 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21114 op = cp_parser_assignment_operator_opt (parser);
21115 if (op == ERROR_MARK)
21116 return error_mark_node;
21118 if (op != NOP_EXPR)
21120 rhs = cp_parser_assignment_expression (parser, false, NULL);
21121 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21122 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21125 lhs = cp_parser_binary_expression (parser, false, false,
21126 PREC_ADDITIVE_EXPRESSION, NULL);
21127 token = cp_lexer_peek_token (parser->lexer);
21128 decl_first = lhs == decl;
21131 if (token->type != CPP_PLUS
21132 && token->type != CPP_MINUS)
21133 return error_mark_node;
21137 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21138 cp_lexer_consume_token (parser->lexer);
21139 rhs = cp_parser_binary_expression (parser, false, false,
21140 PREC_ADDITIVE_EXPRESSION, NULL);
21141 token = cp_lexer_peek_token (parser->lexer);
21142 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21144 if (lhs == NULL_TREE)
21146 if (op == PLUS_EXPR)
21149 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21152 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21153 NULL, tf_warning_or_error);
21156 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21160 if (rhs != decl || op == MINUS_EXPR)
21161 return error_mark_node;
21162 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21165 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21167 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21170 /* Parse the restricted form of the for statement allowed by OpenMP. */
21173 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21175 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21176 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21177 tree this_pre_body, cl;
21178 location_t loc_first;
21179 bool collapse_err = false;
21180 int i, collapse = 1, nbraces = 0;
21182 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21183 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21184 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21186 gcc_assert (collapse >= 1);
21188 declv = make_tree_vec (collapse);
21189 initv = make_tree_vec (collapse);
21190 condv = make_tree_vec (collapse);
21191 incrv = make_tree_vec (collapse);
21193 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21195 for (i = 0; i < collapse; i++)
21197 int bracecount = 0;
21198 bool add_private_clause = false;
21201 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21203 cp_parser_error (parser, "for statement expected");
21206 loc = cp_lexer_consume_token (parser->lexer)->location;
21208 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21211 init = decl = real_decl = NULL;
21212 this_pre_body = push_stmt_list ();
21213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21215 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21219 integer-type var = lb
21220 random-access-iterator-type var = lb
21221 pointer-type var = lb
21223 cp_decl_specifier_seq type_specifiers;
21225 /* First, try to parse as an initialized declaration. See
21226 cp_parser_condition, from whence the bulk of this is copied. */
21228 cp_parser_parse_tentatively (parser);
21229 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21231 if (cp_parser_parse_definitely (parser))
21233 /* If parsing a type specifier seq succeeded, then this
21234 MUST be a initialized declaration. */
21235 tree asm_specification, attributes;
21236 cp_declarator *declarator;
21238 declarator = cp_parser_declarator (parser,
21239 CP_PARSER_DECLARATOR_NAMED,
21240 /*ctor_dtor_or_conv_p=*/NULL,
21241 /*parenthesized_p=*/NULL,
21242 /*member_p=*/false);
21243 attributes = cp_parser_attributes_opt (parser);
21244 asm_specification = cp_parser_asm_specification_opt (parser);
21246 if (declarator == cp_error_declarator)
21247 cp_parser_skip_to_end_of_statement (parser);
21251 tree pushed_scope, auto_node;
21253 decl = start_decl (declarator, &type_specifiers,
21254 SD_INITIALIZED, attributes,
21255 /*prefix_attributes=*/NULL_TREE,
21258 auto_node = type_uses_auto (TREE_TYPE (decl));
21259 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21261 if (cp_lexer_next_token_is (parser->lexer,
21263 error ("parenthesized initialization is not allowed in "
21264 "OpenMP %<for%> loop");
21266 /* Trigger an error. */
21267 cp_parser_require (parser, CPP_EQ, "%<=%>");
21269 init = error_mark_node;
21270 cp_parser_skip_to_end_of_statement (parser);
21272 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21273 || type_dependent_expression_p (decl)
21276 bool is_direct_init, is_non_constant_init;
21278 init = cp_parser_initializer (parser,
21280 &is_non_constant_init);
21282 if (auto_node && describable_type (init))
21285 = do_auto_deduction (TREE_TYPE (decl), init,
21288 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21289 && !type_dependent_expression_p (decl))
21293 cp_finish_decl (decl, init, !is_non_constant_init,
21295 LOOKUP_ONLYCONVERTING);
21296 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21299 = tree_cons (NULL, this_pre_body, for_block);
21303 init = pop_stmt_list (this_pre_body);
21304 this_pre_body = NULL_TREE;
21309 cp_lexer_consume_token (parser->lexer);
21310 init = cp_parser_assignment_expression (parser, false, NULL);
21313 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21314 init = error_mark_node;
21316 cp_finish_decl (decl, NULL_TREE,
21317 /*init_const_expr_p=*/false,
21319 LOOKUP_ONLYCONVERTING);
21323 pop_scope (pushed_scope);
21329 /* If parsing a type specifier sequence failed, then
21330 this MUST be a simple expression. */
21331 cp_parser_parse_tentatively (parser);
21332 decl = cp_parser_primary_expression (parser, false, false,
21334 if (!cp_parser_error_occurred (parser)
21337 && CLASS_TYPE_P (TREE_TYPE (decl)))
21341 cp_parser_parse_definitely (parser);
21342 cp_parser_require (parser, CPP_EQ, "%<=%>");
21343 rhs = cp_parser_assignment_expression (parser, false, NULL);
21344 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21346 tf_warning_or_error));
21347 add_private_clause = true;
21352 cp_parser_abort_tentative_parse (parser);
21353 init = cp_parser_expression (parser, false, NULL);
21356 if (TREE_CODE (init) == MODIFY_EXPR
21357 || TREE_CODE (init) == MODOP_EXPR)
21358 real_decl = TREE_OPERAND (init, 0);
21363 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21366 this_pre_body = pop_stmt_list (this_pre_body);
21370 pre_body = push_stmt_list ();
21372 add_stmt (this_pre_body);
21373 pre_body = pop_stmt_list (pre_body);
21376 pre_body = this_pre_body;
21381 if (par_clauses != NULL && real_decl != NULL_TREE)
21384 for (c = par_clauses; *c ; )
21385 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21386 && OMP_CLAUSE_DECL (*c) == real_decl)
21388 error ("%Hiteration variable %qD should not be firstprivate",
21390 *c = OMP_CLAUSE_CHAIN (*c);
21392 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21393 && OMP_CLAUSE_DECL (*c) == real_decl)
21395 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21396 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21397 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21398 OMP_CLAUSE_DECL (l) = real_decl;
21399 OMP_CLAUSE_CHAIN (l) = clauses;
21400 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21402 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21403 CP_OMP_CLAUSE_INFO (*c) = NULL;
21404 add_private_clause = false;
21408 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21409 && OMP_CLAUSE_DECL (*c) == real_decl)
21410 add_private_clause = false;
21411 c = &OMP_CLAUSE_CHAIN (*c);
21415 if (add_private_clause)
21418 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21420 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21421 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21422 && OMP_CLAUSE_DECL (c) == decl)
21424 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21425 && OMP_CLAUSE_DECL (c) == decl)
21426 error ("%Hiteration variable %qD should not be firstprivate",
21428 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21429 && OMP_CLAUSE_DECL (c) == decl)
21430 error ("%Hiteration variable %qD should not be reduction",
21435 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21436 OMP_CLAUSE_DECL (c) = decl;
21437 c = finish_omp_clauses (c);
21440 OMP_CLAUSE_CHAIN (c) = clauses;
21447 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21448 cond = cp_parser_omp_for_cond (parser, decl);
21449 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21454 /* If decl is an iterator, preserve the operator on decl
21455 until finish_omp_for. */
21457 && (type_dependent_expression_p (decl)
21458 || CLASS_TYPE_P (TREE_TYPE (decl))))
21459 incr = cp_parser_omp_for_incr (parser, decl);
21461 incr = cp_parser_expression (parser, false, NULL);
21464 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21465 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21466 /*or_comma=*/false,
21467 /*consume_paren=*/true);
21469 TREE_VEC_ELT (declv, i) = decl;
21470 TREE_VEC_ELT (initv, i) = init;
21471 TREE_VEC_ELT (condv, i) = cond;
21472 TREE_VEC_ELT (incrv, i) = incr;
21474 if (i == collapse - 1)
21477 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21478 in between the collapsed for loops to be still considered perfectly
21479 nested. Hopefully the final version clarifies this.
21480 For now handle (multiple) {'s and empty statements. */
21481 cp_parser_parse_tentatively (parser);
21484 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21486 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21488 cp_lexer_consume_token (parser->lexer);
21491 else if (bracecount
21492 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21493 cp_lexer_consume_token (parser->lexer);
21496 loc = cp_lexer_peek_token (parser->lexer)->location;
21497 error ("%Hnot enough collapsed for loops", &loc);
21498 collapse_err = true;
21499 cp_parser_abort_tentative_parse (parser);
21508 cp_parser_parse_definitely (parser);
21509 nbraces += bracecount;
21513 /* Note that we saved the original contents of this flag when we entered
21514 the structured block, and so we don't need to re-save it here. */
21515 parser->in_statement = IN_OMP_FOR;
21517 /* Note that the grammar doesn't call for a structured block here,
21518 though the loop as a whole is a structured block. */
21519 body = push_stmt_list ();
21520 cp_parser_statement (parser, NULL_TREE, false, NULL);
21521 body = pop_stmt_list (body);
21523 if (declv == NULL_TREE)
21526 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21527 pre_body, clauses);
21531 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21533 cp_lexer_consume_token (parser->lexer);
21536 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21537 cp_lexer_consume_token (parser->lexer);
21542 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21543 error ("%Hcollapsed loops not perfectly nested", &loc);
21545 collapse_err = true;
21546 cp_parser_statement_seq_opt (parser, NULL);
21547 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21553 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21554 for_block = TREE_CHAIN (for_block);
21561 #pragma omp for for-clause[optseq] new-line
21564 #define OMP_FOR_CLAUSE_MASK \
21565 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21566 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21567 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21568 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21569 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21570 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21571 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21572 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21575 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21577 tree clauses, sb, ret;
21580 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21581 "#pragma omp for", pragma_tok);
21583 sb = begin_omp_structured_block ();
21584 save = cp_parser_begin_omp_structured_block (parser);
21586 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21588 cp_parser_end_omp_structured_block (parser, save);
21589 add_stmt (finish_omp_structured_block (sb));
21595 # pragma omp master new-line
21596 structured-block */
21599 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21601 cp_parser_require_pragma_eol (parser, pragma_tok);
21602 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21606 # pragma omp ordered new-line
21607 structured-block */
21610 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21612 cp_parser_require_pragma_eol (parser, pragma_tok);
21613 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21619 { section-sequence }
21622 section-directive[opt] structured-block
21623 section-sequence section-directive structured-block */
21626 cp_parser_omp_sections_scope (cp_parser *parser)
21628 tree stmt, substmt;
21629 bool error_suppress = false;
21632 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21635 stmt = push_stmt_list ();
21637 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21641 substmt = begin_omp_structured_block ();
21642 save = cp_parser_begin_omp_structured_block (parser);
21646 cp_parser_statement (parser, NULL_TREE, false, NULL);
21648 tok = cp_lexer_peek_token (parser->lexer);
21649 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21651 if (tok->type == CPP_CLOSE_BRACE)
21653 if (tok->type == CPP_EOF)
21657 cp_parser_end_omp_structured_block (parser, save);
21658 substmt = finish_omp_structured_block (substmt);
21659 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21660 add_stmt (substmt);
21665 tok = cp_lexer_peek_token (parser->lexer);
21666 if (tok->type == CPP_CLOSE_BRACE)
21668 if (tok->type == CPP_EOF)
21671 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21673 cp_lexer_consume_token (parser->lexer);
21674 cp_parser_require_pragma_eol (parser, tok);
21675 error_suppress = false;
21677 else if (!error_suppress)
21679 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21680 error_suppress = true;
21683 substmt = cp_parser_omp_structured_block (parser);
21684 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21685 add_stmt (substmt);
21687 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21689 substmt = pop_stmt_list (stmt);
21691 stmt = make_node (OMP_SECTIONS);
21692 TREE_TYPE (stmt) = void_type_node;
21693 OMP_SECTIONS_BODY (stmt) = substmt;
21700 # pragma omp sections sections-clause[optseq] newline
21703 #define OMP_SECTIONS_CLAUSE_MASK \
21704 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21705 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21706 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21707 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21708 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21711 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21715 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21716 "#pragma omp sections", pragma_tok);
21718 ret = cp_parser_omp_sections_scope (parser);
21720 OMP_SECTIONS_CLAUSES (ret) = clauses;
21726 # pragma parallel parallel-clause new-line
21727 # pragma parallel for parallel-for-clause new-line
21728 # pragma parallel sections parallel-sections-clause new-line */
21730 #define OMP_PARALLEL_CLAUSE_MASK \
21731 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21732 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21733 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21734 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21735 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21736 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21737 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21738 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21741 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21743 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21744 const char *p_name = "#pragma omp parallel";
21745 tree stmt, clauses, par_clause, ws_clause, block;
21746 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21751 cp_lexer_consume_token (parser->lexer);
21752 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21753 p_name = "#pragma omp parallel for";
21754 mask |= OMP_FOR_CLAUSE_MASK;
21755 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21757 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21759 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21760 const char *p = IDENTIFIER_POINTER (id);
21761 if (strcmp (p, "sections") == 0)
21763 cp_lexer_consume_token (parser->lexer);
21764 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21765 p_name = "#pragma omp parallel sections";
21766 mask |= OMP_SECTIONS_CLAUSE_MASK;
21767 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21771 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21772 block = begin_omp_parallel ();
21773 save = cp_parser_begin_omp_structured_block (parser);
21777 case PRAGMA_OMP_PARALLEL:
21778 cp_parser_statement (parser, NULL_TREE, false, NULL);
21779 par_clause = clauses;
21782 case PRAGMA_OMP_PARALLEL_FOR:
21783 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21784 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21787 case PRAGMA_OMP_PARALLEL_SECTIONS:
21788 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21789 stmt = cp_parser_omp_sections_scope (parser);
21791 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21795 gcc_unreachable ();
21798 cp_parser_end_omp_structured_block (parser, save);
21799 stmt = finish_omp_parallel (par_clause, block);
21800 if (p_kind != PRAGMA_OMP_PARALLEL)
21801 OMP_PARALLEL_COMBINED (stmt) = 1;
21806 # pragma omp single single-clause[optseq] new-line
21807 structured-block */
21809 #define OMP_SINGLE_CLAUSE_MASK \
21810 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21811 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21812 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21813 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21816 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21818 tree stmt = make_node (OMP_SINGLE);
21819 TREE_TYPE (stmt) = void_type_node;
21821 OMP_SINGLE_CLAUSES (stmt)
21822 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21823 "#pragma omp single", pragma_tok);
21824 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21826 return add_stmt (stmt);
21830 # pragma omp task task-clause[optseq] new-line
21831 structured-block */
21833 #define OMP_TASK_CLAUSE_MASK \
21834 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21835 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21836 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21837 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21838 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21839 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21842 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21844 tree clauses, block;
21847 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21848 "#pragma omp task", pragma_tok);
21849 block = begin_omp_task ();
21850 save = cp_parser_begin_omp_structured_block (parser);
21851 cp_parser_statement (parser, NULL_TREE, false, NULL);
21852 cp_parser_end_omp_structured_block (parser, save);
21853 return finish_omp_task (clauses, block);
21857 # pragma omp taskwait new-line */
21860 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21862 cp_parser_require_pragma_eol (parser, pragma_tok);
21863 finish_omp_taskwait ();
21867 # pragma omp threadprivate (variable-list) */
21870 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21874 vars = cp_parser_omp_var_list (parser, 0, NULL);
21875 cp_parser_require_pragma_eol (parser, pragma_tok);
21877 finish_omp_threadprivate (vars);
21880 /* Main entry point to OpenMP statement pragmas. */
21883 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21887 switch (pragma_tok->pragma_kind)
21889 case PRAGMA_OMP_ATOMIC:
21890 cp_parser_omp_atomic (parser, pragma_tok);
21892 case PRAGMA_OMP_CRITICAL:
21893 stmt = cp_parser_omp_critical (parser, pragma_tok);
21895 case PRAGMA_OMP_FOR:
21896 stmt = cp_parser_omp_for (parser, pragma_tok);
21898 case PRAGMA_OMP_MASTER:
21899 stmt = cp_parser_omp_master (parser, pragma_tok);
21901 case PRAGMA_OMP_ORDERED:
21902 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21904 case PRAGMA_OMP_PARALLEL:
21905 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21907 case PRAGMA_OMP_SECTIONS:
21908 stmt = cp_parser_omp_sections (parser, pragma_tok);
21910 case PRAGMA_OMP_SINGLE:
21911 stmt = cp_parser_omp_single (parser, pragma_tok);
21913 case PRAGMA_OMP_TASK:
21914 stmt = cp_parser_omp_task (parser, pragma_tok);
21917 gcc_unreachable ();
21921 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21926 static GTY (()) cp_parser *the_parser;
21929 /* Special handling for the first token or line in the file. The first
21930 thing in the file might be #pragma GCC pch_preprocess, which loads a
21931 PCH file, which is a GC collection point. So we need to handle this
21932 first pragma without benefit of an existing lexer structure.
21934 Always returns one token to the caller in *FIRST_TOKEN. This is
21935 either the true first token of the file, or the first token after
21936 the initial pragma. */
21939 cp_parser_initial_pragma (cp_token *first_token)
21943 cp_lexer_get_preprocessor_token (NULL, first_token);
21944 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21947 cp_lexer_get_preprocessor_token (NULL, first_token);
21948 if (first_token->type == CPP_STRING)
21950 name = first_token->u.value;
21952 cp_lexer_get_preprocessor_token (NULL, first_token);
21953 if (first_token->type != CPP_PRAGMA_EOL)
21954 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21955 &first_token->location);
21958 error ("%Hexpected string literal", &first_token->location);
21960 /* Skip to the end of the pragma. */
21961 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21962 cp_lexer_get_preprocessor_token (NULL, first_token);
21964 /* Now actually load the PCH file. */
21966 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21968 /* Read one more token to return to our caller. We have to do this
21969 after reading the PCH file in, since its pointers have to be
21971 cp_lexer_get_preprocessor_token (NULL, first_token);
21974 /* Normal parsing of a pragma token. Here we can (and must) use the
21978 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21980 cp_token *pragma_tok;
21983 pragma_tok = cp_lexer_consume_token (parser->lexer);
21984 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21985 parser->lexer->in_pragma = true;
21987 id = pragma_tok->pragma_kind;
21990 case PRAGMA_GCC_PCH_PREPROCESS:
21991 error ("%H%<#pragma GCC pch_preprocess%> must be first",
21992 &pragma_tok->location);
21995 case PRAGMA_OMP_BARRIER:
21998 case pragma_compound:
21999 cp_parser_omp_barrier (parser, pragma_tok);
22002 error ("%H%<#pragma omp barrier%> may only be "
22003 "used in compound statements", &pragma_tok->location);
22010 case PRAGMA_OMP_FLUSH:
22013 case pragma_compound:
22014 cp_parser_omp_flush (parser, pragma_tok);
22017 error ("%H%<#pragma omp flush%> may only be "
22018 "used in compound statements", &pragma_tok->location);
22025 case PRAGMA_OMP_TASKWAIT:
22028 case pragma_compound:
22029 cp_parser_omp_taskwait (parser, pragma_tok);
22032 error ("%H%<#pragma omp taskwait%> may only be "
22033 "used in compound statements",
22034 &pragma_tok->location);
22041 case PRAGMA_OMP_THREADPRIVATE:
22042 cp_parser_omp_threadprivate (parser, pragma_tok);
22045 case PRAGMA_OMP_ATOMIC:
22046 case PRAGMA_OMP_CRITICAL:
22047 case PRAGMA_OMP_FOR:
22048 case PRAGMA_OMP_MASTER:
22049 case PRAGMA_OMP_ORDERED:
22050 case PRAGMA_OMP_PARALLEL:
22051 case PRAGMA_OMP_SECTIONS:
22052 case PRAGMA_OMP_SINGLE:
22053 case PRAGMA_OMP_TASK:
22054 if (context == pragma_external)
22056 cp_parser_omp_construct (parser, pragma_tok);
22059 case PRAGMA_OMP_SECTION:
22060 error ("%H%<#pragma omp section%> may only be used in "
22061 "%<#pragma omp sections%> construct", &pragma_tok->location);
22065 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22066 c_invoke_pragma_handler (id);
22070 cp_parser_error (parser, "expected declaration specifiers");
22074 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22078 /* The interface the pragma parsers have to the lexer. */
22081 pragma_lex (tree *value)
22084 enum cpp_ttype ret;
22086 tok = cp_lexer_peek_token (the_parser->lexer);
22089 *value = tok->u.value;
22091 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22093 else if (ret == CPP_STRING)
22094 *value = cp_parser_string_literal (the_parser, false, false);
22097 cp_lexer_consume_token (the_parser->lexer);
22098 if (ret == CPP_KEYWORD)
22106 /* External interface. */
22108 /* Parse one entire translation unit. */
22111 c_parse_file (void)
22113 bool error_occurred;
22114 static bool already_called = false;
22116 if (already_called)
22118 sorry ("inter-module optimizations not implemented for C++");
22121 already_called = true;
22123 the_parser = cp_parser_new ();
22124 push_deferring_access_checks (flag_access_control
22125 ? dk_no_deferred : dk_no_check);
22126 error_occurred = cp_parser_translation_unit (the_parser);
22130 #include "gt-cp-parser.h"