2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a context where it is implicitly extern "C" */
75 BOOL_BITFIELD implicit_extern_c : 1;
76 /* True for a CPP_NAME token that is not a keyword (i.e., for which
77 KEYWORD is RID_MAX) iff this name was looked up and found to be
78 ambiguous. An error has already been reported. */
79 BOOL_BITFIELD ambiguous_p : 1;
80 /* The location at which this token was found. */
82 /* The value associated with this token, if any. */
83 union cp_token_value {
84 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
85 struct tree_check* GTY((tag ("1"))) tree_check_value;
86 /* Use for all other tokens. */
87 tree GTY((tag ("0"))) value;
88 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91 /* We use a stack of token pointer for saving token sets. */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
96 static cp_token eof_token =
98 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
101 /* The cp_lexer structure represents the C++ lexer. It is responsible
102 for managing the token stream from the preprocessor and supplying
103 it to the parser. Tokens are never added to the cp_lexer after
106 typedef struct cp_lexer GTY (())
108 /* The memory allocated for the buffer. NULL if this lexer does not
109 own the token buffer. */
110 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
111 /* If the lexer owns the buffer, this is the number of tokens in the
113 size_t buffer_length;
115 /* A pointer just past the last available token. The tokens
116 in this lexer are [buffer, last_token). */
117 cp_token_position GTY ((skip)) last_token;
119 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
120 no more available tokens. */
121 cp_token_position GTY ((skip)) next_token;
123 /* A stack indicating positions at which cp_lexer_save_tokens was
124 called. The top entry is the most recent position at which we
125 began saving tokens. If the stack is non-empty, we are saving
127 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129 /* The next lexer in a linked list of lexers. */
130 struct cp_lexer *next;
132 /* True if we should output debugging information. */
135 /* True if we're in the context of parsing a pragma, and should not
136 increment past the end-of-line marker. */
140 /* cp_token_cache is a range of tokens. There is no need to represent
141 allocate heap memory for it, since tokens are never removed from the
142 lexer's array. There is also no need for the GC to walk through
143 a cp_token_cache, since everything in here is referenced through
146 typedef struct cp_token_cache GTY(())
148 /* The beginning of the token range. */
149 cp_token * GTY((skip)) first;
151 /* Points immediately after the last token in the range. */
152 cp_token * GTY ((skip)) last;
157 static cp_lexer *cp_lexer_new_main
159 static cp_lexer *cp_lexer_new_from_tokens
160 (cp_token_cache *tokens);
161 static void cp_lexer_destroy
163 static int cp_lexer_saving_tokens
165 static cp_token_position cp_lexer_token_position
167 static cp_token *cp_lexer_token_at
168 (cp_lexer *, cp_token_position);
169 static void cp_lexer_get_preprocessor_token
170 (cp_lexer *, cp_token *);
171 static inline cp_token *cp_lexer_peek_token
173 static cp_token *cp_lexer_peek_nth_token
174 (cp_lexer *, size_t);
175 static inline bool cp_lexer_next_token_is
176 (cp_lexer *, enum cpp_ttype);
177 static bool cp_lexer_next_token_is_not
178 (cp_lexer *, enum cpp_ttype);
179 static bool cp_lexer_next_token_is_keyword
180 (cp_lexer *, enum rid);
181 static cp_token *cp_lexer_consume_token
183 static void cp_lexer_purge_token
185 static void cp_lexer_purge_tokens_after
186 (cp_lexer *, cp_token_position);
187 static void cp_lexer_save_tokens
189 static void cp_lexer_commit_tokens
191 static void cp_lexer_rollback_tokens
193 #ifdef ENABLE_CHECKING
194 static void cp_lexer_print_token
195 (FILE *, cp_token *);
196 static inline bool cp_lexer_debugging_p
198 static void cp_lexer_start_debugging
199 (cp_lexer *) ATTRIBUTE_UNUSED;
200 static void cp_lexer_stop_debugging
201 (cp_lexer *) ATTRIBUTE_UNUSED;
203 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
204 about passing NULL to functions that require non-NULL arguments
205 (fputs, fprintf). It will never be used, so all we need is a value
206 of the right type that's guaranteed not to be NULL. */
207 #define cp_lexer_debug_stream stdout
208 #define cp_lexer_print_token(str, tok) (void) 0
209 #define cp_lexer_debugging_p(lexer) 0
210 #endif /* ENABLE_CHECKING */
212 static cp_token_cache *cp_token_cache_new
213 (cp_token *, cp_token *);
215 static void cp_parser_initial_pragma
218 /* Manifest constants. */
219 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
220 #define CP_SAVED_TOKEN_STACK 5
222 /* A token type for keywords, as opposed to ordinary identifiers. */
223 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225 /* A token type for template-ids. If a template-id is processed while
226 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
227 the value of the CPP_TEMPLATE_ID is whatever was returned by
228 cp_parser_template_id. */
229 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231 /* A token type for nested-name-specifiers. If a
232 nested-name-specifier is processed while parsing tentatively, it is
233 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
234 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
235 cp_parser_nested_name_specifier_opt. */
236 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238 /* A token type for tokens that are not tokens at all; these are used
239 to represent slots in the array where there used to be a token
240 that has now been deleted. */
241 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243 /* The number of token types, including C++-specific ones. */
244 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
248 #ifdef ENABLE_CHECKING
249 /* The stream to which debugging output should be written. */
250 static FILE *cp_lexer_debug_stream;
251 #endif /* ENABLE_CHECKING */
253 /* Create a new main C++ lexer, the lexer that gets tokens from the
257 cp_lexer_new_main (void)
259 cp_token first_token;
266 /* It's possible that parsing the first pragma will load a PCH file,
267 which is a GC collection point. So we have to do that before
268 allocating any memory. */
269 cp_parser_initial_pragma (&first_token);
271 c_common_no_more_pch ();
273 /* Allocate the memory. */
274 lexer = GGC_CNEW (cp_lexer);
276 #ifdef ENABLE_CHECKING
277 /* Initially we are not debugging. */
278 lexer->debugging_p = false;
279 #endif /* ENABLE_CHECKING */
280 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
281 CP_SAVED_TOKEN_STACK);
283 /* Create the buffer. */
284 alloc = CP_LEXER_BUFFER_SIZE;
285 buffer = GGC_NEWVEC (cp_token, alloc);
287 /* Put the first token in the buffer. */
292 /* Get the remaining tokens from the preprocessor. */
293 while (pos->type != CPP_EOF)
300 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
301 pos = buffer + space;
303 cp_lexer_get_preprocessor_token (lexer, pos);
305 lexer->buffer = buffer;
306 lexer->buffer_length = alloc - space;
307 lexer->last_token = pos;
308 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310 /* Subsequent preprocessor diagnostics should use compiler
311 diagnostic functions to get the compiler source location. */
312 cpp_get_options (parse_in)->client_diagnostic = true;
313 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 /* Frees all resources associated with LEXER. */
350 cp_lexer_destroy (cp_lexer *lexer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
384 /* nonzero if we are presently saving tokens. */
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
425 /* Update the value. Some keywords are mapped to particular
426 entities, rather than simply having the value of the
427 corresponding IDENTIFIER_NODE. For example, `__const' is
428 mapped to `const'. */
429 token->u.value = ridpointers[token->keyword];
433 if (warn_cxx0x_compat
434 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
435 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437 /* Warn about the C++0x keyword (but still treat it as
439 warning (OPT_Wc__0x_compat,
440 "identifier %<%s%> will become a keyword in C++0x",
441 IDENTIFIER_POINTER (token->u.value));
443 /* Clear out the C_RID_CODE so we don't warn about this
444 particular identifier-turned-keyword again. */
445 C_SET_RID_CODE (token->u.value, RID_MAX);
448 token->ambiguous_p = false;
449 token->keyword = RID_MAX;
452 /* Handle Objective-C++ keywords. */
453 else if (token->type == CPP_AT_NAME)
455 token->type = CPP_KEYWORD;
456 switch (C_RID_CODE (token->u.value))
458 /* Map 'class' to '@class', 'private' to '@private', etc. */
459 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
460 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
461 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
462 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
463 case RID_THROW: token->keyword = RID_AT_THROW; break;
464 case RID_TRY: token->keyword = RID_AT_TRY; break;
465 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
466 default: token->keyword = C_RID_CODE (token->u.value);
469 else if (token->type == CPP_PRAGMA)
471 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
472 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
473 token->u.value = NULL_TREE;
477 /* Update the globals input_location and the input file stack from TOKEN. */
479 cp_lexer_set_source_position_from_token (cp_token *token)
481 if (token->type != CPP_EOF)
483 input_location = token->location;
487 /* Return a pointer to the next token in the token stream, but do not
490 static inline cp_token *
491 cp_lexer_peek_token (cp_lexer *lexer)
493 if (cp_lexer_debugging_p (lexer))
495 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
496 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
497 putc ('\n', cp_lexer_debug_stream);
499 return lexer->next_token;
502 /* Return true if the next token has the indicated TYPE. */
505 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 return cp_lexer_peek_token (lexer)->type == type;
510 /* Return true if the next token does not have the indicated TYPE. */
513 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 return !cp_lexer_next_token_is (lexer, type);
518 /* Return true if the next token is the indicated KEYWORD. */
521 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 return cp_lexer_peek_token (lexer)->keyword == keyword;
526 /* Return true if the next token is not the indicated KEYWORD. */
529 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 return cp_lexer_peek_token (lexer)->keyword != keyword;
534 /* Return true if the next token is a keyword for a decl-specifier. */
537 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
541 token = cp_lexer_peek_token (lexer);
542 switch (token->keyword)
544 /* auto specifier: storage-class-specifier in C++,
545 simple-type-specifier in C++0x. */
547 /* Storage classes. */
553 /* Elaborated type specifiers. */
559 /* Simple type specifiers. */
573 /* GNU extensions. */
576 /* C++0x extensions. */
585 /* Return a pointer to the Nth token in the token stream. If N is 1,
586 then this is precisely equivalent to cp_lexer_peek_token (except
587 that it is not inline). One would like to disallow that case, but
588 there is one case (cp_parser_nth_token_starts_template_id) where
589 the caller passes a variable for N and it might be 1. */
592 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
596 /* N is 1-based, not zero-based. */
599 if (cp_lexer_debugging_p (lexer))
600 fprintf (cp_lexer_debug_stream,
601 "cp_lexer: peeking ahead %ld at token: ", (long)n);
604 token = lexer->next_token;
605 gcc_assert (!n || token != &eof_token);
609 if (token == lexer->last_token)
615 if (token->type != CPP_PURGED)
619 if (cp_lexer_debugging_p (lexer))
621 cp_lexer_print_token (cp_lexer_debug_stream, token);
622 putc ('\n', cp_lexer_debug_stream);
628 /* Return the next token, and advance the lexer's next_token pointer
629 to point to the next non-purged token. */
632 cp_lexer_consume_token (cp_lexer* lexer)
634 cp_token *token = lexer->next_token;
636 gcc_assert (token != &eof_token);
637 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
642 if (lexer->next_token == lexer->last_token)
644 lexer->next_token = &eof_token;
649 while (lexer->next_token->type == CPP_PURGED);
651 cp_lexer_set_source_position_from_token (token);
653 /* Provide debugging output. */
654 if (cp_lexer_debugging_p (lexer))
656 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
657 cp_lexer_print_token (cp_lexer_debug_stream, token);
658 putc ('\n', cp_lexer_debug_stream);
664 /* Permanently remove the next token from the token stream, and
665 advance the next_token pointer to refer to the next non-purged
669 cp_lexer_purge_token (cp_lexer *lexer)
671 cp_token *tok = lexer->next_token;
673 gcc_assert (tok != &eof_token);
674 tok->type = CPP_PURGED;
675 tok->location = UNKNOWN_LOCATION;
676 tok->u.value = NULL_TREE;
677 tok->keyword = RID_MAX;
682 if (tok == lexer->last_token)
688 while (tok->type == CPP_PURGED);
689 lexer->next_token = tok;
692 /* Permanently remove all tokens after TOK, up to, but not
693 including, the token that will be returned next by
694 cp_lexer_peek_token. */
697 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 cp_token *peek = lexer->next_token;
701 if (peek == &eof_token)
702 peek = lexer->last_token;
704 gcc_assert (tok < peek);
706 for ( tok += 1; tok != peek; tok += 1)
708 tok->type = CPP_PURGED;
709 tok->location = UNKNOWN_LOCATION;
710 tok->u.value = NULL_TREE;
711 tok->keyword = RID_MAX;
715 /* Begin saving tokens. All tokens consumed after this point will be
719 cp_lexer_save_tokens (cp_lexer* lexer)
721 /* Provide debugging output. */
722 if (cp_lexer_debugging_p (lexer))
723 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725 VEC_safe_push (cp_token_position, heap,
726 lexer->saved_tokens, lexer->next_token);
729 /* Commit to the portion of the token stream most recently saved. */
732 cp_lexer_commit_tokens (cp_lexer* lexer)
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
736 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738 VEC_pop (cp_token_position, lexer->saved_tokens);
741 /* Return all tokens saved since the last call to cp_lexer_save_tokens
742 to the token stream. Stop saving tokens. */
745 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 /* Provide debugging output. */
748 if (cp_lexer_debugging_p (lexer))
749 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
754 /* Print a representation of the TOKEN on the STREAM. */
756 #ifdef ENABLE_CHECKING
759 cp_lexer_print_token (FILE * stream, cp_token *token)
761 /* We don't use cpp_type2name here because the parser defines
762 a few tokens of its own. */
763 static const char *const token_names[] = {
764 /* cpplib-defined token types */
770 /* C++ parser token types - see "Manifest constants", above. */
773 "NESTED_NAME_SPECIFIER",
777 /* If we have a name for the token, print it out. Otherwise, we
778 simply give the numeric code. */
779 gcc_assert (token->type < ARRAY_SIZE(token_names));
780 fputs (token_names[token->type], stream);
782 /* For some tokens, print the associated data. */
786 /* Some keywords have a value that is not an IDENTIFIER_NODE.
787 For example, `struct' is mapped to an INTEGER_CST. */
788 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790 /* else fall through */
792 fputs (IDENTIFIER_POINTER (token->u.value), stream);
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
807 /* Start emitting debugging information. */
810 cp_lexer_start_debugging (cp_lexer* lexer)
812 lexer->debugging_p = true;
815 /* Stop emitting debugging information. */
818 cp_lexer_stop_debugging (cp_lexer* lexer)
820 lexer->debugging_p = false;
823 #endif /* ENABLE_CHECKING */
825 /* Create a new cp_token_cache, representing a range of tokens. */
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
837 /* Decl-specifiers. */
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, tree, cp_cv_quals, tree, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *, bool);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
870 /* The obstack on which declarators and related data structures are
872 static struct obstack declarator_obstack;
874 /* Alloc BYTES from the declarator memory pool. */
877 alloc_declarator (size_t bytes)
879 return obstack_alloc (&declarator_obstack, bytes);
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
888 cp_declarator *declarator;
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
894 declarator->parameter_pack_p = false;
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907 special_function_kind sfk)
909 cp_declarator *declarator;
911 /* It is valid to write:
913 class C { void f(); };
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
921 if (qualifying_scope && TYPE_P (qualifying_scope))
922 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928 declarator = make_declarator (cdk_id);
929 declarator->u.id.qualifying_scope = qualifying_scope;
930 declarator->u.id.unqualified_name = unqualified_name;
931 declarator->u.id.sfk = sfk;
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 cp_declarator *declarator;
945 declarator = make_declarator (cdk_pointer);
946 declarator->declarator = target;
947 declarator->u.pointer.qualifiers = cv_qualifiers;
948 declarator->u.pointer.class_type = NULL_TREE;
951 declarator->parameter_pack_p = target->parameter_pack_p;
952 target->parameter_pack_p = false;
955 declarator->parameter_pack_p = false;
960 /* Like make_pointer_declarator -- but for references. */
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
966 cp_declarator *declarator;
968 declarator = make_declarator (cdk_reference);
969 declarator->declarator = target;
970 declarator->u.reference.qualifiers = cv_qualifiers;
971 declarator->u.reference.rvalue_ref = rvalue_ref;
974 declarator->parameter_pack_p = target->parameter_pack_p;
975 target->parameter_pack_p = false;
978 declarator->parameter_pack_p = false;
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984 member of CLASS_TYPE. */
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988 cp_declarator *pointee)
990 cp_declarator *declarator;
992 declarator = make_declarator (cdk_ptrmem);
993 declarator->declarator = pointee;
994 declarator->u.pointer.qualifiers = cv_qualifiers;
995 declarator->u.pointer.class_type = class_type;
999 declarator->parameter_pack_p = pointee->parameter_pack_p;
1000 pointee->parameter_pack_p = false;
1003 declarator->parameter_pack_p = false;
1008 /* Make a declarator for the function given by TARGET, with the
1009 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1010 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1011 indicates what exceptions can be thrown. */
1014 make_call_declarator (cp_declarator *target,
1016 cp_cv_quals cv_qualifiers,
1017 tree exception_specification,
1018 tree late_return_type)
1020 cp_declarator *declarator;
1022 declarator = make_declarator (cdk_function);
1023 declarator->declarator = target;
1024 declarator->u.function.parameters = parms;
1025 declarator->u.function.qualifiers = cv_qualifiers;
1026 declarator->u.function.exception_specification = exception_specification;
1027 declarator->u.function.late_return_type = late_return_type;
1030 declarator->parameter_pack_p = target->parameter_pack_p;
1031 target->parameter_pack_p = false;
1034 declarator->parameter_pack_p = false;
1039 /* Make a declarator for an array of BOUNDS elements, each of which is
1040 defined by ELEMENT. */
1043 make_array_declarator (cp_declarator *element, tree bounds)
1045 cp_declarator *declarator;
1047 declarator = make_declarator (cdk_array);
1048 declarator->declarator = element;
1049 declarator->u.array.bounds = bounds;
1052 declarator->parameter_pack_p = element->parameter_pack_p;
1053 element->parameter_pack_p = false;
1056 declarator->parameter_pack_p = false;
1061 /* Determine whether the declarator we've seen so far can be a
1062 parameter pack, when followed by an ellipsis. */
1064 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 /* Search for a declarator name, or any other declarator that goes
1067 after the point where the ellipsis could appear in a parameter
1068 pack. If we find any of these, then this declarator can not be
1069 made into a parameter pack. */
1071 while (declarator && !found)
1073 switch ((int)declarator->kind)
1084 declarator = declarator->declarator;
1092 cp_parameter_declarator *no_parameters;
1094 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1095 DECLARATOR and DEFAULT_ARGUMENT. */
1097 cp_parameter_declarator *
1098 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1099 cp_declarator *declarator,
1100 tree default_argument)
1102 cp_parameter_declarator *parameter;
1104 parameter = ((cp_parameter_declarator *)
1105 alloc_declarator (sizeof (cp_parameter_declarator)));
1106 parameter->next = NULL;
1107 if (decl_specifiers)
1108 parameter->decl_specifiers = *decl_specifiers;
1110 clear_decl_specs (¶meter->decl_specifiers);
1111 parameter->declarator = declarator;
1112 parameter->default_argument = default_argument;
1113 parameter->ellipsis_p = false;
1118 /* Returns true iff DECLARATOR is a declaration for a function. */
1121 function_declarator_p (const cp_declarator *declarator)
1125 if (declarator->kind == cdk_function
1126 && declarator->declarator->kind == cdk_id)
1128 if (declarator->kind == cdk_id
1129 || declarator->kind == cdk_error)
1131 declarator = declarator->declarator;
1141 A cp_parser parses the token stream as specified by the C++
1142 grammar. Its job is purely parsing, not semantic analysis. For
1143 example, the parser breaks the token stream into declarators,
1144 expressions, statements, and other similar syntactic constructs.
1145 It does not check that the types of the expressions on either side
1146 of an assignment-statement are compatible, or that a function is
1147 not declared with a parameter of type `void'.
1149 The parser invokes routines elsewhere in the compiler to perform
1150 semantic analysis and to build up the abstract syntax tree for the
1153 The parser (and the template instantiation code, which is, in a
1154 way, a close relative of parsing) are the only parts of the
1155 compiler that should be calling push_scope and pop_scope, or
1156 related functions. The parser (and template instantiation code)
1157 keeps track of what scope is presently active; everything else
1158 should simply honor that. (The code that generates static
1159 initializers may also need to set the scope, in order to check
1160 access control correctly when emitting the initializers.)
1165 The parser is of the standard recursive-descent variety. Upcoming
1166 tokens in the token stream are examined in order to determine which
1167 production to use when parsing a non-terminal. Some C++ constructs
1168 require arbitrary look ahead to disambiguate. For example, it is
1169 impossible, in the general case, to tell whether a statement is an
1170 expression or declaration without scanning the entire statement.
1171 Therefore, the parser is capable of "parsing tentatively." When the
1172 parser is not sure what construct comes next, it enters this mode.
1173 Then, while we attempt to parse the construct, the parser queues up
1174 error messages, rather than issuing them immediately, and saves the
1175 tokens it consumes. If the construct is parsed successfully, the
1176 parser "commits", i.e., it issues any queued error messages and
1177 the tokens that were being preserved are permanently discarded.
1178 If, however, the construct is not parsed successfully, the parser
1179 rolls back its state completely so that it can resume parsing using
1180 a different alternative.
1185 The performance of the parser could probably be improved substantially.
1186 We could often eliminate the need to parse tentatively by looking ahead
1187 a little bit. In some places, this approach might not entirely eliminate
1188 the need to parse tentatively, but it might still speed up the average
1191 /* Flags that are passed to some parsing functions. These values can
1192 be bitwise-ored together. */
1194 typedef enum cp_parser_flags
1197 CP_PARSER_FLAGS_NONE = 0x0,
1198 /* The construct is optional. If it is not present, then no error
1199 should be issued. */
1200 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1201 /* When parsing a type-specifier, treat user-defined type-names
1202 as non-type identifiers. */
1203 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1204 /* When parsing a type-specifier, do not try to parse a class-specifier
1205 or enum-specifier. */
1206 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1209 /* The different kinds of declarators we want to parse. */
1211 typedef enum cp_parser_declarator_kind
1213 /* We want an abstract declarator. */
1214 CP_PARSER_DECLARATOR_ABSTRACT,
1215 /* We want a named declarator. */
1216 CP_PARSER_DECLARATOR_NAMED,
1217 /* We don't mind, but the name must be an unqualified-id. */
1218 CP_PARSER_DECLARATOR_EITHER
1219 } cp_parser_declarator_kind;
1221 /* The precedence values used to parse binary expressions. The minimum value
1222 of PREC must be 1, because zero is reserved to quickly discriminate
1223 binary operators from other tokens. */
1228 PREC_LOGICAL_OR_EXPRESSION,
1229 PREC_LOGICAL_AND_EXPRESSION,
1230 PREC_INCLUSIVE_OR_EXPRESSION,
1231 PREC_EXCLUSIVE_OR_EXPRESSION,
1232 PREC_AND_EXPRESSION,
1233 PREC_EQUALITY_EXPRESSION,
1234 PREC_RELATIONAL_EXPRESSION,
1235 PREC_SHIFT_EXPRESSION,
1236 PREC_ADDITIVE_EXPRESSION,
1237 PREC_MULTIPLICATIVE_EXPRESSION,
1239 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1242 /* A mapping from a token type to a corresponding tree node type, with a
1243 precedence value. */
1245 typedef struct cp_parser_binary_operations_map_node
1247 /* The token type. */
1248 enum cpp_ttype token_type;
1249 /* The corresponding tree code. */
1250 enum tree_code tree_type;
1251 /* The precedence of this operator. */
1252 enum cp_parser_prec prec;
1253 } cp_parser_binary_operations_map_node;
1255 /* The status of a tentative parse. */
1257 typedef enum cp_parser_status_kind
1259 /* No errors have occurred. */
1260 CP_PARSER_STATUS_KIND_NO_ERROR,
1261 /* An error has occurred. */
1262 CP_PARSER_STATUS_KIND_ERROR,
1263 /* We are committed to this tentative parse, whether or not an error
1265 CP_PARSER_STATUS_KIND_COMMITTED
1266 } cp_parser_status_kind;
1268 typedef struct cp_parser_expression_stack_entry
1270 /* Left hand side of the binary operation we are currently
1273 /* Original tree code for left hand side, if it was a binary
1274 expression itself (used for -Wparentheses). */
1275 enum tree_code lhs_type;
1276 /* Tree code for the binary operation we are parsing. */
1277 enum tree_code tree_type;
1278 /* Precedence of the binary operation we are parsing. */
1280 } cp_parser_expression_stack_entry;
1282 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1283 entries because precedence levels on the stack are monotonically
1285 typedef struct cp_parser_expression_stack_entry
1286 cp_parser_expression_stack[NUM_PREC_VALUES];
1288 /* Context that is saved and restored when parsing tentatively. */
1289 typedef struct cp_parser_context GTY (())
1291 /* If this is a tentative parsing context, the status of the
1293 enum cp_parser_status_kind status;
1294 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1295 that are looked up in this context must be looked up both in the
1296 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297 the context of the containing expression. */
1300 /* The next parsing context in the stack. */
1301 struct cp_parser_context *next;
1302 } cp_parser_context;
1306 /* Constructors and destructors. */
1308 static cp_parser_context *cp_parser_context_new
1309 (cp_parser_context *);
1311 /* Class variables. */
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316 Transformed into an associative array (binops_by_token) by
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1323 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1327 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1330 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1333 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1338 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1341 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1343 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1345 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1347 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1349 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1352 /* The same as binops, but initialized by cp_parser_new so that
1353 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1357 /* Constructors and destructors. */
1359 /* Construct a new context. The context below this one on the stack
1360 is given by NEXT. */
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1365 cp_parser_context *context;
1367 /* Allocate the storage. */
1368 if (cp_parser_context_free_list != NULL)
1370 /* Pull the first entry from the free list. */
1371 context = cp_parser_context_free_list;
1372 cp_parser_context_free_list = context->next;
1373 memset (context, 0, sizeof (*context));
1376 context = GGC_CNEW (cp_parser_context);
1378 /* No errors have occurred yet in this context. */
1379 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380 /* If this is not the bottommost context, copy information that we
1381 need from the previous context. */
1384 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385 expression, then we are parsing one in this context, too. */
1386 context->object_type = next->object_type;
1387 /* Thread the stack. */
1388 context->next = next;
1394 /* The cp_parser structure represents the C++ parser. */
1396 typedef struct cp_parser GTY(())
1398 /* The lexer from which we are obtaining tokens. */
1401 /* The scope in which names should be looked up. If NULL_TREE, then
1402 we look up names in the scope that is currently open in the
1403 source program. If non-NULL, this is either a TYPE or
1404 NAMESPACE_DECL for the scope in which we should look. It can
1405 also be ERROR_MARK, when we've parsed a bogus scope.
1407 This value is not cleared automatically after a name is looked
1408 up, so we must be careful to clear it before starting a new look
1409 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1410 will look up `Z' in the scope of `X', rather than the current
1411 scope.) Unfortunately, it is difficult to tell when name lookup
1412 is complete, because we sometimes peek at a token, look it up,
1413 and then decide not to consume it. */
1416 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1417 last lookup took place. OBJECT_SCOPE is used if an expression
1418 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1419 respectively. QUALIFYING_SCOPE is used for an expression of the
1420 form "X::Y"; it refers to X. */
1422 tree qualifying_scope;
1424 /* A stack of parsing contexts. All but the bottom entry on the
1425 stack will be tentative contexts.
1427 We parse tentatively in order to determine which construct is in
1428 use in some situations. For example, in order to determine
1429 whether a statement is an expression-statement or a
1430 declaration-statement we parse it tentatively as a
1431 declaration-statement. If that fails, we then reparse the same
1432 token stream as an expression-statement. */
1433 cp_parser_context *context;
1435 /* True if we are parsing GNU C++. If this flag is not set, then
1436 GNU extensions are not recognized. */
1437 bool allow_gnu_extensions_p;
1439 /* TRUE if the `>' token should be interpreted as the greater-than
1440 operator. FALSE if it is the end of a template-id or
1441 template-parameter-list. In C++0x mode, this flag also applies to
1442 `>>' tokens, which are viewed as two consecutive `>' tokens when
1443 this flag is FALSE. */
1444 bool greater_than_is_operator_p;
1446 /* TRUE if default arguments are allowed within a parameter list
1447 that starts at this point. FALSE if only a gnu extension makes
1448 them permissible. */
1449 bool default_arg_ok_p;
1451 /* TRUE if we are parsing an integral constant-expression. See
1452 [expr.const] for a precise definition. */
1453 bool integral_constant_expression_p;
1455 /* TRUE if we are parsing an integral constant-expression -- but a
1456 non-constant expression should be permitted as well. This flag
1457 is used when parsing an array bound so that GNU variable-length
1458 arrays are tolerated. */
1459 bool allow_non_integral_constant_expression_p;
1461 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1462 been seen that makes the expression non-constant. */
1463 bool non_integral_constant_expression_p;
1465 /* TRUE if local variable names and `this' are forbidden in the
1467 bool local_variables_forbidden_p;
1469 /* TRUE if the declaration we are parsing is part of a
1470 linkage-specification of the form `extern string-literal
1472 bool in_unbraced_linkage_specification_p;
1474 /* TRUE if we are presently parsing a declarator, after the
1475 direct-declarator. */
1476 bool in_declarator_p;
1478 /* TRUE if we are presently parsing a template-argument-list. */
1479 bool in_template_argument_list_p;
1481 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1482 to IN_OMP_BLOCK if parsing OpenMP structured block and
1483 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1484 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1485 iteration-statement, OpenMP block or loop within that switch. */
1486 #define IN_SWITCH_STMT 1
1487 #define IN_ITERATION_STMT 2
1488 #define IN_OMP_BLOCK 4
1489 #define IN_OMP_FOR 8
1490 #define IN_IF_STMT 16
1491 unsigned char in_statement;
1493 /* TRUE if we are presently parsing the body of a switch statement.
1494 Note that this doesn't quite overlap with in_statement above.
1495 The difference relates to giving the right sets of error messages:
1496 "case not in switch" vs "break statement used with OpenMP...". */
1497 bool in_switch_statement_p;
1499 /* TRUE if we are parsing a type-id in an expression context. In
1500 such a situation, both "type (expr)" and "type (type)" are valid
1502 bool in_type_id_in_expr_p;
1504 /* TRUE if we are currently in a header file where declarations are
1505 implicitly extern "C". */
1506 bool implicit_extern_c;
1508 /* TRUE if strings in expressions should be translated to the execution
1510 bool translate_strings_p;
1512 /* TRUE if we are presently parsing the body of a function, but not
1514 bool in_function_body;
1516 /* If non-NULL, then we are parsing a construct where new type
1517 definitions are not permitted. The string stored here will be
1518 issued as an error message if a type is defined. */
1519 const char *type_definition_forbidden_message;
1521 /* A list of lists. The outer list is a stack, used for member
1522 functions of local classes. At each level there are two sub-list,
1523 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1524 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1525 TREE_VALUE's. The functions are chained in reverse declaration
1528 The TREE_PURPOSE sublist contains those functions with default
1529 arguments that need post processing, and the TREE_VALUE sublist
1530 contains those functions with definitions that need post
1533 These lists can only be processed once the outermost class being
1534 defined is complete. */
1535 tree unparsed_functions_queues;
1537 /* The number of classes whose definitions are currently in
1539 unsigned num_classes_being_defined;
1541 /* The number of template parameter lists that apply directly to the
1542 current declaration. */
1543 unsigned num_template_parameter_lists;
1548 /* Constructors and destructors. */
1550 static cp_parser *cp_parser_new
1553 /* Routines to parse various constructs.
1555 Those that return `tree' will return the error_mark_node (rather
1556 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1557 Sometimes, they will return an ordinary node if error-recovery was
1558 attempted, even though a parse error occurred. So, to check
1559 whether or not a parse error occurred, you should always use
1560 cp_parser_error_occurred. If the construct is optional (indicated
1561 either by an `_opt' in the name of the function that does the
1562 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1563 the construct is not present. */
1565 /* Lexical conventions [gram.lex] */
1567 static tree cp_parser_identifier
1569 static tree cp_parser_string_literal
1570 (cp_parser *, bool, bool);
1572 /* Basic concepts [gram.basic] */
1574 static bool cp_parser_translation_unit
1577 /* Expressions [gram.expr] */
1579 static tree cp_parser_primary_expression
1580 (cp_parser *, bool, bool, bool, cp_id_kind *);
1581 static tree cp_parser_id_expression
1582 (cp_parser *, bool, bool, bool *, bool, bool);
1583 static tree cp_parser_unqualified_id
1584 (cp_parser *, bool, bool, bool, bool);
1585 static tree cp_parser_nested_name_specifier_opt
1586 (cp_parser *, bool, bool, bool, bool);
1587 static tree cp_parser_nested_name_specifier
1588 (cp_parser *, bool, bool, bool, bool);
1589 static tree cp_parser_qualifying_entity
1590 (cp_parser *, bool, bool, bool, bool, bool);
1591 static tree cp_parser_postfix_expression
1592 (cp_parser *, bool, bool, bool, cp_id_kind *);
1593 static tree cp_parser_postfix_open_square_expression
1594 (cp_parser *, tree, bool);
1595 static tree cp_parser_postfix_dot_deref_expression
1596 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1597 static tree cp_parser_parenthesized_expression_list
1598 (cp_parser *, bool, bool, bool, bool *);
1599 static void cp_parser_pseudo_destructor_name
1600 (cp_parser *, tree *, tree *);
1601 static tree cp_parser_unary_expression
1602 (cp_parser *, bool, bool, cp_id_kind *);
1603 static enum tree_code cp_parser_unary_operator
1605 static tree cp_parser_new_expression
1607 static tree cp_parser_new_placement
1609 static tree cp_parser_new_type_id
1610 (cp_parser *, tree *);
1611 static cp_declarator *cp_parser_new_declarator_opt
1613 static cp_declarator *cp_parser_direct_new_declarator
1615 static tree cp_parser_new_initializer
1617 static tree cp_parser_delete_expression
1619 static tree cp_parser_cast_expression
1620 (cp_parser *, bool, bool, cp_id_kind *);
1621 static tree cp_parser_binary_expression
1622 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1623 static tree cp_parser_question_colon_clause
1624 (cp_parser *, tree);
1625 static tree cp_parser_assignment_expression
1626 (cp_parser *, bool, cp_id_kind *);
1627 static enum tree_code cp_parser_assignment_operator_opt
1629 static tree cp_parser_expression
1630 (cp_parser *, bool, cp_id_kind *);
1631 static tree cp_parser_constant_expression
1632 (cp_parser *, bool, bool *);
1633 static tree cp_parser_builtin_offsetof
1636 /* Statements [gram.stmt.stmt] */
1638 static void cp_parser_statement
1639 (cp_parser *, tree, bool, bool *);
1640 static void cp_parser_label_for_labeled_statement
1642 static tree cp_parser_expression_statement
1643 (cp_parser *, tree);
1644 static tree cp_parser_compound_statement
1645 (cp_parser *, tree, bool);
1646 static void cp_parser_statement_seq_opt
1647 (cp_parser *, tree);
1648 static tree cp_parser_selection_statement
1649 (cp_parser *, bool *);
1650 static tree cp_parser_condition
1652 static tree cp_parser_iteration_statement
1654 static void cp_parser_for_init_statement
1656 static tree cp_parser_jump_statement
1658 static void cp_parser_declaration_statement
1661 static tree cp_parser_implicitly_scoped_statement
1662 (cp_parser *, bool *);
1663 static void cp_parser_already_scoped_statement
1666 /* Declarations [gram.dcl.dcl] */
1668 static void cp_parser_declaration_seq_opt
1670 static void cp_parser_declaration
1672 static void cp_parser_block_declaration
1673 (cp_parser *, bool);
1674 static void cp_parser_simple_declaration
1675 (cp_parser *, bool);
1676 static void cp_parser_decl_specifier_seq
1677 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1678 static tree cp_parser_storage_class_specifier_opt
1680 static tree cp_parser_function_specifier_opt
1681 (cp_parser *, cp_decl_specifier_seq *);
1682 static tree cp_parser_type_specifier
1683 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1685 static tree cp_parser_simple_type_specifier
1686 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1687 static tree cp_parser_type_name
1689 static tree cp_parser_nonclass_name
1690 (cp_parser* parser);
1691 static tree cp_parser_elaborated_type_specifier
1692 (cp_parser *, bool, bool);
1693 static tree cp_parser_enum_specifier
1695 static void cp_parser_enumerator_list
1696 (cp_parser *, tree);
1697 static void cp_parser_enumerator_definition
1698 (cp_parser *, tree);
1699 static tree cp_parser_namespace_name
1701 static void cp_parser_namespace_definition
1703 static void cp_parser_namespace_body
1705 static tree cp_parser_qualified_namespace_specifier
1707 static void cp_parser_namespace_alias_definition
1709 static bool cp_parser_using_declaration
1710 (cp_parser *, bool);
1711 static void cp_parser_using_directive
1713 static void cp_parser_asm_definition
1715 static void cp_parser_linkage_specification
1717 static void cp_parser_static_assert
1718 (cp_parser *, bool);
1719 static tree cp_parser_decltype
1722 /* Declarators [gram.dcl.decl] */
1724 static tree cp_parser_init_declarator
1725 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1726 static cp_declarator *cp_parser_declarator
1727 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1728 static cp_declarator *cp_parser_direct_declarator
1729 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1730 static enum tree_code cp_parser_ptr_operator
1731 (cp_parser *, tree *, cp_cv_quals *);
1732 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1734 static tree cp_parser_late_return_type_opt
1736 static tree cp_parser_declarator_id
1737 (cp_parser *, bool);
1738 static tree cp_parser_type_id
1740 static tree cp_parser_template_type_arg
1742 static tree cp_parser_trailing_type_id (cp_parser *);
1743 static tree cp_parser_type_id_1
1744 (cp_parser *, bool, bool);
1745 static void cp_parser_type_specifier_seq
1746 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1747 static tree cp_parser_parameter_declaration_clause
1749 static tree cp_parser_parameter_declaration_list
1750 (cp_parser *, bool *);
1751 static cp_parameter_declarator *cp_parser_parameter_declaration
1752 (cp_parser *, bool, bool *);
1753 static tree cp_parser_default_argument
1754 (cp_parser *, bool);
1755 static void cp_parser_function_body
1757 static tree cp_parser_initializer
1758 (cp_parser *, bool *, bool *);
1759 static tree cp_parser_initializer_clause
1760 (cp_parser *, bool *);
1761 static tree cp_parser_braced_list
1762 (cp_parser*, bool*);
1763 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1764 (cp_parser *, bool *);
1766 static bool cp_parser_ctor_initializer_opt_and_function_body
1769 /* Classes [gram.class] */
1771 static tree cp_parser_class_name
1772 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1773 static tree cp_parser_class_specifier
1775 static tree cp_parser_class_head
1776 (cp_parser *, bool *, tree *, tree *);
1777 static enum tag_types cp_parser_class_key
1779 static void cp_parser_member_specification_opt
1781 static void cp_parser_member_declaration
1783 static tree cp_parser_pure_specifier
1785 static tree cp_parser_constant_initializer
1788 /* Derived classes [gram.class.derived] */
1790 static tree cp_parser_base_clause
1792 static tree cp_parser_base_specifier
1795 /* Special member functions [gram.special] */
1797 static tree cp_parser_conversion_function_id
1799 static tree cp_parser_conversion_type_id
1801 static cp_declarator *cp_parser_conversion_declarator_opt
1803 static bool cp_parser_ctor_initializer_opt
1805 static void cp_parser_mem_initializer_list
1807 static tree cp_parser_mem_initializer
1809 static tree cp_parser_mem_initializer_id
1812 /* Overloading [gram.over] */
1814 static tree cp_parser_operator_function_id
1816 static tree cp_parser_operator
1819 /* Templates [gram.temp] */
1821 static void cp_parser_template_declaration
1822 (cp_parser *, bool);
1823 static tree cp_parser_template_parameter_list
1825 static tree cp_parser_template_parameter
1826 (cp_parser *, bool *, bool *);
1827 static tree cp_parser_type_parameter
1828 (cp_parser *, bool *);
1829 static tree cp_parser_template_id
1830 (cp_parser *, bool, bool, bool);
1831 static tree cp_parser_template_name
1832 (cp_parser *, bool, bool, bool, bool *);
1833 static tree cp_parser_template_argument_list
1835 static tree cp_parser_template_argument
1837 static void cp_parser_explicit_instantiation
1839 static void cp_parser_explicit_specialization
1842 /* Exception handling [gram.exception] */
1844 static tree cp_parser_try_block
1846 static bool cp_parser_function_try_block
1848 static void cp_parser_handler_seq
1850 static void cp_parser_handler
1852 static tree cp_parser_exception_declaration
1854 static tree cp_parser_throw_expression
1856 static tree cp_parser_exception_specification_opt
1858 static tree cp_parser_type_id_list
1861 /* GNU Extensions */
1863 static tree cp_parser_asm_specification_opt
1865 static tree cp_parser_asm_operand_list
1867 static tree cp_parser_asm_clobber_list
1869 static tree cp_parser_attributes_opt
1871 static tree cp_parser_attribute_list
1873 static bool cp_parser_extension_opt
1874 (cp_parser *, int *);
1875 static void cp_parser_label_declaration
1878 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1879 static bool cp_parser_pragma
1880 (cp_parser *, enum pragma_context);
1882 /* Objective-C++ Productions */
1884 static tree cp_parser_objc_message_receiver
1886 static tree cp_parser_objc_message_args
1888 static tree cp_parser_objc_message_expression
1890 static tree cp_parser_objc_encode_expression
1892 static tree cp_parser_objc_defs_expression
1894 static tree cp_parser_objc_protocol_expression
1896 static tree cp_parser_objc_selector_expression
1898 static tree cp_parser_objc_expression
1900 static bool cp_parser_objc_selector_p
1902 static tree cp_parser_objc_selector
1904 static tree cp_parser_objc_protocol_refs_opt
1906 static void cp_parser_objc_declaration
1908 static tree cp_parser_objc_statement
1911 /* Utility Routines */
1913 static tree cp_parser_lookup_name
1914 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1915 static tree cp_parser_lookup_name_simple
1916 (cp_parser *, tree, location_t);
1917 static tree cp_parser_maybe_treat_template_as_class
1919 static bool cp_parser_check_declarator_template_parameters
1920 (cp_parser *, cp_declarator *, location_t);
1921 static bool cp_parser_check_template_parameters
1922 (cp_parser *, unsigned, location_t);
1923 static tree cp_parser_simple_cast_expression
1925 static tree cp_parser_global_scope_opt
1926 (cp_parser *, bool);
1927 static bool cp_parser_constructor_declarator_p
1928 (cp_parser *, bool);
1929 static tree cp_parser_function_definition_from_specifiers_and_declarator
1930 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1931 static tree cp_parser_function_definition_after_declarator
1932 (cp_parser *, bool);
1933 static void cp_parser_template_declaration_after_export
1934 (cp_parser *, bool);
1935 static void cp_parser_perform_template_parameter_access_checks
1936 (VEC (deferred_access_check,gc)*);
1937 static tree cp_parser_single_declaration
1938 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1939 static tree cp_parser_functional_cast
1940 (cp_parser *, tree);
1941 static tree cp_parser_save_member_function_body
1942 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1943 static tree cp_parser_enclosed_template_argument_list
1945 static void cp_parser_save_default_args
1946 (cp_parser *, tree);
1947 static void cp_parser_late_parsing_for_member
1948 (cp_parser *, tree);
1949 static void cp_parser_late_parsing_default_args
1950 (cp_parser *, tree);
1951 static tree cp_parser_sizeof_operand
1952 (cp_parser *, enum rid);
1953 static tree cp_parser_trait_expr
1954 (cp_parser *, enum rid);
1955 static bool cp_parser_declares_only_class_p
1957 static void cp_parser_set_storage_class
1958 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1959 static void cp_parser_set_decl_spec_type
1960 (cp_decl_specifier_seq *, tree, location_t, bool);
1961 static bool cp_parser_friend_p
1962 (const cp_decl_specifier_seq *);
1963 static cp_token *cp_parser_require
1964 (cp_parser *, enum cpp_ttype, const char *);
1965 static cp_token *cp_parser_require_keyword
1966 (cp_parser *, enum rid, const char *);
1967 static bool cp_parser_token_starts_function_definition_p
1969 static bool cp_parser_next_token_starts_class_definition_p
1971 static bool cp_parser_next_token_ends_template_argument_p
1973 static bool cp_parser_nth_token_starts_template_argument_list_p
1974 (cp_parser *, size_t);
1975 static enum tag_types cp_parser_token_is_class_key
1977 static void cp_parser_check_class_key
1978 (enum tag_types, tree type);
1979 static void cp_parser_check_access_in_redeclaration
1980 (tree type, location_t location);
1981 static bool cp_parser_optional_template_keyword
1983 static void cp_parser_pre_parsed_nested_name_specifier
1985 static bool cp_parser_cache_group
1986 (cp_parser *, enum cpp_ttype, unsigned);
1987 static void cp_parser_parse_tentatively
1989 static void cp_parser_commit_to_tentative_parse
1991 static void cp_parser_abort_tentative_parse
1993 static bool cp_parser_parse_definitely
1995 static inline bool cp_parser_parsing_tentatively
1997 static bool cp_parser_uncommitted_to_tentative_parse_p
1999 static void cp_parser_error
2000 (cp_parser *, const char *);
2001 static void cp_parser_name_lookup_error
2002 (cp_parser *, tree, tree, const char *, location_t);
2003 static bool cp_parser_simulate_error
2005 static bool cp_parser_check_type_definition
2007 static void cp_parser_check_for_definition_in_return_type
2008 (cp_declarator *, tree, location_t type_location);
2009 static void cp_parser_check_for_invalid_template_id
2010 (cp_parser *, tree, location_t location);
2011 static bool cp_parser_non_integral_constant_expression
2012 (cp_parser *, const char *);
2013 static void cp_parser_diagnose_invalid_type_name
2014 (cp_parser *, tree, tree, location_t);
2015 static bool cp_parser_parse_and_diagnose_invalid_type_name
2017 static int cp_parser_skip_to_closing_parenthesis
2018 (cp_parser *, bool, bool, bool);
2019 static void cp_parser_skip_to_end_of_statement
2021 static void cp_parser_consume_semicolon_at_end_of_statement
2023 static void cp_parser_skip_to_end_of_block_or_statement
2025 static bool cp_parser_skip_to_closing_brace
2027 static void cp_parser_skip_to_end_of_template_parameter_list
2029 static void cp_parser_skip_to_pragma_eol
2030 (cp_parser*, cp_token *);
2031 static bool cp_parser_error_occurred
2033 static bool cp_parser_allow_gnu_extensions_p
2035 static bool cp_parser_is_string_literal
2037 static bool cp_parser_is_keyword
2038 (cp_token *, enum rid);
2039 static tree cp_parser_make_typename_type
2040 (cp_parser *, tree, tree, location_t location);
2041 static cp_declarator * cp_parser_make_indirect_declarator
2042 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2044 /* Returns nonzero if we are parsing tentatively. */
2047 cp_parser_parsing_tentatively (cp_parser* parser)
2049 return parser->context->next != NULL;
2052 /* Returns nonzero if TOKEN is a string literal. */
2055 cp_parser_is_string_literal (cp_token* token)
2057 return (token->type == CPP_STRING ||
2058 token->type == CPP_STRING16 ||
2059 token->type == CPP_STRING32 ||
2060 token->type == CPP_WSTRING);
2063 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2066 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2068 return token->keyword == keyword;
2071 /* If not parsing tentatively, issue a diagnostic of the form
2072 FILE:LINE: MESSAGE before TOKEN
2073 where TOKEN is the next token in the input stream. MESSAGE
2074 (specified by the caller) is usually of the form "expected
2078 cp_parser_error (cp_parser* parser, const char* message)
2080 if (!cp_parser_simulate_error (parser))
2082 cp_token *token = cp_lexer_peek_token (parser->lexer);
2083 /* This diagnostic makes more sense if it is tagged to the line
2084 of the token we just peeked at. */
2085 cp_lexer_set_source_position_from_token (token);
2087 if (token->type == CPP_PRAGMA)
2089 error ("%H%<#pragma%> is not allowed here", &token->location);
2090 cp_parser_skip_to_pragma_eol (parser, token);
2094 c_parse_error (message,
2095 /* Because c_parser_error does not understand
2096 CPP_KEYWORD, keywords are treated like
2098 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2103 /* Issue an error about name-lookup failing. NAME is the
2104 IDENTIFIER_NODE DECL is the result of
2105 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2106 the thing that we hoped to find. */
2109 cp_parser_name_lookup_error (cp_parser* parser,
2112 const char* desired,
2113 location_t location)
2115 /* If name lookup completely failed, tell the user that NAME was not
2117 if (decl == error_mark_node)
2119 if (parser->scope && parser->scope != global_namespace)
2120 error ("%H%<%E::%E%> has not been declared",
2121 &location, parser->scope, name);
2122 else if (parser->scope == global_namespace)
2123 error ("%H%<::%E%> has not been declared", &location, name);
2124 else if (parser->object_scope
2125 && !CLASS_TYPE_P (parser->object_scope))
2126 error ("%Hrequest for member %qE in non-class type %qT",
2127 &location, name, parser->object_scope);
2128 else if (parser->object_scope)
2129 error ("%H%<%T::%E%> has not been declared",
2130 &location, parser->object_scope, name);
2132 error ("%H%qE has not been declared", &location, name);
2134 else if (parser->scope && parser->scope != global_namespace)
2135 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2136 else if (parser->scope == global_namespace)
2137 error ("%H%<::%E%> %s", &location, name, desired);
2139 error ("%H%qE %s", &location, name, desired);
2142 /* If we are parsing tentatively, remember that an error has occurred
2143 during this tentative parse. Returns true if the error was
2144 simulated; false if a message should be issued by the caller. */
2147 cp_parser_simulate_error (cp_parser* parser)
2149 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2151 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2157 /* Check for repeated decl-specifiers. */
2160 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2161 location_t location)
2165 for (ds = ds_first; ds != ds_last; ++ds)
2167 unsigned count = decl_specs->specs[(int)ds];
2170 /* The "long" specifier is a special case because of "long long". */
2174 error ("%H%<long long long%> is too long for GCC", &location);
2175 else if (pedantic && !in_system_header && warn_long_long
2176 && cxx_dialect == cxx98)
2177 pedwarn (location, OPT_Wlong_long,
2178 "ISO C++ 1998 does not support %<long long%>");
2182 static const char *const decl_spec_names[] = {
2198 error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2203 /* This function is called when a type is defined. If type
2204 definitions are forbidden at this point, an error message is
2208 cp_parser_check_type_definition (cp_parser* parser)
2210 /* If types are forbidden here, issue a message. */
2211 if (parser->type_definition_forbidden_message)
2213 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2214 in the message need to be interpreted. */
2215 error (parser->type_definition_forbidden_message);
2221 /* This function is called when the DECLARATOR is processed. The TYPE
2222 was a type defined in the decl-specifiers. If it is invalid to
2223 define a type in the decl-specifiers for DECLARATOR, an error is
2224 issued. TYPE_LOCATION is the location of TYPE and is used
2225 for error reporting. */
2228 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2229 tree type, location_t type_location)
2231 /* [dcl.fct] forbids type definitions in return types.
2232 Unfortunately, it's not easy to know whether or not we are
2233 processing a return type until after the fact. */
2235 && (declarator->kind == cdk_pointer
2236 || declarator->kind == cdk_reference
2237 || declarator->kind == cdk_ptrmem))
2238 declarator = declarator->declarator;
2240 && declarator->kind == cdk_function)
2242 error ("%Hnew types may not be defined in a return type", &type_location);
2243 inform (type_location,
2244 "(perhaps a semicolon is missing after the definition of %qT)",
2249 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2250 "<" in any valid C++ program. If the next token is indeed "<",
2251 issue a message warning the user about what appears to be an
2252 invalid attempt to form a template-id. LOCATION is the location
2253 of the type-specifier (TYPE) */
2256 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2257 tree type, location_t location)
2259 cp_token_position start = 0;
2261 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2264 error ("%H%qT is not a template", &location, type);
2265 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2266 error ("%H%qE is not a template", &location, type);
2268 error ("%Hinvalid template-id", &location);
2269 /* Remember the location of the invalid "<". */
2270 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2271 start = cp_lexer_token_position (parser->lexer, true);
2272 /* Consume the "<". */
2273 cp_lexer_consume_token (parser->lexer);
2274 /* Parse the template arguments. */
2275 cp_parser_enclosed_template_argument_list (parser);
2276 /* Permanently remove the invalid template arguments so that
2277 this error message is not issued again. */
2279 cp_lexer_purge_tokens_after (parser->lexer, start);
2283 /* If parsing an integral constant-expression, issue an error message
2284 about the fact that THING appeared and return true. Otherwise,
2285 return false. In either case, set
2286 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2289 cp_parser_non_integral_constant_expression (cp_parser *parser,
2292 parser->non_integral_constant_expression_p = true;
2293 if (parser->integral_constant_expression_p)
2295 if (!parser->allow_non_integral_constant_expression_p)
2297 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2298 in the message need to be interpreted. */
2299 char *message = concat (thing,
2300 " cannot appear in a constant-expression",
2310 /* Emit a diagnostic for an invalid type name. SCOPE is the
2311 qualifying scope (or NULL, if none) for ID. This function commits
2312 to the current active tentative parse, if any. (Otherwise, the
2313 problematic construct might be encountered again later, resulting
2314 in duplicate error messages.) LOCATION is the location of ID. */
2317 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2318 tree scope, tree id,
2319 location_t location)
2321 tree decl, old_scope;
2322 /* Try to lookup the identifier. */
2323 old_scope = parser->scope;
2324 parser->scope = scope;
2325 decl = cp_parser_lookup_name_simple (parser, id, location);
2326 parser->scope = old_scope;
2327 /* If the lookup found a template-name, it means that the user forgot
2328 to specify an argument list. Emit a useful error message. */
2329 if (TREE_CODE (decl) == TEMPLATE_DECL)
2330 error ("%Hinvalid use of template-name %qE without an argument list",
2332 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2333 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2334 else if (TREE_CODE (decl) == TYPE_DECL)
2335 /* Something like 'unsigned A a;' */
2336 error ("%Hinvalid combination of multiple type-specifiers",
2338 else if (!parser->scope)
2340 /* Issue an error message. */
2341 error ("%H%qE does not name a type", &location, id);
2342 /* If we're in a template class, it's possible that the user was
2343 referring to a type from a base class. For example:
2345 template <typename T> struct A { typedef T X; };
2346 template <typename T> struct B : public A<T> { X x; };
2348 The user should have said "typename A<T>::X". */
2349 if (processing_template_decl && current_class_type
2350 && TYPE_BINFO (current_class_type))
2354 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2358 tree base_type = BINFO_TYPE (b);
2359 if (CLASS_TYPE_P (base_type)
2360 && dependent_type_p (base_type))
2363 /* Go from a particular instantiation of the
2364 template (which will have an empty TYPE_FIELDs),
2365 to the main version. */
2366 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2367 for (field = TYPE_FIELDS (base_type);
2369 field = TREE_CHAIN (field))
2370 if (TREE_CODE (field) == TYPE_DECL
2371 && DECL_NAME (field) == id)
2374 "(perhaps %<typename %T::%E%> was intended)",
2375 BINFO_TYPE (b), id);
2384 /* Here we diagnose qualified-ids where the scope is actually correct,
2385 but the identifier does not resolve to a valid type name. */
2386 else if (parser->scope != error_mark_node)
2388 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2389 error ("%H%qE in namespace %qE does not name a type",
2390 &location, id, parser->scope);
2391 else if (TYPE_P (parser->scope))
2392 error ("%H%qE in class %qT does not name a type",
2393 &location, id, parser->scope);
2397 cp_parser_commit_to_tentative_parse (parser);
2400 /* Check for a common situation where a type-name should be present,
2401 but is not, and issue a sensible error message. Returns true if an
2402 invalid type-name was detected.
2404 The situation handled by this function are variable declarations of the
2405 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2406 Usually, `ID' should name a type, but if we got here it means that it
2407 does not. We try to emit the best possible error message depending on
2408 how exactly the id-expression looks like. */
2411 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2414 cp_token *token = cp_lexer_peek_token (parser->lexer);
2416 cp_parser_parse_tentatively (parser);
2417 id = cp_parser_id_expression (parser,
2418 /*template_keyword_p=*/false,
2419 /*check_dependency_p=*/true,
2420 /*template_p=*/NULL,
2421 /*declarator_p=*/true,
2422 /*optional_p=*/false);
2423 /* After the id-expression, there should be a plain identifier,
2424 otherwise this is not a simple variable declaration. Also, if
2425 the scope is dependent, we cannot do much. */
2426 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2427 || (parser->scope && TYPE_P (parser->scope)
2428 && dependent_type_p (parser->scope))
2429 || TREE_CODE (id) == TYPE_DECL)
2431 cp_parser_abort_tentative_parse (parser);
2434 if (!cp_parser_parse_definitely (parser))
2437 /* Emit a diagnostic for the invalid type. */
2438 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2439 id, token->location);
2440 /* Skip to the end of the declaration; there's no point in
2441 trying to process it. */
2442 cp_parser_skip_to_end_of_block_or_statement (parser);
2446 /* Consume tokens up to, and including, the next non-nested closing `)'.
2447 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2448 are doing error recovery. Returns -1 if OR_COMMA is true and we
2449 found an unnested comma. */
2452 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2457 unsigned paren_depth = 0;
2458 unsigned brace_depth = 0;
2460 if (recovering && !or_comma
2461 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2466 cp_token * token = cp_lexer_peek_token (parser->lexer);
2468 switch (token->type)
2471 case CPP_PRAGMA_EOL:
2472 /* If we've run out of tokens, then there is no closing `)'. */
2476 /* This matches the processing in skip_to_end_of_statement. */
2481 case CPP_OPEN_BRACE:
2484 case CPP_CLOSE_BRACE:
2490 if (recovering && or_comma && !brace_depth && !paren_depth)
2494 case CPP_OPEN_PAREN:
2499 case CPP_CLOSE_PAREN:
2500 if (!brace_depth && !paren_depth--)
2503 cp_lexer_consume_token (parser->lexer);
2512 /* Consume the token. */
2513 cp_lexer_consume_token (parser->lexer);
2517 /* Consume tokens until we reach the end of the current statement.
2518 Normally, that will be just before consuming a `;'. However, if a
2519 non-nested `}' comes first, then we stop before consuming that. */
2522 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2524 unsigned nesting_depth = 0;
2528 cp_token *token = cp_lexer_peek_token (parser->lexer);
2530 switch (token->type)
2533 case CPP_PRAGMA_EOL:
2534 /* If we've run out of tokens, stop. */
2538 /* If the next token is a `;', we have reached the end of the
2544 case CPP_CLOSE_BRACE:
2545 /* If this is a non-nested '}', stop before consuming it.
2546 That way, when confronted with something like:
2550 we stop before consuming the closing '}', even though we
2551 have not yet reached a `;'. */
2552 if (nesting_depth == 0)
2555 /* If it is the closing '}' for a block that we have
2556 scanned, stop -- but only after consuming the token.
2562 we will stop after the body of the erroneously declared
2563 function, but before consuming the following `typedef'
2565 if (--nesting_depth == 0)
2567 cp_lexer_consume_token (parser->lexer);
2571 case CPP_OPEN_BRACE:
2579 /* Consume the token. */
2580 cp_lexer_consume_token (parser->lexer);
2584 /* This function is called at the end of a statement or declaration.
2585 If the next token is a semicolon, it is consumed; otherwise, error
2586 recovery is attempted. */
2589 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2591 /* Look for the trailing `;'. */
2592 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2594 /* If there is additional (erroneous) input, skip to the end of
2596 cp_parser_skip_to_end_of_statement (parser);
2597 /* If the next token is now a `;', consume it. */
2598 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2599 cp_lexer_consume_token (parser->lexer);
2603 /* Skip tokens until we have consumed an entire block, or until we
2604 have consumed a non-nested `;'. */
2607 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2609 int nesting_depth = 0;
2611 while (nesting_depth >= 0)
2613 cp_token *token = cp_lexer_peek_token (parser->lexer);
2615 switch (token->type)
2618 case CPP_PRAGMA_EOL:
2619 /* If we've run out of tokens, stop. */
2623 /* Stop if this is an unnested ';'. */
2628 case CPP_CLOSE_BRACE:
2629 /* Stop if this is an unnested '}', or closes the outermost
2632 if (nesting_depth < 0)
2638 case CPP_OPEN_BRACE:
2647 /* Consume the token. */
2648 cp_lexer_consume_token (parser->lexer);
2652 /* Skip tokens until a non-nested closing curly brace is the next
2653 token, or there are no more tokens. Return true in the first case,
2657 cp_parser_skip_to_closing_brace (cp_parser *parser)
2659 unsigned nesting_depth = 0;
2663 cp_token *token = cp_lexer_peek_token (parser->lexer);
2665 switch (token->type)
2668 case CPP_PRAGMA_EOL:
2669 /* If we've run out of tokens, stop. */
2672 case CPP_CLOSE_BRACE:
2673 /* If the next token is a non-nested `}', then we have reached
2674 the end of the current block. */
2675 if (nesting_depth-- == 0)
2679 case CPP_OPEN_BRACE:
2680 /* If it the next token is a `{', then we are entering a new
2681 block. Consume the entire block. */
2689 /* Consume the token. */
2690 cp_lexer_consume_token (parser->lexer);
2694 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2695 parameter is the PRAGMA token, allowing us to purge the entire pragma
2699 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2703 parser->lexer->in_pragma = false;
2706 token = cp_lexer_consume_token (parser->lexer);
2707 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2709 /* Ensure that the pragma is not parsed again. */
2710 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2713 /* Require pragma end of line, resyncing with it as necessary. The
2714 arguments are as for cp_parser_skip_to_pragma_eol. */
2717 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2719 parser->lexer->in_pragma = false;
2720 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2721 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2724 /* This is a simple wrapper around make_typename_type. When the id is
2725 an unresolved identifier node, we can provide a superior diagnostic
2726 using cp_parser_diagnose_invalid_type_name. */
2729 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2730 tree id, location_t id_location)
2733 if (TREE_CODE (id) == IDENTIFIER_NODE)
2735 result = make_typename_type (scope, id, typename_type,
2736 /*complain=*/tf_none);
2737 if (result == error_mark_node)
2738 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2741 return make_typename_type (scope, id, typename_type, tf_error);
2744 /* This is a wrapper around the
2745 make_{pointer,ptrmem,reference}_declarator functions that decides
2746 which one to call based on the CODE and CLASS_TYPE arguments. The
2747 CODE argument should be one of the values returned by
2748 cp_parser_ptr_operator. */
2749 static cp_declarator *
2750 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2751 cp_cv_quals cv_qualifiers,
2752 cp_declarator *target)
2754 if (code == ERROR_MARK)
2755 return cp_error_declarator;
2757 if (code == INDIRECT_REF)
2758 if (class_type == NULL_TREE)
2759 return make_pointer_declarator (cv_qualifiers, target);
2761 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2762 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2763 return make_reference_declarator (cv_qualifiers, target, false);
2764 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2765 return make_reference_declarator (cv_qualifiers, target, true);
2769 /* Create a new C++ parser. */
2772 cp_parser_new (void)
2778 /* cp_lexer_new_main is called before calling ggc_alloc because
2779 cp_lexer_new_main might load a PCH file. */
2780 lexer = cp_lexer_new_main ();
2782 /* Initialize the binops_by_token so that we can get the tree
2783 directly from the token. */
2784 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2785 binops_by_token[binops[i].token_type] = binops[i];
2787 parser = GGC_CNEW (cp_parser);
2788 parser->lexer = lexer;
2789 parser->context = cp_parser_context_new (NULL);
2791 /* For now, we always accept GNU extensions. */
2792 parser->allow_gnu_extensions_p = 1;
2794 /* The `>' token is a greater-than operator, not the end of a
2796 parser->greater_than_is_operator_p = true;
2798 parser->default_arg_ok_p = true;
2800 /* We are not parsing a constant-expression. */
2801 parser->integral_constant_expression_p = false;
2802 parser->allow_non_integral_constant_expression_p = false;
2803 parser->non_integral_constant_expression_p = false;
2805 /* Local variable names are not forbidden. */
2806 parser->local_variables_forbidden_p = false;
2808 /* We are not processing an `extern "C"' declaration. */
2809 parser->in_unbraced_linkage_specification_p = false;
2811 /* We are not processing a declarator. */
2812 parser->in_declarator_p = false;
2814 /* We are not processing a template-argument-list. */
2815 parser->in_template_argument_list_p = false;
2817 /* We are not in an iteration statement. */
2818 parser->in_statement = 0;
2820 /* We are not in a switch statement. */
2821 parser->in_switch_statement_p = false;
2823 /* We are not parsing a type-id inside an expression. */
2824 parser->in_type_id_in_expr_p = false;
2826 /* Declarations aren't implicitly extern "C". */
2827 parser->implicit_extern_c = false;
2829 /* String literals should be translated to the execution character set. */
2830 parser->translate_strings_p = true;
2832 /* We are not parsing a function body. */
2833 parser->in_function_body = false;
2835 /* The unparsed function queue is empty. */
2836 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2838 /* There are no classes being defined. */
2839 parser->num_classes_being_defined = 0;
2841 /* No template parameters apply. */
2842 parser->num_template_parameter_lists = 0;
2847 /* Create a cp_lexer structure which will emit the tokens in CACHE
2848 and push it onto the parser's lexer stack. This is used for delayed
2849 parsing of in-class method bodies and default arguments, and should
2850 not be confused with tentative parsing. */
2852 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2854 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2855 lexer->next = parser->lexer;
2856 parser->lexer = lexer;
2858 /* Move the current source position to that of the first token in the
2860 cp_lexer_set_source_position_from_token (lexer->next_token);
2863 /* Pop the top lexer off the parser stack. This is never used for the
2864 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2866 cp_parser_pop_lexer (cp_parser *parser)
2868 cp_lexer *lexer = parser->lexer;
2869 parser->lexer = lexer->next;
2870 cp_lexer_destroy (lexer);
2872 /* Put the current source position back where it was before this
2873 lexer was pushed. */
2874 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2877 /* Lexical conventions [gram.lex] */
2879 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2883 cp_parser_identifier (cp_parser* parser)
2887 /* Look for the identifier. */
2888 token = cp_parser_require (parser, CPP_NAME, "identifier");
2889 /* Return the value. */
2890 return token ? token->u.value : error_mark_node;
2893 /* Parse a sequence of adjacent string constants. Returns a
2894 TREE_STRING representing the combined, nul-terminated string
2895 constant. If TRANSLATE is true, translate the string to the
2896 execution character set. If WIDE_OK is true, a wide string is
2899 C++98 [lex.string] says that if a narrow string literal token is
2900 adjacent to a wide string literal token, the behavior is undefined.
2901 However, C99 6.4.5p4 says that this results in a wide string literal.
2902 We follow C99 here, for consistency with the C front end.
2904 This code is largely lifted from lex_string() in c-lex.c.
2906 FUTURE: ObjC++ will need to handle @-strings here. */
2908 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2912 struct obstack str_ob;
2913 cpp_string str, istr, *strs;
2915 enum cpp_ttype type;
2917 tok = cp_lexer_peek_token (parser->lexer);
2918 if (!cp_parser_is_string_literal (tok))
2920 cp_parser_error (parser, "expected string-literal");
2921 return error_mark_node;
2926 /* Try to avoid the overhead of creating and destroying an obstack
2927 for the common case of just one string. */
2928 if (!cp_parser_is_string_literal
2929 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2931 cp_lexer_consume_token (parser->lexer);
2933 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2934 str.len = TREE_STRING_LENGTH (tok->u.value);
2941 gcc_obstack_init (&str_ob);
2946 cp_lexer_consume_token (parser->lexer);
2948 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2949 str.len = TREE_STRING_LENGTH (tok->u.value);
2951 if (type != tok->type)
2953 if (type == CPP_STRING)
2955 else if (tok->type != CPP_STRING)
2956 error ("%Hunsupported non-standard concatenation "
2957 "of string literals", &tok->location);
2960 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2962 tok = cp_lexer_peek_token (parser->lexer);
2964 while (cp_parser_is_string_literal (tok));
2966 strs = (cpp_string *) obstack_finish (&str_ob);
2969 if (type != CPP_STRING && !wide_ok)
2971 cp_parser_error (parser, "a wide string is invalid in this context");
2975 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2976 (parse_in, strs, count, &istr, type))
2978 value = build_string (istr.len, (const char *)istr.text);
2979 free (CONST_CAST (unsigned char *, istr.text));
2985 TREE_TYPE (value) = char_array_type_node;
2988 TREE_TYPE (value) = char16_array_type_node;
2991 TREE_TYPE (value) = char32_array_type_node;
2994 TREE_TYPE (value) = wchar_array_type_node;
2998 value = fix_string_type (value);
3001 /* cpp_interpret_string has issued an error. */
3002 value = error_mark_node;
3005 obstack_free (&str_ob, 0);
3011 /* Basic concepts [gram.basic] */
3013 /* Parse a translation-unit.
3016 declaration-seq [opt]
3018 Returns TRUE if all went well. */
3021 cp_parser_translation_unit (cp_parser* parser)
3023 /* The address of the first non-permanent object on the declarator
3025 static void *declarator_obstack_base;
3029 /* Create the declarator obstack, if necessary. */
3030 if (!cp_error_declarator)
3032 gcc_obstack_init (&declarator_obstack);
3033 /* Create the error declarator. */
3034 cp_error_declarator = make_declarator (cdk_error);
3035 /* Create the empty parameter list. */
3036 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3037 /* Remember where the base of the declarator obstack lies. */
3038 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3041 cp_parser_declaration_seq_opt (parser);
3043 /* If there are no tokens left then all went well. */
3044 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3046 /* Get rid of the token array; we don't need it any more. */
3047 cp_lexer_destroy (parser->lexer);
3048 parser->lexer = NULL;
3050 /* This file might have been a context that's implicitly extern
3051 "C". If so, pop the lang context. (Only relevant for PCH.) */
3052 if (parser->implicit_extern_c)
3054 pop_lang_context ();
3055 parser->implicit_extern_c = false;
3059 finish_translation_unit ();
3065 cp_parser_error (parser, "expected declaration");
3069 /* Make sure the declarator obstack was fully cleaned up. */
3070 gcc_assert (obstack_next_free (&declarator_obstack)
3071 == declarator_obstack_base);
3073 /* All went well. */
3077 /* Expressions [gram.expr] */
3079 /* Parse a primary-expression.
3090 ( compound-statement )
3091 __builtin_va_arg ( assignment-expression , type-id )
3092 __builtin_offsetof ( type-id , offsetof-expression )
3095 __has_nothrow_assign ( type-id )
3096 __has_nothrow_constructor ( type-id )
3097 __has_nothrow_copy ( type-id )
3098 __has_trivial_assign ( type-id )
3099 __has_trivial_constructor ( type-id )
3100 __has_trivial_copy ( type-id )
3101 __has_trivial_destructor ( type-id )
3102 __has_virtual_destructor ( type-id )
3103 __is_abstract ( type-id )
3104 __is_base_of ( type-id , type-id )
3105 __is_class ( type-id )
3106 __is_convertible_to ( type-id , type-id )
3107 __is_empty ( type-id )
3108 __is_enum ( type-id )
3109 __is_pod ( type-id )
3110 __is_polymorphic ( type-id )
3111 __is_union ( type-id )
3113 Objective-C++ Extension:
3121 ADDRESS_P is true iff this expression was immediately preceded by
3122 "&" and therefore might denote a pointer-to-member. CAST_P is true
3123 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3124 true iff this expression is a template argument.
3126 Returns a representation of the expression. Upon return, *IDK
3127 indicates what kind of id-expression (if any) was present. */
3130 cp_parser_primary_expression (cp_parser *parser,
3133 bool template_arg_p,
3136 cp_token *token = NULL;
3138 /* Assume the primary expression is not an id-expression. */
3139 *idk = CP_ID_KIND_NONE;
3141 /* Peek at the next token. */
3142 token = cp_lexer_peek_token (parser->lexer);
3143 switch (token->type)
3156 token = cp_lexer_consume_token (parser->lexer);
3157 if (TREE_CODE (token->u.value) == FIXED_CST)
3159 error ("%Hfixed-point types not supported in C++",
3161 return error_mark_node;
3163 /* Floating-point literals are only allowed in an integral
3164 constant expression if they are cast to an integral or
3165 enumeration type. */
3166 if (TREE_CODE (token->u.value) == REAL_CST
3167 && parser->integral_constant_expression_p
3170 /* CAST_P will be set even in invalid code like "int(2.7 +
3171 ...)". Therefore, we have to check that the next token
3172 is sure to end the cast. */
3175 cp_token *next_token;
3177 next_token = cp_lexer_peek_token (parser->lexer);
3178 if (/* The comma at the end of an
3179 enumerator-definition. */
3180 next_token->type != CPP_COMMA
3181 /* The curly brace at the end of an enum-specifier. */
3182 && next_token->type != CPP_CLOSE_BRACE
3183 /* The end of a statement. */
3184 && next_token->type != CPP_SEMICOLON
3185 /* The end of the cast-expression. */
3186 && next_token->type != CPP_CLOSE_PAREN
3187 /* The end of an array bound. */
3188 && next_token->type != CPP_CLOSE_SQUARE
3189 /* The closing ">" in a template-argument-list. */
3190 && (next_token->type != CPP_GREATER
3191 || parser->greater_than_is_operator_p)
3192 /* C++0x only: A ">>" treated like two ">" tokens,
3193 in a template-argument-list. */
3194 && (next_token->type != CPP_RSHIFT
3195 || (cxx_dialect == cxx98)
3196 || parser->greater_than_is_operator_p))
3200 /* If we are within a cast, then the constraint that the
3201 cast is to an integral or enumeration type will be
3202 checked at that point. If we are not within a cast, then
3203 this code is invalid. */
3205 cp_parser_non_integral_constant_expression
3206 (parser, "floating-point literal");
3208 return token->u.value;
3214 /* ??? Should wide strings be allowed when parser->translate_strings_p
3215 is false (i.e. in attributes)? If not, we can kill the third
3216 argument to cp_parser_string_literal. */
3217 return cp_parser_string_literal (parser,
3218 parser->translate_strings_p,
3221 case CPP_OPEN_PAREN:
3224 bool saved_greater_than_is_operator_p;
3226 /* Consume the `('. */
3227 cp_lexer_consume_token (parser->lexer);
3228 /* Within a parenthesized expression, a `>' token is always
3229 the greater-than operator. */
3230 saved_greater_than_is_operator_p
3231 = parser->greater_than_is_operator_p;
3232 parser->greater_than_is_operator_p = true;
3233 /* If we see `( { ' then we are looking at the beginning of
3234 a GNU statement-expression. */
3235 if (cp_parser_allow_gnu_extensions_p (parser)
3236 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3238 /* Statement-expressions are not allowed by the standard. */
3239 pedwarn (token->location, OPT_pedantic,
3240 "ISO C++ forbids braced-groups within expressions");
3242 /* And they're not allowed outside of a function-body; you
3243 cannot, for example, write:
3245 int i = ({ int j = 3; j + 1; });
3247 at class or namespace scope. */
3248 if (!parser->in_function_body
3249 || parser->in_template_argument_list_p)
3251 error ("%Hstatement-expressions are not allowed outside "
3252 "functions nor in template-argument lists",
3254 cp_parser_skip_to_end_of_block_or_statement (parser);
3255 expr = error_mark_node;
3259 /* Start the statement-expression. */
3260 expr = begin_stmt_expr ();
3261 /* Parse the compound-statement. */
3262 cp_parser_compound_statement (parser, expr, false);
3264 expr = finish_stmt_expr (expr, false);
3269 /* Parse the parenthesized expression. */
3270 expr = cp_parser_expression (parser, cast_p, idk);
3271 /* Let the front end know that this expression was
3272 enclosed in parentheses. This matters in case, for
3273 example, the expression is of the form `A::B', since
3274 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3276 finish_parenthesized_expr (expr);
3278 /* The `>' token might be the end of a template-id or
3279 template-parameter-list now. */
3280 parser->greater_than_is_operator_p
3281 = saved_greater_than_is_operator_p;
3282 /* Consume the `)'. */
3283 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3284 cp_parser_skip_to_end_of_statement (parser);
3290 switch (token->keyword)
3292 /* These two are the boolean literals. */
3294 cp_lexer_consume_token (parser->lexer);
3295 return boolean_true_node;
3297 cp_lexer_consume_token (parser->lexer);
3298 return boolean_false_node;
3300 /* The `__null' literal. */
3302 cp_lexer_consume_token (parser->lexer);
3305 /* Recognize the `this' keyword. */
3307 cp_lexer_consume_token (parser->lexer);
3308 if (parser->local_variables_forbidden_p)
3310 error ("%H%<this%> may not be used in this context",
3312 return error_mark_node;
3314 /* Pointers cannot appear in constant-expressions. */
3315 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3316 return error_mark_node;
3317 return finish_this_expr ();
3319 /* The `operator' keyword can be the beginning of an
3324 case RID_FUNCTION_NAME:
3325 case RID_PRETTY_FUNCTION_NAME:
3326 case RID_C99_FUNCTION_NAME:
3330 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3331 __func__ are the names of variables -- but they are
3332 treated specially. Therefore, they are handled here,
3333 rather than relying on the generic id-expression logic
3334 below. Grammatically, these names are id-expressions.
3336 Consume the token. */
3337 token = cp_lexer_consume_token (parser->lexer);
3339 switch (token->keyword)
3341 case RID_FUNCTION_NAME:
3342 name = "%<__FUNCTION__%>";
3344 case RID_PRETTY_FUNCTION_NAME:
3345 name = "%<__PRETTY_FUNCTION__%>";
3347 case RID_C99_FUNCTION_NAME:
3348 name = "%<__func__%>";
3354 if (cp_parser_non_integral_constant_expression (parser, name))
3355 return error_mark_node;
3357 /* Look up the name. */
3358 return finish_fname (token->u.value);
3366 /* The `__builtin_va_arg' construct is used to handle
3367 `va_arg'. Consume the `__builtin_va_arg' token. */
3368 cp_lexer_consume_token (parser->lexer);
3369 /* Look for the opening `('. */
3370 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3371 /* Now, parse the assignment-expression. */
3372 expression = cp_parser_assignment_expression (parser,
3373 /*cast_p=*/false, NULL);
3374 /* Look for the `,'. */
3375 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3376 /* Parse the type-id. */
3377 type = cp_parser_type_id (parser);
3378 /* Look for the closing `)'. */
3379 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3380 /* Using `va_arg' in a constant-expression is not
3382 if (cp_parser_non_integral_constant_expression (parser,
3384 return error_mark_node;
3385 return build_x_va_arg (expression, type);
3389 return cp_parser_builtin_offsetof (parser);
3391 case RID_HAS_NOTHROW_ASSIGN:
3392 case RID_HAS_NOTHROW_CONSTRUCTOR:
3393 case RID_HAS_NOTHROW_COPY:
3394 case RID_HAS_TRIVIAL_ASSIGN:
3395 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3396 case RID_HAS_TRIVIAL_COPY:
3397 case RID_HAS_TRIVIAL_DESTRUCTOR:
3398 case RID_HAS_VIRTUAL_DESTRUCTOR:
3399 case RID_IS_ABSTRACT:
3400 case RID_IS_BASE_OF:
3402 case RID_IS_CONVERTIBLE_TO:
3406 case RID_IS_POLYMORPHIC:
3408 return cp_parser_trait_expr (parser, token->keyword);
3410 /* Objective-C++ expressions. */
3412 case RID_AT_PROTOCOL:
3413 case RID_AT_SELECTOR:
3414 return cp_parser_objc_expression (parser);
3417 cp_parser_error (parser, "expected primary-expression");
3418 return error_mark_node;
3421 /* An id-expression can start with either an identifier, a
3422 `::' as the beginning of a qualified-id, or the "operator"
3426 case CPP_TEMPLATE_ID:
3427 case CPP_NESTED_NAME_SPECIFIER:
3431 const char *error_msg;
3434 cp_token *id_expr_token;
3437 /* Parse the id-expression. */
3439 = cp_parser_id_expression (parser,
3440 /*template_keyword_p=*/false,
3441 /*check_dependency_p=*/true,
3443 /*declarator_p=*/false,
3444 /*optional_p=*/false);
3445 if (id_expression == error_mark_node)
3446 return error_mark_node;
3447 id_expr_token = token;
3448 token = cp_lexer_peek_token (parser->lexer);
3449 done = (token->type != CPP_OPEN_SQUARE
3450 && token->type != CPP_OPEN_PAREN
3451 && token->type != CPP_DOT
3452 && token->type != CPP_DEREF
3453 && token->type != CPP_PLUS_PLUS
3454 && token->type != CPP_MINUS_MINUS);
3455 /* If we have a template-id, then no further lookup is
3456 required. If the template-id was for a template-class, we
3457 will sometimes have a TYPE_DECL at this point. */
3458 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3459 || TREE_CODE (id_expression) == TYPE_DECL)
3460 decl = id_expression;
3461 /* Look up the name. */
3464 tree ambiguous_decls;
3466 decl = cp_parser_lookup_name (parser, id_expression,
3469 /*is_namespace=*/false,
3470 /*check_dependency=*/true,
3472 id_expr_token->location);
3473 /* If the lookup was ambiguous, an error will already have
3475 if (ambiguous_decls)
3476 return error_mark_node;
3478 /* In Objective-C++, an instance variable (ivar) may be preferred
3479 to whatever cp_parser_lookup_name() found. */
3480 decl = objc_lookup_ivar (decl, id_expression);
3482 /* If name lookup gives us a SCOPE_REF, then the
3483 qualifying scope was dependent. */
3484 if (TREE_CODE (decl) == SCOPE_REF)
3486 /* At this point, we do not know if DECL is a valid
3487 integral constant expression. We assume that it is
3488 in fact such an expression, so that code like:
3490 template <int N> struct A {
3494 is accepted. At template-instantiation time, we
3495 will check that B<N>::i is actually a constant. */
3498 /* Check to see if DECL is a local variable in a context
3499 where that is forbidden. */
3500 if (parser->local_variables_forbidden_p
3501 && local_variable_p (decl))
3503 /* It might be that we only found DECL because we are
3504 trying to be generous with pre-ISO scoping rules.
3505 For example, consider:
3509 for (int i = 0; i < 10; ++i) {}
3510 extern void f(int j = i);
3513 Here, name look up will originally find the out
3514 of scope `i'. We need to issue a warning message,
3515 but then use the global `i'. */
3516 decl = check_for_out_of_scope_variable (decl);
3517 if (local_variable_p (decl))
3519 error ("%Hlocal variable %qD may not appear in this context",
3520 &id_expr_token->location, decl);
3521 return error_mark_node;
3526 decl = (finish_id_expression
3527 (id_expression, decl, parser->scope,
3529 parser->integral_constant_expression_p,
3530 parser->allow_non_integral_constant_expression_p,
3531 &parser->non_integral_constant_expression_p,
3532 template_p, done, address_p,
3535 id_expr_token->location));
3537 cp_parser_error (parser, error_msg);
3541 /* Anything else is an error. */
3543 /* ...unless we have an Objective-C++ message or string literal,
3545 if (c_dialect_objc ()
3546 && (token->type == CPP_OPEN_SQUARE
3547 || token->type == CPP_OBJC_STRING))
3548 return cp_parser_objc_expression (parser);
3550 cp_parser_error (parser, "expected primary-expression");
3551 return error_mark_node;
3555 /* Parse an id-expression.
3562 :: [opt] nested-name-specifier template [opt] unqualified-id
3564 :: operator-function-id
3567 Return a representation of the unqualified portion of the
3568 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3569 a `::' or nested-name-specifier.
3571 Often, if the id-expression was a qualified-id, the caller will
3572 want to make a SCOPE_REF to represent the qualified-id. This
3573 function does not do this in order to avoid wastefully creating
3574 SCOPE_REFs when they are not required.
3576 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3579 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3580 uninstantiated templates.
3582 If *TEMPLATE_P is non-NULL, it is set to true iff the
3583 `template' keyword is used to explicitly indicate that the entity
3584 named is a template.
3586 If DECLARATOR_P is true, the id-expression is appearing as part of
3587 a declarator, rather than as part of an expression. */
3590 cp_parser_id_expression (cp_parser *parser,
3591 bool template_keyword_p,
3592 bool check_dependency_p,
3597 bool global_scope_p;
3598 bool nested_name_specifier_p;
3600 /* Assume the `template' keyword was not used. */
3602 *template_p = template_keyword_p;
3604 /* Look for the optional `::' operator. */
3606 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3608 /* Look for the optional nested-name-specifier. */
3609 nested_name_specifier_p
3610 = (cp_parser_nested_name_specifier_opt (parser,
3611 /*typename_keyword_p=*/false,
3616 /* If there is a nested-name-specifier, then we are looking at
3617 the first qualified-id production. */
3618 if (nested_name_specifier_p)
3621 tree saved_object_scope;
3622 tree saved_qualifying_scope;
3623 tree unqualified_id;
3626 /* See if the next token is the `template' keyword. */
3628 template_p = &is_template;
3629 *template_p = cp_parser_optional_template_keyword (parser);
3630 /* Name lookup we do during the processing of the
3631 unqualified-id might obliterate SCOPE. */
3632 saved_scope = parser->scope;
3633 saved_object_scope = parser->object_scope;
3634 saved_qualifying_scope = parser->qualifying_scope;
3635 /* Process the final unqualified-id. */
3636 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3639 /*optional_p=*/false);
3640 /* Restore the SAVED_SCOPE for our caller. */
3641 parser->scope = saved_scope;
3642 parser->object_scope = saved_object_scope;
3643 parser->qualifying_scope = saved_qualifying_scope;
3645 return unqualified_id;
3647 /* Otherwise, if we are in global scope, then we are looking at one
3648 of the other qualified-id productions. */
3649 else if (global_scope_p)
3654 /* Peek at the next token. */
3655 token = cp_lexer_peek_token (parser->lexer);
3657 /* If it's an identifier, and the next token is not a "<", then
3658 we can avoid the template-id case. This is an optimization
3659 for this common case. */
3660 if (token->type == CPP_NAME
3661 && !cp_parser_nth_token_starts_template_argument_list_p
3663 return cp_parser_identifier (parser);
3665 cp_parser_parse_tentatively (parser);
3666 /* Try a template-id. */
3667 id = cp_parser_template_id (parser,
3668 /*template_keyword_p=*/false,
3669 /*check_dependency_p=*/true,
3671 /* If that worked, we're done. */
3672 if (cp_parser_parse_definitely (parser))
3675 /* Peek at the next token. (Changes in the token buffer may
3676 have invalidated the pointer obtained above.) */
3677 token = cp_lexer_peek_token (parser->lexer);
3679 switch (token->type)
3682 return cp_parser_identifier (parser);
3685 if (token->keyword == RID_OPERATOR)
3686 return cp_parser_operator_function_id (parser);
3690 cp_parser_error (parser, "expected id-expression");
3691 return error_mark_node;
3695 return cp_parser_unqualified_id (parser, template_keyword_p,
3696 /*check_dependency_p=*/true,
3701 /* Parse an unqualified-id.
3705 operator-function-id
3706 conversion-function-id
3710 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3711 keyword, in a construct like `A::template ...'.
3713 Returns a representation of unqualified-id. For the `identifier'
3714 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3715 production a BIT_NOT_EXPR is returned; the operand of the
3716 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3717 other productions, see the documentation accompanying the
3718 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3719 names are looked up in uninstantiated templates. If DECLARATOR_P
3720 is true, the unqualified-id is appearing as part of a declarator,
3721 rather than as part of an expression. */
3724 cp_parser_unqualified_id (cp_parser* parser,
3725 bool template_keyword_p,
3726 bool check_dependency_p,
3732 /* Peek at the next token. */
3733 token = cp_lexer_peek_token (parser->lexer);
3735 switch (token->type)
3741 /* We don't know yet whether or not this will be a
3743 cp_parser_parse_tentatively (parser);
3744 /* Try a template-id. */
3745 id = cp_parser_template_id (parser, template_keyword_p,
3748 /* If it worked, we're done. */
3749 if (cp_parser_parse_definitely (parser))
3751 /* Otherwise, it's an ordinary identifier. */
3752 return cp_parser_identifier (parser);
3755 case CPP_TEMPLATE_ID:
3756 return cp_parser_template_id (parser, template_keyword_p,
3763 tree qualifying_scope;
3768 /* Consume the `~' token. */
3769 cp_lexer_consume_token (parser->lexer);
3770 /* Parse the class-name. The standard, as written, seems to
3773 template <typename T> struct S { ~S (); };
3774 template <typename T> S<T>::~S() {}
3776 is invalid, since `~' must be followed by a class-name, but
3777 `S<T>' is dependent, and so not known to be a class.
3778 That's not right; we need to look in uninstantiated
3779 templates. A further complication arises from:
3781 template <typename T> void f(T t) {
3785 Here, it is not possible to look up `T' in the scope of `T'
3786 itself. We must look in both the current scope, and the
3787 scope of the containing complete expression.
3789 Yet another issue is:
3798 The standard does not seem to say that the `S' in `~S'
3799 should refer to the type `S' and not the data member
3802 /* DR 244 says that we look up the name after the "~" in the
3803 same scope as we looked up the qualifying name. That idea
3804 isn't fully worked out; it's more complicated than that. */
3805 scope = parser->scope;
3806 object_scope = parser->object_scope;
3807 qualifying_scope = parser->qualifying_scope;
3809 /* Check for invalid scopes. */
3810 if (scope == error_mark_node)
3812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813 cp_lexer_consume_token (parser->lexer);
3814 return error_mark_node;
3816 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3818 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3819 error ("%Hscope %qT before %<~%> is not a class-name",
3820 &token->location, scope);
3821 cp_parser_simulate_error (parser);
3822 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3823 cp_lexer_consume_token (parser->lexer);
3824 return error_mark_node;
3826 gcc_assert (!scope || TYPE_P (scope));
3828 /* If the name is of the form "X::~X" it's OK. */
3829 token = cp_lexer_peek_token (parser->lexer);
3831 && token->type == CPP_NAME
3832 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3834 && constructor_name_p (token->u.value, scope))
3836 cp_lexer_consume_token (parser->lexer);
3837 return build_nt (BIT_NOT_EXPR, scope);
3840 /* If there was an explicit qualification (S::~T), first look
3841 in the scope given by the qualification (i.e., S). */
3843 type_decl = NULL_TREE;
3846 cp_parser_parse_tentatively (parser);
3847 type_decl = cp_parser_class_name (parser,
3848 /*typename_keyword_p=*/false,
3849 /*template_keyword_p=*/false,
3851 /*check_dependency=*/false,
3852 /*class_head_p=*/false,
3854 if (cp_parser_parse_definitely (parser))
3857 /* In "N::S::~S", look in "N" as well. */
3858 if (!done && scope && qualifying_scope)
3860 cp_parser_parse_tentatively (parser);
3861 parser->scope = qualifying_scope;
3862 parser->object_scope = NULL_TREE;
3863 parser->qualifying_scope = NULL_TREE;
3865 = cp_parser_class_name (parser,
3866 /*typename_keyword_p=*/false,
3867 /*template_keyword_p=*/false,
3869 /*check_dependency=*/false,
3870 /*class_head_p=*/false,
3872 if (cp_parser_parse_definitely (parser))
3875 /* In "p->S::~T", look in the scope given by "*p" as well. */
3876 else if (!done && object_scope)
3878 cp_parser_parse_tentatively (parser);
3879 parser->scope = object_scope;
3880 parser->object_scope = NULL_TREE;
3881 parser->qualifying_scope = NULL_TREE;
3883 = cp_parser_class_name (parser,
3884 /*typename_keyword_p=*/false,
3885 /*template_keyword_p=*/false,
3887 /*check_dependency=*/false,
3888 /*class_head_p=*/false,
3890 if (cp_parser_parse_definitely (parser))
3893 /* Look in the surrounding context. */
3896 parser->scope = NULL_TREE;
3897 parser->object_scope = NULL_TREE;
3898 parser->qualifying_scope = NULL_TREE;
3899 if (processing_template_decl)
3900 cp_parser_parse_tentatively (parser);
3902 = cp_parser_class_name (parser,
3903 /*typename_keyword_p=*/false,
3904 /*template_keyword_p=*/false,
3906 /*check_dependency=*/false,
3907 /*class_head_p=*/false,
3909 if (processing_template_decl
3910 && ! cp_parser_parse_definitely (parser))
3912 /* We couldn't find a type with this name, so just accept
3913 it and check for a match at instantiation time. */
3914 type_decl = cp_parser_identifier (parser);
3915 if (type_decl != error_mark_node)
3916 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3920 /* If an error occurred, assume that the name of the
3921 destructor is the same as the name of the qualifying
3922 class. That allows us to keep parsing after running
3923 into ill-formed destructor names. */
3924 if (type_decl == error_mark_node && scope)
3925 return build_nt (BIT_NOT_EXPR, scope);
3926 else if (type_decl == error_mark_node)
3927 return error_mark_node;
3929 /* Check that destructor name and scope match. */
3930 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3932 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3933 error ("%Hdeclaration of %<~%T%> as member of %qT",
3934 &token->location, type_decl, scope);
3935 cp_parser_simulate_error (parser);
3936 return error_mark_node;
3941 A typedef-name that names a class shall not be used as the
3942 identifier in the declarator for a destructor declaration. */
3944 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3945 && !DECL_SELF_REFERENCE_P (type_decl)
3946 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3947 error ("%Htypedef-name %qD used as destructor declarator",
3948 &token->location, type_decl);
3950 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3954 if (token->keyword == RID_OPERATOR)
3958 /* This could be a template-id, so we try that first. */
3959 cp_parser_parse_tentatively (parser);
3960 /* Try a template-id. */
3961 id = cp_parser_template_id (parser, template_keyword_p,
3962 /*check_dependency_p=*/true,
3964 /* If that worked, we're done. */
3965 if (cp_parser_parse_definitely (parser))
3967 /* We still don't know whether we're looking at an
3968 operator-function-id or a conversion-function-id. */
3969 cp_parser_parse_tentatively (parser);
3970 /* Try an operator-function-id. */
3971 id = cp_parser_operator_function_id (parser);
3972 /* If that didn't work, try a conversion-function-id. */
3973 if (!cp_parser_parse_definitely (parser))
3974 id = cp_parser_conversion_function_id (parser);
3983 cp_parser_error (parser, "expected unqualified-id");
3984 return error_mark_node;
3988 /* Parse an (optional) nested-name-specifier.
3990 nested-name-specifier: [C++98]
3991 class-or-namespace-name :: nested-name-specifier [opt]
3992 class-or-namespace-name :: template nested-name-specifier [opt]
3994 nested-name-specifier: [C++0x]
3997 nested-name-specifier identifier ::
3998 nested-name-specifier template [opt] simple-template-id ::
4000 PARSER->SCOPE should be set appropriately before this function is
4001 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4002 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4005 Sets PARSER->SCOPE to the class (TYPE) or namespace
4006 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4007 it unchanged if there is no nested-name-specifier. Returns the new
4008 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4010 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4011 part of a declaration and/or decl-specifier. */
4014 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4015 bool typename_keyword_p,
4016 bool check_dependency_p,
4018 bool is_declaration)
4020 bool success = false;
4021 cp_token_position start = 0;
4024 /* Remember where the nested-name-specifier starts. */
4025 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4027 start = cp_lexer_token_position (parser->lexer, false);
4028 push_deferring_access_checks (dk_deferred);
4035 tree saved_qualifying_scope;
4036 bool template_keyword_p;
4038 /* Spot cases that cannot be the beginning of a
4039 nested-name-specifier. */
4040 token = cp_lexer_peek_token (parser->lexer);
4042 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4043 the already parsed nested-name-specifier. */
4044 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4046 /* Grab the nested-name-specifier and continue the loop. */
4047 cp_parser_pre_parsed_nested_name_specifier (parser);
4048 /* If we originally encountered this nested-name-specifier
4049 with IS_DECLARATION set to false, we will not have
4050 resolved TYPENAME_TYPEs, so we must do so here. */
4052 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4054 new_scope = resolve_typename_type (parser->scope,
4055 /*only_current_p=*/false);
4056 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4057 parser->scope = new_scope;
4063 /* Spot cases that cannot be the beginning of a
4064 nested-name-specifier. On the second and subsequent times
4065 through the loop, we look for the `template' keyword. */
4066 if (success && token->keyword == RID_TEMPLATE)
4068 /* A template-id can start a nested-name-specifier. */
4069 else if (token->type == CPP_TEMPLATE_ID)
4073 /* If the next token is not an identifier, then it is
4074 definitely not a type-name or namespace-name. */
4075 if (token->type != CPP_NAME)
4077 /* If the following token is neither a `<' (to begin a
4078 template-id), nor a `::', then we are not looking at a
4079 nested-name-specifier. */
4080 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4081 if (token->type != CPP_SCOPE
4082 && !cp_parser_nth_token_starts_template_argument_list_p
4087 /* The nested-name-specifier is optional, so we parse
4089 cp_parser_parse_tentatively (parser);
4091 /* Look for the optional `template' keyword, if this isn't the
4092 first time through the loop. */
4094 template_keyword_p = cp_parser_optional_template_keyword (parser);
4096 template_keyword_p = false;
4098 /* Save the old scope since the name lookup we are about to do
4099 might destroy it. */
4100 old_scope = parser->scope;
4101 saved_qualifying_scope = parser->qualifying_scope;
4102 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4103 look up names in "X<T>::I" in order to determine that "Y" is
4104 a template. So, if we have a typename at this point, we make
4105 an effort to look through it. */
4107 && !typename_keyword_p
4109 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4110 parser->scope = resolve_typename_type (parser->scope,
4111 /*only_current_p=*/false);
4112 /* Parse the qualifying entity. */
4114 = cp_parser_qualifying_entity (parser,
4120 /* Look for the `::' token. */
4121 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4123 /* If we found what we wanted, we keep going; otherwise, we're
4125 if (!cp_parser_parse_definitely (parser))
4127 bool error_p = false;
4129 /* Restore the OLD_SCOPE since it was valid before the
4130 failed attempt at finding the last
4131 class-or-namespace-name. */
4132 parser->scope = old_scope;
4133 parser->qualifying_scope = saved_qualifying_scope;
4134 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4136 /* If the next token is an identifier, and the one after
4137 that is a `::', then any valid interpretation would have
4138 found a class-or-namespace-name. */
4139 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4140 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4142 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4145 token = cp_lexer_consume_token (parser->lexer);
4148 if (!token->ambiguous_p)
4151 tree ambiguous_decls;
4153 decl = cp_parser_lookup_name (parser, token->u.value,
4155 /*is_template=*/false,
4156 /*is_namespace=*/false,
4157 /*check_dependency=*/true,
4160 if (TREE_CODE (decl) == TEMPLATE_DECL)
4161 error ("%H%qD used without template parameters",
4162 &token->location, decl);
4163 else if (ambiguous_decls)
4165 error ("%Hreference to %qD is ambiguous",
4166 &token->location, token->u.value);
4167 print_candidates (ambiguous_decls);
4168 decl = error_mark_node;
4172 const char* msg = "is not a class or namespace";
4173 if (cxx_dialect != cxx98)
4174 msg = "is not a class, namespace, or enumeration";
4175 cp_parser_name_lookup_error
4176 (parser, token->u.value, decl, msg,
4180 parser->scope = error_mark_node;
4182 /* Treat this as a successful nested-name-specifier
4187 If the name found is not a class-name (clause
4188 _class_) or namespace-name (_namespace.def_), the
4189 program is ill-formed. */
4192 cp_lexer_consume_token (parser->lexer);
4196 /* We've found one valid nested-name-specifier. */
4198 /* Name lookup always gives us a DECL. */
4199 if (TREE_CODE (new_scope) == TYPE_DECL)
4200 new_scope = TREE_TYPE (new_scope);
4201 /* Uses of "template" must be followed by actual templates. */
4202 if (template_keyword_p
4203 && !(CLASS_TYPE_P (new_scope)
4204 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4205 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4206 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4207 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4208 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4209 == TEMPLATE_ID_EXPR)))
4210 permerror (input_location, TYPE_P (new_scope)
4211 ? "%qT is not a template"
4212 : "%qD is not a template",
4214 /* If it is a class scope, try to complete it; we are about to
4215 be looking up names inside the class. */
4216 if (TYPE_P (new_scope)
4217 /* Since checking types for dependency can be expensive,
4218 avoid doing it if the type is already complete. */
4219 && !COMPLETE_TYPE_P (new_scope)
4220 /* Do not try to complete dependent types. */
4221 && !dependent_type_p (new_scope))
4223 new_scope = complete_type (new_scope);
4224 /* If it is a typedef to current class, use the current
4225 class instead, as the typedef won't have any names inside
4227 if (!COMPLETE_TYPE_P (new_scope)
4228 && currently_open_class (new_scope))
4229 new_scope = TYPE_MAIN_VARIANT (new_scope);
4231 /* Make sure we look in the right scope the next time through
4233 parser->scope = new_scope;
4236 /* If parsing tentatively, replace the sequence of tokens that makes
4237 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4238 token. That way, should we re-parse the token stream, we will
4239 not have to repeat the effort required to do the parse, nor will
4240 we issue duplicate error messages. */
4241 if (success && start)
4245 token = cp_lexer_token_at (parser->lexer, start);
4246 /* Reset the contents of the START token. */
4247 token->type = CPP_NESTED_NAME_SPECIFIER;
4248 /* Retrieve any deferred checks. Do not pop this access checks yet
4249 so the memory will not be reclaimed during token replacing below. */
4250 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4251 token->u.tree_check_value->value = parser->scope;
4252 token->u.tree_check_value->checks = get_deferred_access_checks ();
4253 token->u.tree_check_value->qualifying_scope =
4254 parser->qualifying_scope;
4255 token->keyword = RID_MAX;
4257 /* Purge all subsequent tokens. */
4258 cp_lexer_purge_tokens_after (parser->lexer, start);
4262 pop_to_parent_deferring_access_checks ();
4264 return success ? parser->scope : NULL_TREE;
4267 /* Parse a nested-name-specifier. See
4268 cp_parser_nested_name_specifier_opt for details. This function
4269 behaves identically, except that it will an issue an error if no
4270 nested-name-specifier is present. */
4273 cp_parser_nested_name_specifier (cp_parser *parser,
4274 bool typename_keyword_p,
4275 bool check_dependency_p,
4277 bool is_declaration)
4281 /* Look for the nested-name-specifier. */
4282 scope = cp_parser_nested_name_specifier_opt (parser,
4287 /* If it was not present, issue an error message. */
4290 cp_parser_error (parser, "expected nested-name-specifier");
4291 parser->scope = NULL_TREE;
4297 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4298 this is either a class-name or a namespace-name (which corresponds
4299 to the class-or-namespace-name production in the grammar). For
4300 C++0x, it can also be a type-name that refers to an enumeration
4303 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4304 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4305 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4306 TYPE_P is TRUE iff the next name should be taken as a class-name,
4307 even the same name is declared to be another entity in the same
4310 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4311 specified by the class-or-namespace-name. If neither is found the
4312 ERROR_MARK_NODE is returned. */
4315 cp_parser_qualifying_entity (cp_parser *parser,
4316 bool typename_keyword_p,
4317 bool template_keyword_p,
4318 bool check_dependency_p,
4320 bool is_declaration)
4323 tree saved_qualifying_scope;
4324 tree saved_object_scope;
4327 bool successful_parse_p;
4329 /* Before we try to parse the class-name, we must save away the
4330 current PARSER->SCOPE since cp_parser_class_name will destroy
4332 saved_scope = parser->scope;
4333 saved_qualifying_scope = parser->qualifying_scope;
4334 saved_object_scope = parser->object_scope;
4335 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4336 there is no need to look for a namespace-name. */
4337 only_class_p = template_keyword_p
4338 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4340 cp_parser_parse_tentatively (parser);
4341 scope = cp_parser_class_name (parser,
4344 type_p ? class_type : none_type,
4346 /*class_head_p=*/false,
4348 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4349 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4351 && cxx_dialect != cxx98
4352 && !successful_parse_p)
4354 /* Restore the saved scope. */
4355 parser->scope = saved_scope;
4356 parser->qualifying_scope = saved_qualifying_scope;
4357 parser->object_scope = saved_object_scope;
4359 /* Parse tentatively. */
4360 cp_parser_parse_tentatively (parser);
4362 /* Parse a typedef-name or enum-name. */
4363 scope = cp_parser_nonclass_name (parser);
4364 successful_parse_p = cp_parser_parse_definitely (parser);
4366 /* If that didn't work, try for a namespace-name. */
4367 if (!only_class_p && !successful_parse_p)
4369 /* Restore the saved scope. */
4370 parser->scope = saved_scope;
4371 parser->qualifying_scope = saved_qualifying_scope;
4372 parser->object_scope = saved_object_scope;
4373 /* If we are not looking at an identifier followed by the scope
4374 resolution operator, then this is not part of a
4375 nested-name-specifier. (Note that this function is only used
4376 to parse the components of a nested-name-specifier.) */
4377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4378 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4379 return error_mark_node;
4380 scope = cp_parser_namespace_name (parser);
4386 /* Parse a postfix-expression.
4390 postfix-expression [ expression ]
4391 postfix-expression ( expression-list [opt] )
4392 simple-type-specifier ( expression-list [opt] )
4393 typename :: [opt] nested-name-specifier identifier
4394 ( expression-list [opt] )
4395 typename :: [opt] nested-name-specifier template [opt] template-id
4396 ( expression-list [opt] )
4397 postfix-expression . template [opt] id-expression
4398 postfix-expression -> template [opt] id-expression
4399 postfix-expression . pseudo-destructor-name
4400 postfix-expression -> pseudo-destructor-name
4401 postfix-expression ++
4402 postfix-expression --
4403 dynamic_cast < type-id > ( expression )
4404 static_cast < type-id > ( expression )
4405 reinterpret_cast < type-id > ( expression )
4406 const_cast < type-id > ( expression )
4407 typeid ( expression )
4413 ( type-id ) { initializer-list , [opt] }
4415 This extension is a GNU version of the C99 compound-literal
4416 construct. (The C99 grammar uses `type-name' instead of `type-id',
4417 but they are essentially the same concept.)
4419 If ADDRESS_P is true, the postfix expression is the operand of the
4420 `&' operator. CAST_P is true if this expression is the target of a
4423 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4424 class member access expressions [expr.ref].
4426 Returns a representation of the expression. */
4429 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4430 bool member_access_only_p,
4431 cp_id_kind * pidk_return)
4435 cp_id_kind idk = CP_ID_KIND_NONE;
4436 tree postfix_expression = NULL_TREE;
4437 bool is_member_access = false;
4439 /* Peek at the next token. */
4440 token = cp_lexer_peek_token (parser->lexer);
4441 /* Some of the productions are determined by keywords. */
4442 keyword = token->keyword;
4452 const char *saved_message;
4454 /* All of these can be handled in the same way from the point
4455 of view of parsing. Begin by consuming the token
4456 identifying the cast. */
4457 cp_lexer_consume_token (parser->lexer);
4459 /* New types cannot be defined in the cast. */
4460 saved_message = parser->type_definition_forbidden_message;
4461 parser->type_definition_forbidden_message
4462 = "types may not be defined in casts";
4464 /* Look for the opening `<'. */
4465 cp_parser_require (parser, CPP_LESS, "%<<%>");
4466 /* Parse the type to which we are casting. */
4467 type = cp_parser_type_id (parser);
4468 /* Look for the closing `>'. */
4469 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4470 /* Restore the old message. */
4471 parser->type_definition_forbidden_message = saved_message;
4473 /* And the expression which is being cast. */
4474 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4475 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4476 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4478 /* Only type conversions to integral or enumeration types
4479 can be used in constant-expressions. */
4480 if (!cast_valid_in_integral_constant_expression_p (type)
4481 && (cp_parser_non_integral_constant_expression
4483 "a cast to a type other than an integral or "
4484 "enumeration type")))
4485 return error_mark_node;
4491 = build_dynamic_cast (type, expression, tf_warning_or_error);
4495 = build_static_cast (type, expression, tf_warning_or_error);
4499 = build_reinterpret_cast (type, expression,
4500 tf_warning_or_error);
4504 = build_const_cast (type, expression, tf_warning_or_error);
4515 const char *saved_message;
4516 bool saved_in_type_id_in_expr_p;
4518 /* Consume the `typeid' token. */
4519 cp_lexer_consume_token (parser->lexer);
4520 /* Look for the `(' token. */
4521 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4522 /* Types cannot be defined in a `typeid' expression. */
4523 saved_message = parser->type_definition_forbidden_message;
4524 parser->type_definition_forbidden_message
4525 = "types may not be defined in a %<typeid%> expression";
4526 /* We can't be sure yet whether we're looking at a type-id or an
4528 cp_parser_parse_tentatively (parser);
4529 /* Try a type-id first. */
4530 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4531 parser->in_type_id_in_expr_p = true;
4532 type = cp_parser_type_id (parser);
4533 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4534 /* Look for the `)' token. Otherwise, we can't be sure that
4535 we're not looking at an expression: consider `typeid (int
4536 (3))', for example. */
4537 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4538 /* If all went well, simply lookup the type-id. */
4539 if (cp_parser_parse_definitely (parser))
4540 postfix_expression = get_typeid (type);
4541 /* Otherwise, fall back to the expression variant. */
4546 /* Look for an expression. */
4547 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4548 /* Compute its typeid. */
4549 postfix_expression = build_typeid (expression);
4550 /* Look for the `)' token. */
4551 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4553 /* Restore the saved message. */
4554 parser->type_definition_forbidden_message = saved_message;
4555 /* `typeid' may not appear in an integral constant expression. */
4556 if (cp_parser_non_integral_constant_expression(parser,
4557 "%<typeid%> operator"))
4558 return error_mark_node;
4565 /* The syntax permitted here is the same permitted for an
4566 elaborated-type-specifier. */
4567 type = cp_parser_elaborated_type_specifier (parser,
4568 /*is_friend=*/false,
4569 /*is_declaration=*/false);
4570 postfix_expression = cp_parser_functional_cast (parser, type);
4578 /* If the next thing is a simple-type-specifier, we may be
4579 looking at a functional cast. We could also be looking at
4580 an id-expression. So, we try the functional cast, and if
4581 that doesn't work we fall back to the primary-expression. */
4582 cp_parser_parse_tentatively (parser);
4583 /* Look for the simple-type-specifier. */
4584 type = cp_parser_simple_type_specifier (parser,
4585 /*decl_specs=*/NULL,
4586 CP_PARSER_FLAGS_NONE);
4587 /* Parse the cast itself. */
4588 if (!cp_parser_error_occurred (parser))
4590 = cp_parser_functional_cast (parser, type);
4591 /* If that worked, we're done. */
4592 if (cp_parser_parse_definitely (parser))
4595 /* If the functional-cast didn't work out, try a
4596 compound-literal. */
4597 if (cp_parser_allow_gnu_extensions_p (parser)
4598 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4600 VEC(constructor_elt,gc) *initializer_list = NULL;
4601 bool saved_in_type_id_in_expr_p;
4603 cp_parser_parse_tentatively (parser);
4604 /* Consume the `('. */
4605 cp_lexer_consume_token (parser->lexer);
4606 /* Parse the type. */
4607 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4608 parser->in_type_id_in_expr_p = true;
4609 type = cp_parser_type_id (parser);
4610 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4611 /* Look for the `)'. */
4612 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4613 /* Look for the `{'. */
4614 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4615 /* If things aren't going well, there's no need to
4617 if (!cp_parser_error_occurred (parser))
4619 bool non_constant_p;
4620 /* Parse the initializer-list. */
4622 = cp_parser_initializer_list (parser, &non_constant_p);
4623 /* Allow a trailing `,'. */
4624 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4625 cp_lexer_consume_token (parser->lexer);
4626 /* Look for the final `}'. */
4627 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4629 /* If that worked, we're definitely looking at a
4630 compound-literal expression. */
4631 if (cp_parser_parse_definitely (parser))
4633 /* Warn the user that a compound literal is not
4634 allowed in standard C++. */
4635 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4636 /* For simplicity, we disallow compound literals in
4637 constant-expressions. We could
4638 allow compound literals of integer type, whose
4639 initializer was a constant, in constant
4640 expressions. Permitting that usage, as a further
4641 extension, would not change the meaning of any
4642 currently accepted programs. (Of course, as
4643 compound literals are not part of ISO C++, the
4644 standard has nothing to say.) */
4645 if (cp_parser_non_integral_constant_expression
4646 (parser, "non-constant compound literals"))
4648 postfix_expression = error_mark_node;
4651 /* Form the representation of the compound-literal. */
4653 = (finish_compound_literal
4654 (type, build_constructor (init_list_type_node,
4655 initializer_list)));
4660 /* It must be a primary-expression. */
4662 = cp_parser_primary_expression (parser, address_p, cast_p,
4663 /*template_arg_p=*/false,
4669 /* Keep looping until the postfix-expression is complete. */
4672 if (idk == CP_ID_KIND_UNQUALIFIED
4673 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4674 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4675 /* It is not a Koenig lookup function call. */
4677 = unqualified_name_lookup_error (postfix_expression);
4679 /* Peek at the next token. */
4680 token = cp_lexer_peek_token (parser->lexer);
4682 switch (token->type)
4684 case CPP_OPEN_SQUARE:
4686 = cp_parser_postfix_open_square_expression (parser,
4689 idk = CP_ID_KIND_NONE;
4690 is_member_access = false;
4693 case CPP_OPEN_PAREN:
4694 /* postfix-expression ( expression-list [opt] ) */
4697 bool is_builtin_constant_p;
4698 bool saved_integral_constant_expression_p = false;
4699 bool saved_non_integral_constant_expression_p = false;
4702 is_member_access = false;
4704 is_builtin_constant_p
4705 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4706 if (is_builtin_constant_p)
4708 /* The whole point of __builtin_constant_p is to allow
4709 non-constant expressions to appear as arguments. */
4710 saved_integral_constant_expression_p
4711 = parser->integral_constant_expression_p;
4712 saved_non_integral_constant_expression_p
4713 = parser->non_integral_constant_expression_p;
4714 parser->integral_constant_expression_p = false;
4716 args = (cp_parser_parenthesized_expression_list
4717 (parser, /*is_attribute_list=*/false,
4718 /*cast_p=*/false, /*allow_expansion_p=*/true,
4719 /*non_constant_p=*/NULL));
4720 if (is_builtin_constant_p)
4722 parser->integral_constant_expression_p
4723 = saved_integral_constant_expression_p;
4724 parser->non_integral_constant_expression_p
4725 = saved_non_integral_constant_expression_p;
4728 if (args == error_mark_node)
4730 postfix_expression = error_mark_node;
4734 /* Function calls are not permitted in
4735 constant-expressions. */
4736 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4737 && cp_parser_non_integral_constant_expression (parser,
4740 postfix_expression = error_mark_node;
4745 if (idk == CP_ID_KIND_UNQUALIFIED
4746 || idk == CP_ID_KIND_TEMPLATE_ID)
4748 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4753 if (!any_type_dependent_arguments_p (args))
4755 = perform_koenig_lookup (postfix_expression, args);
4759 = unqualified_fn_lookup_error (postfix_expression);
4761 /* We do not perform argument-dependent lookup if
4762 normal lookup finds a non-function, in accordance
4763 with the expected resolution of DR 218. */
4764 else if (args && is_overloaded_fn (postfix_expression))
4766 tree fn = get_first_fn (postfix_expression);
4768 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4769 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4771 /* Only do argument dependent lookup if regular
4772 lookup does not find a set of member functions.
4773 [basic.lookup.koenig]/2a */
4774 if (!DECL_FUNCTION_MEMBER_P (fn))
4777 if (!any_type_dependent_arguments_p (args))
4779 = perform_koenig_lookup (postfix_expression, args);
4784 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4786 tree instance = TREE_OPERAND (postfix_expression, 0);
4787 tree fn = TREE_OPERAND (postfix_expression, 1);
4789 if (processing_template_decl
4790 && (type_dependent_expression_p (instance)
4791 || (!BASELINK_P (fn)
4792 && TREE_CODE (fn) != FIELD_DECL)
4793 || type_dependent_expression_p (fn)
4794 || any_type_dependent_arguments_p (args)))
4797 = build_nt_call_list (postfix_expression, args);
4801 if (BASELINK_P (fn))
4804 = (build_new_method_call
4805 (instance, fn, args, NULL_TREE,
4806 (idk == CP_ID_KIND_QUALIFIED
4807 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4809 tf_warning_or_error));
4813 = finish_call_expr (postfix_expression, args,
4814 /*disallow_virtual=*/false,
4816 tf_warning_or_error);
4818 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4819 || TREE_CODE (postfix_expression) == MEMBER_REF
4820 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4821 postfix_expression = (build_offset_ref_call_from_tree
4822 (postfix_expression, args));
4823 else if (idk == CP_ID_KIND_QUALIFIED)
4824 /* A call to a static class member, or a namespace-scope
4827 = finish_call_expr (postfix_expression, args,
4828 /*disallow_virtual=*/true,
4830 tf_warning_or_error);
4832 /* All other function calls. */
4834 = finish_call_expr (postfix_expression, args,
4835 /*disallow_virtual=*/false,
4837 tf_warning_or_error);
4839 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4840 idk = CP_ID_KIND_NONE;
4846 /* postfix-expression . template [opt] id-expression
4847 postfix-expression . pseudo-destructor-name
4848 postfix-expression -> template [opt] id-expression
4849 postfix-expression -> pseudo-destructor-name */
4851 /* Consume the `.' or `->' operator. */
4852 cp_lexer_consume_token (parser->lexer);
4855 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4860 is_member_access = true;
4864 /* postfix-expression ++ */
4865 /* Consume the `++' token. */
4866 cp_lexer_consume_token (parser->lexer);
4867 /* Generate a representation for the complete expression. */
4869 = finish_increment_expr (postfix_expression,
4870 POSTINCREMENT_EXPR);
4871 /* Increments may not appear in constant-expressions. */
4872 if (cp_parser_non_integral_constant_expression (parser,
4874 postfix_expression = error_mark_node;
4875 idk = CP_ID_KIND_NONE;
4876 is_member_access = false;
4879 case CPP_MINUS_MINUS:
4880 /* postfix-expression -- */
4881 /* Consume the `--' token. */
4882 cp_lexer_consume_token (parser->lexer);
4883 /* Generate a representation for the complete expression. */
4885 = finish_increment_expr (postfix_expression,
4886 POSTDECREMENT_EXPR);
4887 /* Decrements may not appear in constant-expressions. */
4888 if (cp_parser_non_integral_constant_expression (parser,
4890 postfix_expression = error_mark_node;
4891 idk = CP_ID_KIND_NONE;
4892 is_member_access = false;
4896 if (pidk_return != NULL)
4897 * pidk_return = idk;
4898 if (member_access_only_p)
4899 return is_member_access? postfix_expression : error_mark_node;
4901 return postfix_expression;
4905 /* We should never get here. */
4907 return error_mark_node;
4910 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4911 by cp_parser_builtin_offsetof. We're looking for
4913 postfix-expression [ expression ]
4915 FOR_OFFSETOF is set if we're being called in that context, which
4916 changes how we deal with integer constant expressions. */
4919 cp_parser_postfix_open_square_expression (cp_parser *parser,
4920 tree postfix_expression,
4925 /* Consume the `[' token. */
4926 cp_lexer_consume_token (parser->lexer);
4928 /* Parse the index expression. */
4929 /* ??? For offsetof, there is a question of what to allow here. If
4930 offsetof is not being used in an integral constant expression context,
4931 then we *could* get the right answer by computing the value at runtime.
4932 If we are in an integral constant expression context, then we might
4933 could accept any constant expression; hard to say without analysis.
4934 Rather than open the barn door too wide right away, allow only integer
4935 constant expressions here. */
4937 index = cp_parser_constant_expression (parser, false, NULL);
4939 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4941 /* Look for the closing `]'. */
4942 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4944 /* Build the ARRAY_REF. */
4945 postfix_expression = grok_array_decl (postfix_expression, index);
4947 /* When not doing offsetof, array references are not permitted in
4948 constant-expressions. */
4950 && (cp_parser_non_integral_constant_expression
4951 (parser, "an array reference")))
4952 postfix_expression = error_mark_node;
4954 return postfix_expression;
4957 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4958 by cp_parser_builtin_offsetof. We're looking for
4960 postfix-expression . template [opt] id-expression
4961 postfix-expression . pseudo-destructor-name
4962 postfix-expression -> template [opt] id-expression
4963 postfix-expression -> pseudo-destructor-name
4965 FOR_OFFSETOF is set if we're being called in that context. That sorta
4966 limits what of the above we'll actually accept, but nevermind.
4967 TOKEN_TYPE is the "." or "->" token, which will already have been
4968 removed from the stream. */
4971 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4972 enum cpp_ttype token_type,
4973 tree postfix_expression,
4974 bool for_offsetof, cp_id_kind *idk,
4975 location_t location)
4979 bool pseudo_destructor_p;
4980 tree scope = NULL_TREE;
4982 /* If this is a `->' operator, dereference the pointer. */
4983 if (token_type == CPP_DEREF)
4984 postfix_expression = build_x_arrow (postfix_expression);
4985 /* Check to see whether or not the expression is type-dependent. */
4986 dependent_p = type_dependent_expression_p (postfix_expression);
4987 /* The identifier following the `->' or `.' is not qualified. */
4988 parser->scope = NULL_TREE;
4989 parser->qualifying_scope = NULL_TREE;
4990 parser->object_scope = NULL_TREE;
4991 *idk = CP_ID_KIND_NONE;
4993 /* Enter the scope corresponding to the type of the object
4994 given by the POSTFIX_EXPRESSION. */
4995 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4997 scope = TREE_TYPE (postfix_expression);
4998 /* According to the standard, no expression should ever have
4999 reference type. Unfortunately, we do not currently match
5000 the standard in this respect in that our internal representation
5001 of an expression may have reference type even when the standard
5002 says it does not. Therefore, we have to manually obtain the
5003 underlying type here. */
5004 scope = non_reference (scope);
5005 /* The type of the POSTFIX_EXPRESSION must be complete. */
5006 if (scope == unknown_type_node)
5008 error ("%H%qE does not have class type", &location, postfix_expression);
5012 scope = complete_type_or_else (scope, NULL_TREE);
5013 /* Let the name lookup machinery know that we are processing a
5014 class member access expression. */
5015 parser->context->object_type = scope;
5016 /* If something went wrong, we want to be able to discern that case,
5017 as opposed to the case where there was no SCOPE due to the type
5018 of expression being dependent. */
5020 scope = error_mark_node;
5021 /* If the SCOPE was erroneous, make the various semantic analysis
5022 functions exit quickly -- and without issuing additional error
5024 if (scope == error_mark_node)
5025 postfix_expression = error_mark_node;
5028 /* Assume this expression is not a pseudo-destructor access. */
5029 pseudo_destructor_p = false;
5031 /* If the SCOPE is a scalar type, then, if this is a valid program,
5032 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5033 is type dependent, it can be pseudo-destructor-name or something else.
5034 Try to parse it as pseudo-destructor-name first. */
5035 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5040 cp_parser_parse_tentatively (parser);
5041 /* Parse the pseudo-destructor-name. */
5043 cp_parser_pseudo_destructor_name (parser, &s, &type);
5045 && (cp_parser_error_occurred (parser)
5046 || TREE_CODE (type) != TYPE_DECL
5047 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5048 cp_parser_abort_tentative_parse (parser);
5049 else if (cp_parser_parse_definitely (parser))
5051 pseudo_destructor_p = true;
5053 = finish_pseudo_destructor_expr (postfix_expression,
5054 s, TREE_TYPE (type));
5058 if (!pseudo_destructor_p)
5060 /* If the SCOPE is not a scalar type, we are looking at an
5061 ordinary class member access expression, rather than a
5062 pseudo-destructor-name. */
5064 cp_token *token = cp_lexer_peek_token (parser->lexer);
5065 /* Parse the id-expression. */
5066 name = (cp_parser_id_expression
5068 cp_parser_optional_template_keyword (parser),
5069 /*check_dependency_p=*/true,
5071 /*declarator_p=*/false,
5072 /*optional_p=*/false));
5073 /* In general, build a SCOPE_REF if the member name is qualified.
5074 However, if the name was not dependent and has already been
5075 resolved; there is no need to build the SCOPE_REF. For example;
5077 struct X { void f(); };
5078 template <typename T> void f(T* t) { t->X::f(); }
5080 Even though "t" is dependent, "X::f" is not and has been resolved
5081 to a BASELINK; there is no need to include scope information. */
5083 /* But we do need to remember that there was an explicit scope for
5084 virtual function calls. */
5086 *idk = CP_ID_KIND_QUALIFIED;
5088 /* If the name is a template-id that names a type, we will get a
5089 TYPE_DECL here. That is invalid code. */
5090 if (TREE_CODE (name) == TYPE_DECL)
5092 error ("%Hinvalid use of %qD", &token->location, name);
5093 postfix_expression = error_mark_node;
5097 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5099 name = build_qualified_name (/*type=*/NULL_TREE,
5103 parser->scope = NULL_TREE;
5104 parser->qualifying_scope = NULL_TREE;
5105 parser->object_scope = NULL_TREE;
5107 if (scope && name && BASELINK_P (name))
5108 adjust_result_of_qualified_name_lookup
5109 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5111 = finish_class_member_access_expr (postfix_expression, name,
5113 tf_warning_or_error);
5117 /* We no longer need to look up names in the scope of the object on
5118 the left-hand side of the `.' or `->' operator. */
5119 parser->context->object_type = NULL_TREE;
5121 /* Outside of offsetof, these operators may not appear in
5122 constant-expressions. */
5124 && (cp_parser_non_integral_constant_expression
5125 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5126 postfix_expression = error_mark_node;
5128 return postfix_expression;
5131 /* Parse a parenthesized expression-list.
5134 assignment-expression
5135 expression-list, assignment-expression
5140 identifier, expression-list
5142 CAST_P is true if this expression is the target of a cast.
5144 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5147 Returns a TREE_LIST. The TREE_VALUE of each node is a
5148 representation of an assignment-expression. Note that a TREE_LIST
5149 is returned even if there is only a single expression in the list.
5150 error_mark_node is returned if the ( and or ) are
5151 missing. NULL_TREE is returned on no expressions. The parentheses
5152 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5153 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5154 indicates whether or not all of the expressions in the list were
5158 cp_parser_parenthesized_expression_list (cp_parser* parser,
5159 bool is_attribute_list,
5161 bool allow_expansion_p,
5162 bool *non_constant_p)
5164 tree expression_list = NULL_TREE;
5165 bool fold_expr_p = is_attribute_list;
5166 tree identifier = NULL_TREE;
5167 bool saved_greater_than_is_operator_p;
5169 /* Assume all the expressions will be constant. */
5171 *non_constant_p = false;
5173 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5174 return error_mark_node;
5176 /* Within a parenthesized expression, a `>' token is always
5177 the greater-than operator. */
5178 saved_greater_than_is_operator_p
5179 = parser->greater_than_is_operator_p;
5180 parser->greater_than_is_operator_p = true;
5182 /* Consume expressions until there are no more. */
5183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5188 /* At the beginning of attribute lists, check to see if the
5189 next token is an identifier. */
5190 if (is_attribute_list
5191 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5195 /* Consume the identifier. */
5196 token = cp_lexer_consume_token (parser->lexer);
5197 /* Save the identifier. */
5198 identifier = token->u.value;
5202 bool expr_non_constant_p;
5204 /* Parse the next assignment-expression. */
5205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5207 /* A braced-init-list. */
5208 maybe_warn_cpp0x ("extended initializer lists");
5209 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5210 if (non_constant_p && expr_non_constant_p)
5211 *non_constant_p = true;
5213 else if (non_constant_p)
5215 expr = (cp_parser_constant_expression
5216 (parser, /*allow_non_constant_p=*/true,
5217 &expr_non_constant_p));
5218 if (expr_non_constant_p)
5219 *non_constant_p = true;
5222 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5225 expr = fold_non_dependent_expr (expr);
5227 /* If we have an ellipsis, then this is an expression
5229 if (allow_expansion_p
5230 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5232 /* Consume the `...'. */
5233 cp_lexer_consume_token (parser->lexer);
5235 /* Build the argument pack. */
5236 expr = make_pack_expansion (expr);
5239 /* Add it to the list. We add error_mark_node
5240 expressions to the list, so that we can still tell if
5241 the correct form for a parenthesized expression-list
5242 is found. That gives better errors. */
5243 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5245 if (expr == error_mark_node)
5249 /* After the first item, attribute lists look the same as
5250 expression lists. */
5251 is_attribute_list = false;
5254 /* If the next token isn't a `,', then we are done. */
5255 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5258 /* Otherwise, consume the `,' and keep going. */
5259 cp_lexer_consume_token (parser->lexer);
5262 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5267 /* We try and resync to an unnested comma, as that will give the
5268 user better diagnostics. */
5269 ending = cp_parser_skip_to_closing_parenthesis (parser,
5270 /*recovering=*/true,
5272 /*consume_paren=*/true);
5277 parser->greater_than_is_operator_p
5278 = saved_greater_than_is_operator_p;
5279 return error_mark_node;
5283 parser->greater_than_is_operator_p
5284 = saved_greater_than_is_operator_p;
5286 /* We built up the list in reverse order so we must reverse it now. */
5287 expression_list = nreverse (expression_list);
5289 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5291 return expression_list;
5294 /* Parse a pseudo-destructor-name.
5296 pseudo-destructor-name:
5297 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5298 :: [opt] nested-name-specifier template template-id :: ~ type-name
5299 :: [opt] nested-name-specifier [opt] ~ type-name
5301 If either of the first two productions is used, sets *SCOPE to the
5302 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5303 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5304 or ERROR_MARK_NODE if the parse fails. */
5307 cp_parser_pseudo_destructor_name (cp_parser* parser,
5311 bool nested_name_specifier_p;
5313 /* Assume that things will not work out. */
5314 *type = error_mark_node;
5316 /* Look for the optional `::' operator. */
5317 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5318 /* Look for the optional nested-name-specifier. */
5319 nested_name_specifier_p
5320 = (cp_parser_nested_name_specifier_opt (parser,
5321 /*typename_keyword_p=*/false,
5322 /*check_dependency_p=*/true,
5324 /*is_declaration=*/false)
5326 /* Now, if we saw a nested-name-specifier, we might be doing the
5327 second production. */
5328 if (nested_name_specifier_p
5329 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5331 /* Consume the `template' keyword. */
5332 cp_lexer_consume_token (parser->lexer);
5333 /* Parse the template-id. */
5334 cp_parser_template_id (parser,
5335 /*template_keyword_p=*/true,
5336 /*check_dependency_p=*/false,
5337 /*is_declaration=*/true);
5338 /* Look for the `::' token. */
5339 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5341 /* If the next token is not a `~', then there might be some
5342 additional qualification. */
5343 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5345 /* At this point, we're looking for "type-name :: ~". The type-name
5346 must not be a class-name, since this is a pseudo-destructor. So,
5347 it must be either an enum-name, or a typedef-name -- both of which
5348 are just identifiers. So, we peek ahead to check that the "::"
5349 and "~" tokens are present; if they are not, then we can avoid
5350 calling type_name. */
5351 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5352 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5353 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5355 cp_parser_error (parser, "non-scalar type");
5359 /* Look for the type-name. */
5360 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5361 if (*scope == error_mark_node)
5364 /* Look for the `::' token. */
5365 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5370 /* Look for the `~'. */
5371 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5372 /* Look for the type-name again. We are not responsible for
5373 checking that it matches the first type-name. */
5374 *type = cp_parser_nonclass_name (parser);
5377 /* Parse a unary-expression.
5383 unary-operator cast-expression
5384 sizeof unary-expression
5392 __extension__ cast-expression
5393 __alignof__ unary-expression
5394 __alignof__ ( type-id )
5395 __real__ cast-expression
5396 __imag__ cast-expression
5399 ADDRESS_P is true iff the unary-expression is appearing as the
5400 operand of the `&' operator. CAST_P is true if this expression is
5401 the target of a cast.
5403 Returns a representation of the expression. */
5406 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5410 enum tree_code unary_operator;
5412 /* Peek at the next token. */
5413 token = cp_lexer_peek_token (parser->lexer);
5414 /* Some keywords give away the kind of expression. */
5415 if (token->type == CPP_KEYWORD)
5417 enum rid keyword = token->keyword;
5427 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5428 /* Consume the token. */
5429 cp_lexer_consume_token (parser->lexer);
5430 /* Parse the operand. */
5431 operand = cp_parser_sizeof_operand (parser, keyword);
5433 if (TYPE_P (operand))
5434 return cxx_sizeof_or_alignof_type (operand, op, true);
5436 return cxx_sizeof_or_alignof_expr (operand, op, true);
5440 return cp_parser_new_expression (parser);
5443 return cp_parser_delete_expression (parser);
5447 /* The saved value of the PEDANTIC flag. */
5451 /* Save away the PEDANTIC flag. */
5452 cp_parser_extension_opt (parser, &saved_pedantic);
5453 /* Parse the cast-expression. */
5454 expr = cp_parser_simple_cast_expression (parser);
5455 /* Restore the PEDANTIC flag. */
5456 pedantic = saved_pedantic;
5466 /* Consume the `__real__' or `__imag__' token. */
5467 cp_lexer_consume_token (parser->lexer);
5468 /* Parse the cast-expression. */
5469 expression = cp_parser_simple_cast_expression (parser);
5470 /* Create the complete representation. */
5471 return build_x_unary_op ((keyword == RID_REALPART
5472 ? REALPART_EXPR : IMAGPART_EXPR),
5474 tf_warning_or_error);
5483 /* Look for the `:: new' and `:: delete', which also signal the
5484 beginning of a new-expression, or delete-expression,
5485 respectively. If the next token is `::', then it might be one of
5487 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5491 /* See if the token after the `::' is one of the keywords in
5492 which we're interested. */
5493 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5494 /* If it's `new', we have a new-expression. */
5495 if (keyword == RID_NEW)
5496 return cp_parser_new_expression (parser);
5497 /* Similarly, for `delete'. */
5498 else if (keyword == RID_DELETE)
5499 return cp_parser_delete_expression (parser);
5502 /* Look for a unary operator. */
5503 unary_operator = cp_parser_unary_operator (token);
5504 /* The `++' and `--' operators can be handled similarly, even though
5505 they are not technically unary-operators in the grammar. */
5506 if (unary_operator == ERROR_MARK)
5508 if (token->type == CPP_PLUS_PLUS)
5509 unary_operator = PREINCREMENT_EXPR;
5510 else if (token->type == CPP_MINUS_MINUS)
5511 unary_operator = PREDECREMENT_EXPR;
5512 /* Handle the GNU address-of-label extension. */
5513 else if (cp_parser_allow_gnu_extensions_p (parser)
5514 && token->type == CPP_AND_AND)
5518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5520 /* Consume the '&&' token. */
5521 cp_lexer_consume_token (parser->lexer);
5522 /* Look for the identifier. */
5523 identifier = cp_parser_identifier (parser);
5524 /* Create an expression representing the address. */
5525 expression = finish_label_address_expr (identifier, loc);
5526 if (cp_parser_non_integral_constant_expression (parser,
5527 "the address of a label"))
5528 expression = error_mark_node;
5532 if (unary_operator != ERROR_MARK)
5534 tree cast_expression;
5535 tree expression = error_mark_node;
5536 const char *non_constant_p = NULL;
5538 /* Consume the operator token. */
5539 token = cp_lexer_consume_token (parser->lexer);
5540 /* Parse the cast-expression. */
5542 = cp_parser_cast_expression (parser,
5543 unary_operator == ADDR_EXPR,
5544 /*cast_p=*/false, pidk);
5545 /* Now, build an appropriate representation. */
5546 switch (unary_operator)
5549 non_constant_p = "%<*%>";
5550 expression = build_x_indirect_ref (cast_expression, "unary *",
5551 tf_warning_or_error);
5555 non_constant_p = "%<&%>";
5558 expression = build_x_unary_op (unary_operator, cast_expression,
5559 tf_warning_or_error);
5562 case PREINCREMENT_EXPR:
5563 case PREDECREMENT_EXPR:
5564 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5565 ? "%<++%>" : "%<--%>");
5567 case UNARY_PLUS_EXPR:
5569 case TRUTH_NOT_EXPR:
5570 expression = finish_unary_op_expr (unary_operator, cast_expression);
5578 && cp_parser_non_integral_constant_expression (parser,
5580 expression = error_mark_node;
5585 return cp_parser_postfix_expression (parser, address_p, cast_p,
5586 /*member_access_only_p=*/false,
5590 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5591 unary-operator, the corresponding tree code is returned. */
5593 static enum tree_code
5594 cp_parser_unary_operator (cp_token* token)
5596 switch (token->type)
5599 return INDIRECT_REF;
5605 return UNARY_PLUS_EXPR;
5611 return TRUTH_NOT_EXPR;
5614 return BIT_NOT_EXPR;
5621 /* Parse a new-expression.
5624 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5625 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5627 Returns a representation of the expression. */
5630 cp_parser_new_expression (cp_parser* parser)
5632 bool global_scope_p;
5638 /* Look for the optional `::' operator. */
5640 = (cp_parser_global_scope_opt (parser,
5641 /*current_scope_valid_p=*/false)
5643 /* Look for the `new' operator. */
5644 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5645 /* There's no easy way to tell a new-placement from the
5646 `( type-id )' construct. */
5647 cp_parser_parse_tentatively (parser);
5648 /* Look for a new-placement. */
5649 placement = cp_parser_new_placement (parser);
5650 /* If that didn't work out, there's no new-placement. */
5651 if (!cp_parser_parse_definitely (parser))
5652 placement = NULL_TREE;
5654 /* If the next token is a `(', then we have a parenthesized
5656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5659 /* Consume the `('. */
5660 cp_lexer_consume_token (parser->lexer);
5661 /* Parse the type-id. */
5662 type = cp_parser_type_id (parser);
5663 /* Look for the closing `)'. */
5664 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5665 token = cp_lexer_peek_token (parser->lexer);
5666 /* There should not be a direct-new-declarator in this production,
5667 but GCC used to allowed this, so we check and emit a sensible error
5668 message for this case. */
5669 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5671 error ("%Harray bound forbidden after parenthesized type-id",
5673 inform (token->location,
5674 "try removing the parentheses around the type-id");
5675 cp_parser_direct_new_declarator (parser);
5679 /* Otherwise, there must be a new-type-id. */
5681 type = cp_parser_new_type_id (parser, &nelts);
5683 /* If the next token is a `(' or '{', then we have a new-initializer. */
5684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5685 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5686 initializer = cp_parser_new_initializer (parser);
5688 initializer = NULL_TREE;
5690 /* A new-expression may not appear in an integral constant
5692 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5693 return error_mark_node;
5695 /* Create a representation of the new-expression. */
5696 return build_new (placement, type, nelts, initializer, global_scope_p,
5697 tf_warning_or_error);
5700 /* Parse a new-placement.
5705 Returns the same representation as for an expression-list. */
5708 cp_parser_new_placement (cp_parser* parser)
5710 tree expression_list;
5712 /* Parse the expression-list. */
5713 expression_list = (cp_parser_parenthesized_expression_list
5714 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5715 /*non_constant_p=*/NULL));
5717 return expression_list;
5720 /* Parse a new-type-id.
5723 type-specifier-seq new-declarator [opt]
5725 Returns the TYPE allocated. If the new-type-id indicates an array
5726 type, *NELTS is set to the number of elements in the last array
5727 bound; the TYPE will not include the last array bound. */
5730 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5732 cp_decl_specifier_seq type_specifier_seq;
5733 cp_declarator *new_declarator;
5734 cp_declarator *declarator;
5735 cp_declarator *outer_declarator;
5736 const char *saved_message;
5739 /* The type-specifier sequence must not contain type definitions.
5740 (It cannot contain declarations of new types either, but if they
5741 are not definitions we will catch that because they are not
5743 saved_message = parser->type_definition_forbidden_message;
5744 parser->type_definition_forbidden_message
5745 = "types may not be defined in a new-type-id";
5746 /* Parse the type-specifier-seq. */
5747 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5748 /*is_trailing_return=*/false,
5749 &type_specifier_seq);
5750 /* Restore the old message. */
5751 parser->type_definition_forbidden_message = saved_message;
5752 /* Parse the new-declarator. */
5753 new_declarator = cp_parser_new_declarator_opt (parser);
5755 /* Determine the number of elements in the last array dimension, if
5758 /* Skip down to the last array dimension. */
5759 declarator = new_declarator;
5760 outer_declarator = NULL;
5761 while (declarator && (declarator->kind == cdk_pointer
5762 || declarator->kind == cdk_ptrmem))
5764 outer_declarator = declarator;
5765 declarator = declarator->declarator;
5768 && declarator->kind == cdk_array
5769 && declarator->declarator
5770 && declarator->declarator->kind == cdk_array)
5772 outer_declarator = declarator;
5773 declarator = declarator->declarator;
5776 if (declarator && declarator->kind == cdk_array)
5778 *nelts = declarator->u.array.bounds;
5779 if (*nelts == error_mark_node)
5780 *nelts = integer_one_node;
5782 if (outer_declarator)
5783 outer_declarator->declarator = declarator->declarator;
5785 new_declarator = NULL;
5788 type = groktypename (&type_specifier_seq, new_declarator, false);
5792 /* Parse an (optional) new-declarator.
5795 ptr-operator new-declarator [opt]
5796 direct-new-declarator
5798 Returns the declarator. */
5800 static cp_declarator *
5801 cp_parser_new_declarator_opt (cp_parser* parser)
5803 enum tree_code code;
5805 cp_cv_quals cv_quals;
5807 /* We don't know if there's a ptr-operator next, or not. */
5808 cp_parser_parse_tentatively (parser);
5809 /* Look for a ptr-operator. */
5810 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5811 /* If that worked, look for more new-declarators. */
5812 if (cp_parser_parse_definitely (parser))
5814 cp_declarator *declarator;
5816 /* Parse another optional declarator. */
5817 declarator = cp_parser_new_declarator_opt (parser);
5819 return cp_parser_make_indirect_declarator
5820 (code, type, cv_quals, declarator);
5823 /* If the next token is a `[', there is a direct-new-declarator. */
5824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5825 return cp_parser_direct_new_declarator (parser);
5830 /* Parse a direct-new-declarator.
5832 direct-new-declarator:
5834 direct-new-declarator [constant-expression]
5838 static cp_declarator *
5839 cp_parser_direct_new_declarator (cp_parser* parser)
5841 cp_declarator *declarator = NULL;
5847 /* Look for the opening `['. */
5848 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5849 /* The first expression is not required to be constant. */
5852 cp_token *token = cp_lexer_peek_token (parser->lexer);
5853 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5854 /* The standard requires that the expression have integral
5855 type. DR 74 adds enumeration types. We believe that the
5856 real intent is that these expressions be handled like the
5857 expression in a `switch' condition, which also allows
5858 classes with a single conversion to integral or
5859 enumeration type. */
5860 if (!processing_template_decl)
5863 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5868 error ("%Hexpression in new-declarator must have integral "
5869 "or enumeration type", &token->location);
5870 expression = error_mark_node;
5874 /* But all the other expressions must be. */
5877 = cp_parser_constant_expression (parser,
5878 /*allow_non_constant=*/false,
5880 /* Look for the closing `]'. */
5881 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5883 /* Add this bound to the declarator. */
5884 declarator = make_array_declarator (declarator, expression);
5886 /* If the next token is not a `[', then there are no more
5888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5895 /* Parse a new-initializer.
5898 ( expression-list [opt] )
5901 Returns a representation of the expression-list. If there is no
5902 expression-list, VOID_ZERO_NODE is returned. */
5905 cp_parser_new_initializer (cp_parser* parser)
5907 tree expression_list;
5909 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5911 bool expr_non_constant_p;
5912 maybe_warn_cpp0x ("extended initializer lists");
5913 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5914 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5915 expression_list = build_tree_list (NULL_TREE, expression_list);
5918 expression_list = (cp_parser_parenthesized_expression_list
5919 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5920 /*non_constant_p=*/NULL));
5921 if (!expression_list)
5922 expression_list = void_zero_node;
5924 return expression_list;
5927 /* Parse a delete-expression.
5930 :: [opt] delete cast-expression
5931 :: [opt] delete [ ] cast-expression
5933 Returns a representation of the expression. */
5936 cp_parser_delete_expression (cp_parser* parser)
5938 bool global_scope_p;
5942 /* Look for the optional `::' operator. */
5944 = (cp_parser_global_scope_opt (parser,
5945 /*current_scope_valid_p=*/false)
5947 /* Look for the `delete' keyword. */
5948 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5949 /* See if the array syntax is in use. */
5950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5952 /* Consume the `[' token. */
5953 cp_lexer_consume_token (parser->lexer);
5954 /* Look for the `]' token. */
5955 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5956 /* Remember that this is the `[]' construct. */
5962 /* Parse the cast-expression. */
5963 expression = cp_parser_simple_cast_expression (parser);
5965 /* A delete-expression may not appear in an integral constant
5967 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5968 return error_mark_node;
5970 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5973 /* Returns true if TOKEN may start a cast-expression and false
5977 cp_parser_token_starts_cast_expression (cp_token *token)
5979 switch (token->type)
5985 case CPP_CLOSE_SQUARE:
5986 case CPP_CLOSE_PAREN:
5987 case CPP_CLOSE_BRACE:
5991 case CPP_DEREF_STAR:
5999 case CPP_GREATER_EQ:
6019 /* '[' may start a primary-expression in obj-c++. */
6020 case CPP_OPEN_SQUARE:
6021 return c_dialect_objc ();
6028 /* Parse a cast-expression.
6032 ( type-id ) cast-expression
6034 ADDRESS_P is true iff the unary-expression is appearing as the
6035 operand of the `&' operator. CAST_P is true if this expression is
6036 the target of a cast.
6038 Returns a representation of the expression. */
6041 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6044 /* If it's a `(', then we might be looking at a cast. */
6045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6047 tree type = NULL_TREE;
6048 tree expr = NULL_TREE;
6049 bool compound_literal_p;
6050 const char *saved_message;
6052 /* There's no way to know yet whether or not this is a cast.
6053 For example, `(int (3))' is a unary-expression, while `(int)
6054 3' is a cast. So, we resort to parsing tentatively. */
6055 cp_parser_parse_tentatively (parser);
6056 /* Types may not be defined in a cast. */
6057 saved_message = parser->type_definition_forbidden_message;
6058 parser->type_definition_forbidden_message
6059 = "types may not be defined in casts";
6060 /* Consume the `('. */
6061 cp_lexer_consume_token (parser->lexer);
6062 /* A very tricky bit is that `(struct S) { 3 }' is a
6063 compound-literal (which we permit in C++ as an extension).
6064 But, that construct is not a cast-expression -- it is a
6065 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6066 is legal; if the compound-literal were a cast-expression,
6067 you'd need an extra set of parentheses.) But, if we parse
6068 the type-id, and it happens to be a class-specifier, then we
6069 will commit to the parse at that point, because we cannot
6070 undo the action that is done when creating a new class. So,
6071 then we cannot back up and do a postfix-expression.
6073 Therefore, we scan ahead to the closing `)', and check to see
6074 if the token after the `)' is a `{'. If so, we are not
6075 looking at a cast-expression.
6077 Save tokens so that we can put them back. */
6078 cp_lexer_save_tokens (parser->lexer);
6079 /* Skip tokens until the next token is a closing parenthesis.
6080 If we find the closing `)', and the next token is a `{', then
6081 we are looking at a compound-literal. */
6083 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6084 /*consume_paren=*/true)
6085 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6086 /* Roll back the tokens we skipped. */
6087 cp_lexer_rollback_tokens (parser->lexer);
6088 /* If we were looking at a compound-literal, simulate an error
6089 so that the call to cp_parser_parse_definitely below will
6091 if (compound_literal_p)
6092 cp_parser_simulate_error (parser);
6095 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6096 parser->in_type_id_in_expr_p = true;
6097 /* Look for the type-id. */
6098 type = cp_parser_type_id (parser);
6099 /* Look for the closing `)'. */
6100 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6101 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6104 /* Restore the saved message. */
6105 parser->type_definition_forbidden_message = saved_message;
6107 /* At this point this can only be either a cast or a
6108 parenthesized ctor such as `(T ())' that looks like a cast to
6109 function returning T. */
6110 if (!cp_parser_error_occurred (parser)
6111 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6114 cp_parser_parse_definitely (parser);
6115 expr = cp_parser_cast_expression (parser,
6116 /*address_p=*/false,
6117 /*cast_p=*/true, pidk);
6119 /* Warn about old-style casts, if so requested. */
6120 if (warn_old_style_cast
6121 && !in_system_header
6122 && !VOID_TYPE_P (type)
6123 && current_lang_name != lang_name_c)
6124 warning (OPT_Wold_style_cast, "use of old-style cast");
6126 /* Only type conversions to integral or enumeration types
6127 can be used in constant-expressions. */
6128 if (!cast_valid_in_integral_constant_expression_p (type)
6129 && (cp_parser_non_integral_constant_expression
6131 "a cast to a type other than an integral or "
6132 "enumeration type")))
6133 return error_mark_node;
6135 /* Perform the cast. */
6136 expr = build_c_cast (type, expr);
6140 cp_parser_abort_tentative_parse (parser);
6143 /* If we get here, then it's not a cast, so it must be a
6144 unary-expression. */
6145 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6148 /* Parse a binary expression of the general form:
6152 pm-expression .* cast-expression
6153 pm-expression ->* cast-expression
6155 multiplicative-expression:
6157 multiplicative-expression * pm-expression
6158 multiplicative-expression / pm-expression
6159 multiplicative-expression % pm-expression
6161 additive-expression:
6162 multiplicative-expression
6163 additive-expression + multiplicative-expression
6164 additive-expression - multiplicative-expression
6168 shift-expression << additive-expression
6169 shift-expression >> additive-expression
6171 relational-expression:
6173 relational-expression < shift-expression
6174 relational-expression > shift-expression
6175 relational-expression <= shift-expression
6176 relational-expression >= shift-expression
6180 relational-expression:
6181 relational-expression <? shift-expression
6182 relational-expression >? shift-expression
6184 equality-expression:
6185 relational-expression
6186 equality-expression == relational-expression
6187 equality-expression != relational-expression
6191 and-expression & equality-expression
6193 exclusive-or-expression:
6195 exclusive-or-expression ^ and-expression
6197 inclusive-or-expression:
6198 exclusive-or-expression
6199 inclusive-or-expression | exclusive-or-expression
6201 logical-and-expression:
6202 inclusive-or-expression
6203 logical-and-expression && inclusive-or-expression
6205 logical-or-expression:
6206 logical-and-expression
6207 logical-or-expression || logical-and-expression
6209 All these are implemented with a single function like:
6212 simple-cast-expression
6213 binary-expression <token> binary-expression
6215 CAST_P is true if this expression is the target of a cast.
6217 The binops_by_token map is used to get the tree codes for each <token> type.
6218 binary-expressions are associated according to a precedence table. */
6220 #define TOKEN_PRECEDENCE(token) \
6221 (((token->type == CPP_GREATER \
6222 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6223 && !parser->greater_than_is_operator_p) \
6224 ? PREC_NOT_OPERATOR \
6225 : binops_by_token[token->type].prec)
6228 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6229 bool no_toplevel_fold_p,
6230 enum cp_parser_prec prec,
6233 cp_parser_expression_stack stack;
6234 cp_parser_expression_stack_entry *sp = &stack[0];
6237 enum tree_code tree_type, lhs_type, rhs_type;
6238 enum cp_parser_prec new_prec, lookahead_prec;
6241 /* Parse the first expression. */
6242 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6243 lhs_type = ERROR_MARK;
6247 /* Get an operator token. */
6248 token = cp_lexer_peek_token (parser->lexer);
6250 if (warn_cxx0x_compat
6251 && token->type == CPP_RSHIFT
6252 && !parser->greater_than_is_operator_p)
6254 warning (OPT_Wc__0x_compat,
6255 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6257 warning (OPT_Wc__0x_compat,
6258 "suggest parentheses around %<>>%> expression");
6261 new_prec = TOKEN_PRECEDENCE (token);
6263 /* Popping an entry off the stack means we completed a subexpression:
6264 - either we found a token which is not an operator (`>' where it is not
6265 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6266 will happen repeatedly;
6267 - or, we found an operator which has lower priority. This is the case
6268 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6270 if (new_prec <= prec)
6279 tree_type = binops_by_token[token->type].tree_type;
6281 /* We used the operator token. */
6282 cp_lexer_consume_token (parser->lexer);
6284 /* Extract another operand. It may be the RHS of this expression
6285 or the LHS of a new, higher priority expression. */
6286 rhs = cp_parser_simple_cast_expression (parser);
6287 rhs_type = ERROR_MARK;
6289 /* Get another operator token. Look up its precedence to avoid
6290 building a useless (immediately popped) stack entry for common
6291 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6292 token = cp_lexer_peek_token (parser->lexer);
6293 lookahead_prec = TOKEN_PRECEDENCE (token);
6294 if (lookahead_prec > new_prec)
6296 /* ... and prepare to parse the RHS of the new, higher priority
6297 expression. Since precedence levels on the stack are
6298 monotonically increasing, we do not have to care about
6301 sp->tree_type = tree_type;
6303 sp->lhs_type = lhs_type;
6306 lhs_type = rhs_type;
6308 new_prec = lookahead_prec;
6312 lookahead_prec = new_prec;
6313 /* If the stack is not empty, we have parsed into LHS the right side
6314 (`4' in the example above) of an expression we had suspended.
6315 We can use the information on the stack to recover the LHS (`3')
6316 from the stack together with the tree code (`MULT_EXPR'), and
6317 the precedence of the higher level subexpression
6318 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6319 which will be used to actually build the additive expression. */
6322 tree_type = sp->tree_type;
6324 rhs_type = lhs_type;
6326 lhs_type = sp->lhs_type;
6329 overloaded_p = false;
6330 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6331 ERROR_MARK for everything that is not a binary expression.
6332 This makes warn_about_parentheses miss some warnings that
6333 involve unary operators. For unary expressions we should
6334 pass the correct tree_code unless the unary expression was
6335 surrounded by parentheses.
6337 if (no_toplevel_fold_p
6338 && lookahead_prec <= prec
6340 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6341 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6343 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6344 &overloaded_p, tf_warning_or_error);
6345 lhs_type = tree_type;
6347 /* If the binary operator required the use of an overloaded operator,
6348 then this expression cannot be an integral constant-expression.
6349 An overloaded operator can be used even if both operands are
6350 otherwise permissible in an integral constant-expression if at
6351 least one of the operands is of enumeration type. */
6354 && (cp_parser_non_integral_constant_expression
6355 (parser, "calls to overloaded operators")))
6356 return error_mark_node;
6363 /* Parse the `? expression : assignment-expression' part of a
6364 conditional-expression. The LOGICAL_OR_EXPR is the
6365 logical-or-expression that started the conditional-expression.
6366 Returns a representation of the entire conditional-expression.
6368 This routine is used by cp_parser_assignment_expression.
6370 ? expression : assignment-expression
6374 ? : assignment-expression */
6377 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6380 tree assignment_expr;
6382 /* Consume the `?' token. */
6383 cp_lexer_consume_token (parser->lexer);
6384 if (cp_parser_allow_gnu_extensions_p (parser)
6385 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6386 /* Implicit true clause. */
6389 /* Parse the expression. */
6390 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6392 /* The next token should be a `:'. */
6393 cp_parser_require (parser, CPP_COLON, "%<:%>");
6394 /* Parse the assignment-expression. */
6395 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6397 /* Build the conditional-expression. */
6398 return build_x_conditional_expr (logical_or_expr,
6401 tf_warning_or_error);
6404 /* Parse an assignment-expression.
6406 assignment-expression:
6407 conditional-expression
6408 logical-or-expression assignment-operator assignment_expression
6411 CAST_P is true if this expression is the target of a cast.
6413 Returns a representation for the expression. */
6416 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6421 /* If the next token is the `throw' keyword, then we're looking at
6422 a throw-expression. */
6423 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6424 expr = cp_parser_throw_expression (parser);
6425 /* Otherwise, it must be that we are looking at a
6426 logical-or-expression. */
6429 /* Parse the binary expressions (logical-or-expression). */
6430 expr = cp_parser_binary_expression (parser, cast_p, false,
6431 PREC_NOT_OPERATOR, pidk);
6432 /* If the next token is a `?' then we're actually looking at a
6433 conditional-expression. */
6434 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6435 return cp_parser_question_colon_clause (parser, expr);
6438 enum tree_code assignment_operator;
6440 /* If it's an assignment-operator, we're using the second
6443 = cp_parser_assignment_operator_opt (parser);
6444 if (assignment_operator != ERROR_MARK)
6446 bool non_constant_p;
6448 /* Parse the right-hand side of the assignment. */
6449 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6451 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6452 maybe_warn_cpp0x ("extended initializer lists");
6454 /* An assignment may not appear in a
6455 constant-expression. */
6456 if (cp_parser_non_integral_constant_expression (parser,
6458 return error_mark_node;
6459 /* Build the assignment expression. */
6460 expr = build_x_modify_expr (expr,
6461 assignment_operator,
6463 tf_warning_or_error);
6471 /* Parse an (optional) assignment-operator.
6473 assignment-operator: one of
6474 = *= /= %= += -= >>= <<= &= ^= |=
6478 assignment-operator: one of
6481 If the next token is an assignment operator, the corresponding tree
6482 code is returned, and the token is consumed. For example, for
6483 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6484 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6485 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6486 operator, ERROR_MARK is returned. */
6488 static enum tree_code
6489 cp_parser_assignment_operator_opt (cp_parser* parser)
6494 /* Peek at the next token. */
6495 token = cp_lexer_peek_token (parser->lexer);
6497 switch (token->type)
6508 op = TRUNC_DIV_EXPR;
6512 op = TRUNC_MOD_EXPR;
6544 /* Nothing else is an assignment operator. */
6548 /* If it was an assignment operator, consume it. */
6549 if (op != ERROR_MARK)
6550 cp_lexer_consume_token (parser->lexer);
6555 /* Parse an expression.
6558 assignment-expression
6559 expression , assignment-expression
6561 CAST_P is true if this expression is the target of a cast.
6563 Returns a representation of the expression. */
6566 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6568 tree expression = NULL_TREE;
6572 tree assignment_expression;
6574 /* Parse the next assignment-expression. */
6575 assignment_expression
6576 = cp_parser_assignment_expression (parser, cast_p, pidk);
6577 /* If this is the first assignment-expression, we can just
6580 expression = assignment_expression;
6582 expression = build_x_compound_expr (expression,
6583 assignment_expression,
6584 tf_warning_or_error);
6585 /* If the next token is not a comma, then we are done with the
6587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6589 /* Consume the `,'. */
6590 cp_lexer_consume_token (parser->lexer);
6591 /* A comma operator cannot appear in a constant-expression. */
6592 if (cp_parser_non_integral_constant_expression (parser,
6593 "a comma operator"))
6594 expression = error_mark_node;
6600 /* Parse a constant-expression.
6602 constant-expression:
6603 conditional-expression
6605 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6606 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6607 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6608 is false, NON_CONSTANT_P should be NULL. */
6611 cp_parser_constant_expression (cp_parser* parser,
6612 bool allow_non_constant_p,
6613 bool *non_constant_p)
6615 bool saved_integral_constant_expression_p;
6616 bool saved_allow_non_integral_constant_expression_p;
6617 bool saved_non_integral_constant_expression_p;
6620 /* It might seem that we could simply parse the
6621 conditional-expression, and then check to see if it were
6622 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6623 one that the compiler can figure out is constant, possibly after
6624 doing some simplifications or optimizations. The standard has a
6625 precise definition of constant-expression, and we must honor
6626 that, even though it is somewhat more restrictive.
6632 is not a legal declaration, because `(2, 3)' is not a
6633 constant-expression. The `,' operator is forbidden in a
6634 constant-expression. However, GCC's constant-folding machinery
6635 will fold this operation to an INTEGER_CST for `3'. */
6637 /* Save the old settings. */
6638 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6639 saved_allow_non_integral_constant_expression_p
6640 = parser->allow_non_integral_constant_expression_p;
6641 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6642 /* We are now parsing a constant-expression. */
6643 parser->integral_constant_expression_p = true;
6644 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6645 parser->non_integral_constant_expression_p = false;
6646 /* Although the grammar says "conditional-expression", we parse an
6647 "assignment-expression", which also permits "throw-expression"
6648 and the use of assignment operators. In the case that
6649 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6650 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6651 actually essential that we look for an assignment-expression.
6652 For example, cp_parser_initializer_clauses uses this function to
6653 determine whether a particular assignment-expression is in fact
6655 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6656 /* Restore the old settings. */
6657 parser->integral_constant_expression_p
6658 = saved_integral_constant_expression_p;
6659 parser->allow_non_integral_constant_expression_p
6660 = saved_allow_non_integral_constant_expression_p;
6661 if (allow_non_constant_p)
6662 *non_constant_p = parser->non_integral_constant_expression_p;
6663 else if (parser->non_integral_constant_expression_p)
6664 expression = error_mark_node;
6665 parser->non_integral_constant_expression_p
6666 = saved_non_integral_constant_expression_p;
6671 /* Parse __builtin_offsetof.
6673 offsetof-expression:
6674 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6676 offsetof-member-designator:
6678 | offsetof-member-designator "." id-expression
6679 | offsetof-member-designator "[" expression "]"
6680 | offsetof-member-designator "->" id-expression */
6683 cp_parser_builtin_offsetof (cp_parser *parser)
6685 int save_ice_p, save_non_ice_p;
6690 /* We're about to accept non-integral-constant things, but will
6691 definitely yield an integral constant expression. Save and
6692 restore these values around our local parsing. */
6693 save_ice_p = parser->integral_constant_expression_p;
6694 save_non_ice_p = parser->non_integral_constant_expression_p;
6696 /* Consume the "__builtin_offsetof" token. */
6697 cp_lexer_consume_token (parser->lexer);
6698 /* Consume the opening `('. */
6699 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6700 /* Parse the type-id. */
6701 type = cp_parser_type_id (parser);
6702 /* Look for the `,'. */
6703 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6704 token = cp_lexer_peek_token (parser->lexer);
6706 /* Build the (type *)null that begins the traditional offsetof macro. */
6707 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6708 tf_warning_or_error);
6710 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6711 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6712 true, &dummy, token->location);
6715 token = cp_lexer_peek_token (parser->lexer);
6716 switch (token->type)
6718 case CPP_OPEN_SQUARE:
6719 /* offsetof-member-designator "[" expression "]" */
6720 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6724 /* offsetof-member-designator "->" identifier */
6725 expr = grok_array_decl (expr, integer_zero_node);
6729 /* offsetof-member-designator "." identifier */
6730 cp_lexer_consume_token (parser->lexer);
6731 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6736 case CPP_CLOSE_PAREN:
6737 /* Consume the ")" token. */
6738 cp_lexer_consume_token (parser->lexer);
6742 /* Error. We know the following require will fail, but
6743 that gives the proper error message. */
6744 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6745 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6746 expr = error_mark_node;
6752 /* If we're processing a template, we can't finish the semantics yet.
6753 Otherwise we can fold the entire expression now. */
6754 if (processing_template_decl)
6755 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6757 expr = finish_offsetof (expr);
6760 parser->integral_constant_expression_p = save_ice_p;
6761 parser->non_integral_constant_expression_p = save_non_ice_p;
6766 /* Parse a trait expression. */
6769 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6772 tree type1, type2 = NULL_TREE;
6773 bool binary = false;
6774 cp_decl_specifier_seq decl_specs;
6778 case RID_HAS_NOTHROW_ASSIGN:
6779 kind = CPTK_HAS_NOTHROW_ASSIGN;
6781 case RID_HAS_NOTHROW_CONSTRUCTOR:
6782 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6784 case RID_HAS_NOTHROW_COPY:
6785 kind = CPTK_HAS_NOTHROW_COPY;
6787 case RID_HAS_TRIVIAL_ASSIGN:
6788 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6790 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6791 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6793 case RID_HAS_TRIVIAL_COPY:
6794 kind = CPTK_HAS_TRIVIAL_COPY;
6796 case RID_HAS_TRIVIAL_DESTRUCTOR:
6797 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6799 case RID_HAS_VIRTUAL_DESTRUCTOR:
6800 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6802 case RID_IS_ABSTRACT:
6803 kind = CPTK_IS_ABSTRACT;
6805 case RID_IS_BASE_OF:
6806 kind = CPTK_IS_BASE_OF;
6810 kind = CPTK_IS_CLASS;
6812 case RID_IS_CONVERTIBLE_TO:
6813 kind = CPTK_IS_CONVERTIBLE_TO;
6817 kind = CPTK_IS_EMPTY;
6820 kind = CPTK_IS_ENUM;
6825 case RID_IS_POLYMORPHIC:
6826 kind = CPTK_IS_POLYMORPHIC;
6829 kind = CPTK_IS_UNION;
6835 /* Consume the token. */
6836 cp_lexer_consume_token (parser->lexer);
6838 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6840 type1 = cp_parser_type_id (parser);
6842 if (type1 == error_mark_node)
6843 return error_mark_node;
6845 /* Build a trivial decl-specifier-seq. */
6846 clear_decl_specs (&decl_specs);
6847 decl_specs.type = type1;
6849 /* Call grokdeclarator to figure out what type this is. */
6850 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6851 /*initialized=*/0, /*attrlist=*/NULL);
6855 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6857 type2 = cp_parser_type_id (parser);
6859 if (type2 == error_mark_node)
6860 return error_mark_node;
6862 /* Build a trivial decl-specifier-seq. */
6863 clear_decl_specs (&decl_specs);
6864 decl_specs.type = type2;
6866 /* Call grokdeclarator to figure out what type this is. */
6867 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6868 /*initialized=*/0, /*attrlist=*/NULL);
6871 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6873 /* Complete the trait expression, which may mean either processing
6874 the trait expr now or saving it for template instantiation. */
6875 return finish_trait_expr (kind, type1, type2);
6878 /* Statements [gram.stmt.stmt] */
6880 /* Parse a statement.
6884 expression-statement
6889 declaration-statement
6892 IN_COMPOUND is true when the statement is nested inside a
6893 cp_parser_compound_statement; this matters for certain pragmas.
6895 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6896 is a (possibly labeled) if statement which is not enclosed in braces
6897 and has an else clause. This is used to implement -Wparentheses. */
6900 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6901 bool in_compound, bool *if_p)
6905 location_t statement_location;
6910 /* There is no statement yet. */
6911 statement = NULL_TREE;
6912 /* Peek at the next token. */
6913 token = cp_lexer_peek_token (parser->lexer);
6914 /* Remember the location of the first token in the statement. */
6915 statement_location = token->location;
6916 /* If this is a keyword, then that will often determine what kind of
6917 statement we have. */
6918 if (token->type == CPP_KEYWORD)
6920 enum rid keyword = token->keyword;
6926 /* Looks like a labeled-statement with a case label.
6927 Parse the label, and then use tail recursion to parse
6929 cp_parser_label_for_labeled_statement (parser);
6934 statement = cp_parser_selection_statement (parser, if_p);
6940 statement = cp_parser_iteration_statement (parser);
6947 statement = cp_parser_jump_statement (parser);
6950 /* Objective-C++ exception-handling constructs. */
6953 case RID_AT_FINALLY:
6954 case RID_AT_SYNCHRONIZED:
6956 statement = cp_parser_objc_statement (parser);
6960 statement = cp_parser_try_block (parser);
6964 /* This must be a namespace alias definition. */
6965 cp_parser_declaration_statement (parser);
6969 /* It might be a keyword like `int' that can start a
6970 declaration-statement. */
6974 else if (token->type == CPP_NAME)
6976 /* If the next token is a `:', then we are looking at a
6977 labeled-statement. */
6978 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6979 if (token->type == CPP_COLON)
6981 /* Looks like a labeled-statement with an ordinary label.
6982 Parse the label, and then use tail recursion to parse
6984 cp_parser_label_for_labeled_statement (parser);
6988 /* Anything that starts with a `{' must be a compound-statement. */
6989 else if (token->type == CPP_OPEN_BRACE)
6990 statement = cp_parser_compound_statement (parser, NULL, false);
6991 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6992 a statement all its own. */
6993 else if (token->type == CPP_PRAGMA)
6995 /* Only certain OpenMP pragmas are attached to statements, and thus
6996 are considered statements themselves. All others are not. In
6997 the context of a compound, accept the pragma as a "statement" and
6998 return so that we can check for a close brace. Otherwise we
6999 require a real statement and must go back and read one. */
7001 cp_parser_pragma (parser, pragma_compound);
7002 else if (!cp_parser_pragma (parser, pragma_stmt))
7006 else if (token->type == CPP_EOF)
7008 cp_parser_error (parser, "expected statement");
7012 /* Everything else must be a declaration-statement or an
7013 expression-statement. Try for the declaration-statement
7014 first, unless we are looking at a `;', in which case we know that
7015 we have an expression-statement. */
7018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7020 cp_parser_parse_tentatively (parser);
7021 /* Try to parse the declaration-statement. */
7022 cp_parser_declaration_statement (parser);
7023 /* If that worked, we're done. */
7024 if (cp_parser_parse_definitely (parser))
7027 /* Look for an expression-statement instead. */
7028 statement = cp_parser_expression_statement (parser, in_statement_expr);
7031 /* Set the line number for the statement. */
7032 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7033 SET_EXPR_LOCATION (statement, statement_location);
7036 /* Parse the label for a labeled-statement, i.e.
7039 case constant-expression :
7043 case constant-expression ... constant-expression : statement
7045 When a label is parsed without errors, the label is added to the
7046 parse tree by the finish_* functions, so this function doesn't
7047 have to return the label. */
7050 cp_parser_label_for_labeled_statement (cp_parser* parser)
7054 /* The next token should be an identifier. */
7055 token = cp_lexer_peek_token (parser->lexer);
7056 if (token->type != CPP_NAME
7057 && token->type != CPP_KEYWORD)
7059 cp_parser_error (parser, "expected labeled-statement");
7063 switch (token->keyword)
7070 /* Consume the `case' token. */
7071 cp_lexer_consume_token (parser->lexer);
7072 /* Parse the constant-expression. */
7073 expr = cp_parser_constant_expression (parser,
7074 /*allow_non_constant_p=*/false,
7077 ellipsis = cp_lexer_peek_token (parser->lexer);
7078 if (ellipsis->type == CPP_ELLIPSIS)
7080 /* Consume the `...' token. */
7081 cp_lexer_consume_token (parser->lexer);
7083 cp_parser_constant_expression (parser,
7084 /*allow_non_constant_p=*/false,
7086 /* We don't need to emit warnings here, as the common code
7087 will do this for us. */
7090 expr_hi = NULL_TREE;
7092 if (parser->in_switch_statement_p)
7093 finish_case_label (expr, expr_hi);
7095 error ("%Hcase label %qE not within a switch statement",
7096 &token->location, expr);
7101 /* Consume the `default' token. */
7102 cp_lexer_consume_token (parser->lexer);
7104 if (parser->in_switch_statement_p)
7105 finish_case_label (NULL_TREE, NULL_TREE);
7107 error ("%Hcase label not within a switch statement", &token->location);
7111 /* Anything else must be an ordinary label. */
7112 finish_label_stmt (cp_parser_identifier (parser));
7116 /* Require the `:' token. */
7117 cp_parser_require (parser, CPP_COLON, "%<:%>");
7120 /* Parse an expression-statement.
7122 expression-statement:
7125 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7126 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7127 indicates whether this expression-statement is part of an
7128 expression statement. */
7131 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7133 tree statement = NULL_TREE;
7135 /* If the next token is a ';', then there is no expression
7137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7138 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7140 /* Consume the final `;'. */
7141 cp_parser_consume_semicolon_at_end_of_statement (parser);
7143 if (in_statement_expr
7144 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7145 /* This is the final expression statement of a statement
7147 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7149 statement = finish_expr_stmt (statement);
7156 /* Parse a compound-statement.
7159 { statement-seq [opt] }
7164 { label-declaration-seq [opt] statement-seq [opt] }
7166 label-declaration-seq:
7168 label-declaration-seq label-declaration
7170 Returns a tree representing the statement. */
7173 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7178 /* Consume the `{'. */
7179 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7180 return error_mark_node;
7181 /* Begin the compound-statement. */
7182 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7183 /* If the next keyword is `__label__' we have a label declaration. */
7184 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7185 cp_parser_label_declaration (parser);
7186 /* Parse an (optional) statement-seq. */
7187 cp_parser_statement_seq_opt (parser, in_statement_expr);
7188 /* Finish the compound-statement. */
7189 finish_compound_stmt (compound_stmt);
7190 /* Consume the `}'. */
7191 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7193 return compound_stmt;
7196 /* Parse an (optional) statement-seq.
7200 statement-seq [opt] statement */
7203 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7205 /* Scan statements until there aren't any more. */
7208 cp_token *token = cp_lexer_peek_token (parser->lexer);
7210 /* If we're looking at a `}', then we've run out of statements. */
7211 if (token->type == CPP_CLOSE_BRACE
7212 || token->type == CPP_EOF
7213 || token->type == CPP_PRAGMA_EOL)
7216 /* If we are in a compound statement and find 'else' then
7217 something went wrong. */
7218 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7220 if (parser->in_statement & IN_IF_STMT)
7224 token = cp_lexer_consume_token (parser->lexer);
7225 error ("%H%<else%> without a previous %<if%>", &token->location);
7229 /* Parse the statement. */
7230 cp_parser_statement (parser, in_statement_expr, true, NULL);
7234 /* Parse a selection-statement.
7236 selection-statement:
7237 if ( condition ) statement
7238 if ( condition ) statement else statement
7239 switch ( condition ) statement
7241 Returns the new IF_STMT or SWITCH_STMT.
7243 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7244 is a (possibly labeled) if statement which is not enclosed in
7245 braces and has an else clause. This is used to implement
7249 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7257 /* Peek at the next token. */
7258 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7260 /* See what kind of keyword it is. */
7261 keyword = token->keyword;
7270 /* Look for the `('. */
7271 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7273 cp_parser_skip_to_end_of_statement (parser);
7274 return error_mark_node;
7277 /* Begin the selection-statement. */
7278 if (keyword == RID_IF)
7279 statement = begin_if_stmt ();
7281 statement = begin_switch_stmt ();
7283 /* Parse the condition. */
7284 condition = cp_parser_condition (parser);
7285 /* Look for the `)'. */
7286 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7287 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7288 /*consume_paren=*/true);
7290 if (keyword == RID_IF)
7293 unsigned char in_statement;
7295 /* Add the condition. */
7296 finish_if_stmt_cond (condition, statement);
7298 /* Parse the then-clause. */
7299 in_statement = parser->in_statement;
7300 parser->in_statement |= IN_IF_STMT;
7301 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7303 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7304 add_stmt (build_empty_stmt ());
7305 cp_lexer_consume_token (parser->lexer);
7306 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7307 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7308 "empty body in an %<if%> statement");
7312 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7313 parser->in_statement = in_statement;
7315 finish_then_clause (statement);
7317 /* If the next token is `else', parse the else-clause. */
7318 if (cp_lexer_next_token_is_keyword (parser->lexer,
7321 /* Consume the `else' keyword. */
7322 cp_lexer_consume_token (parser->lexer);
7323 begin_else_clause (statement);
7324 /* Parse the else-clause. */
7325 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7327 warning_at (cp_lexer_peek_token (parser->lexer)->location,
7328 OPT_Wempty_body, "suggest braces around "
7329 "empty body in an %<else%> statement");
7330 add_stmt (build_empty_stmt ());
7331 cp_lexer_consume_token (parser->lexer);
7334 cp_parser_implicitly_scoped_statement (parser, NULL);
7336 finish_else_clause (statement);
7338 /* If we are currently parsing a then-clause, then
7339 IF_P will not be NULL. We set it to true to
7340 indicate that this if statement has an else clause.
7341 This may trigger the Wparentheses warning below
7342 when we get back up to the parent if statement. */
7348 /* This if statement does not have an else clause. If
7349 NESTED_IF is true, then the then-clause is an if
7350 statement which does have an else clause. We warn
7351 about the potential ambiguity. */
7353 warning (OPT_Wparentheses,
7354 ("%Hsuggest explicit braces "
7355 "to avoid ambiguous %<else%>"),
7356 EXPR_LOCUS (statement));
7359 /* Now we're all done with the if-statement. */
7360 finish_if_stmt (statement);
7364 bool in_switch_statement_p;
7365 unsigned char in_statement;
7367 /* Add the condition. */
7368 finish_switch_cond (condition, statement);
7370 /* Parse the body of the switch-statement. */
7371 in_switch_statement_p = parser->in_switch_statement_p;
7372 in_statement = parser->in_statement;
7373 parser->in_switch_statement_p = true;
7374 parser->in_statement |= IN_SWITCH_STMT;
7375 cp_parser_implicitly_scoped_statement (parser, NULL);
7376 parser->in_switch_statement_p = in_switch_statement_p;
7377 parser->in_statement = in_statement;
7379 /* Now we're all done with the switch-statement. */
7380 finish_switch_stmt (statement);
7388 cp_parser_error (parser, "expected selection-statement");
7389 return error_mark_node;
7393 /* Parse a condition.
7397 type-specifier-seq declarator = initializer-clause
7398 type-specifier-seq declarator braced-init-list
7403 type-specifier-seq declarator asm-specification [opt]
7404 attributes [opt] = assignment-expression
7406 Returns the expression that should be tested. */
7409 cp_parser_condition (cp_parser* parser)
7411 cp_decl_specifier_seq type_specifiers;
7412 const char *saved_message;
7414 /* Try the declaration first. */
7415 cp_parser_parse_tentatively (parser);
7416 /* New types are not allowed in the type-specifier-seq for a
7418 saved_message = parser->type_definition_forbidden_message;
7419 parser->type_definition_forbidden_message
7420 = "types may not be defined in conditions";
7421 /* Parse the type-specifier-seq. */
7422 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7423 /*is_trailing_return=*/false,
7425 /* Restore the saved message. */
7426 parser->type_definition_forbidden_message = saved_message;
7427 /* If all is well, we might be looking at a declaration. */
7428 if (!cp_parser_error_occurred (parser))
7431 tree asm_specification;
7433 cp_declarator *declarator;
7434 tree initializer = NULL_TREE;
7436 /* Parse the declarator. */
7437 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7438 /*ctor_dtor_or_conv_p=*/NULL,
7439 /*parenthesized_p=*/NULL,
7440 /*member_p=*/false);
7441 /* Parse the attributes. */
7442 attributes = cp_parser_attributes_opt (parser);
7443 /* Parse the asm-specification. */
7444 asm_specification = cp_parser_asm_specification_opt (parser);
7445 /* If the next token is not an `=' or '{', then we might still be
7446 looking at an expression. For example:
7450 looks like a decl-specifier-seq and a declarator -- but then
7451 there is no `=', so this is an expression. */
7452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7453 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7454 cp_parser_simulate_error (parser);
7456 /* If we did see an `=' or '{', then we are looking at a declaration
7458 if (cp_parser_parse_definitely (parser))
7461 bool non_constant_p;
7462 bool flags = LOOKUP_ONLYCONVERTING;
7464 /* Create the declaration. */
7465 decl = start_decl (declarator, &type_specifiers,
7466 /*initialized_p=*/true,
7467 attributes, /*prefix_attributes=*/NULL_TREE,
7470 /* Parse the initializer. */
7471 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7473 initializer = cp_parser_braced_list (parser, &non_constant_p);
7474 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7479 /* Consume the `='. */
7480 cp_parser_require (parser, CPP_EQ, "%<=%>");
7481 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7483 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7484 maybe_warn_cpp0x ("extended initializer lists");
7486 if (!non_constant_p)
7487 initializer = fold_non_dependent_expr (initializer);
7489 /* Process the initializer. */
7490 cp_finish_decl (decl,
7491 initializer, !non_constant_p,
7496 pop_scope (pushed_scope);
7498 return convert_from_reference (decl);
7501 /* If we didn't even get past the declarator successfully, we are
7502 definitely not looking at a declaration. */
7504 cp_parser_abort_tentative_parse (parser);
7506 /* Otherwise, we are looking at an expression. */
7507 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7510 /* Parse an iteration-statement.
7512 iteration-statement:
7513 while ( condition ) statement
7514 do statement while ( expression ) ;
7515 for ( for-init-statement condition [opt] ; expression [opt] )
7518 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7521 cp_parser_iteration_statement (cp_parser* parser)
7526 unsigned char in_statement;
7528 /* Peek at the next token. */
7529 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7531 return error_mark_node;
7533 /* Remember whether or not we are already within an iteration
7535 in_statement = parser->in_statement;
7537 /* See what kind of keyword it is. */
7538 keyword = token->keyword;
7545 /* Begin the while-statement. */
7546 statement = begin_while_stmt ();
7547 /* Look for the `('. */
7548 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7549 /* Parse the condition. */
7550 condition = cp_parser_condition (parser);
7551 finish_while_stmt_cond (condition, statement);
7552 /* Look for the `)'. */
7553 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7554 /* Parse the dependent statement. */
7555 parser->in_statement = IN_ITERATION_STMT;
7556 cp_parser_already_scoped_statement (parser);
7557 parser->in_statement = in_statement;
7558 /* We're done with the while-statement. */
7559 finish_while_stmt (statement);
7567 /* Begin the do-statement. */
7568 statement = begin_do_stmt ();
7569 /* Parse the body of the do-statement. */
7570 parser->in_statement = IN_ITERATION_STMT;
7571 cp_parser_implicitly_scoped_statement (parser, NULL);
7572 parser->in_statement = in_statement;
7573 finish_do_body (statement);
7574 /* Look for the `while' keyword. */
7575 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7576 /* Look for the `('. */
7577 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7578 /* Parse the expression. */
7579 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7580 /* We're done with the do-statement. */
7581 finish_do_stmt (expression, statement);
7582 /* Look for the `)'. */
7583 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7584 /* Look for the `;'. */
7585 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7591 tree condition = NULL_TREE;
7592 tree expression = NULL_TREE;
7594 /* Begin the for-statement. */
7595 statement = begin_for_stmt ();
7596 /* Look for the `('. */
7597 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7598 /* Parse the initialization. */
7599 cp_parser_for_init_statement (parser);
7600 finish_for_init_stmt (statement);
7602 /* If there's a condition, process it. */
7603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7604 condition = cp_parser_condition (parser);
7605 finish_for_cond (condition, statement);
7606 /* Look for the `;'. */
7607 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7609 /* If there's an expression, process it. */
7610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7611 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7612 finish_for_expr (expression, statement);
7613 /* Look for the `)'. */
7614 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7616 /* Parse the body of the for-statement. */
7617 parser->in_statement = IN_ITERATION_STMT;
7618 cp_parser_already_scoped_statement (parser);
7619 parser->in_statement = in_statement;
7621 /* We're done with the for-statement. */
7622 finish_for_stmt (statement);
7627 cp_parser_error (parser, "expected iteration-statement");
7628 statement = error_mark_node;
7635 /* Parse a for-init-statement.
7638 expression-statement
7639 simple-declaration */
7642 cp_parser_for_init_statement (cp_parser* parser)
7644 /* If the next token is a `;', then we have an empty
7645 expression-statement. Grammatically, this is also a
7646 simple-declaration, but an invalid one, because it does not
7647 declare anything. Therefore, if we did not handle this case
7648 specially, we would issue an error message about an invalid
7650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7652 /* We're going to speculatively look for a declaration, falling back
7653 to an expression, if necessary. */
7654 cp_parser_parse_tentatively (parser);
7655 /* Parse the declaration. */
7656 cp_parser_simple_declaration (parser,
7657 /*function_definition_allowed_p=*/false);
7658 /* If the tentative parse failed, then we shall need to look for an
7659 expression-statement. */
7660 if (cp_parser_parse_definitely (parser))
7664 cp_parser_expression_statement (parser, false);
7667 /* Parse a jump-statement.
7672 return expression [opt] ;
7673 return braced-init-list ;
7681 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7684 cp_parser_jump_statement (cp_parser* parser)
7686 tree statement = error_mark_node;
7689 unsigned char in_statement;
7691 /* Peek at the next token. */
7692 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7694 return error_mark_node;
7696 /* See what kind of keyword it is. */
7697 keyword = token->keyword;
7701 in_statement = parser->in_statement & ~IN_IF_STMT;
7702 switch (in_statement)
7705 error ("%Hbreak statement not within loop or switch", &token->location);
7708 gcc_assert ((in_statement & IN_SWITCH_STMT)
7709 || in_statement == IN_ITERATION_STMT);
7710 statement = finish_break_stmt ();
7713 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7716 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7719 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7723 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7726 error ("%Hcontinue statement not within a loop", &token->location);
7728 case IN_ITERATION_STMT:
7730 statement = finish_continue_stmt ();
7733 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7738 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7744 bool expr_non_constant_p;
7746 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7748 maybe_warn_cpp0x ("extended initializer lists");
7749 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7751 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7752 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7754 /* If the next token is a `;', then there is no
7757 /* Build the return-statement. */
7758 statement = finish_return_stmt (expr);
7759 /* Look for the final `;'. */
7760 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7765 /* Create the goto-statement. */
7766 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7768 /* Issue a warning about this use of a GNU extension. */
7769 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7770 /* Consume the '*' token. */
7771 cp_lexer_consume_token (parser->lexer);
7772 /* Parse the dependent expression. */
7773 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7776 finish_goto_stmt (cp_parser_identifier (parser));
7777 /* Look for the final `;'. */
7778 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7782 cp_parser_error (parser, "expected jump-statement");
7789 /* Parse a declaration-statement.
7791 declaration-statement:
7792 block-declaration */
7795 cp_parser_declaration_statement (cp_parser* parser)
7799 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7800 p = obstack_alloc (&declarator_obstack, 0);
7802 /* Parse the block-declaration. */
7803 cp_parser_block_declaration (parser, /*statement_p=*/true);
7805 /* Free any declarators allocated. */
7806 obstack_free (&declarator_obstack, p);
7808 /* Finish off the statement. */
7812 /* Some dependent statements (like `if (cond) statement'), are
7813 implicitly in their own scope. In other words, if the statement is
7814 a single statement (as opposed to a compound-statement), it is
7815 none-the-less treated as if it were enclosed in braces. Any
7816 declarations appearing in the dependent statement are out of scope
7817 after control passes that point. This function parses a statement,
7818 but ensures that is in its own scope, even if it is not a
7821 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7822 is a (possibly labeled) if statement which is not enclosed in
7823 braces and has an else clause. This is used to implement
7826 Returns the new statement. */
7829 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7836 /* Mark if () ; with a special NOP_EXPR. */
7837 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7839 cp_lexer_consume_token (parser->lexer);
7840 statement = add_stmt (build_empty_stmt ());
7842 /* if a compound is opened, we simply parse the statement directly. */
7843 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7844 statement = cp_parser_compound_statement (parser, NULL, false);
7845 /* If the token is not a `{', then we must take special action. */
7848 /* Create a compound-statement. */
7849 statement = begin_compound_stmt (0);
7850 /* Parse the dependent-statement. */
7851 cp_parser_statement (parser, NULL_TREE, false, if_p);
7852 /* Finish the dummy compound-statement. */
7853 finish_compound_stmt (statement);
7856 /* Return the statement. */
7860 /* For some dependent statements (like `while (cond) statement'), we
7861 have already created a scope. Therefore, even if the dependent
7862 statement is a compound-statement, we do not want to create another
7866 cp_parser_already_scoped_statement (cp_parser* parser)
7868 /* If the token is a `{', then we must take special action. */
7869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7870 cp_parser_statement (parser, NULL_TREE, false, NULL);
7873 /* Avoid calling cp_parser_compound_statement, so that we
7874 don't create a new scope. Do everything else by hand. */
7875 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7876 /* If the next keyword is `__label__' we have a label declaration. */
7877 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7878 cp_parser_label_declaration (parser);
7879 /* Parse an (optional) statement-seq. */
7880 cp_parser_statement_seq_opt (parser, NULL_TREE);
7881 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7885 /* Declarations [gram.dcl.dcl] */
7887 /* Parse an optional declaration-sequence.
7891 declaration-seq declaration */
7894 cp_parser_declaration_seq_opt (cp_parser* parser)
7900 token = cp_lexer_peek_token (parser->lexer);
7902 if (token->type == CPP_CLOSE_BRACE
7903 || token->type == CPP_EOF
7904 || token->type == CPP_PRAGMA_EOL)
7907 if (token->type == CPP_SEMICOLON)
7909 /* A declaration consisting of a single semicolon is
7910 invalid. Allow it unless we're being pedantic. */
7911 cp_lexer_consume_token (parser->lexer);
7912 if (!in_system_header)
7913 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7917 /* If we're entering or exiting a region that's implicitly
7918 extern "C", modify the lang context appropriately. */
7919 if (!parser->implicit_extern_c && token->implicit_extern_c)
7921 push_lang_context (lang_name_c);
7922 parser->implicit_extern_c = true;
7924 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7926 pop_lang_context ();
7927 parser->implicit_extern_c = false;
7930 if (token->type == CPP_PRAGMA)
7932 /* A top-level declaration can consist solely of a #pragma.
7933 A nested declaration cannot, so this is done here and not
7934 in cp_parser_declaration. (A #pragma at block scope is
7935 handled in cp_parser_statement.) */
7936 cp_parser_pragma (parser, pragma_external);
7940 /* Parse the declaration itself. */
7941 cp_parser_declaration (parser);
7945 /* Parse a declaration.
7950 template-declaration
7951 explicit-instantiation
7952 explicit-specialization
7953 linkage-specification
7954 namespace-definition
7959 __extension__ declaration */
7962 cp_parser_declaration (cp_parser* parser)
7969 /* Check for the `__extension__' keyword. */
7970 if (cp_parser_extension_opt (parser, &saved_pedantic))
7972 /* Parse the qualified declaration. */
7973 cp_parser_declaration (parser);
7974 /* Restore the PEDANTIC flag. */
7975 pedantic = saved_pedantic;
7980 /* Try to figure out what kind of declaration is present. */
7981 token1 = *cp_lexer_peek_token (parser->lexer);
7983 if (token1.type != CPP_EOF)
7984 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7987 token2.type = CPP_EOF;
7988 token2.keyword = RID_MAX;
7991 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7992 p = obstack_alloc (&declarator_obstack, 0);
7994 /* If the next token is `extern' and the following token is a string
7995 literal, then we have a linkage specification. */
7996 if (token1.keyword == RID_EXTERN
7997 && cp_parser_is_string_literal (&token2))
7998 cp_parser_linkage_specification (parser);
7999 /* If the next token is `template', then we have either a template
8000 declaration, an explicit instantiation, or an explicit
8002 else if (token1.keyword == RID_TEMPLATE)
8004 /* `template <>' indicates a template specialization. */
8005 if (token2.type == CPP_LESS
8006 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8007 cp_parser_explicit_specialization (parser);
8008 /* `template <' indicates a template declaration. */
8009 else if (token2.type == CPP_LESS)
8010 cp_parser_template_declaration (parser, /*member_p=*/false);
8011 /* Anything else must be an explicit instantiation. */
8013 cp_parser_explicit_instantiation (parser);
8015 /* If the next token is `export', then we have a template
8017 else if (token1.keyword == RID_EXPORT)
8018 cp_parser_template_declaration (parser, /*member_p=*/false);
8019 /* If the next token is `extern', 'static' or 'inline' and the one
8020 after that is `template', we have a GNU extended explicit
8021 instantiation directive. */
8022 else if (cp_parser_allow_gnu_extensions_p (parser)
8023 && (token1.keyword == RID_EXTERN
8024 || token1.keyword == RID_STATIC
8025 || token1.keyword == RID_INLINE)
8026 && token2.keyword == RID_TEMPLATE)
8027 cp_parser_explicit_instantiation (parser);
8028 /* If the next token is `namespace', check for a named or unnamed
8029 namespace definition. */
8030 else if (token1.keyword == RID_NAMESPACE
8031 && (/* A named namespace definition. */
8032 (token2.type == CPP_NAME
8033 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8035 /* An unnamed namespace definition. */
8036 || token2.type == CPP_OPEN_BRACE
8037 || token2.keyword == RID_ATTRIBUTE))
8038 cp_parser_namespace_definition (parser);
8039 /* An inline (associated) namespace definition. */
8040 else if (token1.keyword == RID_INLINE
8041 && token2.keyword == RID_NAMESPACE)
8042 cp_parser_namespace_definition (parser);
8043 /* Objective-C++ declaration/definition. */
8044 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8045 cp_parser_objc_declaration (parser);
8046 /* We must have either a block declaration or a function
8049 /* Try to parse a block-declaration, or a function-definition. */
8050 cp_parser_block_declaration (parser, /*statement_p=*/false);
8052 /* Free any declarators allocated. */
8053 obstack_free (&declarator_obstack, p);
8056 /* Parse a block-declaration.
8061 namespace-alias-definition
8068 __extension__ block-declaration
8073 static_assert-declaration
8075 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8076 part of a declaration-statement. */
8079 cp_parser_block_declaration (cp_parser *parser,
8085 /* Check for the `__extension__' keyword. */
8086 if (cp_parser_extension_opt (parser, &saved_pedantic))
8088 /* Parse the qualified declaration. */
8089 cp_parser_block_declaration (parser, statement_p);
8090 /* Restore the PEDANTIC flag. */
8091 pedantic = saved_pedantic;
8096 /* Peek at the next token to figure out which kind of declaration is
8098 token1 = cp_lexer_peek_token (parser->lexer);
8100 /* If the next keyword is `asm', we have an asm-definition. */
8101 if (token1->keyword == RID_ASM)
8104 cp_parser_commit_to_tentative_parse (parser);
8105 cp_parser_asm_definition (parser);
8107 /* If the next keyword is `namespace', we have a
8108 namespace-alias-definition. */
8109 else if (token1->keyword == RID_NAMESPACE)
8110 cp_parser_namespace_alias_definition (parser);
8111 /* If the next keyword is `using', we have either a
8112 using-declaration or a using-directive. */
8113 else if (token1->keyword == RID_USING)
8118 cp_parser_commit_to_tentative_parse (parser);
8119 /* If the token after `using' is `namespace', then we have a
8121 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8122 if (token2->keyword == RID_NAMESPACE)
8123 cp_parser_using_directive (parser);
8124 /* Otherwise, it's a using-declaration. */
8126 cp_parser_using_declaration (parser,
8127 /*access_declaration_p=*/false);
8129 /* If the next keyword is `__label__' we have a misplaced label
8131 else if (token1->keyword == RID_LABEL)
8133 cp_lexer_consume_token (parser->lexer);
8134 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8135 cp_parser_skip_to_end_of_statement (parser);
8136 /* If the next token is now a `;', consume it. */
8137 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8138 cp_lexer_consume_token (parser->lexer);
8140 /* If the next token is `static_assert' we have a static assertion. */
8141 else if (token1->keyword == RID_STATIC_ASSERT)
8142 cp_parser_static_assert (parser, /*member_p=*/false);
8143 /* Anything else must be a simple-declaration. */
8145 cp_parser_simple_declaration (parser, !statement_p);
8148 /* Parse a simple-declaration.
8151 decl-specifier-seq [opt] init-declarator-list [opt] ;
8153 init-declarator-list:
8155 init-declarator-list , init-declarator
8157 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8158 function-definition as a simple-declaration. */
8161 cp_parser_simple_declaration (cp_parser* parser,
8162 bool function_definition_allowed_p)
8164 cp_decl_specifier_seq decl_specifiers;
8165 int declares_class_or_enum;
8166 bool saw_declarator;
8168 /* Defer access checks until we know what is being declared; the
8169 checks for names appearing in the decl-specifier-seq should be
8170 done as if we were in the scope of the thing being declared. */
8171 push_deferring_access_checks (dk_deferred);
8173 /* Parse the decl-specifier-seq. We have to keep track of whether
8174 or not the decl-specifier-seq declares a named class or
8175 enumeration type, since that is the only case in which the
8176 init-declarator-list is allowed to be empty.
8180 In a simple-declaration, the optional init-declarator-list can be
8181 omitted only when declaring a class or enumeration, that is when
8182 the decl-specifier-seq contains either a class-specifier, an
8183 elaborated-type-specifier, or an enum-specifier. */
8184 cp_parser_decl_specifier_seq (parser,
8185 CP_PARSER_FLAGS_OPTIONAL,
8187 &declares_class_or_enum);
8188 /* We no longer need to defer access checks. */
8189 stop_deferring_access_checks ();
8191 /* In a block scope, a valid declaration must always have a
8192 decl-specifier-seq. By not trying to parse declarators, we can
8193 resolve the declaration/expression ambiguity more quickly. */
8194 if (!function_definition_allowed_p
8195 && !decl_specifiers.any_specifiers_p)
8197 cp_parser_error (parser, "expected declaration");
8201 /* If the next two tokens are both identifiers, the code is
8202 erroneous. The usual cause of this situation is code like:
8206 where "T" should name a type -- but does not. */
8207 if (!decl_specifiers.type
8208 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8210 /* If parsing tentatively, we should commit; we really are
8211 looking at a declaration. */
8212 cp_parser_commit_to_tentative_parse (parser);
8217 /* If we have seen at least one decl-specifier, and the next token
8218 is not a parenthesis, then we must be looking at a declaration.
8219 (After "int (" we might be looking at a functional cast.) */
8220 if (decl_specifiers.any_specifiers_p
8221 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8222 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8223 && !cp_parser_error_occurred (parser))
8224 cp_parser_commit_to_tentative_parse (parser);
8226 /* Keep going until we hit the `;' at the end of the simple
8228 saw_declarator = false;
8229 while (cp_lexer_next_token_is_not (parser->lexer,
8233 bool function_definition_p;
8238 /* If we are processing next declarator, coma is expected */
8239 token = cp_lexer_peek_token (parser->lexer);
8240 gcc_assert (token->type == CPP_COMMA);
8241 cp_lexer_consume_token (parser->lexer);
8244 saw_declarator = true;
8246 /* Parse the init-declarator. */
8247 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8249 function_definition_allowed_p,
8251 declares_class_or_enum,
8252 &function_definition_p);
8253 /* If an error occurred while parsing tentatively, exit quickly.
8254 (That usually happens when in the body of a function; each
8255 statement is treated as a declaration-statement until proven
8257 if (cp_parser_error_occurred (parser))
8259 /* Handle function definitions specially. */
8260 if (function_definition_p)
8262 /* If the next token is a `,', then we are probably
8263 processing something like:
8267 which is erroneous. */
8268 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8270 cp_token *token = cp_lexer_peek_token (parser->lexer);
8271 error ("%Hmixing declarations and function-definitions is forbidden",
8274 /* Otherwise, we're done with the list of declarators. */
8277 pop_deferring_access_checks ();
8281 /* The next token should be either a `,' or a `;'. */
8282 token = cp_lexer_peek_token (parser->lexer);
8283 /* If it's a `,', there are more declarators to come. */
8284 if (token->type == CPP_COMMA)
8285 /* will be consumed next time around */;
8286 /* If it's a `;', we are done. */
8287 else if (token->type == CPP_SEMICOLON)
8289 /* Anything else is an error. */
8292 /* If we have already issued an error message we don't need
8293 to issue another one. */
8294 if (decl != error_mark_node
8295 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8296 cp_parser_error (parser, "expected %<,%> or %<;%>");
8297 /* Skip tokens until we reach the end of the statement. */
8298 cp_parser_skip_to_end_of_statement (parser);
8299 /* If the next token is now a `;', consume it. */
8300 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8301 cp_lexer_consume_token (parser->lexer);
8304 /* After the first time around, a function-definition is not
8305 allowed -- even if it was OK at first. For example:
8310 function_definition_allowed_p = false;
8313 /* Issue an error message if no declarators are present, and the
8314 decl-specifier-seq does not itself declare a class or
8316 if (!saw_declarator)
8318 if (cp_parser_declares_only_class_p (parser))
8319 shadow_tag (&decl_specifiers);
8320 /* Perform any deferred access checks. */
8321 perform_deferred_access_checks ();
8324 /* Consume the `;'. */
8325 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8328 pop_deferring_access_checks ();
8331 /* Parse a decl-specifier-seq.
8334 decl-specifier-seq [opt] decl-specifier
8337 storage-class-specifier
8348 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8350 The parser flags FLAGS is used to control type-specifier parsing.
8352 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8355 1: one of the decl-specifiers is an elaborated-type-specifier
8356 (i.e., a type declaration)
8357 2: one of the decl-specifiers is an enum-specifier or a
8358 class-specifier (i.e., a type definition)
8363 cp_parser_decl_specifier_seq (cp_parser* parser,
8364 cp_parser_flags flags,
8365 cp_decl_specifier_seq *decl_specs,
8366 int* declares_class_or_enum)
8368 bool constructor_possible_p = !parser->in_declarator_p;
8369 cp_token *start_token = NULL;
8371 /* Clear DECL_SPECS. */
8372 clear_decl_specs (decl_specs);
8374 /* Assume no class or enumeration type is declared. */
8375 *declares_class_or_enum = 0;
8377 /* Keep reading specifiers until there are no more to read. */
8381 bool found_decl_spec;
8384 /* Peek at the next token. */
8385 token = cp_lexer_peek_token (parser->lexer);
8387 /* Save the first token of the decl spec list for error
8390 start_token = token;
8391 /* Handle attributes. */
8392 if (token->keyword == RID_ATTRIBUTE)
8394 /* Parse the attributes. */
8395 decl_specs->attributes
8396 = chainon (decl_specs->attributes,
8397 cp_parser_attributes_opt (parser));
8400 /* Assume we will find a decl-specifier keyword. */
8401 found_decl_spec = true;
8402 /* If the next token is an appropriate keyword, we can simply
8403 add it to the list. */
8404 switch (token->keyword)
8409 if (!at_class_scope_p ())
8411 error ("%H%<friend%> used outside of class", &token->location);
8412 cp_lexer_purge_token (parser->lexer);
8416 ++decl_specs->specs[(int) ds_friend];
8417 /* Consume the token. */
8418 cp_lexer_consume_token (parser->lexer);
8422 /* function-specifier:
8429 cp_parser_function_specifier_opt (parser, decl_specs);
8435 ++decl_specs->specs[(int) ds_typedef];
8436 /* Consume the token. */
8437 cp_lexer_consume_token (parser->lexer);
8438 /* A constructor declarator cannot appear in a typedef. */
8439 constructor_possible_p = false;
8440 /* The "typedef" keyword can only occur in a declaration; we
8441 may as well commit at this point. */
8442 cp_parser_commit_to_tentative_parse (parser);
8444 if (decl_specs->storage_class != sc_none)
8445 decl_specs->conflicting_specifiers_p = true;
8448 /* storage-class-specifier:
8458 if (cxx_dialect == cxx98)
8460 /* Consume the token. */
8461 cp_lexer_consume_token (parser->lexer);
8463 /* Complain about `auto' as a storage specifier, if
8464 we're complaining about C++0x compatibility. */
8467 "%H%<auto%> will change meaning in C++0x; please remove it",
8470 /* Set the storage class anyway. */
8471 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8475 /* C++0x auto type-specifier. */
8476 found_decl_spec = false;
8483 /* Consume the token. */
8484 cp_lexer_consume_token (parser->lexer);
8485 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8489 /* Consume the token. */
8490 cp_lexer_consume_token (parser->lexer);
8491 ++decl_specs->specs[(int) ds_thread];
8495 /* We did not yet find a decl-specifier yet. */
8496 found_decl_spec = false;
8500 /* Constructors are a special case. The `S' in `S()' is not a
8501 decl-specifier; it is the beginning of the declarator. */
8504 && constructor_possible_p
8505 && (cp_parser_constructor_declarator_p
8506 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8508 /* If we don't have a DECL_SPEC yet, then we must be looking at
8509 a type-specifier. */
8510 if (!found_decl_spec && !constructor_p)
8512 int decl_spec_declares_class_or_enum;
8513 bool is_cv_qualifier;
8517 = cp_parser_type_specifier (parser, flags,
8519 /*is_declaration=*/true,
8520 &decl_spec_declares_class_or_enum,
8522 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8524 /* If this type-specifier referenced a user-defined type
8525 (a typedef, class-name, etc.), then we can't allow any
8526 more such type-specifiers henceforth.
8530 The longest sequence of decl-specifiers that could
8531 possibly be a type name is taken as the
8532 decl-specifier-seq of a declaration. The sequence shall
8533 be self-consistent as described below.
8537 As a general rule, at most one type-specifier is allowed
8538 in the complete decl-specifier-seq of a declaration. The
8539 only exceptions are the following:
8541 -- const or volatile can be combined with any other
8544 -- signed or unsigned can be combined with char, long,
8552 void g (const int Pc);
8554 Here, Pc is *not* part of the decl-specifier seq; it's
8555 the declarator. Therefore, once we see a type-specifier
8556 (other than a cv-qualifier), we forbid any additional
8557 user-defined types. We *do* still allow things like `int
8558 int' to be considered a decl-specifier-seq, and issue the
8559 error message later. */
8560 if (type_spec && !is_cv_qualifier)
8561 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8562 /* A constructor declarator cannot follow a type-specifier. */
8565 constructor_possible_p = false;
8566 found_decl_spec = true;
8570 /* If we still do not have a DECL_SPEC, then there are no more
8572 if (!found_decl_spec)
8575 decl_specs->any_specifiers_p = true;
8576 /* After we see one decl-specifier, further decl-specifiers are
8578 flags |= CP_PARSER_FLAGS_OPTIONAL;
8581 cp_parser_check_decl_spec (decl_specs, start_token->location);
8583 /* Don't allow a friend specifier with a class definition. */
8584 if (decl_specs->specs[(int) ds_friend] != 0
8585 && (*declares_class_or_enum & 2))
8586 error ("%Hclass definition may not be declared a friend",
8587 &start_token->location);
8590 /* Parse an (optional) storage-class-specifier.
8592 storage-class-specifier:
8601 storage-class-specifier:
8604 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8607 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8609 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8612 if (cxx_dialect != cxx98)
8614 /* Fall through for C++98. */
8621 /* Consume the token. */
8622 return cp_lexer_consume_token (parser->lexer)->u.value;
8629 /* Parse an (optional) function-specifier.
8636 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8637 Updates DECL_SPECS, if it is non-NULL. */
8640 cp_parser_function_specifier_opt (cp_parser* parser,
8641 cp_decl_specifier_seq *decl_specs)
8643 cp_token *token = cp_lexer_peek_token (parser->lexer);
8644 switch (token->keyword)
8648 ++decl_specs->specs[(int) ds_inline];
8652 /* 14.5.2.3 [temp.mem]
8654 A member function template shall not be virtual. */
8655 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8656 error ("%Htemplates may not be %<virtual%>", &token->location);
8657 else if (decl_specs)
8658 ++decl_specs->specs[(int) ds_virtual];
8663 ++decl_specs->specs[(int) ds_explicit];
8670 /* Consume the token. */
8671 return cp_lexer_consume_token (parser->lexer)->u.value;
8674 /* Parse a linkage-specification.
8676 linkage-specification:
8677 extern string-literal { declaration-seq [opt] }
8678 extern string-literal declaration */
8681 cp_parser_linkage_specification (cp_parser* parser)
8685 /* Look for the `extern' keyword. */
8686 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8688 /* Look for the string-literal. */
8689 linkage = cp_parser_string_literal (parser, false, false);
8691 /* Transform the literal into an identifier. If the literal is a
8692 wide-character string, or contains embedded NULs, then we can't
8693 handle it as the user wants. */
8694 if (strlen (TREE_STRING_POINTER (linkage))
8695 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8697 cp_parser_error (parser, "invalid linkage-specification");
8698 /* Assume C++ linkage. */
8699 linkage = lang_name_cplusplus;
8702 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8704 /* We're now using the new linkage. */
8705 push_lang_context (linkage);
8707 /* If the next token is a `{', then we're using the first
8709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8711 /* Consume the `{' token. */
8712 cp_lexer_consume_token (parser->lexer);
8713 /* Parse the declarations. */
8714 cp_parser_declaration_seq_opt (parser);
8715 /* Look for the closing `}'. */
8716 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8718 /* Otherwise, there's just one declaration. */
8721 bool saved_in_unbraced_linkage_specification_p;
8723 saved_in_unbraced_linkage_specification_p
8724 = parser->in_unbraced_linkage_specification_p;
8725 parser->in_unbraced_linkage_specification_p = true;
8726 cp_parser_declaration (parser);
8727 parser->in_unbraced_linkage_specification_p
8728 = saved_in_unbraced_linkage_specification_p;
8731 /* We're done with the linkage-specification. */
8732 pop_lang_context ();
8735 /* Parse a static_assert-declaration.
8737 static_assert-declaration:
8738 static_assert ( constant-expression , string-literal ) ;
8740 If MEMBER_P, this static_assert is a class member. */
8743 cp_parser_static_assert(cp_parser *parser, bool member_p)
8748 location_t saved_loc;
8750 /* Peek at the `static_assert' token so we can keep track of exactly
8751 where the static assertion started. */
8752 token = cp_lexer_peek_token (parser->lexer);
8753 saved_loc = token->location;
8755 /* Look for the `static_assert' keyword. */
8756 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8757 "%<static_assert%>"))
8760 /* We know we are in a static assertion; commit to any tentative
8762 if (cp_parser_parsing_tentatively (parser))
8763 cp_parser_commit_to_tentative_parse (parser);
8765 /* Parse the `(' starting the static assertion condition. */
8766 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8768 /* Parse the constant-expression. */
8770 cp_parser_constant_expression (parser,
8771 /*allow_non_constant_p=*/false,
8772 /*non_constant_p=*/NULL);
8774 /* Parse the separating `,'. */
8775 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8777 /* Parse the string-literal message. */
8778 message = cp_parser_string_literal (parser,
8779 /*translate=*/false,
8782 /* A `)' completes the static assertion. */
8783 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8784 cp_parser_skip_to_closing_parenthesis (parser,
8785 /*recovering=*/true,
8787 /*consume_paren=*/true);
8789 /* A semicolon terminates the declaration. */
8790 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8792 /* Complete the static assertion, which may mean either processing
8793 the static assert now or saving it for template instantiation. */
8794 finish_static_assert (condition, message, saved_loc, member_p);
8797 /* Parse a `decltype' type. Returns the type.
8799 simple-type-specifier:
8800 decltype ( expression ) */
8803 cp_parser_decltype (cp_parser *parser)
8806 bool id_expression_or_member_access_p = false;
8807 const char *saved_message;
8808 bool saved_integral_constant_expression_p;
8809 bool saved_non_integral_constant_expression_p;
8810 cp_token *id_expr_start_token;
8812 /* Look for the `decltype' token. */
8813 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8814 return error_mark_node;
8816 /* Types cannot be defined in a `decltype' expression. Save away the
8818 saved_message = parser->type_definition_forbidden_message;
8820 /* And create the new one. */
8821 parser->type_definition_forbidden_message
8822 = "types may not be defined in %<decltype%> expressions";
8824 /* The restrictions on constant-expressions do not apply inside
8825 decltype expressions. */
8826 saved_integral_constant_expression_p
8827 = parser->integral_constant_expression_p;
8828 saved_non_integral_constant_expression_p
8829 = parser->non_integral_constant_expression_p;
8830 parser->integral_constant_expression_p = false;
8832 /* Do not actually evaluate the expression. */
8835 /* Parse the opening `('. */
8836 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8837 return error_mark_node;
8839 /* First, try parsing an id-expression. */
8840 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8841 cp_parser_parse_tentatively (parser);
8842 expr = cp_parser_id_expression (parser,
8843 /*template_keyword_p=*/false,
8844 /*check_dependency_p=*/true,
8845 /*template_p=*/NULL,
8846 /*declarator_p=*/false,
8847 /*optional_p=*/false);
8849 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8851 bool non_integral_constant_expression_p = false;
8852 tree id_expression = expr;
8854 const char *error_msg;
8856 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8857 /* Lookup the name we got back from the id-expression. */
8858 expr = cp_parser_lookup_name (parser, expr,
8860 /*is_template=*/false,
8861 /*is_namespace=*/false,
8862 /*check_dependency=*/true,
8863 /*ambiguous_decls=*/NULL,
8864 id_expr_start_token->location);
8867 && expr != error_mark_node
8868 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8869 && TREE_CODE (expr) != TYPE_DECL
8870 && (TREE_CODE (expr) != BIT_NOT_EXPR
8871 || !TYPE_P (TREE_OPERAND (expr, 0)))
8872 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8874 /* Complete lookup of the id-expression. */
8875 expr = (finish_id_expression
8876 (id_expression, expr, parser->scope, &idk,
8877 /*integral_constant_expression_p=*/false,
8878 /*allow_non_integral_constant_expression_p=*/true,
8879 &non_integral_constant_expression_p,
8880 /*template_p=*/false,
8882 /*address_p=*/false,
8883 /*template_arg_p=*/false,
8885 id_expr_start_token->location));
8887 if (expr == error_mark_node)
8888 /* We found an id-expression, but it was something that we
8889 should not have found. This is an error, not something
8890 we can recover from, so note that we found an
8891 id-expression and we'll recover as gracefully as
8893 id_expression_or_member_access_p = true;
8897 && expr != error_mark_node
8898 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8899 /* We have an id-expression. */
8900 id_expression_or_member_access_p = true;
8903 if (!id_expression_or_member_access_p)
8905 /* Abort the id-expression parse. */
8906 cp_parser_abort_tentative_parse (parser);
8908 /* Parsing tentatively, again. */
8909 cp_parser_parse_tentatively (parser);
8911 /* Parse a class member access. */
8912 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8914 /*member_access_only_p=*/true, NULL);
8917 && expr != error_mark_node
8918 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8919 /* We have an id-expression. */
8920 id_expression_or_member_access_p = true;
8923 if (id_expression_or_member_access_p)
8924 /* We have parsed the complete id-expression or member access. */
8925 cp_parser_parse_definitely (parser);
8928 bool saved_greater_than_is_operator_p;
8930 /* Abort our attempt to parse an id-expression or member access
8932 cp_parser_abort_tentative_parse (parser);
8934 /* Within a parenthesized expression, a `>' token is always
8935 the greater-than operator. */
8936 saved_greater_than_is_operator_p
8937 = parser->greater_than_is_operator_p;
8938 parser->greater_than_is_operator_p = true;
8940 /* Parse a full expression. */
8941 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8943 /* The `>' token might be the end of a template-id or
8944 template-parameter-list now. */
8945 parser->greater_than_is_operator_p
8946 = saved_greater_than_is_operator_p;
8949 /* Go back to evaluating expressions. */
8952 /* Restore the old message and the integral constant expression
8954 parser->type_definition_forbidden_message = saved_message;
8955 parser->integral_constant_expression_p
8956 = saved_integral_constant_expression_p;
8957 parser->non_integral_constant_expression_p
8958 = saved_non_integral_constant_expression_p;
8960 if (expr == error_mark_node)
8962 /* Skip everything up to the closing `)'. */
8963 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8964 /*consume_paren=*/true);
8965 return error_mark_node;
8968 /* Parse to the closing `)'. */
8969 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8971 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8972 /*consume_paren=*/true);
8973 return error_mark_node;
8976 return finish_decltype_type (expr, id_expression_or_member_access_p);
8979 /* Special member functions [gram.special] */
8981 /* Parse a conversion-function-id.
8983 conversion-function-id:
8984 operator conversion-type-id
8986 Returns an IDENTIFIER_NODE representing the operator. */
8989 cp_parser_conversion_function_id (cp_parser* parser)
8993 tree saved_qualifying_scope;
8994 tree saved_object_scope;
8995 tree pushed_scope = NULL_TREE;
8997 /* Look for the `operator' token. */
8998 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8999 return error_mark_node;
9000 /* When we parse the conversion-type-id, the current scope will be
9001 reset. However, we need that information in able to look up the
9002 conversion function later, so we save it here. */
9003 saved_scope = parser->scope;
9004 saved_qualifying_scope = parser->qualifying_scope;
9005 saved_object_scope = parser->object_scope;
9006 /* We must enter the scope of the class so that the names of
9007 entities declared within the class are available in the
9008 conversion-type-id. For example, consider:
9015 S::operator I() { ... }
9017 In order to see that `I' is a type-name in the definition, we
9018 must be in the scope of `S'. */
9020 pushed_scope = push_scope (saved_scope);
9021 /* Parse the conversion-type-id. */
9022 type = cp_parser_conversion_type_id (parser);
9023 /* Leave the scope of the class, if any. */
9025 pop_scope (pushed_scope);
9026 /* Restore the saved scope. */
9027 parser->scope = saved_scope;
9028 parser->qualifying_scope = saved_qualifying_scope;
9029 parser->object_scope = saved_object_scope;
9030 /* If the TYPE is invalid, indicate failure. */
9031 if (type == error_mark_node)
9032 return error_mark_node;
9033 return mangle_conv_op_name_for_type (type);
9036 /* Parse a conversion-type-id:
9039 type-specifier-seq conversion-declarator [opt]
9041 Returns the TYPE specified. */
9044 cp_parser_conversion_type_id (cp_parser* parser)
9047 cp_decl_specifier_seq type_specifiers;
9048 cp_declarator *declarator;
9049 tree type_specified;
9051 /* Parse the attributes. */
9052 attributes = cp_parser_attributes_opt (parser);
9053 /* Parse the type-specifiers. */
9054 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9055 /*is_trailing_return=*/false,
9057 /* If that didn't work, stop. */
9058 if (type_specifiers.type == error_mark_node)
9059 return error_mark_node;
9060 /* Parse the conversion-declarator. */
9061 declarator = cp_parser_conversion_declarator_opt (parser);
9063 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9064 /*initialized=*/0, &attributes);
9066 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9068 /* Don't give this error when parsing tentatively. This happens to
9069 work because we always parse this definitively once. */
9070 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9071 && type_uses_auto (type_specified))
9073 error ("invalid use of %<auto%> in conversion operator");
9074 return error_mark_node;
9077 return type_specified;
9080 /* Parse an (optional) conversion-declarator.
9082 conversion-declarator:
9083 ptr-operator conversion-declarator [opt]
9087 static cp_declarator *
9088 cp_parser_conversion_declarator_opt (cp_parser* parser)
9090 enum tree_code code;
9092 cp_cv_quals cv_quals;
9094 /* We don't know if there's a ptr-operator next, or not. */
9095 cp_parser_parse_tentatively (parser);
9096 /* Try the ptr-operator. */
9097 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9098 /* If it worked, look for more conversion-declarators. */
9099 if (cp_parser_parse_definitely (parser))
9101 cp_declarator *declarator;
9103 /* Parse another optional declarator. */
9104 declarator = cp_parser_conversion_declarator_opt (parser);
9106 return cp_parser_make_indirect_declarator
9107 (code, class_type, cv_quals, declarator);
9113 /* Parse an (optional) ctor-initializer.
9116 : mem-initializer-list
9118 Returns TRUE iff the ctor-initializer was actually present. */
9121 cp_parser_ctor_initializer_opt (cp_parser* parser)
9123 /* If the next token is not a `:', then there is no
9124 ctor-initializer. */
9125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9127 /* Do default initialization of any bases and members. */
9128 if (DECL_CONSTRUCTOR_P (current_function_decl))
9129 finish_mem_initializers (NULL_TREE);
9134 /* Consume the `:' token. */
9135 cp_lexer_consume_token (parser->lexer);
9136 /* And the mem-initializer-list. */
9137 cp_parser_mem_initializer_list (parser);
9142 /* Parse a mem-initializer-list.
9144 mem-initializer-list:
9145 mem-initializer ... [opt]
9146 mem-initializer ... [opt] , mem-initializer-list */
9149 cp_parser_mem_initializer_list (cp_parser* parser)
9151 tree mem_initializer_list = NULL_TREE;
9152 cp_token *token = cp_lexer_peek_token (parser->lexer);
9154 /* Let the semantic analysis code know that we are starting the
9155 mem-initializer-list. */
9156 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9157 error ("%Honly constructors take base initializers",
9160 /* Loop through the list. */
9163 tree mem_initializer;
9165 token = cp_lexer_peek_token (parser->lexer);
9166 /* Parse the mem-initializer. */
9167 mem_initializer = cp_parser_mem_initializer (parser);
9168 /* If the next token is a `...', we're expanding member initializers. */
9169 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9171 /* Consume the `...'. */
9172 cp_lexer_consume_token (parser->lexer);
9174 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9175 can be expanded but members cannot. */
9176 if (mem_initializer != error_mark_node
9177 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9179 error ("%Hcannot expand initializer for member %<%D%>",
9180 &token->location, TREE_PURPOSE (mem_initializer));
9181 mem_initializer = error_mark_node;
9184 /* Construct the pack expansion type. */
9185 if (mem_initializer != error_mark_node)
9186 mem_initializer = make_pack_expansion (mem_initializer);
9188 /* Add it to the list, unless it was erroneous. */
9189 if (mem_initializer != error_mark_node)
9191 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9192 mem_initializer_list = mem_initializer;
9194 /* If the next token is not a `,', we're done. */
9195 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9197 /* Consume the `,' token. */
9198 cp_lexer_consume_token (parser->lexer);
9201 /* Perform semantic analysis. */
9202 if (DECL_CONSTRUCTOR_P (current_function_decl))
9203 finish_mem_initializers (mem_initializer_list);
9206 /* Parse a mem-initializer.
9209 mem-initializer-id ( expression-list [opt] )
9210 mem-initializer-id braced-init-list
9215 ( expression-list [opt] )
9217 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9218 class) or FIELD_DECL (for a non-static data member) to initialize;
9219 the TREE_VALUE is the expression-list. An empty initialization
9220 list is represented by void_list_node. */
9223 cp_parser_mem_initializer (cp_parser* parser)
9225 tree mem_initializer_id;
9226 tree expression_list;
9228 cp_token *token = cp_lexer_peek_token (parser->lexer);
9230 /* Find out what is being initialized. */
9231 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9233 permerror (token->location,
9234 "anachronistic old-style base class initializer");
9235 mem_initializer_id = NULL_TREE;
9239 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9240 if (mem_initializer_id == error_mark_node)
9241 return mem_initializer_id;
9243 member = expand_member_init (mem_initializer_id);
9244 if (member && !DECL_P (member))
9245 in_base_initializer = 1;
9247 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9249 bool expr_non_constant_p;
9250 maybe_warn_cpp0x ("extended initializer lists");
9251 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9252 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9253 expression_list = build_tree_list (NULL_TREE, expression_list);
9257 = cp_parser_parenthesized_expression_list (parser, false,
9259 /*allow_expansion_p=*/true,
9260 /*non_constant_p=*/NULL);
9261 if (expression_list == error_mark_node)
9262 return error_mark_node;
9263 if (!expression_list)
9264 expression_list = void_type_node;
9266 in_base_initializer = 0;
9268 return member ? build_tree_list (member, expression_list) : error_mark_node;
9271 /* Parse a mem-initializer-id.
9274 :: [opt] nested-name-specifier [opt] class-name
9277 Returns a TYPE indicating the class to be initializer for the first
9278 production. Returns an IDENTIFIER_NODE indicating the data member
9279 to be initialized for the second production. */
9282 cp_parser_mem_initializer_id (cp_parser* parser)
9284 bool global_scope_p;
9285 bool nested_name_specifier_p;
9286 bool template_p = false;
9289 cp_token *token = cp_lexer_peek_token (parser->lexer);
9291 /* `typename' is not allowed in this context ([temp.res]). */
9292 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9294 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9295 "member initializer is implicitly a type)",
9297 cp_lexer_consume_token (parser->lexer);
9299 /* Look for the optional `::' operator. */
9301 = (cp_parser_global_scope_opt (parser,
9302 /*current_scope_valid_p=*/false)
9304 /* Look for the optional nested-name-specifier. The simplest way to
9309 The keyword `typename' is not permitted in a base-specifier or
9310 mem-initializer; in these contexts a qualified name that
9311 depends on a template-parameter is implicitly assumed to be a
9314 is to assume that we have seen the `typename' keyword at this
9316 nested_name_specifier_p
9317 = (cp_parser_nested_name_specifier_opt (parser,
9318 /*typename_keyword_p=*/true,
9319 /*check_dependency_p=*/true,
9321 /*is_declaration=*/true)
9323 if (nested_name_specifier_p)
9324 template_p = cp_parser_optional_template_keyword (parser);
9325 /* If there is a `::' operator or a nested-name-specifier, then we
9326 are definitely looking for a class-name. */
9327 if (global_scope_p || nested_name_specifier_p)
9328 return cp_parser_class_name (parser,
9329 /*typename_keyword_p=*/true,
9330 /*template_keyword_p=*/template_p,
9332 /*check_dependency_p=*/true,
9333 /*class_head_p=*/false,
9334 /*is_declaration=*/true);
9335 /* Otherwise, we could also be looking for an ordinary identifier. */
9336 cp_parser_parse_tentatively (parser);
9337 /* Try a class-name. */
9338 id = cp_parser_class_name (parser,
9339 /*typename_keyword_p=*/true,
9340 /*template_keyword_p=*/false,
9342 /*check_dependency_p=*/true,
9343 /*class_head_p=*/false,
9344 /*is_declaration=*/true);
9345 /* If we found one, we're done. */
9346 if (cp_parser_parse_definitely (parser))
9348 /* Otherwise, look for an ordinary identifier. */
9349 return cp_parser_identifier (parser);
9352 /* Overloading [gram.over] */
9354 /* Parse an operator-function-id.
9356 operator-function-id:
9359 Returns an IDENTIFIER_NODE for the operator which is a
9360 human-readable spelling of the identifier, e.g., `operator +'. */
9363 cp_parser_operator_function_id (cp_parser* parser)
9365 /* Look for the `operator' keyword. */
9366 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9367 return error_mark_node;
9368 /* And then the name of the operator itself. */
9369 return cp_parser_operator (parser);
9372 /* Parse an operator.
9375 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9376 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9377 || ++ -- , ->* -> () []
9384 Returns an IDENTIFIER_NODE for the operator which is a
9385 human-readable spelling of the identifier, e.g., `operator +'. */
9388 cp_parser_operator (cp_parser* parser)
9390 tree id = NULL_TREE;
9393 /* Peek at the next token. */
9394 token = cp_lexer_peek_token (parser->lexer);
9395 /* Figure out which operator we have. */
9396 switch (token->type)
9402 /* The keyword should be either `new' or `delete'. */
9403 if (token->keyword == RID_NEW)
9405 else if (token->keyword == RID_DELETE)
9410 /* Consume the `new' or `delete' token. */
9411 cp_lexer_consume_token (parser->lexer);
9413 /* Peek at the next token. */
9414 token = cp_lexer_peek_token (parser->lexer);
9415 /* If it's a `[' token then this is the array variant of the
9417 if (token->type == CPP_OPEN_SQUARE)
9419 /* Consume the `[' token. */
9420 cp_lexer_consume_token (parser->lexer);
9421 /* Look for the `]' token. */
9422 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9423 id = ansi_opname (op == NEW_EXPR
9424 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9426 /* Otherwise, we have the non-array variant. */
9428 id = ansi_opname (op);
9434 id = ansi_opname (PLUS_EXPR);
9438 id = ansi_opname (MINUS_EXPR);
9442 id = ansi_opname (MULT_EXPR);
9446 id = ansi_opname (TRUNC_DIV_EXPR);
9450 id = ansi_opname (TRUNC_MOD_EXPR);
9454 id = ansi_opname (BIT_XOR_EXPR);
9458 id = ansi_opname (BIT_AND_EXPR);
9462 id = ansi_opname (BIT_IOR_EXPR);
9466 id = ansi_opname (BIT_NOT_EXPR);
9470 id = ansi_opname (TRUTH_NOT_EXPR);
9474 id = ansi_assopname (NOP_EXPR);
9478 id = ansi_opname (LT_EXPR);
9482 id = ansi_opname (GT_EXPR);
9486 id = ansi_assopname (PLUS_EXPR);
9490 id = ansi_assopname (MINUS_EXPR);
9494 id = ansi_assopname (MULT_EXPR);
9498 id = ansi_assopname (TRUNC_DIV_EXPR);
9502 id = ansi_assopname (TRUNC_MOD_EXPR);
9506 id = ansi_assopname (BIT_XOR_EXPR);
9510 id = ansi_assopname (BIT_AND_EXPR);
9514 id = ansi_assopname (BIT_IOR_EXPR);
9518 id = ansi_opname (LSHIFT_EXPR);
9522 id = ansi_opname (RSHIFT_EXPR);
9526 id = ansi_assopname (LSHIFT_EXPR);
9530 id = ansi_assopname (RSHIFT_EXPR);
9534 id = ansi_opname (EQ_EXPR);
9538 id = ansi_opname (NE_EXPR);
9542 id = ansi_opname (LE_EXPR);
9545 case CPP_GREATER_EQ:
9546 id = ansi_opname (GE_EXPR);
9550 id = ansi_opname (TRUTH_ANDIF_EXPR);
9554 id = ansi_opname (TRUTH_ORIF_EXPR);
9558 id = ansi_opname (POSTINCREMENT_EXPR);
9561 case CPP_MINUS_MINUS:
9562 id = ansi_opname (PREDECREMENT_EXPR);
9566 id = ansi_opname (COMPOUND_EXPR);
9569 case CPP_DEREF_STAR:
9570 id = ansi_opname (MEMBER_REF);
9574 id = ansi_opname (COMPONENT_REF);
9577 case CPP_OPEN_PAREN:
9578 /* Consume the `('. */
9579 cp_lexer_consume_token (parser->lexer);
9580 /* Look for the matching `)'. */
9581 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9582 return ansi_opname (CALL_EXPR);
9584 case CPP_OPEN_SQUARE:
9585 /* Consume the `['. */
9586 cp_lexer_consume_token (parser->lexer);
9587 /* Look for the matching `]'. */
9588 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9589 return ansi_opname (ARRAY_REF);
9592 /* Anything else is an error. */
9596 /* If we have selected an identifier, we need to consume the
9599 cp_lexer_consume_token (parser->lexer);
9600 /* Otherwise, no valid operator name was present. */
9603 cp_parser_error (parser, "expected operator");
9604 id = error_mark_node;
9610 /* Parse a template-declaration.
9612 template-declaration:
9613 export [opt] template < template-parameter-list > declaration
9615 If MEMBER_P is TRUE, this template-declaration occurs within a
9618 The grammar rule given by the standard isn't correct. What
9621 template-declaration:
9622 export [opt] template-parameter-list-seq
9623 decl-specifier-seq [opt] init-declarator [opt] ;
9624 export [opt] template-parameter-list-seq
9627 template-parameter-list-seq:
9628 template-parameter-list-seq [opt]
9629 template < template-parameter-list > */
9632 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9634 /* Check for `export'. */
9635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9637 /* Consume the `export' token. */
9638 cp_lexer_consume_token (parser->lexer);
9639 /* Warn that we do not support `export'. */
9640 warning (0, "keyword %<export%> not implemented, and will be ignored");
9643 cp_parser_template_declaration_after_export (parser, member_p);
9646 /* Parse a template-parameter-list.
9648 template-parameter-list:
9650 template-parameter-list , template-parameter
9652 Returns a TREE_LIST. Each node represents a template parameter.
9653 The nodes are connected via their TREE_CHAINs. */
9656 cp_parser_template_parameter_list (cp_parser* parser)
9658 tree parameter_list = NULL_TREE;
9660 begin_template_parm_list ();
9665 bool is_parameter_pack;
9667 /* Parse the template-parameter. */
9668 parameter = cp_parser_template_parameter (parser,
9670 &is_parameter_pack);
9671 /* Add it to the list. */
9672 if (parameter != error_mark_node)
9673 parameter_list = process_template_parm (parameter_list,
9679 tree err_parm = build_tree_list (parameter, parameter);
9680 TREE_VALUE (err_parm) = error_mark_node;
9681 parameter_list = chainon (parameter_list, err_parm);
9684 /* If the next token is not a `,', we're done. */
9685 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9687 /* Otherwise, consume the `,' token. */
9688 cp_lexer_consume_token (parser->lexer);
9691 return end_template_parm_list (parameter_list);
9694 /* Parse a template-parameter.
9698 parameter-declaration
9700 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9701 the parameter. The TREE_PURPOSE is the default value, if any.
9702 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9703 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9704 set to true iff this parameter is a parameter pack. */
9707 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9708 bool *is_parameter_pack)
9711 cp_parameter_declarator *parameter_declarator;
9712 cp_declarator *id_declarator;
9715 /* Assume it is a type parameter or a template parameter. */
9716 *is_non_type = false;
9717 /* Assume it not a parameter pack. */
9718 *is_parameter_pack = false;
9719 /* Peek at the next token. */
9720 token = cp_lexer_peek_token (parser->lexer);
9721 /* If it is `class' or `template', we have a type-parameter. */
9722 if (token->keyword == RID_TEMPLATE)
9723 return cp_parser_type_parameter (parser, is_parameter_pack);
9724 /* If it is `class' or `typename' we do not know yet whether it is a
9725 type parameter or a non-type parameter. Consider:
9727 template <typename T, typename T::X X> ...
9731 template <class C, class D*> ...
9733 Here, the first parameter is a type parameter, and the second is
9734 a non-type parameter. We can tell by looking at the token after
9735 the identifier -- if it is a `,', `=', or `>' then we have a type
9737 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9739 /* Peek at the token after `class' or `typename'. */
9740 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9741 /* If it's an ellipsis, we have a template type parameter
9743 if (token->type == CPP_ELLIPSIS)
9744 return cp_parser_type_parameter (parser, is_parameter_pack);
9745 /* If it's an identifier, skip it. */
9746 if (token->type == CPP_NAME)
9747 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9748 /* Now, see if the token looks like the end of a template
9750 if (token->type == CPP_COMMA
9751 || token->type == CPP_EQ
9752 || token->type == CPP_GREATER)
9753 return cp_parser_type_parameter (parser, is_parameter_pack);
9756 /* Otherwise, it is a non-type parameter.
9760 When parsing a default template-argument for a non-type
9761 template-parameter, the first non-nested `>' is taken as the end
9762 of the template parameter-list rather than a greater-than
9764 *is_non_type = true;
9765 parameter_declarator
9766 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9767 /*parenthesized_p=*/NULL);
9769 /* If the parameter declaration is marked as a parameter pack, set
9770 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9771 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9773 if (parameter_declarator
9774 && parameter_declarator->declarator
9775 && parameter_declarator->declarator->parameter_pack_p)
9777 *is_parameter_pack = true;
9778 parameter_declarator->declarator->parameter_pack_p = false;
9781 /* If the next token is an ellipsis, and we don't already have it
9782 marked as a parameter pack, then we have a parameter pack (that
9783 has no declarator). */
9784 if (!*is_parameter_pack
9785 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9786 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9788 /* Consume the `...'. */
9789 cp_lexer_consume_token (parser->lexer);
9790 maybe_warn_variadic_templates ();
9792 *is_parameter_pack = true;
9794 /* We might end up with a pack expansion as the type of the non-type
9795 template parameter, in which case this is a non-type template
9797 else if (parameter_declarator
9798 && parameter_declarator->decl_specifiers.type
9799 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9801 *is_parameter_pack = true;
9802 parameter_declarator->decl_specifiers.type =
9803 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9806 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9808 /* Parameter packs cannot have default arguments. However, a
9809 user may try to do so, so we'll parse them and give an
9810 appropriate diagnostic here. */
9812 /* Consume the `='. */
9813 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9814 cp_lexer_consume_token (parser->lexer);
9816 /* Find the name of the parameter pack. */
9817 id_declarator = parameter_declarator->declarator;
9818 while (id_declarator && id_declarator->kind != cdk_id)
9819 id_declarator = id_declarator->declarator;
9821 if (id_declarator && id_declarator->kind == cdk_id)
9822 error ("%Htemplate parameter pack %qD cannot have a default argument",
9823 &start_token->location, id_declarator->u.id.unqualified_name);
9825 error ("%Htemplate parameter pack cannot have a default argument",
9826 &start_token->location);
9828 /* Parse the default argument, but throw away the result. */
9829 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9832 parm = grokdeclarator (parameter_declarator->declarator,
9833 ¶meter_declarator->decl_specifiers,
9834 PARM, /*initialized=*/0,
9836 if (parm == error_mark_node)
9837 return error_mark_node;
9839 return build_tree_list (parameter_declarator->default_argument, parm);
9842 /* Parse a type-parameter.
9845 class identifier [opt]
9846 class identifier [opt] = type-id
9847 typename identifier [opt]
9848 typename identifier [opt] = type-id
9849 template < template-parameter-list > class identifier [opt]
9850 template < template-parameter-list > class identifier [opt]
9853 GNU Extension (variadic templates):
9856 class ... identifier [opt]
9857 typename ... identifier [opt]
9859 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9860 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9861 the declaration of the parameter.
9863 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9866 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9871 /* Look for a keyword to tell us what kind of parameter this is. */
9872 token = cp_parser_require (parser, CPP_KEYWORD,
9873 "%<class%>, %<typename%>, or %<template%>");
9875 return error_mark_node;
9877 switch (token->keyword)
9883 tree default_argument;
9885 /* If the next token is an ellipsis, we have a template
9887 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9889 /* Consume the `...' token. */
9890 cp_lexer_consume_token (parser->lexer);
9891 maybe_warn_variadic_templates ();
9893 *is_parameter_pack = true;
9896 /* If the next token is an identifier, then it names the
9898 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9899 identifier = cp_parser_identifier (parser);
9901 identifier = NULL_TREE;
9903 /* Create the parameter. */
9904 parameter = finish_template_type_parm (class_type_node, identifier);
9906 /* If the next token is an `=', we have a default argument. */
9907 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9909 /* Consume the `=' token. */
9910 cp_lexer_consume_token (parser->lexer);
9911 /* Parse the default-argument. */
9912 push_deferring_access_checks (dk_no_deferred);
9913 default_argument = cp_parser_type_id (parser);
9915 /* Template parameter packs cannot have default
9917 if (*is_parameter_pack)
9920 error ("%Htemplate parameter pack %qD cannot have a "
9921 "default argument", &token->location, identifier);
9923 error ("%Htemplate parameter packs cannot have "
9924 "default arguments", &token->location);
9925 default_argument = NULL_TREE;
9927 pop_deferring_access_checks ();
9930 default_argument = NULL_TREE;
9932 /* Create the combined representation of the parameter and the
9933 default argument. */
9934 parameter = build_tree_list (default_argument, parameter);
9940 tree parameter_list;
9942 tree default_argument;
9944 /* Look for the `<'. */
9945 cp_parser_require (parser, CPP_LESS, "%<<%>");
9946 /* Parse the template-parameter-list. */
9947 parameter_list = cp_parser_template_parameter_list (parser);
9948 /* Look for the `>'. */
9949 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9950 /* Look for the `class' keyword. */
9951 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9952 /* If the next token is an ellipsis, we have a template
9954 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9956 /* Consume the `...' token. */
9957 cp_lexer_consume_token (parser->lexer);
9958 maybe_warn_variadic_templates ();
9960 *is_parameter_pack = true;
9962 /* If the next token is an `=', then there is a
9963 default-argument. If the next token is a `>', we are at
9964 the end of the parameter-list. If the next token is a `,',
9965 then we are at the end of this parameter. */
9966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9967 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9968 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9970 identifier = cp_parser_identifier (parser);
9971 /* Treat invalid names as if the parameter were nameless. */
9972 if (identifier == error_mark_node)
9973 identifier = NULL_TREE;
9976 identifier = NULL_TREE;
9978 /* Create the template parameter. */
9979 parameter = finish_template_template_parm (class_type_node,
9982 /* If the next token is an `=', then there is a
9983 default-argument. */
9984 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9988 /* Consume the `='. */
9989 cp_lexer_consume_token (parser->lexer);
9990 /* Parse the id-expression. */
9991 push_deferring_access_checks (dk_no_deferred);
9992 /* save token before parsing the id-expression, for error
9994 token = cp_lexer_peek_token (parser->lexer);
9996 = cp_parser_id_expression (parser,
9997 /*template_keyword_p=*/false,
9998 /*check_dependency_p=*/true,
9999 /*template_p=*/&is_template,
10000 /*declarator_p=*/false,
10001 /*optional_p=*/false);
10002 if (TREE_CODE (default_argument) == TYPE_DECL)
10003 /* If the id-expression was a template-id that refers to
10004 a template-class, we already have the declaration here,
10005 so no further lookup is needed. */
10008 /* Look up the name. */
10010 = cp_parser_lookup_name (parser, default_argument,
10012 /*is_template=*/is_template,
10013 /*is_namespace=*/false,
10014 /*check_dependency=*/true,
10015 /*ambiguous_decls=*/NULL,
10017 /* See if the default argument is valid. */
10019 = check_template_template_default_arg (default_argument);
10021 /* Template parameter packs cannot have default
10023 if (*is_parameter_pack)
10026 error ("%Htemplate parameter pack %qD cannot "
10027 "have a default argument",
10028 &token->location, identifier);
10030 error ("%Htemplate parameter packs cannot "
10031 "have default arguments",
10033 default_argument = NULL_TREE;
10035 pop_deferring_access_checks ();
10038 default_argument = NULL_TREE;
10040 /* Create the combined representation of the parameter and the
10041 default argument. */
10042 parameter = build_tree_list (default_argument, parameter);
10047 gcc_unreachable ();
10054 /* Parse a template-id.
10057 template-name < template-argument-list [opt] >
10059 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10060 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10061 returned. Otherwise, if the template-name names a function, or set
10062 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10063 names a class, returns a TYPE_DECL for the specialization.
10065 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10066 uninstantiated templates. */
10069 cp_parser_template_id (cp_parser *parser,
10070 bool template_keyword_p,
10071 bool check_dependency_p,
10072 bool is_declaration)
10078 cp_token_position start_of_id = 0;
10079 deferred_access_check *chk;
10080 VEC (deferred_access_check,gc) *access_check;
10081 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10082 bool is_identifier;
10084 /* If the next token corresponds to a template-id, there is no need
10086 next_token = cp_lexer_peek_token (parser->lexer);
10087 if (next_token->type == CPP_TEMPLATE_ID)
10089 struct tree_check *check_value;
10091 /* Get the stored value. */
10092 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10093 /* Perform any access checks that were deferred. */
10094 access_check = check_value->checks;
10098 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10101 perform_or_defer_access_check (chk->binfo,
10106 /* Return the stored value. */
10107 return check_value->value;
10110 /* Avoid performing name lookup if there is no possibility of
10111 finding a template-id. */
10112 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10113 || (next_token->type == CPP_NAME
10114 && !cp_parser_nth_token_starts_template_argument_list_p
10117 cp_parser_error (parser, "expected template-id");
10118 return error_mark_node;
10121 /* Remember where the template-id starts. */
10122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10123 start_of_id = cp_lexer_token_position (parser->lexer, false);
10125 push_deferring_access_checks (dk_deferred);
10127 /* Parse the template-name. */
10128 is_identifier = false;
10129 token = cp_lexer_peek_token (parser->lexer);
10130 templ = cp_parser_template_name (parser, template_keyword_p,
10131 check_dependency_p,
10134 if (templ == error_mark_node || is_identifier)
10136 pop_deferring_access_checks ();
10140 /* If we find the sequence `[:' after a template-name, it's probably
10141 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10142 parse correctly the argument list. */
10143 next_token = cp_lexer_peek_token (parser->lexer);
10144 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10145 if (next_token->type == CPP_OPEN_SQUARE
10146 && next_token->flags & DIGRAPH
10147 && next_token_2->type == CPP_COLON
10148 && !(next_token_2->flags & PREV_WHITE))
10150 cp_parser_parse_tentatively (parser);
10151 /* Change `:' into `::'. */
10152 next_token_2->type = CPP_SCOPE;
10153 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10155 cp_lexer_consume_token (parser->lexer);
10157 /* Parse the arguments. */
10158 arguments = cp_parser_enclosed_template_argument_list (parser);
10159 if (!cp_parser_parse_definitely (parser))
10161 /* If we couldn't parse an argument list, then we revert our changes
10162 and return simply an error. Maybe this is not a template-id
10164 next_token_2->type = CPP_COLON;
10165 cp_parser_error (parser, "expected %<<%>");
10166 pop_deferring_access_checks ();
10167 return error_mark_node;
10169 /* Otherwise, emit an error about the invalid digraph, but continue
10170 parsing because we got our argument list. */
10171 if (permerror (next_token->location,
10172 "%<<::%> cannot begin a template-argument list"))
10174 static bool hint = false;
10175 inform (next_token->location,
10176 "%<<:%> is an alternate spelling for %<[%>."
10177 " Insert whitespace between %<<%> and %<::%>");
10178 if (!hint && !flag_permissive)
10180 inform (next_token->location, "(if you use %<-fpermissive%>"
10181 " G++ will accept your code)");
10188 /* Look for the `<' that starts the template-argument-list. */
10189 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10191 pop_deferring_access_checks ();
10192 return error_mark_node;
10194 /* Parse the arguments. */
10195 arguments = cp_parser_enclosed_template_argument_list (parser);
10198 /* Build a representation of the specialization. */
10199 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10200 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10201 else if (DECL_CLASS_TEMPLATE_P (templ)
10202 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10204 bool entering_scope;
10205 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10206 template (rather than some instantiation thereof) only if
10207 is not nested within some other construct. For example, in
10208 "template <typename T> void f(T) { A<T>::", A<T> is just an
10209 instantiation of A. */
10210 entering_scope = (template_parm_scope_p ()
10211 && cp_lexer_next_token_is (parser->lexer,
10214 = finish_template_type (templ, arguments, entering_scope);
10218 /* If it's not a class-template or a template-template, it should be
10219 a function-template. */
10220 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10221 || TREE_CODE (templ) == OVERLOAD
10222 || BASELINK_P (templ)));
10224 template_id = lookup_template_function (templ, arguments);
10227 /* If parsing tentatively, replace the sequence of tokens that makes
10228 up the template-id with a CPP_TEMPLATE_ID token. That way,
10229 should we re-parse the token stream, we will not have to repeat
10230 the effort required to do the parse, nor will we issue duplicate
10231 error messages about problems during instantiation of the
10235 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10237 /* Reset the contents of the START_OF_ID token. */
10238 token->type = CPP_TEMPLATE_ID;
10239 /* Retrieve any deferred checks. Do not pop this access checks yet
10240 so the memory will not be reclaimed during token replacing below. */
10241 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10242 token->u.tree_check_value->value = template_id;
10243 token->u.tree_check_value->checks = get_deferred_access_checks ();
10244 token->keyword = RID_MAX;
10246 /* Purge all subsequent tokens. */
10247 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10249 /* ??? Can we actually assume that, if template_id ==
10250 error_mark_node, we will have issued a diagnostic to the
10251 user, as opposed to simply marking the tentative parse as
10253 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10254 error ("%Hparse error in template argument list",
10258 pop_deferring_access_checks ();
10259 return template_id;
10262 /* Parse a template-name.
10267 The standard should actually say:
10271 operator-function-id
10273 A defect report has been filed about this issue.
10275 A conversion-function-id cannot be a template name because they cannot
10276 be part of a template-id. In fact, looking at this code:
10278 a.operator K<int>()
10280 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10281 It is impossible to call a templated conversion-function-id with an
10282 explicit argument list, since the only allowed template parameter is
10283 the type to which it is converting.
10285 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10286 `template' keyword, in a construction like:
10290 In that case `f' is taken to be a template-name, even though there
10291 is no way of knowing for sure.
10293 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10294 name refers to a set of overloaded functions, at least one of which
10295 is a template, or an IDENTIFIER_NODE with the name of the template,
10296 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10297 names are looked up inside uninstantiated templates. */
10300 cp_parser_template_name (cp_parser* parser,
10301 bool template_keyword_p,
10302 bool check_dependency_p,
10303 bool is_declaration,
10304 bool *is_identifier)
10309 cp_token *token = cp_lexer_peek_token (parser->lexer);
10311 /* If the next token is `operator', then we have either an
10312 operator-function-id or a conversion-function-id. */
10313 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10315 /* We don't know whether we're looking at an
10316 operator-function-id or a conversion-function-id. */
10317 cp_parser_parse_tentatively (parser);
10318 /* Try an operator-function-id. */
10319 identifier = cp_parser_operator_function_id (parser);
10320 /* If that didn't work, try a conversion-function-id. */
10321 if (!cp_parser_parse_definitely (parser))
10323 cp_parser_error (parser, "expected template-name");
10324 return error_mark_node;
10327 /* Look for the identifier. */
10329 identifier = cp_parser_identifier (parser);
10331 /* If we didn't find an identifier, we don't have a template-id. */
10332 if (identifier == error_mark_node)
10333 return error_mark_node;
10335 /* If the name immediately followed the `template' keyword, then it
10336 is a template-name. However, if the next token is not `<', then
10337 we do not treat it as a template-name, since it is not being used
10338 as part of a template-id. This enables us to handle constructs
10341 template <typename T> struct S { S(); };
10342 template <typename T> S<T>::S();
10344 correctly. We would treat `S' as a template -- if it were `S<T>'
10345 -- but we do not if there is no `<'. */
10347 if (processing_template_decl
10348 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10350 /* In a declaration, in a dependent context, we pretend that the
10351 "template" keyword was present in order to improve error
10352 recovery. For example, given:
10354 template <typename T> void f(T::X<int>);
10356 we want to treat "X<int>" as a template-id. */
10358 && !template_keyword_p
10359 && parser->scope && TYPE_P (parser->scope)
10360 && check_dependency_p
10361 && dependent_scope_p (parser->scope)
10362 /* Do not do this for dtors (or ctors), since they never
10363 need the template keyword before their name. */
10364 && !constructor_name_p (identifier, parser->scope))
10366 cp_token_position start = 0;
10368 /* Explain what went wrong. */
10369 error ("%Hnon-template %qD used as template",
10370 &token->location, identifier);
10371 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10372 parser->scope, identifier);
10373 /* If parsing tentatively, find the location of the "<" token. */
10374 if (cp_parser_simulate_error (parser))
10375 start = cp_lexer_token_position (parser->lexer, true);
10376 /* Parse the template arguments so that we can issue error
10377 messages about them. */
10378 cp_lexer_consume_token (parser->lexer);
10379 cp_parser_enclosed_template_argument_list (parser);
10380 /* Skip tokens until we find a good place from which to
10381 continue parsing. */
10382 cp_parser_skip_to_closing_parenthesis (parser,
10383 /*recovering=*/true,
10385 /*consume_paren=*/false);
10386 /* If parsing tentatively, permanently remove the
10387 template argument list. That will prevent duplicate
10388 error messages from being issued about the missing
10389 "template" keyword. */
10391 cp_lexer_purge_tokens_after (parser->lexer, start);
10393 *is_identifier = true;
10397 /* If the "template" keyword is present, then there is generally
10398 no point in doing name-lookup, so we just return IDENTIFIER.
10399 But, if the qualifying scope is non-dependent then we can
10400 (and must) do name-lookup normally. */
10401 if (template_keyword_p
10403 || (TYPE_P (parser->scope)
10404 && dependent_type_p (parser->scope))))
10408 /* Look up the name. */
10409 decl = cp_parser_lookup_name (parser, identifier,
10411 /*is_template=*/false,
10412 /*is_namespace=*/false,
10413 check_dependency_p,
10414 /*ambiguous_decls=*/NULL,
10416 decl = maybe_get_template_decl_from_type_decl (decl);
10418 /* If DECL is a template, then the name was a template-name. */
10419 if (TREE_CODE (decl) == TEMPLATE_DECL)
10423 tree fn = NULL_TREE;
10425 /* The standard does not explicitly indicate whether a name that
10426 names a set of overloaded declarations, some of which are
10427 templates, is a template-name. However, such a name should
10428 be a template-name; otherwise, there is no way to form a
10429 template-id for the overloaded templates. */
10430 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10431 if (TREE_CODE (fns) == OVERLOAD)
10432 for (fn = fns; fn; fn = OVL_NEXT (fn))
10433 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10438 /* The name does not name a template. */
10439 cp_parser_error (parser, "expected template-name");
10440 return error_mark_node;
10444 /* If DECL is dependent, and refers to a function, then just return
10445 its name; we will look it up again during template instantiation. */
10446 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10448 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10449 if (TYPE_P (scope) && dependent_type_p (scope))
10456 /* Parse a template-argument-list.
10458 template-argument-list:
10459 template-argument ... [opt]
10460 template-argument-list , template-argument ... [opt]
10462 Returns a TREE_VEC containing the arguments. */
10465 cp_parser_template_argument_list (cp_parser* parser)
10467 tree fixed_args[10];
10468 unsigned n_args = 0;
10469 unsigned alloced = 10;
10470 tree *arg_ary = fixed_args;
10472 bool saved_in_template_argument_list_p;
10474 bool saved_non_ice_p;
10476 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10477 parser->in_template_argument_list_p = true;
10478 /* Even if the template-id appears in an integral
10479 constant-expression, the contents of the argument list do
10481 saved_ice_p = parser->integral_constant_expression_p;
10482 parser->integral_constant_expression_p = false;
10483 saved_non_ice_p = parser->non_integral_constant_expression_p;
10484 parser->non_integral_constant_expression_p = false;
10485 /* Parse the arguments. */
10491 /* Consume the comma. */
10492 cp_lexer_consume_token (parser->lexer);
10494 /* Parse the template-argument. */
10495 argument = cp_parser_template_argument (parser);
10497 /* If the next token is an ellipsis, we're expanding a template
10499 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10501 if (argument == error_mark_node)
10503 cp_token *token = cp_lexer_peek_token (parser->lexer);
10504 error ("%Hexpected parameter pack before %<...%>",
10507 /* Consume the `...' token. */
10508 cp_lexer_consume_token (parser->lexer);
10510 /* Make the argument into a TYPE_PACK_EXPANSION or
10511 EXPR_PACK_EXPANSION. */
10512 argument = make_pack_expansion (argument);
10515 if (n_args == alloced)
10519 if (arg_ary == fixed_args)
10521 arg_ary = XNEWVEC (tree, alloced);
10522 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10525 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10527 arg_ary[n_args++] = argument;
10529 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10531 vec = make_tree_vec (n_args);
10534 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10536 if (arg_ary != fixed_args)
10538 parser->non_integral_constant_expression_p = saved_non_ice_p;
10539 parser->integral_constant_expression_p = saved_ice_p;
10540 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10544 /* Parse a template-argument.
10547 assignment-expression
10551 The representation is that of an assignment-expression, type-id, or
10552 id-expression -- except that the qualified id-expression is
10553 evaluated, so that the value returned is either a DECL or an
10556 Although the standard says "assignment-expression", it forbids
10557 throw-expressions or assignments in the template argument.
10558 Therefore, we use "conditional-expression" instead. */
10561 cp_parser_template_argument (cp_parser* parser)
10566 bool maybe_type_id = false;
10567 cp_token *token = NULL, *argument_start_token = NULL;
10570 /* There's really no way to know what we're looking at, so we just
10571 try each alternative in order.
10575 In a template-argument, an ambiguity between a type-id and an
10576 expression is resolved to a type-id, regardless of the form of
10577 the corresponding template-parameter.
10579 Therefore, we try a type-id first. */
10580 cp_parser_parse_tentatively (parser);
10581 argument = cp_parser_template_type_arg (parser);
10582 /* If there was no error parsing the type-id but the next token is a
10583 '>>', our behavior depends on which dialect of C++ we're
10584 parsing. In C++98, we probably found a typo for '> >'. But there
10585 are type-id which are also valid expressions. For instance:
10587 struct X { int operator >> (int); };
10588 template <int V> struct Foo {};
10591 Here 'X()' is a valid type-id of a function type, but the user just
10592 wanted to write the expression "X() >> 5". Thus, we remember that we
10593 found a valid type-id, but we still try to parse the argument as an
10594 expression to see what happens.
10596 In C++0x, the '>>' will be considered two separate '>'
10598 if (!cp_parser_error_occurred (parser)
10599 && cxx_dialect == cxx98
10600 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10602 maybe_type_id = true;
10603 cp_parser_abort_tentative_parse (parser);
10607 /* If the next token isn't a `,' or a `>', then this argument wasn't
10608 really finished. This means that the argument is not a valid
10610 if (!cp_parser_next_token_ends_template_argument_p (parser))
10611 cp_parser_error (parser, "expected template-argument");
10612 /* If that worked, we're done. */
10613 if (cp_parser_parse_definitely (parser))
10616 /* We're still not sure what the argument will be. */
10617 cp_parser_parse_tentatively (parser);
10618 /* Try a template. */
10619 argument_start_token = cp_lexer_peek_token (parser->lexer);
10620 argument = cp_parser_id_expression (parser,
10621 /*template_keyword_p=*/false,
10622 /*check_dependency_p=*/true,
10624 /*declarator_p=*/false,
10625 /*optional_p=*/false);
10626 /* If the next token isn't a `,' or a `>', then this argument wasn't
10627 really finished. */
10628 if (!cp_parser_next_token_ends_template_argument_p (parser))
10629 cp_parser_error (parser, "expected template-argument");
10630 if (!cp_parser_error_occurred (parser))
10632 /* Figure out what is being referred to. If the id-expression
10633 was for a class template specialization, then we will have a
10634 TYPE_DECL at this point. There is no need to do name lookup
10635 at this point in that case. */
10636 if (TREE_CODE (argument) != TYPE_DECL)
10637 argument = cp_parser_lookup_name (parser, argument,
10639 /*is_template=*/template_p,
10640 /*is_namespace=*/false,
10641 /*check_dependency=*/true,
10642 /*ambiguous_decls=*/NULL,
10643 argument_start_token->location);
10644 if (TREE_CODE (argument) != TEMPLATE_DECL
10645 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10646 cp_parser_error (parser, "expected template-name");
10648 if (cp_parser_parse_definitely (parser))
10650 /* It must be a non-type argument. There permitted cases are given
10651 in [temp.arg.nontype]:
10653 -- an integral constant-expression of integral or enumeration
10656 -- the name of a non-type template-parameter; or
10658 -- the name of an object or function with external linkage...
10660 -- the address of an object or function with external linkage...
10662 -- a pointer to member... */
10663 /* Look for a non-type template parameter. */
10664 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10666 cp_parser_parse_tentatively (parser);
10667 argument = cp_parser_primary_expression (parser,
10668 /*address_p=*/false,
10670 /*template_arg_p=*/true,
10672 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10673 || !cp_parser_next_token_ends_template_argument_p (parser))
10674 cp_parser_simulate_error (parser);
10675 if (cp_parser_parse_definitely (parser))
10679 /* If the next token is "&", the argument must be the address of an
10680 object or function with external linkage. */
10681 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10683 cp_lexer_consume_token (parser->lexer);
10684 /* See if we might have an id-expression. */
10685 token = cp_lexer_peek_token (parser->lexer);
10686 if (token->type == CPP_NAME
10687 || token->keyword == RID_OPERATOR
10688 || token->type == CPP_SCOPE
10689 || token->type == CPP_TEMPLATE_ID
10690 || token->type == CPP_NESTED_NAME_SPECIFIER)
10692 cp_parser_parse_tentatively (parser);
10693 argument = cp_parser_primary_expression (parser,
10696 /*template_arg_p=*/true,
10698 if (cp_parser_error_occurred (parser)
10699 || !cp_parser_next_token_ends_template_argument_p (parser))
10700 cp_parser_abort_tentative_parse (parser);
10705 if (TREE_CODE (argument) == INDIRECT_REF)
10707 gcc_assert (REFERENCE_REF_P (argument));
10708 argument = TREE_OPERAND (argument, 0);
10711 /* If we're in a template, we represent a qualified-id referring
10712 to a static data member as a SCOPE_REF even if the scope isn't
10713 dependent so that we can check access control later. */
10715 if (TREE_CODE (probe) == SCOPE_REF)
10716 probe = TREE_OPERAND (probe, 1);
10717 if (TREE_CODE (probe) == VAR_DECL)
10719 /* A variable without external linkage might still be a
10720 valid constant-expression, so no error is issued here
10721 if the external-linkage check fails. */
10722 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
10723 cp_parser_simulate_error (parser);
10725 else if (is_overloaded_fn (argument))
10726 /* All overloaded functions are allowed; if the external
10727 linkage test does not pass, an error will be issued
10731 && (TREE_CODE (argument) == OFFSET_REF
10732 || TREE_CODE (argument) == SCOPE_REF))
10733 /* A pointer-to-member. */
10735 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10738 cp_parser_simulate_error (parser);
10740 if (cp_parser_parse_definitely (parser))
10743 argument = build_x_unary_op (ADDR_EXPR, argument,
10744 tf_warning_or_error);
10749 /* If the argument started with "&", there are no other valid
10750 alternatives at this point. */
10753 cp_parser_error (parser, "invalid non-type template argument");
10754 return error_mark_node;
10757 /* If the argument wasn't successfully parsed as a type-id followed
10758 by '>>', the argument can only be a constant expression now.
10759 Otherwise, we try parsing the constant-expression tentatively,
10760 because the argument could really be a type-id. */
10762 cp_parser_parse_tentatively (parser);
10763 argument = cp_parser_constant_expression (parser,
10764 /*allow_non_constant_p=*/false,
10765 /*non_constant_p=*/NULL);
10766 argument = fold_non_dependent_expr (argument);
10767 if (!maybe_type_id)
10769 if (!cp_parser_next_token_ends_template_argument_p (parser))
10770 cp_parser_error (parser, "expected template-argument");
10771 if (cp_parser_parse_definitely (parser))
10773 /* We did our best to parse the argument as a non type-id, but that
10774 was the only alternative that matched (albeit with a '>' after
10775 it). We can assume it's just a typo from the user, and a
10776 diagnostic will then be issued. */
10777 return cp_parser_template_type_arg (parser);
10780 /* Parse an explicit-instantiation.
10782 explicit-instantiation:
10783 template declaration
10785 Although the standard says `declaration', what it really means is:
10787 explicit-instantiation:
10788 template decl-specifier-seq [opt] declarator [opt] ;
10790 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10791 supposed to be allowed. A defect report has been filed about this
10796 explicit-instantiation:
10797 storage-class-specifier template
10798 decl-specifier-seq [opt] declarator [opt] ;
10799 function-specifier template
10800 decl-specifier-seq [opt] declarator [opt] ; */
10803 cp_parser_explicit_instantiation (cp_parser* parser)
10805 int declares_class_or_enum;
10806 cp_decl_specifier_seq decl_specifiers;
10807 tree extension_specifier = NULL_TREE;
10810 /* Look for an (optional) storage-class-specifier or
10811 function-specifier. */
10812 if (cp_parser_allow_gnu_extensions_p (parser))
10814 extension_specifier
10815 = cp_parser_storage_class_specifier_opt (parser);
10816 if (!extension_specifier)
10817 extension_specifier
10818 = cp_parser_function_specifier_opt (parser,
10819 /*decl_specs=*/NULL);
10822 /* Look for the `template' keyword. */
10823 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10824 /* Let the front end know that we are processing an explicit
10826 begin_explicit_instantiation ();
10827 /* [temp.explicit] says that we are supposed to ignore access
10828 control while processing explicit instantiation directives. */
10829 push_deferring_access_checks (dk_no_check);
10830 /* Parse a decl-specifier-seq. */
10831 token = cp_lexer_peek_token (parser->lexer);
10832 cp_parser_decl_specifier_seq (parser,
10833 CP_PARSER_FLAGS_OPTIONAL,
10835 &declares_class_or_enum);
10836 /* If there was exactly one decl-specifier, and it declared a class,
10837 and there's no declarator, then we have an explicit type
10839 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10843 type = check_tag_decl (&decl_specifiers);
10844 /* Turn access control back on for names used during
10845 template instantiation. */
10846 pop_deferring_access_checks ();
10848 do_type_instantiation (type, extension_specifier,
10849 /*complain=*/tf_error);
10853 cp_declarator *declarator;
10856 /* Parse the declarator. */
10858 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10859 /*ctor_dtor_or_conv_p=*/NULL,
10860 /*parenthesized_p=*/NULL,
10861 /*member_p=*/false);
10862 if (declares_class_or_enum & 2)
10863 cp_parser_check_for_definition_in_return_type (declarator,
10864 decl_specifiers.type,
10865 decl_specifiers.type_location);
10866 if (declarator != cp_error_declarator)
10868 decl = grokdeclarator (declarator, &decl_specifiers,
10869 NORMAL, 0, &decl_specifiers.attributes);
10870 /* Turn access control back on for names used during
10871 template instantiation. */
10872 pop_deferring_access_checks ();
10873 /* Do the explicit instantiation. */
10874 do_decl_instantiation (decl, extension_specifier);
10878 pop_deferring_access_checks ();
10879 /* Skip the body of the explicit instantiation. */
10880 cp_parser_skip_to_end_of_statement (parser);
10883 /* We're done with the instantiation. */
10884 end_explicit_instantiation ();
10886 cp_parser_consume_semicolon_at_end_of_statement (parser);
10889 /* Parse an explicit-specialization.
10891 explicit-specialization:
10892 template < > declaration
10894 Although the standard says `declaration', what it really means is:
10896 explicit-specialization:
10897 template <> decl-specifier [opt] init-declarator [opt] ;
10898 template <> function-definition
10899 template <> explicit-specialization
10900 template <> template-declaration */
10903 cp_parser_explicit_specialization (cp_parser* parser)
10905 bool need_lang_pop;
10906 cp_token *token = cp_lexer_peek_token (parser->lexer);
10908 /* Look for the `template' keyword. */
10909 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10910 /* Look for the `<'. */
10911 cp_parser_require (parser, CPP_LESS, "%<<%>");
10912 /* Look for the `>'. */
10913 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10914 /* We have processed another parameter list. */
10915 ++parser->num_template_parameter_lists;
10918 A template ... explicit specialization ... shall not have C
10920 if (current_lang_name == lang_name_c)
10922 error ("%Htemplate specialization with C linkage", &token->location);
10923 /* Give it C++ linkage to avoid confusing other parts of the
10925 push_lang_context (lang_name_cplusplus);
10926 need_lang_pop = true;
10929 need_lang_pop = false;
10930 /* Let the front end know that we are beginning a specialization. */
10931 if (!begin_specialization ())
10933 end_specialization ();
10937 /* If the next keyword is `template', we need to figure out whether
10938 or not we're looking a template-declaration. */
10939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10941 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10942 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10943 cp_parser_template_declaration_after_export (parser,
10944 /*member_p=*/false);
10946 cp_parser_explicit_specialization (parser);
10949 /* Parse the dependent declaration. */
10950 cp_parser_single_declaration (parser,
10952 /*member_p=*/false,
10953 /*explicit_specialization_p=*/true,
10954 /*friend_p=*/NULL);
10955 /* We're done with the specialization. */
10956 end_specialization ();
10957 /* For the erroneous case of a template with C linkage, we pushed an
10958 implicit C++ linkage scope; exit that scope now. */
10960 pop_lang_context ();
10961 /* We're done with this parameter list. */
10962 --parser->num_template_parameter_lists;
10965 /* Parse a type-specifier.
10968 simple-type-specifier
10971 elaborated-type-specifier
10979 Returns a representation of the type-specifier. For a
10980 class-specifier, enum-specifier, or elaborated-type-specifier, a
10981 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10983 The parser flags FLAGS is used to control type-specifier parsing.
10985 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10986 in a decl-specifier-seq.
10988 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10989 class-specifier, enum-specifier, or elaborated-type-specifier, then
10990 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10991 if a type is declared; 2 if it is defined. Otherwise, it is set to
10994 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10995 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10996 is set to FALSE. */
10999 cp_parser_type_specifier (cp_parser* parser,
11000 cp_parser_flags flags,
11001 cp_decl_specifier_seq *decl_specs,
11002 bool is_declaration,
11003 int* declares_class_or_enum,
11004 bool* is_cv_qualifier)
11006 tree type_spec = NULL_TREE;
11009 cp_decl_spec ds = ds_last;
11011 /* Assume this type-specifier does not declare a new type. */
11012 if (declares_class_or_enum)
11013 *declares_class_or_enum = 0;
11014 /* And that it does not specify a cv-qualifier. */
11015 if (is_cv_qualifier)
11016 *is_cv_qualifier = false;
11017 /* Peek at the next token. */
11018 token = cp_lexer_peek_token (parser->lexer);
11020 /* If we're looking at a keyword, we can use that to guide the
11021 production we choose. */
11022 keyword = token->keyword;
11026 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11027 goto elaborated_type_specifier;
11029 /* Look for the enum-specifier. */
11030 type_spec = cp_parser_enum_specifier (parser);
11031 /* If that worked, we're done. */
11034 if (declares_class_or_enum)
11035 *declares_class_or_enum = 2;
11037 cp_parser_set_decl_spec_type (decl_specs,
11040 /*user_defined_p=*/true);
11044 goto elaborated_type_specifier;
11046 /* Any of these indicate either a class-specifier, or an
11047 elaborated-type-specifier. */
11051 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11052 goto elaborated_type_specifier;
11054 /* Parse tentatively so that we can back up if we don't find a
11055 class-specifier. */
11056 cp_parser_parse_tentatively (parser);
11057 /* Look for the class-specifier. */
11058 type_spec = cp_parser_class_specifier (parser);
11059 /* If that worked, we're done. */
11060 if (cp_parser_parse_definitely (parser))
11062 if (declares_class_or_enum)
11063 *declares_class_or_enum = 2;
11065 cp_parser_set_decl_spec_type (decl_specs,
11068 /*user_defined_p=*/true);
11072 /* Fall through. */
11073 elaborated_type_specifier:
11074 /* We're declaring (not defining) a class or enum. */
11075 if (declares_class_or_enum)
11076 *declares_class_or_enum = 1;
11078 /* Fall through. */
11080 /* Look for an elaborated-type-specifier. */
11082 = (cp_parser_elaborated_type_specifier
11084 decl_specs && decl_specs->specs[(int) ds_friend],
11087 cp_parser_set_decl_spec_type (decl_specs,
11090 /*user_defined_p=*/true);
11095 if (is_cv_qualifier)
11096 *is_cv_qualifier = true;
11101 if (is_cv_qualifier)
11102 *is_cv_qualifier = true;
11107 if (is_cv_qualifier)
11108 *is_cv_qualifier = true;
11112 /* The `__complex__' keyword is a GNU extension. */
11120 /* Handle simple keywords. */
11125 ++decl_specs->specs[(int)ds];
11126 decl_specs->any_specifiers_p = true;
11128 return cp_lexer_consume_token (parser->lexer)->u.value;
11131 /* If we do not already have a type-specifier, assume we are looking
11132 at a simple-type-specifier. */
11133 type_spec = cp_parser_simple_type_specifier (parser,
11137 /* If we didn't find a type-specifier, and a type-specifier was not
11138 optional in this context, issue an error message. */
11139 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11141 cp_parser_error (parser, "expected type specifier");
11142 return error_mark_node;
11148 /* Parse a simple-type-specifier.
11150 simple-type-specifier:
11151 :: [opt] nested-name-specifier [opt] type-name
11152 :: [opt] nested-name-specifier template template-id
11167 simple-type-specifier:
11169 decltype ( expression )
11175 simple-type-specifier:
11176 __typeof__ unary-expression
11177 __typeof__ ( type-id )
11179 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11180 appropriately updated. */
11183 cp_parser_simple_type_specifier (cp_parser* parser,
11184 cp_decl_specifier_seq *decl_specs,
11185 cp_parser_flags flags)
11187 tree type = NULL_TREE;
11190 /* Peek at the next token. */
11191 token = cp_lexer_peek_token (parser->lexer);
11193 /* If we're looking at a keyword, things are easy. */
11194 switch (token->keyword)
11198 decl_specs->explicit_char_p = true;
11199 type = char_type_node;
11202 type = char16_type_node;
11205 type = char32_type_node;
11208 type = wchar_type_node;
11211 type = boolean_type_node;
11215 ++decl_specs->specs[(int) ds_short];
11216 type = short_integer_type_node;
11220 decl_specs->explicit_int_p = true;
11221 type = integer_type_node;
11225 ++decl_specs->specs[(int) ds_long];
11226 type = long_integer_type_node;
11230 ++decl_specs->specs[(int) ds_signed];
11231 type = integer_type_node;
11235 ++decl_specs->specs[(int) ds_unsigned];
11236 type = unsigned_type_node;
11239 type = float_type_node;
11242 type = double_type_node;
11245 type = void_type_node;
11249 maybe_warn_cpp0x ("C++0x auto");
11250 type = make_auto ();
11254 /* Parse the `decltype' type. */
11255 type = cp_parser_decltype (parser);
11258 cp_parser_set_decl_spec_type (decl_specs, type,
11260 /*user_defined_p=*/true);
11265 /* Consume the `typeof' token. */
11266 cp_lexer_consume_token (parser->lexer);
11267 /* Parse the operand to `typeof'. */
11268 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11269 /* If it is not already a TYPE, take its type. */
11270 if (!TYPE_P (type))
11271 type = finish_typeof (type);
11274 cp_parser_set_decl_spec_type (decl_specs, type,
11276 /*user_defined_p=*/true);
11284 /* If the type-specifier was for a built-in type, we're done. */
11289 /* Record the type. */
11291 && (token->keyword != RID_SIGNED
11292 && token->keyword != RID_UNSIGNED
11293 && token->keyword != RID_SHORT
11294 && token->keyword != RID_LONG))
11295 cp_parser_set_decl_spec_type (decl_specs,
11298 /*user_defined=*/false);
11300 decl_specs->any_specifiers_p = true;
11302 /* Consume the token. */
11303 id = cp_lexer_consume_token (parser->lexer)->u.value;
11305 /* There is no valid C++ program where a non-template type is
11306 followed by a "<". That usually indicates that the user thought
11307 that the type was a template. */
11308 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11310 return TYPE_NAME (type);
11313 /* The type-specifier must be a user-defined type. */
11314 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11319 /* Don't gobble tokens or issue error messages if this is an
11320 optional type-specifier. */
11321 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11322 cp_parser_parse_tentatively (parser);
11324 /* Look for the optional `::' operator. */
11326 = (cp_parser_global_scope_opt (parser,
11327 /*current_scope_valid_p=*/false)
11329 /* Look for the nested-name specifier. */
11331 = (cp_parser_nested_name_specifier_opt (parser,
11332 /*typename_keyword_p=*/false,
11333 /*check_dependency_p=*/true,
11335 /*is_declaration=*/false)
11337 token = cp_lexer_peek_token (parser->lexer);
11338 /* If we have seen a nested-name-specifier, and the next token
11339 is `template', then we are using the template-id production. */
11341 && cp_parser_optional_template_keyword (parser))
11343 /* Look for the template-id. */
11344 type = cp_parser_template_id (parser,
11345 /*template_keyword_p=*/true,
11346 /*check_dependency_p=*/true,
11347 /*is_declaration=*/false);
11348 /* If the template-id did not name a type, we are out of
11350 if (TREE_CODE (type) != TYPE_DECL)
11352 cp_parser_error (parser, "expected template-id for type");
11356 /* Otherwise, look for a type-name. */
11358 type = cp_parser_type_name (parser);
11359 /* Keep track of all name-lookups performed in class scopes. */
11363 && TREE_CODE (type) == TYPE_DECL
11364 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11365 maybe_note_name_used_in_class (DECL_NAME (type), type);
11366 /* If it didn't work out, we don't have a TYPE. */
11367 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11368 && !cp_parser_parse_definitely (parser))
11370 if (type && decl_specs)
11371 cp_parser_set_decl_spec_type (decl_specs, type,
11373 /*user_defined=*/true);
11376 /* If we didn't get a type-name, issue an error message. */
11377 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11379 cp_parser_error (parser, "expected type-name");
11380 return error_mark_node;
11383 /* There is no valid C++ program where a non-template type is
11384 followed by a "<". That usually indicates that the user thought
11385 that the type was a template. */
11386 if (type && type != error_mark_node)
11388 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11389 If it is, then the '<'...'>' enclose protocol names rather than
11390 template arguments, and so everything is fine. */
11391 if (c_dialect_objc ()
11392 && (objc_is_id (type) || objc_is_class_name (type)))
11394 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11395 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11397 /* Clobber the "unqualified" type previously entered into
11398 DECL_SPECS with the new, improved protocol-qualified version. */
11400 decl_specs->type = qual_type;
11405 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11412 /* Parse a type-name.
11425 Returns a TYPE_DECL for the type. */
11428 cp_parser_type_name (cp_parser* parser)
11432 /* We can't know yet whether it is a class-name or not. */
11433 cp_parser_parse_tentatively (parser);
11434 /* Try a class-name. */
11435 type_decl = cp_parser_class_name (parser,
11436 /*typename_keyword_p=*/false,
11437 /*template_keyword_p=*/false,
11439 /*check_dependency_p=*/true,
11440 /*class_head_p=*/false,
11441 /*is_declaration=*/false);
11442 /* If it's not a class-name, keep looking. */
11443 if (!cp_parser_parse_definitely (parser))
11445 /* It must be a typedef-name or an enum-name. */
11446 return cp_parser_nonclass_name (parser);
11452 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11460 Returns a TYPE_DECL for the type. */
11463 cp_parser_nonclass_name (cp_parser* parser)
11468 cp_token *token = cp_lexer_peek_token (parser->lexer);
11469 identifier = cp_parser_identifier (parser);
11470 if (identifier == error_mark_node)
11471 return error_mark_node;
11473 /* Look up the type-name. */
11474 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11476 if (TREE_CODE (type_decl) != TYPE_DECL
11477 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11479 /* See if this is an Objective-C type. */
11480 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11481 tree type = objc_get_protocol_qualified_type (identifier, protos);
11483 type_decl = TYPE_NAME (type);
11486 /* Issue an error if we did not find a type-name. */
11487 if (TREE_CODE (type_decl) != TYPE_DECL)
11489 if (!cp_parser_simulate_error (parser))
11490 cp_parser_name_lookup_error (parser, identifier, type_decl,
11491 "is not a type", token->location);
11492 return error_mark_node;
11494 /* Remember that the name was used in the definition of the
11495 current class so that we can check later to see if the
11496 meaning would have been different after the class was
11497 entirely defined. */
11498 else if (type_decl != error_mark_node
11500 maybe_note_name_used_in_class (identifier, type_decl);
11505 /* Parse an elaborated-type-specifier. Note that the grammar given
11506 here incorporates the resolution to DR68.
11508 elaborated-type-specifier:
11509 class-key :: [opt] nested-name-specifier [opt] identifier
11510 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11511 enum-key :: [opt] nested-name-specifier [opt] identifier
11512 typename :: [opt] nested-name-specifier identifier
11513 typename :: [opt] nested-name-specifier template [opt]
11518 elaborated-type-specifier:
11519 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11520 class-key attributes :: [opt] nested-name-specifier [opt]
11521 template [opt] template-id
11522 enum attributes :: [opt] nested-name-specifier [opt] identifier
11524 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11525 declared `friend'. If IS_DECLARATION is TRUE, then this
11526 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11527 something is being declared.
11529 Returns the TYPE specified. */
11532 cp_parser_elaborated_type_specifier (cp_parser* parser,
11534 bool is_declaration)
11536 enum tag_types tag_type;
11538 tree type = NULL_TREE;
11539 tree attributes = NULL_TREE;
11540 cp_token *token = NULL;
11542 /* See if we're looking at the `enum' keyword. */
11543 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11545 /* Consume the `enum' token. */
11546 cp_lexer_consume_token (parser->lexer);
11547 /* Remember that it's an enumeration type. */
11548 tag_type = enum_type;
11549 /* Parse the optional `struct' or `class' key (for C++0x scoped
11551 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11552 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11554 if (cxx_dialect == cxx98)
11555 maybe_warn_cpp0x ("scoped enums");
11557 /* Consume the `struct' or `class'. */
11558 cp_lexer_consume_token (parser->lexer);
11560 /* Parse the attributes. */
11561 attributes = cp_parser_attributes_opt (parser);
11563 /* Or, it might be `typename'. */
11564 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11567 /* Consume the `typename' token. */
11568 cp_lexer_consume_token (parser->lexer);
11569 /* Remember that it's a `typename' type. */
11570 tag_type = typename_type;
11571 /* The `typename' keyword is only allowed in templates. */
11572 if (!processing_template_decl)
11573 permerror (input_location, "using %<typename%> outside of template");
11575 /* Otherwise it must be a class-key. */
11578 tag_type = cp_parser_class_key (parser);
11579 if (tag_type == none_type)
11580 return error_mark_node;
11581 /* Parse the attributes. */
11582 attributes = cp_parser_attributes_opt (parser);
11585 /* Look for the `::' operator. */
11586 cp_parser_global_scope_opt (parser,
11587 /*current_scope_valid_p=*/false);
11588 /* Look for the nested-name-specifier. */
11589 if (tag_type == typename_type)
11591 if (!cp_parser_nested_name_specifier (parser,
11592 /*typename_keyword_p=*/true,
11593 /*check_dependency_p=*/true,
11596 return error_mark_node;
11599 /* Even though `typename' is not present, the proposed resolution
11600 to Core Issue 180 says that in `class A<T>::B', `B' should be
11601 considered a type-name, even if `A<T>' is dependent. */
11602 cp_parser_nested_name_specifier_opt (parser,
11603 /*typename_keyword_p=*/true,
11604 /*check_dependency_p=*/true,
11607 /* For everything but enumeration types, consider a template-id.
11608 For an enumeration type, consider only a plain identifier. */
11609 if (tag_type != enum_type)
11611 bool template_p = false;
11614 /* Allow the `template' keyword. */
11615 template_p = cp_parser_optional_template_keyword (parser);
11616 /* If we didn't see `template', we don't know if there's a
11617 template-id or not. */
11619 cp_parser_parse_tentatively (parser);
11620 /* Parse the template-id. */
11621 token = cp_lexer_peek_token (parser->lexer);
11622 decl = cp_parser_template_id (parser, template_p,
11623 /*check_dependency_p=*/true,
11625 /* If we didn't find a template-id, look for an ordinary
11627 if (!template_p && !cp_parser_parse_definitely (parser))
11629 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11630 in effect, then we must assume that, upon instantiation, the
11631 template will correspond to a class. */
11632 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11633 && tag_type == typename_type)
11634 type = make_typename_type (parser->scope, decl,
11636 /*complain=*/tf_error);
11637 /* If the `typename' keyword is in effect and DECL is not a type
11638 decl. Then type is non existant. */
11639 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11642 type = TREE_TYPE (decl);
11647 token = cp_lexer_peek_token (parser->lexer);
11648 identifier = cp_parser_identifier (parser);
11650 if (identifier == error_mark_node)
11652 parser->scope = NULL_TREE;
11653 return error_mark_node;
11656 /* For a `typename', we needn't call xref_tag. */
11657 if (tag_type == typename_type
11658 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11659 return cp_parser_make_typename_type (parser, parser->scope,
11662 /* Look up a qualified name in the usual way. */
11666 tree ambiguous_decls;
11668 decl = cp_parser_lookup_name (parser, identifier,
11670 /*is_template=*/false,
11671 /*is_namespace=*/false,
11672 /*check_dependency=*/true,
11676 /* If the lookup was ambiguous, an error will already have been
11678 if (ambiguous_decls)
11679 return error_mark_node;
11681 /* If we are parsing friend declaration, DECL may be a
11682 TEMPLATE_DECL tree node here. However, we need to check
11683 whether this TEMPLATE_DECL results in valid code. Consider
11684 the following example:
11687 template <class T> class C {};
11690 template <class T> friend class N::C; // #1, valid code
11692 template <class T> class Y {
11693 friend class N::C; // #2, invalid code
11696 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11697 name lookup of `N::C'. We see that friend declaration must
11698 be template for the code to be valid. Note that
11699 processing_template_decl does not work here since it is
11700 always 1 for the above two cases. */
11702 decl = (cp_parser_maybe_treat_template_as_class
11703 (decl, /*tag_name_p=*/is_friend
11704 && parser->num_template_parameter_lists));
11706 if (TREE_CODE (decl) != TYPE_DECL)
11708 cp_parser_diagnose_invalid_type_name (parser,
11712 return error_mark_node;
11715 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11717 bool allow_template = (parser->num_template_parameter_lists
11718 || DECL_SELF_REFERENCE_P (decl));
11719 type = check_elaborated_type_specifier (tag_type, decl,
11722 if (type == error_mark_node)
11723 return error_mark_node;
11726 /* Forward declarations of nested types, such as
11731 are invalid unless all components preceding the final '::'
11732 are complete. If all enclosing types are complete, these
11733 declarations become merely pointless.
11735 Invalid forward declarations of nested types are errors
11736 caught elsewhere in parsing. Those that are pointless arrive
11739 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11740 && !is_friend && !processing_explicit_instantiation)
11741 warning (0, "declaration %qD does not declare anything", decl);
11743 type = TREE_TYPE (decl);
11747 /* An elaborated-type-specifier sometimes introduces a new type and
11748 sometimes names an existing type. Normally, the rule is that it
11749 introduces a new type only if there is not an existing type of
11750 the same name already in scope. For example, given:
11753 void f() { struct S s; }
11755 the `struct S' in the body of `f' is the same `struct S' as in
11756 the global scope; the existing definition is used. However, if
11757 there were no global declaration, this would introduce a new
11758 local class named `S'.
11760 An exception to this rule applies to the following code:
11762 namespace N { struct S; }
11764 Here, the elaborated-type-specifier names a new type
11765 unconditionally; even if there is already an `S' in the
11766 containing scope this declaration names a new type.
11767 This exception only applies if the elaborated-type-specifier
11768 forms the complete declaration:
11772 A declaration consisting solely of `class-key identifier ;' is
11773 either a redeclaration of the name in the current scope or a
11774 forward declaration of the identifier as a class name. It
11775 introduces the name into the current scope.
11777 We are in this situation precisely when the next token is a `;'.
11779 An exception to the exception is that a `friend' declaration does
11780 *not* name a new type; i.e., given:
11782 struct S { friend struct T; };
11784 `T' is not a new type in the scope of `S'.
11786 Also, `new struct S' or `sizeof (struct S)' never results in the
11787 definition of a new type; a new type can only be declared in a
11788 declaration context. */
11794 /* Friends have special name lookup rules. */
11795 ts = ts_within_enclosing_non_class;
11796 else if (is_declaration
11797 && cp_lexer_next_token_is (parser->lexer,
11799 /* This is a `class-key identifier ;' */
11805 (parser->num_template_parameter_lists
11806 && (cp_parser_next_token_starts_class_definition_p (parser)
11807 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11808 /* An unqualified name was used to reference this type, so
11809 there were no qualifying templates. */
11810 if (!cp_parser_check_template_parameters (parser,
11811 /*num_templates=*/0,
11813 return error_mark_node;
11814 type = xref_tag (tag_type, identifier, ts, template_p);
11818 if (type == error_mark_node)
11819 return error_mark_node;
11821 /* Allow attributes on forward declarations of classes. */
11824 if (TREE_CODE (type) == TYPENAME_TYPE)
11825 warning (OPT_Wattributes,
11826 "attributes ignored on uninstantiated type");
11827 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11828 && ! processing_explicit_instantiation)
11829 warning (OPT_Wattributes,
11830 "attributes ignored on template instantiation");
11831 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11832 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11834 warning (OPT_Wattributes,
11835 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11838 if (tag_type != enum_type)
11839 cp_parser_check_class_key (tag_type, type);
11841 /* A "<" cannot follow an elaborated type specifier. If that
11842 happens, the user was probably trying to form a template-id. */
11843 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11848 /* Parse an enum-specifier.
11851 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11856 enum struct [C++0x]
11859 : type-specifier-seq
11862 enum-key attributes[opt] identifier [opt] enum-base [opt]
11863 { enumerator-list [opt] }attributes[opt]
11865 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11866 if the token stream isn't an enum-specifier after all. */
11869 cp_parser_enum_specifier (cp_parser* parser)
11874 bool scoped_enum_p = false;
11875 bool has_underlying_type = false;
11876 tree underlying_type = NULL_TREE;
11878 /* Parse tentatively so that we can back up if we don't find a
11880 cp_parser_parse_tentatively (parser);
11882 /* Caller guarantees that the current token is 'enum', an identifier
11883 possibly follows, and the token after that is an opening brace.
11884 If we don't have an identifier, fabricate an anonymous name for
11885 the enumeration being defined. */
11886 cp_lexer_consume_token (parser->lexer);
11888 /* Parse the "class" or "struct", which indicates a scoped
11889 enumeration type in C++0x. */
11890 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11891 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11893 if (cxx_dialect == cxx98)
11894 maybe_warn_cpp0x ("scoped enums");
11896 /* Consume the `struct' or `class' token. */
11897 cp_lexer_consume_token (parser->lexer);
11899 scoped_enum_p = true;
11902 attributes = cp_parser_attributes_opt (parser);
11904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11905 identifier = cp_parser_identifier (parser);
11907 identifier = make_anon_name ();
11909 /* Check for the `:' that denotes a specified underlying type in C++0x.
11910 Note that a ':' could also indicate a bitfield width, however. */
11911 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11913 cp_decl_specifier_seq type_specifiers;
11915 /* Consume the `:'. */
11916 cp_lexer_consume_token (parser->lexer);
11918 /* Parse the type-specifier-seq. */
11919 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11920 /*is_trailing_return=*/false,
11923 /* At this point this is surely not elaborated type specifier. */
11924 if (!cp_parser_parse_definitely (parser))
11927 if (cxx_dialect == cxx98)
11928 maybe_warn_cpp0x ("scoped enums");
11930 has_underlying_type = true;
11932 /* If that didn't work, stop. */
11933 if (type_specifiers.type != error_mark_node)
11935 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11936 /*initialized=*/0, NULL);
11937 if (underlying_type == error_mark_node)
11938 underlying_type = NULL_TREE;
11942 /* Look for the `{' but don't consume it yet. */
11943 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11945 cp_parser_error (parser, "expected %<{%>");
11946 if (has_underlying_type)
11950 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11953 /* Issue an error message if type-definitions are forbidden here. */
11954 if (!cp_parser_check_type_definition (parser))
11955 type = error_mark_node;
11957 /* Create the new type. We do this before consuming the opening
11958 brace so the enum will be recorded as being on the line of its
11959 tag (or the 'enum' keyword, if there is no tag). */
11960 type = start_enum (identifier, underlying_type, scoped_enum_p);
11962 /* Consume the opening brace. */
11963 cp_lexer_consume_token (parser->lexer);
11965 if (type == error_mark_node)
11967 cp_parser_skip_to_end_of_block_or_statement (parser);
11968 return error_mark_node;
11971 /* If the next token is not '}', then there are some enumerators. */
11972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11973 cp_parser_enumerator_list (parser, type);
11975 /* Consume the final '}'. */
11976 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11978 /* Look for trailing attributes to apply to this enumeration, and
11979 apply them if appropriate. */
11980 if (cp_parser_allow_gnu_extensions_p (parser))
11982 tree trailing_attr = cp_parser_attributes_opt (parser);
11983 trailing_attr = chainon (trailing_attr, attributes);
11984 cplus_decl_attributes (&type,
11986 (int) ATTR_FLAG_TYPE_IN_PLACE);
11989 /* Finish up the enumeration. */
11990 finish_enum (type);
11995 /* Parse an enumerator-list. The enumerators all have the indicated
11999 enumerator-definition
12000 enumerator-list , enumerator-definition */
12003 cp_parser_enumerator_list (cp_parser* parser, tree type)
12007 /* Parse an enumerator-definition. */
12008 cp_parser_enumerator_definition (parser, type);
12010 /* If the next token is not a ',', we've reached the end of
12012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12014 /* Otherwise, consume the `,' and keep going. */
12015 cp_lexer_consume_token (parser->lexer);
12016 /* If the next token is a `}', there is a trailing comma. */
12017 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12019 if (!in_system_header)
12020 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12026 /* Parse an enumerator-definition. The enumerator has the indicated
12029 enumerator-definition:
12031 enumerator = constant-expression
12037 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12042 /* Look for the identifier. */
12043 identifier = cp_parser_identifier (parser);
12044 if (identifier == error_mark_node)
12047 /* If the next token is an '=', then there is an explicit value. */
12048 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12050 /* Consume the `=' token. */
12051 cp_lexer_consume_token (parser->lexer);
12052 /* Parse the value. */
12053 value = cp_parser_constant_expression (parser,
12054 /*allow_non_constant_p=*/false,
12060 /* If we are processing a template, make sure the initializer of the
12061 enumerator doesn't contain any bare template parameter pack. */
12062 if (check_for_bare_parameter_packs (value))
12063 value = error_mark_node;
12065 /* Create the enumerator. */
12066 build_enumerator (identifier, value, type);
12069 /* Parse a namespace-name.
12072 original-namespace-name
12075 Returns the NAMESPACE_DECL for the namespace. */
12078 cp_parser_namespace_name (cp_parser* parser)
12081 tree namespace_decl;
12083 cp_token *token = cp_lexer_peek_token (parser->lexer);
12085 /* Get the name of the namespace. */
12086 identifier = cp_parser_identifier (parser);
12087 if (identifier == error_mark_node)
12088 return error_mark_node;
12090 /* Look up the identifier in the currently active scope. Look only
12091 for namespaces, due to:
12093 [basic.lookup.udir]
12095 When looking up a namespace-name in a using-directive or alias
12096 definition, only namespace names are considered.
12100 [basic.lookup.qual]
12102 During the lookup of a name preceding the :: scope resolution
12103 operator, object, function, and enumerator names are ignored.
12105 (Note that cp_parser_qualifying_entity only calls this
12106 function if the token after the name is the scope resolution
12108 namespace_decl = cp_parser_lookup_name (parser, identifier,
12110 /*is_template=*/false,
12111 /*is_namespace=*/true,
12112 /*check_dependency=*/true,
12113 /*ambiguous_decls=*/NULL,
12115 /* If it's not a namespace, issue an error. */
12116 if (namespace_decl == error_mark_node
12117 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12119 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12120 error ("%H%qD is not a namespace-name", &token->location, identifier);
12121 cp_parser_error (parser, "expected namespace-name");
12122 namespace_decl = error_mark_node;
12125 return namespace_decl;
12128 /* Parse a namespace-definition.
12130 namespace-definition:
12131 named-namespace-definition
12132 unnamed-namespace-definition
12134 named-namespace-definition:
12135 original-namespace-definition
12136 extension-namespace-definition
12138 original-namespace-definition:
12139 namespace identifier { namespace-body }
12141 extension-namespace-definition:
12142 namespace original-namespace-name { namespace-body }
12144 unnamed-namespace-definition:
12145 namespace { namespace-body } */
12148 cp_parser_namespace_definition (cp_parser* parser)
12150 tree identifier, attribs;
12151 bool has_visibility;
12154 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12157 cp_lexer_consume_token (parser->lexer);
12162 /* Look for the `namespace' keyword. */
12163 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12165 /* Get the name of the namespace. We do not attempt to distinguish
12166 between an original-namespace-definition and an
12167 extension-namespace-definition at this point. The semantic
12168 analysis routines are responsible for that. */
12169 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12170 identifier = cp_parser_identifier (parser);
12172 identifier = NULL_TREE;
12174 /* Parse any specified attributes. */
12175 attribs = cp_parser_attributes_opt (parser);
12177 /* Look for the `{' to start the namespace. */
12178 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12179 /* Start the namespace. */
12180 push_namespace (identifier);
12182 /* "inline namespace" is equivalent to a stub namespace definition
12183 followed by a strong using directive. */
12186 tree name_space = current_namespace;
12187 /* Set up namespace association. */
12188 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12189 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12190 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12191 /* Import the contents of the inline namespace. */
12193 do_using_directive (name_space);
12194 push_namespace (identifier);
12197 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12199 /* Parse the body of the namespace. */
12200 cp_parser_namespace_body (parser);
12202 #ifdef HANDLE_PRAGMA_VISIBILITY
12203 if (has_visibility)
12207 /* Finish the namespace. */
12209 /* Look for the final `}'. */
12210 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12213 /* Parse a namespace-body.
12216 declaration-seq [opt] */
12219 cp_parser_namespace_body (cp_parser* parser)
12221 cp_parser_declaration_seq_opt (parser);
12224 /* Parse a namespace-alias-definition.
12226 namespace-alias-definition:
12227 namespace identifier = qualified-namespace-specifier ; */
12230 cp_parser_namespace_alias_definition (cp_parser* parser)
12233 tree namespace_specifier;
12235 cp_token *token = cp_lexer_peek_token (parser->lexer);
12237 /* Look for the `namespace' keyword. */
12238 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12239 /* Look for the identifier. */
12240 identifier = cp_parser_identifier (parser);
12241 if (identifier == error_mark_node)
12243 /* Look for the `=' token. */
12244 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12245 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12247 error ("%H%<namespace%> definition is not allowed here", &token->location);
12248 /* Skip the definition. */
12249 cp_lexer_consume_token (parser->lexer);
12250 if (cp_parser_skip_to_closing_brace (parser))
12251 cp_lexer_consume_token (parser->lexer);
12254 cp_parser_require (parser, CPP_EQ, "%<=%>");
12255 /* Look for the qualified-namespace-specifier. */
12256 namespace_specifier
12257 = cp_parser_qualified_namespace_specifier (parser);
12258 /* Look for the `;' token. */
12259 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12261 /* Register the alias in the symbol table. */
12262 do_namespace_alias (identifier, namespace_specifier);
12265 /* Parse a qualified-namespace-specifier.
12267 qualified-namespace-specifier:
12268 :: [opt] nested-name-specifier [opt] namespace-name
12270 Returns a NAMESPACE_DECL corresponding to the specified
12274 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12276 /* Look for the optional `::'. */
12277 cp_parser_global_scope_opt (parser,
12278 /*current_scope_valid_p=*/false);
12280 /* Look for the optional nested-name-specifier. */
12281 cp_parser_nested_name_specifier_opt (parser,
12282 /*typename_keyword_p=*/false,
12283 /*check_dependency_p=*/true,
12285 /*is_declaration=*/true);
12287 return cp_parser_namespace_name (parser);
12290 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12291 access declaration.
12294 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12295 using :: unqualified-id ;
12297 access-declaration:
12303 cp_parser_using_declaration (cp_parser* parser,
12304 bool access_declaration_p)
12307 bool typename_p = false;
12308 bool global_scope_p;
12313 if (access_declaration_p)
12314 cp_parser_parse_tentatively (parser);
12317 /* Look for the `using' keyword. */
12318 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12320 /* Peek at the next token. */
12321 token = cp_lexer_peek_token (parser->lexer);
12322 /* See if it's `typename'. */
12323 if (token->keyword == RID_TYPENAME)
12325 /* Remember that we've seen it. */
12327 /* Consume the `typename' token. */
12328 cp_lexer_consume_token (parser->lexer);
12332 /* Look for the optional global scope qualification. */
12334 = (cp_parser_global_scope_opt (parser,
12335 /*current_scope_valid_p=*/false)
12338 /* If we saw `typename', or didn't see `::', then there must be a
12339 nested-name-specifier present. */
12340 if (typename_p || !global_scope_p)
12341 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12342 /*check_dependency_p=*/true,
12344 /*is_declaration=*/true);
12345 /* Otherwise, we could be in either of the two productions. In that
12346 case, treat the nested-name-specifier as optional. */
12348 qscope = cp_parser_nested_name_specifier_opt (parser,
12349 /*typename_keyword_p=*/false,
12350 /*check_dependency_p=*/true,
12352 /*is_declaration=*/true);
12354 qscope = global_namespace;
12356 if (access_declaration_p && cp_parser_error_occurred (parser))
12357 /* Something has already gone wrong; there's no need to parse
12358 further. Since an error has occurred, the return value of
12359 cp_parser_parse_definitely will be false, as required. */
12360 return cp_parser_parse_definitely (parser);
12362 token = cp_lexer_peek_token (parser->lexer);
12363 /* Parse the unqualified-id. */
12364 identifier = cp_parser_unqualified_id (parser,
12365 /*template_keyword_p=*/false,
12366 /*check_dependency_p=*/true,
12367 /*declarator_p=*/true,
12368 /*optional_p=*/false);
12370 if (access_declaration_p)
12372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12373 cp_parser_simulate_error (parser);
12374 if (!cp_parser_parse_definitely (parser))
12378 /* The function we call to handle a using-declaration is different
12379 depending on what scope we are in. */
12380 if (qscope == error_mark_node || identifier == error_mark_node)
12382 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12383 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12384 /* [namespace.udecl]
12386 A using declaration shall not name a template-id. */
12387 error ("%Ha template-id may not appear in a using-declaration",
12391 if (at_class_scope_p ())
12393 /* Create the USING_DECL. */
12394 decl = do_class_using_decl (parser->scope, identifier);
12396 if (check_for_bare_parameter_packs (decl))
12399 /* Add it to the list of members in this class. */
12400 finish_member_declaration (decl);
12404 decl = cp_parser_lookup_name_simple (parser,
12407 if (decl == error_mark_node)
12408 cp_parser_name_lookup_error (parser, identifier,
12411 else if (check_for_bare_parameter_packs (decl))
12413 else if (!at_namespace_scope_p ())
12414 do_local_using_decl (decl, qscope, identifier);
12416 do_toplevel_using_decl (decl, qscope, identifier);
12420 /* Look for the final `;'. */
12421 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12426 /* Parse a using-directive.
12429 using namespace :: [opt] nested-name-specifier [opt]
12430 namespace-name ; */
12433 cp_parser_using_directive (cp_parser* parser)
12435 tree namespace_decl;
12438 /* Look for the `using' keyword. */
12439 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12440 /* And the `namespace' keyword. */
12441 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12442 /* Look for the optional `::' operator. */
12443 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12444 /* And the optional nested-name-specifier. */
12445 cp_parser_nested_name_specifier_opt (parser,
12446 /*typename_keyword_p=*/false,
12447 /*check_dependency_p=*/true,
12449 /*is_declaration=*/true);
12450 /* Get the namespace being used. */
12451 namespace_decl = cp_parser_namespace_name (parser);
12452 /* And any specified attributes. */
12453 attribs = cp_parser_attributes_opt (parser);
12454 /* Update the symbol table. */
12455 parse_using_directive (namespace_decl, attribs);
12456 /* Look for the final `;'. */
12457 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12460 /* Parse an asm-definition.
12463 asm ( string-literal ) ;
12468 asm volatile [opt] ( string-literal ) ;
12469 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12470 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12471 : asm-operand-list [opt] ) ;
12472 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12473 : asm-operand-list [opt]
12474 : asm-operand-list [opt] ) ; */
12477 cp_parser_asm_definition (cp_parser* parser)
12480 tree outputs = NULL_TREE;
12481 tree inputs = NULL_TREE;
12482 tree clobbers = NULL_TREE;
12484 bool volatile_p = false;
12485 bool extended_p = false;
12486 bool invalid_inputs_p = false;
12487 bool invalid_outputs_p = false;
12489 /* Look for the `asm' keyword. */
12490 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12491 /* See if the next token is `volatile'. */
12492 if (cp_parser_allow_gnu_extensions_p (parser)
12493 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12495 /* Remember that we saw the `volatile' keyword. */
12497 /* Consume the token. */
12498 cp_lexer_consume_token (parser->lexer);
12500 /* Look for the opening `('. */
12501 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12503 /* Look for the string. */
12504 string = cp_parser_string_literal (parser, false, false);
12505 if (string == error_mark_node)
12507 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12508 /*consume_paren=*/true);
12512 /* If we're allowing GNU extensions, check for the extended assembly
12513 syntax. Unfortunately, the `:' tokens need not be separated by
12514 a space in C, and so, for compatibility, we tolerate that here
12515 too. Doing that means that we have to treat the `::' operator as
12517 if (cp_parser_allow_gnu_extensions_p (parser)
12518 && parser->in_function_body
12519 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12520 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12522 bool inputs_p = false;
12523 bool clobbers_p = false;
12525 /* The extended syntax was used. */
12528 /* Look for outputs. */
12529 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12531 /* Consume the `:'. */
12532 cp_lexer_consume_token (parser->lexer);
12533 /* Parse the output-operands. */
12534 if (cp_lexer_next_token_is_not (parser->lexer,
12536 && cp_lexer_next_token_is_not (parser->lexer,
12538 && cp_lexer_next_token_is_not (parser->lexer,
12540 outputs = cp_parser_asm_operand_list (parser);
12542 if (outputs == error_mark_node)
12543 invalid_outputs_p = true;
12545 /* If the next token is `::', there are no outputs, and the
12546 next token is the beginning of the inputs. */
12547 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12548 /* The inputs are coming next. */
12551 /* Look for inputs. */
12553 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12555 /* Consume the `:' or `::'. */
12556 cp_lexer_consume_token (parser->lexer);
12557 /* Parse the output-operands. */
12558 if (cp_lexer_next_token_is_not (parser->lexer,
12560 && cp_lexer_next_token_is_not (parser->lexer,
12562 inputs = cp_parser_asm_operand_list (parser);
12564 if (inputs == error_mark_node)
12565 invalid_inputs_p = true;
12567 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12568 /* The clobbers are coming next. */
12571 /* Look for clobbers. */
12573 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12575 /* Consume the `:' or `::'. */
12576 cp_lexer_consume_token (parser->lexer);
12577 /* Parse the clobbers. */
12578 if (cp_lexer_next_token_is_not (parser->lexer,
12580 clobbers = cp_parser_asm_clobber_list (parser);
12583 /* Look for the closing `)'. */
12584 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12585 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12586 /*consume_paren=*/true);
12587 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12589 if (!invalid_inputs_p && !invalid_outputs_p)
12591 /* Create the ASM_EXPR. */
12592 if (parser->in_function_body)
12594 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12596 /* If the extended syntax was not used, mark the ASM_EXPR. */
12599 tree temp = asm_stmt;
12600 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12601 temp = TREE_OPERAND (temp, 0);
12603 ASM_INPUT_P (temp) = 1;
12607 cgraph_add_asm_node (string);
12611 /* Declarators [gram.dcl.decl] */
12613 /* Parse an init-declarator.
12616 declarator initializer [opt]
12621 declarator asm-specification [opt] attributes [opt] initializer [opt]
12623 function-definition:
12624 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12626 decl-specifier-seq [opt] declarator function-try-block
12630 function-definition:
12631 __extension__ function-definition
12633 The DECL_SPECIFIERS apply to this declarator. Returns a
12634 representation of the entity declared. If MEMBER_P is TRUE, then
12635 this declarator appears in a class scope. The new DECL created by
12636 this declarator is returned.
12638 The CHECKS are access checks that should be performed once we know
12639 what entity is being declared (and, therefore, what classes have
12642 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12643 for a function-definition here as well. If the declarator is a
12644 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12645 be TRUE upon return. By that point, the function-definition will
12646 have been completely parsed.
12648 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12652 cp_parser_init_declarator (cp_parser* parser,
12653 cp_decl_specifier_seq *decl_specifiers,
12654 VEC (deferred_access_check,gc)* checks,
12655 bool function_definition_allowed_p,
12657 int declares_class_or_enum,
12658 bool* function_definition_p)
12660 cp_token *token = NULL, *asm_spec_start_token = NULL,
12661 *attributes_start_token = NULL;
12662 cp_declarator *declarator;
12663 tree prefix_attributes;
12665 tree asm_specification;
12667 tree decl = NULL_TREE;
12669 int is_initialized;
12670 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12671 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12673 enum cpp_ttype initialization_kind;
12674 bool is_direct_init = false;
12675 bool is_non_constant_init;
12676 int ctor_dtor_or_conv_p;
12678 tree pushed_scope = NULL;
12680 /* Gather the attributes that were provided with the
12681 decl-specifiers. */
12682 prefix_attributes = decl_specifiers->attributes;
12684 /* Assume that this is not the declarator for a function
12686 if (function_definition_p)
12687 *function_definition_p = false;
12689 /* Defer access checks while parsing the declarator; we cannot know
12690 what names are accessible until we know what is being
12692 resume_deferring_access_checks ();
12694 /* Parse the declarator. */
12695 token = cp_lexer_peek_token (parser->lexer);
12697 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12698 &ctor_dtor_or_conv_p,
12699 /*parenthesized_p=*/NULL,
12700 /*member_p=*/false);
12701 /* Gather up the deferred checks. */
12702 stop_deferring_access_checks ();
12704 /* If the DECLARATOR was erroneous, there's no need to go
12706 if (declarator == cp_error_declarator)
12707 return error_mark_node;
12709 /* Check that the number of template-parameter-lists is OK. */
12710 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12712 return error_mark_node;
12714 if (declares_class_or_enum & 2)
12715 cp_parser_check_for_definition_in_return_type (declarator,
12716 decl_specifiers->type,
12717 decl_specifiers->type_location);
12719 /* Figure out what scope the entity declared by the DECLARATOR is
12720 located in. `grokdeclarator' sometimes changes the scope, so
12721 we compute it now. */
12722 scope = get_scope_of_declarator (declarator);
12724 /* If we're allowing GNU extensions, look for an asm-specification
12726 if (cp_parser_allow_gnu_extensions_p (parser))
12728 /* Look for an asm-specification. */
12729 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12730 asm_specification = cp_parser_asm_specification_opt (parser);
12731 /* And attributes. */
12732 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12733 attributes = cp_parser_attributes_opt (parser);
12737 asm_specification = NULL_TREE;
12738 attributes = NULL_TREE;
12741 /* Peek at the next token. */
12742 token = cp_lexer_peek_token (parser->lexer);
12743 /* Check to see if the token indicates the start of a
12744 function-definition. */
12745 if (function_declarator_p (declarator)
12746 && cp_parser_token_starts_function_definition_p (token))
12748 if (!function_definition_allowed_p)
12750 /* If a function-definition should not appear here, issue an
12752 cp_parser_error (parser,
12753 "a function-definition is not allowed here");
12754 return error_mark_node;
12758 location_t func_brace_location
12759 = cp_lexer_peek_token (parser->lexer)->location;
12761 /* Neither attributes nor an asm-specification are allowed
12762 on a function-definition. */
12763 if (asm_specification)
12764 error ("%Han asm-specification is not allowed "
12765 "on a function-definition",
12766 &asm_spec_start_token->location);
12768 error ("%Hattributes are not allowed on a function-definition",
12769 &attributes_start_token->location);
12770 /* This is a function-definition. */
12771 *function_definition_p = true;
12773 /* Parse the function definition. */
12775 decl = cp_parser_save_member_function_body (parser,
12778 prefix_attributes);
12781 = (cp_parser_function_definition_from_specifiers_and_declarator
12782 (parser, decl_specifiers, prefix_attributes, declarator));
12784 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12786 /* This is where the prologue starts... */
12787 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12788 = func_brace_location;
12797 Only in function declarations for constructors, destructors, and
12798 type conversions can the decl-specifier-seq be omitted.
12800 We explicitly postpone this check past the point where we handle
12801 function-definitions because we tolerate function-definitions
12802 that are missing their return types in some modes. */
12803 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12805 cp_parser_error (parser,
12806 "expected constructor, destructor, or type conversion");
12807 return error_mark_node;
12810 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12811 if (token->type == CPP_EQ
12812 || token->type == CPP_OPEN_PAREN
12813 || token->type == CPP_OPEN_BRACE)
12815 is_initialized = SD_INITIALIZED;
12816 initialization_kind = token->type;
12818 if (token->type == CPP_EQ
12819 && function_declarator_p (declarator))
12821 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12822 if (t2->keyword == RID_DEFAULT)
12823 is_initialized = SD_DEFAULTED;
12824 else if (t2->keyword == RID_DELETE)
12825 is_initialized = SD_DELETED;
12830 /* If the init-declarator isn't initialized and isn't followed by a
12831 `,' or `;', it's not a valid init-declarator. */
12832 if (token->type != CPP_COMMA
12833 && token->type != CPP_SEMICOLON)
12835 cp_parser_error (parser, "expected initializer");
12836 return error_mark_node;
12838 is_initialized = SD_UNINITIALIZED;
12839 initialization_kind = CPP_EOF;
12842 /* Because start_decl has side-effects, we should only call it if we
12843 know we're going ahead. By this point, we know that we cannot
12844 possibly be looking at any other construct. */
12845 cp_parser_commit_to_tentative_parse (parser);
12847 /* If the decl specifiers were bad, issue an error now that we're
12848 sure this was intended to be a declarator. Then continue
12849 declaring the variable(s), as int, to try to cut down on further
12851 if (decl_specifiers->any_specifiers_p
12852 && decl_specifiers->type == error_mark_node)
12854 cp_parser_error (parser, "invalid type in declaration");
12855 decl_specifiers->type = integer_type_node;
12858 /* Check to see whether or not this declaration is a friend. */
12859 friend_p = cp_parser_friend_p (decl_specifiers);
12861 /* Enter the newly declared entry in the symbol table. If we're
12862 processing a declaration in a class-specifier, we wait until
12863 after processing the initializer. */
12866 if (parser->in_unbraced_linkage_specification_p)
12867 decl_specifiers->storage_class = sc_extern;
12868 decl = start_decl (declarator, decl_specifiers,
12869 is_initialized, attributes, prefix_attributes,
12873 /* Enter the SCOPE. That way unqualified names appearing in the
12874 initializer will be looked up in SCOPE. */
12875 pushed_scope = push_scope (scope);
12877 /* Perform deferred access control checks, now that we know in which
12878 SCOPE the declared entity resides. */
12879 if (!member_p && decl)
12881 tree saved_current_function_decl = NULL_TREE;
12883 /* If the entity being declared is a function, pretend that we
12884 are in its scope. If it is a `friend', it may have access to
12885 things that would not otherwise be accessible. */
12886 if (TREE_CODE (decl) == FUNCTION_DECL)
12888 saved_current_function_decl = current_function_decl;
12889 current_function_decl = decl;
12892 /* Perform access checks for template parameters. */
12893 cp_parser_perform_template_parameter_access_checks (checks);
12895 /* Perform the access control checks for the declarator and the
12896 decl-specifiers. */
12897 perform_deferred_access_checks ();
12899 /* Restore the saved value. */
12900 if (TREE_CODE (decl) == FUNCTION_DECL)
12901 current_function_decl = saved_current_function_decl;
12904 /* Parse the initializer. */
12905 initializer = NULL_TREE;
12906 is_direct_init = false;
12907 is_non_constant_init = true;
12908 if (is_initialized)
12910 if (function_declarator_p (declarator))
12912 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12913 if (initialization_kind == CPP_EQ)
12914 initializer = cp_parser_pure_specifier (parser);
12917 /* If the declaration was erroneous, we don't really
12918 know what the user intended, so just silently
12919 consume the initializer. */
12920 if (decl != error_mark_node)
12921 error ("%Hinitializer provided for function",
12922 &initializer_start_token->location);
12923 cp_parser_skip_to_closing_parenthesis (parser,
12924 /*recovering=*/true,
12925 /*or_comma=*/false,
12926 /*consume_paren=*/true);
12930 initializer = cp_parser_initializer (parser,
12932 &is_non_constant_init);
12935 /* The old parser allows attributes to appear after a parenthesized
12936 initializer. Mark Mitchell proposed removing this functionality
12937 on the GCC mailing lists on 2002-08-13. This parser accepts the
12938 attributes -- but ignores them. */
12939 if (cp_parser_allow_gnu_extensions_p (parser)
12940 && initialization_kind == CPP_OPEN_PAREN)
12941 if (cp_parser_attributes_opt (parser))
12942 warning (OPT_Wattributes,
12943 "attributes after parenthesized initializer ignored");
12945 /* For an in-class declaration, use `grokfield' to create the
12951 pop_scope (pushed_scope);
12952 pushed_scope = false;
12954 decl = grokfield (declarator, decl_specifiers,
12955 initializer, !is_non_constant_init,
12956 /*asmspec=*/NULL_TREE,
12957 prefix_attributes);
12958 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12959 cp_parser_save_default_args (parser, decl);
12962 /* Finish processing the declaration. But, skip friend
12964 if (!friend_p && decl && decl != error_mark_node)
12966 cp_finish_decl (decl,
12967 initializer, !is_non_constant_init,
12969 /* If the initializer is in parentheses, then this is
12970 a direct-initialization, which means that an
12971 `explicit' constructor is OK. Otherwise, an
12972 `explicit' constructor cannot be used. */
12973 ((is_direct_init || !is_initialized)
12974 ? 0 : LOOKUP_ONLYCONVERTING));
12976 else if ((cxx_dialect != cxx98) && friend_p
12977 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12978 /* Core issue #226 (C++0x only): A default template-argument
12979 shall not be specified in a friend class template
12981 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12982 /*is_partial=*/0, /*is_friend_decl=*/1);
12984 if (!friend_p && pushed_scope)
12985 pop_scope (pushed_scope);
12990 /* Parse a declarator.
12994 ptr-operator declarator
12996 abstract-declarator:
12997 ptr-operator abstract-declarator [opt]
12998 direct-abstract-declarator
13003 attributes [opt] direct-declarator
13004 attributes [opt] ptr-operator declarator
13006 abstract-declarator:
13007 attributes [opt] ptr-operator abstract-declarator [opt]
13008 attributes [opt] direct-abstract-declarator
13010 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13011 detect constructor, destructor or conversion operators. It is set
13012 to -1 if the declarator is a name, and +1 if it is a
13013 function. Otherwise it is set to zero. Usually you just want to
13014 test for >0, but internally the negative value is used.
13016 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13017 a decl-specifier-seq unless it declares a constructor, destructor,
13018 or conversion. It might seem that we could check this condition in
13019 semantic analysis, rather than parsing, but that makes it difficult
13020 to handle something like `f()'. We want to notice that there are
13021 no decl-specifiers, and therefore realize that this is an
13022 expression, not a declaration.)
13024 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13025 the declarator is a direct-declarator of the form "(...)".
13027 MEMBER_P is true iff this declarator is a member-declarator. */
13029 static cp_declarator *
13030 cp_parser_declarator (cp_parser* parser,
13031 cp_parser_declarator_kind dcl_kind,
13032 int* ctor_dtor_or_conv_p,
13033 bool* parenthesized_p,
13037 cp_declarator *declarator;
13038 enum tree_code code;
13039 cp_cv_quals cv_quals;
13041 tree attributes = NULL_TREE;
13043 /* Assume this is not a constructor, destructor, or type-conversion
13045 if (ctor_dtor_or_conv_p)
13046 *ctor_dtor_or_conv_p = 0;
13048 if (cp_parser_allow_gnu_extensions_p (parser))
13049 attributes = cp_parser_attributes_opt (parser);
13051 /* Peek at the next token. */
13052 token = cp_lexer_peek_token (parser->lexer);
13054 /* Check for the ptr-operator production. */
13055 cp_parser_parse_tentatively (parser);
13056 /* Parse the ptr-operator. */
13057 code = cp_parser_ptr_operator (parser,
13060 /* If that worked, then we have a ptr-operator. */
13061 if (cp_parser_parse_definitely (parser))
13063 /* If a ptr-operator was found, then this declarator was not
13065 if (parenthesized_p)
13066 *parenthesized_p = true;
13067 /* The dependent declarator is optional if we are parsing an
13068 abstract-declarator. */
13069 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13070 cp_parser_parse_tentatively (parser);
13072 /* Parse the dependent declarator. */
13073 declarator = cp_parser_declarator (parser, dcl_kind,
13074 /*ctor_dtor_or_conv_p=*/NULL,
13075 /*parenthesized_p=*/NULL,
13076 /*member_p=*/false);
13078 /* If we are parsing an abstract-declarator, we must handle the
13079 case where the dependent declarator is absent. */
13080 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13081 && !cp_parser_parse_definitely (parser))
13084 declarator = cp_parser_make_indirect_declarator
13085 (code, class_type, cv_quals, declarator);
13087 /* Everything else is a direct-declarator. */
13090 if (parenthesized_p)
13091 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13093 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13094 ctor_dtor_or_conv_p,
13098 if (attributes && declarator && declarator != cp_error_declarator)
13099 declarator->attributes = attributes;
13104 /* Parse a direct-declarator or direct-abstract-declarator.
13108 direct-declarator ( parameter-declaration-clause )
13109 cv-qualifier-seq [opt]
13110 exception-specification [opt]
13111 direct-declarator [ constant-expression [opt] ]
13114 direct-abstract-declarator:
13115 direct-abstract-declarator [opt]
13116 ( parameter-declaration-clause )
13117 cv-qualifier-seq [opt]
13118 exception-specification [opt]
13119 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13120 ( abstract-declarator )
13122 Returns a representation of the declarator. DCL_KIND is
13123 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13124 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13125 we are parsing a direct-declarator. It is
13126 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13127 of ambiguity we prefer an abstract declarator, as per
13128 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13129 cp_parser_declarator. */
13131 static cp_declarator *
13132 cp_parser_direct_declarator (cp_parser* parser,
13133 cp_parser_declarator_kind dcl_kind,
13134 int* ctor_dtor_or_conv_p,
13138 cp_declarator *declarator = NULL;
13139 tree scope = NULL_TREE;
13140 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13141 bool saved_in_declarator_p = parser->in_declarator_p;
13143 tree pushed_scope = NULL_TREE;
13147 /* Peek at the next token. */
13148 token = cp_lexer_peek_token (parser->lexer);
13149 if (token->type == CPP_OPEN_PAREN)
13151 /* This is either a parameter-declaration-clause, or a
13152 parenthesized declarator. When we know we are parsing a
13153 named declarator, it must be a parenthesized declarator
13154 if FIRST is true. For instance, `(int)' is a
13155 parameter-declaration-clause, with an omitted
13156 direct-abstract-declarator. But `((*))', is a
13157 parenthesized abstract declarator. Finally, when T is a
13158 template parameter `(T)' is a
13159 parameter-declaration-clause, and not a parenthesized
13162 We first try and parse a parameter-declaration-clause,
13163 and then try a nested declarator (if FIRST is true).
13165 It is not an error for it not to be a
13166 parameter-declaration-clause, even when FIRST is
13172 The first is the declaration of a function while the
13173 second is the definition of a variable, including its
13176 Having seen only the parenthesis, we cannot know which of
13177 these two alternatives should be selected. Even more
13178 complex are examples like:
13183 The former is a function-declaration; the latter is a
13184 variable initialization.
13186 Thus again, we try a parameter-declaration-clause, and if
13187 that fails, we back out and return. */
13189 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13192 unsigned saved_num_template_parameter_lists;
13193 bool is_declarator = false;
13196 /* In a member-declarator, the only valid interpretation
13197 of a parenthesis is the start of a
13198 parameter-declaration-clause. (It is invalid to
13199 initialize a static data member with a parenthesized
13200 initializer; only the "=" form of initialization is
13203 cp_parser_parse_tentatively (parser);
13205 /* Consume the `('. */
13206 cp_lexer_consume_token (parser->lexer);
13209 /* If this is going to be an abstract declarator, we're
13210 in a declarator and we can't have default args. */
13211 parser->default_arg_ok_p = false;
13212 parser->in_declarator_p = true;
13215 /* Inside the function parameter list, surrounding
13216 template-parameter-lists do not apply. */
13217 saved_num_template_parameter_lists
13218 = parser->num_template_parameter_lists;
13219 parser->num_template_parameter_lists = 0;
13221 begin_scope (sk_function_parms, NULL_TREE);
13223 /* Parse the parameter-declaration-clause. */
13224 params = cp_parser_parameter_declaration_clause (parser);
13226 parser->num_template_parameter_lists
13227 = saved_num_template_parameter_lists;
13229 /* If all went well, parse the cv-qualifier-seq and the
13230 exception-specification. */
13231 if (member_p || cp_parser_parse_definitely (parser))
13233 cp_cv_quals cv_quals;
13234 tree exception_specification;
13237 is_declarator = true;
13239 if (ctor_dtor_or_conv_p)
13240 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13242 /* Consume the `)'. */
13243 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13245 /* Parse the cv-qualifier-seq. */
13246 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13247 /* And the exception-specification. */
13248 exception_specification
13249 = cp_parser_exception_specification_opt (parser);
13252 = cp_parser_late_return_type_opt (parser);
13254 /* Create the function-declarator. */
13255 declarator = make_call_declarator (declarator,
13258 exception_specification,
13260 /* Any subsequent parameter lists are to do with
13261 return type, so are not those of the declared
13263 parser->default_arg_ok_p = false;
13266 /* Remove the function parms from scope. */
13267 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13268 pop_binding (DECL_NAME (t), t);
13272 /* Repeat the main loop. */
13276 /* If this is the first, we can try a parenthesized
13280 bool saved_in_type_id_in_expr_p;
13282 parser->default_arg_ok_p = saved_default_arg_ok_p;
13283 parser->in_declarator_p = saved_in_declarator_p;
13285 /* Consume the `('. */
13286 cp_lexer_consume_token (parser->lexer);
13287 /* Parse the nested declarator. */
13288 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13289 parser->in_type_id_in_expr_p = true;
13291 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13292 /*parenthesized_p=*/NULL,
13294 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13296 /* Expect a `)'. */
13297 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13298 declarator = cp_error_declarator;
13299 if (declarator == cp_error_declarator)
13302 goto handle_declarator;
13304 /* Otherwise, we must be done. */
13308 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13309 && token->type == CPP_OPEN_SQUARE)
13311 /* Parse an array-declarator. */
13314 if (ctor_dtor_or_conv_p)
13315 *ctor_dtor_or_conv_p = 0;
13318 parser->default_arg_ok_p = false;
13319 parser->in_declarator_p = true;
13320 /* Consume the `['. */
13321 cp_lexer_consume_token (parser->lexer);
13322 /* Peek at the next token. */
13323 token = cp_lexer_peek_token (parser->lexer);
13324 /* If the next token is `]', then there is no
13325 constant-expression. */
13326 if (token->type != CPP_CLOSE_SQUARE)
13328 bool non_constant_p;
13331 = cp_parser_constant_expression (parser,
13332 /*allow_non_constant=*/true,
13334 if (!non_constant_p)
13335 bounds = fold_non_dependent_expr (bounds);
13336 /* Normally, the array bound must be an integral constant
13337 expression. However, as an extension, we allow VLAs
13338 in function scopes as long as they aren't part of a
13339 parameter declaration. */
13340 else if (!parser->in_function_body
13341 || current_binding_level->kind == sk_function_parms)
13343 cp_parser_error (parser,
13344 "array bound is not an integer constant");
13345 bounds = error_mark_node;
13347 else if (processing_template_decl && !error_operand_p (bounds))
13349 /* Remember this wasn't a constant-expression. */
13350 bounds = build_nop (TREE_TYPE (bounds), bounds);
13351 TREE_SIDE_EFFECTS (bounds) = 1;
13355 bounds = NULL_TREE;
13356 /* Look for the closing `]'. */
13357 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13359 declarator = cp_error_declarator;
13363 declarator = make_array_declarator (declarator, bounds);
13365 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13367 tree qualifying_scope;
13368 tree unqualified_name;
13369 special_function_kind sfk;
13371 bool pack_expansion_p = false;
13372 cp_token *declarator_id_start_token;
13374 /* Parse a declarator-id */
13375 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13378 cp_parser_parse_tentatively (parser);
13380 /* If we see an ellipsis, we should be looking at a
13382 if (token->type == CPP_ELLIPSIS)
13384 /* Consume the `...' */
13385 cp_lexer_consume_token (parser->lexer);
13387 pack_expansion_p = true;
13391 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13393 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13394 qualifying_scope = parser->scope;
13399 if (!unqualified_name && pack_expansion_p)
13401 /* Check whether an error occurred. */
13402 okay = !cp_parser_error_occurred (parser);
13404 /* We already consumed the ellipsis to mark a
13405 parameter pack, but we have no way to report it,
13406 so abort the tentative parse. We will be exiting
13407 immediately anyway. */
13408 cp_parser_abort_tentative_parse (parser);
13411 okay = cp_parser_parse_definitely (parser);
13414 unqualified_name = error_mark_node;
13415 else if (unqualified_name
13416 && (qualifying_scope
13417 || (TREE_CODE (unqualified_name)
13418 != IDENTIFIER_NODE)))
13420 cp_parser_error (parser, "expected unqualified-id");
13421 unqualified_name = error_mark_node;
13425 if (!unqualified_name)
13427 if (unqualified_name == error_mark_node)
13429 declarator = cp_error_declarator;
13430 pack_expansion_p = false;
13431 declarator->parameter_pack_p = false;
13435 if (qualifying_scope && at_namespace_scope_p ()
13436 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13438 /* In the declaration of a member of a template class
13439 outside of the class itself, the SCOPE will sometimes
13440 be a TYPENAME_TYPE. For example, given:
13442 template <typename T>
13443 int S<T>::R::i = 3;
13445 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13446 this context, we must resolve S<T>::R to an ordinary
13447 type, rather than a typename type.
13449 The reason we normally avoid resolving TYPENAME_TYPEs
13450 is that a specialization of `S' might render
13451 `S<T>::R' not a type. However, if `S' is
13452 specialized, then this `i' will not be used, so there
13453 is no harm in resolving the types here. */
13456 /* Resolve the TYPENAME_TYPE. */
13457 type = resolve_typename_type (qualifying_scope,
13458 /*only_current_p=*/false);
13459 /* If that failed, the declarator is invalid. */
13460 if (TREE_CODE (type) == TYPENAME_TYPE)
13461 error ("%H%<%T::%E%> is not a type",
13462 &declarator_id_start_token->location,
13463 TYPE_CONTEXT (qualifying_scope),
13464 TYPE_IDENTIFIER (qualifying_scope));
13465 qualifying_scope = type;
13470 if (unqualified_name)
13474 if (qualifying_scope
13475 && CLASS_TYPE_P (qualifying_scope))
13476 class_type = qualifying_scope;
13478 class_type = current_class_type;
13480 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13482 tree name_type = TREE_TYPE (unqualified_name);
13483 if (class_type && same_type_p (name_type, class_type))
13485 if (qualifying_scope
13486 && CLASSTYPE_USE_TEMPLATE (name_type))
13488 error ("%Hinvalid use of constructor as a template",
13489 &declarator_id_start_token->location);
13490 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13491 "name the constructor in a qualified name",
13493 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13494 class_type, name_type);
13495 declarator = cp_error_declarator;
13499 unqualified_name = constructor_name (class_type);
13503 /* We do not attempt to print the declarator
13504 here because we do not have enough
13505 information about its original syntactic
13507 cp_parser_error (parser, "invalid declarator");
13508 declarator = cp_error_declarator;
13515 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13516 sfk = sfk_destructor;
13517 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13518 sfk = sfk_conversion;
13519 else if (/* There's no way to declare a constructor
13520 for an anonymous type, even if the type
13521 got a name for linkage purposes. */
13522 !TYPE_WAS_ANONYMOUS (class_type)
13523 && constructor_name_p (unqualified_name,
13526 unqualified_name = constructor_name (class_type);
13527 sfk = sfk_constructor;
13530 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13531 *ctor_dtor_or_conv_p = -1;
13534 declarator = make_id_declarator (qualifying_scope,
13537 declarator->id_loc = token->location;
13538 declarator->parameter_pack_p = pack_expansion_p;
13540 if (pack_expansion_p)
13541 maybe_warn_variadic_templates ();
13543 handle_declarator:;
13544 scope = get_scope_of_declarator (declarator);
13546 /* Any names that appear after the declarator-id for a
13547 member are looked up in the containing scope. */
13548 pushed_scope = push_scope (scope);
13549 parser->in_declarator_p = true;
13550 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13551 || (declarator && declarator->kind == cdk_id))
13552 /* Default args are only allowed on function
13554 parser->default_arg_ok_p = saved_default_arg_ok_p;
13556 parser->default_arg_ok_p = false;
13565 /* For an abstract declarator, we might wind up with nothing at this
13566 point. That's an error; the declarator is not optional. */
13568 cp_parser_error (parser, "expected declarator");
13570 /* If we entered a scope, we must exit it now. */
13572 pop_scope (pushed_scope);
13574 parser->default_arg_ok_p = saved_default_arg_ok_p;
13575 parser->in_declarator_p = saved_in_declarator_p;
13580 /* Parse a ptr-operator.
13583 * cv-qualifier-seq [opt]
13585 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13590 & cv-qualifier-seq [opt]
13592 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13593 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13594 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13595 filled in with the TYPE containing the member. *CV_QUALS is
13596 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13597 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13598 Note that the tree codes returned by this function have nothing
13599 to do with the types of trees that will be eventually be created
13600 to represent the pointer or reference type being parsed. They are
13601 just constants with suggestive names. */
13602 static enum tree_code
13603 cp_parser_ptr_operator (cp_parser* parser,
13605 cp_cv_quals *cv_quals)
13607 enum tree_code code = ERROR_MARK;
13610 /* Assume that it's not a pointer-to-member. */
13612 /* And that there are no cv-qualifiers. */
13613 *cv_quals = TYPE_UNQUALIFIED;
13615 /* Peek at the next token. */
13616 token = cp_lexer_peek_token (parser->lexer);
13618 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13619 if (token->type == CPP_MULT)
13620 code = INDIRECT_REF;
13621 else if (token->type == CPP_AND)
13623 else if ((cxx_dialect != cxx98) &&
13624 token->type == CPP_AND_AND) /* C++0x only */
13625 code = NON_LVALUE_EXPR;
13627 if (code != ERROR_MARK)
13629 /* Consume the `*', `&' or `&&'. */
13630 cp_lexer_consume_token (parser->lexer);
13632 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13633 `&', if we are allowing GNU extensions. (The only qualifier
13634 that can legally appear after `&' is `restrict', but that is
13635 enforced during semantic analysis. */
13636 if (code == INDIRECT_REF
13637 || cp_parser_allow_gnu_extensions_p (parser))
13638 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13642 /* Try the pointer-to-member case. */
13643 cp_parser_parse_tentatively (parser);
13644 /* Look for the optional `::' operator. */
13645 cp_parser_global_scope_opt (parser,
13646 /*current_scope_valid_p=*/false);
13647 /* Look for the nested-name specifier. */
13648 token = cp_lexer_peek_token (parser->lexer);
13649 cp_parser_nested_name_specifier (parser,
13650 /*typename_keyword_p=*/false,
13651 /*check_dependency_p=*/true,
13653 /*is_declaration=*/false);
13654 /* If we found it, and the next token is a `*', then we are
13655 indeed looking at a pointer-to-member operator. */
13656 if (!cp_parser_error_occurred (parser)
13657 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13659 /* Indicate that the `*' operator was used. */
13660 code = INDIRECT_REF;
13662 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13663 error ("%H%qD is a namespace", &token->location, parser->scope);
13666 /* The type of which the member is a member is given by the
13668 *type = parser->scope;
13669 /* The next name will not be qualified. */
13670 parser->scope = NULL_TREE;
13671 parser->qualifying_scope = NULL_TREE;
13672 parser->object_scope = NULL_TREE;
13673 /* Look for the optional cv-qualifier-seq. */
13674 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13677 /* If that didn't work we don't have a ptr-operator. */
13678 if (!cp_parser_parse_definitely (parser))
13679 cp_parser_error (parser, "expected ptr-operator");
13685 /* Parse an (optional) cv-qualifier-seq.
13688 cv-qualifier cv-qualifier-seq [opt]
13699 Returns a bitmask representing the cv-qualifiers. */
13702 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13704 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13709 cp_cv_quals cv_qualifier;
13711 /* Peek at the next token. */
13712 token = cp_lexer_peek_token (parser->lexer);
13713 /* See if it's a cv-qualifier. */
13714 switch (token->keyword)
13717 cv_qualifier = TYPE_QUAL_CONST;
13721 cv_qualifier = TYPE_QUAL_VOLATILE;
13725 cv_qualifier = TYPE_QUAL_RESTRICT;
13729 cv_qualifier = TYPE_UNQUALIFIED;
13736 if (cv_quals & cv_qualifier)
13738 error ("%Hduplicate cv-qualifier", &token->location);
13739 cp_lexer_purge_token (parser->lexer);
13743 cp_lexer_consume_token (parser->lexer);
13744 cv_quals |= cv_qualifier;
13751 /* Parse a late-specified return type, if any. This is not a separate
13752 non-terminal, but part of a function declarator, which looks like
13754 -> trailing-type-specifier-seq abstract-declarator(opt)
13756 Returns the type indicated by the type-id. */
13759 cp_parser_late_return_type_opt (cp_parser* parser)
13763 /* Peek at the next token. */
13764 token = cp_lexer_peek_token (parser->lexer);
13765 /* A late-specified return type is indicated by an initial '->'. */
13766 if (token->type != CPP_DEREF)
13769 /* Consume the ->. */
13770 cp_lexer_consume_token (parser->lexer);
13772 return cp_parser_trailing_type_id (parser);
13775 /* Parse a declarator-id.
13779 :: [opt] nested-name-specifier [opt] type-name
13781 In the `id-expression' case, the value returned is as for
13782 cp_parser_id_expression if the id-expression was an unqualified-id.
13783 If the id-expression was a qualified-id, then a SCOPE_REF is
13784 returned. The first operand is the scope (either a NAMESPACE_DECL
13785 or TREE_TYPE), but the second is still just a representation of an
13789 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13792 /* The expression must be an id-expression. Assume that qualified
13793 names are the names of types so that:
13796 int S<T>::R::i = 3;
13798 will work; we must treat `S<T>::R' as the name of a type.
13799 Similarly, assume that qualified names are templates, where
13803 int S<T>::R<T>::i = 3;
13806 id = cp_parser_id_expression (parser,
13807 /*template_keyword_p=*/false,
13808 /*check_dependency_p=*/false,
13809 /*template_p=*/NULL,
13810 /*declarator_p=*/true,
13812 if (id && BASELINK_P (id))
13813 id = BASELINK_FUNCTIONS (id);
13817 /* Parse a type-id.
13820 type-specifier-seq abstract-declarator [opt]
13822 Returns the TYPE specified. */
13825 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
13826 bool is_trailing_return)
13828 cp_decl_specifier_seq type_specifier_seq;
13829 cp_declarator *abstract_declarator;
13831 /* Parse the type-specifier-seq. */
13832 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13833 is_trailing_return,
13834 &type_specifier_seq);
13835 if (type_specifier_seq.type == error_mark_node)
13836 return error_mark_node;
13838 /* There might or might not be an abstract declarator. */
13839 cp_parser_parse_tentatively (parser);
13840 /* Look for the declarator. */
13841 abstract_declarator
13842 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13843 /*parenthesized_p=*/NULL,
13844 /*member_p=*/false);
13845 /* Check to see if there really was a declarator. */
13846 if (!cp_parser_parse_definitely (parser))
13847 abstract_declarator = NULL;
13849 if (type_specifier_seq.type
13850 && type_uses_auto (type_specifier_seq.type))
13852 /* A type-id with type 'auto' is only ok if the abstract declarator
13853 is a function declarator with a late-specified return type. */
13854 if (abstract_declarator
13855 && abstract_declarator->kind == cdk_function
13856 && abstract_declarator->u.function.late_return_type)
13860 error ("invalid use of %<auto%>");
13861 return error_mark_node;
13865 return groktypename (&type_specifier_seq, abstract_declarator,
13869 static tree cp_parser_type_id (cp_parser *parser)
13871 return cp_parser_type_id_1 (parser, false, false);
13874 static tree cp_parser_template_type_arg (cp_parser *parser)
13876 return cp_parser_type_id_1 (parser, true, false);
13879 static tree cp_parser_trailing_type_id (cp_parser *parser)
13881 return cp_parser_type_id_1 (parser, false, true);
13884 /* Parse a type-specifier-seq.
13886 type-specifier-seq:
13887 type-specifier type-specifier-seq [opt]
13891 type-specifier-seq:
13892 attributes type-specifier-seq [opt]
13894 If IS_CONDITION is true, we are at the start of a "condition",
13895 e.g., we've just seen "if (".
13897 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
13898 i.e. we've just seen "->".
13900 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13903 cp_parser_type_specifier_seq (cp_parser* parser,
13905 bool is_trailing_return,
13906 cp_decl_specifier_seq *type_specifier_seq)
13908 bool seen_type_specifier = false;
13909 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13910 cp_token *start_token = NULL;
13912 /* Clear the TYPE_SPECIFIER_SEQ. */
13913 clear_decl_specs (type_specifier_seq);
13915 /* In the context of a trailing return type, enum E { } is an
13916 elaborated-type-specifier followed by a function-body, not an
13918 if (is_trailing_return)
13919 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
13921 /* Parse the type-specifiers and attributes. */
13924 tree type_specifier;
13925 bool is_cv_qualifier;
13927 /* Check for attributes first. */
13928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13930 type_specifier_seq->attributes =
13931 chainon (type_specifier_seq->attributes,
13932 cp_parser_attributes_opt (parser));
13936 /* record the token of the beginning of the type specifier seq,
13937 for error reporting purposes*/
13939 start_token = cp_lexer_peek_token (parser->lexer);
13941 /* Look for the type-specifier. */
13942 type_specifier = cp_parser_type_specifier (parser,
13944 type_specifier_seq,
13945 /*is_declaration=*/false,
13948 if (!type_specifier)
13950 /* If the first type-specifier could not be found, this is not a
13951 type-specifier-seq at all. */
13952 if (!seen_type_specifier)
13954 cp_parser_error (parser, "expected type-specifier");
13955 type_specifier_seq->type = error_mark_node;
13958 /* If subsequent type-specifiers could not be found, the
13959 type-specifier-seq is complete. */
13963 seen_type_specifier = true;
13964 /* The standard says that a condition can be:
13966 type-specifier-seq declarator = assignment-expression
13973 we should treat the "S" as a declarator, not as a
13974 type-specifier. The standard doesn't say that explicitly for
13975 type-specifier-seq, but it does say that for
13976 decl-specifier-seq in an ordinary declaration. Perhaps it
13977 would be clearer just to allow a decl-specifier-seq here, and
13978 then add a semantic restriction that if any decl-specifiers
13979 that are not type-specifiers appear, the program is invalid. */
13980 if (is_condition && !is_cv_qualifier)
13981 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13984 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13987 /* Parse a parameter-declaration-clause.
13989 parameter-declaration-clause:
13990 parameter-declaration-list [opt] ... [opt]
13991 parameter-declaration-list , ...
13993 Returns a representation for the parameter declarations. A return
13994 value of NULL indicates a parameter-declaration-clause consisting
13995 only of an ellipsis. */
13998 cp_parser_parameter_declaration_clause (cp_parser* parser)
14005 /* Peek at the next token. */
14006 token = cp_lexer_peek_token (parser->lexer);
14007 /* Check for trivial parameter-declaration-clauses. */
14008 if (token->type == CPP_ELLIPSIS)
14010 /* Consume the `...' token. */
14011 cp_lexer_consume_token (parser->lexer);
14014 else if (token->type == CPP_CLOSE_PAREN)
14015 /* There are no parameters. */
14017 #ifndef NO_IMPLICIT_EXTERN_C
14018 if (in_system_header && current_class_type == NULL
14019 && current_lang_name == lang_name_c)
14023 return void_list_node;
14025 /* Check for `(void)', too, which is a special case. */
14026 else if (token->keyword == RID_VOID
14027 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14028 == CPP_CLOSE_PAREN))
14030 /* Consume the `void' token. */
14031 cp_lexer_consume_token (parser->lexer);
14032 /* There are no parameters. */
14033 return void_list_node;
14036 /* Parse the parameter-declaration-list. */
14037 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14038 /* If a parse error occurred while parsing the
14039 parameter-declaration-list, then the entire
14040 parameter-declaration-clause is erroneous. */
14044 /* Peek at the next token. */
14045 token = cp_lexer_peek_token (parser->lexer);
14046 /* If it's a `,', the clause should terminate with an ellipsis. */
14047 if (token->type == CPP_COMMA)
14049 /* Consume the `,'. */
14050 cp_lexer_consume_token (parser->lexer);
14051 /* Expect an ellipsis. */
14053 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14055 /* It might also be `...' if the optional trailing `,' was
14057 else if (token->type == CPP_ELLIPSIS)
14059 /* Consume the `...' token. */
14060 cp_lexer_consume_token (parser->lexer);
14061 /* And remember that we saw it. */
14065 ellipsis_p = false;
14067 /* Finish the parameter list. */
14069 parameters = chainon (parameters, void_list_node);
14074 /* Parse a parameter-declaration-list.
14076 parameter-declaration-list:
14077 parameter-declaration
14078 parameter-declaration-list , parameter-declaration
14080 Returns a representation of the parameter-declaration-list, as for
14081 cp_parser_parameter_declaration_clause. However, the
14082 `void_list_node' is never appended to the list. Upon return,
14083 *IS_ERROR will be true iff an error occurred. */
14086 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14088 tree parameters = NULL_TREE;
14089 tree *tail = ¶meters;
14090 bool saved_in_unbraced_linkage_specification_p;
14092 /* Assume all will go well. */
14094 /* The special considerations that apply to a function within an
14095 unbraced linkage specifications do not apply to the parameters
14096 to the function. */
14097 saved_in_unbraced_linkage_specification_p
14098 = parser->in_unbraced_linkage_specification_p;
14099 parser->in_unbraced_linkage_specification_p = false;
14101 /* Look for more parameters. */
14104 cp_parameter_declarator *parameter;
14105 tree decl = error_mark_node;
14106 bool parenthesized_p;
14107 /* Parse the parameter. */
14109 = cp_parser_parameter_declaration (parser,
14110 /*template_parm_p=*/false,
14113 /* We don't know yet if the enclosing context is deprecated, so wait
14114 and warn in grokparms if appropriate. */
14115 deprecated_state = DEPRECATED_SUPPRESS;
14118 decl = grokdeclarator (parameter->declarator,
14119 ¶meter->decl_specifiers,
14121 parameter->default_argument != NULL_TREE,
14122 ¶meter->decl_specifiers.attributes);
14124 deprecated_state = DEPRECATED_NORMAL;
14126 /* If a parse error occurred parsing the parameter declaration,
14127 then the entire parameter-declaration-list is erroneous. */
14128 if (decl == error_mark_node)
14131 parameters = error_mark_node;
14135 if (parameter->decl_specifiers.attributes)
14136 cplus_decl_attributes (&decl,
14137 parameter->decl_specifiers.attributes,
14139 if (DECL_NAME (decl))
14140 decl = pushdecl (decl);
14142 /* Add the new parameter to the list. */
14143 *tail = build_tree_list (parameter->default_argument, decl);
14144 tail = &TREE_CHAIN (*tail);
14146 /* Peek at the next token. */
14147 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14148 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14149 /* These are for Objective-C++ */
14150 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14151 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14152 /* The parameter-declaration-list is complete. */
14154 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14158 /* Peek at the next token. */
14159 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14160 /* If it's an ellipsis, then the list is complete. */
14161 if (token->type == CPP_ELLIPSIS)
14163 /* Otherwise, there must be more parameters. Consume the
14165 cp_lexer_consume_token (parser->lexer);
14166 /* When parsing something like:
14168 int i(float f, double d)
14170 we can tell after seeing the declaration for "f" that we
14171 are not looking at an initialization of a variable "i",
14172 but rather at the declaration of a function "i".
14174 Due to the fact that the parsing of template arguments
14175 (as specified to a template-id) requires backtracking we
14176 cannot use this technique when inside a template argument
14178 if (!parser->in_template_argument_list_p
14179 && !parser->in_type_id_in_expr_p
14180 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14181 /* However, a parameter-declaration of the form
14182 "foat(f)" (which is a valid declaration of a
14183 parameter "f") can also be interpreted as an
14184 expression (the conversion of "f" to "float"). */
14185 && !parenthesized_p)
14186 cp_parser_commit_to_tentative_parse (parser);
14190 cp_parser_error (parser, "expected %<,%> or %<...%>");
14191 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14192 cp_parser_skip_to_closing_parenthesis (parser,
14193 /*recovering=*/true,
14194 /*or_comma=*/false,
14195 /*consume_paren=*/false);
14200 parser->in_unbraced_linkage_specification_p
14201 = saved_in_unbraced_linkage_specification_p;
14206 /* Parse a parameter declaration.
14208 parameter-declaration:
14209 decl-specifier-seq ... [opt] declarator
14210 decl-specifier-seq declarator = assignment-expression
14211 decl-specifier-seq ... [opt] abstract-declarator [opt]
14212 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14214 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14215 declares a template parameter. (In that case, a non-nested `>'
14216 token encountered during the parsing of the assignment-expression
14217 is not interpreted as a greater-than operator.)
14219 Returns a representation of the parameter, or NULL if an error
14220 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14221 true iff the declarator is of the form "(p)". */
14223 static cp_parameter_declarator *
14224 cp_parser_parameter_declaration (cp_parser *parser,
14225 bool template_parm_p,
14226 bool *parenthesized_p)
14228 int declares_class_or_enum;
14229 bool greater_than_is_operator_p;
14230 cp_decl_specifier_seq decl_specifiers;
14231 cp_declarator *declarator;
14232 tree default_argument;
14233 cp_token *token = NULL, *declarator_token_start = NULL;
14234 const char *saved_message;
14236 /* In a template parameter, `>' is not an operator.
14240 When parsing a default template-argument for a non-type
14241 template-parameter, the first non-nested `>' is taken as the end
14242 of the template parameter-list rather than a greater-than
14244 greater_than_is_operator_p = !template_parm_p;
14246 /* Type definitions may not appear in parameter types. */
14247 saved_message = parser->type_definition_forbidden_message;
14248 parser->type_definition_forbidden_message
14249 = "types may not be defined in parameter types";
14251 /* Parse the declaration-specifiers. */
14252 cp_parser_decl_specifier_seq (parser,
14253 CP_PARSER_FLAGS_NONE,
14255 &declares_class_or_enum);
14256 /* If an error occurred, there's no reason to attempt to parse the
14257 rest of the declaration. */
14258 if (cp_parser_error_occurred (parser))
14260 parser->type_definition_forbidden_message = saved_message;
14264 /* Peek at the next token. */
14265 token = cp_lexer_peek_token (parser->lexer);
14267 /* If the next token is a `)', `,', `=', `>', or `...', then there
14268 is no declarator. However, when variadic templates are enabled,
14269 there may be a declarator following `...'. */
14270 if (token->type == CPP_CLOSE_PAREN
14271 || token->type == CPP_COMMA
14272 || token->type == CPP_EQ
14273 || token->type == CPP_GREATER)
14276 if (parenthesized_p)
14277 *parenthesized_p = false;
14279 /* Otherwise, there should be a declarator. */
14282 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14283 parser->default_arg_ok_p = false;
14285 /* After seeing a decl-specifier-seq, if the next token is not a
14286 "(", there is no possibility that the code is a valid
14287 expression. Therefore, if parsing tentatively, we commit at
14289 if (!parser->in_template_argument_list_p
14290 /* In an expression context, having seen:
14294 we cannot be sure whether we are looking at a
14295 function-type (taking a "char" as a parameter) or a cast
14296 of some object of type "char" to "int". */
14297 && !parser->in_type_id_in_expr_p
14298 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14299 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14300 cp_parser_commit_to_tentative_parse (parser);
14301 /* Parse the declarator. */
14302 declarator_token_start = token;
14303 declarator = cp_parser_declarator (parser,
14304 CP_PARSER_DECLARATOR_EITHER,
14305 /*ctor_dtor_or_conv_p=*/NULL,
14307 /*member_p=*/false);
14308 parser->default_arg_ok_p = saved_default_arg_ok_p;
14309 /* After the declarator, allow more attributes. */
14310 decl_specifiers.attributes
14311 = chainon (decl_specifiers.attributes,
14312 cp_parser_attributes_opt (parser));
14315 /* If the next token is an ellipsis, and we have not seen a
14316 declarator name, and the type of the declarator contains parameter
14317 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14318 a parameter pack expansion expression. Otherwise, leave the
14319 ellipsis for a C-style variadic function. */
14320 token = cp_lexer_peek_token (parser->lexer);
14321 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14323 tree type = decl_specifiers.type;
14325 if (type && DECL_P (type))
14326 type = TREE_TYPE (type);
14329 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14330 && declarator_can_be_parameter_pack (declarator)
14331 && (!declarator || !declarator->parameter_pack_p)
14332 && uses_parameter_packs (type))
14334 /* Consume the `...'. */
14335 cp_lexer_consume_token (parser->lexer);
14336 maybe_warn_variadic_templates ();
14338 /* Build a pack expansion type */
14340 declarator->parameter_pack_p = true;
14342 decl_specifiers.type = make_pack_expansion (type);
14346 /* The restriction on defining new types applies only to the type
14347 of the parameter, not to the default argument. */
14348 parser->type_definition_forbidden_message = saved_message;
14350 /* If the next token is `=', then process a default argument. */
14351 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14353 /* Consume the `='. */
14354 cp_lexer_consume_token (parser->lexer);
14356 /* If we are defining a class, then the tokens that make up the
14357 default argument must be saved and processed later. */
14358 if (!template_parm_p && at_class_scope_p ()
14359 && TYPE_BEING_DEFINED (current_class_type))
14361 unsigned depth = 0;
14362 int maybe_template_id = 0;
14363 cp_token *first_token;
14366 /* Add tokens until we have processed the entire default
14367 argument. We add the range [first_token, token). */
14368 first_token = cp_lexer_peek_token (parser->lexer);
14373 /* Peek at the next token. */
14374 token = cp_lexer_peek_token (parser->lexer);
14375 /* What we do depends on what token we have. */
14376 switch (token->type)
14378 /* In valid code, a default argument must be
14379 immediately followed by a `,' `)', or `...'. */
14381 if (depth == 0 && maybe_template_id)
14383 /* If we've seen a '<', we might be in a
14384 template-argument-list. Until Core issue 325 is
14385 resolved, we don't know how this situation ought
14386 to be handled, so try to DTRT. We check whether
14387 what comes after the comma is a valid parameter
14388 declaration list. If it is, then the comma ends
14389 the default argument; otherwise the default
14390 argument continues. */
14391 bool error = false;
14394 /* Set ITALP so cp_parser_parameter_declaration_list
14395 doesn't decide to commit to this parse. */
14396 bool saved_italp = parser->in_template_argument_list_p;
14397 parser->in_template_argument_list_p = true;
14399 cp_parser_parse_tentatively (parser);
14400 cp_lexer_consume_token (parser->lexer);
14401 begin_scope (sk_function_parms, NULL_TREE);
14402 cp_parser_parameter_declaration_list (parser, &error);
14403 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14404 pop_binding (DECL_NAME (t), t);
14406 if (!cp_parser_error_occurred (parser) && !error)
14408 cp_parser_abort_tentative_parse (parser);
14410 parser->in_template_argument_list_p = saved_italp;
14413 case CPP_CLOSE_PAREN:
14415 /* If we run into a non-nested `;', `}', or `]',
14416 then the code is invalid -- but the default
14417 argument is certainly over. */
14418 case CPP_SEMICOLON:
14419 case CPP_CLOSE_BRACE:
14420 case CPP_CLOSE_SQUARE:
14423 /* Update DEPTH, if necessary. */
14424 else if (token->type == CPP_CLOSE_PAREN
14425 || token->type == CPP_CLOSE_BRACE
14426 || token->type == CPP_CLOSE_SQUARE)
14430 case CPP_OPEN_PAREN:
14431 case CPP_OPEN_SQUARE:
14432 case CPP_OPEN_BRACE:
14438 /* This might be the comparison operator, or it might
14439 start a template argument list. */
14440 ++maybe_template_id;
14444 if (cxx_dialect == cxx98)
14446 /* Fall through for C++0x, which treats the `>>'
14447 operator like two `>' tokens in certain
14453 /* This might be an operator, or it might close a
14454 template argument list. But if a previous '<'
14455 started a template argument list, this will have
14456 closed it, so we can't be in one anymore. */
14457 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14458 if (maybe_template_id < 0)
14459 maybe_template_id = 0;
14463 /* If we run out of tokens, issue an error message. */
14465 case CPP_PRAGMA_EOL:
14466 error ("%Hfile ends in default argument", &token->location);
14472 /* In these cases, we should look for template-ids.
14473 For example, if the default argument is
14474 `X<int, double>()', we need to do name lookup to
14475 figure out whether or not `X' is a template; if
14476 so, the `,' does not end the default argument.
14478 That is not yet done. */
14485 /* If we've reached the end, stop. */
14489 /* Add the token to the token block. */
14490 token = cp_lexer_consume_token (parser->lexer);
14493 /* Create a DEFAULT_ARG to represent the unparsed default
14495 default_argument = make_node (DEFAULT_ARG);
14496 DEFARG_TOKENS (default_argument)
14497 = cp_token_cache_new (first_token, token);
14498 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14500 /* Outside of a class definition, we can just parse the
14501 assignment-expression. */
14504 token = cp_lexer_peek_token (parser->lexer);
14506 = cp_parser_default_argument (parser, template_parm_p);
14509 if (!parser->default_arg_ok_p)
14511 if (flag_permissive)
14512 warning (0, "deprecated use of default argument for parameter of non-function");
14515 error ("%Hdefault arguments are only "
14516 "permitted for function parameters",
14518 default_argument = NULL_TREE;
14521 else if ((declarator && declarator->parameter_pack_p)
14522 || (decl_specifiers.type
14523 && PACK_EXPANSION_P (decl_specifiers.type)))
14525 const char* kind = template_parm_p? "template " : "";
14527 /* Find the name of the parameter pack. */
14528 cp_declarator *id_declarator = declarator;
14529 while (id_declarator && id_declarator->kind != cdk_id)
14530 id_declarator = id_declarator->declarator;
14532 if (id_declarator && id_declarator->kind == cdk_id)
14533 error ("%H%sparameter pack %qD cannot have a default argument",
14534 &declarator_token_start->location,
14535 kind, id_declarator->u.id.unqualified_name);
14537 error ("%H%sparameter pack cannot have a default argument",
14538 &declarator_token_start->location, kind);
14540 default_argument = NULL_TREE;
14544 default_argument = NULL_TREE;
14546 return make_parameter_declarator (&decl_specifiers,
14551 /* Parse a default argument and return it.
14553 TEMPLATE_PARM_P is true if this is a default argument for a
14554 non-type template parameter. */
14556 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14558 tree default_argument = NULL_TREE;
14559 bool saved_greater_than_is_operator_p;
14560 bool saved_local_variables_forbidden_p;
14562 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14564 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14565 parser->greater_than_is_operator_p = !template_parm_p;
14566 /* Local variable names (and the `this' keyword) may not
14567 appear in a default argument. */
14568 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14569 parser->local_variables_forbidden_p = true;
14570 /* The default argument expression may cause implicitly
14571 defined member functions to be synthesized, which will
14572 result in garbage collection. We must treat this
14573 situation as if we were within the body of function so as
14574 to avoid collecting live data on the stack. */
14576 /* Parse the assignment-expression. */
14577 if (template_parm_p)
14578 push_deferring_access_checks (dk_no_deferred);
14580 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14581 if (template_parm_p)
14582 pop_deferring_access_checks ();
14583 /* Restore saved state. */
14585 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14586 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14588 return default_argument;
14591 /* Parse a function-body.
14594 compound_statement */
14597 cp_parser_function_body (cp_parser *parser)
14599 cp_parser_compound_statement (parser, NULL, false);
14602 /* Parse a ctor-initializer-opt followed by a function-body. Return
14603 true if a ctor-initializer was present. */
14606 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14609 bool ctor_initializer_p;
14611 /* Begin the function body. */
14612 body = begin_function_body ();
14613 /* Parse the optional ctor-initializer. */
14614 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14615 /* Parse the function-body. */
14616 cp_parser_function_body (parser);
14617 /* Finish the function body. */
14618 finish_function_body (body);
14620 return ctor_initializer_p;
14623 /* Parse an initializer.
14626 = initializer-clause
14627 ( expression-list )
14629 Returns an expression representing the initializer. If no
14630 initializer is present, NULL_TREE is returned.
14632 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14633 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14634 set to TRUE if there is no initializer present. If there is an
14635 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14636 is set to true; otherwise it is set to false. */
14639 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14640 bool* non_constant_p)
14645 /* Peek at the next token. */
14646 token = cp_lexer_peek_token (parser->lexer);
14648 /* Let our caller know whether or not this initializer was
14650 *is_direct_init = (token->type != CPP_EQ);
14651 /* Assume that the initializer is constant. */
14652 *non_constant_p = false;
14654 if (token->type == CPP_EQ)
14656 /* Consume the `='. */
14657 cp_lexer_consume_token (parser->lexer);
14658 /* Parse the initializer-clause. */
14659 init = cp_parser_initializer_clause (parser, non_constant_p);
14661 else if (token->type == CPP_OPEN_PAREN)
14662 init = cp_parser_parenthesized_expression_list (parser, false,
14664 /*allow_expansion_p=*/true,
14666 else if (token->type == CPP_OPEN_BRACE)
14668 maybe_warn_cpp0x ("extended initializer lists");
14669 init = cp_parser_braced_list (parser, non_constant_p);
14670 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14674 /* Anything else is an error. */
14675 cp_parser_error (parser, "expected initializer");
14676 init = error_mark_node;
14682 /* Parse an initializer-clause.
14684 initializer-clause:
14685 assignment-expression
14688 Returns an expression representing the initializer.
14690 If the `assignment-expression' production is used the value
14691 returned is simply a representation for the expression.
14693 Otherwise, calls cp_parser_braced_list. */
14696 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14700 /* Assume the expression is constant. */
14701 *non_constant_p = false;
14703 /* If it is not a `{', then we are looking at an
14704 assignment-expression. */
14705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14708 = cp_parser_constant_expression (parser,
14709 /*allow_non_constant_p=*/true,
14711 if (!*non_constant_p)
14712 initializer = fold_non_dependent_expr (initializer);
14715 initializer = cp_parser_braced_list (parser, non_constant_p);
14717 return initializer;
14720 /* Parse a brace-enclosed initializer list.
14723 { initializer-list , [opt] }
14726 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14727 the elements of the initializer-list (or NULL, if the last
14728 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14729 NULL_TREE. There is no way to detect whether or not the optional
14730 trailing `,' was provided. NON_CONSTANT_P is as for
14731 cp_parser_initializer. */
14734 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14738 /* Consume the `{' token. */
14739 cp_lexer_consume_token (parser->lexer);
14740 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14741 initializer = make_node (CONSTRUCTOR);
14742 /* If it's not a `}', then there is a non-trivial initializer. */
14743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14745 /* Parse the initializer list. */
14746 CONSTRUCTOR_ELTS (initializer)
14747 = cp_parser_initializer_list (parser, non_constant_p);
14748 /* A trailing `,' token is allowed. */
14749 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14750 cp_lexer_consume_token (parser->lexer);
14752 /* Now, there should be a trailing `}'. */
14753 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14754 TREE_TYPE (initializer) = init_list_type_node;
14755 return initializer;
14758 /* Parse an initializer-list.
14761 initializer-clause ... [opt]
14762 initializer-list , initializer-clause ... [opt]
14767 identifier : initializer-clause
14768 initializer-list, identifier : initializer-clause
14770 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14771 for the initializer. If the INDEX of the elt is non-NULL, it is the
14772 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14773 as for cp_parser_initializer. */
14775 static VEC(constructor_elt,gc) *
14776 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14778 VEC(constructor_elt,gc) *v = NULL;
14780 /* Assume all of the expressions are constant. */
14781 *non_constant_p = false;
14783 /* Parse the rest of the list. */
14789 bool clause_non_constant_p;
14791 /* If the next token is an identifier and the following one is a
14792 colon, we are looking at the GNU designated-initializer
14794 if (cp_parser_allow_gnu_extensions_p (parser)
14795 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14796 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14798 /* Warn the user that they are using an extension. */
14799 pedwarn (input_location, OPT_pedantic,
14800 "ISO C++ does not allow designated initializers");
14801 /* Consume the identifier. */
14802 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14803 /* Consume the `:'. */
14804 cp_lexer_consume_token (parser->lexer);
14807 identifier = NULL_TREE;
14809 /* Parse the initializer. */
14810 initializer = cp_parser_initializer_clause (parser,
14811 &clause_non_constant_p);
14812 /* If any clause is non-constant, so is the entire initializer. */
14813 if (clause_non_constant_p)
14814 *non_constant_p = true;
14816 /* If we have an ellipsis, this is an initializer pack
14818 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14820 /* Consume the `...'. */
14821 cp_lexer_consume_token (parser->lexer);
14823 /* Turn the initializer into an initializer expansion. */
14824 initializer = make_pack_expansion (initializer);
14827 /* Add it to the vector. */
14828 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14830 /* If the next token is not a comma, we have reached the end of
14832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14835 /* Peek at the next token. */
14836 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14837 /* If the next token is a `}', then we're still done. An
14838 initializer-clause can have a trailing `,' after the
14839 initializer-list and before the closing `}'. */
14840 if (token->type == CPP_CLOSE_BRACE)
14843 /* Consume the `,' token. */
14844 cp_lexer_consume_token (parser->lexer);
14850 /* Classes [gram.class] */
14852 /* Parse a class-name.
14858 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14859 to indicate that names looked up in dependent types should be
14860 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14861 keyword has been used to indicate that the name that appears next
14862 is a template. TAG_TYPE indicates the explicit tag given before
14863 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14864 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14865 is the class being defined in a class-head.
14867 Returns the TYPE_DECL representing the class. */
14870 cp_parser_class_name (cp_parser *parser,
14871 bool typename_keyword_p,
14872 bool template_keyword_p,
14873 enum tag_types tag_type,
14874 bool check_dependency_p,
14876 bool is_declaration)
14882 tree identifier = NULL_TREE;
14884 /* All class-names start with an identifier. */
14885 token = cp_lexer_peek_token (parser->lexer);
14886 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14888 cp_parser_error (parser, "expected class-name");
14889 return error_mark_node;
14892 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14893 to a template-id, so we save it here. */
14894 scope = parser->scope;
14895 if (scope == error_mark_node)
14896 return error_mark_node;
14898 /* Any name names a type if we're following the `typename' keyword
14899 in a qualified name where the enclosing scope is type-dependent. */
14900 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14901 && dependent_type_p (scope));
14902 /* Handle the common case (an identifier, but not a template-id)
14904 if (token->type == CPP_NAME
14905 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14907 cp_token *identifier_token;
14910 /* Look for the identifier. */
14911 identifier_token = cp_lexer_peek_token (parser->lexer);
14912 ambiguous_p = identifier_token->ambiguous_p;
14913 identifier = cp_parser_identifier (parser);
14914 /* If the next token isn't an identifier, we are certainly not
14915 looking at a class-name. */
14916 if (identifier == error_mark_node)
14917 decl = error_mark_node;
14918 /* If we know this is a type-name, there's no need to look it
14920 else if (typename_p)
14924 tree ambiguous_decls;
14925 /* If we already know that this lookup is ambiguous, then
14926 we've already issued an error message; there's no reason
14930 cp_parser_simulate_error (parser);
14931 return error_mark_node;
14933 /* If the next token is a `::', then the name must be a type
14936 [basic.lookup.qual]
14938 During the lookup for a name preceding the :: scope
14939 resolution operator, object, function, and enumerator
14940 names are ignored. */
14941 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14942 tag_type = typename_type;
14943 /* Look up the name. */
14944 decl = cp_parser_lookup_name (parser, identifier,
14946 /*is_template=*/false,
14947 /*is_namespace=*/false,
14948 check_dependency_p,
14950 identifier_token->location);
14951 if (ambiguous_decls)
14953 error ("%Hreference to %qD is ambiguous",
14954 &identifier_token->location, identifier);
14955 print_candidates (ambiguous_decls);
14956 if (cp_parser_parsing_tentatively (parser))
14958 identifier_token->ambiguous_p = true;
14959 cp_parser_simulate_error (parser);
14961 return error_mark_node;
14967 /* Try a template-id. */
14968 decl = cp_parser_template_id (parser, template_keyword_p,
14969 check_dependency_p,
14971 if (decl == error_mark_node)
14972 return error_mark_node;
14975 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14977 /* If this is a typename, create a TYPENAME_TYPE. */
14978 if (typename_p && decl != error_mark_node)
14980 decl = make_typename_type (scope, decl, typename_type,
14981 /*complain=*/tf_error);
14982 if (decl != error_mark_node)
14983 decl = TYPE_NAME (decl);
14986 /* Check to see that it is really the name of a class. */
14987 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14988 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14989 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14990 /* Situations like this:
14992 template <typename T> struct A {
14993 typename T::template X<int>::I i;
14996 are problematic. Is `T::template X<int>' a class-name? The
14997 standard does not seem to be definitive, but there is no other
14998 valid interpretation of the following `::'. Therefore, those
14999 names are considered class-names. */
15001 decl = make_typename_type (scope, decl, tag_type, tf_error);
15002 if (decl != error_mark_node)
15003 decl = TYPE_NAME (decl);
15005 else if (TREE_CODE (decl) != TYPE_DECL
15006 || TREE_TYPE (decl) == error_mark_node
15007 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15008 decl = error_mark_node;
15010 if (decl == error_mark_node)
15011 cp_parser_error (parser, "expected class-name");
15012 else if (identifier && !parser->scope)
15013 maybe_note_name_used_in_class (identifier, decl);
15018 /* Parse a class-specifier.
15021 class-head { member-specification [opt] }
15023 Returns the TREE_TYPE representing the class. */
15026 cp_parser_class_specifier (cp_parser* parser)
15030 tree attributes = NULL_TREE;
15031 int has_trailing_semicolon;
15032 bool nested_name_specifier_p;
15033 unsigned saved_num_template_parameter_lists;
15034 bool saved_in_function_body;
15035 bool saved_in_unbraced_linkage_specification_p;
15036 tree old_scope = NULL_TREE;
15037 tree scope = NULL_TREE;
15040 push_deferring_access_checks (dk_no_deferred);
15042 /* Parse the class-head. */
15043 type = cp_parser_class_head (parser,
15044 &nested_name_specifier_p,
15047 /* If the class-head was a semantic disaster, skip the entire body
15051 cp_parser_skip_to_end_of_block_or_statement (parser);
15052 pop_deferring_access_checks ();
15053 return error_mark_node;
15056 /* Look for the `{'. */
15057 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15059 pop_deferring_access_checks ();
15060 return error_mark_node;
15063 /* Process the base classes. If they're invalid, skip the
15064 entire class body. */
15065 if (!xref_basetypes (type, bases))
15067 /* Consuming the closing brace yields better error messages
15069 if (cp_parser_skip_to_closing_brace (parser))
15070 cp_lexer_consume_token (parser->lexer);
15071 pop_deferring_access_checks ();
15072 return error_mark_node;
15075 /* Issue an error message if type-definitions are forbidden here. */
15076 cp_parser_check_type_definition (parser);
15077 /* Remember that we are defining one more class. */
15078 ++parser->num_classes_being_defined;
15079 /* Inside the class, surrounding template-parameter-lists do not
15081 saved_num_template_parameter_lists
15082 = parser->num_template_parameter_lists;
15083 parser->num_template_parameter_lists = 0;
15084 /* We are not in a function body. */
15085 saved_in_function_body = parser->in_function_body;
15086 parser->in_function_body = false;
15087 /* We are not immediately inside an extern "lang" block. */
15088 saved_in_unbraced_linkage_specification_p
15089 = parser->in_unbraced_linkage_specification_p;
15090 parser->in_unbraced_linkage_specification_p = false;
15092 /* Start the class. */
15093 if (nested_name_specifier_p)
15095 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15096 old_scope = push_inner_scope (scope);
15098 type = begin_class_definition (type, attributes);
15100 if (type == error_mark_node)
15101 /* If the type is erroneous, skip the entire body of the class. */
15102 cp_parser_skip_to_closing_brace (parser);
15104 /* Parse the member-specification. */
15105 cp_parser_member_specification_opt (parser);
15107 /* Look for the trailing `}'. */
15108 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15109 /* We get better error messages by noticing a common problem: a
15110 missing trailing `;'. */
15111 token = cp_lexer_peek_token (parser->lexer);
15112 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
15113 /* Look for trailing attributes to apply to this class. */
15114 if (cp_parser_allow_gnu_extensions_p (parser))
15115 attributes = cp_parser_attributes_opt (parser);
15116 if (type != error_mark_node)
15117 type = finish_struct (type, attributes);
15118 if (nested_name_specifier_p)
15119 pop_inner_scope (old_scope, scope);
15120 /* If this class is not itself within the scope of another class,
15121 then we need to parse the bodies of all of the queued function
15122 definitions. Note that the queued functions defined in a class
15123 are not always processed immediately following the
15124 class-specifier for that class. Consider:
15127 struct B { void f() { sizeof (A); } };
15130 If `f' were processed before the processing of `A' were
15131 completed, there would be no way to compute the size of `A'.
15132 Note that the nesting we are interested in here is lexical --
15133 not the semantic nesting given by TYPE_CONTEXT. In particular,
15136 struct A { struct B; };
15137 struct A::B { void f() { } };
15139 there is no need to delay the parsing of `A::B::f'. */
15140 if (--parser->num_classes_being_defined == 0)
15144 tree class_type = NULL_TREE;
15145 tree pushed_scope = NULL_TREE;
15147 /* In a first pass, parse default arguments to the functions.
15148 Then, in a second pass, parse the bodies of the functions.
15149 This two-phased approach handles cases like:
15157 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15158 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15159 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15160 TREE_PURPOSE (parser->unparsed_functions_queues)
15161 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15163 fn = TREE_VALUE (queue_entry);
15164 /* If there are default arguments that have not yet been processed,
15165 take care of them now. */
15166 if (class_type != TREE_PURPOSE (queue_entry))
15169 pop_scope (pushed_scope);
15170 class_type = TREE_PURPOSE (queue_entry);
15171 pushed_scope = push_scope (class_type);
15173 /* Make sure that any template parameters are in scope. */
15174 maybe_begin_member_template_processing (fn);
15175 /* Parse the default argument expressions. */
15176 cp_parser_late_parsing_default_args (parser, fn);
15177 /* Remove any template parameters from the symbol table. */
15178 maybe_end_member_template_processing ();
15181 pop_scope (pushed_scope);
15182 /* Now parse the body of the functions. */
15183 for (TREE_VALUE (parser->unparsed_functions_queues)
15184 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15185 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15186 TREE_VALUE (parser->unparsed_functions_queues)
15187 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15189 /* Figure out which function we need to process. */
15190 fn = TREE_VALUE (queue_entry);
15191 /* Parse the function. */
15192 cp_parser_late_parsing_for_member (parser, fn);
15196 /* Put back any saved access checks. */
15197 pop_deferring_access_checks ();
15199 /* Restore saved state. */
15200 parser->in_function_body = saved_in_function_body;
15201 parser->num_template_parameter_lists
15202 = saved_num_template_parameter_lists;
15203 parser->in_unbraced_linkage_specification_p
15204 = saved_in_unbraced_linkage_specification_p;
15209 /* Parse a class-head.
15212 class-key identifier [opt] base-clause [opt]
15213 class-key nested-name-specifier identifier base-clause [opt]
15214 class-key nested-name-specifier [opt] template-id
15218 class-key attributes identifier [opt] base-clause [opt]
15219 class-key attributes nested-name-specifier identifier base-clause [opt]
15220 class-key attributes nested-name-specifier [opt] template-id
15223 Upon return BASES is initialized to the list of base classes (or
15224 NULL, if there are none) in the same form returned by
15225 cp_parser_base_clause.
15227 Returns the TYPE of the indicated class. Sets
15228 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15229 involving a nested-name-specifier was used, and FALSE otherwise.
15231 Returns error_mark_node if this is not a class-head.
15233 Returns NULL_TREE if the class-head is syntactically valid, but
15234 semantically invalid in a way that means we should skip the entire
15235 body of the class. */
15238 cp_parser_class_head (cp_parser* parser,
15239 bool* nested_name_specifier_p,
15240 tree *attributes_p,
15243 tree nested_name_specifier;
15244 enum tag_types class_key;
15245 tree id = NULL_TREE;
15246 tree type = NULL_TREE;
15248 bool template_id_p = false;
15249 bool qualified_p = false;
15250 bool invalid_nested_name_p = false;
15251 bool invalid_explicit_specialization_p = false;
15252 tree pushed_scope = NULL_TREE;
15253 unsigned num_templates;
15254 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15255 /* Assume no nested-name-specifier will be present. */
15256 *nested_name_specifier_p = false;
15257 /* Assume no template parameter lists will be used in defining the
15261 *bases = NULL_TREE;
15263 /* Look for the class-key. */
15264 class_key = cp_parser_class_key (parser);
15265 if (class_key == none_type)
15266 return error_mark_node;
15268 /* Parse the attributes. */
15269 attributes = cp_parser_attributes_opt (parser);
15271 /* If the next token is `::', that is invalid -- but sometimes
15272 people do try to write:
15276 Handle this gracefully by accepting the extra qualifier, and then
15277 issuing an error about it later if this really is a
15278 class-head. If it turns out just to be an elaborated type
15279 specifier, remain silent. */
15280 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15281 qualified_p = true;
15283 push_deferring_access_checks (dk_no_check);
15285 /* Determine the name of the class. Begin by looking for an
15286 optional nested-name-specifier. */
15287 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15288 nested_name_specifier
15289 = cp_parser_nested_name_specifier_opt (parser,
15290 /*typename_keyword_p=*/false,
15291 /*check_dependency_p=*/false,
15293 /*is_declaration=*/false);
15294 /* If there was a nested-name-specifier, then there *must* be an
15296 if (nested_name_specifier)
15298 type_start_token = cp_lexer_peek_token (parser->lexer);
15299 /* Although the grammar says `identifier', it really means
15300 `class-name' or `template-name'. You are only allowed to
15301 define a class that has already been declared with this
15304 The proposed resolution for Core Issue 180 says that wherever
15305 you see `class T::X' you should treat `X' as a type-name.
15307 It is OK to define an inaccessible class; for example:
15309 class A { class B; };
15312 We do not know if we will see a class-name, or a
15313 template-name. We look for a class-name first, in case the
15314 class-name is a template-id; if we looked for the
15315 template-name first we would stop after the template-name. */
15316 cp_parser_parse_tentatively (parser);
15317 type = cp_parser_class_name (parser,
15318 /*typename_keyword_p=*/false,
15319 /*template_keyword_p=*/false,
15321 /*check_dependency_p=*/false,
15322 /*class_head_p=*/true,
15323 /*is_declaration=*/false);
15324 /* If that didn't work, ignore the nested-name-specifier. */
15325 if (!cp_parser_parse_definitely (parser))
15327 invalid_nested_name_p = true;
15328 type_start_token = cp_lexer_peek_token (parser->lexer);
15329 id = cp_parser_identifier (parser);
15330 if (id == error_mark_node)
15333 /* If we could not find a corresponding TYPE, treat this
15334 declaration like an unqualified declaration. */
15335 if (type == error_mark_node)
15336 nested_name_specifier = NULL_TREE;
15337 /* Otherwise, count the number of templates used in TYPE and its
15338 containing scopes. */
15343 for (scope = TREE_TYPE (type);
15344 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15345 scope = (TYPE_P (scope)
15346 ? TYPE_CONTEXT (scope)
15347 : DECL_CONTEXT (scope)))
15349 && CLASS_TYPE_P (scope)
15350 && CLASSTYPE_TEMPLATE_INFO (scope)
15351 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15352 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15356 /* Otherwise, the identifier is optional. */
15359 /* We don't know whether what comes next is a template-id,
15360 an identifier, or nothing at all. */
15361 cp_parser_parse_tentatively (parser);
15362 /* Check for a template-id. */
15363 type_start_token = cp_lexer_peek_token (parser->lexer);
15364 id = cp_parser_template_id (parser,
15365 /*template_keyword_p=*/false,
15366 /*check_dependency_p=*/true,
15367 /*is_declaration=*/true);
15368 /* If that didn't work, it could still be an identifier. */
15369 if (!cp_parser_parse_definitely (parser))
15371 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15373 type_start_token = cp_lexer_peek_token (parser->lexer);
15374 id = cp_parser_identifier (parser);
15381 template_id_p = true;
15386 pop_deferring_access_checks ();
15389 cp_parser_check_for_invalid_template_id (parser, id,
15390 type_start_token->location);
15392 /* If it's not a `:' or a `{' then we can't really be looking at a
15393 class-head, since a class-head only appears as part of a
15394 class-specifier. We have to detect this situation before calling
15395 xref_tag, since that has irreversible side-effects. */
15396 if (!cp_parser_next_token_starts_class_definition_p (parser))
15398 cp_parser_error (parser, "expected %<{%> or %<:%>");
15399 return error_mark_node;
15402 /* At this point, we're going ahead with the class-specifier, even
15403 if some other problem occurs. */
15404 cp_parser_commit_to_tentative_parse (parser);
15405 /* Issue the error about the overly-qualified name now. */
15408 cp_parser_error (parser,
15409 "global qualification of class name is invalid");
15410 return error_mark_node;
15412 else if (invalid_nested_name_p)
15414 cp_parser_error (parser,
15415 "qualified name does not name a class");
15416 return error_mark_node;
15418 else if (nested_name_specifier)
15422 /* Reject typedef-names in class heads. */
15423 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15425 error ("%Hinvalid class name in declaration of %qD",
15426 &type_start_token->location, type);
15431 /* Figure out in what scope the declaration is being placed. */
15432 scope = current_scope ();
15433 /* If that scope does not contain the scope in which the
15434 class was originally declared, the program is invalid. */
15435 if (scope && !is_ancestor (scope, nested_name_specifier))
15437 if (at_namespace_scope_p ())
15438 error ("%Hdeclaration of %qD in namespace %qD which does not "
15440 &type_start_token->location,
15441 type, scope, nested_name_specifier);
15443 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15444 &type_start_token->location,
15445 type, scope, nested_name_specifier);
15451 A declarator-id shall not be qualified except for the
15452 definition of a ... nested class outside of its class
15453 ... [or] the definition or explicit instantiation of a
15454 class member of a namespace outside of its namespace. */
15455 if (scope == nested_name_specifier)
15457 permerror (input_location, "%Hextra qualification not allowed",
15458 &nested_name_specifier_token_start->location);
15459 nested_name_specifier = NULL_TREE;
15463 /* An explicit-specialization must be preceded by "template <>". If
15464 it is not, try to recover gracefully. */
15465 if (at_namespace_scope_p ()
15466 && parser->num_template_parameter_lists == 0
15469 error ("%Han explicit specialization must be preceded by %<template <>%>",
15470 &type_start_token->location);
15471 invalid_explicit_specialization_p = true;
15472 /* Take the same action that would have been taken by
15473 cp_parser_explicit_specialization. */
15474 ++parser->num_template_parameter_lists;
15475 begin_specialization ();
15477 /* There must be no "return" statements between this point and the
15478 end of this function; set "type "to the correct return value and
15479 use "goto done;" to return. */
15480 /* Make sure that the right number of template parameters were
15482 if (!cp_parser_check_template_parameters (parser, num_templates,
15483 type_start_token->location))
15485 /* If something went wrong, there is no point in even trying to
15486 process the class-definition. */
15491 /* Look up the type. */
15494 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15495 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15496 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15498 error ("%Hfunction template %qD redeclared as a class template",
15499 &type_start_token->location, id);
15500 type = error_mark_node;
15504 type = TREE_TYPE (id);
15505 type = maybe_process_partial_specialization (type);
15507 if (nested_name_specifier)
15508 pushed_scope = push_scope (nested_name_specifier);
15510 else if (nested_name_specifier)
15516 template <typename T> struct S { struct T };
15517 template <typename T> struct S<T>::T { };
15519 we will get a TYPENAME_TYPE when processing the definition of
15520 `S::T'. We need to resolve it to the actual type before we
15521 try to define it. */
15522 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15524 class_type = resolve_typename_type (TREE_TYPE (type),
15525 /*only_current_p=*/false);
15526 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15527 type = TYPE_NAME (class_type);
15530 cp_parser_error (parser, "could not resolve typename type");
15531 type = error_mark_node;
15535 if (maybe_process_partial_specialization (TREE_TYPE (type))
15536 == error_mark_node)
15542 class_type = current_class_type;
15543 /* Enter the scope indicated by the nested-name-specifier. */
15544 pushed_scope = push_scope (nested_name_specifier);
15545 /* Get the canonical version of this type. */
15546 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15547 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15548 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15550 type = push_template_decl (type);
15551 if (type == error_mark_node)
15558 type = TREE_TYPE (type);
15559 *nested_name_specifier_p = true;
15561 else /* The name is not a nested name. */
15563 /* If the class was unnamed, create a dummy name. */
15565 id = make_anon_name ();
15566 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15567 parser->num_template_parameter_lists);
15570 /* Indicate whether this class was declared as a `class' or as a
15572 if (TREE_CODE (type) == RECORD_TYPE)
15573 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15574 cp_parser_check_class_key (class_key, type);
15576 /* If this type was already complete, and we see another definition,
15577 that's an error. */
15578 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15580 error ("%Hredefinition of %q#T",
15581 &type_start_token->location, type);
15582 error ("%Hprevious definition of %q+#T",
15583 &type_start_token->location, type);
15587 else if (type == error_mark_node)
15590 /* We will have entered the scope containing the class; the names of
15591 base classes should be looked up in that context. For example:
15593 struct A { struct B {}; struct C; };
15594 struct A::C : B {};
15598 /* Get the list of base-classes, if there is one. */
15599 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15600 *bases = cp_parser_base_clause (parser);
15603 /* Leave the scope given by the nested-name-specifier. We will
15604 enter the class scope itself while processing the members. */
15606 pop_scope (pushed_scope);
15608 if (invalid_explicit_specialization_p)
15610 end_specialization ();
15611 --parser->num_template_parameter_lists;
15613 *attributes_p = attributes;
15617 /* Parse a class-key.
15624 Returns the kind of class-key specified, or none_type to indicate
15627 static enum tag_types
15628 cp_parser_class_key (cp_parser* parser)
15631 enum tag_types tag_type;
15633 /* Look for the class-key. */
15634 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15638 /* Check to see if the TOKEN is a class-key. */
15639 tag_type = cp_parser_token_is_class_key (token);
15641 cp_parser_error (parser, "expected class-key");
15645 /* Parse an (optional) member-specification.
15647 member-specification:
15648 member-declaration member-specification [opt]
15649 access-specifier : member-specification [opt] */
15652 cp_parser_member_specification_opt (cp_parser* parser)
15659 /* Peek at the next token. */
15660 token = cp_lexer_peek_token (parser->lexer);
15661 /* If it's a `}', or EOF then we've seen all the members. */
15662 if (token->type == CPP_CLOSE_BRACE
15663 || token->type == CPP_EOF
15664 || token->type == CPP_PRAGMA_EOL)
15667 /* See if this token is a keyword. */
15668 keyword = token->keyword;
15672 case RID_PROTECTED:
15674 /* Consume the access-specifier. */
15675 cp_lexer_consume_token (parser->lexer);
15676 /* Remember which access-specifier is active. */
15677 current_access_specifier = token->u.value;
15678 /* Look for the `:'. */
15679 cp_parser_require (parser, CPP_COLON, "%<:%>");
15683 /* Accept #pragmas at class scope. */
15684 if (token->type == CPP_PRAGMA)
15686 cp_parser_pragma (parser, pragma_external);
15690 /* Otherwise, the next construction must be a
15691 member-declaration. */
15692 cp_parser_member_declaration (parser);
15697 /* Parse a member-declaration.
15699 member-declaration:
15700 decl-specifier-seq [opt] member-declarator-list [opt] ;
15701 function-definition ; [opt]
15702 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15704 template-declaration
15706 member-declarator-list:
15708 member-declarator-list , member-declarator
15711 declarator pure-specifier [opt]
15712 declarator constant-initializer [opt]
15713 identifier [opt] : constant-expression
15717 member-declaration:
15718 __extension__ member-declaration
15721 declarator attributes [opt] pure-specifier [opt]
15722 declarator attributes [opt] constant-initializer [opt]
15723 identifier [opt] attributes [opt] : constant-expression
15727 member-declaration:
15728 static_assert-declaration */
15731 cp_parser_member_declaration (cp_parser* parser)
15733 cp_decl_specifier_seq decl_specifiers;
15734 tree prefix_attributes;
15736 int declares_class_or_enum;
15738 cp_token *token = NULL;
15739 cp_token *decl_spec_token_start = NULL;
15740 cp_token *initializer_token_start = NULL;
15741 int saved_pedantic;
15743 /* Check for the `__extension__' keyword. */
15744 if (cp_parser_extension_opt (parser, &saved_pedantic))
15747 cp_parser_member_declaration (parser);
15748 /* Restore the old value of the PEDANTIC flag. */
15749 pedantic = saved_pedantic;
15754 /* Check for a template-declaration. */
15755 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15757 /* An explicit specialization here is an error condition, and we
15758 expect the specialization handler to detect and report this. */
15759 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15760 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15761 cp_parser_explicit_specialization (parser);
15763 cp_parser_template_declaration (parser, /*member_p=*/true);
15768 /* Check for a using-declaration. */
15769 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15771 /* Parse the using-declaration. */
15772 cp_parser_using_declaration (parser,
15773 /*access_declaration_p=*/false);
15777 /* Check for @defs. */
15778 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15781 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15782 ivar = ivar_chains;
15786 ivar = TREE_CHAIN (member);
15787 TREE_CHAIN (member) = NULL_TREE;
15788 finish_member_declaration (member);
15793 /* If the next token is `static_assert' we have a static assertion. */
15794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15796 cp_parser_static_assert (parser, /*member_p=*/true);
15800 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15803 /* Parse the decl-specifier-seq. */
15804 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15805 cp_parser_decl_specifier_seq (parser,
15806 CP_PARSER_FLAGS_OPTIONAL,
15808 &declares_class_or_enum);
15809 prefix_attributes = decl_specifiers.attributes;
15810 decl_specifiers.attributes = NULL_TREE;
15811 /* Check for an invalid type-name. */
15812 if (!decl_specifiers.type
15813 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15815 /* If there is no declarator, then the decl-specifier-seq should
15817 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15819 /* If there was no decl-specifier-seq, and the next token is a
15820 `;', then we have something like:
15826 Each member-declaration shall declare at least one member
15827 name of the class. */
15828 if (!decl_specifiers.any_specifiers_p)
15830 cp_token *token = cp_lexer_peek_token (parser->lexer);
15831 if (!in_system_header_at (token->location))
15832 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15838 /* See if this declaration is a friend. */
15839 friend_p = cp_parser_friend_p (&decl_specifiers);
15840 /* If there were decl-specifiers, check to see if there was
15841 a class-declaration. */
15842 type = check_tag_decl (&decl_specifiers);
15843 /* Nested classes have already been added to the class, but
15844 a `friend' needs to be explicitly registered. */
15847 /* If the `friend' keyword was present, the friend must
15848 be introduced with a class-key. */
15849 if (!declares_class_or_enum)
15850 error ("%Ha class-key must be used when declaring a friend",
15851 &decl_spec_token_start->location);
15854 template <typename T> struct A {
15855 friend struct A<T>::B;
15858 A<T>::B will be represented by a TYPENAME_TYPE, and
15859 therefore not recognized by check_tag_decl. */
15861 && decl_specifiers.type
15862 && TYPE_P (decl_specifiers.type))
15863 type = decl_specifiers.type;
15864 if (!type || !TYPE_P (type))
15865 error ("%Hfriend declaration does not name a class or "
15866 "function", &decl_spec_token_start->location);
15868 make_friend_class (current_class_type, type,
15869 /*complain=*/true);
15871 /* If there is no TYPE, an error message will already have
15873 else if (!type || type == error_mark_node)
15875 /* An anonymous aggregate has to be handled specially; such
15876 a declaration really declares a data member (with a
15877 particular type), as opposed to a nested class. */
15878 else if (ANON_AGGR_TYPE_P (type))
15880 /* Remove constructors and such from TYPE, now that we
15881 know it is an anonymous aggregate. */
15882 fixup_anonymous_aggr (type);
15883 /* And make the corresponding data member. */
15884 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15885 /* Add it to the class. */
15886 finish_member_declaration (decl);
15889 cp_parser_check_access_in_redeclaration
15891 decl_spec_token_start->location);
15896 /* See if these declarations will be friends. */
15897 friend_p = cp_parser_friend_p (&decl_specifiers);
15899 /* Keep going until we hit the `;' at the end of the
15901 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15903 tree attributes = NULL_TREE;
15904 tree first_attribute;
15906 /* Peek at the next token. */
15907 token = cp_lexer_peek_token (parser->lexer);
15909 /* Check for a bitfield declaration. */
15910 if (token->type == CPP_COLON
15911 || (token->type == CPP_NAME
15912 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15918 /* Get the name of the bitfield. Note that we cannot just
15919 check TOKEN here because it may have been invalidated by
15920 the call to cp_lexer_peek_nth_token above. */
15921 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15922 identifier = cp_parser_identifier (parser);
15924 identifier = NULL_TREE;
15926 /* Consume the `:' token. */
15927 cp_lexer_consume_token (parser->lexer);
15928 /* Get the width of the bitfield. */
15930 = cp_parser_constant_expression (parser,
15931 /*allow_non_constant=*/false,
15934 /* Look for attributes that apply to the bitfield. */
15935 attributes = cp_parser_attributes_opt (parser);
15936 /* Remember which attributes are prefix attributes and
15938 first_attribute = attributes;
15939 /* Combine the attributes. */
15940 attributes = chainon (prefix_attributes, attributes);
15942 /* Create the bitfield declaration. */
15943 decl = grokbitfield (identifier
15944 ? make_id_declarator (NULL_TREE,
15954 cp_declarator *declarator;
15956 tree asm_specification;
15957 int ctor_dtor_or_conv_p;
15959 /* Parse the declarator. */
15961 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15962 &ctor_dtor_or_conv_p,
15963 /*parenthesized_p=*/NULL,
15964 /*member_p=*/true);
15966 /* If something went wrong parsing the declarator, make sure
15967 that we at least consume some tokens. */
15968 if (declarator == cp_error_declarator)
15970 /* Skip to the end of the statement. */
15971 cp_parser_skip_to_end_of_statement (parser);
15972 /* If the next token is not a semicolon, that is
15973 probably because we just skipped over the body of
15974 a function. So, we consume a semicolon if
15975 present, but do not issue an error message if it
15977 if (cp_lexer_next_token_is (parser->lexer,
15979 cp_lexer_consume_token (parser->lexer);
15983 if (declares_class_or_enum & 2)
15984 cp_parser_check_for_definition_in_return_type
15985 (declarator, decl_specifiers.type,
15986 decl_specifiers.type_location);
15988 /* Look for an asm-specification. */
15989 asm_specification = cp_parser_asm_specification_opt (parser);
15990 /* Look for attributes that apply to the declaration. */
15991 attributes = cp_parser_attributes_opt (parser);
15992 /* Remember which attributes are prefix attributes and
15994 first_attribute = attributes;
15995 /* Combine the attributes. */
15996 attributes = chainon (prefix_attributes, attributes);
15998 /* If it's an `=', then we have a constant-initializer or a
15999 pure-specifier. It is not correct to parse the
16000 initializer before registering the member declaration
16001 since the member declaration should be in scope while
16002 its initializer is processed. However, the rest of the
16003 front end does not yet provide an interface that allows
16004 us to handle this correctly. */
16005 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16009 A pure-specifier shall be used only in the declaration of
16010 a virtual function.
16012 A member-declarator can contain a constant-initializer
16013 only if it declares a static member of integral or
16016 Therefore, if the DECLARATOR is for a function, we look
16017 for a pure-specifier; otherwise, we look for a
16018 constant-initializer. When we call `grokfield', it will
16019 perform more stringent semantics checks. */
16020 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16021 if (function_declarator_p (declarator))
16022 initializer = cp_parser_pure_specifier (parser);
16024 /* Parse the initializer. */
16025 initializer = cp_parser_constant_initializer (parser);
16027 /* Otherwise, there is no initializer. */
16029 initializer = NULL_TREE;
16031 /* See if we are probably looking at a function
16032 definition. We are certainly not looking at a
16033 member-declarator. Calling `grokfield' has
16034 side-effects, so we must not do it unless we are sure
16035 that we are looking at a member-declarator. */
16036 if (cp_parser_token_starts_function_definition_p
16037 (cp_lexer_peek_token (parser->lexer)))
16039 /* The grammar does not allow a pure-specifier to be
16040 used when a member function is defined. (It is
16041 possible that this fact is an oversight in the
16042 standard, since a pure function may be defined
16043 outside of the class-specifier. */
16045 error ("%Hpure-specifier on function-definition",
16046 &initializer_token_start->location);
16047 decl = cp_parser_save_member_function_body (parser,
16051 /* If the member was not a friend, declare it here. */
16053 finish_member_declaration (decl);
16054 /* Peek at the next token. */
16055 token = cp_lexer_peek_token (parser->lexer);
16056 /* If the next token is a semicolon, consume it. */
16057 if (token->type == CPP_SEMICOLON)
16058 cp_lexer_consume_token (parser->lexer);
16062 if (declarator->kind == cdk_function)
16063 declarator->id_loc = token->location;
16064 /* Create the declaration. */
16065 decl = grokfield (declarator, &decl_specifiers,
16066 initializer, /*init_const_expr_p=*/true,
16071 /* Reset PREFIX_ATTRIBUTES. */
16072 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16073 attributes = TREE_CHAIN (attributes);
16075 TREE_CHAIN (attributes) = NULL_TREE;
16077 /* If there is any qualification still in effect, clear it
16078 now; we will be starting fresh with the next declarator. */
16079 parser->scope = NULL_TREE;
16080 parser->qualifying_scope = NULL_TREE;
16081 parser->object_scope = NULL_TREE;
16082 /* If it's a `,', then there are more declarators. */
16083 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16084 cp_lexer_consume_token (parser->lexer);
16085 /* If the next token isn't a `;', then we have a parse error. */
16086 else if (cp_lexer_next_token_is_not (parser->lexer,
16089 cp_parser_error (parser, "expected %<;%>");
16090 /* Skip tokens until we find a `;'. */
16091 cp_parser_skip_to_end_of_statement (parser);
16098 /* Add DECL to the list of members. */
16100 finish_member_declaration (decl);
16102 if (TREE_CODE (decl) == FUNCTION_DECL)
16103 cp_parser_save_default_args (parser, decl);
16108 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16111 /* Parse a pure-specifier.
16116 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16117 Otherwise, ERROR_MARK_NODE is returned. */
16120 cp_parser_pure_specifier (cp_parser* parser)
16124 /* Look for the `=' token. */
16125 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16126 return error_mark_node;
16127 /* Look for the `0' token. */
16128 token = cp_lexer_peek_token (parser->lexer);
16130 if (token->type == CPP_EOF
16131 || token->type == CPP_PRAGMA_EOL)
16132 return error_mark_node;
16134 cp_lexer_consume_token (parser->lexer);
16136 /* Accept = default or = delete in c++0x mode. */
16137 if (token->keyword == RID_DEFAULT
16138 || token->keyword == RID_DELETE)
16140 maybe_warn_cpp0x ("defaulted and deleted functions");
16141 return token->u.value;
16144 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16145 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16147 cp_parser_error (parser,
16148 "invalid pure specifier (only %<= 0%> is allowed)");
16149 cp_parser_skip_to_end_of_statement (parser);
16150 return error_mark_node;
16152 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16154 error ("%Htemplates may not be %<virtual%>", &token->location);
16155 return error_mark_node;
16158 return integer_zero_node;
16161 /* Parse a constant-initializer.
16163 constant-initializer:
16164 = constant-expression
16166 Returns a representation of the constant-expression. */
16169 cp_parser_constant_initializer (cp_parser* parser)
16171 /* Look for the `=' token. */
16172 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16173 return error_mark_node;
16175 /* It is invalid to write:
16177 struct S { static const int i = { 7 }; };
16180 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16182 cp_parser_error (parser,
16183 "a brace-enclosed initializer is not allowed here");
16184 /* Consume the opening brace. */
16185 cp_lexer_consume_token (parser->lexer);
16186 /* Skip the initializer. */
16187 cp_parser_skip_to_closing_brace (parser);
16188 /* Look for the trailing `}'. */
16189 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16191 return error_mark_node;
16194 return cp_parser_constant_expression (parser,
16195 /*allow_non_constant=*/false,
16199 /* Derived classes [gram.class.derived] */
16201 /* Parse a base-clause.
16204 : base-specifier-list
16206 base-specifier-list:
16207 base-specifier ... [opt]
16208 base-specifier-list , base-specifier ... [opt]
16210 Returns a TREE_LIST representing the base-classes, in the order in
16211 which they were declared. The representation of each node is as
16212 described by cp_parser_base_specifier.
16214 In the case that no bases are specified, this function will return
16215 NULL_TREE, not ERROR_MARK_NODE. */
16218 cp_parser_base_clause (cp_parser* parser)
16220 tree bases = NULL_TREE;
16222 /* Look for the `:' that begins the list. */
16223 cp_parser_require (parser, CPP_COLON, "%<:%>");
16225 /* Scan the base-specifier-list. */
16230 bool pack_expansion_p = false;
16232 /* Look for the base-specifier. */
16233 base = cp_parser_base_specifier (parser);
16234 /* Look for the (optional) ellipsis. */
16235 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16237 /* Consume the `...'. */
16238 cp_lexer_consume_token (parser->lexer);
16240 pack_expansion_p = true;
16243 /* Add BASE to the front of the list. */
16244 if (base != error_mark_node)
16246 if (pack_expansion_p)
16247 /* Make this a pack expansion type. */
16248 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16251 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16253 TREE_CHAIN (base) = bases;
16257 /* Peek at the next token. */
16258 token = cp_lexer_peek_token (parser->lexer);
16259 /* If it's not a comma, then the list is complete. */
16260 if (token->type != CPP_COMMA)
16262 /* Consume the `,'. */
16263 cp_lexer_consume_token (parser->lexer);
16266 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16267 base class had a qualified name. However, the next name that
16268 appears is certainly not qualified. */
16269 parser->scope = NULL_TREE;
16270 parser->qualifying_scope = NULL_TREE;
16271 parser->object_scope = NULL_TREE;
16273 return nreverse (bases);
16276 /* Parse a base-specifier.
16279 :: [opt] nested-name-specifier [opt] class-name
16280 virtual access-specifier [opt] :: [opt] nested-name-specifier
16282 access-specifier virtual [opt] :: [opt] nested-name-specifier
16285 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16286 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16287 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16288 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16291 cp_parser_base_specifier (cp_parser* parser)
16295 bool virtual_p = false;
16296 bool duplicate_virtual_error_issued_p = false;
16297 bool duplicate_access_error_issued_p = false;
16298 bool class_scope_p, template_p;
16299 tree access = access_default_node;
16302 /* Process the optional `virtual' and `access-specifier'. */
16305 /* Peek at the next token. */
16306 token = cp_lexer_peek_token (parser->lexer);
16307 /* Process `virtual'. */
16308 switch (token->keyword)
16311 /* If `virtual' appears more than once, issue an error. */
16312 if (virtual_p && !duplicate_virtual_error_issued_p)
16314 cp_parser_error (parser,
16315 "%<virtual%> specified more than once in base-specified");
16316 duplicate_virtual_error_issued_p = true;
16321 /* Consume the `virtual' token. */
16322 cp_lexer_consume_token (parser->lexer);
16327 case RID_PROTECTED:
16329 /* If more than one access specifier appears, issue an
16331 if (access != access_default_node
16332 && !duplicate_access_error_issued_p)
16334 cp_parser_error (parser,
16335 "more than one access specifier in base-specified");
16336 duplicate_access_error_issued_p = true;
16339 access = ridpointers[(int) token->keyword];
16341 /* Consume the access-specifier. */
16342 cp_lexer_consume_token (parser->lexer);
16351 /* It is not uncommon to see programs mechanically, erroneously, use
16352 the 'typename' keyword to denote (dependent) qualified types
16353 as base classes. */
16354 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16356 token = cp_lexer_peek_token (parser->lexer);
16357 if (!processing_template_decl)
16358 error ("%Hkeyword %<typename%> not allowed outside of templates",
16361 error ("%Hkeyword %<typename%> not allowed in this context "
16362 "(the base class is implicitly a type)",
16364 cp_lexer_consume_token (parser->lexer);
16367 /* Look for the optional `::' operator. */
16368 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16369 /* Look for the nested-name-specifier. The simplest way to
16374 The keyword `typename' is not permitted in a base-specifier or
16375 mem-initializer; in these contexts a qualified name that
16376 depends on a template-parameter is implicitly assumed to be a
16379 is to pretend that we have seen the `typename' keyword at this
16381 cp_parser_nested_name_specifier_opt (parser,
16382 /*typename_keyword_p=*/true,
16383 /*check_dependency_p=*/true,
16385 /*is_declaration=*/true);
16386 /* If the base class is given by a qualified name, assume that names
16387 we see are type names or templates, as appropriate. */
16388 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16389 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16391 /* Finally, look for the class-name. */
16392 type = cp_parser_class_name (parser,
16396 /*check_dependency_p=*/true,
16397 /*class_head_p=*/false,
16398 /*is_declaration=*/true);
16400 if (type == error_mark_node)
16401 return error_mark_node;
16403 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16406 /* Exception handling [gram.exception] */
16408 /* Parse an (optional) exception-specification.
16410 exception-specification:
16411 throw ( type-id-list [opt] )
16413 Returns a TREE_LIST representing the exception-specification. The
16414 TREE_VALUE of each node is a type. */
16417 cp_parser_exception_specification_opt (cp_parser* parser)
16422 /* Peek at the next token. */
16423 token = cp_lexer_peek_token (parser->lexer);
16424 /* If it's not `throw', then there's no exception-specification. */
16425 if (!cp_parser_is_keyword (token, RID_THROW))
16428 /* Consume the `throw'. */
16429 cp_lexer_consume_token (parser->lexer);
16431 /* Look for the `('. */
16432 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16434 /* Peek at the next token. */
16435 token = cp_lexer_peek_token (parser->lexer);
16436 /* If it's not a `)', then there is a type-id-list. */
16437 if (token->type != CPP_CLOSE_PAREN)
16439 const char *saved_message;
16441 /* Types may not be defined in an exception-specification. */
16442 saved_message = parser->type_definition_forbidden_message;
16443 parser->type_definition_forbidden_message
16444 = "types may not be defined in an exception-specification";
16445 /* Parse the type-id-list. */
16446 type_id_list = cp_parser_type_id_list (parser);
16447 /* Restore the saved message. */
16448 parser->type_definition_forbidden_message = saved_message;
16451 type_id_list = empty_except_spec;
16453 /* Look for the `)'. */
16454 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16456 return type_id_list;
16459 /* Parse an (optional) type-id-list.
16463 type-id-list , type-id ... [opt]
16465 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16466 in the order that the types were presented. */
16469 cp_parser_type_id_list (cp_parser* parser)
16471 tree types = NULL_TREE;
16478 /* Get the next type-id. */
16479 type = cp_parser_type_id (parser);
16480 /* Parse the optional ellipsis. */
16481 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16483 /* Consume the `...'. */
16484 cp_lexer_consume_token (parser->lexer);
16486 /* Turn the type into a pack expansion expression. */
16487 type = make_pack_expansion (type);
16489 /* Add it to the list. */
16490 types = add_exception_specifier (types, type, /*complain=*/1);
16491 /* Peek at the next token. */
16492 token = cp_lexer_peek_token (parser->lexer);
16493 /* If it is not a `,', we are done. */
16494 if (token->type != CPP_COMMA)
16496 /* Consume the `,'. */
16497 cp_lexer_consume_token (parser->lexer);
16500 return nreverse (types);
16503 /* Parse a try-block.
16506 try compound-statement handler-seq */
16509 cp_parser_try_block (cp_parser* parser)
16513 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16514 try_block = begin_try_block ();
16515 cp_parser_compound_statement (parser, NULL, true);
16516 finish_try_block (try_block);
16517 cp_parser_handler_seq (parser);
16518 finish_handler_sequence (try_block);
16523 /* Parse a function-try-block.
16525 function-try-block:
16526 try ctor-initializer [opt] function-body handler-seq */
16529 cp_parser_function_try_block (cp_parser* parser)
16531 tree compound_stmt;
16533 bool ctor_initializer_p;
16535 /* Look for the `try' keyword. */
16536 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16538 /* Let the rest of the front end know where we are. */
16539 try_block = begin_function_try_block (&compound_stmt);
16540 /* Parse the function-body. */
16542 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16543 /* We're done with the `try' part. */
16544 finish_function_try_block (try_block);
16545 /* Parse the handlers. */
16546 cp_parser_handler_seq (parser);
16547 /* We're done with the handlers. */
16548 finish_function_handler_sequence (try_block, compound_stmt);
16550 return ctor_initializer_p;
16553 /* Parse a handler-seq.
16556 handler handler-seq [opt] */
16559 cp_parser_handler_seq (cp_parser* parser)
16565 /* Parse the handler. */
16566 cp_parser_handler (parser);
16567 /* Peek at the next token. */
16568 token = cp_lexer_peek_token (parser->lexer);
16569 /* If it's not `catch' then there are no more handlers. */
16570 if (!cp_parser_is_keyword (token, RID_CATCH))
16575 /* Parse a handler.
16578 catch ( exception-declaration ) compound-statement */
16581 cp_parser_handler (cp_parser* parser)
16586 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16587 handler = begin_handler ();
16588 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16589 declaration = cp_parser_exception_declaration (parser);
16590 finish_handler_parms (declaration, handler);
16591 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16592 cp_parser_compound_statement (parser, NULL, false);
16593 finish_handler (handler);
16596 /* Parse an exception-declaration.
16598 exception-declaration:
16599 type-specifier-seq declarator
16600 type-specifier-seq abstract-declarator
16604 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16605 ellipsis variant is used. */
16608 cp_parser_exception_declaration (cp_parser* parser)
16610 cp_decl_specifier_seq type_specifiers;
16611 cp_declarator *declarator;
16612 const char *saved_message;
16614 /* If it's an ellipsis, it's easy to handle. */
16615 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16617 /* Consume the `...' token. */
16618 cp_lexer_consume_token (parser->lexer);
16622 /* Types may not be defined in exception-declarations. */
16623 saved_message = parser->type_definition_forbidden_message;
16624 parser->type_definition_forbidden_message
16625 = "types may not be defined in exception-declarations";
16627 /* Parse the type-specifier-seq. */
16628 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
16629 /*is_trailing_return=*/false,
16631 /* If it's a `)', then there is no declarator. */
16632 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16635 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16636 /*ctor_dtor_or_conv_p=*/NULL,
16637 /*parenthesized_p=*/NULL,
16638 /*member_p=*/false);
16640 /* Restore the saved message. */
16641 parser->type_definition_forbidden_message = saved_message;
16643 if (!type_specifiers.any_specifiers_p)
16644 return error_mark_node;
16646 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16649 /* Parse a throw-expression.
16652 throw assignment-expression [opt]
16654 Returns a THROW_EXPR representing the throw-expression. */
16657 cp_parser_throw_expression (cp_parser* parser)
16662 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16663 token = cp_lexer_peek_token (parser->lexer);
16664 /* Figure out whether or not there is an assignment-expression
16665 following the "throw" keyword. */
16666 if (token->type == CPP_COMMA
16667 || token->type == CPP_SEMICOLON
16668 || token->type == CPP_CLOSE_PAREN
16669 || token->type == CPP_CLOSE_SQUARE
16670 || token->type == CPP_CLOSE_BRACE
16671 || token->type == CPP_COLON)
16672 expression = NULL_TREE;
16674 expression = cp_parser_assignment_expression (parser,
16675 /*cast_p=*/false, NULL);
16677 return build_throw (expression);
16680 /* GNU Extensions */
16682 /* Parse an (optional) asm-specification.
16685 asm ( string-literal )
16687 If the asm-specification is present, returns a STRING_CST
16688 corresponding to the string-literal. Otherwise, returns
16692 cp_parser_asm_specification_opt (cp_parser* parser)
16695 tree asm_specification;
16697 /* Peek at the next token. */
16698 token = cp_lexer_peek_token (parser->lexer);
16699 /* If the next token isn't the `asm' keyword, then there's no
16700 asm-specification. */
16701 if (!cp_parser_is_keyword (token, RID_ASM))
16704 /* Consume the `asm' token. */
16705 cp_lexer_consume_token (parser->lexer);
16706 /* Look for the `('. */
16707 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16709 /* Look for the string-literal. */
16710 asm_specification = cp_parser_string_literal (parser, false, false);
16712 /* Look for the `)'. */
16713 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16715 return asm_specification;
16718 /* Parse an asm-operand-list.
16722 asm-operand-list , asm-operand
16725 string-literal ( expression )
16726 [ string-literal ] string-literal ( expression )
16728 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16729 each node is the expression. The TREE_PURPOSE is itself a
16730 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16731 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16732 is a STRING_CST for the string literal before the parenthesis. Returns
16733 ERROR_MARK_NODE if any of the operands are invalid. */
16736 cp_parser_asm_operand_list (cp_parser* parser)
16738 tree asm_operands = NULL_TREE;
16739 bool invalid_operands = false;
16743 tree string_literal;
16747 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16749 /* Consume the `[' token. */
16750 cp_lexer_consume_token (parser->lexer);
16751 /* Read the operand name. */
16752 name = cp_parser_identifier (parser);
16753 if (name != error_mark_node)
16754 name = build_string (IDENTIFIER_LENGTH (name),
16755 IDENTIFIER_POINTER (name));
16756 /* Look for the closing `]'. */
16757 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16761 /* Look for the string-literal. */
16762 string_literal = cp_parser_string_literal (parser, false, false);
16764 /* Look for the `('. */
16765 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16766 /* Parse the expression. */
16767 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16768 /* Look for the `)'. */
16769 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16771 if (name == error_mark_node
16772 || string_literal == error_mark_node
16773 || expression == error_mark_node)
16774 invalid_operands = true;
16776 /* Add this operand to the list. */
16777 asm_operands = tree_cons (build_tree_list (name, string_literal),
16780 /* If the next token is not a `,', there are no more
16782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16784 /* Consume the `,'. */
16785 cp_lexer_consume_token (parser->lexer);
16788 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16791 /* Parse an asm-clobber-list.
16795 asm-clobber-list , string-literal
16797 Returns a TREE_LIST, indicating the clobbers in the order that they
16798 appeared. The TREE_VALUE of each node is a STRING_CST. */
16801 cp_parser_asm_clobber_list (cp_parser* parser)
16803 tree clobbers = NULL_TREE;
16807 tree string_literal;
16809 /* Look for the string literal. */
16810 string_literal = cp_parser_string_literal (parser, false, false);
16811 /* Add it to the list. */
16812 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16813 /* If the next token is not a `,', then the list is
16815 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16817 /* Consume the `,' token. */
16818 cp_lexer_consume_token (parser->lexer);
16824 /* Parse an (optional) series of attributes.
16827 attributes attribute
16830 __attribute__ (( attribute-list [opt] ))
16832 The return value is as for cp_parser_attribute_list. */
16835 cp_parser_attributes_opt (cp_parser* parser)
16837 tree attributes = NULL_TREE;
16842 tree attribute_list;
16844 /* Peek at the next token. */
16845 token = cp_lexer_peek_token (parser->lexer);
16846 /* If it's not `__attribute__', then we're done. */
16847 if (token->keyword != RID_ATTRIBUTE)
16850 /* Consume the `__attribute__' keyword. */
16851 cp_lexer_consume_token (parser->lexer);
16852 /* Look for the two `(' tokens. */
16853 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16854 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16856 /* Peek at the next token. */
16857 token = cp_lexer_peek_token (parser->lexer);
16858 if (token->type != CPP_CLOSE_PAREN)
16859 /* Parse the attribute-list. */
16860 attribute_list = cp_parser_attribute_list (parser);
16862 /* If the next token is a `)', then there is no attribute
16864 attribute_list = NULL;
16866 /* Look for the two `)' tokens. */
16867 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16868 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16870 /* Add these new attributes to the list. */
16871 attributes = chainon (attributes, attribute_list);
16877 /* Parse an attribute-list.
16881 attribute-list , attribute
16885 identifier ( identifier )
16886 identifier ( identifier , expression-list )
16887 identifier ( expression-list )
16889 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16890 to an attribute. The TREE_PURPOSE of each node is the identifier
16891 indicating which attribute is in use. The TREE_VALUE represents
16892 the arguments, if any. */
16895 cp_parser_attribute_list (cp_parser* parser)
16897 tree attribute_list = NULL_TREE;
16898 bool save_translate_strings_p = parser->translate_strings_p;
16900 parser->translate_strings_p = false;
16907 /* Look for the identifier. We also allow keywords here; for
16908 example `__attribute__ ((const))' is legal. */
16909 token = cp_lexer_peek_token (parser->lexer);
16910 if (token->type == CPP_NAME
16911 || token->type == CPP_KEYWORD)
16913 tree arguments = NULL_TREE;
16915 /* Consume the token. */
16916 token = cp_lexer_consume_token (parser->lexer);
16918 /* Save away the identifier that indicates which attribute
16920 identifier = token->u.value;
16921 attribute = build_tree_list (identifier, NULL_TREE);
16923 /* Peek at the next token. */
16924 token = cp_lexer_peek_token (parser->lexer);
16925 /* If it's an `(', then parse the attribute arguments. */
16926 if (token->type == CPP_OPEN_PAREN)
16928 arguments = cp_parser_parenthesized_expression_list
16929 (parser, true, /*cast_p=*/false,
16930 /*allow_expansion_p=*/false,
16931 /*non_constant_p=*/NULL);
16932 /* Save the arguments away. */
16933 TREE_VALUE (attribute) = arguments;
16936 if (arguments != error_mark_node)
16938 /* Add this attribute to the list. */
16939 TREE_CHAIN (attribute) = attribute_list;
16940 attribute_list = attribute;
16943 token = cp_lexer_peek_token (parser->lexer);
16945 /* Now, look for more attributes. If the next token isn't a
16946 `,', we're done. */
16947 if (token->type != CPP_COMMA)
16950 /* Consume the comma and keep going. */
16951 cp_lexer_consume_token (parser->lexer);
16953 parser->translate_strings_p = save_translate_strings_p;
16955 /* We built up the list in reverse order. */
16956 return nreverse (attribute_list);
16959 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16960 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16961 current value of the PEDANTIC flag, regardless of whether or not
16962 the `__extension__' keyword is present. The caller is responsible
16963 for restoring the value of the PEDANTIC flag. */
16966 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16968 /* Save the old value of the PEDANTIC flag. */
16969 *saved_pedantic = pedantic;
16971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16973 /* Consume the `__extension__' token. */
16974 cp_lexer_consume_token (parser->lexer);
16975 /* We're not being pedantic while the `__extension__' keyword is
16985 /* Parse a label declaration.
16988 __label__ label-declarator-seq ;
16990 label-declarator-seq:
16991 identifier , label-declarator-seq
16995 cp_parser_label_declaration (cp_parser* parser)
16997 /* Look for the `__label__' keyword. */
16998 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17004 /* Look for an identifier. */
17005 identifier = cp_parser_identifier (parser);
17006 /* If we failed, stop. */
17007 if (identifier == error_mark_node)
17009 /* Declare it as a label. */
17010 finish_label_decl (identifier);
17011 /* If the next token is a `;', stop. */
17012 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17014 /* Look for the `,' separating the label declarations. */
17015 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17018 /* Look for the final `;'. */
17019 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17022 /* Support Functions */
17024 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17025 NAME should have one of the representations used for an
17026 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17027 is returned. If PARSER->SCOPE is a dependent type, then a
17028 SCOPE_REF is returned.
17030 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17031 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17032 was formed. Abstractly, such entities should not be passed to this
17033 function, because they do not need to be looked up, but it is
17034 simpler to check for this special case here, rather than at the
17037 In cases not explicitly covered above, this function returns a
17038 DECL, OVERLOAD, or baselink representing the result of the lookup.
17039 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17042 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17043 (e.g., "struct") that was used. In that case bindings that do not
17044 refer to types are ignored.
17046 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17049 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17052 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17055 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17056 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17057 NULL_TREE otherwise. */
17060 cp_parser_lookup_name (cp_parser *parser, tree name,
17061 enum tag_types tag_type,
17064 bool check_dependency,
17065 tree *ambiguous_decls,
17066 location_t name_location)
17070 tree object_type = parser->context->object_type;
17072 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17073 flags |= LOOKUP_COMPLAIN;
17075 /* Assume that the lookup will be unambiguous. */
17076 if (ambiguous_decls)
17077 *ambiguous_decls = NULL_TREE;
17079 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17080 no longer valid. Note that if we are parsing tentatively, and
17081 the parse fails, OBJECT_TYPE will be automatically restored. */
17082 parser->context->object_type = NULL_TREE;
17084 if (name == error_mark_node)
17085 return error_mark_node;
17087 /* A template-id has already been resolved; there is no lookup to
17089 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17091 if (BASELINK_P (name))
17093 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17094 == TEMPLATE_ID_EXPR);
17098 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17099 it should already have been checked to make sure that the name
17100 used matches the type being destroyed. */
17101 if (TREE_CODE (name) == BIT_NOT_EXPR)
17105 /* Figure out to which type this destructor applies. */
17107 type = parser->scope;
17108 else if (object_type)
17109 type = object_type;
17111 type = current_class_type;
17112 /* If that's not a class type, there is no destructor. */
17113 if (!type || !CLASS_TYPE_P (type))
17114 return error_mark_node;
17115 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17116 lazily_declare_fn (sfk_destructor, type);
17117 if (!CLASSTYPE_DESTRUCTORS (type))
17118 return error_mark_node;
17119 /* If it was a class type, return the destructor. */
17120 return CLASSTYPE_DESTRUCTORS (type);
17123 /* By this point, the NAME should be an ordinary identifier. If
17124 the id-expression was a qualified name, the qualifying scope is
17125 stored in PARSER->SCOPE at this point. */
17126 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17128 /* Perform the lookup. */
17133 if (parser->scope == error_mark_node)
17134 return error_mark_node;
17136 /* If the SCOPE is dependent, the lookup must be deferred until
17137 the template is instantiated -- unless we are explicitly
17138 looking up names in uninstantiated templates. Even then, we
17139 cannot look up the name if the scope is not a class type; it
17140 might, for example, be a template type parameter. */
17141 dependent_p = (TYPE_P (parser->scope)
17142 && dependent_scope_p (parser->scope));
17143 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17145 /* Defer lookup. */
17146 decl = error_mark_node;
17149 tree pushed_scope = NULL_TREE;
17151 /* If PARSER->SCOPE is a dependent type, then it must be a
17152 class type, and we must not be checking dependencies;
17153 otherwise, we would have processed this lookup above. So
17154 that PARSER->SCOPE is not considered a dependent base by
17155 lookup_member, we must enter the scope here. */
17157 pushed_scope = push_scope (parser->scope);
17158 /* If the PARSER->SCOPE is a template specialization, it
17159 may be instantiated during name lookup. In that case,
17160 errors may be issued. Even if we rollback the current
17161 tentative parse, those errors are valid. */
17162 decl = lookup_qualified_name (parser->scope, name,
17163 tag_type != none_type,
17164 /*complain=*/true);
17166 /* If we have a single function from a using decl, pull it out. */
17167 if (TREE_CODE (decl) == OVERLOAD
17168 && !really_overloaded_fn (decl))
17169 decl = OVL_FUNCTION (decl);
17172 pop_scope (pushed_scope);
17175 /* If the scope is a dependent type and either we deferred lookup or
17176 we did lookup but didn't find the name, rememeber the name. */
17177 if (decl == error_mark_node && TYPE_P (parser->scope)
17178 && dependent_type_p (parser->scope))
17184 /* The resolution to Core Issue 180 says that `struct
17185 A::B' should be considered a type-name, even if `A'
17187 type = make_typename_type (parser->scope, name, tag_type,
17188 /*complain=*/tf_error);
17189 decl = TYPE_NAME (type);
17191 else if (is_template
17192 && (cp_parser_next_token_ends_template_argument_p (parser)
17193 || cp_lexer_next_token_is (parser->lexer,
17195 decl = make_unbound_class_template (parser->scope,
17197 /*complain=*/tf_error);
17199 decl = build_qualified_name (/*type=*/NULL_TREE,
17200 parser->scope, name,
17203 parser->qualifying_scope = parser->scope;
17204 parser->object_scope = NULL_TREE;
17206 else if (object_type)
17208 tree object_decl = NULL_TREE;
17209 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17210 OBJECT_TYPE is not a class. */
17211 if (CLASS_TYPE_P (object_type))
17212 /* If the OBJECT_TYPE is a template specialization, it may
17213 be instantiated during name lookup. In that case, errors
17214 may be issued. Even if we rollback the current tentative
17215 parse, those errors are valid. */
17216 object_decl = lookup_member (object_type,
17219 tag_type != none_type);
17220 /* Look it up in the enclosing context, too. */
17221 decl = lookup_name_real (name, tag_type != none_type,
17223 /*block_p=*/true, is_namespace, flags);
17224 parser->object_scope = object_type;
17225 parser->qualifying_scope = NULL_TREE;
17227 decl = object_decl;
17231 decl = lookup_name_real (name, tag_type != none_type,
17233 /*block_p=*/true, is_namespace, flags);
17234 parser->qualifying_scope = NULL_TREE;
17235 parser->object_scope = NULL_TREE;
17238 /* If the lookup failed, let our caller know. */
17239 if (!decl || decl == error_mark_node)
17240 return error_mark_node;
17242 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17243 if (TREE_CODE (decl) == TREE_LIST)
17245 if (ambiguous_decls)
17246 *ambiguous_decls = decl;
17247 /* The error message we have to print is too complicated for
17248 cp_parser_error, so we incorporate its actions directly. */
17249 if (!cp_parser_simulate_error (parser))
17251 error ("%Hreference to %qD is ambiguous",
17252 &name_location, name);
17253 print_candidates (decl);
17255 return error_mark_node;
17258 gcc_assert (DECL_P (decl)
17259 || TREE_CODE (decl) == OVERLOAD
17260 || TREE_CODE (decl) == SCOPE_REF
17261 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17262 || BASELINK_P (decl));
17264 /* If we have resolved the name of a member declaration, check to
17265 see if the declaration is accessible. When the name resolves to
17266 set of overloaded functions, accessibility is checked when
17267 overload resolution is done.
17269 During an explicit instantiation, access is not checked at all,
17270 as per [temp.explicit]. */
17272 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17277 /* Like cp_parser_lookup_name, but for use in the typical case where
17278 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17279 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17282 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17284 return cp_parser_lookup_name (parser, name,
17286 /*is_template=*/false,
17287 /*is_namespace=*/false,
17288 /*check_dependency=*/true,
17289 /*ambiguous_decls=*/NULL,
17293 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17294 the current context, return the TYPE_DECL. If TAG_NAME_P is
17295 true, the DECL indicates the class being defined in a class-head,
17296 or declared in an elaborated-type-specifier.
17298 Otherwise, return DECL. */
17301 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17303 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17304 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17307 template <typename T> struct B;
17310 template <typename T> struct A::B {};
17312 Similarly, in an elaborated-type-specifier:
17314 namespace N { struct X{}; }
17317 template <typename T> friend struct N::X;
17320 However, if the DECL refers to a class type, and we are in
17321 the scope of the class, then the name lookup automatically
17322 finds the TYPE_DECL created by build_self_reference rather
17323 than a TEMPLATE_DECL. For example, in:
17325 template <class T> struct S {
17329 there is no need to handle such case. */
17331 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17332 return DECL_TEMPLATE_RESULT (decl);
17337 /* If too many, or too few, template-parameter lists apply to the
17338 declarator, issue an error message. Returns TRUE if all went well,
17339 and FALSE otherwise. */
17342 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17343 cp_declarator *declarator,
17344 location_t declarator_location)
17346 unsigned num_templates;
17348 /* We haven't seen any classes that involve template parameters yet. */
17351 switch (declarator->kind)
17354 if (declarator->u.id.qualifying_scope)
17359 scope = declarator->u.id.qualifying_scope;
17360 member = declarator->u.id.unqualified_name;
17362 while (scope && CLASS_TYPE_P (scope))
17364 /* You're supposed to have one `template <...>'
17365 for every template class, but you don't need one
17366 for a full specialization. For example:
17368 template <class T> struct S{};
17369 template <> struct S<int> { void f(); };
17370 void S<int>::f () {}
17372 is correct; there shouldn't be a `template <>' for
17373 the definition of `S<int>::f'. */
17374 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17375 /* If SCOPE does not have template information of any
17376 kind, then it is not a template, nor is it nested
17377 within a template. */
17379 if (explicit_class_specialization_p (scope))
17381 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17384 scope = TYPE_CONTEXT (scope);
17387 else if (TREE_CODE (declarator->u.id.unqualified_name)
17388 == TEMPLATE_ID_EXPR)
17389 /* If the DECLARATOR has the form `X<y>' then it uses one
17390 additional level of template parameters. */
17393 return cp_parser_check_template_parameters (parser,
17395 declarator_location);
17400 case cdk_reference:
17402 return (cp_parser_check_declarator_template_parameters
17403 (parser, declarator->declarator, declarator_location));
17409 gcc_unreachable ();
17414 /* NUM_TEMPLATES were used in the current declaration. If that is
17415 invalid, return FALSE and issue an error messages. Otherwise,
17419 cp_parser_check_template_parameters (cp_parser* parser,
17420 unsigned num_templates,
17421 location_t location)
17423 /* If there are more template classes than parameter lists, we have
17426 template <class T> void S<T>::R<T>::f (); */
17427 if (parser->num_template_parameter_lists < num_templates)
17429 error ("%Htoo few template-parameter-lists", &location);
17432 /* If there are the same number of template classes and parameter
17433 lists, that's OK. */
17434 if (parser->num_template_parameter_lists == num_templates)
17436 /* If there are more, but only one more, then we are referring to a
17437 member template. That's OK too. */
17438 if (parser->num_template_parameter_lists == num_templates + 1)
17440 /* Otherwise, there are too many template parameter lists. We have
17443 template <class T> template <class U> void S::f(); */
17444 error ("%Htoo many template-parameter-lists", &location);
17448 /* Parse an optional `::' token indicating that the following name is
17449 from the global namespace. If so, PARSER->SCOPE is set to the
17450 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17451 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17452 Returns the new value of PARSER->SCOPE, if the `::' token is
17453 present, and NULL_TREE otherwise. */
17456 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17460 /* Peek at the next token. */
17461 token = cp_lexer_peek_token (parser->lexer);
17462 /* If we're looking at a `::' token then we're starting from the
17463 global namespace, not our current location. */
17464 if (token->type == CPP_SCOPE)
17466 /* Consume the `::' token. */
17467 cp_lexer_consume_token (parser->lexer);
17468 /* Set the SCOPE so that we know where to start the lookup. */
17469 parser->scope = global_namespace;
17470 parser->qualifying_scope = global_namespace;
17471 parser->object_scope = NULL_TREE;
17473 return parser->scope;
17475 else if (!current_scope_valid_p)
17477 parser->scope = NULL_TREE;
17478 parser->qualifying_scope = NULL_TREE;
17479 parser->object_scope = NULL_TREE;
17485 /* Returns TRUE if the upcoming token sequence is the start of a
17486 constructor declarator. If FRIEND_P is true, the declarator is
17487 preceded by the `friend' specifier. */
17490 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17492 bool constructor_p;
17493 tree type_decl = NULL_TREE;
17494 bool nested_name_p;
17495 cp_token *next_token;
17497 /* The common case is that this is not a constructor declarator, so
17498 try to avoid doing lots of work if at all possible. It's not
17499 valid declare a constructor at function scope. */
17500 if (parser->in_function_body)
17502 /* And only certain tokens can begin a constructor declarator. */
17503 next_token = cp_lexer_peek_token (parser->lexer);
17504 if (next_token->type != CPP_NAME
17505 && next_token->type != CPP_SCOPE
17506 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17507 && next_token->type != CPP_TEMPLATE_ID)
17510 /* Parse tentatively; we are going to roll back all of the tokens
17512 cp_parser_parse_tentatively (parser);
17513 /* Assume that we are looking at a constructor declarator. */
17514 constructor_p = true;
17516 /* Look for the optional `::' operator. */
17517 cp_parser_global_scope_opt (parser,
17518 /*current_scope_valid_p=*/false);
17519 /* Look for the nested-name-specifier. */
17521 = (cp_parser_nested_name_specifier_opt (parser,
17522 /*typename_keyword_p=*/false,
17523 /*check_dependency_p=*/false,
17525 /*is_declaration=*/false)
17527 /* Outside of a class-specifier, there must be a
17528 nested-name-specifier. */
17529 if (!nested_name_p &&
17530 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17532 constructor_p = false;
17533 /* If we still think that this might be a constructor-declarator,
17534 look for a class-name. */
17539 template <typename T> struct S { S(); };
17540 template <typename T> S<T>::S ();
17542 we must recognize that the nested `S' names a class.
17545 template <typename T> S<T>::S<T> ();
17547 we must recognize that the nested `S' names a template. */
17548 type_decl = cp_parser_class_name (parser,
17549 /*typename_keyword_p=*/false,
17550 /*template_keyword_p=*/false,
17552 /*check_dependency_p=*/false,
17553 /*class_head_p=*/false,
17554 /*is_declaration=*/false);
17555 /* If there was no class-name, then this is not a constructor. */
17556 constructor_p = !cp_parser_error_occurred (parser);
17559 /* If we're still considering a constructor, we have to see a `(',
17560 to begin the parameter-declaration-clause, followed by either a
17561 `)', an `...', or a decl-specifier. We need to check for a
17562 type-specifier to avoid being fooled into thinking that:
17566 is a constructor. (It is actually a function named `f' that
17567 takes one parameter (of type `int') and returns a value of type
17570 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17573 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17574 /* A parameter declaration begins with a decl-specifier,
17575 which is either the "attribute" keyword, a storage class
17576 specifier, or (usually) a type-specifier. */
17577 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17580 tree pushed_scope = NULL_TREE;
17581 unsigned saved_num_template_parameter_lists;
17583 /* Names appearing in the type-specifier should be looked up
17584 in the scope of the class. */
17585 if (current_class_type)
17589 type = TREE_TYPE (type_decl);
17590 if (TREE_CODE (type) == TYPENAME_TYPE)
17592 type = resolve_typename_type (type,
17593 /*only_current_p=*/false);
17594 if (TREE_CODE (type) == TYPENAME_TYPE)
17596 cp_parser_abort_tentative_parse (parser);
17600 pushed_scope = push_scope (type);
17603 /* Inside the constructor parameter list, surrounding
17604 template-parameter-lists do not apply. */
17605 saved_num_template_parameter_lists
17606 = parser->num_template_parameter_lists;
17607 parser->num_template_parameter_lists = 0;
17609 /* Look for the type-specifier. */
17610 cp_parser_type_specifier (parser,
17611 CP_PARSER_FLAGS_NONE,
17612 /*decl_specs=*/NULL,
17613 /*is_declarator=*/true,
17614 /*declares_class_or_enum=*/NULL,
17615 /*is_cv_qualifier=*/NULL);
17617 parser->num_template_parameter_lists
17618 = saved_num_template_parameter_lists;
17620 /* Leave the scope of the class. */
17622 pop_scope (pushed_scope);
17624 constructor_p = !cp_parser_error_occurred (parser);
17628 constructor_p = false;
17629 /* We did not really want to consume any tokens. */
17630 cp_parser_abort_tentative_parse (parser);
17632 return constructor_p;
17635 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17636 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17637 they must be performed once we are in the scope of the function.
17639 Returns the function defined. */
17642 cp_parser_function_definition_from_specifiers_and_declarator
17643 (cp_parser* parser,
17644 cp_decl_specifier_seq *decl_specifiers,
17646 const cp_declarator *declarator)
17651 /* Begin the function-definition. */
17652 success_p = start_function (decl_specifiers, declarator, attributes);
17654 /* The things we're about to see are not directly qualified by any
17655 template headers we've seen thus far. */
17656 reset_specialization ();
17658 /* If there were names looked up in the decl-specifier-seq that we
17659 did not check, check them now. We must wait until we are in the
17660 scope of the function to perform the checks, since the function
17661 might be a friend. */
17662 perform_deferred_access_checks ();
17666 /* Skip the entire function. */
17667 cp_parser_skip_to_end_of_block_or_statement (parser);
17668 fn = error_mark_node;
17670 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17672 /* Seen already, skip it. An error message has already been output. */
17673 cp_parser_skip_to_end_of_block_or_statement (parser);
17674 fn = current_function_decl;
17675 current_function_decl = NULL_TREE;
17676 /* If this is a function from a class, pop the nested class. */
17677 if (current_class_name)
17678 pop_nested_class ();
17681 fn = cp_parser_function_definition_after_declarator (parser,
17682 /*inline_p=*/false);
17687 /* Parse the part of a function-definition that follows the
17688 declarator. INLINE_P is TRUE iff this function is an inline
17689 function defined with a class-specifier.
17691 Returns the function defined. */
17694 cp_parser_function_definition_after_declarator (cp_parser* parser,
17698 bool ctor_initializer_p = false;
17699 bool saved_in_unbraced_linkage_specification_p;
17700 bool saved_in_function_body;
17701 unsigned saved_num_template_parameter_lists;
17704 saved_in_function_body = parser->in_function_body;
17705 parser->in_function_body = true;
17706 /* If the next token is `return', then the code may be trying to
17707 make use of the "named return value" extension that G++ used to
17709 token = cp_lexer_peek_token (parser->lexer);
17710 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17712 /* Consume the `return' keyword. */
17713 cp_lexer_consume_token (parser->lexer);
17714 /* Look for the identifier that indicates what value is to be
17716 cp_parser_identifier (parser);
17717 /* Issue an error message. */
17718 error ("%Hnamed return values are no longer supported",
17720 /* Skip tokens until we reach the start of the function body. */
17723 cp_token *token = cp_lexer_peek_token (parser->lexer);
17724 if (token->type == CPP_OPEN_BRACE
17725 || token->type == CPP_EOF
17726 || token->type == CPP_PRAGMA_EOL)
17728 cp_lexer_consume_token (parser->lexer);
17731 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17732 anything declared inside `f'. */
17733 saved_in_unbraced_linkage_specification_p
17734 = parser->in_unbraced_linkage_specification_p;
17735 parser->in_unbraced_linkage_specification_p = false;
17736 /* Inside the function, surrounding template-parameter-lists do not
17738 saved_num_template_parameter_lists
17739 = parser->num_template_parameter_lists;
17740 parser->num_template_parameter_lists = 0;
17741 /* If the next token is `try', then we are looking at a
17742 function-try-block. */
17743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17744 ctor_initializer_p = cp_parser_function_try_block (parser);
17745 /* A function-try-block includes the function-body, so we only do
17746 this next part if we're not processing a function-try-block. */
17749 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17751 /* Finish the function. */
17752 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17753 (inline_p ? 2 : 0));
17754 /* Generate code for it, if necessary. */
17755 expand_or_defer_fn (fn);
17756 /* Restore the saved values. */
17757 parser->in_unbraced_linkage_specification_p
17758 = saved_in_unbraced_linkage_specification_p;
17759 parser->num_template_parameter_lists
17760 = saved_num_template_parameter_lists;
17761 parser->in_function_body = saved_in_function_body;
17766 /* Parse a template-declaration, assuming that the `export' (and
17767 `extern') keywords, if present, has already been scanned. MEMBER_P
17768 is as for cp_parser_template_declaration. */
17771 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17773 tree decl = NULL_TREE;
17774 VEC (deferred_access_check,gc) *checks;
17775 tree parameter_list;
17776 bool friend_p = false;
17777 bool need_lang_pop;
17780 /* Look for the `template' keyword. */
17781 token = cp_lexer_peek_token (parser->lexer);
17782 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17786 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17788 if (at_class_scope_p () && current_function_decl)
17790 /* 14.5.2.2 [temp.mem]
17792 A local class shall not have member templates. */
17793 error ("%Hinvalid declaration of member template in local class",
17795 cp_parser_skip_to_end_of_block_or_statement (parser);
17800 A template ... shall not have C linkage. */
17801 if (current_lang_name == lang_name_c)
17803 error ("%Htemplate with C linkage", &token->location);
17804 /* Give it C++ linkage to avoid confusing other parts of the
17806 push_lang_context (lang_name_cplusplus);
17807 need_lang_pop = true;
17810 need_lang_pop = false;
17812 /* We cannot perform access checks on the template parameter
17813 declarations until we know what is being declared, just as we
17814 cannot check the decl-specifier list. */
17815 push_deferring_access_checks (dk_deferred);
17817 /* If the next token is `>', then we have an invalid
17818 specialization. Rather than complain about an invalid template
17819 parameter, issue an error message here. */
17820 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17822 cp_parser_error (parser, "invalid explicit specialization");
17823 begin_specialization ();
17824 parameter_list = NULL_TREE;
17827 /* Parse the template parameters. */
17828 parameter_list = cp_parser_template_parameter_list (parser);
17830 /* Get the deferred access checks from the parameter list. These
17831 will be checked once we know what is being declared, as for a
17832 member template the checks must be performed in the scope of the
17833 class containing the member. */
17834 checks = get_deferred_access_checks ();
17836 /* Look for the `>'. */
17837 cp_parser_skip_to_end_of_template_parameter_list (parser);
17838 /* We just processed one more parameter list. */
17839 ++parser->num_template_parameter_lists;
17840 /* If the next token is `template', there are more template
17842 if (cp_lexer_next_token_is_keyword (parser->lexer,
17844 cp_parser_template_declaration_after_export (parser, member_p);
17847 /* There are no access checks when parsing a template, as we do not
17848 know if a specialization will be a friend. */
17849 push_deferring_access_checks (dk_no_check);
17850 token = cp_lexer_peek_token (parser->lexer);
17851 decl = cp_parser_single_declaration (parser,
17854 /*explicit_specialization_p=*/false,
17856 pop_deferring_access_checks ();
17858 /* If this is a member template declaration, let the front
17860 if (member_p && !friend_p && decl)
17862 if (TREE_CODE (decl) == TYPE_DECL)
17863 cp_parser_check_access_in_redeclaration (decl, token->location);
17865 decl = finish_member_template_decl (decl);
17867 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17868 make_friend_class (current_class_type, TREE_TYPE (decl),
17869 /*complain=*/true);
17871 /* We are done with the current parameter list. */
17872 --parser->num_template_parameter_lists;
17874 pop_deferring_access_checks ();
17877 finish_template_decl (parameter_list);
17879 /* Register member declarations. */
17880 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17881 finish_member_declaration (decl);
17882 /* For the erroneous case of a template with C linkage, we pushed an
17883 implicit C++ linkage scope; exit that scope now. */
17885 pop_lang_context ();
17886 /* If DECL is a function template, we must return to parse it later.
17887 (Even though there is no definition, there might be default
17888 arguments that need handling.) */
17889 if (member_p && decl
17890 && (TREE_CODE (decl) == FUNCTION_DECL
17891 || DECL_FUNCTION_TEMPLATE_P (decl)))
17892 TREE_VALUE (parser->unparsed_functions_queues)
17893 = tree_cons (NULL_TREE, decl,
17894 TREE_VALUE (parser->unparsed_functions_queues));
17897 /* Perform the deferred access checks from a template-parameter-list.
17898 CHECKS is a TREE_LIST of access checks, as returned by
17899 get_deferred_access_checks. */
17902 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17904 ++processing_template_parmlist;
17905 perform_access_checks (checks);
17906 --processing_template_parmlist;
17909 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17910 `function-definition' sequence. MEMBER_P is true, this declaration
17911 appears in a class scope.
17913 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17914 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17917 cp_parser_single_declaration (cp_parser* parser,
17918 VEC (deferred_access_check,gc)* checks,
17920 bool explicit_specialization_p,
17923 int declares_class_or_enum;
17924 tree decl = NULL_TREE;
17925 cp_decl_specifier_seq decl_specifiers;
17926 bool function_definition_p = false;
17927 cp_token *decl_spec_token_start;
17929 /* This function is only used when processing a template
17931 gcc_assert (innermost_scope_kind () == sk_template_parms
17932 || innermost_scope_kind () == sk_template_spec);
17934 /* Defer access checks until we know what is being declared. */
17935 push_deferring_access_checks (dk_deferred);
17937 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17939 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17940 cp_parser_decl_specifier_seq (parser,
17941 CP_PARSER_FLAGS_OPTIONAL,
17943 &declares_class_or_enum);
17945 *friend_p = cp_parser_friend_p (&decl_specifiers);
17947 /* There are no template typedefs. */
17948 if (decl_specifiers.specs[(int) ds_typedef])
17950 error ("%Htemplate declaration of %qs",
17951 &decl_spec_token_start->location, "typedef");
17952 decl = error_mark_node;
17955 /* Gather up the access checks that occurred the
17956 decl-specifier-seq. */
17957 stop_deferring_access_checks ();
17959 /* Check for the declaration of a template class. */
17960 if (declares_class_or_enum)
17962 if (cp_parser_declares_only_class_p (parser))
17964 decl = shadow_tag (&decl_specifiers);
17969 friend template <typename T> struct A<T>::B;
17972 A<T>::B will be represented by a TYPENAME_TYPE, and
17973 therefore not recognized by shadow_tag. */
17974 if (friend_p && *friend_p
17976 && decl_specifiers.type
17977 && TYPE_P (decl_specifiers.type))
17978 decl = decl_specifiers.type;
17980 if (decl && decl != error_mark_node)
17981 decl = TYPE_NAME (decl);
17983 decl = error_mark_node;
17985 /* Perform access checks for template parameters. */
17986 cp_parser_perform_template_parameter_access_checks (checks);
17989 /* If it's not a template class, try for a template function. If
17990 the next token is a `;', then this declaration does not declare
17991 anything. But, if there were errors in the decl-specifiers, then
17992 the error might well have come from an attempted class-specifier.
17993 In that case, there's no need to warn about a missing declarator. */
17995 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17996 || decl_specifiers.type != error_mark_node))
17998 decl = cp_parser_init_declarator (parser,
18001 /*function_definition_allowed_p=*/true,
18003 declares_class_or_enum,
18004 &function_definition_p);
18006 /* 7.1.1-1 [dcl.stc]
18008 A storage-class-specifier shall not be specified in an explicit
18009 specialization... */
18011 && explicit_specialization_p
18012 && decl_specifiers.storage_class != sc_none)
18014 error ("%Hexplicit template specialization cannot have a storage class",
18015 &decl_spec_token_start->location);
18016 decl = error_mark_node;
18020 pop_deferring_access_checks ();
18022 /* Clear any current qualification; whatever comes next is the start
18023 of something new. */
18024 parser->scope = NULL_TREE;
18025 parser->qualifying_scope = NULL_TREE;
18026 parser->object_scope = NULL_TREE;
18027 /* Look for a trailing `;' after the declaration. */
18028 if (!function_definition_p
18029 && (decl == error_mark_node
18030 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18031 cp_parser_skip_to_end_of_block_or_statement (parser);
18036 /* Parse a cast-expression that is not the operand of a unary "&". */
18039 cp_parser_simple_cast_expression (cp_parser *parser)
18041 return cp_parser_cast_expression (parser, /*address_p=*/false,
18042 /*cast_p=*/false, NULL);
18045 /* Parse a functional cast to TYPE. Returns an expression
18046 representing the cast. */
18049 cp_parser_functional_cast (cp_parser* parser, tree type)
18051 tree expression_list;
18055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18057 maybe_warn_cpp0x ("extended initializer lists");
18058 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18059 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18060 if (TREE_CODE (type) == TYPE_DECL)
18061 type = TREE_TYPE (type);
18062 return finish_compound_literal (type, expression_list);
18066 = cp_parser_parenthesized_expression_list (parser, false,
18068 /*allow_expansion_p=*/true,
18069 /*non_constant_p=*/NULL);
18071 cast = build_functional_cast (type, expression_list,
18072 tf_warning_or_error);
18073 /* [expr.const]/1: In an integral constant expression "only type
18074 conversions to integral or enumeration type can be used". */
18075 if (TREE_CODE (type) == TYPE_DECL)
18076 type = TREE_TYPE (type);
18077 if (cast != error_mark_node
18078 && !cast_valid_in_integral_constant_expression_p (type)
18079 && (cp_parser_non_integral_constant_expression
18080 (parser, "a call to a constructor")))
18081 return error_mark_node;
18085 /* Save the tokens that make up the body of a member function defined
18086 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18087 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18088 specifiers applied to the declaration. Returns the FUNCTION_DECL
18089 for the member function. */
18092 cp_parser_save_member_function_body (cp_parser* parser,
18093 cp_decl_specifier_seq *decl_specifiers,
18094 cp_declarator *declarator,
18101 /* Create the function-declaration. */
18102 fn = start_method (decl_specifiers, declarator, attributes);
18103 /* If something went badly wrong, bail out now. */
18104 if (fn == error_mark_node)
18106 /* If there's a function-body, skip it. */
18107 if (cp_parser_token_starts_function_definition_p
18108 (cp_lexer_peek_token (parser->lexer)))
18109 cp_parser_skip_to_end_of_block_or_statement (parser);
18110 return error_mark_node;
18113 /* Remember it, if there default args to post process. */
18114 cp_parser_save_default_args (parser, fn);
18116 /* Save away the tokens that make up the body of the
18118 first = parser->lexer->next_token;
18119 /* We can have braced-init-list mem-initializers before the fn body. */
18120 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18122 cp_lexer_consume_token (parser->lexer);
18123 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18124 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18126 /* cache_group will stop after an un-nested { } pair, too. */
18127 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18130 /* variadic mem-inits have ... after the ')'. */
18131 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18132 cp_lexer_consume_token (parser->lexer);
18135 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18136 /* Handle function try blocks. */
18137 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18138 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18139 last = parser->lexer->next_token;
18141 /* Save away the inline definition; we will process it when the
18142 class is complete. */
18143 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18144 DECL_PENDING_INLINE_P (fn) = 1;
18146 /* We need to know that this was defined in the class, so that
18147 friend templates are handled correctly. */
18148 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18150 /* We're done with the inline definition. */
18151 finish_method (fn);
18153 /* Add FN to the queue of functions to be parsed later. */
18154 TREE_VALUE (parser->unparsed_functions_queues)
18155 = tree_cons (NULL_TREE, fn,
18156 TREE_VALUE (parser->unparsed_functions_queues));
18161 /* Parse a template-argument-list, as well as the trailing ">" (but
18162 not the opening ">"). See cp_parser_template_argument_list for the
18166 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18170 tree saved_qualifying_scope;
18171 tree saved_object_scope;
18172 bool saved_greater_than_is_operator_p;
18173 bool saved_skip_evaluation;
18177 When parsing a template-id, the first non-nested `>' is taken as
18178 the end of the template-argument-list rather than a greater-than
18180 saved_greater_than_is_operator_p
18181 = parser->greater_than_is_operator_p;
18182 parser->greater_than_is_operator_p = false;
18183 /* Parsing the argument list may modify SCOPE, so we save it
18185 saved_scope = parser->scope;
18186 saved_qualifying_scope = parser->qualifying_scope;
18187 saved_object_scope = parser->object_scope;
18188 /* We need to evaluate the template arguments, even though this
18189 template-id may be nested within a "sizeof". */
18190 saved_skip_evaluation = skip_evaluation;
18191 skip_evaluation = false;
18192 /* Parse the template-argument-list itself. */
18193 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18194 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18195 arguments = NULL_TREE;
18197 arguments = cp_parser_template_argument_list (parser);
18198 /* Look for the `>' that ends the template-argument-list. If we find
18199 a '>>' instead, it's probably just a typo. */
18200 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18202 if (cxx_dialect != cxx98)
18204 /* In C++0x, a `>>' in a template argument list or cast
18205 expression is considered to be two separate `>'
18206 tokens. So, change the current token to a `>', but don't
18207 consume it: it will be consumed later when the outer
18208 template argument list (or cast expression) is parsed.
18209 Note that this replacement of `>' for `>>' is necessary
18210 even if we are parsing tentatively: in the tentative
18211 case, after calling
18212 cp_parser_enclosed_template_argument_list we will always
18213 throw away all of the template arguments and the first
18214 closing `>', either because the template argument list
18215 was erroneous or because we are replacing those tokens
18216 with a CPP_TEMPLATE_ID token. The second `>' (which will
18217 not have been thrown away) is needed either to close an
18218 outer template argument list or to complete a new-style
18220 cp_token *token = cp_lexer_peek_token (parser->lexer);
18221 token->type = CPP_GREATER;
18223 else if (!saved_greater_than_is_operator_p)
18225 /* If we're in a nested template argument list, the '>>' has
18226 to be a typo for '> >'. We emit the error message, but we
18227 continue parsing and we push a '>' as next token, so that
18228 the argument list will be parsed correctly. Note that the
18229 global source location is still on the token before the
18230 '>>', so we need to say explicitly where we want it. */
18231 cp_token *token = cp_lexer_peek_token (parser->lexer);
18232 error ("%H%<>>%> should be %<> >%> "
18233 "within a nested template argument list",
18236 token->type = CPP_GREATER;
18240 /* If this is not a nested template argument list, the '>>'
18241 is a typo for '>'. Emit an error message and continue.
18242 Same deal about the token location, but here we can get it
18243 right by consuming the '>>' before issuing the diagnostic. */
18244 cp_token *token = cp_lexer_consume_token (parser->lexer);
18245 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18246 "a template argument list", &token->location);
18250 cp_parser_skip_to_end_of_template_parameter_list (parser);
18251 /* The `>' token might be a greater-than operator again now. */
18252 parser->greater_than_is_operator_p
18253 = saved_greater_than_is_operator_p;
18254 /* Restore the SAVED_SCOPE. */
18255 parser->scope = saved_scope;
18256 parser->qualifying_scope = saved_qualifying_scope;
18257 parser->object_scope = saved_object_scope;
18258 skip_evaluation = saved_skip_evaluation;
18263 /* MEMBER_FUNCTION is a member function, or a friend. If default
18264 arguments, or the body of the function have not yet been parsed,
18268 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18270 /* If this member is a template, get the underlying
18272 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18273 member_function = DECL_TEMPLATE_RESULT (member_function);
18275 /* There should not be any class definitions in progress at this
18276 point; the bodies of members are only parsed outside of all class
18278 gcc_assert (parser->num_classes_being_defined == 0);
18279 /* While we're parsing the member functions we might encounter more
18280 classes. We want to handle them right away, but we don't want
18281 them getting mixed up with functions that are currently in the
18283 parser->unparsed_functions_queues
18284 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18286 /* Make sure that any template parameters are in scope. */
18287 maybe_begin_member_template_processing (member_function);
18289 /* If the body of the function has not yet been parsed, parse it
18291 if (DECL_PENDING_INLINE_P (member_function))
18293 tree function_scope;
18294 cp_token_cache *tokens;
18296 /* The function is no longer pending; we are processing it. */
18297 tokens = DECL_PENDING_INLINE_INFO (member_function);
18298 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18299 DECL_PENDING_INLINE_P (member_function) = 0;
18301 /* If this is a local class, enter the scope of the containing
18303 function_scope = current_function_decl;
18304 if (function_scope)
18305 push_function_context ();
18307 /* Push the body of the function onto the lexer stack. */
18308 cp_parser_push_lexer_for_tokens (parser, tokens);
18310 /* Let the front end know that we going to be defining this
18312 start_preparsed_function (member_function, NULL_TREE,
18313 SF_PRE_PARSED | SF_INCLASS_INLINE);
18315 /* Don't do access checking if it is a templated function. */
18316 if (processing_template_decl)
18317 push_deferring_access_checks (dk_no_check);
18319 /* Now, parse the body of the function. */
18320 cp_parser_function_definition_after_declarator (parser,
18321 /*inline_p=*/true);
18323 if (processing_template_decl)
18324 pop_deferring_access_checks ();
18326 /* Leave the scope of the containing function. */
18327 if (function_scope)
18328 pop_function_context ();
18329 cp_parser_pop_lexer (parser);
18332 /* Remove any template parameters from the symbol table. */
18333 maybe_end_member_template_processing ();
18335 /* Restore the queue. */
18336 parser->unparsed_functions_queues
18337 = TREE_CHAIN (parser->unparsed_functions_queues);
18340 /* If DECL contains any default args, remember it on the unparsed
18341 functions queue. */
18344 cp_parser_save_default_args (cp_parser* parser, tree decl)
18348 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18350 probe = TREE_CHAIN (probe))
18351 if (TREE_PURPOSE (probe))
18353 TREE_PURPOSE (parser->unparsed_functions_queues)
18354 = tree_cons (current_class_type, decl,
18355 TREE_PURPOSE (parser->unparsed_functions_queues));
18360 /* FN is a FUNCTION_DECL which may contains a parameter with an
18361 unparsed DEFAULT_ARG. Parse the default args now. This function
18362 assumes that the current scope is the scope in which the default
18363 argument should be processed. */
18366 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18368 bool saved_local_variables_forbidden_p;
18371 /* While we're parsing the default args, we might (due to the
18372 statement expression extension) encounter more classes. We want
18373 to handle them right away, but we don't want them getting mixed
18374 up with default args that are currently in the queue. */
18375 parser->unparsed_functions_queues
18376 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18378 /* Local variable names (and the `this' keyword) may not appear
18379 in a default argument. */
18380 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18381 parser->local_variables_forbidden_p = true;
18383 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18385 parm = TREE_CHAIN (parm))
18387 cp_token_cache *tokens;
18388 tree default_arg = TREE_PURPOSE (parm);
18390 VEC(tree,gc) *insts;
18397 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18398 /* This can happen for a friend declaration for a function
18399 already declared with default arguments. */
18402 /* Push the saved tokens for the default argument onto the parser's
18404 tokens = DEFARG_TOKENS (default_arg);
18405 cp_parser_push_lexer_for_tokens (parser, tokens);
18407 /* Parse the assignment-expression. */
18408 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18409 if (parsed_arg == error_mark_node)
18411 cp_parser_pop_lexer (parser);
18415 if (!processing_template_decl)
18416 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18418 TREE_PURPOSE (parm) = parsed_arg;
18420 /* Update any instantiations we've already created. */
18421 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18422 VEC_iterate (tree, insts, ix, copy); ix++)
18423 TREE_PURPOSE (copy) = parsed_arg;
18425 /* If the token stream has not been completely used up, then
18426 there was extra junk after the end of the default
18428 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18429 cp_parser_error (parser, "expected %<,%>");
18431 /* Revert to the main lexer. */
18432 cp_parser_pop_lexer (parser);
18435 /* Make sure no default arg is missing. */
18436 check_default_args (fn);
18438 /* Restore the state of local_variables_forbidden_p. */
18439 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18441 /* Restore the queue. */
18442 parser->unparsed_functions_queues
18443 = TREE_CHAIN (parser->unparsed_functions_queues);
18446 /* Parse the operand of `sizeof' (or a similar operator). Returns
18447 either a TYPE or an expression, depending on the form of the
18448 input. The KEYWORD indicates which kind of expression we have
18452 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18454 tree expr = NULL_TREE;
18455 const char *saved_message;
18457 bool saved_integral_constant_expression_p;
18458 bool saved_non_integral_constant_expression_p;
18459 bool pack_expansion_p = false;
18461 /* Types cannot be defined in a `sizeof' expression. Save away the
18463 saved_message = parser->type_definition_forbidden_message;
18464 /* And create the new one. */
18465 tmp = concat ("types may not be defined in %<",
18466 IDENTIFIER_POINTER (ridpointers[keyword]),
18467 "%> expressions", NULL);
18468 parser->type_definition_forbidden_message = tmp;
18470 /* The restrictions on constant-expressions do not apply inside
18471 sizeof expressions. */
18472 saved_integral_constant_expression_p
18473 = parser->integral_constant_expression_p;
18474 saved_non_integral_constant_expression_p
18475 = parser->non_integral_constant_expression_p;
18476 parser->integral_constant_expression_p = false;
18478 /* If it's a `...', then we are computing the length of a parameter
18480 if (keyword == RID_SIZEOF
18481 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18483 /* Consume the `...'. */
18484 cp_lexer_consume_token (parser->lexer);
18485 maybe_warn_variadic_templates ();
18487 /* Note that this is an expansion. */
18488 pack_expansion_p = true;
18491 /* Do not actually evaluate the expression. */
18493 /* If it's a `(', then we might be looking at the type-id
18495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18498 bool saved_in_type_id_in_expr_p;
18500 /* We can't be sure yet whether we're looking at a type-id or an
18502 cp_parser_parse_tentatively (parser);
18503 /* Consume the `('. */
18504 cp_lexer_consume_token (parser->lexer);
18505 /* Parse the type-id. */
18506 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18507 parser->in_type_id_in_expr_p = true;
18508 type = cp_parser_type_id (parser);
18509 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18510 /* Now, look for the trailing `)'. */
18511 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18512 /* If all went well, then we're done. */
18513 if (cp_parser_parse_definitely (parser))
18515 cp_decl_specifier_seq decl_specs;
18517 /* Build a trivial decl-specifier-seq. */
18518 clear_decl_specs (&decl_specs);
18519 decl_specs.type = type;
18521 /* Call grokdeclarator to figure out what type this is. */
18522 expr = grokdeclarator (NULL,
18526 /*attrlist=*/NULL);
18530 /* If the type-id production did not work out, then we must be
18531 looking at the unary-expression production. */
18533 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18534 /*cast_p=*/false, NULL);
18536 if (pack_expansion_p)
18537 /* Build a pack expansion. */
18538 expr = make_pack_expansion (expr);
18540 /* Go back to evaluating expressions. */
18543 /* Free the message we created. */
18545 /* And restore the old one. */
18546 parser->type_definition_forbidden_message = saved_message;
18547 parser->integral_constant_expression_p
18548 = saved_integral_constant_expression_p;
18549 parser->non_integral_constant_expression_p
18550 = saved_non_integral_constant_expression_p;
18555 /* If the current declaration has no declarator, return true. */
18558 cp_parser_declares_only_class_p (cp_parser *parser)
18560 /* If the next token is a `;' or a `,' then there is no
18562 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18563 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18566 /* Update the DECL_SPECS to reflect the storage class indicated by
18570 cp_parser_set_storage_class (cp_parser *parser,
18571 cp_decl_specifier_seq *decl_specs,
18573 location_t location)
18575 cp_storage_class storage_class;
18577 if (parser->in_unbraced_linkage_specification_p)
18579 error ("%Hinvalid use of %qD in linkage specification",
18580 &location, ridpointers[keyword]);
18583 else if (decl_specs->storage_class != sc_none)
18585 decl_specs->conflicting_specifiers_p = true;
18589 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18590 && decl_specs->specs[(int) ds_thread])
18592 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18593 decl_specs->specs[(int) ds_thread] = 0;
18599 storage_class = sc_auto;
18602 storage_class = sc_register;
18605 storage_class = sc_static;
18608 storage_class = sc_extern;
18611 storage_class = sc_mutable;
18614 gcc_unreachable ();
18616 decl_specs->storage_class = storage_class;
18618 /* A storage class specifier cannot be applied alongside a typedef
18619 specifier. If there is a typedef specifier present then set
18620 conflicting_specifiers_p which will trigger an error later
18621 on in grokdeclarator. */
18622 if (decl_specs->specs[(int)ds_typedef])
18623 decl_specs->conflicting_specifiers_p = true;
18626 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18627 is true, the type is a user-defined type; otherwise it is a
18628 built-in type specified by a keyword. */
18631 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18633 location_t location,
18634 bool user_defined_p)
18636 decl_specs->any_specifiers_p = true;
18638 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18639 (with, for example, in "typedef int wchar_t;") we remember that
18640 this is what happened. In system headers, we ignore these
18641 declarations so that G++ can work with system headers that are not
18643 if (decl_specs->specs[(int) ds_typedef]
18645 && (type_spec == boolean_type_node
18646 || type_spec == char16_type_node
18647 || type_spec == char32_type_node
18648 || type_spec == wchar_type_node)
18649 && (decl_specs->type
18650 || decl_specs->specs[(int) ds_long]
18651 || decl_specs->specs[(int) ds_short]
18652 || decl_specs->specs[(int) ds_unsigned]
18653 || decl_specs->specs[(int) ds_signed]))
18655 decl_specs->redefined_builtin_type = type_spec;
18656 if (!decl_specs->type)
18658 decl_specs->type = type_spec;
18659 decl_specs->user_defined_type_p = false;
18660 decl_specs->type_location = location;
18663 else if (decl_specs->type)
18664 decl_specs->multiple_types_p = true;
18667 decl_specs->type = type_spec;
18668 decl_specs->user_defined_type_p = user_defined_p;
18669 decl_specs->redefined_builtin_type = NULL_TREE;
18670 decl_specs->type_location = location;
18674 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18675 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18678 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18680 return decl_specifiers->specs[(int) ds_friend] != 0;
18683 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18684 issue an error message indicating that TOKEN_DESC was expected.
18686 Returns the token consumed, if the token had the appropriate type.
18687 Otherwise, returns NULL. */
18690 cp_parser_require (cp_parser* parser,
18691 enum cpp_ttype type,
18692 const char* token_desc)
18694 if (cp_lexer_next_token_is (parser->lexer, type))
18695 return cp_lexer_consume_token (parser->lexer);
18698 /* Output the MESSAGE -- unless we're parsing tentatively. */
18699 if (!cp_parser_simulate_error (parser))
18701 char *message = concat ("expected ", token_desc, NULL);
18702 cp_parser_error (parser, message);
18709 /* An error message is produced if the next token is not '>'.
18710 All further tokens are skipped until the desired token is
18711 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18714 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18716 /* Current level of '< ... >'. */
18717 unsigned level = 0;
18718 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18719 unsigned nesting_depth = 0;
18721 /* Are we ready, yet? If not, issue error message. */
18722 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18725 /* Skip tokens until the desired token is found. */
18728 /* Peek at the next token. */
18729 switch (cp_lexer_peek_token (parser->lexer)->type)
18732 if (!nesting_depth)
18737 if (cxx_dialect == cxx98)
18738 /* C++0x views the `>>' operator as two `>' tokens, but
18741 else if (!nesting_depth && level-- == 0)
18743 /* We've hit a `>>' where the first `>' closes the
18744 template argument list, and the second `>' is
18745 spurious. Just consume the `>>' and stop; we've
18746 already produced at least one error. */
18747 cp_lexer_consume_token (parser->lexer);
18750 /* Fall through for C++0x, so we handle the second `>' in
18754 if (!nesting_depth && level-- == 0)
18756 /* We've reached the token we want, consume it and stop. */
18757 cp_lexer_consume_token (parser->lexer);
18762 case CPP_OPEN_PAREN:
18763 case CPP_OPEN_SQUARE:
18767 case CPP_CLOSE_PAREN:
18768 case CPP_CLOSE_SQUARE:
18769 if (nesting_depth-- == 0)
18774 case CPP_PRAGMA_EOL:
18775 case CPP_SEMICOLON:
18776 case CPP_OPEN_BRACE:
18777 case CPP_CLOSE_BRACE:
18778 /* The '>' was probably forgotten, don't look further. */
18785 /* Consume this token. */
18786 cp_lexer_consume_token (parser->lexer);
18790 /* If the next token is the indicated keyword, consume it. Otherwise,
18791 issue an error message indicating that TOKEN_DESC was expected.
18793 Returns the token consumed, if the token had the appropriate type.
18794 Otherwise, returns NULL. */
18797 cp_parser_require_keyword (cp_parser* parser,
18799 const char* token_desc)
18801 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18803 if (token && token->keyword != keyword)
18805 dyn_string_t error_msg;
18807 /* Format the error message. */
18808 error_msg = dyn_string_new (0);
18809 dyn_string_append_cstr (error_msg, "expected ");
18810 dyn_string_append_cstr (error_msg, token_desc);
18811 cp_parser_error (parser, error_msg->s);
18812 dyn_string_delete (error_msg);
18819 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18820 function-definition. */
18823 cp_parser_token_starts_function_definition_p (cp_token* token)
18825 return (/* An ordinary function-body begins with an `{'. */
18826 token->type == CPP_OPEN_BRACE
18827 /* A ctor-initializer begins with a `:'. */
18828 || token->type == CPP_COLON
18829 /* A function-try-block begins with `try'. */
18830 || token->keyword == RID_TRY
18831 /* The named return value extension begins with `return'. */
18832 || token->keyword == RID_RETURN);
18835 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18839 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18843 token = cp_lexer_peek_token (parser->lexer);
18844 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18847 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18848 C++0x) ending a template-argument. */
18851 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18855 token = cp_lexer_peek_token (parser->lexer);
18856 return (token->type == CPP_COMMA
18857 || token->type == CPP_GREATER
18858 || token->type == CPP_ELLIPSIS
18859 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18862 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18863 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18866 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18871 token = cp_lexer_peek_nth_token (parser->lexer, n);
18872 if (token->type == CPP_LESS)
18874 /* Check for the sequence `<::' in the original code. It would be lexed as
18875 `[:', where `[' is a digraph, and there is no whitespace before
18877 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18880 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18881 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18887 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18888 or none_type otherwise. */
18890 static enum tag_types
18891 cp_parser_token_is_class_key (cp_token* token)
18893 switch (token->keyword)
18898 return record_type;
18907 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18910 cp_parser_check_class_key (enum tag_types class_key, tree type)
18912 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18913 permerror (input_location, "%qs tag used in naming %q#T",
18914 class_key == union_type ? "union"
18915 : class_key == record_type ? "struct" : "class",
18919 /* Issue an error message if DECL is redeclared with different
18920 access than its original declaration [class.access.spec/3].
18921 This applies to nested classes and nested class templates.
18925 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18927 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18930 if ((TREE_PRIVATE (decl)
18931 != (current_access_specifier == access_private_node))
18932 || (TREE_PROTECTED (decl)
18933 != (current_access_specifier == access_protected_node)))
18934 error ("%H%qD redeclared with different access", &location, decl);
18937 /* Look for the `template' keyword, as a syntactic disambiguator.
18938 Return TRUE iff it is present, in which case it will be
18942 cp_parser_optional_template_keyword (cp_parser *parser)
18944 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18946 /* The `template' keyword can only be used within templates;
18947 outside templates the parser can always figure out what is a
18948 template and what is not. */
18949 if (!processing_template_decl)
18951 cp_token *token = cp_lexer_peek_token (parser->lexer);
18952 error ("%H%<template%> (as a disambiguator) is only allowed "
18953 "within templates", &token->location);
18954 /* If this part of the token stream is rescanned, the same
18955 error message would be generated. So, we purge the token
18956 from the stream. */
18957 cp_lexer_purge_token (parser->lexer);
18962 /* Consume the `template' keyword. */
18963 cp_lexer_consume_token (parser->lexer);
18971 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18972 set PARSER->SCOPE, and perform other related actions. */
18975 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18978 struct tree_check *check_value;
18979 deferred_access_check *chk;
18980 VEC (deferred_access_check,gc) *checks;
18982 /* Get the stored value. */
18983 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18984 /* Perform any access checks that were deferred. */
18985 checks = check_value->checks;
18989 VEC_iterate (deferred_access_check, checks, i, chk) ;
18992 perform_or_defer_access_check (chk->binfo,
18997 /* Set the scope from the stored value. */
18998 parser->scope = check_value->value;
18999 parser->qualifying_scope = check_value->qualifying_scope;
19000 parser->object_scope = NULL_TREE;
19003 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19004 encounter the end of a block before what we were looking for. */
19007 cp_parser_cache_group (cp_parser *parser,
19008 enum cpp_ttype end,
19013 cp_token *token = cp_lexer_peek_token (parser->lexer);
19015 /* Abort a parenthesized expression if we encounter a semicolon. */
19016 if ((end == CPP_CLOSE_PAREN || depth == 0)
19017 && token->type == CPP_SEMICOLON)
19019 /* If we've reached the end of the file, stop. */
19020 if (token->type == CPP_EOF
19021 || (end != CPP_PRAGMA_EOL
19022 && token->type == CPP_PRAGMA_EOL))
19024 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19025 /* We've hit the end of an enclosing block, so there's been some
19026 kind of syntax error. */
19029 /* Consume the token. */
19030 cp_lexer_consume_token (parser->lexer);
19031 /* See if it starts a new group. */
19032 if (token->type == CPP_OPEN_BRACE)
19034 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19035 /* In theory this should probably check end == '}', but
19036 cp_parser_save_member_function_body needs it to exit
19037 after either '}' or ')' when called with ')'. */
19041 else if (token->type == CPP_OPEN_PAREN)
19043 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19044 if (depth == 0 && end == CPP_CLOSE_PAREN)
19047 else if (token->type == CPP_PRAGMA)
19048 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19049 else if (token->type == end)
19054 /* Begin parsing tentatively. We always save tokens while parsing
19055 tentatively so that if the tentative parsing fails we can restore the
19059 cp_parser_parse_tentatively (cp_parser* parser)
19061 /* Enter a new parsing context. */
19062 parser->context = cp_parser_context_new (parser->context);
19063 /* Begin saving tokens. */
19064 cp_lexer_save_tokens (parser->lexer);
19065 /* In order to avoid repetitive access control error messages,
19066 access checks are queued up until we are no longer parsing
19068 push_deferring_access_checks (dk_deferred);
19071 /* Commit to the currently active tentative parse. */
19074 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19076 cp_parser_context *context;
19079 /* Mark all of the levels as committed. */
19080 lexer = parser->lexer;
19081 for (context = parser->context; context->next; context = context->next)
19083 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19085 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19086 while (!cp_lexer_saving_tokens (lexer))
19087 lexer = lexer->next;
19088 cp_lexer_commit_tokens (lexer);
19092 /* Abort the currently active tentative parse. All consumed tokens
19093 will be rolled back, and no diagnostics will be issued. */
19096 cp_parser_abort_tentative_parse (cp_parser* parser)
19098 cp_parser_simulate_error (parser);
19099 /* Now, pretend that we want to see if the construct was
19100 successfully parsed. */
19101 cp_parser_parse_definitely (parser);
19104 /* Stop parsing tentatively. If a parse error has occurred, restore the
19105 token stream. Otherwise, commit to the tokens we have consumed.
19106 Returns true if no error occurred; false otherwise. */
19109 cp_parser_parse_definitely (cp_parser* parser)
19111 bool error_occurred;
19112 cp_parser_context *context;
19114 /* Remember whether or not an error occurred, since we are about to
19115 destroy that information. */
19116 error_occurred = cp_parser_error_occurred (parser);
19117 /* Remove the topmost context from the stack. */
19118 context = parser->context;
19119 parser->context = context->next;
19120 /* If no parse errors occurred, commit to the tentative parse. */
19121 if (!error_occurred)
19123 /* Commit to the tokens read tentatively, unless that was
19125 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19126 cp_lexer_commit_tokens (parser->lexer);
19128 pop_to_parent_deferring_access_checks ();
19130 /* Otherwise, if errors occurred, roll back our state so that things
19131 are just as they were before we began the tentative parse. */
19134 cp_lexer_rollback_tokens (parser->lexer);
19135 pop_deferring_access_checks ();
19137 /* Add the context to the front of the free list. */
19138 context->next = cp_parser_context_free_list;
19139 cp_parser_context_free_list = context;
19141 return !error_occurred;
19144 /* Returns true if we are parsing tentatively and are not committed to
19145 this tentative parse. */
19148 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19150 return (cp_parser_parsing_tentatively (parser)
19151 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19154 /* Returns nonzero iff an error has occurred during the most recent
19155 tentative parse. */
19158 cp_parser_error_occurred (cp_parser* parser)
19160 return (cp_parser_parsing_tentatively (parser)
19161 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19164 /* Returns nonzero if GNU extensions are allowed. */
19167 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19169 return parser->allow_gnu_extensions_p;
19172 /* Objective-C++ Productions */
19175 /* Parse an Objective-C expression, which feeds into a primary-expression
19179 objc-message-expression
19180 objc-string-literal
19181 objc-encode-expression
19182 objc-protocol-expression
19183 objc-selector-expression
19185 Returns a tree representation of the expression. */
19188 cp_parser_objc_expression (cp_parser* parser)
19190 /* Try to figure out what kind of declaration is present. */
19191 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19195 case CPP_OPEN_SQUARE:
19196 return cp_parser_objc_message_expression (parser);
19198 case CPP_OBJC_STRING:
19199 kwd = cp_lexer_consume_token (parser->lexer);
19200 return objc_build_string_object (kwd->u.value);
19203 switch (kwd->keyword)
19205 case RID_AT_ENCODE:
19206 return cp_parser_objc_encode_expression (parser);
19208 case RID_AT_PROTOCOL:
19209 return cp_parser_objc_protocol_expression (parser);
19211 case RID_AT_SELECTOR:
19212 return cp_parser_objc_selector_expression (parser);
19218 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19219 &kwd->location, kwd->u.value);
19220 cp_parser_skip_to_end_of_block_or_statement (parser);
19223 return error_mark_node;
19226 /* Parse an Objective-C message expression.
19228 objc-message-expression:
19229 [ objc-message-receiver objc-message-args ]
19231 Returns a representation of an Objective-C message. */
19234 cp_parser_objc_message_expression (cp_parser* parser)
19236 tree receiver, messageargs;
19238 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19239 receiver = cp_parser_objc_message_receiver (parser);
19240 messageargs = cp_parser_objc_message_args (parser);
19241 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19243 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19246 /* Parse an objc-message-receiver.
19248 objc-message-receiver:
19250 simple-type-specifier
19252 Returns a representation of the type or expression. */
19255 cp_parser_objc_message_receiver (cp_parser* parser)
19259 /* An Objective-C message receiver may be either (1) a type
19260 or (2) an expression. */
19261 cp_parser_parse_tentatively (parser);
19262 rcv = cp_parser_expression (parser, false, NULL);
19264 if (cp_parser_parse_definitely (parser))
19267 rcv = cp_parser_simple_type_specifier (parser,
19268 /*decl_specs=*/NULL,
19269 CP_PARSER_FLAGS_NONE);
19271 return objc_get_class_reference (rcv);
19274 /* Parse the arguments and selectors comprising an Objective-C message.
19279 objc-selector-args , objc-comma-args
19281 objc-selector-args:
19282 objc-selector [opt] : assignment-expression
19283 objc-selector-args objc-selector [opt] : assignment-expression
19286 assignment-expression
19287 objc-comma-args , assignment-expression
19289 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19290 selector arguments and TREE_VALUE containing a list of comma
19294 cp_parser_objc_message_args (cp_parser* parser)
19296 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19297 bool maybe_unary_selector_p = true;
19298 cp_token *token = cp_lexer_peek_token (parser->lexer);
19300 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19302 tree selector = NULL_TREE, arg;
19304 if (token->type != CPP_COLON)
19305 selector = cp_parser_objc_selector (parser);
19307 /* Detect if we have a unary selector. */
19308 if (maybe_unary_selector_p
19309 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19310 return build_tree_list (selector, NULL_TREE);
19312 maybe_unary_selector_p = false;
19313 cp_parser_require (parser, CPP_COLON, "%<:%>");
19314 arg = cp_parser_assignment_expression (parser, false, NULL);
19317 = chainon (sel_args,
19318 build_tree_list (selector, arg));
19320 token = cp_lexer_peek_token (parser->lexer);
19323 /* Handle non-selector arguments, if any. */
19324 while (token->type == CPP_COMMA)
19328 cp_lexer_consume_token (parser->lexer);
19329 arg = cp_parser_assignment_expression (parser, false, NULL);
19332 = chainon (addl_args,
19333 build_tree_list (NULL_TREE, arg));
19335 token = cp_lexer_peek_token (parser->lexer);
19338 return build_tree_list (sel_args, addl_args);
19341 /* Parse an Objective-C encode expression.
19343 objc-encode-expression:
19344 @encode objc-typename
19346 Returns an encoded representation of the type argument. */
19349 cp_parser_objc_encode_expression (cp_parser* parser)
19354 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19355 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19356 token = cp_lexer_peek_token (parser->lexer);
19357 type = complete_type (cp_parser_type_id (parser));
19358 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19362 error ("%H%<@encode%> must specify a type as an argument",
19364 return error_mark_node;
19367 return objc_build_encode_expr (type);
19370 /* Parse an Objective-C @defs expression. */
19373 cp_parser_objc_defs_expression (cp_parser *parser)
19377 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19378 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19379 name = cp_parser_identifier (parser);
19380 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19382 return objc_get_class_ivars (name);
19385 /* Parse an Objective-C protocol expression.
19387 objc-protocol-expression:
19388 @protocol ( identifier )
19390 Returns a representation of the protocol expression. */
19393 cp_parser_objc_protocol_expression (cp_parser* parser)
19397 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19398 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19399 proto = cp_parser_identifier (parser);
19400 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19402 return objc_build_protocol_expr (proto);
19405 /* Parse an Objective-C selector expression.
19407 objc-selector-expression:
19408 @selector ( objc-method-signature )
19410 objc-method-signature:
19416 objc-selector-seq objc-selector :
19418 Returns a representation of the method selector. */
19421 cp_parser_objc_selector_expression (cp_parser* parser)
19423 tree sel_seq = NULL_TREE;
19424 bool maybe_unary_selector_p = true;
19427 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19428 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19429 token = cp_lexer_peek_token (parser->lexer);
19431 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19432 || token->type == CPP_SCOPE)
19434 tree selector = NULL_TREE;
19436 if (token->type != CPP_COLON
19437 || token->type == CPP_SCOPE)
19438 selector = cp_parser_objc_selector (parser);
19440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19441 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19443 /* Detect if we have a unary selector. */
19444 if (maybe_unary_selector_p)
19446 sel_seq = selector;
19447 goto finish_selector;
19451 cp_parser_error (parser, "expected %<:%>");
19454 maybe_unary_selector_p = false;
19455 token = cp_lexer_consume_token (parser->lexer);
19457 if (token->type == CPP_SCOPE)
19460 = chainon (sel_seq,
19461 build_tree_list (selector, NULL_TREE));
19463 = chainon (sel_seq,
19464 build_tree_list (NULL_TREE, NULL_TREE));
19468 = chainon (sel_seq,
19469 build_tree_list (selector, NULL_TREE));
19471 token = cp_lexer_peek_token (parser->lexer);
19475 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19477 return objc_build_selector_expr (sel_seq);
19480 /* Parse a list of identifiers.
19482 objc-identifier-list:
19484 objc-identifier-list , identifier
19486 Returns a TREE_LIST of identifier nodes. */
19489 cp_parser_objc_identifier_list (cp_parser* parser)
19491 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19492 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19494 while (sep->type == CPP_COMMA)
19496 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19497 list = chainon (list,
19498 build_tree_list (NULL_TREE,
19499 cp_parser_identifier (parser)));
19500 sep = cp_lexer_peek_token (parser->lexer);
19506 /* Parse an Objective-C alias declaration.
19508 objc-alias-declaration:
19509 @compatibility_alias identifier identifier ;
19511 This function registers the alias mapping with the Objective-C front end.
19512 It returns nothing. */
19515 cp_parser_objc_alias_declaration (cp_parser* parser)
19519 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19520 alias = cp_parser_identifier (parser);
19521 orig = cp_parser_identifier (parser);
19522 objc_declare_alias (alias, orig);
19523 cp_parser_consume_semicolon_at_end_of_statement (parser);
19526 /* Parse an Objective-C class forward-declaration.
19528 objc-class-declaration:
19529 @class objc-identifier-list ;
19531 The function registers the forward declarations with the Objective-C
19532 front end. It returns nothing. */
19535 cp_parser_objc_class_declaration (cp_parser* parser)
19537 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19538 objc_declare_class (cp_parser_objc_identifier_list (parser));
19539 cp_parser_consume_semicolon_at_end_of_statement (parser);
19542 /* Parse a list of Objective-C protocol references.
19544 objc-protocol-refs-opt:
19545 objc-protocol-refs [opt]
19547 objc-protocol-refs:
19548 < objc-identifier-list >
19550 Returns a TREE_LIST of identifiers, if any. */
19553 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19555 tree protorefs = NULL_TREE;
19557 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19559 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19560 protorefs = cp_parser_objc_identifier_list (parser);
19561 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19567 /* Parse a Objective-C visibility specification. */
19570 cp_parser_objc_visibility_spec (cp_parser* parser)
19572 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19574 switch (vis->keyword)
19576 case RID_AT_PRIVATE:
19577 objc_set_visibility (2);
19579 case RID_AT_PROTECTED:
19580 objc_set_visibility (0);
19582 case RID_AT_PUBLIC:
19583 objc_set_visibility (1);
19589 /* Eat '@private'/'@protected'/'@public'. */
19590 cp_lexer_consume_token (parser->lexer);
19593 /* Parse an Objective-C method type. */
19596 cp_parser_objc_method_type (cp_parser* parser)
19598 objc_set_method_type
19599 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19604 /* Parse an Objective-C protocol qualifier. */
19607 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19609 tree quals = NULL_TREE, node;
19610 cp_token *token = cp_lexer_peek_token (parser->lexer);
19612 node = token->u.value;
19614 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19615 && (node == ridpointers [(int) RID_IN]
19616 || node == ridpointers [(int) RID_OUT]
19617 || node == ridpointers [(int) RID_INOUT]
19618 || node == ridpointers [(int) RID_BYCOPY]
19619 || node == ridpointers [(int) RID_BYREF]
19620 || node == ridpointers [(int) RID_ONEWAY]))
19622 quals = tree_cons (NULL_TREE, node, quals);
19623 cp_lexer_consume_token (parser->lexer);
19624 token = cp_lexer_peek_token (parser->lexer);
19625 node = token->u.value;
19631 /* Parse an Objective-C typename. */
19634 cp_parser_objc_typename (cp_parser* parser)
19636 tree type_name = NULL_TREE;
19638 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19640 tree proto_quals, cp_type = NULL_TREE;
19642 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19643 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19645 /* An ObjC type name may consist of just protocol qualifiers, in which
19646 case the type shall default to 'id'. */
19647 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19648 cp_type = cp_parser_type_id (parser);
19650 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19651 type_name = build_tree_list (proto_quals, cp_type);
19657 /* Check to see if TYPE refers to an Objective-C selector name. */
19660 cp_parser_objc_selector_p (enum cpp_ttype type)
19662 return (type == CPP_NAME || type == CPP_KEYWORD
19663 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19664 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19665 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19666 || type == CPP_XOR || type == CPP_XOR_EQ);
19669 /* Parse an Objective-C selector. */
19672 cp_parser_objc_selector (cp_parser* parser)
19674 cp_token *token = cp_lexer_consume_token (parser->lexer);
19676 if (!cp_parser_objc_selector_p (token->type))
19678 error ("%Hinvalid Objective-C++ selector name", &token->location);
19679 return error_mark_node;
19682 /* C++ operator names are allowed to appear in ObjC selectors. */
19683 switch (token->type)
19685 case CPP_AND_AND: return get_identifier ("and");
19686 case CPP_AND_EQ: return get_identifier ("and_eq");
19687 case CPP_AND: return get_identifier ("bitand");
19688 case CPP_OR: return get_identifier ("bitor");
19689 case CPP_COMPL: return get_identifier ("compl");
19690 case CPP_NOT: return get_identifier ("not");
19691 case CPP_NOT_EQ: return get_identifier ("not_eq");
19692 case CPP_OR_OR: return get_identifier ("or");
19693 case CPP_OR_EQ: return get_identifier ("or_eq");
19694 case CPP_XOR: return get_identifier ("xor");
19695 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19696 default: return token->u.value;
19700 /* Parse an Objective-C params list. */
19703 cp_parser_objc_method_keyword_params (cp_parser* parser)
19705 tree params = NULL_TREE;
19706 bool maybe_unary_selector_p = true;
19707 cp_token *token = cp_lexer_peek_token (parser->lexer);
19709 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19711 tree selector = NULL_TREE, type_name, identifier;
19713 if (token->type != CPP_COLON)
19714 selector = cp_parser_objc_selector (parser);
19716 /* Detect if we have a unary selector. */
19717 if (maybe_unary_selector_p
19718 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19721 maybe_unary_selector_p = false;
19722 cp_parser_require (parser, CPP_COLON, "%<:%>");
19723 type_name = cp_parser_objc_typename (parser);
19724 identifier = cp_parser_identifier (parser);
19728 objc_build_keyword_decl (selector,
19732 token = cp_lexer_peek_token (parser->lexer);
19738 /* Parse the non-keyword Objective-C params. */
19741 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19743 tree params = make_node (TREE_LIST);
19744 cp_token *token = cp_lexer_peek_token (parser->lexer);
19745 *ellipsisp = false; /* Initially, assume no ellipsis. */
19747 while (token->type == CPP_COMMA)
19749 cp_parameter_declarator *parmdecl;
19752 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19753 token = cp_lexer_peek_token (parser->lexer);
19755 if (token->type == CPP_ELLIPSIS)
19757 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19762 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19763 parm = grokdeclarator (parmdecl->declarator,
19764 &parmdecl->decl_specifiers,
19765 PARM, /*initialized=*/0,
19766 /*attrlist=*/NULL);
19768 chainon (params, build_tree_list (NULL_TREE, parm));
19769 token = cp_lexer_peek_token (parser->lexer);
19775 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19778 cp_parser_objc_interstitial_code (cp_parser* parser)
19780 cp_token *token = cp_lexer_peek_token (parser->lexer);
19782 /* If the next token is `extern' and the following token is a string
19783 literal, then we have a linkage specification. */
19784 if (token->keyword == RID_EXTERN
19785 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19786 cp_parser_linkage_specification (parser);
19787 /* Handle #pragma, if any. */
19788 else if (token->type == CPP_PRAGMA)
19789 cp_parser_pragma (parser, pragma_external);
19790 /* Allow stray semicolons. */
19791 else if (token->type == CPP_SEMICOLON)
19792 cp_lexer_consume_token (parser->lexer);
19793 /* Finally, try to parse a block-declaration, or a function-definition. */
19795 cp_parser_block_declaration (parser, /*statement_p=*/false);
19798 /* Parse a method signature. */
19801 cp_parser_objc_method_signature (cp_parser* parser)
19803 tree rettype, kwdparms, optparms;
19804 bool ellipsis = false;
19806 cp_parser_objc_method_type (parser);
19807 rettype = cp_parser_objc_typename (parser);
19808 kwdparms = cp_parser_objc_method_keyword_params (parser);
19809 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19811 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19814 /* Pars an Objective-C method prototype list. */
19817 cp_parser_objc_method_prototype_list (cp_parser* parser)
19819 cp_token *token = cp_lexer_peek_token (parser->lexer);
19821 while (token->keyword != RID_AT_END)
19823 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19825 objc_add_method_declaration
19826 (cp_parser_objc_method_signature (parser));
19827 cp_parser_consume_semicolon_at_end_of_statement (parser);
19830 /* Allow for interspersed non-ObjC++ code. */
19831 cp_parser_objc_interstitial_code (parser);
19833 token = cp_lexer_peek_token (parser->lexer);
19836 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19837 objc_finish_interface ();
19840 /* Parse an Objective-C method definition list. */
19843 cp_parser_objc_method_definition_list (cp_parser* parser)
19845 cp_token *token = cp_lexer_peek_token (parser->lexer);
19847 while (token->keyword != RID_AT_END)
19851 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19853 push_deferring_access_checks (dk_deferred);
19854 objc_start_method_definition
19855 (cp_parser_objc_method_signature (parser));
19857 /* For historical reasons, we accept an optional semicolon. */
19858 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19859 cp_lexer_consume_token (parser->lexer);
19861 perform_deferred_access_checks ();
19862 stop_deferring_access_checks ();
19863 meth = cp_parser_function_definition_after_declarator (parser,
19865 pop_deferring_access_checks ();
19866 objc_finish_method_definition (meth);
19869 /* Allow for interspersed non-ObjC++ code. */
19870 cp_parser_objc_interstitial_code (parser);
19872 token = cp_lexer_peek_token (parser->lexer);
19875 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19876 objc_finish_implementation ();
19879 /* Parse Objective-C ivars. */
19882 cp_parser_objc_class_ivars (cp_parser* parser)
19884 cp_token *token = cp_lexer_peek_token (parser->lexer);
19886 if (token->type != CPP_OPEN_BRACE)
19887 return; /* No ivars specified. */
19889 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19890 token = cp_lexer_peek_token (parser->lexer);
19892 while (token->type != CPP_CLOSE_BRACE)
19894 cp_decl_specifier_seq declspecs;
19895 int decl_class_or_enum_p;
19896 tree prefix_attributes;
19898 cp_parser_objc_visibility_spec (parser);
19900 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19903 cp_parser_decl_specifier_seq (parser,
19904 CP_PARSER_FLAGS_OPTIONAL,
19906 &decl_class_or_enum_p);
19907 prefix_attributes = declspecs.attributes;
19908 declspecs.attributes = NULL_TREE;
19910 /* Keep going until we hit the `;' at the end of the
19912 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19914 tree width = NULL_TREE, attributes, first_attribute, decl;
19915 cp_declarator *declarator = NULL;
19916 int ctor_dtor_or_conv_p;
19918 /* Check for a (possibly unnamed) bitfield declaration. */
19919 token = cp_lexer_peek_token (parser->lexer);
19920 if (token->type == CPP_COLON)
19923 if (token->type == CPP_NAME
19924 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19927 /* Get the name of the bitfield. */
19928 declarator = make_id_declarator (NULL_TREE,
19929 cp_parser_identifier (parser),
19933 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19934 /* Get the width of the bitfield. */
19936 = cp_parser_constant_expression (parser,
19937 /*allow_non_constant=*/false,
19942 /* Parse the declarator. */
19944 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19945 &ctor_dtor_or_conv_p,
19946 /*parenthesized_p=*/NULL,
19947 /*member_p=*/false);
19950 /* Look for attributes that apply to the ivar. */
19951 attributes = cp_parser_attributes_opt (parser);
19952 /* Remember which attributes are prefix attributes and
19954 first_attribute = attributes;
19955 /* Combine the attributes. */
19956 attributes = chainon (prefix_attributes, attributes);
19959 /* Create the bitfield declaration. */
19960 decl = grokbitfield (declarator, &declspecs,
19964 decl = grokfield (declarator, &declspecs,
19965 NULL_TREE, /*init_const_expr_p=*/false,
19966 NULL_TREE, attributes);
19968 /* Add the instance variable. */
19969 objc_add_instance_variable (decl);
19971 /* Reset PREFIX_ATTRIBUTES. */
19972 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19973 attributes = TREE_CHAIN (attributes);
19975 TREE_CHAIN (attributes) = NULL_TREE;
19977 token = cp_lexer_peek_token (parser->lexer);
19979 if (token->type == CPP_COMMA)
19981 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19987 cp_parser_consume_semicolon_at_end_of_statement (parser);
19988 token = cp_lexer_peek_token (parser->lexer);
19991 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19992 /* For historical reasons, we accept an optional semicolon. */
19993 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19994 cp_lexer_consume_token (parser->lexer);
19997 /* Parse an Objective-C protocol declaration. */
20000 cp_parser_objc_protocol_declaration (cp_parser* parser)
20002 tree proto, protorefs;
20005 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20008 tok = cp_lexer_peek_token (parser->lexer);
20009 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
20013 /* See if we have a forward declaration or a definition. */
20014 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20016 /* Try a forward declaration first. */
20017 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20019 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20021 cp_parser_consume_semicolon_at_end_of_statement (parser);
20024 /* Ok, we got a full-fledged definition (or at least should). */
20027 proto = cp_parser_identifier (parser);
20028 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20029 objc_start_protocol (proto, protorefs);
20030 cp_parser_objc_method_prototype_list (parser);
20034 /* Parse an Objective-C superclass or category. */
20037 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20040 cp_token *next = cp_lexer_peek_token (parser->lexer);
20042 *super = *categ = NULL_TREE;
20043 if (next->type == CPP_COLON)
20045 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20046 *super = cp_parser_identifier (parser);
20048 else if (next->type == CPP_OPEN_PAREN)
20050 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20051 *categ = cp_parser_identifier (parser);
20052 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20056 /* Parse an Objective-C class interface. */
20059 cp_parser_objc_class_interface (cp_parser* parser)
20061 tree name, super, categ, protos;
20063 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20064 name = cp_parser_identifier (parser);
20065 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20066 protos = cp_parser_objc_protocol_refs_opt (parser);
20068 /* We have either a class or a category on our hands. */
20070 objc_start_category_interface (name, categ, protos);
20073 objc_start_class_interface (name, super, protos);
20074 /* Handle instance variable declarations, if any. */
20075 cp_parser_objc_class_ivars (parser);
20076 objc_continue_interface ();
20079 cp_parser_objc_method_prototype_list (parser);
20082 /* Parse an Objective-C class implementation. */
20085 cp_parser_objc_class_implementation (cp_parser* parser)
20087 tree name, super, categ;
20089 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20090 name = cp_parser_identifier (parser);
20091 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20093 /* We have either a class or a category on our hands. */
20095 objc_start_category_implementation (name, categ);
20098 objc_start_class_implementation (name, super);
20099 /* Handle instance variable declarations, if any. */
20100 cp_parser_objc_class_ivars (parser);
20101 objc_continue_implementation ();
20104 cp_parser_objc_method_definition_list (parser);
20107 /* Consume the @end token and finish off the implementation. */
20110 cp_parser_objc_end_implementation (cp_parser* parser)
20112 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20113 objc_finish_implementation ();
20116 /* Parse an Objective-C declaration. */
20119 cp_parser_objc_declaration (cp_parser* parser)
20121 /* Try to figure out what kind of declaration is present. */
20122 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20124 switch (kwd->keyword)
20127 cp_parser_objc_alias_declaration (parser);
20130 cp_parser_objc_class_declaration (parser);
20132 case RID_AT_PROTOCOL:
20133 cp_parser_objc_protocol_declaration (parser);
20135 case RID_AT_INTERFACE:
20136 cp_parser_objc_class_interface (parser);
20138 case RID_AT_IMPLEMENTATION:
20139 cp_parser_objc_class_implementation (parser);
20142 cp_parser_objc_end_implementation (parser);
20145 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20146 &kwd->location, kwd->u.value);
20147 cp_parser_skip_to_end_of_block_or_statement (parser);
20151 /* Parse an Objective-C try-catch-finally statement.
20153 objc-try-catch-finally-stmt:
20154 @try compound-statement objc-catch-clause-seq [opt]
20155 objc-finally-clause [opt]
20157 objc-catch-clause-seq:
20158 objc-catch-clause objc-catch-clause-seq [opt]
20161 @catch ( exception-declaration ) compound-statement
20163 objc-finally-clause
20164 @finally compound-statement
20166 Returns NULL_TREE. */
20169 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20170 location_t location;
20173 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20174 location = cp_lexer_peek_token (parser->lexer)->location;
20175 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20176 node, lest it get absorbed into the surrounding block. */
20177 stmt = push_stmt_list ();
20178 cp_parser_compound_statement (parser, NULL, false);
20179 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20181 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20183 cp_parameter_declarator *parmdecl;
20186 cp_lexer_consume_token (parser->lexer);
20187 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20188 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20189 parm = grokdeclarator (parmdecl->declarator,
20190 &parmdecl->decl_specifiers,
20191 PARM, /*initialized=*/0,
20192 /*attrlist=*/NULL);
20193 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20194 objc_begin_catch_clause (parm);
20195 cp_parser_compound_statement (parser, NULL, false);
20196 objc_finish_catch_clause ();
20199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20201 cp_lexer_consume_token (parser->lexer);
20202 location = cp_lexer_peek_token (parser->lexer)->location;
20203 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20204 node, lest it get absorbed into the surrounding block. */
20205 stmt = push_stmt_list ();
20206 cp_parser_compound_statement (parser, NULL, false);
20207 objc_build_finally_clause (location, pop_stmt_list (stmt));
20210 return objc_finish_try_stmt ();
20213 /* Parse an Objective-C synchronized statement.
20215 objc-synchronized-stmt:
20216 @synchronized ( expression ) compound-statement
20218 Returns NULL_TREE. */
20221 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20222 location_t location;
20225 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20227 location = cp_lexer_peek_token (parser->lexer)->location;
20228 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20229 lock = cp_parser_expression (parser, false, NULL);
20230 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20232 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20233 node, lest it get absorbed into the surrounding block. */
20234 stmt = push_stmt_list ();
20235 cp_parser_compound_statement (parser, NULL, false);
20237 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20240 /* Parse an Objective-C throw statement.
20243 @throw assignment-expression [opt] ;
20245 Returns a constructed '@throw' statement. */
20248 cp_parser_objc_throw_statement (cp_parser *parser) {
20249 tree expr = NULL_TREE;
20251 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20254 expr = cp_parser_assignment_expression (parser, false, NULL);
20256 cp_parser_consume_semicolon_at_end_of_statement (parser);
20258 return objc_build_throw_stmt (expr);
20261 /* Parse an Objective-C statement. */
20264 cp_parser_objc_statement (cp_parser * parser) {
20265 /* Try to figure out what kind of declaration is present. */
20266 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20268 switch (kwd->keyword)
20271 return cp_parser_objc_try_catch_finally_statement (parser);
20272 case RID_AT_SYNCHRONIZED:
20273 return cp_parser_objc_synchronized_statement (parser);
20275 return cp_parser_objc_throw_statement (parser);
20277 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20278 &kwd->location, kwd->u.value);
20279 cp_parser_skip_to_end_of_block_or_statement (parser);
20282 return error_mark_node;
20285 /* OpenMP 2.5 parsing routines. */
20287 /* Returns name of the next clause.
20288 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20289 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20290 returned and the token is consumed. */
20292 static pragma_omp_clause
20293 cp_parser_omp_clause_name (cp_parser *parser)
20295 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20297 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20298 result = PRAGMA_OMP_CLAUSE_IF;
20299 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20300 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20301 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20302 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20303 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20305 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20306 const char *p = IDENTIFIER_POINTER (id);
20311 if (!strcmp ("collapse", p))
20312 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20313 else if (!strcmp ("copyin", p))
20314 result = PRAGMA_OMP_CLAUSE_COPYIN;
20315 else if (!strcmp ("copyprivate", p))
20316 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20319 if (!strcmp ("firstprivate", p))
20320 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20323 if (!strcmp ("lastprivate", p))
20324 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20327 if (!strcmp ("nowait", p))
20328 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20329 else if (!strcmp ("num_threads", p))
20330 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20333 if (!strcmp ("ordered", p))
20334 result = PRAGMA_OMP_CLAUSE_ORDERED;
20337 if (!strcmp ("reduction", p))
20338 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20341 if (!strcmp ("schedule", p))
20342 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20343 else if (!strcmp ("shared", p))
20344 result = PRAGMA_OMP_CLAUSE_SHARED;
20347 if (!strcmp ("untied", p))
20348 result = PRAGMA_OMP_CLAUSE_UNTIED;
20353 if (result != PRAGMA_OMP_CLAUSE_NONE)
20354 cp_lexer_consume_token (parser->lexer);
20359 /* Validate that a clause of the given type does not already exist. */
20362 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20363 const char *name, location_t location)
20367 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20368 if (OMP_CLAUSE_CODE (c) == code)
20370 error ("%Htoo many %qs clauses", &location, name);
20378 variable-list , identifier
20380 In addition, we match a closing parenthesis. An opening parenthesis
20381 will have been consumed by the caller.
20383 If KIND is nonzero, create the appropriate node and install the decl
20384 in OMP_CLAUSE_DECL and add the node to the head of the list.
20386 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20387 return the list created. */
20390 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20398 token = cp_lexer_peek_token (parser->lexer);
20399 name = cp_parser_id_expression (parser, /*template_p=*/false,
20400 /*check_dependency_p=*/true,
20401 /*template_p=*/NULL,
20402 /*declarator_p=*/false,
20403 /*optional_p=*/false);
20404 if (name == error_mark_node)
20407 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20408 if (decl == error_mark_node)
20409 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20410 else if (kind != 0)
20412 tree u = build_omp_clause (kind);
20413 OMP_CLAUSE_DECL (u) = decl;
20414 OMP_CLAUSE_CHAIN (u) = list;
20418 list = tree_cons (decl, NULL_TREE, list);
20421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20423 cp_lexer_consume_token (parser->lexer);
20426 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20430 /* Try to resync to an unnested comma. Copied from
20431 cp_parser_parenthesized_expression_list. */
20433 ending = cp_parser_skip_to_closing_parenthesis (parser,
20434 /*recovering=*/true,
20436 /*consume_paren=*/true);
20444 /* Similarly, but expect leading and trailing parenthesis. This is a very
20445 common case for omp clauses. */
20448 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20450 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20451 return cp_parser_omp_var_list_no_open (parser, kind, list);
20456 collapse ( constant-expression ) */
20459 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20465 loc = cp_lexer_peek_token (parser->lexer)->location;
20466 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20469 num = cp_parser_constant_expression (parser, false, NULL);
20471 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20472 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20473 /*or_comma=*/false,
20474 /*consume_paren=*/true);
20476 if (num == error_mark_node)
20478 num = fold_non_dependent_expr (num);
20479 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20480 || !host_integerp (num, 0)
20481 || (n = tree_low_cst (num, 0)) <= 0
20484 error ("%Hcollapse argument needs positive constant integer expression",
20489 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20490 c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20491 OMP_CLAUSE_CHAIN (c) = list;
20492 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20498 default ( shared | none ) */
20501 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20503 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20506 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20508 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20510 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20511 const char *p = IDENTIFIER_POINTER (id);
20516 if (strcmp ("none", p) != 0)
20518 kind = OMP_CLAUSE_DEFAULT_NONE;
20522 if (strcmp ("shared", p) != 0)
20524 kind = OMP_CLAUSE_DEFAULT_SHARED;
20531 cp_lexer_consume_token (parser->lexer);
20536 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20539 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20540 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20541 /*or_comma=*/false,
20542 /*consume_paren=*/true);
20544 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20547 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20548 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20549 OMP_CLAUSE_CHAIN (c) = list;
20550 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20556 if ( expression ) */
20559 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20563 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20566 t = cp_parser_condition (parser);
20568 if (t == error_mark_node
20569 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20570 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20571 /*or_comma=*/false,
20572 /*consume_paren=*/true);
20574 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20576 c = build_omp_clause (OMP_CLAUSE_IF);
20577 OMP_CLAUSE_IF_EXPR (c) = t;
20578 OMP_CLAUSE_CHAIN (c) = list;
20587 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20588 tree list, location_t location)
20592 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20594 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20595 OMP_CLAUSE_CHAIN (c) = list;
20600 num_threads ( expression ) */
20603 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20604 location_t location)
20608 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20611 t = cp_parser_expression (parser, false, NULL);
20613 if (t == error_mark_node
20614 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20615 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20616 /*or_comma=*/false,
20617 /*consume_paren=*/true);
20619 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20620 "num_threads", location);
20622 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20623 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20624 OMP_CLAUSE_CHAIN (c) = list;
20633 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20634 tree list, location_t location)
20638 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20639 "ordered", location);
20641 c = build_omp_clause (OMP_CLAUSE_ORDERED);
20642 OMP_CLAUSE_CHAIN (c) = list;
20647 reduction ( reduction-operator : variable-list )
20649 reduction-operator:
20650 One of: + * - & ^ | && || */
20653 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20655 enum tree_code code;
20658 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20661 switch (cp_lexer_peek_token (parser->lexer)->type)
20673 code = BIT_AND_EXPR;
20676 code = BIT_XOR_EXPR;
20679 code = BIT_IOR_EXPR;
20682 code = TRUTH_ANDIF_EXPR;
20685 code = TRUTH_ORIF_EXPR;
20688 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20689 "%<|%>, %<&&%>, or %<||%>");
20691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20692 /*or_comma=*/false,
20693 /*consume_paren=*/true);
20696 cp_lexer_consume_token (parser->lexer);
20698 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20701 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20702 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20703 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20709 schedule ( schedule-kind )
20710 schedule ( schedule-kind , expression )
20713 static | dynamic | guided | runtime | auto */
20716 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20720 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20723 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20725 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20727 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20728 const char *p = IDENTIFIER_POINTER (id);
20733 if (strcmp ("dynamic", p) != 0)
20735 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20739 if (strcmp ("guided", p) != 0)
20741 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20745 if (strcmp ("runtime", p) != 0)
20747 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20754 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20755 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20756 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20757 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20760 cp_lexer_consume_token (parser->lexer);
20762 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20765 cp_lexer_consume_token (parser->lexer);
20767 token = cp_lexer_peek_token (parser->lexer);
20768 t = cp_parser_assignment_expression (parser, false, NULL);
20770 if (t == error_mark_node)
20772 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20773 error ("%Hschedule %<runtime%> does not take "
20774 "a %<chunk_size%> parameter", &token->location);
20775 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20776 error ("%Hschedule %<auto%> does not take "
20777 "a %<chunk_size%> parameter", &token->location);
20779 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20781 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20784 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20787 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20788 OMP_CLAUSE_CHAIN (c) = list;
20792 cp_parser_error (parser, "invalid schedule kind");
20794 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20795 /*or_comma=*/false,
20796 /*consume_paren=*/true);
20804 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20805 tree list, location_t location)
20809 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20811 c = build_omp_clause (OMP_CLAUSE_UNTIED);
20812 OMP_CLAUSE_CHAIN (c) = list;
20816 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20817 is a bitmask in MASK. Return the list of clauses found; the result
20818 of clause default goes in *pdefault. */
20821 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20822 const char *where, cp_token *pragma_tok)
20824 tree clauses = NULL;
20826 cp_token *token = NULL;
20828 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20830 pragma_omp_clause c_kind;
20831 const char *c_name;
20832 tree prev = clauses;
20834 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20835 cp_lexer_consume_token (parser->lexer);
20837 token = cp_lexer_peek_token (parser->lexer);
20838 c_kind = cp_parser_omp_clause_name (parser);
20843 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20844 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20846 c_name = "collapse";
20848 case PRAGMA_OMP_CLAUSE_COPYIN:
20849 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20852 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20853 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20855 c_name = "copyprivate";
20857 case PRAGMA_OMP_CLAUSE_DEFAULT:
20858 clauses = cp_parser_omp_clause_default (parser, clauses,
20860 c_name = "default";
20862 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20863 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20865 c_name = "firstprivate";
20867 case PRAGMA_OMP_CLAUSE_IF:
20868 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20871 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20872 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20874 c_name = "lastprivate";
20876 case PRAGMA_OMP_CLAUSE_NOWAIT:
20877 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20880 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20881 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20883 c_name = "num_threads";
20885 case PRAGMA_OMP_CLAUSE_ORDERED:
20886 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20888 c_name = "ordered";
20890 case PRAGMA_OMP_CLAUSE_PRIVATE:
20891 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20893 c_name = "private";
20895 case PRAGMA_OMP_CLAUSE_REDUCTION:
20896 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20897 c_name = "reduction";
20899 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20900 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20902 c_name = "schedule";
20904 case PRAGMA_OMP_CLAUSE_SHARED:
20905 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20909 case PRAGMA_OMP_CLAUSE_UNTIED:
20910 clauses = cp_parser_omp_clause_untied (parser, clauses,
20915 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20919 if (((mask >> c_kind) & 1) == 0)
20921 /* Remove the invalid clause(s) from the list to avoid
20922 confusing the rest of the compiler. */
20924 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20928 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20929 return finish_omp_clauses (clauses);
20936 In practice, we're also interested in adding the statement to an
20937 outer node. So it is convenient if we work around the fact that
20938 cp_parser_statement calls add_stmt. */
20941 cp_parser_begin_omp_structured_block (cp_parser *parser)
20943 unsigned save = parser->in_statement;
20945 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20946 This preserves the "not within loop or switch" style error messages
20947 for nonsense cases like
20953 if (parser->in_statement)
20954 parser->in_statement = IN_OMP_BLOCK;
20960 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20962 parser->in_statement = save;
20966 cp_parser_omp_structured_block (cp_parser *parser)
20968 tree stmt = begin_omp_structured_block ();
20969 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20971 cp_parser_statement (parser, NULL_TREE, false, NULL);
20973 cp_parser_end_omp_structured_block (parser, save);
20974 return finish_omp_structured_block (stmt);
20978 # pragma omp atomic new-line
20982 x binop= expr | x++ | ++x | x-- | --x
20984 +, *, -, /, &, ^, |, <<, >>
20986 where x is an lvalue expression with scalar type. */
20989 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20992 enum tree_code code;
20994 cp_parser_require_pragma_eol (parser, pragma_tok);
20996 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20997 /*cast_p=*/false, NULL);
20998 switch (TREE_CODE (lhs))
21003 case PREINCREMENT_EXPR:
21004 case POSTINCREMENT_EXPR:
21005 lhs = TREE_OPERAND (lhs, 0);
21007 rhs = integer_one_node;
21010 case PREDECREMENT_EXPR:
21011 case POSTDECREMENT_EXPR:
21012 lhs = TREE_OPERAND (lhs, 0);
21014 rhs = integer_one_node;
21017 case COMPOUND_EXPR:
21018 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
21019 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
21020 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
21021 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
21022 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
21023 (TREE_OPERAND (lhs, 1), 0), 0)))
21025 /* Undo effects of boolean_increment for post {in,de}crement. */
21026 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
21029 if (TREE_CODE (lhs) == MODIFY_EXPR
21030 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
21032 /* Undo effects of boolean_increment. */
21033 if (integer_onep (TREE_OPERAND (lhs, 1)))
21035 /* This is pre or post increment. */
21036 rhs = TREE_OPERAND (lhs, 1);
21037 lhs = TREE_OPERAND (lhs, 0);
21044 switch (cp_lexer_peek_token (parser->lexer)->type)
21050 code = TRUNC_DIV_EXPR;
21058 case CPP_LSHIFT_EQ:
21059 code = LSHIFT_EXPR;
21061 case CPP_RSHIFT_EQ:
21062 code = RSHIFT_EXPR;
21065 code = BIT_AND_EXPR;
21068 code = BIT_IOR_EXPR;
21071 code = BIT_XOR_EXPR;
21074 cp_parser_error (parser,
21075 "invalid operator for %<#pragma omp atomic%>");
21078 cp_lexer_consume_token (parser->lexer);
21080 rhs = cp_parser_expression (parser, false, NULL);
21081 if (rhs == error_mark_node)
21085 finish_omp_atomic (code, lhs, rhs);
21086 cp_parser_consume_semicolon_at_end_of_statement (parser);
21090 cp_parser_skip_to_end_of_block_or_statement (parser);
21095 # pragma omp barrier new-line */
21098 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21100 cp_parser_require_pragma_eol (parser, pragma_tok);
21101 finish_omp_barrier ();
21105 # pragma omp critical [(name)] new-line
21106 structured-block */
21109 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21111 tree stmt, name = NULL;
21113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21115 cp_lexer_consume_token (parser->lexer);
21117 name = cp_parser_identifier (parser);
21119 if (name == error_mark_node
21120 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21121 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21122 /*or_comma=*/false,
21123 /*consume_paren=*/true);
21124 if (name == error_mark_node)
21127 cp_parser_require_pragma_eol (parser, pragma_tok);
21129 stmt = cp_parser_omp_structured_block (parser);
21130 return c_finish_omp_critical (stmt, name);
21134 # pragma omp flush flush-vars[opt] new-line
21137 ( variable-list ) */
21140 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21143 (void) cp_parser_omp_var_list (parser, 0, NULL);
21144 cp_parser_require_pragma_eol (parser, pragma_tok);
21146 finish_omp_flush ();
21149 /* Helper function, to parse omp for increment expression. */
21152 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21154 tree cond = cp_parser_binary_expression (parser, false, true,
21155 PREC_NOT_OPERATOR, NULL);
21158 if (cond == error_mark_node
21159 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21161 cp_parser_skip_to_end_of_statement (parser);
21162 return error_mark_node;
21165 switch (TREE_CODE (cond))
21173 return error_mark_node;
21176 /* If decl is an iterator, preserve LHS and RHS of the relational
21177 expr until finish_omp_for. */
21179 && (type_dependent_expression_p (decl)
21180 || CLASS_TYPE_P (TREE_TYPE (decl))))
21183 return build_x_binary_op (TREE_CODE (cond),
21184 TREE_OPERAND (cond, 0), ERROR_MARK,
21185 TREE_OPERAND (cond, 1), ERROR_MARK,
21186 &overloaded_p, tf_warning_or_error);
21189 /* Helper function, to parse omp for increment expression. */
21192 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21194 cp_token *token = cp_lexer_peek_token (parser->lexer);
21200 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21202 op = (token->type == CPP_PLUS_PLUS
21203 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21204 cp_lexer_consume_token (parser->lexer);
21205 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21207 return error_mark_node;
21208 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21211 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21213 return error_mark_node;
21215 token = cp_lexer_peek_token (parser->lexer);
21216 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21218 op = (token->type == CPP_PLUS_PLUS
21219 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21220 cp_lexer_consume_token (parser->lexer);
21221 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21224 op = cp_parser_assignment_operator_opt (parser);
21225 if (op == ERROR_MARK)
21226 return error_mark_node;
21228 if (op != NOP_EXPR)
21230 rhs = cp_parser_assignment_expression (parser, false, NULL);
21231 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21232 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21235 lhs = cp_parser_binary_expression (parser, false, false,
21236 PREC_ADDITIVE_EXPRESSION, NULL);
21237 token = cp_lexer_peek_token (parser->lexer);
21238 decl_first = lhs == decl;
21241 if (token->type != CPP_PLUS
21242 && token->type != CPP_MINUS)
21243 return error_mark_node;
21247 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21248 cp_lexer_consume_token (parser->lexer);
21249 rhs = cp_parser_binary_expression (parser, false, false,
21250 PREC_ADDITIVE_EXPRESSION, NULL);
21251 token = cp_lexer_peek_token (parser->lexer);
21252 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21254 if (lhs == NULL_TREE)
21256 if (op == PLUS_EXPR)
21259 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21262 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21263 NULL, tf_warning_or_error);
21266 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21270 if (rhs != decl || op == MINUS_EXPR)
21271 return error_mark_node;
21272 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21275 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21277 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21280 /* Parse the restricted form of the for statement allowed by OpenMP. */
21283 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21285 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21286 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21287 tree this_pre_body, cl;
21288 location_t loc_first;
21289 bool collapse_err = false;
21290 int i, collapse = 1, nbraces = 0;
21292 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21293 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21294 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21296 gcc_assert (collapse >= 1);
21298 declv = make_tree_vec (collapse);
21299 initv = make_tree_vec (collapse);
21300 condv = make_tree_vec (collapse);
21301 incrv = make_tree_vec (collapse);
21303 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21305 for (i = 0; i < collapse; i++)
21307 int bracecount = 0;
21308 bool add_private_clause = false;
21311 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21313 cp_parser_error (parser, "for statement expected");
21316 loc = cp_lexer_consume_token (parser->lexer)->location;
21318 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21321 init = decl = real_decl = NULL;
21322 this_pre_body = push_stmt_list ();
21323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21325 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21329 integer-type var = lb
21330 random-access-iterator-type var = lb
21331 pointer-type var = lb
21333 cp_decl_specifier_seq type_specifiers;
21335 /* First, try to parse as an initialized declaration. See
21336 cp_parser_condition, from whence the bulk of this is copied. */
21338 cp_parser_parse_tentatively (parser);
21339 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21340 /*is_trailing_return=*/false,
21342 if (cp_parser_parse_definitely (parser))
21344 /* If parsing a type specifier seq succeeded, then this
21345 MUST be a initialized declaration. */
21346 tree asm_specification, attributes;
21347 cp_declarator *declarator;
21349 declarator = cp_parser_declarator (parser,
21350 CP_PARSER_DECLARATOR_NAMED,
21351 /*ctor_dtor_or_conv_p=*/NULL,
21352 /*parenthesized_p=*/NULL,
21353 /*member_p=*/false);
21354 attributes = cp_parser_attributes_opt (parser);
21355 asm_specification = cp_parser_asm_specification_opt (parser);
21357 if (declarator == cp_error_declarator)
21358 cp_parser_skip_to_end_of_statement (parser);
21362 tree pushed_scope, auto_node;
21364 decl = start_decl (declarator, &type_specifiers,
21365 SD_INITIALIZED, attributes,
21366 /*prefix_attributes=*/NULL_TREE,
21369 auto_node = type_uses_auto (TREE_TYPE (decl));
21370 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21372 if (cp_lexer_next_token_is (parser->lexer,
21374 error ("parenthesized initialization is not allowed in "
21375 "OpenMP %<for%> loop");
21377 /* Trigger an error. */
21378 cp_parser_require (parser, CPP_EQ, "%<=%>");
21380 init = error_mark_node;
21381 cp_parser_skip_to_end_of_statement (parser);
21383 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21384 || type_dependent_expression_p (decl)
21387 bool is_direct_init, is_non_constant_init;
21389 init = cp_parser_initializer (parser,
21391 &is_non_constant_init);
21393 if (auto_node && describable_type (init))
21396 = do_auto_deduction (TREE_TYPE (decl), init,
21399 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21400 && !type_dependent_expression_p (decl))
21404 cp_finish_decl (decl, init, !is_non_constant_init,
21406 LOOKUP_ONLYCONVERTING);
21407 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21410 = tree_cons (NULL, this_pre_body, for_block);
21414 init = pop_stmt_list (this_pre_body);
21415 this_pre_body = NULL_TREE;
21420 cp_lexer_consume_token (parser->lexer);
21421 init = cp_parser_assignment_expression (parser, false, NULL);
21424 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21425 init = error_mark_node;
21427 cp_finish_decl (decl, NULL_TREE,
21428 /*init_const_expr_p=*/false,
21430 LOOKUP_ONLYCONVERTING);
21434 pop_scope (pushed_scope);
21440 /* If parsing a type specifier sequence failed, then
21441 this MUST be a simple expression. */
21442 cp_parser_parse_tentatively (parser);
21443 decl = cp_parser_primary_expression (parser, false, false,
21445 if (!cp_parser_error_occurred (parser)
21448 && CLASS_TYPE_P (TREE_TYPE (decl)))
21452 cp_parser_parse_definitely (parser);
21453 cp_parser_require (parser, CPP_EQ, "%<=%>");
21454 rhs = cp_parser_assignment_expression (parser, false, NULL);
21455 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21457 tf_warning_or_error));
21458 add_private_clause = true;
21463 cp_parser_abort_tentative_parse (parser);
21464 init = cp_parser_expression (parser, false, NULL);
21467 if (TREE_CODE (init) == MODIFY_EXPR
21468 || TREE_CODE (init) == MODOP_EXPR)
21469 real_decl = TREE_OPERAND (init, 0);
21474 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21477 this_pre_body = pop_stmt_list (this_pre_body);
21481 pre_body = push_stmt_list ();
21483 add_stmt (this_pre_body);
21484 pre_body = pop_stmt_list (pre_body);
21487 pre_body = this_pre_body;
21492 if (par_clauses != NULL && real_decl != NULL_TREE)
21495 for (c = par_clauses; *c ; )
21496 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21497 && OMP_CLAUSE_DECL (*c) == real_decl)
21499 error ("%Hiteration variable %qD should not be firstprivate",
21501 *c = OMP_CLAUSE_CHAIN (*c);
21503 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21504 && OMP_CLAUSE_DECL (*c) == real_decl)
21506 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21507 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21508 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21509 OMP_CLAUSE_DECL (l) = real_decl;
21510 OMP_CLAUSE_CHAIN (l) = clauses;
21511 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21513 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21514 CP_OMP_CLAUSE_INFO (*c) = NULL;
21515 add_private_clause = false;
21519 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21520 && OMP_CLAUSE_DECL (*c) == real_decl)
21521 add_private_clause = false;
21522 c = &OMP_CLAUSE_CHAIN (*c);
21526 if (add_private_clause)
21529 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21531 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21532 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21533 && OMP_CLAUSE_DECL (c) == decl)
21535 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21536 && OMP_CLAUSE_DECL (c) == decl)
21537 error ("%Hiteration variable %qD should not be firstprivate",
21539 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21540 && OMP_CLAUSE_DECL (c) == decl)
21541 error ("%Hiteration variable %qD should not be reduction",
21546 c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21547 OMP_CLAUSE_DECL (c) = decl;
21548 c = finish_omp_clauses (c);
21551 OMP_CLAUSE_CHAIN (c) = clauses;
21558 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21559 cond = cp_parser_omp_for_cond (parser, decl);
21560 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21565 /* If decl is an iterator, preserve the operator on decl
21566 until finish_omp_for. */
21568 && (type_dependent_expression_p (decl)
21569 || CLASS_TYPE_P (TREE_TYPE (decl))))
21570 incr = cp_parser_omp_for_incr (parser, decl);
21572 incr = cp_parser_expression (parser, false, NULL);
21575 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21576 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21577 /*or_comma=*/false,
21578 /*consume_paren=*/true);
21580 TREE_VEC_ELT (declv, i) = decl;
21581 TREE_VEC_ELT (initv, i) = init;
21582 TREE_VEC_ELT (condv, i) = cond;
21583 TREE_VEC_ELT (incrv, i) = incr;
21585 if (i == collapse - 1)
21588 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21589 in between the collapsed for loops to be still considered perfectly
21590 nested. Hopefully the final version clarifies this.
21591 For now handle (multiple) {'s and empty statements. */
21592 cp_parser_parse_tentatively (parser);
21595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21597 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21599 cp_lexer_consume_token (parser->lexer);
21602 else if (bracecount
21603 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21604 cp_lexer_consume_token (parser->lexer);
21607 loc = cp_lexer_peek_token (parser->lexer)->location;
21608 error ("%Hnot enough collapsed for loops", &loc);
21609 collapse_err = true;
21610 cp_parser_abort_tentative_parse (parser);
21619 cp_parser_parse_definitely (parser);
21620 nbraces += bracecount;
21624 /* Note that we saved the original contents of this flag when we entered
21625 the structured block, and so we don't need to re-save it here. */
21626 parser->in_statement = IN_OMP_FOR;
21628 /* Note that the grammar doesn't call for a structured block here,
21629 though the loop as a whole is a structured block. */
21630 body = push_stmt_list ();
21631 cp_parser_statement (parser, NULL_TREE, false, NULL);
21632 body = pop_stmt_list (body);
21634 if (declv == NULL_TREE)
21637 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21638 pre_body, clauses);
21642 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21644 cp_lexer_consume_token (parser->lexer);
21647 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21648 cp_lexer_consume_token (parser->lexer);
21653 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21654 error ("%Hcollapsed loops not perfectly nested", &loc);
21656 collapse_err = true;
21657 cp_parser_statement_seq_opt (parser, NULL);
21658 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21665 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21666 for_block = TREE_CHAIN (for_block);
21673 #pragma omp for for-clause[optseq] new-line
21676 #define OMP_FOR_CLAUSE_MASK \
21677 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21678 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21679 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21680 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21681 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21682 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21683 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21684 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21687 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21689 tree clauses, sb, ret;
21692 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21693 "#pragma omp for", pragma_tok);
21695 sb = begin_omp_structured_block ();
21696 save = cp_parser_begin_omp_structured_block (parser);
21698 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21700 cp_parser_end_omp_structured_block (parser, save);
21701 add_stmt (finish_omp_structured_block (sb));
21707 # pragma omp master new-line
21708 structured-block */
21711 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21713 cp_parser_require_pragma_eol (parser, pragma_tok);
21714 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21718 # pragma omp ordered new-line
21719 structured-block */
21722 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21724 cp_parser_require_pragma_eol (parser, pragma_tok);
21725 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21731 { section-sequence }
21734 section-directive[opt] structured-block
21735 section-sequence section-directive structured-block */
21738 cp_parser_omp_sections_scope (cp_parser *parser)
21740 tree stmt, substmt;
21741 bool error_suppress = false;
21744 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21747 stmt = push_stmt_list ();
21749 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21753 substmt = begin_omp_structured_block ();
21754 save = cp_parser_begin_omp_structured_block (parser);
21758 cp_parser_statement (parser, NULL_TREE, false, NULL);
21760 tok = cp_lexer_peek_token (parser->lexer);
21761 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21763 if (tok->type == CPP_CLOSE_BRACE)
21765 if (tok->type == CPP_EOF)
21769 cp_parser_end_omp_structured_block (parser, save);
21770 substmt = finish_omp_structured_block (substmt);
21771 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21772 add_stmt (substmt);
21777 tok = cp_lexer_peek_token (parser->lexer);
21778 if (tok->type == CPP_CLOSE_BRACE)
21780 if (tok->type == CPP_EOF)
21783 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21785 cp_lexer_consume_token (parser->lexer);
21786 cp_parser_require_pragma_eol (parser, tok);
21787 error_suppress = false;
21789 else if (!error_suppress)
21791 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21792 error_suppress = true;
21795 substmt = cp_parser_omp_structured_block (parser);
21796 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21797 add_stmt (substmt);
21799 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21801 substmt = pop_stmt_list (stmt);
21803 stmt = make_node (OMP_SECTIONS);
21804 TREE_TYPE (stmt) = void_type_node;
21805 OMP_SECTIONS_BODY (stmt) = substmt;
21812 # pragma omp sections sections-clause[optseq] newline
21815 #define OMP_SECTIONS_CLAUSE_MASK \
21816 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21817 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21818 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21819 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21820 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21823 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21827 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21828 "#pragma omp sections", pragma_tok);
21830 ret = cp_parser_omp_sections_scope (parser);
21832 OMP_SECTIONS_CLAUSES (ret) = clauses;
21838 # pragma parallel parallel-clause new-line
21839 # pragma parallel for parallel-for-clause new-line
21840 # pragma parallel sections parallel-sections-clause new-line */
21842 #define OMP_PARALLEL_CLAUSE_MASK \
21843 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21844 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21845 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21846 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21847 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21848 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21849 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21850 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21853 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21855 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21856 const char *p_name = "#pragma omp parallel";
21857 tree stmt, clauses, par_clause, ws_clause, block;
21858 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21863 cp_lexer_consume_token (parser->lexer);
21864 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21865 p_name = "#pragma omp parallel for";
21866 mask |= OMP_FOR_CLAUSE_MASK;
21867 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21869 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21871 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21872 const char *p = IDENTIFIER_POINTER (id);
21873 if (strcmp (p, "sections") == 0)
21875 cp_lexer_consume_token (parser->lexer);
21876 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21877 p_name = "#pragma omp parallel sections";
21878 mask |= OMP_SECTIONS_CLAUSE_MASK;
21879 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21883 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21884 block = begin_omp_parallel ();
21885 save = cp_parser_begin_omp_structured_block (parser);
21889 case PRAGMA_OMP_PARALLEL:
21890 cp_parser_statement (parser, NULL_TREE, false, NULL);
21891 par_clause = clauses;
21894 case PRAGMA_OMP_PARALLEL_FOR:
21895 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21896 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21899 case PRAGMA_OMP_PARALLEL_SECTIONS:
21900 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21901 stmt = cp_parser_omp_sections_scope (parser);
21903 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21907 gcc_unreachable ();
21910 cp_parser_end_omp_structured_block (parser, save);
21911 stmt = finish_omp_parallel (par_clause, block);
21912 if (p_kind != PRAGMA_OMP_PARALLEL)
21913 OMP_PARALLEL_COMBINED (stmt) = 1;
21918 # pragma omp single single-clause[optseq] new-line
21919 structured-block */
21921 #define OMP_SINGLE_CLAUSE_MASK \
21922 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21923 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21924 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21925 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21928 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21930 tree stmt = make_node (OMP_SINGLE);
21931 TREE_TYPE (stmt) = void_type_node;
21933 OMP_SINGLE_CLAUSES (stmt)
21934 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21935 "#pragma omp single", pragma_tok);
21936 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21938 return add_stmt (stmt);
21942 # pragma omp task task-clause[optseq] new-line
21943 structured-block */
21945 #define OMP_TASK_CLAUSE_MASK \
21946 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21947 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21948 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21949 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21950 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21951 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21954 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21956 tree clauses, block;
21959 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21960 "#pragma omp task", pragma_tok);
21961 block = begin_omp_task ();
21962 save = cp_parser_begin_omp_structured_block (parser);
21963 cp_parser_statement (parser, NULL_TREE, false, NULL);
21964 cp_parser_end_omp_structured_block (parser, save);
21965 return finish_omp_task (clauses, block);
21969 # pragma omp taskwait new-line */
21972 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21974 cp_parser_require_pragma_eol (parser, pragma_tok);
21975 finish_omp_taskwait ();
21979 # pragma omp threadprivate (variable-list) */
21982 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21986 vars = cp_parser_omp_var_list (parser, 0, NULL);
21987 cp_parser_require_pragma_eol (parser, pragma_tok);
21989 finish_omp_threadprivate (vars);
21992 /* Main entry point to OpenMP statement pragmas. */
21995 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21999 switch (pragma_tok->pragma_kind)
22001 case PRAGMA_OMP_ATOMIC:
22002 cp_parser_omp_atomic (parser, pragma_tok);
22004 case PRAGMA_OMP_CRITICAL:
22005 stmt = cp_parser_omp_critical (parser, pragma_tok);
22007 case PRAGMA_OMP_FOR:
22008 stmt = cp_parser_omp_for (parser, pragma_tok);
22010 case PRAGMA_OMP_MASTER:
22011 stmt = cp_parser_omp_master (parser, pragma_tok);
22013 case PRAGMA_OMP_ORDERED:
22014 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22016 case PRAGMA_OMP_PARALLEL:
22017 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22019 case PRAGMA_OMP_SECTIONS:
22020 stmt = cp_parser_omp_sections (parser, pragma_tok);
22022 case PRAGMA_OMP_SINGLE:
22023 stmt = cp_parser_omp_single (parser, pragma_tok);
22025 case PRAGMA_OMP_TASK:
22026 stmt = cp_parser_omp_task (parser, pragma_tok);
22029 gcc_unreachable ();
22033 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22038 static GTY (()) cp_parser *the_parser;
22041 /* Special handling for the first token or line in the file. The first
22042 thing in the file might be #pragma GCC pch_preprocess, which loads a
22043 PCH file, which is a GC collection point. So we need to handle this
22044 first pragma without benefit of an existing lexer structure.
22046 Always returns one token to the caller in *FIRST_TOKEN. This is
22047 either the true first token of the file, or the first token after
22048 the initial pragma. */
22051 cp_parser_initial_pragma (cp_token *first_token)
22055 cp_lexer_get_preprocessor_token (NULL, first_token);
22056 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22059 cp_lexer_get_preprocessor_token (NULL, first_token);
22060 if (first_token->type == CPP_STRING)
22062 name = first_token->u.value;
22064 cp_lexer_get_preprocessor_token (NULL, first_token);
22065 if (first_token->type != CPP_PRAGMA_EOL)
22066 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22067 &first_token->location);
22070 error ("%Hexpected string literal", &first_token->location);
22072 /* Skip to the end of the pragma. */
22073 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22074 cp_lexer_get_preprocessor_token (NULL, first_token);
22076 /* Now actually load the PCH file. */
22078 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22080 /* Read one more token to return to our caller. We have to do this
22081 after reading the PCH file in, since its pointers have to be
22083 cp_lexer_get_preprocessor_token (NULL, first_token);
22086 /* Normal parsing of a pragma token. Here we can (and must) use the
22090 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22092 cp_token *pragma_tok;
22095 pragma_tok = cp_lexer_consume_token (parser->lexer);
22096 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22097 parser->lexer->in_pragma = true;
22099 id = pragma_tok->pragma_kind;
22102 case PRAGMA_GCC_PCH_PREPROCESS:
22103 error ("%H%<#pragma GCC pch_preprocess%> must be first",
22104 &pragma_tok->location);
22107 case PRAGMA_OMP_BARRIER:
22110 case pragma_compound:
22111 cp_parser_omp_barrier (parser, pragma_tok);
22114 error ("%H%<#pragma omp barrier%> may only be "
22115 "used in compound statements", &pragma_tok->location);
22122 case PRAGMA_OMP_FLUSH:
22125 case pragma_compound:
22126 cp_parser_omp_flush (parser, pragma_tok);
22129 error ("%H%<#pragma omp flush%> may only be "
22130 "used in compound statements", &pragma_tok->location);
22137 case PRAGMA_OMP_TASKWAIT:
22140 case pragma_compound:
22141 cp_parser_omp_taskwait (parser, pragma_tok);
22144 error ("%H%<#pragma omp taskwait%> may only be "
22145 "used in compound statements",
22146 &pragma_tok->location);
22153 case PRAGMA_OMP_THREADPRIVATE:
22154 cp_parser_omp_threadprivate (parser, pragma_tok);
22157 case PRAGMA_OMP_ATOMIC:
22158 case PRAGMA_OMP_CRITICAL:
22159 case PRAGMA_OMP_FOR:
22160 case PRAGMA_OMP_MASTER:
22161 case PRAGMA_OMP_ORDERED:
22162 case PRAGMA_OMP_PARALLEL:
22163 case PRAGMA_OMP_SECTIONS:
22164 case PRAGMA_OMP_SINGLE:
22165 case PRAGMA_OMP_TASK:
22166 if (context == pragma_external)
22168 cp_parser_omp_construct (parser, pragma_tok);
22171 case PRAGMA_OMP_SECTION:
22172 error ("%H%<#pragma omp section%> may only be used in "
22173 "%<#pragma omp sections%> construct", &pragma_tok->location);
22177 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22178 c_invoke_pragma_handler (id);
22182 cp_parser_error (parser, "expected declaration specifiers");
22186 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22190 /* The interface the pragma parsers have to the lexer. */
22193 pragma_lex (tree *value)
22196 enum cpp_ttype ret;
22198 tok = cp_lexer_peek_token (the_parser->lexer);
22201 *value = tok->u.value;
22203 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22205 else if (ret == CPP_STRING)
22206 *value = cp_parser_string_literal (the_parser, false, false);
22209 cp_lexer_consume_token (the_parser->lexer);
22210 if (ret == CPP_KEYWORD)
22218 /* External interface. */
22220 /* Parse one entire translation unit. */
22223 c_parse_file (void)
22225 bool error_occurred;
22226 static bool already_called = false;
22228 if (already_called)
22230 sorry ("inter-module optimizations not implemented for C++");
22233 already_called = true;
22235 the_parser = cp_parser_new ();
22236 push_deferring_access_checks (flag_access_control
22237 ? dk_no_deferred : dk_no_check);
22238 error_occurred = cp_parser_translation_unit (the_parser);
22242 #include "gt-cp-parser.h"