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_RAW_STRINGS);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
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);
795 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
803 /* Start emitting debugging information. */
806 cp_lexer_start_debugging (cp_lexer* lexer)
808 lexer->debugging_p = true;
811 /* Stop emitting debugging information. */
814 cp_lexer_stop_debugging (cp_lexer* lexer)
816 lexer->debugging_p = false;
819 #endif /* ENABLE_CHECKING */
821 /* Create a new cp_token_cache, representing a range of tokens. */
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
826 cp_token_cache *cache = GGC_NEW (cp_token_cache);
827 cache->first = first;
833 /* Decl-specifiers. */
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
840 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 /* Nothing other than the parser should be creating declarators;
846 declarators are a semi-syntactic representation of C++ entities.
847 Other parts of the front end that need to create entities (like
848 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
850 static cp_declarator *make_call_declarator
851 (cp_declarator *, tree, cp_cv_quals, tree, tree);
852 static cp_declarator *make_array_declarator
853 (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855 (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857 (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859 (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861 (cp_cv_quals, tree, cp_declarator *);
863 /* An erroneous declarator. */
864 static cp_declarator *cp_error_declarator;
866 /* The obstack on which declarators and related data structures are
868 static struct obstack declarator_obstack;
870 /* Alloc BYTES from the declarator memory pool. */
873 alloc_declarator (size_t bytes)
875 return obstack_alloc (&declarator_obstack, bytes);
878 /* Allocate a declarator of the indicated KIND. Clear fields that are
879 common to all declarators. */
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
884 cp_declarator *declarator;
886 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887 declarator->kind = kind;
888 declarator->attributes = NULL_TREE;
889 declarator->declarator = NULL;
890 declarator->parameter_pack_p = false;
895 /* Make a declarator for a generalized identifier. If
896 QUALIFYING_SCOPE is non-NULL, the identifier is
897 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898 UNQUALIFIED_NAME. SFK indicates the kind of special function this
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903 special_function_kind sfk)
905 cp_declarator *declarator;
907 /* It is valid to write:
909 class C { void f(); };
913 The standard is not clear about whether `typedef const C D' is
914 legal; as of 2002-09-15 the committee is considering that
915 question. EDG 3.0 allows that syntax. Therefore, we do as
917 if (qualifying_scope && TYPE_P (qualifying_scope))
918 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
920 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
924 declarator = make_declarator (cdk_id);
925 declarator->u.id.qualifying_scope = qualifying_scope;
926 declarator->u.id.unqualified_name = unqualified_name;
927 declarator->u.id.sfk = sfk;
932 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
933 of modifiers such as const or volatile to apply to the pointer
934 type, represented as identifiers. */
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
939 cp_declarator *declarator;
941 declarator = make_declarator (cdk_pointer);
942 declarator->declarator = target;
943 declarator->u.pointer.qualifiers = cv_qualifiers;
944 declarator->u.pointer.class_type = NULL_TREE;
947 declarator->parameter_pack_p = target->parameter_pack_p;
948 target->parameter_pack_p = false;
951 declarator->parameter_pack_p = false;
956 /* Like make_pointer_declarator -- but for references. */
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962 cp_declarator *declarator;
964 declarator = make_declarator (cdk_reference);
965 declarator->declarator = target;
966 declarator->u.reference.qualifiers = cv_qualifiers;
967 declarator->u.reference.rvalue_ref = rvalue_ref;
970 declarator->parameter_pack_p = target->parameter_pack_p;
971 target->parameter_pack_p = false;
974 declarator->parameter_pack_p = false;
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980 member of CLASS_TYPE. */
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984 cp_declarator *pointee)
986 cp_declarator *declarator;
988 declarator = make_declarator (cdk_ptrmem);
989 declarator->declarator = pointee;
990 declarator->u.pointer.qualifiers = cv_qualifiers;
991 declarator->u.pointer.class_type = class_type;
995 declarator->parameter_pack_p = pointee->parameter_pack_p;
996 pointee->parameter_pack_p = false;
999 declarator->parameter_pack_p = false;
1004 /* Make a declarator for the function given by TARGET, with the
1005 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1006 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1007 indicates what exceptions can be thrown. */
1010 make_call_declarator (cp_declarator *target,
1012 cp_cv_quals cv_qualifiers,
1013 tree exception_specification,
1014 tree late_return_type)
1016 cp_declarator *declarator;
1018 declarator = make_declarator (cdk_function);
1019 declarator->declarator = target;
1020 declarator->u.function.parameters = parms;
1021 declarator->u.function.qualifiers = cv_qualifiers;
1022 declarator->u.function.exception_specification = exception_specification;
1023 declarator->u.function.late_return_type = late_return_type;
1026 declarator->parameter_pack_p = target->parameter_pack_p;
1027 target->parameter_pack_p = false;
1030 declarator->parameter_pack_p = false;
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036 defined by ELEMENT. */
1039 make_array_declarator (cp_declarator *element, tree bounds)
1041 cp_declarator *declarator;
1043 declarator = make_declarator (cdk_array);
1044 declarator->declarator = element;
1045 declarator->u.array.bounds = bounds;
1048 declarator->parameter_pack_p = element->parameter_pack_p;
1049 element->parameter_pack_p = false;
1052 declarator->parameter_pack_p = false;
1057 /* Determine whether the declarator we've seen so far can be a
1058 parameter pack, when followed by an ellipsis. */
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 /* Search for a declarator name, or any other declarator that goes
1063 after the point where the ellipsis could appear in a parameter
1064 pack. If we find any of these, then this declarator can not be
1065 made into a parameter pack. */
1067 while (declarator && !found)
1069 switch ((int)declarator->kind)
1080 declarator = declarator->declarator;
1088 cp_parameter_declarator *no_parameters;
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091 DECLARATOR and DEFAULT_ARGUMENT. */
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095 cp_declarator *declarator,
1096 tree default_argument)
1098 cp_parameter_declarator *parameter;
1100 parameter = ((cp_parameter_declarator *)
1101 alloc_declarator (sizeof (cp_parameter_declarator)));
1102 parameter->next = NULL;
1103 if (decl_specifiers)
1104 parameter->decl_specifiers = *decl_specifiers;
1106 clear_decl_specs (¶meter->decl_specifiers);
1107 parameter->declarator = declarator;
1108 parameter->default_argument = default_argument;
1109 parameter->ellipsis_p = false;
1114 /* Returns true iff DECLARATOR is a declaration for a function. */
1117 function_declarator_p (const cp_declarator *declarator)
1121 if (declarator->kind == cdk_function
1122 && declarator->declarator->kind == cdk_id)
1124 if (declarator->kind == cdk_id
1125 || declarator->kind == cdk_error)
1127 declarator = declarator->declarator;
1137 A cp_parser parses the token stream as specified by the C++
1138 grammar. Its job is purely parsing, not semantic analysis. For
1139 example, the parser breaks the token stream into declarators,
1140 expressions, statements, and other similar syntactic constructs.
1141 It does not check that the types of the expressions on either side
1142 of an assignment-statement are compatible, or that a function is
1143 not declared with a parameter of type `void'.
1145 The parser invokes routines elsewhere in the compiler to perform
1146 semantic analysis and to build up the abstract syntax tree for the
1149 The parser (and the template instantiation code, which is, in a
1150 way, a close relative of parsing) are the only parts of the
1151 compiler that should be calling push_scope and pop_scope, or
1152 related functions. The parser (and template instantiation code)
1153 keeps track of what scope is presently active; everything else
1154 should simply honor that. (The code that generates static
1155 initializers may also need to set the scope, in order to check
1156 access control correctly when emitting the initializers.)
1161 The parser is of the standard recursive-descent variety. Upcoming
1162 tokens in the token stream are examined in order to determine which
1163 production to use when parsing a non-terminal. Some C++ constructs
1164 require arbitrary look ahead to disambiguate. For example, it is
1165 impossible, in the general case, to tell whether a statement is an
1166 expression or declaration without scanning the entire statement.
1167 Therefore, the parser is capable of "parsing tentatively." When the
1168 parser is not sure what construct comes next, it enters this mode.
1169 Then, while we attempt to parse the construct, the parser queues up
1170 error messages, rather than issuing them immediately, and saves the
1171 tokens it consumes. If the construct is parsed successfully, the
1172 parser "commits", i.e., it issues any queued error messages and
1173 the tokens that were being preserved are permanently discarded.
1174 If, however, the construct is not parsed successfully, the parser
1175 rolls back its state completely so that it can resume parsing using
1176 a different alternative.
1181 The performance of the parser could probably be improved substantially.
1182 We could often eliminate the need to parse tentatively by looking ahead
1183 a little bit. In some places, this approach might not entirely eliminate
1184 the need to parse tentatively, but it might still speed up the average
1187 /* Flags that are passed to some parsing functions. These values can
1188 be bitwise-ored together. */
1193 CP_PARSER_FLAGS_NONE = 0x0,
1194 /* The construct is optional. If it is not present, then no error
1195 should be issued. */
1196 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197 /* When parsing a type-specifier, do not allow user-defined types. */
1198 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1201 /* This type is used for parameters and variables which hold
1202 combinations of the above flags. */
1203 typedef int cp_parser_flags;
1205 /* The different kinds of declarators we want to parse. */
1207 typedef enum cp_parser_declarator_kind
1209 /* We want an abstract declarator. */
1210 CP_PARSER_DECLARATOR_ABSTRACT,
1211 /* We want a named declarator. */
1212 CP_PARSER_DECLARATOR_NAMED,
1213 /* We don't mind, but the name must be an unqualified-id. */
1214 CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1217 /* The precedence values used to parse binary expressions. The minimum value
1218 of PREC must be 1, because zero is reserved to quickly discriminate
1219 binary operators from other tokens. */
1224 PREC_LOGICAL_OR_EXPRESSION,
1225 PREC_LOGICAL_AND_EXPRESSION,
1226 PREC_INCLUSIVE_OR_EXPRESSION,
1227 PREC_EXCLUSIVE_OR_EXPRESSION,
1228 PREC_AND_EXPRESSION,
1229 PREC_EQUALITY_EXPRESSION,
1230 PREC_RELATIONAL_EXPRESSION,
1231 PREC_SHIFT_EXPRESSION,
1232 PREC_ADDITIVE_EXPRESSION,
1233 PREC_MULTIPLICATIVE_EXPRESSION,
1235 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239 precedence value. */
1241 typedef struct cp_parser_binary_operations_map_node
1243 /* The token type. */
1244 enum cpp_ttype token_type;
1245 /* The corresponding tree code. */
1246 enum tree_code tree_type;
1247 /* The precedence of this operator. */
1248 enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1251 /* The status of a tentative parse. */
1253 typedef enum cp_parser_status_kind
1255 /* No errors have occurred. */
1256 CP_PARSER_STATUS_KIND_NO_ERROR,
1257 /* An error has occurred. */
1258 CP_PARSER_STATUS_KIND_ERROR,
1259 /* We are committed to this tentative parse, whether or not an error
1261 CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1264 typedef struct cp_parser_expression_stack_entry
1266 /* Left hand side of the binary operation we are currently
1269 /* Original tree code for left hand side, if it was a binary
1270 expression itself (used for -Wparentheses). */
1271 enum tree_code lhs_type;
1272 /* Tree code for the binary operation we are parsing. */
1273 enum tree_code tree_type;
1274 /* Precedence of the binary operation we are parsing. */
1275 enum cp_parser_prec prec;
1276 } cp_parser_expression_stack_entry;
1278 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1279 entries because precedence levels on the stack are monotonically
1281 typedef struct cp_parser_expression_stack_entry
1282 cp_parser_expression_stack[NUM_PREC_VALUES];
1284 /* Context that is saved and restored when parsing tentatively. */
1285 typedef struct GTY (()) cp_parser_context {
1286 /* If this is a tentative parsing context, the status of the
1288 enum cp_parser_status_kind status;
1289 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1290 that are looked up in this context must be looked up both in the
1291 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292 the context of the containing expression. */
1295 /* The next parsing context in the stack. */
1296 struct cp_parser_context *next;
1297 } cp_parser_context;
1301 /* Constructors and destructors. */
1303 static cp_parser_context *cp_parser_context_new
1304 (cp_parser_context *);
1306 /* Class variables. */
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311 Transformed into an associative array (binops_by_token) by
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1318 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1338 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1340 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1342 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1344 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 /* The same as binops, but initialized by cp_parser_new so that
1348 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1352 /* Constructors and destructors. */
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1360 cp_parser_context *context;
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1371 context = GGC_CNEW (cp_parser_context);
1373 /* No errors have occurred yet in this context. */
1374 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375 /* If this is not the bottommost context, copy information that we
1376 need from the previous context. */
1379 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380 expression, then we are parsing one in this context, too. */
1381 context->object_type = next->object_type;
1382 /* Thread the stack. */
1383 context->next = next;
1389 /* The cp_parser structure represents the C++ parser. */
1391 typedef struct GTY(()) cp_parser {
1392 /* The lexer from which we are obtaining tokens. */
1395 /* The scope in which names should be looked up. If NULL_TREE, then
1396 we look up names in the scope that is currently open in the
1397 source program. If non-NULL, this is either a TYPE or
1398 NAMESPACE_DECL for the scope in which we should look. It can
1399 also be ERROR_MARK, when we've parsed a bogus scope.
1401 This value is not cleared automatically after a name is looked
1402 up, so we must be careful to clear it before starting a new look
1403 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1404 will look up `Z' in the scope of `X', rather than the current
1405 scope.) Unfortunately, it is difficult to tell when name lookup
1406 is complete, because we sometimes peek at a token, look it up,
1407 and then decide not to consume it. */
1410 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411 last lookup took place. OBJECT_SCOPE is used if an expression
1412 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413 respectively. QUALIFYING_SCOPE is used for an expression of the
1414 form "X::Y"; it refers to X. */
1416 tree qualifying_scope;
1418 /* A stack of parsing contexts. All but the bottom entry on the
1419 stack will be tentative contexts.
1421 We parse tentatively in order to determine which construct is in
1422 use in some situations. For example, in order to determine
1423 whether a statement is an expression-statement or a
1424 declaration-statement we parse it tentatively as a
1425 declaration-statement. If that fails, we then reparse the same
1426 token stream as an expression-statement. */
1427 cp_parser_context *context;
1429 /* True if we are parsing GNU C++. If this flag is not set, then
1430 GNU extensions are not recognized. */
1431 bool allow_gnu_extensions_p;
1433 /* TRUE if the `>' token should be interpreted as the greater-than
1434 operator. FALSE if it is the end of a template-id or
1435 template-parameter-list. In C++0x mode, this flag also applies to
1436 `>>' tokens, which are viewed as two consecutive `>' tokens when
1437 this flag is FALSE. */
1438 bool greater_than_is_operator_p;
1440 /* TRUE if default arguments are allowed within a parameter list
1441 that starts at this point. FALSE if only a gnu extension makes
1442 them permissible. */
1443 bool default_arg_ok_p;
1445 /* TRUE if we are parsing an integral constant-expression. See
1446 [expr.const] for a precise definition. */
1447 bool integral_constant_expression_p;
1449 /* TRUE if we are parsing an integral constant-expression -- but a
1450 non-constant expression should be permitted as well. This flag
1451 is used when parsing an array bound so that GNU variable-length
1452 arrays are tolerated. */
1453 bool allow_non_integral_constant_expression_p;
1455 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456 been seen that makes the expression non-constant. */
1457 bool non_integral_constant_expression_p;
1459 /* TRUE if local variable names and `this' are forbidden in the
1461 bool local_variables_forbidden_p;
1463 /* TRUE if the declaration we are parsing is part of a
1464 linkage-specification of the form `extern string-literal
1466 bool in_unbraced_linkage_specification_p;
1468 /* TRUE if we are presently parsing a declarator, after the
1469 direct-declarator. */
1470 bool in_declarator_p;
1472 /* TRUE if we are presently parsing a template-argument-list. */
1473 bool in_template_argument_list_p;
1475 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476 to IN_OMP_BLOCK if parsing OpenMP structured block and
1477 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1478 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479 iteration-statement, OpenMP block or loop within that switch. */
1480 #define IN_SWITCH_STMT 1
1481 #define IN_ITERATION_STMT 2
1482 #define IN_OMP_BLOCK 4
1483 #define IN_OMP_FOR 8
1484 #define IN_IF_STMT 16
1485 unsigned char in_statement;
1487 /* TRUE if we are presently parsing the body of a switch statement.
1488 Note that this doesn't quite overlap with in_statement above.
1489 The difference relates to giving the right sets of error messages:
1490 "case not in switch" vs "break statement used with OpenMP...". */
1491 bool in_switch_statement_p;
1493 /* TRUE if we are parsing a type-id in an expression context. In
1494 such a situation, both "type (expr)" and "type (type)" are valid
1496 bool in_type_id_in_expr_p;
1498 /* TRUE if we are currently in a header file where declarations are
1499 implicitly extern "C". */
1500 bool implicit_extern_c;
1502 /* TRUE if strings in expressions should be translated to the execution
1504 bool translate_strings_p;
1506 /* TRUE if we are presently parsing the body of a function, but not
1508 bool in_function_body;
1510 /* If non-NULL, then we are parsing a construct where new type
1511 definitions are not permitted. The string stored here will be
1512 issued as an error message if a type is defined. */
1513 const char *type_definition_forbidden_message;
1515 /* A list of lists. The outer list is a stack, used for member
1516 functions of local classes. At each level there are two sub-list,
1517 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519 TREE_VALUE's. The functions are chained in reverse declaration
1522 The TREE_PURPOSE sublist contains those functions with default
1523 arguments that need post processing, and the TREE_VALUE sublist
1524 contains those functions with definitions that need post
1527 These lists can only be processed once the outermost class being
1528 defined is complete. */
1529 tree unparsed_functions_queues;
1531 /* The number of classes whose definitions are currently in
1533 unsigned num_classes_being_defined;
1535 /* The number of template parameter lists that apply directly to the
1536 current declaration. */
1537 unsigned num_template_parameter_lists;
1542 /* Constructors and destructors. */
1544 static cp_parser *cp_parser_new
1547 /* Routines to parse various constructs.
1549 Those that return `tree' will return the error_mark_node (rather
1550 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551 Sometimes, they will return an ordinary node if error-recovery was
1552 attempted, even though a parse error occurred. So, to check
1553 whether or not a parse error occurred, you should always use
1554 cp_parser_error_occurred. If the construct is optional (indicated
1555 either by an `_opt' in the name of the function that does the
1556 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557 the construct is not present. */
1559 /* Lexical conventions [gram.lex] */
1561 static tree cp_parser_identifier
1563 static tree cp_parser_string_literal
1564 (cp_parser *, bool, bool);
1566 /* Basic concepts [gram.basic] */
1568 static bool cp_parser_translation_unit
1571 /* Expressions [gram.expr] */
1573 static tree cp_parser_primary_expression
1574 (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576 (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582 (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_qualifying_entity
1584 (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586 (cp_parser *, bool, bool, bool, cp_id_kind *);
1587 static tree cp_parser_postfix_open_square_expression
1588 (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1591 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1592 (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594 (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596 (cp_parser *, bool, bool, cp_id_kind *);
1597 static enum tree_code cp_parser_unary_operator
1599 static tree cp_parser_new_expression
1601 static VEC(tree,gc) *cp_parser_new_placement
1603 static tree cp_parser_new_type_id
1604 (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1607 static cp_declarator *cp_parser_direct_new_declarator
1609 static VEC(tree,gc) *cp_parser_new_initializer
1611 static tree cp_parser_delete_expression
1613 static tree cp_parser_cast_expression
1614 (cp_parser *, bool, bool, cp_id_kind *);
1615 static tree cp_parser_binary_expression
1616 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1617 static tree cp_parser_question_colon_clause
1618 (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620 (cp_parser *, bool, cp_id_kind *);
1621 static enum tree_code cp_parser_assignment_operator_opt
1623 static tree cp_parser_expression
1624 (cp_parser *, bool, cp_id_kind *);
1625 static tree cp_parser_constant_expression
1626 (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1630 /* Statements [gram.stmt.stmt] */
1632 static void cp_parser_statement
1633 (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1636 static tree cp_parser_expression_statement
1637 (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639 (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641 (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643 (cp_parser *, bool *);
1644 static tree cp_parser_condition
1646 static tree cp_parser_iteration_statement
1648 static void cp_parser_for_init_statement
1650 static tree cp_parser_jump_statement
1652 static void cp_parser_declaration_statement
1655 static tree cp_parser_implicitly_scoped_statement
1656 (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1660 /* Declarations [gram.dcl.dcl] */
1662 static void cp_parser_declaration_seq_opt
1664 static void cp_parser_declaration
1666 static void cp_parser_block_declaration
1667 (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669 (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1674 static tree cp_parser_function_specifier_opt
1675 (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679 static tree cp_parser_simple_type_specifier
1680 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1683 static tree cp_parser_nonclass_name
1684 (cp_parser* parser);
1685 static tree cp_parser_elaborated_type_specifier
1686 (cp_parser *, bool, bool);
1687 static tree cp_parser_enum_specifier
1689 static void cp_parser_enumerator_list
1690 (cp_parser *, tree);
1691 static void cp_parser_enumerator_definition
1692 (cp_parser *, tree);
1693 static tree cp_parser_namespace_name
1695 static void cp_parser_namespace_definition
1697 static void cp_parser_namespace_body
1699 static tree cp_parser_qualified_namespace_specifier
1701 static void cp_parser_namespace_alias_definition
1703 static bool cp_parser_using_declaration
1704 (cp_parser *, bool);
1705 static void cp_parser_using_directive
1707 static void cp_parser_asm_definition
1709 static void cp_parser_linkage_specification
1711 static void cp_parser_static_assert
1712 (cp_parser *, bool);
1713 static tree cp_parser_decltype
1716 /* Declarators [gram.dcl.decl] */
1718 static tree cp_parser_init_declarator
1719 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1720 static cp_declarator *cp_parser_declarator
1721 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1722 static cp_declarator *cp_parser_direct_declarator
1723 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1724 static enum tree_code cp_parser_ptr_operator
1725 (cp_parser *, tree *, cp_cv_quals *);
1726 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728 static tree cp_parser_late_return_type_opt
1730 static tree cp_parser_declarator_id
1731 (cp_parser *, bool);
1732 static tree cp_parser_type_id
1734 static tree cp_parser_template_type_arg
1736 static tree cp_parser_type_id_1
1737 (cp_parser *, bool);
1738 static void cp_parser_type_specifier_seq
1739 (cp_parser *, bool, cp_decl_specifier_seq *);
1740 static tree cp_parser_parameter_declaration_clause
1742 static tree cp_parser_parameter_declaration_list
1743 (cp_parser *, bool *);
1744 static cp_parameter_declarator *cp_parser_parameter_declaration
1745 (cp_parser *, bool, bool *);
1746 static tree cp_parser_default_argument
1747 (cp_parser *, bool);
1748 static void cp_parser_function_body
1750 static tree cp_parser_initializer
1751 (cp_parser *, bool *, bool *);
1752 static tree cp_parser_initializer_clause
1753 (cp_parser *, bool *);
1754 static tree cp_parser_braced_list
1755 (cp_parser*, bool*);
1756 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1757 (cp_parser *, bool *);
1759 static bool cp_parser_ctor_initializer_opt_and_function_body
1762 /* Classes [gram.class] */
1764 static tree cp_parser_class_name
1765 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1766 static tree cp_parser_class_specifier
1768 static tree cp_parser_class_head
1769 (cp_parser *, bool *, tree *, tree *);
1770 static enum tag_types cp_parser_class_key
1772 static void cp_parser_member_specification_opt
1774 static void cp_parser_member_declaration
1776 static tree cp_parser_pure_specifier
1778 static tree cp_parser_constant_initializer
1781 /* Derived classes [gram.class.derived] */
1783 static tree cp_parser_base_clause
1785 static tree cp_parser_base_specifier
1788 /* Special member functions [gram.special] */
1790 static tree cp_parser_conversion_function_id
1792 static tree cp_parser_conversion_type_id
1794 static cp_declarator *cp_parser_conversion_declarator_opt
1796 static bool cp_parser_ctor_initializer_opt
1798 static void cp_parser_mem_initializer_list
1800 static tree cp_parser_mem_initializer
1802 static tree cp_parser_mem_initializer_id
1805 /* Overloading [gram.over] */
1807 static tree cp_parser_operator_function_id
1809 static tree cp_parser_operator
1812 /* Templates [gram.temp] */
1814 static void cp_parser_template_declaration
1815 (cp_parser *, bool);
1816 static tree cp_parser_template_parameter_list
1818 static tree cp_parser_template_parameter
1819 (cp_parser *, bool *, bool *);
1820 static tree cp_parser_type_parameter
1821 (cp_parser *, bool *);
1822 static tree cp_parser_template_id
1823 (cp_parser *, bool, bool, bool);
1824 static tree cp_parser_template_name
1825 (cp_parser *, bool, bool, bool, bool *);
1826 static tree cp_parser_template_argument_list
1828 static tree cp_parser_template_argument
1830 static void cp_parser_explicit_instantiation
1832 static void cp_parser_explicit_specialization
1835 /* Exception handling [gram.exception] */
1837 static tree cp_parser_try_block
1839 static bool cp_parser_function_try_block
1841 static void cp_parser_handler_seq
1843 static void cp_parser_handler
1845 static tree cp_parser_exception_declaration
1847 static tree cp_parser_throw_expression
1849 static tree cp_parser_exception_specification_opt
1851 static tree cp_parser_type_id_list
1854 /* GNU Extensions */
1856 static tree cp_parser_asm_specification_opt
1858 static tree cp_parser_asm_operand_list
1860 static tree cp_parser_asm_clobber_list
1862 static tree cp_parser_asm_label_list
1864 static tree cp_parser_attributes_opt
1866 static tree cp_parser_attribute_list
1868 static bool cp_parser_extension_opt
1869 (cp_parser *, int *);
1870 static void cp_parser_label_declaration
1873 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1874 static bool cp_parser_pragma
1875 (cp_parser *, enum pragma_context);
1877 /* Objective-C++ Productions */
1879 static tree cp_parser_objc_message_receiver
1881 static tree cp_parser_objc_message_args
1883 static tree cp_parser_objc_message_expression
1885 static tree cp_parser_objc_encode_expression
1887 static tree cp_parser_objc_defs_expression
1889 static tree cp_parser_objc_protocol_expression
1891 static tree cp_parser_objc_selector_expression
1893 static tree cp_parser_objc_expression
1895 static bool cp_parser_objc_selector_p
1897 static tree cp_parser_objc_selector
1899 static tree cp_parser_objc_protocol_refs_opt
1901 static void cp_parser_objc_declaration
1903 static tree cp_parser_objc_statement
1906 /* Utility Routines */
1908 static tree cp_parser_lookup_name
1909 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1910 static tree cp_parser_lookup_name_simple
1911 (cp_parser *, tree, location_t);
1912 static tree cp_parser_maybe_treat_template_as_class
1914 static bool cp_parser_check_declarator_template_parameters
1915 (cp_parser *, cp_declarator *, location_t);
1916 static bool cp_parser_check_template_parameters
1917 (cp_parser *, unsigned, location_t, cp_declarator *);
1918 static tree cp_parser_simple_cast_expression
1920 static tree cp_parser_global_scope_opt
1921 (cp_parser *, bool);
1922 static bool cp_parser_constructor_declarator_p
1923 (cp_parser *, bool);
1924 static tree cp_parser_function_definition_from_specifiers_and_declarator
1925 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1926 static tree cp_parser_function_definition_after_declarator
1927 (cp_parser *, bool);
1928 static void cp_parser_template_declaration_after_export
1929 (cp_parser *, bool);
1930 static void cp_parser_perform_template_parameter_access_checks
1931 (VEC (deferred_access_check,gc)*);
1932 static tree cp_parser_single_declaration
1933 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1934 static tree cp_parser_functional_cast
1935 (cp_parser *, tree);
1936 static tree cp_parser_save_member_function_body
1937 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1938 static tree cp_parser_enclosed_template_argument_list
1940 static void cp_parser_save_default_args
1941 (cp_parser *, tree);
1942 static void cp_parser_late_parsing_for_member
1943 (cp_parser *, tree);
1944 static void cp_parser_late_parsing_default_args
1945 (cp_parser *, tree);
1946 static tree cp_parser_sizeof_operand
1947 (cp_parser *, enum rid);
1948 static tree cp_parser_trait_expr
1949 (cp_parser *, enum rid);
1950 static bool cp_parser_declares_only_class_p
1952 static void cp_parser_set_storage_class
1953 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1954 static void cp_parser_set_decl_spec_type
1955 (cp_decl_specifier_seq *, tree, location_t, bool);
1956 static bool cp_parser_friend_p
1957 (const cp_decl_specifier_seq *);
1958 static cp_token *cp_parser_require
1959 (cp_parser *, enum cpp_ttype, const char *);
1960 static cp_token *cp_parser_require_keyword
1961 (cp_parser *, enum rid, const char *);
1962 static bool cp_parser_token_starts_function_definition_p
1964 static bool cp_parser_next_token_starts_class_definition_p
1966 static bool cp_parser_next_token_ends_template_argument_p
1968 static bool cp_parser_nth_token_starts_template_argument_list_p
1969 (cp_parser *, size_t);
1970 static enum tag_types cp_parser_token_is_class_key
1972 static void cp_parser_check_class_key
1973 (enum tag_types, tree type);
1974 static void cp_parser_check_access_in_redeclaration
1975 (tree type, location_t location);
1976 static bool cp_parser_optional_template_keyword
1978 static void cp_parser_pre_parsed_nested_name_specifier
1980 static bool cp_parser_cache_group
1981 (cp_parser *, enum cpp_ttype, unsigned);
1982 static void cp_parser_parse_tentatively
1984 static void cp_parser_commit_to_tentative_parse
1986 static void cp_parser_abort_tentative_parse
1988 static bool cp_parser_parse_definitely
1990 static inline bool cp_parser_parsing_tentatively
1992 static bool cp_parser_uncommitted_to_tentative_parse_p
1994 static void cp_parser_error
1995 (cp_parser *, const char *);
1996 static void cp_parser_name_lookup_error
1997 (cp_parser *, tree, tree, const char *, location_t);
1998 static bool cp_parser_simulate_error
2000 static bool cp_parser_check_type_definition
2002 static void cp_parser_check_for_definition_in_return_type
2003 (cp_declarator *, tree, location_t type_location);
2004 static void cp_parser_check_for_invalid_template_id
2005 (cp_parser *, tree, location_t location);
2006 static bool cp_parser_non_integral_constant_expression
2007 (cp_parser *, const char *);
2008 static void cp_parser_diagnose_invalid_type_name
2009 (cp_parser *, tree, tree, location_t);
2010 static bool cp_parser_parse_and_diagnose_invalid_type_name
2012 static int cp_parser_skip_to_closing_parenthesis
2013 (cp_parser *, bool, bool, bool);
2014 static void cp_parser_skip_to_end_of_statement
2016 static void cp_parser_consume_semicolon_at_end_of_statement
2018 static void cp_parser_skip_to_end_of_block_or_statement
2020 static bool cp_parser_skip_to_closing_brace
2022 static void cp_parser_skip_to_end_of_template_parameter_list
2024 static void cp_parser_skip_to_pragma_eol
2025 (cp_parser*, cp_token *);
2026 static bool cp_parser_error_occurred
2028 static bool cp_parser_allow_gnu_extensions_p
2030 static bool cp_parser_is_string_literal
2032 static bool cp_parser_is_keyword
2033 (cp_token *, enum rid);
2034 static tree cp_parser_make_typename_type
2035 (cp_parser *, tree, tree, location_t location);
2036 static cp_declarator * cp_parser_make_indirect_declarator
2037 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2039 /* Returns nonzero if we are parsing tentatively. */
2042 cp_parser_parsing_tentatively (cp_parser* parser)
2044 return parser->context->next != NULL;
2047 /* Returns nonzero if TOKEN is a string literal. */
2050 cp_parser_is_string_literal (cp_token* token)
2052 return (token->type == CPP_STRING ||
2053 token->type == CPP_STRING16 ||
2054 token->type == CPP_STRING32 ||
2055 token->type == CPP_WSTRING);
2058 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2061 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2063 return token->keyword == keyword;
2066 /* If not parsing tentatively, issue a diagnostic of the form
2067 FILE:LINE: MESSAGE before TOKEN
2068 where TOKEN is the next token in the input stream. MESSAGE
2069 (specified by the caller) is usually of the form "expected
2073 cp_parser_error (cp_parser* parser, const char* message)
2075 if (!cp_parser_simulate_error (parser))
2077 cp_token *token = cp_lexer_peek_token (parser->lexer);
2078 /* This diagnostic makes more sense if it is tagged to the line
2079 of the token we just peeked at. */
2080 cp_lexer_set_source_position_from_token (token);
2082 if (token->type == CPP_PRAGMA)
2084 error_at (token->location,
2085 "%<#pragma%> is not allowed here");
2086 cp_parser_skip_to_pragma_eol (parser, token);
2090 c_parse_error (message,
2091 /* Because c_parser_error does not understand
2092 CPP_KEYWORD, keywords are treated like
2094 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2095 token->u.value, token->flags);
2099 /* Issue an error about name-lookup failing. NAME is the
2100 IDENTIFIER_NODE DECL is the result of
2101 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2102 the thing that we hoped to find. */
2105 cp_parser_name_lookup_error (cp_parser* parser,
2108 const char* desired,
2109 location_t location)
2111 /* If name lookup completely failed, tell the user that NAME was not
2113 if (decl == error_mark_node)
2115 if (parser->scope && parser->scope != global_namespace)
2116 error_at (location, "%<%E::%E%> has not been declared",
2117 parser->scope, name);
2118 else if (parser->scope == global_namespace)
2119 error_at (location, "%<::%E%> has not been declared", name);
2120 else if (parser->object_scope
2121 && !CLASS_TYPE_P (parser->object_scope))
2122 error_at (location, "request for member %qE in non-class type %qT",
2123 name, parser->object_scope);
2124 else if (parser->object_scope)
2125 error_at (location, "%<%T::%E%> has not been declared",
2126 parser->object_scope, name);
2128 error_at (location, "%qE has not been declared", name);
2130 else if (parser->scope && parser->scope != global_namespace)
2131 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2132 else if (parser->scope == global_namespace)
2133 error_at (location, "%<::%E%> %s", name, desired);
2135 error_at (location, "%qE %s", name, desired);
2138 /* If we are parsing tentatively, remember that an error has occurred
2139 during this tentative parse. Returns true if the error was
2140 simulated; false if a message should be issued by the caller. */
2143 cp_parser_simulate_error (cp_parser* parser)
2145 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2147 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2153 /* Check for repeated decl-specifiers. */
2156 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2157 location_t location)
2161 for (ds = ds_first; ds != ds_last; ++ds)
2163 unsigned count = decl_specs->specs[ds];
2166 /* The "long" specifier is a special case because of "long long". */
2170 error_at (location, "%<long long long%> is too long for GCC");
2172 pedwarn_cxx98 (location, OPT_Wlong_long,
2173 "ISO C++ 1998 does not support %<long long%>");
2177 static const char *const decl_spec_names[] = {
2193 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2198 /* This function is called when a type is defined. If type
2199 definitions are forbidden at this point, an error message is
2203 cp_parser_check_type_definition (cp_parser* parser)
2205 /* If types are forbidden here, issue a message. */
2206 if (parser->type_definition_forbidden_message)
2208 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2209 in the message need to be interpreted. */
2210 error (parser->type_definition_forbidden_message);
2216 /* This function is called when the DECLARATOR is processed. The TYPE
2217 was a type defined in the decl-specifiers. If it is invalid to
2218 define a type in the decl-specifiers for DECLARATOR, an error is
2219 issued. TYPE_LOCATION is the location of TYPE and is used
2220 for error reporting. */
2223 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2224 tree type, location_t type_location)
2226 /* [dcl.fct] forbids type definitions in return types.
2227 Unfortunately, it's not easy to know whether or not we are
2228 processing a return type until after the fact. */
2230 && (declarator->kind == cdk_pointer
2231 || declarator->kind == cdk_reference
2232 || declarator->kind == cdk_ptrmem))
2233 declarator = declarator->declarator;
2235 && declarator->kind == cdk_function)
2237 error_at (type_location,
2238 "new types may not be defined in a return type");
2239 inform (type_location,
2240 "(perhaps a semicolon is missing after the definition of %qT)",
2245 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2246 "<" in any valid C++ program. If the next token is indeed "<",
2247 issue a message warning the user about what appears to be an
2248 invalid attempt to form a template-id. LOCATION is the location
2249 of the type-specifier (TYPE) */
2252 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2253 tree type, location_t location)
2255 cp_token_position start = 0;
2257 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2260 error_at (location, "%qT is not a template", type);
2261 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2262 error_at (location, "%qE is not a template", type);
2264 error_at (location, "invalid template-id");
2265 /* Remember the location of the invalid "<". */
2266 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2267 start = cp_lexer_token_position (parser->lexer, true);
2268 /* Consume the "<". */
2269 cp_lexer_consume_token (parser->lexer);
2270 /* Parse the template arguments. */
2271 cp_parser_enclosed_template_argument_list (parser);
2272 /* Permanently remove the invalid template arguments so that
2273 this error message is not issued again. */
2275 cp_lexer_purge_tokens_after (parser->lexer, start);
2279 /* If parsing an integral constant-expression, issue an error message
2280 about the fact that THING appeared and return true. Otherwise,
2281 return false. In either case, set
2282 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2285 cp_parser_non_integral_constant_expression (cp_parser *parser,
2288 parser->non_integral_constant_expression_p = true;
2289 if (parser->integral_constant_expression_p)
2291 if (!parser->allow_non_integral_constant_expression_p)
2293 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2294 in the message need to be interpreted. */
2295 char *message = concat (thing,
2296 " cannot appear in a constant-expression",
2306 /* Emit a diagnostic for an invalid type name. SCOPE is the
2307 qualifying scope (or NULL, if none) for ID. This function commits
2308 to the current active tentative parse, if any. (Otherwise, the
2309 problematic construct might be encountered again later, resulting
2310 in duplicate error messages.) LOCATION is the location of ID. */
2313 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2314 tree scope, tree id,
2315 location_t location)
2317 tree decl, old_scope;
2318 /* Try to lookup the identifier. */
2319 old_scope = parser->scope;
2320 parser->scope = scope;
2321 decl = cp_parser_lookup_name_simple (parser, id, location);
2322 parser->scope = old_scope;
2323 /* If the lookup found a template-name, it means that the user forgot
2324 to specify an argument list. Emit a useful error message. */
2325 if (TREE_CODE (decl) == TEMPLATE_DECL)
2327 "invalid use of template-name %qE without an argument list",
2329 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2330 error_at (location, "invalid use of destructor %qD as a type", id);
2331 else if (TREE_CODE (decl) == TYPE_DECL)
2332 /* Something like 'unsigned A a;' */
2333 error_at (location, "invalid combination of multiple type-specifiers");
2334 else if (!parser->scope)
2336 /* Issue an error message. */
2337 error_at (location, "%qE does not name a type", id);
2338 /* If we're in a template class, it's possible that the user was
2339 referring to a type from a base class. For example:
2341 template <typename T> struct A { typedef T X; };
2342 template <typename T> struct B : public A<T> { X x; };
2344 The user should have said "typename A<T>::X". */
2345 if (processing_template_decl && current_class_type
2346 && TYPE_BINFO (current_class_type))
2350 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2354 tree base_type = BINFO_TYPE (b);
2355 if (CLASS_TYPE_P (base_type)
2356 && dependent_type_p (base_type))
2359 /* Go from a particular instantiation of the
2360 template (which will have an empty TYPE_FIELDs),
2361 to the main version. */
2362 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2363 for (field = TYPE_FIELDS (base_type);
2365 field = TREE_CHAIN (field))
2366 if (TREE_CODE (field) == TYPE_DECL
2367 && DECL_NAME (field) == id)
2370 "(perhaps %<typename %T::%E%> was intended)",
2371 BINFO_TYPE (b), id);
2380 /* Here we diagnose qualified-ids where the scope is actually correct,
2381 but the identifier does not resolve to a valid type name. */
2382 else if (parser->scope != error_mark_node)
2384 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2385 error_at (location, "%qE in namespace %qE does not name a type",
2387 else if (TYPE_P (parser->scope))
2388 error_at (location, "%qE in class %qT does not name a type",
2393 cp_parser_commit_to_tentative_parse (parser);
2396 /* Check for a common situation where a type-name should be present,
2397 but is not, and issue a sensible error message. Returns true if an
2398 invalid type-name was detected.
2400 The situation handled by this function are variable declarations of the
2401 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2402 Usually, `ID' should name a type, but if we got here it means that it
2403 does not. We try to emit the best possible error message depending on
2404 how exactly the id-expression looks like. */
2407 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2410 cp_token *token = cp_lexer_peek_token (parser->lexer);
2412 cp_parser_parse_tentatively (parser);
2413 id = cp_parser_id_expression (parser,
2414 /*template_keyword_p=*/false,
2415 /*check_dependency_p=*/true,
2416 /*template_p=*/NULL,
2417 /*declarator_p=*/true,
2418 /*optional_p=*/false);
2419 /* After the id-expression, there should be a plain identifier,
2420 otherwise this is not a simple variable declaration. Also, if
2421 the scope is dependent, we cannot do much. */
2422 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2423 || (parser->scope && TYPE_P (parser->scope)
2424 && dependent_type_p (parser->scope))
2425 || TREE_CODE (id) == TYPE_DECL)
2427 cp_parser_abort_tentative_parse (parser);
2430 if (!cp_parser_parse_definitely (parser))
2433 /* Emit a diagnostic for the invalid type. */
2434 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2435 id, token->location);
2436 /* Skip to the end of the declaration; there's no point in
2437 trying to process it. */
2438 cp_parser_skip_to_end_of_block_or_statement (parser);
2442 /* Consume tokens up to, and including, the next non-nested closing `)'.
2443 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2444 are doing error recovery. Returns -1 if OR_COMMA is true and we
2445 found an unnested comma. */
2448 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2453 unsigned paren_depth = 0;
2454 unsigned brace_depth = 0;
2456 if (recovering && !or_comma
2457 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2462 cp_token * token = cp_lexer_peek_token (parser->lexer);
2464 switch (token->type)
2467 case CPP_PRAGMA_EOL:
2468 /* If we've run out of tokens, then there is no closing `)'. */
2472 /* This matches the processing in skip_to_end_of_statement. */
2477 case CPP_OPEN_BRACE:
2480 case CPP_CLOSE_BRACE:
2486 if (recovering && or_comma && !brace_depth && !paren_depth)
2490 case CPP_OPEN_PAREN:
2495 case CPP_CLOSE_PAREN:
2496 if (!brace_depth && !paren_depth--)
2499 cp_lexer_consume_token (parser->lexer);
2508 /* Consume the token. */
2509 cp_lexer_consume_token (parser->lexer);
2513 /* Consume tokens until we reach the end of the current statement.
2514 Normally, that will be just before consuming a `;'. However, if a
2515 non-nested `}' comes first, then we stop before consuming that. */
2518 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2520 unsigned nesting_depth = 0;
2524 cp_token *token = cp_lexer_peek_token (parser->lexer);
2526 switch (token->type)
2529 case CPP_PRAGMA_EOL:
2530 /* If we've run out of tokens, stop. */
2534 /* If the next token is a `;', we have reached the end of the
2540 case CPP_CLOSE_BRACE:
2541 /* If this is a non-nested '}', stop before consuming it.
2542 That way, when confronted with something like:
2546 we stop before consuming the closing '}', even though we
2547 have not yet reached a `;'. */
2548 if (nesting_depth == 0)
2551 /* If it is the closing '}' for a block that we have
2552 scanned, stop -- but only after consuming the token.
2558 we will stop after the body of the erroneously declared
2559 function, but before consuming the following `typedef'
2561 if (--nesting_depth == 0)
2563 cp_lexer_consume_token (parser->lexer);
2567 case CPP_OPEN_BRACE:
2575 /* Consume the token. */
2576 cp_lexer_consume_token (parser->lexer);
2580 /* This function is called at the end of a statement or declaration.
2581 If the next token is a semicolon, it is consumed; otherwise, error
2582 recovery is attempted. */
2585 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2587 /* Look for the trailing `;'. */
2588 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2590 /* If there is additional (erroneous) input, skip to the end of
2592 cp_parser_skip_to_end_of_statement (parser);
2593 /* If the next token is now a `;', consume it. */
2594 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2595 cp_lexer_consume_token (parser->lexer);
2599 /* Skip tokens until we have consumed an entire block, or until we
2600 have consumed a non-nested `;'. */
2603 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2605 int nesting_depth = 0;
2607 while (nesting_depth >= 0)
2609 cp_token *token = cp_lexer_peek_token (parser->lexer);
2611 switch (token->type)
2614 case CPP_PRAGMA_EOL:
2615 /* If we've run out of tokens, stop. */
2619 /* Stop if this is an unnested ';'. */
2624 case CPP_CLOSE_BRACE:
2625 /* Stop if this is an unnested '}', or closes the outermost
2628 if (nesting_depth < 0)
2634 case CPP_OPEN_BRACE:
2643 /* Consume the token. */
2644 cp_lexer_consume_token (parser->lexer);
2648 /* Skip tokens until a non-nested closing curly brace is the next
2649 token, or there are no more tokens. Return true in the first case,
2653 cp_parser_skip_to_closing_brace (cp_parser *parser)
2655 unsigned nesting_depth = 0;
2659 cp_token *token = cp_lexer_peek_token (parser->lexer);
2661 switch (token->type)
2664 case CPP_PRAGMA_EOL:
2665 /* If we've run out of tokens, stop. */
2668 case CPP_CLOSE_BRACE:
2669 /* If the next token is a non-nested `}', then we have reached
2670 the end of the current block. */
2671 if (nesting_depth-- == 0)
2675 case CPP_OPEN_BRACE:
2676 /* If it the next token is a `{', then we are entering a new
2677 block. Consume the entire block. */
2685 /* Consume the token. */
2686 cp_lexer_consume_token (parser->lexer);
2690 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2691 parameter is the PRAGMA token, allowing us to purge the entire pragma
2695 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2699 parser->lexer->in_pragma = false;
2702 token = cp_lexer_consume_token (parser->lexer);
2703 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2705 /* Ensure that the pragma is not parsed again. */
2706 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2709 /* Require pragma end of line, resyncing with it as necessary. The
2710 arguments are as for cp_parser_skip_to_pragma_eol. */
2713 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2715 parser->lexer->in_pragma = false;
2716 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2717 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2720 /* This is a simple wrapper around make_typename_type. When the id is
2721 an unresolved identifier node, we can provide a superior diagnostic
2722 using cp_parser_diagnose_invalid_type_name. */
2725 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2726 tree id, location_t id_location)
2729 if (TREE_CODE (id) == IDENTIFIER_NODE)
2731 result = make_typename_type (scope, id, typename_type,
2732 /*complain=*/tf_none);
2733 if (result == error_mark_node)
2734 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2737 return make_typename_type (scope, id, typename_type, tf_error);
2740 /* This is a wrapper around the
2741 make_{pointer,ptrmem,reference}_declarator functions that decides
2742 which one to call based on the CODE and CLASS_TYPE arguments. The
2743 CODE argument should be one of the values returned by
2744 cp_parser_ptr_operator. */
2745 static cp_declarator *
2746 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2747 cp_cv_quals cv_qualifiers,
2748 cp_declarator *target)
2750 if (code == ERROR_MARK)
2751 return cp_error_declarator;
2753 if (code == INDIRECT_REF)
2754 if (class_type == NULL_TREE)
2755 return make_pointer_declarator (cv_qualifiers, target);
2757 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2758 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2759 return make_reference_declarator (cv_qualifiers, target, false);
2760 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2761 return make_reference_declarator (cv_qualifiers, target, true);
2765 /* Create a new C++ parser. */
2768 cp_parser_new (void)
2774 /* cp_lexer_new_main is called before calling ggc_alloc because
2775 cp_lexer_new_main might load a PCH file. */
2776 lexer = cp_lexer_new_main ();
2778 /* Initialize the binops_by_token so that we can get the tree
2779 directly from the token. */
2780 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2781 binops_by_token[binops[i].token_type] = binops[i];
2783 parser = GGC_CNEW (cp_parser);
2784 parser->lexer = lexer;
2785 parser->context = cp_parser_context_new (NULL);
2787 /* For now, we always accept GNU extensions. */
2788 parser->allow_gnu_extensions_p = 1;
2790 /* The `>' token is a greater-than operator, not the end of a
2792 parser->greater_than_is_operator_p = true;
2794 parser->default_arg_ok_p = true;
2796 /* We are not parsing a constant-expression. */
2797 parser->integral_constant_expression_p = false;
2798 parser->allow_non_integral_constant_expression_p = false;
2799 parser->non_integral_constant_expression_p = false;
2801 /* Local variable names are not forbidden. */
2802 parser->local_variables_forbidden_p = false;
2804 /* We are not processing an `extern "C"' declaration. */
2805 parser->in_unbraced_linkage_specification_p = false;
2807 /* We are not processing a declarator. */
2808 parser->in_declarator_p = false;
2810 /* We are not processing a template-argument-list. */
2811 parser->in_template_argument_list_p = false;
2813 /* We are not in an iteration statement. */
2814 parser->in_statement = 0;
2816 /* We are not in a switch statement. */
2817 parser->in_switch_statement_p = false;
2819 /* We are not parsing a type-id inside an expression. */
2820 parser->in_type_id_in_expr_p = false;
2822 /* Declarations aren't implicitly extern "C". */
2823 parser->implicit_extern_c = false;
2825 /* String literals should be translated to the execution character set. */
2826 parser->translate_strings_p = true;
2828 /* We are not parsing a function body. */
2829 parser->in_function_body = false;
2831 /* The unparsed function queue is empty. */
2832 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2834 /* There are no classes being defined. */
2835 parser->num_classes_being_defined = 0;
2837 /* No template parameters apply. */
2838 parser->num_template_parameter_lists = 0;
2843 /* Create a cp_lexer structure which will emit the tokens in CACHE
2844 and push it onto the parser's lexer stack. This is used for delayed
2845 parsing of in-class method bodies and default arguments, and should
2846 not be confused with tentative parsing. */
2848 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2850 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2851 lexer->next = parser->lexer;
2852 parser->lexer = lexer;
2854 /* Move the current source position to that of the first token in the
2856 cp_lexer_set_source_position_from_token (lexer->next_token);
2859 /* Pop the top lexer off the parser stack. This is never used for the
2860 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2862 cp_parser_pop_lexer (cp_parser *parser)
2864 cp_lexer *lexer = parser->lexer;
2865 parser->lexer = lexer->next;
2866 cp_lexer_destroy (lexer);
2868 /* Put the current source position back where it was before this
2869 lexer was pushed. */
2870 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2873 /* Lexical conventions [gram.lex] */
2875 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2879 cp_parser_identifier (cp_parser* parser)
2883 /* Look for the identifier. */
2884 token = cp_parser_require (parser, CPP_NAME, "identifier");
2885 /* Return the value. */
2886 return token ? token->u.value : error_mark_node;
2889 /* Parse a sequence of adjacent string constants. Returns a
2890 TREE_STRING representing the combined, nul-terminated string
2891 constant. If TRANSLATE is true, translate the string to the
2892 execution character set. If WIDE_OK is true, a wide string is
2895 C++98 [lex.string] says that if a narrow string literal token is
2896 adjacent to a wide string literal token, the behavior is undefined.
2897 However, C99 6.4.5p4 says that this results in a wide string literal.
2898 We follow C99 here, for consistency with the C front end.
2900 This code is largely lifted from lex_string() in c-lex.c.
2902 FUTURE: ObjC++ will need to handle @-strings here. */
2904 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2908 struct obstack str_ob;
2909 cpp_string str, istr, *strs;
2911 enum cpp_ttype type;
2913 tok = cp_lexer_peek_token (parser->lexer);
2914 if (!cp_parser_is_string_literal (tok))
2916 cp_parser_error (parser, "expected string-literal");
2917 return error_mark_node;
2922 /* Try to avoid the overhead of creating and destroying an obstack
2923 for the common case of just one string. */
2924 if (!cp_parser_is_string_literal
2925 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2927 cp_lexer_consume_token (parser->lexer);
2929 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2930 str.len = TREE_STRING_LENGTH (tok->u.value);
2937 gcc_obstack_init (&str_ob);
2942 cp_lexer_consume_token (parser->lexer);
2944 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2945 str.len = TREE_STRING_LENGTH (tok->u.value);
2947 if (type != tok->type)
2949 if (type == CPP_STRING)
2951 else if (tok->type != CPP_STRING)
2952 error_at (tok->location,
2953 "unsupported non-standard concatenation "
2954 "of string literals");
2957 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2959 tok = cp_lexer_peek_token (parser->lexer);
2961 while (cp_parser_is_string_literal (tok));
2963 strs = (cpp_string *) obstack_finish (&str_ob);
2966 if (type != CPP_STRING && !wide_ok)
2968 cp_parser_error (parser, "a wide string is invalid in this context");
2972 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2973 (parse_in, strs, count, &istr, type))
2975 value = build_string (istr.len, (const char *)istr.text);
2976 free (CONST_CAST (unsigned char *, istr.text));
2982 TREE_TYPE (value) = char_array_type_node;
2985 TREE_TYPE (value) = char16_array_type_node;
2988 TREE_TYPE (value) = char32_array_type_node;
2991 TREE_TYPE (value) = wchar_array_type_node;
2995 value = fix_string_type (value);
2998 /* cpp_interpret_string has issued an error. */
2999 value = error_mark_node;
3002 obstack_free (&str_ob, 0);
3008 /* Basic concepts [gram.basic] */
3010 /* Parse a translation-unit.
3013 declaration-seq [opt]
3015 Returns TRUE if all went well. */
3018 cp_parser_translation_unit (cp_parser* parser)
3020 /* The address of the first non-permanent object on the declarator
3022 static void *declarator_obstack_base;
3026 /* Create the declarator obstack, if necessary. */
3027 if (!cp_error_declarator)
3029 gcc_obstack_init (&declarator_obstack);
3030 /* Create the error declarator. */
3031 cp_error_declarator = make_declarator (cdk_error);
3032 /* Create the empty parameter list. */
3033 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3034 /* Remember where the base of the declarator obstack lies. */
3035 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3038 cp_parser_declaration_seq_opt (parser);
3040 /* If there are no tokens left then all went well. */
3041 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3043 /* Get rid of the token array; we don't need it any more. */
3044 cp_lexer_destroy (parser->lexer);
3045 parser->lexer = NULL;
3047 /* This file might have been a context that's implicitly extern
3048 "C". If so, pop the lang context. (Only relevant for PCH.) */
3049 if (parser->implicit_extern_c)
3051 pop_lang_context ();
3052 parser->implicit_extern_c = false;
3056 finish_translation_unit ();
3062 cp_parser_error (parser, "expected declaration");
3066 /* Make sure the declarator obstack was fully cleaned up. */
3067 gcc_assert (obstack_next_free (&declarator_obstack)
3068 == declarator_obstack_base);
3070 /* All went well. */
3074 /* Expressions [gram.expr] */
3076 /* Parse a primary-expression.
3087 ( compound-statement )
3088 __builtin_va_arg ( assignment-expression , type-id )
3089 __builtin_offsetof ( type-id , offsetof-expression )
3092 __has_nothrow_assign ( type-id )
3093 __has_nothrow_constructor ( type-id )
3094 __has_nothrow_copy ( type-id )
3095 __has_trivial_assign ( type-id )
3096 __has_trivial_constructor ( type-id )
3097 __has_trivial_copy ( type-id )
3098 __has_trivial_destructor ( type-id )
3099 __has_virtual_destructor ( type-id )
3100 __is_abstract ( type-id )
3101 __is_base_of ( type-id , type-id )
3102 __is_class ( type-id )
3103 __is_convertible_to ( type-id , type-id )
3104 __is_empty ( type-id )
3105 __is_enum ( type-id )
3106 __is_pod ( type-id )
3107 __is_polymorphic ( type-id )
3108 __is_union ( type-id )
3110 Objective-C++ Extension:
3118 ADDRESS_P is true iff this expression was immediately preceded by
3119 "&" and therefore might denote a pointer-to-member. CAST_P is true
3120 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3121 true iff this expression is a template argument.
3123 Returns a representation of the expression. Upon return, *IDK
3124 indicates what kind of id-expression (if any) was present. */
3127 cp_parser_primary_expression (cp_parser *parser,
3130 bool template_arg_p,
3133 cp_token *token = NULL;
3135 /* Assume the primary expression is not an id-expression. */
3136 *idk = CP_ID_KIND_NONE;
3138 /* Peek at the next token. */
3139 token = cp_lexer_peek_token (parser->lexer);
3140 switch (token->type)
3153 token = cp_lexer_consume_token (parser->lexer);
3154 if (TREE_CODE (token->u.value) == FIXED_CST)
3156 error_at (token->location,
3157 "fixed-point types not supported in C++");
3158 return error_mark_node;
3160 /* Floating-point literals are only allowed in an integral
3161 constant expression if they are cast to an integral or
3162 enumeration type. */
3163 if (TREE_CODE (token->u.value) == REAL_CST
3164 && parser->integral_constant_expression_p
3167 /* CAST_P will be set even in invalid code like "int(2.7 +
3168 ...)". Therefore, we have to check that the next token
3169 is sure to end the cast. */
3172 cp_token *next_token;
3174 next_token = cp_lexer_peek_token (parser->lexer);
3175 if (/* The comma at the end of an
3176 enumerator-definition. */
3177 next_token->type != CPP_COMMA
3178 /* The curly brace at the end of an enum-specifier. */
3179 && next_token->type != CPP_CLOSE_BRACE
3180 /* The end of a statement. */
3181 && next_token->type != CPP_SEMICOLON
3182 /* The end of the cast-expression. */
3183 && next_token->type != CPP_CLOSE_PAREN
3184 /* The end of an array bound. */
3185 && next_token->type != CPP_CLOSE_SQUARE
3186 /* The closing ">" in a template-argument-list. */
3187 && (next_token->type != CPP_GREATER
3188 || parser->greater_than_is_operator_p)
3189 /* C++0x only: A ">>" treated like two ">" tokens,
3190 in a template-argument-list. */
3191 && (next_token->type != CPP_RSHIFT
3192 || (cxx_dialect == cxx98)
3193 || parser->greater_than_is_operator_p))
3197 /* If we are within a cast, then the constraint that the
3198 cast is to an integral or enumeration type will be
3199 checked at that point. If we are not within a cast, then
3200 this code is invalid. */
3202 cp_parser_non_integral_constant_expression
3203 (parser, "floating-point literal");
3205 return token->u.value;
3211 /* ??? Should wide strings be allowed when parser->translate_strings_p
3212 is false (i.e. in attributes)? If not, we can kill the third
3213 argument to cp_parser_string_literal. */
3214 return cp_parser_string_literal (parser,
3215 parser->translate_strings_p,
3218 case CPP_OPEN_PAREN:
3221 bool saved_greater_than_is_operator_p;
3223 /* Consume the `('. */
3224 cp_lexer_consume_token (parser->lexer);
3225 /* Within a parenthesized expression, a `>' token is always
3226 the greater-than operator. */
3227 saved_greater_than_is_operator_p
3228 = parser->greater_than_is_operator_p;
3229 parser->greater_than_is_operator_p = true;
3230 /* If we see `( { ' then we are looking at the beginning of
3231 a GNU statement-expression. */
3232 if (cp_parser_allow_gnu_extensions_p (parser)
3233 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3235 /* Statement-expressions are not allowed by the standard. */
3236 pedwarn (token->location, OPT_pedantic,
3237 "ISO C++ forbids braced-groups within expressions");
3239 /* And they're not allowed outside of a function-body; you
3240 cannot, for example, write:
3242 int i = ({ int j = 3; j + 1; });
3244 at class or namespace scope. */
3245 if (!parser->in_function_body
3246 || parser->in_template_argument_list_p)
3248 error_at (token->location,
3249 "statement-expressions are not allowed outside "
3250 "functions nor in template-argument lists");
3251 cp_parser_skip_to_end_of_block_or_statement (parser);
3252 expr = error_mark_node;
3256 /* Start the statement-expression. */
3257 expr = begin_stmt_expr ();
3258 /* Parse the compound-statement. */
3259 cp_parser_compound_statement (parser, expr, false);
3261 expr = finish_stmt_expr (expr, false);
3266 /* Parse the parenthesized expression. */
3267 expr = cp_parser_expression (parser, cast_p, idk);
3268 /* Let the front end know that this expression was
3269 enclosed in parentheses. This matters in case, for
3270 example, the expression is of the form `A::B', since
3271 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3273 finish_parenthesized_expr (expr);
3275 /* The `>' token might be the end of a template-id or
3276 template-parameter-list now. */
3277 parser->greater_than_is_operator_p
3278 = saved_greater_than_is_operator_p;
3279 /* Consume the `)'. */
3280 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3281 cp_parser_skip_to_end_of_statement (parser);
3287 switch (token->keyword)
3289 /* These two are the boolean literals. */
3291 cp_lexer_consume_token (parser->lexer);
3292 return boolean_true_node;
3294 cp_lexer_consume_token (parser->lexer);
3295 return boolean_false_node;
3297 /* The `__null' literal. */
3299 cp_lexer_consume_token (parser->lexer);
3302 /* Recognize the `this' keyword. */
3304 cp_lexer_consume_token (parser->lexer);
3305 if (parser->local_variables_forbidden_p)
3307 error_at (token->location,
3308 "%<this%> may not be used in this context");
3309 return error_mark_node;
3311 /* Pointers cannot appear in constant-expressions. */
3312 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3313 return error_mark_node;
3314 return finish_this_expr ();
3316 /* The `operator' keyword can be the beginning of an
3321 case RID_FUNCTION_NAME:
3322 case RID_PRETTY_FUNCTION_NAME:
3323 case RID_C99_FUNCTION_NAME:
3327 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3328 __func__ are the names of variables -- but they are
3329 treated specially. Therefore, they are handled here,
3330 rather than relying on the generic id-expression logic
3331 below. Grammatically, these names are id-expressions.
3333 Consume the token. */
3334 token = cp_lexer_consume_token (parser->lexer);
3336 switch (token->keyword)
3338 case RID_FUNCTION_NAME:
3339 name = "%<__FUNCTION__%>";
3341 case RID_PRETTY_FUNCTION_NAME:
3342 name = "%<__PRETTY_FUNCTION__%>";
3344 case RID_C99_FUNCTION_NAME:
3345 name = "%<__func__%>";
3351 if (cp_parser_non_integral_constant_expression (parser, name))
3352 return error_mark_node;
3354 /* Look up the name. */
3355 return finish_fname (token->u.value);
3363 /* The `__builtin_va_arg' construct is used to handle
3364 `va_arg'. Consume the `__builtin_va_arg' token. */
3365 cp_lexer_consume_token (parser->lexer);
3366 /* Look for the opening `('. */
3367 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3368 /* Now, parse the assignment-expression. */
3369 expression = cp_parser_assignment_expression (parser,
3370 /*cast_p=*/false, NULL);
3371 /* Look for the `,'. */
3372 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3373 /* Parse the type-id. */
3374 type = cp_parser_type_id (parser);
3375 /* Look for the closing `)'. */
3376 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3377 /* Using `va_arg' in a constant-expression is not
3379 if (cp_parser_non_integral_constant_expression (parser,
3381 return error_mark_node;
3382 return build_x_va_arg (expression, type);
3386 return cp_parser_builtin_offsetof (parser);
3388 case RID_HAS_NOTHROW_ASSIGN:
3389 case RID_HAS_NOTHROW_CONSTRUCTOR:
3390 case RID_HAS_NOTHROW_COPY:
3391 case RID_HAS_TRIVIAL_ASSIGN:
3392 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3393 case RID_HAS_TRIVIAL_COPY:
3394 case RID_HAS_TRIVIAL_DESTRUCTOR:
3395 case RID_HAS_VIRTUAL_DESTRUCTOR:
3396 case RID_IS_ABSTRACT:
3397 case RID_IS_BASE_OF:
3399 case RID_IS_CONVERTIBLE_TO:
3403 case RID_IS_POLYMORPHIC:
3404 case RID_IS_STD_LAYOUT:
3405 case RID_IS_TRIVIAL:
3407 return cp_parser_trait_expr (parser, token->keyword);
3409 /* Objective-C++ expressions. */
3411 case RID_AT_PROTOCOL:
3412 case RID_AT_SELECTOR:
3413 return cp_parser_objc_expression (parser);
3416 cp_parser_error (parser, "expected primary-expression");
3417 return error_mark_node;
3420 /* An id-expression can start with either an identifier, a
3421 `::' as the beginning of a qualified-id, or the "operator"
3425 case CPP_TEMPLATE_ID:
3426 case CPP_NESTED_NAME_SPECIFIER:
3430 const char *error_msg;
3433 cp_token *id_expr_token;
3436 /* Parse the id-expression. */
3438 = cp_parser_id_expression (parser,
3439 /*template_keyword_p=*/false,
3440 /*check_dependency_p=*/true,
3442 /*declarator_p=*/false,
3443 /*optional_p=*/false);
3444 if (id_expression == error_mark_node)
3445 return error_mark_node;
3446 id_expr_token = token;
3447 token = cp_lexer_peek_token (parser->lexer);
3448 done = (token->type != CPP_OPEN_SQUARE
3449 && token->type != CPP_OPEN_PAREN
3450 && token->type != CPP_DOT
3451 && token->type != CPP_DEREF
3452 && token->type != CPP_PLUS_PLUS
3453 && token->type != CPP_MINUS_MINUS);
3454 /* If we have a template-id, then no further lookup is
3455 required. If the template-id was for a template-class, we
3456 will sometimes have a TYPE_DECL at this point. */
3457 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3458 || TREE_CODE (id_expression) == TYPE_DECL)
3459 decl = id_expression;
3460 /* Look up the name. */
3463 tree ambiguous_decls;
3465 decl = cp_parser_lookup_name (parser, id_expression,
3468 /*is_namespace=*/false,
3469 /*check_dependency=*/true,
3471 id_expr_token->location);
3472 /* If the lookup was ambiguous, an error will already have
3474 if (ambiguous_decls)
3475 return error_mark_node;
3477 /* In Objective-C++, an instance variable (ivar) may be preferred
3478 to whatever cp_parser_lookup_name() found. */
3479 decl = objc_lookup_ivar (decl, id_expression);
3481 /* If name lookup gives us a SCOPE_REF, then the
3482 qualifying scope was dependent. */
3483 if (TREE_CODE (decl) == SCOPE_REF)
3485 /* At this point, we do not know if DECL is a valid
3486 integral constant expression. We assume that it is
3487 in fact such an expression, so that code like:
3489 template <int N> struct A {
3493 is accepted. At template-instantiation time, we
3494 will check that B<N>::i is actually a constant. */
3497 /* Check to see if DECL is a local variable in a context
3498 where that is forbidden. */
3499 if (parser->local_variables_forbidden_p
3500 && local_variable_p (decl))
3502 /* It might be that we only found DECL because we are
3503 trying to be generous with pre-ISO scoping rules.
3504 For example, consider:
3508 for (int i = 0; i < 10; ++i) {}
3509 extern void f(int j = i);
3512 Here, name look up will originally find the out
3513 of scope `i'. We need to issue a warning message,
3514 but then use the global `i'. */
3515 decl = check_for_out_of_scope_variable (decl);
3516 if (local_variable_p (decl))
3518 error_at (id_expr_token->location,
3519 "local variable %qD may not appear in this context",
3521 return error_mark_node;
3526 decl = (finish_id_expression
3527 (id_expression, decl, parser->scope,
3529 parser->integral_constant_expression_p,
3530 parser->allow_non_integral_constant_expression_p,
3531 &parser->non_integral_constant_expression_p,
3532 template_p, done, address_p,
3535 id_expr_token->location));
3537 cp_parser_error (parser, error_msg);
3541 /* Anything else is an error. */
3543 /* ...unless we have an Objective-C++ message or string literal,
3545 if (c_dialect_objc ()
3546 && (token->type == CPP_OPEN_SQUARE
3547 || token->type == CPP_OBJC_STRING))
3548 return cp_parser_objc_expression (parser);
3550 cp_parser_error (parser, "expected primary-expression");
3551 return error_mark_node;
3555 /* Parse an id-expression.
3562 :: [opt] nested-name-specifier template [opt] unqualified-id
3564 :: operator-function-id
3567 Return a representation of the unqualified portion of the
3568 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3569 a `::' or nested-name-specifier.
3571 Often, if the id-expression was a qualified-id, the caller will
3572 want to make a SCOPE_REF to represent the qualified-id. This
3573 function does not do this in order to avoid wastefully creating
3574 SCOPE_REFs when they are not required.
3576 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3579 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3580 uninstantiated templates.
3582 If *TEMPLATE_P is non-NULL, it is set to true iff the
3583 `template' keyword is used to explicitly indicate that the entity
3584 named is a template.
3586 If DECLARATOR_P is true, the id-expression is appearing as part of
3587 a declarator, rather than as part of an expression. */
3590 cp_parser_id_expression (cp_parser *parser,
3591 bool template_keyword_p,
3592 bool check_dependency_p,
3597 bool global_scope_p;
3598 bool nested_name_specifier_p;
3600 /* Assume the `template' keyword was not used. */
3602 *template_p = template_keyword_p;
3604 /* Look for the optional `::' operator. */
3606 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3608 /* Look for the optional nested-name-specifier. */
3609 nested_name_specifier_p
3610 = (cp_parser_nested_name_specifier_opt (parser,
3611 /*typename_keyword_p=*/false,
3616 /* If there is a nested-name-specifier, then we are looking at
3617 the first qualified-id production. */
3618 if (nested_name_specifier_p)
3621 tree saved_object_scope;
3622 tree saved_qualifying_scope;
3623 tree unqualified_id;
3626 /* See if the next token is the `template' keyword. */
3628 template_p = &is_template;
3629 *template_p = cp_parser_optional_template_keyword (parser);
3630 /* Name lookup we do during the processing of the
3631 unqualified-id might obliterate SCOPE. */
3632 saved_scope = parser->scope;
3633 saved_object_scope = parser->object_scope;
3634 saved_qualifying_scope = parser->qualifying_scope;
3635 /* Process the final unqualified-id. */
3636 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3639 /*optional_p=*/false);
3640 /* Restore the SAVED_SCOPE for our caller. */
3641 parser->scope = saved_scope;
3642 parser->object_scope = saved_object_scope;
3643 parser->qualifying_scope = saved_qualifying_scope;
3645 return unqualified_id;
3647 /* Otherwise, if we are in global scope, then we are looking at one
3648 of the other qualified-id productions. */
3649 else if (global_scope_p)
3654 /* Peek at the next token. */
3655 token = cp_lexer_peek_token (parser->lexer);
3657 /* If it's an identifier, and the next token is not a "<", then
3658 we can avoid the template-id case. This is an optimization
3659 for this common case. */
3660 if (token->type == CPP_NAME
3661 && !cp_parser_nth_token_starts_template_argument_list_p
3663 return cp_parser_identifier (parser);
3665 cp_parser_parse_tentatively (parser);
3666 /* Try a template-id. */
3667 id = cp_parser_template_id (parser,
3668 /*template_keyword_p=*/false,
3669 /*check_dependency_p=*/true,
3671 /* If that worked, we're done. */
3672 if (cp_parser_parse_definitely (parser))
3675 /* Peek at the next token. (Changes in the token buffer may
3676 have invalidated the pointer obtained above.) */
3677 token = cp_lexer_peek_token (parser->lexer);
3679 switch (token->type)
3682 return cp_parser_identifier (parser);
3685 if (token->keyword == RID_OPERATOR)
3686 return cp_parser_operator_function_id (parser);
3690 cp_parser_error (parser, "expected id-expression");
3691 return error_mark_node;
3695 return cp_parser_unqualified_id (parser, template_keyword_p,
3696 /*check_dependency_p=*/true,
3701 /* Parse an unqualified-id.
3705 operator-function-id
3706 conversion-function-id
3710 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3711 keyword, in a construct like `A::template ...'.
3713 Returns a representation of unqualified-id. For the `identifier'
3714 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3715 production a BIT_NOT_EXPR is returned; the operand of the
3716 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3717 other productions, see the documentation accompanying the
3718 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3719 names are looked up in uninstantiated templates. If DECLARATOR_P
3720 is true, the unqualified-id is appearing as part of a declarator,
3721 rather than as part of an expression. */
3724 cp_parser_unqualified_id (cp_parser* parser,
3725 bool template_keyword_p,
3726 bool check_dependency_p,
3732 /* Peek at the next token. */
3733 token = cp_lexer_peek_token (parser->lexer);
3735 switch (token->type)
3741 /* We don't know yet whether or not this will be a
3743 cp_parser_parse_tentatively (parser);
3744 /* Try a template-id. */
3745 id = cp_parser_template_id (parser, template_keyword_p,
3748 /* If it worked, we're done. */
3749 if (cp_parser_parse_definitely (parser))
3751 /* Otherwise, it's an ordinary identifier. */
3752 return cp_parser_identifier (parser);
3755 case CPP_TEMPLATE_ID:
3756 return cp_parser_template_id (parser, template_keyword_p,
3763 tree qualifying_scope;
3768 /* Consume the `~' token. */
3769 cp_lexer_consume_token (parser->lexer);
3770 /* Parse the class-name. The standard, as written, seems to
3773 template <typename T> struct S { ~S (); };
3774 template <typename T> S<T>::~S() {}
3776 is invalid, since `~' must be followed by a class-name, but
3777 `S<T>' is dependent, and so not known to be a class.
3778 That's not right; we need to look in uninstantiated
3779 templates. A further complication arises from:
3781 template <typename T> void f(T t) {
3785 Here, it is not possible to look up `T' in the scope of `T'
3786 itself. We must look in both the current scope, and the
3787 scope of the containing complete expression.
3789 Yet another issue is:
3798 The standard does not seem to say that the `S' in `~S'
3799 should refer to the type `S' and not the data member
3802 /* DR 244 says that we look up the name after the "~" in the
3803 same scope as we looked up the qualifying name. That idea
3804 isn't fully worked out; it's more complicated than that. */
3805 scope = parser->scope;
3806 object_scope = parser->object_scope;
3807 qualifying_scope = parser->qualifying_scope;
3809 /* Check for invalid scopes. */
3810 if (scope == error_mark_node)
3812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3813 cp_lexer_consume_token (parser->lexer);
3814 return error_mark_node;
3816 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3818 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3819 error_at (token->location,
3820 "scope %qT before %<~%> is not a class-name",
3822 cp_parser_simulate_error (parser);
3823 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3824 cp_lexer_consume_token (parser->lexer);
3825 return error_mark_node;
3827 gcc_assert (!scope || TYPE_P (scope));
3829 /* If the name is of the form "X::~X" it's OK. */
3830 token = cp_lexer_peek_token (parser->lexer);
3832 && token->type == CPP_NAME
3833 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3835 && constructor_name_p (token->u.value, scope))
3837 cp_lexer_consume_token (parser->lexer);
3838 return build_nt (BIT_NOT_EXPR, scope);
3841 /* If there was an explicit qualification (S::~T), first look
3842 in the scope given by the qualification (i.e., S). */
3844 type_decl = NULL_TREE;
3847 cp_parser_parse_tentatively (parser);
3848 type_decl = cp_parser_class_name (parser,
3849 /*typename_keyword_p=*/false,
3850 /*template_keyword_p=*/false,
3852 /*check_dependency=*/false,
3853 /*class_head_p=*/false,
3855 if (cp_parser_parse_definitely (parser))
3858 /* In "N::S::~S", look in "N" as well. */
3859 if (!done && scope && qualifying_scope)
3861 cp_parser_parse_tentatively (parser);
3862 parser->scope = qualifying_scope;
3863 parser->object_scope = NULL_TREE;
3864 parser->qualifying_scope = NULL_TREE;
3866 = cp_parser_class_name (parser,
3867 /*typename_keyword_p=*/false,
3868 /*template_keyword_p=*/false,
3870 /*check_dependency=*/false,
3871 /*class_head_p=*/false,
3873 if (cp_parser_parse_definitely (parser))
3876 /* In "p->S::~T", look in the scope given by "*p" as well. */
3877 else if (!done && object_scope)
3879 cp_parser_parse_tentatively (parser);
3880 parser->scope = object_scope;
3881 parser->object_scope = NULL_TREE;
3882 parser->qualifying_scope = NULL_TREE;
3884 = cp_parser_class_name (parser,
3885 /*typename_keyword_p=*/false,
3886 /*template_keyword_p=*/false,
3888 /*check_dependency=*/false,
3889 /*class_head_p=*/false,
3891 if (cp_parser_parse_definitely (parser))
3894 /* Look in the surrounding context. */
3897 parser->scope = NULL_TREE;
3898 parser->object_scope = NULL_TREE;
3899 parser->qualifying_scope = NULL_TREE;
3900 if (processing_template_decl)
3901 cp_parser_parse_tentatively (parser);
3903 = cp_parser_class_name (parser,
3904 /*typename_keyword_p=*/false,
3905 /*template_keyword_p=*/false,
3907 /*check_dependency=*/false,
3908 /*class_head_p=*/false,
3910 if (processing_template_decl
3911 && ! cp_parser_parse_definitely (parser))
3913 /* We couldn't find a type with this name, so just accept
3914 it and check for a match at instantiation time. */
3915 type_decl = cp_parser_identifier (parser);
3916 if (type_decl != error_mark_node)
3917 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3921 /* If an error occurred, assume that the name of the
3922 destructor is the same as the name of the qualifying
3923 class. That allows us to keep parsing after running
3924 into ill-formed destructor names. */
3925 if (type_decl == error_mark_node && scope)
3926 return build_nt (BIT_NOT_EXPR, scope);
3927 else if (type_decl == error_mark_node)
3928 return error_mark_node;
3930 /* Check that destructor name and scope match. */
3931 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3933 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3934 error_at (token->location,
3935 "declaration of %<~%T%> as member of %qT",
3937 cp_parser_simulate_error (parser);
3938 return error_mark_node;
3943 A typedef-name that names a class shall not be used as the
3944 identifier in the declarator for a destructor declaration. */
3946 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3947 && !DECL_SELF_REFERENCE_P (type_decl)
3948 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3949 error_at (token->location,
3950 "typedef-name %qD used as destructor declarator",
3953 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3957 if (token->keyword == RID_OPERATOR)
3961 /* This could be a template-id, so we try that first. */
3962 cp_parser_parse_tentatively (parser);
3963 /* Try a template-id. */
3964 id = cp_parser_template_id (parser, template_keyword_p,
3965 /*check_dependency_p=*/true,
3967 /* If that worked, we're done. */
3968 if (cp_parser_parse_definitely (parser))
3970 /* We still don't know whether we're looking at an
3971 operator-function-id or a conversion-function-id. */
3972 cp_parser_parse_tentatively (parser);
3973 /* Try an operator-function-id. */
3974 id = cp_parser_operator_function_id (parser);
3975 /* If that didn't work, try a conversion-function-id. */
3976 if (!cp_parser_parse_definitely (parser))
3977 id = cp_parser_conversion_function_id (parser);
3986 cp_parser_error (parser, "expected unqualified-id");
3987 return error_mark_node;
3991 /* Parse an (optional) nested-name-specifier.
3993 nested-name-specifier: [C++98]
3994 class-or-namespace-name :: nested-name-specifier [opt]
3995 class-or-namespace-name :: template nested-name-specifier [opt]
3997 nested-name-specifier: [C++0x]
4000 nested-name-specifier identifier ::
4001 nested-name-specifier template [opt] simple-template-id ::
4003 PARSER->SCOPE should be set appropriately before this function is
4004 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4005 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4008 Sets PARSER->SCOPE to the class (TYPE) or namespace
4009 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4010 it unchanged if there is no nested-name-specifier. Returns the new
4011 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4013 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4014 part of a declaration and/or decl-specifier. */
4017 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4018 bool typename_keyword_p,
4019 bool check_dependency_p,
4021 bool is_declaration)
4023 bool success = false;
4024 cp_token_position start = 0;
4027 /* Remember where the nested-name-specifier starts. */
4028 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4030 start = cp_lexer_token_position (parser->lexer, false);
4031 push_deferring_access_checks (dk_deferred);
4038 tree saved_qualifying_scope;
4039 bool template_keyword_p;
4041 /* Spot cases that cannot be the beginning of a
4042 nested-name-specifier. */
4043 token = cp_lexer_peek_token (parser->lexer);
4045 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4046 the already parsed nested-name-specifier. */
4047 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4049 /* Grab the nested-name-specifier and continue the loop. */
4050 cp_parser_pre_parsed_nested_name_specifier (parser);
4051 /* If we originally encountered this nested-name-specifier
4052 with IS_DECLARATION set to false, we will not have
4053 resolved TYPENAME_TYPEs, so we must do so here. */
4055 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4057 new_scope = resolve_typename_type (parser->scope,
4058 /*only_current_p=*/false);
4059 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4060 parser->scope = new_scope;
4066 /* Spot cases that cannot be the beginning of a
4067 nested-name-specifier. On the second and subsequent times
4068 through the loop, we look for the `template' keyword. */
4069 if (success && token->keyword == RID_TEMPLATE)
4071 /* A template-id can start a nested-name-specifier. */
4072 else if (token->type == CPP_TEMPLATE_ID)
4076 /* If the next token is not an identifier, then it is
4077 definitely not a type-name or namespace-name. */
4078 if (token->type != CPP_NAME)
4080 /* If the following token is neither a `<' (to begin a
4081 template-id), nor a `::', then we are not looking at a
4082 nested-name-specifier. */
4083 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4084 if (token->type != CPP_SCOPE
4085 && !cp_parser_nth_token_starts_template_argument_list_p
4090 /* The nested-name-specifier is optional, so we parse
4092 cp_parser_parse_tentatively (parser);
4094 /* Look for the optional `template' keyword, if this isn't the
4095 first time through the loop. */
4097 template_keyword_p = cp_parser_optional_template_keyword (parser);
4099 template_keyword_p = false;
4101 /* Save the old scope since the name lookup we are about to do
4102 might destroy it. */
4103 old_scope = parser->scope;
4104 saved_qualifying_scope = parser->qualifying_scope;
4105 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4106 look up names in "X<T>::I" in order to determine that "Y" is
4107 a template. So, if we have a typename at this point, we make
4108 an effort to look through it. */
4110 && !typename_keyword_p
4112 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4113 parser->scope = resolve_typename_type (parser->scope,
4114 /*only_current_p=*/false);
4115 /* Parse the qualifying entity. */
4117 = cp_parser_qualifying_entity (parser,
4123 /* Look for the `::' token. */
4124 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4126 /* If we found what we wanted, we keep going; otherwise, we're
4128 if (!cp_parser_parse_definitely (parser))
4130 bool error_p = false;
4132 /* Restore the OLD_SCOPE since it was valid before the
4133 failed attempt at finding the last
4134 class-or-namespace-name. */
4135 parser->scope = old_scope;
4136 parser->qualifying_scope = saved_qualifying_scope;
4137 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4139 /* If the next token is an identifier, and the one after
4140 that is a `::', then any valid interpretation would have
4141 found a class-or-namespace-name. */
4142 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4143 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4145 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4148 token = cp_lexer_consume_token (parser->lexer);
4151 if (!token->ambiguous_p)
4154 tree ambiguous_decls;
4156 decl = cp_parser_lookup_name (parser, token->u.value,
4158 /*is_template=*/false,
4159 /*is_namespace=*/false,
4160 /*check_dependency=*/true,
4163 if (TREE_CODE (decl) == TEMPLATE_DECL)
4164 error_at (token->location,
4165 "%qD used without template parameters",
4167 else if (ambiguous_decls)
4169 error_at (token->location,
4170 "reference to %qD is ambiguous",
4172 print_candidates (ambiguous_decls);
4173 decl = error_mark_node;
4177 const char* msg = "is not a class or namespace";
4178 if (cxx_dialect != cxx98)
4179 msg = "is not a class, namespace, or enumeration";
4180 cp_parser_name_lookup_error
4181 (parser, token->u.value, decl, msg,
4185 parser->scope = error_mark_node;
4187 /* Treat this as a successful nested-name-specifier
4192 If the name found is not a class-name (clause
4193 _class_) or namespace-name (_namespace.def_), the
4194 program is ill-formed. */
4197 cp_lexer_consume_token (parser->lexer);
4201 /* We've found one valid nested-name-specifier. */
4203 /* Name lookup always gives us a DECL. */
4204 if (TREE_CODE (new_scope) == TYPE_DECL)
4205 new_scope = TREE_TYPE (new_scope);
4206 /* Uses of "template" must be followed by actual templates. */
4207 if (template_keyword_p
4208 && !(CLASS_TYPE_P (new_scope)
4209 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4210 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4211 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4212 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4213 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4214 == TEMPLATE_ID_EXPR)))
4215 permerror (input_location, TYPE_P (new_scope)
4216 ? "%qT is not a template"
4217 : "%qD is not a template",
4219 /* If it is a class scope, try to complete it; we are about to
4220 be looking up names inside the class. */
4221 if (TYPE_P (new_scope)
4222 /* Since checking types for dependency can be expensive,
4223 avoid doing it if the type is already complete. */
4224 && !COMPLETE_TYPE_P (new_scope)
4225 /* Do not try to complete dependent types. */
4226 && !dependent_type_p (new_scope))
4228 new_scope = complete_type (new_scope);
4229 /* If it is a typedef to current class, use the current
4230 class instead, as the typedef won't have any names inside
4232 if (!COMPLETE_TYPE_P (new_scope)
4233 && currently_open_class (new_scope))
4234 new_scope = TYPE_MAIN_VARIANT (new_scope);
4236 /* Make sure we look in the right scope the next time through
4238 parser->scope = new_scope;
4241 /* If parsing tentatively, replace the sequence of tokens that makes
4242 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4243 token. That way, should we re-parse the token stream, we will
4244 not have to repeat the effort required to do the parse, nor will
4245 we issue duplicate error messages. */
4246 if (success && start)
4250 token = cp_lexer_token_at (parser->lexer, start);
4251 /* Reset the contents of the START token. */
4252 token->type = CPP_NESTED_NAME_SPECIFIER;
4253 /* Retrieve any deferred checks. Do not pop this access checks yet
4254 so the memory will not be reclaimed during token replacing below. */
4255 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4256 token->u.tree_check_value->value = parser->scope;
4257 token->u.tree_check_value->checks = get_deferred_access_checks ();
4258 token->u.tree_check_value->qualifying_scope =
4259 parser->qualifying_scope;
4260 token->keyword = RID_MAX;
4262 /* Purge all subsequent tokens. */
4263 cp_lexer_purge_tokens_after (parser->lexer, start);
4267 pop_to_parent_deferring_access_checks ();
4269 return success ? parser->scope : NULL_TREE;
4272 /* Parse a nested-name-specifier. See
4273 cp_parser_nested_name_specifier_opt for details. This function
4274 behaves identically, except that it will an issue an error if no
4275 nested-name-specifier is present. */
4278 cp_parser_nested_name_specifier (cp_parser *parser,
4279 bool typename_keyword_p,
4280 bool check_dependency_p,
4282 bool is_declaration)
4286 /* Look for the nested-name-specifier. */
4287 scope = cp_parser_nested_name_specifier_opt (parser,
4292 /* If it was not present, issue an error message. */
4295 cp_parser_error (parser, "expected nested-name-specifier");
4296 parser->scope = NULL_TREE;
4302 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4303 this is either a class-name or a namespace-name (which corresponds
4304 to the class-or-namespace-name production in the grammar). For
4305 C++0x, it can also be a type-name that refers to an enumeration
4308 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4309 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4310 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4311 TYPE_P is TRUE iff the next name should be taken as a class-name,
4312 even the same name is declared to be another entity in the same
4315 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4316 specified by the class-or-namespace-name. If neither is found the
4317 ERROR_MARK_NODE is returned. */
4320 cp_parser_qualifying_entity (cp_parser *parser,
4321 bool typename_keyword_p,
4322 bool template_keyword_p,
4323 bool check_dependency_p,
4325 bool is_declaration)
4328 tree saved_qualifying_scope;
4329 tree saved_object_scope;
4332 bool successful_parse_p;
4334 /* Before we try to parse the class-name, we must save away the
4335 current PARSER->SCOPE since cp_parser_class_name will destroy
4337 saved_scope = parser->scope;
4338 saved_qualifying_scope = parser->qualifying_scope;
4339 saved_object_scope = parser->object_scope;
4340 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4341 there is no need to look for a namespace-name. */
4342 only_class_p = template_keyword_p
4343 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4345 cp_parser_parse_tentatively (parser);
4346 scope = cp_parser_class_name (parser,
4349 type_p ? class_type : none_type,
4351 /*class_head_p=*/false,
4353 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4354 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4356 && cxx_dialect != cxx98
4357 && !successful_parse_p)
4359 /* Restore the saved scope. */
4360 parser->scope = saved_scope;
4361 parser->qualifying_scope = saved_qualifying_scope;
4362 parser->object_scope = saved_object_scope;
4364 /* Parse tentatively. */
4365 cp_parser_parse_tentatively (parser);
4367 /* Parse a typedef-name or enum-name. */
4368 scope = cp_parser_nonclass_name (parser);
4369 successful_parse_p = cp_parser_parse_definitely (parser);
4371 /* If that didn't work, try for a namespace-name. */
4372 if (!only_class_p && !successful_parse_p)
4374 /* Restore the saved scope. */
4375 parser->scope = saved_scope;
4376 parser->qualifying_scope = saved_qualifying_scope;
4377 parser->object_scope = saved_object_scope;
4378 /* If we are not looking at an identifier followed by the scope
4379 resolution operator, then this is not part of a
4380 nested-name-specifier. (Note that this function is only used
4381 to parse the components of a nested-name-specifier.) */
4382 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4383 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4384 return error_mark_node;
4385 scope = cp_parser_namespace_name (parser);
4391 /* Parse a postfix-expression.
4395 postfix-expression [ expression ]
4396 postfix-expression ( expression-list [opt] )
4397 simple-type-specifier ( expression-list [opt] )
4398 typename :: [opt] nested-name-specifier identifier
4399 ( expression-list [opt] )
4400 typename :: [opt] nested-name-specifier template [opt] template-id
4401 ( expression-list [opt] )
4402 postfix-expression . template [opt] id-expression
4403 postfix-expression -> template [opt] id-expression
4404 postfix-expression . pseudo-destructor-name
4405 postfix-expression -> pseudo-destructor-name
4406 postfix-expression ++
4407 postfix-expression --
4408 dynamic_cast < type-id > ( expression )
4409 static_cast < type-id > ( expression )
4410 reinterpret_cast < type-id > ( expression )
4411 const_cast < type-id > ( expression )
4412 typeid ( expression )
4418 ( type-id ) { initializer-list , [opt] }
4420 This extension is a GNU version of the C99 compound-literal
4421 construct. (The C99 grammar uses `type-name' instead of `type-id',
4422 but they are essentially the same concept.)
4424 If ADDRESS_P is true, the postfix expression is the operand of the
4425 `&' operator. CAST_P is true if this expression is the target of a
4428 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4429 class member access expressions [expr.ref].
4431 Returns a representation of the expression. */
4434 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4435 bool member_access_only_p,
4436 cp_id_kind * pidk_return)
4440 cp_id_kind idk = CP_ID_KIND_NONE;
4441 tree postfix_expression = NULL_TREE;
4442 bool is_member_access = false;
4444 /* Peek at the next token. */
4445 token = cp_lexer_peek_token (parser->lexer);
4446 /* Some of the productions are determined by keywords. */
4447 keyword = token->keyword;
4457 const char *saved_message;
4459 /* All of these can be handled in the same way from the point
4460 of view of parsing. Begin by consuming the token
4461 identifying the cast. */
4462 cp_lexer_consume_token (parser->lexer);
4464 /* New types cannot be defined in the cast. */
4465 saved_message = parser->type_definition_forbidden_message;
4466 parser->type_definition_forbidden_message
4467 = "types may not be defined in casts";
4469 /* Look for the opening `<'. */
4470 cp_parser_require (parser, CPP_LESS, "%<<%>");
4471 /* Parse the type to which we are casting. */
4472 type = cp_parser_type_id (parser);
4473 /* Look for the closing `>'. */
4474 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4475 /* Restore the old message. */
4476 parser->type_definition_forbidden_message = saved_message;
4478 /* And the expression which is being cast. */
4479 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4480 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4481 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4483 /* Only type conversions to integral or enumeration types
4484 can be used in constant-expressions. */
4485 if (!cast_valid_in_integral_constant_expression_p (type)
4486 && (cp_parser_non_integral_constant_expression
4488 "a cast to a type other than an integral or "
4489 "enumeration type")))
4490 return error_mark_node;
4496 = build_dynamic_cast (type, expression, tf_warning_or_error);
4500 = build_static_cast (type, expression, tf_warning_or_error);
4504 = build_reinterpret_cast (type, expression,
4505 tf_warning_or_error);
4509 = build_const_cast (type, expression, tf_warning_or_error);
4520 const char *saved_message;
4521 bool saved_in_type_id_in_expr_p;
4523 /* Consume the `typeid' token. */
4524 cp_lexer_consume_token (parser->lexer);
4525 /* Look for the `(' token. */
4526 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4527 /* Types cannot be defined in a `typeid' expression. */
4528 saved_message = parser->type_definition_forbidden_message;
4529 parser->type_definition_forbidden_message
4530 = "types may not be defined in a %<typeid%> expression";
4531 /* We can't be sure yet whether we're looking at a type-id or an
4533 cp_parser_parse_tentatively (parser);
4534 /* Try a type-id first. */
4535 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4536 parser->in_type_id_in_expr_p = true;
4537 type = cp_parser_type_id (parser);
4538 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4539 /* Look for the `)' token. Otherwise, we can't be sure that
4540 we're not looking at an expression: consider `typeid (int
4541 (3))', for example. */
4542 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4543 /* If all went well, simply lookup the type-id. */
4544 if (cp_parser_parse_definitely (parser))
4545 postfix_expression = get_typeid (type);
4546 /* Otherwise, fall back to the expression variant. */
4551 /* Look for an expression. */
4552 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4553 /* Compute its typeid. */
4554 postfix_expression = build_typeid (expression);
4555 /* Look for the `)' token. */
4556 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4558 /* Restore the saved message. */
4559 parser->type_definition_forbidden_message = saved_message;
4560 /* `typeid' may not appear in an integral constant expression. */
4561 if (cp_parser_non_integral_constant_expression(parser,
4562 "%<typeid%> operator"))
4563 return error_mark_node;
4570 /* The syntax permitted here is the same permitted for an
4571 elaborated-type-specifier. */
4572 type = cp_parser_elaborated_type_specifier (parser,
4573 /*is_friend=*/false,
4574 /*is_declaration=*/false);
4575 postfix_expression = cp_parser_functional_cast (parser, type);
4583 /* If the next thing is a simple-type-specifier, we may be
4584 looking at a functional cast. We could also be looking at
4585 an id-expression. So, we try the functional cast, and if
4586 that doesn't work we fall back to the primary-expression. */
4587 cp_parser_parse_tentatively (parser);
4588 /* Look for the simple-type-specifier. */
4589 type = cp_parser_simple_type_specifier (parser,
4590 /*decl_specs=*/NULL,
4591 CP_PARSER_FLAGS_NONE);
4592 /* Parse the cast itself. */
4593 if (!cp_parser_error_occurred (parser))
4595 = cp_parser_functional_cast (parser, type);
4596 /* If that worked, we're done. */
4597 if (cp_parser_parse_definitely (parser))
4600 /* If the functional-cast didn't work out, try a
4601 compound-literal. */
4602 if (cp_parser_allow_gnu_extensions_p (parser)
4603 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4605 VEC(constructor_elt,gc) *initializer_list = NULL;
4606 bool saved_in_type_id_in_expr_p;
4608 cp_parser_parse_tentatively (parser);
4609 /* Consume the `('. */
4610 cp_lexer_consume_token (parser->lexer);
4611 /* Parse the type. */
4612 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4613 parser->in_type_id_in_expr_p = true;
4614 type = cp_parser_type_id (parser);
4615 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4616 /* Look for the `)'. */
4617 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4618 /* Look for the `{'. */
4619 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4620 /* If things aren't going well, there's no need to
4622 if (!cp_parser_error_occurred (parser))
4624 bool non_constant_p;
4625 /* Parse the initializer-list. */
4627 = cp_parser_initializer_list (parser, &non_constant_p);
4628 /* Allow a trailing `,'. */
4629 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4630 cp_lexer_consume_token (parser->lexer);
4631 /* Look for the final `}'. */
4632 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4634 /* If that worked, we're definitely looking at a
4635 compound-literal expression. */
4636 if (cp_parser_parse_definitely (parser))
4638 /* Warn the user that a compound literal is not
4639 allowed in standard C++. */
4640 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4641 /* For simplicity, we disallow compound literals in
4642 constant-expressions. We could
4643 allow compound literals of integer type, whose
4644 initializer was a constant, in constant
4645 expressions. Permitting that usage, as a further
4646 extension, would not change the meaning of any
4647 currently accepted programs. (Of course, as
4648 compound literals are not part of ISO C++, the
4649 standard has nothing to say.) */
4650 if (cp_parser_non_integral_constant_expression
4651 (parser, "non-constant compound literals"))
4653 postfix_expression = error_mark_node;
4656 /* Form the representation of the compound-literal. */
4658 = (finish_compound_literal
4659 (type, build_constructor (init_list_type_node,
4660 initializer_list)));
4665 /* It must be a primary-expression. */
4667 = cp_parser_primary_expression (parser, address_p, cast_p,
4668 /*template_arg_p=*/false,
4674 /* Keep looping until the postfix-expression is complete. */
4677 if (idk == CP_ID_KIND_UNQUALIFIED
4678 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4679 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4680 /* It is not a Koenig lookup function call. */
4682 = unqualified_name_lookup_error (postfix_expression);
4684 /* Peek at the next token. */
4685 token = cp_lexer_peek_token (parser->lexer);
4687 switch (token->type)
4689 case CPP_OPEN_SQUARE:
4691 = cp_parser_postfix_open_square_expression (parser,
4694 idk = CP_ID_KIND_NONE;
4695 is_member_access = false;
4698 case CPP_OPEN_PAREN:
4699 /* postfix-expression ( expression-list [opt] ) */
4702 bool is_builtin_constant_p;
4703 bool saved_integral_constant_expression_p = false;
4704 bool saved_non_integral_constant_expression_p = false;
4707 is_member_access = false;
4709 is_builtin_constant_p
4710 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4711 if (is_builtin_constant_p)
4713 /* The whole point of __builtin_constant_p is to allow
4714 non-constant expressions to appear as arguments. */
4715 saved_integral_constant_expression_p
4716 = parser->integral_constant_expression_p;
4717 saved_non_integral_constant_expression_p
4718 = parser->non_integral_constant_expression_p;
4719 parser->integral_constant_expression_p = false;
4721 args = (cp_parser_parenthesized_expression_list
4722 (parser, /*is_attribute_list=*/false,
4723 /*cast_p=*/false, /*allow_expansion_p=*/true,
4724 /*non_constant_p=*/NULL));
4725 if (is_builtin_constant_p)
4727 parser->integral_constant_expression_p
4728 = saved_integral_constant_expression_p;
4729 parser->non_integral_constant_expression_p
4730 = saved_non_integral_constant_expression_p;
4735 postfix_expression = error_mark_node;
4739 /* Function calls are not permitted in
4740 constant-expressions. */
4741 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4742 && cp_parser_non_integral_constant_expression (parser,
4745 postfix_expression = error_mark_node;
4746 release_tree_vector (args);
4751 if (idk == CP_ID_KIND_UNQUALIFIED
4752 || idk == CP_ID_KIND_TEMPLATE_ID)
4754 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4756 if (!VEC_empty (tree, args))
4759 if (!any_type_dependent_arguments_p (args))
4761 = perform_koenig_lookup (postfix_expression, args);
4765 = unqualified_fn_lookup_error (postfix_expression);
4767 /* We do not perform argument-dependent lookup if
4768 normal lookup finds a non-function, in accordance
4769 with the expected resolution of DR 218. */
4770 else if (!VEC_empty (tree, args)
4771 && is_overloaded_fn (postfix_expression))
4773 tree fn = get_first_fn (postfix_expression);
4775 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4776 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4778 /* Only do argument dependent lookup if regular
4779 lookup does not find a set of member functions.
4780 [basic.lookup.koenig]/2a */
4781 if (!DECL_FUNCTION_MEMBER_P (fn))
4784 if (!any_type_dependent_arguments_p (args))
4786 = perform_koenig_lookup (postfix_expression, args);
4791 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4793 tree instance = TREE_OPERAND (postfix_expression, 0);
4794 tree fn = TREE_OPERAND (postfix_expression, 1);
4796 if (processing_template_decl
4797 && (type_dependent_expression_p (instance)
4798 || (!BASELINK_P (fn)
4799 && TREE_CODE (fn) != FIELD_DECL)
4800 || type_dependent_expression_p (fn)
4801 || any_type_dependent_arguments_p (args)))
4804 = build_nt_call_vec (postfix_expression, args);
4805 release_tree_vector (args);
4809 if (BASELINK_P (fn))
4812 = (build_new_method_call
4813 (instance, fn, &args, NULL_TREE,
4814 (idk == CP_ID_KIND_QUALIFIED
4815 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4817 tf_warning_or_error));
4821 = finish_call_expr (postfix_expression, &args,
4822 /*disallow_virtual=*/false,
4824 tf_warning_or_error);
4826 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4827 || TREE_CODE (postfix_expression) == MEMBER_REF
4828 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4829 postfix_expression = (build_offset_ref_call_from_tree
4830 (postfix_expression, &args));
4831 else if (idk == CP_ID_KIND_QUALIFIED)
4832 /* A call to a static class member, or a namespace-scope
4835 = finish_call_expr (postfix_expression, &args,
4836 /*disallow_virtual=*/true,
4838 tf_warning_or_error);
4840 /* All other function calls. */
4842 = finish_call_expr (postfix_expression, &args,
4843 /*disallow_virtual=*/false,
4845 tf_warning_or_error);
4847 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4848 idk = CP_ID_KIND_NONE;
4850 release_tree_vector (args);
4856 /* postfix-expression . template [opt] id-expression
4857 postfix-expression . pseudo-destructor-name
4858 postfix-expression -> template [opt] id-expression
4859 postfix-expression -> pseudo-destructor-name */
4861 /* Consume the `.' or `->' operator. */
4862 cp_lexer_consume_token (parser->lexer);
4865 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4870 is_member_access = true;
4874 /* postfix-expression ++ */
4875 /* Consume the `++' token. */
4876 cp_lexer_consume_token (parser->lexer);
4877 /* Generate a representation for the complete expression. */
4879 = finish_increment_expr (postfix_expression,
4880 POSTINCREMENT_EXPR);
4881 /* Increments may not appear in constant-expressions. */
4882 if (cp_parser_non_integral_constant_expression (parser,
4884 postfix_expression = error_mark_node;
4885 idk = CP_ID_KIND_NONE;
4886 is_member_access = false;
4889 case CPP_MINUS_MINUS:
4890 /* postfix-expression -- */
4891 /* Consume the `--' token. */
4892 cp_lexer_consume_token (parser->lexer);
4893 /* Generate a representation for the complete expression. */
4895 = finish_increment_expr (postfix_expression,
4896 POSTDECREMENT_EXPR);
4897 /* Decrements may not appear in constant-expressions. */
4898 if (cp_parser_non_integral_constant_expression (parser,
4900 postfix_expression = error_mark_node;
4901 idk = CP_ID_KIND_NONE;
4902 is_member_access = false;
4906 if (pidk_return != NULL)
4907 * pidk_return = idk;
4908 if (member_access_only_p)
4909 return is_member_access? postfix_expression : error_mark_node;
4911 return postfix_expression;
4915 /* We should never get here. */
4917 return error_mark_node;
4920 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4921 by cp_parser_builtin_offsetof. We're looking for
4923 postfix-expression [ expression ]
4925 FOR_OFFSETOF is set if we're being called in that context, which
4926 changes how we deal with integer constant expressions. */
4929 cp_parser_postfix_open_square_expression (cp_parser *parser,
4930 tree postfix_expression,
4935 /* Consume the `[' token. */
4936 cp_lexer_consume_token (parser->lexer);
4938 /* Parse the index expression. */
4939 /* ??? For offsetof, there is a question of what to allow here. If
4940 offsetof is not being used in an integral constant expression context,
4941 then we *could* get the right answer by computing the value at runtime.
4942 If we are in an integral constant expression context, then we might
4943 could accept any constant expression; hard to say without analysis.
4944 Rather than open the barn door too wide right away, allow only integer
4945 constant expressions here. */
4947 index = cp_parser_constant_expression (parser, false, NULL);
4949 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4951 /* Look for the closing `]'. */
4952 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4954 /* Build the ARRAY_REF. */
4955 postfix_expression = grok_array_decl (postfix_expression, index);
4957 /* When not doing offsetof, array references are not permitted in
4958 constant-expressions. */
4960 && (cp_parser_non_integral_constant_expression
4961 (parser, "an array reference")))
4962 postfix_expression = error_mark_node;
4964 return postfix_expression;
4967 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4968 by cp_parser_builtin_offsetof. We're looking for
4970 postfix-expression . template [opt] id-expression
4971 postfix-expression . pseudo-destructor-name
4972 postfix-expression -> template [opt] id-expression
4973 postfix-expression -> pseudo-destructor-name
4975 FOR_OFFSETOF is set if we're being called in that context. That sorta
4976 limits what of the above we'll actually accept, but nevermind.
4977 TOKEN_TYPE is the "." or "->" token, which will already have been
4978 removed from the stream. */
4981 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4982 enum cpp_ttype token_type,
4983 tree postfix_expression,
4984 bool for_offsetof, cp_id_kind *idk,
4985 location_t location)
4989 bool pseudo_destructor_p;
4990 tree scope = NULL_TREE;
4992 /* If this is a `->' operator, dereference the pointer. */
4993 if (token_type == CPP_DEREF)
4994 postfix_expression = build_x_arrow (postfix_expression);
4995 /* Check to see whether or not the expression is type-dependent. */
4996 dependent_p = type_dependent_expression_p (postfix_expression);
4997 /* The identifier following the `->' or `.' is not qualified. */
4998 parser->scope = NULL_TREE;
4999 parser->qualifying_scope = NULL_TREE;
5000 parser->object_scope = NULL_TREE;
5001 *idk = CP_ID_KIND_NONE;
5003 /* Enter the scope corresponding to the type of the object
5004 given by the POSTFIX_EXPRESSION. */
5005 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5007 scope = TREE_TYPE (postfix_expression);
5008 /* According to the standard, no expression should ever have
5009 reference type. Unfortunately, we do not currently match
5010 the standard in this respect in that our internal representation
5011 of an expression may have reference type even when the standard
5012 says it does not. Therefore, we have to manually obtain the
5013 underlying type here. */
5014 scope = non_reference (scope);
5015 /* The type of the POSTFIX_EXPRESSION must be complete. */
5016 if (scope == unknown_type_node)
5018 error_at (location, "%qE does not have class type",
5019 postfix_expression);
5023 scope = complete_type_or_else (scope, NULL_TREE);
5024 /* Let the name lookup machinery know that we are processing a
5025 class member access expression. */
5026 parser->context->object_type = scope;
5027 /* If something went wrong, we want to be able to discern that case,
5028 as opposed to the case where there was no SCOPE due to the type
5029 of expression being dependent. */
5031 scope = error_mark_node;
5032 /* If the SCOPE was erroneous, make the various semantic analysis
5033 functions exit quickly -- and without issuing additional error
5035 if (scope == error_mark_node)
5036 postfix_expression = error_mark_node;
5039 /* Assume this expression is not a pseudo-destructor access. */
5040 pseudo_destructor_p = false;
5042 /* If the SCOPE is a scalar type, then, if this is a valid program,
5043 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5044 is type dependent, it can be pseudo-destructor-name or something else.
5045 Try to parse it as pseudo-destructor-name first. */
5046 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5051 cp_parser_parse_tentatively (parser);
5052 /* Parse the pseudo-destructor-name. */
5054 cp_parser_pseudo_destructor_name (parser, &s, &type);
5056 && (cp_parser_error_occurred (parser)
5057 || TREE_CODE (type) != TYPE_DECL
5058 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5059 cp_parser_abort_tentative_parse (parser);
5060 else if (cp_parser_parse_definitely (parser))
5062 pseudo_destructor_p = true;
5064 = finish_pseudo_destructor_expr (postfix_expression,
5065 s, TREE_TYPE (type));
5069 if (!pseudo_destructor_p)
5071 /* If the SCOPE is not a scalar type, we are looking at an
5072 ordinary class member access expression, rather than a
5073 pseudo-destructor-name. */
5075 cp_token *token = cp_lexer_peek_token (parser->lexer);
5076 /* Parse the id-expression. */
5077 name = (cp_parser_id_expression
5079 cp_parser_optional_template_keyword (parser),
5080 /*check_dependency_p=*/true,
5082 /*declarator_p=*/false,
5083 /*optional_p=*/false));
5084 /* In general, build a SCOPE_REF if the member name is qualified.
5085 However, if the name was not dependent and has already been
5086 resolved; there is no need to build the SCOPE_REF. For example;
5088 struct X { void f(); };
5089 template <typename T> void f(T* t) { t->X::f(); }
5091 Even though "t" is dependent, "X::f" is not and has been resolved
5092 to a BASELINK; there is no need to include scope information. */
5094 /* But we do need to remember that there was an explicit scope for
5095 virtual function calls. */
5097 *idk = CP_ID_KIND_QUALIFIED;
5099 /* If the name is a template-id that names a type, we will get a
5100 TYPE_DECL here. That is invalid code. */
5101 if (TREE_CODE (name) == TYPE_DECL)
5103 error_at (token->location, "invalid use of %qD", name);
5104 postfix_expression = error_mark_node;
5108 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5110 name = build_qualified_name (/*type=*/NULL_TREE,
5114 parser->scope = NULL_TREE;
5115 parser->qualifying_scope = NULL_TREE;
5116 parser->object_scope = NULL_TREE;
5118 if (scope && name && BASELINK_P (name))
5119 adjust_result_of_qualified_name_lookup
5120 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5122 = finish_class_member_access_expr (postfix_expression, name,
5124 tf_warning_or_error);
5128 /* We no longer need to look up names in the scope of the object on
5129 the left-hand side of the `.' or `->' operator. */
5130 parser->context->object_type = NULL_TREE;
5132 /* Outside of offsetof, these operators may not appear in
5133 constant-expressions. */
5135 && (cp_parser_non_integral_constant_expression
5136 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5137 postfix_expression = error_mark_node;
5139 return postfix_expression;
5142 /* Parse a parenthesized expression-list.
5145 assignment-expression
5146 expression-list, assignment-expression
5151 identifier, expression-list
5153 CAST_P is true if this expression is the target of a cast.
5155 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5158 Returns a vector of trees. Each element is a representation of an
5159 assignment-expression. NULL is returned if the ( and or ) are
5160 missing. An empty, but allocated, vector is returned on no
5161 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5162 if this is really an attribute list being parsed. If
5163 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5164 not all of the expressions in the list were constant. */
5166 static VEC(tree,gc) *
5167 cp_parser_parenthesized_expression_list (cp_parser* parser,
5168 bool is_attribute_list,
5170 bool allow_expansion_p,
5171 bool *non_constant_p)
5173 VEC(tree,gc) *expression_list;
5174 bool fold_expr_p = is_attribute_list;
5175 tree identifier = NULL_TREE;
5176 bool saved_greater_than_is_operator_p;
5178 /* Assume all the expressions will be constant. */
5180 *non_constant_p = false;
5182 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5185 expression_list = make_tree_vector ();
5187 /* Within a parenthesized expression, a `>' token is always
5188 the greater-than operator. */
5189 saved_greater_than_is_operator_p
5190 = parser->greater_than_is_operator_p;
5191 parser->greater_than_is_operator_p = true;
5193 /* Consume expressions until there are no more. */
5194 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5199 /* At the beginning of attribute lists, check to see if the
5200 next token is an identifier. */
5201 if (is_attribute_list
5202 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5206 /* Consume the identifier. */
5207 token = cp_lexer_consume_token (parser->lexer);
5208 /* Save the identifier. */
5209 identifier = token->u.value;
5213 bool expr_non_constant_p;
5215 /* Parse the next assignment-expression. */
5216 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5218 /* A braced-init-list. */
5219 maybe_warn_cpp0x ("extended initializer lists");
5220 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5221 if (non_constant_p && expr_non_constant_p)
5222 *non_constant_p = true;
5224 else if (non_constant_p)
5226 expr = (cp_parser_constant_expression
5227 (parser, /*allow_non_constant_p=*/true,
5228 &expr_non_constant_p));
5229 if (expr_non_constant_p)
5230 *non_constant_p = true;
5233 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5236 expr = fold_non_dependent_expr (expr);
5238 /* If we have an ellipsis, then this is an expression
5240 if (allow_expansion_p
5241 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5243 /* Consume the `...'. */
5244 cp_lexer_consume_token (parser->lexer);
5246 /* Build the argument pack. */
5247 expr = make_pack_expansion (expr);
5250 /* Add it to the list. We add error_mark_node
5251 expressions to the list, so that we can still tell if
5252 the correct form for a parenthesized expression-list
5253 is found. That gives better errors. */
5254 VEC_safe_push (tree, gc, expression_list, expr);
5256 if (expr == error_mark_node)
5260 /* After the first item, attribute lists look the same as
5261 expression lists. */
5262 is_attribute_list = false;
5265 /* If the next token isn't a `,', then we are done. */
5266 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5269 /* Otherwise, consume the `,' and keep going. */
5270 cp_lexer_consume_token (parser->lexer);
5273 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5278 /* We try and resync to an unnested comma, as that will give the
5279 user better diagnostics. */
5280 ending = cp_parser_skip_to_closing_parenthesis (parser,
5281 /*recovering=*/true,
5283 /*consume_paren=*/true);
5288 parser->greater_than_is_operator_p
5289 = saved_greater_than_is_operator_p;
5294 parser->greater_than_is_operator_p
5295 = saved_greater_than_is_operator_p;
5298 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5300 return expression_list;
5303 /* Parse a pseudo-destructor-name.
5305 pseudo-destructor-name:
5306 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5307 :: [opt] nested-name-specifier template template-id :: ~ type-name
5308 :: [opt] nested-name-specifier [opt] ~ type-name
5310 If either of the first two productions is used, sets *SCOPE to the
5311 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5312 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5313 or ERROR_MARK_NODE if the parse fails. */
5316 cp_parser_pseudo_destructor_name (cp_parser* parser,
5320 bool nested_name_specifier_p;
5322 /* Assume that things will not work out. */
5323 *type = error_mark_node;
5325 /* Look for the optional `::' operator. */
5326 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5327 /* Look for the optional nested-name-specifier. */
5328 nested_name_specifier_p
5329 = (cp_parser_nested_name_specifier_opt (parser,
5330 /*typename_keyword_p=*/false,
5331 /*check_dependency_p=*/true,
5333 /*is_declaration=*/false)
5335 /* Now, if we saw a nested-name-specifier, we might be doing the
5336 second production. */
5337 if (nested_name_specifier_p
5338 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5340 /* Consume the `template' keyword. */
5341 cp_lexer_consume_token (parser->lexer);
5342 /* Parse the template-id. */
5343 cp_parser_template_id (parser,
5344 /*template_keyword_p=*/true,
5345 /*check_dependency_p=*/false,
5346 /*is_declaration=*/true);
5347 /* Look for the `::' token. */
5348 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5350 /* If the next token is not a `~', then there might be some
5351 additional qualification. */
5352 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5354 /* At this point, we're looking for "type-name :: ~". The type-name
5355 must not be a class-name, since this is a pseudo-destructor. So,
5356 it must be either an enum-name, or a typedef-name -- both of which
5357 are just identifiers. So, we peek ahead to check that the "::"
5358 and "~" tokens are present; if they are not, then we can avoid
5359 calling type_name. */
5360 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5361 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5362 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5364 cp_parser_error (parser, "non-scalar type");
5368 /* Look for the type-name. */
5369 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5370 if (*scope == error_mark_node)
5373 /* Look for the `::' token. */
5374 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5379 /* Look for the `~'. */
5380 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5381 /* Look for the type-name again. We are not responsible for
5382 checking that it matches the first type-name. */
5383 *type = cp_parser_nonclass_name (parser);
5386 /* Parse a unary-expression.
5392 unary-operator cast-expression
5393 sizeof unary-expression
5401 __extension__ cast-expression
5402 __alignof__ unary-expression
5403 __alignof__ ( type-id )
5404 __real__ cast-expression
5405 __imag__ cast-expression
5408 ADDRESS_P is true iff the unary-expression is appearing as the
5409 operand of the `&' operator. CAST_P is true if this expression is
5410 the target of a cast.
5412 Returns a representation of the expression. */
5415 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5419 enum tree_code unary_operator;
5421 /* Peek at the next token. */
5422 token = cp_lexer_peek_token (parser->lexer);
5423 /* Some keywords give away the kind of expression. */
5424 if (token->type == CPP_KEYWORD)
5426 enum rid keyword = token->keyword;
5436 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5437 /* Consume the token. */
5438 cp_lexer_consume_token (parser->lexer);
5439 /* Parse the operand. */
5440 operand = cp_parser_sizeof_operand (parser, keyword);
5442 if (TYPE_P (operand))
5443 return cxx_sizeof_or_alignof_type (operand, op, true);
5445 return cxx_sizeof_or_alignof_expr (operand, op, true);
5449 return cp_parser_new_expression (parser);
5452 return cp_parser_delete_expression (parser);
5456 /* The saved value of the PEDANTIC flag. */
5460 /* Save away the PEDANTIC flag. */
5461 cp_parser_extension_opt (parser, &saved_pedantic);
5462 /* Parse the cast-expression. */
5463 expr = cp_parser_simple_cast_expression (parser);
5464 /* Restore the PEDANTIC flag. */
5465 pedantic = saved_pedantic;
5475 /* Consume the `__real__' or `__imag__' token. */
5476 cp_lexer_consume_token (parser->lexer);
5477 /* Parse the cast-expression. */
5478 expression = cp_parser_simple_cast_expression (parser);
5479 /* Create the complete representation. */
5480 return build_x_unary_op ((keyword == RID_REALPART
5481 ? REALPART_EXPR : IMAGPART_EXPR),
5483 tf_warning_or_error);
5492 /* Look for the `:: new' and `:: delete', which also signal the
5493 beginning of a new-expression, or delete-expression,
5494 respectively. If the next token is `::', then it might be one of
5496 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5500 /* See if the token after the `::' is one of the keywords in
5501 which we're interested. */
5502 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5503 /* If it's `new', we have a new-expression. */
5504 if (keyword == RID_NEW)
5505 return cp_parser_new_expression (parser);
5506 /* Similarly, for `delete'. */
5507 else if (keyword == RID_DELETE)
5508 return cp_parser_delete_expression (parser);
5511 /* Look for a unary operator. */
5512 unary_operator = cp_parser_unary_operator (token);
5513 /* The `++' and `--' operators can be handled similarly, even though
5514 they are not technically unary-operators in the grammar. */
5515 if (unary_operator == ERROR_MARK)
5517 if (token->type == CPP_PLUS_PLUS)
5518 unary_operator = PREINCREMENT_EXPR;
5519 else if (token->type == CPP_MINUS_MINUS)
5520 unary_operator = PREDECREMENT_EXPR;
5521 /* Handle the GNU address-of-label extension. */
5522 else if (cp_parser_allow_gnu_extensions_p (parser)
5523 && token->type == CPP_AND_AND)
5527 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5529 /* Consume the '&&' token. */
5530 cp_lexer_consume_token (parser->lexer);
5531 /* Look for the identifier. */
5532 identifier = cp_parser_identifier (parser);
5533 /* Create an expression representing the address. */
5534 expression = finish_label_address_expr (identifier, loc);
5535 if (cp_parser_non_integral_constant_expression (parser,
5536 "the address of a label"))
5537 expression = error_mark_node;
5541 if (unary_operator != ERROR_MARK)
5543 tree cast_expression;
5544 tree expression = error_mark_node;
5545 const char *non_constant_p = NULL;
5547 /* Consume the operator token. */
5548 token = cp_lexer_consume_token (parser->lexer);
5549 /* Parse the cast-expression. */
5551 = cp_parser_cast_expression (parser,
5552 unary_operator == ADDR_EXPR,
5553 /*cast_p=*/false, pidk);
5554 /* Now, build an appropriate representation. */
5555 switch (unary_operator)
5558 non_constant_p = "%<*%>";
5559 expression = build_x_indirect_ref (cast_expression, "unary *",
5560 tf_warning_or_error);
5564 non_constant_p = "%<&%>";
5567 expression = build_x_unary_op (unary_operator, cast_expression,
5568 tf_warning_or_error);
5571 case PREINCREMENT_EXPR:
5572 case PREDECREMENT_EXPR:
5573 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5574 ? "%<++%>" : "%<--%>");
5576 case UNARY_PLUS_EXPR:
5578 case TRUTH_NOT_EXPR:
5579 expression = finish_unary_op_expr (unary_operator, cast_expression);
5587 && cp_parser_non_integral_constant_expression (parser,
5589 expression = error_mark_node;
5594 return cp_parser_postfix_expression (parser, address_p, cast_p,
5595 /*member_access_only_p=*/false,
5599 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5600 unary-operator, the corresponding tree code is returned. */
5602 static enum tree_code
5603 cp_parser_unary_operator (cp_token* token)
5605 switch (token->type)
5608 return INDIRECT_REF;
5614 return UNARY_PLUS_EXPR;
5620 return TRUTH_NOT_EXPR;
5623 return BIT_NOT_EXPR;
5630 /* Parse a new-expression.
5633 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5634 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5636 Returns a representation of the expression. */
5639 cp_parser_new_expression (cp_parser* parser)
5641 bool global_scope_p;
5642 VEC(tree,gc) *placement;
5644 VEC(tree,gc) *initializer;
5648 /* Look for the optional `::' operator. */
5650 = (cp_parser_global_scope_opt (parser,
5651 /*current_scope_valid_p=*/false)
5653 /* Look for the `new' operator. */
5654 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5655 /* There's no easy way to tell a new-placement from the
5656 `( type-id )' construct. */
5657 cp_parser_parse_tentatively (parser);
5658 /* Look for a new-placement. */
5659 placement = cp_parser_new_placement (parser);
5660 /* If that didn't work out, there's no new-placement. */
5661 if (!cp_parser_parse_definitely (parser))
5663 if (placement != NULL)
5664 release_tree_vector (placement);
5668 /* If the next token is a `(', then we have a parenthesized
5670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5673 /* Consume the `('. */
5674 cp_lexer_consume_token (parser->lexer);
5675 /* Parse the type-id. */
5676 type = cp_parser_type_id (parser);
5677 /* Look for the closing `)'. */
5678 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5679 token = cp_lexer_peek_token (parser->lexer);
5680 /* There should not be a direct-new-declarator in this production,
5681 but GCC used to allowed this, so we check and emit a sensible error
5682 message for this case. */
5683 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5685 error_at (token->location,
5686 "array bound forbidden after parenthesized type-id");
5687 inform (token->location,
5688 "try removing the parentheses around the type-id");
5689 cp_parser_direct_new_declarator (parser);
5693 /* Otherwise, there must be a new-type-id. */
5695 type = cp_parser_new_type_id (parser, &nelts);
5697 /* If the next token is a `(' or '{', then we have a new-initializer. */
5698 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5699 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5700 initializer = cp_parser_new_initializer (parser);
5704 /* A new-expression may not appear in an integral constant
5706 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5707 ret = error_mark_node;
5710 /* Create a representation of the new-expression. */
5711 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5712 tf_warning_or_error);
5715 if (placement != NULL)
5716 release_tree_vector (placement);
5717 if (initializer != NULL)
5718 release_tree_vector (initializer);
5723 /* Parse a new-placement.
5728 Returns the same representation as for an expression-list. */
5730 static VEC(tree,gc) *
5731 cp_parser_new_placement (cp_parser* parser)
5733 VEC(tree,gc) *expression_list;
5735 /* Parse the expression-list. */
5736 expression_list = (cp_parser_parenthesized_expression_list
5737 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5738 /*non_constant_p=*/NULL));
5740 return expression_list;
5743 /* Parse a new-type-id.
5746 type-specifier-seq new-declarator [opt]
5748 Returns the TYPE allocated. If the new-type-id indicates an array
5749 type, *NELTS is set to the number of elements in the last array
5750 bound; the TYPE will not include the last array bound. */
5753 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5755 cp_decl_specifier_seq type_specifier_seq;
5756 cp_declarator *new_declarator;
5757 cp_declarator *declarator;
5758 cp_declarator *outer_declarator;
5759 const char *saved_message;
5762 /* The type-specifier sequence must not contain type definitions.
5763 (It cannot contain declarations of new types either, but if they
5764 are not definitions we will catch that because they are not
5766 saved_message = parser->type_definition_forbidden_message;
5767 parser->type_definition_forbidden_message
5768 = "types may not be defined in a new-type-id";
5769 /* Parse the type-specifier-seq. */
5770 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5771 &type_specifier_seq);
5772 /* Restore the old message. */
5773 parser->type_definition_forbidden_message = saved_message;
5774 /* Parse the new-declarator. */
5775 new_declarator = cp_parser_new_declarator_opt (parser);
5777 /* Determine the number of elements in the last array dimension, if
5780 /* Skip down to the last array dimension. */
5781 declarator = new_declarator;
5782 outer_declarator = NULL;
5783 while (declarator && (declarator->kind == cdk_pointer
5784 || declarator->kind == cdk_ptrmem))
5786 outer_declarator = declarator;
5787 declarator = declarator->declarator;
5790 && declarator->kind == cdk_array
5791 && declarator->declarator
5792 && declarator->declarator->kind == cdk_array)
5794 outer_declarator = declarator;
5795 declarator = declarator->declarator;
5798 if (declarator && declarator->kind == cdk_array)
5800 *nelts = declarator->u.array.bounds;
5801 if (*nelts == error_mark_node)
5802 *nelts = integer_one_node;
5804 if (outer_declarator)
5805 outer_declarator->declarator = declarator->declarator;
5807 new_declarator = NULL;
5810 type = groktypename (&type_specifier_seq, new_declarator, false);
5814 /* Parse an (optional) new-declarator.
5817 ptr-operator new-declarator [opt]
5818 direct-new-declarator
5820 Returns the declarator. */
5822 static cp_declarator *
5823 cp_parser_new_declarator_opt (cp_parser* parser)
5825 enum tree_code code;
5827 cp_cv_quals cv_quals;
5829 /* We don't know if there's a ptr-operator next, or not. */
5830 cp_parser_parse_tentatively (parser);
5831 /* Look for a ptr-operator. */
5832 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5833 /* If that worked, look for more new-declarators. */
5834 if (cp_parser_parse_definitely (parser))
5836 cp_declarator *declarator;
5838 /* Parse another optional declarator. */
5839 declarator = cp_parser_new_declarator_opt (parser);
5841 return cp_parser_make_indirect_declarator
5842 (code, type, cv_quals, declarator);
5845 /* If the next token is a `[', there is a direct-new-declarator. */
5846 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5847 return cp_parser_direct_new_declarator (parser);
5852 /* Parse a direct-new-declarator.
5854 direct-new-declarator:
5856 direct-new-declarator [constant-expression]
5860 static cp_declarator *
5861 cp_parser_direct_new_declarator (cp_parser* parser)
5863 cp_declarator *declarator = NULL;
5869 /* Look for the opening `['. */
5870 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5871 /* The first expression is not required to be constant. */
5874 cp_token *token = cp_lexer_peek_token (parser->lexer);
5875 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5876 /* The standard requires that the expression have integral
5877 type. DR 74 adds enumeration types. We believe that the
5878 real intent is that these expressions be handled like the
5879 expression in a `switch' condition, which also allows
5880 classes with a single conversion to integral or
5881 enumeration type. */
5882 if (!processing_template_decl)
5885 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5890 error_at (token->location,
5891 "expression in new-declarator must have integral "
5892 "or enumeration type");
5893 expression = error_mark_node;
5897 /* But all the other expressions must be. */
5900 = cp_parser_constant_expression (parser,
5901 /*allow_non_constant=*/false,
5903 /* Look for the closing `]'. */
5904 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5906 /* Add this bound to the declarator. */
5907 declarator = make_array_declarator (declarator, expression);
5909 /* If the next token is not a `[', then there are no more
5911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5918 /* Parse a new-initializer.
5921 ( expression-list [opt] )
5924 Returns a representation of the expression-list. */
5926 static VEC(tree,gc) *
5927 cp_parser_new_initializer (cp_parser* parser)
5929 VEC(tree,gc) *expression_list;
5931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5934 bool expr_non_constant_p;
5935 maybe_warn_cpp0x ("extended initializer lists");
5936 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5937 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5938 expression_list = make_tree_vector_single (t);
5941 expression_list = (cp_parser_parenthesized_expression_list
5942 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5943 /*non_constant_p=*/NULL));
5945 return expression_list;
5948 /* Parse a delete-expression.
5951 :: [opt] delete cast-expression
5952 :: [opt] delete [ ] cast-expression
5954 Returns a representation of the expression. */
5957 cp_parser_delete_expression (cp_parser* parser)
5959 bool global_scope_p;
5963 /* Look for the optional `::' operator. */
5965 = (cp_parser_global_scope_opt (parser,
5966 /*current_scope_valid_p=*/false)
5968 /* Look for the `delete' keyword. */
5969 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5970 /* See if the array syntax is in use. */
5971 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5973 /* Consume the `[' token. */
5974 cp_lexer_consume_token (parser->lexer);
5975 /* Look for the `]' token. */
5976 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5977 /* Remember that this is the `[]' construct. */
5983 /* Parse the cast-expression. */
5984 expression = cp_parser_simple_cast_expression (parser);
5986 /* A delete-expression may not appear in an integral constant
5988 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5989 return error_mark_node;
5991 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5994 /* Returns true if TOKEN may start a cast-expression and false
5998 cp_parser_token_starts_cast_expression (cp_token *token)
6000 switch (token->type)
6006 case CPP_CLOSE_SQUARE:
6007 case CPP_CLOSE_PAREN:
6008 case CPP_CLOSE_BRACE:
6012 case CPP_DEREF_STAR:
6020 case CPP_GREATER_EQ:
6040 /* '[' may start a primary-expression in obj-c++. */
6041 case CPP_OPEN_SQUARE:
6042 return c_dialect_objc ();
6049 /* Parse a cast-expression.
6053 ( type-id ) cast-expression
6055 ADDRESS_P is true iff the unary-expression is appearing as the
6056 operand of the `&' operator. CAST_P is true if this expression is
6057 the target of a cast.
6059 Returns a representation of the expression. */
6062 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6065 /* If it's a `(', then we might be looking at a cast. */
6066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6068 tree type = NULL_TREE;
6069 tree expr = NULL_TREE;
6070 bool compound_literal_p;
6071 const char *saved_message;
6073 /* There's no way to know yet whether or not this is a cast.
6074 For example, `(int (3))' is a unary-expression, while `(int)
6075 3' is a cast. So, we resort to parsing tentatively. */
6076 cp_parser_parse_tentatively (parser);
6077 /* Types may not be defined in a cast. */
6078 saved_message = parser->type_definition_forbidden_message;
6079 parser->type_definition_forbidden_message
6080 = "types may not be defined in casts";
6081 /* Consume the `('. */
6082 cp_lexer_consume_token (parser->lexer);
6083 /* A very tricky bit is that `(struct S) { 3 }' is a
6084 compound-literal (which we permit in C++ as an extension).
6085 But, that construct is not a cast-expression -- it is a
6086 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6087 is legal; if the compound-literal were a cast-expression,
6088 you'd need an extra set of parentheses.) But, if we parse
6089 the type-id, and it happens to be a class-specifier, then we
6090 will commit to the parse at that point, because we cannot
6091 undo the action that is done when creating a new class. So,
6092 then we cannot back up and do a postfix-expression.
6094 Therefore, we scan ahead to the closing `)', and check to see
6095 if the token after the `)' is a `{'. If so, we are not
6096 looking at a cast-expression.
6098 Save tokens so that we can put them back. */
6099 cp_lexer_save_tokens (parser->lexer);
6100 /* Skip tokens until the next token is a closing parenthesis.
6101 If we find the closing `)', and the next token is a `{', then
6102 we are looking at a compound-literal. */
6104 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6105 /*consume_paren=*/true)
6106 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6107 /* Roll back the tokens we skipped. */
6108 cp_lexer_rollback_tokens (parser->lexer);
6109 /* If we were looking at a compound-literal, simulate an error
6110 so that the call to cp_parser_parse_definitely below will
6112 if (compound_literal_p)
6113 cp_parser_simulate_error (parser);
6116 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6117 parser->in_type_id_in_expr_p = true;
6118 /* Look for the type-id. */
6119 type = cp_parser_type_id (parser);
6120 /* Look for the closing `)'. */
6121 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6122 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6125 /* Restore the saved message. */
6126 parser->type_definition_forbidden_message = saved_message;
6128 /* At this point this can only be either a cast or a
6129 parenthesized ctor such as `(T ())' that looks like a cast to
6130 function returning T. */
6131 if (!cp_parser_error_occurred (parser)
6132 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6135 cp_parser_parse_definitely (parser);
6136 expr = cp_parser_cast_expression (parser,
6137 /*address_p=*/false,
6138 /*cast_p=*/true, pidk);
6140 /* Warn about old-style casts, if so requested. */
6141 if (warn_old_style_cast
6142 && !in_system_header
6143 && !VOID_TYPE_P (type)
6144 && current_lang_name != lang_name_c)
6145 warning (OPT_Wold_style_cast, "use of old-style cast");
6147 /* Only type conversions to integral or enumeration types
6148 can be used in constant-expressions. */
6149 if (!cast_valid_in_integral_constant_expression_p (type)
6150 && (cp_parser_non_integral_constant_expression
6152 "a cast to a type other than an integral or "
6153 "enumeration type")))
6154 return error_mark_node;
6156 /* Perform the cast. */
6157 expr = build_c_cast (input_location, type, expr);
6161 cp_parser_abort_tentative_parse (parser);
6164 /* If we get here, then it's not a cast, so it must be a
6165 unary-expression. */
6166 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6169 /* Parse a binary expression of the general form:
6173 pm-expression .* cast-expression
6174 pm-expression ->* cast-expression
6176 multiplicative-expression:
6178 multiplicative-expression * pm-expression
6179 multiplicative-expression / pm-expression
6180 multiplicative-expression % pm-expression
6182 additive-expression:
6183 multiplicative-expression
6184 additive-expression + multiplicative-expression
6185 additive-expression - multiplicative-expression
6189 shift-expression << additive-expression
6190 shift-expression >> additive-expression
6192 relational-expression:
6194 relational-expression < shift-expression
6195 relational-expression > shift-expression
6196 relational-expression <= shift-expression
6197 relational-expression >= shift-expression
6201 relational-expression:
6202 relational-expression <? shift-expression
6203 relational-expression >? shift-expression
6205 equality-expression:
6206 relational-expression
6207 equality-expression == relational-expression
6208 equality-expression != relational-expression
6212 and-expression & equality-expression
6214 exclusive-or-expression:
6216 exclusive-or-expression ^ and-expression
6218 inclusive-or-expression:
6219 exclusive-or-expression
6220 inclusive-or-expression | exclusive-or-expression
6222 logical-and-expression:
6223 inclusive-or-expression
6224 logical-and-expression && inclusive-or-expression
6226 logical-or-expression:
6227 logical-and-expression
6228 logical-or-expression || logical-and-expression
6230 All these are implemented with a single function like:
6233 simple-cast-expression
6234 binary-expression <token> binary-expression
6236 CAST_P is true if this expression is the target of a cast.
6238 The binops_by_token map is used to get the tree codes for each <token> type.
6239 binary-expressions are associated according to a precedence table. */
6241 #define TOKEN_PRECEDENCE(token) \
6242 (((token->type == CPP_GREATER \
6243 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6244 && !parser->greater_than_is_operator_p) \
6245 ? PREC_NOT_OPERATOR \
6246 : binops_by_token[token->type].prec)
6249 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6250 bool no_toplevel_fold_p,
6251 enum cp_parser_prec prec,
6254 cp_parser_expression_stack stack;
6255 cp_parser_expression_stack_entry *sp = &stack[0];
6258 enum tree_code tree_type, lhs_type, rhs_type;
6259 enum cp_parser_prec new_prec, lookahead_prec;
6262 /* Parse the first expression. */
6263 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6264 lhs_type = ERROR_MARK;
6268 /* Get an operator token. */
6269 token = cp_lexer_peek_token (parser->lexer);
6271 if (warn_cxx0x_compat
6272 && token->type == CPP_RSHIFT
6273 && !parser->greater_than_is_operator_p)
6275 if (warning_at (token->location, OPT_Wc__0x_compat,
6276 "%<>>%> operator will be treated as"
6277 " two right angle brackets in C++0x"))
6278 inform (token->location,
6279 "suggest parentheses around %<>>%> expression");
6282 new_prec = TOKEN_PRECEDENCE (token);
6284 /* Popping an entry off the stack means we completed a subexpression:
6285 - either we found a token which is not an operator (`>' where it is not
6286 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6287 will happen repeatedly;
6288 - or, we found an operator which has lower priority. This is the case
6289 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6291 if (new_prec <= prec)
6300 tree_type = binops_by_token[token->type].tree_type;
6302 /* We used the operator token. */
6303 cp_lexer_consume_token (parser->lexer);
6305 /* For "false && x" or "true || x", x will never be executed;
6306 disable warnings while evaluating it. */
6307 if (tree_type == TRUTH_ANDIF_EXPR)
6308 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6309 else if (tree_type == TRUTH_ORIF_EXPR)
6310 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6312 /* Extract another operand. It may be the RHS of this expression
6313 or the LHS of a new, higher priority expression. */
6314 rhs = cp_parser_simple_cast_expression (parser);
6315 rhs_type = ERROR_MARK;
6317 /* Get another operator token. Look up its precedence to avoid
6318 building a useless (immediately popped) stack entry for common
6319 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6320 token = cp_lexer_peek_token (parser->lexer);
6321 lookahead_prec = TOKEN_PRECEDENCE (token);
6322 if (lookahead_prec > new_prec)
6324 /* ... and prepare to parse the RHS of the new, higher priority
6325 expression. Since precedence levels on the stack are
6326 monotonically increasing, we do not have to care about
6329 sp->tree_type = tree_type;
6331 sp->lhs_type = lhs_type;
6334 lhs_type = rhs_type;
6336 new_prec = lookahead_prec;
6340 lookahead_prec = new_prec;
6341 /* If the stack is not empty, we have parsed into LHS the right side
6342 (`4' in the example above) of an expression we had suspended.
6343 We can use the information on the stack to recover the LHS (`3')
6344 from the stack together with the tree code (`MULT_EXPR'), and
6345 the precedence of the higher level subexpression
6346 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6347 which will be used to actually build the additive expression. */
6350 tree_type = sp->tree_type;
6352 rhs_type = lhs_type;
6354 lhs_type = sp->lhs_type;
6357 /* Undo the disabling of warnings done above. */
6358 if (tree_type == TRUTH_ANDIF_EXPR)
6359 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6360 else if (tree_type == TRUTH_ORIF_EXPR)
6361 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6363 overloaded_p = false;
6364 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6365 ERROR_MARK for everything that is not a binary expression.
6366 This makes warn_about_parentheses miss some warnings that
6367 involve unary operators. For unary expressions we should
6368 pass the correct tree_code unless the unary expression was
6369 surrounded by parentheses.
6371 if (no_toplevel_fold_p
6372 && lookahead_prec <= prec
6374 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6375 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6377 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6378 &overloaded_p, tf_warning_or_error);
6379 lhs_type = tree_type;
6381 /* If the binary operator required the use of an overloaded operator,
6382 then this expression cannot be an integral constant-expression.
6383 An overloaded operator can be used even if both operands are
6384 otherwise permissible in an integral constant-expression if at
6385 least one of the operands is of enumeration type. */
6388 && (cp_parser_non_integral_constant_expression
6389 (parser, "calls to overloaded operators")))
6390 return error_mark_node;
6397 /* Parse the `? expression : assignment-expression' part of a
6398 conditional-expression. The LOGICAL_OR_EXPR is the
6399 logical-or-expression that started the conditional-expression.
6400 Returns a representation of the entire conditional-expression.
6402 This routine is used by cp_parser_assignment_expression.
6404 ? expression : assignment-expression
6408 ? : assignment-expression */
6411 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6414 tree assignment_expr;
6416 /* Consume the `?' token. */
6417 cp_lexer_consume_token (parser->lexer);
6418 if (cp_parser_allow_gnu_extensions_p (parser)
6419 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6421 /* Implicit true clause. */
6423 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6427 /* Parse the expression. */
6428 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6429 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6430 c_inhibit_evaluation_warnings +=
6431 ((logical_or_expr == truthvalue_true_node)
6432 - (logical_or_expr == truthvalue_false_node));
6435 /* The next token should be a `:'. */
6436 cp_parser_require (parser, CPP_COLON, "%<:%>");
6437 /* Parse the assignment-expression. */
6438 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6439 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6441 /* Build the conditional-expression. */
6442 return build_x_conditional_expr (logical_or_expr,
6445 tf_warning_or_error);
6448 /* Parse an assignment-expression.
6450 assignment-expression:
6451 conditional-expression
6452 logical-or-expression assignment-operator assignment_expression
6455 CAST_P is true if this expression is the target of a cast.
6457 Returns a representation for the expression. */
6460 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6465 /* If the next token is the `throw' keyword, then we're looking at
6466 a throw-expression. */
6467 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6468 expr = cp_parser_throw_expression (parser);
6469 /* Otherwise, it must be that we are looking at a
6470 logical-or-expression. */
6473 /* Parse the binary expressions (logical-or-expression). */
6474 expr = cp_parser_binary_expression (parser, cast_p, false,
6475 PREC_NOT_OPERATOR, pidk);
6476 /* If the next token is a `?' then we're actually looking at a
6477 conditional-expression. */
6478 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6479 return cp_parser_question_colon_clause (parser, expr);
6482 enum tree_code assignment_operator;
6484 /* If it's an assignment-operator, we're using the second
6487 = cp_parser_assignment_operator_opt (parser);
6488 if (assignment_operator != ERROR_MARK)
6490 bool non_constant_p;
6492 /* Parse the right-hand side of the assignment. */
6493 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6495 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6496 maybe_warn_cpp0x ("extended initializer lists");
6498 /* An assignment may not appear in a
6499 constant-expression. */
6500 if (cp_parser_non_integral_constant_expression (parser,
6502 return error_mark_node;
6503 /* Build the assignment expression. */
6504 expr = build_x_modify_expr (expr,
6505 assignment_operator,
6507 tf_warning_or_error);
6515 /* Parse an (optional) assignment-operator.
6517 assignment-operator: one of
6518 = *= /= %= += -= >>= <<= &= ^= |=
6522 assignment-operator: one of
6525 If the next token is an assignment operator, the corresponding tree
6526 code is returned, and the token is consumed. For example, for
6527 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6528 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6529 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6530 operator, ERROR_MARK is returned. */
6532 static enum tree_code
6533 cp_parser_assignment_operator_opt (cp_parser* parser)
6538 /* Peek at the next token. */
6539 token = cp_lexer_peek_token (parser->lexer);
6541 switch (token->type)
6552 op = TRUNC_DIV_EXPR;
6556 op = TRUNC_MOD_EXPR;
6588 /* Nothing else is an assignment operator. */
6592 /* If it was an assignment operator, consume it. */
6593 if (op != ERROR_MARK)
6594 cp_lexer_consume_token (parser->lexer);
6599 /* Parse an expression.
6602 assignment-expression
6603 expression , assignment-expression
6605 CAST_P is true if this expression is the target of a cast.
6607 Returns a representation of the expression. */
6610 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6612 tree expression = NULL_TREE;
6616 tree assignment_expression;
6618 /* Parse the next assignment-expression. */
6619 assignment_expression
6620 = cp_parser_assignment_expression (parser, cast_p, pidk);
6621 /* If this is the first assignment-expression, we can just
6624 expression = assignment_expression;
6626 expression = build_x_compound_expr (expression,
6627 assignment_expression,
6628 tf_warning_or_error);
6629 /* If the next token is not a comma, then we are done with the
6631 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6633 /* Consume the `,'. */
6634 cp_lexer_consume_token (parser->lexer);
6635 /* A comma operator cannot appear in a constant-expression. */
6636 if (cp_parser_non_integral_constant_expression (parser,
6637 "a comma operator"))
6638 expression = error_mark_node;
6644 /* Parse a constant-expression.
6646 constant-expression:
6647 conditional-expression
6649 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6650 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6651 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6652 is false, NON_CONSTANT_P should be NULL. */
6655 cp_parser_constant_expression (cp_parser* parser,
6656 bool allow_non_constant_p,
6657 bool *non_constant_p)
6659 bool saved_integral_constant_expression_p;
6660 bool saved_allow_non_integral_constant_expression_p;
6661 bool saved_non_integral_constant_expression_p;
6664 /* It might seem that we could simply parse the
6665 conditional-expression, and then check to see if it were
6666 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6667 one that the compiler can figure out is constant, possibly after
6668 doing some simplifications or optimizations. The standard has a
6669 precise definition of constant-expression, and we must honor
6670 that, even though it is somewhat more restrictive.
6676 is not a legal declaration, because `(2, 3)' is not a
6677 constant-expression. The `,' operator is forbidden in a
6678 constant-expression. However, GCC's constant-folding machinery
6679 will fold this operation to an INTEGER_CST for `3'. */
6681 /* Save the old settings. */
6682 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6683 saved_allow_non_integral_constant_expression_p
6684 = parser->allow_non_integral_constant_expression_p;
6685 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6686 /* We are now parsing a constant-expression. */
6687 parser->integral_constant_expression_p = true;
6688 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6689 parser->non_integral_constant_expression_p = false;
6690 /* Although the grammar says "conditional-expression", we parse an
6691 "assignment-expression", which also permits "throw-expression"
6692 and the use of assignment operators. In the case that
6693 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6694 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6695 actually essential that we look for an assignment-expression.
6696 For example, cp_parser_initializer_clauses uses this function to
6697 determine whether a particular assignment-expression is in fact
6699 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6700 /* Restore the old settings. */
6701 parser->integral_constant_expression_p
6702 = saved_integral_constant_expression_p;
6703 parser->allow_non_integral_constant_expression_p
6704 = saved_allow_non_integral_constant_expression_p;
6705 if (allow_non_constant_p)
6706 *non_constant_p = parser->non_integral_constant_expression_p;
6707 else if (parser->non_integral_constant_expression_p)
6708 expression = error_mark_node;
6709 parser->non_integral_constant_expression_p
6710 = saved_non_integral_constant_expression_p;
6715 /* Parse __builtin_offsetof.
6717 offsetof-expression:
6718 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6720 offsetof-member-designator:
6722 | offsetof-member-designator "." id-expression
6723 | offsetof-member-designator "[" expression "]"
6724 | offsetof-member-designator "->" id-expression */
6727 cp_parser_builtin_offsetof (cp_parser *parser)
6729 int save_ice_p, save_non_ice_p;
6734 /* We're about to accept non-integral-constant things, but will
6735 definitely yield an integral constant expression. Save and
6736 restore these values around our local parsing. */
6737 save_ice_p = parser->integral_constant_expression_p;
6738 save_non_ice_p = parser->non_integral_constant_expression_p;
6740 /* Consume the "__builtin_offsetof" token. */
6741 cp_lexer_consume_token (parser->lexer);
6742 /* Consume the opening `('. */
6743 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6744 /* Parse the type-id. */
6745 type = cp_parser_type_id (parser);
6746 /* Look for the `,'. */
6747 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6748 token = cp_lexer_peek_token (parser->lexer);
6750 /* Build the (type *)null that begins the traditional offsetof macro. */
6751 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6752 tf_warning_or_error);
6754 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6755 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6756 true, &dummy, token->location);
6759 token = cp_lexer_peek_token (parser->lexer);
6760 switch (token->type)
6762 case CPP_OPEN_SQUARE:
6763 /* offsetof-member-designator "[" expression "]" */
6764 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6768 /* offsetof-member-designator "->" identifier */
6769 expr = grok_array_decl (expr, integer_zero_node);
6773 /* offsetof-member-designator "." identifier */
6774 cp_lexer_consume_token (parser->lexer);
6775 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6780 case CPP_CLOSE_PAREN:
6781 /* Consume the ")" token. */
6782 cp_lexer_consume_token (parser->lexer);
6786 /* Error. We know the following require will fail, but
6787 that gives the proper error message. */
6788 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6789 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6790 expr = error_mark_node;
6796 /* If we're processing a template, we can't finish the semantics yet.
6797 Otherwise we can fold the entire expression now. */
6798 if (processing_template_decl)
6799 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6801 expr = finish_offsetof (expr);
6804 parser->integral_constant_expression_p = save_ice_p;
6805 parser->non_integral_constant_expression_p = save_non_ice_p;
6810 /* Parse a trait expression. */
6813 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6816 tree type1, type2 = NULL_TREE;
6817 bool binary = false;
6818 cp_decl_specifier_seq decl_specs;
6822 case RID_HAS_NOTHROW_ASSIGN:
6823 kind = CPTK_HAS_NOTHROW_ASSIGN;
6825 case RID_HAS_NOTHROW_CONSTRUCTOR:
6826 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6828 case RID_HAS_NOTHROW_COPY:
6829 kind = CPTK_HAS_NOTHROW_COPY;
6831 case RID_HAS_TRIVIAL_ASSIGN:
6832 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6834 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6835 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6837 case RID_HAS_TRIVIAL_COPY:
6838 kind = CPTK_HAS_TRIVIAL_COPY;
6840 case RID_HAS_TRIVIAL_DESTRUCTOR:
6841 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6843 case RID_HAS_VIRTUAL_DESTRUCTOR:
6844 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6846 case RID_IS_ABSTRACT:
6847 kind = CPTK_IS_ABSTRACT;
6849 case RID_IS_BASE_OF:
6850 kind = CPTK_IS_BASE_OF;
6854 kind = CPTK_IS_CLASS;
6856 case RID_IS_CONVERTIBLE_TO:
6857 kind = CPTK_IS_CONVERTIBLE_TO;
6861 kind = CPTK_IS_EMPTY;
6864 kind = CPTK_IS_ENUM;
6869 case RID_IS_POLYMORPHIC:
6870 kind = CPTK_IS_POLYMORPHIC;
6872 case RID_IS_STD_LAYOUT:
6873 kind = CPTK_IS_STD_LAYOUT;
6875 case RID_IS_TRIVIAL:
6876 kind = CPTK_IS_TRIVIAL;
6879 kind = CPTK_IS_UNION;
6885 /* Consume the token. */
6886 cp_lexer_consume_token (parser->lexer);
6888 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6890 type1 = cp_parser_type_id (parser);
6892 if (type1 == error_mark_node)
6893 return error_mark_node;
6895 /* Build a trivial decl-specifier-seq. */
6896 clear_decl_specs (&decl_specs);
6897 decl_specs.type = type1;
6899 /* Call grokdeclarator to figure out what type this is. */
6900 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6901 /*initialized=*/0, /*attrlist=*/NULL);
6905 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6907 type2 = cp_parser_type_id (parser);
6909 if (type2 == error_mark_node)
6910 return error_mark_node;
6912 /* Build a trivial decl-specifier-seq. */
6913 clear_decl_specs (&decl_specs);
6914 decl_specs.type = type2;
6916 /* Call grokdeclarator to figure out what type this is. */
6917 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6918 /*initialized=*/0, /*attrlist=*/NULL);
6921 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6923 /* Complete the trait expression, which may mean either processing
6924 the trait expr now or saving it for template instantiation. */
6925 return finish_trait_expr (kind, type1, type2);
6928 /* Statements [gram.stmt.stmt] */
6930 /* Parse a statement.
6934 expression-statement
6939 declaration-statement
6942 IN_COMPOUND is true when the statement is nested inside a
6943 cp_parser_compound_statement; this matters for certain pragmas.
6945 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6946 is a (possibly labeled) if statement which is not enclosed in braces
6947 and has an else clause. This is used to implement -Wparentheses. */
6950 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6951 bool in_compound, bool *if_p)
6955 location_t statement_location;
6960 /* There is no statement yet. */
6961 statement = NULL_TREE;
6962 /* Peek at the next token. */
6963 token = cp_lexer_peek_token (parser->lexer);
6964 /* Remember the location of the first token in the statement. */
6965 statement_location = token->location;
6966 /* If this is a keyword, then that will often determine what kind of
6967 statement we have. */
6968 if (token->type == CPP_KEYWORD)
6970 enum rid keyword = token->keyword;
6976 /* Looks like a labeled-statement with a case label.
6977 Parse the label, and then use tail recursion to parse
6979 cp_parser_label_for_labeled_statement (parser);
6984 statement = cp_parser_selection_statement (parser, if_p);
6990 statement = cp_parser_iteration_statement (parser);
6997 statement = cp_parser_jump_statement (parser);
7000 /* Objective-C++ exception-handling constructs. */
7003 case RID_AT_FINALLY:
7004 case RID_AT_SYNCHRONIZED:
7006 statement = cp_parser_objc_statement (parser);
7010 statement = cp_parser_try_block (parser);
7014 /* This must be a namespace alias definition. */
7015 cp_parser_declaration_statement (parser);
7019 /* It might be a keyword like `int' that can start a
7020 declaration-statement. */
7024 else if (token->type == CPP_NAME)
7026 /* If the next token is a `:', then we are looking at a
7027 labeled-statement. */
7028 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7029 if (token->type == CPP_COLON)
7031 /* Looks like a labeled-statement with an ordinary label.
7032 Parse the label, and then use tail recursion to parse
7034 cp_parser_label_for_labeled_statement (parser);
7038 /* Anything that starts with a `{' must be a compound-statement. */
7039 else if (token->type == CPP_OPEN_BRACE)
7040 statement = cp_parser_compound_statement (parser, NULL, false);
7041 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7042 a statement all its own. */
7043 else if (token->type == CPP_PRAGMA)
7045 /* Only certain OpenMP pragmas are attached to statements, and thus
7046 are considered statements themselves. All others are not. In
7047 the context of a compound, accept the pragma as a "statement" and
7048 return so that we can check for a close brace. Otherwise we
7049 require a real statement and must go back and read one. */
7051 cp_parser_pragma (parser, pragma_compound);
7052 else if (!cp_parser_pragma (parser, pragma_stmt))
7056 else if (token->type == CPP_EOF)
7058 cp_parser_error (parser, "expected statement");
7062 /* Everything else must be a declaration-statement or an
7063 expression-statement. Try for the declaration-statement
7064 first, unless we are looking at a `;', in which case we know that
7065 we have an expression-statement. */
7068 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7070 cp_parser_parse_tentatively (parser);
7071 /* Try to parse the declaration-statement. */
7072 cp_parser_declaration_statement (parser);
7073 /* If that worked, we're done. */
7074 if (cp_parser_parse_definitely (parser))
7077 /* Look for an expression-statement instead. */
7078 statement = cp_parser_expression_statement (parser, in_statement_expr);
7081 /* Set the line number for the statement. */
7082 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7083 SET_EXPR_LOCATION (statement, statement_location);
7086 /* Parse the label for a labeled-statement, i.e.
7089 case constant-expression :
7093 case constant-expression ... constant-expression : statement
7095 When a label is parsed without errors, the label is added to the
7096 parse tree by the finish_* functions, so this function doesn't
7097 have to return the label. */
7100 cp_parser_label_for_labeled_statement (cp_parser* parser)
7103 tree label = NULL_TREE;
7105 /* The next token should be an identifier. */
7106 token = cp_lexer_peek_token (parser->lexer);
7107 if (token->type != CPP_NAME
7108 && token->type != CPP_KEYWORD)
7110 cp_parser_error (parser, "expected labeled-statement");
7114 switch (token->keyword)
7121 /* Consume the `case' token. */
7122 cp_lexer_consume_token (parser->lexer);
7123 /* Parse the constant-expression. */
7124 expr = cp_parser_constant_expression (parser,
7125 /*allow_non_constant_p=*/false,
7128 ellipsis = cp_lexer_peek_token (parser->lexer);
7129 if (ellipsis->type == CPP_ELLIPSIS)
7131 /* Consume the `...' token. */
7132 cp_lexer_consume_token (parser->lexer);
7134 cp_parser_constant_expression (parser,
7135 /*allow_non_constant_p=*/false,
7137 /* We don't need to emit warnings here, as the common code
7138 will do this for us. */
7141 expr_hi = NULL_TREE;
7143 if (parser->in_switch_statement_p)
7144 finish_case_label (token->location, expr, expr_hi);
7146 error_at (token->location,
7147 "case label %qE not within a switch statement",
7153 /* Consume the `default' token. */
7154 cp_lexer_consume_token (parser->lexer);
7156 if (parser->in_switch_statement_p)
7157 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7159 error_at (token->location, "case label not within a switch statement");
7163 /* Anything else must be an ordinary label. */
7164 label = finish_label_stmt (cp_parser_identifier (parser));
7168 /* Require the `:' token. */
7169 cp_parser_require (parser, CPP_COLON, "%<:%>");
7171 /* An ordinary label may optionally be followed by attributes.
7172 However, this is only permitted if the attributes are then
7173 followed by a semicolon. This is because, for backward
7174 compatibility, when parsing
7175 lab: __attribute__ ((unused)) int i;
7176 we want the attribute to attach to "i", not "lab". */
7177 if (label != NULL_TREE
7178 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7182 cp_parser_parse_tentatively (parser);
7183 attrs = cp_parser_attributes_opt (parser);
7184 if (attrs == NULL_TREE
7185 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7186 cp_parser_abort_tentative_parse (parser);
7187 else if (!cp_parser_parse_definitely (parser))
7190 cplus_decl_attributes (&label, attrs, 0);
7194 /* Parse an expression-statement.
7196 expression-statement:
7199 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7200 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7201 indicates whether this expression-statement is part of an
7202 expression statement. */
7205 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7207 tree statement = NULL_TREE;
7209 /* If the next token is a ';', then there is no expression
7211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7212 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7214 /* Consume the final `;'. */
7215 cp_parser_consume_semicolon_at_end_of_statement (parser);
7217 if (in_statement_expr
7218 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7219 /* This is the final expression statement of a statement
7221 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7223 statement = finish_expr_stmt (statement);
7230 /* Parse a compound-statement.
7233 { statement-seq [opt] }
7238 { label-declaration-seq [opt] statement-seq [opt] }
7240 label-declaration-seq:
7242 label-declaration-seq label-declaration
7244 Returns a tree representing the statement. */
7247 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7252 /* Consume the `{'. */
7253 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7254 return error_mark_node;
7255 /* Begin the compound-statement. */
7256 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7257 /* If the next keyword is `__label__' we have a label declaration. */
7258 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7259 cp_parser_label_declaration (parser);
7260 /* Parse an (optional) statement-seq. */
7261 cp_parser_statement_seq_opt (parser, in_statement_expr);
7262 /* Finish the compound-statement. */
7263 finish_compound_stmt (compound_stmt);
7264 /* Consume the `}'. */
7265 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7267 return compound_stmt;
7270 /* Parse an (optional) statement-seq.
7274 statement-seq [opt] statement */
7277 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7279 /* Scan statements until there aren't any more. */
7282 cp_token *token = cp_lexer_peek_token (parser->lexer);
7284 /* If we're looking at a `}', then we've run out of statements. */
7285 if (token->type == CPP_CLOSE_BRACE
7286 || token->type == CPP_EOF
7287 || token->type == CPP_PRAGMA_EOL)
7290 /* If we are in a compound statement and find 'else' then
7291 something went wrong. */
7292 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7294 if (parser->in_statement & IN_IF_STMT)
7298 token = cp_lexer_consume_token (parser->lexer);
7299 error_at (token->location, "%<else%> without a previous %<if%>");
7303 /* Parse the statement. */
7304 cp_parser_statement (parser, in_statement_expr, true, NULL);
7308 /* Parse a selection-statement.
7310 selection-statement:
7311 if ( condition ) statement
7312 if ( condition ) statement else statement
7313 switch ( condition ) statement
7315 Returns the new IF_STMT or SWITCH_STMT.
7317 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7318 is a (possibly labeled) if statement which is not enclosed in
7319 braces and has an else clause. This is used to implement
7323 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7331 /* Peek at the next token. */
7332 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7334 /* See what kind of keyword it is. */
7335 keyword = token->keyword;
7344 /* Look for the `('. */
7345 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7347 cp_parser_skip_to_end_of_statement (parser);
7348 return error_mark_node;
7351 /* Begin the selection-statement. */
7352 if (keyword == RID_IF)
7353 statement = begin_if_stmt ();
7355 statement = begin_switch_stmt ();
7357 /* Parse the condition. */
7358 condition = cp_parser_condition (parser);
7359 /* Look for the `)'. */
7360 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7361 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7362 /*consume_paren=*/true);
7364 if (keyword == RID_IF)
7367 unsigned char in_statement;
7369 /* Add the condition. */
7370 finish_if_stmt_cond (condition, statement);
7372 /* Parse the then-clause. */
7373 in_statement = parser->in_statement;
7374 parser->in_statement |= IN_IF_STMT;
7375 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7377 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7378 add_stmt (build_empty_stmt (loc));
7379 cp_lexer_consume_token (parser->lexer);
7380 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7381 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7382 "empty body in an %<if%> statement");
7386 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7387 parser->in_statement = in_statement;
7389 finish_then_clause (statement);
7391 /* If the next token is `else', parse the else-clause. */
7392 if (cp_lexer_next_token_is_keyword (parser->lexer,
7395 /* Consume the `else' keyword. */
7396 cp_lexer_consume_token (parser->lexer);
7397 begin_else_clause (statement);
7398 /* Parse the else-clause. */
7399 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7402 loc = cp_lexer_peek_token (parser->lexer)->location;
7404 OPT_Wempty_body, "suggest braces around "
7405 "empty body in an %<else%> statement");
7406 add_stmt (build_empty_stmt (loc));
7407 cp_lexer_consume_token (parser->lexer);
7410 cp_parser_implicitly_scoped_statement (parser, NULL);
7412 finish_else_clause (statement);
7414 /* If we are currently parsing a then-clause, then
7415 IF_P will not be NULL. We set it to true to
7416 indicate that this if statement has an else clause.
7417 This may trigger the Wparentheses warning below
7418 when we get back up to the parent if statement. */
7424 /* This if statement does not have an else clause. If
7425 NESTED_IF is true, then the then-clause is an if
7426 statement which does have an else clause. We warn
7427 about the potential ambiguity. */
7429 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7430 "suggest explicit braces to avoid ambiguous"
7434 /* Now we're all done with the if-statement. */
7435 finish_if_stmt (statement);
7439 bool in_switch_statement_p;
7440 unsigned char in_statement;
7442 /* Add the condition. */
7443 finish_switch_cond (condition, statement);
7445 /* Parse the body of the switch-statement. */
7446 in_switch_statement_p = parser->in_switch_statement_p;
7447 in_statement = parser->in_statement;
7448 parser->in_switch_statement_p = true;
7449 parser->in_statement |= IN_SWITCH_STMT;
7450 cp_parser_implicitly_scoped_statement (parser, NULL);
7451 parser->in_switch_statement_p = in_switch_statement_p;
7452 parser->in_statement = in_statement;
7454 /* Now we're all done with the switch-statement. */
7455 finish_switch_stmt (statement);
7463 cp_parser_error (parser, "expected selection-statement");
7464 return error_mark_node;
7468 /* Parse a condition.
7472 type-specifier-seq declarator = initializer-clause
7473 type-specifier-seq declarator braced-init-list
7478 type-specifier-seq declarator asm-specification [opt]
7479 attributes [opt] = assignment-expression
7481 Returns the expression that should be tested. */
7484 cp_parser_condition (cp_parser* parser)
7486 cp_decl_specifier_seq type_specifiers;
7487 const char *saved_message;
7489 /* Try the declaration first. */
7490 cp_parser_parse_tentatively (parser);
7491 /* New types are not allowed in the type-specifier-seq for a
7493 saved_message = parser->type_definition_forbidden_message;
7494 parser->type_definition_forbidden_message
7495 = "types may not be defined in conditions";
7496 /* Parse the type-specifier-seq. */
7497 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7499 /* Restore the saved message. */
7500 parser->type_definition_forbidden_message = saved_message;
7501 /* If all is well, we might be looking at a declaration. */
7502 if (!cp_parser_error_occurred (parser))
7505 tree asm_specification;
7507 cp_declarator *declarator;
7508 tree initializer = NULL_TREE;
7510 /* Parse the declarator. */
7511 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7512 /*ctor_dtor_or_conv_p=*/NULL,
7513 /*parenthesized_p=*/NULL,
7514 /*member_p=*/false);
7515 /* Parse the attributes. */
7516 attributes = cp_parser_attributes_opt (parser);
7517 /* Parse the asm-specification. */
7518 asm_specification = cp_parser_asm_specification_opt (parser);
7519 /* If the next token is not an `=' or '{', then we might still be
7520 looking at an expression. For example:
7524 looks like a decl-specifier-seq and a declarator -- but then
7525 there is no `=', so this is an expression. */
7526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7527 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7528 cp_parser_simulate_error (parser);
7530 /* If we did see an `=' or '{', then we are looking at a declaration
7532 if (cp_parser_parse_definitely (parser))
7535 bool non_constant_p;
7536 bool flags = LOOKUP_ONLYCONVERTING;
7538 /* Create the declaration. */
7539 decl = start_decl (declarator, &type_specifiers,
7540 /*initialized_p=*/true,
7541 attributes, /*prefix_attributes=*/NULL_TREE,
7544 /* Parse the initializer. */
7545 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7547 initializer = cp_parser_braced_list (parser, &non_constant_p);
7548 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7553 /* Consume the `='. */
7554 cp_parser_require (parser, CPP_EQ, "%<=%>");
7555 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7557 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7558 maybe_warn_cpp0x ("extended initializer lists");
7560 if (!non_constant_p)
7561 initializer = fold_non_dependent_expr (initializer);
7563 /* Process the initializer. */
7564 cp_finish_decl (decl,
7565 initializer, !non_constant_p,
7570 pop_scope (pushed_scope);
7572 return convert_from_reference (decl);
7575 /* If we didn't even get past the declarator successfully, we are
7576 definitely not looking at a declaration. */
7578 cp_parser_abort_tentative_parse (parser);
7580 /* Otherwise, we are looking at an expression. */
7581 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7584 /* Parse an iteration-statement.
7586 iteration-statement:
7587 while ( condition ) statement
7588 do statement while ( expression ) ;
7589 for ( for-init-statement condition [opt] ; expression [opt] )
7592 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7595 cp_parser_iteration_statement (cp_parser* parser)
7600 unsigned char in_statement;
7602 /* Peek at the next token. */
7603 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7605 return error_mark_node;
7607 /* Remember whether or not we are already within an iteration
7609 in_statement = parser->in_statement;
7611 /* See what kind of keyword it is. */
7612 keyword = token->keyword;
7619 /* Begin the while-statement. */
7620 statement = begin_while_stmt ();
7621 /* Look for the `('. */
7622 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7623 /* Parse the condition. */
7624 condition = cp_parser_condition (parser);
7625 finish_while_stmt_cond (condition, statement);
7626 /* Look for the `)'. */
7627 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7628 /* Parse the dependent statement. */
7629 parser->in_statement = IN_ITERATION_STMT;
7630 cp_parser_already_scoped_statement (parser);
7631 parser->in_statement = in_statement;
7632 /* We're done with the while-statement. */
7633 finish_while_stmt (statement);
7641 /* Begin the do-statement. */
7642 statement = begin_do_stmt ();
7643 /* Parse the body of the do-statement. */
7644 parser->in_statement = IN_ITERATION_STMT;
7645 cp_parser_implicitly_scoped_statement (parser, NULL);
7646 parser->in_statement = in_statement;
7647 finish_do_body (statement);
7648 /* Look for the `while' keyword. */
7649 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7650 /* Look for the `('. */
7651 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7652 /* Parse the expression. */
7653 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7654 /* We're done with the do-statement. */
7655 finish_do_stmt (expression, statement);
7656 /* Look for the `)'. */
7657 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7658 /* Look for the `;'. */
7659 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7665 tree condition = NULL_TREE;
7666 tree expression = NULL_TREE;
7668 /* Begin the for-statement. */
7669 statement = begin_for_stmt ();
7670 /* Look for the `('. */
7671 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7672 /* Parse the initialization. */
7673 cp_parser_for_init_statement (parser);
7674 finish_for_init_stmt (statement);
7676 /* If there's a condition, process it. */
7677 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7678 condition = cp_parser_condition (parser);
7679 finish_for_cond (condition, statement);
7680 /* Look for the `;'. */
7681 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7683 /* If there's an expression, process it. */
7684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7685 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7686 finish_for_expr (expression, statement);
7687 /* Look for the `)'. */
7688 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7690 /* Parse the body of the for-statement. */
7691 parser->in_statement = IN_ITERATION_STMT;
7692 cp_parser_already_scoped_statement (parser);
7693 parser->in_statement = in_statement;
7695 /* We're done with the for-statement. */
7696 finish_for_stmt (statement);
7701 cp_parser_error (parser, "expected iteration-statement");
7702 statement = error_mark_node;
7709 /* Parse a for-init-statement.
7712 expression-statement
7713 simple-declaration */
7716 cp_parser_for_init_statement (cp_parser* parser)
7718 /* If the next token is a `;', then we have an empty
7719 expression-statement. Grammatically, this is also a
7720 simple-declaration, but an invalid one, because it does not
7721 declare anything. Therefore, if we did not handle this case
7722 specially, we would issue an error message about an invalid
7724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7726 /* We're going to speculatively look for a declaration, falling back
7727 to an expression, if necessary. */
7728 cp_parser_parse_tentatively (parser);
7729 /* Parse the declaration. */
7730 cp_parser_simple_declaration (parser,
7731 /*function_definition_allowed_p=*/false);
7732 /* If the tentative parse failed, then we shall need to look for an
7733 expression-statement. */
7734 if (cp_parser_parse_definitely (parser))
7738 cp_parser_expression_statement (parser, false);
7741 /* Parse a jump-statement.
7746 return expression [opt] ;
7747 return braced-init-list ;
7755 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7758 cp_parser_jump_statement (cp_parser* parser)
7760 tree statement = error_mark_node;
7763 unsigned char in_statement;
7765 /* Peek at the next token. */
7766 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7768 return error_mark_node;
7770 /* See what kind of keyword it is. */
7771 keyword = token->keyword;
7775 in_statement = parser->in_statement & ~IN_IF_STMT;
7776 switch (in_statement)
7779 error_at (token->location, "break statement not within loop or switch");
7782 gcc_assert ((in_statement & IN_SWITCH_STMT)
7783 || in_statement == IN_ITERATION_STMT);
7784 statement = finish_break_stmt ();
7787 error_at (token->location, "invalid exit from OpenMP structured block");
7790 error_at (token->location, "break statement used with OpenMP for loop");
7793 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7797 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7800 error_at (token->location, "continue statement not within a loop");
7802 case IN_ITERATION_STMT:
7804 statement = finish_continue_stmt ();
7807 error_at (token->location, "invalid exit from OpenMP structured block");
7812 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7818 bool expr_non_constant_p;
7820 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7822 maybe_warn_cpp0x ("extended initializer lists");
7823 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7825 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7826 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7828 /* If the next token is a `;', then there is no
7831 /* Build the return-statement. */
7832 statement = finish_return_stmt (expr);
7833 /* Look for the final `;'. */
7834 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7839 /* Create the goto-statement. */
7840 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7842 /* Issue a warning about this use of a GNU extension. */
7843 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7844 /* Consume the '*' token. */
7845 cp_lexer_consume_token (parser->lexer);
7846 /* Parse the dependent expression. */
7847 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7850 finish_goto_stmt (cp_parser_identifier (parser));
7851 /* Look for the final `;'. */
7852 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7856 cp_parser_error (parser, "expected jump-statement");
7863 /* Parse a declaration-statement.
7865 declaration-statement:
7866 block-declaration */
7869 cp_parser_declaration_statement (cp_parser* parser)
7873 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7874 p = obstack_alloc (&declarator_obstack, 0);
7876 /* Parse the block-declaration. */
7877 cp_parser_block_declaration (parser, /*statement_p=*/true);
7879 /* Free any declarators allocated. */
7880 obstack_free (&declarator_obstack, p);
7882 /* Finish off the statement. */
7886 /* Some dependent statements (like `if (cond) statement'), are
7887 implicitly in their own scope. In other words, if the statement is
7888 a single statement (as opposed to a compound-statement), it is
7889 none-the-less treated as if it were enclosed in braces. Any
7890 declarations appearing in the dependent statement are out of scope
7891 after control passes that point. This function parses a statement,
7892 but ensures that is in its own scope, even if it is not a
7895 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7896 is a (possibly labeled) if statement which is not enclosed in
7897 braces and has an else clause. This is used to implement
7900 Returns the new statement. */
7903 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7910 /* Mark if () ; with a special NOP_EXPR. */
7911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7913 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7914 cp_lexer_consume_token (parser->lexer);
7915 statement = add_stmt (build_empty_stmt (loc));
7917 /* if a compound is opened, we simply parse the statement directly. */
7918 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7919 statement = cp_parser_compound_statement (parser, NULL, false);
7920 /* If the token is not a `{', then we must take special action. */
7923 /* Create a compound-statement. */
7924 statement = begin_compound_stmt (0);
7925 /* Parse the dependent-statement. */
7926 cp_parser_statement (parser, NULL_TREE, false, if_p);
7927 /* Finish the dummy compound-statement. */
7928 finish_compound_stmt (statement);
7931 /* Return the statement. */
7935 /* For some dependent statements (like `while (cond) statement'), we
7936 have already created a scope. Therefore, even if the dependent
7937 statement is a compound-statement, we do not want to create another
7941 cp_parser_already_scoped_statement (cp_parser* parser)
7943 /* If the token is a `{', then we must take special action. */
7944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7945 cp_parser_statement (parser, NULL_TREE, false, NULL);
7948 /* Avoid calling cp_parser_compound_statement, so that we
7949 don't create a new scope. Do everything else by hand. */
7950 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7951 /* If the next keyword is `__label__' we have a label declaration. */
7952 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7953 cp_parser_label_declaration (parser);
7954 /* Parse an (optional) statement-seq. */
7955 cp_parser_statement_seq_opt (parser, NULL_TREE);
7956 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7960 /* Declarations [gram.dcl.dcl] */
7962 /* Parse an optional declaration-sequence.
7966 declaration-seq declaration */
7969 cp_parser_declaration_seq_opt (cp_parser* parser)
7975 token = cp_lexer_peek_token (parser->lexer);
7977 if (token->type == CPP_CLOSE_BRACE
7978 || token->type == CPP_EOF
7979 || token->type == CPP_PRAGMA_EOL)
7982 if (token->type == CPP_SEMICOLON)
7984 /* A declaration consisting of a single semicolon is
7985 invalid. Allow it unless we're being pedantic. */
7986 cp_lexer_consume_token (parser->lexer);
7987 if (!in_system_header)
7988 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7992 /* If we're entering or exiting a region that's implicitly
7993 extern "C", modify the lang context appropriately. */
7994 if (!parser->implicit_extern_c && token->implicit_extern_c)
7996 push_lang_context (lang_name_c);
7997 parser->implicit_extern_c = true;
7999 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8001 pop_lang_context ();
8002 parser->implicit_extern_c = false;
8005 if (token->type == CPP_PRAGMA)
8007 /* A top-level declaration can consist solely of a #pragma.
8008 A nested declaration cannot, so this is done here and not
8009 in cp_parser_declaration. (A #pragma at block scope is
8010 handled in cp_parser_statement.) */
8011 cp_parser_pragma (parser, pragma_external);
8015 /* Parse the declaration itself. */
8016 cp_parser_declaration (parser);
8020 /* Parse a declaration.
8025 template-declaration
8026 explicit-instantiation
8027 explicit-specialization
8028 linkage-specification
8029 namespace-definition
8034 __extension__ declaration */
8037 cp_parser_declaration (cp_parser* parser)
8044 /* Check for the `__extension__' keyword. */
8045 if (cp_parser_extension_opt (parser, &saved_pedantic))
8047 /* Parse the qualified declaration. */
8048 cp_parser_declaration (parser);
8049 /* Restore the PEDANTIC flag. */
8050 pedantic = saved_pedantic;
8055 /* Try to figure out what kind of declaration is present. */
8056 token1 = *cp_lexer_peek_token (parser->lexer);
8058 if (token1.type != CPP_EOF)
8059 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8062 token2.type = CPP_EOF;
8063 token2.keyword = RID_MAX;
8066 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8067 p = obstack_alloc (&declarator_obstack, 0);
8069 /* If the next token is `extern' and the following token is a string
8070 literal, then we have a linkage specification. */
8071 if (token1.keyword == RID_EXTERN
8072 && cp_parser_is_string_literal (&token2))
8073 cp_parser_linkage_specification (parser);
8074 /* If the next token is `template', then we have either a template
8075 declaration, an explicit instantiation, or an explicit
8077 else if (token1.keyword == RID_TEMPLATE)
8079 /* `template <>' indicates a template specialization. */
8080 if (token2.type == CPP_LESS
8081 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8082 cp_parser_explicit_specialization (parser);
8083 /* `template <' indicates a template declaration. */
8084 else if (token2.type == CPP_LESS)
8085 cp_parser_template_declaration (parser, /*member_p=*/false);
8086 /* Anything else must be an explicit instantiation. */
8088 cp_parser_explicit_instantiation (parser);
8090 /* If the next token is `export', then we have a template
8092 else if (token1.keyword == RID_EXPORT)
8093 cp_parser_template_declaration (parser, /*member_p=*/false);
8094 /* If the next token is `extern', 'static' or 'inline' and the one
8095 after that is `template', we have a GNU extended explicit
8096 instantiation directive. */
8097 else if (cp_parser_allow_gnu_extensions_p (parser)
8098 && (token1.keyword == RID_EXTERN
8099 || token1.keyword == RID_STATIC
8100 || token1.keyword == RID_INLINE)
8101 && token2.keyword == RID_TEMPLATE)
8102 cp_parser_explicit_instantiation (parser);
8103 /* If the next token is `namespace', check for a named or unnamed
8104 namespace definition. */
8105 else if (token1.keyword == RID_NAMESPACE
8106 && (/* A named namespace definition. */
8107 (token2.type == CPP_NAME
8108 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8110 /* An unnamed namespace definition. */
8111 || token2.type == CPP_OPEN_BRACE
8112 || token2.keyword == RID_ATTRIBUTE))
8113 cp_parser_namespace_definition (parser);
8114 /* An inline (associated) namespace definition. */
8115 else if (token1.keyword == RID_INLINE
8116 && token2.keyword == RID_NAMESPACE)
8117 cp_parser_namespace_definition (parser);
8118 /* Objective-C++ declaration/definition. */
8119 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8120 cp_parser_objc_declaration (parser);
8121 /* We must have either a block declaration or a function
8124 /* Try to parse a block-declaration, or a function-definition. */
8125 cp_parser_block_declaration (parser, /*statement_p=*/false);
8127 /* Free any declarators allocated. */
8128 obstack_free (&declarator_obstack, p);
8131 /* Parse a block-declaration.
8136 namespace-alias-definition
8143 __extension__ block-declaration
8148 static_assert-declaration
8150 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8151 part of a declaration-statement. */
8154 cp_parser_block_declaration (cp_parser *parser,
8160 /* Check for the `__extension__' keyword. */
8161 if (cp_parser_extension_opt (parser, &saved_pedantic))
8163 /* Parse the qualified declaration. */
8164 cp_parser_block_declaration (parser, statement_p);
8165 /* Restore the PEDANTIC flag. */
8166 pedantic = saved_pedantic;
8171 /* Peek at the next token to figure out which kind of declaration is
8173 token1 = cp_lexer_peek_token (parser->lexer);
8175 /* If the next keyword is `asm', we have an asm-definition. */
8176 if (token1->keyword == RID_ASM)
8179 cp_parser_commit_to_tentative_parse (parser);
8180 cp_parser_asm_definition (parser);
8182 /* If the next keyword is `namespace', we have a
8183 namespace-alias-definition. */
8184 else if (token1->keyword == RID_NAMESPACE)
8185 cp_parser_namespace_alias_definition (parser);
8186 /* If the next keyword is `using', we have either a
8187 using-declaration or a using-directive. */
8188 else if (token1->keyword == RID_USING)
8193 cp_parser_commit_to_tentative_parse (parser);
8194 /* If the token after `using' is `namespace', then we have a
8196 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8197 if (token2->keyword == RID_NAMESPACE)
8198 cp_parser_using_directive (parser);
8199 /* Otherwise, it's a using-declaration. */
8201 cp_parser_using_declaration (parser,
8202 /*access_declaration_p=*/false);
8204 /* If the next keyword is `__label__' we have a misplaced label
8206 else if (token1->keyword == RID_LABEL)
8208 cp_lexer_consume_token (parser->lexer);
8209 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8210 cp_parser_skip_to_end_of_statement (parser);
8211 /* If the next token is now a `;', consume it. */
8212 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8213 cp_lexer_consume_token (parser->lexer);
8215 /* If the next token is `static_assert' we have a static assertion. */
8216 else if (token1->keyword == RID_STATIC_ASSERT)
8217 cp_parser_static_assert (parser, /*member_p=*/false);
8218 /* Anything else must be a simple-declaration. */
8220 cp_parser_simple_declaration (parser, !statement_p);
8223 /* Parse a simple-declaration.
8226 decl-specifier-seq [opt] init-declarator-list [opt] ;
8228 init-declarator-list:
8230 init-declarator-list , init-declarator
8232 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8233 function-definition as a simple-declaration. */
8236 cp_parser_simple_declaration (cp_parser* parser,
8237 bool function_definition_allowed_p)
8239 cp_decl_specifier_seq decl_specifiers;
8240 int declares_class_or_enum;
8241 bool saw_declarator;
8243 /* Defer access checks until we know what is being declared; the
8244 checks for names appearing in the decl-specifier-seq should be
8245 done as if we were in the scope of the thing being declared. */
8246 push_deferring_access_checks (dk_deferred);
8248 /* Parse the decl-specifier-seq. We have to keep track of whether
8249 or not the decl-specifier-seq declares a named class or
8250 enumeration type, since that is the only case in which the
8251 init-declarator-list is allowed to be empty.
8255 In a simple-declaration, the optional init-declarator-list can be
8256 omitted only when declaring a class or enumeration, that is when
8257 the decl-specifier-seq contains either a class-specifier, an
8258 elaborated-type-specifier, or an enum-specifier. */
8259 cp_parser_decl_specifier_seq (parser,
8260 CP_PARSER_FLAGS_OPTIONAL,
8262 &declares_class_or_enum);
8263 /* We no longer need to defer access checks. */
8264 stop_deferring_access_checks ();
8266 /* In a block scope, a valid declaration must always have a
8267 decl-specifier-seq. By not trying to parse declarators, we can
8268 resolve the declaration/expression ambiguity more quickly. */
8269 if (!function_definition_allowed_p
8270 && !decl_specifiers.any_specifiers_p)
8272 cp_parser_error (parser, "expected declaration");
8276 /* If the next two tokens are both identifiers, the code is
8277 erroneous. The usual cause of this situation is code like:
8281 where "T" should name a type -- but does not. */
8282 if (!decl_specifiers.type
8283 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8285 /* If parsing tentatively, we should commit; we really are
8286 looking at a declaration. */
8287 cp_parser_commit_to_tentative_parse (parser);
8292 /* If we have seen at least one decl-specifier, and the next token
8293 is not a parenthesis, then we must be looking at a declaration.
8294 (After "int (" we might be looking at a functional cast.) */
8295 if (decl_specifiers.any_specifiers_p
8296 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8297 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8298 && !cp_parser_error_occurred (parser))
8299 cp_parser_commit_to_tentative_parse (parser);
8301 /* Keep going until we hit the `;' at the end of the simple
8303 saw_declarator = false;
8304 while (cp_lexer_next_token_is_not (parser->lexer,
8308 bool function_definition_p;
8313 /* If we are processing next declarator, coma is expected */
8314 token = cp_lexer_peek_token (parser->lexer);
8315 gcc_assert (token->type == CPP_COMMA);
8316 cp_lexer_consume_token (parser->lexer);
8319 saw_declarator = true;
8321 /* Parse the init-declarator. */
8322 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8324 function_definition_allowed_p,
8326 declares_class_or_enum,
8327 &function_definition_p);
8328 /* If an error occurred while parsing tentatively, exit quickly.
8329 (That usually happens when in the body of a function; each
8330 statement is treated as a declaration-statement until proven
8332 if (cp_parser_error_occurred (parser))
8334 /* Handle function definitions specially. */
8335 if (function_definition_p)
8337 /* If the next token is a `,', then we are probably
8338 processing something like:
8342 which is erroneous. */
8343 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8345 cp_token *token = cp_lexer_peek_token (parser->lexer);
8346 error_at (token->location,
8348 " declarations and function-definitions is forbidden");
8350 /* Otherwise, we're done with the list of declarators. */
8353 pop_deferring_access_checks ();
8357 /* The next token should be either a `,' or a `;'. */
8358 token = cp_lexer_peek_token (parser->lexer);
8359 /* If it's a `,', there are more declarators to come. */
8360 if (token->type == CPP_COMMA)
8361 /* will be consumed next time around */;
8362 /* If it's a `;', we are done. */
8363 else if (token->type == CPP_SEMICOLON)
8365 /* Anything else is an error. */
8368 /* If we have already issued an error message we don't need
8369 to issue another one. */
8370 if (decl != error_mark_node
8371 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8372 cp_parser_error (parser, "expected %<,%> or %<;%>");
8373 /* Skip tokens until we reach the end of the statement. */
8374 cp_parser_skip_to_end_of_statement (parser);
8375 /* If the next token is now a `;', consume it. */
8376 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8377 cp_lexer_consume_token (parser->lexer);
8380 /* After the first time around, a function-definition is not
8381 allowed -- even if it was OK at first. For example:
8386 function_definition_allowed_p = false;
8389 /* Issue an error message if no declarators are present, and the
8390 decl-specifier-seq does not itself declare a class or
8392 if (!saw_declarator)
8394 if (cp_parser_declares_only_class_p (parser))
8395 shadow_tag (&decl_specifiers);
8396 /* Perform any deferred access checks. */
8397 perform_deferred_access_checks ();
8400 /* Consume the `;'. */
8401 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8404 pop_deferring_access_checks ();
8407 /* Parse a decl-specifier-seq.
8410 decl-specifier-seq [opt] decl-specifier
8413 storage-class-specifier
8424 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8426 The parser flags FLAGS is used to control type-specifier parsing.
8428 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8431 1: one of the decl-specifiers is an elaborated-type-specifier
8432 (i.e., a type declaration)
8433 2: one of the decl-specifiers is an enum-specifier or a
8434 class-specifier (i.e., a type definition)
8439 cp_parser_decl_specifier_seq (cp_parser* parser,
8440 cp_parser_flags flags,
8441 cp_decl_specifier_seq *decl_specs,
8442 int* declares_class_or_enum)
8444 bool constructor_possible_p = !parser->in_declarator_p;
8445 cp_token *start_token = NULL;
8447 /* Clear DECL_SPECS. */
8448 clear_decl_specs (decl_specs);
8450 /* Assume no class or enumeration type is declared. */
8451 *declares_class_or_enum = 0;
8453 /* Keep reading specifiers until there are no more to read. */
8457 bool found_decl_spec;
8460 /* Peek at the next token. */
8461 token = cp_lexer_peek_token (parser->lexer);
8463 /* Save the first token of the decl spec list for error
8466 start_token = token;
8467 /* Handle attributes. */
8468 if (token->keyword == RID_ATTRIBUTE)
8470 /* Parse the attributes. */
8471 decl_specs->attributes
8472 = chainon (decl_specs->attributes,
8473 cp_parser_attributes_opt (parser));
8476 /* Assume we will find a decl-specifier keyword. */
8477 found_decl_spec = true;
8478 /* If the next token is an appropriate keyword, we can simply
8479 add it to the list. */
8480 switch (token->keyword)
8485 if (!at_class_scope_p ())
8487 error_at (token->location, "%<friend%> used outside of class");
8488 cp_lexer_purge_token (parser->lexer);
8492 ++decl_specs->specs[(int) ds_friend];
8493 /* Consume the token. */
8494 cp_lexer_consume_token (parser->lexer);
8498 /* function-specifier:
8505 cp_parser_function_specifier_opt (parser, decl_specs);
8511 ++decl_specs->specs[(int) ds_typedef];
8512 /* Consume the token. */
8513 cp_lexer_consume_token (parser->lexer);
8514 /* A constructor declarator cannot appear in a typedef. */
8515 constructor_possible_p = false;
8516 /* The "typedef" keyword can only occur in a declaration; we
8517 may as well commit at this point. */
8518 cp_parser_commit_to_tentative_parse (parser);
8520 if (decl_specs->storage_class != sc_none)
8521 decl_specs->conflicting_specifiers_p = true;
8524 /* storage-class-specifier:
8534 if (cxx_dialect == cxx98)
8536 /* Consume the token. */
8537 cp_lexer_consume_token (parser->lexer);
8539 /* Complain about `auto' as a storage specifier, if
8540 we're complaining about C++0x compatibility. */
8541 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
8542 " will change meaning in C++0x; please remove it");
8544 /* Set the storage class anyway. */
8545 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8549 /* C++0x auto type-specifier. */
8550 found_decl_spec = false;
8557 /* Consume the token. */
8558 cp_lexer_consume_token (parser->lexer);
8559 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8563 /* Consume the token. */
8564 cp_lexer_consume_token (parser->lexer);
8565 ++decl_specs->specs[(int) ds_thread];
8569 /* We did not yet find a decl-specifier yet. */
8570 found_decl_spec = false;
8574 /* Constructors are a special case. The `S' in `S()' is not a
8575 decl-specifier; it is the beginning of the declarator. */
8578 && constructor_possible_p
8579 && (cp_parser_constructor_declarator_p
8580 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8582 /* If we don't have a DECL_SPEC yet, then we must be looking at
8583 a type-specifier. */
8584 if (!found_decl_spec && !constructor_p)
8586 int decl_spec_declares_class_or_enum;
8587 bool is_cv_qualifier;
8591 = cp_parser_type_specifier (parser, flags,
8593 /*is_declaration=*/true,
8594 &decl_spec_declares_class_or_enum,
8596 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8598 /* If this type-specifier referenced a user-defined type
8599 (a typedef, class-name, etc.), then we can't allow any
8600 more such type-specifiers henceforth.
8604 The longest sequence of decl-specifiers that could
8605 possibly be a type name is taken as the
8606 decl-specifier-seq of a declaration. The sequence shall
8607 be self-consistent as described below.
8611 As a general rule, at most one type-specifier is allowed
8612 in the complete decl-specifier-seq of a declaration. The
8613 only exceptions are the following:
8615 -- const or volatile can be combined with any other
8618 -- signed or unsigned can be combined with char, long,
8626 void g (const int Pc);
8628 Here, Pc is *not* part of the decl-specifier seq; it's
8629 the declarator. Therefore, once we see a type-specifier
8630 (other than a cv-qualifier), we forbid any additional
8631 user-defined types. We *do* still allow things like `int
8632 int' to be considered a decl-specifier-seq, and issue the
8633 error message later. */
8634 if (type_spec && !is_cv_qualifier)
8635 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8636 /* A constructor declarator cannot follow a type-specifier. */
8639 constructor_possible_p = false;
8640 found_decl_spec = true;
8644 /* If we still do not have a DECL_SPEC, then there are no more
8646 if (!found_decl_spec)
8649 decl_specs->any_specifiers_p = true;
8650 /* After we see one decl-specifier, further decl-specifiers are
8652 flags |= CP_PARSER_FLAGS_OPTIONAL;
8655 cp_parser_check_decl_spec (decl_specs, start_token->location);
8657 /* Don't allow a friend specifier with a class definition. */
8658 if (decl_specs->specs[(int) ds_friend] != 0
8659 && (*declares_class_or_enum & 2))
8660 error_at (start_token->location,
8661 "class definition may not be declared a friend");
8664 /* Parse an (optional) storage-class-specifier.
8666 storage-class-specifier:
8675 storage-class-specifier:
8678 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8681 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8683 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8686 if (cxx_dialect != cxx98)
8688 /* Fall through for C++98. */
8695 /* Consume the token. */
8696 return cp_lexer_consume_token (parser->lexer)->u.value;
8703 /* Parse an (optional) function-specifier.
8710 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8711 Updates DECL_SPECS, if it is non-NULL. */
8714 cp_parser_function_specifier_opt (cp_parser* parser,
8715 cp_decl_specifier_seq *decl_specs)
8717 cp_token *token = cp_lexer_peek_token (parser->lexer);
8718 switch (token->keyword)
8722 ++decl_specs->specs[(int) ds_inline];
8726 /* 14.5.2.3 [temp.mem]
8728 A member function template shall not be virtual. */
8729 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8730 error_at (token->location, "templates may not be %<virtual%>");
8731 else if (decl_specs)
8732 ++decl_specs->specs[(int) ds_virtual];
8737 ++decl_specs->specs[(int) ds_explicit];
8744 /* Consume the token. */
8745 return cp_lexer_consume_token (parser->lexer)->u.value;
8748 /* Parse a linkage-specification.
8750 linkage-specification:
8751 extern string-literal { declaration-seq [opt] }
8752 extern string-literal declaration */
8755 cp_parser_linkage_specification (cp_parser* parser)
8759 /* Look for the `extern' keyword. */
8760 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8762 /* Look for the string-literal. */
8763 linkage = cp_parser_string_literal (parser, false, false);
8765 /* Transform the literal into an identifier. If the literal is a
8766 wide-character string, or contains embedded NULs, then we can't
8767 handle it as the user wants. */
8768 if (strlen (TREE_STRING_POINTER (linkage))
8769 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8771 cp_parser_error (parser, "invalid linkage-specification");
8772 /* Assume C++ linkage. */
8773 linkage = lang_name_cplusplus;
8776 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8778 /* We're now using the new linkage. */
8779 push_lang_context (linkage);
8781 /* If the next token is a `{', then we're using the first
8783 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8785 /* Consume the `{' token. */
8786 cp_lexer_consume_token (parser->lexer);
8787 /* Parse the declarations. */
8788 cp_parser_declaration_seq_opt (parser);
8789 /* Look for the closing `}'. */
8790 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8792 /* Otherwise, there's just one declaration. */
8795 bool saved_in_unbraced_linkage_specification_p;
8797 saved_in_unbraced_linkage_specification_p
8798 = parser->in_unbraced_linkage_specification_p;
8799 parser->in_unbraced_linkage_specification_p = true;
8800 cp_parser_declaration (parser);
8801 parser->in_unbraced_linkage_specification_p
8802 = saved_in_unbraced_linkage_specification_p;
8805 /* We're done with the linkage-specification. */
8806 pop_lang_context ();
8809 /* Parse a static_assert-declaration.
8811 static_assert-declaration:
8812 static_assert ( constant-expression , string-literal ) ;
8814 If MEMBER_P, this static_assert is a class member. */
8817 cp_parser_static_assert(cp_parser *parser, bool member_p)
8822 location_t saved_loc;
8824 /* Peek at the `static_assert' token so we can keep track of exactly
8825 where the static assertion started. */
8826 token = cp_lexer_peek_token (parser->lexer);
8827 saved_loc = token->location;
8829 /* Look for the `static_assert' keyword. */
8830 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8831 "%<static_assert%>"))
8834 /* We know we are in a static assertion; commit to any tentative
8836 if (cp_parser_parsing_tentatively (parser))
8837 cp_parser_commit_to_tentative_parse (parser);
8839 /* Parse the `(' starting the static assertion condition. */
8840 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8842 /* Parse the constant-expression. */
8844 cp_parser_constant_expression (parser,
8845 /*allow_non_constant_p=*/false,
8846 /*non_constant_p=*/NULL);
8848 /* Parse the separating `,'. */
8849 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8851 /* Parse the string-literal message. */
8852 message = cp_parser_string_literal (parser,
8853 /*translate=*/false,
8856 /* A `)' completes the static assertion. */
8857 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8858 cp_parser_skip_to_closing_parenthesis (parser,
8859 /*recovering=*/true,
8861 /*consume_paren=*/true);
8863 /* A semicolon terminates the declaration. */
8864 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8866 /* Complete the static assertion, which may mean either processing
8867 the static assert now or saving it for template instantiation. */
8868 finish_static_assert (condition, message, saved_loc, member_p);
8871 /* Parse a `decltype' type. Returns the type.
8873 simple-type-specifier:
8874 decltype ( expression ) */
8877 cp_parser_decltype (cp_parser *parser)
8880 bool id_expression_or_member_access_p = false;
8881 const char *saved_message;
8882 bool saved_integral_constant_expression_p;
8883 bool saved_non_integral_constant_expression_p;
8884 cp_token *id_expr_start_token;
8886 /* Look for the `decltype' token. */
8887 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8888 return error_mark_node;
8890 /* Types cannot be defined in a `decltype' expression. Save away the
8892 saved_message = parser->type_definition_forbidden_message;
8894 /* And create the new one. */
8895 parser->type_definition_forbidden_message
8896 = "types may not be defined in %<decltype%> expressions";
8898 /* The restrictions on constant-expressions do not apply inside
8899 decltype expressions. */
8900 saved_integral_constant_expression_p
8901 = parser->integral_constant_expression_p;
8902 saved_non_integral_constant_expression_p
8903 = parser->non_integral_constant_expression_p;
8904 parser->integral_constant_expression_p = false;
8906 /* Do not actually evaluate the expression. */
8907 ++cp_unevaluated_operand;
8909 /* Do not warn about problems with the expression. */
8910 ++c_inhibit_evaluation_warnings;
8912 /* Parse the opening `('. */
8913 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8914 return error_mark_node;
8916 /* First, try parsing an id-expression. */
8917 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8918 cp_parser_parse_tentatively (parser);
8919 expr = cp_parser_id_expression (parser,
8920 /*template_keyword_p=*/false,
8921 /*check_dependency_p=*/true,
8922 /*template_p=*/NULL,
8923 /*declarator_p=*/false,
8924 /*optional_p=*/false);
8926 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8928 bool non_integral_constant_expression_p = false;
8929 tree id_expression = expr;
8931 const char *error_msg;
8933 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8934 /* Lookup the name we got back from the id-expression. */
8935 expr = cp_parser_lookup_name (parser, expr,
8937 /*is_template=*/false,
8938 /*is_namespace=*/false,
8939 /*check_dependency=*/true,
8940 /*ambiguous_decls=*/NULL,
8941 id_expr_start_token->location);
8944 && expr != error_mark_node
8945 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8946 && TREE_CODE (expr) != TYPE_DECL
8947 && (TREE_CODE (expr) != BIT_NOT_EXPR
8948 || !TYPE_P (TREE_OPERAND (expr, 0)))
8949 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8951 /* Complete lookup of the id-expression. */
8952 expr = (finish_id_expression
8953 (id_expression, expr, parser->scope, &idk,
8954 /*integral_constant_expression_p=*/false,
8955 /*allow_non_integral_constant_expression_p=*/true,
8956 &non_integral_constant_expression_p,
8957 /*template_p=*/false,
8959 /*address_p=*/false,
8960 /*template_arg_p=*/false,
8962 id_expr_start_token->location));
8964 if (expr == error_mark_node)
8965 /* We found an id-expression, but it was something that we
8966 should not have found. This is an error, not something
8967 we can recover from, so note that we found an
8968 id-expression and we'll recover as gracefully as
8970 id_expression_or_member_access_p = true;
8974 && expr != error_mark_node
8975 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8976 /* We have an id-expression. */
8977 id_expression_or_member_access_p = true;
8980 if (!id_expression_or_member_access_p)
8982 /* Abort the id-expression parse. */
8983 cp_parser_abort_tentative_parse (parser);
8985 /* Parsing tentatively, again. */
8986 cp_parser_parse_tentatively (parser);
8988 /* Parse a class member access. */
8989 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8991 /*member_access_only_p=*/true, NULL);
8994 && expr != error_mark_node
8995 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8996 /* We have an id-expression. */
8997 id_expression_or_member_access_p = true;
9000 if (id_expression_or_member_access_p)
9001 /* We have parsed the complete id-expression or member access. */
9002 cp_parser_parse_definitely (parser);
9005 /* Abort our attempt to parse an id-expression or member access
9007 cp_parser_abort_tentative_parse (parser);
9009 /* Parse a full expression. */
9010 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9013 /* Go back to evaluating expressions. */
9014 --cp_unevaluated_operand;
9015 --c_inhibit_evaluation_warnings;
9017 /* Restore the old message and the integral constant expression
9019 parser->type_definition_forbidden_message = saved_message;
9020 parser->integral_constant_expression_p
9021 = saved_integral_constant_expression_p;
9022 parser->non_integral_constant_expression_p
9023 = saved_non_integral_constant_expression_p;
9025 if (expr == error_mark_node)
9027 /* Skip everything up to the closing `)'. */
9028 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9029 /*consume_paren=*/true);
9030 return error_mark_node;
9033 /* Parse to the closing `)'. */
9034 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9036 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9037 /*consume_paren=*/true);
9038 return error_mark_node;
9041 return finish_decltype_type (expr, id_expression_or_member_access_p);
9044 /* Special member functions [gram.special] */
9046 /* Parse a conversion-function-id.
9048 conversion-function-id:
9049 operator conversion-type-id
9051 Returns an IDENTIFIER_NODE representing the operator. */
9054 cp_parser_conversion_function_id (cp_parser* parser)
9058 tree saved_qualifying_scope;
9059 tree saved_object_scope;
9060 tree pushed_scope = NULL_TREE;
9062 /* Look for the `operator' token. */
9063 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9064 return error_mark_node;
9065 /* When we parse the conversion-type-id, the current scope will be
9066 reset. However, we need that information in able to look up the
9067 conversion function later, so we save it here. */
9068 saved_scope = parser->scope;
9069 saved_qualifying_scope = parser->qualifying_scope;
9070 saved_object_scope = parser->object_scope;
9071 /* We must enter the scope of the class so that the names of
9072 entities declared within the class are available in the
9073 conversion-type-id. For example, consider:
9080 S::operator I() { ... }
9082 In order to see that `I' is a type-name in the definition, we
9083 must be in the scope of `S'. */
9085 pushed_scope = push_scope (saved_scope);
9086 /* Parse the conversion-type-id. */
9087 type = cp_parser_conversion_type_id (parser);
9088 /* Leave the scope of the class, if any. */
9090 pop_scope (pushed_scope);
9091 /* Restore the saved scope. */
9092 parser->scope = saved_scope;
9093 parser->qualifying_scope = saved_qualifying_scope;
9094 parser->object_scope = saved_object_scope;
9095 /* If the TYPE is invalid, indicate failure. */
9096 if (type == error_mark_node)
9097 return error_mark_node;
9098 return mangle_conv_op_name_for_type (type);
9101 /* Parse a conversion-type-id:
9104 type-specifier-seq conversion-declarator [opt]
9106 Returns the TYPE specified. */
9109 cp_parser_conversion_type_id (cp_parser* parser)
9112 cp_decl_specifier_seq type_specifiers;
9113 cp_declarator *declarator;
9114 tree type_specified;
9116 /* Parse the attributes. */
9117 attributes = cp_parser_attributes_opt (parser);
9118 /* Parse the type-specifiers. */
9119 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9121 /* If that didn't work, stop. */
9122 if (type_specifiers.type == error_mark_node)
9123 return error_mark_node;
9124 /* Parse the conversion-declarator. */
9125 declarator = cp_parser_conversion_declarator_opt (parser);
9127 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9128 /*initialized=*/0, &attributes);
9130 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9132 /* Don't give this error when parsing tentatively. This happens to
9133 work because we always parse this definitively once. */
9134 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9135 && type_uses_auto (type_specified))
9137 error ("invalid use of %<auto%> in conversion operator");
9138 return error_mark_node;
9141 return type_specified;
9144 /* Parse an (optional) conversion-declarator.
9146 conversion-declarator:
9147 ptr-operator conversion-declarator [opt]
9151 static cp_declarator *
9152 cp_parser_conversion_declarator_opt (cp_parser* parser)
9154 enum tree_code code;
9156 cp_cv_quals cv_quals;
9158 /* We don't know if there's a ptr-operator next, or not. */
9159 cp_parser_parse_tentatively (parser);
9160 /* Try the ptr-operator. */
9161 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9162 /* If it worked, look for more conversion-declarators. */
9163 if (cp_parser_parse_definitely (parser))
9165 cp_declarator *declarator;
9167 /* Parse another optional declarator. */
9168 declarator = cp_parser_conversion_declarator_opt (parser);
9170 return cp_parser_make_indirect_declarator
9171 (code, class_type, cv_quals, declarator);
9177 /* Parse an (optional) ctor-initializer.
9180 : mem-initializer-list
9182 Returns TRUE iff the ctor-initializer was actually present. */
9185 cp_parser_ctor_initializer_opt (cp_parser* parser)
9187 /* If the next token is not a `:', then there is no
9188 ctor-initializer. */
9189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9191 /* Do default initialization of any bases and members. */
9192 if (DECL_CONSTRUCTOR_P (current_function_decl))
9193 finish_mem_initializers (NULL_TREE);
9198 /* Consume the `:' token. */
9199 cp_lexer_consume_token (parser->lexer);
9200 /* And the mem-initializer-list. */
9201 cp_parser_mem_initializer_list (parser);
9206 /* Parse a mem-initializer-list.
9208 mem-initializer-list:
9209 mem-initializer ... [opt]
9210 mem-initializer ... [opt] , mem-initializer-list */
9213 cp_parser_mem_initializer_list (cp_parser* parser)
9215 tree mem_initializer_list = NULL_TREE;
9216 cp_token *token = cp_lexer_peek_token (parser->lexer);
9218 /* Let the semantic analysis code know that we are starting the
9219 mem-initializer-list. */
9220 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9221 error_at (token->location,
9222 "only constructors take base initializers");
9224 /* Loop through the list. */
9227 tree mem_initializer;
9229 token = cp_lexer_peek_token (parser->lexer);
9230 /* Parse the mem-initializer. */
9231 mem_initializer = cp_parser_mem_initializer (parser);
9232 /* If the next token is a `...', we're expanding member initializers. */
9233 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9235 /* Consume the `...'. */
9236 cp_lexer_consume_token (parser->lexer);
9238 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9239 can be expanded but members cannot. */
9240 if (mem_initializer != error_mark_node
9241 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9243 error_at (token->location,
9244 "cannot expand initializer for member %<%D%>",
9245 TREE_PURPOSE (mem_initializer));
9246 mem_initializer = error_mark_node;
9249 /* Construct the pack expansion type. */
9250 if (mem_initializer != error_mark_node)
9251 mem_initializer = make_pack_expansion (mem_initializer);
9253 /* Add it to the list, unless it was erroneous. */
9254 if (mem_initializer != error_mark_node)
9256 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9257 mem_initializer_list = mem_initializer;
9259 /* If the next token is not a `,', we're done. */
9260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9262 /* Consume the `,' token. */
9263 cp_lexer_consume_token (parser->lexer);
9266 /* Perform semantic analysis. */
9267 if (DECL_CONSTRUCTOR_P (current_function_decl))
9268 finish_mem_initializers (mem_initializer_list);
9271 /* Parse a mem-initializer.
9274 mem-initializer-id ( expression-list [opt] )
9275 mem-initializer-id braced-init-list
9280 ( expression-list [opt] )
9282 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9283 class) or FIELD_DECL (for a non-static data member) to initialize;
9284 the TREE_VALUE is the expression-list. An empty initialization
9285 list is represented by void_list_node. */
9288 cp_parser_mem_initializer (cp_parser* parser)
9290 tree mem_initializer_id;
9291 tree expression_list;
9293 cp_token *token = cp_lexer_peek_token (parser->lexer);
9295 /* Find out what is being initialized. */
9296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9298 permerror (token->location,
9299 "anachronistic old-style base class initializer");
9300 mem_initializer_id = NULL_TREE;
9304 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9305 if (mem_initializer_id == error_mark_node)
9306 return mem_initializer_id;
9308 member = expand_member_init (mem_initializer_id);
9309 if (member && !DECL_P (member))
9310 in_base_initializer = 1;
9312 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9314 bool expr_non_constant_p;
9315 maybe_warn_cpp0x ("extended initializer lists");
9316 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9317 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9318 expression_list = build_tree_list (NULL_TREE, expression_list);
9323 vec = cp_parser_parenthesized_expression_list (parser, false,
9325 /*allow_expansion_p=*/true,
9326 /*non_constant_p=*/NULL);
9328 return error_mark_node;
9329 expression_list = build_tree_list_vec (vec);
9330 release_tree_vector (vec);
9333 if (expression_list == error_mark_node)
9334 return error_mark_node;
9335 if (!expression_list)
9336 expression_list = void_type_node;
9338 in_base_initializer = 0;
9340 return member ? build_tree_list (member, expression_list) : error_mark_node;
9343 /* Parse a mem-initializer-id.
9346 :: [opt] nested-name-specifier [opt] class-name
9349 Returns a TYPE indicating the class to be initializer for the first
9350 production. Returns an IDENTIFIER_NODE indicating the data member
9351 to be initialized for the second production. */
9354 cp_parser_mem_initializer_id (cp_parser* parser)
9356 bool global_scope_p;
9357 bool nested_name_specifier_p;
9358 bool template_p = false;
9361 cp_token *token = cp_lexer_peek_token (parser->lexer);
9363 /* `typename' is not allowed in this context ([temp.res]). */
9364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9366 error_at (token->location,
9367 "keyword %<typename%> not allowed in this context (a qualified "
9368 "member initializer is implicitly a type)");
9369 cp_lexer_consume_token (parser->lexer);
9371 /* Look for the optional `::' operator. */
9373 = (cp_parser_global_scope_opt (parser,
9374 /*current_scope_valid_p=*/false)
9376 /* Look for the optional nested-name-specifier. The simplest way to
9381 The keyword `typename' is not permitted in a base-specifier or
9382 mem-initializer; in these contexts a qualified name that
9383 depends on a template-parameter is implicitly assumed to be a
9386 is to assume that we have seen the `typename' keyword at this
9388 nested_name_specifier_p
9389 = (cp_parser_nested_name_specifier_opt (parser,
9390 /*typename_keyword_p=*/true,
9391 /*check_dependency_p=*/true,
9393 /*is_declaration=*/true)
9395 if (nested_name_specifier_p)
9396 template_p = cp_parser_optional_template_keyword (parser);
9397 /* If there is a `::' operator or a nested-name-specifier, then we
9398 are definitely looking for a class-name. */
9399 if (global_scope_p || nested_name_specifier_p)
9400 return cp_parser_class_name (parser,
9401 /*typename_keyword_p=*/true,
9402 /*template_keyword_p=*/template_p,
9404 /*check_dependency_p=*/true,
9405 /*class_head_p=*/false,
9406 /*is_declaration=*/true);
9407 /* Otherwise, we could also be looking for an ordinary identifier. */
9408 cp_parser_parse_tentatively (parser);
9409 /* Try a class-name. */
9410 id = cp_parser_class_name (parser,
9411 /*typename_keyword_p=*/true,
9412 /*template_keyword_p=*/false,
9414 /*check_dependency_p=*/true,
9415 /*class_head_p=*/false,
9416 /*is_declaration=*/true);
9417 /* If we found one, we're done. */
9418 if (cp_parser_parse_definitely (parser))
9420 /* Otherwise, look for an ordinary identifier. */
9421 return cp_parser_identifier (parser);
9424 /* Overloading [gram.over] */
9426 /* Parse an operator-function-id.
9428 operator-function-id:
9431 Returns an IDENTIFIER_NODE for the operator which is a
9432 human-readable spelling of the identifier, e.g., `operator +'. */
9435 cp_parser_operator_function_id (cp_parser* parser)
9437 /* Look for the `operator' keyword. */
9438 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9439 return error_mark_node;
9440 /* And then the name of the operator itself. */
9441 return cp_parser_operator (parser);
9444 /* Parse an operator.
9447 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9448 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9449 || ++ -- , ->* -> () []
9456 Returns an IDENTIFIER_NODE for the operator which is a
9457 human-readable spelling of the identifier, e.g., `operator +'. */
9460 cp_parser_operator (cp_parser* parser)
9462 tree id = NULL_TREE;
9465 /* Peek at the next token. */
9466 token = cp_lexer_peek_token (parser->lexer);
9467 /* Figure out which operator we have. */
9468 switch (token->type)
9474 /* The keyword should be either `new' or `delete'. */
9475 if (token->keyword == RID_NEW)
9477 else if (token->keyword == RID_DELETE)
9482 /* Consume the `new' or `delete' token. */
9483 cp_lexer_consume_token (parser->lexer);
9485 /* Peek at the next token. */
9486 token = cp_lexer_peek_token (parser->lexer);
9487 /* If it's a `[' token then this is the array variant of the
9489 if (token->type == CPP_OPEN_SQUARE)
9491 /* Consume the `[' token. */
9492 cp_lexer_consume_token (parser->lexer);
9493 /* Look for the `]' token. */
9494 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9495 id = ansi_opname (op == NEW_EXPR
9496 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9498 /* Otherwise, we have the non-array variant. */
9500 id = ansi_opname (op);
9506 id = ansi_opname (PLUS_EXPR);
9510 id = ansi_opname (MINUS_EXPR);
9514 id = ansi_opname (MULT_EXPR);
9518 id = ansi_opname (TRUNC_DIV_EXPR);
9522 id = ansi_opname (TRUNC_MOD_EXPR);
9526 id = ansi_opname (BIT_XOR_EXPR);
9530 id = ansi_opname (BIT_AND_EXPR);
9534 id = ansi_opname (BIT_IOR_EXPR);
9538 id = ansi_opname (BIT_NOT_EXPR);
9542 id = ansi_opname (TRUTH_NOT_EXPR);
9546 id = ansi_assopname (NOP_EXPR);
9550 id = ansi_opname (LT_EXPR);
9554 id = ansi_opname (GT_EXPR);
9558 id = ansi_assopname (PLUS_EXPR);
9562 id = ansi_assopname (MINUS_EXPR);
9566 id = ansi_assopname (MULT_EXPR);
9570 id = ansi_assopname (TRUNC_DIV_EXPR);
9574 id = ansi_assopname (TRUNC_MOD_EXPR);
9578 id = ansi_assopname (BIT_XOR_EXPR);
9582 id = ansi_assopname (BIT_AND_EXPR);
9586 id = ansi_assopname (BIT_IOR_EXPR);
9590 id = ansi_opname (LSHIFT_EXPR);
9594 id = ansi_opname (RSHIFT_EXPR);
9598 id = ansi_assopname (LSHIFT_EXPR);
9602 id = ansi_assopname (RSHIFT_EXPR);
9606 id = ansi_opname (EQ_EXPR);
9610 id = ansi_opname (NE_EXPR);
9614 id = ansi_opname (LE_EXPR);
9617 case CPP_GREATER_EQ:
9618 id = ansi_opname (GE_EXPR);
9622 id = ansi_opname (TRUTH_ANDIF_EXPR);
9626 id = ansi_opname (TRUTH_ORIF_EXPR);
9630 id = ansi_opname (POSTINCREMENT_EXPR);
9633 case CPP_MINUS_MINUS:
9634 id = ansi_opname (PREDECREMENT_EXPR);
9638 id = ansi_opname (COMPOUND_EXPR);
9641 case CPP_DEREF_STAR:
9642 id = ansi_opname (MEMBER_REF);
9646 id = ansi_opname (COMPONENT_REF);
9649 case CPP_OPEN_PAREN:
9650 /* Consume the `('. */
9651 cp_lexer_consume_token (parser->lexer);
9652 /* Look for the matching `)'. */
9653 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9654 return ansi_opname (CALL_EXPR);
9656 case CPP_OPEN_SQUARE:
9657 /* Consume the `['. */
9658 cp_lexer_consume_token (parser->lexer);
9659 /* Look for the matching `]'. */
9660 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9661 return ansi_opname (ARRAY_REF);
9664 /* Anything else is an error. */
9668 /* If we have selected an identifier, we need to consume the
9671 cp_lexer_consume_token (parser->lexer);
9672 /* Otherwise, no valid operator name was present. */
9675 cp_parser_error (parser, "expected operator");
9676 id = error_mark_node;
9682 /* Parse a template-declaration.
9684 template-declaration:
9685 export [opt] template < template-parameter-list > declaration
9687 If MEMBER_P is TRUE, this template-declaration occurs within a
9690 The grammar rule given by the standard isn't correct. What
9693 template-declaration:
9694 export [opt] template-parameter-list-seq
9695 decl-specifier-seq [opt] init-declarator [opt] ;
9696 export [opt] template-parameter-list-seq
9699 template-parameter-list-seq:
9700 template-parameter-list-seq [opt]
9701 template < template-parameter-list > */
9704 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9706 /* Check for `export'. */
9707 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9709 /* Consume the `export' token. */
9710 cp_lexer_consume_token (parser->lexer);
9711 /* Warn that we do not support `export'. */
9712 warning (0, "keyword %<export%> not implemented, and will be ignored");
9715 cp_parser_template_declaration_after_export (parser, member_p);
9718 /* Parse a template-parameter-list.
9720 template-parameter-list:
9722 template-parameter-list , template-parameter
9724 Returns a TREE_LIST. Each node represents a template parameter.
9725 The nodes are connected via their TREE_CHAINs. */
9728 cp_parser_template_parameter_list (cp_parser* parser)
9730 tree parameter_list = NULL_TREE;
9732 begin_template_parm_list ();
9737 bool is_parameter_pack;
9738 location_t parm_loc;
9740 /* Parse the template-parameter. */
9741 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
9742 parameter = cp_parser_template_parameter (parser,
9744 &is_parameter_pack);
9745 /* Add it to the list. */
9746 if (parameter != error_mark_node)
9747 parameter_list = process_template_parm (parameter_list,
9754 tree err_parm = build_tree_list (parameter, parameter);
9755 TREE_VALUE (err_parm) = error_mark_node;
9756 parameter_list = chainon (parameter_list, err_parm);
9759 /* If the next token is not a `,', we're done. */
9760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9762 /* Otherwise, consume the `,' token. */
9763 cp_lexer_consume_token (parser->lexer);
9766 return end_template_parm_list (parameter_list);
9769 /* Parse a template-parameter.
9773 parameter-declaration
9775 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9776 the parameter. The TREE_PURPOSE is the default value, if any.
9777 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9778 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9779 set to true iff this parameter is a parameter pack. */
9782 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9783 bool *is_parameter_pack)
9786 cp_parameter_declarator *parameter_declarator;
9787 cp_declarator *id_declarator;
9790 /* Assume it is a type parameter or a template parameter. */
9791 *is_non_type = false;
9792 /* Assume it not a parameter pack. */
9793 *is_parameter_pack = false;
9794 /* Peek at the next token. */
9795 token = cp_lexer_peek_token (parser->lexer);
9796 /* If it is `class' or `template', we have a type-parameter. */
9797 if (token->keyword == RID_TEMPLATE)
9798 return cp_parser_type_parameter (parser, is_parameter_pack);
9799 /* If it is `class' or `typename' we do not know yet whether it is a
9800 type parameter or a non-type parameter. Consider:
9802 template <typename T, typename T::X X> ...
9806 template <class C, class D*> ...
9808 Here, the first parameter is a type parameter, and the second is
9809 a non-type parameter. We can tell by looking at the token after
9810 the identifier -- if it is a `,', `=', or `>' then we have a type
9812 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9814 /* Peek at the token after `class' or `typename'. */
9815 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9816 /* If it's an ellipsis, we have a template type parameter
9818 if (token->type == CPP_ELLIPSIS)
9819 return cp_parser_type_parameter (parser, is_parameter_pack);
9820 /* If it's an identifier, skip it. */
9821 if (token->type == CPP_NAME)
9822 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9823 /* Now, see if the token looks like the end of a template
9825 if (token->type == CPP_COMMA
9826 || token->type == CPP_EQ
9827 || token->type == CPP_GREATER)
9828 return cp_parser_type_parameter (parser, is_parameter_pack);
9831 /* Otherwise, it is a non-type parameter.
9835 When parsing a default template-argument for a non-type
9836 template-parameter, the first non-nested `>' is taken as the end
9837 of the template parameter-list rather than a greater-than
9839 *is_non_type = true;
9840 parameter_declarator
9841 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9842 /*parenthesized_p=*/NULL);
9844 /* If the parameter declaration is marked as a parameter pack, set
9845 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9846 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9848 if (parameter_declarator
9849 && parameter_declarator->declarator
9850 && parameter_declarator->declarator->parameter_pack_p)
9852 *is_parameter_pack = true;
9853 parameter_declarator->declarator->parameter_pack_p = false;
9856 /* If the next token is an ellipsis, and we don't already have it
9857 marked as a parameter pack, then we have a parameter pack (that
9858 has no declarator). */
9859 if (!*is_parameter_pack
9860 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9861 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9863 /* Consume the `...'. */
9864 cp_lexer_consume_token (parser->lexer);
9865 maybe_warn_variadic_templates ();
9867 *is_parameter_pack = true;
9869 /* We might end up with a pack expansion as the type of the non-type
9870 template parameter, in which case this is a non-type template
9872 else if (parameter_declarator
9873 && parameter_declarator->decl_specifiers.type
9874 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9876 *is_parameter_pack = true;
9877 parameter_declarator->decl_specifiers.type =
9878 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9881 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9883 /* Parameter packs cannot have default arguments. However, a
9884 user may try to do so, so we'll parse them and give an
9885 appropriate diagnostic here. */
9887 /* Consume the `='. */
9888 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9889 cp_lexer_consume_token (parser->lexer);
9891 /* Find the name of the parameter pack. */
9892 id_declarator = parameter_declarator->declarator;
9893 while (id_declarator && id_declarator->kind != cdk_id)
9894 id_declarator = id_declarator->declarator;
9896 if (id_declarator && id_declarator->kind == cdk_id)
9897 error_at (start_token->location,
9898 "template parameter pack %qD cannot have a default argument",
9899 id_declarator->u.id.unqualified_name);
9901 error_at (start_token->location,
9902 "template parameter pack cannot have a default argument");
9904 /* Parse the default argument, but throw away the result. */
9905 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9908 parm = grokdeclarator (parameter_declarator->declarator,
9909 ¶meter_declarator->decl_specifiers,
9910 PARM, /*initialized=*/0,
9912 if (parm == error_mark_node)
9913 return error_mark_node;
9915 return build_tree_list (parameter_declarator->default_argument, parm);
9918 /* Parse a type-parameter.
9921 class identifier [opt]
9922 class identifier [opt] = type-id
9923 typename identifier [opt]
9924 typename identifier [opt] = type-id
9925 template < template-parameter-list > class identifier [opt]
9926 template < template-parameter-list > class identifier [opt]
9929 GNU Extension (variadic templates):
9932 class ... identifier [opt]
9933 typename ... identifier [opt]
9935 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9936 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9937 the declaration of the parameter.
9939 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9942 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9947 /* Look for a keyword to tell us what kind of parameter this is. */
9948 token = cp_parser_require (parser, CPP_KEYWORD,
9949 "%<class%>, %<typename%>, or %<template%>");
9951 return error_mark_node;
9953 switch (token->keyword)
9959 tree default_argument;
9961 /* If the next token is an ellipsis, we have a template
9963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9965 /* Consume the `...' token. */
9966 cp_lexer_consume_token (parser->lexer);
9967 maybe_warn_variadic_templates ();
9969 *is_parameter_pack = true;
9972 /* If the next token is an identifier, then it names the
9974 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9975 identifier = cp_parser_identifier (parser);
9977 identifier = NULL_TREE;
9979 /* Create the parameter. */
9980 parameter = finish_template_type_parm (class_type_node, identifier);
9982 /* If the next token is an `=', we have a default argument. */
9983 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9985 /* Consume the `=' token. */
9986 cp_lexer_consume_token (parser->lexer);
9987 /* Parse the default-argument. */
9988 push_deferring_access_checks (dk_no_deferred);
9989 default_argument = cp_parser_type_id (parser);
9991 /* Template parameter packs cannot have default
9993 if (*is_parameter_pack)
9996 error_at (token->location,
9997 "template parameter pack %qD cannot have a "
9998 "default argument", identifier);
10000 error_at (token->location,
10001 "template parameter packs cannot have "
10002 "default arguments");
10003 default_argument = NULL_TREE;
10005 pop_deferring_access_checks ();
10008 default_argument = NULL_TREE;
10010 /* Create the combined representation of the parameter and the
10011 default argument. */
10012 parameter = build_tree_list (default_argument, parameter);
10018 tree parameter_list;
10020 tree default_argument;
10022 /* Look for the `<'. */
10023 cp_parser_require (parser, CPP_LESS, "%<<%>");
10024 /* Parse the template-parameter-list. */
10025 parameter_list = cp_parser_template_parameter_list (parser);
10026 /* Look for the `>'. */
10027 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10028 /* Look for the `class' keyword. */
10029 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10030 /* If the next token is an ellipsis, we have a template
10032 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10034 /* Consume the `...' token. */
10035 cp_lexer_consume_token (parser->lexer);
10036 maybe_warn_variadic_templates ();
10038 *is_parameter_pack = true;
10040 /* If the next token is an `=', then there is a
10041 default-argument. If the next token is a `>', we are at
10042 the end of the parameter-list. If the next token is a `,',
10043 then we are at the end of this parameter. */
10044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10045 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10046 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10048 identifier = cp_parser_identifier (parser);
10049 /* Treat invalid names as if the parameter were nameless. */
10050 if (identifier == error_mark_node)
10051 identifier = NULL_TREE;
10054 identifier = NULL_TREE;
10056 /* Create the template parameter. */
10057 parameter = finish_template_template_parm (class_type_node,
10060 /* If the next token is an `=', then there is a
10061 default-argument. */
10062 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10066 /* Consume the `='. */
10067 cp_lexer_consume_token (parser->lexer);
10068 /* Parse the id-expression. */
10069 push_deferring_access_checks (dk_no_deferred);
10070 /* save token before parsing the id-expression, for error
10072 token = cp_lexer_peek_token (parser->lexer);
10074 = cp_parser_id_expression (parser,
10075 /*template_keyword_p=*/false,
10076 /*check_dependency_p=*/true,
10077 /*template_p=*/&is_template,
10078 /*declarator_p=*/false,
10079 /*optional_p=*/false);
10080 if (TREE_CODE (default_argument) == TYPE_DECL)
10081 /* If the id-expression was a template-id that refers to
10082 a template-class, we already have the declaration here,
10083 so no further lookup is needed. */
10086 /* Look up the name. */
10088 = cp_parser_lookup_name (parser, default_argument,
10090 /*is_template=*/is_template,
10091 /*is_namespace=*/false,
10092 /*check_dependency=*/true,
10093 /*ambiguous_decls=*/NULL,
10095 /* See if the default argument is valid. */
10097 = check_template_template_default_arg (default_argument);
10099 /* Template parameter packs cannot have default
10101 if (*is_parameter_pack)
10104 error_at (token->location,
10105 "template parameter pack %qD cannot "
10106 "have a default argument",
10109 error_at (token->location, "template parameter packs cannot "
10110 "have default arguments");
10111 default_argument = NULL_TREE;
10113 pop_deferring_access_checks ();
10116 default_argument = NULL_TREE;
10118 /* Create the combined representation of the parameter and the
10119 default argument. */
10120 parameter = build_tree_list (default_argument, parameter);
10125 gcc_unreachable ();
10132 /* Parse a template-id.
10135 template-name < template-argument-list [opt] >
10137 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10138 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10139 returned. Otherwise, if the template-name names a function, or set
10140 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10141 names a class, returns a TYPE_DECL for the specialization.
10143 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10144 uninstantiated templates. */
10147 cp_parser_template_id (cp_parser *parser,
10148 bool template_keyword_p,
10149 bool check_dependency_p,
10150 bool is_declaration)
10156 cp_token_position start_of_id = 0;
10157 deferred_access_check *chk;
10158 VEC (deferred_access_check,gc) *access_check;
10159 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10160 bool is_identifier;
10162 /* If the next token corresponds to a template-id, there is no need
10164 next_token = cp_lexer_peek_token (parser->lexer);
10165 if (next_token->type == CPP_TEMPLATE_ID)
10167 struct tree_check *check_value;
10169 /* Get the stored value. */
10170 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10171 /* Perform any access checks that were deferred. */
10172 access_check = check_value->checks;
10176 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10179 perform_or_defer_access_check (chk->binfo,
10184 /* Return the stored value. */
10185 return check_value->value;
10188 /* Avoid performing name lookup if there is no possibility of
10189 finding a template-id. */
10190 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10191 || (next_token->type == CPP_NAME
10192 && !cp_parser_nth_token_starts_template_argument_list_p
10195 cp_parser_error (parser, "expected template-id");
10196 return error_mark_node;
10199 /* Remember where the template-id starts. */
10200 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10201 start_of_id = cp_lexer_token_position (parser->lexer, false);
10203 push_deferring_access_checks (dk_deferred);
10205 /* Parse the template-name. */
10206 is_identifier = false;
10207 token = cp_lexer_peek_token (parser->lexer);
10208 templ = cp_parser_template_name (parser, template_keyword_p,
10209 check_dependency_p,
10212 if (templ == error_mark_node || is_identifier)
10214 pop_deferring_access_checks ();
10218 /* If we find the sequence `[:' after a template-name, it's probably
10219 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10220 parse correctly the argument list. */
10221 next_token = cp_lexer_peek_token (parser->lexer);
10222 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10223 if (next_token->type == CPP_OPEN_SQUARE
10224 && next_token->flags & DIGRAPH
10225 && next_token_2->type == CPP_COLON
10226 && !(next_token_2->flags & PREV_WHITE))
10228 cp_parser_parse_tentatively (parser);
10229 /* Change `:' into `::'. */
10230 next_token_2->type = CPP_SCOPE;
10231 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10233 cp_lexer_consume_token (parser->lexer);
10235 /* Parse the arguments. */
10236 arguments = cp_parser_enclosed_template_argument_list (parser);
10237 if (!cp_parser_parse_definitely (parser))
10239 /* If we couldn't parse an argument list, then we revert our changes
10240 and return simply an error. Maybe this is not a template-id
10242 next_token_2->type = CPP_COLON;
10243 cp_parser_error (parser, "expected %<<%>");
10244 pop_deferring_access_checks ();
10245 return error_mark_node;
10247 /* Otherwise, emit an error about the invalid digraph, but continue
10248 parsing because we got our argument list. */
10249 if (permerror (next_token->location,
10250 "%<<::%> cannot begin a template-argument list"))
10252 static bool hint = false;
10253 inform (next_token->location,
10254 "%<<:%> is an alternate spelling for %<[%>."
10255 " Insert whitespace between %<<%> and %<::%>");
10256 if (!hint && !flag_permissive)
10258 inform (next_token->location, "(if you use %<-fpermissive%>"
10259 " G++ will accept your code)");
10266 /* Look for the `<' that starts the template-argument-list. */
10267 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10269 pop_deferring_access_checks ();
10270 return error_mark_node;
10272 /* Parse the arguments. */
10273 arguments = cp_parser_enclosed_template_argument_list (parser);
10276 /* Build a representation of the specialization. */
10277 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10278 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10279 else if (DECL_CLASS_TEMPLATE_P (templ)
10280 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10282 bool entering_scope;
10283 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10284 template (rather than some instantiation thereof) only if
10285 is not nested within some other construct. For example, in
10286 "template <typename T> void f(T) { A<T>::", A<T> is just an
10287 instantiation of A. */
10288 entering_scope = (template_parm_scope_p ()
10289 && cp_lexer_next_token_is (parser->lexer,
10292 = finish_template_type (templ, arguments, entering_scope);
10296 /* If it's not a class-template or a template-template, it should be
10297 a function-template. */
10298 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10299 || TREE_CODE (templ) == OVERLOAD
10300 || BASELINK_P (templ)));
10302 template_id = lookup_template_function (templ, arguments);
10305 /* If parsing tentatively, replace the sequence of tokens that makes
10306 up the template-id with a CPP_TEMPLATE_ID token. That way,
10307 should we re-parse the token stream, we will not have to repeat
10308 the effort required to do the parse, nor will we issue duplicate
10309 error messages about problems during instantiation of the
10313 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10315 /* Reset the contents of the START_OF_ID token. */
10316 token->type = CPP_TEMPLATE_ID;
10317 /* Retrieve any deferred checks. Do not pop this access checks yet
10318 so the memory will not be reclaimed during token replacing below. */
10319 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10320 token->u.tree_check_value->value = template_id;
10321 token->u.tree_check_value->checks = get_deferred_access_checks ();
10322 token->keyword = RID_MAX;
10324 /* Purge all subsequent tokens. */
10325 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10327 /* ??? Can we actually assume that, if template_id ==
10328 error_mark_node, we will have issued a diagnostic to the
10329 user, as opposed to simply marking the tentative parse as
10331 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10332 error_at (token->location, "parse error in template argument list");
10335 pop_deferring_access_checks ();
10336 return template_id;
10339 /* Parse a template-name.
10344 The standard should actually say:
10348 operator-function-id
10350 A defect report has been filed about this issue.
10352 A conversion-function-id cannot be a template name because they cannot
10353 be part of a template-id. In fact, looking at this code:
10355 a.operator K<int>()
10357 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10358 It is impossible to call a templated conversion-function-id with an
10359 explicit argument list, since the only allowed template parameter is
10360 the type to which it is converting.
10362 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10363 `template' keyword, in a construction like:
10367 In that case `f' is taken to be a template-name, even though there
10368 is no way of knowing for sure.
10370 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10371 name refers to a set of overloaded functions, at least one of which
10372 is a template, or an IDENTIFIER_NODE with the name of the template,
10373 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10374 names are looked up inside uninstantiated templates. */
10377 cp_parser_template_name (cp_parser* parser,
10378 bool template_keyword_p,
10379 bool check_dependency_p,
10380 bool is_declaration,
10381 bool *is_identifier)
10386 cp_token *token = cp_lexer_peek_token (parser->lexer);
10388 /* If the next token is `operator', then we have either an
10389 operator-function-id or a conversion-function-id. */
10390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10392 /* We don't know whether we're looking at an
10393 operator-function-id or a conversion-function-id. */
10394 cp_parser_parse_tentatively (parser);
10395 /* Try an operator-function-id. */
10396 identifier = cp_parser_operator_function_id (parser);
10397 /* If that didn't work, try a conversion-function-id. */
10398 if (!cp_parser_parse_definitely (parser))
10400 cp_parser_error (parser, "expected template-name");
10401 return error_mark_node;
10404 /* Look for the identifier. */
10406 identifier = cp_parser_identifier (parser);
10408 /* If we didn't find an identifier, we don't have a template-id. */
10409 if (identifier == error_mark_node)
10410 return error_mark_node;
10412 /* If the name immediately followed the `template' keyword, then it
10413 is a template-name. However, if the next token is not `<', then
10414 we do not treat it as a template-name, since it is not being used
10415 as part of a template-id. This enables us to handle constructs
10418 template <typename T> struct S { S(); };
10419 template <typename T> S<T>::S();
10421 correctly. We would treat `S' as a template -- if it were `S<T>'
10422 -- but we do not if there is no `<'. */
10424 if (processing_template_decl
10425 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10427 /* In a declaration, in a dependent context, we pretend that the
10428 "template" keyword was present in order to improve error
10429 recovery. For example, given:
10431 template <typename T> void f(T::X<int>);
10433 we want to treat "X<int>" as a template-id. */
10435 && !template_keyword_p
10436 && parser->scope && TYPE_P (parser->scope)
10437 && check_dependency_p
10438 && dependent_scope_p (parser->scope)
10439 /* Do not do this for dtors (or ctors), since they never
10440 need the template keyword before their name. */
10441 && !constructor_name_p (identifier, parser->scope))
10443 cp_token_position start = 0;
10445 /* Explain what went wrong. */
10446 error_at (token->location, "non-template %qD used as template",
10448 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
10449 parser->scope, identifier);
10450 /* If parsing tentatively, find the location of the "<" token. */
10451 if (cp_parser_simulate_error (parser))
10452 start = cp_lexer_token_position (parser->lexer, true);
10453 /* Parse the template arguments so that we can issue error
10454 messages about them. */
10455 cp_lexer_consume_token (parser->lexer);
10456 cp_parser_enclosed_template_argument_list (parser);
10457 /* Skip tokens until we find a good place from which to
10458 continue parsing. */
10459 cp_parser_skip_to_closing_parenthesis (parser,
10460 /*recovering=*/true,
10462 /*consume_paren=*/false);
10463 /* If parsing tentatively, permanently remove the
10464 template argument list. That will prevent duplicate
10465 error messages from being issued about the missing
10466 "template" keyword. */
10468 cp_lexer_purge_tokens_after (parser->lexer, start);
10470 *is_identifier = true;
10474 /* If the "template" keyword is present, then there is generally
10475 no point in doing name-lookup, so we just return IDENTIFIER.
10476 But, if the qualifying scope is non-dependent then we can
10477 (and must) do name-lookup normally. */
10478 if (template_keyword_p
10480 || (TYPE_P (parser->scope)
10481 && dependent_type_p (parser->scope))))
10485 /* Look up the name. */
10486 decl = cp_parser_lookup_name (parser, identifier,
10488 /*is_template=*/false,
10489 /*is_namespace=*/false,
10490 check_dependency_p,
10491 /*ambiguous_decls=*/NULL,
10493 decl = maybe_get_template_decl_from_type_decl (decl);
10495 /* If DECL is a template, then the name was a template-name. */
10496 if (TREE_CODE (decl) == TEMPLATE_DECL)
10500 tree fn = NULL_TREE;
10502 /* The standard does not explicitly indicate whether a name that
10503 names a set of overloaded declarations, some of which are
10504 templates, is a template-name. However, such a name should
10505 be a template-name; otherwise, there is no way to form a
10506 template-id for the overloaded templates. */
10507 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10508 if (TREE_CODE (fns) == OVERLOAD)
10509 for (fn = fns; fn; fn = OVL_NEXT (fn))
10510 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10515 /* The name does not name a template. */
10516 cp_parser_error (parser, "expected template-name");
10517 return error_mark_node;
10521 /* If DECL is dependent, and refers to a function, then just return
10522 its name; we will look it up again during template instantiation. */
10523 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10525 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10526 if (TYPE_P (scope) && dependent_type_p (scope))
10533 /* Parse a template-argument-list.
10535 template-argument-list:
10536 template-argument ... [opt]
10537 template-argument-list , template-argument ... [opt]
10539 Returns a TREE_VEC containing the arguments. */
10542 cp_parser_template_argument_list (cp_parser* parser)
10544 tree fixed_args[10];
10545 unsigned n_args = 0;
10546 unsigned alloced = 10;
10547 tree *arg_ary = fixed_args;
10549 bool saved_in_template_argument_list_p;
10551 bool saved_non_ice_p;
10553 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10554 parser->in_template_argument_list_p = true;
10555 /* Even if the template-id appears in an integral
10556 constant-expression, the contents of the argument list do
10558 saved_ice_p = parser->integral_constant_expression_p;
10559 parser->integral_constant_expression_p = false;
10560 saved_non_ice_p = parser->non_integral_constant_expression_p;
10561 parser->non_integral_constant_expression_p = false;
10562 /* Parse the arguments. */
10568 /* Consume the comma. */
10569 cp_lexer_consume_token (parser->lexer);
10571 /* Parse the template-argument. */
10572 argument = cp_parser_template_argument (parser);
10574 /* If the next token is an ellipsis, we're expanding a template
10576 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10578 if (argument == error_mark_node)
10580 cp_token *token = cp_lexer_peek_token (parser->lexer);
10581 error_at (token->location,
10582 "expected parameter pack before %<...%>");
10584 /* Consume the `...' token. */
10585 cp_lexer_consume_token (parser->lexer);
10587 /* Make the argument into a TYPE_PACK_EXPANSION or
10588 EXPR_PACK_EXPANSION. */
10589 argument = make_pack_expansion (argument);
10592 if (n_args == alloced)
10596 if (arg_ary == fixed_args)
10598 arg_ary = XNEWVEC (tree, alloced);
10599 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10602 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10604 arg_ary[n_args++] = argument;
10606 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10608 vec = make_tree_vec (n_args);
10611 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10613 if (arg_ary != fixed_args)
10615 parser->non_integral_constant_expression_p = saved_non_ice_p;
10616 parser->integral_constant_expression_p = saved_ice_p;
10617 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10621 /* Parse a template-argument.
10624 assignment-expression
10628 The representation is that of an assignment-expression, type-id, or
10629 id-expression -- except that the qualified id-expression is
10630 evaluated, so that the value returned is either a DECL or an
10633 Although the standard says "assignment-expression", it forbids
10634 throw-expressions or assignments in the template argument.
10635 Therefore, we use "conditional-expression" instead. */
10638 cp_parser_template_argument (cp_parser* parser)
10643 bool maybe_type_id = false;
10644 cp_token *token = NULL, *argument_start_token = NULL;
10647 /* There's really no way to know what we're looking at, so we just
10648 try each alternative in order.
10652 In a template-argument, an ambiguity between a type-id and an
10653 expression is resolved to a type-id, regardless of the form of
10654 the corresponding template-parameter.
10656 Therefore, we try a type-id first. */
10657 cp_parser_parse_tentatively (parser);
10658 argument = cp_parser_template_type_arg (parser);
10659 /* If there was no error parsing the type-id but the next token is a
10660 '>>', our behavior depends on which dialect of C++ we're
10661 parsing. In C++98, we probably found a typo for '> >'. But there
10662 are type-id which are also valid expressions. For instance:
10664 struct X { int operator >> (int); };
10665 template <int V> struct Foo {};
10668 Here 'X()' is a valid type-id of a function type, but the user just
10669 wanted to write the expression "X() >> 5". Thus, we remember that we
10670 found a valid type-id, but we still try to parse the argument as an
10671 expression to see what happens.
10673 In C++0x, the '>>' will be considered two separate '>'
10675 if (!cp_parser_error_occurred (parser)
10676 && cxx_dialect == cxx98
10677 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10679 maybe_type_id = true;
10680 cp_parser_abort_tentative_parse (parser);
10684 /* If the next token isn't a `,' or a `>', then this argument wasn't
10685 really finished. This means that the argument is not a valid
10687 if (!cp_parser_next_token_ends_template_argument_p (parser))
10688 cp_parser_error (parser, "expected template-argument");
10689 /* If that worked, we're done. */
10690 if (cp_parser_parse_definitely (parser))
10693 /* We're still not sure what the argument will be. */
10694 cp_parser_parse_tentatively (parser);
10695 /* Try a template. */
10696 argument_start_token = cp_lexer_peek_token (parser->lexer);
10697 argument = cp_parser_id_expression (parser,
10698 /*template_keyword_p=*/false,
10699 /*check_dependency_p=*/true,
10701 /*declarator_p=*/false,
10702 /*optional_p=*/false);
10703 /* If the next token isn't a `,' or a `>', then this argument wasn't
10704 really finished. */
10705 if (!cp_parser_next_token_ends_template_argument_p (parser))
10706 cp_parser_error (parser, "expected template-argument");
10707 if (!cp_parser_error_occurred (parser))
10709 /* Figure out what is being referred to. If the id-expression
10710 was for a class template specialization, then we will have a
10711 TYPE_DECL at this point. There is no need to do name lookup
10712 at this point in that case. */
10713 if (TREE_CODE (argument) != TYPE_DECL)
10714 argument = cp_parser_lookup_name (parser, argument,
10716 /*is_template=*/template_p,
10717 /*is_namespace=*/false,
10718 /*check_dependency=*/true,
10719 /*ambiguous_decls=*/NULL,
10720 argument_start_token->location);
10721 if (TREE_CODE (argument) != TEMPLATE_DECL
10722 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10723 cp_parser_error (parser, "expected template-name");
10725 if (cp_parser_parse_definitely (parser))
10727 /* It must be a non-type argument. There permitted cases are given
10728 in [temp.arg.nontype]:
10730 -- an integral constant-expression of integral or enumeration
10733 -- the name of a non-type template-parameter; or
10735 -- the name of an object or function with external linkage...
10737 -- the address of an object or function with external linkage...
10739 -- a pointer to member... */
10740 /* Look for a non-type template parameter. */
10741 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10743 cp_parser_parse_tentatively (parser);
10744 argument = cp_parser_primary_expression (parser,
10745 /*address_p=*/false,
10747 /*template_arg_p=*/true,
10749 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10750 || !cp_parser_next_token_ends_template_argument_p (parser))
10751 cp_parser_simulate_error (parser);
10752 if (cp_parser_parse_definitely (parser))
10756 /* If the next token is "&", the argument must be the address of an
10757 object or function with external linkage. */
10758 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10760 cp_lexer_consume_token (parser->lexer);
10761 /* See if we might have an id-expression. */
10762 token = cp_lexer_peek_token (parser->lexer);
10763 if (token->type == CPP_NAME
10764 || token->keyword == RID_OPERATOR
10765 || token->type == CPP_SCOPE
10766 || token->type == CPP_TEMPLATE_ID
10767 || token->type == CPP_NESTED_NAME_SPECIFIER)
10769 cp_parser_parse_tentatively (parser);
10770 argument = cp_parser_primary_expression (parser,
10773 /*template_arg_p=*/true,
10775 if (cp_parser_error_occurred (parser)
10776 || !cp_parser_next_token_ends_template_argument_p (parser))
10777 cp_parser_abort_tentative_parse (parser);
10780 if (TREE_CODE (argument) == INDIRECT_REF)
10782 gcc_assert (REFERENCE_REF_P (argument));
10783 argument = TREE_OPERAND (argument, 0);
10786 if (TREE_CODE (argument) == VAR_DECL)
10788 /* A variable without external linkage might still be a
10789 valid constant-expression, so no error is issued here
10790 if the external-linkage check fails. */
10791 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10792 cp_parser_simulate_error (parser);
10794 else if (is_overloaded_fn (argument))
10795 /* All overloaded functions are allowed; if the external
10796 linkage test does not pass, an error will be issued
10800 && (TREE_CODE (argument) == OFFSET_REF
10801 || TREE_CODE (argument) == SCOPE_REF))
10802 /* A pointer-to-member. */
10804 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10807 cp_parser_simulate_error (parser);
10809 if (cp_parser_parse_definitely (parser))
10812 argument = build_x_unary_op (ADDR_EXPR, argument,
10813 tf_warning_or_error);
10818 /* If the argument started with "&", there are no other valid
10819 alternatives at this point. */
10822 cp_parser_error (parser, "invalid non-type template argument");
10823 return error_mark_node;
10826 /* If the argument wasn't successfully parsed as a type-id followed
10827 by '>>', the argument can only be a constant expression now.
10828 Otherwise, we try parsing the constant-expression tentatively,
10829 because the argument could really be a type-id. */
10831 cp_parser_parse_tentatively (parser);
10832 argument = cp_parser_constant_expression (parser,
10833 /*allow_non_constant_p=*/false,
10834 /*non_constant_p=*/NULL);
10835 argument = fold_non_dependent_expr (argument);
10836 if (!maybe_type_id)
10838 if (!cp_parser_next_token_ends_template_argument_p (parser))
10839 cp_parser_error (parser, "expected template-argument");
10840 if (cp_parser_parse_definitely (parser))
10842 /* We did our best to parse the argument as a non type-id, but that
10843 was the only alternative that matched (albeit with a '>' after
10844 it). We can assume it's just a typo from the user, and a
10845 diagnostic will then be issued. */
10846 return cp_parser_template_type_arg (parser);
10849 /* Parse an explicit-instantiation.
10851 explicit-instantiation:
10852 template declaration
10854 Although the standard says `declaration', what it really means is:
10856 explicit-instantiation:
10857 template decl-specifier-seq [opt] declarator [opt] ;
10859 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10860 supposed to be allowed. A defect report has been filed about this
10865 explicit-instantiation:
10866 storage-class-specifier template
10867 decl-specifier-seq [opt] declarator [opt] ;
10868 function-specifier template
10869 decl-specifier-seq [opt] declarator [opt] ; */
10872 cp_parser_explicit_instantiation (cp_parser* parser)
10874 int declares_class_or_enum;
10875 cp_decl_specifier_seq decl_specifiers;
10876 tree extension_specifier = NULL_TREE;
10879 /* Look for an (optional) storage-class-specifier or
10880 function-specifier. */
10881 if (cp_parser_allow_gnu_extensions_p (parser))
10883 extension_specifier
10884 = cp_parser_storage_class_specifier_opt (parser);
10885 if (!extension_specifier)
10886 extension_specifier
10887 = cp_parser_function_specifier_opt (parser,
10888 /*decl_specs=*/NULL);
10891 /* Look for the `template' keyword. */
10892 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10893 /* Let the front end know that we are processing an explicit
10895 begin_explicit_instantiation ();
10896 /* [temp.explicit] says that we are supposed to ignore access
10897 control while processing explicit instantiation directives. */
10898 push_deferring_access_checks (dk_no_check);
10899 /* Parse a decl-specifier-seq. */
10900 token = cp_lexer_peek_token (parser->lexer);
10901 cp_parser_decl_specifier_seq (parser,
10902 CP_PARSER_FLAGS_OPTIONAL,
10904 &declares_class_or_enum);
10905 /* If there was exactly one decl-specifier, and it declared a class,
10906 and there's no declarator, then we have an explicit type
10908 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10912 type = check_tag_decl (&decl_specifiers);
10913 /* Turn access control back on for names used during
10914 template instantiation. */
10915 pop_deferring_access_checks ();
10917 do_type_instantiation (type, extension_specifier,
10918 /*complain=*/tf_error);
10922 cp_declarator *declarator;
10925 /* Parse the declarator. */
10927 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10928 /*ctor_dtor_or_conv_p=*/NULL,
10929 /*parenthesized_p=*/NULL,
10930 /*member_p=*/false);
10931 if (declares_class_or_enum & 2)
10932 cp_parser_check_for_definition_in_return_type (declarator,
10933 decl_specifiers.type,
10934 decl_specifiers.type_location);
10935 if (declarator != cp_error_declarator)
10937 decl = grokdeclarator (declarator, &decl_specifiers,
10938 NORMAL, 0, &decl_specifiers.attributes);
10939 /* Turn access control back on for names used during
10940 template instantiation. */
10941 pop_deferring_access_checks ();
10942 /* Do the explicit instantiation. */
10943 do_decl_instantiation (decl, extension_specifier);
10947 pop_deferring_access_checks ();
10948 /* Skip the body of the explicit instantiation. */
10949 cp_parser_skip_to_end_of_statement (parser);
10952 /* We're done with the instantiation. */
10953 end_explicit_instantiation ();
10955 cp_parser_consume_semicolon_at_end_of_statement (parser);
10958 /* Parse an explicit-specialization.
10960 explicit-specialization:
10961 template < > declaration
10963 Although the standard says `declaration', what it really means is:
10965 explicit-specialization:
10966 template <> decl-specifier [opt] init-declarator [opt] ;
10967 template <> function-definition
10968 template <> explicit-specialization
10969 template <> template-declaration */
10972 cp_parser_explicit_specialization (cp_parser* parser)
10974 bool need_lang_pop;
10975 cp_token *token = cp_lexer_peek_token (parser->lexer);
10977 /* Look for the `template' keyword. */
10978 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10979 /* Look for the `<'. */
10980 cp_parser_require (parser, CPP_LESS, "%<<%>");
10981 /* Look for the `>'. */
10982 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10983 /* We have processed another parameter list. */
10984 ++parser->num_template_parameter_lists;
10987 A template ... explicit specialization ... shall not have C
10989 if (current_lang_name == lang_name_c)
10991 error_at (token->location, "template specialization with C linkage");
10992 /* Give it C++ linkage to avoid confusing other parts of the
10994 push_lang_context (lang_name_cplusplus);
10995 need_lang_pop = true;
10998 need_lang_pop = false;
10999 /* Let the front end know that we are beginning a specialization. */
11000 if (!begin_specialization ())
11002 end_specialization ();
11006 /* If the next keyword is `template', we need to figure out whether
11007 or not we're looking a template-declaration. */
11008 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11010 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11011 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11012 cp_parser_template_declaration_after_export (parser,
11013 /*member_p=*/false);
11015 cp_parser_explicit_specialization (parser);
11018 /* Parse the dependent declaration. */
11019 cp_parser_single_declaration (parser,
11021 /*member_p=*/false,
11022 /*explicit_specialization_p=*/true,
11023 /*friend_p=*/NULL);
11024 /* We're done with the specialization. */
11025 end_specialization ();
11026 /* For the erroneous case of a template with C linkage, we pushed an
11027 implicit C++ linkage scope; exit that scope now. */
11029 pop_lang_context ();
11030 /* We're done with this parameter list. */
11031 --parser->num_template_parameter_lists;
11034 /* Parse a type-specifier.
11037 simple-type-specifier
11040 elaborated-type-specifier
11048 Returns a representation of the type-specifier. For a
11049 class-specifier, enum-specifier, or elaborated-type-specifier, a
11050 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11052 The parser flags FLAGS is used to control type-specifier parsing.
11054 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11055 in a decl-specifier-seq.
11057 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11058 class-specifier, enum-specifier, or elaborated-type-specifier, then
11059 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11060 if a type is declared; 2 if it is defined. Otherwise, it is set to
11063 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11064 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11065 is set to FALSE. */
11068 cp_parser_type_specifier (cp_parser* parser,
11069 cp_parser_flags flags,
11070 cp_decl_specifier_seq *decl_specs,
11071 bool is_declaration,
11072 int* declares_class_or_enum,
11073 bool* is_cv_qualifier)
11075 tree type_spec = NULL_TREE;
11078 cp_decl_spec ds = ds_last;
11080 /* Assume this type-specifier does not declare a new type. */
11081 if (declares_class_or_enum)
11082 *declares_class_or_enum = 0;
11083 /* And that it does not specify a cv-qualifier. */
11084 if (is_cv_qualifier)
11085 *is_cv_qualifier = false;
11086 /* Peek at the next token. */
11087 token = cp_lexer_peek_token (parser->lexer);
11089 /* If we're looking at a keyword, we can use that to guide the
11090 production we choose. */
11091 keyword = token->keyword;
11095 /* Look for the enum-specifier. */
11096 type_spec = cp_parser_enum_specifier (parser);
11097 /* If that worked, we're done. */
11100 if (declares_class_or_enum)
11101 *declares_class_or_enum = 2;
11103 cp_parser_set_decl_spec_type (decl_specs,
11106 /*user_defined_p=*/true);
11110 goto elaborated_type_specifier;
11112 /* Any of these indicate either a class-specifier, or an
11113 elaborated-type-specifier. */
11117 /* Parse tentatively so that we can back up if we don't find a
11118 class-specifier. */
11119 cp_parser_parse_tentatively (parser);
11120 /* Look for the class-specifier. */
11121 type_spec = cp_parser_class_specifier (parser);
11122 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11123 /* If that worked, we're done. */
11124 if (cp_parser_parse_definitely (parser))
11126 if (declares_class_or_enum)
11127 *declares_class_or_enum = 2;
11129 cp_parser_set_decl_spec_type (decl_specs,
11132 /*user_defined_p=*/true);
11136 /* Fall through. */
11137 elaborated_type_specifier:
11138 /* We're declaring (not defining) a class or enum. */
11139 if (declares_class_or_enum)
11140 *declares_class_or_enum = 1;
11142 /* Fall through. */
11144 /* Look for an elaborated-type-specifier. */
11146 = (cp_parser_elaborated_type_specifier
11148 decl_specs && decl_specs->specs[(int) ds_friend],
11151 cp_parser_set_decl_spec_type (decl_specs,
11154 /*user_defined_p=*/true);
11159 if (is_cv_qualifier)
11160 *is_cv_qualifier = true;
11165 if (is_cv_qualifier)
11166 *is_cv_qualifier = true;
11171 if (is_cv_qualifier)
11172 *is_cv_qualifier = true;
11176 /* The `__complex__' keyword is a GNU extension. */
11184 /* Handle simple keywords. */
11189 ++decl_specs->specs[(int)ds];
11190 decl_specs->any_specifiers_p = true;
11192 return cp_lexer_consume_token (parser->lexer)->u.value;
11195 /* If we do not already have a type-specifier, assume we are looking
11196 at a simple-type-specifier. */
11197 type_spec = cp_parser_simple_type_specifier (parser,
11201 /* If we didn't find a type-specifier, and a type-specifier was not
11202 optional in this context, issue an error message. */
11203 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11205 cp_parser_error (parser, "expected type specifier");
11206 return error_mark_node;
11212 /* Parse a simple-type-specifier.
11214 simple-type-specifier:
11215 :: [opt] nested-name-specifier [opt] type-name
11216 :: [opt] nested-name-specifier template template-id
11231 simple-type-specifier:
11233 decltype ( expression )
11239 simple-type-specifier:
11240 __typeof__ unary-expression
11241 __typeof__ ( type-id )
11243 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11244 appropriately updated. */
11247 cp_parser_simple_type_specifier (cp_parser* parser,
11248 cp_decl_specifier_seq *decl_specs,
11249 cp_parser_flags flags)
11251 tree type = NULL_TREE;
11254 /* Peek at the next token. */
11255 token = cp_lexer_peek_token (parser->lexer);
11257 /* If we're looking at a keyword, things are easy. */
11258 switch (token->keyword)
11262 decl_specs->explicit_char_p = true;
11263 type = char_type_node;
11266 type = char16_type_node;
11269 type = char32_type_node;
11272 type = wchar_type_node;
11275 type = boolean_type_node;
11279 ++decl_specs->specs[(int) ds_short];
11280 type = short_integer_type_node;
11284 decl_specs->explicit_int_p = true;
11285 type = integer_type_node;
11289 ++decl_specs->specs[(int) ds_long];
11290 type = long_integer_type_node;
11294 ++decl_specs->specs[(int) ds_signed];
11295 type = integer_type_node;
11299 ++decl_specs->specs[(int) ds_unsigned];
11300 type = unsigned_type_node;
11303 type = float_type_node;
11306 type = double_type_node;
11309 type = void_type_node;
11313 maybe_warn_cpp0x ("C++0x auto");
11314 type = make_auto ();
11318 /* Parse the `decltype' type. */
11319 type = cp_parser_decltype (parser);
11322 cp_parser_set_decl_spec_type (decl_specs, type,
11324 /*user_defined_p=*/true);
11329 /* Consume the `typeof' token. */
11330 cp_lexer_consume_token (parser->lexer);
11331 /* Parse the operand to `typeof'. */
11332 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11333 /* If it is not already a TYPE, take its type. */
11334 if (!TYPE_P (type))
11335 type = finish_typeof (type);
11338 cp_parser_set_decl_spec_type (decl_specs, type,
11340 /*user_defined_p=*/true);
11348 /* If the type-specifier was for a built-in type, we're done. */
11353 /* Record the type. */
11355 && (token->keyword != RID_SIGNED
11356 && token->keyword != RID_UNSIGNED
11357 && token->keyword != RID_SHORT
11358 && token->keyword != RID_LONG))
11359 cp_parser_set_decl_spec_type (decl_specs,
11362 /*user_defined=*/false);
11364 decl_specs->any_specifiers_p = true;
11366 /* Consume the token. */
11367 id = cp_lexer_consume_token (parser->lexer)->u.value;
11369 /* There is no valid C++ program where a non-template type is
11370 followed by a "<". That usually indicates that the user thought
11371 that the type was a template. */
11372 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11374 return TYPE_NAME (type);
11377 /* The type-specifier must be a user-defined type. */
11378 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11383 /* Don't gobble tokens or issue error messages if this is an
11384 optional type-specifier. */
11385 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11386 cp_parser_parse_tentatively (parser);
11388 /* Look for the optional `::' operator. */
11390 = (cp_parser_global_scope_opt (parser,
11391 /*current_scope_valid_p=*/false)
11393 /* Look for the nested-name specifier. */
11395 = (cp_parser_nested_name_specifier_opt (parser,
11396 /*typename_keyword_p=*/false,
11397 /*check_dependency_p=*/true,
11399 /*is_declaration=*/false)
11401 token = cp_lexer_peek_token (parser->lexer);
11402 /* If we have seen a nested-name-specifier, and the next token
11403 is `template', then we are using the template-id production. */
11405 && cp_parser_optional_template_keyword (parser))
11407 /* Look for the template-id. */
11408 type = cp_parser_template_id (parser,
11409 /*template_keyword_p=*/true,
11410 /*check_dependency_p=*/true,
11411 /*is_declaration=*/false);
11412 /* If the template-id did not name a type, we are out of
11414 if (TREE_CODE (type) != TYPE_DECL)
11416 cp_parser_error (parser, "expected template-id for type");
11420 /* Otherwise, look for a type-name. */
11422 type = cp_parser_type_name (parser);
11423 /* Keep track of all name-lookups performed in class scopes. */
11427 && TREE_CODE (type) == TYPE_DECL
11428 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11429 maybe_note_name_used_in_class (DECL_NAME (type), type);
11430 /* If it didn't work out, we don't have a TYPE. */
11431 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11432 && !cp_parser_parse_definitely (parser))
11434 if (type && decl_specs)
11435 cp_parser_set_decl_spec_type (decl_specs, type,
11437 /*user_defined=*/true);
11440 /* If we didn't get a type-name, issue an error message. */
11441 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11443 cp_parser_error (parser, "expected type-name");
11444 return error_mark_node;
11447 /* There is no valid C++ program where a non-template type is
11448 followed by a "<". That usually indicates that the user thought
11449 that the type was a template. */
11450 if (type && type != error_mark_node)
11452 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11453 If it is, then the '<'...'>' enclose protocol names rather than
11454 template arguments, and so everything is fine. */
11455 if (c_dialect_objc ()
11456 && (objc_is_id (type) || objc_is_class_name (type)))
11458 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11459 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11461 /* Clobber the "unqualified" type previously entered into
11462 DECL_SPECS with the new, improved protocol-qualified version. */
11464 decl_specs->type = qual_type;
11469 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11476 /* Parse a type-name.
11489 Returns a TYPE_DECL for the type. */
11492 cp_parser_type_name (cp_parser* parser)
11496 /* We can't know yet whether it is a class-name or not. */
11497 cp_parser_parse_tentatively (parser);
11498 /* Try a class-name. */
11499 type_decl = cp_parser_class_name (parser,
11500 /*typename_keyword_p=*/false,
11501 /*template_keyword_p=*/false,
11503 /*check_dependency_p=*/true,
11504 /*class_head_p=*/false,
11505 /*is_declaration=*/false);
11506 /* If it's not a class-name, keep looking. */
11507 if (!cp_parser_parse_definitely (parser))
11509 /* It must be a typedef-name or an enum-name. */
11510 return cp_parser_nonclass_name (parser);
11516 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11524 Returns a TYPE_DECL for the type. */
11527 cp_parser_nonclass_name (cp_parser* parser)
11532 cp_token *token = cp_lexer_peek_token (parser->lexer);
11533 identifier = cp_parser_identifier (parser);
11534 if (identifier == error_mark_node)
11535 return error_mark_node;
11537 /* Look up the type-name. */
11538 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11540 if (TREE_CODE (type_decl) != TYPE_DECL
11541 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11543 /* See if this is an Objective-C type. */
11544 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11545 tree type = objc_get_protocol_qualified_type (identifier, protos);
11547 type_decl = TYPE_NAME (type);
11550 /* Issue an error if we did not find a type-name. */
11551 if (TREE_CODE (type_decl) != TYPE_DECL)
11553 if (!cp_parser_simulate_error (parser))
11554 cp_parser_name_lookup_error (parser, identifier, type_decl,
11555 "is not a type", token->location);
11556 return error_mark_node;
11558 /* Remember that the name was used in the definition of the
11559 current class so that we can check later to see if the
11560 meaning would have been different after the class was
11561 entirely defined. */
11562 else if (type_decl != error_mark_node
11564 maybe_note_name_used_in_class (identifier, type_decl);
11569 /* Parse an elaborated-type-specifier. Note that the grammar given
11570 here incorporates the resolution to DR68.
11572 elaborated-type-specifier:
11573 class-key :: [opt] nested-name-specifier [opt] identifier
11574 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11575 enum-key :: [opt] nested-name-specifier [opt] identifier
11576 typename :: [opt] nested-name-specifier identifier
11577 typename :: [opt] nested-name-specifier template [opt]
11582 elaborated-type-specifier:
11583 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11584 class-key attributes :: [opt] nested-name-specifier [opt]
11585 template [opt] template-id
11586 enum attributes :: [opt] nested-name-specifier [opt] identifier
11588 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11589 declared `friend'. If IS_DECLARATION is TRUE, then this
11590 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11591 something is being declared.
11593 Returns the TYPE specified. */
11596 cp_parser_elaborated_type_specifier (cp_parser* parser,
11598 bool is_declaration)
11600 enum tag_types tag_type;
11602 tree type = NULL_TREE;
11603 tree attributes = NULL_TREE;
11605 cp_token *token = NULL;
11607 /* See if we're looking at the `enum' keyword. */
11608 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11610 /* Consume the `enum' token. */
11611 cp_lexer_consume_token (parser->lexer);
11612 /* Remember that it's an enumeration type. */
11613 tag_type = enum_type;
11614 /* Parse the optional `struct' or `class' key (for C++0x scoped
11616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11617 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11619 if (cxx_dialect == cxx98)
11620 maybe_warn_cpp0x ("scoped enums");
11622 /* Consume the `struct' or `class'. */
11623 cp_lexer_consume_token (parser->lexer);
11625 /* Parse the attributes. */
11626 attributes = cp_parser_attributes_opt (parser);
11628 /* Or, it might be `typename'. */
11629 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11632 /* Consume the `typename' token. */
11633 cp_lexer_consume_token (parser->lexer);
11634 /* Remember that it's a `typename' type. */
11635 tag_type = typename_type;
11637 /* Otherwise it must be a class-key. */
11640 tag_type = cp_parser_class_key (parser);
11641 if (tag_type == none_type)
11642 return error_mark_node;
11643 /* Parse the attributes. */
11644 attributes = cp_parser_attributes_opt (parser);
11647 /* Look for the `::' operator. */
11648 globalscope = cp_parser_global_scope_opt (parser,
11649 /*current_scope_valid_p=*/false);
11650 /* Look for the nested-name-specifier. */
11651 if (tag_type == typename_type && !globalscope)
11653 if (!cp_parser_nested_name_specifier (parser,
11654 /*typename_keyword_p=*/true,
11655 /*check_dependency_p=*/true,
11658 return error_mark_node;
11661 /* Even though `typename' is not present, the proposed resolution
11662 to Core Issue 180 says that in `class A<T>::B', `B' should be
11663 considered a type-name, even if `A<T>' is dependent. */
11664 cp_parser_nested_name_specifier_opt (parser,
11665 /*typename_keyword_p=*/true,
11666 /*check_dependency_p=*/true,
11669 /* For everything but enumeration types, consider a template-id.
11670 For an enumeration type, consider only a plain identifier. */
11671 if (tag_type != enum_type)
11673 bool template_p = false;
11676 /* Allow the `template' keyword. */
11677 template_p = cp_parser_optional_template_keyword (parser);
11678 /* If we didn't see `template', we don't know if there's a
11679 template-id or not. */
11681 cp_parser_parse_tentatively (parser);
11682 /* Parse the template-id. */
11683 token = cp_lexer_peek_token (parser->lexer);
11684 decl = cp_parser_template_id (parser, template_p,
11685 /*check_dependency_p=*/true,
11687 /* If we didn't find a template-id, look for an ordinary
11689 if (!template_p && !cp_parser_parse_definitely (parser))
11691 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11692 in effect, then we must assume that, upon instantiation, the
11693 template will correspond to a class. */
11694 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11695 && tag_type == typename_type)
11696 type = make_typename_type (parser->scope, decl,
11698 /*complain=*/tf_error);
11699 /* If the `typename' keyword is in effect and DECL is not a type
11700 decl. Then type is non existant. */
11701 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11704 type = TREE_TYPE (decl);
11709 token = cp_lexer_peek_token (parser->lexer);
11710 identifier = cp_parser_identifier (parser);
11712 if (identifier == error_mark_node)
11714 parser->scope = NULL_TREE;
11715 return error_mark_node;
11718 /* For a `typename', we needn't call xref_tag. */
11719 if (tag_type == typename_type
11720 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11721 return cp_parser_make_typename_type (parser, parser->scope,
11724 /* Look up a qualified name in the usual way. */
11728 tree ambiguous_decls;
11730 decl = cp_parser_lookup_name (parser, identifier,
11732 /*is_template=*/false,
11733 /*is_namespace=*/false,
11734 /*check_dependency=*/true,
11738 /* If the lookup was ambiguous, an error will already have been
11740 if (ambiguous_decls)
11741 return error_mark_node;
11743 /* If we are parsing friend declaration, DECL may be a
11744 TEMPLATE_DECL tree node here. However, we need to check
11745 whether this TEMPLATE_DECL results in valid code. Consider
11746 the following example:
11749 template <class T> class C {};
11752 template <class T> friend class N::C; // #1, valid code
11754 template <class T> class Y {
11755 friend class N::C; // #2, invalid code
11758 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11759 name lookup of `N::C'. We see that friend declaration must
11760 be template for the code to be valid. Note that
11761 processing_template_decl does not work here since it is
11762 always 1 for the above two cases. */
11764 decl = (cp_parser_maybe_treat_template_as_class
11765 (decl, /*tag_name_p=*/is_friend
11766 && parser->num_template_parameter_lists));
11768 if (TREE_CODE (decl) != TYPE_DECL)
11770 cp_parser_diagnose_invalid_type_name (parser,
11774 return error_mark_node;
11777 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11779 bool allow_template = (parser->num_template_parameter_lists
11780 || DECL_SELF_REFERENCE_P (decl));
11781 type = check_elaborated_type_specifier (tag_type, decl,
11784 if (type == error_mark_node)
11785 return error_mark_node;
11788 /* Forward declarations of nested types, such as
11793 are invalid unless all components preceding the final '::'
11794 are complete. If all enclosing types are complete, these
11795 declarations become merely pointless.
11797 Invalid forward declarations of nested types are errors
11798 caught elsewhere in parsing. Those that are pointless arrive
11801 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11802 && !is_friend && !processing_explicit_instantiation)
11803 warning (0, "declaration %qD does not declare anything", decl);
11805 type = TREE_TYPE (decl);
11809 /* An elaborated-type-specifier sometimes introduces a new type and
11810 sometimes names an existing type. Normally, the rule is that it
11811 introduces a new type only if there is not an existing type of
11812 the same name already in scope. For example, given:
11815 void f() { struct S s; }
11817 the `struct S' in the body of `f' is the same `struct S' as in
11818 the global scope; the existing definition is used. However, if
11819 there were no global declaration, this would introduce a new
11820 local class named `S'.
11822 An exception to this rule applies to the following code:
11824 namespace N { struct S; }
11826 Here, the elaborated-type-specifier names a new type
11827 unconditionally; even if there is already an `S' in the
11828 containing scope this declaration names a new type.
11829 This exception only applies if the elaborated-type-specifier
11830 forms the complete declaration:
11834 A declaration consisting solely of `class-key identifier ;' is
11835 either a redeclaration of the name in the current scope or a
11836 forward declaration of the identifier as a class name. It
11837 introduces the name into the current scope.
11839 We are in this situation precisely when the next token is a `;'.
11841 An exception to the exception is that a `friend' declaration does
11842 *not* name a new type; i.e., given:
11844 struct S { friend struct T; };
11846 `T' is not a new type in the scope of `S'.
11848 Also, `new struct S' or `sizeof (struct S)' never results in the
11849 definition of a new type; a new type can only be declared in a
11850 declaration context. */
11856 /* Friends have special name lookup rules. */
11857 ts = ts_within_enclosing_non_class;
11858 else if (is_declaration
11859 && cp_lexer_next_token_is (parser->lexer,
11861 /* This is a `class-key identifier ;' */
11867 (parser->num_template_parameter_lists
11868 && (cp_parser_next_token_starts_class_definition_p (parser)
11869 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11870 /* An unqualified name was used to reference this type, so
11871 there were no qualifying templates. */
11872 if (!cp_parser_check_template_parameters (parser,
11873 /*num_templates=*/0,
11875 /*declarator=*/NULL))
11876 return error_mark_node;
11877 type = xref_tag (tag_type, identifier, ts, template_p);
11881 if (type == error_mark_node)
11882 return error_mark_node;
11884 /* Allow attributes on forward declarations of classes. */
11887 if (TREE_CODE (type) == TYPENAME_TYPE)
11888 warning (OPT_Wattributes,
11889 "attributes ignored on uninstantiated type");
11890 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11891 && ! processing_explicit_instantiation)
11892 warning (OPT_Wattributes,
11893 "attributes ignored on template instantiation");
11894 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11895 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11897 warning (OPT_Wattributes,
11898 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11901 if (tag_type != enum_type)
11902 cp_parser_check_class_key (tag_type, type);
11904 /* A "<" cannot follow an elaborated type specifier. If that
11905 happens, the user was probably trying to form a template-id. */
11906 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11911 /* Parse an enum-specifier.
11914 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11919 enum struct [C++0x]
11922 : type-specifier-seq
11925 enum-key attributes[opt] identifier [opt] enum-base [opt]
11926 { enumerator-list [opt] }attributes[opt]
11928 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11929 if the token stream isn't an enum-specifier after all. */
11932 cp_parser_enum_specifier (cp_parser* parser)
11937 bool scoped_enum_p = false;
11938 bool has_underlying_type = false;
11939 tree underlying_type = NULL_TREE;
11941 /* Parse tentatively so that we can back up if we don't find a
11943 cp_parser_parse_tentatively (parser);
11945 /* Caller guarantees that the current token is 'enum', an identifier
11946 possibly follows, and the token after that is an opening brace.
11947 If we don't have an identifier, fabricate an anonymous name for
11948 the enumeration being defined. */
11949 cp_lexer_consume_token (parser->lexer);
11951 /* Parse the "class" or "struct", which indicates a scoped
11952 enumeration type in C++0x. */
11953 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11954 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11956 if (cxx_dialect == cxx98)
11957 maybe_warn_cpp0x ("scoped enums");
11959 /* Consume the `struct' or `class' token. */
11960 cp_lexer_consume_token (parser->lexer);
11962 scoped_enum_p = true;
11965 attributes = cp_parser_attributes_opt (parser);
11967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11968 identifier = cp_parser_identifier (parser);
11970 identifier = make_anon_name ();
11972 /* Check for the `:' that denotes a specified underlying type in C++0x.
11973 Note that a ':' could also indicate a bitfield width, however. */
11974 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11976 cp_decl_specifier_seq type_specifiers;
11978 /* Consume the `:'. */
11979 cp_lexer_consume_token (parser->lexer);
11981 /* Parse the type-specifier-seq. */
11982 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11985 /* At this point this is surely not elaborated type specifier. */
11986 if (!cp_parser_parse_definitely (parser))
11989 if (cxx_dialect == cxx98)
11990 maybe_warn_cpp0x ("scoped enums");
11992 has_underlying_type = true;
11994 /* If that didn't work, stop. */
11995 if (type_specifiers.type != error_mark_node)
11997 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11998 /*initialized=*/0, NULL);
11999 if (underlying_type == error_mark_node)
12000 underlying_type = NULL_TREE;
12004 /* Look for the `{' but don't consume it yet. */
12005 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12007 cp_parser_error (parser, "expected %<{%>");
12008 if (has_underlying_type)
12012 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12015 /* Issue an error message if type-definitions are forbidden here. */
12016 if (!cp_parser_check_type_definition (parser))
12017 type = error_mark_node;
12019 /* Create the new type. We do this before consuming the opening
12020 brace so the enum will be recorded as being on the line of its
12021 tag (or the 'enum' keyword, if there is no tag). */
12022 type = start_enum (identifier, underlying_type, scoped_enum_p);
12024 /* Consume the opening brace. */
12025 cp_lexer_consume_token (parser->lexer);
12027 if (type == error_mark_node)
12029 cp_parser_skip_to_end_of_block_or_statement (parser);
12030 return error_mark_node;
12033 /* If the next token is not '}', then there are some enumerators. */
12034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12035 cp_parser_enumerator_list (parser, type);
12037 /* Consume the final '}'. */
12038 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12040 /* Look for trailing attributes to apply to this enumeration, and
12041 apply them if appropriate. */
12042 if (cp_parser_allow_gnu_extensions_p (parser))
12044 tree trailing_attr = cp_parser_attributes_opt (parser);
12045 trailing_attr = chainon (trailing_attr, attributes);
12046 cplus_decl_attributes (&type,
12048 (int) ATTR_FLAG_TYPE_IN_PLACE);
12051 /* Finish up the enumeration. */
12052 finish_enum (type);
12057 /* Parse an enumerator-list. The enumerators all have the indicated
12061 enumerator-definition
12062 enumerator-list , enumerator-definition */
12065 cp_parser_enumerator_list (cp_parser* parser, tree type)
12069 /* Parse an enumerator-definition. */
12070 cp_parser_enumerator_definition (parser, type);
12072 /* If the next token is not a ',', we've reached the end of
12074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12076 /* Otherwise, consume the `,' and keep going. */
12077 cp_lexer_consume_token (parser->lexer);
12078 /* If the next token is a `}', there is a trailing comma. */
12079 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12081 if (!in_system_header)
12082 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12088 /* Parse an enumerator-definition. The enumerator has the indicated
12091 enumerator-definition:
12093 enumerator = constant-expression
12099 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12104 /* Look for the identifier. */
12105 identifier = cp_parser_identifier (parser);
12106 if (identifier == error_mark_node)
12109 /* If the next token is an '=', then there is an explicit value. */
12110 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12112 /* Consume the `=' token. */
12113 cp_lexer_consume_token (parser->lexer);
12114 /* Parse the value. */
12115 value = cp_parser_constant_expression (parser,
12116 /*allow_non_constant_p=*/false,
12122 /* If we are processing a template, make sure the initializer of the
12123 enumerator doesn't contain any bare template parameter pack. */
12124 if (check_for_bare_parameter_packs (value))
12125 value = error_mark_node;
12127 /* Create the enumerator. */
12128 build_enumerator (identifier, value, type);
12131 /* Parse a namespace-name.
12134 original-namespace-name
12137 Returns the NAMESPACE_DECL for the namespace. */
12140 cp_parser_namespace_name (cp_parser* parser)
12143 tree namespace_decl;
12145 cp_token *token = cp_lexer_peek_token (parser->lexer);
12147 /* Get the name of the namespace. */
12148 identifier = cp_parser_identifier (parser);
12149 if (identifier == error_mark_node)
12150 return error_mark_node;
12152 /* Look up the identifier in the currently active scope. Look only
12153 for namespaces, due to:
12155 [basic.lookup.udir]
12157 When looking up a namespace-name in a using-directive or alias
12158 definition, only namespace names are considered.
12162 [basic.lookup.qual]
12164 During the lookup of a name preceding the :: scope resolution
12165 operator, object, function, and enumerator names are ignored.
12167 (Note that cp_parser_qualifying_entity only calls this
12168 function if the token after the name is the scope resolution
12170 namespace_decl = cp_parser_lookup_name (parser, identifier,
12172 /*is_template=*/false,
12173 /*is_namespace=*/true,
12174 /*check_dependency=*/true,
12175 /*ambiguous_decls=*/NULL,
12177 /* If it's not a namespace, issue an error. */
12178 if (namespace_decl == error_mark_node
12179 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12182 error_at (token->location, "%qD is not a namespace-name", identifier);
12183 cp_parser_error (parser, "expected namespace-name");
12184 namespace_decl = error_mark_node;
12187 return namespace_decl;
12190 /* Parse a namespace-definition.
12192 namespace-definition:
12193 named-namespace-definition
12194 unnamed-namespace-definition
12196 named-namespace-definition:
12197 original-namespace-definition
12198 extension-namespace-definition
12200 original-namespace-definition:
12201 namespace identifier { namespace-body }
12203 extension-namespace-definition:
12204 namespace original-namespace-name { namespace-body }
12206 unnamed-namespace-definition:
12207 namespace { namespace-body } */
12210 cp_parser_namespace_definition (cp_parser* parser)
12212 tree identifier, attribs;
12213 bool has_visibility;
12216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12219 cp_lexer_consume_token (parser->lexer);
12224 /* Look for the `namespace' keyword. */
12225 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12227 /* Get the name of the namespace. We do not attempt to distinguish
12228 between an original-namespace-definition and an
12229 extension-namespace-definition at this point. The semantic
12230 analysis routines are responsible for that. */
12231 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12232 identifier = cp_parser_identifier (parser);
12234 identifier = NULL_TREE;
12236 /* Parse any specified attributes. */
12237 attribs = cp_parser_attributes_opt (parser);
12239 /* Look for the `{' to start the namespace. */
12240 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12241 /* Start the namespace. */
12242 push_namespace (identifier);
12244 /* "inline namespace" is equivalent to a stub namespace definition
12245 followed by a strong using directive. */
12248 tree name_space = current_namespace;
12249 /* Set up namespace association. */
12250 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12251 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12252 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12253 /* Import the contents of the inline namespace. */
12255 do_using_directive (name_space);
12256 push_namespace (identifier);
12259 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12261 /* Parse the body of the namespace. */
12262 cp_parser_namespace_body (parser);
12264 #ifdef HANDLE_PRAGMA_VISIBILITY
12265 if (has_visibility)
12269 /* Finish the namespace. */
12271 /* Look for the final `}'. */
12272 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12275 /* Parse a namespace-body.
12278 declaration-seq [opt] */
12281 cp_parser_namespace_body (cp_parser* parser)
12283 cp_parser_declaration_seq_opt (parser);
12286 /* Parse a namespace-alias-definition.
12288 namespace-alias-definition:
12289 namespace identifier = qualified-namespace-specifier ; */
12292 cp_parser_namespace_alias_definition (cp_parser* parser)
12295 tree namespace_specifier;
12297 cp_token *token = cp_lexer_peek_token (parser->lexer);
12299 /* Look for the `namespace' keyword. */
12300 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12301 /* Look for the identifier. */
12302 identifier = cp_parser_identifier (parser);
12303 if (identifier == error_mark_node)
12305 /* Look for the `=' token. */
12306 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12307 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12309 error_at (token->location, "%<namespace%> definition is not allowed here");
12310 /* Skip the definition. */
12311 cp_lexer_consume_token (parser->lexer);
12312 if (cp_parser_skip_to_closing_brace (parser))
12313 cp_lexer_consume_token (parser->lexer);
12316 cp_parser_require (parser, CPP_EQ, "%<=%>");
12317 /* Look for the qualified-namespace-specifier. */
12318 namespace_specifier
12319 = cp_parser_qualified_namespace_specifier (parser);
12320 /* Look for the `;' token. */
12321 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12323 /* Register the alias in the symbol table. */
12324 do_namespace_alias (identifier, namespace_specifier);
12327 /* Parse a qualified-namespace-specifier.
12329 qualified-namespace-specifier:
12330 :: [opt] nested-name-specifier [opt] namespace-name
12332 Returns a NAMESPACE_DECL corresponding to the specified
12336 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12338 /* Look for the optional `::'. */
12339 cp_parser_global_scope_opt (parser,
12340 /*current_scope_valid_p=*/false);
12342 /* Look for the optional nested-name-specifier. */
12343 cp_parser_nested_name_specifier_opt (parser,
12344 /*typename_keyword_p=*/false,
12345 /*check_dependency_p=*/true,
12347 /*is_declaration=*/true);
12349 return cp_parser_namespace_name (parser);
12352 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12353 access declaration.
12356 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12357 using :: unqualified-id ;
12359 access-declaration:
12365 cp_parser_using_declaration (cp_parser* parser,
12366 bool access_declaration_p)
12369 bool typename_p = false;
12370 bool global_scope_p;
12375 if (access_declaration_p)
12376 cp_parser_parse_tentatively (parser);
12379 /* Look for the `using' keyword. */
12380 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12382 /* Peek at the next token. */
12383 token = cp_lexer_peek_token (parser->lexer);
12384 /* See if it's `typename'. */
12385 if (token->keyword == RID_TYPENAME)
12387 /* Remember that we've seen it. */
12389 /* Consume the `typename' token. */
12390 cp_lexer_consume_token (parser->lexer);
12394 /* Look for the optional global scope qualification. */
12396 = (cp_parser_global_scope_opt (parser,
12397 /*current_scope_valid_p=*/false)
12400 /* If we saw `typename', or didn't see `::', then there must be a
12401 nested-name-specifier present. */
12402 if (typename_p || !global_scope_p)
12403 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12404 /*check_dependency_p=*/true,
12406 /*is_declaration=*/true);
12407 /* Otherwise, we could be in either of the two productions. In that
12408 case, treat the nested-name-specifier as optional. */
12410 qscope = cp_parser_nested_name_specifier_opt (parser,
12411 /*typename_keyword_p=*/false,
12412 /*check_dependency_p=*/true,
12414 /*is_declaration=*/true);
12416 qscope = global_namespace;
12418 if (access_declaration_p && cp_parser_error_occurred (parser))
12419 /* Something has already gone wrong; there's no need to parse
12420 further. Since an error has occurred, the return value of
12421 cp_parser_parse_definitely will be false, as required. */
12422 return cp_parser_parse_definitely (parser);
12424 token = cp_lexer_peek_token (parser->lexer);
12425 /* Parse the unqualified-id. */
12426 identifier = cp_parser_unqualified_id (parser,
12427 /*template_keyword_p=*/false,
12428 /*check_dependency_p=*/true,
12429 /*declarator_p=*/true,
12430 /*optional_p=*/false);
12432 if (access_declaration_p)
12434 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12435 cp_parser_simulate_error (parser);
12436 if (!cp_parser_parse_definitely (parser))
12440 /* The function we call to handle a using-declaration is different
12441 depending on what scope we are in. */
12442 if (qscope == error_mark_node || identifier == error_mark_node)
12444 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12445 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12446 /* [namespace.udecl]
12448 A using declaration shall not name a template-id. */
12449 error_at (token->location,
12450 "a template-id may not appear in a using-declaration");
12453 if (at_class_scope_p ())
12455 /* Create the USING_DECL. */
12456 decl = do_class_using_decl (parser->scope, identifier);
12458 if (check_for_bare_parameter_packs (decl))
12461 /* Add it to the list of members in this class. */
12462 finish_member_declaration (decl);
12466 decl = cp_parser_lookup_name_simple (parser,
12469 if (decl == error_mark_node)
12470 cp_parser_name_lookup_error (parser, identifier,
12473 else if (check_for_bare_parameter_packs (decl))
12475 else if (!at_namespace_scope_p ())
12476 do_local_using_decl (decl, qscope, identifier);
12478 do_toplevel_using_decl (decl, qscope, identifier);
12482 /* Look for the final `;'. */
12483 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12488 /* Parse a using-directive.
12491 using namespace :: [opt] nested-name-specifier [opt]
12492 namespace-name ; */
12495 cp_parser_using_directive (cp_parser* parser)
12497 tree namespace_decl;
12500 /* Look for the `using' keyword. */
12501 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12502 /* And the `namespace' keyword. */
12503 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12504 /* Look for the optional `::' operator. */
12505 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12506 /* And the optional nested-name-specifier. */
12507 cp_parser_nested_name_specifier_opt (parser,
12508 /*typename_keyword_p=*/false,
12509 /*check_dependency_p=*/true,
12511 /*is_declaration=*/true);
12512 /* Get the namespace being used. */
12513 namespace_decl = cp_parser_namespace_name (parser);
12514 /* And any specified attributes. */
12515 attribs = cp_parser_attributes_opt (parser);
12516 /* Update the symbol table. */
12517 parse_using_directive (namespace_decl, attribs);
12518 /* Look for the final `;'. */
12519 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12522 /* Parse an asm-definition.
12525 asm ( string-literal ) ;
12530 asm volatile [opt] ( string-literal ) ;
12531 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12532 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12533 : asm-operand-list [opt] ) ;
12534 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12535 : asm-operand-list [opt]
12536 : asm-clobber-list [opt] ) ;
12537 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
12538 : asm-clobber-list [opt]
12539 : asm-goto-list ) ; */
12542 cp_parser_asm_definition (cp_parser* parser)
12545 tree outputs = NULL_TREE;
12546 tree inputs = NULL_TREE;
12547 tree clobbers = NULL_TREE;
12548 tree labels = NULL_TREE;
12550 bool volatile_p = false;
12551 bool extended_p = false;
12552 bool invalid_inputs_p = false;
12553 bool invalid_outputs_p = false;
12554 bool goto_p = false;
12555 const char *missing = NULL;
12557 /* Look for the `asm' keyword. */
12558 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12559 /* See if the next token is `volatile'. */
12560 if (cp_parser_allow_gnu_extensions_p (parser)
12561 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12563 /* Remember that we saw the `volatile' keyword. */
12565 /* Consume the token. */
12566 cp_lexer_consume_token (parser->lexer);
12568 if (cp_parser_allow_gnu_extensions_p (parser)
12569 && parser->in_function_body
12570 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
12572 /* Remember that we saw the `goto' keyword. */
12574 /* Consume the token. */
12575 cp_lexer_consume_token (parser->lexer);
12577 /* Look for the opening `('. */
12578 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12580 /* Look for the string. */
12581 string = cp_parser_string_literal (parser, false, false);
12582 if (string == error_mark_node)
12584 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12585 /*consume_paren=*/true);
12589 /* If we're allowing GNU extensions, check for the extended assembly
12590 syntax. Unfortunately, the `:' tokens need not be separated by
12591 a space in C, and so, for compatibility, we tolerate that here
12592 too. Doing that means that we have to treat the `::' operator as
12594 if (cp_parser_allow_gnu_extensions_p (parser)
12595 && parser->in_function_body
12596 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12597 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12599 bool inputs_p = false;
12600 bool clobbers_p = false;
12601 bool labels_p = false;
12603 /* The extended syntax was used. */
12606 /* Look for outputs. */
12607 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12609 /* Consume the `:'. */
12610 cp_lexer_consume_token (parser->lexer);
12611 /* Parse the output-operands. */
12612 if (cp_lexer_next_token_is_not (parser->lexer,
12614 && cp_lexer_next_token_is_not (parser->lexer,
12616 && cp_lexer_next_token_is_not (parser->lexer,
12619 outputs = cp_parser_asm_operand_list (parser);
12621 if (outputs == error_mark_node)
12622 invalid_outputs_p = true;
12624 /* If the next token is `::', there are no outputs, and the
12625 next token is the beginning of the inputs. */
12626 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12627 /* The inputs are coming next. */
12630 /* Look for inputs. */
12632 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12634 /* Consume the `:' or `::'. */
12635 cp_lexer_consume_token (parser->lexer);
12636 /* Parse the output-operands. */
12637 if (cp_lexer_next_token_is_not (parser->lexer,
12639 && cp_lexer_next_token_is_not (parser->lexer,
12641 && cp_lexer_next_token_is_not (parser->lexer,
12643 inputs = cp_parser_asm_operand_list (parser);
12645 if (inputs == error_mark_node)
12646 invalid_inputs_p = true;
12648 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12649 /* The clobbers are coming next. */
12652 /* Look for clobbers. */
12654 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12657 /* Consume the `:' or `::'. */
12658 cp_lexer_consume_token (parser->lexer);
12659 /* Parse the clobbers. */
12660 if (cp_lexer_next_token_is_not (parser->lexer,
12662 && cp_lexer_next_token_is_not (parser->lexer,
12664 clobbers = cp_parser_asm_clobber_list (parser);
12667 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12668 /* The labels are coming next. */
12671 /* Look for labels. */
12673 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
12676 /* Consume the `:' or `::'. */
12677 cp_lexer_consume_token (parser->lexer);
12678 /* Parse the labels. */
12679 labels = cp_parser_asm_label_list (parser);
12682 if (goto_p && !labels_p)
12683 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
12686 missing = "%<:%> or %<::%>";
12688 /* Look for the closing `)'. */
12689 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
12690 missing ? missing : "%<)%>"))
12691 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12692 /*consume_paren=*/true);
12693 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12695 if (!invalid_inputs_p && !invalid_outputs_p)
12697 /* Create the ASM_EXPR. */
12698 if (parser->in_function_body)
12700 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12701 inputs, clobbers, labels);
12702 /* If the extended syntax was not used, mark the ASM_EXPR. */
12705 tree temp = asm_stmt;
12706 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12707 temp = TREE_OPERAND (temp, 0);
12709 ASM_INPUT_P (temp) = 1;
12713 cgraph_add_asm_node (string);
12717 /* Declarators [gram.dcl.decl] */
12719 /* Parse an init-declarator.
12722 declarator initializer [opt]
12727 declarator asm-specification [opt] attributes [opt] initializer [opt]
12729 function-definition:
12730 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12732 decl-specifier-seq [opt] declarator function-try-block
12736 function-definition:
12737 __extension__ function-definition
12739 The DECL_SPECIFIERS apply to this declarator. Returns a
12740 representation of the entity declared. If MEMBER_P is TRUE, then
12741 this declarator appears in a class scope. The new DECL created by
12742 this declarator is returned.
12744 The CHECKS are access checks that should be performed once we know
12745 what entity is being declared (and, therefore, what classes have
12748 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12749 for a function-definition here as well. If the declarator is a
12750 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12751 be TRUE upon return. By that point, the function-definition will
12752 have been completely parsed.
12754 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12758 cp_parser_init_declarator (cp_parser* parser,
12759 cp_decl_specifier_seq *decl_specifiers,
12760 VEC (deferred_access_check,gc)* checks,
12761 bool function_definition_allowed_p,
12763 int declares_class_or_enum,
12764 bool* function_definition_p)
12766 cp_token *token = NULL, *asm_spec_start_token = NULL,
12767 *attributes_start_token = NULL;
12768 cp_declarator *declarator;
12769 tree prefix_attributes;
12771 tree asm_specification;
12773 tree decl = NULL_TREE;
12775 int is_initialized;
12776 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12777 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12779 enum cpp_ttype initialization_kind;
12780 bool is_direct_init = false;
12781 bool is_non_constant_init;
12782 int ctor_dtor_or_conv_p;
12784 tree pushed_scope = NULL;
12786 /* Gather the attributes that were provided with the
12787 decl-specifiers. */
12788 prefix_attributes = decl_specifiers->attributes;
12790 /* Assume that this is not the declarator for a function
12792 if (function_definition_p)
12793 *function_definition_p = false;
12795 /* Defer access checks while parsing the declarator; we cannot know
12796 what names are accessible until we know what is being
12798 resume_deferring_access_checks ();
12800 /* Parse the declarator. */
12801 token = cp_lexer_peek_token (parser->lexer);
12803 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12804 &ctor_dtor_or_conv_p,
12805 /*parenthesized_p=*/NULL,
12806 /*member_p=*/false);
12807 /* Gather up the deferred checks. */
12808 stop_deferring_access_checks ();
12810 /* If the DECLARATOR was erroneous, there's no need to go
12812 if (declarator == cp_error_declarator)
12813 return error_mark_node;
12815 /* Check that the number of template-parameter-lists is OK. */
12816 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12818 return error_mark_node;
12820 if (declares_class_or_enum & 2)
12821 cp_parser_check_for_definition_in_return_type (declarator,
12822 decl_specifiers->type,
12823 decl_specifiers->type_location);
12825 /* Figure out what scope the entity declared by the DECLARATOR is
12826 located in. `grokdeclarator' sometimes changes the scope, so
12827 we compute it now. */
12828 scope = get_scope_of_declarator (declarator);
12830 /* If we're allowing GNU extensions, look for an asm-specification
12832 if (cp_parser_allow_gnu_extensions_p (parser))
12834 /* Look for an asm-specification. */
12835 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12836 asm_specification = cp_parser_asm_specification_opt (parser);
12837 /* And attributes. */
12838 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12839 attributes = cp_parser_attributes_opt (parser);
12843 asm_specification = NULL_TREE;
12844 attributes = NULL_TREE;
12847 /* Peek at the next token. */
12848 token = cp_lexer_peek_token (parser->lexer);
12849 /* Check to see if the token indicates the start of a
12850 function-definition. */
12851 if (function_declarator_p (declarator)
12852 && cp_parser_token_starts_function_definition_p (token))
12854 if (!function_definition_allowed_p)
12856 /* If a function-definition should not appear here, issue an
12858 cp_parser_error (parser,
12859 "a function-definition is not allowed here");
12860 return error_mark_node;
12864 location_t func_brace_location
12865 = cp_lexer_peek_token (parser->lexer)->location;
12867 /* Neither attributes nor an asm-specification are allowed
12868 on a function-definition. */
12869 if (asm_specification)
12870 error_at (asm_spec_start_token->location,
12871 "an asm-specification is not allowed "
12872 "on a function-definition");
12874 error_at (attributes_start_token->location,
12875 "attributes are not allowed on a function-definition");
12876 /* This is a function-definition. */
12877 *function_definition_p = true;
12879 /* Parse the function definition. */
12881 decl = cp_parser_save_member_function_body (parser,
12884 prefix_attributes);
12887 = (cp_parser_function_definition_from_specifiers_and_declarator
12888 (parser, decl_specifiers, prefix_attributes, declarator));
12890 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12892 /* This is where the prologue starts... */
12893 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12894 = func_brace_location;
12903 Only in function declarations for constructors, destructors, and
12904 type conversions can the decl-specifier-seq be omitted.
12906 We explicitly postpone this check past the point where we handle
12907 function-definitions because we tolerate function-definitions
12908 that are missing their return types in some modes. */
12909 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12911 cp_parser_error (parser,
12912 "expected constructor, destructor, or type conversion");
12913 return error_mark_node;
12916 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12917 if (token->type == CPP_EQ
12918 || token->type == CPP_OPEN_PAREN
12919 || token->type == CPP_OPEN_BRACE)
12921 is_initialized = SD_INITIALIZED;
12922 initialization_kind = token->type;
12924 if (token->type == CPP_EQ
12925 && function_declarator_p (declarator))
12927 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12928 if (t2->keyword == RID_DEFAULT)
12929 is_initialized = SD_DEFAULTED;
12930 else if (t2->keyword == RID_DELETE)
12931 is_initialized = SD_DELETED;
12936 /* If the init-declarator isn't initialized and isn't followed by a
12937 `,' or `;', it's not a valid init-declarator. */
12938 if (token->type != CPP_COMMA
12939 && token->type != CPP_SEMICOLON)
12941 cp_parser_error (parser, "expected initializer");
12942 return error_mark_node;
12944 is_initialized = SD_UNINITIALIZED;
12945 initialization_kind = CPP_EOF;
12948 /* Because start_decl has side-effects, we should only call it if we
12949 know we're going ahead. By this point, we know that we cannot
12950 possibly be looking at any other construct. */
12951 cp_parser_commit_to_tentative_parse (parser);
12953 /* If the decl specifiers were bad, issue an error now that we're
12954 sure this was intended to be a declarator. Then continue
12955 declaring the variable(s), as int, to try to cut down on further
12957 if (decl_specifiers->any_specifiers_p
12958 && decl_specifiers->type == error_mark_node)
12960 cp_parser_error (parser, "invalid type in declaration");
12961 decl_specifiers->type = integer_type_node;
12964 /* Check to see whether or not this declaration is a friend. */
12965 friend_p = cp_parser_friend_p (decl_specifiers);
12967 /* Enter the newly declared entry in the symbol table. If we're
12968 processing a declaration in a class-specifier, we wait until
12969 after processing the initializer. */
12972 if (parser->in_unbraced_linkage_specification_p)
12973 decl_specifiers->storage_class = sc_extern;
12974 decl = start_decl (declarator, decl_specifiers,
12975 is_initialized, attributes, prefix_attributes,
12979 /* Enter the SCOPE. That way unqualified names appearing in the
12980 initializer will be looked up in SCOPE. */
12981 pushed_scope = push_scope (scope);
12983 /* Perform deferred access control checks, now that we know in which
12984 SCOPE the declared entity resides. */
12985 if (!member_p && decl)
12987 tree saved_current_function_decl = NULL_TREE;
12989 /* If the entity being declared is a function, pretend that we
12990 are in its scope. If it is a `friend', it may have access to
12991 things that would not otherwise be accessible. */
12992 if (TREE_CODE (decl) == FUNCTION_DECL)
12994 saved_current_function_decl = current_function_decl;
12995 current_function_decl = decl;
12998 /* Perform access checks for template parameters. */
12999 cp_parser_perform_template_parameter_access_checks (checks);
13001 /* Perform the access control checks for the declarator and the
13002 decl-specifiers. */
13003 perform_deferred_access_checks ();
13005 /* Restore the saved value. */
13006 if (TREE_CODE (decl) == FUNCTION_DECL)
13007 current_function_decl = saved_current_function_decl;
13010 /* Parse the initializer. */
13011 initializer = NULL_TREE;
13012 is_direct_init = false;
13013 is_non_constant_init = true;
13014 if (is_initialized)
13016 if (function_declarator_p (declarator))
13018 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13019 if (initialization_kind == CPP_EQ)
13020 initializer = cp_parser_pure_specifier (parser);
13023 /* If the declaration was erroneous, we don't really
13024 know what the user intended, so just silently
13025 consume the initializer. */
13026 if (decl != error_mark_node)
13027 error_at (initializer_start_token->location,
13028 "initializer provided for function");
13029 cp_parser_skip_to_closing_parenthesis (parser,
13030 /*recovering=*/true,
13031 /*or_comma=*/false,
13032 /*consume_paren=*/true);
13036 initializer = cp_parser_initializer (parser,
13038 &is_non_constant_init);
13041 /* The old parser allows attributes to appear after a parenthesized
13042 initializer. Mark Mitchell proposed removing this functionality
13043 on the GCC mailing lists on 2002-08-13. This parser accepts the
13044 attributes -- but ignores them. */
13045 if (cp_parser_allow_gnu_extensions_p (parser)
13046 && initialization_kind == CPP_OPEN_PAREN)
13047 if (cp_parser_attributes_opt (parser))
13048 warning (OPT_Wattributes,
13049 "attributes after parenthesized initializer ignored");
13051 /* For an in-class declaration, use `grokfield' to create the
13057 pop_scope (pushed_scope);
13058 pushed_scope = false;
13060 decl = grokfield (declarator, decl_specifiers,
13061 initializer, !is_non_constant_init,
13062 /*asmspec=*/NULL_TREE,
13063 prefix_attributes);
13064 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13065 cp_parser_save_default_args (parser, decl);
13068 /* Finish processing the declaration. But, skip friend
13070 if (!friend_p && decl && decl != error_mark_node)
13072 cp_finish_decl (decl,
13073 initializer, !is_non_constant_init,
13075 /* If the initializer is in parentheses, then this is
13076 a direct-initialization, which means that an
13077 `explicit' constructor is OK. Otherwise, an
13078 `explicit' constructor cannot be used. */
13079 ((is_direct_init || !is_initialized)
13080 ? 0 : LOOKUP_ONLYCONVERTING));
13082 else if ((cxx_dialect != cxx98) && friend_p
13083 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13084 /* Core issue #226 (C++0x only): A default template-argument
13085 shall not be specified in a friend class template
13087 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13088 /*is_partial=*/0, /*is_friend_decl=*/1);
13090 if (!friend_p && pushed_scope)
13091 pop_scope (pushed_scope);
13096 /* Parse a declarator.
13100 ptr-operator declarator
13102 abstract-declarator:
13103 ptr-operator abstract-declarator [opt]
13104 direct-abstract-declarator
13109 attributes [opt] direct-declarator
13110 attributes [opt] ptr-operator declarator
13112 abstract-declarator:
13113 attributes [opt] ptr-operator abstract-declarator [opt]
13114 attributes [opt] direct-abstract-declarator
13116 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13117 detect constructor, destructor or conversion operators. It is set
13118 to -1 if the declarator is a name, and +1 if it is a
13119 function. Otherwise it is set to zero. Usually you just want to
13120 test for >0, but internally the negative value is used.
13122 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13123 a decl-specifier-seq unless it declares a constructor, destructor,
13124 or conversion. It might seem that we could check this condition in
13125 semantic analysis, rather than parsing, but that makes it difficult
13126 to handle something like `f()'. We want to notice that there are
13127 no decl-specifiers, and therefore realize that this is an
13128 expression, not a declaration.)
13130 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13131 the declarator is a direct-declarator of the form "(...)".
13133 MEMBER_P is true iff this declarator is a member-declarator. */
13135 static cp_declarator *
13136 cp_parser_declarator (cp_parser* parser,
13137 cp_parser_declarator_kind dcl_kind,
13138 int* ctor_dtor_or_conv_p,
13139 bool* parenthesized_p,
13143 cp_declarator *declarator;
13144 enum tree_code code;
13145 cp_cv_quals cv_quals;
13147 tree attributes = NULL_TREE;
13149 /* Assume this is not a constructor, destructor, or type-conversion
13151 if (ctor_dtor_or_conv_p)
13152 *ctor_dtor_or_conv_p = 0;
13154 if (cp_parser_allow_gnu_extensions_p (parser))
13155 attributes = cp_parser_attributes_opt (parser);
13157 /* Peek at the next token. */
13158 token = cp_lexer_peek_token (parser->lexer);
13160 /* Check for the ptr-operator production. */
13161 cp_parser_parse_tentatively (parser);
13162 /* Parse the ptr-operator. */
13163 code = cp_parser_ptr_operator (parser,
13166 /* If that worked, then we have a ptr-operator. */
13167 if (cp_parser_parse_definitely (parser))
13169 /* If a ptr-operator was found, then this declarator was not
13171 if (parenthesized_p)
13172 *parenthesized_p = true;
13173 /* The dependent declarator is optional if we are parsing an
13174 abstract-declarator. */
13175 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13176 cp_parser_parse_tentatively (parser);
13178 /* Parse the dependent declarator. */
13179 declarator = cp_parser_declarator (parser, dcl_kind,
13180 /*ctor_dtor_or_conv_p=*/NULL,
13181 /*parenthesized_p=*/NULL,
13182 /*member_p=*/false);
13184 /* If we are parsing an abstract-declarator, we must handle the
13185 case where the dependent declarator is absent. */
13186 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13187 && !cp_parser_parse_definitely (parser))
13190 declarator = cp_parser_make_indirect_declarator
13191 (code, class_type, cv_quals, declarator);
13193 /* Everything else is a direct-declarator. */
13196 if (parenthesized_p)
13197 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13199 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13200 ctor_dtor_or_conv_p,
13204 if (attributes && declarator && declarator != cp_error_declarator)
13205 declarator->attributes = attributes;
13210 /* Parse a direct-declarator or direct-abstract-declarator.
13214 direct-declarator ( parameter-declaration-clause )
13215 cv-qualifier-seq [opt]
13216 exception-specification [opt]
13217 direct-declarator [ constant-expression [opt] ]
13220 direct-abstract-declarator:
13221 direct-abstract-declarator [opt]
13222 ( parameter-declaration-clause )
13223 cv-qualifier-seq [opt]
13224 exception-specification [opt]
13225 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13226 ( abstract-declarator )
13228 Returns a representation of the declarator. DCL_KIND is
13229 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13230 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13231 we are parsing a direct-declarator. It is
13232 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13233 of ambiguity we prefer an abstract declarator, as per
13234 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13235 cp_parser_declarator. */
13237 static cp_declarator *
13238 cp_parser_direct_declarator (cp_parser* parser,
13239 cp_parser_declarator_kind dcl_kind,
13240 int* ctor_dtor_or_conv_p,
13244 cp_declarator *declarator = NULL;
13245 tree scope = NULL_TREE;
13246 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13247 bool saved_in_declarator_p = parser->in_declarator_p;
13249 tree pushed_scope = NULL_TREE;
13253 /* Peek at the next token. */
13254 token = cp_lexer_peek_token (parser->lexer);
13255 if (token->type == CPP_OPEN_PAREN)
13257 /* This is either a parameter-declaration-clause, or a
13258 parenthesized declarator. When we know we are parsing a
13259 named declarator, it must be a parenthesized declarator
13260 if FIRST is true. For instance, `(int)' is a
13261 parameter-declaration-clause, with an omitted
13262 direct-abstract-declarator. But `((*))', is a
13263 parenthesized abstract declarator. Finally, when T is a
13264 template parameter `(T)' is a
13265 parameter-declaration-clause, and not a parenthesized
13268 We first try and parse a parameter-declaration-clause,
13269 and then try a nested declarator (if FIRST is true).
13271 It is not an error for it not to be a
13272 parameter-declaration-clause, even when FIRST is
13278 The first is the declaration of a function while the
13279 second is the definition of a variable, including its
13282 Having seen only the parenthesis, we cannot know which of
13283 these two alternatives should be selected. Even more
13284 complex are examples like:
13289 The former is a function-declaration; the latter is a
13290 variable initialization.
13292 Thus again, we try a parameter-declaration-clause, and if
13293 that fails, we back out and return. */
13295 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13298 unsigned saved_num_template_parameter_lists;
13299 bool is_declarator = false;
13302 /* In a member-declarator, the only valid interpretation
13303 of a parenthesis is the start of a
13304 parameter-declaration-clause. (It is invalid to
13305 initialize a static data member with a parenthesized
13306 initializer; only the "=" form of initialization is
13309 cp_parser_parse_tentatively (parser);
13311 /* Consume the `('. */
13312 cp_lexer_consume_token (parser->lexer);
13315 /* If this is going to be an abstract declarator, we're
13316 in a declarator and we can't have default args. */
13317 parser->default_arg_ok_p = false;
13318 parser->in_declarator_p = true;
13321 /* Inside the function parameter list, surrounding
13322 template-parameter-lists do not apply. */
13323 saved_num_template_parameter_lists
13324 = parser->num_template_parameter_lists;
13325 parser->num_template_parameter_lists = 0;
13327 begin_scope (sk_function_parms, NULL_TREE);
13329 /* Parse the parameter-declaration-clause. */
13330 params = cp_parser_parameter_declaration_clause (parser);
13332 parser->num_template_parameter_lists
13333 = saved_num_template_parameter_lists;
13335 /* If all went well, parse the cv-qualifier-seq and the
13336 exception-specification. */
13337 if (member_p || cp_parser_parse_definitely (parser))
13339 cp_cv_quals cv_quals;
13340 tree exception_specification;
13343 is_declarator = true;
13345 if (ctor_dtor_or_conv_p)
13346 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13348 /* Consume the `)'. */
13349 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13351 /* Parse the cv-qualifier-seq. */
13352 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13353 /* And the exception-specification. */
13354 exception_specification
13355 = cp_parser_exception_specification_opt (parser);
13358 = cp_parser_late_return_type_opt (parser);
13360 /* Create the function-declarator. */
13361 declarator = make_call_declarator (declarator,
13364 exception_specification,
13366 /* Any subsequent parameter lists are to do with
13367 return type, so are not those of the declared
13369 parser->default_arg_ok_p = false;
13372 /* Remove the function parms from scope. */
13373 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13374 pop_binding (DECL_NAME (t), t);
13378 /* Repeat the main loop. */
13382 /* If this is the first, we can try a parenthesized
13386 bool saved_in_type_id_in_expr_p;
13388 parser->default_arg_ok_p = saved_default_arg_ok_p;
13389 parser->in_declarator_p = saved_in_declarator_p;
13391 /* Consume the `('. */
13392 cp_lexer_consume_token (parser->lexer);
13393 /* Parse the nested declarator. */
13394 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13395 parser->in_type_id_in_expr_p = true;
13397 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13398 /*parenthesized_p=*/NULL,
13400 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13402 /* Expect a `)'. */
13403 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13404 declarator = cp_error_declarator;
13405 if (declarator == cp_error_declarator)
13408 goto handle_declarator;
13410 /* Otherwise, we must be done. */
13414 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13415 && token->type == CPP_OPEN_SQUARE)
13417 /* Parse an array-declarator. */
13420 if (ctor_dtor_or_conv_p)
13421 *ctor_dtor_or_conv_p = 0;
13424 parser->default_arg_ok_p = false;
13425 parser->in_declarator_p = true;
13426 /* Consume the `['. */
13427 cp_lexer_consume_token (parser->lexer);
13428 /* Peek at the next token. */
13429 token = cp_lexer_peek_token (parser->lexer);
13430 /* If the next token is `]', then there is no
13431 constant-expression. */
13432 if (token->type != CPP_CLOSE_SQUARE)
13434 bool non_constant_p;
13437 = cp_parser_constant_expression (parser,
13438 /*allow_non_constant=*/true,
13440 if (!non_constant_p)
13441 bounds = fold_non_dependent_expr (bounds);
13442 /* Normally, the array bound must be an integral constant
13443 expression. However, as an extension, we allow VLAs
13444 in function scopes. */
13445 else if (!parser->in_function_body)
13447 error_at (token->location,
13448 "array bound is not an integer constant");
13449 bounds = error_mark_node;
13451 else if (processing_template_decl && !error_operand_p (bounds))
13453 /* Remember this wasn't a constant-expression. */
13454 bounds = build_nop (TREE_TYPE (bounds), bounds);
13455 TREE_SIDE_EFFECTS (bounds) = 1;
13459 bounds = NULL_TREE;
13460 /* Look for the closing `]'. */
13461 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13463 declarator = cp_error_declarator;
13467 declarator = make_array_declarator (declarator, bounds);
13469 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13472 tree qualifying_scope;
13473 tree unqualified_name;
13474 special_function_kind sfk;
13476 bool pack_expansion_p = false;
13477 cp_token *declarator_id_start_token;
13479 /* Parse a declarator-id */
13480 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13483 cp_parser_parse_tentatively (parser);
13485 /* If we see an ellipsis, we should be looking at a
13487 if (token->type == CPP_ELLIPSIS)
13489 /* Consume the `...' */
13490 cp_lexer_consume_token (parser->lexer);
13492 pack_expansion_p = true;
13496 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13498 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13499 qualifying_scope = parser->scope;
13504 if (!unqualified_name && pack_expansion_p)
13506 /* Check whether an error occurred. */
13507 okay = !cp_parser_error_occurred (parser);
13509 /* We already consumed the ellipsis to mark a
13510 parameter pack, but we have no way to report it,
13511 so abort the tentative parse. We will be exiting
13512 immediately anyway. */
13513 cp_parser_abort_tentative_parse (parser);
13516 okay = cp_parser_parse_definitely (parser);
13519 unqualified_name = error_mark_node;
13520 else if (unqualified_name
13521 && (qualifying_scope
13522 || (TREE_CODE (unqualified_name)
13523 != IDENTIFIER_NODE)))
13525 cp_parser_error (parser, "expected unqualified-id");
13526 unqualified_name = error_mark_node;
13530 if (!unqualified_name)
13532 if (unqualified_name == error_mark_node)
13534 declarator = cp_error_declarator;
13535 pack_expansion_p = false;
13536 declarator->parameter_pack_p = false;
13540 if (qualifying_scope && at_namespace_scope_p ()
13541 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13543 /* In the declaration of a member of a template class
13544 outside of the class itself, the SCOPE will sometimes
13545 be a TYPENAME_TYPE. For example, given:
13547 template <typename T>
13548 int S<T>::R::i = 3;
13550 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13551 this context, we must resolve S<T>::R to an ordinary
13552 type, rather than a typename type.
13554 The reason we normally avoid resolving TYPENAME_TYPEs
13555 is that a specialization of `S' might render
13556 `S<T>::R' not a type. However, if `S' is
13557 specialized, then this `i' will not be used, so there
13558 is no harm in resolving the types here. */
13561 /* Resolve the TYPENAME_TYPE. */
13562 type = resolve_typename_type (qualifying_scope,
13563 /*only_current_p=*/false);
13564 /* If that failed, the declarator is invalid. */
13565 if (TREE_CODE (type) == TYPENAME_TYPE)
13566 error_at (declarator_id_start_token->location,
13567 "%<%T::%E%> is not a type",
13568 TYPE_CONTEXT (qualifying_scope),
13569 TYPE_IDENTIFIER (qualifying_scope));
13570 qualifying_scope = type;
13575 if (unqualified_name)
13579 if (qualifying_scope
13580 && CLASS_TYPE_P (qualifying_scope))
13581 class_type = qualifying_scope;
13583 class_type = current_class_type;
13585 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13587 tree name_type = TREE_TYPE (unqualified_name);
13588 if (class_type && same_type_p (name_type, class_type))
13590 if (qualifying_scope
13591 && CLASSTYPE_USE_TEMPLATE (name_type))
13593 error_at (declarator_id_start_token->location,
13594 "invalid use of constructor as a template");
13595 inform (declarator_id_start_token->location,
13596 "use %<%T::%D%> instead of %<%T::%D%> to "
13597 "name the constructor in a qualified name",
13599 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13600 class_type, name_type);
13601 declarator = cp_error_declarator;
13605 unqualified_name = constructor_name (class_type);
13609 /* We do not attempt to print the declarator
13610 here because we do not have enough
13611 information about its original syntactic
13613 cp_parser_error (parser, "invalid declarator");
13614 declarator = cp_error_declarator;
13621 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13622 sfk = sfk_destructor;
13623 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13624 sfk = sfk_conversion;
13625 else if (/* There's no way to declare a constructor
13626 for an anonymous type, even if the type
13627 got a name for linkage purposes. */
13628 !TYPE_WAS_ANONYMOUS (class_type)
13629 && constructor_name_p (unqualified_name,
13632 unqualified_name = constructor_name (class_type);
13633 sfk = sfk_constructor;
13636 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13637 *ctor_dtor_or_conv_p = -1;
13640 declarator = make_id_declarator (qualifying_scope,
13643 declarator->id_loc = token->location;
13644 declarator->parameter_pack_p = pack_expansion_p;
13646 if (pack_expansion_p)
13647 maybe_warn_variadic_templates ();
13650 handle_declarator:;
13651 scope = get_scope_of_declarator (declarator);
13653 /* Any names that appear after the declarator-id for a
13654 member are looked up in the containing scope. */
13655 pushed_scope = push_scope (scope);
13656 parser->in_declarator_p = true;
13657 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13658 || (declarator && declarator->kind == cdk_id))
13659 /* Default args are only allowed on function
13661 parser->default_arg_ok_p = saved_default_arg_ok_p;
13663 parser->default_arg_ok_p = false;
13672 /* For an abstract declarator, we might wind up with nothing at this
13673 point. That's an error; the declarator is not optional. */
13675 cp_parser_error (parser, "expected declarator");
13677 /* If we entered a scope, we must exit it now. */
13679 pop_scope (pushed_scope);
13681 parser->default_arg_ok_p = saved_default_arg_ok_p;
13682 parser->in_declarator_p = saved_in_declarator_p;
13687 /* Parse a ptr-operator.
13690 * cv-qualifier-seq [opt]
13692 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13697 & cv-qualifier-seq [opt]
13699 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13700 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13701 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13702 filled in with the TYPE containing the member. *CV_QUALS is
13703 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13704 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13705 Note that the tree codes returned by this function have nothing
13706 to do with the types of trees that will be eventually be created
13707 to represent the pointer or reference type being parsed. They are
13708 just constants with suggestive names. */
13709 static enum tree_code
13710 cp_parser_ptr_operator (cp_parser* parser,
13712 cp_cv_quals *cv_quals)
13714 enum tree_code code = ERROR_MARK;
13717 /* Assume that it's not a pointer-to-member. */
13719 /* And that there are no cv-qualifiers. */
13720 *cv_quals = TYPE_UNQUALIFIED;
13722 /* Peek at the next token. */
13723 token = cp_lexer_peek_token (parser->lexer);
13725 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13726 if (token->type == CPP_MULT)
13727 code = INDIRECT_REF;
13728 else if (token->type == CPP_AND)
13730 else if ((cxx_dialect != cxx98) &&
13731 token->type == CPP_AND_AND) /* C++0x only */
13732 code = NON_LVALUE_EXPR;
13734 if (code != ERROR_MARK)
13736 /* Consume the `*', `&' or `&&'. */
13737 cp_lexer_consume_token (parser->lexer);
13739 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13740 `&', if we are allowing GNU extensions. (The only qualifier
13741 that can legally appear after `&' is `restrict', but that is
13742 enforced during semantic analysis. */
13743 if (code == INDIRECT_REF
13744 || cp_parser_allow_gnu_extensions_p (parser))
13745 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13749 /* Try the pointer-to-member case. */
13750 cp_parser_parse_tentatively (parser);
13751 /* Look for the optional `::' operator. */
13752 cp_parser_global_scope_opt (parser,
13753 /*current_scope_valid_p=*/false);
13754 /* Look for the nested-name specifier. */
13755 token = cp_lexer_peek_token (parser->lexer);
13756 cp_parser_nested_name_specifier (parser,
13757 /*typename_keyword_p=*/false,
13758 /*check_dependency_p=*/true,
13760 /*is_declaration=*/false);
13761 /* If we found it, and the next token is a `*', then we are
13762 indeed looking at a pointer-to-member operator. */
13763 if (!cp_parser_error_occurred (parser)
13764 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13766 /* Indicate that the `*' operator was used. */
13767 code = INDIRECT_REF;
13769 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13770 error_at (token->location, "%qD is a namespace", parser->scope);
13773 /* The type of which the member is a member is given by the
13775 *type = parser->scope;
13776 /* The next name will not be qualified. */
13777 parser->scope = NULL_TREE;
13778 parser->qualifying_scope = NULL_TREE;
13779 parser->object_scope = NULL_TREE;
13780 /* Look for the optional cv-qualifier-seq. */
13781 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13784 /* If that didn't work we don't have a ptr-operator. */
13785 if (!cp_parser_parse_definitely (parser))
13786 cp_parser_error (parser, "expected ptr-operator");
13792 /* Parse an (optional) cv-qualifier-seq.
13795 cv-qualifier cv-qualifier-seq [opt]
13806 Returns a bitmask representing the cv-qualifiers. */
13809 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13811 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13816 cp_cv_quals cv_qualifier;
13818 /* Peek at the next token. */
13819 token = cp_lexer_peek_token (parser->lexer);
13820 /* See if it's a cv-qualifier. */
13821 switch (token->keyword)
13824 cv_qualifier = TYPE_QUAL_CONST;
13828 cv_qualifier = TYPE_QUAL_VOLATILE;
13832 cv_qualifier = TYPE_QUAL_RESTRICT;
13836 cv_qualifier = TYPE_UNQUALIFIED;
13843 if (cv_quals & cv_qualifier)
13845 error_at (token->location, "duplicate cv-qualifier");
13846 cp_lexer_purge_token (parser->lexer);
13850 cp_lexer_consume_token (parser->lexer);
13851 cv_quals |= cv_qualifier;
13858 /* Parse a late-specified return type, if any. This is not a separate
13859 non-terminal, but part of a function declarator, which looks like
13863 Returns the type indicated by the type-id. */
13866 cp_parser_late_return_type_opt (cp_parser* parser)
13870 /* Peek at the next token. */
13871 token = cp_lexer_peek_token (parser->lexer);
13872 /* A late-specified return type is indicated by an initial '->'. */
13873 if (token->type != CPP_DEREF)
13876 /* Consume the ->. */
13877 cp_lexer_consume_token (parser->lexer);
13879 return cp_parser_type_id (parser);
13882 /* Parse a declarator-id.
13886 :: [opt] nested-name-specifier [opt] type-name
13888 In the `id-expression' case, the value returned is as for
13889 cp_parser_id_expression if the id-expression was an unqualified-id.
13890 If the id-expression was a qualified-id, then a SCOPE_REF is
13891 returned. The first operand is the scope (either a NAMESPACE_DECL
13892 or TREE_TYPE), but the second is still just a representation of an
13896 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13899 /* The expression must be an id-expression. Assume that qualified
13900 names are the names of types so that:
13903 int S<T>::R::i = 3;
13905 will work; we must treat `S<T>::R' as the name of a type.
13906 Similarly, assume that qualified names are templates, where
13910 int S<T>::R<T>::i = 3;
13913 id = cp_parser_id_expression (parser,
13914 /*template_keyword_p=*/false,
13915 /*check_dependency_p=*/false,
13916 /*template_p=*/NULL,
13917 /*declarator_p=*/true,
13919 if (id && BASELINK_P (id))
13920 id = BASELINK_FUNCTIONS (id);
13924 /* Parse a type-id.
13927 type-specifier-seq abstract-declarator [opt]
13929 Returns the TYPE specified. */
13932 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13934 cp_decl_specifier_seq type_specifier_seq;
13935 cp_declarator *abstract_declarator;
13937 /* Parse the type-specifier-seq. */
13938 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13939 &type_specifier_seq);
13940 if (type_specifier_seq.type == error_mark_node)
13941 return error_mark_node;
13943 /* There might or might not be an abstract declarator. */
13944 cp_parser_parse_tentatively (parser);
13945 /* Look for the declarator. */
13946 abstract_declarator
13947 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13948 /*parenthesized_p=*/NULL,
13949 /*member_p=*/false);
13950 /* Check to see if there really was a declarator. */
13951 if (!cp_parser_parse_definitely (parser))
13952 abstract_declarator = NULL;
13954 if (type_specifier_seq.type
13955 && type_uses_auto (type_specifier_seq.type))
13957 /* A type-id with type 'auto' is only ok if the abstract declarator
13958 is a function declarator with a late-specified return type. */
13959 if (abstract_declarator
13960 && abstract_declarator->kind == cdk_function
13961 && abstract_declarator->u.function.late_return_type)
13965 error ("invalid use of %<auto%>");
13966 return error_mark_node;
13970 return groktypename (&type_specifier_seq, abstract_declarator,
13974 static tree cp_parser_type_id (cp_parser *parser)
13976 return cp_parser_type_id_1 (parser, false);
13979 static tree cp_parser_template_type_arg (cp_parser *parser)
13981 return cp_parser_type_id_1 (parser, true);
13984 /* Parse a type-specifier-seq.
13986 type-specifier-seq:
13987 type-specifier type-specifier-seq [opt]
13991 type-specifier-seq:
13992 attributes type-specifier-seq [opt]
13994 If IS_CONDITION is true, we are at the start of a "condition",
13995 e.g., we've just seen "if (".
13997 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14000 cp_parser_type_specifier_seq (cp_parser* parser,
14002 cp_decl_specifier_seq *type_specifier_seq)
14004 bool seen_type_specifier = false;
14005 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14006 cp_token *start_token = NULL;
14008 /* Clear the TYPE_SPECIFIER_SEQ. */
14009 clear_decl_specs (type_specifier_seq);
14011 /* Parse the type-specifiers and attributes. */
14014 tree type_specifier;
14015 bool is_cv_qualifier;
14017 /* Check for attributes first. */
14018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14020 type_specifier_seq->attributes =
14021 chainon (type_specifier_seq->attributes,
14022 cp_parser_attributes_opt (parser));
14026 /* record the token of the beginning of the type specifier seq,
14027 for error reporting purposes*/
14029 start_token = cp_lexer_peek_token (parser->lexer);
14031 /* Look for the type-specifier. */
14032 type_specifier = cp_parser_type_specifier (parser,
14034 type_specifier_seq,
14035 /*is_declaration=*/false,
14038 if (!type_specifier)
14040 /* If the first type-specifier could not be found, this is not a
14041 type-specifier-seq at all. */
14042 if (!seen_type_specifier)
14044 cp_parser_error (parser, "expected type-specifier");
14045 type_specifier_seq->type = error_mark_node;
14048 /* If subsequent type-specifiers could not be found, the
14049 type-specifier-seq is complete. */
14053 seen_type_specifier = true;
14054 /* The standard says that a condition can be:
14056 type-specifier-seq declarator = assignment-expression
14063 we should treat the "S" as a declarator, not as a
14064 type-specifier. The standard doesn't say that explicitly for
14065 type-specifier-seq, but it does say that for
14066 decl-specifier-seq in an ordinary declaration. Perhaps it
14067 would be clearer just to allow a decl-specifier-seq here, and
14068 then add a semantic restriction that if any decl-specifiers
14069 that are not type-specifiers appear, the program is invalid. */
14070 if (is_condition && !is_cv_qualifier)
14071 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14074 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14077 /* Parse a parameter-declaration-clause.
14079 parameter-declaration-clause:
14080 parameter-declaration-list [opt] ... [opt]
14081 parameter-declaration-list , ...
14083 Returns a representation for the parameter declarations. A return
14084 value of NULL indicates a parameter-declaration-clause consisting
14085 only of an ellipsis. */
14088 cp_parser_parameter_declaration_clause (cp_parser* parser)
14095 /* Peek at the next token. */
14096 token = cp_lexer_peek_token (parser->lexer);
14097 /* Check for trivial parameter-declaration-clauses. */
14098 if (token->type == CPP_ELLIPSIS)
14100 /* Consume the `...' token. */
14101 cp_lexer_consume_token (parser->lexer);
14104 else if (token->type == CPP_CLOSE_PAREN)
14105 /* There are no parameters. */
14107 #ifndef NO_IMPLICIT_EXTERN_C
14108 if (in_system_header && current_class_type == NULL
14109 && current_lang_name == lang_name_c)
14113 return void_list_node;
14115 /* Check for `(void)', too, which is a special case. */
14116 else if (token->keyword == RID_VOID
14117 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14118 == CPP_CLOSE_PAREN))
14120 /* Consume the `void' token. */
14121 cp_lexer_consume_token (parser->lexer);
14122 /* There are no parameters. */
14123 return void_list_node;
14126 /* Parse the parameter-declaration-list. */
14127 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14128 /* If a parse error occurred while parsing the
14129 parameter-declaration-list, then the entire
14130 parameter-declaration-clause is erroneous. */
14134 /* Peek at the next token. */
14135 token = cp_lexer_peek_token (parser->lexer);
14136 /* If it's a `,', the clause should terminate with an ellipsis. */
14137 if (token->type == CPP_COMMA)
14139 /* Consume the `,'. */
14140 cp_lexer_consume_token (parser->lexer);
14141 /* Expect an ellipsis. */
14143 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14145 /* It might also be `...' if the optional trailing `,' was
14147 else if (token->type == CPP_ELLIPSIS)
14149 /* Consume the `...' token. */
14150 cp_lexer_consume_token (parser->lexer);
14151 /* And remember that we saw it. */
14155 ellipsis_p = false;
14157 /* Finish the parameter list. */
14159 parameters = chainon (parameters, void_list_node);
14164 /* Parse a parameter-declaration-list.
14166 parameter-declaration-list:
14167 parameter-declaration
14168 parameter-declaration-list , parameter-declaration
14170 Returns a representation of the parameter-declaration-list, as for
14171 cp_parser_parameter_declaration_clause. However, the
14172 `void_list_node' is never appended to the list. Upon return,
14173 *IS_ERROR will be true iff an error occurred. */
14176 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14178 tree parameters = NULL_TREE;
14179 tree *tail = ¶meters;
14180 bool saved_in_unbraced_linkage_specification_p;
14183 /* Assume all will go well. */
14185 /* The special considerations that apply to a function within an
14186 unbraced linkage specifications do not apply to the parameters
14187 to the function. */
14188 saved_in_unbraced_linkage_specification_p
14189 = parser->in_unbraced_linkage_specification_p;
14190 parser->in_unbraced_linkage_specification_p = false;
14192 /* Look for more parameters. */
14195 cp_parameter_declarator *parameter;
14196 tree decl = error_mark_node;
14197 bool parenthesized_p;
14198 /* Parse the parameter. */
14200 = cp_parser_parameter_declaration (parser,
14201 /*template_parm_p=*/false,
14204 /* We don't know yet if the enclosing context is deprecated, so wait
14205 and warn in grokparms if appropriate. */
14206 deprecated_state = DEPRECATED_SUPPRESS;
14209 decl = grokdeclarator (parameter->declarator,
14210 ¶meter->decl_specifiers,
14212 parameter->default_argument != NULL_TREE,
14213 ¶meter->decl_specifiers.attributes);
14215 deprecated_state = DEPRECATED_NORMAL;
14217 /* If a parse error occurred parsing the parameter declaration,
14218 then the entire parameter-declaration-list is erroneous. */
14219 if (decl == error_mark_node)
14222 parameters = error_mark_node;
14226 if (parameter->decl_specifiers.attributes)
14227 cplus_decl_attributes (&decl,
14228 parameter->decl_specifiers.attributes,
14230 if (DECL_NAME (decl))
14231 decl = pushdecl (decl);
14233 if (decl != error_mark_node)
14235 retrofit_lang_decl (decl);
14236 DECL_PARM_INDEX (decl) = ++index;
14239 /* Add the new parameter to the list. */
14240 *tail = build_tree_list (parameter->default_argument, decl);
14241 tail = &TREE_CHAIN (*tail);
14243 /* Peek at the next token. */
14244 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14245 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14246 /* These are for Objective-C++ */
14247 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14248 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14249 /* The parameter-declaration-list is complete. */
14251 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14255 /* Peek at the next token. */
14256 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14257 /* If it's an ellipsis, then the list is complete. */
14258 if (token->type == CPP_ELLIPSIS)
14260 /* Otherwise, there must be more parameters. Consume the
14262 cp_lexer_consume_token (parser->lexer);
14263 /* When parsing something like:
14265 int i(float f, double d)
14267 we can tell after seeing the declaration for "f" that we
14268 are not looking at an initialization of a variable "i",
14269 but rather at the declaration of a function "i".
14271 Due to the fact that the parsing of template arguments
14272 (as specified to a template-id) requires backtracking we
14273 cannot use this technique when inside a template argument
14275 if (!parser->in_template_argument_list_p
14276 && !parser->in_type_id_in_expr_p
14277 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14278 /* However, a parameter-declaration of the form
14279 "foat(f)" (which is a valid declaration of a
14280 parameter "f") can also be interpreted as an
14281 expression (the conversion of "f" to "float"). */
14282 && !parenthesized_p)
14283 cp_parser_commit_to_tentative_parse (parser);
14287 cp_parser_error (parser, "expected %<,%> or %<...%>");
14288 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14289 cp_parser_skip_to_closing_parenthesis (parser,
14290 /*recovering=*/true,
14291 /*or_comma=*/false,
14292 /*consume_paren=*/false);
14297 parser->in_unbraced_linkage_specification_p
14298 = saved_in_unbraced_linkage_specification_p;
14303 /* Parse a parameter declaration.
14305 parameter-declaration:
14306 decl-specifier-seq ... [opt] declarator
14307 decl-specifier-seq declarator = assignment-expression
14308 decl-specifier-seq ... [opt] abstract-declarator [opt]
14309 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14311 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14312 declares a template parameter. (In that case, a non-nested `>'
14313 token encountered during the parsing of the assignment-expression
14314 is not interpreted as a greater-than operator.)
14316 Returns a representation of the parameter, or NULL if an error
14317 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14318 true iff the declarator is of the form "(p)". */
14320 static cp_parameter_declarator *
14321 cp_parser_parameter_declaration (cp_parser *parser,
14322 bool template_parm_p,
14323 bool *parenthesized_p)
14325 int declares_class_or_enum;
14326 bool greater_than_is_operator_p;
14327 cp_decl_specifier_seq decl_specifiers;
14328 cp_declarator *declarator;
14329 tree default_argument;
14330 cp_token *token = NULL, *declarator_token_start = NULL;
14331 const char *saved_message;
14333 /* In a template parameter, `>' is not an operator.
14337 When parsing a default template-argument for a non-type
14338 template-parameter, the first non-nested `>' is taken as the end
14339 of the template parameter-list rather than a greater-than
14341 greater_than_is_operator_p = !template_parm_p;
14343 /* Type definitions may not appear in parameter types. */
14344 saved_message = parser->type_definition_forbidden_message;
14345 parser->type_definition_forbidden_message
14346 = "types may not be defined in parameter types";
14348 /* Parse the declaration-specifiers. */
14349 cp_parser_decl_specifier_seq (parser,
14350 CP_PARSER_FLAGS_NONE,
14352 &declares_class_or_enum);
14353 /* If an error occurred, there's no reason to attempt to parse the
14354 rest of the declaration. */
14355 if (cp_parser_error_occurred (parser))
14357 parser->type_definition_forbidden_message = saved_message;
14361 /* Peek at the next token. */
14362 token = cp_lexer_peek_token (parser->lexer);
14364 /* If the next token is a `)', `,', `=', `>', or `...', then there
14365 is no declarator. However, when variadic templates are enabled,
14366 there may be a declarator following `...'. */
14367 if (token->type == CPP_CLOSE_PAREN
14368 || token->type == CPP_COMMA
14369 || token->type == CPP_EQ
14370 || token->type == CPP_GREATER)
14373 if (parenthesized_p)
14374 *parenthesized_p = false;
14376 /* Otherwise, there should be a declarator. */
14379 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14380 parser->default_arg_ok_p = false;
14382 /* After seeing a decl-specifier-seq, if the next token is not a
14383 "(", there is no possibility that the code is a valid
14384 expression. Therefore, if parsing tentatively, we commit at
14386 if (!parser->in_template_argument_list_p
14387 /* In an expression context, having seen:
14391 we cannot be sure whether we are looking at a
14392 function-type (taking a "char" as a parameter) or a cast
14393 of some object of type "char" to "int". */
14394 && !parser->in_type_id_in_expr_p
14395 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14396 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14397 cp_parser_commit_to_tentative_parse (parser);
14398 /* Parse the declarator. */
14399 declarator_token_start = token;
14400 declarator = cp_parser_declarator (parser,
14401 CP_PARSER_DECLARATOR_EITHER,
14402 /*ctor_dtor_or_conv_p=*/NULL,
14404 /*member_p=*/false);
14405 parser->default_arg_ok_p = saved_default_arg_ok_p;
14406 /* After the declarator, allow more attributes. */
14407 decl_specifiers.attributes
14408 = chainon (decl_specifiers.attributes,
14409 cp_parser_attributes_opt (parser));
14412 /* If the next token is an ellipsis, and we have not seen a
14413 declarator name, and the type of the declarator contains parameter
14414 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14415 a parameter pack expansion expression. Otherwise, leave the
14416 ellipsis for a C-style variadic function. */
14417 token = cp_lexer_peek_token (parser->lexer);
14418 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14420 tree type = decl_specifiers.type;
14422 if (type && DECL_P (type))
14423 type = TREE_TYPE (type);
14426 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14427 && declarator_can_be_parameter_pack (declarator)
14428 && (!declarator || !declarator->parameter_pack_p)
14429 && uses_parameter_packs (type))
14431 /* Consume the `...'. */
14432 cp_lexer_consume_token (parser->lexer);
14433 maybe_warn_variadic_templates ();
14435 /* Build a pack expansion type */
14437 declarator->parameter_pack_p = true;
14439 decl_specifiers.type = make_pack_expansion (type);
14443 /* The restriction on defining new types applies only to the type
14444 of the parameter, not to the default argument. */
14445 parser->type_definition_forbidden_message = saved_message;
14447 /* If the next token is `=', then process a default argument. */
14448 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14450 /* Consume the `='. */
14451 cp_lexer_consume_token (parser->lexer);
14453 /* If we are defining a class, then the tokens that make up the
14454 default argument must be saved and processed later. */
14455 if (!template_parm_p && at_class_scope_p ()
14456 && TYPE_BEING_DEFINED (current_class_type))
14458 unsigned depth = 0;
14459 int maybe_template_id = 0;
14460 cp_token *first_token;
14463 /* Add tokens until we have processed the entire default
14464 argument. We add the range [first_token, token). */
14465 first_token = cp_lexer_peek_token (parser->lexer);
14470 /* Peek at the next token. */
14471 token = cp_lexer_peek_token (parser->lexer);
14472 /* What we do depends on what token we have. */
14473 switch (token->type)
14475 /* In valid code, a default argument must be
14476 immediately followed by a `,' `)', or `...'. */
14478 if (depth == 0 && maybe_template_id)
14480 /* If we've seen a '<', we might be in a
14481 template-argument-list. Until Core issue 325 is
14482 resolved, we don't know how this situation ought
14483 to be handled, so try to DTRT. We check whether
14484 what comes after the comma is a valid parameter
14485 declaration list. If it is, then the comma ends
14486 the default argument; otherwise the default
14487 argument continues. */
14488 bool error = false;
14490 /* Set ITALP so cp_parser_parameter_declaration_list
14491 doesn't decide to commit to this parse. */
14492 bool saved_italp = parser->in_template_argument_list_p;
14493 parser->in_template_argument_list_p = true;
14495 cp_parser_parse_tentatively (parser);
14496 cp_lexer_consume_token (parser->lexer);
14497 cp_parser_parameter_declaration_list (parser, &error);
14498 if (!cp_parser_error_occurred (parser) && !error)
14500 cp_parser_abort_tentative_parse (parser);
14502 parser->in_template_argument_list_p = saved_italp;
14505 case CPP_CLOSE_PAREN:
14507 /* If we run into a non-nested `;', `}', or `]',
14508 then the code is invalid -- but the default
14509 argument is certainly over. */
14510 case CPP_SEMICOLON:
14511 case CPP_CLOSE_BRACE:
14512 case CPP_CLOSE_SQUARE:
14515 /* Update DEPTH, if necessary. */
14516 else if (token->type == CPP_CLOSE_PAREN
14517 || token->type == CPP_CLOSE_BRACE
14518 || token->type == CPP_CLOSE_SQUARE)
14522 case CPP_OPEN_PAREN:
14523 case CPP_OPEN_SQUARE:
14524 case CPP_OPEN_BRACE:
14530 /* This might be the comparison operator, or it might
14531 start a template argument list. */
14532 ++maybe_template_id;
14536 if (cxx_dialect == cxx98)
14538 /* Fall through for C++0x, which treats the `>>'
14539 operator like two `>' tokens in certain
14545 /* This might be an operator, or it might close a
14546 template argument list. But if a previous '<'
14547 started a template argument list, this will have
14548 closed it, so we can't be in one anymore. */
14549 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14550 if (maybe_template_id < 0)
14551 maybe_template_id = 0;
14555 /* If we run out of tokens, issue an error message. */
14557 case CPP_PRAGMA_EOL:
14558 error_at (token->location, "file ends in default argument");
14564 /* In these cases, we should look for template-ids.
14565 For example, if the default argument is
14566 `X<int, double>()', we need to do name lookup to
14567 figure out whether or not `X' is a template; if
14568 so, the `,' does not end the default argument.
14570 That is not yet done. */
14577 /* If we've reached the end, stop. */
14581 /* Add the token to the token block. */
14582 token = cp_lexer_consume_token (parser->lexer);
14585 /* Create a DEFAULT_ARG to represent the unparsed default
14587 default_argument = make_node (DEFAULT_ARG);
14588 DEFARG_TOKENS (default_argument)
14589 = cp_token_cache_new (first_token, token);
14590 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14592 /* Outside of a class definition, we can just parse the
14593 assignment-expression. */
14596 token = cp_lexer_peek_token (parser->lexer);
14598 = cp_parser_default_argument (parser, template_parm_p);
14601 if (!parser->default_arg_ok_p)
14603 if (flag_permissive)
14604 warning (0, "deprecated use of default argument for parameter of non-function");
14607 error_at (token->location,
14608 "default arguments are only "
14609 "permitted for function parameters");
14610 default_argument = NULL_TREE;
14613 else if ((declarator && declarator->parameter_pack_p)
14614 || (decl_specifiers.type
14615 && PACK_EXPANSION_P (decl_specifiers.type)))
14617 /* Find the name of the parameter pack. */
14618 cp_declarator *id_declarator = declarator;
14619 while (id_declarator && id_declarator->kind != cdk_id)
14620 id_declarator = id_declarator->declarator;
14622 if (id_declarator && id_declarator->kind == cdk_id)
14623 error_at (declarator_token_start->location,
14625 ? "template parameter pack %qD"
14626 " cannot have a default argument"
14627 : "parameter pack %qD cannot have a default argument",
14628 id_declarator->u.id.unqualified_name);
14630 error_at (declarator_token_start->location,
14632 ? "template parameter pack cannot have a default argument"
14633 : "parameter pack cannot have a default argument");
14635 default_argument = NULL_TREE;
14639 default_argument = NULL_TREE;
14641 return make_parameter_declarator (&decl_specifiers,
14646 /* Parse a default argument and return it.
14648 TEMPLATE_PARM_P is true if this is a default argument for a
14649 non-type template parameter. */
14651 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14653 tree default_argument = NULL_TREE;
14654 bool saved_greater_than_is_operator_p;
14655 bool saved_local_variables_forbidden_p;
14657 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14659 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14660 parser->greater_than_is_operator_p = !template_parm_p;
14661 /* Local variable names (and the `this' keyword) may not
14662 appear in a default argument. */
14663 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14664 parser->local_variables_forbidden_p = true;
14665 /* Parse the assignment-expression. */
14666 if (template_parm_p)
14667 push_deferring_access_checks (dk_no_deferred);
14669 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14670 if (template_parm_p)
14671 pop_deferring_access_checks ();
14672 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14673 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14675 return default_argument;
14678 /* Parse a function-body.
14681 compound_statement */
14684 cp_parser_function_body (cp_parser *parser)
14686 cp_parser_compound_statement (parser, NULL, false);
14689 /* Parse a ctor-initializer-opt followed by a function-body. Return
14690 true if a ctor-initializer was present. */
14693 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14696 bool ctor_initializer_p;
14698 /* Begin the function body. */
14699 body = begin_function_body ();
14700 /* Parse the optional ctor-initializer. */
14701 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14702 /* Parse the function-body. */
14703 cp_parser_function_body (parser);
14704 /* Finish the function body. */
14705 finish_function_body (body);
14707 return ctor_initializer_p;
14710 /* Parse an initializer.
14713 = initializer-clause
14714 ( expression-list )
14716 Returns an expression representing the initializer. If no
14717 initializer is present, NULL_TREE is returned.
14719 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14720 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14721 set to TRUE if there is no initializer present. If there is an
14722 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14723 is set to true; otherwise it is set to false. */
14726 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14727 bool* non_constant_p)
14732 /* Peek at the next token. */
14733 token = cp_lexer_peek_token (parser->lexer);
14735 /* Let our caller know whether or not this initializer was
14737 *is_direct_init = (token->type != CPP_EQ);
14738 /* Assume that the initializer is constant. */
14739 *non_constant_p = false;
14741 if (token->type == CPP_EQ)
14743 /* Consume the `='. */
14744 cp_lexer_consume_token (parser->lexer);
14745 /* Parse the initializer-clause. */
14746 init = cp_parser_initializer_clause (parser, non_constant_p);
14748 else if (token->type == CPP_OPEN_PAREN)
14751 vec = cp_parser_parenthesized_expression_list (parser, false,
14753 /*allow_expansion_p=*/true,
14756 return error_mark_node;
14757 init = build_tree_list_vec (vec);
14758 release_tree_vector (vec);
14760 else if (token->type == CPP_OPEN_BRACE)
14762 maybe_warn_cpp0x ("extended initializer lists");
14763 init = cp_parser_braced_list (parser, non_constant_p);
14764 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14768 /* Anything else is an error. */
14769 cp_parser_error (parser, "expected initializer");
14770 init = error_mark_node;
14776 /* Parse an initializer-clause.
14778 initializer-clause:
14779 assignment-expression
14782 Returns an expression representing the initializer.
14784 If the `assignment-expression' production is used the value
14785 returned is simply a representation for the expression.
14787 Otherwise, calls cp_parser_braced_list. */
14790 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14794 /* Assume the expression is constant. */
14795 *non_constant_p = false;
14797 /* If it is not a `{', then we are looking at an
14798 assignment-expression. */
14799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14802 = cp_parser_constant_expression (parser,
14803 /*allow_non_constant_p=*/true,
14805 if (!*non_constant_p)
14806 initializer = fold_non_dependent_expr (initializer);
14809 initializer = cp_parser_braced_list (parser, non_constant_p);
14811 return initializer;
14814 /* Parse a brace-enclosed initializer list.
14817 { initializer-list , [opt] }
14820 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14821 the elements of the initializer-list (or NULL, if the last
14822 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14823 NULL_TREE. There is no way to detect whether or not the optional
14824 trailing `,' was provided. NON_CONSTANT_P is as for
14825 cp_parser_initializer. */
14828 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14832 /* Consume the `{' token. */
14833 cp_lexer_consume_token (parser->lexer);
14834 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14835 initializer = make_node (CONSTRUCTOR);
14836 /* If it's not a `}', then there is a non-trivial initializer. */
14837 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14839 /* Parse the initializer list. */
14840 CONSTRUCTOR_ELTS (initializer)
14841 = cp_parser_initializer_list (parser, non_constant_p);
14842 /* A trailing `,' token is allowed. */
14843 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14844 cp_lexer_consume_token (parser->lexer);
14846 /* Now, there should be a trailing `}'. */
14847 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14848 TREE_TYPE (initializer) = init_list_type_node;
14849 return initializer;
14852 /* Parse an initializer-list.
14855 initializer-clause ... [opt]
14856 initializer-list , initializer-clause ... [opt]
14861 identifier : initializer-clause
14862 initializer-list, identifier : initializer-clause
14864 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14865 for the initializer. If the INDEX of the elt is non-NULL, it is the
14866 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14867 as for cp_parser_initializer. */
14869 static VEC(constructor_elt,gc) *
14870 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14872 VEC(constructor_elt,gc) *v = NULL;
14874 /* Assume all of the expressions are constant. */
14875 *non_constant_p = false;
14877 /* Parse the rest of the list. */
14883 bool clause_non_constant_p;
14885 /* If the next token is an identifier and the following one is a
14886 colon, we are looking at the GNU designated-initializer
14888 if (cp_parser_allow_gnu_extensions_p (parser)
14889 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14890 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14892 /* Warn the user that they are using an extension. */
14893 pedwarn (input_location, OPT_pedantic,
14894 "ISO C++ does not allow designated initializers");
14895 /* Consume the identifier. */
14896 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14897 /* Consume the `:'. */
14898 cp_lexer_consume_token (parser->lexer);
14901 identifier = NULL_TREE;
14903 /* Parse the initializer. */
14904 initializer = cp_parser_initializer_clause (parser,
14905 &clause_non_constant_p);
14906 /* If any clause is non-constant, so is the entire initializer. */
14907 if (clause_non_constant_p)
14908 *non_constant_p = true;
14910 /* If we have an ellipsis, this is an initializer pack
14912 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14914 /* Consume the `...'. */
14915 cp_lexer_consume_token (parser->lexer);
14917 /* Turn the initializer into an initializer expansion. */
14918 initializer = make_pack_expansion (initializer);
14921 /* Add it to the vector. */
14922 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14924 /* If the next token is not a comma, we have reached the end of
14926 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14929 /* Peek at the next token. */
14930 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14931 /* If the next token is a `}', then we're still done. An
14932 initializer-clause can have a trailing `,' after the
14933 initializer-list and before the closing `}'. */
14934 if (token->type == CPP_CLOSE_BRACE)
14937 /* Consume the `,' token. */
14938 cp_lexer_consume_token (parser->lexer);
14944 /* Classes [gram.class] */
14946 /* Parse a class-name.
14952 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14953 to indicate that names looked up in dependent types should be
14954 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14955 keyword has been used to indicate that the name that appears next
14956 is a template. TAG_TYPE indicates the explicit tag given before
14957 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14958 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14959 is the class being defined in a class-head.
14961 Returns the TYPE_DECL representing the class. */
14964 cp_parser_class_name (cp_parser *parser,
14965 bool typename_keyword_p,
14966 bool template_keyword_p,
14967 enum tag_types tag_type,
14968 bool check_dependency_p,
14970 bool is_declaration)
14976 tree identifier = NULL_TREE;
14978 /* All class-names start with an identifier. */
14979 token = cp_lexer_peek_token (parser->lexer);
14980 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14982 cp_parser_error (parser, "expected class-name");
14983 return error_mark_node;
14986 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14987 to a template-id, so we save it here. */
14988 scope = parser->scope;
14989 if (scope == error_mark_node)
14990 return error_mark_node;
14992 /* Any name names a type if we're following the `typename' keyword
14993 in a qualified name where the enclosing scope is type-dependent. */
14994 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14995 && dependent_type_p (scope));
14996 /* Handle the common case (an identifier, but not a template-id)
14998 if (token->type == CPP_NAME
14999 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15001 cp_token *identifier_token;
15004 /* Look for the identifier. */
15005 identifier_token = cp_lexer_peek_token (parser->lexer);
15006 ambiguous_p = identifier_token->ambiguous_p;
15007 identifier = cp_parser_identifier (parser);
15008 /* If the next token isn't an identifier, we are certainly not
15009 looking at a class-name. */
15010 if (identifier == error_mark_node)
15011 decl = error_mark_node;
15012 /* If we know this is a type-name, there's no need to look it
15014 else if (typename_p)
15018 tree ambiguous_decls;
15019 /* If we already know that this lookup is ambiguous, then
15020 we've already issued an error message; there's no reason
15024 cp_parser_simulate_error (parser);
15025 return error_mark_node;
15027 /* If the next token is a `::', then the name must be a type
15030 [basic.lookup.qual]
15032 During the lookup for a name preceding the :: scope
15033 resolution operator, object, function, and enumerator
15034 names are ignored. */
15035 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15036 tag_type = typename_type;
15037 /* Look up the name. */
15038 decl = cp_parser_lookup_name (parser, identifier,
15040 /*is_template=*/false,
15041 /*is_namespace=*/false,
15042 check_dependency_p,
15044 identifier_token->location);
15045 if (ambiguous_decls)
15047 error_at (identifier_token->location,
15048 "reference to %qD is ambiguous", identifier);
15049 print_candidates (ambiguous_decls);
15050 if (cp_parser_parsing_tentatively (parser))
15052 identifier_token->ambiguous_p = true;
15053 cp_parser_simulate_error (parser);
15055 return error_mark_node;
15061 /* Try a template-id. */
15062 decl = cp_parser_template_id (parser, template_keyword_p,
15063 check_dependency_p,
15065 if (decl == error_mark_node)
15066 return error_mark_node;
15069 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15071 /* If this is a typename, create a TYPENAME_TYPE. */
15072 if (typename_p && decl != error_mark_node)
15074 decl = make_typename_type (scope, decl, typename_type,
15075 /*complain=*/tf_error);
15076 if (decl != error_mark_node)
15077 decl = TYPE_NAME (decl);
15080 /* Check to see that it is really the name of a class. */
15081 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15082 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15083 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15084 /* Situations like this:
15086 template <typename T> struct A {
15087 typename T::template X<int>::I i;
15090 are problematic. Is `T::template X<int>' a class-name? The
15091 standard does not seem to be definitive, but there is no other
15092 valid interpretation of the following `::'. Therefore, those
15093 names are considered class-names. */
15095 decl = make_typename_type (scope, decl, tag_type, tf_error);
15096 if (decl != error_mark_node)
15097 decl = TYPE_NAME (decl);
15099 else if (TREE_CODE (decl) != TYPE_DECL
15100 || TREE_TYPE (decl) == error_mark_node
15101 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15102 decl = error_mark_node;
15104 if (decl == error_mark_node)
15105 cp_parser_error (parser, "expected class-name");
15106 else if (identifier && !parser->scope)
15107 maybe_note_name_used_in_class (identifier, decl);
15112 /* Parse a class-specifier.
15115 class-head { member-specification [opt] }
15117 Returns the TREE_TYPE representing the class. */
15120 cp_parser_class_specifier (cp_parser* parser)
15123 tree attributes = NULL_TREE;
15124 bool nested_name_specifier_p;
15125 unsigned saved_num_template_parameter_lists;
15126 bool saved_in_function_body;
15127 bool saved_in_unbraced_linkage_specification_p;
15128 tree old_scope = NULL_TREE;
15129 tree scope = NULL_TREE;
15132 push_deferring_access_checks (dk_no_deferred);
15134 /* Parse the class-head. */
15135 type = cp_parser_class_head (parser,
15136 &nested_name_specifier_p,
15139 /* If the class-head was a semantic disaster, skip the entire body
15143 cp_parser_skip_to_end_of_block_or_statement (parser);
15144 pop_deferring_access_checks ();
15145 return error_mark_node;
15148 /* Look for the `{'. */
15149 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15151 pop_deferring_access_checks ();
15152 return error_mark_node;
15155 /* Process the base classes. If they're invalid, skip the
15156 entire class body. */
15157 if (!xref_basetypes (type, bases))
15159 /* Consuming the closing brace yields better error messages
15161 if (cp_parser_skip_to_closing_brace (parser))
15162 cp_lexer_consume_token (parser->lexer);
15163 pop_deferring_access_checks ();
15164 return error_mark_node;
15167 /* Issue an error message if type-definitions are forbidden here. */
15168 cp_parser_check_type_definition (parser);
15169 /* Remember that we are defining one more class. */
15170 ++parser->num_classes_being_defined;
15171 /* Inside the class, surrounding template-parameter-lists do not
15173 saved_num_template_parameter_lists
15174 = parser->num_template_parameter_lists;
15175 parser->num_template_parameter_lists = 0;
15176 /* We are not in a function body. */
15177 saved_in_function_body = parser->in_function_body;
15178 parser->in_function_body = false;
15179 /* We are not immediately inside an extern "lang" block. */
15180 saved_in_unbraced_linkage_specification_p
15181 = parser->in_unbraced_linkage_specification_p;
15182 parser->in_unbraced_linkage_specification_p = false;
15184 /* Start the class. */
15185 if (nested_name_specifier_p)
15187 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15188 old_scope = push_inner_scope (scope);
15190 type = begin_class_definition (type, attributes);
15192 if (type == error_mark_node)
15193 /* If the type is erroneous, skip the entire body of the class. */
15194 cp_parser_skip_to_closing_brace (parser);
15196 /* Parse the member-specification. */
15197 cp_parser_member_specification_opt (parser);
15199 /* Look for the trailing `}'. */
15200 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15201 /* Look for trailing attributes to apply to this class. */
15202 if (cp_parser_allow_gnu_extensions_p (parser))
15203 attributes = cp_parser_attributes_opt (parser);
15204 if (type != error_mark_node)
15205 type = finish_struct (type, attributes);
15206 if (nested_name_specifier_p)
15207 pop_inner_scope (old_scope, scope);
15208 /* If this class is not itself within the scope of another class,
15209 then we need to parse the bodies of all of the queued function
15210 definitions. Note that the queued functions defined in a class
15211 are not always processed immediately following the
15212 class-specifier for that class. Consider:
15215 struct B { void f() { sizeof (A); } };
15218 If `f' were processed before the processing of `A' were
15219 completed, there would be no way to compute the size of `A'.
15220 Note that the nesting we are interested in here is lexical --
15221 not the semantic nesting given by TYPE_CONTEXT. In particular,
15224 struct A { struct B; };
15225 struct A::B { void f() { } };
15227 there is no need to delay the parsing of `A::B::f'. */
15228 if (--parser->num_classes_being_defined == 0)
15232 tree class_type = NULL_TREE;
15233 tree pushed_scope = NULL_TREE;
15235 /* In a first pass, parse default arguments to the functions.
15236 Then, in a second pass, parse the bodies of the functions.
15237 This two-phased approach handles cases like:
15245 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15246 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15247 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15248 TREE_PURPOSE (parser->unparsed_functions_queues)
15249 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15251 fn = TREE_VALUE (queue_entry);
15252 /* If there are default arguments that have not yet been processed,
15253 take care of them now. */
15254 if (class_type != TREE_PURPOSE (queue_entry))
15257 pop_scope (pushed_scope);
15258 class_type = TREE_PURPOSE (queue_entry);
15259 pushed_scope = push_scope (class_type);
15261 /* Make sure that any template parameters are in scope. */
15262 maybe_begin_member_template_processing (fn);
15263 /* Parse the default argument expressions. */
15264 cp_parser_late_parsing_default_args (parser, fn);
15265 /* Remove any template parameters from the symbol table. */
15266 maybe_end_member_template_processing ();
15269 pop_scope (pushed_scope);
15270 /* Now parse the body of the functions. */
15271 for (TREE_VALUE (parser->unparsed_functions_queues)
15272 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15273 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15274 TREE_VALUE (parser->unparsed_functions_queues)
15275 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15277 /* Figure out which function we need to process. */
15278 fn = TREE_VALUE (queue_entry);
15279 /* Parse the function. */
15280 cp_parser_late_parsing_for_member (parser, fn);
15284 /* Put back any saved access checks. */
15285 pop_deferring_access_checks ();
15287 /* Restore saved state. */
15288 parser->in_function_body = saved_in_function_body;
15289 parser->num_template_parameter_lists
15290 = saved_num_template_parameter_lists;
15291 parser->in_unbraced_linkage_specification_p
15292 = saved_in_unbraced_linkage_specification_p;
15297 /* Parse a class-head.
15300 class-key identifier [opt] base-clause [opt]
15301 class-key nested-name-specifier identifier base-clause [opt]
15302 class-key nested-name-specifier [opt] template-id
15306 class-key attributes identifier [opt] base-clause [opt]
15307 class-key attributes nested-name-specifier identifier base-clause [opt]
15308 class-key attributes nested-name-specifier [opt] template-id
15311 Upon return BASES is initialized to the list of base classes (or
15312 NULL, if there are none) in the same form returned by
15313 cp_parser_base_clause.
15315 Returns the TYPE of the indicated class. Sets
15316 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15317 involving a nested-name-specifier was used, and FALSE otherwise.
15319 Returns error_mark_node if this is not a class-head.
15321 Returns NULL_TREE if the class-head is syntactically valid, but
15322 semantically invalid in a way that means we should skip the entire
15323 body of the class. */
15326 cp_parser_class_head (cp_parser* parser,
15327 bool* nested_name_specifier_p,
15328 tree *attributes_p,
15331 tree nested_name_specifier;
15332 enum tag_types class_key;
15333 tree id = NULL_TREE;
15334 tree type = NULL_TREE;
15336 bool template_id_p = false;
15337 bool qualified_p = false;
15338 bool invalid_nested_name_p = false;
15339 bool invalid_explicit_specialization_p = false;
15340 tree pushed_scope = NULL_TREE;
15341 unsigned num_templates;
15342 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15343 /* Assume no nested-name-specifier will be present. */
15344 *nested_name_specifier_p = false;
15345 /* Assume no template parameter lists will be used in defining the
15349 *bases = NULL_TREE;
15351 /* Look for the class-key. */
15352 class_key = cp_parser_class_key (parser);
15353 if (class_key == none_type)
15354 return error_mark_node;
15356 /* Parse the attributes. */
15357 attributes = cp_parser_attributes_opt (parser);
15359 /* If the next token is `::', that is invalid -- but sometimes
15360 people do try to write:
15364 Handle this gracefully by accepting the extra qualifier, and then
15365 issuing an error about it later if this really is a
15366 class-head. If it turns out just to be an elaborated type
15367 specifier, remain silent. */
15368 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15369 qualified_p = true;
15371 push_deferring_access_checks (dk_no_check);
15373 /* Determine the name of the class. Begin by looking for an
15374 optional nested-name-specifier. */
15375 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15376 nested_name_specifier
15377 = cp_parser_nested_name_specifier_opt (parser,
15378 /*typename_keyword_p=*/false,
15379 /*check_dependency_p=*/false,
15381 /*is_declaration=*/false);
15382 /* If there was a nested-name-specifier, then there *must* be an
15384 if (nested_name_specifier)
15386 type_start_token = cp_lexer_peek_token (parser->lexer);
15387 /* Although the grammar says `identifier', it really means
15388 `class-name' or `template-name'. You are only allowed to
15389 define a class that has already been declared with this
15392 The proposed resolution for Core Issue 180 says that wherever
15393 you see `class T::X' you should treat `X' as a type-name.
15395 It is OK to define an inaccessible class; for example:
15397 class A { class B; };
15400 We do not know if we will see a class-name, or a
15401 template-name. We look for a class-name first, in case the
15402 class-name is a template-id; if we looked for the
15403 template-name first we would stop after the template-name. */
15404 cp_parser_parse_tentatively (parser);
15405 type = cp_parser_class_name (parser,
15406 /*typename_keyword_p=*/false,
15407 /*template_keyword_p=*/false,
15409 /*check_dependency_p=*/false,
15410 /*class_head_p=*/true,
15411 /*is_declaration=*/false);
15412 /* If that didn't work, ignore the nested-name-specifier. */
15413 if (!cp_parser_parse_definitely (parser))
15415 invalid_nested_name_p = true;
15416 type_start_token = cp_lexer_peek_token (parser->lexer);
15417 id = cp_parser_identifier (parser);
15418 if (id == error_mark_node)
15421 /* If we could not find a corresponding TYPE, treat this
15422 declaration like an unqualified declaration. */
15423 if (type == error_mark_node)
15424 nested_name_specifier = NULL_TREE;
15425 /* Otherwise, count the number of templates used in TYPE and its
15426 containing scopes. */
15431 for (scope = TREE_TYPE (type);
15432 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15433 scope = (TYPE_P (scope)
15434 ? TYPE_CONTEXT (scope)
15435 : DECL_CONTEXT (scope)))
15437 && CLASS_TYPE_P (scope)
15438 && CLASSTYPE_TEMPLATE_INFO (scope)
15439 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15440 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15444 /* Otherwise, the identifier is optional. */
15447 /* We don't know whether what comes next is a template-id,
15448 an identifier, or nothing at all. */
15449 cp_parser_parse_tentatively (parser);
15450 /* Check for a template-id. */
15451 type_start_token = cp_lexer_peek_token (parser->lexer);
15452 id = cp_parser_template_id (parser,
15453 /*template_keyword_p=*/false,
15454 /*check_dependency_p=*/true,
15455 /*is_declaration=*/true);
15456 /* If that didn't work, it could still be an identifier. */
15457 if (!cp_parser_parse_definitely (parser))
15459 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15461 type_start_token = cp_lexer_peek_token (parser->lexer);
15462 id = cp_parser_identifier (parser);
15469 template_id_p = true;
15474 pop_deferring_access_checks ();
15477 cp_parser_check_for_invalid_template_id (parser, id,
15478 type_start_token->location);
15480 /* If it's not a `:' or a `{' then we can't really be looking at a
15481 class-head, since a class-head only appears as part of a
15482 class-specifier. We have to detect this situation before calling
15483 xref_tag, since that has irreversible side-effects. */
15484 if (!cp_parser_next_token_starts_class_definition_p (parser))
15486 cp_parser_error (parser, "expected %<{%> or %<:%>");
15487 return error_mark_node;
15490 /* At this point, we're going ahead with the class-specifier, even
15491 if some other problem occurs. */
15492 cp_parser_commit_to_tentative_parse (parser);
15493 /* Issue the error about the overly-qualified name now. */
15496 cp_parser_error (parser,
15497 "global qualification of class name is invalid");
15498 return error_mark_node;
15500 else if (invalid_nested_name_p)
15502 cp_parser_error (parser,
15503 "qualified name does not name a class");
15504 return error_mark_node;
15506 else if (nested_name_specifier)
15510 /* Reject typedef-names in class heads. */
15511 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15513 error_at (type_start_token->location,
15514 "invalid class name in declaration of %qD",
15520 /* Figure out in what scope the declaration is being placed. */
15521 scope = current_scope ();
15522 /* If that scope does not contain the scope in which the
15523 class was originally declared, the program is invalid. */
15524 if (scope && !is_ancestor (scope, nested_name_specifier))
15526 if (at_namespace_scope_p ())
15527 error_at (type_start_token->location,
15528 "declaration of %qD in namespace %qD which does not "
15530 type, scope, nested_name_specifier);
15532 error_at (type_start_token->location,
15533 "declaration of %qD in %qD which does not enclose %qD",
15534 type, scope, nested_name_specifier);
15540 A declarator-id shall not be qualified except for the
15541 definition of a ... nested class outside of its class
15542 ... [or] the definition or explicit instantiation of a
15543 class member of a namespace outside of its namespace. */
15544 if (scope == nested_name_specifier)
15546 permerror (nested_name_specifier_token_start->location,
15547 "extra qualification not allowed");
15548 nested_name_specifier = NULL_TREE;
15552 /* An explicit-specialization must be preceded by "template <>". If
15553 it is not, try to recover gracefully. */
15554 if (at_namespace_scope_p ()
15555 && parser->num_template_parameter_lists == 0
15558 error_at (type_start_token->location,
15559 "an explicit specialization must be preceded by %<template <>%>");
15560 invalid_explicit_specialization_p = true;
15561 /* Take the same action that would have been taken by
15562 cp_parser_explicit_specialization. */
15563 ++parser->num_template_parameter_lists;
15564 begin_specialization ();
15566 /* There must be no "return" statements between this point and the
15567 end of this function; set "type "to the correct return value and
15568 use "goto done;" to return. */
15569 /* Make sure that the right number of template parameters were
15571 if (!cp_parser_check_template_parameters (parser, num_templates,
15572 type_start_token->location,
15573 /*declarator=*/NULL))
15575 /* If something went wrong, there is no point in even trying to
15576 process the class-definition. */
15581 /* Look up the type. */
15584 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15585 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15586 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15588 error_at (type_start_token->location,
15589 "function template %qD redeclared as a class template", id);
15590 type = error_mark_node;
15594 type = TREE_TYPE (id);
15595 type = maybe_process_partial_specialization (type);
15597 if (nested_name_specifier)
15598 pushed_scope = push_scope (nested_name_specifier);
15600 else if (nested_name_specifier)
15606 template <typename T> struct S { struct T };
15607 template <typename T> struct S<T>::T { };
15609 we will get a TYPENAME_TYPE when processing the definition of
15610 `S::T'. We need to resolve it to the actual type before we
15611 try to define it. */
15612 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15614 class_type = resolve_typename_type (TREE_TYPE (type),
15615 /*only_current_p=*/false);
15616 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15617 type = TYPE_NAME (class_type);
15620 cp_parser_error (parser, "could not resolve typename type");
15621 type = error_mark_node;
15625 if (maybe_process_partial_specialization (TREE_TYPE (type))
15626 == error_mark_node)
15632 class_type = current_class_type;
15633 /* Enter the scope indicated by the nested-name-specifier. */
15634 pushed_scope = push_scope (nested_name_specifier);
15635 /* Get the canonical version of this type. */
15636 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15637 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15638 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15640 type = push_template_decl (type);
15641 if (type == error_mark_node)
15648 type = TREE_TYPE (type);
15649 *nested_name_specifier_p = true;
15651 else /* The name is not a nested name. */
15653 /* If the class was unnamed, create a dummy name. */
15655 id = make_anon_name ();
15656 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15657 parser->num_template_parameter_lists);
15660 /* Indicate whether this class was declared as a `class' or as a
15662 if (TREE_CODE (type) == RECORD_TYPE)
15663 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15664 cp_parser_check_class_key (class_key, type);
15666 /* If this type was already complete, and we see another definition,
15667 that's an error. */
15668 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15670 error_at (type_start_token->location, "redefinition of %q#T",
15672 error_at (type_start_token->location, "previous definition of %q+#T",
15677 else if (type == error_mark_node)
15680 /* We will have entered the scope containing the class; the names of
15681 base classes should be looked up in that context. For example:
15683 struct A { struct B {}; struct C; };
15684 struct A::C : B {};
15688 /* Get the list of base-classes, if there is one. */
15689 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15690 *bases = cp_parser_base_clause (parser);
15693 /* Leave the scope given by the nested-name-specifier. We will
15694 enter the class scope itself while processing the members. */
15696 pop_scope (pushed_scope);
15698 if (invalid_explicit_specialization_p)
15700 end_specialization ();
15701 --parser->num_template_parameter_lists;
15703 *attributes_p = attributes;
15707 /* Parse a class-key.
15714 Returns the kind of class-key specified, or none_type to indicate
15717 static enum tag_types
15718 cp_parser_class_key (cp_parser* parser)
15721 enum tag_types tag_type;
15723 /* Look for the class-key. */
15724 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15728 /* Check to see if the TOKEN is a class-key. */
15729 tag_type = cp_parser_token_is_class_key (token);
15731 cp_parser_error (parser, "expected class-key");
15735 /* Parse an (optional) member-specification.
15737 member-specification:
15738 member-declaration member-specification [opt]
15739 access-specifier : member-specification [opt] */
15742 cp_parser_member_specification_opt (cp_parser* parser)
15749 /* Peek at the next token. */
15750 token = cp_lexer_peek_token (parser->lexer);
15751 /* If it's a `}', or EOF then we've seen all the members. */
15752 if (token->type == CPP_CLOSE_BRACE
15753 || token->type == CPP_EOF
15754 || token->type == CPP_PRAGMA_EOL)
15757 /* See if this token is a keyword. */
15758 keyword = token->keyword;
15762 case RID_PROTECTED:
15764 /* Consume the access-specifier. */
15765 cp_lexer_consume_token (parser->lexer);
15766 /* Remember which access-specifier is active. */
15767 current_access_specifier = token->u.value;
15768 /* Look for the `:'. */
15769 cp_parser_require (parser, CPP_COLON, "%<:%>");
15773 /* Accept #pragmas at class scope. */
15774 if (token->type == CPP_PRAGMA)
15776 cp_parser_pragma (parser, pragma_external);
15780 /* Otherwise, the next construction must be a
15781 member-declaration. */
15782 cp_parser_member_declaration (parser);
15787 /* Parse a member-declaration.
15789 member-declaration:
15790 decl-specifier-seq [opt] member-declarator-list [opt] ;
15791 function-definition ; [opt]
15792 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15794 template-declaration
15796 member-declarator-list:
15798 member-declarator-list , member-declarator
15801 declarator pure-specifier [opt]
15802 declarator constant-initializer [opt]
15803 identifier [opt] : constant-expression
15807 member-declaration:
15808 __extension__ member-declaration
15811 declarator attributes [opt] pure-specifier [opt]
15812 declarator attributes [opt] constant-initializer [opt]
15813 identifier [opt] attributes [opt] : constant-expression
15817 member-declaration:
15818 static_assert-declaration */
15821 cp_parser_member_declaration (cp_parser* parser)
15823 cp_decl_specifier_seq decl_specifiers;
15824 tree prefix_attributes;
15826 int declares_class_or_enum;
15828 cp_token *token = NULL;
15829 cp_token *decl_spec_token_start = NULL;
15830 cp_token *initializer_token_start = NULL;
15831 int saved_pedantic;
15833 /* Check for the `__extension__' keyword. */
15834 if (cp_parser_extension_opt (parser, &saved_pedantic))
15837 cp_parser_member_declaration (parser);
15838 /* Restore the old value of the PEDANTIC flag. */
15839 pedantic = saved_pedantic;
15844 /* Check for a template-declaration. */
15845 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15847 /* An explicit specialization here is an error condition, and we
15848 expect the specialization handler to detect and report this. */
15849 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15850 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15851 cp_parser_explicit_specialization (parser);
15853 cp_parser_template_declaration (parser, /*member_p=*/true);
15858 /* Check for a using-declaration. */
15859 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15861 /* Parse the using-declaration. */
15862 cp_parser_using_declaration (parser,
15863 /*access_declaration_p=*/false);
15867 /* Check for @defs. */
15868 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15871 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15872 ivar = ivar_chains;
15876 ivar = TREE_CHAIN (member);
15877 TREE_CHAIN (member) = NULL_TREE;
15878 finish_member_declaration (member);
15883 /* If the next token is `static_assert' we have a static assertion. */
15884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15886 cp_parser_static_assert (parser, /*member_p=*/true);
15890 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15893 /* Parse the decl-specifier-seq. */
15894 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15895 cp_parser_decl_specifier_seq (parser,
15896 CP_PARSER_FLAGS_OPTIONAL,
15898 &declares_class_or_enum);
15899 prefix_attributes = decl_specifiers.attributes;
15900 decl_specifiers.attributes = NULL_TREE;
15901 /* Check for an invalid type-name. */
15902 if (!decl_specifiers.type
15903 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15905 /* If there is no declarator, then the decl-specifier-seq should
15907 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15909 /* If there was no decl-specifier-seq, and the next token is a
15910 `;', then we have something like:
15916 Each member-declaration shall declare at least one member
15917 name of the class. */
15918 if (!decl_specifiers.any_specifiers_p)
15920 cp_token *token = cp_lexer_peek_token (parser->lexer);
15921 if (!in_system_header_at (token->location))
15922 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15928 /* See if this declaration is a friend. */
15929 friend_p = cp_parser_friend_p (&decl_specifiers);
15930 /* If there were decl-specifiers, check to see if there was
15931 a class-declaration. */
15932 type = check_tag_decl (&decl_specifiers);
15933 /* Nested classes have already been added to the class, but
15934 a `friend' needs to be explicitly registered. */
15937 /* If the `friend' keyword was present, the friend must
15938 be introduced with a class-key. */
15939 if (!declares_class_or_enum)
15940 error_at (decl_spec_token_start->location,
15941 "a class-key must be used when declaring a friend");
15944 template <typename T> struct A {
15945 friend struct A<T>::B;
15948 A<T>::B will be represented by a TYPENAME_TYPE, and
15949 therefore not recognized by check_tag_decl. */
15951 && decl_specifiers.type
15952 && TYPE_P (decl_specifiers.type))
15953 type = decl_specifiers.type;
15954 if (!type || !TYPE_P (type))
15955 error_at (decl_spec_token_start->location,
15956 "friend declaration does not name a class or "
15959 make_friend_class (current_class_type, type,
15960 /*complain=*/true);
15962 /* If there is no TYPE, an error message will already have
15964 else if (!type || type == error_mark_node)
15966 /* An anonymous aggregate has to be handled specially; such
15967 a declaration really declares a data member (with a
15968 particular type), as opposed to a nested class. */
15969 else if (ANON_AGGR_TYPE_P (type))
15971 /* Remove constructors and such from TYPE, now that we
15972 know it is an anonymous aggregate. */
15973 fixup_anonymous_aggr (type);
15974 /* And make the corresponding data member. */
15975 decl = build_decl (decl_spec_token_start->location,
15976 FIELD_DECL, NULL_TREE, type);
15977 /* Add it to the class. */
15978 finish_member_declaration (decl);
15981 cp_parser_check_access_in_redeclaration
15983 decl_spec_token_start->location);
15988 /* See if these declarations will be friends. */
15989 friend_p = cp_parser_friend_p (&decl_specifiers);
15991 /* Keep going until we hit the `;' at the end of the
15993 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15995 tree attributes = NULL_TREE;
15996 tree first_attribute;
15998 /* Peek at the next token. */
15999 token = cp_lexer_peek_token (parser->lexer);
16001 /* Check for a bitfield declaration. */
16002 if (token->type == CPP_COLON
16003 || (token->type == CPP_NAME
16004 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16010 /* Get the name of the bitfield. Note that we cannot just
16011 check TOKEN here because it may have been invalidated by
16012 the call to cp_lexer_peek_nth_token above. */
16013 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16014 identifier = cp_parser_identifier (parser);
16016 identifier = NULL_TREE;
16018 /* Consume the `:' token. */
16019 cp_lexer_consume_token (parser->lexer);
16020 /* Get the width of the bitfield. */
16022 = cp_parser_constant_expression (parser,
16023 /*allow_non_constant=*/false,
16026 /* Look for attributes that apply to the bitfield. */
16027 attributes = cp_parser_attributes_opt (parser);
16028 /* Remember which attributes are prefix attributes and
16030 first_attribute = attributes;
16031 /* Combine the attributes. */
16032 attributes = chainon (prefix_attributes, attributes);
16034 /* Create the bitfield declaration. */
16035 decl = grokbitfield (identifier
16036 ? make_id_declarator (NULL_TREE,
16046 cp_declarator *declarator;
16048 tree asm_specification;
16049 int ctor_dtor_or_conv_p;
16051 /* Parse the declarator. */
16053 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16054 &ctor_dtor_or_conv_p,
16055 /*parenthesized_p=*/NULL,
16056 /*member_p=*/true);
16058 /* If something went wrong parsing the declarator, make sure
16059 that we at least consume some tokens. */
16060 if (declarator == cp_error_declarator)
16062 /* Skip to the end of the statement. */
16063 cp_parser_skip_to_end_of_statement (parser);
16064 /* If the next token is not a semicolon, that is
16065 probably because we just skipped over the body of
16066 a function. So, we consume a semicolon if
16067 present, but do not issue an error message if it
16069 if (cp_lexer_next_token_is (parser->lexer,
16071 cp_lexer_consume_token (parser->lexer);
16075 if (declares_class_or_enum & 2)
16076 cp_parser_check_for_definition_in_return_type
16077 (declarator, decl_specifiers.type,
16078 decl_specifiers.type_location);
16080 /* Look for an asm-specification. */
16081 asm_specification = cp_parser_asm_specification_opt (parser);
16082 /* Look for attributes that apply to the declaration. */
16083 attributes = cp_parser_attributes_opt (parser);
16084 /* Remember which attributes are prefix attributes and
16086 first_attribute = attributes;
16087 /* Combine the attributes. */
16088 attributes = chainon (prefix_attributes, attributes);
16090 /* If it's an `=', then we have a constant-initializer or a
16091 pure-specifier. It is not correct to parse the
16092 initializer before registering the member declaration
16093 since the member declaration should be in scope while
16094 its initializer is processed. However, the rest of the
16095 front end does not yet provide an interface that allows
16096 us to handle this correctly. */
16097 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16101 A pure-specifier shall be used only in the declaration of
16102 a virtual function.
16104 A member-declarator can contain a constant-initializer
16105 only if it declares a static member of integral or
16108 Therefore, if the DECLARATOR is for a function, we look
16109 for a pure-specifier; otherwise, we look for a
16110 constant-initializer. When we call `grokfield', it will
16111 perform more stringent semantics checks. */
16112 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16113 if (function_declarator_p (declarator))
16114 initializer = cp_parser_pure_specifier (parser);
16116 /* Parse the initializer. */
16117 initializer = cp_parser_constant_initializer (parser);
16119 /* Otherwise, there is no initializer. */
16121 initializer = NULL_TREE;
16123 /* See if we are probably looking at a function
16124 definition. We are certainly not looking at a
16125 member-declarator. Calling `grokfield' has
16126 side-effects, so we must not do it unless we are sure
16127 that we are looking at a member-declarator. */
16128 if (cp_parser_token_starts_function_definition_p
16129 (cp_lexer_peek_token (parser->lexer)))
16131 /* The grammar does not allow a pure-specifier to be
16132 used when a member function is defined. (It is
16133 possible that this fact is an oversight in the
16134 standard, since a pure function may be defined
16135 outside of the class-specifier. */
16137 error_at (initializer_token_start->location,
16138 "pure-specifier on function-definition");
16139 decl = cp_parser_save_member_function_body (parser,
16143 /* If the member was not a friend, declare it here. */
16145 finish_member_declaration (decl);
16146 /* Peek at the next token. */
16147 token = cp_lexer_peek_token (parser->lexer);
16148 /* If the next token is a semicolon, consume it. */
16149 if (token->type == CPP_SEMICOLON)
16150 cp_lexer_consume_token (parser->lexer);
16154 if (declarator->kind == cdk_function)
16155 declarator->id_loc = token->location;
16156 /* Create the declaration. */
16157 decl = grokfield (declarator, &decl_specifiers,
16158 initializer, /*init_const_expr_p=*/true,
16163 /* Reset PREFIX_ATTRIBUTES. */
16164 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16165 attributes = TREE_CHAIN (attributes);
16167 TREE_CHAIN (attributes) = NULL_TREE;
16169 /* If there is any qualification still in effect, clear it
16170 now; we will be starting fresh with the next declarator. */
16171 parser->scope = NULL_TREE;
16172 parser->qualifying_scope = NULL_TREE;
16173 parser->object_scope = NULL_TREE;
16174 /* If it's a `,', then there are more declarators. */
16175 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16176 cp_lexer_consume_token (parser->lexer);
16177 /* If the next token isn't a `;', then we have a parse error. */
16178 else if (cp_lexer_next_token_is_not (parser->lexer,
16181 cp_parser_error (parser, "expected %<;%>");
16182 /* Skip tokens until we find a `;'. */
16183 cp_parser_skip_to_end_of_statement (parser);
16190 /* Add DECL to the list of members. */
16192 finish_member_declaration (decl);
16194 if (TREE_CODE (decl) == FUNCTION_DECL)
16195 cp_parser_save_default_args (parser, decl);
16200 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16203 /* Parse a pure-specifier.
16208 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16209 Otherwise, ERROR_MARK_NODE is returned. */
16212 cp_parser_pure_specifier (cp_parser* parser)
16216 /* Look for the `=' token. */
16217 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16218 return error_mark_node;
16219 /* Look for the `0' token. */
16220 token = cp_lexer_peek_token (parser->lexer);
16222 if (token->type == CPP_EOF
16223 || token->type == CPP_PRAGMA_EOL)
16224 return error_mark_node;
16226 cp_lexer_consume_token (parser->lexer);
16228 /* Accept = default or = delete in c++0x mode. */
16229 if (token->keyword == RID_DEFAULT
16230 || token->keyword == RID_DELETE)
16232 maybe_warn_cpp0x ("defaulted and deleted functions");
16233 return token->u.value;
16236 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16237 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16239 cp_parser_error (parser,
16240 "invalid pure specifier (only %<= 0%> is allowed)");
16241 cp_parser_skip_to_end_of_statement (parser);
16242 return error_mark_node;
16244 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16246 error_at (token->location, "templates may not be %<virtual%>");
16247 return error_mark_node;
16250 return integer_zero_node;
16253 /* Parse a constant-initializer.
16255 constant-initializer:
16256 = constant-expression
16258 Returns a representation of the constant-expression. */
16261 cp_parser_constant_initializer (cp_parser* parser)
16263 /* Look for the `=' token. */
16264 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16265 return error_mark_node;
16267 /* It is invalid to write:
16269 struct S { static const int i = { 7 }; };
16272 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16274 cp_parser_error (parser,
16275 "a brace-enclosed initializer is not allowed here");
16276 /* Consume the opening brace. */
16277 cp_lexer_consume_token (parser->lexer);
16278 /* Skip the initializer. */
16279 cp_parser_skip_to_closing_brace (parser);
16280 /* Look for the trailing `}'. */
16281 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16283 return error_mark_node;
16286 return cp_parser_constant_expression (parser,
16287 /*allow_non_constant=*/false,
16291 /* Derived classes [gram.class.derived] */
16293 /* Parse a base-clause.
16296 : base-specifier-list
16298 base-specifier-list:
16299 base-specifier ... [opt]
16300 base-specifier-list , base-specifier ... [opt]
16302 Returns a TREE_LIST representing the base-classes, in the order in
16303 which they were declared. The representation of each node is as
16304 described by cp_parser_base_specifier.
16306 In the case that no bases are specified, this function will return
16307 NULL_TREE, not ERROR_MARK_NODE. */
16310 cp_parser_base_clause (cp_parser* parser)
16312 tree bases = NULL_TREE;
16314 /* Look for the `:' that begins the list. */
16315 cp_parser_require (parser, CPP_COLON, "%<:%>");
16317 /* Scan the base-specifier-list. */
16322 bool pack_expansion_p = false;
16324 /* Look for the base-specifier. */
16325 base = cp_parser_base_specifier (parser);
16326 /* Look for the (optional) ellipsis. */
16327 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16329 /* Consume the `...'. */
16330 cp_lexer_consume_token (parser->lexer);
16332 pack_expansion_p = true;
16335 /* Add BASE to the front of the list. */
16336 if (base != error_mark_node)
16338 if (pack_expansion_p)
16339 /* Make this a pack expansion type. */
16340 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16343 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16345 TREE_CHAIN (base) = bases;
16349 /* Peek at the next token. */
16350 token = cp_lexer_peek_token (parser->lexer);
16351 /* If it's not a comma, then the list is complete. */
16352 if (token->type != CPP_COMMA)
16354 /* Consume the `,'. */
16355 cp_lexer_consume_token (parser->lexer);
16358 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16359 base class had a qualified name. However, the next name that
16360 appears is certainly not qualified. */
16361 parser->scope = NULL_TREE;
16362 parser->qualifying_scope = NULL_TREE;
16363 parser->object_scope = NULL_TREE;
16365 return nreverse (bases);
16368 /* Parse a base-specifier.
16371 :: [opt] nested-name-specifier [opt] class-name
16372 virtual access-specifier [opt] :: [opt] nested-name-specifier
16374 access-specifier virtual [opt] :: [opt] nested-name-specifier
16377 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16378 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16379 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16380 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16383 cp_parser_base_specifier (cp_parser* parser)
16387 bool virtual_p = false;
16388 bool duplicate_virtual_error_issued_p = false;
16389 bool duplicate_access_error_issued_p = false;
16390 bool class_scope_p, template_p;
16391 tree access = access_default_node;
16394 /* Process the optional `virtual' and `access-specifier'. */
16397 /* Peek at the next token. */
16398 token = cp_lexer_peek_token (parser->lexer);
16399 /* Process `virtual'. */
16400 switch (token->keyword)
16403 /* If `virtual' appears more than once, issue an error. */
16404 if (virtual_p && !duplicate_virtual_error_issued_p)
16406 cp_parser_error (parser,
16407 "%<virtual%> specified more than once in base-specified");
16408 duplicate_virtual_error_issued_p = true;
16413 /* Consume the `virtual' token. */
16414 cp_lexer_consume_token (parser->lexer);
16419 case RID_PROTECTED:
16421 /* If more than one access specifier appears, issue an
16423 if (access != access_default_node
16424 && !duplicate_access_error_issued_p)
16426 cp_parser_error (parser,
16427 "more than one access specifier in base-specified");
16428 duplicate_access_error_issued_p = true;
16431 access = ridpointers[(int) token->keyword];
16433 /* Consume the access-specifier. */
16434 cp_lexer_consume_token (parser->lexer);
16443 /* It is not uncommon to see programs mechanically, erroneously, use
16444 the 'typename' keyword to denote (dependent) qualified types
16445 as base classes. */
16446 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16448 token = cp_lexer_peek_token (parser->lexer);
16449 if (!processing_template_decl)
16450 error_at (token->location,
16451 "keyword %<typename%> not allowed outside of templates");
16453 error_at (token->location,
16454 "keyword %<typename%> not allowed in this context "
16455 "(the base class is implicitly a type)");
16456 cp_lexer_consume_token (parser->lexer);
16459 /* Look for the optional `::' operator. */
16460 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16461 /* Look for the nested-name-specifier. The simplest way to
16466 The keyword `typename' is not permitted in a base-specifier or
16467 mem-initializer; in these contexts a qualified name that
16468 depends on a template-parameter is implicitly assumed to be a
16471 is to pretend that we have seen the `typename' keyword at this
16473 cp_parser_nested_name_specifier_opt (parser,
16474 /*typename_keyword_p=*/true,
16475 /*check_dependency_p=*/true,
16477 /*is_declaration=*/true);
16478 /* If the base class is given by a qualified name, assume that names
16479 we see are type names or templates, as appropriate. */
16480 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16481 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16483 /* Finally, look for the class-name. */
16484 type = cp_parser_class_name (parser,
16488 /*check_dependency_p=*/true,
16489 /*class_head_p=*/false,
16490 /*is_declaration=*/true);
16492 if (type == error_mark_node)
16493 return error_mark_node;
16495 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16498 /* Exception handling [gram.exception] */
16500 /* Parse an (optional) exception-specification.
16502 exception-specification:
16503 throw ( type-id-list [opt] )
16505 Returns a TREE_LIST representing the exception-specification. The
16506 TREE_VALUE of each node is a type. */
16509 cp_parser_exception_specification_opt (cp_parser* parser)
16514 /* Peek at the next token. */
16515 token = cp_lexer_peek_token (parser->lexer);
16516 /* If it's not `throw', then there's no exception-specification. */
16517 if (!cp_parser_is_keyword (token, RID_THROW))
16520 /* Consume the `throw'. */
16521 cp_lexer_consume_token (parser->lexer);
16523 /* Look for the `('. */
16524 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16526 /* Peek at the next token. */
16527 token = cp_lexer_peek_token (parser->lexer);
16528 /* If it's not a `)', then there is a type-id-list. */
16529 if (token->type != CPP_CLOSE_PAREN)
16531 const char *saved_message;
16533 /* Types may not be defined in an exception-specification. */
16534 saved_message = parser->type_definition_forbidden_message;
16535 parser->type_definition_forbidden_message
16536 = "types may not be defined in an exception-specification";
16537 /* Parse the type-id-list. */
16538 type_id_list = cp_parser_type_id_list (parser);
16539 /* Restore the saved message. */
16540 parser->type_definition_forbidden_message = saved_message;
16543 type_id_list = empty_except_spec;
16545 /* Look for the `)'. */
16546 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16548 return type_id_list;
16551 /* Parse an (optional) type-id-list.
16555 type-id-list , type-id ... [opt]
16557 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16558 in the order that the types were presented. */
16561 cp_parser_type_id_list (cp_parser* parser)
16563 tree types = NULL_TREE;
16570 /* Get the next type-id. */
16571 type = cp_parser_type_id (parser);
16572 /* Parse the optional ellipsis. */
16573 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16575 /* Consume the `...'. */
16576 cp_lexer_consume_token (parser->lexer);
16578 /* Turn the type into a pack expansion expression. */
16579 type = make_pack_expansion (type);
16581 /* Add it to the list. */
16582 types = add_exception_specifier (types, type, /*complain=*/1);
16583 /* Peek at the next token. */
16584 token = cp_lexer_peek_token (parser->lexer);
16585 /* If it is not a `,', we are done. */
16586 if (token->type != CPP_COMMA)
16588 /* Consume the `,'. */
16589 cp_lexer_consume_token (parser->lexer);
16592 return nreverse (types);
16595 /* Parse a try-block.
16598 try compound-statement handler-seq */
16601 cp_parser_try_block (cp_parser* parser)
16605 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16606 try_block = begin_try_block ();
16607 cp_parser_compound_statement (parser, NULL, true);
16608 finish_try_block (try_block);
16609 cp_parser_handler_seq (parser);
16610 finish_handler_sequence (try_block);
16615 /* Parse a function-try-block.
16617 function-try-block:
16618 try ctor-initializer [opt] function-body handler-seq */
16621 cp_parser_function_try_block (cp_parser* parser)
16623 tree compound_stmt;
16625 bool ctor_initializer_p;
16627 /* Look for the `try' keyword. */
16628 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16630 /* Let the rest of the front end know where we are. */
16631 try_block = begin_function_try_block (&compound_stmt);
16632 /* Parse the function-body. */
16634 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16635 /* We're done with the `try' part. */
16636 finish_function_try_block (try_block);
16637 /* Parse the handlers. */
16638 cp_parser_handler_seq (parser);
16639 /* We're done with the handlers. */
16640 finish_function_handler_sequence (try_block, compound_stmt);
16642 return ctor_initializer_p;
16645 /* Parse a handler-seq.
16648 handler handler-seq [opt] */
16651 cp_parser_handler_seq (cp_parser* parser)
16657 /* Parse the handler. */
16658 cp_parser_handler (parser);
16659 /* Peek at the next token. */
16660 token = cp_lexer_peek_token (parser->lexer);
16661 /* If it's not `catch' then there are no more handlers. */
16662 if (!cp_parser_is_keyword (token, RID_CATCH))
16667 /* Parse a handler.
16670 catch ( exception-declaration ) compound-statement */
16673 cp_parser_handler (cp_parser* parser)
16678 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16679 handler = begin_handler ();
16680 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16681 declaration = cp_parser_exception_declaration (parser);
16682 finish_handler_parms (declaration, handler);
16683 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16684 cp_parser_compound_statement (parser, NULL, false);
16685 finish_handler (handler);
16688 /* Parse an exception-declaration.
16690 exception-declaration:
16691 type-specifier-seq declarator
16692 type-specifier-seq abstract-declarator
16696 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16697 ellipsis variant is used. */
16700 cp_parser_exception_declaration (cp_parser* parser)
16702 cp_decl_specifier_seq type_specifiers;
16703 cp_declarator *declarator;
16704 const char *saved_message;
16706 /* If it's an ellipsis, it's easy to handle. */
16707 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16709 /* Consume the `...' token. */
16710 cp_lexer_consume_token (parser->lexer);
16714 /* Types may not be defined in exception-declarations. */
16715 saved_message = parser->type_definition_forbidden_message;
16716 parser->type_definition_forbidden_message
16717 = "types may not be defined in exception-declarations";
16719 /* Parse the type-specifier-seq. */
16720 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16722 /* If it's a `)', then there is no declarator. */
16723 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16726 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16727 /*ctor_dtor_or_conv_p=*/NULL,
16728 /*parenthesized_p=*/NULL,
16729 /*member_p=*/false);
16731 /* Restore the saved message. */
16732 parser->type_definition_forbidden_message = saved_message;
16734 if (!type_specifiers.any_specifiers_p)
16735 return error_mark_node;
16737 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16740 /* Parse a throw-expression.
16743 throw assignment-expression [opt]
16745 Returns a THROW_EXPR representing the throw-expression. */
16748 cp_parser_throw_expression (cp_parser* parser)
16753 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16754 token = cp_lexer_peek_token (parser->lexer);
16755 /* Figure out whether or not there is an assignment-expression
16756 following the "throw" keyword. */
16757 if (token->type == CPP_COMMA
16758 || token->type == CPP_SEMICOLON
16759 || token->type == CPP_CLOSE_PAREN
16760 || token->type == CPP_CLOSE_SQUARE
16761 || token->type == CPP_CLOSE_BRACE
16762 || token->type == CPP_COLON)
16763 expression = NULL_TREE;
16765 expression = cp_parser_assignment_expression (parser,
16766 /*cast_p=*/false, NULL);
16768 return build_throw (expression);
16771 /* GNU Extensions */
16773 /* Parse an (optional) asm-specification.
16776 asm ( string-literal )
16778 If the asm-specification is present, returns a STRING_CST
16779 corresponding to the string-literal. Otherwise, returns
16783 cp_parser_asm_specification_opt (cp_parser* parser)
16786 tree asm_specification;
16788 /* Peek at the next token. */
16789 token = cp_lexer_peek_token (parser->lexer);
16790 /* If the next token isn't the `asm' keyword, then there's no
16791 asm-specification. */
16792 if (!cp_parser_is_keyword (token, RID_ASM))
16795 /* Consume the `asm' token. */
16796 cp_lexer_consume_token (parser->lexer);
16797 /* Look for the `('. */
16798 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16800 /* Look for the string-literal. */
16801 asm_specification = cp_parser_string_literal (parser, false, false);
16803 /* Look for the `)'. */
16804 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16806 return asm_specification;
16809 /* Parse an asm-operand-list.
16813 asm-operand-list , asm-operand
16816 string-literal ( expression )
16817 [ string-literal ] string-literal ( expression )
16819 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16820 each node is the expression. The TREE_PURPOSE is itself a
16821 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16822 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16823 is a STRING_CST for the string literal before the parenthesis. Returns
16824 ERROR_MARK_NODE if any of the operands are invalid. */
16827 cp_parser_asm_operand_list (cp_parser* parser)
16829 tree asm_operands = NULL_TREE;
16830 bool invalid_operands = false;
16834 tree string_literal;
16838 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16840 /* Consume the `[' token. */
16841 cp_lexer_consume_token (parser->lexer);
16842 /* Read the operand name. */
16843 name = cp_parser_identifier (parser);
16844 if (name != error_mark_node)
16845 name = build_string (IDENTIFIER_LENGTH (name),
16846 IDENTIFIER_POINTER (name));
16847 /* Look for the closing `]'. */
16848 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16852 /* Look for the string-literal. */
16853 string_literal = cp_parser_string_literal (parser, false, false);
16855 /* Look for the `('. */
16856 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16857 /* Parse the expression. */
16858 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16859 /* Look for the `)'. */
16860 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16862 if (name == error_mark_node
16863 || string_literal == error_mark_node
16864 || expression == error_mark_node)
16865 invalid_operands = true;
16867 /* Add this operand to the list. */
16868 asm_operands = tree_cons (build_tree_list (name, string_literal),
16871 /* If the next token is not a `,', there are no more
16873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16875 /* Consume the `,'. */
16876 cp_lexer_consume_token (parser->lexer);
16879 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16882 /* Parse an asm-clobber-list.
16886 asm-clobber-list , string-literal
16888 Returns a TREE_LIST, indicating the clobbers in the order that they
16889 appeared. The TREE_VALUE of each node is a STRING_CST. */
16892 cp_parser_asm_clobber_list (cp_parser* parser)
16894 tree clobbers = NULL_TREE;
16898 tree string_literal;
16900 /* Look for the string literal. */
16901 string_literal = cp_parser_string_literal (parser, false, false);
16902 /* Add it to the list. */
16903 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16904 /* If the next token is not a `,', then the list is
16906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16908 /* Consume the `,' token. */
16909 cp_lexer_consume_token (parser->lexer);
16915 /* Parse an asm-label-list.
16919 asm-label-list , identifier
16921 Returns a TREE_LIST, indicating the labels in the order that they
16922 appeared. The TREE_VALUE of each node is a label. */
16925 cp_parser_asm_label_list (cp_parser* parser)
16927 tree labels = NULL_TREE;
16931 tree identifier, label, name;
16933 /* Look for the identifier. */
16934 identifier = cp_parser_identifier (parser);
16935 if (!error_operand_p (identifier))
16937 label = lookup_label (identifier);
16938 if (TREE_CODE (label) == LABEL_DECL)
16940 TREE_USED (label) = 1;
16941 check_goto (label);
16942 name = build_string (IDENTIFIER_LENGTH (identifier),
16943 IDENTIFIER_POINTER (identifier));
16944 labels = tree_cons (name, label, labels);
16947 /* If the next token is not a `,', then the list is
16949 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16951 /* Consume the `,' token. */
16952 cp_lexer_consume_token (parser->lexer);
16955 return nreverse (labels);
16958 /* Parse an (optional) series of attributes.
16961 attributes attribute
16964 __attribute__ (( attribute-list [opt] ))
16966 The return value is as for cp_parser_attribute_list. */
16969 cp_parser_attributes_opt (cp_parser* parser)
16971 tree attributes = NULL_TREE;
16976 tree attribute_list;
16978 /* Peek at the next token. */
16979 token = cp_lexer_peek_token (parser->lexer);
16980 /* If it's not `__attribute__', then we're done. */
16981 if (token->keyword != RID_ATTRIBUTE)
16984 /* Consume the `__attribute__' keyword. */
16985 cp_lexer_consume_token (parser->lexer);
16986 /* Look for the two `(' tokens. */
16987 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16988 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16990 /* Peek at the next token. */
16991 token = cp_lexer_peek_token (parser->lexer);
16992 if (token->type != CPP_CLOSE_PAREN)
16993 /* Parse the attribute-list. */
16994 attribute_list = cp_parser_attribute_list (parser);
16996 /* If the next token is a `)', then there is no attribute
16998 attribute_list = NULL;
17000 /* Look for the two `)' tokens. */
17001 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17002 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17004 /* Add these new attributes to the list. */
17005 attributes = chainon (attributes, attribute_list);
17011 /* Parse an attribute-list.
17015 attribute-list , attribute
17019 identifier ( identifier )
17020 identifier ( identifier , expression-list )
17021 identifier ( expression-list )
17023 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17024 to an attribute. The TREE_PURPOSE of each node is the identifier
17025 indicating which attribute is in use. The TREE_VALUE represents
17026 the arguments, if any. */
17029 cp_parser_attribute_list (cp_parser* parser)
17031 tree attribute_list = NULL_TREE;
17032 bool save_translate_strings_p = parser->translate_strings_p;
17034 parser->translate_strings_p = false;
17041 /* Look for the identifier. We also allow keywords here; for
17042 example `__attribute__ ((const))' is legal. */
17043 token = cp_lexer_peek_token (parser->lexer);
17044 if (token->type == CPP_NAME
17045 || token->type == CPP_KEYWORD)
17047 tree arguments = NULL_TREE;
17049 /* Consume the token. */
17050 token = cp_lexer_consume_token (parser->lexer);
17052 /* Save away the identifier that indicates which attribute
17054 identifier = (token->type == CPP_KEYWORD)
17055 /* For keywords, use the canonical spelling, not the
17056 parsed identifier. */
17057 ? ridpointers[(int) token->keyword]
17060 attribute = build_tree_list (identifier, NULL_TREE);
17062 /* Peek at the next token. */
17063 token = cp_lexer_peek_token (parser->lexer);
17064 /* If it's an `(', then parse the attribute arguments. */
17065 if (token->type == CPP_OPEN_PAREN)
17068 vec = cp_parser_parenthesized_expression_list
17069 (parser, true, /*cast_p=*/false,
17070 /*allow_expansion_p=*/false,
17071 /*non_constant_p=*/NULL);
17073 arguments = error_mark_node;
17076 arguments = build_tree_list_vec (vec);
17077 release_tree_vector (vec);
17079 /* Save the arguments away. */
17080 TREE_VALUE (attribute) = arguments;
17083 if (arguments != error_mark_node)
17085 /* Add this attribute to the list. */
17086 TREE_CHAIN (attribute) = attribute_list;
17087 attribute_list = attribute;
17090 token = cp_lexer_peek_token (parser->lexer);
17092 /* Now, look for more attributes. If the next token isn't a
17093 `,', we're done. */
17094 if (token->type != CPP_COMMA)
17097 /* Consume the comma and keep going. */
17098 cp_lexer_consume_token (parser->lexer);
17100 parser->translate_strings_p = save_translate_strings_p;
17102 /* We built up the list in reverse order. */
17103 return nreverse (attribute_list);
17106 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17107 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17108 current value of the PEDANTIC flag, regardless of whether or not
17109 the `__extension__' keyword is present. The caller is responsible
17110 for restoring the value of the PEDANTIC flag. */
17113 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17115 /* Save the old value of the PEDANTIC flag. */
17116 *saved_pedantic = pedantic;
17118 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17120 /* Consume the `__extension__' token. */
17121 cp_lexer_consume_token (parser->lexer);
17122 /* We're not being pedantic while the `__extension__' keyword is
17132 /* Parse a label declaration.
17135 __label__ label-declarator-seq ;
17137 label-declarator-seq:
17138 identifier , label-declarator-seq
17142 cp_parser_label_declaration (cp_parser* parser)
17144 /* Look for the `__label__' keyword. */
17145 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17151 /* Look for an identifier. */
17152 identifier = cp_parser_identifier (parser);
17153 /* If we failed, stop. */
17154 if (identifier == error_mark_node)
17156 /* Declare it as a label. */
17157 finish_label_decl (identifier);
17158 /* If the next token is a `;', stop. */
17159 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17161 /* Look for the `,' separating the label declarations. */
17162 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17165 /* Look for the final `;'. */
17166 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17169 /* Support Functions */
17171 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17172 NAME should have one of the representations used for an
17173 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17174 is returned. If PARSER->SCOPE is a dependent type, then a
17175 SCOPE_REF is returned.
17177 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17178 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17179 was formed. Abstractly, such entities should not be passed to this
17180 function, because they do not need to be looked up, but it is
17181 simpler to check for this special case here, rather than at the
17184 In cases not explicitly covered above, this function returns a
17185 DECL, OVERLOAD, or baselink representing the result of the lookup.
17186 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17189 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17190 (e.g., "struct") that was used. In that case bindings that do not
17191 refer to types are ignored.
17193 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17196 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17199 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17202 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17203 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17204 NULL_TREE otherwise. */
17207 cp_parser_lookup_name (cp_parser *parser, tree name,
17208 enum tag_types tag_type,
17211 bool check_dependency,
17212 tree *ambiguous_decls,
17213 location_t name_location)
17217 tree object_type = parser->context->object_type;
17219 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17220 flags |= LOOKUP_COMPLAIN;
17222 /* Assume that the lookup will be unambiguous. */
17223 if (ambiguous_decls)
17224 *ambiguous_decls = NULL_TREE;
17226 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17227 no longer valid. Note that if we are parsing tentatively, and
17228 the parse fails, OBJECT_TYPE will be automatically restored. */
17229 parser->context->object_type = NULL_TREE;
17231 if (name == error_mark_node)
17232 return error_mark_node;
17234 /* A template-id has already been resolved; there is no lookup to
17236 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17238 if (BASELINK_P (name))
17240 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17241 == TEMPLATE_ID_EXPR);
17245 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17246 it should already have been checked to make sure that the name
17247 used matches the type being destroyed. */
17248 if (TREE_CODE (name) == BIT_NOT_EXPR)
17252 /* Figure out to which type this destructor applies. */
17254 type = parser->scope;
17255 else if (object_type)
17256 type = object_type;
17258 type = current_class_type;
17259 /* If that's not a class type, there is no destructor. */
17260 if (!type || !CLASS_TYPE_P (type))
17261 return error_mark_node;
17262 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17263 lazily_declare_fn (sfk_destructor, type);
17264 if (!CLASSTYPE_DESTRUCTORS (type))
17265 return error_mark_node;
17266 /* If it was a class type, return the destructor. */
17267 return CLASSTYPE_DESTRUCTORS (type);
17270 /* By this point, the NAME should be an ordinary identifier. If
17271 the id-expression was a qualified name, the qualifying scope is
17272 stored in PARSER->SCOPE at this point. */
17273 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17275 /* Perform the lookup. */
17280 if (parser->scope == error_mark_node)
17281 return error_mark_node;
17283 /* If the SCOPE is dependent, the lookup must be deferred until
17284 the template is instantiated -- unless we are explicitly
17285 looking up names in uninstantiated templates. Even then, we
17286 cannot look up the name if the scope is not a class type; it
17287 might, for example, be a template type parameter. */
17288 dependent_p = (TYPE_P (parser->scope)
17289 && dependent_scope_p (parser->scope));
17290 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17292 /* Defer lookup. */
17293 decl = error_mark_node;
17296 tree pushed_scope = NULL_TREE;
17298 /* If PARSER->SCOPE is a dependent type, then it must be a
17299 class type, and we must not be checking dependencies;
17300 otherwise, we would have processed this lookup above. So
17301 that PARSER->SCOPE is not considered a dependent base by
17302 lookup_member, we must enter the scope here. */
17304 pushed_scope = push_scope (parser->scope);
17305 /* If the PARSER->SCOPE is a template specialization, it
17306 may be instantiated during name lookup. In that case,
17307 errors may be issued. Even if we rollback the current
17308 tentative parse, those errors are valid. */
17309 decl = lookup_qualified_name (parser->scope, name,
17310 tag_type != none_type,
17311 /*complain=*/true);
17313 /* If we have a single function from a using decl, pull it out. */
17314 if (TREE_CODE (decl) == OVERLOAD
17315 && !really_overloaded_fn (decl))
17316 decl = OVL_FUNCTION (decl);
17319 pop_scope (pushed_scope);
17322 /* If the scope is a dependent type and either we deferred lookup or
17323 we did lookup but didn't find the name, rememeber the name. */
17324 if (decl == error_mark_node && TYPE_P (parser->scope)
17325 && dependent_type_p (parser->scope))
17331 /* The resolution to Core Issue 180 says that `struct
17332 A::B' should be considered a type-name, even if `A'
17334 type = make_typename_type (parser->scope, name, tag_type,
17335 /*complain=*/tf_error);
17336 decl = TYPE_NAME (type);
17338 else if (is_template
17339 && (cp_parser_next_token_ends_template_argument_p (parser)
17340 || cp_lexer_next_token_is (parser->lexer,
17342 decl = make_unbound_class_template (parser->scope,
17344 /*complain=*/tf_error);
17346 decl = build_qualified_name (/*type=*/NULL_TREE,
17347 parser->scope, name,
17350 parser->qualifying_scope = parser->scope;
17351 parser->object_scope = NULL_TREE;
17353 else if (object_type)
17355 tree object_decl = NULL_TREE;
17356 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17357 OBJECT_TYPE is not a class. */
17358 if (CLASS_TYPE_P (object_type))
17359 /* If the OBJECT_TYPE is a template specialization, it may
17360 be instantiated during name lookup. In that case, errors
17361 may be issued. Even if we rollback the current tentative
17362 parse, those errors are valid. */
17363 object_decl = lookup_member (object_type,
17366 tag_type != none_type);
17367 /* Look it up in the enclosing context, too. */
17368 decl = lookup_name_real (name, tag_type != none_type,
17370 /*block_p=*/true, is_namespace, flags);
17371 parser->object_scope = object_type;
17372 parser->qualifying_scope = NULL_TREE;
17374 decl = object_decl;
17378 decl = lookup_name_real (name, tag_type != none_type,
17380 /*block_p=*/true, is_namespace, flags);
17381 parser->qualifying_scope = NULL_TREE;
17382 parser->object_scope = NULL_TREE;
17385 /* If the lookup failed, let our caller know. */
17386 if (!decl || decl == error_mark_node)
17387 return error_mark_node;
17389 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17390 if (TREE_CODE (decl) == TREE_LIST)
17392 if (ambiguous_decls)
17393 *ambiguous_decls = decl;
17394 /* The error message we have to print is too complicated for
17395 cp_parser_error, so we incorporate its actions directly. */
17396 if (!cp_parser_simulate_error (parser))
17398 error_at (name_location, "reference to %qD is ambiguous",
17400 print_candidates (decl);
17402 return error_mark_node;
17405 gcc_assert (DECL_P (decl)
17406 || TREE_CODE (decl) == OVERLOAD
17407 || TREE_CODE (decl) == SCOPE_REF
17408 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17409 || BASELINK_P (decl));
17411 /* If we have resolved the name of a member declaration, check to
17412 see if the declaration is accessible. When the name resolves to
17413 set of overloaded functions, accessibility is checked when
17414 overload resolution is done.
17416 During an explicit instantiation, access is not checked at all,
17417 as per [temp.explicit]. */
17419 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17424 /* Like cp_parser_lookup_name, but for use in the typical case where
17425 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17426 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17429 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17431 return cp_parser_lookup_name (parser, name,
17433 /*is_template=*/false,
17434 /*is_namespace=*/false,
17435 /*check_dependency=*/true,
17436 /*ambiguous_decls=*/NULL,
17440 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17441 the current context, return the TYPE_DECL. If TAG_NAME_P is
17442 true, the DECL indicates the class being defined in a class-head,
17443 or declared in an elaborated-type-specifier.
17445 Otherwise, return DECL. */
17448 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17450 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17451 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17454 template <typename T> struct B;
17457 template <typename T> struct A::B {};
17459 Similarly, in an elaborated-type-specifier:
17461 namespace N { struct X{}; }
17464 template <typename T> friend struct N::X;
17467 However, if the DECL refers to a class type, and we are in
17468 the scope of the class, then the name lookup automatically
17469 finds the TYPE_DECL created by build_self_reference rather
17470 than a TEMPLATE_DECL. For example, in:
17472 template <class T> struct S {
17476 there is no need to handle such case. */
17478 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17479 return DECL_TEMPLATE_RESULT (decl);
17484 /* If too many, or too few, template-parameter lists apply to the
17485 declarator, issue an error message. Returns TRUE if all went well,
17486 and FALSE otherwise. */
17489 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17490 cp_declarator *declarator,
17491 location_t declarator_location)
17493 unsigned num_templates;
17495 /* We haven't seen any classes that involve template parameters yet. */
17498 switch (declarator->kind)
17501 if (declarator->u.id.qualifying_scope)
17506 scope = declarator->u.id.qualifying_scope;
17507 member = declarator->u.id.unqualified_name;
17509 while (scope && CLASS_TYPE_P (scope))
17511 /* You're supposed to have one `template <...>'
17512 for every template class, but you don't need one
17513 for a full specialization. For example:
17515 template <class T> struct S{};
17516 template <> struct S<int> { void f(); };
17517 void S<int>::f () {}
17519 is correct; there shouldn't be a `template <>' for
17520 the definition of `S<int>::f'. */
17521 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17522 /* If SCOPE does not have template information of any
17523 kind, then it is not a template, nor is it nested
17524 within a template. */
17526 if (explicit_class_specialization_p (scope))
17528 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17531 scope = TYPE_CONTEXT (scope);
17534 else if (TREE_CODE (declarator->u.id.unqualified_name)
17535 == TEMPLATE_ID_EXPR)
17536 /* If the DECLARATOR has the form `X<y>' then it uses one
17537 additional level of template parameters. */
17540 return cp_parser_check_template_parameters
17541 (parser, num_templates, declarator_location, declarator);
17547 case cdk_reference:
17549 return (cp_parser_check_declarator_template_parameters
17550 (parser, declarator->declarator, declarator_location));
17556 gcc_unreachable ();
17561 /* NUM_TEMPLATES were used in the current declaration. If that is
17562 invalid, return FALSE and issue an error messages. Otherwise,
17563 return TRUE. If DECLARATOR is non-NULL, then we are checking a
17564 declarator and we can print more accurate diagnostics. */
17567 cp_parser_check_template_parameters (cp_parser* parser,
17568 unsigned num_templates,
17569 location_t location,
17570 cp_declarator *declarator)
17572 /* If there are the same number of template classes and parameter
17573 lists, that's OK. */
17574 if (parser->num_template_parameter_lists == num_templates)
17576 /* If there are more, but only one more, then we are referring to a
17577 member template. That's OK too. */
17578 if (parser->num_template_parameter_lists == num_templates + 1)
17580 /* If there are more template classes than parameter lists, we have
17583 template <class T> void S<T>::R<T>::f (); */
17584 if (parser->num_template_parameter_lists < num_templates)
17587 error_at (location, "specializing member %<%T::%E%> "
17588 "requires %<template<>%> syntax",
17589 declarator->u.id.qualifying_scope,
17590 declarator->u.id.unqualified_name);
17592 error_at (location, "too few template-parameter-lists");
17595 /* Otherwise, there are too many template parameter lists. We have
17598 template <class T> template <class U> void S::f(); */
17599 error_at (location, "too many template-parameter-lists");
17603 /* Parse an optional `::' token indicating that the following name is
17604 from the global namespace. If so, PARSER->SCOPE is set to the
17605 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17606 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17607 Returns the new value of PARSER->SCOPE, if the `::' token is
17608 present, and NULL_TREE otherwise. */
17611 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17615 /* Peek at the next token. */
17616 token = cp_lexer_peek_token (parser->lexer);
17617 /* If we're looking at a `::' token then we're starting from the
17618 global namespace, not our current location. */
17619 if (token->type == CPP_SCOPE)
17621 /* Consume the `::' token. */
17622 cp_lexer_consume_token (parser->lexer);
17623 /* Set the SCOPE so that we know where to start the lookup. */
17624 parser->scope = global_namespace;
17625 parser->qualifying_scope = global_namespace;
17626 parser->object_scope = NULL_TREE;
17628 return parser->scope;
17630 else if (!current_scope_valid_p)
17632 parser->scope = NULL_TREE;
17633 parser->qualifying_scope = NULL_TREE;
17634 parser->object_scope = NULL_TREE;
17640 /* Returns TRUE if the upcoming token sequence is the start of a
17641 constructor declarator. If FRIEND_P is true, the declarator is
17642 preceded by the `friend' specifier. */
17645 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17647 bool constructor_p;
17648 tree type_decl = NULL_TREE;
17649 bool nested_name_p;
17650 cp_token *next_token;
17652 /* The common case is that this is not a constructor declarator, so
17653 try to avoid doing lots of work if at all possible. It's not
17654 valid declare a constructor at function scope. */
17655 if (parser->in_function_body)
17657 /* And only certain tokens can begin a constructor declarator. */
17658 next_token = cp_lexer_peek_token (parser->lexer);
17659 if (next_token->type != CPP_NAME
17660 && next_token->type != CPP_SCOPE
17661 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17662 && next_token->type != CPP_TEMPLATE_ID)
17665 /* Parse tentatively; we are going to roll back all of the tokens
17667 cp_parser_parse_tentatively (parser);
17668 /* Assume that we are looking at a constructor declarator. */
17669 constructor_p = true;
17671 /* Look for the optional `::' operator. */
17672 cp_parser_global_scope_opt (parser,
17673 /*current_scope_valid_p=*/false);
17674 /* Look for the nested-name-specifier. */
17676 = (cp_parser_nested_name_specifier_opt (parser,
17677 /*typename_keyword_p=*/false,
17678 /*check_dependency_p=*/false,
17680 /*is_declaration=*/false)
17682 /* Outside of a class-specifier, there must be a
17683 nested-name-specifier. */
17684 if (!nested_name_p &&
17685 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17687 constructor_p = false;
17688 /* If we still think that this might be a constructor-declarator,
17689 look for a class-name. */
17694 template <typename T> struct S { S(); };
17695 template <typename T> S<T>::S ();
17697 we must recognize that the nested `S' names a class.
17700 template <typename T> S<T>::S<T> ();
17702 we must recognize that the nested `S' names a template. */
17703 type_decl = cp_parser_class_name (parser,
17704 /*typename_keyword_p=*/false,
17705 /*template_keyword_p=*/false,
17707 /*check_dependency_p=*/false,
17708 /*class_head_p=*/false,
17709 /*is_declaration=*/false);
17710 /* If there was no class-name, then this is not a constructor. */
17711 constructor_p = !cp_parser_error_occurred (parser);
17714 /* If we're still considering a constructor, we have to see a `(',
17715 to begin the parameter-declaration-clause, followed by either a
17716 `)', an `...', or a decl-specifier. We need to check for a
17717 type-specifier to avoid being fooled into thinking that:
17721 is a constructor. (It is actually a function named `f' that
17722 takes one parameter (of type `int') and returns a value of type
17725 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17727 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17728 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17729 /* A parameter declaration begins with a decl-specifier,
17730 which is either the "attribute" keyword, a storage class
17731 specifier, or (usually) a type-specifier. */
17732 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17735 tree pushed_scope = NULL_TREE;
17736 unsigned saved_num_template_parameter_lists;
17738 /* Names appearing in the type-specifier should be looked up
17739 in the scope of the class. */
17740 if (current_class_type)
17744 type = TREE_TYPE (type_decl);
17745 if (TREE_CODE (type) == TYPENAME_TYPE)
17747 type = resolve_typename_type (type,
17748 /*only_current_p=*/false);
17749 if (TREE_CODE (type) == TYPENAME_TYPE)
17751 cp_parser_abort_tentative_parse (parser);
17755 pushed_scope = push_scope (type);
17758 /* Inside the constructor parameter list, surrounding
17759 template-parameter-lists do not apply. */
17760 saved_num_template_parameter_lists
17761 = parser->num_template_parameter_lists;
17762 parser->num_template_parameter_lists = 0;
17764 /* Look for the type-specifier. */
17765 cp_parser_type_specifier (parser,
17766 CP_PARSER_FLAGS_NONE,
17767 /*decl_specs=*/NULL,
17768 /*is_declarator=*/true,
17769 /*declares_class_or_enum=*/NULL,
17770 /*is_cv_qualifier=*/NULL);
17772 parser->num_template_parameter_lists
17773 = saved_num_template_parameter_lists;
17775 /* Leave the scope of the class. */
17777 pop_scope (pushed_scope);
17779 constructor_p = !cp_parser_error_occurred (parser);
17783 constructor_p = false;
17784 /* We did not really want to consume any tokens. */
17785 cp_parser_abort_tentative_parse (parser);
17787 return constructor_p;
17790 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17791 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17792 they must be performed once we are in the scope of the function.
17794 Returns the function defined. */
17797 cp_parser_function_definition_from_specifiers_and_declarator
17798 (cp_parser* parser,
17799 cp_decl_specifier_seq *decl_specifiers,
17801 const cp_declarator *declarator)
17806 /* Begin the function-definition. */
17807 success_p = start_function (decl_specifiers, declarator, attributes);
17809 /* The things we're about to see are not directly qualified by any
17810 template headers we've seen thus far. */
17811 reset_specialization ();
17813 /* If there were names looked up in the decl-specifier-seq that we
17814 did not check, check them now. We must wait until we are in the
17815 scope of the function to perform the checks, since the function
17816 might be a friend. */
17817 perform_deferred_access_checks ();
17821 /* Skip the entire function. */
17822 cp_parser_skip_to_end_of_block_or_statement (parser);
17823 fn = error_mark_node;
17825 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17827 /* Seen already, skip it. An error message has already been output. */
17828 cp_parser_skip_to_end_of_block_or_statement (parser);
17829 fn = current_function_decl;
17830 current_function_decl = NULL_TREE;
17831 /* If this is a function from a class, pop the nested class. */
17832 if (current_class_name)
17833 pop_nested_class ();
17836 fn = cp_parser_function_definition_after_declarator (parser,
17837 /*inline_p=*/false);
17842 /* Parse the part of a function-definition that follows the
17843 declarator. INLINE_P is TRUE iff this function is an inline
17844 function defined with a class-specifier.
17846 Returns the function defined. */
17849 cp_parser_function_definition_after_declarator (cp_parser* parser,
17853 bool ctor_initializer_p = false;
17854 bool saved_in_unbraced_linkage_specification_p;
17855 bool saved_in_function_body;
17856 unsigned saved_num_template_parameter_lists;
17859 saved_in_function_body = parser->in_function_body;
17860 parser->in_function_body = true;
17861 /* If the next token is `return', then the code may be trying to
17862 make use of the "named return value" extension that G++ used to
17864 token = cp_lexer_peek_token (parser->lexer);
17865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17867 /* Consume the `return' keyword. */
17868 cp_lexer_consume_token (parser->lexer);
17869 /* Look for the identifier that indicates what value is to be
17871 cp_parser_identifier (parser);
17872 /* Issue an error message. */
17873 error_at (token->location,
17874 "named return values are no longer supported");
17875 /* Skip tokens until we reach the start of the function body. */
17878 cp_token *token = cp_lexer_peek_token (parser->lexer);
17879 if (token->type == CPP_OPEN_BRACE
17880 || token->type == CPP_EOF
17881 || token->type == CPP_PRAGMA_EOL)
17883 cp_lexer_consume_token (parser->lexer);
17886 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17887 anything declared inside `f'. */
17888 saved_in_unbraced_linkage_specification_p
17889 = parser->in_unbraced_linkage_specification_p;
17890 parser->in_unbraced_linkage_specification_p = false;
17891 /* Inside the function, surrounding template-parameter-lists do not
17893 saved_num_template_parameter_lists
17894 = parser->num_template_parameter_lists;
17895 parser->num_template_parameter_lists = 0;
17896 /* If the next token is `try', then we are looking at a
17897 function-try-block. */
17898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17899 ctor_initializer_p = cp_parser_function_try_block (parser);
17900 /* A function-try-block includes the function-body, so we only do
17901 this next part if we're not processing a function-try-block. */
17904 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17906 /* Finish the function. */
17907 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17908 (inline_p ? 2 : 0));
17909 /* Generate code for it, if necessary. */
17910 expand_or_defer_fn (fn);
17911 /* Restore the saved values. */
17912 parser->in_unbraced_linkage_specification_p
17913 = saved_in_unbraced_linkage_specification_p;
17914 parser->num_template_parameter_lists
17915 = saved_num_template_parameter_lists;
17916 parser->in_function_body = saved_in_function_body;
17921 /* Parse a template-declaration, assuming that the `export' (and
17922 `extern') keywords, if present, has already been scanned. MEMBER_P
17923 is as for cp_parser_template_declaration. */
17926 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17928 tree decl = NULL_TREE;
17929 VEC (deferred_access_check,gc) *checks;
17930 tree parameter_list;
17931 bool friend_p = false;
17932 bool need_lang_pop;
17935 /* Look for the `template' keyword. */
17936 token = cp_lexer_peek_token (parser->lexer);
17937 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17941 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17943 if (at_class_scope_p () && current_function_decl)
17945 /* 14.5.2.2 [temp.mem]
17947 A local class shall not have member templates. */
17948 error_at (token->location,
17949 "invalid declaration of member template in local class");
17950 cp_parser_skip_to_end_of_block_or_statement (parser);
17955 A template ... shall not have C linkage. */
17956 if (current_lang_name == lang_name_c)
17958 error_at (token->location, "template with C linkage");
17959 /* Give it C++ linkage to avoid confusing other parts of the
17961 push_lang_context (lang_name_cplusplus);
17962 need_lang_pop = true;
17965 need_lang_pop = false;
17967 /* We cannot perform access checks on the template parameter
17968 declarations until we know what is being declared, just as we
17969 cannot check the decl-specifier list. */
17970 push_deferring_access_checks (dk_deferred);
17972 /* If the next token is `>', then we have an invalid
17973 specialization. Rather than complain about an invalid template
17974 parameter, issue an error message here. */
17975 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17977 cp_parser_error (parser, "invalid explicit specialization");
17978 begin_specialization ();
17979 parameter_list = NULL_TREE;
17982 /* Parse the template parameters. */
17983 parameter_list = cp_parser_template_parameter_list (parser);
17985 /* Get the deferred access checks from the parameter list. These
17986 will be checked once we know what is being declared, as for a
17987 member template the checks must be performed in the scope of the
17988 class containing the member. */
17989 checks = get_deferred_access_checks ();
17991 /* Look for the `>'. */
17992 cp_parser_skip_to_end_of_template_parameter_list (parser);
17993 /* We just processed one more parameter list. */
17994 ++parser->num_template_parameter_lists;
17995 /* If the next token is `template', there are more template
17997 if (cp_lexer_next_token_is_keyword (parser->lexer,
17999 cp_parser_template_declaration_after_export (parser, member_p);
18002 /* There are no access checks when parsing a template, as we do not
18003 know if a specialization will be a friend. */
18004 push_deferring_access_checks (dk_no_check);
18005 token = cp_lexer_peek_token (parser->lexer);
18006 decl = cp_parser_single_declaration (parser,
18009 /*explicit_specialization_p=*/false,
18011 pop_deferring_access_checks ();
18013 /* If this is a member template declaration, let the front
18015 if (member_p && !friend_p && decl)
18017 if (TREE_CODE (decl) == TYPE_DECL)
18018 cp_parser_check_access_in_redeclaration (decl, token->location);
18020 decl = finish_member_template_decl (decl);
18022 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18023 make_friend_class (current_class_type, TREE_TYPE (decl),
18024 /*complain=*/true);
18026 /* We are done with the current parameter list. */
18027 --parser->num_template_parameter_lists;
18029 pop_deferring_access_checks ();
18032 finish_template_decl (parameter_list);
18034 /* Register member declarations. */
18035 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18036 finish_member_declaration (decl);
18037 /* For the erroneous case of a template with C linkage, we pushed an
18038 implicit C++ linkage scope; exit that scope now. */
18040 pop_lang_context ();
18041 /* If DECL is a function template, we must return to parse it later.
18042 (Even though there is no definition, there might be default
18043 arguments that need handling.) */
18044 if (member_p && decl
18045 && (TREE_CODE (decl) == FUNCTION_DECL
18046 || DECL_FUNCTION_TEMPLATE_P (decl)))
18047 TREE_VALUE (parser->unparsed_functions_queues)
18048 = tree_cons (NULL_TREE, decl,
18049 TREE_VALUE (parser->unparsed_functions_queues));
18052 /* Perform the deferred access checks from a template-parameter-list.
18053 CHECKS is a TREE_LIST of access checks, as returned by
18054 get_deferred_access_checks. */
18057 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18059 ++processing_template_parmlist;
18060 perform_access_checks (checks);
18061 --processing_template_parmlist;
18064 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18065 `function-definition' sequence. MEMBER_P is true, this declaration
18066 appears in a class scope.
18068 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18069 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18072 cp_parser_single_declaration (cp_parser* parser,
18073 VEC (deferred_access_check,gc)* checks,
18075 bool explicit_specialization_p,
18078 int declares_class_or_enum;
18079 tree decl = NULL_TREE;
18080 cp_decl_specifier_seq decl_specifiers;
18081 bool function_definition_p = false;
18082 cp_token *decl_spec_token_start;
18084 /* This function is only used when processing a template
18086 gcc_assert (innermost_scope_kind () == sk_template_parms
18087 || innermost_scope_kind () == sk_template_spec);
18089 /* Defer access checks until we know what is being declared. */
18090 push_deferring_access_checks (dk_deferred);
18092 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18094 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18095 cp_parser_decl_specifier_seq (parser,
18096 CP_PARSER_FLAGS_OPTIONAL,
18098 &declares_class_or_enum);
18100 *friend_p = cp_parser_friend_p (&decl_specifiers);
18102 /* There are no template typedefs. */
18103 if (decl_specifiers.specs[(int) ds_typedef])
18105 error_at (decl_spec_token_start->location,
18106 "template declaration of %<typedef%>");
18107 decl = error_mark_node;
18110 /* Gather up the access checks that occurred the
18111 decl-specifier-seq. */
18112 stop_deferring_access_checks ();
18114 /* Check for the declaration of a template class. */
18115 if (declares_class_or_enum)
18117 if (cp_parser_declares_only_class_p (parser))
18119 decl = shadow_tag (&decl_specifiers);
18124 friend template <typename T> struct A<T>::B;
18127 A<T>::B will be represented by a TYPENAME_TYPE, and
18128 therefore not recognized by shadow_tag. */
18129 if (friend_p && *friend_p
18131 && decl_specifiers.type
18132 && TYPE_P (decl_specifiers.type))
18133 decl = decl_specifiers.type;
18135 if (decl && decl != error_mark_node)
18136 decl = TYPE_NAME (decl);
18138 decl = error_mark_node;
18140 /* Perform access checks for template parameters. */
18141 cp_parser_perform_template_parameter_access_checks (checks);
18144 /* If it's not a template class, try for a template function. If
18145 the next token is a `;', then this declaration does not declare
18146 anything. But, if there were errors in the decl-specifiers, then
18147 the error might well have come from an attempted class-specifier.
18148 In that case, there's no need to warn about a missing declarator. */
18150 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18151 || decl_specifiers.type != error_mark_node))
18153 decl = cp_parser_init_declarator (parser,
18156 /*function_definition_allowed_p=*/true,
18158 declares_class_or_enum,
18159 &function_definition_p);
18161 /* 7.1.1-1 [dcl.stc]
18163 A storage-class-specifier shall not be specified in an explicit
18164 specialization... */
18166 && explicit_specialization_p
18167 && decl_specifiers.storage_class != sc_none)
18169 error_at (decl_spec_token_start->location,
18170 "explicit template specialization cannot have a storage class");
18171 decl = error_mark_node;
18175 pop_deferring_access_checks ();
18177 /* Clear any current qualification; whatever comes next is the start
18178 of something new. */
18179 parser->scope = NULL_TREE;
18180 parser->qualifying_scope = NULL_TREE;
18181 parser->object_scope = NULL_TREE;
18182 /* Look for a trailing `;' after the declaration. */
18183 if (!function_definition_p
18184 && (decl == error_mark_node
18185 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18186 cp_parser_skip_to_end_of_block_or_statement (parser);
18191 /* Parse a cast-expression that is not the operand of a unary "&". */
18194 cp_parser_simple_cast_expression (cp_parser *parser)
18196 return cp_parser_cast_expression (parser, /*address_p=*/false,
18197 /*cast_p=*/false, NULL);
18200 /* Parse a functional cast to TYPE. Returns an expression
18201 representing the cast. */
18204 cp_parser_functional_cast (cp_parser* parser, tree type)
18207 tree expression_list;
18211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18213 maybe_warn_cpp0x ("extended initializer lists");
18214 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18215 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18216 if (TREE_CODE (type) == TYPE_DECL)
18217 type = TREE_TYPE (type);
18218 return finish_compound_literal (type, expression_list);
18222 vec = cp_parser_parenthesized_expression_list (parser, false,
18224 /*allow_expansion_p=*/true,
18225 /*non_constant_p=*/NULL);
18227 expression_list = error_mark_node;
18230 expression_list = build_tree_list_vec (vec);
18231 release_tree_vector (vec);
18234 cast = build_functional_cast (type, expression_list,
18235 tf_warning_or_error);
18236 /* [expr.const]/1: In an integral constant expression "only type
18237 conversions to integral or enumeration type can be used". */
18238 if (TREE_CODE (type) == TYPE_DECL)
18239 type = TREE_TYPE (type);
18240 if (cast != error_mark_node
18241 && !cast_valid_in_integral_constant_expression_p (type)
18242 && (cp_parser_non_integral_constant_expression
18243 (parser, "a call to a constructor")))
18244 return error_mark_node;
18248 /* Save the tokens that make up the body of a member function defined
18249 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18250 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18251 specifiers applied to the declaration. Returns the FUNCTION_DECL
18252 for the member function. */
18255 cp_parser_save_member_function_body (cp_parser* parser,
18256 cp_decl_specifier_seq *decl_specifiers,
18257 cp_declarator *declarator,
18264 /* Create the FUNCTION_DECL. */
18265 fn = grokmethod (decl_specifiers, declarator, attributes);
18266 /* If something went badly wrong, bail out now. */
18267 if (fn == error_mark_node)
18269 /* If there's a function-body, skip it. */
18270 if (cp_parser_token_starts_function_definition_p
18271 (cp_lexer_peek_token (parser->lexer)))
18272 cp_parser_skip_to_end_of_block_or_statement (parser);
18273 return error_mark_node;
18276 /* Remember it, if there default args to post process. */
18277 cp_parser_save_default_args (parser, fn);
18279 /* Save away the tokens that make up the body of the
18281 first = parser->lexer->next_token;
18282 /* We can have braced-init-list mem-initializers before the fn body. */
18283 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18285 cp_lexer_consume_token (parser->lexer);
18286 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18287 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18289 /* cache_group will stop after an un-nested { } pair, too. */
18290 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18293 /* variadic mem-inits have ... after the ')'. */
18294 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18295 cp_lexer_consume_token (parser->lexer);
18298 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18299 /* Handle function try blocks. */
18300 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18301 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18302 last = parser->lexer->next_token;
18304 /* Save away the inline definition; we will process it when the
18305 class is complete. */
18306 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18307 DECL_PENDING_INLINE_P (fn) = 1;
18309 /* We need to know that this was defined in the class, so that
18310 friend templates are handled correctly. */
18311 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18313 /* Add FN to the queue of functions to be parsed later. */
18314 TREE_VALUE (parser->unparsed_functions_queues)
18315 = tree_cons (NULL_TREE, fn,
18316 TREE_VALUE (parser->unparsed_functions_queues));
18321 /* Parse a template-argument-list, as well as the trailing ">" (but
18322 not the opening ">"). See cp_parser_template_argument_list for the
18326 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18330 tree saved_qualifying_scope;
18331 tree saved_object_scope;
18332 bool saved_greater_than_is_operator_p;
18333 int saved_unevaluated_operand;
18334 int saved_inhibit_evaluation_warnings;
18338 When parsing a template-id, the first non-nested `>' is taken as
18339 the end of the template-argument-list rather than a greater-than
18341 saved_greater_than_is_operator_p
18342 = parser->greater_than_is_operator_p;
18343 parser->greater_than_is_operator_p = false;
18344 /* Parsing the argument list may modify SCOPE, so we save it
18346 saved_scope = parser->scope;
18347 saved_qualifying_scope = parser->qualifying_scope;
18348 saved_object_scope = parser->object_scope;
18349 /* We need to evaluate the template arguments, even though this
18350 template-id may be nested within a "sizeof". */
18351 saved_unevaluated_operand = cp_unevaluated_operand;
18352 cp_unevaluated_operand = 0;
18353 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18354 c_inhibit_evaluation_warnings = 0;
18355 /* Parse the template-argument-list itself. */
18356 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18357 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18358 arguments = NULL_TREE;
18360 arguments = cp_parser_template_argument_list (parser);
18361 /* Look for the `>' that ends the template-argument-list. If we find
18362 a '>>' instead, it's probably just a typo. */
18363 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18365 if (cxx_dialect != cxx98)
18367 /* In C++0x, a `>>' in a template argument list or cast
18368 expression is considered to be two separate `>'
18369 tokens. So, change the current token to a `>', but don't
18370 consume it: it will be consumed later when the outer
18371 template argument list (or cast expression) is parsed.
18372 Note that this replacement of `>' for `>>' is necessary
18373 even if we are parsing tentatively: in the tentative
18374 case, after calling
18375 cp_parser_enclosed_template_argument_list we will always
18376 throw away all of the template arguments and the first
18377 closing `>', either because the template argument list
18378 was erroneous or because we are replacing those tokens
18379 with a CPP_TEMPLATE_ID token. The second `>' (which will
18380 not have been thrown away) is needed either to close an
18381 outer template argument list or to complete a new-style
18383 cp_token *token = cp_lexer_peek_token (parser->lexer);
18384 token->type = CPP_GREATER;
18386 else if (!saved_greater_than_is_operator_p)
18388 /* If we're in a nested template argument list, the '>>' has
18389 to be a typo for '> >'. We emit the error message, but we
18390 continue parsing and we push a '>' as next token, so that
18391 the argument list will be parsed correctly. Note that the
18392 global source location is still on the token before the
18393 '>>', so we need to say explicitly where we want it. */
18394 cp_token *token = cp_lexer_peek_token (parser->lexer);
18395 error_at (token->location, "%<>>%> should be %<> >%> "
18396 "within a nested template argument list");
18398 token->type = CPP_GREATER;
18402 /* If this is not a nested template argument list, the '>>'
18403 is a typo for '>'. Emit an error message and continue.
18404 Same deal about the token location, but here we can get it
18405 right by consuming the '>>' before issuing the diagnostic. */
18406 cp_token *token = cp_lexer_consume_token (parser->lexer);
18407 error_at (token->location,
18408 "spurious %<>>%>, use %<>%> to terminate "
18409 "a template argument list");
18413 cp_parser_skip_to_end_of_template_parameter_list (parser);
18414 /* The `>' token might be a greater-than operator again now. */
18415 parser->greater_than_is_operator_p
18416 = saved_greater_than_is_operator_p;
18417 /* Restore the SAVED_SCOPE. */
18418 parser->scope = saved_scope;
18419 parser->qualifying_scope = saved_qualifying_scope;
18420 parser->object_scope = saved_object_scope;
18421 cp_unevaluated_operand = saved_unevaluated_operand;
18422 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18427 /* MEMBER_FUNCTION is a member function, or a friend. If default
18428 arguments, or the body of the function have not yet been parsed,
18432 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18434 /* If this member is a template, get the underlying
18436 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18437 member_function = DECL_TEMPLATE_RESULT (member_function);
18439 /* There should not be any class definitions in progress at this
18440 point; the bodies of members are only parsed outside of all class
18442 gcc_assert (parser->num_classes_being_defined == 0);
18443 /* While we're parsing the member functions we might encounter more
18444 classes. We want to handle them right away, but we don't want
18445 them getting mixed up with functions that are currently in the
18447 parser->unparsed_functions_queues
18448 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18450 /* Make sure that any template parameters are in scope. */
18451 maybe_begin_member_template_processing (member_function);
18453 /* If the body of the function has not yet been parsed, parse it
18455 if (DECL_PENDING_INLINE_P (member_function))
18457 tree function_scope;
18458 cp_token_cache *tokens;
18460 /* The function is no longer pending; we are processing it. */
18461 tokens = DECL_PENDING_INLINE_INFO (member_function);
18462 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18463 DECL_PENDING_INLINE_P (member_function) = 0;
18465 /* If this is a local class, enter the scope of the containing
18467 function_scope = current_function_decl;
18468 if (function_scope)
18469 push_function_context ();
18471 /* Push the body of the function onto the lexer stack. */
18472 cp_parser_push_lexer_for_tokens (parser, tokens);
18474 /* Let the front end know that we going to be defining this
18476 start_preparsed_function (member_function, NULL_TREE,
18477 SF_PRE_PARSED | SF_INCLASS_INLINE);
18479 /* Don't do access checking if it is a templated function. */
18480 if (processing_template_decl)
18481 push_deferring_access_checks (dk_no_check);
18483 /* Now, parse the body of the function. */
18484 cp_parser_function_definition_after_declarator (parser,
18485 /*inline_p=*/true);
18487 if (processing_template_decl)
18488 pop_deferring_access_checks ();
18490 /* Leave the scope of the containing function. */
18491 if (function_scope)
18492 pop_function_context ();
18493 cp_parser_pop_lexer (parser);
18496 /* Remove any template parameters from the symbol table. */
18497 maybe_end_member_template_processing ();
18499 /* Restore the queue. */
18500 parser->unparsed_functions_queues
18501 = TREE_CHAIN (parser->unparsed_functions_queues);
18504 /* If DECL contains any default args, remember it on the unparsed
18505 functions queue. */
18508 cp_parser_save_default_args (cp_parser* parser, tree decl)
18512 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18514 probe = TREE_CHAIN (probe))
18515 if (TREE_PURPOSE (probe))
18517 TREE_PURPOSE (parser->unparsed_functions_queues)
18518 = tree_cons (current_class_type, decl,
18519 TREE_PURPOSE (parser->unparsed_functions_queues));
18524 /* FN is a FUNCTION_DECL which may contains a parameter with an
18525 unparsed DEFAULT_ARG. Parse the default args now. This function
18526 assumes that the current scope is the scope in which the default
18527 argument should be processed. */
18530 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18532 bool saved_local_variables_forbidden_p;
18535 /* While we're parsing the default args, we might (due to the
18536 statement expression extension) encounter more classes. We want
18537 to handle them right away, but we don't want them getting mixed
18538 up with default args that are currently in the queue. */
18539 parser->unparsed_functions_queues
18540 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18542 /* Local variable names (and the `this' keyword) may not appear
18543 in a default argument. */
18544 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18545 parser->local_variables_forbidden_p = true;
18547 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18549 parm = TREE_CHAIN (parm))
18551 cp_token_cache *tokens;
18552 tree default_arg = TREE_PURPOSE (parm);
18554 VEC(tree,gc) *insts;
18561 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18562 /* This can happen for a friend declaration for a function
18563 already declared with default arguments. */
18566 /* Push the saved tokens for the default argument onto the parser's
18568 tokens = DEFARG_TOKENS (default_arg);
18569 cp_parser_push_lexer_for_tokens (parser, tokens);
18571 /* Parse the assignment-expression. */
18572 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18573 if (parsed_arg == error_mark_node)
18575 cp_parser_pop_lexer (parser);
18579 if (!processing_template_decl)
18580 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18582 TREE_PURPOSE (parm) = parsed_arg;
18584 /* Update any instantiations we've already created. */
18585 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18586 VEC_iterate (tree, insts, ix, copy); ix++)
18587 TREE_PURPOSE (copy) = parsed_arg;
18589 /* If the token stream has not been completely used up, then
18590 there was extra junk after the end of the default
18592 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18593 cp_parser_error (parser, "expected %<,%>");
18595 /* Revert to the main lexer. */
18596 cp_parser_pop_lexer (parser);
18599 /* Make sure no default arg is missing. */
18600 check_default_args (fn);
18602 /* Restore the state of local_variables_forbidden_p. */
18603 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18605 /* Restore the queue. */
18606 parser->unparsed_functions_queues
18607 = TREE_CHAIN (parser->unparsed_functions_queues);
18610 /* Parse the operand of `sizeof' (or a similar operator). Returns
18611 either a TYPE or an expression, depending on the form of the
18612 input. The KEYWORD indicates which kind of expression we have
18616 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18618 tree expr = NULL_TREE;
18619 const char *saved_message;
18621 bool saved_integral_constant_expression_p;
18622 bool saved_non_integral_constant_expression_p;
18623 bool pack_expansion_p = false;
18625 /* Types cannot be defined in a `sizeof' expression. Save away the
18627 saved_message = parser->type_definition_forbidden_message;
18628 /* And create the new one. */
18629 tmp = concat ("types may not be defined in %<",
18630 IDENTIFIER_POINTER (ridpointers[keyword]),
18631 "%> expressions", NULL);
18632 parser->type_definition_forbidden_message = tmp;
18634 /* The restrictions on constant-expressions do not apply inside
18635 sizeof expressions. */
18636 saved_integral_constant_expression_p
18637 = parser->integral_constant_expression_p;
18638 saved_non_integral_constant_expression_p
18639 = parser->non_integral_constant_expression_p;
18640 parser->integral_constant_expression_p = false;
18642 /* If it's a `...', then we are computing the length of a parameter
18644 if (keyword == RID_SIZEOF
18645 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18647 /* Consume the `...'. */
18648 cp_lexer_consume_token (parser->lexer);
18649 maybe_warn_variadic_templates ();
18651 /* Note that this is an expansion. */
18652 pack_expansion_p = true;
18655 /* Do not actually evaluate the expression. */
18656 ++cp_unevaluated_operand;
18657 ++c_inhibit_evaluation_warnings;
18658 /* If it's a `(', then we might be looking at the type-id
18660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18663 bool saved_in_type_id_in_expr_p;
18665 /* We can't be sure yet whether we're looking at a type-id or an
18667 cp_parser_parse_tentatively (parser);
18668 /* Consume the `('. */
18669 cp_lexer_consume_token (parser->lexer);
18670 /* Parse the type-id. */
18671 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18672 parser->in_type_id_in_expr_p = true;
18673 type = cp_parser_type_id (parser);
18674 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18675 /* Now, look for the trailing `)'. */
18676 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18677 /* If all went well, then we're done. */
18678 if (cp_parser_parse_definitely (parser))
18680 cp_decl_specifier_seq decl_specs;
18682 /* Build a trivial decl-specifier-seq. */
18683 clear_decl_specs (&decl_specs);
18684 decl_specs.type = type;
18686 /* Call grokdeclarator to figure out what type this is. */
18687 expr = grokdeclarator (NULL,
18691 /*attrlist=*/NULL);
18695 /* If the type-id production did not work out, then we must be
18696 looking at the unary-expression production. */
18698 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18699 /*cast_p=*/false, NULL);
18701 if (pack_expansion_p)
18702 /* Build a pack expansion. */
18703 expr = make_pack_expansion (expr);
18705 /* Go back to evaluating expressions. */
18706 --cp_unevaluated_operand;
18707 --c_inhibit_evaluation_warnings;
18709 /* Free the message we created. */
18711 /* And restore the old one. */
18712 parser->type_definition_forbidden_message = saved_message;
18713 parser->integral_constant_expression_p
18714 = saved_integral_constant_expression_p;
18715 parser->non_integral_constant_expression_p
18716 = saved_non_integral_constant_expression_p;
18721 /* If the current declaration has no declarator, return true. */
18724 cp_parser_declares_only_class_p (cp_parser *parser)
18726 /* If the next token is a `;' or a `,' then there is no
18728 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18729 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18732 /* Update the DECL_SPECS to reflect the storage class indicated by
18736 cp_parser_set_storage_class (cp_parser *parser,
18737 cp_decl_specifier_seq *decl_specs,
18739 location_t location)
18741 cp_storage_class storage_class;
18743 if (parser->in_unbraced_linkage_specification_p)
18745 error_at (location, "invalid use of %qD in linkage specification",
18746 ridpointers[keyword]);
18749 else if (decl_specs->storage_class != sc_none)
18751 decl_specs->conflicting_specifiers_p = true;
18755 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18756 && decl_specs->specs[(int) ds_thread])
18758 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
18759 decl_specs->specs[(int) ds_thread] = 0;
18765 storage_class = sc_auto;
18768 storage_class = sc_register;
18771 storage_class = sc_static;
18774 storage_class = sc_extern;
18777 storage_class = sc_mutable;
18780 gcc_unreachable ();
18782 decl_specs->storage_class = storage_class;
18784 /* A storage class specifier cannot be applied alongside a typedef
18785 specifier. If there is a typedef specifier present then set
18786 conflicting_specifiers_p which will trigger an error later
18787 on in grokdeclarator. */
18788 if (decl_specs->specs[(int)ds_typedef])
18789 decl_specs->conflicting_specifiers_p = true;
18792 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18793 is true, the type is a user-defined type; otherwise it is a
18794 built-in type specified by a keyword. */
18797 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18799 location_t location,
18800 bool user_defined_p)
18802 decl_specs->any_specifiers_p = true;
18804 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18805 (with, for example, in "typedef int wchar_t;") we remember that
18806 this is what happened. In system headers, we ignore these
18807 declarations so that G++ can work with system headers that are not
18809 if (decl_specs->specs[(int) ds_typedef]
18811 && (type_spec == boolean_type_node
18812 || type_spec == char16_type_node
18813 || type_spec == char32_type_node
18814 || type_spec == wchar_type_node)
18815 && (decl_specs->type
18816 || decl_specs->specs[(int) ds_long]
18817 || decl_specs->specs[(int) ds_short]
18818 || decl_specs->specs[(int) ds_unsigned]
18819 || decl_specs->specs[(int) ds_signed]))
18821 decl_specs->redefined_builtin_type = type_spec;
18822 if (!decl_specs->type)
18824 decl_specs->type = type_spec;
18825 decl_specs->user_defined_type_p = false;
18826 decl_specs->type_location = location;
18829 else if (decl_specs->type)
18830 decl_specs->multiple_types_p = true;
18833 decl_specs->type = type_spec;
18834 decl_specs->user_defined_type_p = user_defined_p;
18835 decl_specs->redefined_builtin_type = NULL_TREE;
18836 decl_specs->type_location = location;
18840 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18841 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18844 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18846 return decl_specifiers->specs[(int) ds_friend] != 0;
18849 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18850 issue an error message indicating that TOKEN_DESC was expected.
18852 Returns the token consumed, if the token had the appropriate type.
18853 Otherwise, returns NULL. */
18856 cp_parser_require (cp_parser* parser,
18857 enum cpp_ttype type,
18858 const char* token_desc)
18860 if (cp_lexer_next_token_is (parser->lexer, type))
18861 return cp_lexer_consume_token (parser->lexer);
18864 /* Output the MESSAGE -- unless we're parsing tentatively. */
18865 if (!cp_parser_simulate_error (parser))
18867 char *message = concat ("expected ", token_desc, NULL);
18868 cp_parser_error (parser, message);
18875 /* An error message is produced if the next token is not '>'.
18876 All further tokens are skipped until the desired token is
18877 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18880 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18882 /* Current level of '< ... >'. */
18883 unsigned level = 0;
18884 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18885 unsigned nesting_depth = 0;
18887 /* Are we ready, yet? If not, issue error message. */
18888 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18891 /* Skip tokens until the desired token is found. */
18894 /* Peek at the next token. */
18895 switch (cp_lexer_peek_token (parser->lexer)->type)
18898 if (!nesting_depth)
18903 if (cxx_dialect == cxx98)
18904 /* C++0x views the `>>' operator as two `>' tokens, but
18907 else if (!nesting_depth && level-- == 0)
18909 /* We've hit a `>>' where the first `>' closes the
18910 template argument list, and the second `>' is
18911 spurious. Just consume the `>>' and stop; we've
18912 already produced at least one error. */
18913 cp_lexer_consume_token (parser->lexer);
18916 /* Fall through for C++0x, so we handle the second `>' in
18920 if (!nesting_depth && level-- == 0)
18922 /* We've reached the token we want, consume it and stop. */
18923 cp_lexer_consume_token (parser->lexer);
18928 case CPP_OPEN_PAREN:
18929 case CPP_OPEN_SQUARE:
18933 case CPP_CLOSE_PAREN:
18934 case CPP_CLOSE_SQUARE:
18935 if (nesting_depth-- == 0)
18940 case CPP_PRAGMA_EOL:
18941 case CPP_SEMICOLON:
18942 case CPP_OPEN_BRACE:
18943 case CPP_CLOSE_BRACE:
18944 /* The '>' was probably forgotten, don't look further. */
18951 /* Consume this token. */
18952 cp_lexer_consume_token (parser->lexer);
18956 /* If the next token is the indicated keyword, consume it. Otherwise,
18957 issue an error message indicating that TOKEN_DESC was expected.
18959 Returns the token consumed, if the token had the appropriate type.
18960 Otherwise, returns NULL. */
18963 cp_parser_require_keyword (cp_parser* parser,
18965 const char* token_desc)
18967 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18969 if (token && token->keyword != keyword)
18971 dyn_string_t error_msg;
18973 /* Format the error message. */
18974 error_msg = dyn_string_new (0);
18975 dyn_string_append_cstr (error_msg, "expected ");
18976 dyn_string_append_cstr (error_msg, token_desc);
18977 cp_parser_error (parser, error_msg->s);
18978 dyn_string_delete (error_msg);
18985 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18986 function-definition. */
18989 cp_parser_token_starts_function_definition_p (cp_token* token)
18991 return (/* An ordinary function-body begins with an `{'. */
18992 token->type == CPP_OPEN_BRACE
18993 /* A ctor-initializer begins with a `:'. */
18994 || token->type == CPP_COLON
18995 /* A function-try-block begins with `try'. */
18996 || token->keyword == RID_TRY
18997 /* The named return value extension begins with `return'. */
18998 || token->keyword == RID_RETURN);
19001 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19005 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19009 token = cp_lexer_peek_token (parser->lexer);
19010 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19013 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19014 C++0x) ending a template-argument. */
19017 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19021 token = cp_lexer_peek_token (parser->lexer);
19022 return (token->type == CPP_COMMA
19023 || token->type == CPP_GREATER
19024 || token->type == CPP_ELLIPSIS
19025 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19028 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19029 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19032 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19037 token = cp_lexer_peek_nth_token (parser->lexer, n);
19038 if (token->type == CPP_LESS)
19040 /* Check for the sequence `<::' in the original code. It would be lexed as
19041 `[:', where `[' is a digraph, and there is no whitespace before
19043 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19046 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19047 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19053 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19054 or none_type otherwise. */
19056 static enum tag_types
19057 cp_parser_token_is_class_key (cp_token* token)
19059 switch (token->keyword)
19064 return record_type;
19073 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19076 cp_parser_check_class_key (enum tag_types class_key, tree type)
19078 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19079 permerror (input_location, "%qs tag used in naming %q#T",
19080 class_key == union_type ? "union"
19081 : class_key == record_type ? "struct" : "class",
19085 /* Issue an error message if DECL is redeclared with different
19086 access than its original declaration [class.access.spec/3].
19087 This applies to nested classes and nested class templates.
19091 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19093 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19096 if ((TREE_PRIVATE (decl)
19097 != (current_access_specifier == access_private_node))
19098 || (TREE_PROTECTED (decl)
19099 != (current_access_specifier == access_protected_node)))
19100 error_at (location, "%qD redeclared with different access", decl);
19103 /* Look for the `template' keyword, as a syntactic disambiguator.
19104 Return TRUE iff it is present, in which case it will be
19108 cp_parser_optional_template_keyword (cp_parser *parser)
19110 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19112 /* The `template' keyword can only be used within templates;
19113 outside templates the parser can always figure out what is a
19114 template and what is not. */
19115 if (!processing_template_decl)
19117 cp_token *token = cp_lexer_peek_token (parser->lexer);
19118 error_at (token->location,
19119 "%<template%> (as a disambiguator) is only allowed "
19120 "within templates");
19121 /* If this part of the token stream is rescanned, the same
19122 error message would be generated. So, we purge the token
19123 from the stream. */
19124 cp_lexer_purge_token (parser->lexer);
19129 /* Consume the `template' keyword. */
19130 cp_lexer_consume_token (parser->lexer);
19138 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19139 set PARSER->SCOPE, and perform other related actions. */
19142 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19145 struct tree_check *check_value;
19146 deferred_access_check *chk;
19147 VEC (deferred_access_check,gc) *checks;
19149 /* Get the stored value. */
19150 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19151 /* Perform any access checks that were deferred. */
19152 checks = check_value->checks;
19156 VEC_iterate (deferred_access_check, checks, i, chk) ;
19159 perform_or_defer_access_check (chk->binfo,
19164 /* Set the scope from the stored value. */
19165 parser->scope = check_value->value;
19166 parser->qualifying_scope = check_value->qualifying_scope;
19167 parser->object_scope = NULL_TREE;
19170 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19171 encounter the end of a block before what we were looking for. */
19174 cp_parser_cache_group (cp_parser *parser,
19175 enum cpp_ttype end,
19180 cp_token *token = cp_lexer_peek_token (parser->lexer);
19182 /* Abort a parenthesized expression if we encounter a semicolon. */
19183 if ((end == CPP_CLOSE_PAREN || depth == 0)
19184 && token->type == CPP_SEMICOLON)
19186 /* If we've reached the end of the file, stop. */
19187 if (token->type == CPP_EOF
19188 || (end != CPP_PRAGMA_EOL
19189 && token->type == CPP_PRAGMA_EOL))
19191 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19192 /* We've hit the end of an enclosing block, so there's been some
19193 kind of syntax error. */
19196 /* Consume the token. */
19197 cp_lexer_consume_token (parser->lexer);
19198 /* See if it starts a new group. */
19199 if (token->type == CPP_OPEN_BRACE)
19201 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19202 /* In theory this should probably check end == '}', but
19203 cp_parser_save_member_function_body needs it to exit
19204 after either '}' or ')' when called with ')'. */
19208 else if (token->type == CPP_OPEN_PAREN)
19210 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19211 if (depth == 0 && end == CPP_CLOSE_PAREN)
19214 else if (token->type == CPP_PRAGMA)
19215 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19216 else if (token->type == end)
19221 /* Begin parsing tentatively. We always save tokens while parsing
19222 tentatively so that if the tentative parsing fails we can restore the
19226 cp_parser_parse_tentatively (cp_parser* parser)
19228 /* Enter a new parsing context. */
19229 parser->context = cp_parser_context_new (parser->context);
19230 /* Begin saving tokens. */
19231 cp_lexer_save_tokens (parser->lexer);
19232 /* In order to avoid repetitive access control error messages,
19233 access checks are queued up until we are no longer parsing
19235 push_deferring_access_checks (dk_deferred);
19238 /* Commit to the currently active tentative parse. */
19241 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19243 cp_parser_context *context;
19246 /* Mark all of the levels as committed. */
19247 lexer = parser->lexer;
19248 for (context = parser->context; context->next; context = context->next)
19250 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19252 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19253 while (!cp_lexer_saving_tokens (lexer))
19254 lexer = lexer->next;
19255 cp_lexer_commit_tokens (lexer);
19259 /* Abort the currently active tentative parse. All consumed tokens
19260 will be rolled back, and no diagnostics will be issued. */
19263 cp_parser_abort_tentative_parse (cp_parser* parser)
19265 cp_parser_simulate_error (parser);
19266 /* Now, pretend that we want to see if the construct was
19267 successfully parsed. */
19268 cp_parser_parse_definitely (parser);
19271 /* Stop parsing tentatively. If a parse error has occurred, restore the
19272 token stream. Otherwise, commit to the tokens we have consumed.
19273 Returns true if no error occurred; false otherwise. */
19276 cp_parser_parse_definitely (cp_parser* parser)
19278 bool error_occurred;
19279 cp_parser_context *context;
19281 /* Remember whether or not an error occurred, since we are about to
19282 destroy that information. */
19283 error_occurred = cp_parser_error_occurred (parser);
19284 /* Remove the topmost context from the stack. */
19285 context = parser->context;
19286 parser->context = context->next;
19287 /* If no parse errors occurred, commit to the tentative parse. */
19288 if (!error_occurred)
19290 /* Commit to the tokens read tentatively, unless that was
19292 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19293 cp_lexer_commit_tokens (parser->lexer);
19295 pop_to_parent_deferring_access_checks ();
19297 /* Otherwise, if errors occurred, roll back our state so that things
19298 are just as they were before we began the tentative parse. */
19301 cp_lexer_rollback_tokens (parser->lexer);
19302 pop_deferring_access_checks ();
19304 /* Add the context to the front of the free list. */
19305 context->next = cp_parser_context_free_list;
19306 cp_parser_context_free_list = context;
19308 return !error_occurred;
19311 /* Returns true if we are parsing tentatively and are not committed to
19312 this tentative parse. */
19315 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19317 return (cp_parser_parsing_tentatively (parser)
19318 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19321 /* Returns nonzero iff an error has occurred during the most recent
19322 tentative parse. */
19325 cp_parser_error_occurred (cp_parser* parser)
19327 return (cp_parser_parsing_tentatively (parser)
19328 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19331 /* Returns nonzero if GNU extensions are allowed. */
19334 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19336 return parser->allow_gnu_extensions_p;
19339 /* Objective-C++ Productions */
19342 /* Parse an Objective-C expression, which feeds into a primary-expression
19346 objc-message-expression
19347 objc-string-literal
19348 objc-encode-expression
19349 objc-protocol-expression
19350 objc-selector-expression
19352 Returns a tree representation of the expression. */
19355 cp_parser_objc_expression (cp_parser* parser)
19357 /* Try to figure out what kind of declaration is present. */
19358 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19362 case CPP_OPEN_SQUARE:
19363 return cp_parser_objc_message_expression (parser);
19365 case CPP_OBJC_STRING:
19366 kwd = cp_lexer_consume_token (parser->lexer);
19367 return objc_build_string_object (kwd->u.value);
19370 switch (kwd->keyword)
19372 case RID_AT_ENCODE:
19373 return cp_parser_objc_encode_expression (parser);
19375 case RID_AT_PROTOCOL:
19376 return cp_parser_objc_protocol_expression (parser);
19378 case RID_AT_SELECTOR:
19379 return cp_parser_objc_selector_expression (parser);
19385 error_at (kwd->location,
19386 "misplaced %<@%D%> Objective-C++ construct",
19388 cp_parser_skip_to_end_of_block_or_statement (parser);
19391 return error_mark_node;
19394 /* Parse an Objective-C message expression.
19396 objc-message-expression:
19397 [ objc-message-receiver objc-message-args ]
19399 Returns a representation of an Objective-C message. */
19402 cp_parser_objc_message_expression (cp_parser* parser)
19404 tree receiver, messageargs;
19406 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19407 receiver = cp_parser_objc_message_receiver (parser);
19408 messageargs = cp_parser_objc_message_args (parser);
19409 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19411 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19414 /* Parse an objc-message-receiver.
19416 objc-message-receiver:
19418 simple-type-specifier
19420 Returns a representation of the type or expression. */
19423 cp_parser_objc_message_receiver (cp_parser* parser)
19427 /* An Objective-C message receiver may be either (1) a type
19428 or (2) an expression. */
19429 cp_parser_parse_tentatively (parser);
19430 rcv = cp_parser_expression (parser, false, NULL);
19432 if (cp_parser_parse_definitely (parser))
19435 rcv = cp_parser_simple_type_specifier (parser,
19436 /*decl_specs=*/NULL,
19437 CP_PARSER_FLAGS_NONE);
19439 return objc_get_class_reference (rcv);
19442 /* Parse the arguments and selectors comprising an Objective-C message.
19447 objc-selector-args , objc-comma-args
19449 objc-selector-args:
19450 objc-selector [opt] : assignment-expression
19451 objc-selector-args objc-selector [opt] : assignment-expression
19454 assignment-expression
19455 objc-comma-args , assignment-expression
19457 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19458 selector arguments and TREE_VALUE containing a list of comma
19462 cp_parser_objc_message_args (cp_parser* parser)
19464 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19465 bool maybe_unary_selector_p = true;
19466 cp_token *token = cp_lexer_peek_token (parser->lexer);
19468 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19470 tree selector = NULL_TREE, arg;
19472 if (token->type != CPP_COLON)
19473 selector = cp_parser_objc_selector (parser);
19475 /* Detect if we have a unary selector. */
19476 if (maybe_unary_selector_p
19477 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19478 return build_tree_list (selector, NULL_TREE);
19480 maybe_unary_selector_p = false;
19481 cp_parser_require (parser, CPP_COLON, "%<:%>");
19482 arg = cp_parser_assignment_expression (parser, false, NULL);
19485 = chainon (sel_args,
19486 build_tree_list (selector, arg));
19488 token = cp_lexer_peek_token (parser->lexer);
19491 /* Handle non-selector arguments, if any. */
19492 while (token->type == CPP_COMMA)
19496 cp_lexer_consume_token (parser->lexer);
19497 arg = cp_parser_assignment_expression (parser, false, NULL);
19500 = chainon (addl_args,
19501 build_tree_list (NULL_TREE, arg));
19503 token = cp_lexer_peek_token (parser->lexer);
19506 return build_tree_list (sel_args, addl_args);
19509 /* Parse an Objective-C encode expression.
19511 objc-encode-expression:
19512 @encode objc-typename
19514 Returns an encoded representation of the type argument. */
19517 cp_parser_objc_encode_expression (cp_parser* parser)
19522 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19523 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19524 token = cp_lexer_peek_token (parser->lexer);
19525 type = complete_type (cp_parser_type_id (parser));
19526 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19530 error_at (token->location,
19531 "%<@encode%> must specify a type as an argument");
19532 return error_mark_node;
19535 return objc_build_encode_expr (type);
19538 /* Parse an Objective-C @defs expression. */
19541 cp_parser_objc_defs_expression (cp_parser *parser)
19545 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19546 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19547 name = cp_parser_identifier (parser);
19548 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19550 return objc_get_class_ivars (name);
19553 /* Parse an Objective-C protocol expression.
19555 objc-protocol-expression:
19556 @protocol ( identifier )
19558 Returns a representation of the protocol expression. */
19561 cp_parser_objc_protocol_expression (cp_parser* parser)
19565 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19566 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19567 proto = cp_parser_identifier (parser);
19568 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19570 return objc_build_protocol_expr (proto);
19573 /* Parse an Objective-C selector expression.
19575 objc-selector-expression:
19576 @selector ( objc-method-signature )
19578 objc-method-signature:
19584 objc-selector-seq objc-selector :
19586 Returns a representation of the method selector. */
19589 cp_parser_objc_selector_expression (cp_parser* parser)
19591 tree sel_seq = NULL_TREE;
19592 bool maybe_unary_selector_p = true;
19594 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
19596 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19597 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19598 token = cp_lexer_peek_token (parser->lexer);
19600 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19601 || token->type == CPP_SCOPE)
19603 tree selector = NULL_TREE;
19605 if (token->type != CPP_COLON
19606 || token->type == CPP_SCOPE)
19607 selector = cp_parser_objc_selector (parser);
19609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19610 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19612 /* Detect if we have a unary selector. */
19613 if (maybe_unary_selector_p)
19615 sel_seq = selector;
19616 goto finish_selector;
19620 cp_parser_error (parser, "expected %<:%>");
19623 maybe_unary_selector_p = false;
19624 token = cp_lexer_consume_token (parser->lexer);
19626 if (token->type == CPP_SCOPE)
19629 = chainon (sel_seq,
19630 build_tree_list (selector, NULL_TREE));
19632 = chainon (sel_seq,
19633 build_tree_list (NULL_TREE, NULL_TREE));
19637 = chainon (sel_seq,
19638 build_tree_list (selector, NULL_TREE));
19640 token = cp_lexer_peek_token (parser->lexer);
19644 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19646 return objc_build_selector_expr (loc, sel_seq);
19649 /* Parse a list of identifiers.
19651 objc-identifier-list:
19653 objc-identifier-list , identifier
19655 Returns a TREE_LIST of identifier nodes. */
19658 cp_parser_objc_identifier_list (cp_parser* parser)
19660 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19661 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19663 while (sep->type == CPP_COMMA)
19665 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19666 list = chainon (list,
19667 build_tree_list (NULL_TREE,
19668 cp_parser_identifier (parser)));
19669 sep = cp_lexer_peek_token (parser->lexer);
19675 /* Parse an Objective-C alias declaration.
19677 objc-alias-declaration:
19678 @compatibility_alias identifier identifier ;
19680 This function registers the alias mapping with the Objective-C front end.
19681 It returns nothing. */
19684 cp_parser_objc_alias_declaration (cp_parser* parser)
19688 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19689 alias = cp_parser_identifier (parser);
19690 orig = cp_parser_identifier (parser);
19691 objc_declare_alias (alias, orig);
19692 cp_parser_consume_semicolon_at_end_of_statement (parser);
19695 /* Parse an Objective-C class forward-declaration.
19697 objc-class-declaration:
19698 @class objc-identifier-list ;
19700 The function registers the forward declarations with the Objective-C
19701 front end. It returns nothing. */
19704 cp_parser_objc_class_declaration (cp_parser* parser)
19706 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19707 objc_declare_class (cp_parser_objc_identifier_list (parser));
19708 cp_parser_consume_semicolon_at_end_of_statement (parser);
19711 /* Parse a list of Objective-C protocol references.
19713 objc-protocol-refs-opt:
19714 objc-protocol-refs [opt]
19716 objc-protocol-refs:
19717 < objc-identifier-list >
19719 Returns a TREE_LIST of identifiers, if any. */
19722 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19724 tree protorefs = NULL_TREE;
19726 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19728 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19729 protorefs = cp_parser_objc_identifier_list (parser);
19730 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19736 /* Parse a Objective-C visibility specification. */
19739 cp_parser_objc_visibility_spec (cp_parser* parser)
19741 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19743 switch (vis->keyword)
19745 case RID_AT_PRIVATE:
19746 objc_set_visibility (2);
19748 case RID_AT_PROTECTED:
19749 objc_set_visibility (0);
19751 case RID_AT_PUBLIC:
19752 objc_set_visibility (1);
19758 /* Eat '@private'/'@protected'/'@public'. */
19759 cp_lexer_consume_token (parser->lexer);
19762 /* Parse an Objective-C method type. */
19765 cp_parser_objc_method_type (cp_parser* parser)
19767 objc_set_method_type
19768 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19773 /* Parse an Objective-C protocol qualifier. */
19776 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19778 tree quals = NULL_TREE, node;
19779 cp_token *token = cp_lexer_peek_token (parser->lexer);
19781 node = token->u.value;
19783 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19784 && (node == ridpointers [(int) RID_IN]
19785 || node == ridpointers [(int) RID_OUT]
19786 || node == ridpointers [(int) RID_INOUT]
19787 || node == ridpointers [(int) RID_BYCOPY]
19788 || node == ridpointers [(int) RID_BYREF]
19789 || node == ridpointers [(int) RID_ONEWAY]))
19791 quals = tree_cons (NULL_TREE, node, quals);
19792 cp_lexer_consume_token (parser->lexer);
19793 token = cp_lexer_peek_token (parser->lexer);
19794 node = token->u.value;
19800 /* Parse an Objective-C typename. */
19803 cp_parser_objc_typename (cp_parser* parser)
19805 tree type_name = NULL_TREE;
19807 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19809 tree proto_quals, cp_type = NULL_TREE;
19811 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19812 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19814 /* An ObjC type name may consist of just protocol qualifiers, in which
19815 case the type shall default to 'id'. */
19816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19817 cp_type = cp_parser_type_id (parser);
19819 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19820 type_name = build_tree_list (proto_quals, cp_type);
19826 /* Check to see if TYPE refers to an Objective-C selector name. */
19829 cp_parser_objc_selector_p (enum cpp_ttype type)
19831 return (type == CPP_NAME || type == CPP_KEYWORD
19832 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19833 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19834 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19835 || type == CPP_XOR || type == CPP_XOR_EQ);
19838 /* Parse an Objective-C selector. */
19841 cp_parser_objc_selector (cp_parser* parser)
19843 cp_token *token = cp_lexer_consume_token (parser->lexer);
19845 if (!cp_parser_objc_selector_p (token->type))
19847 error_at (token->location, "invalid Objective-C++ selector name");
19848 return error_mark_node;
19851 /* C++ operator names are allowed to appear in ObjC selectors. */
19852 switch (token->type)
19854 case CPP_AND_AND: return get_identifier ("and");
19855 case CPP_AND_EQ: return get_identifier ("and_eq");
19856 case CPP_AND: return get_identifier ("bitand");
19857 case CPP_OR: return get_identifier ("bitor");
19858 case CPP_COMPL: return get_identifier ("compl");
19859 case CPP_NOT: return get_identifier ("not");
19860 case CPP_NOT_EQ: return get_identifier ("not_eq");
19861 case CPP_OR_OR: return get_identifier ("or");
19862 case CPP_OR_EQ: return get_identifier ("or_eq");
19863 case CPP_XOR: return get_identifier ("xor");
19864 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19865 default: return token->u.value;
19869 /* Parse an Objective-C params list. */
19872 cp_parser_objc_method_keyword_params (cp_parser* parser)
19874 tree params = NULL_TREE;
19875 bool maybe_unary_selector_p = true;
19876 cp_token *token = cp_lexer_peek_token (parser->lexer);
19878 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19880 tree selector = NULL_TREE, type_name, identifier;
19882 if (token->type != CPP_COLON)
19883 selector = cp_parser_objc_selector (parser);
19885 /* Detect if we have a unary selector. */
19886 if (maybe_unary_selector_p
19887 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19890 maybe_unary_selector_p = false;
19891 cp_parser_require (parser, CPP_COLON, "%<:%>");
19892 type_name = cp_parser_objc_typename (parser);
19893 identifier = cp_parser_identifier (parser);
19897 objc_build_keyword_decl (selector,
19901 token = cp_lexer_peek_token (parser->lexer);
19907 /* Parse the non-keyword Objective-C params. */
19910 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19912 tree params = make_node (TREE_LIST);
19913 cp_token *token = cp_lexer_peek_token (parser->lexer);
19914 *ellipsisp = false; /* Initially, assume no ellipsis. */
19916 while (token->type == CPP_COMMA)
19918 cp_parameter_declarator *parmdecl;
19921 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19922 token = cp_lexer_peek_token (parser->lexer);
19924 if (token->type == CPP_ELLIPSIS)
19926 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19931 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19932 parm = grokdeclarator (parmdecl->declarator,
19933 &parmdecl->decl_specifiers,
19934 PARM, /*initialized=*/0,
19935 /*attrlist=*/NULL);
19937 chainon (params, build_tree_list (NULL_TREE, parm));
19938 token = cp_lexer_peek_token (parser->lexer);
19944 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19947 cp_parser_objc_interstitial_code (cp_parser* parser)
19949 cp_token *token = cp_lexer_peek_token (parser->lexer);
19951 /* If the next token is `extern' and the following token is a string
19952 literal, then we have a linkage specification. */
19953 if (token->keyword == RID_EXTERN
19954 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19955 cp_parser_linkage_specification (parser);
19956 /* Handle #pragma, if any. */
19957 else if (token->type == CPP_PRAGMA)
19958 cp_parser_pragma (parser, pragma_external);
19959 /* Allow stray semicolons. */
19960 else if (token->type == CPP_SEMICOLON)
19961 cp_lexer_consume_token (parser->lexer);
19962 /* Finally, try to parse a block-declaration, or a function-definition. */
19964 cp_parser_block_declaration (parser, /*statement_p=*/false);
19967 /* Parse a method signature. */
19970 cp_parser_objc_method_signature (cp_parser* parser)
19972 tree rettype, kwdparms, optparms;
19973 bool ellipsis = false;
19975 cp_parser_objc_method_type (parser);
19976 rettype = cp_parser_objc_typename (parser);
19977 kwdparms = cp_parser_objc_method_keyword_params (parser);
19978 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19980 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19983 /* Pars an Objective-C method prototype list. */
19986 cp_parser_objc_method_prototype_list (cp_parser* parser)
19988 cp_token *token = cp_lexer_peek_token (parser->lexer);
19990 while (token->keyword != RID_AT_END)
19992 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19994 objc_add_method_declaration
19995 (cp_parser_objc_method_signature (parser));
19996 cp_parser_consume_semicolon_at_end_of_statement (parser);
19999 /* Allow for interspersed non-ObjC++ code. */
20000 cp_parser_objc_interstitial_code (parser);
20002 token = cp_lexer_peek_token (parser->lexer);
20005 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20006 objc_finish_interface ();
20009 /* Parse an Objective-C method definition list. */
20012 cp_parser_objc_method_definition_list (cp_parser* parser)
20014 cp_token *token = cp_lexer_peek_token (parser->lexer);
20016 while (token->keyword != RID_AT_END)
20020 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20022 push_deferring_access_checks (dk_deferred);
20023 objc_start_method_definition
20024 (cp_parser_objc_method_signature (parser));
20026 /* For historical reasons, we accept an optional semicolon. */
20027 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20028 cp_lexer_consume_token (parser->lexer);
20030 perform_deferred_access_checks ();
20031 stop_deferring_access_checks ();
20032 meth = cp_parser_function_definition_after_declarator (parser,
20034 pop_deferring_access_checks ();
20035 objc_finish_method_definition (meth);
20038 /* Allow for interspersed non-ObjC++ code. */
20039 cp_parser_objc_interstitial_code (parser);
20041 token = cp_lexer_peek_token (parser->lexer);
20044 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20045 objc_finish_implementation ();
20048 /* Parse Objective-C ivars. */
20051 cp_parser_objc_class_ivars (cp_parser* parser)
20053 cp_token *token = cp_lexer_peek_token (parser->lexer);
20055 if (token->type != CPP_OPEN_BRACE)
20056 return; /* No ivars specified. */
20058 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20059 token = cp_lexer_peek_token (parser->lexer);
20061 while (token->type != CPP_CLOSE_BRACE)
20063 cp_decl_specifier_seq declspecs;
20064 int decl_class_or_enum_p;
20065 tree prefix_attributes;
20067 cp_parser_objc_visibility_spec (parser);
20069 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20072 cp_parser_decl_specifier_seq (parser,
20073 CP_PARSER_FLAGS_OPTIONAL,
20075 &decl_class_or_enum_p);
20076 prefix_attributes = declspecs.attributes;
20077 declspecs.attributes = NULL_TREE;
20079 /* Keep going until we hit the `;' at the end of the
20081 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20083 tree width = NULL_TREE, attributes, first_attribute, decl;
20084 cp_declarator *declarator = NULL;
20085 int ctor_dtor_or_conv_p;
20087 /* Check for a (possibly unnamed) bitfield declaration. */
20088 token = cp_lexer_peek_token (parser->lexer);
20089 if (token->type == CPP_COLON)
20092 if (token->type == CPP_NAME
20093 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20096 /* Get the name of the bitfield. */
20097 declarator = make_id_declarator (NULL_TREE,
20098 cp_parser_identifier (parser),
20102 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20103 /* Get the width of the bitfield. */
20105 = cp_parser_constant_expression (parser,
20106 /*allow_non_constant=*/false,
20111 /* Parse the declarator. */
20113 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20114 &ctor_dtor_or_conv_p,
20115 /*parenthesized_p=*/NULL,
20116 /*member_p=*/false);
20119 /* Look for attributes that apply to the ivar. */
20120 attributes = cp_parser_attributes_opt (parser);
20121 /* Remember which attributes are prefix attributes and
20123 first_attribute = attributes;
20124 /* Combine the attributes. */
20125 attributes = chainon (prefix_attributes, attributes);
20128 /* Create the bitfield declaration. */
20129 decl = grokbitfield (declarator, &declspecs,
20133 decl = grokfield (declarator, &declspecs,
20134 NULL_TREE, /*init_const_expr_p=*/false,
20135 NULL_TREE, attributes);
20137 /* Add the instance variable. */
20138 objc_add_instance_variable (decl);
20140 /* Reset PREFIX_ATTRIBUTES. */
20141 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20142 attributes = TREE_CHAIN (attributes);
20144 TREE_CHAIN (attributes) = NULL_TREE;
20146 token = cp_lexer_peek_token (parser->lexer);
20148 if (token->type == CPP_COMMA)
20150 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20156 cp_parser_consume_semicolon_at_end_of_statement (parser);
20157 token = cp_lexer_peek_token (parser->lexer);
20160 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20161 /* For historical reasons, we accept an optional semicolon. */
20162 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20163 cp_lexer_consume_token (parser->lexer);
20166 /* Parse an Objective-C protocol declaration. */
20169 cp_parser_objc_protocol_declaration (cp_parser* parser)
20171 tree proto, protorefs;
20174 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20177 tok = cp_lexer_peek_token (parser->lexer);
20178 error_at (tok->location, "identifier expected after %<@protocol%>");
20182 /* See if we have a forward declaration or a definition. */
20183 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20185 /* Try a forward declaration first. */
20186 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20188 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20190 cp_parser_consume_semicolon_at_end_of_statement (parser);
20193 /* Ok, we got a full-fledged definition (or at least should). */
20196 proto = cp_parser_identifier (parser);
20197 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20198 objc_start_protocol (proto, protorefs);
20199 cp_parser_objc_method_prototype_list (parser);
20203 /* Parse an Objective-C superclass or category. */
20206 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20209 cp_token *next = cp_lexer_peek_token (parser->lexer);
20211 *super = *categ = NULL_TREE;
20212 if (next->type == CPP_COLON)
20214 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20215 *super = cp_parser_identifier (parser);
20217 else if (next->type == CPP_OPEN_PAREN)
20219 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20220 *categ = cp_parser_identifier (parser);
20221 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20225 /* Parse an Objective-C class interface. */
20228 cp_parser_objc_class_interface (cp_parser* parser)
20230 tree name, super, categ, protos;
20232 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20233 name = cp_parser_identifier (parser);
20234 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20235 protos = cp_parser_objc_protocol_refs_opt (parser);
20237 /* We have either a class or a category on our hands. */
20239 objc_start_category_interface (name, categ, protos);
20242 objc_start_class_interface (name, super, protos);
20243 /* Handle instance variable declarations, if any. */
20244 cp_parser_objc_class_ivars (parser);
20245 objc_continue_interface ();
20248 cp_parser_objc_method_prototype_list (parser);
20251 /* Parse an Objective-C class implementation. */
20254 cp_parser_objc_class_implementation (cp_parser* parser)
20256 tree name, super, categ;
20258 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20259 name = cp_parser_identifier (parser);
20260 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20262 /* We have either a class or a category on our hands. */
20264 objc_start_category_implementation (name, categ);
20267 objc_start_class_implementation (name, super);
20268 /* Handle instance variable declarations, if any. */
20269 cp_parser_objc_class_ivars (parser);
20270 objc_continue_implementation ();
20273 cp_parser_objc_method_definition_list (parser);
20276 /* Consume the @end token and finish off the implementation. */
20279 cp_parser_objc_end_implementation (cp_parser* parser)
20281 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20282 objc_finish_implementation ();
20285 /* Parse an Objective-C declaration. */
20288 cp_parser_objc_declaration (cp_parser* parser)
20290 /* Try to figure out what kind of declaration is present. */
20291 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20293 switch (kwd->keyword)
20296 cp_parser_objc_alias_declaration (parser);
20299 cp_parser_objc_class_declaration (parser);
20301 case RID_AT_PROTOCOL:
20302 cp_parser_objc_protocol_declaration (parser);
20304 case RID_AT_INTERFACE:
20305 cp_parser_objc_class_interface (parser);
20307 case RID_AT_IMPLEMENTATION:
20308 cp_parser_objc_class_implementation (parser);
20311 cp_parser_objc_end_implementation (parser);
20314 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20316 cp_parser_skip_to_end_of_block_or_statement (parser);
20320 /* Parse an Objective-C try-catch-finally statement.
20322 objc-try-catch-finally-stmt:
20323 @try compound-statement objc-catch-clause-seq [opt]
20324 objc-finally-clause [opt]
20326 objc-catch-clause-seq:
20327 objc-catch-clause objc-catch-clause-seq [opt]
20330 @catch ( exception-declaration ) compound-statement
20332 objc-finally-clause
20333 @finally compound-statement
20335 Returns NULL_TREE. */
20338 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20339 location_t location;
20342 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20343 location = cp_lexer_peek_token (parser->lexer)->location;
20344 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20345 node, lest it get absorbed into the surrounding block. */
20346 stmt = push_stmt_list ();
20347 cp_parser_compound_statement (parser, NULL, false);
20348 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20350 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20352 cp_parameter_declarator *parmdecl;
20355 cp_lexer_consume_token (parser->lexer);
20356 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20357 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20358 parm = grokdeclarator (parmdecl->declarator,
20359 &parmdecl->decl_specifiers,
20360 PARM, /*initialized=*/0,
20361 /*attrlist=*/NULL);
20362 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20363 objc_begin_catch_clause (parm);
20364 cp_parser_compound_statement (parser, NULL, false);
20365 objc_finish_catch_clause ();
20368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20370 cp_lexer_consume_token (parser->lexer);
20371 location = cp_lexer_peek_token (parser->lexer)->location;
20372 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20373 node, lest it get absorbed into the surrounding block. */
20374 stmt = push_stmt_list ();
20375 cp_parser_compound_statement (parser, NULL, false);
20376 objc_build_finally_clause (location, pop_stmt_list (stmt));
20379 return objc_finish_try_stmt ();
20382 /* Parse an Objective-C synchronized statement.
20384 objc-synchronized-stmt:
20385 @synchronized ( expression ) compound-statement
20387 Returns NULL_TREE. */
20390 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20391 location_t location;
20394 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20396 location = cp_lexer_peek_token (parser->lexer)->location;
20397 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20398 lock = cp_parser_expression (parser, false, NULL);
20399 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20401 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20402 node, lest it get absorbed into the surrounding block. */
20403 stmt = push_stmt_list ();
20404 cp_parser_compound_statement (parser, NULL, false);
20406 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20409 /* Parse an Objective-C throw statement.
20412 @throw assignment-expression [opt] ;
20414 Returns a constructed '@throw' statement. */
20417 cp_parser_objc_throw_statement (cp_parser *parser) {
20418 tree expr = NULL_TREE;
20419 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20421 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20424 expr = cp_parser_assignment_expression (parser, false, NULL);
20426 cp_parser_consume_semicolon_at_end_of_statement (parser);
20428 return objc_build_throw_stmt (loc, expr);
20431 /* Parse an Objective-C statement. */
20434 cp_parser_objc_statement (cp_parser * parser) {
20435 /* Try to figure out what kind of declaration is present. */
20436 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20438 switch (kwd->keyword)
20441 return cp_parser_objc_try_catch_finally_statement (parser);
20442 case RID_AT_SYNCHRONIZED:
20443 return cp_parser_objc_synchronized_statement (parser);
20445 return cp_parser_objc_throw_statement (parser);
20447 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20449 cp_parser_skip_to_end_of_block_or_statement (parser);
20452 return error_mark_node;
20455 /* OpenMP 2.5 parsing routines. */
20457 /* Returns name of the next clause.
20458 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20459 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20460 returned and the token is consumed. */
20462 static pragma_omp_clause
20463 cp_parser_omp_clause_name (cp_parser *parser)
20465 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20467 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20468 result = PRAGMA_OMP_CLAUSE_IF;
20469 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20470 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20471 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20472 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20473 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20475 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20476 const char *p = IDENTIFIER_POINTER (id);
20481 if (!strcmp ("collapse", p))
20482 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20483 else if (!strcmp ("copyin", p))
20484 result = PRAGMA_OMP_CLAUSE_COPYIN;
20485 else if (!strcmp ("copyprivate", p))
20486 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20489 if (!strcmp ("firstprivate", p))
20490 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20493 if (!strcmp ("lastprivate", p))
20494 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20497 if (!strcmp ("nowait", p))
20498 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20499 else if (!strcmp ("num_threads", p))
20500 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20503 if (!strcmp ("ordered", p))
20504 result = PRAGMA_OMP_CLAUSE_ORDERED;
20507 if (!strcmp ("reduction", p))
20508 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20511 if (!strcmp ("schedule", p))
20512 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20513 else if (!strcmp ("shared", p))
20514 result = PRAGMA_OMP_CLAUSE_SHARED;
20517 if (!strcmp ("untied", p))
20518 result = PRAGMA_OMP_CLAUSE_UNTIED;
20523 if (result != PRAGMA_OMP_CLAUSE_NONE)
20524 cp_lexer_consume_token (parser->lexer);
20529 /* Validate that a clause of the given type does not already exist. */
20532 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20533 const char *name, location_t location)
20537 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20538 if (OMP_CLAUSE_CODE (c) == code)
20540 error_at (location, "too many %qs clauses", name);
20548 variable-list , identifier
20550 In addition, we match a closing parenthesis. An opening parenthesis
20551 will have been consumed by the caller.
20553 If KIND is nonzero, create the appropriate node and install the decl
20554 in OMP_CLAUSE_DECL and add the node to the head of the list.
20556 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20557 return the list created. */
20560 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20568 token = cp_lexer_peek_token (parser->lexer);
20569 name = cp_parser_id_expression (parser, /*template_p=*/false,
20570 /*check_dependency_p=*/true,
20571 /*template_p=*/NULL,
20572 /*declarator_p=*/false,
20573 /*optional_p=*/false);
20574 if (name == error_mark_node)
20577 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20578 if (decl == error_mark_node)
20579 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20580 else if (kind != 0)
20582 tree u = build_omp_clause (token->location, kind);
20583 OMP_CLAUSE_DECL (u) = decl;
20584 OMP_CLAUSE_CHAIN (u) = list;
20588 list = tree_cons (decl, NULL_TREE, list);
20591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20593 cp_lexer_consume_token (parser->lexer);
20596 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20600 /* Try to resync to an unnested comma. Copied from
20601 cp_parser_parenthesized_expression_list. */
20603 ending = cp_parser_skip_to_closing_parenthesis (parser,
20604 /*recovering=*/true,
20606 /*consume_paren=*/true);
20614 /* Similarly, but expect leading and trailing parenthesis. This is a very
20615 common case for omp clauses. */
20618 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20620 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20621 return cp_parser_omp_var_list_no_open (parser, kind, list);
20626 collapse ( constant-expression ) */
20629 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20635 loc = cp_lexer_peek_token (parser->lexer)->location;
20636 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20639 num = cp_parser_constant_expression (parser, false, NULL);
20641 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20642 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20643 /*or_comma=*/false,
20644 /*consume_paren=*/true);
20646 if (num == error_mark_node)
20648 num = fold_non_dependent_expr (num);
20649 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20650 || !host_integerp (num, 0)
20651 || (n = tree_low_cst (num, 0)) <= 0
20654 error_at (loc, "collapse argument needs positive constant integer expression");
20658 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20659 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
20660 OMP_CLAUSE_CHAIN (c) = list;
20661 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20667 default ( shared | none ) */
20670 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20672 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20675 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20679 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20680 const char *p = IDENTIFIER_POINTER (id);
20685 if (strcmp ("none", p) != 0)
20687 kind = OMP_CLAUSE_DEFAULT_NONE;
20691 if (strcmp ("shared", p) != 0)
20693 kind = OMP_CLAUSE_DEFAULT_SHARED;
20700 cp_lexer_consume_token (parser->lexer);
20705 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20708 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20709 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20710 /*or_comma=*/false,
20711 /*consume_paren=*/true);
20713 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20716 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20717 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
20718 OMP_CLAUSE_CHAIN (c) = list;
20719 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20725 if ( expression ) */
20728 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20732 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20735 t = cp_parser_condition (parser);
20737 if (t == error_mark_node
20738 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20739 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20740 /*or_comma=*/false,
20741 /*consume_paren=*/true);
20743 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20745 c = build_omp_clause (location, OMP_CLAUSE_IF);
20746 OMP_CLAUSE_IF_EXPR (c) = t;
20747 OMP_CLAUSE_CHAIN (c) = list;
20756 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20757 tree list, location_t location)
20761 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20763 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
20764 OMP_CLAUSE_CHAIN (c) = list;
20769 num_threads ( expression ) */
20772 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20773 location_t location)
20777 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20780 t = cp_parser_expression (parser, false, NULL);
20782 if (t == error_mark_node
20783 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20784 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20785 /*or_comma=*/false,
20786 /*consume_paren=*/true);
20788 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20789 "num_threads", location);
20791 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
20792 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20793 OMP_CLAUSE_CHAIN (c) = list;
20802 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20803 tree list, location_t location)
20807 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20808 "ordered", location);
20810 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
20811 OMP_CLAUSE_CHAIN (c) = list;
20816 reduction ( reduction-operator : variable-list )
20818 reduction-operator:
20819 One of: + * - & ^ | && || */
20822 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20824 enum tree_code code;
20827 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20830 switch (cp_lexer_peek_token (parser->lexer)->type)
20842 code = BIT_AND_EXPR;
20845 code = BIT_XOR_EXPR;
20848 code = BIT_IOR_EXPR;
20851 code = TRUTH_ANDIF_EXPR;
20854 code = TRUTH_ORIF_EXPR;
20857 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20858 "%<|%>, %<&&%>, or %<||%>");
20860 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20861 /*or_comma=*/false,
20862 /*consume_paren=*/true);
20865 cp_lexer_consume_token (parser->lexer);
20867 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20870 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20871 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20872 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20878 schedule ( schedule-kind )
20879 schedule ( schedule-kind , expression )
20882 static | dynamic | guided | runtime | auto */
20885 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20889 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20892 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
20894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20896 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20897 const char *p = IDENTIFIER_POINTER (id);
20902 if (strcmp ("dynamic", p) != 0)
20904 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20908 if (strcmp ("guided", p) != 0)
20910 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20914 if (strcmp ("runtime", p) != 0)
20916 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20923 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20924 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20925 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20926 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20929 cp_lexer_consume_token (parser->lexer);
20931 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20934 cp_lexer_consume_token (parser->lexer);
20936 token = cp_lexer_peek_token (parser->lexer);
20937 t = cp_parser_assignment_expression (parser, false, NULL);
20939 if (t == error_mark_node)
20941 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20942 error_at (token->location, "schedule %<runtime%> does not take "
20943 "a %<chunk_size%> parameter");
20944 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20945 error_at (token->location, "schedule %<auto%> does not take "
20946 "a %<chunk_size%> parameter");
20948 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20950 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20953 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20956 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20957 OMP_CLAUSE_CHAIN (c) = list;
20961 cp_parser_error (parser, "invalid schedule kind");
20963 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20964 /*or_comma=*/false,
20965 /*consume_paren=*/true);
20973 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20974 tree list, location_t location)
20978 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20980 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
20981 OMP_CLAUSE_CHAIN (c) = list;
20985 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20986 is a bitmask in MASK. Return the list of clauses found; the result
20987 of clause default goes in *pdefault. */
20990 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20991 const char *where, cp_token *pragma_tok)
20993 tree clauses = NULL;
20995 cp_token *token = NULL;
20997 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20999 pragma_omp_clause c_kind;
21000 const char *c_name;
21001 tree prev = clauses;
21003 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21004 cp_lexer_consume_token (parser->lexer);
21006 token = cp_lexer_peek_token (parser->lexer);
21007 c_kind = cp_parser_omp_clause_name (parser);
21012 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21013 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21015 c_name = "collapse";
21017 case PRAGMA_OMP_CLAUSE_COPYIN:
21018 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21021 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21022 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21024 c_name = "copyprivate";
21026 case PRAGMA_OMP_CLAUSE_DEFAULT:
21027 clauses = cp_parser_omp_clause_default (parser, clauses,
21029 c_name = "default";
21031 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21032 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21034 c_name = "firstprivate";
21036 case PRAGMA_OMP_CLAUSE_IF:
21037 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21040 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21041 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21043 c_name = "lastprivate";
21045 case PRAGMA_OMP_CLAUSE_NOWAIT:
21046 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21049 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21050 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21052 c_name = "num_threads";
21054 case PRAGMA_OMP_CLAUSE_ORDERED:
21055 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21057 c_name = "ordered";
21059 case PRAGMA_OMP_CLAUSE_PRIVATE:
21060 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21062 c_name = "private";
21064 case PRAGMA_OMP_CLAUSE_REDUCTION:
21065 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21066 c_name = "reduction";
21068 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21069 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21071 c_name = "schedule";
21073 case PRAGMA_OMP_CLAUSE_SHARED:
21074 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21078 case PRAGMA_OMP_CLAUSE_UNTIED:
21079 clauses = cp_parser_omp_clause_untied (parser, clauses,
21084 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21088 if (((mask >> c_kind) & 1) == 0)
21090 /* Remove the invalid clause(s) from the list to avoid
21091 confusing the rest of the compiler. */
21093 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21097 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21098 return finish_omp_clauses (clauses);
21105 In practice, we're also interested in adding the statement to an
21106 outer node. So it is convenient if we work around the fact that
21107 cp_parser_statement calls add_stmt. */
21110 cp_parser_begin_omp_structured_block (cp_parser *parser)
21112 unsigned save = parser->in_statement;
21114 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21115 This preserves the "not within loop or switch" style error messages
21116 for nonsense cases like
21122 if (parser->in_statement)
21123 parser->in_statement = IN_OMP_BLOCK;
21129 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21131 parser->in_statement = save;
21135 cp_parser_omp_structured_block (cp_parser *parser)
21137 tree stmt = begin_omp_structured_block ();
21138 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21140 cp_parser_statement (parser, NULL_TREE, false, NULL);
21142 cp_parser_end_omp_structured_block (parser, save);
21143 return finish_omp_structured_block (stmt);
21147 # pragma omp atomic new-line
21151 x binop= expr | x++ | ++x | x-- | --x
21153 +, *, -, /, &, ^, |, <<, >>
21155 where x is an lvalue expression with scalar type. */
21158 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21161 enum tree_code code;
21163 cp_parser_require_pragma_eol (parser, pragma_tok);
21165 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21166 /*cast_p=*/false, NULL);
21167 switch (TREE_CODE (lhs))
21172 case PREINCREMENT_EXPR:
21173 case POSTINCREMENT_EXPR:
21174 lhs = TREE_OPERAND (lhs, 0);
21176 rhs = integer_one_node;
21179 case PREDECREMENT_EXPR:
21180 case POSTDECREMENT_EXPR:
21181 lhs = TREE_OPERAND (lhs, 0);
21183 rhs = integer_one_node;
21187 switch (cp_lexer_peek_token (parser->lexer)->type)
21193 code = TRUNC_DIV_EXPR;
21201 case CPP_LSHIFT_EQ:
21202 code = LSHIFT_EXPR;
21204 case CPP_RSHIFT_EQ:
21205 code = RSHIFT_EXPR;
21208 code = BIT_AND_EXPR;
21211 code = BIT_IOR_EXPR;
21214 code = BIT_XOR_EXPR;
21217 cp_parser_error (parser,
21218 "invalid operator for %<#pragma omp atomic%>");
21221 cp_lexer_consume_token (parser->lexer);
21223 rhs = cp_parser_expression (parser, false, NULL);
21224 if (rhs == error_mark_node)
21228 finish_omp_atomic (code, lhs, rhs);
21229 cp_parser_consume_semicolon_at_end_of_statement (parser);
21233 cp_parser_skip_to_end_of_block_or_statement (parser);
21238 # pragma omp barrier new-line */
21241 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21243 cp_parser_require_pragma_eol (parser, pragma_tok);
21244 finish_omp_barrier ();
21248 # pragma omp critical [(name)] new-line
21249 structured-block */
21252 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21254 tree stmt, name = NULL;
21256 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21258 cp_lexer_consume_token (parser->lexer);
21260 name = cp_parser_identifier (parser);
21262 if (name == error_mark_node
21263 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21264 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21265 /*or_comma=*/false,
21266 /*consume_paren=*/true);
21267 if (name == error_mark_node)
21270 cp_parser_require_pragma_eol (parser, pragma_tok);
21272 stmt = cp_parser_omp_structured_block (parser);
21273 return c_finish_omp_critical (input_location, stmt, name);
21277 # pragma omp flush flush-vars[opt] new-line
21280 ( variable-list ) */
21283 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21286 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21287 cp_parser_require_pragma_eol (parser, pragma_tok);
21289 finish_omp_flush ();
21292 /* Helper function, to parse omp for increment expression. */
21295 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21297 tree cond = cp_parser_binary_expression (parser, false, true,
21298 PREC_NOT_OPERATOR, NULL);
21301 if (cond == error_mark_node
21302 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21304 cp_parser_skip_to_end_of_statement (parser);
21305 return error_mark_node;
21308 switch (TREE_CODE (cond))
21316 return error_mark_node;
21319 /* If decl is an iterator, preserve LHS and RHS of the relational
21320 expr until finish_omp_for. */
21322 && (type_dependent_expression_p (decl)
21323 || CLASS_TYPE_P (TREE_TYPE (decl))))
21326 return build_x_binary_op (TREE_CODE (cond),
21327 TREE_OPERAND (cond, 0), ERROR_MARK,
21328 TREE_OPERAND (cond, 1), ERROR_MARK,
21329 &overloaded_p, tf_warning_or_error);
21332 /* Helper function, to parse omp for increment expression. */
21335 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21337 cp_token *token = cp_lexer_peek_token (parser->lexer);
21343 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21345 op = (token->type == CPP_PLUS_PLUS
21346 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21347 cp_lexer_consume_token (parser->lexer);
21348 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21350 return error_mark_node;
21351 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21354 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21356 return error_mark_node;
21358 token = cp_lexer_peek_token (parser->lexer);
21359 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21361 op = (token->type == CPP_PLUS_PLUS
21362 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21363 cp_lexer_consume_token (parser->lexer);
21364 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21367 op = cp_parser_assignment_operator_opt (parser);
21368 if (op == ERROR_MARK)
21369 return error_mark_node;
21371 if (op != NOP_EXPR)
21373 rhs = cp_parser_assignment_expression (parser, false, NULL);
21374 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21375 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21378 lhs = cp_parser_binary_expression (parser, false, false,
21379 PREC_ADDITIVE_EXPRESSION, NULL);
21380 token = cp_lexer_peek_token (parser->lexer);
21381 decl_first = lhs == decl;
21384 if (token->type != CPP_PLUS
21385 && token->type != CPP_MINUS)
21386 return error_mark_node;
21390 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21391 cp_lexer_consume_token (parser->lexer);
21392 rhs = cp_parser_binary_expression (parser, false, false,
21393 PREC_ADDITIVE_EXPRESSION, NULL);
21394 token = cp_lexer_peek_token (parser->lexer);
21395 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21397 if (lhs == NULL_TREE)
21399 if (op == PLUS_EXPR)
21402 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21405 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21406 NULL, tf_warning_or_error);
21409 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21413 if (rhs != decl || op == MINUS_EXPR)
21414 return error_mark_node;
21415 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21418 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21420 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21423 /* Parse the restricted form of the for statement allowed by OpenMP. */
21426 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21428 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21429 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21430 tree this_pre_body, cl;
21431 location_t loc_first;
21432 bool collapse_err = false;
21433 int i, collapse = 1, nbraces = 0;
21435 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21436 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21437 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21439 gcc_assert (collapse >= 1);
21441 declv = make_tree_vec (collapse);
21442 initv = make_tree_vec (collapse);
21443 condv = make_tree_vec (collapse);
21444 incrv = make_tree_vec (collapse);
21446 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21448 for (i = 0; i < collapse; i++)
21450 int bracecount = 0;
21451 bool add_private_clause = false;
21454 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21456 cp_parser_error (parser, "for statement expected");
21459 loc = cp_lexer_consume_token (parser->lexer)->location;
21461 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21464 init = decl = real_decl = NULL;
21465 this_pre_body = push_stmt_list ();
21466 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21468 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21472 integer-type var = lb
21473 random-access-iterator-type var = lb
21474 pointer-type var = lb
21476 cp_decl_specifier_seq type_specifiers;
21478 /* First, try to parse as an initialized declaration. See
21479 cp_parser_condition, from whence the bulk of this is copied. */
21481 cp_parser_parse_tentatively (parser);
21482 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21484 if (cp_parser_parse_definitely (parser))
21486 /* If parsing a type specifier seq succeeded, then this
21487 MUST be a initialized declaration. */
21488 tree asm_specification, attributes;
21489 cp_declarator *declarator;
21491 declarator = cp_parser_declarator (parser,
21492 CP_PARSER_DECLARATOR_NAMED,
21493 /*ctor_dtor_or_conv_p=*/NULL,
21494 /*parenthesized_p=*/NULL,
21495 /*member_p=*/false);
21496 attributes = cp_parser_attributes_opt (parser);
21497 asm_specification = cp_parser_asm_specification_opt (parser);
21499 if (declarator == cp_error_declarator)
21500 cp_parser_skip_to_end_of_statement (parser);
21504 tree pushed_scope, auto_node;
21506 decl = start_decl (declarator, &type_specifiers,
21507 SD_INITIALIZED, attributes,
21508 /*prefix_attributes=*/NULL_TREE,
21511 auto_node = type_uses_auto (TREE_TYPE (decl));
21512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21514 if (cp_lexer_next_token_is (parser->lexer,
21516 error ("parenthesized initialization is not allowed in "
21517 "OpenMP %<for%> loop");
21519 /* Trigger an error. */
21520 cp_parser_require (parser, CPP_EQ, "%<=%>");
21522 init = error_mark_node;
21523 cp_parser_skip_to_end_of_statement (parser);
21525 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21526 || type_dependent_expression_p (decl)
21529 bool is_direct_init, is_non_constant_init;
21531 init = cp_parser_initializer (parser,
21533 &is_non_constant_init);
21535 if (auto_node && describable_type (init))
21538 = do_auto_deduction (TREE_TYPE (decl), init,
21541 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21542 && !type_dependent_expression_p (decl))
21546 cp_finish_decl (decl, init, !is_non_constant_init,
21548 LOOKUP_ONLYCONVERTING);
21549 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21552 = tree_cons (NULL, this_pre_body, for_block);
21556 init = pop_stmt_list (this_pre_body);
21557 this_pre_body = NULL_TREE;
21562 cp_lexer_consume_token (parser->lexer);
21563 init = cp_parser_assignment_expression (parser, false, NULL);
21566 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21567 init = error_mark_node;
21569 cp_finish_decl (decl, NULL_TREE,
21570 /*init_const_expr_p=*/false,
21572 LOOKUP_ONLYCONVERTING);
21576 pop_scope (pushed_scope);
21582 /* If parsing a type specifier sequence failed, then
21583 this MUST be a simple expression. */
21584 cp_parser_parse_tentatively (parser);
21585 decl = cp_parser_primary_expression (parser, false, false,
21587 if (!cp_parser_error_occurred (parser)
21590 && CLASS_TYPE_P (TREE_TYPE (decl)))
21594 cp_parser_parse_definitely (parser);
21595 cp_parser_require (parser, CPP_EQ, "%<=%>");
21596 rhs = cp_parser_assignment_expression (parser, false, NULL);
21597 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21599 tf_warning_or_error));
21600 add_private_clause = true;
21605 cp_parser_abort_tentative_parse (parser);
21606 init = cp_parser_expression (parser, false, NULL);
21609 if (TREE_CODE (init) == MODIFY_EXPR
21610 || TREE_CODE (init) == MODOP_EXPR)
21611 real_decl = TREE_OPERAND (init, 0);
21616 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21619 this_pre_body = pop_stmt_list (this_pre_body);
21623 pre_body = push_stmt_list ();
21625 add_stmt (this_pre_body);
21626 pre_body = pop_stmt_list (pre_body);
21629 pre_body = this_pre_body;
21634 if (par_clauses != NULL && real_decl != NULL_TREE)
21637 for (c = par_clauses; *c ; )
21638 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21639 && OMP_CLAUSE_DECL (*c) == real_decl)
21641 error_at (loc, "iteration variable %qD"
21642 " should not be firstprivate", real_decl);
21643 *c = OMP_CLAUSE_CHAIN (*c);
21645 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21646 && OMP_CLAUSE_DECL (*c) == real_decl)
21648 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21649 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21650 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
21651 OMP_CLAUSE_DECL (l) = real_decl;
21652 OMP_CLAUSE_CHAIN (l) = clauses;
21653 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21655 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21656 CP_OMP_CLAUSE_INFO (*c) = NULL;
21657 add_private_clause = false;
21661 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21662 && OMP_CLAUSE_DECL (*c) == real_decl)
21663 add_private_clause = false;
21664 c = &OMP_CLAUSE_CHAIN (*c);
21668 if (add_private_clause)
21671 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21673 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21674 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21675 && OMP_CLAUSE_DECL (c) == decl)
21677 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21678 && OMP_CLAUSE_DECL (c) == decl)
21679 error_at (loc, "iteration variable %qD "
21680 "should not be firstprivate",
21682 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21683 && OMP_CLAUSE_DECL (c) == decl)
21684 error_at (loc, "iteration variable %qD should not be reduction",
21689 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
21690 OMP_CLAUSE_DECL (c) = decl;
21691 c = finish_omp_clauses (c);
21694 OMP_CLAUSE_CHAIN (c) = clauses;
21701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21702 cond = cp_parser_omp_for_cond (parser, decl);
21703 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21708 /* If decl is an iterator, preserve the operator on decl
21709 until finish_omp_for. */
21711 && (type_dependent_expression_p (decl)
21712 || CLASS_TYPE_P (TREE_TYPE (decl))))
21713 incr = cp_parser_omp_for_incr (parser, decl);
21715 incr = cp_parser_expression (parser, false, NULL);
21718 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21720 /*or_comma=*/false,
21721 /*consume_paren=*/true);
21723 TREE_VEC_ELT (declv, i) = decl;
21724 TREE_VEC_ELT (initv, i) = init;
21725 TREE_VEC_ELT (condv, i) = cond;
21726 TREE_VEC_ELT (incrv, i) = incr;
21728 if (i == collapse - 1)
21731 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21732 in between the collapsed for loops to be still considered perfectly
21733 nested. Hopefully the final version clarifies this.
21734 For now handle (multiple) {'s and empty statements. */
21735 cp_parser_parse_tentatively (parser);
21738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21740 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21742 cp_lexer_consume_token (parser->lexer);
21745 else if (bracecount
21746 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21747 cp_lexer_consume_token (parser->lexer);
21750 loc = cp_lexer_peek_token (parser->lexer)->location;
21751 error_at (loc, "not enough collapsed for loops");
21752 collapse_err = true;
21753 cp_parser_abort_tentative_parse (parser);
21762 cp_parser_parse_definitely (parser);
21763 nbraces += bracecount;
21767 /* Note that we saved the original contents of this flag when we entered
21768 the structured block, and so we don't need to re-save it here. */
21769 parser->in_statement = IN_OMP_FOR;
21771 /* Note that the grammar doesn't call for a structured block here,
21772 though the loop as a whole is a structured block. */
21773 body = push_stmt_list ();
21774 cp_parser_statement (parser, NULL_TREE, false, NULL);
21775 body = pop_stmt_list (body);
21777 if (declv == NULL_TREE)
21780 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21781 pre_body, clauses);
21785 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21787 cp_lexer_consume_token (parser->lexer);
21790 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21791 cp_lexer_consume_token (parser->lexer);
21796 error_at (cp_lexer_peek_token (parser->lexer)->location,
21797 "collapsed loops not perfectly nested");
21799 collapse_err = true;
21800 cp_parser_statement_seq_opt (parser, NULL);
21801 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21807 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21808 for_block = TREE_CHAIN (for_block);
21815 #pragma omp for for-clause[optseq] new-line
21818 #define OMP_FOR_CLAUSE_MASK \
21819 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21820 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21821 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21822 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21823 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21824 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21825 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21826 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21829 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21831 tree clauses, sb, ret;
21834 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21835 "#pragma omp for", pragma_tok);
21837 sb = begin_omp_structured_block ();
21838 save = cp_parser_begin_omp_structured_block (parser);
21840 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21842 cp_parser_end_omp_structured_block (parser, save);
21843 add_stmt (finish_omp_structured_block (sb));
21849 # pragma omp master new-line
21850 structured-block */
21853 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21855 cp_parser_require_pragma_eol (parser, pragma_tok);
21856 return c_finish_omp_master (input_location,
21857 cp_parser_omp_structured_block (parser));
21861 # pragma omp ordered new-line
21862 structured-block */
21865 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21867 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21868 cp_parser_require_pragma_eol (parser, pragma_tok);
21869 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
21875 { section-sequence }
21878 section-directive[opt] structured-block
21879 section-sequence section-directive structured-block */
21882 cp_parser_omp_sections_scope (cp_parser *parser)
21884 tree stmt, substmt;
21885 bool error_suppress = false;
21888 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21891 stmt = push_stmt_list ();
21893 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21897 substmt = begin_omp_structured_block ();
21898 save = cp_parser_begin_omp_structured_block (parser);
21902 cp_parser_statement (parser, NULL_TREE, false, NULL);
21904 tok = cp_lexer_peek_token (parser->lexer);
21905 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21907 if (tok->type == CPP_CLOSE_BRACE)
21909 if (tok->type == CPP_EOF)
21913 cp_parser_end_omp_structured_block (parser, save);
21914 substmt = finish_omp_structured_block (substmt);
21915 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21916 add_stmt (substmt);
21921 tok = cp_lexer_peek_token (parser->lexer);
21922 if (tok->type == CPP_CLOSE_BRACE)
21924 if (tok->type == CPP_EOF)
21927 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21929 cp_lexer_consume_token (parser->lexer);
21930 cp_parser_require_pragma_eol (parser, tok);
21931 error_suppress = false;
21933 else if (!error_suppress)
21935 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21936 error_suppress = true;
21939 substmt = cp_parser_omp_structured_block (parser);
21940 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21941 add_stmt (substmt);
21943 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21945 substmt = pop_stmt_list (stmt);
21947 stmt = make_node (OMP_SECTIONS);
21948 TREE_TYPE (stmt) = void_type_node;
21949 OMP_SECTIONS_BODY (stmt) = substmt;
21956 # pragma omp sections sections-clause[optseq] newline
21959 #define OMP_SECTIONS_CLAUSE_MASK \
21960 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21961 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21962 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21963 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21964 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21967 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21971 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21972 "#pragma omp sections", pragma_tok);
21974 ret = cp_parser_omp_sections_scope (parser);
21976 OMP_SECTIONS_CLAUSES (ret) = clauses;
21982 # pragma parallel parallel-clause new-line
21983 # pragma parallel for parallel-for-clause new-line
21984 # pragma parallel sections parallel-sections-clause new-line */
21986 #define OMP_PARALLEL_CLAUSE_MASK \
21987 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21988 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21989 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21990 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21991 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21992 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21993 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21994 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21997 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21999 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22000 const char *p_name = "#pragma omp parallel";
22001 tree stmt, clauses, par_clause, ws_clause, block;
22002 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22004 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22008 cp_lexer_consume_token (parser->lexer);
22009 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22010 p_name = "#pragma omp parallel for";
22011 mask |= OMP_FOR_CLAUSE_MASK;
22012 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22014 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22016 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22017 const char *p = IDENTIFIER_POINTER (id);
22018 if (strcmp (p, "sections") == 0)
22020 cp_lexer_consume_token (parser->lexer);
22021 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22022 p_name = "#pragma omp parallel sections";
22023 mask |= OMP_SECTIONS_CLAUSE_MASK;
22024 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22028 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22029 block = begin_omp_parallel ();
22030 save = cp_parser_begin_omp_structured_block (parser);
22034 case PRAGMA_OMP_PARALLEL:
22035 cp_parser_statement (parser, NULL_TREE, false, NULL);
22036 par_clause = clauses;
22039 case PRAGMA_OMP_PARALLEL_FOR:
22040 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22041 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22044 case PRAGMA_OMP_PARALLEL_SECTIONS:
22045 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22046 stmt = cp_parser_omp_sections_scope (parser);
22048 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22052 gcc_unreachable ();
22055 cp_parser_end_omp_structured_block (parser, save);
22056 stmt = finish_omp_parallel (par_clause, block);
22057 if (p_kind != PRAGMA_OMP_PARALLEL)
22058 OMP_PARALLEL_COMBINED (stmt) = 1;
22063 # pragma omp single single-clause[optseq] new-line
22064 structured-block */
22066 #define OMP_SINGLE_CLAUSE_MASK \
22067 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22068 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22069 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22070 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22073 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22075 tree stmt = make_node (OMP_SINGLE);
22076 TREE_TYPE (stmt) = void_type_node;
22078 OMP_SINGLE_CLAUSES (stmt)
22079 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22080 "#pragma omp single", pragma_tok);
22081 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22083 return add_stmt (stmt);
22087 # pragma omp task task-clause[optseq] new-line
22088 structured-block */
22090 #define OMP_TASK_CLAUSE_MASK \
22091 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22092 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22093 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22094 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22095 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22096 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22099 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22101 tree clauses, block;
22104 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22105 "#pragma omp task", pragma_tok);
22106 block = begin_omp_task ();
22107 save = cp_parser_begin_omp_structured_block (parser);
22108 cp_parser_statement (parser, NULL_TREE, false, NULL);
22109 cp_parser_end_omp_structured_block (parser, save);
22110 return finish_omp_task (clauses, block);
22114 # pragma omp taskwait new-line */
22117 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22119 cp_parser_require_pragma_eol (parser, pragma_tok);
22120 finish_omp_taskwait ();
22124 # pragma omp threadprivate (variable-list) */
22127 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22131 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22132 cp_parser_require_pragma_eol (parser, pragma_tok);
22134 finish_omp_threadprivate (vars);
22137 /* Main entry point to OpenMP statement pragmas. */
22140 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22144 switch (pragma_tok->pragma_kind)
22146 case PRAGMA_OMP_ATOMIC:
22147 cp_parser_omp_atomic (parser, pragma_tok);
22149 case PRAGMA_OMP_CRITICAL:
22150 stmt = cp_parser_omp_critical (parser, pragma_tok);
22152 case PRAGMA_OMP_FOR:
22153 stmt = cp_parser_omp_for (parser, pragma_tok);
22155 case PRAGMA_OMP_MASTER:
22156 stmt = cp_parser_omp_master (parser, pragma_tok);
22158 case PRAGMA_OMP_ORDERED:
22159 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22161 case PRAGMA_OMP_PARALLEL:
22162 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22164 case PRAGMA_OMP_SECTIONS:
22165 stmt = cp_parser_omp_sections (parser, pragma_tok);
22167 case PRAGMA_OMP_SINGLE:
22168 stmt = cp_parser_omp_single (parser, pragma_tok);
22170 case PRAGMA_OMP_TASK:
22171 stmt = cp_parser_omp_task (parser, pragma_tok);
22174 gcc_unreachable ();
22178 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22183 static GTY (()) cp_parser *the_parser;
22186 /* Special handling for the first token or line in the file. The first
22187 thing in the file might be #pragma GCC pch_preprocess, which loads a
22188 PCH file, which is a GC collection point. So we need to handle this
22189 first pragma without benefit of an existing lexer structure.
22191 Always returns one token to the caller in *FIRST_TOKEN. This is
22192 either the true first token of the file, or the first token after
22193 the initial pragma. */
22196 cp_parser_initial_pragma (cp_token *first_token)
22200 cp_lexer_get_preprocessor_token (NULL, first_token);
22201 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22204 cp_lexer_get_preprocessor_token (NULL, first_token);
22205 if (first_token->type == CPP_STRING)
22207 name = first_token->u.value;
22209 cp_lexer_get_preprocessor_token (NULL, first_token);
22210 if (first_token->type != CPP_PRAGMA_EOL)
22211 error_at (first_token->location,
22212 "junk at end of %<#pragma GCC pch_preprocess%>");
22215 error_at (first_token->location, "expected string literal");
22217 /* Skip to the end of the pragma. */
22218 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22219 cp_lexer_get_preprocessor_token (NULL, first_token);
22221 /* Now actually load the PCH file. */
22223 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22225 /* Read one more token to return to our caller. We have to do this
22226 after reading the PCH file in, since its pointers have to be
22228 cp_lexer_get_preprocessor_token (NULL, first_token);
22231 /* Normal parsing of a pragma token. Here we can (and must) use the
22235 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22237 cp_token *pragma_tok;
22240 pragma_tok = cp_lexer_consume_token (parser->lexer);
22241 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22242 parser->lexer->in_pragma = true;
22244 id = pragma_tok->pragma_kind;
22247 case PRAGMA_GCC_PCH_PREPROCESS:
22248 error_at (pragma_tok->location,
22249 "%<#pragma GCC pch_preprocess%> must be first");
22252 case PRAGMA_OMP_BARRIER:
22255 case pragma_compound:
22256 cp_parser_omp_barrier (parser, pragma_tok);
22259 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22260 "used in compound statements");
22267 case PRAGMA_OMP_FLUSH:
22270 case pragma_compound:
22271 cp_parser_omp_flush (parser, pragma_tok);
22274 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22275 "used in compound statements");
22282 case PRAGMA_OMP_TASKWAIT:
22285 case pragma_compound:
22286 cp_parser_omp_taskwait (parser, pragma_tok);
22289 error_at (pragma_tok->location,
22290 "%<#pragma omp taskwait%> may only be "
22291 "used in compound statements");
22298 case PRAGMA_OMP_THREADPRIVATE:
22299 cp_parser_omp_threadprivate (parser, pragma_tok);
22302 case PRAGMA_OMP_ATOMIC:
22303 case PRAGMA_OMP_CRITICAL:
22304 case PRAGMA_OMP_FOR:
22305 case PRAGMA_OMP_MASTER:
22306 case PRAGMA_OMP_ORDERED:
22307 case PRAGMA_OMP_PARALLEL:
22308 case PRAGMA_OMP_SECTIONS:
22309 case PRAGMA_OMP_SINGLE:
22310 case PRAGMA_OMP_TASK:
22311 if (context == pragma_external)
22313 cp_parser_omp_construct (parser, pragma_tok);
22316 case PRAGMA_OMP_SECTION:
22317 error_at (pragma_tok->location,
22318 "%<#pragma omp section%> may only be used in "
22319 "%<#pragma omp sections%> construct");
22323 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22324 c_invoke_pragma_handler (id);
22328 cp_parser_error (parser, "expected declaration specifiers");
22332 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22336 /* The interface the pragma parsers have to the lexer. */
22339 pragma_lex (tree *value)
22342 enum cpp_ttype ret;
22344 tok = cp_lexer_peek_token (the_parser->lexer);
22347 *value = tok->u.value;
22349 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22351 else if (ret == CPP_STRING)
22352 *value = cp_parser_string_literal (the_parser, false, false);
22355 cp_lexer_consume_token (the_parser->lexer);
22356 if (ret == CPP_KEYWORD)
22364 /* External interface. */
22366 /* Parse one entire translation unit. */
22369 c_parse_file (void)
22371 bool error_occurred;
22372 static bool already_called = false;
22374 if (already_called)
22376 sorry ("inter-module optimizations not implemented for C++");
22379 already_called = true;
22381 the_parser = cp_parser_new ();
22382 push_deferring_access_checks (flag_access_control
22383 ? dk_no_deferred : dk_no_check);
22384 error_occurred = cp_parser_translation_unit (the_parser);
22388 #include "gt-cp-parser.h"