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"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct GTY(()) tree_check {
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 GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
154 static cp_lexer *cp_lexer_new_main
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
160 static int cp_lexer_saving_tokens
162 static cp_token_position cp_lexer_token_position
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
180 static void cp_lexer_purge_token
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
186 static void cp_lexer_commit_tokens
188 static void cp_lexer_rollback_tokens
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
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_STRING_NO_JOIN);
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);
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432 /* Warn about the C++0x keyword (but still treat it as
434 warning (OPT_Wc__0x_compat,
435 "identifier %qE will become a keyword in C++0x",
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token->u.value, RID_MAX);
443 token->ambiguous_p = false;
444 token->keyword = RID_MAX;
447 /* Handle Objective-C++ keywords. */
448 else if (token->type == CPP_AT_NAME)
450 token->type = CPP_KEYWORD;
451 switch (C_RID_CODE (token->u.value))
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458 case RID_THROW: token->keyword = RID_AT_THROW; break;
459 case RID_TRY: token->keyword = RID_AT_TRY; break;
460 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461 default: token->keyword = C_RID_CODE (token->u.value);
464 else if (token->type == CPP_PRAGMA)
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token->pragma_kind = ((enum pragma_kind)
468 TREE_INT_CST_LOW (token->u.value));
469 token->u.value = NULL_TREE;
473 /* Update the globals input_location and the input file stack from TOKEN. */
475 cp_lexer_set_source_position_from_token (cp_token *token)
477 if (token->type != CPP_EOF)
479 input_location = token->location;
483 /* Return a pointer to the next token in the token stream, but do not
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
489 if (cp_lexer_debugging_p (lexer))
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493 putc ('\n', cp_lexer_debug_stream);
495 return lexer->next_token;
498 /* Return true if the next token has the indicated TYPE. */
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 return cp_lexer_peek_token (lexer)->type == type;
506 /* Return true if the next token does not have the indicated TYPE. */
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 return !cp_lexer_next_token_is (lexer, type);
514 /* Return true if the next token is the indicated KEYWORD. */
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 return cp_lexer_peek_token (lexer)->keyword == keyword;
522 /* Return true if the next token is not the indicated KEYWORD. */
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 return cp_lexer_peek_token (lexer)->keyword != keyword;
530 /* Return true if the next token is a keyword for a decl-specifier. */
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 token = cp_lexer_peek_token (lexer);
538 switch (token->keyword)
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
543 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
569 /* GNU extensions. */
572 /* C++0x extensions. */
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 /* N is 1-based, not zero-based. */
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
605 if (token == lexer->last_token)
611 if (token->type != CPP_PURGED)
615 if (cp_lexer_debugging_p (lexer))
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
628 cp_lexer_consume_token (cp_lexer* lexer)
630 cp_token *token = lexer->next_token;
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 if (lexer->next_token == lexer->last_token)
640 lexer->next_token = &eof_token;
645 while (lexer->next_token->type == CPP_PURGED);
647 cp_lexer_set_source_position_from_token (token);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
665 cp_lexer_purge_token (cp_lexer *lexer)
667 cp_token *tok = lexer->next_token;
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
678 if (tok == lexer->last_token)
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 cp_token *peek = lexer->next_token;
697 if (peek == &eof_token)
698 peek = lexer->last_token;
700 gcc_assert (tok < peek);
702 for ( tok += 1; tok != peek; tok += 1)
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
711 /* Begin saving tokens. All tokens consumed after this point will be
715 cp_lexer_save_tokens (cp_lexer* lexer)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
725 /* Commit to the portion of the token stream most recently saved. */
728 cp_lexer_commit_tokens (cp_lexer* lexer)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position, lexer->saved_tokens);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
755 cp_lexer_print_token (FILE * stream, cp_token *token)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
766 /* C++ parser token types - see "Manifest constants", above. */
769 "NESTED_NAME_SPECIFIER",
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
778 /* For some tokens, print the associated data. */
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 /* else fall through */
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
796 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
804 /* Start emitting debugging information. */
807 cp_lexer_start_debugging (cp_lexer* lexer)
809 lexer->debugging_p = true;
812 /* Stop emitting debugging information. */
815 cp_lexer_stop_debugging (cp_lexer* lexer)
817 lexer->debugging_p = false;
820 #endif /* ENABLE_CHECKING */
822 /* Create a new cp_token_cache, representing a range of tokens. */
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
827 cp_token_cache *cache = GGC_NEW (cp_token_cache);
828 cache->first = first;
834 /* Decl-specifiers. */
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
841 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 /* Nothing other than the parser should be creating declarators;
847 declarators are a semi-syntactic representation of C++ entities.
848 Other parts of the front end that need to create entities (like
849 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
851 static cp_declarator *make_call_declarator
852 (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854 (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856 (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858 (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860 (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862 (cp_cv_quals, tree, cp_declarator *);
864 /* An erroneous declarator. */
865 static cp_declarator *cp_error_declarator;
867 /* The obstack on which declarators and related data structures are
869 static struct obstack declarator_obstack;
871 /* Alloc BYTES from the declarator memory pool. */
874 alloc_declarator (size_t bytes)
876 return obstack_alloc (&declarator_obstack, bytes);
879 /* Allocate a declarator of the indicated KIND. Clear fields that are
880 common to all declarators. */
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
885 cp_declarator *declarator;
887 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888 declarator->kind = kind;
889 declarator->attributes = NULL_TREE;
890 declarator->declarator = NULL;
891 declarator->parameter_pack_p = false;
896 /* Make a declarator for a generalized identifier. If
897 QUALIFYING_SCOPE is non-NULL, the identifier is
898 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
899 UNQUALIFIED_NAME. SFK indicates the kind of special function this
902 static cp_declarator *
903 make_id_declarator (tree qualifying_scope, tree unqualified_name,
904 special_function_kind sfk)
906 cp_declarator *declarator;
908 /* It is valid to write:
910 class C { void f(); };
914 The standard is not clear about whether `typedef const C D' is
915 legal; as of 2002-09-15 the committee is considering that
916 question. EDG 3.0 allows that syntax. Therefore, we do as
918 if (qualifying_scope && TYPE_P (qualifying_scope))
919 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
921 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
922 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
923 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
925 declarator = make_declarator (cdk_id);
926 declarator->u.id.qualifying_scope = qualifying_scope;
927 declarator->u.id.unqualified_name = unqualified_name;
928 declarator->u.id.sfk = sfk;
933 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
934 of modifiers such as const or volatile to apply to the pointer
935 type, represented as identifiers. */
938 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
940 cp_declarator *declarator;
942 declarator = make_declarator (cdk_pointer);
943 declarator->declarator = target;
944 declarator->u.pointer.qualifiers = cv_qualifiers;
945 declarator->u.pointer.class_type = NULL_TREE;
948 declarator->parameter_pack_p = target->parameter_pack_p;
949 target->parameter_pack_p = false;
952 declarator->parameter_pack_p = false;
957 /* Like make_pointer_declarator -- but for references. */
960 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
963 cp_declarator *declarator;
965 declarator = make_declarator (cdk_reference);
966 declarator->declarator = target;
967 declarator->u.reference.qualifiers = cv_qualifiers;
968 declarator->u.reference.rvalue_ref = rvalue_ref;
971 declarator->parameter_pack_p = target->parameter_pack_p;
972 target->parameter_pack_p = false;
975 declarator->parameter_pack_p = false;
980 /* Like make_pointer_declarator -- but for a pointer to a non-static
981 member of CLASS_TYPE. */
984 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
985 cp_declarator *pointee)
987 cp_declarator *declarator;
989 declarator = make_declarator (cdk_ptrmem);
990 declarator->declarator = pointee;
991 declarator->u.pointer.qualifiers = cv_qualifiers;
992 declarator->u.pointer.class_type = class_type;
996 declarator->parameter_pack_p = pointee->parameter_pack_p;
997 pointee->parameter_pack_p = false;
1000 declarator->parameter_pack_p = false;
1005 /* Make a declarator for the function given by TARGET, with the
1006 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1007 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1008 indicates what exceptions can be thrown. */
1011 make_call_declarator (cp_declarator *target,
1013 cp_cv_quals cv_qualifiers,
1014 tree exception_specification,
1015 tree late_return_type)
1017 cp_declarator *declarator;
1019 declarator = make_declarator (cdk_function);
1020 declarator->declarator = target;
1021 declarator->u.function.parameters = parms;
1022 declarator->u.function.qualifiers = cv_qualifiers;
1023 declarator->u.function.exception_specification = exception_specification;
1024 declarator->u.function.late_return_type = late_return_type;
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1031 declarator->parameter_pack_p = false;
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1040 make_array_declarator (cp_declarator *element, tree bounds)
1042 cp_declarator *declarator;
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1053 declarator->parameter_pack_p = false;
1058 /* Determine whether the declarator we've seen so far can be a
1059 parameter pack, when followed by an ellipsis. */
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1063 /* Search for a declarator name, or any other declarator that goes
1064 after the point where the ellipsis could appear in a parameter
1065 pack. If we find any of these, then this declarator can not be
1066 made into a parameter pack. */
1068 while (declarator && !found)
1070 switch ((int)declarator->kind)
1081 declarator = declarator->declarator;
1089 cp_parameter_declarator *no_parameters;
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092 DECLARATOR and DEFAULT_ARGUMENT. */
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096 cp_declarator *declarator,
1097 tree default_argument)
1099 cp_parameter_declarator *parameter;
1101 parameter = ((cp_parameter_declarator *)
1102 alloc_declarator (sizeof (cp_parameter_declarator)));
1103 parameter->next = NULL;
1104 if (decl_specifiers)
1105 parameter->decl_specifiers = *decl_specifiers;
1107 clear_decl_specs (¶meter->decl_specifiers);
1108 parameter->declarator = declarator;
1109 parameter->default_argument = default_argument;
1110 parameter->ellipsis_p = false;
1115 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 function_declarator_p (const cp_declarator *declarator)
1122 if (declarator->kind == cdk_function
1123 && declarator->declarator->kind == cdk_id)
1125 if (declarator->kind == cdk_id
1126 || declarator->kind == cdk_error)
1128 declarator = declarator->declarator;
1138 A cp_parser parses the token stream as specified by the C++
1139 grammar. Its job is purely parsing, not semantic analysis. For
1140 example, the parser breaks the token stream into declarators,
1141 expressions, statements, and other similar syntactic constructs.
1142 It does not check that the types of the expressions on either side
1143 of an assignment-statement are compatible, or that a function is
1144 not declared with a parameter of type `void'.
1146 The parser invokes routines elsewhere in the compiler to perform
1147 semantic analysis and to build up the abstract syntax tree for the
1150 The parser (and the template instantiation code, which is, in a
1151 way, a close relative of parsing) are the only parts of the
1152 compiler that should be calling push_scope and pop_scope, or
1153 related functions. The parser (and template instantiation code)
1154 keeps track of what scope is presently active; everything else
1155 should simply honor that. (The code that generates static
1156 initializers may also need to set the scope, in order to check
1157 access control correctly when emitting the initializers.)
1162 The parser is of the standard recursive-descent variety. Upcoming
1163 tokens in the token stream are examined in order to determine which
1164 production to use when parsing a non-terminal. Some C++ constructs
1165 require arbitrary look ahead to disambiguate. For example, it is
1166 impossible, in the general case, to tell whether a statement is an
1167 expression or declaration without scanning the entire statement.
1168 Therefore, the parser is capable of "parsing tentatively." When the
1169 parser is not sure what construct comes next, it enters this mode.
1170 Then, while we attempt to parse the construct, the parser queues up
1171 error messages, rather than issuing them immediately, and saves the
1172 tokens it consumes. If the construct is parsed successfully, the
1173 parser "commits", i.e., it issues any queued error messages and
1174 the tokens that were being preserved are permanently discarded.
1175 If, however, the construct is not parsed successfully, the parser
1176 rolls back its state completely so that it can resume parsing using
1177 a different alternative.
1182 The performance of the parser could probably be improved substantially.
1183 We could often eliminate the need to parse tentatively by looking ahead
1184 a little bit. In some places, this approach might not entirely eliminate
1185 the need to parse tentatively, but it might still speed up the average
1188 /* Flags that are passed to some parsing functions. These values can
1189 be bitwise-ored together. */
1194 CP_PARSER_FLAGS_NONE = 0x0,
1195 /* The construct is optional. If it is not present, then no error
1196 should be issued. */
1197 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198 /* When parsing a type-specifier, treat user-defined type-names
1199 as non-type identifiers. */
1200 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1201 /* When parsing a type-specifier, do not try to parse a class-specifier
1202 or enum-specifier. */
1203 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1206 /* This type is used for parameters and variables which hold
1207 combinations of the above flags. */
1208 typedef int cp_parser_flags;
1210 /* The different kinds of declarators we want to parse. */
1212 typedef enum cp_parser_declarator_kind
1214 /* We want an abstract declarator. */
1215 CP_PARSER_DECLARATOR_ABSTRACT,
1216 /* We want a named declarator. */
1217 CP_PARSER_DECLARATOR_NAMED,
1218 /* We don't mind, but the name must be an unqualified-id. */
1219 CP_PARSER_DECLARATOR_EITHER
1220 } cp_parser_declarator_kind;
1222 /* The precedence values used to parse binary expressions. The minimum value
1223 of PREC must be 1, because zero is reserved to quickly discriminate
1224 binary operators from other tokens. */
1229 PREC_LOGICAL_OR_EXPRESSION,
1230 PREC_LOGICAL_AND_EXPRESSION,
1231 PREC_INCLUSIVE_OR_EXPRESSION,
1232 PREC_EXCLUSIVE_OR_EXPRESSION,
1233 PREC_AND_EXPRESSION,
1234 PREC_EQUALITY_EXPRESSION,
1235 PREC_RELATIONAL_EXPRESSION,
1236 PREC_SHIFT_EXPRESSION,
1237 PREC_ADDITIVE_EXPRESSION,
1238 PREC_MULTIPLICATIVE_EXPRESSION,
1240 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1243 /* A mapping from a token type to a corresponding tree node type, with a
1244 precedence value. */
1246 typedef struct cp_parser_binary_operations_map_node
1248 /* The token type. */
1249 enum cpp_ttype token_type;
1250 /* The corresponding tree code. */
1251 enum tree_code tree_type;
1252 /* The precedence of this operator. */
1253 enum cp_parser_prec prec;
1254 } cp_parser_binary_operations_map_node;
1256 /* The status of a tentative parse. */
1258 typedef enum cp_parser_status_kind
1260 /* No errors have occurred. */
1261 CP_PARSER_STATUS_KIND_NO_ERROR,
1262 /* An error has occurred. */
1263 CP_PARSER_STATUS_KIND_ERROR,
1264 /* We are committed to this tentative parse, whether or not an error
1266 CP_PARSER_STATUS_KIND_COMMITTED
1267 } cp_parser_status_kind;
1269 typedef struct cp_parser_expression_stack_entry
1271 /* Left hand side of the binary operation we are currently
1274 /* Original tree code for left hand side, if it was a binary
1275 expression itself (used for -Wparentheses). */
1276 enum tree_code lhs_type;
1277 /* Tree code for the binary operation we are parsing. */
1278 enum tree_code tree_type;
1279 /* Precedence of the binary operation we are parsing. */
1280 enum cp_parser_prec prec;
1281 } cp_parser_expression_stack_entry;
1283 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1284 entries because precedence levels on the stack are monotonically
1286 typedef struct cp_parser_expression_stack_entry
1287 cp_parser_expression_stack[NUM_PREC_VALUES];
1289 /* Context that is saved and restored when parsing tentatively. */
1290 typedef struct GTY (()) cp_parser_context {
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 GTY(()) cp_parser {
1397 /* The lexer from which we are obtaining tokens. */
1400 /* The scope in which names should be looked up. If NULL_TREE, then
1401 we look up names in the scope that is currently open in the
1402 source program. If non-NULL, this is either a TYPE or
1403 NAMESPACE_DECL for the scope in which we should look. It can
1404 also be ERROR_MARK, when we've parsed a bogus scope.
1406 This value is not cleared automatically after a name is looked
1407 up, so we must be careful to clear it before starting a new look
1408 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1409 will look up `Z' in the scope of `X', rather than the current
1410 scope.) Unfortunately, it is difficult to tell when name lookup
1411 is complete, because we sometimes peek at a token, look it up,
1412 and then decide not to consume it. */
1415 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1416 last lookup took place. OBJECT_SCOPE is used if an expression
1417 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1418 respectively. QUALIFYING_SCOPE is used for an expression of the
1419 form "X::Y"; it refers to X. */
1421 tree qualifying_scope;
1423 /* A stack of parsing contexts. All but the bottom entry on the
1424 stack will be tentative contexts.
1426 We parse tentatively in order to determine which construct is in
1427 use in some situations. For example, in order to determine
1428 whether a statement is an expression-statement or a
1429 declaration-statement we parse it tentatively as a
1430 declaration-statement. If that fails, we then reparse the same
1431 token stream as an expression-statement. */
1432 cp_parser_context *context;
1434 /* True if we are parsing GNU C++. If this flag is not set, then
1435 GNU extensions are not recognized. */
1436 bool allow_gnu_extensions_p;
1438 /* TRUE if the `>' token should be interpreted as the greater-than
1439 operator. FALSE if it is the end of a template-id or
1440 template-parameter-list. In C++0x mode, this flag also applies to
1441 `>>' tokens, which are viewed as two consecutive `>' tokens when
1442 this flag is FALSE. */
1443 bool greater_than_is_operator_p;
1445 /* TRUE if default arguments are allowed within a parameter list
1446 that starts at this point. FALSE if only a gnu extension makes
1447 them permissible. */
1448 bool default_arg_ok_p;
1450 /* TRUE if we are parsing an integral constant-expression. See
1451 [expr.const] for a precise definition. */
1452 bool integral_constant_expression_p;
1454 /* TRUE if we are parsing an integral constant-expression -- but a
1455 non-constant expression should be permitted as well. This flag
1456 is used when parsing an array bound so that GNU variable-length
1457 arrays are tolerated. */
1458 bool allow_non_integral_constant_expression_p;
1460 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1461 been seen that makes the expression non-constant. */
1462 bool non_integral_constant_expression_p;
1464 /* TRUE if local variable names and `this' are forbidden in the
1466 bool local_variables_forbidden_p;
1468 /* TRUE if the declaration we are parsing is part of a
1469 linkage-specification of the form `extern string-literal
1471 bool in_unbraced_linkage_specification_p;
1473 /* TRUE if we are presently parsing a declarator, after the
1474 direct-declarator. */
1475 bool in_declarator_p;
1477 /* TRUE if we are presently parsing a template-argument-list. */
1478 bool in_template_argument_list_p;
1480 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1481 to IN_OMP_BLOCK if parsing OpenMP structured block and
1482 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1483 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1484 iteration-statement, OpenMP block or loop within that switch. */
1485 #define IN_SWITCH_STMT 1
1486 #define IN_ITERATION_STMT 2
1487 #define IN_OMP_BLOCK 4
1488 #define IN_OMP_FOR 8
1489 #define IN_IF_STMT 16
1490 unsigned char in_statement;
1492 /* TRUE if we are presently parsing the body of a switch statement.
1493 Note that this doesn't quite overlap with in_statement above.
1494 The difference relates to giving the right sets of error messages:
1495 "case not in switch" vs "break statement used with OpenMP...". */
1496 bool in_switch_statement_p;
1498 /* TRUE if we are parsing a type-id in an expression context. In
1499 such a situation, both "type (expr)" and "type (type)" are valid
1501 bool in_type_id_in_expr_p;
1503 /* TRUE if we are currently in a header file where declarations are
1504 implicitly extern "C". */
1505 bool implicit_extern_c;
1507 /* TRUE if strings in expressions should be translated to the execution
1509 bool translate_strings_p;
1511 /* TRUE if we are presently parsing the body of a function, but not
1513 bool in_function_body;
1515 /* If non-NULL, then we are parsing a construct where new type
1516 definitions are not permitted. The string stored here will be
1517 issued as an error message if a type is defined. */
1518 const char *type_definition_forbidden_message;
1520 /* A list of lists. The outer list is a stack, used for member
1521 functions of local classes. At each level there are two sub-list,
1522 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1523 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1524 TREE_VALUE's. The functions are chained in reverse declaration
1527 The TREE_PURPOSE sublist contains those functions with default
1528 arguments that need post processing, and the TREE_VALUE sublist
1529 contains those functions with definitions that need post
1532 These lists can only be processed once the outermost class being
1533 defined is complete. */
1534 tree unparsed_functions_queues;
1536 /* The number of classes whose definitions are currently in
1538 unsigned num_classes_being_defined;
1540 /* The number of template parameter lists that apply directly to the
1541 current declaration. */
1542 unsigned num_template_parameter_lists;
1547 /* Constructors and destructors. */
1549 static cp_parser *cp_parser_new
1552 /* Routines to parse various constructs.
1554 Those that return `tree' will return the error_mark_node (rather
1555 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1556 Sometimes, they will return an ordinary node if error-recovery was
1557 attempted, even though a parse error occurred. So, to check
1558 whether or not a parse error occurred, you should always use
1559 cp_parser_error_occurred. If the construct is optional (indicated
1560 either by an `_opt' in the name of the function that does the
1561 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1562 the construct is not present. */
1564 /* Lexical conventions [gram.lex] */
1566 static tree cp_parser_identifier
1568 static tree cp_parser_string_literal
1569 (cp_parser *, bool, bool);
1571 /* Basic concepts [gram.basic] */
1573 static bool cp_parser_translation_unit
1576 /* Expressions [gram.expr] */
1578 static tree cp_parser_primary_expression
1579 (cp_parser *, bool, bool, bool, cp_id_kind *);
1580 static tree cp_parser_id_expression
1581 (cp_parser *, bool, bool, bool *, bool, bool);
1582 static tree cp_parser_unqualified_id
1583 (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier_opt
1585 (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier
1587 (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_qualifying_entity
1589 (cp_parser *, bool, bool, bool, bool, bool);
1590 static tree cp_parser_postfix_expression
1591 (cp_parser *, bool, bool, bool, cp_id_kind *);
1592 static tree cp_parser_postfix_open_square_expression
1593 (cp_parser *, tree, bool);
1594 static tree cp_parser_postfix_dot_deref_expression
1595 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1596 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1597 (cp_parser *, bool, bool, bool, bool *);
1598 static void cp_parser_pseudo_destructor_name
1599 (cp_parser *, tree *, tree *);
1600 static tree cp_parser_unary_expression
1601 (cp_parser *, bool, bool, cp_id_kind *);
1602 static enum tree_code cp_parser_unary_operator
1604 static tree cp_parser_new_expression
1606 static VEC(tree,gc) *cp_parser_new_placement
1608 static tree cp_parser_new_type_id
1609 (cp_parser *, tree *);
1610 static cp_declarator *cp_parser_new_declarator_opt
1612 static cp_declarator *cp_parser_direct_new_declarator
1614 static VEC(tree,gc) *cp_parser_new_initializer
1616 static tree cp_parser_delete_expression
1618 static tree cp_parser_cast_expression
1619 (cp_parser *, bool, bool, cp_id_kind *);
1620 static tree cp_parser_binary_expression
1621 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1622 static tree cp_parser_question_colon_clause
1623 (cp_parser *, tree);
1624 static tree cp_parser_assignment_expression
1625 (cp_parser *, bool, cp_id_kind *);
1626 static enum tree_code cp_parser_assignment_operator_opt
1628 static tree cp_parser_expression
1629 (cp_parser *, bool, cp_id_kind *);
1630 static tree cp_parser_constant_expression
1631 (cp_parser *, bool, bool *);
1632 static tree cp_parser_builtin_offsetof
1634 static tree cp_parser_lambda_expression
1636 static void cp_parser_lambda_introducer
1637 (cp_parser *, tree);
1638 static void cp_parser_lambda_declarator_opt
1639 (cp_parser *, tree);
1640 static void cp_parser_lambda_body
1641 (cp_parser *, tree);
1643 /* Statements [gram.stmt.stmt] */
1645 static void cp_parser_statement
1646 (cp_parser *, tree, bool, bool *);
1647 static void cp_parser_label_for_labeled_statement
1649 static tree cp_parser_expression_statement
1650 (cp_parser *, tree);
1651 static tree cp_parser_compound_statement
1652 (cp_parser *, tree, bool);
1653 static void cp_parser_statement_seq_opt
1654 (cp_parser *, tree);
1655 static tree cp_parser_selection_statement
1656 (cp_parser *, bool *);
1657 static tree cp_parser_condition
1659 static tree cp_parser_iteration_statement
1661 static void cp_parser_for_init_statement
1663 static tree cp_parser_jump_statement
1665 static void cp_parser_declaration_statement
1668 static tree cp_parser_implicitly_scoped_statement
1669 (cp_parser *, bool *);
1670 static void cp_parser_already_scoped_statement
1673 /* Declarations [gram.dcl.dcl] */
1675 static void cp_parser_declaration_seq_opt
1677 static void cp_parser_declaration
1679 static void cp_parser_block_declaration
1680 (cp_parser *, bool);
1681 static void cp_parser_simple_declaration
1682 (cp_parser *, bool);
1683 static void cp_parser_decl_specifier_seq
1684 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1685 static tree cp_parser_storage_class_specifier_opt
1687 static tree cp_parser_function_specifier_opt
1688 (cp_parser *, cp_decl_specifier_seq *);
1689 static tree cp_parser_type_specifier
1690 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1692 static tree cp_parser_simple_type_specifier
1693 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1694 static tree cp_parser_type_name
1696 static tree cp_parser_nonclass_name
1697 (cp_parser* parser);
1698 static tree cp_parser_elaborated_type_specifier
1699 (cp_parser *, bool, bool);
1700 static tree cp_parser_enum_specifier
1702 static void cp_parser_enumerator_list
1703 (cp_parser *, tree);
1704 static void cp_parser_enumerator_definition
1705 (cp_parser *, tree);
1706 static tree cp_parser_namespace_name
1708 static void cp_parser_namespace_definition
1710 static void cp_parser_namespace_body
1712 static tree cp_parser_qualified_namespace_specifier
1714 static void cp_parser_namespace_alias_definition
1716 static bool cp_parser_using_declaration
1717 (cp_parser *, bool);
1718 static void cp_parser_using_directive
1720 static void cp_parser_asm_definition
1722 static void cp_parser_linkage_specification
1724 static void cp_parser_static_assert
1725 (cp_parser *, bool);
1726 static tree cp_parser_decltype
1729 /* Declarators [gram.dcl.decl] */
1731 static tree cp_parser_init_declarator
1732 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1733 static cp_declarator *cp_parser_declarator
1734 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1735 static cp_declarator *cp_parser_direct_declarator
1736 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1737 static enum tree_code cp_parser_ptr_operator
1738 (cp_parser *, tree *, cp_cv_quals *);
1739 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1741 static tree cp_parser_late_return_type_opt
1743 static tree cp_parser_declarator_id
1744 (cp_parser *, bool);
1745 static tree cp_parser_type_id
1747 static tree cp_parser_template_type_arg
1749 static tree cp_parser_trailing_type_id (cp_parser *);
1750 static tree cp_parser_type_id_1
1751 (cp_parser *, bool, bool);
1752 static void cp_parser_type_specifier_seq
1753 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1754 static tree cp_parser_parameter_declaration_clause
1756 static tree cp_parser_parameter_declaration_list
1757 (cp_parser *, bool *);
1758 static cp_parameter_declarator *cp_parser_parameter_declaration
1759 (cp_parser *, bool, bool *);
1760 static tree cp_parser_default_argument
1761 (cp_parser *, bool);
1762 static void cp_parser_function_body
1764 static tree cp_parser_initializer
1765 (cp_parser *, bool *, bool *);
1766 static tree cp_parser_initializer_clause
1767 (cp_parser *, bool *);
1768 static tree cp_parser_braced_list
1769 (cp_parser*, bool*);
1770 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1771 (cp_parser *, bool *);
1773 static bool cp_parser_ctor_initializer_opt_and_function_body
1776 /* Classes [gram.class] */
1778 static tree cp_parser_class_name
1779 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1780 static tree cp_parser_class_specifier
1782 static tree cp_parser_class_head
1783 (cp_parser *, bool *, tree *, tree *);
1784 static enum tag_types cp_parser_class_key
1786 static void cp_parser_member_specification_opt
1788 static void cp_parser_member_declaration
1790 static tree cp_parser_pure_specifier
1792 static tree cp_parser_constant_initializer
1795 /* Derived classes [gram.class.derived] */
1797 static tree cp_parser_base_clause
1799 static tree cp_parser_base_specifier
1802 /* Special member functions [gram.special] */
1804 static tree cp_parser_conversion_function_id
1806 static tree cp_parser_conversion_type_id
1808 static cp_declarator *cp_parser_conversion_declarator_opt
1810 static bool cp_parser_ctor_initializer_opt
1812 static void cp_parser_mem_initializer_list
1814 static tree cp_parser_mem_initializer
1816 static tree cp_parser_mem_initializer_id
1819 /* Overloading [gram.over] */
1821 static tree cp_parser_operator_function_id
1823 static tree cp_parser_operator
1826 /* Templates [gram.temp] */
1828 static void cp_parser_template_declaration
1829 (cp_parser *, bool);
1830 static tree cp_parser_template_parameter_list
1832 static tree cp_parser_template_parameter
1833 (cp_parser *, bool *, bool *);
1834 static tree cp_parser_type_parameter
1835 (cp_parser *, bool *);
1836 static tree cp_parser_template_id
1837 (cp_parser *, bool, bool, bool);
1838 static tree cp_parser_template_name
1839 (cp_parser *, bool, bool, bool, bool *);
1840 static tree cp_parser_template_argument_list
1842 static tree cp_parser_template_argument
1844 static void cp_parser_explicit_instantiation
1846 static void cp_parser_explicit_specialization
1849 /* Exception handling [gram.exception] */
1851 static tree cp_parser_try_block
1853 static bool cp_parser_function_try_block
1855 static void cp_parser_handler_seq
1857 static void cp_parser_handler
1859 static tree cp_parser_exception_declaration
1861 static tree cp_parser_throw_expression
1863 static tree cp_parser_exception_specification_opt
1865 static tree cp_parser_type_id_list
1868 /* GNU Extensions */
1870 static tree cp_parser_asm_specification_opt
1872 static tree cp_parser_asm_operand_list
1874 static tree cp_parser_asm_clobber_list
1876 static tree cp_parser_asm_label_list
1878 static tree cp_parser_attributes_opt
1880 static tree cp_parser_attribute_list
1882 static bool cp_parser_extension_opt
1883 (cp_parser *, int *);
1884 static void cp_parser_label_declaration
1887 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1888 static bool cp_parser_pragma
1889 (cp_parser *, enum pragma_context);
1891 /* Objective-C++ Productions */
1893 static tree cp_parser_objc_message_receiver
1895 static tree cp_parser_objc_message_args
1897 static tree cp_parser_objc_message_expression
1899 static tree cp_parser_objc_encode_expression
1901 static tree cp_parser_objc_defs_expression
1903 static tree cp_parser_objc_protocol_expression
1905 static tree cp_parser_objc_selector_expression
1907 static tree cp_parser_objc_expression
1909 static bool cp_parser_objc_selector_p
1911 static tree cp_parser_objc_selector
1913 static tree cp_parser_objc_protocol_refs_opt
1915 static void cp_parser_objc_declaration
1917 static tree cp_parser_objc_statement
1920 /* Utility Routines */
1922 static tree cp_parser_lookup_name
1923 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1924 static tree cp_parser_lookup_name_simple
1925 (cp_parser *, tree, location_t);
1926 static tree cp_parser_maybe_treat_template_as_class
1928 static bool cp_parser_check_declarator_template_parameters
1929 (cp_parser *, cp_declarator *, location_t);
1930 static bool cp_parser_check_template_parameters
1931 (cp_parser *, unsigned, location_t, cp_declarator *);
1932 static tree cp_parser_simple_cast_expression
1934 static tree cp_parser_global_scope_opt
1935 (cp_parser *, bool);
1936 static bool cp_parser_constructor_declarator_p
1937 (cp_parser *, bool);
1938 static tree cp_parser_function_definition_from_specifiers_and_declarator
1939 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1940 static tree cp_parser_function_definition_after_declarator
1941 (cp_parser *, bool);
1942 static void cp_parser_template_declaration_after_export
1943 (cp_parser *, bool);
1944 static void cp_parser_perform_template_parameter_access_checks
1945 (VEC (deferred_access_check,gc)*);
1946 static tree cp_parser_single_declaration
1947 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1948 static tree cp_parser_functional_cast
1949 (cp_parser *, tree);
1950 static tree cp_parser_save_member_function_body
1951 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1952 static tree cp_parser_enclosed_template_argument_list
1954 static void cp_parser_save_default_args
1955 (cp_parser *, tree);
1956 static void cp_parser_late_parsing_for_member
1957 (cp_parser *, tree);
1958 static void cp_parser_late_parsing_default_args
1959 (cp_parser *, tree);
1960 static tree cp_parser_sizeof_operand
1961 (cp_parser *, enum rid);
1962 static tree cp_parser_trait_expr
1963 (cp_parser *, enum rid);
1964 static bool cp_parser_declares_only_class_p
1966 static void cp_parser_set_storage_class
1967 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1968 static void cp_parser_set_decl_spec_type
1969 (cp_decl_specifier_seq *, tree, location_t, bool);
1970 static bool cp_parser_friend_p
1971 (const cp_decl_specifier_seq *);
1972 static cp_token *cp_parser_require
1973 (cp_parser *, enum cpp_ttype, const char *);
1974 static cp_token *cp_parser_require_keyword
1975 (cp_parser *, enum rid, const char *);
1976 static bool cp_parser_token_starts_function_definition_p
1978 static bool cp_parser_next_token_starts_class_definition_p
1980 static bool cp_parser_next_token_ends_template_argument_p
1982 static bool cp_parser_nth_token_starts_template_argument_list_p
1983 (cp_parser *, size_t);
1984 static enum tag_types cp_parser_token_is_class_key
1986 static void cp_parser_check_class_key
1987 (enum tag_types, tree type);
1988 static void cp_parser_check_access_in_redeclaration
1989 (tree type, location_t location);
1990 static bool cp_parser_optional_template_keyword
1992 static void cp_parser_pre_parsed_nested_name_specifier
1994 static bool cp_parser_cache_group
1995 (cp_parser *, enum cpp_ttype, unsigned);
1996 static void cp_parser_parse_tentatively
1998 static void cp_parser_commit_to_tentative_parse
2000 static void cp_parser_abort_tentative_parse
2002 static bool cp_parser_parse_definitely
2004 static inline bool cp_parser_parsing_tentatively
2006 static bool cp_parser_uncommitted_to_tentative_parse_p
2008 static void cp_parser_error
2009 (cp_parser *, const char *);
2010 static void cp_parser_name_lookup_error
2011 (cp_parser *, tree, tree, const char *, location_t);
2012 static bool cp_parser_simulate_error
2014 static bool cp_parser_check_type_definition
2016 static void cp_parser_check_for_definition_in_return_type
2017 (cp_declarator *, tree, location_t type_location);
2018 static void cp_parser_check_for_invalid_template_id
2019 (cp_parser *, tree, location_t location);
2020 static bool cp_parser_non_integral_constant_expression
2021 (cp_parser *, const char *);
2022 static void cp_parser_diagnose_invalid_type_name
2023 (cp_parser *, tree, tree, location_t);
2024 static bool cp_parser_parse_and_diagnose_invalid_type_name
2026 static int cp_parser_skip_to_closing_parenthesis
2027 (cp_parser *, bool, bool, bool);
2028 static void cp_parser_skip_to_end_of_statement
2030 static void cp_parser_consume_semicolon_at_end_of_statement
2032 static void cp_parser_skip_to_end_of_block_or_statement
2034 static bool cp_parser_skip_to_closing_brace
2036 static void cp_parser_skip_to_end_of_template_parameter_list
2038 static void cp_parser_skip_to_pragma_eol
2039 (cp_parser*, cp_token *);
2040 static bool cp_parser_error_occurred
2042 static bool cp_parser_allow_gnu_extensions_p
2044 static bool cp_parser_is_string_literal
2046 static bool cp_parser_is_keyword
2047 (cp_token *, enum rid);
2048 static tree cp_parser_make_typename_type
2049 (cp_parser *, tree, tree, location_t location);
2050 static cp_declarator * cp_parser_make_indirect_declarator
2051 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2053 /* Returns nonzero if we are parsing tentatively. */
2056 cp_parser_parsing_tentatively (cp_parser* parser)
2058 return parser->context->next != NULL;
2061 /* Returns nonzero if TOKEN is a string literal. */
2064 cp_parser_is_string_literal (cp_token* token)
2066 return (token->type == CPP_STRING ||
2067 token->type == CPP_STRING16 ||
2068 token->type == CPP_STRING32 ||
2069 token->type == CPP_WSTRING ||
2070 token->type == CPP_UTF8STRING);
2073 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2076 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2078 return token->keyword == keyword;
2081 /* If not parsing tentatively, issue a diagnostic of the form
2082 FILE:LINE: MESSAGE before TOKEN
2083 where TOKEN is the next token in the input stream. MESSAGE
2084 (specified by the caller) is usually of the form "expected
2088 cp_parser_error (cp_parser* parser, const char* message)
2090 if (!cp_parser_simulate_error (parser))
2092 cp_token *token = cp_lexer_peek_token (parser->lexer);
2093 /* This diagnostic makes more sense if it is tagged to the line
2094 of the token we just peeked at. */
2095 cp_lexer_set_source_position_from_token (token);
2097 if (token->type == CPP_PRAGMA)
2099 error_at (token->location,
2100 "%<#pragma%> is not allowed here");
2101 cp_parser_skip_to_pragma_eol (parser, token);
2105 c_parse_error (message,
2106 /* Because c_parser_error does not understand
2107 CPP_KEYWORD, keywords are treated like
2109 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2110 token->u.value, token->flags);
2114 /* Issue an error about name-lookup failing. NAME is the
2115 IDENTIFIER_NODE DECL is the result of
2116 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2117 the thing that we hoped to find. */
2120 cp_parser_name_lookup_error (cp_parser* parser,
2123 const char* desired,
2124 location_t location)
2126 /* If name lookup completely failed, tell the user that NAME was not
2128 if (decl == error_mark_node)
2130 if (parser->scope && parser->scope != global_namespace)
2131 error_at (location, "%<%E::%E%> has not been declared",
2132 parser->scope, name);
2133 else if (parser->scope == global_namespace)
2134 error_at (location, "%<::%E%> has not been declared", name);
2135 else if (parser->object_scope
2136 && !CLASS_TYPE_P (parser->object_scope))
2137 error_at (location, "request for member %qE in non-class type %qT",
2138 name, parser->object_scope);
2139 else if (parser->object_scope)
2140 error_at (location, "%<%T::%E%> has not been declared",
2141 parser->object_scope, name);
2143 error_at (location, "%qE has not been declared", name);
2145 else if (parser->scope && parser->scope != global_namespace)
2146 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2147 else if (parser->scope == global_namespace)
2148 error_at (location, "%<::%E%> %s", name, desired);
2150 error_at (location, "%qE %s", name, desired);
2153 /* If we are parsing tentatively, remember that an error has occurred
2154 during this tentative parse. Returns true if the error was
2155 simulated; false if a message should be issued by the caller. */
2158 cp_parser_simulate_error (cp_parser* parser)
2160 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2162 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2168 /* Check for repeated decl-specifiers. */
2171 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2172 location_t location)
2176 for (ds = ds_first; ds != ds_last; ++ds)
2178 unsigned count = decl_specs->specs[ds];
2181 /* The "long" specifier is a special case because of "long long". */
2185 error_at (location, "%<long long long%> is too long for GCC");
2187 pedwarn_cxx98 (location, OPT_Wlong_long,
2188 "ISO C++ 1998 does not support %<long long%>");
2192 static const char *const decl_spec_names[] = {
2209 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2214 /* This function is called when a type is defined. If type
2215 definitions are forbidden at this point, an error message is
2219 cp_parser_check_type_definition (cp_parser* parser)
2221 /* If types are forbidden here, issue a message. */
2222 if (parser->type_definition_forbidden_message)
2224 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2225 in the message need to be interpreted. */
2226 error (parser->type_definition_forbidden_message);
2232 /* This function is called when the DECLARATOR is processed. The TYPE
2233 was a type defined in the decl-specifiers. If it is invalid to
2234 define a type in the decl-specifiers for DECLARATOR, an error is
2235 issued. TYPE_LOCATION is the location of TYPE and is used
2236 for error reporting. */
2239 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2240 tree type, location_t type_location)
2242 /* [dcl.fct] forbids type definitions in return types.
2243 Unfortunately, it's not easy to know whether or not we are
2244 processing a return type until after the fact. */
2246 && (declarator->kind == cdk_pointer
2247 || declarator->kind == cdk_reference
2248 || declarator->kind == cdk_ptrmem))
2249 declarator = declarator->declarator;
2251 && declarator->kind == cdk_function)
2253 error_at (type_location,
2254 "new types may not be defined in a return type");
2255 inform (type_location,
2256 "(perhaps a semicolon is missing after the definition of %qT)",
2261 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2262 "<" in any valid C++ program. If the next token is indeed "<",
2263 issue a message warning the user about what appears to be an
2264 invalid attempt to form a template-id. LOCATION is the location
2265 of the type-specifier (TYPE) */
2268 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2269 tree type, location_t location)
2271 cp_token_position start = 0;
2273 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2276 error_at (location, "%qT is not a template", type);
2277 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2278 error_at (location, "%qE is not a template", type);
2280 error_at (location, "invalid template-id");
2281 /* Remember the location of the invalid "<". */
2282 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2283 start = cp_lexer_token_position (parser->lexer, true);
2284 /* Consume the "<". */
2285 cp_lexer_consume_token (parser->lexer);
2286 /* Parse the template arguments. */
2287 cp_parser_enclosed_template_argument_list (parser);
2288 /* Permanently remove the invalid template arguments so that
2289 this error message is not issued again. */
2291 cp_lexer_purge_tokens_after (parser->lexer, start);
2295 /* If parsing an integral constant-expression, issue an error message
2296 about the fact that THING appeared and return true. Otherwise,
2297 return false. In either case, set
2298 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2301 cp_parser_non_integral_constant_expression (cp_parser *parser,
2304 parser->non_integral_constant_expression_p = true;
2305 if (parser->integral_constant_expression_p)
2307 if (!parser->allow_non_integral_constant_expression_p)
2309 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2310 in the message need to be interpreted. */
2311 char *message = concat (thing,
2312 " cannot appear in a constant-expression",
2322 /* Emit a diagnostic for an invalid type name. SCOPE is the
2323 qualifying scope (or NULL, if none) for ID. This function commits
2324 to the current active tentative parse, if any. (Otherwise, the
2325 problematic construct might be encountered again later, resulting
2326 in duplicate error messages.) LOCATION is the location of ID. */
2329 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2330 tree scope, tree id,
2331 location_t location)
2333 tree decl, old_scope;
2334 /* Try to lookup the identifier. */
2335 old_scope = parser->scope;
2336 parser->scope = scope;
2337 decl = cp_parser_lookup_name_simple (parser, id, location);
2338 parser->scope = old_scope;
2339 /* If the lookup found a template-name, it means that the user forgot
2340 to specify an argument list. Emit a useful error message. */
2341 if (TREE_CODE (decl) == TEMPLATE_DECL)
2343 "invalid use of template-name %qE without an argument list",
2345 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2346 error_at (location, "invalid use of destructor %qD as a type", id);
2347 else if (TREE_CODE (decl) == TYPE_DECL)
2348 /* Something like 'unsigned A a;' */
2349 error_at (location, "invalid combination of multiple type-specifiers");
2350 else if (!parser->scope)
2352 /* Issue an error message. */
2353 error_at (location, "%qE does not name a type", id);
2354 /* If we're in a template class, it's possible that the user was
2355 referring to a type from a base class. For example:
2357 template <typename T> struct A { typedef T X; };
2358 template <typename T> struct B : public A<T> { X x; };
2360 The user should have said "typename A<T>::X". */
2361 if (processing_template_decl && current_class_type
2362 && TYPE_BINFO (current_class_type))
2366 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2370 tree base_type = BINFO_TYPE (b);
2371 if (CLASS_TYPE_P (base_type)
2372 && dependent_type_p (base_type))
2375 /* Go from a particular instantiation of the
2376 template (which will have an empty TYPE_FIELDs),
2377 to the main version. */
2378 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2379 for (field = TYPE_FIELDS (base_type);
2381 field = TREE_CHAIN (field))
2382 if (TREE_CODE (field) == TYPE_DECL
2383 && DECL_NAME (field) == id)
2386 "(perhaps %<typename %T::%E%> was intended)",
2387 BINFO_TYPE (b), id);
2396 /* Here we diagnose qualified-ids where the scope is actually correct,
2397 but the identifier does not resolve to a valid type name. */
2398 else if (parser->scope != error_mark_node)
2400 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2401 error_at (location, "%qE in namespace %qE does not name a type",
2403 else if (TYPE_P (parser->scope))
2404 error_at (location, "%qE in class %qT does not name a type",
2409 cp_parser_commit_to_tentative_parse (parser);
2412 /* Check for a common situation where a type-name should be present,
2413 but is not, and issue a sensible error message. Returns true if an
2414 invalid type-name was detected.
2416 The situation handled by this function are variable declarations of the
2417 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2418 Usually, `ID' should name a type, but if we got here it means that it
2419 does not. We try to emit the best possible error message depending on
2420 how exactly the id-expression looks like. */
2423 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2426 cp_token *token = cp_lexer_peek_token (parser->lexer);
2428 cp_parser_parse_tentatively (parser);
2429 id = cp_parser_id_expression (parser,
2430 /*template_keyword_p=*/false,
2431 /*check_dependency_p=*/true,
2432 /*template_p=*/NULL,
2433 /*declarator_p=*/true,
2434 /*optional_p=*/false);
2435 /* After the id-expression, there should be a plain identifier,
2436 otherwise this is not a simple variable declaration. Also, if
2437 the scope is dependent, we cannot do much. */
2438 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2439 || (parser->scope && TYPE_P (parser->scope)
2440 && dependent_type_p (parser->scope))
2441 || TREE_CODE (id) == TYPE_DECL)
2443 cp_parser_abort_tentative_parse (parser);
2446 if (!cp_parser_parse_definitely (parser))
2449 /* Emit a diagnostic for the invalid type. */
2450 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2451 id, token->location);
2452 /* Skip to the end of the declaration; there's no point in
2453 trying to process it. */
2454 cp_parser_skip_to_end_of_block_or_statement (parser);
2458 /* Consume tokens up to, and including, the next non-nested closing `)'.
2459 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2460 are doing error recovery. Returns -1 if OR_COMMA is true and we
2461 found an unnested comma. */
2464 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2469 unsigned paren_depth = 0;
2470 unsigned brace_depth = 0;
2471 unsigned square_depth = 0;
2473 if (recovering && !or_comma
2474 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2479 cp_token * token = cp_lexer_peek_token (parser->lexer);
2481 switch (token->type)
2484 case CPP_PRAGMA_EOL:
2485 /* If we've run out of tokens, then there is no closing `)'. */
2488 /* This is good for lambda expression capture-lists. */
2489 case CPP_OPEN_SQUARE:
2492 case CPP_CLOSE_SQUARE:
2493 if (!square_depth--)
2498 /* This matches the processing in skip_to_end_of_statement. */
2503 case CPP_OPEN_BRACE:
2506 case CPP_CLOSE_BRACE:
2512 if (recovering && or_comma && !brace_depth && !paren_depth
2517 case CPP_OPEN_PAREN:
2522 case CPP_CLOSE_PAREN:
2523 if (!brace_depth && !paren_depth--)
2526 cp_lexer_consume_token (parser->lexer);
2535 /* Consume the token. */
2536 cp_lexer_consume_token (parser->lexer);
2540 /* Consume tokens until we reach the end of the current statement.
2541 Normally, that will be just before consuming a `;'. However, if a
2542 non-nested `}' comes first, then we stop before consuming that. */
2545 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2547 unsigned nesting_depth = 0;
2551 cp_token *token = cp_lexer_peek_token (parser->lexer);
2553 switch (token->type)
2556 case CPP_PRAGMA_EOL:
2557 /* If we've run out of tokens, stop. */
2561 /* If the next token is a `;', we have reached the end of the
2567 case CPP_CLOSE_BRACE:
2568 /* If this is a non-nested '}', stop before consuming it.
2569 That way, when confronted with something like:
2573 we stop before consuming the closing '}', even though we
2574 have not yet reached a `;'. */
2575 if (nesting_depth == 0)
2578 /* If it is the closing '}' for a block that we have
2579 scanned, stop -- but only after consuming the token.
2585 we will stop after the body of the erroneously declared
2586 function, but before consuming the following `typedef'
2588 if (--nesting_depth == 0)
2590 cp_lexer_consume_token (parser->lexer);
2594 case CPP_OPEN_BRACE:
2602 /* Consume the token. */
2603 cp_lexer_consume_token (parser->lexer);
2607 /* This function is called at the end of a statement or declaration.
2608 If the next token is a semicolon, it is consumed; otherwise, error
2609 recovery is attempted. */
2612 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2614 /* Look for the trailing `;'. */
2615 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2617 /* If there is additional (erroneous) input, skip to the end of
2619 cp_parser_skip_to_end_of_statement (parser);
2620 /* If the next token is now a `;', consume it. */
2621 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2622 cp_lexer_consume_token (parser->lexer);
2626 /* Skip tokens until we have consumed an entire block, or until we
2627 have consumed a non-nested `;'. */
2630 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2632 int nesting_depth = 0;
2634 while (nesting_depth >= 0)
2636 cp_token *token = cp_lexer_peek_token (parser->lexer);
2638 switch (token->type)
2641 case CPP_PRAGMA_EOL:
2642 /* If we've run out of tokens, stop. */
2646 /* Stop if this is an unnested ';'. */
2651 case CPP_CLOSE_BRACE:
2652 /* Stop if this is an unnested '}', or closes the outermost
2655 if (nesting_depth < 0)
2661 case CPP_OPEN_BRACE:
2670 /* Consume the token. */
2671 cp_lexer_consume_token (parser->lexer);
2675 /* Skip tokens until a non-nested closing curly brace is the next
2676 token, or there are no more tokens. Return true in the first case,
2680 cp_parser_skip_to_closing_brace (cp_parser *parser)
2682 unsigned nesting_depth = 0;
2686 cp_token *token = cp_lexer_peek_token (parser->lexer);
2688 switch (token->type)
2691 case CPP_PRAGMA_EOL:
2692 /* If we've run out of tokens, stop. */
2695 case CPP_CLOSE_BRACE:
2696 /* If the next token is a non-nested `}', then we have reached
2697 the end of the current block. */
2698 if (nesting_depth-- == 0)
2702 case CPP_OPEN_BRACE:
2703 /* If it the next token is a `{', then we are entering a new
2704 block. Consume the entire block. */
2712 /* Consume the token. */
2713 cp_lexer_consume_token (parser->lexer);
2717 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2718 parameter is the PRAGMA token, allowing us to purge the entire pragma
2722 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2726 parser->lexer->in_pragma = false;
2729 token = cp_lexer_consume_token (parser->lexer);
2730 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2732 /* Ensure that the pragma is not parsed again. */
2733 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2736 /* Require pragma end of line, resyncing with it as necessary. The
2737 arguments are as for cp_parser_skip_to_pragma_eol. */
2740 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2742 parser->lexer->in_pragma = false;
2743 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2744 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2747 /* This is a simple wrapper around make_typename_type. When the id is
2748 an unresolved identifier node, we can provide a superior diagnostic
2749 using cp_parser_diagnose_invalid_type_name. */
2752 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2753 tree id, location_t id_location)
2756 if (TREE_CODE (id) == IDENTIFIER_NODE)
2758 result = make_typename_type (scope, id, typename_type,
2759 /*complain=*/tf_none);
2760 if (result == error_mark_node)
2761 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2764 return make_typename_type (scope, id, typename_type, tf_error);
2767 /* This is a wrapper around the
2768 make_{pointer,ptrmem,reference}_declarator functions that decides
2769 which one to call based on the CODE and CLASS_TYPE arguments. The
2770 CODE argument should be one of the values returned by
2771 cp_parser_ptr_operator. */
2772 static cp_declarator *
2773 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2774 cp_cv_quals cv_qualifiers,
2775 cp_declarator *target)
2777 if (code == ERROR_MARK)
2778 return cp_error_declarator;
2780 if (code == INDIRECT_REF)
2781 if (class_type == NULL_TREE)
2782 return make_pointer_declarator (cv_qualifiers, target);
2784 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2785 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2786 return make_reference_declarator (cv_qualifiers, target, false);
2787 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2788 return make_reference_declarator (cv_qualifiers, target, true);
2792 /* Create a new C++ parser. */
2795 cp_parser_new (void)
2801 /* cp_lexer_new_main is called before calling ggc_alloc because
2802 cp_lexer_new_main might load a PCH file. */
2803 lexer = cp_lexer_new_main ();
2805 /* Initialize the binops_by_token so that we can get the tree
2806 directly from the token. */
2807 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2808 binops_by_token[binops[i].token_type] = binops[i];
2810 parser = GGC_CNEW (cp_parser);
2811 parser->lexer = lexer;
2812 parser->context = cp_parser_context_new (NULL);
2814 /* For now, we always accept GNU extensions. */
2815 parser->allow_gnu_extensions_p = 1;
2817 /* The `>' token is a greater-than operator, not the end of a
2819 parser->greater_than_is_operator_p = true;
2821 parser->default_arg_ok_p = true;
2823 /* We are not parsing a constant-expression. */
2824 parser->integral_constant_expression_p = false;
2825 parser->allow_non_integral_constant_expression_p = false;
2826 parser->non_integral_constant_expression_p = false;
2828 /* Local variable names are not forbidden. */
2829 parser->local_variables_forbidden_p = false;
2831 /* We are not processing an `extern "C"' declaration. */
2832 parser->in_unbraced_linkage_specification_p = false;
2834 /* We are not processing a declarator. */
2835 parser->in_declarator_p = false;
2837 /* We are not processing a template-argument-list. */
2838 parser->in_template_argument_list_p = false;
2840 /* We are not in an iteration statement. */
2841 parser->in_statement = 0;
2843 /* We are not in a switch statement. */
2844 parser->in_switch_statement_p = false;
2846 /* We are not parsing a type-id inside an expression. */
2847 parser->in_type_id_in_expr_p = false;
2849 /* Declarations aren't implicitly extern "C". */
2850 parser->implicit_extern_c = false;
2852 /* String literals should be translated to the execution character set. */
2853 parser->translate_strings_p = true;
2855 /* We are not parsing a function body. */
2856 parser->in_function_body = false;
2858 /* The unparsed function queue is empty. */
2859 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2861 /* There are no classes being defined. */
2862 parser->num_classes_being_defined = 0;
2864 /* No template parameters apply. */
2865 parser->num_template_parameter_lists = 0;
2870 /* Create a cp_lexer structure which will emit the tokens in CACHE
2871 and push it onto the parser's lexer stack. This is used for delayed
2872 parsing of in-class method bodies and default arguments, and should
2873 not be confused with tentative parsing. */
2875 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2877 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2878 lexer->next = parser->lexer;
2879 parser->lexer = lexer;
2881 /* Move the current source position to that of the first token in the
2883 cp_lexer_set_source_position_from_token (lexer->next_token);
2886 /* Pop the top lexer off the parser stack. This is never used for the
2887 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2889 cp_parser_pop_lexer (cp_parser *parser)
2891 cp_lexer *lexer = parser->lexer;
2892 parser->lexer = lexer->next;
2893 cp_lexer_destroy (lexer);
2895 /* Put the current source position back where it was before this
2896 lexer was pushed. */
2897 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2900 /* Lexical conventions [gram.lex] */
2902 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2906 cp_parser_identifier (cp_parser* parser)
2910 /* Look for the identifier. */
2911 token = cp_parser_require (parser, CPP_NAME, "identifier");
2912 /* Return the value. */
2913 return token ? token->u.value : error_mark_node;
2916 /* Parse a sequence of adjacent string constants. Returns a
2917 TREE_STRING representing the combined, nul-terminated string
2918 constant. If TRANSLATE is true, translate the string to the
2919 execution character set. If WIDE_OK is true, a wide string is
2922 C++98 [lex.string] says that if a narrow string literal token is
2923 adjacent to a wide string literal token, the behavior is undefined.
2924 However, C99 6.4.5p4 says that this results in a wide string literal.
2925 We follow C99 here, for consistency with the C front end.
2927 This code is largely lifted from lex_string() in c-lex.c.
2929 FUTURE: ObjC++ will need to handle @-strings here. */
2931 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2935 struct obstack str_ob;
2936 cpp_string str, istr, *strs;
2938 enum cpp_ttype type;
2940 tok = cp_lexer_peek_token (parser->lexer);
2941 if (!cp_parser_is_string_literal (tok))
2943 cp_parser_error (parser, "expected string-literal");
2944 return error_mark_node;
2949 /* Try to avoid the overhead of creating and destroying an obstack
2950 for the common case of just one string. */
2951 if (!cp_parser_is_string_literal
2952 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2954 cp_lexer_consume_token (parser->lexer);
2956 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2957 str.len = TREE_STRING_LENGTH (tok->u.value);
2964 gcc_obstack_init (&str_ob);
2969 cp_lexer_consume_token (parser->lexer);
2971 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2972 str.len = TREE_STRING_LENGTH (tok->u.value);
2974 if (type != tok->type)
2976 if (type == CPP_STRING)
2978 else if (tok->type != CPP_STRING)
2979 error_at (tok->location,
2980 "unsupported non-standard concatenation "
2981 "of string literals");
2984 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2986 tok = cp_lexer_peek_token (parser->lexer);
2988 while (cp_parser_is_string_literal (tok));
2990 strs = (cpp_string *) obstack_finish (&str_ob);
2993 if (type != CPP_STRING && !wide_ok)
2995 cp_parser_error (parser, "a wide string is invalid in this context");
2999 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3000 (parse_in, strs, count, &istr, type))
3002 value = build_string (istr.len, (const char *)istr.text);
3003 free (CONST_CAST (unsigned char *, istr.text));
3009 case CPP_UTF8STRING:
3010 TREE_TYPE (value) = char_array_type_node;
3013 TREE_TYPE (value) = char16_array_type_node;
3016 TREE_TYPE (value) = char32_array_type_node;
3019 TREE_TYPE (value) = wchar_array_type_node;
3023 value = fix_string_type (value);
3026 /* cpp_interpret_string has issued an error. */
3027 value = error_mark_node;
3030 obstack_free (&str_ob, 0);
3036 /* Basic concepts [gram.basic] */
3038 /* Parse a translation-unit.
3041 declaration-seq [opt]
3043 Returns TRUE if all went well. */
3046 cp_parser_translation_unit (cp_parser* parser)
3048 /* The address of the first non-permanent object on the declarator
3050 static void *declarator_obstack_base;
3054 /* Create the declarator obstack, if necessary. */
3055 if (!cp_error_declarator)
3057 gcc_obstack_init (&declarator_obstack);
3058 /* Create the error declarator. */
3059 cp_error_declarator = make_declarator (cdk_error);
3060 /* Create the empty parameter list. */
3061 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3062 /* Remember where the base of the declarator obstack lies. */
3063 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3066 cp_parser_declaration_seq_opt (parser);
3068 /* If there are no tokens left then all went well. */
3069 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3071 /* Get rid of the token array; we don't need it any more. */
3072 cp_lexer_destroy (parser->lexer);
3073 parser->lexer = NULL;
3075 /* This file might have been a context that's implicitly extern
3076 "C". If so, pop the lang context. (Only relevant for PCH.) */
3077 if (parser->implicit_extern_c)
3079 pop_lang_context ();
3080 parser->implicit_extern_c = false;
3084 finish_translation_unit ();
3090 cp_parser_error (parser, "expected declaration");
3094 /* Make sure the declarator obstack was fully cleaned up. */
3095 gcc_assert (obstack_next_free (&declarator_obstack)
3096 == declarator_obstack_base);
3098 /* All went well. */
3102 /* Expressions [gram.expr] */
3104 /* Parse a primary-expression.
3115 ( compound-statement )
3116 __builtin_va_arg ( assignment-expression , type-id )
3117 __builtin_offsetof ( type-id , offsetof-expression )
3120 __has_nothrow_assign ( type-id )
3121 __has_nothrow_constructor ( type-id )
3122 __has_nothrow_copy ( type-id )
3123 __has_trivial_assign ( type-id )
3124 __has_trivial_constructor ( type-id )
3125 __has_trivial_copy ( type-id )
3126 __has_trivial_destructor ( type-id )
3127 __has_virtual_destructor ( type-id )
3128 __is_abstract ( type-id )
3129 __is_base_of ( type-id , type-id )
3130 __is_class ( type-id )
3131 __is_convertible_to ( type-id , type-id )
3132 __is_empty ( type-id )
3133 __is_enum ( type-id )
3134 __is_pod ( type-id )
3135 __is_polymorphic ( type-id )
3136 __is_union ( type-id )
3138 Objective-C++ Extension:
3146 ADDRESS_P is true iff this expression was immediately preceded by
3147 "&" and therefore might denote a pointer-to-member. CAST_P is true
3148 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3149 true iff this expression is a template argument.
3151 Returns a representation of the expression. Upon return, *IDK
3152 indicates what kind of id-expression (if any) was present. */
3155 cp_parser_primary_expression (cp_parser *parser,
3158 bool template_arg_p,
3161 cp_token *token = NULL;
3163 /* Assume the primary expression is not an id-expression. */
3164 *idk = CP_ID_KIND_NONE;
3166 /* Peek at the next token. */
3167 token = cp_lexer_peek_token (parser->lexer);
3168 switch (token->type)
3181 token = cp_lexer_consume_token (parser->lexer);
3182 if (TREE_CODE (token->u.value) == FIXED_CST)
3184 error_at (token->location,
3185 "fixed-point types not supported in C++");
3186 return error_mark_node;
3188 /* Floating-point literals are only allowed in an integral
3189 constant expression if they are cast to an integral or
3190 enumeration type. */
3191 if (TREE_CODE (token->u.value) == REAL_CST
3192 && parser->integral_constant_expression_p
3195 /* CAST_P will be set even in invalid code like "int(2.7 +
3196 ...)". Therefore, we have to check that the next token
3197 is sure to end the cast. */
3200 cp_token *next_token;
3202 next_token = cp_lexer_peek_token (parser->lexer);
3203 if (/* The comma at the end of an
3204 enumerator-definition. */
3205 next_token->type != CPP_COMMA
3206 /* The curly brace at the end of an enum-specifier. */
3207 && next_token->type != CPP_CLOSE_BRACE
3208 /* The end of a statement. */
3209 && next_token->type != CPP_SEMICOLON
3210 /* The end of the cast-expression. */
3211 && next_token->type != CPP_CLOSE_PAREN
3212 /* The end of an array bound. */
3213 && next_token->type != CPP_CLOSE_SQUARE
3214 /* The closing ">" in a template-argument-list. */
3215 && (next_token->type != CPP_GREATER
3216 || parser->greater_than_is_operator_p)
3217 /* C++0x only: A ">>" treated like two ">" tokens,
3218 in a template-argument-list. */
3219 && (next_token->type != CPP_RSHIFT
3220 || (cxx_dialect == cxx98)
3221 || parser->greater_than_is_operator_p))
3225 /* If we are within a cast, then the constraint that the
3226 cast is to an integral or enumeration type will be
3227 checked at that point. If we are not within a cast, then
3228 this code is invalid. */
3230 cp_parser_non_integral_constant_expression
3231 (parser, "floating-point literal");
3233 return token->u.value;
3239 case CPP_UTF8STRING:
3240 /* ??? Should wide strings be allowed when parser->translate_strings_p
3241 is false (i.e. in attributes)? If not, we can kill the third
3242 argument to cp_parser_string_literal. */
3243 return cp_parser_string_literal (parser,
3244 parser->translate_strings_p,
3247 case CPP_OPEN_PAREN:
3250 bool saved_greater_than_is_operator_p;
3252 /* Consume the `('. */
3253 cp_lexer_consume_token (parser->lexer);
3254 /* Within a parenthesized expression, a `>' token is always
3255 the greater-than operator. */
3256 saved_greater_than_is_operator_p
3257 = parser->greater_than_is_operator_p;
3258 parser->greater_than_is_operator_p = true;
3259 /* If we see `( { ' then we are looking at the beginning of
3260 a GNU statement-expression. */
3261 if (cp_parser_allow_gnu_extensions_p (parser)
3262 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3264 /* Statement-expressions are not allowed by the standard. */
3265 pedwarn (token->location, OPT_pedantic,
3266 "ISO C++ forbids braced-groups within expressions");
3268 /* And they're not allowed outside of a function-body; you
3269 cannot, for example, write:
3271 int i = ({ int j = 3; j + 1; });
3273 at class or namespace scope. */
3274 if (!parser->in_function_body
3275 || parser->in_template_argument_list_p)
3277 error_at (token->location,
3278 "statement-expressions are not allowed outside "
3279 "functions nor in template-argument lists");
3280 cp_parser_skip_to_end_of_block_or_statement (parser);
3281 expr = error_mark_node;
3285 /* Start the statement-expression. */
3286 expr = begin_stmt_expr ();
3287 /* Parse the compound-statement. */
3288 cp_parser_compound_statement (parser, expr, false);
3290 expr = finish_stmt_expr (expr, false);
3295 /* Parse the parenthesized expression. */
3296 expr = cp_parser_expression (parser, cast_p, idk);
3297 /* Let the front end know that this expression was
3298 enclosed in parentheses. This matters in case, for
3299 example, the expression is of the form `A::B', since
3300 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3302 finish_parenthesized_expr (expr);
3304 /* The `>' token might be the end of a template-id or
3305 template-parameter-list now. */
3306 parser->greater_than_is_operator_p
3307 = saved_greater_than_is_operator_p;
3308 /* Consume the `)'. */
3309 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3310 cp_parser_skip_to_end_of_statement (parser);
3315 case CPP_OPEN_SQUARE:
3316 if (c_dialect_objc ())
3317 /* We have an Objective-C++ message. */
3318 return cp_parser_objc_expression (parser);
3319 maybe_warn_cpp0x ("lambda expressions");
3320 return cp_parser_lambda_expression (parser);
3322 case CPP_OBJC_STRING:
3323 if (c_dialect_objc ())
3324 /* We have an Objective-C++ string literal. */
3325 return cp_parser_objc_expression (parser);
3326 cp_parser_error (parser, "expected primary-expression");
3327 return error_mark_node;
3330 switch (token->keyword)
3332 /* These two are the boolean literals. */
3334 cp_lexer_consume_token (parser->lexer);
3335 return boolean_true_node;
3337 cp_lexer_consume_token (parser->lexer);
3338 return boolean_false_node;
3340 /* The `__null' literal. */
3342 cp_lexer_consume_token (parser->lexer);
3345 /* Recognize the `this' keyword. */
3347 cp_lexer_consume_token (parser->lexer);
3348 if (parser->local_variables_forbidden_p)
3350 error_at (token->location,
3351 "%<this%> may not be used in this context");
3352 return error_mark_node;
3354 /* Pointers cannot appear in constant-expressions. */
3355 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3356 return error_mark_node;
3357 return finish_this_expr ();
3359 /* The `operator' keyword can be the beginning of an
3364 case RID_FUNCTION_NAME:
3365 case RID_PRETTY_FUNCTION_NAME:
3366 case RID_C99_FUNCTION_NAME:
3370 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3371 __func__ are the names of variables -- but they are
3372 treated specially. Therefore, they are handled here,
3373 rather than relying on the generic id-expression logic
3374 below. Grammatically, these names are id-expressions.
3376 Consume the token. */
3377 token = cp_lexer_consume_token (parser->lexer);
3379 switch (token->keyword)
3381 case RID_FUNCTION_NAME:
3382 name = "%<__FUNCTION__%>";
3384 case RID_PRETTY_FUNCTION_NAME:
3385 name = "%<__PRETTY_FUNCTION__%>";
3387 case RID_C99_FUNCTION_NAME:
3388 name = "%<__func__%>";
3394 if (cp_parser_non_integral_constant_expression (parser, name))
3395 return error_mark_node;
3397 /* Look up the name. */
3398 return finish_fname (token->u.value);
3406 /* The `__builtin_va_arg' construct is used to handle
3407 `va_arg'. Consume the `__builtin_va_arg' token. */
3408 cp_lexer_consume_token (parser->lexer);
3409 /* Look for the opening `('. */
3410 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3411 /* Now, parse the assignment-expression. */
3412 expression = cp_parser_assignment_expression (parser,
3413 /*cast_p=*/false, NULL);
3414 /* Look for the `,'. */
3415 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3416 /* Parse the type-id. */
3417 type = cp_parser_type_id (parser);
3418 /* Look for the closing `)'. */
3419 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3420 /* Using `va_arg' in a constant-expression is not
3422 if (cp_parser_non_integral_constant_expression (parser,
3424 return error_mark_node;
3425 return build_x_va_arg (expression, type);
3429 return cp_parser_builtin_offsetof (parser);
3431 case RID_HAS_NOTHROW_ASSIGN:
3432 case RID_HAS_NOTHROW_CONSTRUCTOR:
3433 case RID_HAS_NOTHROW_COPY:
3434 case RID_HAS_TRIVIAL_ASSIGN:
3435 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3436 case RID_HAS_TRIVIAL_COPY:
3437 case RID_HAS_TRIVIAL_DESTRUCTOR:
3438 case RID_HAS_VIRTUAL_DESTRUCTOR:
3439 case RID_IS_ABSTRACT:
3440 case RID_IS_BASE_OF:
3442 case RID_IS_CONVERTIBLE_TO:
3446 case RID_IS_POLYMORPHIC:
3447 case RID_IS_STD_LAYOUT:
3448 case RID_IS_TRIVIAL:
3450 return cp_parser_trait_expr (parser, token->keyword);
3452 /* Objective-C++ expressions. */
3454 case RID_AT_PROTOCOL:
3455 case RID_AT_SELECTOR:
3456 return cp_parser_objc_expression (parser);
3459 cp_parser_error (parser, "expected primary-expression");
3460 return error_mark_node;
3463 /* An id-expression can start with either an identifier, a
3464 `::' as the beginning of a qualified-id, or the "operator"
3468 case CPP_TEMPLATE_ID:
3469 case CPP_NESTED_NAME_SPECIFIER:
3473 const char *error_msg;
3476 cp_token *id_expr_token;
3479 /* Parse the id-expression. */
3481 = cp_parser_id_expression (parser,
3482 /*template_keyword_p=*/false,
3483 /*check_dependency_p=*/true,
3485 /*declarator_p=*/false,
3486 /*optional_p=*/false);
3487 if (id_expression == error_mark_node)
3488 return error_mark_node;
3489 id_expr_token = token;
3490 token = cp_lexer_peek_token (parser->lexer);
3491 done = (token->type != CPP_OPEN_SQUARE
3492 && token->type != CPP_OPEN_PAREN
3493 && token->type != CPP_DOT
3494 && token->type != CPP_DEREF
3495 && token->type != CPP_PLUS_PLUS
3496 && token->type != CPP_MINUS_MINUS);
3497 /* If we have a template-id, then no further lookup is
3498 required. If the template-id was for a template-class, we
3499 will sometimes have a TYPE_DECL at this point. */
3500 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3501 || TREE_CODE (id_expression) == TYPE_DECL)
3502 decl = id_expression;
3503 /* Look up the name. */
3506 tree ambiguous_decls;
3508 decl = cp_parser_lookup_name (parser, id_expression,
3511 /*is_namespace=*/false,
3512 /*check_dependency=*/true,
3514 id_expr_token->location);
3515 /* If the lookup was ambiguous, an error will already have
3517 if (ambiguous_decls)
3518 return error_mark_node;
3520 /* In Objective-C++, an instance variable (ivar) may be preferred
3521 to whatever cp_parser_lookup_name() found. */
3522 decl = objc_lookup_ivar (decl, id_expression);
3524 /* If name lookup gives us a SCOPE_REF, then the
3525 qualifying scope was dependent. */
3526 if (TREE_CODE (decl) == SCOPE_REF)
3528 /* At this point, we do not know if DECL is a valid
3529 integral constant expression. We assume that it is
3530 in fact such an expression, so that code like:
3532 template <int N> struct A {
3536 is accepted. At template-instantiation time, we
3537 will check that B<N>::i is actually a constant. */
3540 /* Check to see if DECL is a local variable in a context
3541 where that is forbidden. */
3542 if (parser->local_variables_forbidden_p
3543 && local_variable_p (decl))
3545 /* It might be that we only found DECL because we are
3546 trying to be generous with pre-ISO scoping rules.
3547 For example, consider:
3551 for (int i = 0; i < 10; ++i) {}
3552 extern void f(int j = i);
3555 Here, name look up will originally find the out
3556 of scope `i'. We need to issue a warning message,
3557 but then use the global `i'. */
3558 decl = check_for_out_of_scope_variable (decl);
3559 if (local_variable_p (decl))
3561 error_at (id_expr_token->location,
3562 "local variable %qD may not appear in this context",
3564 return error_mark_node;
3569 decl = (finish_id_expression
3570 (id_expression, decl, parser->scope,
3572 parser->integral_constant_expression_p,
3573 parser->allow_non_integral_constant_expression_p,
3574 &parser->non_integral_constant_expression_p,
3575 template_p, done, address_p,
3578 id_expr_token->location));
3580 cp_parser_error (parser, error_msg);
3584 /* Anything else is an error. */
3586 cp_parser_error (parser, "expected primary-expression");
3587 return error_mark_node;
3591 /* Parse an id-expression.
3598 :: [opt] nested-name-specifier template [opt] unqualified-id
3600 :: operator-function-id
3603 Return a representation of the unqualified portion of the
3604 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3605 a `::' or nested-name-specifier.
3607 Often, if the id-expression was a qualified-id, the caller will
3608 want to make a SCOPE_REF to represent the qualified-id. This
3609 function does not do this in order to avoid wastefully creating
3610 SCOPE_REFs when they are not required.
3612 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3615 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3616 uninstantiated templates.
3618 If *TEMPLATE_P is non-NULL, it is set to true iff the
3619 `template' keyword is used to explicitly indicate that the entity
3620 named is a template.
3622 If DECLARATOR_P is true, the id-expression is appearing as part of
3623 a declarator, rather than as part of an expression. */
3626 cp_parser_id_expression (cp_parser *parser,
3627 bool template_keyword_p,
3628 bool check_dependency_p,
3633 bool global_scope_p;
3634 bool nested_name_specifier_p;
3636 /* Assume the `template' keyword was not used. */
3638 *template_p = template_keyword_p;
3640 /* Look for the optional `::' operator. */
3642 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3644 /* Look for the optional nested-name-specifier. */
3645 nested_name_specifier_p
3646 = (cp_parser_nested_name_specifier_opt (parser,
3647 /*typename_keyword_p=*/false,
3652 /* If there is a nested-name-specifier, then we are looking at
3653 the first qualified-id production. */
3654 if (nested_name_specifier_p)
3657 tree saved_object_scope;
3658 tree saved_qualifying_scope;
3659 tree unqualified_id;
3662 /* See if the next token is the `template' keyword. */
3664 template_p = &is_template;
3665 *template_p = cp_parser_optional_template_keyword (parser);
3666 /* Name lookup we do during the processing of the
3667 unqualified-id might obliterate SCOPE. */
3668 saved_scope = parser->scope;
3669 saved_object_scope = parser->object_scope;
3670 saved_qualifying_scope = parser->qualifying_scope;
3671 /* Process the final unqualified-id. */
3672 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3675 /*optional_p=*/false);
3676 /* Restore the SAVED_SCOPE for our caller. */
3677 parser->scope = saved_scope;
3678 parser->object_scope = saved_object_scope;
3679 parser->qualifying_scope = saved_qualifying_scope;
3681 return unqualified_id;
3683 /* Otherwise, if we are in global scope, then we are looking at one
3684 of the other qualified-id productions. */
3685 else if (global_scope_p)
3690 /* Peek at the next token. */
3691 token = cp_lexer_peek_token (parser->lexer);
3693 /* If it's an identifier, and the next token is not a "<", then
3694 we can avoid the template-id case. This is an optimization
3695 for this common case. */
3696 if (token->type == CPP_NAME
3697 && !cp_parser_nth_token_starts_template_argument_list_p
3699 return cp_parser_identifier (parser);
3701 cp_parser_parse_tentatively (parser);
3702 /* Try a template-id. */
3703 id = cp_parser_template_id (parser,
3704 /*template_keyword_p=*/false,
3705 /*check_dependency_p=*/true,
3707 /* If that worked, we're done. */
3708 if (cp_parser_parse_definitely (parser))
3711 /* Peek at the next token. (Changes in the token buffer may
3712 have invalidated the pointer obtained above.) */
3713 token = cp_lexer_peek_token (parser->lexer);
3715 switch (token->type)
3718 return cp_parser_identifier (parser);
3721 if (token->keyword == RID_OPERATOR)
3722 return cp_parser_operator_function_id (parser);
3726 cp_parser_error (parser, "expected id-expression");
3727 return error_mark_node;
3731 return cp_parser_unqualified_id (parser, template_keyword_p,
3732 /*check_dependency_p=*/true,
3737 /* Parse an unqualified-id.
3741 operator-function-id
3742 conversion-function-id
3746 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3747 keyword, in a construct like `A::template ...'.
3749 Returns a representation of unqualified-id. For the `identifier'
3750 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3751 production a BIT_NOT_EXPR is returned; the operand of the
3752 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3753 other productions, see the documentation accompanying the
3754 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3755 names are looked up in uninstantiated templates. If DECLARATOR_P
3756 is true, the unqualified-id is appearing as part of a declarator,
3757 rather than as part of an expression. */
3760 cp_parser_unqualified_id (cp_parser* parser,
3761 bool template_keyword_p,
3762 bool check_dependency_p,
3768 /* Peek at the next token. */
3769 token = cp_lexer_peek_token (parser->lexer);
3771 switch (token->type)
3777 /* We don't know yet whether or not this will be a
3779 cp_parser_parse_tentatively (parser);
3780 /* Try a template-id. */
3781 id = cp_parser_template_id (parser, template_keyword_p,
3784 /* If it worked, we're done. */
3785 if (cp_parser_parse_definitely (parser))
3787 /* Otherwise, it's an ordinary identifier. */
3788 return cp_parser_identifier (parser);
3791 case CPP_TEMPLATE_ID:
3792 return cp_parser_template_id (parser, template_keyword_p,
3799 tree qualifying_scope;
3804 /* Consume the `~' token. */
3805 cp_lexer_consume_token (parser->lexer);
3806 /* Parse the class-name. The standard, as written, seems to
3809 template <typename T> struct S { ~S (); };
3810 template <typename T> S<T>::~S() {}
3812 is invalid, since `~' must be followed by a class-name, but
3813 `S<T>' is dependent, and so not known to be a class.
3814 That's not right; we need to look in uninstantiated
3815 templates. A further complication arises from:
3817 template <typename T> void f(T t) {
3821 Here, it is not possible to look up `T' in the scope of `T'
3822 itself. We must look in both the current scope, and the
3823 scope of the containing complete expression.
3825 Yet another issue is:
3834 The standard does not seem to say that the `S' in `~S'
3835 should refer to the type `S' and not the data member
3838 /* DR 244 says that we look up the name after the "~" in the
3839 same scope as we looked up the qualifying name. That idea
3840 isn't fully worked out; it's more complicated than that. */
3841 scope = parser->scope;
3842 object_scope = parser->object_scope;
3843 qualifying_scope = parser->qualifying_scope;
3845 /* Check for invalid scopes. */
3846 if (scope == error_mark_node)
3848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3849 cp_lexer_consume_token (parser->lexer);
3850 return error_mark_node;
3852 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3854 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3855 error_at (token->location,
3856 "scope %qT before %<~%> is not a class-name",
3858 cp_parser_simulate_error (parser);
3859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3860 cp_lexer_consume_token (parser->lexer);
3861 return error_mark_node;
3863 gcc_assert (!scope || TYPE_P (scope));
3865 /* If the name is of the form "X::~X" it's OK. */
3866 token = cp_lexer_peek_token (parser->lexer);
3868 && token->type == CPP_NAME
3869 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3871 && constructor_name_p (token->u.value, scope))
3873 cp_lexer_consume_token (parser->lexer);
3874 return build_nt (BIT_NOT_EXPR, scope);
3877 /* If there was an explicit qualification (S::~T), first look
3878 in the scope given by the qualification (i.e., S). */
3880 type_decl = NULL_TREE;
3883 cp_parser_parse_tentatively (parser);
3884 type_decl = cp_parser_class_name (parser,
3885 /*typename_keyword_p=*/false,
3886 /*template_keyword_p=*/false,
3888 /*check_dependency=*/false,
3889 /*class_head_p=*/false,
3891 if (cp_parser_parse_definitely (parser))
3894 /* In "N::S::~S", look in "N" as well. */
3895 if (!done && scope && qualifying_scope)
3897 cp_parser_parse_tentatively (parser);
3898 parser->scope = qualifying_scope;
3899 parser->object_scope = NULL_TREE;
3900 parser->qualifying_scope = NULL_TREE;
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 (cp_parser_parse_definitely (parser))
3912 /* In "p->S::~T", look in the scope given by "*p" as well. */
3913 else if (!done && object_scope)
3915 cp_parser_parse_tentatively (parser);
3916 parser->scope = object_scope;
3917 parser->object_scope = NULL_TREE;
3918 parser->qualifying_scope = NULL_TREE;
3920 = cp_parser_class_name (parser,
3921 /*typename_keyword_p=*/false,
3922 /*template_keyword_p=*/false,
3924 /*check_dependency=*/false,
3925 /*class_head_p=*/false,
3927 if (cp_parser_parse_definitely (parser))
3930 /* Look in the surrounding context. */
3933 parser->scope = NULL_TREE;
3934 parser->object_scope = NULL_TREE;
3935 parser->qualifying_scope = NULL_TREE;
3936 if (processing_template_decl)
3937 cp_parser_parse_tentatively (parser);
3939 = cp_parser_class_name (parser,
3940 /*typename_keyword_p=*/false,
3941 /*template_keyword_p=*/false,
3943 /*check_dependency=*/false,
3944 /*class_head_p=*/false,
3946 if (processing_template_decl
3947 && ! cp_parser_parse_definitely (parser))
3949 /* We couldn't find a type with this name, so just accept
3950 it and check for a match at instantiation time. */
3951 type_decl = cp_parser_identifier (parser);
3952 if (type_decl != error_mark_node)
3953 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3957 /* If an error occurred, assume that the name of the
3958 destructor is the same as the name of the qualifying
3959 class. That allows us to keep parsing after running
3960 into ill-formed destructor names. */
3961 if (type_decl == error_mark_node && scope)
3962 return build_nt (BIT_NOT_EXPR, scope);
3963 else if (type_decl == error_mark_node)
3964 return error_mark_node;
3966 /* Check that destructor name and scope match. */
3967 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3969 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3970 error_at (token->location,
3971 "declaration of %<~%T%> as member of %qT",
3973 cp_parser_simulate_error (parser);
3974 return error_mark_node;
3979 A typedef-name that names a class shall not be used as the
3980 identifier in the declarator for a destructor declaration. */
3982 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3983 && !DECL_SELF_REFERENCE_P (type_decl)
3984 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3985 error_at (token->location,
3986 "typedef-name %qD used as destructor declarator",
3989 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3993 if (token->keyword == RID_OPERATOR)
3997 /* This could be a template-id, so we try that first. */
3998 cp_parser_parse_tentatively (parser);
3999 /* Try a template-id. */
4000 id = cp_parser_template_id (parser, template_keyword_p,
4001 /*check_dependency_p=*/true,
4003 /* If that worked, we're done. */
4004 if (cp_parser_parse_definitely (parser))
4006 /* We still don't know whether we're looking at an
4007 operator-function-id or a conversion-function-id. */
4008 cp_parser_parse_tentatively (parser);
4009 /* Try an operator-function-id. */
4010 id = cp_parser_operator_function_id (parser);
4011 /* If that didn't work, try a conversion-function-id. */
4012 if (!cp_parser_parse_definitely (parser))
4013 id = cp_parser_conversion_function_id (parser);
4022 cp_parser_error (parser, "expected unqualified-id");
4023 return error_mark_node;
4027 /* Parse an (optional) nested-name-specifier.
4029 nested-name-specifier: [C++98]
4030 class-or-namespace-name :: nested-name-specifier [opt]
4031 class-or-namespace-name :: template nested-name-specifier [opt]
4033 nested-name-specifier: [C++0x]
4036 nested-name-specifier identifier ::
4037 nested-name-specifier template [opt] simple-template-id ::
4039 PARSER->SCOPE should be set appropriately before this function is
4040 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4041 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4044 Sets PARSER->SCOPE to the class (TYPE) or namespace
4045 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4046 it unchanged if there is no nested-name-specifier. Returns the new
4047 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4049 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4050 part of a declaration and/or decl-specifier. */
4053 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4054 bool typename_keyword_p,
4055 bool check_dependency_p,
4057 bool is_declaration)
4059 bool success = false;
4060 cp_token_position start = 0;
4063 /* Remember where the nested-name-specifier starts. */
4064 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4066 start = cp_lexer_token_position (parser->lexer, false);
4067 push_deferring_access_checks (dk_deferred);
4074 tree saved_qualifying_scope;
4075 bool template_keyword_p;
4077 /* Spot cases that cannot be the beginning of a
4078 nested-name-specifier. */
4079 token = cp_lexer_peek_token (parser->lexer);
4081 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4082 the already parsed nested-name-specifier. */
4083 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4085 /* Grab the nested-name-specifier and continue the loop. */
4086 cp_parser_pre_parsed_nested_name_specifier (parser);
4087 /* If we originally encountered this nested-name-specifier
4088 with IS_DECLARATION set to false, we will not have
4089 resolved TYPENAME_TYPEs, so we must do so here. */
4091 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4093 new_scope = resolve_typename_type (parser->scope,
4094 /*only_current_p=*/false);
4095 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4096 parser->scope = new_scope;
4102 /* Spot cases that cannot be the beginning of a
4103 nested-name-specifier. On the second and subsequent times
4104 through the loop, we look for the `template' keyword. */
4105 if (success && token->keyword == RID_TEMPLATE)
4107 /* A template-id can start a nested-name-specifier. */
4108 else if (token->type == CPP_TEMPLATE_ID)
4112 /* If the next token is not an identifier, then it is
4113 definitely not a type-name or namespace-name. */
4114 if (token->type != CPP_NAME)
4116 /* If the following token is neither a `<' (to begin a
4117 template-id), nor a `::', then we are not looking at a
4118 nested-name-specifier. */
4119 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4120 if (token->type != CPP_SCOPE
4121 && !cp_parser_nth_token_starts_template_argument_list_p
4126 /* The nested-name-specifier is optional, so we parse
4128 cp_parser_parse_tentatively (parser);
4130 /* Look for the optional `template' keyword, if this isn't the
4131 first time through the loop. */
4133 template_keyword_p = cp_parser_optional_template_keyword (parser);
4135 template_keyword_p = false;
4137 /* Save the old scope since the name lookup we are about to do
4138 might destroy it. */
4139 old_scope = parser->scope;
4140 saved_qualifying_scope = parser->qualifying_scope;
4141 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4142 look up names in "X<T>::I" in order to determine that "Y" is
4143 a template. So, if we have a typename at this point, we make
4144 an effort to look through it. */
4146 && !typename_keyword_p
4148 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4149 parser->scope = resolve_typename_type (parser->scope,
4150 /*only_current_p=*/false);
4151 /* Parse the qualifying entity. */
4153 = cp_parser_qualifying_entity (parser,
4159 /* Look for the `::' token. */
4160 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4162 /* If we found what we wanted, we keep going; otherwise, we're
4164 if (!cp_parser_parse_definitely (parser))
4166 bool error_p = false;
4168 /* Restore the OLD_SCOPE since it was valid before the
4169 failed attempt at finding the last
4170 class-or-namespace-name. */
4171 parser->scope = old_scope;
4172 parser->qualifying_scope = saved_qualifying_scope;
4173 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4175 /* If the next token is an identifier, and the one after
4176 that is a `::', then any valid interpretation would have
4177 found a class-or-namespace-name. */
4178 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4179 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4181 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4184 token = cp_lexer_consume_token (parser->lexer);
4187 if (!token->ambiguous_p)
4190 tree ambiguous_decls;
4192 decl = cp_parser_lookup_name (parser, token->u.value,
4194 /*is_template=*/false,
4195 /*is_namespace=*/false,
4196 /*check_dependency=*/true,
4199 if (TREE_CODE (decl) == TEMPLATE_DECL)
4200 error_at (token->location,
4201 "%qD used without template parameters",
4203 else if (ambiguous_decls)
4205 error_at (token->location,
4206 "reference to %qD is ambiguous",
4208 print_candidates (ambiguous_decls);
4209 decl = error_mark_node;
4213 const char* msg = "is not a class or namespace";
4214 if (cxx_dialect != cxx98)
4215 msg = "is not a class, namespace, or enumeration";
4216 cp_parser_name_lookup_error
4217 (parser, token->u.value, decl, msg,
4221 parser->scope = error_mark_node;
4223 /* Treat this as a successful nested-name-specifier
4228 If the name found is not a class-name (clause
4229 _class_) or namespace-name (_namespace.def_), the
4230 program is ill-formed. */
4233 cp_lexer_consume_token (parser->lexer);
4237 /* We've found one valid nested-name-specifier. */
4239 /* Name lookup always gives us a DECL. */
4240 if (TREE_CODE (new_scope) == TYPE_DECL)
4241 new_scope = TREE_TYPE (new_scope);
4242 /* Uses of "template" must be followed by actual templates. */
4243 if (template_keyword_p
4244 && !(CLASS_TYPE_P (new_scope)
4245 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4246 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4247 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4248 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4249 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4250 == TEMPLATE_ID_EXPR)))
4251 permerror (input_location, TYPE_P (new_scope)
4252 ? "%qT is not a template"
4253 : "%qD is not a template",
4255 /* If it is a class scope, try to complete it; we are about to
4256 be looking up names inside the class. */
4257 if (TYPE_P (new_scope)
4258 /* Since checking types for dependency can be expensive,
4259 avoid doing it if the type is already complete. */
4260 && !COMPLETE_TYPE_P (new_scope)
4261 /* Do not try to complete dependent types. */
4262 && !dependent_type_p (new_scope))
4264 new_scope = complete_type (new_scope);
4265 /* If it is a typedef to current class, use the current
4266 class instead, as the typedef won't have any names inside
4268 if (!COMPLETE_TYPE_P (new_scope)
4269 && currently_open_class (new_scope))
4270 new_scope = TYPE_MAIN_VARIANT (new_scope);
4272 /* Make sure we look in the right scope the next time through
4274 parser->scope = new_scope;
4277 /* If parsing tentatively, replace the sequence of tokens that makes
4278 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4279 token. That way, should we re-parse the token stream, we will
4280 not have to repeat the effort required to do the parse, nor will
4281 we issue duplicate error messages. */
4282 if (success && start)
4286 token = cp_lexer_token_at (parser->lexer, start);
4287 /* Reset the contents of the START token. */
4288 token->type = CPP_NESTED_NAME_SPECIFIER;
4289 /* Retrieve any deferred checks. Do not pop this access checks yet
4290 so the memory will not be reclaimed during token replacing below. */
4291 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4292 token->u.tree_check_value->value = parser->scope;
4293 token->u.tree_check_value->checks = get_deferred_access_checks ();
4294 token->u.tree_check_value->qualifying_scope =
4295 parser->qualifying_scope;
4296 token->keyword = RID_MAX;
4298 /* Purge all subsequent tokens. */
4299 cp_lexer_purge_tokens_after (parser->lexer, start);
4303 pop_to_parent_deferring_access_checks ();
4305 return success ? parser->scope : NULL_TREE;
4308 /* Parse a nested-name-specifier. See
4309 cp_parser_nested_name_specifier_opt for details. This function
4310 behaves identically, except that it will an issue an error if no
4311 nested-name-specifier is present. */
4314 cp_parser_nested_name_specifier (cp_parser *parser,
4315 bool typename_keyword_p,
4316 bool check_dependency_p,
4318 bool is_declaration)
4322 /* Look for the nested-name-specifier. */
4323 scope = cp_parser_nested_name_specifier_opt (parser,
4328 /* If it was not present, issue an error message. */
4331 cp_parser_error (parser, "expected nested-name-specifier");
4332 parser->scope = NULL_TREE;
4338 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4339 this is either a class-name or a namespace-name (which corresponds
4340 to the class-or-namespace-name production in the grammar). For
4341 C++0x, it can also be a type-name that refers to an enumeration
4344 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4345 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4346 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4347 TYPE_P is TRUE iff the next name should be taken as a class-name,
4348 even the same name is declared to be another entity in the same
4351 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4352 specified by the class-or-namespace-name. If neither is found the
4353 ERROR_MARK_NODE is returned. */
4356 cp_parser_qualifying_entity (cp_parser *parser,
4357 bool typename_keyword_p,
4358 bool template_keyword_p,
4359 bool check_dependency_p,
4361 bool is_declaration)
4364 tree saved_qualifying_scope;
4365 tree saved_object_scope;
4368 bool successful_parse_p;
4370 /* Before we try to parse the class-name, we must save away the
4371 current PARSER->SCOPE since cp_parser_class_name will destroy
4373 saved_scope = parser->scope;
4374 saved_qualifying_scope = parser->qualifying_scope;
4375 saved_object_scope = parser->object_scope;
4376 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4377 there is no need to look for a namespace-name. */
4378 only_class_p = template_keyword_p
4379 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4381 cp_parser_parse_tentatively (parser);
4382 scope = cp_parser_class_name (parser,
4385 type_p ? class_type : none_type,
4387 /*class_head_p=*/false,
4389 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4390 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4392 && cxx_dialect != cxx98
4393 && !successful_parse_p)
4395 /* Restore the saved scope. */
4396 parser->scope = saved_scope;
4397 parser->qualifying_scope = saved_qualifying_scope;
4398 parser->object_scope = saved_object_scope;
4400 /* Parse tentatively. */
4401 cp_parser_parse_tentatively (parser);
4403 /* Parse a typedef-name or enum-name. */
4404 scope = cp_parser_nonclass_name (parser);
4405 successful_parse_p = cp_parser_parse_definitely (parser);
4407 /* If that didn't work, try for a namespace-name. */
4408 if (!only_class_p && !successful_parse_p)
4410 /* Restore the saved scope. */
4411 parser->scope = saved_scope;
4412 parser->qualifying_scope = saved_qualifying_scope;
4413 parser->object_scope = saved_object_scope;
4414 /* If we are not looking at an identifier followed by the scope
4415 resolution operator, then this is not part of a
4416 nested-name-specifier. (Note that this function is only used
4417 to parse the components of a nested-name-specifier.) */
4418 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4419 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4420 return error_mark_node;
4421 scope = cp_parser_namespace_name (parser);
4427 /* Parse a postfix-expression.
4431 postfix-expression [ expression ]
4432 postfix-expression ( expression-list [opt] )
4433 simple-type-specifier ( expression-list [opt] )
4434 typename :: [opt] nested-name-specifier identifier
4435 ( expression-list [opt] )
4436 typename :: [opt] nested-name-specifier template [opt] template-id
4437 ( expression-list [opt] )
4438 postfix-expression . template [opt] id-expression
4439 postfix-expression -> template [opt] id-expression
4440 postfix-expression . pseudo-destructor-name
4441 postfix-expression -> pseudo-destructor-name
4442 postfix-expression ++
4443 postfix-expression --
4444 dynamic_cast < type-id > ( expression )
4445 static_cast < type-id > ( expression )
4446 reinterpret_cast < type-id > ( expression )
4447 const_cast < type-id > ( expression )
4448 typeid ( expression )
4454 ( type-id ) { initializer-list , [opt] }
4456 This extension is a GNU version of the C99 compound-literal
4457 construct. (The C99 grammar uses `type-name' instead of `type-id',
4458 but they are essentially the same concept.)
4460 If ADDRESS_P is true, the postfix expression is the operand of the
4461 `&' operator. CAST_P is true if this expression is the target of a
4464 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4465 class member access expressions [expr.ref].
4467 Returns a representation of the expression. */
4470 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4471 bool member_access_only_p,
4472 cp_id_kind * pidk_return)
4476 cp_id_kind idk = CP_ID_KIND_NONE;
4477 tree postfix_expression = NULL_TREE;
4478 bool is_member_access = false;
4480 /* Peek at the next token. */
4481 token = cp_lexer_peek_token (parser->lexer);
4482 /* Some of the productions are determined by keywords. */
4483 keyword = token->keyword;
4493 const char *saved_message;
4495 /* All of these can be handled in the same way from the point
4496 of view of parsing. Begin by consuming the token
4497 identifying the cast. */
4498 cp_lexer_consume_token (parser->lexer);
4500 /* New types cannot be defined in the cast. */
4501 saved_message = parser->type_definition_forbidden_message;
4502 parser->type_definition_forbidden_message
4503 = "types may not be defined in casts";
4505 /* Look for the opening `<'. */
4506 cp_parser_require (parser, CPP_LESS, "%<<%>");
4507 /* Parse the type to which we are casting. */
4508 type = cp_parser_type_id (parser);
4509 /* Look for the closing `>'. */
4510 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4511 /* Restore the old message. */
4512 parser->type_definition_forbidden_message = saved_message;
4514 /* And the expression which is being cast. */
4515 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4516 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4517 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4519 /* Only type conversions to integral or enumeration types
4520 can be used in constant-expressions. */
4521 if (!cast_valid_in_integral_constant_expression_p (type)
4522 && (cp_parser_non_integral_constant_expression
4524 "a cast to a type other than an integral or "
4525 "enumeration type")))
4526 return error_mark_node;
4532 = build_dynamic_cast (type, expression, tf_warning_or_error);
4536 = build_static_cast (type, expression, tf_warning_or_error);
4540 = build_reinterpret_cast (type, expression,
4541 tf_warning_or_error);
4545 = build_const_cast (type, expression, tf_warning_or_error);
4556 const char *saved_message;
4557 bool saved_in_type_id_in_expr_p;
4559 /* Consume the `typeid' token. */
4560 cp_lexer_consume_token (parser->lexer);
4561 /* Look for the `(' token. */
4562 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4563 /* Types cannot be defined in a `typeid' expression. */
4564 saved_message = parser->type_definition_forbidden_message;
4565 parser->type_definition_forbidden_message
4566 = "types may not be defined in a %<typeid%> expression";
4567 /* We can't be sure yet whether we're looking at a type-id or an
4569 cp_parser_parse_tentatively (parser);
4570 /* Try a type-id first. */
4571 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4572 parser->in_type_id_in_expr_p = true;
4573 type = cp_parser_type_id (parser);
4574 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4575 /* Look for the `)' token. Otherwise, we can't be sure that
4576 we're not looking at an expression: consider `typeid (int
4577 (3))', for example. */
4578 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4579 /* If all went well, simply lookup the type-id. */
4580 if (cp_parser_parse_definitely (parser))
4581 postfix_expression = get_typeid (type);
4582 /* Otherwise, fall back to the expression variant. */
4587 /* Look for an expression. */
4588 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4589 /* Compute its typeid. */
4590 postfix_expression = build_typeid (expression);
4591 /* Look for the `)' token. */
4592 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4594 /* Restore the saved message. */
4595 parser->type_definition_forbidden_message = saved_message;
4596 /* `typeid' may not appear in an integral constant expression. */
4597 if (cp_parser_non_integral_constant_expression(parser,
4598 "%<typeid%> operator"))
4599 return error_mark_node;
4606 /* The syntax permitted here is the same permitted for an
4607 elaborated-type-specifier. */
4608 type = cp_parser_elaborated_type_specifier (parser,
4609 /*is_friend=*/false,
4610 /*is_declaration=*/false);
4611 postfix_expression = cp_parser_functional_cast (parser, type);
4619 /* If the next thing is a simple-type-specifier, we may be
4620 looking at a functional cast. We could also be looking at
4621 an id-expression. So, we try the functional cast, and if
4622 that doesn't work we fall back to the primary-expression. */
4623 cp_parser_parse_tentatively (parser);
4624 /* Look for the simple-type-specifier. */
4625 type = cp_parser_simple_type_specifier (parser,
4626 /*decl_specs=*/NULL,
4627 CP_PARSER_FLAGS_NONE);
4628 /* Parse the cast itself. */
4629 if (!cp_parser_error_occurred (parser))
4631 = cp_parser_functional_cast (parser, type);
4632 /* If that worked, we're done. */
4633 if (cp_parser_parse_definitely (parser))
4636 /* If the functional-cast didn't work out, try a
4637 compound-literal. */
4638 if (cp_parser_allow_gnu_extensions_p (parser)
4639 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4641 VEC(constructor_elt,gc) *initializer_list = NULL;
4642 bool saved_in_type_id_in_expr_p;
4644 cp_parser_parse_tentatively (parser);
4645 /* Consume the `('. */
4646 cp_lexer_consume_token (parser->lexer);
4647 /* Parse the type. */
4648 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4649 parser->in_type_id_in_expr_p = true;
4650 type = cp_parser_type_id (parser);
4651 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4652 /* Look for the `)'. */
4653 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4654 /* Look for the `{'. */
4655 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4656 /* If things aren't going well, there's no need to
4658 if (!cp_parser_error_occurred (parser))
4660 bool non_constant_p;
4661 /* Parse the initializer-list. */
4663 = cp_parser_initializer_list (parser, &non_constant_p);
4664 /* Allow a trailing `,'. */
4665 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4666 cp_lexer_consume_token (parser->lexer);
4667 /* Look for the final `}'. */
4668 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4670 /* If that worked, we're definitely looking at a
4671 compound-literal expression. */
4672 if (cp_parser_parse_definitely (parser))
4674 /* Warn the user that a compound literal is not
4675 allowed in standard C++. */
4676 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4677 /* For simplicity, we disallow compound literals in
4678 constant-expressions. We could
4679 allow compound literals of integer type, whose
4680 initializer was a constant, in constant
4681 expressions. Permitting that usage, as a further
4682 extension, would not change the meaning of any
4683 currently accepted programs. (Of course, as
4684 compound literals are not part of ISO C++, the
4685 standard has nothing to say.) */
4686 if (cp_parser_non_integral_constant_expression
4687 (parser, "non-constant compound literals"))
4689 postfix_expression = error_mark_node;
4692 /* Form the representation of the compound-literal. */
4694 = (finish_compound_literal
4695 (type, build_constructor (init_list_type_node,
4696 initializer_list)));
4701 /* It must be a primary-expression. */
4703 = cp_parser_primary_expression (parser, address_p, cast_p,
4704 /*template_arg_p=*/false,
4710 /* Keep looping until the postfix-expression is complete. */
4713 if (idk == CP_ID_KIND_UNQUALIFIED
4714 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4715 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4716 /* It is not a Koenig lookup function call. */
4718 = unqualified_name_lookup_error (postfix_expression);
4720 /* Peek at the next token. */
4721 token = cp_lexer_peek_token (parser->lexer);
4723 switch (token->type)
4725 case CPP_OPEN_SQUARE:
4727 = cp_parser_postfix_open_square_expression (parser,
4730 idk = CP_ID_KIND_NONE;
4731 is_member_access = false;
4734 case CPP_OPEN_PAREN:
4735 /* postfix-expression ( expression-list [opt] ) */
4738 bool is_builtin_constant_p;
4739 bool saved_integral_constant_expression_p = false;
4740 bool saved_non_integral_constant_expression_p = false;
4743 is_member_access = false;
4745 is_builtin_constant_p
4746 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4747 if (is_builtin_constant_p)
4749 /* The whole point of __builtin_constant_p is to allow
4750 non-constant expressions to appear as arguments. */
4751 saved_integral_constant_expression_p
4752 = parser->integral_constant_expression_p;
4753 saved_non_integral_constant_expression_p
4754 = parser->non_integral_constant_expression_p;
4755 parser->integral_constant_expression_p = false;
4757 args = (cp_parser_parenthesized_expression_list
4758 (parser, /*is_attribute_list=*/false,
4759 /*cast_p=*/false, /*allow_expansion_p=*/true,
4760 /*non_constant_p=*/NULL));
4761 if (is_builtin_constant_p)
4763 parser->integral_constant_expression_p
4764 = saved_integral_constant_expression_p;
4765 parser->non_integral_constant_expression_p
4766 = saved_non_integral_constant_expression_p;
4771 postfix_expression = error_mark_node;
4775 /* Function calls are not permitted in
4776 constant-expressions. */
4777 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4778 && cp_parser_non_integral_constant_expression (parser,
4781 postfix_expression = error_mark_node;
4782 release_tree_vector (args);
4787 if (idk == CP_ID_KIND_UNQUALIFIED
4788 || idk == CP_ID_KIND_TEMPLATE_ID)
4790 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4792 if (!VEC_empty (tree, args))
4795 if (!any_type_dependent_arguments_p (args))
4797 = perform_koenig_lookup (postfix_expression, args);
4801 = unqualified_fn_lookup_error (postfix_expression);
4803 /* We do not perform argument-dependent lookup if
4804 normal lookup finds a non-function, in accordance
4805 with the expected resolution of DR 218. */
4806 else if (!VEC_empty (tree, args)
4807 && is_overloaded_fn (postfix_expression))
4809 tree fn = get_first_fn (postfix_expression);
4811 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4812 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4814 /* Only do argument dependent lookup if regular
4815 lookup does not find a set of member functions.
4816 [basic.lookup.koenig]/2a */
4817 if (!DECL_FUNCTION_MEMBER_P (fn))
4820 if (!any_type_dependent_arguments_p (args))
4822 = perform_koenig_lookup (postfix_expression, args);
4827 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4829 tree instance = TREE_OPERAND (postfix_expression, 0);
4830 tree fn = TREE_OPERAND (postfix_expression, 1);
4832 if (processing_template_decl
4833 && (type_dependent_expression_p (instance)
4834 || (!BASELINK_P (fn)
4835 && TREE_CODE (fn) != FIELD_DECL)
4836 || type_dependent_expression_p (fn)
4837 || any_type_dependent_arguments_p (args)))
4840 = build_nt_call_vec (postfix_expression, args);
4841 release_tree_vector (args);
4845 if (BASELINK_P (fn))
4848 = (build_new_method_call
4849 (instance, fn, &args, NULL_TREE,
4850 (idk == CP_ID_KIND_QUALIFIED
4851 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4853 tf_warning_or_error));
4857 = finish_call_expr (postfix_expression, &args,
4858 /*disallow_virtual=*/false,
4860 tf_warning_or_error);
4862 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4863 || TREE_CODE (postfix_expression) == MEMBER_REF
4864 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4865 postfix_expression = (build_offset_ref_call_from_tree
4866 (postfix_expression, &args));
4867 else if (idk == CP_ID_KIND_QUALIFIED)
4868 /* A call to a static class member, or a namespace-scope
4871 = finish_call_expr (postfix_expression, &args,
4872 /*disallow_virtual=*/true,
4874 tf_warning_or_error);
4876 /* All other function calls. */
4878 = finish_call_expr (postfix_expression, &args,
4879 /*disallow_virtual=*/false,
4881 tf_warning_or_error);
4883 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4884 idk = CP_ID_KIND_NONE;
4886 release_tree_vector (args);
4892 /* postfix-expression . template [opt] id-expression
4893 postfix-expression . pseudo-destructor-name
4894 postfix-expression -> template [opt] id-expression
4895 postfix-expression -> pseudo-destructor-name */
4897 /* Consume the `.' or `->' operator. */
4898 cp_lexer_consume_token (parser->lexer);
4901 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4906 is_member_access = true;
4910 /* postfix-expression ++ */
4911 /* Consume the `++' token. */
4912 cp_lexer_consume_token (parser->lexer);
4913 /* Generate a representation for the complete expression. */
4915 = finish_increment_expr (postfix_expression,
4916 POSTINCREMENT_EXPR);
4917 /* Increments may not appear in constant-expressions. */
4918 if (cp_parser_non_integral_constant_expression (parser,
4920 postfix_expression = error_mark_node;
4921 idk = CP_ID_KIND_NONE;
4922 is_member_access = false;
4925 case CPP_MINUS_MINUS:
4926 /* postfix-expression -- */
4927 /* Consume the `--' token. */
4928 cp_lexer_consume_token (parser->lexer);
4929 /* Generate a representation for the complete expression. */
4931 = finish_increment_expr (postfix_expression,
4932 POSTDECREMENT_EXPR);
4933 /* Decrements may not appear in constant-expressions. */
4934 if (cp_parser_non_integral_constant_expression (parser,
4936 postfix_expression = error_mark_node;
4937 idk = CP_ID_KIND_NONE;
4938 is_member_access = false;
4942 if (pidk_return != NULL)
4943 * pidk_return = idk;
4944 if (member_access_only_p)
4945 return is_member_access? postfix_expression : error_mark_node;
4947 return postfix_expression;
4951 /* We should never get here. */
4953 return error_mark_node;
4956 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4957 by cp_parser_builtin_offsetof. We're looking for
4959 postfix-expression [ expression ]
4961 FOR_OFFSETOF is set if we're being called in that context, which
4962 changes how we deal with integer constant expressions. */
4965 cp_parser_postfix_open_square_expression (cp_parser *parser,
4966 tree postfix_expression,
4971 /* Consume the `[' token. */
4972 cp_lexer_consume_token (parser->lexer);
4974 /* Parse the index expression. */
4975 /* ??? For offsetof, there is a question of what to allow here. If
4976 offsetof is not being used in an integral constant expression context,
4977 then we *could* get the right answer by computing the value at runtime.
4978 If we are in an integral constant expression context, then we might
4979 could accept any constant expression; hard to say without analysis.
4980 Rather than open the barn door too wide right away, allow only integer
4981 constant expressions here. */
4983 index = cp_parser_constant_expression (parser, false, NULL);
4985 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4987 /* Look for the closing `]'. */
4988 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4990 /* Build the ARRAY_REF. */
4991 postfix_expression = grok_array_decl (postfix_expression, index);
4993 /* When not doing offsetof, array references are not permitted in
4994 constant-expressions. */
4996 && (cp_parser_non_integral_constant_expression
4997 (parser, "an array reference")))
4998 postfix_expression = error_mark_node;
5000 return postfix_expression;
5003 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5004 by cp_parser_builtin_offsetof. We're looking for
5006 postfix-expression . template [opt] id-expression
5007 postfix-expression . pseudo-destructor-name
5008 postfix-expression -> template [opt] id-expression
5009 postfix-expression -> pseudo-destructor-name
5011 FOR_OFFSETOF is set if we're being called in that context. That sorta
5012 limits what of the above we'll actually accept, but nevermind.
5013 TOKEN_TYPE is the "." or "->" token, which will already have been
5014 removed from the stream. */
5017 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5018 enum cpp_ttype token_type,
5019 tree postfix_expression,
5020 bool for_offsetof, cp_id_kind *idk,
5021 location_t location)
5025 bool pseudo_destructor_p;
5026 tree scope = NULL_TREE;
5028 /* If this is a `->' operator, dereference the pointer. */
5029 if (token_type == CPP_DEREF)
5030 postfix_expression = build_x_arrow (postfix_expression);
5031 /* Check to see whether or not the expression is type-dependent. */
5032 dependent_p = type_dependent_expression_p (postfix_expression);
5033 /* The identifier following the `->' or `.' is not qualified. */
5034 parser->scope = NULL_TREE;
5035 parser->qualifying_scope = NULL_TREE;
5036 parser->object_scope = NULL_TREE;
5037 *idk = CP_ID_KIND_NONE;
5039 /* Enter the scope corresponding to the type of the object
5040 given by the POSTFIX_EXPRESSION. */
5041 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5043 scope = TREE_TYPE (postfix_expression);
5044 /* According to the standard, no expression should ever have
5045 reference type. Unfortunately, we do not currently match
5046 the standard in this respect in that our internal representation
5047 of an expression may have reference type even when the standard
5048 says it does not. Therefore, we have to manually obtain the
5049 underlying type here. */
5050 scope = non_reference (scope);
5051 /* The type of the POSTFIX_EXPRESSION must be complete. */
5052 if (scope == unknown_type_node)
5054 error_at (location, "%qE does not have class type",
5055 postfix_expression);
5059 scope = complete_type_or_else (scope, NULL_TREE);
5060 /* Let the name lookup machinery know that we are processing a
5061 class member access expression. */
5062 parser->context->object_type = scope;
5063 /* If something went wrong, we want to be able to discern that case,
5064 as opposed to the case where there was no SCOPE due to the type
5065 of expression being dependent. */
5067 scope = error_mark_node;
5068 /* If the SCOPE was erroneous, make the various semantic analysis
5069 functions exit quickly -- and without issuing additional error
5071 if (scope == error_mark_node)
5072 postfix_expression = error_mark_node;
5075 /* Assume this expression is not a pseudo-destructor access. */
5076 pseudo_destructor_p = false;
5078 /* If the SCOPE is a scalar type, then, if this is a valid program,
5079 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5080 is type dependent, it can be pseudo-destructor-name or something else.
5081 Try to parse it as pseudo-destructor-name first. */
5082 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5087 cp_parser_parse_tentatively (parser);
5088 /* Parse the pseudo-destructor-name. */
5090 cp_parser_pseudo_destructor_name (parser, &s, &type);
5092 && (cp_parser_error_occurred (parser)
5093 || TREE_CODE (type) != TYPE_DECL
5094 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5095 cp_parser_abort_tentative_parse (parser);
5096 else if (cp_parser_parse_definitely (parser))
5098 pseudo_destructor_p = true;
5100 = finish_pseudo_destructor_expr (postfix_expression,
5101 s, TREE_TYPE (type));
5105 if (!pseudo_destructor_p)
5107 /* If the SCOPE is not a scalar type, we are looking at an
5108 ordinary class member access expression, rather than a
5109 pseudo-destructor-name. */
5111 cp_token *token = cp_lexer_peek_token (parser->lexer);
5112 /* Parse the id-expression. */
5113 name = (cp_parser_id_expression
5115 cp_parser_optional_template_keyword (parser),
5116 /*check_dependency_p=*/true,
5118 /*declarator_p=*/false,
5119 /*optional_p=*/false));
5120 /* In general, build a SCOPE_REF if the member name is qualified.
5121 However, if the name was not dependent and has already been
5122 resolved; there is no need to build the SCOPE_REF. For example;
5124 struct X { void f(); };
5125 template <typename T> void f(T* t) { t->X::f(); }
5127 Even though "t" is dependent, "X::f" is not and has been resolved
5128 to a BASELINK; there is no need to include scope information. */
5130 /* But we do need to remember that there was an explicit scope for
5131 virtual function calls. */
5133 *idk = CP_ID_KIND_QUALIFIED;
5135 /* If the name is a template-id that names a type, we will get a
5136 TYPE_DECL here. That is invalid code. */
5137 if (TREE_CODE (name) == TYPE_DECL)
5139 error_at (token->location, "invalid use of %qD", name);
5140 postfix_expression = error_mark_node;
5144 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5146 name = build_qualified_name (/*type=*/NULL_TREE,
5150 parser->scope = NULL_TREE;
5151 parser->qualifying_scope = NULL_TREE;
5152 parser->object_scope = NULL_TREE;
5154 if (scope && name && BASELINK_P (name))
5155 adjust_result_of_qualified_name_lookup
5156 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5158 = finish_class_member_access_expr (postfix_expression, name,
5160 tf_warning_or_error);
5164 /* We no longer need to look up names in the scope of the object on
5165 the left-hand side of the `.' or `->' operator. */
5166 parser->context->object_type = NULL_TREE;
5168 /* Outside of offsetof, these operators may not appear in
5169 constant-expressions. */
5171 && (cp_parser_non_integral_constant_expression
5172 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5173 postfix_expression = error_mark_node;
5175 return postfix_expression;
5178 /* Parse a parenthesized expression-list.
5181 assignment-expression
5182 expression-list, assignment-expression
5187 identifier, expression-list
5189 CAST_P is true if this expression is the target of a cast.
5191 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5194 Returns a vector of trees. Each element is a representation of an
5195 assignment-expression. NULL is returned if the ( and or ) are
5196 missing. An empty, but allocated, vector is returned on no
5197 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5198 if this is really an attribute list being parsed. If
5199 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5200 not all of the expressions in the list were constant. */
5202 static VEC(tree,gc) *
5203 cp_parser_parenthesized_expression_list (cp_parser* parser,
5204 bool is_attribute_list,
5206 bool allow_expansion_p,
5207 bool *non_constant_p)
5209 VEC(tree,gc) *expression_list;
5210 bool fold_expr_p = is_attribute_list;
5211 tree identifier = NULL_TREE;
5212 bool saved_greater_than_is_operator_p;
5214 /* Assume all the expressions will be constant. */
5216 *non_constant_p = false;
5218 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5221 expression_list = make_tree_vector ();
5223 /* Within a parenthesized expression, a `>' token is always
5224 the greater-than operator. */
5225 saved_greater_than_is_operator_p
5226 = parser->greater_than_is_operator_p;
5227 parser->greater_than_is_operator_p = true;
5229 /* Consume expressions until there are no more. */
5230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5235 /* At the beginning of attribute lists, check to see if the
5236 next token is an identifier. */
5237 if (is_attribute_list
5238 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5242 /* Consume the identifier. */
5243 token = cp_lexer_consume_token (parser->lexer);
5244 /* Save the identifier. */
5245 identifier = token->u.value;
5249 bool expr_non_constant_p;
5251 /* Parse the next assignment-expression. */
5252 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5254 /* A braced-init-list. */
5255 maybe_warn_cpp0x ("extended initializer lists");
5256 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5257 if (non_constant_p && expr_non_constant_p)
5258 *non_constant_p = true;
5260 else if (non_constant_p)
5262 expr = (cp_parser_constant_expression
5263 (parser, /*allow_non_constant_p=*/true,
5264 &expr_non_constant_p));
5265 if (expr_non_constant_p)
5266 *non_constant_p = true;
5269 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5272 expr = fold_non_dependent_expr (expr);
5274 /* If we have an ellipsis, then this is an expression
5276 if (allow_expansion_p
5277 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5279 /* Consume the `...'. */
5280 cp_lexer_consume_token (parser->lexer);
5282 /* Build the argument pack. */
5283 expr = make_pack_expansion (expr);
5286 /* Add it to the list. We add error_mark_node
5287 expressions to the list, so that we can still tell if
5288 the correct form for a parenthesized expression-list
5289 is found. That gives better errors. */
5290 VEC_safe_push (tree, gc, expression_list, expr);
5292 if (expr == error_mark_node)
5296 /* After the first item, attribute lists look the same as
5297 expression lists. */
5298 is_attribute_list = false;
5301 /* If the next token isn't a `,', then we are done. */
5302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5305 /* Otherwise, consume the `,' and keep going. */
5306 cp_lexer_consume_token (parser->lexer);
5309 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5314 /* We try and resync to an unnested comma, as that will give the
5315 user better diagnostics. */
5316 ending = cp_parser_skip_to_closing_parenthesis (parser,
5317 /*recovering=*/true,
5319 /*consume_paren=*/true);
5324 parser->greater_than_is_operator_p
5325 = saved_greater_than_is_operator_p;
5330 parser->greater_than_is_operator_p
5331 = saved_greater_than_is_operator_p;
5334 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5336 return expression_list;
5339 /* Parse a pseudo-destructor-name.
5341 pseudo-destructor-name:
5342 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5343 :: [opt] nested-name-specifier template template-id :: ~ type-name
5344 :: [opt] nested-name-specifier [opt] ~ type-name
5346 If either of the first two productions is used, sets *SCOPE to the
5347 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5348 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5349 or ERROR_MARK_NODE if the parse fails. */
5352 cp_parser_pseudo_destructor_name (cp_parser* parser,
5356 bool nested_name_specifier_p;
5358 /* Assume that things will not work out. */
5359 *type = error_mark_node;
5361 /* Look for the optional `::' operator. */
5362 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5363 /* Look for the optional nested-name-specifier. */
5364 nested_name_specifier_p
5365 = (cp_parser_nested_name_specifier_opt (parser,
5366 /*typename_keyword_p=*/false,
5367 /*check_dependency_p=*/true,
5369 /*is_declaration=*/false)
5371 /* Now, if we saw a nested-name-specifier, we might be doing the
5372 second production. */
5373 if (nested_name_specifier_p
5374 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5376 /* Consume the `template' keyword. */
5377 cp_lexer_consume_token (parser->lexer);
5378 /* Parse the template-id. */
5379 cp_parser_template_id (parser,
5380 /*template_keyword_p=*/true,
5381 /*check_dependency_p=*/false,
5382 /*is_declaration=*/true);
5383 /* Look for the `::' token. */
5384 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5386 /* If the next token is not a `~', then there might be some
5387 additional qualification. */
5388 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5390 /* At this point, we're looking for "type-name :: ~". The type-name
5391 must not be a class-name, since this is a pseudo-destructor. So,
5392 it must be either an enum-name, or a typedef-name -- both of which
5393 are just identifiers. So, we peek ahead to check that the "::"
5394 and "~" tokens are present; if they are not, then we can avoid
5395 calling type_name. */
5396 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5397 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5398 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5400 cp_parser_error (parser, "non-scalar type");
5404 /* Look for the type-name. */
5405 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5406 if (*scope == error_mark_node)
5409 /* Look for the `::' token. */
5410 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5415 /* Look for the `~'. */
5416 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5417 /* Look for the type-name again. We are not responsible for
5418 checking that it matches the first type-name. */
5419 *type = cp_parser_nonclass_name (parser);
5422 /* Parse a unary-expression.
5428 unary-operator cast-expression
5429 sizeof unary-expression
5437 __extension__ cast-expression
5438 __alignof__ unary-expression
5439 __alignof__ ( type-id )
5440 __real__ cast-expression
5441 __imag__ cast-expression
5444 ADDRESS_P is true iff the unary-expression is appearing as the
5445 operand of the `&' operator. CAST_P is true if this expression is
5446 the target of a cast.
5448 Returns a representation of the expression. */
5451 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5455 enum tree_code unary_operator;
5457 /* Peek at the next token. */
5458 token = cp_lexer_peek_token (parser->lexer);
5459 /* Some keywords give away the kind of expression. */
5460 if (token->type == CPP_KEYWORD)
5462 enum rid keyword = token->keyword;
5472 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5473 /* Consume the token. */
5474 cp_lexer_consume_token (parser->lexer);
5475 /* Parse the operand. */
5476 operand = cp_parser_sizeof_operand (parser, keyword);
5478 if (TYPE_P (operand))
5479 return cxx_sizeof_or_alignof_type (operand, op, true);
5481 return cxx_sizeof_or_alignof_expr (operand, op, true);
5485 return cp_parser_new_expression (parser);
5488 return cp_parser_delete_expression (parser);
5492 /* The saved value of the PEDANTIC flag. */
5496 /* Save away the PEDANTIC flag. */
5497 cp_parser_extension_opt (parser, &saved_pedantic);
5498 /* Parse the cast-expression. */
5499 expr = cp_parser_simple_cast_expression (parser);
5500 /* Restore the PEDANTIC flag. */
5501 pedantic = saved_pedantic;
5511 /* Consume the `__real__' or `__imag__' token. */
5512 cp_lexer_consume_token (parser->lexer);
5513 /* Parse the cast-expression. */
5514 expression = cp_parser_simple_cast_expression (parser);
5515 /* Create the complete representation. */
5516 return build_x_unary_op ((keyword == RID_REALPART
5517 ? REALPART_EXPR : IMAGPART_EXPR),
5519 tf_warning_or_error);
5528 /* Look for the `:: new' and `:: delete', which also signal the
5529 beginning of a new-expression, or delete-expression,
5530 respectively. If the next token is `::', then it might be one of
5532 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5536 /* See if the token after the `::' is one of the keywords in
5537 which we're interested. */
5538 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5539 /* If it's `new', we have a new-expression. */
5540 if (keyword == RID_NEW)
5541 return cp_parser_new_expression (parser);
5542 /* Similarly, for `delete'. */
5543 else if (keyword == RID_DELETE)
5544 return cp_parser_delete_expression (parser);
5547 /* Look for a unary operator. */
5548 unary_operator = cp_parser_unary_operator (token);
5549 /* The `++' and `--' operators can be handled similarly, even though
5550 they are not technically unary-operators in the grammar. */
5551 if (unary_operator == ERROR_MARK)
5553 if (token->type == CPP_PLUS_PLUS)
5554 unary_operator = PREINCREMENT_EXPR;
5555 else if (token->type == CPP_MINUS_MINUS)
5556 unary_operator = PREDECREMENT_EXPR;
5557 /* Handle the GNU address-of-label extension. */
5558 else if (cp_parser_allow_gnu_extensions_p (parser)
5559 && token->type == CPP_AND_AND)
5563 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5565 /* Consume the '&&' token. */
5566 cp_lexer_consume_token (parser->lexer);
5567 /* Look for the identifier. */
5568 identifier = cp_parser_identifier (parser);
5569 /* Create an expression representing the address. */
5570 expression = finish_label_address_expr (identifier, loc);
5571 if (cp_parser_non_integral_constant_expression (parser,
5572 "the address of a label"))
5573 expression = error_mark_node;
5577 if (unary_operator != ERROR_MARK)
5579 tree cast_expression;
5580 tree expression = error_mark_node;
5581 const char *non_constant_p = NULL;
5583 /* Consume the operator token. */
5584 token = cp_lexer_consume_token (parser->lexer);
5585 /* Parse the cast-expression. */
5587 = cp_parser_cast_expression (parser,
5588 unary_operator == ADDR_EXPR,
5589 /*cast_p=*/false, pidk);
5590 /* Now, build an appropriate representation. */
5591 switch (unary_operator)
5594 non_constant_p = "%<*%>";
5595 expression = build_x_indirect_ref (cast_expression, "unary *",
5596 tf_warning_or_error);
5600 non_constant_p = "%<&%>";
5603 expression = build_x_unary_op (unary_operator, cast_expression,
5604 tf_warning_or_error);
5607 case PREINCREMENT_EXPR:
5608 case PREDECREMENT_EXPR:
5609 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5610 ? "%<++%>" : "%<--%>");
5612 case UNARY_PLUS_EXPR:
5614 case TRUTH_NOT_EXPR:
5615 expression = finish_unary_op_expr (unary_operator, cast_expression);
5623 && cp_parser_non_integral_constant_expression (parser,
5625 expression = error_mark_node;
5630 return cp_parser_postfix_expression (parser, address_p, cast_p,
5631 /*member_access_only_p=*/false,
5635 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5636 unary-operator, the corresponding tree code is returned. */
5638 static enum tree_code
5639 cp_parser_unary_operator (cp_token* token)
5641 switch (token->type)
5644 return INDIRECT_REF;
5650 return UNARY_PLUS_EXPR;
5656 return TRUTH_NOT_EXPR;
5659 return BIT_NOT_EXPR;
5666 /* Parse a new-expression.
5669 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5670 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5672 Returns a representation of the expression. */
5675 cp_parser_new_expression (cp_parser* parser)
5677 bool global_scope_p;
5678 VEC(tree,gc) *placement;
5680 VEC(tree,gc) *initializer;
5684 /* Look for the optional `::' operator. */
5686 = (cp_parser_global_scope_opt (parser,
5687 /*current_scope_valid_p=*/false)
5689 /* Look for the `new' operator. */
5690 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5691 /* There's no easy way to tell a new-placement from the
5692 `( type-id )' construct. */
5693 cp_parser_parse_tentatively (parser);
5694 /* Look for a new-placement. */
5695 placement = cp_parser_new_placement (parser);
5696 /* If that didn't work out, there's no new-placement. */
5697 if (!cp_parser_parse_definitely (parser))
5699 if (placement != NULL)
5700 release_tree_vector (placement);
5704 /* If the next token is a `(', then we have a parenthesized
5706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5709 /* Consume the `('. */
5710 cp_lexer_consume_token (parser->lexer);
5711 /* Parse the type-id. */
5712 type = cp_parser_type_id (parser);
5713 /* Look for the closing `)'. */
5714 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5715 token = cp_lexer_peek_token (parser->lexer);
5716 /* There should not be a direct-new-declarator in this production,
5717 but GCC used to allowed this, so we check and emit a sensible error
5718 message for this case. */
5719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5721 error_at (token->location,
5722 "array bound forbidden after parenthesized type-id");
5723 inform (token->location,
5724 "try removing the parentheses around the type-id");
5725 cp_parser_direct_new_declarator (parser);
5729 /* Otherwise, there must be a new-type-id. */
5731 type = cp_parser_new_type_id (parser, &nelts);
5733 /* If the next token is a `(' or '{', then we have a new-initializer. */
5734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5735 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5736 initializer = cp_parser_new_initializer (parser);
5740 /* A new-expression may not appear in an integral constant
5742 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5743 ret = error_mark_node;
5746 /* Create a representation of the new-expression. */
5747 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5748 tf_warning_or_error);
5751 if (placement != NULL)
5752 release_tree_vector (placement);
5753 if (initializer != NULL)
5754 release_tree_vector (initializer);
5759 /* Parse a new-placement.
5764 Returns the same representation as for an expression-list. */
5766 static VEC(tree,gc) *
5767 cp_parser_new_placement (cp_parser* parser)
5769 VEC(tree,gc) *expression_list;
5771 /* Parse the expression-list. */
5772 expression_list = (cp_parser_parenthesized_expression_list
5773 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5774 /*non_constant_p=*/NULL));
5776 return expression_list;
5779 /* Parse a new-type-id.
5782 type-specifier-seq new-declarator [opt]
5784 Returns the TYPE allocated. If the new-type-id indicates an array
5785 type, *NELTS is set to the number of elements in the last array
5786 bound; the TYPE will not include the last array bound. */
5789 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5791 cp_decl_specifier_seq type_specifier_seq;
5792 cp_declarator *new_declarator;
5793 cp_declarator *declarator;
5794 cp_declarator *outer_declarator;
5795 const char *saved_message;
5798 /* The type-specifier sequence must not contain type definitions.
5799 (It cannot contain declarations of new types either, but if they
5800 are not definitions we will catch that because they are not
5802 saved_message = parser->type_definition_forbidden_message;
5803 parser->type_definition_forbidden_message
5804 = "types may not be defined in a new-type-id";
5805 /* Parse the type-specifier-seq. */
5806 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5807 /*is_trailing_return=*/false,
5808 &type_specifier_seq);
5809 /* Restore the old message. */
5810 parser->type_definition_forbidden_message = saved_message;
5811 /* Parse the new-declarator. */
5812 new_declarator = cp_parser_new_declarator_opt (parser);
5814 /* Determine the number of elements in the last array dimension, if
5817 /* Skip down to the last array dimension. */
5818 declarator = new_declarator;
5819 outer_declarator = NULL;
5820 while (declarator && (declarator->kind == cdk_pointer
5821 || declarator->kind == cdk_ptrmem))
5823 outer_declarator = declarator;
5824 declarator = declarator->declarator;
5827 && declarator->kind == cdk_array
5828 && declarator->declarator
5829 && declarator->declarator->kind == cdk_array)
5831 outer_declarator = declarator;
5832 declarator = declarator->declarator;
5835 if (declarator && declarator->kind == cdk_array)
5837 *nelts = declarator->u.array.bounds;
5838 if (*nelts == error_mark_node)
5839 *nelts = integer_one_node;
5841 if (outer_declarator)
5842 outer_declarator->declarator = declarator->declarator;
5844 new_declarator = NULL;
5847 type = groktypename (&type_specifier_seq, new_declarator, false);
5851 /* Parse an (optional) new-declarator.
5854 ptr-operator new-declarator [opt]
5855 direct-new-declarator
5857 Returns the declarator. */
5859 static cp_declarator *
5860 cp_parser_new_declarator_opt (cp_parser* parser)
5862 enum tree_code code;
5864 cp_cv_quals cv_quals;
5866 /* We don't know if there's a ptr-operator next, or not. */
5867 cp_parser_parse_tentatively (parser);
5868 /* Look for a ptr-operator. */
5869 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5870 /* If that worked, look for more new-declarators. */
5871 if (cp_parser_parse_definitely (parser))
5873 cp_declarator *declarator;
5875 /* Parse another optional declarator. */
5876 declarator = cp_parser_new_declarator_opt (parser);
5878 return cp_parser_make_indirect_declarator
5879 (code, type, cv_quals, declarator);
5882 /* If the next token is a `[', there is a direct-new-declarator. */
5883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5884 return cp_parser_direct_new_declarator (parser);
5889 /* Parse a direct-new-declarator.
5891 direct-new-declarator:
5893 direct-new-declarator [constant-expression]
5897 static cp_declarator *
5898 cp_parser_direct_new_declarator (cp_parser* parser)
5900 cp_declarator *declarator = NULL;
5906 /* Look for the opening `['. */
5907 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5908 /* The first expression is not required to be constant. */
5911 cp_token *token = cp_lexer_peek_token (parser->lexer);
5912 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5913 /* The standard requires that the expression have integral
5914 type. DR 74 adds enumeration types. We believe that the
5915 real intent is that these expressions be handled like the
5916 expression in a `switch' condition, which also allows
5917 classes with a single conversion to integral or
5918 enumeration type. */
5919 if (!processing_template_decl)
5922 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5927 error_at (token->location,
5928 "expression in new-declarator must have integral "
5929 "or enumeration type");
5930 expression = error_mark_node;
5934 /* But all the other expressions must be. */
5937 = cp_parser_constant_expression (parser,
5938 /*allow_non_constant=*/false,
5940 /* Look for the closing `]'. */
5941 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5943 /* Add this bound to the declarator. */
5944 declarator = make_array_declarator (declarator, expression);
5946 /* If the next token is not a `[', then there are no more
5948 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5955 /* Parse a new-initializer.
5958 ( expression-list [opt] )
5961 Returns a representation of the expression-list. */
5963 static VEC(tree,gc) *
5964 cp_parser_new_initializer (cp_parser* parser)
5966 VEC(tree,gc) *expression_list;
5968 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5971 bool expr_non_constant_p;
5972 maybe_warn_cpp0x ("extended initializer lists");
5973 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5974 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5975 expression_list = make_tree_vector_single (t);
5978 expression_list = (cp_parser_parenthesized_expression_list
5979 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5980 /*non_constant_p=*/NULL));
5982 return expression_list;
5985 /* Parse a delete-expression.
5988 :: [opt] delete cast-expression
5989 :: [opt] delete [ ] cast-expression
5991 Returns a representation of the expression. */
5994 cp_parser_delete_expression (cp_parser* parser)
5996 bool global_scope_p;
6000 /* Look for the optional `::' operator. */
6002 = (cp_parser_global_scope_opt (parser,
6003 /*current_scope_valid_p=*/false)
6005 /* Look for the `delete' keyword. */
6006 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6007 /* See if the array syntax is in use. */
6008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6010 /* Consume the `[' token. */
6011 cp_lexer_consume_token (parser->lexer);
6012 /* Look for the `]' token. */
6013 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6014 /* Remember that this is the `[]' construct. */
6020 /* Parse the cast-expression. */
6021 expression = cp_parser_simple_cast_expression (parser);
6023 /* A delete-expression may not appear in an integral constant
6025 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6026 return error_mark_node;
6028 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6031 /* Returns true if TOKEN may start a cast-expression and false
6035 cp_parser_token_starts_cast_expression (cp_token *token)
6037 switch (token->type)
6043 case CPP_CLOSE_SQUARE:
6044 case CPP_CLOSE_PAREN:
6045 case CPP_CLOSE_BRACE:
6049 case CPP_DEREF_STAR:
6057 case CPP_GREATER_EQ:
6077 /* '[' may start a primary-expression in obj-c++. */
6078 case CPP_OPEN_SQUARE:
6079 return c_dialect_objc ();
6086 /* Parse a cast-expression.
6090 ( type-id ) cast-expression
6092 ADDRESS_P is true iff the unary-expression is appearing as the
6093 operand of the `&' operator. CAST_P is true if this expression is
6094 the target of a cast.
6096 Returns a representation of the expression. */
6099 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6102 /* If it's a `(', then we might be looking at a cast. */
6103 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6105 tree type = NULL_TREE;
6106 tree expr = NULL_TREE;
6107 bool compound_literal_p;
6108 const char *saved_message;
6110 /* There's no way to know yet whether or not this is a cast.
6111 For example, `(int (3))' is a unary-expression, while `(int)
6112 3' is a cast. So, we resort to parsing tentatively. */
6113 cp_parser_parse_tentatively (parser);
6114 /* Types may not be defined in a cast. */
6115 saved_message = parser->type_definition_forbidden_message;
6116 parser->type_definition_forbidden_message
6117 = "types may not be defined in casts";
6118 /* Consume the `('. */
6119 cp_lexer_consume_token (parser->lexer);
6120 /* A very tricky bit is that `(struct S) { 3 }' is a
6121 compound-literal (which we permit in C++ as an extension).
6122 But, that construct is not a cast-expression -- it is a
6123 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6124 is legal; if the compound-literal were a cast-expression,
6125 you'd need an extra set of parentheses.) But, if we parse
6126 the type-id, and it happens to be a class-specifier, then we
6127 will commit to the parse at that point, because we cannot
6128 undo the action that is done when creating a new class. So,
6129 then we cannot back up and do a postfix-expression.
6131 Therefore, we scan ahead to the closing `)', and check to see
6132 if the token after the `)' is a `{'. If so, we are not
6133 looking at a cast-expression.
6135 Save tokens so that we can put them back. */
6136 cp_lexer_save_tokens (parser->lexer);
6137 /* Skip tokens until the next token is a closing parenthesis.
6138 If we find the closing `)', and the next token is a `{', then
6139 we are looking at a compound-literal. */
6141 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6142 /*consume_paren=*/true)
6143 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6144 /* Roll back the tokens we skipped. */
6145 cp_lexer_rollback_tokens (parser->lexer);
6146 /* If we were looking at a compound-literal, simulate an error
6147 so that the call to cp_parser_parse_definitely below will
6149 if (compound_literal_p)
6150 cp_parser_simulate_error (parser);
6153 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6154 parser->in_type_id_in_expr_p = true;
6155 /* Look for the type-id. */
6156 type = cp_parser_type_id (parser);
6157 /* Look for the closing `)'. */
6158 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6159 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6162 /* Restore the saved message. */
6163 parser->type_definition_forbidden_message = saved_message;
6165 /* At this point this can only be either a cast or a
6166 parenthesized ctor such as `(T ())' that looks like a cast to
6167 function returning T. */
6168 if (!cp_parser_error_occurred (parser)
6169 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6172 cp_parser_parse_definitely (parser);
6173 expr = cp_parser_cast_expression (parser,
6174 /*address_p=*/false,
6175 /*cast_p=*/true, pidk);
6177 /* Warn about old-style casts, if so requested. */
6178 if (warn_old_style_cast
6179 && !in_system_header
6180 && !VOID_TYPE_P (type)
6181 && current_lang_name != lang_name_c)
6182 warning (OPT_Wold_style_cast, "use of old-style cast");
6184 /* Only type conversions to integral or enumeration types
6185 can be used in constant-expressions. */
6186 if (!cast_valid_in_integral_constant_expression_p (type)
6187 && (cp_parser_non_integral_constant_expression
6189 "a cast to a type other than an integral or "
6190 "enumeration type")))
6191 return error_mark_node;
6193 /* Perform the cast. */
6194 expr = build_c_cast (input_location, type, expr);
6198 cp_parser_abort_tentative_parse (parser);
6201 /* If we get here, then it's not a cast, so it must be a
6202 unary-expression. */
6203 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6206 /* Parse a binary expression of the general form:
6210 pm-expression .* cast-expression
6211 pm-expression ->* cast-expression
6213 multiplicative-expression:
6215 multiplicative-expression * pm-expression
6216 multiplicative-expression / pm-expression
6217 multiplicative-expression % pm-expression
6219 additive-expression:
6220 multiplicative-expression
6221 additive-expression + multiplicative-expression
6222 additive-expression - multiplicative-expression
6226 shift-expression << additive-expression
6227 shift-expression >> additive-expression
6229 relational-expression:
6231 relational-expression < shift-expression
6232 relational-expression > shift-expression
6233 relational-expression <= shift-expression
6234 relational-expression >= shift-expression
6238 relational-expression:
6239 relational-expression <? shift-expression
6240 relational-expression >? shift-expression
6242 equality-expression:
6243 relational-expression
6244 equality-expression == relational-expression
6245 equality-expression != relational-expression
6249 and-expression & equality-expression
6251 exclusive-or-expression:
6253 exclusive-or-expression ^ and-expression
6255 inclusive-or-expression:
6256 exclusive-or-expression
6257 inclusive-or-expression | exclusive-or-expression
6259 logical-and-expression:
6260 inclusive-or-expression
6261 logical-and-expression && inclusive-or-expression
6263 logical-or-expression:
6264 logical-and-expression
6265 logical-or-expression || logical-and-expression
6267 All these are implemented with a single function like:
6270 simple-cast-expression
6271 binary-expression <token> binary-expression
6273 CAST_P is true if this expression is the target of a cast.
6275 The binops_by_token map is used to get the tree codes for each <token> type.
6276 binary-expressions are associated according to a precedence table. */
6278 #define TOKEN_PRECEDENCE(token) \
6279 (((token->type == CPP_GREATER \
6280 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6281 && !parser->greater_than_is_operator_p) \
6282 ? PREC_NOT_OPERATOR \
6283 : binops_by_token[token->type].prec)
6286 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6287 bool no_toplevel_fold_p,
6288 enum cp_parser_prec prec,
6291 cp_parser_expression_stack stack;
6292 cp_parser_expression_stack_entry *sp = &stack[0];
6295 enum tree_code tree_type, lhs_type, rhs_type;
6296 enum cp_parser_prec new_prec, lookahead_prec;
6299 /* Parse the first expression. */
6300 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6301 lhs_type = ERROR_MARK;
6305 /* Get an operator token. */
6306 token = cp_lexer_peek_token (parser->lexer);
6308 if (warn_cxx0x_compat
6309 && token->type == CPP_RSHIFT
6310 && !parser->greater_than_is_operator_p)
6312 if (warning_at (token->location, OPT_Wc__0x_compat,
6313 "%<>>%> operator will be treated as"
6314 " two right angle brackets in C++0x"))
6315 inform (token->location,
6316 "suggest parentheses around %<>>%> expression");
6319 new_prec = TOKEN_PRECEDENCE (token);
6321 /* Popping an entry off the stack means we completed a subexpression:
6322 - either we found a token which is not an operator (`>' where it is not
6323 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6324 will happen repeatedly;
6325 - or, we found an operator which has lower priority. This is the case
6326 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6328 if (new_prec <= prec)
6337 tree_type = binops_by_token[token->type].tree_type;
6339 /* We used the operator token. */
6340 cp_lexer_consume_token (parser->lexer);
6342 /* For "false && x" or "true || x", x will never be executed;
6343 disable warnings while evaluating it. */
6344 if (tree_type == TRUTH_ANDIF_EXPR)
6345 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6346 else if (tree_type == TRUTH_ORIF_EXPR)
6347 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6349 /* Extract another operand. It may be the RHS of this expression
6350 or the LHS of a new, higher priority expression. */
6351 rhs = cp_parser_simple_cast_expression (parser);
6352 rhs_type = ERROR_MARK;
6354 /* Get another operator token. Look up its precedence to avoid
6355 building a useless (immediately popped) stack entry for common
6356 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6357 token = cp_lexer_peek_token (parser->lexer);
6358 lookahead_prec = TOKEN_PRECEDENCE (token);
6359 if (lookahead_prec > new_prec)
6361 /* ... and prepare to parse the RHS of the new, higher priority
6362 expression. Since precedence levels on the stack are
6363 monotonically increasing, we do not have to care about
6366 sp->tree_type = tree_type;
6368 sp->lhs_type = lhs_type;
6371 lhs_type = rhs_type;
6373 new_prec = lookahead_prec;
6377 lookahead_prec = new_prec;
6378 /* If the stack is not empty, we have parsed into LHS the right side
6379 (`4' in the example above) of an expression we had suspended.
6380 We can use the information on the stack to recover the LHS (`3')
6381 from the stack together with the tree code (`MULT_EXPR'), and
6382 the precedence of the higher level subexpression
6383 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6384 which will be used to actually build the additive expression. */
6387 tree_type = sp->tree_type;
6389 rhs_type = lhs_type;
6391 lhs_type = sp->lhs_type;
6394 /* Undo the disabling of warnings done above. */
6395 if (tree_type == TRUTH_ANDIF_EXPR)
6396 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6397 else if (tree_type == TRUTH_ORIF_EXPR)
6398 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6400 overloaded_p = false;
6401 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6402 ERROR_MARK for everything that is not a binary expression.
6403 This makes warn_about_parentheses miss some warnings that
6404 involve unary operators. For unary expressions we should
6405 pass the correct tree_code unless the unary expression was
6406 surrounded by parentheses.
6408 if (no_toplevel_fold_p
6409 && lookahead_prec <= prec
6411 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6412 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6414 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6415 &overloaded_p, tf_warning_or_error);
6416 lhs_type = tree_type;
6418 /* If the binary operator required the use of an overloaded operator,
6419 then this expression cannot be an integral constant-expression.
6420 An overloaded operator can be used even if both operands are
6421 otherwise permissible in an integral constant-expression if at
6422 least one of the operands is of enumeration type. */
6425 && (cp_parser_non_integral_constant_expression
6426 (parser, "calls to overloaded operators")))
6427 return error_mark_node;
6434 /* Parse the `? expression : assignment-expression' part of a
6435 conditional-expression. The LOGICAL_OR_EXPR is the
6436 logical-or-expression that started the conditional-expression.
6437 Returns a representation of the entire conditional-expression.
6439 This routine is used by cp_parser_assignment_expression.
6441 ? expression : assignment-expression
6445 ? : assignment-expression */
6448 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6451 tree assignment_expr;
6453 /* Consume the `?' token. */
6454 cp_lexer_consume_token (parser->lexer);
6455 if (cp_parser_allow_gnu_extensions_p (parser)
6456 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6458 /* Implicit true clause. */
6460 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6464 /* Parse the expression. */
6465 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6466 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6467 c_inhibit_evaluation_warnings +=
6468 ((logical_or_expr == truthvalue_true_node)
6469 - (logical_or_expr == truthvalue_false_node));
6472 /* The next token should be a `:'. */
6473 cp_parser_require (parser, CPP_COLON, "%<:%>");
6474 /* Parse the assignment-expression. */
6475 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6476 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6478 /* Build the conditional-expression. */
6479 return build_x_conditional_expr (logical_or_expr,
6482 tf_warning_or_error);
6485 /* Parse an assignment-expression.
6487 assignment-expression:
6488 conditional-expression
6489 logical-or-expression assignment-operator assignment_expression
6492 CAST_P is true if this expression is the target of a cast.
6494 Returns a representation for the expression. */
6497 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6502 /* If the next token is the `throw' keyword, then we're looking at
6503 a throw-expression. */
6504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6505 expr = cp_parser_throw_expression (parser);
6506 /* Otherwise, it must be that we are looking at a
6507 logical-or-expression. */
6510 /* Parse the binary expressions (logical-or-expression). */
6511 expr = cp_parser_binary_expression (parser, cast_p, false,
6512 PREC_NOT_OPERATOR, pidk);
6513 /* If the next token is a `?' then we're actually looking at a
6514 conditional-expression. */
6515 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6516 return cp_parser_question_colon_clause (parser, expr);
6519 enum tree_code assignment_operator;
6521 /* If it's an assignment-operator, we're using the second
6524 = cp_parser_assignment_operator_opt (parser);
6525 if (assignment_operator != ERROR_MARK)
6527 bool non_constant_p;
6529 /* Parse the right-hand side of the assignment. */
6530 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6532 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6533 maybe_warn_cpp0x ("extended initializer lists");
6535 /* An assignment may not appear in a
6536 constant-expression. */
6537 if (cp_parser_non_integral_constant_expression (parser,
6539 return error_mark_node;
6540 /* Build the assignment expression. */
6541 expr = build_x_modify_expr (expr,
6542 assignment_operator,
6544 tf_warning_or_error);
6552 /* Parse an (optional) assignment-operator.
6554 assignment-operator: one of
6555 = *= /= %= += -= >>= <<= &= ^= |=
6559 assignment-operator: one of
6562 If the next token is an assignment operator, the corresponding tree
6563 code is returned, and the token is consumed. For example, for
6564 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6565 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6566 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6567 operator, ERROR_MARK is returned. */
6569 static enum tree_code
6570 cp_parser_assignment_operator_opt (cp_parser* parser)
6575 /* Peek at the next token. */
6576 token = cp_lexer_peek_token (parser->lexer);
6578 switch (token->type)
6589 op = TRUNC_DIV_EXPR;
6593 op = TRUNC_MOD_EXPR;
6625 /* Nothing else is an assignment operator. */
6629 /* If it was an assignment operator, consume it. */
6630 if (op != ERROR_MARK)
6631 cp_lexer_consume_token (parser->lexer);
6636 /* Parse an expression.
6639 assignment-expression
6640 expression , assignment-expression
6642 CAST_P is true if this expression is the target of a cast.
6644 Returns a representation of the expression. */
6647 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6649 tree expression = NULL_TREE;
6653 tree assignment_expression;
6655 /* Parse the next assignment-expression. */
6656 assignment_expression
6657 = cp_parser_assignment_expression (parser, cast_p, pidk);
6658 /* If this is the first assignment-expression, we can just
6661 expression = assignment_expression;
6663 expression = build_x_compound_expr (expression,
6664 assignment_expression,
6665 tf_warning_or_error);
6666 /* If the next token is not a comma, then we are done with the
6668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6670 /* Consume the `,'. */
6671 cp_lexer_consume_token (parser->lexer);
6672 /* A comma operator cannot appear in a constant-expression. */
6673 if (cp_parser_non_integral_constant_expression (parser,
6674 "a comma operator"))
6675 expression = error_mark_node;
6681 /* Parse a constant-expression.
6683 constant-expression:
6684 conditional-expression
6686 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6687 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6688 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6689 is false, NON_CONSTANT_P should be NULL. */
6692 cp_parser_constant_expression (cp_parser* parser,
6693 bool allow_non_constant_p,
6694 bool *non_constant_p)
6696 bool saved_integral_constant_expression_p;
6697 bool saved_allow_non_integral_constant_expression_p;
6698 bool saved_non_integral_constant_expression_p;
6701 /* It might seem that we could simply parse the
6702 conditional-expression, and then check to see if it were
6703 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6704 one that the compiler can figure out is constant, possibly after
6705 doing some simplifications or optimizations. The standard has a
6706 precise definition of constant-expression, and we must honor
6707 that, even though it is somewhat more restrictive.
6713 is not a legal declaration, because `(2, 3)' is not a
6714 constant-expression. The `,' operator is forbidden in a
6715 constant-expression. However, GCC's constant-folding machinery
6716 will fold this operation to an INTEGER_CST for `3'. */
6718 /* Save the old settings. */
6719 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6720 saved_allow_non_integral_constant_expression_p
6721 = parser->allow_non_integral_constant_expression_p;
6722 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6723 /* We are now parsing a constant-expression. */
6724 parser->integral_constant_expression_p = true;
6725 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6726 parser->non_integral_constant_expression_p = false;
6727 /* Although the grammar says "conditional-expression", we parse an
6728 "assignment-expression", which also permits "throw-expression"
6729 and the use of assignment operators. In the case that
6730 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6731 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6732 actually essential that we look for an assignment-expression.
6733 For example, cp_parser_initializer_clauses uses this function to
6734 determine whether a particular assignment-expression is in fact
6736 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6737 /* Restore the old settings. */
6738 parser->integral_constant_expression_p
6739 = saved_integral_constant_expression_p;
6740 parser->allow_non_integral_constant_expression_p
6741 = saved_allow_non_integral_constant_expression_p;
6742 if (allow_non_constant_p)
6743 *non_constant_p = parser->non_integral_constant_expression_p;
6744 else if (parser->non_integral_constant_expression_p)
6745 expression = error_mark_node;
6746 parser->non_integral_constant_expression_p
6747 = saved_non_integral_constant_expression_p;
6752 /* Parse __builtin_offsetof.
6754 offsetof-expression:
6755 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6757 offsetof-member-designator:
6759 | offsetof-member-designator "." id-expression
6760 | offsetof-member-designator "[" expression "]"
6761 | offsetof-member-designator "->" id-expression */
6764 cp_parser_builtin_offsetof (cp_parser *parser)
6766 int save_ice_p, save_non_ice_p;
6771 /* We're about to accept non-integral-constant things, but will
6772 definitely yield an integral constant expression. Save and
6773 restore these values around our local parsing. */
6774 save_ice_p = parser->integral_constant_expression_p;
6775 save_non_ice_p = parser->non_integral_constant_expression_p;
6777 /* Consume the "__builtin_offsetof" token. */
6778 cp_lexer_consume_token (parser->lexer);
6779 /* Consume the opening `('. */
6780 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6781 /* Parse the type-id. */
6782 type = cp_parser_type_id (parser);
6783 /* Look for the `,'. */
6784 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6785 token = cp_lexer_peek_token (parser->lexer);
6787 /* Build the (type *)null that begins the traditional offsetof macro. */
6788 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6789 tf_warning_or_error);
6791 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6792 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6793 true, &dummy, token->location);
6796 token = cp_lexer_peek_token (parser->lexer);
6797 switch (token->type)
6799 case CPP_OPEN_SQUARE:
6800 /* offsetof-member-designator "[" expression "]" */
6801 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6805 /* offsetof-member-designator "->" identifier */
6806 expr = grok_array_decl (expr, integer_zero_node);
6810 /* offsetof-member-designator "." identifier */
6811 cp_lexer_consume_token (parser->lexer);
6812 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6817 case CPP_CLOSE_PAREN:
6818 /* Consume the ")" token. */
6819 cp_lexer_consume_token (parser->lexer);
6823 /* Error. We know the following require will fail, but
6824 that gives the proper error message. */
6825 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6826 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6827 expr = error_mark_node;
6833 /* If we're processing a template, we can't finish the semantics yet.
6834 Otherwise we can fold the entire expression now. */
6835 if (processing_template_decl)
6836 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6838 expr = finish_offsetof (expr);
6841 parser->integral_constant_expression_p = save_ice_p;
6842 parser->non_integral_constant_expression_p = save_non_ice_p;
6847 /* Parse a trait expression. */
6850 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6853 tree type1, type2 = NULL_TREE;
6854 bool binary = false;
6855 cp_decl_specifier_seq decl_specs;
6859 case RID_HAS_NOTHROW_ASSIGN:
6860 kind = CPTK_HAS_NOTHROW_ASSIGN;
6862 case RID_HAS_NOTHROW_CONSTRUCTOR:
6863 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6865 case RID_HAS_NOTHROW_COPY:
6866 kind = CPTK_HAS_NOTHROW_COPY;
6868 case RID_HAS_TRIVIAL_ASSIGN:
6869 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6871 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6872 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6874 case RID_HAS_TRIVIAL_COPY:
6875 kind = CPTK_HAS_TRIVIAL_COPY;
6877 case RID_HAS_TRIVIAL_DESTRUCTOR:
6878 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6880 case RID_HAS_VIRTUAL_DESTRUCTOR:
6881 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6883 case RID_IS_ABSTRACT:
6884 kind = CPTK_IS_ABSTRACT;
6886 case RID_IS_BASE_OF:
6887 kind = CPTK_IS_BASE_OF;
6891 kind = CPTK_IS_CLASS;
6893 case RID_IS_CONVERTIBLE_TO:
6894 kind = CPTK_IS_CONVERTIBLE_TO;
6898 kind = CPTK_IS_EMPTY;
6901 kind = CPTK_IS_ENUM;
6906 case RID_IS_POLYMORPHIC:
6907 kind = CPTK_IS_POLYMORPHIC;
6909 case RID_IS_STD_LAYOUT:
6910 kind = CPTK_IS_STD_LAYOUT;
6912 case RID_IS_TRIVIAL:
6913 kind = CPTK_IS_TRIVIAL;
6916 kind = CPTK_IS_UNION;
6922 /* Consume the token. */
6923 cp_lexer_consume_token (parser->lexer);
6925 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6927 type1 = cp_parser_type_id (parser);
6929 if (type1 == error_mark_node)
6930 return error_mark_node;
6932 /* Build a trivial decl-specifier-seq. */
6933 clear_decl_specs (&decl_specs);
6934 decl_specs.type = type1;
6936 /* Call grokdeclarator to figure out what type this is. */
6937 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6938 /*initialized=*/0, /*attrlist=*/NULL);
6942 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6944 type2 = cp_parser_type_id (parser);
6946 if (type2 == error_mark_node)
6947 return error_mark_node;
6949 /* Build a trivial decl-specifier-seq. */
6950 clear_decl_specs (&decl_specs);
6951 decl_specs.type = type2;
6953 /* Call grokdeclarator to figure out what type this is. */
6954 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6955 /*initialized=*/0, /*attrlist=*/NULL);
6958 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6960 /* Complete the trait expression, which may mean either processing
6961 the trait expr now or saving it for template instantiation. */
6962 return finish_trait_expr (kind, type1, type2);
6965 /* Lambdas that appear in variable initializer or default argument scope
6966 get that in their mangling, so we need to record it. We might as well
6967 use the count for function and namespace scopes as well. */
6968 static GTY(()) tree lambda_scope;
6969 static GTY(()) int lambda_count;
6970 typedef struct GTY(()) tree_int
6975 DEF_VEC_O(tree_int);
6976 DEF_VEC_ALLOC_O(tree_int,gc);
6977 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6980 start_lambda_scope (tree decl)
6984 /* Once we're inside a function, we ignore other scopes and just push
6985 the function again so that popping works properly. */
6986 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6987 decl = current_function_decl;
6988 ti.t = lambda_scope;
6989 ti.i = lambda_count;
6990 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6991 if (lambda_scope != decl)
6993 /* Don't reset the count if we're still in the same function. */
6994 lambda_scope = decl;
7000 record_lambda_scope (tree lambda)
7002 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7003 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7007 finish_lambda_scope (void)
7009 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7010 if (lambda_scope != p->t)
7012 lambda_scope = p->t;
7013 lambda_count = p->i;
7015 VEC_pop (tree_int, lambda_scope_stack);
7018 /* Parse a lambda expression.
7021 lambda-introducer lambda-declarator [opt] compound-statement
7023 Returns a representation of the expression. */
7026 cp_parser_lambda_expression (cp_parser* parser)
7028 tree lambda_expr = build_lambda_expr ();
7031 LAMBDA_EXPR_LOCATION (lambda_expr)
7032 = cp_lexer_peek_token (parser->lexer)->location;
7034 /* We may be in the middle of deferred access check. Disable
7036 push_deferring_access_checks (dk_no_deferred);
7038 type = begin_lambda_type (lambda_expr);
7040 record_lambda_scope (lambda_expr);
7042 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7043 determine_visibility (TYPE_NAME (type));
7046 /* Inside the class, surrounding template-parameter-lists do not apply. */
7047 unsigned int saved_num_template_parameter_lists
7048 = parser->num_template_parameter_lists;
7050 parser->num_template_parameter_lists = 0;
7052 cp_parser_lambda_introducer (parser, lambda_expr);
7054 /* By virtue of defining a local class, a lambda expression has access to
7055 the private variables of enclosing classes. */
7057 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7059 cp_parser_lambda_body (parser, lambda_expr);
7061 /* The capture list was built up in reverse order; fix that now. */
7063 tree newlist = NULL_TREE;
7066 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7069 tree field = TREE_PURPOSE (elt);
7072 next = TREE_CHAIN (elt);
7073 TREE_CHAIN (elt) = newlist;
7076 /* Also add __ to the beginning of the field name so that code
7077 outside the lambda body can't see the captured name. We could
7078 just remove the name entirely, but this is more useful for
7080 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7081 /* The 'this' capture already starts with __. */
7084 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7085 buf[1] = buf[0] = '_';
7086 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7087 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7088 DECL_NAME (field) = get_identifier (buf);
7090 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7093 maybe_add_lambda_conv_op (type);
7095 type = finish_struct (type, /*attributes=*/NULL_TREE);
7097 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7100 pop_deferring_access_checks ();
7102 return build_lambda_object (lambda_expr);
7105 /* Parse the beginning of a lambda expression.
7108 [ lambda-capture [opt] ]
7110 LAMBDA_EXPR is the current representation of the lambda expression. */
7113 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7115 /* Need commas after the first capture. */
7118 /* Eat the leading `['. */
7119 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7121 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7122 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7123 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7124 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7125 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7126 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7128 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7130 cp_lexer_consume_token (parser->lexer);
7134 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7136 cp_token* capture_token;
7138 tree capture_init_expr;
7139 cp_id_kind idk = CP_ID_KIND_NONE;
7140 bool explicit_init_p = false;
7142 enum capture_kind_type
7147 enum capture_kind_type capture_kind = BY_COPY;
7149 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7151 error ("expected end of capture-list");
7158 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7160 /* Possibly capture `this'. */
7161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7163 cp_lexer_consume_token (parser->lexer);
7164 add_capture (lambda_expr,
7165 /*id=*/get_identifier ("__this"),
7166 /*initializer=*/finish_this_expr(),
7167 /*by_reference_p=*/false,
7172 /* Remember whether we want to capture as a reference or not. */
7173 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7175 capture_kind = BY_REFERENCE;
7176 cp_lexer_consume_token (parser->lexer);
7179 /* Get the identifier. */
7180 capture_token = cp_lexer_peek_token (parser->lexer);
7181 capture_id = cp_parser_identifier (parser);
7183 if (capture_id == error_mark_node)
7184 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7185 delimiters, but I modified this to stop on unnested ']' as well. It
7186 was already changed to stop on unnested '}', so the
7187 "closing_parenthesis" name is no more misleading with my change. */
7189 cp_parser_skip_to_closing_parenthesis (parser,
7190 /*recovering=*/true,
7192 /*consume_paren=*/true);
7196 /* Find the initializer for this capture. */
7197 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7199 /* An explicit expression exists. */
7200 cp_lexer_consume_token (parser->lexer);
7201 pedwarn (input_location, OPT_pedantic,
7202 "ISO C++ does not allow initializers "
7203 "in lambda expression capture lists");
7204 capture_init_expr = cp_parser_assignment_expression (parser,
7207 explicit_init_p = true;
7211 const char* error_msg;
7213 /* Turn the identifier into an id-expression. */
7215 = cp_parser_lookup_name
7219 /*is_template=*/false,
7220 /*is_namespace=*/false,
7221 /*check_dependency=*/true,
7222 /*ambiguous_decls=*/NULL,
7223 capture_token->location);
7226 = finish_id_expression
7231 /*integral_constant_expression_p=*/false,
7232 /*allow_non_integral_constant_expression_p=*/false,
7233 /*non_integral_constant_expression_p=*/NULL,
7234 /*template_p=*/false,
7236 /*address_p=*/false,
7237 /*template_arg_p=*/false,
7239 capture_token->location);
7242 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7244 = unqualified_name_lookup_error (capture_init_expr);
7246 add_capture (lambda_expr,
7249 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7253 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7256 /* Parse the (optional) middle of a lambda expression.
7259 ( parameter-declaration-clause [opt] )
7260 attribute-specifier [opt]
7262 exception-specification [opt]
7263 lambda-return-type-clause [opt]
7265 LAMBDA_EXPR is the current representation of the lambda expression. */
7268 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7270 /* 5.1.1.4 of the standard says:
7271 If a lambda-expression does not include a lambda-declarator, it is as if
7272 the lambda-declarator were ().
7273 This means an empty parameter list, no attributes, and no exception
7275 tree param_list = void_list_node;
7276 tree attributes = NULL_TREE;
7277 tree exception_spec = NULL_TREE;
7280 /* The lambda-declarator is optional, but must begin with an opening
7281 parenthesis if present. */
7282 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7284 cp_lexer_consume_token (parser->lexer);
7286 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7288 /* Parse parameters. */
7289 param_list = cp_parser_parameter_declaration_clause (parser);
7291 /* Default arguments shall not be specified in the
7292 parameter-declaration-clause of a lambda-declarator. */
7293 for (t = param_list; t; t = TREE_CHAIN (t))
7294 if (TREE_PURPOSE (t))
7295 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7296 "default argument specified for lambda parameter");
7298 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7300 attributes = cp_parser_attributes_opt (parser);
7302 /* Parse optional `mutable' keyword. */
7303 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7305 cp_lexer_consume_token (parser->lexer);
7306 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7309 /* Parse optional exception specification. */
7310 exception_spec = cp_parser_exception_specification_opt (parser);
7312 /* Parse optional trailing return type. */
7313 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7315 cp_lexer_consume_token (parser->lexer);
7316 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7319 /* The function parameters must be in scope all the way until after the
7320 trailing-return-type in case of decltype. */
7321 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7322 pop_binding (DECL_NAME (t), t);
7327 /* Create the function call operator.
7329 Messing with declarators like this is no uglier than building up the
7330 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7333 cp_decl_specifier_seq return_type_specs;
7334 cp_declarator* declarator;
7339 clear_decl_specs (&return_type_specs);
7340 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7341 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7343 /* Maybe we will deduce the return type later, but we can use void
7344 as a placeholder return type anyways. */
7345 return_type_specs.type = void_type_node;
7347 p = obstack_alloc (&declarator_obstack, 0);
7349 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7352 quals = TYPE_UNQUALIFIED;
7353 if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7354 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7356 /* A lambda with no captures has a static op() and a conversion op
7357 to function type. */
7358 if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7359 error ("lambda expression with no captures declared mutable");
7360 return_type_specs.storage_class = sc_static;
7362 else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7363 quals = TYPE_QUAL_CONST;
7364 declarator = make_call_declarator (declarator, param_list, quals,
7366 /*late_return_type=*/NULL_TREE);
7368 fco = grokmethod (&return_type_specs,
7371 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7372 DECL_ARTIFICIAL (fco) = 1;
7374 finish_member_declaration (fco);
7376 obstack_free (&declarator_obstack, p);
7380 /* Parse the body of a lambda expression, which is simply
7384 but which requires special handling.
7385 LAMBDA_EXPR is the current representation of the lambda expression. */
7388 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7390 bool nested = (current_function_decl != NULL_TREE);
7392 push_function_context ();
7394 /* Finish the function call operator
7396 + late_parsing_for_member
7397 + function_definition_after_declarator
7398 + ctor_initializer_opt_and_function_body */
7400 tree fco = lambda_function (lambda_expr);
7404 /* Let the front end know that we are going to be defining this
7406 start_preparsed_function (fco,
7408 SF_PRE_PARSED | SF_INCLASS_INLINE);
7410 start_lambda_scope (fco);
7411 body = begin_function_body ();
7413 /* 5.1.1.4 of the standard says:
7414 If a lambda-expression does not include a trailing-return-type, it
7415 is as if the trailing-return-type denotes the following type:
7416 * if the compound-statement is of the form
7417 { return attribute-specifier [opt] expression ; }
7418 the type of the returned expression after lvalue-to-rvalue
7419 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7420 (_conv.array_ 4.2), and function-to-pointer conversion
7422 * otherwise, void. */
7424 /* In a lambda that has neither a lambda-return-type-clause
7425 nor a deducible form, errors should be reported for return statements
7426 in the body. Since we used void as the placeholder return type, parsing
7427 the body as usual will give such desired behavior. */
7428 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7429 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7430 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7431 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7434 tree expr = NULL_TREE;
7435 cp_id_kind idk = CP_ID_KIND_NONE;
7437 /* Parse tentatively in case there's more after the initial return
7439 cp_parser_parse_tentatively (parser);
7441 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7442 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7444 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7446 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7447 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7449 if (cp_parser_parse_definitely (parser))
7451 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7453 compound_stmt = begin_compound_stmt (0);
7454 /* Will get error here if type not deduced yet. */
7455 finish_return_stmt (expr);
7456 finish_compound_stmt (compound_stmt);
7464 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7465 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7466 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7467 cp_parser_compound_stmt does not pass it. */
7468 cp_parser_function_body (parser);
7469 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7472 finish_function_body (body);
7473 finish_lambda_scope ();
7475 /* Finish the function and generate code for it if necessary. */
7476 expand_or_defer_fn (finish_function (/*inline*/2));
7480 pop_function_context();
7483 /* Statements [gram.stmt.stmt] */
7485 /* Parse a statement.
7489 expression-statement
7494 declaration-statement
7497 IN_COMPOUND is true when the statement is nested inside a
7498 cp_parser_compound_statement; this matters for certain pragmas.
7500 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7501 is a (possibly labeled) if statement which is not enclosed in braces
7502 and has an else clause. This is used to implement -Wparentheses. */
7505 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7506 bool in_compound, bool *if_p)
7510 location_t statement_location;
7515 /* There is no statement yet. */
7516 statement = NULL_TREE;
7517 /* Peek at the next token. */
7518 token = cp_lexer_peek_token (parser->lexer);
7519 /* Remember the location of the first token in the statement. */
7520 statement_location = token->location;
7521 /* If this is a keyword, then that will often determine what kind of
7522 statement we have. */
7523 if (token->type == CPP_KEYWORD)
7525 enum rid keyword = token->keyword;
7531 /* Looks like a labeled-statement with a case label.
7532 Parse the label, and then use tail recursion to parse
7534 cp_parser_label_for_labeled_statement (parser);
7539 statement = cp_parser_selection_statement (parser, if_p);
7545 statement = cp_parser_iteration_statement (parser);
7552 statement = cp_parser_jump_statement (parser);
7555 /* Objective-C++ exception-handling constructs. */
7558 case RID_AT_FINALLY:
7559 case RID_AT_SYNCHRONIZED:
7561 statement = cp_parser_objc_statement (parser);
7565 statement = cp_parser_try_block (parser);
7569 /* This must be a namespace alias definition. */
7570 cp_parser_declaration_statement (parser);
7574 /* It might be a keyword like `int' that can start a
7575 declaration-statement. */
7579 else if (token->type == CPP_NAME)
7581 /* If the next token is a `:', then we are looking at a
7582 labeled-statement. */
7583 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7584 if (token->type == CPP_COLON)
7586 /* Looks like a labeled-statement with an ordinary label.
7587 Parse the label, and then use tail recursion to parse
7589 cp_parser_label_for_labeled_statement (parser);
7593 /* Anything that starts with a `{' must be a compound-statement. */
7594 else if (token->type == CPP_OPEN_BRACE)
7595 statement = cp_parser_compound_statement (parser, NULL, false);
7596 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7597 a statement all its own. */
7598 else if (token->type == CPP_PRAGMA)
7600 /* Only certain OpenMP pragmas are attached to statements, and thus
7601 are considered statements themselves. All others are not. In
7602 the context of a compound, accept the pragma as a "statement" and
7603 return so that we can check for a close brace. Otherwise we
7604 require a real statement and must go back and read one. */
7606 cp_parser_pragma (parser, pragma_compound);
7607 else if (!cp_parser_pragma (parser, pragma_stmt))
7611 else if (token->type == CPP_EOF)
7613 cp_parser_error (parser, "expected statement");
7617 /* Everything else must be a declaration-statement or an
7618 expression-statement. Try for the declaration-statement
7619 first, unless we are looking at a `;', in which case we know that
7620 we have an expression-statement. */
7623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7625 cp_parser_parse_tentatively (parser);
7626 /* Try to parse the declaration-statement. */
7627 cp_parser_declaration_statement (parser);
7628 /* If that worked, we're done. */
7629 if (cp_parser_parse_definitely (parser))
7632 /* Look for an expression-statement instead. */
7633 statement = cp_parser_expression_statement (parser, in_statement_expr);
7636 /* Set the line number for the statement. */
7637 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7638 SET_EXPR_LOCATION (statement, statement_location);
7641 /* Parse the label for a labeled-statement, i.e.
7644 case constant-expression :
7648 case constant-expression ... constant-expression : statement
7650 When a label is parsed without errors, the label is added to the
7651 parse tree by the finish_* functions, so this function doesn't
7652 have to return the label. */
7655 cp_parser_label_for_labeled_statement (cp_parser* parser)
7658 tree label = NULL_TREE;
7660 /* The next token should be an identifier. */
7661 token = cp_lexer_peek_token (parser->lexer);
7662 if (token->type != CPP_NAME
7663 && token->type != CPP_KEYWORD)
7665 cp_parser_error (parser, "expected labeled-statement");
7669 switch (token->keyword)
7676 /* Consume the `case' token. */
7677 cp_lexer_consume_token (parser->lexer);
7678 /* Parse the constant-expression. */
7679 expr = cp_parser_constant_expression (parser,
7680 /*allow_non_constant_p=*/false,
7683 ellipsis = cp_lexer_peek_token (parser->lexer);
7684 if (ellipsis->type == CPP_ELLIPSIS)
7686 /* Consume the `...' token. */
7687 cp_lexer_consume_token (parser->lexer);
7689 cp_parser_constant_expression (parser,
7690 /*allow_non_constant_p=*/false,
7692 /* We don't need to emit warnings here, as the common code
7693 will do this for us. */
7696 expr_hi = NULL_TREE;
7698 if (parser->in_switch_statement_p)
7699 finish_case_label (token->location, expr, expr_hi);
7701 error_at (token->location,
7702 "case label %qE not within a switch statement",
7708 /* Consume the `default' token. */
7709 cp_lexer_consume_token (parser->lexer);
7711 if (parser->in_switch_statement_p)
7712 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7714 error_at (token->location, "case label not within a switch statement");
7718 /* Anything else must be an ordinary label. */
7719 label = finish_label_stmt (cp_parser_identifier (parser));
7723 /* Require the `:' token. */
7724 cp_parser_require (parser, CPP_COLON, "%<:%>");
7726 /* An ordinary label may optionally be followed by attributes.
7727 However, this is only permitted if the attributes are then
7728 followed by a semicolon. This is because, for backward
7729 compatibility, when parsing
7730 lab: __attribute__ ((unused)) int i;
7731 we want the attribute to attach to "i", not "lab". */
7732 if (label != NULL_TREE
7733 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7737 cp_parser_parse_tentatively (parser);
7738 attrs = cp_parser_attributes_opt (parser);
7739 if (attrs == NULL_TREE
7740 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7741 cp_parser_abort_tentative_parse (parser);
7742 else if (!cp_parser_parse_definitely (parser))
7745 cplus_decl_attributes (&label, attrs, 0);
7749 /* Parse an expression-statement.
7751 expression-statement:
7754 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7755 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7756 indicates whether this expression-statement is part of an
7757 expression statement. */
7760 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7762 tree statement = NULL_TREE;
7764 /* If the next token is a ';', then there is no expression
7766 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7767 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7769 /* Consume the final `;'. */
7770 cp_parser_consume_semicolon_at_end_of_statement (parser);
7772 if (in_statement_expr
7773 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7774 /* This is the final expression statement of a statement
7776 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7778 statement = finish_expr_stmt (statement);
7785 /* Parse a compound-statement.
7788 { statement-seq [opt] }
7793 { label-declaration-seq [opt] statement-seq [opt] }
7795 label-declaration-seq:
7797 label-declaration-seq label-declaration
7799 Returns a tree representing the statement. */
7802 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7807 /* Consume the `{'. */
7808 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7809 return error_mark_node;
7810 /* Begin the compound-statement. */
7811 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7812 /* If the next keyword is `__label__' we have a label declaration. */
7813 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7814 cp_parser_label_declaration (parser);
7815 /* Parse an (optional) statement-seq. */
7816 cp_parser_statement_seq_opt (parser, in_statement_expr);
7817 /* Finish the compound-statement. */
7818 finish_compound_stmt (compound_stmt);
7819 /* Consume the `}'. */
7820 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7822 return compound_stmt;
7825 /* Parse an (optional) statement-seq.
7829 statement-seq [opt] statement */
7832 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7834 /* Scan statements until there aren't any more. */
7837 cp_token *token = cp_lexer_peek_token (parser->lexer);
7839 /* If we're looking at a `}', then we've run out of statements. */
7840 if (token->type == CPP_CLOSE_BRACE
7841 || token->type == CPP_EOF
7842 || token->type == CPP_PRAGMA_EOL)
7845 /* If we are in a compound statement and find 'else' then
7846 something went wrong. */
7847 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7849 if (parser->in_statement & IN_IF_STMT)
7853 token = cp_lexer_consume_token (parser->lexer);
7854 error_at (token->location, "%<else%> without a previous %<if%>");
7858 /* Parse the statement. */
7859 cp_parser_statement (parser, in_statement_expr, true, NULL);
7863 /* Parse a selection-statement.
7865 selection-statement:
7866 if ( condition ) statement
7867 if ( condition ) statement else statement
7868 switch ( condition ) statement
7870 Returns the new IF_STMT or SWITCH_STMT.
7872 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7873 is a (possibly labeled) if statement which is not enclosed in
7874 braces and has an else clause. This is used to implement
7878 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7886 /* Peek at the next token. */
7887 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7889 /* See what kind of keyword it is. */
7890 keyword = token->keyword;
7899 /* Look for the `('. */
7900 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7902 cp_parser_skip_to_end_of_statement (parser);
7903 return error_mark_node;
7906 /* Begin the selection-statement. */
7907 if (keyword == RID_IF)
7908 statement = begin_if_stmt ();
7910 statement = begin_switch_stmt ();
7912 /* Parse the condition. */
7913 condition = cp_parser_condition (parser);
7914 /* Look for the `)'. */
7915 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7916 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7917 /*consume_paren=*/true);
7919 if (keyword == RID_IF)
7922 unsigned char in_statement;
7924 /* Add the condition. */
7925 finish_if_stmt_cond (condition, statement);
7927 /* Parse the then-clause. */
7928 in_statement = parser->in_statement;
7929 parser->in_statement |= IN_IF_STMT;
7930 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7932 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7933 add_stmt (build_empty_stmt (loc));
7934 cp_lexer_consume_token (parser->lexer);
7935 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7936 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7937 "empty body in an %<if%> statement");
7941 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7942 parser->in_statement = in_statement;
7944 finish_then_clause (statement);
7946 /* If the next token is `else', parse the else-clause. */
7947 if (cp_lexer_next_token_is_keyword (parser->lexer,
7950 /* Consume the `else' keyword. */
7951 cp_lexer_consume_token (parser->lexer);
7952 begin_else_clause (statement);
7953 /* Parse the else-clause. */
7954 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7957 loc = cp_lexer_peek_token (parser->lexer)->location;
7959 OPT_Wempty_body, "suggest braces around "
7960 "empty body in an %<else%> statement");
7961 add_stmt (build_empty_stmt (loc));
7962 cp_lexer_consume_token (parser->lexer);
7965 cp_parser_implicitly_scoped_statement (parser, NULL);
7967 finish_else_clause (statement);
7969 /* If we are currently parsing a then-clause, then
7970 IF_P will not be NULL. We set it to true to
7971 indicate that this if statement has an else clause.
7972 This may trigger the Wparentheses warning below
7973 when we get back up to the parent if statement. */
7979 /* This if statement does not have an else clause. If
7980 NESTED_IF is true, then the then-clause is an if
7981 statement which does have an else clause. We warn
7982 about the potential ambiguity. */
7984 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7985 "suggest explicit braces to avoid ambiguous"
7989 /* Now we're all done with the if-statement. */
7990 finish_if_stmt (statement);
7994 bool in_switch_statement_p;
7995 unsigned char in_statement;
7997 /* Add the condition. */
7998 finish_switch_cond (condition, statement);
8000 /* Parse the body of the switch-statement. */
8001 in_switch_statement_p = parser->in_switch_statement_p;
8002 in_statement = parser->in_statement;
8003 parser->in_switch_statement_p = true;
8004 parser->in_statement |= IN_SWITCH_STMT;
8005 cp_parser_implicitly_scoped_statement (parser, NULL);
8006 parser->in_switch_statement_p = in_switch_statement_p;
8007 parser->in_statement = in_statement;
8009 /* Now we're all done with the switch-statement. */
8010 finish_switch_stmt (statement);
8018 cp_parser_error (parser, "expected selection-statement");
8019 return error_mark_node;
8023 /* Parse a condition.
8027 type-specifier-seq declarator = initializer-clause
8028 type-specifier-seq declarator braced-init-list
8033 type-specifier-seq declarator asm-specification [opt]
8034 attributes [opt] = assignment-expression
8036 Returns the expression that should be tested. */
8039 cp_parser_condition (cp_parser* parser)
8041 cp_decl_specifier_seq type_specifiers;
8042 const char *saved_message;
8044 /* Try the declaration first. */
8045 cp_parser_parse_tentatively (parser);
8046 /* New types are not allowed in the type-specifier-seq for a
8048 saved_message = parser->type_definition_forbidden_message;
8049 parser->type_definition_forbidden_message
8050 = "types may not be defined in conditions";
8051 /* Parse the type-specifier-seq. */
8052 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8053 /*is_trailing_return=*/false,
8055 /* Restore the saved message. */
8056 parser->type_definition_forbidden_message = saved_message;
8057 /* If all is well, we might be looking at a declaration. */
8058 if (!cp_parser_error_occurred (parser))
8061 tree asm_specification;
8063 cp_declarator *declarator;
8064 tree initializer = NULL_TREE;
8066 /* Parse the declarator. */
8067 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8068 /*ctor_dtor_or_conv_p=*/NULL,
8069 /*parenthesized_p=*/NULL,
8070 /*member_p=*/false);
8071 /* Parse the attributes. */
8072 attributes = cp_parser_attributes_opt (parser);
8073 /* Parse the asm-specification. */
8074 asm_specification = cp_parser_asm_specification_opt (parser);
8075 /* If the next token is not an `=' or '{', then we might still be
8076 looking at an expression. For example:
8080 looks like a decl-specifier-seq and a declarator -- but then
8081 there is no `=', so this is an expression. */
8082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8083 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8084 cp_parser_simulate_error (parser);
8086 /* If we did see an `=' or '{', then we are looking at a declaration
8088 if (cp_parser_parse_definitely (parser))
8091 bool non_constant_p;
8092 bool flags = LOOKUP_ONLYCONVERTING;
8094 /* Create the declaration. */
8095 decl = start_decl (declarator, &type_specifiers,
8096 /*initialized_p=*/true,
8097 attributes, /*prefix_attributes=*/NULL_TREE,
8100 /* Parse the initializer. */
8101 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8103 initializer = cp_parser_braced_list (parser, &non_constant_p);
8104 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8109 /* Consume the `='. */
8110 cp_parser_require (parser, CPP_EQ, "%<=%>");
8111 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8113 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8114 maybe_warn_cpp0x ("extended initializer lists");
8116 if (!non_constant_p)
8117 initializer = fold_non_dependent_expr (initializer);
8119 /* Process the initializer. */
8120 cp_finish_decl (decl,
8121 initializer, !non_constant_p,
8126 pop_scope (pushed_scope);
8128 return convert_from_reference (decl);
8131 /* If we didn't even get past the declarator successfully, we are
8132 definitely not looking at a declaration. */
8134 cp_parser_abort_tentative_parse (parser);
8136 /* Otherwise, we are looking at an expression. */
8137 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8140 /* Parse an iteration-statement.
8142 iteration-statement:
8143 while ( condition ) statement
8144 do statement while ( expression ) ;
8145 for ( for-init-statement condition [opt] ; expression [opt] )
8148 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8151 cp_parser_iteration_statement (cp_parser* parser)
8156 unsigned char in_statement;
8158 /* Peek at the next token. */
8159 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8161 return error_mark_node;
8163 /* Remember whether or not we are already within an iteration
8165 in_statement = parser->in_statement;
8167 /* See what kind of keyword it is. */
8168 keyword = token->keyword;
8175 /* Begin the while-statement. */
8176 statement = begin_while_stmt ();
8177 /* Look for the `('. */
8178 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8179 /* Parse the condition. */
8180 condition = cp_parser_condition (parser);
8181 finish_while_stmt_cond (condition, statement);
8182 /* Look for the `)'. */
8183 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8184 /* Parse the dependent statement. */
8185 parser->in_statement = IN_ITERATION_STMT;
8186 cp_parser_already_scoped_statement (parser);
8187 parser->in_statement = in_statement;
8188 /* We're done with the while-statement. */
8189 finish_while_stmt (statement);
8197 /* Begin the do-statement. */
8198 statement = begin_do_stmt ();
8199 /* Parse the body of the do-statement. */
8200 parser->in_statement = IN_ITERATION_STMT;
8201 cp_parser_implicitly_scoped_statement (parser, NULL);
8202 parser->in_statement = in_statement;
8203 finish_do_body (statement);
8204 /* Look for the `while' keyword. */
8205 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8206 /* Look for the `('. */
8207 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8208 /* Parse the expression. */
8209 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8210 /* We're done with the do-statement. */
8211 finish_do_stmt (expression, statement);
8212 /* Look for the `)'. */
8213 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8214 /* Look for the `;'. */
8215 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8221 tree condition = NULL_TREE;
8222 tree expression = NULL_TREE;
8224 /* Begin the for-statement. */
8225 statement = begin_for_stmt ();
8226 /* Look for the `('. */
8227 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8228 /* Parse the initialization. */
8229 cp_parser_for_init_statement (parser);
8230 finish_for_init_stmt (statement);
8232 /* If there's a condition, process it. */
8233 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8234 condition = cp_parser_condition (parser);
8235 finish_for_cond (condition, statement);
8236 /* Look for the `;'. */
8237 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8239 /* If there's an expression, process it. */
8240 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8241 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8242 finish_for_expr (expression, statement);
8243 /* Look for the `)'. */
8244 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8246 /* Parse the body of the for-statement. */
8247 parser->in_statement = IN_ITERATION_STMT;
8248 cp_parser_already_scoped_statement (parser);
8249 parser->in_statement = in_statement;
8251 /* We're done with the for-statement. */
8252 finish_for_stmt (statement);
8257 cp_parser_error (parser, "expected iteration-statement");
8258 statement = error_mark_node;
8265 /* Parse a for-init-statement.
8268 expression-statement
8269 simple-declaration */
8272 cp_parser_for_init_statement (cp_parser* parser)
8274 /* If the next token is a `;', then we have an empty
8275 expression-statement. Grammatically, this is also a
8276 simple-declaration, but an invalid one, because it does not
8277 declare anything. Therefore, if we did not handle this case
8278 specially, we would issue an error message about an invalid
8280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8282 /* We're going to speculatively look for a declaration, falling back
8283 to an expression, if necessary. */
8284 cp_parser_parse_tentatively (parser);
8285 /* Parse the declaration. */
8286 cp_parser_simple_declaration (parser,
8287 /*function_definition_allowed_p=*/false);
8288 /* If the tentative parse failed, then we shall need to look for an
8289 expression-statement. */
8290 if (cp_parser_parse_definitely (parser))
8294 cp_parser_expression_statement (parser, false);
8297 /* Parse a jump-statement.
8302 return expression [opt] ;
8303 return braced-init-list ;
8311 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8314 cp_parser_jump_statement (cp_parser* parser)
8316 tree statement = error_mark_node;
8319 unsigned char in_statement;
8321 /* Peek at the next token. */
8322 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8324 return error_mark_node;
8326 /* See what kind of keyword it is. */
8327 keyword = token->keyword;
8331 in_statement = parser->in_statement & ~IN_IF_STMT;
8332 switch (in_statement)
8335 error_at (token->location, "break statement not within loop or switch");
8338 gcc_assert ((in_statement & IN_SWITCH_STMT)
8339 || in_statement == IN_ITERATION_STMT);
8340 statement = finish_break_stmt ();
8343 error_at (token->location, "invalid exit from OpenMP structured block");
8346 error_at (token->location, "break statement used with OpenMP for loop");
8349 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8353 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8356 error_at (token->location, "continue statement not within a loop");
8358 case IN_ITERATION_STMT:
8360 statement = finish_continue_stmt ();
8363 error_at (token->location, "invalid exit from OpenMP structured block");
8368 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8374 bool expr_non_constant_p;
8376 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8378 maybe_warn_cpp0x ("extended initializer lists");
8379 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8381 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8382 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8384 /* If the next token is a `;', then there is no
8387 /* Build the return-statement. */
8388 statement = finish_return_stmt (expr);
8389 /* Look for the final `;'. */
8390 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8395 /* Create the goto-statement. */
8396 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8398 /* Issue a warning about this use of a GNU extension. */
8399 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8400 /* Consume the '*' token. */
8401 cp_lexer_consume_token (parser->lexer);
8402 /* Parse the dependent expression. */
8403 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8406 finish_goto_stmt (cp_parser_identifier (parser));
8407 /* Look for the final `;'. */
8408 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8412 cp_parser_error (parser, "expected jump-statement");
8419 /* Parse a declaration-statement.
8421 declaration-statement:
8422 block-declaration */
8425 cp_parser_declaration_statement (cp_parser* parser)
8429 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8430 p = obstack_alloc (&declarator_obstack, 0);
8432 /* Parse the block-declaration. */
8433 cp_parser_block_declaration (parser, /*statement_p=*/true);
8435 /* Free any declarators allocated. */
8436 obstack_free (&declarator_obstack, p);
8438 /* Finish off the statement. */
8442 /* Some dependent statements (like `if (cond) statement'), are
8443 implicitly in their own scope. In other words, if the statement is
8444 a single statement (as opposed to a compound-statement), it is
8445 none-the-less treated as if it were enclosed in braces. Any
8446 declarations appearing in the dependent statement are out of scope
8447 after control passes that point. This function parses a statement,
8448 but ensures that is in its own scope, even if it is not a
8451 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8452 is a (possibly labeled) if statement which is not enclosed in
8453 braces and has an else clause. This is used to implement
8456 Returns the new statement. */
8459 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8466 /* Mark if () ; with a special NOP_EXPR. */
8467 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8469 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8470 cp_lexer_consume_token (parser->lexer);
8471 statement = add_stmt (build_empty_stmt (loc));
8473 /* if a compound is opened, we simply parse the statement directly. */
8474 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8475 statement = cp_parser_compound_statement (parser, NULL, false);
8476 /* If the token is not a `{', then we must take special action. */
8479 /* Create a compound-statement. */
8480 statement = begin_compound_stmt (0);
8481 /* Parse the dependent-statement. */
8482 cp_parser_statement (parser, NULL_TREE, false, if_p);
8483 /* Finish the dummy compound-statement. */
8484 finish_compound_stmt (statement);
8487 /* Return the statement. */
8491 /* For some dependent statements (like `while (cond) statement'), we
8492 have already created a scope. Therefore, even if the dependent
8493 statement is a compound-statement, we do not want to create another
8497 cp_parser_already_scoped_statement (cp_parser* parser)
8499 /* If the token is a `{', then we must take special action. */
8500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8501 cp_parser_statement (parser, NULL_TREE, false, NULL);
8504 /* Avoid calling cp_parser_compound_statement, so that we
8505 don't create a new scope. Do everything else by hand. */
8506 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8507 /* If the next keyword is `__label__' we have a label declaration. */
8508 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8509 cp_parser_label_declaration (parser);
8510 /* Parse an (optional) statement-seq. */
8511 cp_parser_statement_seq_opt (parser, NULL_TREE);
8512 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8516 /* Declarations [gram.dcl.dcl] */
8518 /* Parse an optional declaration-sequence.
8522 declaration-seq declaration */
8525 cp_parser_declaration_seq_opt (cp_parser* parser)
8531 token = cp_lexer_peek_token (parser->lexer);
8533 if (token->type == CPP_CLOSE_BRACE
8534 || token->type == CPP_EOF
8535 || token->type == CPP_PRAGMA_EOL)
8538 if (token->type == CPP_SEMICOLON)
8540 /* A declaration consisting of a single semicolon is
8541 invalid. Allow it unless we're being pedantic. */
8542 cp_lexer_consume_token (parser->lexer);
8543 if (!in_system_header)
8544 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8548 /* If we're entering or exiting a region that's implicitly
8549 extern "C", modify the lang context appropriately. */
8550 if (!parser->implicit_extern_c && token->implicit_extern_c)
8552 push_lang_context (lang_name_c);
8553 parser->implicit_extern_c = true;
8555 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8557 pop_lang_context ();
8558 parser->implicit_extern_c = false;
8561 if (token->type == CPP_PRAGMA)
8563 /* A top-level declaration can consist solely of a #pragma.
8564 A nested declaration cannot, so this is done here and not
8565 in cp_parser_declaration. (A #pragma at block scope is
8566 handled in cp_parser_statement.) */
8567 cp_parser_pragma (parser, pragma_external);
8571 /* Parse the declaration itself. */
8572 cp_parser_declaration (parser);
8576 /* Parse a declaration.
8581 template-declaration
8582 explicit-instantiation
8583 explicit-specialization
8584 linkage-specification
8585 namespace-definition
8590 __extension__ declaration */
8593 cp_parser_declaration (cp_parser* parser)
8600 /* Check for the `__extension__' keyword. */
8601 if (cp_parser_extension_opt (parser, &saved_pedantic))
8603 /* Parse the qualified declaration. */
8604 cp_parser_declaration (parser);
8605 /* Restore the PEDANTIC flag. */
8606 pedantic = saved_pedantic;
8611 /* Try to figure out what kind of declaration is present. */
8612 token1 = *cp_lexer_peek_token (parser->lexer);
8614 if (token1.type != CPP_EOF)
8615 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8618 token2.type = CPP_EOF;
8619 token2.keyword = RID_MAX;
8622 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8623 p = obstack_alloc (&declarator_obstack, 0);
8625 /* If the next token is `extern' and the following token is a string
8626 literal, then we have a linkage specification. */
8627 if (token1.keyword == RID_EXTERN
8628 && cp_parser_is_string_literal (&token2))
8629 cp_parser_linkage_specification (parser);
8630 /* If the next token is `template', then we have either a template
8631 declaration, an explicit instantiation, or an explicit
8633 else if (token1.keyword == RID_TEMPLATE)
8635 /* `template <>' indicates a template specialization. */
8636 if (token2.type == CPP_LESS
8637 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8638 cp_parser_explicit_specialization (parser);
8639 /* `template <' indicates a template declaration. */
8640 else if (token2.type == CPP_LESS)
8641 cp_parser_template_declaration (parser, /*member_p=*/false);
8642 /* Anything else must be an explicit instantiation. */
8644 cp_parser_explicit_instantiation (parser);
8646 /* If the next token is `export', then we have a template
8648 else if (token1.keyword == RID_EXPORT)
8649 cp_parser_template_declaration (parser, /*member_p=*/false);
8650 /* If the next token is `extern', 'static' or 'inline' and the one
8651 after that is `template', we have a GNU extended explicit
8652 instantiation directive. */
8653 else if (cp_parser_allow_gnu_extensions_p (parser)
8654 && (token1.keyword == RID_EXTERN
8655 || token1.keyword == RID_STATIC
8656 || token1.keyword == RID_INLINE)
8657 && token2.keyword == RID_TEMPLATE)
8658 cp_parser_explicit_instantiation (parser);
8659 /* If the next token is `namespace', check for a named or unnamed
8660 namespace definition. */
8661 else if (token1.keyword == RID_NAMESPACE
8662 && (/* A named namespace definition. */
8663 (token2.type == CPP_NAME
8664 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8666 /* An unnamed namespace definition. */
8667 || token2.type == CPP_OPEN_BRACE
8668 || token2.keyword == RID_ATTRIBUTE))
8669 cp_parser_namespace_definition (parser);
8670 /* An inline (associated) namespace definition. */
8671 else if (token1.keyword == RID_INLINE
8672 && token2.keyword == RID_NAMESPACE)
8673 cp_parser_namespace_definition (parser);
8674 /* Objective-C++ declaration/definition. */
8675 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8676 cp_parser_objc_declaration (parser);
8677 /* We must have either a block declaration or a function
8680 /* Try to parse a block-declaration, or a function-definition. */
8681 cp_parser_block_declaration (parser, /*statement_p=*/false);
8683 /* Free any declarators allocated. */
8684 obstack_free (&declarator_obstack, p);
8687 /* Parse a block-declaration.
8692 namespace-alias-definition
8699 __extension__ block-declaration
8704 static_assert-declaration
8706 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8707 part of a declaration-statement. */
8710 cp_parser_block_declaration (cp_parser *parser,
8716 /* Check for the `__extension__' keyword. */
8717 if (cp_parser_extension_opt (parser, &saved_pedantic))
8719 /* Parse the qualified declaration. */
8720 cp_parser_block_declaration (parser, statement_p);
8721 /* Restore the PEDANTIC flag. */
8722 pedantic = saved_pedantic;
8727 /* Peek at the next token to figure out which kind of declaration is
8729 token1 = cp_lexer_peek_token (parser->lexer);
8731 /* If the next keyword is `asm', we have an asm-definition. */
8732 if (token1->keyword == RID_ASM)
8735 cp_parser_commit_to_tentative_parse (parser);
8736 cp_parser_asm_definition (parser);
8738 /* If the next keyword is `namespace', we have a
8739 namespace-alias-definition. */
8740 else if (token1->keyword == RID_NAMESPACE)
8741 cp_parser_namespace_alias_definition (parser);
8742 /* If the next keyword is `using', we have either a
8743 using-declaration or a using-directive. */
8744 else if (token1->keyword == RID_USING)
8749 cp_parser_commit_to_tentative_parse (parser);
8750 /* If the token after `using' is `namespace', then we have a
8752 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8753 if (token2->keyword == RID_NAMESPACE)
8754 cp_parser_using_directive (parser);
8755 /* Otherwise, it's a using-declaration. */
8757 cp_parser_using_declaration (parser,
8758 /*access_declaration_p=*/false);
8760 /* If the next keyword is `__label__' we have a misplaced label
8762 else if (token1->keyword == RID_LABEL)
8764 cp_lexer_consume_token (parser->lexer);
8765 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8766 cp_parser_skip_to_end_of_statement (parser);
8767 /* If the next token is now a `;', consume it. */
8768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8769 cp_lexer_consume_token (parser->lexer);
8771 /* If the next token is `static_assert' we have a static assertion. */
8772 else if (token1->keyword == RID_STATIC_ASSERT)
8773 cp_parser_static_assert (parser, /*member_p=*/false);
8774 /* Anything else must be a simple-declaration. */
8776 cp_parser_simple_declaration (parser, !statement_p);
8779 /* Parse a simple-declaration.
8782 decl-specifier-seq [opt] init-declarator-list [opt] ;
8784 init-declarator-list:
8786 init-declarator-list , init-declarator
8788 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8789 function-definition as a simple-declaration. */
8792 cp_parser_simple_declaration (cp_parser* parser,
8793 bool function_definition_allowed_p)
8795 cp_decl_specifier_seq decl_specifiers;
8796 int declares_class_or_enum;
8797 bool saw_declarator;
8799 /* Defer access checks until we know what is being declared; the
8800 checks for names appearing in the decl-specifier-seq should be
8801 done as if we were in the scope of the thing being declared. */
8802 push_deferring_access_checks (dk_deferred);
8804 /* Parse the decl-specifier-seq. We have to keep track of whether
8805 or not the decl-specifier-seq declares a named class or
8806 enumeration type, since that is the only case in which the
8807 init-declarator-list is allowed to be empty.
8811 In a simple-declaration, the optional init-declarator-list can be
8812 omitted only when declaring a class or enumeration, that is when
8813 the decl-specifier-seq contains either a class-specifier, an
8814 elaborated-type-specifier, or an enum-specifier. */
8815 cp_parser_decl_specifier_seq (parser,
8816 CP_PARSER_FLAGS_OPTIONAL,
8818 &declares_class_or_enum);
8819 /* We no longer need to defer access checks. */
8820 stop_deferring_access_checks ();
8822 /* In a block scope, a valid declaration must always have a
8823 decl-specifier-seq. By not trying to parse declarators, we can
8824 resolve the declaration/expression ambiguity more quickly. */
8825 if (!function_definition_allowed_p
8826 && !decl_specifiers.any_specifiers_p)
8828 cp_parser_error (parser, "expected declaration");
8832 /* If the next two tokens are both identifiers, the code is
8833 erroneous. The usual cause of this situation is code like:
8837 where "T" should name a type -- but does not. */
8838 if (!decl_specifiers.type
8839 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8841 /* If parsing tentatively, we should commit; we really are
8842 looking at a declaration. */
8843 cp_parser_commit_to_tentative_parse (parser);
8848 /* If we have seen at least one decl-specifier, and the next token
8849 is not a parenthesis, then we must be looking at a declaration.
8850 (After "int (" we might be looking at a functional cast.) */
8851 if (decl_specifiers.any_specifiers_p
8852 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8853 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8854 && !cp_parser_error_occurred (parser))
8855 cp_parser_commit_to_tentative_parse (parser);
8857 /* Keep going until we hit the `;' at the end of the simple
8859 saw_declarator = false;
8860 while (cp_lexer_next_token_is_not (parser->lexer,
8864 bool function_definition_p;
8869 /* If we are processing next declarator, coma is expected */
8870 token = cp_lexer_peek_token (parser->lexer);
8871 gcc_assert (token->type == CPP_COMMA);
8872 cp_lexer_consume_token (parser->lexer);
8875 saw_declarator = true;
8877 /* Parse the init-declarator. */
8878 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8880 function_definition_allowed_p,
8882 declares_class_or_enum,
8883 &function_definition_p);
8884 /* If an error occurred while parsing tentatively, exit quickly.
8885 (That usually happens when in the body of a function; each
8886 statement is treated as a declaration-statement until proven
8888 if (cp_parser_error_occurred (parser))
8890 /* Handle function definitions specially. */
8891 if (function_definition_p)
8893 /* If the next token is a `,', then we are probably
8894 processing something like:
8898 which is erroneous. */
8899 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8901 cp_token *token = cp_lexer_peek_token (parser->lexer);
8902 error_at (token->location,
8904 " declarations and function-definitions is forbidden");
8906 /* Otherwise, we're done with the list of declarators. */
8909 pop_deferring_access_checks ();
8913 /* The next token should be either a `,' or a `;'. */
8914 token = cp_lexer_peek_token (parser->lexer);
8915 /* If it's a `,', there are more declarators to come. */
8916 if (token->type == CPP_COMMA)
8917 /* will be consumed next time around */;
8918 /* If it's a `;', we are done. */
8919 else if (token->type == CPP_SEMICOLON)
8921 /* Anything else is an error. */
8924 /* If we have already issued an error message we don't need
8925 to issue another one. */
8926 if (decl != error_mark_node
8927 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8928 cp_parser_error (parser, "expected %<,%> or %<;%>");
8929 /* Skip tokens until we reach the end of the statement. */
8930 cp_parser_skip_to_end_of_statement (parser);
8931 /* If the next token is now a `;', consume it. */
8932 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8933 cp_lexer_consume_token (parser->lexer);
8936 /* After the first time around, a function-definition is not
8937 allowed -- even if it was OK at first. For example:
8942 function_definition_allowed_p = false;
8945 /* Issue an error message if no declarators are present, and the
8946 decl-specifier-seq does not itself declare a class or
8948 if (!saw_declarator)
8950 if (cp_parser_declares_only_class_p (parser))
8951 shadow_tag (&decl_specifiers);
8952 /* Perform any deferred access checks. */
8953 perform_deferred_access_checks ();
8956 /* Consume the `;'. */
8957 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8960 pop_deferring_access_checks ();
8963 /* Parse a decl-specifier-seq.
8966 decl-specifier-seq [opt] decl-specifier
8969 storage-class-specifier
8980 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8982 The parser flags FLAGS is used to control type-specifier parsing.
8984 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8987 1: one of the decl-specifiers is an elaborated-type-specifier
8988 (i.e., a type declaration)
8989 2: one of the decl-specifiers is an enum-specifier or a
8990 class-specifier (i.e., a type definition)
8995 cp_parser_decl_specifier_seq (cp_parser* parser,
8996 cp_parser_flags flags,
8997 cp_decl_specifier_seq *decl_specs,
8998 int* declares_class_or_enum)
9000 bool constructor_possible_p = !parser->in_declarator_p;
9001 cp_token *start_token = NULL;
9003 /* Clear DECL_SPECS. */
9004 clear_decl_specs (decl_specs);
9006 /* Assume no class or enumeration type is declared. */
9007 *declares_class_or_enum = 0;
9009 /* Keep reading specifiers until there are no more to read. */
9013 bool found_decl_spec;
9016 /* Peek at the next token. */
9017 token = cp_lexer_peek_token (parser->lexer);
9019 /* Save the first token of the decl spec list for error
9022 start_token = token;
9023 /* Handle attributes. */
9024 if (token->keyword == RID_ATTRIBUTE)
9026 /* Parse the attributes. */
9027 decl_specs->attributes
9028 = chainon (decl_specs->attributes,
9029 cp_parser_attributes_opt (parser));
9032 /* Assume we will find a decl-specifier keyword. */
9033 found_decl_spec = true;
9034 /* If the next token is an appropriate keyword, we can simply
9035 add it to the list. */
9036 switch (token->keyword)
9042 if (!at_class_scope_p ())
9044 error_at (token->location, "%<friend%> used outside of class");
9045 cp_lexer_purge_token (parser->lexer);
9049 ++decl_specs->specs[(int) ds_friend];
9050 /* Consume the token. */
9051 cp_lexer_consume_token (parser->lexer);
9056 ++decl_specs->specs[(int) ds_constexpr];
9057 cp_lexer_consume_token (parser->lexer);
9060 /* function-specifier:
9067 cp_parser_function_specifier_opt (parser, decl_specs);
9073 ++decl_specs->specs[(int) ds_typedef];
9074 /* Consume the token. */
9075 cp_lexer_consume_token (parser->lexer);
9076 /* A constructor declarator cannot appear in a typedef. */
9077 constructor_possible_p = false;
9078 /* The "typedef" keyword can only occur in a declaration; we
9079 may as well commit at this point. */
9080 cp_parser_commit_to_tentative_parse (parser);
9082 if (decl_specs->storage_class != sc_none)
9083 decl_specs->conflicting_specifiers_p = true;
9086 /* storage-class-specifier:
9096 if (cxx_dialect == cxx98)
9098 /* Consume the token. */
9099 cp_lexer_consume_token (parser->lexer);
9101 /* Complain about `auto' as a storage specifier, if
9102 we're complaining about C++0x compatibility. */
9103 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9104 " will change meaning in C++0x; please remove it");
9106 /* Set the storage class anyway. */
9107 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9111 /* C++0x auto type-specifier. */
9112 found_decl_spec = false;
9119 /* Consume the token. */
9120 cp_lexer_consume_token (parser->lexer);
9121 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9125 /* Consume the token. */
9126 cp_lexer_consume_token (parser->lexer);
9127 ++decl_specs->specs[(int) ds_thread];
9131 /* We did not yet find a decl-specifier yet. */
9132 found_decl_spec = false;
9136 /* Constructors are a special case. The `S' in `S()' is not a
9137 decl-specifier; it is the beginning of the declarator. */
9140 && constructor_possible_p
9141 && (cp_parser_constructor_declarator_p
9142 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9144 /* If we don't have a DECL_SPEC yet, then we must be looking at
9145 a type-specifier. */
9146 if (!found_decl_spec && !constructor_p)
9148 int decl_spec_declares_class_or_enum;
9149 bool is_cv_qualifier;
9153 = cp_parser_type_specifier (parser, flags,
9155 /*is_declaration=*/true,
9156 &decl_spec_declares_class_or_enum,
9158 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9160 /* If this type-specifier referenced a user-defined type
9161 (a typedef, class-name, etc.), then we can't allow any
9162 more such type-specifiers henceforth.
9166 The longest sequence of decl-specifiers that could
9167 possibly be a type name is taken as the
9168 decl-specifier-seq of a declaration. The sequence shall
9169 be self-consistent as described below.
9173 As a general rule, at most one type-specifier is allowed
9174 in the complete decl-specifier-seq of a declaration. The
9175 only exceptions are the following:
9177 -- const or volatile can be combined with any other
9180 -- signed or unsigned can be combined with char, long,
9188 void g (const int Pc);
9190 Here, Pc is *not* part of the decl-specifier seq; it's
9191 the declarator. Therefore, once we see a type-specifier
9192 (other than a cv-qualifier), we forbid any additional
9193 user-defined types. We *do* still allow things like `int
9194 int' to be considered a decl-specifier-seq, and issue the
9195 error message later. */
9196 if (type_spec && !is_cv_qualifier)
9197 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9198 /* A constructor declarator cannot follow a type-specifier. */
9201 constructor_possible_p = false;
9202 found_decl_spec = true;
9206 /* If we still do not have a DECL_SPEC, then there are no more
9208 if (!found_decl_spec)
9211 decl_specs->any_specifiers_p = true;
9212 /* After we see one decl-specifier, further decl-specifiers are
9214 flags |= CP_PARSER_FLAGS_OPTIONAL;
9217 cp_parser_check_decl_spec (decl_specs, start_token->location);
9219 /* Don't allow a friend specifier with a class definition. */
9220 if (decl_specs->specs[(int) ds_friend] != 0
9221 && (*declares_class_or_enum & 2))
9222 error_at (start_token->location,
9223 "class definition may not be declared a friend");
9226 /* Parse an (optional) storage-class-specifier.
9228 storage-class-specifier:
9237 storage-class-specifier:
9240 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9243 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9245 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9248 if (cxx_dialect != cxx98)
9250 /* Fall through for C++98. */
9257 /* Consume the token. */
9258 return cp_lexer_consume_token (parser->lexer)->u.value;
9265 /* Parse an (optional) function-specifier.
9272 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9273 Updates DECL_SPECS, if it is non-NULL. */
9276 cp_parser_function_specifier_opt (cp_parser* parser,
9277 cp_decl_specifier_seq *decl_specs)
9279 cp_token *token = cp_lexer_peek_token (parser->lexer);
9280 switch (token->keyword)
9284 ++decl_specs->specs[(int) ds_inline];
9288 /* 14.5.2.3 [temp.mem]
9290 A member function template shall not be virtual. */
9291 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9292 error_at (token->location, "templates may not be %<virtual%>");
9293 else if (decl_specs)
9294 ++decl_specs->specs[(int) ds_virtual];
9299 ++decl_specs->specs[(int) ds_explicit];
9306 /* Consume the token. */
9307 return cp_lexer_consume_token (parser->lexer)->u.value;
9310 /* Parse a linkage-specification.
9312 linkage-specification:
9313 extern string-literal { declaration-seq [opt] }
9314 extern string-literal declaration */
9317 cp_parser_linkage_specification (cp_parser* parser)
9321 /* Look for the `extern' keyword. */
9322 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9324 /* Look for the string-literal. */
9325 linkage = cp_parser_string_literal (parser, false, false);
9327 /* Transform the literal into an identifier. If the literal is a
9328 wide-character string, or contains embedded NULs, then we can't
9329 handle it as the user wants. */
9330 if (strlen (TREE_STRING_POINTER (linkage))
9331 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9333 cp_parser_error (parser, "invalid linkage-specification");
9334 /* Assume C++ linkage. */
9335 linkage = lang_name_cplusplus;
9338 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9340 /* We're now using the new linkage. */
9341 push_lang_context (linkage);
9343 /* If the next token is a `{', then we're using the first
9345 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9347 /* Consume the `{' token. */
9348 cp_lexer_consume_token (parser->lexer);
9349 /* Parse the declarations. */
9350 cp_parser_declaration_seq_opt (parser);
9351 /* Look for the closing `}'. */
9352 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9354 /* Otherwise, there's just one declaration. */
9357 bool saved_in_unbraced_linkage_specification_p;
9359 saved_in_unbraced_linkage_specification_p
9360 = parser->in_unbraced_linkage_specification_p;
9361 parser->in_unbraced_linkage_specification_p = true;
9362 cp_parser_declaration (parser);
9363 parser->in_unbraced_linkage_specification_p
9364 = saved_in_unbraced_linkage_specification_p;
9367 /* We're done with the linkage-specification. */
9368 pop_lang_context ();
9371 /* Parse a static_assert-declaration.
9373 static_assert-declaration:
9374 static_assert ( constant-expression , string-literal ) ;
9376 If MEMBER_P, this static_assert is a class member. */
9379 cp_parser_static_assert(cp_parser *parser, bool member_p)
9384 location_t saved_loc;
9386 /* Peek at the `static_assert' token so we can keep track of exactly
9387 where the static assertion started. */
9388 token = cp_lexer_peek_token (parser->lexer);
9389 saved_loc = token->location;
9391 /* Look for the `static_assert' keyword. */
9392 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9393 "%<static_assert%>"))
9396 /* We know we are in a static assertion; commit to any tentative
9398 if (cp_parser_parsing_tentatively (parser))
9399 cp_parser_commit_to_tentative_parse (parser);
9401 /* Parse the `(' starting the static assertion condition. */
9402 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9404 /* Parse the constant-expression. */
9406 cp_parser_constant_expression (parser,
9407 /*allow_non_constant_p=*/false,
9408 /*non_constant_p=*/NULL);
9410 /* Parse the separating `,'. */
9411 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9413 /* Parse the string-literal message. */
9414 message = cp_parser_string_literal (parser,
9415 /*translate=*/false,
9418 /* A `)' completes the static assertion. */
9419 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9420 cp_parser_skip_to_closing_parenthesis (parser,
9421 /*recovering=*/true,
9423 /*consume_paren=*/true);
9425 /* A semicolon terminates the declaration. */
9426 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9428 /* Complete the static assertion, which may mean either processing
9429 the static assert now or saving it for template instantiation. */
9430 finish_static_assert (condition, message, saved_loc, member_p);
9433 /* Parse a `decltype' type. Returns the type.
9435 simple-type-specifier:
9436 decltype ( expression ) */
9439 cp_parser_decltype (cp_parser *parser)
9442 bool id_expression_or_member_access_p = false;
9443 const char *saved_message;
9444 bool saved_integral_constant_expression_p;
9445 bool saved_non_integral_constant_expression_p;
9446 cp_token *id_expr_start_token;
9448 /* Look for the `decltype' token. */
9449 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9450 return error_mark_node;
9452 /* Types cannot be defined in a `decltype' expression. Save away the
9454 saved_message = parser->type_definition_forbidden_message;
9456 /* And create the new one. */
9457 parser->type_definition_forbidden_message
9458 = "types may not be defined in %<decltype%> expressions";
9460 /* The restrictions on constant-expressions do not apply inside
9461 decltype expressions. */
9462 saved_integral_constant_expression_p
9463 = parser->integral_constant_expression_p;
9464 saved_non_integral_constant_expression_p
9465 = parser->non_integral_constant_expression_p;
9466 parser->integral_constant_expression_p = false;
9468 /* Do not actually evaluate the expression. */
9469 ++cp_unevaluated_operand;
9471 /* Do not warn about problems with the expression. */
9472 ++c_inhibit_evaluation_warnings;
9474 /* Parse the opening `('. */
9475 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9476 return error_mark_node;
9478 /* First, try parsing an id-expression. */
9479 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9480 cp_parser_parse_tentatively (parser);
9481 expr = cp_parser_id_expression (parser,
9482 /*template_keyword_p=*/false,
9483 /*check_dependency_p=*/true,
9484 /*template_p=*/NULL,
9485 /*declarator_p=*/false,
9486 /*optional_p=*/false);
9488 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9490 bool non_integral_constant_expression_p = false;
9491 tree id_expression = expr;
9493 const char *error_msg;
9495 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9496 /* Lookup the name we got back from the id-expression. */
9497 expr = cp_parser_lookup_name (parser, expr,
9499 /*is_template=*/false,
9500 /*is_namespace=*/false,
9501 /*check_dependency=*/true,
9502 /*ambiguous_decls=*/NULL,
9503 id_expr_start_token->location);
9506 && expr != error_mark_node
9507 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9508 && TREE_CODE (expr) != TYPE_DECL
9509 && (TREE_CODE (expr) != BIT_NOT_EXPR
9510 || !TYPE_P (TREE_OPERAND (expr, 0)))
9511 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9513 /* Complete lookup of the id-expression. */
9514 expr = (finish_id_expression
9515 (id_expression, expr, parser->scope, &idk,
9516 /*integral_constant_expression_p=*/false,
9517 /*allow_non_integral_constant_expression_p=*/true,
9518 &non_integral_constant_expression_p,
9519 /*template_p=*/false,
9521 /*address_p=*/false,
9522 /*template_arg_p=*/false,
9524 id_expr_start_token->location));
9526 if (expr == error_mark_node)
9527 /* We found an id-expression, but it was something that we
9528 should not have found. This is an error, not something
9529 we can recover from, so note that we found an
9530 id-expression and we'll recover as gracefully as
9532 id_expression_or_member_access_p = true;
9536 && expr != error_mark_node
9537 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9538 /* We have an id-expression. */
9539 id_expression_or_member_access_p = true;
9542 if (!id_expression_or_member_access_p)
9544 /* Abort the id-expression parse. */
9545 cp_parser_abort_tentative_parse (parser);
9547 /* Parsing tentatively, again. */
9548 cp_parser_parse_tentatively (parser);
9550 /* Parse a class member access. */
9551 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9553 /*member_access_only_p=*/true, NULL);
9556 && expr != error_mark_node
9557 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9558 /* We have an id-expression. */
9559 id_expression_or_member_access_p = true;
9562 if (id_expression_or_member_access_p)
9563 /* We have parsed the complete id-expression or member access. */
9564 cp_parser_parse_definitely (parser);
9567 bool saved_greater_than_is_operator_p;
9569 /* Abort our attempt to parse an id-expression or member access
9571 cp_parser_abort_tentative_parse (parser);
9573 /* Within a parenthesized expression, a `>' token is always
9574 the greater-than operator. */
9575 saved_greater_than_is_operator_p
9576 = parser->greater_than_is_operator_p;
9577 parser->greater_than_is_operator_p = true;
9579 /* Parse a full expression. */
9580 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9582 /* The `>' token might be the end of a template-id or
9583 template-parameter-list now. */
9584 parser->greater_than_is_operator_p
9585 = saved_greater_than_is_operator_p;
9588 /* Go back to evaluating expressions. */
9589 --cp_unevaluated_operand;
9590 --c_inhibit_evaluation_warnings;
9592 /* Restore the old message and the integral constant expression
9594 parser->type_definition_forbidden_message = saved_message;
9595 parser->integral_constant_expression_p
9596 = saved_integral_constant_expression_p;
9597 parser->non_integral_constant_expression_p
9598 = saved_non_integral_constant_expression_p;
9600 if (expr == error_mark_node)
9602 /* Skip everything up to the closing `)'. */
9603 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9604 /*consume_paren=*/true);
9605 return error_mark_node;
9608 /* Parse to the closing `)'. */
9609 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9611 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9612 /*consume_paren=*/true);
9613 return error_mark_node;
9616 return finish_decltype_type (expr, id_expression_or_member_access_p);
9619 /* Special member functions [gram.special] */
9621 /* Parse a conversion-function-id.
9623 conversion-function-id:
9624 operator conversion-type-id
9626 Returns an IDENTIFIER_NODE representing the operator. */
9629 cp_parser_conversion_function_id (cp_parser* parser)
9633 tree saved_qualifying_scope;
9634 tree saved_object_scope;
9635 tree pushed_scope = NULL_TREE;
9637 /* Look for the `operator' token. */
9638 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9639 return error_mark_node;
9640 /* When we parse the conversion-type-id, the current scope will be
9641 reset. However, we need that information in able to look up the
9642 conversion function later, so we save it here. */
9643 saved_scope = parser->scope;
9644 saved_qualifying_scope = parser->qualifying_scope;
9645 saved_object_scope = parser->object_scope;
9646 /* We must enter the scope of the class so that the names of
9647 entities declared within the class are available in the
9648 conversion-type-id. For example, consider:
9655 S::operator I() { ... }
9657 In order to see that `I' is a type-name in the definition, we
9658 must be in the scope of `S'. */
9660 pushed_scope = push_scope (saved_scope);
9661 /* Parse the conversion-type-id. */
9662 type = cp_parser_conversion_type_id (parser);
9663 /* Leave the scope of the class, if any. */
9665 pop_scope (pushed_scope);
9666 /* Restore the saved scope. */
9667 parser->scope = saved_scope;
9668 parser->qualifying_scope = saved_qualifying_scope;
9669 parser->object_scope = saved_object_scope;
9670 /* If the TYPE is invalid, indicate failure. */
9671 if (type == error_mark_node)
9672 return error_mark_node;
9673 return mangle_conv_op_name_for_type (type);
9676 /* Parse a conversion-type-id:
9679 type-specifier-seq conversion-declarator [opt]
9681 Returns the TYPE specified. */
9684 cp_parser_conversion_type_id (cp_parser* parser)
9687 cp_decl_specifier_seq type_specifiers;
9688 cp_declarator *declarator;
9689 tree type_specified;
9691 /* Parse the attributes. */
9692 attributes = cp_parser_attributes_opt (parser);
9693 /* Parse the type-specifiers. */
9694 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9695 /*is_trailing_return=*/false,
9697 /* If that didn't work, stop. */
9698 if (type_specifiers.type == error_mark_node)
9699 return error_mark_node;
9700 /* Parse the conversion-declarator. */
9701 declarator = cp_parser_conversion_declarator_opt (parser);
9703 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9704 /*initialized=*/0, &attributes);
9706 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9708 /* Don't give this error when parsing tentatively. This happens to
9709 work because we always parse this definitively once. */
9710 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9711 && type_uses_auto (type_specified))
9713 error ("invalid use of %<auto%> in conversion operator");
9714 return error_mark_node;
9717 return type_specified;
9720 /* Parse an (optional) conversion-declarator.
9722 conversion-declarator:
9723 ptr-operator conversion-declarator [opt]
9727 static cp_declarator *
9728 cp_parser_conversion_declarator_opt (cp_parser* parser)
9730 enum tree_code code;
9732 cp_cv_quals cv_quals;
9734 /* We don't know if there's a ptr-operator next, or not. */
9735 cp_parser_parse_tentatively (parser);
9736 /* Try the ptr-operator. */
9737 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9738 /* If it worked, look for more conversion-declarators. */
9739 if (cp_parser_parse_definitely (parser))
9741 cp_declarator *declarator;
9743 /* Parse another optional declarator. */
9744 declarator = cp_parser_conversion_declarator_opt (parser);
9746 return cp_parser_make_indirect_declarator
9747 (code, class_type, cv_quals, declarator);
9753 /* Parse an (optional) ctor-initializer.
9756 : mem-initializer-list
9758 Returns TRUE iff the ctor-initializer was actually present. */
9761 cp_parser_ctor_initializer_opt (cp_parser* parser)
9763 /* If the next token is not a `:', then there is no
9764 ctor-initializer. */
9765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9767 /* Do default initialization of any bases and members. */
9768 if (DECL_CONSTRUCTOR_P (current_function_decl))
9769 finish_mem_initializers (NULL_TREE);
9774 /* Consume the `:' token. */
9775 cp_lexer_consume_token (parser->lexer);
9776 /* And the mem-initializer-list. */
9777 cp_parser_mem_initializer_list (parser);
9782 /* Parse a mem-initializer-list.
9784 mem-initializer-list:
9785 mem-initializer ... [opt]
9786 mem-initializer ... [opt] , mem-initializer-list */
9789 cp_parser_mem_initializer_list (cp_parser* parser)
9791 tree mem_initializer_list = NULL_TREE;
9792 cp_token *token = cp_lexer_peek_token (parser->lexer);
9794 /* Let the semantic analysis code know that we are starting the
9795 mem-initializer-list. */
9796 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9797 error_at (token->location,
9798 "only constructors take base initializers");
9800 /* Loop through the list. */
9803 tree mem_initializer;
9805 token = cp_lexer_peek_token (parser->lexer);
9806 /* Parse the mem-initializer. */
9807 mem_initializer = cp_parser_mem_initializer (parser);
9808 /* If the next token is a `...', we're expanding member initializers. */
9809 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9811 /* Consume the `...'. */
9812 cp_lexer_consume_token (parser->lexer);
9814 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9815 can be expanded but members cannot. */
9816 if (mem_initializer != error_mark_node
9817 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9819 error_at (token->location,
9820 "cannot expand initializer for member %<%D%>",
9821 TREE_PURPOSE (mem_initializer));
9822 mem_initializer = error_mark_node;
9825 /* Construct the pack expansion type. */
9826 if (mem_initializer != error_mark_node)
9827 mem_initializer = make_pack_expansion (mem_initializer);
9829 /* Add it to the list, unless it was erroneous. */
9830 if (mem_initializer != error_mark_node)
9832 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9833 mem_initializer_list = mem_initializer;
9835 /* If the next token is not a `,', we're done. */
9836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9838 /* Consume the `,' token. */
9839 cp_lexer_consume_token (parser->lexer);
9842 /* Perform semantic analysis. */
9843 if (DECL_CONSTRUCTOR_P (current_function_decl))
9844 finish_mem_initializers (mem_initializer_list);
9847 /* Parse a mem-initializer.
9850 mem-initializer-id ( expression-list [opt] )
9851 mem-initializer-id braced-init-list
9856 ( expression-list [opt] )
9858 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9859 class) or FIELD_DECL (for a non-static data member) to initialize;
9860 the TREE_VALUE is the expression-list. An empty initialization
9861 list is represented by void_list_node. */
9864 cp_parser_mem_initializer (cp_parser* parser)
9866 tree mem_initializer_id;
9867 tree expression_list;
9869 cp_token *token = cp_lexer_peek_token (parser->lexer);
9871 /* Find out what is being initialized. */
9872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9874 permerror (token->location,
9875 "anachronistic old-style base class initializer");
9876 mem_initializer_id = NULL_TREE;
9880 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9881 if (mem_initializer_id == error_mark_node)
9882 return mem_initializer_id;
9884 member = expand_member_init (mem_initializer_id);
9885 if (member && !DECL_P (member))
9886 in_base_initializer = 1;
9888 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9890 bool expr_non_constant_p;
9891 maybe_warn_cpp0x ("extended initializer lists");
9892 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9893 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9894 expression_list = build_tree_list (NULL_TREE, expression_list);
9899 vec = cp_parser_parenthesized_expression_list (parser, false,
9901 /*allow_expansion_p=*/true,
9902 /*non_constant_p=*/NULL);
9904 return error_mark_node;
9905 expression_list = build_tree_list_vec (vec);
9906 release_tree_vector (vec);
9909 if (expression_list == error_mark_node)
9910 return error_mark_node;
9911 if (!expression_list)
9912 expression_list = void_type_node;
9914 in_base_initializer = 0;
9916 return member ? build_tree_list (member, expression_list) : error_mark_node;
9919 /* Parse a mem-initializer-id.
9922 :: [opt] nested-name-specifier [opt] class-name
9925 Returns a TYPE indicating the class to be initializer for the first
9926 production. Returns an IDENTIFIER_NODE indicating the data member
9927 to be initialized for the second production. */
9930 cp_parser_mem_initializer_id (cp_parser* parser)
9932 bool global_scope_p;
9933 bool nested_name_specifier_p;
9934 bool template_p = false;
9937 cp_token *token = cp_lexer_peek_token (parser->lexer);
9939 /* `typename' is not allowed in this context ([temp.res]). */
9940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9942 error_at (token->location,
9943 "keyword %<typename%> not allowed in this context (a qualified "
9944 "member initializer is implicitly a type)");
9945 cp_lexer_consume_token (parser->lexer);
9947 /* Look for the optional `::' operator. */
9949 = (cp_parser_global_scope_opt (parser,
9950 /*current_scope_valid_p=*/false)
9952 /* Look for the optional nested-name-specifier. The simplest way to
9957 The keyword `typename' is not permitted in a base-specifier or
9958 mem-initializer; in these contexts a qualified name that
9959 depends on a template-parameter is implicitly assumed to be a
9962 is to assume that we have seen the `typename' keyword at this
9964 nested_name_specifier_p
9965 = (cp_parser_nested_name_specifier_opt (parser,
9966 /*typename_keyword_p=*/true,
9967 /*check_dependency_p=*/true,
9969 /*is_declaration=*/true)
9971 if (nested_name_specifier_p)
9972 template_p = cp_parser_optional_template_keyword (parser);
9973 /* If there is a `::' operator or a nested-name-specifier, then we
9974 are definitely looking for a class-name. */
9975 if (global_scope_p || nested_name_specifier_p)
9976 return cp_parser_class_name (parser,
9977 /*typename_keyword_p=*/true,
9978 /*template_keyword_p=*/template_p,
9980 /*check_dependency_p=*/true,
9981 /*class_head_p=*/false,
9982 /*is_declaration=*/true);
9983 /* Otherwise, we could also be looking for an ordinary identifier. */
9984 cp_parser_parse_tentatively (parser);
9985 /* Try a class-name. */
9986 id = cp_parser_class_name (parser,
9987 /*typename_keyword_p=*/true,
9988 /*template_keyword_p=*/false,
9990 /*check_dependency_p=*/true,
9991 /*class_head_p=*/false,
9992 /*is_declaration=*/true);
9993 /* If we found one, we're done. */
9994 if (cp_parser_parse_definitely (parser))
9996 /* Otherwise, look for an ordinary identifier. */
9997 return cp_parser_identifier (parser);
10000 /* Overloading [gram.over] */
10002 /* Parse an operator-function-id.
10004 operator-function-id:
10007 Returns an IDENTIFIER_NODE for the operator which is a
10008 human-readable spelling of the identifier, e.g., `operator +'. */
10011 cp_parser_operator_function_id (cp_parser* parser)
10013 /* Look for the `operator' keyword. */
10014 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10015 return error_mark_node;
10016 /* And then the name of the operator itself. */
10017 return cp_parser_operator (parser);
10020 /* Parse an operator.
10023 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10024 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10025 || ++ -- , ->* -> () []
10032 Returns an IDENTIFIER_NODE for the operator which is a
10033 human-readable spelling of the identifier, e.g., `operator +'. */
10036 cp_parser_operator (cp_parser* parser)
10038 tree id = NULL_TREE;
10041 /* Peek at the next token. */
10042 token = cp_lexer_peek_token (parser->lexer);
10043 /* Figure out which operator we have. */
10044 switch (token->type)
10050 /* The keyword should be either `new' or `delete'. */
10051 if (token->keyword == RID_NEW)
10053 else if (token->keyword == RID_DELETE)
10058 /* Consume the `new' or `delete' token. */
10059 cp_lexer_consume_token (parser->lexer);
10061 /* Peek at the next token. */
10062 token = cp_lexer_peek_token (parser->lexer);
10063 /* If it's a `[' token then this is the array variant of the
10065 if (token->type == CPP_OPEN_SQUARE)
10067 /* Consume the `[' token. */
10068 cp_lexer_consume_token (parser->lexer);
10069 /* Look for the `]' token. */
10070 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10071 id = ansi_opname (op == NEW_EXPR
10072 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10074 /* Otherwise, we have the non-array variant. */
10076 id = ansi_opname (op);
10082 id = ansi_opname (PLUS_EXPR);
10086 id = ansi_opname (MINUS_EXPR);
10090 id = ansi_opname (MULT_EXPR);
10094 id = ansi_opname (TRUNC_DIV_EXPR);
10098 id = ansi_opname (TRUNC_MOD_EXPR);
10102 id = ansi_opname (BIT_XOR_EXPR);
10106 id = ansi_opname (BIT_AND_EXPR);
10110 id = ansi_opname (BIT_IOR_EXPR);
10114 id = ansi_opname (BIT_NOT_EXPR);
10118 id = ansi_opname (TRUTH_NOT_EXPR);
10122 id = ansi_assopname (NOP_EXPR);
10126 id = ansi_opname (LT_EXPR);
10130 id = ansi_opname (GT_EXPR);
10134 id = ansi_assopname (PLUS_EXPR);
10138 id = ansi_assopname (MINUS_EXPR);
10142 id = ansi_assopname (MULT_EXPR);
10146 id = ansi_assopname (TRUNC_DIV_EXPR);
10150 id = ansi_assopname (TRUNC_MOD_EXPR);
10154 id = ansi_assopname (BIT_XOR_EXPR);
10158 id = ansi_assopname (BIT_AND_EXPR);
10162 id = ansi_assopname (BIT_IOR_EXPR);
10166 id = ansi_opname (LSHIFT_EXPR);
10170 id = ansi_opname (RSHIFT_EXPR);
10173 case CPP_LSHIFT_EQ:
10174 id = ansi_assopname (LSHIFT_EXPR);
10177 case CPP_RSHIFT_EQ:
10178 id = ansi_assopname (RSHIFT_EXPR);
10182 id = ansi_opname (EQ_EXPR);
10186 id = ansi_opname (NE_EXPR);
10190 id = ansi_opname (LE_EXPR);
10193 case CPP_GREATER_EQ:
10194 id = ansi_opname (GE_EXPR);
10198 id = ansi_opname (TRUTH_ANDIF_EXPR);
10202 id = ansi_opname (TRUTH_ORIF_EXPR);
10205 case CPP_PLUS_PLUS:
10206 id = ansi_opname (POSTINCREMENT_EXPR);
10209 case CPP_MINUS_MINUS:
10210 id = ansi_opname (PREDECREMENT_EXPR);
10214 id = ansi_opname (COMPOUND_EXPR);
10217 case CPP_DEREF_STAR:
10218 id = ansi_opname (MEMBER_REF);
10222 id = ansi_opname (COMPONENT_REF);
10225 case CPP_OPEN_PAREN:
10226 /* Consume the `('. */
10227 cp_lexer_consume_token (parser->lexer);
10228 /* Look for the matching `)'. */
10229 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10230 return ansi_opname (CALL_EXPR);
10232 case CPP_OPEN_SQUARE:
10233 /* Consume the `['. */
10234 cp_lexer_consume_token (parser->lexer);
10235 /* Look for the matching `]'. */
10236 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10237 return ansi_opname (ARRAY_REF);
10240 /* Anything else is an error. */
10244 /* If we have selected an identifier, we need to consume the
10247 cp_lexer_consume_token (parser->lexer);
10248 /* Otherwise, no valid operator name was present. */
10251 cp_parser_error (parser, "expected operator");
10252 id = error_mark_node;
10258 /* Parse a template-declaration.
10260 template-declaration:
10261 export [opt] template < template-parameter-list > declaration
10263 If MEMBER_P is TRUE, this template-declaration occurs within a
10266 The grammar rule given by the standard isn't correct. What
10267 is really meant is:
10269 template-declaration:
10270 export [opt] template-parameter-list-seq
10271 decl-specifier-seq [opt] init-declarator [opt] ;
10272 export [opt] template-parameter-list-seq
10273 function-definition
10275 template-parameter-list-seq:
10276 template-parameter-list-seq [opt]
10277 template < template-parameter-list > */
10280 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10282 /* Check for `export'. */
10283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10285 /* Consume the `export' token. */
10286 cp_lexer_consume_token (parser->lexer);
10287 /* Warn that we do not support `export'. */
10288 warning (0, "keyword %<export%> not implemented, and will be ignored");
10291 cp_parser_template_declaration_after_export (parser, member_p);
10294 /* Parse a template-parameter-list.
10296 template-parameter-list:
10298 template-parameter-list , template-parameter
10300 Returns a TREE_LIST. Each node represents a template parameter.
10301 The nodes are connected via their TREE_CHAINs. */
10304 cp_parser_template_parameter_list (cp_parser* parser)
10306 tree parameter_list = NULL_TREE;
10308 begin_template_parm_list ();
10313 bool is_parameter_pack;
10314 location_t parm_loc;
10316 /* Parse the template-parameter. */
10317 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10318 parameter = cp_parser_template_parameter (parser,
10320 &is_parameter_pack);
10321 /* Add it to the list. */
10322 if (parameter != error_mark_node)
10323 parameter_list = process_template_parm (parameter_list,
10327 is_parameter_pack);
10330 tree err_parm = build_tree_list (parameter, parameter);
10331 TREE_VALUE (err_parm) = error_mark_node;
10332 parameter_list = chainon (parameter_list, err_parm);
10335 /* If the next token is not a `,', we're done. */
10336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10338 /* Otherwise, consume the `,' token. */
10339 cp_lexer_consume_token (parser->lexer);
10342 return end_template_parm_list (parameter_list);
10345 /* Parse a template-parameter.
10347 template-parameter:
10349 parameter-declaration
10351 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10352 the parameter. The TREE_PURPOSE is the default value, if any.
10353 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10354 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10355 set to true iff this parameter is a parameter pack. */
10358 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10359 bool *is_parameter_pack)
10362 cp_parameter_declarator *parameter_declarator;
10363 cp_declarator *id_declarator;
10366 /* Assume it is a type parameter or a template parameter. */
10367 *is_non_type = false;
10368 /* Assume it not a parameter pack. */
10369 *is_parameter_pack = false;
10370 /* Peek at the next token. */
10371 token = cp_lexer_peek_token (parser->lexer);
10372 /* If it is `class' or `template', we have a type-parameter. */
10373 if (token->keyword == RID_TEMPLATE)
10374 return cp_parser_type_parameter (parser, is_parameter_pack);
10375 /* If it is `class' or `typename' we do not know yet whether it is a
10376 type parameter or a non-type parameter. Consider:
10378 template <typename T, typename T::X X> ...
10382 template <class C, class D*> ...
10384 Here, the first parameter is a type parameter, and the second is
10385 a non-type parameter. We can tell by looking at the token after
10386 the identifier -- if it is a `,', `=', or `>' then we have a type
10388 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10390 /* Peek at the token after `class' or `typename'. */
10391 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10392 /* If it's an ellipsis, we have a template type parameter
10394 if (token->type == CPP_ELLIPSIS)
10395 return cp_parser_type_parameter (parser, is_parameter_pack);
10396 /* If it's an identifier, skip it. */
10397 if (token->type == CPP_NAME)
10398 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10399 /* Now, see if the token looks like the end of a template
10401 if (token->type == CPP_COMMA
10402 || token->type == CPP_EQ
10403 || token->type == CPP_GREATER)
10404 return cp_parser_type_parameter (parser, is_parameter_pack);
10407 /* Otherwise, it is a non-type parameter.
10411 When parsing a default template-argument for a non-type
10412 template-parameter, the first non-nested `>' is taken as the end
10413 of the template parameter-list rather than a greater-than
10415 *is_non_type = true;
10416 parameter_declarator
10417 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10418 /*parenthesized_p=*/NULL);
10420 /* If the parameter declaration is marked as a parameter pack, set
10421 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10422 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10424 if (parameter_declarator
10425 && parameter_declarator->declarator
10426 && parameter_declarator->declarator->parameter_pack_p)
10428 *is_parameter_pack = true;
10429 parameter_declarator->declarator->parameter_pack_p = false;
10432 /* If the next token is an ellipsis, and we don't already have it
10433 marked as a parameter pack, then we have a parameter pack (that
10434 has no declarator). */
10435 if (!*is_parameter_pack
10436 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10437 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10439 /* Consume the `...'. */
10440 cp_lexer_consume_token (parser->lexer);
10441 maybe_warn_variadic_templates ();
10443 *is_parameter_pack = true;
10445 /* We might end up with a pack expansion as the type of the non-type
10446 template parameter, in which case this is a non-type template
10448 else if (parameter_declarator
10449 && parameter_declarator->decl_specifiers.type
10450 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10452 *is_parameter_pack = true;
10453 parameter_declarator->decl_specifiers.type =
10454 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10457 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10459 /* Parameter packs cannot have default arguments. However, a
10460 user may try to do so, so we'll parse them and give an
10461 appropriate diagnostic here. */
10463 /* Consume the `='. */
10464 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10465 cp_lexer_consume_token (parser->lexer);
10467 /* Find the name of the parameter pack. */
10468 id_declarator = parameter_declarator->declarator;
10469 while (id_declarator && id_declarator->kind != cdk_id)
10470 id_declarator = id_declarator->declarator;
10472 if (id_declarator && id_declarator->kind == cdk_id)
10473 error_at (start_token->location,
10474 "template parameter pack %qD cannot have a default argument",
10475 id_declarator->u.id.unqualified_name);
10477 error_at (start_token->location,
10478 "template parameter pack cannot have a default argument");
10480 /* Parse the default argument, but throw away the result. */
10481 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10484 parm = grokdeclarator (parameter_declarator->declarator,
10485 ¶meter_declarator->decl_specifiers,
10486 PARM, /*initialized=*/0,
10487 /*attrlist=*/NULL);
10488 if (parm == error_mark_node)
10489 return error_mark_node;
10491 return build_tree_list (parameter_declarator->default_argument, parm);
10494 /* Parse a type-parameter.
10497 class identifier [opt]
10498 class identifier [opt] = type-id
10499 typename identifier [opt]
10500 typename identifier [opt] = type-id
10501 template < template-parameter-list > class identifier [opt]
10502 template < template-parameter-list > class identifier [opt]
10505 GNU Extension (variadic templates):
10508 class ... identifier [opt]
10509 typename ... identifier [opt]
10511 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10512 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10513 the declaration of the parameter.
10515 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10518 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10523 /* Look for a keyword to tell us what kind of parameter this is. */
10524 token = cp_parser_require (parser, CPP_KEYWORD,
10525 "%<class%>, %<typename%>, or %<template%>");
10527 return error_mark_node;
10529 switch (token->keyword)
10535 tree default_argument;
10537 /* If the next token is an ellipsis, we have a template
10539 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10541 /* Consume the `...' token. */
10542 cp_lexer_consume_token (parser->lexer);
10543 maybe_warn_variadic_templates ();
10545 *is_parameter_pack = true;
10548 /* If the next token is an identifier, then it names the
10550 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10551 identifier = cp_parser_identifier (parser);
10553 identifier = NULL_TREE;
10555 /* Create the parameter. */
10556 parameter = finish_template_type_parm (class_type_node, identifier);
10558 /* If the next token is an `=', we have a default argument. */
10559 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10561 /* Consume the `=' token. */
10562 cp_lexer_consume_token (parser->lexer);
10563 /* Parse the default-argument. */
10564 push_deferring_access_checks (dk_no_deferred);
10565 default_argument = cp_parser_type_id (parser);
10567 /* Template parameter packs cannot have default
10569 if (*is_parameter_pack)
10572 error_at (token->location,
10573 "template parameter pack %qD cannot have a "
10574 "default argument", identifier);
10576 error_at (token->location,
10577 "template parameter packs cannot have "
10578 "default arguments");
10579 default_argument = NULL_TREE;
10581 pop_deferring_access_checks ();
10584 default_argument = NULL_TREE;
10586 /* Create the combined representation of the parameter and the
10587 default argument. */
10588 parameter = build_tree_list (default_argument, parameter);
10594 tree parameter_list;
10596 tree default_argument;
10598 /* Look for the `<'. */
10599 cp_parser_require (parser, CPP_LESS, "%<<%>");
10600 /* Parse the template-parameter-list. */
10601 parameter_list = cp_parser_template_parameter_list (parser);
10602 /* Look for the `>'. */
10603 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10604 /* Look for the `class' keyword. */
10605 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10606 /* If the next token is an ellipsis, we have a template
10608 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10610 /* Consume the `...' token. */
10611 cp_lexer_consume_token (parser->lexer);
10612 maybe_warn_variadic_templates ();
10614 *is_parameter_pack = true;
10616 /* If the next token is an `=', then there is a
10617 default-argument. If the next token is a `>', we are at
10618 the end of the parameter-list. If the next token is a `,',
10619 then we are at the end of this parameter. */
10620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10621 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10622 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10624 identifier = cp_parser_identifier (parser);
10625 /* Treat invalid names as if the parameter were nameless. */
10626 if (identifier == error_mark_node)
10627 identifier = NULL_TREE;
10630 identifier = NULL_TREE;
10632 /* Create the template parameter. */
10633 parameter = finish_template_template_parm (class_type_node,
10636 /* If the next token is an `=', then there is a
10637 default-argument. */
10638 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10642 /* Consume the `='. */
10643 cp_lexer_consume_token (parser->lexer);
10644 /* Parse the id-expression. */
10645 push_deferring_access_checks (dk_no_deferred);
10646 /* save token before parsing the id-expression, for error
10648 token = cp_lexer_peek_token (parser->lexer);
10650 = cp_parser_id_expression (parser,
10651 /*template_keyword_p=*/false,
10652 /*check_dependency_p=*/true,
10653 /*template_p=*/&is_template,
10654 /*declarator_p=*/false,
10655 /*optional_p=*/false);
10656 if (TREE_CODE (default_argument) == TYPE_DECL)
10657 /* If the id-expression was a template-id that refers to
10658 a template-class, we already have the declaration here,
10659 so no further lookup is needed. */
10662 /* Look up the name. */
10664 = cp_parser_lookup_name (parser, default_argument,
10666 /*is_template=*/is_template,
10667 /*is_namespace=*/false,
10668 /*check_dependency=*/true,
10669 /*ambiguous_decls=*/NULL,
10671 /* See if the default argument is valid. */
10673 = check_template_template_default_arg (default_argument);
10675 /* Template parameter packs cannot have default
10677 if (*is_parameter_pack)
10680 error_at (token->location,
10681 "template parameter pack %qD cannot "
10682 "have a default argument",
10685 error_at (token->location, "template parameter packs cannot "
10686 "have default arguments");
10687 default_argument = NULL_TREE;
10689 pop_deferring_access_checks ();
10692 default_argument = NULL_TREE;
10694 /* Create the combined representation of the parameter and the
10695 default argument. */
10696 parameter = build_tree_list (default_argument, parameter);
10701 gcc_unreachable ();
10708 /* Parse a template-id.
10711 template-name < template-argument-list [opt] >
10713 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10714 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10715 returned. Otherwise, if the template-name names a function, or set
10716 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10717 names a class, returns a TYPE_DECL for the specialization.
10719 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10720 uninstantiated templates. */
10723 cp_parser_template_id (cp_parser *parser,
10724 bool template_keyword_p,
10725 bool check_dependency_p,
10726 bool is_declaration)
10732 cp_token_position start_of_id = 0;
10733 deferred_access_check *chk;
10734 VEC (deferred_access_check,gc) *access_check;
10735 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10736 bool is_identifier;
10738 /* If the next token corresponds to a template-id, there is no need
10740 next_token = cp_lexer_peek_token (parser->lexer);
10741 if (next_token->type == CPP_TEMPLATE_ID)
10743 struct tree_check *check_value;
10745 /* Get the stored value. */
10746 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10747 /* Perform any access checks that were deferred. */
10748 access_check = check_value->checks;
10752 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10755 perform_or_defer_access_check (chk->binfo,
10760 /* Return the stored value. */
10761 return check_value->value;
10764 /* Avoid performing name lookup if there is no possibility of
10765 finding a template-id. */
10766 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10767 || (next_token->type == CPP_NAME
10768 && !cp_parser_nth_token_starts_template_argument_list_p
10771 cp_parser_error (parser, "expected template-id");
10772 return error_mark_node;
10775 /* Remember where the template-id starts. */
10776 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10777 start_of_id = cp_lexer_token_position (parser->lexer, false);
10779 push_deferring_access_checks (dk_deferred);
10781 /* Parse the template-name. */
10782 is_identifier = false;
10783 token = cp_lexer_peek_token (parser->lexer);
10784 templ = cp_parser_template_name (parser, template_keyword_p,
10785 check_dependency_p,
10788 if (templ == error_mark_node || is_identifier)
10790 pop_deferring_access_checks ();
10794 /* If we find the sequence `[:' after a template-name, it's probably
10795 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10796 parse correctly the argument list. */
10797 next_token = cp_lexer_peek_token (parser->lexer);
10798 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10799 if (next_token->type == CPP_OPEN_SQUARE
10800 && next_token->flags & DIGRAPH
10801 && next_token_2->type == CPP_COLON
10802 && !(next_token_2->flags & PREV_WHITE))
10804 cp_parser_parse_tentatively (parser);
10805 /* Change `:' into `::'. */
10806 next_token_2->type = CPP_SCOPE;
10807 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10809 cp_lexer_consume_token (parser->lexer);
10811 /* Parse the arguments. */
10812 arguments = cp_parser_enclosed_template_argument_list (parser);
10813 if (!cp_parser_parse_definitely (parser))
10815 /* If we couldn't parse an argument list, then we revert our changes
10816 and return simply an error. Maybe this is not a template-id
10818 next_token_2->type = CPP_COLON;
10819 cp_parser_error (parser, "expected %<<%>");
10820 pop_deferring_access_checks ();
10821 return error_mark_node;
10823 /* Otherwise, emit an error about the invalid digraph, but continue
10824 parsing because we got our argument list. */
10825 if (permerror (next_token->location,
10826 "%<<::%> cannot begin a template-argument list"))
10828 static bool hint = false;
10829 inform (next_token->location,
10830 "%<<:%> is an alternate spelling for %<[%>."
10831 " Insert whitespace between %<<%> and %<::%>");
10832 if (!hint && !flag_permissive)
10834 inform (next_token->location, "(if you use %<-fpermissive%>"
10835 " G++ will accept your code)");
10842 /* Look for the `<' that starts the template-argument-list. */
10843 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10845 pop_deferring_access_checks ();
10846 return error_mark_node;
10848 /* Parse the arguments. */
10849 arguments = cp_parser_enclosed_template_argument_list (parser);
10852 /* Build a representation of the specialization. */
10853 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10854 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10855 else if (DECL_CLASS_TEMPLATE_P (templ)
10856 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10858 bool entering_scope;
10859 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10860 template (rather than some instantiation thereof) only if
10861 is not nested within some other construct. For example, in
10862 "template <typename T> void f(T) { A<T>::", A<T> is just an
10863 instantiation of A. */
10864 entering_scope = (template_parm_scope_p ()
10865 && cp_lexer_next_token_is (parser->lexer,
10868 = finish_template_type (templ, arguments, entering_scope);
10872 /* If it's not a class-template or a template-template, it should be
10873 a function-template. */
10874 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10875 || TREE_CODE (templ) == OVERLOAD
10876 || BASELINK_P (templ)));
10878 template_id = lookup_template_function (templ, arguments);
10881 /* If parsing tentatively, replace the sequence of tokens that makes
10882 up the template-id with a CPP_TEMPLATE_ID token. That way,
10883 should we re-parse the token stream, we will not have to repeat
10884 the effort required to do the parse, nor will we issue duplicate
10885 error messages about problems during instantiation of the
10889 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10891 /* Reset the contents of the START_OF_ID token. */
10892 token->type = CPP_TEMPLATE_ID;
10893 /* Retrieve any deferred checks. Do not pop this access checks yet
10894 so the memory will not be reclaimed during token replacing below. */
10895 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10896 token->u.tree_check_value->value = template_id;
10897 token->u.tree_check_value->checks = get_deferred_access_checks ();
10898 token->keyword = RID_MAX;
10900 /* Purge all subsequent tokens. */
10901 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10903 /* ??? Can we actually assume that, if template_id ==
10904 error_mark_node, we will have issued a diagnostic to the
10905 user, as opposed to simply marking the tentative parse as
10907 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10908 error_at (token->location, "parse error in template argument list");
10911 pop_deferring_access_checks ();
10912 return template_id;
10915 /* Parse a template-name.
10920 The standard should actually say:
10924 operator-function-id
10926 A defect report has been filed about this issue.
10928 A conversion-function-id cannot be a template name because they cannot
10929 be part of a template-id. In fact, looking at this code:
10931 a.operator K<int>()
10933 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10934 It is impossible to call a templated conversion-function-id with an
10935 explicit argument list, since the only allowed template parameter is
10936 the type to which it is converting.
10938 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10939 `template' keyword, in a construction like:
10943 In that case `f' is taken to be a template-name, even though there
10944 is no way of knowing for sure.
10946 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10947 name refers to a set of overloaded functions, at least one of which
10948 is a template, or an IDENTIFIER_NODE with the name of the template,
10949 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10950 names are looked up inside uninstantiated templates. */
10953 cp_parser_template_name (cp_parser* parser,
10954 bool template_keyword_p,
10955 bool check_dependency_p,
10956 bool is_declaration,
10957 bool *is_identifier)
10962 cp_token *token = cp_lexer_peek_token (parser->lexer);
10964 /* If the next token is `operator', then we have either an
10965 operator-function-id or a conversion-function-id. */
10966 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10968 /* We don't know whether we're looking at an
10969 operator-function-id or a conversion-function-id. */
10970 cp_parser_parse_tentatively (parser);
10971 /* Try an operator-function-id. */
10972 identifier = cp_parser_operator_function_id (parser);
10973 /* If that didn't work, try a conversion-function-id. */
10974 if (!cp_parser_parse_definitely (parser))
10976 cp_parser_error (parser, "expected template-name");
10977 return error_mark_node;
10980 /* Look for the identifier. */
10982 identifier = cp_parser_identifier (parser);
10984 /* If we didn't find an identifier, we don't have a template-id. */
10985 if (identifier == error_mark_node)
10986 return error_mark_node;
10988 /* If the name immediately followed the `template' keyword, then it
10989 is a template-name. However, if the next token is not `<', then
10990 we do not treat it as a template-name, since it is not being used
10991 as part of a template-id. This enables us to handle constructs
10994 template <typename T> struct S { S(); };
10995 template <typename T> S<T>::S();
10997 correctly. We would treat `S' as a template -- if it were `S<T>'
10998 -- but we do not if there is no `<'. */
11000 if (processing_template_decl
11001 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11003 /* In a declaration, in a dependent context, we pretend that the
11004 "template" keyword was present in order to improve error
11005 recovery. For example, given:
11007 template <typename T> void f(T::X<int>);
11009 we want to treat "X<int>" as a template-id. */
11011 && !template_keyword_p
11012 && parser->scope && TYPE_P (parser->scope)
11013 && check_dependency_p
11014 && dependent_scope_p (parser->scope)
11015 /* Do not do this for dtors (or ctors), since they never
11016 need the template keyword before their name. */
11017 && !constructor_name_p (identifier, parser->scope))
11019 cp_token_position start = 0;
11021 /* Explain what went wrong. */
11022 error_at (token->location, "non-template %qD used as template",
11024 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11025 parser->scope, identifier);
11026 /* If parsing tentatively, find the location of the "<" token. */
11027 if (cp_parser_simulate_error (parser))
11028 start = cp_lexer_token_position (parser->lexer, true);
11029 /* Parse the template arguments so that we can issue error
11030 messages about them. */
11031 cp_lexer_consume_token (parser->lexer);
11032 cp_parser_enclosed_template_argument_list (parser);
11033 /* Skip tokens until we find a good place from which to
11034 continue parsing. */
11035 cp_parser_skip_to_closing_parenthesis (parser,
11036 /*recovering=*/true,
11038 /*consume_paren=*/false);
11039 /* If parsing tentatively, permanently remove the
11040 template argument list. That will prevent duplicate
11041 error messages from being issued about the missing
11042 "template" keyword. */
11044 cp_lexer_purge_tokens_after (parser->lexer, start);
11046 *is_identifier = true;
11050 /* If the "template" keyword is present, then there is generally
11051 no point in doing name-lookup, so we just return IDENTIFIER.
11052 But, if the qualifying scope is non-dependent then we can
11053 (and must) do name-lookup normally. */
11054 if (template_keyword_p
11056 || (TYPE_P (parser->scope)
11057 && dependent_type_p (parser->scope))))
11061 /* Look up the name. */
11062 decl = cp_parser_lookup_name (parser, identifier,
11064 /*is_template=*/false,
11065 /*is_namespace=*/false,
11066 check_dependency_p,
11067 /*ambiguous_decls=*/NULL,
11069 decl = maybe_get_template_decl_from_type_decl (decl);
11071 /* If DECL is a template, then the name was a template-name. */
11072 if (TREE_CODE (decl) == TEMPLATE_DECL)
11076 tree fn = NULL_TREE;
11078 /* The standard does not explicitly indicate whether a name that
11079 names a set of overloaded declarations, some of which are
11080 templates, is a template-name. However, such a name should
11081 be a template-name; otherwise, there is no way to form a
11082 template-id for the overloaded templates. */
11083 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11084 if (TREE_CODE (fns) == OVERLOAD)
11085 for (fn = fns; fn; fn = OVL_NEXT (fn))
11086 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11091 /* The name does not name a template. */
11092 cp_parser_error (parser, "expected template-name");
11093 return error_mark_node;
11097 /* If DECL is dependent, and refers to a function, then just return
11098 its name; we will look it up again during template instantiation. */
11099 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11101 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11102 if (TYPE_P (scope) && dependent_type_p (scope))
11109 /* Parse a template-argument-list.
11111 template-argument-list:
11112 template-argument ... [opt]
11113 template-argument-list , template-argument ... [opt]
11115 Returns a TREE_VEC containing the arguments. */
11118 cp_parser_template_argument_list (cp_parser* parser)
11120 tree fixed_args[10];
11121 unsigned n_args = 0;
11122 unsigned alloced = 10;
11123 tree *arg_ary = fixed_args;
11125 bool saved_in_template_argument_list_p;
11127 bool saved_non_ice_p;
11129 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11130 parser->in_template_argument_list_p = true;
11131 /* Even if the template-id appears in an integral
11132 constant-expression, the contents of the argument list do
11134 saved_ice_p = parser->integral_constant_expression_p;
11135 parser->integral_constant_expression_p = false;
11136 saved_non_ice_p = parser->non_integral_constant_expression_p;
11137 parser->non_integral_constant_expression_p = false;
11138 /* Parse the arguments. */
11144 /* Consume the comma. */
11145 cp_lexer_consume_token (parser->lexer);
11147 /* Parse the template-argument. */
11148 argument = cp_parser_template_argument (parser);
11150 /* If the next token is an ellipsis, we're expanding a template
11152 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11154 if (argument == error_mark_node)
11156 cp_token *token = cp_lexer_peek_token (parser->lexer);
11157 error_at (token->location,
11158 "expected parameter pack before %<...%>");
11160 /* Consume the `...' token. */
11161 cp_lexer_consume_token (parser->lexer);
11163 /* Make the argument into a TYPE_PACK_EXPANSION or
11164 EXPR_PACK_EXPANSION. */
11165 argument = make_pack_expansion (argument);
11168 if (n_args == alloced)
11172 if (arg_ary == fixed_args)
11174 arg_ary = XNEWVEC (tree, alloced);
11175 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11178 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11180 arg_ary[n_args++] = argument;
11182 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11184 vec = make_tree_vec (n_args);
11187 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11189 if (arg_ary != fixed_args)
11191 parser->non_integral_constant_expression_p = saved_non_ice_p;
11192 parser->integral_constant_expression_p = saved_ice_p;
11193 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11197 /* Parse a template-argument.
11200 assignment-expression
11204 The representation is that of an assignment-expression, type-id, or
11205 id-expression -- except that the qualified id-expression is
11206 evaluated, so that the value returned is either a DECL or an
11209 Although the standard says "assignment-expression", it forbids
11210 throw-expressions or assignments in the template argument.
11211 Therefore, we use "conditional-expression" instead. */
11214 cp_parser_template_argument (cp_parser* parser)
11219 bool maybe_type_id = false;
11220 cp_token *token = NULL, *argument_start_token = NULL;
11223 /* There's really no way to know what we're looking at, so we just
11224 try each alternative in order.
11228 In a template-argument, an ambiguity between a type-id and an
11229 expression is resolved to a type-id, regardless of the form of
11230 the corresponding template-parameter.
11232 Therefore, we try a type-id first. */
11233 cp_parser_parse_tentatively (parser);
11234 argument = cp_parser_template_type_arg (parser);
11235 /* If there was no error parsing the type-id but the next token is a
11236 '>>', our behavior depends on which dialect of C++ we're
11237 parsing. In C++98, we probably found a typo for '> >'. But there
11238 are type-id which are also valid expressions. For instance:
11240 struct X { int operator >> (int); };
11241 template <int V> struct Foo {};
11244 Here 'X()' is a valid type-id of a function type, but the user just
11245 wanted to write the expression "X() >> 5". Thus, we remember that we
11246 found a valid type-id, but we still try to parse the argument as an
11247 expression to see what happens.
11249 In C++0x, the '>>' will be considered two separate '>'
11251 if (!cp_parser_error_occurred (parser)
11252 && cxx_dialect == cxx98
11253 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11255 maybe_type_id = true;
11256 cp_parser_abort_tentative_parse (parser);
11260 /* If the next token isn't a `,' or a `>', then this argument wasn't
11261 really finished. This means that the argument is not a valid
11263 if (!cp_parser_next_token_ends_template_argument_p (parser))
11264 cp_parser_error (parser, "expected template-argument");
11265 /* If that worked, we're done. */
11266 if (cp_parser_parse_definitely (parser))
11269 /* We're still not sure what the argument will be. */
11270 cp_parser_parse_tentatively (parser);
11271 /* Try a template. */
11272 argument_start_token = cp_lexer_peek_token (parser->lexer);
11273 argument = cp_parser_id_expression (parser,
11274 /*template_keyword_p=*/false,
11275 /*check_dependency_p=*/true,
11277 /*declarator_p=*/false,
11278 /*optional_p=*/false);
11279 /* If the next token isn't a `,' or a `>', then this argument wasn't
11280 really finished. */
11281 if (!cp_parser_next_token_ends_template_argument_p (parser))
11282 cp_parser_error (parser, "expected template-argument");
11283 if (!cp_parser_error_occurred (parser))
11285 /* Figure out what is being referred to. If the id-expression
11286 was for a class template specialization, then we will have a
11287 TYPE_DECL at this point. There is no need to do name lookup
11288 at this point in that case. */
11289 if (TREE_CODE (argument) != TYPE_DECL)
11290 argument = cp_parser_lookup_name (parser, argument,
11292 /*is_template=*/template_p,
11293 /*is_namespace=*/false,
11294 /*check_dependency=*/true,
11295 /*ambiguous_decls=*/NULL,
11296 argument_start_token->location);
11297 if (TREE_CODE (argument) != TEMPLATE_DECL
11298 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11299 cp_parser_error (parser, "expected template-name");
11301 if (cp_parser_parse_definitely (parser))
11303 /* It must be a non-type argument. There permitted cases are given
11304 in [temp.arg.nontype]:
11306 -- an integral constant-expression of integral or enumeration
11309 -- the name of a non-type template-parameter; or
11311 -- the name of an object or function with external linkage...
11313 -- the address of an object or function with external linkage...
11315 -- a pointer to member... */
11316 /* Look for a non-type template parameter. */
11317 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11319 cp_parser_parse_tentatively (parser);
11320 argument = cp_parser_primary_expression (parser,
11321 /*address_p=*/false,
11323 /*template_arg_p=*/true,
11325 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11326 || !cp_parser_next_token_ends_template_argument_p (parser))
11327 cp_parser_simulate_error (parser);
11328 if (cp_parser_parse_definitely (parser))
11332 /* If the next token is "&", the argument must be the address of an
11333 object or function with external linkage. */
11334 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11336 cp_lexer_consume_token (parser->lexer);
11337 /* See if we might have an id-expression. */
11338 token = cp_lexer_peek_token (parser->lexer);
11339 if (token->type == CPP_NAME
11340 || token->keyword == RID_OPERATOR
11341 || token->type == CPP_SCOPE
11342 || token->type == CPP_TEMPLATE_ID
11343 || token->type == CPP_NESTED_NAME_SPECIFIER)
11345 cp_parser_parse_tentatively (parser);
11346 argument = cp_parser_primary_expression (parser,
11349 /*template_arg_p=*/true,
11351 if (cp_parser_error_occurred (parser)
11352 || !cp_parser_next_token_ends_template_argument_p (parser))
11353 cp_parser_abort_tentative_parse (parser);
11356 if (TREE_CODE (argument) == INDIRECT_REF)
11358 gcc_assert (REFERENCE_REF_P (argument));
11359 argument = TREE_OPERAND (argument, 0);
11362 if (TREE_CODE (argument) == VAR_DECL)
11364 /* A variable without external linkage might still be a
11365 valid constant-expression, so no error is issued here
11366 if the external-linkage check fails. */
11367 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11368 cp_parser_simulate_error (parser);
11370 else if (is_overloaded_fn (argument))
11371 /* All overloaded functions are allowed; if the external
11372 linkage test does not pass, an error will be issued
11376 && (TREE_CODE (argument) == OFFSET_REF
11377 || TREE_CODE (argument) == SCOPE_REF))
11378 /* A pointer-to-member. */
11380 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11383 cp_parser_simulate_error (parser);
11385 if (cp_parser_parse_definitely (parser))
11388 argument = build_x_unary_op (ADDR_EXPR, argument,
11389 tf_warning_or_error);
11394 /* If the argument started with "&", there are no other valid
11395 alternatives at this point. */
11398 cp_parser_error (parser, "invalid non-type template argument");
11399 return error_mark_node;
11402 /* If the argument wasn't successfully parsed as a type-id followed
11403 by '>>', the argument can only be a constant expression now.
11404 Otherwise, we try parsing the constant-expression tentatively,
11405 because the argument could really be a type-id. */
11407 cp_parser_parse_tentatively (parser);
11408 argument = cp_parser_constant_expression (parser,
11409 /*allow_non_constant_p=*/false,
11410 /*non_constant_p=*/NULL);
11411 argument = fold_non_dependent_expr (argument);
11412 if (!maybe_type_id)
11414 if (!cp_parser_next_token_ends_template_argument_p (parser))
11415 cp_parser_error (parser, "expected template-argument");
11416 if (cp_parser_parse_definitely (parser))
11418 /* We did our best to parse the argument as a non type-id, but that
11419 was the only alternative that matched (albeit with a '>' after
11420 it). We can assume it's just a typo from the user, and a
11421 diagnostic will then be issued. */
11422 return cp_parser_template_type_arg (parser);
11425 /* Parse an explicit-instantiation.
11427 explicit-instantiation:
11428 template declaration
11430 Although the standard says `declaration', what it really means is:
11432 explicit-instantiation:
11433 template decl-specifier-seq [opt] declarator [opt] ;
11435 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11436 supposed to be allowed. A defect report has been filed about this
11441 explicit-instantiation:
11442 storage-class-specifier template
11443 decl-specifier-seq [opt] declarator [opt] ;
11444 function-specifier template
11445 decl-specifier-seq [opt] declarator [opt] ; */
11448 cp_parser_explicit_instantiation (cp_parser* parser)
11450 int declares_class_or_enum;
11451 cp_decl_specifier_seq decl_specifiers;
11452 tree extension_specifier = NULL_TREE;
11455 /* Look for an (optional) storage-class-specifier or
11456 function-specifier. */
11457 if (cp_parser_allow_gnu_extensions_p (parser))
11459 extension_specifier
11460 = cp_parser_storage_class_specifier_opt (parser);
11461 if (!extension_specifier)
11462 extension_specifier
11463 = cp_parser_function_specifier_opt (parser,
11464 /*decl_specs=*/NULL);
11467 /* Look for the `template' keyword. */
11468 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11469 /* Let the front end know that we are processing an explicit
11471 begin_explicit_instantiation ();
11472 /* [temp.explicit] says that we are supposed to ignore access
11473 control while processing explicit instantiation directives. */
11474 push_deferring_access_checks (dk_no_check);
11475 /* Parse a decl-specifier-seq. */
11476 token = cp_lexer_peek_token (parser->lexer);
11477 cp_parser_decl_specifier_seq (parser,
11478 CP_PARSER_FLAGS_OPTIONAL,
11480 &declares_class_or_enum);
11481 /* If there was exactly one decl-specifier, and it declared a class,
11482 and there's no declarator, then we have an explicit type
11484 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11488 type = check_tag_decl (&decl_specifiers);
11489 /* Turn access control back on for names used during
11490 template instantiation. */
11491 pop_deferring_access_checks ();
11493 do_type_instantiation (type, extension_specifier,
11494 /*complain=*/tf_error);
11498 cp_declarator *declarator;
11501 /* Parse the declarator. */
11503 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11504 /*ctor_dtor_or_conv_p=*/NULL,
11505 /*parenthesized_p=*/NULL,
11506 /*member_p=*/false);
11507 if (declares_class_or_enum & 2)
11508 cp_parser_check_for_definition_in_return_type (declarator,
11509 decl_specifiers.type,
11510 decl_specifiers.type_location);
11511 if (declarator != cp_error_declarator)
11513 decl = grokdeclarator (declarator, &decl_specifiers,
11514 NORMAL, 0, &decl_specifiers.attributes);
11515 /* Turn access control back on for names used during
11516 template instantiation. */
11517 pop_deferring_access_checks ();
11518 /* Do the explicit instantiation. */
11519 do_decl_instantiation (decl, extension_specifier);
11523 pop_deferring_access_checks ();
11524 /* Skip the body of the explicit instantiation. */
11525 cp_parser_skip_to_end_of_statement (parser);
11528 /* We're done with the instantiation. */
11529 end_explicit_instantiation ();
11531 cp_parser_consume_semicolon_at_end_of_statement (parser);
11534 /* Parse an explicit-specialization.
11536 explicit-specialization:
11537 template < > declaration
11539 Although the standard says `declaration', what it really means is:
11541 explicit-specialization:
11542 template <> decl-specifier [opt] init-declarator [opt] ;
11543 template <> function-definition
11544 template <> explicit-specialization
11545 template <> template-declaration */
11548 cp_parser_explicit_specialization (cp_parser* parser)
11550 bool need_lang_pop;
11551 cp_token *token = cp_lexer_peek_token (parser->lexer);
11553 /* Look for the `template' keyword. */
11554 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11555 /* Look for the `<'. */
11556 cp_parser_require (parser, CPP_LESS, "%<<%>");
11557 /* Look for the `>'. */
11558 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11559 /* We have processed another parameter list. */
11560 ++parser->num_template_parameter_lists;
11563 A template ... explicit specialization ... shall not have C
11565 if (current_lang_name == lang_name_c)
11567 error_at (token->location, "template specialization with C linkage");
11568 /* Give it C++ linkage to avoid confusing other parts of the
11570 push_lang_context (lang_name_cplusplus);
11571 need_lang_pop = true;
11574 need_lang_pop = false;
11575 /* Let the front end know that we are beginning a specialization. */
11576 if (!begin_specialization ())
11578 end_specialization ();
11582 /* If the next keyword is `template', we need to figure out whether
11583 or not we're looking a template-declaration. */
11584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11586 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11587 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11588 cp_parser_template_declaration_after_export (parser,
11589 /*member_p=*/false);
11591 cp_parser_explicit_specialization (parser);
11594 /* Parse the dependent declaration. */
11595 cp_parser_single_declaration (parser,
11597 /*member_p=*/false,
11598 /*explicit_specialization_p=*/true,
11599 /*friend_p=*/NULL);
11600 /* We're done with the specialization. */
11601 end_specialization ();
11602 /* For the erroneous case of a template with C linkage, we pushed an
11603 implicit C++ linkage scope; exit that scope now. */
11605 pop_lang_context ();
11606 /* We're done with this parameter list. */
11607 --parser->num_template_parameter_lists;
11610 /* Parse a type-specifier.
11613 simple-type-specifier
11616 elaborated-type-specifier
11624 Returns a representation of the type-specifier. For a
11625 class-specifier, enum-specifier, or elaborated-type-specifier, a
11626 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11628 The parser flags FLAGS is used to control type-specifier parsing.
11630 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11631 in a decl-specifier-seq.
11633 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11634 class-specifier, enum-specifier, or elaborated-type-specifier, then
11635 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11636 if a type is declared; 2 if it is defined. Otherwise, it is set to
11639 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11640 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11641 is set to FALSE. */
11644 cp_parser_type_specifier (cp_parser* parser,
11645 cp_parser_flags flags,
11646 cp_decl_specifier_seq *decl_specs,
11647 bool is_declaration,
11648 int* declares_class_or_enum,
11649 bool* is_cv_qualifier)
11651 tree type_spec = NULL_TREE;
11654 cp_decl_spec ds = ds_last;
11656 /* Assume this type-specifier does not declare a new type. */
11657 if (declares_class_or_enum)
11658 *declares_class_or_enum = 0;
11659 /* And that it does not specify a cv-qualifier. */
11660 if (is_cv_qualifier)
11661 *is_cv_qualifier = false;
11662 /* Peek at the next token. */
11663 token = cp_lexer_peek_token (parser->lexer);
11665 /* If we're looking at a keyword, we can use that to guide the
11666 production we choose. */
11667 keyword = token->keyword;
11671 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11672 goto elaborated_type_specifier;
11674 /* Look for the enum-specifier. */
11675 type_spec = cp_parser_enum_specifier (parser);
11676 /* If that worked, we're done. */
11679 if (declares_class_or_enum)
11680 *declares_class_or_enum = 2;
11682 cp_parser_set_decl_spec_type (decl_specs,
11685 /*user_defined_p=*/true);
11689 goto elaborated_type_specifier;
11691 /* Any of these indicate either a class-specifier, or an
11692 elaborated-type-specifier. */
11696 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11697 goto elaborated_type_specifier;
11699 /* Parse tentatively so that we can back up if we don't find a
11700 class-specifier. */
11701 cp_parser_parse_tentatively (parser);
11702 /* Look for the class-specifier. */
11703 type_spec = cp_parser_class_specifier (parser);
11704 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11705 /* If that worked, we're done. */
11706 if (cp_parser_parse_definitely (parser))
11708 if (declares_class_or_enum)
11709 *declares_class_or_enum = 2;
11711 cp_parser_set_decl_spec_type (decl_specs,
11714 /*user_defined_p=*/true);
11718 /* Fall through. */
11719 elaborated_type_specifier:
11720 /* We're declaring (not defining) a class or enum. */
11721 if (declares_class_or_enum)
11722 *declares_class_or_enum = 1;
11724 /* Fall through. */
11726 /* Look for an elaborated-type-specifier. */
11728 = (cp_parser_elaborated_type_specifier
11730 decl_specs && decl_specs->specs[(int) ds_friend],
11733 cp_parser_set_decl_spec_type (decl_specs,
11736 /*user_defined_p=*/true);
11741 if (is_cv_qualifier)
11742 *is_cv_qualifier = true;
11747 if (is_cv_qualifier)
11748 *is_cv_qualifier = true;
11753 if (is_cv_qualifier)
11754 *is_cv_qualifier = true;
11758 /* The `__complex__' keyword is a GNU extension. */
11766 /* Handle simple keywords. */
11771 ++decl_specs->specs[(int)ds];
11772 decl_specs->any_specifiers_p = true;
11774 return cp_lexer_consume_token (parser->lexer)->u.value;
11777 /* If we do not already have a type-specifier, assume we are looking
11778 at a simple-type-specifier. */
11779 type_spec = cp_parser_simple_type_specifier (parser,
11783 /* If we didn't find a type-specifier, and a type-specifier was not
11784 optional in this context, issue an error message. */
11785 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11787 cp_parser_error (parser, "expected type specifier");
11788 return error_mark_node;
11794 /* Parse a simple-type-specifier.
11796 simple-type-specifier:
11797 :: [opt] nested-name-specifier [opt] type-name
11798 :: [opt] nested-name-specifier template template-id
11813 simple-type-specifier:
11815 decltype ( expression )
11821 simple-type-specifier:
11822 __typeof__ unary-expression
11823 __typeof__ ( type-id )
11825 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11826 appropriately updated. */
11829 cp_parser_simple_type_specifier (cp_parser* parser,
11830 cp_decl_specifier_seq *decl_specs,
11831 cp_parser_flags flags)
11833 tree type = NULL_TREE;
11836 /* Peek at the next token. */
11837 token = cp_lexer_peek_token (parser->lexer);
11839 /* If we're looking at a keyword, things are easy. */
11840 switch (token->keyword)
11844 decl_specs->explicit_char_p = true;
11845 type = char_type_node;
11848 type = char16_type_node;
11851 type = char32_type_node;
11854 type = wchar_type_node;
11857 type = boolean_type_node;
11861 ++decl_specs->specs[(int) ds_short];
11862 type = short_integer_type_node;
11866 decl_specs->explicit_int_p = true;
11867 type = integer_type_node;
11871 ++decl_specs->specs[(int) ds_long];
11872 type = long_integer_type_node;
11876 ++decl_specs->specs[(int) ds_signed];
11877 type = integer_type_node;
11881 ++decl_specs->specs[(int) ds_unsigned];
11882 type = unsigned_type_node;
11885 type = float_type_node;
11888 type = double_type_node;
11891 type = void_type_node;
11895 maybe_warn_cpp0x ("C++0x auto");
11896 type = make_auto ();
11900 /* Parse the `decltype' type. */
11901 type = cp_parser_decltype (parser);
11904 cp_parser_set_decl_spec_type (decl_specs, type,
11906 /*user_defined_p=*/true);
11911 /* Consume the `typeof' token. */
11912 cp_lexer_consume_token (parser->lexer);
11913 /* Parse the operand to `typeof'. */
11914 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11915 /* If it is not already a TYPE, take its type. */
11916 if (!TYPE_P (type))
11917 type = finish_typeof (type);
11920 cp_parser_set_decl_spec_type (decl_specs, type,
11922 /*user_defined_p=*/true);
11930 /* If the type-specifier was for a built-in type, we're done. */
11935 /* Record the type. */
11937 && (token->keyword != RID_SIGNED
11938 && token->keyword != RID_UNSIGNED
11939 && token->keyword != RID_SHORT
11940 && token->keyword != RID_LONG))
11941 cp_parser_set_decl_spec_type (decl_specs,
11944 /*user_defined=*/false);
11946 decl_specs->any_specifiers_p = true;
11948 /* Consume the token. */
11949 id = cp_lexer_consume_token (parser->lexer)->u.value;
11951 /* There is no valid C++ program where a non-template type is
11952 followed by a "<". That usually indicates that the user thought
11953 that the type was a template. */
11954 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11956 return TYPE_NAME (type);
11959 /* The type-specifier must be a user-defined type. */
11960 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11965 /* Don't gobble tokens or issue error messages if this is an
11966 optional type-specifier. */
11967 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11968 cp_parser_parse_tentatively (parser);
11970 /* Look for the optional `::' operator. */
11972 = (cp_parser_global_scope_opt (parser,
11973 /*current_scope_valid_p=*/false)
11975 /* Look for the nested-name specifier. */
11977 = (cp_parser_nested_name_specifier_opt (parser,
11978 /*typename_keyword_p=*/false,
11979 /*check_dependency_p=*/true,
11981 /*is_declaration=*/false)
11983 token = cp_lexer_peek_token (parser->lexer);
11984 /* If we have seen a nested-name-specifier, and the next token
11985 is `template', then we are using the template-id production. */
11987 && cp_parser_optional_template_keyword (parser))
11989 /* Look for the template-id. */
11990 type = cp_parser_template_id (parser,
11991 /*template_keyword_p=*/true,
11992 /*check_dependency_p=*/true,
11993 /*is_declaration=*/false);
11994 /* If the template-id did not name a type, we are out of
11996 if (TREE_CODE (type) != TYPE_DECL)
11998 cp_parser_error (parser, "expected template-id for type");
12002 /* Otherwise, look for a type-name. */
12004 type = cp_parser_type_name (parser);
12005 /* Keep track of all name-lookups performed in class scopes. */
12009 && TREE_CODE (type) == TYPE_DECL
12010 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12011 maybe_note_name_used_in_class (DECL_NAME (type), type);
12012 /* If it didn't work out, we don't have a TYPE. */
12013 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12014 && !cp_parser_parse_definitely (parser))
12016 if (type && decl_specs)
12017 cp_parser_set_decl_spec_type (decl_specs, type,
12019 /*user_defined=*/true);
12022 /* If we didn't get a type-name, issue an error message. */
12023 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12025 cp_parser_error (parser, "expected type-name");
12026 return error_mark_node;
12029 /* There is no valid C++ program where a non-template type is
12030 followed by a "<". That usually indicates that the user thought
12031 that the type was a template. */
12032 if (type && type != error_mark_node)
12034 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12035 If it is, then the '<'...'>' enclose protocol names rather than
12036 template arguments, and so everything is fine. */
12037 if (c_dialect_objc ()
12038 && (objc_is_id (type) || objc_is_class_name (type)))
12040 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12041 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12043 /* Clobber the "unqualified" type previously entered into
12044 DECL_SPECS with the new, improved protocol-qualified version. */
12046 decl_specs->type = qual_type;
12051 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12058 /* Parse a type-name.
12071 Returns a TYPE_DECL for the type. */
12074 cp_parser_type_name (cp_parser* parser)
12078 /* We can't know yet whether it is a class-name or not. */
12079 cp_parser_parse_tentatively (parser);
12080 /* Try a class-name. */
12081 type_decl = cp_parser_class_name (parser,
12082 /*typename_keyword_p=*/false,
12083 /*template_keyword_p=*/false,
12085 /*check_dependency_p=*/true,
12086 /*class_head_p=*/false,
12087 /*is_declaration=*/false);
12088 /* If it's not a class-name, keep looking. */
12089 if (!cp_parser_parse_definitely (parser))
12091 /* It must be a typedef-name or an enum-name. */
12092 return cp_parser_nonclass_name (parser);
12098 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12106 Returns a TYPE_DECL for the type. */
12109 cp_parser_nonclass_name (cp_parser* parser)
12114 cp_token *token = cp_lexer_peek_token (parser->lexer);
12115 identifier = cp_parser_identifier (parser);
12116 if (identifier == error_mark_node)
12117 return error_mark_node;
12119 /* Look up the type-name. */
12120 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12122 if (TREE_CODE (type_decl) != TYPE_DECL
12123 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12125 /* See if this is an Objective-C type. */
12126 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12127 tree type = objc_get_protocol_qualified_type (identifier, protos);
12129 type_decl = TYPE_NAME (type);
12132 /* Issue an error if we did not find a type-name. */
12133 if (TREE_CODE (type_decl) != TYPE_DECL)
12135 if (!cp_parser_simulate_error (parser))
12136 cp_parser_name_lookup_error (parser, identifier, type_decl,
12137 "is not a type", token->location);
12138 return error_mark_node;
12140 /* Remember that the name was used in the definition of the
12141 current class so that we can check later to see if the
12142 meaning would have been different after the class was
12143 entirely defined. */
12144 else if (type_decl != error_mark_node
12146 maybe_note_name_used_in_class (identifier, type_decl);
12151 /* Parse an elaborated-type-specifier. Note that the grammar given
12152 here incorporates the resolution to DR68.
12154 elaborated-type-specifier:
12155 class-key :: [opt] nested-name-specifier [opt] identifier
12156 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12157 enum-key :: [opt] nested-name-specifier [opt] identifier
12158 typename :: [opt] nested-name-specifier identifier
12159 typename :: [opt] nested-name-specifier template [opt]
12164 elaborated-type-specifier:
12165 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12166 class-key attributes :: [opt] nested-name-specifier [opt]
12167 template [opt] template-id
12168 enum attributes :: [opt] nested-name-specifier [opt] identifier
12170 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12171 declared `friend'. If IS_DECLARATION is TRUE, then this
12172 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12173 something is being declared.
12175 Returns the TYPE specified. */
12178 cp_parser_elaborated_type_specifier (cp_parser* parser,
12180 bool is_declaration)
12182 enum tag_types tag_type;
12184 tree type = NULL_TREE;
12185 tree attributes = NULL_TREE;
12187 cp_token *token = NULL;
12189 /* See if we're looking at the `enum' keyword. */
12190 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12192 /* Consume the `enum' token. */
12193 cp_lexer_consume_token (parser->lexer);
12194 /* Remember that it's an enumeration type. */
12195 tag_type = enum_type;
12196 /* Parse the optional `struct' or `class' key (for C++0x scoped
12198 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12199 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12201 if (cxx_dialect == cxx98)
12202 maybe_warn_cpp0x ("scoped enums");
12204 /* Consume the `struct' or `class'. */
12205 cp_lexer_consume_token (parser->lexer);
12207 /* Parse the attributes. */
12208 attributes = cp_parser_attributes_opt (parser);
12210 /* Or, it might be `typename'. */
12211 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12214 /* Consume the `typename' token. */
12215 cp_lexer_consume_token (parser->lexer);
12216 /* Remember that it's a `typename' type. */
12217 tag_type = typename_type;
12219 /* Otherwise it must be a class-key. */
12222 tag_type = cp_parser_class_key (parser);
12223 if (tag_type == none_type)
12224 return error_mark_node;
12225 /* Parse the attributes. */
12226 attributes = cp_parser_attributes_opt (parser);
12229 /* Look for the `::' operator. */
12230 globalscope = cp_parser_global_scope_opt (parser,
12231 /*current_scope_valid_p=*/false);
12232 /* Look for the nested-name-specifier. */
12233 if (tag_type == typename_type && !globalscope)
12235 if (!cp_parser_nested_name_specifier (parser,
12236 /*typename_keyword_p=*/true,
12237 /*check_dependency_p=*/true,
12240 return error_mark_node;
12243 /* Even though `typename' is not present, the proposed resolution
12244 to Core Issue 180 says that in `class A<T>::B', `B' should be
12245 considered a type-name, even if `A<T>' is dependent. */
12246 cp_parser_nested_name_specifier_opt (parser,
12247 /*typename_keyword_p=*/true,
12248 /*check_dependency_p=*/true,
12251 /* For everything but enumeration types, consider a template-id.
12252 For an enumeration type, consider only a plain identifier. */
12253 if (tag_type != enum_type)
12255 bool template_p = false;
12258 /* Allow the `template' keyword. */
12259 template_p = cp_parser_optional_template_keyword (parser);
12260 /* If we didn't see `template', we don't know if there's a
12261 template-id or not. */
12263 cp_parser_parse_tentatively (parser);
12264 /* Parse the template-id. */
12265 token = cp_lexer_peek_token (parser->lexer);
12266 decl = cp_parser_template_id (parser, template_p,
12267 /*check_dependency_p=*/true,
12269 /* If we didn't find a template-id, look for an ordinary
12271 if (!template_p && !cp_parser_parse_definitely (parser))
12273 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12274 in effect, then we must assume that, upon instantiation, the
12275 template will correspond to a class. */
12276 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12277 && tag_type == typename_type)
12278 type = make_typename_type (parser->scope, decl,
12280 /*complain=*/tf_error);
12281 /* If the `typename' keyword is in effect and DECL is not a type
12282 decl. Then type is non existant. */
12283 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12286 type = TREE_TYPE (decl);
12291 token = cp_lexer_peek_token (parser->lexer);
12292 identifier = cp_parser_identifier (parser);
12294 if (identifier == error_mark_node)
12296 parser->scope = NULL_TREE;
12297 return error_mark_node;
12300 /* For a `typename', we needn't call xref_tag. */
12301 if (tag_type == typename_type
12302 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12303 return cp_parser_make_typename_type (parser, parser->scope,
12306 /* Look up a qualified name in the usual way. */
12310 tree ambiguous_decls;
12312 decl = cp_parser_lookup_name (parser, identifier,
12314 /*is_template=*/false,
12315 /*is_namespace=*/false,
12316 /*check_dependency=*/true,
12320 /* If the lookup was ambiguous, an error will already have been
12322 if (ambiguous_decls)
12323 return error_mark_node;
12325 /* If we are parsing friend declaration, DECL may be a
12326 TEMPLATE_DECL tree node here. However, we need to check
12327 whether this TEMPLATE_DECL results in valid code. Consider
12328 the following example:
12331 template <class T> class C {};
12334 template <class T> friend class N::C; // #1, valid code
12336 template <class T> class Y {
12337 friend class N::C; // #2, invalid code
12340 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12341 name lookup of `N::C'. We see that friend declaration must
12342 be template for the code to be valid. Note that
12343 processing_template_decl does not work here since it is
12344 always 1 for the above two cases. */
12346 decl = (cp_parser_maybe_treat_template_as_class
12347 (decl, /*tag_name_p=*/is_friend
12348 && parser->num_template_parameter_lists));
12350 if (TREE_CODE (decl) != TYPE_DECL)
12352 cp_parser_diagnose_invalid_type_name (parser,
12356 return error_mark_node;
12359 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12361 bool allow_template = (parser->num_template_parameter_lists
12362 || DECL_SELF_REFERENCE_P (decl));
12363 type = check_elaborated_type_specifier (tag_type, decl,
12366 if (type == error_mark_node)
12367 return error_mark_node;
12370 /* Forward declarations of nested types, such as
12375 are invalid unless all components preceding the final '::'
12376 are complete. If all enclosing types are complete, these
12377 declarations become merely pointless.
12379 Invalid forward declarations of nested types are errors
12380 caught elsewhere in parsing. Those that are pointless arrive
12383 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12384 && !is_friend && !processing_explicit_instantiation)
12385 warning (0, "declaration %qD does not declare anything", decl);
12387 type = TREE_TYPE (decl);
12391 /* An elaborated-type-specifier sometimes introduces a new type and
12392 sometimes names an existing type. Normally, the rule is that it
12393 introduces a new type only if there is not an existing type of
12394 the same name already in scope. For example, given:
12397 void f() { struct S s; }
12399 the `struct S' in the body of `f' is the same `struct S' as in
12400 the global scope; the existing definition is used. However, if
12401 there were no global declaration, this would introduce a new
12402 local class named `S'.
12404 An exception to this rule applies to the following code:
12406 namespace N { struct S; }
12408 Here, the elaborated-type-specifier names a new type
12409 unconditionally; even if there is already an `S' in the
12410 containing scope this declaration names a new type.
12411 This exception only applies if the elaborated-type-specifier
12412 forms the complete declaration:
12416 A declaration consisting solely of `class-key identifier ;' is
12417 either a redeclaration of the name in the current scope or a
12418 forward declaration of the identifier as a class name. It
12419 introduces the name into the current scope.
12421 We are in this situation precisely when the next token is a `;'.
12423 An exception to the exception is that a `friend' declaration does
12424 *not* name a new type; i.e., given:
12426 struct S { friend struct T; };
12428 `T' is not a new type in the scope of `S'.
12430 Also, `new struct S' or `sizeof (struct S)' never results in the
12431 definition of a new type; a new type can only be declared in a
12432 declaration context. */
12438 /* Friends have special name lookup rules. */
12439 ts = ts_within_enclosing_non_class;
12440 else if (is_declaration
12441 && cp_lexer_next_token_is (parser->lexer,
12443 /* This is a `class-key identifier ;' */
12449 (parser->num_template_parameter_lists
12450 && (cp_parser_next_token_starts_class_definition_p (parser)
12451 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12452 /* An unqualified name was used to reference this type, so
12453 there were no qualifying templates. */
12454 if (!cp_parser_check_template_parameters (parser,
12455 /*num_templates=*/0,
12457 /*declarator=*/NULL))
12458 return error_mark_node;
12459 type = xref_tag (tag_type, identifier, ts, template_p);
12463 if (type == error_mark_node)
12464 return error_mark_node;
12466 /* Allow attributes on forward declarations of classes. */
12469 if (TREE_CODE (type) == TYPENAME_TYPE)
12470 warning (OPT_Wattributes,
12471 "attributes ignored on uninstantiated type");
12472 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12473 && ! processing_explicit_instantiation)
12474 warning (OPT_Wattributes,
12475 "attributes ignored on template instantiation");
12476 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12477 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12479 warning (OPT_Wattributes,
12480 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12483 if (tag_type != enum_type)
12484 cp_parser_check_class_key (tag_type, type);
12486 /* A "<" cannot follow an elaborated type specifier. If that
12487 happens, the user was probably trying to form a template-id. */
12488 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12493 /* Parse an enum-specifier.
12496 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12501 enum struct [C++0x]
12504 : type-specifier-seq
12507 enum-key attributes[opt] identifier [opt] enum-base [opt]
12508 { enumerator-list [opt] }attributes[opt]
12510 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12511 if the token stream isn't an enum-specifier after all. */
12514 cp_parser_enum_specifier (cp_parser* parser)
12519 bool scoped_enum_p = false;
12520 bool has_underlying_type = false;
12521 tree underlying_type = NULL_TREE;
12523 /* Parse tentatively so that we can back up if we don't find a
12525 cp_parser_parse_tentatively (parser);
12527 /* Caller guarantees that the current token is 'enum', an identifier
12528 possibly follows, and the token after that is an opening brace.
12529 If we don't have an identifier, fabricate an anonymous name for
12530 the enumeration being defined. */
12531 cp_lexer_consume_token (parser->lexer);
12533 /* Parse the "class" or "struct", which indicates a scoped
12534 enumeration type in C++0x. */
12535 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12536 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12538 if (cxx_dialect == cxx98)
12539 maybe_warn_cpp0x ("scoped enums");
12541 /* Consume the `struct' or `class' token. */
12542 cp_lexer_consume_token (parser->lexer);
12544 scoped_enum_p = true;
12547 attributes = cp_parser_attributes_opt (parser);
12549 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12550 identifier = cp_parser_identifier (parser);
12552 identifier = make_anon_name ();
12554 /* Check for the `:' that denotes a specified underlying type in C++0x.
12555 Note that a ':' could also indicate a bitfield width, however. */
12556 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12558 cp_decl_specifier_seq type_specifiers;
12560 /* Consume the `:'. */
12561 cp_lexer_consume_token (parser->lexer);
12563 /* Parse the type-specifier-seq. */
12564 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12565 /*is_trailing_return=*/false,
12568 /* At this point this is surely not elaborated type specifier. */
12569 if (!cp_parser_parse_definitely (parser))
12572 if (cxx_dialect == cxx98)
12573 maybe_warn_cpp0x ("scoped enums");
12575 has_underlying_type = true;
12577 /* If that didn't work, stop. */
12578 if (type_specifiers.type != error_mark_node)
12580 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12581 /*initialized=*/0, NULL);
12582 if (underlying_type == error_mark_node)
12583 underlying_type = NULL_TREE;
12587 /* Look for the `{' but don't consume it yet. */
12588 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12590 cp_parser_error (parser, "expected %<{%>");
12591 if (has_underlying_type)
12595 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12598 /* Issue an error message if type-definitions are forbidden here. */
12599 if (!cp_parser_check_type_definition (parser))
12600 type = error_mark_node;
12602 /* Create the new type. We do this before consuming the opening
12603 brace so the enum will be recorded as being on the line of its
12604 tag (or the 'enum' keyword, if there is no tag). */
12605 type = start_enum (identifier, underlying_type, scoped_enum_p);
12607 /* Consume the opening brace. */
12608 cp_lexer_consume_token (parser->lexer);
12610 if (type == error_mark_node)
12612 cp_parser_skip_to_end_of_block_or_statement (parser);
12613 return error_mark_node;
12616 /* If the next token is not '}', then there are some enumerators. */
12617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12618 cp_parser_enumerator_list (parser, type);
12620 /* Consume the final '}'. */
12621 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12623 /* Look for trailing attributes to apply to this enumeration, and
12624 apply them if appropriate. */
12625 if (cp_parser_allow_gnu_extensions_p (parser))
12627 tree trailing_attr = cp_parser_attributes_opt (parser);
12628 trailing_attr = chainon (trailing_attr, attributes);
12629 cplus_decl_attributes (&type,
12631 (int) ATTR_FLAG_TYPE_IN_PLACE);
12634 /* Finish up the enumeration. */
12635 finish_enum (type);
12640 /* Parse an enumerator-list. The enumerators all have the indicated
12644 enumerator-definition
12645 enumerator-list , enumerator-definition */
12648 cp_parser_enumerator_list (cp_parser* parser, tree type)
12652 /* Parse an enumerator-definition. */
12653 cp_parser_enumerator_definition (parser, type);
12655 /* If the next token is not a ',', we've reached the end of
12657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12659 /* Otherwise, consume the `,' and keep going. */
12660 cp_lexer_consume_token (parser->lexer);
12661 /* If the next token is a `}', there is a trailing comma. */
12662 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12664 if (!in_system_header)
12665 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12671 /* Parse an enumerator-definition. The enumerator has the indicated
12674 enumerator-definition:
12676 enumerator = constant-expression
12682 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12687 /* Look for the identifier. */
12688 identifier = cp_parser_identifier (parser);
12689 if (identifier == error_mark_node)
12692 /* If the next token is an '=', then there is an explicit value. */
12693 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12695 /* Consume the `=' token. */
12696 cp_lexer_consume_token (parser->lexer);
12697 /* Parse the value. */
12698 value = cp_parser_constant_expression (parser,
12699 /*allow_non_constant_p=*/false,
12705 /* If we are processing a template, make sure the initializer of the
12706 enumerator doesn't contain any bare template parameter pack. */
12707 if (check_for_bare_parameter_packs (value))
12708 value = error_mark_node;
12710 /* Create the enumerator. */
12711 build_enumerator (identifier, value, type);
12714 /* Parse a namespace-name.
12717 original-namespace-name
12720 Returns the NAMESPACE_DECL for the namespace. */
12723 cp_parser_namespace_name (cp_parser* parser)
12726 tree namespace_decl;
12728 cp_token *token = cp_lexer_peek_token (parser->lexer);
12730 /* Get the name of the namespace. */
12731 identifier = cp_parser_identifier (parser);
12732 if (identifier == error_mark_node)
12733 return error_mark_node;
12735 /* Look up the identifier in the currently active scope. Look only
12736 for namespaces, due to:
12738 [basic.lookup.udir]
12740 When looking up a namespace-name in a using-directive or alias
12741 definition, only namespace names are considered.
12745 [basic.lookup.qual]
12747 During the lookup of a name preceding the :: scope resolution
12748 operator, object, function, and enumerator names are ignored.
12750 (Note that cp_parser_qualifying_entity only calls this
12751 function if the token after the name is the scope resolution
12753 namespace_decl = cp_parser_lookup_name (parser, identifier,
12755 /*is_template=*/false,
12756 /*is_namespace=*/true,
12757 /*check_dependency=*/true,
12758 /*ambiguous_decls=*/NULL,
12760 /* If it's not a namespace, issue an error. */
12761 if (namespace_decl == error_mark_node
12762 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12764 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12765 error_at (token->location, "%qD is not a namespace-name", identifier);
12766 cp_parser_error (parser, "expected namespace-name");
12767 namespace_decl = error_mark_node;
12770 return namespace_decl;
12773 /* Parse a namespace-definition.
12775 namespace-definition:
12776 named-namespace-definition
12777 unnamed-namespace-definition
12779 named-namespace-definition:
12780 original-namespace-definition
12781 extension-namespace-definition
12783 original-namespace-definition:
12784 namespace identifier { namespace-body }
12786 extension-namespace-definition:
12787 namespace original-namespace-name { namespace-body }
12789 unnamed-namespace-definition:
12790 namespace { namespace-body } */
12793 cp_parser_namespace_definition (cp_parser* parser)
12795 tree identifier, attribs;
12796 bool has_visibility;
12799 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12802 cp_lexer_consume_token (parser->lexer);
12807 /* Look for the `namespace' keyword. */
12808 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12810 /* Get the name of the namespace. We do not attempt to distinguish
12811 between an original-namespace-definition and an
12812 extension-namespace-definition at this point. The semantic
12813 analysis routines are responsible for that. */
12814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12815 identifier = cp_parser_identifier (parser);
12817 identifier = NULL_TREE;
12819 /* Parse any specified attributes. */
12820 attribs = cp_parser_attributes_opt (parser);
12822 /* Look for the `{' to start the namespace. */
12823 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12824 /* Start the namespace. */
12825 push_namespace (identifier);
12827 /* "inline namespace" is equivalent to a stub namespace definition
12828 followed by a strong using directive. */
12831 tree name_space = current_namespace;
12832 /* Set up namespace association. */
12833 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12834 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12835 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12836 /* Import the contents of the inline namespace. */
12838 do_using_directive (name_space);
12839 push_namespace (identifier);
12842 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12844 /* Parse the body of the namespace. */
12845 cp_parser_namespace_body (parser);
12847 #ifdef HANDLE_PRAGMA_VISIBILITY
12848 if (has_visibility)
12849 pop_visibility (1);
12852 /* Finish the namespace. */
12854 /* Look for the final `}'. */
12855 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12858 /* Parse a namespace-body.
12861 declaration-seq [opt] */
12864 cp_parser_namespace_body (cp_parser* parser)
12866 cp_parser_declaration_seq_opt (parser);
12869 /* Parse a namespace-alias-definition.
12871 namespace-alias-definition:
12872 namespace identifier = qualified-namespace-specifier ; */
12875 cp_parser_namespace_alias_definition (cp_parser* parser)
12878 tree namespace_specifier;
12880 cp_token *token = cp_lexer_peek_token (parser->lexer);
12882 /* Look for the `namespace' keyword. */
12883 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12884 /* Look for the identifier. */
12885 identifier = cp_parser_identifier (parser);
12886 if (identifier == error_mark_node)
12888 /* Look for the `=' token. */
12889 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12890 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12892 error_at (token->location, "%<namespace%> definition is not allowed here");
12893 /* Skip the definition. */
12894 cp_lexer_consume_token (parser->lexer);
12895 if (cp_parser_skip_to_closing_brace (parser))
12896 cp_lexer_consume_token (parser->lexer);
12899 cp_parser_require (parser, CPP_EQ, "%<=%>");
12900 /* Look for the qualified-namespace-specifier. */
12901 namespace_specifier
12902 = cp_parser_qualified_namespace_specifier (parser);
12903 /* Look for the `;' token. */
12904 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12906 /* Register the alias in the symbol table. */
12907 do_namespace_alias (identifier, namespace_specifier);
12910 /* Parse a qualified-namespace-specifier.
12912 qualified-namespace-specifier:
12913 :: [opt] nested-name-specifier [opt] namespace-name
12915 Returns a NAMESPACE_DECL corresponding to the specified
12919 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12921 /* Look for the optional `::'. */
12922 cp_parser_global_scope_opt (parser,
12923 /*current_scope_valid_p=*/false);
12925 /* Look for the optional nested-name-specifier. */
12926 cp_parser_nested_name_specifier_opt (parser,
12927 /*typename_keyword_p=*/false,
12928 /*check_dependency_p=*/true,
12930 /*is_declaration=*/true);
12932 return cp_parser_namespace_name (parser);
12935 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12936 access declaration.
12939 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12940 using :: unqualified-id ;
12942 access-declaration:
12948 cp_parser_using_declaration (cp_parser* parser,
12949 bool access_declaration_p)
12952 bool typename_p = false;
12953 bool global_scope_p;
12958 if (access_declaration_p)
12959 cp_parser_parse_tentatively (parser);
12962 /* Look for the `using' keyword. */
12963 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12965 /* Peek at the next token. */
12966 token = cp_lexer_peek_token (parser->lexer);
12967 /* See if it's `typename'. */
12968 if (token->keyword == RID_TYPENAME)
12970 /* Remember that we've seen it. */
12972 /* Consume the `typename' token. */
12973 cp_lexer_consume_token (parser->lexer);
12977 /* Look for the optional global scope qualification. */
12979 = (cp_parser_global_scope_opt (parser,
12980 /*current_scope_valid_p=*/false)
12983 /* If we saw `typename', or didn't see `::', then there must be a
12984 nested-name-specifier present. */
12985 if (typename_p || !global_scope_p)
12986 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12987 /*check_dependency_p=*/true,
12989 /*is_declaration=*/true);
12990 /* Otherwise, we could be in either of the two productions. In that
12991 case, treat the nested-name-specifier as optional. */
12993 qscope = cp_parser_nested_name_specifier_opt (parser,
12994 /*typename_keyword_p=*/false,
12995 /*check_dependency_p=*/true,
12997 /*is_declaration=*/true);
12999 qscope = global_namespace;
13001 if (access_declaration_p && cp_parser_error_occurred (parser))
13002 /* Something has already gone wrong; there's no need to parse
13003 further. Since an error has occurred, the return value of
13004 cp_parser_parse_definitely will be false, as required. */
13005 return cp_parser_parse_definitely (parser);
13007 token = cp_lexer_peek_token (parser->lexer);
13008 /* Parse the unqualified-id. */
13009 identifier = cp_parser_unqualified_id (parser,
13010 /*template_keyword_p=*/false,
13011 /*check_dependency_p=*/true,
13012 /*declarator_p=*/true,
13013 /*optional_p=*/false);
13015 if (access_declaration_p)
13017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13018 cp_parser_simulate_error (parser);
13019 if (!cp_parser_parse_definitely (parser))
13023 /* The function we call to handle a using-declaration is different
13024 depending on what scope we are in. */
13025 if (qscope == error_mark_node || identifier == error_mark_node)
13027 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13028 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13029 /* [namespace.udecl]
13031 A using declaration shall not name a template-id. */
13032 error_at (token->location,
13033 "a template-id may not appear in a using-declaration");
13036 if (at_class_scope_p ())
13038 /* Create the USING_DECL. */
13039 decl = do_class_using_decl (parser->scope, identifier);
13041 if (check_for_bare_parameter_packs (decl))
13044 /* Add it to the list of members in this class. */
13045 finish_member_declaration (decl);
13049 decl = cp_parser_lookup_name_simple (parser,
13052 if (decl == error_mark_node)
13053 cp_parser_name_lookup_error (parser, identifier,
13056 else if (check_for_bare_parameter_packs (decl))
13058 else if (!at_namespace_scope_p ())
13059 do_local_using_decl (decl, qscope, identifier);
13061 do_toplevel_using_decl (decl, qscope, identifier);
13065 /* Look for the final `;'. */
13066 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13071 /* Parse a using-directive.
13074 using namespace :: [opt] nested-name-specifier [opt]
13075 namespace-name ; */
13078 cp_parser_using_directive (cp_parser* parser)
13080 tree namespace_decl;
13083 /* Look for the `using' keyword. */
13084 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13085 /* And the `namespace' keyword. */
13086 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13087 /* Look for the optional `::' operator. */
13088 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13089 /* And the optional nested-name-specifier. */
13090 cp_parser_nested_name_specifier_opt (parser,
13091 /*typename_keyword_p=*/false,
13092 /*check_dependency_p=*/true,
13094 /*is_declaration=*/true);
13095 /* Get the namespace being used. */
13096 namespace_decl = cp_parser_namespace_name (parser);
13097 /* And any specified attributes. */
13098 attribs = cp_parser_attributes_opt (parser);
13099 /* Update the symbol table. */
13100 parse_using_directive (namespace_decl, attribs);
13101 /* Look for the final `;'. */
13102 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13105 /* Parse an asm-definition.
13108 asm ( string-literal ) ;
13113 asm volatile [opt] ( string-literal ) ;
13114 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13115 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13116 : asm-operand-list [opt] ) ;
13117 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13118 : asm-operand-list [opt]
13119 : asm-clobber-list [opt] ) ;
13120 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13121 : asm-clobber-list [opt]
13122 : asm-goto-list ) ; */
13125 cp_parser_asm_definition (cp_parser* parser)
13128 tree outputs = NULL_TREE;
13129 tree inputs = NULL_TREE;
13130 tree clobbers = NULL_TREE;
13131 tree labels = NULL_TREE;
13133 bool volatile_p = false;
13134 bool extended_p = false;
13135 bool invalid_inputs_p = false;
13136 bool invalid_outputs_p = false;
13137 bool goto_p = false;
13138 const char *missing = NULL;
13140 /* Look for the `asm' keyword. */
13141 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13142 /* See if the next token is `volatile'. */
13143 if (cp_parser_allow_gnu_extensions_p (parser)
13144 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13146 /* Remember that we saw the `volatile' keyword. */
13148 /* Consume the token. */
13149 cp_lexer_consume_token (parser->lexer);
13151 if (cp_parser_allow_gnu_extensions_p (parser)
13152 && parser->in_function_body
13153 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13155 /* Remember that we saw the `goto' keyword. */
13157 /* Consume the token. */
13158 cp_lexer_consume_token (parser->lexer);
13160 /* Look for the opening `('. */
13161 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13163 /* Look for the string. */
13164 string = cp_parser_string_literal (parser, false, false);
13165 if (string == error_mark_node)
13167 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13168 /*consume_paren=*/true);
13172 /* If we're allowing GNU extensions, check for the extended assembly
13173 syntax. Unfortunately, the `:' tokens need not be separated by
13174 a space in C, and so, for compatibility, we tolerate that here
13175 too. Doing that means that we have to treat the `::' operator as
13177 if (cp_parser_allow_gnu_extensions_p (parser)
13178 && parser->in_function_body
13179 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13180 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13182 bool inputs_p = false;
13183 bool clobbers_p = false;
13184 bool labels_p = false;
13186 /* The extended syntax was used. */
13189 /* Look for outputs. */
13190 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13192 /* Consume the `:'. */
13193 cp_lexer_consume_token (parser->lexer);
13194 /* Parse the output-operands. */
13195 if (cp_lexer_next_token_is_not (parser->lexer,
13197 && cp_lexer_next_token_is_not (parser->lexer,
13199 && cp_lexer_next_token_is_not (parser->lexer,
13202 outputs = cp_parser_asm_operand_list (parser);
13204 if (outputs == error_mark_node)
13205 invalid_outputs_p = true;
13207 /* If the next token is `::', there are no outputs, and the
13208 next token is the beginning of the inputs. */
13209 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13210 /* The inputs are coming next. */
13213 /* Look for inputs. */
13215 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13217 /* Consume the `:' or `::'. */
13218 cp_lexer_consume_token (parser->lexer);
13219 /* Parse the output-operands. */
13220 if (cp_lexer_next_token_is_not (parser->lexer,
13222 && cp_lexer_next_token_is_not (parser->lexer,
13224 && cp_lexer_next_token_is_not (parser->lexer,
13226 inputs = cp_parser_asm_operand_list (parser);
13228 if (inputs == error_mark_node)
13229 invalid_inputs_p = true;
13231 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13232 /* The clobbers are coming next. */
13235 /* Look for clobbers. */
13237 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13240 /* Consume the `:' or `::'. */
13241 cp_lexer_consume_token (parser->lexer);
13242 /* Parse the clobbers. */
13243 if (cp_lexer_next_token_is_not (parser->lexer,
13245 && cp_lexer_next_token_is_not (parser->lexer,
13247 clobbers = cp_parser_asm_clobber_list (parser);
13250 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13251 /* The labels are coming next. */
13254 /* Look for labels. */
13256 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13259 /* Consume the `:' or `::'. */
13260 cp_lexer_consume_token (parser->lexer);
13261 /* Parse the labels. */
13262 labels = cp_parser_asm_label_list (parser);
13265 if (goto_p && !labels_p)
13266 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13269 missing = "%<:%> or %<::%>";
13271 /* Look for the closing `)'. */
13272 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13273 missing ? missing : "%<)%>"))
13274 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13275 /*consume_paren=*/true);
13276 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13278 if (!invalid_inputs_p && !invalid_outputs_p)
13280 /* Create the ASM_EXPR. */
13281 if (parser->in_function_body)
13283 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13284 inputs, clobbers, labels);
13285 /* If the extended syntax was not used, mark the ASM_EXPR. */
13288 tree temp = asm_stmt;
13289 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13290 temp = TREE_OPERAND (temp, 0);
13292 ASM_INPUT_P (temp) = 1;
13296 cgraph_add_asm_node (string);
13300 /* Declarators [gram.dcl.decl] */
13302 /* Parse an init-declarator.
13305 declarator initializer [opt]
13310 declarator asm-specification [opt] attributes [opt] initializer [opt]
13312 function-definition:
13313 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13315 decl-specifier-seq [opt] declarator function-try-block
13319 function-definition:
13320 __extension__ function-definition
13322 The DECL_SPECIFIERS apply to this declarator. Returns a
13323 representation of the entity declared. If MEMBER_P is TRUE, then
13324 this declarator appears in a class scope. The new DECL created by
13325 this declarator is returned.
13327 The CHECKS are access checks that should be performed once we know
13328 what entity is being declared (and, therefore, what classes have
13331 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13332 for a function-definition here as well. If the declarator is a
13333 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13334 be TRUE upon return. By that point, the function-definition will
13335 have been completely parsed.
13337 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13341 cp_parser_init_declarator (cp_parser* parser,
13342 cp_decl_specifier_seq *decl_specifiers,
13343 VEC (deferred_access_check,gc)* checks,
13344 bool function_definition_allowed_p,
13346 int declares_class_or_enum,
13347 bool* function_definition_p)
13349 cp_token *token = NULL, *asm_spec_start_token = NULL,
13350 *attributes_start_token = NULL;
13351 cp_declarator *declarator;
13352 tree prefix_attributes;
13354 tree asm_specification;
13356 tree decl = NULL_TREE;
13358 int is_initialized;
13359 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13360 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13362 enum cpp_ttype initialization_kind;
13363 bool is_direct_init = false;
13364 bool is_non_constant_init;
13365 int ctor_dtor_or_conv_p;
13367 tree pushed_scope = NULL;
13369 /* Gather the attributes that were provided with the
13370 decl-specifiers. */
13371 prefix_attributes = decl_specifiers->attributes;
13373 /* Assume that this is not the declarator for a function
13375 if (function_definition_p)
13376 *function_definition_p = false;
13378 /* Defer access checks while parsing the declarator; we cannot know
13379 what names are accessible until we know what is being
13381 resume_deferring_access_checks ();
13383 /* Parse the declarator. */
13384 token = cp_lexer_peek_token (parser->lexer);
13386 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13387 &ctor_dtor_or_conv_p,
13388 /*parenthesized_p=*/NULL,
13389 /*member_p=*/false);
13390 /* Gather up the deferred checks. */
13391 stop_deferring_access_checks ();
13393 /* If the DECLARATOR was erroneous, there's no need to go
13395 if (declarator == cp_error_declarator)
13396 return error_mark_node;
13398 /* Check that the number of template-parameter-lists is OK. */
13399 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13401 return error_mark_node;
13403 if (declares_class_or_enum & 2)
13404 cp_parser_check_for_definition_in_return_type (declarator,
13405 decl_specifiers->type,
13406 decl_specifiers->type_location);
13408 /* Figure out what scope the entity declared by the DECLARATOR is
13409 located in. `grokdeclarator' sometimes changes the scope, so
13410 we compute it now. */
13411 scope = get_scope_of_declarator (declarator);
13413 /* If we're allowing GNU extensions, look for an asm-specification
13415 if (cp_parser_allow_gnu_extensions_p (parser))
13417 /* Look for an asm-specification. */
13418 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13419 asm_specification = cp_parser_asm_specification_opt (parser);
13420 /* And attributes. */
13421 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13422 attributes = cp_parser_attributes_opt (parser);
13426 asm_specification = NULL_TREE;
13427 attributes = NULL_TREE;
13430 /* Peek at the next token. */
13431 token = cp_lexer_peek_token (parser->lexer);
13432 /* Check to see if the token indicates the start of a
13433 function-definition. */
13434 if (function_declarator_p (declarator)
13435 && cp_parser_token_starts_function_definition_p (token))
13437 if (!function_definition_allowed_p)
13439 /* If a function-definition should not appear here, issue an
13441 cp_parser_error (parser,
13442 "a function-definition is not allowed here");
13443 return error_mark_node;
13447 location_t func_brace_location
13448 = cp_lexer_peek_token (parser->lexer)->location;
13450 /* Neither attributes nor an asm-specification are allowed
13451 on a function-definition. */
13452 if (asm_specification)
13453 error_at (asm_spec_start_token->location,
13454 "an asm-specification is not allowed "
13455 "on a function-definition");
13457 error_at (attributes_start_token->location,
13458 "attributes are not allowed on a function-definition");
13459 /* This is a function-definition. */
13460 *function_definition_p = true;
13462 /* Parse the function definition. */
13464 decl = cp_parser_save_member_function_body (parser,
13467 prefix_attributes);
13470 = (cp_parser_function_definition_from_specifiers_and_declarator
13471 (parser, decl_specifiers, prefix_attributes, declarator));
13473 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13475 /* This is where the prologue starts... */
13476 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13477 = func_brace_location;
13486 Only in function declarations for constructors, destructors, and
13487 type conversions can the decl-specifier-seq be omitted.
13489 We explicitly postpone this check past the point where we handle
13490 function-definitions because we tolerate function-definitions
13491 that are missing their return types in some modes. */
13492 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13494 cp_parser_error (parser,
13495 "expected constructor, destructor, or type conversion");
13496 return error_mark_node;
13499 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13500 if (token->type == CPP_EQ
13501 || token->type == CPP_OPEN_PAREN
13502 || token->type == CPP_OPEN_BRACE)
13504 is_initialized = SD_INITIALIZED;
13505 initialization_kind = token->type;
13507 if (token->type == CPP_EQ
13508 && function_declarator_p (declarator))
13510 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13511 if (t2->keyword == RID_DEFAULT)
13512 is_initialized = SD_DEFAULTED;
13513 else if (t2->keyword == RID_DELETE)
13514 is_initialized = SD_DELETED;
13519 /* If the init-declarator isn't initialized and isn't followed by a
13520 `,' or `;', it's not a valid init-declarator. */
13521 if (token->type != CPP_COMMA
13522 && token->type != CPP_SEMICOLON)
13524 cp_parser_error (parser, "expected initializer");
13525 return error_mark_node;
13527 is_initialized = SD_UNINITIALIZED;
13528 initialization_kind = CPP_EOF;
13531 /* Because start_decl has side-effects, we should only call it if we
13532 know we're going ahead. By this point, we know that we cannot
13533 possibly be looking at any other construct. */
13534 cp_parser_commit_to_tentative_parse (parser);
13536 /* If the decl specifiers were bad, issue an error now that we're
13537 sure this was intended to be a declarator. Then continue
13538 declaring the variable(s), as int, to try to cut down on further
13540 if (decl_specifiers->any_specifiers_p
13541 && decl_specifiers->type == error_mark_node)
13543 cp_parser_error (parser, "invalid type in declaration");
13544 decl_specifiers->type = integer_type_node;
13547 /* Check to see whether or not this declaration is a friend. */
13548 friend_p = cp_parser_friend_p (decl_specifiers);
13550 /* Enter the newly declared entry in the symbol table. If we're
13551 processing a declaration in a class-specifier, we wait until
13552 after processing the initializer. */
13555 if (parser->in_unbraced_linkage_specification_p)
13556 decl_specifiers->storage_class = sc_extern;
13557 decl = start_decl (declarator, decl_specifiers,
13558 is_initialized, attributes, prefix_attributes,
13562 /* Enter the SCOPE. That way unqualified names appearing in the
13563 initializer will be looked up in SCOPE. */
13564 pushed_scope = push_scope (scope);
13566 /* Perform deferred access control checks, now that we know in which
13567 SCOPE the declared entity resides. */
13568 if (!member_p && decl)
13570 tree saved_current_function_decl = NULL_TREE;
13572 /* If the entity being declared is a function, pretend that we
13573 are in its scope. If it is a `friend', it may have access to
13574 things that would not otherwise be accessible. */
13575 if (TREE_CODE (decl) == FUNCTION_DECL)
13577 saved_current_function_decl = current_function_decl;
13578 current_function_decl = decl;
13581 /* Perform access checks for template parameters. */
13582 cp_parser_perform_template_parameter_access_checks (checks);
13584 /* Perform the access control checks for the declarator and the
13585 decl-specifiers. */
13586 perform_deferred_access_checks ();
13588 /* Restore the saved value. */
13589 if (TREE_CODE (decl) == FUNCTION_DECL)
13590 current_function_decl = saved_current_function_decl;
13593 /* Parse the initializer. */
13594 initializer = NULL_TREE;
13595 is_direct_init = false;
13596 is_non_constant_init = true;
13597 if (is_initialized)
13599 if (function_declarator_p (declarator))
13601 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13602 if (initialization_kind == CPP_EQ)
13603 initializer = cp_parser_pure_specifier (parser);
13606 /* If the declaration was erroneous, we don't really
13607 know what the user intended, so just silently
13608 consume the initializer. */
13609 if (decl != error_mark_node)
13610 error_at (initializer_start_token->location,
13611 "initializer provided for function");
13612 cp_parser_skip_to_closing_parenthesis (parser,
13613 /*recovering=*/true,
13614 /*or_comma=*/false,
13615 /*consume_paren=*/true);
13620 /* We want to record the extra mangling scope for in-class
13621 initializers of class members and initializers of static data
13622 member templates. The former is a C++0x feature which isn't
13623 implemented yet, and I expect it will involve deferring
13624 parsing of the initializer until end of class as with default
13625 arguments. So right here we only handle the latter. */
13626 if (!member_p && processing_template_decl)
13627 start_lambda_scope (decl);
13628 initializer = cp_parser_initializer (parser,
13630 &is_non_constant_init);
13631 if (!member_p && processing_template_decl)
13632 finish_lambda_scope ();
13636 /* The old parser allows attributes to appear after a parenthesized
13637 initializer. Mark Mitchell proposed removing this functionality
13638 on the GCC mailing lists on 2002-08-13. This parser accepts the
13639 attributes -- but ignores them. */
13640 if (cp_parser_allow_gnu_extensions_p (parser)
13641 && initialization_kind == CPP_OPEN_PAREN)
13642 if (cp_parser_attributes_opt (parser))
13643 warning (OPT_Wattributes,
13644 "attributes after parenthesized initializer ignored");
13646 /* For an in-class declaration, use `grokfield' to create the
13652 pop_scope (pushed_scope);
13653 pushed_scope = false;
13655 decl = grokfield (declarator, decl_specifiers,
13656 initializer, !is_non_constant_init,
13657 /*asmspec=*/NULL_TREE,
13658 prefix_attributes);
13659 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13660 cp_parser_save_default_args (parser, decl);
13663 /* Finish processing the declaration. But, skip friend
13665 if (!friend_p && decl && decl != error_mark_node)
13667 cp_finish_decl (decl,
13668 initializer, !is_non_constant_init,
13670 /* If the initializer is in parentheses, then this is
13671 a direct-initialization, which means that an
13672 `explicit' constructor is OK. Otherwise, an
13673 `explicit' constructor cannot be used. */
13674 ((is_direct_init || !is_initialized)
13675 ? 0 : LOOKUP_ONLYCONVERTING));
13677 else if ((cxx_dialect != cxx98) && friend_p
13678 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13679 /* Core issue #226 (C++0x only): A default template-argument
13680 shall not be specified in a friend class template
13682 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13683 /*is_partial=*/0, /*is_friend_decl=*/1);
13685 if (!friend_p && pushed_scope)
13686 pop_scope (pushed_scope);
13691 /* Parse a declarator.
13695 ptr-operator declarator
13697 abstract-declarator:
13698 ptr-operator abstract-declarator [opt]
13699 direct-abstract-declarator
13704 attributes [opt] direct-declarator
13705 attributes [opt] ptr-operator declarator
13707 abstract-declarator:
13708 attributes [opt] ptr-operator abstract-declarator [opt]
13709 attributes [opt] direct-abstract-declarator
13711 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13712 detect constructor, destructor or conversion operators. It is set
13713 to -1 if the declarator is a name, and +1 if it is a
13714 function. Otherwise it is set to zero. Usually you just want to
13715 test for >0, but internally the negative value is used.
13717 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13718 a decl-specifier-seq unless it declares a constructor, destructor,
13719 or conversion. It might seem that we could check this condition in
13720 semantic analysis, rather than parsing, but that makes it difficult
13721 to handle something like `f()'. We want to notice that there are
13722 no decl-specifiers, and therefore realize that this is an
13723 expression, not a declaration.)
13725 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13726 the declarator is a direct-declarator of the form "(...)".
13728 MEMBER_P is true iff this declarator is a member-declarator. */
13730 static cp_declarator *
13731 cp_parser_declarator (cp_parser* parser,
13732 cp_parser_declarator_kind dcl_kind,
13733 int* ctor_dtor_or_conv_p,
13734 bool* parenthesized_p,
13738 cp_declarator *declarator;
13739 enum tree_code code;
13740 cp_cv_quals cv_quals;
13742 tree attributes = NULL_TREE;
13744 /* Assume this is not a constructor, destructor, or type-conversion
13746 if (ctor_dtor_or_conv_p)
13747 *ctor_dtor_or_conv_p = 0;
13749 if (cp_parser_allow_gnu_extensions_p (parser))
13750 attributes = cp_parser_attributes_opt (parser);
13752 /* Peek at the next token. */
13753 token = cp_lexer_peek_token (parser->lexer);
13755 /* Check for the ptr-operator production. */
13756 cp_parser_parse_tentatively (parser);
13757 /* Parse the ptr-operator. */
13758 code = cp_parser_ptr_operator (parser,
13761 /* If that worked, then we have a ptr-operator. */
13762 if (cp_parser_parse_definitely (parser))
13764 /* If a ptr-operator was found, then this declarator was not
13766 if (parenthesized_p)
13767 *parenthesized_p = true;
13768 /* The dependent declarator is optional if we are parsing an
13769 abstract-declarator. */
13770 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13771 cp_parser_parse_tentatively (parser);
13773 /* Parse the dependent declarator. */
13774 declarator = cp_parser_declarator (parser, dcl_kind,
13775 /*ctor_dtor_or_conv_p=*/NULL,
13776 /*parenthesized_p=*/NULL,
13777 /*member_p=*/false);
13779 /* If we are parsing an abstract-declarator, we must handle the
13780 case where the dependent declarator is absent. */
13781 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13782 && !cp_parser_parse_definitely (parser))
13785 declarator = cp_parser_make_indirect_declarator
13786 (code, class_type, cv_quals, declarator);
13788 /* Everything else is a direct-declarator. */
13791 if (parenthesized_p)
13792 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13794 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13795 ctor_dtor_or_conv_p,
13799 if (attributes && declarator && declarator != cp_error_declarator)
13800 declarator->attributes = attributes;
13805 /* Parse a direct-declarator or direct-abstract-declarator.
13809 direct-declarator ( parameter-declaration-clause )
13810 cv-qualifier-seq [opt]
13811 exception-specification [opt]
13812 direct-declarator [ constant-expression [opt] ]
13815 direct-abstract-declarator:
13816 direct-abstract-declarator [opt]
13817 ( parameter-declaration-clause )
13818 cv-qualifier-seq [opt]
13819 exception-specification [opt]
13820 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13821 ( abstract-declarator )
13823 Returns a representation of the declarator. DCL_KIND is
13824 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13825 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13826 we are parsing a direct-declarator. It is
13827 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13828 of ambiguity we prefer an abstract declarator, as per
13829 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13830 cp_parser_declarator. */
13832 static cp_declarator *
13833 cp_parser_direct_declarator (cp_parser* parser,
13834 cp_parser_declarator_kind dcl_kind,
13835 int* ctor_dtor_or_conv_p,
13839 cp_declarator *declarator = NULL;
13840 tree scope = NULL_TREE;
13841 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13842 bool saved_in_declarator_p = parser->in_declarator_p;
13844 tree pushed_scope = NULL_TREE;
13848 /* Peek at the next token. */
13849 token = cp_lexer_peek_token (parser->lexer);
13850 if (token->type == CPP_OPEN_PAREN)
13852 /* This is either a parameter-declaration-clause, or a
13853 parenthesized declarator. When we know we are parsing a
13854 named declarator, it must be a parenthesized declarator
13855 if FIRST is true. For instance, `(int)' is a
13856 parameter-declaration-clause, with an omitted
13857 direct-abstract-declarator. But `((*))', is a
13858 parenthesized abstract declarator. Finally, when T is a
13859 template parameter `(T)' is a
13860 parameter-declaration-clause, and not a parenthesized
13863 We first try and parse a parameter-declaration-clause,
13864 and then try a nested declarator (if FIRST is true).
13866 It is not an error for it not to be a
13867 parameter-declaration-clause, even when FIRST is
13873 The first is the declaration of a function while the
13874 second is the definition of a variable, including its
13877 Having seen only the parenthesis, we cannot know which of
13878 these two alternatives should be selected. Even more
13879 complex are examples like:
13884 The former is a function-declaration; the latter is a
13885 variable initialization.
13887 Thus again, we try a parameter-declaration-clause, and if
13888 that fails, we back out and return. */
13890 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13893 unsigned saved_num_template_parameter_lists;
13894 bool is_declarator = false;
13897 /* In a member-declarator, the only valid interpretation
13898 of a parenthesis is the start of a
13899 parameter-declaration-clause. (It is invalid to
13900 initialize a static data member with a parenthesized
13901 initializer; only the "=" form of initialization is
13904 cp_parser_parse_tentatively (parser);
13906 /* Consume the `('. */
13907 cp_lexer_consume_token (parser->lexer);
13910 /* If this is going to be an abstract declarator, we're
13911 in a declarator and we can't have default args. */
13912 parser->default_arg_ok_p = false;
13913 parser->in_declarator_p = true;
13916 /* Inside the function parameter list, surrounding
13917 template-parameter-lists do not apply. */
13918 saved_num_template_parameter_lists
13919 = parser->num_template_parameter_lists;
13920 parser->num_template_parameter_lists = 0;
13922 begin_scope (sk_function_parms, NULL_TREE);
13924 /* Parse the parameter-declaration-clause. */
13925 params = cp_parser_parameter_declaration_clause (parser);
13927 parser->num_template_parameter_lists
13928 = saved_num_template_parameter_lists;
13930 /* If all went well, parse the cv-qualifier-seq and the
13931 exception-specification. */
13932 if (member_p || cp_parser_parse_definitely (parser))
13934 cp_cv_quals cv_quals;
13935 tree exception_specification;
13938 is_declarator = true;
13940 if (ctor_dtor_or_conv_p)
13941 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13943 /* Consume the `)'. */
13944 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13946 /* Parse the cv-qualifier-seq. */
13947 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13948 /* And the exception-specification. */
13949 exception_specification
13950 = cp_parser_exception_specification_opt (parser);
13953 = cp_parser_late_return_type_opt (parser);
13955 /* Create the function-declarator. */
13956 declarator = make_call_declarator (declarator,
13959 exception_specification,
13961 /* Any subsequent parameter lists are to do with
13962 return type, so are not those of the declared
13964 parser->default_arg_ok_p = false;
13967 /* Remove the function parms from scope. */
13968 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13969 pop_binding (DECL_NAME (t), t);
13973 /* Repeat the main loop. */
13977 /* If this is the first, we can try a parenthesized
13981 bool saved_in_type_id_in_expr_p;
13983 parser->default_arg_ok_p = saved_default_arg_ok_p;
13984 parser->in_declarator_p = saved_in_declarator_p;
13986 /* Consume the `('. */
13987 cp_lexer_consume_token (parser->lexer);
13988 /* Parse the nested declarator. */
13989 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13990 parser->in_type_id_in_expr_p = true;
13992 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13993 /*parenthesized_p=*/NULL,
13995 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13997 /* Expect a `)'. */
13998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13999 declarator = cp_error_declarator;
14000 if (declarator == cp_error_declarator)
14003 goto handle_declarator;
14005 /* Otherwise, we must be done. */
14009 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14010 && token->type == CPP_OPEN_SQUARE)
14012 /* Parse an array-declarator. */
14015 if (ctor_dtor_or_conv_p)
14016 *ctor_dtor_or_conv_p = 0;
14019 parser->default_arg_ok_p = false;
14020 parser->in_declarator_p = true;
14021 /* Consume the `['. */
14022 cp_lexer_consume_token (parser->lexer);
14023 /* Peek at the next token. */
14024 token = cp_lexer_peek_token (parser->lexer);
14025 /* If the next token is `]', then there is no
14026 constant-expression. */
14027 if (token->type != CPP_CLOSE_SQUARE)
14029 bool non_constant_p;
14032 = cp_parser_constant_expression (parser,
14033 /*allow_non_constant=*/true,
14035 if (!non_constant_p)
14036 bounds = fold_non_dependent_expr (bounds);
14037 /* Normally, the array bound must be an integral constant
14038 expression. However, as an extension, we allow VLAs
14039 in function scopes. */
14040 else if (!parser->in_function_body)
14042 error_at (token->location,
14043 "array bound is not an integer constant");
14044 bounds = error_mark_node;
14046 else if (processing_template_decl && !error_operand_p (bounds))
14048 /* Remember this wasn't a constant-expression. */
14049 bounds = build_nop (TREE_TYPE (bounds), bounds);
14050 TREE_SIDE_EFFECTS (bounds) = 1;
14054 bounds = NULL_TREE;
14055 /* Look for the closing `]'. */
14056 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14058 declarator = cp_error_declarator;
14062 declarator = make_array_declarator (declarator, bounds);
14064 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14067 tree qualifying_scope;
14068 tree unqualified_name;
14069 special_function_kind sfk;
14071 bool pack_expansion_p = false;
14072 cp_token *declarator_id_start_token;
14074 /* Parse a declarator-id */
14075 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14078 cp_parser_parse_tentatively (parser);
14080 /* If we see an ellipsis, we should be looking at a
14082 if (token->type == CPP_ELLIPSIS)
14084 /* Consume the `...' */
14085 cp_lexer_consume_token (parser->lexer);
14087 pack_expansion_p = true;
14091 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14093 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14094 qualifying_scope = parser->scope;
14099 if (!unqualified_name && pack_expansion_p)
14101 /* Check whether an error occurred. */
14102 okay = !cp_parser_error_occurred (parser);
14104 /* We already consumed the ellipsis to mark a
14105 parameter pack, but we have no way to report it,
14106 so abort the tentative parse. We will be exiting
14107 immediately anyway. */
14108 cp_parser_abort_tentative_parse (parser);
14111 okay = cp_parser_parse_definitely (parser);
14114 unqualified_name = error_mark_node;
14115 else if (unqualified_name
14116 && (qualifying_scope
14117 || (TREE_CODE (unqualified_name)
14118 != IDENTIFIER_NODE)))
14120 cp_parser_error (parser, "expected unqualified-id");
14121 unqualified_name = error_mark_node;
14125 if (!unqualified_name)
14127 if (unqualified_name == error_mark_node)
14129 declarator = cp_error_declarator;
14130 pack_expansion_p = false;
14131 declarator->parameter_pack_p = false;
14135 if (qualifying_scope && at_namespace_scope_p ()
14136 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14138 /* In the declaration of a member of a template class
14139 outside of the class itself, the SCOPE will sometimes
14140 be a TYPENAME_TYPE. For example, given:
14142 template <typename T>
14143 int S<T>::R::i = 3;
14145 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14146 this context, we must resolve S<T>::R to an ordinary
14147 type, rather than a typename type.
14149 The reason we normally avoid resolving TYPENAME_TYPEs
14150 is that a specialization of `S' might render
14151 `S<T>::R' not a type. However, if `S' is
14152 specialized, then this `i' will not be used, so there
14153 is no harm in resolving the types here. */
14156 /* Resolve the TYPENAME_TYPE. */
14157 type = resolve_typename_type (qualifying_scope,
14158 /*only_current_p=*/false);
14159 /* If that failed, the declarator is invalid. */
14160 if (TREE_CODE (type) == TYPENAME_TYPE)
14161 error_at (declarator_id_start_token->location,
14162 "%<%T::%E%> is not a type",
14163 TYPE_CONTEXT (qualifying_scope),
14164 TYPE_IDENTIFIER (qualifying_scope));
14165 qualifying_scope = type;
14170 if (unqualified_name)
14174 if (qualifying_scope
14175 && CLASS_TYPE_P (qualifying_scope))
14176 class_type = qualifying_scope;
14178 class_type = current_class_type;
14180 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14182 tree name_type = TREE_TYPE (unqualified_name);
14183 if (class_type && same_type_p (name_type, class_type))
14185 if (qualifying_scope
14186 && CLASSTYPE_USE_TEMPLATE (name_type))
14188 error_at (declarator_id_start_token->location,
14189 "invalid use of constructor as a template");
14190 inform (declarator_id_start_token->location,
14191 "use %<%T::%D%> instead of %<%T::%D%> to "
14192 "name the constructor in a qualified name",
14194 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14195 class_type, name_type);
14196 declarator = cp_error_declarator;
14200 unqualified_name = constructor_name (class_type);
14204 /* We do not attempt to print the declarator
14205 here because we do not have enough
14206 information about its original syntactic
14208 cp_parser_error (parser, "invalid declarator");
14209 declarator = cp_error_declarator;
14216 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14217 sfk = sfk_destructor;
14218 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14219 sfk = sfk_conversion;
14220 else if (/* There's no way to declare a constructor
14221 for an anonymous type, even if the type
14222 got a name for linkage purposes. */
14223 !TYPE_WAS_ANONYMOUS (class_type)
14224 && constructor_name_p (unqualified_name,
14227 unqualified_name = constructor_name (class_type);
14228 sfk = sfk_constructor;
14231 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14232 *ctor_dtor_or_conv_p = -1;
14235 declarator = make_id_declarator (qualifying_scope,
14238 declarator->id_loc = token->location;
14239 declarator->parameter_pack_p = pack_expansion_p;
14241 if (pack_expansion_p)
14242 maybe_warn_variadic_templates ();
14245 handle_declarator:;
14246 scope = get_scope_of_declarator (declarator);
14248 /* Any names that appear after the declarator-id for a
14249 member are looked up in the containing scope. */
14250 pushed_scope = push_scope (scope);
14251 parser->in_declarator_p = true;
14252 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14253 || (declarator && declarator->kind == cdk_id))
14254 /* Default args are only allowed on function
14256 parser->default_arg_ok_p = saved_default_arg_ok_p;
14258 parser->default_arg_ok_p = false;
14267 /* For an abstract declarator, we might wind up with nothing at this
14268 point. That's an error; the declarator is not optional. */
14270 cp_parser_error (parser, "expected declarator");
14272 /* If we entered a scope, we must exit it now. */
14274 pop_scope (pushed_scope);
14276 parser->default_arg_ok_p = saved_default_arg_ok_p;
14277 parser->in_declarator_p = saved_in_declarator_p;
14282 /* Parse a ptr-operator.
14285 * cv-qualifier-seq [opt]
14287 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14292 & cv-qualifier-seq [opt]
14294 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14295 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14296 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14297 filled in with the TYPE containing the member. *CV_QUALS is
14298 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14299 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14300 Note that the tree codes returned by this function have nothing
14301 to do with the types of trees that will be eventually be created
14302 to represent the pointer or reference type being parsed. They are
14303 just constants with suggestive names. */
14304 static enum tree_code
14305 cp_parser_ptr_operator (cp_parser* parser,
14307 cp_cv_quals *cv_quals)
14309 enum tree_code code = ERROR_MARK;
14312 /* Assume that it's not a pointer-to-member. */
14314 /* And that there are no cv-qualifiers. */
14315 *cv_quals = TYPE_UNQUALIFIED;
14317 /* Peek at the next token. */
14318 token = cp_lexer_peek_token (parser->lexer);
14320 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14321 if (token->type == CPP_MULT)
14322 code = INDIRECT_REF;
14323 else if (token->type == CPP_AND)
14325 else if ((cxx_dialect != cxx98) &&
14326 token->type == CPP_AND_AND) /* C++0x only */
14327 code = NON_LVALUE_EXPR;
14329 if (code != ERROR_MARK)
14331 /* Consume the `*', `&' or `&&'. */
14332 cp_lexer_consume_token (parser->lexer);
14334 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14335 `&', if we are allowing GNU extensions. (The only qualifier
14336 that can legally appear after `&' is `restrict', but that is
14337 enforced during semantic analysis. */
14338 if (code == INDIRECT_REF
14339 || cp_parser_allow_gnu_extensions_p (parser))
14340 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14344 /* Try the pointer-to-member case. */
14345 cp_parser_parse_tentatively (parser);
14346 /* Look for the optional `::' operator. */
14347 cp_parser_global_scope_opt (parser,
14348 /*current_scope_valid_p=*/false);
14349 /* Look for the nested-name specifier. */
14350 token = cp_lexer_peek_token (parser->lexer);
14351 cp_parser_nested_name_specifier (parser,
14352 /*typename_keyword_p=*/false,
14353 /*check_dependency_p=*/true,
14355 /*is_declaration=*/false);
14356 /* If we found it, and the next token is a `*', then we are
14357 indeed looking at a pointer-to-member operator. */
14358 if (!cp_parser_error_occurred (parser)
14359 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14361 /* Indicate that the `*' operator was used. */
14362 code = INDIRECT_REF;
14364 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14365 error_at (token->location, "%qD is a namespace", parser->scope);
14368 /* The type of which the member is a member is given by the
14370 *type = parser->scope;
14371 /* The next name will not be qualified. */
14372 parser->scope = NULL_TREE;
14373 parser->qualifying_scope = NULL_TREE;
14374 parser->object_scope = NULL_TREE;
14375 /* Look for the optional cv-qualifier-seq. */
14376 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14379 /* If that didn't work we don't have a ptr-operator. */
14380 if (!cp_parser_parse_definitely (parser))
14381 cp_parser_error (parser, "expected ptr-operator");
14387 /* Parse an (optional) cv-qualifier-seq.
14390 cv-qualifier cv-qualifier-seq [opt]
14401 Returns a bitmask representing the cv-qualifiers. */
14404 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14406 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14411 cp_cv_quals cv_qualifier;
14413 /* Peek at the next token. */
14414 token = cp_lexer_peek_token (parser->lexer);
14415 /* See if it's a cv-qualifier. */
14416 switch (token->keyword)
14419 cv_qualifier = TYPE_QUAL_CONST;
14423 cv_qualifier = TYPE_QUAL_VOLATILE;
14427 cv_qualifier = TYPE_QUAL_RESTRICT;
14431 cv_qualifier = TYPE_UNQUALIFIED;
14438 if (cv_quals & cv_qualifier)
14440 error_at (token->location, "duplicate cv-qualifier");
14441 cp_lexer_purge_token (parser->lexer);
14445 cp_lexer_consume_token (parser->lexer);
14446 cv_quals |= cv_qualifier;
14453 /* Parse a late-specified return type, if any. This is not a separate
14454 non-terminal, but part of a function declarator, which looks like
14456 -> trailing-type-specifier-seq abstract-declarator(opt)
14458 Returns the type indicated by the type-id. */
14461 cp_parser_late_return_type_opt (cp_parser* parser)
14465 /* Peek at the next token. */
14466 token = cp_lexer_peek_token (parser->lexer);
14467 /* A late-specified return type is indicated by an initial '->'. */
14468 if (token->type != CPP_DEREF)
14471 /* Consume the ->. */
14472 cp_lexer_consume_token (parser->lexer);
14474 return cp_parser_trailing_type_id (parser);
14477 /* Parse a declarator-id.
14481 :: [opt] nested-name-specifier [opt] type-name
14483 In the `id-expression' case, the value returned is as for
14484 cp_parser_id_expression if the id-expression was an unqualified-id.
14485 If the id-expression was a qualified-id, then a SCOPE_REF is
14486 returned. The first operand is the scope (either a NAMESPACE_DECL
14487 or TREE_TYPE), but the second is still just a representation of an
14491 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14494 /* The expression must be an id-expression. Assume that qualified
14495 names are the names of types so that:
14498 int S<T>::R::i = 3;
14500 will work; we must treat `S<T>::R' as the name of a type.
14501 Similarly, assume that qualified names are templates, where
14505 int S<T>::R<T>::i = 3;
14508 id = cp_parser_id_expression (parser,
14509 /*template_keyword_p=*/false,
14510 /*check_dependency_p=*/false,
14511 /*template_p=*/NULL,
14512 /*declarator_p=*/true,
14514 if (id && BASELINK_P (id))
14515 id = BASELINK_FUNCTIONS (id);
14519 /* Parse a type-id.
14522 type-specifier-seq abstract-declarator [opt]
14524 Returns the TYPE specified. */
14527 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14528 bool is_trailing_return)
14530 cp_decl_specifier_seq type_specifier_seq;
14531 cp_declarator *abstract_declarator;
14533 /* Parse the type-specifier-seq. */
14534 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14535 is_trailing_return,
14536 &type_specifier_seq);
14537 if (type_specifier_seq.type == error_mark_node)
14538 return error_mark_node;
14540 /* There might or might not be an abstract declarator. */
14541 cp_parser_parse_tentatively (parser);
14542 /* Look for the declarator. */
14543 abstract_declarator
14544 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14545 /*parenthesized_p=*/NULL,
14546 /*member_p=*/false);
14547 /* Check to see if there really was a declarator. */
14548 if (!cp_parser_parse_definitely (parser))
14549 abstract_declarator = NULL;
14551 if (type_specifier_seq.type
14552 && type_uses_auto (type_specifier_seq.type))
14554 /* A type-id with type 'auto' is only ok if the abstract declarator
14555 is a function declarator with a late-specified return type. */
14556 if (abstract_declarator
14557 && abstract_declarator->kind == cdk_function
14558 && abstract_declarator->u.function.late_return_type)
14562 error ("invalid use of %<auto%>");
14563 return error_mark_node;
14567 return groktypename (&type_specifier_seq, abstract_declarator,
14571 static tree cp_parser_type_id (cp_parser *parser)
14573 return cp_parser_type_id_1 (parser, false, false);
14576 static tree cp_parser_template_type_arg (cp_parser *parser)
14578 return cp_parser_type_id_1 (parser, true, false);
14581 static tree cp_parser_trailing_type_id (cp_parser *parser)
14583 return cp_parser_type_id_1 (parser, false, true);
14586 /* Parse a type-specifier-seq.
14588 type-specifier-seq:
14589 type-specifier type-specifier-seq [opt]
14593 type-specifier-seq:
14594 attributes type-specifier-seq [opt]
14596 If IS_DECLARATION is true, we are at the start of a "condition" or
14597 exception-declaration, so we might be followed by a declarator-id.
14599 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14600 i.e. we've just seen "->".
14602 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14605 cp_parser_type_specifier_seq (cp_parser* parser,
14606 bool is_declaration,
14607 bool is_trailing_return,
14608 cp_decl_specifier_seq *type_specifier_seq)
14610 bool seen_type_specifier = false;
14611 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14612 cp_token *start_token = NULL;
14614 /* Clear the TYPE_SPECIFIER_SEQ. */
14615 clear_decl_specs (type_specifier_seq);
14617 /* In the context of a trailing return type, enum E { } is an
14618 elaborated-type-specifier followed by a function-body, not an
14620 if (is_trailing_return)
14621 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14623 /* Parse the type-specifiers and attributes. */
14626 tree type_specifier;
14627 bool is_cv_qualifier;
14629 /* Check for attributes first. */
14630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14632 type_specifier_seq->attributes =
14633 chainon (type_specifier_seq->attributes,
14634 cp_parser_attributes_opt (parser));
14638 /* record the token of the beginning of the type specifier seq,
14639 for error reporting purposes*/
14641 start_token = cp_lexer_peek_token (parser->lexer);
14643 /* Look for the type-specifier. */
14644 type_specifier = cp_parser_type_specifier (parser,
14646 type_specifier_seq,
14647 /*is_declaration=*/false,
14650 if (!type_specifier)
14652 /* If the first type-specifier could not be found, this is not a
14653 type-specifier-seq at all. */
14654 if (!seen_type_specifier)
14656 cp_parser_error (parser, "expected type-specifier");
14657 type_specifier_seq->type = error_mark_node;
14660 /* If subsequent type-specifiers could not be found, the
14661 type-specifier-seq is complete. */
14665 seen_type_specifier = true;
14666 /* The standard says that a condition can be:
14668 type-specifier-seq declarator = assignment-expression
14675 we should treat the "S" as a declarator, not as a
14676 type-specifier. The standard doesn't say that explicitly for
14677 type-specifier-seq, but it does say that for
14678 decl-specifier-seq in an ordinary declaration. Perhaps it
14679 would be clearer just to allow a decl-specifier-seq here, and
14680 then add a semantic restriction that if any decl-specifiers
14681 that are not type-specifiers appear, the program is invalid. */
14682 if (is_declaration && !is_cv_qualifier)
14683 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14686 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14689 /* Parse a parameter-declaration-clause.
14691 parameter-declaration-clause:
14692 parameter-declaration-list [opt] ... [opt]
14693 parameter-declaration-list , ...
14695 Returns a representation for the parameter declarations. A return
14696 value of NULL indicates a parameter-declaration-clause consisting
14697 only of an ellipsis. */
14700 cp_parser_parameter_declaration_clause (cp_parser* parser)
14707 /* Peek at the next token. */
14708 token = cp_lexer_peek_token (parser->lexer);
14709 /* Check for trivial parameter-declaration-clauses. */
14710 if (token->type == CPP_ELLIPSIS)
14712 /* Consume the `...' token. */
14713 cp_lexer_consume_token (parser->lexer);
14716 else if (token->type == CPP_CLOSE_PAREN)
14717 /* There are no parameters. */
14719 #ifndef NO_IMPLICIT_EXTERN_C
14720 if (in_system_header && current_class_type == NULL
14721 && current_lang_name == lang_name_c)
14725 return void_list_node;
14727 /* Check for `(void)', too, which is a special case. */
14728 else if (token->keyword == RID_VOID
14729 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14730 == CPP_CLOSE_PAREN))
14732 /* Consume the `void' token. */
14733 cp_lexer_consume_token (parser->lexer);
14734 /* There are no parameters. */
14735 return void_list_node;
14738 /* Parse the parameter-declaration-list. */
14739 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14740 /* If a parse error occurred while parsing the
14741 parameter-declaration-list, then the entire
14742 parameter-declaration-clause is erroneous. */
14746 /* Peek at the next token. */
14747 token = cp_lexer_peek_token (parser->lexer);
14748 /* If it's a `,', the clause should terminate with an ellipsis. */
14749 if (token->type == CPP_COMMA)
14751 /* Consume the `,'. */
14752 cp_lexer_consume_token (parser->lexer);
14753 /* Expect an ellipsis. */
14755 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14757 /* It might also be `...' if the optional trailing `,' was
14759 else if (token->type == CPP_ELLIPSIS)
14761 /* Consume the `...' token. */
14762 cp_lexer_consume_token (parser->lexer);
14763 /* And remember that we saw it. */
14767 ellipsis_p = false;
14769 /* Finish the parameter list. */
14771 parameters = chainon (parameters, void_list_node);
14776 /* Parse a parameter-declaration-list.
14778 parameter-declaration-list:
14779 parameter-declaration
14780 parameter-declaration-list , parameter-declaration
14782 Returns a representation of the parameter-declaration-list, as for
14783 cp_parser_parameter_declaration_clause. However, the
14784 `void_list_node' is never appended to the list. Upon return,
14785 *IS_ERROR will be true iff an error occurred. */
14788 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14790 tree parameters = NULL_TREE;
14791 tree *tail = ¶meters;
14792 bool saved_in_unbraced_linkage_specification_p;
14795 /* Assume all will go well. */
14797 /* The special considerations that apply to a function within an
14798 unbraced linkage specifications do not apply to the parameters
14799 to the function. */
14800 saved_in_unbraced_linkage_specification_p
14801 = parser->in_unbraced_linkage_specification_p;
14802 parser->in_unbraced_linkage_specification_p = false;
14804 /* Look for more parameters. */
14807 cp_parameter_declarator *parameter;
14808 tree decl = error_mark_node;
14809 bool parenthesized_p;
14810 /* Parse the parameter. */
14812 = cp_parser_parameter_declaration (parser,
14813 /*template_parm_p=*/false,
14816 /* We don't know yet if the enclosing context is deprecated, so wait
14817 and warn in grokparms if appropriate. */
14818 deprecated_state = DEPRECATED_SUPPRESS;
14821 decl = grokdeclarator (parameter->declarator,
14822 ¶meter->decl_specifiers,
14824 parameter->default_argument != NULL_TREE,
14825 ¶meter->decl_specifiers.attributes);
14827 deprecated_state = DEPRECATED_NORMAL;
14829 /* If a parse error occurred parsing the parameter declaration,
14830 then the entire parameter-declaration-list is erroneous. */
14831 if (decl == error_mark_node)
14834 parameters = error_mark_node;
14838 if (parameter->decl_specifiers.attributes)
14839 cplus_decl_attributes (&decl,
14840 parameter->decl_specifiers.attributes,
14842 if (DECL_NAME (decl))
14843 decl = pushdecl (decl);
14845 if (decl != error_mark_node)
14847 retrofit_lang_decl (decl);
14848 DECL_PARM_INDEX (decl) = ++index;
14851 /* Add the new parameter to the list. */
14852 *tail = build_tree_list (parameter->default_argument, decl);
14853 tail = &TREE_CHAIN (*tail);
14855 /* Peek at the next token. */
14856 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14857 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14858 /* These are for Objective-C++ */
14859 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14860 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14861 /* The parameter-declaration-list is complete. */
14863 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14867 /* Peek at the next token. */
14868 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14869 /* If it's an ellipsis, then the list is complete. */
14870 if (token->type == CPP_ELLIPSIS)
14872 /* Otherwise, there must be more parameters. Consume the
14874 cp_lexer_consume_token (parser->lexer);
14875 /* When parsing something like:
14877 int i(float f, double d)
14879 we can tell after seeing the declaration for "f" that we
14880 are not looking at an initialization of a variable "i",
14881 but rather at the declaration of a function "i".
14883 Due to the fact that the parsing of template arguments
14884 (as specified to a template-id) requires backtracking we
14885 cannot use this technique when inside a template argument
14887 if (!parser->in_template_argument_list_p
14888 && !parser->in_type_id_in_expr_p
14889 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14890 /* However, a parameter-declaration of the form
14891 "foat(f)" (which is a valid declaration of a
14892 parameter "f") can also be interpreted as an
14893 expression (the conversion of "f" to "float"). */
14894 && !parenthesized_p)
14895 cp_parser_commit_to_tentative_parse (parser);
14899 cp_parser_error (parser, "expected %<,%> or %<...%>");
14900 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14901 cp_parser_skip_to_closing_parenthesis (parser,
14902 /*recovering=*/true,
14903 /*or_comma=*/false,
14904 /*consume_paren=*/false);
14909 parser->in_unbraced_linkage_specification_p
14910 = saved_in_unbraced_linkage_specification_p;
14915 /* Parse a parameter declaration.
14917 parameter-declaration:
14918 decl-specifier-seq ... [opt] declarator
14919 decl-specifier-seq declarator = assignment-expression
14920 decl-specifier-seq ... [opt] abstract-declarator [opt]
14921 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14923 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14924 declares a template parameter. (In that case, a non-nested `>'
14925 token encountered during the parsing of the assignment-expression
14926 is not interpreted as a greater-than operator.)
14928 Returns a representation of the parameter, or NULL if an error
14929 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14930 true iff the declarator is of the form "(p)". */
14932 static cp_parameter_declarator *
14933 cp_parser_parameter_declaration (cp_parser *parser,
14934 bool template_parm_p,
14935 bool *parenthesized_p)
14937 int declares_class_or_enum;
14938 bool greater_than_is_operator_p;
14939 cp_decl_specifier_seq decl_specifiers;
14940 cp_declarator *declarator;
14941 tree default_argument;
14942 cp_token *token = NULL, *declarator_token_start = NULL;
14943 const char *saved_message;
14945 /* In a template parameter, `>' is not an operator.
14949 When parsing a default template-argument for a non-type
14950 template-parameter, the first non-nested `>' is taken as the end
14951 of the template parameter-list rather than a greater-than
14953 greater_than_is_operator_p = !template_parm_p;
14955 /* Type definitions may not appear in parameter types. */
14956 saved_message = parser->type_definition_forbidden_message;
14957 parser->type_definition_forbidden_message
14958 = "types may not be defined in parameter types";
14960 /* Parse the declaration-specifiers. */
14961 cp_parser_decl_specifier_seq (parser,
14962 CP_PARSER_FLAGS_NONE,
14964 &declares_class_or_enum);
14965 /* If an error occurred, there's no reason to attempt to parse the
14966 rest of the declaration. */
14967 if (cp_parser_error_occurred (parser))
14969 parser->type_definition_forbidden_message = saved_message;
14973 /* Peek at the next token. */
14974 token = cp_lexer_peek_token (parser->lexer);
14976 /* If the next token is a `)', `,', `=', `>', or `...', then there
14977 is no declarator. However, when variadic templates are enabled,
14978 there may be a declarator following `...'. */
14979 if (token->type == CPP_CLOSE_PAREN
14980 || token->type == CPP_COMMA
14981 || token->type == CPP_EQ
14982 || token->type == CPP_GREATER)
14985 if (parenthesized_p)
14986 *parenthesized_p = false;
14988 /* Otherwise, there should be a declarator. */
14991 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14992 parser->default_arg_ok_p = false;
14994 /* After seeing a decl-specifier-seq, if the next token is not a
14995 "(", there is no possibility that the code is a valid
14996 expression. Therefore, if parsing tentatively, we commit at
14998 if (!parser->in_template_argument_list_p
14999 /* In an expression context, having seen:
15003 we cannot be sure whether we are looking at a
15004 function-type (taking a "char" as a parameter) or a cast
15005 of some object of type "char" to "int". */
15006 && !parser->in_type_id_in_expr_p
15007 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15008 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15009 cp_parser_commit_to_tentative_parse (parser);
15010 /* Parse the declarator. */
15011 declarator_token_start = token;
15012 declarator = cp_parser_declarator (parser,
15013 CP_PARSER_DECLARATOR_EITHER,
15014 /*ctor_dtor_or_conv_p=*/NULL,
15016 /*member_p=*/false);
15017 parser->default_arg_ok_p = saved_default_arg_ok_p;
15018 /* After the declarator, allow more attributes. */
15019 decl_specifiers.attributes
15020 = chainon (decl_specifiers.attributes,
15021 cp_parser_attributes_opt (parser));
15024 /* If the next token is an ellipsis, and we have not seen a
15025 declarator name, and the type of the declarator contains parameter
15026 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15027 a parameter pack expansion expression. Otherwise, leave the
15028 ellipsis for a C-style variadic function. */
15029 token = cp_lexer_peek_token (parser->lexer);
15030 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15032 tree type = decl_specifiers.type;
15034 if (type && DECL_P (type))
15035 type = TREE_TYPE (type);
15038 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15039 && declarator_can_be_parameter_pack (declarator)
15040 && (!declarator || !declarator->parameter_pack_p)
15041 && uses_parameter_packs (type))
15043 /* Consume the `...'. */
15044 cp_lexer_consume_token (parser->lexer);
15045 maybe_warn_variadic_templates ();
15047 /* Build a pack expansion type */
15049 declarator->parameter_pack_p = true;
15051 decl_specifiers.type = make_pack_expansion (type);
15055 /* The restriction on defining new types applies only to the type
15056 of the parameter, not to the default argument. */
15057 parser->type_definition_forbidden_message = saved_message;
15059 /* If the next token is `=', then process a default argument. */
15060 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15062 /* Consume the `='. */
15063 cp_lexer_consume_token (parser->lexer);
15065 /* If we are defining a class, then the tokens that make up the
15066 default argument must be saved and processed later. */
15067 if (!template_parm_p && at_class_scope_p ()
15068 && TYPE_BEING_DEFINED (current_class_type)
15069 && !LAMBDA_TYPE_P (current_class_type))
15071 unsigned depth = 0;
15072 int maybe_template_id = 0;
15073 cp_token *first_token;
15076 /* Add tokens until we have processed the entire default
15077 argument. We add the range [first_token, token). */
15078 first_token = cp_lexer_peek_token (parser->lexer);
15083 /* Peek at the next token. */
15084 token = cp_lexer_peek_token (parser->lexer);
15085 /* What we do depends on what token we have. */
15086 switch (token->type)
15088 /* In valid code, a default argument must be
15089 immediately followed by a `,' `)', or `...'. */
15091 if (depth == 0 && maybe_template_id)
15093 /* If we've seen a '<', we might be in a
15094 template-argument-list. Until Core issue 325 is
15095 resolved, we don't know how this situation ought
15096 to be handled, so try to DTRT. We check whether
15097 what comes after the comma is a valid parameter
15098 declaration list. If it is, then the comma ends
15099 the default argument; otherwise the default
15100 argument continues. */
15101 bool error = false;
15103 /* Set ITALP so cp_parser_parameter_declaration_list
15104 doesn't decide to commit to this parse. */
15105 bool saved_italp = parser->in_template_argument_list_p;
15106 parser->in_template_argument_list_p = true;
15108 cp_parser_parse_tentatively (parser);
15109 cp_lexer_consume_token (parser->lexer);
15110 cp_parser_parameter_declaration_list (parser, &error);
15111 if (!cp_parser_error_occurred (parser) && !error)
15113 cp_parser_abort_tentative_parse (parser);
15115 parser->in_template_argument_list_p = saved_italp;
15118 case CPP_CLOSE_PAREN:
15120 /* If we run into a non-nested `;', `}', or `]',
15121 then the code is invalid -- but the default
15122 argument is certainly over. */
15123 case CPP_SEMICOLON:
15124 case CPP_CLOSE_BRACE:
15125 case CPP_CLOSE_SQUARE:
15128 /* Update DEPTH, if necessary. */
15129 else if (token->type == CPP_CLOSE_PAREN
15130 || token->type == CPP_CLOSE_BRACE
15131 || token->type == CPP_CLOSE_SQUARE)
15135 case CPP_OPEN_PAREN:
15136 case CPP_OPEN_SQUARE:
15137 case CPP_OPEN_BRACE:
15143 /* This might be the comparison operator, or it might
15144 start a template argument list. */
15145 ++maybe_template_id;
15149 if (cxx_dialect == cxx98)
15151 /* Fall through for C++0x, which treats the `>>'
15152 operator like two `>' tokens in certain
15158 /* This might be an operator, or it might close a
15159 template argument list. But if a previous '<'
15160 started a template argument list, this will have
15161 closed it, so we can't be in one anymore. */
15162 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15163 if (maybe_template_id < 0)
15164 maybe_template_id = 0;
15168 /* If we run out of tokens, issue an error message. */
15170 case CPP_PRAGMA_EOL:
15171 error_at (token->location, "file ends in default argument");
15177 /* In these cases, we should look for template-ids.
15178 For example, if the default argument is
15179 `X<int, double>()', we need to do name lookup to
15180 figure out whether or not `X' is a template; if
15181 so, the `,' does not end the default argument.
15183 That is not yet done. */
15190 /* If we've reached the end, stop. */
15194 /* Add the token to the token block. */
15195 token = cp_lexer_consume_token (parser->lexer);
15198 /* Create a DEFAULT_ARG to represent the unparsed default
15200 default_argument = make_node (DEFAULT_ARG);
15201 DEFARG_TOKENS (default_argument)
15202 = cp_token_cache_new (first_token, token);
15203 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15205 /* Outside of a class definition, we can just parse the
15206 assignment-expression. */
15209 token = cp_lexer_peek_token (parser->lexer);
15211 = cp_parser_default_argument (parser, template_parm_p);
15214 if (!parser->default_arg_ok_p)
15216 if (flag_permissive)
15217 warning (0, "deprecated use of default argument for parameter of non-function");
15220 error_at (token->location,
15221 "default arguments are only "
15222 "permitted for function parameters");
15223 default_argument = NULL_TREE;
15226 else if ((declarator && declarator->parameter_pack_p)
15227 || (decl_specifiers.type
15228 && PACK_EXPANSION_P (decl_specifiers.type)))
15230 /* Find the name of the parameter pack. */
15231 cp_declarator *id_declarator = declarator;
15232 while (id_declarator && id_declarator->kind != cdk_id)
15233 id_declarator = id_declarator->declarator;
15235 if (id_declarator && id_declarator->kind == cdk_id)
15236 error_at (declarator_token_start->location,
15238 ? "template parameter pack %qD"
15239 " cannot have a default argument"
15240 : "parameter pack %qD cannot have a default argument",
15241 id_declarator->u.id.unqualified_name);
15243 error_at (declarator_token_start->location,
15245 ? "template parameter pack cannot have a default argument"
15246 : "parameter pack cannot have a default argument");
15248 default_argument = NULL_TREE;
15252 default_argument = NULL_TREE;
15254 return make_parameter_declarator (&decl_specifiers,
15259 /* Parse a default argument and return it.
15261 TEMPLATE_PARM_P is true if this is a default argument for a
15262 non-type template parameter. */
15264 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15266 tree default_argument = NULL_TREE;
15267 bool saved_greater_than_is_operator_p;
15268 bool saved_local_variables_forbidden_p;
15270 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15272 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15273 parser->greater_than_is_operator_p = !template_parm_p;
15274 /* Local variable names (and the `this' keyword) may not
15275 appear in a default argument. */
15276 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15277 parser->local_variables_forbidden_p = true;
15278 /* Parse the assignment-expression. */
15279 if (template_parm_p)
15280 push_deferring_access_checks (dk_no_deferred);
15282 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15283 if (template_parm_p)
15284 pop_deferring_access_checks ();
15285 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15286 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15288 return default_argument;
15291 /* Parse a function-body.
15294 compound_statement */
15297 cp_parser_function_body (cp_parser *parser)
15299 cp_parser_compound_statement (parser, NULL, false);
15302 /* Parse a ctor-initializer-opt followed by a function-body. Return
15303 true if a ctor-initializer was present. */
15306 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15309 bool ctor_initializer_p;
15311 /* Begin the function body. */
15312 body = begin_function_body ();
15313 /* Parse the optional ctor-initializer. */
15314 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15315 /* Parse the function-body. */
15316 cp_parser_function_body (parser);
15317 /* Finish the function body. */
15318 finish_function_body (body);
15320 return ctor_initializer_p;
15323 /* Parse an initializer.
15326 = initializer-clause
15327 ( expression-list )
15329 Returns an expression representing the initializer. If no
15330 initializer is present, NULL_TREE is returned.
15332 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15333 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15334 set to TRUE if there is no initializer present. If there is an
15335 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15336 is set to true; otherwise it is set to false. */
15339 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15340 bool* non_constant_p)
15345 /* Peek at the next token. */
15346 token = cp_lexer_peek_token (parser->lexer);
15348 /* Let our caller know whether or not this initializer was
15350 *is_direct_init = (token->type != CPP_EQ);
15351 /* Assume that the initializer is constant. */
15352 *non_constant_p = false;
15354 if (token->type == CPP_EQ)
15356 /* Consume the `='. */
15357 cp_lexer_consume_token (parser->lexer);
15358 /* Parse the initializer-clause. */
15359 init = cp_parser_initializer_clause (parser, non_constant_p);
15361 else if (token->type == CPP_OPEN_PAREN)
15364 vec = cp_parser_parenthesized_expression_list (parser, false,
15366 /*allow_expansion_p=*/true,
15369 return error_mark_node;
15370 init = build_tree_list_vec (vec);
15371 release_tree_vector (vec);
15373 else if (token->type == CPP_OPEN_BRACE)
15375 maybe_warn_cpp0x ("extended initializer lists");
15376 init = cp_parser_braced_list (parser, non_constant_p);
15377 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15381 /* Anything else is an error. */
15382 cp_parser_error (parser, "expected initializer");
15383 init = error_mark_node;
15389 /* Parse an initializer-clause.
15391 initializer-clause:
15392 assignment-expression
15395 Returns an expression representing the initializer.
15397 If the `assignment-expression' production is used the value
15398 returned is simply a representation for the expression.
15400 Otherwise, calls cp_parser_braced_list. */
15403 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15407 /* Assume the expression is constant. */
15408 *non_constant_p = false;
15410 /* If it is not a `{', then we are looking at an
15411 assignment-expression. */
15412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15415 = cp_parser_constant_expression (parser,
15416 /*allow_non_constant_p=*/true,
15418 if (!*non_constant_p)
15419 initializer = fold_non_dependent_expr (initializer);
15422 initializer = cp_parser_braced_list (parser, non_constant_p);
15424 return initializer;
15427 /* Parse a brace-enclosed initializer list.
15430 { initializer-list , [opt] }
15433 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15434 the elements of the initializer-list (or NULL, if the last
15435 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15436 NULL_TREE. There is no way to detect whether or not the optional
15437 trailing `,' was provided. NON_CONSTANT_P is as for
15438 cp_parser_initializer. */
15441 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15445 /* Consume the `{' token. */
15446 cp_lexer_consume_token (parser->lexer);
15447 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15448 initializer = make_node (CONSTRUCTOR);
15449 /* If it's not a `}', then there is a non-trivial initializer. */
15450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15452 /* Parse the initializer list. */
15453 CONSTRUCTOR_ELTS (initializer)
15454 = cp_parser_initializer_list (parser, non_constant_p);
15455 /* A trailing `,' token is allowed. */
15456 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15457 cp_lexer_consume_token (parser->lexer);
15459 /* Now, there should be a trailing `}'. */
15460 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15461 TREE_TYPE (initializer) = init_list_type_node;
15462 return initializer;
15465 /* Parse an initializer-list.
15468 initializer-clause ... [opt]
15469 initializer-list , initializer-clause ... [opt]
15474 identifier : initializer-clause
15475 initializer-list, identifier : initializer-clause
15477 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15478 for the initializer. If the INDEX of the elt is non-NULL, it is the
15479 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15480 as for cp_parser_initializer. */
15482 static VEC(constructor_elt,gc) *
15483 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15485 VEC(constructor_elt,gc) *v = NULL;
15487 /* Assume all of the expressions are constant. */
15488 *non_constant_p = false;
15490 /* Parse the rest of the list. */
15496 bool clause_non_constant_p;
15498 /* If the next token is an identifier and the following one is a
15499 colon, we are looking at the GNU designated-initializer
15501 if (cp_parser_allow_gnu_extensions_p (parser)
15502 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15503 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15505 /* Warn the user that they are using an extension. */
15506 pedwarn (input_location, OPT_pedantic,
15507 "ISO C++ does not allow designated initializers");
15508 /* Consume the identifier. */
15509 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15510 /* Consume the `:'. */
15511 cp_lexer_consume_token (parser->lexer);
15514 identifier = NULL_TREE;
15516 /* Parse the initializer. */
15517 initializer = cp_parser_initializer_clause (parser,
15518 &clause_non_constant_p);
15519 /* If any clause is non-constant, so is the entire initializer. */
15520 if (clause_non_constant_p)
15521 *non_constant_p = true;
15523 /* If we have an ellipsis, this is an initializer pack
15525 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15527 /* Consume the `...'. */
15528 cp_lexer_consume_token (parser->lexer);
15530 /* Turn the initializer into an initializer expansion. */
15531 initializer = make_pack_expansion (initializer);
15534 /* Add it to the vector. */
15535 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15537 /* If the next token is not a comma, we have reached the end of
15539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15542 /* Peek at the next token. */
15543 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15544 /* If the next token is a `}', then we're still done. An
15545 initializer-clause can have a trailing `,' after the
15546 initializer-list and before the closing `}'. */
15547 if (token->type == CPP_CLOSE_BRACE)
15550 /* Consume the `,' token. */
15551 cp_lexer_consume_token (parser->lexer);
15557 /* Classes [gram.class] */
15559 /* Parse a class-name.
15565 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15566 to indicate that names looked up in dependent types should be
15567 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15568 keyword has been used to indicate that the name that appears next
15569 is a template. TAG_TYPE indicates the explicit tag given before
15570 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15571 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15572 is the class being defined in a class-head.
15574 Returns the TYPE_DECL representing the class. */
15577 cp_parser_class_name (cp_parser *parser,
15578 bool typename_keyword_p,
15579 bool template_keyword_p,
15580 enum tag_types tag_type,
15581 bool check_dependency_p,
15583 bool is_declaration)
15589 tree identifier = NULL_TREE;
15591 /* All class-names start with an identifier. */
15592 token = cp_lexer_peek_token (parser->lexer);
15593 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15595 cp_parser_error (parser, "expected class-name");
15596 return error_mark_node;
15599 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15600 to a template-id, so we save it here. */
15601 scope = parser->scope;
15602 if (scope == error_mark_node)
15603 return error_mark_node;
15605 /* Any name names a type if we're following the `typename' keyword
15606 in a qualified name where the enclosing scope is type-dependent. */
15607 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15608 && dependent_type_p (scope));
15609 /* Handle the common case (an identifier, but not a template-id)
15611 if (token->type == CPP_NAME
15612 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15614 cp_token *identifier_token;
15617 /* Look for the identifier. */
15618 identifier_token = cp_lexer_peek_token (parser->lexer);
15619 ambiguous_p = identifier_token->ambiguous_p;
15620 identifier = cp_parser_identifier (parser);
15621 /* If the next token isn't an identifier, we are certainly not
15622 looking at a class-name. */
15623 if (identifier == error_mark_node)
15624 decl = error_mark_node;
15625 /* If we know this is a type-name, there's no need to look it
15627 else if (typename_p)
15631 tree ambiguous_decls;
15632 /* If we already know that this lookup is ambiguous, then
15633 we've already issued an error message; there's no reason
15637 cp_parser_simulate_error (parser);
15638 return error_mark_node;
15640 /* If the next token is a `::', then the name must be a type
15643 [basic.lookup.qual]
15645 During the lookup for a name preceding the :: scope
15646 resolution operator, object, function, and enumerator
15647 names are ignored. */
15648 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15649 tag_type = typename_type;
15650 /* Look up the name. */
15651 decl = cp_parser_lookup_name (parser, identifier,
15653 /*is_template=*/false,
15654 /*is_namespace=*/false,
15655 check_dependency_p,
15657 identifier_token->location);
15658 if (ambiguous_decls)
15660 error_at (identifier_token->location,
15661 "reference to %qD is ambiguous", identifier);
15662 print_candidates (ambiguous_decls);
15663 if (cp_parser_parsing_tentatively (parser))
15665 identifier_token->ambiguous_p = true;
15666 cp_parser_simulate_error (parser);
15668 return error_mark_node;
15674 /* Try a template-id. */
15675 decl = cp_parser_template_id (parser, template_keyword_p,
15676 check_dependency_p,
15678 if (decl == error_mark_node)
15679 return error_mark_node;
15682 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15684 /* If this is a typename, create a TYPENAME_TYPE. */
15685 if (typename_p && decl != error_mark_node)
15687 decl = make_typename_type (scope, decl, typename_type,
15688 /*complain=*/tf_error);
15689 if (decl != error_mark_node)
15690 decl = TYPE_NAME (decl);
15693 /* Check to see that it is really the name of a class. */
15694 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15695 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15696 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15697 /* Situations like this:
15699 template <typename T> struct A {
15700 typename T::template X<int>::I i;
15703 are problematic. Is `T::template X<int>' a class-name? The
15704 standard does not seem to be definitive, but there is no other
15705 valid interpretation of the following `::'. Therefore, those
15706 names are considered class-names. */
15708 decl = make_typename_type (scope, decl, tag_type, tf_error);
15709 if (decl != error_mark_node)
15710 decl = TYPE_NAME (decl);
15712 else if (TREE_CODE (decl) != TYPE_DECL
15713 || TREE_TYPE (decl) == error_mark_node
15714 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15715 decl = error_mark_node;
15717 if (decl == error_mark_node)
15718 cp_parser_error (parser, "expected class-name");
15719 else if (identifier && !parser->scope)
15720 maybe_note_name_used_in_class (identifier, decl);
15725 /* Parse a class-specifier.
15728 class-head { member-specification [opt] }
15730 Returns the TREE_TYPE representing the class. */
15733 cp_parser_class_specifier (cp_parser* parser)
15736 tree attributes = NULL_TREE;
15737 bool nested_name_specifier_p;
15738 unsigned saved_num_template_parameter_lists;
15739 bool saved_in_function_body;
15740 bool saved_in_unbraced_linkage_specification_p;
15741 tree old_scope = NULL_TREE;
15742 tree scope = NULL_TREE;
15745 push_deferring_access_checks (dk_no_deferred);
15747 /* Parse the class-head. */
15748 type = cp_parser_class_head (parser,
15749 &nested_name_specifier_p,
15752 /* If the class-head was a semantic disaster, skip the entire body
15756 cp_parser_skip_to_end_of_block_or_statement (parser);
15757 pop_deferring_access_checks ();
15758 return error_mark_node;
15761 /* Look for the `{'. */
15762 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15764 pop_deferring_access_checks ();
15765 return error_mark_node;
15768 /* Process the base classes. If they're invalid, skip the
15769 entire class body. */
15770 if (!xref_basetypes (type, bases))
15772 /* Consuming the closing brace yields better error messages
15774 if (cp_parser_skip_to_closing_brace (parser))
15775 cp_lexer_consume_token (parser->lexer);
15776 pop_deferring_access_checks ();
15777 return error_mark_node;
15780 /* Issue an error message if type-definitions are forbidden here. */
15781 cp_parser_check_type_definition (parser);
15782 /* Remember that we are defining one more class. */
15783 ++parser->num_classes_being_defined;
15784 /* Inside the class, surrounding template-parameter-lists do not
15786 saved_num_template_parameter_lists
15787 = parser->num_template_parameter_lists;
15788 parser->num_template_parameter_lists = 0;
15789 /* We are not in a function body. */
15790 saved_in_function_body = parser->in_function_body;
15791 parser->in_function_body = false;
15792 /* We are not immediately inside an extern "lang" block. */
15793 saved_in_unbraced_linkage_specification_p
15794 = parser->in_unbraced_linkage_specification_p;
15795 parser->in_unbraced_linkage_specification_p = false;
15797 /* Start the class. */
15798 if (nested_name_specifier_p)
15800 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15801 old_scope = push_inner_scope (scope);
15803 type = begin_class_definition (type, attributes);
15805 if (type == error_mark_node)
15806 /* If the type is erroneous, skip the entire body of the class. */
15807 cp_parser_skip_to_closing_brace (parser);
15809 /* Parse the member-specification. */
15810 cp_parser_member_specification_opt (parser);
15812 /* Look for the trailing `}'. */
15813 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15814 /* Look for trailing attributes to apply to this class. */
15815 if (cp_parser_allow_gnu_extensions_p (parser))
15816 attributes = cp_parser_attributes_opt (parser);
15817 if (type != error_mark_node)
15818 type = finish_struct (type, attributes);
15819 if (nested_name_specifier_p)
15820 pop_inner_scope (old_scope, scope);
15821 /* If this class is not itself within the scope of another class,
15822 then we need to parse the bodies of all of the queued function
15823 definitions. Note that the queued functions defined in a class
15824 are not always processed immediately following the
15825 class-specifier for that class. Consider:
15828 struct B { void f() { sizeof (A); } };
15831 If `f' were processed before the processing of `A' were
15832 completed, there would be no way to compute the size of `A'.
15833 Note that the nesting we are interested in here is lexical --
15834 not the semantic nesting given by TYPE_CONTEXT. In particular,
15837 struct A { struct B; };
15838 struct A::B { void f() { } };
15840 there is no need to delay the parsing of `A::B::f'. */
15841 if (--parser->num_classes_being_defined == 0)
15845 tree class_type = NULL_TREE;
15846 tree pushed_scope = NULL_TREE;
15848 /* In a first pass, parse default arguments to the functions.
15849 Then, in a second pass, parse the bodies of the functions.
15850 This two-phased approach handles cases like:
15858 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15859 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15860 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15861 TREE_PURPOSE (parser->unparsed_functions_queues)
15862 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15864 fn = TREE_VALUE (queue_entry);
15865 /* If there are default arguments that have not yet been processed,
15866 take care of them now. */
15867 if (class_type != TREE_PURPOSE (queue_entry))
15870 pop_scope (pushed_scope);
15871 class_type = TREE_PURPOSE (queue_entry);
15872 pushed_scope = push_scope (class_type);
15874 /* Make sure that any template parameters are in scope. */
15875 maybe_begin_member_template_processing (fn);
15876 /* Parse the default argument expressions. */
15877 cp_parser_late_parsing_default_args (parser, fn);
15878 /* Remove any template parameters from the symbol table. */
15879 maybe_end_member_template_processing ();
15882 pop_scope (pushed_scope);
15883 /* Now parse the body of the functions. */
15884 for (TREE_VALUE (parser->unparsed_functions_queues)
15885 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15886 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15887 TREE_VALUE (parser->unparsed_functions_queues)
15888 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15890 /* Figure out which function we need to process. */
15891 fn = TREE_VALUE (queue_entry);
15892 /* Parse the function. */
15893 cp_parser_late_parsing_for_member (parser, fn);
15897 /* Put back any saved access checks. */
15898 pop_deferring_access_checks ();
15900 /* Restore saved state. */
15901 parser->in_function_body = saved_in_function_body;
15902 parser->num_template_parameter_lists
15903 = saved_num_template_parameter_lists;
15904 parser->in_unbraced_linkage_specification_p
15905 = saved_in_unbraced_linkage_specification_p;
15910 /* Parse a class-head.
15913 class-key identifier [opt] base-clause [opt]
15914 class-key nested-name-specifier identifier base-clause [opt]
15915 class-key nested-name-specifier [opt] template-id
15919 class-key attributes identifier [opt] base-clause [opt]
15920 class-key attributes nested-name-specifier identifier base-clause [opt]
15921 class-key attributes nested-name-specifier [opt] template-id
15924 Upon return BASES is initialized to the list of base classes (or
15925 NULL, if there are none) in the same form returned by
15926 cp_parser_base_clause.
15928 Returns the TYPE of the indicated class. Sets
15929 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15930 involving a nested-name-specifier was used, and FALSE otherwise.
15932 Returns error_mark_node if this is not a class-head.
15934 Returns NULL_TREE if the class-head is syntactically valid, but
15935 semantically invalid in a way that means we should skip the entire
15936 body of the class. */
15939 cp_parser_class_head (cp_parser* parser,
15940 bool* nested_name_specifier_p,
15941 tree *attributes_p,
15944 tree nested_name_specifier;
15945 enum tag_types class_key;
15946 tree id = NULL_TREE;
15947 tree type = NULL_TREE;
15949 bool template_id_p = false;
15950 bool qualified_p = false;
15951 bool invalid_nested_name_p = false;
15952 bool invalid_explicit_specialization_p = false;
15953 tree pushed_scope = NULL_TREE;
15954 unsigned num_templates;
15955 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15956 /* Assume no nested-name-specifier will be present. */
15957 *nested_name_specifier_p = false;
15958 /* Assume no template parameter lists will be used in defining the
15962 *bases = NULL_TREE;
15964 /* Look for the class-key. */
15965 class_key = cp_parser_class_key (parser);
15966 if (class_key == none_type)
15967 return error_mark_node;
15969 /* Parse the attributes. */
15970 attributes = cp_parser_attributes_opt (parser);
15972 /* If the next token is `::', that is invalid -- but sometimes
15973 people do try to write:
15977 Handle this gracefully by accepting the extra qualifier, and then
15978 issuing an error about it later if this really is a
15979 class-head. If it turns out just to be an elaborated type
15980 specifier, remain silent. */
15981 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15982 qualified_p = true;
15984 push_deferring_access_checks (dk_no_check);
15986 /* Determine the name of the class. Begin by looking for an
15987 optional nested-name-specifier. */
15988 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15989 nested_name_specifier
15990 = cp_parser_nested_name_specifier_opt (parser,
15991 /*typename_keyword_p=*/false,
15992 /*check_dependency_p=*/false,
15994 /*is_declaration=*/false);
15995 /* If there was a nested-name-specifier, then there *must* be an
15997 if (nested_name_specifier)
15999 type_start_token = cp_lexer_peek_token (parser->lexer);
16000 /* Although the grammar says `identifier', it really means
16001 `class-name' or `template-name'. You are only allowed to
16002 define a class that has already been declared with this
16005 The proposed resolution for Core Issue 180 says that wherever
16006 you see `class T::X' you should treat `X' as a type-name.
16008 It is OK to define an inaccessible class; for example:
16010 class A { class B; };
16013 We do not know if we will see a class-name, or a
16014 template-name. We look for a class-name first, in case the
16015 class-name is a template-id; if we looked for the
16016 template-name first we would stop after the template-name. */
16017 cp_parser_parse_tentatively (parser);
16018 type = cp_parser_class_name (parser,
16019 /*typename_keyword_p=*/false,
16020 /*template_keyword_p=*/false,
16022 /*check_dependency_p=*/false,
16023 /*class_head_p=*/true,
16024 /*is_declaration=*/false);
16025 /* If that didn't work, ignore the nested-name-specifier. */
16026 if (!cp_parser_parse_definitely (parser))
16028 invalid_nested_name_p = true;
16029 type_start_token = cp_lexer_peek_token (parser->lexer);
16030 id = cp_parser_identifier (parser);
16031 if (id == error_mark_node)
16034 /* If we could not find a corresponding TYPE, treat this
16035 declaration like an unqualified declaration. */
16036 if (type == error_mark_node)
16037 nested_name_specifier = NULL_TREE;
16038 /* Otherwise, count the number of templates used in TYPE and its
16039 containing scopes. */
16044 for (scope = TREE_TYPE (type);
16045 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16046 scope = (TYPE_P (scope)
16047 ? TYPE_CONTEXT (scope)
16048 : DECL_CONTEXT (scope)))
16050 && CLASS_TYPE_P (scope)
16051 && CLASSTYPE_TEMPLATE_INFO (scope)
16052 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16053 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16057 /* Otherwise, the identifier is optional. */
16060 /* We don't know whether what comes next is a template-id,
16061 an identifier, or nothing at all. */
16062 cp_parser_parse_tentatively (parser);
16063 /* Check for a template-id. */
16064 type_start_token = cp_lexer_peek_token (parser->lexer);
16065 id = cp_parser_template_id (parser,
16066 /*template_keyword_p=*/false,
16067 /*check_dependency_p=*/true,
16068 /*is_declaration=*/true);
16069 /* If that didn't work, it could still be an identifier. */
16070 if (!cp_parser_parse_definitely (parser))
16072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16074 type_start_token = cp_lexer_peek_token (parser->lexer);
16075 id = cp_parser_identifier (parser);
16082 template_id_p = true;
16087 pop_deferring_access_checks ();
16090 cp_parser_check_for_invalid_template_id (parser, id,
16091 type_start_token->location);
16093 /* If it's not a `:' or a `{' then we can't really be looking at a
16094 class-head, since a class-head only appears as part of a
16095 class-specifier. We have to detect this situation before calling
16096 xref_tag, since that has irreversible side-effects. */
16097 if (!cp_parser_next_token_starts_class_definition_p (parser))
16099 cp_parser_error (parser, "expected %<{%> or %<:%>");
16100 return error_mark_node;
16103 /* At this point, we're going ahead with the class-specifier, even
16104 if some other problem occurs. */
16105 cp_parser_commit_to_tentative_parse (parser);
16106 /* Issue the error about the overly-qualified name now. */
16109 cp_parser_error (parser,
16110 "global qualification of class name is invalid");
16111 return error_mark_node;
16113 else if (invalid_nested_name_p)
16115 cp_parser_error (parser,
16116 "qualified name does not name a class");
16117 return error_mark_node;
16119 else if (nested_name_specifier)
16123 /* Reject typedef-names in class heads. */
16124 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16126 error_at (type_start_token->location,
16127 "invalid class name in declaration of %qD",
16133 /* Figure out in what scope the declaration is being placed. */
16134 scope = current_scope ();
16135 /* If that scope does not contain the scope in which the
16136 class was originally declared, the program is invalid. */
16137 if (scope && !is_ancestor (scope, nested_name_specifier))
16139 if (at_namespace_scope_p ())
16140 error_at (type_start_token->location,
16141 "declaration of %qD in namespace %qD which does not "
16143 type, scope, nested_name_specifier);
16145 error_at (type_start_token->location,
16146 "declaration of %qD in %qD which does not enclose %qD",
16147 type, scope, nested_name_specifier);
16153 A declarator-id shall not be qualified except for the
16154 definition of a ... nested class outside of its class
16155 ... [or] the definition or explicit instantiation of a
16156 class member of a namespace outside of its namespace. */
16157 if (scope == nested_name_specifier)
16159 permerror (nested_name_specifier_token_start->location,
16160 "extra qualification not allowed");
16161 nested_name_specifier = NULL_TREE;
16165 /* An explicit-specialization must be preceded by "template <>". If
16166 it is not, try to recover gracefully. */
16167 if (at_namespace_scope_p ()
16168 && parser->num_template_parameter_lists == 0
16171 error_at (type_start_token->location,
16172 "an explicit specialization must be preceded by %<template <>%>");
16173 invalid_explicit_specialization_p = true;
16174 /* Take the same action that would have been taken by
16175 cp_parser_explicit_specialization. */
16176 ++parser->num_template_parameter_lists;
16177 begin_specialization ();
16179 /* There must be no "return" statements between this point and the
16180 end of this function; set "type "to the correct return value and
16181 use "goto done;" to return. */
16182 /* Make sure that the right number of template parameters were
16184 if (!cp_parser_check_template_parameters (parser, num_templates,
16185 type_start_token->location,
16186 /*declarator=*/NULL))
16188 /* If something went wrong, there is no point in even trying to
16189 process the class-definition. */
16194 /* Look up the type. */
16197 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16198 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16199 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16201 error_at (type_start_token->location,
16202 "function template %qD redeclared as a class template", id);
16203 type = error_mark_node;
16207 type = TREE_TYPE (id);
16208 type = maybe_process_partial_specialization (type);
16210 if (nested_name_specifier)
16211 pushed_scope = push_scope (nested_name_specifier);
16213 else if (nested_name_specifier)
16219 template <typename T> struct S { struct T };
16220 template <typename T> struct S<T>::T { };
16222 we will get a TYPENAME_TYPE when processing the definition of
16223 `S::T'. We need to resolve it to the actual type before we
16224 try to define it. */
16225 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16227 class_type = resolve_typename_type (TREE_TYPE (type),
16228 /*only_current_p=*/false);
16229 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16230 type = TYPE_NAME (class_type);
16233 cp_parser_error (parser, "could not resolve typename type");
16234 type = error_mark_node;
16238 if (maybe_process_partial_specialization (TREE_TYPE (type))
16239 == error_mark_node)
16245 class_type = current_class_type;
16246 /* Enter the scope indicated by the nested-name-specifier. */
16247 pushed_scope = push_scope (nested_name_specifier);
16248 /* Get the canonical version of this type. */
16249 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16250 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16251 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16253 type = push_template_decl (type);
16254 if (type == error_mark_node)
16261 type = TREE_TYPE (type);
16262 *nested_name_specifier_p = true;
16264 else /* The name is not a nested name. */
16266 /* If the class was unnamed, create a dummy name. */
16268 id = make_anon_name ();
16269 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16270 parser->num_template_parameter_lists);
16273 /* Indicate whether this class was declared as a `class' or as a
16275 if (TREE_CODE (type) == RECORD_TYPE)
16276 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16277 cp_parser_check_class_key (class_key, type);
16279 /* If this type was already complete, and we see another definition,
16280 that's an error. */
16281 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16283 error_at (type_start_token->location, "redefinition of %q#T",
16285 error_at (type_start_token->location, "previous definition of %q+#T",
16290 else if (type == error_mark_node)
16293 /* We will have entered the scope containing the class; the names of
16294 base classes should be looked up in that context. For example:
16296 struct A { struct B {}; struct C; };
16297 struct A::C : B {};
16301 /* Get the list of base-classes, if there is one. */
16302 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16303 *bases = cp_parser_base_clause (parser);
16306 /* Leave the scope given by the nested-name-specifier. We will
16307 enter the class scope itself while processing the members. */
16309 pop_scope (pushed_scope);
16311 if (invalid_explicit_specialization_p)
16313 end_specialization ();
16314 --parser->num_template_parameter_lists;
16316 *attributes_p = attributes;
16320 /* Parse a class-key.
16327 Returns the kind of class-key specified, or none_type to indicate
16330 static enum tag_types
16331 cp_parser_class_key (cp_parser* parser)
16334 enum tag_types tag_type;
16336 /* Look for the class-key. */
16337 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16341 /* Check to see if the TOKEN is a class-key. */
16342 tag_type = cp_parser_token_is_class_key (token);
16344 cp_parser_error (parser, "expected class-key");
16348 /* Parse an (optional) member-specification.
16350 member-specification:
16351 member-declaration member-specification [opt]
16352 access-specifier : member-specification [opt] */
16355 cp_parser_member_specification_opt (cp_parser* parser)
16362 /* Peek at the next token. */
16363 token = cp_lexer_peek_token (parser->lexer);
16364 /* If it's a `}', or EOF then we've seen all the members. */
16365 if (token->type == CPP_CLOSE_BRACE
16366 || token->type == CPP_EOF
16367 || token->type == CPP_PRAGMA_EOL)
16370 /* See if this token is a keyword. */
16371 keyword = token->keyword;
16375 case RID_PROTECTED:
16377 /* Consume the access-specifier. */
16378 cp_lexer_consume_token (parser->lexer);
16379 /* Remember which access-specifier is active. */
16380 current_access_specifier = token->u.value;
16381 /* Look for the `:'. */
16382 cp_parser_require (parser, CPP_COLON, "%<:%>");
16386 /* Accept #pragmas at class scope. */
16387 if (token->type == CPP_PRAGMA)
16389 cp_parser_pragma (parser, pragma_external);
16393 /* Otherwise, the next construction must be a
16394 member-declaration. */
16395 cp_parser_member_declaration (parser);
16400 /* Parse a member-declaration.
16402 member-declaration:
16403 decl-specifier-seq [opt] member-declarator-list [opt] ;
16404 function-definition ; [opt]
16405 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16407 template-declaration
16409 member-declarator-list:
16411 member-declarator-list , member-declarator
16414 declarator pure-specifier [opt]
16415 declarator constant-initializer [opt]
16416 identifier [opt] : constant-expression
16420 member-declaration:
16421 __extension__ member-declaration
16424 declarator attributes [opt] pure-specifier [opt]
16425 declarator attributes [opt] constant-initializer [opt]
16426 identifier [opt] attributes [opt] : constant-expression
16430 member-declaration:
16431 static_assert-declaration */
16434 cp_parser_member_declaration (cp_parser* parser)
16436 cp_decl_specifier_seq decl_specifiers;
16437 tree prefix_attributes;
16439 int declares_class_or_enum;
16441 cp_token *token = NULL;
16442 cp_token *decl_spec_token_start = NULL;
16443 cp_token *initializer_token_start = NULL;
16444 int saved_pedantic;
16446 /* Check for the `__extension__' keyword. */
16447 if (cp_parser_extension_opt (parser, &saved_pedantic))
16450 cp_parser_member_declaration (parser);
16451 /* Restore the old value of the PEDANTIC flag. */
16452 pedantic = saved_pedantic;
16457 /* Check for a template-declaration. */
16458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16460 /* An explicit specialization here is an error condition, and we
16461 expect the specialization handler to detect and report this. */
16462 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16463 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16464 cp_parser_explicit_specialization (parser);
16466 cp_parser_template_declaration (parser, /*member_p=*/true);
16471 /* Check for a using-declaration. */
16472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16474 /* Parse the using-declaration. */
16475 cp_parser_using_declaration (parser,
16476 /*access_declaration_p=*/false);
16480 /* Check for @defs. */
16481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16484 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16485 ivar = ivar_chains;
16489 ivar = TREE_CHAIN (member);
16490 TREE_CHAIN (member) = NULL_TREE;
16491 finish_member_declaration (member);
16496 /* If the next token is `static_assert' we have a static assertion. */
16497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16499 cp_parser_static_assert (parser, /*member_p=*/true);
16503 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16506 /* Parse the decl-specifier-seq. */
16507 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16508 cp_parser_decl_specifier_seq (parser,
16509 CP_PARSER_FLAGS_OPTIONAL,
16511 &declares_class_or_enum);
16512 prefix_attributes = decl_specifiers.attributes;
16513 decl_specifiers.attributes = NULL_TREE;
16514 /* Check for an invalid type-name. */
16515 if (!decl_specifiers.type
16516 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16518 /* If there is no declarator, then the decl-specifier-seq should
16520 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16522 /* If there was no decl-specifier-seq, and the next token is a
16523 `;', then we have something like:
16529 Each member-declaration shall declare at least one member
16530 name of the class. */
16531 if (!decl_specifiers.any_specifiers_p)
16533 cp_token *token = cp_lexer_peek_token (parser->lexer);
16534 if (!in_system_header_at (token->location))
16535 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16541 /* See if this declaration is a friend. */
16542 friend_p = cp_parser_friend_p (&decl_specifiers);
16543 /* If there were decl-specifiers, check to see if there was
16544 a class-declaration. */
16545 type = check_tag_decl (&decl_specifiers);
16546 /* Nested classes have already been added to the class, but
16547 a `friend' needs to be explicitly registered. */
16550 /* If the `friend' keyword was present, the friend must
16551 be introduced with a class-key. */
16552 if (!declares_class_or_enum)
16553 error_at (decl_spec_token_start->location,
16554 "a class-key must be used when declaring a friend");
16557 template <typename T> struct A {
16558 friend struct A<T>::B;
16561 A<T>::B will be represented by a TYPENAME_TYPE, and
16562 therefore not recognized by check_tag_decl. */
16564 && decl_specifiers.type
16565 && TYPE_P (decl_specifiers.type))
16566 type = decl_specifiers.type;
16567 if (!type || !TYPE_P (type))
16568 error_at (decl_spec_token_start->location,
16569 "friend declaration does not name a class or "
16572 make_friend_class (current_class_type, type,
16573 /*complain=*/true);
16575 /* If there is no TYPE, an error message will already have
16577 else if (!type || type == error_mark_node)
16579 /* An anonymous aggregate has to be handled specially; such
16580 a declaration really declares a data member (with a
16581 particular type), as opposed to a nested class. */
16582 else if (ANON_AGGR_TYPE_P (type))
16584 /* Remove constructors and such from TYPE, now that we
16585 know it is an anonymous aggregate. */
16586 fixup_anonymous_aggr (type);
16587 /* And make the corresponding data member. */
16588 decl = build_decl (decl_spec_token_start->location,
16589 FIELD_DECL, NULL_TREE, type);
16590 /* Add it to the class. */
16591 finish_member_declaration (decl);
16594 cp_parser_check_access_in_redeclaration
16596 decl_spec_token_start->location);
16601 /* See if these declarations will be friends. */
16602 friend_p = cp_parser_friend_p (&decl_specifiers);
16604 /* Keep going until we hit the `;' at the end of the
16606 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16608 tree attributes = NULL_TREE;
16609 tree first_attribute;
16611 /* Peek at the next token. */
16612 token = cp_lexer_peek_token (parser->lexer);
16614 /* Check for a bitfield declaration. */
16615 if (token->type == CPP_COLON
16616 || (token->type == CPP_NAME
16617 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16623 /* Get the name of the bitfield. Note that we cannot just
16624 check TOKEN here because it may have been invalidated by
16625 the call to cp_lexer_peek_nth_token above. */
16626 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16627 identifier = cp_parser_identifier (parser);
16629 identifier = NULL_TREE;
16631 /* Consume the `:' token. */
16632 cp_lexer_consume_token (parser->lexer);
16633 /* Get the width of the bitfield. */
16635 = cp_parser_constant_expression (parser,
16636 /*allow_non_constant=*/false,
16639 /* Look for attributes that apply to the bitfield. */
16640 attributes = cp_parser_attributes_opt (parser);
16641 /* Remember which attributes are prefix attributes and
16643 first_attribute = attributes;
16644 /* Combine the attributes. */
16645 attributes = chainon (prefix_attributes, attributes);
16647 /* Create the bitfield declaration. */
16648 decl = grokbitfield (identifier
16649 ? make_id_declarator (NULL_TREE,
16659 cp_declarator *declarator;
16661 tree asm_specification;
16662 int ctor_dtor_or_conv_p;
16664 /* Parse the declarator. */
16666 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16667 &ctor_dtor_or_conv_p,
16668 /*parenthesized_p=*/NULL,
16669 /*member_p=*/true);
16671 /* If something went wrong parsing the declarator, make sure
16672 that we at least consume some tokens. */
16673 if (declarator == cp_error_declarator)
16675 /* Skip to the end of the statement. */
16676 cp_parser_skip_to_end_of_statement (parser);
16677 /* If the next token is not a semicolon, that is
16678 probably because we just skipped over the body of
16679 a function. So, we consume a semicolon if
16680 present, but do not issue an error message if it
16682 if (cp_lexer_next_token_is (parser->lexer,
16684 cp_lexer_consume_token (parser->lexer);
16688 if (declares_class_or_enum & 2)
16689 cp_parser_check_for_definition_in_return_type
16690 (declarator, decl_specifiers.type,
16691 decl_specifiers.type_location);
16693 /* Look for an asm-specification. */
16694 asm_specification = cp_parser_asm_specification_opt (parser);
16695 /* Look for attributes that apply to the declaration. */
16696 attributes = cp_parser_attributes_opt (parser);
16697 /* Remember which attributes are prefix attributes and
16699 first_attribute = attributes;
16700 /* Combine the attributes. */
16701 attributes = chainon (prefix_attributes, attributes);
16703 /* If it's an `=', then we have a constant-initializer or a
16704 pure-specifier. It is not correct to parse the
16705 initializer before registering the member declaration
16706 since the member declaration should be in scope while
16707 its initializer is processed. However, the rest of the
16708 front end does not yet provide an interface that allows
16709 us to handle this correctly. */
16710 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16714 A pure-specifier shall be used only in the declaration of
16715 a virtual function.
16717 A member-declarator can contain a constant-initializer
16718 only if it declares a static member of integral or
16721 Therefore, if the DECLARATOR is for a function, we look
16722 for a pure-specifier; otherwise, we look for a
16723 constant-initializer. When we call `grokfield', it will
16724 perform more stringent semantics checks. */
16725 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16726 if (function_declarator_p (declarator))
16727 initializer = cp_parser_pure_specifier (parser);
16729 /* Parse the initializer. */
16730 initializer = cp_parser_constant_initializer (parser);
16732 /* Otherwise, there is no initializer. */
16734 initializer = NULL_TREE;
16736 /* See if we are probably looking at a function
16737 definition. We are certainly not looking at a
16738 member-declarator. Calling `grokfield' has
16739 side-effects, so we must not do it unless we are sure
16740 that we are looking at a member-declarator. */
16741 if (cp_parser_token_starts_function_definition_p
16742 (cp_lexer_peek_token (parser->lexer)))
16744 /* The grammar does not allow a pure-specifier to be
16745 used when a member function is defined. (It is
16746 possible that this fact is an oversight in the
16747 standard, since a pure function may be defined
16748 outside of the class-specifier. */
16750 error_at (initializer_token_start->location,
16751 "pure-specifier on function-definition");
16752 decl = cp_parser_save_member_function_body (parser,
16756 /* If the member was not a friend, declare it here. */
16758 finish_member_declaration (decl);
16759 /* Peek at the next token. */
16760 token = cp_lexer_peek_token (parser->lexer);
16761 /* If the next token is a semicolon, consume it. */
16762 if (token->type == CPP_SEMICOLON)
16763 cp_lexer_consume_token (parser->lexer);
16767 if (declarator->kind == cdk_function)
16768 declarator->id_loc = token->location;
16769 /* Create the declaration. */
16770 decl = grokfield (declarator, &decl_specifiers,
16771 initializer, /*init_const_expr_p=*/true,
16776 /* Reset PREFIX_ATTRIBUTES. */
16777 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16778 attributes = TREE_CHAIN (attributes);
16780 TREE_CHAIN (attributes) = NULL_TREE;
16782 /* If there is any qualification still in effect, clear it
16783 now; we will be starting fresh with the next declarator. */
16784 parser->scope = NULL_TREE;
16785 parser->qualifying_scope = NULL_TREE;
16786 parser->object_scope = NULL_TREE;
16787 /* If it's a `,', then there are more declarators. */
16788 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16789 cp_lexer_consume_token (parser->lexer);
16790 /* If the next token isn't a `;', then we have a parse error. */
16791 else if (cp_lexer_next_token_is_not (parser->lexer,
16794 cp_parser_error (parser, "expected %<;%>");
16795 /* Skip tokens until we find a `;'. */
16796 cp_parser_skip_to_end_of_statement (parser);
16803 /* Add DECL to the list of members. */
16805 finish_member_declaration (decl);
16807 if (TREE_CODE (decl) == FUNCTION_DECL)
16808 cp_parser_save_default_args (parser, decl);
16813 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16816 /* Parse a pure-specifier.
16821 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16822 Otherwise, ERROR_MARK_NODE is returned. */
16825 cp_parser_pure_specifier (cp_parser* parser)
16829 /* Look for the `=' token. */
16830 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16831 return error_mark_node;
16832 /* Look for the `0' token. */
16833 token = cp_lexer_peek_token (parser->lexer);
16835 if (token->type == CPP_EOF
16836 || token->type == CPP_PRAGMA_EOL)
16837 return error_mark_node;
16839 cp_lexer_consume_token (parser->lexer);
16841 /* Accept = default or = delete in c++0x mode. */
16842 if (token->keyword == RID_DEFAULT
16843 || token->keyword == RID_DELETE)
16845 maybe_warn_cpp0x ("defaulted and deleted functions");
16846 return token->u.value;
16849 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16850 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16852 cp_parser_error (parser,
16853 "invalid pure specifier (only %<= 0%> is allowed)");
16854 cp_parser_skip_to_end_of_statement (parser);
16855 return error_mark_node;
16857 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16859 error_at (token->location, "templates may not be %<virtual%>");
16860 return error_mark_node;
16863 return integer_zero_node;
16866 /* Parse a constant-initializer.
16868 constant-initializer:
16869 = constant-expression
16871 Returns a representation of the constant-expression. */
16874 cp_parser_constant_initializer (cp_parser* parser)
16876 /* Look for the `=' token. */
16877 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16878 return error_mark_node;
16880 /* It is invalid to write:
16882 struct S { static const int i = { 7 }; };
16885 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16887 cp_parser_error (parser,
16888 "a brace-enclosed initializer is not allowed here");
16889 /* Consume the opening brace. */
16890 cp_lexer_consume_token (parser->lexer);
16891 /* Skip the initializer. */
16892 cp_parser_skip_to_closing_brace (parser);
16893 /* Look for the trailing `}'. */
16894 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16896 return error_mark_node;
16899 return cp_parser_constant_expression (parser,
16900 /*allow_non_constant=*/false,
16904 /* Derived classes [gram.class.derived] */
16906 /* Parse a base-clause.
16909 : base-specifier-list
16911 base-specifier-list:
16912 base-specifier ... [opt]
16913 base-specifier-list , base-specifier ... [opt]
16915 Returns a TREE_LIST representing the base-classes, in the order in
16916 which they were declared. The representation of each node is as
16917 described by cp_parser_base_specifier.
16919 In the case that no bases are specified, this function will return
16920 NULL_TREE, not ERROR_MARK_NODE. */
16923 cp_parser_base_clause (cp_parser* parser)
16925 tree bases = NULL_TREE;
16927 /* Look for the `:' that begins the list. */
16928 cp_parser_require (parser, CPP_COLON, "%<:%>");
16930 /* Scan the base-specifier-list. */
16935 bool pack_expansion_p = false;
16937 /* Look for the base-specifier. */
16938 base = cp_parser_base_specifier (parser);
16939 /* Look for the (optional) ellipsis. */
16940 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16942 /* Consume the `...'. */
16943 cp_lexer_consume_token (parser->lexer);
16945 pack_expansion_p = true;
16948 /* Add BASE to the front of the list. */
16949 if (base != error_mark_node)
16951 if (pack_expansion_p)
16952 /* Make this a pack expansion type. */
16953 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16956 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16958 TREE_CHAIN (base) = bases;
16962 /* Peek at the next token. */
16963 token = cp_lexer_peek_token (parser->lexer);
16964 /* If it's not a comma, then the list is complete. */
16965 if (token->type != CPP_COMMA)
16967 /* Consume the `,'. */
16968 cp_lexer_consume_token (parser->lexer);
16971 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16972 base class had a qualified name. However, the next name that
16973 appears is certainly not qualified. */
16974 parser->scope = NULL_TREE;
16975 parser->qualifying_scope = NULL_TREE;
16976 parser->object_scope = NULL_TREE;
16978 return nreverse (bases);
16981 /* Parse a base-specifier.
16984 :: [opt] nested-name-specifier [opt] class-name
16985 virtual access-specifier [opt] :: [opt] nested-name-specifier
16987 access-specifier virtual [opt] :: [opt] nested-name-specifier
16990 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16991 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16992 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16993 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16996 cp_parser_base_specifier (cp_parser* parser)
17000 bool virtual_p = false;
17001 bool duplicate_virtual_error_issued_p = false;
17002 bool duplicate_access_error_issued_p = false;
17003 bool class_scope_p, template_p;
17004 tree access = access_default_node;
17007 /* Process the optional `virtual' and `access-specifier'. */
17010 /* Peek at the next token. */
17011 token = cp_lexer_peek_token (parser->lexer);
17012 /* Process `virtual'. */
17013 switch (token->keyword)
17016 /* If `virtual' appears more than once, issue an error. */
17017 if (virtual_p && !duplicate_virtual_error_issued_p)
17019 cp_parser_error (parser,
17020 "%<virtual%> specified more than once in base-specified");
17021 duplicate_virtual_error_issued_p = true;
17026 /* Consume the `virtual' token. */
17027 cp_lexer_consume_token (parser->lexer);
17032 case RID_PROTECTED:
17034 /* If more than one access specifier appears, issue an
17036 if (access != access_default_node
17037 && !duplicate_access_error_issued_p)
17039 cp_parser_error (parser,
17040 "more than one access specifier in base-specified");
17041 duplicate_access_error_issued_p = true;
17044 access = ridpointers[(int) token->keyword];
17046 /* Consume the access-specifier. */
17047 cp_lexer_consume_token (parser->lexer);
17056 /* It is not uncommon to see programs mechanically, erroneously, use
17057 the 'typename' keyword to denote (dependent) qualified types
17058 as base classes. */
17059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17061 token = cp_lexer_peek_token (parser->lexer);
17062 if (!processing_template_decl)
17063 error_at (token->location,
17064 "keyword %<typename%> not allowed outside of templates");
17066 error_at (token->location,
17067 "keyword %<typename%> not allowed in this context "
17068 "(the base class is implicitly a type)");
17069 cp_lexer_consume_token (parser->lexer);
17072 /* Look for the optional `::' operator. */
17073 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17074 /* Look for the nested-name-specifier. The simplest way to
17079 The keyword `typename' is not permitted in a base-specifier or
17080 mem-initializer; in these contexts a qualified name that
17081 depends on a template-parameter is implicitly assumed to be a
17084 is to pretend that we have seen the `typename' keyword at this
17086 cp_parser_nested_name_specifier_opt (parser,
17087 /*typename_keyword_p=*/true,
17088 /*check_dependency_p=*/true,
17090 /*is_declaration=*/true);
17091 /* If the base class is given by a qualified name, assume that names
17092 we see are type names or templates, as appropriate. */
17093 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17094 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17096 /* Finally, look for the class-name. */
17097 type = cp_parser_class_name (parser,
17101 /*check_dependency_p=*/true,
17102 /*class_head_p=*/false,
17103 /*is_declaration=*/true);
17105 if (type == error_mark_node)
17106 return error_mark_node;
17108 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17111 /* Exception handling [gram.exception] */
17113 /* Parse an (optional) exception-specification.
17115 exception-specification:
17116 throw ( type-id-list [opt] )
17118 Returns a TREE_LIST representing the exception-specification. The
17119 TREE_VALUE of each node is a type. */
17122 cp_parser_exception_specification_opt (cp_parser* parser)
17127 /* Peek at the next token. */
17128 token = cp_lexer_peek_token (parser->lexer);
17129 /* If it's not `throw', then there's no exception-specification. */
17130 if (!cp_parser_is_keyword (token, RID_THROW))
17133 /* Consume the `throw'. */
17134 cp_lexer_consume_token (parser->lexer);
17136 /* Look for the `('. */
17137 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17139 /* Peek at the next token. */
17140 token = cp_lexer_peek_token (parser->lexer);
17141 /* If it's not a `)', then there is a type-id-list. */
17142 if (token->type != CPP_CLOSE_PAREN)
17144 const char *saved_message;
17146 /* Types may not be defined in an exception-specification. */
17147 saved_message = parser->type_definition_forbidden_message;
17148 parser->type_definition_forbidden_message
17149 = "types may not be defined in an exception-specification";
17150 /* Parse the type-id-list. */
17151 type_id_list = cp_parser_type_id_list (parser);
17152 /* Restore the saved message. */
17153 parser->type_definition_forbidden_message = saved_message;
17156 type_id_list = empty_except_spec;
17158 /* Look for the `)'. */
17159 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17161 return type_id_list;
17164 /* Parse an (optional) type-id-list.
17168 type-id-list , type-id ... [opt]
17170 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17171 in the order that the types were presented. */
17174 cp_parser_type_id_list (cp_parser* parser)
17176 tree types = NULL_TREE;
17183 /* Get the next type-id. */
17184 type = cp_parser_type_id (parser);
17185 /* Parse the optional ellipsis. */
17186 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17188 /* Consume the `...'. */
17189 cp_lexer_consume_token (parser->lexer);
17191 /* Turn the type into a pack expansion expression. */
17192 type = make_pack_expansion (type);
17194 /* Add it to the list. */
17195 types = add_exception_specifier (types, type, /*complain=*/1);
17196 /* Peek at the next token. */
17197 token = cp_lexer_peek_token (parser->lexer);
17198 /* If it is not a `,', we are done. */
17199 if (token->type != CPP_COMMA)
17201 /* Consume the `,'. */
17202 cp_lexer_consume_token (parser->lexer);
17205 return nreverse (types);
17208 /* Parse a try-block.
17211 try compound-statement handler-seq */
17214 cp_parser_try_block (cp_parser* parser)
17218 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17219 try_block = begin_try_block ();
17220 cp_parser_compound_statement (parser, NULL, true);
17221 finish_try_block (try_block);
17222 cp_parser_handler_seq (parser);
17223 finish_handler_sequence (try_block);
17228 /* Parse a function-try-block.
17230 function-try-block:
17231 try ctor-initializer [opt] function-body handler-seq */
17234 cp_parser_function_try_block (cp_parser* parser)
17236 tree compound_stmt;
17238 bool ctor_initializer_p;
17240 /* Look for the `try' keyword. */
17241 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17243 /* Let the rest of the front end know where we are. */
17244 try_block = begin_function_try_block (&compound_stmt);
17245 /* Parse the function-body. */
17247 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17248 /* We're done with the `try' part. */
17249 finish_function_try_block (try_block);
17250 /* Parse the handlers. */
17251 cp_parser_handler_seq (parser);
17252 /* We're done with the handlers. */
17253 finish_function_handler_sequence (try_block, compound_stmt);
17255 return ctor_initializer_p;
17258 /* Parse a handler-seq.
17261 handler handler-seq [opt] */
17264 cp_parser_handler_seq (cp_parser* parser)
17270 /* Parse the handler. */
17271 cp_parser_handler (parser);
17272 /* Peek at the next token. */
17273 token = cp_lexer_peek_token (parser->lexer);
17274 /* If it's not `catch' then there are no more handlers. */
17275 if (!cp_parser_is_keyword (token, RID_CATCH))
17280 /* Parse a handler.
17283 catch ( exception-declaration ) compound-statement */
17286 cp_parser_handler (cp_parser* parser)
17291 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17292 handler = begin_handler ();
17293 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17294 declaration = cp_parser_exception_declaration (parser);
17295 finish_handler_parms (declaration, handler);
17296 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17297 cp_parser_compound_statement (parser, NULL, false);
17298 finish_handler (handler);
17301 /* Parse an exception-declaration.
17303 exception-declaration:
17304 type-specifier-seq declarator
17305 type-specifier-seq abstract-declarator
17309 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17310 ellipsis variant is used. */
17313 cp_parser_exception_declaration (cp_parser* parser)
17315 cp_decl_specifier_seq type_specifiers;
17316 cp_declarator *declarator;
17317 const char *saved_message;
17319 /* If it's an ellipsis, it's easy to handle. */
17320 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17322 /* Consume the `...' token. */
17323 cp_lexer_consume_token (parser->lexer);
17327 /* Types may not be defined in exception-declarations. */
17328 saved_message = parser->type_definition_forbidden_message;
17329 parser->type_definition_forbidden_message
17330 = "types may not be defined in exception-declarations";
17332 /* Parse the type-specifier-seq. */
17333 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17334 /*is_trailing_return=*/false,
17336 /* If it's a `)', then there is no declarator. */
17337 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17340 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17341 /*ctor_dtor_or_conv_p=*/NULL,
17342 /*parenthesized_p=*/NULL,
17343 /*member_p=*/false);
17345 /* Restore the saved message. */
17346 parser->type_definition_forbidden_message = saved_message;
17348 if (!type_specifiers.any_specifiers_p)
17349 return error_mark_node;
17351 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17354 /* Parse a throw-expression.
17357 throw assignment-expression [opt]
17359 Returns a THROW_EXPR representing the throw-expression. */
17362 cp_parser_throw_expression (cp_parser* parser)
17367 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17368 token = cp_lexer_peek_token (parser->lexer);
17369 /* Figure out whether or not there is an assignment-expression
17370 following the "throw" keyword. */
17371 if (token->type == CPP_COMMA
17372 || token->type == CPP_SEMICOLON
17373 || token->type == CPP_CLOSE_PAREN
17374 || token->type == CPP_CLOSE_SQUARE
17375 || token->type == CPP_CLOSE_BRACE
17376 || token->type == CPP_COLON)
17377 expression = NULL_TREE;
17379 expression = cp_parser_assignment_expression (parser,
17380 /*cast_p=*/false, NULL);
17382 return build_throw (expression);
17385 /* GNU Extensions */
17387 /* Parse an (optional) asm-specification.
17390 asm ( string-literal )
17392 If the asm-specification is present, returns a STRING_CST
17393 corresponding to the string-literal. Otherwise, returns
17397 cp_parser_asm_specification_opt (cp_parser* parser)
17400 tree asm_specification;
17402 /* Peek at the next token. */
17403 token = cp_lexer_peek_token (parser->lexer);
17404 /* If the next token isn't the `asm' keyword, then there's no
17405 asm-specification. */
17406 if (!cp_parser_is_keyword (token, RID_ASM))
17409 /* Consume the `asm' token. */
17410 cp_lexer_consume_token (parser->lexer);
17411 /* Look for the `('. */
17412 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17414 /* Look for the string-literal. */
17415 asm_specification = cp_parser_string_literal (parser, false, false);
17417 /* Look for the `)'. */
17418 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17420 return asm_specification;
17423 /* Parse an asm-operand-list.
17427 asm-operand-list , asm-operand
17430 string-literal ( expression )
17431 [ string-literal ] string-literal ( expression )
17433 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17434 each node is the expression. The TREE_PURPOSE is itself a
17435 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17436 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17437 is a STRING_CST for the string literal before the parenthesis. Returns
17438 ERROR_MARK_NODE if any of the operands are invalid. */
17441 cp_parser_asm_operand_list (cp_parser* parser)
17443 tree asm_operands = NULL_TREE;
17444 bool invalid_operands = false;
17448 tree string_literal;
17452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17454 /* Consume the `[' token. */
17455 cp_lexer_consume_token (parser->lexer);
17456 /* Read the operand name. */
17457 name = cp_parser_identifier (parser);
17458 if (name != error_mark_node)
17459 name = build_string (IDENTIFIER_LENGTH (name),
17460 IDENTIFIER_POINTER (name));
17461 /* Look for the closing `]'. */
17462 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17466 /* Look for the string-literal. */
17467 string_literal = cp_parser_string_literal (parser, false, false);
17469 /* Look for the `('. */
17470 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17471 /* Parse the expression. */
17472 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17473 /* Look for the `)'. */
17474 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17476 if (name == error_mark_node
17477 || string_literal == error_mark_node
17478 || expression == error_mark_node)
17479 invalid_operands = true;
17481 /* Add this operand to the list. */
17482 asm_operands = tree_cons (build_tree_list (name, string_literal),
17485 /* If the next token is not a `,', there are no more
17487 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17489 /* Consume the `,'. */
17490 cp_lexer_consume_token (parser->lexer);
17493 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17496 /* Parse an asm-clobber-list.
17500 asm-clobber-list , string-literal
17502 Returns a TREE_LIST, indicating the clobbers in the order that they
17503 appeared. The TREE_VALUE of each node is a STRING_CST. */
17506 cp_parser_asm_clobber_list (cp_parser* parser)
17508 tree clobbers = NULL_TREE;
17512 tree string_literal;
17514 /* Look for the string literal. */
17515 string_literal = cp_parser_string_literal (parser, false, false);
17516 /* Add it to the list. */
17517 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17518 /* If the next token is not a `,', then the list is
17520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17522 /* Consume the `,' token. */
17523 cp_lexer_consume_token (parser->lexer);
17529 /* Parse an asm-label-list.
17533 asm-label-list , identifier
17535 Returns a TREE_LIST, indicating the labels in the order that they
17536 appeared. The TREE_VALUE of each node is a label. */
17539 cp_parser_asm_label_list (cp_parser* parser)
17541 tree labels = NULL_TREE;
17545 tree identifier, label, name;
17547 /* Look for the identifier. */
17548 identifier = cp_parser_identifier (parser);
17549 if (!error_operand_p (identifier))
17551 label = lookup_label (identifier);
17552 if (TREE_CODE (label) == LABEL_DECL)
17554 TREE_USED (label) = 1;
17555 check_goto (label);
17556 name = build_string (IDENTIFIER_LENGTH (identifier),
17557 IDENTIFIER_POINTER (identifier));
17558 labels = tree_cons (name, label, labels);
17561 /* If the next token is not a `,', then the list is
17563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17565 /* Consume the `,' token. */
17566 cp_lexer_consume_token (parser->lexer);
17569 return nreverse (labels);
17572 /* Parse an (optional) series of attributes.
17575 attributes attribute
17578 __attribute__ (( attribute-list [opt] ))
17580 The return value is as for cp_parser_attribute_list. */
17583 cp_parser_attributes_opt (cp_parser* parser)
17585 tree attributes = NULL_TREE;
17590 tree attribute_list;
17592 /* Peek at the next token. */
17593 token = cp_lexer_peek_token (parser->lexer);
17594 /* If it's not `__attribute__', then we're done. */
17595 if (token->keyword != RID_ATTRIBUTE)
17598 /* Consume the `__attribute__' keyword. */
17599 cp_lexer_consume_token (parser->lexer);
17600 /* Look for the two `(' tokens. */
17601 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17602 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17604 /* Peek at the next token. */
17605 token = cp_lexer_peek_token (parser->lexer);
17606 if (token->type != CPP_CLOSE_PAREN)
17607 /* Parse the attribute-list. */
17608 attribute_list = cp_parser_attribute_list (parser);
17610 /* If the next token is a `)', then there is no attribute
17612 attribute_list = NULL;
17614 /* Look for the two `)' tokens. */
17615 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17616 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17618 /* Add these new attributes to the list. */
17619 attributes = chainon (attributes, attribute_list);
17625 /* Parse an attribute-list.
17629 attribute-list , attribute
17633 identifier ( identifier )
17634 identifier ( identifier , expression-list )
17635 identifier ( expression-list )
17637 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17638 to an attribute. The TREE_PURPOSE of each node is the identifier
17639 indicating which attribute is in use. The TREE_VALUE represents
17640 the arguments, if any. */
17643 cp_parser_attribute_list (cp_parser* parser)
17645 tree attribute_list = NULL_TREE;
17646 bool save_translate_strings_p = parser->translate_strings_p;
17648 parser->translate_strings_p = false;
17655 /* Look for the identifier. We also allow keywords here; for
17656 example `__attribute__ ((const))' is legal. */
17657 token = cp_lexer_peek_token (parser->lexer);
17658 if (token->type == CPP_NAME
17659 || token->type == CPP_KEYWORD)
17661 tree arguments = NULL_TREE;
17663 /* Consume the token. */
17664 token = cp_lexer_consume_token (parser->lexer);
17666 /* Save away the identifier that indicates which attribute
17668 identifier = (token->type == CPP_KEYWORD)
17669 /* For keywords, use the canonical spelling, not the
17670 parsed identifier. */
17671 ? ridpointers[(int) token->keyword]
17674 attribute = build_tree_list (identifier, NULL_TREE);
17676 /* Peek at the next token. */
17677 token = cp_lexer_peek_token (parser->lexer);
17678 /* If it's an `(', then parse the attribute arguments. */
17679 if (token->type == CPP_OPEN_PAREN)
17682 vec = cp_parser_parenthesized_expression_list
17683 (parser, true, /*cast_p=*/false,
17684 /*allow_expansion_p=*/false,
17685 /*non_constant_p=*/NULL);
17687 arguments = error_mark_node;
17690 arguments = build_tree_list_vec (vec);
17691 release_tree_vector (vec);
17693 /* Save the arguments away. */
17694 TREE_VALUE (attribute) = arguments;
17697 if (arguments != error_mark_node)
17699 /* Add this attribute to the list. */
17700 TREE_CHAIN (attribute) = attribute_list;
17701 attribute_list = attribute;
17704 token = cp_lexer_peek_token (parser->lexer);
17706 /* Now, look for more attributes. If the next token isn't a
17707 `,', we're done. */
17708 if (token->type != CPP_COMMA)
17711 /* Consume the comma and keep going. */
17712 cp_lexer_consume_token (parser->lexer);
17714 parser->translate_strings_p = save_translate_strings_p;
17716 /* We built up the list in reverse order. */
17717 return nreverse (attribute_list);
17720 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17721 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17722 current value of the PEDANTIC flag, regardless of whether or not
17723 the `__extension__' keyword is present. The caller is responsible
17724 for restoring the value of the PEDANTIC flag. */
17727 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17729 /* Save the old value of the PEDANTIC flag. */
17730 *saved_pedantic = pedantic;
17732 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17734 /* Consume the `__extension__' token. */
17735 cp_lexer_consume_token (parser->lexer);
17736 /* We're not being pedantic while the `__extension__' keyword is
17746 /* Parse a label declaration.
17749 __label__ label-declarator-seq ;
17751 label-declarator-seq:
17752 identifier , label-declarator-seq
17756 cp_parser_label_declaration (cp_parser* parser)
17758 /* Look for the `__label__' keyword. */
17759 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17765 /* Look for an identifier. */
17766 identifier = cp_parser_identifier (parser);
17767 /* If we failed, stop. */
17768 if (identifier == error_mark_node)
17770 /* Declare it as a label. */
17771 finish_label_decl (identifier);
17772 /* If the next token is a `;', stop. */
17773 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17775 /* Look for the `,' separating the label declarations. */
17776 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17779 /* Look for the final `;'. */
17780 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17783 /* Support Functions */
17785 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17786 NAME should have one of the representations used for an
17787 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17788 is returned. If PARSER->SCOPE is a dependent type, then a
17789 SCOPE_REF is returned.
17791 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17792 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17793 was formed. Abstractly, such entities should not be passed to this
17794 function, because they do not need to be looked up, but it is
17795 simpler to check for this special case here, rather than at the
17798 In cases not explicitly covered above, this function returns a
17799 DECL, OVERLOAD, or baselink representing the result of the lookup.
17800 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17803 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17804 (e.g., "struct") that was used. In that case bindings that do not
17805 refer to types are ignored.
17807 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17810 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17813 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17816 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17817 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17818 NULL_TREE otherwise. */
17821 cp_parser_lookup_name (cp_parser *parser, tree name,
17822 enum tag_types tag_type,
17825 bool check_dependency,
17826 tree *ambiguous_decls,
17827 location_t name_location)
17831 tree object_type = parser->context->object_type;
17833 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17834 flags |= LOOKUP_COMPLAIN;
17836 /* Assume that the lookup will be unambiguous. */
17837 if (ambiguous_decls)
17838 *ambiguous_decls = NULL_TREE;
17840 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17841 no longer valid. Note that if we are parsing tentatively, and
17842 the parse fails, OBJECT_TYPE will be automatically restored. */
17843 parser->context->object_type = NULL_TREE;
17845 if (name == error_mark_node)
17846 return error_mark_node;
17848 /* A template-id has already been resolved; there is no lookup to
17850 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17852 if (BASELINK_P (name))
17854 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17855 == TEMPLATE_ID_EXPR);
17859 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17860 it should already have been checked to make sure that the name
17861 used matches the type being destroyed. */
17862 if (TREE_CODE (name) == BIT_NOT_EXPR)
17866 /* Figure out to which type this destructor applies. */
17868 type = parser->scope;
17869 else if (object_type)
17870 type = object_type;
17872 type = current_class_type;
17873 /* If that's not a class type, there is no destructor. */
17874 if (!type || !CLASS_TYPE_P (type))
17875 return error_mark_node;
17876 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17877 lazily_declare_fn (sfk_destructor, type);
17878 if (!CLASSTYPE_DESTRUCTORS (type))
17879 return error_mark_node;
17880 /* If it was a class type, return the destructor. */
17881 return CLASSTYPE_DESTRUCTORS (type);
17884 /* By this point, the NAME should be an ordinary identifier. If
17885 the id-expression was a qualified name, the qualifying scope is
17886 stored in PARSER->SCOPE at this point. */
17887 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17889 /* Perform the lookup. */
17894 if (parser->scope == error_mark_node)
17895 return error_mark_node;
17897 /* If the SCOPE is dependent, the lookup must be deferred until
17898 the template is instantiated -- unless we are explicitly
17899 looking up names in uninstantiated templates. Even then, we
17900 cannot look up the name if the scope is not a class type; it
17901 might, for example, be a template type parameter. */
17902 dependent_p = (TYPE_P (parser->scope)
17903 && dependent_scope_p (parser->scope));
17904 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17906 /* Defer lookup. */
17907 decl = error_mark_node;
17910 tree pushed_scope = NULL_TREE;
17912 /* If PARSER->SCOPE is a dependent type, then it must be a
17913 class type, and we must not be checking dependencies;
17914 otherwise, we would have processed this lookup above. So
17915 that PARSER->SCOPE is not considered a dependent base by
17916 lookup_member, we must enter the scope here. */
17918 pushed_scope = push_scope (parser->scope);
17919 /* If the PARSER->SCOPE is a template specialization, it
17920 may be instantiated during name lookup. In that case,
17921 errors may be issued. Even if we rollback the current
17922 tentative parse, those errors are valid. */
17923 decl = lookup_qualified_name (parser->scope, name,
17924 tag_type != none_type,
17925 /*complain=*/true);
17927 /* If we have a single function from a using decl, pull it out. */
17928 if (TREE_CODE (decl) == OVERLOAD
17929 && !really_overloaded_fn (decl))
17930 decl = OVL_FUNCTION (decl);
17933 pop_scope (pushed_scope);
17936 /* If the scope is a dependent type and either we deferred lookup or
17937 we did lookup but didn't find the name, rememeber the name. */
17938 if (decl == error_mark_node && TYPE_P (parser->scope)
17939 && dependent_type_p (parser->scope))
17945 /* The resolution to Core Issue 180 says that `struct
17946 A::B' should be considered a type-name, even if `A'
17948 type = make_typename_type (parser->scope, name, tag_type,
17949 /*complain=*/tf_error);
17950 decl = TYPE_NAME (type);
17952 else if (is_template
17953 && (cp_parser_next_token_ends_template_argument_p (parser)
17954 || cp_lexer_next_token_is (parser->lexer,
17956 decl = make_unbound_class_template (parser->scope,
17958 /*complain=*/tf_error);
17960 decl = build_qualified_name (/*type=*/NULL_TREE,
17961 parser->scope, name,
17964 parser->qualifying_scope = parser->scope;
17965 parser->object_scope = NULL_TREE;
17967 else if (object_type)
17969 tree object_decl = NULL_TREE;
17970 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17971 OBJECT_TYPE is not a class. */
17972 if (CLASS_TYPE_P (object_type))
17973 /* If the OBJECT_TYPE is a template specialization, it may
17974 be instantiated during name lookup. In that case, errors
17975 may be issued. Even if we rollback the current tentative
17976 parse, those errors are valid. */
17977 object_decl = lookup_member (object_type,
17980 tag_type != none_type);
17981 /* Look it up in the enclosing context, too. */
17982 decl = lookup_name_real (name, tag_type != none_type,
17984 /*block_p=*/true, is_namespace, flags);
17985 parser->object_scope = object_type;
17986 parser->qualifying_scope = NULL_TREE;
17988 decl = object_decl;
17992 decl = lookup_name_real (name, tag_type != none_type,
17994 /*block_p=*/true, is_namespace, flags);
17995 parser->qualifying_scope = NULL_TREE;
17996 parser->object_scope = NULL_TREE;
17999 /* If the lookup failed, let our caller know. */
18000 if (!decl || decl == error_mark_node)
18001 return error_mark_node;
18003 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18004 if (TREE_CODE (decl) == TREE_LIST)
18006 if (ambiguous_decls)
18007 *ambiguous_decls = decl;
18008 /* The error message we have to print is too complicated for
18009 cp_parser_error, so we incorporate its actions directly. */
18010 if (!cp_parser_simulate_error (parser))
18012 error_at (name_location, "reference to %qD is ambiguous",
18014 print_candidates (decl);
18016 return error_mark_node;
18019 gcc_assert (DECL_P (decl)
18020 || TREE_CODE (decl) == OVERLOAD
18021 || TREE_CODE (decl) == SCOPE_REF
18022 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18023 || BASELINK_P (decl));
18025 /* If we have resolved the name of a member declaration, check to
18026 see if the declaration is accessible. When the name resolves to
18027 set of overloaded functions, accessibility is checked when
18028 overload resolution is done.
18030 During an explicit instantiation, access is not checked at all,
18031 as per [temp.explicit]. */
18033 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18038 /* Like cp_parser_lookup_name, but for use in the typical case where
18039 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18040 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18043 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18045 return cp_parser_lookup_name (parser, name,
18047 /*is_template=*/false,
18048 /*is_namespace=*/false,
18049 /*check_dependency=*/true,
18050 /*ambiguous_decls=*/NULL,
18054 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18055 the current context, return the TYPE_DECL. If TAG_NAME_P is
18056 true, the DECL indicates the class being defined in a class-head,
18057 or declared in an elaborated-type-specifier.
18059 Otherwise, return DECL. */
18062 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18064 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18065 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18068 template <typename T> struct B;
18071 template <typename T> struct A::B {};
18073 Similarly, in an elaborated-type-specifier:
18075 namespace N { struct X{}; }
18078 template <typename T> friend struct N::X;
18081 However, if the DECL refers to a class type, and we are in
18082 the scope of the class, then the name lookup automatically
18083 finds the TYPE_DECL created by build_self_reference rather
18084 than a TEMPLATE_DECL. For example, in:
18086 template <class T> struct S {
18090 there is no need to handle such case. */
18092 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18093 return DECL_TEMPLATE_RESULT (decl);
18098 /* If too many, or too few, template-parameter lists apply to the
18099 declarator, issue an error message. Returns TRUE if all went well,
18100 and FALSE otherwise. */
18103 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18104 cp_declarator *declarator,
18105 location_t declarator_location)
18107 unsigned num_templates;
18109 /* We haven't seen any classes that involve template parameters yet. */
18112 switch (declarator->kind)
18115 if (declarator->u.id.qualifying_scope)
18120 scope = declarator->u.id.qualifying_scope;
18121 member = declarator->u.id.unqualified_name;
18123 while (scope && CLASS_TYPE_P (scope))
18125 /* You're supposed to have one `template <...>'
18126 for every template class, but you don't need one
18127 for a full specialization. For example:
18129 template <class T> struct S{};
18130 template <> struct S<int> { void f(); };
18131 void S<int>::f () {}
18133 is correct; there shouldn't be a `template <>' for
18134 the definition of `S<int>::f'. */
18135 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18136 /* If SCOPE does not have template information of any
18137 kind, then it is not a template, nor is it nested
18138 within a template. */
18140 if (explicit_class_specialization_p (scope))
18142 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18145 scope = TYPE_CONTEXT (scope);
18148 else if (TREE_CODE (declarator->u.id.unqualified_name)
18149 == TEMPLATE_ID_EXPR)
18150 /* If the DECLARATOR has the form `X<y>' then it uses one
18151 additional level of template parameters. */
18154 return cp_parser_check_template_parameters
18155 (parser, num_templates, declarator_location, declarator);
18161 case cdk_reference:
18163 return (cp_parser_check_declarator_template_parameters
18164 (parser, declarator->declarator, declarator_location));
18170 gcc_unreachable ();
18175 /* NUM_TEMPLATES were used in the current declaration. If that is
18176 invalid, return FALSE and issue an error messages. Otherwise,
18177 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18178 declarator and we can print more accurate diagnostics. */
18181 cp_parser_check_template_parameters (cp_parser* parser,
18182 unsigned num_templates,
18183 location_t location,
18184 cp_declarator *declarator)
18186 /* If there are the same number of template classes and parameter
18187 lists, that's OK. */
18188 if (parser->num_template_parameter_lists == num_templates)
18190 /* If there are more, but only one more, then we are referring to a
18191 member template. That's OK too. */
18192 if (parser->num_template_parameter_lists == num_templates + 1)
18194 /* If there are more template classes than parameter lists, we have
18197 template <class T> void S<T>::R<T>::f (); */
18198 if (parser->num_template_parameter_lists < num_templates)
18201 error_at (location, "specializing member %<%T::%E%> "
18202 "requires %<template<>%> syntax",
18203 declarator->u.id.qualifying_scope,
18204 declarator->u.id.unqualified_name);
18206 error_at (location, "too few template-parameter-lists");
18209 /* Otherwise, there are too many template parameter lists. We have
18212 template <class T> template <class U> void S::f(); */
18213 error_at (location, "too many template-parameter-lists");
18217 /* Parse an optional `::' token indicating that the following name is
18218 from the global namespace. If so, PARSER->SCOPE is set to the
18219 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18220 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18221 Returns the new value of PARSER->SCOPE, if the `::' token is
18222 present, and NULL_TREE otherwise. */
18225 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18229 /* Peek at the next token. */
18230 token = cp_lexer_peek_token (parser->lexer);
18231 /* If we're looking at a `::' token then we're starting from the
18232 global namespace, not our current location. */
18233 if (token->type == CPP_SCOPE)
18235 /* Consume the `::' token. */
18236 cp_lexer_consume_token (parser->lexer);
18237 /* Set the SCOPE so that we know where to start the lookup. */
18238 parser->scope = global_namespace;
18239 parser->qualifying_scope = global_namespace;
18240 parser->object_scope = NULL_TREE;
18242 return parser->scope;
18244 else if (!current_scope_valid_p)
18246 parser->scope = NULL_TREE;
18247 parser->qualifying_scope = NULL_TREE;
18248 parser->object_scope = NULL_TREE;
18254 /* Returns TRUE if the upcoming token sequence is the start of a
18255 constructor declarator. If FRIEND_P is true, the declarator is
18256 preceded by the `friend' specifier. */
18259 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18261 bool constructor_p;
18262 tree type_decl = NULL_TREE;
18263 bool nested_name_p;
18264 cp_token *next_token;
18266 /* The common case is that this is not a constructor declarator, so
18267 try to avoid doing lots of work if at all possible. It's not
18268 valid declare a constructor at function scope. */
18269 if (parser->in_function_body)
18271 /* And only certain tokens can begin a constructor declarator. */
18272 next_token = cp_lexer_peek_token (parser->lexer);
18273 if (next_token->type != CPP_NAME
18274 && next_token->type != CPP_SCOPE
18275 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18276 && next_token->type != CPP_TEMPLATE_ID)
18279 /* Parse tentatively; we are going to roll back all of the tokens
18281 cp_parser_parse_tentatively (parser);
18282 /* Assume that we are looking at a constructor declarator. */
18283 constructor_p = true;
18285 /* Look for the optional `::' operator. */
18286 cp_parser_global_scope_opt (parser,
18287 /*current_scope_valid_p=*/false);
18288 /* Look for the nested-name-specifier. */
18290 = (cp_parser_nested_name_specifier_opt (parser,
18291 /*typename_keyword_p=*/false,
18292 /*check_dependency_p=*/false,
18294 /*is_declaration=*/false)
18296 /* Outside of a class-specifier, there must be a
18297 nested-name-specifier. */
18298 if (!nested_name_p &&
18299 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18301 constructor_p = false;
18302 /* If we still think that this might be a constructor-declarator,
18303 look for a class-name. */
18308 template <typename T> struct S { S(); };
18309 template <typename T> S<T>::S ();
18311 we must recognize that the nested `S' names a class.
18314 template <typename T> S<T>::S<T> ();
18316 we must recognize that the nested `S' names a template. */
18317 type_decl = cp_parser_class_name (parser,
18318 /*typename_keyword_p=*/false,
18319 /*template_keyword_p=*/false,
18321 /*check_dependency_p=*/false,
18322 /*class_head_p=*/false,
18323 /*is_declaration=*/false);
18324 /* If there was no class-name, then this is not a constructor. */
18325 constructor_p = !cp_parser_error_occurred (parser);
18328 /* If we're still considering a constructor, we have to see a `(',
18329 to begin the parameter-declaration-clause, followed by either a
18330 `)', an `...', or a decl-specifier. We need to check for a
18331 type-specifier to avoid being fooled into thinking that:
18335 is a constructor. (It is actually a function named `f' that
18336 takes one parameter (of type `int') and returns a value of type
18339 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18342 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18343 /* A parameter declaration begins with a decl-specifier,
18344 which is either the "attribute" keyword, a storage class
18345 specifier, or (usually) a type-specifier. */
18346 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18349 tree pushed_scope = NULL_TREE;
18350 unsigned saved_num_template_parameter_lists;
18352 /* Names appearing in the type-specifier should be looked up
18353 in the scope of the class. */
18354 if (current_class_type)
18358 type = TREE_TYPE (type_decl);
18359 if (TREE_CODE (type) == TYPENAME_TYPE)
18361 type = resolve_typename_type (type,
18362 /*only_current_p=*/false);
18363 if (TREE_CODE (type) == TYPENAME_TYPE)
18365 cp_parser_abort_tentative_parse (parser);
18369 pushed_scope = push_scope (type);
18372 /* Inside the constructor parameter list, surrounding
18373 template-parameter-lists do not apply. */
18374 saved_num_template_parameter_lists
18375 = parser->num_template_parameter_lists;
18376 parser->num_template_parameter_lists = 0;
18378 /* Look for the type-specifier. */
18379 cp_parser_type_specifier (parser,
18380 CP_PARSER_FLAGS_NONE,
18381 /*decl_specs=*/NULL,
18382 /*is_declarator=*/true,
18383 /*declares_class_or_enum=*/NULL,
18384 /*is_cv_qualifier=*/NULL);
18386 parser->num_template_parameter_lists
18387 = saved_num_template_parameter_lists;
18389 /* Leave the scope of the class. */
18391 pop_scope (pushed_scope);
18393 constructor_p = !cp_parser_error_occurred (parser);
18397 constructor_p = false;
18398 /* We did not really want to consume any tokens. */
18399 cp_parser_abort_tentative_parse (parser);
18401 return constructor_p;
18404 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18405 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18406 they must be performed once we are in the scope of the function.
18408 Returns the function defined. */
18411 cp_parser_function_definition_from_specifiers_and_declarator
18412 (cp_parser* parser,
18413 cp_decl_specifier_seq *decl_specifiers,
18415 const cp_declarator *declarator)
18420 /* Begin the function-definition. */
18421 success_p = start_function (decl_specifiers, declarator, attributes);
18423 /* The things we're about to see are not directly qualified by any
18424 template headers we've seen thus far. */
18425 reset_specialization ();
18427 /* If there were names looked up in the decl-specifier-seq that we
18428 did not check, check them now. We must wait until we are in the
18429 scope of the function to perform the checks, since the function
18430 might be a friend. */
18431 perform_deferred_access_checks ();
18435 /* Skip the entire function. */
18436 cp_parser_skip_to_end_of_block_or_statement (parser);
18437 fn = error_mark_node;
18439 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18441 /* Seen already, skip it. An error message has already been output. */
18442 cp_parser_skip_to_end_of_block_or_statement (parser);
18443 fn = current_function_decl;
18444 current_function_decl = NULL_TREE;
18445 /* If this is a function from a class, pop the nested class. */
18446 if (current_class_name)
18447 pop_nested_class ();
18450 fn = cp_parser_function_definition_after_declarator (parser,
18451 /*inline_p=*/false);
18456 /* Parse the part of a function-definition that follows the
18457 declarator. INLINE_P is TRUE iff this function is an inline
18458 function defined within a class-specifier.
18460 Returns the function defined. */
18463 cp_parser_function_definition_after_declarator (cp_parser* parser,
18467 bool ctor_initializer_p = false;
18468 bool saved_in_unbraced_linkage_specification_p;
18469 bool saved_in_function_body;
18470 unsigned saved_num_template_parameter_lists;
18473 saved_in_function_body = parser->in_function_body;
18474 parser->in_function_body = true;
18475 /* If the next token is `return', then the code may be trying to
18476 make use of the "named return value" extension that G++ used to
18478 token = cp_lexer_peek_token (parser->lexer);
18479 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18481 /* Consume the `return' keyword. */
18482 cp_lexer_consume_token (parser->lexer);
18483 /* Look for the identifier that indicates what value is to be
18485 cp_parser_identifier (parser);
18486 /* Issue an error message. */
18487 error_at (token->location,
18488 "named return values are no longer supported");
18489 /* Skip tokens until we reach the start of the function body. */
18492 cp_token *token = cp_lexer_peek_token (parser->lexer);
18493 if (token->type == CPP_OPEN_BRACE
18494 || token->type == CPP_EOF
18495 || token->type == CPP_PRAGMA_EOL)
18497 cp_lexer_consume_token (parser->lexer);
18500 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18501 anything declared inside `f'. */
18502 saved_in_unbraced_linkage_specification_p
18503 = parser->in_unbraced_linkage_specification_p;
18504 parser->in_unbraced_linkage_specification_p = false;
18505 /* Inside the function, surrounding template-parameter-lists do not
18507 saved_num_template_parameter_lists
18508 = parser->num_template_parameter_lists;
18509 parser->num_template_parameter_lists = 0;
18511 start_lambda_scope (current_function_decl);
18513 /* If the next token is `try', then we are looking at a
18514 function-try-block. */
18515 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18516 ctor_initializer_p = cp_parser_function_try_block (parser);
18517 /* A function-try-block includes the function-body, so we only do
18518 this next part if we're not processing a function-try-block. */
18521 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18523 finish_lambda_scope ();
18525 /* Finish the function. */
18526 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18527 (inline_p ? 2 : 0));
18528 /* Generate code for it, if necessary. */
18529 expand_or_defer_fn (fn);
18530 /* Restore the saved values. */
18531 parser->in_unbraced_linkage_specification_p
18532 = saved_in_unbraced_linkage_specification_p;
18533 parser->num_template_parameter_lists
18534 = saved_num_template_parameter_lists;
18535 parser->in_function_body = saved_in_function_body;
18540 /* Parse a template-declaration, assuming that the `export' (and
18541 `extern') keywords, if present, has already been scanned. MEMBER_P
18542 is as for cp_parser_template_declaration. */
18545 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18547 tree decl = NULL_TREE;
18548 VEC (deferred_access_check,gc) *checks;
18549 tree parameter_list;
18550 bool friend_p = false;
18551 bool need_lang_pop;
18554 /* Look for the `template' keyword. */
18555 token = cp_lexer_peek_token (parser->lexer);
18556 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18560 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18562 if (at_class_scope_p () && current_function_decl)
18564 /* 14.5.2.2 [temp.mem]
18566 A local class shall not have member templates. */
18567 error_at (token->location,
18568 "invalid declaration of member template in local class");
18569 cp_parser_skip_to_end_of_block_or_statement (parser);
18574 A template ... shall not have C linkage. */
18575 if (current_lang_name == lang_name_c)
18577 error_at (token->location, "template with C linkage");
18578 /* Give it C++ linkage to avoid confusing other parts of the
18580 push_lang_context (lang_name_cplusplus);
18581 need_lang_pop = true;
18584 need_lang_pop = false;
18586 /* We cannot perform access checks on the template parameter
18587 declarations until we know what is being declared, just as we
18588 cannot check the decl-specifier list. */
18589 push_deferring_access_checks (dk_deferred);
18591 /* If the next token is `>', then we have an invalid
18592 specialization. Rather than complain about an invalid template
18593 parameter, issue an error message here. */
18594 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18596 cp_parser_error (parser, "invalid explicit specialization");
18597 begin_specialization ();
18598 parameter_list = NULL_TREE;
18601 /* Parse the template parameters. */
18602 parameter_list = cp_parser_template_parameter_list (parser);
18604 /* Get the deferred access checks from the parameter list. These
18605 will be checked once we know what is being declared, as for a
18606 member template the checks must be performed in the scope of the
18607 class containing the member. */
18608 checks = get_deferred_access_checks ();
18610 /* Look for the `>'. */
18611 cp_parser_skip_to_end_of_template_parameter_list (parser);
18612 /* We just processed one more parameter list. */
18613 ++parser->num_template_parameter_lists;
18614 /* If the next token is `template', there are more template
18616 if (cp_lexer_next_token_is_keyword (parser->lexer,
18618 cp_parser_template_declaration_after_export (parser, member_p);
18621 /* There are no access checks when parsing a template, as we do not
18622 know if a specialization will be a friend. */
18623 push_deferring_access_checks (dk_no_check);
18624 token = cp_lexer_peek_token (parser->lexer);
18625 decl = cp_parser_single_declaration (parser,
18628 /*explicit_specialization_p=*/false,
18630 pop_deferring_access_checks ();
18632 /* If this is a member template declaration, let the front
18634 if (member_p && !friend_p && decl)
18636 if (TREE_CODE (decl) == TYPE_DECL)
18637 cp_parser_check_access_in_redeclaration (decl, token->location);
18639 decl = finish_member_template_decl (decl);
18641 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18642 make_friend_class (current_class_type, TREE_TYPE (decl),
18643 /*complain=*/true);
18645 /* We are done with the current parameter list. */
18646 --parser->num_template_parameter_lists;
18648 pop_deferring_access_checks ();
18651 finish_template_decl (parameter_list);
18653 /* Register member declarations. */
18654 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18655 finish_member_declaration (decl);
18656 /* For the erroneous case of a template with C linkage, we pushed an
18657 implicit C++ linkage scope; exit that scope now. */
18659 pop_lang_context ();
18660 /* If DECL is a function template, we must return to parse it later.
18661 (Even though there is no definition, there might be default
18662 arguments that need handling.) */
18663 if (member_p && decl
18664 && (TREE_CODE (decl) == FUNCTION_DECL
18665 || DECL_FUNCTION_TEMPLATE_P (decl)))
18666 TREE_VALUE (parser->unparsed_functions_queues)
18667 = tree_cons (NULL_TREE, decl,
18668 TREE_VALUE (parser->unparsed_functions_queues));
18671 /* Perform the deferred access checks from a template-parameter-list.
18672 CHECKS is a TREE_LIST of access checks, as returned by
18673 get_deferred_access_checks. */
18676 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18678 ++processing_template_parmlist;
18679 perform_access_checks (checks);
18680 --processing_template_parmlist;
18683 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18684 `function-definition' sequence. MEMBER_P is true, this declaration
18685 appears in a class scope.
18687 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18688 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18691 cp_parser_single_declaration (cp_parser* parser,
18692 VEC (deferred_access_check,gc)* checks,
18694 bool explicit_specialization_p,
18697 int declares_class_or_enum;
18698 tree decl = NULL_TREE;
18699 cp_decl_specifier_seq decl_specifiers;
18700 bool function_definition_p = false;
18701 cp_token *decl_spec_token_start;
18703 /* This function is only used when processing a template
18705 gcc_assert (innermost_scope_kind () == sk_template_parms
18706 || innermost_scope_kind () == sk_template_spec);
18708 /* Defer access checks until we know what is being declared. */
18709 push_deferring_access_checks (dk_deferred);
18711 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18713 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18714 cp_parser_decl_specifier_seq (parser,
18715 CP_PARSER_FLAGS_OPTIONAL,
18717 &declares_class_or_enum);
18719 *friend_p = cp_parser_friend_p (&decl_specifiers);
18721 /* There are no template typedefs. */
18722 if (decl_specifiers.specs[(int) ds_typedef])
18724 error_at (decl_spec_token_start->location,
18725 "template declaration of %<typedef%>");
18726 decl = error_mark_node;
18729 /* Gather up the access checks that occurred the
18730 decl-specifier-seq. */
18731 stop_deferring_access_checks ();
18733 /* Check for the declaration of a template class. */
18734 if (declares_class_or_enum)
18736 if (cp_parser_declares_only_class_p (parser))
18738 decl = shadow_tag (&decl_specifiers);
18743 friend template <typename T> struct A<T>::B;
18746 A<T>::B will be represented by a TYPENAME_TYPE, and
18747 therefore not recognized by shadow_tag. */
18748 if (friend_p && *friend_p
18750 && decl_specifiers.type
18751 && TYPE_P (decl_specifiers.type))
18752 decl = decl_specifiers.type;
18754 if (decl && decl != error_mark_node)
18755 decl = TYPE_NAME (decl);
18757 decl = error_mark_node;
18759 /* Perform access checks for template parameters. */
18760 cp_parser_perform_template_parameter_access_checks (checks);
18763 /* If it's not a template class, try for a template function. If
18764 the next token is a `;', then this declaration does not declare
18765 anything. But, if there were errors in the decl-specifiers, then
18766 the error might well have come from an attempted class-specifier.
18767 In that case, there's no need to warn about a missing declarator. */
18769 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18770 || decl_specifiers.type != error_mark_node))
18772 decl = cp_parser_init_declarator (parser,
18775 /*function_definition_allowed_p=*/true,
18777 declares_class_or_enum,
18778 &function_definition_p);
18780 /* 7.1.1-1 [dcl.stc]
18782 A storage-class-specifier shall not be specified in an explicit
18783 specialization... */
18785 && explicit_specialization_p
18786 && decl_specifiers.storage_class != sc_none)
18788 error_at (decl_spec_token_start->location,
18789 "explicit template specialization cannot have a storage class");
18790 decl = error_mark_node;
18794 pop_deferring_access_checks ();
18796 /* Clear any current qualification; whatever comes next is the start
18797 of something new. */
18798 parser->scope = NULL_TREE;
18799 parser->qualifying_scope = NULL_TREE;
18800 parser->object_scope = NULL_TREE;
18801 /* Look for a trailing `;' after the declaration. */
18802 if (!function_definition_p
18803 && (decl == error_mark_node
18804 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18805 cp_parser_skip_to_end_of_block_or_statement (parser);
18810 /* Parse a cast-expression that is not the operand of a unary "&". */
18813 cp_parser_simple_cast_expression (cp_parser *parser)
18815 return cp_parser_cast_expression (parser, /*address_p=*/false,
18816 /*cast_p=*/false, NULL);
18819 /* Parse a functional cast to TYPE. Returns an expression
18820 representing the cast. */
18823 cp_parser_functional_cast (cp_parser* parser, tree type)
18826 tree expression_list;
18830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18832 maybe_warn_cpp0x ("extended initializer lists");
18833 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18834 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18835 if (TREE_CODE (type) == TYPE_DECL)
18836 type = TREE_TYPE (type);
18837 return finish_compound_literal (type, expression_list);
18841 vec = cp_parser_parenthesized_expression_list (parser, false,
18843 /*allow_expansion_p=*/true,
18844 /*non_constant_p=*/NULL);
18846 expression_list = error_mark_node;
18849 expression_list = build_tree_list_vec (vec);
18850 release_tree_vector (vec);
18853 cast = build_functional_cast (type, expression_list,
18854 tf_warning_or_error);
18855 /* [expr.const]/1: In an integral constant expression "only type
18856 conversions to integral or enumeration type can be used". */
18857 if (TREE_CODE (type) == TYPE_DECL)
18858 type = TREE_TYPE (type);
18859 if (cast != error_mark_node
18860 && !cast_valid_in_integral_constant_expression_p (type)
18861 && (cp_parser_non_integral_constant_expression
18862 (parser, "a call to a constructor")))
18863 return error_mark_node;
18867 /* Save the tokens that make up the body of a member function defined
18868 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18869 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18870 specifiers applied to the declaration. Returns the FUNCTION_DECL
18871 for the member function. */
18874 cp_parser_save_member_function_body (cp_parser* parser,
18875 cp_decl_specifier_seq *decl_specifiers,
18876 cp_declarator *declarator,
18883 /* Create the FUNCTION_DECL. */
18884 fn = grokmethod (decl_specifiers, declarator, attributes);
18885 /* If something went badly wrong, bail out now. */
18886 if (fn == error_mark_node)
18888 /* If there's a function-body, skip it. */
18889 if (cp_parser_token_starts_function_definition_p
18890 (cp_lexer_peek_token (parser->lexer)))
18891 cp_parser_skip_to_end_of_block_or_statement (parser);
18892 return error_mark_node;
18895 /* Remember it, if there default args to post process. */
18896 cp_parser_save_default_args (parser, fn);
18898 /* Save away the tokens that make up the body of the
18900 first = parser->lexer->next_token;
18901 /* We can have braced-init-list mem-initializers before the fn body. */
18902 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18904 cp_lexer_consume_token (parser->lexer);
18905 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18906 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18908 /* cache_group will stop after an un-nested { } pair, too. */
18909 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18912 /* variadic mem-inits have ... after the ')'. */
18913 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18914 cp_lexer_consume_token (parser->lexer);
18917 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18918 /* Handle function try blocks. */
18919 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18920 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18921 last = parser->lexer->next_token;
18923 /* Save away the inline definition; we will process it when the
18924 class is complete. */
18925 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18926 DECL_PENDING_INLINE_P (fn) = 1;
18928 /* We need to know that this was defined in the class, so that
18929 friend templates are handled correctly. */
18930 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18932 /* Add FN to the queue of functions to be parsed later. */
18933 TREE_VALUE (parser->unparsed_functions_queues)
18934 = tree_cons (NULL_TREE, fn,
18935 TREE_VALUE (parser->unparsed_functions_queues));
18940 /* Parse a template-argument-list, as well as the trailing ">" (but
18941 not the opening ">"). See cp_parser_template_argument_list for the
18945 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18949 tree saved_qualifying_scope;
18950 tree saved_object_scope;
18951 bool saved_greater_than_is_operator_p;
18952 int saved_unevaluated_operand;
18953 int saved_inhibit_evaluation_warnings;
18957 When parsing a template-id, the first non-nested `>' is taken as
18958 the end of the template-argument-list rather than a greater-than
18960 saved_greater_than_is_operator_p
18961 = parser->greater_than_is_operator_p;
18962 parser->greater_than_is_operator_p = false;
18963 /* Parsing the argument list may modify SCOPE, so we save it
18965 saved_scope = parser->scope;
18966 saved_qualifying_scope = parser->qualifying_scope;
18967 saved_object_scope = parser->object_scope;
18968 /* We need to evaluate the template arguments, even though this
18969 template-id may be nested within a "sizeof". */
18970 saved_unevaluated_operand = cp_unevaluated_operand;
18971 cp_unevaluated_operand = 0;
18972 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18973 c_inhibit_evaluation_warnings = 0;
18974 /* Parse the template-argument-list itself. */
18975 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18976 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18977 arguments = NULL_TREE;
18979 arguments = cp_parser_template_argument_list (parser);
18980 /* Look for the `>' that ends the template-argument-list. If we find
18981 a '>>' instead, it's probably just a typo. */
18982 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18984 if (cxx_dialect != cxx98)
18986 /* In C++0x, a `>>' in a template argument list or cast
18987 expression is considered to be two separate `>'
18988 tokens. So, change the current token to a `>', but don't
18989 consume it: it will be consumed later when the outer
18990 template argument list (or cast expression) is parsed.
18991 Note that this replacement of `>' for `>>' is necessary
18992 even if we are parsing tentatively: in the tentative
18993 case, after calling
18994 cp_parser_enclosed_template_argument_list we will always
18995 throw away all of the template arguments and the first
18996 closing `>', either because the template argument list
18997 was erroneous or because we are replacing those tokens
18998 with a CPP_TEMPLATE_ID token. The second `>' (which will
18999 not have been thrown away) is needed either to close an
19000 outer template argument list or to complete a new-style
19002 cp_token *token = cp_lexer_peek_token (parser->lexer);
19003 token->type = CPP_GREATER;
19005 else if (!saved_greater_than_is_operator_p)
19007 /* If we're in a nested template argument list, the '>>' has
19008 to be a typo for '> >'. We emit the error message, but we
19009 continue parsing and we push a '>' as next token, so that
19010 the argument list will be parsed correctly. Note that the
19011 global source location is still on the token before the
19012 '>>', so we need to say explicitly where we want it. */
19013 cp_token *token = cp_lexer_peek_token (parser->lexer);
19014 error_at (token->location, "%<>>%> should be %<> >%> "
19015 "within a nested template argument list");
19017 token->type = CPP_GREATER;
19021 /* If this is not a nested template argument list, the '>>'
19022 is a typo for '>'. Emit an error message and continue.
19023 Same deal about the token location, but here we can get it
19024 right by consuming the '>>' before issuing the diagnostic. */
19025 cp_token *token = cp_lexer_consume_token (parser->lexer);
19026 error_at (token->location,
19027 "spurious %<>>%>, use %<>%> to terminate "
19028 "a template argument list");
19032 cp_parser_skip_to_end_of_template_parameter_list (parser);
19033 /* The `>' token might be a greater-than operator again now. */
19034 parser->greater_than_is_operator_p
19035 = saved_greater_than_is_operator_p;
19036 /* Restore the SAVED_SCOPE. */
19037 parser->scope = saved_scope;
19038 parser->qualifying_scope = saved_qualifying_scope;
19039 parser->object_scope = saved_object_scope;
19040 cp_unevaluated_operand = saved_unevaluated_operand;
19041 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19046 /* MEMBER_FUNCTION is a member function, or a friend. If default
19047 arguments, or the body of the function have not yet been parsed,
19051 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19053 /* If this member is a template, get the underlying
19055 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19056 member_function = DECL_TEMPLATE_RESULT (member_function);
19058 /* There should not be any class definitions in progress at this
19059 point; the bodies of members are only parsed outside of all class
19061 gcc_assert (parser->num_classes_being_defined == 0);
19062 /* While we're parsing the member functions we might encounter more
19063 classes. We want to handle them right away, but we don't want
19064 them getting mixed up with functions that are currently in the
19066 parser->unparsed_functions_queues
19067 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19069 /* Make sure that any template parameters are in scope. */
19070 maybe_begin_member_template_processing (member_function);
19072 /* If the body of the function has not yet been parsed, parse it
19074 if (DECL_PENDING_INLINE_P (member_function))
19076 tree function_scope;
19077 cp_token_cache *tokens;
19079 /* The function is no longer pending; we are processing it. */
19080 tokens = DECL_PENDING_INLINE_INFO (member_function);
19081 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19082 DECL_PENDING_INLINE_P (member_function) = 0;
19084 /* If this is a local class, enter the scope of the containing
19086 function_scope = current_function_decl;
19087 if (function_scope)
19088 push_function_context ();
19090 /* Push the body of the function onto the lexer stack. */
19091 cp_parser_push_lexer_for_tokens (parser, tokens);
19093 /* Let the front end know that we going to be defining this
19095 start_preparsed_function (member_function, NULL_TREE,
19096 SF_PRE_PARSED | SF_INCLASS_INLINE);
19098 /* Don't do access checking if it is a templated function. */
19099 if (processing_template_decl)
19100 push_deferring_access_checks (dk_no_check);
19102 /* Now, parse the body of the function. */
19103 cp_parser_function_definition_after_declarator (parser,
19104 /*inline_p=*/true);
19106 if (processing_template_decl)
19107 pop_deferring_access_checks ();
19109 /* Leave the scope of the containing function. */
19110 if (function_scope)
19111 pop_function_context ();
19112 cp_parser_pop_lexer (parser);
19115 /* Remove any template parameters from the symbol table. */
19116 maybe_end_member_template_processing ();
19118 /* Restore the queue. */
19119 parser->unparsed_functions_queues
19120 = TREE_CHAIN (parser->unparsed_functions_queues);
19123 /* If DECL contains any default args, remember it on the unparsed
19124 functions queue. */
19127 cp_parser_save_default_args (cp_parser* parser, tree decl)
19131 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19133 probe = TREE_CHAIN (probe))
19134 if (TREE_PURPOSE (probe))
19136 TREE_PURPOSE (parser->unparsed_functions_queues)
19137 = tree_cons (current_class_type, decl,
19138 TREE_PURPOSE (parser->unparsed_functions_queues));
19143 /* FN is a FUNCTION_DECL which may contains a parameter with an
19144 unparsed DEFAULT_ARG. Parse the default args now. This function
19145 assumes that the current scope is the scope in which the default
19146 argument should be processed. */
19149 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19151 bool saved_local_variables_forbidden_p;
19152 tree parm, parmdecl;
19154 /* While we're parsing the default args, we might (due to the
19155 statement expression extension) encounter more classes. We want
19156 to handle them right away, but we don't want them getting mixed
19157 up with default args that are currently in the queue. */
19158 parser->unparsed_functions_queues
19159 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19161 /* Local variable names (and the `this' keyword) may not appear
19162 in a default argument. */
19163 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19164 parser->local_variables_forbidden_p = true;
19166 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19167 parmdecl = DECL_ARGUMENTS (fn);
19168 parm && parm != void_list_node;
19169 parm = TREE_CHAIN (parm),
19170 parmdecl = TREE_CHAIN (parmdecl))
19172 cp_token_cache *tokens;
19173 tree default_arg = TREE_PURPOSE (parm);
19175 VEC(tree,gc) *insts;
19182 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19183 /* This can happen for a friend declaration for a function
19184 already declared with default arguments. */
19187 /* Push the saved tokens for the default argument onto the parser's
19189 tokens = DEFARG_TOKENS (default_arg);
19190 cp_parser_push_lexer_for_tokens (parser, tokens);
19192 start_lambda_scope (parmdecl);
19194 /* Parse the assignment-expression. */
19195 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19196 if (parsed_arg == error_mark_node)
19198 cp_parser_pop_lexer (parser);
19202 if (!processing_template_decl)
19203 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19205 TREE_PURPOSE (parm) = parsed_arg;
19207 /* Update any instantiations we've already created. */
19208 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19209 VEC_iterate (tree, insts, ix, copy); ix++)
19210 TREE_PURPOSE (copy) = parsed_arg;
19212 finish_lambda_scope ();
19214 /* If the token stream has not been completely used up, then
19215 there was extra junk after the end of the default
19217 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19218 cp_parser_error (parser, "expected %<,%>");
19220 /* Revert to the main lexer. */
19221 cp_parser_pop_lexer (parser);
19224 /* Make sure no default arg is missing. */
19225 check_default_args (fn);
19227 /* Restore the state of local_variables_forbidden_p. */
19228 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19230 /* Restore the queue. */
19231 parser->unparsed_functions_queues
19232 = TREE_CHAIN (parser->unparsed_functions_queues);
19235 /* Parse the operand of `sizeof' (or a similar operator). Returns
19236 either a TYPE or an expression, depending on the form of the
19237 input. The KEYWORD indicates which kind of expression we have
19241 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19243 tree expr = NULL_TREE;
19244 const char *saved_message;
19246 bool saved_integral_constant_expression_p;
19247 bool saved_non_integral_constant_expression_p;
19248 bool pack_expansion_p = false;
19250 /* Types cannot be defined in a `sizeof' expression. Save away the
19252 saved_message = parser->type_definition_forbidden_message;
19253 /* And create the new one. */
19254 tmp = concat ("types may not be defined in %<",
19255 IDENTIFIER_POINTER (ridpointers[keyword]),
19256 "%> expressions", NULL);
19257 parser->type_definition_forbidden_message = tmp;
19259 /* The restrictions on constant-expressions do not apply inside
19260 sizeof expressions. */
19261 saved_integral_constant_expression_p
19262 = parser->integral_constant_expression_p;
19263 saved_non_integral_constant_expression_p
19264 = parser->non_integral_constant_expression_p;
19265 parser->integral_constant_expression_p = false;
19267 /* If it's a `...', then we are computing the length of a parameter
19269 if (keyword == RID_SIZEOF
19270 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19272 /* Consume the `...'. */
19273 cp_lexer_consume_token (parser->lexer);
19274 maybe_warn_variadic_templates ();
19276 /* Note that this is an expansion. */
19277 pack_expansion_p = true;
19280 /* Do not actually evaluate the expression. */
19281 ++cp_unevaluated_operand;
19282 ++c_inhibit_evaluation_warnings;
19283 /* If it's a `(', then we might be looking at the type-id
19285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19288 bool saved_in_type_id_in_expr_p;
19290 /* We can't be sure yet whether we're looking at a type-id or an
19292 cp_parser_parse_tentatively (parser);
19293 /* Consume the `('. */
19294 cp_lexer_consume_token (parser->lexer);
19295 /* Parse the type-id. */
19296 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19297 parser->in_type_id_in_expr_p = true;
19298 type = cp_parser_type_id (parser);
19299 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19300 /* Now, look for the trailing `)'. */
19301 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19302 /* If all went well, then we're done. */
19303 if (cp_parser_parse_definitely (parser))
19305 cp_decl_specifier_seq decl_specs;
19307 /* Build a trivial decl-specifier-seq. */
19308 clear_decl_specs (&decl_specs);
19309 decl_specs.type = type;
19311 /* Call grokdeclarator to figure out what type this is. */
19312 expr = grokdeclarator (NULL,
19316 /*attrlist=*/NULL);
19320 /* If the type-id production did not work out, then we must be
19321 looking at the unary-expression production. */
19323 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19324 /*cast_p=*/false, NULL);
19326 if (pack_expansion_p)
19327 /* Build a pack expansion. */
19328 expr = make_pack_expansion (expr);
19330 /* Go back to evaluating expressions. */
19331 --cp_unevaluated_operand;
19332 --c_inhibit_evaluation_warnings;
19334 /* Free the message we created. */
19336 /* And restore the old one. */
19337 parser->type_definition_forbidden_message = saved_message;
19338 parser->integral_constant_expression_p
19339 = saved_integral_constant_expression_p;
19340 parser->non_integral_constant_expression_p
19341 = saved_non_integral_constant_expression_p;
19346 /* If the current declaration has no declarator, return true. */
19349 cp_parser_declares_only_class_p (cp_parser *parser)
19351 /* If the next token is a `;' or a `,' then there is no
19353 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19354 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19357 /* Update the DECL_SPECS to reflect the storage class indicated by
19361 cp_parser_set_storage_class (cp_parser *parser,
19362 cp_decl_specifier_seq *decl_specs,
19364 location_t location)
19366 cp_storage_class storage_class;
19368 if (parser->in_unbraced_linkage_specification_p)
19370 error_at (location, "invalid use of %qD in linkage specification",
19371 ridpointers[keyword]);
19374 else if (decl_specs->storage_class != sc_none)
19376 decl_specs->conflicting_specifiers_p = true;
19380 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19381 && decl_specs->specs[(int) ds_thread])
19383 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19384 decl_specs->specs[(int) ds_thread] = 0;
19390 storage_class = sc_auto;
19393 storage_class = sc_register;
19396 storage_class = sc_static;
19399 storage_class = sc_extern;
19402 storage_class = sc_mutable;
19405 gcc_unreachable ();
19407 decl_specs->storage_class = storage_class;
19409 /* A storage class specifier cannot be applied alongside a typedef
19410 specifier. If there is a typedef specifier present then set
19411 conflicting_specifiers_p which will trigger an error later
19412 on in grokdeclarator. */
19413 if (decl_specs->specs[(int)ds_typedef])
19414 decl_specs->conflicting_specifiers_p = true;
19417 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19418 is true, the type is a user-defined type; otherwise it is a
19419 built-in type specified by a keyword. */
19422 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19424 location_t location,
19425 bool user_defined_p)
19427 decl_specs->any_specifiers_p = true;
19429 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19430 (with, for example, in "typedef int wchar_t;") we remember that
19431 this is what happened. In system headers, we ignore these
19432 declarations so that G++ can work with system headers that are not
19434 if (decl_specs->specs[(int) ds_typedef]
19436 && (type_spec == boolean_type_node
19437 || type_spec == char16_type_node
19438 || type_spec == char32_type_node
19439 || type_spec == wchar_type_node)
19440 && (decl_specs->type
19441 || decl_specs->specs[(int) ds_long]
19442 || decl_specs->specs[(int) ds_short]
19443 || decl_specs->specs[(int) ds_unsigned]
19444 || decl_specs->specs[(int) ds_signed]))
19446 decl_specs->redefined_builtin_type = type_spec;
19447 if (!decl_specs->type)
19449 decl_specs->type = type_spec;
19450 decl_specs->user_defined_type_p = false;
19451 decl_specs->type_location = location;
19454 else if (decl_specs->type)
19455 decl_specs->multiple_types_p = true;
19458 decl_specs->type = type_spec;
19459 decl_specs->user_defined_type_p = user_defined_p;
19460 decl_specs->redefined_builtin_type = NULL_TREE;
19461 decl_specs->type_location = location;
19465 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19466 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19469 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19471 return decl_specifiers->specs[(int) ds_friend] != 0;
19474 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19475 issue an error message indicating that TOKEN_DESC was expected.
19477 Returns the token consumed, if the token had the appropriate type.
19478 Otherwise, returns NULL. */
19481 cp_parser_require (cp_parser* parser,
19482 enum cpp_ttype type,
19483 const char* token_desc)
19485 if (cp_lexer_next_token_is (parser->lexer, type))
19486 return cp_lexer_consume_token (parser->lexer);
19489 /* Output the MESSAGE -- unless we're parsing tentatively. */
19490 if (!cp_parser_simulate_error (parser))
19492 char *message = concat ("expected ", token_desc, NULL);
19493 cp_parser_error (parser, message);
19500 /* An error message is produced if the next token is not '>'.
19501 All further tokens are skipped until the desired token is
19502 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19505 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19507 /* Current level of '< ... >'. */
19508 unsigned level = 0;
19509 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19510 unsigned nesting_depth = 0;
19512 /* Are we ready, yet? If not, issue error message. */
19513 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19516 /* Skip tokens until the desired token is found. */
19519 /* Peek at the next token. */
19520 switch (cp_lexer_peek_token (parser->lexer)->type)
19523 if (!nesting_depth)
19528 if (cxx_dialect == cxx98)
19529 /* C++0x views the `>>' operator as two `>' tokens, but
19532 else if (!nesting_depth && level-- == 0)
19534 /* We've hit a `>>' where the first `>' closes the
19535 template argument list, and the second `>' is
19536 spurious. Just consume the `>>' and stop; we've
19537 already produced at least one error. */
19538 cp_lexer_consume_token (parser->lexer);
19541 /* Fall through for C++0x, so we handle the second `>' in
19545 if (!nesting_depth && level-- == 0)
19547 /* We've reached the token we want, consume it and stop. */
19548 cp_lexer_consume_token (parser->lexer);
19553 case CPP_OPEN_PAREN:
19554 case CPP_OPEN_SQUARE:
19558 case CPP_CLOSE_PAREN:
19559 case CPP_CLOSE_SQUARE:
19560 if (nesting_depth-- == 0)
19565 case CPP_PRAGMA_EOL:
19566 case CPP_SEMICOLON:
19567 case CPP_OPEN_BRACE:
19568 case CPP_CLOSE_BRACE:
19569 /* The '>' was probably forgotten, don't look further. */
19576 /* Consume this token. */
19577 cp_lexer_consume_token (parser->lexer);
19581 /* If the next token is the indicated keyword, consume it. Otherwise,
19582 issue an error message indicating that TOKEN_DESC was expected.
19584 Returns the token consumed, if the token had the appropriate type.
19585 Otherwise, returns NULL. */
19588 cp_parser_require_keyword (cp_parser* parser,
19590 const char* token_desc)
19592 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19594 if (token && token->keyword != keyword)
19596 dyn_string_t error_msg;
19598 /* Format the error message. */
19599 error_msg = dyn_string_new (0);
19600 dyn_string_append_cstr (error_msg, "expected ");
19601 dyn_string_append_cstr (error_msg, token_desc);
19602 cp_parser_error (parser, error_msg->s);
19603 dyn_string_delete (error_msg);
19610 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19611 function-definition. */
19614 cp_parser_token_starts_function_definition_p (cp_token* token)
19616 return (/* An ordinary function-body begins with an `{'. */
19617 token->type == CPP_OPEN_BRACE
19618 /* A ctor-initializer begins with a `:'. */
19619 || token->type == CPP_COLON
19620 /* A function-try-block begins with `try'. */
19621 || token->keyword == RID_TRY
19622 /* The named return value extension begins with `return'. */
19623 || token->keyword == RID_RETURN);
19626 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19630 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19634 token = cp_lexer_peek_token (parser->lexer);
19635 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19638 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19639 C++0x) ending a template-argument. */
19642 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19646 token = cp_lexer_peek_token (parser->lexer);
19647 return (token->type == CPP_COMMA
19648 || token->type == CPP_GREATER
19649 || token->type == CPP_ELLIPSIS
19650 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19653 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19654 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19657 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19662 token = cp_lexer_peek_nth_token (parser->lexer, n);
19663 if (token->type == CPP_LESS)
19665 /* Check for the sequence `<::' in the original code. It would be lexed as
19666 `[:', where `[' is a digraph, and there is no whitespace before
19668 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19671 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19672 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19678 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19679 or none_type otherwise. */
19681 static enum tag_types
19682 cp_parser_token_is_class_key (cp_token* token)
19684 switch (token->keyword)
19689 return record_type;
19698 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19701 cp_parser_check_class_key (enum tag_types class_key, tree type)
19703 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19704 permerror (input_location, "%qs tag used in naming %q#T",
19705 class_key == union_type ? "union"
19706 : class_key == record_type ? "struct" : "class",
19710 /* Issue an error message if DECL is redeclared with different
19711 access than its original declaration [class.access.spec/3].
19712 This applies to nested classes and nested class templates.
19716 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19718 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19721 if ((TREE_PRIVATE (decl)
19722 != (current_access_specifier == access_private_node))
19723 || (TREE_PROTECTED (decl)
19724 != (current_access_specifier == access_protected_node)))
19725 error_at (location, "%qD redeclared with different access", decl);
19728 /* Look for the `template' keyword, as a syntactic disambiguator.
19729 Return TRUE iff it is present, in which case it will be
19733 cp_parser_optional_template_keyword (cp_parser *parser)
19735 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19737 /* The `template' keyword can only be used within templates;
19738 outside templates the parser can always figure out what is a
19739 template and what is not. */
19740 if (!processing_template_decl)
19742 cp_token *token = cp_lexer_peek_token (parser->lexer);
19743 error_at (token->location,
19744 "%<template%> (as a disambiguator) is only allowed "
19745 "within templates");
19746 /* If this part of the token stream is rescanned, the same
19747 error message would be generated. So, we purge the token
19748 from the stream. */
19749 cp_lexer_purge_token (parser->lexer);
19754 /* Consume the `template' keyword. */
19755 cp_lexer_consume_token (parser->lexer);
19763 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19764 set PARSER->SCOPE, and perform other related actions. */
19767 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19770 struct tree_check *check_value;
19771 deferred_access_check *chk;
19772 VEC (deferred_access_check,gc) *checks;
19774 /* Get the stored value. */
19775 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19776 /* Perform any access checks that were deferred. */
19777 checks = check_value->checks;
19781 VEC_iterate (deferred_access_check, checks, i, chk) ;
19784 perform_or_defer_access_check (chk->binfo,
19789 /* Set the scope from the stored value. */
19790 parser->scope = check_value->value;
19791 parser->qualifying_scope = check_value->qualifying_scope;
19792 parser->object_scope = NULL_TREE;
19795 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19796 encounter the end of a block before what we were looking for. */
19799 cp_parser_cache_group (cp_parser *parser,
19800 enum cpp_ttype end,
19805 cp_token *token = cp_lexer_peek_token (parser->lexer);
19807 /* Abort a parenthesized expression if we encounter a semicolon. */
19808 if ((end == CPP_CLOSE_PAREN || depth == 0)
19809 && token->type == CPP_SEMICOLON)
19811 /* If we've reached the end of the file, stop. */
19812 if (token->type == CPP_EOF
19813 || (end != CPP_PRAGMA_EOL
19814 && token->type == CPP_PRAGMA_EOL))
19816 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19817 /* We've hit the end of an enclosing block, so there's been some
19818 kind of syntax error. */
19821 /* Consume the token. */
19822 cp_lexer_consume_token (parser->lexer);
19823 /* See if it starts a new group. */
19824 if (token->type == CPP_OPEN_BRACE)
19826 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19827 /* In theory this should probably check end == '}', but
19828 cp_parser_save_member_function_body needs it to exit
19829 after either '}' or ')' when called with ')'. */
19833 else if (token->type == CPP_OPEN_PAREN)
19835 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19836 if (depth == 0 && end == CPP_CLOSE_PAREN)
19839 else if (token->type == CPP_PRAGMA)
19840 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19841 else if (token->type == end)
19846 /* Begin parsing tentatively. We always save tokens while parsing
19847 tentatively so that if the tentative parsing fails we can restore the
19851 cp_parser_parse_tentatively (cp_parser* parser)
19853 /* Enter a new parsing context. */
19854 parser->context = cp_parser_context_new (parser->context);
19855 /* Begin saving tokens. */
19856 cp_lexer_save_tokens (parser->lexer);
19857 /* In order to avoid repetitive access control error messages,
19858 access checks are queued up until we are no longer parsing
19860 push_deferring_access_checks (dk_deferred);
19863 /* Commit to the currently active tentative parse. */
19866 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19868 cp_parser_context *context;
19871 /* Mark all of the levels as committed. */
19872 lexer = parser->lexer;
19873 for (context = parser->context; context->next; context = context->next)
19875 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19877 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19878 while (!cp_lexer_saving_tokens (lexer))
19879 lexer = lexer->next;
19880 cp_lexer_commit_tokens (lexer);
19884 /* Abort the currently active tentative parse. All consumed tokens
19885 will be rolled back, and no diagnostics will be issued. */
19888 cp_parser_abort_tentative_parse (cp_parser* parser)
19890 cp_parser_simulate_error (parser);
19891 /* Now, pretend that we want to see if the construct was
19892 successfully parsed. */
19893 cp_parser_parse_definitely (parser);
19896 /* Stop parsing tentatively. If a parse error has occurred, restore the
19897 token stream. Otherwise, commit to the tokens we have consumed.
19898 Returns true if no error occurred; false otherwise. */
19901 cp_parser_parse_definitely (cp_parser* parser)
19903 bool error_occurred;
19904 cp_parser_context *context;
19906 /* Remember whether or not an error occurred, since we are about to
19907 destroy that information. */
19908 error_occurred = cp_parser_error_occurred (parser);
19909 /* Remove the topmost context from the stack. */
19910 context = parser->context;
19911 parser->context = context->next;
19912 /* If no parse errors occurred, commit to the tentative parse. */
19913 if (!error_occurred)
19915 /* Commit to the tokens read tentatively, unless that was
19917 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19918 cp_lexer_commit_tokens (parser->lexer);
19920 pop_to_parent_deferring_access_checks ();
19922 /* Otherwise, if errors occurred, roll back our state so that things
19923 are just as they were before we began the tentative parse. */
19926 cp_lexer_rollback_tokens (parser->lexer);
19927 pop_deferring_access_checks ();
19929 /* Add the context to the front of the free list. */
19930 context->next = cp_parser_context_free_list;
19931 cp_parser_context_free_list = context;
19933 return !error_occurred;
19936 /* Returns true if we are parsing tentatively and are not committed to
19937 this tentative parse. */
19940 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19942 return (cp_parser_parsing_tentatively (parser)
19943 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19946 /* Returns nonzero iff an error has occurred during the most recent
19947 tentative parse. */
19950 cp_parser_error_occurred (cp_parser* parser)
19952 return (cp_parser_parsing_tentatively (parser)
19953 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19956 /* Returns nonzero if GNU extensions are allowed. */
19959 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19961 return parser->allow_gnu_extensions_p;
19964 /* Objective-C++ Productions */
19967 /* Parse an Objective-C expression, which feeds into a primary-expression
19971 objc-message-expression
19972 objc-string-literal
19973 objc-encode-expression
19974 objc-protocol-expression
19975 objc-selector-expression
19977 Returns a tree representation of the expression. */
19980 cp_parser_objc_expression (cp_parser* parser)
19982 /* Try to figure out what kind of declaration is present. */
19983 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19987 case CPP_OPEN_SQUARE:
19988 return cp_parser_objc_message_expression (parser);
19990 case CPP_OBJC_STRING:
19991 kwd = cp_lexer_consume_token (parser->lexer);
19992 return objc_build_string_object (kwd->u.value);
19995 switch (kwd->keyword)
19997 case RID_AT_ENCODE:
19998 return cp_parser_objc_encode_expression (parser);
20000 case RID_AT_PROTOCOL:
20001 return cp_parser_objc_protocol_expression (parser);
20003 case RID_AT_SELECTOR:
20004 return cp_parser_objc_selector_expression (parser);
20010 error_at (kwd->location,
20011 "misplaced %<@%D%> Objective-C++ construct",
20013 cp_parser_skip_to_end_of_block_or_statement (parser);
20016 return error_mark_node;
20019 /* Parse an Objective-C message expression.
20021 objc-message-expression:
20022 [ objc-message-receiver objc-message-args ]
20024 Returns a representation of an Objective-C message. */
20027 cp_parser_objc_message_expression (cp_parser* parser)
20029 tree receiver, messageargs;
20031 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20032 receiver = cp_parser_objc_message_receiver (parser);
20033 messageargs = cp_parser_objc_message_args (parser);
20034 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20036 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20039 /* Parse an objc-message-receiver.
20041 objc-message-receiver:
20043 simple-type-specifier
20045 Returns a representation of the type or expression. */
20048 cp_parser_objc_message_receiver (cp_parser* parser)
20052 /* An Objective-C message receiver may be either (1) a type
20053 or (2) an expression. */
20054 cp_parser_parse_tentatively (parser);
20055 rcv = cp_parser_expression (parser, false, NULL);
20057 if (cp_parser_parse_definitely (parser))
20060 rcv = cp_parser_simple_type_specifier (parser,
20061 /*decl_specs=*/NULL,
20062 CP_PARSER_FLAGS_NONE);
20064 return objc_get_class_reference (rcv);
20067 /* Parse the arguments and selectors comprising an Objective-C message.
20072 objc-selector-args , objc-comma-args
20074 objc-selector-args:
20075 objc-selector [opt] : assignment-expression
20076 objc-selector-args objc-selector [opt] : assignment-expression
20079 assignment-expression
20080 objc-comma-args , assignment-expression
20082 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20083 selector arguments and TREE_VALUE containing a list of comma
20087 cp_parser_objc_message_args (cp_parser* parser)
20089 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20090 bool maybe_unary_selector_p = true;
20091 cp_token *token = cp_lexer_peek_token (parser->lexer);
20093 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20095 tree selector = NULL_TREE, arg;
20097 if (token->type != CPP_COLON)
20098 selector = cp_parser_objc_selector (parser);
20100 /* Detect if we have a unary selector. */
20101 if (maybe_unary_selector_p
20102 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20103 return build_tree_list (selector, NULL_TREE);
20105 maybe_unary_selector_p = false;
20106 cp_parser_require (parser, CPP_COLON, "%<:%>");
20107 arg = cp_parser_assignment_expression (parser, false, NULL);
20110 = chainon (sel_args,
20111 build_tree_list (selector, arg));
20113 token = cp_lexer_peek_token (parser->lexer);
20116 /* Handle non-selector arguments, if any. */
20117 while (token->type == CPP_COMMA)
20121 cp_lexer_consume_token (parser->lexer);
20122 arg = cp_parser_assignment_expression (parser, false, NULL);
20125 = chainon (addl_args,
20126 build_tree_list (NULL_TREE, arg));
20128 token = cp_lexer_peek_token (parser->lexer);
20131 return build_tree_list (sel_args, addl_args);
20134 /* Parse an Objective-C encode expression.
20136 objc-encode-expression:
20137 @encode objc-typename
20139 Returns an encoded representation of the type argument. */
20142 cp_parser_objc_encode_expression (cp_parser* parser)
20147 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20148 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20149 token = cp_lexer_peek_token (parser->lexer);
20150 type = complete_type (cp_parser_type_id (parser));
20151 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20155 error_at (token->location,
20156 "%<@encode%> must specify a type as an argument");
20157 return error_mark_node;
20160 return objc_build_encode_expr (type);
20163 /* Parse an Objective-C @defs expression. */
20166 cp_parser_objc_defs_expression (cp_parser *parser)
20170 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20171 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20172 name = cp_parser_identifier (parser);
20173 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20175 return objc_get_class_ivars (name);
20178 /* Parse an Objective-C protocol expression.
20180 objc-protocol-expression:
20181 @protocol ( identifier )
20183 Returns a representation of the protocol expression. */
20186 cp_parser_objc_protocol_expression (cp_parser* parser)
20190 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20191 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20192 proto = cp_parser_identifier (parser);
20193 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20195 return objc_build_protocol_expr (proto);
20198 /* Parse an Objective-C selector expression.
20200 objc-selector-expression:
20201 @selector ( objc-method-signature )
20203 objc-method-signature:
20209 objc-selector-seq objc-selector :
20211 Returns a representation of the method selector. */
20214 cp_parser_objc_selector_expression (cp_parser* parser)
20216 tree sel_seq = NULL_TREE;
20217 bool maybe_unary_selector_p = true;
20219 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20221 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20222 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20223 token = cp_lexer_peek_token (parser->lexer);
20225 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20226 || token->type == CPP_SCOPE)
20228 tree selector = NULL_TREE;
20230 if (token->type != CPP_COLON
20231 || token->type == CPP_SCOPE)
20232 selector = cp_parser_objc_selector (parser);
20234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20235 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20237 /* Detect if we have a unary selector. */
20238 if (maybe_unary_selector_p)
20240 sel_seq = selector;
20241 goto finish_selector;
20245 cp_parser_error (parser, "expected %<:%>");
20248 maybe_unary_selector_p = false;
20249 token = cp_lexer_consume_token (parser->lexer);
20251 if (token->type == CPP_SCOPE)
20254 = chainon (sel_seq,
20255 build_tree_list (selector, NULL_TREE));
20257 = chainon (sel_seq,
20258 build_tree_list (NULL_TREE, NULL_TREE));
20262 = chainon (sel_seq,
20263 build_tree_list (selector, NULL_TREE));
20265 token = cp_lexer_peek_token (parser->lexer);
20269 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20271 return objc_build_selector_expr (loc, sel_seq);
20274 /* Parse a list of identifiers.
20276 objc-identifier-list:
20278 objc-identifier-list , identifier
20280 Returns a TREE_LIST of identifier nodes. */
20283 cp_parser_objc_identifier_list (cp_parser* parser)
20285 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20286 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20288 while (sep->type == CPP_COMMA)
20290 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20291 list = chainon (list,
20292 build_tree_list (NULL_TREE,
20293 cp_parser_identifier (parser)));
20294 sep = cp_lexer_peek_token (parser->lexer);
20300 /* Parse an Objective-C alias declaration.
20302 objc-alias-declaration:
20303 @compatibility_alias identifier identifier ;
20305 This function registers the alias mapping with the Objective-C front end.
20306 It returns nothing. */
20309 cp_parser_objc_alias_declaration (cp_parser* parser)
20313 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20314 alias = cp_parser_identifier (parser);
20315 orig = cp_parser_identifier (parser);
20316 objc_declare_alias (alias, orig);
20317 cp_parser_consume_semicolon_at_end_of_statement (parser);
20320 /* Parse an Objective-C class forward-declaration.
20322 objc-class-declaration:
20323 @class objc-identifier-list ;
20325 The function registers the forward declarations with the Objective-C
20326 front end. It returns nothing. */
20329 cp_parser_objc_class_declaration (cp_parser* parser)
20331 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20332 objc_declare_class (cp_parser_objc_identifier_list (parser));
20333 cp_parser_consume_semicolon_at_end_of_statement (parser);
20336 /* Parse a list of Objective-C protocol references.
20338 objc-protocol-refs-opt:
20339 objc-protocol-refs [opt]
20341 objc-protocol-refs:
20342 < objc-identifier-list >
20344 Returns a TREE_LIST of identifiers, if any. */
20347 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20349 tree protorefs = NULL_TREE;
20351 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20353 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20354 protorefs = cp_parser_objc_identifier_list (parser);
20355 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20361 /* Parse a Objective-C visibility specification. */
20364 cp_parser_objc_visibility_spec (cp_parser* parser)
20366 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20368 switch (vis->keyword)
20370 case RID_AT_PRIVATE:
20371 objc_set_visibility (2);
20373 case RID_AT_PROTECTED:
20374 objc_set_visibility (0);
20376 case RID_AT_PUBLIC:
20377 objc_set_visibility (1);
20383 /* Eat '@private'/'@protected'/'@public'. */
20384 cp_lexer_consume_token (parser->lexer);
20387 /* Parse an Objective-C method type. */
20390 cp_parser_objc_method_type (cp_parser* parser)
20392 objc_set_method_type
20393 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20398 /* Parse an Objective-C protocol qualifier. */
20401 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20403 tree quals = NULL_TREE, node;
20404 cp_token *token = cp_lexer_peek_token (parser->lexer);
20406 node = token->u.value;
20408 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20409 && (node == ridpointers [(int) RID_IN]
20410 || node == ridpointers [(int) RID_OUT]
20411 || node == ridpointers [(int) RID_INOUT]
20412 || node == ridpointers [(int) RID_BYCOPY]
20413 || node == ridpointers [(int) RID_BYREF]
20414 || node == ridpointers [(int) RID_ONEWAY]))
20416 quals = tree_cons (NULL_TREE, node, quals);
20417 cp_lexer_consume_token (parser->lexer);
20418 token = cp_lexer_peek_token (parser->lexer);
20419 node = token->u.value;
20425 /* Parse an Objective-C typename. */
20428 cp_parser_objc_typename (cp_parser* parser)
20430 tree type_name = NULL_TREE;
20432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20434 tree proto_quals, cp_type = NULL_TREE;
20436 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20437 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20439 /* An ObjC type name may consist of just protocol qualifiers, in which
20440 case the type shall default to 'id'. */
20441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20442 cp_type = cp_parser_type_id (parser);
20444 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20445 type_name = build_tree_list (proto_quals, cp_type);
20451 /* Check to see if TYPE refers to an Objective-C selector name. */
20454 cp_parser_objc_selector_p (enum cpp_ttype type)
20456 return (type == CPP_NAME || type == CPP_KEYWORD
20457 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20458 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20459 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20460 || type == CPP_XOR || type == CPP_XOR_EQ);
20463 /* Parse an Objective-C selector. */
20466 cp_parser_objc_selector (cp_parser* parser)
20468 cp_token *token = cp_lexer_consume_token (parser->lexer);
20470 if (!cp_parser_objc_selector_p (token->type))
20472 error_at (token->location, "invalid Objective-C++ selector name");
20473 return error_mark_node;
20476 /* C++ operator names are allowed to appear in ObjC selectors. */
20477 switch (token->type)
20479 case CPP_AND_AND: return get_identifier ("and");
20480 case CPP_AND_EQ: return get_identifier ("and_eq");
20481 case CPP_AND: return get_identifier ("bitand");
20482 case CPP_OR: return get_identifier ("bitor");
20483 case CPP_COMPL: return get_identifier ("compl");
20484 case CPP_NOT: return get_identifier ("not");
20485 case CPP_NOT_EQ: return get_identifier ("not_eq");
20486 case CPP_OR_OR: return get_identifier ("or");
20487 case CPP_OR_EQ: return get_identifier ("or_eq");
20488 case CPP_XOR: return get_identifier ("xor");
20489 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20490 default: return token->u.value;
20494 /* Parse an Objective-C params list. */
20497 cp_parser_objc_method_keyword_params (cp_parser* parser)
20499 tree params = NULL_TREE;
20500 bool maybe_unary_selector_p = true;
20501 cp_token *token = cp_lexer_peek_token (parser->lexer);
20503 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20505 tree selector = NULL_TREE, type_name, identifier;
20507 if (token->type != CPP_COLON)
20508 selector = cp_parser_objc_selector (parser);
20510 /* Detect if we have a unary selector. */
20511 if (maybe_unary_selector_p
20512 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20515 maybe_unary_selector_p = false;
20516 cp_parser_require (parser, CPP_COLON, "%<:%>");
20517 type_name = cp_parser_objc_typename (parser);
20518 identifier = cp_parser_identifier (parser);
20522 objc_build_keyword_decl (selector,
20526 token = cp_lexer_peek_token (parser->lexer);
20532 /* Parse the non-keyword Objective-C params. */
20535 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20537 tree params = make_node (TREE_LIST);
20538 cp_token *token = cp_lexer_peek_token (parser->lexer);
20539 *ellipsisp = false; /* Initially, assume no ellipsis. */
20541 while (token->type == CPP_COMMA)
20543 cp_parameter_declarator *parmdecl;
20546 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20547 token = cp_lexer_peek_token (parser->lexer);
20549 if (token->type == CPP_ELLIPSIS)
20551 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20556 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20557 parm = grokdeclarator (parmdecl->declarator,
20558 &parmdecl->decl_specifiers,
20559 PARM, /*initialized=*/0,
20560 /*attrlist=*/NULL);
20562 chainon (params, build_tree_list (NULL_TREE, parm));
20563 token = cp_lexer_peek_token (parser->lexer);
20569 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20572 cp_parser_objc_interstitial_code (cp_parser* parser)
20574 cp_token *token = cp_lexer_peek_token (parser->lexer);
20576 /* If the next token is `extern' and the following token is a string
20577 literal, then we have a linkage specification. */
20578 if (token->keyword == RID_EXTERN
20579 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20580 cp_parser_linkage_specification (parser);
20581 /* Handle #pragma, if any. */
20582 else if (token->type == CPP_PRAGMA)
20583 cp_parser_pragma (parser, pragma_external);
20584 /* Allow stray semicolons. */
20585 else if (token->type == CPP_SEMICOLON)
20586 cp_lexer_consume_token (parser->lexer);
20587 /* Finally, try to parse a block-declaration, or a function-definition. */
20589 cp_parser_block_declaration (parser, /*statement_p=*/false);
20592 /* Parse a method signature. */
20595 cp_parser_objc_method_signature (cp_parser* parser)
20597 tree rettype, kwdparms, optparms;
20598 bool ellipsis = false;
20600 cp_parser_objc_method_type (parser);
20601 rettype = cp_parser_objc_typename (parser);
20602 kwdparms = cp_parser_objc_method_keyword_params (parser);
20603 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20605 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20608 /* Pars an Objective-C method prototype list. */
20611 cp_parser_objc_method_prototype_list (cp_parser* parser)
20613 cp_token *token = cp_lexer_peek_token (parser->lexer);
20615 while (token->keyword != RID_AT_END)
20617 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20619 objc_add_method_declaration
20620 (cp_parser_objc_method_signature (parser));
20621 cp_parser_consume_semicolon_at_end_of_statement (parser);
20624 /* Allow for interspersed non-ObjC++ code. */
20625 cp_parser_objc_interstitial_code (parser);
20627 token = cp_lexer_peek_token (parser->lexer);
20630 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20631 objc_finish_interface ();
20634 /* Parse an Objective-C method definition list. */
20637 cp_parser_objc_method_definition_list (cp_parser* parser)
20639 cp_token *token = cp_lexer_peek_token (parser->lexer);
20641 while (token->keyword != RID_AT_END)
20645 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20647 push_deferring_access_checks (dk_deferred);
20648 objc_start_method_definition
20649 (cp_parser_objc_method_signature (parser));
20651 /* For historical reasons, we accept an optional semicolon. */
20652 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20653 cp_lexer_consume_token (parser->lexer);
20655 perform_deferred_access_checks ();
20656 stop_deferring_access_checks ();
20657 meth = cp_parser_function_definition_after_declarator (parser,
20659 pop_deferring_access_checks ();
20660 objc_finish_method_definition (meth);
20663 /* Allow for interspersed non-ObjC++ code. */
20664 cp_parser_objc_interstitial_code (parser);
20666 token = cp_lexer_peek_token (parser->lexer);
20669 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20670 objc_finish_implementation ();
20673 /* Parse Objective-C ivars. */
20676 cp_parser_objc_class_ivars (cp_parser* parser)
20678 cp_token *token = cp_lexer_peek_token (parser->lexer);
20680 if (token->type != CPP_OPEN_BRACE)
20681 return; /* No ivars specified. */
20683 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20684 token = cp_lexer_peek_token (parser->lexer);
20686 while (token->type != CPP_CLOSE_BRACE)
20688 cp_decl_specifier_seq declspecs;
20689 int decl_class_or_enum_p;
20690 tree prefix_attributes;
20692 cp_parser_objc_visibility_spec (parser);
20694 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20697 cp_parser_decl_specifier_seq (parser,
20698 CP_PARSER_FLAGS_OPTIONAL,
20700 &decl_class_or_enum_p);
20701 prefix_attributes = declspecs.attributes;
20702 declspecs.attributes = NULL_TREE;
20704 /* Keep going until we hit the `;' at the end of the
20706 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20708 tree width = NULL_TREE, attributes, first_attribute, decl;
20709 cp_declarator *declarator = NULL;
20710 int ctor_dtor_or_conv_p;
20712 /* Check for a (possibly unnamed) bitfield declaration. */
20713 token = cp_lexer_peek_token (parser->lexer);
20714 if (token->type == CPP_COLON)
20717 if (token->type == CPP_NAME
20718 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20721 /* Get the name of the bitfield. */
20722 declarator = make_id_declarator (NULL_TREE,
20723 cp_parser_identifier (parser),
20727 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20728 /* Get the width of the bitfield. */
20730 = cp_parser_constant_expression (parser,
20731 /*allow_non_constant=*/false,
20736 /* Parse the declarator. */
20738 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20739 &ctor_dtor_or_conv_p,
20740 /*parenthesized_p=*/NULL,
20741 /*member_p=*/false);
20744 /* Look for attributes that apply to the ivar. */
20745 attributes = cp_parser_attributes_opt (parser);
20746 /* Remember which attributes are prefix attributes and
20748 first_attribute = attributes;
20749 /* Combine the attributes. */
20750 attributes = chainon (prefix_attributes, attributes);
20753 /* Create the bitfield declaration. */
20754 decl = grokbitfield (declarator, &declspecs,
20758 decl = grokfield (declarator, &declspecs,
20759 NULL_TREE, /*init_const_expr_p=*/false,
20760 NULL_TREE, attributes);
20762 /* Add the instance variable. */
20763 objc_add_instance_variable (decl);
20765 /* Reset PREFIX_ATTRIBUTES. */
20766 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20767 attributes = TREE_CHAIN (attributes);
20769 TREE_CHAIN (attributes) = NULL_TREE;
20771 token = cp_lexer_peek_token (parser->lexer);
20773 if (token->type == CPP_COMMA)
20775 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20781 cp_parser_consume_semicolon_at_end_of_statement (parser);
20782 token = cp_lexer_peek_token (parser->lexer);
20785 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20786 /* For historical reasons, we accept an optional semicolon. */
20787 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20788 cp_lexer_consume_token (parser->lexer);
20791 /* Parse an Objective-C protocol declaration. */
20794 cp_parser_objc_protocol_declaration (cp_parser* parser)
20796 tree proto, protorefs;
20799 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20802 tok = cp_lexer_peek_token (parser->lexer);
20803 error_at (tok->location, "identifier expected after %<@protocol%>");
20807 /* See if we have a forward declaration or a definition. */
20808 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20810 /* Try a forward declaration first. */
20811 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20813 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20815 cp_parser_consume_semicolon_at_end_of_statement (parser);
20818 /* Ok, we got a full-fledged definition (or at least should). */
20821 proto = cp_parser_identifier (parser);
20822 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20823 objc_start_protocol (proto, protorefs);
20824 cp_parser_objc_method_prototype_list (parser);
20828 /* Parse an Objective-C superclass or category. */
20831 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20834 cp_token *next = cp_lexer_peek_token (parser->lexer);
20836 *super = *categ = NULL_TREE;
20837 if (next->type == CPP_COLON)
20839 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20840 *super = cp_parser_identifier (parser);
20842 else if (next->type == CPP_OPEN_PAREN)
20844 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20845 *categ = cp_parser_identifier (parser);
20846 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20850 /* Parse an Objective-C class interface. */
20853 cp_parser_objc_class_interface (cp_parser* parser)
20855 tree name, super, categ, protos;
20857 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20858 name = cp_parser_identifier (parser);
20859 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20860 protos = cp_parser_objc_protocol_refs_opt (parser);
20862 /* We have either a class or a category on our hands. */
20864 objc_start_category_interface (name, categ, protos);
20867 objc_start_class_interface (name, super, protos);
20868 /* Handle instance variable declarations, if any. */
20869 cp_parser_objc_class_ivars (parser);
20870 objc_continue_interface ();
20873 cp_parser_objc_method_prototype_list (parser);
20876 /* Parse an Objective-C class implementation. */
20879 cp_parser_objc_class_implementation (cp_parser* parser)
20881 tree name, super, categ;
20883 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20884 name = cp_parser_identifier (parser);
20885 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20887 /* We have either a class or a category on our hands. */
20889 objc_start_category_implementation (name, categ);
20892 objc_start_class_implementation (name, super);
20893 /* Handle instance variable declarations, if any. */
20894 cp_parser_objc_class_ivars (parser);
20895 objc_continue_implementation ();
20898 cp_parser_objc_method_definition_list (parser);
20901 /* Consume the @end token and finish off the implementation. */
20904 cp_parser_objc_end_implementation (cp_parser* parser)
20906 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20907 objc_finish_implementation ();
20910 /* Parse an Objective-C declaration. */
20913 cp_parser_objc_declaration (cp_parser* parser)
20915 /* Try to figure out what kind of declaration is present. */
20916 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20918 switch (kwd->keyword)
20921 cp_parser_objc_alias_declaration (parser);
20924 cp_parser_objc_class_declaration (parser);
20926 case RID_AT_PROTOCOL:
20927 cp_parser_objc_protocol_declaration (parser);
20929 case RID_AT_INTERFACE:
20930 cp_parser_objc_class_interface (parser);
20932 case RID_AT_IMPLEMENTATION:
20933 cp_parser_objc_class_implementation (parser);
20936 cp_parser_objc_end_implementation (parser);
20939 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20941 cp_parser_skip_to_end_of_block_or_statement (parser);
20945 /* Parse an Objective-C try-catch-finally statement.
20947 objc-try-catch-finally-stmt:
20948 @try compound-statement objc-catch-clause-seq [opt]
20949 objc-finally-clause [opt]
20951 objc-catch-clause-seq:
20952 objc-catch-clause objc-catch-clause-seq [opt]
20955 @catch ( exception-declaration ) compound-statement
20957 objc-finally-clause
20958 @finally compound-statement
20960 Returns NULL_TREE. */
20963 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20964 location_t location;
20967 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20968 location = cp_lexer_peek_token (parser->lexer)->location;
20969 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20970 node, lest it get absorbed into the surrounding block. */
20971 stmt = push_stmt_list ();
20972 cp_parser_compound_statement (parser, NULL, false);
20973 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20975 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20977 cp_parameter_declarator *parmdecl;
20980 cp_lexer_consume_token (parser->lexer);
20981 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20982 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20983 parm = grokdeclarator (parmdecl->declarator,
20984 &parmdecl->decl_specifiers,
20985 PARM, /*initialized=*/0,
20986 /*attrlist=*/NULL);
20987 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20988 objc_begin_catch_clause (parm);
20989 cp_parser_compound_statement (parser, NULL, false);
20990 objc_finish_catch_clause ();
20993 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20995 cp_lexer_consume_token (parser->lexer);
20996 location = cp_lexer_peek_token (parser->lexer)->location;
20997 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20998 node, lest it get absorbed into the surrounding block. */
20999 stmt = push_stmt_list ();
21000 cp_parser_compound_statement (parser, NULL, false);
21001 objc_build_finally_clause (location, pop_stmt_list (stmt));
21004 return objc_finish_try_stmt ();
21007 /* Parse an Objective-C synchronized statement.
21009 objc-synchronized-stmt:
21010 @synchronized ( expression ) compound-statement
21012 Returns NULL_TREE. */
21015 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21016 location_t location;
21019 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21021 location = cp_lexer_peek_token (parser->lexer)->location;
21022 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21023 lock = cp_parser_expression (parser, false, NULL);
21024 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21026 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21027 node, lest it get absorbed into the surrounding block. */
21028 stmt = push_stmt_list ();
21029 cp_parser_compound_statement (parser, NULL, false);
21031 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21034 /* Parse an Objective-C throw statement.
21037 @throw assignment-expression [opt] ;
21039 Returns a constructed '@throw' statement. */
21042 cp_parser_objc_throw_statement (cp_parser *parser) {
21043 tree expr = NULL_TREE;
21044 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21046 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21049 expr = cp_parser_assignment_expression (parser, false, NULL);
21051 cp_parser_consume_semicolon_at_end_of_statement (parser);
21053 return objc_build_throw_stmt (loc, expr);
21056 /* Parse an Objective-C statement. */
21059 cp_parser_objc_statement (cp_parser * parser) {
21060 /* Try to figure out what kind of declaration is present. */
21061 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21063 switch (kwd->keyword)
21066 return cp_parser_objc_try_catch_finally_statement (parser);
21067 case RID_AT_SYNCHRONIZED:
21068 return cp_parser_objc_synchronized_statement (parser);
21070 return cp_parser_objc_throw_statement (parser);
21072 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21074 cp_parser_skip_to_end_of_block_or_statement (parser);
21077 return error_mark_node;
21080 /* OpenMP 2.5 parsing routines. */
21082 /* Returns name of the next clause.
21083 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21084 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21085 returned and the token is consumed. */
21087 static pragma_omp_clause
21088 cp_parser_omp_clause_name (cp_parser *parser)
21090 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21093 result = PRAGMA_OMP_CLAUSE_IF;
21094 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21095 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21096 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21097 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21098 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21100 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21101 const char *p = IDENTIFIER_POINTER (id);
21106 if (!strcmp ("collapse", p))
21107 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21108 else if (!strcmp ("copyin", p))
21109 result = PRAGMA_OMP_CLAUSE_COPYIN;
21110 else if (!strcmp ("copyprivate", p))
21111 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21114 if (!strcmp ("firstprivate", p))
21115 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21118 if (!strcmp ("lastprivate", p))
21119 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21122 if (!strcmp ("nowait", p))
21123 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21124 else if (!strcmp ("num_threads", p))
21125 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21128 if (!strcmp ("ordered", p))
21129 result = PRAGMA_OMP_CLAUSE_ORDERED;
21132 if (!strcmp ("reduction", p))
21133 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21136 if (!strcmp ("schedule", p))
21137 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21138 else if (!strcmp ("shared", p))
21139 result = PRAGMA_OMP_CLAUSE_SHARED;
21142 if (!strcmp ("untied", p))
21143 result = PRAGMA_OMP_CLAUSE_UNTIED;
21148 if (result != PRAGMA_OMP_CLAUSE_NONE)
21149 cp_lexer_consume_token (parser->lexer);
21154 /* Validate that a clause of the given type does not already exist. */
21157 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21158 const char *name, location_t location)
21162 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21163 if (OMP_CLAUSE_CODE (c) == code)
21165 error_at (location, "too many %qs clauses", name);
21173 variable-list , identifier
21175 In addition, we match a closing parenthesis. An opening parenthesis
21176 will have been consumed by the caller.
21178 If KIND is nonzero, create the appropriate node and install the decl
21179 in OMP_CLAUSE_DECL and add the node to the head of the list.
21181 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21182 return the list created. */
21185 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21193 token = cp_lexer_peek_token (parser->lexer);
21194 name = cp_parser_id_expression (parser, /*template_p=*/false,
21195 /*check_dependency_p=*/true,
21196 /*template_p=*/NULL,
21197 /*declarator_p=*/false,
21198 /*optional_p=*/false);
21199 if (name == error_mark_node)
21202 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21203 if (decl == error_mark_node)
21204 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21205 else if (kind != 0)
21207 tree u = build_omp_clause (token->location, kind);
21208 OMP_CLAUSE_DECL (u) = decl;
21209 OMP_CLAUSE_CHAIN (u) = list;
21213 list = tree_cons (decl, NULL_TREE, list);
21216 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21218 cp_lexer_consume_token (parser->lexer);
21221 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21225 /* Try to resync to an unnested comma. Copied from
21226 cp_parser_parenthesized_expression_list. */
21228 ending = cp_parser_skip_to_closing_parenthesis (parser,
21229 /*recovering=*/true,
21231 /*consume_paren=*/true);
21239 /* Similarly, but expect leading and trailing parenthesis. This is a very
21240 common case for omp clauses. */
21243 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21245 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21246 return cp_parser_omp_var_list_no_open (parser, kind, list);
21251 collapse ( constant-expression ) */
21254 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21260 loc = cp_lexer_peek_token (parser->lexer)->location;
21261 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21264 num = cp_parser_constant_expression (parser, false, NULL);
21266 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21267 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21268 /*or_comma=*/false,
21269 /*consume_paren=*/true);
21271 if (num == error_mark_node)
21273 num = fold_non_dependent_expr (num);
21274 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21275 || !host_integerp (num, 0)
21276 || (n = tree_low_cst (num, 0)) <= 0
21279 error_at (loc, "collapse argument needs positive constant integer expression");
21283 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21284 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21285 OMP_CLAUSE_CHAIN (c) = list;
21286 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21292 default ( shared | none ) */
21295 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21297 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21300 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21302 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21304 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21305 const char *p = IDENTIFIER_POINTER (id);
21310 if (strcmp ("none", p) != 0)
21312 kind = OMP_CLAUSE_DEFAULT_NONE;
21316 if (strcmp ("shared", p) != 0)
21318 kind = OMP_CLAUSE_DEFAULT_SHARED;
21325 cp_lexer_consume_token (parser->lexer);
21330 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21333 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21334 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21335 /*or_comma=*/false,
21336 /*consume_paren=*/true);
21338 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21341 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21342 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21343 OMP_CLAUSE_CHAIN (c) = list;
21344 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21350 if ( expression ) */
21353 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21357 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21360 t = cp_parser_condition (parser);
21362 if (t == error_mark_node
21363 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21364 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21365 /*or_comma=*/false,
21366 /*consume_paren=*/true);
21368 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21370 c = build_omp_clause (location, OMP_CLAUSE_IF);
21371 OMP_CLAUSE_IF_EXPR (c) = t;
21372 OMP_CLAUSE_CHAIN (c) = list;
21381 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21382 tree list, location_t location)
21386 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21388 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21389 OMP_CLAUSE_CHAIN (c) = list;
21394 num_threads ( expression ) */
21397 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21398 location_t location)
21402 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21405 t = cp_parser_expression (parser, false, NULL);
21407 if (t == error_mark_node
21408 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21409 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21410 /*or_comma=*/false,
21411 /*consume_paren=*/true);
21413 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21414 "num_threads", location);
21416 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21417 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21418 OMP_CLAUSE_CHAIN (c) = list;
21427 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21428 tree list, location_t location)
21432 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21433 "ordered", location);
21435 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21436 OMP_CLAUSE_CHAIN (c) = list;
21441 reduction ( reduction-operator : variable-list )
21443 reduction-operator:
21444 One of: + * - & ^ | && || */
21447 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21449 enum tree_code code;
21452 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21455 switch (cp_lexer_peek_token (parser->lexer)->type)
21467 code = BIT_AND_EXPR;
21470 code = BIT_XOR_EXPR;
21473 code = BIT_IOR_EXPR;
21476 code = TRUTH_ANDIF_EXPR;
21479 code = TRUTH_ORIF_EXPR;
21482 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21483 "%<|%>, %<&&%>, or %<||%>");
21485 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21486 /*or_comma=*/false,
21487 /*consume_paren=*/true);
21490 cp_lexer_consume_token (parser->lexer);
21492 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21495 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21496 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21497 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21503 schedule ( schedule-kind )
21504 schedule ( schedule-kind , expression )
21507 static | dynamic | guided | runtime | auto */
21510 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21514 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21517 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21519 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21521 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21522 const char *p = IDENTIFIER_POINTER (id);
21527 if (strcmp ("dynamic", p) != 0)
21529 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21533 if (strcmp ("guided", p) != 0)
21535 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21539 if (strcmp ("runtime", p) != 0)
21541 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21548 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21549 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21550 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21551 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21554 cp_lexer_consume_token (parser->lexer);
21556 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21559 cp_lexer_consume_token (parser->lexer);
21561 token = cp_lexer_peek_token (parser->lexer);
21562 t = cp_parser_assignment_expression (parser, false, NULL);
21564 if (t == error_mark_node)
21566 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21567 error_at (token->location, "schedule %<runtime%> does not take "
21568 "a %<chunk_size%> parameter");
21569 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21570 error_at (token->location, "schedule %<auto%> does not take "
21571 "a %<chunk_size%> parameter");
21573 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21575 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21578 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21581 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21582 OMP_CLAUSE_CHAIN (c) = list;
21586 cp_parser_error (parser, "invalid schedule kind");
21588 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21589 /*or_comma=*/false,
21590 /*consume_paren=*/true);
21598 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21599 tree list, location_t location)
21603 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21605 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21606 OMP_CLAUSE_CHAIN (c) = list;
21610 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21611 is a bitmask in MASK. Return the list of clauses found; the result
21612 of clause default goes in *pdefault. */
21615 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21616 const char *where, cp_token *pragma_tok)
21618 tree clauses = NULL;
21620 cp_token *token = NULL;
21622 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21624 pragma_omp_clause c_kind;
21625 const char *c_name;
21626 tree prev = clauses;
21628 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21629 cp_lexer_consume_token (parser->lexer);
21631 token = cp_lexer_peek_token (parser->lexer);
21632 c_kind = cp_parser_omp_clause_name (parser);
21637 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21638 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21640 c_name = "collapse";
21642 case PRAGMA_OMP_CLAUSE_COPYIN:
21643 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21646 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21647 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21649 c_name = "copyprivate";
21651 case PRAGMA_OMP_CLAUSE_DEFAULT:
21652 clauses = cp_parser_omp_clause_default (parser, clauses,
21654 c_name = "default";
21656 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21657 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21659 c_name = "firstprivate";
21661 case PRAGMA_OMP_CLAUSE_IF:
21662 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21665 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21668 c_name = "lastprivate";
21670 case PRAGMA_OMP_CLAUSE_NOWAIT:
21671 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21674 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21675 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21677 c_name = "num_threads";
21679 case PRAGMA_OMP_CLAUSE_ORDERED:
21680 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21682 c_name = "ordered";
21684 case PRAGMA_OMP_CLAUSE_PRIVATE:
21685 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21687 c_name = "private";
21689 case PRAGMA_OMP_CLAUSE_REDUCTION:
21690 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21691 c_name = "reduction";
21693 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21694 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21696 c_name = "schedule";
21698 case PRAGMA_OMP_CLAUSE_SHARED:
21699 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21703 case PRAGMA_OMP_CLAUSE_UNTIED:
21704 clauses = cp_parser_omp_clause_untied (parser, clauses,
21709 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21713 if (((mask >> c_kind) & 1) == 0)
21715 /* Remove the invalid clause(s) from the list to avoid
21716 confusing the rest of the compiler. */
21718 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21722 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21723 return finish_omp_clauses (clauses);
21730 In practice, we're also interested in adding the statement to an
21731 outer node. So it is convenient if we work around the fact that
21732 cp_parser_statement calls add_stmt. */
21735 cp_parser_begin_omp_structured_block (cp_parser *parser)
21737 unsigned save = parser->in_statement;
21739 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21740 This preserves the "not within loop or switch" style error messages
21741 for nonsense cases like
21747 if (parser->in_statement)
21748 parser->in_statement = IN_OMP_BLOCK;
21754 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21756 parser->in_statement = save;
21760 cp_parser_omp_structured_block (cp_parser *parser)
21762 tree stmt = begin_omp_structured_block ();
21763 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21765 cp_parser_statement (parser, NULL_TREE, false, NULL);
21767 cp_parser_end_omp_structured_block (parser, save);
21768 return finish_omp_structured_block (stmt);
21772 # pragma omp atomic new-line
21776 x binop= expr | x++ | ++x | x-- | --x
21778 +, *, -, /, &, ^, |, <<, >>
21780 where x is an lvalue expression with scalar type. */
21783 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21786 enum tree_code code;
21788 cp_parser_require_pragma_eol (parser, pragma_tok);
21790 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21791 /*cast_p=*/false, NULL);
21792 switch (TREE_CODE (lhs))
21797 case PREINCREMENT_EXPR:
21798 case POSTINCREMENT_EXPR:
21799 lhs = TREE_OPERAND (lhs, 0);
21801 rhs = integer_one_node;
21804 case PREDECREMENT_EXPR:
21805 case POSTDECREMENT_EXPR:
21806 lhs = TREE_OPERAND (lhs, 0);
21808 rhs = integer_one_node;
21812 switch (cp_lexer_peek_token (parser->lexer)->type)
21818 code = TRUNC_DIV_EXPR;
21826 case CPP_LSHIFT_EQ:
21827 code = LSHIFT_EXPR;
21829 case CPP_RSHIFT_EQ:
21830 code = RSHIFT_EXPR;
21833 code = BIT_AND_EXPR;
21836 code = BIT_IOR_EXPR;
21839 code = BIT_XOR_EXPR;
21842 cp_parser_error (parser,
21843 "invalid operator for %<#pragma omp atomic%>");
21846 cp_lexer_consume_token (parser->lexer);
21848 rhs = cp_parser_expression (parser, false, NULL);
21849 if (rhs == error_mark_node)
21853 finish_omp_atomic (code, lhs, rhs);
21854 cp_parser_consume_semicolon_at_end_of_statement (parser);
21858 cp_parser_skip_to_end_of_block_or_statement (parser);
21863 # pragma omp barrier new-line */
21866 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21868 cp_parser_require_pragma_eol (parser, pragma_tok);
21869 finish_omp_barrier ();
21873 # pragma omp critical [(name)] new-line
21874 structured-block */
21877 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21879 tree stmt, name = NULL;
21881 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21883 cp_lexer_consume_token (parser->lexer);
21885 name = cp_parser_identifier (parser);
21887 if (name == error_mark_node
21888 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21889 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21890 /*or_comma=*/false,
21891 /*consume_paren=*/true);
21892 if (name == error_mark_node)
21895 cp_parser_require_pragma_eol (parser, pragma_tok);
21897 stmt = cp_parser_omp_structured_block (parser);
21898 return c_finish_omp_critical (input_location, stmt, name);
21902 # pragma omp flush flush-vars[opt] new-line
21905 ( variable-list ) */
21908 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21911 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21912 cp_parser_require_pragma_eol (parser, pragma_tok);
21914 finish_omp_flush ();
21917 /* Helper function, to parse omp for increment expression. */
21920 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21922 tree cond = cp_parser_binary_expression (parser, false, true,
21923 PREC_NOT_OPERATOR, NULL);
21926 if (cond == error_mark_node
21927 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21929 cp_parser_skip_to_end_of_statement (parser);
21930 return error_mark_node;
21933 switch (TREE_CODE (cond))
21941 return error_mark_node;
21944 /* If decl is an iterator, preserve LHS and RHS of the relational
21945 expr until finish_omp_for. */
21947 && (type_dependent_expression_p (decl)
21948 || CLASS_TYPE_P (TREE_TYPE (decl))))
21951 return build_x_binary_op (TREE_CODE (cond),
21952 TREE_OPERAND (cond, 0), ERROR_MARK,
21953 TREE_OPERAND (cond, 1), ERROR_MARK,
21954 &overloaded_p, tf_warning_or_error);
21957 /* Helper function, to parse omp for increment expression. */
21960 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21962 cp_token *token = cp_lexer_peek_token (parser->lexer);
21968 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21970 op = (token->type == CPP_PLUS_PLUS
21971 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21972 cp_lexer_consume_token (parser->lexer);
21973 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21975 return error_mark_node;
21976 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21979 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21981 return error_mark_node;
21983 token = cp_lexer_peek_token (parser->lexer);
21984 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21986 op = (token->type == CPP_PLUS_PLUS
21987 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21988 cp_lexer_consume_token (parser->lexer);
21989 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21992 op = cp_parser_assignment_operator_opt (parser);
21993 if (op == ERROR_MARK)
21994 return error_mark_node;
21996 if (op != NOP_EXPR)
21998 rhs = cp_parser_assignment_expression (parser, false, NULL);
21999 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22000 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22003 lhs = cp_parser_binary_expression (parser, false, false,
22004 PREC_ADDITIVE_EXPRESSION, NULL);
22005 token = cp_lexer_peek_token (parser->lexer);
22006 decl_first = lhs == decl;
22009 if (token->type != CPP_PLUS
22010 && token->type != CPP_MINUS)
22011 return error_mark_node;
22015 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22016 cp_lexer_consume_token (parser->lexer);
22017 rhs = cp_parser_binary_expression (parser, false, false,
22018 PREC_ADDITIVE_EXPRESSION, NULL);
22019 token = cp_lexer_peek_token (parser->lexer);
22020 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22022 if (lhs == NULL_TREE)
22024 if (op == PLUS_EXPR)
22027 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22030 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22031 NULL, tf_warning_or_error);
22034 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22038 if (rhs != decl || op == MINUS_EXPR)
22039 return error_mark_node;
22040 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22043 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22045 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22048 /* Parse the restricted form of the for statement allowed by OpenMP. */
22051 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22053 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22054 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22055 tree this_pre_body, cl;
22056 location_t loc_first;
22057 bool collapse_err = false;
22058 int i, collapse = 1, nbraces = 0;
22060 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22061 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22062 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22064 gcc_assert (collapse >= 1);
22066 declv = make_tree_vec (collapse);
22067 initv = make_tree_vec (collapse);
22068 condv = make_tree_vec (collapse);
22069 incrv = make_tree_vec (collapse);
22071 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22073 for (i = 0; i < collapse; i++)
22075 int bracecount = 0;
22076 bool add_private_clause = false;
22079 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22081 cp_parser_error (parser, "for statement expected");
22084 loc = cp_lexer_consume_token (parser->lexer)->location;
22086 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22089 init = decl = real_decl = NULL;
22090 this_pre_body = push_stmt_list ();
22091 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22093 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22097 integer-type var = lb
22098 random-access-iterator-type var = lb
22099 pointer-type var = lb
22101 cp_decl_specifier_seq type_specifiers;
22103 /* First, try to parse as an initialized declaration. See
22104 cp_parser_condition, from whence the bulk of this is copied. */
22106 cp_parser_parse_tentatively (parser);
22107 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22108 /*is_trailing_return=*/false,
22110 if (cp_parser_parse_definitely (parser))
22112 /* If parsing a type specifier seq succeeded, then this
22113 MUST be a initialized declaration. */
22114 tree asm_specification, attributes;
22115 cp_declarator *declarator;
22117 declarator = cp_parser_declarator (parser,
22118 CP_PARSER_DECLARATOR_NAMED,
22119 /*ctor_dtor_or_conv_p=*/NULL,
22120 /*parenthesized_p=*/NULL,
22121 /*member_p=*/false);
22122 attributes = cp_parser_attributes_opt (parser);
22123 asm_specification = cp_parser_asm_specification_opt (parser);
22125 if (declarator == cp_error_declarator)
22126 cp_parser_skip_to_end_of_statement (parser);
22130 tree pushed_scope, auto_node;
22132 decl = start_decl (declarator, &type_specifiers,
22133 SD_INITIALIZED, attributes,
22134 /*prefix_attributes=*/NULL_TREE,
22137 auto_node = type_uses_auto (TREE_TYPE (decl));
22138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22140 if (cp_lexer_next_token_is (parser->lexer,
22142 error ("parenthesized initialization is not allowed in "
22143 "OpenMP %<for%> loop");
22145 /* Trigger an error. */
22146 cp_parser_require (parser, CPP_EQ, "%<=%>");
22148 init = error_mark_node;
22149 cp_parser_skip_to_end_of_statement (parser);
22151 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22152 || type_dependent_expression_p (decl)
22155 bool is_direct_init, is_non_constant_init;
22157 init = cp_parser_initializer (parser,
22159 &is_non_constant_init);
22161 if (auto_node && describable_type (init))
22164 = do_auto_deduction (TREE_TYPE (decl), init,
22167 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22168 && !type_dependent_expression_p (decl))
22172 cp_finish_decl (decl, init, !is_non_constant_init,
22174 LOOKUP_ONLYCONVERTING);
22175 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22178 = tree_cons (NULL, this_pre_body, for_block);
22182 init = pop_stmt_list (this_pre_body);
22183 this_pre_body = NULL_TREE;
22188 cp_lexer_consume_token (parser->lexer);
22189 init = cp_parser_assignment_expression (parser, false, NULL);
22192 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22193 init = error_mark_node;
22195 cp_finish_decl (decl, NULL_TREE,
22196 /*init_const_expr_p=*/false,
22198 LOOKUP_ONLYCONVERTING);
22202 pop_scope (pushed_scope);
22208 /* If parsing a type specifier sequence failed, then
22209 this MUST be a simple expression. */
22210 cp_parser_parse_tentatively (parser);
22211 decl = cp_parser_primary_expression (parser, false, false,
22213 if (!cp_parser_error_occurred (parser)
22216 && CLASS_TYPE_P (TREE_TYPE (decl)))
22220 cp_parser_parse_definitely (parser);
22221 cp_parser_require (parser, CPP_EQ, "%<=%>");
22222 rhs = cp_parser_assignment_expression (parser, false, NULL);
22223 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22225 tf_warning_or_error));
22226 add_private_clause = true;
22231 cp_parser_abort_tentative_parse (parser);
22232 init = cp_parser_expression (parser, false, NULL);
22235 if (TREE_CODE (init) == MODIFY_EXPR
22236 || TREE_CODE (init) == MODOP_EXPR)
22237 real_decl = TREE_OPERAND (init, 0);
22242 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22245 this_pre_body = pop_stmt_list (this_pre_body);
22249 pre_body = push_stmt_list ();
22251 add_stmt (this_pre_body);
22252 pre_body = pop_stmt_list (pre_body);
22255 pre_body = this_pre_body;
22260 if (par_clauses != NULL && real_decl != NULL_TREE)
22263 for (c = par_clauses; *c ; )
22264 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22265 && OMP_CLAUSE_DECL (*c) == real_decl)
22267 error_at (loc, "iteration variable %qD"
22268 " should not be firstprivate", real_decl);
22269 *c = OMP_CLAUSE_CHAIN (*c);
22271 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22272 && OMP_CLAUSE_DECL (*c) == real_decl)
22274 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22275 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22276 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22277 OMP_CLAUSE_DECL (l) = real_decl;
22278 OMP_CLAUSE_CHAIN (l) = clauses;
22279 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22281 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22282 CP_OMP_CLAUSE_INFO (*c) = NULL;
22283 add_private_clause = false;
22287 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22288 && OMP_CLAUSE_DECL (*c) == real_decl)
22289 add_private_clause = false;
22290 c = &OMP_CLAUSE_CHAIN (*c);
22294 if (add_private_clause)
22297 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22299 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22300 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22301 && OMP_CLAUSE_DECL (c) == decl)
22303 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22304 && OMP_CLAUSE_DECL (c) == decl)
22305 error_at (loc, "iteration variable %qD "
22306 "should not be firstprivate",
22308 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22309 && OMP_CLAUSE_DECL (c) == decl)
22310 error_at (loc, "iteration variable %qD should not be reduction",
22315 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22316 OMP_CLAUSE_DECL (c) = decl;
22317 c = finish_omp_clauses (c);
22320 OMP_CLAUSE_CHAIN (c) = clauses;
22327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22328 cond = cp_parser_omp_for_cond (parser, decl);
22329 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22332 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22334 /* If decl is an iterator, preserve the operator on decl
22335 until finish_omp_for. */
22337 && (type_dependent_expression_p (decl)
22338 || CLASS_TYPE_P (TREE_TYPE (decl))))
22339 incr = cp_parser_omp_for_incr (parser, decl);
22341 incr = cp_parser_expression (parser, false, NULL);
22344 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22345 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22346 /*or_comma=*/false,
22347 /*consume_paren=*/true);
22349 TREE_VEC_ELT (declv, i) = decl;
22350 TREE_VEC_ELT (initv, i) = init;
22351 TREE_VEC_ELT (condv, i) = cond;
22352 TREE_VEC_ELT (incrv, i) = incr;
22354 if (i == collapse - 1)
22357 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22358 in between the collapsed for loops to be still considered perfectly
22359 nested. Hopefully the final version clarifies this.
22360 For now handle (multiple) {'s and empty statements. */
22361 cp_parser_parse_tentatively (parser);
22364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22366 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22368 cp_lexer_consume_token (parser->lexer);
22371 else if (bracecount
22372 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22373 cp_lexer_consume_token (parser->lexer);
22376 loc = cp_lexer_peek_token (parser->lexer)->location;
22377 error_at (loc, "not enough collapsed for loops");
22378 collapse_err = true;
22379 cp_parser_abort_tentative_parse (parser);
22388 cp_parser_parse_definitely (parser);
22389 nbraces += bracecount;
22393 /* Note that we saved the original contents of this flag when we entered
22394 the structured block, and so we don't need to re-save it here. */
22395 parser->in_statement = IN_OMP_FOR;
22397 /* Note that the grammar doesn't call for a structured block here,
22398 though the loop as a whole is a structured block. */
22399 body = push_stmt_list ();
22400 cp_parser_statement (parser, NULL_TREE, false, NULL);
22401 body = pop_stmt_list (body);
22403 if (declv == NULL_TREE)
22406 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22407 pre_body, clauses);
22411 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22413 cp_lexer_consume_token (parser->lexer);
22416 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22417 cp_lexer_consume_token (parser->lexer);
22422 error_at (cp_lexer_peek_token (parser->lexer)->location,
22423 "collapsed loops not perfectly nested");
22425 collapse_err = true;
22426 cp_parser_statement_seq_opt (parser, NULL);
22427 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22434 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22435 for_block = TREE_CHAIN (for_block);
22442 #pragma omp for for-clause[optseq] new-line
22445 #define OMP_FOR_CLAUSE_MASK \
22446 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22447 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22448 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22449 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22450 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22451 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22452 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22453 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22456 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22458 tree clauses, sb, ret;
22461 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22462 "#pragma omp for", pragma_tok);
22464 sb = begin_omp_structured_block ();
22465 save = cp_parser_begin_omp_structured_block (parser);
22467 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22469 cp_parser_end_omp_structured_block (parser, save);
22470 add_stmt (finish_omp_structured_block (sb));
22476 # pragma omp master new-line
22477 structured-block */
22480 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22482 cp_parser_require_pragma_eol (parser, pragma_tok);
22483 return c_finish_omp_master (input_location,
22484 cp_parser_omp_structured_block (parser));
22488 # pragma omp ordered new-line
22489 structured-block */
22492 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22494 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22495 cp_parser_require_pragma_eol (parser, pragma_tok);
22496 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22502 { section-sequence }
22505 section-directive[opt] structured-block
22506 section-sequence section-directive structured-block */
22509 cp_parser_omp_sections_scope (cp_parser *parser)
22511 tree stmt, substmt;
22512 bool error_suppress = false;
22515 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22518 stmt = push_stmt_list ();
22520 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22524 substmt = begin_omp_structured_block ();
22525 save = cp_parser_begin_omp_structured_block (parser);
22529 cp_parser_statement (parser, NULL_TREE, false, NULL);
22531 tok = cp_lexer_peek_token (parser->lexer);
22532 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22534 if (tok->type == CPP_CLOSE_BRACE)
22536 if (tok->type == CPP_EOF)
22540 cp_parser_end_omp_structured_block (parser, save);
22541 substmt = finish_omp_structured_block (substmt);
22542 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22543 add_stmt (substmt);
22548 tok = cp_lexer_peek_token (parser->lexer);
22549 if (tok->type == CPP_CLOSE_BRACE)
22551 if (tok->type == CPP_EOF)
22554 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22556 cp_lexer_consume_token (parser->lexer);
22557 cp_parser_require_pragma_eol (parser, tok);
22558 error_suppress = false;
22560 else if (!error_suppress)
22562 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22563 error_suppress = true;
22566 substmt = cp_parser_omp_structured_block (parser);
22567 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22568 add_stmt (substmt);
22570 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22572 substmt = pop_stmt_list (stmt);
22574 stmt = make_node (OMP_SECTIONS);
22575 TREE_TYPE (stmt) = void_type_node;
22576 OMP_SECTIONS_BODY (stmt) = substmt;
22583 # pragma omp sections sections-clause[optseq] newline
22586 #define OMP_SECTIONS_CLAUSE_MASK \
22587 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22588 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22589 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22590 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22591 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22594 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22598 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22599 "#pragma omp sections", pragma_tok);
22601 ret = cp_parser_omp_sections_scope (parser);
22603 OMP_SECTIONS_CLAUSES (ret) = clauses;
22609 # pragma parallel parallel-clause new-line
22610 # pragma parallel for parallel-for-clause new-line
22611 # pragma parallel sections parallel-sections-clause new-line */
22613 #define OMP_PARALLEL_CLAUSE_MASK \
22614 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22615 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22616 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22617 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22618 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22619 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22620 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22621 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22624 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22626 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22627 const char *p_name = "#pragma omp parallel";
22628 tree stmt, clauses, par_clause, ws_clause, block;
22629 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22631 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22633 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22635 cp_lexer_consume_token (parser->lexer);
22636 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22637 p_name = "#pragma omp parallel for";
22638 mask |= OMP_FOR_CLAUSE_MASK;
22639 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22641 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22644 const char *p = IDENTIFIER_POINTER (id);
22645 if (strcmp (p, "sections") == 0)
22647 cp_lexer_consume_token (parser->lexer);
22648 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22649 p_name = "#pragma omp parallel sections";
22650 mask |= OMP_SECTIONS_CLAUSE_MASK;
22651 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22655 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22656 block = begin_omp_parallel ();
22657 save = cp_parser_begin_omp_structured_block (parser);
22661 case PRAGMA_OMP_PARALLEL:
22662 cp_parser_statement (parser, NULL_TREE, false, NULL);
22663 par_clause = clauses;
22666 case PRAGMA_OMP_PARALLEL_FOR:
22667 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22668 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22671 case PRAGMA_OMP_PARALLEL_SECTIONS:
22672 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22673 stmt = cp_parser_omp_sections_scope (parser);
22675 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22679 gcc_unreachable ();
22682 cp_parser_end_omp_structured_block (parser, save);
22683 stmt = finish_omp_parallel (par_clause, block);
22684 if (p_kind != PRAGMA_OMP_PARALLEL)
22685 OMP_PARALLEL_COMBINED (stmt) = 1;
22690 # pragma omp single single-clause[optseq] new-line
22691 structured-block */
22693 #define OMP_SINGLE_CLAUSE_MASK \
22694 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22695 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22696 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22697 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22700 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22702 tree stmt = make_node (OMP_SINGLE);
22703 TREE_TYPE (stmt) = void_type_node;
22705 OMP_SINGLE_CLAUSES (stmt)
22706 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22707 "#pragma omp single", pragma_tok);
22708 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22710 return add_stmt (stmt);
22714 # pragma omp task task-clause[optseq] new-line
22715 structured-block */
22717 #define OMP_TASK_CLAUSE_MASK \
22718 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22719 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22720 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22721 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22722 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22723 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22726 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22728 tree clauses, block;
22731 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22732 "#pragma omp task", pragma_tok);
22733 block = begin_omp_task ();
22734 save = cp_parser_begin_omp_structured_block (parser);
22735 cp_parser_statement (parser, NULL_TREE, false, NULL);
22736 cp_parser_end_omp_structured_block (parser, save);
22737 return finish_omp_task (clauses, block);
22741 # pragma omp taskwait new-line */
22744 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22746 cp_parser_require_pragma_eol (parser, pragma_tok);
22747 finish_omp_taskwait ();
22751 # pragma omp threadprivate (variable-list) */
22754 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22758 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22759 cp_parser_require_pragma_eol (parser, pragma_tok);
22761 finish_omp_threadprivate (vars);
22764 /* Main entry point to OpenMP statement pragmas. */
22767 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22771 switch (pragma_tok->pragma_kind)
22773 case PRAGMA_OMP_ATOMIC:
22774 cp_parser_omp_atomic (parser, pragma_tok);
22776 case PRAGMA_OMP_CRITICAL:
22777 stmt = cp_parser_omp_critical (parser, pragma_tok);
22779 case PRAGMA_OMP_FOR:
22780 stmt = cp_parser_omp_for (parser, pragma_tok);
22782 case PRAGMA_OMP_MASTER:
22783 stmt = cp_parser_omp_master (parser, pragma_tok);
22785 case PRAGMA_OMP_ORDERED:
22786 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22788 case PRAGMA_OMP_PARALLEL:
22789 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22791 case PRAGMA_OMP_SECTIONS:
22792 stmt = cp_parser_omp_sections (parser, pragma_tok);
22794 case PRAGMA_OMP_SINGLE:
22795 stmt = cp_parser_omp_single (parser, pragma_tok);
22797 case PRAGMA_OMP_TASK:
22798 stmt = cp_parser_omp_task (parser, pragma_tok);
22801 gcc_unreachable ();
22805 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22810 static GTY (()) cp_parser *the_parser;
22813 /* Special handling for the first token or line in the file. The first
22814 thing in the file might be #pragma GCC pch_preprocess, which loads a
22815 PCH file, which is a GC collection point. So we need to handle this
22816 first pragma without benefit of an existing lexer structure.
22818 Always returns one token to the caller in *FIRST_TOKEN. This is
22819 either the true first token of the file, or the first token after
22820 the initial pragma. */
22823 cp_parser_initial_pragma (cp_token *first_token)
22827 cp_lexer_get_preprocessor_token (NULL, first_token);
22828 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22831 cp_lexer_get_preprocessor_token (NULL, first_token);
22832 if (first_token->type == CPP_STRING)
22834 name = first_token->u.value;
22836 cp_lexer_get_preprocessor_token (NULL, first_token);
22837 if (first_token->type != CPP_PRAGMA_EOL)
22838 error_at (first_token->location,
22839 "junk at end of %<#pragma GCC pch_preprocess%>");
22842 error_at (first_token->location, "expected string literal");
22844 /* Skip to the end of the pragma. */
22845 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22846 cp_lexer_get_preprocessor_token (NULL, first_token);
22848 /* Now actually load the PCH file. */
22850 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22852 /* Read one more token to return to our caller. We have to do this
22853 after reading the PCH file in, since its pointers have to be
22855 cp_lexer_get_preprocessor_token (NULL, first_token);
22858 /* Normal parsing of a pragma token. Here we can (and must) use the
22862 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22864 cp_token *pragma_tok;
22867 pragma_tok = cp_lexer_consume_token (parser->lexer);
22868 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22869 parser->lexer->in_pragma = true;
22871 id = pragma_tok->pragma_kind;
22874 case PRAGMA_GCC_PCH_PREPROCESS:
22875 error_at (pragma_tok->location,
22876 "%<#pragma GCC pch_preprocess%> must be first");
22879 case PRAGMA_OMP_BARRIER:
22882 case pragma_compound:
22883 cp_parser_omp_barrier (parser, pragma_tok);
22886 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22887 "used in compound statements");
22894 case PRAGMA_OMP_FLUSH:
22897 case pragma_compound:
22898 cp_parser_omp_flush (parser, pragma_tok);
22901 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22902 "used in compound statements");
22909 case PRAGMA_OMP_TASKWAIT:
22912 case pragma_compound:
22913 cp_parser_omp_taskwait (parser, pragma_tok);
22916 error_at (pragma_tok->location,
22917 "%<#pragma omp taskwait%> may only be "
22918 "used in compound statements");
22925 case PRAGMA_OMP_THREADPRIVATE:
22926 cp_parser_omp_threadprivate (parser, pragma_tok);
22929 case PRAGMA_OMP_ATOMIC:
22930 case PRAGMA_OMP_CRITICAL:
22931 case PRAGMA_OMP_FOR:
22932 case PRAGMA_OMP_MASTER:
22933 case PRAGMA_OMP_ORDERED:
22934 case PRAGMA_OMP_PARALLEL:
22935 case PRAGMA_OMP_SECTIONS:
22936 case PRAGMA_OMP_SINGLE:
22937 case PRAGMA_OMP_TASK:
22938 if (context == pragma_external)
22940 cp_parser_omp_construct (parser, pragma_tok);
22943 case PRAGMA_OMP_SECTION:
22944 error_at (pragma_tok->location,
22945 "%<#pragma omp section%> may only be used in "
22946 "%<#pragma omp sections%> construct");
22950 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22951 c_invoke_pragma_handler (id);
22955 cp_parser_error (parser, "expected declaration specifiers");
22959 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22963 /* The interface the pragma parsers have to the lexer. */
22966 pragma_lex (tree *value)
22969 enum cpp_ttype ret;
22971 tok = cp_lexer_peek_token (the_parser->lexer);
22974 *value = tok->u.value;
22976 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22978 else if (ret == CPP_STRING)
22979 *value = cp_parser_string_literal (the_parser, false, false);
22982 cp_lexer_consume_token (the_parser->lexer);
22983 if (ret == CPP_KEYWORD)
22991 /* External interface. */
22993 /* Parse one entire translation unit. */
22996 c_parse_file (void)
22998 bool error_occurred;
22999 static bool already_called = false;
23001 if (already_called)
23003 sorry ("inter-module optimizations not implemented for C++");
23006 already_called = true;
23008 the_parser = cp_parser_new ();
23009 push_deferring_access_checks (flag_access_control
23010 ? dk_no_deferred : dk_no_check);
23011 error_occurred = cp_parser_translation_unit (the_parser);
23015 #include "gt-cp-parser.h"