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 && dependent_scope_p (parser->scope))
2405 error_at (location, "need %<typename%> before %<%T::%E%> because "
2406 "%qT is a dependent scope",
2407 parser->scope, id, parser->scope);
2408 else if (TYPE_P (parser->scope))
2409 error_at (location, "%qE in class %qT does not name a type",
2414 cp_parser_commit_to_tentative_parse (parser);
2417 /* Check for a common situation where a type-name should be present,
2418 but is not, and issue a sensible error message. Returns true if an
2419 invalid type-name was detected.
2421 The situation handled by this function are variable declarations of the
2422 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2423 Usually, `ID' should name a type, but if we got here it means that it
2424 does not. We try to emit the best possible error message depending on
2425 how exactly the id-expression looks like. */
2428 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2431 cp_token *token = cp_lexer_peek_token (parser->lexer);
2433 cp_parser_parse_tentatively (parser);
2434 id = cp_parser_id_expression (parser,
2435 /*template_keyword_p=*/false,
2436 /*check_dependency_p=*/true,
2437 /*template_p=*/NULL,
2438 /*declarator_p=*/true,
2439 /*optional_p=*/false);
2440 /* If the next token is a (, this is a function with no explicit return
2441 type, i.e. constructor, destructor or conversion op. */
2442 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2443 || TREE_CODE (id) == TYPE_DECL)
2445 cp_parser_abort_tentative_parse (parser);
2448 if (!cp_parser_parse_definitely (parser))
2451 /* Emit a diagnostic for the invalid type. */
2452 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2453 id, token->location);
2454 /* If we aren't in the middle of a declarator (i.e. in a
2455 parameter-declaration-clause), skip to the end of the declaration;
2456 there's no point in trying to process it. */
2457 if (!parser->in_declarator_p)
2458 cp_parser_skip_to_end_of_block_or_statement (parser);
2462 /* Consume tokens up to, and including, the next non-nested closing `)'.
2463 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2464 are doing error recovery. Returns -1 if OR_COMMA is true and we
2465 found an unnested comma. */
2468 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2473 unsigned paren_depth = 0;
2474 unsigned brace_depth = 0;
2475 unsigned square_depth = 0;
2477 if (recovering && !or_comma
2478 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2483 cp_token * token = cp_lexer_peek_token (parser->lexer);
2485 switch (token->type)
2488 case CPP_PRAGMA_EOL:
2489 /* If we've run out of tokens, then there is no closing `)'. */
2492 /* This is good for lambda expression capture-lists. */
2493 case CPP_OPEN_SQUARE:
2496 case CPP_CLOSE_SQUARE:
2497 if (!square_depth--)
2502 /* This matches the processing in skip_to_end_of_statement. */
2507 case CPP_OPEN_BRACE:
2510 case CPP_CLOSE_BRACE:
2516 if (recovering && or_comma && !brace_depth && !paren_depth
2521 case CPP_OPEN_PAREN:
2526 case CPP_CLOSE_PAREN:
2527 if (!brace_depth && !paren_depth--)
2530 cp_lexer_consume_token (parser->lexer);
2539 /* Consume the token. */
2540 cp_lexer_consume_token (parser->lexer);
2544 /* Consume tokens until we reach the end of the current statement.
2545 Normally, that will be just before consuming a `;'. However, if a
2546 non-nested `}' comes first, then we stop before consuming that. */
2549 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2551 unsigned nesting_depth = 0;
2555 cp_token *token = cp_lexer_peek_token (parser->lexer);
2557 switch (token->type)
2560 case CPP_PRAGMA_EOL:
2561 /* If we've run out of tokens, stop. */
2565 /* If the next token is a `;', we have reached the end of the
2571 case CPP_CLOSE_BRACE:
2572 /* If this is a non-nested '}', stop before consuming it.
2573 That way, when confronted with something like:
2577 we stop before consuming the closing '}', even though we
2578 have not yet reached a `;'. */
2579 if (nesting_depth == 0)
2582 /* If it is the closing '}' for a block that we have
2583 scanned, stop -- but only after consuming the token.
2589 we will stop after the body of the erroneously declared
2590 function, but before consuming the following `typedef'
2592 if (--nesting_depth == 0)
2594 cp_lexer_consume_token (parser->lexer);
2598 case CPP_OPEN_BRACE:
2606 /* Consume the token. */
2607 cp_lexer_consume_token (parser->lexer);
2611 /* This function is called at the end of a statement or declaration.
2612 If the next token is a semicolon, it is consumed; otherwise, error
2613 recovery is attempted. */
2616 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2618 /* Look for the trailing `;'. */
2619 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2621 /* If there is additional (erroneous) input, skip to the end of
2623 cp_parser_skip_to_end_of_statement (parser);
2624 /* If the next token is now a `;', consume it. */
2625 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2626 cp_lexer_consume_token (parser->lexer);
2630 /* Skip tokens until we have consumed an entire block, or until we
2631 have consumed a non-nested `;'. */
2634 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2636 int nesting_depth = 0;
2638 while (nesting_depth >= 0)
2640 cp_token *token = cp_lexer_peek_token (parser->lexer);
2642 switch (token->type)
2645 case CPP_PRAGMA_EOL:
2646 /* If we've run out of tokens, stop. */
2650 /* Stop if this is an unnested ';'. */
2655 case CPP_CLOSE_BRACE:
2656 /* Stop if this is an unnested '}', or closes the outermost
2659 if (nesting_depth < 0)
2665 case CPP_OPEN_BRACE:
2674 /* Consume the token. */
2675 cp_lexer_consume_token (parser->lexer);
2679 /* Skip tokens until a non-nested closing curly brace is the next
2680 token, or there are no more tokens. Return true in the first case,
2684 cp_parser_skip_to_closing_brace (cp_parser *parser)
2686 unsigned nesting_depth = 0;
2690 cp_token *token = cp_lexer_peek_token (parser->lexer);
2692 switch (token->type)
2695 case CPP_PRAGMA_EOL:
2696 /* If we've run out of tokens, stop. */
2699 case CPP_CLOSE_BRACE:
2700 /* If the next token is a non-nested `}', then we have reached
2701 the end of the current block. */
2702 if (nesting_depth-- == 0)
2706 case CPP_OPEN_BRACE:
2707 /* If it the next token is a `{', then we are entering a new
2708 block. Consume the entire block. */
2716 /* Consume the token. */
2717 cp_lexer_consume_token (parser->lexer);
2721 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2722 parameter is the PRAGMA token, allowing us to purge the entire pragma
2726 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2730 parser->lexer->in_pragma = false;
2733 token = cp_lexer_consume_token (parser->lexer);
2734 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2736 /* Ensure that the pragma is not parsed again. */
2737 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2740 /* Require pragma end of line, resyncing with it as necessary. The
2741 arguments are as for cp_parser_skip_to_pragma_eol. */
2744 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2746 parser->lexer->in_pragma = false;
2747 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2748 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2751 /* This is a simple wrapper around make_typename_type. When the id is
2752 an unresolved identifier node, we can provide a superior diagnostic
2753 using cp_parser_diagnose_invalid_type_name. */
2756 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2757 tree id, location_t id_location)
2760 if (TREE_CODE (id) == IDENTIFIER_NODE)
2762 result = make_typename_type (scope, id, typename_type,
2763 /*complain=*/tf_none);
2764 if (result == error_mark_node)
2765 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2768 return make_typename_type (scope, id, typename_type, tf_error);
2771 /* This is a wrapper around the
2772 make_{pointer,ptrmem,reference}_declarator functions that decides
2773 which one to call based on the CODE and CLASS_TYPE arguments. The
2774 CODE argument should be one of the values returned by
2775 cp_parser_ptr_operator. */
2776 static cp_declarator *
2777 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2778 cp_cv_quals cv_qualifiers,
2779 cp_declarator *target)
2781 if (code == ERROR_MARK)
2782 return cp_error_declarator;
2784 if (code == INDIRECT_REF)
2785 if (class_type == NULL_TREE)
2786 return make_pointer_declarator (cv_qualifiers, target);
2788 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2789 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2790 return make_reference_declarator (cv_qualifiers, target, false);
2791 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2792 return make_reference_declarator (cv_qualifiers, target, true);
2796 /* Create a new C++ parser. */
2799 cp_parser_new (void)
2805 /* cp_lexer_new_main is called before calling ggc_alloc because
2806 cp_lexer_new_main might load a PCH file. */
2807 lexer = cp_lexer_new_main ();
2809 /* Initialize the binops_by_token so that we can get the tree
2810 directly from the token. */
2811 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2812 binops_by_token[binops[i].token_type] = binops[i];
2814 parser = GGC_CNEW (cp_parser);
2815 parser->lexer = lexer;
2816 parser->context = cp_parser_context_new (NULL);
2818 /* For now, we always accept GNU extensions. */
2819 parser->allow_gnu_extensions_p = 1;
2821 /* The `>' token is a greater-than operator, not the end of a
2823 parser->greater_than_is_operator_p = true;
2825 parser->default_arg_ok_p = true;
2827 /* We are not parsing a constant-expression. */
2828 parser->integral_constant_expression_p = false;
2829 parser->allow_non_integral_constant_expression_p = false;
2830 parser->non_integral_constant_expression_p = false;
2832 /* Local variable names are not forbidden. */
2833 parser->local_variables_forbidden_p = false;
2835 /* We are not processing an `extern "C"' declaration. */
2836 parser->in_unbraced_linkage_specification_p = false;
2838 /* We are not processing a declarator. */
2839 parser->in_declarator_p = false;
2841 /* We are not processing a template-argument-list. */
2842 parser->in_template_argument_list_p = false;
2844 /* We are not in an iteration statement. */
2845 parser->in_statement = 0;
2847 /* We are not in a switch statement. */
2848 parser->in_switch_statement_p = false;
2850 /* We are not parsing a type-id inside an expression. */
2851 parser->in_type_id_in_expr_p = false;
2853 /* Declarations aren't implicitly extern "C". */
2854 parser->implicit_extern_c = false;
2856 /* String literals should be translated to the execution character set. */
2857 parser->translate_strings_p = true;
2859 /* We are not parsing a function body. */
2860 parser->in_function_body = false;
2862 /* The unparsed function queue is empty. */
2863 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2865 /* There are no classes being defined. */
2866 parser->num_classes_being_defined = 0;
2868 /* No template parameters apply. */
2869 parser->num_template_parameter_lists = 0;
2874 /* Create a cp_lexer structure which will emit the tokens in CACHE
2875 and push it onto the parser's lexer stack. This is used for delayed
2876 parsing of in-class method bodies and default arguments, and should
2877 not be confused with tentative parsing. */
2879 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2881 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2882 lexer->next = parser->lexer;
2883 parser->lexer = lexer;
2885 /* Move the current source position to that of the first token in the
2887 cp_lexer_set_source_position_from_token (lexer->next_token);
2890 /* Pop the top lexer off the parser stack. This is never used for the
2891 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2893 cp_parser_pop_lexer (cp_parser *parser)
2895 cp_lexer *lexer = parser->lexer;
2896 parser->lexer = lexer->next;
2897 cp_lexer_destroy (lexer);
2899 /* Put the current source position back where it was before this
2900 lexer was pushed. */
2901 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2904 /* Lexical conventions [gram.lex] */
2906 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2910 cp_parser_identifier (cp_parser* parser)
2914 /* Look for the identifier. */
2915 token = cp_parser_require (parser, CPP_NAME, "identifier");
2916 /* Return the value. */
2917 return token ? token->u.value : error_mark_node;
2920 /* Parse a sequence of adjacent string constants. Returns a
2921 TREE_STRING representing the combined, nul-terminated string
2922 constant. If TRANSLATE is true, translate the string to the
2923 execution character set. If WIDE_OK is true, a wide string is
2926 C++98 [lex.string] says that if a narrow string literal token is
2927 adjacent to a wide string literal token, the behavior is undefined.
2928 However, C99 6.4.5p4 says that this results in a wide string literal.
2929 We follow C99 here, for consistency with the C front end.
2931 This code is largely lifted from lex_string() in c-lex.c.
2933 FUTURE: ObjC++ will need to handle @-strings here. */
2935 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2939 struct obstack str_ob;
2940 cpp_string str, istr, *strs;
2942 enum cpp_ttype type;
2944 tok = cp_lexer_peek_token (parser->lexer);
2945 if (!cp_parser_is_string_literal (tok))
2947 cp_parser_error (parser, "expected string-literal");
2948 return error_mark_node;
2953 /* Try to avoid the overhead of creating and destroying an obstack
2954 for the common case of just one string. */
2955 if (!cp_parser_is_string_literal
2956 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2958 cp_lexer_consume_token (parser->lexer);
2960 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2961 str.len = TREE_STRING_LENGTH (tok->u.value);
2968 gcc_obstack_init (&str_ob);
2973 cp_lexer_consume_token (parser->lexer);
2975 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2976 str.len = TREE_STRING_LENGTH (tok->u.value);
2978 if (type != tok->type)
2980 if (type == CPP_STRING)
2982 else if (tok->type != CPP_STRING)
2983 error_at (tok->location,
2984 "unsupported non-standard concatenation "
2985 "of string literals");
2988 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2990 tok = cp_lexer_peek_token (parser->lexer);
2992 while (cp_parser_is_string_literal (tok));
2994 strs = (cpp_string *) obstack_finish (&str_ob);
2997 if (type != CPP_STRING && !wide_ok)
2999 cp_parser_error (parser, "a wide string is invalid in this context");
3003 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3004 (parse_in, strs, count, &istr, type))
3006 value = build_string (istr.len, (const char *)istr.text);
3007 free (CONST_CAST (unsigned char *, istr.text));
3013 case CPP_UTF8STRING:
3014 TREE_TYPE (value) = char_array_type_node;
3017 TREE_TYPE (value) = char16_array_type_node;
3020 TREE_TYPE (value) = char32_array_type_node;
3023 TREE_TYPE (value) = wchar_array_type_node;
3027 value = fix_string_type (value);
3030 /* cpp_interpret_string has issued an error. */
3031 value = error_mark_node;
3034 obstack_free (&str_ob, 0);
3040 /* Basic concepts [gram.basic] */
3042 /* Parse a translation-unit.
3045 declaration-seq [opt]
3047 Returns TRUE if all went well. */
3050 cp_parser_translation_unit (cp_parser* parser)
3052 /* The address of the first non-permanent object on the declarator
3054 static void *declarator_obstack_base;
3058 /* Create the declarator obstack, if necessary. */
3059 if (!cp_error_declarator)
3061 gcc_obstack_init (&declarator_obstack);
3062 /* Create the error declarator. */
3063 cp_error_declarator = make_declarator (cdk_error);
3064 /* Create the empty parameter list. */
3065 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3066 /* Remember where the base of the declarator obstack lies. */
3067 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3070 cp_parser_declaration_seq_opt (parser);
3072 /* If there are no tokens left then all went well. */
3073 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3075 /* Get rid of the token array; we don't need it any more. */
3076 cp_lexer_destroy (parser->lexer);
3077 parser->lexer = NULL;
3079 /* This file might have been a context that's implicitly extern
3080 "C". If so, pop the lang context. (Only relevant for PCH.) */
3081 if (parser->implicit_extern_c)
3083 pop_lang_context ();
3084 parser->implicit_extern_c = false;
3088 finish_translation_unit ();
3094 cp_parser_error (parser, "expected declaration");
3098 /* Make sure the declarator obstack was fully cleaned up. */
3099 gcc_assert (obstack_next_free (&declarator_obstack)
3100 == declarator_obstack_base);
3102 /* All went well. */
3106 /* Expressions [gram.expr] */
3108 /* Parse a primary-expression.
3119 ( compound-statement )
3120 __builtin_va_arg ( assignment-expression , type-id )
3121 __builtin_offsetof ( type-id , offsetof-expression )
3124 __has_nothrow_assign ( type-id )
3125 __has_nothrow_constructor ( type-id )
3126 __has_nothrow_copy ( type-id )
3127 __has_trivial_assign ( type-id )
3128 __has_trivial_constructor ( type-id )
3129 __has_trivial_copy ( type-id )
3130 __has_trivial_destructor ( type-id )
3131 __has_virtual_destructor ( type-id )
3132 __is_abstract ( type-id )
3133 __is_base_of ( type-id , type-id )
3134 __is_class ( type-id )
3135 __is_convertible_to ( type-id , type-id )
3136 __is_empty ( type-id )
3137 __is_enum ( type-id )
3138 __is_pod ( type-id )
3139 __is_polymorphic ( type-id )
3140 __is_union ( type-id )
3142 Objective-C++ Extension:
3150 ADDRESS_P is true iff this expression was immediately preceded by
3151 "&" and therefore might denote a pointer-to-member. CAST_P is true
3152 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3153 true iff this expression is a template argument.
3155 Returns a representation of the expression. Upon return, *IDK
3156 indicates what kind of id-expression (if any) was present. */
3159 cp_parser_primary_expression (cp_parser *parser,
3162 bool template_arg_p,
3165 cp_token *token = NULL;
3167 /* Assume the primary expression is not an id-expression. */
3168 *idk = CP_ID_KIND_NONE;
3170 /* Peek at the next token. */
3171 token = cp_lexer_peek_token (parser->lexer);
3172 switch (token->type)
3185 token = cp_lexer_consume_token (parser->lexer);
3186 if (TREE_CODE (token->u.value) == FIXED_CST)
3188 error_at (token->location,
3189 "fixed-point types not supported in C++");
3190 return error_mark_node;
3192 /* Floating-point literals are only allowed in an integral
3193 constant expression if they are cast to an integral or
3194 enumeration type. */
3195 if (TREE_CODE (token->u.value) == REAL_CST
3196 && parser->integral_constant_expression_p
3199 /* CAST_P will be set even in invalid code like "int(2.7 +
3200 ...)". Therefore, we have to check that the next token
3201 is sure to end the cast. */
3204 cp_token *next_token;
3206 next_token = cp_lexer_peek_token (parser->lexer);
3207 if (/* The comma at the end of an
3208 enumerator-definition. */
3209 next_token->type != CPP_COMMA
3210 /* The curly brace at the end of an enum-specifier. */
3211 && next_token->type != CPP_CLOSE_BRACE
3212 /* The end of a statement. */
3213 && next_token->type != CPP_SEMICOLON
3214 /* The end of the cast-expression. */
3215 && next_token->type != CPP_CLOSE_PAREN
3216 /* The end of an array bound. */
3217 && next_token->type != CPP_CLOSE_SQUARE
3218 /* The closing ">" in a template-argument-list. */
3219 && (next_token->type != CPP_GREATER
3220 || parser->greater_than_is_operator_p)
3221 /* C++0x only: A ">>" treated like two ">" tokens,
3222 in a template-argument-list. */
3223 && (next_token->type != CPP_RSHIFT
3224 || (cxx_dialect == cxx98)
3225 || parser->greater_than_is_operator_p))
3229 /* If we are within a cast, then the constraint that the
3230 cast is to an integral or enumeration type will be
3231 checked at that point. If we are not within a cast, then
3232 this code is invalid. */
3234 cp_parser_non_integral_constant_expression
3235 (parser, "floating-point literal");
3237 return token->u.value;
3243 case CPP_UTF8STRING:
3244 /* ??? Should wide strings be allowed when parser->translate_strings_p
3245 is false (i.e. in attributes)? If not, we can kill the third
3246 argument to cp_parser_string_literal. */
3247 return cp_parser_string_literal (parser,
3248 parser->translate_strings_p,
3251 case CPP_OPEN_PAREN:
3254 bool saved_greater_than_is_operator_p;
3256 /* Consume the `('. */
3257 cp_lexer_consume_token (parser->lexer);
3258 /* Within a parenthesized expression, a `>' token is always
3259 the greater-than operator. */
3260 saved_greater_than_is_operator_p
3261 = parser->greater_than_is_operator_p;
3262 parser->greater_than_is_operator_p = true;
3263 /* If we see `( { ' then we are looking at the beginning of
3264 a GNU statement-expression. */
3265 if (cp_parser_allow_gnu_extensions_p (parser)
3266 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3268 /* Statement-expressions are not allowed by the standard. */
3269 pedwarn (token->location, OPT_pedantic,
3270 "ISO C++ forbids braced-groups within expressions");
3272 /* And they're not allowed outside of a function-body; you
3273 cannot, for example, write:
3275 int i = ({ int j = 3; j + 1; });
3277 at class or namespace scope. */
3278 if (!parser->in_function_body
3279 || parser->in_template_argument_list_p)
3281 error_at (token->location,
3282 "statement-expressions are not allowed outside "
3283 "functions nor in template-argument lists");
3284 cp_parser_skip_to_end_of_block_or_statement (parser);
3285 expr = error_mark_node;
3289 /* Start the statement-expression. */
3290 expr = begin_stmt_expr ();
3291 /* Parse the compound-statement. */
3292 cp_parser_compound_statement (parser, expr, false);
3294 expr = finish_stmt_expr (expr, false);
3299 /* Parse the parenthesized expression. */
3300 expr = cp_parser_expression (parser, cast_p, idk);
3301 /* Let the front end know that this expression was
3302 enclosed in parentheses. This matters in case, for
3303 example, the expression is of the form `A::B', since
3304 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3306 finish_parenthesized_expr (expr);
3308 /* The `>' token might be the end of a template-id or
3309 template-parameter-list now. */
3310 parser->greater_than_is_operator_p
3311 = saved_greater_than_is_operator_p;
3312 /* Consume the `)'. */
3313 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3314 cp_parser_skip_to_end_of_statement (parser);
3319 case CPP_OPEN_SQUARE:
3320 if (c_dialect_objc ())
3321 /* We have an Objective-C++ message. */
3322 return cp_parser_objc_expression (parser);
3323 maybe_warn_cpp0x ("lambda expressions");
3324 return cp_parser_lambda_expression (parser);
3326 case CPP_OBJC_STRING:
3327 if (c_dialect_objc ())
3328 /* We have an Objective-C++ string literal. */
3329 return cp_parser_objc_expression (parser);
3330 cp_parser_error (parser, "expected primary-expression");
3331 return error_mark_node;
3334 switch (token->keyword)
3336 /* These two are the boolean literals. */
3338 cp_lexer_consume_token (parser->lexer);
3339 return boolean_true_node;
3341 cp_lexer_consume_token (parser->lexer);
3342 return boolean_false_node;
3344 /* The `__null' literal. */
3346 cp_lexer_consume_token (parser->lexer);
3349 /* Recognize the `this' keyword. */
3351 cp_lexer_consume_token (parser->lexer);
3352 if (parser->local_variables_forbidden_p)
3354 error_at (token->location,
3355 "%<this%> may not be used in this context");
3356 return error_mark_node;
3358 /* Pointers cannot appear in constant-expressions. */
3359 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3360 return error_mark_node;
3361 return finish_this_expr ();
3363 /* The `operator' keyword can be the beginning of an
3368 case RID_FUNCTION_NAME:
3369 case RID_PRETTY_FUNCTION_NAME:
3370 case RID_C99_FUNCTION_NAME:
3374 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3375 __func__ are the names of variables -- but they are
3376 treated specially. Therefore, they are handled here,
3377 rather than relying on the generic id-expression logic
3378 below. Grammatically, these names are id-expressions.
3380 Consume the token. */
3381 token = cp_lexer_consume_token (parser->lexer);
3383 switch (token->keyword)
3385 case RID_FUNCTION_NAME:
3386 name = "%<__FUNCTION__%>";
3388 case RID_PRETTY_FUNCTION_NAME:
3389 name = "%<__PRETTY_FUNCTION__%>";
3391 case RID_C99_FUNCTION_NAME:
3392 name = "%<__func__%>";
3398 if (cp_parser_non_integral_constant_expression (parser, name))
3399 return error_mark_node;
3401 /* Look up the name. */
3402 return finish_fname (token->u.value);
3410 /* The `__builtin_va_arg' construct is used to handle
3411 `va_arg'. Consume the `__builtin_va_arg' token. */
3412 cp_lexer_consume_token (parser->lexer);
3413 /* Look for the opening `('. */
3414 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3415 /* Now, parse the assignment-expression. */
3416 expression = cp_parser_assignment_expression (parser,
3417 /*cast_p=*/false, NULL);
3418 /* Look for the `,'. */
3419 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3420 /* Parse the type-id. */
3421 type = cp_parser_type_id (parser);
3422 /* Look for the closing `)'. */
3423 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3424 /* Using `va_arg' in a constant-expression is not
3426 if (cp_parser_non_integral_constant_expression (parser,
3428 return error_mark_node;
3429 return build_x_va_arg (expression, type);
3433 return cp_parser_builtin_offsetof (parser);
3435 case RID_HAS_NOTHROW_ASSIGN:
3436 case RID_HAS_NOTHROW_CONSTRUCTOR:
3437 case RID_HAS_NOTHROW_COPY:
3438 case RID_HAS_TRIVIAL_ASSIGN:
3439 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3440 case RID_HAS_TRIVIAL_COPY:
3441 case RID_HAS_TRIVIAL_DESTRUCTOR:
3442 case RID_HAS_VIRTUAL_DESTRUCTOR:
3443 case RID_IS_ABSTRACT:
3444 case RID_IS_BASE_OF:
3446 case RID_IS_CONVERTIBLE_TO:
3450 case RID_IS_POLYMORPHIC:
3451 case RID_IS_STD_LAYOUT:
3452 case RID_IS_TRIVIAL:
3454 return cp_parser_trait_expr (parser, token->keyword);
3456 /* Objective-C++ expressions. */
3458 case RID_AT_PROTOCOL:
3459 case RID_AT_SELECTOR:
3460 return cp_parser_objc_expression (parser);
3463 cp_parser_error (parser, "expected primary-expression");
3464 return error_mark_node;
3467 /* An id-expression can start with either an identifier, a
3468 `::' as the beginning of a qualified-id, or the "operator"
3472 case CPP_TEMPLATE_ID:
3473 case CPP_NESTED_NAME_SPECIFIER:
3477 const char *error_msg;
3480 cp_token *id_expr_token;
3483 /* Parse the id-expression. */
3485 = cp_parser_id_expression (parser,
3486 /*template_keyword_p=*/false,
3487 /*check_dependency_p=*/true,
3489 /*declarator_p=*/false,
3490 /*optional_p=*/false);
3491 if (id_expression == error_mark_node)
3492 return error_mark_node;
3493 id_expr_token = token;
3494 token = cp_lexer_peek_token (parser->lexer);
3495 done = (token->type != CPP_OPEN_SQUARE
3496 && token->type != CPP_OPEN_PAREN
3497 && token->type != CPP_DOT
3498 && token->type != CPP_DEREF
3499 && token->type != CPP_PLUS_PLUS
3500 && token->type != CPP_MINUS_MINUS);
3501 /* If we have a template-id, then no further lookup is
3502 required. If the template-id was for a template-class, we
3503 will sometimes have a TYPE_DECL at this point. */
3504 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3505 || TREE_CODE (id_expression) == TYPE_DECL)
3506 decl = id_expression;
3507 /* Look up the name. */
3510 tree ambiguous_decls;
3512 decl = cp_parser_lookup_name (parser, id_expression,
3515 /*is_namespace=*/false,
3516 /*check_dependency=*/true,
3518 id_expr_token->location);
3519 /* If the lookup was ambiguous, an error will already have
3521 if (ambiguous_decls)
3522 return error_mark_node;
3524 /* In Objective-C++, an instance variable (ivar) may be preferred
3525 to whatever cp_parser_lookup_name() found. */
3526 decl = objc_lookup_ivar (decl, id_expression);
3528 /* If name lookup gives us a SCOPE_REF, then the
3529 qualifying scope was dependent. */
3530 if (TREE_CODE (decl) == SCOPE_REF)
3532 /* At this point, we do not know if DECL is a valid
3533 integral constant expression. We assume that it is
3534 in fact such an expression, so that code like:
3536 template <int N> struct A {
3540 is accepted. At template-instantiation time, we
3541 will check that B<N>::i is actually a constant. */
3544 /* Check to see if DECL is a local variable in a context
3545 where that is forbidden. */
3546 if (parser->local_variables_forbidden_p
3547 && local_variable_p (decl))
3549 /* It might be that we only found DECL because we are
3550 trying to be generous with pre-ISO scoping rules.
3551 For example, consider:
3555 for (int i = 0; i < 10; ++i) {}
3556 extern void f(int j = i);
3559 Here, name look up will originally find the out
3560 of scope `i'. We need to issue a warning message,
3561 but then use the global `i'. */
3562 decl = check_for_out_of_scope_variable (decl);
3563 if (local_variable_p (decl))
3565 error_at (id_expr_token->location,
3566 "local variable %qD may not appear in this context",
3568 return error_mark_node;
3573 decl = (finish_id_expression
3574 (id_expression, decl, parser->scope,
3576 parser->integral_constant_expression_p,
3577 parser->allow_non_integral_constant_expression_p,
3578 &parser->non_integral_constant_expression_p,
3579 template_p, done, address_p,
3582 id_expr_token->location));
3584 cp_parser_error (parser, error_msg);
3588 /* Anything else is an error. */
3590 cp_parser_error (parser, "expected primary-expression");
3591 return error_mark_node;
3595 /* Parse an id-expression.
3602 :: [opt] nested-name-specifier template [opt] unqualified-id
3604 :: operator-function-id
3607 Return a representation of the unqualified portion of the
3608 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3609 a `::' or nested-name-specifier.
3611 Often, if the id-expression was a qualified-id, the caller will
3612 want to make a SCOPE_REF to represent the qualified-id. This
3613 function does not do this in order to avoid wastefully creating
3614 SCOPE_REFs when they are not required.
3616 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3619 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3620 uninstantiated templates.
3622 If *TEMPLATE_P is non-NULL, it is set to true iff the
3623 `template' keyword is used to explicitly indicate that the entity
3624 named is a template.
3626 If DECLARATOR_P is true, the id-expression is appearing as part of
3627 a declarator, rather than as part of an expression. */
3630 cp_parser_id_expression (cp_parser *parser,
3631 bool template_keyword_p,
3632 bool check_dependency_p,
3637 bool global_scope_p;
3638 bool nested_name_specifier_p;
3640 /* Assume the `template' keyword was not used. */
3642 *template_p = template_keyword_p;
3644 /* Look for the optional `::' operator. */
3646 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3648 /* Look for the optional nested-name-specifier. */
3649 nested_name_specifier_p
3650 = (cp_parser_nested_name_specifier_opt (parser,
3651 /*typename_keyword_p=*/false,
3656 /* If there is a nested-name-specifier, then we are looking at
3657 the first qualified-id production. */
3658 if (nested_name_specifier_p)
3661 tree saved_object_scope;
3662 tree saved_qualifying_scope;
3663 tree unqualified_id;
3666 /* See if the next token is the `template' keyword. */
3668 template_p = &is_template;
3669 *template_p = cp_parser_optional_template_keyword (parser);
3670 /* Name lookup we do during the processing of the
3671 unqualified-id might obliterate SCOPE. */
3672 saved_scope = parser->scope;
3673 saved_object_scope = parser->object_scope;
3674 saved_qualifying_scope = parser->qualifying_scope;
3675 /* Process the final unqualified-id. */
3676 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3679 /*optional_p=*/false);
3680 /* Restore the SAVED_SCOPE for our caller. */
3681 parser->scope = saved_scope;
3682 parser->object_scope = saved_object_scope;
3683 parser->qualifying_scope = saved_qualifying_scope;
3685 return unqualified_id;
3687 /* Otherwise, if we are in global scope, then we are looking at one
3688 of the other qualified-id productions. */
3689 else if (global_scope_p)
3694 /* Peek at the next token. */
3695 token = cp_lexer_peek_token (parser->lexer);
3697 /* If it's an identifier, and the next token is not a "<", then
3698 we can avoid the template-id case. This is an optimization
3699 for this common case. */
3700 if (token->type == CPP_NAME
3701 && !cp_parser_nth_token_starts_template_argument_list_p
3703 return cp_parser_identifier (parser);
3705 cp_parser_parse_tentatively (parser);
3706 /* Try a template-id. */
3707 id = cp_parser_template_id (parser,
3708 /*template_keyword_p=*/false,
3709 /*check_dependency_p=*/true,
3711 /* If that worked, we're done. */
3712 if (cp_parser_parse_definitely (parser))
3715 /* Peek at the next token. (Changes in the token buffer may
3716 have invalidated the pointer obtained above.) */
3717 token = cp_lexer_peek_token (parser->lexer);
3719 switch (token->type)
3722 return cp_parser_identifier (parser);
3725 if (token->keyword == RID_OPERATOR)
3726 return cp_parser_operator_function_id (parser);
3730 cp_parser_error (parser, "expected id-expression");
3731 return error_mark_node;
3735 return cp_parser_unqualified_id (parser, template_keyword_p,
3736 /*check_dependency_p=*/true,
3741 /* Parse an unqualified-id.
3745 operator-function-id
3746 conversion-function-id
3750 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3751 keyword, in a construct like `A::template ...'.
3753 Returns a representation of unqualified-id. For the `identifier'
3754 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3755 production a BIT_NOT_EXPR is returned; the operand of the
3756 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3757 other productions, see the documentation accompanying the
3758 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3759 names are looked up in uninstantiated templates. If DECLARATOR_P
3760 is true, the unqualified-id is appearing as part of a declarator,
3761 rather than as part of an expression. */
3764 cp_parser_unqualified_id (cp_parser* parser,
3765 bool template_keyword_p,
3766 bool check_dependency_p,
3772 /* Peek at the next token. */
3773 token = cp_lexer_peek_token (parser->lexer);
3775 switch (token->type)
3781 /* We don't know yet whether or not this will be a
3783 cp_parser_parse_tentatively (parser);
3784 /* Try a template-id. */
3785 id = cp_parser_template_id (parser, template_keyword_p,
3788 /* If it worked, we're done. */
3789 if (cp_parser_parse_definitely (parser))
3791 /* Otherwise, it's an ordinary identifier. */
3792 return cp_parser_identifier (parser);
3795 case CPP_TEMPLATE_ID:
3796 return cp_parser_template_id (parser, template_keyword_p,
3803 tree qualifying_scope;
3808 /* Consume the `~' token. */
3809 cp_lexer_consume_token (parser->lexer);
3810 /* Parse the class-name. The standard, as written, seems to
3813 template <typename T> struct S { ~S (); };
3814 template <typename T> S<T>::~S() {}
3816 is invalid, since `~' must be followed by a class-name, but
3817 `S<T>' is dependent, and so not known to be a class.
3818 That's not right; we need to look in uninstantiated
3819 templates. A further complication arises from:
3821 template <typename T> void f(T t) {
3825 Here, it is not possible to look up `T' in the scope of `T'
3826 itself. We must look in both the current scope, and the
3827 scope of the containing complete expression.
3829 Yet another issue is:
3838 The standard does not seem to say that the `S' in `~S'
3839 should refer to the type `S' and not the data member
3842 /* DR 244 says that we look up the name after the "~" in the
3843 same scope as we looked up the qualifying name. That idea
3844 isn't fully worked out; it's more complicated than that. */
3845 scope = parser->scope;
3846 object_scope = parser->object_scope;
3847 qualifying_scope = parser->qualifying_scope;
3849 /* Check for invalid scopes. */
3850 if (scope == error_mark_node)
3852 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3853 cp_lexer_consume_token (parser->lexer);
3854 return error_mark_node;
3856 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3858 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3859 error_at (token->location,
3860 "scope %qT before %<~%> is not a class-name",
3862 cp_parser_simulate_error (parser);
3863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3864 cp_lexer_consume_token (parser->lexer);
3865 return error_mark_node;
3867 gcc_assert (!scope || TYPE_P (scope));
3869 /* If the name is of the form "X::~X" it's OK. */
3870 token = cp_lexer_peek_token (parser->lexer);
3872 && token->type == CPP_NAME
3873 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3875 && constructor_name_p (token->u.value, scope))
3877 cp_lexer_consume_token (parser->lexer);
3878 return build_nt (BIT_NOT_EXPR, scope);
3881 /* If there was an explicit qualification (S::~T), first look
3882 in the scope given by the qualification (i.e., S). */
3884 type_decl = NULL_TREE;
3887 cp_parser_parse_tentatively (parser);
3888 type_decl = cp_parser_class_name (parser,
3889 /*typename_keyword_p=*/false,
3890 /*template_keyword_p=*/false,
3892 /*check_dependency=*/false,
3893 /*class_head_p=*/false,
3895 if (cp_parser_parse_definitely (parser))
3898 /* In "N::S::~S", look in "N" as well. */
3899 if (!done && scope && qualifying_scope)
3901 cp_parser_parse_tentatively (parser);
3902 parser->scope = qualifying_scope;
3903 parser->object_scope = NULL_TREE;
3904 parser->qualifying_scope = NULL_TREE;
3906 = cp_parser_class_name (parser,
3907 /*typename_keyword_p=*/false,
3908 /*template_keyword_p=*/false,
3910 /*check_dependency=*/false,
3911 /*class_head_p=*/false,
3913 if (cp_parser_parse_definitely (parser))
3916 /* In "p->S::~T", look in the scope given by "*p" as well. */
3917 else if (!done && object_scope)
3919 cp_parser_parse_tentatively (parser);
3920 parser->scope = object_scope;
3921 parser->object_scope = NULL_TREE;
3922 parser->qualifying_scope = NULL_TREE;
3924 = cp_parser_class_name (parser,
3925 /*typename_keyword_p=*/false,
3926 /*template_keyword_p=*/false,
3928 /*check_dependency=*/false,
3929 /*class_head_p=*/false,
3931 if (cp_parser_parse_definitely (parser))
3934 /* Look in the surrounding context. */
3937 parser->scope = NULL_TREE;
3938 parser->object_scope = NULL_TREE;
3939 parser->qualifying_scope = NULL_TREE;
3940 if (processing_template_decl)
3941 cp_parser_parse_tentatively (parser);
3943 = cp_parser_class_name (parser,
3944 /*typename_keyword_p=*/false,
3945 /*template_keyword_p=*/false,
3947 /*check_dependency=*/false,
3948 /*class_head_p=*/false,
3950 if (processing_template_decl
3951 && ! cp_parser_parse_definitely (parser))
3953 /* We couldn't find a type with this name, so just accept
3954 it and check for a match at instantiation time. */
3955 type_decl = cp_parser_identifier (parser);
3956 if (type_decl != error_mark_node)
3957 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3961 /* If an error occurred, assume that the name of the
3962 destructor is the same as the name of the qualifying
3963 class. That allows us to keep parsing after running
3964 into ill-formed destructor names. */
3965 if (type_decl == error_mark_node && scope)
3966 return build_nt (BIT_NOT_EXPR, scope);
3967 else if (type_decl == error_mark_node)
3968 return error_mark_node;
3970 /* Check that destructor name and scope match. */
3971 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3973 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3974 error_at (token->location,
3975 "declaration of %<~%T%> as member of %qT",
3977 cp_parser_simulate_error (parser);
3978 return error_mark_node;
3983 A typedef-name that names a class shall not be used as the
3984 identifier in the declarator for a destructor declaration. */
3986 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3987 && !DECL_SELF_REFERENCE_P (type_decl)
3988 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3989 error_at (token->location,
3990 "typedef-name %qD used as destructor declarator",
3993 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3997 if (token->keyword == RID_OPERATOR)
4001 /* This could be a template-id, so we try that first. */
4002 cp_parser_parse_tentatively (parser);
4003 /* Try a template-id. */
4004 id = cp_parser_template_id (parser, template_keyword_p,
4005 /*check_dependency_p=*/true,
4007 /* If that worked, we're done. */
4008 if (cp_parser_parse_definitely (parser))
4010 /* We still don't know whether we're looking at an
4011 operator-function-id or a conversion-function-id. */
4012 cp_parser_parse_tentatively (parser);
4013 /* Try an operator-function-id. */
4014 id = cp_parser_operator_function_id (parser);
4015 /* If that didn't work, try a conversion-function-id. */
4016 if (!cp_parser_parse_definitely (parser))
4017 id = cp_parser_conversion_function_id (parser);
4026 cp_parser_error (parser, "expected unqualified-id");
4027 return error_mark_node;
4031 /* Parse an (optional) nested-name-specifier.
4033 nested-name-specifier: [C++98]
4034 class-or-namespace-name :: nested-name-specifier [opt]
4035 class-or-namespace-name :: template nested-name-specifier [opt]
4037 nested-name-specifier: [C++0x]
4040 nested-name-specifier identifier ::
4041 nested-name-specifier template [opt] simple-template-id ::
4043 PARSER->SCOPE should be set appropriately before this function is
4044 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4045 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4048 Sets PARSER->SCOPE to the class (TYPE) or namespace
4049 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4050 it unchanged if there is no nested-name-specifier. Returns the new
4051 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4053 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4054 part of a declaration and/or decl-specifier. */
4057 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4058 bool typename_keyword_p,
4059 bool check_dependency_p,
4061 bool is_declaration)
4063 bool success = false;
4064 cp_token_position start = 0;
4067 /* Remember where the nested-name-specifier starts. */
4068 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4070 start = cp_lexer_token_position (parser->lexer, false);
4071 push_deferring_access_checks (dk_deferred);
4078 tree saved_qualifying_scope;
4079 bool template_keyword_p;
4081 /* Spot cases that cannot be the beginning of a
4082 nested-name-specifier. */
4083 token = cp_lexer_peek_token (parser->lexer);
4085 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4086 the already parsed nested-name-specifier. */
4087 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4089 /* Grab the nested-name-specifier and continue the loop. */
4090 cp_parser_pre_parsed_nested_name_specifier (parser);
4091 /* If we originally encountered this nested-name-specifier
4092 with IS_DECLARATION set to false, we will not have
4093 resolved TYPENAME_TYPEs, so we must do so here. */
4095 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4097 new_scope = resolve_typename_type (parser->scope,
4098 /*only_current_p=*/false);
4099 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4100 parser->scope = new_scope;
4106 /* Spot cases that cannot be the beginning of a
4107 nested-name-specifier. On the second and subsequent times
4108 through the loop, we look for the `template' keyword. */
4109 if (success && token->keyword == RID_TEMPLATE)
4111 /* A template-id can start a nested-name-specifier. */
4112 else if (token->type == CPP_TEMPLATE_ID)
4116 /* If the next token is not an identifier, then it is
4117 definitely not a type-name or namespace-name. */
4118 if (token->type != CPP_NAME)
4120 /* If the following token is neither a `<' (to begin a
4121 template-id), nor a `::', then we are not looking at a
4122 nested-name-specifier. */
4123 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4124 if (token->type != CPP_SCOPE
4125 && !cp_parser_nth_token_starts_template_argument_list_p
4130 /* The nested-name-specifier is optional, so we parse
4132 cp_parser_parse_tentatively (parser);
4134 /* Look for the optional `template' keyword, if this isn't the
4135 first time through the loop. */
4137 template_keyword_p = cp_parser_optional_template_keyword (parser);
4139 template_keyword_p = false;
4141 /* Save the old scope since the name lookup we are about to do
4142 might destroy it. */
4143 old_scope = parser->scope;
4144 saved_qualifying_scope = parser->qualifying_scope;
4145 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4146 look up names in "X<T>::I" in order to determine that "Y" is
4147 a template. So, if we have a typename at this point, we make
4148 an effort to look through it. */
4150 && !typename_keyword_p
4152 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4153 parser->scope = resolve_typename_type (parser->scope,
4154 /*only_current_p=*/false);
4155 /* Parse the qualifying entity. */
4157 = cp_parser_qualifying_entity (parser,
4163 /* Look for the `::' token. */
4164 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4166 /* If we found what we wanted, we keep going; otherwise, we're
4168 if (!cp_parser_parse_definitely (parser))
4170 bool error_p = false;
4172 /* Restore the OLD_SCOPE since it was valid before the
4173 failed attempt at finding the last
4174 class-or-namespace-name. */
4175 parser->scope = old_scope;
4176 parser->qualifying_scope = saved_qualifying_scope;
4177 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4179 /* If the next token is an identifier, and the one after
4180 that is a `::', then any valid interpretation would have
4181 found a class-or-namespace-name. */
4182 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4183 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4185 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4188 token = cp_lexer_consume_token (parser->lexer);
4191 if (!token->ambiguous_p)
4194 tree ambiguous_decls;
4196 decl = cp_parser_lookup_name (parser, token->u.value,
4198 /*is_template=*/false,
4199 /*is_namespace=*/false,
4200 /*check_dependency=*/true,
4203 if (TREE_CODE (decl) == TEMPLATE_DECL)
4204 error_at (token->location,
4205 "%qD used without template parameters",
4207 else if (ambiguous_decls)
4209 error_at (token->location,
4210 "reference to %qD is ambiguous",
4212 print_candidates (ambiguous_decls);
4213 decl = error_mark_node;
4217 const char* msg = "is not a class or namespace";
4218 if (cxx_dialect != cxx98)
4219 msg = "is not a class, namespace, or enumeration";
4220 cp_parser_name_lookup_error
4221 (parser, token->u.value, decl, msg,
4225 parser->scope = error_mark_node;
4227 /* Treat this as a successful nested-name-specifier
4232 If the name found is not a class-name (clause
4233 _class_) or namespace-name (_namespace.def_), the
4234 program is ill-formed. */
4237 cp_lexer_consume_token (parser->lexer);
4241 /* We've found one valid nested-name-specifier. */
4243 /* Name lookup always gives us a DECL. */
4244 if (TREE_CODE (new_scope) == TYPE_DECL)
4245 new_scope = TREE_TYPE (new_scope);
4246 /* Uses of "template" must be followed by actual templates. */
4247 if (template_keyword_p
4248 && !(CLASS_TYPE_P (new_scope)
4249 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4250 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4251 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4252 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4253 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4254 == TEMPLATE_ID_EXPR)))
4255 permerror (input_location, TYPE_P (new_scope)
4256 ? "%qT is not a template"
4257 : "%qD is not a template",
4259 /* If it is a class scope, try to complete it; we are about to
4260 be looking up names inside the class. */
4261 if (TYPE_P (new_scope)
4262 /* Since checking types for dependency can be expensive,
4263 avoid doing it if the type is already complete. */
4264 && !COMPLETE_TYPE_P (new_scope)
4265 /* Do not try to complete dependent types. */
4266 && !dependent_type_p (new_scope))
4268 new_scope = complete_type (new_scope);
4269 /* If it is a typedef to current class, use the current
4270 class instead, as the typedef won't have any names inside
4272 if (!COMPLETE_TYPE_P (new_scope)
4273 && currently_open_class (new_scope))
4274 new_scope = TYPE_MAIN_VARIANT (new_scope);
4276 /* Make sure we look in the right scope the next time through
4278 parser->scope = new_scope;
4281 /* If parsing tentatively, replace the sequence of tokens that makes
4282 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4283 token. That way, should we re-parse the token stream, we will
4284 not have to repeat the effort required to do the parse, nor will
4285 we issue duplicate error messages. */
4286 if (success && start)
4290 token = cp_lexer_token_at (parser->lexer, start);
4291 /* Reset the contents of the START token. */
4292 token->type = CPP_NESTED_NAME_SPECIFIER;
4293 /* Retrieve any deferred checks. Do not pop this access checks yet
4294 so the memory will not be reclaimed during token replacing below. */
4295 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4296 token->u.tree_check_value->value = parser->scope;
4297 token->u.tree_check_value->checks = get_deferred_access_checks ();
4298 token->u.tree_check_value->qualifying_scope =
4299 parser->qualifying_scope;
4300 token->keyword = RID_MAX;
4302 /* Purge all subsequent tokens. */
4303 cp_lexer_purge_tokens_after (parser->lexer, start);
4307 pop_to_parent_deferring_access_checks ();
4309 return success ? parser->scope : NULL_TREE;
4312 /* Parse a nested-name-specifier. See
4313 cp_parser_nested_name_specifier_opt for details. This function
4314 behaves identically, except that it will an issue an error if no
4315 nested-name-specifier is present. */
4318 cp_parser_nested_name_specifier (cp_parser *parser,
4319 bool typename_keyword_p,
4320 bool check_dependency_p,
4322 bool is_declaration)
4326 /* Look for the nested-name-specifier. */
4327 scope = cp_parser_nested_name_specifier_opt (parser,
4332 /* If it was not present, issue an error message. */
4335 cp_parser_error (parser, "expected nested-name-specifier");
4336 parser->scope = NULL_TREE;
4342 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4343 this is either a class-name or a namespace-name (which corresponds
4344 to the class-or-namespace-name production in the grammar). For
4345 C++0x, it can also be a type-name that refers to an enumeration
4348 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4349 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4350 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4351 TYPE_P is TRUE iff the next name should be taken as a class-name,
4352 even the same name is declared to be another entity in the same
4355 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4356 specified by the class-or-namespace-name. If neither is found the
4357 ERROR_MARK_NODE is returned. */
4360 cp_parser_qualifying_entity (cp_parser *parser,
4361 bool typename_keyword_p,
4362 bool template_keyword_p,
4363 bool check_dependency_p,
4365 bool is_declaration)
4368 tree saved_qualifying_scope;
4369 tree saved_object_scope;
4372 bool successful_parse_p;
4374 /* Before we try to parse the class-name, we must save away the
4375 current PARSER->SCOPE since cp_parser_class_name will destroy
4377 saved_scope = parser->scope;
4378 saved_qualifying_scope = parser->qualifying_scope;
4379 saved_object_scope = parser->object_scope;
4380 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4381 there is no need to look for a namespace-name. */
4382 only_class_p = template_keyword_p
4383 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4385 cp_parser_parse_tentatively (parser);
4386 scope = cp_parser_class_name (parser,
4389 type_p ? class_type : none_type,
4391 /*class_head_p=*/false,
4393 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4394 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4396 && cxx_dialect != cxx98
4397 && !successful_parse_p)
4399 /* Restore the saved scope. */
4400 parser->scope = saved_scope;
4401 parser->qualifying_scope = saved_qualifying_scope;
4402 parser->object_scope = saved_object_scope;
4404 /* Parse tentatively. */
4405 cp_parser_parse_tentatively (parser);
4407 /* Parse a typedef-name or enum-name. */
4408 scope = cp_parser_nonclass_name (parser);
4409 successful_parse_p = cp_parser_parse_definitely (parser);
4411 /* If that didn't work, try for a namespace-name. */
4412 if (!only_class_p && !successful_parse_p)
4414 /* Restore the saved scope. */
4415 parser->scope = saved_scope;
4416 parser->qualifying_scope = saved_qualifying_scope;
4417 parser->object_scope = saved_object_scope;
4418 /* If we are not looking at an identifier followed by the scope
4419 resolution operator, then this is not part of a
4420 nested-name-specifier. (Note that this function is only used
4421 to parse the components of a nested-name-specifier.) */
4422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4423 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4424 return error_mark_node;
4425 scope = cp_parser_namespace_name (parser);
4431 /* Parse a postfix-expression.
4435 postfix-expression [ expression ]
4436 postfix-expression ( expression-list [opt] )
4437 simple-type-specifier ( expression-list [opt] )
4438 typename :: [opt] nested-name-specifier identifier
4439 ( expression-list [opt] )
4440 typename :: [opt] nested-name-specifier template [opt] template-id
4441 ( expression-list [opt] )
4442 postfix-expression . template [opt] id-expression
4443 postfix-expression -> template [opt] id-expression
4444 postfix-expression . pseudo-destructor-name
4445 postfix-expression -> pseudo-destructor-name
4446 postfix-expression ++
4447 postfix-expression --
4448 dynamic_cast < type-id > ( expression )
4449 static_cast < type-id > ( expression )
4450 reinterpret_cast < type-id > ( expression )
4451 const_cast < type-id > ( expression )
4452 typeid ( expression )
4458 ( type-id ) { initializer-list , [opt] }
4460 This extension is a GNU version of the C99 compound-literal
4461 construct. (The C99 grammar uses `type-name' instead of `type-id',
4462 but they are essentially the same concept.)
4464 If ADDRESS_P is true, the postfix expression is the operand of the
4465 `&' operator. CAST_P is true if this expression is the target of a
4468 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4469 class member access expressions [expr.ref].
4471 Returns a representation of the expression. */
4474 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4475 bool member_access_only_p,
4476 cp_id_kind * pidk_return)
4480 cp_id_kind idk = CP_ID_KIND_NONE;
4481 tree postfix_expression = NULL_TREE;
4482 bool is_member_access = false;
4484 /* Peek at the next token. */
4485 token = cp_lexer_peek_token (parser->lexer);
4486 /* Some of the productions are determined by keywords. */
4487 keyword = token->keyword;
4497 const char *saved_message;
4499 /* All of these can be handled in the same way from the point
4500 of view of parsing. Begin by consuming the token
4501 identifying the cast. */
4502 cp_lexer_consume_token (parser->lexer);
4504 /* New types cannot be defined in the cast. */
4505 saved_message = parser->type_definition_forbidden_message;
4506 parser->type_definition_forbidden_message
4507 = "types may not be defined in casts";
4509 /* Look for the opening `<'. */
4510 cp_parser_require (parser, CPP_LESS, "%<<%>");
4511 /* Parse the type to which we are casting. */
4512 type = cp_parser_type_id (parser);
4513 /* Look for the closing `>'. */
4514 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4515 /* Restore the old message. */
4516 parser->type_definition_forbidden_message = saved_message;
4518 /* And the expression which is being cast. */
4519 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4520 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4521 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4523 /* Only type conversions to integral or enumeration types
4524 can be used in constant-expressions. */
4525 if (!cast_valid_in_integral_constant_expression_p (type)
4526 && (cp_parser_non_integral_constant_expression
4528 "a cast to a type other than an integral or "
4529 "enumeration type")))
4530 return error_mark_node;
4536 = build_dynamic_cast (type, expression, tf_warning_or_error);
4540 = build_static_cast (type, expression, tf_warning_or_error);
4544 = build_reinterpret_cast (type, expression,
4545 tf_warning_or_error);
4549 = build_const_cast (type, expression, tf_warning_or_error);
4560 const char *saved_message;
4561 bool saved_in_type_id_in_expr_p;
4563 /* Consume the `typeid' token. */
4564 cp_lexer_consume_token (parser->lexer);
4565 /* Look for the `(' token. */
4566 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4567 /* Types cannot be defined in a `typeid' expression. */
4568 saved_message = parser->type_definition_forbidden_message;
4569 parser->type_definition_forbidden_message
4570 = "types may not be defined in a %<typeid%> expression";
4571 /* We can't be sure yet whether we're looking at a type-id or an
4573 cp_parser_parse_tentatively (parser);
4574 /* Try a type-id first. */
4575 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4576 parser->in_type_id_in_expr_p = true;
4577 type = cp_parser_type_id (parser);
4578 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4579 /* Look for the `)' token. Otherwise, we can't be sure that
4580 we're not looking at an expression: consider `typeid (int
4581 (3))', for example. */
4582 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4583 /* If all went well, simply lookup the type-id. */
4584 if (cp_parser_parse_definitely (parser))
4585 postfix_expression = get_typeid (type);
4586 /* Otherwise, fall back to the expression variant. */
4591 /* Look for an expression. */
4592 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4593 /* Compute its typeid. */
4594 postfix_expression = build_typeid (expression);
4595 /* Look for the `)' token. */
4596 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4598 /* Restore the saved message. */
4599 parser->type_definition_forbidden_message = saved_message;
4600 /* `typeid' may not appear in an integral constant expression. */
4601 if (cp_parser_non_integral_constant_expression(parser,
4602 "%<typeid%> operator"))
4603 return error_mark_node;
4610 /* The syntax permitted here is the same permitted for an
4611 elaborated-type-specifier. */
4612 type = cp_parser_elaborated_type_specifier (parser,
4613 /*is_friend=*/false,
4614 /*is_declaration=*/false);
4615 postfix_expression = cp_parser_functional_cast (parser, type);
4623 /* If the next thing is a simple-type-specifier, we may be
4624 looking at a functional cast. We could also be looking at
4625 an id-expression. So, we try the functional cast, and if
4626 that doesn't work we fall back to the primary-expression. */
4627 cp_parser_parse_tentatively (parser);
4628 /* Look for the simple-type-specifier. */
4629 type = cp_parser_simple_type_specifier (parser,
4630 /*decl_specs=*/NULL,
4631 CP_PARSER_FLAGS_NONE);
4632 /* Parse the cast itself. */
4633 if (!cp_parser_error_occurred (parser))
4635 = cp_parser_functional_cast (parser, type);
4636 /* If that worked, we're done. */
4637 if (cp_parser_parse_definitely (parser))
4640 /* If the functional-cast didn't work out, try a
4641 compound-literal. */
4642 if (cp_parser_allow_gnu_extensions_p (parser)
4643 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4645 VEC(constructor_elt,gc) *initializer_list = NULL;
4646 bool saved_in_type_id_in_expr_p;
4648 cp_parser_parse_tentatively (parser);
4649 /* Consume the `('. */
4650 cp_lexer_consume_token (parser->lexer);
4651 /* Parse the type. */
4652 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4653 parser->in_type_id_in_expr_p = true;
4654 type = cp_parser_type_id (parser);
4655 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4656 /* Look for the `)'. */
4657 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4658 /* Look for the `{'. */
4659 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4660 /* If things aren't going well, there's no need to
4662 if (!cp_parser_error_occurred (parser))
4664 bool non_constant_p;
4665 /* Parse the initializer-list. */
4667 = cp_parser_initializer_list (parser, &non_constant_p);
4668 /* Allow a trailing `,'. */
4669 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4670 cp_lexer_consume_token (parser->lexer);
4671 /* Look for the final `}'. */
4672 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4674 /* If that worked, we're definitely looking at a
4675 compound-literal expression. */
4676 if (cp_parser_parse_definitely (parser))
4678 /* Warn the user that a compound literal is not
4679 allowed in standard C++. */
4680 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4681 /* For simplicity, we disallow compound literals in
4682 constant-expressions. We could
4683 allow compound literals of integer type, whose
4684 initializer was a constant, in constant
4685 expressions. Permitting that usage, as a further
4686 extension, would not change the meaning of any
4687 currently accepted programs. (Of course, as
4688 compound literals are not part of ISO C++, the
4689 standard has nothing to say.) */
4690 if (cp_parser_non_integral_constant_expression
4691 (parser, "non-constant compound literals"))
4693 postfix_expression = error_mark_node;
4696 /* Form the representation of the compound-literal. */
4698 = (finish_compound_literal
4699 (type, build_constructor (init_list_type_node,
4700 initializer_list)));
4705 /* It must be a primary-expression. */
4707 = cp_parser_primary_expression (parser, address_p, cast_p,
4708 /*template_arg_p=*/false,
4714 /* Keep looping until the postfix-expression is complete. */
4717 if (idk == CP_ID_KIND_UNQUALIFIED
4718 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4719 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4720 /* It is not a Koenig lookup function call. */
4722 = unqualified_name_lookup_error (postfix_expression);
4724 /* Peek at the next token. */
4725 token = cp_lexer_peek_token (parser->lexer);
4727 switch (token->type)
4729 case CPP_OPEN_SQUARE:
4731 = cp_parser_postfix_open_square_expression (parser,
4734 idk = CP_ID_KIND_NONE;
4735 is_member_access = false;
4738 case CPP_OPEN_PAREN:
4739 /* postfix-expression ( expression-list [opt] ) */
4742 bool is_builtin_constant_p;
4743 bool saved_integral_constant_expression_p = false;
4744 bool saved_non_integral_constant_expression_p = false;
4747 is_member_access = false;
4749 is_builtin_constant_p
4750 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4751 if (is_builtin_constant_p)
4753 /* The whole point of __builtin_constant_p is to allow
4754 non-constant expressions to appear as arguments. */
4755 saved_integral_constant_expression_p
4756 = parser->integral_constant_expression_p;
4757 saved_non_integral_constant_expression_p
4758 = parser->non_integral_constant_expression_p;
4759 parser->integral_constant_expression_p = false;
4761 args = (cp_parser_parenthesized_expression_list
4762 (parser, /*is_attribute_list=*/false,
4763 /*cast_p=*/false, /*allow_expansion_p=*/true,
4764 /*non_constant_p=*/NULL));
4765 if (is_builtin_constant_p)
4767 parser->integral_constant_expression_p
4768 = saved_integral_constant_expression_p;
4769 parser->non_integral_constant_expression_p
4770 = saved_non_integral_constant_expression_p;
4775 postfix_expression = error_mark_node;
4779 /* Function calls are not permitted in
4780 constant-expressions. */
4781 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4782 && cp_parser_non_integral_constant_expression (parser,
4785 postfix_expression = error_mark_node;
4786 release_tree_vector (args);
4791 if (idk == CP_ID_KIND_UNQUALIFIED
4792 || idk == CP_ID_KIND_TEMPLATE_ID)
4794 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4796 if (!VEC_empty (tree, args))
4799 if (!any_type_dependent_arguments_p (args))
4801 = perform_koenig_lookup (postfix_expression, args);
4805 = unqualified_fn_lookup_error (postfix_expression);
4807 /* We do not perform argument-dependent lookup if
4808 normal lookup finds a non-function, in accordance
4809 with the expected resolution of DR 218. */
4810 else if (!VEC_empty (tree, args)
4811 && is_overloaded_fn (postfix_expression))
4813 tree fn = get_first_fn (postfix_expression);
4815 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4816 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4818 /* Only do argument dependent lookup if regular
4819 lookup does not find a set of member functions.
4820 [basic.lookup.koenig]/2a */
4821 if (!DECL_FUNCTION_MEMBER_P (fn))
4824 if (!any_type_dependent_arguments_p (args))
4826 = perform_koenig_lookup (postfix_expression, args);
4831 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4833 tree instance = TREE_OPERAND (postfix_expression, 0);
4834 tree fn = TREE_OPERAND (postfix_expression, 1);
4836 if (processing_template_decl
4837 && (type_dependent_expression_p (instance)
4838 || (!BASELINK_P (fn)
4839 && TREE_CODE (fn) != FIELD_DECL)
4840 || type_dependent_expression_p (fn)
4841 || any_type_dependent_arguments_p (args)))
4844 = build_nt_call_vec (postfix_expression, args);
4845 release_tree_vector (args);
4849 if (BASELINK_P (fn))
4852 = (build_new_method_call
4853 (instance, fn, &args, NULL_TREE,
4854 (idk == CP_ID_KIND_QUALIFIED
4855 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4857 tf_warning_or_error));
4861 = finish_call_expr (postfix_expression, &args,
4862 /*disallow_virtual=*/false,
4864 tf_warning_or_error);
4866 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4867 || TREE_CODE (postfix_expression) == MEMBER_REF
4868 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4869 postfix_expression = (build_offset_ref_call_from_tree
4870 (postfix_expression, &args));
4871 else if (idk == CP_ID_KIND_QUALIFIED)
4872 /* A call to a static class member, or a namespace-scope
4875 = finish_call_expr (postfix_expression, &args,
4876 /*disallow_virtual=*/true,
4878 tf_warning_or_error);
4880 /* All other function calls. */
4882 = finish_call_expr (postfix_expression, &args,
4883 /*disallow_virtual=*/false,
4885 tf_warning_or_error);
4887 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4888 idk = CP_ID_KIND_NONE;
4890 release_tree_vector (args);
4896 /* postfix-expression . template [opt] id-expression
4897 postfix-expression . pseudo-destructor-name
4898 postfix-expression -> template [opt] id-expression
4899 postfix-expression -> pseudo-destructor-name */
4901 /* Consume the `.' or `->' operator. */
4902 cp_lexer_consume_token (parser->lexer);
4905 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4910 is_member_access = true;
4914 /* postfix-expression ++ */
4915 /* Consume the `++' token. */
4916 cp_lexer_consume_token (parser->lexer);
4917 /* Generate a representation for the complete expression. */
4919 = finish_increment_expr (postfix_expression,
4920 POSTINCREMENT_EXPR);
4921 /* Increments may not appear in constant-expressions. */
4922 if (cp_parser_non_integral_constant_expression (parser,
4924 postfix_expression = error_mark_node;
4925 idk = CP_ID_KIND_NONE;
4926 is_member_access = false;
4929 case CPP_MINUS_MINUS:
4930 /* postfix-expression -- */
4931 /* Consume the `--' token. */
4932 cp_lexer_consume_token (parser->lexer);
4933 /* Generate a representation for the complete expression. */
4935 = finish_increment_expr (postfix_expression,
4936 POSTDECREMENT_EXPR);
4937 /* Decrements may not appear in constant-expressions. */
4938 if (cp_parser_non_integral_constant_expression (parser,
4940 postfix_expression = error_mark_node;
4941 idk = CP_ID_KIND_NONE;
4942 is_member_access = false;
4946 if (pidk_return != NULL)
4947 * pidk_return = idk;
4948 if (member_access_only_p)
4949 return is_member_access? postfix_expression : error_mark_node;
4951 return postfix_expression;
4955 /* We should never get here. */
4957 return error_mark_node;
4960 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4961 by cp_parser_builtin_offsetof. We're looking for
4963 postfix-expression [ expression ]
4965 FOR_OFFSETOF is set if we're being called in that context, which
4966 changes how we deal with integer constant expressions. */
4969 cp_parser_postfix_open_square_expression (cp_parser *parser,
4970 tree postfix_expression,
4975 /* Consume the `[' token. */
4976 cp_lexer_consume_token (parser->lexer);
4978 /* Parse the index expression. */
4979 /* ??? For offsetof, there is a question of what to allow here. If
4980 offsetof is not being used in an integral constant expression context,
4981 then we *could* get the right answer by computing the value at runtime.
4982 If we are in an integral constant expression context, then we might
4983 could accept any constant expression; hard to say without analysis.
4984 Rather than open the barn door too wide right away, allow only integer
4985 constant expressions here. */
4987 index = cp_parser_constant_expression (parser, false, NULL);
4989 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4991 /* Look for the closing `]'. */
4992 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4994 /* Build the ARRAY_REF. */
4995 postfix_expression = grok_array_decl (postfix_expression, index);
4997 /* When not doing offsetof, array references are not permitted in
4998 constant-expressions. */
5000 && (cp_parser_non_integral_constant_expression
5001 (parser, "an array reference")))
5002 postfix_expression = error_mark_node;
5004 return postfix_expression;
5007 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5008 by cp_parser_builtin_offsetof. We're looking for
5010 postfix-expression . template [opt] id-expression
5011 postfix-expression . pseudo-destructor-name
5012 postfix-expression -> template [opt] id-expression
5013 postfix-expression -> pseudo-destructor-name
5015 FOR_OFFSETOF is set if we're being called in that context. That sorta
5016 limits what of the above we'll actually accept, but nevermind.
5017 TOKEN_TYPE is the "." or "->" token, which will already have been
5018 removed from the stream. */
5021 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5022 enum cpp_ttype token_type,
5023 tree postfix_expression,
5024 bool for_offsetof, cp_id_kind *idk,
5025 location_t location)
5029 bool pseudo_destructor_p;
5030 tree scope = NULL_TREE;
5032 /* If this is a `->' operator, dereference the pointer. */
5033 if (token_type == CPP_DEREF)
5034 postfix_expression = build_x_arrow (postfix_expression);
5035 /* Check to see whether or not the expression is type-dependent. */
5036 dependent_p = type_dependent_expression_p (postfix_expression);
5037 /* The identifier following the `->' or `.' is not qualified. */
5038 parser->scope = NULL_TREE;
5039 parser->qualifying_scope = NULL_TREE;
5040 parser->object_scope = NULL_TREE;
5041 *idk = CP_ID_KIND_NONE;
5043 /* Enter the scope corresponding to the type of the object
5044 given by the POSTFIX_EXPRESSION. */
5045 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5047 scope = TREE_TYPE (postfix_expression);
5048 /* According to the standard, no expression should ever have
5049 reference type. Unfortunately, we do not currently match
5050 the standard in this respect in that our internal representation
5051 of an expression may have reference type even when the standard
5052 says it does not. Therefore, we have to manually obtain the
5053 underlying type here. */
5054 scope = non_reference (scope);
5055 /* The type of the POSTFIX_EXPRESSION must be complete. */
5056 if (scope == unknown_type_node)
5058 error_at (location, "%qE does not have class type",
5059 postfix_expression);
5063 scope = complete_type_or_else (scope, NULL_TREE);
5064 /* Let the name lookup machinery know that we are processing a
5065 class member access expression. */
5066 parser->context->object_type = scope;
5067 /* If something went wrong, we want to be able to discern that case,
5068 as opposed to the case where there was no SCOPE due to the type
5069 of expression being dependent. */
5071 scope = error_mark_node;
5072 /* If the SCOPE was erroneous, make the various semantic analysis
5073 functions exit quickly -- and without issuing additional error
5075 if (scope == error_mark_node)
5076 postfix_expression = error_mark_node;
5079 /* Assume this expression is not a pseudo-destructor access. */
5080 pseudo_destructor_p = false;
5082 /* If the SCOPE is a scalar type, then, if this is a valid program,
5083 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5084 is type dependent, it can be pseudo-destructor-name or something else.
5085 Try to parse it as pseudo-destructor-name first. */
5086 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5091 cp_parser_parse_tentatively (parser);
5092 /* Parse the pseudo-destructor-name. */
5094 cp_parser_pseudo_destructor_name (parser, &s, &type);
5096 && (cp_parser_error_occurred (parser)
5097 || TREE_CODE (type) != TYPE_DECL
5098 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5099 cp_parser_abort_tentative_parse (parser);
5100 else if (cp_parser_parse_definitely (parser))
5102 pseudo_destructor_p = true;
5104 = finish_pseudo_destructor_expr (postfix_expression,
5105 s, TREE_TYPE (type));
5109 if (!pseudo_destructor_p)
5111 /* If the SCOPE is not a scalar type, we are looking at an
5112 ordinary class member access expression, rather than a
5113 pseudo-destructor-name. */
5115 cp_token *token = cp_lexer_peek_token (parser->lexer);
5116 /* Parse the id-expression. */
5117 name = (cp_parser_id_expression
5119 cp_parser_optional_template_keyword (parser),
5120 /*check_dependency_p=*/true,
5122 /*declarator_p=*/false,
5123 /*optional_p=*/false));
5124 /* In general, build a SCOPE_REF if the member name is qualified.
5125 However, if the name was not dependent and has already been
5126 resolved; there is no need to build the SCOPE_REF. For example;
5128 struct X { void f(); };
5129 template <typename T> void f(T* t) { t->X::f(); }
5131 Even though "t" is dependent, "X::f" is not and has been resolved
5132 to a BASELINK; there is no need to include scope information. */
5134 /* But we do need to remember that there was an explicit scope for
5135 virtual function calls. */
5137 *idk = CP_ID_KIND_QUALIFIED;
5139 /* If the name is a template-id that names a type, we will get a
5140 TYPE_DECL here. That is invalid code. */
5141 if (TREE_CODE (name) == TYPE_DECL)
5143 error_at (token->location, "invalid use of %qD", name);
5144 postfix_expression = error_mark_node;
5148 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5150 name = build_qualified_name (/*type=*/NULL_TREE,
5154 parser->scope = NULL_TREE;
5155 parser->qualifying_scope = NULL_TREE;
5156 parser->object_scope = NULL_TREE;
5158 if (scope && name && BASELINK_P (name))
5159 adjust_result_of_qualified_name_lookup
5160 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5162 = finish_class_member_access_expr (postfix_expression, name,
5164 tf_warning_or_error);
5168 /* We no longer need to look up names in the scope of the object on
5169 the left-hand side of the `.' or `->' operator. */
5170 parser->context->object_type = NULL_TREE;
5172 /* Outside of offsetof, these operators may not appear in
5173 constant-expressions. */
5175 && (cp_parser_non_integral_constant_expression
5176 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5177 postfix_expression = error_mark_node;
5179 return postfix_expression;
5182 /* Parse a parenthesized expression-list.
5185 assignment-expression
5186 expression-list, assignment-expression
5191 identifier, expression-list
5193 CAST_P is true if this expression is the target of a cast.
5195 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5198 Returns a vector of trees. Each element is a representation of an
5199 assignment-expression. NULL is returned if the ( and or ) are
5200 missing. An empty, but allocated, vector is returned on no
5201 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5202 if this is really an attribute list being parsed. If
5203 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5204 not all of the expressions in the list were constant. */
5206 static VEC(tree,gc) *
5207 cp_parser_parenthesized_expression_list (cp_parser* parser,
5208 bool is_attribute_list,
5210 bool allow_expansion_p,
5211 bool *non_constant_p)
5213 VEC(tree,gc) *expression_list;
5214 bool fold_expr_p = is_attribute_list;
5215 tree identifier = NULL_TREE;
5216 bool saved_greater_than_is_operator_p;
5218 /* Assume all the expressions will be constant. */
5220 *non_constant_p = false;
5222 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5225 expression_list = make_tree_vector ();
5227 /* Within a parenthesized expression, a `>' token is always
5228 the greater-than operator. */
5229 saved_greater_than_is_operator_p
5230 = parser->greater_than_is_operator_p;
5231 parser->greater_than_is_operator_p = true;
5233 /* Consume expressions until there are no more. */
5234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5239 /* At the beginning of attribute lists, check to see if the
5240 next token is an identifier. */
5241 if (is_attribute_list
5242 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5246 /* Consume the identifier. */
5247 token = cp_lexer_consume_token (parser->lexer);
5248 /* Save the identifier. */
5249 identifier = token->u.value;
5253 bool expr_non_constant_p;
5255 /* Parse the next assignment-expression. */
5256 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5258 /* A braced-init-list. */
5259 maybe_warn_cpp0x ("extended initializer lists");
5260 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5261 if (non_constant_p && expr_non_constant_p)
5262 *non_constant_p = true;
5264 else if (non_constant_p)
5266 expr = (cp_parser_constant_expression
5267 (parser, /*allow_non_constant_p=*/true,
5268 &expr_non_constant_p));
5269 if (expr_non_constant_p)
5270 *non_constant_p = true;
5273 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5276 expr = fold_non_dependent_expr (expr);
5278 /* If we have an ellipsis, then this is an expression
5280 if (allow_expansion_p
5281 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5283 /* Consume the `...'. */
5284 cp_lexer_consume_token (parser->lexer);
5286 /* Build the argument pack. */
5287 expr = make_pack_expansion (expr);
5290 /* Add it to the list. We add error_mark_node
5291 expressions to the list, so that we can still tell if
5292 the correct form for a parenthesized expression-list
5293 is found. That gives better errors. */
5294 VEC_safe_push (tree, gc, expression_list, expr);
5296 if (expr == error_mark_node)
5300 /* After the first item, attribute lists look the same as
5301 expression lists. */
5302 is_attribute_list = false;
5305 /* If the next token isn't a `,', then we are done. */
5306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5309 /* Otherwise, consume the `,' and keep going. */
5310 cp_lexer_consume_token (parser->lexer);
5313 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5318 /* We try and resync to an unnested comma, as that will give the
5319 user better diagnostics. */
5320 ending = cp_parser_skip_to_closing_parenthesis (parser,
5321 /*recovering=*/true,
5323 /*consume_paren=*/true);
5328 parser->greater_than_is_operator_p
5329 = saved_greater_than_is_operator_p;
5334 parser->greater_than_is_operator_p
5335 = saved_greater_than_is_operator_p;
5338 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5340 return expression_list;
5343 /* Parse a pseudo-destructor-name.
5345 pseudo-destructor-name:
5346 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5347 :: [opt] nested-name-specifier template template-id :: ~ type-name
5348 :: [opt] nested-name-specifier [opt] ~ type-name
5350 If either of the first two productions is used, sets *SCOPE to the
5351 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5352 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5353 or ERROR_MARK_NODE if the parse fails. */
5356 cp_parser_pseudo_destructor_name (cp_parser* parser,
5360 bool nested_name_specifier_p;
5362 /* Assume that things will not work out. */
5363 *type = error_mark_node;
5365 /* Look for the optional `::' operator. */
5366 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5367 /* Look for the optional nested-name-specifier. */
5368 nested_name_specifier_p
5369 = (cp_parser_nested_name_specifier_opt (parser,
5370 /*typename_keyword_p=*/false,
5371 /*check_dependency_p=*/true,
5373 /*is_declaration=*/false)
5375 /* Now, if we saw a nested-name-specifier, we might be doing the
5376 second production. */
5377 if (nested_name_specifier_p
5378 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5380 /* Consume the `template' keyword. */
5381 cp_lexer_consume_token (parser->lexer);
5382 /* Parse the template-id. */
5383 cp_parser_template_id (parser,
5384 /*template_keyword_p=*/true,
5385 /*check_dependency_p=*/false,
5386 /*is_declaration=*/true);
5387 /* Look for the `::' token. */
5388 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5390 /* If the next token is not a `~', then there might be some
5391 additional qualification. */
5392 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5394 /* At this point, we're looking for "type-name :: ~". The type-name
5395 must not be a class-name, since this is a pseudo-destructor. So,
5396 it must be either an enum-name, or a typedef-name -- both of which
5397 are just identifiers. So, we peek ahead to check that the "::"
5398 and "~" tokens are present; if they are not, then we can avoid
5399 calling type_name. */
5400 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5401 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5402 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5404 cp_parser_error (parser, "non-scalar type");
5408 /* Look for the type-name. */
5409 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5410 if (*scope == error_mark_node)
5413 /* Look for the `::' token. */
5414 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5419 /* Look for the `~'. */
5420 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5421 /* Look for the type-name again. We are not responsible for
5422 checking that it matches the first type-name. */
5423 *type = cp_parser_nonclass_name (parser);
5426 /* Parse a unary-expression.
5432 unary-operator cast-expression
5433 sizeof unary-expression
5441 __extension__ cast-expression
5442 __alignof__ unary-expression
5443 __alignof__ ( type-id )
5444 __real__ cast-expression
5445 __imag__ cast-expression
5448 ADDRESS_P is true iff the unary-expression is appearing as the
5449 operand of the `&' operator. CAST_P is true if this expression is
5450 the target of a cast.
5452 Returns a representation of the expression. */
5455 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5459 enum tree_code unary_operator;
5461 /* Peek at the next token. */
5462 token = cp_lexer_peek_token (parser->lexer);
5463 /* Some keywords give away the kind of expression. */
5464 if (token->type == CPP_KEYWORD)
5466 enum rid keyword = token->keyword;
5476 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5477 /* Consume the token. */
5478 cp_lexer_consume_token (parser->lexer);
5479 /* Parse the operand. */
5480 operand = cp_parser_sizeof_operand (parser, keyword);
5482 if (TYPE_P (operand))
5483 return cxx_sizeof_or_alignof_type (operand, op, true);
5485 return cxx_sizeof_or_alignof_expr (operand, op, true);
5489 return cp_parser_new_expression (parser);
5492 return cp_parser_delete_expression (parser);
5496 /* The saved value of the PEDANTIC flag. */
5500 /* Save away the PEDANTIC flag. */
5501 cp_parser_extension_opt (parser, &saved_pedantic);
5502 /* Parse the cast-expression. */
5503 expr = cp_parser_simple_cast_expression (parser);
5504 /* Restore the PEDANTIC flag. */
5505 pedantic = saved_pedantic;
5515 /* Consume the `__real__' or `__imag__' token. */
5516 cp_lexer_consume_token (parser->lexer);
5517 /* Parse the cast-expression. */
5518 expression = cp_parser_simple_cast_expression (parser);
5519 /* Create the complete representation. */
5520 return build_x_unary_op ((keyword == RID_REALPART
5521 ? REALPART_EXPR : IMAGPART_EXPR),
5523 tf_warning_or_error);
5532 /* Look for the `:: new' and `:: delete', which also signal the
5533 beginning of a new-expression, or delete-expression,
5534 respectively. If the next token is `::', then it might be one of
5536 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5540 /* See if the token after the `::' is one of the keywords in
5541 which we're interested. */
5542 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5543 /* If it's `new', we have a new-expression. */
5544 if (keyword == RID_NEW)
5545 return cp_parser_new_expression (parser);
5546 /* Similarly, for `delete'. */
5547 else if (keyword == RID_DELETE)
5548 return cp_parser_delete_expression (parser);
5551 /* Look for a unary operator. */
5552 unary_operator = cp_parser_unary_operator (token);
5553 /* The `++' and `--' operators can be handled similarly, even though
5554 they are not technically unary-operators in the grammar. */
5555 if (unary_operator == ERROR_MARK)
5557 if (token->type == CPP_PLUS_PLUS)
5558 unary_operator = PREINCREMENT_EXPR;
5559 else if (token->type == CPP_MINUS_MINUS)
5560 unary_operator = PREDECREMENT_EXPR;
5561 /* Handle the GNU address-of-label extension. */
5562 else if (cp_parser_allow_gnu_extensions_p (parser)
5563 && token->type == CPP_AND_AND)
5567 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5569 /* Consume the '&&' token. */
5570 cp_lexer_consume_token (parser->lexer);
5571 /* Look for the identifier. */
5572 identifier = cp_parser_identifier (parser);
5573 /* Create an expression representing the address. */
5574 expression = finish_label_address_expr (identifier, loc);
5575 if (cp_parser_non_integral_constant_expression (parser,
5576 "the address of a label"))
5577 expression = error_mark_node;
5581 if (unary_operator != ERROR_MARK)
5583 tree cast_expression;
5584 tree expression = error_mark_node;
5585 const char *non_constant_p = NULL;
5587 /* Consume the operator token. */
5588 token = cp_lexer_consume_token (parser->lexer);
5589 /* Parse the cast-expression. */
5591 = cp_parser_cast_expression (parser,
5592 unary_operator == ADDR_EXPR,
5593 /*cast_p=*/false, pidk);
5594 /* Now, build an appropriate representation. */
5595 switch (unary_operator)
5598 non_constant_p = "%<*%>";
5599 expression = build_x_indirect_ref (cast_expression, "unary *",
5600 tf_warning_or_error);
5604 non_constant_p = "%<&%>";
5607 expression = build_x_unary_op (unary_operator, cast_expression,
5608 tf_warning_or_error);
5611 case PREINCREMENT_EXPR:
5612 case PREDECREMENT_EXPR:
5613 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5614 ? "%<++%>" : "%<--%>");
5616 case UNARY_PLUS_EXPR:
5618 case TRUTH_NOT_EXPR:
5619 expression = finish_unary_op_expr (unary_operator, cast_expression);
5627 && cp_parser_non_integral_constant_expression (parser,
5629 expression = error_mark_node;
5634 return cp_parser_postfix_expression (parser, address_p, cast_p,
5635 /*member_access_only_p=*/false,
5639 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5640 unary-operator, the corresponding tree code is returned. */
5642 static enum tree_code
5643 cp_parser_unary_operator (cp_token* token)
5645 switch (token->type)
5648 return INDIRECT_REF;
5654 return UNARY_PLUS_EXPR;
5660 return TRUTH_NOT_EXPR;
5663 return BIT_NOT_EXPR;
5670 /* Parse a new-expression.
5673 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5674 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5676 Returns a representation of the expression. */
5679 cp_parser_new_expression (cp_parser* parser)
5681 bool global_scope_p;
5682 VEC(tree,gc) *placement;
5684 VEC(tree,gc) *initializer;
5688 /* Look for the optional `::' operator. */
5690 = (cp_parser_global_scope_opt (parser,
5691 /*current_scope_valid_p=*/false)
5693 /* Look for the `new' operator. */
5694 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5695 /* There's no easy way to tell a new-placement from the
5696 `( type-id )' construct. */
5697 cp_parser_parse_tentatively (parser);
5698 /* Look for a new-placement. */
5699 placement = cp_parser_new_placement (parser);
5700 /* If that didn't work out, there's no new-placement. */
5701 if (!cp_parser_parse_definitely (parser))
5703 if (placement != NULL)
5704 release_tree_vector (placement);
5708 /* If the next token is a `(', then we have a parenthesized
5710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5713 /* Consume the `('. */
5714 cp_lexer_consume_token (parser->lexer);
5715 /* Parse the type-id. */
5716 type = cp_parser_type_id (parser);
5717 /* Look for the closing `)'. */
5718 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5719 token = cp_lexer_peek_token (parser->lexer);
5720 /* There should not be a direct-new-declarator in this production,
5721 but GCC used to allowed this, so we check and emit a sensible error
5722 message for this case. */
5723 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5725 error_at (token->location,
5726 "array bound forbidden after parenthesized type-id");
5727 inform (token->location,
5728 "try removing the parentheses around the type-id");
5729 cp_parser_direct_new_declarator (parser);
5733 /* Otherwise, there must be a new-type-id. */
5735 type = cp_parser_new_type_id (parser, &nelts);
5737 /* If the next token is a `(' or '{', then we have a new-initializer. */
5738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5739 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5740 initializer = cp_parser_new_initializer (parser);
5744 /* A new-expression may not appear in an integral constant
5746 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5747 ret = error_mark_node;
5750 /* Create a representation of the new-expression. */
5751 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5752 tf_warning_or_error);
5755 if (placement != NULL)
5756 release_tree_vector (placement);
5757 if (initializer != NULL)
5758 release_tree_vector (initializer);
5763 /* Parse a new-placement.
5768 Returns the same representation as for an expression-list. */
5770 static VEC(tree,gc) *
5771 cp_parser_new_placement (cp_parser* parser)
5773 VEC(tree,gc) *expression_list;
5775 /* Parse the expression-list. */
5776 expression_list = (cp_parser_parenthesized_expression_list
5777 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5778 /*non_constant_p=*/NULL));
5780 return expression_list;
5783 /* Parse a new-type-id.
5786 type-specifier-seq new-declarator [opt]
5788 Returns the TYPE allocated. If the new-type-id indicates an array
5789 type, *NELTS is set to the number of elements in the last array
5790 bound; the TYPE will not include the last array bound. */
5793 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5795 cp_decl_specifier_seq type_specifier_seq;
5796 cp_declarator *new_declarator;
5797 cp_declarator *declarator;
5798 cp_declarator *outer_declarator;
5799 const char *saved_message;
5802 /* The type-specifier sequence must not contain type definitions.
5803 (It cannot contain declarations of new types either, but if they
5804 are not definitions we will catch that because they are not
5806 saved_message = parser->type_definition_forbidden_message;
5807 parser->type_definition_forbidden_message
5808 = "types may not be defined in a new-type-id";
5809 /* Parse the type-specifier-seq. */
5810 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5811 /*is_trailing_return=*/false,
5812 &type_specifier_seq);
5813 /* Restore the old message. */
5814 parser->type_definition_forbidden_message = saved_message;
5815 /* Parse the new-declarator. */
5816 new_declarator = cp_parser_new_declarator_opt (parser);
5818 /* Determine the number of elements in the last array dimension, if
5821 /* Skip down to the last array dimension. */
5822 declarator = new_declarator;
5823 outer_declarator = NULL;
5824 while (declarator && (declarator->kind == cdk_pointer
5825 || declarator->kind == cdk_ptrmem))
5827 outer_declarator = declarator;
5828 declarator = declarator->declarator;
5831 && declarator->kind == cdk_array
5832 && declarator->declarator
5833 && declarator->declarator->kind == cdk_array)
5835 outer_declarator = declarator;
5836 declarator = declarator->declarator;
5839 if (declarator && declarator->kind == cdk_array)
5841 *nelts = declarator->u.array.bounds;
5842 if (*nelts == error_mark_node)
5843 *nelts = integer_one_node;
5845 if (outer_declarator)
5846 outer_declarator->declarator = declarator->declarator;
5848 new_declarator = NULL;
5851 type = groktypename (&type_specifier_seq, new_declarator, false);
5855 /* Parse an (optional) new-declarator.
5858 ptr-operator new-declarator [opt]
5859 direct-new-declarator
5861 Returns the declarator. */
5863 static cp_declarator *
5864 cp_parser_new_declarator_opt (cp_parser* parser)
5866 enum tree_code code;
5868 cp_cv_quals cv_quals;
5870 /* We don't know if there's a ptr-operator next, or not. */
5871 cp_parser_parse_tentatively (parser);
5872 /* Look for a ptr-operator. */
5873 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5874 /* If that worked, look for more new-declarators. */
5875 if (cp_parser_parse_definitely (parser))
5877 cp_declarator *declarator;
5879 /* Parse another optional declarator. */
5880 declarator = cp_parser_new_declarator_opt (parser);
5882 return cp_parser_make_indirect_declarator
5883 (code, type, cv_quals, declarator);
5886 /* If the next token is a `[', there is a direct-new-declarator. */
5887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5888 return cp_parser_direct_new_declarator (parser);
5893 /* Parse a direct-new-declarator.
5895 direct-new-declarator:
5897 direct-new-declarator [constant-expression]
5901 static cp_declarator *
5902 cp_parser_direct_new_declarator (cp_parser* parser)
5904 cp_declarator *declarator = NULL;
5910 /* Look for the opening `['. */
5911 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5912 /* The first expression is not required to be constant. */
5915 cp_token *token = cp_lexer_peek_token (parser->lexer);
5916 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5917 /* The standard requires that the expression have integral
5918 type. DR 74 adds enumeration types. We believe that the
5919 real intent is that these expressions be handled like the
5920 expression in a `switch' condition, which also allows
5921 classes with a single conversion to integral or
5922 enumeration type. */
5923 if (!processing_template_decl)
5926 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5931 error_at (token->location,
5932 "expression in new-declarator must have integral "
5933 "or enumeration type");
5934 expression = error_mark_node;
5938 /* But all the other expressions must be. */
5941 = cp_parser_constant_expression (parser,
5942 /*allow_non_constant=*/false,
5944 /* Look for the closing `]'. */
5945 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5947 /* Add this bound to the declarator. */
5948 declarator = make_array_declarator (declarator, expression);
5950 /* If the next token is not a `[', then there are no more
5952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5959 /* Parse a new-initializer.
5962 ( expression-list [opt] )
5965 Returns a representation of the expression-list. */
5967 static VEC(tree,gc) *
5968 cp_parser_new_initializer (cp_parser* parser)
5970 VEC(tree,gc) *expression_list;
5972 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5975 bool expr_non_constant_p;
5976 maybe_warn_cpp0x ("extended initializer lists");
5977 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5978 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5979 expression_list = make_tree_vector_single (t);
5982 expression_list = (cp_parser_parenthesized_expression_list
5983 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5984 /*non_constant_p=*/NULL));
5986 return expression_list;
5989 /* Parse a delete-expression.
5992 :: [opt] delete cast-expression
5993 :: [opt] delete [ ] cast-expression
5995 Returns a representation of the expression. */
5998 cp_parser_delete_expression (cp_parser* parser)
6000 bool global_scope_p;
6004 /* Look for the optional `::' operator. */
6006 = (cp_parser_global_scope_opt (parser,
6007 /*current_scope_valid_p=*/false)
6009 /* Look for the `delete' keyword. */
6010 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6011 /* See if the array syntax is in use. */
6012 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6014 /* Consume the `[' token. */
6015 cp_lexer_consume_token (parser->lexer);
6016 /* Look for the `]' token. */
6017 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6018 /* Remember that this is the `[]' construct. */
6024 /* Parse the cast-expression. */
6025 expression = cp_parser_simple_cast_expression (parser);
6027 /* A delete-expression may not appear in an integral constant
6029 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6030 return error_mark_node;
6032 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6035 /* Returns true if TOKEN may start a cast-expression and false
6039 cp_parser_token_starts_cast_expression (cp_token *token)
6041 switch (token->type)
6047 case CPP_CLOSE_SQUARE:
6048 case CPP_CLOSE_PAREN:
6049 case CPP_CLOSE_BRACE:
6053 case CPP_DEREF_STAR:
6061 case CPP_GREATER_EQ:
6081 /* '[' may start a primary-expression in obj-c++. */
6082 case CPP_OPEN_SQUARE:
6083 return c_dialect_objc ();
6090 /* Parse a cast-expression.
6094 ( type-id ) cast-expression
6096 ADDRESS_P is true iff the unary-expression is appearing as the
6097 operand of the `&' operator. CAST_P is true if this expression is
6098 the target of a cast.
6100 Returns a representation of the expression. */
6103 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6106 /* If it's a `(', then we might be looking at a cast. */
6107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6109 tree type = NULL_TREE;
6110 tree expr = NULL_TREE;
6111 bool compound_literal_p;
6112 const char *saved_message;
6114 /* There's no way to know yet whether or not this is a cast.
6115 For example, `(int (3))' is a unary-expression, while `(int)
6116 3' is a cast. So, we resort to parsing tentatively. */
6117 cp_parser_parse_tentatively (parser);
6118 /* Types may not be defined in a cast. */
6119 saved_message = parser->type_definition_forbidden_message;
6120 parser->type_definition_forbidden_message
6121 = "types may not be defined in casts";
6122 /* Consume the `('. */
6123 cp_lexer_consume_token (parser->lexer);
6124 /* A very tricky bit is that `(struct S) { 3 }' is a
6125 compound-literal (which we permit in C++ as an extension).
6126 But, that construct is not a cast-expression -- it is a
6127 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6128 is legal; if the compound-literal were a cast-expression,
6129 you'd need an extra set of parentheses.) But, if we parse
6130 the type-id, and it happens to be a class-specifier, then we
6131 will commit to the parse at that point, because we cannot
6132 undo the action that is done when creating a new class. So,
6133 then we cannot back up and do a postfix-expression.
6135 Therefore, we scan ahead to the closing `)', and check to see
6136 if the token after the `)' is a `{'. If so, we are not
6137 looking at a cast-expression.
6139 Save tokens so that we can put them back. */
6140 cp_lexer_save_tokens (parser->lexer);
6141 /* Skip tokens until the next token is a closing parenthesis.
6142 If we find the closing `)', and the next token is a `{', then
6143 we are looking at a compound-literal. */
6145 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6146 /*consume_paren=*/true)
6147 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6148 /* Roll back the tokens we skipped. */
6149 cp_lexer_rollback_tokens (parser->lexer);
6150 /* If we were looking at a compound-literal, simulate an error
6151 so that the call to cp_parser_parse_definitely below will
6153 if (compound_literal_p)
6154 cp_parser_simulate_error (parser);
6157 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6158 parser->in_type_id_in_expr_p = true;
6159 /* Look for the type-id. */
6160 type = cp_parser_type_id (parser);
6161 /* Look for the closing `)'. */
6162 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6163 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6166 /* Restore the saved message. */
6167 parser->type_definition_forbidden_message = saved_message;
6169 /* At this point this can only be either a cast or a
6170 parenthesized ctor such as `(T ())' that looks like a cast to
6171 function returning T. */
6172 if (!cp_parser_error_occurred (parser)
6173 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6176 cp_parser_parse_definitely (parser);
6177 expr = cp_parser_cast_expression (parser,
6178 /*address_p=*/false,
6179 /*cast_p=*/true, pidk);
6181 /* Warn about old-style casts, if so requested. */
6182 if (warn_old_style_cast
6183 && !in_system_header
6184 && !VOID_TYPE_P (type)
6185 && current_lang_name != lang_name_c)
6186 warning (OPT_Wold_style_cast, "use of old-style cast");
6188 /* Only type conversions to integral or enumeration types
6189 can be used in constant-expressions. */
6190 if (!cast_valid_in_integral_constant_expression_p (type)
6191 && (cp_parser_non_integral_constant_expression
6193 "a cast to a type other than an integral or "
6194 "enumeration type")))
6195 return error_mark_node;
6197 /* Perform the cast. */
6198 expr = build_c_cast (input_location, type, expr);
6202 cp_parser_abort_tentative_parse (parser);
6205 /* If we get here, then it's not a cast, so it must be a
6206 unary-expression. */
6207 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6210 /* Parse a binary expression of the general form:
6214 pm-expression .* cast-expression
6215 pm-expression ->* cast-expression
6217 multiplicative-expression:
6219 multiplicative-expression * pm-expression
6220 multiplicative-expression / pm-expression
6221 multiplicative-expression % pm-expression
6223 additive-expression:
6224 multiplicative-expression
6225 additive-expression + multiplicative-expression
6226 additive-expression - multiplicative-expression
6230 shift-expression << additive-expression
6231 shift-expression >> additive-expression
6233 relational-expression:
6235 relational-expression < shift-expression
6236 relational-expression > shift-expression
6237 relational-expression <= shift-expression
6238 relational-expression >= shift-expression
6242 relational-expression:
6243 relational-expression <? shift-expression
6244 relational-expression >? shift-expression
6246 equality-expression:
6247 relational-expression
6248 equality-expression == relational-expression
6249 equality-expression != relational-expression
6253 and-expression & equality-expression
6255 exclusive-or-expression:
6257 exclusive-or-expression ^ and-expression
6259 inclusive-or-expression:
6260 exclusive-or-expression
6261 inclusive-or-expression | exclusive-or-expression
6263 logical-and-expression:
6264 inclusive-or-expression
6265 logical-and-expression && inclusive-or-expression
6267 logical-or-expression:
6268 logical-and-expression
6269 logical-or-expression || logical-and-expression
6271 All these are implemented with a single function like:
6274 simple-cast-expression
6275 binary-expression <token> binary-expression
6277 CAST_P is true if this expression is the target of a cast.
6279 The binops_by_token map is used to get the tree codes for each <token> type.
6280 binary-expressions are associated according to a precedence table. */
6282 #define TOKEN_PRECEDENCE(token) \
6283 (((token->type == CPP_GREATER \
6284 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6285 && !parser->greater_than_is_operator_p) \
6286 ? PREC_NOT_OPERATOR \
6287 : binops_by_token[token->type].prec)
6290 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6291 bool no_toplevel_fold_p,
6292 enum cp_parser_prec prec,
6295 cp_parser_expression_stack stack;
6296 cp_parser_expression_stack_entry *sp = &stack[0];
6299 enum tree_code tree_type, lhs_type, rhs_type;
6300 enum cp_parser_prec new_prec, lookahead_prec;
6303 /* Parse the first expression. */
6304 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6305 lhs_type = ERROR_MARK;
6309 /* Get an operator token. */
6310 token = cp_lexer_peek_token (parser->lexer);
6312 if (warn_cxx0x_compat
6313 && token->type == CPP_RSHIFT
6314 && !parser->greater_than_is_operator_p)
6316 if (warning_at (token->location, OPT_Wc__0x_compat,
6317 "%<>>%> operator will be treated as"
6318 " two right angle brackets in C++0x"))
6319 inform (token->location,
6320 "suggest parentheses around %<>>%> expression");
6323 new_prec = TOKEN_PRECEDENCE (token);
6325 /* Popping an entry off the stack means we completed a subexpression:
6326 - either we found a token which is not an operator (`>' where it is not
6327 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6328 will happen repeatedly;
6329 - or, we found an operator which has lower priority. This is the case
6330 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6332 if (new_prec <= prec)
6341 tree_type = binops_by_token[token->type].tree_type;
6343 /* We used the operator token. */
6344 cp_lexer_consume_token (parser->lexer);
6346 /* For "false && x" or "true || x", x will never be executed;
6347 disable warnings while evaluating it. */
6348 if (tree_type == TRUTH_ANDIF_EXPR)
6349 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6350 else if (tree_type == TRUTH_ORIF_EXPR)
6351 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6353 /* Extract another operand. It may be the RHS of this expression
6354 or the LHS of a new, higher priority expression. */
6355 rhs = cp_parser_simple_cast_expression (parser);
6356 rhs_type = ERROR_MARK;
6358 /* Get another operator token. Look up its precedence to avoid
6359 building a useless (immediately popped) stack entry for common
6360 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6361 token = cp_lexer_peek_token (parser->lexer);
6362 lookahead_prec = TOKEN_PRECEDENCE (token);
6363 if (lookahead_prec > new_prec)
6365 /* ... and prepare to parse the RHS of the new, higher priority
6366 expression. Since precedence levels on the stack are
6367 monotonically increasing, we do not have to care about
6370 sp->tree_type = tree_type;
6372 sp->lhs_type = lhs_type;
6375 lhs_type = rhs_type;
6377 new_prec = lookahead_prec;
6381 lookahead_prec = new_prec;
6382 /* If the stack is not empty, we have parsed into LHS the right side
6383 (`4' in the example above) of an expression we had suspended.
6384 We can use the information on the stack to recover the LHS (`3')
6385 from the stack together with the tree code (`MULT_EXPR'), and
6386 the precedence of the higher level subexpression
6387 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6388 which will be used to actually build the additive expression. */
6391 tree_type = sp->tree_type;
6393 rhs_type = lhs_type;
6395 lhs_type = sp->lhs_type;
6398 /* Undo the disabling of warnings done above. */
6399 if (tree_type == TRUTH_ANDIF_EXPR)
6400 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6401 else if (tree_type == TRUTH_ORIF_EXPR)
6402 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6404 overloaded_p = false;
6405 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6406 ERROR_MARK for everything that is not a binary expression.
6407 This makes warn_about_parentheses miss some warnings that
6408 involve unary operators. For unary expressions we should
6409 pass the correct tree_code unless the unary expression was
6410 surrounded by parentheses.
6412 if (no_toplevel_fold_p
6413 && lookahead_prec <= prec
6415 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6416 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6418 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6419 &overloaded_p, tf_warning_or_error);
6420 lhs_type = tree_type;
6422 /* If the binary operator required the use of an overloaded operator,
6423 then this expression cannot be an integral constant-expression.
6424 An overloaded operator can be used even if both operands are
6425 otherwise permissible in an integral constant-expression if at
6426 least one of the operands is of enumeration type. */
6429 && (cp_parser_non_integral_constant_expression
6430 (parser, "calls to overloaded operators")))
6431 return error_mark_node;
6438 /* Parse the `? expression : assignment-expression' part of a
6439 conditional-expression. The LOGICAL_OR_EXPR is the
6440 logical-or-expression that started the conditional-expression.
6441 Returns a representation of the entire conditional-expression.
6443 This routine is used by cp_parser_assignment_expression.
6445 ? expression : assignment-expression
6449 ? : assignment-expression */
6452 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6455 tree assignment_expr;
6457 /* Consume the `?' token. */
6458 cp_lexer_consume_token (parser->lexer);
6459 if (cp_parser_allow_gnu_extensions_p (parser)
6460 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6462 /* Implicit true clause. */
6464 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6468 /* Parse the expression. */
6469 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6470 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6471 c_inhibit_evaluation_warnings +=
6472 ((logical_or_expr == truthvalue_true_node)
6473 - (logical_or_expr == truthvalue_false_node));
6476 /* The next token should be a `:'. */
6477 cp_parser_require (parser, CPP_COLON, "%<:%>");
6478 /* Parse the assignment-expression. */
6479 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6480 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6482 /* Build the conditional-expression. */
6483 return build_x_conditional_expr (logical_or_expr,
6486 tf_warning_or_error);
6489 /* Parse an assignment-expression.
6491 assignment-expression:
6492 conditional-expression
6493 logical-or-expression assignment-operator assignment_expression
6496 CAST_P is true if this expression is the target of a cast.
6498 Returns a representation for the expression. */
6501 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6506 /* If the next token is the `throw' keyword, then we're looking at
6507 a throw-expression. */
6508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6509 expr = cp_parser_throw_expression (parser);
6510 /* Otherwise, it must be that we are looking at a
6511 logical-or-expression. */
6514 /* Parse the binary expressions (logical-or-expression). */
6515 expr = cp_parser_binary_expression (parser, cast_p, false,
6516 PREC_NOT_OPERATOR, pidk);
6517 /* If the next token is a `?' then we're actually looking at a
6518 conditional-expression. */
6519 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6520 return cp_parser_question_colon_clause (parser, expr);
6523 enum tree_code assignment_operator;
6525 /* If it's an assignment-operator, we're using the second
6528 = cp_parser_assignment_operator_opt (parser);
6529 if (assignment_operator != ERROR_MARK)
6531 bool non_constant_p;
6533 /* Parse the right-hand side of the assignment. */
6534 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6536 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6537 maybe_warn_cpp0x ("extended initializer lists");
6539 /* An assignment may not appear in a
6540 constant-expression. */
6541 if (cp_parser_non_integral_constant_expression (parser,
6543 return error_mark_node;
6544 /* Build the assignment expression. */
6545 expr = build_x_modify_expr (expr,
6546 assignment_operator,
6548 tf_warning_or_error);
6556 /* Parse an (optional) assignment-operator.
6558 assignment-operator: one of
6559 = *= /= %= += -= >>= <<= &= ^= |=
6563 assignment-operator: one of
6566 If the next token is an assignment operator, the corresponding tree
6567 code is returned, and the token is consumed. For example, for
6568 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6569 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6570 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6571 operator, ERROR_MARK is returned. */
6573 static enum tree_code
6574 cp_parser_assignment_operator_opt (cp_parser* parser)
6579 /* Peek at the next token. */
6580 token = cp_lexer_peek_token (parser->lexer);
6582 switch (token->type)
6593 op = TRUNC_DIV_EXPR;
6597 op = TRUNC_MOD_EXPR;
6629 /* Nothing else is an assignment operator. */
6633 /* If it was an assignment operator, consume it. */
6634 if (op != ERROR_MARK)
6635 cp_lexer_consume_token (parser->lexer);
6640 /* Parse an expression.
6643 assignment-expression
6644 expression , assignment-expression
6646 CAST_P is true if this expression is the target of a cast.
6648 Returns a representation of the expression. */
6651 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6653 tree expression = NULL_TREE;
6657 tree assignment_expression;
6659 /* Parse the next assignment-expression. */
6660 assignment_expression
6661 = cp_parser_assignment_expression (parser, cast_p, pidk);
6662 /* If this is the first assignment-expression, we can just
6665 expression = assignment_expression;
6667 expression = build_x_compound_expr (expression,
6668 assignment_expression,
6669 tf_warning_or_error);
6670 /* If the next token is not a comma, then we are done with the
6672 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6674 /* Consume the `,'. */
6675 cp_lexer_consume_token (parser->lexer);
6676 /* A comma operator cannot appear in a constant-expression. */
6677 if (cp_parser_non_integral_constant_expression (parser,
6678 "a comma operator"))
6679 expression = error_mark_node;
6685 /* Parse a constant-expression.
6687 constant-expression:
6688 conditional-expression
6690 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6691 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6692 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6693 is false, NON_CONSTANT_P should be NULL. */
6696 cp_parser_constant_expression (cp_parser* parser,
6697 bool allow_non_constant_p,
6698 bool *non_constant_p)
6700 bool saved_integral_constant_expression_p;
6701 bool saved_allow_non_integral_constant_expression_p;
6702 bool saved_non_integral_constant_expression_p;
6705 /* It might seem that we could simply parse the
6706 conditional-expression, and then check to see if it were
6707 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6708 one that the compiler can figure out is constant, possibly after
6709 doing some simplifications or optimizations. The standard has a
6710 precise definition of constant-expression, and we must honor
6711 that, even though it is somewhat more restrictive.
6717 is not a legal declaration, because `(2, 3)' is not a
6718 constant-expression. The `,' operator is forbidden in a
6719 constant-expression. However, GCC's constant-folding machinery
6720 will fold this operation to an INTEGER_CST for `3'. */
6722 /* Save the old settings. */
6723 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6724 saved_allow_non_integral_constant_expression_p
6725 = parser->allow_non_integral_constant_expression_p;
6726 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6727 /* We are now parsing a constant-expression. */
6728 parser->integral_constant_expression_p = true;
6729 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6730 parser->non_integral_constant_expression_p = false;
6731 /* Although the grammar says "conditional-expression", we parse an
6732 "assignment-expression", which also permits "throw-expression"
6733 and the use of assignment operators. In the case that
6734 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6735 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6736 actually essential that we look for an assignment-expression.
6737 For example, cp_parser_initializer_clauses uses this function to
6738 determine whether a particular assignment-expression is in fact
6740 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6741 /* Restore the old settings. */
6742 parser->integral_constant_expression_p
6743 = saved_integral_constant_expression_p;
6744 parser->allow_non_integral_constant_expression_p
6745 = saved_allow_non_integral_constant_expression_p;
6746 if (allow_non_constant_p)
6747 *non_constant_p = parser->non_integral_constant_expression_p;
6748 else if (parser->non_integral_constant_expression_p)
6749 expression = error_mark_node;
6750 parser->non_integral_constant_expression_p
6751 = saved_non_integral_constant_expression_p;
6756 /* Parse __builtin_offsetof.
6758 offsetof-expression:
6759 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6761 offsetof-member-designator:
6763 | offsetof-member-designator "." id-expression
6764 | offsetof-member-designator "[" expression "]"
6765 | offsetof-member-designator "->" id-expression */
6768 cp_parser_builtin_offsetof (cp_parser *parser)
6770 int save_ice_p, save_non_ice_p;
6775 /* We're about to accept non-integral-constant things, but will
6776 definitely yield an integral constant expression. Save and
6777 restore these values around our local parsing. */
6778 save_ice_p = parser->integral_constant_expression_p;
6779 save_non_ice_p = parser->non_integral_constant_expression_p;
6781 /* Consume the "__builtin_offsetof" token. */
6782 cp_lexer_consume_token (parser->lexer);
6783 /* Consume the opening `('. */
6784 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6785 /* Parse the type-id. */
6786 type = cp_parser_type_id (parser);
6787 /* Look for the `,'. */
6788 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6789 token = cp_lexer_peek_token (parser->lexer);
6791 /* Build the (type *)null that begins the traditional offsetof macro. */
6792 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6793 tf_warning_or_error);
6795 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6796 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6797 true, &dummy, token->location);
6800 token = cp_lexer_peek_token (parser->lexer);
6801 switch (token->type)
6803 case CPP_OPEN_SQUARE:
6804 /* offsetof-member-designator "[" expression "]" */
6805 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6809 /* offsetof-member-designator "->" identifier */
6810 expr = grok_array_decl (expr, integer_zero_node);
6814 /* offsetof-member-designator "." identifier */
6815 cp_lexer_consume_token (parser->lexer);
6816 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6821 case CPP_CLOSE_PAREN:
6822 /* Consume the ")" token. */
6823 cp_lexer_consume_token (parser->lexer);
6827 /* Error. We know the following require will fail, but
6828 that gives the proper error message. */
6829 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6830 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6831 expr = error_mark_node;
6837 /* If we're processing a template, we can't finish the semantics yet.
6838 Otherwise we can fold the entire expression now. */
6839 if (processing_template_decl)
6840 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6842 expr = finish_offsetof (expr);
6845 parser->integral_constant_expression_p = save_ice_p;
6846 parser->non_integral_constant_expression_p = save_non_ice_p;
6851 /* Parse a trait expression. */
6854 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6857 tree type1, type2 = NULL_TREE;
6858 bool binary = false;
6859 cp_decl_specifier_seq decl_specs;
6863 case RID_HAS_NOTHROW_ASSIGN:
6864 kind = CPTK_HAS_NOTHROW_ASSIGN;
6866 case RID_HAS_NOTHROW_CONSTRUCTOR:
6867 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6869 case RID_HAS_NOTHROW_COPY:
6870 kind = CPTK_HAS_NOTHROW_COPY;
6872 case RID_HAS_TRIVIAL_ASSIGN:
6873 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6875 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6876 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6878 case RID_HAS_TRIVIAL_COPY:
6879 kind = CPTK_HAS_TRIVIAL_COPY;
6881 case RID_HAS_TRIVIAL_DESTRUCTOR:
6882 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6884 case RID_HAS_VIRTUAL_DESTRUCTOR:
6885 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6887 case RID_IS_ABSTRACT:
6888 kind = CPTK_IS_ABSTRACT;
6890 case RID_IS_BASE_OF:
6891 kind = CPTK_IS_BASE_OF;
6895 kind = CPTK_IS_CLASS;
6897 case RID_IS_CONVERTIBLE_TO:
6898 kind = CPTK_IS_CONVERTIBLE_TO;
6902 kind = CPTK_IS_EMPTY;
6905 kind = CPTK_IS_ENUM;
6910 case RID_IS_POLYMORPHIC:
6911 kind = CPTK_IS_POLYMORPHIC;
6913 case RID_IS_STD_LAYOUT:
6914 kind = CPTK_IS_STD_LAYOUT;
6916 case RID_IS_TRIVIAL:
6917 kind = CPTK_IS_TRIVIAL;
6920 kind = CPTK_IS_UNION;
6926 /* Consume the token. */
6927 cp_lexer_consume_token (parser->lexer);
6929 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6931 type1 = cp_parser_type_id (parser);
6933 if (type1 == error_mark_node)
6934 return error_mark_node;
6936 /* Build a trivial decl-specifier-seq. */
6937 clear_decl_specs (&decl_specs);
6938 decl_specs.type = type1;
6940 /* Call grokdeclarator to figure out what type this is. */
6941 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6942 /*initialized=*/0, /*attrlist=*/NULL);
6946 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6948 type2 = cp_parser_type_id (parser);
6950 if (type2 == error_mark_node)
6951 return error_mark_node;
6953 /* Build a trivial decl-specifier-seq. */
6954 clear_decl_specs (&decl_specs);
6955 decl_specs.type = type2;
6957 /* Call grokdeclarator to figure out what type this is. */
6958 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6959 /*initialized=*/0, /*attrlist=*/NULL);
6962 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6964 /* Complete the trait expression, which may mean either processing
6965 the trait expr now or saving it for template instantiation. */
6966 return finish_trait_expr (kind, type1, type2);
6969 /* Lambdas that appear in variable initializer or default argument scope
6970 get that in their mangling, so we need to record it. We might as well
6971 use the count for function and namespace scopes as well. */
6972 static GTY(()) tree lambda_scope;
6973 static GTY(()) int lambda_count;
6974 typedef struct GTY(()) tree_int
6979 DEF_VEC_O(tree_int);
6980 DEF_VEC_ALLOC_O(tree_int,gc);
6981 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6984 start_lambda_scope (tree decl)
6988 /* Once we're inside a function, we ignore other scopes and just push
6989 the function again so that popping works properly. */
6990 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6991 decl = current_function_decl;
6992 ti.t = lambda_scope;
6993 ti.i = lambda_count;
6994 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6995 if (lambda_scope != decl)
6997 /* Don't reset the count if we're still in the same function. */
6998 lambda_scope = decl;
7004 record_lambda_scope (tree lambda)
7006 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7007 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7011 finish_lambda_scope (void)
7013 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7014 if (lambda_scope != p->t)
7016 lambda_scope = p->t;
7017 lambda_count = p->i;
7019 VEC_pop (tree_int, lambda_scope_stack);
7022 /* Parse a lambda expression.
7025 lambda-introducer lambda-declarator [opt] compound-statement
7027 Returns a representation of the expression. */
7030 cp_parser_lambda_expression (cp_parser* parser)
7032 tree lambda_expr = build_lambda_expr ();
7035 LAMBDA_EXPR_LOCATION (lambda_expr)
7036 = cp_lexer_peek_token (parser->lexer)->location;
7038 /* We may be in the middle of deferred access check. Disable
7040 push_deferring_access_checks (dk_no_deferred);
7042 type = begin_lambda_type (lambda_expr);
7044 record_lambda_scope (lambda_expr);
7046 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7047 determine_visibility (TYPE_NAME (type));
7050 /* Inside the class, surrounding template-parameter-lists do not apply. */
7051 unsigned int saved_num_template_parameter_lists
7052 = parser->num_template_parameter_lists;
7054 parser->num_template_parameter_lists = 0;
7056 cp_parser_lambda_introducer (parser, lambda_expr);
7058 /* By virtue of defining a local class, a lambda expression has access to
7059 the private variables of enclosing classes. */
7061 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7063 cp_parser_lambda_body (parser, lambda_expr);
7065 /* The capture list was built up in reverse order; fix that now. */
7067 tree newlist = NULL_TREE;
7070 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7073 tree field = TREE_PURPOSE (elt);
7076 next = TREE_CHAIN (elt);
7077 TREE_CHAIN (elt) = newlist;
7080 /* Also add __ to the beginning of the field name so that code
7081 outside the lambda body can't see the captured name. We could
7082 just remove the name entirely, but this is more useful for
7084 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7085 /* The 'this' capture already starts with __. */
7088 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7089 buf[1] = buf[0] = '_';
7090 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7091 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7092 DECL_NAME (field) = get_identifier (buf);
7094 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7097 maybe_add_lambda_conv_op (type);
7099 type = finish_struct (type, /*attributes=*/NULL_TREE);
7101 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7104 pop_deferring_access_checks ();
7106 return build_lambda_object (lambda_expr);
7109 /* Parse the beginning of a lambda expression.
7112 [ lambda-capture [opt] ]
7114 LAMBDA_EXPR is the current representation of the lambda expression. */
7117 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7119 /* Need commas after the first capture. */
7122 /* Eat the leading `['. */
7123 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7125 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7126 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7127 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7128 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7129 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7130 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7132 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7134 cp_lexer_consume_token (parser->lexer);
7138 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7140 cp_token* capture_token;
7142 tree capture_init_expr;
7143 cp_id_kind idk = CP_ID_KIND_NONE;
7144 bool explicit_init_p = false;
7146 enum capture_kind_type
7151 enum capture_kind_type capture_kind = BY_COPY;
7153 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7155 error ("expected end of capture-list");
7162 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7164 /* Possibly capture `this'. */
7165 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7167 cp_lexer_consume_token (parser->lexer);
7168 add_capture (lambda_expr,
7169 /*id=*/get_identifier ("__this"),
7170 /*initializer=*/finish_this_expr(),
7171 /*by_reference_p=*/false,
7176 /* Remember whether we want to capture as a reference or not. */
7177 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7179 capture_kind = BY_REFERENCE;
7180 cp_lexer_consume_token (parser->lexer);
7183 /* Get the identifier. */
7184 capture_token = cp_lexer_peek_token (parser->lexer);
7185 capture_id = cp_parser_identifier (parser);
7187 if (capture_id == error_mark_node)
7188 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7189 delimiters, but I modified this to stop on unnested ']' as well. It
7190 was already changed to stop on unnested '}', so the
7191 "closing_parenthesis" name is no more misleading with my change. */
7193 cp_parser_skip_to_closing_parenthesis (parser,
7194 /*recovering=*/true,
7196 /*consume_paren=*/true);
7200 /* Find the initializer for this capture. */
7201 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7203 /* An explicit expression exists. */
7204 cp_lexer_consume_token (parser->lexer);
7205 pedwarn (input_location, OPT_pedantic,
7206 "ISO C++ does not allow initializers "
7207 "in lambda expression capture lists");
7208 capture_init_expr = cp_parser_assignment_expression (parser,
7211 explicit_init_p = true;
7215 const char* error_msg;
7217 /* Turn the identifier into an id-expression. */
7219 = cp_parser_lookup_name
7223 /*is_template=*/false,
7224 /*is_namespace=*/false,
7225 /*check_dependency=*/true,
7226 /*ambiguous_decls=*/NULL,
7227 capture_token->location);
7230 = finish_id_expression
7235 /*integral_constant_expression_p=*/false,
7236 /*allow_non_integral_constant_expression_p=*/false,
7237 /*non_integral_constant_expression_p=*/NULL,
7238 /*template_p=*/false,
7240 /*address_p=*/false,
7241 /*template_arg_p=*/false,
7243 capture_token->location);
7246 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7248 = unqualified_name_lookup_error (capture_init_expr);
7250 add_capture (lambda_expr,
7253 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7257 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7260 /* Parse the (optional) middle of a lambda expression.
7263 ( parameter-declaration-clause [opt] )
7264 attribute-specifier [opt]
7266 exception-specification [opt]
7267 lambda-return-type-clause [opt]
7269 LAMBDA_EXPR is the current representation of the lambda expression. */
7272 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7274 /* 5.1.1.4 of the standard says:
7275 If a lambda-expression does not include a lambda-declarator, it is as if
7276 the lambda-declarator were ().
7277 This means an empty parameter list, no attributes, and no exception
7279 tree param_list = void_list_node;
7280 tree attributes = NULL_TREE;
7281 tree exception_spec = NULL_TREE;
7284 /* The lambda-declarator is optional, but must begin with an opening
7285 parenthesis if present. */
7286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7288 cp_lexer_consume_token (parser->lexer);
7290 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7292 /* Parse parameters. */
7293 param_list = cp_parser_parameter_declaration_clause (parser);
7295 /* Default arguments shall not be specified in the
7296 parameter-declaration-clause of a lambda-declarator. */
7297 for (t = param_list; t; t = TREE_CHAIN (t))
7298 if (TREE_PURPOSE (t))
7299 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7300 "default argument specified for lambda parameter");
7302 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7304 attributes = cp_parser_attributes_opt (parser);
7306 /* Parse optional `mutable' keyword. */
7307 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7309 cp_lexer_consume_token (parser->lexer);
7310 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7313 /* Parse optional exception specification. */
7314 exception_spec = cp_parser_exception_specification_opt (parser);
7316 /* Parse optional trailing return type. */
7317 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7319 cp_lexer_consume_token (parser->lexer);
7320 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7323 /* The function parameters must be in scope all the way until after the
7324 trailing-return-type in case of decltype. */
7325 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7326 pop_binding (DECL_NAME (t), t);
7331 /* Create the function call operator.
7333 Messing with declarators like this is no uglier than building up the
7334 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7337 cp_decl_specifier_seq return_type_specs;
7338 cp_declarator* declarator;
7343 clear_decl_specs (&return_type_specs);
7344 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7345 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7347 /* Maybe we will deduce the return type later, but we can use void
7348 as a placeholder return type anyways. */
7349 return_type_specs.type = void_type_node;
7351 p = obstack_alloc (&declarator_obstack, 0);
7353 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7356 quals = TYPE_UNQUALIFIED;
7357 if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7358 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7360 /* A lambda with no captures has a static op() and a conversion op
7361 to function type. */
7362 if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7363 error ("lambda expression with no captures declared mutable");
7364 return_type_specs.storage_class = sc_static;
7366 else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7367 quals = TYPE_QUAL_CONST;
7368 declarator = make_call_declarator (declarator, param_list, quals,
7370 /*late_return_type=*/NULL_TREE);
7372 fco = grokmethod (&return_type_specs,
7375 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7376 DECL_ARTIFICIAL (fco) = 1;
7378 finish_member_declaration (fco);
7380 obstack_free (&declarator_obstack, p);
7384 /* Parse the body of a lambda expression, which is simply
7388 but which requires special handling.
7389 LAMBDA_EXPR is the current representation of the lambda expression. */
7392 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7394 bool nested = (current_function_decl != NULL_TREE);
7396 push_function_context ();
7398 /* Finish the function call operator
7400 + late_parsing_for_member
7401 + function_definition_after_declarator
7402 + ctor_initializer_opt_and_function_body */
7404 tree fco = lambda_function (lambda_expr);
7408 /* Let the front end know that we are going to be defining this
7410 start_preparsed_function (fco,
7412 SF_PRE_PARSED | SF_INCLASS_INLINE);
7414 start_lambda_scope (fco);
7415 body = begin_function_body ();
7417 /* 5.1.1.4 of the standard says:
7418 If a lambda-expression does not include a trailing-return-type, it
7419 is as if the trailing-return-type denotes the following type:
7420 * if the compound-statement is of the form
7421 { return attribute-specifier [opt] expression ; }
7422 the type of the returned expression after lvalue-to-rvalue
7423 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7424 (_conv.array_ 4.2), and function-to-pointer conversion
7426 * otherwise, void. */
7428 /* In a lambda that has neither a lambda-return-type-clause
7429 nor a deducible form, errors should be reported for return statements
7430 in the body. Since we used void as the placeholder return type, parsing
7431 the body as usual will give such desired behavior. */
7432 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7433 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7434 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7435 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7438 tree expr = NULL_TREE;
7439 cp_id_kind idk = CP_ID_KIND_NONE;
7441 /* Parse tentatively in case there's more after the initial return
7443 cp_parser_parse_tentatively (parser);
7445 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7446 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7448 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7450 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7451 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7453 if (cp_parser_parse_definitely (parser))
7455 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7457 compound_stmt = begin_compound_stmt (0);
7458 /* Will get error here if type not deduced yet. */
7459 finish_return_stmt (expr);
7460 finish_compound_stmt (compound_stmt);
7468 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7469 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7470 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7471 cp_parser_compound_stmt does not pass it. */
7472 cp_parser_function_body (parser);
7473 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7476 finish_function_body (body);
7477 finish_lambda_scope ();
7479 /* Finish the function and generate code for it if necessary. */
7480 expand_or_defer_fn (finish_function (/*inline*/2));
7484 pop_function_context();
7487 /* Statements [gram.stmt.stmt] */
7489 /* Parse a statement.
7493 expression-statement
7498 declaration-statement
7501 IN_COMPOUND is true when the statement is nested inside a
7502 cp_parser_compound_statement; this matters for certain pragmas.
7504 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7505 is a (possibly labeled) if statement which is not enclosed in braces
7506 and has an else clause. This is used to implement -Wparentheses. */
7509 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7510 bool in_compound, bool *if_p)
7514 location_t statement_location;
7519 /* There is no statement yet. */
7520 statement = NULL_TREE;
7521 /* Peek at the next token. */
7522 token = cp_lexer_peek_token (parser->lexer);
7523 /* Remember the location of the first token in the statement. */
7524 statement_location = token->location;
7525 /* If this is a keyword, then that will often determine what kind of
7526 statement we have. */
7527 if (token->type == CPP_KEYWORD)
7529 enum rid keyword = token->keyword;
7535 /* Looks like a labeled-statement with a case label.
7536 Parse the label, and then use tail recursion to parse
7538 cp_parser_label_for_labeled_statement (parser);
7543 statement = cp_parser_selection_statement (parser, if_p);
7549 statement = cp_parser_iteration_statement (parser);
7556 statement = cp_parser_jump_statement (parser);
7559 /* Objective-C++ exception-handling constructs. */
7562 case RID_AT_FINALLY:
7563 case RID_AT_SYNCHRONIZED:
7565 statement = cp_parser_objc_statement (parser);
7569 statement = cp_parser_try_block (parser);
7573 /* This must be a namespace alias definition. */
7574 cp_parser_declaration_statement (parser);
7578 /* It might be a keyword like `int' that can start a
7579 declaration-statement. */
7583 else if (token->type == CPP_NAME)
7585 /* If the next token is a `:', then we are looking at a
7586 labeled-statement. */
7587 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7588 if (token->type == CPP_COLON)
7590 /* Looks like a labeled-statement with an ordinary label.
7591 Parse the label, and then use tail recursion to parse
7593 cp_parser_label_for_labeled_statement (parser);
7597 /* Anything that starts with a `{' must be a compound-statement. */
7598 else if (token->type == CPP_OPEN_BRACE)
7599 statement = cp_parser_compound_statement (parser, NULL, false);
7600 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7601 a statement all its own. */
7602 else if (token->type == CPP_PRAGMA)
7604 /* Only certain OpenMP pragmas are attached to statements, and thus
7605 are considered statements themselves. All others are not. In
7606 the context of a compound, accept the pragma as a "statement" and
7607 return so that we can check for a close brace. Otherwise we
7608 require a real statement and must go back and read one. */
7610 cp_parser_pragma (parser, pragma_compound);
7611 else if (!cp_parser_pragma (parser, pragma_stmt))
7615 else if (token->type == CPP_EOF)
7617 cp_parser_error (parser, "expected statement");
7621 /* Everything else must be a declaration-statement or an
7622 expression-statement. Try for the declaration-statement
7623 first, unless we are looking at a `;', in which case we know that
7624 we have an expression-statement. */
7627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7629 cp_parser_parse_tentatively (parser);
7630 /* Try to parse the declaration-statement. */
7631 cp_parser_declaration_statement (parser);
7632 /* If that worked, we're done. */
7633 if (cp_parser_parse_definitely (parser))
7636 /* Look for an expression-statement instead. */
7637 statement = cp_parser_expression_statement (parser, in_statement_expr);
7640 /* Set the line number for the statement. */
7641 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7642 SET_EXPR_LOCATION (statement, statement_location);
7645 /* Parse the label for a labeled-statement, i.e.
7648 case constant-expression :
7652 case constant-expression ... constant-expression : statement
7654 When a label is parsed without errors, the label is added to the
7655 parse tree by the finish_* functions, so this function doesn't
7656 have to return the label. */
7659 cp_parser_label_for_labeled_statement (cp_parser* parser)
7662 tree label = NULL_TREE;
7664 /* The next token should be an identifier. */
7665 token = cp_lexer_peek_token (parser->lexer);
7666 if (token->type != CPP_NAME
7667 && token->type != CPP_KEYWORD)
7669 cp_parser_error (parser, "expected labeled-statement");
7673 switch (token->keyword)
7680 /* Consume the `case' token. */
7681 cp_lexer_consume_token (parser->lexer);
7682 /* Parse the constant-expression. */
7683 expr = cp_parser_constant_expression (parser,
7684 /*allow_non_constant_p=*/false,
7687 ellipsis = cp_lexer_peek_token (parser->lexer);
7688 if (ellipsis->type == CPP_ELLIPSIS)
7690 /* Consume the `...' token. */
7691 cp_lexer_consume_token (parser->lexer);
7693 cp_parser_constant_expression (parser,
7694 /*allow_non_constant_p=*/false,
7696 /* We don't need to emit warnings here, as the common code
7697 will do this for us. */
7700 expr_hi = NULL_TREE;
7702 if (parser->in_switch_statement_p)
7703 finish_case_label (token->location, expr, expr_hi);
7705 error_at (token->location,
7706 "case label %qE not within a switch statement",
7712 /* Consume the `default' token. */
7713 cp_lexer_consume_token (parser->lexer);
7715 if (parser->in_switch_statement_p)
7716 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7718 error_at (token->location, "case label not within a switch statement");
7722 /* Anything else must be an ordinary label. */
7723 label = finish_label_stmt (cp_parser_identifier (parser));
7727 /* Require the `:' token. */
7728 cp_parser_require (parser, CPP_COLON, "%<:%>");
7730 /* An ordinary label may optionally be followed by attributes.
7731 However, this is only permitted if the attributes are then
7732 followed by a semicolon. This is because, for backward
7733 compatibility, when parsing
7734 lab: __attribute__ ((unused)) int i;
7735 we want the attribute to attach to "i", not "lab". */
7736 if (label != NULL_TREE
7737 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7741 cp_parser_parse_tentatively (parser);
7742 attrs = cp_parser_attributes_opt (parser);
7743 if (attrs == NULL_TREE
7744 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7745 cp_parser_abort_tentative_parse (parser);
7746 else if (!cp_parser_parse_definitely (parser))
7749 cplus_decl_attributes (&label, attrs, 0);
7753 /* Parse an expression-statement.
7755 expression-statement:
7758 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7759 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7760 indicates whether this expression-statement is part of an
7761 expression statement. */
7764 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7766 tree statement = NULL_TREE;
7767 cp_token *token = cp_lexer_peek_token (parser->lexer);
7769 /* If the next token is a ';', then there is no expression
7771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7772 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7774 /* Give a helpful message for "A<T>::type t;" */
7775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7776 && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7777 && TREE_CODE (statement) == SCOPE_REF)
7778 error_at (token->location, "need %<typename%> before %qE because "
7779 "%qT is a dependent scope",
7780 statement, TREE_OPERAND (statement, 0));
7782 /* Consume the final `;'. */
7783 cp_parser_consume_semicolon_at_end_of_statement (parser);
7785 if (in_statement_expr
7786 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7787 /* This is the final expression statement of a statement
7789 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7791 statement = finish_expr_stmt (statement);
7798 /* Parse a compound-statement.
7801 { statement-seq [opt] }
7806 { label-declaration-seq [opt] statement-seq [opt] }
7808 label-declaration-seq:
7810 label-declaration-seq label-declaration
7812 Returns a tree representing the statement. */
7815 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7820 /* Consume the `{'. */
7821 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7822 return error_mark_node;
7823 /* Begin the compound-statement. */
7824 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7825 /* If the next keyword is `__label__' we have a label declaration. */
7826 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7827 cp_parser_label_declaration (parser);
7828 /* Parse an (optional) statement-seq. */
7829 cp_parser_statement_seq_opt (parser, in_statement_expr);
7830 /* Finish the compound-statement. */
7831 finish_compound_stmt (compound_stmt);
7832 /* Consume the `}'. */
7833 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7835 return compound_stmt;
7838 /* Parse an (optional) statement-seq.
7842 statement-seq [opt] statement */
7845 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7847 /* Scan statements until there aren't any more. */
7850 cp_token *token = cp_lexer_peek_token (parser->lexer);
7852 /* If we're looking at a `}', then we've run out of statements. */
7853 if (token->type == CPP_CLOSE_BRACE
7854 || token->type == CPP_EOF
7855 || token->type == CPP_PRAGMA_EOL)
7858 /* If we are in a compound statement and find 'else' then
7859 something went wrong. */
7860 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7862 if (parser->in_statement & IN_IF_STMT)
7866 token = cp_lexer_consume_token (parser->lexer);
7867 error_at (token->location, "%<else%> without a previous %<if%>");
7871 /* Parse the statement. */
7872 cp_parser_statement (parser, in_statement_expr, true, NULL);
7876 /* Parse a selection-statement.
7878 selection-statement:
7879 if ( condition ) statement
7880 if ( condition ) statement else statement
7881 switch ( condition ) statement
7883 Returns the new IF_STMT or SWITCH_STMT.
7885 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7886 is a (possibly labeled) if statement which is not enclosed in
7887 braces and has an else clause. This is used to implement
7891 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7899 /* Peek at the next token. */
7900 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7902 /* See what kind of keyword it is. */
7903 keyword = token->keyword;
7912 /* Look for the `('. */
7913 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7915 cp_parser_skip_to_end_of_statement (parser);
7916 return error_mark_node;
7919 /* Begin the selection-statement. */
7920 if (keyword == RID_IF)
7921 statement = begin_if_stmt ();
7923 statement = begin_switch_stmt ();
7925 /* Parse the condition. */
7926 condition = cp_parser_condition (parser);
7927 /* Look for the `)'. */
7928 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7929 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7930 /*consume_paren=*/true);
7932 if (keyword == RID_IF)
7935 unsigned char in_statement;
7937 /* Add the condition. */
7938 finish_if_stmt_cond (condition, statement);
7940 /* Parse the then-clause. */
7941 in_statement = parser->in_statement;
7942 parser->in_statement |= IN_IF_STMT;
7943 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7945 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7946 add_stmt (build_empty_stmt (loc));
7947 cp_lexer_consume_token (parser->lexer);
7948 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7949 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7950 "empty body in an %<if%> statement");
7954 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7955 parser->in_statement = in_statement;
7957 finish_then_clause (statement);
7959 /* If the next token is `else', parse the else-clause. */
7960 if (cp_lexer_next_token_is_keyword (parser->lexer,
7963 /* Consume the `else' keyword. */
7964 cp_lexer_consume_token (parser->lexer);
7965 begin_else_clause (statement);
7966 /* Parse the else-clause. */
7967 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7970 loc = cp_lexer_peek_token (parser->lexer)->location;
7972 OPT_Wempty_body, "suggest braces around "
7973 "empty body in an %<else%> statement");
7974 add_stmt (build_empty_stmt (loc));
7975 cp_lexer_consume_token (parser->lexer);
7978 cp_parser_implicitly_scoped_statement (parser, NULL);
7980 finish_else_clause (statement);
7982 /* If we are currently parsing a then-clause, then
7983 IF_P will not be NULL. We set it to true to
7984 indicate that this if statement has an else clause.
7985 This may trigger the Wparentheses warning below
7986 when we get back up to the parent if statement. */
7992 /* This if statement does not have an else clause. If
7993 NESTED_IF is true, then the then-clause is an if
7994 statement which does have an else clause. We warn
7995 about the potential ambiguity. */
7997 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7998 "suggest explicit braces to avoid ambiguous"
8002 /* Now we're all done with the if-statement. */
8003 finish_if_stmt (statement);
8007 bool in_switch_statement_p;
8008 unsigned char in_statement;
8010 /* Add the condition. */
8011 finish_switch_cond (condition, statement);
8013 /* Parse the body of the switch-statement. */
8014 in_switch_statement_p = parser->in_switch_statement_p;
8015 in_statement = parser->in_statement;
8016 parser->in_switch_statement_p = true;
8017 parser->in_statement |= IN_SWITCH_STMT;
8018 cp_parser_implicitly_scoped_statement (parser, NULL);
8019 parser->in_switch_statement_p = in_switch_statement_p;
8020 parser->in_statement = in_statement;
8022 /* Now we're all done with the switch-statement. */
8023 finish_switch_stmt (statement);
8031 cp_parser_error (parser, "expected selection-statement");
8032 return error_mark_node;
8036 /* Parse a condition.
8040 type-specifier-seq declarator = initializer-clause
8041 type-specifier-seq declarator braced-init-list
8046 type-specifier-seq declarator asm-specification [opt]
8047 attributes [opt] = assignment-expression
8049 Returns the expression that should be tested. */
8052 cp_parser_condition (cp_parser* parser)
8054 cp_decl_specifier_seq type_specifiers;
8055 const char *saved_message;
8057 /* Try the declaration first. */
8058 cp_parser_parse_tentatively (parser);
8059 /* New types are not allowed in the type-specifier-seq for a
8061 saved_message = parser->type_definition_forbidden_message;
8062 parser->type_definition_forbidden_message
8063 = "types may not be defined in conditions";
8064 /* Parse the type-specifier-seq. */
8065 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8066 /*is_trailing_return=*/false,
8068 /* Restore the saved message. */
8069 parser->type_definition_forbidden_message = saved_message;
8070 /* If all is well, we might be looking at a declaration. */
8071 if (!cp_parser_error_occurred (parser))
8074 tree asm_specification;
8076 cp_declarator *declarator;
8077 tree initializer = NULL_TREE;
8079 /* Parse the declarator. */
8080 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8081 /*ctor_dtor_or_conv_p=*/NULL,
8082 /*parenthesized_p=*/NULL,
8083 /*member_p=*/false);
8084 /* Parse the attributes. */
8085 attributes = cp_parser_attributes_opt (parser);
8086 /* Parse the asm-specification. */
8087 asm_specification = cp_parser_asm_specification_opt (parser);
8088 /* If the next token is not an `=' or '{', then we might still be
8089 looking at an expression. For example:
8093 looks like a decl-specifier-seq and a declarator -- but then
8094 there is no `=', so this is an expression. */
8095 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8096 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8097 cp_parser_simulate_error (parser);
8099 /* If we did see an `=' or '{', then we are looking at a declaration
8101 if (cp_parser_parse_definitely (parser))
8104 bool non_constant_p;
8105 bool flags = LOOKUP_ONLYCONVERTING;
8107 /* Create the declaration. */
8108 decl = start_decl (declarator, &type_specifiers,
8109 /*initialized_p=*/true,
8110 attributes, /*prefix_attributes=*/NULL_TREE,
8113 /* Parse the initializer. */
8114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8116 initializer = cp_parser_braced_list (parser, &non_constant_p);
8117 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8122 /* Consume the `='. */
8123 cp_parser_require (parser, CPP_EQ, "%<=%>");
8124 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8126 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8127 maybe_warn_cpp0x ("extended initializer lists");
8129 if (!non_constant_p)
8130 initializer = fold_non_dependent_expr (initializer);
8132 /* Process the initializer. */
8133 cp_finish_decl (decl,
8134 initializer, !non_constant_p,
8139 pop_scope (pushed_scope);
8141 return convert_from_reference (decl);
8144 /* If we didn't even get past the declarator successfully, we are
8145 definitely not looking at a declaration. */
8147 cp_parser_abort_tentative_parse (parser);
8149 /* Otherwise, we are looking at an expression. */
8150 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8153 /* Parse an iteration-statement.
8155 iteration-statement:
8156 while ( condition ) statement
8157 do statement while ( expression ) ;
8158 for ( for-init-statement condition [opt] ; expression [opt] )
8161 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8164 cp_parser_iteration_statement (cp_parser* parser)
8169 unsigned char in_statement;
8171 /* Peek at the next token. */
8172 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8174 return error_mark_node;
8176 /* Remember whether or not we are already within an iteration
8178 in_statement = parser->in_statement;
8180 /* See what kind of keyword it is. */
8181 keyword = token->keyword;
8188 /* Begin the while-statement. */
8189 statement = begin_while_stmt ();
8190 /* Look for the `('. */
8191 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8192 /* Parse the condition. */
8193 condition = cp_parser_condition (parser);
8194 finish_while_stmt_cond (condition, statement);
8195 /* Look for the `)'. */
8196 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8197 /* Parse the dependent statement. */
8198 parser->in_statement = IN_ITERATION_STMT;
8199 cp_parser_already_scoped_statement (parser);
8200 parser->in_statement = in_statement;
8201 /* We're done with the while-statement. */
8202 finish_while_stmt (statement);
8210 /* Begin the do-statement. */
8211 statement = begin_do_stmt ();
8212 /* Parse the body of the do-statement. */
8213 parser->in_statement = IN_ITERATION_STMT;
8214 cp_parser_implicitly_scoped_statement (parser, NULL);
8215 parser->in_statement = in_statement;
8216 finish_do_body (statement);
8217 /* Look for the `while' keyword. */
8218 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8219 /* Look for the `('. */
8220 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8221 /* Parse the expression. */
8222 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8223 /* We're done with the do-statement. */
8224 finish_do_stmt (expression, statement);
8225 /* Look for the `)'. */
8226 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8227 /* Look for the `;'. */
8228 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8234 tree condition = NULL_TREE;
8235 tree expression = NULL_TREE;
8237 /* Begin the for-statement. */
8238 statement = begin_for_stmt ();
8239 /* Look for the `('. */
8240 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8241 /* Parse the initialization. */
8242 cp_parser_for_init_statement (parser);
8243 finish_for_init_stmt (statement);
8245 /* If there's a condition, process it. */
8246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8247 condition = cp_parser_condition (parser);
8248 finish_for_cond (condition, statement);
8249 /* Look for the `;'. */
8250 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8252 /* If there's an expression, process it. */
8253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8254 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8255 finish_for_expr (expression, statement);
8256 /* Look for the `)'. */
8257 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8259 /* Parse the body of the for-statement. */
8260 parser->in_statement = IN_ITERATION_STMT;
8261 cp_parser_already_scoped_statement (parser);
8262 parser->in_statement = in_statement;
8264 /* We're done with the for-statement. */
8265 finish_for_stmt (statement);
8270 cp_parser_error (parser, "expected iteration-statement");
8271 statement = error_mark_node;
8278 /* Parse a for-init-statement.
8281 expression-statement
8282 simple-declaration */
8285 cp_parser_for_init_statement (cp_parser* parser)
8287 /* If the next token is a `;', then we have an empty
8288 expression-statement. Grammatically, this is also a
8289 simple-declaration, but an invalid one, because it does not
8290 declare anything. Therefore, if we did not handle this case
8291 specially, we would issue an error message about an invalid
8293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8295 /* We're going to speculatively look for a declaration, falling back
8296 to an expression, if necessary. */
8297 cp_parser_parse_tentatively (parser);
8298 /* Parse the declaration. */
8299 cp_parser_simple_declaration (parser,
8300 /*function_definition_allowed_p=*/false);
8301 /* If the tentative parse failed, then we shall need to look for an
8302 expression-statement. */
8303 if (cp_parser_parse_definitely (parser))
8307 cp_parser_expression_statement (parser, false);
8310 /* Parse a jump-statement.
8315 return expression [opt] ;
8316 return braced-init-list ;
8324 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8327 cp_parser_jump_statement (cp_parser* parser)
8329 tree statement = error_mark_node;
8332 unsigned char in_statement;
8334 /* Peek at the next token. */
8335 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8337 return error_mark_node;
8339 /* See what kind of keyword it is. */
8340 keyword = token->keyword;
8344 in_statement = parser->in_statement & ~IN_IF_STMT;
8345 switch (in_statement)
8348 error_at (token->location, "break statement not within loop or switch");
8351 gcc_assert ((in_statement & IN_SWITCH_STMT)
8352 || in_statement == IN_ITERATION_STMT);
8353 statement = finish_break_stmt ();
8356 error_at (token->location, "invalid exit from OpenMP structured block");
8359 error_at (token->location, "break statement used with OpenMP for loop");
8362 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8366 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8369 error_at (token->location, "continue statement not within a loop");
8371 case IN_ITERATION_STMT:
8373 statement = finish_continue_stmt ();
8376 error_at (token->location, "invalid exit from OpenMP structured block");
8381 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8387 bool expr_non_constant_p;
8389 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8391 maybe_warn_cpp0x ("extended initializer lists");
8392 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8394 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8395 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8397 /* If the next token is a `;', then there is no
8400 /* Build the return-statement. */
8401 statement = finish_return_stmt (expr);
8402 /* Look for the final `;'. */
8403 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8408 /* Create the goto-statement. */
8409 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8411 /* Issue a warning about this use of a GNU extension. */
8412 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8413 /* Consume the '*' token. */
8414 cp_lexer_consume_token (parser->lexer);
8415 /* Parse the dependent expression. */
8416 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8419 finish_goto_stmt (cp_parser_identifier (parser));
8420 /* Look for the final `;'. */
8421 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8425 cp_parser_error (parser, "expected jump-statement");
8432 /* Parse a declaration-statement.
8434 declaration-statement:
8435 block-declaration */
8438 cp_parser_declaration_statement (cp_parser* parser)
8442 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8443 p = obstack_alloc (&declarator_obstack, 0);
8445 /* Parse the block-declaration. */
8446 cp_parser_block_declaration (parser, /*statement_p=*/true);
8448 /* Free any declarators allocated. */
8449 obstack_free (&declarator_obstack, p);
8451 /* Finish off the statement. */
8455 /* Some dependent statements (like `if (cond) statement'), are
8456 implicitly in their own scope. In other words, if the statement is
8457 a single statement (as opposed to a compound-statement), it is
8458 none-the-less treated as if it were enclosed in braces. Any
8459 declarations appearing in the dependent statement are out of scope
8460 after control passes that point. This function parses a statement,
8461 but ensures that is in its own scope, even if it is not a
8464 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8465 is a (possibly labeled) if statement which is not enclosed in
8466 braces and has an else clause. This is used to implement
8469 Returns the new statement. */
8472 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8479 /* Mark if () ; with a special NOP_EXPR. */
8480 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8482 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8483 cp_lexer_consume_token (parser->lexer);
8484 statement = add_stmt (build_empty_stmt (loc));
8486 /* if a compound is opened, we simply parse the statement directly. */
8487 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8488 statement = cp_parser_compound_statement (parser, NULL, false);
8489 /* If the token is not a `{', then we must take special action. */
8492 /* Create a compound-statement. */
8493 statement = begin_compound_stmt (0);
8494 /* Parse the dependent-statement. */
8495 cp_parser_statement (parser, NULL_TREE, false, if_p);
8496 /* Finish the dummy compound-statement. */
8497 finish_compound_stmt (statement);
8500 /* Return the statement. */
8504 /* For some dependent statements (like `while (cond) statement'), we
8505 have already created a scope. Therefore, even if the dependent
8506 statement is a compound-statement, we do not want to create another
8510 cp_parser_already_scoped_statement (cp_parser* parser)
8512 /* If the token is a `{', then we must take special action. */
8513 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8514 cp_parser_statement (parser, NULL_TREE, false, NULL);
8517 /* Avoid calling cp_parser_compound_statement, so that we
8518 don't create a new scope. Do everything else by hand. */
8519 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8520 /* If the next keyword is `__label__' we have a label declaration. */
8521 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8522 cp_parser_label_declaration (parser);
8523 /* Parse an (optional) statement-seq. */
8524 cp_parser_statement_seq_opt (parser, NULL_TREE);
8525 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8529 /* Declarations [gram.dcl.dcl] */
8531 /* Parse an optional declaration-sequence.
8535 declaration-seq declaration */
8538 cp_parser_declaration_seq_opt (cp_parser* parser)
8544 token = cp_lexer_peek_token (parser->lexer);
8546 if (token->type == CPP_CLOSE_BRACE
8547 || token->type == CPP_EOF
8548 || token->type == CPP_PRAGMA_EOL)
8551 if (token->type == CPP_SEMICOLON)
8553 /* A declaration consisting of a single semicolon is
8554 invalid. Allow it unless we're being pedantic. */
8555 cp_lexer_consume_token (parser->lexer);
8556 if (!in_system_header)
8557 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8561 /* If we're entering or exiting a region that's implicitly
8562 extern "C", modify the lang context appropriately. */
8563 if (!parser->implicit_extern_c && token->implicit_extern_c)
8565 push_lang_context (lang_name_c);
8566 parser->implicit_extern_c = true;
8568 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8570 pop_lang_context ();
8571 parser->implicit_extern_c = false;
8574 if (token->type == CPP_PRAGMA)
8576 /* A top-level declaration can consist solely of a #pragma.
8577 A nested declaration cannot, so this is done here and not
8578 in cp_parser_declaration. (A #pragma at block scope is
8579 handled in cp_parser_statement.) */
8580 cp_parser_pragma (parser, pragma_external);
8584 /* Parse the declaration itself. */
8585 cp_parser_declaration (parser);
8589 /* Parse a declaration.
8594 template-declaration
8595 explicit-instantiation
8596 explicit-specialization
8597 linkage-specification
8598 namespace-definition
8603 __extension__ declaration */
8606 cp_parser_declaration (cp_parser* parser)
8613 /* Check for the `__extension__' keyword. */
8614 if (cp_parser_extension_opt (parser, &saved_pedantic))
8616 /* Parse the qualified declaration. */
8617 cp_parser_declaration (parser);
8618 /* Restore the PEDANTIC flag. */
8619 pedantic = saved_pedantic;
8624 /* Try to figure out what kind of declaration is present. */
8625 token1 = *cp_lexer_peek_token (parser->lexer);
8627 if (token1.type != CPP_EOF)
8628 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8631 token2.type = CPP_EOF;
8632 token2.keyword = RID_MAX;
8635 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8636 p = obstack_alloc (&declarator_obstack, 0);
8638 /* If the next token is `extern' and the following token is a string
8639 literal, then we have a linkage specification. */
8640 if (token1.keyword == RID_EXTERN
8641 && cp_parser_is_string_literal (&token2))
8642 cp_parser_linkage_specification (parser);
8643 /* If the next token is `template', then we have either a template
8644 declaration, an explicit instantiation, or an explicit
8646 else if (token1.keyword == RID_TEMPLATE)
8648 /* `template <>' indicates a template specialization. */
8649 if (token2.type == CPP_LESS
8650 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8651 cp_parser_explicit_specialization (parser);
8652 /* `template <' indicates a template declaration. */
8653 else if (token2.type == CPP_LESS)
8654 cp_parser_template_declaration (parser, /*member_p=*/false);
8655 /* Anything else must be an explicit instantiation. */
8657 cp_parser_explicit_instantiation (parser);
8659 /* If the next token is `export', then we have a template
8661 else if (token1.keyword == RID_EXPORT)
8662 cp_parser_template_declaration (parser, /*member_p=*/false);
8663 /* If the next token is `extern', 'static' or 'inline' and the one
8664 after that is `template', we have a GNU extended explicit
8665 instantiation directive. */
8666 else if (cp_parser_allow_gnu_extensions_p (parser)
8667 && (token1.keyword == RID_EXTERN
8668 || token1.keyword == RID_STATIC
8669 || token1.keyword == RID_INLINE)
8670 && token2.keyword == RID_TEMPLATE)
8671 cp_parser_explicit_instantiation (parser);
8672 /* If the next token is `namespace', check for a named or unnamed
8673 namespace definition. */
8674 else if (token1.keyword == RID_NAMESPACE
8675 && (/* A named namespace definition. */
8676 (token2.type == CPP_NAME
8677 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8679 /* An unnamed namespace definition. */
8680 || token2.type == CPP_OPEN_BRACE
8681 || token2.keyword == RID_ATTRIBUTE))
8682 cp_parser_namespace_definition (parser);
8683 /* An inline (associated) namespace definition. */
8684 else if (token1.keyword == RID_INLINE
8685 && token2.keyword == RID_NAMESPACE)
8686 cp_parser_namespace_definition (parser);
8687 /* Objective-C++ declaration/definition. */
8688 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8689 cp_parser_objc_declaration (parser);
8690 /* We must have either a block declaration or a function
8693 /* Try to parse a block-declaration, or a function-definition. */
8694 cp_parser_block_declaration (parser, /*statement_p=*/false);
8696 /* Free any declarators allocated. */
8697 obstack_free (&declarator_obstack, p);
8700 /* Parse a block-declaration.
8705 namespace-alias-definition
8712 __extension__ block-declaration
8717 static_assert-declaration
8719 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8720 part of a declaration-statement. */
8723 cp_parser_block_declaration (cp_parser *parser,
8729 /* Check for the `__extension__' keyword. */
8730 if (cp_parser_extension_opt (parser, &saved_pedantic))
8732 /* Parse the qualified declaration. */
8733 cp_parser_block_declaration (parser, statement_p);
8734 /* Restore the PEDANTIC flag. */
8735 pedantic = saved_pedantic;
8740 /* Peek at the next token to figure out which kind of declaration is
8742 token1 = cp_lexer_peek_token (parser->lexer);
8744 /* If the next keyword is `asm', we have an asm-definition. */
8745 if (token1->keyword == RID_ASM)
8748 cp_parser_commit_to_tentative_parse (parser);
8749 cp_parser_asm_definition (parser);
8751 /* If the next keyword is `namespace', we have a
8752 namespace-alias-definition. */
8753 else if (token1->keyword == RID_NAMESPACE)
8754 cp_parser_namespace_alias_definition (parser);
8755 /* If the next keyword is `using', we have either a
8756 using-declaration or a using-directive. */
8757 else if (token1->keyword == RID_USING)
8762 cp_parser_commit_to_tentative_parse (parser);
8763 /* If the token after `using' is `namespace', then we have a
8765 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8766 if (token2->keyword == RID_NAMESPACE)
8767 cp_parser_using_directive (parser);
8768 /* Otherwise, it's a using-declaration. */
8770 cp_parser_using_declaration (parser,
8771 /*access_declaration_p=*/false);
8773 /* If the next keyword is `__label__' we have a misplaced label
8775 else if (token1->keyword == RID_LABEL)
8777 cp_lexer_consume_token (parser->lexer);
8778 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8779 cp_parser_skip_to_end_of_statement (parser);
8780 /* If the next token is now a `;', consume it. */
8781 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8782 cp_lexer_consume_token (parser->lexer);
8784 /* If the next token is `static_assert' we have a static assertion. */
8785 else if (token1->keyword == RID_STATIC_ASSERT)
8786 cp_parser_static_assert (parser, /*member_p=*/false);
8787 /* Anything else must be a simple-declaration. */
8789 cp_parser_simple_declaration (parser, !statement_p);
8792 /* Parse a simple-declaration.
8795 decl-specifier-seq [opt] init-declarator-list [opt] ;
8797 init-declarator-list:
8799 init-declarator-list , init-declarator
8801 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8802 function-definition as a simple-declaration. */
8805 cp_parser_simple_declaration (cp_parser* parser,
8806 bool function_definition_allowed_p)
8808 cp_decl_specifier_seq decl_specifiers;
8809 int declares_class_or_enum;
8810 bool saw_declarator;
8812 /* Defer access checks until we know what is being declared; the
8813 checks for names appearing in the decl-specifier-seq should be
8814 done as if we were in the scope of the thing being declared. */
8815 push_deferring_access_checks (dk_deferred);
8817 /* Parse the decl-specifier-seq. We have to keep track of whether
8818 or not the decl-specifier-seq declares a named class or
8819 enumeration type, since that is the only case in which the
8820 init-declarator-list is allowed to be empty.
8824 In a simple-declaration, the optional init-declarator-list can be
8825 omitted only when declaring a class or enumeration, that is when
8826 the decl-specifier-seq contains either a class-specifier, an
8827 elaborated-type-specifier, or an enum-specifier. */
8828 cp_parser_decl_specifier_seq (parser,
8829 CP_PARSER_FLAGS_OPTIONAL,
8831 &declares_class_or_enum);
8832 /* We no longer need to defer access checks. */
8833 stop_deferring_access_checks ();
8835 /* In a block scope, a valid declaration must always have a
8836 decl-specifier-seq. By not trying to parse declarators, we can
8837 resolve the declaration/expression ambiguity more quickly. */
8838 if (!function_definition_allowed_p
8839 && !decl_specifiers.any_specifiers_p)
8841 cp_parser_error (parser, "expected declaration");
8845 /* If the next two tokens are both identifiers, the code is
8846 erroneous. The usual cause of this situation is code like:
8850 where "T" should name a type -- but does not. */
8851 if (!decl_specifiers.any_type_specifiers_p
8852 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8854 /* If parsing tentatively, we should commit; we really are
8855 looking at a declaration. */
8856 cp_parser_commit_to_tentative_parse (parser);
8861 /* If we have seen at least one decl-specifier, and the next token
8862 is not a parenthesis, then we must be looking at a declaration.
8863 (After "int (" we might be looking at a functional cast.) */
8864 if (decl_specifiers.any_specifiers_p
8865 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8866 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8867 && !cp_parser_error_occurred (parser))
8868 cp_parser_commit_to_tentative_parse (parser);
8870 /* Keep going until we hit the `;' at the end of the simple
8872 saw_declarator = false;
8873 while (cp_lexer_next_token_is_not (parser->lexer,
8877 bool function_definition_p;
8882 /* If we are processing next declarator, coma is expected */
8883 token = cp_lexer_peek_token (parser->lexer);
8884 gcc_assert (token->type == CPP_COMMA);
8885 cp_lexer_consume_token (parser->lexer);
8888 saw_declarator = true;
8890 /* Parse the init-declarator. */
8891 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8893 function_definition_allowed_p,
8895 declares_class_or_enum,
8896 &function_definition_p);
8897 /* If an error occurred while parsing tentatively, exit quickly.
8898 (That usually happens when in the body of a function; each
8899 statement is treated as a declaration-statement until proven
8901 if (cp_parser_error_occurred (parser))
8903 /* Handle function definitions specially. */
8904 if (function_definition_p)
8906 /* If the next token is a `,', then we are probably
8907 processing something like:
8911 which is erroneous. */
8912 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8914 cp_token *token = cp_lexer_peek_token (parser->lexer);
8915 error_at (token->location,
8917 " declarations and function-definitions is forbidden");
8919 /* Otherwise, we're done with the list of declarators. */
8922 pop_deferring_access_checks ();
8926 /* The next token should be either a `,' or a `;'. */
8927 token = cp_lexer_peek_token (parser->lexer);
8928 /* If it's a `,', there are more declarators to come. */
8929 if (token->type == CPP_COMMA)
8930 /* will be consumed next time around */;
8931 /* If it's a `;', we are done. */
8932 else if (token->type == CPP_SEMICOLON)
8934 /* Anything else is an error. */
8937 /* If we have already issued an error message we don't need
8938 to issue another one. */
8939 if (decl != error_mark_node
8940 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8941 cp_parser_error (parser, "expected %<,%> or %<;%>");
8942 /* Skip tokens until we reach the end of the statement. */
8943 cp_parser_skip_to_end_of_statement (parser);
8944 /* If the next token is now a `;', consume it. */
8945 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8946 cp_lexer_consume_token (parser->lexer);
8949 /* After the first time around, a function-definition is not
8950 allowed -- even if it was OK at first. For example:
8955 function_definition_allowed_p = false;
8958 /* Issue an error message if no declarators are present, and the
8959 decl-specifier-seq does not itself declare a class or
8961 if (!saw_declarator)
8963 if (cp_parser_declares_only_class_p (parser))
8964 shadow_tag (&decl_specifiers);
8965 /* Perform any deferred access checks. */
8966 perform_deferred_access_checks ();
8969 /* Consume the `;'. */
8970 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8973 pop_deferring_access_checks ();
8976 /* Parse a decl-specifier-seq.
8979 decl-specifier-seq [opt] decl-specifier
8982 storage-class-specifier
8993 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8995 The parser flags FLAGS is used to control type-specifier parsing.
8997 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9000 1: one of the decl-specifiers is an elaborated-type-specifier
9001 (i.e., a type declaration)
9002 2: one of the decl-specifiers is an enum-specifier or a
9003 class-specifier (i.e., a type definition)
9008 cp_parser_decl_specifier_seq (cp_parser* parser,
9009 cp_parser_flags flags,
9010 cp_decl_specifier_seq *decl_specs,
9011 int* declares_class_or_enum)
9013 bool constructor_possible_p = !parser->in_declarator_p;
9014 cp_token *start_token = NULL;
9016 /* Clear DECL_SPECS. */
9017 clear_decl_specs (decl_specs);
9019 /* Assume no class or enumeration type is declared. */
9020 *declares_class_or_enum = 0;
9022 /* Keep reading specifiers until there are no more to read. */
9026 bool found_decl_spec;
9029 /* Peek at the next token. */
9030 token = cp_lexer_peek_token (parser->lexer);
9032 /* Save the first token of the decl spec list for error
9035 start_token = token;
9036 /* Handle attributes. */
9037 if (token->keyword == RID_ATTRIBUTE)
9039 /* Parse the attributes. */
9040 decl_specs->attributes
9041 = chainon (decl_specs->attributes,
9042 cp_parser_attributes_opt (parser));
9045 /* Assume we will find a decl-specifier keyword. */
9046 found_decl_spec = true;
9047 /* If the next token is an appropriate keyword, we can simply
9048 add it to the list. */
9049 switch (token->keyword)
9055 if (!at_class_scope_p ())
9057 error_at (token->location, "%<friend%> used outside of class");
9058 cp_lexer_purge_token (parser->lexer);
9062 ++decl_specs->specs[(int) ds_friend];
9063 /* Consume the token. */
9064 cp_lexer_consume_token (parser->lexer);
9069 ++decl_specs->specs[(int) ds_constexpr];
9070 cp_lexer_consume_token (parser->lexer);
9073 /* function-specifier:
9080 cp_parser_function_specifier_opt (parser, decl_specs);
9086 ++decl_specs->specs[(int) ds_typedef];
9087 /* Consume the token. */
9088 cp_lexer_consume_token (parser->lexer);
9089 /* A constructor declarator cannot appear in a typedef. */
9090 constructor_possible_p = false;
9091 /* The "typedef" keyword can only occur in a declaration; we
9092 may as well commit at this point. */
9093 cp_parser_commit_to_tentative_parse (parser);
9095 if (decl_specs->storage_class != sc_none)
9096 decl_specs->conflicting_specifiers_p = true;
9099 /* storage-class-specifier:
9109 if (cxx_dialect == cxx98)
9111 /* Consume the token. */
9112 cp_lexer_consume_token (parser->lexer);
9114 /* Complain about `auto' as a storage specifier, if
9115 we're complaining about C++0x compatibility. */
9116 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9117 " will change meaning in C++0x; please remove it");
9119 /* Set the storage class anyway. */
9120 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9124 /* C++0x auto type-specifier. */
9125 found_decl_spec = false;
9132 /* Consume the token. */
9133 cp_lexer_consume_token (parser->lexer);
9134 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9138 /* Consume the token. */
9139 cp_lexer_consume_token (parser->lexer);
9140 ++decl_specs->specs[(int) ds_thread];
9144 /* We did not yet find a decl-specifier yet. */
9145 found_decl_spec = false;
9149 /* Constructors are a special case. The `S' in `S()' is not a
9150 decl-specifier; it is the beginning of the declarator. */
9153 && constructor_possible_p
9154 && (cp_parser_constructor_declarator_p
9155 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9157 /* If we don't have a DECL_SPEC yet, then we must be looking at
9158 a type-specifier. */
9159 if (!found_decl_spec && !constructor_p)
9161 int decl_spec_declares_class_or_enum;
9162 bool is_cv_qualifier;
9166 = cp_parser_type_specifier (parser, flags,
9168 /*is_declaration=*/true,
9169 &decl_spec_declares_class_or_enum,
9171 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9173 /* If this type-specifier referenced a user-defined type
9174 (a typedef, class-name, etc.), then we can't allow any
9175 more such type-specifiers henceforth.
9179 The longest sequence of decl-specifiers that could
9180 possibly be a type name is taken as the
9181 decl-specifier-seq of a declaration. The sequence shall
9182 be self-consistent as described below.
9186 As a general rule, at most one type-specifier is allowed
9187 in the complete decl-specifier-seq of a declaration. The
9188 only exceptions are the following:
9190 -- const or volatile can be combined with any other
9193 -- signed or unsigned can be combined with char, long,
9201 void g (const int Pc);
9203 Here, Pc is *not* part of the decl-specifier seq; it's
9204 the declarator. Therefore, once we see a type-specifier
9205 (other than a cv-qualifier), we forbid any additional
9206 user-defined types. We *do* still allow things like `int
9207 int' to be considered a decl-specifier-seq, and issue the
9208 error message later. */
9209 if (type_spec && !is_cv_qualifier)
9210 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9211 /* A constructor declarator cannot follow a type-specifier. */
9214 constructor_possible_p = false;
9215 found_decl_spec = true;
9216 if (!is_cv_qualifier)
9217 decl_specs->any_type_specifiers_p = true;
9221 /* If we still do not have a DECL_SPEC, then there are no more
9223 if (!found_decl_spec)
9226 decl_specs->any_specifiers_p = true;
9227 /* After we see one decl-specifier, further decl-specifiers are
9229 flags |= CP_PARSER_FLAGS_OPTIONAL;
9232 cp_parser_check_decl_spec (decl_specs, start_token->location);
9234 /* Don't allow a friend specifier with a class definition. */
9235 if (decl_specs->specs[(int) ds_friend] != 0
9236 && (*declares_class_or_enum & 2))
9237 error_at (start_token->location,
9238 "class definition may not be declared a friend");
9241 /* Parse an (optional) storage-class-specifier.
9243 storage-class-specifier:
9252 storage-class-specifier:
9255 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9258 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9260 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9263 if (cxx_dialect != cxx98)
9265 /* Fall through for C++98. */
9272 /* Consume the token. */
9273 return cp_lexer_consume_token (parser->lexer)->u.value;
9280 /* Parse an (optional) function-specifier.
9287 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9288 Updates DECL_SPECS, if it is non-NULL. */
9291 cp_parser_function_specifier_opt (cp_parser* parser,
9292 cp_decl_specifier_seq *decl_specs)
9294 cp_token *token = cp_lexer_peek_token (parser->lexer);
9295 switch (token->keyword)
9299 ++decl_specs->specs[(int) ds_inline];
9303 /* 14.5.2.3 [temp.mem]
9305 A member function template shall not be virtual. */
9306 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9307 error_at (token->location, "templates may not be %<virtual%>");
9308 else if (decl_specs)
9309 ++decl_specs->specs[(int) ds_virtual];
9314 ++decl_specs->specs[(int) ds_explicit];
9321 /* Consume the token. */
9322 return cp_lexer_consume_token (parser->lexer)->u.value;
9325 /* Parse a linkage-specification.
9327 linkage-specification:
9328 extern string-literal { declaration-seq [opt] }
9329 extern string-literal declaration */
9332 cp_parser_linkage_specification (cp_parser* parser)
9336 /* Look for the `extern' keyword. */
9337 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9339 /* Look for the string-literal. */
9340 linkage = cp_parser_string_literal (parser, false, false);
9342 /* Transform the literal into an identifier. If the literal is a
9343 wide-character string, or contains embedded NULs, then we can't
9344 handle it as the user wants. */
9345 if (strlen (TREE_STRING_POINTER (linkage))
9346 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9348 cp_parser_error (parser, "invalid linkage-specification");
9349 /* Assume C++ linkage. */
9350 linkage = lang_name_cplusplus;
9353 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9355 /* We're now using the new linkage. */
9356 push_lang_context (linkage);
9358 /* If the next token is a `{', then we're using the first
9360 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9362 /* Consume the `{' token. */
9363 cp_lexer_consume_token (parser->lexer);
9364 /* Parse the declarations. */
9365 cp_parser_declaration_seq_opt (parser);
9366 /* Look for the closing `}'. */
9367 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9369 /* Otherwise, there's just one declaration. */
9372 bool saved_in_unbraced_linkage_specification_p;
9374 saved_in_unbraced_linkage_specification_p
9375 = parser->in_unbraced_linkage_specification_p;
9376 parser->in_unbraced_linkage_specification_p = true;
9377 cp_parser_declaration (parser);
9378 parser->in_unbraced_linkage_specification_p
9379 = saved_in_unbraced_linkage_specification_p;
9382 /* We're done with the linkage-specification. */
9383 pop_lang_context ();
9386 /* Parse a static_assert-declaration.
9388 static_assert-declaration:
9389 static_assert ( constant-expression , string-literal ) ;
9391 If MEMBER_P, this static_assert is a class member. */
9394 cp_parser_static_assert(cp_parser *parser, bool member_p)
9399 location_t saved_loc;
9401 /* Peek at the `static_assert' token so we can keep track of exactly
9402 where the static assertion started. */
9403 token = cp_lexer_peek_token (parser->lexer);
9404 saved_loc = token->location;
9406 /* Look for the `static_assert' keyword. */
9407 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9408 "%<static_assert%>"))
9411 /* We know we are in a static assertion; commit to any tentative
9413 if (cp_parser_parsing_tentatively (parser))
9414 cp_parser_commit_to_tentative_parse (parser);
9416 /* Parse the `(' starting the static assertion condition. */
9417 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9419 /* Parse the constant-expression. */
9421 cp_parser_constant_expression (parser,
9422 /*allow_non_constant_p=*/false,
9423 /*non_constant_p=*/NULL);
9425 /* Parse the separating `,'. */
9426 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9428 /* Parse the string-literal message. */
9429 message = cp_parser_string_literal (parser,
9430 /*translate=*/false,
9433 /* A `)' completes the static assertion. */
9434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9435 cp_parser_skip_to_closing_parenthesis (parser,
9436 /*recovering=*/true,
9438 /*consume_paren=*/true);
9440 /* A semicolon terminates the declaration. */
9441 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9443 /* Complete the static assertion, which may mean either processing
9444 the static assert now or saving it for template instantiation. */
9445 finish_static_assert (condition, message, saved_loc, member_p);
9448 /* Parse a `decltype' type. Returns the type.
9450 simple-type-specifier:
9451 decltype ( expression ) */
9454 cp_parser_decltype (cp_parser *parser)
9457 bool id_expression_or_member_access_p = false;
9458 const char *saved_message;
9459 bool saved_integral_constant_expression_p;
9460 bool saved_non_integral_constant_expression_p;
9461 cp_token *id_expr_start_token;
9463 /* Look for the `decltype' token. */
9464 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9465 return error_mark_node;
9467 /* Types cannot be defined in a `decltype' expression. Save away the
9469 saved_message = parser->type_definition_forbidden_message;
9471 /* And create the new one. */
9472 parser->type_definition_forbidden_message
9473 = "types may not be defined in %<decltype%> expressions";
9475 /* The restrictions on constant-expressions do not apply inside
9476 decltype expressions. */
9477 saved_integral_constant_expression_p
9478 = parser->integral_constant_expression_p;
9479 saved_non_integral_constant_expression_p
9480 = parser->non_integral_constant_expression_p;
9481 parser->integral_constant_expression_p = false;
9483 /* Do not actually evaluate the expression. */
9484 ++cp_unevaluated_operand;
9486 /* Do not warn about problems with the expression. */
9487 ++c_inhibit_evaluation_warnings;
9489 /* Parse the opening `('. */
9490 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9491 return error_mark_node;
9493 /* First, try parsing an id-expression. */
9494 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9495 cp_parser_parse_tentatively (parser);
9496 expr = cp_parser_id_expression (parser,
9497 /*template_keyword_p=*/false,
9498 /*check_dependency_p=*/true,
9499 /*template_p=*/NULL,
9500 /*declarator_p=*/false,
9501 /*optional_p=*/false);
9503 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9505 bool non_integral_constant_expression_p = false;
9506 tree id_expression = expr;
9508 const char *error_msg;
9510 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9511 /* Lookup the name we got back from the id-expression. */
9512 expr = cp_parser_lookup_name (parser, expr,
9514 /*is_template=*/false,
9515 /*is_namespace=*/false,
9516 /*check_dependency=*/true,
9517 /*ambiguous_decls=*/NULL,
9518 id_expr_start_token->location);
9521 && expr != error_mark_node
9522 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9523 && TREE_CODE (expr) != TYPE_DECL
9524 && (TREE_CODE (expr) != BIT_NOT_EXPR
9525 || !TYPE_P (TREE_OPERAND (expr, 0)))
9526 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9528 /* Complete lookup of the id-expression. */
9529 expr = (finish_id_expression
9530 (id_expression, expr, parser->scope, &idk,
9531 /*integral_constant_expression_p=*/false,
9532 /*allow_non_integral_constant_expression_p=*/true,
9533 &non_integral_constant_expression_p,
9534 /*template_p=*/false,
9536 /*address_p=*/false,
9537 /*template_arg_p=*/false,
9539 id_expr_start_token->location));
9541 if (expr == error_mark_node)
9542 /* We found an id-expression, but it was something that we
9543 should not have found. This is an error, not something
9544 we can recover from, so note that we found an
9545 id-expression and we'll recover as gracefully as
9547 id_expression_or_member_access_p = true;
9551 && expr != error_mark_node
9552 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9553 /* We have an id-expression. */
9554 id_expression_or_member_access_p = true;
9557 if (!id_expression_or_member_access_p)
9559 /* Abort the id-expression parse. */
9560 cp_parser_abort_tentative_parse (parser);
9562 /* Parsing tentatively, again. */
9563 cp_parser_parse_tentatively (parser);
9565 /* Parse a class member access. */
9566 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9568 /*member_access_only_p=*/true, NULL);
9571 && expr != error_mark_node
9572 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9573 /* We have an id-expression. */
9574 id_expression_or_member_access_p = true;
9577 if (id_expression_or_member_access_p)
9578 /* We have parsed the complete id-expression or member access. */
9579 cp_parser_parse_definitely (parser);
9582 bool saved_greater_than_is_operator_p;
9584 /* Abort our attempt to parse an id-expression or member access
9586 cp_parser_abort_tentative_parse (parser);
9588 /* Within a parenthesized expression, a `>' token is always
9589 the greater-than operator. */
9590 saved_greater_than_is_operator_p
9591 = parser->greater_than_is_operator_p;
9592 parser->greater_than_is_operator_p = true;
9594 /* Parse a full expression. */
9595 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9597 /* The `>' token might be the end of a template-id or
9598 template-parameter-list now. */
9599 parser->greater_than_is_operator_p
9600 = saved_greater_than_is_operator_p;
9603 /* Go back to evaluating expressions. */
9604 --cp_unevaluated_operand;
9605 --c_inhibit_evaluation_warnings;
9607 /* Restore the old message and the integral constant expression
9609 parser->type_definition_forbidden_message = saved_message;
9610 parser->integral_constant_expression_p
9611 = saved_integral_constant_expression_p;
9612 parser->non_integral_constant_expression_p
9613 = saved_non_integral_constant_expression_p;
9615 if (expr == error_mark_node)
9617 /* Skip everything up to the closing `)'. */
9618 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9619 /*consume_paren=*/true);
9620 return error_mark_node;
9623 /* Parse to the closing `)'. */
9624 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9626 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9627 /*consume_paren=*/true);
9628 return error_mark_node;
9631 return finish_decltype_type (expr, id_expression_or_member_access_p);
9634 /* Special member functions [gram.special] */
9636 /* Parse a conversion-function-id.
9638 conversion-function-id:
9639 operator conversion-type-id
9641 Returns an IDENTIFIER_NODE representing the operator. */
9644 cp_parser_conversion_function_id (cp_parser* parser)
9648 tree saved_qualifying_scope;
9649 tree saved_object_scope;
9650 tree pushed_scope = NULL_TREE;
9652 /* Look for the `operator' token. */
9653 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9654 return error_mark_node;
9655 /* When we parse the conversion-type-id, the current scope will be
9656 reset. However, we need that information in able to look up the
9657 conversion function later, so we save it here. */
9658 saved_scope = parser->scope;
9659 saved_qualifying_scope = parser->qualifying_scope;
9660 saved_object_scope = parser->object_scope;
9661 /* We must enter the scope of the class so that the names of
9662 entities declared within the class are available in the
9663 conversion-type-id. For example, consider:
9670 S::operator I() { ... }
9672 In order to see that `I' is a type-name in the definition, we
9673 must be in the scope of `S'. */
9675 pushed_scope = push_scope (saved_scope);
9676 /* Parse the conversion-type-id. */
9677 type = cp_parser_conversion_type_id (parser);
9678 /* Leave the scope of the class, if any. */
9680 pop_scope (pushed_scope);
9681 /* Restore the saved scope. */
9682 parser->scope = saved_scope;
9683 parser->qualifying_scope = saved_qualifying_scope;
9684 parser->object_scope = saved_object_scope;
9685 /* If the TYPE is invalid, indicate failure. */
9686 if (type == error_mark_node)
9687 return error_mark_node;
9688 return mangle_conv_op_name_for_type (type);
9691 /* Parse a conversion-type-id:
9694 type-specifier-seq conversion-declarator [opt]
9696 Returns the TYPE specified. */
9699 cp_parser_conversion_type_id (cp_parser* parser)
9702 cp_decl_specifier_seq type_specifiers;
9703 cp_declarator *declarator;
9704 tree type_specified;
9706 /* Parse the attributes. */
9707 attributes = cp_parser_attributes_opt (parser);
9708 /* Parse the type-specifiers. */
9709 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9710 /*is_trailing_return=*/false,
9712 /* If that didn't work, stop. */
9713 if (type_specifiers.type == error_mark_node)
9714 return error_mark_node;
9715 /* Parse the conversion-declarator. */
9716 declarator = cp_parser_conversion_declarator_opt (parser);
9718 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9719 /*initialized=*/0, &attributes);
9721 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9723 /* Don't give this error when parsing tentatively. This happens to
9724 work because we always parse this definitively once. */
9725 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9726 && type_uses_auto (type_specified))
9728 error ("invalid use of %<auto%> in conversion operator");
9729 return error_mark_node;
9732 return type_specified;
9735 /* Parse an (optional) conversion-declarator.
9737 conversion-declarator:
9738 ptr-operator conversion-declarator [opt]
9742 static cp_declarator *
9743 cp_parser_conversion_declarator_opt (cp_parser* parser)
9745 enum tree_code code;
9747 cp_cv_quals cv_quals;
9749 /* We don't know if there's a ptr-operator next, or not. */
9750 cp_parser_parse_tentatively (parser);
9751 /* Try the ptr-operator. */
9752 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9753 /* If it worked, look for more conversion-declarators. */
9754 if (cp_parser_parse_definitely (parser))
9756 cp_declarator *declarator;
9758 /* Parse another optional declarator. */
9759 declarator = cp_parser_conversion_declarator_opt (parser);
9761 return cp_parser_make_indirect_declarator
9762 (code, class_type, cv_quals, declarator);
9768 /* Parse an (optional) ctor-initializer.
9771 : mem-initializer-list
9773 Returns TRUE iff the ctor-initializer was actually present. */
9776 cp_parser_ctor_initializer_opt (cp_parser* parser)
9778 /* If the next token is not a `:', then there is no
9779 ctor-initializer. */
9780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9782 /* Do default initialization of any bases and members. */
9783 if (DECL_CONSTRUCTOR_P (current_function_decl))
9784 finish_mem_initializers (NULL_TREE);
9789 /* Consume the `:' token. */
9790 cp_lexer_consume_token (parser->lexer);
9791 /* And the mem-initializer-list. */
9792 cp_parser_mem_initializer_list (parser);
9797 /* Parse a mem-initializer-list.
9799 mem-initializer-list:
9800 mem-initializer ... [opt]
9801 mem-initializer ... [opt] , mem-initializer-list */
9804 cp_parser_mem_initializer_list (cp_parser* parser)
9806 tree mem_initializer_list = NULL_TREE;
9807 cp_token *token = cp_lexer_peek_token (parser->lexer);
9809 /* Let the semantic analysis code know that we are starting the
9810 mem-initializer-list. */
9811 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9812 error_at (token->location,
9813 "only constructors take base initializers");
9815 /* Loop through the list. */
9818 tree mem_initializer;
9820 token = cp_lexer_peek_token (parser->lexer);
9821 /* Parse the mem-initializer. */
9822 mem_initializer = cp_parser_mem_initializer (parser);
9823 /* If the next token is a `...', we're expanding member initializers. */
9824 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9826 /* Consume the `...'. */
9827 cp_lexer_consume_token (parser->lexer);
9829 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9830 can be expanded but members cannot. */
9831 if (mem_initializer != error_mark_node
9832 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9834 error_at (token->location,
9835 "cannot expand initializer for member %<%D%>",
9836 TREE_PURPOSE (mem_initializer));
9837 mem_initializer = error_mark_node;
9840 /* Construct the pack expansion type. */
9841 if (mem_initializer != error_mark_node)
9842 mem_initializer = make_pack_expansion (mem_initializer);
9844 /* Add it to the list, unless it was erroneous. */
9845 if (mem_initializer != error_mark_node)
9847 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9848 mem_initializer_list = mem_initializer;
9850 /* If the next token is not a `,', we're done. */
9851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9853 /* Consume the `,' token. */
9854 cp_lexer_consume_token (parser->lexer);
9857 /* Perform semantic analysis. */
9858 if (DECL_CONSTRUCTOR_P (current_function_decl))
9859 finish_mem_initializers (mem_initializer_list);
9862 /* Parse a mem-initializer.
9865 mem-initializer-id ( expression-list [opt] )
9866 mem-initializer-id braced-init-list
9871 ( expression-list [opt] )
9873 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9874 class) or FIELD_DECL (for a non-static data member) to initialize;
9875 the TREE_VALUE is the expression-list. An empty initialization
9876 list is represented by void_list_node. */
9879 cp_parser_mem_initializer (cp_parser* parser)
9881 tree mem_initializer_id;
9882 tree expression_list;
9884 cp_token *token = cp_lexer_peek_token (parser->lexer);
9886 /* Find out what is being initialized. */
9887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9889 permerror (token->location,
9890 "anachronistic old-style base class initializer");
9891 mem_initializer_id = NULL_TREE;
9895 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9896 if (mem_initializer_id == error_mark_node)
9897 return mem_initializer_id;
9899 member = expand_member_init (mem_initializer_id);
9900 if (member && !DECL_P (member))
9901 in_base_initializer = 1;
9903 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9905 bool expr_non_constant_p;
9906 maybe_warn_cpp0x ("extended initializer lists");
9907 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9908 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9909 expression_list = build_tree_list (NULL_TREE, expression_list);
9914 vec = cp_parser_parenthesized_expression_list (parser, false,
9916 /*allow_expansion_p=*/true,
9917 /*non_constant_p=*/NULL);
9919 return error_mark_node;
9920 expression_list = build_tree_list_vec (vec);
9921 release_tree_vector (vec);
9924 if (expression_list == error_mark_node)
9925 return error_mark_node;
9926 if (!expression_list)
9927 expression_list = void_type_node;
9929 in_base_initializer = 0;
9931 return member ? build_tree_list (member, expression_list) : error_mark_node;
9934 /* Parse a mem-initializer-id.
9937 :: [opt] nested-name-specifier [opt] class-name
9940 Returns a TYPE indicating the class to be initializer for the first
9941 production. Returns an IDENTIFIER_NODE indicating the data member
9942 to be initialized for the second production. */
9945 cp_parser_mem_initializer_id (cp_parser* parser)
9947 bool global_scope_p;
9948 bool nested_name_specifier_p;
9949 bool template_p = false;
9952 cp_token *token = cp_lexer_peek_token (parser->lexer);
9954 /* `typename' is not allowed in this context ([temp.res]). */
9955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9957 error_at (token->location,
9958 "keyword %<typename%> not allowed in this context (a qualified "
9959 "member initializer is implicitly a type)");
9960 cp_lexer_consume_token (parser->lexer);
9962 /* Look for the optional `::' operator. */
9964 = (cp_parser_global_scope_opt (parser,
9965 /*current_scope_valid_p=*/false)
9967 /* Look for the optional nested-name-specifier. The simplest way to
9972 The keyword `typename' is not permitted in a base-specifier or
9973 mem-initializer; in these contexts a qualified name that
9974 depends on a template-parameter is implicitly assumed to be a
9977 is to assume that we have seen the `typename' keyword at this
9979 nested_name_specifier_p
9980 = (cp_parser_nested_name_specifier_opt (parser,
9981 /*typename_keyword_p=*/true,
9982 /*check_dependency_p=*/true,
9984 /*is_declaration=*/true)
9986 if (nested_name_specifier_p)
9987 template_p = cp_parser_optional_template_keyword (parser);
9988 /* If there is a `::' operator or a nested-name-specifier, then we
9989 are definitely looking for a class-name. */
9990 if (global_scope_p || nested_name_specifier_p)
9991 return cp_parser_class_name (parser,
9992 /*typename_keyword_p=*/true,
9993 /*template_keyword_p=*/template_p,
9995 /*check_dependency_p=*/true,
9996 /*class_head_p=*/false,
9997 /*is_declaration=*/true);
9998 /* Otherwise, we could also be looking for an ordinary identifier. */
9999 cp_parser_parse_tentatively (parser);
10000 /* Try a class-name. */
10001 id = cp_parser_class_name (parser,
10002 /*typename_keyword_p=*/true,
10003 /*template_keyword_p=*/false,
10005 /*check_dependency_p=*/true,
10006 /*class_head_p=*/false,
10007 /*is_declaration=*/true);
10008 /* If we found one, we're done. */
10009 if (cp_parser_parse_definitely (parser))
10011 /* Otherwise, look for an ordinary identifier. */
10012 return cp_parser_identifier (parser);
10015 /* Overloading [gram.over] */
10017 /* Parse an operator-function-id.
10019 operator-function-id:
10022 Returns an IDENTIFIER_NODE for the operator which is a
10023 human-readable spelling of the identifier, e.g., `operator +'. */
10026 cp_parser_operator_function_id (cp_parser* parser)
10028 /* Look for the `operator' keyword. */
10029 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10030 return error_mark_node;
10031 /* And then the name of the operator itself. */
10032 return cp_parser_operator (parser);
10035 /* Parse an operator.
10038 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10039 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10040 || ++ -- , ->* -> () []
10047 Returns an IDENTIFIER_NODE for the operator which is a
10048 human-readable spelling of the identifier, e.g., `operator +'. */
10051 cp_parser_operator (cp_parser* parser)
10053 tree id = NULL_TREE;
10056 /* Peek at the next token. */
10057 token = cp_lexer_peek_token (parser->lexer);
10058 /* Figure out which operator we have. */
10059 switch (token->type)
10065 /* The keyword should be either `new' or `delete'. */
10066 if (token->keyword == RID_NEW)
10068 else if (token->keyword == RID_DELETE)
10073 /* Consume the `new' or `delete' token. */
10074 cp_lexer_consume_token (parser->lexer);
10076 /* Peek at the next token. */
10077 token = cp_lexer_peek_token (parser->lexer);
10078 /* If it's a `[' token then this is the array variant of the
10080 if (token->type == CPP_OPEN_SQUARE)
10082 /* Consume the `[' token. */
10083 cp_lexer_consume_token (parser->lexer);
10084 /* Look for the `]' token. */
10085 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10086 id = ansi_opname (op == NEW_EXPR
10087 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10089 /* Otherwise, we have the non-array variant. */
10091 id = ansi_opname (op);
10097 id = ansi_opname (PLUS_EXPR);
10101 id = ansi_opname (MINUS_EXPR);
10105 id = ansi_opname (MULT_EXPR);
10109 id = ansi_opname (TRUNC_DIV_EXPR);
10113 id = ansi_opname (TRUNC_MOD_EXPR);
10117 id = ansi_opname (BIT_XOR_EXPR);
10121 id = ansi_opname (BIT_AND_EXPR);
10125 id = ansi_opname (BIT_IOR_EXPR);
10129 id = ansi_opname (BIT_NOT_EXPR);
10133 id = ansi_opname (TRUTH_NOT_EXPR);
10137 id = ansi_assopname (NOP_EXPR);
10141 id = ansi_opname (LT_EXPR);
10145 id = ansi_opname (GT_EXPR);
10149 id = ansi_assopname (PLUS_EXPR);
10153 id = ansi_assopname (MINUS_EXPR);
10157 id = ansi_assopname (MULT_EXPR);
10161 id = ansi_assopname (TRUNC_DIV_EXPR);
10165 id = ansi_assopname (TRUNC_MOD_EXPR);
10169 id = ansi_assopname (BIT_XOR_EXPR);
10173 id = ansi_assopname (BIT_AND_EXPR);
10177 id = ansi_assopname (BIT_IOR_EXPR);
10181 id = ansi_opname (LSHIFT_EXPR);
10185 id = ansi_opname (RSHIFT_EXPR);
10188 case CPP_LSHIFT_EQ:
10189 id = ansi_assopname (LSHIFT_EXPR);
10192 case CPP_RSHIFT_EQ:
10193 id = ansi_assopname (RSHIFT_EXPR);
10197 id = ansi_opname (EQ_EXPR);
10201 id = ansi_opname (NE_EXPR);
10205 id = ansi_opname (LE_EXPR);
10208 case CPP_GREATER_EQ:
10209 id = ansi_opname (GE_EXPR);
10213 id = ansi_opname (TRUTH_ANDIF_EXPR);
10217 id = ansi_opname (TRUTH_ORIF_EXPR);
10220 case CPP_PLUS_PLUS:
10221 id = ansi_opname (POSTINCREMENT_EXPR);
10224 case CPP_MINUS_MINUS:
10225 id = ansi_opname (PREDECREMENT_EXPR);
10229 id = ansi_opname (COMPOUND_EXPR);
10232 case CPP_DEREF_STAR:
10233 id = ansi_opname (MEMBER_REF);
10237 id = ansi_opname (COMPONENT_REF);
10240 case CPP_OPEN_PAREN:
10241 /* Consume the `('. */
10242 cp_lexer_consume_token (parser->lexer);
10243 /* Look for the matching `)'. */
10244 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10245 return ansi_opname (CALL_EXPR);
10247 case CPP_OPEN_SQUARE:
10248 /* Consume the `['. */
10249 cp_lexer_consume_token (parser->lexer);
10250 /* Look for the matching `]'. */
10251 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10252 return ansi_opname (ARRAY_REF);
10255 /* Anything else is an error. */
10259 /* If we have selected an identifier, we need to consume the
10262 cp_lexer_consume_token (parser->lexer);
10263 /* Otherwise, no valid operator name was present. */
10266 cp_parser_error (parser, "expected operator");
10267 id = error_mark_node;
10273 /* Parse a template-declaration.
10275 template-declaration:
10276 export [opt] template < template-parameter-list > declaration
10278 If MEMBER_P is TRUE, this template-declaration occurs within a
10281 The grammar rule given by the standard isn't correct. What
10282 is really meant is:
10284 template-declaration:
10285 export [opt] template-parameter-list-seq
10286 decl-specifier-seq [opt] init-declarator [opt] ;
10287 export [opt] template-parameter-list-seq
10288 function-definition
10290 template-parameter-list-seq:
10291 template-parameter-list-seq [opt]
10292 template < template-parameter-list > */
10295 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10297 /* Check for `export'. */
10298 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10300 /* Consume the `export' token. */
10301 cp_lexer_consume_token (parser->lexer);
10302 /* Warn that we do not support `export'. */
10303 warning (0, "keyword %<export%> not implemented, and will be ignored");
10306 cp_parser_template_declaration_after_export (parser, member_p);
10309 /* Parse a template-parameter-list.
10311 template-parameter-list:
10313 template-parameter-list , template-parameter
10315 Returns a TREE_LIST. Each node represents a template parameter.
10316 The nodes are connected via their TREE_CHAINs. */
10319 cp_parser_template_parameter_list (cp_parser* parser)
10321 tree parameter_list = NULL_TREE;
10323 begin_template_parm_list ();
10328 bool is_parameter_pack;
10329 location_t parm_loc;
10331 /* Parse the template-parameter. */
10332 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10333 parameter = cp_parser_template_parameter (parser,
10335 &is_parameter_pack);
10336 /* Add it to the list. */
10337 if (parameter != error_mark_node)
10338 parameter_list = process_template_parm (parameter_list,
10342 is_parameter_pack);
10345 tree err_parm = build_tree_list (parameter, parameter);
10346 TREE_VALUE (err_parm) = error_mark_node;
10347 parameter_list = chainon (parameter_list, err_parm);
10350 /* If the next token is not a `,', we're done. */
10351 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10353 /* Otherwise, consume the `,' token. */
10354 cp_lexer_consume_token (parser->lexer);
10357 return end_template_parm_list (parameter_list);
10360 /* Parse a template-parameter.
10362 template-parameter:
10364 parameter-declaration
10366 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10367 the parameter. The TREE_PURPOSE is the default value, if any.
10368 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10369 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10370 set to true iff this parameter is a parameter pack. */
10373 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10374 bool *is_parameter_pack)
10377 cp_parameter_declarator *parameter_declarator;
10378 cp_declarator *id_declarator;
10381 /* Assume it is a type parameter or a template parameter. */
10382 *is_non_type = false;
10383 /* Assume it not a parameter pack. */
10384 *is_parameter_pack = false;
10385 /* Peek at the next token. */
10386 token = cp_lexer_peek_token (parser->lexer);
10387 /* If it is `class' or `template', we have a type-parameter. */
10388 if (token->keyword == RID_TEMPLATE)
10389 return cp_parser_type_parameter (parser, is_parameter_pack);
10390 /* If it is `class' or `typename' we do not know yet whether it is a
10391 type parameter or a non-type parameter. Consider:
10393 template <typename T, typename T::X X> ...
10397 template <class C, class D*> ...
10399 Here, the first parameter is a type parameter, and the second is
10400 a non-type parameter. We can tell by looking at the token after
10401 the identifier -- if it is a `,', `=', or `>' then we have a type
10403 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10405 /* Peek at the token after `class' or `typename'. */
10406 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10407 /* If it's an ellipsis, we have a template type parameter
10409 if (token->type == CPP_ELLIPSIS)
10410 return cp_parser_type_parameter (parser, is_parameter_pack);
10411 /* If it's an identifier, skip it. */
10412 if (token->type == CPP_NAME)
10413 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10414 /* Now, see if the token looks like the end of a template
10416 if (token->type == CPP_COMMA
10417 || token->type == CPP_EQ
10418 || token->type == CPP_GREATER)
10419 return cp_parser_type_parameter (parser, is_parameter_pack);
10422 /* Otherwise, it is a non-type parameter.
10426 When parsing a default template-argument for a non-type
10427 template-parameter, the first non-nested `>' is taken as the end
10428 of the template parameter-list rather than a greater-than
10430 *is_non_type = true;
10431 parameter_declarator
10432 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10433 /*parenthesized_p=*/NULL);
10435 /* If the parameter declaration is marked as a parameter pack, set
10436 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10437 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10439 if (parameter_declarator
10440 && parameter_declarator->declarator
10441 && parameter_declarator->declarator->parameter_pack_p)
10443 *is_parameter_pack = true;
10444 parameter_declarator->declarator->parameter_pack_p = false;
10447 /* If the next token is an ellipsis, and we don't already have it
10448 marked as a parameter pack, then we have a parameter pack (that
10449 has no declarator). */
10450 if (!*is_parameter_pack
10451 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10452 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10454 /* Consume the `...'. */
10455 cp_lexer_consume_token (parser->lexer);
10456 maybe_warn_variadic_templates ();
10458 *is_parameter_pack = true;
10460 /* We might end up with a pack expansion as the type of the non-type
10461 template parameter, in which case this is a non-type template
10463 else if (parameter_declarator
10464 && parameter_declarator->decl_specifiers.type
10465 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10467 *is_parameter_pack = true;
10468 parameter_declarator->decl_specifiers.type =
10469 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10472 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10474 /* Parameter packs cannot have default arguments. However, a
10475 user may try to do so, so we'll parse them and give an
10476 appropriate diagnostic here. */
10478 /* Consume the `='. */
10479 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10480 cp_lexer_consume_token (parser->lexer);
10482 /* Find the name of the parameter pack. */
10483 id_declarator = parameter_declarator->declarator;
10484 while (id_declarator && id_declarator->kind != cdk_id)
10485 id_declarator = id_declarator->declarator;
10487 if (id_declarator && id_declarator->kind == cdk_id)
10488 error_at (start_token->location,
10489 "template parameter pack %qD cannot have a default argument",
10490 id_declarator->u.id.unqualified_name);
10492 error_at (start_token->location,
10493 "template parameter pack cannot have a default argument");
10495 /* Parse the default argument, but throw away the result. */
10496 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10499 parm = grokdeclarator (parameter_declarator->declarator,
10500 ¶meter_declarator->decl_specifiers,
10501 PARM, /*initialized=*/0,
10502 /*attrlist=*/NULL);
10503 if (parm == error_mark_node)
10504 return error_mark_node;
10506 return build_tree_list (parameter_declarator->default_argument, parm);
10509 /* Parse a type-parameter.
10512 class identifier [opt]
10513 class identifier [opt] = type-id
10514 typename identifier [opt]
10515 typename identifier [opt] = type-id
10516 template < template-parameter-list > class identifier [opt]
10517 template < template-parameter-list > class identifier [opt]
10520 GNU Extension (variadic templates):
10523 class ... identifier [opt]
10524 typename ... identifier [opt]
10526 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10527 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10528 the declaration of the parameter.
10530 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10533 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10538 /* Look for a keyword to tell us what kind of parameter this is. */
10539 token = cp_parser_require (parser, CPP_KEYWORD,
10540 "%<class%>, %<typename%>, or %<template%>");
10542 return error_mark_node;
10544 switch (token->keyword)
10550 tree default_argument;
10552 /* If the next token is an ellipsis, we have a template
10554 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10556 /* Consume the `...' token. */
10557 cp_lexer_consume_token (parser->lexer);
10558 maybe_warn_variadic_templates ();
10560 *is_parameter_pack = true;
10563 /* If the next token is an identifier, then it names the
10565 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10566 identifier = cp_parser_identifier (parser);
10568 identifier = NULL_TREE;
10570 /* Create the parameter. */
10571 parameter = finish_template_type_parm (class_type_node, identifier);
10573 /* If the next token is an `=', we have a default argument. */
10574 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10576 /* Consume the `=' token. */
10577 cp_lexer_consume_token (parser->lexer);
10578 /* Parse the default-argument. */
10579 push_deferring_access_checks (dk_no_deferred);
10580 default_argument = cp_parser_type_id (parser);
10582 /* Template parameter packs cannot have default
10584 if (*is_parameter_pack)
10587 error_at (token->location,
10588 "template parameter pack %qD cannot have a "
10589 "default argument", identifier);
10591 error_at (token->location,
10592 "template parameter packs cannot have "
10593 "default arguments");
10594 default_argument = NULL_TREE;
10596 pop_deferring_access_checks ();
10599 default_argument = NULL_TREE;
10601 /* Create the combined representation of the parameter and the
10602 default argument. */
10603 parameter = build_tree_list (default_argument, parameter);
10609 tree parameter_list;
10611 tree default_argument;
10613 /* Look for the `<'. */
10614 cp_parser_require (parser, CPP_LESS, "%<<%>");
10615 /* Parse the template-parameter-list. */
10616 parameter_list = cp_parser_template_parameter_list (parser);
10617 /* Look for the `>'. */
10618 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10619 /* Look for the `class' keyword. */
10620 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10621 /* If the next token is an ellipsis, we have a template
10623 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10625 /* Consume the `...' token. */
10626 cp_lexer_consume_token (parser->lexer);
10627 maybe_warn_variadic_templates ();
10629 *is_parameter_pack = true;
10631 /* If the next token is an `=', then there is a
10632 default-argument. If the next token is a `>', we are at
10633 the end of the parameter-list. If the next token is a `,',
10634 then we are at the end of this parameter. */
10635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10636 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10637 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10639 identifier = cp_parser_identifier (parser);
10640 /* Treat invalid names as if the parameter were nameless. */
10641 if (identifier == error_mark_node)
10642 identifier = NULL_TREE;
10645 identifier = NULL_TREE;
10647 /* Create the template parameter. */
10648 parameter = finish_template_template_parm (class_type_node,
10651 /* If the next token is an `=', then there is a
10652 default-argument. */
10653 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10657 /* Consume the `='. */
10658 cp_lexer_consume_token (parser->lexer);
10659 /* Parse the id-expression. */
10660 push_deferring_access_checks (dk_no_deferred);
10661 /* save token before parsing the id-expression, for error
10663 token = cp_lexer_peek_token (parser->lexer);
10665 = cp_parser_id_expression (parser,
10666 /*template_keyword_p=*/false,
10667 /*check_dependency_p=*/true,
10668 /*template_p=*/&is_template,
10669 /*declarator_p=*/false,
10670 /*optional_p=*/false);
10671 if (TREE_CODE (default_argument) == TYPE_DECL)
10672 /* If the id-expression was a template-id that refers to
10673 a template-class, we already have the declaration here,
10674 so no further lookup is needed. */
10677 /* Look up the name. */
10679 = cp_parser_lookup_name (parser, default_argument,
10681 /*is_template=*/is_template,
10682 /*is_namespace=*/false,
10683 /*check_dependency=*/true,
10684 /*ambiguous_decls=*/NULL,
10686 /* See if the default argument is valid. */
10688 = check_template_template_default_arg (default_argument);
10690 /* Template parameter packs cannot have default
10692 if (*is_parameter_pack)
10695 error_at (token->location,
10696 "template parameter pack %qD cannot "
10697 "have a default argument",
10700 error_at (token->location, "template parameter packs cannot "
10701 "have default arguments");
10702 default_argument = NULL_TREE;
10704 pop_deferring_access_checks ();
10707 default_argument = NULL_TREE;
10709 /* Create the combined representation of the parameter and the
10710 default argument. */
10711 parameter = build_tree_list (default_argument, parameter);
10716 gcc_unreachable ();
10723 /* Parse a template-id.
10726 template-name < template-argument-list [opt] >
10728 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10729 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10730 returned. Otherwise, if the template-name names a function, or set
10731 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10732 names a class, returns a TYPE_DECL for the specialization.
10734 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10735 uninstantiated templates. */
10738 cp_parser_template_id (cp_parser *parser,
10739 bool template_keyword_p,
10740 bool check_dependency_p,
10741 bool is_declaration)
10747 cp_token_position start_of_id = 0;
10748 deferred_access_check *chk;
10749 VEC (deferred_access_check,gc) *access_check;
10750 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10751 bool is_identifier;
10753 /* If the next token corresponds to a template-id, there is no need
10755 next_token = cp_lexer_peek_token (parser->lexer);
10756 if (next_token->type == CPP_TEMPLATE_ID)
10758 struct tree_check *check_value;
10760 /* Get the stored value. */
10761 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10762 /* Perform any access checks that were deferred. */
10763 access_check = check_value->checks;
10767 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10770 perform_or_defer_access_check (chk->binfo,
10775 /* Return the stored value. */
10776 return check_value->value;
10779 /* Avoid performing name lookup if there is no possibility of
10780 finding a template-id. */
10781 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10782 || (next_token->type == CPP_NAME
10783 && !cp_parser_nth_token_starts_template_argument_list_p
10786 cp_parser_error (parser, "expected template-id");
10787 return error_mark_node;
10790 /* Remember where the template-id starts. */
10791 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10792 start_of_id = cp_lexer_token_position (parser->lexer, false);
10794 push_deferring_access_checks (dk_deferred);
10796 /* Parse the template-name. */
10797 is_identifier = false;
10798 token = cp_lexer_peek_token (parser->lexer);
10799 templ = cp_parser_template_name (parser, template_keyword_p,
10800 check_dependency_p,
10803 if (templ == error_mark_node || is_identifier)
10805 pop_deferring_access_checks ();
10809 /* If we find the sequence `[:' after a template-name, it's probably
10810 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10811 parse correctly the argument list. */
10812 next_token = cp_lexer_peek_token (parser->lexer);
10813 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10814 if (next_token->type == CPP_OPEN_SQUARE
10815 && next_token->flags & DIGRAPH
10816 && next_token_2->type == CPP_COLON
10817 && !(next_token_2->flags & PREV_WHITE))
10819 cp_parser_parse_tentatively (parser);
10820 /* Change `:' into `::'. */
10821 next_token_2->type = CPP_SCOPE;
10822 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10824 cp_lexer_consume_token (parser->lexer);
10826 /* Parse the arguments. */
10827 arguments = cp_parser_enclosed_template_argument_list (parser);
10828 if (!cp_parser_parse_definitely (parser))
10830 /* If we couldn't parse an argument list, then we revert our changes
10831 and return simply an error. Maybe this is not a template-id
10833 next_token_2->type = CPP_COLON;
10834 cp_parser_error (parser, "expected %<<%>");
10835 pop_deferring_access_checks ();
10836 return error_mark_node;
10838 /* Otherwise, emit an error about the invalid digraph, but continue
10839 parsing because we got our argument list. */
10840 if (permerror (next_token->location,
10841 "%<<::%> cannot begin a template-argument list"))
10843 static bool hint = false;
10844 inform (next_token->location,
10845 "%<<:%> is an alternate spelling for %<[%>."
10846 " Insert whitespace between %<<%> and %<::%>");
10847 if (!hint && !flag_permissive)
10849 inform (next_token->location, "(if you use %<-fpermissive%>"
10850 " G++ will accept your code)");
10857 /* Look for the `<' that starts the template-argument-list. */
10858 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10860 pop_deferring_access_checks ();
10861 return error_mark_node;
10863 /* Parse the arguments. */
10864 arguments = cp_parser_enclosed_template_argument_list (parser);
10867 /* Build a representation of the specialization. */
10868 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10869 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10870 else if (DECL_CLASS_TEMPLATE_P (templ)
10871 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10873 bool entering_scope;
10874 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10875 template (rather than some instantiation thereof) only if
10876 is not nested within some other construct. For example, in
10877 "template <typename T> void f(T) { A<T>::", A<T> is just an
10878 instantiation of A. */
10879 entering_scope = (template_parm_scope_p ()
10880 && cp_lexer_next_token_is (parser->lexer,
10883 = finish_template_type (templ, arguments, entering_scope);
10887 /* If it's not a class-template or a template-template, it should be
10888 a function-template. */
10889 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10890 || TREE_CODE (templ) == OVERLOAD
10891 || BASELINK_P (templ)));
10893 template_id = lookup_template_function (templ, arguments);
10896 /* If parsing tentatively, replace the sequence of tokens that makes
10897 up the template-id with a CPP_TEMPLATE_ID token. That way,
10898 should we re-parse the token stream, we will not have to repeat
10899 the effort required to do the parse, nor will we issue duplicate
10900 error messages about problems during instantiation of the
10904 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10906 /* Reset the contents of the START_OF_ID token. */
10907 token->type = CPP_TEMPLATE_ID;
10908 /* Retrieve any deferred checks. Do not pop this access checks yet
10909 so the memory will not be reclaimed during token replacing below. */
10910 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10911 token->u.tree_check_value->value = template_id;
10912 token->u.tree_check_value->checks = get_deferred_access_checks ();
10913 token->keyword = RID_MAX;
10915 /* Purge all subsequent tokens. */
10916 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10918 /* ??? Can we actually assume that, if template_id ==
10919 error_mark_node, we will have issued a diagnostic to the
10920 user, as opposed to simply marking the tentative parse as
10922 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10923 error_at (token->location, "parse error in template argument list");
10926 pop_deferring_access_checks ();
10927 return template_id;
10930 /* Parse a template-name.
10935 The standard should actually say:
10939 operator-function-id
10941 A defect report has been filed about this issue.
10943 A conversion-function-id cannot be a template name because they cannot
10944 be part of a template-id. In fact, looking at this code:
10946 a.operator K<int>()
10948 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10949 It is impossible to call a templated conversion-function-id with an
10950 explicit argument list, since the only allowed template parameter is
10951 the type to which it is converting.
10953 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10954 `template' keyword, in a construction like:
10958 In that case `f' is taken to be a template-name, even though there
10959 is no way of knowing for sure.
10961 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10962 name refers to a set of overloaded functions, at least one of which
10963 is a template, or an IDENTIFIER_NODE with the name of the template,
10964 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10965 names are looked up inside uninstantiated templates. */
10968 cp_parser_template_name (cp_parser* parser,
10969 bool template_keyword_p,
10970 bool check_dependency_p,
10971 bool is_declaration,
10972 bool *is_identifier)
10977 cp_token *token = cp_lexer_peek_token (parser->lexer);
10979 /* If the next token is `operator', then we have either an
10980 operator-function-id or a conversion-function-id. */
10981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10983 /* We don't know whether we're looking at an
10984 operator-function-id or a conversion-function-id. */
10985 cp_parser_parse_tentatively (parser);
10986 /* Try an operator-function-id. */
10987 identifier = cp_parser_operator_function_id (parser);
10988 /* If that didn't work, try a conversion-function-id. */
10989 if (!cp_parser_parse_definitely (parser))
10991 cp_parser_error (parser, "expected template-name");
10992 return error_mark_node;
10995 /* Look for the identifier. */
10997 identifier = cp_parser_identifier (parser);
10999 /* If we didn't find an identifier, we don't have a template-id. */
11000 if (identifier == error_mark_node)
11001 return error_mark_node;
11003 /* If the name immediately followed the `template' keyword, then it
11004 is a template-name. However, if the next token is not `<', then
11005 we do not treat it as a template-name, since it is not being used
11006 as part of a template-id. This enables us to handle constructs
11009 template <typename T> struct S { S(); };
11010 template <typename T> S<T>::S();
11012 correctly. We would treat `S' as a template -- if it were `S<T>'
11013 -- but we do not if there is no `<'. */
11015 if (processing_template_decl
11016 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11018 /* In a declaration, in a dependent context, we pretend that the
11019 "template" keyword was present in order to improve error
11020 recovery. For example, given:
11022 template <typename T> void f(T::X<int>);
11024 we want to treat "X<int>" as a template-id. */
11026 && !template_keyword_p
11027 && parser->scope && TYPE_P (parser->scope)
11028 && check_dependency_p
11029 && dependent_scope_p (parser->scope)
11030 /* Do not do this for dtors (or ctors), since they never
11031 need the template keyword before their name. */
11032 && !constructor_name_p (identifier, parser->scope))
11034 cp_token_position start = 0;
11036 /* Explain what went wrong. */
11037 error_at (token->location, "non-template %qD used as template",
11039 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11040 parser->scope, identifier);
11041 /* If parsing tentatively, find the location of the "<" token. */
11042 if (cp_parser_simulate_error (parser))
11043 start = cp_lexer_token_position (parser->lexer, true);
11044 /* Parse the template arguments so that we can issue error
11045 messages about them. */
11046 cp_lexer_consume_token (parser->lexer);
11047 cp_parser_enclosed_template_argument_list (parser);
11048 /* Skip tokens until we find a good place from which to
11049 continue parsing. */
11050 cp_parser_skip_to_closing_parenthesis (parser,
11051 /*recovering=*/true,
11053 /*consume_paren=*/false);
11054 /* If parsing tentatively, permanently remove the
11055 template argument list. That will prevent duplicate
11056 error messages from being issued about the missing
11057 "template" keyword. */
11059 cp_lexer_purge_tokens_after (parser->lexer, start);
11061 *is_identifier = true;
11065 /* If the "template" keyword is present, then there is generally
11066 no point in doing name-lookup, so we just return IDENTIFIER.
11067 But, if the qualifying scope is non-dependent then we can
11068 (and must) do name-lookup normally. */
11069 if (template_keyword_p
11071 || (TYPE_P (parser->scope)
11072 && dependent_type_p (parser->scope))))
11076 /* Look up the name. */
11077 decl = cp_parser_lookup_name (parser, identifier,
11079 /*is_template=*/false,
11080 /*is_namespace=*/false,
11081 check_dependency_p,
11082 /*ambiguous_decls=*/NULL,
11084 decl = maybe_get_template_decl_from_type_decl (decl);
11086 /* If DECL is a template, then the name was a template-name. */
11087 if (TREE_CODE (decl) == TEMPLATE_DECL)
11091 tree fn = NULL_TREE;
11093 /* The standard does not explicitly indicate whether a name that
11094 names a set of overloaded declarations, some of which are
11095 templates, is a template-name. However, such a name should
11096 be a template-name; otherwise, there is no way to form a
11097 template-id for the overloaded templates. */
11098 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11099 if (TREE_CODE (fns) == OVERLOAD)
11100 for (fn = fns; fn; fn = OVL_NEXT (fn))
11101 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11106 /* The name does not name a template. */
11107 cp_parser_error (parser, "expected template-name");
11108 return error_mark_node;
11112 /* If DECL is dependent, and refers to a function, then just return
11113 its name; we will look it up again during template instantiation. */
11114 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11116 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11117 if (TYPE_P (scope) && dependent_type_p (scope))
11124 /* Parse a template-argument-list.
11126 template-argument-list:
11127 template-argument ... [opt]
11128 template-argument-list , template-argument ... [opt]
11130 Returns a TREE_VEC containing the arguments. */
11133 cp_parser_template_argument_list (cp_parser* parser)
11135 tree fixed_args[10];
11136 unsigned n_args = 0;
11137 unsigned alloced = 10;
11138 tree *arg_ary = fixed_args;
11140 bool saved_in_template_argument_list_p;
11142 bool saved_non_ice_p;
11144 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11145 parser->in_template_argument_list_p = true;
11146 /* Even if the template-id appears in an integral
11147 constant-expression, the contents of the argument list do
11149 saved_ice_p = parser->integral_constant_expression_p;
11150 parser->integral_constant_expression_p = false;
11151 saved_non_ice_p = parser->non_integral_constant_expression_p;
11152 parser->non_integral_constant_expression_p = false;
11153 /* Parse the arguments. */
11159 /* Consume the comma. */
11160 cp_lexer_consume_token (parser->lexer);
11162 /* Parse the template-argument. */
11163 argument = cp_parser_template_argument (parser);
11165 /* If the next token is an ellipsis, we're expanding a template
11167 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11169 if (argument == error_mark_node)
11171 cp_token *token = cp_lexer_peek_token (parser->lexer);
11172 error_at (token->location,
11173 "expected parameter pack before %<...%>");
11175 /* Consume the `...' token. */
11176 cp_lexer_consume_token (parser->lexer);
11178 /* Make the argument into a TYPE_PACK_EXPANSION or
11179 EXPR_PACK_EXPANSION. */
11180 argument = make_pack_expansion (argument);
11183 if (n_args == alloced)
11187 if (arg_ary == fixed_args)
11189 arg_ary = XNEWVEC (tree, alloced);
11190 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11193 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11195 arg_ary[n_args++] = argument;
11197 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11199 vec = make_tree_vec (n_args);
11202 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11204 if (arg_ary != fixed_args)
11206 parser->non_integral_constant_expression_p = saved_non_ice_p;
11207 parser->integral_constant_expression_p = saved_ice_p;
11208 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11212 /* Parse a template-argument.
11215 assignment-expression
11219 The representation is that of an assignment-expression, type-id, or
11220 id-expression -- except that the qualified id-expression is
11221 evaluated, so that the value returned is either a DECL or an
11224 Although the standard says "assignment-expression", it forbids
11225 throw-expressions or assignments in the template argument.
11226 Therefore, we use "conditional-expression" instead. */
11229 cp_parser_template_argument (cp_parser* parser)
11234 bool maybe_type_id = false;
11235 cp_token *token = NULL, *argument_start_token = NULL;
11238 /* There's really no way to know what we're looking at, so we just
11239 try each alternative in order.
11243 In a template-argument, an ambiguity between a type-id and an
11244 expression is resolved to a type-id, regardless of the form of
11245 the corresponding template-parameter.
11247 Therefore, we try a type-id first. */
11248 cp_parser_parse_tentatively (parser);
11249 argument = cp_parser_template_type_arg (parser);
11250 /* If there was no error parsing the type-id but the next token is a
11251 '>>', our behavior depends on which dialect of C++ we're
11252 parsing. In C++98, we probably found a typo for '> >'. But there
11253 are type-id which are also valid expressions. For instance:
11255 struct X { int operator >> (int); };
11256 template <int V> struct Foo {};
11259 Here 'X()' is a valid type-id of a function type, but the user just
11260 wanted to write the expression "X() >> 5". Thus, we remember that we
11261 found a valid type-id, but we still try to parse the argument as an
11262 expression to see what happens.
11264 In C++0x, the '>>' will be considered two separate '>'
11266 if (!cp_parser_error_occurred (parser)
11267 && cxx_dialect == cxx98
11268 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11270 maybe_type_id = true;
11271 cp_parser_abort_tentative_parse (parser);
11275 /* If the next token isn't a `,' or a `>', then this argument wasn't
11276 really finished. This means that the argument is not a valid
11278 if (!cp_parser_next_token_ends_template_argument_p (parser))
11279 cp_parser_error (parser, "expected template-argument");
11280 /* If that worked, we're done. */
11281 if (cp_parser_parse_definitely (parser))
11284 /* We're still not sure what the argument will be. */
11285 cp_parser_parse_tentatively (parser);
11286 /* Try a template. */
11287 argument_start_token = cp_lexer_peek_token (parser->lexer);
11288 argument = cp_parser_id_expression (parser,
11289 /*template_keyword_p=*/false,
11290 /*check_dependency_p=*/true,
11292 /*declarator_p=*/false,
11293 /*optional_p=*/false);
11294 /* If the next token isn't a `,' or a `>', then this argument wasn't
11295 really finished. */
11296 if (!cp_parser_next_token_ends_template_argument_p (parser))
11297 cp_parser_error (parser, "expected template-argument");
11298 if (!cp_parser_error_occurred (parser))
11300 /* Figure out what is being referred to. If the id-expression
11301 was for a class template specialization, then we will have a
11302 TYPE_DECL at this point. There is no need to do name lookup
11303 at this point in that case. */
11304 if (TREE_CODE (argument) != TYPE_DECL)
11305 argument = cp_parser_lookup_name (parser, argument,
11307 /*is_template=*/template_p,
11308 /*is_namespace=*/false,
11309 /*check_dependency=*/true,
11310 /*ambiguous_decls=*/NULL,
11311 argument_start_token->location);
11312 if (TREE_CODE (argument) != TEMPLATE_DECL
11313 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11314 cp_parser_error (parser, "expected template-name");
11316 if (cp_parser_parse_definitely (parser))
11318 /* It must be a non-type argument. There permitted cases are given
11319 in [temp.arg.nontype]:
11321 -- an integral constant-expression of integral or enumeration
11324 -- the name of a non-type template-parameter; or
11326 -- the name of an object or function with external linkage...
11328 -- the address of an object or function with external linkage...
11330 -- a pointer to member... */
11331 /* Look for a non-type template parameter. */
11332 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11334 cp_parser_parse_tentatively (parser);
11335 argument = cp_parser_primary_expression (parser,
11336 /*address_p=*/false,
11338 /*template_arg_p=*/true,
11340 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11341 || !cp_parser_next_token_ends_template_argument_p (parser))
11342 cp_parser_simulate_error (parser);
11343 if (cp_parser_parse_definitely (parser))
11347 /* If the next token is "&", the argument must be the address of an
11348 object or function with external linkage. */
11349 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11351 cp_lexer_consume_token (parser->lexer);
11352 /* See if we might have an id-expression. */
11353 token = cp_lexer_peek_token (parser->lexer);
11354 if (token->type == CPP_NAME
11355 || token->keyword == RID_OPERATOR
11356 || token->type == CPP_SCOPE
11357 || token->type == CPP_TEMPLATE_ID
11358 || token->type == CPP_NESTED_NAME_SPECIFIER)
11360 cp_parser_parse_tentatively (parser);
11361 argument = cp_parser_primary_expression (parser,
11364 /*template_arg_p=*/true,
11366 if (cp_parser_error_occurred (parser)
11367 || !cp_parser_next_token_ends_template_argument_p (parser))
11368 cp_parser_abort_tentative_parse (parser);
11373 if (TREE_CODE (argument) == INDIRECT_REF)
11375 gcc_assert (REFERENCE_REF_P (argument));
11376 argument = TREE_OPERAND (argument, 0);
11379 /* If we're in a template, we represent a qualified-id referring
11380 to a static data member as a SCOPE_REF even if the scope isn't
11381 dependent so that we can check access control later. */
11383 if (TREE_CODE (probe) == SCOPE_REF)
11384 probe = TREE_OPERAND (probe, 1);
11385 if (TREE_CODE (probe) == VAR_DECL)
11387 /* A variable without external linkage might still be a
11388 valid constant-expression, so no error is issued here
11389 if the external-linkage check fails. */
11390 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11391 cp_parser_simulate_error (parser);
11393 else if (is_overloaded_fn (argument))
11394 /* All overloaded functions are allowed; if the external
11395 linkage test does not pass, an error will be issued
11399 && (TREE_CODE (argument) == OFFSET_REF
11400 || TREE_CODE (argument) == SCOPE_REF))
11401 /* A pointer-to-member. */
11403 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11406 cp_parser_simulate_error (parser);
11408 if (cp_parser_parse_definitely (parser))
11411 argument = build_x_unary_op (ADDR_EXPR, argument,
11412 tf_warning_or_error);
11417 /* If the argument started with "&", there are no other valid
11418 alternatives at this point. */
11421 cp_parser_error (parser, "invalid non-type template argument");
11422 return error_mark_node;
11425 /* If the argument wasn't successfully parsed as a type-id followed
11426 by '>>', the argument can only be a constant expression now.
11427 Otherwise, we try parsing the constant-expression tentatively,
11428 because the argument could really be a type-id. */
11430 cp_parser_parse_tentatively (parser);
11431 argument = cp_parser_constant_expression (parser,
11432 /*allow_non_constant_p=*/false,
11433 /*non_constant_p=*/NULL);
11434 argument = fold_non_dependent_expr (argument);
11435 if (!maybe_type_id)
11437 if (!cp_parser_next_token_ends_template_argument_p (parser))
11438 cp_parser_error (parser, "expected template-argument");
11439 if (cp_parser_parse_definitely (parser))
11441 /* We did our best to parse the argument as a non type-id, but that
11442 was the only alternative that matched (albeit with a '>' after
11443 it). We can assume it's just a typo from the user, and a
11444 diagnostic will then be issued. */
11445 return cp_parser_template_type_arg (parser);
11448 /* Parse an explicit-instantiation.
11450 explicit-instantiation:
11451 template declaration
11453 Although the standard says `declaration', what it really means is:
11455 explicit-instantiation:
11456 template decl-specifier-seq [opt] declarator [opt] ;
11458 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11459 supposed to be allowed. A defect report has been filed about this
11464 explicit-instantiation:
11465 storage-class-specifier template
11466 decl-specifier-seq [opt] declarator [opt] ;
11467 function-specifier template
11468 decl-specifier-seq [opt] declarator [opt] ; */
11471 cp_parser_explicit_instantiation (cp_parser* parser)
11473 int declares_class_or_enum;
11474 cp_decl_specifier_seq decl_specifiers;
11475 tree extension_specifier = NULL_TREE;
11478 /* Look for an (optional) storage-class-specifier or
11479 function-specifier. */
11480 if (cp_parser_allow_gnu_extensions_p (parser))
11482 extension_specifier
11483 = cp_parser_storage_class_specifier_opt (parser);
11484 if (!extension_specifier)
11485 extension_specifier
11486 = cp_parser_function_specifier_opt (parser,
11487 /*decl_specs=*/NULL);
11490 /* Look for the `template' keyword. */
11491 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11492 /* Let the front end know that we are processing an explicit
11494 begin_explicit_instantiation ();
11495 /* [temp.explicit] says that we are supposed to ignore access
11496 control while processing explicit instantiation directives. */
11497 push_deferring_access_checks (dk_no_check);
11498 /* Parse a decl-specifier-seq. */
11499 token = cp_lexer_peek_token (parser->lexer);
11500 cp_parser_decl_specifier_seq (parser,
11501 CP_PARSER_FLAGS_OPTIONAL,
11503 &declares_class_or_enum);
11504 /* If there was exactly one decl-specifier, and it declared a class,
11505 and there's no declarator, then we have an explicit type
11507 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11511 type = check_tag_decl (&decl_specifiers);
11512 /* Turn access control back on for names used during
11513 template instantiation. */
11514 pop_deferring_access_checks ();
11516 do_type_instantiation (type, extension_specifier,
11517 /*complain=*/tf_error);
11521 cp_declarator *declarator;
11524 /* Parse the declarator. */
11526 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11527 /*ctor_dtor_or_conv_p=*/NULL,
11528 /*parenthesized_p=*/NULL,
11529 /*member_p=*/false);
11530 if (declares_class_or_enum & 2)
11531 cp_parser_check_for_definition_in_return_type (declarator,
11532 decl_specifiers.type,
11533 decl_specifiers.type_location);
11534 if (declarator != cp_error_declarator)
11536 decl = grokdeclarator (declarator, &decl_specifiers,
11537 NORMAL, 0, &decl_specifiers.attributes);
11538 /* Turn access control back on for names used during
11539 template instantiation. */
11540 pop_deferring_access_checks ();
11541 /* Do the explicit instantiation. */
11542 do_decl_instantiation (decl, extension_specifier);
11546 pop_deferring_access_checks ();
11547 /* Skip the body of the explicit instantiation. */
11548 cp_parser_skip_to_end_of_statement (parser);
11551 /* We're done with the instantiation. */
11552 end_explicit_instantiation ();
11554 cp_parser_consume_semicolon_at_end_of_statement (parser);
11557 /* Parse an explicit-specialization.
11559 explicit-specialization:
11560 template < > declaration
11562 Although the standard says `declaration', what it really means is:
11564 explicit-specialization:
11565 template <> decl-specifier [opt] init-declarator [opt] ;
11566 template <> function-definition
11567 template <> explicit-specialization
11568 template <> template-declaration */
11571 cp_parser_explicit_specialization (cp_parser* parser)
11573 bool need_lang_pop;
11574 cp_token *token = cp_lexer_peek_token (parser->lexer);
11576 /* Look for the `template' keyword. */
11577 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11578 /* Look for the `<'. */
11579 cp_parser_require (parser, CPP_LESS, "%<<%>");
11580 /* Look for the `>'. */
11581 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11582 /* We have processed another parameter list. */
11583 ++parser->num_template_parameter_lists;
11586 A template ... explicit specialization ... shall not have C
11588 if (current_lang_name == lang_name_c)
11590 error_at (token->location, "template specialization with C linkage");
11591 /* Give it C++ linkage to avoid confusing other parts of the
11593 push_lang_context (lang_name_cplusplus);
11594 need_lang_pop = true;
11597 need_lang_pop = false;
11598 /* Let the front end know that we are beginning a specialization. */
11599 if (!begin_specialization ())
11601 end_specialization ();
11605 /* If the next keyword is `template', we need to figure out whether
11606 or not we're looking a template-declaration. */
11607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11609 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11610 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11611 cp_parser_template_declaration_after_export (parser,
11612 /*member_p=*/false);
11614 cp_parser_explicit_specialization (parser);
11617 /* Parse the dependent declaration. */
11618 cp_parser_single_declaration (parser,
11620 /*member_p=*/false,
11621 /*explicit_specialization_p=*/true,
11622 /*friend_p=*/NULL);
11623 /* We're done with the specialization. */
11624 end_specialization ();
11625 /* For the erroneous case of a template with C linkage, we pushed an
11626 implicit C++ linkage scope; exit that scope now. */
11628 pop_lang_context ();
11629 /* We're done with this parameter list. */
11630 --parser->num_template_parameter_lists;
11633 /* Parse a type-specifier.
11636 simple-type-specifier
11639 elaborated-type-specifier
11647 Returns a representation of the type-specifier. For a
11648 class-specifier, enum-specifier, or elaborated-type-specifier, a
11649 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11651 The parser flags FLAGS is used to control type-specifier parsing.
11653 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11654 in a decl-specifier-seq.
11656 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11657 class-specifier, enum-specifier, or elaborated-type-specifier, then
11658 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11659 if a type is declared; 2 if it is defined. Otherwise, it is set to
11662 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11663 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11664 is set to FALSE. */
11667 cp_parser_type_specifier (cp_parser* parser,
11668 cp_parser_flags flags,
11669 cp_decl_specifier_seq *decl_specs,
11670 bool is_declaration,
11671 int* declares_class_or_enum,
11672 bool* is_cv_qualifier)
11674 tree type_spec = NULL_TREE;
11677 cp_decl_spec ds = ds_last;
11679 /* Assume this type-specifier does not declare a new type. */
11680 if (declares_class_or_enum)
11681 *declares_class_or_enum = 0;
11682 /* And that it does not specify a cv-qualifier. */
11683 if (is_cv_qualifier)
11684 *is_cv_qualifier = false;
11685 /* Peek at the next token. */
11686 token = cp_lexer_peek_token (parser->lexer);
11688 /* If we're looking at a keyword, we can use that to guide the
11689 production we choose. */
11690 keyword = token->keyword;
11694 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11695 goto elaborated_type_specifier;
11697 /* Look for the enum-specifier. */
11698 type_spec = cp_parser_enum_specifier (parser);
11699 /* If that worked, we're done. */
11702 if (declares_class_or_enum)
11703 *declares_class_or_enum = 2;
11705 cp_parser_set_decl_spec_type (decl_specs,
11708 /*user_defined_p=*/true);
11712 goto elaborated_type_specifier;
11714 /* Any of these indicate either a class-specifier, or an
11715 elaborated-type-specifier. */
11719 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11720 goto elaborated_type_specifier;
11722 /* Parse tentatively so that we can back up if we don't find a
11723 class-specifier. */
11724 cp_parser_parse_tentatively (parser);
11725 /* Look for the class-specifier. */
11726 type_spec = cp_parser_class_specifier (parser);
11727 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11728 /* If that worked, we're done. */
11729 if (cp_parser_parse_definitely (parser))
11731 if (declares_class_or_enum)
11732 *declares_class_or_enum = 2;
11734 cp_parser_set_decl_spec_type (decl_specs,
11737 /*user_defined_p=*/true);
11741 /* Fall through. */
11742 elaborated_type_specifier:
11743 /* We're declaring (not defining) a class or enum. */
11744 if (declares_class_or_enum)
11745 *declares_class_or_enum = 1;
11747 /* Fall through. */
11749 /* Look for an elaborated-type-specifier. */
11751 = (cp_parser_elaborated_type_specifier
11753 decl_specs && decl_specs->specs[(int) ds_friend],
11756 cp_parser_set_decl_spec_type (decl_specs,
11759 /*user_defined_p=*/true);
11764 if (is_cv_qualifier)
11765 *is_cv_qualifier = true;
11770 if (is_cv_qualifier)
11771 *is_cv_qualifier = true;
11776 if (is_cv_qualifier)
11777 *is_cv_qualifier = true;
11781 /* The `__complex__' keyword is a GNU extension. */
11789 /* Handle simple keywords. */
11794 ++decl_specs->specs[(int)ds];
11795 decl_specs->any_specifiers_p = true;
11797 return cp_lexer_consume_token (parser->lexer)->u.value;
11800 /* If we do not already have a type-specifier, assume we are looking
11801 at a simple-type-specifier. */
11802 type_spec = cp_parser_simple_type_specifier (parser,
11806 /* If we didn't find a type-specifier, and a type-specifier was not
11807 optional in this context, issue an error message. */
11808 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11810 cp_parser_error (parser, "expected type specifier");
11811 return error_mark_node;
11817 /* Parse a simple-type-specifier.
11819 simple-type-specifier:
11820 :: [opt] nested-name-specifier [opt] type-name
11821 :: [opt] nested-name-specifier template template-id
11836 simple-type-specifier:
11838 decltype ( expression )
11844 simple-type-specifier:
11845 __typeof__ unary-expression
11846 __typeof__ ( type-id )
11848 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11849 appropriately updated. */
11852 cp_parser_simple_type_specifier (cp_parser* parser,
11853 cp_decl_specifier_seq *decl_specs,
11854 cp_parser_flags flags)
11856 tree type = NULL_TREE;
11859 /* Peek at the next token. */
11860 token = cp_lexer_peek_token (parser->lexer);
11862 /* If we're looking at a keyword, things are easy. */
11863 switch (token->keyword)
11867 decl_specs->explicit_char_p = true;
11868 type = char_type_node;
11871 type = char16_type_node;
11874 type = char32_type_node;
11877 type = wchar_type_node;
11880 type = boolean_type_node;
11884 ++decl_specs->specs[(int) ds_short];
11885 type = short_integer_type_node;
11889 decl_specs->explicit_int_p = true;
11890 type = integer_type_node;
11894 ++decl_specs->specs[(int) ds_long];
11895 type = long_integer_type_node;
11899 ++decl_specs->specs[(int) ds_signed];
11900 type = integer_type_node;
11904 ++decl_specs->specs[(int) ds_unsigned];
11905 type = unsigned_type_node;
11908 type = float_type_node;
11911 type = double_type_node;
11914 type = void_type_node;
11918 maybe_warn_cpp0x ("C++0x auto");
11919 type = make_auto ();
11923 /* Parse the `decltype' type. */
11924 type = cp_parser_decltype (parser);
11927 cp_parser_set_decl_spec_type (decl_specs, type,
11929 /*user_defined_p=*/true);
11934 /* Consume the `typeof' token. */
11935 cp_lexer_consume_token (parser->lexer);
11936 /* Parse the operand to `typeof'. */
11937 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11938 /* If it is not already a TYPE, take its type. */
11939 if (!TYPE_P (type))
11940 type = finish_typeof (type);
11943 cp_parser_set_decl_spec_type (decl_specs, type,
11945 /*user_defined_p=*/true);
11953 /* If the type-specifier was for a built-in type, we're done. */
11958 /* Record the type. */
11960 && (token->keyword != RID_SIGNED
11961 && token->keyword != RID_UNSIGNED
11962 && token->keyword != RID_SHORT
11963 && token->keyword != RID_LONG))
11964 cp_parser_set_decl_spec_type (decl_specs,
11967 /*user_defined=*/false);
11969 decl_specs->any_specifiers_p = true;
11971 /* Consume the token. */
11972 id = cp_lexer_consume_token (parser->lexer)->u.value;
11974 /* There is no valid C++ program where a non-template type is
11975 followed by a "<". That usually indicates that the user thought
11976 that the type was a template. */
11977 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11979 return TYPE_NAME (type);
11982 /* The type-specifier must be a user-defined type. */
11983 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11988 /* Don't gobble tokens or issue error messages if this is an
11989 optional type-specifier. */
11990 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11991 cp_parser_parse_tentatively (parser);
11993 /* Look for the optional `::' operator. */
11995 = (cp_parser_global_scope_opt (parser,
11996 /*current_scope_valid_p=*/false)
11998 /* Look for the nested-name specifier. */
12000 = (cp_parser_nested_name_specifier_opt (parser,
12001 /*typename_keyword_p=*/false,
12002 /*check_dependency_p=*/true,
12004 /*is_declaration=*/false)
12006 token = cp_lexer_peek_token (parser->lexer);
12007 /* If we have seen a nested-name-specifier, and the next token
12008 is `template', then we are using the template-id production. */
12010 && cp_parser_optional_template_keyword (parser))
12012 /* Look for the template-id. */
12013 type = cp_parser_template_id (parser,
12014 /*template_keyword_p=*/true,
12015 /*check_dependency_p=*/true,
12016 /*is_declaration=*/false);
12017 /* If the template-id did not name a type, we are out of
12019 if (TREE_CODE (type) != TYPE_DECL)
12021 cp_parser_error (parser, "expected template-id for type");
12025 /* Otherwise, look for a type-name. */
12027 type = cp_parser_type_name (parser);
12028 /* Keep track of all name-lookups performed in class scopes. */
12032 && TREE_CODE (type) == TYPE_DECL
12033 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12034 maybe_note_name_used_in_class (DECL_NAME (type), type);
12035 /* If it didn't work out, we don't have a TYPE. */
12036 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12037 && !cp_parser_parse_definitely (parser))
12039 if (type && decl_specs)
12040 cp_parser_set_decl_spec_type (decl_specs, type,
12042 /*user_defined=*/true);
12045 /* If we didn't get a type-name, issue an error message. */
12046 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12048 cp_parser_error (parser, "expected type-name");
12049 return error_mark_node;
12052 /* There is no valid C++ program where a non-template type is
12053 followed by a "<". That usually indicates that the user thought
12054 that the type was a template. */
12055 if (type && type != error_mark_node)
12057 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12058 If it is, then the '<'...'>' enclose protocol names rather than
12059 template arguments, and so everything is fine. */
12060 if (c_dialect_objc ()
12061 && (objc_is_id (type) || objc_is_class_name (type)))
12063 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12064 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12066 /* Clobber the "unqualified" type previously entered into
12067 DECL_SPECS with the new, improved protocol-qualified version. */
12069 decl_specs->type = qual_type;
12074 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12081 /* Parse a type-name.
12094 Returns a TYPE_DECL for the type. */
12097 cp_parser_type_name (cp_parser* parser)
12101 /* We can't know yet whether it is a class-name or not. */
12102 cp_parser_parse_tentatively (parser);
12103 /* Try a class-name. */
12104 type_decl = cp_parser_class_name (parser,
12105 /*typename_keyword_p=*/false,
12106 /*template_keyword_p=*/false,
12108 /*check_dependency_p=*/true,
12109 /*class_head_p=*/false,
12110 /*is_declaration=*/false);
12111 /* If it's not a class-name, keep looking. */
12112 if (!cp_parser_parse_definitely (parser))
12114 /* It must be a typedef-name or an enum-name. */
12115 return cp_parser_nonclass_name (parser);
12121 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12129 Returns a TYPE_DECL for the type. */
12132 cp_parser_nonclass_name (cp_parser* parser)
12137 cp_token *token = cp_lexer_peek_token (parser->lexer);
12138 identifier = cp_parser_identifier (parser);
12139 if (identifier == error_mark_node)
12140 return error_mark_node;
12142 /* Look up the type-name. */
12143 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12145 if (TREE_CODE (type_decl) != TYPE_DECL
12146 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12148 /* See if this is an Objective-C type. */
12149 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12150 tree type = objc_get_protocol_qualified_type (identifier, protos);
12152 type_decl = TYPE_NAME (type);
12155 /* Issue an error if we did not find a type-name. */
12156 if (TREE_CODE (type_decl) != TYPE_DECL)
12158 if (!cp_parser_simulate_error (parser))
12159 cp_parser_name_lookup_error (parser, identifier, type_decl,
12160 "is not a type", token->location);
12161 return error_mark_node;
12163 /* Remember that the name was used in the definition of the
12164 current class so that we can check later to see if the
12165 meaning would have been different after the class was
12166 entirely defined. */
12167 else if (type_decl != error_mark_node
12169 maybe_note_name_used_in_class (identifier, type_decl);
12174 /* Parse an elaborated-type-specifier. Note that the grammar given
12175 here incorporates the resolution to DR68.
12177 elaborated-type-specifier:
12178 class-key :: [opt] nested-name-specifier [opt] identifier
12179 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12180 enum-key :: [opt] nested-name-specifier [opt] identifier
12181 typename :: [opt] nested-name-specifier identifier
12182 typename :: [opt] nested-name-specifier template [opt]
12187 elaborated-type-specifier:
12188 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12189 class-key attributes :: [opt] nested-name-specifier [opt]
12190 template [opt] template-id
12191 enum attributes :: [opt] nested-name-specifier [opt] identifier
12193 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12194 declared `friend'. If IS_DECLARATION is TRUE, then this
12195 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12196 something is being declared.
12198 Returns the TYPE specified. */
12201 cp_parser_elaborated_type_specifier (cp_parser* parser,
12203 bool is_declaration)
12205 enum tag_types tag_type;
12207 tree type = NULL_TREE;
12208 tree attributes = NULL_TREE;
12210 cp_token *token = NULL;
12212 /* See if we're looking at the `enum' keyword. */
12213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12215 /* Consume the `enum' token. */
12216 cp_lexer_consume_token (parser->lexer);
12217 /* Remember that it's an enumeration type. */
12218 tag_type = enum_type;
12219 /* Parse the optional `struct' or `class' key (for C++0x scoped
12221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12222 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12224 if (cxx_dialect == cxx98)
12225 maybe_warn_cpp0x ("scoped enums");
12227 /* Consume the `struct' or `class'. */
12228 cp_lexer_consume_token (parser->lexer);
12230 /* Parse the attributes. */
12231 attributes = cp_parser_attributes_opt (parser);
12233 /* Or, it might be `typename'. */
12234 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12237 /* Consume the `typename' token. */
12238 cp_lexer_consume_token (parser->lexer);
12239 /* Remember that it's a `typename' type. */
12240 tag_type = typename_type;
12242 /* Otherwise it must be a class-key. */
12245 tag_type = cp_parser_class_key (parser);
12246 if (tag_type == none_type)
12247 return error_mark_node;
12248 /* Parse the attributes. */
12249 attributes = cp_parser_attributes_opt (parser);
12252 /* Look for the `::' operator. */
12253 globalscope = cp_parser_global_scope_opt (parser,
12254 /*current_scope_valid_p=*/false);
12255 /* Look for the nested-name-specifier. */
12256 if (tag_type == typename_type && !globalscope)
12258 if (!cp_parser_nested_name_specifier (parser,
12259 /*typename_keyword_p=*/true,
12260 /*check_dependency_p=*/true,
12263 return error_mark_node;
12266 /* Even though `typename' is not present, the proposed resolution
12267 to Core Issue 180 says that in `class A<T>::B', `B' should be
12268 considered a type-name, even if `A<T>' is dependent. */
12269 cp_parser_nested_name_specifier_opt (parser,
12270 /*typename_keyword_p=*/true,
12271 /*check_dependency_p=*/true,
12274 /* For everything but enumeration types, consider a template-id.
12275 For an enumeration type, consider only a plain identifier. */
12276 if (tag_type != enum_type)
12278 bool template_p = false;
12281 /* Allow the `template' keyword. */
12282 template_p = cp_parser_optional_template_keyword (parser);
12283 /* If we didn't see `template', we don't know if there's a
12284 template-id or not. */
12286 cp_parser_parse_tentatively (parser);
12287 /* Parse the template-id. */
12288 token = cp_lexer_peek_token (parser->lexer);
12289 decl = cp_parser_template_id (parser, template_p,
12290 /*check_dependency_p=*/true,
12292 /* If we didn't find a template-id, look for an ordinary
12294 if (!template_p && !cp_parser_parse_definitely (parser))
12296 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12297 in effect, then we must assume that, upon instantiation, the
12298 template will correspond to a class. */
12299 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12300 && tag_type == typename_type)
12301 type = make_typename_type (parser->scope, decl,
12303 /*complain=*/tf_error);
12304 /* If the `typename' keyword is in effect and DECL is not a type
12305 decl. Then type is non existant. */
12306 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12309 type = TREE_TYPE (decl);
12314 token = cp_lexer_peek_token (parser->lexer);
12315 identifier = cp_parser_identifier (parser);
12317 if (identifier == error_mark_node)
12319 parser->scope = NULL_TREE;
12320 return error_mark_node;
12323 /* For a `typename', we needn't call xref_tag. */
12324 if (tag_type == typename_type
12325 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12326 return cp_parser_make_typename_type (parser, parser->scope,
12329 /* Look up a qualified name in the usual way. */
12333 tree ambiguous_decls;
12335 decl = cp_parser_lookup_name (parser, identifier,
12337 /*is_template=*/false,
12338 /*is_namespace=*/false,
12339 /*check_dependency=*/true,
12343 /* If the lookup was ambiguous, an error will already have been
12345 if (ambiguous_decls)
12346 return error_mark_node;
12348 /* If we are parsing friend declaration, DECL may be a
12349 TEMPLATE_DECL tree node here. However, we need to check
12350 whether this TEMPLATE_DECL results in valid code. Consider
12351 the following example:
12354 template <class T> class C {};
12357 template <class T> friend class N::C; // #1, valid code
12359 template <class T> class Y {
12360 friend class N::C; // #2, invalid code
12363 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12364 name lookup of `N::C'. We see that friend declaration must
12365 be template for the code to be valid. Note that
12366 processing_template_decl does not work here since it is
12367 always 1 for the above two cases. */
12369 decl = (cp_parser_maybe_treat_template_as_class
12370 (decl, /*tag_name_p=*/is_friend
12371 && parser->num_template_parameter_lists));
12373 if (TREE_CODE (decl) != TYPE_DECL)
12375 cp_parser_diagnose_invalid_type_name (parser,
12379 return error_mark_node;
12382 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12384 bool allow_template = (parser->num_template_parameter_lists
12385 || DECL_SELF_REFERENCE_P (decl));
12386 type = check_elaborated_type_specifier (tag_type, decl,
12389 if (type == error_mark_node)
12390 return error_mark_node;
12393 /* Forward declarations of nested types, such as
12398 are invalid unless all components preceding the final '::'
12399 are complete. If all enclosing types are complete, these
12400 declarations become merely pointless.
12402 Invalid forward declarations of nested types are errors
12403 caught elsewhere in parsing. Those that are pointless arrive
12406 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12407 && !is_friend && !processing_explicit_instantiation)
12408 warning (0, "declaration %qD does not declare anything", decl);
12410 type = TREE_TYPE (decl);
12414 /* An elaborated-type-specifier sometimes introduces a new type and
12415 sometimes names an existing type. Normally, the rule is that it
12416 introduces a new type only if there is not an existing type of
12417 the same name already in scope. For example, given:
12420 void f() { struct S s; }
12422 the `struct S' in the body of `f' is the same `struct S' as in
12423 the global scope; the existing definition is used. However, if
12424 there were no global declaration, this would introduce a new
12425 local class named `S'.
12427 An exception to this rule applies to the following code:
12429 namespace N { struct S; }
12431 Here, the elaborated-type-specifier names a new type
12432 unconditionally; even if there is already an `S' in the
12433 containing scope this declaration names a new type.
12434 This exception only applies if the elaborated-type-specifier
12435 forms the complete declaration:
12439 A declaration consisting solely of `class-key identifier ;' is
12440 either a redeclaration of the name in the current scope or a
12441 forward declaration of the identifier as a class name. It
12442 introduces the name into the current scope.
12444 We are in this situation precisely when the next token is a `;'.
12446 An exception to the exception is that a `friend' declaration does
12447 *not* name a new type; i.e., given:
12449 struct S { friend struct T; };
12451 `T' is not a new type in the scope of `S'.
12453 Also, `new struct S' or `sizeof (struct S)' never results in the
12454 definition of a new type; a new type can only be declared in a
12455 declaration context. */
12461 /* Friends have special name lookup rules. */
12462 ts = ts_within_enclosing_non_class;
12463 else if (is_declaration
12464 && cp_lexer_next_token_is (parser->lexer,
12466 /* This is a `class-key identifier ;' */
12472 (parser->num_template_parameter_lists
12473 && (cp_parser_next_token_starts_class_definition_p (parser)
12474 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12475 /* An unqualified name was used to reference this type, so
12476 there were no qualifying templates. */
12477 if (!cp_parser_check_template_parameters (parser,
12478 /*num_templates=*/0,
12480 /*declarator=*/NULL))
12481 return error_mark_node;
12482 type = xref_tag (tag_type, identifier, ts, template_p);
12486 if (type == error_mark_node)
12487 return error_mark_node;
12489 /* Allow attributes on forward declarations of classes. */
12492 if (TREE_CODE (type) == TYPENAME_TYPE)
12493 warning (OPT_Wattributes,
12494 "attributes ignored on uninstantiated type");
12495 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12496 && ! processing_explicit_instantiation)
12497 warning (OPT_Wattributes,
12498 "attributes ignored on template instantiation");
12499 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12500 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12502 warning (OPT_Wattributes,
12503 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12506 if (tag_type != enum_type)
12507 cp_parser_check_class_key (tag_type, type);
12509 /* A "<" cannot follow an elaborated type specifier. If that
12510 happens, the user was probably trying to form a template-id. */
12511 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12516 /* Parse an enum-specifier.
12519 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12524 enum struct [C++0x]
12527 : type-specifier-seq
12530 enum-key attributes[opt] identifier [opt] enum-base [opt]
12531 { enumerator-list [opt] }attributes[opt]
12533 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12534 if the token stream isn't an enum-specifier after all. */
12537 cp_parser_enum_specifier (cp_parser* parser)
12542 bool scoped_enum_p = false;
12543 bool has_underlying_type = false;
12544 tree underlying_type = NULL_TREE;
12546 /* Parse tentatively so that we can back up if we don't find a
12548 cp_parser_parse_tentatively (parser);
12550 /* Caller guarantees that the current token is 'enum', an identifier
12551 possibly follows, and the token after that is an opening brace.
12552 If we don't have an identifier, fabricate an anonymous name for
12553 the enumeration being defined. */
12554 cp_lexer_consume_token (parser->lexer);
12556 /* Parse the "class" or "struct", which indicates a scoped
12557 enumeration type in C++0x. */
12558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12559 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12561 if (cxx_dialect == cxx98)
12562 maybe_warn_cpp0x ("scoped enums");
12564 /* Consume the `struct' or `class' token. */
12565 cp_lexer_consume_token (parser->lexer);
12567 scoped_enum_p = true;
12570 attributes = cp_parser_attributes_opt (parser);
12572 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12573 identifier = cp_parser_identifier (parser);
12575 identifier = make_anon_name ();
12577 /* Check for the `:' that denotes a specified underlying type in C++0x.
12578 Note that a ':' could also indicate a bitfield width, however. */
12579 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12581 cp_decl_specifier_seq type_specifiers;
12583 /* Consume the `:'. */
12584 cp_lexer_consume_token (parser->lexer);
12586 /* Parse the type-specifier-seq. */
12587 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12588 /*is_trailing_return=*/false,
12591 /* At this point this is surely not elaborated type specifier. */
12592 if (!cp_parser_parse_definitely (parser))
12595 if (cxx_dialect == cxx98)
12596 maybe_warn_cpp0x ("scoped enums");
12598 has_underlying_type = true;
12600 /* If that didn't work, stop. */
12601 if (type_specifiers.type != error_mark_node)
12603 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12604 /*initialized=*/0, NULL);
12605 if (underlying_type == error_mark_node)
12606 underlying_type = NULL_TREE;
12610 /* Look for the `{' but don't consume it yet. */
12611 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12613 cp_parser_error (parser, "expected %<{%>");
12614 if (has_underlying_type)
12618 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12621 /* Issue an error message if type-definitions are forbidden here. */
12622 if (!cp_parser_check_type_definition (parser))
12623 type = error_mark_node;
12625 /* Create the new type. We do this before consuming the opening
12626 brace so the enum will be recorded as being on the line of its
12627 tag (or the 'enum' keyword, if there is no tag). */
12628 type = start_enum (identifier, underlying_type, scoped_enum_p);
12630 /* Consume the opening brace. */
12631 cp_lexer_consume_token (parser->lexer);
12633 if (type == error_mark_node)
12635 cp_parser_skip_to_end_of_block_or_statement (parser);
12636 return error_mark_node;
12639 /* If the next token is not '}', then there are some enumerators. */
12640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12641 cp_parser_enumerator_list (parser, type);
12643 /* Consume the final '}'. */
12644 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12646 /* Look for trailing attributes to apply to this enumeration, and
12647 apply them if appropriate. */
12648 if (cp_parser_allow_gnu_extensions_p (parser))
12650 tree trailing_attr = cp_parser_attributes_opt (parser);
12651 trailing_attr = chainon (trailing_attr, attributes);
12652 cplus_decl_attributes (&type,
12654 (int) ATTR_FLAG_TYPE_IN_PLACE);
12657 /* Finish up the enumeration. */
12658 finish_enum (type);
12663 /* Parse an enumerator-list. The enumerators all have the indicated
12667 enumerator-definition
12668 enumerator-list , enumerator-definition */
12671 cp_parser_enumerator_list (cp_parser* parser, tree type)
12675 /* Parse an enumerator-definition. */
12676 cp_parser_enumerator_definition (parser, type);
12678 /* If the next token is not a ',', we've reached the end of
12680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12682 /* Otherwise, consume the `,' and keep going. */
12683 cp_lexer_consume_token (parser->lexer);
12684 /* If the next token is a `}', there is a trailing comma. */
12685 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12687 if (!in_system_header)
12688 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12694 /* Parse an enumerator-definition. The enumerator has the indicated
12697 enumerator-definition:
12699 enumerator = constant-expression
12705 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12710 /* Look for the identifier. */
12711 identifier = cp_parser_identifier (parser);
12712 if (identifier == error_mark_node)
12715 /* If the next token is an '=', then there is an explicit value. */
12716 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12718 /* Consume the `=' token. */
12719 cp_lexer_consume_token (parser->lexer);
12720 /* Parse the value. */
12721 value = cp_parser_constant_expression (parser,
12722 /*allow_non_constant_p=*/false,
12728 /* If we are processing a template, make sure the initializer of the
12729 enumerator doesn't contain any bare template parameter pack. */
12730 if (check_for_bare_parameter_packs (value))
12731 value = error_mark_node;
12733 /* Create the enumerator. */
12734 build_enumerator (identifier, value, type);
12737 /* Parse a namespace-name.
12740 original-namespace-name
12743 Returns the NAMESPACE_DECL for the namespace. */
12746 cp_parser_namespace_name (cp_parser* parser)
12749 tree namespace_decl;
12751 cp_token *token = cp_lexer_peek_token (parser->lexer);
12753 /* Get the name of the namespace. */
12754 identifier = cp_parser_identifier (parser);
12755 if (identifier == error_mark_node)
12756 return error_mark_node;
12758 /* Look up the identifier in the currently active scope. Look only
12759 for namespaces, due to:
12761 [basic.lookup.udir]
12763 When looking up a namespace-name in a using-directive or alias
12764 definition, only namespace names are considered.
12768 [basic.lookup.qual]
12770 During the lookup of a name preceding the :: scope resolution
12771 operator, object, function, and enumerator names are ignored.
12773 (Note that cp_parser_qualifying_entity only calls this
12774 function if the token after the name is the scope resolution
12776 namespace_decl = cp_parser_lookup_name (parser, identifier,
12778 /*is_template=*/false,
12779 /*is_namespace=*/true,
12780 /*check_dependency=*/true,
12781 /*ambiguous_decls=*/NULL,
12783 /* If it's not a namespace, issue an error. */
12784 if (namespace_decl == error_mark_node
12785 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12787 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12788 error_at (token->location, "%qD is not a namespace-name", identifier);
12789 cp_parser_error (parser, "expected namespace-name");
12790 namespace_decl = error_mark_node;
12793 return namespace_decl;
12796 /* Parse a namespace-definition.
12798 namespace-definition:
12799 named-namespace-definition
12800 unnamed-namespace-definition
12802 named-namespace-definition:
12803 original-namespace-definition
12804 extension-namespace-definition
12806 original-namespace-definition:
12807 namespace identifier { namespace-body }
12809 extension-namespace-definition:
12810 namespace original-namespace-name { namespace-body }
12812 unnamed-namespace-definition:
12813 namespace { namespace-body } */
12816 cp_parser_namespace_definition (cp_parser* parser)
12818 tree identifier, attribs;
12819 bool has_visibility;
12822 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12825 cp_lexer_consume_token (parser->lexer);
12830 /* Look for the `namespace' keyword. */
12831 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12833 /* Get the name of the namespace. We do not attempt to distinguish
12834 between an original-namespace-definition and an
12835 extension-namespace-definition at this point. The semantic
12836 analysis routines are responsible for that. */
12837 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12838 identifier = cp_parser_identifier (parser);
12840 identifier = NULL_TREE;
12842 /* Parse any specified attributes. */
12843 attribs = cp_parser_attributes_opt (parser);
12845 /* Look for the `{' to start the namespace. */
12846 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12847 /* Start the namespace. */
12848 push_namespace (identifier);
12850 /* "inline namespace" is equivalent to a stub namespace definition
12851 followed by a strong using directive. */
12854 tree name_space = current_namespace;
12855 /* Set up namespace association. */
12856 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12857 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12858 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12859 /* Import the contents of the inline namespace. */
12861 do_using_directive (name_space);
12862 push_namespace (identifier);
12865 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12867 /* Parse the body of the namespace. */
12868 cp_parser_namespace_body (parser);
12870 #ifdef HANDLE_PRAGMA_VISIBILITY
12871 if (has_visibility)
12872 pop_visibility (1);
12875 /* Finish the namespace. */
12877 /* Look for the final `}'. */
12878 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12881 /* Parse a namespace-body.
12884 declaration-seq [opt] */
12887 cp_parser_namespace_body (cp_parser* parser)
12889 cp_parser_declaration_seq_opt (parser);
12892 /* Parse a namespace-alias-definition.
12894 namespace-alias-definition:
12895 namespace identifier = qualified-namespace-specifier ; */
12898 cp_parser_namespace_alias_definition (cp_parser* parser)
12901 tree namespace_specifier;
12903 cp_token *token = cp_lexer_peek_token (parser->lexer);
12905 /* Look for the `namespace' keyword. */
12906 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12907 /* Look for the identifier. */
12908 identifier = cp_parser_identifier (parser);
12909 if (identifier == error_mark_node)
12911 /* Look for the `=' token. */
12912 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12913 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12915 error_at (token->location, "%<namespace%> definition is not allowed here");
12916 /* Skip the definition. */
12917 cp_lexer_consume_token (parser->lexer);
12918 if (cp_parser_skip_to_closing_brace (parser))
12919 cp_lexer_consume_token (parser->lexer);
12922 cp_parser_require (parser, CPP_EQ, "%<=%>");
12923 /* Look for the qualified-namespace-specifier. */
12924 namespace_specifier
12925 = cp_parser_qualified_namespace_specifier (parser);
12926 /* Look for the `;' token. */
12927 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12929 /* Register the alias in the symbol table. */
12930 do_namespace_alias (identifier, namespace_specifier);
12933 /* Parse a qualified-namespace-specifier.
12935 qualified-namespace-specifier:
12936 :: [opt] nested-name-specifier [opt] namespace-name
12938 Returns a NAMESPACE_DECL corresponding to the specified
12942 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12944 /* Look for the optional `::'. */
12945 cp_parser_global_scope_opt (parser,
12946 /*current_scope_valid_p=*/false);
12948 /* Look for the optional nested-name-specifier. */
12949 cp_parser_nested_name_specifier_opt (parser,
12950 /*typename_keyword_p=*/false,
12951 /*check_dependency_p=*/true,
12953 /*is_declaration=*/true);
12955 return cp_parser_namespace_name (parser);
12958 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12959 access declaration.
12962 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12963 using :: unqualified-id ;
12965 access-declaration:
12971 cp_parser_using_declaration (cp_parser* parser,
12972 bool access_declaration_p)
12975 bool typename_p = false;
12976 bool global_scope_p;
12981 if (access_declaration_p)
12982 cp_parser_parse_tentatively (parser);
12985 /* Look for the `using' keyword. */
12986 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12988 /* Peek at the next token. */
12989 token = cp_lexer_peek_token (parser->lexer);
12990 /* See if it's `typename'. */
12991 if (token->keyword == RID_TYPENAME)
12993 /* Remember that we've seen it. */
12995 /* Consume the `typename' token. */
12996 cp_lexer_consume_token (parser->lexer);
13000 /* Look for the optional global scope qualification. */
13002 = (cp_parser_global_scope_opt (parser,
13003 /*current_scope_valid_p=*/false)
13006 /* If we saw `typename', or didn't see `::', then there must be a
13007 nested-name-specifier present. */
13008 if (typename_p || !global_scope_p)
13009 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13010 /*check_dependency_p=*/true,
13012 /*is_declaration=*/true);
13013 /* Otherwise, we could be in either of the two productions. In that
13014 case, treat the nested-name-specifier as optional. */
13016 qscope = cp_parser_nested_name_specifier_opt (parser,
13017 /*typename_keyword_p=*/false,
13018 /*check_dependency_p=*/true,
13020 /*is_declaration=*/true);
13022 qscope = global_namespace;
13024 if (access_declaration_p && cp_parser_error_occurred (parser))
13025 /* Something has already gone wrong; there's no need to parse
13026 further. Since an error has occurred, the return value of
13027 cp_parser_parse_definitely will be false, as required. */
13028 return cp_parser_parse_definitely (parser);
13030 token = cp_lexer_peek_token (parser->lexer);
13031 /* Parse the unqualified-id. */
13032 identifier = cp_parser_unqualified_id (parser,
13033 /*template_keyword_p=*/false,
13034 /*check_dependency_p=*/true,
13035 /*declarator_p=*/true,
13036 /*optional_p=*/false);
13038 if (access_declaration_p)
13040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13041 cp_parser_simulate_error (parser);
13042 if (!cp_parser_parse_definitely (parser))
13046 /* The function we call to handle a using-declaration is different
13047 depending on what scope we are in. */
13048 if (qscope == error_mark_node || identifier == error_mark_node)
13050 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13051 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13052 /* [namespace.udecl]
13054 A using declaration shall not name a template-id. */
13055 error_at (token->location,
13056 "a template-id may not appear in a using-declaration");
13059 if (at_class_scope_p ())
13061 /* Create the USING_DECL. */
13062 decl = do_class_using_decl (parser->scope, identifier);
13064 if (check_for_bare_parameter_packs (decl))
13067 /* Add it to the list of members in this class. */
13068 finish_member_declaration (decl);
13072 decl = cp_parser_lookup_name_simple (parser,
13075 if (decl == error_mark_node)
13076 cp_parser_name_lookup_error (parser, identifier,
13079 else if (check_for_bare_parameter_packs (decl))
13081 else if (!at_namespace_scope_p ())
13082 do_local_using_decl (decl, qscope, identifier);
13084 do_toplevel_using_decl (decl, qscope, identifier);
13088 /* Look for the final `;'. */
13089 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13094 /* Parse a using-directive.
13097 using namespace :: [opt] nested-name-specifier [opt]
13098 namespace-name ; */
13101 cp_parser_using_directive (cp_parser* parser)
13103 tree namespace_decl;
13106 /* Look for the `using' keyword. */
13107 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13108 /* And the `namespace' keyword. */
13109 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13110 /* Look for the optional `::' operator. */
13111 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13112 /* And the optional nested-name-specifier. */
13113 cp_parser_nested_name_specifier_opt (parser,
13114 /*typename_keyword_p=*/false,
13115 /*check_dependency_p=*/true,
13117 /*is_declaration=*/true);
13118 /* Get the namespace being used. */
13119 namespace_decl = cp_parser_namespace_name (parser);
13120 /* And any specified attributes. */
13121 attribs = cp_parser_attributes_opt (parser);
13122 /* Update the symbol table. */
13123 parse_using_directive (namespace_decl, attribs);
13124 /* Look for the final `;'. */
13125 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13128 /* Parse an asm-definition.
13131 asm ( string-literal ) ;
13136 asm volatile [opt] ( string-literal ) ;
13137 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13138 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13139 : asm-operand-list [opt] ) ;
13140 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13141 : asm-operand-list [opt]
13142 : asm-clobber-list [opt] ) ;
13143 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13144 : asm-clobber-list [opt]
13145 : asm-goto-list ) ; */
13148 cp_parser_asm_definition (cp_parser* parser)
13151 tree outputs = NULL_TREE;
13152 tree inputs = NULL_TREE;
13153 tree clobbers = NULL_TREE;
13154 tree labels = NULL_TREE;
13156 bool volatile_p = false;
13157 bool extended_p = false;
13158 bool invalid_inputs_p = false;
13159 bool invalid_outputs_p = false;
13160 bool goto_p = false;
13161 const char *missing = NULL;
13163 /* Look for the `asm' keyword. */
13164 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13165 /* See if the next token is `volatile'. */
13166 if (cp_parser_allow_gnu_extensions_p (parser)
13167 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13169 /* Remember that we saw the `volatile' keyword. */
13171 /* Consume the token. */
13172 cp_lexer_consume_token (parser->lexer);
13174 if (cp_parser_allow_gnu_extensions_p (parser)
13175 && parser->in_function_body
13176 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13178 /* Remember that we saw the `goto' keyword. */
13180 /* Consume the token. */
13181 cp_lexer_consume_token (parser->lexer);
13183 /* Look for the opening `('. */
13184 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13186 /* Look for the string. */
13187 string = cp_parser_string_literal (parser, false, false);
13188 if (string == error_mark_node)
13190 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13191 /*consume_paren=*/true);
13195 /* If we're allowing GNU extensions, check for the extended assembly
13196 syntax. Unfortunately, the `:' tokens need not be separated by
13197 a space in C, and so, for compatibility, we tolerate that here
13198 too. Doing that means that we have to treat the `::' operator as
13200 if (cp_parser_allow_gnu_extensions_p (parser)
13201 && parser->in_function_body
13202 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13203 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13205 bool inputs_p = false;
13206 bool clobbers_p = false;
13207 bool labels_p = false;
13209 /* The extended syntax was used. */
13212 /* Look for outputs. */
13213 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13215 /* Consume the `:'. */
13216 cp_lexer_consume_token (parser->lexer);
13217 /* Parse the output-operands. */
13218 if (cp_lexer_next_token_is_not (parser->lexer,
13220 && cp_lexer_next_token_is_not (parser->lexer,
13222 && cp_lexer_next_token_is_not (parser->lexer,
13225 outputs = cp_parser_asm_operand_list (parser);
13227 if (outputs == error_mark_node)
13228 invalid_outputs_p = true;
13230 /* If the next token is `::', there are no outputs, and the
13231 next token is the beginning of the inputs. */
13232 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13233 /* The inputs are coming next. */
13236 /* Look for inputs. */
13238 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13240 /* Consume the `:' or `::'. */
13241 cp_lexer_consume_token (parser->lexer);
13242 /* Parse the output-operands. */
13243 if (cp_lexer_next_token_is_not (parser->lexer,
13245 && cp_lexer_next_token_is_not (parser->lexer,
13247 && cp_lexer_next_token_is_not (parser->lexer,
13249 inputs = cp_parser_asm_operand_list (parser);
13251 if (inputs == error_mark_node)
13252 invalid_inputs_p = true;
13254 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13255 /* The clobbers are coming next. */
13258 /* Look for clobbers. */
13260 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13263 /* Consume the `:' or `::'. */
13264 cp_lexer_consume_token (parser->lexer);
13265 /* Parse the clobbers. */
13266 if (cp_lexer_next_token_is_not (parser->lexer,
13268 && cp_lexer_next_token_is_not (parser->lexer,
13270 clobbers = cp_parser_asm_clobber_list (parser);
13273 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13274 /* The labels are coming next. */
13277 /* Look for labels. */
13279 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13282 /* Consume the `:' or `::'. */
13283 cp_lexer_consume_token (parser->lexer);
13284 /* Parse the labels. */
13285 labels = cp_parser_asm_label_list (parser);
13288 if (goto_p && !labels_p)
13289 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13292 missing = "%<:%> or %<::%>";
13294 /* Look for the closing `)'. */
13295 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13296 missing ? missing : "%<)%>"))
13297 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13298 /*consume_paren=*/true);
13299 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13301 if (!invalid_inputs_p && !invalid_outputs_p)
13303 /* Create the ASM_EXPR. */
13304 if (parser->in_function_body)
13306 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13307 inputs, clobbers, labels);
13308 /* If the extended syntax was not used, mark the ASM_EXPR. */
13311 tree temp = asm_stmt;
13312 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13313 temp = TREE_OPERAND (temp, 0);
13315 ASM_INPUT_P (temp) = 1;
13319 cgraph_add_asm_node (string);
13323 /* Declarators [gram.dcl.decl] */
13325 /* Parse an init-declarator.
13328 declarator initializer [opt]
13333 declarator asm-specification [opt] attributes [opt] initializer [opt]
13335 function-definition:
13336 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13338 decl-specifier-seq [opt] declarator function-try-block
13342 function-definition:
13343 __extension__ function-definition
13345 The DECL_SPECIFIERS apply to this declarator. Returns a
13346 representation of the entity declared. If MEMBER_P is TRUE, then
13347 this declarator appears in a class scope. The new DECL created by
13348 this declarator is returned.
13350 The CHECKS are access checks that should be performed once we know
13351 what entity is being declared (and, therefore, what classes have
13354 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13355 for a function-definition here as well. If the declarator is a
13356 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13357 be TRUE upon return. By that point, the function-definition will
13358 have been completely parsed.
13360 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13364 cp_parser_init_declarator (cp_parser* parser,
13365 cp_decl_specifier_seq *decl_specifiers,
13366 VEC (deferred_access_check,gc)* checks,
13367 bool function_definition_allowed_p,
13369 int declares_class_or_enum,
13370 bool* function_definition_p)
13372 cp_token *token = NULL, *asm_spec_start_token = NULL,
13373 *attributes_start_token = NULL;
13374 cp_declarator *declarator;
13375 tree prefix_attributes;
13377 tree asm_specification;
13379 tree decl = NULL_TREE;
13381 int is_initialized;
13382 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13383 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13385 enum cpp_ttype initialization_kind;
13386 bool is_direct_init = false;
13387 bool is_non_constant_init;
13388 int ctor_dtor_or_conv_p;
13390 tree pushed_scope = NULL;
13392 /* Gather the attributes that were provided with the
13393 decl-specifiers. */
13394 prefix_attributes = decl_specifiers->attributes;
13396 /* Assume that this is not the declarator for a function
13398 if (function_definition_p)
13399 *function_definition_p = false;
13401 /* Defer access checks while parsing the declarator; we cannot know
13402 what names are accessible until we know what is being
13404 resume_deferring_access_checks ();
13406 /* Parse the declarator. */
13407 token = cp_lexer_peek_token (parser->lexer);
13409 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13410 &ctor_dtor_or_conv_p,
13411 /*parenthesized_p=*/NULL,
13412 /*member_p=*/false);
13413 /* Gather up the deferred checks. */
13414 stop_deferring_access_checks ();
13416 /* If the DECLARATOR was erroneous, there's no need to go
13418 if (declarator == cp_error_declarator)
13419 return error_mark_node;
13421 /* Check that the number of template-parameter-lists is OK. */
13422 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13424 return error_mark_node;
13426 if (declares_class_or_enum & 2)
13427 cp_parser_check_for_definition_in_return_type (declarator,
13428 decl_specifiers->type,
13429 decl_specifiers->type_location);
13431 /* Figure out what scope the entity declared by the DECLARATOR is
13432 located in. `grokdeclarator' sometimes changes the scope, so
13433 we compute it now. */
13434 scope = get_scope_of_declarator (declarator);
13436 /* If we're allowing GNU extensions, look for an asm-specification
13438 if (cp_parser_allow_gnu_extensions_p (parser))
13440 /* Look for an asm-specification. */
13441 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13442 asm_specification = cp_parser_asm_specification_opt (parser);
13443 /* And attributes. */
13444 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13445 attributes = cp_parser_attributes_opt (parser);
13449 asm_specification = NULL_TREE;
13450 attributes = NULL_TREE;
13453 /* Peek at the next token. */
13454 token = cp_lexer_peek_token (parser->lexer);
13455 /* Check to see if the token indicates the start of a
13456 function-definition. */
13457 if (function_declarator_p (declarator)
13458 && cp_parser_token_starts_function_definition_p (token))
13460 if (!function_definition_allowed_p)
13462 /* If a function-definition should not appear here, issue an
13464 cp_parser_error (parser,
13465 "a function-definition is not allowed here");
13466 return error_mark_node;
13470 location_t func_brace_location
13471 = cp_lexer_peek_token (parser->lexer)->location;
13473 /* Neither attributes nor an asm-specification are allowed
13474 on a function-definition. */
13475 if (asm_specification)
13476 error_at (asm_spec_start_token->location,
13477 "an asm-specification is not allowed "
13478 "on a function-definition");
13480 error_at (attributes_start_token->location,
13481 "attributes are not allowed on a function-definition");
13482 /* This is a function-definition. */
13483 *function_definition_p = true;
13485 /* Parse the function definition. */
13487 decl = cp_parser_save_member_function_body (parser,
13490 prefix_attributes);
13493 = (cp_parser_function_definition_from_specifiers_and_declarator
13494 (parser, decl_specifiers, prefix_attributes, declarator));
13496 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13498 /* This is where the prologue starts... */
13499 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13500 = func_brace_location;
13509 Only in function declarations for constructors, destructors, and
13510 type conversions can the decl-specifier-seq be omitted.
13512 We explicitly postpone this check past the point where we handle
13513 function-definitions because we tolerate function-definitions
13514 that are missing their return types in some modes. */
13515 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13517 cp_parser_error (parser,
13518 "expected constructor, destructor, or type conversion");
13519 return error_mark_node;
13522 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13523 if (token->type == CPP_EQ
13524 || token->type == CPP_OPEN_PAREN
13525 || token->type == CPP_OPEN_BRACE)
13527 is_initialized = SD_INITIALIZED;
13528 initialization_kind = token->type;
13530 if (token->type == CPP_EQ
13531 && function_declarator_p (declarator))
13533 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13534 if (t2->keyword == RID_DEFAULT)
13535 is_initialized = SD_DEFAULTED;
13536 else if (t2->keyword == RID_DELETE)
13537 is_initialized = SD_DELETED;
13542 /* If the init-declarator isn't initialized and isn't followed by a
13543 `,' or `;', it's not a valid init-declarator. */
13544 if (token->type != CPP_COMMA
13545 && token->type != CPP_SEMICOLON)
13547 cp_parser_error (parser, "expected initializer");
13548 return error_mark_node;
13550 is_initialized = SD_UNINITIALIZED;
13551 initialization_kind = CPP_EOF;
13554 /* Because start_decl has side-effects, we should only call it if we
13555 know we're going ahead. By this point, we know that we cannot
13556 possibly be looking at any other construct. */
13557 cp_parser_commit_to_tentative_parse (parser);
13559 /* If the decl specifiers were bad, issue an error now that we're
13560 sure this was intended to be a declarator. Then continue
13561 declaring the variable(s), as int, to try to cut down on further
13563 if (decl_specifiers->any_specifiers_p
13564 && decl_specifiers->type == error_mark_node)
13566 cp_parser_error (parser, "invalid type in declaration");
13567 decl_specifiers->type = integer_type_node;
13570 /* Check to see whether or not this declaration is a friend. */
13571 friend_p = cp_parser_friend_p (decl_specifiers);
13573 /* Enter the newly declared entry in the symbol table. If we're
13574 processing a declaration in a class-specifier, we wait until
13575 after processing the initializer. */
13578 if (parser->in_unbraced_linkage_specification_p)
13579 decl_specifiers->storage_class = sc_extern;
13580 decl = start_decl (declarator, decl_specifiers,
13581 is_initialized, attributes, prefix_attributes,
13585 /* Enter the SCOPE. That way unqualified names appearing in the
13586 initializer will be looked up in SCOPE. */
13587 pushed_scope = push_scope (scope);
13589 /* Perform deferred access control checks, now that we know in which
13590 SCOPE the declared entity resides. */
13591 if (!member_p && decl)
13593 tree saved_current_function_decl = NULL_TREE;
13595 /* If the entity being declared is a function, pretend that we
13596 are in its scope. If it is a `friend', it may have access to
13597 things that would not otherwise be accessible. */
13598 if (TREE_CODE (decl) == FUNCTION_DECL)
13600 saved_current_function_decl = current_function_decl;
13601 current_function_decl = decl;
13604 /* Perform access checks for template parameters. */
13605 cp_parser_perform_template_parameter_access_checks (checks);
13607 /* Perform the access control checks for the declarator and the
13608 decl-specifiers. */
13609 perform_deferred_access_checks ();
13611 /* Restore the saved value. */
13612 if (TREE_CODE (decl) == FUNCTION_DECL)
13613 current_function_decl = saved_current_function_decl;
13616 /* Parse the initializer. */
13617 initializer = NULL_TREE;
13618 is_direct_init = false;
13619 is_non_constant_init = true;
13620 if (is_initialized)
13622 if (function_declarator_p (declarator))
13624 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13625 if (initialization_kind == CPP_EQ)
13626 initializer = cp_parser_pure_specifier (parser);
13629 /* If the declaration was erroneous, we don't really
13630 know what the user intended, so just silently
13631 consume the initializer. */
13632 if (decl != error_mark_node)
13633 error_at (initializer_start_token->location,
13634 "initializer provided for function");
13635 cp_parser_skip_to_closing_parenthesis (parser,
13636 /*recovering=*/true,
13637 /*or_comma=*/false,
13638 /*consume_paren=*/true);
13643 /* We want to record the extra mangling scope for in-class
13644 initializers of class members and initializers of static data
13645 member templates. The former is a C++0x feature which isn't
13646 implemented yet, and I expect it will involve deferring
13647 parsing of the initializer until end of class as with default
13648 arguments. So right here we only handle the latter. */
13649 if (!member_p && processing_template_decl)
13650 start_lambda_scope (decl);
13651 initializer = cp_parser_initializer (parser,
13653 &is_non_constant_init);
13654 if (!member_p && processing_template_decl)
13655 finish_lambda_scope ();
13659 /* The old parser allows attributes to appear after a parenthesized
13660 initializer. Mark Mitchell proposed removing this functionality
13661 on the GCC mailing lists on 2002-08-13. This parser accepts the
13662 attributes -- but ignores them. */
13663 if (cp_parser_allow_gnu_extensions_p (parser)
13664 && initialization_kind == CPP_OPEN_PAREN)
13665 if (cp_parser_attributes_opt (parser))
13666 warning (OPT_Wattributes,
13667 "attributes after parenthesized initializer ignored");
13669 /* For an in-class declaration, use `grokfield' to create the
13675 pop_scope (pushed_scope);
13676 pushed_scope = false;
13678 decl = grokfield (declarator, decl_specifiers,
13679 initializer, !is_non_constant_init,
13680 /*asmspec=*/NULL_TREE,
13681 prefix_attributes);
13682 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13683 cp_parser_save_default_args (parser, decl);
13686 /* Finish processing the declaration. But, skip friend
13688 if (!friend_p && decl && decl != error_mark_node)
13690 cp_finish_decl (decl,
13691 initializer, !is_non_constant_init,
13693 /* If the initializer is in parentheses, then this is
13694 a direct-initialization, which means that an
13695 `explicit' constructor is OK. Otherwise, an
13696 `explicit' constructor cannot be used. */
13697 ((is_direct_init || !is_initialized)
13698 ? 0 : LOOKUP_ONLYCONVERTING));
13700 else if ((cxx_dialect != cxx98) && friend_p
13701 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13702 /* Core issue #226 (C++0x only): A default template-argument
13703 shall not be specified in a friend class template
13705 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13706 /*is_partial=*/0, /*is_friend_decl=*/1);
13708 if (!friend_p && pushed_scope)
13709 pop_scope (pushed_scope);
13714 /* Parse a declarator.
13718 ptr-operator declarator
13720 abstract-declarator:
13721 ptr-operator abstract-declarator [opt]
13722 direct-abstract-declarator
13727 attributes [opt] direct-declarator
13728 attributes [opt] ptr-operator declarator
13730 abstract-declarator:
13731 attributes [opt] ptr-operator abstract-declarator [opt]
13732 attributes [opt] direct-abstract-declarator
13734 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13735 detect constructor, destructor or conversion operators. It is set
13736 to -1 if the declarator is a name, and +1 if it is a
13737 function. Otherwise it is set to zero. Usually you just want to
13738 test for >0, but internally the negative value is used.
13740 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13741 a decl-specifier-seq unless it declares a constructor, destructor,
13742 or conversion. It might seem that we could check this condition in
13743 semantic analysis, rather than parsing, but that makes it difficult
13744 to handle something like `f()'. We want to notice that there are
13745 no decl-specifiers, and therefore realize that this is an
13746 expression, not a declaration.)
13748 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13749 the declarator is a direct-declarator of the form "(...)".
13751 MEMBER_P is true iff this declarator is a member-declarator. */
13753 static cp_declarator *
13754 cp_parser_declarator (cp_parser* parser,
13755 cp_parser_declarator_kind dcl_kind,
13756 int* ctor_dtor_or_conv_p,
13757 bool* parenthesized_p,
13761 cp_declarator *declarator;
13762 enum tree_code code;
13763 cp_cv_quals cv_quals;
13765 tree attributes = NULL_TREE;
13767 /* Assume this is not a constructor, destructor, or type-conversion
13769 if (ctor_dtor_or_conv_p)
13770 *ctor_dtor_or_conv_p = 0;
13772 if (cp_parser_allow_gnu_extensions_p (parser))
13773 attributes = cp_parser_attributes_opt (parser);
13775 /* Peek at the next token. */
13776 token = cp_lexer_peek_token (parser->lexer);
13778 /* Check for the ptr-operator production. */
13779 cp_parser_parse_tentatively (parser);
13780 /* Parse the ptr-operator. */
13781 code = cp_parser_ptr_operator (parser,
13784 /* If that worked, then we have a ptr-operator. */
13785 if (cp_parser_parse_definitely (parser))
13787 /* If a ptr-operator was found, then this declarator was not
13789 if (parenthesized_p)
13790 *parenthesized_p = true;
13791 /* The dependent declarator is optional if we are parsing an
13792 abstract-declarator. */
13793 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13794 cp_parser_parse_tentatively (parser);
13796 /* Parse the dependent declarator. */
13797 declarator = cp_parser_declarator (parser, dcl_kind,
13798 /*ctor_dtor_or_conv_p=*/NULL,
13799 /*parenthesized_p=*/NULL,
13800 /*member_p=*/false);
13802 /* If we are parsing an abstract-declarator, we must handle the
13803 case where the dependent declarator is absent. */
13804 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13805 && !cp_parser_parse_definitely (parser))
13808 declarator = cp_parser_make_indirect_declarator
13809 (code, class_type, cv_quals, declarator);
13811 /* Everything else is a direct-declarator. */
13814 if (parenthesized_p)
13815 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13817 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13818 ctor_dtor_or_conv_p,
13822 if (attributes && declarator && declarator != cp_error_declarator)
13823 declarator->attributes = attributes;
13828 /* Parse a direct-declarator or direct-abstract-declarator.
13832 direct-declarator ( parameter-declaration-clause )
13833 cv-qualifier-seq [opt]
13834 exception-specification [opt]
13835 direct-declarator [ constant-expression [opt] ]
13838 direct-abstract-declarator:
13839 direct-abstract-declarator [opt]
13840 ( parameter-declaration-clause )
13841 cv-qualifier-seq [opt]
13842 exception-specification [opt]
13843 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13844 ( abstract-declarator )
13846 Returns a representation of the declarator. DCL_KIND is
13847 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13848 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13849 we are parsing a direct-declarator. It is
13850 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13851 of ambiguity we prefer an abstract declarator, as per
13852 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13853 cp_parser_declarator. */
13855 static cp_declarator *
13856 cp_parser_direct_declarator (cp_parser* parser,
13857 cp_parser_declarator_kind dcl_kind,
13858 int* ctor_dtor_or_conv_p,
13862 cp_declarator *declarator = NULL;
13863 tree scope = NULL_TREE;
13864 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13865 bool saved_in_declarator_p = parser->in_declarator_p;
13867 tree pushed_scope = NULL_TREE;
13871 /* Peek at the next token. */
13872 token = cp_lexer_peek_token (parser->lexer);
13873 if (token->type == CPP_OPEN_PAREN)
13875 /* This is either a parameter-declaration-clause, or a
13876 parenthesized declarator. When we know we are parsing a
13877 named declarator, it must be a parenthesized declarator
13878 if FIRST is true. For instance, `(int)' is a
13879 parameter-declaration-clause, with an omitted
13880 direct-abstract-declarator. But `((*))', is a
13881 parenthesized abstract declarator. Finally, when T is a
13882 template parameter `(T)' is a
13883 parameter-declaration-clause, and not a parenthesized
13886 We first try and parse a parameter-declaration-clause,
13887 and then try a nested declarator (if FIRST is true).
13889 It is not an error for it not to be a
13890 parameter-declaration-clause, even when FIRST is
13896 The first is the declaration of a function while the
13897 second is the definition of a variable, including its
13900 Having seen only the parenthesis, we cannot know which of
13901 these two alternatives should be selected. Even more
13902 complex are examples like:
13907 The former is a function-declaration; the latter is a
13908 variable initialization.
13910 Thus again, we try a parameter-declaration-clause, and if
13911 that fails, we back out and return. */
13913 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13916 unsigned saved_num_template_parameter_lists;
13917 bool is_declarator = false;
13920 /* In a member-declarator, the only valid interpretation
13921 of a parenthesis is the start of a
13922 parameter-declaration-clause. (It is invalid to
13923 initialize a static data member with a parenthesized
13924 initializer; only the "=" form of initialization is
13927 cp_parser_parse_tentatively (parser);
13929 /* Consume the `('. */
13930 cp_lexer_consume_token (parser->lexer);
13933 /* If this is going to be an abstract declarator, we're
13934 in a declarator and we can't have default args. */
13935 parser->default_arg_ok_p = false;
13936 parser->in_declarator_p = true;
13939 /* Inside the function parameter list, surrounding
13940 template-parameter-lists do not apply. */
13941 saved_num_template_parameter_lists
13942 = parser->num_template_parameter_lists;
13943 parser->num_template_parameter_lists = 0;
13945 begin_scope (sk_function_parms, NULL_TREE);
13947 /* Parse the parameter-declaration-clause. */
13948 params = cp_parser_parameter_declaration_clause (parser);
13950 parser->num_template_parameter_lists
13951 = saved_num_template_parameter_lists;
13953 /* If all went well, parse the cv-qualifier-seq and the
13954 exception-specification. */
13955 if (member_p || cp_parser_parse_definitely (parser))
13957 cp_cv_quals cv_quals;
13958 tree exception_specification;
13961 is_declarator = true;
13963 if (ctor_dtor_or_conv_p)
13964 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13966 /* Consume the `)'. */
13967 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13969 /* Parse the cv-qualifier-seq. */
13970 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13971 /* And the exception-specification. */
13972 exception_specification
13973 = cp_parser_exception_specification_opt (parser);
13976 = cp_parser_late_return_type_opt (parser);
13978 /* Create the function-declarator. */
13979 declarator = make_call_declarator (declarator,
13982 exception_specification,
13984 /* Any subsequent parameter lists are to do with
13985 return type, so are not those of the declared
13987 parser->default_arg_ok_p = false;
13990 /* Remove the function parms from scope. */
13991 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13992 pop_binding (DECL_NAME (t), t);
13996 /* Repeat the main loop. */
14000 /* If this is the first, we can try a parenthesized
14004 bool saved_in_type_id_in_expr_p;
14006 parser->default_arg_ok_p = saved_default_arg_ok_p;
14007 parser->in_declarator_p = saved_in_declarator_p;
14009 /* Consume the `('. */
14010 cp_lexer_consume_token (parser->lexer);
14011 /* Parse the nested declarator. */
14012 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14013 parser->in_type_id_in_expr_p = true;
14015 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14016 /*parenthesized_p=*/NULL,
14018 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14020 /* Expect a `)'. */
14021 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14022 declarator = cp_error_declarator;
14023 if (declarator == cp_error_declarator)
14026 goto handle_declarator;
14028 /* Otherwise, we must be done. */
14032 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14033 && token->type == CPP_OPEN_SQUARE)
14035 /* Parse an array-declarator. */
14038 if (ctor_dtor_or_conv_p)
14039 *ctor_dtor_or_conv_p = 0;
14042 parser->default_arg_ok_p = false;
14043 parser->in_declarator_p = true;
14044 /* Consume the `['. */
14045 cp_lexer_consume_token (parser->lexer);
14046 /* Peek at the next token. */
14047 token = cp_lexer_peek_token (parser->lexer);
14048 /* If the next token is `]', then there is no
14049 constant-expression. */
14050 if (token->type != CPP_CLOSE_SQUARE)
14052 bool non_constant_p;
14055 = cp_parser_constant_expression (parser,
14056 /*allow_non_constant=*/true,
14058 if (!non_constant_p)
14059 bounds = fold_non_dependent_expr (bounds);
14060 /* Normally, the array bound must be an integral constant
14061 expression. However, as an extension, we allow VLAs
14062 in function scopes. */
14063 else if (!parser->in_function_body)
14065 error_at (token->location,
14066 "array bound is not an integer constant");
14067 bounds = error_mark_node;
14069 else if (processing_template_decl && !error_operand_p (bounds))
14071 /* Remember this wasn't a constant-expression. */
14072 bounds = build_nop (TREE_TYPE (bounds), bounds);
14073 TREE_SIDE_EFFECTS (bounds) = 1;
14077 bounds = NULL_TREE;
14078 /* Look for the closing `]'. */
14079 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14081 declarator = cp_error_declarator;
14085 declarator = make_array_declarator (declarator, bounds);
14087 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14090 tree qualifying_scope;
14091 tree unqualified_name;
14092 special_function_kind sfk;
14094 bool pack_expansion_p = false;
14095 cp_token *declarator_id_start_token;
14097 /* Parse a declarator-id */
14098 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14101 cp_parser_parse_tentatively (parser);
14103 /* If we see an ellipsis, we should be looking at a
14105 if (token->type == CPP_ELLIPSIS)
14107 /* Consume the `...' */
14108 cp_lexer_consume_token (parser->lexer);
14110 pack_expansion_p = true;
14114 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14116 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14117 qualifying_scope = parser->scope;
14122 if (!unqualified_name && pack_expansion_p)
14124 /* Check whether an error occurred. */
14125 okay = !cp_parser_error_occurred (parser);
14127 /* We already consumed the ellipsis to mark a
14128 parameter pack, but we have no way to report it,
14129 so abort the tentative parse. We will be exiting
14130 immediately anyway. */
14131 cp_parser_abort_tentative_parse (parser);
14134 okay = cp_parser_parse_definitely (parser);
14137 unqualified_name = error_mark_node;
14138 else if (unqualified_name
14139 && (qualifying_scope
14140 || (TREE_CODE (unqualified_name)
14141 != IDENTIFIER_NODE)))
14143 cp_parser_error (parser, "expected unqualified-id");
14144 unqualified_name = error_mark_node;
14148 if (!unqualified_name)
14150 if (unqualified_name == error_mark_node)
14152 declarator = cp_error_declarator;
14153 pack_expansion_p = false;
14154 declarator->parameter_pack_p = false;
14158 if (qualifying_scope && at_namespace_scope_p ()
14159 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14161 /* In the declaration of a member of a template class
14162 outside of the class itself, the SCOPE will sometimes
14163 be a TYPENAME_TYPE. For example, given:
14165 template <typename T>
14166 int S<T>::R::i = 3;
14168 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14169 this context, we must resolve S<T>::R to an ordinary
14170 type, rather than a typename type.
14172 The reason we normally avoid resolving TYPENAME_TYPEs
14173 is that a specialization of `S' might render
14174 `S<T>::R' not a type. However, if `S' is
14175 specialized, then this `i' will not be used, so there
14176 is no harm in resolving the types here. */
14179 /* Resolve the TYPENAME_TYPE. */
14180 type = resolve_typename_type (qualifying_scope,
14181 /*only_current_p=*/false);
14182 /* If that failed, the declarator is invalid. */
14183 if (TREE_CODE (type) == TYPENAME_TYPE)
14184 error_at (declarator_id_start_token->location,
14185 "%<%T::%E%> is not a type",
14186 TYPE_CONTEXT (qualifying_scope),
14187 TYPE_IDENTIFIER (qualifying_scope));
14188 qualifying_scope = type;
14193 if (unqualified_name)
14197 if (qualifying_scope
14198 && CLASS_TYPE_P (qualifying_scope))
14199 class_type = qualifying_scope;
14201 class_type = current_class_type;
14203 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14205 tree name_type = TREE_TYPE (unqualified_name);
14206 if (class_type && same_type_p (name_type, class_type))
14208 if (qualifying_scope
14209 && CLASSTYPE_USE_TEMPLATE (name_type))
14211 error_at (declarator_id_start_token->location,
14212 "invalid use of constructor as a template");
14213 inform (declarator_id_start_token->location,
14214 "use %<%T::%D%> instead of %<%T::%D%> to "
14215 "name the constructor in a qualified name",
14217 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14218 class_type, name_type);
14219 declarator = cp_error_declarator;
14223 unqualified_name = constructor_name (class_type);
14227 /* We do not attempt to print the declarator
14228 here because we do not have enough
14229 information about its original syntactic
14231 cp_parser_error (parser, "invalid declarator");
14232 declarator = cp_error_declarator;
14239 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14240 sfk = sfk_destructor;
14241 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14242 sfk = sfk_conversion;
14243 else if (/* There's no way to declare a constructor
14244 for an anonymous type, even if the type
14245 got a name for linkage purposes. */
14246 !TYPE_WAS_ANONYMOUS (class_type)
14247 && constructor_name_p (unqualified_name,
14250 unqualified_name = constructor_name (class_type);
14251 sfk = sfk_constructor;
14254 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14255 *ctor_dtor_or_conv_p = -1;
14258 declarator = make_id_declarator (qualifying_scope,
14261 declarator->id_loc = token->location;
14262 declarator->parameter_pack_p = pack_expansion_p;
14264 if (pack_expansion_p)
14265 maybe_warn_variadic_templates ();
14268 handle_declarator:;
14269 scope = get_scope_of_declarator (declarator);
14271 /* Any names that appear after the declarator-id for a
14272 member are looked up in the containing scope. */
14273 pushed_scope = push_scope (scope);
14274 parser->in_declarator_p = true;
14275 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14276 || (declarator && declarator->kind == cdk_id))
14277 /* Default args are only allowed on function
14279 parser->default_arg_ok_p = saved_default_arg_ok_p;
14281 parser->default_arg_ok_p = false;
14290 /* For an abstract declarator, we might wind up with nothing at this
14291 point. That's an error; the declarator is not optional. */
14293 cp_parser_error (parser, "expected declarator");
14295 /* If we entered a scope, we must exit it now. */
14297 pop_scope (pushed_scope);
14299 parser->default_arg_ok_p = saved_default_arg_ok_p;
14300 parser->in_declarator_p = saved_in_declarator_p;
14305 /* Parse a ptr-operator.
14308 * cv-qualifier-seq [opt]
14310 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14315 & cv-qualifier-seq [opt]
14317 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14318 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14319 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14320 filled in with the TYPE containing the member. *CV_QUALS is
14321 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14322 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14323 Note that the tree codes returned by this function have nothing
14324 to do with the types of trees that will be eventually be created
14325 to represent the pointer or reference type being parsed. They are
14326 just constants with suggestive names. */
14327 static enum tree_code
14328 cp_parser_ptr_operator (cp_parser* parser,
14330 cp_cv_quals *cv_quals)
14332 enum tree_code code = ERROR_MARK;
14335 /* Assume that it's not a pointer-to-member. */
14337 /* And that there are no cv-qualifiers. */
14338 *cv_quals = TYPE_UNQUALIFIED;
14340 /* Peek at the next token. */
14341 token = cp_lexer_peek_token (parser->lexer);
14343 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14344 if (token->type == CPP_MULT)
14345 code = INDIRECT_REF;
14346 else if (token->type == CPP_AND)
14348 else if ((cxx_dialect != cxx98) &&
14349 token->type == CPP_AND_AND) /* C++0x only */
14350 code = NON_LVALUE_EXPR;
14352 if (code != ERROR_MARK)
14354 /* Consume the `*', `&' or `&&'. */
14355 cp_lexer_consume_token (parser->lexer);
14357 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14358 `&', if we are allowing GNU extensions. (The only qualifier
14359 that can legally appear after `&' is `restrict', but that is
14360 enforced during semantic analysis. */
14361 if (code == INDIRECT_REF
14362 || cp_parser_allow_gnu_extensions_p (parser))
14363 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14367 /* Try the pointer-to-member case. */
14368 cp_parser_parse_tentatively (parser);
14369 /* Look for the optional `::' operator. */
14370 cp_parser_global_scope_opt (parser,
14371 /*current_scope_valid_p=*/false);
14372 /* Look for the nested-name specifier. */
14373 token = cp_lexer_peek_token (parser->lexer);
14374 cp_parser_nested_name_specifier (parser,
14375 /*typename_keyword_p=*/false,
14376 /*check_dependency_p=*/true,
14378 /*is_declaration=*/false);
14379 /* If we found it, and the next token is a `*', then we are
14380 indeed looking at a pointer-to-member operator. */
14381 if (!cp_parser_error_occurred (parser)
14382 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14384 /* Indicate that the `*' operator was used. */
14385 code = INDIRECT_REF;
14387 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14388 error_at (token->location, "%qD is a namespace", parser->scope);
14391 /* The type of which the member is a member is given by the
14393 *type = parser->scope;
14394 /* The next name will not be qualified. */
14395 parser->scope = NULL_TREE;
14396 parser->qualifying_scope = NULL_TREE;
14397 parser->object_scope = NULL_TREE;
14398 /* Look for the optional cv-qualifier-seq. */
14399 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14402 /* If that didn't work we don't have a ptr-operator. */
14403 if (!cp_parser_parse_definitely (parser))
14404 cp_parser_error (parser, "expected ptr-operator");
14410 /* Parse an (optional) cv-qualifier-seq.
14413 cv-qualifier cv-qualifier-seq [opt]
14424 Returns a bitmask representing the cv-qualifiers. */
14427 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14429 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14434 cp_cv_quals cv_qualifier;
14436 /* Peek at the next token. */
14437 token = cp_lexer_peek_token (parser->lexer);
14438 /* See if it's a cv-qualifier. */
14439 switch (token->keyword)
14442 cv_qualifier = TYPE_QUAL_CONST;
14446 cv_qualifier = TYPE_QUAL_VOLATILE;
14450 cv_qualifier = TYPE_QUAL_RESTRICT;
14454 cv_qualifier = TYPE_UNQUALIFIED;
14461 if (cv_quals & cv_qualifier)
14463 error_at (token->location, "duplicate cv-qualifier");
14464 cp_lexer_purge_token (parser->lexer);
14468 cp_lexer_consume_token (parser->lexer);
14469 cv_quals |= cv_qualifier;
14476 /* Parse a late-specified return type, if any. This is not a separate
14477 non-terminal, but part of a function declarator, which looks like
14479 -> trailing-type-specifier-seq abstract-declarator(opt)
14481 Returns the type indicated by the type-id. */
14484 cp_parser_late_return_type_opt (cp_parser* parser)
14488 /* Peek at the next token. */
14489 token = cp_lexer_peek_token (parser->lexer);
14490 /* A late-specified return type is indicated by an initial '->'. */
14491 if (token->type != CPP_DEREF)
14494 /* Consume the ->. */
14495 cp_lexer_consume_token (parser->lexer);
14497 return cp_parser_trailing_type_id (parser);
14500 /* Parse a declarator-id.
14504 :: [opt] nested-name-specifier [opt] type-name
14506 In the `id-expression' case, the value returned is as for
14507 cp_parser_id_expression if the id-expression was an unqualified-id.
14508 If the id-expression was a qualified-id, then a SCOPE_REF is
14509 returned. The first operand is the scope (either a NAMESPACE_DECL
14510 or TREE_TYPE), but the second is still just a representation of an
14514 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14517 /* The expression must be an id-expression. Assume that qualified
14518 names are the names of types so that:
14521 int S<T>::R::i = 3;
14523 will work; we must treat `S<T>::R' as the name of a type.
14524 Similarly, assume that qualified names are templates, where
14528 int S<T>::R<T>::i = 3;
14531 id = cp_parser_id_expression (parser,
14532 /*template_keyword_p=*/false,
14533 /*check_dependency_p=*/false,
14534 /*template_p=*/NULL,
14535 /*declarator_p=*/true,
14537 if (id && BASELINK_P (id))
14538 id = BASELINK_FUNCTIONS (id);
14542 /* Parse a type-id.
14545 type-specifier-seq abstract-declarator [opt]
14547 Returns the TYPE specified. */
14550 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14551 bool is_trailing_return)
14553 cp_decl_specifier_seq type_specifier_seq;
14554 cp_declarator *abstract_declarator;
14556 /* Parse the type-specifier-seq. */
14557 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14558 is_trailing_return,
14559 &type_specifier_seq);
14560 if (type_specifier_seq.type == error_mark_node)
14561 return error_mark_node;
14563 /* There might or might not be an abstract declarator. */
14564 cp_parser_parse_tentatively (parser);
14565 /* Look for the declarator. */
14566 abstract_declarator
14567 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14568 /*parenthesized_p=*/NULL,
14569 /*member_p=*/false);
14570 /* Check to see if there really was a declarator. */
14571 if (!cp_parser_parse_definitely (parser))
14572 abstract_declarator = NULL;
14574 if (type_specifier_seq.type
14575 && type_uses_auto (type_specifier_seq.type))
14577 /* A type-id with type 'auto' is only ok if the abstract declarator
14578 is a function declarator with a late-specified return type. */
14579 if (abstract_declarator
14580 && abstract_declarator->kind == cdk_function
14581 && abstract_declarator->u.function.late_return_type)
14585 error ("invalid use of %<auto%>");
14586 return error_mark_node;
14590 return groktypename (&type_specifier_seq, abstract_declarator,
14594 static tree cp_parser_type_id (cp_parser *parser)
14596 return cp_parser_type_id_1 (parser, false, false);
14599 static tree cp_parser_template_type_arg (cp_parser *parser)
14601 return cp_parser_type_id_1 (parser, true, false);
14604 static tree cp_parser_trailing_type_id (cp_parser *parser)
14606 return cp_parser_type_id_1 (parser, false, true);
14609 /* Parse a type-specifier-seq.
14611 type-specifier-seq:
14612 type-specifier type-specifier-seq [opt]
14616 type-specifier-seq:
14617 attributes type-specifier-seq [opt]
14619 If IS_DECLARATION is true, we are at the start of a "condition" or
14620 exception-declaration, so we might be followed by a declarator-id.
14622 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14623 i.e. we've just seen "->".
14625 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14628 cp_parser_type_specifier_seq (cp_parser* parser,
14629 bool is_declaration,
14630 bool is_trailing_return,
14631 cp_decl_specifier_seq *type_specifier_seq)
14633 bool seen_type_specifier = false;
14634 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14635 cp_token *start_token = NULL;
14637 /* Clear the TYPE_SPECIFIER_SEQ. */
14638 clear_decl_specs (type_specifier_seq);
14640 /* In the context of a trailing return type, enum E { } is an
14641 elaborated-type-specifier followed by a function-body, not an
14643 if (is_trailing_return)
14644 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14646 /* Parse the type-specifiers and attributes. */
14649 tree type_specifier;
14650 bool is_cv_qualifier;
14652 /* Check for attributes first. */
14653 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14655 type_specifier_seq->attributes =
14656 chainon (type_specifier_seq->attributes,
14657 cp_parser_attributes_opt (parser));
14661 /* record the token of the beginning of the type specifier seq,
14662 for error reporting purposes*/
14664 start_token = cp_lexer_peek_token (parser->lexer);
14666 /* Look for the type-specifier. */
14667 type_specifier = cp_parser_type_specifier (parser,
14669 type_specifier_seq,
14670 /*is_declaration=*/false,
14673 if (!type_specifier)
14675 /* If the first type-specifier could not be found, this is not a
14676 type-specifier-seq at all. */
14677 if (!seen_type_specifier)
14679 cp_parser_error (parser, "expected type-specifier");
14680 type_specifier_seq->type = error_mark_node;
14683 /* If subsequent type-specifiers could not be found, the
14684 type-specifier-seq is complete. */
14688 seen_type_specifier = true;
14689 /* The standard says that a condition can be:
14691 type-specifier-seq declarator = assignment-expression
14698 we should treat the "S" as a declarator, not as a
14699 type-specifier. The standard doesn't say that explicitly for
14700 type-specifier-seq, but it does say that for
14701 decl-specifier-seq in an ordinary declaration. Perhaps it
14702 would be clearer just to allow a decl-specifier-seq here, and
14703 then add a semantic restriction that if any decl-specifiers
14704 that are not type-specifiers appear, the program is invalid. */
14705 if (is_declaration && !is_cv_qualifier)
14706 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14709 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14712 /* Parse a parameter-declaration-clause.
14714 parameter-declaration-clause:
14715 parameter-declaration-list [opt] ... [opt]
14716 parameter-declaration-list , ...
14718 Returns a representation for the parameter declarations. A return
14719 value of NULL indicates a parameter-declaration-clause consisting
14720 only of an ellipsis. */
14723 cp_parser_parameter_declaration_clause (cp_parser* parser)
14730 /* Peek at the next token. */
14731 token = cp_lexer_peek_token (parser->lexer);
14732 /* Check for trivial parameter-declaration-clauses. */
14733 if (token->type == CPP_ELLIPSIS)
14735 /* Consume the `...' token. */
14736 cp_lexer_consume_token (parser->lexer);
14739 else if (token->type == CPP_CLOSE_PAREN)
14740 /* There are no parameters. */
14742 #ifndef NO_IMPLICIT_EXTERN_C
14743 if (in_system_header && current_class_type == NULL
14744 && current_lang_name == lang_name_c)
14748 return void_list_node;
14750 /* Check for `(void)', too, which is a special case. */
14751 else if (token->keyword == RID_VOID
14752 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14753 == CPP_CLOSE_PAREN))
14755 /* Consume the `void' token. */
14756 cp_lexer_consume_token (parser->lexer);
14757 /* There are no parameters. */
14758 return void_list_node;
14761 /* Parse the parameter-declaration-list. */
14762 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14763 /* If a parse error occurred while parsing the
14764 parameter-declaration-list, then the entire
14765 parameter-declaration-clause is erroneous. */
14769 /* Peek at the next token. */
14770 token = cp_lexer_peek_token (parser->lexer);
14771 /* If it's a `,', the clause should terminate with an ellipsis. */
14772 if (token->type == CPP_COMMA)
14774 /* Consume the `,'. */
14775 cp_lexer_consume_token (parser->lexer);
14776 /* Expect an ellipsis. */
14778 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14780 /* It might also be `...' if the optional trailing `,' was
14782 else if (token->type == CPP_ELLIPSIS)
14784 /* Consume the `...' token. */
14785 cp_lexer_consume_token (parser->lexer);
14786 /* And remember that we saw it. */
14790 ellipsis_p = false;
14792 /* Finish the parameter list. */
14794 parameters = chainon (parameters, void_list_node);
14799 /* Parse a parameter-declaration-list.
14801 parameter-declaration-list:
14802 parameter-declaration
14803 parameter-declaration-list , parameter-declaration
14805 Returns a representation of the parameter-declaration-list, as for
14806 cp_parser_parameter_declaration_clause. However, the
14807 `void_list_node' is never appended to the list. Upon return,
14808 *IS_ERROR will be true iff an error occurred. */
14811 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14813 tree parameters = NULL_TREE;
14814 tree *tail = ¶meters;
14815 bool saved_in_unbraced_linkage_specification_p;
14818 /* Assume all will go well. */
14820 /* The special considerations that apply to a function within an
14821 unbraced linkage specifications do not apply to the parameters
14822 to the function. */
14823 saved_in_unbraced_linkage_specification_p
14824 = parser->in_unbraced_linkage_specification_p;
14825 parser->in_unbraced_linkage_specification_p = false;
14827 /* Look for more parameters. */
14830 cp_parameter_declarator *parameter;
14831 tree decl = error_mark_node;
14832 bool parenthesized_p;
14833 /* Parse the parameter. */
14835 = cp_parser_parameter_declaration (parser,
14836 /*template_parm_p=*/false,
14839 /* We don't know yet if the enclosing context is deprecated, so wait
14840 and warn in grokparms if appropriate. */
14841 deprecated_state = DEPRECATED_SUPPRESS;
14844 decl = grokdeclarator (parameter->declarator,
14845 ¶meter->decl_specifiers,
14847 parameter->default_argument != NULL_TREE,
14848 ¶meter->decl_specifiers.attributes);
14850 deprecated_state = DEPRECATED_NORMAL;
14852 /* If a parse error occurred parsing the parameter declaration,
14853 then the entire parameter-declaration-list is erroneous. */
14854 if (decl == error_mark_node)
14857 parameters = error_mark_node;
14861 if (parameter->decl_specifiers.attributes)
14862 cplus_decl_attributes (&decl,
14863 parameter->decl_specifiers.attributes,
14865 if (DECL_NAME (decl))
14866 decl = pushdecl (decl);
14868 if (decl != error_mark_node)
14870 retrofit_lang_decl (decl);
14871 DECL_PARM_INDEX (decl) = ++index;
14874 /* Add the new parameter to the list. */
14875 *tail = build_tree_list (parameter->default_argument, decl);
14876 tail = &TREE_CHAIN (*tail);
14878 /* Peek at the next token. */
14879 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14880 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14881 /* These are for Objective-C++ */
14882 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14883 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14884 /* The parameter-declaration-list is complete. */
14886 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14890 /* Peek at the next token. */
14891 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14892 /* If it's an ellipsis, then the list is complete. */
14893 if (token->type == CPP_ELLIPSIS)
14895 /* Otherwise, there must be more parameters. Consume the
14897 cp_lexer_consume_token (parser->lexer);
14898 /* When parsing something like:
14900 int i(float f, double d)
14902 we can tell after seeing the declaration for "f" that we
14903 are not looking at an initialization of a variable "i",
14904 but rather at the declaration of a function "i".
14906 Due to the fact that the parsing of template arguments
14907 (as specified to a template-id) requires backtracking we
14908 cannot use this technique when inside a template argument
14910 if (!parser->in_template_argument_list_p
14911 && !parser->in_type_id_in_expr_p
14912 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14913 /* However, a parameter-declaration of the form
14914 "foat(f)" (which is a valid declaration of a
14915 parameter "f") can also be interpreted as an
14916 expression (the conversion of "f" to "float"). */
14917 && !parenthesized_p)
14918 cp_parser_commit_to_tentative_parse (parser);
14922 cp_parser_error (parser, "expected %<,%> or %<...%>");
14923 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14924 cp_parser_skip_to_closing_parenthesis (parser,
14925 /*recovering=*/true,
14926 /*or_comma=*/false,
14927 /*consume_paren=*/false);
14932 parser->in_unbraced_linkage_specification_p
14933 = saved_in_unbraced_linkage_specification_p;
14938 /* Parse a parameter declaration.
14940 parameter-declaration:
14941 decl-specifier-seq ... [opt] declarator
14942 decl-specifier-seq declarator = assignment-expression
14943 decl-specifier-seq ... [opt] abstract-declarator [opt]
14944 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14946 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14947 declares a template parameter. (In that case, a non-nested `>'
14948 token encountered during the parsing of the assignment-expression
14949 is not interpreted as a greater-than operator.)
14951 Returns a representation of the parameter, or NULL if an error
14952 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14953 true iff the declarator is of the form "(p)". */
14955 static cp_parameter_declarator *
14956 cp_parser_parameter_declaration (cp_parser *parser,
14957 bool template_parm_p,
14958 bool *parenthesized_p)
14960 int declares_class_or_enum;
14961 bool greater_than_is_operator_p;
14962 cp_decl_specifier_seq decl_specifiers;
14963 cp_declarator *declarator;
14964 tree default_argument;
14965 cp_token *token = NULL, *declarator_token_start = NULL;
14966 const char *saved_message;
14968 /* In a template parameter, `>' is not an operator.
14972 When parsing a default template-argument for a non-type
14973 template-parameter, the first non-nested `>' is taken as the end
14974 of the template parameter-list rather than a greater-than
14976 greater_than_is_operator_p = !template_parm_p;
14978 /* Type definitions may not appear in parameter types. */
14979 saved_message = parser->type_definition_forbidden_message;
14980 parser->type_definition_forbidden_message
14981 = "types may not be defined in parameter types";
14983 /* Parse the declaration-specifiers. */
14984 cp_parser_decl_specifier_seq (parser,
14985 CP_PARSER_FLAGS_NONE,
14987 &declares_class_or_enum);
14989 /* Complain about missing 'typename' or other invalid type names. */
14990 if (!decl_specifiers.any_type_specifiers_p)
14991 cp_parser_parse_and_diagnose_invalid_type_name (parser);
14993 /* If an error occurred, there's no reason to attempt to parse the
14994 rest of the declaration. */
14995 if (cp_parser_error_occurred (parser))
14997 parser->type_definition_forbidden_message = saved_message;
15001 /* Peek at the next token. */
15002 token = cp_lexer_peek_token (parser->lexer);
15004 /* If the next token is a `)', `,', `=', `>', or `...', then there
15005 is no declarator. However, when variadic templates are enabled,
15006 there may be a declarator following `...'. */
15007 if (token->type == CPP_CLOSE_PAREN
15008 || token->type == CPP_COMMA
15009 || token->type == CPP_EQ
15010 || token->type == CPP_GREATER)
15013 if (parenthesized_p)
15014 *parenthesized_p = false;
15016 /* Otherwise, there should be a declarator. */
15019 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15020 parser->default_arg_ok_p = false;
15022 /* After seeing a decl-specifier-seq, if the next token is not a
15023 "(", there is no possibility that the code is a valid
15024 expression. Therefore, if parsing tentatively, we commit at
15026 if (!parser->in_template_argument_list_p
15027 /* In an expression context, having seen:
15031 we cannot be sure whether we are looking at a
15032 function-type (taking a "char" as a parameter) or a cast
15033 of some object of type "char" to "int". */
15034 && !parser->in_type_id_in_expr_p
15035 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15036 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15037 cp_parser_commit_to_tentative_parse (parser);
15038 /* Parse the declarator. */
15039 declarator_token_start = token;
15040 declarator = cp_parser_declarator (parser,
15041 CP_PARSER_DECLARATOR_EITHER,
15042 /*ctor_dtor_or_conv_p=*/NULL,
15044 /*member_p=*/false);
15045 parser->default_arg_ok_p = saved_default_arg_ok_p;
15046 /* After the declarator, allow more attributes. */
15047 decl_specifiers.attributes
15048 = chainon (decl_specifiers.attributes,
15049 cp_parser_attributes_opt (parser));
15052 /* If the next token is an ellipsis, and we have not seen a
15053 declarator name, and the type of the declarator contains parameter
15054 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15055 a parameter pack expansion expression. Otherwise, leave the
15056 ellipsis for a C-style variadic function. */
15057 token = cp_lexer_peek_token (parser->lexer);
15058 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15060 tree type = decl_specifiers.type;
15062 if (type && DECL_P (type))
15063 type = TREE_TYPE (type);
15066 && TREE_CODE (type) != TYPE_PACK_EXPANSION
15067 && declarator_can_be_parameter_pack (declarator)
15068 && (!declarator || !declarator->parameter_pack_p)
15069 && uses_parameter_packs (type))
15071 /* Consume the `...'. */
15072 cp_lexer_consume_token (parser->lexer);
15073 maybe_warn_variadic_templates ();
15075 /* Build a pack expansion type */
15077 declarator->parameter_pack_p = true;
15079 decl_specifiers.type = make_pack_expansion (type);
15083 /* The restriction on defining new types applies only to the type
15084 of the parameter, not to the default argument. */
15085 parser->type_definition_forbidden_message = saved_message;
15087 /* If the next token is `=', then process a default argument. */
15088 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15090 /* Consume the `='. */
15091 cp_lexer_consume_token (parser->lexer);
15093 /* If we are defining a class, then the tokens that make up the
15094 default argument must be saved and processed later. */
15095 if (!template_parm_p && at_class_scope_p ()
15096 && TYPE_BEING_DEFINED (current_class_type)
15097 && !LAMBDA_TYPE_P (current_class_type))
15099 unsigned depth = 0;
15100 int maybe_template_id = 0;
15101 cp_token *first_token;
15104 /* Add tokens until we have processed the entire default
15105 argument. We add the range [first_token, token). */
15106 first_token = cp_lexer_peek_token (parser->lexer);
15111 /* Peek at the next token. */
15112 token = cp_lexer_peek_token (parser->lexer);
15113 /* What we do depends on what token we have. */
15114 switch (token->type)
15116 /* In valid code, a default argument must be
15117 immediately followed by a `,' `)', or `...'. */
15119 if (depth == 0 && maybe_template_id)
15121 /* If we've seen a '<', we might be in a
15122 template-argument-list. Until Core issue 325 is
15123 resolved, we don't know how this situation ought
15124 to be handled, so try to DTRT. We check whether
15125 what comes after the comma is a valid parameter
15126 declaration list. If it is, then the comma ends
15127 the default argument; otherwise the default
15128 argument continues. */
15129 bool error = false;
15131 /* Set ITALP so cp_parser_parameter_declaration_list
15132 doesn't decide to commit to this parse. */
15133 bool saved_italp = parser->in_template_argument_list_p;
15134 parser->in_template_argument_list_p = true;
15136 cp_parser_parse_tentatively (parser);
15137 cp_lexer_consume_token (parser->lexer);
15138 cp_parser_parameter_declaration_list (parser, &error);
15139 if (!cp_parser_error_occurred (parser) && !error)
15141 cp_parser_abort_tentative_parse (parser);
15143 parser->in_template_argument_list_p = saved_italp;
15146 case CPP_CLOSE_PAREN:
15148 /* If we run into a non-nested `;', `}', or `]',
15149 then the code is invalid -- but the default
15150 argument is certainly over. */
15151 case CPP_SEMICOLON:
15152 case CPP_CLOSE_BRACE:
15153 case CPP_CLOSE_SQUARE:
15156 /* Update DEPTH, if necessary. */
15157 else if (token->type == CPP_CLOSE_PAREN
15158 || token->type == CPP_CLOSE_BRACE
15159 || token->type == CPP_CLOSE_SQUARE)
15163 case CPP_OPEN_PAREN:
15164 case CPP_OPEN_SQUARE:
15165 case CPP_OPEN_BRACE:
15171 /* This might be the comparison operator, or it might
15172 start a template argument list. */
15173 ++maybe_template_id;
15177 if (cxx_dialect == cxx98)
15179 /* Fall through for C++0x, which treats the `>>'
15180 operator like two `>' tokens in certain
15186 /* This might be an operator, or it might close a
15187 template argument list. But if a previous '<'
15188 started a template argument list, this will have
15189 closed it, so we can't be in one anymore. */
15190 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15191 if (maybe_template_id < 0)
15192 maybe_template_id = 0;
15196 /* If we run out of tokens, issue an error message. */
15198 case CPP_PRAGMA_EOL:
15199 error_at (token->location, "file ends in default argument");
15205 /* In these cases, we should look for template-ids.
15206 For example, if the default argument is
15207 `X<int, double>()', we need to do name lookup to
15208 figure out whether or not `X' is a template; if
15209 so, the `,' does not end the default argument.
15211 That is not yet done. */
15218 /* If we've reached the end, stop. */
15222 /* Add the token to the token block. */
15223 token = cp_lexer_consume_token (parser->lexer);
15226 /* Create a DEFAULT_ARG to represent the unparsed default
15228 default_argument = make_node (DEFAULT_ARG);
15229 DEFARG_TOKENS (default_argument)
15230 = cp_token_cache_new (first_token, token);
15231 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15233 /* Outside of a class definition, we can just parse the
15234 assignment-expression. */
15237 token = cp_lexer_peek_token (parser->lexer);
15239 = cp_parser_default_argument (parser, template_parm_p);
15242 if (!parser->default_arg_ok_p)
15244 if (flag_permissive)
15245 warning (0, "deprecated use of default argument for parameter of non-function");
15248 error_at (token->location,
15249 "default arguments are only "
15250 "permitted for function parameters");
15251 default_argument = NULL_TREE;
15254 else if ((declarator && declarator->parameter_pack_p)
15255 || (decl_specifiers.type
15256 && PACK_EXPANSION_P (decl_specifiers.type)))
15258 /* Find the name of the parameter pack. */
15259 cp_declarator *id_declarator = declarator;
15260 while (id_declarator && id_declarator->kind != cdk_id)
15261 id_declarator = id_declarator->declarator;
15263 if (id_declarator && id_declarator->kind == cdk_id)
15264 error_at (declarator_token_start->location,
15266 ? "template parameter pack %qD"
15267 " cannot have a default argument"
15268 : "parameter pack %qD cannot have a default argument",
15269 id_declarator->u.id.unqualified_name);
15271 error_at (declarator_token_start->location,
15273 ? "template parameter pack cannot have a default argument"
15274 : "parameter pack cannot have a default argument");
15276 default_argument = NULL_TREE;
15280 default_argument = NULL_TREE;
15282 return make_parameter_declarator (&decl_specifiers,
15287 /* Parse a default argument and return it.
15289 TEMPLATE_PARM_P is true if this is a default argument for a
15290 non-type template parameter. */
15292 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15294 tree default_argument = NULL_TREE;
15295 bool saved_greater_than_is_operator_p;
15296 bool saved_local_variables_forbidden_p;
15298 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15300 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15301 parser->greater_than_is_operator_p = !template_parm_p;
15302 /* Local variable names (and the `this' keyword) may not
15303 appear in a default argument. */
15304 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15305 parser->local_variables_forbidden_p = true;
15306 /* Parse the assignment-expression. */
15307 if (template_parm_p)
15308 push_deferring_access_checks (dk_no_deferred);
15310 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15311 if (template_parm_p)
15312 pop_deferring_access_checks ();
15313 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15314 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15316 return default_argument;
15319 /* Parse a function-body.
15322 compound_statement */
15325 cp_parser_function_body (cp_parser *parser)
15327 cp_parser_compound_statement (parser, NULL, false);
15330 /* Parse a ctor-initializer-opt followed by a function-body. Return
15331 true if a ctor-initializer was present. */
15334 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15337 bool ctor_initializer_p;
15339 /* Begin the function body. */
15340 body = begin_function_body ();
15341 /* Parse the optional ctor-initializer. */
15342 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15343 /* Parse the function-body. */
15344 cp_parser_function_body (parser);
15345 /* Finish the function body. */
15346 finish_function_body (body);
15348 return ctor_initializer_p;
15351 /* Parse an initializer.
15354 = initializer-clause
15355 ( expression-list )
15357 Returns an expression representing the initializer. If no
15358 initializer is present, NULL_TREE is returned.
15360 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15361 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15362 set to TRUE if there is no initializer present. If there is an
15363 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15364 is set to true; otherwise it is set to false. */
15367 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15368 bool* non_constant_p)
15373 /* Peek at the next token. */
15374 token = cp_lexer_peek_token (parser->lexer);
15376 /* Let our caller know whether or not this initializer was
15378 *is_direct_init = (token->type != CPP_EQ);
15379 /* Assume that the initializer is constant. */
15380 *non_constant_p = false;
15382 if (token->type == CPP_EQ)
15384 /* Consume the `='. */
15385 cp_lexer_consume_token (parser->lexer);
15386 /* Parse the initializer-clause. */
15387 init = cp_parser_initializer_clause (parser, non_constant_p);
15389 else if (token->type == CPP_OPEN_PAREN)
15392 vec = cp_parser_parenthesized_expression_list (parser, false,
15394 /*allow_expansion_p=*/true,
15397 return error_mark_node;
15398 init = build_tree_list_vec (vec);
15399 release_tree_vector (vec);
15401 else if (token->type == CPP_OPEN_BRACE)
15403 maybe_warn_cpp0x ("extended initializer lists");
15404 init = cp_parser_braced_list (parser, non_constant_p);
15405 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15409 /* Anything else is an error. */
15410 cp_parser_error (parser, "expected initializer");
15411 init = error_mark_node;
15417 /* Parse an initializer-clause.
15419 initializer-clause:
15420 assignment-expression
15423 Returns an expression representing the initializer.
15425 If the `assignment-expression' production is used the value
15426 returned is simply a representation for the expression.
15428 Otherwise, calls cp_parser_braced_list. */
15431 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15435 /* Assume the expression is constant. */
15436 *non_constant_p = false;
15438 /* If it is not a `{', then we are looking at an
15439 assignment-expression. */
15440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15443 = cp_parser_constant_expression (parser,
15444 /*allow_non_constant_p=*/true,
15446 if (!*non_constant_p)
15447 initializer = fold_non_dependent_expr (initializer);
15450 initializer = cp_parser_braced_list (parser, non_constant_p);
15452 return initializer;
15455 /* Parse a brace-enclosed initializer list.
15458 { initializer-list , [opt] }
15461 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15462 the elements of the initializer-list (or NULL, if the last
15463 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15464 NULL_TREE. There is no way to detect whether or not the optional
15465 trailing `,' was provided. NON_CONSTANT_P is as for
15466 cp_parser_initializer. */
15469 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15473 /* Consume the `{' token. */
15474 cp_lexer_consume_token (parser->lexer);
15475 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15476 initializer = make_node (CONSTRUCTOR);
15477 /* If it's not a `}', then there is a non-trivial initializer. */
15478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15480 /* Parse the initializer list. */
15481 CONSTRUCTOR_ELTS (initializer)
15482 = cp_parser_initializer_list (parser, non_constant_p);
15483 /* A trailing `,' token is allowed. */
15484 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15485 cp_lexer_consume_token (parser->lexer);
15487 /* Now, there should be a trailing `}'. */
15488 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15489 TREE_TYPE (initializer) = init_list_type_node;
15490 return initializer;
15493 /* Parse an initializer-list.
15496 initializer-clause ... [opt]
15497 initializer-list , initializer-clause ... [opt]
15502 identifier : initializer-clause
15503 initializer-list, identifier : initializer-clause
15505 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15506 for the initializer. If the INDEX of the elt is non-NULL, it is the
15507 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15508 as for cp_parser_initializer. */
15510 static VEC(constructor_elt,gc) *
15511 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15513 VEC(constructor_elt,gc) *v = NULL;
15515 /* Assume all of the expressions are constant. */
15516 *non_constant_p = false;
15518 /* Parse the rest of the list. */
15524 bool clause_non_constant_p;
15526 /* If the next token is an identifier and the following one is a
15527 colon, we are looking at the GNU designated-initializer
15529 if (cp_parser_allow_gnu_extensions_p (parser)
15530 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15531 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15533 /* Warn the user that they are using an extension. */
15534 pedwarn (input_location, OPT_pedantic,
15535 "ISO C++ does not allow designated initializers");
15536 /* Consume the identifier. */
15537 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15538 /* Consume the `:'. */
15539 cp_lexer_consume_token (parser->lexer);
15542 identifier = NULL_TREE;
15544 /* Parse the initializer. */
15545 initializer = cp_parser_initializer_clause (parser,
15546 &clause_non_constant_p);
15547 /* If any clause is non-constant, so is the entire initializer. */
15548 if (clause_non_constant_p)
15549 *non_constant_p = true;
15551 /* If we have an ellipsis, this is an initializer pack
15553 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15555 /* Consume the `...'. */
15556 cp_lexer_consume_token (parser->lexer);
15558 /* Turn the initializer into an initializer expansion. */
15559 initializer = make_pack_expansion (initializer);
15562 /* Add it to the vector. */
15563 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15565 /* If the next token is not a comma, we have reached the end of
15567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15570 /* Peek at the next token. */
15571 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15572 /* If the next token is a `}', then we're still done. An
15573 initializer-clause can have a trailing `,' after the
15574 initializer-list and before the closing `}'. */
15575 if (token->type == CPP_CLOSE_BRACE)
15578 /* Consume the `,' token. */
15579 cp_lexer_consume_token (parser->lexer);
15585 /* Classes [gram.class] */
15587 /* Parse a class-name.
15593 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15594 to indicate that names looked up in dependent types should be
15595 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15596 keyword has been used to indicate that the name that appears next
15597 is a template. TAG_TYPE indicates the explicit tag given before
15598 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15599 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15600 is the class being defined in a class-head.
15602 Returns the TYPE_DECL representing the class. */
15605 cp_parser_class_name (cp_parser *parser,
15606 bool typename_keyword_p,
15607 bool template_keyword_p,
15608 enum tag_types tag_type,
15609 bool check_dependency_p,
15611 bool is_declaration)
15617 tree identifier = NULL_TREE;
15619 /* All class-names start with an identifier. */
15620 token = cp_lexer_peek_token (parser->lexer);
15621 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15623 cp_parser_error (parser, "expected class-name");
15624 return error_mark_node;
15627 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15628 to a template-id, so we save it here. */
15629 scope = parser->scope;
15630 if (scope == error_mark_node)
15631 return error_mark_node;
15633 /* Any name names a type if we're following the `typename' keyword
15634 in a qualified name where the enclosing scope is type-dependent. */
15635 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15636 && dependent_type_p (scope));
15637 /* Handle the common case (an identifier, but not a template-id)
15639 if (token->type == CPP_NAME
15640 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15642 cp_token *identifier_token;
15645 /* Look for the identifier. */
15646 identifier_token = cp_lexer_peek_token (parser->lexer);
15647 ambiguous_p = identifier_token->ambiguous_p;
15648 identifier = cp_parser_identifier (parser);
15649 /* If the next token isn't an identifier, we are certainly not
15650 looking at a class-name. */
15651 if (identifier == error_mark_node)
15652 decl = error_mark_node;
15653 /* If we know this is a type-name, there's no need to look it
15655 else if (typename_p)
15659 tree ambiguous_decls;
15660 /* If we already know that this lookup is ambiguous, then
15661 we've already issued an error message; there's no reason
15665 cp_parser_simulate_error (parser);
15666 return error_mark_node;
15668 /* If the next token is a `::', then the name must be a type
15671 [basic.lookup.qual]
15673 During the lookup for a name preceding the :: scope
15674 resolution operator, object, function, and enumerator
15675 names are ignored. */
15676 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15677 tag_type = typename_type;
15678 /* Look up the name. */
15679 decl = cp_parser_lookup_name (parser, identifier,
15681 /*is_template=*/false,
15682 /*is_namespace=*/false,
15683 check_dependency_p,
15685 identifier_token->location);
15686 if (ambiguous_decls)
15688 error_at (identifier_token->location,
15689 "reference to %qD is ambiguous", identifier);
15690 print_candidates (ambiguous_decls);
15691 if (cp_parser_parsing_tentatively (parser))
15693 identifier_token->ambiguous_p = true;
15694 cp_parser_simulate_error (parser);
15696 return error_mark_node;
15702 /* Try a template-id. */
15703 decl = cp_parser_template_id (parser, template_keyword_p,
15704 check_dependency_p,
15706 if (decl == error_mark_node)
15707 return error_mark_node;
15710 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15712 /* If this is a typename, create a TYPENAME_TYPE. */
15713 if (typename_p && decl != error_mark_node)
15715 decl = make_typename_type (scope, decl, typename_type,
15716 /*complain=*/tf_error);
15717 if (decl != error_mark_node)
15718 decl = TYPE_NAME (decl);
15721 /* Check to see that it is really the name of a class. */
15722 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15723 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15724 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15725 /* Situations like this:
15727 template <typename T> struct A {
15728 typename T::template X<int>::I i;
15731 are problematic. Is `T::template X<int>' a class-name? The
15732 standard does not seem to be definitive, but there is no other
15733 valid interpretation of the following `::'. Therefore, those
15734 names are considered class-names. */
15736 decl = make_typename_type (scope, decl, tag_type, tf_error);
15737 if (decl != error_mark_node)
15738 decl = TYPE_NAME (decl);
15740 else if (TREE_CODE (decl) != TYPE_DECL
15741 || TREE_TYPE (decl) == error_mark_node
15742 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15743 decl = error_mark_node;
15745 if (decl == error_mark_node)
15746 cp_parser_error (parser, "expected class-name");
15747 else if (identifier && !parser->scope)
15748 maybe_note_name_used_in_class (identifier, decl);
15753 /* Parse a class-specifier.
15756 class-head { member-specification [opt] }
15758 Returns the TREE_TYPE representing the class. */
15761 cp_parser_class_specifier (cp_parser* parser)
15764 tree attributes = NULL_TREE;
15765 bool nested_name_specifier_p;
15766 unsigned saved_num_template_parameter_lists;
15767 bool saved_in_function_body;
15768 bool saved_in_unbraced_linkage_specification_p;
15769 tree old_scope = NULL_TREE;
15770 tree scope = NULL_TREE;
15773 push_deferring_access_checks (dk_no_deferred);
15775 /* Parse the class-head. */
15776 type = cp_parser_class_head (parser,
15777 &nested_name_specifier_p,
15780 /* If the class-head was a semantic disaster, skip the entire body
15784 cp_parser_skip_to_end_of_block_or_statement (parser);
15785 pop_deferring_access_checks ();
15786 return error_mark_node;
15789 /* Look for the `{'. */
15790 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15792 pop_deferring_access_checks ();
15793 return error_mark_node;
15796 /* Process the base classes. If they're invalid, skip the
15797 entire class body. */
15798 if (!xref_basetypes (type, bases))
15800 /* Consuming the closing brace yields better error messages
15802 if (cp_parser_skip_to_closing_brace (parser))
15803 cp_lexer_consume_token (parser->lexer);
15804 pop_deferring_access_checks ();
15805 return error_mark_node;
15808 /* Issue an error message if type-definitions are forbidden here. */
15809 cp_parser_check_type_definition (parser);
15810 /* Remember that we are defining one more class. */
15811 ++parser->num_classes_being_defined;
15812 /* Inside the class, surrounding template-parameter-lists do not
15814 saved_num_template_parameter_lists
15815 = parser->num_template_parameter_lists;
15816 parser->num_template_parameter_lists = 0;
15817 /* We are not in a function body. */
15818 saved_in_function_body = parser->in_function_body;
15819 parser->in_function_body = false;
15820 /* We are not immediately inside an extern "lang" block. */
15821 saved_in_unbraced_linkage_specification_p
15822 = parser->in_unbraced_linkage_specification_p;
15823 parser->in_unbraced_linkage_specification_p = false;
15825 /* Start the class. */
15826 if (nested_name_specifier_p)
15828 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15829 old_scope = push_inner_scope (scope);
15831 type = begin_class_definition (type, attributes);
15833 if (type == error_mark_node)
15834 /* If the type is erroneous, skip the entire body of the class. */
15835 cp_parser_skip_to_closing_brace (parser);
15837 /* Parse the member-specification. */
15838 cp_parser_member_specification_opt (parser);
15840 /* Look for the trailing `}'. */
15841 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15842 /* Look for trailing attributes to apply to this class. */
15843 if (cp_parser_allow_gnu_extensions_p (parser))
15844 attributes = cp_parser_attributes_opt (parser);
15845 if (type != error_mark_node)
15846 type = finish_struct (type, attributes);
15847 if (nested_name_specifier_p)
15848 pop_inner_scope (old_scope, scope);
15849 /* If this class is not itself within the scope of another class,
15850 then we need to parse the bodies of all of the queued function
15851 definitions. Note that the queued functions defined in a class
15852 are not always processed immediately following the
15853 class-specifier for that class. Consider:
15856 struct B { void f() { sizeof (A); } };
15859 If `f' were processed before the processing of `A' were
15860 completed, there would be no way to compute the size of `A'.
15861 Note that the nesting we are interested in here is lexical --
15862 not the semantic nesting given by TYPE_CONTEXT. In particular,
15865 struct A { struct B; };
15866 struct A::B { void f() { } };
15868 there is no need to delay the parsing of `A::B::f'. */
15869 if (--parser->num_classes_being_defined == 0)
15873 tree class_type = NULL_TREE;
15874 tree pushed_scope = NULL_TREE;
15876 /* In a first pass, parse default arguments to the functions.
15877 Then, in a second pass, parse the bodies of the functions.
15878 This two-phased approach handles cases like:
15886 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15887 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15888 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15889 TREE_PURPOSE (parser->unparsed_functions_queues)
15890 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15892 fn = TREE_VALUE (queue_entry);
15893 /* If there are default arguments that have not yet been processed,
15894 take care of them now. */
15895 if (class_type != TREE_PURPOSE (queue_entry))
15898 pop_scope (pushed_scope);
15899 class_type = TREE_PURPOSE (queue_entry);
15900 pushed_scope = push_scope (class_type);
15902 /* Make sure that any template parameters are in scope. */
15903 maybe_begin_member_template_processing (fn);
15904 /* Parse the default argument expressions. */
15905 cp_parser_late_parsing_default_args (parser, fn);
15906 /* Remove any template parameters from the symbol table. */
15907 maybe_end_member_template_processing ();
15910 pop_scope (pushed_scope);
15911 /* Now parse the body of the functions. */
15912 for (TREE_VALUE (parser->unparsed_functions_queues)
15913 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15914 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15915 TREE_VALUE (parser->unparsed_functions_queues)
15916 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15918 /* Figure out which function we need to process. */
15919 fn = TREE_VALUE (queue_entry);
15920 /* Parse the function. */
15921 cp_parser_late_parsing_for_member (parser, fn);
15925 /* Put back any saved access checks. */
15926 pop_deferring_access_checks ();
15928 /* Restore saved state. */
15929 parser->in_function_body = saved_in_function_body;
15930 parser->num_template_parameter_lists
15931 = saved_num_template_parameter_lists;
15932 parser->in_unbraced_linkage_specification_p
15933 = saved_in_unbraced_linkage_specification_p;
15938 /* Parse a class-head.
15941 class-key identifier [opt] base-clause [opt]
15942 class-key nested-name-specifier identifier base-clause [opt]
15943 class-key nested-name-specifier [opt] template-id
15947 class-key attributes identifier [opt] base-clause [opt]
15948 class-key attributes nested-name-specifier identifier base-clause [opt]
15949 class-key attributes nested-name-specifier [opt] template-id
15952 Upon return BASES is initialized to the list of base classes (or
15953 NULL, if there are none) in the same form returned by
15954 cp_parser_base_clause.
15956 Returns the TYPE of the indicated class. Sets
15957 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15958 involving a nested-name-specifier was used, and FALSE otherwise.
15960 Returns error_mark_node if this is not a class-head.
15962 Returns NULL_TREE if the class-head is syntactically valid, but
15963 semantically invalid in a way that means we should skip the entire
15964 body of the class. */
15967 cp_parser_class_head (cp_parser* parser,
15968 bool* nested_name_specifier_p,
15969 tree *attributes_p,
15972 tree nested_name_specifier;
15973 enum tag_types class_key;
15974 tree id = NULL_TREE;
15975 tree type = NULL_TREE;
15977 bool template_id_p = false;
15978 bool qualified_p = false;
15979 bool invalid_nested_name_p = false;
15980 bool invalid_explicit_specialization_p = false;
15981 tree pushed_scope = NULL_TREE;
15982 unsigned num_templates;
15983 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15984 /* Assume no nested-name-specifier will be present. */
15985 *nested_name_specifier_p = false;
15986 /* Assume no template parameter lists will be used in defining the
15990 *bases = NULL_TREE;
15992 /* Look for the class-key. */
15993 class_key = cp_parser_class_key (parser);
15994 if (class_key == none_type)
15995 return error_mark_node;
15997 /* Parse the attributes. */
15998 attributes = cp_parser_attributes_opt (parser);
16000 /* If the next token is `::', that is invalid -- but sometimes
16001 people do try to write:
16005 Handle this gracefully by accepting the extra qualifier, and then
16006 issuing an error about it later if this really is a
16007 class-head. If it turns out just to be an elaborated type
16008 specifier, remain silent. */
16009 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16010 qualified_p = true;
16012 push_deferring_access_checks (dk_no_check);
16014 /* Determine the name of the class. Begin by looking for an
16015 optional nested-name-specifier. */
16016 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16017 nested_name_specifier
16018 = cp_parser_nested_name_specifier_opt (parser,
16019 /*typename_keyword_p=*/false,
16020 /*check_dependency_p=*/false,
16022 /*is_declaration=*/false);
16023 /* If there was a nested-name-specifier, then there *must* be an
16025 if (nested_name_specifier)
16027 type_start_token = cp_lexer_peek_token (parser->lexer);
16028 /* Although the grammar says `identifier', it really means
16029 `class-name' or `template-name'. You are only allowed to
16030 define a class that has already been declared with this
16033 The proposed resolution for Core Issue 180 says that wherever
16034 you see `class T::X' you should treat `X' as a type-name.
16036 It is OK to define an inaccessible class; for example:
16038 class A { class B; };
16041 We do not know if we will see a class-name, or a
16042 template-name. We look for a class-name first, in case the
16043 class-name is a template-id; if we looked for the
16044 template-name first we would stop after the template-name. */
16045 cp_parser_parse_tentatively (parser);
16046 type = cp_parser_class_name (parser,
16047 /*typename_keyword_p=*/false,
16048 /*template_keyword_p=*/false,
16050 /*check_dependency_p=*/false,
16051 /*class_head_p=*/true,
16052 /*is_declaration=*/false);
16053 /* If that didn't work, ignore the nested-name-specifier. */
16054 if (!cp_parser_parse_definitely (parser))
16056 invalid_nested_name_p = true;
16057 type_start_token = cp_lexer_peek_token (parser->lexer);
16058 id = cp_parser_identifier (parser);
16059 if (id == error_mark_node)
16062 /* If we could not find a corresponding TYPE, treat this
16063 declaration like an unqualified declaration. */
16064 if (type == error_mark_node)
16065 nested_name_specifier = NULL_TREE;
16066 /* Otherwise, count the number of templates used in TYPE and its
16067 containing scopes. */
16072 for (scope = TREE_TYPE (type);
16073 scope && TREE_CODE (scope) != NAMESPACE_DECL;
16074 scope = (TYPE_P (scope)
16075 ? TYPE_CONTEXT (scope)
16076 : DECL_CONTEXT (scope)))
16078 && CLASS_TYPE_P (scope)
16079 && CLASSTYPE_TEMPLATE_INFO (scope)
16080 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16081 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16085 /* Otherwise, the identifier is optional. */
16088 /* We don't know whether what comes next is a template-id,
16089 an identifier, or nothing at all. */
16090 cp_parser_parse_tentatively (parser);
16091 /* Check for a template-id. */
16092 type_start_token = cp_lexer_peek_token (parser->lexer);
16093 id = cp_parser_template_id (parser,
16094 /*template_keyword_p=*/false,
16095 /*check_dependency_p=*/true,
16096 /*is_declaration=*/true);
16097 /* If that didn't work, it could still be an identifier. */
16098 if (!cp_parser_parse_definitely (parser))
16100 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16102 type_start_token = cp_lexer_peek_token (parser->lexer);
16103 id = cp_parser_identifier (parser);
16110 template_id_p = true;
16115 pop_deferring_access_checks ();
16118 cp_parser_check_for_invalid_template_id (parser, id,
16119 type_start_token->location);
16121 /* If it's not a `:' or a `{' then we can't really be looking at a
16122 class-head, since a class-head only appears as part of a
16123 class-specifier. We have to detect this situation before calling
16124 xref_tag, since that has irreversible side-effects. */
16125 if (!cp_parser_next_token_starts_class_definition_p (parser))
16127 cp_parser_error (parser, "expected %<{%> or %<:%>");
16128 return error_mark_node;
16131 /* At this point, we're going ahead with the class-specifier, even
16132 if some other problem occurs. */
16133 cp_parser_commit_to_tentative_parse (parser);
16134 /* Issue the error about the overly-qualified name now. */
16137 cp_parser_error (parser,
16138 "global qualification of class name is invalid");
16139 return error_mark_node;
16141 else if (invalid_nested_name_p)
16143 cp_parser_error (parser,
16144 "qualified name does not name a class");
16145 return error_mark_node;
16147 else if (nested_name_specifier)
16151 /* Reject typedef-names in class heads. */
16152 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16154 error_at (type_start_token->location,
16155 "invalid class name in declaration of %qD",
16161 /* Figure out in what scope the declaration is being placed. */
16162 scope = current_scope ();
16163 /* If that scope does not contain the scope in which the
16164 class was originally declared, the program is invalid. */
16165 if (scope && !is_ancestor (scope, nested_name_specifier))
16167 if (at_namespace_scope_p ())
16168 error_at (type_start_token->location,
16169 "declaration of %qD in namespace %qD which does not "
16171 type, scope, nested_name_specifier);
16173 error_at (type_start_token->location,
16174 "declaration of %qD in %qD which does not enclose %qD",
16175 type, scope, nested_name_specifier);
16181 A declarator-id shall not be qualified except for the
16182 definition of a ... nested class outside of its class
16183 ... [or] the definition or explicit instantiation of a
16184 class member of a namespace outside of its namespace. */
16185 if (scope == nested_name_specifier)
16187 permerror (nested_name_specifier_token_start->location,
16188 "extra qualification not allowed");
16189 nested_name_specifier = NULL_TREE;
16193 /* An explicit-specialization must be preceded by "template <>". If
16194 it is not, try to recover gracefully. */
16195 if (at_namespace_scope_p ()
16196 && parser->num_template_parameter_lists == 0
16199 error_at (type_start_token->location,
16200 "an explicit specialization must be preceded by %<template <>%>");
16201 invalid_explicit_specialization_p = true;
16202 /* Take the same action that would have been taken by
16203 cp_parser_explicit_specialization. */
16204 ++parser->num_template_parameter_lists;
16205 begin_specialization ();
16207 /* There must be no "return" statements between this point and the
16208 end of this function; set "type "to the correct return value and
16209 use "goto done;" to return. */
16210 /* Make sure that the right number of template parameters were
16212 if (!cp_parser_check_template_parameters (parser, num_templates,
16213 type_start_token->location,
16214 /*declarator=*/NULL))
16216 /* If something went wrong, there is no point in even trying to
16217 process the class-definition. */
16222 /* Look up the type. */
16225 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16226 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16227 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16229 error_at (type_start_token->location,
16230 "function template %qD redeclared as a class template", id);
16231 type = error_mark_node;
16235 type = TREE_TYPE (id);
16236 type = maybe_process_partial_specialization (type);
16238 if (nested_name_specifier)
16239 pushed_scope = push_scope (nested_name_specifier);
16241 else if (nested_name_specifier)
16247 template <typename T> struct S { struct T };
16248 template <typename T> struct S<T>::T { };
16250 we will get a TYPENAME_TYPE when processing the definition of
16251 `S::T'. We need to resolve it to the actual type before we
16252 try to define it. */
16253 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16255 class_type = resolve_typename_type (TREE_TYPE (type),
16256 /*only_current_p=*/false);
16257 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16258 type = TYPE_NAME (class_type);
16261 cp_parser_error (parser, "could not resolve typename type");
16262 type = error_mark_node;
16266 if (maybe_process_partial_specialization (TREE_TYPE (type))
16267 == error_mark_node)
16273 class_type = current_class_type;
16274 /* Enter the scope indicated by the nested-name-specifier. */
16275 pushed_scope = push_scope (nested_name_specifier);
16276 /* Get the canonical version of this type. */
16277 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16278 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16279 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16281 type = push_template_decl (type);
16282 if (type == error_mark_node)
16289 type = TREE_TYPE (type);
16290 *nested_name_specifier_p = true;
16292 else /* The name is not a nested name. */
16294 /* If the class was unnamed, create a dummy name. */
16296 id = make_anon_name ();
16297 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16298 parser->num_template_parameter_lists);
16301 /* Indicate whether this class was declared as a `class' or as a
16303 if (TREE_CODE (type) == RECORD_TYPE)
16304 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16305 cp_parser_check_class_key (class_key, type);
16307 /* If this type was already complete, and we see another definition,
16308 that's an error. */
16309 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16311 error_at (type_start_token->location, "redefinition of %q#T",
16313 error_at (type_start_token->location, "previous definition of %q+#T",
16318 else if (type == error_mark_node)
16321 /* We will have entered the scope containing the class; the names of
16322 base classes should be looked up in that context. For example:
16324 struct A { struct B {}; struct C; };
16325 struct A::C : B {};
16329 /* Get the list of base-classes, if there is one. */
16330 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16331 *bases = cp_parser_base_clause (parser);
16334 /* Leave the scope given by the nested-name-specifier. We will
16335 enter the class scope itself while processing the members. */
16337 pop_scope (pushed_scope);
16339 if (invalid_explicit_specialization_p)
16341 end_specialization ();
16342 --parser->num_template_parameter_lists;
16344 *attributes_p = attributes;
16348 /* Parse a class-key.
16355 Returns the kind of class-key specified, or none_type to indicate
16358 static enum tag_types
16359 cp_parser_class_key (cp_parser* parser)
16362 enum tag_types tag_type;
16364 /* Look for the class-key. */
16365 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16369 /* Check to see if the TOKEN is a class-key. */
16370 tag_type = cp_parser_token_is_class_key (token);
16372 cp_parser_error (parser, "expected class-key");
16376 /* Parse an (optional) member-specification.
16378 member-specification:
16379 member-declaration member-specification [opt]
16380 access-specifier : member-specification [opt] */
16383 cp_parser_member_specification_opt (cp_parser* parser)
16390 /* Peek at the next token. */
16391 token = cp_lexer_peek_token (parser->lexer);
16392 /* If it's a `}', or EOF then we've seen all the members. */
16393 if (token->type == CPP_CLOSE_BRACE
16394 || token->type == CPP_EOF
16395 || token->type == CPP_PRAGMA_EOL)
16398 /* See if this token is a keyword. */
16399 keyword = token->keyword;
16403 case RID_PROTECTED:
16405 /* Consume the access-specifier. */
16406 cp_lexer_consume_token (parser->lexer);
16407 /* Remember which access-specifier is active. */
16408 current_access_specifier = token->u.value;
16409 /* Look for the `:'. */
16410 cp_parser_require (parser, CPP_COLON, "%<:%>");
16414 /* Accept #pragmas at class scope. */
16415 if (token->type == CPP_PRAGMA)
16417 cp_parser_pragma (parser, pragma_external);
16421 /* Otherwise, the next construction must be a
16422 member-declaration. */
16423 cp_parser_member_declaration (parser);
16428 /* Parse a member-declaration.
16430 member-declaration:
16431 decl-specifier-seq [opt] member-declarator-list [opt] ;
16432 function-definition ; [opt]
16433 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16435 template-declaration
16437 member-declarator-list:
16439 member-declarator-list , member-declarator
16442 declarator pure-specifier [opt]
16443 declarator constant-initializer [opt]
16444 identifier [opt] : constant-expression
16448 member-declaration:
16449 __extension__ member-declaration
16452 declarator attributes [opt] pure-specifier [opt]
16453 declarator attributes [opt] constant-initializer [opt]
16454 identifier [opt] attributes [opt] : constant-expression
16458 member-declaration:
16459 static_assert-declaration */
16462 cp_parser_member_declaration (cp_parser* parser)
16464 cp_decl_specifier_seq decl_specifiers;
16465 tree prefix_attributes;
16467 int declares_class_or_enum;
16469 cp_token *token = NULL;
16470 cp_token *decl_spec_token_start = NULL;
16471 cp_token *initializer_token_start = NULL;
16472 int saved_pedantic;
16474 /* Check for the `__extension__' keyword. */
16475 if (cp_parser_extension_opt (parser, &saved_pedantic))
16478 cp_parser_member_declaration (parser);
16479 /* Restore the old value of the PEDANTIC flag. */
16480 pedantic = saved_pedantic;
16485 /* Check for a template-declaration. */
16486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16488 /* An explicit specialization here is an error condition, and we
16489 expect the specialization handler to detect and report this. */
16490 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16491 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16492 cp_parser_explicit_specialization (parser);
16494 cp_parser_template_declaration (parser, /*member_p=*/true);
16499 /* Check for a using-declaration. */
16500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16502 /* Parse the using-declaration. */
16503 cp_parser_using_declaration (parser,
16504 /*access_declaration_p=*/false);
16508 /* Check for @defs. */
16509 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16512 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16513 ivar = ivar_chains;
16517 ivar = TREE_CHAIN (member);
16518 TREE_CHAIN (member) = NULL_TREE;
16519 finish_member_declaration (member);
16524 /* If the next token is `static_assert' we have a static assertion. */
16525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16527 cp_parser_static_assert (parser, /*member_p=*/true);
16531 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16534 /* Parse the decl-specifier-seq. */
16535 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16536 cp_parser_decl_specifier_seq (parser,
16537 CP_PARSER_FLAGS_OPTIONAL,
16539 &declares_class_or_enum);
16540 prefix_attributes = decl_specifiers.attributes;
16541 decl_specifiers.attributes = NULL_TREE;
16542 /* Check for an invalid type-name. */
16543 if (!decl_specifiers.any_type_specifiers_p
16544 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16546 /* If there is no declarator, then the decl-specifier-seq should
16548 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16550 /* If there was no decl-specifier-seq, and the next token is a
16551 `;', then we have something like:
16557 Each member-declaration shall declare at least one member
16558 name of the class. */
16559 if (!decl_specifiers.any_specifiers_p)
16561 cp_token *token = cp_lexer_peek_token (parser->lexer);
16562 if (!in_system_header_at (token->location))
16563 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16569 /* See if this declaration is a friend. */
16570 friend_p = cp_parser_friend_p (&decl_specifiers);
16571 /* If there were decl-specifiers, check to see if there was
16572 a class-declaration. */
16573 type = check_tag_decl (&decl_specifiers);
16574 /* Nested classes have already been added to the class, but
16575 a `friend' needs to be explicitly registered. */
16578 /* If the `friend' keyword was present, the friend must
16579 be introduced with a class-key. */
16580 if (!declares_class_or_enum)
16581 error_at (decl_spec_token_start->location,
16582 "a class-key must be used when declaring a friend");
16585 template <typename T> struct A {
16586 friend struct A<T>::B;
16589 A<T>::B will be represented by a TYPENAME_TYPE, and
16590 therefore not recognized by check_tag_decl. */
16592 && decl_specifiers.type
16593 && TYPE_P (decl_specifiers.type))
16594 type = decl_specifiers.type;
16595 if (!type || !TYPE_P (type))
16596 error_at (decl_spec_token_start->location,
16597 "friend declaration does not name a class or "
16600 make_friend_class (current_class_type, type,
16601 /*complain=*/true);
16603 /* If there is no TYPE, an error message will already have
16605 else if (!type || type == error_mark_node)
16607 /* An anonymous aggregate has to be handled specially; such
16608 a declaration really declares a data member (with a
16609 particular type), as opposed to a nested class. */
16610 else if (ANON_AGGR_TYPE_P (type))
16612 /* Remove constructors and such from TYPE, now that we
16613 know it is an anonymous aggregate. */
16614 fixup_anonymous_aggr (type);
16615 /* And make the corresponding data member. */
16616 decl = build_decl (decl_spec_token_start->location,
16617 FIELD_DECL, NULL_TREE, type);
16618 /* Add it to the class. */
16619 finish_member_declaration (decl);
16622 cp_parser_check_access_in_redeclaration
16624 decl_spec_token_start->location);
16629 /* See if these declarations will be friends. */
16630 friend_p = cp_parser_friend_p (&decl_specifiers);
16632 /* Keep going until we hit the `;' at the end of the
16634 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16636 tree attributes = NULL_TREE;
16637 tree first_attribute;
16639 /* Peek at the next token. */
16640 token = cp_lexer_peek_token (parser->lexer);
16642 /* Check for a bitfield declaration. */
16643 if (token->type == CPP_COLON
16644 || (token->type == CPP_NAME
16645 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16651 /* Get the name of the bitfield. Note that we cannot just
16652 check TOKEN here because it may have been invalidated by
16653 the call to cp_lexer_peek_nth_token above. */
16654 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16655 identifier = cp_parser_identifier (parser);
16657 identifier = NULL_TREE;
16659 /* Consume the `:' token. */
16660 cp_lexer_consume_token (parser->lexer);
16661 /* Get the width of the bitfield. */
16663 = cp_parser_constant_expression (parser,
16664 /*allow_non_constant=*/false,
16667 /* Look for attributes that apply to the bitfield. */
16668 attributes = cp_parser_attributes_opt (parser);
16669 /* Remember which attributes are prefix attributes and
16671 first_attribute = attributes;
16672 /* Combine the attributes. */
16673 attributes = chainon (prefix_attributes, attributes);
16675 /* Create the bitfield declaration. */
16676 decl = grokbitfield (identifier
16677 ? make_id_declarator (NULL_TREE,
16687 cp_declarator *declarator;
16689 tree asm_specification;
16690 int ctor_dtor_or_conv_p;
16692 /* Parse the declarator. */
16694 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16695 &ctor_dtor_or_conv_p,
16696 /*parenthesized_p=*/NULL,
16697 /*member_p=*/true);
16699 /* If something went wrong parsing the declarator, make sure
16700 that we at least consume some tokens. */
16701 if (declarator == cp_error_declarator)
16703 /* Skip to the end of the statement. */
16704 cp_parser_skip_to_end_of_statement (parser);
16705 /* If the next token is not a semicolon, that is
16706 probably because we just skipped over the body of
16707 a function. So, we consume a semicolon if
16708 present, but do not issue an error message if it
16710 if (cp_lexer_next_token_is (parser->lexer,
16712 cp_lexer_consume_token (parser->lexer);
16716 if (declares_class_or_enum & 2)
16717 cp_parser_check_for_definition_in_return_type
16718 (declarator, decl_specifiers.type,
16719 decl_specifiers.type_location);
16721 /* Look for an asm-specification. */
16722 asm_specification = cp_parser_asm_specification_opt (parser);
16723 /* Look for attributes that apply to the declaration. */
16724 attributes = cp_parser_attributes_opt (parser);
16725 /* Remember which attributes are prefix attributes and
16727 first_attribute = attributes;
16728 /* Combine the attributes. */
16729 attributes = chainon (prefix_attributes, attributes);
16731 /* If it's an `=', then we have a constant-initializer or a
16732 pure-specifier. It is not correct to parse the
16733 initializer before registering the member declaration
16734 since the member declaration should be in scope while
16735 its initializer is processed. However, the rest of the
16736 front end does not yet provide an interface that allows
16737 us to handle this correctly. */
16738 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16742 A pure-specifier shall be used only in the declaration of
16743 a virtual function.
16745 A member-declarator can contain a constant-initializer
16746 only if it declares a static member of integral or
16749 Therefore, if the DECLARATOR is for a function, we look
16750 for a pure-specifier; otherwise, we look for a
16751 constant-initializer. When we call `grokfield', it will
16752 perform more stringent semantics checks. */
16753 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16754 if (function_declarator_p (declarator))
16755 initializer = cp_parser_pure_specifier (parser);
16757 /* Parse the initializer. */
16758 initializer = cp_parser_constant_initializer (parser);
16760 /* Otherwise, there is no initializer. */
16762 initializer = NULL_TREE;
16764 /* See if we are probably looking at a function
16765 definition. We are certainly not looking at a
16766 member-declarator. Calling `grokfield' has
16767 side-effects, so we must not do it unless we are sure
16768 that we are looking at a member-declarator. */
16769 if (cp_parser_token_starts_function_definition_p
16770 (cp_lexer_peek_token (parser->lexer)))
16772 /* The grammar does not allow a pure-specifier to be
16773 used when a member function is defined. (It is
16774 possible that this fact is an oversight in the
16775 standard, since a pure function may be defined
16776 outside of the class-specifier. */
16778 error_at (initializer_token_start->location,
16779 "pure-specifier on function-definition");
16780 decl = cp_parser_save_member_function_body (parser,
16784 /* If the member was not a friend, declare it here. */
16786 finish_member_declaration (decl);
16787 /* Peek at the next token. */
16788 token = cp_lexer_peek_token (parser->lexer);
16789 /* If the next token is a semicolon, consume it. */
16790 if (token->type == CPP_SEMICOLON)
16791 cp_lexer_consume_token (parser->lexer);
16795 if (declarator->kind == cdk_function)
16796 declarator->id_loc = token->location;
16797 /* Create the declaration. */
16798 decl = grokfield (declarator, &decl_specifiers,
16799 initializer, /*init_const_expr_p=*/true,
16804 /* Reset PREFIX_ATTRIBUTES. */
16805 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16806 attributes = TREE_CHAIN (attributes);
16808 TREE_CHAIN (attributes) = NULL_TREE;
16810 /* If there is any qualification still in effect, clear it
16811 now; we will be starting fresh with the next declarator. */
16812 parser->scope = NULL_TREE;
16813 parser->qualifying_scope = NULL_TREE;
16814 parser->object_scope = NULL_TREE;
16815 /* If it's a `,', then there are more declarators. */
16816 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16817 cp_lexer_consume_token (parser->lexer);
16818 /* If the next token isn't a `;', then we have a parse error. */
16819 else if (cp_lexer_next_token_is_not (parser->lexer,
16822 cp_parser_error (parser, "expected %<;%>");
16823 /* Skip tokens until we find a `;'. */
16824 cp_parser_skip_to_end_of_statement (parser);
16831 /* Add DECL to the list of members. */
16833 finish_member_declaration (decl);
16835 if (TREE_CODE (decl) == FUNCTION_DECL)
16836 cp_parser_save_default_args (parser, decl);
16841 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16844 /* Parse a pure-specifier.
16849 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16850 Otherwise, ERROR_MARK_NODE is returned. */
16853 cp_parser_pure_specifier (cp_parser* parser)
16857 /* Look for the `=' token. */
16858 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16859 return error_mark_node;
16860 /* Look for the `0' token. */
16861 token = cp_lexer_peek_token (parser->lexer);
16863 if (token->type == CPP_EOF
16864 || token->type == CPP_PRAGMA_EOL)
16865 return error_mark_node;
16867 cp_lexer_consume_token (parser->lexer);
16869 /* Accept = default or = delete in c++0x mode. */
16870 if (token->keyword == RID_DEFAULT
16871 || token->keyword == RID_DELETE)
16873 maybe_warn_cpp0x ("defaulted and deleted functions");
16874 return token->u.value;
16877 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16878 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16880 cp_parser_error (parser,
16881 "invalid pure specifier (only %<= 0%> is allowed)");
16882 cp_parser_skip_to_end_of_statement (parser);
16883 return error_mark_node;
16885 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16887 error_at (token->location, "templates may not be %<virtual%>");
16888 return error_mark_node;
16891 return integer_zero_node;
16894 /* Parse a constant-initializer.
16896 constant-initializer:
16897 = constant-expression
16899 Returns a representation of the constant-expression. */
16902 cp_parser_constant_initializer (cp_parser* parser)
16904 /* Look for the `=' token. */
16905 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16906 return error_mark_node;
16908 /* It is invalid to write:
16910 struct S { static const int i = { 7 }; };
16913 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16915 cp_parser_error (parser,
16916 "a brace-enclosed initializer is not allowed here");
16917 /* Consume the opening brace. */
16918 cp_lexer_consume_token (parser->lexer);
16919 /* Skip the initializer. */
16920 cp_parser_skip_to_closing_brace (parser);
16921 /* Look for the trailing `}'. */
16922 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16924 return error_mark_node;
16927 return cp_parser_constant_expression (parser,
16928 /*allow_non_constant=*/false,
16932 /* Derived classes [gram.class.derived] */
16934 /* Parse a base-clause.
16937 : base-specifier-list
16939 base-specifier-list:
16940 base-specifier ... [opt]
16941 base-specifier-list , base-specifier ... [opt]
16943 Returns a TREE_LIST representing the base-classes, in the order in
16944 which they were declared. The representation of each node is as
16945 described by cp_parser_base_specifier.
16947 In the case that no bases are specified, this function will return
16948 NULL_TREE, not ERROR_MARK_NODE. */
16951 cp_parser_base_clause (cp_parser* parser)
16953 tree bases = NULL_TREE;
16955 /* Look for the `:' that begins the list. */
16956 cp_parser_require (parser, CPP_COLON, "%<:%>");
16958 /* Scan the base-specifier-list. */
16963 bool pack_expansion_p = false;
16965 /* Look for the base-specifier. */
16966 base = cp_parser_base_specifier (parser);
16967 /* Look for the (optional) ellipsis. */
16968 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16970 /* Consume the `...'. */
16971 cp_lexer_consume_token (parser->lexer);
16973 pack_expansion_p = true;
16976 /* Add BASE to the front of the list. */
16977 if (base != error_mark_node)
16979 if (pack_expansion_p)
16980 /* Make this a pack expansion type. */
16981 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16984 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16986 TREE_CHAIN (base) = bases;
16990 /* Peek at the next token. */
16991 token = cp_lexer_peek_token (parser->lexer);
16992 /* If it's not a comma, then the list is complete. */
16993 if (token->type != CPP_COMMA)
16995 /* Consume the `,'. */
16996 cp_lexer_consume_token (parser->lexer);
16999 /* PARSER->SCOPE may still be non-NULL at this point, if the last
17000 base class had a qualified name. However, the next name that
17001 appears is certainly not qualified. */
17002 parser->scope = NULL_TREE;
17003 parser->qualifying_scope = NULL_TREE;
17004 parser->object_scope = NULL_TREE;
17006 return nreverse (bases);
17009 /* Parse a base-specifier.
17012 :: [opt] nested-name-specifier [opt] class-name
17013 virtual access-specifier [opt] :: [opt] nested-name-specifier
17015 access-specifier virtual [opt] :: [opt] nested-name-specifier
17018 Returns a TREE_LIST. The TREE_PURPOSE will be one of
17019 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17020 indicate the specifiers provided. The TREE_VALUE will be a TYPE
17021 (or the ERROR_MARK_NODE) indicating the type that was specified. */
17024 cp_parser_base_specifier (cp_parser* parser)
17028 bool virtual_p = false;
17029 bool duplicate_virtual_error_issued_p = false;
17030 bool duplicate_access_error_issued_p = false;
17031 bool class_scope_p, template_p;
17032 tree access = access_default_node;
17035 /* Process the optional `virtual' and `access-specifier'. */
17038 /* Peek at the next token. */
17039 token = cp_lexer_peek_token (parser->lexer);
17040 /* Process `virtual'. */
17041 switch (token->keyword)
17044 /* If `virtual' appears more than once, issue an error. */
17045 if (virtual_p && !duplicate_virtual_error_issued_p)
17047 cp_parser_error (parser,
17048 "%<virtual%> specified more than once in base-specified");
17049 duplicate_virtual_error_issued_p = true;
17054 /* Consume the `virtual' token. */
17055 cp_lexer_consume_token (parser->lexer);
17060 case RID_PROTECTED:
17062 /* If more than one access specifier appears, issue an
17064 if (access != access_default_node
17065 && !duplicate_access_error_issued_p)
17067 cp_parser_error (parser,
17068 "more than one access specifier in base-specified");
17069 duplicate_access_error_issued_p = true;
17072 access = ridpointers[(int) token->keyword];
17074 /* Consume the access-specifier. */
17075 cp_lexer_consume_token (parser->lexer);
17084 /* It is not uncommon to see programs mechanically, erroneously, use
17085 the 'typename' keyword to denote (dependent) qualified types
17086 as base classes. */
17087 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17089 token = cp_lexer_peek_token (parser->lexer);
17090 if (!processing_template_decl)
17091 error_at (token->location,
17092 "keyword %<typename%> not allowed outside of templates");
17094 error_at (token->location,
17095 "keyword %<typename%> not allowed in this context "
17096 "(the base class is implicitly a type)");
17097 cp_lexer_consume_token (parser->lexer);
17100 /* Look for the optional `::' operator. */
17101 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17102 /* Look for the nested-name-specifier. The simplest way to
17107 The keyword `typename' is not permitted in a base-specifier or
17108 mem-initializer; in these contexts a qualified name that
17109 depends on a template-parameter is implicitly assumed to be a
17112 is to pretend that we have seen the `typename' keyword at this
17114 cp_parser_nested_name_specifier_opt (parser,
17115 /*typename_keyword_p=*/true,
17116 /*check_dependency_p=*/true,
17118 /*is_declaration=*/true);
17119 /* If the base class is given by a qualified name, assume that names
17120 we see are type names or templates, as appropriate. */
17121 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17122 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17124 /* Finally, look for the class-name. */
17125 type = cp_parser_class_name (parser,
17129 /*check_dependency_p=*/true,
17130 /*class_head_p=*/false,
17131 /*is_declaration=*/true);
17133 if (type == error_mark_node)
17134 return error_mark_node;
17136 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17139 /* Exception handling [gram.exception] */
17141 /* Parse an (optional) exception-specification.
17143 exception-specification:
17144 throw ( type-id-list [opt] )
17146 Returns a TREE_LIST representing the exception-specification. The
17147 TREE_VALUE of each node is a type. */
17150 cp_parser_exception_specification_opt (cp_parser* parser)
17155 /* Peek at the next token. */
17156 token = cp_lexer_peek_token (parser->lexer);
17157 /* If it's not `throw', then there's no exception-specification. */
17158 if (!cp_parser_is_keyword (token, RID_THROW))
17161 /* Consume the `throw'. */
17162 cp_lexer_consume_token (parser->lexer);
17164 /* Look for the `('. */
17165 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17167 /* Peek at the next token. */
17168 token = cp_lexer_peek_token (parser->lexer);
17169 /* If it's not a `)', then there is a type-id-list. */
17170 if (token->type != CPP_CLOSE_PAREN)
17172 const char *saved_message;
17174 /* Types may not be defined in an exception-specification. */
17175 saved_message = parser->type_definition_forbidden_message;
17176 parser->type_definition_forbidden_message
17177 = "types may not be defined in an exception-specification";
17178 /* Parse the type-id-list. */
17179 type_id_list = cp_parser_type_id_list (parser);
17180 /* Restore the saved message. */
17181 parser->type_definition_forbidden_message = saved_message;
17184 type_id_list = empty_except_spec;
17186 /* Look for the `)'. */
17187 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17189 return type_id_list;
17192 /* Parse an (optional) type-id-list.
17196 type-id-list , type-id ... [opt]
17198 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17199 in the order that the types were presented. */
17202 cp_parser_type_id_list (cp_parser* parser)
17204 tree types = NULL_TREE;
17211 /* Get the next type-id. */
17212 type = cp_parser_type_id (parser);
17213 /* Parse the optional ellipsis. */
17214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17216 /* Consume the `...'. */
17217 cp_lexer_consume_token (parser->lexer);
17219 /* Turn the type into a pack expansion expression. */
17220 type = make_pack_expansion (type);
17222 /* Add it to the list. */
17223 types = add_exception_specifier (types, type, /*complain=*/1);
17224 /* Peek at the next token. */
17225 token = cp_lexer_peek_token (parser->lexer);
17226 /* If it is not a `,', we are done. */
17227 if (token->type != CPP_COMMA)
17229 /* Consume the `,'. */
17230 cp_lexer_consume_token (parser->lexer);
17233 return nreverse (types);
17236 /* Parse a try-block.
17239 try compound-statement handler-seq */
17242 cp_parser_try_block (cp_parser* parser)
17246 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17247 try_block = begin_try_block ();
17248 cp_parser_compound_statement (parser, NULL, true);
17249 finish_try_block (try_block);
17250 cp_parser_handler_seq (parser);
17251 finish_handler_sequence (try_block);
17256 /* Parse a function-try-block.
17258 function-try-block:
17259 try ctor-initializer [opt] function-body handler-seq */
17262 cp_parser_function_try_block (cp_parser* parser)
17264 tree compound_stmt;
17266 bool ctor_initializer_p;
17268 /* Look for the `try' keyword. */
17269 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17271 /* Let the rest of the front end know where we are. */
17272 try_block = begin_function_try_block (&compound_stmt);
17273 /* Parse the function-body. */
17275 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17276 /* We're done with the `try' part. */
17277 finish_function_try_block (try_block);
17278 /* Parse the handlers. */
17279 cp_parser_handler_seq (parser);
17280 /* We're done with the handlers. */
17281 finish_function_handler_sequence (try_block, compound_stmt);
17283 return ctor_initializer_p;
17286 /* Parse a handler-seq.
17289 handler handler-seq [opt] */
17292 cp_parser_handler_seq (cp_parser* parser)
17298 /* Parse the handler. */
17299 cp_parser_handler (parser);
17300 /* Peek at the next token. */
17301 token = cp_lexer_peek_token (parser->lexer);
17302 /* If it's not `catch' then there are no more handlers. */
17303 if (!cp_parser_is_keyword (token, RID_CATCH))
17308 /* Parse a handler.
17311 catch ( exception-declaration ) compound-statement */
17314 cp_parser_handler (cp_parser* parser)
17319 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17320 handler = begin_handler ();
17321 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17322 declaration = cp_parser_exception_declaration (parser);
17323 finish_handler_parms (declaration, handler);
17324 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17325 cp_parser_compound_statement (parser, NULL, false);
17326 finish_handler (handler);
17329 /* Parse an exception-declaration.
17331 exception-declaration:
17332 type-specifier-seq declarator
17333 type-specifier-seq abstract-declarator
17337 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17338 ellipsis variant is used. */
17341 cp_parser_exception_declaration (cp_parser* parser)
17343 cp_decl_specifier_seq type_specifiers;
17344 cp_declarator *declarator;
17345 const char *saved_message;
17347 /* If it's an ellipsis, it's easy to handle. */
17348 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17350 /* Consume the `...' token. */
17351 cp_lexer_consume_token (parser->lexer);
17355 /* Types may not be defined in exception-declarations. */
17356 saved_message = parser->type_definition_forbidden_message;
17357 parser->type_definition_forbidden_message
17358 = "types may not be defined in exception-declarations";
17360 /* Parse the type-specifier-seq. */
17361 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17362 /*is_trailing_return=*/false,
17364 /* If it's a `)', then there is no declarator. */
17365 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17368 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17369 /*ctor_dtor_or_conv_p=*/NULL,
17370 /*parenthesized_p=*/NULL,
17371 /*member_p=*/false);
17373 /* Restore the saved message. */
17374 parser->type_definition_forbidden_message = saved_message;
17376 if (!type_specifiers.any_specifiers_p)
17377 return error_mark_node;
17379 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17382 /* Parse a throw-expression.
17385 throw assignment-expression [opt]
17387 Returns a THROW_EXPR representing the throw-expression. */
17390 cp_parser_throw_expression (cp_parser* parser)
17395 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17396 token = cp_lexer_peek_token (parser->lexer);
17397 /* Figure out whether or not there is an assignment-expression
17398 following the "throw" keyword. */
17399 if (token->type == CPP_COMMA
17400 || token->type == CPP_SEMICOLON
17401 || token->type == CPP_CLOSE_PAREN
17402 || token->type == CPP_CLOSE_SQUARE
17403 || token->type == CPP_CLOSE_BRACE
17404 || token->type == CPP_COLON)
17405 expression = NULL_TREE;
17407 expression = cp_parser_assignment_expression (parser,
17408 /*cast_p=*/false, NULL);
17410 return build_throw (expression);
17413 /* GNU Extensions */
17415 /* Parse an (optional) asm-specification.
17418 asm ( string-literal )
17420 If the asm-specification is present, returns a STRING_CST
17421 corresponding to the string-literal. Otherwise, returns
17425 cp_parser_asm_specification_opt (cp_parser* parser)
17428 tree asm_specification;
17430 /* Peek at the next token. */
17431 token = cp_lexer_peek_token (parser->lexer);
17432 /* If the next token isn't the `asm' keyword, then there's no
17433 asm-specification. */
17434 if (!cp_parser_is_keyword (token, RID_ASM))
17437 /* Consume the `asm' token. */
17438 cp_lexer_consume_token (parser->lexer);
17439 /* Look for the `('. */
17440 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17442 /* Look for the string-literal. */
17443 asm_specification = cp_parser_string_literal (parser, false, false);
17445 /* Look for the `)'. */
17446 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17448 return asm_specification;
17451 /* Parse an asm-operand-list.
17455 asm-operand-list , asm-operand
17458 string-literal ( expression )
17459 [ string-literal ] string-literal ( expression )
17461 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17462 each node is the expression. The TREE_PURPOSE is itself a
17463 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17464 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17465 is a STRING_CST for the string literal before the parenthesis. Returns
17466 ERROR_MARK_NODE if any of the operands are invalid. */
17469 cp_parser_asm_operand_list (cp_parser* parser)
17471 tree asm_operands = NULL_TREE;
17472 bool invalid_operands = false;
17476 tree string_literal;
17480 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17482 /* Consume the `[' token. */
17483 cp_lexer_consume_token (parser->lexer);
17484 /* Read the operand name. */
17485 name = cp_parser_identifier (parser);
17486 if (name != error_mark_node)
17487 name = build_string (IDENTIFIER_LENGTH (name),
17488 IDENTIFIER_POINTER (name));
17489 /* Look for the closing `]'. */
17490 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17494 /* Look for the string-literal. */
17495 string_literal = cp_parser_string_literal (parser, false, false);
17497 /* Look for the `('. */
17498 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17499 /* Parse the expression. */
17500 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17501 /* Look for the `)'. */
17502 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17504 if (name == error_mark_node
17505 || string_literal == error_mark_node
17506 || expression == error_mark_node)
17507 invalid_operands = true;
17509 /* Add this operand to the list. */
17510 asm_operands = tree_cons (build_tree_list (name, string_literal),
17513 /* If the next token is not a `,', there are no more
17515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17517 /* Consume the `,'. */
17518 cp_lexer_consume_token (parser->lexer);
17521 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17524 /* Parse an asm-clobber-list.
17528 asm-clobber-list , string-literal
17530 Returns a TREE_LIST, indicating the clobbers in the order that they
17531 appeared. The TREE_VALUE of each node is a STRING_CST. */
17534 cp_parser_asm_clobber_list (cp_parser* parser)
17536 tree clobbers = NULL_TREE;
17540 tree string_literal;
17542 /* Look for the string literal. */
17543 string_literal = cp_parser_string_literal (parser, false, false);
17544 /* Add it to the list. */
17545 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17546 /* If the next token is not a `,', then the list is
17548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17550 /* Consume the `,' token. */
17551 cp_lexer_consume_token (parser->lexer);
17557 /* Parse an asm-label-list.
17561 asm-label-list , identifier
17563 Returns a TREE_LIST, indicating the labels in the order that they
17564 appeared. The TREE_VALUE of each node is a label. */
17567 cp_parser_asm_label_list (cp_parser* parser)
17569 tree labels = NULL_TREE;
17573 tree identifier, label, name;
17575 /* Look for the identifier. */
17576 identifier = cp_parser_identifier (parser);
17577 if (!error_operand_p (identifier))
17579 label = lookup_label (identifier);
17580 if (TREE_CODE (label) == LABEL_DECL)
17582 TREE_USED (label) = 1;
17583 check_goto (label);
17584 name = build_string (IDENTIFIER_LENGTH (identifier),
17585 IDENTIFIER_POINTER (identifier));
17586 labels = tree_cons (name, label, labels);
17589 /* If the next token is not a `,', then the list is
17591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17593 /* Consume the `,' token. */
17594 cp_lexer_consume_token (parser->lexer);
17597 return nreverse (labels);
17600 /* Parse an (optional) series of attributes.
17603 attributes attribute
17606 __attribute__ (( attribute-list [opt] ))
17608 The return value is as for cp_parser_attribute_list. */
17611 cp_parser_attributes_opt (cp_parser* parser)
17613 tree attributes = NULL_TREE;
17618 tree attribute_list;
17620 /* Peek at the next token. */
17621 token = cp_lexer_peek_token (parser->lexer);
17622 /* If it's not `__attribute__', then we're done. */
17623 if (token->keyword != RID_ATTRIBUTE)
17626 /* Consume the `__attribute__' keyword. */
17627 cp_lexer_consume_token (parser->lexer);
17628 /* Look for the two `(' tokens. */
17629 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17630 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17632 /* Peek at the next token. */
17633 token = cp_lexer_peek_token (parser->lexer);
17634 if (token->type != CPP_CLOSE_PAREN)
17635 /* Parse the attribute-list. */
17636 attribute_list = cp_parser_attribute_list (parser);
17638 /* If the next token is a `)', then there is no attribute
17640 attribute_list = NULL;
17642 /* Look for the two `)' tokens. */
17643 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17644 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17646 /* Add these new attributes to the list. */
17647 attributes = chainon (attributes, attribute_list);
17653 /* Parse an attribute-list.
17657 attribute-list , attribute
17661 identifier ( identifier )
17662 identifier ( identifier , expression-list )
17663 identifier ( expression-list )
17665 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17666 to an attribute. The TREE_PURPOSE of each node is the identifier
17667 indicating which attribute is in use. The TREE_VALUE represents
17668 the arguments, if any. */
17671 cp_parser_attribute_list (cp_parser* parser)
17673 tree attribute_list = NULL_TREE;
17674 bool save_translate_strings_p = parser->translate_strings_p;
17676 parser->translate_strings_p = false;
17683 /* Look for the identifier. We also allow keywords here; for
17684 example `__attribute__ ((const))' is legal. */
17685 token = cp_lexer_peek_token (parser->lexer);
17686 if (token->type == CPP_NAME
17687 || token->type == CPP_KEYWORD)
17689 tree arguments = NULL_TREE;
17691 /* Consume the token. */
17692 token = cp_lexer_consume_token (parser->lexer);
17694 /* Save away the identifier that indicates which attribute
17696 identifier = (token->type == CPP_KEYWORD)
17697 /* For keywords, use the canonical spelling, not the
17698 parsed identifier. */
17699 ? ridpointers[(int) token->keyword]
17702 attribute = build_tree_list (identifier, NULL_TREE);
17704 /* Peek at the next token. */
17705 token = cp_lexer_peek_token (parser->lexer);
17706 /* If it's an `(', then parse the attribute arguments. */
17707 if (token->type == CPP_OPEN_PAREN)
17710 vec = cp_parser_parenthesized_expression_list
17711 (parser, true, /*cast_p=*/false,
17712 /*allow_expansion_p=*/false,
17713 /*non_constant_p=*/NULL);
17715 arguments = error_mark_node;
17718 arguments = build_tree_list_vec (vec);
17719 release_tree_vector (vec);
17721 /* Save the arguments away. */
17722 TREE_VALUE (attribute) = arguments;
17725 if (arguments != error_mark_node)
17727 /* Add this attribute to the list. */
17728 TREE_CHAIN (attribute) = attribute_list;
17729 attribute_list = attribute;
17732 token = cp_lexer_peek_token (parser->lexer);
17734 /* Now, look for more attributes. If the next token isn't a
17735 `,', we're done. */
17736 if (token->type != CPP_COMMA)
17739 /* Consume the comma and keep going. */
17740 cp_lexer_consume_token (parser->lexer);
17742 parser->translate_strings_p = save_translate_strings_p;
17744 /* We built up the list in reverse order. */
17745 return nreverse (attribute_list);
17748 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17749 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17750 current value of the PEDANTIC flag, regardless of whether or not
17751 the `__extension__' keyword is present. The caller is responsible
17752 for restoring the value of the PEDANTIC flag. */
17755 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17757 /* Save the old value of the PEDANTIC flag. */
17758 *saved_pedantic = pedantic;
17760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17762 /* Consume the `__extension__' token. */
17763 cp_lexer_consume_token (parser->lexer);
17764 /* We're not being pedantic while the `__extension__' keyword is
17774 /* Parse a label declaration.
17777 __label__ label-declarator-seq ;
17779 label-declarator-seq:
17780 identifier , label-declarator-seq
17784 cp_parser_label_declaration (cp_parser* parser)
17786 /* Look for the `__label__' keyword. */
17787 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17793 /* Look for an identifier. */
17794 identifier = cp_parser_identifier (parser);
17795 /* If we failed, stop. */
17796 if (identifier == error_mark_node)
17798 /* Declare it as a label. */
17799 finish_label_decl (identifier);
17800 /* If the next token is a `;', stop. */
17801 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17803 /* Look for the `,' separating the label declarations. */
17804 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17807 /* Look for the final `;'. */
17808 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17811 /* Support Functions */
17813 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17814 NAME should have one of the representations used for an
17815 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17816 is returned. If PARSER->SCOPE is a dependent type, then a
17817 SCOPE_REF is returned.
17819 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17820 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17821 was formed. Abstractly, such entities should not be passed to this
17822 function, because they do not need to be looked up, but it is
17823 simpler to check for this special case here, rather than at the
17826 In cases not explicitly covered above, this function returns a
17827 DECL, OVERLOAD, or baselink representing the result of the lookup.
17828 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17831 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17832 (e.g., "struct") that was used. In that case bindings that do not
17833 refer to types are ignored.
17835 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17838 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17841 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17844 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17845 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17846 NULL_TREE otherwise. */
17849 cp_parser_lookup_name (cp_parser *parser, tree name,
17850 enum tag_types tag_type,
17853 bool check_dependency,
17854 tree *ambiguous_decls,
17855 location_t name_location)
17859 tree object_type = parser->context->object_type;
17861 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17862 flags |= LOOKUP_COMPLAIN;
17864 /* Assume that the lookup will be unambiguous. */
17865 if (ambiguous_decls)
17866 *ambiguous_decls = NULL_TREE;
17868 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17869 no longer valid. Note that if we are parsing tentatively, and
17870 the parse fails, OBJECT_TYPE will be automatically restored. */
17871 parser->context->object_type = NULL_TREE;
17873 if (name == error_mark_node)
17874 return error_mark_node;
17876 /* A template-id has already been resolved; there is no lookup to
17878 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17880 if (BASELINK_P (name))
17882 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17883 == TEMPLATE_ID_EXPR);
17887 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17888 it should already have been checked to make sure that the name
17889 used matches the type being destroyed. */
17890 if (TREE_CODE (name) == BIT_NOT_EXPR)
17894 /* Figure out to which type this destructor applies. */
17896 type = parser->scope;
17897 else if (object_type)
17898 type = object_type;
17900 type = current_class_type;
17901 /* If that's not a class type, there is no destructor. */
17902 if (!type || !CLASS_TYPE_P (type))
17903 return error_mark_node;
17904 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17905 lazily_declare_fn (sfk_destructor, type);
17906 if (!CLASSTYPE_DESTRUCTORS (type))
17907 return error_mark_node;
17908 /* If it was a class type, return the destructor. */
17909 return CLASSTYPE_DESTRUCTORS (type);
17912 /* By this point, the NAME should be an ordinary identifier. If
17913 the id-expression was a qualified name, the qualifying scope is
17914 stored in PARSER->SCOPE at this point. */
17915 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17917 /* Perform the lookup. */
17922 if (parser->scope == error_mark_node)
17923 return error_mark_node;
17925 /* If the SCOPE is dependent, the lookup must be deferred until
17926 the template is instantiated -- unless we are explicitly
17927 looking up names in uninstantiated templates. Even then, we
17928 cannot look up the name if the scope is not a class type; it
17929 might, for example, be a template type parameter. */
17930 dependent_p = (TYPE_P (parser->scope)
17931 && dependent_scope_p (parser->scope));
17932 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17934 /* Defer lookup. */
17935 decl = error_mark_node;
17938 tree pushed_scope = NULL_TREE;
17940 /* If PARSER->SCOPE is a dependent type, then it must be a
17941 class type, and we must not be checking dependencies;
17942 otherwise, we would have processed this lookup above. So
17943 that PARSER->SCOPE is not considered a dependent base by
17944 lookup_member, we must enter the scope here. */
17946 pushed_scope = push_scope (parser->scope);
17947 /* If the PARSER->SCOPE is a template specialization, it
17948 may be instantiated during name lookup. In that case,
17949 errors may be issued. Even if we rollback the current
17950 tentative parse, those errors are valid. */
17951 decl = lookup_qualified_name (parser->scope, name,
17952 tag_type != none_type,
17953 /*complain=*/true);
17955 /* If we have a single function from a using decl, pull it out. */
17956 if (TREE_CODE (decl) == OVERLOAD
17957 && !really_overloaded_fn (decl))
17958 decl = OVL_FUNCTION (decl);
17961 pop_scope (pushed_scope);
17964 /* If the scope is a dependent type and either we deferred lookup or
17965 we did lookup but didn't find the name, rememeber the name. */
17966 if (decl == error_mark_node && TYPE_P (parser->scope)
17967 && dependent_type_p (parser->scope))
17973 /* The resolution to Core Issue 180 says that `struct
17974 A::B' should be considered a type-name, even if `A'
17976 type = make_typename_type (parser->scope, name, tag_type,
17977 /*complain=*/tf_error);
17978 decl = TYPE_NAME (type);
17980 else if (is_template
17981 && (cp_parser_next_token_ends_template_argument_p (parser)
17982 || cp_lexer_next_token_is (parser->lexer,
17984 decl = make_unbound_class_template (parser->scope,
17986 /*complain=*/tf_error);
17988 decl = build_qualified_name (/*type=*/NULL_TREE,
17989 parser->scope, name,
17992 parser->qualifying_scope = parser->scope;
17993 parser->object_scope = NULL_TREE;
17995 else if (object_type)
17997 tree object_decl = NULL_TREE;
17998 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17999 OBJECT_TYPE is not a class. */
18000 if (CLASS_TYPE_P (object_type))
18001 /* If the OBJECT_TYPE is a template specialization, it may
18002 be instantiated during name lookup. In that case, errors
18003 may be issued. Even if we rollback the current tentative
18004 parse, those errors are valid. */
18005 object_decl = lookup_member (object_type,
18008 tag_type != none_type);
18009 /* Look it up in the enclosing context, too. */
18010 decl = lookup_name_real (name, tag_type != none_type,
18012 /*block_p=*/true, is_namespace, flags);
18013 parser->object_scope = object_type;
18014 parser->qualifying_scope = NULL_TREE;
18016 decl = object_decl;
18020 decl = lookup_name_real (name, tag_type != none_type,
18022 /*block_p=*/true, is_namespace, flags);
18023 parser->qualifying_scope = NULL_TREE;
18024 parser->object_scope = NULL_TREE;
18027 /* If the lookup failed, let our caller know. */
18028 if (!decl || decl == error_mark_node)
18029 return error_mark_node;
18031 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
18032 if (TREE_CODE (decl) == TREE_LIST)
18034 if (ambiguous_decls)
18035 *ambiguous_decls = decl;
18036 /* The error message we have to print is too complicated for
18037 cp_parser_error, so we incorporate its actions directly. */
18038 if (!cp_parser_simulate_error (parser))
18040 error_at (name_location, "reference to %qD is ambiguous",
18042 print_candidates (decl);
18044 return error_mark_node;
18047 gcc_assert (DECL_P (decl)
18048 || TREE_CODE (decl) == OVERLOAD
18049 || TREE_CODE (decl) == SCOPE_REF
18050 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18051 || BASELINK_P (decl));
18053 /* If we have resolved the name of a member declaration, check to
18054 see if the declaration is accessible. When the name resolves to
18055 set of overloaded functions, accessibility is checked when
18056 overload resolution is done.
18058 During an explicit instantiation, access is not checked at all,
18059 as per [temp.explicit]. */
18061 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18066 /* Like cp_parser_lookup_name, but for use in the typical case where
18067 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18068 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
18071 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18073 return cp_parser_lookup_name (parser, name,
18075 /*is_template=*/false,
18076 /*is_namespace=*/false,
18077 /*check_dependency=*/true,
18078 /*ambiguous_decls=*/NULL,
18082 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18083 the current context, return the TYPE_DECL. If TAG_NAME_P is
18084 true, the DECL indicates the class being defined in a class-head,
18085 or declared in an elaborated-type-specifier.
18087 Otherwise, return DECL. */
18090 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18092 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18093 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18096 template <typename T> struct B;
18099 template <typename T> struct A::B {};
18101 Similarly, in an elaborated-type-specifier:
18103 namespace N { struct X{}; }
18106 template <typename T> friend struct N::X;
18109 However, if the DECL refers to a class type, and we are in
18110 the scope of the class, then the name lookup automatically
18111 finds the TYPE_DECL created by build_self_reference rather
18112 than a TEMPLATE_DECL. For example, in:
18114 template <class T> struct S {
18118 there is no need to handle such case. */
18120 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18121 return DECL_TEMPLATE_RESULT (decl);
18126 /* If too many, or too few, template-parameter lists apply to the
18127 declarator, issue an error message. Returns TRUE if all went well,
18128 and FALSE otherwise. */
18131 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18132 cp_declarator *declarator,
18133 location_t declarator_location)
18135 unsigned num_templates;
18137 /* We haven't seen any classes that involve template parameters yet. */
18140 switch (declarator->kind)
18143 if (declarator->u.id.qualifying_scope)
18148 scope = declarator->u.id.qualifying_scope;
18149 member = declarator->u.id.unqualified_name;
18151 while (scope && CLASS_TYPE_P (scope))
18153 /* You're supposed to have one `template <...>'
18154 for every template class, but you don't need one
18155 for a full specialization. For example:
18157 template <class T> struct S{};
18158 template <> struct S<int> { void f(); };
18159 void S<int>::f () {}
18161 is correct; there shouldn't be a `template <>' for
18162 the definition of `S<int>::f'. */
18163 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18164 /* If SCOPE does not have template information of any
18165 kind, then it is not a template, nor is it nested
18166 within a template. */
18168 if (explicit_class_specialization_p (scope))
18170 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18173 scope = TYPE_CONTEXT (scope);
18176 else if (TREE_CODE (declarator->u.id.unqualified_name)
18177 == TEMPLATE_ID_EXPR)
18178 /* If the DECLARATOR has the form `X<y>' then it uses one
18179 additional level of template parameters. */
18182 return cp_parser_check_template_parameters
18183 (parser, num_templates, declarator_location, declarator);
18189 case cdk_reference:
18191 return (cp_parser_check_declarator_template_parameters
18192 (parser, declarator->declarator, declarator_location));
18198 gcc_unreachable ();
18203 /* NUM_TEMPLATES were used in the current declaration. If that is
18204 invalid, return FALSE and issue an error messages. Otherwise,
18205 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18206 declarator and we can print more accurate diagnostics. */
18209 cp_parser_check_template_parameters (cp_parser* parser,
18210 unsigned num_templates,
18211 location_t location,
18212 cp_declarator *declarator)
18214 /* If there are the same number of template classes and parameter
18215 lists, that's OK. */
18216 if (parser->num_template_parameter_lists == num_templates)
18218 /* If there are more, but only one more, then we are referring to a
18219 member template. That's OK too. */
18220 if (parser->num_template_parameter_lists == num_templates + 1)
18222 /* If there are more template classes than parameter lists, we have
18225 template <class T> void S<T>::R<T>::f (); */
18226 if (parser->num_template_parameter_lists < num_templates)
18228 if (declarator && !current_function_decl)
18229 error_at (location, "specializing member %<%T::%E%> "
18230 "requires %<template<>%> syntax",
18231 declarator->u.id.qualifying_scope,
18232 declarator->u.id.unqualified_name);
18233 else if (declarator)
18234 error_at (location, "invalid declaration of %<%T::%E%>",
18235 declarator->u.id.qualifying_scope,
18236 declarator->u.id.unqualified_name);
18238 error_at (location, "too few template-parameter-lists");
18241 /* Otherwise, there are too many template parameter lists. We have
18244 template <class T> template <class U> void S::f(); */
18245 error_at (location, "too many template-parameter-lists");
18249 /* Parse an optional `::' token indicating that the following name is
18250 from the global namespace. If so, PARSER->SCOPE is set to the
18251 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18252 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18253 Returns the new value of PARSER->SCOPE, if the `::' token is
18254 present, and NULL_TREE otherwise. */
18257 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18261 /* Peek at the next token. */
18262 token = cp_lexer_peek_token (parser->lexer);
18263 /* If we're looking at a `::' token then we're starting from the
18264 global namespace, not our current location. */
18265 if (token->type == CPP_SCOPE)
18267 /* Consume the `::' token. */
18268 cp_lexer_consume_token (parser->lexer);
18269 /* Set the SCOPE so that we know where to start the lookup. */
18270 parser->scope = global_namespace;
18271 parser->qualifying_scope = global_namespace;
18272 parser->object_scope = NULL_TREE;
18274 return parser->scope;
18276 else if (!current_scope_valid_p)
18278 parser->scope = NULL_TREE;
18279 parser->qualifying_scope = NULL_TREE;
18280 parser->object_scope = NULL_TREE;
18286 /* Returns TRUE if the upcoming token sequence is the start of a
18287 constructor declarator. If FRIEND_P is true, the declarator is
18288 preceded by the `friend' specifier. */
18291 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18293 bool constructor_p;
18294 tree type_decl = NULL_TREE;
18295 bool nested_name_p;
18296 cp_token *next_token;
18298 /* The common case is that this is not a constructor declarator, so
18299 try to avoid doing lots of work if at all possible. It's not
18300 valid declare a constructor at function scope. */
18301 if (parser->in_function_body)
18303 /* And only certain tokens can begin a constructor declarator. */
18304 next_token = cp_lexer_peek_token (parser->lexer);
18305 if (next_token->type != CPP_NAME
18306 && next_token->type != CPP_SCOPE
18307 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18308 && next_token->type != CPP_TEMPLATE_ID)
18311 /* Parse tentatively; we are going to roll back all of the tokens
18313 cp_parser_parse_tentatively (parser);
18314 /* Assume that we are looking at a constructor declarator. */
18315 constructor_p = true;
18317 /* Look for the optional `::' operator. */
18318 cp_parser_global_scope_opt (parser,
18319 /*current_scope_valid_p=*/false);
18320 /* Look for the nested-name-specifier. */
18322 = (cp_parser_nested_name_specifier_opt (parser,
18323 /*typename_keyword_p=*/false,
18324 /*check_dependency_p=*/false,
18326 /*is_declaration=*/false)
18328 /* Outside of a class-specifier, there must be a
18329 nested-name-specifier. */
18330 if (!nested_name_p &&
18331 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18333 constructor_p = false;
18334 /* If we still think that this might be a constructor-declarator,
18335 look for a class-name. */
18340 template <typename T> struct S { S(); };
18341 template <typename T> S<T>::S ();
18343 we must recognize that the nested `S' names a class.
18346 template <typename T> S<T>::S<T> ();
18348 we must recognize that the nested `S' names a template. */
18349 type_decl = cp_parser_class_name (parser,
18350 /*typename_keyword_p=*/false,
18351 /*template_keyword_p=*/false,
18353 /*check_dependency_p=*/false,
18354 /*class_head_p=*/false,
18355 /*is_declaration=*/false);
18356 /* If there was no class-name, then this is not a constructor. */
18357 constructor_p = !cp_parser_error_occurred (parser);
18360 /* If we're still considering a constructor, we have to see a `(',
18361 to begin the parameter-declaration-clause, followed by either a
18362 `)', an `...', or a decl-specifier. We need to check for a
18363 type-specifier to avoid being fooled into thinking that:
18367 is a constructor. (It is actually a function named `f' that
18368 takes one parameter (of type `int') and returns a value of type
18371 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18373 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18374 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18375 /* A parameter declaration begins with a decl-specifier,
18376 which is either the "attribute" keyword, a storage class
18377 specifier, or (usually) a type-specifier. */
18378 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18381 tree pushed_scope = NULL_TREE;
18382 unsigned saved_num_template_parameter_lists;
18384 /* Names appearing in the type-specifier should be looked up
18385 in the scope of the class. */
18386 if (current_class_type)
18390 type = TREE_TYPE (type_decl);
18391 if (TREE_CODE (type) == TYPENAME_TYPE)
18393 type = resolve_typename_type (type,
18394 /*only_current_p=*/false);
18395 if (TREE_CODE (type) == TYPENAME_TYPE)
18397 cp_parser_abort_tentative_parse (parser);
18401 pushed_scope = push_scope (type);
18404 /* Inside the constructor parameter list, surrounding
18405 template-parameter-lists do not apply. */
18406 saved_num_template_parameter_lists
18407 = parser->num_template_parameter_lists;
18408 parser->num_template_parameter_lists = 0;
18410 /* Look for the type-specifier. */
18411 cp_parser_type_specifier (parser,
18412 CP_PARSER_FLAGS_NONE,
18413 /*decl_specs=*/NULL,
18414 /*is_declarator=*/true,
18415 /*declares_class_or_enum=*/NULL,
18416 /*is_cv_qualifier=*/NULL);
18418 parser->num_template_parameter_lists
18419 = saved_num_template_parameter_lists;
18421 /* Leave the scope of the class. */
18423 pop_scope (pushed_scope);
18425 constructor_p = !cp_parser_error_occurred (parser);
18429 constructor_p = false;
18430 /* We did not really want to consume any tokens. */
18431 cp_parser_abort_tentative_parse (parser);
18433 return constructor_p;
18436 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18437 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18438 they must be performed once we are in the scope of the function.
18440 Returns the function defined. */
18443 cp_parser_function_definition_from_specifiers_and_declarator
18444 (cp_parser* parser,
18445 cp_decl_specifier_seq *decl_specifiers,
18447 const cp_declarator *declarator)
18452 /* Begin the function-definition. */
18453 success_p = start_function (decl_specifiers, declarator, attributes);
18455 /* The things we're about to see are not directly qualified by any
18456 template headers we've seen thus far. */
18457 reset_specialization ();
18459 /* If there were names looked up in the decl-specifier-seq that we
18460 did not check, check them now. We must wait until we are in the
18461 scope of the function to perform the checks, since the function
18462 might be a friend. */
18463 perform_deferred_access_checks ();
18467 /* Skip the entire function. */
18468 cp_parser_skip_to_end_of_block_or_statement (parser);
18469 fn = error_mark_node;
18471 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18473 /* Seen already, skip it. An error message has already been output. */
18474 cp_parser_skip_to_end_of_block_or_statement (parser);
18475 fn = current_function_decl;
18476 current_function_decl = NULL_TREE;
18477 /* If this is a function from a class, pop the nested class. */
18478 if (current_class_name)
18479 pop_nested_class ();
18482 fn = cp_parser_function_definition_after_declarator (parser,
18483 /*inline_p=*/false);
18488 /* Parse the part of a function-definition that follows the
18489 declarator. INLINE_P is TRUE iff this function is an inline
18490 function defined within a class-specifier.
18492 Returns the function defined. */
18495 cp_parser_function_definition_after_declarator (cp_parser* parser,
18499 bool ctor_initializer_p = false;
18500 bool saved_in_unbraced_linkage_specification_p;
18501 bool saved_in_function_body;
18502 unsigned saved_num_template_parameter_lists;
18505 saved_in_function_body = parser->in_function_body;
18506 parser->in_function_body = true;
18507 /* If the next token is `return', then the code may be trying to
18508 make use of the "named return value" extension that G++ used to
18510 token = cp_lexer_peek_token (parser->lexer);
18511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18513 /* Consume the `return' keyword. */
18514 cp_lexer_consume_token (parser->lexer);
18515 /* Look for the identifier that indicates what value is to be
18517 cp_parser_identifier (parser);
18518 /* Issue an error message. */
18519 error_at (token->location,
18520 "named return values are no longer supported");
18521 /* Skip tokens until we reach the start of the function body. */
18524 cp_token *token = cp_lexer_peek_token (parser->lexer);
18525 if (token->type == CPP_OPEN_BRACE
18526 || token->type == CPP_EOF
18527 || token->type == CPP_PRAGMA_EOL)
18529 cp_lexer_consume_token (parser->lexer);
18532 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18533 anything declared inside `f'. */
18534 saved_in_unbraced_linkage_specification_p
18535 = parser->in_unbraced_linkage_specification_p;
18536 parser->in_unbraced_linkage_specification_p = false;
18537 /* Inside the function, surrounding template-parameter-lists do not
18539 saved_num_template_parameter_lists
18540 = parser->num_template_parameter_lists;
18541 parser->num_template_parameter_lists = 0;
18543 start_lambda_scope (current_function_decl);
18545 /* If the next token is `try', then we are looking at a
18546 function-try-block. */
18547 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18548 ctor_initializer_p = cp_parser_function_try_block (parser);
18549 /* A function-try-block includes the function-body, so we only do
18550 this next part if we're not processing a function-try-block. */
18553 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18555 finish_lambda_scope ();
18557 /* Finish the function. */
18558 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18559 (inline_p ? 2 : 0));
18560 /* Generate code for it, if necessary. */
18561 expand_or_defer_fn (fn);
18562 /* Restore the saved values. */
18563 parser->in_unbraced_linkage_specification_p
18564 = saved_in_unbraced_linkage_specification_p;
18565 parser->num_template_parameter_lists
18566 = saved_num_template_parameter_lists;
18567 parser->in_function_body = saved_in_function_body;
18572 /* Parse a template-declaration, assuming that the `export' (and
18573 `extern') keywords, if present, has already been scanned. MEMBER_P
18574 is as for cp_parser_template_declaration. */
18577 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18579 tree decl = NULL_TREE;
18580 VEC (deferred_access_check,gc) *checks;
18581 tree parameter_list;
18582 bool friend_p = false;
18583 bool need_lang_pop;
18586 /* Look for the `template' keyword. */
18587 token = cp_lexer_peek_token (parser->lexer);
18588 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18592 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18594 if (at_class_scope_p () && current_function_decl)
18596 /* 14.5.2.2 [temp.mem]
18598 A local class shall not have member templates. */
18599 error_at (token->location,
18600 "invalid declaration of member template in local class");
18601 cp_parser_skip_to_end_of_block_or_statement (parser);
18606 A template ... shall not have C linkage. */
18607 if (current_lang_name == lang_name_c)
18609 error_at (token->location, "template with C linkage");
18610 /* Give it C++ linkage to avoid confusing other parts of the
18612 push_lang_context (lang_name_cplusplus);
18613 need_lang_pop = true;
18616 need_lang_pop = false;
18618 /* We cannot perform access checks on the template parameter
18619 declarations until we know what is being declared, just as we
18620 cannot check the decl-specifier list. */
18621 push_deferring_access_checks (dk_deferred);
18623 /* If the next token is `>', then we have an invalid
18624 specialization. Rather than complain about an invalid template
18625 parameter, issue an error message here. */
18626 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18628 cp_parser_error (parser, "invalid explicit specialization");
18629 begin_specialization ();
18630 parameter_list = NULL_TREE;
18633 /* Parse the template parameters. */
18634 parameter_list = cp_parser_template_parameter_list (parser);
18636 /* Get the deferred access checks from the parameter list. These
18637 will be checked once we know what is being declared, as for a
18638 member template the checks must be performed in the scope of the
18639 class containing the member. */
18640 checks = get_deferred_access_checks ();
18642 /* Look for the `>'. */
18643 cp_parser_skip_to_end_of_template_parameter_list (parser);
18644 /* We just processed one more parameter list. */
18645 ++parser->num_template_parameter_lists;
18646 /* If the next token is `template', there are more template
18648 if (cp_lexer_next_token_is_keyword (parser->lexer,
18650 cp_parser_template_declaration_after_export (parser, member_p);
18653 /* There are no access checks when parsing a template, as we do not
18654 know if a specialization will be a friend. */
18655 push_deferring_access_checks (dk_no_check);
18656 token = cp_lexer_peek_token (parser->lexer);
18657 decl = cp_parser_single_declaration (parser,
18660 /*explicit_specialization_p=*/false,
18662 pop_deferring_access_checks ();
18664 /* If this is a member template declaration, let the front
18666 if (member_p && !friend_p && decl)
18668 if (TREE_CODE (decl) == TYPE_DECL)
18669 cp_parser_check_access_in_redeclaration (decl, token->location);
18671 decl = finish_member_template_decl (decl);
18673 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18674 make_friend_class (current_class_type, TREE_TYPE (decl),
18675 /*complain=*/true);
18677 /* We are done with the current parameter list. */
18678 --parser->num_template_parameter_lists;
18680 pop_deferring_access_checks ();
18683 finish_template_decl (parameter_list);
18685 /* Register member declarations. */
18686 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18687 finish_member_declaration (decl);
18688 /* For the erroneous case of a template with C linkage, we pushed an
18689 implicit C++ linkage scope; exit that scope now. */
18691 pop_lang_context ();
18692 /* If DECL is a function template, we must return to parse it later.
18693 (Even though there is no definition, there might be default
18694 arguments that need handling.) */
18695 if (member_p && decl
18696 && (TREE_CODE (decl) == FUNCTION_DECL
18697 || DECL_FUNCTION_TEMPLATE_P (decl)))
18698 TREE_VALUE (parser->unparsed_functions_queues)
18699 = tree_cons (NULL_TREE, decl,
18700 TREE_VALUE (parser->unparsed_functions_queues));
18703 /* Perform the deferred access checks from a template-parameter-list.
18704 CHECKS is a TREE_LIST of access checks, as returned by
18705 get_deferred_access_checks. */
18708 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18710 ++processing_template_parmlist;
18711 perform_access_checks (checks);
18712 --processing_template_parmlist;
18715 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18716 `function-definition' sequence. MEMBER_P is true, this declaration
18717 appears in a class scope.
18719 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18720 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18723 cp_parser_single_declaration (cp_parser* parser,
18724 VEC (deferred_access_check,gc)* checks,
18726 bool explicit_specialization_p,
18729 int declares_class_or_enum;
18730 tree decl = NULL_TREE;
18731 cp_decl_specifier_seq decl_specifiers;
18732 bool function_definition_p = false;
18733 cp_token *decl_spec_token_start;
18735 /* This function is only used when processing a template
18737 gcc_assert (innermost_scope_kind () == sk_template_parms
18738 || innermost_scope_kind () == sk_template_spec);
18740 /* Defer access checks until we know what is being declared. */
18741 push_deferring_access_checks (dk_deferred);
18743 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18745 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18746 cp_parser_decl_specifier_seq (parser,
18747 CP_PARSER_FLAGS_OPTIONAL,
18749 &declares_class_or_enum);
18751 *friend_p = cp_parser_friend_p (&decl_specifiers);
18753 /* There are no template typedefs. */
18754 if (decl_specifiers.specs[(int) ds_typedef])
18756 error_at (decl_spec_token_start->location,
18757 "template declaration of %<typedef%>");
18758 decl = error_mark_node;
18761 /* Gather up the access checks that occurred the
18762 decl-specifier-seq. */
18763 stop_deferring_access_checks ();
18765 /* Check for the declaration of a template class. */
18766 if (declares_class_or_enum)
18768 if (cp_parser_declares_only_class_p (parser))
18770 decl = shadow_tag (&decl_specifiers);
18775 friend template <typename T> struct A<T>::B;
18778 A<T>::B will be represented by a TYPENAME_TYPE, and
18779 therefore not recognized by shadow_tag. */
18780 if (friend_p && *friend_p
18782 && decl_specifiers.type
18783 && TYPE_P (decl_specifiers.type))
18784 decl = decl_specifiers.type;
18786 if (decl && decl != error_mark_node)
18787 decl = TYPE_NAME (decl);
18789 decl = error_mark_node;
18791 /* Perform access checks for template parameters. */
18792 cp_parser_perform_template_parameter_access_checks (checks);
18796 /* Complain about missing 'typename' or other invalid type names. */
18797 if (!decl_specifiers.any_type_specifiers_p)
18798 cp_parser_parse_and_diagnose_invalid_type_name (parser);
18800 /* If it's not a template class, try for a template function. If
18801 the next token is a `;', then this declaration does not declare
18802 anything. But, if there were errors in the decl-specifiers, then
18803 the error might well have come from an attempted class-specifier.
18804 In that case, there's no need to warn about a missing declarator. */
18806 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18807 || decl_specifiers.type != error_mark_node))
18809 decl = cp_parser_init_declarator (parser,
18812 /*function_definition_allowed_p=*/true,
18814 declares_class_or_enum,
18815 &function_definition_p);
18817 /* 7.1.1-1 [dcl.stc]
18819 A storage-class-specifier shall not be specified in an explicit
18820 specialization... */
18822 && explicit_specialization_p
18823 && decl_specifiers.storage_class != sc_none)
18825 error_at (decl_spec_token_start->location,
18826 "explicit template specialization cannot have a storage class");
18827 decl = error_mark_node;
18831 pop_deferring_access_checks ();
18833 /* Clear any current qualification; whatever comes next is the start
18834 of something new. */
18835 parser->scope = NULL_TREE;
18836 parser->qualifying_scope = NULL_TREE;
18837 parser->object_scope = NULL_TREE;
18838 /* Look for a trailing `;' after the declaration. */
18839 if (!function_definition_p
18840 && (decl == error_mark_node
18841 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18842 cp_parser_skip_to_end_of_block_or_statement (parser);
18847 /* Parse a cast-expression that is not the operand of a unary "&". */
18850 cp_parser_simple_cast_expression (cp_parser *parser)
18852 return cp_parser_cast_expression (parser, /*address_p=*/false,
18853 /*cast_p=*/false, NULL);
18856 /* Parse a functional cast to TYPE. Returns an expression
18857 representing the cast. */
18860 cp_parser_functional_cast (cp_parser* parser, tree type)
18863 tree expression_list;
18867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18869 maybe_warn_cpp0x ("extended initializer lists");
18870 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18871 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18872 if (TREE_CODE (type) == TYPE_DECL)
18873 type = TREE_TYPE (type);
18874 return finish_compound_literal (type, expression_list);
18878 vec = cp_parser_parenthesized_expression_list (parser, false,
18880 /*allow_expansion_p=*/true,
18881 /*non_constant_p=*/NULL);
18883 expression_list = error_mark_node;
18886 expression_list = build_tree_list_vec (vec);
18887 release_tree_vector (vec);
18890 cast = build_functional_cast (type, expression_list,
18891 tf_warning_or_error);
18892 /* [expr.const]/1: In an integral constant expression "only type
18893 conversions to integral or enumeration type can be used". */
18894 if (TREE_CODE (type) == TYPE_DECL)
18895 type = TREE_TYPE (type);
18896 if (cast != error_mark_node
18897 && !cast_valid_in_integral_constant_expression_p (type)
18898 && (cp_parser_non_integral_constant_expression
18899 (parser, "a call to a constructor")))
18900 return error_mark_node;
18904 /* Save the tokens that make up the body of a member function defined
18905 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18906 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18907 specifiers applied to the declaration. Returns the FUNCTION_DECL
18908 for the member function. */
18911 cp_parser_save_member_function_body (cp_parser* parser,
18912 cp_decl_specifier_seq *decl_specifiers,
18913 cp_declarator *declarator,
18920 /* Create the FUNCTION_DECL. */
18921 fn = grokmethod (decl_specifiers, declarator, attributes);
18922 /* If something went badly wrong, bail out now. */
18923 if (fn == error_mark_node)
18925 /* If there's a function-body, skip it. */
18926 if (cp_parser_token_starts_function_definition_p
18927 (cp_lexer_peek_token (parser->lexer)))
18928 cp_parser_skip_to_end_of_block_or_statement (parser);
18929 return error_mark_node;
18932 /* Remember it, if there default args to post process. */
18933 cp_parser_save_default_args (parser, fn);
18935 /* Save away the tokens that make up the body of the
18937 first = parser->lexer->next_token;
18938 /* We can have braced-init-list mem-initializers before the fn body. */
18939 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18941 cp_lexer_consume_token (parser->lexer);
18942 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18943 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18945 /* cache_group will stop after an un-nested { } pair, too. */
18946 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18949 /* variadic mem-inits have ... after the ')'. */
18950 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18951 cp_lexer_consume_token (parser->lexer);
18954 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18955 /* Handle function try blocks. */
18956 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18957 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18958 last = parser->lexer->next_token;
18960 /* Save away the inline definition; we will process it when the
18961 class is complete. */
18962 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18963 DECL_PENDING_INLINE_P (fn) = 1;
18965 /* We need to know that this was defined in the class, so that
18966 friend templates are handled correctly. */
18967 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18969 /* Add FN to the queue of functions to be parsed later. */
18970 TREE_VALUE (parser->unparsed_functions_queues)
18971 = tree_cons (NULL_TREE, fn,
18972 TREE_VALUE (parser->unparsed_functions_queues));
18977 /* Parse a template-argument-list, as well as the trailing ">" (but
18978 not the opening ">"). See cp_parser_template_argument_list for the
18982 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18986 tree saved_qualifying_scope;
18987 tree saved_object_scope;
18988 bool saved_greater_than_is_operator_p;
18989 int saved_unevaluated_operand;
18990 int saved_inhibit_evaluation_warnings;
18994 When parsing a template-id, the first non-nested `>' is taken as
18995 the end of the template-argument-list rather than a greater-than
18997 saved_greater_than_is_operator_p
18998 = parser->greater_than_is_operator_p;
18999 parser->greater_than_is_operator_p = false;
19000 /* Parsing the argument list may modify SCOPE, so we save it
19002 saved_scope = parser->scope;
19003 saved_qualifying_scope = parser->qualifying_scope;
19004 saved_object_scope = parser->object_scope;
19005 /* We need to evaluate the template arguments, even though this
19006 template-id may be nested within a "sizeof". */
19007 saved_unevaluated_operand = cp_unevaluated_operand;
19008 cp_unevaluated_operand = 0;
19009 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19010 c_inhibit_evaluation_warnings = 0;
19011 /* Parse the template-argument-list itself. */
19012 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19013 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19014 arguments = NULL_TREE;
19016 arguments = cp_parser_template_argument_list (parser);
19017 /* Look for the `>' that ends the template-argument-list. If we find
19018 a '>>' instead, it's probably just a typo. */
19019 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19021 if (cxx_dialect != cxx98)
19023 /* In C++0x, a `>>' in a template argument list or cast
19024 expression is considered to be two separate `>'
19025 tokens. So, change the current token to a `>', but don't
19026 consume it: it will be consumed later when the outer
19027 template argument list (or cast expression) is parsed.
19028 Note that this replacement of `>' for `>>' is necessary
19029 even if we are parsing tentatively: in the tentative
19030 case, after calling
19031 cp_parser_enclosed_template_argument_list we will always
19032 throw away all of the template arguments and the first
19033 closing `>', either because the template argument list
19034 was erroneous or because we are replacing those tokens
19035 with a CPP_TEMPLATE_ID token. The second `>' (which will
19036 not have been thrown away) is needed either to close an
19037 outer template argument list or to complete a new-style
19039 cp_token *token = cp_lexer_peek_token (parser->lexer);
19040 token->type = CPP_GREATER;
19042 else if (!saved_greater_than_is_operator_p)
19044 /* If we're in a nested template argument list, the '>>' has
19045 to be a typo for '> >'. We emit the error message, but we
19046 continue parsing and we push a '>' as next token, so that
19047 the argument list will be parsed correctly. Note that the
19048 global source location is still on the token before the
19049 '>>', so we need to say explicitly where we want it. */
19050 cp_token *token = cp_lexer_peek_token (parser->lexer);
19051 error_at (token->location, "%<>>%> should be %<> >%> "
19052 "within a nested template argument list");
19054 token->type = CPP_GREATER;
19058 /* If this is not a nested template argument list, the '>>'
19059 is a typo for '>'. Emit an error message and continue.
19060 Same deal about the token location, but here we can get it
19061 right by consuming the '>>' before issuing the diagnostic. */
19062 cp_token *token = cp_lexer_consume_token (parser->lexer);
19063 error_at (token->location,
19064 "spurious %<>>%>, use %<>%> to terminate "
19065 "a template argument list");
19069 cp_parser_skip_to_end_of_template_parameter_list (parser);
19070 /* The `>' token might be a greater-than operator again now. */
19071 parser->greater_than_is_operator_p
19072 = saved_greater_than_is_operator_p;
19073 /* Restore the SAVED_SCOPE. */
19074 parser->scope = saved_scope;
19075 parser->qualifying_scope = saved_qualifying_scope;
19076 parser->object_scope = saved_object_scope;
19077 cp_unevaluated_operand = saved_unevaluated_operand;
19078 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19083 /* MEMBER_FUNCTION is a member function, or a friend. If default
19084 arguments, or the body of the function have not yet been parsed,
19088 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19090 /* If this member is a template, get the underlying
19092 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19093 member_function = DECL_TEMPLATE_RESULT (member_function);
19095 /* There should not be any class definitions in progress at this
19096 point; the bodies of members are only parsed outside of all class
19098 gcc_assert (parser->num_classes_being_defined == 0);
19099 /* While we're parsing the member functions we might encounter more
19100 classes. We want to handle them right away, but we don't want
19101 them getting mixed up with functions that are currently in the
19103 parser->unparsed_functions_queues
19104 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19106 /* Make sure that any template parameters are in scope. */
19107 maybe_begin_member_template_processing (member_function);
19109 /* If the body of the function has not yet been parsed, parse it
19111 if (DECL_PENDING_INLINE_P (member_function))
19113 tree function_scope;
19114 cp_token_cache *tokens;
19116 /* The function is no longer pending; we are processing it. */
19117 tokens = DECL_PENDING_INLINE_INFO (member_function);
19118 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19119 DECL_PENDING_INLINE_P (member_function) = 0;
19121 /* If this is a local class, enter the scope of the containing
19123 function_scope = current_function_decl;
19124 if (function_scope)
19125 push_function_context ();
19127 /* Push the body of the function onto the lexer stack. */
19128 cp_parser_push_lexer_for_tokens (parser, tokens);
19130 /* Let the front end know that we going to be defining this
19132 start_preparsed_function (member_function, NULL_TREE,
19133 SF_PRE_PARSED | SF_INCLASS_INLINE);
19135 /* Don't do access checking if it is a templated function. */
19136 if (processing_template_decl)
19137 push_deferring_access_checks (dk_no_check);
19139 /* Now, parse the body of the function. */
19140 cp_parser_function_definition_after_declarator (parser,
19141 /*inline_p=*/true);
19143 if (processing_template_decl)
19144 pop_deferring_access_checks ();
19146 /* Leave the scope of the containing function. */
19147 if (function_scope)
19148 pop_function_context ();
19149 cp_parser_pop_lexer (parser);
19152 /* Remove any template parameters from the symbol table. */
19153 maybe_end_member_template_processing ();
19155 /* Restore the queue. */
19156 parser->unparsed_functions_queues
19157 = TREE_CHAIN (parser->unparsed_functions_queues);
19160 /* If DECL contains any default args, remember it on the unparsed
19161 functions queue. */
19164 cp_parser_save_default_args (cp_parser* parser, tree decl)
19168 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19170 probe = TREE_CHAIN (probe))
19171 if (TREE_PURPOSE (probe))
19173 TREE_PURPOSE (parser->unparsed_functions_queues)
19174 = tree_cons (current_class_type, decl,
19175 TREE_PURPOSE (parser->unparsed_functions_queues));
19180 /* FN is a FUNCTION_DECL which may contains a parameter with an
19181 unparsed DEFAULT_ARG. Parse the default args now. This function
19182 assumes that the current scope is the scope in which the default
19183 argument should be processed. */
19186 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19188 bool saved_local_variables_forbidden_p;
19189 tree parm, parmdecl;
19191 /* While we're parsing the default args, we might (due to the
19192 statement expression extension) encounter more classes. We want
19193 to handle them right away, but we don't want them getting mixed
19194 up with default args that are currently in the queue. */
19195 parser->unparsed_functions_queues
19196 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19198 /* Local variable names (and the `this' keyword) may not appear
19199 in a default argument. */
19200 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19201 parser->local_variables_forbidden_p = true;
19203 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19204 parmdecl = DECL_ARGUMENTS (fn);
19205 parm && parm != void_list_node;
19206 parm = TREE_CHAIN (parm),
19207 parmdecl = TREE_CHAIN (parmdecl))
19209 cp_token_cache *tokens;
19210 tree default_arg = TREE_PURPOSE (parm);
19212 VEC(tree,gc) *insts;
19219 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19220 /* This can happen for a friend declaration for a function
19221 already declared with default arguments. */
19224 /* Push the saved tokens for the default argument onto the parser's
19226 tokens = DEFARG_TOKENS (default_arg);
19227 cp_parser_push_lexer_for_tokens (parser, tokens);
19229 start_lambda_scope (parmdecl);
19231 /* Parse the assignment-expression. */
19232 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19233 if (parsed_arg == error_mark_node)
19235 cp_parser_pop_lexer (parser);
19239 if (!processing_template_decl)
19240 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19242 TREE_PURPOSE (parm) = parsed_arg;
19244 /* Update any instantiations we've already created. */
19245 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19246 VEC_iterate (tree, insts, ix, copy); ix++)
19247 TREE_PURPOSE (copy) = parsed_arg;
19249 finish_lambda_scope ();
19251 /* If the token stream has not been completely used up, then
19252 there was extra junk after the end of the default
19254 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19255 cp_parser_error (parser, "expected %<,%>");
19257 /* Revert to the main lexer. */
19258 cp_parser_pop_lexer (parser);
19261 /* Make sure no default arg is missing. */
19262 check_default_args (fn);
19264 /* Restore the state of local_variables_forbidden_p. */
19265 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19267 /* Restore the queue. */
19268 parser->unparsed_functions_queues
19269 = TREE_CHAIN (parser->unparsed_functions_queues);
19272 /* Parse the operand of `sizeof' (or a similar operator). Returns
19273 either a TYPE or an expression, depending on the form of the
19274 input. The KEYWORD indicates which kind of expression we have
19278 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19280 tree expr = NULL_TREE;
19281 const char *saved_message;
19283 bool saved_integral_constant_expression_p;
19284 bool saved_non_integral_constant_expression_p;
19285 bool pack_expansion_p = false;
19287 /* Types cannot be defined in a `sizeof' expression. Save away the
19289 saved_message = parser->type_definition_forbidden_message;
19290 /* And create the new one. */
19291 tmp = concat ("types may not be defined in %<",
19292 IDENTIFIER_POINTER (ridpointers[keyword]),
19293 "%> expressions", NULL);
19294 parser->type_definition_forbidden_message = tmp;
19296 /* The restrictions on constant-expressions do not apply inside
19297 sizeof expressions. */
19298 saved_integral_constant_expression_p
19299 = parser->integral_constant_expression_p;
19300 saved_non_integral_constant_expression_p
19301 = parser->non_integral_constant_expression_p;
19302 parser->integral_constant_expression_p = false;
19304 /* If it's a `...', then we are computing the length of a parameter
19306 if (keyword == RID_SIZEOF
19307 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19309 /* Consume the `...'. */
19310 cp_lexer_consume_token (parser->lexer);
19311 maybe_warn_variadic_templates ();
19313 /* Note that this is an expansion. */
19314 pack_expansion_p = true;
19317 /* Do not actually evaluate the expression. */
19318 ++cp_unevaluated_operand;
19319 ++c_inhibit_evaluation_warnings;
19320 /* If it's a `(', then we might be looking at the type-id
19322 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19325 bool saved_in_type_id_in_expr_p;
19327 /* We can't be sure yet whether we're looking at a type-id or an
19329 cp_parser_parse_tentatively (parser);
19330 /* Consume the `('. */
19331 cp_lexer_consume_token (parser->lexer);
19332 /* Parse the type-id. */
19333 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19334 parser->in_type_id_in_expr_p = true;
19335 type = cp_parser_type_id (parser);
19336 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19337 /* Now, look for the trailing `)'. */
19338 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19339 /* If all went well, then we're done. */
19340 if (cp_parser_parse_definitely (parser))
19342 cp_decl_specifier_seq decl_specs;
19344 /* Build a trivial decl-specifier-seq. */
19345 clear_decl_specs (&decl_specs);
19346 decl_specs.type = type;
19348 /* Call grokdeclarator to figure out what type this is. */
19349 expr = grokdeclarator (NULL,
19353 /*attrlist=*/NULL);
19357 /* If the type-id production did not work out, then we must be
19358 looking at the unary-expression production. */
19360 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19361 /*cast_p=*/false, NULL);
19363 if (pack_expansion_p)
19364 /* Build a pack expansion. */
19365 expr = make_pack_expansion (expr);
19367 /* Go back to evaluating expressions. */
19368 --cp_unevaluated_operand;
19369 --c_inhibit_evaluation_warnings;
19371 /* Free the message we created. */
19373 /* And restore the old one. */
19374 parser->type_definition_forbidden_message = saved_message;
19375 parser->integral_constant_expression_p
19376 = saved_integral_constant_expression_p;
19377 parser->non_integral_constant_expression_p
19378 = saved_non_integral_constant_expression_p;
19383 /* If the current declaration has no declarator, return true. */
19386 cp_parser_declares_only_class_p (cp_parser *parser)
19388 /* If the next token is a `;' or a `,' then there is no
19390 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19391 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19394 /* Update the DECL_SPECS to reflect the storage class indicated by
19398 cp_parser_set_storage_class (cp_parser *parser,
19399 cp_decl_specifier_seq *decl_specs,
19401 location_t location)
19403 cp_storage_class storage_class;
19405 if (parser->in_unbraced_linkage_specification_p)
19407 error_at (location, "invalid use of %qD in linkage specification",
19408 ridpointers[keyword]);
19411 else if (decl_specs->storage_class != sc_none)
19413 decl_specs->conflicting_specifiers_p = true;
19417 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19418 && decl_specs->specs[(int) ds_thread])
19420 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19421 decl_specs->specs[(int) ds_thread] = 0;
19427 storage_class = sc_auto;
19430 storage_class = sc_register;
19433 storage_class = sc_static;
19436 storage_class = sc_extern;
19439 storage_class = sc_mutable;
19442 gcc_unreachable ();
19444 decl_specs->storage_class = storage_class;
19446 /* A storage class specifier cannot be applied alongside a typedef
19447 specifier. If there is a typedef specifier present then set
19448 conflicting_specifiers_p which will trigger an error later
19449 on in grokdeclarator. */
19450 if (decl_specs->specs[(int)ds_typedef])
19451 decl_specs->conflicting_specifiers_p = true;
19454 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19455 is true, the type is a user-defined type; otherwise it is a
19456 built-in type specified by a keyword. */
19459 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19461 location_t location,
19462 bool user_defined_p)
19464 decl_specs->any_specifiers_p = true;
19466 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19467 (with, for example, in "typedef int wchar_t;") we remember that
19468 this is what happened. In system headers, we ignore these
19469 declarations so that G++ can work with system headers that are not
19471 if (decl_specs->specs[(int) ds_typedef]
19473 && (type_spec == boolean_type_node
19474 || type_spec == char16_type_node
19475 || type_spec == char32_type_node
19476 || type_spec == wchar_type_node)
19477 && (decl_specs->type
19478 || decl_specs->specs[(int) ds_long]
19479 || decl_specs->specs[(int) ds_short]
19480 || decl_specs->specs[(int) ds_unsigned]
19481 || decl_specs->specs[(int) ds_signed]))
19483 decl_specs->redefined_builtin_type = type_spec;
19484 if (!decl_specs->type)
19486 decl_specs->type = type_spec;
19487 decl_specs->user_defined_type_p = false;
19488 decl_specs->type_location = location;
19491 else if (decl_specs->type)
19492 decl_specs->multiple_types_p = true;
19495 decl_specs->type = type_spec;
19496 decl_specs->user_defined_type_p = user_defined_p;
19497 decl_specs->redefined_builtin_type = NULL_TREE;
19498 decl_specs->type_location = location;
19502 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19503 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19506 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19508 return decl_specifiers->specs[(int) ds_friend] != 0;
19511 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19512 issue an error message indicating that TOKEN_DESC was expected.
19514 Returns the token consumed, if the token had the appropriate type.
19515 Otherwise, returns NULL. */
19518 cp_parser_require (cp_parser* parser,
19519 enum cpp_ttype type,
19520 const char* token_desc)
19522 if (cp_lexer_next_token_is (parser->lexer, type))
19523 return cp_lexer_consume_token (parser->lexer);
19526 /* Output the MESSAGE -- unless we're parsing tentatively. */
19527 if (!cp_parser_simulate_error (parser))
19529 char *message = concat ("expected ", token_desc, NULL);
19530 cp_parser_error (parser, message);
19537 /* An error message is produced if the next token is not '>'.
19538 All further tokens are skipped until the desired token is
19539 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19542 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19544 /* Current level of '< ... >'. */
19545 unsigned level = 0;
19546 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19547 unsigned nesting_depth = 0;
19549 /* Are we ready, yet? If not, issue error message. */
19550 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19553 /* Skip tokens until the desired token is found. */
19556 /* Peek at the next token. */
19557 switch (cp_lexer_peek_token (parser->lexer)->type)
19560 if (!nesting_depth)
19565 if (cxx_dialect == cxx98)
19566 /* C++0x views the `>>' operator as two `>' tokens, but
19569 else if (!nesting_depth && level-- == 0)
19571 /* We've hit a `>>' where the first `>' closes the
19572 template argument list, and the second `>' is
19573 spurious. Just consume the `>>' and stop; we've
19574 already produced at least one error. */
19575 cp_lexer_consume_token (parser->lexer);
19578 /* Fall through for C++0x, so we handle the second `>' in
19582 if (!nesting_depth && level-- == 0)
19584 /* We've reached the token we want, consume it and stop. */
19585 cp_lexer_consume_token (parser->lexer);
19590 case CPP_OPEN_PAREN:
19591 case CPP_OPEN_SQUARE:
19595 case CPP_CLOSE_PAREN:
19596 case CPP_CLOSE_SQUARE:
19597 if (nesting_depth-- == 0)
19602 case CPP_PRAGMA_EOL:
19603 case CPP_SEMICOLON:
19604 case CPP_OPEN_BRACE:
19605 case CPP_CLOSE_BRACE:
19606 /* The '>' was probably forgotten, don't look further. */
19613 /* Consume this token. */
19614 cp_lexer_consume_token (parser->lexer);
19618 /* If the next token is the indicated keyword, consume it. Otherwise,
19619 issue an error message indicating that TOKEN_DESC was expected.
19621 Returns the token consumed, if the token had the appropriate type.
19622 Otherwise, returns NULL. */
19625 cp_parser_require_keyword (cp_parser* parser,
19627 const char* token_desc)
19629 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19631 if (token && token->keyword != keyword)
19633 dyn_string_t error_msg;
19635 /* Format the error message. */
19636 error_msg = dyn_string_new (0);
19637 dyn_string_append_cstr (error_msg, "expected ");
19638 dyn_string_append_cstr (error_msg, token_desc);
19639 cp_parser_error (parser, error_msg->s);
19640 dyn_string_delete (error_msg);
19647 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19648 function-definition. */
19651 cp_parser_token_starts_function_definition_p (cp_token* token)
19653 return (/* An ordinary function-body begins with an `{'. */
19654 token->type == CPP_OPEN_BRACE
19655 /* A ctor-initializer begins with a `:'. */
19656 || token->type == CPP_COLON
19657 /* A function-try-block begins with `try'. */
19658 || token->keyword == RID_TRY
19659 /* The named return value extension begins with `return'. */
19660 || token->keyword == RID_RETURN);
19663 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19667 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19671 token = cp_lexer_peek_token (parser->lexer);
19672 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19675 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19676 C++0x) ending a template-argument. */
19679 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19683 token = cp_lexer_peek_token (parser->lexer);
19684 return (token->type == CPP_COMMA
19685 || token->type == CPP_GREATER
19686 || token->type == CPP_ELLIPSIS
19687 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19690 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19691 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19694 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19699 token = cp_lexer_peek_nth_token (parser->lexer, n);
19700 if (token->type == CPP_LESS)
19702 /* Check for the sequence `<::' in the original code. It would be lexed as
19703 `[:', where `[' is a digraph, and there is no whitespace before
19705 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19708 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19709 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19715 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19716 or none_type otherwise. */
19718 static enum tag_types
19719 cp_parser_token_is_class_key (cp_token* token)
19721 switch (token->keyword)
19726 return record_type;
19735 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19738 cp_parser_check_class_key (enum tag_types class_key, tree type)
19740 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19741 permerror (input_location, "%qs tag used in naming %q#T",
19742 class_key == union_type ? "union"
19743 : class_key == record_type ? "struct" : "class",
19747 /* Issue an error message if DECL is redeclared with different
19748 access than its original declaration [class.access.spec/3].
19749 This applies to nested classes and nested class templates.
19753 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19755 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19758 if ((TREE_PRIVATE (decl)
19759 != (current_access_specifier == access_private_node))
19760 || (TREE_PROTECTED (decl)
19761 != (current_access_specifier == access_protected_node)))
19762 error_at (location, "%qD redeclared with different access", decl);
19765 /* Look for the `template' keyword, as a syntactic disambiguator.
19766 Return TRUE iff it is present, in which case it will be
19770 cp_parser_optional_template_keyword (cp_parser *parser)
19772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19774 /* The `template' keyword can only be used within templates;
19775 outside templates the parser can always figure out what is a
19776 template and what is not. */
19777 if (!processing_template_decl)
19779 cp_token *token = cp_lexer_peek_token (parser->lexer);
19780 error_at (token->location,
19781 "%<template%> (as a disambiguator) is only allowed "
19782 "within templates");
19783 /* If this part of the token stream is rescanned, the same
19784 error message would be generated. So, we purge the token
19785 from the stream. */
19786 cp_lexer_purge_token (parser->lexer);
19791 /* Consume the `template' keyword. */
19792 cp_lexer_consume_token (parser->lexer);
19800 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19801 set PARSER->SCOPE, and perform other related actions. */
19804 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19807 struct tree_check *check_value;
19808 deferred_access_check *chk;
19809 VEC (deferred_access_check,gc) *checks;
19811 /* Get the stored value. */
19812 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19813 /* Perform any access checks that were deferred. */
19814 checks = check_value->checks;
19818 VEC_iterate (deferred_access_check, checks, i, chk) ;
19821 perform_or_defer_access_check (chk->binfo,
19826 /* Set the scope from the stored value. */
19827 parser->scope = check_value->value;
19828 parser->qualifying_scope = check_value->qualifying_scope;
19829 parser->object_scope = NULL_TREE;
19832 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19833 encounter the end of a block before what we were looking for. */
19836 cp_parser_cache_group (cp_parser *parser,
19837 enum cpp_ttype end,
19842 cp_token *token = cp_lexer_peek_token (parser->lexer);
19844 /* Abort a parenthesized expression if we encounter a semicolon. */
19845 if ((end == CPP_CLOSE_PAREN || depth == 0)
19846 && token->type == CPP_SEMICOLON)
19848 /* If we've reached the end of the file, stop. */
19849 if (token->type == CPP_EOF
19850 || (end != CPP_PRAGMA_EOL
19851 && token->type == CPP_PRAGMA_EOL))
19853 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19854 /* We've hit the end of an enclosing block, so there's been some
19855 kind of syntax error. */
19858 /* Consume the token. */
19859 cp_lexer_consume_token (parser->lexer);
19860 /* See if it starts a new group. */
19861 if (token->type == CPP_OPEN_BRACE)
19863 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19864 /* In theory this should probably check end == '}', but
19865 cp_parser_save_member_function_body needs it to exit
19866 after either '}' or ')' when called with ')'. */
19870 else if (token->type == CPP_OPEN_PAREN)
19872 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19873 if (depth == 0 && end == CPP_CLOSE_PAREN)
19876 else if (token->type == CPP_PRAGMA)
19877 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19878 else if (token->type == end)
19883 /* Begin parsing tentatively. We always save tokens while parsing
19884 tentatively so that if the tentative parsing fails we can restore the
19888 cp_parser_parse_tentatively (cp_parser* parser)
19890 /* Enter a new parsing context. */
19891 parser->context = cp_parser_context_new (parser->context);
19892 /* Begin saving tokens. */
19893 cp_lexer_save_tokens (parser->lexer);
19894 /* In order to avoid repetitive access control error messages,
19895 access checks are queued up until we are no longer parsing
19897 push_deferring_access_checks (dk_deferred);
19900 /* Commit to the currently active tentative parse. */
19903 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19905 cp_parser_context *context;
19908 /* Mark all of the levels as committed. */
19909 lexer = parser->lexer;
19910 for (context = parser->context; context->next; context = context->next)
19912 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19914 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19915 while (!cp_lexer_saving_tokens (lexer))
19916 lexer = lexer->next;
19917 cp_lexer_commit_tokens (lexer);
19921 /* Abort the currently active tentative parse. All consumed tokens
19922 will be rolled back, and no diagnostics will be issued. */
19925 cp_parser_abort_tentative_parse (cp_parser* parser)
19927 cp_parser_simulate_error (parser);
19928 /* Now, pretend that we want to see if the construct was
19929 successfully parsed. */
19930 cp_parser_parse_definitely (parser);
19933 /* Stop parsing tentatively. If a parse error has occurred, restore the
19934 token stream. Otherwise, commit to the tokens we have consumed.
19935 Returns true if no error occurred; false otherwise. */
19938 cp_parser_parse_definitely (cp_parser* parser)
19940 bool error_occurred;
19941 cp_parser_context *context;
19943 /* Remember whether or not an error occurred, since we are about to
19944 destroy that information. */
19945 error_occurred = cp_parser_error_occurred (parser);
19946 /* Remove the topmost context from the stack. */
19947 context = parser->context;
19948 parser->context = context->next;
19949 /* If no parse errors occurred, commit to the tentative parse. */
19950 if (!error_occurred)
19952 /* Commit to the tokens read tentatively, unless that was
19954 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19955 cp_lexer_commit_tokens (parser->lexer);
19957 pop_to_parent_deferring_access_checks ();
19959 /* Otherwise, if errors occurred, roll back our state so that things
19960 are just as they were before we began the tentative parse. */
19963 cp_lexer_rollback_tokens (parser->lexer);
19964 pop_deferring_access_checks ();
19966 /* Add the context to the front of the free list. */
19967 context->next = cp_parser_context_free_list;
19968 cp_parser_context_free_list = context;
19970 return !error_occurred;
19973 /* Returns true if we are parsing tentatively and are not committed to
19974 this tentative parse. */
19977 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19979 return (cp_parser_parsing_tentatively (parser)
19980 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19983 /* Returns nonzero iff an error has occurred during the most recent
19984 tentative parse. */
19987 cp_parser_error_occurred (cp_parser* parser)
19989 return (cp_parser_parsing_tentatively (parser)
19990 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19993 /* Returns nonzero if GNU extensions are allowed. */
19996 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19998 return parser->allow_gnu_extensions_p;
20001 /* Objective-C++ Productions */
20004 /* Parse an Objective-C expression, which feeds into a primary-expression
20008 objc-message-expression
20009 objc-string-literal
20010 objc-encode-expression
20011 objc-protocol-expression
20012 objc-selector-expression
20014 Returns a tree representation of the expression. */
20017 cp_parser_objc_expression (cp_parser* parser)
20019 /* Try to figure out what kind of declaration is present. */
20020 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20024 case CPP_OPEN_SQUARE:
20025 return cp_parser_objc_message_expression (parser);
20027 case CPP_OBJC_STRING:
20028 kwd = cp_lexer_consume_token (parser->lexer);
20029 return objc_build_string_object (kwd->u.value);
20032 switch (kwd->keyword)
20034 case RID_AT_ENCODE:
20035 return cp_parser_objc_encode_expression (parser);
20037 case RID_AT_PROTOCOL:
20038 return cp_parser_objc_protocol_expression (parser);
20040 case RID_AT_SELECTOR:
20041 return cp_parser_objc_selector_expression (parser);
20047 error_at (kwd->location,
20048 "misplaced %<@%D%> Objective-C++ construct",
20050 cp_parser_skip_to_end_of_block_or_statement (parser);
20053 return error_mark_node;
20056 /* Parse an Objective-C message expression.
20058 objc-message-expression:
20059 [ objc-message-receiver objc-message-args ]
20061 Returns a representation of an Objective-C message. */
20064 cp_parser_objc_message_expression (cp_parser* parser)
20066 tree receiver, messageargs;
20068 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
20069 receiver = cp_parser_objc_message_receiver (parser);
20070 messageargs = cp_parser_objc_message_args (parser);
20071 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20073 return objc_build_message_expr (build_tree_list (receiver, messageargs));
20076 /* Parse an objc-message-receiver.
20078 objc-message-receiver:
20080 simple-type-specifier
20082 Returns a representation of the type or expression. */
20085 cp_parser_objc_message_receiver (cp_parser* parser)
20089 /* An Objective-C message receiver may be either (1) a type
20090 or (2) an expression. */
20091 cp_parser_parse_tentatively (parser);
20092 rcv = cp_parser_expression (parser, false, NULL);
20094 if (cp_parser_parse_definitely (parser))
20097 rcv = cp_parser_simple_type_specifier (parser,
20098 /*decl_specs=*/NULL,
20099 CP_PARSER_FLAGS_NONE);
20101 return objc_get_class_reference (rcv);
20104 /* Parse the arguments and selectors comprising an Objective-C message.
20109 objc-selector-args , objc-comma-args
20111 objc-selector-args:
20112 objc-selector [opt] : assignment-expression
20113 objc-selector-args objc-selector [opt] : assignment-expression
20116 assignment-expression
20117 objc-comma-args , assignment-expression
20119 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20120 selector arguments and TREE_VALUE containing a list of comma
20124 cp_parser_objc_message_args (cp_parser* parser)
20126 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20127 bool maybe_unary_selector_p = true;
20128 cp_token *token = cp_lexer_peek_token (parser->lexer);
20130 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20132 tree selector = NULL_TREE, arg;
20134 if (token->type != CPP_COLON)
20135 selector = cp_parser_objc_selector (parser);
20137 /* Detect if we have a unary selector. */
20138 if (maybe_unary_selector_p
20139 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20140 return build_tree_list (selector, NULL_TREE);
20142 maybe_unary_selector_p = false;
20143 cp_parser_require (parser, CPP_COLON, "%<:%>");
20144 arg = cp_parser_assignment_expression (parser, false, NULL);
20147 = chainon (sel_args,
20148 build_tree_list (selector, arg));
20150 token = cp_lexer_peek_token (parser->lexer);
20153 /* Handle non-selector arguments, if any. */
20154 while (token->type == CPP_COMMA)
20158 cp_lexer_consume_token (parser->lexer);
20159 arg = cp_parser_assignment_expression (parser, false, NULL);
20162 = chainon (addl_args,
20163 build_tree_list (NULL_TREE, arg));
20165 token = cp_lexer_peek_token (parser->lexer);
20168 return build_tree_list (sel_args, addl_args);
20171 /* Parse an Objective-C encode expression.
20173 objc-encode-expression:
20174 @encode objc-typename
20176 Returns an encoded representation of the type argument. */
20179 cp_parser_objc_encode_expression (cp_parser* parser)
20184 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20185 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20186 token = cp_lexer_peek_token (parser->lexer);
20187 type = complete_type (cp_parser_type_id (parser));
20188 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20192 error_at (token->location,
20193 "%<@encode%> must specify a type as an argument");
20194 return error_mark_node;
20197 return objc_build_encode_expr (type);
20200 /* Parse an Objective-C @defs expression. */
20203 cp_parser_objc_defs_expression (cp_parser *parser)
20207 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20208 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20209 name = cp_parser_identifier (parser);
20210 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20212 return objc_get_class_ivars (name);
20215 /* Parse an Objective-C protocol expression.
20217 objc-protocol-expression:
20218 @protocol ( identifier )
20220 Returns a representation of the protocol expression. */
20223 cp_parser_objc_protocol_expression (cp_parser* parser)
20227 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20228 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20229 proto = cp_parser_identifier (parser);
20230 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20232 return objc_build_protocol_expr (proto);
20235 /* Parse an Objective-C selector expression.
20237 objc-selector-expression:
20238 @selector ( objc-method-signature )
20240 objc-method-signature:
20246 objc-selector-seq objc-selector :
20248 Returns a representation of the method selector. */
20251 cp_parser_objc_selector_expression (cp_parser* parser)
20253 tree sel_seq = NULL_TREE;
20254 bool maybe_unary_selector_p = true;
20256 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20258 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20259 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20260 token = cp_lexer_peek_token (parser->lexer);
20262 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20263 || token->type == CPP_SCOPE)
20265 tree selector = NULL_TREE;
20267 if (token->type != CPP_COLON
20268 || token->type == CPP_SCOPE)
20269 selector = cp_parser_objc_selector (parser);
20271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20272 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20274 /* Detect if we have a unary selector. */
20275 if (maybe_unary_selector_p)
20277 sel_seq = selector;
20278 goto finish_selector;
20282 cp_parser_error (parser, "expected %<:%>");
20285 maybe_unary_selector_p = false;
20286 token = cp_lexer_consume_token (parser->lexer);
20288 if (token->type == CPP_SCOPE)
20291 = chainon (sel_seq,
20292 build_tree_list (selector, NULL_TREE));
20294 = chainon (sel_seq,
20295 build_tree_list (NULL_TREE, NULL_TREE));
20299 = chainon (sel_seq,
20300 build_tree_list (selector, NULL_TREE));
20302 token = cp_lexer_peek_token (parser->lexer);
20306 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20308 return objc_build_selector_expr (loc, sel_seq);
20311 /* Parse a list of identifiers.
20313 objc-identifier-list:
20315 objc-identifier-list , identifier
20317 Returns a TREE_LIST of identifier nodes. */
20320 cp_parser_objc_identifier_list (cp_parser* parser)
20322 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20323 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20325 while (sep->type == CPP_COMMA)
20327 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20328 list = chainon (list,
20329 build_tree_list (NULL_TREE,
20330 cp_parser_identifier (parser)));
20331 sep = cp_lexer_peek_token (parser->lexer);
20337 /* Parse an Objective-C alias declaration.
20339 objc-alias-declaration:
20340 @compatibility_alias identifier identifier ;
20342 This function registers the alias mapping with the Objective-C front end.
20343 It returns nothing. */
20346 cp_parser_objc_alias_declaration (cp_parser* parser)
20350 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20351 alias = cp_parser_identifier (parser);
20352 orig = cp_parser_identifier (parser);
20353 objc_declare_alias (alias, orig);
20354 cp_parser_consume_semicolon_at_end_of_statement (parser);
20357 /* Parse an Objective-C class forward-declaration.
20359 objc-class-declaration:
20360 @class objc-identifier-list ;
20362 The function registers the forward declarations with the Objective-C
20363 front end. It returns nothing. */
20366 cp_parser_objc_class_declaration (cp_parser* parser)
20368 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20369 objc_declare_class (cp_parser_objc_identifier_list (parser));
20370 cp_parser_consume_semicolon_at_end_of_statement (parser);
20373 /* Parse a list of Objective-C protocol references.
20375 objc-protocol-refs-opt:
20376 objc-protocol-refs [opt]
20378 objc-protocol-refs:
20379 < objc-identifier-list >
20381 Returns a TREE_LIST of identifiers, if any. */
20384 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20386 tree protorefs = NULL_TREE;
20388 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20390 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20391 protorefs = cp_parser_objc_identifier_list (parser);
20392 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20398 /* Parse a Objective-C visibility specification. */
20401 cp_parser_objc_visibility_spec (cp_parser* parser)
20403 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20405 switch (vis->keyword)
20407 case RID_AT_PRIVATE:
20408 objc_set_visibility (2);
20410 case RID_AT_PROTECTED:
20411 objc_set_visibility (0);
20413 case RID_AT_PUBLIC:
20414 objc_set_visibility (1);
20420 /* Eat '@private'/'@protected'/'@public'. */
20421 cp_lexer_consume_token (parser->lexer);
20424 /* Parse an Objective-C method type. */
20427 cp_parser_objc_method_type (cp_parser* parser)
20429 objc_set_method_type
20430 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20435 /* Parse an Objective-C protocol qualifier. */
20438 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20440 tree quals = NULL_TREE, node;
20441 cp_token *token = cp_lexer_peek_token (parser->lexer);
20443 node = token->u.value;
20445 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20446 && (node == ridpointers [(int) RID_IN]
20447 || node == ridpointers [(int) RID_OUT]
20448 || node == ridpointers [(int) RID_INOUT]
20449 || node == ridpointers [(int) RID_BYCOPY]
20450 || node == ridpointers [(int) RID_BYREF]
20451 || node == ridpointers [(int) RID_ONEWAY]))
20453 quals = tree_cons (NULL_TREE, node, quals);
20454 cp_lexer_consume_token (parser->lexer);
20455 token = cp_lexer_peek_token (parser->lexer);
20456 node = token->u.value;
20462 /* Parse an Objective-C typename. */
20465 cp_parser_objc_typename (cp_parser* parser)
20467 tree type_name = NULL_TREE;
20469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20471 tree proto_quals, cp_type = NULL_TREE;
20473 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20474 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20476 /* An ObjC type name may consist of just protocol qualifiers, in which
20477 case the type shall default to 'id'. */
20478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20479 cp_type = cp_parser_type_id (parser);
20481 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20482 type_name = build_tree_list (proto_quals, cp_type);
20488 /* Check to see if TYPE refers to an Objective-C selector name. */
20491 cp_parser_objc_selector_p (enum cpp_ttype type)
20493 return (type == CPP_NAME || type == CPP_KEYWORD
20494 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20495 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20496 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20497 || type == CPP_XOR || type == CPP_XOR_EQ);
20500 /* Parse an Objective-C selector. */
20503 cp_parser_objc_selector (cp_parser* parser)
20505 cp_token *token = cp_lexer_consume_token (parser->lexer);
20507 if (!cp_parser_objc_selector_p (token->type))
20509 error_at (token->location, "invalid Objective-C++ selector name");
20510 return error_mark_node;
20513 /* C++ operator names are allowed to appear in ObjC selectors. */
20514 switch (token->type)
20516 case CPP_AND_AND: return get_identifier ("and");
20517 case CPP_AND_EQ: return get_identifier ("and_eq");
20518 case CPP_AND: return get_identifier ("bitand");
20519 case CPP_OR: return get_identifier ("bitor");
20520 case CPP_COMPL: return get_identifier ("compl");
20521 case CPP_NOT: return get_identifier ("not");
20522 case CPP_NOT_EQ: return get_identifier ("not_eq");
20523 case CPP_OR_OR: return get_identifier ("or");
20524 case CPP_OR_EQ: return get_identifier ("or_eq");
20525 case CPP_XOR: return get_identifier ("xor");
20526 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20527 default: return token->u.value;
20531 /* Parse an Objective-C params list. */
20534 cp_parser_objc_method_keyword_params (cp_parser* parser)
20536 tree params = NULL_TREE;
20537 bool maybe_unary_selector_p = true;
20538 cp_token *token = cp_lexer_peek_token (parser->lexer);
20540 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20542 tree selector = NULL_TREE, type_name, identifier;
20544 if (token->type != CPP_COLON)
20545 selector = cp_parser_objc_selector (parser);
20547 /* Detect if we have a unary selector. */
20548 if (maybe_unary_selector_p
20549 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20552 maybe_unary_selector_p = false;
20553 cp_parser_require (parser, CPP_COLON, "%<:%>");
20554 type_name = cp_parser_objc_typename (parser);
20555 identifier = cp_parser_identifier (parser);
20559 objc_build_keyword_decl (selector,
20563 token = cp_lexer_peek_token (parser->lexer);
20569 /* Parse the non-keyword Objective-C params. */
20572 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20574 tree params = make_node (TREE_LIST);
20575 cp_token *token = cp_lexer_peek_token (parser->lexer);
20576 *ellipsisp = false; /* Initially, assume no ellipsis. */
20578 while (token->type == CPP_COMMA)
20580 cp_parameter_declarator *parmdecl;
20583 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20584 token = cp_lexer_peek_token (parser->lexer);
20586 if (token->type == CPP_ELLIPSIS)
20588 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20593 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20594 parm = grokdeclarator (parmdecl->declarator,
20595 &parmdecl->decl_specifiers,
20596 PARM, /*initialized=*/0,
20597 /*attrlist=*/NULL);
20599 chainon (params, build_tree_list (NULL_TREE, parm));
20600 token = cp_lexer_peek_token (parser->lexer);
20606 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20609 cp_parser_objc_interstitial_code (cp_parser* parser)
20611 cp_token *token = cp_lexer_peek_token (parser->lexer);
20613 /* If the next token is `extern' and the following token is a string
20614 literal, then we have a linkage specification. */
20615 if (token->keyword == RID_EXTERN
20616 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20617 cp_parser_linkage_specification (parser);
20618 /* Handle #pragma, if any. */
20619 else if (token->type == CPP_PRAGMA)
20620 cp_parser_pragma (parser, pragma_external);
20621 /* Allow stray semicolons. */
20622 else if (token->type == CPP_SEMICOLON)
20623 cp_lexer_consume_token (parser->lexer);
20624 /* Finally, try to parse a block-declaration, or a function-definition. */
20626 cp_parser_block_declaration (parser, /*statement_p=*/false);
20629 /* Parse a method signature. */
20632 cp_parser_objc_method_signature (cp_parser* parser)
20634 tree rettype, kwdparms, optparms;
20635 bool ellipsis = false;
20637 cp_parser_objc_method_type (parser);
20638 rettype = cp_parser_objc_typename (parser);
20639 kwdparms = cp_parser_objc_method_keyword_params (parser);
20640 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20642 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20645 /* Pars an Objective-C method prototype list. */
20648 cp_parser_objc_method_prototype_list (cp_parser* parser)
20650 cp_token *token = cp_lexer_peek_token (parser->lexer);
20652 while (token->keyword != RID_AT_END)
20654 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20656 objc_add_method_declaration
20657 (cp_parser_objc_method_signature (parser));
20658 cp_parser_consume_semicolon_at_end_of_statement (parser);
20661 /* Allow for interspersed non-ObjC++ code. */
20662 cp_parser_objc_interstitial_code (parser);
20664 token = cp_lexer_peek_token (parser->lexer);
20667 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20668 objc_finish_interface ();
20671 /* Parse an Objective-C method definition list. */
20674 cp_parser_objc_method_definition_list (cp_parser* parser)
20676 cp_token *token = cp_lexer_peek_token (parser->lexer);
20678 while (token->keyword != RID_AT_END)
20682 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20684 push_deferring_access_checks (dk_deferred);
20685 objc_start_method_definition
20686 (cp_parser_objc_method_signature (parser));
20688 /* For historical reasons, we accept an optional semicolon. */
20689 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20690 cp_lexer_consume_token (parser->lexer);
20692 perform_deferred_access_checks ();
20693 stop_deferring_access_checks ();
20694 meth = cp_parser_function_definition_after_declarator (parser,
20696 pop_deferring_access_checks ();
20697 objc_finish_method_definition (meth);
20700 /* Allow for interspersed non-ObjC++ code. */
20701 cp_parser_objc_interstitial_code (parser);
20703 token = cp_lexer_peek_token (parser->lexer);
20706 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20707 objc_finish_implementation ();
20710 /* Parse Objective-C ivars. */
20713 cp_parser_objc_class_ivars (cp_parser* parser)
20715 cp_token *token = cp_lexer_peek_token (parser->lexer);
20717 if (token->type != CPP_OPEN_BRACE)
20718 return; /* No ivars specified. */
20720 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20721 token = cp_lexer_peek_token (parser->lexer);
20723 while (token->type != CPP_CLOSE_BRACE)
20725 cp_decl_specifier_seq declspecs;
20726 int decl_class_or_enum_p;
20727 tree prefix_attributes;
20729 cp_parser_objc_visibility_spec (parser);
20731 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20734 cp_parser_decl_specifier_seq (parser,
20735 CP_PARSER_FLAGS_OPTIONAL,
20737 &decl_class_or_enum_p);
20738 prefix_attributes = declspecs.attributes;
20739 declspecs.attributes = NULL_TREE;
20741 /* Keep going until we hit the `;' at the end of the
20743 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20745 tree width = NULL_TREE, attributes, first_attribute, decl;
20746 cp_declarator *declarator = NULL;
20747 int ctor_dtor_or_conv_p;
20749 /* Check for a (possibly unnamed) bitfield declaration. */
20750 token = cp_lexer_peek_token (parser->lexer);
20751 if (token->type == CPP_COLON)
20754 if (token->type == CPP_NAME
20755 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20758 /* Get the name of the bitfield. */
20759 declarator = make_id_declarator (NULL_TREE,
20760 cp_parser_identifier (parser),
20764 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20765 /* Get the width of the bitfield. */
20767 = cp_parser_constant_expression (parser,
20768 /*allow_non_constant=*/false,
20773 /* Parse the declarator. */
20775 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20776 &ctor_dtor_or_conv_p,
20777 /*parenthesized_p=*/NULL,
20778 /*member_p=*/false);
20781 /* Look for attributes that apply to the ivar. */
20782 attributes = cp_parser_attributes_opt (parser);
20783 /* Remember which attributes are prefix attributes and
20785 first_attribute = attributes;
20786 /* Combine the attributes. */
20787 attributes = chainon (prefix_attributes, attributes);
20790 /* Create the bitfield declaration. */
20791 decl = grokbitfield (declarator, &declspecs,
20795 decl = grokfield (declarator, &declspecs,
20796 NULL_TREE, /*init_const_expr_p=*/false,
20797 NULL_TREE, attributes);
20799 /* Add the instance variable. */
20800 objc_add_instance_variable (decl);
20802 /* Reset PREFIX_ATTRIBUTES. */
20803 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20804 attributes = TREE_CHAIN (attributes);
20806 TREE_CHAIN (attributes) = NULL_TREE;
20808 token = cp_lexer_peek_token (parser->lexer);
20810 if (token->type == CPP_COMMA)
20812 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20818 cp_parser_consume_semicolon_at_end_of_statement (parser);
20819 token = cp_lexer_peek_token (parser->lexer);
20822 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20823 /* For historical reasons, we accept an optional semicolon. */
20824 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20825 cp_lexer_consume_token (parser->lexer);
20828 /* Parse an Objective-C protocol declaration. */
20831 cp_parser_objc_protocol_declaration (cp_parser* parser)
20833 tree proto, protorefs;
20836 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20839 tok = cp_lexer_peek_token (parser->lexer);
20840 error_at (tok->location, "identifier expected after %<@protocol%>");
20844 /* See if we have a forward declaration or a definition. */
20845 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20847 /* Try a forward declaration first. */
20848 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20850 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20852 cp_parser_consume_semicolon_at_end_of_statement (parser);
20855 /* Ok, we got a full-fledged definition (or at least should). */
20858 proto = cp_parser_identifier (parser);
20859 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20860 objc_start_protocol (proto, protorefs);
20861 cp_parser_objc_method_prototype_list (parser);
20865 /* Parse an Objective-C superclass or category. */
20868 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20871 cp_token *next = cp_lexer_peek_token (parser->lexer);
20873 *super = *categ = NULL_TREE;
20874 if (next->type == CPP_COLON)
20876 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20877 *super = cp_parser_identifier (parser);
20879 else if (next->type == CPP_OPEN_PAREN)
20881 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20882 *categ = cp_parser_identifier (parser);
20883 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20887 /* Parse an Objective-C class interface. */
20890 cp_parser_objc_class_interface (cp_parser* parser)
20892 tree name, super, categ, protos;
20894 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20895 name = cp_parser_identifier (parser);
20896 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20897 protos = cp_parser_objc_protocol_refs_opt (parser);
20899 /* We have either a class or a category on our hands. */
20901 objc_start_category_interface (name, categ, protos);
20904 objc_start_class_interface (name, super, protos);
20905 /* Handle instance variable declarations, if any. */
20906 cp_parser_objc_class_ivars (parser);
20907 objc_continue_interface ();
20910 cp_parser_objc_method_prototype_list (parser);
20913 /* Parse an Objective-C class implementation. */
20916 cp_parser_objc_class_implementation (cp_parser* parser)
20918 tree name, super, categ;
20920 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20921 name = cp_parser_identifier (parser);
20922 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20924 /* We have either a class or a category on our hands. */
20926 objc_start_category_implementation (name, categ);
20929 objc_start_class_implementation (name, super);
20930 /* Handle instance variable declarations, if any. */
20931 cp_parser_objc_class_ivars (parser);
20932 objc_continue_implementation ();
20935 cp_parser_objc_method_definition_list (parser);
20938 /* Consume the @end token and finish off the implementation. */
20941 cp_parser_objc_end_implementation (cp_parser* parser)
20943 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20944 objc_finish_implementation ();
20947 /* Parse an Objective-C declaration. */
20950 cp_parser_objc_declaration (cp_parser* parser)
20952 /* Try to figure out what kind of declaration is present. */
20953 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20955 switch (kwd->keyword)
20958 cp_parser_objc_alias_declaration (parser);
20961 cp_parser_objc_class_declaration (parser);
20963 case RID_AT_PROTOCOL:
20964 cp_parser_objc_protocol_declaration (parser);
20966 case RID_AT_INTERFACE:
20967 cp_parser_objc_class_interface (parser);
20969 case RID_AT_IMPLEMENTATION:
20970 cp_parser_objc_class_implementation (parser);
20973 cp_parser_objc_end_implementation (parser);
20976 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20978 cp_parser_skip_to_end_of_block_or_statement (parser);
20982 /* Parse an Objective-C try-catch-finally statement.
20984 objc-try-catch-finally-stmt:
20985 @try compound-statement objc-catch-clause-seq [opt]
20986 objc-finally-clause [opt]
20988 objc-catch-clause-seq:
20989 objc-catch-clause objc-catch-clause-seq [opt]
20992 @catch ( exception-declaration ) compound-statement
20994 objc-finally-clause
20995 @finally compound-statement
20997 Returns NULL_TREE. */
21000 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21001 location_t location;
21004 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21005 location = cp_lexer_peek_token (parser->lexer)->location;
21006 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21007 node, lest it get absorbed into the surrounding block. */
21008 stmt = push_stmt_list ();
21009 cp_parser_compound_statement (parser, NULL, false);
21010 objc_begin_try_stmt (location, pop_stmt_list (stmt));
21012 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21014 cp_parameter_declarator *parmdecl;
21017 cp_lexer_consume_token (parser->lexer);
21018 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21019 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21020 parm = grokdeclarator (parmdecl->declarator,
21021 &parmdecl->decl_specifiers,
21022 PARM, /*initialized=*/0,
21023 /*attrlist=*/NULL);
21024 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21025 objc_begin_catch_clause (parm);
21026 cp_parser_compound_statement (parser, NULL, false);
21027 objc_finish_catch_clause ();
21030 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21032 cp_lexer_consume_token (parser->lexer);
21033 location = cp_lexer_peek_token (parser->lexer)->location;
21034 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21035 node, lest it get absorbed into the surrounding block. */
21036 stmt = push_stmt_list ();
21037 cp_parser_compound_statement (parser, NULL, false);
21038 objc_build_finally_clause (location, pop_stmt_list (stmt));
21041 return objc_finish_try_stmt ();
21044 /* Parse an Objective-C synchronized statement.
21046 objc-synchronized-stmt:
21047 @synchronized ( expression ) compound-statement
21049 Returns NULL_TREE. */
21052 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21053 location_t location;
21056 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21058 location = cp_lexer_peek_token (parser->lexer)->location;
21059 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21060 lock = cp_parser_expression (parser, false, NULL);
21061 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21063 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21064 node, lest it get absorbed into the surrounding block. */
21065 stmt = push_stmt_list ();
21066 cp_parser_compound_statement (parser, NULL, false);
21068 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21071 /* Parse an Objective-C throw statement.
21074 @throw assignment-expression [opt] ;
21076 Returns a constructed '@throw' statement. */
21079 cp_parser_objc_throw_statement (cp_parser *parser) {
21080 tree expr = NULL_TREE;
21081 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21083 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21085 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21086 expr = cp_parser_assignment_expression (parser, false, NULL);
21088 cp_parser_consume_semicolon_at_end_of_statement (parser);
21090 return objc_build_throw_stmt (loc, expr);
21093 /* Parse an Objective-C statement. */
21096 cp_parser_objc_statement (cp_parser * parser) {
21097 /* Try to figure out what kind of declaration is present. */
21098 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21100 switch (kwd->keyword)
21103 return cp_parser_objc_try_catch_finally_statement (parser);
21104 case RID_AT_SYNCHRONIZED:
21105 return cp_parser_objc_synchronized_statement (parser);
21107 return cp_parser_objc_throw_statement (parser);
21109 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21111 cp_parser_skip_to_end_of_block_or_statement (parser);
21114 return error_mark_node;
21117 /* OpenMP 2.5 parsing routines. */
21119 /* Returns name of the next clause.
21120 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21121 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21122 returned and the token is consumed. */
21124 static pragma_omp_clause
21125 cp_parser_omp_clause_name (cp_parser *parser)
21127 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21129 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21130 result = PRAGMA_OMP_CLAUSE_IF;
21131 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21132 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21133 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21134 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21135 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21137 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21138 const char *p = IDENTIFIER_POINTER (id);
21143 if (!strcmp ("collapse", p))
21144 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21145 else if (!strcmp ("copyin", p))
21146 result = PRAGMA_OMP_CLAUSE_COPYIN;
21147 else if (!strcmp ("copyprivate", p))
21148 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21151 if (!strcmp ("firstprivate", p))
21152 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21155 if (!strcmp ("lastprivate", p))
21156 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21159 if (!strcmp ("nowait", p))
21160 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21161 else if (!strcmp ("num_threads", p))
21162 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21165 if (!strcmp ("ordered", p))
21166 result = PRAGMA_OMP_CLAUSE_ORDERED;
21169 if (!strcmp ("reduction", p))
21170 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21173 if (!strcmp ("schedule", p))
21174 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21175 else if (!strcmp ("shared", p))
21176 result = PRAGMA_OMP_CLAUSE_SHARED;
21179 if (!strcmp ("untied", p))
21180 result = PRAGMA_OMP_CLAUSE_UNTIED;
21185 if (result != PRAGMA_OMP_CLAUSE_NONE)
21186 cp_lexer_consume_token (parser->lexer);
21191 /* Validate that a clause of the given type does not already exist. */
21194 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21195 const char *name, location_t location)
21199 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21200 if (OMP_CLAUSE_CODE (c) == code)
21202 error_at (location, "too many %qs clauses", name);
21210 variable-list , identifier
21212 In addition, we match a closing parenthesis. An opening parenthesis
21213 will have been consumed by the caller.
21215 If KIND is nonzero, create the appropriate node and install the decl
21216 in OMP_CLAUSE_DECL and add the node to the head of the list.
21218 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21219 return the list created. */
21222 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21230 token = cp_lexer_peek_token (parser->lexer);
21231 name = cp_parser_id_expression (parser, /*template_p=*/false,
21232 /*check_dependency_p=*/true,
21233 /*template_p=*/NULL,
21234 /*declarator_p=*/false,
21235 /*optional_p=*/false);
21236 if (name == error_mark_node)
21239 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21240 if (decl == error_mark_node)
21241 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21242 else if (kind != 0)
21244 tree u = build_omp_clause (token->location, kind);
21245 OMP_CLAUSE_DECL (u) = decl;
21246 OMP_CLAUSE_CHAIN (u) = list;
21250 list = tree_cons (decl, NULL_TREE, list);
21253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21255 cp_lexer_consume_token (parser->lexer);
21258 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21262 /* Try to resync to an unnested comma. Copied from
21263 cp_parser_parenthesized_expression_list. */
21265 ending = cp_parser_skip_to_closing_parenthesis (parser,
21266 /*recovering=*/true,
21268 /*consume_paren=*/true);
21276 /* Similarly, but expect leading and trailing parenthesis. This is a very
21277 common case for omp clauses. */
21280 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21282 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21283 return cp_parser_omp_var_list_no_open (parser, kind, list);
21288 collapse ( constant-expression ) */
21291 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21297 loc = cp_lexer_peek_token (parser->lexer)->location;
21298 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21301 num = cp_parser_constant_expression (parser, false, NULL);
21303 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21304 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21305 /*or_comma=*/false,
21306 /*consume_paren=*/true);
21308 if (num == error_mark_node)
21310 num = fold_non_dependent_expr (num);
21311 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21312 || !host_integerp (num, 0)
21313 || (n = tree_low_cst (num, 0)) <= 0
21316 error_at (loc, "collapse argument needs positive constant integer expression");
21320 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21321 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21322 OMP_CLAUSE_CHAIN (c) = list;
21323 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21329 default ( shared | none ) */
21332 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21334 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21337 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21341 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21342 const char *p = IDENTIFIER_POINTER (id);
21347 if (strcmp ("none", p) != 0)
21349 kind = OMP_CLAUSE_DEFAULT_NONE;
21353 if (strcmp ("shared", p) != 0)
21355 kind = OMP_CLAUSE_DEFAULT_SHARED;
21362 cp_lexer_consume_token (parser->lexer);
21367 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21370 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21371 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21372 /*or_comma=*/false,
21373 /*consume_paren=*/true);
21375 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21378 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21379 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21380 OMP_CLAUSE_CHAIN (c) = list;
21381 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21387 if ( expression ) */
21390 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21397 t = cp_parser_condition (parser);
21399 if (t == error_mark_node
21400 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21401 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21402 /*or_comma=*/false,
21403 /*consume_paren=*/true);
21405 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21407 c = build_omp_clause (location, OMP_CLAUSE_IF);
21408 OMP_CLAUSE_IF_EXPR (c) = t;
21409 OMP_CLAUSE_CHAIN (c) = list;
21418 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21419 tree list, location_t location)
21423 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21425 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21426 OMP_CLAUSE_CHAIN (c) = list;
21431 num_threads ( expression ) */
21434 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21435 location_t location)
21439 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21442 t = cp_parser_expression (parser, false, NULL);
21444 if (t == error_mark_node
21445 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21446 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21447 /*or_comma=*/false,
21448 /*consume_paren=*/true);
21450 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21451 "num_threads", location);
21453 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21454 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21455 OMP_CLAUSE_CHAIN (c) = list;
21464 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21465 tree list, location_t location)
21469 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21470 "ordered", location);
21472 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21473 OMP_CLAUSE_CHAIN (c) = list;
21478 reduction ( reduction-operator : variable-list )
21480 reduction-operator:
21481 One of: + * - & ^ | && || */
21484 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21486 enum tree_code code;
21489 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21492 switch (cp_lexer_peek_token (parser->lexer)->type)
21504 code = BIT_AND_EXPR;
21507 code = BIT_XOR_EXPR;
21510 code = BIT_IOR_EXPR;
21513 code = TRUTH_ANDIF_EXPR;
21516 code = TRUTH_ORIF_EXPR;
21519 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21520 "%<|%>, %<&&%>, or %<||%>");
21522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21523 /*or_comma=*/false,
21524 /*consume_paren=*/true);
21527 cp_lexer_consume_token (parser->lexer);
21529 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21532 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21533 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21534 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21540 schedule ( schedule-kind )
21541 schedule ( schedule-kind , expression )
21544 static | dynamic | guided | runtime | auto */
21547 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21551 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21554 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21559 const char *p = IDENTIFIER_POINTER (id);
21564 if (strcmp ("dynamic", p) != 0)
21566 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21570 if (strcmp ("guided", p) != 0)
21572 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21576 if (strcmp ("runtime", p) != 0)
21578 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21585 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21586 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21587 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21588 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21591 cp_lexer_consume_token (parser->lexer);
21593 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21596 cp_lexer_consume_token (parser->lexer);
21598 token = cp_lexer_peek_token (parser->lexer);
21599 t = cp_parser_assignment_expression (parser, false, NULL);
21601 if (t == error_mark_node)
21603 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21604 error_at (token->location, "schedule %<runtime%> does not take "
21605 "a %<chunk_size%> parameter");
21606 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21607 error_at (token->location, "schedule %<auto%> does not take "
21608 "a %<chunk_size%> parameter");
21610 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21615 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21618 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21619 OMP_CLAUSE_CHAIN (c) = list;
21623 cp_parser_error (parser, "invalid schedule kind");
21625 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21626 /*or_comma=*/false,
21627 /*consume_paren=*/true);
21635 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21636 tree list, location_t location)
21640 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21642 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21643 OMP_CLAUSE_CHAIN (c) = list;
21647 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21648 is a bitmask in MASK. Return the list of clauses found; the result
21649 of clause default goes in *pdefault. */
21652 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21653 const char *where, cp_token *pragma_tok)
21655 tree clauses = NULL;
21657 cp_token *token = NULL;
21659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21661 pragma_omp_clause c_kind;
21662 const char *c_name;
21663 tree prev = clauses;
21665 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21666 cp_lexer_consume_token (parser->lexer);
21668 token = cp_lexer_peek_token (parser->lexer);
21669 c_kind = cp_parser_omp_clause_name (parser);
21674 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21675 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21677 c_name = "collapse";
21679 case PRAGMA_OMP_CLAUSE_COPYIN:
21680 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21683 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21684 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21686 c_name = "copyprivate";
21688 case PRAGMA_OMP_CLAUSE_DEFAULT:
21689 clauses = cp_parser_omp_clause_default (parser, clauses,
21691 c_name = "default";
21693 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21694 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21696 c_name = "firstprivate";
21698 case PRAGMA_OMP_CLAUSE_IF:
21699 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21702 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21703 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21705 c_name = "lastprivate";
21707 case PRAGMA_OMP_CLAUSE_NOWAIT:
21708 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21711 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21712 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21714 c_name = "num_threads";
21716 case PRAGMA_OMP_CLAUSE_ORDERED:
21717 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21719 c_name = "ordered";
21721 case PRAGMA_OMP_CLAUSE_PRIVATE:
21722 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21724 c_name = "private";
21726 case PRAGMA_OMP_CLAUSE_REDUCTION:
21727 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21728 c_name = "reduction";
21730 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21731 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21733 c_name = "schedule";
21735 case PRAGMA_OMP_CLAUSE_SHARED:
21736 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21740 case PRAGMA_OMP_CLAUSE_UNTIED:
21741 clauses = cp_parser_omp_clause_untied (parser, clauses,
21746 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21750 if (((mask >> c_kind) & 1) == 0)
21752 /* Remove the invalid clause(s) from the list to avoid
21753 confusing the rest of the compiler. */
21755 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21759 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21760 return finish_omp_clauses (clauses);
21767 In practice, we're also interested in adding the statement to an
21768 outer node. So it is convenient if we work around the fact that
21769 cp_parser_statement calls add_stmt. */
21772 cp_parser_begin_omp_structured_block (cp_parser *parser)
21774 unsigned save = parser->in_statement;
21776 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21777 This preserves the "not within loop or switch" style error messages
21778 for nonsense cases like
21784 if (parser->in_statement)
21785 parser->in_statement = IN_OMP_BLOCK;
21791 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21793 parser->in_statement = save;
21797 cp_parser_omp_structured_block (cp_parser *parser)
21799 tree stmt = begin_omp_structured_block ();
21800 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21802 cp_parser_statement (parser, NULL_TREE, false, NULL);
21804 cp_parser_end_omp_structured_block (parser, save);
21805 return finish_omp_structured_block (stmt);
21809 # pragma omp atomic new-line
21813 x binop= expr | x++ | ++x | x-- | --x
21815 +, *, -, /, &, ^, |, <<, >>
21817 where x is an lvalue expression with scalar type. */
21820 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21823 enum tree_code code;
21825 cp_parser_require_pragma_eol (parser, pragma_tok);
21827 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21828 /*cast_p=*/false, NULL);
21829 switch (TREE_CODE (lhs))
21834 case PREINCREMENT_EXPR:
21835 case POSTINCREMENT_EXPR:
21836 lhs = TREE_OPERAND (lhs, 0);
21838 rhs = integer_one_node;
21841 case PREDECREMENT_EXPR:
21842 case POSTDECREMENT_EXPR:
21843 lhs = TREE_OPERAND (lhs, 0);
21845 rhs = integer_one_node;
21849 switch (cp_lexer_peek_token (parser->lexer)->type)
21855 code = TRUNC_DIV_EXPR;
21863 case CPP_LSHIFT_EQ:
21864 code = LSHIFT_EXPR;
21866 case CPP_RSHIFT_EQ:
21867 code = RSHIFT_EXPR;
21870 code = BIT_AND_EXPR;
21873 code = BIT_IOR_EXPR;
21876 code = BIT_XOR_EXPR;
21879 cp_parser_error (parser,
21880 "invalid operator for %<#pragma omp atomic%>");
21883 cp_lexer_consume_token (parser->lexer);
21885 rhs = cp_parser_expression (parser, false, NULL);
21886 if (rhs == error_mark_node)
21890 finish_omp_atomic (code, lhs, rhs);
21891 cp_parser_consume_semicolon_at_end_of_statement (parser);
21895 cp_parser_skip_to_end_of_block_or_statement (parser);
21900 # pragma omp barrier new-line */
21903 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21905 cp_parser_require_pragma_eol (parser, pragma_tok);
21906 finish_omp_barrier ();
21910 # pragma omp critical [(name)] new-line
21911 structured-block */
21914 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21916 tree stmt, name = NULL;
21918 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21920 cp_lexer_consume_token (parser->lexer);
21922 name = cp_parser_identifier (parser);
21924 if (name == error_mark_node
21925 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21926 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21927 /*or_comma=*/false,
21928 /*consume_paren=*/true);
21929 if (name == error_mark_node)
21932 cp_parser_require_pragma_eol (parser, pragma_tok);
21934 stmt = cp_parser_omp_structured_block (parser);
21935 return c_finish_omp_critical (input_location, stmt, name);
21939 # pragma omp flush flush-vars[opt] new-line
21942 ( variable-list ) */
21945 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21947 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21948 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21949 cp_parser_require_pragma_eol (parser, pragma_tok);
21951 finish_omp_flush ();
21954 /* Helper function, to parse omp for increment expression. */
21957 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21959 tree cond = cp_parser_binary_expression (parser, false, true,
21960 PREC_NOT_OPERATOR, NULL);
21963 if (cond == error_mark_node
21964 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21966 cp_parser_skip_to_end_of_statement (parser);
21967 return error_mark_node;
21970 switch (TREE_CODE (cond))
21978 return error_mark_node;
21981 /* If decl is an iterator, preserve LHS and RHS of the relational
21982 expr until finish_omp_for. */
21984 && (type_dependent_expression_p (decl)
21985 || CLASS_TYPE_P (TREE_TYPE (decl))))
21988 return build_x_binary_op (TREE_CODE (cond),
21989 TREE_OPERAND (cond, 0), ERROR_MARK,
21990 TREE_OPERAND (cond, 1), ERROR_MARK,
21991 &overloaded_p, tf_warning_or_error);
21994 /* Helper function, to parse omp for increment expression. */
21997 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21999 cp_token *token = cp_lexer_peek_token (parser->lexer);
22005 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22007 op = (token->type == CPP_PLUS_PLUS
22008 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22009 cp_lexer_consume_token (parser->lexer);
22010 lhs = cp_parser_cast_expression (parser, false, false, NULL);
22012 return error_mark_node;
22013 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22016 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22018 return error_mark_node;
22020 token = cp_lexer_peek_token (parser->lexer);
22021 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22023 op = (token->type == CPP_PLUS_PLUS
22024 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22025 cp_lexer_consume_token (parser->lexer);
22026 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22029 op = cp_parser_assignment_operator_opt (parser);
22030 if (op == ERROR_MARK)
22031 return error_mark_node;
22033 if (op != NOP_EXPR)
22035 rhs = cp_parser_assignment_expression (parser, false, NULL);
22036 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22037 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22040 lhs = cp_parser_binary_expression (parser, false, false,
22041 PREC_ADDITIVE_EXPRESSION, NULL);
22042 token = cp_lexer_peek_token (parser->lexer);
22043 decl_first = lhs == decl;
22046 if (token->type != CPP_PLUS
22047 && token->type != CPP_MINUS)
22048 return error_mark_node;
22052 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22053 cp_lexer_consume_token (parser->lexer);
22054 rhs = cp_parser_binary_expression (parser, false, false,
22055 PREC_ADDITIVE_EXPRESSION, NULL);
22056 token = cp_lexer_peek_token (parser->lexer);
22057 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22059 if (lhs == NULL_TREE)
22061 if (op == PLUS_EXPR)
22064 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22067 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22068 NULL, tf_warning_or_error);
22071 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22075 if (rhs != decl || op == MINUS_EXPR)
22076 return error_mark_node;
22077 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22080 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22082 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22085 /* Parse the restricted form of the for statement allowed by OpenMP. */
22088 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22090 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22091 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22092 tree this_pre_body, cl;
22093 location_t loc_first;
22094 bool collapse_err = false;
22095 int i, collapse = 1, nbraces = 0;
22097 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22098 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22099 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22101 gcc_assert (collapse >= 1);
22103 declv = make_tree_vec (collapse);
22104 initv = make_tree_vec (collapse);
22105 condv = make_tree_vec (collapse);
22106 incrv = make_tree_vec (collapse);
22108 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22110 for (i = 0; i < collapse; i++)
22112 int bracecount = 0;
22113 bool add_private_clause = false;
22116 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22118 cp_parser_error (parser, "for statement expected");
22121 loc = cp_lexer_consume_token (parser->lexer)->location;
22123 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22126 init = decl = real_decl = NULL;
22127 this_pre_body = push_stmt_list ();
22128 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22130 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22134 integer-type var = lb
22135 random-access-iterator-type var = lb
22136 pointer-type var = lb
22138 cp_decl_specifier_seq type_specifiers;
22140 /* First, try to parse as an initialized declaration. See
22141 cp_parser_condition, from whence the bulk of this is copied. */
22143 cp_parser_parse_tentatively (parser);
22144 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22145 /*is_trailing_return=*/false,
22147 if (cp_parser_parse_definitely (parser))
22149 /* If parsing a type specifier seq succeeded, then this
22150 MUST be a initialized declaration. */
22151 tree asm_specification, attributes;
22152 cp_declarator *declarator;
22154 declarator = cp_parser_declarator (parser,
22155 CP_PARSER_DECLARATOR_NAMED,
22156 /*ctor_dtor_or_conv_p=*/NULL,
22157 /*parenthesized_p=*/NULL,
22158 /*member_p=*/false);
22159 attributes = cp_parser_attributes_opt (parser);
22160 asm_specification = cp_parser_asm_specification_opt (parser);
22162 if (declarator == cp_error_declarator)
22163 cp_parser_skip_to_end_of_statement (parser);
22167 tree pushed_scope, auto_node;
22169 decl = start_decl (declarator, &type_specifiers,
22170 SD_INITIALIZED, attributes,
22171 /*prefix_attributes=*/NULL_TREE,
22174 auto_node = type_uses_auto (TREE_TYPE (decl));
22175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22177 if (cp_lexer_next_token_is (parser->lexer,
22179 error ("parenthesized initialization is not allowed in "
22180 "OpenMP %<for%> loop");
22182 /* Trigger an error. */
22183 cp_parser_require (parser, CPP_EQ, "%<=%>");
22185 init = error_mark_node;
22186 cp_parser_skip_to_end_of_statement (parser);
22188 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22189 || type_dependent_expression_p (decl)
22192 bool is_direct_init, is_non_constant_init;
22194 init = cp_parser_initializer (parser,
22196 &is_non_constant_init);
22198 if (auto_node && describable_type (init))
22201 = do_auto_deduction (TREE_TYPE (decl), init,
22204 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22205 && !type_dependent_expression_p (decl))
22209 cp_finish_decl (decl, init, !is_non_constant_init,
22211 LOOKUP_ONLYCONVERTING);
22212 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22215 = tree_cons (NULL, this_pre_body, for_block);
22219 init = pop_stmt_list (this_pre_body);
22220 this_pre_body = NULL_TREE;
22225 cp_lexer_consume_token (parser->lexer);
22226 init = cp_parser_assignment_expression (parser, false, NULL);
22229 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22230 init = error_mark_node;
22232 cp_finish_decl (decl, NULL_TREE,
22233 /*init_const_expr_p=*/false,
22235 LOOKUP_ONLYCONVERTING);
22239 pop_scope (pushed_scope);
22245 /* If parsing a type specifier sequence failed, then
22246 this MUST be a simple expression. */
22247 cp_parser_parse_tentatively (parser);
22248 decl = cp_parser_primary_expression (parser, false, false,
22250 if (!cp_parser_error_occurred (parser)
22253 && CLASS_TYPE_P (TREE_TYPE (decl)))
22257 cp_parser_parse_definitely (parser);
22258 cp_parser_require (parser, CPP_EQ, "%<=%>");
22259 rhs = cp_parser_assignment_expression (parser, false, NULL);
22260 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22262 tf_warning_or_error));
22263 add_private_clause = true;
22268 cp_parser_abort_tentative_parse (parser);
22269 init = cp_parser_expression (parser, false, NULL);
22272 if (TREE_CODE (init) == MODIFY_EXPR
22273 || TREE_CODE (init) == MODOP_EXPR)
22274 real_decl = TREE_OPERAND (init, 0);
22279 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22282 this_pre_body = pop_stmt_list (this_pre_body);
22286 pre_body = push_stmt_list ();
22288 add_stmt (this_pre_body);
22289 pre_body = pop_stmt_list (pre_body);
22292 pre_body = this_pre_body;
22297 if (par_clauses != NULL && real_decl != NULL_TREE)
22300 for (c = par_clauses; *c ; )
22301 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22302 && OMP_CLAUSE_DECL (*c) == real_decl)
22304 error_at (loc, "iteration variable %qD"
22305 " should not be firstprivate", real_decl);
22306 *c = OMP_CLAUSE_CHAIN (*c);
22308 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22309 && OMP_CLAUSE_DECL (*c) == real_decl)
22311 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22312 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22313 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22314 OMP_CLAUSE_DECL (l) = real_decl;
22315 OMP_CLAUSE_CHAIN (l) = clauses;
22316 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22318 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22319 CP_OMP_CLAUSE_INFO (*c) = NULL;
22320 add_private_clause = false;
22324 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22325 && OMP_CLAUSE_DECL (*c) == real_decl)
22326 add_private_clause = false;
22327 c = &OMP_CLAUSE_CHAIN (*c);
22331 if (add_private_clause)
22334 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22336 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22338 && OMP_CLAUSE_DECL (c) == decl)
22340 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22341 && OMP_CLAUSE_DECL (c) == decl)
22342 error_at (loc, "iteration variable %qD "
22343 "should not be firstprivate",
22345 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22346 && OMP_CLAUSE_DECL (c) == decl)
22347 error_at (loc, "iteration variable %qD should not be reduction",
22352 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22353 OMP_CLAUSE_DECL (c) = decl;
22354 c = finish_omp_clauses (c);
22357 OMP_CLAUSE_CHAIN (c) = clauses;
22364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22365 cond = cp_parser_omp_for_cond (parser, decl);
22366 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22371 /* If decl is an iterator, preserve the operator on decl
22372 until finish_omp_for. */
22374 && (type_dependent_expression_p (decl)
22375 || CLASS_TYPE_P (TREE_TYPE (decl))))
22376 incr = cp_parser_omp_for_incr (parser, decl);
22378 incr = cp_parser_expression (parser, false, NULL);
22381 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22382 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22383 /*or_comma=*/false,
22384 /*consume_paren=*/true);
22386 TREE_VEC_ELT (declv, i) = decl;
22387 TREE_VEC_ELT (initv, i) = init;
22388 TREE_VEC_ELT (condv, i) = cond;
22389 TREE_VEC_ELT (incrv, i) = incr;
22391 if (i == collapse - 1)
22394 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22395 in between the collapsed for loops to be still considered perfectly
22396 nested. Hopefully the final version clarifies this.
22397 For now handle (multiple) {'s and empty statements. */
22398 cp_parser_parse_tentatively (parser);
22401 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22403 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22405 cp_lexer_consume_token (parser->lexer);
22408 else if (bracecount
22409 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22410 cp_lexer_consume_token (parser->lexer);
22413 loc = cp_lexer_peek_token (parser->lexer)->location;
22414 error_at (loc, "not enough collapsed for loops");
22415 collapse_err = true;
22416 cp_parser_abort_tentative_parse (parser);
22425 cp_parser_parse_definitely (parser);
22426 nbraces += bracecount;
22430 /* Note that we saved the original contents of this flag when we entered
22431 the structured block, and so we don't need to re-save it here. */
22432 parser->in_statement = IN_OMP_FOR;
22434 /* Note that the grammar doesn't call for a structured block here,
22435 though the loop as a whole is a structured block. */
22436 body = push_stmt_list ();
22437 cp_parser_statement (parser, NULL_TREE, false, NULL);
22438 body = pop_stmt_list (body);
22440 if (declv == NULL_TREE)
22443 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22444 pre_body, clauses);
22448 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22450 cp_lexer_consume_token (parser->lexer);
22453 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22454 cp_lexer_consume_token (parser->lexer);
22459 error_at (cp_lexer_peek_token (parser->lexer)->location,
22460 "collapsed loops not perfectly nested");
22462 collapse_err = true;
22463 cp_parser_statement_seq_opt (parser, NULL);
22464 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22471 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22472 for_block = TREE_CHAIN (for_block);
22479 #pragma omp for for-clause[optseq] new-line
22482 #define OMP_FOR_CLAUSE_MASK \
22483 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22484 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22485 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22486 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22487 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22488 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22489 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22490 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22493 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22495 tree clauses, sb, ret;
22498 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22499 "#pragma omp for", pragma_tok);
22501 sb = begin_omp_structured_block ();
22502 save = cp_parser_begin_omp_structured_block (parser);
22504 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22506 cp_parser_end_omp_structured_block (parser, save);
22507 add_stmt (finish_omp_structured_block (sb));
22513 # pragma omp master new-line
22514 structured-block */
22517 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22519 cp_parser_require_pragma_eol (parser, pragma_tok);
22520 return c_finish_omp_master (input_location,
22521 cp_parser_omp_structured_block (parser));
22525 # pragma omp ordered new-line
22526 structured-block */
22529 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22531 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22532 cp_parser_require_pragma_eol (parser, pragma_tok);
22533 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22539 { section-sequence }
22542 section-directive[opt] structured-block
22543 section-sequence section-directive structured-block */
22546 cp_parser_omp_sections_scope (cp_parser *parser)
22548 tree stmt, substmt;
22549 bool error_suppress = false;
22552 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22555 stmt = push_stmt_list ();
22557 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22561 substmt = begin_omp_structured_block ();
22562 save = cp_parser_begin_omp_structured_block (parser);
22566 cp_parser_statement (parser, NULL_TREE, false, NULL);
22568 tok = cp_lexer_peek_token (parser->lexer);
22569 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22571 if (tok->type == CPP_CLOSE_BRACE)
22573 if (tok->type == CPP_EOF)
22577 cp_parser_end_omp_structured_block (parser, save);
22578 substmt = finish_omp_structured_block (substmt);
22579 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22580 add_stmt (substmt);
22585 tok = cp_lexer_peek_token (parser->lexer);
22586 if (tok->type == CPP_CLOSE_BRACE)
22588 if (tok->type == CPP_EOF)
22591 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22593 cp_lexer_consume_token (parser->lexer);
22594 cp_parser_require_pragma_eol (parser, tok);
22595 error_suppress = false;
22597 else if (!error_suppress)
22599 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22600 error_suppress = true;
22603 substmt = cp_parser_omp_structured_block (parser);
22604 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22605 add_stmt (substmt);
22607 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22609 substmt = pop_stmt_list (stmt);
22611 stmt = make_node (OMP_SECTIONS);
22612 TREE_TYPE (stmt) = void_type_node;
22613 OMP_SECTIONS_BODY (stmt) = substmt;
22620 # pragma omp sections sections-clause[optseq] newline
22623 #define OMP_SECTIONS_CLAUSE_MASK \
22624 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22625 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22626 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22627 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22628 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22631 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22635 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22636 "#pragma omp sections", pragma_tok);
22638 ret = cp_parser_omp_sections_scope (parser);
22640 OMP_SECTIONS_CLAUSES (ret) = clauses;
22646 # pragma parallel parallel-clause new-line
22647 # pragma parallel for parallel-for-clause new-line
22648 # pragma parallel sections parallel-sections-clause new-line */
22650 #define OMP_PARALLEL_CLAUSE_MASK \
22651 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22652 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22653 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22654 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22655 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22656 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22657 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22658 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22661 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22663 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22664 const char *p_name = "#pragma omp parallel";
22665 tree stmt, clauses, par_clause, ws_clause, block;
22666 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22668 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22670 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22672 cp_lexer_consume_token (parser->lexer);
22673 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22674 p_name = "#pragma omp parallel for";
22675 mask |= OMP_FOR_CLAUSE_MASK;
22676 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22678 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22680 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22681 const char *p = IDENTIFIER_POINTER (id);
22682 if (strcmp (p, "sections") == 0)
22684 cp_lexer_consume_token (parser->lexer);
22685 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22686 p_name = "#pragma omp parallel sections";
22687 mask |= OMP_SECTIONS_CLAUSE_MASK;
22688 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22692 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22693 block = begin_omp_parallel ();
22694 save = cp_parser_begin_omp_structured_block (parser);
22698 case PRAGMA_OMP_PARALLEL:
22699 cp_parser_statement (parser, NULL_TREE, false, NULL);
22700 par_clause = clauses;
22703 case PRAGMA_OMP_PARALLEL_FOR:
22704 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22705 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22708 case PRAGMA_OMP_PARALLEL_SECTIONS:
22709 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22710 stmt = cp_parser_omp_sections_scope (parser);
22712 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22716 gcc_unreachable ();
22719 cp_parser_end_omp_structured_block (parser, save);
22720 stmt = finish_omp_parallel (par_clause, block);
22721 if (p_kind != PRAGMA_OMP_PARALLEL)
22722 OMP_PARALLEL_COMBINED (stmt) = 1;
22727 # pragma omp single single-clause[optseq] new-line
22728 structured-block */
22730 #define OMP_SINGLE_CLAUSE_MASK \
22731 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22732 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22733 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22734 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22737 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22739 tree stmt = make_node (OMP_SINGLE);
22740 TREE_TYPE (stmt) = void_type_node;
22742 OMP_SINGLE_CLAUSES (stmt)
22743 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22744 "#pragma omp single", pragma_tok);
22745 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22747 return add_stmt (stmt);
22751 # pragma omp task task-clause[optseq] new-line
22752 structured-block */
22754 #define OMP_TASK_CLAUSE_MASK \
22755 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22756 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22757 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22758 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22759 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22760 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22763 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22765 tree clauses, block;
22768 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22769 "#pragma omp task", pragma_tok);
22770 block = begin_omp_task ();
22771 save = cp_parser_begin_omp_structured_block (parser);
22772 cp_parser_statement (parser, NULL_TREE, false, NULL);
22773 cp_parser_end_omp_structured_block (parser, save);
22774 return finish_omp_task (clauses, block);
22778 # pragma omp taskwait new-line */
22781 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22783 cp_parser_require_pragma_eol (parser, pragma_tok);
22784 finish_omp_taskwait ();
22788 # pragma omp threadprivate (variable-list) */
22791 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22795 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22796 cp_parser_require_pragma_eol (parser, pragma_tok);
22798 finish_omp_threadprivate (vars);
22801 /* Main entry point to OpenMP statement pragmas. */
22804 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22808 switch (pragma_tok->pragma_kind)
22810 case PRAGMA_OMP_ATOMIC:
22811 cp_parser_omp_atomic (parser, pragma_tok);
22813 case PRAGMA_OMP_CRITICAL:
22814 stmt = cp_parser_omp_critical (parser, pragma_tok);
22816 case PRAGMA_OMP_FOR:
22817 stmt = cp_parser_omp_for (parser, pragma_tok);
22819 case PRAGMA_OMP_MASTER:
22820 stmt = cp_parser_omp_master (parser, pragma_tok);
22822 case PRAGMA_OMP_ORDERED:
22823 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22825 case PRAGMA_OMP_PARALLEL:
22826 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22828 case PRAGMA_OMP_SECTIONS:
22829 stmt = cp_parser_omp_sections (parser, pragma_tok);
22831 case PRAGMA_OMP_SINGLE:
22832 stmt = cp_parser_omp_single (parser, pragma_tok);
22834 case PRAGMA_OMP_TASK:
22835 stmt = cp_parser_omp_task (parser, pragma_tok);
22838 gcc_unreachable ();
22842 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22847 static GTY (()) cp_parser *the_parser;
22850 /* Special handling for the first token or line in the file. The first
22851 thing in the file might be #pragma GCC pch_preprocess, which loads a
22852 PCH file, which is a GC collection point. So we need to handle this
22853 first pragma without benefit of an existing lexer structure.
22855 Always returns one token to the caller in *FIRST_TOKEN. This is
22856 either the true first token of the file, or the first token after
22857 the initial pragma. */
22860 cp_parser_initial_pragma (cp_token *first_token)
22864 cp_lexer_get_preprocessor_token (NULL, first_token);
22865 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22868 cp_lexer_get_preprocessor_token (NULL, first_token);
22869 if (first_token->type == CPP_STRING)
22871 name = first_token->u.value;
22873 cp_lexer_get_preprocessor_token (NULL, first_token);
22874 if (first_token->type != CPP_PRAGMA_EOL)
22875 error_at (first_token->location,
22876 "junk at end of %<#pragma GCC pch_preprocess%>");
22879 error_at (first_token->location, "expected string literal");
22881 /* Skip to the end of the pragma. */
22882 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22883 cp_lexer_get_preprocessor_token (NULL, first_token);
22885 /* Now actually load the PCH file. */
22887 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22889 /* Read one more token to return to our caller. We have to do this
22890 after reading the PCH file in, since its pointers have to be
22892 cp_lexer_get_preprocessor_token (NULL, first_token);
22895 /* Normal parsing of a pragma token. Here we can (and must) use the
22899 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22901 cp_token *pragma_tok;
22904 pragma_tok = cp_lexer_consume_token (parser->lexer);
22905 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22906 parser->lexer->in_pragma = true;
22908 id = pragma_tok->pragma_kind;
22911 case PRAGMA_GCC_PCH_PREPROCESS:
22912 error_at (pragma_tok->location,
22913 "%<#pragma GCC pch_preprocess%> must be first");
22916 case PRAGMA_OMP_BARRIER:
22919 case pragma_compound:
22920 cp_parser_omp_barrier (parser, pragma_tok);
22923 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22924 "used in compound statements");
22931 case PRAGMA_OMP_FLUSH:
22934 case pragma_compound:
22935 cp_parser_omp_flush (parser, pragma_tok);
22938 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22939 "used in compound statements");
22946 case PRAGMA_OMP_TASKWAIT:
22949 case pragma_compound:
22950 cp_parser_omp_taskwait (parser, pragma_tok);
22953 error_at (pragma_tok->location,
22954 "%<#pragma omp taskwait%> may only be "
22955 "used in compound statements");
22962 case PRAGMA_OMP_THREADPRIVATE:
22963 cp_parser_omp_threadprivate (parser, pragma_tok);
22966 case PRAGMA_OMP_ATOMIC:
22967 case PRAGMA_OMP_CRITICAL:
22968 case PRAGMA_OMP_FOR:
22969 case PRAGMA_OMP_MASTER:
22970 case PRAGMA_OMP_ORDERED:
22971 case PRAGMA_OMP_PARALLEL:
22972 case PRAGMA_OMP_SECTIONS:
22973 case PRAGMA_OMP_SINGLE:
22974 case PRAGMA_OMP_TASK:
22975 if (context == pragma_external)
22977 cp_parser_omp_construct (parser, pragma_tok);
22980 case PRAGMA_OMP_SECTION:
22981 error_at (pragma_tok->location,
22982 "%<#pragma omp section%> may only be used in "
22983 "%<#pragma omp sections%> construct");
22987 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22988 c_invoke_pragma_handler (id);
22992 cp_parser_error (parser, "expected declaration specifiers");
22996 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23000 /* The interface the pragma parsers have to the lexer. */
23003 pragma_lex (tree *value)
23006 enum cpp_ttype ret;
23008 tok = cp_lexer_peek_token (the_parser->lexer);
23011 *value = tok->u.value;
23013 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23015 else if (ret == CPP_STRING)
23016 *value = cp_parser_string_literal (the_parser, false, false);
23019 cp_lexer_consume_token (the_parser->lexer);
23020 if (ret == CPP_KEYWORD)
23028 /* External interface. */
23030 /* Parse one entire translation unit. */
23033 c_parse_file (void)
23035 bool error_occurred;
23036 static bool already_called = false;
23038 if (already_called)
23040 sorry ("inter-module optimizations not implemented for C++");
23043 already_called = true;
23045 the_parser = cp_parser_new ();
23046 push_deferring_access_checks (flag_access_control
23047 ? dk_no_deferred : dk_no_check);
23048 error_occurred = cp_parser_translation_unit (the_parser);
23052 #include "gt-cp-parser.h"