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, cp_declarator *);
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
2626 if (nesting_depth < 0)
2632 case CPP_OPEN_BRACE:
2641 /* Consume the token. */
2642 cp_lexer_consume_token (parser->lexer);
2646 /* Skip tokens until a non-nested closing curly brace is the next
2647 token, or there are no more tokens. Return true in the first case,
2651 cp_parser_skip_to_closing_brace (cp_parser *parser)
2653 unsigned nesting_depth = 0;
2657 cp_token *token = cp_lexer_peek_token (parser->lexer);
2659 switch (token->type)
2662 case CPP_PRAGMA_EOL:
2663 /* If we've run out of tokens, stop. */
2666 case CPP_CLOSE_BRACE:
2667 /* If the next token is a non-nested `}', then we have reached
2668 the end of the current block. */
2669 if (nesting_depth-- == 0)
2673 case CPP_OPEN_BRACE:
2674 /* If it the next token is a `{', then we are entering a new
2675 block. Consume the entire block. */
2683 /* Consume the token. */
2684 cp_lexer_consume_token (parser->lexer);
2688 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2689 parameter is the PRAGMA token, allowing us to purge the entire pragma
2693 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2697 parser->lexer->in_pragma = false;
2700 token = cp_lexer_consume_token (parser->lexer);
2701 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2703 /* Ensure that the pragma is not parsed again. */
2704 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2707 /* Require pragma end of line, resyncing with it as necessary. The
2708 arguments are as for cp_parser_skip_to_pragma_eol. */
2711 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2713 parser->lexer->in_pragma = false;
2714 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2715 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2718 /* This is a simple wrapper around make_typename_type. When the id is
2719 an unresolved identifier node, we can provide a superior diagnostic
2720 using cp_parser_diagnose_invalid_type_name. */
2723 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2724 tree id, location_t id_location)
2727 if (TREE_CODE (id) == IDENTIFIER_NODE)
2729 result = make_typename_type (scope, id, typename_type,
2730 /*complain=*/tf_none);
2731 if (result == error_mark_node)
2732 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2735 return make_typename_type (scope, id, typename_type, tf_error);
2738 /* This is a wrapper around the
2739 make_{pointer,ptrmem,reference}_declarator functions that decides
2740 which one to call based on the CODE and CLASS_TYPE arguments. The
2741 CODE argument should be one of the values returned by
2742 cp_parser_ptr_operator. */
2743 static cp_declarator *
2744 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2745 cp_cv_quals cv_qualifiers,
2746 cp_declarator *target)
2748 if (code == ERROR_MARK)
2749 return cp_error_declarator;
2751 if (code == INDIRECT_REF)
2752 if (class_type == NULL_TREE)
2753 return make_pointer_declarator (cv_qualifiers, target);
2755 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2756 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2757 return make_reference_declarator (cv_qualifiers, target, false);
2758 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2759 return make_reference_declarator (cv_qualifiers, target, true);
2763 /* Create a new C++ parser. */
2766 cp_parser_new (void)
2772 /* cp_lexer_new_main is called before calling ggc_alloc because
2773 cp_lexer_new_main might load a PCH file. */
2774 lexer = cp_lexer_new_main ();
2776 /* Initialize the binops_by_token so that we can get the tree
2777 directly from the token. */
2778 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2779 binops_by_token[binops[i].token_type] = binops[i];
2781 parser = GGC_CNEW (cp_parser);
2782 parser->lexer = lexer;
2783 parser->context = cp_parser_context_new (NULL);
2785 /* For now, we always accept GNU extensions. */
2786 parser->allow_gnu_extensions_p = 1;
2788 /* The `>' token is a greater-than operator, not the end of a
2790 parser->greater_than_is_operator_p = true;
2792 parser->default_arg_ok_p = true;
2794 /* We are not parsing a constant-expression. */
2795 parser->integral_constant_expression_p = false;
2796 parser->allow_non_integral_constant_expression_p = false;
2797 parser->non_integral_constant_expression_p = false;
2799 /* Local variable names are not forbidden. */
2800 parser->local_variables_forbidden_p = false;
2802 /* We are not processing an `extern "C"' declaration. */
2803 parser->in_unbraced_linkage_specification_p = false;
2805 /* We are not processing a declarator. */
2806 parser->in_declarator_p = false;
2808 /* We are not processing a template-argument-list. */
2809 parser->in_template_argument_list_p = false;
2811 /* We are not in an iteration statement. */
2812 parser->in_statement = 0;
2814 /* We are not in a switch statement. */
2815 parser->in_switch_statement_p = false;
2817 /* We are not parsing a type-id inside an expression. */
2818 parser->in_type_id_in_expr_p = false;
2820 /* Declarations aren't implicitly extern "C". */
2821 parser->implicit_extern_c = false;
2823 /* String literals should be translated to the execution character set. */
2824 parser->translate_strings_p = true;
2826 /* We are not parsing a function body. */
2827 parser->in_function_body = false;
2829 /* The unparsed function queue is empty. */
2830 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2832 /* There are no classes being defined. */
2833 parser->num_classes_being_defined = 0;
2835 /* No template parameters apply. */
2836 parser->num_template_parameter_lists = 0;
2841 /* Create a cp_lexer structure which will emit the tokens in CACHE
2842 and push it onto the parser's lexer stack. This is used for delayed
2843 parsing of in-class method bodies and default arguments, and should
2844 not be confused with tentative parsing. */
2846 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2848 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2849 lexer->next = parser->lexer;
2850 parser->lexer = lexer;
2852 /* Move the current source position to that of the first token in the
2854 cp_lexer_set_source_position_from_token (lexer->next_token);
2857 /* Pop the top lexer off the parser stack. This is never used for the
2858 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2860 cp_parser_pop_lexer (cp_parser *parser)
2862 cp_lexer *lexer = parser->lexer;
2863 parser->lexer = lexer->next;
2864 cp_lexer_destroy (lexer);
2866 /* Put the current source position back where it was before this
2867 lexer was pushed. */
2868 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2871 /* Lexical conventions [gram.lex] */
2873 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2877 cp_parser_identifier (cp_parser* parser)
2881 /* Look for the identifier. */
2882 token = cp_parser_require (parser, CPP_NAME, "identifier");
2883 /* Return the value. */
2884 return token ? token->u.value : error_mark_node;
2887 /* Parse a sequence of adjacent string constants. Returns a
2888 TREE_STRING representing the combined, nul-terminated string
2889 constant. If TRANSLATE is true, translate the string to the
2890 execution character set. If WIDE_OK is true, a wide string is
2893 C++98 [lex.string] says that if a narrow string literal token is
2894 adjacent to a wide string literal token, the behavior is undefined.
2895 However, C99 6.4.5p4 says that this results in a wide string literal.
2896 We follow C99 here, for consistency with the C front end.
2898 This code is largely lifted from lex_string() in c-lex.c.
2900 FUTURE: ObjC++ will need to handle @-strings here. */
2902 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2906 struct obstack str_ob;
2907 cpp_string str, istr, *strs;
2909 enum cpp_ttype type;
2911 tok = cp_lexer_peek_token (parser->lexer);
2912 if (!cp_parser_is_string_literal (tok))
2914 cp_parser_error (parser, "expected string-literal");
2915 return error_mark_node;
2920 /* Try to avoid the overhead of creating and destroying an obstack
2921 for the common case of just one string. */
2922 if (!cp_parser_is_string_literal
2923 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2925 cp_lexer_consume_token (parser->lexer);
2927 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2928 str.len = TREE_STRING_LENGTH (tok->u.value);
2935 gcc_obstack_init (&str_ob);
2940 cp_lexer_consume_token (parser->lexer);
2942 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2943 str.len = TREE_STRING_LENGTH (tok->u.value);
2945 if (type != tok->type)
2947 if (type == CPP_STRING)
2949 else if (tok->type != CPP_STRING)
2950 error ("%Hunsupported non-standard concatenation "
2951 "of string literals", &tok->location);
2954 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2956 tok = cp_lexer_peek_token (parser->lexer);
2958 while (cp_parser_is_string_literal (tok));
2960 strs = (cpp_string *) obstack_finish (&str_ob);
2963 if (type != CPP_STRING && !wide_ok)
2965 cp_parser_error (parser, "a wide string is invalid in this context");
2969 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2970 (parse_in, strs, count, &istr, type))
2972 value = build_string (istr.len, (const char *)istr.text);
2973 free (CONST_CAST (unsigned char *, istr.text));
2979 TREE_TYPE (value) = char_array_type_node;
2982 TREE_TYPE (value) = char16_array_type_node;
2985 TREE_TYPE (value) = char32_array_type_node;
2988 TREE_TYPE (value) = wchar_array_type_node;
2992 value = fix_string_type (value);
2995 /* cpp_interpret_string has issued an error. */
2996 value = error_mark_node;
2999 obstack_free (&str_ob, 0);
3005 /* Basic concepts [gram.basic] */
3007 /* Parse a translation-unit.
3010 declaration-seq [opt]
3012 Returns TRUE if all went well. */
3015 cp_parser_translation_unit (cp_parser* parser)
3017 /* The address of the first non-permanent object on the declarator
3019 static void *declarator_obstack_base;
3023 /* Create the declarator obstack, if necessary. */
3024 if (!cp_error_declarator)
3026 gcc_obstack_init (&declarator_obstack);
3027 /* Create the error declarator. */
3028 cp_error_declarator = make_declarator (cdk_error);
3029 /* Create the empty parameter list. */
3030 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3031 /* Remember where the base of the declarator obstack lies. */
3032 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3035 cp_parser_declaration_seq_opt (parser);
3037 /* If there are no tokens left then all went well. */
3038 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3040 /* Get rid of the token array; we don't need it any more. */
3041 cp_lexer_destroy (parser->lexer);
3042 parser->lexer = NULL;
3044 /* This file might have been a context that's implicitly extern
3045 "C". If so, pop the lang context. (Only relevant for PCH.) */
3046 if (parser->implicit_extern_c)
3048 pop_lang_context ();
3049 parser->implicit_extern_c = false;
3053 finish_translation_unit ();
3059 cp_parser_error (parser, "expected declaration");
3063 /* Make sure the declarator obstack was fully cleaned up. */
3064 gcc_assert (obstack_next_free (&declarator_obstack)
3065 == declarator_obstack_base);
3067 /* All went well. */
3071 /* Expressions [gram.expr] */
3073 /* Parse a primary-expression.
3084 ( compound-statement )
3085 __builtin_va_arg ( assignment-expression , type-id )
3086 __builtin_offsetof ( type-id , offsetof-expression )
3089 __has_nothrow_assign ( type-id )
3090 __has_nothrow_constructor ( type-id )
3091 __has_nothrow_copy ( type-id )
3092 __has_trivial_assign ( type-id )
3093 __has_trivial_constructor ( type-id )
3094 __has_trivial_copy ( type-id )
3095 __has_trivial_destructor ( type-id )
3096 __has_virtual_destructor ( type-id )
3097 __is_abstract ( type-id )
3098 __is_base_of ( type-id , type-id )
3099 __is_class ( type-id )
3100 __is_convertible_to ( type-id , type-id )
3101 __is_empty ( type-id )
3102 __is_enum ( type-id )
3103 __is_pod ( type-id )
3104 __is_polymorphic ( type-id )
3105 __is_union ( type-id )
3107 Objective-C++ Extension:
3115 ADDRESS_P is true iff this expression was immediately preceded by
3116 "&" and therefore might denote a pointer-to-member. CAST_P is true
3117 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3118 true iff this expression is a template argument.
3120 Returns a representation of the expression. Upon return, *IDK
3121 indicates what kind of id-expression (if any) was present. */
3124 cp_parser_primary_expression (cp_parser *parser,
3127 bool template_arg_p,
3130 cp_token *token = NULL;
3132 /* Assume the primary expression is not an id-expression. */
3133 *idk = CP_ID_KIND_NONE;
3135 /* Peek at the next token. */
3136 token = cp_lexer_peek_token (parser->lexer);
3137 switch (token->type)
3150 token = cp_lexer_consume_token (parser->lexer);
3151 if (TREE_CODE (token->u.value) == FIXED_CST)
3153 error ("%Hfixed-point types not supported in C++",
3155 return error_mark_node;
3157 /* Floating-point literals are only allowed in an integral
3158 constant expression if they are cast to an integral or
3159 enumeration type. */
3160 if (TREE_CODE (token->u.value) == REAL_CST
3161 && parser->integral_constant_expression_p
3164 /* CAST_P will be set even in invalid code like "int(2.7 +
3165 ...)". Therefore, we have to check that the next token
3166 is sure to end the cast. */
3169 cp_token *next_token;
3171 next_token = cp_lexer_peek_token (parser->lexer);
3172 if (/* The comma at the end of an
3173 enumerator-definition. */
3174 next_token->type != CPP_COMMA
3175 /* The curly brace at the end of an enum-specifier. */
3176 && next_token->type != CPP_CLOSE_BRACE
3177 /* The end of a statement. */
3178 && next_token->type != CPP_SEMICOLON
3179 /* The end of the cast-expression. */
3180 && next_token->type != CPP_CLOSE_PAREN
3181 /* The end of an array bound. */
3182 && next_token->type != CPP_CLOSE_SQUARE
3183 /* The closing ">" in a template-argument-list. */
3184 && (next_token->type != CPP_GREATER
3185 || parser->greater_than_is_operator_p)
3186 /* C++0x only: A ">>" treated like two ">" tokens,
3187 in a template-argument-list. */
3188 && (next_token->type != CPP_RSHIFT
3189 || (cxx_dialect == cxx98)
3190 || parser->greater_than_is_operator_p))
3194 /* If we are within a cast, then the constraint that the
3195 cast is to an integral or enumeration type will be
3196 checked at that point. If we are not within a cast, then
3197 this code is invalid. */
3199 cp_parser_non_integral_constant_expression
3200 (parser, "floating-point literal");
3202 return token->u.value;
3208 /* ??? Should wide strings be allowed when parser->translate_strings_p
3209 is false (i.e. in attributes)? If not, we can kill the third
3210 argument to cp_parser_string_literal. */
3211 return cp_parser_string_literal (parser,
3212 parser->translate_strings_p,
3215 case CPP_OPEN_PAREN:
3218 bool saved_greater_than_is_operator_p;
3220 /* Consume the `('. */
3221 cp_lexer_consume_token (parser->lexer);
3222 /* Within a parenthesized expression, a `>' token is always
3223 the greater-than operator. */
3224 saved_greater_than_is_operator_p
3225 = parser->greater_than_is_operator_p;
3226 parser->greater_than_is_operator_p = true;
3227 /* If we see `( { ' then we are looking at the beginning of
3228 a GNU statement-expression. */
3229 if (cp_parser_allow_gnu_extensions_p (parser)
3230 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3232 /* Statement-expressions are not allowed by the standard. */
3233 pedwarn (token->location, OPT_pedantic,
3234 "ISO C++ forbids braced-groups within expressions");
3236 /* And they're not allowed outside of a function-body; you
3237 cannot, for example, write:
3239 int i = ({ int j = 3; j + 1; });
3241 at class or namespace scope. */
3242 if (!parser->in_function_body
3243 || parser->in_template_argument_list_p)
3245 error ("%Hstatement-expressions are not allowed outside "
3246 "functions nor in template-argument lists",
3248 cp_parser_skip_to_end_of_block_or_statement (parser);
3249 expr = error_mark_node;
3253 /* Start the statement-expression. */
3254 expr = begin_stmt_expr ();
3255 /* Parse the compound-statement. */
3256 cp_parser_compound_statement (parser, expr, false);
3258 expr = finish_stmt_expr (expr, false);
3263 /* Parse the parenthesized expression. */
3264 expr = cp_parser_expression (parser, cast_p, idk);
3265 /* Let the front end know that this expression was
3266 enclosed in parentheses. This matters in case, for
3267 example, the expression is of the form `A::B', since
3268 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3270 finish_parenthesized_expr (expr);
3272 /* The `>' token might be the end of a template-id or
3273 template-parameter-list now. */
3274 parser->greater_than_is_operator_p
3275 = saved_greater_than_is_operator_p;
3276 /* Consume the `)'. */
3277 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3278 cp_parser_skip_to_end_of_statement (parser);
3284 switch (token->keyword)
3286 /* These two are the boolean literals. */
3288 cp_lexer_consume_token (parser->lexer);
3289 return boolean_true_node;
3291 cp_lexer_consume_token (parser->lexer);
3292 return boolean_false_node;
3294 /* The `__null' literal. */
3296 cp_lexer_consume_token (parser->lexer);
3299 /* Recognize the `this' keyword. */
3301 cp_lexer_consume_token (parser->lexer);
3302 if (parser->local_variables_forbidden_p)
3304 error ("%H%<this%> may not be used in this context",
3306 return error_mark_node;
3308 /* Pointers cannot appear in constant-expressions. */
3309 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3310 return error_mark_node;
3311 return finish_this_expr ();
3313 /* The `operator' keyword can be the beginning of an
3318 case RID_FUNCTION_NAME:
3319 case RID_PRETTY_FUNCTION_NAME:
3320 case RID_C99_FUNCTION_NAME:
3324 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3325 __func__ are the names of variables -- but they are
3326 treated specially. Therefore, they are handled here,
3327 rather than relying on the generic id-expression logic
3328 below. Grammatically, these names are id-expressions.
3330 Consume the token. */
3331 token = cp_lexer_consume_token (parser->lexer);
3333 switch (token->keyword)
3335 case RID_FUNCTION_NAME:
3336 name = "%<__FUNCTION__%>";
3338 case RID_PRETTY_FUNCTION_NAME:
3339 name = "%<__PRETTY_FUNCTION__%>";
3341 case RID_C99_FUNCTION_NAME:
3342 name = "%<__func__%>";
3348 if (cp_parser_non_integral_constant_expression (parser, name))
3349 return error_mark_node;
3351 /* Look up the name. */
3352 return finish_fname (token->u.value);
3360 /* The `__builtin_va_arg' construct is used to handle
3361 `va_arg'. Consume the `__builtin_va_arg' token. */
3362 cp_lexer_consume_token (parser->lexer);
3363 /* Look for the opening `('. */
3364 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3365 /* Now, parse the assignment-expression. */
3366 expression = cp_parser_assignment_expression (parser,
3367 /*cast_p=*/false, NULL);
3368 /* Look for the `,'. */
3369 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3370 /* Parse the type-id. */
3371 type = cp_parser_type_id (parser);
3372 /* Look for the closing `)'. */
3373 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3374 /* Using `va_arg' in a constant-expression is not
3376 if (cp_parser_non_integral_constant_expression (parser,
3378 return error_mark_node;
3379 return build_x_va_arg (expression, type);
3383 return cp_parser_builtin_offsetof (parser);
3385 case RID_HAS_NOTHROW_ASSIGN:
3386 case RID_HAS_NOTHROW_CONSTRUCTOR:
3387 case RID_HAS_NOTHROW_COPY:
3388 case RID_HAS_TRIVIAL_ASSIGN:
3389 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3390 case RID_HAS_TRIVIAL_COPY:
3391 case RID_HAS_TRIVIAL_DESTRUCTOR:
3392 case RID_HAS_VIRTUAL_DESTRUCTOR:
3393 case RID_IS_ABSTRACT:
3394 case RID_IS_BASE_OF:
3396 case RID_IS_CONVERTIBLE_TO:
3400 case RID_IS_POLYMORPHIC:
3402 return cp_parser_trait_expr (parser, token->keyword);
3404 /* Objective-C++ expressions. */
3406 case RID_AT_PROTOCOL:
3407 case RID_AT_SELECTOR:
3408 return cp_parser_objc_expression (parser);
3411 cp_parser_error (parser, "expected primary-expression");
3412 return error_mark_node;
3415 /* An id-expression can start with either an identifier, a
3416 `::' as the beginning of a qualified-id, or the "operator"
3420 case CPP_TEMPLATE_ID:
3421 case CPP_NESTED_NAME_SPECIFIER:
3425 const char *error_msg;
3428 cp_token *id_expr_token;
3431 /* Parse the id-expression. */
3433 = cp_parser_id_expression (parser,
3434 /*template_keyword_p=*/false,
3435 /*check_dependency_p=*/true,
3437 /*declarator_p=*/false,
3438 /*optional_p=*/false);
3439 if (id_expression == error_mark_node)
3440 return error_mark_node;
3441 id_expr_token = token;
3442 token = cp_lexer_peek_token (parser->lexer);
3443 done = (token->type != CPP_OPEN_SQUARE
3444 && token->type != CPP_OPEN_PAREN
3445 && token->type != CPP_DOT
3446 && token->type != CPP_DEREF
3447 && token->type != CPP_PLUS_PLUS
3448 && token->type != CPP_MINUS_MINUS);
3449 /* If we have a template-id, then no further lookup is
3450 required. If the template-id was for a template-class, we
3451 will sometimes have a TYPE_DECL at this point. */
3452 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3453 || TREE_CODE (id_expression) == TYPE_DECL)
3454 decl = id_expression;
3455 /* Look up the name. */
3458 tree ambiguous_decls;
3460 decl = cp_parser_lookup_name (parser, id_expression,
3463 /*is_namespace=*/false,
3464 /*check_dependency=*/true,
3466 id_expr_token->location);
3467 /* If the lookup was ambiguous, an error will already have
3469 if (ambiguous_decls)
3470 return error_mark_node;
3472 /* In Objective-C++, an instance variable (ivar) may be preferred
3473 to whatever cp_parser_lookup_name() found. */
3474 decl = objc_lookup_ivar (decl, id_expression);
3476 /* If name lookup gives us a SCOPE_REF, then the
3477 qualifying scope was dependent. */
3478 if (TREE_CODE (decl) == SCOPE_REF)
3480 /* At this point, we do not know if DECL is a valid
3481 integral constant expression. We assume that it is
3482 in fact such an expression, so that code like:
3484 template <int N> struct A {
3488 is accepted. At template-instantiation time, we
3489 will check that B<N>::i is actually a constant. */
3492 /* Check to see if DECL is a local variable in a context
3493 where that is forbidden. */
3494 if (parser->local_variables_forbidden_p
3495 && local_variable_p (decl))
3497 /* It might be that we only found DECL because we are
3498 trying to be generous with pre-ISO scoping rules.
3499 For example, consider:
3503 for (int i = 0; i < 10; ++i) {}
3504 extern void f(int j = i);
3507 Here, name look up will originally find the out
3508 of scope `i'. We need to issue a warning message,
3509 but then use the global `i'. */
3510 decl = check_for_out_of_scope_variable (decl);
3511 if (local_variable_p (decl))
3513 error ("%Hlocal variable %qD may not appear in this context",
3514 &id_expr_token->location, decl);
3515 return error_mark_node;
3520 decl = (finish_id_expression
3521 (id_expression, decl, parser->scope,
3523 parser->integral_constant_expression_p,
3524 parser->allow_non_integral_constant_expression_p,
3525 &parser->non_integral_constant_expression_p,
3526 template_p, done, address_p,
3529 id_expr_token->location));
3531 cp_parser_error (parser, error_msg);
3535 /* Anything else is an error. */
3537 /* ...unless we have an Objective-C++ message or string literal,
3539 if (c_dialect_objc ()
3540 && (token->type == CPP_OPEN_SQUARE
3541 || token->type == CPP_OBJC_STRING))
3542 return cp_parser_objc_expression (parser);
3544 cp_parser_error (parser, "expected primary-expression");
3545 return error_mark_node;
3549 /* Parse an id-expression.
3556 :: [opt] nested-name-specifier template [opt] unqualified-id
3558 :: operator-function-id
3561 Return a representation of the unqualified portion of the
3562 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3563 a `::' or nested-name-specifier.
3565 Often, if the id-expression was a qualified-id, the caller will
3566 want to make a SCOPE_REF to represent the qualified-id. This
3567 function does not do this in order to avoid wastefully creating
3568 SCOPE_REFs when they are not required.
3570 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3573 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3574 uninstantiated templates.
3576 If *TEMPLATE_P is non-NULL, it is set to true iff the
3577 `template' keyword is used to explicitly indicate that the entity
3578 named is a template.
3580 If DECLARATOR_P is true, the id-expression is appearing as part of
3581 a declarator, rather than as part of an expression. */
3584 cp_parser_id_expression (cp_parser *parser,
3585 bool template_keyword_p,
3586 bool check_dependency_p,
3591 bool global_scope_p;
3592 bool nested_name_specifier_p;
3594 /* Assume the `template' keyword was not used. */
3596 *template_p = template_keyword_p;
3598 /* Look for the optional `::' operator. */
3600 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3602 /* Look for the optional nested-name-specifier. */
3603 nested_name_specifier_p
3604 = (cp_parser_nested_name_specifier_opt (parser,
3605 /*typename_keyword_p=*/false,
3610 /* If there is a nested-name-specifier, then we are looking at
3611 the first qualified-id production. */
3612 if (nested_name_specifier_p)
3615 tree saved_object_scope;
3616 tree saved_qualifying_scope;
3617 tree unqualified_id;
3620 /* See if the next token is the `template' keyword. */
3622 template_p = &is_template;
3623 *template_p = cp_parser_optional_template_keyword (parser);
3624 /* Name lookup we do during the processing of the
3625 unqualified-id might obliterate SCOPE. */
3626 saved_scope = parser->scope;
3627 saved_object_scope = parser->object_scope;
3628 saved_qualifying_scope = parser->qualifying_scope;
3629 /* Process the final unqualified-id. */
3630 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3633 /*optional_p=*/false);
3634 /* Restore the SAVED_SCOPE for our caller. */
3635 parser->scope = saved_scope;
3636 parser->object_scope = saved_object_scope;
3637 parser->qualifying_scope = saved_qualifying_scope;
3639 return unqualified_id;
3641 /* Otherwise, if we are in global scope, then we are looking at one
3642 of the other qualified-id productions. */
3643 else if (global_scope_p)
3648 /* Peek at the next token. */
3649 token = cp_lexer_peek_token (parser->lexer);
3651 /* If it's an identifier, and the next token is not a "<", then
3652 we can avoid the template-id case. This is an optimization
3653 for this common case. */
3654 if (token->type == CPP_NAME
3655 && !cp_parser_nth_token_starts_template_argument_list_p
3657 return cp_parser_identifier (parser);
3659 cp_parser_parse_tentatively (parser);
3660 /* Try a template-id. */
3661 id = cp_parser_template_id (parser,
3662 /*template_keyword_p=*/false,
3663 /*check_dependency_p=*/true,
3665 /* If that worked, we're done. */
3666 if (cp_parser_parse_definitely (parser))
3669 /* Peek at the next token. (Changes in the token buffer may
3670 have invalidated the pointer obtained above.) */
3671 token = cp_lexer_peek_token (parser->lexer);
3673 switch (token->type)
3676 return cp_parser_identifier (parser);
3679 if (token->keyword == RID_OPERATOR)
3680 return cp_parser_operator_function_id (parser);
3684 cp_parser_error (parser, "expected id-expression");
3685 return error_mark_node;
3689 return cp_parser_unqualified_id (parser, template_keyword_p,
3690 /*check_dependency_p=*/true,
3695 /* Parse an unqualified-id.
3699 operator-function-id
3700 conversion-function-id
3704 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3705 keyword, in a construct like `A::template ...'.
3707 Returns a representation of unqualified-id. For the `identifier'
3708 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3709 production a BIT_NOT_EXPR is returned; the operand of the
3710 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3711 other productions, see the documentation accompanying the
3712 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3713 names are looked up in uninstantiated templates. If DECLARATOR_P
3714 is true, the unqualified-id is appearing as part of a declarator,
3715 rather than as part of an expression. */
3718 cp_parser_unqualified_id (cp_parser* parser,
3719 bool template_keyword_p,
3720 bool check_dependency_p,
3726 /* Peek at the next token. */
3727 token = cp_lexer_peek_token (parser->lexer);
3729 switch (token->type)
3735 /* We don't know yet whether or not this will be a
3737 cp_parser_parse_tentatively (parser);
3738 /* Try a template-id. */
3739 id = cp_parser_template_id (parser, template_keyword_p,
3742 /* If it worked, we're done. */
3743 if (cp_parser_parse_definitely (parser))
3745 /* Otherwise, it's an ordinary identifier. */
3746 return cp_parser_identifier (parser);
3749 case CPP_TEMPLATE_ID:
3750 return cp_parser_template_id (parser, template_keyword_p,
3757 tree qualifying_scope;
3762 /* Consume the `~' token. */
3763 cp_lexer_consume_token (parser->lexer);
3764 /* Parse the class-name. The standard, as written, seems to
3767 template <typename T> struct S { ~S (); };
3768 template <typename T> S<T>::~S() {}
3770 is invalid, since `~' must be followed by a class-name, but
3771 `S<T>' is dependent, and so not known to be a class.
3772 That's not right; we need to look in uninstantiated
3773 templates. A further complication arises from:
3775 template <typename T> void f(T t) {
3779 Here, it is not possible to look up `T' in the scope of `T'
3780 itself. We must look in both the current scope, and the
3781 scope of the containing complete expression.
3783 Yet another issue is:
3792 The standard does not seem to say that the `S' in `~S'
3793 should refer to the type `S' and not the data member
3796 /* DR 244 says that we look up the name after the "~" in the
3797 same scope as we looked up the qualifying name. That idea
3798 isn't fully worked out; it's more complicated than that. */
3799 scope = parser->scope;
3800 object_scope = parser->object_scope;
3801 qualifying_scope = parser->qualifying_scope;
3803 /* Check for invalid scopes. */
3804 if (scope == error_mark_node)
3806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3807 cp_lexer_consume_token (parser->lexer);
3808 return error_mark_node;
3810 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3812 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3813 error ("%Hscope %qT before %<~%> is not a class-name",
3814 &token->location, scope);
3815 cp_parser_simulate_error (parser);
3816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3817 cp_lexer_consume_token (parser->lexer);
3818 return error_mark_node;
3820 gcc_assert (!scope || TYPE_P (scope));
3822 /* If the name is of the form "X::~X" it's OK. */
3823 token = cp_lexer_peek_token (parser->lexer);
3825 && token->type == CPP_NAME
3826 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3828 && constructor_name_p (token->u.value, scope))
3830 cp_lexer_consume_token (parser->lexer);
3831 return build_nt (BIT_NOT_EXPR, scope);
3834 /* If there was an explicit qualification (S::~T), first look
3835 in the scope given by the qualification (i.e., S). */
3837 type_decl = NULL_TREE;
3840 cp_parser_parse_tentatively (parser);
3841 type_decl = cp_parser_class_name (parser,
3842 /*typename_keyword_p=*/false,
3843 /*template_keyword_p=*/false,
3845 /*check_dependency=*/false,
3846 /*class_head_p=*/false,
3848 if (cp_parser_parse_definitely (parser))
3851 /* In "N::S::~S", look in "N" as well. */
3852 if (!done && scope && qualifying_scope)
3854 cp_parser_parse_tentatively (parser);
3855 parser->scope = qualifying_scope;
3856 parser->object_scope = NULL_TREE;
3857 parser->qualifying_scope = NULL_TREE;
3859 = cp_parser_class_name (parser,
3860 /*typename_keyword_p=*/false,
3861 /*template_keyword_p=*/false,
3863 /*check_dependency=*/false,
3864 /*class_head_p=*/false,
3866 if (cp_parser_parse_definitely (parser))
3869 /* In "p->S::~T", look in the scope given by "*p" as well. */
3870 else if (!done && object_scope)
3872 cp_parser_parse_tentatively (parser);
3873 parser->scope = object_scope;
3874 parser->object_scope = NULL_TREE;
3875 parser->qualifying_scope = NULL_TREE;
3877 = cp_parser_class_name (parser,
3878 /*typename_keyword_p=*/false,
3879 /*template_keyword_p=*/false,
3881 /*check_dependency=*/false,
3882 /*class_head_p=*/false,
3884 if (cp_parser_parse_definitely (parser))
3887 /* Look in the surrounding context. */
3890 parser->scope = NULL_TREE;
3891 parser->object_scope = NULL_TREE;
3892 parser->qualifying_scope = NULL_TREE;
3893 if (processing_template_decl)
3894 cp_parser_parse_tentatively (parser);
3896 = cp_parser_class_name (parser,
3897 /*typename_keyword_p=*/false,
3898 /*template_keyword_p=*/false,
3900 /*check_dependency=*/false,
3901 /*class_head_p=*/false,
3903 if (processing_template_decl
3904 && ! cp_parser_parse_definitely (parser))
3906 /* We couldn't find a type with this name, so just accept
3907 it and check for a match at instantiation time. */
3908 type_decl = cp_parser_identifier (parser);
3909 if (type_decl != error_mark_node)
3910 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3914 /* If an error occurred, assume that the name of the
3915 destructor is the same as the name of the qualifying
3916 class. That allows us to keep parsing after running
3917 into ill-formed destructor names. */
3918 if (type_decl == error_mark_node && scope)
3919 return build_nt (BIT_NOT_EXPR, scope);
3920 else if (type_decl == error_mark_node)
3921 return error_mark_node;
3923 /* Check that destructor name and scope match. */
3924 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3926 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3927 error ("%Hdeclaration of %<~%T%> as member of %qT",
3928 &token->location, type_decl, scope);
3929 cp_parser_simulate_error (parser);
3930 return error_mark_node;
3935 A typedef-name that names a class shall not be used as the
3936 identifier in the declarator for a destructor declaration. */
3938 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3939 && !DECL_SELF_REFERENCE_P (type_decl)
3940 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3941 error ("%Htypedef-name %qD used as destructor declarator",
3942 &token->location, type_decl);
3944 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3948 if (token->keyword == RID_OPERATOR)
3952 /* This could be a template-id, so we try that first. */
3953 cp_parser_parse_tentatively (parser);
3954 /* Try a template-id. */
3955 id = cp_parser_template_id (parser, template_keyword_p,
3956 /*check_dependency_p=*/true,
3958 /* If that worked, we're done. */
3959 if (cp_parser_parse_definitely (parser))
3961 /* We still don't know whether we're looking at an
3962 operator-function-id or a conversion-function-id. */
3963 cp_parser_parse_tentatively (parser);
3964 /* Try an operator-function-id. */
3965 id = cp_parser_operator_function_id (parser);
3966 /* If that didn't work, try a conversion-function-id. */
3967 if (!cp_parser_parse_definitely (parser))
3968 id = cp_parser_conversion_function_id (parser);
3977 cp_parser_error (parser, "expected unqualified-id");
3978 return error_mark_node;
3982 /* Parse an (optional) nested-name-specifier.
3984 nested-name-specifier: [C++98]
3985 class-or-namespace-name :: nested-name-specifier [opt]
3986 class-or-namespace-name :: template nested-name-specifier [opt]
3988 nested-name-specifier: [C++0x]
3991 nested-name-specifier identifier ::
3992 nested-name-specifier template [opt] simple-template-id ::
3994 PARSER->SCOPE should be set appropriately before this function is
3995 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3996 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3999 Sets PARSER->SCOPE to the class (TYPE) or namespace
4000 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4001 it unchanged if there is no nested-name-specifier. Returns the new
4002 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4004 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4005 part of a declaration and/or decl-specifier. */
4008 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4009 bool typename_keyword_p,
4010 bool check_dependency_p,
4012 bool is_declaration)
4014 bool success = false;
4015 cp_token_position start = 0;
4018 /* Remember where the nested-name-specifier starts. */
4019 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4021 start = cp_lexer_token_position (parser->lexer, false);
4022 push_deferring_access_checks (dk_deferred);
4029 tree saved_qualifying_scope;
4030 bool template_keyword_p;
4032 /* Spot cases that cannot be the beginning of a
4033 nested-name-specifier. */
4034 token = cp_lexer_peek_token (parser->lexer);
4036 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4037 the already parsed nested-name-specifier. */
4038 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4040 /* Grab the nested-name-specifier and continue the loop. */
4041 cp_parser_pre_parsed_nested_name_specifier (parser);
4042 /* If we originally encountered this nested-name-specifier
4043 with IS_DECLARATION set to false, we will not have
4044 resolved TYPENAME_TYPEs, so we must do so here. */
4046 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4048 new_scope = resolve_typename_type (parser->scope,
4049 /*only_current_p=*/false);
4050 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4051 parser->scope = new_scope;
4057 /* Spot cases that cannot be the beginning of a
4058 nested-name-specifier. On the second and subsequent times
4059 through the loop, we look for the `template' keyword. */
4060 if (success && token->keyword == RID_TEMPLATE)
4062 /* A template-id can start a nested-name-specifier. */
4063 else if (token->type == CPP_TEMPLATE_ID)
4067 /* If the next token is not an identifier, then it is
4068 definitely not a type-name or namespace-name. */
4069 if (token->type != CPP_NAME)
4071 /* If the following token is neither a `<' (to begin a
4072 template-id), nor a `::', then we are not looking at a
4073 nested-name-specifier. */
4074 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4075 if (token->type != CPP_SCOPE
4076 && !cp_parser_nth_token_starts_template_argument_list_p
4081 /* The nested-name-specifier is optional, so we parse
4083 cp_parser_parse_tentatively (parser);
4085 /* Look for the optional `template' keyword, if this isn't the
4086 first time through the loop. */
4088 template_keyword_p = cp_parser_optional_template_keyword (parser);
4090 template_keyword_p = false;
4092 /* Save the old scope since the name lookup we are about to do
4093 might destroy it. */
4094 old_scope = parser->scope;
4095 saved_qualifying_scope = parser->qualifying_scope;
4096 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4097 look up names in "X<T>::I" in order to determine that "Y" is
4098 a template. So, if we have a typename at this point, we make
4099 an effort to look through it. */
4101 && !typename_keyword_p
4103 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4104 parser->scope = resolve_typename_type (parser->scope,
4105 /*only_current_p=*/false);
4106 /* Parse the qualifying entity. */
4108 = cp_parser_qualifying_entity (parser,
4114 /* Look for the `::' token. */
4115 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4117 /* If we found what we wanted, we keep going; otherwise, we're
4119 if (!cp_parser_parse_definitely (parser))
4121 bool error_p = false;
4123 /* Restore the OLD_SCOPE since it was valid before the
4124 failed attempt at finding the last
4125 class-or-namespace-name. */
4126 parser->scope = old_scope;
4127 parser->qualifying_scope = saved_qualifying_scope;
4128 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4130 /* If the next token is an identifier, and the one after
4131 that is a `::', then any valid interpretation would have
4132 found a class-or-namespace-name. */
4133 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4134 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4136 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4139 token = cp_lexer_consume_token (parser->lexer);
4142 if (!token->ambiguous_p)
4145 tree ambiguous_decls;
4147 decl = cp_parser_lookup_name (parser, token->u.value,
4149 /*is_template=*/false,
4150 /*is_namespace=*/false,
4151 /*check_dependency=*/true,
4154 if (TREE_CODE (decl) == TEMPLATE_DECL)
4155 error ("%H%qD used without template parameters",
4156 &token->location, decl);
4157 else if (ambiguous_decls)
4159 error ("%Hreference to %qD is ambiguous",
4160 &token->location, token->u.value);
4161 print_candidates (ambiguous_decls);
4162 decl = error_mark_node;
4166 const char* msg = "is not a class or namespace";
4167 if (cxx_dialect != cxx98)
4168 msg = "is not a class, namespace, or enumeration";
4169 cp_parser_name_lookup_error
4170 (parser, token->u.value, decl, msg,
4174 parser->scope = error_mark_node;
4176 /* Treat this as a successful nested-name-specifier
4181 If the name found is not a class-name (clause
4182 _class_) or namespace-name (_namespace.def_), the
4183 program is ill-formed. */
4186 cp_lexer_consume_token (parser->lexer);
4190 /* We've found one valid nested-name-specifier. */
4192 /* Name lookup always gives us a DECL. */
4193 if (TREE_CODE (new_scope) == TYPE_DECL)
4194 new_scope = TREE_TYPE (new_scope);
4195 /* Uses of "template" must be followed by actual templates. */
4196 if (template_keyword_p
4197 && !(CLASS_TYPE_P (new_scope)
4198 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4199 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4200 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4201 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4202 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4203 == TEMPLATE_ID_EXPR)))
4204 permerror (input_location, TYPE_P (new_scope)
4205 ? "%qT is not a template"
4206 : "%qD is not a template",
4208 /* If it is a class scope, try to complete it; we are about to
4209 be looking up names inside the class. */
4210 if (TYPE_P (new_scope)
4211 /* Since checking types for dependency can be expensive,
4212 avoid doing it if the type is already complete. */
4213 && !COMPLETE_TYPE_P (new_scope)
4214 /* Do not try to complete dependent types. */
4215 && !dependent_type_p (new_scope))
4217 new_scope = complete_type (new_scope);
4218 /* If it is a typedef to current class, use the current
4219 class instead, as the typedef won't have any names inside
4221 if (!COMPLETE_TYPE_P (new_scope)
4222 && currently_open_class (new_scope))
4223 new_scope = TYPE_MAIN_VARIANT (new_scope);
4225 /* Make sure we look in the right scope the next time through
4227 parser->scope = new_scope;
4230 /* If parsing tentatively, replace the sequence of tokens that makes
4231 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4232 token. That way, should we re-parse the token stream, we will
4233 not have to repeat the effort required to do the parse, nor will
4234 we issue duplicate error messages. */
4235 if (success && start)
4239 token = cp_lexer_token_at (parser->lexer, start);
4240 /* Reset the contents of the START token. */
4241 token->type = CPP_NESTED_NAME_SPECIFIER;
4242 /* Retrieve any deferred checks. Do not pop this access checks yet
4243 so the memory will not be reclaimed during token replacing below. */
4244 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4245 token->u.tree_check_value->value = parser->scope;
4246 token->u.tree_check_value->checks = get_deferred_access_checks ();
4247 token->u.tree_check_value->qualifying_scope =
4248 parser->qualifying_scope;
4249 token->keyword = RID_MAX;
4251 /* Purge all subsequent tokens. */
4252 cp_lexer_purge_tokens_after (parser->lexer, start);
4256 pop_to_parent_deferring_access_checks ();
4258 return success ? parser->scope : NULL_TREE;
4261 /* Parse a nested-name-specifier. See
4262 cp_parser_nested_name_specifier_opt for details. This function
4263 behaves identically, except that it will an issue an error if no
4264 nested-name-specifier is present. */
4267 cp_parser_nested_name_specifier (cp_parser *parser,
4268 bool typename_keyword_p,
4269 bool check_dependency_p,
4271 bool is_declaration)
4275 /* Look for the nested-name-specifier. */
4276 scope = cp_parser_nested_name_specifier_opt (parser,
4281 /* If it was not present, issue an error message. */
4284 cp_parser_error (parser, "expected nested-name-specifier");
4285 parser->scope = NULL_TREE;
4291 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4292 this is either a class-name or a namespace-name (which corresponds
4293 to the class-or-namespace-name production in the grammar). For
4294 C++0x, it can also be a type-name that refers to an enumeration
4297 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4298 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4299 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4300 TYPE_P is TRUE iff the next name should be taken as a class-name,
4301 even the same name is declared to be another entity in the same
4304 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4305 specified by the class-or-namespace-name. If neither is found the
4306 ERROR_MARK_NODE is returned. */
4309 cp_parser_qualifying_entity (cp_parser *parser,
4310 bool typename_keyword_p,
4311 bool template_keyword_p,
4312 bool check_dependency_p,
4314 bool is_declaration)
4317 tree saved_qualifying_scope;
4318 tree saved_object_scope;
4321 bool successful_parse_p;
4323 /* Before we try to parse the class-name, we must save away the
4324 current PARSER->SCOPE since cp_parser_class_name will destroy
4326 saved_scope = parser->scope;
4327 saved_qualifying_scope = parser->qualifying_scope;
4328 saved_object_scope = parser->object_scope;
4329 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4330 there is no need to look for a namespace-name. */
4331 only_class_p = template_keyword_p
4332 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4334 cp_parser_parse_tentatively (parser);
4335 scope = cp_parser_class_name (parser,
4338 type_p ? class_type : none_type,
4340 /*class_head_p=*/false,
4342 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4343 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4345 && cxx_dialect != cxx98
4346 && !successful_parse_p)
4348 /* Restore the saved scope. */
4349 parser->scope = saved_scope;
4350 parser->qualifying_scope = saved_qualifying_scope;
4351 parser->object_scope = saved_object_scope;
4353 /* Parse tentatively. */
4354 cp_parser_parse_tentatively (parser);
4356 /* Parse a typedef-name or enum-name. */
4357 scope = cp_parser_nonclass_name (parser);
4358 successful_parse_p = cp_parser_parse_definitely (parser);
4360 /* If that didn't work, try for a namespace-name. */
4361 if (!only_class_p && !successful_parse_p)
4363 /* Restore the saved scope. */
4364 parser->scope = saved_scope;
4365 parser->qualifying_scope = saved_qualifying_scope;
4366 parser->object_scope = saved_object_scope;
4367 /* If we are not looking at an identifier followed by the scope
4368 resolution operator, then this is not part of a
4369 nested-name-specifier. (Note that this function is only used
4370 to parse the components of a nested-name-specifier.) */
4371 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4372 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4373 return error_mark_node;
4374 scope = cp_parser_namespace_name (parser);
4380 /* Parse a postfix-expression.
4384 postfix-expression [ expression ]
4385 postfix-expression ( expression-list [opt] )
4386 simple-type-specifier ( expression-list [opt] )
4387 typename :: [opt] nested-name-specifier identifier
4388 ( expression-list [opt] )
4389 typename :: [opt] nested-name-specifier template [opt] template-id
4390 ( expression-list [opt] )
4391 postfix-expression . template [opt] id-expression
4392 postfix-expression -> template [opt] id-expression
4393 postfix-expression . pseudo-destructor-name
4394 postfix-expression -> pseudo-destructor-name
4395 postfix-expression ++
4396 postfix-expression --
4397 dynamic_cast < type-id > ( expression )
4398 static_cast < type-id > ( expression )
4399 reinterpret_cast < type-id > ( expression )
4400 const_cast < type-id > ( expression )
4401 typeid ( expression )
4407 ( type-id ) { initializer-list , [opt] }
4409 This extension is a GNU version of the C99 compound-literal
4410 construct. (The C99 grammar uses `type-name' instead of `type-id',
4411 but they are essentially the same concept.)
4413 If ADDRESS_P is true, the postfix expression is the operand of the
4414 `&' operator. CAST_P is true if this expression is the target of a
4417 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4418 class member access expressions [expr.ref].
4420 Returns a representation of the expression. */
4423 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4424 bool member_access_only_p,
4425 cp_id_kind * pidk_return)
4429 cp_id_kind idk = CP_ID_KIND_NONE;
4430 tree postfix_expression = NULL_TREE;
4431 bool is_member_access = false;
4433 /* Peek at the next token. */
4434 token = cp_lexer_peek_token (parser->lexer);
4435 /* Some of the productions are determined by keywords. */
4436 keyword = token->keyword;
4446 const char *saved_message;
4448 /* All of these can be handled in the same way from the point
4449 of view of parsing. Begin by consuming the token
4450 identifying the cast. */
4451 cp_lexer_consume_token (parser->lexer);
4453 /* New types cannot be defined in the cast. */
4454 saved_message = parser->type_definition_forbidden_message;
4455 parser->type_definition_forbidden_message
4456 = "types may not be defined in casts";
4458 /* Look for the opening `<'. */
4459 cp_parser_require (parser, CPP_LESS, "%<<%>");
4460 /* Parse the type to which we are casting. */
4461 type = cp_parser_type_id (parser);
4462 /* Look for the closing `>'. */
4463 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4464 /* Restore the old message. */
4465 parser->type_definition_forbidden_message = saved_message;
4467 /* And the expression which is being cast. */
4468 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4469 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4470 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4472 /* Only type conversions to integral or enumeration types
4473 can be used in constant-expressions. */
4474 if (!cast_valid_in_integral_constant_expression_p (type)
4475 && (cp_parser_non_integral_constant_expression
4477 "a cast to a type other than an integral or "
4478 "enumeration type")))
4479 return error_mark_node;
4485 = build_dynamic_cast (type, expression, tf_warning_or_error);
4489 = build_static_cast (type, expression, tf_warning_or_error);
4493 = build_reinterpret_cast (type, expression,
4494 tf_warning_or_error);
4498 = build_const_cast (type, expression, tf_warning_or_error);
4509 const char *saved_message;
4510 bool saved_in_type_id_in_expr_p;
4512 /* Consume the `typeid' token. */
4513 cp_lexer_consume_token (parser->lexer);
4514 /* Look for the `(' token. */
4515 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4516 /* Types cannot be defined in a `typeid' expression. */
4517 saved_message = parser->type_definition_forbidden_message;
4518 parser->type_definition_forbidden_message
4519 = "types may not be defined in a %<typeid%> expression";
4520 /* We can't be sure yet whether we're looking at a type-id or an
4522 cp_parser_parse_tentatively (parser);
4523 /* Try a type-id first. */
4524 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4525 parser->in_type_id_in_expr_p = true;
4526 type = cp_parser_type_id (parser);
4527 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4528 /* Look for the `)' token. Otherwise, we can't be sure that
4529 we're not looking at an expression: consider `typeid (int
4530 (3))', for example. */
4531 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4532 /* If all went well, simply lookup the type-id. */
4533 if (cp_parser_parse_definitely (parser))
4534 postfix_expression = get_typeid (type);
4535 /* Otherwise, fall back to the expression variant. */
4540 /* Look for an expression. */
4541 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4542 /* Compute its typeid. */
4543 postfix_expression = build_typeid (expression);
4544 /* Look for the `)' token. */
4545 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4547 /* Restore the saved message. */
4548 parser->type_definition_forbidden_message = saved_message;
4549 /* `typeid' may not appear in an integral constant expression. */
4550 if (cp_parser_non_integral_constant_expression(parser,
4551 "%<typeid%> operator"))
4552 return error_mark_node;
4559 /* The syntax permitted here is the same permitted for an
4560 elaborated-type-specifier. */
4561 type = cp_parser_elaborated_type_specifier (parser,
4562 /*is_friend=*/false,
4563 /*is_declaration=*/false);
4564 postfix_expression = cp_parser_functional_cast (parser, type);
4572 /* If the next thing is a simple-type-specifier, we may be
4573 looking at a functional cast. We could also be looking at
4574 an id-expression. So, we try the functional cast, and if
4575 that doesn't work we fall back to the primary-expression. */
4576 cp_parser_parse_tentatively (parser);
4577 /* Look for the simple-type-specifier. */
4578 type = cp_parser_simple_type_specifier (parser,
4579 /*decl_specs=*/NULL,
4580 CP_PARSER_FLAGS_NONE);
4581 /* Parse the cast itself. */
4582 if (!cp_parser_error_occurred (parser))
4584 = cp_parser_functional_cast (parser, type);
4585 /* If that worked, we're done. */
4586 if (cp_parser_parse_definitely (parser))
4589 /* If the functional-cast didn't work out, try a
4590 compound-literal. */
4591 if (cp_parser_allow_gnu_extensions_p (parser)
4592 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4594 VEC(constructor_elt,gc) *initializer_list = NULL;
4595 bool saved_in_type_id_in_expr_p;
4597 cp_parser_parse_tentatively (parser);
4598 /* Consume the `('. */
4599 cp_lexer_consume_token (parser->lexer);
4600 /* Parse the type. */
4601 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4602 parser->in_type_id_in_expr_p = true;
4603 type = cp_parser_type_id (parser);
4604 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4605 /* Look for the `)'. */
4606 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4607 /* Look for the `{'. */
4608 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4609 /* If things aren't going well, there's no need to
4611 if (!cp_parser_error_occurred (parser))
4613 bool non_constant_p;
4614 /* Parse the initializer-list. */
4616 = cp_parser_initializer_list (parser, &non_constant_p);
4617 /* Allow a trailing `,'. */
4618 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4619 cp_lexer_consume_token (parser->lexer);
4620 /* Look for the final `}'. */
4621 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4623 /* If that worked, we're definitely looking at a
4624 compound-literal expression. */
4625 if (cp_parser_parse_definitely (parser))
4627 /* Warn the user that a compound literal is not
4628 allowed in standard C++. */
4629 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4630 /* For simplicity, we disallow compound literals in
4631 constant-expressions. We could
4632 allow compound literals of integer type, whose
4633 initializer was a constant, in constant
4634 expressions. Permitting that usage, as a further
4635 extension, would not change the meaning of any
4636 currently accepted programs. (Of course, as
4637 compound literals are not part of ISO C++, the
4638 standard has nothing to say.) */
4639 if (cp_parser_non_integral_constant_expression
4640 (parser, "non-constant compound literals"))
4642 postfix_expression = error_mark_node;
4645 /* Form the representation of the compound-literal. */
4647 = (finish_compound_literal
4648 (type, build_constructor (init_list_type_node,
4649 initializer_list)));
4654 /* It must be a primary-expression. */
4656 = cp_parser_primary_expression (parser, address_p, cast_p,
4657 /*template_arg_p=*/false,
4663 /* Keep looping until the postfix-expression is complete. */
4666 if (idk == CP_ID_KIND_UNQUALIFIED
4667 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4668 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4669 /* It is not a Koenig lookup function call. */
4671 = unqualified_name_lookup_error (postfix_expression);
4673 /* Peek at the next token. */
4674 token = cp_lexer_peek_token (parser->lexer);
4676 switch (token->type)
4678 case CPP_OPEN_SQUARE:
4680 = cp_parser_postfix_open_square_expression (parser,
4683 idk = CP_ID_KIND_NONE;
4684 is_member_access = false;
4687 case CPP_OPEN_PAREN:
4688 /* postfix-expression ( expression-list [opt] ) */
4691 bool is_builtin_constant_p;
4692 bool saved_integral_constant_expression_p = false;
4693 bool saved_non_integral_constant_expression_p = false;
4696 is_member_access = false;
4698 is_builtin_constant_p
4699 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4700 if (is_builtin_constant_p)
4702 /* The whole point of __builtin_constant_p is to allow
4703 non-constant expressions to appear as arguments. */
4704 saved_integral_constant_expression_p
4705 = parser->integral_constant_expression_p;
4706 saved_non_integral_constant_expression_p
4707 = parser->non_integral_constant_expression_p;
4708 parser->integral_constant_expression_p = false;
4710 args = (cp_parser_parenthesized_expression_list
4711 (parser, /*is_attribute_list=*/false,
4712 /*cast_p=*/false, /*allow_expansion_p=*/true,
4713 /*non_constant_p=*/NULL));
4714 if (is_builtin_constant_p)
4716 parser->integral_constant_expression_p
4717 = saved_integral_constant_expression_p;
4718 parser->non_integral_constant_expression_p
4719 = saved_non_integral_constant_expression_p;
4722 if (args == error_mark_node)
4724 postfix_expression = error_mark_node;
4728 /* Function calls are not permitted in
4729 constant-expressions. */
4730 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4731 && cp_parser_non_integral_constant_expression (parser,
4734 postfix_expression = error_mark_node;
4739 if (idk == CP_ID_KIND_UNQUALIFIED
4740 || idk == CP_ID_KIND_TEMPLATE_ID)
4742 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4747 if (!any_type_dependent_arguments_p (args))
4749 = perform_koenig_lookup (postfix_expression, args);
4753 = unqualified_fn_lookup_error (postfix_expression);
4755 /* We do not perform argument-dependent lookup if
4756 normal lookup finds a non-function, in accordance
4757 with the expected resolution of DR 218. */
4758 else if (args && is_overloaded_fn (postfix_expression))
4760 tree fn = get_first_fn (postfix_expression);
4762 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4763 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4765 /* Only do argument dependent lookup if regular
4766 lookup does not find a set of member functions.
4767 [basic.lookup.koenig]/2a */
4768 if (!DECL_FUNCTION_MEMBER_P (fn))
4771 if (!any_type_dependent_arguments_p (args))
4773 = perform_koenig_lookup (postfix_expression, args);
4778 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4780 tree instance = TREE_OPERAND (postfix_expression, 0);
4781 tree fn = TREE_OPERAND (postfix_expression, 1);
4783 if (processing_template_decl
4784 && (type_dependent_expression_p (instance)
4785 || (!BASELINK_P (fn)
4786 && TREE_CODE (fn) != FIELD_DECL)
4787 || type_dependent_expression_p (fn)
4788 || any_type_dependent_arguments_p (args)))
4791 = build_nt_call_list (postfix_expression, args);
4795 if (BASELINK_P (fn))
4798 = (build_new_method_call
4799 (instance, fn, args, NULL_TREE,
4800 (idk == CP_ID_KIND_QUALIFIED
4801 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4803 tf_warning_or_error));
4807 = finish_call_expr (postfix_expression, args,
4808 /*disallow_virtual=*/false,
4810 tf_warning_or_error);
4812 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4813 || TREE_CODE (postfix_expression) == MEMBER_REF
4814 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4815 postfix_expression = (build_offset_ref_call_from_tree
4816 (postfix_expression, args));
4817 else if (idk == CP_ID_KIND_QUALIFIED)
4818 /* A call to a static class member, or a namespace-scope
4821 = finish_call_expr (postfix_expression, args,
4822 /*disallow_virtual=*/true,
4824 tf_warning_or_error);
4826 /* All other function calls. */
4828 = finish_call_expr (postfix_expression, args,
4829 /*disallow_virtual=*/false,
4831 tf_warning_or_error);
4833 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4834 idk = CP_ID_KIND_NONE;
4840 /* postfix-expression . template [opt] id-expression
4841 postfix-expression . pseudo-destructor-name
4842 postfix-expression -> template [opt] id-expression
4843 postfix-expression -> pseudo-destructor-name */
4845 /* Consume the `.' or `->' operator. */
4846 cp_lexer_consume_token (parser->lexer);
4849 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4854 is_member_access = true;
4858 /* postfix-expression ++ */
4859 /* Consume the `++' token. */
4860 cp_lexer_consume_token (parser->lexer);
4861 /* Generate a representation for the complete expression. */
4863 = finish_increment_expr (postfix_expression,
4864 POSTINCREMENT_EXPR);
4865 /* Increments may not appear in constant-expressions. */
4866 if (cp_parser_non_integral_constant_expression (parser,
4868 postfix_expression = error_mark_node;
4869 idk = CP_ID_KIND_NONE;
4870 is_member_access = false;
4873 case CPP_MINUS_MINUS:
4874 /* postfix-expression -- */
4875 /* Consume the `--' token. */
4876 cp_lexer_consume_token (parser->lexer);
4877 /* Generate a representation for the complete expression. */
4879 = finish_increment_expr (postfix_expression,
4880 POSTDECREMENT_EXPR);
4881 /* Decrements may not appear in constant-expressions. */
4882 if (cp_parser_non_integral_constant_expression (parser,
4884 postfix_expression = error_mark_node;
4885 idk = CP_ID_KIND_NONE;
4886 is_member_access = false;
4890 if (pidk_return != NULL)
4891 * pidk_return = idk;
4892 if (member_access_only_p)
4893 return is_member_access? postfix_expression : error_mark_node;
4895 return postfix_expression;
4899 /* We should never get here. */
4901 return error_mark_node;
4904 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4905 by cp_parser_builtin_offsetof. We're looking for
4907 postfix-expression [ expression ]
4909 FOR_OFFSETOF is set if we're being called in that context, which
4910 changes how we deal with integer constant expressions. */
4913 cp_parser_postfix_open_square_expression (cp_parser *parser,
4914 tree postfix_expression,
4919 /* Consume the `[' token. */
4920 cp_lexer_consume_token (parser->lexer);
4922 /* Parse the index expression. */
4923 /* ??? For offsetof, there is a question of what to allow here. If
4924 offsetof is not being used in an integral constant expression context,
4925 then we *could* get the right answer by computing the value at runtime.
4926 If we are in an integral constant expression context, then we might
4927 could accept any constant expression; hard to say without analysis.
4928 Rather than open the barn door too wide right away, allow only integer
4929 constant expressions here. */
4931 index = cp_parser_constant_expression (parser, false, NULL);
4933 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4935 /* Look for the closing `]'. */
4936 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4938 /* Build the ARRAY_REF. */
4939 postfix_expression = grok_array_decl (postfix_expression, index);
4941 /* When not doing offsetof, array references are not permitted in
4942 constant-expressions. */
4944 && (cp_parser_non_integral_constant_expression
4945 (parser, "an array reference")))
4946 postfix_expression = error_mark_node;
4948 return postfix_expression;
4951 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4952 by cp_parser_builtin_offsetof. We're looking for
4954 postfix-expression . template [opt] id-expression
4955 postfix-expression . pseudo-destructor-name
4956 postfix-expression -> template [opt] id-expression
4957 postfix-expression -> pseudo-destructor-name
4959 FOR_OFFSETOF is set if we're being called in that context. That sorta
4960 limits what of the above we'll actually accept, but nevermind.
4961 TOKEN_TYPE is the "." or "->" token, which will already have been
4962 removed from the stream. */
4965 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4966 enum cpp_ttype token_type,
4967 tree postfix_expression,
4968 bool for_offsetof, cp_id_kind *idk,
4969 location_t location)
4973 bool pseudo_destructor_p;
4974 tree scope = NULL_TREE;
4976 /* If this is a `->' operator, dereference the pointer. */
4977 if (token_type == CPP_DEREF)
4978 postfix_expression = build_x_arrow (postfix_expression);
4979 /* Check to see whether or not the expression is type-dependent. */
4980 dependent_p = type_dependent_expression_p (postfix_expression);
4981 /* The identifier following the `->' or `.' is not qualified. */
4982 parser->scope = NULL_TREE;
4983 parser->qualifying_scope = NULL_TREE;
4984 parser->object_scope = NULL_TREE;
4985 *idk = CP_ID_KIND_NONE;
4987 /* Enter the scope corresponding to the type of the object
4988 given by the POSTFIX_EXPRESSION. */
4989 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4991 scope = TREE_TYPE (postfix_expression);
4992 /* According to the standard, no expression should ever have
4993 reference type. Unfortunately, we do not currently match
4994 the standard in this respect in that our internal representation
4995 of an expression may have reference type even when the standard
4996 says it does not. Therefore, we have to manually obtain the
4997 underlying type here. */
4998 scope = non_reference (scope);
4999 /* The type of the POSTFIX_EXPRESSION must be complete. */
5000 if (scope == unknown_type_node)
5002 error ("%H%qE does not have class type", &location, postfix_expression);
5006 scope = complete_type_or_else (scope, NULL_TREE);
5007 /* Let the name lookup machinery know that we are processing a
5008 class member access expression. */
5009 parser->context->object_type = scope;
5010 /* If something went wrong, we want to be able to discern that case,
5011 as opposed to the case where there was no SCOPE due to the type
5012 of expression being dependent. */
5014 scope = error_mark_node;
5015 /* If the SCOPE was erroneous, make the various semantic analysis
5016 functions exit quickly -- and without issuing additional error
5018 if (scope == error_mark_node)
5019 postfix_expression = error_mark_node;
5022 /* Assume this expression is not a pseudo-destructor access. */
5023 pseudo_destructor_p = false;
5025 /* If the SCOPE is a scalar type, then, if this is a valid program,
5026 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5027 is type dependent, it can be pseudo-destructor-name or something else.
5028 Try to parse it as pseudo-destructor-name first. */
5029 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5034 cp_parser_parse_tentatively (parser);
5035 /* Parse the pseudo-destructor-name. */
5037 cp_parser_pseudo_destructor_name (parser, &s, &type);
5039 && (cp_parser_error_occurred (parser)
5040 || TREE_CODE (type) != TYPE_DECL
5041 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5042 cp_parser_abort_tentative_parse (parser);
5043 else if (cp_parser_parse_definitely (parser))
5045 pseudo_destructor_p = true;
5047 = finish_pseudo_destructor_expr (postfix_expression,
5048 s, TREE_TYPE (type));
5052 if (!pseudo_destructor_p)
5054 /* If the SCOPE is not a scalar type, we are looking at an
5055 ordinary class member access expression, rather than a
5056 pseudo-destructor-name. */
5058 cp_token *token = cp_lexer_peek_token (parser->lexer);
5059 /* Parse the id-expression. */
5060 name = (cp_parser_id_expression
5062 cp_parser_optional_template_keyword (parser),
5063 /*check_dependency_p=*/true,
5065 /*declarator_p=*/false,
5066 /*optional_p=*/false));
5067 /* In general, build a SCOPE_REF if the member name is qualified.
5068 However, if the name was not dependent and has already been
5069 resolved; there is no need to build the SCOPE_REF. For example;
5071 struct X { void f(); };
5072 template <typename T> void f(T* t) { t->X::f(); }
5074 Even though "t" is dependent, "X::f" is not and has been resolved
5075 to a BASELINK; there is no need to include scope information. */
5077 /* But we do need to remember that there was an explicit scope for
5078 virtual function calls. */
5080 *idk = CP_ID_KIND_QUALIFIED;
5082 /* If the name is a template-id that names a type, we will get a
5083 TYPE_DECL here. That is invalid code. */
5084 if (TREE_CODE (name) == TYPE_DECL)
5086 error ("%Hinvalid use of %qD", &token->location, name);
5087 postfix_expression = error_mark_node;
5091 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5093 name = build_qualified_name (/*type=*/NULL_TREE,
5097 parser->scope = NULL_TREE;
5098 parser->qualifying_scope = NULL_TREE;
5099 parser->object_scope = NULL_TREE;
5101 if (scope && name && BASELINK_P (name))
5102 adjust_result_of_qualified_name_lookup
5103 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5105 = finish_class_member_access_expr (postfix_expression, name,
5107 tf_warning_or_error);
5111 /* We no longer need to look up names in the scope of the object on
5112 the left-hand side of the `.' or `->' operator. */
5113 parser->context->object_type = NULL_TREE;
5115 /* Outside of offsetof, these operators may not appear in
5116 constant-expressions. */
5118 && (cp_parser_non_integral_constant_expression
5119 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5120 postfix_expression = error_mark_node;
5122 return postfix_expression;
5125 /* Parse a parenthesized expression-list.
5128 assignment-expression
5129 expression-list, assignment-expression
5134 identifier, expression-list
5136 CAST_P is true if this expression is the target of a cast.
5138 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5141 Returns a TREE_LIST. The TREE_VALUE of each node is a
5142 representation of an assignment-expression. Note that a TREE_LIST
5143 is returned even if there is only a single expression in the list.
5144 error_mark_node is returned if the ( and or ) are
5145 missing. NULL_TREE is returned on no expressions. The parentheses
5146 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5147 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5148 indicates whether or not all of the expressions in the list were
5152 cp_parser_parenthesized_expression_list (cp_parser* parser,
5153 bool is_attribute_list,
5155 bool allow_expansion_p,
5156 bool *non_constant_p)
5158 tree expression_list = NULL_TREE;
5159 bool fold_expr_p = is_attribute_list;
5160 tree identifier = NULL_TREE;
5161 bool saved_greater_than_is_operator_p;
5163 /* Assume all the expressions will be constant. */
5165 *non_constant_p = false;
5167 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5168 return error_mark_node;
5170 /* Within a parenthesized expression, a `>' token is always
5171 the greater-than operator. */
5172 saved_greater_than_is_operator_p
5173 = parser->greater_than_is_operator_p;
5174 parser->greater_than_is_operator_p = true;
5176 /* Consume expressions until there are no more. */
5177 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5182 /* At the beginning of attribute lists, check to see if the
5183 next token is an identifier. */
5184 if (is_attribute_list
5185 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5189 /* Consume the identifier. */
5190 token = cp_lexer_consume_token (parser->lexer);
5191 /* Save the identifier. */
5192 identifier = token->u.value;
5196 bool expr_non_constant_p;
5198 /* Parse the next assignment-expression. */
5199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5201 /* A braced-init-list. */
5202 maybe_warn_cpp0x ("extended initializer lists");
5203 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5204 if (non_constant_p && expr_non_constant_p)
5205 *non_constant_p = true;
5207 else if (non_constant_p)
5209 expr = (cp_parser_constant_expression
5210 (parser, /*allow_non_constant_p=*/true,
5211 &expr_non_constant_p));
5212 if (expr_non_constant_p)
5213 *non_constant_p = true;
5216 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5219 expr = fold_non_dependent_expr (expr);
5221 /* If we have an ellipsis, then this is an expression
5223 if (allow_expansion_p
5224 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5226 /* Consume the `...'. */
5227 cp_lexer_consume_token (parser->lexer);
5229 /* Build the argument pack. */
5230 expr = make_pack_expansion (expr);
5233 /* Add it to the list. We add error_mark_node
5234 expressions to the list, so that we can still tell if
5235 the correct form for a parenthesized expression-list
5236 is found. That gives better errors. */
5237 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5239 if (expr == error_mark_node)
5243 /* After the first item, attribute lists look the same as
5244 expression lists. */
5245 is_attribute_list = false;
5248 /* If the next token isn't a `,', then we are done. */
5249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5252 /* Otherwise, consume the `,' and keep going. */
5253 cp_lexer_consume_token (parser->lexer);
5256 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5261 /* We try and resync to an unnested comma, as that will give the
5262 user better diagnostics. */
5263 ending = cp_parser_skip_to_closing_parenthesis (parser,
5264 /*recovering=*/true,
5266 /*consume_paren=*/true);
5271 parser->greater_than_is_operator_p
5272 = saved_greater_than_is_operator_p;
5273 return error_mark_node;
5277 parser->greater_than_is_operator_p
5278 = saved_greater_than_is_operator_p;
5280 /* We built up the list in reverse order so we must reverse it now. */
5281 expression_list = nreverse (expression_list);
5283 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5285 return expression_list;
5288 /* Parse a pseudo-destructor-name.
5290 pseudo-destructor-name:
5291 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5292 :: [opt] nested-name-specifier template template-id :: ~ type-name
5293 :: [opt] nested-name-specifier [opt] ~ type-name
5295 If either of the first two productions is used, sets *SCOPE to the
5296 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5297 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5298 or ERROR_MARK_NODE if the parse fails. */
5301 cp_parser_pseudo_destructor_name (cp_parser* parser,
5305 bool nested_name_specifier_p;
5307 /* Assume that things will not work out. */
5308 *type = error_mark_node;
5310 /* Look for the optional `::' operator. */
5311 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5312 /* Look for the optional nested-name-specifier. */
5313 nested_name_specifier_p
5314 = (cp_parser_nested_name_specifier_opt (parser,
5315 /*typename_keyword_p=*/false,
5316 /*check_dependency_p=*/true,
5318 /*is_declaration=*/false)
5320 /* Now, if we saw a nested-name-specifier, we might be doing the
5321 second production. */
5322 if (nested_name_specifier_p
5323 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5325 /* Consume the `template' keyword. */
5326 cp_lexer_consume_token (parser->lexer);
5327 /* Parse the template-id. */
5328 cp_parser_template_id (parser,
5329 /*template_keyword_p=*/true,
5330 /*check_dependency_p=*/false,
5331 /*is_declaration=*/true);
5332 /* Look for the `::' token. */
5333 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5335 /* If the next token is not a `~', then there might be some
5336 additional qualification. */
5337 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5339 /* At this point, we're looking for "type-name :: ~". The type-name
5340 must not be a class-name, since this is a pseudo-destructor. So,
5341 it must be either an enum-name, or a typedef-name -- both of which
5342 are just identifiers. So, we peek ahead to check that the "::"
5343 and "~" tokens are present; if they are not, then we can avoid
5344 calling type_name. */
5345 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5346 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5347 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5349 cp_parser_error (parser, "non-scalar type");
5353 /* Look for the type-name. */
5354 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5355 if (*scope == error_mark_node)
5358 /* Look for the `::' token. */
5359 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5364 /* Look for the `~'. */
5365 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5366 /* Look for the type-name again. We are not responsible for
5367 checking that it matches the first type-name. */
5368 *type = cp_parser_nonclass_name (parser);
5371 /* Parse a unary-expression.
5377 unary-operator cast-expression
5378 sizeof unary-expression
5386 __extension__ cast-expression
5387 __alignof__ unary-expression
5388 __alignof__ ( type-id )
5389 __real__ cast-expression
5390 __imag__ cast-expression
5393 ADDRESS_P is true iff the unary-expression is appearing as the
5394 operand of the `&' operator. CAST_P is true if this expression is
5395 the target of a cast.
5397 Returns a representation of the expression. */
5400 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5404 enum tree_code unary_operator;
5406 /* Peek at the next token. */
5407 token = cp_lexer_peek_token (parser->lexer);
5408 /* Some keywords give away the kind of expression. */
5409 if (token->type == CPP_KEYWORD)
5411 enum rid keyword = token->keyword;
5421 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5422 /* Consume the token. */
5423 cp_lexer_consume_token (parser->lexer);
5424 /* Parse the operand. */
5425 operand = cp_parser_sizeof_operand (parser, keyword);
5427 if (TYPE_P (operand))
5428 return cxx_sizeof_or_alignof_type (operand, op, true);
5430 return cxx_sizeof_or_alignof_expr (operand, op, true);
5434 return cp_parser_new_expression (parser);
5437 return cp_parser_delete_expression (parser);
5441 /* The saved value of the PEDANTIC flag. */
5445 /* Save away the PEDANTIC flag. */
5446 cp_parser_extension_opt (parser, &saved_pedantic);
5447 /* Parse the cast-expression. */
5448 expr = cp_parser_simple_cast_expression (parser);
5449 /* Restore the PEDANTIC flag. */
5450 pedantic = saved_pedantic;
5460 /* Consume the `__real__' or `__imag__' token. */
5461 cp_lexer_consume_token (parser->lexer);
5462 /* Parse the cast-expression. */
5463 expression = cp_parser_simple_cast_expression (parser);
5464 /* Create the complete representation. */
5465 return build_x_unary_op ((keyword == RID_REALPART
5466 ? REALPART_EXPR : IMAGPART_EXPR),
5468 tf_warning_or_error);
5477 /* Look for the `:: new' and `:: delete', which also signal the
5478 beginning of a new-expression, or delete-expression,
5479 respectively. If the next token is `::', then it might be one of
5481 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5485 /* See if the token after the `::' is one of the keywords in
5486 which we're interested. */
5487 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5488 /* If it's `new', we have a new-expression. */
5489 if (keyword == RID_NEW)
5490 return cp_parser_new_expression (parser);
5491 /* Similarly, for `delete'. */
5492 else if (keyword == RID_DELETE)
5493 return cp_parser_delete_expression (parser);
5496 /* Look for a unary operator. */
5497 unary_operator = cp_parser_unary_operator (token);
5498 /* The `++' and `--' operators can be handled similarly, even though
5499 they are not technically unary-operators in the grammar. */
5500 if (unary_operator == ERROR_MARK)
5502 if (token->type == CPP_PLUS_PLUS)
5503 unary_operator = PREINCREMENT_EXPR;
5504 else if (token->type == CPP_MINUS_MINUS)
5505 unary_operator = PREDECREMENT_EXPR;
5506 /* Handle the GNU address-of-label extension. */
5507 else if (cp_parser_allow_gnu_extensions_p (parser)
5508 && token->type == CPP_AND_AND)
5512 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5514 /* Consume the '&&' token. */
5515 cp_lexer_consume_token (parser->lexer);
5516 /* Look for the identifier. */
5517 identifier = cp_parser_identifier (parser);
5518 /* Create an expression representing the address. */
5519 expression = finish_label_address_expr (identifier, loc);
5520 if (cp_parser_non_integral_constant_expression (parser,
5521 "the address of a label"))
5522 expression = error_mark_node;
5526 if (unary_operator != ERROR_MARK)
5528 tree cast_expression;
5529 tree expression = error_mark_node;
5530 const char *non_constant_p = NULL;
5532 /* Consume the operator token. */
5533 token = cp_lexer_consume_token (parser->lexer);
5534 /* Parse the cast-expression. */
5536 = cp_parser_cast_expression (parser,
5537 unary_operator == ADDR_EXPR,
5538 /*cast_p=*/false, pidk);
5539 /* Now, build an appropriate representation. */
5540 switch (unary_operator)
5543 non_constant_p = "%<*%>";
5544 expression = build_x_indirect_ref (cast_expression, "unary *",
5545 tf_warning_or_error);
5549 non_constant_p = "%<&%>";
5552 expression = build_x_unary_op (unary_operator, cast_expression,
5553 tf_warning_or_error);
5556 case PREINCREMENT_EXPR:
5557 case PREDECREMENT_EXPR:
5558 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5559 ? "%<++%>" : "%<--%>");
5561 case UNARY_PLUS_EXPR:
5563 case TRUTH_NOT_EXPR:
5564 expression = finish_unary_op_expr (unary_operator, cast_expression);
5572 && cp_parser_non_integral_constant_expression (parser,
5574 expression = error_mark_node;
5579 return cp_parser_postfix_expression (parser, address_p, cast_p,
5580 /*member_access_only_p=*/false,
5584 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5585 unary-operator, the corresponding tree code is returned. */
5587 static enum tree_code
5588 cp_parser_unary_operator (cp_token* token)
5590 switch (token->type)
5593 return INDIRECT_REF;
5599 return UNARY_PLUS_EXPR;
5605 return TRUTH_NOT_EXPR;
5608 return BIT_NOT_EXPR;
5615 /* Parse a new-expression.
5618 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5619 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5621 Returns a representation of the expression. */
5624 cp_parser_new_expression (cp_parser* parser)
5626 bool global_scope_p;
5632 /* Look for the optional `::' operator. */
5634 = (cp_parser_global_scope_opt (parser,
5635 /*current_scope_valid_p=*/false)
5637 /* Look for the `new' operator. */
5638 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5639 /* There's no easy way to tell a new-placement from the
5640 `( type-id )' construct. */
5641 cp_parser_parse_tentatively (parser);
5642 /* Look for a new-placement. */
5643 placement = cp_parser_new_placement (parser);
5644 /* If that didn't work out, there's no new-placement. */
5645 if (!cp_parser_parse_definitely (parser))
5646 placement = NULL_TREE;
5648 /* If the next token is a `(', then we have a parenthesized
5650 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5653 /* Consume the `('. */
5654 cp_lexer_consume_token (parser->lexer);
5655 /* Parse the type-id. */
5656 type = cp_parser_type_id (parser);
5657 /* Look for the closing `)'. */
5658 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5659 token = cp_lexer_peek_token (parser->lexer);
5660 /* There should not be a direct-new-declarator in this production,
5661 but GCC used to allowed this, so we check and emit a sensible error
5662 message for this case. */
5663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5665 error ("%Harray bound forbidden after parenthesized type-id",
5667 inform (token->location,
5668 "try removing the parentheses around the type-id");
5669 cp_parser_direct_new_declarator (parser);
5673 /* Otherwise, there must be a new-type-id. */
5675 type = cp_parser_new_type_id (parser, &nelts);
5677 /* If the next token is a `(' or '{', then we have a new-initializer. */
5678 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5679 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5680 initializer = cp_parser_new_initializer (parser);
5682 initializer = NULL_TREE;
5684 /* A new-expression may not appear in an integral constant
5686 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5687 return error_mark_node;
5689 /* Create a representation of the new-expression. */
5690 return build_new (placement, type, nelts, initializer, global_scope_p,
5691 tf_warning_or_error);
5694 /* Parse a new-placement.
5699 Returns the same representation as for an expression-list. */
5702 cp_parser_new_placement (cp_parser* parser)
5704 tree expression_list;
5706 /* Parse the expression-list. */
5707 expression_list = (cp_parser_parenthesized_expression_list
5708 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5709 /*non_constant_p=*/NULL));
5711 return expression_list;
5714 /* Parse a new-type-id.
5717 type-specifier-seq new-declarator [opt]
5719 Returns the TYPE allocated. If the new-type-id indicates an array
5720 type, *NELTS is set to the number of elements in the last array
5721 bound; the TYPE will not include the last array bound. */
5724 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5726 cp_decl_specifier_seq type_specifier_seq;
5727 cp_declarator *new_declarator;
5728 cp_declarator *declarator;
5729 cp_declarator *outer_declarator;
5730 const char *saved_message;
5733 /* The type-specifier sequence must not contain type definitions.
5734 (It cannot contain declarations of new types either, but if they
5735 are not definitions we will catch that because they are not
5737 saved_message = parser->type_definition_forbidden_message;
5738 parser->type_definition_forbidden_message
5739 = "types may not be defined in a new-type-id";
5740 /* Parse the type-specifier-seq. */
5741 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5742 &type_specifier_seq);
5743 /* Restore the old message. */
5744 parser->type_definition_forbidden_message = saved_message;
5745 /* Parse the new-declarator. */
5746 new_declarator = cp_parser_new_declarator_opt (parser);
5748 /* Determine the number of elements in the last array dimension, if
5751 /* Skip down to the last array dimension. */
5752 declarator = new_declarator;
5753 outer_declarator = NULL;
5754 while (declarator && (declarator->kind == cdk_pointer
5755 || declarator->kind == cdk_ptrmem))
5757 outer_declarator = declarator;
5758 declarator = declarator->declarator;
5761 && declarator->kind == cdk_array
5762 && declarator->declarator
5763 && declarator->declarator->kind == cdk_array)
5765 outer_declarator = declarator;
5766 declarator = declarator->declarator;
5769 if (declarator && declarator->kind == cdk_array)
5771 *nelts = declarator->u.array.bounds;
5772 if (*nelts == error_mark_node)
5773 *nelts = integer_one_node;
5775 if (outer_declarator)
5776 outer_declarator->declarator = declarator->declarator;
5778 new_declarator = NULL;
5781 type = groktypename (&type_specifier_seq, new_declarator, false);
5785 /* Parse an (optional) new-declarator.
5788 ptr-operator new-declarator [opt]
5789 direct-new-declarator
5791 Returns the declarator. */
5793 static cp_declarator *
5794 cp_parser_new_declarator_opt (cp_parser* parser)
5796 enum tree_code code;
5798 cp_cv_quals cv_quals;
5800 /* We don't know if there's a ptr-operator next, or not. */
5801 cp_parser_parse_tentatively (parser);
5802 /* Look for a ptr-operator. */
5803 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5804 /* If that worked, look for more new-declarators. */
5805 if (cp_parser_parse_definitely (parser))
5807 cp_declarator *declarator;
5809 /* Parse another optional declarator. */
5810 declarator = cp_parser_new_declarator_opt (parser);
5812 return cp_parser_make_indirect_declarator
5813 (code, type, cv_quals, declarator);
5816 /* If the next token is a `[', there is a direct-new-declarator. */
5817 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5818 return cp_parser_direct_new_declarator (parser);
5823 /* Parse a direct-new-declarator.
5825 direct-new-declarator:
5827 direct-new-declarator [constant-expression]
5831 static cp_declarator *
5832 cp_parser_direct_new_declarator (cp_parser* parser)
5834 cp_declarator *declarator = NULL;
5840 /* Look for the opening `['. */
5841 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5842 /* The first expression is not required to be constant. */
5845 cp_token *token = cp_lexer_peek_token (parser->lexer);
5846 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5847 /* The standard requires that the expression have integral
5848 type. DR 74 adds enumeration types. We believe that the
5849 real intent is that these expressions be handled like the
5850 expression in a `switch' condition, which also allows
5851 classes with a single conversion to integral or
5852 enumeration type. */
5853 if (!processing_template_decl)
5856 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5861 error ("%Hexpression in new-declarator must have integral "
5862 "or enumeration type", &token->location);
5863 expression = error_mark_node;
5867 /* But all the other expressions must be. */
5870 = cp_parser_constant_expression (parser,
5871 /*allow_non_constant=*/false,
5873 /* Look for the closing `]'. */
5874 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5876 /* Add this bound to the declarator. */
5877 declarator = make_array_declarator (declarator, expression);
5879 /* If the next token is not a `[', then there are no more
5881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5888 /* Parse a new-initializer.
5891 ( expression-list [opt] )
5894 Returns a representation of the expression-list. If there is no
5895 expression-list, VOID_ZERO_NODE is returned. */
5898 cp_parser_new_initializer (cp_parser* parser)
5900 tree expression_list;
5902 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5904 bool expr_non_constant_p;
5905 maybe_warn_cpp0x ("extended initializer lists");
5906 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5907 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5908 expression_list = build_tree_list (NULL_TREE, expression_list);
5911 expression_list = (cp_parser_parenthesized_expression_list
5912 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5913 /*non_constant_p=*/NULL));
5914 if (!expression_list)
5915 expression_list = void_zero_node;
5917 return expression_list;
5920 /* Parse a delete-expression.
5923 :: [opt] delete cast-expression
5924 :: [opt] delete [ ] cast-expression
5926 Returns a representation of the expression. */
5929 cp_parser_delete_expression (cp_parser* parser)
5931 bool global_scope_p;
5935 /* Look for the optional `::' operator. */
5937 = (cp_parser_global_scope_opt (parser,
5938 /*current_scope_valid_p=*/false)
5940 /* Look for the `delete' keyword. */
5941 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5942 /* See if the array syntax is in use. */
5943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5945 /* Consume the `[' token. */
5946 cp_lexer_consume_token (parser->lexer);
5947 /* Look for the `]' token. */
5948 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5949 /* Remember that this is the `[]' construct. */
5955 /* Parse the cast-expression. */
5956 expression = cp_parser_simple_cast_expression (parser);
5958 /* A delete-expression may not appear in an integral constant
5960 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5961 return error_mark_node;
5963 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5966 /* Returns true if TOKEN may start a cast-expression and false
5970 cp_parser_token_starts_cast_expression (cp_token *token)
5972 switch (token->type)
5978 case CPP_CLOSE_SQUARE:
5979 case CPP_CLOSE_PAREN:
5980 case CPP_CLOSE_BRACE:
5984 case CPP_DEREF_STAR:
5992 case CPP_GREATER_EQ:
6012 /* '[' may start a primary-expression in obj-c++. */
6013 case CPP_OPEN_SQUARE:
6014 return c_dialect_objc ();
6021 /* Parse a cast-expression.
6025 ( type-id ) cast-expression
6027 ADDRESS_P is true iff the unary-expression is appearing as the
6028 operand of the `&' operator. CAST_P is true if this expression is
6029 the target of a cast.
6031 Returns a representation of the expression. */
6034 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6037 /* If it's a `(', then we might be looking at a cast. */
6038 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6040 tree type = NULL_TREE;
6041 tree expr = NULL_TREE;
6042 bool compound_literal_p;
6043 const char *saved_message;
6045 /* There's no way to know yet whether or not this is a cast.
6046 For example, `(int (3))' is a unary-expression, while `(int)
6047 3' is a cast. So, we resort to parsing tentatively. */
6048 cp_parser_parse_tentatively (parser);
6049 /* Types may not be defined in a cast. */
6050 saved_message = parser->type_definition_forbidden_message;
6051 parser->type_definition_forbidden_message
6052 = "types may not be defined in casts";
6053 /* Consume the `('. */
6054 cp_lexer_consume_token (parser->lexer);
6055 /* A very tricky bit is that `(struct S) { 3 }' is a
6056 compound-literal (which we permit in C++ as an extension).
6057 But, that construct is not a cast-expression -- it is a
6058 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6059 is legal; if the compound-literal were a cast-expression,
6060 you'd need an extra set of parentheses.) But, if we parse
6061 the type-id, and it happens to be a class-specifier, then we
6062 will commit to the parse at that point, because we cannot
6063 undo the action that is done when creating a new class. So,
6064 then we cannot back up and do a postfix-expression.
6066 Therefore, we scan ahead to the closing `)', and check to see
6067 if the token after the `)' is a `{'. If so, we are not
6068 looking at a cast-expression.
6070 Save tokens so that we can put them back. */
6071 cp_lexer_save_tokens (parser->lexer);
6072 /* Skip tokens until the next token is a closing parenthesis.
6073 If we find the closing `)', and the next token is a `{', then
6074 we are looking at a compound-literal. */
6076 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6077 /*consume_paren=*/true)
6078 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6079 /* Roll back the tokens we skipped. */
6080 cp_lexer_rollback_tokens (parser->lexer);
6081 /* If we were looking at a compound-literal, simulate an error
6082 so that the call to cp_parser_parse_definitely below will
6084 if (compound_literal_p)
6085 cp_parser_simulate_error (parser);
6088 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6089 parser->in_type_id_in_expr_p = true;
6090 /* Look for the type-id. */
6091 type = cp_parser_type_id (parser);
6092 /* Look for the closing `)'. */
6093 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6094 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6097 /* Restore the saved message. */
6098 parser->type_definition_forbidden_message = saved_message;
6100 /* At this point this can only be either a cast or a
6101 parenthesized ctor such as `(T ())' that looks like a cast to
6102 function returning T. */
6103 if (!cp_parser_error_occurred (parser)
6104 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6107 cp_parser_parse_definitely (parser);
6108 expr = cp_parser_cast_expression (parser,
6109 /*address_p=*/false,
6110 /*cast_p=*/true, pidk);
6112 /* Warn about old-style casts, if so requested. */
6113 if (warn_old_style_cast
6114 && !in_system_header
6115 && !VOID_TYPE_P (type)
6116 && current_lang_name != lang_name_c)
6117 warning (OPT_Wold_style_cast, "use of old-style cast");
6119 /* Only type conversions to integral or enumeration types
6120 can be used in constant-expressions. */
6121 if (!cast_valid_in_integral_constant_expression_p (type)
6122 && (cp_parser_non_integral_constant_expression
6124 "a cast to a type other than an integral or "
6125 "enumeration type")))
6126 return error_mark_node;
6128 /* Perform the cast. */
6129 expr = build_c_cast (type, expr);
6133 cp_parser_abort_tentative_parse (parser);
6136 /* If we get here, then it's not a cast, so it must be a
6137 unary-expression. */
6138 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6141 /* Parse a binary expression of the general form:
6145 pm-expression .* cast-expression
6146 pm-expression ->* cast-expression
6148 multiplicative-expression:
6150 multiplicative-expression * pm-expression
6151 multiplicative-expression / pm-expression
6152 multiplicative-expression % pm-expression
6154 additive-expression:
6155 multiplicative-expression
6156 additive-expression + multiplicative-expression
6157 additive-expression - multiplicative-expression
6161 shift-expression << additive-expression
6162 shift-expression >> additive-expression
6164 relational-expression:
6166 relational-expression < shift-expression
6167 relational-expression > shift-expression
6168 relational-expression <= shift-expression
6169 relational-expression >= shift-expression
6173 relational-expression:
6174 relational-expression <? shift-expression
6175 relational-expression >? shift-expression
6177 equality-expression:
6178 relational-expression
6179 equality-expression == relational-expression
6180 equality-expression != relational-expression
6184 and-expression & equality-expression
6186 exclusive-or-expression:
6188 exclusive-or-expression ^ and-expression
6190 inclusive-or-expression:
6191 exclusive-or-expression
6192 inclusive-or-expression | exclusive-or-expression
6194 logical-and-expression:
6195 inclusive-or-expression
6196 logical-and-expression && inclusive-or-expression
6198 logical-or-expression:
6199 logical-and-expression
6200 logical-or-expression || logical-and-expression
6202 All these are implemented with a single function like:
6205 simple-cast-expression
6206 binary-expression <token> binary-expression
6208 CAST_P is true if this expression is the target of a cast.
6210 The binops_by_token map is used to get the tree codes for each <token> type.
6211 binary-expressions are associated according to a precedence table. */
6213 #define TOKEN_PRECEDENCE(token) \
6214 (((token->type == CPP_GREATER \
6215 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6216 && !parser->greater_than_is_operator_p) \
6217 ? PREC_NOT_OPERATOR \
6218 : binops_by_token[token->type].prec)
6221 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6222 bool no_toplevel_fold_p,
6223 enum cp_parser_prec prec,
6226 cp_parser_expression_stack stack;
6227 cp_parser_expression_stack_entry *sp = &stack[0];
6230 enum tree_code tree_type, lhs_type, rhs_type;
6231 enum cp_parser_prec new_prec, lookahead_prec;
6234 /* Parse the first expression. */
6235 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6236 lhs_type = ERROR_MARK;
6240 /* Get an operator token. */
6241 token = cp_lexer_peek_token (parser->lexer);
6243 if (warn_cxx0x_compat
6244 && token->type == CPP_RSHIFT
6245 && !parser->greater_than_is_operator_p)
6247 warning (OPT_Wc__0x_compat,
6248 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6250 warning (OPT_Wc__0x_compat,
6251 "suggest parentheses around %<>>%> expression");
6254 new_prec = TOKEN_PRECEDENCE (token);
6256 /* Popping an entry off the stack means we completed a subexpression:
6257 - either we found a token which is not an operator (`>' where it is not
6258 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6259 will happen repeatedly;
6260 - or, we found an operator which has lower priority. This is the case
6261 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6263 if (new_prec <= prec)
6272 tree_type = binops_by_token[token->type].tree_type;
6274 /* We used the operator token. */
6275 cp_lexer_consume_token (parser->lexer);
6277 /* Extract another operand. It may be the RHS of this expression
6278 or the LHS of a new, higher priority expression. */
6279 rhs = cp_parser_simple_cast_expression (parser);
6280 rhs_type = ERROR_MARK;
6282 /* Get another operator token. Look up its precedence to avoid
6283 building a useless (immediately popped) stack entry for common
6284 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6285 token = cp_lexer_peek_token (parser->lexer);
6286 lookahead_prec = TOKEN_PRECEDENCE (token);
6287 if (lookahead_prec > new_prec)
6289 /* ... and prepare to parse the RHS of the new, higher priority
6290 expression. Since precedence levels on the stack are
6291 monotonically increasing, we do not have to care about
6294 sp->tree_type = tree_type;
6296 sp->lhs_type = lhs_type;
6299 lhs_type = rhs_type;
6301 new_prec = lookahead_prec;
6305 lookahead_prec = new_prec;
6306 /* If the stack is not empty, we have parsed into LHS the right side
6307 (`4' in the example above) of an expression we had suspended.
6308 We can use the information on the stack to recover the LHS (`3')
6309 from the stack together with the tree code (`MULT_EXPR'), and
6310 the precedence of the higher level subexpression
6311 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6312 which will be used to actually build the additive expression. */
6315 tree_type = sp->tree_type;
6317 rhs_type = lhs_type;
6319 lhs_type = sp->lhs_type;
6322 overloaded_p = false;
6323 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6324 ERROR_MARK for everything that is not a binary expression.
6325 This makes warn_about_parentheses miss some warnings that
6326 involve unary operators. For unary expressions we should
6327 pass the correct tree_code unless the unary expression was
6328 surrounded by parentheses.
6330 if (no_toplevel_fold_p
6331 && lookahead_prec <= prec
6333 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6334 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6336 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6337 &overloaded_p, tf_warning_or_error);
6338 lhs_type = tree_type;
6340 /* If the binary operator required the use of an overloaded operator,
6341 then this expression cannot be an integral constant-expression.
6342 An overloaded operator can be used even if both operands are
6343 otherwise permissible in an integral constant-expression if at
6344 least one of the operands is of enumeration type. */
6347 && (cp_parser_non_integral_constant_expression
6348 (parser, "calls to overloaded operators")))
6349 return error_mark_node;
6356 /* Parse the `? expression : assignment-expression' part of a
6357 conditional-expression. The LOGICAL_OR_EXPR is the
6358 logical-or-expression that started the conditional-expression.
6359 Returns a representation of the entire conditional-expression.
6361 This routine is used by cp_parser_assignment_expression.
6363 ? expression : assignment-expression
6367 ? : assignment-expression */
6370 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6373 tree assignment_expr;
6375 /* Consume the `?' token. */
6376 cp_lexer_consume_token (parser->lexer);
6377 if (cp_parser_allow_gnu_extensions_p (parser)
6378 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6379 /* Implicit true clause. */
6382 /* Parse the expression. */
6383 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6385 /* The next token should be a `:'. */
6386 cp_parser_require (parser, CPP_COLON, "%<:%>");
6387 /* Parse the assignment-expression. */
6388 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6390 /* Build the conditional-expression. */
6391 return build_x_conditional_expr (logical_or_expr,
6394 tf_warning_or_error);
6397 /* Parse an assignment-expression.
6399 assignment-expression:
6400 conditional-expression
6401 logical-or-expression assignment-operator assignment_expression
6404 CAST_P is true if this expression is the target of a cast.
6406 Returns a representation for the expression. */
6409 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6414 /* If the next token is the `throw' keyword, then we're looking at
6415 a throw-expression. */
6416 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6417 expr = cp_parser_throw_expression (parser);
6418 /* Otherwise, it must be that we are looking at a
6419 logical-or-expression. */
6422 /* Parse the binary expressions (logical-or-expression). */
6423 expr = cp_parser_binary_expression (parser, cast_p, false,
6424 PREC_NOT_OPERATOR, pidk);
6425 /* If the next token is a `?' then we're actually looking at a
6426 conditional-expression. */
6427 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6428 return cp_parser_question_colon_clause (parser, expr);
6431 enum tree_code assignment_operator;
6433 /* If it's an assignment-operator, we're using the second
6436 = cp_parser_assignment_operator_opt (parser);
6437 if (assignment_operator != ERROR_MARK)
6439 bool non_constant_p;
6441 /* Parse the right-hand side of the assignment. */
6442 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6444 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6445 maybe_warn_cpp0x ("extended initializer lists");
6447 /* An assignment may not appear in a
6448 constant-expression. */
6449 if (cp_parser_non_integral_constant_expression (parser,
6451 return error_mark_node;
6452 /* Build the assignment expression. */
6453 expr = build_x_modify_expr (expr,
6454 assignment_operator,
6456 tf_warning_or_error);
6464 /* Parse an (optional) assignment-operator.
6466 assignment-operator: one of
6467 = *= /= %= += -= >>= <<= &= ^= |=
6471 assignment-operator: one of
6474 If the next token is an assignment operator, the corresponding tree
6475 code is returned, and the token is consumed. For example, for
6476 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6477 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6478 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6479 operator, ERROR_MARK is returned. */
6481 static enum tree_code
6482 cp_parser_assignment_operator_opt (cp_parser* parser)
6487 /* Peek at the next token. */
6488 token = cp_lexer_peek_token (parser->lexer);
6490 switch (token->type)
6501 op = TRUNC_DIV_EXPR;
6505 op = TRUNC_MOD_EXPR;
6537 /* Nothing else is an assignment operator. */
6541 /* If it was an assignment operator, consume it. */
6542 if (op != ERROR_MARK)
6543 cp_lexer_consume_token (parser->lexer);
6548 /* Parse an expression.
6551 assignment-expression
6552 expression , assignment-expression
6554 CAST_P is true if this expression is the target of a cast.
6556 Returns a representation of the expression. */
6559 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6561 tree expression = NULL_TREE;
6565 tree assignment_expression;
6567 /* Parse the next assignment-expression. */
6568 assignment_expression
6569 = cp_parser_assignment_expression (parser, cast_p, pidk);
6570 /* If this is the first assignment-expression, we can just
6573 expression = assignment_expression;
6575 expression = build_x_compound_expr (expression,
6576 assignment_expression,
6577 tf_warning_or_error);
6578 /* If the next token is not a comma, then we are done with the
6580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6582 /* Consume the `,'. */
6583 cp_lexer_consume_token (parser->lexer);
6584 /* A comma operator cannot appear in a constant-expression. */
6585 if (cp_parser_non_integral_constant_expression (parser,
6586 "a comma operator"))
6587 expression = error_mark_node;
6593 /* Parse a constant-expression.
6595 constant-expression:
6596 conditional-expression
6598 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6599 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6600 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6601 is false, NON_CONSTANT_P should be NULL. */
6604 cp_parser_constant_expression (cp_parser* parser,
6605 bool allow_non_constant_p,
6606 bool *non_constant_p)
6608 bool saved_integral_constant_expression_p;
6609 bool saved_allow_non_integral_constant_expression_p;
6610 bool saved_non_integral_constant_expression_p;
6613 /* It might seem that we could simply parse the
6614 conditional-expression, and then check to see if it were
6615 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6616 one that the compiler can figure out is constant, possibly after
6617 doing some simplifications or optimizations. The standard has a
6618 precise definition of constant-expression, and we must honor
6619 that, even though it is somewhat more restrictive.
6625 is not a legal declaration, because `(2, 3)' is not a
6626 constant-expression. The `,' operator is forbidden in a
6627 constant-expression. However, GCC's constant-folding machinery
6628 will fold this operation to an INTEGER_CST for `3'. */
6630 /* Save the old settings. */
6631 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6632 saved_allow_non_integral_constant_expression_p
6633 = parser->allow_non_integral_constant_expression_p;
6634 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6635 /* We are now parsing a constant-expression. */
6636 parser->integral_constant_expression_p = true;
6637 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6638 parser->non_integral_constant_expression_p = false;
6639 /* Although the grammar says "conditional-expression", we parse an
6640 "assignment-expression", which also permits "throw-expression"
6641 and the use of assignment operators. In the case that
6642 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6643 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6644 actually essential that we look for an assignment-expression.
6645 For example, cp_parser_initializer_clauses uses this function to
6646 determine whether a particular assignment-expression is in fact
6648 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6649 /* Restore the old settings. */
6650 parser->integral_constant_expression_p
6651 = saved_integral_constant_expression_p;
6652 parser->allow_non_integral_constant_expression_p
6653 = saved_allow_non_integral_constant_expression_p;
6654 if (allow_non_constant_p)
6655 *non_constant_p = parser->non_integral_constant_expression_p;
6656 else if (parser->non_integral_constant_expression_p)
6657 expression = error_mark_node;
6658 parser->non_integral_constant_expression_p
6659 = saved_non_integral_constant_expression_p;
6664 /* Parse __builtin_offsetof.
6666 offsetof-expression:
6667 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6669 offsetof-member-designator:
6671 | offsetof-member-designator "." id-expression
6672 | offsetof-member-designator "[" expression "]"
6673 | offsetof-member-designator "->" id-expression */
6676 cp_parser_builtin_offsetof (cp_parser *parser)
6678 int save_ice_p, save_non_ice_p;
6683 /* We're about to accept non-integral-constant things, but will
6684 definitely yield an integral constant expression. Save and
6685 restore these values around our local parsing. */
6686 save_ice_p = parser->integral_constant_expression_p;
6687 save_non_ice_p = parser->non_integral_constant_expression_p;
6689 /* Consume the "__builtin_offsetof" token. */
6690 cp_lexer_consume_token (parser->lexer);
6691 /* Consume the opening `('. */
6692 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6693 /* Parse the type-id. */
6694 type = cp_parser_type_id (parser);
6695 /* Look for the `,'. */
6696 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6697 token = cp_lexer_peek_token (parser->lexer);
6699 /* Build the (type *)null that begins the traditional offsetof macro. */
6700 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6701 tf_warning_or_error);
6703 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6704 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6705 true, &dummy, token->location);
6708 token = cp_lexer_peek_token (parser->lexer);
6709 switch (token->type)
6711 case CPP_OPEN_SQUARE:
6712 /* offsetof-member-designator "[" expression "]" */
6713 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6717 /* offsetof-member-designator "->" identifier */
6718 expr = grok_array_decl (expr, integer_zero_node);
6722 /* offsetof-member-designator "." identifier */
6723 cp_lexer_consume_token (parser->lexer);
6724 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6729 case CPP_CLOSE_PAREN:
6730 /* Consume the ")" token. */
6731 cp_lexer_consume_token (parser->lexer);
6735 /* Error. We know the following require will fail, but
6736 that gives the proper error message. */
6737 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6738 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6739 expr = error_mark_node;
6745 /* If we're processing a template, we can't finish the semantics yet.
6746 Otherwise we can fold the entire expression now. */
6747 if (processing_template_decl)
6748 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6750 expr = finish_offsetof (expr);
6753 parser->integral_constant_expression_p = save_ice_p;
6754 parser->non_integral_constant_expression_p = save_non_ice_p;
6759 /* Parse a trait expression. */
6762 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6765 tree type1, type2 = NULL_TREE;
6766 bool binary = false;
6767 cp_decl_specifier_seq decl_specs;
6771 case RID_HAS_NOTHROW_ASSIGN:
6772 kind = CPTK_HAS_NOTHROW_ASSIGN;
6774 case RID_HAS_NOTHROW_CONSTRUCTOR:
6775 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6777 case RID_HAS_NOTHROW_COPY:
6778 kind = CPTK_HAS_NOTHROW_COPY;
6780 case RID_HAS_TRIVIAL_ASSIGN:
6781 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6783 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6784 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6786 case RID_HAS_TRIVIAL_COPY:
6787 kind = CPTK_HAS_TRIVIAL_COPY;
6789 case RID_HAS_TRIVIAL_DESTRUCTOR:
6790 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6792 case RID_HAS_VIRTUAL_DESTRUCTOR:
6793 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6795 case RID_IS_ABSTRACT:
6796 kind = CPTK_IS_ABSTRACT;
6798 case RID_IS_BASE_OF:
6799 kind = CPTK_IS_BASE_OF;
6803 kind = CPTK_IS_CLASS;
6805 case RID_IS_CONVERTIBLE_TO:
6806 kind = CPTK_IS_CONVERTIBLE_TO;
6810 kind = CPTK_IS_EMPTY;
6813 kind = CPTK_IS_ENUM;
6818 case RID_IS_POLYMORPHIC:
6819 kind = CPTK_IS_POLYMORPHIC;
6822 kind = CPTK_IS_UNION;
6828 /* Consume the token. */
6829 cp_lexer_consume_token (parser->lexer);
6831 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6833 type1 = cp_parser_type_id (parser);
6835 if (type1 == error_mark_node)
6836 return error_mark_node;
6838 /* Build a trivial decl-specifier-seq. */
6839 clear_decl_specs (&decl_specs);
6840 decl_specs.type = type1;
6842 /* Call grokdeclarator to figure out what type this is. */
6843 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6844 /*initialized=*/0, /*attrlist=*/NULL);
6848 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6850 type2 = cp_parser_type_id (parser);
6852 if (type2 == error_mark_node)
6853 return error_mark_node;
6855 /* Build a trivial decl-specifier-seq. */
6856 clear_decl_specs (&decl_specs);
6857 decl_specs.type = type2;
6859 /* Call grokdeclarator to figure out what type this is. */
6860 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6861 /*initialized=*/0, /*attrlist=*/NULL);
6864 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6866 /* Complete the trait expression, which may mean either processing
6867 the trait expr now or saving it for template instantiation. */
6868 return finish_trait_expr (kind, type1, type2);
6871 /* Statements [gram.stmt.stmt] */
6873 /* Parse a statement.
6877 expression-statement
6882 declaration-statement
6885 IN_COMPOUND is true when the statement is nested inside a
6886 cp_parser_compound_statement; this matters for certain pragmas.
6888 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6889 is a (possibly labeled) if statement which is not enclosed in braces
6890 and has an else clause. This is used to implement -Wparentheses. */
6893 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6894 bool in_compound, bool *if_p)
6898 location_t statement_location;
6903 /* There is no statement yet. */
6904 statement = NULL_TREE;
6905 /* Peek at the next token. */
6906 token = cp_lexer_peek_token (parser->lexer);
6907 /* Remember the location of the first token in the statement. */
6908 statement_location = token->location;
6909 /* If this is a keyword, then that will often determine what kind of
6910 statement we have. */
6911 if (token->type == CPP_KEYWORD)
6913 enum rid keyword = token->keyword;
6919 /* Looks like a labeled-statement with a case label.
6920 Parse the label, and then use tail recursion to parse
6922 cp_parser_label_for_labeled_statement (parser);
6927 statement = cp_parser_selection_statement (parser, if_p);
6933 statement = cp_parser_iteration_statement (parser);
6940 statement = cp_parser_jump_statement (parser);
6943 /* Objective-C++ exception-handling constructs. */
6946 case RID_AT_FINALLY:
6947 case RID_AT_SYNCHRONIZED:
6949 statement = cp_parser_objc_statement (parser);
6953 statement = cp_parser_try_block (parser);
6957 /* This must be a namespace alias definition. */
6958 cp_parser_declaration_statement (parser);
6962 /* It might be a keyword like `int' that can start a
6963 declaration-statement. */
6967 else if (token->type == CPP_NAME)
6969 /* If the next token is a `:', then we are looking at a
6970 labeled-statement. */
6971 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6972 if (token->type == CPP_COLON)
6974 /* Looks like a labeled-statement with an ordinary label.
6975 Parse the label, and then use tail recursion to parse
6977 cp_parser_label_for_labeled_statement (parser);
6981 /* Anything that starts with a `{' must be a compound-statement. */
6982 else if (token->type == CPP_OPEN_BRACE)
6983 statement = cp_parser_compound_statement (parser, NULL, false);
6984 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6985 a statement all its own. */
6986 else if (token->type == CPP_PRAGMA)
6988 /* Only certain OpenMP pragmas are attached to statements, and thus
6989 are considered statements themselves. All others are not. In
6990 the context of a compound, accept the pragma as a "statement" and
6991 return so that we can check for a close brace. Otherwise we
6992 require a real statement and must go back and read one. */
6994 cp_parser_pragma (parser, pragma_compound);
6995 else if (!cp_parser_pragma (parser, pragma_stmt))
6999 else if (token->type == CPP_EOF)
7001 cp_parser_error (parser, "expected statement");
7005 /* Everything else must be a declaration-statement or an
7006 expression-statement. Try for the declaration-statement
7007 first, unless we are looking at a `;', in which case we know that
7008 we have an expression-statement. */
7011 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7013 cp_parser_parse_tentatively (parser);
7014 /* Try to parse the declaration-statement. */
7015 cp_parser_declaration_statement (parser);
7016 /* If that worked, we're done. */
7017 if (cp_parser_parse_definitely (parser))
7020 /* Look for an expression-statement instead. */
7021 statement = cp_parser_expression_statement (parser, in_statement_expr);
7024 /* Set the line number for the statement. */
7025 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7026 SET_EXPR_LOCATION (statement, statement_location);
7029 /* Parse the label for a labeled-statement, i.e.
7032 case constant-expression :
7036 case constant-expression ... constant-expression : statement
7038 When a label is parsed without errors, the label is added to the
7039 parse tree by the finish_* functions, so this function doesn't
7040 have to return the label. */
7043 cp_parser_label_for_labeled_statement (cp_parser* parser)
7047 /* The next token should be an identifier. */
7048 token = cp_lexer_peek_token (parser->lexer);
7049 if (token->type != CPP_NAME
7050 && token->type != CPP_KEYWORD)
7052 cp_parser_error (parser, "expected labeled-statement");
7056 switch (token->keyword)
7063 /* Consume the `case' token. */
7064 cp_lexer_consume_token (parser->lexer);
7065 /* Parse the constant-expression. */
7066 expr = cp_parser_constant_expression (parser,
7067 /*allow_non_constant_p=*/false,
7070 ellipsis = cp_lexer_peek_token (parser->lexer);
7071 if (ellipsis->type == CPP_ELLIPSIS)
7073 /* Consume the `...' token. */
7074 cp_lexer_consume_token (parser->lexer);
7076 cp_parser_constant_expression (parser,
7077 /*allow_non_constant_p=*/false,
7079 /* We don't need to emit warnings here, as the common code
7080 will do this for us. */
7083 expr_hi = NULL_TREE;
7085 if (parser->in_switch_statement_p)
7086 finish_case_label (expr, expr_hi);
7088 error ("%Hcase label %qE not within a switch statement",
7089 &token->location, expr);
7094 /* Consume the `default' token. */
7095 cp_lexer_consume_token (parser->lexer);
7097 if (parser->in_switch_statement_p)
7098 finish_case_label (NULL_TREE, NULL_TREE);
7100 error ("%Hcase label not within a switch statement", &token->location);
7104 /* Anything else must be an ordinary label. */
7105 finish_label_stmt (cp_parser_identifier (parser));
7109 /* Require the `:' token. */
7110 cp_parser_require (parser, CPP_COLON, "%<:%>");
7113 /* Parse an expression-statement.
7115 expression-statement:
7118 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7119 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7120 indicates whether this expression-statement is part of an
7121 expression statement. */
7124 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7126 tree statement = NULL_TREE;
7128 /* If the next token is a ';', then there is no expression
7130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7131 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7133 /* Consume the final `;'. */
7134 cp_parser_consume_semicolon_at_end_of_statement (parser);
7136 if (in_statement_expr
7137 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7138 /* This is the final expression statement of a statement
7140 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7142 statement = finish_expr_stmt (statement);
7149 /* Parse a compound-statement.
7152 { statement-seq [opt] }
7157 { label-declaration-seq [opt] statement-seq [opt] }
7159 label-declaration-seq:
7161 label-declaration-seq label-declaration
7163 Returns a tree representing the statement. */
7166 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7171 /* Consume the `{'. */
7172 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7173 return error_mark_node;
7174 /* Begin the compound-statement. */
7175 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7176 /* If the next keyword is `__label__' we have a label declaration. */
7177 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7178 cp_parser_label_declaration (parser);
7179 /* Parse an (optional) statement-seq. */
7180 cp_parser_statement_seq_opt (parser, in_statement_expr);
7181 /* Finish the compound-statement. */
7182 finish_compound_stmt (compound_stmt);
7183 /* Consume the `}'. */
7184 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7186 return compound_stmt;
7189 /* Parse an (optional) statement-seq.
7193 statement-seq [opt] statement */
7196 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7198 /* Scan statements until there aren't any more. */
7201 cp_token *token = cp_lexer_peek_token (parser->lexer);
7203 /* If we're looking at a `}', then we've run out of statements. */
7204 if (token->type == CPP_CLOSE_BRACE
7205 || token->type == CPP_EOF
7206 || token->type == CPP_PRAGMA_EOL)
7209 /* If we are in a compound statement and find 'else' then
7210 something went wrong. */
7211 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7213 if (parser->in_statement & IN_IF_STMT)
7217 token = cp_lexer_consume_token (parser->lexer);
7218 error ("%H%<else%> without a previous %<if%>", &token->location);
7222 /* Parse the statement. */
7223 cp_parser_statement (parser, in_statement_expr, true, NULL);
7227 /* Parse a selection-statement.
7229 selection-statement:
7230 if ( condition ) statement
7231 if ( condition ) statement else statement
7232 switch ( condition ) statement
7234 Returns the new IF_STMT or SWITCH_STMT.
7236 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7237 is a (possibly labeled) if statement which is not enclosed in
7238 braces and has an else clause. This is used to implement
7242 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7250 /* Peek at the next token. */
7251 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7253 /* See what kind of keyword it is. */
7254 keyword = token->keyword;
7263 /* Look for the `('. */
7264 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7266 cp_parser_skip_to_end_of_statement (parser);
7267 return error_mark_node;
7270 /* Begin the selection-statement. */
7271 if (keyword == RID_IF)
7272 statement = begin_if_stmt ();
7274 statement = begin_switch_stmt ();
7276 /* Parse the condition. */
7277 condition = cp_parser_condition (parser);
7278 /* Look for the `)'. */
7279 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7280 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7281 /*consume_paren=*/true);
7283 if (keyword == RID_IF)
7286 unsigned char in_statement;
7288 /* Add the condition. */
7289 finish_if_stmt_cond (condition, statement);
7291 /* Parse the then-clause. */
7292 in_statement = parser->in_statement;
7293 parser->in_statement |= IN_IF_STMT;
7294 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7296 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7297 add_stmt (build_empty_stmt ());
7298 cp_lexer_consume_token (parser->lexer);
7299 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7300 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7301 "empty body in an %<if%> statement");
7305 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7306 parser->in_statement = in_statement;
7308 finish_then_clause (statement);
7310 /* If the next token is `else', parse the else-clause. */
7311 if (cp_lexer_next_token_is_keyword (parser->lexer,
7314 /* Consume the `else' keyword. */
7315 cp_lexer_consume_token (parser->lexer);
7316 begin_else_clause (statement);
7317 /* Parse the else-clause. */
7318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7320 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7321 OPT_Wempty_body, "suggest braces around "
7322 "empty body in an %<else%> statement");
7323 add_stmt (build_empty_stmt ());
7324 cp_lexer_consume_token (parser->lexer);
7327 cp_parser_implicitly_scoped_statement (parser, NULL);
7329 finish_else_clause (statement);
7331 /* If we are currently parsing a then-clause, then
7332 IF_P will not be NULL. We set it to true to
7333 indicate that this if statement has an else clause.
7334 This may trigger the Wparentheses warning below
7335 when we get back up to the parent if statement. */
7341 /* This if statement does not have an else clause. If
7342 NESTED_IF is true, then the then-clause is an if
7343 statement which does have an else clause. We warn
7344 about the potential ambiguity. */
7346 warning (OPT_Wparentheses,
7347 ("%Hsuggest explicit braces "
7348 "to avoid ambiguous %<else%>"),
7349 EXPR_LOCUS (statement));
7352 /* Now we're all done with the if-statement. */
7353 finish_if_stmt (statement);
7357 bool in_switch_statement_p;
7358 unsigned char in_statement;
7360 /* Add the condition. */
7361 finish_switch_cond (condition, statement);
7363 /* Parse the body of the switch-statement. */
7364 in_switch_statement_p = parser->in_switch_statement_p;
7365 in_statement = parser->in_statement;
7366 parser->in_switch_statement_p = true;
7367 parser->in_statement |= IN_SWITCH_STMT;
7368 cp_parser_implicitly_scoped_statement (parser, NULL);
7369 parser->in_switch_statement_p = in_switch_statement_p;
7370 parser->in_statement = in_statement;
7372 /* Now we're all done with the switch-statement. */
7373 finish_switch_stmt (statement);
7381 cp_parser_error (parser, "expected selection-statement");
7382 return error_mark_node;
7386 /* Parse a condition.
7390 type-specifier-seq declarator = initializer-clause
7391 type-specifier-seq declarator braced-init-list
7396 type-specifier-seq declarator asm-specification [opt]
7397 attributes [opt] = assignment-expression
7399 Returns the expression that should be tested. */
7402 cp_parser_condition (cp_parser* parser)
7404 cp_decl_specifier_seq type_specifiers;
7405 const char *saved_message;
7407 /* Try the declaration first. */
7408 cp_parser_parse_tentatively (parser);
7409 /* New types are not allowed in the type-specifier-seq for a
7411 saved_message = parser->type_definition_forbidden_message;
7412 parser->type_definition_forbidden_message
7413 = "types may not be defined in conditions";
7414 /* Parse the type-specifier-seq. */
7415 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7417 /* Restore the saved message. */
7418 parser->type_definition_forbidden_message = saved_message;
7419 /* If all is well, we might be looking at a declaration. */
7420 if (!cp_parser_error_occurred (parser))
7423 tree asm_specification;
7425 cp_declarator *declarator;
7426 tree initializer = NULL_TREE;
7428 /* Parse the declarator. */
7429 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7430 /*ctor_dtor_or_conv_p=*/NULL,
7431 /*parenthesized_p=*/NULL,
7432 /*member_p=*/false);
7433 /* Parse the attributes. */
7434 attributes = cp_parser_attributes_opt (parser);
7435 /* Parse the asm-specification. */
7436 asm_specification = cp_parser_asm_specification_opt (parser);
7437 /* If the next token is not an `=' or '{', then we might still be
7438 looking at an expression. For example:
7442 looks like a decl-specifier-seq and a declarator -- but then
7443 there is no `=', so this is an expression. */
7444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7445 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7446 cp_parser_simulate_error (parser);
7448 /* If we did see an `=' or '{', then we are looking at a declaration
7450 if (cp_parser_parse_definitely (parser))
7453 bool non_constant_p;
7454 bool flags = LOOKUP_ONLYCONVERTING;
7456 /* Create the declaration. */
7457 decl = start_decl (declarator, &type_specifiers,
7458 /*initialized_p=*/true,
7459 attributes, /*prefix_attributes=*/NULL_TREE,
7462 /* Parse the initializer. */
7463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7465 initializer = cp_parser_braced_list (parser, &non_constant_p);
7466 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7471 /* Consume the `='. */
7472 cp_parser_require (parser, CPP_EQ, "%<=%>");
7473 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7475 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7476 maybe_warn_cpp0x ("extended initializer lists");
7478 if (!non_constant_p)
7479 initializer = fold_non_dependent_expr (initializer);
7481 /* Process the initializer. */
7482 cp_finish_decl (decl,
7483 initializer, !non_constant_p,
7488 pop_scope (pushed_scope);
7490 return convert_from_reference (decl);
7493 /* If we didn't even get past the declarator successfully, we are
7494 definitely not looking at a declaration. */
7496 cp_parser_abort_tentative_parse (parser);
7498 /* Otherwise, we are looking at an expression. */
7499 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7502 /* Parse an iteration-statement.
7504 iteration-statement:
7505 while ( condition ) statement
7506 do statement while ( expression ) ;
7507 for ( for-init-statement condition [opt] ; expression [opt] )
7510 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7513 cp_parser_iteration_statement (cp_parser* parser)
7518 unsigned char in_statement;
7520 /* Peek at the next token. */
7521 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7523 return error_mark_node;
7525 /* Remember whether or not we are already within an iteration
7527 in_statement = parser->in_statement;
7529 /* See what kind of keyword it is. */
7530 keyword = token->keyword;
7537 /* Begin the while-statement. */
7538 statement = begin_while_stmt ();
7539 /* Look for the `('. */
7540 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7541 /* Parse the condition. */
7542 condition = cp_parser_condition (parser);
7543 finish_while_stmt_cond (condition, statement);
7544 /* Look for the `)'. */
7545 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7546 /* Parse the dependent statement. */
7547 parser->in_statement = IN_ITERATION_STMT;
7548 cp_parser_already_scoped_statement (parser);
7549 parser->in_statement = in_statement;
7550 /* We're done with the while-statement. */
7551 finish_while_stmt (statement);
7559 /* Begin the do-statement. */
7560 statement = begin_do_stmt ();
7561 /* Parse the body of the do-statement. */
7562 parser->in_statement = IN_ITERATION_STMT;
7563 cp_parser_implicitly_scoped_statement (parser, NULL);
7564 parser->in_statement = in_statement;
7565 finish_do_body (statement);
7566 /* Look for the `while' keyword. */
7567 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7568 /* Look for the `('. */
7569 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7570 /* Parse the expression. */
7571 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7572 /* We're done with the do-statement. */
7573 finish_do_stmt (expression, statement);
7574 /* Look for the `)'. */
7575 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7576 /* Look for the `;'. */
7577 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7583 tree condition = NULL_TREE;
7584 tree expression = NULL_TREE;
7586 /* Begin the for-statement. */
7587 statement = begin_for_stmt ();
7588 /* Look for the `('. */
7589 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7590 /* Parse the initialization. */
7591 cp_parser_for_init_statement (parser);
7592 finish_for_init_stmt (statement);
7594 /* If there's a condition, process it. */
7595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7596 condition = cp_parser_condition (parser);
7597 finish_for_cond (condition, statement);
7598 /* Look for the `;'. */
7599 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7601 /* If there's an expression, process it. */
7602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7603 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7604 finish_for_expr (expression, statement);
7605 /* Look for the `)'. */
7606 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7608 /* Parse the body of the for-statement. */
7609 parser->in_statement = IN_ITERATION_STMT;
7610 cp_parser_already_scoped_statement (parser);
7611 parser->in_statement = in_statement;
7613 /* We're done with the for-statement. */
7614 finish_for_stmt (statement);
7619 cp_parser_error (parser, "expected iteration-statement");
7620 statement = error_mark_node;
7627 /* Parse a for-init-statement.
7630 expression-statement
7631 simple-declaration */
7634 cp_parser_for_init_statement (cp_parser* parser)
7636 /* If the next token is a `;', then we have an empty
7637 expression-statement. Grammatically, this is also a
7638 simple-declaration, but an invalid one, because it does not
7639 declare anything. Therefore, if we did not handle this case
7640 specially, we would issue an error message about an invalid
7642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7644 /* We're going to speculatively look for a declaration, falling back
7645 to an expression, if necessary. */
7646 cp_parser_parse_tentatively (parser);
7647 /* Parse the declaration. */
7648 cp_parser_simple_declaration (parser,
7649 /*function_definition_allowed_p=*/false);
7650 /* If the tentative parse failed, then we shall need to look for an
7651 expression-statement. */
7652 if (cp_parser_parse_definitely (parser))
7656 cp_parser_expression_statement (parser, false);
7659 /* Parse a jump-statement.
7664 return expression [opt] ;
7665 return braced-init-list ;
7673 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7676 cp_parser_jump_statement (cp_parser* parser)
7678 tree statement = error_mark_node;
7681 unsigned char in_statement;
7683 /* Peek at the next token. */
7684 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7686 return error_mark_node;
7688 /* See what kind of keyword it is. */
7689 keyword = token->keyword;
7693 in_statement = parser->in_statement & ~IN_IF_STMT;
7694 switch (in_statement)
7697 error ("%Hbreak statement not within loop or switch", &token->location);
7700 gcc_assert ((in_statement & IN_SWITCH_STMT)
7701 || in_statement == IN_ITERATION_STMT);
7702 statement = finish_break_stmt ();
7705 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7708 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7711 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7715 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7718 error ("%Hcontinue statement not within a loop", &token->location);
7720 case IN_ITERATION_STMT:
7722 statement = finish_continue_stmt ();
7725 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7730 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7736 bool expr_non_constant_p;
7738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7740 maybe_warn_cpp0x ("extended initializer lists");
7741 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7743 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7744 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7746 /* If the next token is a `;', then there is no
7749 /* Build the return-statement. */
7750 statement = finish_return_stmt (expr);
7751 /* Look for the final `;'. */
7752 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7757 /* Create the goto-statement. */
7758 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7760 /* Issue a warning about this use of a GNU extension. */
7761 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7762 /* Consume the '*' token. */
7763 cp_lexer_consume_token (parser->lexer);
7764 /* Parse the dependent expression. */
7765 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7768 finish_goto_stmt (cp_parser_identifier (parser));
7769 /* Look for the final `;'. */
7770 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7774 cp_parser_error (parser, "expected jump-statement");
7781 /* Parse a declaration-statement.
7783 declaration-statement:
7784 block-declaration */
7787 cp_parser_declaration_statement (cp_parser* parser)
7791 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7792 p = obstack_alloc (&declarator_obstack, 0);
7794 /* Parse the block-declaration. */
7795 cp_parser_block_declaration (parser, /*statement_p=*/true);
7797 /* Free any declarators allocated. */
7798 obstack_free (&declarator_obstack, p);
7800 /* Finish off the statement. */
7804 /* Some dependent statements (like `if (cond) statement'), are
7805 implicitly in their own scope. In other words, if the statement is
7806 a single statement (as opposed to a compound-statement), it is
7807 none-the-less treated as if it were enclosed in braces. Any
7808 declarations appearing in the dependent statement are out of scope
7809 after control passes that point. This function parses a statement,
7810 but ensures that is in its own scope, even if it is not a
7813 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7814 is a (possibly labeled) if statement which is not enclosed in
7815 braces and has an else clause. This is used to implement
7818 Returns the new statement. */
7821 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7828 /* Mark if () ; with a special NOP_EXPR. */
7829 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7831 cp_lexer_consume_token (parser->lexer);
7832 statement = add_stmt (build_empty_stmt ());
7834 /* if a compound is opened, we simply parse the statement directly. */
7835 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7836 statement = cp_parser_compound_statement (parser, NULL, false);
7837 /* If the token is not a `{', then we must take special action. */
7840 /* Create a compound-statement. */
7841 statement = begin_compound_stmt (0);
7842 /* Parse the dependent-statement. */
7843 cp_parser_statement (parser, NULL_TREE, false, if_p);
7844 /* Finish the dummy compound-statement. */
7845 finish_compound_stmt (statement);
7848 /* Return the statement. */
7852 /* For some dependent statements (like `while (cond) statement'), we
7853 have already created a scope. Therefore, even if the dependent
7854 statement is a compound-statement, we do not want to create another
7858 cp_parser_already_scoped_statement (cp_parser* parser)
7860 /* If the token is a `{', then we must take special action. */
7861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7862 cp_parser_statement (parser, NULL_TREE, false, NULL);
7865 /* Avoid calling cp_parser_compound_statement, so that we
7866 don't create a new scope. Do everything else by hand. */
7867 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7868 /* If the next keyword is `__label__' we have a label declaration. */
7869 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7870 cp_parser_label_declaration (parser);
7871 /* Parse an (optional) statement-seq. */
7872 cp_parser_statement_seq_opt (parser, NULL_TREE);
7873 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7877 /* Declarations [gram.dcl.dcl] */
7879 /* Parse an optional declaration-sequence.
7883 declaration-seq declaration */
7886 cp_parser_declaration_seq_opt (cp_parser* parser)
7892 token = cp_lexer_peek_token (parser->lexer);
7894 if (token->type == CPP_CLOSE_BRACE
7895 || token->type == CPP_EOF
7896 || token->type == CPP_PRAGMA_EOL)
7899 if (token->type == CPP_SEMICOLON)
7901 /* A declaration consisting of a single semicolon is
7902 invalid. Allow it unless we're being pedantic. */
7903 cp_lexer_consume_token (parser->lexer);
7904 if (!in_system_header)
7905 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7909 /* If we're entering or exiting a region that's implicitly
7910 extern "C", modify the lang context appropriately. */
7911 if (!parser->implicit_extern_c && token->implicit_extern_c)
7913 push_lang_context (lang_name_c);
7914 parser->implicit_extern_c = true;
7916 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7918 pop_lang_context ();
7919 parser->implicit_extern_c = false;
7922 if (token->type == CPP_PRAGMA)
7924 /* A top-level declaration can consist solely of a #pragma.
7925 A nested declaration cannot, so this is done here and not
7926 in cp_parser_declaration. (A #pragma at block scope is
7927 handled in cp_parser_statement.) */
7928 cp_parser_pragma (parser, pragma_external);
7932 /* Parse the declaration itself. */
7933 cp_parser_declaration (parser);
7937 /* Parse a declaration.
7942 template-declaration
7943 explicit-instantiation
7944 explicit-specialization
7945 linkage-specification
7946 namespace-definition
7951 __extension__ declaration */
7954 cp_parser_declaration (cp_parser* parser)
7961 /* Check for the `__extension__' keyword. */
7962 if (cp_parser_extension_opt (parser, &saved_pedantic))
7964 /* Parse the qualified declaration. */
7965 cp_parser_declaration (parser);
7966 /* Restore the PEDANTIC flag. */
7967 pedantic = saved_pedantic;
7972 /* Try to figure out what kind of declaration is present. */
7973 token1 = *cp_lexer_peek_token (parser->lexer);
7975 if (token1.type != CPP_EOF)
7976 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7979 token2.type = CPP_EOF;
7980 token2.keyword = RID_MAX;
7983 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7984 p = obstack_alloc (&declarator_obstack, 0);
7986 /* If the next token is `extern' and the following token is a string
7987 literal, then we have a linkage specification. */
7988 if (token1.keyword == RID_EXTERN
7989 && cp_parser_is_string_literal (&token2))
7990 cp_parser_linkage_specification (parser);
7991 /* If the next token is `template', then we have either a template
7992 declaration, an explicit instantiation, or an explicit
7994 else if (token1.keyword == RID_TEMPLATE)
7996 /* `template <>' indicates a template specialization. */
7997 if (token2.type == CPP_LESS
7998 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7999 cp_parser_explicit_specialization (parser);
8000 /* `template <' indicates a template declaration. */
8001 else if (token2.type == CPP_LESS)
8002 cp_parser_template_declaration (parser, /*member_p=*/false);
8003 /* Anything else must be an explicit instantiation. */
8005 cp_parser_explicit_instantiation (parser);
8007 /* If the next token is `export', then we have a template
8009 else if (token1.keyword == RID_EXPORT)
8010 cp_parser_template_declaration (parser, /*member_p=*/false);
8011 /* If the next token is `extern', 'static' or 'inline' and the one
8012 after that is `template', we have a GNU extended explicit
8013 instantiation directive. */
8014 else if (cp_parser_allow_gnu_extensions_p (parser)
8015 && (token1.keyword == RID_EXTERN
8016 || token1.keyword == RID_STATIC
8017 || token1.keyword == RID_INLINE)
8018 && token2.keyword == RID_TEMPLATE)
8019 cp_parser_explicit_instantiation (parser);
8020 /* If the next token is `namespace', check for a named or unnamed
8021 namespace definition. */
8022 else if (token1.keyword == RID_NAMESPACE
8023 && (/* A named namespace definition. */
8024 (token2.type == CPP_NAME
8025 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8027 /* An unnamed namespace definition. */
8028 || token2.type == CPP_OPEN_BRACE
8029 || token2.keyword == RID_ATTRIBUTE))
8030 cp_parser_namespace_definition (parser);
8031 /* An inline (associated) namespace definition. */
8032 else if (token1.keyword == RID_INLINE
8033 && token2.keyword == RID_NAMESPACE)
8034 cp_parser_namespace_definition (parser);
8035 /* Objective-C++ declaration/definition. */
8036 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8037 cp_parser_objc_declaration (parser);
8038 /* We must have either a block declaration or a function
8041 /* Try to parse a block-declaration, or a function-definition. */
8042 cp_parser_block_declaration (parser, /*statement_p=*/false);
8044 /* Free any declarators allocated. */
8045 obstack_free (&declarator_obstack, p);
8048 /* Parse a block-declaration.
8053 namespace-alias-definition
8060 __extension__ block-declaration
8065 static_assert-declaration
8067 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8068 part of a declaration-statement. */
8071 cp_parser_block_declaration (cp_parser *parser,
8077 /* Check for the `__extension__' keyword. */
8078 if (cp_parser_extension_opt (parser, &saved_pedantic))
8080 /* Parse the qualified declaration. */
8081 cp_parser_block_declaration (parser, statement_p);
8082 /* Restore the PEDANTIC flag. */
8083 pedantic = saved_pedantic;
8088 /* Peek at the next token to figure out which kind of declaration is
8090 token1 = cp_lexer_peek_token (parser->lexer);
8092 /* If the next keyword is `asm', we have an asm-definition. */
8093 if (token1->keyword == RID_ASM)
8096 cp_parser_commit_to_tentative_parse (parser);
8097 cp_parser_asm_definition (parser);
8099 /* If the next keyword is `namespace', we have a
8100 namespace-alias-definition. */
8101 else if (token1->keyword == RID_NAMESPACE)
8102 cp_parser_namespace_alias_definition (parser);
8103 /* If the next keyword is `using', we have either a
8104 using-declaration or a using-directive. */
8105 else if (token1->keyword == RID_USING)
8110 cp_parser_commit_to_tentative_parse (parser);
8111 /* If the token after `using' is `namespace', then we have a
8113 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8114 if (token2->keyword == RID_NAMESPACE)
8115 cp_parser_using_directive (parser);
8116 /* Otherwise, it's a using-declaration. */
8118 cp_parser_using_declaration (parser,
8119 /*access_declaration_p=*/false);
8121 /* If the next keyword is `__label__' we have a misplaced label
8123 else if (token1->keyword == RID_LABEL)
8125 cp_lexer_consume_token (parser->lexer);
8126 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8127 cp_parser_skip_to_end_of_statement (parser);
8128 /* If the next token is now a `;', consume it. */
8129 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8130 cp_lexer_consume_token (parser->lexer);
8132 /* If the next token is `static_assert' we have a static assertion. */
8133 else if (token1->keyword == RID_STATIC_ASSERT)
8134 cp_parser_static_assert (parser, /*member_p=*/false);
8135 /* Anything else must be a simple-declaration. */
8137 cp_parser_simple_declaration (parser, !statement_p);
8140 /* Parse a simple-declaration.
8143 decl-specifier-seq [opt] init-declarator-list [opt] ;
8145 init-declarator-list:
8147 init-declarator-list , init-declarator
8149 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8150 function-definition as a simple-declaration. */
8153 cp_parser_simple_declaration (cp_parser* parser,
8154 bool function_definition_allowed_p)
8156 cp_decl_specifier_seq decl_specifiers;
8157 int declares_class_or_enum;
8158 bool saw_declarator;
8160 /* Defer access checks until we know what is being declared; the
8161 checks for names appearing in the decl-specifier-seq should be
8162 done as if we were in the scope of the thing being declared. */
8163 push_deferring_access_checks (dk_deferred);
8165 /* Parse the decl-specifier-seq. We have to keep track of whether
8166 or not the decl-specifier-seq declares a named class or
8167 enumeration type, since that is the only case in which the
8168 init-declarator-list is allowed to be empty.
8172 In a simple-declaration, the optional init-declarator-list can be
8173 omitted only when declaring a class or enumeration, that is when
8174 the decl-specifier-seq contains either a class-specifier, an
8175 elaborated-type-specifier, or an enum-specifier. */
8176 cp_parser_decl_specifier_seq (parser,
8177 CP_PARSER_FLAGS_OPTIONAL,
8179 &declares_class_or_enum);
8180 /* We no longer need to defer access checks. */
8181 stop_deferring_access_checks ();
8183 /* In a block scope, a valid declaration must always have a
8184 decl-specifier-seq. By not trying to parse declarators, we can
8185 resolve the declaration/expression ambiguity more quickly. */
8186 if (!function_definition_allowed_p
8187 && !decl_specifiers.any_specifiers_p)
8189 cp_parser_error (parser, "expected declaration");
8193 /* If the next two tokens are both identifiers, the code is
8194 erroneous. The usual cause of this situation is code like:
8198 where "T" should name a type -- but does not. */
8199 if (!decl_specifiers.type
8200 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8202 /* If parsing tentatively, we should commit; we really are
8203 looking at a declaration. */
8204 cp_parser_commit_to_tentative_parse (parser);
8209 /* If we have seen at least one decl-specifier, and the next token
8210 is not a parenthesis, then we must be looking at a declaration.
8211 (After "int (" we might be looking at a functional cast.) */
8212 if (decl_specifiers.any_specifiers_p
8213 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8214 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8215 && !cp_parser_error_occurred (parser))
8216 cp_parser_commit_to_tentative_parse (parser);
8218 /* Keep going until we hit the `;' at the end of the simple
8220 saw_declarator = false;
8221 while (cp_lexer_next_token_is_not (parser->lexer,
8225 bool function_definition_p;
8230 /* If we are processing next declarator, coma is expected */
8231 token = cp_lexer_peek_token (parser->lexer);
8232 gcc_assert (token->type == CPP_COMMA);
8233 cp_lexer_consume_token (parser->lexer);
8236 saw_declarator = true;
8238 /* Parse the init-declarator. */
8239 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8241 function_definition_allowed_p,
8243 declares_class_or_enum,
8244 &function_definition_p);
8245 /* If an error occurred while parsing tentatively, exit quickly.
8246 (That usually happens when in the body of a function; each
8247 statement is treated as a declaration-statement until proven
8249 if (cp_parser_error_occurred (parser))
8251 /* Handle function definitions specially. */
8252 if (function_definition_p)
8254 /* If the next token is a `,', then we are probably
8255 processing something like:
8259 which is erroneous. */
8260 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8262 cp_token *token = cp_lexer_peek_token (parser->lexer);
8263 error ("%Hmixing declarations and function-definitions is forbidden",
8266 /* Otherwise, we're done with the list of declarators. */
8269 pop_deferring_access_checks ();
8273 /* The next token should be either a `,' or a `;'. */
8274 token = cp_lexer_peek_token (parser->lexer);
8275 /* If it's a `,', there are more declarators to come. */
8276 if (token->type == CPP_COMMA)
8277 /* will be consumed next time around */;
8278 /* If it's a `;', we are done. */
8279 else if (token->type == CPP_SEMICOLON)
8281 /* Anything else is an error. */
8284 /* If we have already issued an error message we don't need
8285 to issue another one. */
8286 if (decl != error_mark_node
8287 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8288 cp_parser_error (parser, "expected %<,%> or %<;%>");
8289 /* Skip tokens until we reach the end of the statement. */
8290 cp_parser_skip_to_end_of_statement (parser);
8291 /* If the next token is now a `;', consume it. */
8292 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8293 cp_lexer_consume_token (parser->lexer);
8296 /* After the first time around, a function-definition is not
8297 allowed -- even if it was OK at first. For example:
8302 function_definition_allowed_p = false;
8305 /* Issue an error message if no declarators are present, and the
8306 decl-specifier-seq does not itself declare a class or
8308 if (!saw_declarator)
8310 if (cp_parser_declares_only_class_p (parser))
8311 shadow_tag (&decl_specifiers);
8312 /* Perform any deferred access checks. */
8313 perform_deferred_access_checks ();
8316 /* Consume the `;'. */
8317 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8320 pop_deferring_access_checks ();
8323 /* Parse a decl-specifier-seq.
8326 decl-specifier-seq [opt] decl-specifier
8329 storage-class-specifier
8340 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8342 The parser flags FLAGS is used to control type-specifier parsing.
8344 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8347 1: one of the decl-specifiers is an elaborated-type-specifier
8348 (i.e., a type declaration)
8349 2: one of the decl-specifiers is an enum-specifier or a
8350 class-specifier (i.e., a type definition)
8355 cp_parser_decl_specifier_seq (cp_parser* parser,
8356 cp_parser_flags flags,
8357 cp_decl_specifier_seq *decl_specs,
8358 int* declares_class_or_enum)
8360 bool constructor_possible_p = !parser->in_declarator_p;
8361 cp_token *start_token = NULL;
8363 /* Clear DECL_SPECS. */
8364 clear_decl_specs (decl_specs);
8366 /* Assume no class or enumeration type is declared. */
8367 *declares_class_or_enum = 0;
8369 /* Keep reading specifiers until there are no more to read. */
8373 bool found_decl_spec;
8376 /* Peek at the next token. */
8377 token = cp_lexer_peek_token (parser->lexer);
8379 /* Save the first token of the decl spec list for error
8382 start_token = token;
8383 /* Handle attributes. */
8384 if (token->keyword == RID_ATTRIBUTE)
8386 /* Parse the attributes. */
8387 decl_specs->attributes
8388 = chainon (decl_specs->attributes,
8389 cp_parser_attributes_opt (parser));
8392 /* Assume we will find a decl-specifier keyword. */
8393 found_decl_spec = true;
8394 /* If the next token is an appropriate keyword, we can simply
8395 add it to the list. */
8396 switch (token->keyword)
8401 if (!at_class_scope_p ())
8403 error ("%H%<friend%> used outside of class", &token->location);
8404 cp_lexer_purge_token (parser->lexer);
8408 ++decl_specs->specs[(int) ds_friend];
8409 /* Consume the token. */
8410 cp_lexer_consume_token (parser->lexer);
8414 /* function-specifier:
8421 cp_parser_function_specifier_opt (parser, decl_specs);
8427 ++decl_specs->specs[(int) ds_typedef];
8428 /* Consume the token. */
8429 cp_lexer_consume_token (parser->lexer);
8430 /* A constructor declarator cannot appear in a typedef. */
8431 constructor_possible_p = false;
8432 /* The "typedef" keyword can only occur in a declaration; we
8433 may as well commit at this point. */
8434 cp_parser_commit_to_tentative_parse (parser);
8436 if (decl_specs->storage_class != sc_none)
8437 decl_specs->conflicting_specifiers_p = true;
8440 /* storage-class-specifier:
8450 if (cxx_dialect == cxx98)
8452 /* Consume the token. */
8453 cp_lexer_consume_token (parser->lexer);
8455 /* Complain about `auto' as a storage specifier, if
8456 we're complaining about C++0x compatibility. */
8459 "%H%<auto%> will change meaning in C++0x; please remove it",
8462 /* Set the storage class anyway. */
8463 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8467 /* C++0x auto type-specifier. */
8468 found_decl_spec = false;
8475 /* Consume the token. */
8476 cp_lexer_consume_token (parser->lexer);
8477 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8481 /* Consume the token. */
8482 cp_lexer_consume_token (parser->lexer);
8483 ++decl_specs->specs[(int) ds_thread];
8487 /* We did not yet find a decl-specifier yet. */
8488 found_decl_spec = false;
8492 /* Constructors are a special case. The `S' in `S()' is not a
8493 decl-specifier; it is the beginning of the declarator. */
8496 && constructor_possible_p
8497 && (cp_parser_constructor_declarator_p
8498 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8500 /* If we don't have a DECL_SPEC yet, then we must be looking at
8501 a type-specifier. */
8502 if (!found_decl_spec && !constructor_p)
8504 int decl_spec_declares_class_or_enum;
8505 bool is_cv_qualifier;
8509 = cp_parser_type_specifier (parser, flags,
8511 /*is_declaration=*/true,
8512 &decl_spec_declares_class_or_enum,
8514 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8516 /* If this type-specifier referenced a user-defined type
8517 (a typedef, class-name, etc.), then we can't allow any
8518 more such type-specifiers henceforth.
8522 The longest sequence of decl-specifiers that could
8523 possibly be a type name is taken as the
8524 decl-specifier-seq of a declaration. The sequence shall
8525 be self-consistent as described below.
8529 As a general rule, at most one type-specifier is allowed
8530 in the complete decl-specifier-seq of a declaration. The
8531 only exceptions are the following:
8533 -- const or volatile can be combined with any other
8536 -- signed or unsigned can be combined with char, long,
8544 void g (const int Pc);
8546 Here, Pc is *not* part of the decl-specifier seq; it's
8547 the declarator. Therefore, once we see a type-specifier
8548 (other than a cv-qualifier), we forbid any additional
8549 user-defined types. We *do* still allow things like `int
8550 int' to be considered a decl-specifier-seq, and issue the
8551 error message later. */
8552 if (type_spec && !is_cv_qualifier)
8553 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8554 /* A constructor declarator cannot follow a type-specifier. */
8557 constructor_possible_p = false;
8558 found_decl_spec = true;
8562 /* If we still do not have a DECL_SPEC, then there are no more
8564 if (!found_decl_spec)
8567 decl_specs->any_specifiers_p = true;
8568 /* After we see one decl-specifier, further decl-specifiers are
8570 flags |= CP_PARSER_FLAGS_OPTIONAL;
8573 cp_parser_check_decl_spec (decl_specs, start_token->location);
8575 /* Don't allow a friend specifier with a class definition. */
8576 if (decl_specs->specs[(int) ds_friend] != 0
8577 && (*declares_class_or_enum & 2))
8578 error ("%Hclass definition may not be declared a friend",
8579 &start_token->location);
8582 /* Parse an (optional) storage-class-specifier.
8584 storage-class-specifier:
8593 storage-class-specifier:
8596 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8599 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8601 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8604 if (cxx_dialect != cxx98)
8606 /* Fall through for C++98. */
8613 /* Consume the token. */
8614 return cp_lexer_consume_token (parser->lexer)->u.value;
8621 /* Parse an (optional) function-specifier.
8628 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8629 Updates DECL_SPECS, if it is non-NULL. */
8632 cp_parser_function_specifier_opt (cp_parser* parser,
8633 cp_decl_specifier_seq *decl_specs)
8635 cp_token *token = cp_lexer_peek_token (parser->lexer);
8636 switch (token->keyword)
8640 ++decl_specs->specs[(int) ds_inline];
8644 /* 14.5.2.3 [temp.mem]
8646 A member function template shall not be virtual. */
8647 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8648 error ("%Htemplates may not be %<virtual%>", &token->location);
8649 else if (decl_specs)
8650 ++decl_specs->specs[(int) ds_virtual];
8655 ++decl_specs->specs[(int) ds_explicit];
8662 /* Consume the token. */
8663 return cp_lexer_consume_token (parser->lexer)->u.value;
8666 /* Parse a linkage-specification.
8668 linkage-specification:
8669 extern string-literal { declaration-seq [opt] }
8670 extern string-literal declaration */
8673 cp_parser_linkage_specification (cp_parser* parser)
8677 /* Look for the `extern' keyword. */
8678 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8680 /* Look for the string-literal. */
8681 linkage = cp_parser_string_literal (parser, false, false);
8683 /* Transform the literal into an identifier. If the literal is a
8684 wide-character string, or contains embedded NULs, then we can't
8685 handle it as the user wants. */
8686 if (strlen (TREE_STRING_POINTER (linkage))
8687 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8689 cp_parser_error (parser, "invalid linkage-specification");
8690 /* Assume C++ linkage. */
8691 linkage = lang_name_cplusplus;
8694 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8696 /* We're now using the new linkage. */
8697 push_lang_context (linkage);
8699 /* If the next token is a `{', then we're using the first
8701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8703 /* Consume the `{' token. */
8704 cp_lexer_consume_token (parser->lexer);
8705 /* Parse the declarations. */
8706 cp_parser_declaration_seq_opt (parser);
8707 /* Look for the closing `}'. */
8708 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8710 /* Otherwise, there's just one declaration. */
8713 bool saved_in_unbraced_linkage_specification_p;
8715 saved_in_unbraced_linkage_specification_p
8716 = parser->in_unbraced_linkage_specification_p;
8717 parser->in_unbraced_linkage_specification_p = true;
8718 cp_parser_declaration (parser);
8719 parser->in_unbraced_linkage_specification_p
8720 = saved_in_unbraced_linkage_specification_p;
8723 /* We're done with the linkage-specification. */
8724 pop_lang_context ();
8727 /* Parse a static_assert-declaration.
8729 static_assert-declaration:
8730 static_assert ( constant-expression , string-literal ) ;
8732 If MEMBER_P, this static_assert is a class member. */
8735 cp_parser_static_assert(cp_parser *parser, bool member_p)
8740 location_t saved_loc;
8742 /* Peek at the `static_assert' token so we can keep track of exactly
8743 where the static assertion started. */
8744 token = cp_lexer_peek_token (parser->lexer);
8745 saved_loc = token->location;
8747 /* Look for the `static_assert' keyword. */
8748 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8749 "%<static_assert%>"))
8752 /* We know we are in a static assertion; commit to any tentative
8754 if (cp_parser_parsing_tentatively (parser))
8755 cp_parser_commit_to_tentative_parse (parser);
8757 /* Parse the `(' starting the static assertion condition. */
8758 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8760 /* Parse the constant-expression. */
8762 cp_parser_constant_expression (parser,
8763 /*allow_non_constant_p=*/false,
8764 /*non_constant_p=*/NULL);
8766 /* Parse the separating `,'. */
8767 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8769 /* Parse the string-literal message. */
8770 message = cp_parser_string_literal (parser,
8771 /*translate=*/false,
8774 /* A `)' completes the static assertion. */
8775 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8776 cp_parser_skip_to_closing_parenthesis (parser,
8777 /*recovering=*/true,
8779 /*consume_paren=*/true);
8781 /* A semicolon terminates the declaration. */
8782 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8784 /* Complete the static assertion, which may mean either processing
8785 the static assert now or saving it for template instantiation. */
8786 finish_static_assert (condition, message, saved_loc, member_p);
8789 /* Parse a `decltype' type. Returns the type.
8791 simple-type-specifier:
8792 decltype ( expression ) */
8795 cp_parser_decltype (cp_parser *parser)
8798 bool id_expression_or_member_access_p = false;
8799 const char *saved_message;
8800 bool saved_integral_constant_expression_p;
8801 bool saved_non_integral_constant_expression_p;
8802 cp_token *id_expr_start_token;
8804 /* Look for the `decltype' token. */
8805 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8806 return error_mark_node;
8808 /* Types cannot be defined in a `decltype' expression. Save away the
8810 saved_message = parser->type_definition_forbidden_message;
8812 /* And create the new one. */
8813 parser->type_definition_forbidden_message
8814 = "types may not be defined in %<decltype%> expressions";
8816 /* The restrictions on constant-expressions do not apply inside
8817 decltype expressions. */
8818 saved_integral_constant_expression_p
8819 = parser->integral_constant_expression_p;
8820 saved_non_integral_constant_expression_p
8821 = parser->non_integral_constant_expression_p;
8822 parser->integral_constant_expression_p = false;
8824 /* Do not actually evaluate the expression. */
8827 /* Parse the opening `('. */
8828 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8829 return error_mark_node;
8831 /* First, try parsing an id-expression. */
8832 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8833 cp_parser_parse_tentatively (parser);
8834 expr = cp_parser_id_expression (parser,
8835 /*template_keyword_p=*/false,
8836 /*check_dependency_p=*/true,
8837 /*template_p=*/NULL,
8838 /*declarator_p=*/false,
8839 /*optional_p=*/false);
8841 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8843 bool non_integral_constant_expression_p = false;
8844 tree id_expression = expr;
8846 const char *error_msg;
8848 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8849 /* Lookup the name we got back from the id-expression. */
8850 expr = cp_parser_lookup_name (parser, expr,
8852 /*is_template=*/false,
8853 /*is_namespace=*/false,
8854 /*check_dependency=*/true,
8855 /*ambiguous_decls=*/NULL,
8856 id_expr_start_token->location);
8859 && expr != error_mark_node
8860 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8861 && TREE_CODE (expr) != TYPE_DECL
8862 && (TREE_CODE (expr) != BIT_NOT_EXPR
8863 || !TYPE_P (TREE_OPERAND (expr, 0)))
8864 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8866 /* Complete lookup of the id-expression. */
8867 expr = (finish_id_expression
8868 (id_expression, expr, parser->scope, &idk,
8869 /*integral_constant_expression_p=*/false,
8870 /*allow_non_integral_constant_expression_p=*/true,
8871 &non_integral_constant_expression_p,
8872 /*template_p=*/false,
8874 /*address_p=*/false,
8875 /*template_arg_p=*/false,
8877 id_expr_start_token->location));
8879 if (expr == error_mark_node)
8880 /* We found an id-expression, but it was something that we
8881 should not have found. This is an error, not something
8882 we can recover from, so note that we found an
8883 id-expression and we'll recover as gracefully as
8885 id_expression_or_member_access_p = true;
8889 && expr != error_mark_node
8890 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8891 /* We have an id-expression. */
8892 id_expression_or_member_access_p = true;
8895 if (!id_expression_or_member_access_p)
8897 /* Abort the id-expression parse. */
8898 cp_parser_abort_tentative_parse (parser);
8900 /* Parsing tentatively, again. */
8901 cp_parser_parse_tentatively (parser);
8903 /* Parse a class member access. */
8904 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8906 /*member_access_only_p=*/true, NULL);
8909 && expr != error_mark_node
8910 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8911 /* We have an id-expression. */
8912 id_expression_or_member_access_p = true;
8915 if (id_expression_or_member_access_p)
8916 /* We have parsed the complete id-expression or member access. */
8917 cp_parser_parse_definitely (parser);
8920 /* Abort our attempt to parse an id-expression or member access
8922 cp_parser_abort_tentative_parse (parser);
8924 /* Parse a full expression. */
8925 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8928 /* Go back to evaluating expressions. */
8931 /* Restore the old message and the integral constant expression
8933 parser->type_definition_forbidden_message = saved_message;
8934 parser->integral_constant_expression_p
8935 = saved_integral_constant_expression_p;
8936 parser->non_integral_constant_expression_p
8937 = saved_non_integral_constant_expression_p;
8939 if (expr == error_mark_node)
8941 /* Skip everything up to the closing `)'. */
8942 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8943 /*consume_paren=*/true);
8944 return error_mark_node;
8947 /* Parse to the closing `)'. */
8948 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8950 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8951 /*consume_paren=*/true);
8952 return error_mark_node;
8955 return finish_decltype_type (expr, id_expression_or_member_access_p);
8958 /* Special member functions [gram.special] */
8960 /* Parse a conversion-function-id.
8962 conversion-function-id:
8963 operator conversion-type-id
8965 Returns an IDENTIFIER_NODE representing the operator. */
8968 cp_parser_conversion_function_id (cp_parser* parser)
8972 tree saved_qualifying_scope;
8973 tree saved_object_scope;
8974 tree pushed_scope = NULL_TREE;
8976 /* Look for the `operator' token. */
8977 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8978 return error_mark_node;
8979 /* When we parse the conversion-type-id, the current scope will be
8980 reset. However, we need that information in able to look up the
8981 conversion function later, so we save it here. */
8982 saved_scope = parser->scope;
8983 saved_qualifying_scope = parser->qualifying_scope;
8984 saved_object_scope = parser->object_scope;
8985 /* We must enter the scope of the class so that the names of
8986 entities declared within the class are available in the
8987 conversion-type-id. For example, consider:
8994 S::operator I() { ... }
8996 In order to see that `I' is a type-name in the definition, we
8997 must be in the scope of `S'. */
8999 pushed_scope = push_scope (saved_scope);
9000 /* Parse the conversion-type-id. */
9001 type = cp_parser_conversion_type_id (parser);
9002 /* Leave the scope of the class, if any. */
9004 pop_scope (pushed_scope);
9005 /* Restore the saved scope. */
9006 parser->scope = saved_scope;
9007 parser->qualifying_scope = saved_qualifying_scope;
9008 parser->object_scope = saved_object_scope;
9009 /* If the TYPE is invalid, indicate failure. */
9010 if (type == error_mark_node)
9011 return error_mark_node;
9012 return mangle_conv_op_name_for_type (type);
9015 /* Parse a conversion-type-id:
9018 type-specifier-seq conversion-declarator [opt]
9020 Returns the TYPE specified. */
9023 cp_parser_conversion_type_id (cp_parser* parser)
9026 cp_decl_specifier_seq type_specifiers;
9027 cp_declarator *declarator;
9028 tree type_specified;
9030 /* Parse the attributes. */
9031 attributes = cp_parser_attributes_opt (parser);
9032 /* Parse the type-specifiers. */
9033 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9035 /* If that didn't work, stop. */
9036 if (type_specifiers.type == error_mark_node)
9037 return error_mark_node;
9038 /* Parse the conversion-declarator. */
9039 declarator = cp_parser_conversion_declarator_opt (parser);
9041 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9042 /*initialized=*/0, &attributes);
9044 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9046 /* Don't give this error when parsing tentatively. This happens to
9047 work because we always parse this definitively once. */
9048 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9049 && type_uses_auto (type_specified))
9051 error ("invalid use of %<auto%> in conversion operator");
9052 return error_mark_node;
9055 return type_specified;
9058 /* Parse an (optional) conversion-declarator.
9060 conversion-declarator:
9061 ptr-operator conversion-declarator [opt]
9065 static cp_declarator *
9066 cp_parser_conversion_declarator_opt (cp_parser* parser)
9068 enum tree_code code;
9070 cp_cv_quals cv_quals;
9072 /* We don't know if there's a ptr-operator next, or not. */
9073 cp_parser_parse_tentatively (parser);
9074 /* Try the ptr-operator. */
9075 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9076 /* If it worked, look for more conversion-declarators. */
9077 if (cp_parser_parse_definitely (parser))
9079 cp_declarator *declarator;
9081 /* Parse another optional declarator. */
9082 declarator = cp_parser_conversion_declarator_opt (parser);
9084 return cp_parser_make_indirect_declarator
9085 (code, class_type, cv_quals, declarator);
9091 /* Parse an (optional) ctor-initializer.
9094 : mem-initializer-list
9096 Returns TRUE iff the ctor-initializer was actually present. */
9099 cp_parser_ctor_initializer_opt (cp_parser* parser)
9101 /* If the next token is not a `:', then there is no
9102 ctor-initializer. */
9103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9105 /* Do default initialization of any bases and members. */
9106 if (DECL_CONSTRUCTOR_P (current_function_decl))
9107 finish_mem_initializers (NULL_TREE);
9112 /* Consume the `:' token. */
9113 cp_lexer_consume_token (parser->lexer);
9114 /* And the mem-initializer-list. */
9115 cp_parser_mem_initializer_list (parser);
9120 /* Parse a mem-initializer-list.
9122 mem-initializer-list:
9123 mem-initializer ... [opt]
9124 mem-initializer ... [opt] , mem-initializer-list */
9127 cp_parser_mem_initializer_list (cp_parser* parser)
9129 tree mem_initializer_list = NULL_TREE;
9130 cp_token *token = cp_lexer_peek_token (parser->lexer);
9132 /* Let the semantic analysis code know that we are starting the
9133 mem-initializer-list. */
9134 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9135 error ("%Honly constructors take base initializers",
9138 /* Loop through the list. */
9141 tree mem_initializer;
9143 token = cp_lexer_peek_token (parser->lexer);
9144 /* Parse the mem-initializer. */
9145 mem_initializer = cp_parser_mem_initializer (parser);
9146 /* If the next token is a `...', we're expanding member initializers. */
9147 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9149 /* Consume the `...'. */
9150 cp_lexer_consume_token (parser->lexer);
9152 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9153 can be expanded but members cannot. */
9154 if (mem_initializer != error_mark_node
9155 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9157 error ("%Hcannot expand initializer for member %<%D%>",
9158 &token->location, TREE_PURPOSE (mem_initializer));
9159 mem_initializer = error_mark_node;
9162 /* Construct the pack expansion type. */
9163 if (mem_initializer != error_mark_node)
9164 mem_initializer = make_pack_expansion (mem_initializer);
9166 /* Add it to the list, unless it was erroneous. */
9167 if (mem_initializer != error_mark_node)
9169 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9170 mem_initializer_list = mem_initializer;
9172 /* If the next token is not a `,', we're done. */
9173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9175 /* Consume the `,' token. */
9176 cp_lexer_consume_token (parser->lexer);
9179 /* Perform semantic analysis. */
9180 if (DECL_CONSTRUCTOR_P (current_function_decl))
9181 finish_mem_initializers (mem_initializer_list);
9184 /* Parse a mem-initializer.
9187 mem-initializer-id ( expression-list [opt] )
9188 mem-initializer-id braced-init-list
9193 ( expression-list [opt] )
9195 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9196 class) or FIELD_DECL (for a non-static data member) to initialize;
9197 the TREE_VALUE is the expression-list. An empty initialization
9198 list is represented by void_list_node. */
9201 cp_parser_mem_initializer (cp_parser* parser)
9203 tree mem_initializer_id;
9204 tree expression_list;
9206 cp_token *token = cp_lexer_peek_token (parser->lexer);
9208 /* Find out what is being initialized. */
9209 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9211 permerror (token->location,
9212 "anachronistic old-style base class initializer");
9213 mem_initializer_id = NULL_TREE;
9217 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9218 if (mem_initializer_id == error_mark_node)
9219 return mem_initializer_id;
9221 member = expand_member_init (mem_initializer_id);
9222 if (member && !DECL_P (member))
9223 in_base_initializer = 1;
9225 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9227 bool expr_non_constant_p;
9228 maybe_warn_cpp0x ("extended initializer lists");
9229 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9230 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9231 expression_list = build_tree_list (NULL_TREE, expression_list);
9235 = cp_parser_parenthesized_expression_list (parser, false,
9237 /*allow_expansion_p=*/true,
9238 /*non_constant_p=*/NULL);
9239 if (expression_list == error_mark_node)
9240 return error_mark_node;
9241 if (!expression_list)
9242 expression_list = void_type_node;
9244 in_base_initializer = 0;
9246 return member ? build_tree_list (member, expression_list) : error_mark_node;
9249 /* Parse a mem-initializer-id.
9252 :: [opt] nested-name-specifier [opt] class-name
9255 Returns a TYPE indicating the class to be initializer for the first
9256 production. Returns an IDENTIFIER_NODE indicating the data member
9257 to be initialized for the second production. */
9260 cp_parser_mem_initializer_id (cp_parser* parser)
9262 bool global_scope_p;
9263 bool nested_name_specifier_p;
9264 bool template_p = false;
9267 cp_token *token = cp_lexer_peek_token (parser->lexer);
9269 /* `typename' is not allowed in this context ([temp.res]). */
9270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9272 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9273 "member initializer is implicitly a type)",
9275 cp_lexer_consume_token (parser->lexer);
9277 /* Look for the optional `::' operator. */
9279 = (cp_parser_global_scope_opt (parser,
9280 /*current_scope_valid_p=*/false)
9282 /* Look for the optional nested-name-specifier. The simplest way to
9287 The keyword `typename' is not permitted in a base-specifier or
9288 mem-initializer; in these contexts a qualified name that
9289 depends on a template-parameter is implicitly assumed to be a
9292 is to assume that we have seen the `typename' keyword at this
9294 nested_name_specifier_p
9295 = (cp_parser_nested_name_specifier_opt (parser,
9296 /*typename_keyword_p=*/true,
9297 /*check_dependency_p=*/true,
9299 /*is_declaration=*/true)
9301 if (nested_name_specifier_p)
9302 template_p = cp_parser_optional_template_keyword (parser);
9303 /* If there is a `::' operator or a nested-name-specifier, then we
9304 are definitely looking for a class-name. */
9305 if (global_scope_p || nested_name_specifier_p)
9306 return cp_parser_class_name (parser,
9307 /*typename_keyword_p=*/true,
9308 /*template_keyword_p=*/template_p,
9310 /*check_dependency_p=*/true,
9311 /*class_head_p=*/false,
9312 /*is_declaration=*/true);
9313 /* Otherwise, we could also be looking for an ordinary identifier. */
9314 cp_parser_parse_tentatively (parser);
9315 /* Try a class-name. */
9316 id = cp_parser_class_name (parser,
9317 /*typename_keyword_p=*/true,
9318 /*template_keyword_p=*/false,
9320 /*check_dependency_p=*/true,
9321 /*class_head_p=*/false,
9322 /*is_declaration=*/true);
9323 /* If we found one, we're done. */
9324 if (cp_parser_parse_definitely (parser))
9326 /* Otherwise, look for an ordinary identifier. */
9327 return cp_parser_identifier (parser);
9330 /* Overloading [gram.over] */
9332 /* Parse an operator-function-id.
9334 operator-function-id:
9337 Returns an IDENTIFIER_NODE for the operator which is a
9338 human-readable spelling of the identifier, e.g., `operator +'. */
9341 cp_parser_operator_function_id (cp_parser* parser)
9343 /* Look for the `operator' keyword. */
9344 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9345 return error_mark_node;
9346 /* And then the name of the operator itself. */
9347 return cp_parser_operator (parser);
9350 /* Parse an operator.
9353 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9354 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9355 || ++ -- , ->* -> () []
9362 Returns an IDENTIFIER_NODE for the operator which is a
9363 human-readable spelling of the identifier, e.g., `operator +'. */
9366 cp_parser_operator (cp_parser* parser)
9368 tree id = NULL_TREE;
9371 /* Peek at the next token. */
9372 token = cp_lexer_peek_token (parser->lexer);
9373 /* Figure out which operator we have. */
9374 switch (token->type)
9380 /* The keyword should be either `new' or `delete'. */
9381 if (token->keyword == RID_NEW)
9383 else if (token->keyword == RID_DELETE)
9388 /* Consume the `new' or `delete' token. */
9389 cp_lexer_consume_token (parser->lexer);
9391 /* Peek at the next token. */
9392 token = cp_lexer_peek_token (parser->lexer);
9393 /* If it's a `[' token then this is the array variant of the
9395 if (token->type == CPP_OPEN_SQUARE)
9397 /* Consume the `[' token. */
9398 cp_lexer_consume_token (parser->lexer);
9399 /* Look for the `]' token. */
9400 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9401 id = ansi_opname (op == NEW_EXPR
9402 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9404 /* Otherwise, we have the non-array variant. */
9406 id = ansi_opname (op);
9412 id = ansi_opname (PLUS_EXPR);
9416 id = ansi_opname (MINUS_EXPR);
9420 id = ansi_opname (MULT_EXPR);
9424 id = ansi_opname (TRUNC_DIV_EXPR);
9428 id = ansi_opname (TRUNC_MOD_EXPR);
9432 id = ansi_opname (BIT_XOR_EXPR);
9436 id = ansi_opname (BIT_AND_EXPR);
9440 id = ansi_opname (BIT_IOR_EXPR);
9444 id = ansi_opname (BIT_NOT_EXPR);
9448 id = ansi_opname (TRUTH_NOT_EXPR);
9452 id = ansi_assopname (NOP_EXPR);
9456 id = ansi_opname (LT_EXPR);
9460 id = ansi_opname (GT_EXPR);
9464 id = ansi_assopname (PLUS_EXPR);
9468 id = ansi_assopname (MINUS_EXPR);
9472 id = ansi_assopname (MULT_EXPR);
9476 id = ansi_assopname (TRUNC_DIV_EXPR);
9480 id = ansi_assopname (TRUNC_MOD_EXPR);
9484 id = ansi_assopname (BIT_XOR_EXPR);
9488 id = ansi_assopname (BIT_AND_EXPR);
9492 id = ansi_assopname (BIT_IOR_EXPR);
9496 id = ansi_opname (LSHIFT_EXPR);
9500 id = ansi_opname (RSHIFT_EXPR);
9504 id = ansi_assopname (LSHIFT_EXPR);
9508 id = ansi_assopname (RSHIFT_EXPR);
9512 id = ansi_opname (EQ_EXPR);
9516 id = ansi_opname (NE_EXPR);
9520 id = ansi_opname (LE_EXPR);
9523 case CPP_GREATER_EQ:
9524 id = ansi_opname (GE_EXPR);
9528 id = ansi_opname (TRUTH_ANDIF_EXPR);
9532 id = ansi_opname (TRUTH_ORIF_EXPR);
9536 id = ansi_opname (POSTINCREMENT_EXPR);
9539 case CPP_MINUS_MINUS:
9540 id = ansi_opname (PREDECREMENT_EXPR);
9544 id = ansi_opname (COMPOUND_EXPR);
9547 case CPP_DEREF_STAR:
9548 id = ansi_opname (MEMBER_REF);
9552 id = ansi_opname (COMPONENT_REF);
9555 case CPP_OPEN_PAREN:
9556 /* Consume the `('. */
9557 cp_lexer_consume_token (parser->lexer);
9558 /* Look for the matching `)'. */
9559 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9560 return ansi_opname (CALL_EXPR);
9562 case CPP_OPEN_SQUARE:
9563 /* Consume the `['. */
9564 cp_lexer_consume_token (parser->lexer);
9565 /* Look for the matching `]'. */
9566 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9567 return ansi_opname (ARRAY_REF);
9570 /* Anything else is an error. */
9574 /* If we have selected an identifier, we need to consume the
9577 cp_lexer_consume_token (parser->lexer);
9578 /* Otherwise, no valid operator name was present. */
9581 cp_parser_error (parser, "expected operator");
9582 id = error_mark_node;
9588 /* Parse a template-declaration.
9590 template-declaration:
9591 export [opt] template < template-parameter-list > declaration
9593 If MEMBER_P is TRUE, this template-declaration occurs within a
9596 The grammar rule given by the standard isn't correct. What
9599 template-declaration:
9600 export [opt] template-parameter-list-seq
9601 decl-specifier-seq [opt] init-declarator [opt] ;
9602 export [opt] template-parameter-list-seq
9605 template-parameter-list-seq:
9606 template-parameter-list-seq [opt]
9607 template < template-parameter-list > */
9610 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9612 /* Check for `export'. */
9613 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9615 /* Consume the `export' token. */
9616 cp_lexer_consume_token (parser->lexer);
9617 /* Warn that we do not support `export'. */
9618 warning (0, "keyword %<export%> not implemented, and will be ignored");
9621 cp_parser_template_declaration_after_export (parser, member_p);
9624 /* Parse a template-parameter-list.
9626 template-parameter-list:
9628 template-parameter-list , template-parameter
9630 Returns a TREE_LIST. Each node represents a template parameter.
9631 The nodes are connected via their TREE_CHAINs. */
9634 cp_parser_template_parameter_list (cp_parser* parser)
9636 tree parameter_list = NULL_TREE;
9638 begin_template_parm_list ();
9643 bool is_parameter_pack;
9645 /* Parse the template-parameter. */
9646 parameter = cp_parser_template_parameter (parser,
9648 &is_parameter_pack);
9649 /* Add it to the list. */
9650 if (parameter != error_mark_node)
9651 parameter_list = process_template_parm (parameter_list,
9657 tree err_parm = build_tree_list (parameter, parameter);
9658 TREE_VALUE (err_parm) = error_mark_node;
9659 parameter_list = chainon (parameter_list, err_parm);
9662 /* If the next token is not a `,', we're done. */
9663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9665 /* Otherwise, consume the `,' token. */
9666 cp_lexer_consume_token (parser->lexer);
9669 return end_template_parm_list (parameter_list);
9672 /* Parse a template-parameter.
9676 parameter-declaration
9678 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9679 the parameter. The TREE_PURPOSE is the default value, if any.
9680 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9681 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9682 set to true iff this parameter is a parameter pack. */
9685 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9686 bool *is_parameter_pack)
9689 cp_parameter_declarator *parameter_declarator;
9690 cp_declarator *id_declarator;
9693 /* Assume it is a type parameter or a template parameter. */
9694 *is_non_type = false;
9695 /* Assume it not a parameter pack. */
9696 *is_parameter_pack = false;
9697 /* Peek at the next token. */
9698 token = cp_lexer_peek_token (parser->lexer);
9699 /* If it is `class' or `template', we have a type-parameter. */
9700 if (token->keyword == RID_TEMPLATE)
9701 return cp_parser_type_parameter (parser, is_parameter_pack);
9702 /* If it is `class' or `typename' we do not know yet whether it is a
9703 type parameter or a non-type parameter. Consider:
9705 template <typename T, typename T::X X> ...
9709 template <class C, class D*> ...
9711 Here, the first parameter is a type parameter, and the second is
9712 a non-type parameter. We can tell by looking at the token after
9713 the identifier -- if it is a `,', `=', or `>' then we have a type
9715 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9717 /* Peek at the token after `class' or `typename'. */
9718 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9719 /* If it's an ellipsis, we have a template type parameter
9721 if (token->type == CPP_ELLIPSIS)
9722 return cp_parser_type_parameter (parser, is_parameter_pack);
9723 /* If it's an identifier, skip it. */
9724 if (token->type == CPP_NAME)
9725 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9726 /* Now, see if the token looks like the end of a template
9728 if (token->type == CPP_COMMA
9729 || token->type == CPP_EQ
9730 || token->type == CPP_GREATER)
9731 return cp_parser_type_parameter (parser, is_parameter_pack);
9734 /* Otherwise, it is a non-type parameter.
9738 When parsing a default template-argument for a non-type
9739 template-parameter, the first non-nested `>' is taken as the end
9740 of the template parameter-list rather than a greater-than
9742 *is_non_type = true;
9743 parameter_declarator
9744 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9745 /*parenthesized_p=*/NULL);
9747 /* If the parameter declaration is marked as a parameter pack, set
9748 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9749 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9751 if (parameter_declarator
9752 && parameter_declarator->declarator
9753 && parameter_declarator->declarator->parameter_pack_p)
9755 *is_parameter_pack = true;
9756 parameter_declarator->declarator->parameter_pack_p = false;
9759 /* If the next token is an ellipsis, and we don't already have it
9760 marked as a parameter pack, then we have a parameter pack (that
9761 has no declarator). */
9762 if (!*is_parameter_pack
9763 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9764 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9766 /* Consume the `...'. */
9767 cp_lexer_consume_token (parser->lexer);
9768 maybe_warn_variadic_templates ();
9770 *is_parameter_pack = true;
9772 /* We might end up with a pack expansion as the type of the non-type
9773 template parameter, in which case this is a non-type template
9775 else if (parameter_declarator
9776 && parameter_declarator->decl_specifiers.type
9777 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9779 *is_parameter_pack = true;
9780 parameter_declarator->decl_specifiers.type =
9781 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9784 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9786 /* Parameter packs cannot have default arguments. However, a
9787 user may try to do so, so we'll parse them and give an
9788 appropriate diagnostic here. */
9790 /* Consume the `='. */
9791 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9792 cp_lexer_consume_token (parser->lexer);
9794 /* Find the name of the parameter pack. */
9795 id_declarator = parameter_declarator->declarator;
9796 while (id_declarator && id_declarator->kind != cdk_id)
9797 id_declarator = id_declarator->declarator;
9799 if (id_declarator && id_declarator->kind == cdk_id)
9800 error ("%Htemplate parameter pack %qD cannot have a default argument",
9801 &start_token->location, id_declarator->u.id.unqualified_name);
9803 error ("%Htemplate parameter pack cannot have a default argument",
9804 &start_token->location);
9806 /* Parse the default argument, but throw away the result. */
9807 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9810 parm = grokdeclarator (parameter_declarator->declarator,
9811 ¶meter_declarator->decl_specifiers,
9812 PARM, /*initialized=*/0,
9814 if (parm == error_mark_node)
9815 return error_mark_node;
9817 return build_tree_list (parameter_declarator->default_argument, parm);
9820 /* Parse a type-parameter.
9823 class identifier [opt]
9824 class identifier [opt] = type-id
9825 typename identifier [opt]
9826 typename identifier [opt] = type-id
9827 template < template-parameter-list > class identifier [opt]
9828 template < template-parameter-list > class identifier [opt]
9831 GNU Extension (variadic templates):
9834 class ... identifier [opt]
9835 typename ... identifier [opt]
9837 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9838 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9839 the declaration of the parameter.
9841 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9844 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9849 /* Look for a keyword to tell us what kind of parameter this is. */
9850 token = cp_parser_require (parser, CPP_KEYWORD,
9851 "%<class%>, %<typename%>, or %<template%>");
9853 return error_mark_node;
9855 switch (token->keyword)
9861 tree default_argument;
9863 /* If the next token is an ellipsis, we have a template
9865 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9867 /* Consume the `...' token. */
9868 cp_lexer_consume_token (parser->lexer);
9869 maybe_warn_variadic_templates ();
9871 *is_parameter_pack = true;
9874 /* If the next token is an identifier, then it names the
9876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9877 identifier = cp_parser_identifier (parser);
9879 identifier = NULL_TREE;
9881 /* Create the parameter. */
9882 parameter = finish_template_type_parm (class_type_node, identifier);
9884 /* If the next token is an `=', we have a default argument. */
9885 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9887 /* Consume the `=' token. */
9888 cp_lexer_consume_token (parser->lexer);
9889 /* Parse the default-argument. */
9890 push_deferring_access_checks (dk_no_deferred);
9891 default_argument = cp_parser_type_id (parser);
9893 /* Template parameter packs cannot have default
9895 if (*is_parameter_pack)
9898 error ("%Htemplate parameter pack %qD cannot have a "
9899 "default argument", &token->location, identifier);
9901 error ("%Htemplate parameter packs cannot have "
9902 "default arguments", &token->location);
9903 default_argument = NULL_TREE;
9905 pop_deferring_access_checks ();
9908 default_argument = NULL_TREE;
9910 /* Create the combined representation of the parameter and the
9911 default argument. */
9912 parameter = build_tree_list (default_argument, parameter);
9918 tree parameter_list;
9920 tree default_argument;
9922 /* Look for the `<'. */
9923 cp_parser_require (parser, CPP_LESS, "%<<%>");
9924 /* Parse the template-parameter-list. */
9925 parameter_list = cp_parser_template_parameter_list (parser);
9926 /* Look for the `>'. */
9927 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9928 /* Look for the `class' keyword. */
9929 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9930 /* If the next token is an ellipsis, we have a template
9932 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9934 /* Consume the `...' token. */
9935 cp_lexer_consume_token (parser->lexer);
9936 maybe_warn_variadic_templates ();
9938 *is_parameter_pack = true;
9940 /* If the next token is an `=', then there is a
9941 default-argument. If the next token is a `>', we are at
9942 the end of the parameter-list. If the next token is a `,',
9943 then we are at the end of this parameter. */
9944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9945 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9946 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9948 identifier = cp_parser_identifier (parser);
9949 /* Treat invalid names as if the parameter were nameless. */
9950 if (identifier == error_mark_node)
9951 identifier = NULL_TREE;
9954 identifier = NULL_TREE;
9956 /* Create the template parameter. */
9957 parameter = finish_template_template_parm (class_type_node,
9960 /* If the next token is an `=', then there is a
9961 default-argument. */
9962 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9966 /* Consume the `='. */
9967 cp_lexer_consume_token (parser->lexer);
9968 /* Parse the id-expression. */
9969 push_deferring_access_checks (dk_no_deferred);
9970 /* save token before parsing the id-expression, for error
9972 token = cp_lexer_peek_token (parser->lexer);
9974 = cp_parser_id_expression (parser,
9975 /*template_keyword_p=*/false,
9976 /*check_dependency_p=*/true,
9977 /*template_p=*/&is_template,
9978 /*declarator_p=*/false,
9979 /*optional_p=*/false);
9980 if (TREE_CODE (default_argument) == TYPE_DECL)
9981 /* If the id-expression was a template-id that refers to
9982 a template-class, we already have the declaration here,
9983 so no further lookup is needed. */
9986 /* Look up the name. */
9988 = cp_parser_lookup_name (parser, default_argument,
9990 /*is_template=*/is_template,
9991 /*is_namespace=*/false,
9992 /*check_dependency=*/true,
9993 /*ambiguous_decls=*/NULL,
9995 /* See if the default argument is valid. */
9997 = check_template_template_default_arg (default_argument);
9999 /* Template parameter packs cannot have default
10001 if (*is_parameter_pack)
10004 error ("%Htemplate parameter pack %qD cannot "
10005 "have a default argument",
10006 &token->location, identifier);
10008 error ("%Htemplate parameter packs cannot "
10009 "have default arguments",
10011 default_argument = NULL_TREE;
10013 pop_deferring_access_checks ();
10016 default_argument = NULL_TREE;
10018 /* Create the combined representation of the parameter and the
10019 default argument. */
10020 parameter = build_tree_list (default_argument, parameter);
10025 gcc_unreachable ();
10032 /* Parse a template-id.
10035 template-name < template-argument-list [opt] >
10037 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10038 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10039 returned. Otherwise, if the template-name names a function, or set
10040 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10041 names a class, returns a TYPE_DECL for the specialization.
10043 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10044 uninstantiated templates. */
10047 cp_parser_template_id (cp_parser *parser,
10048 bool template_keyword_p,
10049 bool check_dependency_p,
10050 bool is_declaration)
10056 cp_token_position start_of_id = 0;
10057 deferred_access_check *chk;
10058 VEC (deferred_access_check,gc) *access_check;
10059 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10060 bool is_identifier;
10062 /* If the next token corresponds to a template-id, there is no need
10064 next_token = cp_lexer_peek_token (parser->lexer);
10065 if (next_token->type == CPP_TEMPLATE_ID)
10067 struct tree_check *check_value;
10069 /* Get the stored value. */
10070 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10071 /* Perform any access checks that were deferred. */
10072 access_check = check_value->checks;
10076 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10079 perform_or_defer_access_check (chk->binfo,
10084 /* Return the stored value. */
10085 return check_value->value;
10088 /* Avoid performing name lookup if there is no possibility of
10089 finding a template-id. */
10090 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10091 || (next_token->type == CPP_NAME
10092 && !cp_parser_nth_token_starts_template_argument_list_p
10095 cp_parser_error (parser, "expected template-id");
10096 return error_mark_node;
10099 /* Remember where the template-id starts. */
10100 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10101 start_of_id = cp_lexer_token_position (parser->lexer, false);
10103 push_deferring_access_checks (dk_deferred);
10105 /* Parse the template-name. */
10106 is_identifier = false;
10107 token = cp_lexer_peek_token (parser->lexer);
10108 templ = cp_parser_template_name (parser, template_keyword_p,
10109 check_dependency_p,
10112 if (templ == error_mark_node || is_identifier)
10114 pop_deferring_access_checks ();
10118 /* If we find the sequence `[:' after a template-name, it's probably
10119 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10120 parse correctly the argument list. */
10121 next_token = cp_lexer_peek_token (parser->lexer);
10122 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10123 if (next_token->type == CPP_OPEN_SQUARE
10124 && next_token->flags & DIGRAPH
10125 && next_token_2->type == CPP_COLON
10126 && !(next_token_2->flags & PREV_WHITE))
10128 cp_parser_parse_tentatively (parser);
10129 /* Change `:' into `::'. */
10130 next_token_2->type = CPP_SCOPE;
10131 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10133 cp_lexer_consume_token (parser->lexer);
10135 /* Parse the arguments. */
10136 arguments = cp_parser_enclosed_template_argument_list (parser);
10137 if (!cp_parser_parse_definitely (parser))
10139 /* If we couldn't parse an argument list, then we revert our changes
10140 and return simply an error. Maybe this is not a template-id
10142 next_token_2->type = CPP_COLON;
10143 cp_parser_error (parser, "expected %<<%>");
10144 pop_deferring_access_checks ();
10145 return error_mark_node;
10147 /* Otherwise, emit an error about the invalid digraph, but continue
10148 parsing because we got our argument list. */
10149 if (permerror (next_token->location,
10150 "%<<::%> cannot begin a template-argument list"))
10152 static bool hint = false;
10153 inform (next_token->location,
10154 "%<<:%> is an alternate spelling for %<[%>."
10155 " Insert whitespace between %<<%> and %<::%>");
10156 if (!hint && !flag_permissive)
10158 inform (next_token->location, "(if you use %<-fpermissive%>"
10159 " G++ will accept your code)");
10166 /* Look for the `<' that starts the template-argument-list. */
10167 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10169 pop_deferring_access_checks ();
10170 return error_mark_node;
10172 /* Parse the arguments. */
10173 arguments = cp_parser_enclosed_template_argument_list (parser);
10176 /* Build a representation of the specialization. */
10177 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10178 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10179 else if (DECL_CLASS_TEMPLATE_P (templ)
10180 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10182 bool entering_scope;
10183 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10184 template (rather than some instantiation thereof) only if
10185 is not nested within some other construct. For example, in
10186 "template <typename T> void f(T) { A<T>::", A<T> is just an
10187 instantiation of A. */
10188 entering_scope = (template_parm_scope_p ()
10189 && cp_lexer_next_token_is (parser->lexer,
10192 = finish_template_type (templ, arguments, entering_scope);
10196 /* If it's not a class-template or a template-template, it should be
10197 a function-template. */
10198 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10199 || TREE_CODE (templ) == OVERLOAD
10200 || BASELINK_P (templ)));
10202 template_id = lookup_template_function (templ, arguments);
10205 /* If parsing tentatively, replace the sequence of tokens that makes
10206 up the template-id with a CPP_TEMPLATE_ID token. That way,
10207 should we re-parse the token stream, we will not have to repeat
10208 the effort required to do the parse, nor will we issue duplicate
10209 error messages about problems during instantiation of the
10213 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10215 /* Reset the contents of the START_OF_ID token. */
10216 token->type = CPP_TEMPLATE_ID;
10217 /* Retrieve any deferred checks. Do not pop this access checks yet
10218 so the memory will not be reclaimed during token replacing below. */
10219 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10220 token->u.tree_check_value->value = template_id;
10221 token->u.tree_check_value->checks = get_deferred_access_checks ();
10222 token->keyword = RID_MAX;
10224 /* Purge all subsequent tokens. */
10225 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10227 /* ??? Can we actually assume that, if template_id ==
10228 error_mark_node, we will have issued a diagnostic to the
10229 user, as opposed to simply marking the tentative parse as
10231 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10232 error ("%Hparse error in template argument list",
10236 pop_deferring_access_checks ();
10237 return template_id;
10240 /* Parse a template-name.
10245 The standard should actually say:
10249 operator-function-id
10251 A defect report has been filed about this issue.
10253 A conversion-function-id cannot be a template name because they cannot
10254 be part of a template-id. In fact, looking at this code:
10256 a.operator K<int>()
10258 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10259 It is impossible to call a templated conversion-function-id with an
10260 explicit argument list, since the only allowed template parameter is
10261 the type to which it is converting.
10263 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10264 `template' keyword, in a construction like:
10268 In that case `f' is taken to be a template-name, even though there
10269 is no way of knowing for sure.
10271 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10272 name refers to a set of overloaded functions, at least one of which
10273 is a template, or an IDENTIFIER_NODE with the name of the template,
10274 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10275 names are looked up inside uninstantiated templates. */
10278 cp_parser_template_name (cp_parser* parser,
10279 bool template_keyword_p,
10280 bool check_dependency_p,
10281 bool is_declaration,
10282 bool *is_identifier)
10287 cp_token *token = cp_lexer_peek_token (parser->lexer);
10289 /* If the next token is `operator', then we have either an
10290 operator-function-id or a conversion-function-id. */
10291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10293 /* We don't know whether we're looking at an
10294 operator-function-id or a conversion-function-id. */
10295 cp_parser_parse_tentatively (parser);
10296 /* Try an operator-function-id. */
10297 identifier = cp_parser_operator_function_id (parser);
10298 /* If that didn't work, try a conversion-function-id. */
10299 if (!cp_parser_parse_definitely (parser))
10301 cp_parser_error (parser, "expected template-name");
10302 return error_mark_node;
10305 /* Look for the identifier. */
10307 identifier = cp_parser_identifier (parser);
10309 /* If we didn't find an identifier, we don't have a template-id. */
10310 if (identifier == error_mark_node)
10311 return error_mark_node;
10313 /* If the name immediately followed the `template' keyword, then it
10314 is a template-name. However, if the next token is not `<', then
10315 we do not treat it as a template-name, since it is not being used
10316 as part of a template-id. This enables us to handle constructs
10319 template <typename T> struct S { S(); };
10320 template <typename T> S<T>::S();
10322 correctly. We would treat `S' as a template -- if it were `S<T>'
10323 -- but we do not if there is no `<'. */
10325 if (processing_template_decl
10326 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10328 /* In a declaration, in a dependent context, we pretend that the
10329 "template" keyword was present in order to improve error
10330 recovery. For example, given:
10332 template <typename T> void f(T::X<int>);
10334 we want to treat "X<int>" as a template-id. */
10336 && !template_keyword_p
10337 && parser->scope && TYPE_P (parser->scope)
10338 && check_dependency_p
10339 && dependent_scope_p (parser->scope)
10340 /* Do not do this for dtors (or ctors), since they never
10341 need the template keyword before their name. */
10342 && !constructor_name_p (identifier, parser->scope))
10344 cp_token_position start = 0;
10346 /* Explain what went wrong. */
10347 error ("%Hnon-template %qD used as template",
10348 &token->location, identifier);
10349 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10350 parser->scope, identifier);
10351 /* If parsing tentatively, find the location of the "<" token. */
10352 if (cp_parser_simulate_error (parser))
10353 start = cp_lexer_token_position (parser->lexer, true);
10354 /* Parse the template arguments so that we can issue error
10355 messages about them. */
10356 cp_lexer_consume_token (parser->lexer);
10357 cp_parser_enclosed_template_argument_list (parser);
10358 /* Skip tokens until we find a good place from which to
10359 continue parsing. */
10360 cp_parser_skip_to_closing_parenthesis (parser,
10361 /*recovering=*/true,
10363 /*consume_paren=*/false);
10364 /* If parsing tentatively, permanently remove the
10365 template argument list. That will prevent duplicate
10366 error messages from being issued about the missing
10367 "template" keyword. */
10369 cp_lexer_purge_tokens_after (parser->lexer, start);
10371 *is_identifier = true;
10375 /* If the "template" keyword is present, then there is generally
10376 no point in doing name-lookup, so we just return IDENTIFIER.
10377 But, if the qualifying scope is non-dependent then we can
10378 (and must) do name-lookup normally. */
10379 if (template_keyword_p
10381 || (TYPE_P (parser->scope)
10382 && dependent_type_p (parser->scope))))
10386 /* Look up the name. */
10387 decl = cp_parser_lookup_name (parser, identifier,
10389 /*is_template=*/false,
10390 /*is_namespace=*/false,
10391 check_dependency_p,
10392 /*ambiguous_decls=*/NULL,
10394 decl = maybe_get_template_decl_from_type_decl (decl);
10396 /* If DECL is a template, then the name was a template-name. */
10397 if (TREE_CODE (decl) == TEMPLATE_DECL)
10401 tree fn = NULL_TREE;
10403 /* The standard does not explicitly indicate whether a name that
10404 names a set of overloaded declarations, some of which are
10405 templates, is a template-name. However, such a name should
10406 be a template-name; otherwise, there is no way to form a
10407 template-id for the overloaded templates. */
10408 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10409 if (TREE_CODE (fns) == OVERLOAD)
10410 for (fn = fns; fn; fn = OVL_NEXT (fn))
10411 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10416 /* The name does not name a template. */
10417 cp_parser_error (parser, "expected template-name");
10418 return error_mark_node;
10422 /* If DECL is dependent, and refers to a function, then just return
10423 its name; we will look it up again during template instantiation. */
10424 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10426 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10427 if (TYPE_P (scope) && dependent_type_p (scope))
10434 /* Parse a template-argument-list.
10436 template-argument-list:
10437 template-argument ... [opt]
10438 template-argument-list , template-argument ... [opt]
10440 Returns a TREE_VEC containing the arguments. */
10443 cp_parser_template_argument_list (cp_parser* parser)
10445 tree fixed_args[10];
10446 unsigned n_args = 0;
10447 unsigned alloced = 10;
10448 tree *arg_ary = fixed_args;
10450 bool saved_in_template_argument_list_p;
10452 bool saved_non_ice_p;
10454 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10455 parser->in_template_argument_list_p = true;
10456 /* Even if the template-id appears in an integral
10457 constant-expression, the contents of the argument list do
10459 saved_ice_p = parser->integral_constant_expression_p;
10460 parser->integral_constant_expression_p = false;
10461 saved_non_ice_p = parser->non_integral_constant_expression_p;
10462 parser->non_integral_constant_expression_p = false;
10463 /* Parse the arguments. */
10469 /* Consume the comma. */
10470 cp_lexer_consume_token (parser->lexer);
10472 /* Parse the template-argument. */
10473 argument = cp_parser_template_argument (parser);
10475 /* If the next token is an ellipsis, we're expanding a template
10477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10479 /* Consume the `...' token. */
10480 cp_lexer_consume_token (parser->lexer);
10482 /* Make the argument into a TYPE_PACK_EXPANSION or
10483 EXPR_PACK_EXPANSION. */
10484 argument = make_pack_expansion (argument);
10487 if (n_args == alloced)
10491 if (arg_ary == fixed_args)
10493 arg_ary = XNEWVEC (tree, alloced);
10494 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10497 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10499 arg_ary[n_args++] = argument;
10501 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10503 vec = make_tree_vec (n_args);
10506 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10508 if (arg_ary != fixed_args)
10510 parser->non_integral_constant_expression_p = saved_non_ice_p;
10511 parser->integral_constant_expression_p = saved_ice_p;
10512 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10516 /* Parse a template-argument.
10519 assignment-expression
10523 The representation is that of an assignment-expression, type-id, or
10524 id-expression -- except that the qualified id-expression is
10525 evaluated, so that the value returned is either a DECL or an
10528 Although the standard says "assignment-expression", it forbids
10529 throw-expressions or assignments in the template argument.
10530 Therefore, we use "conditional-expression" instead. */
10533 cp_parser_template_argument (cp_parser* parser)
10538 bool maybe_type_id = false;
10539 cp_token *token = NULL, *argument_start_token = NULL;
10542 /* There's really no way to know what we're looking at, so we just
10543 try each alternative in order.
10547 In a template-argument, an ambiguity between a type-id and an
10548 expression is resolved to a type-id, regardless of the form of
10549 the corresponding template-parameter.
10551 Therefore, we try a type-id first. */
10552 cp_parser_parse_tentatively (parser);
10553 argument = cp_parser_template_type_arg (parser);
10554 /* If there was no error parsing the type-id but the next token is a
10555 '>>', our behavior depends on which dialect of C++ we're
10556 parsing. In C++98, we probably found a typo for '> >'. But there
10557 are type-id which are also valid expressions. For instance:
10559 struct X { int operator >> (int); };
10560 template <int V> struct Foo {};
10563 Here 'X()' is a valid type-id of a function type, but the user just
10564 wanted to write the expression "X() >> 5". Thus, we remember that we
10565 found a valid type-id, but we still try to parse the argument as an
10566 expression to see what happens.
10568 In C++0x, the '>>' will be considered two separate '>'
10570 if (!cp_parser_error_occurred (parser)
10571 && cxx_dialect == cxx98
10572 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10574 maybe_type_id = true;
10575 cp_parser_abort_tentative_parse (parser);
10579 /* If the next token isn't a `,' or a `>', then this argument wasn't
10580 really finished. This means that the argument is not a valid
10582 if (!cp_parser_next_token_ends_template_argument_p (parser))
10583 cp_parser_error (parser, "expected template-argument");
10584 /* If that worked, we're done. */
10585 if (cp_parser_parse_definitely (parser))
10588 /* We're still not sure what the argument will be. */
10589 cp_parser_parse_tentatively (parser);
10590 /* Try a template. */
10591 argument_start_token = cp_lexer_peek_token (parser->lexer);
10592 argument = cp_parser_id_expression (parser,
10593 /*template_keyword_p=*/false,
10594 /*check_dependency_p=*/true,
10596 /*declarator_p=*/false,
10597 /*optional_p=*/false);
10598 /* If the next token isn't a `,' or a `>', then this argument wasn't
10599 really finished. */
10600 if (!cp_parser_next_token_ends_template_argument_p (parser))
10601 cp_parser_error (parser, "expected template-argument");
10602 if (!cp_parser_error_occurred (parser))
10604 /* Figure out what is being referred to. If the id-expression
10605 was for a class template specialization, then we will have a
10606 TYPE_DECL at this point. There is no need to do name lookup
10607 at this point in that case. */
10608 if (TREE_CODE (argument) != TYPE_DECL)
10609 argument = cp_parser_lookup_name (parser, argument,
10611 /*is_template=*/template_p,
10612 /*is_namespace=*/false,
10613 /*check_dependency=*/true,
10614 /*ambiguous_decls=*/NULL,
10615 argument_start_token->location);
10616 if (TREE_CODE (argument) != TEMPLATE_DECL
10617 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10618 cp_parser_error (parser, "expected template-name");
10620 if (cp_parser_parse_definitely (parser))
10622 /* It must be a non-type argument. There permitted cases are given
10623 in [temp.arg.nontype]:
10625 -- an integral constant-expression of integral or enumeration
10628 -- the name of a non-type template-parameter; or
10630 -- the name of an object or function with external linkage...
10632 -- the address of an object or function with external linkage...
10634 -- a pointer to member... */
10635 /* Look for a non-type template parameter. */
10636 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10638 cp_parser_parse_tentatively (parser);
10639 argument = cp_parser_primary_expression (parser,
10640 /*address_p=*/false,
10642 /*template_arg_p=*/true,
10644 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10645 || !cp_parser_next_token_ends_template_argument_p (parser))
10646 cp_parser_simulate_error (parser);
10647 if (cp_parser_parse_definitely (parser))
10651 /* If the next token is "&", the argument must be the address of an
10652 object or function with external linkage. */
10653 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10655 cp_lexer_consume_token (parser->lexer);
10656 /* See if we might have an id-expression. */
10657 token = cp_lexer_peek_token (parser->lexer);
10658 if (token->type == CPP_NAME
10659 || token->keyword == RID_OPERATOR
10660 || token->type == CPP_SCOPE
10661 || token->type == CPP_TEMPLATE_ID
10662 || token->type == CPP_NESTED_NAME_SPECIFIER)
10664 cp_parser_parse_tentatively (parser);
10665 argument = cp_parser_primary_expression (parser,
10668 /*template_arg_p=*/true,
10670 if (cp_parser_error_occurred (parser)
10671 || !cp_parser_next_token_ends_template_argument_p (parser))
10672 cp_parser_abort_tentative_parse (parser);
10675 if (TREE_CODE (argument) == INDIRECT_REF)
10677 gcc_assert (REFERENCE_REF_P (argument));
10678 argument = TREE_OPERAND (argument, 0);
10681 if (TREE_CODE (argument) == VAR_DECL)
10683 /* A variable without external linkage might still be a
10684 valid constant-expression, so no error is issued here
10685 if the external-linkage check fails. */
10686 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10687 cp_parser_simulate_error (parser);
10689 else if (is_overloaded_fn (argument))
10690 /* All overloaded functions are allowed; if the external
10691 linkage test does not pass, an error will be issued
10695 && (TREE_CODE (argument) == OFFSET_REF
10696 || TREE_CODE (argument) == SCOPE_REF))
10697 /* A pointer-to-member. */
10699 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10702 cp_parser_simulate_error (parser);
10704 if (cp_parser_parse_definitely (parser))
10707 argument = build_x_unary_op (ADDR_EXPR, argument,
10708 tf_warning_or_error);
10713 /* If the argument started with "&", there are no other valid
10714 alternatives at this point. */
10717 cp_parser_error (parser, "invalid non-type template argument");
10718 return error_mark_node;
10721 /* If the argument wasn't successfully parsed as a type-id followed
10722 by '>>', the argument can only be a constant expression now.
10723 Otherwise, we try parsing the constant-expression tentatively,
10724 because the argument could really be a type-id. */
10726 cp_parser_parse_tentatively (parser);
10727 argument = cp_parser_constant_expression (parser,
10728 /*allow_non_constant_p=*/false,
10729 /*non_constant_p=*/NULL);
10730 argument = fold_non_dependent_expr (argument);
10731 if (!maybe_type_id)
10733 if (!cp_parser_next_token_ends_template_argument_p (parser))
10734 cp_parser_error (parser, "expected template-argument");
10735 if (cp_parser_parse_definitely (parser))
10737 /* We did our best to parse the argument as a non type-id, but that
10738 was the only alternative that matched (albeit with a '>' after
10739 it). We can assume it's just a typo from the user, and a
10740 diagnostic will then be issued. */
10741 return cp_parser_template_type_arg (parser);
10744 /* Parse an explicit-instantiation.
10746 explicit-instantiation:
10747 template declaration
10749 Although the standard says `declaration', what it really means is:
10751 explicit-instantiation:
10752 template decl-specifier-seq [opt] declarator [opt] ;
10754 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10755 supposed to be allowed. A defect report has been filed about this
10760 explicit-instantiation:
10761 storage-class-specifier template
10762 decl-specifier-seq [opt] declarator [opt] ;
10763 function-specifier template
10764 decl-specifier-seq [opt] declarator [opt] ; */
10767 cp_parser_explicit_instantiation (cp_parser* parser)
10769 int declares_class_or_enum;
10770 cp_decl_specifier_seq decl_specifiers;
10771 tree extension_specifier = NULL_TREE;
10774 /* Look for an (optional) storage-class-specifier or
10775 function-specifier. */
10776 if (cp_parser_allow_gnu_extensions_p (parser))
10778 extension_specifier
10779 = cp_parser_storage_class_specifier_opt (parser);
10780 if (!extension_specifier)
10781 extension_specifier
10782 = cp_parser_function_specifier_opt (parser,
10783 /*decl_specs=*/NULL);
10786 /* Look for the `template' keyword. */
10787 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10788 /* Let the front end know that we are processing an explicit
10790 begin_explicit_instantiation ();
10791 /* [temp.explicit] says that we are supposed to ignore access
10792 control while processing explicit instantiation directives. */
10793 push_deferring_access_checks (dk_no_check);
10794 /* Parse a decl-specifier-seq. */
10795 token = cp_lexer_peek_token (parser->lexer);
10796 cp_parser_decl_specifier_seq (parser,
10797 CP_PARSER_FLAGS_OPTIONAL,
10799 &declares_class_or_enum);
10800 /* If there was exactly one decl-specifier, and it declared a class,
10801 and there's no declarator, then we have an explicit type
10803 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10807 type = check_tag_decl (&decl_specifiers);
10808 /* Turn access control back on for names used during
10809 template instantiation. */
10810 pop_deferring_access_checks ();
10812 do_type_instantiation (type, extension_specifier,
10813 /*complain=*/tf_error);
10817 cp_declarator *declarator;
10820 /* Parse the declarator. */
10822 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10823 /*ctor_dtor_or_conv_p=*/NULL,
10824 /*parenthesized_p=*/NULL,
10825 /*member_p=*/false);
10826 if (declares_class_or_enum & 2)
10827 cp_parser_check_for_definition_in_return_type (declarator,
10828 decl_specifiers.type,
10829 decl_specifiers.type_location);
10830 if (declarator != cp_error_declarator)
10832 decl = grokdeclarator (declarator, &decl_specifiers,
10833 NORMAL, 0, &decl_specifiers.attributes);
10834 /* Turn access control back on for names used during
10835 template instantiation. */
10836 pop_deferring_access_checks ();
10837 /* Do the explicit instantiation. */
10838 do_decl_instantiation (decl, extension_specifier);
10842 pop_deferring_access_checks ();
10843 /* Skip the body of the explicit instantiation. */
10844 cp_parser_skip_to_end_of_statement (parser);
10847 /* We're done with the instantiation. */
10848 end_explicit_instantiation ();
10850 cp_parser_consume_semicolon_at_end_of_statement (parser);
10853 /* Parse an explicit-specialization.
10855 explicit-specialization:
10856 template < > declaration
10858 Although the standard says `declaration', what it really means is:
10860 explicit-specialization:
10861 template <> decl-specifier [opt] init-declarator [opt] ;
10862 template <> function-definition
10863 template <> explicit-specialization
10864 template <> template-declaration */
10867 cp_parser_explicit_specialization (cp_parser* parser)
10869 bool need_lang_pop;
10870 cp_token *token = cp_lexer_peek_token (parser->lexer);
10872 /* Look for the `template' keyword. */
10873 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10874 /* Look for the `<'. */
10875 cp_parser_require (parser, CPP_LESS, "%<<%>");
10876 /* Look for the `>'. */
10877 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10878 /* We have processed another parameter list. */
10879 ++parser->num_template_parameter_lists;
10882 A template ... explicit specialization ... shall not have C
10884 if (current_lang_name == lang_name_c)
10886 error ("%Htemplate specialization with C linkage", &token->location);
10887 /* Give it C++ linkage to avoid confusing other parts of the
10889 push_lang_context (lang_name_cplusplus);
10890 need_lang_pop = true;
10893 need_lang_pop = false;
10894 /* Let the front end know that we are beginning a specialization. */
10895 if (!begin_specialization ())
10897 end_specialization ();
10901 /* If the next keyword is `template', we need to figure out whether
10902 or not we're looking a template-declaration. */
10903 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10905 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10906 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10907 cp_parser_template_declaration_after_export (parser,
10908 /*member_p=*/false);
10910 cp_parser_explicit_specialization (parser);
10913 /* Parse the dependent declaration. */
10914 cp_parser_single_declaration (parser,
10916 /*member_p=*/false,
10917 /*explicit_specialization_p=*/true,
10918 /*friend_p=*/NULL);
10919 /* We're done with the specialization. */
10920 end_specialization ();
10921 /* For the erroneous case of a template with C linkage, we pushed an
10922 implicit C++ linkage scope; exit that scope now. */
10924 pop_lang_context ();
10925 /* We're done with this parameter list. */
10926 --parser->num_template_parameter_lists;
10929 /* Parse a type-specifier.
10932 simple-type-specifier
10935 elaborated-type-specifier
10943 Returns a representation of the type-specifier. For a
10944 class-specifier, enum-specifier, or elaborated-type-specifier, a
10945 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10947 The parser flags FLAGS is used to control type-specifier parsing.
10949 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10950 in a decl-specifier-seq.
10952 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10953 class-specifier, enum-specifier, or elaborated-type-specifier, then
10954 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10955 if a type is declared; 2 if it is defined. Otherwise, it is set to
10958 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10959 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10960 is set to FALSE. */
10963 cp_parser_type_specifier (cp_parser* parser,
10964 cp_parser_flags flags,
10965 cp_decl_specifier_seq *decl_specs,
10966 bool is_declaration,
10967 int* declares_class_or_enum,
10968 bool* is_cv_qualifier)
10970 tree type_spec = NULL_TREE;
10973 cp_decl_spec ds = ds_last;
10975 /* Assume this type-specifier does not declare a new type. */
10976 if (declares_class_or_enum)
10977 *declares_class_or_enum = 0;
10978 /* And that it does not specify a cv-qualifier. */
10979 if (is_cv_qualifier)
10980 *is_cv_qualifier = false;
10981 /* Peek at the next token. */
10982 token = cp_lexer_peek_token (parser->lexer);
10984 /* If we're looking at a keyword, we can use that to guide the
10985 production we choose. */
10986 keyword = token->keyword;
10990 /* Look for the enum-specifier. */
10991 type_spec = cp_parser_enum_specifier (parser);
10992 /* If that worked, we're done. */
10995 if (declares_class_or_enum)
10996 *declares_class_or_enum = 2;
10998 cp_parser_set_decl_spec_type (decl_specs,
11001 /*user_defined_p=*/true);
11005 goto elaborated_type_specifier;
11007 /* Any of these indicate either a class-specifier, or an
11008 elaborated-type-specifier. */
11012 /* Parse tentatively so that we can back up if we don't find a
11013 class-specifier. */
11014 cp_parser_parse_tentatively (parser);
11015 /* Look for the class-specifier. */
11016 type_spec = cp_parser_class_specifier (parser);
11017 /* If that worked, we're done. */
11018 if (cp_parser_parse_definitely (parser))
11020 if (declares_class_or_enum)
11021 *declares_class_or_enum = 2;
11023 cp_parser_set_decl_spec_type (decl_specs,
11026 /*user_defined_p=*/true);
11030 /* Fall through. */
11031 elaborated_type_specifier:
11032 /* We're declaring (not defining) a class or enum. */
11033 if (declares_class_or_enum)
11034 *declares_class_or_enum = 1;
11036 /* Fall through. */
11038 /* Look for an elaborated-type-specifier. */
11040 = (cp_parser_elaborated_type_specifier
11042 decl_specs && decl_specs->specs[(int) ds_friend],
11045 cp_parser_set_decl_spec_type (decl_specs,
11048 /*user_defined_p=*/true);
11053 if (is_cv_qualifier)
11054 *is_cv_qualifier = true;
11059 if (is_cv_qualifier)
11060 *is_cv_qualifier = true;
11065 if (is_cv_qualifier)
11066 *is_cv_qualifier = true;
11070 /* The `__complex__' keyword is a GNU extension. */
11078 /* Handle simple keywords. */
11083 ++decl_specs->specs[(int)ds];
11084 decl_specs->any_specifiers_p = true;
11086 return cp_lexer_consume_token (parser->lexer)->u.value;
11089 /* If we do not already have a type-specifier, assume we are looking
11090 at a simple-type-specifier. */
11091 type_spec = cp_parser_simple_type_specifier (parser,
11095 /* If we didn't find a type-specifier, and a type-specifier was not
11096 optional in this context, issue an error message. */
11097 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11099 cp_parser_error (parser, "expected type specifier");
11100 return error_mark_node;
11106 /* Parse a simple-type-specifier.
11108 simple-type-specifier:
11109 :: [opt] nested-name-specifier [opt] type-name
11110 :: [opt] nested-name-specifier template template-id
11125 simple-type-specifier:
11127 decltype ( expression )
11133 simple-type-specifier:
11134 __typeof__ unary-expression
11135 __typeof__ ( type-id )
11137 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11138 appropriately updated. */
11141 cp_parser_simple_type_specifier (cp_parser* parser,
11142 cp_decl_specifier_seq *decl_specs,
11143 cp_parser_flags flags)
11145 tree type = NULL_TREE;
11148 /* Peek at the next token. */
11149 token = cp_lexer_peek_token (parser->lexer);
11151 /* If we're looking at a keyword, things are easy. */
11152 switch (token->keyword)
11156 decl_specs->explicit_char_p = true;
11157 type = char_type_node;
11160 type = char16_type_node;
11163 type = char32_type_node;
11166 type = wchar_type_node;
11169 type = boolean_type_node;
11173 ++decl_specs->specs[(int) ds_short];
11174 type = short_integer_type_node;
11178 decl_specs->explicit_int_p = true;
11179 type = integer_type_node;
11183 ++decl_specs->specs[(int) ds_long];
11184 type = long_integer_type_node;
11188 ++decl_specs->specs[(int) ds_signed];
11189 type = integer_type_node;
11193 ++decl_specs->specs[(int) ds_unsigned];
11194 type = unsigned_type_node;
11197 type = float_type_node;
11200 type = double_type_node;
11203 type = void_type_node;
11207 maybe_warn_cpp0x ("C++0x auto");
11208 type = make_auto ();
11212 /* Parse the `decltype' type. */
11213 type = cp_parser_decltype (parser);
11216 cp_parser_set_decl_spec_type (decl_specs, type,
11218 /*user_defined_p=*/true);
11223 /* Consume the `typeof' token. */
11224 cp_lexer_consume_token (parser->lexer);
11225 /* Parse the operand to `typeof'. */
11226 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11227 /* If it is not already a TYPE, take its type. */
11228 if (!TYPE_P (type))
11229 type = finish_typeof (type);
11232 cp_parser_set_decl_spec_type (decl_specs, type,
11234 /*user_defined_p=*/true);
11242 /* If the type-specifier was for a built-in type, we're done. */
11247 /* Record the type. */
11249 && (token->keyword != RID_SIGNED
11250 && token->keyword != RID_UNSIGNED
11251 && token->keyword != RID_SHORT
11252 && token->keyword != RID_LONG))
11253 cp_parser_set_decl_spec_type (decl_specs,
11256 /*user_defined=*/false);
11258 decl_specs->any_specifiers_p = true;
11260 /* Consume the token. */
11261 id = cp_lexer_consume_token (parser->lexer)->u.value;
11263 /* There is no valid C++ program where a non-template type is
11264 followed by a "<". That usually indicates that the user thought
11265 that the type was a template. */
11266 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11268 return TYPE_NAME (type);
11271 /* The type-specifier must be a user-defined type. */
11272 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11277 /* Don't gobble tokens or issue error messages if this is an
11278 optional type-specifier. */
11279 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11280 cp_parser_parse_tentatively (parser);
11282 /* Look for the optional `::' operator. */
11284 = (cp_parser_global_scope_opt (parser,
11285 /*current_scope_valid_p=*/false)
11287 /* Look for the nested-name specifier. */
11289 = (cp_parser_nested_name_specifier_opt (parser,
11290 /*typename_keyword_p=*/false,
11291 /*check_dependency_p=*/true,
11293 /*is_declaration=*/false)
11295 token = cp_lexer_peek_token (parser->lexer);
11296 /* If we have seen a nested-name-specifier, and the next token
11297 is `template', then we are using the template-id production. */
11299 && cp_parser_optional_template_keyword (parser))
11301 /* Look for the template-id. */
11302 type = cp_parser_template_id (parser,
11303 /*template_keyword_p=*/true,
11304 /*check_dependency_p=*/true,
11305 /*is_declaration=*/false);
11306 /* If the template-id did not name a type, we are out of
11308 if (TREE_CODE (type) != TYPE_DECL)
11310 cp_parser_error (parser, "expected template-id for type");
11314 /* Otherwise, look for a type-name. */
11316 type = cp_parser_type_name (parser);
11317 /* Keep track of all name-lookups performed in class scopes. */
11321 && TREE_CODE (type) == TYPE_DECL
11322 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11323 maybe_note_name_used_in_class (DECL_NAME (type), type);
11324 /* If it didn't work out, we don't have a TYPE. */
11325 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11326 && !cp_parser_parse_definitely (parser))
11328 if (type && decl_specs)
11329 cp_parser_set_decl_spec_type (decl_specs, type,
11331 /*user_defined=*/true);
11334 /* If we didn't get a type-name, issue an error message. */
11335 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11337 cp_parser_error (parser, "expected type-name");
11338 return error_mark_node;
11341 /* There is no valid C++ program where a non-template type is
11342 followed by a "<". That usually indicates that the user thought
11343 that the type was a template. */
11344 if (type && type != error_mark_node)
11346 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11347 If it is, then the '<'...'>' enclose protocol names rather than
11348 template arguments, and so everything is fine. */
11349 if (c_dialect_objc ()
11350 && (objc_is_id (type) || objc_is_class_name (type)))
11352 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11353 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11355 /* Clobber the "unqualified" type previously entered into
11356 DECL_SPECS with the new, improved protocol-qualified version. */
11358 decl_specs->type = qual_type;
11363 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11370 /* Parse a type-name.
11383 Returns a TYPE_DECL for the type. */
11386 cp_parser_type_name (cp_parser* parser)
11390 /* We can't know yet whether it is a class-name or not. */
11391 cp_parser_parse_tentatively (parser);
11392 /* Try a class-name. */
11393 type_decl = cp_parser_class_name (parser,
11394 /*typename_keyword_p=*/false,
11395 /*template_keyword_p=*/false,
11397 /*check_dependency_p=*/true,
11398 /*class_head_p=*/false,
11399 /*is_declaration=*/false);
11400 /* If it's not a class-name, keep looking. */
11401 if (!cp_parser_parse_definitely (parser))
11403 /* It must be a typedef-name or an enum-name. */
11404 return cp_parser_nonclass_name (parser);
11410 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11418 Returns a TYPE_DECL for the type. */
11421 cp_parser_nonclass_name (cp_parser* parser)
11426 cp_token *token = cp_lexer_peek_token (parser->lexer);
11427 identifier = cp_parser_identifier (parser);
11428 if (identifier == error_mark_node)
11429 return error_mark_node;
11431 /* Look up the type-name. */
11432 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11434 if (TREE_CODE (type_decl) != TYPE_DECL
11435 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11437 /* See if this is an Objective-C type. */
11438 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11439 tree type = objc_get_protocol_qualified_type (identifier, protos);
11441 type_decl = TYPE_NAME (type);
11444 /* Issue an error if we did not find a type-name. */
11445 if (TREE_CODE (type_decl) != TYPE_DECL)
11447 if (!cp_parser_simulate_error (parser))
11448 cp_parser_name_lookup_error (parser, identifier, type_decl,
11449 "is not a type", token->location);
11450 return error_mark_node;
11452 /* Remember that the name was used in the definition of the
11453 current class so that we can check later to see if the
11454 meaning would have been different after the class was
11455 entirely defined. */
11456 else if (type_decl != error_mark_node
11458 maybe_note_name_used_in_class (identifier, type_decl);
11463 /* Parse an elaborated-type-specifier. Note that the grammar given
11464 here incorporates the resolution to DR68.
11466 elaborated-type-specifier:
11467 class-key :: [opt] nested-name-specifier [opt] identifier
11468 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11469 enum-key :: [opt] nested-name-specifier [opt] identifier
11470 typename :: [opt] nested-name-specifier identifier
11471 typename :: [opt] nested-name-specifier template [opt]
11476 elaborated-type-specifier:
11477 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11478 class-key attributes :: [opt] nested-name-specifier [opt]
11479 template [opt] template-id
11480 enum attributes :: [opt] nested-name-specifier [opt] identifier
11482 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11483 declared `friend'. If IS_DECLARATION is TRUE, then this
11484 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11485 something is being declared.
11487 Returns the TYPE specified. */
11490 cp_parser_elaborated_type_specifier (cp_parser* parser,
11492 bool is_declaration)
11494 enum tag_types tag_type;
11496 tree type = NULL_TREE;
11497 tree attributes = NULL_TREE;
11498 cp_token *token = NULL;
11500 /* See if we're looking at the `enum' keyword. */
11501 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11503 /* Consume the `enum' token. */
11504 cp_lexer_consume_token (parser->lexer);
11505 /* Remember that it's an enumeration type. */
11506 tag_type = enum_type;
11507 /* Parse the optional `struct' or `class' key (for C++0x scoped
11509 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11510 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11512 if (cxx_dialect == cxx98)
11513 maybe_warn_cpp0x ("scoped enums");
11515 /* Consume the `struct' or `class'. */
11516 cp_lexer_consume_token (parser->lexer);
11518 /* Parse the attributes. */
11519 attributes = cp_parser_attributes_opt (parser);
11521 /* Or, it might be `typename'. */
11522 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11525 /* Consume the `typename' token. */
11526 cp_lexer_consume_token (parser->lexer);
11527 /* Remember that it's a `typename' type. */
11528 tag_type = typename_type;
11529 /* The `typename' keyword is only allowed in templates. */
11530 if (!processing_template_decl)
11531 permerror (input_location, "using %<typename%> outside of template");
11533 /* Otherwise it must be a class-key. */
11536 tag_type = cp_parser_class_key (parser);
11537 if (tag_type == none_type)
11538 return error_mark_node;
11539 /* Parse the attributes. */
11540 attributes = cp_parser_attributes_opt (parser);
11543 /* Look for the `::' operator. */
11544 cp_parser_global_scope_opt (parser,
11545 /*current_scope_valid_p=*/false);
11546 /* Look for the nested-name-specifier. */
11547 if (tag_type == typename_type)
11549 if (!cp_parser_nested_name_specifier (parser,
11550 /*typename_keyword_p=*/true,
11551 /*check_dependency_p=*/true,
11554 return error_mark_node;
11557 /* Even though `typename' is not present, the proposed resolution
11558 to Core Issue 180 says that in `class A<T>::B', `B' should be
11559 considered a type-name, even if `A<T>' is dependent. */
11560 cp_parser_nested_name_specifier_opt (parser,
11561 /*typename_keyword_p=*/true,
11562 /*check_dependency_p=*/true,
11565 /* For everything but enumeration types, consider a template-id.
11566 For an enumeration type, consider only a plain identifier. */
11567 if (tag_type != enum_type)
11569 bool template_p = false;
11572 /* Allow the `template' keyword. */
11573 template_p = cp_parser_optional_template_keyword (parser);
11574 /* If we didn't see `template', we don't know if there's a
11575 template-id or not. */
11577 cp_parser_parse_tentatively (parser);
11578 /* Parse the template-id. */
11579 token = cp_lexer_peek_token (parser->lexer);
11580 decl = cp_parser_template_id (parser, template_p,
11581 /*check_dependency_p=*/true,
11583 /* If we didn't find a template-id, look for an ordinary
11585 if (!template_p && !cp_parser_parse_definitely (parser))
11587 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11588 in effect, then we must assume that, upon instantiation, the
11589 template will correspond to a class. */
11590 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11591 && tag_type == typename_type)
11592 type = make_typename_type (parser->scope, decl,
11594 /*complain=*/tf_error);
11595 /* If the `typename' keyword is in effect and DECL is not a type
11596 decl. Then type is non existant. */
11597 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11600 type = TREE_TYPE (decl);
11605 token = cp_lexer_peek_token (parser->lexer);
11606 identifier = cp_parser_identifier (parser);
11608 if (identifier == error_mark_node)
11610 parser->scope = NULL_TREE;
11611 return error_mark_node;
11614 /* For a `typename', we needn't call xref_tag. */
11615 if (tag_type == typename_type
11616 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11617 return cp_parser_make_typename_type (parser, parser->scope,
11620 /* Look up a qualified name in the usual way. */
11624 tree ambiguous_decls;
11626 decl = cp_parser_lookup_name (parser, identifier,
11628 /*is_template=*/false,
11629 /*is_namespace=*/false,
11630 /*check_dependency=*/true,
11634 /* If the lookup was ambiguous, an error will already have been
11636 if (ambiguous_decls)
11637 return error_mark_node;
11639 /* If we are parsing friend declaration, DECL may be a
11640 TEMPLATE_DECL tree node here. However, we need to check
11641 whether this TEMPLATE_DECL results in valid code. Consider
11642 the following example:
11645 template <class T> class C {};
11648 template <class T> friend class N::C; // #1, valid code
11650 template <class T> class Y {
11651 friend class N::C; // #2, invalid code
11654 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11655 name lookup of `N::C'. We see that friend declaration must
11656 be template for the code to be valid. Note that
11657 processing_template_decl does not work here since it is
11658 always 1 for the above two cases. */
11660 decl = (cp_parser_maybe_treat_template_as_class
11661 (decl, /*tag_name_p=*/is_friend
11662 && parser->num_template_parameter_lists));
11664 if (TREE_CODE (decl) != TYPE_DECL)
11666 cp_parser_diagnose_invalid_type_name (parser,
11670 return error_mark_node;
11673 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11675 bool allow_template = (parser->num_template_parameter_lists
11676 || DECL_SELF_REFERENCE_P (decl));
11677 type = check_elaborated_type_specifier (tag_type, decl,
11680 if (type == error_mark_node)
11681 return error_mark_node;
11684 /* Forward declarations of nested types, such as
11689 are invalid unless all components preceding the final '::'
11690 are complete. If all enclosing types are complete, these
11691 declarations become merely pointless.
11693 Invalid forward declarations of nested types are errors
11694 caught elsewhere in parsing. Those that are pointless arrive
11697 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11698 && !is_friend && !processing_explicit_instantiation)
11699 warning (0, "declaration %qD does not declare anything", decl);
11701 type = TREE_TYPE (decl);
11705 /* An elaborated-type-specifier sometimes introduces a new type and
11706 sometimes names an existing type. Normally, the rule is that it
11707 introduces a new type only if there is not an existing type of
11708 the same name already in scope. For example, given:
11711 void f() { struct S s; }
11713 the `struct S' in the body of `f' is the same `struct S' as in
11714 the global scope; the existing definition is used. However, if
11715 there were no global declaration, this would introduce a new
11716 local class named `S'.
11718 An exception to this rule applies to the following code:
11720 namespace N { struct S; }
11722 Here, the elaborated-type-specifier names a new type
11723 unconditionally; even if there is already an `S' in the
11724 containing scope this declaration names a new type.
11725 This exception only applies if the elaborated-type-specifier
11726 forms the complete declaration:
11730 A declaration consisting solely of `class-key identifier ;' is
11731 either a redeclaration of the name in the current scope or a
11732 forward declaration of the identifier as a class name. It
11733 introduces the name into the current scope.
11735 We are in this situation precisely when the next token is a `;'.
11737 An exception to the exception is that a `friend' declaration does
11738 *not* name a new type; i.e., given:
11740 struct S { friend struct T; };
11742 `T' is not a new type in the scope of `S'.
11744 Also, `new struct S' or `sizeof (struct S)' never results in the
11745 definition of a new type; a new type can only be declared in a
11746 declaration context. */
11752 /* Friends have special name lookup rules. */
11753 ts = ts_within_enclosing_non_class;
11754 else if (is_declaration
11755 && cp_lexer_next_token_is (parser->lexer,
11757 /* This is a `class-key identifier ;' */
11763 (parser->num_template_parameter_lists
11764 && (cp_parser_next_token_starts_class_definition_p (parser)
11765 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11766 /* An unqualified name was used to reference this type, so
11767 there were no qualifying templates. */
11768 if (!cp_parser_check_template_parameters (parser,
11769 /*num_templates=*/0,
11771 /*declarator=*/NULL))
11772 return error_mark_node;
11773 type = xref_tag (tag_type, identifier, ts, template_p);
11777 if (type == error_mark_node)
11778 return error_mark_node;
11780 /* Allow attributes on forward declarations of classes. */
11783 if (TREE_CODE (type) == TYPENAME_TYPE)
11784 warning (OPT_Wattributes,
11785 "attributes ignored on uninstantiated type");
11786 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11787 && ! processing_explicit_instantiation)
11788 warning (OPT_Wattributes,
11789 "attributes ignored on template instantiation");
11790 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11791 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11793 warning (OPT_Wattributes,
11794 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11797 if (tag_type != enum_type)
11798 cp_parser_check_class_key (tag_type, type);
11800 /* A "<" cannot follow an elaborated type specifier. If that
11801 happens, the user was probably trying to form a template-id. */
11802 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11807 /* Parse an enum-specifier.
11810 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11815 enum struct [C++0x]
11818 : type-specifier-seq
11821 enum-key attributes[opt] identifier [opt] enum-base [opt]
11822 { enumerator-list [opt] }attributes[opt]
11824 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11825 if the token stream isn't an enum-specifier after all. */
11828 cp_parser_enum_specifier (cp_parser* parser)
11833 bool scoped_enum_p = false;
11834 bool has_underlying_type = false;
11835 tree underlying_type = NULL_TREE;
11837 /* Parse tentatively so that we can back up if we don't find a
11839 cp_parser_parse_tentatively (parser);
11841 /* Caller guarantees that the current token is 'enum', an identifier
11842 possibly follows, and the token after that is an opening brace.
11843 If we don't have an identifier, fabricate an anonymous name for
11844 the enumeration being defined. */
11845 cp_lexer_consume_token (parser->lexer);
11847 /* Parse the "class" or "struct", which indicates a scoped
11848 enumeration type in C++0x. */
11849 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11850 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11852 if (cxx_dialect == cxx98)
11853 maybe_warn_cpp0x ("scoped enums");
11855 /* Consume the `struct' or `class' token. */
11856 cp_lexer_consume_token (parser->lexer);
11858 scoped_enum_p = true;
11861 attributes = cp_parser_attributes_opt (parser);
11863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11864 identifier = cp_parser_identifier (parser);
11866 identifier = make_anon_name ();
11868 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11869 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11871 cp_decl_specifier_seq type_specifiers;
11873 /* At this point this is surely not elaborated type specifier. */
11874 if (!cp_parser_parse_definitely (parser))
11877 if (cxx_dialect == cxx98)
11878 maybe_warn_cpp0x ("scoped enums");
11880 /* Consume the `:'. */
11881 cp_lexer_consume_token (parser->lexer);
11883 has_underlying_type = true;
11885 /* Parse the type-specifier-seq. */
11886 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11889 /* If that didn't work, stop. */
11890 if (type_specifiers.type != error_mark_node)
11892 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11893 /*initialized=*/0, NULL);
11894 if (underlying_type == error_mark_node)
11895 underlying_type = NULL_TREE;
11899 /* Look for the `{' but don't consume it yet. */
11900 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11902 cp_parser_error (parser, "expected %<{%>");
11903 if (has_underlying_type)
11907 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11910 /* Issue an error message if type-definitions are forbidden here. */
11911 if (!cp_parser_check_type_definition (parser))
11912 type = error_mark_node;
11914 /* Create the new type. We do this before consuming the opening
11915 brace so the enum will be recorded as being on the line of its
11916 tag (or the 'enum' keyword, if there is no tag). */
11917 type = start_enum (identifier, underlying_type, scoped_enum_p);
11919 /* Consume the opening brace. */
11920 cp_lexer_consume_token (parser->lexer);
11922 if (type == error_mark_node)
11924 cp_parser_skip_to_end_of_block_or_statement (parser);
11925 return error_mark_node;
11928 /* If the next token is not '}', then there are some enumerators. */
11929 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11930 cp_parser_enumerator_list (parser, type);
11932 /* Consume the final '}'. */
11933 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11935 /* Look for trailing attributes to apply to this enumeration, and
11936 apply them if appropriate. */
11937 if (cp_parser_allow_gnu_extensions_p (parser))
11939 tree trailing_attr = cp_parser_attributes_opt (parser);
11940 trailing_attr = chainon (trailing_attr, attributes);
11941 cplus_decl_attributes (&type,
11943 (int) ATTR_FLAG_TYPE_IN_PLACE);
11946 /* Finish up the enumeration. */
11947 finish_enum (type);
11952 /* Parse an enumerator-list. The enumerators all have the indicated
11956 enumerator-definition
11957 enumerator-list , enumerator-definition */
11960 cp_parser_enumerator_list (cp_parser* parser, tree type)
11964 /* Parse an enumerator-definition. */
11965 cp_parser_enumerator_definition (parser, type);
11967 /* If the next token is not a ',', we've reached the end of
11969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11971 /* Otherwise, consume the `,' and keep going. */
11972 cp_lexer_consume_token (parser->lexer);
11973 /* If the next token is a `}', there is a trailing comma. */
11974 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11976 if (!in_system_header)
11977 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11983 /* Parse an enumerator-definition. The enumerator has the indicated
11986 enumerator-definition:
11988 enumerator = constant-expression
11994 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11999 /* Look for the identifier. */
12000 identifier = cp_parser_identifier (parser);
12001 if (identifier == error_mark_node)
12004 /* If the next token is an '=', then there is an explicit value. */
12005 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12007 /* Consume the `=' token. */
12008 cp_lexer_consume_token (parser->lexer);
12009 /* Parse the value. */
12010 value = cp_parser_constant_expression (parser,
12011 /*allow_non_constant_p=*/false,
12017 /* If we are processing a template, make sure the initializer of the
12018 enumerator doesn't contain any bare template parameter pack. */
12019 if (check_for_bare_parameter_packs (value))
12020 value = error_mark_node;
12022 /* Create the enumerator. */
12023 build_enumerator (identifier, value, type);
12026 /* Parse a namespace-name.
12029 original-namespace-name
12032 Returns the NAMESPACE_DECL for the namespace. */
12035 cp_parser_namespace_name (cp_parser* parser)
12038 tree namespace_decl;
12040 cp_token *token = cp_lexer_peek_token (parser->lexer);
12042 /* Get the name of the namespace. */
12043 identifier = cp_parser_identifier (parser);
12044 if (identifier == error_mark_node)
12045 return error_mark_node;
12047 /* Look up the identifier in the currently active scope. Look only
12048 for namespaces, due to:
12050 [basic.lookup.udir]
12052 When looking up a namespace-name in a using-directive or alias
12053 definition, only namespace names are considered.
12057 [basic.lookup.qual]
12059 During the lookup of a name preceding the :: scope resolution
12060 operator, object, function, and enumerator names are ignored.
12062 (Note that cp_parser_qualifying_entity only calls this
12063 function if the token after the name is the scope resolution
12065 namespace_decl = cp_parser_lookup_name (parser, identifier,
12067 /*is_template=*/false,
12068 /*is_namespace=*/true,
12069 /*check_dependency=*/true,
12070 /*ambiguous_decls=*/NULL,
12072 /* If it's not a namespace, issue an error. */
12073 if (namespace_decl == error_mark_node
12074 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12076 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12077 error ("%H%qD is not a namespace-name", &token->location, identifier);
12078 cp_parser_error (parser, "expected namespace-name");
12079 namespace_decl = error_mark_node;
12082 return namespace_decl;
12085 /* Parse a namespace-definition.
12087 namespace-definition:
12088 named-namespace-definition
12089 unnamed-namespace-definition
12091 named-namespace-definition:
12092 original-namespace-definition
12093 extension-namespace-definition
12095 original-namespace-definition:
12096 namespace identifier { namespace-body }
12098 extension-namespace-definition:
12099 namespace original-namespace-name { namespace-body }
12101 unnamed-namespace-definition:
12102 namespace { namespace-body } */
12105 cp_parser_namespace_definition (cp_parser* parser)
12107 tree identifier, attribs;
12108 bool has_visibility;
12111 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12114 cp_lexer_consume_token (parser->lexer);
12119 /* Look for the `namespace' keyword. */
12120 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12122 /* Get the name of the namespace. We do not attempt to distinguish
12123 between an original-namespace-definition and an
12124 extension-namespace-definition at this point. The semantic
12125 analysis routines are responsible for that. */
12126 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12127 identifier = cp_parser_identifier (parser);
12129 identifier = NULL_TREE;
12131 /* Parse any specified attributes. */
12132 attribs = cp_parser_attributes_opt (parser);
12134 /* Look for the `{' to start the namespace. */
12135 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12136 /* Start the namespace. */
12137 push_namespace (identifier);
12139 /* "inline namespace" is equivalent to a stub namespace definition
12140 followed by a strong using directive. */
12143 tree name_space = current_namespace;
12144 /* Set up namespace association. */
12145 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12146 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12147 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12148 /* Import the contents of the inline namespace. */
12150 do_using_directive (name_space);
12151 push_namespace (identifier);
12154 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12156 /* Parse the body of the namespace. */
12157 cp_parser_namespace_body (parser);
12159 #ifdef HANDLE_PRAGMA_VISIBILITY
12160 if (has_visibility)
12164 /* Finish the namespace. */
12166 /* Look for the final `}'. */
12167 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12170 /* Parse a namespace-body.
12173 declaration-seq [opt] */
12176 cp_parser_namespace_body (cp_parser* parser)
12178 cp_parser_declaration_seq_opt (parser);
12181 /* Parse a namespace-alias-definition.
12183 namespace-alias-definition:
12184 namespace identifier = qualified-namespace-specifier ; */
12187 cp_parser_namespace_alias_definition (cp_parser* parser)
12190 tree namespace_specifier;
12192 cp_token *token = cp_lexer_peek_token (parser->lexer);
12194 /* Look for the `namespace' keyword. */
12195 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12196 /* Look for the identifier. */
12197 identifier = cp_parser_identifier (parser);
12198 if (identifier == error_mark_node)
12200 /* Look for the `=' token. */
12201 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12202 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12204 error ("%H%<namespace%> definition is not allowed here", &token->location);
12205 /* Skip the definition. */
12206 cp_lexer_consume_token (parser->lexer);
12207 if (cp_parser_skip_to_closing_brace (parser))
12208 cp_lexer_consume_token (parser->lexer);
12211 cp_parser_require (parser, CPP_EQ, "%<=%>");
12212 /* Look for the qualified-namespace-specifier. */
12213 namespace_specifier
12214 = cp_parser_qualified_namespace_specifier (parser);
12215 /* Look for the `;' token. */
12216 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12218 /* Register the alias in the symbol table. */
12219 do_namespace_alias (identifier, namespace_specifier);
12222 /* Parse a qualified-namespace-specifier.
12224 qualified-namespace-specifier:
12225 :: [opt] nested-name-specifier [opt] namespace-name
12227 Returns a NAMESPACE_DECL corresponding to the specified
12231 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12233 /* Look for the optional `::'. */
12234 cp_parser_global_scope_opt (parser,
12235 /*current_scope_valid_p=*/false);
12237 /* Look for the optional nested-name-specifier. */
12238 cp_parser_nested_name_specifier_opt (parser,
12239 /*typename_keyword_p=*/false,
12240 /*check_dependency_p=*/true,
12242 /*is_declaration=*/true);
12244 return cp_parser_namespace_name (parser);
12247 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12248 access declaration.
12251 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12252 using :: unqualified-id ;
12254 access-declaration:
12260 cp_parser_using_declaration (cp_parser* parser,
12261 bool access_declaration_p)
12264 bool typename_p = false;
12265 bool global_scope_p;
12270 if (access_declaration_p)
12271 cp_parser_parse_tentatively (parser);
12274 /* Look for the `using' keyword. */
12275 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12277 /* Peek at the next token. */
12278 token = cp_lexer_peek_token (parser->lexer);
12279 /* See if it's `typename'. */
12280 if (token->keyword == RID_TYPENAME)
12282 /* Remember that we've seen it. */
12284 /* Consume the `typename' token. */
12285 cp_lexer_consume_token (parser->lexer);
12289 /* Look for the optional global scope qualification. */
12291 = (cp_parser_global_scope_opt (parser,
12292 /*current_scope_valid_p=*/false)
12295 /* If we saw `typename', or didn't see `::', then there must be a
12296 nested-name-specifier present. */
12297 if (typename_p || !global_scope_p)
12298 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12299 /*check_dependency_p=*/true,
12301 /*is_declaration=*/true);
12302 /* Otherwise, we could be in either of the two productions. In that
12303 case, treat the nested-name-specifier as optional. */
12305 qscope = cp_parser_nested_name_specifier_opt (parser,
12306 /*typename_keyword_p=*/false,
12307 /*check_dependency_p=*/true,
12309 /*is_declaration=*/true);
12311 qscope = global_namespace;
12313 if (access_declaration_p && cp_parser_error_occurred (parser))
12314 /* Something has already gone wrong; there's no need to parse
12315 further. Since an error has occurred, the return value of
12316 cp_parser_parse_definitely will be false, as required. */
12317 return cp_parser_parse_definitely (parser);
12319 token = cp_lexer_peek_token (parser->lexer);
12320 /* Parse the unqualified-id. */
12321 identifier = cp_parser_unqualified_id (parser,
12322 /*template_keyword_p=*/false,
12323 /*check_dependency_p=*/true,
12324 /*declarator_p=*/true,
12325 /*optional_p=*/false);
12327 if (access_declaration_p)
12329 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12330 cp_parser_simulate_error (parser);
12331 if (!cp_parser_parse_definitely (parser))
12335 /* The function we call to handle a using-declaration is different
12336 depending on what scope we are in. */
12337 if (qscope == error_mark_node || identifier == error_mark_node)
12339 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12340 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12341 /* [namespace.udecl]
12343 A using declaration shall not name a template-id. */
12344 error ("%Ha template-id may not appear in a using-declaration",
12348 if (at_class_scope_p ())
12350 /* Create the USING_DECL. */
12351 decl = do_class_using_decl (parser->scope, identifier);
12353 if (check_for_bare_parameter_packs (decl))
12356 /* Add it to the list of members in this class. */
12357 finish_member_declaration (decl);
12361 decl = cp_parser_lookup_name_simple (parser,
12364 if (decl == error_mark_node)
12365 cp_parser_name_lookup_error (parser, identifier,
12368 else if (check_for_bare_parameter_packs (decl))
12370 else if (!at_namespace_scope_p ())
12371 do_local_using_decl (decl, qscope, identifier);
12373 do_toplevel_using_decl (decl, qscope, identifier);
12377 /* Look for the final `;'. */
12378 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12383 /* Parse a using-directive.
12386 using namespace :: [opt] nested-name-specifier [opt]
12387 namespace-name ; */
12390 cp_parser_using_directive (cp_parser* parser)
12392 tree namespace_decl;
12395 /* Look for the `using' keyword. */
12396 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12397 /* And the `namespace' keyword. */
12398 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12399 /* Look for the optional `::' operator. */
12400 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12401 /* And the optional nested-name-specifier. */
12402 cp_parser_nested_name_specifier_opt (parser,
12403 /*typename_keyword_p=*/false,
12404 /*check_dependency_p=*/true,
12406 /*is_declaration=*/true);
12407 /* Get the namespace being used. */
12408 namespace_decl = cp_parser_namespace_name (parser);
12409 /* And any specified attributes. */
12410 attribs = cp_parser_attributes_opt (parser);
12411 /* Update the symbol table. */
12412 parse_using_directive (namespace_decl, attribs);
12413 /* Look for the final `;'. */
12414 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12417 /* Parse an asm-definition.
12420 asm ( string-literal ) ;
12425 asm volatile [opt] ( string-literal ) ;
12426 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12427 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12428 : asm-operand-list [opt] ) ;
12429 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12430 : asm-operand-list [opt]
12431 : asm-operand-list [opt] ) ; */
12434 cp_parser_asm_definition (cp_parser* parser)
12437 tree outputs = NULL_TREE;
12438 tree inputs = NULL_TREE;
12439 tree clobbers = NULL_TREE;
12441 bool volatile_p = false;
12442 bool extended_p = false;
12443 bool invalid_inputs_p = false;
12444 bool invalid_outputs_p = false;
12446 /* Look for the `asm' keyword. */
12447 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12448 /* See if the next token is `volatile'. */
12449 if (cp_parser_allow_gnu_extensions_p (parser)
12450 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12452 /* Remember that we saw the `volatile' keyword. */
12454 /* Consume the token. */
12455 cp_lexer_consume_token (parser->lexer);
12457 /* Look for the opening `('. */
12458 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12460 /* Look for the string. */
12461 string = cp_parser_string_literal (parser, false, false);
12462 if (string == error_mark_node)
12464 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12465 /*consume_paren=*/true);
12469 /* If we're allowing GNU extensions, check for the extended assembly
12470 syntax. Unfortunately, the `:' tokens need not be separated by
12471 a space in C, and so, for compatibility, we tolerate that here
12472 too. Doing that means that we have to treat the `::' operator as
12474 if (cp_parser_allow_gnu_extensions_p (parser)
12475 && parser->in_function_body
12476 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12477 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12479 bool inputs_p = false;
12480 bool clobbers_p = false;
12482 /* The extended syntax was used. */
12485 /* Look for outputs. */
12486 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12488 /* Consume the `:'. */
12489 cp_lexer_consume_token (parser->lexer);
12490 /* Parse the output-operands. */
12491 if (cp_lexer_next_token_is_not (parser->lexer,
12493 && cp_lexer_next_token_is_not (parser->lexer,
12495 && cp_lexer_next_token_is_not (parser->lexer,
12497 outputs = cp_parser_asm_operand_list (parser);
12499 if (outputs == error_mark_node)
12500 invalid_outputs_p = true;
12502 /* If the next token is `::', there are no outputs, and the
12503 next token is the beginning of the inputs. */
12504 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12505 /* The inputs are coming next. */
12508 /* Look for inputs. */
12510 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12512 /* Consume the `:' or `::'. */
12513 cp_lexer_consume_token (parser->lexer);
12514 /* Parse the output-operands. */
12515 if (cp_lexer_next_token_is_not (parser->lexer,
12517 && cp_lexer_next_token_is_not (parser->lexer,
12519 inputs = cp_parser_asm_operand_list (parser);
12521 if (inputs == error_mark_node)
12522 invalid_inputs_p = true;
12524 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12525 /* The clobbers are coming next. */
12528 /* Look for clobbers. */
12530 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12532 /* Consume the `:' or `::'. */
12533 cp_lexer_consume_token (parser->lexer);
12534 /* Parse the clobbers. */
12535 if (cp_lexer_next_token_is_not (parser->lexer,
12537 clobbers = cp_parser_asm_clobber_list (parser);
12540 /* Look for the closing `)'. */
12541 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12542 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12543 /*consume_paren=*/true);
12544 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12546 if (!invalid_inputs_p && !invalid_outputs_p)
12548 /* Create the ASM_EXPR. */
12549 if (parser->in_function_body)
12551 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12553 /* If the extended syntax was not used, mark the ASM_EXPR. */
12556 tree temp = asm_stmt;
12557 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12558 temp = TREE_OPERAND (temp, 0);
12560 ASM_INPUT_P (temp) = 1;
12564 cgraph_add_asm_node (string);
12568 /* Declarators [gram.dcl.decl] */
12570 /* Parse an init-declarator.
12573 declarator initializer [opt]
12578 declarator asm-specification [opt] attributes [opt] initializer [opt]
12580 function-definition:
12581 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12583 decl-specifier-seq [opt] declarator function-try-block
12587 function-definition:
12588 __extension__ function-definition
12590 The DECL_SPECIFIERS apply to this declarator. Returns a
12591 representation of the entity declared. If MEMBER_P is TRUE, then
12592 this declarator appears in a class scope. The new DECL created by
12593 this declarator is returned.
12595 The CHECKS are access checks that should be performed once we know
12596 what entity is being declared (and, therefore, what classes have
12599 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12600 for a function-definition here as well. If the declarator is a
12601 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12602 be TRUE upon return. By that point, the function-definition will
12603 have been completely parsed.
12605 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12609 cp_parser_init_declarator (cp_parser* parser,
12610 cp_decl_specifier_seq *decl_specifiers,
12611 VEC (deferred_access_check,gc)* checks,
12612 bool function_definition_allowed_p,
12614 int declares_class_or_enum,
12615 bool* function_definition_p)
12617 cp_token *token = NULL, *asm_spec_start_token = NULL,
12618 *attributes_start_token = NULL;
12619 cp_declarator *declarator;
12620 tree prefix_attributes;
12622 tree asm_specification;
12624 tree decl = NULL_TREE;
12626 int is_initialized;
12627 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12628 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12630 enum cpp_ttype initialization_kind;
12631 bool is_direct_init = false;
12632 bool is_non_constant_init;
12633 int ctor_dtor_or_conv_p;
12635 tree pushed_scope = NULL;
12637 /* Gather the attributes that were provided with the
12638 decl-specifiers. */
12639 prefix_attributes = decl_specifiers->attributes;
12641 /* Assume that this is not the declarator for a function
12643 if (function_definition_p)
12644 *function_definition_p = false;
12646 /* Defer access checks while parsing the declarator; we cannot know
12647 what names are accessible until we know what is being
12649 resume_deferring_access_checks ();
12651 /* Parse the declarator. */
12652 token = cp_lexer_peek_token (parser->lexer);
12654 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12655 &ctor_dtor_or_conv_p,
12656 /*parenthesized_p=*/NULL,
12657 /*member_p=*/false);
12658 /* Gather up the deferred checks. */
12659 stop_deferring_access_checks ();
12661 /* If the DECLARATOR was erroneous, there's no need to go
12663 if (declarator == cp_error_declarator)
12664 return error_mark_node;
12666 /* Check that the number of template-parameter-lists is OK. */
12667 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12669 return error_mark_node;
12671 if (declares_class_or_enum & 2)
12672 cp_parser_check_for_definition_in_return_type (declarator,
12673 decl_specifiers->type,
12674 decl_specifiers->type_location);
12676 /* Figure out what scope the entity declared by the DECLARATOR is
12677 located in. `grokdeclarator' sometimes changes the scope, so
12678 we compute it now. */
12679 scope = get_scope_of_declarator (declarator);
12681 /* If we're allowing GNU extensions, look for an asm-specification
12683 if (cp_parser_allow_gnu_extensions_p (parser))
12685 /* Look for an asm-specification. */
12686 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12687 asm_specification = cp_parser_asm_specification_opt (parser);
12688 /* And attributes. */
12689 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12690 attributes = cp_parser_attributes_opt (parser);
12694 asm_specification = NULL_TREE;
12695 attributes = NULL_TREE;
12698 /* Peek at the next token. */
12699 token = cp_lexer_peek_token (parser->lexer);
12700 /* Check to see if the token indicates the start of a
12701 function-definition. */
12702 if (function_declarator_p (declarator)
12703 && cp_parser_token_starts_function_definition_p (token))
12705 if (!function_definition_allowed_p)
12707 /* If a function-definition should not appear here, issue an
12709 cp_parser_error (parser,
12710 "a function-definition is not allowed here");
12711 return error_mark_node;
12715 location_t func_brace_location
12716 = cp_lexer_peek_token (parser->lexer)->location;
12718 /* Neither attributes nor an asm-specification are allowed
12719 on a function-definition. */
12720 if (asm_specification)
12721 error ("%Han asm-specification is not allowed "
12722 "on a function-definition",
12723 &asm_spec_start_token->location);
12725 error ("%Hattributes are not allowed on a function-definition",
12726 &attributes_start_token->location);
12727 /* This is a function-definition. */
12728 *function_definition_p = true;
12730 /* Parse the function definition. */
12732 decl = cp_parser_save_member_function_body (parser,
12735 prefix_attributes);
12738 = (cp_parser_function_definition_from_specifiers_and_declarator
12739 (parser, decl_specifiers, prefix_attributes, declarator));
12741 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12743 /* This is where the prologue starts... */
12744 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12745 = func_brace_location;
12754 Only in function declarations for constructors, destructors, and
12755 type conversions can the decl-specifier-seq be omitted.
12757 We explicitly postpone this check past the point where we handle
12758 function-definitions because we tolerate function-definitions
12759 that are missing their return types in some modes. */
12760 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12762 cp_parser_error (parser,
12763 "expected constructor, destructor, or type conversion");
12764 return error_mark_node;
12767 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12768 if (token->type == CPP_EQ
12769 || token->type == CPP_OPEN_PAREN
12770 || token->type == CPP_OPEN_BRACE)
12772 is_initialized = SD_INITIALIZED;
12773 initialization_kind = token->type;
12775 if (token->type == CPP_EQ
12776 && function_declarator_p (declarator))
12778 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12779 if (t2->keyword == RID_DEFAULT)
12780 is_initialized = SD_DEFAULTED;
12781 else if (t2->keyword == RID_DELETE)
12782 is_initialized = SD_DELETED;
12787 /* If the init-declarator isn't initialized and isn't followed by a
12788 `,' or `;', it's not a valid init-declarator. */
12789 if (token->type != CPP_COMMA
12790 && token->type != CPP_SEMICOLON)
12792 cp_parser_error (parser, "expected initializer");
12793 return error_mark_node;
12795 is_initialized = SD_UNINITIALIZED;
12796 initialization_kind = CPP_EOF;
12799 /* Because start_decl has side-effects, we should only call it if we
12800 know we're going ahead. By this point, we know that we cannot
12801 possibly be looking at any other construct. */
12802 cp_parser_commit_to_tentative_parse (parser);
12804 /* If the decl specifiers were bad, issue an error now that we're
12805 sure this was intended to be a declarator. Then continue
12806 declaring the variable(s), as int, to try to cut down on further
12808 if (decl_specifiers->any_specifiers_p
12809 && decl_specifiers->type == error_mark_node)
12811 cp_parser_error (parser, "invalid type in declaration");
12812 decl_specifiers->type = integer_type_node;
12815 /* Check to see whether or not this declaration is a friend. */
12816 friend_p = cp_parser_friend_p (decl_specifiers);
12818 /* Enter the newly declared entry in the symbol table. If we're
12819 processing a declaration in a class-specifier, we wait until
12820 after processing the initializer. */
12823 if (parser->in_unbraced_linkage_specification_p)
12824 decl_specifiers->storage_class = sc_extern;
12825 decl = start_decl (declarator, decl_specifiers,
12826 is_initialized, attributes, prefix_attributes,
12830 /* Enter the SCOPE. That way unqualified names appearing in the
12831 initializer will be looked up in SCOPE. */
12832 pushed_scope = push_scope (scope);
12834 /* Perform deferred access control checks, now that we know in which
12835 SCOPE the declared entity resides. */
12836 if (!member_p && decl)
12838 tree saved_current_function_decl = NULL_TREE;
12840 /* If the entity being declared is a function, pretend that we
12841 are in its scope. If it is a `friend', it may have access to
12842 things that would not otherwise be accessible. */
12843 if (TREE_CODE (decl) == FUNCTION_DECL)
12845 saved_current_function_decl = current_function_decl;
12846 current_function_decl = decl;
12849 /* Perform access checks for template parameters. */
12850 cp_parser_perform_template_parameter_access_checks (checks);
12852 /* Perform the access control checks for the declarator and the
12853 decl-specifiers. */
12854 perform_deferred_access_checks ();
12856 /* Restore the saved value. */
12857 if (TREE_CODE (decl) == FUNCTION_DECL)
12858 current_function_decl = saved_current_function_decl;
12861 /* Parse the initializer. */
12862 initializer = NULL_TREE;
12863 is_direct_init = false;
12864 is_non_constant_init = true;
12865 if (is_initialized)
12867 if (function_declarator_p (declarator))
12869 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12870 if (initialization_kind == CPP_EQ)
12871 initializer = cp_parser_pure_specifier (parser);
12874 /* If the declaration was erroneous, we don't really
12875 know what the user intended, so just silently
12876 consume the initializer. */
12877 if (decl != error_mark_node)
12878 error ("%Hinitializer provided for function",
12879 &initializer_start_token->location);
12880 cp_parser_skip_to_closing_parenthesis (parser,
12881 /*recovering=*/true,
12882 /*or_comma=*/false,
12883 /*consume_paren=*/true);
12887 initializer = cp_parser_initializer (parser,
12889 &is_non_constant_init);
12892 /* The old parser allows attributes to appear after a parenthesized
12893 initializer. Mark Mitchell proposed removing this functionality
12894 on the GCC mailing lists on 2002-08-13. This parser accepts the
12895 attributes -- but ignores them. */
12896 if (cp_parser_allow_gnu_extensions_p (parser)
12897 && initialization_kind == CPP_OPEN_PAREN)
12898 if (cp_parser_attributes_opt (parser))
12899 warning (OPT_Wattributes,
12900 "attributes after parenthesized initializer ignored");
12902 /* For an in-class declaration, use `grokfield' to create the
12908 pop_scope (pushed_scope);
12909 pushed_scope = false;
12911 decl = grokfield (declarator, decl_specifiers,
12912 initializer, !is_non_constant_init,
12913 /*asmspec=*/NULL_TREE,
12914 prefix_attributes);
12915 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12916 cp_parser_save_default_args (parser, decl);
12919 /* Finish processing the declaration. But, skip friend
12921 if (!friend_p && decl && decl != error_mark_node)
12923 cp_finish_decl (decl,
12924 initializer, !is_non_constant_init,
12926 /* If the initializer is in parentheses, then this is
12927 a direct-initialization, which means that an
12928 `explicit' constructor is OK. Otherwise, an
12929 `explicit' constructor cannot be used. */
12930 ((is_direct_init || !is_initialized)
12931 ? 0 : LOOKUP_ONLYCONVERTING));
12933 else if ((cxx_dialect != cxx98) && friend_p
12934 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12935 /* Core issue #226 (C++0x only): A default template-argument
12936 shall not be specified in a friend class template
12938 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12939 /*is_partial=*/0, /*is_friend_decl=*/1);
12941 if (!friend_p && pushed_scope)
12942 pop_scope (pushed_scope);
12947 /* Parse a declarator.
12951 ptr-operator declarator
12953 abstract-declarator:
12954 ptr-operator abstract-declarator [opt]
12955 direct-abstract-declarator
12960 attributes [opt] direct-declarator
12961 attributes [opt] ptr-operator declarator
12963 abstract-declarator:
12964 attributes [opt] ptr-operator abstract-declarator [opt]
12965 attributes [opt] direct-abstract-declarator
12967 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12968 detect constructor, destructor or conversion operators. It is set
12969 to -1 if the declarator is a name, and +1 if it is a
12970 function. Otherwise it is set to zero. Usually you just want to
12971 test for >0, but internally the negative value is used.
12973 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12974 a decl-specifier-seq unless it declares a constructor, destructor,
12975 or conversion. It might seem that we could check this condition in
12976 semantic analysis, rather than parsing, but that makes it difficult
12977 to handle something like `f()'. We want to notice that there are
12978 no decl-specifiers, and therefore realize that this is an
12979 expression, not a declaration.)
12981 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12982 the declarator is a direct-declarator of the form "(...)".
12984 MEMBER_P is true iff this declarator is a member-declarator. */
12986 static cp_declarator *
12987 cp_parser_declarator (cp_parser* parser,
12988 cp_parser_declarator_kind dcl_kind,
12989 int* ctor_dtor_or_conv_p,
12990 bool* parenthesized_p,
12994 cp_declarator *declarator;
12995 enum tree_code code;
12996 cp_cv_quals cv_quals;
12998 tree attributes = NULL_TREE;
13000 /* Assume this is not a constructor, destructor, or type-conversion
13002 if (ctor_dtor_or_conv_p)
13003 *ctor_dtor_or_conv_p = 0;
13005 if (cp_parser_allow_gnu_extensions_p (parser))
13006 attributes = cp_parser_attributes_opt (parser);
13008 /* Peek at the next token. */
13009 token = cp_lexer_peek_token (parser->lexer);
13011 /* Check for the ptr-operator production. */
13012 cp_parser_parse_tentatively (parser);
13013 /* Parse the ptr-operator. */
13014 code = cp_parser_ptr_operator (parser,
13017 /* If that worked, then we have a ptr-operator. */
13018 if (cp_parser_parse_definitely (parser))
13020 /* If a ptr-operator was found, then this declarator was not
13022 if (parenthesized_p)
13023 *parenthesized_p = true;
13024 /* The dependent declarator is optional if we are parsing an
13025 abstract-declarator. */
13026 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13027 cp_parser_parse_tentatively (parser);
13029 /* Parse the dependent declarator. */
13030 declarator = cp_parser_declarator (parser, dcl_kind,
13031 /*ctor_dtor_or_conv_p=*/NULL,
13032 /*parenthesized_p=*/NULL,
13033 /*member_p=*/false);
13035 /* If we are parsing an abstract-declarator, we must handle the
13036 case where the dependent declarator is absent. */
13037 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13038 && !cp_parser_parse_definitely (parser))
13041 declarator = cp_parser_make_indirect_declarator
13042 (code, class_type, cv_quals, declarator);
13044 /* Everything else is a direct-declarator. */
13047 if (parenthesized_p)
13048 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13050 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13051 ctor_dtor_or_conv_p,
13055 if (attributes && declarator && declarator != cp_error_declarator)
13056 declarator->attributes = attributes;
13061 /* Parse a direct-declarator or direct-abstract-declarator.
13065 direct-declarator ( parameter-declaration-clause )
13066 cv-qualifier-seq [opt]
13067 exception-specification [opt]
13068 direct-declarator [ constant-expression [opt] ]
13071 direct-abstract-declarator:
13072 direct-abstract-declarator [opt]
13073 ( parameter-declaration-clause )
13074 cv-qualifier-seq [opt]
13075 exception-specification [opt]
13076 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13077 ( abstract-declarator )
13079 Returns a representation of the declarator. DCL_KIND is
13080 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13081 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13082 we are parsing a direct-declarator. It is
13083 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13084 of ambiguity we prefer an abstract declarator, as per
13085 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13086 cp_parser_declarator. */
13088 static cp_declarator *
13089 cp_parser_direct_declarator (cp_parser* parser,
13090 cp_parser_declarator_kind dcl_kind,
13091 int* ctor_dtor_or_conv_p,
13095 cp_declarator *declarator = NULL;
13096 tree scope = NULL_TREE;
13097 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13098 bool saved_in_declarator_p = parser->in_declarator_p;
13100 tree pushed_scope = NULL_TREE;
13104 /* Peek at the next token. */
13105 token = cp_lexer_peek_token (parser->lexer);
13106 if (token->type == CPP_OPEN_PAREN)
13108 /* This is either a parameter-declaration-clause, or a
13109 parenthesized declarator. When we know we are parsing a
13110 named declarator, it must be a parenthesized declarator
13111 if FIRST is true. For instance, `(int)' is a
13112 parameter-declaration-clause, with an omitted
13113 direct-abstract-declarator. But `((*))', is a
13114 parenthesized abstract declarator. Finally, when T is a
13115 template parameter `(T)' is a
13116 parameter-declaration-clause, and not a parenthesized
13119 We first try and parse a parameter-declaration-clause,
13120 and then try a nested declarator (if FIRST is true).
13122 It is not an error for it not to be a
13123 parameter-declaration-clause, even when FIRST is
13129 The first is the declaration of a function while the
13130 second is the definition of a variable, including its
13133 Having seen only the parenthesis, we cannot know which of
13134 these two alternatives should be selected. Even more
13135 complex are examples like:
13140 The former is a function-declaration; the latter is a
13141 variable initialization.
13143 Thus again, we try a parameter-declaration-clause, and if
13144 that fails, we back out and return. */
13146 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13149 unsigned saved_num_template_parameter_lists;
13150 bool is_declarator = false;
13153 /* In a member-declarator, the only valid interpretation
13154 of a parenthesis is the start of a
13155 parameter-declaration-clause. (It is invalid to
13156 initialize a static data member with a parenthesized
13157 initializer; only the "=" form of initialization is
13160 cp_parser_parse_tentatively (parser);
13162 /* Consume the `('. */
13163 cp_lexer_consume_token (parser->lexer);
13166 /* If this is going to be an abstract declarator, we're
13167 in a declarator and we can't have default args. */
13168 parser->default_arg_ok_p = false;
13169 parser->in_declarator_p = true;
13172 /* Inside the function parameter list, surrounding
13173 template-parameter-lists do not apply. */
13174 saved_num_template_parameter_lists
13175 = parser->num_template_parameter_lists;
13176 parser->num_template_parameter_lists = 0;
13178 begin_scope (sk_function_parms, NULL_TREE);
13180 /* Parse the parameter-declaration-clause. */
13181 params = cp_parser_parameter_declaration_clause (parser);
13183 parser->num_template_parameter_lists
13184 = saved_num_template_parameter_lists;
13186 /* If all went well, parse the cv-qualifier-seq and the
13187 exception-specification. */
13188 if (member_p || cp_parser_parse_definitely (parser))
13190 cp_cv_quals cv_quals;
13191 tree exception_specification;
13194 is_declarator = true;
13196 if (ctor_dtor_or_conv_p)
13197 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13199 /* Consume the `)'. */
13200 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13202 /* Parse the cv-qualifier-seq. */
13203 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13204 /* And the exception-specification. */
13205 exception_specification
13206 = cp_parser_exception_specification_opt (parser);
13209 = cp_parser_late_return_type_opt (parser);
13211 /* Create the function-declarator. */
13212 declarator = make_call_declarator (declarator,
13215 exception_specification,
13217 /* Any subsequent parameter lists are to do with
13218 return type, so are not those of the declared
13220 parser->default_arg_ok_p = false;
13223 /* Remove the function parms from scope. */
13224 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13225 pop_binding (DECL_NAME (t), t);
13229 /* Repeat the main loop. */
13233 /* If this is the first, we can try a parenthesized
13237 bool saved_in_type_id_in_expr_p;
13239 parser->default_arg_ok_p = saved_default_arg_ok_p;
13240 parser->in_declarator_p = saved_in_declarator_p;
13242 /* Consume the `('. */
13243 cp_lexer_consume_token (parser->lexer);
13244 /* Parse the nested declarator. */
13245 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13246 parser->in_type_id_in_expr_p = true;
13248 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13249 /*parenthesized_p=*/NULL,
13251 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13253 /* Expect a `)'. */
13254 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13255 declarator = cp_error_declarator;
13256 if (declarator == cp_error_declarator)
13259 goto handle_declarator;
13261 /* Otherwise, we must be done. */
13265 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13266 && token->type == CPP_OPEN_SQUARE)
13268 /* Parse an array-declarator. */
13271 if (ctor_dtor_or_conv_p)
13272 *ctor_dtor_or_conv_p = 0;
13275 parser->default_arg_ok_p = false;
13276 parser->in_declarator_p = true;
13277 /* Consume the `['. */
13278 cp_lexer_consume_token (parser->lexer);
13279 /* Peek at the next token. */
13280 token = cp_lexer_peek_token (parser->lexer);
13281 /* If the next token is `]', then there is no
13282 constant-expression. */
13283 if (token->type != CPP_CLOSE_SQUARE)
13285 bool non_constant_p;
13288 = cp_parser_constant_expression (parser,
13289 /*allow_non_constant=*/true,
13291 if (!non_constant_p)
13292 bounds = fold_non_dependent_expr (bounds);
13293 else if (processing_template_decl)
13295 /* Remember this wasn't a constant-expression. */
13296 bounds = build_nop (TREE_TYPE (bounds), bounds);
13297 TREE_SIDE_EFFECTS (bounds) = 1;
13300 /* Normally, the array bound must be an integral constant
13301 expression. However, as an extension, we allow VLAs
13302 in function scopes. */
13303 else if (!parser->in_function_body)
13305 error ("%Harray bound is not an integer constant",
13307 bounds = error_mark_node;
13311 bounds = NULL_TREE;
13312 /* Look for the closing `]'. */
13313 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13315 declarator = cp_error_declarator;
13319 declarator = make_array_declarator (declarator, bounds);
13321 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13323 tree qualifying_scope;
13324 tree unqualified_name;
13325 special_function_kind sfk;
13327 bool pack_expansion_p = false;
13328 cp_token *declarator_id_start_token;
13330 /* Parse a declarator-id */
13331 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13334 cp_parser_parse_tentatively (parser);
13336 /* If we see an ellipsis, we should be looking at a
13338 if (token->type == CPP_ELLIPSIS)
13340 /* Consume the `...' */
13341 cp_lexer_consume_token (parser->lexer);
13343 pack_expansion_p = true;
13347 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13349 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13350 qualifying_scope = parser->scope;
13355 if (!unqualified_name && pack_expansion_p)
13357 /* Check whether an error occurred. */
13358 okay = !cp_parser_error_occurred (parser);
13360 /* We already consumed the ellipsis to mark a
13361 parameter pack, but we have no way to report it,
13362 so abort the tentative parse. We will be exiting
13363 immediately anyway. */
13364 cp_parser_abort_tentative_parse (parser);
13367 okay = cp_parser_parse_definitely (parser);
13370 unqualified_name = error_mark_node;
13371 else if (unqualified_name
13372 && (qualifying_scope
13373 || (TREE_CODE (unqualified_name)
13374 != IDENTIFIER_NODE)))
13376 cp_parser_error (parser, "expected unqualified-id");
13377 unqualified_name = error_mark_node;
13381 if (!unqualified_name)
13383 if (unqualified_name == error_mark_node)
13385 declarator = cp_error_declarator;
13386 pack_expansion_p = false;
13387 declarator->parameter_pack_p = false;
13391 if (qualifying_scope && at_namespace_scope_p ()
13392 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13394 /* In the declaration of a member of a template class
13395 outside of the class itself, the SCOPE will sometimes
13396 be a TYPENAME_TYPE. For example, given:
13398 template <typename T>
13399 int S<T>::R::i = 3;
13401 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13402 this context, we must resolve S<T>::R to an ordinary
13403 type, rather than a typename type.
13405 The reason we normally avoid resolving TYPENAME_TYPEs
13406 is that a specialization of `S' might render
13407 `S<T>::R' not a type. However, if `S' is
13408 specialized, then this `i' will not be used, so there
13409 is no harm in resolving the types here. */
13412 /* Resolve the TYPENAME_TYPE. */
13413 type = resolve_typename_type (qualifying_scope,
13414 /*only_current_p=*/false);
13415 /* If that failed, the declarator is invalid. */
13416 if (TREE_CODE (type) == TYPENAME_TYPE)
13417 error ("%H%<%T::%E%> is not a type",
13418 &declarator_id_start_token->location,
13419 TYPE_CONTEXT (qualifying_scope),
13420 TYPE_IDENTIFIER (qualifying_scope));
13421 qualifying_scope = type;
13426 if (unqualified_name)
13430 if (qualifying_scope
13431 && CLASS_TYPE_P (qualifying_scope))
13432 class_type = qualifying_scope;
13434 class_type = current_class_type;
13436 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13438 tree name_type = TREE_TYPE (unqualified_name);
13439 if (class_type && same_type_p (name_type, class_type))
13441 if (qualifying_scope
13442 && CLASSTYPE_USE_TEMPLATE (name_type))
13444 error ("%Hinvalid use of constructor as a template",
13445 &declarator_id_start_token->location);
13446 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13447 "name the constructor in a qualified name",
13449 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13450 class_type, name_type);
13451 declarator = cp_error_declarator;
13455 unqualified_name = constructor_name (class_type);
13459 /* We do not attempt to print the declarator
13460 here because we do not have enough
13461 information about its original syntactic
13463 cp_parser_error (parser, "invalid declarator");
13464 declarator = cp_error_declarator;
13471 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13472 sfk = sfk_destructor;
13473 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13474 sfk = sfk_conversion;
13475 else if (/* There's no way to declare a constructor
13476 for an anonymous type, even if the type
13477 got a name for linkage purposes. */
13478 !TYPE_WAS_ANONYMOUS (class_type)
13479 && constructor_name_p (unqualified_name,
13482 unqualified_name = constructor_name (class_type);
13483 sfk = sfk_constructor;
13486 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13487 *ctor_dtor_or_conv_p = -1;
13490 declarator = make_id_declarator (qualifying_scope,
13493 declarator->id_loc = token->location;
13494 declarator->parameter_pack_p = pack_expansion_p;
13496 if (pack_expansion_p)
13497 maybe_warn_variadic_templates ();
13499 handle_declarator:;
13500 scope = get_scope_of_declarator (declarator);
13502 /* Any names that appear after the declarator-id for a
13503 member are looked up in the containing scope. */
13504 pushed_scope = push_scope (scope);
13505 parser->in_declarator_p = true;
13506 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13507 || (declarator && declarator->kind == cdk_id))
13508 /* Default args are only allowed on function
13510 parser->default_arg_ok_p = saved_default_arg_ok_p;
13512 parser->default_arg_ok_p = false;
13521 /* For an abstract declarator, we might wind up with nothing at this
13522 point. That's an error; the declarator is not optional. */
13524 cp_parser_error (parser, "expected declarator");
13526 /* If we entered a scope, we must exit it now. */
13528 pop_scope (pushed_scope);
13530 parser->default_arg_ok_p = saved_default_arg_ok_p;
13531 parser->in_declarator_p = saved_in_declarator_p;
13536 /* Parse a ptr-operator.
13539 * cv-qualifier-seq [opt]
13541 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13546 & cv-qualifier-seq [opt]
13548 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13549 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13550 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13551 filled in with the TYPE containing the member. *CV_QUALS is
13552 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13553 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13554 Note that the tree codes returned by this function have nothing
13555 to do with the types of trees that will be eventually be created
13556 to represent the pointer or reference type being parsed. They are
13557 just constants with suggestive names. */
13558 static enum tree_code
13559 cp_parser_ptr_operator (cp_parser* parser,
13561 cp_cv_quals *cv_quals)
13563 enum tree_code code = ERROR_MARK;
13566 /* Assume that it's not a pointer-to-member. */
13568 /* And that there are no cv-qualifiers. */
13569 *cv_quals = TYPE_UNQUALIFIED;
13571 /* Peek at the next token. */
13572 token = cp_lexer_peek_token (parser->lexer);
13574 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13575 if (token->type == CPP_MULT)
13576 code = INDIRECT_REF;
13577 else if (token->type == CPP_AND)
13579 else if ((cxx_dialect != cxx98) &&
13580 token->type == CPP_AND_AND) /* C++0x only */
13581 code = NON_LVALUE_EXPR;
13583 if (code != ERROR_MARK)
13585 /* Consume the `*', `&' or `&&'. */
13586 cp_lexer_consume_token (parser->lexer);
13588 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13589 `&', if we are allowing GNU extensions. (The only qualifier
13590 that can legally appear after `&' is `restrict', but that is
13591 enforced during semantic analysis. */
13592 if (code == INDIRECT_REF
13593 || cp_parser_allow_gnu_extensions_p (parser))
13594 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13598 /* Try the pointer-to-member case. */
13599 cp_parser_parse_tentatively (parser);
13600 /* Look for the optional `::' operator. */
13601 cp_parser_global_scope_opt (parser,
13602 /*current_scope_valid_p=*/false);
13603 /* Look for the nested-name specifier. */
13604 token = cp_lexer_peek_token (parser->lexer);
13605 cp_parser_nested_name_specifier (parser,
13606 /*typename_keyword_p=*/false,
13607 /*check_dependency_p=*/true,
13609 /*is_declaration=*/false);
13610 /* If we found it, and the next token is a `*', then we are
13611 indeed looking at a pointer-to-member operator. */
13612 if (!cp_parser_error_occurred (parser)
13613 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13615 /* Indicate that the `*' operator was used. */
13616 code = INDIRECT_REF;
13618 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13619 error ("%H%qD is a namespace", &token->location, parser->scope);
13622 /* The type of which the member is a member is given by the
13624 *type = parser->scope;
13625 /* The next name will not be qualified. */
13626 parser->scope = NULL_TREE;
13627 parser->qualifying_scope = NULL_TREE;
13628 parser->object_scope = NULL_TREE;
13629 /* Look for the optional cv-qualifier-seq. */
13630 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13633 /* If that didn't work we don't have a ptr-operator. */
13634 if (!cp_parser_parse_definitely (parser))
13635 cp_parser_error (parser, "expected ptr-operator");
13641 /* Parse an (optional) cv-qualifier-seq.
13644 cv-qualifier cv-qualifier-seq [opt]
13655 Returns a bitmask representing the cv-qualifiers. */
13658 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13660 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13665 cp_cv_quals cv_qualifier;
13667 /* Peek at the next token. */
13668 token = cp_lexer_peek_token (parser->lexer);
13669 /* See if it's a cv-qualifier. */
13670 switch (token->keyword)
13673 cv_qualifier = TYPE_QUAL_CONST;
13677 cv_qualifier = TYPE_QUAL_VOLATILE;
13681 cv_qualifier = TYPE_QUAL_RESTRICT;
13685 cv_qualifier = TYPE_UNQUALIFIED;
13692 if (cv_quals & cv_qualifier)
13694 error ("%Hduplicate cv-qualifier", &token->location);
13695 cp_lexer_purge_token (parser->lexer);
13699 cp_lexer_consume_token (parser->lexer);
13700 cv_quals |= cv_qualifier;
13707 /* Parse a late-specified return type, if any. This is not a separate
13708 non-terminal, but part of a function declarator, which looks like
13712 Returns the type indicated by the type-id. */
13715 cp_parser_late_return_type_opt (cp_parser* parser)
13719 /* Peek at the next token. */
13720 token = cp_lexer_peek_token (parser->lexer);
13721 /* A late-specified return type is indicated by an initial '->'. */
13722 if (token->type != CPP_DEREF)
13725 /* Consume the ->. */
13726 cp_lexer_consume_token (parser->lexer);
13728 return cp_parser_type_id (parser);
13731 /* Parse a declarator-id.
13735 :: [opt] nested-name-specifier [opt] type-name
13737 In the `id-expression' case, the value returned is as for
13738 cp_parser_id_expression if the id-expression was an unqualified-id.
13739 If the id-expression was a qualified-id, then a SCOPE_REF is
13740 returned. The first operand is the scope (either a NAMESPACE_DECL
13741 or TREE_TYPE), but the second is still just a representation of an
13745 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13748 /* The expression must be an id-expression. Assume that qualified
13749 names are the names of types so that:
13752 int S<T>::R::i = 3;
13754 will work; we must treat `S<T>::R' as the name of a type.
13755 Similarly, assume that qualified names are templates, where
13759 int S<T>::R<T>::i = 3;
13762 id = cp_parser_id_expression (parser,
13763 /*template_keyword_p=*/false,
13764 /*check_dependency_p=*/false,
13765 /*template_p=*/NULL,
13766 /*declarator_p=*/true,
13768 if (id && BASELINK_P (id))
13769 id = BASELINK_FUNCTIONS (id);
13773 /* Parse a type-id.
13776 type-specifier-seq abstract-declarator [opt]
13778 Returns the TYPE specified. */
13781 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13783 cp_decl_specifier_seq type_specifier_seq;
13784 cp_declarator *abstract_declarator;
13786 /* Parse the type-specifier-seq. */
13787 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13788 &type_specifier_seq);
13789 if (type_specifier_seq.type == error_mark_node)
13790 return error_mark_node;
13792 /* There might or might not be an abstract declarator. */
13793 cp_parser_parse_tentatively (parser);
13794 /* Look for the declarator. */
13795 abstract_declarator
13796 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13797 /*parenthesized_p=*/NULL,
13798 /*member_p=*/false);
13799 /* Check to see if there really was a declarator. */
13800 if (!cp_parser_parse_definitely (parser))
13801 abstract_declarator = NULL;
13803 if (type_specifier_seq.type
13804 && type_uses_auto (type_specifier_seq.type))
13806 error ("invalid use of %<auto%>");
13807 return error_mark_node;
13810 return groktypename (&type_specifier_seq, abstract_declarator,
13814 static tree cp_parser_type_id (cp_parser *parser)
13816 return cp_parser_type_id_1 (parser, false);
13819 static tree cp_parser_template_type_arg (cp_parser *parser)
13821 return cp_parser_type_id_1 (parser, true);
13824 /* Parse a type-specifier-seq.
13826 type-specifier-seq:
13827 type-specifier type-specifier-seq [opt]
13831 type-specifier-seq:
13832 attributes type-specifier-seq [opt]
13834 If IS_CONDITION is true, we are at the start of a "condition",
13835 e.g., we've just seen "if (".
13837 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13840 cp_parser_type_specifier_seq (cp_parser* parser,
13842 cp_decl_specifier_seq *type_specifier_seq)
13844 bool seen_type_specifier = false;
13845 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13846 cp_token *start_token = NULL;
13848 /* Clear the TYPE_SPECIFIER_SEQ. */
13849 clear_decl_specs (type_specifier_seq);
13851 /* Parse the type-specifiers and attributes. */
13854 tree type_specifier;
13855 bool is_cv_qualifier;
13857 /* Check for attributes first. */
13858 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13860 type_specifier_seq->attributes =
13861 chainon (type_specifier_seq->attributes,
13862 cp_parser_attributes_opt (parser));
13866 /* record the token of the beginning of the type specifier seq,
13867 for error reporting purposes*/
13869 start_token = cp_lexer_peek_token (parser->lexer);
13871 /* Look for the type-specifier. */
13872 type_specifier = cp_parser_type_specifier (parser,
13874 type_specifier_seq,
13875 /*is_declaration=*/false,
13878 if (!type_specifier)
13880 /* If the first type-specifier could not be found, this is not a
13881 type-specifier-seq at all. */
13882 if (!seen_type_specifier)
13884 cp_parser_error (parser, "expected type-specifier");
13885 type_specifier_seq->type = error_mark_node;
13888 /* If subsequent type-specifiers could not be found, the
13889 type-specifier-seq is complete. */
13893 seen_type_specifier = true;
13894 /* The standard says that a condition can be:
13896 type-specifier-seq declarator = assignment-expression
13903 we should treat the "S" as a declarator, not as a
13904 type-specifier. The standard doesn't say that explicitly for
13905 type-specifier-seq, but it does say that for
13906 decl-specifier-seq in an ordinary declaration. Perhaps it
13907 would be clearer just to allow a decl-specifier-seq here, and
13908 then add a semantic restriction that if any decl-specifiers
13909 that are not type-specifiers appear, the program is invalid. */
13910 if (is_condition && !is_cv_qualifier)
13911 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13914 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13917 /* Parse a parameter-declaration-clause.
13919 parameter-declaration-clause:
13920 parameter-declaration-list [opt] ... [opt]
13921 parameter-declaration-list , ...
13923 Returns a representation for the parameter declarations. A return
13924 value of NULL indicates a parameter-declaration-clause consisting
13925 only of an ellipsis. */
13928 cp_parser_parameter_declaration_clause (cp_parser* parser)
13935 /* Peek at the next token. */
13936 token = cp_lexer_peek_token (parser->lexer);
13937 /* Check for trivial parameter-declaration-clauses. */
13938 if (token->type == CPP_ELLIPSIS)
13940 /* Consume the `...' token. */
13941 cp_lexer_consume_token (parser->lexer);
13944 else if (token->type == CPP_CLOSE_PAREN)
13945 /* There are no parameters. */
13947 #ifndef NO_IMPLICIT_EXTERN_C
13948 if (in_system_header && current_class_type == NULL
13949 && current_lang_name == lang_name_c)
13953 return void_list_node;
13955 /* Check for `(void)', too, which is a special case. */
13956 else if (token->keyword == RID_VOID
13957 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13958 == CPP_CLOSE_PAREN))
13960 /* Consume the `void' token. */
13961 cp_lexer_consume_token (parser->lexer);
13962 /* There are no parameters. */
13963 return void_list_node;
13966 /* Parse the parameter-declaration-list. */
13967 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13968 /* If a parse error occurred while parsing the
13969 parameter-declaration-list, then the entire
13970 parameter-declaration-clause is erroneous. */
13974 /* Peek at the next token. */
13975 token = cp_lexer_peek_token (parser->lexer);
13976 /* If it's a `,', the clause should terminate with an ellipsis. */
13977 if (token->type == CPP_COMMA)
13979 /* Consume the `,'. */
13980 cp_lexer_consume_token (parser->lexer);
13981 /* Expect an ellipsis. */
13983 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13985 /* It might also be `...' if the optional trailing `,' was
13987 else if (token->type == CPP_ELLIPSIS)
13989 /* Consume the `...' token. */
13990 cp_lexer_consume_token (parser->lexer);
13991 /* And remember that we saw it. */
13995 ellipsis_p = false;
13997 /* Finish the parameter list. */
13999 parameters = chainon (parameters, void_list_node);
14004 /* Parse a parameter-declaration-list.
14006 parameter-declaration-list:
14007 parameter-declaration
14008 parameter-declaration-list , parameter-declaration
14010 Returns a representation of the parameter-declaration-list, as for
14011 cp_parser_parameter_declaration_clause. However, the
14012 `void_list_node' is never appended to the list. Upon return,
14013 *IS_ERROR will be true iff an error occurred. */
14016 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14018 tree parameters = NULL_TREE;
14019 tree *tail = ¶meters;
14020 bool saved_in_unbraced_linkage_specification_p;
14022 /* Assume all will go well. */
14024 /* The special considerations that apply to a function within an
14025 unbraced linkage specifications do not apply to the parameters
14026 to the function. */
14027 saved_in_unbraced_linkage_specification_p
14028 = parser->in_unbraced_linkage_specification_p;
14029 parser->in_unbraced_linkage_specification_p = false;
14031 /* Look for more parameters. */
14034 cp_parameter_declarator *parameter;
14035 tree decl = error_mark_node;
14036 bool parenthesized_p;
14037 /* Parse the parameter. */
14039 = cp_parser_parameter_declaration (parser,
14040 /*template_parm_p=*/false,
14043 /* We don't know yet if the enclosing context is deprecated, so wait
14044 and warn in grokparms if appropriate. */
14045 deprecated_state = DEPRECATED_SUPPRESS;
14048 decl = grokdeclarator (parameter->declarator,
14049 ¶meter->decl_specifiers,
14051 parameter->default_argument != NULL_TREE,
14052 ¶meter->decl_specifiers.attributes);
14054 deprecated_state = DEPRECATED_NORMAL;
14056 /* If a parse error occurred parsing the parameter declaration,
14057 then the entire parameter-declaration-list is erroneous. */
14058 if (decl == error_mark_node)
14061 parameters = error_mark_node;
14065 if (parameter->decl_specifiers.attributes)
14066 cplus_decl_attributes (&decl,
14067 parameter->decl_specifiers.attributes,
14069 if (DECL_NAME (decl))
14070 decl = pushdecl (decl);
14072 /* Add the new parameter to the list. */
14073 *tail = build_tree_list (parameter->default_argument, decl);
14074 tail = &TREE_CHAIN (*tail);
14076 /* Peek at the next token. */
14077 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14078 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14079 /* These are for Objective-C++ */
14080 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14081 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14082 /* The parameter-declaration-list is complete. */
14084 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14088 /* Peek at the next token. */
14089 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14090 /* If it's an ellipsis, then the list is complete. */
14091 if (token->type == CPP_ELLIPSIS)
14093 /* Otherwise, there must be more parameters. Consume the
14095 cp_lexer_consume_token (parser->lexer);
14096 /* When parsing something like:
14098 int i(float f, double d)
14100 we can tell after seeing the declaration for "f" that we
14101 are not looking at an initialization of a variable "i",
14102 but rather at the declaration of a function "i".
14104 Due to the fact that the parsing of template arguments
14105 (as specified to a template-id) requires backtracking we
14106 cannot use this technique when inside a template argument
14108 if (!parser->in_template_argument_list_p
14109 && !parser->in_type_id_in_expr_p
14110 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14111 /* However, a parameter-declaration of the form
14112 "foat(f)" (which is a valid declaration of a
14113 parameter "f") can also be interpreted as an
14114 expression (the conversion of "f" to "float"). */
14115 && !parenthesized_p)
14116 cp_parser_commit_to_tentative_parse (parser);
14120 cp_parser_error (parser, "expected %<,%> or %<...%>");
14121 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14122 cp_parser_skip_to_closing_parenthesis (parser,
14123 /*recovering=*/true,
14124 /*or_comma=*/false,
14125 /*consume_paren=*/false);
14130 parser->in_unbraced_linkage_specification_p
14131 = saved_in_unbraced_linkage_specification_p;
14136 /* Parse a parameter declaration.
14138 parameter-declaration:
14139 decl-specifier-seq ... [opt] declarator
14140 decl-specifier-seq declarator = assignment-expression
14141 decl-specifier-seq ... [opt] abstract-declarator [opt]
14142 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14144 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14145 declares a template parameter. (In that case, a non-nested `>'
14146 token encountered during the parsing of the assignment-expression
14147 is not interpreted as a greater-than operator.)
14149 Returns a representation of the parameter, or NULL if an error
14150 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14151 true iff the declarator is of the form "(p)". */
14153 static cp_parameter_declarator *
14154 cp_parser_parameter_declaration (cp_parser *parser,
14155 bool template_parm_p,
14156 bool *parenthesized_p)
14158 int declares_class_or_enum;
14159 bool greater_than_is_operator_p;
14160 cp_decl_specifier_seq decl_specifiers;
14161 cp_declarator *declarator;
14162 tree default_argument;
14163 cp_token *token = NULL, *declarator_token_start = NULL;
14164 const char *saved_message;
14166 /* In a template parameter, `>' is not an operator.
14170 When parsing a default template-argument for a non-type
14171 template-parameter, the first non-nested `>' is taken as the end
14172 of the template parameter-list rather than a greater-than
14174 greater_than_is_operator_p = !template_parm_p;
14176 /* Type definitions may not appear in parameter types. */
14177 saved_message = parser->type_definition_forbidden_message;
14178 parser->type_definition_forbidden_message
14179 = "types may not be defined in parameter types";
14181 /* Parse the declaration-specifiers. */
14182 cp_parser_decl_specifier_seq (parser,
14183 CP_PARSER_FLAGS_NONE,
14185 &declares_class_or_enum);
14186 /* If an error occurred, there's no reason to attempt to parse the
14187 rest of the declaration. */
14188 if (cp_parser_error_occurred (parser))
14190 parser->type_definition_forbidden_message = saved_message;
14194 /* Peek at the next token. */
14195 token = cp_lexer_peek_token (parser->lexer);
14197 /* If the next token is a `)', `,', `=', `>', or `...', then there
14198 is no declarator. However, when variadic templates are enabled,
14199 there may be a declarator following `...'. */
14200 if (token->type == CPP_CLOSE_PAREN
14201 || token->type == CPP_COMMA
14202 || token->type == CPP_EQ
14203 || token->type == CPP_GREATER)
14206 if (parenthesized_p)
14207 *parenthesized_p = false;
14209 /* Otherwise, there should be a declarator. */
14212 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14213 parser->default_arg_ok_p = false;
14215 /* After seeing a decl-specifier-seq, if the next token is not a
14216 "(", there is no possibility that the code is a valid
14217 expression. Therefore, if parsing tentatively, we commit at
14219 if (!parser->in_template_argument_list_p
14220 /* In an expression context, having seen:
14224 we cannot be sure whether we are looking at a
14225 function-type (taking a "char" as a parameter) or a cast
14226 of some object of type "char" to "int". */
14227 && !parser->in_type_id_in_expr_p
14228 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14229 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14230 cp_parser_commit_to_tentative_parse (parser);
14231 /* Parse the declarator. */
14232 declarator_token_start = token;
14233 declarator = cp_parser_declarator (parser,
14234 CP_PARSER_DECLARATOR_EITHER,
14235 /*ctor_dtor_or_conv_p=*/NULL,
14237 /*member_p=*/false);
14238 parser->default_arg_ok_p = saved_default_arg_ok_p;
14239 /* After the declarator, allow more attributes. */
14240 decl_specifiers.attributes
14241 = chainon (decl_specifiers.attributes,
14242 cp_parser_attributes_opt (parser));
14245 /* If the next token is an ellipsis, and we have not seen a
14246 declarator name, and the type of the declarator contains parameter
14247 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14248 a parameter pack expansion expression. Otherwise, leave the
14249 ellipsis for a C-style variadic function. */
14250 token = cp_lexer_peek_token (parser->lexer);
14251 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14253 tree type = decl_specifiers.type;
14255 if (type && DECL_P (type))
14256 type = TREE_TYPE (type);
14259 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14260 && declarator_can_be_parameter_pack (declarator)
14261 && (!declarator || !declarator->parameter_pack_p)
14262 && uses_parameter_packs (type))
14264 /* Consume the `...'. */
14265 cp_lexer_consume_token (parser->lexer);
14266 maybe_warn_variadic_templates ();
14268 /* Build a pack expansion type */
14270 declarator->parameter_pack_p = true;
14272 decl_specifiers.type = make_pack_expansion (type);
14276 /* The restriction on defining new types applies only to the type
14277 of the parameter, not to the default argument. */
14278 parser->type_definition_forbidden_message = saved_message;
14280 /* If the next token is `=', then process a default argument. */
14281 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14283 /* Consume the `='. */
14284 cp_lexer_consume_token (parser->lexer);
14286 /* If we are defining a class, then the tokens that make up the
14287 default argument must be saved and processed later. */
14288 if (!template_parm_p && at_class_scope_p ()
14289 && TYPE_BEING_DEFINED (current_class_type))
14291 unsigned depth = 0;
14292 int maybe_template_id = 0;
14293 cp_token *first_token;
14296 /* Add tokens until we have processed the entire default
14297 argument. We add the range [first_token, token). */
14298 first_token = cp_lexer_peek_token (parser->lexer);
14303 /* Peek at the next token. */
14304 token = cp_lexer_peek_token (parser->lexer);
14305 /* What we do depends on what token we have. */
14306 switch (token->type)
14308 /* In valid code, a default argument must be
14309 immediately followed by a `,' `)', or `...'. */
14311 if (depth == 0 && maybe_template_id)
14313 /* If we've seen a '<', we might be in a
14314 template-argument-list. Until Core issue 325 is
14315 resolved, we don't know how this situation ought
14316 to be handled, so try to DTRT. We check whether
14317 what comes after the comma is a valid parameter
14318 declaration list. If it is, then the comma ends
14319 the default argument; otherwise the default
14320 argument continues. */
14321 bool error = false;
14323 /* Set ITALP so cp_parser_parameter_declaration_list
14324 doesn't decide to commit to this parse. */
14325 bool saved_italp = parser->in_template_argument_list_p;
14326 parser->in_template_argument_list_p = true;
14328 cp_parser_parse_tentatively (parser);
14329 cp_lexer_consume_token (parser->lexer);
14330 cp_parser_parameter_declaration_list (parser, &error);
14331 if (!cp_parser_error_occurred (parser) && !error)
14333 cp_parser_abort_tentative_parse (parser);
14335 parser->in_template_argument_list_p = saved_italp;
14338 case CPP_CLOSE_PAREN:
14340 /* If we run into a non-nested `;', `}', or `]',
14341 then the code is invalid -- but the default
14342 argument is certainly over. */
14343 case CPP_SEMICOLON:
14344 case CPP_CLOSE_BRACE:
14345 case CPP_CLOSE_SQUARE:
14348 /* Update DEPTH, if necessary. */
14349 else if (token->type == CPP_CLOSE_PAREN
14350 || token->type == CPP_CLOSE_BRACE
14351 || token->type == CPP_CLOSE_SQUARE)
14355 case CPP_OPEN_PAREN:
14356 case CPP_OPEN_SQUARE:
14357 case CPP_OPEN_BRACE:
14363 /* This might be the comparison operator, or it might
14364 start a template argument list. */
14365 ++maybe_template_id;
14369 if (cxx_dialect == cxx98)
14371 /* Fall through for C++0x, which treats the `>>'
14372 operator like two `>' tokens in certain
14378 /* This might be an operator, or it might close a
14379 template argument list. But if a previous '<'
14380 started a template argument list, this will have
14381 closed it, so we can't be in one anymore. */
14382 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14383 if (maybe_template_id < 0)
14384 maybe_template_id = 0;
14388 /* If we run out of tokens, issue an error message. */
14390 case CPP_PRAGMA_EOL:
14391 error ("%Hfile ends in default argument", &token->location);
14397 /* In these cases, we should look for template-ids.
14398 For example, if the default argument is
14399 `X<int, double>()', we need to do name lookup to
14400 figure out whether or not `X' is a template; if
14401 so, the `,' does not end the default argument.
14403 That is not yet done. */
14410 /* If we've reached the end, stop. */
14414 /* Add the token to the token block. */
14415 token = cp_lexer_consume_token (parser->lexer);
14418 /* Create a DEFAULT_ARG to represent the unparsed default
14420 default_argument = make_node (DEFAULT_ARG);
14421 DEFARG_TOKENS (default_argument)
14422 = cp_token_cache_new (first_token, token);
14423 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14425 /* Outside of a class definition, we can just parse the
14426 assignment-expression. */
14429 token = cp_lexer_peek_token (parser->lexer);
14431 = cp_parser_default_argument (parser, template_parm_p);
14434 if (!parser->default_arg_ok_p)
14436 if (flag_permissive)
14437 warning (0, "deprecated use of default argument for parameter of non-function");
14440 error ("%Hdefault arguments are only "
14441 "permitted for function parameters",
14443 default_argument = NULL_TREE;
14446 else if ((declarator && declarator->parameter_pack_p)
14447 || (decl_specifiers.type
14448 && PACK_EXPANSION_P (decl_specifiers.type)))
14450 const char* kind = template_parm_p? "template " : "";
14452 /* Find the name of the parameter pack. */
14453 cp_declarator *id_declarator = declarator;
14454 while (id_declarator && id_declarator->kind != cdk_id)
14455 id_declarator = id_declarator->declarator;
14457 if (id_declarator && id_declarator->kind == cdk_id)
14458 error ("%H%sparameter pack %qD cannot have a default argument",
14459 &declarator_token_start->location,
14460 kind, id_declarator->u.id.unqualified_name);
14462 error ("%H%sparameter pack cannot have a default argument",
14463 &declarator_token_start->location, kind);
14465 default_argument = NULL_TREE;
14469 default_argument = NULL_TREE;
14471 return make_parameter_declarator (&decl_specifiers,
14476 /* Parse a default argument and return it.
14478 TEMPLATE_PARM_P is true if this is a default argument for a
14479 non-type template parameter. */
14481 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14483 tree default_argument = NULL_TREE;
14484 bool saved_greater_than_is_operator_p;
14485 bool saved_local_variables_forbidden_p;
14487 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14489 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14490 parser->greater_than_is_operator_p = !template_parm_p;
14491 /* Local variable names (and the `this' keyword) may not
14492 appear in a default argument. */
14493 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14494 parser->local_variables_forbidden_p = true;
14495 /* The default argument expression may cause implicitly
14496 defined member functions to be synthesized, which will
14497 result in garbage collection. We must treat this
14498 situation as if we were within the body of function so as
14499 to avoid collecting live data on the stack. */
14501 /* Parse the assignment-expression. */
14502 if (template_parm_p)
14503 push_deferring_access_checks (dk_no_deferred);
14505 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14506 if (template_parm_p)
14507 pop_deferring_access_checks ();
14508 /* Restore saved state. */
14510 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14511 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14513 return default_argument;
14516 /* Parse a function-body.
14519 compound_statement */
14522 cp_parser_function_body (cp_parser *parser)
14524 cp_parser_compound_statement (parser, NULL, false);
14527 /* Parse a ctor-initializer-opt followed by a function-body. Return
14528 true if a ctor-initializer was present. */
14531 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14534 bool ctor_initializer_p;
14536 /* Begin the function body. */
14537 body = begin_function_body ();
14538 /* Parse the optional ctor-initializer. */
14539 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14540 /* Parse the function-body. */
14541 cp_parser_function_body (parser);
14542 /* Finish the function body. */
14543 finish_function_body (body);
14545 return ctor_initializer_p;
14548 /* Parse an initializer.
14551 = initializer-clause
14552 ( expression-list )
14554 Returns an expression representing the initializer. If no
14555 initializer is present, NULL_TREE is returned.
14557 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14558 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14559 set to TRUE if there is no initializer present. If there is an
14560 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14561 is set to true; otherwise it is set to false. */
14564 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14565 bool* non_constant_p)
14570 /* Peek at the next token. */
14571 token = cp_lexer_peek_token (parser->lexer);
14573 /* Let our caller know whether or not this initializer was
14575 *is_direct_init = (token->type != CPP_EQ);
14576 /* Assume that the initializer is constant. */
14577 *non_constant_p = false;
14579 if (token->type == CPP_EQ)
14581 /* Consume the `='. */
14582 cp_lexer_consume_token (parser->lexer);
14583 /* Parse the initializer-clause. */
14584 init = cp_parser_initializer_clause (parser, non_constant_p);
14586 else if (token->type == CPP_OPEN_PAREN)
14587 init = cp_parser_parenthesized_expression_list (parser, false,
14589 /*allow_expansion_p=*/true,
14591 else if (token->type == CPP_OPEN_BRACE)
14593 maybe_warn_cpp0x ("extended initializer lists");
14594 init = cp_parser_braced_list (parser, non_constant_p);
14595 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14599 /* Anything else is an error. */
14600 cp_parser_error (parser, "expected initializer");
14601 init = error_mark_node;
14607 /* Parse an initializer-clause.
14609 initializer-clause:
14610 assignment-expression
14613 Returns an expression representing the initializer.
14615 If the `assignment-expression' production is used the value
14616 returned is simply a representation for the expression.
14618 Otherwise, calls cp_parser_braced_list. */
14621 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14625 /* Assume the expression is constant. */
14626 *non_constant_p = false;
14628 /* If it is not a `{', then we are looking at an
14629 assignment-expression. */
14630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14633 = cp_parser_constant_expression (parser,
14634 /*allow_non_constant_p=*/true,
14636 if (!*non_constant_p)
14637 initializer = fold_non_dependent_expr (initializer);
14640 initializer = cp_parser_braced_list (parser, non_constant_p);
14642 return initializer;
14645 /* Parse a brace-enclosed initializer list.
14648 { initializer-list , [opt] }
14651 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14652 the elements of the initializer-list (or NULL, if the last
14653 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14654 NULL_TREE. There is no way to detect whether or not the optional
14655 trailing `,' was provided. NON_CONSTANT_P is as for
14656 cp_parser_initializer. */
14659 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14663 /* Consume the `{' token. */
14664 cp_lexer_consume_token (parser->lexer);
14665 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14666 initializer = make_node (CONSTRUCTOR);
14667 /* If it's not a `}', then there is a non-trivial initializer. */
14668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14670 /* Parse the initializer list. */
14671 CONSTRUCTOR_ELTS (initializer)
14672 = cp_parser_initializer_list (parser, non_constant_p);
14673 /* A trailing `,' token is allowed. */
14674 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14675 cp_lexer_consume_token (parser->lexer);
14677 /* Now, there should be a trailing `}'. */
14678 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14679 TREE_TYPE (initializer) = init_list_type_node;
14680 return initializer;
14683 /* Parse an initializer-list.
14686 initializer-clause ... [opt]
14687 initializer-list , initializer-clause ... [opt]
14692 identifier : initializer-clause
14693 initializer-list, identifier : initializer-clause
14695 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14696 for the initializer. If the INDEX of the elt is non-NULL, it is the
14697 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14698 as for cp_parser_initializer. */
14700 static VEC(constructor_elt,gc) *
14701 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14703 VEC(constructor_elt,gc) *v = NULL;
14705 /* Assume all of the expressions are constant. */
14706 *non_constant_p = false;
14708 /* Parse the rest of the list. */
14714 bool clause_non_constant_p;
14716 /* If the next token is an identifier and the following one is a
14717 colon, we are looking at the GNU designated-initializer
14719 if (cp_parser_allow_gnu_extensions_p (parser)
14720 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14721 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14723 /* Warn the user that they are using an extension. */
14724 pedwarn (input_location, OPT_pedantic,
14725 "ISO C++ does not allow designated initializers");
14726 /* Consume the identifier. */
14727 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14728 /* Consume the `:'. */
14729 cp_lexer_consume_token (parser->lexer);
14732 identifier = NULL_TREE;
14734 /* Parse the initializer. */
14735 initializer = cp_parser_initializer_clause (parser,
14736 &clause_non_constant_p);
14737 /* If any clause is non-constant, so is the entire initializer. */
14738 if (clause_non_constant_p)
14739 *non_constant_p = true;
14741 /* If we have an ellipsis, this is an initializer pack
14743 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14745 /* Consume the `...'. */
14746 cp_lexer_consume_token (parser->lexer);
14748 /* Turn the initializer into an initializer expansion. */
14749 initializer = make_pack_expansion (initializer);
14752 /* Add it to the vector. */
14753 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14755 /* If the next token is not a comma, we have reached the end of
14757 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14760 /* Peek at the next token. */
14761 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14762 /* If the next token is a `}', then we're still done. An
14763 initializer-clause can have a trailing `,' after the
14764 initializer-list and before the closing `}'. */
14765 if (token->type == CPP_CLOSE_BRACE)
14768 /* Consume the `,' token. */
14769 cp_lexer_consume_token (parser->lexer);
14775 /* Classes [gram.class] */
14777 /* Parse a class-name.
14783 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14784 to indicate that names looked up in dependent types should be
14785 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14786 keyword has been used to indicate that the name that appears next
14787 is a template. TAG_TYPE indicates the explicit tag given before
14788 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14789 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14790 is the class being defined in a class-head.
14792 Returns the TYPE_DECL representing the class. */
14795 cp_parser_class_name (cp_parser *parser,
14796 bool typename_keyword_p,
14797 bool template_keyword_p,
14798 enum tag_types tag_type,
14799 bool check_dependency_p,
14801 bool is_declaration)
14807 tree identifier = NULL_TREE;
14809 /* All class-names start with an identifier. */
14810 token = cp_lexer_peek_token (parser->lexer);
14811 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14813 cp_parser_error (parser, "expected class-name");
14814 return error_mark_node;
14817 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14818 to a template-id, so we save it here. */
14819 scope = parser->scope;
14820 if (scope == error_mark_node)
14821 return error_mark_node;
14823 /* Any name names a type if we're following the `typename' keyword
14824 in a qualified name where the enclosing scope is type-dependent. */
14825 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14826 && dependent_type_p (scope));
14827 /* Handle the common case (an identifier, but not a template-id)
14829 if (token->type == CPP_NAME
14830 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14832 cp_token *identifier_token;
14835 /* Look for the identifier. */
14836 identifier_token = cp_lexer_peek_token (parser->lexer);
14837 ambiguous_p = identifier_token->ambiguous_p;
14838 identifier = cp_parser_identifier (parser);
14839 /* If the next token isn't an identifier, we are certainly not
14840 looking at a class-name. */
14841 if (identifier == error_mark_node)
14842 decl = error_mark_node;
14843 /* If we know this is a type-name, there's no need to look it
14845 else if (typename_p)
14849 tree ambiguous_decls;
14850 /* If we already know that this lookup is ambiguous, then
14851 we've already issued an error message; there's no reason
14855 cp_parser_simulate_error (parser);
14856 return error_mark_node;
14858 /* If the next token is a `::', then the name must be a type
14861 [basic.lookup.qual]
14863 During the lookup for a name preceding the :: scope
14864 resolution operator, object, function, and enumerator
14865 names are ignored. */
14866 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14867 tag_type = typename_type;
14868 /* Look up the name. */
14869 decl = cp_parser_lookup_name (parser, identifier,
14871 /*is_template=*/false,
14872 /*is_namespace=*/false,
14873 check_dependency_p,
14875 identifier_token->location);
14876 if (ambiguous_decls)
14878 error ("%Hreference to %qD is ambiguous",
14879 &identifier_token->location, identifier);
14880 print_candidates (ambiguous_decls);
14881 if (cp_parser_parsing_tentatively (parser))
14883 identifier_token->ambiguous_p = true;
14884 cp_parser_simulate_error (parser);
14886 return error_mark_node;
14892 /* Try a template-id. */
14893 decl = cp_parser_template_id (parser, template_keyword_p,
14894 check_dependency_p,
14896 if (decl == error_mark_node)
14897 return error_mark_node;
14900 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14902 /* If this is a typename, create a TYPENAME_TYPE. */
14903 if (typename_p && decl != error_mark_node)
14905 decl = make_typename_type (scope, decl, typename_type,
14906 /*complain=*/tf_error);
14907 if (decl != error_mark_node)
14908 decl = TYPE_NAME (decl);
14911 /* Check to see that it is really the name of a class. */
14912 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14913 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14914 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14915 /* Situations like this:
14917 template <typename T> struct A {
14918 typename T::template X<int>::I i;
14921 are problematic. Is `T::template X<int>' a class-name? The
14922 standard does not seem to be definitive, but there is no other
14923 valid interpretation of the following `::'. Therefore, those
14924 names are considered class-names. */
14926 decl = make_typename_type (scope, decl, tag_type, tf_error);
14927 if (decl != error_mark_node)
14928 decl = TYPE_NAME (decl);
14930 else if (TREE_CODE (decl) != TYPE_DECL
14931 || TREE_TYPE (decl) == error_mark_node
14932 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14933 decl = error_mark_node;
14935 if (decl == error_mark_node)
14936 cp_parser_error (parser, "expected class-name");
14937 else if (identifier && !parser->scope)
14938 maybe_note_name_used_in_class (identifier, decl);
14943 /* Parse a class-specifier.
14946 class-head { member-specification [opt] }
14948 Returns the TREE_TYPE representing the class. */
14951 cp_parser_class_specifier (cp_parser* parser)
14954 tree attributes = NULL_TREE;
14955 bool nested_name_specifier_p;
14956 unsigned saved_num_template_parameter_lists;
14957 bool saved_in_function_body;
14958 bool saved_in_unbraced_linkage_specification_p;
14959 tree old_scope = NULL_TREE;
14960 tree scope = NULL_TREE;
14963 push_deferring_access_checks (dk_no_deferred);
14965 /* Parse the class-head. */
14966 type = cp_parser_class_head (parser,
14967 &nested_name_specifier_p,
14970 /* If the class-head was a semantic disaster, skip the entire body
14974 cp_parser_skip_to_end_of_block_or_statement (parser);
14975 pop_deferring_access_checks ();
14976 return error_mark_node;
14979 /* Look for the `{'. */
14980 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14982 pop_deferring_access_checks ();
14983 return error_mark_node;
14986 /* Process the base classes. If they're invalid, skip the
14987 entire class body. */
14988 if (!xref_basetypes (type, bases))
14990 /* Consuming the closing brace yields better error messages
14992 if (cp_parser_skip_to_closing_brace (parser))
14993 cp_lexer_consume_token (parser->lexer);
14994 pop_deferring_access_checks ();
14995 return error_mark_node;
14998 /* Issue an error message if type-definitions are forbidden here. */
14999 cp_parser_check_type_definition (parser);
15000 /* Remember that we are defining one more class. */
15001 ++parser->num_classes_being_defined;
15002 /* Inside the class, surrounding template-parameter-lists do not
15004 saved_num_template_parameter_lists
15005 = parser->num_template_parameter_lists;
15006 parser->num_template_parameter_lists = 0;
15007 /* We are not in a function body. */
15008 saved_in_function_body = parser->in_function_body;
15009 parser->in_function_body = false;
15010 /* We are not immediately inside an extern "lang" block. */
15011 saved_in_unbraced_linkage_specification_p
15012 = parser->in_unbraced_linkage_specification_p;
15013 parser->in_unbraced_linkage_specification_p = false;
15015 /* Start the class. */
15016 if (nested_name_specifier_p)
15018 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15019 old_scope = push_inner_scope (scope);
15021 type = begin_class_definition (type, attributes);
15023 if (type == error_mark_node)
15024 /* If the type is erroneous, skip the entire body of the class. */
15025 cp_parser_skip_to_closing_brace (parser);
15027 /* Parse the member-specification. */
15028 cp_parser_member_specification_opt (parser);
15030 /* Look for the trailing `}'. */
15031 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15032 /* Look for trailing attributes to apply to this class. */
15033 if (cp_parser_allow_gnu_extensions_p (parser))
15034 attributes = cp_parser_attributes_opt (parser);
15035 if (type != error_mark_node)
15036 type = finish_struct (type, attributes);
15037 if (nested_name_specifier_p)
15038 pop_inner_scope (old_scope, scope);
15039 /* If this class is not itself within the scope of another class,
15040 then we need to parse the bodies of all of the queued function
15041 definitions. Note that the queued functions defined in a class
15042 are not always processed immediately following the
15043 class-specifier for that class. Consider:
15046 struct B { void f() { sizeof (A); } };
15049 If `f' were processed before the processing of `A' were
15050 completed, there would be no way to compute the size of `A'.
15051 Note that the nesting we are interested in here is lexical --
15052 not the semantic nesting given by TYPE_CONTEXT. In particular,
15055 struct A { struct B; };
15056 struct A::B { void f() { } };
15058 there is no need to delay the parsing of `A::B::f'. */
15059 if (--parser->num_classes_being_defined == 0)
15063 tree class_type = NULL_TREE;
15064 tree pushed_scope = NULL_TREE;
15066 /* In a first pass, parse default arguments to the functions.
15067 Then, in a second pass, parse the bodies of the functions.
15068 This two-phased approach handles cases like:
15076 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15077 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15078 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15079 TREE_PURPOSE (parser->unparsed_functions_queues)
15080 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15082 fn = TREE_VALUE (queue_entry);
15083 /* If there are default arguments that have not yet been processed,
15084 take care of them now. */
15085 if (class_type != TREE_PURPOSE (queue_entry))
15088 pop_scope (pushed_scope);
15089 class_type = TREE_PURPOSE (queue_entry);
15090 pushed_scope = push_scope (class_type);
15092 /* Make sure that any template parameters are in scope. */
15093 maybe_begin_member_template_processing (fn);
15094 /* Parse the default argument expressions. */
15095 cp_parser_late_parsing_default_args (parser, fn);
15096 /* Remove any template parameters from the symbol table. */
15097 maybe_end_member_template_processing ();
15100 pop_scope (pushed_scope);
15101 /* Now parse the body of the functions. */
15102 for (TREE_VALUE (parser->unparsed_functions_queues)
15103 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15104 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15105 TREE_VALUE (parser->unparsed_functions_queues)
15106 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15108 /* Figure out which function we need to process. */
15109 fn = TREE_VALUE (queue_entry);
15110 /* Parse the function. */
15111 cp_parser_late_parsing_for_member (parser, fn);
15115 /* Put back any saved access checks. */
15116 pop_deferring_access_checks ();
15118 /* Restore saved state. */
15119 parser->in_function_body = saved_in_function_body;
15120 parser->num_template_parameter_lists
15121 = saved_num_template_parameter_lists;
15122 parser->in_unbraced_linkage_specification_p
15123 = saved_in_unbraced_linkage_specification_p;
15128 /* Parse a class-head.
15131 class-key identifier [opt] base-clause [opt]
15132 class-key nested-name-specifier identifier base-clause [opt]
15133 class-key nested-name-specifier [opt] template-id
15137 class-key attributes identifier [opt] base-clause [opt]
15138 class-key attributes nested-name-specifier identifier base-clause [opt]
15139 class-key attributes nested-name-specifier [opt] template-id
15142 Upon return BASES is initialized to the list of base classes (or
15143 NULL, if there are none) in the same form returned by
15144 cp_parser_base_clause.
15146 Returns the TYPE of the indicated class. Sets
15147 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15148 involving a nested-name-specifier was used, and FALSE otherwise.
15150 Returns error_mark_node if this is not a class-head.
15152 Returns NULL_TREE if the class-head is syntactically valid, but
15153 semantically invalid in a way that means we should skip the entire
15154 body of the class. */
15157 cp_parser_class_head (cp_parser* parser,
15158 bool* nested_name_specifier_p,
15159 tree *attributes_p,
15162 tree nested_name_specifier;
15163 enum tag_types class_key;
15164 tree id = NULL_TREE;
15165 tree type = NULL_TREE;
15167 bool template_id_p = false;
15168 bool qualified_p = false;
15169 bool invalid_nested_name_p = false;
15170 bool invalid_explicit_specialization_p = false;
15171 tree pushed_scope = NULL_TREE;
15172 unsigned num_templates;
15173 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15174 /* Assume no nested-name-specifier will be present. */
15175 *nested_name_specifier_p = false;
15176 /* Assume no template parameter lists will be used in defining the
15180 *bases = NULL_TREE;
15182 /* Look for the class-key. */
15183 class_key = cp_parser_class_key (parser);
15184 if (class_key == none_type)
15185 return error_mark_node;
15187 /* Parse the attributes. */
15188 attributes = cp_parser_attributes_opt (parser);
15190 /* If the next token is `::', that is invalid -- but sometimes
15191 people do try to write:
15195 Handle this gracefully by accepting the extra qualifier, and then
15196 issuing an error about it later if this really is a
15197 class-head. If it turns out just to be an elaborated type
15198 specifier, remain silent. */
15199 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15200 qualified_p = true;
15202 push_deferring_access_checks (dk_no_check);
15204 /* Determine the name of the class. Begin by looking for an
15205 optional nested-name-specifier. */
15206 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15207 nested_name_specifier
15208 = cp_parser_nested_name_specifier_opt (parser,
15209 /*typename_keyword_p=*/false,
15210 /*check_dependency_p=*/false,
15212 /*is_declaration=*/false);
15213 /* If there was a nested-name-specifier, then there *must* be an
15215 if (nested_name_specifier)
15217 type_start_token = cp_lexer_peek_token (parser->lexer);
15218 /* Although the grammar says `identifier', it really means
15219 `class-name' or `template-name'. You are only allowed to
15220 define a class that has already been declared with this
15223 The proposed resolution for Core Issue 180 says that wherever
15224 you see `class T::X' you should treat `X' as a type-name.
15226 It is OK to define an inaccessible class; for example:
15228 class A { class B; };
15231 We do not know if we will see a class-name, or a
15232 template-name. We look for a class-name first, in case the
15233 class-name is a template-id; if we looked for the
15234 template-name first we would stop after the template-name. */
15235 cp_parser_parse_tentatively (parser);
15236 type = cp_parser_class_name (parser,
15237 /*typename_keyword_p=*/false,
15238 /*template_keyword_p=*/false,
15240 /*check_dependency_p=*/false,
15241 /*class_head_p=*/true,
15242 /*is_declaration=*/false);
15243 /* If that didn't work, ignore the nested-name-specifier. */
15244 if (!cp_parser_parse_definitely (parser))
15246 invalid_nested_name_p = true;
15247 type_start_token = cp_lexer_peek_token (parser->lexer);
15248 id = cp_parser_identifier (parser);
15249 if (id == error_mark_node)
15252 /* If we could not find a corresponding TYPE, treat this
15253 declaration like an unqualified declaration. */
15254 if (type == error_mark_node)
15255 nested_name_specifier = NULL_TREE;
15256 /* Otherwise, count the number of templates used in TYPE and its
15257 containing scopes. */
15262 for (scope = TREE_TYPE (type);
15263 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15264 scope = (TYPE_P (scope)
15265 ? TYPE_CONTEXT (scope)
15266 : DECL_CONTEXT (scope)))
15268 && CLASS_TYPE_P (scope)
15269 && CLASSTYPE_TEMPLATE_INFO (scope)
15270 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15271 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15275 /* Otherwise, the identifier is optional. */
15278 /* We don't know whether what comes next is a template-id,
15279 an identifier, or nothing at all. */
15280 cp_parser_parse_tentatively (parser);
15281 /* Check for a template-id. */
15282 type_start_token = cp_lexer_peek_token (parser->lexer);
15283 id = cp_parser_template_id (parser,
15284 /*template_keyword_p=*/false,
15285 /*check_dependency_p=*/true,
15286 /*is_declaration=*/true);
15287 /* If that didn't work, it could still be an identifier. */
15288 if (!cp_parser_parse_definitely (parser))
15290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15292 type_start_token = cp_lexer_peek_token (parser->lexer);
15293 id = cp_parser_identifier (parser);
15300 template_id_p = true;
15305 pop_deferring_access_checks ();
15308 cp_parser_check_for_invalid_template_id (parser, id,
15309 type_start_token->location);
15311 /* If it's not a `:' or a `{' then we can't really be looking at a
15312 class-head, since a class-head only appears as part of a
15313 class-specifier. We have to detect this situation before calling
15314 xref_tag, since that has irreversible side-effects. */
15315 if (!cp_parser_next_token_starts_class_definition_p (parser))
15317 cp_parser_error (parser, "expected %<{%> or %<:%>");
15318 return error_mark_node;
15321 /* At this point, we're going ahead with the class-specifier, even
15322 if some other problem occurs. */
15323 cp_parser_commit_to_tentative_parse (parser);
15324 /* Issue the error about the overly-qualified name now. */
15327 cp_parser_error (parser,
15328 "global qualification of class name is invalid");
15329 return error_mark_node;
15331 else if (invalid_nested_name_p)
15333 cp_parser_error (parser,
15334 "qualified name does not name a class");
15335 return error_mark_node;
15337 else if (nested_name_specifier)
15341 /* Reject typedef-names in class heads. */
15342 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15344 error ("%Hinvalid class name in declaration of %qD",
15345 &type_start_token->location, type);
15350 /* Figure out in what scope the declaration is being placed. */
15351 scope = current_scope ();
15352 /* If that scope does not contain the scope in which the
15353 class was originally declared, the program is invalid. */
15354 if (scope && !is_ancestor (scope, nested_name_specifier))
15356 if (at_namespace_scope_p ())
15357 error ("%Hdeclaration of %qD in namespace %qD which does not "
15359 &type_start_token->location,
15360 type, scope, nested_name_specifier);
15362 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15363 &type_start_token->location,
15364 type, scope, nested_name_specifier);
15370 A declarator-id shall not be qualified except for the
15371 definition of a ... nested class outside of its class
15372 ... [or] the definition or explicit instantiation of a
15373 class member of a namespace outside of its namespace. */
15374 if (scope == nested_name_specifier)
15376 permerror (input_location, "%Hextra qualification not allowed",
15377 &nested_name_specifier_token_start->location);
15378 nested_name_specifier = NULL_TREE;
15382 /* An explicit-specialization must be preceded by "template <>". If
15383 it is not, try to recover gracefully. */
15384 if (at_namespace_scope_p ()
15385 && parser->num_template_parameter_lists == 0
15388 error ("%Han explicit specialization must be preceded by %<template <>%>",
15389 &type_start_token->location);
15390 invalid_explicit_specialization_p = true;
15391 /* Take the same action that would have been taken by
15392 cp_parser_explicit_specialization. */
15393 ++parser->num_template_parameter_lists;
15394 begin_specialization ();
15396 /* There must be no "return" statements between this point and the
15397 end of this function; set "type "to the correct return value and
15398 use "goto done;" to return. */
15399 /* Make sure that the right number of template parameters were
15401 if (!cp_parser_check_template_parameters (parser, num_templates,
15402 type_start_token->location,
15403 /*declarator=*/NULL))
15405 /* If something went wrong, there is no point in even trying to
15406 process the class-definition. */
15411 /* Look up the type. */
15414 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15415 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15416 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15418 error ("%Hfunction template %qD redeclared as a class template",
15419 &type_start_token->location, id);
15420 type = error_mark_node;
15424 type = TREE_TYPE (id);
15425 type = maybe_process_partial_specialization (type);
15427 if (nested_name_specifier)
15428 pushed_scope = push_scope (nested_name_specifier);
15430 else if (nested_name_specifier)
15436 template <typename T> struct S { struct T };
15437 template <typename T> struct S<T>::T { };
15439 we will get a TYPENAME_TYPE when processing the definition of
15440 `S::T'. We need to resolve it to the actual type before we
15441 try to define it. */
15442 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15444 class_type = resolve_typename_type (TREE_TYPE (type),
15445 /*only_current_p=*/false);
15446 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15447 type = TYPE_NAME (class_type);
15450 cp_parser_error (parser, "could not resolve typename type");
15451 type = error_mark_node;
15455 if (maybe_process_partial_specialization (TREE_TYPE (type))
15456 == error_mark_node)
15462 class_type = current_class_type;
15463 /* Enter the scope indicated by the nested-name-specifier. */
15464 pushed_scope = push_scope (nested_name_specifier);
15465 /* Get the canonical version of this type. */
15466 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15467 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15468 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15470 type = push_template_decl (type);
15471 if (type == error_mark_node)
15478 type = TREE_TYPE (type);
15479 *nested_name_specifier_p = true;
15481 else /* The name is not a nested name. */
15483 /* If the class was unnamed, create a dummy name. */
15485 id = make_anon_name ();
15486 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15487 parser->num_template_parameter_lists);
15490 /* Indicate whether this class was declared as a `class' or as a
15492 if (TREE_CODE (type) == RECORD_TYPE)
15493 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15494 cp_parser_check_class_key (class_key, type);
15496 /* If this type was already complete, and we see another definition,
15497 that's an error. */
15498 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15500 error ("%Hredefinition of %q#T",
15501 &type_start_token->location, type);
15502 error ("%Hprevious definition of %q+#T",
15503 &type_start_token->location, type);
15507 else if (type == error_mark_node)
15510 /* We will have entered the scope containing the class; the names of
15511 base classes should be looked up in that context. For example:
15513 struct A { struct B {}; struct C; };
15514 struct A::C : B {};
15518 /* Get the list of base-classes, if there is one. */
15519 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15520 *bases = cp_parser_base_clause (parser);
15523 /* Leave the scope given by the nested-name-specifier. We will
15524 enter the class scope itself while processing the members. */
15526 pop_scope (pushed_scope);
15528 if (invalid_explicit_specialization_p)
15530 end_specialization ();
15531 --parser->num_template_parameter_lists;
15533 *attributes_p = attributes;
15537 /* Parse a class-key.
15544 Returns the kind of class-key specified, or none_type to indicate
15547 static enum tag_types
15548 cp_parser_class_key (cp_parser* parser)
15551 enum tag_types tag_type;
15553 /* Look for the class-key. */
15554 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15558 /* Check to see if the TOKEN is a class-key. */
15559 tag_type = cp_parser_token_is_class_key (token);
15561 cp_parser_error (parser, "expected class-key");
15565 /* Parse an (optional) member-specification.
15567 member-specification:
15568 member-declaration member-specification [opt]
15569 access-specifier : member-specification [opt] */
15572 cp_parser_member_specification_opt (cp_parser* parser)
15579 /* Peek at the next token. */
15580 token = cp_lexer_peek_token (parser->lexer);
15581 /* If it's a `}', or EOF then we've seen all the members. */
15582 if (token->type == CPP_CLOSE_BRACE
15583 || token->type == CPP_EOF
15584 || token->type == CPP_PRAGMA_EOL)
15587 /* See if this token is a keyword. */
15588 keyword = token->keyword;
15592 case RID_PROTECTED:
15594 /* Consume the access-specifier. */
15595 cp_lexer_consume_token (parser->lexer);
15596 /* Remember which access-specifier is active. */
15597 current_access_specifier = token->u.value;
15598 /* Look for the `:'. */
15599 cp_parser_require (parser, CPP_COLON, "%<:%>");
15603 /* Accept #pragmas at class scope. */
15604 if (token->type == CPP_PRAGMA)
15606 cp_parser_pragma (parser, pragma_external);
15610 /* Otherwise, the next construction must be a
15611 member-declaration. */
15612 cp_parser_member_declaration (parser);
15617 /* Parse a member-declaration.
15619 member-declaration:
15620 decl-specifier-seq [opt] member-declarator-list [opt] ;
15621 function-definition ; [opt]
15622 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15624 template-declaration
15626 member-declarator-list:
15628 member-declarator-list , member-declarator
15631 declarator pure-specifier [opt]
15632 declarator constant-initializer [opt]
15633 identifier [opt] : constant-expression
15637 member-declaration:
15638 __extension__ member-declaration
15641 declarator attributes [opt] pure-specifier [opt]
15642 declarator attributes [opt] constant-initializer [opt]
15643 identifier [opt] attributes [opt] : constant-expression
15647 member-declaration:
15648 static_assert-declaration */
15651 cp_parser_member_declaration (cp_parser* parser)
15653 cp_decl_specifier_seq decl_specifiers;
15654 tree prefix_attributes;
15656 int declares_class_or_enum;
15658 cp_token *token = NULL;
15659 cp_token *decl_spec_token_start = NULL;
15660 cp_token *initializer_token_start = NULL;
15661 int saved_pedantic;
15663 /* Check for the `__extension__' keyword. */
15664 if (cp_parser_extension_opt (parser, &saved_pedantic))
15667 cp_parser_member_declaration (parser);
15668 /* Restore the old value of the PEDANTIC flag. */
15669 pedantic = saved_pedantic;
15674 /* Check for a template-declaration. */
15675 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15677 /* An explicit specialization here is an error condition, and we
15678 expect the specialization handler to detect and report this. */
15679 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15680 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15681 cp_parser_explicit_specialization (parser);
15683 cp_parser_template_declaration (parser, /*member_p=*/true);
15688 /* Check for a using-declaration. */
15689 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15691 /* Parse the using-declaration. */
15692 cp_parser_using_declaration (parser,
15693 /*access_declaration_p=*/false);
15697 /* Check for @defs. */
15698 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15701 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15702 ivar = ivar_chains;
15706 ivar = TREE_CHAIN (member);
15707 TREE_CHAIN (member) = NULL_TREE;
15708 finish_member_declaration (member);
15713 /* If the next token is `static_assert' we have a static assertion. */
15714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15716 cp_parser_static_assert (parser, /*member_p=*/true);
15720 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15723 /* Parse the decl-specifier-seq. */
15724 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15725 cp_parser_decl_specifier_seq (parser,
15726 CP_PARSER_FLAGS_OPTIONAL,
15728 &declares_class_or_enum);
15729 prefix_attributes = decl_specifiers.attributes;
15730 decl_specifiers.attributes = NULL_TREE;
15731 /* Check for an invalid type-name. */
15732 if (!decl_specifiers.type
15733 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15735 /* If there is no declarator, then the decl-specifier-seq should
15737 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15739 /* If there was no decl-specifier-seq, and the next token is a
15740 `;', then we have something like:
15746 Each member-declaration shall declare at least one member
15747 name of the class. */
15748 if (!decl_specifiers.any_specifiers_p)
15750 cp_token *token = cp_lexer_peek_token (parser->lexer);
15751 if (!in_system_header_at (token->location))
15752 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15758 /* See if this declaration is a friend. */
15759 friend_p = cp_parser_friend_p (&decl_specifiers);
15760 /* If there were decl-specifiers, check to see if there was
15761 a class-declaration. */
15762 type = check_tag_decl (&decl_specifiers);
15763 /* Nested classes have already been added to the class, but
15764 a `friend' needs to be explicitly registered. */
15767 /* If the `friend' keyword was present, the friend must
15768 be introduced with a class-key. */
15769 if (!declares_class_or_enum)
15770 error ("%Ha class-key must be used when declaring a friend",
15771 &decl_spec_token_start->location);
15774 template <typename T> struct A {
15775 friend struct A<T>::B;
15778 A<T>::B will be represented by a TYPENAME_TYPE, and
15779 therefore not recognized by check_tag_decl. */
15781 && decl_specifiers.type
15782 && TYPE_P (decl_specifiers.type))
15783 type = decl_specifiers.type;
15784 if (!type || !TYPE_P (type))
15785 error ("%Hfriend declaration does not name a class or "
15786 "function", &decl_spec_token_start->location);
15788 make_friend_class (current_class_type, type,
15789 /*complain=*/true);
15791 /* If there is no TYPE, an error message will already have
15793 else if (!type || type == error_mark_node)
15795 /* An anonymous aggregate has to be handled specially; such
15796 a declaration really declares a data member (with a
15797 particular type), as opposed to a nested class. */
15798 else if (ANON_AGGR_TYPE_P (type))
15800 /* Remove constructors and such from TYPE, now that we
15801 know it is an anonymous aggregate. */
15802 fixup_anonymous_aggr (type);
15803 /* And make the corresponding data member. */
15804 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15805 /* Add it to the class. */
15806 finish_member_declaration (decl);
15809 cp_parser_check_access_in_redeclaration
15811 decl_spec_token_start->location);
15816 /* See if these declarations will be friends. */
15817 friend_p = cp_parser_friend_p (&decl_specifiers);
15819 /* Keep going until we hit the `;' at the end of the
15821 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15823 tree attributes = NULL_TREE;
15824 tree first_attribute;
15826 /* Peek at the next token. */
15827 token = cp_lexer_peek_token (parser->lexer);
15829 /* Check for a bitfield declaration. */
15830 if (token->type == CPP_COLON
15831 || (token->type == CPP_NAME
15832 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15838 /* Get the name of the bitfield. Note that we cannot just
15839 check TOKEN here because it may have been invalidated by
15840 the call to cp_lexer_peek_nth_token above. */
15841 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15842 identifier = cp_parser_identifier (parser);
15844 identifier = NULL_TREE;
15846 /* Consume the `:' token. */
15847 cp_lexer_consume_token (parser->lexer);
15848 /* Get the width of the bitfield. */
15850 = cp_parser_constant_expression (parser,
15851 /*allow_non_constant=*/false,
15854 /* Look for attributes that apply to the bitfield. */
15855 attributes = cp_parser_attributes_opt (parser);
15856 /* Remember which attributes are prefix attributes and
15858 first_attribute = attributes;
15859 /* Combine the attributes. */
15860 attributes = chainon (prefix_attributes, attributes);
15862 /* Create the bitfield declaration. */
15863 decl = grokbitfield (identifier
15864 ? make_id_declarator (NULL_TREE,
15874 cp_declarator *declarator;
15876 tree asm_specification;
15877 int ctor_dtor_or_conv_p;
15879 /* Parse the declarator. */
15881 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15882 &ctor_dtor_or_conv_p,
15883 /*parenthesized_p=*/NULL,
15884 /*member_p=*/true);
15886 /* If something went wrong parsing the declarator, make sure
15887 that we at least consume some tokens. */
15888 if (declarator == cp_error_declarator)
15890 /* Skip to the end of the statement. */
15891 cp_parser_skip_to_end_of_statement (parser);
15892 /* If the next token is not a semicolon, that is
15893 probably because we just skipped over the body of
15894 a function. So, we consume a semicolon if
15895 present, but do not issue an error message if it
15897 if (cp_lexer_next_token_is (parser->lexer,
15899 cp_lexer_consume_token (parser->lexer);
15903 if (declares_class_or_enum & 2)
15904 cp_parser_check_for_definition_in_return_type
15905 (declarator, decl_specifiers.type,
15906 decl_specifiers.type_location);
15908 /* Look for an asm-specification. */
15909 asm_specification = cp_parser_asm_specification_opt (parser);
15910 /* Look for attributes that apply to the declaration. */
15911 attributes = cp_parser_attributes_opt (parser);
15912 /* Remember which attributes are prefix attributes and
15914 first_attribute = attributes;
15915 /* Combine the attributes. */
15916 attributes = chainon (prefix_attributes, attributes);
15918 /* If it's an `=', then we have a constant-initializer or a
15919 pure-specifier. It is not correct to parse the
15920 initializer before registering the member declaration
15921 since the member declaration should be in scope while
15922 its initializer is processed. However, the rest of the
15923 front end does not yet provide an interface that allows
15924 us to handle this correctly. */
15925 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15929 A pure-specifier shall be used only in the declaration of
15930 a virtual function.
15932 A member-declarator can contain a constant-initializer
15933 only if it declares a static member of integral or
15936 Therefore, if the DECLARATOR is for a function, we look
15937 for a pure-specifier; otherwise, we look for a
15938 constant-initializer. When we call `grokfield', it will
15939 perform more stringent semantics checks. */
15940 initializer_token_start = cp_lexer_peek_token (parser->lexer);
15941 if (function_declarator_p (declarator))
15942 initializer = cp_parser_pure_specifier (parser);
15944 /* Parse the initializer. */
15945 initializer = cp_parser_constant_initializer (parser);
15947 /* Otherwise, there is no initializer. */
15949 initializer = NULL_TREE;
15951 /* See if we are probably looking at a function
15952 definition. We are certainly not looking at a
15953 member-declarator. Calling `grokfield' has
15954 side-effects, so we must not do it unless we are sure
15955 that we are looking at a member-declarator. */
15956 if (cp_parser_token_starts_function_definition_p
15957 (cp_lexer_peek_token (parser->lexer)))
15959 /* The grammar does not allow a pure-specifier to be
15960 used when a member function is defined. (It is
15961 possible that this fact is an oversight in the
15962 standard, since a pure function may be defined
15963 outside of the class-specifier. */
15965 error ("%Hpure-specifier on function-definition",
15966 &initializer_token_start->location);
15967 decl = cp_parser_save_member_function_body (parser,
15971 /* If the member was not a friend, declare it here. */
15973 finish_member_declaration (decl);
15974 /* Peek at the next token. */
15975 token = cp_lexer_peek_token (parser->lexer);
15976 /* If the next token is a semicolon, consume it. */
15977 if (token->type == CPP_SEMICOLON)
15978 cp_lexer_consume_token (parser->lexer);
15982 if (declarator->kind == cdk_function)
15983 declarator->id_loc = token->location;
15984 /* Create the declaration. */
15985 decl = grokfield (declarator, &decl_specifiers,
15986 initializer, /*init_const_expr_p=*/true,
15991 /* Reset PREFIX_ATTRIBUTES. */
15992 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15993 attributes = TREE_CHAIN (attributes);
15995 TREE_CHAIN (attributes) = NULL_TREE;
15997 /* If there is any qualification still in effect, clear it
15998 now; we will be starting fresh with the next declarator. */
15999 parser->scope = NULL_TREE;
16000 parser->qualifying_scope = NULL_TREE;
16001 parser->object_scope = NULL_TREE;
16002 /* If it's a `,', then there are more declarators. */
16003 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16004 cp_lexer_consume_token (parser->lexer);
16005 /* If the next token isn't a `;', then we have a parse error. */
16006 else if (cp_lexer_next_token_is_not (parser->lexer,
16009 cp_parser_error (parser, "expected %<;%>");
16010 /* Skip tokens until we find a `;'. */
16011 cp_parser_skip_to_end_of_statement (parser);
16018 /* Add DECL to the list of members. */
16020 finish_member_declaration (decl);
16022 if (TREE_CODE (decl) == FUNCTION_DECL)
16023 cp_parser_save_default_args (parser, decl);
16028 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16031 /* Parse a pure-specifier.
16036 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16037 Otherwise, ERROR_MARK_NODE is returned. */
16040 cp_parser_pure_specifier (cp_parser* parser)
16044 /* Look for the `=' token. */
16045 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16046 return error_mark_node;
16047 /* Look for the `0' token. */
16048 token = cp_lexer_peek_token (parser->lexer);
16050 if (token->type == CPP_EOF
16051 || token->type == CPP_PRAGMA_EOL)
16052 return error_mark_node;
16054 cp_lexer_consume_token (parser->lexer);
16056 /* Accept = default or = delete in c++0x mode. */
16057 if (token->keyword == RID_DEFAULT
16058 || token->keyword == RID_DELETE)
16060 maybe_warn_cpp0x ("defaulted and deleted functions");
16061 return token->u.value;
16064 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16065 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16067 cp_parser_error (parser,
16068 "invalid pure specifier (only %<= 0%> is allowed)");
16069 cp_parser_skip_to_end_of_statement (parser);
16070 return error_mark_node;
16072 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16074 error ("%Htemplates may not be %<virtual%>", &token->location);
16075 return error_mark_node;
16078 return integer_zero_node;
16081 /* Parse a constant-initializer.
16083 constant-initializer:
16084 = constant-expression
16086 Returns a representation of the constant-expression. */
16089 cp_parser_constant_initializer (cp_parser* parser)
16091 /* Look for the `=' token. */
16092 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16093 return error_mark_node;
16095 /* It is invalid to write:
16097 struct S { static const int i = { 7 }; };
16100 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16102 cp_parser_error (parser,
16103 "a brace-enclosed initializer is not allowed here");
16104 /* Consume the opening brace. */
16105 cp_lexer_consume_token (parser->lexer);
16106 /* Skip the initializer. */
16107 cp_parser_skip_to_closing_brace (parser);
16108 /* Look for the trailing `}'. */
16109 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16111 return error_mark_node;
16114 return cp_parser_constant_expression (parser,
16115 /*allow_non_constant=*/false,
16119 /* Derived classes [gram.class.derived] */
16121 /* Parse a base-clause.
16124 : base-specifier-list
16126 base-specifier-list:
16127 base-specifier ... [opt]
16128 base-specifier-list , base-specifier ... [opt]
16130 Returns a TREE_LIST representing the base-classes, in the order in
16131 which they were declared. The representation of each node is as
16132 described by cp_parser_base_specifier.
16134 In the case that no bases are specified, this function will return
16135 NULL_TREE, not ERROR_MARK_NODE. */
16138 cp_parser_base_clause (cp_parser* parser)
16140 tree bases = NULL_TREE;
16142 /* Look for the `:' that begins the list. */
16143 cp_parser_require (parser, CPP_COLON, "%<:%>");
16145 /* Scan the base-specifier-list. */
16150 bool pack_expansion_p = false;
16152 /* Look for the base-specifier. */
16153 base = cp_parser_base_specifier (parser);
16154 /* Look for the (optional) ellipsis. */
16155 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16157 /* Consume the `...'. */
16158 cp_lexer_consume_token (parser->lexer);
16160 pack_expansion_p = true;
16163 /* Add BASE to the front of the list. */
16164 if (base != error_mark_node)
16166 if (pack_expansion_p)
16167 /* Make this a pack expansion type. */
16168 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16171 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16173 TREE_CHAIN (base) = bases;
16177 /* Peek at the next token. */
16178 token = cp_lexer_peek_token (parser->lexer);
16179 /* If it's not a comma, then the list is complete. */
16180 if (token->type != CPP_COMMA)
16182 /* Consume the `,'. */
16183 cp_lexer_consume_token (parser->lexer);
16186 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16187 base class had a qualified name. However, the next name that
16188 appears is certainly not qualified. */
16189 parser->scope = NULL_TREE;
16190 parser->qualifying_scope = NULL_TREE;
16191 parser->object_scope = NULL_TREE;
16193 return nreverse (bases);
16196 /* Parse a base-specifier.
16199 :: [opt] nested-name-specifier [opt] class-name
16200 virtual access-specifier [opt] :: [opt] nested-name-specifier
16202 access-specifier virtual [opt] :: [opt] nested-name-specifier
16205 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16206 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16207 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16208 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16211 cp_parser_base_specifier (cp_parser* parser)
16215 bool virtual_p = false;
16216 bool duplicate_virtual_error_issued_p = false;
16217 bool duplicate_access_error_issued_p = false;
16218 bool class_scope_p, template_p;
16219 tree access = access_default_node;
16222 /* Process the optional `virtual' and `access-specifier'. */
16225 /* Peek at the next token. */
16226 token = cp_lexer_peek_token (parser->lexer);
16227 /* Process `virtual'. */
16228 switch (token->keyword)
16231 /* If `virtual' appears more than once, issue an error. */
16232 if (virtual_p && !duplicate_virtual_error_issued_p)
16234 cp_parser_error (parser,
16235 "%<virtual%> specified more than once in base-specified");
16236 duplicate_virtual_error_issued_p = true;
16241 /* Consume the `virtual' token. */
16242 cp_lexer_consume_token (parser->lexer);
16247 case RID_PROTECTED:
16249 /* If more than one access specifier appears, issue an
16251 if (access != access_default_node
16252 && !duplicate_access_error_issued_p)
16254 cp_parser_error (parser,
16255 "more than one access specifier in base-specified");
16256 duplicate_access_error_issued_p = true;
16259 access = ridpointers[(int) token->keyword];
16261 /* Consume the access-specifier. */
16262 cp_lexer_consume_token (parser->lexer);
16271 /* It is not uncommon to see programs mechanically, erroneously, use
16272 the 'typename' keyword to denote (dependent) qualified types
16273 as base classes. */
16274 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16276 token = cp_lexer_peek_token (parser->lexer);
16277 if (!processing_template_decl)
16278 error ("%Hkeyword %<typename%> not allowed outside of templates",
16281 error ("%Hkeyword %<typename%> not allowed in this context "
16282 "(the base class is implicitly a type)",
16284 cp_lexer_consume_token (parser->lexer);
16287 /* Look for the optional `::' operator. */
16288 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16289 /* Look for the nested-name-specifier. The simplest way to
16294 The keyword `typename' is not permitted in a base-specifier or
16295 mem-initializer; in these contexts a qualified name that
16296 depends on a template-parameter is implicitly assumed to be a
16299 is to pretend that we have seen the `typename' keyword at this
16301 cp_parser_nested_name_specifier_opt (parser,
16302 /*typename_keyword_p=*/true,
16303 /*check_dependency_p=*/true,
16305 /*is_declaration=*/true);
16306 /* If the base class is given by a qualified name, assume that names
16307 we see are type names or templates, as appropriate. */
16308 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16309 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16311 /* Finally, look for the class-name. */
16312 type = cp_parser_class_name (parser,
16316 /*check_dependency_p=*/true,
16317 /*class_head_p=*/false,
16318 /*is_declaration=*/true);
16320 if (type == error_mark_node)
16321 return error_mark_node;
16323 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16326 /* Exception handling [gram.exception] */
16328 /* Parse an (optional) exception-specification.
16330 exception-specification:
16331 throw ( type-id-list [opt] )
16333 Returns a TREE_LIST representing the exception-specification. The
16334 TREE_VALUE of each node is a type. */
16337 cp_parser_exception_specification_opt (cp_parser* parser)
16342 /* Peek at the next token. */
16343 token = cp_lexer_peek_token (parser->lexer);
16344 /* If it's not `throw', then there's no exception-specification. */
16345 if (!cp_parser_is_keyword (token, RID_THROW))
16348 /* Consume the `throw'. */
16349 cp_lexer_consume_token (parser->lexer);
16351 /* Look for the `('. */
16352 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16354 /* Peek at the next token. */
16355 token = cp_lexer_peek_token (parser->lexer);
16356 /* If it's not a `)', then there is a type-id-list. */
16357 if (token->type != CPP_CLOSE_PAREN)
16359 const char *saved_message;
16361 /* Types may not be defined in an exception-specification. */
16362 saved_message = parser->type_definition_forbidden_message;
16363 parser->type_definition_forbidden_message
16364 = "types may not be defined in an exception-specification";
16365 /* Parse the type-id-list. */
16366 type_id_list = cp_parser_type_id_list (parser);
16367 /* Restore the saved message. */
16368 parser->type_definition_forbidden_message = saved_message;
16371 type_id_list = empty_except_spec;
16373 /* Look for the `)'. */
16374 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16376 return type_id_list;
16379 /* Parse an (optional) type-id-list.
16383 type-id-list , type-id ... [opt]
16385 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16386 in the order that the types were presented. */
16389 cp_parser_type_id_list (cp_parser* parser)
16391 tree types = NULL_TREE;
16398 /* Get the next type-id. */
16399 type = cp_parser_type_id (parser);
16400 /* Parse the optional ellipsis. */
16401 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16403 /* Consume the `...'. */
16404 cp_lexer_consume_token (parser->lexer);
16406 /* Turn the type into a pack expansion expression. */
16407 type = make_pack_expansion (type);
16409 /* Add it to the list. */
16410 types = add_exception_specifier (types, type, /*complain=*/1);
16411 /* Peek at the next token. */
16412 token = cp_lexer_peek_token (parser->lexer);
16413 /* If it is not a `,', we are done. */
16414 if (token->type != CPP_COMMA)
16416 /* Consume the `,'. */
16417 cp_lexer_consume_token (parser->lexer);
16420 return nreverse (types);
16423 /* Parse a try-block.
16426 try compound-statement handler-seq */
16429 cp_parser_try_block (cp_parser* parser)
16433 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16434 try_block = begin_try_block ();
16435 cp_parser_compound_statement (parser, NULL, true);
16436 finish_try_block (try_block);
16437 cp_parser_handler_seq (parser);
16438 finish_handler_sequence (try_block);
16443 /* Parse a function-try-block.
16445 function-try-block:
16446 try ctor-initializer [opt] function-body handler-seq */
16449 cp_parser_function_try_block (cp_parser* parser)
16451 tree compound_stmt;
16453 bool ctor_initializer_p;
16455 /* Look for the `try' keyword. */
16456 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16458 /* Let the rest of the front end know where we are. */
16459 try_block = begin_function_try_block (&compound_stmt);
16460 /* Parse the function-body. */
16462 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16463 /* We're done with the `try' part. */
16464 finish_function_try_block (try_block);
16465 /* Parse the handlers. */
16466 cp_parser_handler_seq (parser);
16467 /* We're done with the handlers. */
16468 finish_function_handler_sequence (try_block, compound_stmt);
16470 return ctor_initializer_p;
16473 /* Parse a handler-seq.
16476 handler handler-seq [opt] */
16479 cp_parser_handler_seq (cp_parser* parser)
16485 /* Parse the handler. */
16486 cp_parser_handler (parser);
16487 /* Peek at the next token. */
16488 token = cp_lexer_peek_token (parser->lexer);
16489 /* If it's not `catch' then there are no more handlers. */
16490 if (!cp_parser_is_keyword (token, RID_CATCH))
16495 /* Parse a handler.
16498 catch ( exception-declaration ) compound-statement */
16501 cp_parser_handler (cp_parser* parser)
16506 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16507 handler = begin_handler ();
16508 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16509 declaration = cp_parser_exception_declaration (parser);
16510 finish_handler_parms (declaration, handler);
16511 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16512 cp_parser_compound_statement (parser, NULL, false);
16513 finish_handler (handler);
16516 /* Parse an exception-declaration.
16518 exception-declaration:
16519 type-specifier-seq declarator
16520 type-specifier-seq abstract-declarator
16524 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16525 ellipsis variant is used. */
16528 cp_parser_exception_declaration (cp_parser* parser)
16530 cp_decl_specifier_seq type_specifiers;
16531 cp_declarator *declarator;
16532 const char *saved_message;
16534 /* If it's an ellipsis, it's easy to handle. */
16535 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16537 /* Consume the `...' token. */
16538 cp_lexer_consume_token (parser->lexer);
16542 /* Types may not be defined in exception-declarations. */
16543 saved_message = parser->type_definition_forbidden_message;
16544 parser->type_definition_forbidden_message
16545 = "types may not be defined in exception-declarations";
16547 /* Parse the type-specifier-seq. */
16548 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16550 /* If it's a `)', then there is no declarator. */
16551 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16554 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16555 /*ctor_dtor_or_conv_p=*/NULL,
16556 /*parenthesized_p=*/NULL,
16557 /*member_p=*/false);
16559 /* Restore the saved message. */
16560 parser->type_definition_forbidden_message = saved_message;
16562 if (!type_specifiers.any_specifiers_p)
16563 return error_mark_node;
16565 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16568 /* Parse a throw-expression.
16571 throw assignment-expression [opt]
16573 Returns a THROW_EXPR representing the throw-expression. */
16576 cp_parser_throw_expression (cp_parser* parser)
16581 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16582 token = cp_lexer_peek_token (parser->lexer);
16583 /* Figure out whether or not there is an assignment-expression
16584 following the "throw" keyword. */
16585 if (token->type == CPP_COMMA
16586 || token->type == CPP_SEMICOLON
16587 || token->type == CPP_CLOSE_PAREN
16588 || token->type == CPP_CLOSE_SQUARE
16589 || token->type == CPP_CLOSE_BRACE
16590 || token->type == CPP_COLON)
16591 expression = NULL_TREE;
16593 expression = cp_parser_assignment_expression (parser,
16594 /*cast_p=*/false, NULL);
16596 return build_throw (expression);
16599 /* GNU Extensions */
16601 /* Parse an (optional) asm-specification.
16604 asm ( string-literal )
16606 If the asm-specification is present, returns a STRING_CST
16607 corresponding to the string-literal. Otherwise, returns
16611 cp_parser_asm_specification_opt (cp_parser* parser)
16614 tree asm_specification;
16616 /* Peek at the next token. */
16617 token = cp_lexer_peek_token (parser->lexer);
16618 /* If the next token isn't the `asm' keyword, then there's no
16619 asm-specification. */
16620 if (!cp_parser_is_keyword (token, RID_ASM))
16623 /* Consume the `asm' token. */
16624 cp_lexer_consume_token (parser->lexer);
16625 /* Look for the `('. */
16626 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16628 /* Look for the string-literal. */
16629 asm_specification = cp_parser_string_literal (parser, false, false);
16631 /* Look for the `)'. */
16632 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16634 return asm_specification;
16637 /* Parse an asm-operand-list.
16641 asm-operand-list , asm-operand
16644 string-literal ( expression )
16645 [ string-literal ] string-literal ( expression )
16647 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16648 each node is the expression. The TREE_PURPOSE is itself a
16649 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16650 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16651 is a STRING_CST for the string literal before the parenthesis. Returns
16652 ERROR_MARK_NODE if any of the operands are invalid. */
16655 cp_parser_asm_operand_list (cp_parser* parser)
16657 tree asm_operands = NULL_TREE;
16658 bool invalid_operands = false;
16662 tree string_literal;
16666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16668 /* Consume the `[' token. */
16669 cp_lexer_consume_token (parser->lexer);
16670 /* Read the operand name. */
16671 name = cp_parser_identifier (parser);
16672 if (name != error_mark_node)
16673 name = build_string (IDENTIFIER_LENGTH (name),
16674 IDENTIFIER_POINTER (name));
16675 /* Look for the closing `]'. */
16676 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16680 /* Look for the string-literal. */
16681 string_literal = cp_parser_string_literal (parser, false, false);
16683 /* Look for the `('. */
16684 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16685 /* Parse the expression. */
16686 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16687 /* Look for the `)'. */
16688 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16690 if (name == error_mark_node
16691 || string_literal == error_mark_node
16692 || expression == error_mark_node)
16693 invalid_operands = true;
16695 /* Add this operand to the list. */
16696 asm_operands = tree_cons (build_tree_list (name, string_literal),
16699 /* If the next token is not a `,', there are no more
16701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16703 /* Consume the `,'. */
16704 cp_lexer_consume_token (parser->lexer);
16707 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16710 /* Parse an asm-clobber-list.
16714 asm-clobber-list , string-literal
16716 Returns a TREE_LIST, indicating the clobbers in the order that they
16717 appeared. The TREE_VALUE of each node is a STRING_CST. */
16720 cp_parser_asm_clobber_list (cp_parser* parser)
16722 tree clobbers = NULL_TREE;
16726 tree string_literal;
16728 /* Look for the string literal. */
16729 string_literal = cp_parser_string_literal (parser, false, false);
16730 /* Add it to the list. */
16731 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16732 /* If the next token is not a `,', then the list is
16734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16736 /* Consume the `,' token. */
16737 cp_lexer_consume_token (parser->lexer);
16743 /* Parse an (optional) series of attributes.
16746 attributes attribute
16749 __attribute__ (( attribute-list [opt] ))
16751 The return value is as for cp_parser_attribute_list. */
16754 cp_parser_attributes_opt (cp_parser* parser)
16756 tree attributes = NULL_TREE;
16761 tree attribute_list;
16763 /* Peek at the next token. */
16764 token = cp_lexer_peek_token (parser->lexer);
16765 /* If it's not `__attribute__', then we're done. */
16766 if (token->keyword != RID_ATTRIBUTE)
16769 /* Consume the `__attribute__' keyword. */
16770 cp_lexer_consume_token (parser->lexer);
16771 /* Look for the two `(' tokens. */
16772 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16773 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16775 /* Peek at the next token. */
16776 token = cp_lexer_peek_token (parser->lexer);
16777 if (token->type != CPP_CLOSE_PAREN)
16778 /* Parse the attribute-list. */
16779 attribute_list = cp_parser_attribute_list (parser);
16781 /* If the next token is a `)', then there is no attribute
16783 attribute_list = NULL;
16785 /* Look for the two `)' tokens. */
16786 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16787 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16789 /* Add these new attributes to the list. */
16790 attributes = chainon (attributes, attribute_list);
16796 /* Parse an attribute-list.
16800 attribute-list , attribute
16804 identifier ( identifier )
16805 identifier ( identifier , expression-list )
16806 identifier ( expression-list )
16808 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16809 to an attribute. The TREE_PURPOSE of each node is the identifier
16810 indicating which attribute is in use. The TREE_VALUE represents
16811 the arguments, if any. */
16814 cp_parser_attribute_list (cp_parser* parser)
16816 tree attribute_list = NULL_TREE;
16817 bool save_translate_strings_p = parser->translate_strings_p;
16819 parser->translate_strings_p = false;
16826 /* Look for the identifier. We also allow keywords here; for
16827 example `__attribute__ ((const))' is legal. */
16828 token = cp_lexer_peek_token (parser->lexer);
16829 if (token->type == CPP_NAME
16830 || token->type == CPP_KEYWORD)
16832 tree arguments = NULL_TREE;
16834 /* Consume the token. */
16835 token = cp_lexer_consume_token (parser->lexer);
16837 /* Save away the identifier that indicates which attribute
16839 identifier = token->u.value;
16840 attribute = build_tree_list (identifier, NULL_TREE);
16842 /* Peek at the next token. */
16843 token = cp_lexer_peek_token (parser->lexer);
16844 /* If it's an `(', then parse the attribute arguments. */
16845 if (token->type == CPP_OPEN_PAREN)
16847 arguments = cp_parser_parenthesized_expression_list
16848 (parser, true, /*cast_p=*/false,
16849 /*allow_expansion_p=*/false,
16850 /*non_constant_p=*/NULL);
16851 /* Save the arguments away. */
16852 TREE_VALUE (attribute) = arguments;
16855 if (arguments != error_mark_node)
16857 /* Add this attribute to the list. */
16858 TREE_CHAIN (attribute) = attribute_list;
16859 attribute_list = attribute;
16862 token = cp_lexer_peek_token (parser->lexer);
16864 /* Now, look for more attributes. If the next token isn't a
16865 `,', we're done. */
16866 if (token->type != CPP_COMMA)
16869 /* Consume the comma and keep going. */
16870 cp_lexer_consume_token (parser->lexer);
16872 parser->translate_strings_p = save_translate_strings_p;
16874 /* We built up the list in reverse order. */
16875 return nreverse (attribute_list);
16878 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16879 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16880 current value of the PEDANTIC flag, regardless of whether or not
16881 the `__extension__' keyword is present. The caller is responsible
16882 for restoring the value of the PEDANTIC flag. */
16885 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16887 /* Save the old value of the PEDANTIC flag. */
16888 *saved_pedantic = pedantic;
16890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16892 /* Consume the `__extension__' token. */
16893 cp_lexer_consume_token (parser->lexer);
16894 /* We're not being pedantic while the `__extension__' keyword is
16904 /* Parse a label declaration.
16907 __label__ label-declarator-seq ;
16909 label-declarator-seq:
16910 identifier , label-declarator-seq
16914 cp_parser_label_declaration (cp_parser* parser)
16916 /* Look for the `__label__' keyword. */
16917 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16923 /* Look for an identifier. */
16924 identifier = cp_parser_identifier (parser);
16925 /* If we failed, stop. */
16926 if (identifier == error_mark_node)
16928 /* Declare it as a label. */
16929 finish_label_decl (identifier);
16930 /* If the next token is a `;', stop. */
16931 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16933 /* Look for the `,' separating the label declarations. */
16934 cp_parser_require (parser, CPP_COMMA, "%<,%>");
16937 /* Look for the final `;'. */
16938 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16941 /* Support Functions */
16943 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16944 NAME should have one of the representations used for an
16945 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16946 is returned. If PARSER->SCOPE is a dependent type, then a
16947 SCOPE_REF is returned.
16949 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16950 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16951 was formed. Abstractly, such entities should not be passed to this
16952 function, because they do not need to be looked up, but it is
16953 simpler to check for this special case here, rather than at the
16956 In cases not explicitly covered above, this function returns a
16957 DECL, OVERLOAD, or baselink representing the result of the lookup.
16958 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16961 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16962 (e.g., "struct") that was used. In that case bindings that do not
16963 refer to types are ignored.
16965 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16968 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16971 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16974 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16975 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16976 NULL_TREE otherwise. */
16979 cp_parser_lookup_name (cp_parser *parser, tree name,
16980 enum tag_types tag_type,
16983 bool check_dependency,
16984 tree *ambiguous_decls,
16985 location_t name_location)
16989 tree object_type = parser->context->object_type;
16991 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16992 flags |= LOOKUP_COMPLAIN;
16994 /* Assume that the lookup will be unambiguous. */
16995 if (ambiguous_decls)
16996 *ambiguous_decls = NULL_TREE;
16998 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16999 no longer valid. Note that if we are parsing tentatively, and
17000 the parse fails, OBJECT_TYPE will be automatically restored. */
17001 parser->context->object_type = NULL_TREE;
17003 if (name == error_mark_node)
17004 return error_mark_node;
17006 /* A template-id has already been resolved; there is no lookup to
17008 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17010 if (BASELINK_P (name))
17012 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17013 == TEMPLATE_ID_EXPR);
17017 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17018 it should already have been checked to make sure that the name
17019 used matches the type being destroyed. */
17020 if (TREE_CODE (name) == BIT_NOT_EXPR)
17024 /* Figure out to which type this destructor applies. */
17026 type = parser->scope;
17027 else if (object_type)
17028 type = object_type;
17030 type = current_class_type;
17031 /* If that's not a class type, there is no destructor. */
17032 if (!type || !CLASS_TYPE_P (type))
17033 return error_mark_node;
17034 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17035 lazily_declare_fn (sfk_destructor, type);
17036 if (!CLASSTYPE_DESTRUCTORS (type))
17037 return error_mark_node;
17038 /* If it was a class type, return the destructor. */
17039 return CLASSTYPE_DESTRUCTORS (type);
17042 /* By this point, the NAME should be an ordinary identifier. If
17043 the id-expression was a qualified name, the qualifying scope is
17044 stored in PARSER->SCOPE at this point. */
17045 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17047 /* Perform the lookup. */
17052 if (parser->scope == error_mark_node)
17053 return error_mark_node;
17055 /* If the SCOPE is dependent, the lookup must be deferred until
17056 the template is instantiated -- unless we are explicitly
17057 looking up names in uninstantiated templates. Even then, we
17058 cannot look up the name if the scope is not a class type; it
17059 might, for example, be a template type parameter. */
17060 dependent_p = (TYPE_P (parser->scope)
17061 && dependent_scope_p (parser->scope));
17062 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17064 /* Defer lookup. */
17065 decl = error_mark_node;
17068 tree pushed_scope = NULL_TREE;
17070 /* If PARSER->SCOPE is a dependent type, then it must be a
17071 class type, and we must not be checking dependencies;
17072 otherwise, we would have processed this lookup above. So
17073 that PARSER->SCOPE is not considered a dependent base by
17074 lookup_member, we must enter the scope here. */
17076 pushed_scope = push_scope (parser->scope);
17077 /* If the PARSER->SCOPE is a template specialization, it
17078 may be instantiated during name lookup. In that case,
17079 errors may be issued. Even if we rollback the current
17080 tentative parse, those errors are valid. */
17081 decl = lookup_qualified_name (parser->scope, name,
17082 tag_type != none_type,
17083 /*complain=*/true);
17085 /* If we have a single function from a using decl, pull it out. */
17086 if (TREE_CODE (decl) == OVERLOAD
17087 && !really_overloaded_fn (decl))
17088 decl = OVL_FUNCTION (decl);
17091 pop_scope (pushed_scope);
17094 /* If the scope is a dependent type and either we deferred lookup or
17095 we did lookup but didn't find the name, rememeber the name. */
17096 if (decl == error_mark_node && TYPE_P (parser->scope)
17097 && dependent_type_p (parser->scope))
17103 /* The resolution to Core Issue 180 says that `struct
17104 A::B' should be considered a type-name, even if `A'
17106 type = make_typename_type (parser->scope, name, tag_type,
17107 /*complain=*/tf_error);
17108 decl = TYPE_NAME (type);
17110 else if (is_template
17111 && (cp_parser_next_token_ends_template_argument_p (parser)
17112 || cp_lexer_next_token_is (parser->lexer,
17114 decl = make_unbound_class_template (parser->scope,
17116 /*complain=*/tf_error);
17118 decl = build_qualified_name (/*type=*/NULL_TREE,
17119 parser->scope, name,
17122 parser->qualifying_scope = parser->scope;
17123 parser->object_scope = NULL_TREE;
17125 else if (object_type)
17127 tree object_decl = NULL_TREE;
17128 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17129 OBJECT_TYPE is not a class. */
17130 if (CLASS_TYPE_P (object_type))
17131 /* If the OBJECT_TYPE is a template specialization, it may
17132 be instantiated during name lookup. In that case, errors
17133 may be issued. Even if we rollback the current tentative
17134 parse, those errors are valid. */
17135 object_decl = lookup_member (object_type,
17138 tag_type != none_type);
17139 /* Look it up in the enclosing context, too. */
17140 decl = lookup_name_real (name, tag_type != none_type,
17142 /*block_p=*/true, is_namespace, flags);
17143 parser->object_scope = object_type;
17144 parser->qualifying_scope = NULL_TREE;
17146 decl = object_decl;
17150 decl = lookup_name_real (name, tag_type != none_type,
17152 /*block_p=*/true, is_namespace, flags);
17153 parser->qualifying_scope = NULL_TREE;
17154 parser->object_scope = NULL_TREE;
17157 /* If the lookup failed, let our caller know. */
17158 if (!decl || decl == error_mark_node)
17159 return error_mark_node;
17161 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17162 if (TREE_CODE (decl) == TREE_LIST)
17164 if (ambiguous_decls)
17165 *ambiguous_decls = decl;
17166 /* The error message we have to print is too complicated for
17167 cp_parser_error, so we incorporate its actions directly. */
17168 if (!cp_parser_simulate_error (parser))
17170 error ("%Hreference to %qD is ambiguous",
17171 &name_location, name);
17172 print_candidates (decl);
17174 return error_mark_node;
17177 gcc_assert (DECL_P (decl)
17178 || TREE_CODE (decl) == OVERLOAD
17179 || TREE_CODE (decl) == SCOPE_REF
17180 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17181 || BASELINK_P (decl));
17183 /* If we have resolved the name of a member declaration, check to
17184 see if the declaration is accessible. When the name resolves to
17185 set of overloaded functions, accessibility is checked when
17186 overload resolution is done.
17188 During an explicit instantiation, access is not checked at all,
17189 as per [temp.explicit]. */
17191 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17196 /* Like cp_parser_lookup_name, but for use in the typical case where
17197 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17198 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17201 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17203 return cp_parser_lookup_name (parser, name,
17205 /*is_template=*/false,
17206 /*is_namespace=*/false,
17207 /*check_dependency=*/true,
17208 /*ambiguous_decls=*/NULL,
17212 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17213 the current context, return the TYPE_DECL. If TAG_NAME_P is
17214 true, the DECL indicates the class being defined in a class-head,
17215 or declared in an elaborated-type-specifier.
17217 Otherwise, return DECL. */
17220 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17222 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17223 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17226 template <typename T> struct B;
17229 template <typename T> struct A::B {};
17231 Similarly, in an elaborated-type-specifier:
17233 namespace N { struct X{}; }
17236 template <typename T> friend struct N::X;
17239 However, if the DECL refers to a class type, and we are in
17240 the scope of the class, then the name lookup automatically
17241 finds the TYPE_DECL created by build_self_reference rather
17242 than a TEMPLATE_DECL. For example, in:
17244 template <class T> struct S {
17248 there is no need to handle such case. */
17250 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17251 return DECL_TEMPLATE_RESULT (decl);
17256 /* If too many, or too few, template-parameter lists apply to the
17257 declarator, issue an error message. Returns TRUE if all went well,
17258 and FALSE otherwise. */
17261 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17262 cp_declarator *declarator,
17263 location_t declarator_location)
17265 unsigned num_templates;
17267 /* We haven't seen any classes that involve template parameters yet. */
17270 switch (declarator->kind)
17273 if (declarator->u.id.qualifying_scope)
17278 scope = declarator->u.id.qualifying_scope;
17279 member = declarator->u.id.unqualified_name;
17281 while (scope && CLASS_TYPE_P (scope))
17283 /* You're supposed to have one `template <...>'
17284 for every template class, but you don't need one
17285 for a full specialization. For example:
17287 template <class T> struct S{};
17288 template <> struct S<int> { void f(); };
17289 void S<int>::f () {}
17291 is correct; there shouldn't be a `template <>' for
17292 the definition of `S<int>::f'. */
17293 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17294 /* If SCOPE does not have template information of any
17295 kind, then it is not a template, nor is it nested
17296 within a template. */
17298 if (explicit_class_specialization_p (scope))
17300 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17303 scope = TYPE_CONTEXT (scope);
17306 else if (TREE_CODE (declarator->u.id.unqualified_name)
17307 == TEMPLATE_ID_EXPR)
17308 /* If the DECLARATOR has the form `X<y>' then it uses one
17309 additional level of template parameters. */
17312 return cp_parser_check_template_parameters
17313 (parser, num_templates, declarator_location, declarator);
17319 case cdk_reference:
17321 return (cp_parser_check_declarator_template_parameters
17322 (parser, declarator->declarator, declarator_location));
17328 gcc_unreachable ();
17333 /* NUM_TEMPLATES were used in the current declaration. If that is
17334 invalid, return FALSE and issue an error messages. Otherwise,
17335 return TRUE. If DECLARATOR is non-NULL, then we are checking a
17336 declarator and we can print more accurate diagnostics. */
17339 cp_parser_check_template_parameters (cp_parser* parser,
17340 unsigned num_templates,
17341 location_t location,
17342 cp_declarator *declarator)
17344 /* If there are the same number of template classes and parameter
17345 lists, that's OK. */
17346 if (parser->num_template_parameter_lists == num_templates)
17348 /* If there are more, but only one more, then we are referring to a
17349 member template. That's OK too. */
17350 if (parser->num_template_parameter_lists == num_templates + 1)
17352 /* If there are more template classes than parameter lists, we have
17355 template <class T> void S<T>::R<T>::f (); */
17356 if (parser->num_template_parameter_lists < num_templates)
17359 error_at (location, "specializing member %<%T::%E%> "
17360 "requires %<template<>%> syntax",
17361 declarator->u.id.qualifying_scope,
17362 declarator->u.id.unqualified_name);
17364 error_at (location, "too few template-parameter-lists");
17367 /* Otherwise, there are too many template parameter lists. We have
17370 template <class T> template <class U> void S::f(); */
17371 error ("%Htoo many template-parameter-lists", &location);
17375 /* Parse an optional `::' token indicating that the following name is
17376 from the global namespace. If so, PARSER->SCOPE is set to the
17377 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17378 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17379 Returns the new value of PARSER->SCOPE, if the `::' token is
17380 present, and NULL_TREE otherwise. */
17383 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17387 /* Peek at the next token. */
17388 token = cp_lexer_peek_token (parser->lexer);
17389 /* If we're looking at a `::' token then we're starting from the
17390 global namespace, not our current location. */
17391 if (token->type == CPP_SCOPE)
17393 /* Consume the `::' token. */
17394 cp_lexer_consume_token (parser->lexer);
17395 /* Set the SCOPE so that we know where to start the lookup. */
17396 parser->scope = global_namespace;
17397 parser->qualifying_scope = global_namespace;
17398 parser->object_scope = NULL_TREE;
17400 return parser->scope;
17402 else if (!current_scope_valid_p)
17404 parser->scope = NULL_TREE;
17405 parser->qualifying_scope = NULL_TREE;
17406 parser->object_scope = NULL_TREE;
17412 /* Returns TRUE if the upcoming token sequence is the start of a
17413 constructor declarator. If FRIEND_P is true, the declarator is
17414 preceded by the `friend' specifier. */
17417 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17419 bool constructor_p;
17420 tree type_decl = NULL_TREE;
17421 bool nested_name_p;
17422 cp_token *next_token;
17424 /* The common case is that this is not a constructor declarator, so
17425 try to avoid doing lots of work if at all possible. It's not
17426 valid declare a constructor at function scope. */
17427 if (parser->in_function_body)
17429 /* And only certain tokens can begin a constructor declarator. */
17430 next_token = cp_lexer_peek_token (parser->lexer);
17431 if (next_token->type != CPP_NAME
17432 && next_token->type != CPP_SCOPE
17433 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17434 && next_token->type != CPP_TEMPLATE_ID)
17437 /* Parse tentatively; we are going to roll back all of the tokens
17439 cp_parser_parse_tentatively (parser);
17440 /* Assume that we are looking at a constructor declarator. */
17441 constructor_p = true;
17443 /* Look for the optional `::' operator. */
17444 cp_parser_global_scope_opt (parser,
17445 /*current_scope_valid_p=*/false);
17446 /* Look for the nested-name-specifier. */
17448 = (cp_parser_nested_name_specifier_opt (parser,
17449 /*typename_keyword_p=*/false,
17450 /*check_dependency_p=*/false,
17452 /*is_declaration=*/false)
17454 /* Outside of a class-specifier, there must be a
17455 nested-name-specifier. */
17456 if (!nested_name_p &&
17457 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17459 constructor_p = false;
17460 /* If we still think that this might be a constructor-declarator,
17461 look for a class-name. */
17466 template <typename T> struct S { S(); };
17467 template <typename T> S<T>::S ();
17469 we must recognize that the nested `S' names a class.
17472 template <typename T> S<T>::S<T> ();
17474 we must recognize that the nested `S' names a template. */
17475 type_decl = cp_parser_class_name (parser,
17476 /*typename_keyword_p=*/false,
17477 /*template_keyword_p=*/false,
17479 /*check_dependency_p=*/false,
17480 /*class_head_p=*/false,
17481 /*is_declaration=*/false);
17482 /* If there was no class-name, then this is not a constructor. */
17483 constructor_p = !cp_parser_error_occurred (parser);
17486 /* If we're still considering a constructor, we have to see a `(',
17487 to begin the parameter-declaration-clause, followed by either a
17488 `)', an `...', or a decl-specifier. We need to check for a
17489 type-specifier to avoid being fooled into thinking that:
17493 is a constructor. (It is actually a function named `f' that
17494 takes one parameter (of type `int') and returns a value of type
17497 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17500 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17501 /* A parameter declaration begins with a decl-specifier,
17502 which is either the "attribute" keyword, a storage class
17503 specifier, or (usually) a type-specifier. */
17504 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17507 tree pushed_scope = NULL_TREE;
17508 unsigned saved_num_template_parameter_lists;
17510 /* Names appearing in the type-specifier should be looked up
17511 in the scope of the class. */
17512 if (current_class_type)
17516 type = TREE_TYPE (type_decl);
17517 if (TREE_CODE (type) == TYPENAME_TYPE)
17519 type = resolve_typename_type (type,
17520 /*only_current_p=*/false);
17521 if (TREE_CODE (type) == TYPENAME_TYPE)
17523 cp_parser_abort_tentative_parse (parser);
17527 pushed_scope = push_scope (type);
17530 /* Inside the constructor parameter list, surrounding
17531 template-parameter-lists do not apply. */
17532 saved_num_template_parameter_lists
17533 = parser->num_template_parameter_lists;
17534 parser->num_template_parameter_lists = 0;
17536 /* Look for the type-specifier. */
17537 cp_parser_type_specifier (parser,
17538 CP_PARSER_FLAGS_NONE,
17539 /*decl_specs=*/NULL,
17540 /*is_declarator=*/true,
17541 /*declares_class_or_enum=*/NULL,
17542 /*is_cv_qualifier=*/NULL);
17544 parser->num_template_parameter_lists
17545 = saved_num_template_parameter_lists;
17547 /* Leave the scope of the class. */
17549 pop_scope (pushed_scope);
17551 constructor_p = !cp_parser_error_occurred (parser);
17555 constructor_p = false;
17556 /* We did not really want to consume any tokens. */
17557 cp_parser_abort_tentative_parse (parser);
17559 return constructor_p;
17562 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17563 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17564 they must be performed once we are in the scope of the function.
17566 Returns the function defined. */
17569 cp_parser_function_definition_from_specifiers_and_declarator
17570 (cp_parser* parser,
17571 cp_decl_specifier_seq *decl_specifiers,
17573 const cp_declarator *declarator)
17578 /* Begin the function-definition. */
17579 success_p = start_function (decl_specifiers, declarator, attributes);
17581 /* The things we're about to see are not directly qualified by any
17582 template headers we've seen thus far. */
17583 reset_specialization ();
17585 /* If there were names looked up in the decl-specifier-seq that we
17586 did not check, check them now. We must wait until we are in the
17587 scope of the function to perform the checks, since the function
17588 might be a friend. */
17589 perform_deferred_access_checks ();
17593 /* Skip the entire function. */
17594 cp_parser_skip_to_end_of_block_or_statement (parser);
17595 fn = error_mark_node;
17597 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17599 /* Seen already, skip it. An error message has already been output. */
17600 cp_parser_skip_to_end_of_block_or_statement (parser);
17601 fn = current_function_decl;
17602 current_function_decl = NULL_TREE;
17603 /* If this is a function from a class, pop the nested class. */
17604 if (current_class_name)
17605 pop_nested_class ();
17608 fn = cp_parser_function_definition_after_declarator (parser,
17609 /*inline_p=*/false);
17614 /* Parse the part of a function-definition that follows the
17615 declarator. INLINE_P is TRUE iff this function is an inline
17616 function defined with a class-specifier.
17618 Returns the function defined. */
17621 cp_parser_function_definition_after_declarator (cp_parser* parser,
17625 bool ctor_initializer_p = false;
17626 bool saved_in_unbraced_linkage_specification_p;
17627 bool saved_in_function_body;
17628 unsigned saved_num_template_parameter_lists;
17631 saved_in_function_body = parser->in_function_body;
17632 parser->in_function_body = true;
17633 /* If the next token is `return', then the code may be trying to
17634 make use of the "named return value" extension that G++ used to
17636 token = cp_lexer_peek_token (parser->lexer);
17637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17639 /* Consume the `return' keyword. */
17640 cp_lexer_consume_token (parser->lexer);
17641 /* Look for the identifier that indicates what value is to be
17643 cp_parser_identifier (parser);
17644 /* Issue an error message. */
17645 error ("%Hnamed return values are no longer supported",
17647 /* Skip tokens until we reach the start of the function body. */
17650 cp_token *token = cp_lexer_peek_token (parser->lexer);
17651 if (token->type == CPP_OPEN_BRACE
17652 || token->type == CPP_EOF
17653 || token->type == CPP_PRAGMA_EOL)
17655 cp_lexer_consume_token (parser->lexer);
17658 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17659 anything declared inside `f'. */
17660 saved_in_unbraced_linkage_specification_p
17661 = parser->in_unbraced_linkage_specification_p;
17662 parser->in_unbraced_linkage_specification_p = false;
17663 /* Inside the function, surrounding template-parameter-lists do not
17665 saved_num_template_parameter_lists
17666 = parser->num_template_parameter_lists;
17667 parser->num_template_parameter_lists = 0;
17668 /* If the next token is `try', then we are looking at a
17669 function-try-block. */
17670 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17671 ctor_initializer_p = cp_parser_function_try_block (parser);
17672 /* A function-try-block includes the function-body, so we only do
17673 this next part if we're not processing a function-try-block. */
17676 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17678 /* Finish the function. */
17679 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17680 (inline_p ? 2 : 0));
17681 /* Generate code for it, if necessary. */
17682 expand_or_defer_fn (fn);
17683 /* Restore the saved values. */
17684 parser->in_unbraced_linkage_specification_p
17685 = saved_in_unbraced_linkage_specification_p;
17686 parser->num_template_parameter_lists
17687 = saved_num_template_parameter_lists;
17688 parser->in_function_body = saved_in_function_body;
17693 /* Parse a template-declaration, assuming that the `export' (and
17694 `extern') keywords, if present, has already been scanned. MEMBER_P
17695 is as for cp_parser_template_declaration. */
17698 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17700 tree decl = NULL_TREE;
17701 VEC (deferred_access_check,gc) *checks;
17702 tree parameter_list;
17703 bool friend_p = false;
17704 bool need_lang_pop;
17707 /* Look for the `template' keyword. */
17708 token = cp_lexer_peek_token (parser->lexer);
17709 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17713 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17715 if (at_class_scope_p () && current_function_decl)
17717 /* 14.5.2.2 [temp.mem]
17719 A local class shall not have member templates. */
17720 error ("%Hinvalid declaration of member template in local class",
17722 cp_parser_skip_to_end_of_block_or_statement (parser);
17727 A template ... shall not have C linkage. */
17728 if (current_lang_name == lang_name_c)
17730 error ("%Htemplate with C linkage", &token->location);
17731 /* Give it C++ linkage to avoid confusing other parts of the
17733 push_lang_context (lang_name_cplusplus);
17734 need_lang_pop = true;
17737 need_lang_pop = false;
17739 /* We cannot perform access checks on the template parameter
17740 declarations until we know what is being declared, just as we
17741 cannot check the decl-specifier list. */
17742 push_deferring_access_checks (dk_deferred);
17744 /* If the next token is `>', then we have an invalid
17745 specialization. Rather than complain about an invalid template
17746 parameter, issue an error message here. */
17747 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17749 cp_parser_error (parser, "invalid explicit specialization");
17750 begin_specialization ();
17751 parameter_list = NULL_TREE;
17754 /* Parse the template parameters. */
17755 parameter_list = cp_parser_template_parameter_list (parser);
17757 /* Get the deferred access checks from the parameter list. These
17758 will be checked once we know what is being declared, as for a
17759 member template the checks must be performed in the scope of the
17760 class containing the member. */
17761 checks = get_deferred_access_checks ();
17763 /* Look for the `>'. */
17764 cp_parser_skip_to_end_of_template_parameter_list (parser);
17765 /* We just processed one more parameter list. */
17766 ++parser->num_template_parameter_lists;
17767 /* If the next token is `template', there are more template
17769 if (cp_lexer_next_token_is_keyword (parser->lexer,
17771 cp_parser_template_declaration_after_export (parser, member_p);
17774 /* There are no access checks when parsing a template, as we do not
17775 know if a specialization will be a friend. */
17776 push_deferring_access_checks (dk_no_check);
17777 token = cp_lexer_peek_token (parser->lexer);
17778 decl = cp_parser_single_declaration (parser,
17781 /*explicit_specialization_p=*/false,
17783 pop_deferring_access_checks ();
17785 /* If this is a member template declaration, let the front
17787 if (member_p && !friend_p && decl)
17789 if (TREE_CODE (decl) == TYPE_DECL)
17790 cp_parser_check_access_in_redeclaration (decl, token->location);
17792 decl = finish_member_template_decl (decl);
17794 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17795 make_friend_class (current_class_type, TREE_TYPE (decl),
17796 /*complain=*/true);
17798 /* We are done with the current parameter list. */
17799 --parser->num_template_parameter_lists;
17801 pop_deferring_access_checks ();
17804 finish_template_decl (parameter_list);
17806 /* Register member declarations. */
17807 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17808 finish_member_declaration (decl);
17809 /* For the erroneous case of a template with C linkage, we pushed an
17810 implicit C++ linkage scope; exit that scope now. */
17812 pop_lang_context ();
17813 /* If DECL is a function template, we must return to parse it later.
17814 (Even though there is no definition, there might be default
17815 arguments that need handling.) */
17816 if (member_p && decl
17817 && (TREE_CODE (decl) == FUNCTION_DECL
17818 || DECL_FUNCTION_TEMPLATE_P (decl)))
17819 TREE_VALUE (parser->unparsed_functions_queues)
17820 = tree_cons (NULL_TREE, decl,
17821 TREE_VALUE (parser->unparsed_functions_queues));
17824 /* Perform the deferred access checks from a template-parameter-list.
17825 CHECKS is a TREE_LIST of access checks, as returned by
17826 get_deferred_access_checks. */
17829 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17831 ++processing_template_parmlist;
17832 perform_access_checks (checks);
17833 --processing_template_parmlist;
17836 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17837 `function-definition' sequence. MEMBER_P is true, this declaration
17838 appears in a class scope.
17840 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17841 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17844 cp_parser_single_declaration (cp_parser* parser,
17845 VEC (deferred_access_check,gc)* checks,
17847 bool explicit_specialization_p,
17850 int declares_class_or_enum;
17851 tree decl = NULL_TREE;
17852 cp_decl_specifier_seq decl_specifiers;
17853 bool function_definition_p = false;
17854 cp_token *decl_spec_token_start;
17856 /* This function is only used when processing a template
17858 gcc_assert (innermost_scope_kind () == sk_template_parms
17859 || innermost_scope_kind () == sk_template_spec);
17861 /* Defer access checks until we know what is being declared. */
17862 push_deferring_access_checks (dk_deferred);
17864 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17866 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17867 cp_parser_decl_specifier_seq (parser,
17868 CP_PARSER_FLAGS_OPTIONAL,
17870 &declares_class_or_enum);
17872 *friend_p = cp_parser_friend_p (&decl_specifiers);
17874 /* There are no template typedefs. */
17875 if (decl_specifiers.specs[(int) ds_typedef])
17877 error ("%Htemplate declaration of %qs",
17878 &decl_spec_token_start->location, "typedef");
17879 decl = error_mark_node;
17882 /* Gather up the access checks that occurred the
17883 decl-specifier-seq. */
17884 stop_deferring_access_checks ();
17886 /* Check for the declaration of a template class. */
17887 if (declares_class_or_enum)
17889 if (cp_parser_declares_only_class_p (parser))
17891 decl = shadow_tag (&decl_specifiers);
17896 friend template <typename T> struct A<T>::B;
17899 A<T>::B will be represented by a TYPENAME_TYPE, and
17900 therefore not recognized by shadow_tag. */
17901 if (friend_p && *friend_p
17903 && decl_specifiers.type
17904 && TYPE_P (decl_specifiers.type))
17905 decl = decl_specifiers.type;
17907 if (decl && decl != error_mark_node)
17908 decl = TYPE_NAME (decl);
17910 decl = error_mark_node;
17912 /* Perform access checks for template parameters. */
17913 cp_parser_perform_template_parameter_access_checks (checks);
17916 /* If it's not a template class, try for a template function. If
17917 the next token is a `;', then this declaration does not declare
17918 anything. But, if there were errors in the decl-specifiers, then
17919 the error might well have come from an attempted class-specifier.
17920 In that case, there's no need to warn about a missing declarator. */
17922 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17923 || decl_specifiers.type != error_mark_node))
17925 decl = cp_parser_init_declarator (parser,
17928 /*function_definition_allowed_p=*/true,
17930 declares_class_or_enum,
17931 &function_definition_p);
17933 /* 7.1.1-1 [dcl.stc]
17935 A storage-class-specifier shall not be specified in an explicit
17936 specialization... */
17938 && explicit_specialization_p
17939 && decl_specifiers.storage_class != sc_none)
17941 error ("%Hexplicit template specialization cannot have a storage class",
17942 &decl_spec_token_start->location);
17943 decl = error_mark_node;
17947 pop_deferring_access_checks ();
17949 /* Clear any current qualification; whatever comes next is the start
17950 of something new. */
17951 parser->scope = NULL_TREE;
17952 parser->qualifying_scope = NULL_TREE;
17953 parser->object_scope = NULL_TREE;
17954 /* Look for a trailing `;' after the declaration. */
17955 if (!function_definition_p
17956 && (decl == error_mark_node
17957 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17958 cp_parser_skip_to_end_of_block_or_statement (parser);
17963 /* Parse a cast-expression that is not the operand of a unary "&". */
17966 cp_parser_simple_cast_expression (cp_parser *parser)
17968 return cp_parser_cast_expression (parser, /*address_p=*/false,
17969 /*cast_p=*/false, NULL);
17972 /* Parse a functional cast to TYPE. Returns an expression
17973 representing the cast. */
17976 cp_parser_functional_cast (cp_parser* parser, tree type)
17978 tree expression_list;
17982 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17984 maybe_warn_cpp0x ("extended initializer lists");
17985 expression_list = cp_parser_braced_list (parser, &nonconst_p);
17986 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17987 if (TREE_CODE (type) == TYPE_DECL)
17988 type = TREE_TYPE (type);
17989 return finish_compound_literal (type, expression_list);
17993 = cp_parser_parenthesized_expression_list (parser, false,
17995 /*allow_expansion_p=*/true,
17996 /*non_constant_p=*/NULL);
17998 cast = build_functional_cast (type, expression_list,
17999 tf_warning_or_error);
18000 /* [expr.const]/1: In an integral constant expression "only type
18001 conversions to integral or enumeration type can be used". */
18002 if (TREE_CODE (type) == TYPE_DECL)
18003 type = TREE_TYPE (type);
18004 if (cast != error_mark_node
18005 && !cast_valid_in_integral_constant_expression_p (type)
18006 && (cp_parser_non_integral_constant_expression
18007 (parser, "a call to a constructor")))
18008 return error_mark_node;
18012 /* Save the tokens that make up the body of a member function defined
18013 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18014 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18015 specifiers applied to the declaration. Returns the FUNCTION_DECL
18016 for the member function. */
18019 cp_parser_save_member_function_body (cp_parser* parser,
18020 cp_decl_specifier_seq *decl_specifiers,
18021 cp_declarator *declarator,
18028 /* Create the function-declaration. */
18029 fn = start_method (decl_specifiers, declarator, attributes);
18030 /* If something went badly wrong, bail out now. */
18031 if (fn == error_mark_node)
18033 /* If there's a function-body, skip it. */
18034 if (cp_parser_token_starts_function_definition_p
18035 (cp_lexer_peek_token (parser->lexer)))
18036 cp_parser_skip_to_end_of_block_or_statement (parser);
18037 return error_mark_node;
18040 /* Remember it, if there default args to post process. */
18041 cp_parser_save_default_args (parser, fn);
18043 /* Save away the tokens that make up the body of the
18045 first = parser->lexer->next_token;
18046 /* We can have braced-init-list mem-initializers before the fn body. */
18047 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18049 cp_lexer_consume_token (parser->lexer);
18050 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18051 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18053 /* cache_group will stop after an un-nested { } pair, too. */
18054 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18057 /* variadic mem-inits have ... after the ')'. */
18058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18059 cp_lexer_consume_token (parser->lexer);
18062 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18063 /* Handle function try blocks. */
18064 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18065 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18066 last = parser->lexer->next_token;
18068 /* Save away the inline definition; we will process it when the
18069 class is complete. */
18070 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18071 DECL_PENDING_INLINE_P (fn) = 1;
18073 /* We need to know that this was defined in the class, so that
18074 friend templates are handled correctly. */
18075 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18077 /* We're done with the inline definition. */
18078 finish_method (fn);
18080 /* Add FN to the queue of functions to be parsed later. */
18081 TREE_VALUE (parser->unparsed_functions_queues)
18082 = tree_cons (NULL_TREE, fn,
18083 TREE_VALUE (parser->unparsed_functions_queues));
18088 /* Parse a template-argument-list, as well as the trailing ">" (but
18089 not the opening ">"). See cp_parser_template_argument_list for the
18093 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18097 tree saved_qualifying_scope;
18098 tree saved_object_scope;
18099 bool saved_greater_than_is_operator_p;
18100 bool saved_skip_evaluation;
18104 When parsing a template-id, the first non-nested `>' is taken as
18105 the end of the template-argument-list rather than a greater-than
18107 saved_greater_than_is_operator_p
18108 = parser->greater_than_is_operator_p;
18109 parser->greater_than_is_operator_p = false;
18110 /* Parsing the argument list may modify SCOPE, so we save it
18112 saved_scope = parser->scope;
18113 saved_qualifying_scope = parser->qualifying_scope;
18114 saved_object_scope = parser->object_scope;
18115 /* We need to evaluate the template arguments, even though this
18116 template-id may be nested within a "sizeof". */
18117 saved_skip_evaluation = skip_evaluation;
18118 skip_evaluation = false;
18119 /* Parse the template-argument-list itself. */
18120 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18121 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18122 arguments = NULL_TREE;
18124 arguments = cp_parser_template_argument_list (parser);
18125 /* Look for the `>' that ends the template-argument-list. If we find
18126 a '>>' instead, it's probably just a typo. */
18127 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18129 if (cxx_dialect != cxx98)
18131 /* In C++0x, a `>>' in a template argument list or cast
18132 expression is considered to be two separate `>'
18133 tokens. So, change the current token to a `>', but don't
18134 consume it: it will be consumed later when the outer
18135 template argument list (or cast expression) is parsed.
18136 Note that this replacement of `>' for `>>' is necessary
18137 even if we are parsing tentatively: in the tentative
18138 case, after calling
18139 cp_parser_enclosed_template_argument_list we will always
18140 throw away all of the template arguments and the first
18141 closing `>', either because the template argument list
18142 was erroneous or because we are replacing those tokens
18143 with a CPP_TEMPLATE_ID token. The second `>' (which will
18144 not have been thrown away) is needed either to close an
18145 outer template argument list or to complete a new-style
18147 cp_token *token = cp_lexer_peek_token (parser->lexer);
18148 token->type = CPP_GREATER;
18150 else if (!saved_greater_than_is_operator_p)
18152 /* If we're in a nested template argument list, the '>>' has
18153 to be a typo for '> >'. We emit the error message, but we
18154 continue parsing and we push a '>' as next token, so that
18155 the argument list will be parsed correctly. Note that the
18156 global source location is still on the token before the
18157 '>>', so we need to say explicitly where we want it. */
18158 cp_token *token = cp_lexer_peek_token (parser->lexer);
18159 error ("%H%<>>%> should be %<> >%> "
18160 "within a nested template argument list",
18163 token->type = CPP_GREATER;
18167 /* If this is not a nested template argument list, the '>>'
18168 is a typo for '>'. Emit an error message and continue.
18169 Same deal about the token location, but here we can get it
18170 right by consuming the '>>' before issuing the diagnostic. */
18171 cp_token *token = cp_lexer_consume_token (parser->lexer);
18172 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18173 "a template argument list", &token->location);
18177 cp_parser_skip_to_end_of_template_parameter_list (parser);
18178 /* The `>' token might be a greater-than operator again now. */
18179 parser->greater_than_is_operator_p
18180 = saved_greater_than_is_operator_p;
18181 /* Restore the SAVED_SCOPE. */
18182 parser->scope = saved_scope;
18183 parser->qualifying_scope = saved_qualifying_scope;
18184 parser->object_scope = saved_object_scope;
18185 skip_evaluation = saved_skip_evaluation;
18190 /* MEMBER_FUNCTION is a member function, or a friend. If default
18191 arguments, or the body of the function have not yet been parsed,
18195 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18197 /* If this member is a template, get the underlying
18199 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18200 member_function = DECL_TEMPLATE_RESULT (member_function);
18202 /* There should not be any class definitions in progress at this
18203 point; the bodies of members are only parsed outside of all class
18205 gcc_assert (parser->num_classes_being_defined == 0);
18206 /* While we're parsing the member functions we might encounter more
18207 classes. We want to handle them right away, but we don't want
18208 them getting mixed up with functions that are currently in the
18210 parser->unparsed_functions_queues
18211 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18213 /* Make sure that any template parameters are in scope. */
18214 maybe_begin_member_template_processing (member_function);
18216 /* If the body of the function has not yet been parsed, parse it
18218 if (DECL_PENDING_INLINE_P (member_function))
18220 tree function_scope;
18221 cp_token_cache *tokens;
18223 /* The function is no longer pending; we are processing it. */
18224 tokens = DECL_PENDING_INLINE_INFO (member_function);
18225 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18226 DECL_PENDING_INLINE_P (member_function) = 0;
18228 /* If this is a local class, enter the scope of the containing
18230 function_scope = current_function_decl;
18231 if (function_scope)
18232 push_function_context ();
18234 /* Push the body of the function onto the lexer stack. */
18235 cp_parser_push_lexer_for_tokens (parser, tokens);
18237 /* Let the front end know that we going to be defining this
18239 start_preparsed_function (member_function, NULL_TREE,
18240 SF_PRE_PARSED | SF_INCLASS_INLINE);
18242 /* Don't do access checking if it is a templated function. */
18243 if (processing_template_decl)
18244 push_deferring_access_checks (dk_no_check);
18246 /* Now, parse the body of the function. */
18247 cp_parser_function_definition_after_declarator (parser,
18248 /*inline_p=*/true);
18250 if (processing_template_decl)
18251 pop_deferring_access_checks ();
18253 /* Leave the scope of the containing function. */
18254 if (function_scope)
18255 pop_function_context ();
18256 cp_parser_pop_lexer (parser);
18259 /* Remove any template parameters from the symbol table. */
18260 maybe_end_member_template_processing ();
18262 /* Restore the queue. */
18263 parser->unparsed_functions_queues
18264 = TREE_CHAIN (parser->unparsed_functions_queues);
18267 /* If DECL contains any default args, remember it on the unparsed
18268 functions queue. */
18271 cp_parser_save_default_args (cp_parser* parser, tree decl)
18275 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18277 probe = TREE_CHAIN (probe))
18278 if (TREE_PURPOSE (probe))
18280 TREE_PURPOSE (parser->unparsed_functions_queues)
18281 = tree_cons (current_class_type, decl,
18282 TREE_PURPOSE (parser->unparsed_functions_queues));
18287 /* FN is a FUNCTION_DECL which may contains a parameter with an
18288 unparsed DEFAULT_ARG. Parse the default args now. This function
18289 assumes that the current scope is the scope in which the default
18290 argument should be processed. */
18293 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18295 bool saved_local_variables_forbidden_p;
18298 /* While we're parsing the default args, we might (due to the
18299 statement expression extension) encounter more classes. We want
18300 to handle them right away, but we don't want them getting mixed
18301 up with default args that are currently in the queue. */
18302 parser->unparsed_functions_queues
18303 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18305 /* Local variable names (and the `this' keyword) may not appear
18306 in a default argument. */
18307 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18308 parser->local_variables_forbidden_p = true;
18310 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18312 parm = TREE_CHAIN (parm))
18314 cp_token_cache *tokens;
18315 tree default_arg = TREE_PURPOSE (parm);
18317 VEC(tree,gc) *insts;
18324 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18325 /* This can happen for a friend declaration for a function
18326 already declared with default arguments. */
18329 /* Push the saved tokens for the default argument onto the parser's
18331 tokens = DEFARG_TOKENS (default_arg);
18332 cp_parser_push_lexer_for_tokens (parser, tokens);
18334 /* Parse the assignment-expression. */
18335 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18336 if (parsed_arg == error_mark_node)
18338 cp_parser_pop_lexer (parser);
18342 if (!processing_template_decl)
18343 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18345 TREE_PURPOSE (parm) = parsed_arg;
18347 /* Update any instantiations we've already created. */
18348 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18349 VEC_iterate (tree, insts, ix, copy); ix++)
18350 TREE_PURPOSE (copy) = parsed_arg;
18352 /* If the token stream has not been completely used up, then
18353 there was extra junk after the end of the default
18355 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18356 cp_parser_error (parser, "expected %<,%>");
18358 /* Revert to the main lexer. */
18359 cp_parser_pop_lexer (parser);
18362 /* Make sure no default arg is missing. */
18363 check_default_args (fn);
18365 /* Restore the state of local_variables_forbidden_p. */
18366 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18368 /* Restore the queue. */
18369 parser->unparsed_functions_queues
18370 = TREE_CHAIN (parser->unparsed_functions_queues);
18373 /* Parse the operand of `sizeof' (or a similar operator). Returns
18374 either a TYPE or an expression, depending on the form of the
18375 input. The KEYWORD indicates which kind of expression we have
18379 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18381 tree expr = NULL_TREE;
18382 const char *saved_message;
18384 bool saved_integral_constant_expression_p;
18385 bool saved_non_integral_constant_expression_p;
18386 bool pack_expansion_p = false;
18388 /* Types cannot be defined in a `sizeof' expression. Save away the
18390 saved_message = parser->type_definition_forbidden_message;
18391 /* And create the new one. */
18392 tmp = concat ("types may not be defined in %<",
18393 IDENTIFIER_POINTER (ridpointers[keyword]),
18394 "%> expressions", NULL);
18395 parser->type_definition_forbidden_message = tmp;
18397 /* The restrictions on constant-expressions do not apply inside
18398 sizeof expressions. */
18399 saved_integral_constant_expression_p
18400 = parser->integral_constant_expression_p;
18401 saved_non_integral_constant_expression_p
18402 = parser->non_integral_constant_expression_p;
18403 parser->integral_constant_expression_p = false;
18405 /* If it's a `...', then we are computing the length of a parameter
18407 if (keyword == RID_SIZEOF
18408 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18410 /* Consume the `...'. */
18411 cp_lexer_consume_token (parser->lexer);
18412 maybe_warn_variadic_templates ();
18414 /* Note that this is an expansion. */
18415 pack_expansion_p = true;
18418 /* Do not actually evaluate the expression. */
18420 /* If it's a `(', then we might be looking at the type-id
18422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18425 bool saved_in_type_id_in_expr_p;
18427 /* We can't be sure yet whether we're looking at a type-id or an
18429 cp_parser_parse_tentatively (parser);
18430 /* Consume the `('. */
18431 cp_lexer_consume_token (parser->lexer);
18432 /* Parse the type-id. */
18433 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18434 parser->in_type_id_in_expr_p = true;
18435 type = cp_parser_type_id (parser);
18436 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18437 /* Now, look for the trailing `)'. */
18438 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18439 /* If all went well, then we're done. */
18440 if (cp_parser_parse_definitely (parser))
18442 cp_decl_specifier_seq decl_specs;
18444 /* Build a trivial decl-specifier-seq. */
18445 clear_decl_specs (&decl_specs);
18446 decl_specs.type = type;
18448 /* Call grokdeclarator to figure out what type this is. */
18449 expr = grokdeclarator (NULL,
18453 /*attrlist=*/NULL);
18457 /* If the type-id production did not work out, then we must be
18458 looking at the unary-expression production. */
18460 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18461 /*cast_p=*/false, NULL);
18463 if (pack_expansion_p)
18464 /* Build a pack expansion. */
18465 expr = make_pack_expansion (expr);
18467 /* Go back to evaluating expressions. */
18470 /* Free the message we created. */
18472 /* And restore the old one. */
18473 parser->type_definition_forbidden_message = saved_message;
18474 parser->integral_constant_expression_p
18475 = saved_integral_constant_expression_p;
18476 parser->non_integral_constant_expression_p
18477 = saved_non_integral_constant_expression_p;
18482 /* If the current declaration has no declarator, return true. */
18485 cp_parser_declares_only_class_p (cp_parser *parser)
18487 /* If the next token is a `;' or a `,' then there is no
18489 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18490 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18493 /* Update the DECL_SPECS to reflect the storage class indicated by
18497 cp_parser_set_storage_class (cp_parser *parser,
18498 cp_decl_specifier_seq *decl_specs,
18500 location_t location)
18502 cp_storage_class storage_class;
18504 if (parser->in_unbraced_linkage_specification_p)
18506 error ("%Hinvalid use of %qD in linkage specification",
18507 &location, ridpointers[keyword]);
18510 else if (decl_specs->storage_class != sc_none)
18512 decl_specs->conflicting_specifiers_p = true;
18516 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18517 && decl_specs->specs[(int) ds_thread])
18519 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18520 decl_specs->specs[(int) ds_thread] = 0;
18526 storage_class = sc_auto;
18529 storage_class = sc_register;
18532 storage_class = sc_static;
18535 storage_class = sc_extern;
18538 storage_class = sc_mutable;
18541 gcc_unreachable ();
18543 decl_specs->storage_class = storage_class;
18545 /* A storage class specifier cannot be applied alongside a typedef
18546 specifier. If there is a typedef specifier present then set
18547 conflicting_specifiers_p which will trigger an error later
18548 on in grokdeclarator. */
18549 if (decl_specs->specs[(int)ds_typedef])
18550 decl_specs->conflicting_specifiers_p = true;
18553 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18554 is true, the type is a user-defined type; otherwise it is a
18555 built-in type specified by a keyword. */
18558 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18560 location_t location,
18561 bool user_defined_p)
18563 decl_specs->any_specifiers_p = true;
18565 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18566 (with, for example, in "typedef int wchar_t;") we remember that
18567 this is what happened. In system headers, we ignore these
18568 declarations so that G++ can work with system headers that are not
18570 if (decl_specs->specs[(int) ds_typedef]
18572 && (type_spec == boolean_type_node
18573 || type_spec == char16_type_node
18574 || type_spec == char32_type_node
18575 || type_spec == wchar_type_node)
18576 && (decl_specs->type
18577 || decl_specs->specs[(int) ds_long]
18578 || decl_specs->specs[(int) ds_short]
18579 || decl_specs->specs[(int) ds_unsigned]
18580 || decl_specs->specs[(int) ds_signed]))
18582 decl_specs->redefined_builtin_type = type_spec;
18583 if (!decl_specs->type)
18585 decl_specs->type = type_spec;
18586 decl_specs->user_defined_type_p = false;
18587 decl_specs->type_location = location;
18590 else if (decl_specs->type)
18591 decl_specs->multiple_types_p = true;
18594 decl_specs->type = type_spec;
18595 decl_specs->user_defined_type_p = user_defined_p;
18596 decl_specs->redefined_builtin_type = NULL_TREE;
18597 decl_specs->type_location = location;
18601 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18602 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18605 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18607 return decl_specifiers->specs[(int) ds_friend] != 0;
18610 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18611 issue an error message indicating that TOKEN_DESC was expected.
18613 Returns the token consumed, if the token had the appropriate type.
18614 Otherwise, returns NULL. */
18617 cp_parser_require (cp_parser* parser,
18618 enum cpp_ttype type,
18619 const char* token_desc)
18621 if (cp_lexer_next_token_is (parser->lexer, type))
18622 return cp_lexer_consume_token (parser->lexer);
18625 /* Output the MESSAGE -- unless we're parsing tentatively. */
18626 if (!cp_parser_simulate_error (parser))
18628 char *message = concat ("expected ", token_desc, NULL);
18629 cp_parser_error (parser, message);
18636 /* An error message is produced if the next token is not '>'.
18637 All further tokens are skipped until the desired token is
18638 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18641 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18643 /* Current level of '< ... >'. */
18644 unsigned level = 0;
18645 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18646 unsigned nesting_depth = 0;
18648 /* Are we ready, yet? If not, issue error message. */
18649 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18652 /* Skip tokens until the desired token is found. */
18655 /* Peek at the next token. */
18656 switch (cp_lexer_peek_token (parser->lexer)->type)
18659 if (!nesting_depth)
18664 if (cxx_dialect == cxx98)
18665 /* C++0x views the `>>' operator as two `>' tokens, but
18668 else if (!nesting_depth && level-- == 0)
18670 /* We've hit a `>>' where the first `>' closes the
18671 template argument list, and the second `>' is
18672 spurious. Just consume the `>>' and stop; we've
18673 already produced at least one error. */
18674 cp_lexer_consume_token (parser->lexer);
18677 /* Fall through for C++0x, so we handle the second `>' in
18681 if (!nesting_depth && level-- == 0)
18683 /* We've reached the token we want, consume it and stop. */
18684 cp_lexer_consume_token (parser->lexer);
18689 case CPP_OPEN_PAREN:
18690 case CPP_OPEN_SQUARE:
18694 case CPP_CLOSE_PAREN:
18695 case CPP_CLOSE_SQUARE:
18696 if (nesting_depth-- == 0)
18701 case CPP_PRAGMA_EOL:
18702 case CPP_SEMICOLON:
18703 case CPP_OPEN_BRACE:
18704 case CPP_CLOSE_BRACE:
18705 /* The '>' was probably forgotten, don't look further. */
18712 /* Consume this token. */
18713 cp_lexer_consume_token (parser->lexer);
18717 /* If the next token is the indicated keyword, consume it. Otherwise,
18718 issue an error message indicating that TOKEN_DESC was expected.
18720 Returns the token consumed, if the token had the appropriate type.
18721 Otherwise, returns NULL. */
18724 cp_parser_require_keyword (cp_parser* parser,
18726 const char* token_desc)
18728 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18730 if (token && token->keyword != keyword)
18732 dyn_string_t error_msg;
18734 /* Format the error message. */
18735 error_msg = dyn_string_new (0);
18736 dyn_string_append_cstr (error_msg, "expected ");
18737 dyn_string_append_cstr (error_msg, token_desc);
18738 cp_parser_error (parser, error_msg->s);
18739 dyn_string_delete (error_msg);
18746 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18747 function-definition. */
18750 cp_parser_token_starts_function_definition_p (cp_token* token)
18752 return (/* An ordinary function-body begins with an `{'. */
18753 token->type == CPP_OPEN_BRACE
18754 /* A ctor-initializer begins with a `:'. */
18755 || token->type == CPP_COLON
18756 /* A function-try-block begins with `try'. */
18757 || token->keyword == RID_TRY
18758 /* The named return value extension begins with `return'. */
18759 || token->keyword == RID_RETURN);
18762 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18766 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18770 token = cp_lexer_peek_token (parser->lexer);
18771 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18774 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18775 C++0x) ending a template-argument. */
18778 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18782 token = cp_lexer_peek_token (parser->lexer);
18783 return (token->type == CPP_COMMA
18784 || token->type == CPP_GREATER
18785 || token->type == CPP_ELLIPSIS
18786 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18789 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18790 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18793 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18798 token = cp_lexer_peek_nth_token (parser->lexer, n);
18799 if (token->type == CPP_LESS)
18801 /* Check for the sequence `<::' in the original code. It would be lexed as
18802 `[:', where `[' is a digraph, and there is no whitespace before
18804 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18807 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18808 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18814 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18815 or none_type otherwise. */
18817 static enum tag_types
18818 cp_parser_token_is_class_key (cp_token* token)
18820 switch (token->keyword)
18825 return record_type;
18834 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18837 cp_parser_check_class_key (enum tag_types class_key, tree type)
18839 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18840 permerror (input_location, "%qs tag used in naming %q#T",
18841 class_key == union_type ? "union"
18842 : class_key == record_type ? "struct" : "class",
18846 /* Issue an error message if DECL is redeclared with different
18847 access than its original declaration [class.access.spec/3].
18848 This applies to nested classes and nested class templates.
18852 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18854 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18857 if ((TREE_PRIVATE (decl)
18858 != (current_access_specifier == access_private_node))
18859 || (TREE_PROTECTED (decl)
18860 != (current_access_specifier == access_protected_node)))
18861 error ("%H%qD redeclared with different access", &location, decl);
18864 /* Look for the `template' keyword, as a syntactic disambiguator.
18865 Return TRUE iff it is present, in which case it will be
18869 cp_parser_optional_template_keyword (cp_parser *parser)
18871 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18873 /* The `template' keyword can only be used within templates;
18874 outside templates the parser can always figure out what is a
18875 template and what is not. */
18876 if (!processing_template_decl)
18878 cp_token *token = cp_lexer_peek_token (parser->lexer);
18879 error ("%H%<template%> (as a disambiguator) is only allowed "
18880 "within templates", &token->location);
18881 /* If this part of the token stream is rescanned, the same
18882 error message would be generated. So, we purge the token
18883 from the stream. */
18884 cp_lexer_purge_token (parser->lexer);
18889 /* Consume the `template' keyword. */
18890 cp_lexer_consume_token (parser->lexer);
18898 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18899 set PARSER->SCOPE, and perform other related actions. */
18902 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18905 struct tree_check *check_value;
18906 deferred_access_check *chk;
18907 VEC (deferred_access_check,gc) *checks;
18909 /* Get the stored value. */
18910 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18911 /* Perform any access checks that were deferred. */
18912 checks = check_value->checks;
18916 VEC_iterate (deferred_access_check, checks, i, chk) ;
18919 perform_or_defer_access_check (chk->binfo,
18924 /* Set the scope from the stored value. */
18925 parser->scope = check_value->value;
18926 parser->qualifying_scope = check_value->qualifying_scope;
18927 parser->object_scope = NULL_TREE;
18930 /* Consume tokens up through a non-nested END token. Returns TRUE if we
18931 encounter the end of a block before what we were looking for. */
18934 cp_parser_cache_group (cp_parser *parser,
18935 enum cpp_ttype end,
18940 cp_token *token = cp_lexer_peek_token (parser->lexer);
18942 /* Abort a parenthesized expression if we encounter a semicolon. */
18943 if ((end == CPP_CLOSE_PAREN || depth == 0)
18944 && token->type == CPP_SEMICOLON)
18946 /* If we've reached the end of the file, stop. */
18947 if (token->type == CPP_EOF
18948 || (end != CPP_PRAGMA_EOL
18949 && token->type == CPP_PRAGMA_EOL))
18951 if (token->type == CPP_CLOSE_BRACE && depth == 0)
18952 /* We've hit the end of an enclosing block, so there's been some
18953 kind of syntax error. */
18956 /* Consume the token. */
18957 cp_lexer_consume_token (parser->lexer);
18958 /* See if it starts a new group. */
18959 if (token->type == CPP_OPEN_BRACE)
18961 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18962 /* In theory this should probably check end == '}', but
18963 cp_parser_save_member_function_body needs it to exit
18964 after either '}' or ')' when called with ')'. */
18968 else if (token->type == CPP_OPEN_PAREN)
18970 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18971 if (depth == 0 && end == CPP_CLOSE_PAREN)
18974 else if (token->type == CPP_PRAGMA)
18975 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18976 else if (token->type == end)
18981 /* Begin parsing tentatively. We always save tokens while parsing
18982 tentatively so that if the tentative parsing fails we can restore the
18986 cp_parser_parse_tentatively (cp_parser* parser)
18988 /* Enter a new parsing context. */
18989 parser->context = cp_parser_context_new (parser->context);
18990 /* Begin saving tokens. */
18991 cp_lexer_save_tokens (parser->lexer);
18992 /* In order to avoid repetitive access control error messages,
18993 access checks are queued up until we are no longer parsing
18995 push_deferring_access_checks (dk_deferred);
18998 /* Commit to the currently active tentative parse. */
19001 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19003 cp_parser_context *context;
19006 /* Mark all of the levels as committed. */
19007 lexer = parser->lexer;
19008 for (context = parser->context; context->next; context = context->next)
19010 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19012 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19013 while (!cp_lexer_saving_tokens (lexer))
19014 lexer = lexer->next;
19015 cp_lexer_commit_tokens (lexer);
19019 /* Abort the currently active tentative parse. All consumed tokens
19020 will be rolled back, and no diagnostics will be issued. */
19023 cp_parser_abort_tentative_parse (cp_parser* parser)
19025 cp_parser_simulate_error (parser);
19026 /* Now, pretend that we want to see if the construct was
19027 successfully parsed. */
19028 cp_parser_parse_definitely (parser);
19031 /* Stop parsing tentatively. If a parse error has occurred, restore the
19032 token stream. Otherwise, commit to the tokens we have consumed.
19033 Returns true if no error occurred; false otherwise. */
19036 cp_parser_parse_definitely (cp_parser* parser)
19038 bool error_occurred;
19039 cp_parser_context *context;
19041 /* Remember whether or not an error occurred, since we are about to
19042 destroy that information. */
19043 error_occurred = cp_parser_error_occurred (parser);
19044 /* Remove the topmost context from the stack. */
19045 context = parser->context;
19046 parser->context = context->next;
19047 /* If no parse errors occurred, commit to the tentative parse. */
19048 if (!error_occurred)
19050 /* Commit to the tokens read tentatively, unless that was
19052 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19053 cp_lexer_commit_tokens (parser->lexer);
19055 pop_to_parent_deferring_access_checks ();
19057 /* Otherwise, if errors occurred, roll back our state so that things
19058 are just as they were before we began the tentative parse. */
19061 cp_lexer_rollback_tokens (parser->lexer);
19062 pop_deferring_access_checks ();
19064 /* Add the context to the front of the free list. */
19065 context->next = cp_parser_context_free_list;
19066 cp_parser_context_free_list = context;
19068 return !error_occurred;
19071 /* Returns true if we are parsing tentatively and are not committed to
19072 this tentative parse. */
19075 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19077 return (cp_parser_parsing_tentatively (parser)
19078 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19081 /* Returns nonzero iff an error has occurred during the most recent
19082 tentative parse. */
19085 cp_parser_error_occurred (cp_parser* parser)
19087 return (cp_parser_parsing_tentatively (parser)
19088 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19091 /* Returns nonzero if GNU extensions are allowed. */
19094 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19096 return parser->allow_gnu_extensions_p;
19099 /* Objective-C++ Productions */
19102 /* Parse an Objective-C expression, which feeds into a primary-expression
19106 objc-message-expression
19107 objc-string-literal
19108 objc-encode-expression
19109 objc-protocol-expression
19110 objc-selector-expression
19112 Returns a tree representation of the expression. */
19115 cp_parser_objc_expression (cp_parser* parser)
19117 /* Try to figure out what kind of declaration is present. */
19118 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19122 case CPP_OPEN_SQUARE:
19123 return cp_parser_objc_message_expression (parser);
19125 case CPP_OBJC_STRING:
19126 kwd = cp_lexer_consume_token (parser->lexer);
19127 return objc_build_string_object (kwd->u.value);
19130 switch (kwd->keyword)
19132 case RID_AT_ENCODE:
19133 return cp_parser_objc_encode_expression (parser);
19135 case RID_AT_PROTOCOL:
19136 return cp_parser_objc_protocol_expression (parser);
19138 case RID_AT_SELECTOR:
19139 return cp_parser_objc_selector_expression (parser);
19145 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19146 &kwd->location, kwd->u.value);
19147 cp_parser_skip_to_end_of_block_or_statement (parser);
19150 return error_mark_node;
19153 /* Parse an Objective-C message expression.
19155 objc-message-expression:
19156 [ objc-message-receiver objc-message-args ]
19158 Returns a representation of an Objective-C message. */
19161 cp_parser_objc_message_expression (cp_parser* parser)
19163 tree receiver, messageargs;
19165 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19166 receiver = cp_parser_objc_message_receiver (parser);
19167 messageargs = cp_parser_objc_message_args (parser);
19168 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19170 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19173 /* Parse an objc-message-receiver.
19175 objc-message-receiver:
19177 simple-type-specifier
19179 Returns a representation of the type or expression. */
19182 cp_parser_objc_message_receiver (cp_parser* parser)
19186 /* An Objective-C message receiver may be either (1) a type
19187 or (2) an expression. */
19188 cp_parser_parse_tentatively (parser);
19189 rcv = cp_parser_expression (parser, false, NULL);
19191 if (cp_parser_parse_definitely (parser))
19194 rcv = cp_parser_simple_type_specifier (parser,
19195 /*decl_specs=*/NULL,
19196 CP_PARSER_FLAGS_NONE);
19198 return objc_get_class_reference (rcv);
19201 /* Parse the arguments and selectors comprising an Objective-C message.
19206 objc-selector-args , objc-comma-args
19208 objc-selector-args:
19209 objc-selector [opt] : assignment-expression
19210 objc-selector-args objc-selector [opt] : assignment-expression
19213 assignment-expression
19214 objc-comma-args , assignment-expression
19216 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19217 selector arguments and TREE_VALUE containing a list of comma
19221 cp_parser_objc_message_args (cp_parser* parser)
19223 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19224 bool maybe_unary_selector_p = true;
19225 cp_token *token = cp_lexer_peek_token (parser->lexer);
19227 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19229 tree selector = NULL_TREE, arg;
19231 if (token->type != CPP_COLON)
19232 selector = cp_parser_objc_selector (parser);
19234 /* Detect if we have a unary selector. */
19235 if (maybe_unary_selector_p
19236 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19237 return build_tree_list (selector, NULL_TREE);
19239 maybe_unary_selector_p = false;
19240 cp_parser_require (parser, CPP_COLON, "%<:%>");
19241 arg = cp_parser_assignment_expression (parser, false, NULL);
19244 = chainon (sel_args,
19245 build_tree_list (selector, arg));
19247 token = cp_lexer_peek_token (parser->lexer);
19250 /* Handle non-selector arguments, if any. */
19251 while (token->type == CPP_COMMA)
19255 cp_lexer_consume_token (parser->lexer);
19256 arg = cp_parser_assignment_expression (parser, false, NULL);
19259 = chainon (addl_args,
19260 build_tree_list (NULL_TREE, arg));
19262 token = cp_lexer_peek_token (parser->lexer);
19265 return build_tree_list (sel_args, addl_args);
19268 /* Parse an Objective-C encode expression.
19270 objc-encode-expression:
19271 @encode objc-typename
19273 Returns an encoded representation of the type argument. */
19276 cp_parser_objc_encode_expression (cp_parser* parser)
19281 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19282 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19283 token = cp_lexer_peek_token (parser->lexer);
19284 type = complete_type (cp_parser_type_id (parser));
19285 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19289 error ("%H%<@encode%> must specify a type as an argument",
19291 return error_mark_node;
19294 return objc_build_encode_expr (type);
19297 /* Parse an Objective-C @defs expression. */
19300 cp_parser_objc_defs_expression (cp_parser *parser)
19304 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19305 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19306 name = cp_parser_identifier (parser);
19307 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19309 return objc_get_class_ivars (name);
19312 /* Parse an Objective-C protocol expression.
19314 objc-protocol-expression:
19315 @protocol ( identifier )
19317 Returns a representation of the protocol expression. */
19320 cp_parser_objc_protocol_expression (cp_parser* parser)
19324 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19325 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19326 proto = cp_parser_identifier (parser);
19327 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19329 return objc_build_protocol_expr (proto);
19332 /* Parse an Objective-C selector expression.
19334 objc-selector-expression:
19335 @selector ( objc-method-signature )
19337 objc-method-signature:
19343 objc-selector-seq objc-selector :
19345 Returns a representation of the method selector. */
19348 cp_parser_objc_selector_expression (cp_parser* parser)
19350 tree sel_seq = NULL_TREE;
19351 bool maybe_unary_selector_p = true;
19354 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19355 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19356 token = cp_lexer_peek_token (parser->lexer);
19358 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19359 || token->type == CPP_SCOPE)
19361 tree selector = NULL_TREE;
19363 if (token->type != CPP_COLON
19364 || token->type == CPP_SCOPE)
19365 selector = cp_parser_objc_selector (parser);
19367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19368 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19370 /* Detect if we have a unary selector. */
19371 if (maybe_unary_selector_p)
19373 sel_seq = selector;
19374 goto finish_selector;
19378 cp_parser_error (parser, "expected %<:%>");
19381 maybe_unary_selector_p = false;
19382 token = cp_lexer_consume_token (parser->lexer);
19384 if (token->type == CPP_SCOPE)
19387 = chainon (sel_seq,
19388 build_tree_list (selector, NULL_TREE));
19390 = chainon (sel_seq,
19391 build_tree_list (NULL_TREE, NULL_TREE));
19395 = chainon (sel_seq,
19396 build_tree_list (selector, NULL_TREE));
19398 token = cp_lexer_peek_token (parser->lexer);
19402 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19404 return objc_build_selector_expr (sel_seq);
19407 /* Parse a list of identifiers.
19409 objc-identifier-list:
19411 objc-identifier-list , identifier
19413 Returns a TREE_LIST of identifier nodes. */
19416 cp_parser_objc_identifier_list (cp_parser* parser)
19418 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19419 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19421 while (sep->type == CPP_COMMA)
19423 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19424 list = chainon (list,
19425 build_tree_list (NULL_TREE,
19426 cp_parser_identifier (parser)));
19427 sep = cp_lexer_peek_token (parser->lexer);
19433 /* Parse an Objective-C alias declaration.
19435 objc-alias-declaration:
19436 @compatibility_alias identifier identifier ;
19438 This function registers the alias mapping with the Objective-C front end.
19439 It returns nothing. */
19442 cp_parser_objc_alias_declaration (cp_parser* parser)
19446 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19447 alias = cp_parser_identifier (parser);
19448 orig = cp_parser_identifier (parser);
19449 objc_declare_alias (alias, orig);
19450 cp_parser_consume_semicolon_at_end_of_statement (parser);
19453 /* Parse an Objective-C class forward-declaration.
19455 objc-class-declaration:
19456 @class objc-identifier-list ;
19458 The function registers the forward declarations with the Objective-C
19459 front end. It returns nothing. */
19462 cp_parser_objc_class_declaration (cp_parser* parser)
19464 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19465 objc_declare_class (cp_parser_objc_identifier_list (parser));
19466 cp_parser_consume_semicolon_at_end_of_statement (parser);
19469 /* Parse a list of Objective-C protocol references.
19471 objc-protocol-refs-opt:
19472 objc-protocol-refs [opt]
19474 objc-protocol-refs:
19475 < objc-identifier-list >
19477 Returns a TREE_LIST of identifiers, if any. */
19480 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19482 tree protorefs = NULL_TREE;
19484 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19486 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19487 protorefs = cp_parser_objc_identifier_list (parser);
19488 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19494 /* Parse a Objective-C visibility specification. */
19497 cp_parser_objc_visibility_spec (cp_parser* parser)
19499 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19501 switch (vis->keyword)
19503 case RID_AT_PRIVATE:
19504 objc_set_visibility (2);
19506 case RID_AT_PROTECTED:
19507 objc_set_visibility (0);
19509 case RID_AT_PUBLIC:
19510 objc_set_visibility (1);
19516 /* Eat '@private'/'@protected'/'@public'. */
19517 cp_lexer_consume_token (parser->lexer);
19520 /* Parse an Objective-C method type. */
19523 cp_parser_objc_method_type (cp_parser* parser)
19525 objc_set_method_type
19526 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19531 /* Parse an Objective-C protocol qualifier. */
19534 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19536 tree quals = NULL_TREE, node;
19537 cp_token *token = cp_lexer_peek_token (parser->lexer);
19539 node = token->u.value;
19541 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19542 && (node == ridpointers [(int) RID_IN]
19543 || node == ridpointers [(int) RID_OUT]
19544 || node == ridpointers [(int) RID_INOUT]
19545 || node == ridpointers [(int) RID_BYCOPY]
19546 || node == ridpointers [(int) RID_BYREF]
19547 || node == ridpointers [(int) RID_ONEWAY]))
19549 quals = tree_cons (NULL_TREE, node, quals);
19550 cp_lexer_consume_token (parser->lexer);
19551 token = cp_lexer_peek_token (parser->lexer);
19552 node = token->u.value;
19558 /* Parse an Objective-C typename. */
19561 cp_parser_objc_typename (cp_parser* parser)
19563 tree type_name = NULL_TREE;
19565 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19567 tree proto_quals, cp_type = NULL_TREE;
19569 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19570 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19572 /* An ObjC type name may consist of just protocol qualifiers, in which
19573 case the type shall default to 'id'. */
19574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19575 cp_type = cp_parser_type_id (parser);
19577 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19578 type_name = build_tree_list (proto_quals, cp_type);
19584 /* Check to see if TYPE refers to an Objective-C selector name. */
19587 cp_parser_objc_selector_p (enum cpp_ttype type)
19589 return (type == CPP_NAME || type == CPP_KEYWORD
19590 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19591 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19592 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19593 || type == CPP_XOR || type == CPP_XOR_EQ);
19596 /* Parse an Objective-C selector. */
19599 cp_parser_objc_selector (cp_parser* parser)
19601 cp_token *token = cp_lexer_consume_token (parser->lexer);
19603 if (!cp_parser_objc_selector_p (token->type))
19605 error ("%Hinvalid Objective-C++ selector name", &token->location);
19606 return error_mark_node;
19609 /* C++ operator names are allowed to appear in ObjC selectors. */
19610 switch (token->type)
19612 case CPP_AND_AND: return get_identifier ("and");
19613 case CPP_AND_EQ: return get_identifier ("and_eq");
19614 case CPP_AND: return get_identifier ("bitand");
19615 case CPP_OR: return get_identifier ("bitor");
19616 case CPP_COMPL: return get_identifier ("compl");
19617 case CPP_NOT: return get_identifier ("not");
19618 case CPP_NOT_EQ: return get_identifier ("not_eq");
19619 case CPP_OR_OR: return get_identifier ("or");
19620 case CPP_OR_EQ: return get_identifier ("or_eq");
19621 case CPP_XOR: return get_identifier ("xor");
19622 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19623 default: return token->u.value;
19627 /* Parse an Objective-C params list. */
19630 cp_parser_objc_method_keyword_params (cp_parser* parser)
19632 tree params = NULL_TREE;
19633 bool maybe_unary_selector_p = true;
19634 cp_token *token = cp_lexer_peek_token (parser->lexer);
19636 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19638 tree selector = NULL_TREE, type_name, identifier;
19640 if (token->type != CPP_COLON)
19641 selector = cp_parser_objc_selector (parser);
19643 /* Detect if we have a unary selector. */
19644 if (maybe_unary_selector_p
19645 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19648 maybe_unary_selector_p = false;
19649 cp_parser_require (parser, CPP_COLON, "%<:%>");
19650 type_name = cp_parser_objc_typename (parser);
19651 identifier = cp_parser_identifier (parser);
19655 objc_build_keyword_decl (selector,
19659 token = cp_lexer_peek_token (parser->lexer);
19665 /* Parse the non-keyword Objective-C params. */
19668 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19670 tree params = make_node (TREE_LIST);
19671 cp_token *token = cp_lexer_peek_token (parser->lexer);
19672 *ellipsisp = false; /* Initially, assume no ellipsis. */
19674 while (token->type == CPP_COMMA)
19676 cp_parameter_declarator *parmdecl;
19679 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19680 token = cp_lexer_peek_token (parser->lexer);
19682 if (token->type == CPP_ELLIPSIS)
19684 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19689 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19690 parm = grokdeclarator (parmdecl->declarator,
19691 &parmdecl->decl_specifiers,
19692 PARM, /*initialized=*/0,
19693 /*attrlist=*/NULL);
19695 chainon (params, build_tree_list (NULL_TREE, parm));
19696 token = cp_lexer_peek_token (parser->lexer);
19702 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19705 cp_parser_objc_interstitial_code (cp_parser* parser)
19707 cp_token *token = cp_lexer_peek_token (parser->lexer);
19709 /* If the next token is `extern' and the following token is a string
19710 literal, then we have a linkage specification. */
19711 if (token->keyword == RID_EXTERN
19712 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19713 cp_parser_linkage_specification (parser);
19714 /* Handle #pragma, if any. */
19715 else if (token->type == CPP_PRAGMA)
19716 cp_parser_pragma (parser, pragma_external);
19717 /* Allow stray semicolons. */
19718 else if (token->type == CPP_SEMICOLON)
19719 cp_lexer_consume_token (parser->lexer);
19720 /* Finally, try to parse a block-declaration, or a function-definition. */
19722 cp_parser_block_declaration (parser, /*statement_p=*/false);
19725 /* Parse a method signature. */
19728 cp_parser_objc_method_signature (cp_parser* parser)
19730 tree rettype, kwdparms, optparms;
19731 bool ellipsis = false;
19733 cp_parser_objc_method_type (parser);
19734 rettype = cp_parser_objc_typename (parser);
19735 kwdparms = cp_parser_objc_method_keyword_params (parser);
19736 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19738 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19741 /* Pars an Objective-C method prototype list. */
19744 cp_parser_objc_method_prototype_list (cp_parser* parser)
19746 cp_token *token = cp_lexer_peek_token (parser->lexer);
19748 while (token->keyword != RID_AT_END)
19750 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19752 objc_add_method_declaration
19753 (cp_parser_objc_method_signature (parser));
19754 cp_parser_consume_semicolon_at_end_of_statement (parser);
19757 /* Allow for interspersed non-ObjC++ code. */
19758 cp_parser_objc_interstitial_code (parser);
19760 token = cp_lexer_peek_token (parser->lexer);
19763 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19764 objc_finish_interface ();
19767 /* Parse an Objective-C method definition list. */
19770 cp_parser_objc_method_definition_list (cp_parser* parser)
19772 cp_token *token = cp_lexer_peek_token (parser->lexer);
19774 while (token->keyword != RID_AT_END)
19778 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19780 push_deferring_access_checks (dk_deferred);
19781 objc_start_method_definition
19782 (cp_parser_objc_method_signature (parser));
19784 /* For historical reasons, we accept an optional semicolon. */
19785 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19786 cp_lexer_consume_token (parser->lexer);
19788 perform_deferred_access_checks ();
19789 stop_deferring_access_checks ();
19790 meth = cp_parser_function_definition_after_declarator (parser,
19792 pop_deferring_access_checks ();
19793 objc_finish_method_definition (meth);
19796 /* Allow for interspersed non-ObjC++ code. */
19797 cp_parser_objc_interstitial_code (parser);
19799 token = cp_lexer_peek_token (parser->lexer);
19802 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19803 objc_finish_implementation ();
19806 /* Parse Objective-C ivars. */
19809 cp_parser_objc_class_ivars (cp_parser* parser)
19811 cp_token *token = cp_lexer_peek_token (parser->lexer);
19813 if (token->type != CPP_OPEN_BRACE)
19814 return; /* No ivars specified. */
19816 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19817 token = cp_lexer_peek_token (parser->lexer);
19819 while (token->type != CPP_CLOSE_BRACE)
19821 cp_decl_specifier_seq declspecs;
19822 int decl_class_or_enum_p;
19823 tree prefix_attributes;
19825 cp_parser_objc_visibility_spec (parser);
19827 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19830 cp_parser_decl_specifier_seq (parser,
19831 CP_PARSER_FLAGS_OPTIONAL,
19833 &decl_class_or_enum_p);
19834 prefix_attributes = declspecs.attributes;
19835 declspecs.attributes = NULL_TREE;
19837 /* Keep going until we hit the `;' at the end of the
19839 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19841 tree width = NULL_TREE, attributes, first_attribute, decl;
19842 cp_declarator *declarator = NULL;
19843 int ctor_dtor_or_conv_p;
19845 /* Check for a (possibly unnamed) bitfield declaration. */
19846 token = cp_lexer_peek_token (parser->lexer);
19847 if (token->type == CPP_COLON)
19850 if (token->type == CPP_NAME
19851 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19854 /* Get the name of the bitfield. */
19855 declarator = make_id_declarator (NULL_TREE,
19856 cp_parser_identifier (parser),
19860 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19861 /* Get the width of the bitfield. */
19863 = cp_parser_constant_expression (parser,
19864 /*allow_non_constant=*/false,
19869 /* Parse the declarator. */
19871 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19872 &ctor_dtor_or_conv_p,
19873 /*parenthesized_p=*/NULL,
19874 /*member_p=*/false);
19877 /* Look for attributes that apply to the ivar. */
19878 attributes = cp_parser_attributes_opt (parser);
19879 /* Remember which attributes are prefix attributes and
19881 first_attribute = attributes;
19882 /* Combine the attributes. */
19883 attributes = chainon (prefix_attributes, attributes);
19886 /* Create the bitfield declaration. */
19887 decl = grokbitfield (declarator, &declspecs,
19891 decl = grokfield (declarator, &declspecs,
19892 NULL_TREE, /*init_const_expr_p=*/false,
19893 NULL_TREE, attributes);
19895 /* Add the instance variable. */
19896 objc_add_instance_variable (decl);
19898 /* Reset PREFIX_ATTRIBUTES. */
19899 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19900 attributes = TREE_CHAIN (attributes);
19902 TREE_CHAIN (attributes) = NULL_TREE;
19904 token = cp_lexer_peek_token (parser->lexer);
19906 if (token->type == CPP_COMMA)
19908 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19914 cp_parser_consume_semicolon_at_end_of_statement (parser);
19915 token = cp_lexer_peek_token (parser->lexer);
19918 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19919 /* For historical reasons, we accept an optional semicolon. */
19920 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19921 cp_lexer_consume_token (parser->lexer);
19924 /* Parse an Objective-C protocol declaration. */
19927 cp_parser_objc_protocol_declaration (cp_parser* parser)
19929 tree proto, protorefs;
19932 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19935 tok = cp_lexer_peek_token (parser->lexer);
19936 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19940 /* See if we have a forward declaration or a definition. */
19941 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19943 /* Try a forward declaration first. */
19944 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19946 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19948 cp_parser_consume_semicolon_at_end_of_statement (parser);
19951 /* Ok, we got a full-fledged definition (or at least should). */
19954 proto = cp_parser_identifier (parser);
19955 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19956 objc_start_protocol (proto, protorefs);
19957 cp_parser_objc_method_prototype_list (parser);
19961 /* Parse an Objective-C superclass or category. */
19964 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19967 cp_token *next = cp_lexer_peek_token (parser->lexer);
19969 *super = *categ = NULL_TREE;
19970 if (next->type == CPP_COLON)
19972 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19973 *super = cp_parser_identifier (parser);
19975 else if (next->type == CPP_OPEN_PAREN)
19977 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19978 *categ = cp_parser_identifier (parser);
19979 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19983 /* Parse an Objective-C class interface. */
19986 cp_parser_objc_class_interface (cp_parser* parser)
19988 tree name, super, categ, protos;
19990 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19991 name = cp_parser_identifier (parser);
19992 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19993 protos = cp_parser_objc_protocol_refs_opt (parser);
19995 /* We have either a class or a category on our hands. */
19997 objc_start_category_interface (name, categ, protos);
20000 objc_start_class_interface (name, super, protos);
20001 /* Handle instance variable declarations, if any. */
20002 cp_parser_objc_class_ivars (parser);
20003 objc_continue_interface ();
20006 cp_parser_objc_method_prototype_list (parser);
20009 /* Parse an Objective-C class implementation. */
20012 cp_parser_objc_class_implementation (cp_parser* parser)
20014 tree name, super, categ;
20016 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20017 name = cp_parser_identifier (parser);
20018 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20020 /* We have either a class or a category on our hands. */
20022 objc_start_category_implementation (name, categ);
20025 objc_start_class_implementation (name, super);
20026 /* Handle instance variable declarations, if any. */
20027 cp_parser_objc_class_ivars (parser);
20028 objc_continue_implementation ();
20031 cp_parser_objc_method_definition_list (parser);
20034 /* Consume the @end token and finish off the implementation. */
20037 cp_parser_objc_end_implementation (cp_parser* parser)
20039 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20040 objc_finish_implementation ();
20043 /* Parse an Objective-C declaration. */
20046 cp_parser_objc_declaration (cp_parser* parser)
20048 /* Try to figure out what kind of declaration is present. */
20049 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20051 switch (kwd->keyword)
20054 cp_parser_objc_alias_declaration (parser);
20057 cp_parser_objc_class_declaration (parser);
20059 case RID_AT_PROTOCOL:
20060 cp_parser_objc_protocol_declaration (parser);
20062 case RID_AT_INTERFACE:
20063 cp_parser_objc_class_interface (parser);
20065 case RID_AT_IMPLEMENTATION:
20066 cp_parser_objc_class_implementation (parser);
20069 cp_parser_objc_end_implementation (parser);
20072 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20073 &kwd->location, kwd->u.value);
20074 cp_parser_skip_to_end_of_block_or_statement (parser);
20078 /* Parse an Objective-C try-catch-finally statement.
20080 objc-try-catch-finally-stmt:
20081 @try compound-statement objc-catch-clause-seq [opt]
20082 objc-finally-clause [opt]
20084 objc-catch-clause-seq:
20085 objc-catch-clause objc-catch-clause-seq [opt]
20088 @catch ( exception-declaration ) compound-statement
20090 objc-finally-clause
20091 @finally compound-statement
20093 Returns NULL_TREE. */
20096 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20097 location_t location;
20100 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20101 location = cp_lexer_peek_token (parser->lexer)->location;
20102 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20103 node, lest it get absorbed into the surrounding block. */
20104 stmt = push_stmt_list ();
20105 cp_parser_compound_statement (parser, NULL, false);
20106 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20108 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20110 cp_parameter_declarator *parmdecl;
20113 cp_lexer_consume_token (parser->lexer);
20114 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20115 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20116 parm = grokdeclarator (parmdecl->declarator,
20117 &parmdecl->decl_specifiers,
20118 PARM, /*initialized=*/0,
20119 /*attrlist=*/NULL);
20120 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20121 objc_begin_catch_clause (parm);
20122 cp_parser_compound_statement (parser, NULL, false);
20123 objc_finish_catch_clause ();
20126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20128 cp_lexer_consume_token (parser->lexer);
20129 location = cp_lexer_peek_token (parser->lexer)->location;
20130 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20131 node, lest it get absorbed into the surrounding block. */
20132 stmt = push_stmt_list ();
20133 cp_parser_compound_statement (parser, NULL, false);
20134 objc_build_finally_clause (location, pop_stmt_list (stmt));
20137 return objc_finish_try_stmt ();
20140 /* Parse an Objective-C synchronized statement.
20142 objc-synchronized-stmt:
20143 @synchronized ( expression ) compound-statement
20145 Returns NULL_TREE. */
20148 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20149 location_t location;
20152 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20154 location = cp_lexer_peek_token (parser->lexer)->location;
20155 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20156 lock = cp_parser_expression (parser, false, NULL);
20157 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20159 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20160 node, lest it get absorbed into the surrounding block. */
20161 stmt = push_stmt_list ();
20162 cp_parser_compound_statement (parser, NULL, false);
20164 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20167 /* Parse an Objective-C throw statement.
20170 @throw assignment-expression [opt] ;
20172 Returns a constructed '@throw' statement. */
20175 cp_parser_objc_throw_statement (cp_parser *parser) {
20176 tree expr = NULL_TREE;
20178 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20181 expr = cp_parser_assignment_expression (parser, false, NULL);
20183 cp_parser_consume_semicolon_at_end_of_statement (parser);
20185 return objc_build_throw_stmt (expr);
20188 /* Parse an Objective-C statement. */
20191 cp_parser_objc_statement (cp_parser * parser) {
20192 /* Try to figure out what kind of declaration is present. */
20193 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20195 switch (kwd->keyword)
20198 return cp_parser_objc_try_catch_finally_statement (parser);
20199 case RID_AT_SYNCHRONIZED:
20200 return cp_parser_objc_synchronized_statement (parser);
20202 return cp_parser_objc_throw_statement (parser);
20204 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20205 &kwd->location, kwd->u.value);
20206 cp_parser_skip_to_end_of_block_or_statement (parser);
20209 return error_mark_node;
20212 /* OpenMP 2.5 parsing routines. */
20214 /* Returns name of the next clause.
20215 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20216 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20217 returned and the token is consumed. */
20219 static pragma_omp_clause
20220 cp_parser_omp_clause_name (cp_parser *parser)
20222 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20225 result = PRAGMA_OMP_CLAUSE_IF;
20226 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20227 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20228 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20229 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20230 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20232 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20233 const char *p = IDENTIFIER_POINTER (id);
20238 if (!strcmp ("collapse", p))
20239 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20240 else if (!strcmp ("copyin", p))
20241 result = PRAGMA_OMP_CLAUSE_COPYIN;
20242 else if (!strcmp ("copyprivate", p))
20243 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20246 if (!strcmp ("firstprivate", p))
20247 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20250 if (!strcmp ("lastprivate", p))
20251 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20254 if (!strcmp ("nowait", p))
20255 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20256 else if (!strcmp ("num_threads", p))
20257 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20260 if (!strcmp ("ordered", p))
20261 result = PRAGMA_OMP_CLAUSE_ORDERED;
20264 if (!strcmp ("reduction", p))
20265 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20268 if (!strcmp ("schedule", p))
20269 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20270 else if (!strcmp ("shared", p))
20271 result = PRAGMA_OMP_CLAUSE_SHARED;
20274 if (!strcmp ("untied", p))
20275 result = PRAGMA_OMP_CLAUSE_UNTIED;
20280 if (result != PRAGMA_OMP_CLAUSE_NONE)
20281 cp_lexer_consume_token (parser->lexer);
20286 /* Validate that a clause of the given type does not already exist. */
20289 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20290 const char *name, location_t location)
20294 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20295 if (OMP_CLAUSE_CODE (c) == code)
20297 error ("%Htoo many %qs clauses", &location, name);
20305 variable-list , identifier
20307 In addition, we match a closing parenthesis. An opening parenthesis
20308 will have been consumed by the caller.
20310 If KIND is nonzero, create the appropriate node and install the decl
20311 in OMP_CLAUSE_DECL and add the node to the head of the list.
20313 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20314 return the list created. */
20317 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20325 token = cp_lexer_peek_token (parser->lexer);
20326 name = cp_parser_id_expression (parser, /*template_p=*/false,
20327 /*check_dependency_p=*/true,
20328 /*template_p=*/NULL,
20329 /*declarator_p=*/false,
20330 /*optional_p=*/false);
20331 if (name == error_mark_node)
20334 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20335 if (decl == error_mark_node)
20336 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20337 else if (kind != 0)
20339 tree u = build_omp_clause (kind);
20340 OMP_CLAUSE_DECL (u) = decl;
20341 OMP_CLAUSE_CHAIN (u) = list;
20345 list = tree_cons (decl, NULL_TREE, list);
20348 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20350 cp_lexer_consume_token (parser->lexer);
20353 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20357 /* Try to resync to an unnested comma. Copied from
20358 cp_parser_parenthesized_expression_list. */
20360 ending = cp_parser_skip_to_closing_parenthesis (parser,
20361 /*recovering=*/true,
20363 /*consume_paren=*/true);
20371 /* Similarly, but expect leading and trailing parenthesis. This is a very
20372 common case for omp clauses. */
20375 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20377 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20378 return cp_parser_omp_var_list_no_open (parser, kind, list);
20383 collapse ( constant-expression ) */
20386 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20392 loc = cp_lexer_peek_token (parser->lexer)->location;
20393 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20396 num = cp_parser_constant_expression (parser, false, NULL);
20398 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20399 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20400 /*or_comma=*/false,
20401 /*consume_paren=*/true);
20403 if (num == error_mark_node)
20405 num = fold_non_dependent_expr (num);
20406 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20407 || !host_integerp (num, 0)
20408 || (n = tree_low_cst (num, 0)) <= 0
20411 error ("%Hcollapse argument needs positive constant integer expression",
20416 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20417 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20418 OMP_CLAUSE_CHAIN (c) = list;
20419 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20425 default ( shared | none ) */
20428 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20430 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20433 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20435 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20437 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20438 const char *p = IDENTIFIER_POINTER (id);
20443 if (strcmp ("none", p) != 0)
20445 kind = OMP_CLAUSE_DEFAULT_NONE;
20449 if (strcmp ("shared", p) != 0)
20451 kind = OMP_CLAUSE_DEFAULT_SHARED;
20458 cp_lexer_consume_token (parser->lexer);
20463 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20466 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20467 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20468 /*or_comma=*/false,
20469 /*consume_paren=*/true);
20471 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20474 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20475 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20476 OMP_CLAUSE_CHAIN (c) = list;
20477 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20483 if ( expression ) */
20486 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20490 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20493 t = cp_parser_condition (parser);
20495 if (t == error_mark_node
20496 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20497 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20498 /*or_comma=*/false,
20499 /*consume_paren=*/true);
20501 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20503 c = build_omp_clause (OMP_CLAUSE_IF);
20504 OMP_CLAUSE_IF_EXPR (c) = t;
20505 OMP_CLAUSE_CHAIN (c) = list;
20514 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20515 tree list, location_t location)
20519 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20521 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20522 OMP_CLAUSE_CHAIN (c) = list;
20527 num_threads ( expression ) */
20530 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20531 location_t location)
20535 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20538 t = cp_parser_expression (parser, false, NULL);
20540 if (t == error_mark_node
20541 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20542 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20543 /*or_comma=*/false,
20544 /*consume_paren=*/true);
20546 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20547 "num_threads", location);
20549 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20550 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20551 OMP_CLAUSE_CHAIN (c) = list;
20560 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20561 tree list, location_t location)
20565 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20566 "ordered", location);
20568 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20569 OMP_CLAUSE_CHAIN (c) = list;
20574 reduction ( reduction-operator : variable-list )
20576 reduction-operator:
20577 One of: + * - & ^ | && || */
20580 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20582 enum tree_code code;
20585 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20588 switch (cp_lexer_peek_token (parser->lexer)->type)
20600 code = BIT_AND_EXPR;
20603 code = BIT_XOR_EXPR;
20606 code = BIT_IOR_EXPR;
20609 code = TRUTH_ANDIF_EXPR;
20612 code = TRUTH_ORIF_EXPR;
20615 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20616 "%<|%>, %<&&%>, or %<||%>");
20618 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20619 /*or_comma=*/false,
20620 /*consume_paren=*/true);
20623 cp_lexer_consume_token (parser->lexer);
20625 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20628 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20629 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20630 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20636 schedule ( schedule-kind )
20637 schedule ( schedule-kind , expression )
20640 static | dynamic | guided | runtime | auto */
20643 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20647 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20650 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20652 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20654 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20655 const char *p = IDENTIFIER_POINTER (id);
20660 if (strcmp ("dynamic", p) != 0)
20662 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20666 if (strcmp ("guided", p) != 0)
20668 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20672 if (strcmp ("runtime", p) != 0)
20674 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20681 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20682 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20683 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20684 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20687 cp_lexer_consume_token (parser->lexer);
20689 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20692 cp_lexer_consume_token (parser->lexer);
20694 token = cp_lexer_peek_token (parser->lexer);
20695 t = cp_parser_assignment_expression (parser, false, NULL);
20697 if (t == error_mark_node)
20699 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20700 error ("%Hschedule %<runtime%> does not take "
20701 "a %<chunk_size%> parameter", &token->location);
20702 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20703 error ("%Hschedule %<auto%> does not take "
20704 "a %<chunk_size%> parameter", &token->location);
20706 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20708 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20711 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20714 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20715 OMP_CLAUSE_CHAIN (c) = list;
20719 cp_parser_error (parser, "invalid schedule kind");
20721 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20722 /*or_comma=*/false,
20723 /*consume_paren=*/true);
20731 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20732 tree list, location_t location)
20736 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20738 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20739 OMP_CLAUSE_CHAIN (c) = list;
20743 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20744 is a bitmask in MASK. Return the list of clauses found; the result
20745 of clause default goes in *pdefault. */
20748 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20749 const char *where, cp_token *pragma_tok)
20751 tree clauses = NULL;
20753 cp_token *token = NULL;
20755 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20757 pragma_omp_clause c_kind;
20758 const char *c_name;
20759 tree prev = clauses;
20761 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20762 cp_lexer_consume_token (parser->lexer);
20764 token = cp_lexer_peek_token (parser->lexer);
20765 c_kind = cp_parser_omp_clause_name (parser);
20770 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20771 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20773 c_name = "collapse";
20775 case PRAGMA_OMP_CLAUSE_COPYIN:
20776 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20779 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20780 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20782 c_name = "copyprivate";
20784 case PRAGMA_OMP_CLAUSE_DEFAULT:
20785 clauses = cp_parser_omp_clause_default (parser, clauses,
20787 c_name = "default";
20789 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20790 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20792 c_name = "firstprivate";
20794 case PRAGMA_OMP_CLAUSE_IF:
20795 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20798 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20799 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20801 c_name = "lastprivate";
20803 case PRAGMA_OMP_CLAUSE_NOWAIT:
20804 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20807 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20808 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20810 c_name = "num_threads";
20812 case PRAGMA_OMP_CLAUSE_ORDERED:
20813 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20815 c_name = "ordered";
20817 case PRAGMA_OMP_CLAUSE_PRIVATE:
20818 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20820 c_name = "private";
20822 case PRAGMA_OMP_CLAUSE_REDUCTION:
20823 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20824 c_name = "reduction";
20826 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20827 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20829 c_name = "schedule";
20831 case PRAGMA_OMP_CLAUSE_SHARED:
20832 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20836 case PRAGMA_OMP_CLAUSE_UNTIED:
20837 clauses = cp_parser_omp_clause_untied (parser, clauses,
20842 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20846 if (((mask >> c_kind) & 1) == 0)
20848 /* Remove the invalid clause(s) from the list to avoid
20849 confusing the rest of the compiler. */
20851 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20855 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20856 return finish_omp_clauses (clauses);
20863 In practice, we're also interested in adding the statement to an
20864 outer node. So it is convenient if we work around the fact that
20865 cp_parser_statement calls add_stmt. */
20868 cp_parser_begin_omp_structured_block (cp_parser *parser)
20870 unsigned save = parser->in_statement;
20872 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20873 This preserves the "not within loop or switch" style error messages
20874 for nonsense cases like
20880 if (parser->in_statement)
20881 parser->in_statement = IN_OMP_BLOCK;
20887 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20889 parser->in_statement = save;
20893 cp_parser_omp_structured_block (cp_parser *parser)
20895 tree stmt = begin_omp_structured_block ();
20896 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20898 cp_parser_statement (parser, NULL_TREE, false, NULL);
20900 cp_parser_end_omp_structured_block (parser, save);
20901 return finish_omp_structured_block (stmt);
20905 # pragma omp atomic new-line
20909 x binop= expr | x++ | ++x | x-- | --x
20911 +, *, -, /, &, ^, |, <<, >>
20913 where x is an lvalue expression with scalar type. */
20916 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20919 enum tree_code code;
20921 cp_parser_require_pragma_eol (parser, pragma_tok);
20923 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20924 /*cast_p=*/false, NULL);
20925 switch (TREE_CODE (lhs))
20930 case PREINCREMENT_EXPR:
20931 case POSTINCREMENT_EXPR:
20932 lhs = TREE_OPERAND (lhs, 0);
20934 rhs = integer_one_node;
20937 case PREDECREMENT_EXPR:
20938 case POSTDECREMENT_EXPR:
20939 lhs = TREE_OPERAND (lhs, 0);
20941 rhs = integer_one_node;
20945 switch (cp_lexer_peek_token (parser->lexer)->type)
20951 code = TRUNC_DIV_EXPR;
20959 case CPP_LSHIFT_EQ:
20960 code = LSHIFT_EXPR;
20962 case CPP_RSHIFT_EQ:
20963 code = RSHIFT_EXPR;
20966 code = BIT_AND_EXPR;
20969 code = BIT_IOR_EXPR;
20972 code = BIT_XOR_EXPR;
20975 cp_parser_error (parser,
20976 "invalid operator for %<#pragma omp atomic%>");
20979 cp_lexer_consume_token (parser->lexer);
20981 rhs = cp_parser_expression (parser, false, NULL);
20982 if (rhs == error_mark_node)
20986 finish_omp_atomic (code, lhs, rhs);
20987 cp_parser_consume_semicolon_at_end_of_statement (parser);
20991 cp_parser_skip_to_end_of_block_or_statement (parser);
20996 # pragma omp barrier new-line */
20999 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21001 cp_parser_require_pragma_eol (parser, pragma_tok);
21002 finish_omp_barrier ();
21006 # pragma omp critical [(name)] new-line
21007 structured-block */
21010 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21012 tree stmt, name = NULL;
21014 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21016 cp_lexer_consume_token (parser->lexer);
21018 name = cp_parser_identifier (parser);
21020 if (name == error_mark_node
21021 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21022 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21023 /*or_comma=*/false,
21024 /*consume_paren=*/true);
21025 if (name == error_mark_node)
21028 cp_parser_require_pragma_eol (parser, pragma_tok);
21030 stmt = cp_parser_omp_structured_block (parser);
21031 return c_finish_omp_critical (stmt, name);
21035 # pragma omp flush flush-vars[opt] new-line
21038 ( variable-list ) */
21041 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21043 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21044 (void) cp_parser_omp_var_list (parser, 0, NULL);
21045 cp_parser_require_pragma_eol (parser, pragma_tok);
21047 finish_omp_flush ();
21050 /* Helper function, to parse omp for increment expression. */
21053 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21055 tree cond = cp_parser_binary_expression (parser, false, true,
21056 PREC_NOT_OPERATOR, NULL);
21059 if (cond == error_mark_node
21060 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21062 cp_parser_skip_to_end_of_statement (parser);
21063 return error_mark_node;
21066 switch (TREE_CODE (cond))
21074 return error_mark_node;
21077 /* If decl is an iterator, preserve LHS and RHS of the relational
21078 expr until finish_omp_for. */
21080 && (type_dependent_expression_p (decl)
21081 || CLASS_TYPE_P (TREE_TYPE (decl))))
21084 return build_x_binary_op (TREE_CODE (cond),
21085 TREE_OPERAND (cond, 0), ERROR_MARK,
21086 TREE_OPERAND (cond, 1), ERROR_MARK,
21087 &overloaded_p, tf_warning_or_error);
21090 /* Helper function, to parse omp for increment expression. */
21093 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21095 cp_token *token = cp_lexer_peek_token (parser->lexer);
21101 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21103 op = (token->type == CPP_PLUS_PLUS
21104 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21105 cp_lexer_consume_token (parser->lexer);
21106 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21108 return error_mark_node;
21109 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21112 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21114 return error_mark_node;
21116 token = cp_lexer_peek_token (parser->lexer);
21117 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21119 op = (token->type == CPP_PLUS_PLUS
21120 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21121 cp_lexer_consume_token (parser->lexer);
21122 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21125 op = cp_parser_assignment_operator_opt (parser);
21126 if (op == ERROR_MARK)
21127 return error_mark_node;
21129 if (op != NOP_EXPR)
21131 rhs = cp_parser_assignment_expression (parser, false, NULL);
21132 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21133 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21136 lhs = cp_parser_binary_expression (parser, false, false,
21137 PREC_ADDITIVE_EXPRESSION, NULL);
21138 token = cp_lexer_peek_token (parser->lexer);
21139 decl_first = lhs == decl;
21142 if (token->type != CPP_PLUS
21143 && token->type != CPP_MINUS)
21144 return error_mark_node;
21148 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21149 cp_lexer_consume_token (parser->lexer);
21150 rhs = cp_parser_binary_expression (parser, false, false,
21151 PREC_ADDITIVE_EXPRESSION, NULL);
21152 token = cp_lexer_peek_token (parser->lexer);
21153 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21155 if (lhs == NULL_TREE)
21157 if (op == PLUS_EXPR)
21160 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21163 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21164 NULL, tf_warning_or_error);
21167 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21171 if (rhs != decl || op == MINUS_EXPR)
21172 return error_mark_node;
21173 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21176 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21178 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21181 /* Parse the restricted form of the for statement allowed by OpenMP. */
21184 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21186 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21187 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21188 tree this_pre_body, cl;
21189 location_t loc_first;
21190 bool collapse_err = false;
21191 int i, collapse = 1, nbraces = 0;
21193 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21194 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21195 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21197 gcc_assert (collapse >= 1);
21199 declv = make_tree_vec (collapse);
21200 initv = make_tree_vec (collapse);
21201 condv = make_tree_vec (collapse);
21202 incrv = make_tree_vec (collapse);
21204 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21206 for (i = 0; i < collapse; i++)
21208 int bracecount = 0;
21209 bool add_private_clause = false;
21212 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21214 cp_parser_error (parser, "for statement expected");
21217 loc = cp_lexer_consume_token (parser->lexer)->location;
21219 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21222 init = decl = real_decl = NULL;
21223 this_pre_body = push_stmt_list ();
21224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21226 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21230 integer-type var = lb
21231 random-access-iterator-type var = lb
21232 pointer-type var = lb
21234 cp_decl_specifier_seq type_specifiers;
21236 /* First, try to parse as an initialized declaration. See
21237 cp_parser_condition, from whence the bulk of this is copied. */
21239 cp_parser_parse_tentatively (parser);
21240 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21242 if (cp_parser_parse_definitely (parser))
21244 /* If parsing a type specifier seq succeeded, then this
21245 MUST be a initialized declaration. */
21246 tree asm_specification, attributes;
21247 cp_declarator *declarator;
21249 declarator = cp_parser_declarator (parser,
21250 CP_PARSER_DECLARATOR_NAMED,
21251 /*ctor_dtor_or_conv_p=*/NULL,
21252 /*parenthesized_p=*/NULL,
21253 /*member_p=*/false);
21254 attributes = cp_parser_attributes_opt (parser);
21255 asm_specification = cp_parser_asm_specification_opt (parser);
21257 if (declarator == cp_error_declarator)
21258 cp_parser_skip_to_end_of_statement (parser);
21262 tree pushed_scope, auto_node;
21264 decl = start_decl (declarator, &type_specifiers,
21265 SD_INITIALIZED, attributes,
21266 /*prefix_attributes=*/NULL_TREE,
21269 auto_node = type_uses_auto (TREE_TYPE (decl));
21270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21272 if (cp_lexer_next_token_is (parser->lexer,
21274 error ("parenthesized initialization is not allowed in "
21275 "OpenMP %<for%> loop");
21277 /* Trigger an error. */
21278 cp_parser_require (parser, CPP_EQ, "%<=%>");
21280 init = error_mark_node;
21281 cp_parser_skip_to_end_of_statement (parser);
21283 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21284 || type_dependent_expression_p (decl)
21287 bool is_direct_init, is_non_constant_init;
21289 init = cp_parser_initializer (parser,
21291 &is_non_constant_init);
21293 if (auto_node && describable_type (init))
21296 = do_auto_deduction (TREE_TYPE (decl), init,
21299 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21300 && !type_dependent_expression_p (decl))
21304 cp_finish_decl (decl, init, !is_non_constant_init,
21306 LOOKUP_ONLYCONVERTING);
21307 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21310 = tree_cons (NULL, this_pre_body, for_block);
21314 init = pop_stmt_list (this_pre_body);
21315 this_pre_body = NULL_TREE;
21320 cp_lexer_consume_token (parser->lexer);
21321 init = cp_parser_assignment_expression (parser, false, NULL);
21324 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21325 init = error_mark_node;
21327 cp_finish_decl (decl, NULL_TREE,
21328 /*init_const_expr_p=*/false,
21330 LOOKUP_ONLYCONVERTING);
21334 pop_scope (pushed_scope);
21340 /* If parsing a type specifier sequence failed, then
21341 this MUST be a simple expression. */
21342 cp_parser_parse_tentatively (parser);
21343 decl = cp_parser_primary_expression (parser, false, false,
21345 if (!cp_parser_error_occurred (parser)
21348 && CLASS_TYPE_P (TREE_TYPE (decl)))
21352 cp_parser_parse_definitely (parser);
21353 cp_parser_require (parser, CPP_EQ, "%<=%>");
21354 rhs = cp_parser_assignment_expression (parser, false, NULL);
21355 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21357 tf_warning_or_error));
21358 add_private_clause = true;
21363 cp_parser_abort_tentative_parse (parser);
21364 init = cp_parser_expression (parser, false, NULL);
21367 if (TREE_CODE (init) == MODIFY_EXPR
21368 || TREE_CODE (init) == MODOP_EXPR)
21369 real_decl = TREE_OPERAND (init, 0);
21374 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21377 this_pre_body = pop_stmt_list (this_pre_body);
21381 pre_body = push_stmt_list ();
21383 add_stmt (this_pre_body);
21384 pre_body = pop_stmt_list (pre_body);
21387 pre_body = this_pre_body;
21392 if (par_clauses != NULL && real_decl != NULL_TREE)
21395 for (c = par_clauses; *c ; )
21396 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21397 && OMP_CLAUSE_DECL (*c) == real_decl)
21399 error ("%Hiteration variable %qD should not be firstprivate",
21401 *c = OMP_CLAUSE_CHAIN (*c);
21403 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21404 && OMP_CLAUSE_DECL (*c) == real_decl)
21406 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21407 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21408 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21409 OMP_CLAUSE_DECL (l) = real_decl;
21410 OMP_CLAUSE_CHAIN (l) = clauses;
21411 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21413 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21414 CP_OMP_CLAUSE_INFO (*c) = NULL;
21415 add_private_clause = false;
21419 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21420 && OMP_CLAUSE_DECL (*c) == real_decl)
21421 add_private_clause = false;
21422 c = &OMP_CLAUSE_CHAIN (*c);
21426 if (add_private_clause)
21429 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21431 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21432 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21433 && OMP_CLAUSE_DECL (c) == decl)
21435 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21436 && OMP_CLAUSE_DECL (c) == decl)
21437 error ("%Hiteration variable %qD should not be firstprivate",
21439 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21440 && OMP_CLAUSE_DECL (c) == decl)
21441 error ("%Hiteration variable %qD should not be reduction",
21446 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21447 OMP_CLAUSE_DECL (c) = decl;
21448 c = finish_omp_clauses (c);
21451 OMP_CLAUSE_CHAIN (c) = clauses;
21458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21459 cond = cp_parser_omp_for_cond (parser, decl);
21460 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21465 /* If decl is an iterator, preserve the operator on decl
21466 until finish_omp_for. */
21468 && (type_dependent_expression_p (decl)
21469 || CLASS_TYPE_P (TREE_TYPE (decl))))
21470 incr = cp_parser_omp_for_incr (parser, decl);
21472 incr = cp_parser_expression (parser, false, NULL);
21475 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21476 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21477 /*or_comma=*/false,
21478 /*consume_paren=*/true);
21480 TREE_VEC_ELT (declv, i) = decl;
21481 TREE_VEC_ELT (initv, i) = init;
21482 TREE_VEC_ELT (condv, i) = cond;
21483 TREE_VEC_ELT (incrv, i) = incr;
21485 if (i == collapse - 1)
21488 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21489 in between the collapsed for loops to be still considered perfectly
21490 nested. Hopefully the final version clarifies this.
21491 For now handle (multiple) {'s and empty statements. */
21492 cp_parser_parse_tentatively (parser);
21495 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21497 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21499 cp_lexer_consume_token (parser->lexer);
21502 else if (bracecount
21503 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21504 cp_lexer_consume_token (parser->lexer);
21507 loc = cp_lexer_peek_token (parser->lexer)->location;
21508 error ("%Hnot enough collapsed for loops", &loc);
21509 collapse_err = true;
21510 cp_parser_abort_tentative_parse (parser);
21519 cp_parser_parse_definitely (parser);
21520 nbraces += bracecount;
21524 /* Note that we saved the original contents of this flag when we entered
21525 the structured block, and so we don't need to re-save it here. */
21526 parser->in_statement = IN_OMP_FOR;
21528 /* Note that the grammar doesn't call for a structured block here,
21529 though the loop as a whole is a structured block. */
21530 body = push_stmt_list ();
21531 cp_parser_statement (parser, NULL_TREE, false, NULL);
21532 body = pop_stmt_list (body);
21534 if (declv == NULL_TREE)
21537 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21538 pre_body, clauses);
21542 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21544 cp_lexer_consume_token (parser->lexer);
21547 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21548 cp_lexer_consume_token (parser->lexer);
21553 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21554 error ("%Hcollapsed loops not perfectly nested", &loc);
21556 collapse_err = true;
21557 cp_parser_statement_seq_opt (parser, NULL);
21558 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21564 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21565 for_block = TREE_CHAIN (for_block);
21572 #pragma omp for for-clause[optseq] new-line
21575 #define OMP_FOR_CLAUSE_MASK \
21576 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21577 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21578 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21579 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21580 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21581 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21582 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21583 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21586 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21588 tree clauses, sb, ret;
21591 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21592 "#pragma omp for", pragma_tok);
21594 sb = begin_omp_structured_block ();
21595 save = cp_parser_begin_omp_structured_block (parser);
21597 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21599 cp_parser_end_omp_structured_block (parser, save);
21600 add_stmt (finish_omp_structured_block (sb));
21606 # pragma omp master new-line
21607 structured-block */
21610 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21612 cp_parser_require_pragma_eol (parser, pragma_tok);
21613 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21617 # pragma omp ordered new-line
21618 structured-block */
21621 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21623 cp_parser_require_pragma_eol (parser, pragma_tok);
21624 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21630 { section-sequence }
21633 section-directive[opt] structured-block
21634 section-sequence section-directive structured-block */
21637 cp_parser_omp_sections_scope (cp_parser *parser)
21639 tree stmt, substmt;
21640 bool error_suppress = false;
21643 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21646 stmt = push_stmt_list ();
21648 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21652 substmt = begin_omp_structured_block ();
21653 save = cp_parser_begin_omp_structured_block (parser);
21657 cp_parser_statement (parser, NULL_TREE, false, NULL);
21659 tok = cp_lexer_peek_token (parser->lexer);
21660 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21662 if (tok->type == CPP_CLOSE_BRACE)
21664 if (tok->type == CPP_EOF)
21668 cp_parser_end_omp_structured_block (parser, save);
21669 substmt = finish_omp_structured_block (substmt);
21670 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21671 add_stmt (substmt);
21676 tok = cp_lexer_peek_token (parser->lexer);
21677 if (tok->type == CPP_CLOSE_BRACE)
21679 if (tok->type == CPP_EOF)
21682 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21684 cp_lexer_consume_token (parser->lexer);
21685 cp_parser_require_pragma_eol (parser, tok);
21686 error_suppress = false;
21688 else if (!error_suppress)
21690 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21691 error_suppress = true;
21694 substmt = cp_parser_omp_structured_block (parser);
21695 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21696 add_stmt (substmt);
21698 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21700 substmt = pop_stmt_list (stmt);
21702 stmt = make_node (OMP_SECTIONS);
21703 TREE_TYPE (stmt) = void_type_node;
21704 OMP_SECTIONS_BODY (stmt) = substmt;
21711 # pragma omp sections sections-clause[optseq] newline
21714 #define OMP_SECTIONS_CLAUSE_MASK \
21715 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21716 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21717 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21718 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21719 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21722 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21726 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21727 "#pragma omp sections", pragma_tok);
21729 ret = cp_parser_omp_sections_scope (parser);
21731 OMP_SECTIONS_CLAUSES (ret) = clauses;
21737 # pragma parallel parallel-clause new-line
21738 # pragma parallel for parallel-for-clause new-line
21739 # pragma parallel sections parallel-sections-clause new-line */
21741 #define OMP_PARALLEL_CLAUSE_MASK \
21742 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21743 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21744 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21745 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21746 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21747 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21748 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21749 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21752 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21754 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21755 const char *p_name = "#pragma omp parallel";
21756 tree stmt, clauses, par_clause, ws_clause, block;
21757 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21762 cp_lexer_consume_token (parser->lexer);
21763 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21764 p_name = "#pragma omp parallel for";
21765 mask |= OMP_FOR_CLAUSE_MASK;
21766 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21768 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21770 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21771 const char *p = IDENTIFIER_POINTER (id);
21772 if (strcmp (p, "sections") == 0)
21774 cp_lexer_consume_token (parser->lexer);
21775 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21776 p_name = "#pragma omp parallel sections";
21777 mask |= OMP_SECTIONS_CLAUSE_MASK;
21778 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21782 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21783 block = begin_omp_parallel ();
21784 save = cp_parser_begin_omp_structured_block (parser);
21788 case PRAGMA_OMP_PARALLEL:
21789 cp_parser_statement (parser, NULL_TREE, false, NULL);
21790 par_clause = clauses;
21793 case PRAGMA_OMP_PARALLEL_FOR:
21794 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21795 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21798 case PRAGMA_OMP_PARALLEL_SECTIONS:
21799 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21800 stmt = cp_parser_omp_sections_scope (parser);
21802 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21806 gcc_unreachable ();
21809 cp_parser_end_omp_structured_block (parser, save);
21810 stmt = finish_omp_parallel (par_clause, block);
21811 if (p_kind != PRAGMA_OMP_PARALLEL)
21812 OMP_PARALLEL_COMBINED (stmt) = 1;
21817 # pragma omp single single-clause[optseq] new-line
21818 structured-block */
21820 #define OMP_SINGLE_CLAUSE_MASK \
21821 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21822 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21823 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21824 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21827 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21829 tree stmt = make_node (OMP_SINGLE);
21830 TREE_TYPE (stmt) = void_type_node;
21832 OMP_SINGLE_CLAUSES (stmt)
21833 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21834 "#pragma omp single", pragma_tok);
21835 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21837 return add_stmt (stmt);
21841 # pragma omp task task-clause[optseq] new-line
21842 structured-block */
21844 #define OMP_TASK_CLAUSE_MASK \
21845 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21846 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21847 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21848 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21849 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21850 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21853 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21855 tree clauses, block;
21858 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21859 "#pragma omp task", pragma_tok);
21860 block = begin_omp_task ();
21861 save = cp_parser_begin_omp_structured_block (parser);
21862 cp_parser_statement (parser, NULL_TREE, false, NULL);
21863 cp_parser_end_omp_structured_block (parser, save);
21864 return finish_omp_task (clauses, block);
21868 # pragma omp taskwait new-line */
21871 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21873 cp_parser_require_pragma_eol (parser, pragma_tok);
21874 finish_omp_taskwait ();
21878 # pragma omp threadprivate (variable-list) */
21881 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21885 vars = cp_parser_omp_var_list (parser, 0, NULL);
21886 cp_parser_require_pragma_eol (parser, pragma_tok);
21888 finish_omp_threadprivate (vars);
21891 /* Main entry point to OpenMP statement pragmas. */
21894 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21898 switch (pragma_tok->pragma_kind)
21900 case PRAGMA_OMP_ATOMIC:
21901 cp_parser_omp_atomic (parser, pragma_tok);
21903 case PRAGMA_OMP_CRITICAL:
21904 stmt = cp_parser_omp_critical (parser, pragma_tok);
21906 case PRAGMA_OMP_FOR:
21907 stmt = cp_parser_omp_for (parser, pragma_tok);
21909 case PRAGMA_OMP_MASTER:
21910 stmt = cp_parser_omp_master (parser, pragma_tok);
21912 case PRAGMA_OMP_ORDERED:
21913 stmt = cp_parser_omp_ordered (parser, pragma_tok);
21915 case PRAGMA_OMP_PARALLEL:
21916 stmt = cp_parser_omp_parallel (parser, pragma_tok);
21918 case PRAGMA_OMP_SECTIONS:
21919 stmt = cp_parser_omp_sections (parser, pragma_tok);
21921 case PRAGMA_OMP_SINGLE:
21922 stmt = cp_parser_omp_single (parser, pragma_tok);
21924 case PRAGMA_OMP_TASK:
21925 stmt = cp_parser_omp_task (parser, pragma_tok);
21928 gcc_unreachable ();
21932 SET_EXPR_LOCATION (stmt, pragma_tok->location);
21937 static GTY (()) cp_parser *the_parser;
21940 /* Special handling for the first token or line in the file. The first
21941 thing in the file might be #pragma GCC pch_preprocess, which loads a
21942 PCH file, which is a GC collection point. So we need to handle this
21943 first pragma without benefit of an existing lexer structure.
21945 Always returns one token to the caller in *FIRST_TOKEN. This is
21946 either the true first token of the file, or the first token after
21947 the initial pragma. */
21950 cp_parser_initial_pragma (cp_token *first_token)
21954 cp_lexer_get_preprocessor_token (NULL, first_token);
21955 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21958 cp_lexer_get_preprocessor_token (NULL, first_token);
21959 if (first_token->type == CPP_STRING)
21961 name = first_token->u.value;
21963 cp_lexer_get_preprocessor_token (NULL, first_token);
21964 if (first_token->type != CPP_PRAGMA_EOL)
21965 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21966 &first_token->location);
21969 error ("%Hexpected string literal", &first_token->location);
21971 /* Skip to the end of the pragma. */
21972 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21973 cp_lexer_get_preprocessor_token (NULL, first_token);
21975 /* Now actually load the PCH file. */
21977 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21979 /* Read one more token to return to our caller. We have to do this
21980 after reading the PCH file in, since its pointers have to be
21982 cp_lexer_get_preprocessor_token (NULL, first_token);
21985 /* Normal parsing of a pragma token. Here we can (and must) use the
21989 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21991 cp_token *pragma_tok;
21994 pragma_tok = cp_lexer_consume_token (parser->lexer);
21995 gcc_assert (pragma_tok->type == CPP_PRAGMA);
21996 parser->lexer->in_pragma = true;
21998 id = pragma_tok->pragma_kind;
22001 case PRAGMA_GCC_PCH_PREPROCESS:
22002 error ("%H%<#pragma GCC pch_preprocess%> must be first",
22003 &pragma_tok->location);
22006 case PRAGMA_OMP_BARRIER:
22009 case pragma_compound:
22010 cp_parser_omp_barrier (parser, pragma_tok);
22013 error ("%H%<#pragma omp barrier%> may only be "
22014 "used in compound statements", &pragma_tok->location);
22021 case PRAGMA_OMP_FLUSH:
22024 case pragma_compound:
22025 cp_parser_omp_flush (parser, pragma_tok);
22028 error ("%H%<#pragma omp flush%> may only be "
22029 "used in compound statements", &pragma_tok->location);
22036 case PRAGMA_OMP_TASKWAIT:
22039 case pragma_compound:
22040 cp_parser_omp_taskwait (parser, pragma_tok);
22043 error ("%H%<#pragma omp taskwait%> may only be "
22044 "used in compound statements",
22045 &pragma_tok->location);
22052 case PRAGMA_OMP_THREADPRIVATE:
22053 cp_parser_omp_threadprivate (parser, pragma_tok);
22056 case PRAGMA_OMP_ATOMIC:
22057 case PRAGMA_OMP_CRITICAL:
22058 case PRAGMA_OMP_FOR:
22059 case PRAGMA_OMP_MASTER:
22060 case PRAGMA_OMP_ORDERED:
22061 case PRAGMA_OMP_PARALLEL:
22062 case PRAGMA_OMP_SECTIONS:
22063 case PRAGMA_OMP_SINGLE:
22064 case PRAGMA_OMP_TASK:
22065 if (context == pragma_external)
22067 cp_parser_omp_construct (parser, pragma_tok);
22070 case PRAGMA_OMP_SECTION:
22071 error ("%H%<#pragma omp section%> may only be used in "
22072 "%<#pragma omp sections%> construct", &pragma_tok->location);
22076 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22077 c_invoke_pragma_handler (id);
22081 cp_parser_error (parser, "expected declaration specifiers");
22085 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22089 /* The interface the pragma parsers have to the lexer. */
22092 pragma_lex (tree *value)
22095 enum cpp_ttype ret;
22097 tok = cp_lexer_peek_token (the_parser->lexer);
22100 *value = tok->u.value;
22102 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22104 else if (ret == CPP_STRING)
22105 *value = cp_parser_string_literal (the_parser, false, false);
22108 cp_lexer_consume_token (the_parser->lexer);
22109 if (ret == CPP_KEYWORD)
22117 /* External interface. */
22119 /* Parse one entire translation unit. */
22122 c_parse_file (void)
22124 bool error_occurred;
22125 static bool already_called = false;
22127 if (already_called)
22129 sorry ("inter-module optimizations not implemented for C++");
22132 already_called = true;
22134 the_parser = cp_parser_new ();
22135 push_deferring_access_checks (flag_access_control
22136 ? dk_no_deferred : dk_no_check);
22137 error_occurred = cp_parser_translation_unit (the_parser);
22141 #include "gt-cp-parser.h"