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
1629 static tree cp_parser_lambda_expression
1631 static void cp_parser_lambda_introducer
1632 (cp_parser *, tree);
1633 static void cp_parser_lambda_declarator_opt
1634 (cp_parser *, tree);
1635 static void cp_parser_lambda_body
1636 (cp_parser *, tree);
1638 /* Statements [gram.stmt.stmt] */
1640 static void cp_parser_statement
1641 (cp_parser *, tree, bool, bool *);
1642 static void cp_parser_label_for_labeled_statement
1644 static tree cp_parser_expression_statement
1645 (cp_parser *, tree);
1646 static tree cp_parser_compound_statement
1647 (cp_parser *, tree, bool);
1648 static void cp_parser_statement_seq_opt
1649 (cp_parser *, tree);
1650 static tree cp_parser_selection_statement
1651 (cp_parser *, bool *);
1652 static tree cp_parser_condition
1654 static tree cp_parser_iteration_statement
1656 static void cp_parser_for_init_statement
1658 static tree cp_parser_jump_statement
1660 static void cp_parser_declaration_statement
1663 static tree cp_parser_implicitly_scoped_statement
1664 (cp_parser *, bool *);
1665 static void cp_parser_already_scoped_statement
1668 /* Declarations [gram.dcl.dcl] */
1670 static void cp_parser_declaration_seq_opt
1672 static void cp_parser_declaration
1674 static void cp_parser_block_declaration
1675 (cp_parser *, bool);
1676 static void cp_parser_simple_declaration
1677 (cp_parser *, bool);
1678 static void cp_parser_decl_specifier_seq
1679 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1680 static tree cp_parser_storage_class_specifier_opt
1682 static tree cp_parser_function_specifier_opt
1683 (cp_parser *, cp_decl_specifier_seq *);
1684 static tree cp_parser_type_specifier
1685 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1687 static tree cp_parser_simple_type_specifier
1688 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1689 static tree cp_parser_type_name
1691 static tree cp_parser_nonclass_name
1692 (cp_parser* parser);
1693 static tree cp_parser_elaborated_type_specifier
1694 (cp_parser *, bool, bool);
1695 static tree cp_parser_enum_specifier
1697 static void cp_parser_enumerator_list
1698 (cp_parser *, tree);
1699 static void cp_parser_enumerator_definition
1700 (cp_parser *, tree);
1701 static tree cp_parser_namespace_name
1703 static void cp_parser_namespace_definition
1705 static void cp_parser_namespace_body
1707 static tree cp_parser_qualified_namespace_specifier
1709 static void cp_parser_namespace_alias_definition
1711 static bool cp_parser_using_declaration
1712 (cp_parser *, bool);
1713 static void cp_parser_using_directive
1715 static void cp_parser_asm_definition
1717 static void cp_parser_linkage_specification
1719 static void cp_parser_static_assert
1720 (cp_parser *, bool);
1721 static tree cp_parser_decltype
1724 /* Declarators [gram.dcl.decl] */
1726 static tree cp_parser_init_declarator
1727 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1728 static cp_declarator *cp_parser_declarator
1729 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1730 static cp_declarator *cp_parser_direct_declarator
1731 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1732 static enum tree_code cp_parser_ptr_operator
1733 (cp_parser *, tree *, cp_cv_quals *);
1734 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1736 static tree cp_parser_late_return_type_opt
1738 static tree cp_parser_declarator_id
1739 (cp_parser *, bool);
1740 static tree cp_parser_type_id
1742 static tree cp_parser_template_type_arg
1744 static tree cp_parser_type_id_1
1745 (cp_parser *, bool);
1746 static void cp_parser_type_specifier_seq
1747 (cp_parser *, bool, cp_decl_specifier_seq *);
1748 static tree cp_parser_parameter_declaration_clause
1750 static tree cp_parser_parameter_declaration_list
1751 (cp_parser *, bool *);
1752 static cp_parameter_declarator *cp_parser_parameter_declaration
1753 (cp_parser *, bool, bool *);
1754 static tree cp_parser_default_argument
1755 (cp_parser *, bool);
1756 static void cp_parser_function_body
1758 static tree cp_parser_initializer
1759 (cp_parser *, bool *, bool *);
1760 static tree cp_parser_initializer_clause
1761 (cp_parser *, bool *);
1762 static tree cp_parser_braced_list
1763 (cp_parser*, bool*);
1764 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1765 (cp_parser *, bool *);
1767 static bool cp_parser_ctor_initializer_opt_and_function_body
1770 /* Classes [gram.class] */
1772 static tree cp_parser_class_name
1773 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1774 static tree cp_parser_class_specifier
1776 static tree cp_parser_class_head
1777 (cp_parser *, bool *, tree *, tree *);
1778 static enum tag_types cp_parser_class_key
1780 static void cp_parser_member_specification_opt
1782 static void cp_parser_member_declaration
1784 static tree cp_parser_pure_specifier
1786 static tree cp_parser_constant_initializer
1789 /* Derived classes [gram.class.derived] */
1791 static tree cp_parser_base_clause
1793 static tree cp_parser_base_specifier
1796 /* Special member functions [gram.special] */
1798 static tree cp_parser_conversion_function_id
1800 static tree cp_parser_conversion_type_id
1802 static cp_declarator *cp_parser_conversion_declarator_opt
1804 static bool cp_parser_ctor_initializer_opt
1806 static void cp_parser_mem_initializer_list
1808 static tree cp_parser_mem_initializer
1810 static tree cp_parser_mem_initializer_id
1813 /* Overloading [gram.over] */
1815 static tree cp_parser_operator_function_id
1817 static tree cp_parser_operator
1820 /* Templates [gram.temp] */
1822 static void cp_parser_template_declaration
1823 (cp_parser *, bool);
1824 static tree cp_parser_template_parameter_list
1826 static tree cp_parser_template_parameter
1827 (cp_parser *, bool *, bool *);
1828 static tree cp_parser_type_parameter
1829 (cp_parser *, bool *);
1830 static tree cp_parser_template_id
1831 (cp_parser *, bool, bool, bool);
1832 static tree cp_parser_template_name
1833 (cp_parser *, bool, bool, bool, bool *);
1834 static tree cp_parser_template_argument_list
1836 static tree cp_parser_template_argument
1838 static void cp_parser_explicit_instantiation
1840 static void cp_parser_explicit_specialization
1843 /* Exception handling [gram.exception] */
1845 static tree cp_parser_try_block
1847 static bool cp_parser_function_try_block
1849 static void cp_parser_handler_seq
1851 static void cp_parser_handler
1853 static tree cp_parser_exception_declaration
1855 static tree cp_parser_throw_expression
1857 static tree cp_parser_exception_specification_opt
1859 static tree cp_parser_type_id_list
1862 /* GNU Extensions */
1864 static tree cp_parser_asm_specification_opt
1866 static tree cp_parser_asm_operand_list
1868 static tree cp_parser_asm_clobber_list
1870 static tree cp_parser_asm_label_list
1872 static tree cp_parser_attributes_opt
1874 static tree cp_parser_attribute_list
1876 static bool cp_parser_extension_opt
1877 (cp_parser *, int *);
1878 static void cp_parser_label_declaration
1881 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1882 static bool cp_parser_pragma
1883 (cp_parser *, enum pragma_context);
1885 /* Objective-C++ Productions */
1887 static tree cp_parser_objc_message_receiver
1889 static tree cp_parser_objc_message_args
1891 static tree cp_parser_objc_message_expression
1893 static tree cp_parser_objc_encode_expression
1895 static tree cp_parser_objc_defs_expression
1897 static tree cp_parser_objc_protocol_expression
1899 static tree cp_parser_objc_selector_expression
1901 static tree cp_parser_objc_expression
1903 static bool cp_parser_objc_selector_p
1905 static tree cp_parser_objc_selector
1907 static tree cp_parser_objc_protocol_refs_opt
1909 static void cp_parser_objc_declaration
1911 static tree cp_parser_objc_statement
1914 /* Utility Routines */
1916 static tree cp_parser_lookup_name
1917 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1918 static tree cp_parser_lookup_name_simple
1919 (cp_parser *, tree, location_t);
1920 static tree cp_parser_maybe_treat_template_as_class
1922 static bool cp_parser_check_declarator_template_parameters
1923 (cp_parser *, cp_declarator *, location_t);
1924 static bool cp_parser_check_template_parameters
1925 (cp_parser *, unsigned, location_t, cp_declarator *);
1926 static tree cp_parser_simple_cast_expression
1928 static tree cp_parser_global_scope_opt
1929 (cp_parser *, bool);
1930 static bool cp_parser_constructor_declarator_p
1931 (cp_parser *, bool);
1932 static tree cp_parser_function_definition_from_specifiers_and_declarator
1933 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1934 static tree cp_parser_function_definition_after_declarator
1935 (cp_parser *, bool);
1936 static void cp_parser_template_declaration_after_export
1937 (cp_parser *, bool);
1938 static void cp_parser_perform_template_parameter_access_checks
1939 (VEC (deferred_access_check,gc)*);
1940 static tree cp_parser_single_declaration
1941 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1942 static tree cp_parser_functional_cast
1943 (cp_parser *, tree);
1944 static tree cp_parser_save_member_function_body
1945 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1946 static tree cp_parser_enclosed_template_argument_list
1948 static void cp_parser_save_default_args
1949 (cp_parser *, tree);
1950 static void cp_parser_late_parsing_for_member
1951 (cp_parser *, tree);
1952 static void cp_parser_late_parsing_default_args
1953 (cp_parser *, tree);
1954 static tree cp_parser_sizeof_operand
1955 (cp_parser *, enum rid);
1956 static tree cp_parser_trait_expr
1957 (cp_parser *, enum rid);
1958 static bool cp_parser_declares_only_class_p
1960 static void cp_parser_set_storage_class
1961 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1962 static void cp_parser_set_decl_spec_type
1963 (cp_decl_specifier_seq *, tree, location_t, bool);
1964 static bool cp_parser_friend_p
1965 (const cp_decl_specifier_seq *);
1966 static cp_token *cp_parser_require
1967 (cp_parser *, enum cpp_ttype, const char *);
1968 static cp_token *cp_parser_require_keyword
1969 (cp_parser *, enum rid, const char *);
1970 static bool cp_parser_token_starts_function_definition_p
1972 static bool cp_parser_next_token_starts_class_definition_p
1974 static bool cp_parser_next_token_ends_template_argument_p
1976 static bool cp_parser_nth_token_starts_template_argument_list_p
1977 (cp_parser *, size_t);
1978 static enum tag_types cp_parser_token_is_class_key
1980 static void cp_parser_check_class_key
1981 (enum tag_types, tree type);
1982 static void cp_parser_check_access_in_redeclaration
1983 (tree type, location_t location);
1984 static bool cp_parser_optional_template_keyword
1986 static void cp_parser_pre_parsed_nested_name_specifier
1988 static bool cp_parser_cache_group
1989 (cp_parser *, enum cpp_ttype, unsigned);
1990 static void cp_parser_parse_tentatively
1992 static void cp_parser_commit_to_tentative_parse
1994 static void cp_parser_abort_tentative_parse
1996 static bool cp_parser_parse_definitely
1998 static inline bool cp_parser_parsing_tentatively
2000 static bool cp_parser_uncommitted_to_tentative_parse_p
2002 static void cp_parser_error
2003 (cp_parser *, const char *);
2004 static void cp_parser_name_lookup_error
2005 (cp_parser *, tree, tree, const char *, location_t);
2006 static bool cp_parser_simulate_error
2008 static bool cp_parser_check_type_definition
2010 static void cp_parser_check_for_definition_in_return_type
2011 (cp_declarator *, tree, location_t type_location);
2012 static void cp_parser_check_for_invalid_template_id
2013 (cp_parser *, tree, location_t location);
2014 static bool cp_parser_non_integral_constant_expression
2015 (cp_parser *, const char *);
2016 static void cp_parser_diagnose_invalid_type_name
2017 (cp_parser *, tree, tree, location_t);
2018 static bool cp_parser_parse_and_diagnose_invalid_type_name
2020 static int cp_parser_skip_to_closing_parenthesis
2021 (cp_parser *, bool, bool, bool);
2022 static void cp_parser_skip_to_end_of_statement
2024 static void cp_parser_consume_semicolon_at_end_of_statement
2026 static void cp_parser_skip_to_end_of_block_or_statement
2028 static bool cp_parser_skip_to_closing_brace
2030 static void cp_parser_skip_to_end_of_template_parameter_list
2032 static void cp_parser_skip_to_pragma_eol
2033 (cp_parser*, cp_token *);
2034 static bool cp_parser_error_occurred
2036 static bool cp_parser_allow_gnu_extensions_p
2038 static bool cp_parser_is_string_literal
2040 static bool cp_parser_is_keyword
2041 (cp_token *, enum rid);
2042 static tree cp_parser_make_typename_type
2043 (cp_parser *, tree, tree, location_t location);
2044 static cp_declarator * cp_parser_make_indirect_declarator
2045 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2047 /* Returns nonzero if we are parsing tentatively. */
2050 cp_parser_parsing_tentatively (cp_parser* parser)
2052 return parser->context->next != NULL;
2055 /* Returns nonzero if TOKEN is a string literal. */
2058 cp_parser_is_string_literal (cp_token* token)
2060 return (token->type == CPP_STRING ||
2061 token->type == CPP_STRING16 ||
2062 token->type == CPP_STRING32 ||
2063 token->type == CPP_WSTRING);
2066 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2069 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2071 return token->keyword == keyword;
2074 /* If not parsing tentatively, issue a diagnostic of the form
2075 FILE:LINE: MESSAGE before TOKEN
2076 where TOKEN is the next token in the input stream. MESSAGE
2077 (specified by the caller) is usually of the form "expected
2081 cp_parser_error (cp_parser* parser, const char* message)
2083 if (!cp_parser_simulate_error (parser))
2085 cp_token *token = cp_lexer_peek_token (parser->lexer);
2086 /* This diagnostic makes more sense if it is tagged to the line
2087 of the token we just peeked at. */
2088 cp_lexer_set_source_position_from_token (token);
2090 if (token->type == CPP_PRAGMA)
2092 error_at (token->location,
2093 "%<#pragma%> is not allowed here");
2094 cp_parser_skip_to_pragma_eol (parser, token);
2098 c_parse_error (message,
2099 /* Because c_parser_error does not understand
2100 CPP_KEYWORD, keywords are treated like
2102 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2103 token->u.value, token->flags);
2107 /* Issue an error about name-lookup failing. NAME is the
2108 IDENTIFIER_NODE DECL is the result of
2109 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2110 the thing that we hoped to find. */
2113 cp_parser_name_lookup_error (cp_parser* parser,
2116 const char* desired,
2117 location_t location)
2119 /* If name lookup completely failed, tell the user that NAME was not
2121 if (decl == error_mark_node)
2123 if (parser->scope && parser->scope != global_namespace)
2124 error_at (location, "%<%E::%E%> has not been declared",
2125 parser->scope, name);
2126 else if (parser->scope == global_namespace)
2127 error_at (location, "%<::%E%> has not been declared", name);
2128 else if (parser->object_scope
2129 && !CLASS_TYPE_P (parser->object_scope))
2130 error_at (location, "request for member %qE in non-class type %qT",
2131 name, parser->object_scope);
2132 else if (parser->object_scope)
2133 error_at (location, "%<%T::%E%> has not been declared",
2134 parser->object_scope, name);
2136 error_at (location, "%qE has not been declared", name);
2138 else if (parser->scope && parser->scope != global_namespace)
2139 error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2140 else if (parser->scope == global_namespace)
2141 error_at (location, "%<::%E%> %s", name, desired);
2143 error_at (location, "%qE %s", name, desired);
2146 /* If we are parsing tentatively, remember that an error has occurred
2147 during this tentative parse. Returns true if the error was
2148 simulated; false if a message should be issued by the caller. */
2151 cp_parser_simulate_error (cp_parser* parser)
2153 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2155 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2161 /* Check for repeated decl-specifiers. */
2164 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2165 location_t location)
2169 for (ds = ds_first; ds != ds_last; ++ds)
2171 unsigned count = decl_specs->specs[ds];
2174 /* The "long" specifier is a special case because of "long long". */
2178 error_at (location, "%<long long long%> is too long for GCC");
2180 pedwarn_cxx98 (location, OPT_Wlong_long,
2181 "ISO C++ 1998 does not support %<long long%>");
2185 static const char *const decl_spec_names[] = {
2201 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2206 /* This function is called when a type is defined. If type
2207 definitions are forbidden at this point, an error message is
2211 cp_parser_check_type_definition (cp_parser* parser)
2213 /* If types are forbidden here, issue a message. */
2214 if (parser->type_definition_forbidden_message)
2216 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2217 in the message need to be interpreted. */
2218 error (parser->type_definition_forbidden_message);
2224 /* This function is called when the DECLARATOR is processed. The TYPE
2225 was a type defined in the decl-specifiers. If it is invalid to
2226 define a type in the decl-specifiers for DECLARATOR, an error is
2227 issued. TYPE_LOCATION is the location of TYPE and is used
2228 for error reporting. */
2231 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2232 tree type, location_t type_location)
2234 /* [dcl.fct] forbids type definitions in return types.
2235 Unfortunately, it's not easy to know whether or not we are
2236 processing a return type until after the fact. */
2238 && (declarator->kind == cdk_pointer
2239 || declarator->kind == cdk_reference
2240 || declarator->kind == cdk_ptrmem))
2241 declarator = declarator->declarator;
2243 && declarator->kind == cdk_function)
2245 error_at (type_location,
2246 "new types may not be defined in a return type");
2247 inform (type_location,
2248 "(perhaps a semicolon is missing after the definition of %qT)",
2253 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2254 "<" in any valid C++ program. If the next token is indeed "<",
2255 issue a message warning the user about what appears to be an
2256 invalid attempt to form a template-id. LOCATION is the location
2257 of the type-specifier (TYPE) */
2260 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2261 tree type, location_t location)
2263 cp_token_position start = 0;
2265 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2268 error_at (location, "%qT is not a template", type);
2269 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2270 error_at (location, "%qE is not a template", type);
2272 error_at (location, "invalid template-id");
2273 /* Remember the location of the invalid "<". */
2274 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2275 start = cp_lexer_token_position (parser->lexer, true);
2276 /* Consume the "<". */
2277 cp_lexer_consume_token (parser->lexer);
2278 /* Parse the template arguments. */
2279 cp_parser_enclosed_template_argument_list (parser);
2280 /* Permanently remove the invalid template arguments so that
2281 this error message is not issued again. */
2283 cp_lexer_purge_tokens_after (parser->lexer, start);
2287 /* If parsing an integral constant-expression, issue an error message
2288 about the fact that THING appeared and return true. Otherwise,
2289 return false. In either case, set
2290 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2293 cp_parser_non_integral_constant_expression (cp_parser *parser,
2296 parser->non_integral_constant_expression_p = true;
2297 if (parser->integral_constant_expression_p)
2299 if (!parser->allow_non_integral_constant_expression_p)
2301 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2302 in the message need to be interpreted. */
2303 char *message = concat (thing,
2304 " cannot appear in a constant-expression",
2314 /* Emit a diagnostic for an invalid type name. SCOPE is the
2315 qualifying scope (or NULL, if none) for ID. This function commits
2316 to the current active tentative parse, if any. (Otherwise, the
2317 problematic construct might be encountered again later, resulting
2318 in duplicate error messages.) LOCATION is the location of ID. */
2321 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2322 tree scope, tree id,
2323 location_t location)
2325 tree decl, old_scope;
2326 /* Try to lookup the identifier. */
2327 old_scope = parser->scope;
2328 parser->scope = scope;
2329 decl = cp_parser_lookup_name_simple (parser, id, location);
2330 parser->scope = old_scope;
2331 /* If the lookup found a template-name, it means that the user forgot
2332 to specify an argument list. Emit a useful error message. */
2333 if (TREE_CODE (decl) == TEMPLATE_DECL)
2335 "invalid use of template-name %qE without an argument list",
2337 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2338 error_at (location, "invalid use of destructor %qD as a type", id);
2339 else if (TREE_CODE (decl) == TYPE_DECL)
2340 /* Something like 'unsigned A a;' */
2341 error_at (location, "invalid combination of multiple type-specifiers");
2342 else if (!parser->scope)
2344 /* Issue an error message. */
2345 error_at (location, "%qE does not name a type", id);
2346 /* If we're in a template class, it's possible that the user was
2347 referring to a type from a base class. For example:
2349 template <typename T> struct A { typedef T X; };
2350 template <typename T> struct B : public A<T> { X x; };
2352 The user should have said "typename A<T>::X". */
2353 if (processing_template_decl && current_class_type
2354 && TYPE_BINFO (current_class_type))
2358 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2362 tree base_type = BINFO_TYPE (b);
2363 if (CLASS_TYPE_P (base_type)
2364 && dependent_type_p (base_type))
2367 /* Go from a particular instantiation of the
2368 template (which will have an empty TYPE_FIELDs),
2369 to the main version. */
2370 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2371 for (field = TYPE_FIELDS (base_type);
2373 field = TREE_CHAIN (field))
2374 if (TREE_CODE (field) == TYPE_DECL
2375 && DECL_NAME (field) == id)
2378 "(perhaps %<typename %T::%E%> was intended)",
2379 BINFO_TYPE (b), id);
2388 /* Here we diagnose qualified-ids where the scope is actually correct,
2389 but the identifier does not resolve to a valid type name. */
2390 else if (parser->scope != error_mark_node)
2392 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2393 error_at (location, "%qE in namespace %qE does not name a type",
2395 else if (TYPE_P (parser->scope))
2396 error_at (location, "%qE in class %qT does not name a type",
2401 cp_parser_commit_to_tentative_parse (parser);
2404 /* Check for a common situation where a type-name should be present,
2405 but is not, and issue a sensible error message. Returns true if an
2406 invalid type-name was detected.
2408 The situation handled by this function are variable declarations of the
2409 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2410 Usually, `ID' should name a type, but if we got here it means that it
2411 does not. We try to emit the best possible error message depending on
2412 how exactly the id-expression looks like. */
2415 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2418 cp_token *token = cp_lexer_peek_token (parser->lexer);
2420 cp_parser_parse_tentatively (parser);
2421 id = cp_parser_id_expression (parser,
2422 /*template_keyword_p=*/false,
2423 /*check_dependency_p=*/true,
2424 /*template_p=*/NULL,
2425 /*declarator_p=*/true,
2426 /*optional_p=*/false);
2427 /* After the id-expression, there should be a plain identifier,
2428 otherwise this is not a simple variable declaration. Also, if
2429 the scope is dependent, we cannot do much. */
2430 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2431 || (parser->scope && TYPE_P (parser->scope)
2432 && dependent_type_p (parser->scope))
2433 || TREE_CODE (id) == TYPE_DECL)
2435 cp_parser_abort_tentative_parse (parser);
2438 if (!cp_parser_parse_definitely (parser))
2441 /* Emit a diagnostic for the invalid type. */
2442 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2443 id, token->location);
2444 /* Skip to the end of the declaration; there's no point in
2445 trying to process it. */
2446 cp_parser_skip_to_end_of_block_or_statement (parser);
2450 /* Consume tokens up to, and including, the next non-nested closing `)'.
2451 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2452 are doing error recovery. Returns -1 if OR_COMMA is true and we
2453 found an unnested comma. */
2456 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2461 unsigned paren_depth = 0;
2462 unsigned brace_depth = 0;
2463 unsigned square_depth = 0;
2465 if (recovering && !or_comma
2466 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2471 cp_token * token = cp_lexer_peek_token (parser->lexer);
2473 switch (token->type)
2476 case CPP_PRAGMA_EOL:
2477 /* If we've run out of tokens, then there is no closing `)'. */
2480 /* This is good for lambda expression capture-lists. */
2481 case CPP_OPEN_SQUARE:
2484 case CPP_CLOSE_SQUARE:
2485 if (!square_depth--)
2490 /* This matches the processing in skip_to_end_of_statement. */
2495 case CPP_OPEN_BRACE:
2498 case CPP_CLOSE_BRACE:
2504 if (recovering && or_comma && !brace_depth && !paren_depth
2509 case CPP_OPEN_PAREN:
2514 case CPP_CLOSE_PAREN:
2515 if (!brace_depth && !paren_depth--)
2518 cp_lexer_consume_token (parser->lexer);
2527 /* Consume the token. */
2528 cp_lexer_consume_token (parser->lexer);
2532 /* Consume tokens until we reach the end of the current statement.
2533 Normally, that will be just before consuming a `;'. However, if a
2534 non-nested `}' comes first, then we stop before consuming that. */
2537 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2539 unsigned nesting_depth = 0;
2543 cp_token *token = cp_lexer_peek_token (parser->lexer);
2545 switch (token->type)
2548 case CPP_PRAGMA_EOL:
2549 /* If we've run out of tokens, stop. */
2553 /* If the next token is a `;', we have reached the end of the
2559 case CPP_CLOSE_BRACE:
2560 /* If this is a non-nested '}', stop before consuming it.
2561 That way, when confronted with something like:
2565 we stop before consuming the closing '}', even though we
2566 have not yet reached a `;'. */
2567 if (nesting_depth == 0)
2570 /* If it is the closing '}' for a block that we have
2571 scanned, stop -- but only after consuming the token.
2577 we will stop after the body of the erroneously declared
2578 function, but before consuming the following `typedef'
2580 if (--nesting_depth == 0)
2582 cp_lexer_consume_token (parser->lexer);
2586 case CPP_OPEN_BRACE:
2594 /* Consume the token. */
2595 cp_lexer_consume_token (parser->lexer);
2599 /* This function is called at the end of a statement or declaration.
2600 If the next token is a semicolon, it is consumed; otherwise, error
2601 recovery is attempted. */
2604 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2606 /* Look for the trailing `;'. */
2607 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2609 /* If there is additional (erroneous) input, skip to the end of
2611 cp_parser_skip_to_end_of_statement (parser);
2612 /* If the next token is now a `;', consume it. */
2613 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2614 cp_lexer_consume_token (parser->lexer);
2618 /* Skip tokens until we have consumed an entire block, or until we
2619 have consumed a non-nested `;'. */
2622 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2624 int nesting_depth = 0;
2626 while (nesting_depth >= 0)
2628 cp_token *token = cp_lexer_peek_token (parser->lexer);
2630 switch (token->type)
2633 case CPP_PRAGMA_EOL:
2634 /* If we've run out of tokens, stop. */
2638 /* Stop if this is an unnested ';'. */
2643 case CPP_CLOSE_BRACE:
2644 /* Stop if this is an unnested '}', or closes the outermost
2647 if (nesting_depth < 0)
2653 case CPP_OPEN_BRACE:
2662 /* Consume the token. */
2663 cp_lexer_consume_token (parser->lexer);
2667 /* Skip tokens until a non-nested closing curly brace is the next
2668 token, or there are no more tokens. Return true in the first case,
2672 cp_parser_skip_to_closing_brace (cp_parser *parser)
2674 unsigned nesting_depth = 0;
2678 cp_token *token = cp_lexer_peek_token (parser->lexer);
2680 switch (token->type)
2683 case CPP_PRAGMA_EOL:
2684 /* If we've run out of tokens, stop. */
2687 case CPP_CLOSE_BRACE:
2688 /* If the next token is a non-nested `}', then we have reached
2689 the end of the current block. */
2690 if (nesting_depth-- == 0)
2694 case CPP_OPEN_BRACE:
2695 /* If it the next token is a `{', then we are entering a new
2696 block. Consume the entire block. */
2704 /* Consume the token. */
2705 cp_lexer_consume_token (parser->lexer);
2709 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2710 parameter is the PRAGMA token, allowing us to purge the entire pragma
2714 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2718 parser->lexer->in_pragma = false;
2721 token = cp_lexer_consume_token (parser->lexer);
2722 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2724 /* Ensure that the pragma is not parsed again. */
2725 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2728 /* Require pragma end of line, resyncing with it as necessary. The
2729 arguments are as for cp_parser_skip_to_pragma_eol. */
2732 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2734 parser->lexer->in_pragma = false;
2735 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2736 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2739 /* This is a simple wrapper around make_typename_type. When the id is
2740 an unresolved identifier node, we can provide a superior diagnostic
2741 using cp_parser_diagnose_invalid_type_name. */
2744 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2745 tree id, location_t id_location)
2748 if (TREE_CODE (id) == IDENTIFIER_NODE)
2750 result = make_typename_type (scope, id, typename_type,
2751 /*complain=*/tf_none);
2752 if (result == error_mark_node)
2753 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2756 return make_typename_type (scope, id, typename_type, tf_error);
2759 /* This is a wrapper around the
2760 make_{pointer,ptrmem,reference}_declarator functions that decides
2761 which one to call based on the CODE and CLASS_TYPE arguments. The
2762 CODE argument should be one of the values returned by
2763 cp_parser_ptr_operator. */
2764 static cp_declarator *
2765 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2766 cp_cv_quals cv_qualifiers,
2767 cp_declarator *target)
2769 if (code == ERROR_MARK)
2770 return cp_error_declarator;
2772 if (code == INDIRECT_REF)
2773 if (class_type == NULL_TREE)
2774 return make_pointer_declarator (cv_qualifiers, target);
2776 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2777 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2778 return make_reference_declarator (cv_qualifiers, target, false);
2779 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2780 return make_reference_declarator (cv_qualifiers, target, true);
2784 /* Create a new C++ parser. */
2787 cp_parser_new (void)
2793 /* cp_lexer_new_main is called before calling ggc_alloc because
2794 cp_lexer_new_main might load a PCH file. */
2795 lexer = cp_lexer_new_main ();
2797 /* Initialize the binops_by_token so that we can get the tree
2798 directly from the token. */
2799 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2800 binops_by_token[binops[i].token_type] = binops[i];
2802 parser = GGC_CNEW (cp_parser);
2803 parser->lexer = lexer;
2804 parser->context = cp_parser_context_new (NULL);
2806 /* For now, we always accept GNU extensions. */
2807 parser->allow_gnu_extensions_p = 1;
2809 /* The `>' token is a greater-than operator, not the end of a
2811 parser->greater_than_is_operator_p = true;
2813 parser->default_arg_ok_p = true;
2815 /* We are not parsing a constant-expression. */
2816 parser->integral_constant_expression_p = false;
2817 parser->allow_non_integral_constant_expression_p = false;
2818 parser->non_integral_constant_expression_p = false;
2820 /* Local variable names are not forbidden. */
2821 parser->local_variables_forbidden_p = false;
2823 /* We are not processing an `extern "C"' declaration. */
2824 parser->in_unbraced_linkage_specification_p = false;
2826 /* We are not processing a declarator. */
2827 parser->in_declarator_p = false;
2829 /* We are not processing a template-argument-list. */
2830 parser->in_template_argument_list_p = false;
2832 /* We are not in an iteration statement. */
2833 parser->in_statement = 0;
2835 /* We are not in a switch statement. */
2836 parser->in_switch_statement_p = false;
2838 /* We are not parsing a type-id inside an expression. */
2839 parser->in_type_id_in_expr_p = false;
2841 /* Declarations aren't implicitly extern "C". */
2842 parser->implicit_extern_c = false;
2844 /* String literals should be translated to the execution character set. */
2845 parser->translate_strings_p = true;
2847 /* We are not parsing a function body. */
2848 parser->in_function_body = false;
2850 /* The unparsed function queue is empty. */
2851 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2853 /* There are no classes being defined. */
2854 parser->num_classes_being_defined = 0;
2856 /* No template parameters apply. */
2857 parser->num_template_parameter_lists = 0;
2862 /* Create a cp_lexer structure which will emit the tokens in CACHE
2863 and push it onto the parser's lexer stack. This is used for delayed
2864 parsing of in-class method bodies and default arguments, and should
2865 not be confused with tentative parsing. */
2867 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2869 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2870 lexer->next = parser->lexer;
2871 parser->lexer = lexer;
2873 /* Move the current source position to that of the first token in the
2875 cp_lexer_set_source_position_from_token (lexer->next_token);
2878 /* Pop the top lexer off the parser stack. This is never used for the
2879 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2881 cp_parser_pop_lexer (cp_parser *parser)
2883 cp_lexer *lexer = parser->lexer;
2884 parser->lexer = lexer->next;
2885 cp_lexer_destroy (lexer);
2887 /* Put the current source position back where it was before this
2888 lexer was pushed. */
2889 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2892 /* Lexical conventions [gram.lex] */
2894 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2898 cp_parser_identifier (cp_parser* parser)
2902 /* Look for the identifier. */
2903 token = cp_parser_require (parser, CPP_NAME, "identifier");
2904 /* Return the value. */
2905 return token ? token->u.value : error_mark_node;
2908 /* Parse a sequence of adjacent string constants. Returns a
2909 TREE_STRING representing the combined, nul-terminated string
2910 constant. If TRANSLATE is true, translate the string to the
2911 execution character set. If WIDE_OK is true, a wide string is
2914 C++98 [lex.string] says that if a narrow string literal token is
2915 adjacent to a wide string literal token, the behavior is undefined.
2916 However, C99 6.4.5p4 says that this results in a wide string literal.
2917 We follow C99 here, for consistency with the C front end.
2919 This code is largely lifted from lex_string() in c-lex.c.
2921 FUTURE: ObjC++ will need to handle @-strings here. */
2923 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2927 struct obstack str_ob;
2928 cpp_string str, istr, *strs;
2930 enum cpp_ttype type;
2932 tok = cp_lexer_peek_token (parser->lexer);
2933 if (!cp_parser_is_string_literal (tok))
2935 cp_parser_error (parser, "expected string-literal");
2936 return error_mark_node;
2941 /* Try to avoid the overhead of creating and destroying an obstack
2942 for the common case of just one string. */
2943 if (!cp_parser_is_string_literal
2944 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2946 cp_lexer_consume_token (parser->lexer);
2948 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2949 str.len = TREE_STRING_LENGTH (tok->u.value);
2956 gcc_obstack_init (&str_ob);
2961 cp_lexer_consume_token (parser->lexer);
2963 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2964 str.len = TREE_STRING_LENGTH (tok->u.value);
2966 if (type != tok->type)
2968 if (type == CPP_STRING)
2970 else if (tok->type != CPP_STRING)
2971 error_at (tok->location,
2972 "unsupported non-standard concatenation "
2973 "of string literals");
2976 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2978 tok = cp_lexer_peek_token (parser->lexer);
2980 while (cp_parser_is_string_literal (tok));
2982 strs = (cpp_string *) obstack_finish (&str_ob);
2985 if (type != CPP_STRING && !wide_ok)
2987 cp_parser_error (parser, "a wide string is invalid in this context");
2991 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2992 (parse_in, strs, count, &istr, type))
2994 value = build_string (istr.len, (const char *)istr.text);
2995 free (CONST_CAST (unsigned char *, istr.text));
3001 TREE_TYPE (value) = char_array_type_node;
3004 TREE_TYPE (value) = char16_array_type_node;
3007 TREE_TYPE (value) = char32_array_type_node;
3010 TREE_TYPE (value) = wchar_array_type_node;
3014 value = fix_string_type (value);
3017 /* cpp_interpret_string has issued an error. */
3018 value = error_mark_node;
3021 obstack_free (&str_ob, 0);
3027 /* Basic concepts [gram.basic] */
3029 /* Parse a translation-unit.
3032 declaration-seq [opt]
3034 Returns TRUE if all went well. */
3037 cp_parser_translation_unit (cp_parser* parser)
3039 /* The address of the first non-permanent object on the declarator
3041 static void *declarator_obstack_base;
3045 /* Create the declarator obstack, if necessary. */
3046 if (!cp_error_declarator)
3048 gcc_obstack_init (&declarator_obstack);
3049 /* Create the error declarator. */
3050 cp_error_declarator = make_declarator (cdk_error);
3051 /* Create the empty parameter list. */
3052 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3053 /* Remember where the base of the declarator obstack lies. */
3054 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3057 cp_parser_declaration_seq_opt (parser);
3059 /* If there are no tokens left then all went well. */
3060 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3062 /* Get rid of the token array; we don't need it any more. */
3063 cp_lexer_destroy (parser->lexer);
3064 parser->lexer = NULL;
3066 /* This file might have been a context that's implicitly extern
3067 "C". If so, pop the lang context. (Only relevant for PCH.) */
3068 if (parser->implicit_extern_c)
3070 pop_lang_context ();
3071 parser->implicit_extern_c = false;
3075 finish_translation_unit ();
3081 cp_parser_error (parser, "expected declaration");
3085 /* Make sure the declarator obstack was fully cleaned up. */
3086 gcc_assert (obstack_next_free (&declarator_obstack)
3087 == declarator_obstack_base);
3089 /* All went well. */
3093 /* Expressions [gram.expr] */
3095 /* Parse a primary-expression.
3106 ( compound-statement )
3107 __builtin_va_arg ( assignment-expression , type-id )
3108 __builtin_offsetof ( type-id , offsetof-expression )
3111 __has_nothrow_assign ( type-id )
3112 __has_nothrow_constructor ( type-id )
3113 __has_nothrow_copy ( type-id )
3114 __has_trivial_assign ( type-id )
3115 __has_trivial_constructor ( type-id )
3116 __has_trivial_copy ( type-id )
3117 __has_trivial_destructor ( type-id )
3118 __has_virtual_destructor ( type-id )
3119 __is_abstract ( type-id )
3120 __is_base_of ( type-id , type-id )
3121 __is_class ( type-id )
3122 __is_convertible_to ( type-id , type-id )
3123 __is_empty ( type-id )
3124 __is_enum ( type-id )
3125 __is_pod ( type-id )
3126 __is_polymorphic ( type-id )
3127 __is_union ( type-id )
3129 Objective-C++ Extension:
3137 ADDRESS_P is true iff this expression was immediately preceded by
3138 "&" and therefore might denote a pointer-to-member. CAST_P is true
3139 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3140 true iff this expression is a template argument.
3142 Returns a representation of the expression. Upon return, *IDK
3143 indicates what kind of id-expression (if any) was present. */
3146 cp_parser_primary_expression (cp_parser *parser,
3149 bool template_arg_p,
3152 cp_token *token = NULL;
3154 /* Assume the primary expression is not an id-expression. */
3155 *idk = CP_ID_KIND_NONE;
3157 /* Peek at the next token. */
3158 token = cp_lexer_peek_token (parser->lexer);
3159 switch (token->type)
3172 token = cp_lexer_consume_token (parser->lexer);
3173 if (TREE_CODE (token->u.value) == FIXED_CST)
3175 error_at (token->location,
3176 "fixed-point types not supported in C++");
3177 return error_mark_node;
3179 /* Floating-point literals are only allowed in an integral
3180 constant expression if they are cast to an integral or
3181 enumeration type. */
3182 if (TREE_CODE (token->u.value) == REAL_CST
3183 && parser->integral_constant_expression_p
3186 /* CAST_P will be set even in invalid code like "int(2.7 +
3187 ...)". Therefore, we have to check that the next token
3188 is sure to end the cast. */
3191 cp_token *next_token;
3193 next_token = cp_lexer_peek_token (parser->lexer);
3194 if (/* The comma at the end of an
3195 enumerator-definition. */
3196 next_token->type != CPP_COMMA
3197 /* The curly brace at the end of an enum-specifier. */
3198 && next_token->type != CPP_CLOSE_BRACE
3199 /* The end of a statement. */
3200 && next_token->type != CPP_SEMICOLON
3201 /* The end of the cast-expression. */
3202 && next_token->type != CPP_CLOSE_PAREN
3203 /* The end of an array bound. */
3204 && next_token->type != CPP_CLOSE_SQUARE
3205 /* The closing ">" in a template-argument-list. */
3206 && (next_token->type != CPP_GREATER
3207 || parser->greater_than_is_operator_p)
3208 /* C++0x only: A ">>" treated like two ">" tokens,
3209 in a template-argument-list. */
3210 && (next_token->type != CPP_RSHIFT
3211 || (cxx_dialect == cxx98)
3212 || parser->greater_than_is_operator_p))
3216 /* If we are within a cast, then the constraint that the
3217 cast is to an integral or enumeration type will be
3218 checked at that point. If we are not within a cast, then
3219 this code is invalid. */
3221 cp_parser_non_integral_constant_expression
3222 (parser, "floating-point literal");
3224 return token->u.value;
3230 /* ??? Should wide strings be allowed when parser->translate_strings_p
3231 is false (i.e. in attributes)? If not, we can kill the third
3232 argument to cp_parser_string_literal. */
3233 return cp_parser_string_literal (parser,
3234 parser->translate_strings_p,
3237 case CPP_OPEN_PAREN:
3240 bool saved_greater_than_is_operator_p;
3242 /* Consume the `('. */
3243 cp_lexer_consume_token (parser->lexer);
3244 /* Within a parenthesized expression, a `>' token is always
3245 the greater-than operator. */
3246 saved_greater_than_is_operator_p
3247 = parser->greater_than_is_operator_p;
3248 parser->greater_than_is_operator_p = true;
3249 /* If we see `( { ' then we are looking at the beginning of
3250 a GNU statement-expression. */
3251 if (cp_parser_allow_gnu_extensions_p (parser)
3252 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3254 /* Statement-expressions are not allowed by the standard. */
3255 pedwarn (token->location, OPT_pedantic,
3256 "ISO C++ forbids braced-groups within expressions");
3258 /* And they're not allowed outside of a function-body; you
3259 cannot, for example, write:
3261 int i = ({ int j = 3; j + 1; });
3263 at class or namespace scope. */
3264 if (!parser->in_function_body
3265 || parser->in_template_argument_list_p)
3267 error_at (token->location,
3268 "statement-expressions are not allowed outside "
3269 "functions nor in template-argument lists");
3270 cp_parser_skip_to_end_of_block_or_statement (parser);
3271 expr = error_mark_node;
3275 /* Start the statement-expression. */
3276 expr = begin_stmt_expr ();
3277 /* Parse the compound-statement. */
3278 cp_parser_compound_statement (parser, expr, false);
3280 expr = finish_stmt_expr (expr, false);
3285 /* Parse the parenthesized expression. */
3286 expr = cp_parser_expression (parser, cast_p, idk);
3287 /* Let the front end know that this expression was
3288 enclosed in parentheses. This matters in case, for
3289 example, the expression is of the form `A::B', since
3290 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3292 finish_parenthesized_expr (expr);
3294 /* The `>' token might be the end of a template-id or
3295 template-parameter-list now. */
3296 parser->greater_than_is_operator_p
3297 = saved_greater_than_is_operator_p;
3298 /* Consume the `)'. */
3299 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3300 cp_parser_skip_to_end_of_statement (parser);
3305 case CPP_OPEN_SQUARE:
3306 if (c_dialect_objc ())
3307 /* We have an Objective-C++ message. */
3308 return cp_parser_objc_expression (parser);
3309 maybe_warn_cpp0x ("lambda expressions");
3310 return cp_parser_lambda_expression (parser);
3312 case CPP_OBJC_STRING:
3313 if (c_dialect_objc ())
3314 /* We have an Objective-C++ string literal. */
3315 return cp_parser_objc_expression (parser);
3316 cp_parser_error (parser, "expected primary-expression");
3317 return error_mark_node;
3320 switch (token->keyword)
3322 /* These two are the boolean literals. */
3324 cp_lexer_consume_token (parser->lexer);
3325 return boolean_true_node;
3327 cp_lexer_consume_token (parser->lexer);
3328 return boolean_false_node;
3330 /* The `__null' literal. */
3332 cp_lexer_consume_token (parser->lexer);
3335 /* Recognize the `this' keyword. */
3337 cp_lexer_consume_token (parser->lexer);
3338 if (parser->local_variables_forbidden_p)
3340 error_at (token->location,
3341 "%<this%> may not be used in this context");
3342 return error_mark_node;
3344 /* Pointers cannot appear in constant-expressions. */
3345 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3346 return error_mark_node;
3347 return finish_this_expr ();
3349 /* The `operator' keyword can be the beginning of an
3354 case RID_FUNCTION_NAME:
3355 case RID_PRETTY_FUNCTION_NAME:
3356 case RID_C99_FUNCTION_NAME:
3360 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3361 __func__ are the names of variables -- but they are
3362 treated specially. Therefore, they are handled here,
3363 rather than relying on the generic id-expression logic
3364 below. Grammatically, these names are id-expressions.
3366 Consume the token. */
3367 token = cp_lexer_consume_token (parser->lexer);
3369 switch (token->keyword)
3371 case RID_FUNCTION_NAME:
3372 name = "%<__FUNCTION__%>";
3374 case RID_PRETTY_FUNCTION_NAME:
3375 name = "%<__PRETTY_FUNCTION__%>";
3377 case RID_C99_FUNCTION_NAME:
3378 name = "%<__func__%>";
3384 if (cp_parser_non_integral_constant_expression (parser, name))
3385 return error_mark_node;
3387 /* Look up the name. */
3388 return finish_fname (token->u.value);
3396 /* The `__builtin_va_arg' construct is used to handle
3397 `va_arg'. Consume the `__builtin_va_arg' token. */
3398 cp_lexer_consume_token (parser->lexer);
3399 /* Look for the opening `('. */
3400 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3401 /* Now, parse the assignment-expression. */
3402 expression = cp_parser_assignment_expression (parser,
3403 /*cast_p=*/false, NULL);
3404 /* Look for the `,'. */
3405 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3406 /* Parse the type-id. */
3407 type = cp_parser_type_id (parser);
3408 /* Look for the closing `)'. */
3409 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3410 /* Using `va_arg' in a constant-expression is not
3412 if (cp_parser_non_integral_constant_expression (parser,
3414 return error_mark_node;
3415 return build_x_va_arg (expression, type);
3419 return cp_parser_builtin_offsetof (parser);
3421 case RID_HAS_NOTHROW_ASSIGN:
3422 case RID_HAS_NOTHROW_CONSTRUCTOR:
3423 case RID_HAS_NOTHROW_COPY:
3424 case RID_HAS_TRIVIAL_ASSIGN:
3425 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3426 case RID_HAS_TRIVIAL_COPY:
3427 case RID_HAS_TRIVIAL_DESTRUCTOR:
3428 case RID_HAS_VIRTUAL_DESTRUCTOR:
3429 case RID_IS_ABSTRACT:
3430 case RID_IS_BASE_OF:
3432 case RID_IS_CONVERTIBLE_TO:
3436 case RID_IS_POLYMORPHIC:
3437 case RID_IS_STD_LAYOUT:
3438 case RID_IS_TRIVIAL:
3440 return cp_parser_trait_expr (parser, token->keyword);
3442 /* Objective-C++ expressions. */
3444 case RID_AT_PROTOCOL:
3445 case RID_AT_SELECTOR:
3446 return cp_parser_objc_expression (parser);
3449 cp_parser_error (parser, "expected primary-expression");
3450 return error_mark_node;
3453 /* An id-expression can start with either an identifier, a
3454 `::' as the beginning of a qualified-id, or the "operator"
3458 case CPP_TEMPLATE_ID:
3459 case CPP_NESTED_NAME_SPECIFIER:
3463 const char *error_msg;
3466 cp_token *id_expr_token;
3469 /* Parse the id-expression. */
3471 = cp_parser_id_expression (parser,
3472 /*template_keyword_p=*/false,
3473 /*check_dependency_p=*/true,
3475 /*declarator_p=*/false,
3476 /*optional_p=*/false);
3477 if (id_expression == error_mark_node)
3478 return error_mark_node;
3479 id_expr_token = token;
3480 token = cp_lexer_peek_token (parser->lexer);
3481 done = (token->type != CPP_OPEN_SQUARE
3482 && token->type != CPP_OPEN_PAREN
3483 && token->type != CPP_DOT
3484 && token->type != CPP_DEREF
3485 && token->type != CPP_PLUS_PLUS
3486 && token->type != CPP_MINUS_MINUS);
3487 /* If we have a template-id, then no further lookup is
3488 required. If the template-id was for a template-class, we
3489 will sometimes have a TYPE_DECL at this point. */
3490 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3491 || TREE_CODE (id_expression) == TYPE_DECL)
3492 decl = id_expression;
3493 /* Look up the name. */
3496 tree ambiguous_decls;
3498 decl = cp_parser_lookup_name (parser, id_expression,
3501 /*is_namespace=*/false,
3502 /*check_dependency=*/true,
3504 id_expr_token->location);
3505 /* If the lookup was ambiguous, an error will already have
3507 if (ambiguous_decls)
3508 return error_mark_node;
3510 /* In Objective-C++, an instance variable (ivar) may be preferred
3511 to whatever cp_parser_lookup_name() found. */
3512 decl = objc_lookup_ivar (decl, id_expression);
3514 /* If name lookup gives us a SCOPE_REF, then the
3515 qualifying scope was dependent. */
3516 if (TREE_CODE (decl) == SCOPE_REF)
3518 /* At this point, we do not know if DECL is a valid
3519 integral constant expression. We assume that it is
3520 in fact such an expression, so that code like:
3522 template <int N> struct A {
3526 is accepted. At template-instantiation time, we
3527 will check that B<N>::i is actually a constant. */
3530 /* Check to see if DECL is a local variable in a context
3531 where that is forbidden. */
3532 if (parser->local_variables_forbidden_p
3533 && local_variable_p (decl))
3535 /* It might be that we only found DECL because we are
3536 trying to be generous with pre-ISO scoping rules.
3537 For example, consider:
3541 for (int i = 0; i < 10; ++i) {}
3542 extern void f(int j = i);
3545 Here, name look up will originally find the out
3546 of scope `i'. We need to issue a warning message,
3547 but then use the global `i'. */
3548 decl = check_for_out_of_scope_variable (decl);
3549 if (local_variable_p (decl))
3551 error_at (id_expr_token->location,
3552 "local variable %qD may not appear in this context",
3554 return error_mark_node;
3559 decl = (finish_id_expression
3560 (id_expression, decl, parser->scope,
3562 parser->integral_constant_expression_p,
3563 parser->allow_non_integral_constant_expression_p,
3564 &parser->non_integral_constant_expression_p,
3565 template_p, done, address_p,
3568 id_expr_token->location));
3570 cp_parser_error (parser, error_msg);
3574 /* Anything else is an error. */
3576 cp_parser_error (parser, "expected primary-expression");
3577 return error_mark_node;
3581 /* Parse an id-expression.
3588 :: [opt] nested-name-specifier template [opt] unqualified-id
3590 :: operator-function-id
3593 Return a representation of the unqualified portion of the
3594 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3595 a `::' or nested-name-specifier.
3597 Often, if the id-expression was a qualified-id, the caller will
3598 want to make a SCOPE_REF to represent the qualified-id. This
3599 function does not do this in order to avoid wastefully creating
3600 SCOPE_REFs when they are not required.
3602 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3605 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3606 uninstantiated templates.
3608 If *TEMPLATE_P is non-NULL, it is set to true iff the
3609 `template' keyword is used to explicitly indicate that the entity
3610 named is a template.
3612 If DECLARATOR_P is true, the id-expression is appearing as part of
3613 a declarator, rather than as part of an expression. */
3616 cp_parser_id_expression (cp_parser *parser,
3617 bool template_keyword_p,
3618 bool check_dependency_p,
3623 bool global_scope_p;
3624 bool nested_name_specifier_p;
3626 /* Assume the `template' keyword was not used. */
3628 *template_p = template_keyword_p;
3630 /* Look for the optional `::' operator. */
3632 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3634 /* Look for the optional nested-name-specifier. */
3635 nested_name_specifier_p
3636 = (cp_parser_nested_name_specifier_opt (parser,
3637 /*typename_keyword_p=*/false,
3642 /* If there is a nested-name-specifier, then we are looking at
3643 the first qualified-id production. */
3644 if (nested_name_specifier_p)
3647 tree saved_object_scope;
3648 tree saved_qualifying_scope;
3649 tree unqualified_id;
3652 /* See if the next token is the `template' keyword. */
3654 template_p = &is_template;
3655 *template_p = cp_parser_optional_template_keyword (parser);
3656 /* Name lookup we do during the processing of the
3657 unqualified-id might obliterate SCOPE. */
3658 saved_scope = parser->scope;
3659 saved_object_scope = parser->object_scope;
3660 saved_qualifying_scope = parser->qualifying_scope;
3661 /* Process the final unqualified-id. */
3662 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3665 /*optional_p=*/false);
3666 /* Restore the SAVED_SCOPE for our caller. */
3667 parser->scope = saved_scope;
3668 parser->object_scope = saved_object_scope;
3669 parser->qualifying_scope = saved_qualifying_scope;
3671 return unqualified_id;
3673 /* Otherwise, if we are in global scope, then we are looking at one
3674 of the other qualified-id productions. */
3675 else if (global_scope_p)
3680 /* Peek at the next token. */
3681 token = cp_lexer_peek_token (parser->lexer);
3683 /* If it's an identifier, and the next token is not a "<", then
3684 we can avoid the template-id case. This is an optimization
3685 for this common case. */
3686 if (token->type == CPP_NAME
3687 && !cp_parser_nth_token_starts_template_argument_list_p
3689 return cp_parser_identifier (parser);
3691 cp_parser_parse_tentatively (parser);
3692 /* Try a template-id. */
3693 id = cp_parser_template_id (parser,
3694 /*template_keyword_p=*/false,
3695 /*check_dependency_p=*/true,
3697 /* If that worked, we're done. */
3698 if (cp_parser_parse_definitely (parser))
3701 /* Peek at the next token. (Changes in the token buffer may
3702 have invalidated the pointer obtained above.) */
3703 token = cp_lexer_peek_token (parser->lexer);
3705 switch (token->type)
3708 return cp_parser_identifier (parser);
3711 if (token->keyword == RID_OPERATOR)
3712 return cp_parser_operator_function_id (parser);
3716 cp_parser_error (parser, "expected id-expression");
3717 return error_mark_node;
3721 return cp_parser_unqualified_id (parser, template_keyword_p,
3722 /*check_dependency_p=*/true,
3727 /* Parse an unqualified-id.
3731 operator-function-id
3732 conversion-function-id
3736 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3737 keyword, in a construct like `A::template ...'.
3739 Returns a representation of unqualified-id. For the `identifier'
3740 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3741 production a BIT_NOT_EXPR is returned; the operand of the
3742 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3743 other productions, see the documentation accompanying the
3744 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3745 names are looked up in uninstantiated templates. If DECLARATOR_P
3746 is true, the unqualified-id is appearing as part of a declarator,
3747 rather than as part of an expression. */
3750 cp_parser_unqualified_id (cp_parser* parser,
3751 bool template_keyword_p,
3752 bool check_dependency_p,
3758 /* Peek at the next token. */
3759 token = cp_lexer_peek_token (parser->lexer);
3761 switch (token->type)
3767 /* We don't know yet whether or not this will be a
3769 cp_parser_parse_tentatively (parser);
3770 /* Try a template-id. */
3771 id = cp_parser_template_id (parser, template_keyword_p,
3774 /* If it worked, we're done. */
3775 if (cp_parser_parse_definitely (parser))
3777 /* Otherwise, it's an ordinary identifier. */
3778 return cp_parser_identifier (parser);
3781 case CPP_TEMPLATE_ID:
3782 return cp_parser_template_id (parser, template_keyword_p,
3789 tree qualifying_scope;
3794 /* Consume the `~' token. */
3795 cp_lexer_consume_token (parser->lexer);
3796 /* Parse the class-name. The standard, as written, seems to
3799 template <typename T> struct S { ~S (); };
3800 template <typename T> S<T>::~S() {}
3802 is invalid, since `~' must be followed by a class-name, but
3803 `S<T>' is dependent, and so not known to be a class.
3804 That's not right; we need to look in uninstantiated
3805 templates. A further complication arises from:
3807 template <typename T> void f(T t) {
3811 Here, it is not possible to look up `T' in the scope of `T'
3812 itself. We must look in both the current scope, and the
3813 scope of the containing complete expression.
3815 Yet another issue is:
3824 The standard does not seem to say that the `S' in `~S'
3825 should refer to the type `S' and not the data member
3828 /* DR 244 says that we look up the name after the "~" in the
3829 same scope as we looked up the qualifying name. That idea
3830 isn't fully worked out; it's more complicated than that. */
3831 scope = parser->scope;
3832 object_scope = parser->object_scope;
3833 qualifying_scope = parser->qualifying_scope;
3835 /* Check for invalid scopes. */
3836 if (scope == error_mark_node)
3838 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3839 cp_lexer_consume_token (parser->lexer);
3840 return error_mark_node;
3842 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3844 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3845 error_at (token->location,
3846 "scope %qT before %<~%> is not a class-name",
3848 cp_parser_simulate_error (parser);
3849 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3850 cp_lexer_consume_token (parser->lexer);
3851 return error_mark_node;
3853 gcc_assert (!scope || TYPE_P (scope));
3855 /* If the name is of the form "X::~X" it's OK. */
3856 token = cp_lexer_peek_token (parser->lexer);
3858 && token->type == CPP_NAME
3859 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3861 && constructor_name_p (token->u.value, scope))
3863 cp_lexer_consume_token (parser->lexer);
3864 return build_nt (BIT_NOT_EXPR, scope);
3867 /* If there was an explicit qualification (S::~T), first look
3868 in the scope given by the qualification (i.e., S). */
3870 type_decl = NULL_TREE;
3873 cp_parser_parse_tentatively (parser);
3874 type_decl = cp_parser_class_name (parser,
3875 /*typename_keyword_p=*/false,
3876 /*template_keyword_p=*/false,
3878 /*check_dependency=*/false,
3879 /*class_head_p=*/false,
3881 if (cp_parser_parse_definitely (parser))
3884 /* In "N::S::~S", look in "N" as well. */
3885 if (!done && scope && qualifying_scope)
3887 cp_parser_parse_tentatively (parser);
3888 parser->scope = qualifying_scope;
3889 parser->object_scope = NULL_TREE;
3890 parser->qualifying_scope = NULL_TREE;
3892 = cp_parser_class_name (parser,
3893 /*typename_keyword_p=*/false,
3894 /*template_keyword_p=*/false,
3896 /*check_dependency=*/false,
3897 /*class_head_p=*/false,
3899 if (cp_parser_parse_definitely (parser))
3902 /* In "p->S::~T", look in the scope given by "*p" as well. */
3903 else if (!done && object_scope)
3905 cp_parser_parse_tentatively (parser);
3906 parser->scope = object_scope;
3907 parser->object_scope = NULL_TREE;
3908 parser->qualifying_scope = NULL_TREE;
3910 = cp_parser_class_name (parser,
3911 /*typename_keyword_p=*/false,
3912 /*template_keyword_p=*/false,
3914 /*check_dependency=*/false,
3915 /*class_head_p=*/false,
3917 if (cp_parser_parse_definitely (parser))
3920 /* Look in the surrounding context. */
3923 parser->scope = NULL_TREE;
3924 parser->object_scope = NULL_TREE;
3925 parser->qualifying_scope = NULL_TREE;
3926 if (processing_template_decl)
3927 cp_parser_parse_tentatively (parser);
3929 = cp_parser_class_name (parser,
3930 /*typename_keyword_p=*/false,
3931 /*template_keyword_p=*/false,
3933 /*check_dependency=*/false,
3934 /*class_head_p=*/false,
3936 if (processing_template_decl
3937 && ! cp_parser_parse_definitely (parser))
3939 /* We couldn't find a type with this name, so just accept
3940 it and check for a match at instantiation time. */
3941 type_decl = cp_parser_identifier (parser);
3942 if (type_decl != error_mark_node)
3943 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3947 /* If an error occurred, assume that the name of the
3948 destructor is the same as the name of the qualifying
3949 class. That allows us to keep parsing after running
3950 into ill-formed destructor names. */
3951 if (type_decl == error_mark_node && scope)
3952 return build_nt (BIT_NOT_EXPR, scope);
3953 else if (type_decl == error_mark_node)
3954 return error_mark_node;
3956 /* Check that destructor name and scope match. */
3957 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3959 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3960 error_at (token->location,
3961 "declaration of %<~%T%> as member of %qT",
3963 cp_parser_simulate_error (parser);
3964 return error_mark_node;
3969 A typedef-name that names a class shall not be used as the
3970 identifier in the declarator for a destructor declaration. */
3972 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3973 && !DECL_SELF_REFERENCE_P (type_decl)
3974 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3975 error_at (token->location,
3976 "typedef-name %qD used as destructor declarator",
3979 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3983 if (token->keyword == RID_OPERATOR)
3987 /* This could be a template-id, so we try that first. */
3988 cp_parser_parse_tentatively (parser);
3989 /* Try a template-id. */
3990 id = cp_parser_template_id (parser, template_keyword_p,
3991 /*check_dependency_p=*/true,
3993 /* If that worked, we're done. */
3994 if (cp_parser_parse_definitely (parser))
3996 /* We still don't know whether we're looking at an
3997 operator-function-id or a conversion-function-id. */
3998 cp_parser_parse_tentatively (parser);
3999 /* Try an operator-function-id. */
4000 id = cp_parser_operator_function_id (parser);
4001 /* If that didn't work, try a conversion-function-id. */
4002 if (!cp_parser_parse_definitely (parser))
4003 id = cp_parser_conversion_function_id (parser);
4012 cp_parser_error (parser, "expected unqualified-id");
4013 return error_mark_node;
4017 /* Parse an (optional) nested-name-specifier.
4019 nested-name-specifier: [C++98]
4020 class-or-namespace-name :: nested-name-specifier [opt]
4021 class-or-namespace-name :: template nested-name-specifier [opt]
4023 nested-name-specifier: [C++0x]
4026 nested-name-specifier identifier ::
4027 nested-name-specifier template [opt] simple-template-id ::
4029 PARSER->SCOPE should be set appropriately before this function is
4030 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4031 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4034 Sets PARSER->SCOPE to the class (TYPE) or namespace
4035 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4036 it unchanged if there is no nested-name-specifier. Returns the new
4037 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4039 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4040 part of a declaration and/or decl-specifier. */
4043 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4044 bool typename_keyword_p,
4045 bool check_dependency_p,
4047 bool is_declaration)
4049 bool success = false;
4050 cp_token_position start = 0;
4053 /* Remember where the nested-name-specifier starts. */
4054 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4056 start = cp_lexer_token_position (parser->lexer, false);
4057 push_deferring_access_checks (dk_deferred);
4064 tree saved_qualifying_scope;
4065 bool template_keyword_p;
4067 /* Spot cases that cannot be the beginning of a
4068 nested-name-specifier. */
4069 token = cp_lexer_peek_token (parser->lexer);
4071 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4072 the already parsed nested-name-specifier. */
4073 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4075 /* Grab the nested-name-specifier and continue the loop. */
4076 cp_parser_pre_parsed_nested_name_specifier (parser);
4077 /* If we originally encountered this nested-name-specifier
4078 with IS_DECLARATION set to false, we will not have
4079 resolved TYPENAME_TYPEs, so we must do so here. */
4081 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4083 new_scope = resolve_typename_type (parser->scope,
4084 /*only_current_p=*/false);
4085 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4086 parser->scope = new_scope;
4092 /* Spot cases that cannot be the beginning of a
4093 nested-name-specifier. On the second and subsequent times
4094 through the loop, we look for the `template' keyword. */
4095 if (success && token->keyword == RID_TEMPLATE)
4097 /* A template-id can start a nested-name-specifier. */
4098 else if (token->type == CPP_TEMPLATE_ID)
4102 /* If the next token is not an identifier, then it is
4103 definitely not a type-name or namespace-name. */
4104 if (token->type != CPP_NAME)
4106 /* If the following token is neither a `<' (to begin a
4107 template-id), nor a `::', then we are not looking at a
4108 nested-name-specifier. */
4109 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4110 if (token->type != CPP_SCOPE
4111 && !cp_parser_nth_token_starts_template_argument_list_p
4116 /* The nested-name-specifier is optional, so we parse
4118 cp_parser_parse_tentatively (parser);
4120 /* Look for the optional `template' keyword, if this isn't the
4121 first time through the loop. */
4123 template_keyword_p = cp_parser_optional_template_keyword (parser);
4125 template_keyword_p = false;
4127 /* Save the old scope since the name lookup we are about to do
4128 might destroy it. */
4129 old_scope = parser->scope;
4130 saved_qualifying_scope = parser->qualifying_scope;
4131 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4132 look up names in "X<T>::I" in order to determine that "Y" is
4133 a template. So, if we have a typename at this point, we make
4134 an effort to look through it. */
4136 && !typename_keyword_p
4138 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4139 parser->scope = resolve_typename_type (parser->scope,
4140 /*only_current_p=*/false);
4141 /* Parse the qualifying entity. */
4143 = cp_parser_qualifying_entity (parser,
4149 /* Look for the `::' token. */
4150 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4152 /* If we found what we wanted, we keep going; otherwise, we're
4154 if (!cp_parser_parse_definitely (parser))
4156 bool error_p = false;
4158 /* Restore the OLD_SCOPE since it was valid before the
4159 failed attempt at finding the last
4160 class-or-namespace-name. */
4161 parser->scope = old_scope;
4162 parser->qualifying_scope = saved_qualifying_scope;
4163 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4165 /* If the next token is an identifier, and the one after
4166 that is a `::', then any valid interpretation would have
4167 found a class-or-namespace-name. */
4168 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4169 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4171 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4174 token = cp_lexer_consume_token (parser->lexer);
4177 if (!token->ambiguous_p)
4180 tree ambiguous_decls;
4182 decl = cp_parser_lookup_name (parser, token->u.value,
4184 /*is_template=*/false,
4185 /*is_namespace=*/false,
4186 /*check_dependency=*/true,
4189 if (TREE_CODE (decl) == TEMPLATE_DECL)
4190 error_at (token->location,
4191 "%qD used without template parameters",
4193 else if (ambiguous_decls)
4195 error_at (token->location,
4196 "reference to %qD is ambiguous",
4198 print_candidates (ambiguous_decls);
4199 decl = error_mark_node;
4203 const char* msg = "is not a class or namespace";
4204 if (cxx_dialect != cxx98)
4205 msg = "is not a class, namespace, or enumeration";
4206 cp_parser_name_lookup_error
4207 (parser, token->u.value, decl, msg,
4211 parser->scope = error_mark_node;
4213 /* Treat this as a successful nested-name-specifier
4218 If the name found is not a class-name (clause
4219 _class_) or namespace-name (_namespace.def_), the
4220 program is ill-formed. */
4223 cp_lexer_consume_token (parser->lexer);
4227 /* We've found one valid nested-name-specifier. */
4229 /* Name lookup always gives us a DECL. */
4230 if (TREE_CODE (new_scope) == TYPE_DECL)
4231 new_scope = TREE_TYPE (new_scope);
4232 /* Uses of "template" must be followed by actual templates. */
4233 if (template_keyword_p
4234 && !(CLASS_TYPE_P (new_scope)
4235 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4236 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4237 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4238 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4239 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4240 == TEMPLATE_ID_EXPR)))
4241 permerror (input_location, TYPE_P (new_scope)
4242 ? "%qT is not a template"
4243 : "%qD is not a template",
4245 /* If it is a class scope, try to complete it; we are about to
4246 be looking up names inside the class. */
4247 if (TYPE_P (new_scope)
4248 /* Since checking types for dependency can be expensive,
4249 avoid doing it if the type is already complete. */
4250 && !COMPLETE_TYPE_P (new_scope)
4251 /* Do not try to complete dependent types. */
4252 && !dependent_type_p (new_scope))
4254 new_scope = complete_type (new_scope);
4255 /* If it is a typedef to current class, use the current
4256 class instead, as the typedef won't have any names inside
4258 if (!COMPLETE_TYPE_P (new_scope)
4259 && currently_open_class (new_scope))
4260 new_scope = TYPE_MAIN_VARIANT (new_scope);
4262 /* Make sure we look in the right scope the next time through
4264 parser->scope = new_scope;
4267 /* If parsing tentatively, replace the sequence of tokens that makes
4268 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4269 token. That way, should we re-parse the token stream, we will
4270 not have to repeat the effort required to do the parse, nor will
4271 we issue duplicate error messages. */
4272 if (success && start)
4276 token = cp_lexer_token_at (parser->lexer, start);
4277 /* Reset the contents of the START token. */
4278 token->type = CPP_NESTED_NAME_SPECIFIER;
4279 /* Retrieve any deferred checks. Do not pop this access checks yet
4280 so the memory will not be reclaimed during token replacing below. */
4281 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4282 token->u.tree_check_value->value = parser->scope;
4283 token->u.tree_check_value->checks = get_deferred_access_checks ();
4284 token->u.tree_check_value->qualifying_scope =
4285 parser->qualifying_scope;
4286 token->keyword = RID_MAX;
4288 /* Purge all subsequent tokens. */
4289 cp_lexer_purge_tokens_after (parser->lexer, start);
4293 pop_to_parent_deferring_access_checks ();
4295 return success ? parser->scope : NULL_TREE;
4298 /* Parse a nested-name-specifier. See
4299 cp_parser_nested_name_specifier_opt for details. This function
4300 behaves identically, except that it will an issue an error if no
4301 nested-name-specifier is present. */
4304 cp_parser_nested_name_specifier (cp_parser *parser,
4305 bool typename_keyword_p,
4306 bool check_dependency_p,
4308 bool is_declaration)
4312 /* Look for the nested-name-specifier. */
4313 scope = cp_parser_nested_name_specifier_opt (parser,
4318 /* If it was not present, issue an error message. */
4321 cp_parser_error (parser, "expected nested-name-specifier");
4322 parser->scope = NULL_TREE;
4328 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4329 this is either a class-name or a namespace-name (which corresponds
4330 to the class-or-namespace-name production in the grammar). For
4331 C++0x, it can also be a type-name that refers to an enumeration
4334 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4335 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4336 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4337 TYPE_P is TRUE iff the next name should be taken as a class-name,
4338 even the same name is declared to be another entity in the same
4341 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4342 specified by the class-or-namespace-name. If neither is found the
4343 ERROR_MARK_NODE is returned. */
4346 cp_parser_qualifying_entity (cp_parser *parser,
4347 bool typename_keyword_p,
4348 bool template_keyword_p,
4349 bool check_dependency_p,
4351 bool is_declaration)
4354 tree saved_qualifying_scope;
4355 tree saved_object_scope;
4358 bool successful_parse_p;
4360 /* Before we try to parse the class-name, we must save away the
4361 current PARSER->SCOPE since cp_parser_class_name will destroy
4363 saved_scope = parser->scope;
4364 saved_qualifying_scope = parser->qualifying_scope;
4365 saved_object_scope = parser->object_scope;
4366 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4367 there is no need to look for a namespace-name. */
4368 only_class_p = template_keyword_p
4369 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4371 cp_parser_parse_tentatively (parser);
4372 scope = cp_parser_class_name (parser,
4375 type_p ? class_type : none_type,
4377 /*class_head_p=*/false,
4379 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4380 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4382 && cxx_dialect != cxx98
4383 && !successful_parse_p)
4385 /* Restore the saved scope. */
4386 parser->scope = saved_scope;
4387 parser->qualifying_scope = saved_qualifying_scope;
4388 parser->object_scope = saved_object_scope;
4390 /* Parse tentatively. */
4391 cp_parser_parse_tentatively (parser);
4393 /* Parse a typedef-name or enum-name. */
4394 scope = cp_parser_nonclass_name (parser);
4395 successful_parse_p = cp_parser_parse_definitely (parser);
4397 /* If that didn't work, try for a namespace-name. */
4398 if (!only_class_p && !successful_parse_p)
4400 /* Restore the saved scope. */
4401 parser->scope = saved_scope;
4402 parser->qualifying_scope = saved_qualifying_scope;
4403 parser->object_scope = saved_object_scope;
4404 /* If we are not looking at an identifier followed by the scope
4405 resolution operator, then this is not part of a
4406 nested-name-specifier. (Note that this function is only used
4407 to parse the components of a nested-name-specifier.) */
4408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4409 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4410 return error_mark_node;
4411 scope = cp_parser_namespace_name (parser);
4417 /* Parse a postfix-expression.
4421 postfix-expression [ expression ]
4422 postfix-expression ( expression-list [opt] )
4423 simple-type-specifier ( expression-list [opt] )
4424 typename :: [opt] nested-name-specifier identifier
4425 ( expression-list [opt] )
4426 typename :: [opt] nested-name-specifier template [opt] template-id
4427 ( expression-list [opt] )
4428 postfix-expression . template [opt] id-expression
4429 postfix-expression -> template [opt] id-expression
4430 postfix-expression . pseudo-destructor-name
4431 postfix-expression -> pseudo-destructor-name
4432 postfix-expression ++
4433 postfix-expression --
4434 dynamic_cast < type-id > ( expression )
4435 static_cast < type-id > ( expression )
4436 reinterpret_cast < type-id > ( expression )
4437 const_cast < type-id > ( expression )
4438 typeid ( expression )
4444 ( type-id ) { initializer-list , [opt] }
4446 This extension is a GNU version of the C99 compound-literal
4447 construct. (The C99 grammar uses `type-name' instead of `type-id',
4448 but they are essentially the same concept.)
4450 If ADDRESS_P is true, the postfix expression is the operand of the
4451 `&' operator. CAST_P is true if this expression is the target of a
4454 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4455 class member access expressions [expr.ref].
4457 Returns a representation of the expression. */
4460 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4461 bool member_access_only_p,
4462 cp_id_kind * pidk_return)
4466 cp_id_kind idk = CP_ID_KIND_NONE;
4467 tree postfix_expression = NULL_TREE;
4468 bool is_member_access = false;
4470 /* Peek at the next token. */
4471 token = cp_lexer_peek_token (parser->lexer);
4472 /* Some of the productions are determined by keywords. */
4473 keyword = token->keyword;
4483 const char *saved_message;
4485 /* All of these can be handled in the same way from the point
4486 of view of parsing. Begin by consuming the token
4487 identifying the cast. */
4488 cp_lexer_consume_token (parser->lexer);
4490 /* New types cannot be defined in the cast. */
4491 saved_message = parser->type_definition_forbidden_message;
4492 parser->type_definition_forbidden_message
4493 = "types may not be defined in casts";
4495 /* Look for the opening `<'. */
4496 cp_parser_require (parser, CPP_LESS, "%<<%>");
4497 /* Parse the type to which we are casting. */
4498 type = cp_parser_type_id (parser);
4499 /* Look for the closing `>'. */
4500 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4501 /* Restore the old message. */
4502 parser->type_definition_forbidden_message = saved_message;
4504 /* And the expression which is being cast. */
4505 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4506 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4507 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4509 /* Only type conversions to integral or enumeration types
4510 can be used in constant-expressions. */
4511 if (!cast_valid_in_integral_constant_expression_p (type)
4512 && (cp_parser_non_integral_constant_expression
4514 "a cast to a type other than an integral or "
4515 "enumeration type")))
4516 return error_mark_node;
4522 = build_dynamic_cast (type, expression, tf_warning_or_error);
4526 = build_static_cast (type, expression, tf_warning_or_error);
4530 = build_reinterpret_cast (type, expression,
4531 tf_warning_or_error);
4535 = build_const_cast (type, expression, tf_warning_or_error);
4546 const char *saved_message;
4547 bool saved_in_type_id_in_expr_p;
4549 /* Consume the `typeid' token. */
4550 cp_lexer_consume_token (parser->lexer);
4551 /* Look for the `(' token. */
4552 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4553 /* Types cannot be defined in a `typeid' expression. */
4554 saved_message = parser->type_definition_forbidden_message;
4555 parser->type_definition_forbidden_message
4556 = "types may not be defined in a %<typeid%> expression";
4557 /* We can't be sure yet whether we're looking at a type-id or an
4559 cp_parser_parse_tentatively (parser);
4560 /* Try a type-id first. */
4561 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4562 parser->in_type_id_in_expr_p = true;
4563 type = cp_parser_type_id (parser);
4564 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4565 /* Look for the `)' token. Otherwise, we can't be sure that
4566 we're not looking at an expression: consider `typeid (int
4567 (3))', for example. */
4568 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4569 /* If all went well, simply lookup the type-id. */
4570 if (cp_parser_parse_definitely (parser))
4571 postfix_expression = get_typeid (type);
4572 /* Otherwise, fall back to the expression variant. */
4577 /* Look for an expression. */
4578 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4579 /* Compute its typeid. */
4580 postfix_expression = build_typeid (expression);
4581 /* Look for the `)' token. */
4582 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4584 /* Restore the saved message. */
4585 parser->type_definition_forbidden_message = saved_message;
4586 /* `typeid' may not appear in an integral constant expression. */
4587 if (cp_parser_non_integral_constant_expression(parser,
4588 "%<typeid%> operator"))
4589 return error_mark_node;
4596 /* The syntax permitted here is the same permitted for an
4597 elaborated-type-specifier. */
4598 type = cp_parser_elaborated_type_specifier (parser,
4599 /*is_friend=*/false,
4600 /*is_declaration=*/false);
4601 postfix_expression = cp_parser_functional_cast (parser, type);
4609 /* If the next thing is a simple-type-specifier, we may be
4610 looking at a functional cast. We could also be looking at
4611 an id-expression. So, we try the functional cast, and if
4612 that doesn't work we fall back to the primary-expression. */
4613 cp_parser_parse_tentatively (parser);
4614 /* Look for the simple-type-specifier. */
4615 type = cp_parser_simple_type_specifier (parser,
4616 /*decl_specs=*/NULL,
4617 CP_PARSER_FLAGS_NONE);
4618 /* Parse the cast itself. */
4619 if (!cp_parser_error_occurred (parser))
4621 = cp_parser_functional_cast (parser, type);
4622 /* If that worked, we're done. */
4623 if (cp_parser_parse_definitely (parser))
4626 /* If the functional-cast didn't work out, try a
4627 compound-literal. */
4628 if (cp_parser_allow_gnu_extensions_p (parser)
4629 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4631 VEC(constructor_elt,gc) *initializer_list = NULL;
4632 bool saved_in_type_id_in_expr_p;
4634 cp_parser_parse_tentatively (parser);
4635 /* Consume the `('. */
4636 cp_lexer_consume_token (parser->lexer);
4637 /* Parse the type. */
4638 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4639 parser->in_type_id_in_expr_p = true;
4640 type = cp_parser_type_id (parser);
4641 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4642 /* Look for the `)'. */
4643 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4644 /* Look for the `{'. */
4645 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4646 /* If things aren't going well, there's no need to
4648 if (!cp_parser_error_occurred (parser))
4650 bool non_constant_p;
4651 /* Parse the initializer-list. */
4653 = cp_parser_initializer_list (parser, &non_constant_p);
4654 /* Allow a trailing `,'. */
4655 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4656 cp_lexer_consume_token (parser->lexer);
4657 /* Look for the final `}'. */
4658 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4660 /* If that worked, we're definitely looking at a
4661 compound-literal expression. */
4662 if (cp_parser_parse_definitely (parser))
4664 /* Warn the user that a compound literal is not
4665 allowed in standard C++. */
4666 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4667 /* For simplicity, we disallow compound literals in
4668 constant-expressions. We could
4669 allow compound literals of integer type, whose
4670 initializer was a constant, in constant
4671 expressions. Permitting that usage, as a further
4672 extension, would not change the meaning of any
4673 currently accepted programs. (Of course, as
4674 compound literals are not part of ISO C++, the
4675 standard has nothing to say.) */
4676 if (cp_parser_non_integral_constant_expression
4677 (parser, "non-constant compound literals"))
4679 postfix_expression = error_mark_node;
4682 /* Form the representation of the compound-literal. */
4684 = (finish_compound_literal
4685 (type, build_constructor (init_list_type_node,
4686 initializer_list)));
4691 /* It must be a primary-expression. */
4693 = cp_parser_primary_expression (parser, address_p, cast_p,
4694 /*template_arg_p=*/false,
4700 /* Keep looping until the postfix-expression is complete. */
4703 if (idk == CP_ID_KIND_UNQUALIFIED
4704 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4705 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4706 /* It is not a Koenig lookup function call. */
4708 = unqualified_name_lookup_error (postfix_expression);
4710 /* Peek at the next token. */
4711 token = cp_lexer_peek_token (parser->lexer);
4713 switch (token->type)
4715 case CPP_OPEN_SQUARE:
4717 = cp_parser_postfix_open_square_expression (parser,
4720 idk = CP_ID_KIND_NONE;
4721 is_member_access = false;
4724 case CPP_OPEN_PAREN:
4725 /* postfix-expression ( expression-list [opt] ) */
4728 bool is_builtin_constant_p;
4729 bool saved_integral_constant_expression_p = false;
4730 bool saved_non_integral_constant_expression_p = false;
4733 is_member_access = false;
4735 is_builtin_constant_p
4736 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4737 if (is_builtin_constant_p)
4739 /* The whole point of __builtin_constant_p is to allow
4740 non-constant expressions to appear as arguments. */
4741 saved_integral_constant_expression_p
4742 = parser->integral_constant_expression_p;
4743 saved_non_integral_constant_expression_p
4744 = parser->non_integral_constant_expression_p;
4745 parser->integral_constant_expression_p = false;
4747 args = (cp_parser_parenthesized_expression_list
4748 (parser, /*is_attribute_list=*/false,
4749 /*cast_p=*/false, /*allow_expansion_p=*/true,
4750 /*non_constant_p=*/NULL));
4751 if (is_builtin_constant_p)
4753 parser->integral_constant_expression_p
4754 = saved_integral_constant_expression_p;
4755 parser->non_integral_constant_expression_p
4756 = saved_non_integral_constant_expression_p;
4761 postfix_expression = error_mark_node;
4765 /* Function calls are not permitted in
4766 constant-expressions. */
4767 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4768 && cp_parser_non_integral_constant_expression (parser,
4771 postfix_expression = error_mark_node;
4772 release_tree_vector (args);
4777 if (idk == CP_ID_KIND_UNQUALIFIED
4778 || idk == CP_ID_KIND_TEMPLATE_ID)
4780 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4782 if (!VEC_empty (tree, args))
4785 if (!any_type_dependent_arguments_p (args))
4787 = perform_koenig_lookup (postfix_expression, args);
4791 = unqualified_fn_lookup_error (postfix_expression);
4793 /* We do not perform argument-dependent lookup if
4794 normal lookup finds a non-function, in accordance
4795 with the expected resolution of DR 218. */
4796 else if (!VEC_empty (tree, args)
4797 && is_overloaded_fn (postfix_expression))
4799 tree fn = get_first_fn (postfix_expression);
4801 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4802 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4804 /* Only do argument dependent lookup if regular
4805 lookup does not find a set of member functions.
4806 [basic.lookup.koenig]/2a */
4807 if (!DECL_FUNCTION_MEMBER_P (fn))
4810 if (!any_type_dependent_arguments_p (args))
4812 = perform_koenig_lookup (postfix_expression, args);
4817 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4819 tree instance = TREE_OPERAND (postfix_expression, 0);
4820 tree fn = TREE_OPERAND (postfix_expression, 1);
4822 if (processing_template_decl
4823 && (type_dependent_expression_p (instance)
4824 || (!BASELINK_P (fn)
4825 && TREE_CODE (fn) != FIELD_DECL)
4826 || type_dependent_expression_p (fn)
4827 || any_type_dependent_arguments_p (args)))
4830 = build_nt_call_vec (postfix_expression, args);
4831 release_tree_vector (args);
4835 if (BASELINK_P (fn))
4838 = (build_new_method_call
4839 (instance, fn, &args, NULL_TREE,
4840 (idk == CP_ID_KIND_QUALIFIED
4841 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4843 tf_warning_or_error));
4847 = finish_call_expr (postfix_expression, &args,
4848 /*disallow_virtual=*/false,
4850 tf_warning_or_error);
4852 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4853 || TREE_CODE (postfix_expression) == MEMBER_REF
4854 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4855 postfix_expression = (build_offset_ref_call_from_tree
4856 (postfix_expression, &args));
4857 else if (idk == CP_ID_KIND_QUALIFIED)
4858 /* A call to a static class member, or a namespace-scope
4861 = finish_call_expr (postfix_expression, &args,
4862 /*disallow_virtual=*/true,
4864 tf_warning_or_error);
4866 /* All other function calls. */
4868 = finish_call_expr (postfix_expression, &args,
4869 /*disallow_virtual=*/false,
4871 tf_warning_or_error);
4873 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4874 idk = CP_ID_KIND_NONE;
4876 release_tree_vector (args);
4882 /* postfix-expression . template [opt] id-expression
4883 postfix-expression . pseudo-destructor-name
4884 postfix-expression -> template [opt] id-expression
4885 postfix-expression -> pseudo-destructor-name */
4887 /* Consume the `.' or `->' operator. */
4888 cp_lexer_consume_token (parser->lexer);
4891 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4896 is_member_access = true;
4900 /* postfix-expression ++ */
4901 /* Consume the `++' token. */
4902 cp_lexer_consume_token (parser->lexer);
4903 /* Generate a representation for the complete expression. */
4905 = finish_increment_expr (postfix_expression,
4906 POSTINCREMENT_EXPR);
4907 /* Increments may not appear in constant-expressions. */
4908 if (cp_parser_non_integral_constant_expression (parser,
4910 postfix_expression = error_mark_node;
4911 idk = CP_ID_KIND_NONE;
4912 is_member_access = false;
4915 case CPP_MINUS_MINUS:
4916 /* postfix-expression -- */
4917 /* Consume the `--' token. */
4918 cp_lexer_consume_token (parser->lexer);
4919 /* Generate a representation for the complete expression. */
4921 = finish_increment_expr (postfix_expression,
4922 POSTDECREMENT_EXPR);
4923 /* Decrements may not appear in constant-expressions. */
4924 if (cp_parser_non_integral_constant_expression (parser,
4926 postfix_expression = error_mark_node;
4927 idk = CP_ID_KIND_NONE;
4928 is_member_access = false;
4932 if (pidk_return != NULL)
4933 * pidk_return = idk;
4934 if (member_access_only_p)
4935 return is_member_access? postfix_expression : error_mark_node;
4937 return postfix_expression;
4941 /* We should never get here. */
4943 return error_mark_node;
4946 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4947 by cp_parser_builtin_offsetof. We're looking for
4949 postfix-expression [ expression ]
4951 FOR_OFFSETOF is set if we're being called in that context, which
4952 changes how we deal with integer constant expressions. */
4955 cp_parser_postfix_open_square_expression (cp_parser *parser,
4956 tree postfix_expression,
4961 /* Consume the `[' token. */
4962 cp_lexer_consume_token (parser->lexer);
4964 /* Parse the index expression. */
4965 /* ??? For offsetof, there is a question of what to allow here. If
4966 offsetof is not being used in an integral constant expression context,
4967 then we *could* get the right answer by computing the value at runtime.
4968 If we are in an integral constant expression context, then we might
4969 could accept any constant expression; hard to say without analysis.
4970 Rather than open the barn door too wide right away, allow only integer
4971 constant expressions here. */
4973 index = cp_parser_constant_expression (parser, false, NULL);
4975 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4977 /* Look for the closing `]'. */
4978 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4980 /* Build the ARRAY_REF. */
4981 postfix_expression = grok_array_decl (postfix_expression, index);
4983 /* When not doing offsetof, array references are not permitted in
4984 constant-expressions. */
4986 && (cp_parser_non_integral_constant_expression
4987 (parser, "an array reference")))
4988 postfix_expression = error_mark_node;
4990 return postfix_expression;
4993 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4994 by cp_parser_builtin_offsetof. We're looking for
4996 postfix-expression . template [opt] id-expression
4997 postfix-expression . pseudo-destructor-name
4998 postfix-expression -> template [opt] id-expression
4999 postfix-expression -> pseudo-destructor-name
5001 FOR_OFFSETOF is set if we're being called in that context. That sorta
5002 limits what of the above we'll actually accept, but nevermind.
5003 TOKEN_TYPE is the "." or "->" token, which will already have been
5004 removed from the stream. */
5007 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5008 enum cpp_ttype token_type,
5009 tree postfix_expression,
5010 bool for_offsetof, cp_id_kind *idk,
5011 location_t location)
5015 bool pseudo_destructor_p;
5016 tree scope = NULL_TREE;
5018 /* If this is a `->' operator, dereference the pointer. */
5019 if (token_type == CPP_DEREF)
5020 postfix_expression = build_x_arrow (postfix_expression);
5021 /* Check to see whether or not the expression is type-dependent. */
5022 dependent_p = type_dependent_expression_p (postfix_expression);
5023 /* The identifier following the `->' or `.' is not qualified. */
5024 parser->scope = NULL_TREE;
5025 parser->qualifying_scope = NULL_TREE;
5026 parser->object_scope = NULL_TREE;
5027 *idk = CP_ID_KIND_NONE;
5029 /* Enter the scope corresponding to the type of the object
5030 given by the POSTFIX_EXPRESSION. */
5031 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5033 scope = TREE_TYPE (postfix_expression);
5034 /* According to the standard, no expression should ever have
5035 reference type. Unfortunately, we do not currently match
5036 the standard in this respect in that our internal representation
5037 of an expression may have reference type even when the standard
5038 says it does not. Therefore, we have to manually obtain the
5039 underlying type here. */
5040 scope = non_reference (scope);
5041 /* The type of the POSTFIX_EXPRESSION must be complete. */
5042 if (scope == unknown_type_node)
5044 error_at (location, "%qE does not have class type",
5045 postfix_expression);
5049 scope = complete_type_or_else (scope, NULL_TREE);
5050 /* Let the name lookup machinery know that we are processing a
5051 class member access expression. */
5052 parser->context->object_type = scope;
5053 /* If something went wrong, we want to be able to discern that case,
5054 as opposed to the case where there was no SCOPE due to the type
5055 of expression being dependent. */
5057 scope = error_mark_node;
5058 /* If the SCOPE was erroneous, make the various semantic analysis
5059 functions exit quickly -- and without issuing additional error
5061 if (scope == error_mark_node)
5062 postfix_expression = error_mark_node;
5065 /* Assume this expression is not a pseudo-destructor access. */
5066 pseudo_destructor_p = false;
5068 /* If the SCOPE is a scalar type, then, if this is a valid program,
5069 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5070 is type dependent, it can be pseudo-destructor-name or something else.
5071 Try to parse it as pseudo-destructor-name first. */
5072 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5077 cp_parser_parse_tentatively (parser);
5078 /* Parse the pseudo-destructor-name. */
5080 cp_parser_pseudo_destructor_name (parser, &s, &type);
5082 && (cp_parser_error_occurred (parser)
5083 || TREE_CODE (type) != TYPE_DECL
5084 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5085 cp_parser_abort_tentative_parse (parser);
5086 else if (cp_parser_parse_definitely (parser))
5088 pseudo_destructor_p = true;
5090 = finish_pseudo_destructor_expr (postfix_expression,
5091 s, TREE_TYPE (type));
5095 if (!pseudo_destructor_p)
5097 /* If the SCOPE is not a scalar type, we are looking at an
5098 ordinary class member access expression, rather than a
5099 pseudo-destructor-name. */
5101 cp_token *token = cp_lexer_peek_token (parser->lexer);
5102 /* Parse the id-expression. */
5103 name = (cp_parser_id_expression
5105 cp_parser_optional_template_keyword (parser),
5106 /*check_dependency_p=*/true,
5108 /*declarator_p=*/false,
5109 /*optional_p=*/false));
5110 /* In general, build a SCOPE_REF if the member name is qualified.
5111 However, if the name was not dependent and has already been
5112 resolved; there is no need to build the SCOPE_REF. For example;
5114 struct X { void f(); };
5115 template <typename T> void f(T* t) { t->X::f(); }
5117 Even though "t" is dependent, "X::f" is not and has been resolved
5118 to a BASELINK; there is no need to include scope information. */
5120 /* But we do need to remember that there was an explicit scope for
5121 virtual function calls. */
5123 *idk = CP_ID_KIND_QUALIFIED;
5125 /* If the name is a template-id that names a type, we will get a
5126 TYPE_DECL here. That is invalid code. */
5127 if (TREE_CODE (name) == TYPE_DECL)
5129 error_at (token->location, "invalid use of %qD", name);
5130 postfix_expression = error_mark_node;
5134 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5136 name = build_qualified_name (/*type=*/NULL_TREE,
5140 parser->scope = NULL_TREE;
5141 parser->qualifying_scope = NULL_TREE;
5142 parser->object_scope = NULL_TREE;
5144 if (scope && name && BASELINK_P (name))
5145 adjust_result_of_qualified_name_lookup
5146 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5148 = finish_class_member_access_expr (postfix_expression, name,
5150 tf_warning_or_error);
5154 /* We no longer need to look up names in the scope of the object on
5155 the left-hand side of the `.' or `->' operator. */
5156 parser->context->object_type = NULL_TREE;
5158 /* Outside of offsetof, these operators may not appear in
5159 constant-expressions. */
5161 && (cp_parser_non_integral_constant_expression
5162 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5163 postfix_expression = error_mark_node;
5165 return postfix_expression;
5168 /* Parse a parenthesized expression-list.
5171 assignment-expression
5172 expression-list, assignment-expression
5177 identifier, expression-list
5179 CAST_P is true if this expression is the target of a cast.
5181 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5184 Returns a vector of trees. Each element is a representation of an
5185 assignment-expression. NULL is returned if the ( and or ) are
5186 missing. An empty, but allocated, vector is returned on no
5187 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5188 if this is really an attribute list being parsed. If
5189 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5190 not all of the expressions in the list were constant. */
5192 static VEC(tree,gc) *
5193 cp_parser_parenthesized_expression_list (cp_parser* parser,
5194 bool is_attribute_list,
5196 bool allow_expansion_p,
5197 bool *non_constant_p)
5199 VEC(tree,gc) *expression_list;
5200 bool fold_expr_p = is_attribute_list;
5201 tree identifier = NULL_TREE;
5202 bool saved_greater_than_is_operator_p;
5204 /* Assume all the expressions will be constant. */
5206 *non_constant_p = false;
5208 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5211 expression_list = make_tree_vector ();
5213 /* Within a parenthesized expression, a `>' token is always
5214 the greater-than operator. */
5215 saved_greater_than_is_operator_p
5216 = parser->greater_than_is_operator_p;
5217 parser->greater_than_is_operator_p = true;
5219 /* Consume expressions until there are no more. */
5220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5225 /* At the beginning of attribute lists, check to see if the
5226 next token is an identifier. */
5227 if (is_attribute_list
5228 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5232 /* Consume the identifier. */
5233 token = cp_lexer_consume_token (parser->lexer);
5234 /* Save the identifier. */
5235 identifier = token->u.value;
5239 bool expr_non_constant_p;
5241 /* Parse the next assignment-expression. */
5242 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5244 /* A braced-init-list. */
5245 maybe_warn_cpp0x ("extended initializer lists");
5246 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5247 if (non_constant_p && expr_non_constant_p)
5248 *non_constant_p = true;
5250 else if (non_constant_p)
5252 expr = (cp_parser_constant_expression
5253 (parser, /*allow_non_constant_p=*/true,
5254 &expr_non_constant_p));
5255 if (expr_non_constant_p)
5256 *non_constant_p = true;
5259 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5262 expr = fold_non_dependent_expr (expr);
5264 /* If we have an ellipsis, then this is an expression
5266 if (allow_expansion_p
5267 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5269 /* Consume the `...'. */
5270 cp_lexer_consume_token (parser->lexer);
5272 /* Build the argument pack. */
5273 expr = make_pack_expansion (expr);
5276 /* Add it to the list. We add error_mark_node
5277 expressions to the list, so that we can still tell if
5278 the correct form for a parenthesized expression-list
5279 is found. That gives better errors. */
5280 VEC_safe_push (tree, gc, expression_list, expr);
5282 if (expr == error_mark_node)
5286 /* After the first item, attribute lists look the same as
5287 expression lists. */
5288 is_attribute_list = false;
5291 /* If the next token isn't a `,', then we are done. */
5292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5295 /* Otherwise, consume the `,' and keep going. */
5296 cp_lexer_consume_token (parser->lexer);
5299 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5304 /* We try and resync to an unnested comma, as that will give the
5305 user better diagnostics. */
5306 ending = cp_parser_skip_to_closing_parenthesis (parser,
5307 /*recovering=*/true,
5309 /*consume_paren=*/true);
5314 parser->greater_than_is_operator_p
5315 = saved_greater_than_is_operator_p;
5320 parser->greater_than_is_operator_p
5321 = saved_greater_than_is_operator_p;
5324 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5326 return expression_list;
5329 /* Parse a pseudo-destructor-name.
5331 pseudo-destructor-name:
5332 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5333 :: [opt] nested-name-specifier template template-id :: ~ type-name
5334 :: [opt] nested-name-specifier [opt] ~ type-name
5336 If either of the first two productions is used, sets *SCOPE to the
5337 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5338 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5339 or ERROR_MARK_NODE if the parse fails. */
5342 cp_parser_pseudo_destructor_name (cp_parser* parser,
5346 bool nested_name_specifier_p;
5348 /* Assume that things will not work out. */
5349 *type = error_mark_node;
5351 /* Look for the optional `::' operator. */
5352 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5353 /* Look for the optional nested-name-specifier. */
5354 nested_name_specifier_p
5355 = (cp_parser_nested_name_specifier_opt (parser,
5356 /*typename_keyword_p=*/false,
5357 /*check_dependency_p=*/true,
5359 /*is_declaration=*/false)
5361 /* Now, if we saw a nested-name-specifier, we might be doing the
5362 second production. */
5363 if (nested_name_specifier_p
5364 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5366 /* Consume the `template' keyword. */
5367 cp_lexer_consume_token (parser->lexer);
5368 /* Parse the template-id. */
5369 cp_parser_template_id (parser,
5370 /*template_keyword_p=*/true,
5371 /*check_dependency_p=*/false,
5372 /*is_declaration=*/true);
5373 /* Look for the `::' token. */
5374 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5376 /* If the next token is not a `~', then there might be some
5377 additional qualification. */
5378 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5380 /* At this point, we're looking for "type-name :: ~". The type-name
5381 must not be a class-name, since this is a pseudo-destructor. So,
5382 it must be either an enum-name, or a typedef-name -- both of which
5383 are just identifiers. So, we peek ahead to check that the "::"
5384 and "~" tokens are present; if they are not, then we can avoid
5385 calling type_name. */
5386 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5387 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5388 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5390 cp_parser_error (parser, "non-scalar type");
5394 /* Look for the type-name. */
5395 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5396 if (*scope == error_mark_node)
5399 /* Look for the `::' token. */
5400 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5405 /* Look for the `~'. */
5406 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5407 /* Look for the type-name again. We are not responsible for
5408 checking that it matches the first type-name. */
5409 *type = cp_parser_nonclass_name (parser);
5412 /* Parse a unary-expression.
5418 unary-operator cast-expression
5419 sizeof unary-expression
5427 __extension__ cast-expression
5428 __alignof__ unary-expression
5429 __alignof__ ( type-id )
5430 __real__ cast-expression
5431 __imag__ cast-expression
5434 ADDRESS_P is true iff the unary-expression is appearing as the
5435 operand of the `&' operator. CAST_P is true if this expression is
5436 the target of a cast.
5438 Returns a representation of the expression. */
5441 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5445 enum tree_code unary_operator;
5447 /* Peek at the next token. */
5448 token = cp_lexer_peek_token (parser->lexer);
5449 /* Some keywords give away the kind of expression. */
5450 if (token->type == CPP_KEYWORD)
5452 enum rid keyword = token->keyword;
5462 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5463 /* Consume the token. */
5464 cp_lexer_consume_token (parser->lexer);
5465 /* Parse the operand. */
5466 operand = cp_parser_sizeof_operand (parser, keyword);
5468 if (TYPE_P (operand))
5469 return cxx_sizeof_or_alignof_type (operand, op, true);
5471 return cxx_sizeof_or_alignof_expr (operand, op, true);
5475 return cp_parser_new_expression (parser);
5478 return cp_parser_delete_expression (parser);
5482 /* The saved value of the PEDANTIC flag. */
5486 /* Save away the PEDANTIC flag. */
5487 cp_parser_extension_opt (parser, &saved_pedantic);
5488 /* Parse the cast-expression. */
5489 expr = cp_parser_simple_cast_expression (parser);
5490 /* Restore the PEDANTIC flag. */
5491 pedantic = saved_pedantic;
5501 /* Consume the `__real__' or `__imag__' token. */
5502 cp_lexer_consume_token (parser->lexer);
5503 /* Parse the cast-expression. */
5504 expression = cp_parser_simple_cast_expression (parser);
5505 /* Create the complete representation. */
5506 return build_x_unary_op ((keyword == RID_REALPART
5507 ? REALPART_EXPR : IMAGPART_EXPR),
5509 tf_warning_or_error);
5518 /* Look for the `:: new' and `:: delete', which also signal the
5519 beginning of a new-expression, or delete-expression,
5520 respectively. If the next token is `::', then it might be one of
5522 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5526 /* See if the token after the `::' is one of the keywords in
5527 which we're interested. */
5528 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5529 /* If it's `new', we have a new-expression. */
5530 if (keyword == RID_NEW)
5531 return cp_parser_new_expression (parser);
5532 /* Similarly, for `delete'. */
5533 else if (keyword == RID_DELETE)
5534 return cp_parser_delete_expression (parser);
5537 /* Look for a unary operator. */
5538 unary_operator = cp_parser_unary_operator (token);
5539 /* The `++' and `--' operators can be handled similarly, even though
5540 they are not technically unary-operators in the grammar. */
5541 if (unary_operator == ERROR_MARK)
5543 if (token->type == CPP_PLUS_PLUS)
5544 unary_operator = PREINCREMENT_EXPR;
5545 else if (token->type == CPP_MINUS_MINUS)
5546 unary_operator = PREDECREMENT_EXPR;
5547 /* Handle the GNU address-of-label extension. */
5548 else if (cp_parser_allow_gnu_extensions_p (parser)
5549 && token->type == CPP_AND_AND)
5553 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5555 /* Consume the '&&' token. */
5556 cp_lexer_consume_token (parser->lexer);
5557 /* Look for the identifier. */
5558 identifier = cp_parser_identifier (parser);
5559 /* Create an expression representing the address. */
5560 expression = finish_label_address_expr (identifier, loc);
5561 if (cp_parser_non_integral_constant_expression (parser,
5562 "the address of a label"))
5563 expression = error_mark_node;
5567 if (unary_operator != ERROR_MARK)
5569 tree cast_expression;
5570 tree expression = error_mark_node;
5571 const char *non_constant_p = NULL;
5573 /* Consume the operator token. */
5574 token = cp_lexer_consume_token (parser->lexer);
5575 /* Parse the cast-expression. */
5577 = cp_parser_cast_expression (parser,
5578 unary_operator == ADDR_EXPR,
5579 /*cast_p=*/false, pidk);
5580 /* Now, build an appropriate representation. */
5581 switch (unary_operator)
5584 non_constant_p = "%<*%>";
5585 expression = build_x_indirect_ref (cast_expression, "unary *",
5586 tf_warning_or_error);
5590 non_constant_p = "%<&%>";
5593 expression = build_x_unary_op (unary_operator, cast_expression,
5594 tf_warning_or_error);
5597 case PREINCREMENT_EXPR:
5598 case PREDECREMENT_EXPR:
5599 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5600 ? "%<++%>" : "%<--%>");
5602 case UNARY_PLUS_EXPR:
5604 case TRUTH_NOT_EXPR:
5605 expression = finish_unary_op_expr (unary_operator, cast_expression);
5613 && cp_parser_non_integral_constant_expression (parser,
5615 expression = error_mark_node;
5620 return cp_parser_postfix_expression (parser, address_p, cast_p,
5621 /*member_access_only_p=*/false,
5625 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5626 unary-operator, the corresponding tree code is returned. */
5628 static enum tree_code
5629 cp_parser_unary_operator (cp_token* token)
5631 switch (token->type)
5634 return INDIRECT_REF;
5640 return UNARY_PLUS_EXPR;
5646 return TRUTH_NOT_EXPR;
5649 return BIT_NOT_EXPR;
5656 /* Parse a new-expression.
5659 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5660 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5662 Returns a representation of the expression. */
5665 cp_parser_new_expression (cp_parser* parser)
5667 bool global_scope_p;
5668 VEC(tree,gc) *placement;
5670 VEC(tree,gc) *initializer;
5674 /* Look for the optional `::' operator. */
5676 = (cp_parser_global_scope_opt (parser,
5677 /*current_scope_valid_p=*/false)
5679 /* Look for the `new' operator. */
5680 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5681 /* There's no easy way to tell a new-placement from the
5682 `( type-id )' construct. */
5683 cp_parser_parse_tentatively (parser);
5684 /* Look for a new-placement. */
5685 placement = cp_parser_new_placement (parser);
5686 /* If that didn't work out, there's no new-placement. */
5687 if (!cp_parser_parse_definitely (parser))
5689 if (placement != NULL)
5690 release_tree_vector (placement);
5694 /* If the next token is a `(', then we have a parenthesized
5696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5699 /* Consume the `('. */
5700 cp_lexer_consume_token (parser->lexer);
5701 /* Parse the type-id. */
5702 type = cp_parser_type_id (parser);
5703 /* Look for the closing `)'. */
5704 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5705 token = cp_lexer_peek_token (parser->lexer);
5706 /* There should not be a direct-new-declarator in this production,
5707 but GCC used to allowed this, so we check and emit a sensible error
5708 message for this case. */
5709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5711 error_at (token->location,
5712 "array bound forbidden after parenthesized type-id");
5713 inform (token->location,
5714 "try removing the parentheses around the type-id");
5715 cp_parser_direct_new_declarator (parser);
5719 /* Otherwise, there must be a new-type-id. */
5721 type = cp_parser_new_type_id (parser, &nelts);
5723 /* If the next token is a `(' or '{', then we have a new-initializer. */
5724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5725 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5726 initializer = cp_parser_new_initializer (parser);
5730 /* A new-expression may not appear in an integral constant
5732 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5733 ret = error_mark_node;
5736 /* Create a representation of the new-expression. */
5737 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5738 tf_warning_or_error);
5741 if (placement != NULL)
5742 release_tree_vector (placement);
5743 if (initializer != NULL)
5744 release_tree_vector (initializer);
5749 /* Parse a new-placement.
5754 Returns the same representation as for an expression-list. */
5756 static VEC(tree,gc) *
5757 cp_parser_new_placement (cp_parser* parser)
5759 VEC(tree,gc) *expression_list;
5761 /* Parse the expression-list. */
5762 expression_list = (cp_parser_parenthesized_expression_list
5763 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5764 /*non_constant_p=*/NULL));
5766 return expression_list;
5769 /* Parse a new-type-id.
5772 type-specifier-seq new-declarator [opt]
5774 Returns the TYPE allocated. If the new-type-id indicates an array
5775 type, *NELTS is set to the number of elements in the last array
5776 bound; the TYPE will not include the last array bound. */
5779 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5781 cp_decl_specifier_seq type_specifier_seq;
5782 cp_declarator *new_declarator;
5783 cp_declarator *declarator;
5784 cp_declarator *outer_declarator;
5785 const char *saved_message;
5788 /* The type-specifier sequence must not contain type definitions.
5789 (It cannot contain declarations of new types either, but if they
5790 are not definitions we will catch that because they are not
5792 saved_message = parser->type_definition_forbidden_message;
5793 parser->type_definition_forbidden_message
5794 = "types may not be defined in a new-type-id";
5795 /* Parse the type-specifier-seq. */
5796 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5797 &type_specifier_seq);
5798 /* Restore the old message. */
5799 parser->type_definition_forbidden_message = saved_message;
5800 /* Parse the new-declarator. */
5801 new_declarator = cp_parser_new_declarator_opt (parser);
5803 /* Determine the number of elements in the last array dimension, if
5806 /* Skip down to the last array dimension. */
5807 declarator = new_declarator;
5808 outer_declarator = NULL;
5809 while (declarator && (declarator->kind == cdk_pointer
5810 || declarator->kind == cdk_ptrmem))
5812 outer_declarator = declarator;
5813 declarator = declarator->declarator;
5816 && declarator->kind == cdk_array
5817 && declarator->declarator
5818 && declarator->declarator->kind == cdk_array)
5820 outer_declarator = declarator;
5821 declarator = declarator->declarator;
5824 if (declarator && declarator->kind == cdk_array)
5826 *nelts = declarator->u.array.bounds;
5827 if (*nelts == error_mark_node)
5828 *nelts = integer_one_node;
5830 if (outer_declarator)
5831 outer_declarator->declarator = declarator->declarator;
5833 new_declarator = NULL;
5836 type = groktypename (&type_specifier_seq, new_declarator, false);
5840 /* Parse an (optional) new-declarator.
5843 ptr-operator new-declarator [opt]
5844 direct-new-declarator
5846 Returns the declarator. */
5848 static cp_declarator *
5849 cp_parser_new_declarator_opt (cp_parser* parser)
5851 enum tree_code code;
5853 cp_cv_quals cv_quals;
5855 /* We don't know if there's a ptr-operator next, or not. */
5856 cp_parser_parse_tentatively (parser);
5857 /* Look for a ptr-operator. */
5858 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5859 /* If that worked, look for more new-declarators. */
5860 if (cp_parser_parse_definitely (parser))
5862 cp_declarator *declarator;
5864 /* Parse another optional declarator. */
5865 declarator = cp_parser_new_declarator_opt (parser);
5867 return cp_parser_make_indirect_declarator
5868 (code, type, cv_quals, declarator);
5871 /* If the next token is a `[', there is a direct-new-declarator. */
5872 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5873 return cp_parser_direct_new_declarator (parser);
5878 /* Parse a direct-new-declarator.
5880 direct-new-declarator:
5882 direct-new-declarator [constant-expression]
5886 static cp_declarator *
5887 cp_parser_direct_new_declarator (cp_parser* parser)
5889 cp_declarator *declarator = NULL;
5895 /* Look for the opening `['. */
5896 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5897 /* The first expression is not required to be constant. */
5900 cp_token *token = cp_lexer_peek_token (parser->lexer);
5901 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5902 /* The standard requires that the expression have integral
5903 type. DR 74 adds enumeration types. We believe that the
5904 real intent is that these expressions be handled like the
5905 expression in a `switch' condition, which also allows
5906 classes with a single conversion to integral or
5907 enumeration type. */
5908 if (!processing_template_decl)
5911 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5916 error_at (token->location,
5917 "expression in new-declarator must have integral "
5918 "or enumeration type");
5919 expression = error_mark_node;
5923 /* But all the other expressions must be. */
5926 = cp_parser_constant_expression (parser,
5927 /*allow_non_constant=*/false,
5929 /* Look for the closing `]'. */
5930 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5932 /* Add this bound to the declarator. */
5933 declarator = make_array_declarator (declarator, expression);
5935 /* If the next token is not a `[', then there are no more
5937 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5944 /* Parse a new-initializer.
5947 ( expression-list [opt] )
5950 Returns a representation of the expression-list. */
5952 static VEC(tree,gc) *
5953 cp_parser_new_initializer (cp_parser* parser)
5955 VEC(tree,gc) *expression_list;
5957 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5960 bool expr_non_constant_p;
5961 maybe_warn_cpp0x ("extended initializer lists");
5962 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5963 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5964 expression_list = make_tree_vector_single (t);
5967 expression_list = (cp_parser_parenthesized_expression_list
5968 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5969 /*non_constant_p=*/NULL));
5971 return expression_list;
5974 /* Parse a delete-expression.
5977 :: [opt] delete cast-expression
5978 :: [opt] delete [ ] cast-expression
5980 Returns a representation of the expression. */
5983 cp_parser_delete_expression (cp_parser* parser)
5985 bool global_scope_p;
5989 /* Look for the optional `::' operator. */
5991 = (cp_parser_global_scope_opt (parser,
5992 /*current_scope_valid_p=*/false)
5994 /* Look for the `delete' keyword. */
5995 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5996 /* See if the array syntax is in use. */
5997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5999 /* Consume the `[' token. */
6000 cp_lexer_consume_token (parser->lexer);
6001 /* Look for the `]' token. */
6002 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6003 /* Remember that this is the `[]' construct. */
6009 /* Parse the cast-expression. */
6010 expression = cp_parser_simple_cast_expression (parser);
6012 /* A delete-expression may not appear in an integral constant
6014 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6015 return error_mark_node;
6017 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6020 /* Returns true if TOKEN may start a cast-expression and false
6024 cp_parser_token_starts_cast_expression (cp_token *token)
6026 switch (token->type)
6032 case CPP_CLOSE_SQUARE:
6033 case CPP_CLOSE_PAREN:
6034 case CPP_CLOSE_BRACE:
6038 case CPP_DEREF_STAR:
6046 case CPP_GREATER_EQ:
6066 /* '[' may start a primary-expression in obj-c++. */
6067 case CPP_OPEN_SQUARE:
6068 return c_dialect_objc ();
6075 /* Parse a cast-expression.
6079 ( type-id ) cast-expression
6081 ADDRESS_P is true iff the unary-expression is appearing as the
6082 operand of the `&' operator. CAST_P is true if this expression is
6083 the target of a cast.
6085 Returns a representation of the expression. */
6088 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6091 /* If it's a `(', then we might be looking at a cast. */
6092 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6094 tree type = NULL_TREE;
6095 tree expr = NULL_TREE;
6096 bool compound_literal_p;
6097 const char *saved_message;
6099 /* There's no way to know yet whether or not this is a cast.
6100 For example, `(int (3))' is a unary-expression, while `(int)
6101 3' is a cast. So, we resort to parsing tentatively. */
6102 cp_parser_parse_tentatively (parser);
6103 /* Types may not be defined in a cast. */
6104 saved_message = parser->type_definition_forbidden_message;
6105 parser->type_definition_forbidden_message
6106 = "types may not be defined in casts";
6107 /* Consume the `('. */
6108 cp_lexer_consume_token (parser->lexer);
6109 /* A very tricky bit is that `(struct S) { 3 }' is a
6110 compound-literal (which we permit in C++ as an extension).
6111 But, that construct is not a cast-expression -- it is a
6112 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6113 is legal; if the compound-literal were a cast-expression,
6114 you'd need an extra set of parentheses.) But, if we parse
6115 the type-id, and it happens to be a class-specifier, then we
6116 will commit to the parse at that point, because we cannot
6117 undo the action that is done when creating a new class. So,
6118 then we cannot back up and do a postfix-expression.
6120 Therefore, we scan ahead to the closing `)', and check to see
6121 if the token after the `)' is a `{'. If so, we are not
6122 looking at a cast-expression.
6124 Save tokens so that we can put them back. */
6125 cp_lexer_save_tokens (parser->lexer);
6126 /* Skip tokens until the next token is a closing parenthesis.
6127 If we find the closing `)', and the next token is a `{', then
6128 we are looking at a compound-literal. */
6130 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6131 /*consume_paren=*/true)
6132 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6133 /* Roll back the tokens we skipped. */
6134 cp_lexer_rollback_tokens (parser->lexer);
6135 /* If we were looking at a compound-literal, simulate an error
6136 so that the call to cp_parser_parse_definitely below will
6138 if (compound_literal_p)
6139 cp_parser_simulate_error (parser);
6142 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6143 parser->in_type_id_in_expr_p = true;
6144 /* Look for the type-id. */
6145 type = cp_parser_type_id (parser);
6146 /* Look for the closing `)'. */
6147 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6148 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6151 /* Restore the saved message. */
6152 parser->type_definition_forbidden_message = saved_message;
6154 /* At this point this can only be either a cast or a
6155 parenthesized ctor such as `(T ())' that looks like a cast to
6156 function returning T. */
6157 if (!cp_parser_error_occurred (parser)
6158 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6161 cp_parser_parse_definitely (parser);
6162 expr = cp_parser_cast_expression (parser,
6163 /*address_p=*/false,
6164 /*cast_p=*/true, pidk);
6166 /* Warn about old-style casts, if so requested. */
6167 if (warn_old_style_cast
6168 && !in_system_header
6169 && !VOID_TYPE_P (type)
6170 && current_lang_name != lang_name_c)
6171 warning (OPT_Wold_style_cast, "use of old-style cast");
6173 /* Only type conversions to integral or enumeration types
6174 can be used in constant-expressions. */
6175 if (!cast_valid_in_integral_constant_expression_p (type)
6176 && (cp_parser_non_integral_constant_expression
6178 "a cast to a type other than an integral or "
6179 "enumeration type")))
6180 return error_mark_node;
6182 /* Perform the cast. */
6183 expr = build_c_cast (input_location, type, expr);
6187 cp_parser_abort_tentative_parse (parser);
6190 /* If we get here, then it's not a cast, so it must be a
6191 unary-expression. */
6192 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6195 /* Parse a binary expression of the general form:
6199 pm-expression .* cast-expression
6200 pm-expression ->* cast-expression
6202 multiplicative-expression:
6204 multiplicative-expression * pm-expression
6205 multiplicative-expression / pm-expression
6206 multiplicative-expression % pm-expression
6208 additive-expression:
6209 multiplicative-expression
6210 additive-expression + multiplicative-expression
6211 additive-expression - multiplicative-expression
6215 shift-expression << additive-expression
6216 shift-expression >> additive-expression
6218 relational-expression:
6220 relational-expression < shift-expression
6221 relational-expression > shift-expression
6222 relational-expression <= shift-expression
6223 relational-expression >= shift-expression
6227 relational-expression:
6228 relational-expression <? shift-expression
6229 relational-expression >? shift-expression
6231 equality-expression:
6232 relational-expression
6233 equality-expression == relational-expression
6234 equality-expression != relational-expression
6238 and-expression & equality-expression
6240 exclusive-or-expression:
6242 exclusive-or-expression ^ and-expression
6244 inclusive-or-expression:
6245 exclusive-or-expression
6246 inclusive-or-expression | exclusive-or-expression
6248 logical-and-expression:
6249 inclusive-or-expression
6250 logical-and-expression && inclusive-or-expression
6252 logical-or-expression:
6253 logical-and-expression
6254 logical-or-expression || logical-and-expression
6256 All these are implemented with a single function like:
6259 simple-cast-expression
6260 binary-expression <token> binary-expression
6262 CAST_P is true if this expression is the target of a cast.
6264 The binops_by_token map is used to get the tree codes for each <token> type.
6265 binary-expressions are associated according to a precedence table. */
6267 #define TOKEN_PRECEDENCE(token) \
6268 (((token->type == CPP_GREATER \
6269 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6270 && !parser->greater_than_is_operator_p) \
6271 ? PREC_NOT_OPERATOR \
6272 : binops_by_token[token->type].prec)
6275 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6276 bool no_toplevel_fold_p,
6277 enum cp_parser_prec prec,
6280 cp_parser_expression_stack stack;
6281 cp_parser_expression_stack_entry *sp = &stack[0];
6284 enum tree_code tree_type, lhs_type, rhs_type;
6285 enum cp_parser_prec new_prec, lookahead_prec;
6288 /* Parse the first expression. */
6289 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6290 lhs_type = ERROR_MARK;
6294 /* Get an operator token. */
6295 token = cp_lexer_peek_token (parser->lexer);
6297 if (warn_cxx0x_compat
6298 && token->type == CPP_RSHIFT
6299 && !parser->greater_than_is_operator_p)
6301 if (warning_at (token->location, OPT_Wc__0x_compat,
6302 "%<>>%> operator will be treated as"
6303 " two right angle brackets in C++0x"))
6304 inform (token->location,
6305 "suggest parentheses around %<>>%> expression");
6308 new_prec = TOKEN_PRECEDENCE (token);
6310 /* Popping an entry off the stack means we completed a subexpression:
6311 - either we found a token which is not an operator (`>' where it is not
6312 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6313 will happen repeatedly;
6314 - or, we found an operator which has lower priority. This is the case
6315 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6317 if (new_prec <= prec)
6326 tree_type = binops_by_token[token->type].tree_type;
6328 /* We used the operator token. */
6329 cp_lexer_consume_token (parser->lexer);
6331 /* For "false && x" or "true || x", x will never be executed;
6332 disable warnings while evaluating it. */
6333 if (tree_type == TRUTH_ANDIF_EXPR)
6334 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6335 else if (tree_type == TRUTH_ORIF_EXPR)
6336 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6338 /* Extract another operand. It may be the RHS of this expression
6339 or the LHS of a new, higher priority expression. */
6340 rhs = cp_parser_simple_cast_expression (parser);
6341 rhs_type = ERROR_MARK;
6343 /* Get another operator token. Look up its precedence to avoid
6344 building a useless (immediately popped) stack entry for common
6345 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6346 token = cp_lexer_peek_token (parser->lexer);
6347 lookahead_prec = TOKEN_PRECEDENCE (token);
6348 if (lookahead_prec > new_prec)
6350 /* ... and prepare to parse the RHS of the new, higher priority
6351 expression. Since precedence levels on the stack are
6352 monotonically increasing, we do not have to care about
6355 sp->tree_type = tree_type;
6357 sp->lhs_type = lhs_type;
6360 lhs_type = rhs_type;
6362 new_prec = lookahead_prec;
6366 lookahead_prec = new_prec;
6367 /* If the stack is not empty, we have parsed into LHS the right side
6368 (`4' in the example above) of an expression we had suspended.
6369 We can use the information on the stack to recover the LHS (`3')
6370 from the stack together with the tree code (`MULT_EXPR'), and
6371 the precedence of the higher level subexpression
6372 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6373 which will be used to actually build the additive expression. */
6376 tree_type = sp->tree_type;
6378 rhs_type = lhs_type;
6380 lhs_type = sp->lhs_type;
6383 /* Undo the disabling of warnings done above. */
6384 if (tree_type == TRUTH_ANDIF_EXPR)
6385 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6386 else if (tree_type == TRUTH_ORIF_EXPR)
6387 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6389 overloaded_p = false;
6390 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6391 ERROR_MARK for everything that is not a binary expression.
6392 This makes warn_about_parentheses miss some warnings that
6393 involve unary operators. For unary expressions we should
6394 pass the correct tree_code unless the unary expression was
6395 surrounded by parentheses.
6397 if (no_toplevel_fold_p
6398 && lookahead_prec <= prec
6400 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6401 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6403 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6404 &overloaded_p, tf_warning_or_error);
6405 lhs_type = tree_type;
6407 /* If the binary operator required the use of an overloaded operator,
6408 then this expression cannot be an integral constant-expression.
6409 An overloaded operator can be used even if both operands are
6410 otherwise permissible in an integral constant-expression if at
6411 least one of the operands is of enumeration type. */
6414 && (cp_parser_non_integral_constant_expression
6415 (parser, "calls to overloaded operators")))
6416 return error_mark_node;
6423 /* Parse the `? expression : assignment-expression' part of a
6424 conditional-expression. The LOGICAL_OR_EXPR is the
6425 logical-or-expression that started the conditional-expression.
6426 Returns a representation of the entire conditional-expression.
6428 This routine is used by cp_parser_assignment_expression.
6430 ? expression : assignment-expression
6434 ? : assignment-expression */
6437 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6440 tree assignment_expr;
6442 /* Consume the `?' token. */
6443 cp_lexer_consume_token (parser->lexer);
6444 if (cp_parser_allow_gnu_extensions_p (parser)
6445 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6447 /* Implicit true clause. */
6449 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6453 /* Parse the expression. */
6454 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6455 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6456 c_inhibit_evaluation_warnings +=
6457 ((logical_or_expr == truthvalue_true_node)
6458 - (logical_or_expr == truthvalue_false_node));
6461 /* The next token should be a `:'. */
6462 cp_parser_require (parser, CPP_COLON, "%<:%>");
6463 /* Parse the assignment-expression. */
6464 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6465 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6467 /* Build the conditional-expression. */
6468 return build_x_conditional_expr (logical_or_expr,
6471 tf_warning_or_error);
6474 /* Parse an assignment-expression.
6476 assignment-expression:
6477 conditional-expression
6478 logical-or-expression assignment-operator assignment_expression
6481 CAST_P is true if this expression is the target of a cast.
6483 Returns a representation for the expression. */
6486 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6491 /* If the next token is the `throw' keyword, then we're looking at
6492 a throw-expression. */
6493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6494 expr = cp_parser_throw_expression (parser);
6495 /* Otherwise, it must be that we are looking at a
6496 logical-or-expression. */
6499 /* Parse the binary expressions (logical-or-expression). */
6500 expr = cp_parser_binary_expression (parser, cast_p, false,
6501 PREC_NOT_OPERATOR, pidk);
6502 /* If the next token is a `?' then we're actually looking at a
6503 conditional-expression. */
6504 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6505 return cp_parser_question_colon_clause (parser, expr);
6508 enum tree_code assignment_operator;
6510 /* If it's an assignment-operator, we're using the second
6513 = cp_parser_assignment_operator_opt (parser);
6514 if (assignment_operator != ERROR_MARK)
6516 bool non_constant_p;
6518 /* Parse the right-hand side of the assignment. */
6519 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6521 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6522 maybe_warn_cpp0x ("extended initializer lists");
6524 /* An assignment may not appear in a
6525 constant-expression. */
6526 if (cp_parser_non_integral_constant_expression (parser,
6528 return error_mark_node;
6529 /* Build the assignment expression. */
6530 expr = build_x_modify_expr (expr,
6531 assignment_operator,
6533 tf_warning_or_error);
6541 /* Parse an (optional) assignment-operator.
6543 assignment-operator: one of
6544 = *= /= %= += -= >>= <<= &= ^= |=
6548 assignment-operator: one of
6551 If the next token is an assignment operator, the corresponding tree
6552 code is returned, and the token is consumed. For example, for
6553 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6554 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6555 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6556 operator, ERROR_MARK is returned. */
6558 static enum tree_code
6559 cp_parser_assignment_operator_opt (cp_parser* parser)
6564 /* Peek at the next token. */
6565 token = cp_lexer_peek_token (parser->lexer);
6567 switch (token->type)
6578 op = TRUNC_DIV_EXPR;
6582 op = TRUNC_MOD_EXPR;
6614 /* Nothing else is an assignment operator. */
6618 /* If it was an assignment operator, consume it. */
6619 if (op != ERROR_MARK)
6620 cp_lexer_consume_token (parser->lexer);
6625 /* Parse an expression.
6628 assignment-expression
6629 expression , assignment-expression
6631 CAST_P is true if this expression is the target of a cast.
6633 Returns a representation of the expression. */
6636 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6638 tree expression = NULL_TREE;
6642 tree assignment_expression;
6644 /* Parse the next assignment-expression. */
6645 assignment_expression
6646 = cp_parser_assignment_expression (parser, cast_p, pidk);
6647 /* If this is the first assignment-expression, we can just
6650 expression = assignment_expression;
6652 expression = build_x_compound_expr (expression,
6653 assignment_expression,
6654 tf_warning_or_error);
6655 /* If the next token is not a comma, then we are done with the
6657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6659 /* Consume the `,'. */
6660 cp_lexer_consume_token (parser->lexer);
6661 /* A comma operator cannot appear in a constant-expression. */
6662 if (cp_parser_non_integral_constant_expression (parser,
6663 "a comma operator"))
6664 expression = error_mark_node;
6670 /* Parse a constant-expression.
6672 constant-expression:
6673 conditional-expression
6675 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6676 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6677 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6678 is false, NON_CONSTANT_P should be NULL. */
6681 cp_parser_constant_expression (cp_parser* parser,
6682 bool allow_non_constant_p,
6683 bool *non_constant_p)
6685 bool saved_integral_constant_expression_p;
6686 bool saved_allow_non_integral_constant_expression_p;
6687 bool saved_non_integral_constant_expression_p;
6690 /* It might seem that we could simply parse the
6691 conditional-expression, and then check to see if it were
6692 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6693 one that the compiler can figure out is constant, possibly after
6694 doing some simplifications or optimizations. The standard has a
6695 precise definition of constant-expression, and we must honor
6696 that, even though it is somewhat more restrictive.
6702 is not a legal declaration, because `(2, 3)' is not a
6703 constant-expression. The `,' operator is forbidden in a
6704 constant-expression. However, GCC's constant-folding machinery
6705 will fold this operation to an INTEGER_CST for `3'. */
6707 /* Save the old settings. */
6708 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6709 saved_allow_non_integral_constant_expression_p
6710 = parser->allow_non_integral_constant_expression_p;
6711 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6712 /* We are now parsing a constant-expression. */
6713 parser->integral_constant_expression_p = true;
6714 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6715 parser->non_integral_constant_expression_p = false;
6716 /* Although the grammar says "conditional-expression", we parse an
6717 "assignment-expression", which also permits "throw-expression"
6718 and the use of assignment operators. In the case that
6719 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6720 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6721 actually essential that we look for an assignment-expression.
6722 For example, cp_parser_initializer_clauses uses this function to
6723 determine whether a particular assignment-expression is in fact
6725 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6726 /* Restore the old settings. */
6727 parser->integral_constant_expression_p
6728 = saved_integral_constant_expression_p;
6729 parser->allow_non_integral_constant_expression_p
6730 = saved_allow_non_integral_constant_expression_p;
6731 if (allow_non_constant_p)
6732 *non_constant_p = parser->non_integral_constant_expression_p;
6733 else if (parser->non_integral_constant_expression_p)
6734 expression = error_mark_node;
6735 parser->non_integral_constant_expression_p
6736 = saved_non_integral_constant_expression_p;
6741 /* Parse __builtin_offsetof.
6743 offsetof-expression:
6744 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6746 offsetof-member-designator:
6748 | offsetof-member-designator "." id-expression
6749 | offsetof-member-designator "[" expression "]"
6750 | offsetof-member-designator "->" id-expression */
6753 cp_parser_builtin_offsetof (cp_parser *parser)
6755 int save_ice_p, save_non_ice_p;
6760 /* We're about to accept non-integral-constant things, but will
6761 definitely yield an integral constant expression. Save and
6762 restore these values around our local parsing. */
6763 save_ice_p = parser->integral_constant_expression_p;
6764 save_non_ice_p = parser->non_integral_constant_expression_p;
6766 /* Consume the "__builtin_offsetof" token. */
6767 cp_lexer_consume_token (parser->lexer);
6768 /* Consume the opening `('. */
6769 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6770 /* Parse the type-id. */
6771 type = cp_parser_type_id (parser);
6772 /* Look for the `,'. */
6773 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6774 token = cp_lexer_peek_token (parser->lexer);
6776 /* Build the (type *)null that begins the traditional offsetof macro. */
6777 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6778 tf_warning_or_error);
6780 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6781 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6782 true, &dummy, token->location);
6785 token = cp_lexer_peek_token (parser->lexer);
6786 switch (token->type)
6788 case CPP_OPEN_SQUARE:
6789 /* offsetof-member-designator "[" expression "]" */
6790 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6794 /* offsetof-member-designator "->" identifier */
6795 expr = grok_array_decl (expr, integer_zero_node);
6799 /* offsetof-member-designator "." identifier */
6800 cp_lexer_consume_token (parser->lexer);
6801 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6806 case CPP_CLOSE_PAREN:
6807 /* Consume the ")" token. */
6808 cp_lexer_consume_token (parser->lexer);
6812 /* Error. We know the following require will fail, but
6813 that gives the proper error message. */
6814 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6815 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6816 expr = error_mark_node;
6822 /* If we're processing a template, we can't finish the semantics yet.
6823 Otherwise we can fold the entire expression now. */
6824 if (processing_template_decl)
6825 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6827 expr = finish_offsetof (expr);
6830 parser->integral_constant_expression_p = save_ice_p;
6831 parser->non_integral_constant_expression_p = save_non_ice_p;
6836 /* Parse a trait expression. */
6839 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6842 tree type1, type2 = NULL_TREE;
6843 bool binary = false;
6844 cp_decl_specifier_seq decl_specs;
6848 case RID_HAS_NOTHROW_ASSIGN:
6849 kind = CPTK_HAS_NOTHROW_ASSIGN;
6851 case RID_HAS_NOTHROW_CONSTRUCTOR:
6852 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6854 case RID_HAS_NOTHROW_COPY:
6855 kind = CPTK_HAS_NOTHROW_COPY;
6857 case RID_HAS_TRIVIAL_ASSIGN:
6858 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6860 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6861 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6863 case RID_HAS_TRIVIAL_COPY:
6864 kind = CPTK_HAS_TRIVIAL_COPY;
6866 case RID_HAS_TRIVIAL_DESTRUCTOR:
6867 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6869 case RID_HAS_VIRTUAL_DESTRUCTOR:
6870 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6872 case RID_IS_ABSTRACT:
6873 kind = CPTK_IS_ABSTRACT;
6875 case RID_IS_BASE_OF:
6876 kind = CPTK_IS_BASE_OF;
6880 kind = CPTK_IS_CLASS;
6882 case RID_IS_CONVERTIBLE_TO:
6883 kind = CPTK_IS_CONVERTIBLE_TO;
6887 kind = CPTK_IS_EMPTY;
6890 kind = CPTK_IS_ENUM;
6895 case RID_IS_POLYMORPHIC:
6896 kind = CPTK_IS_POLYMORPHIC;
6898 case RID_IS_STD_LAYOUT:
6899 kind = CPTK_IS_STD_LAYOUT;
6901 case RID_IS_TRIVIAL:
6902 kind = CPTK_IS_TRIVIAL;
6905 kind = CPTK_IS_UNION;
6911 /* Consume the token. */
6912 cp_lexer_consume_token (parser->lexer);
6914 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6916 type1 = cp_parser_type_id (parser);
6918 if (type1 == error_mark_node)
6919 return error_mark_node;
6921 /* Build a trivial decl-specifier-seq. */
6922 clear_decl_specs (&decl_specs);
6923 decl_specs.type = type1;
6925 /* Call grokdeclarator to figure out what type this is. */
6926 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6927 /*initialized=*/0, /*attrlist=*/NULL);
6931 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6933 type2 = cp_parser_type_id (parser);
6935 if (type2 == error_mark_node)
6936 return error_mark_node;
6938 /* Build a trivial decl-specifier-seq. */
6939 clear_decl_specs (&decl_specs);
6940 decl_specs.type = type2;
6942 /* Call grokdeclarator to figure out what type this is. */
6943 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6944 /*initialized=*/0, /*attrlist=*/NULL);
6947 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6949 /* Complete the trait expression, which may mean either processing
6950 the trait expr now or saving it for template instantiation. */
6951 return finish_trait_expr (kind, type1, type2);
6954 /* Lambdas that appear in variable initializer or default argument scope
6955 get that in their mangling, so we need to record it. We might as well
6956 use the count for function and namespace scopes as well. */
6957 static tree lambda_scope;
6958 static int lambda_count;
6959 typedef struct GTY(()) tree_int
6964 DEF_VEC_O(tree_int);
6965 DEF_VEC_ALLOC_O(tree_int,gc);
6966 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6969 start_lambda_scope (tree decl)
6973 /* Once we're inside a function, we ignore other scopes and just push
6974 the function again so that popping works properly. */
6975 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6976 decl = current_function_decl;
6977 ti.t = lambda_scope;
6978 ti.i = lambda_count;
6979 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6980 if (lambda_scope != decl)
6982 /* Don't reset the count if we're still in the same function. */
6983 lambda_scope = decl;
6989 record_lambda_scope (tree lambda)
6991 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
6992 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
6996 finish_lambda_scope (void)
6998 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
6999 if (lambda_scope != p->t)
7001 lambda_scope = p->t;
7002 lambda_count = p->i;
7004 VEC_pop (tree_int, lambda_scope_stack);
7007 /* We want to determine the linkage of a lambda type at pushtag time,
7008 before CLASSTYPE_LAMBDA_EXPR has been set. So this callback allows us
7009 to find out whether the current lambda mangling scope will give us
7013 no_linkage_lambda_type_p (tree type)
7016 if (!LAMBDA_TYPE_P (type))
7019 lambda = CLASSTYPE_LAMBDA_EXPR (type);
7021 scope = LAMBDA_EXPR_EXTRA_SCOPE (lambda);
7022 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
7023 /* We can't use lambda_scope, and CLASSTYPE_TEMPLATE_INFO won't be set
7024 yet either, so guess it's public for now. */
7027 scope = lambda_scope;
7029 return (scope == NULL_TREE);
7032 /* Parse a lambda expression.
7035 lambda-introducer lambda-declarator [opt] compound-statement
7037 Returns a representation of the expression. */
7040 cp_parser_lambda_expression (cp_parser* parser)
7042 tree lambda_expr = build_lambda_expr ();
7045 LAMBDA_EXPR_LOCATION (lambda_expr)
7046 = cp_lexer_peek_token (parser->lexer)->location;
7048 /* We may be in the middle of deferred access check. Disable
7050 push_deferring_access_checks (dk_no_deferred);
7052 type = begin_lambda_type (lambda_expr);
7054 record_lambda_scope (lambda_expr);
7057 /* Inside the class, surrounding template-parameter-lists do not apply. */
7058 unsigned int saved_num_template_parameter_lists
7059 = parser->num_template_parameter_lists;
7061 parser->num_template_parameter_lists = 0;
7063 cp_parser_lambda_introducer (parser, lambda_expr);
7065 /* By virtue of defining a local class, a lambda expression has access to
7066 the private variables of enclosing classes. */
7068 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7070 cp_parser_lambda_body (parser, lambda_expr);
7072 /* The capture list was built up in reverse order; fix that now. */
7074 tree newlist = NULL_TREE;
7077 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7080 tree field = TREE_PURPOSE (elt);
7083 next = TREE_CHAIN (elt);
7084 TREE_CHAIN (elt) = newlist;
7087 /* Also add __ to the beginning of the field name so that code
7088 outside the lambda body can't see the captured name. We could
7089 just remove the name entirely, but this is more useful for
7091 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7092 /* The 'this' capture already starts with __. */
7095 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7096 buf[1] = buf[0] = '_';
7097 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7098 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7099 DECL_NAME (field) = get_identifier (buf);
7101 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7104 type = finish_struct (type, /*attributes=*/NULL_TREE);
7106 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7109 pop_deferring_access_checks ();
7111 return build_lambda_object (lambda_expr);
7114 /* Parse the beginning of a lambda expression.
7117 [ lambda-capture [opt] ]
7119 LAMBDA_EXPR is the current representation of the lambda expression. */
7122 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7124 /* Need commas after the first capture. */
7127 /* Eat the leading `['. */
7128 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7130 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7131 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7132 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7133 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7134 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7135 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7137 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7139 cp_lexer_consume_token (parser->lexer);
7143 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7145 cp_token* capture_token;
7147 tree capture_init_expr;
7148 cp_id_kind idk = CP_ID_KIND_NONE;
7150 enum capture_kind_type
7155 enum capture_kind_type capture_kind = BY_COPY;
7157 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7159 error ("expected end of capture-list");
7166 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7168 /* Possibly capture `this'. */
7169 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7171 cp_lexer_consume_token (parser->lexer);
7172 add_capture (lambda_expr,
7173 /*id=*/get_identifier ("__this"),
7174 /*initializer=*/finish_this_expr(),
7175 /*by_reference_p=*/false);
7179 /* Remember whether we want to capture as a reference or not. */
7180 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7182 capture_kind = BY_REFERENCE;
7183 cp_lexer_consume_token (parser->lexer);
7186 /* Get the identifier. */
7187 capture_token = cp_lexer_peek_token (parser->lexer);
7188 capture_id = cp_parser_identifier (parser);
7190 if (capture_id == error_mark_node)
7191 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7192 delimiters, but I modified this to stop on unnested ']' as well. It
7193 was already changed to stop on unnested '}', so the
7194 "closing_parenthesis" name is no more misleading with my change. */
7196 cp_parser_skip_to_closing_parenthesis (parser,
7197 /*recovering=*/true,
7199 /*consume_paren=*/true);
7203 /* Find the initializer for this capture. */
7204 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7206 /* An explicit expression exists. */
7207 cp_lexer_consume_token (parser->lexer);
7208 pedwarn (input_location, OPT_pedantic,
7209 "ISO C++ does not allow initializers "
7210 "in lambda expression capture lists");
7211 capture_init_expr = cp_parser_assignment_expression (parser,
7217 const char* error_msg;
7219 /* Turn the identifier into an id-expression. */
7221 = cp_parser_lookup_name
7225 /*is_template=*/false,
7226 /*is_namespace=*/false,
7227 /*check_dependency=*/true,
7228 /*ambiguous_decls=*/NULL,
7229 capture_token->location);
7232 = finish_id_expression
7237 /*integral_constant_expression_p=*/false,
7238 /*allow_non_integral_constant_expression_p=*/false,
7239 /*non_integral_constant_expression_p=*/NULL,
7240 /*template_p=*/false,
7242 /*address_p=*/false,
7243 /*template_arg_p=*/false,
7245 capture_token->location);
7248 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7250 = unqualified_name_lookup_error (capture_init_expr);
7252 add_capture (lambda_expr,
7255 /*by_reference_p=*/capture_kind == BY_REFERENCE);
7258 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7261 /* Parse the (optional) middle of a lambda expression.
7264 ( parameter-declaration-clause [opt] )
7265 attribute-specifier [opt]
7267 exception-specification [opt]
7268 lambda-return-type-clause [opt]
7270 LAMBDA_EXPR is the current representation of the lambda expression. */
7273 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7275 /* 5.1.1.4 of the standard says:
7276 If a lambda-expression does not include a lambda-declarator, it is as if
7277 the lambda-declarator were ().
7278 This means an empty parameter list, no attributes, and no exception
7280 tree param_list = void_list_node;
7281 tree attributes = NULL_TREE;
7282 tree exception_spec = NULL_TREE;
7285 /* The lambda-declarator is optional, but must begin with an opening
7286 parenthesis if present. */
7287 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7289 cp_lexer_consume_token (parser->lexer);
7291 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7293 /* Parse parameters. */
7294 param_list = cp_parser_parameter_declaration_clause (parser);
7296 /* Default arguments shall not be specified in the
7297 parameter-declaration-clause of a lambda-declarator. */
7298 for (t = param_list; t; t = TREE_CHAIN (t))
7299 if (TREE_PURPOSE (t))
7300 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7301 "default argument specified for lambda parameter");
7303 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7305 attributes = cp_parser_attributes_opt (parser);
7307 /* Parse optional `mutable' keyword. */
7308 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7310 cp_lexer_consume_token (parser->lexer);
7311 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7314 /* Parse optional exception specification. */
7315 exception_spec = cp_parser_exception_specification_opt (parser);
7317 /* Parse optional trailing return type. */
7318 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7320 cp_lexer_consume_token (parser->lexer);
7321 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7324 /* The function parameters must be in scope all the way until after the
7325 trailing-return-type in case of decltype. */
7326 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7327 pop_binding (DECL_NAME (t), t);
7332 /* Create the function call operator.
7334 Messing with declarators like this is no uglier than building up the
7335 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7338 cp_decl_specifier_seq return_type_specs;
7339 cp_declarator* declarator;
7344 clear_decl_specs (&return_type_specs);
7345 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7346 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7348 /* Maybe we will deduce the return type later, but we can use void
7349 as a placeholder return type anyways. */
7350 return_type_specs.type = void_type_node;
7352 p = obstack_alloc (&declarator_obstack, 0);
7354 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7357 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7358 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7359 declarator = make_call_declarator (declarator, param_list, quals,
7361 /*late_return_type=*/NULL_TREE);
7363 fco = grokmethod (&return_type_specs,
7366 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7367 DECL_ARTIFICIAL (fco) = 1;
7369 finish_member_declaration (fco);
7371 obstack_free (&declarator_obstack, p);
7375 /* Parse the body of a lambda expression, which is simply
7379 but which requires special handling.
7380 LAMBDA_EXPR is the current representation of the lambda expression. */
7383 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7385 bool nested = (current_function_decl != NULL_TREE);
7387 push_function_context ();
7389 /* Finish the function call operator
7391 + late_parsing_for_member
7392 + function_definition_after_declarator
7393 + ctor_initializer_opt_and_function_body */
7395 tree fco = lambda_function (lambda_expr);
7399 /* Let the front end know that we are going to be defining this
7401 start_preparsed_function (fco,
7403 SF_PRE_PARSED | SF_INCLASS_INLINE);
7405 start_lambda_scope (fco);
7406 body = begin_function_body ();
7408 /* 5.1.1.4 of the standard says:
7409 If a lambda-expression does not include a trailing-return-type, it
7410 is as if the trailing-return-type denotes the following type:
7411 * if the compound-statement is of the form
7412 { return attribute-specifier [opt] expression ; }
7413 the type of the returned expression after lvalue-to-rvalue
7414 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7415 (_conv.array_ 4.2), and function-to-pointer conversion
7417 * otherwise, void. */
7419 /* In a lambda that has neither a lambda-return-type-clause
7420 nor a deducible form, errors should be reported for return statements
7421 in the body. Since we used void as the placeholder return type, parsing
7422 the body as usual will give such desired behavior. */
7423 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7424 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7425 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7426 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7429 tree expr = NULL_TREE;
7430 cp_id_kind idk = CP_ID_KIND_NONE;
7432 /* Parse tentatively in case there's more after the initial return
7434 cp_parser_parse_tentatively (parser);
7436 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7437 cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7439 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7441 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7442 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7444 if (cp_parser_parse_definitely (parser))
7446 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7448 compound_stmt = begin_compound_stmt (0);
7449 /* Will get error here if type not deduced yet. */
7450 finish_return_stmt (expr);
7451 finish_compound_stmt (compound_stmt);
7459 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7460 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7461 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7462 cp_parser_compound_stmt does not pass it. */
7463 cp_parser_function_body (parser);
7464 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7467 finish_function_body (body);
7468 finish_lambda_scope ();
7470 /* Finish the function and generate code for it if necessary. */
7471 expand_or_defer_fn (finish_function (/*inline*/2));
7475 pop_function_context();
7478 /* Statements [gram.stmt.stmt] */
7480 /* Parse a statement.
7484 expression-statement
7489 declaration-statement
7492 IN_COMPOUND is true when the statement is nested inside a
7493 cp_parser_compound_statement; this matters for certain pragmas.
7495 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7496 is a (possibly labeled) if statement which is not enclosed in braces
7497 and has an else clause. This is used to implement -Wparentheses. */
7500 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7501 bool in_compound, bool *if_p)
7505 location_t statement_location;
7510 /* There is no statement yet. */
7511 statement = NULL_TREE;
7512 /* Peek at the next token. */
7513 token = cp_lexer_peek_token (parser->lexer);
7514 /* Remember the location of the first token in the statement. */
7515 statement_location = token->location;
7516 /* If this is a keyword, then that will often determine what kind of
7517 statement we have. */
7518 if (token->type == CPP_KEYWORD)
7520 enum rid keyword = token->keyword;
7526 /* Looks like a labeled-statement with a case label.
7527 Parse the label, and then use tail recursion to parse
7529 cp_parser_label_for_labeled_statement (parser);
7534 statement = cp_parser_selection_statement (parser, if_p);
7540 statement = cp_parser_iteration_statement (parser);
7547 statement = cp_parser_jump_statement (parser);
7550 /* Objective-C++ exception-handling constructs. */
7553 case RID_AT_FINALLY:
7554 case RID_AT_SYNCHRONIZED:
7556 statement = cp_parser_objc_statement (parser);
7560 statement = cp_parser_try_block (parser);
7564 /* This must be a namespace alias definition. */
7565 cp_parser_declaration_statement (parser);
7569 /* It might be a keyword like `int' that can start a
7570 declaration-statement. */
7574 else if (token->type == CPP_NAME)
7576 /* If the next token is a `:', then we are looking at a
7577 labeled-statement. */
7578 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7579 if (token->type == CPP_COLON)
7581 /* Looks like a labeled-statement with an ordinary label.
7582 Parse the label, and then use tail recursion to parse
7584 cp_parser_label_for_labeled_statement (parser);
7588 /* Anything that starts with a `{' must be a compound-statement. */
7589 else if (token->type == CPP_OPEN_BRACE)
7590 statement = cp_parser_compound_statement (parser, NULL, false);
7591 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7592 a statement all its own. */
7593 else if (token->type == CPP_PRAGMA)
7595 /* Only certain OpenMP pragmas are attached to statements, and thus
7596 are considered statements themselves. All others are not. In
7597 the context of a compound, accept the pragma as a "statement" and
7598 return so that we can check for a close brace. Otherwise we
7599 require a real statement and must go back and read one. */
7601 cp_parser_pragma (parser, pragma_compound);
7602 else if (!cp_parser_pragma (parser, pragma_stmt))
7606 else if (token->type == CPP_EOF)
7608 cp_parser_error (parser, "expected statement");
7612 /* Everything else must be a declaration-statement or an
7613 expression-statement. Try for the declaration-statement
7614 first, unless we are looking at a `;', in which case we know that
7615 we have an expression-statement. */
7618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7620 cp_parser_parse_tentatively (parser);
7621 /* Try to parse the declaration-statement. */
7622 cp_parser_declaration_statement (parser);
7623 /* If that worked, we're done. */
7624 if (cp_parser_parse_definitely (parser))
7627 /* Look for an expression-statement instead. */
7628 statement = cp_parser_expression_statement (parser, in_statement_expr);
7631 /* Set the line number for the statement. */
7632 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7633 SET_EXPR_LOCATION (statement, statement_location);
7636 /* Parse the label for a labeled-statement, i.e.
7639 case constant-expression :
7643 case constant-expression ... constant-expression : statement
7645 When a label is parsed without errors, the label is added to the
7646 parse tree by the finish_* functions, so this function doesn't
7647 have to return the label. */
7650 cp_parser_label_for_labeled_statement (cp_parser* parser)
7653 tree label = NULL_TREE;
7655 /* The next token should be an identifier. */
7656 token = cp_lexer_peek_token (parser->lexer);
7657 if (token->type != CPP_NAME
7658 && token->type != CPP_KEYWORD)
7660 cp_parser_error (parser, "expected labeled-statement");
7664 switch (token->keyword)
7671 /* Consume the `case' token. */
7672 cp_lexer_consume_token (parser->lexer);
7673 /* Parse the constant-expression. */
7674 expr = cp_parser_constant_expression (parser,
7675 /*allow_non_constant_p=*/false,
7678 ellipsis = cp_lexer_peek_token (parser->lexer);
7679 if (ellipsis->type == CPP_ELLIPSIS)
7681 /* Consume the `...' token. */
7682 cp_lexer_consume_token (parser->lexer);
7684 cp_parser_constant_expression (parser,
7685 /*allow_non_constant_p=*/false,
7687 /* We don't need to emit warnings here, as the common code
7688 will do this for us. */
7691 expr_hi = NULL_TREE;
7693 if (parser->in_switch_statement_p)
7694 finish_case_label (token->location, expr, expr_hi);
7696 error_at (token->location,
7697 "case label %qE not within a switch statement",
7703 /* Consume the `default' token. */
7704 cp_lexer_consume_token (parser->lexer);
7706 if (parser->in_switch_statement_p)
7707 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7709 error_at (token->location, "case label not within a switch statement");
7713 /* Anything else must be an ordinary label. */
7714 label = finish_label_stmt (cp_parser_identifier (parser));
7718 /* Require the `:' token. */
7719 cp_parser_require (parser, CPP_COLON, "%<:%>");
7721 /* An ordinary label may optionally be followed by attributes.
7722 However, this is only permitted if the attributes are then
7723 followed by a semicolon. This is because, for backward
7724 compatibility, when parsing
7725 lab: __attribute__ ((unused)) int i;
7726 we want the attribute to attach to "i", not "lab". */
7727 if (label != NULL_TREE
7728 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7732 cp_parser_parse_tentatively (parser);
7733 attrs = cp_parser_attributes_opt (parser);
7734 if (attrs == NULL_TREE
7735 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7736 cp_parser_abort_tentative_parse (parser);
7737 else if (!cp_parser_parse_definitely (parser))
7740 cplus_decl_attributes (&label, attrs, 0);
7744 /* Parse an expression-statement.
7746 expression-statement:
7749 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7750 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7751 indicates whether this expression-statement is part of an
7752 expression statement. */
7755 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7757 tree statement = NULL_TREE;
7759 /* If the next token is a ';', then there is no expression
7761 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7762 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7764 /* Consume the final `;'. */
7765 cp_parser_consume_semicolon_at_end_of_statement (parser);
7767 if (in_statement_expr
7768 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7769 /* This is the final expression statement of a statement
7771 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7773 statement = finish_expr_stmt (statement);
7780 /* Parse a compound-statement.
7783 { statement-seq [opt] }
7788 { label-declaration-seq [opt] statement-seq [opt] }
7790 label-declaration-seq:
7792 label-declaration-seq label-declaration
7794 Returns a tree representing the statement. */
7797 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7802 /* Consume the `{'. */
7803 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7804 return error_mark_node;
7805 /* Begin the compound-statement. */
7806 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7807 /* If the next keyword is `__label__' we have a label declaration. */
7808 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7809 cp_parser_label_declaration (parser);
7810 /* Parse an (optional) statement-seq. */
7811 cp_parser_statement_seq_opt (parser, in_statement_expr);
7812 /* Finish the compound-statement. */
7813 finish_compound_stmt (compound_stmt);
7814 /* Consume the `}'. */
7815 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7817 return compound_stmt;
7820 /* Parse an (optional) statement-seq.
7824 statement-seq [opt] statement */
7827 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7829 /* Scan statements until there aren't any more. */
7832 cp_token *token = cp_lexer_peek_token (parser->lexer);
7834 /* If we're looking at a `}', then we've run out of statements. */
7835 if (token->type == CPP_CLOSE_BRACE
7836 || token->type == CPP_EOF
7837 || token->type == CPP_PRAGMA_EOL)
7840 /* If we are in a compound statement and find 'else' then
7841 something went wrong. */
7842 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7844 if (parser->in_statement & IN_IF_STMT)
7848 token = cp_lexer_consume_token (parser->lexer);
7849 error_at (token->location, "%<else%> without a previous %<if%>");
7853 /* Parse the statement. */
7854 cp_parser_statement (parser, in_statement_expr, true, NULL);
7858 /* Parse a selection-statement.
7860 selection-statement:
7861 if ( condition ) statement
7862 if ( condition ) statement else statement
7863 switch ( condition ) statement
7865 Returns the new IF_STMT or SWITCH_STMT.
7867 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7868 is a (possibly labeled) if statement which is not enclosed in
7869 braces and has an else clause. This is used to implement
7873 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7881 /* Peek at the next token. */
7882 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7884 /* See what kind of keyword it is. */
7885 keyword = token->keyword;
7894 /* Look for the `('. */
7895 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7897 cp_parser_skip_to_end_of_statement (parser);
7898 return error_mark_node;
7901 /* Begin the selection-statement. */
7902 if (keyword == RID_IF)
7903 statement = begin_if_stmt ();
7905 statement = begin_switch_stmt ();
7907 /* Parse the condition. */
7908 condition = cp_parser_condition (parser);
7909 /* Look for the `)'. */
7910 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7911 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7912 /*consume_paren=*/true);
7914 if (keyword == RID_IF)
7917 unsigned char in_statement;
7919 /* Add the condition. */
7920 finish_if_stmt_cond (condition, statement);
7922 /* Parse the then-clause. */
7923 in_statement = parser->in_statement;
7924 parser->in_statement |= IN_IF_STMT;
7925 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7927 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7928 add_stmt (build_empty_stmt (loc));
7929 cp_lexer_consume_token (parser->lexer);
7930 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7931 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7932 "empty body in an %<if%> statement");
7936 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7937 parser->in_statement = in_statement;
7939 finish_then_clause (statement);
7941 /* If the next token is `else', parse the else-clause. */
7942 if (cp_lexer_next_token_is_keyword (parser->lexer,
7945 /* Consume the `else' keyword. */
7946 cp_lexer_consume_token (parser->lexer);
7947 begin_else_clause (statement);
7948 /* Parse the else-clause. */
7949 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7952 loc = cp_lexer_peek_token (parser->lexer)->location;
7954 OPT_Wempty_body, "suggest braces around "
7955 "empty body in an %<else%> statement");
7956 add_stmt (build_empty_stmt (loc));
7957 cp_lexer_consume_token (parser->lexer);
7960 cp_parser_implicitly_scoped_statement (parser, NULL);
7962 finish_else_clause (statement);
7964 /* If we are currently parsing a then-clause, then
7965 IF_P will not be NULL. We set it to true to
7966 indicate that this if statement has an else clause.
7967 This may trigger the Wparentheses warning below
7968 when we get back up to the parent if statement. */
7974 /* This if statement does not have an else clause. If
7975 NESTED_IF is true, then the then-clause is an if
7976 statement which does have an else clause. We warn
7977 about the potential ambiguity. */
7979 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7980 "suggest explicit braces to avoid ambiguous"
7984 /* Now we're all done with the if-statement. */
7985 finish_if_stmt (statement);
7989 bool in_switch_statement_p;
7990 unsigned char in_statement;
7992 /* Add the condition. */
7993 finish_switch_cond (condition, statement);
7995 /* Parse the body of the switch-statement. */
7996 in_switch_statement_p = parser->in_switch_statement_p;
7997 in_statement = parser->in_statement;
7998 parser->in_switch_statement_p = true;
7999 parser->in_statement |= IN_SWITCH_STMT;
8000 cp_parser_implicitly_scoped_statement (parser, NULL);
8001 parser->in_switch_statement_p = in_switch_statement_p;
8002 parser->in_statement = in_statement;
8004 /* Now we're all done with the switch-statement. */
8005 finish_switch_stmt (statement);
8013 cp_parser_error (parser, "expected selection-statement");
8014 return error_mark_node;
8018 /* Parse a condition.
8022 type-specifier-seq declarator = initializer-clause
8023 type-specifier-seq declarator braced-init-list
8028 type-specifier-seq declarator asm-specification [opt]
8029 attributes [opt] = assignment-expression
8031 Returns the expression that should be tested. */
8034 cp_parser_condition (cp_parser* parser)
8036 cp_decl_specifier_seq type_specifiers;
8037 const char *saved_message;
8039 /* Try the declaration first. */
8040 cp_parser_parse_tentatively (parser);
8041 /* New types are not allowed in the type-specifier-seq for a
8043 saved_message = parser->type_definition_forbidden_message;
8044 parser->type_definition_forbidden_message
8045 = "types may not be defined in conditions";
8046 /* Parse the type-specifier-seq. */
8047 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
8049 /* Restore the saved message. */
8050 parser->type_definition_forbidden_message = saved_message;
8051 /* If all is well, we might be looking at a declaration. */
8052 if (!cp_parser_error_occurred (parser))
8055 tree asm_specification;
8057 cp_declarator *declarator;
8058 tree initializer = NULL_TREE;
8060 /* Parse the declarator. */
8061 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8062 /*ctor_dtor_or_conv_p=*/NULL,
8063 /*parenthesized_p=*/NULL,
8064 /*member_p=*/false);
8065 /* Parse the attributes. */
8066 attributes = cp_parser_attributes_opt (parser);
8067 /* Parse the asm-specification. */
8068 asm_specification = cp_parser_asm_specification_opt (parser);
8069 /* If the next token is not an `=' or '{', then we might still be
8070 looking at an expression. For example:
8074 looks like a decl-specifier-seq and a declarator -- but then
8075 there is no `=', so this is an expression. */
8076 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8077 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8078 cp_parser_simulate_error (parser);
8080 /* If we did see an `=' or '{', then we are looking at a declaration
8082 if (cp_parser_parse_definitely (parser))
8085 bool non_constant_p;
8086 bool flags = LOOKUP_ONLYCONVERTING;
8088 /* Create the declaration. */
8089 decl = start_decl (declarator, &type_specifiers,
8090 /*initialized_p=*/true,
8091 attributes, /*prefix_attributes=*/NULL_TREE,
8094 /* Parse the initializer. */
8095 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8097 initializer = cp_parser_braced_list (parser, &non_constant_p);
8098 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8103 /* Consume the `='. */
8104 cp_parser_require (parser, CPP_EQ, "%<=%>");
8105 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8107 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8108 maybe_warn_cpp0x ("extended initializer lists");
8110 if (!non_constant_p)
8111 initializer = fold_non_dependent_expr (initializer);
8113 /* Process the initializer. */
8114 cp_finish_decl (decl,
8115 initializer, !non_constant_p,
8120 pop_scope (pushed_scope);
8122 return convert_from_reference (decl);
8125 /* If we didn't even get past the declarator successfully, we are
8126 definitely not looking at a declaration. */
8128 cp_parser_abort_tentative_parse (parser);
8130 /* Otherwise, we are looking at an expression. */
8131 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8134 /* Parse an iteration-statement.
8136 iteration-statement:
8137 while ( condition ) statement
8138 do statement while ( expression ) ;
8139 for ( for-init-statement condition [opt] ; expression [opt] )
8142 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
8145 cp_parser_iteration_statement (cp_parser* parser)
8150 unsigned char in_statement;
8152 /* Peek at the next token. */
8153 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8155 return error_mark_node;
8157 /* Remember whether or not we are already within an iteration
8159 in_statement = parser->in_statement;
8161 /* See what kind of keyword it is. */
8162 keyword = token->keyword;
8169 /* Begin the while-statement. */
8170 statement = begin_while_stmt ();
8171 /* Look for the `('. */
8172 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8173 /* Parse the condition. */
8174 condition = cp_parser_condition (parser);
8175 finish_while_stmt_cond (condition, statement);
8176 /* Look for the `)'. */
8177 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8178 /* Parse the dependent statement. */
8179 parser->in_statement = IN_ITERATION_STMT;
8180 cp_parser_already_scoped_statement (parser);
8181 parser->in_statement = in_statement;
8182 /* We're done with the while-statement. */
8183 finish_while_stmt (statement);
8191 /* Begin the do-statement. */
8192 statement = begin_do_stmt ();
8193 /* Parse the body of the do-statement. */
8194 parser->in_statement = IN_ITERATION_STMT;
8195 cp_parser_implicitly_scoped_statement (parser, NULL);
8196 parser->in_statement = in_statement;
8197 finish_do_body (statement);
8198 /* Look for the `while' keyword. */
8199 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8200 /* Look for the `('. */
8201 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8202 /* Parse the expression. */
8203 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8204 /* We're done with the do-statement. */
8205 finish_do_stmt (expression, statement);
8206 /* Look for the `)'. */
8207 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8208 /* Look for the `;'. */
8209 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8215 tree condition = NULL_TREE;
8216 tree expression = NULL_TREE;
8218 /* Begin the for-statement. */
8219 statement = begin_for_stmt ();
8220 /* Look for the `('. */
8221 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8222 /* Parse the initialization. */
8223 cp_parser_for_init_statement (parser);
8224 finish_for_init_stmt (statement);
8226 /* If there's a condition, process it. */
8227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8228 condition = cp_parser_condition (parser);
8229 finish_for_cond (condition, statement);
8230 /* Look for the `;'. */
8231 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8233 /* If there's an expression, process it. */
8234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8235 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8236 finish_for_expr (expression, statement);
8237 /* Look for the `)'. */
8238 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8240 /* Parse the body of the for-statement. */
8241 parser->in_statement = IN_ITERATION_STMT;
8242 cp_parser_already_scoped_statement (parser);
8243 parser->in_statement = in_statement;
8245 /* We're done with the for-statement. */
8246 finish_for_stmt (statement);
8251 cp_parser_error (parser, "expected iteration-statement");
8252 statement = error_mark_node;
8259 /* Parse a for-init-statement.
8262 expression-statement
8263 simple-declaration */
8266 cp_parser_for_init_statement (cp_parser* parser)
8268 /* If the next token is a `;', then we have an empty
8269 expression-statement. Grammatically, this is also a
8270 simple-declaration, but an invalid one, because it does not
8271 declare anything. Therefore, if we did not handle this case
8272 specially, we would issue an error message about an invalid
8274 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8276 /* We're going to speculatively look for a declaration, falling back
8277 to an expression, if necessary. */
8278 cp_parser_parse_tentatively (parser);
8279 /* Parse the declaration. */
8280 cp_parser_simple_declaration (parser,
8281 /*function_definition_allowed_p=*/false);
8282 /* If the tentative parse failed, then we shall need to look for an
8283 expression-statement. */
8284 if (cp_parser_parse_definitely (parser))
8288 cp_parser_expression_statement (parser, false);
8291 /* Parse a jump-statement.
8296 return expression [opt] ;
8297 return braced-init-list ;
8305 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8308 cp_parser_jump_statement (cp_parser* parser)
8310 tree statement = error_mark_node;
8313 unsigned char in_statement;
8315 /* Peek at the next token. */
8316 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8318 return error_mark_node;
8320 /* See what kind of keyword it is. */
8321 keyword = token->keyword;
8325 in_statement = parser->in_statement & ~IN_IF_STMT;
8326 switch (in_statement)
8329 error_at (token->location, "break statement not within loop or switch");
8332 gcc_assert ((in_statement & IN_SWITCH_STMT)
8333 || in_statement == IN_ITERATION_STMT);
8334 statement = finish_break_stmt ();
8337 error_at (token->location, "invalid exit from OpenMP structured block");
8340 error_at (token->location, "break statement used with OpenMP for loop");
8343 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8347 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8350 error_at (token->location, "continue statement not within a loop");
8352 case IN_ITERATION_STMT:
8354 statement = finish_continue_stmt ();
8357 error_at (token->location, "invalid exit from OpenMP structured block");
8362 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8368 bool expr_non_constant_p;
8370 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8372 maybe_warn_cpp0x ("extended initializer lists");
8373 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8375 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8376 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8378 /* If the next token is a `;', then there is no
8381 /* Build the return-statement. */
8382 statement = finish_return_stmt (expr);
8383 /* Look for the final `;'. */
8384 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8389 /* Create the goto-statement. */
8390 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8392 /* Issue a warning about this use of a GNU extension. */
8393 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8394 /* Consume the '*' token. */
8395 cp_lexer_consume_token (parser->lexer);
8396 /* Parse the dependent expression. */
8397 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8400 finish_goto_stmt (cp_parser_identifier (parser));
8401 /* Look for the final `;'. */
8402 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8406 cp_parser_error (parser, "expected jump-statement");
8413 /* Parse a declaration-statement.
8415 declaration-statement:
8416 block-declaration */
8419 cp_parser_declaration_statement (cp_parser* parser)
8423 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8424 p = obstack_alloc (&declarator_obstack, 0);
8426 /* Parse the block-declaration. */
8427 cp_parser_block_declaration (parser, /*statement_p=*/true);
8429 /* Free any declarators allocated. */
8430 obstack_free (&declarator_obstack, p);
8432 /* Finish off the statement. */
8436 /* Some dependent statements (like `if (cond) statement'), are
8437 implicitly in their own scope. In other words, if the statement is
8438 a single statement (as opposed to a compound-statement), it is
8439 none-the-less treated as if it were enclosed in braces. Any
8440 declarations appearing in the dependent statement are out of scope
8441 after control passes that point. This function parses a statement,
8442 but ensures that is in its own scope, even if it is not a
8445 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8446 is a (possibly labeled) if statement which is not enclosed in
8447 braces and has an else clause. This is used to implement
8450 Returns the new statement. */
8453 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8460 /* Mark if () ; with a special NOP_EXPR. */
8461 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8463 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8464 cp_lexer_consume_token (parser->lexer);
8465 statement = add_stmt (build_empty_stmt (loc));
8467 /* if a compound is opened, we simply parse the statement directly. */
8468 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8469 statement = cp_parser_compound_statement (parser, NULL, false);
8470 /* If the token is not a `{', then we must take special action. */
8473 /* Create a compound-statement. */
8474 statement = begin_compound_stmt (0);
8475 /* Parse the dependent-statement. */
8476 cp_parser_statement (parser, NULL_TREE, false, if_p);
8477 /* Finish the dummy compound-statement. */
8478 finish_compound_stmt (statement);
8481 /* Return the statement. */
8485 /* For some dependent statements (like `while (cond) statement'), we
8486 have already created a scope. Therefore, even if the dependent
8487 statement is a compound-statement, we do not want to create another
8491 cp_parser_already_scoped_statement (cp_parser* parser)
8493 /* If the token is a `{', then we must take special action. */
8494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8495 cp_parser_statement (parser, NULL_TREE, false, NULL);
8498 /* Avoid calling cp_parser_compound_statement, so that we
8499 don't create a new scope. Do everything else by hand. */
8500 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8501 /* If the next keyword is `__label__' we have a label declaration. */
8502 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8503 cp_parser_label_declaration (parser);
8504 /* Parse an (optional) statement-seq. */
8505 cp_parser_statement_seq_opt (parser, NULL_TREE);
8506 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8510 /* Declarations [gram.dcl.dcl] */
8512 /* Parse an optional declaration-sequence.
8516 declaration-seq declaration */
8519 cp_parser_declaration_seq_opt (cp_parser* parser)
8525 token = cp_lexer_peek_token (parser->lexer);
8527 if (token->type == CPP_CLOSE_BRACE
8528 || token->type == CPP_EOF
8529 || token->type == CPP_PRAGMA_EOL)
8532 if (token->type == CPP_SEMICOLON)
8534 /* A declaration consisting of a single semicolon is
8535 invalid. Allow it unless we're being pedantic. */
8536 cp_lexer_consume_token (parser->lexer);
8537 if (!in_system_header)
8538 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8542 /* If we're entering or exiting a region that's implicitly
8543 extern "C", modify the lang context appropriately. */
8544 if (!parser->implicit_extern_c && token->implicit_extern_c)
8546 push_lang_context (lang_name_c);
8547 parser->implicit_extern_c = true;
8549 else if (parser->implicit_extern_c && !token->implicit_extern_c)
8551 pop_lang_context ();
8552 parser->implicit_extern_c = false;
8555 if (token->type == CPP_PRAGMA)
8557 /* A top-level declaration can consist solely of a #pragma.
8558 A nested declaration cannot, so this is done here and not
8559 in cp_parser_declaration. (A #pragma at block scope is
8560 handled in cp_parser_statement.) */
8561 cp_parser_pragma (parser, pragma_external);
8565 /* Parse the declaration itself. */
8566 cp_parser_declaration (parser);
8570 /* Parse a declaration.
8575 template-declaration
8576 explicit-instantiation
8577 explicit-specialization
8578 linkage-specification
8579 namespace-definition
8584 __extension__ declaration */
8587 cp_parser_declaration (cp_parser* parser)
8594 /* Check for the `__extension__' keyword. */
8595 if (cp_parser_extension_opt (parser, &saved_pedantic))
8597 /* Parse the qualified declaration. */
8598 cp_parser_declaration (parser);
8599 /* Restore the PEDANTIC flag. */
8600 pedantic = saved_pedantic;
8605 /* Try to figure out what kind of declaration is present. */
8606 token1 = *cp_lexer_peek_token (parser->lexer);
8608 if (token1.type != CPP_EOF)
8609 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8612 token2.type = CPP_EOF;
8613 token2.keyword = RID_MAX;
8616 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8617 p = obstack_alloc (&declarator_obstack, 0);
8619 /* If the next token is `extern' and the following token is a string
8620 literal, then we have a linkage specification. */
8621 if (token1.keyword == RID_EXTERN
8622 && cp_parser_is_string_literal (&token2))
8623 cp_parser_linkage_specification (parser);
8624 /* If the next token is `template', then we have either a template
8625 declaration, an explicit instantiation, or an explicit
8627 else if (token1.keyword == RID_TEMPLATE)
8629 /* `template <>' indicates a template specialization. */
8630 if (token2.type == CPP_LESS
8631 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8632 cp_parser_explicit_specialization (parser);
8633 /* `template <' indicates a template declaration. */
8634 else if (token2.type == CPP_LESS)
8635 cp_parser_template_declaration (parser, /*member_p=*/false);
8636 /* Anything else must be an explicit instantiation. */
8638 cp_parser_explicit_instantiation (parser);
8640 /* If the next token is `export', then we have a template
8642 else if (token1.keyword == RID_EXPORT)
8643 cp_parser_template_declaration (parser, /*member_p=*/false);
8644 /* If the next token is `extern', 'static' or 'inline' and the one
8645 after that is `template', we have a GNU extended explicit
8646 instantiation directive. */
8647 else if (cp_parser_allow_gnu_extensions_p (parser)
8648 && (token1.keyword == RID_EXTERN
8649 || token1.keyword == RID_STATIC
8650 || token1.keyword == RID_INLINE)
8651 && token2.keyword == RID_TEMPLATE)
8652 cp_parser_explicit_instantiation (parser);
8653 /* If the next token is `namespace', check for a named or unnamed
8654 namespace definition. */
8655 else if (token1.keyword == RID_NAMESPACE
8656 && (/* A named namespace definition. */
8657 (token2.type == CPP_NAME
8658 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8660 /* An unnamed namespace definition. */
8661 || token2.type == CPP_OPEN_BRACE
8662 || token2.keyword == RID_ATTRIBUTE))
8663 cp_parser_namespace_definition (parser);
8664 /* An inline (associated) namespace definition. */
8665 else if (token1.keyword == RID_INLINE
8666 && token2.keyword == RID_NAMESPACE)
8667 cp_parser_namespace_definition (parser);
8668 /* Objective-C++ declaration/definition. */
8669 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8670 cp_parser_objc_declaration (parser);
8671 /* We must have either a block declaration or a function
8674 /* Try to parse a block-declaration, or a function-definition. */
8675 cp_parser_block_declaration (parser, /*statement_p=*/false);
8677 /* Free any declarators allocated. */
8678 obstack_free (&declarator_obstack, p);
8681 /* Parse a block-declaration.
8686 namespace-alias-definition
8693 __extension__ block-declaration
8698 static_assert-declaration
8700 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8701 part of a declaration-statement. */
8704 cp_parser_block_declaration (cp_parser *parser,
8710 /* Check for the `__extension__' keyword. */
8711 if (cp_parser_extension_opt (parser, &saved_pedantic))
8713 /* Parse the qualified declaration. */
8714 cp_parser_block_declaration (parser, statement_p);
8715 /* Restore the PEDANTIC flag. */
8716 pedantic = saved_pedantic;
8721 /* Peek at the next token to figure out which kind of declaration is
8723 token1 = cp_lexer_peek_token (parser->lexer);
8725 /* If the next keyword is `asm', we have an asm-definition. */
8726 if (token1->keyword == RID_ASM)
8729 cp_parser_commit_to_tentative_parse (parser);
8730 cp_parser_asm_definition (parser);
8732 /* If the next keyword is `namespace', we have a
8733 namespace-alias-definition. */
8734 else if (token1->keyword == RID_NAMESPACE)
8735 cp_parser_namespace_alias_definition (parser);
8736 /* If the next keyword is `using', we have either a
8737 using-declaration or a using-directive. */
8738 else if (token1->keyword == RID_USING)
8743 cp_parser_commit_to_tentative_parse (parser);
8744 /* If the token after `using' is `namespace', then we have a
8746 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8747 if (token2->keyword == RID_NAMESPACE)
8748 cp_parser_using_directive (parser);
8749 /* Otherwise, it's a using-declaration. */
8751 cp_parser_using_declaration (parser,
8752 /*access_declaration_p=*/false);
8754 /* If the next keyword is `__label__' we have a misplaced label
8756 else if (token1->keyword == RID_LABEL)
8758 cp_lexer_consume_token (parser->lexer);
8759 error_at (token1->location, "%<__label__%> not at the beginning of a block");
8760 cp_parser_skip_to_end_of_statement (parser);
8761 /* If the next token is now a `;', consume it. */
8762 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8763 cp_lexer_consume_token (parser->lexer);
8765 /* If the next token is `static_assert' we have a static assertion. */
8766 else if (token1->keyword == RID_STATIC_ASSERT)
8767 cp_parser_static_assert (parser, /*member_p=*/false);
8768 /* Anything else must be a simple-declaration. */
8770 cp_parser_simple_declaration (parser, !statement_p);
8773 /* Parse a simple-declaration.
8776 decl-specifier-seq [opt] init-declarator-list [opt] ;
8778 init-declarator-list:
8780 init-declarator-list , init-declarator
8782 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8783 function-definition as a simple-declaration. */
8786 cp_parser_simple_declaration (cp_parser* parser,
8787 bool function_definition_allowed_p)
8789 cp_decl_specifier_seq decl_specifiers;
8790 int declares_class_or_enum;
8791 bool saw_declarator;
8793 /* Defer access checks until we know what is being declared; the
8794 checks for names appearing in the decl-specifier-seq should be
8795 done as if we were in the scope of the thing being declared. */
8796 push_deferring_access_checks (dk_deferred);
8798 /* Parse the decl-specifier-seq. We have to keep track of whether
8799 or not the decl-specifier-seq declares a named class or
8800 enumeration type, since that is the only case in which the
8801 init-declarator-list is allowed to be empty.
8805 In a simple-declaration, the optional init-declarator-list can be
8806 omitted only when declaring a class or enumeration, that is when
8807 the decl-specifier-seq contains either a class-specifier, an
8808 elaborated-type-specifier, or an enum-specifier. */
8809 cp_parser_decl_specifier_seq (parser,
8810 CP_PARSER_FLAGS_OPTIONAL,
8812 &declares_class_or_enum);
8813 /* We no longer need to defer access checks. */
8814 stop_deferring_access_checks ();
8816 /* In a block scope, a valid declaration must always have a
8817 decl-specifier-seq. By not trying to parse declarators, we can
8818 resolve the declaration/expression ambiguity more quickly. */
8819 if (!function_definition_allowed_p
8820 && !decl_specifiers.any_specifiers_p)
8822 cp_parser_error (parser, "expected declaration");
8826 /* If the next two tokens are both identifiers, the code is
8827 erroneous. The usual cause of this situation is code like:
8831 where "T" should name a type -- but does not. */
8832 if (!decl_specifiers.type
8833 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8835 /* If parsing tentatively, we should commit; we really are
8836 looking at a declaration. */
8837 cp_parser_commit_to_tentative_parse (parser);
8842 /* If we have seen at least one decl-specifier, and the next token
8843 is not a parenthesis, then we must be looking at a declaration.
8844 (After "int (" we might be looking at a functional cast.) */
8845 if (decl_specifiers.any_specifiers_p
8846 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8847 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8848 && !cp_parser_error_occurred (parser))
8849 cp_parser_commit_to_tentative_parse (parser);
8851 /* Keep going until we hit the `;' at the end of the simple
8853 saw_declarator = false;
8854 while (cp_lexer_next_token_is_not (parser->lexer,
8858 bool function_definition_p;
8863 /* If we are processing next declarator, coma is expected */
8864 token = cp_lexer_peek_token (parser->lexer);
8865 gcc_assert (token->type == CPP_COMMA);
8866 cp_lexer_consume_token (parser->lexer);
8869 saw_declarator = true;
8871 /* Parse the init-declarator. */
8872 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8874 function_definition_allowed_p,
8876 declares_class_or_enum,
8877 &function_definition_p);
8878 /* If an error occurred while parsing tentatively, exit quickly.
8879 (That usually happens when in the body of a function; each
8880 statement is treated as a declaration-statement until proven
8882 if (cp_parser_error_occurred (parser))
8884 /* Handle function definitions specially. */
8885 if (function_definition_p)
8887 /* If the next token is a `,', then we are probably
8888 processing something like:
8892 which is erroneous. */
8893 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8895 cp_token *token = cp_lexer_peek_token (parser->lexer);
8896 error_at (token->location,
8898 " declarations and function-definitions is forbidden");
8900 /* Otherwise, we're done with the list of declarators. */
8903 pop_deferring_access_checks ();
8907 /* The next token should be either a `,' or a `;'. */
8908 token = cp_lexer_peek_token (parser->lexer);
8909 /* If it's a `,', there are more declarators to come. */
8910 if (token->type == CPP_COMMA)
8911 /* will be consumed next time around */;
8912 /* If it's a `;', we are done. */
8913 else if (token->type == CPP_SEMICOLON)
8915 /* Anything else is an error. */
8918 /* If we have already issued an error message we don't need
8919 to issue another one. */
8920 if (decl != error_mark_node
8921 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8922 cp_parser_error (parser, "expected %<,%> or %<;%>");
8923 /* Skip tokens until we reach the end of the statement. */
8924 cp_parser_skip_to_end_of_statement (parser);
8925 /* If the next token is now a `;', consume it. */
8926 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8927 cp_lexer_consume_token (parser->lexer);
8930 /* After the first time around, a function-definition is not
8931 allowed -- even if it was OK at first. For example:
8936 function_definition_allowed_p = false;
8939 /* Issue an error message if no declarators are present, and the
8940 decl-specifier-seq does not itself declare a class or
8942 if (!saw_declarator)
8944 if (cp_parser_declares_only_class_p (parser))
8945 shadow_tag (&decl_specifiers);
8946 /* Perform any deferred access checks. */
8947 perform_deferred_access_checks ();
8950 /* Consume the `;'. */
8951 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8954 pop_deferring_access_checks ();
8957 /* Parse a decl-specifier-seq.
8960 decl-specifier-seq [opt] decl-specifier
8963 storage-class-specifier
8974 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8976 The parser flags FLAGS is used to control type-specifier parsing.
8978 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8981 1: one of the decl-specifiers is an elaborated-type-specifier
8982 (i.e., a type declaration)
8983 2: one of the decl-specifiers is an enum-specifier or a
8984 class-specifier (i.e., a type definition)
8989 cp_parser_decl_specifier_seq (cp_parser* parser,
8990 cp_parser_flags flags,
8991 cp_decl_specifier_seq *decl_specs,
8992 int* declares_class_or_enum)
8994 bool constructor_possible_p = !parser->in_declarator_p;
8995 cp_token *start_token = NULL;
8997 /* Clear DECL_SPECS. */
8998 clear_decl_specs (decl_specs);
9000 /* Assume no class or enumeration type is declared. */
9001 *declares_class_or_enum = 0;
9003 /* Keep reading specifiers until there are no more to read. */
9007 bool found_decl_spec;
9010 /* Peek at the next token. */
9011 token = cp_lexer_peek_token (parser->lexer);
9013 /* Save the first token of the decl spec list for error
9016 start_token = token;
9017 /* Handle attributes. */
9018 if (token->keyword == RID_ATTRIBUTE)
9020 /* Parse the attributes. */
9021 decl_specs->attributes
9022 = chainon (decl_specs->attributes,
9023 cp_parser_attributes_opt (parser));
9026 /* Assume we will find a decl-specifier keyword. */
9027 found_decl_spec = true;
9028 /* If the next token is an appropriate keyword, we can simply
9029 add it to the list. */
9030 switch (token->keyword)
9035 if (!at_class_scope_p ())
9037 error_at (token->location, "%<friend%> used outside of class");
9038 cp_lexer_purge_token (parser->lexer);
9042 ++decl_specs->specs[(int) ds_friend];
9043 /* Consume the token. */
9044 cp_lexer_consume_token (parser->lexer);
9048 /* function-specifier:
9055 cp_parser_function_specifier_opt (parser, decl_specs);
9061 ++decl_specs->specs[(int) ds_typedef];
9062 /* Consume the token. */
9063 cp_lexer_consume_token (parser->lexer);
9064 /* A constructor declarator cannot appear in a typedef. */
9065 constructor_possible_p = false;
9066 /* The "typedef" keyword can only occur in a declaration; we
9067 may as well commit at this point. */
9068 cp_parser_commit_to_tentative_parse (parser);
9070 if (decl_specs->storage_class != sc_none)
9071 decl_specs->conflicting_specifiers_p = true;
9074 /* storage-class-specifier:
9084 if (cxx_dialect == cxx98)
9086 /* Consume the token. */
9087 cp_lexer_consume_token (parser->lexer);
9089 /* Complain about `auto' as a storage specifier, if
9090 we're complaining about C++0x compatibility. */
9091 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9092 " will change meaning in C++0x; please remove it");
9094 /* Set the storage class anyway. */
9095 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9099 /* C++0x auto type-specifier. */
9100 found_decl_spec = false;
9107 /* Consume the token. */
9108 cp_lexer_consume_token (parser->lexer);
9109 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9113 /* Consume the token. */
9114 cp_lexer_consume_token (parser->lexer);
9115 ++decl_specs->specs[(int) ds_thread];
9119 /* We did not yet find a decl-specifier yet. */
9120 found_decl_spec = false;
9124 /* Constructors are a special case. The `S' in `S()' is not a
9125 decl-specifier; it is the beginning of the declarator. */
9128 && constructor_possible_p
9129 && (cp_parser_constructor_declarator_p
9130 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9132 /* If we don't have a DECL_SPEC yet, then we must be looking at
9133 a type-specifier. */
9134 if (!found_decl_spec && !constructor_p)
9136 int decl_spec_declares_class_or_enum;
9137 bool is_cv_qualifier;
9141 = cp_parser_type_specifier (parser, flags,
9143 /*is_declaration=*/true,
9144 &decl_spec_declares_class_or_enum,
9146 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9148 /* If this type-specifier referenced a user-defined type
9149 (a typedef, class-name, etc.), then we can't allow any
9150 more such type-specifiers henceforth.
9154 The longest sequence of decl-specifiers that could
9155 possibly be a type name is taken as the
9156 decl-specifier-seq of a declaration. The sequence shall
9157 be self-consistent as described below.
9161 As a general rule, at most one type-specifier is allowed
9162 in the complete decl-specifier-seq of a declaration. The
9163 only exceptions are the following:
9165 -- const or volatile can be combined with any other
9168 -- signed or unsigned can be combined with char, long,
9176 void g (const int Pc);
9178 Here, Pc is *not* part of the decl-specifier seq; it's
9179 the declarator. Therefore, once we see a type-specifier
9180 (other than a cv-qualifier), we forbid any additional
9181 user-defined types. We *do* still allow things like `int
9182 int' to be considered a decl-specifier-seq, and issue the
9183 error message later. */
9184 if (type_spec && !is_cv_qualifier)
9185 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9186 /* A constructor declarator cannot follow a type-specifier. */
9189 constructor_possible_p = false;
9190 found_decl_spec = true;
9194 /* If we still do not have a DECL_SPEC, then there are no more
9196 if (!found_decl_spec)
9199 decl_specs->any_specifiers_p = true;
9200 /* After we see one decl-specifier, further decl-specifiers are
9202 flags |= CP_PARSER_FLAGS_OPTIONAL;
9205 cp_parser_check_decl_spec (decl_specs, start_token->location);
9207 /* Don't allow a friend specifier with a class definition. */
9208 if (decl_specs->specs[(int) ds_friend] != 0
9209 && (*declares_class_or_enum & 2))
9210 error_at (start_token->location,
9211 "class definition may not be declared a friend");
9214 /* Parse an (optional) storage-class-specifier.
9216 storage-class-specifier:
9225 storage-class-specifier:
9228 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9231 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9233 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9236 if (cxx_dialect != cxx98)
9238 /* Fall through for C++98. */
9245 /* Consume the token. */
9246 return cp_lexer_consume_token (parser->lexer)->u.value;
9253 /* Parse an (optional) function-specifier.
9260 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9261 Updates DECL_SPECS, if it is non-NULL. */
9264 cp_parser_function_specifier_opt (cp_parser* parser,
9265 cp_decl_specifier_seq *decl_specs)
9267 cp_token *token = cp_lexer_peek_token (parser->lexer);
9268 switch (token->keyword)
9272 ++decl_specs->specs[(int) ds_inline];
9276 /* 14.5.2.3 [temp.mem]
9278 A member function template shall not be virtual. */
9279 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9280 error_at (token->location, "templates may not be %<virtual%>");
9281 else if (decl_specs)
9282 ++decl_specs->specs[(int) ds_virtual];
9287 ++decl_specs->specs[(int) ds_explicit];
9294 /* Consume the token. */
9295 return cp_lexer_consume_token (parser->lexer)->u.value;
9298 /* Parse a linkage-specification.
9300 linkage-specification:
9301 extern string-literal { declaration-seq [opt] }
9302 extern string-literal declaration */
9305 cp_parser_linkage_specification (cp_parser* parser)
9309 /* Look for the `extern' keyword. */
9310 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9312 /* Look for the string-literal. */
9313 linkage = cp_parser_string_literal (parser, false, false);
9315 /* Transform the literal into an identifier. If the literal is a
9316 wide-character string, or contains embedded NULs, then we can't
9317 handle it as the user wants. */
9318 if (strlen (TREE_STRING_POINTER (linkage))
9319 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9321 cp_parser_error (parser, "invalid linkage-specification");
9322 /* Assume C++ linkage. */
9323 linkage = lang_name_cplusplus;
9326 linkage = get_identifier (TREE_STRING_POINTER (linkage));
9328 /* We're now using the new linkage. */
9329 push_lang_context (linkage);
9331 /* If the next token is a `{', then we're using the first
9333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9335 /* Consume the `{' token. */
9336 cp_lexer_consume_token (parser->lexer);
9337 /* Parse the declarations. */
9338 cp_parser_declaration_seq_opt (parser);
9339 /* Look for the closing `}'. */
9340 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9342 /* Otherwise, there's just one declaration. */
9345 bool saved_in_unbraced_linkage_specification_p;
9347 saved_in_unbraced_linkage_specification_p
9348 = parser->in_unbraced_linkage_specification_p;
9349 parser->in_unbraced_linkage_specification_p = true;
9350 cp_parser_declaration (parser);
9351 parser->in_unbraced_linkage_specification_p
9352 = saved_in_unbraced_linkage_specification_p;
9355 /* We're done with the linkage-specification. */
9356 pop_lang_context ();
9359 /* Parse a static_assert-declaration.
9361 static_assert-declaration:
9362 static_assert ( constant-expression , string-literal ) ;
9364 If MEMBER_P, this static_assert is a class member. */
9367 cp_parser_static_assert(cp_parser *parser, bool member_p)
9372 location_t saved_loc;
9374 /* Peek at the `static_assert' token so we can keep track of exactly
9375 where the static assertion started. */
9376 token = cp_lexer_peek_token (parser->lexer);
9377 saved_loc = token->location;
9379 /* Look for the `static_assert' keyword. */
9380 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
9381 "%<static_assert%>"))
9384 /* We know we are in a static assertion; commit to any tentative
9386 if (cp_parser_parsing_tentatively (parser))
9387 cp_parser_commit_to_tentative_parse (parser);
9389 /* Parse the `(' starting the static assertion condition. */
9390 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9392 /* Parse the constant-expression. */
9394 cp_parser_constant_expression (parser,
9395 /*allow_non_constant_p=*/false,
9396 /*non_constant_p=*/NULL);
9398 /* Parse the separating `,'. */
9399 cp_parser_require (parser, CPP_COMMA, "%<,%>");
9401 /* Parse the string-literal message. */
9402 message = cp_parser_string_literal (parser,
9403 /*translate=*/false,
9406 /* A `)' completes the static assertion. */
9407 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9408 cp_parser_skip_to_closing_parenthesis (parser,
9409 /*recovering=*/true,
9411 /*consume_paren=*/true);
9413 /* A semicolon terminates the declaration. */
9414 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9416 /* Complete the static assertion, which may mean either processing
9417 the static assert now or saving it for template instantiation. */
9418 finish_static_assert (condition, message, saved_loc, member_p);
9421 /* Parse a `decltype' type. Returns the type.
9423 simple-type-specifier:
9424 decltype ( expression ) */
9427 cp_parser_decltype (cp_parser *parser)
9430 bool id_expression_or_member_access_p = false;
9431 const char *saved_message;
9432 bool saved_integral_constant_expression_p;
9433 bool saved_non_integral_constant_expression_p;
9434 cp_token *id_expr_start_token;
9436 /* Look for the `decltype' token. */
9437 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9438 return error_mark_node;
9440 /* Types cannot be defined in a `decltype' expression. Save away the
9442 saved_message = parser->type_definition_forbidden_message;
9444 /* And create the new one. */
9445 parser->type_definition_forbidden_message
9446 = "types may not be defined in %<decltype%> expressions";
9448 /* The restrictions on constant-expressions do not apply inside
9449 decltype expressions. */
9450 saved_integral_constant_expression_p
9451 = parser->integral_constant_expression_p;
9452 saved_non_integral_constant_expression_p
9453 = parser->non_integral_constant_expression_p;
9454 parser->integral_constant_expression_p = false;
9456 /* Do not actually evaluate the expression. */
9457 ++cp_unevaluated_operand;
9459 /* Do not warn about problems with the expression. */
9460 ++c_inhibit_evaluation_warnings;
9462 /* Parse the opening `('. */
9463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9464 return error_mark_node;
9466 /* First, try parsing an id-expression. */
9467 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9468 cp_parser_parse_tentatively (parser);
9469 expr = cp_parser_id_expression (parser,
9470 /*template_keyword_p=*/false,
9471 /*check_dependency_p=*/true,
9472 /*template_p=*/NULL,
9473 /*declarator_p=*/false,
9474 /*optional_p=*/false);
9476 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9478 bool non_integral_constant_expression_p = false;
9479 tree id_expression = expr;
9481 const char *error_msg;
9483 if (TREE_CODE (expr) == IDENTIFIER_NODE)
9484 /* Lookup the name we got back from the id-expression. */
9485 expr = cp_parser_lookup_name (parser, expr,
9487 /*is_template=*/false,
9488 /*is_namespace=*/false,
9489 /*check_dependency=*/true,
9490 /*ambiguous_decls=*/NULL,
9491 id_expr_start_token->location);
9494 && expr != error_mark_node
9495 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9496 && TREE_CODE (expr) != TYPE_DECL
9497 && (TREE_CODE (expr) != BIT_NOT_EXPR
9498 || !TYPE_P (TREE_OPERAND (expr, 0)))
9499 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9501 /* Complete lookup of the id-expression. */
9502 expr = (finish_id_expression
9503 (id_expression, expr, parser->scope, &idk,
9504 /*integral_constant_expression_p=*/false,
9505 /*allow_non_integral_constant_expression_p=*/true,
9506 &non_integral_constant_expression_p,
9507 /*template_p=*/false,
9509 /*address_p=*/false,
9510 /*template_arg_p=*/false,
9512 id_expr_start_token->location));
9514 if (expr == error_mark_node)
9515 /* We found an id-expression, but it was something that we
9516 should not have found. This is an error, not something
9517 we can recover from, so note that we found an
9518 id-expression and we'll recover as gracefully as
9520 id_expression_or_member_access_p = true;
9524 && expr != error_mark_node
9525 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9526 /* We have an id-expression. */
9527 id_expression_or_member_access_p = true;
9530 if (!id_expression_or_member_access_p)
9532 /* Abort the id-expression parse. */
9533 cp_parser_abort_tentative_parse (parser);
9535 /* Parsing tentatively, again. */
9536 cp_parser_parse_tentatively (parser);
9538 /* Parse a class member access. */
9539 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9541 /*member_access_only_p=*/true, NULL);
9544 && expr != error_mark_node
9545 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9546 /* We have an id-expression. */
9547 id_expression_or_member_access_p = true;
9550 if (id_expression_or_member_access_p)
9551 /* We have parsed the complete id-expression or member access. */
9552 cp_parser_parse_definitely (parser);
9555 /* Abort our attempt to parse an id-expression or member access
9557 cp_parser_abort_tentative_parse (parser);
9559 /* Parse a full expression. */
9560 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9563 /* Go back to evaluating expressions. */
9564 --cp_unevaluated_operand;
9565 --c_inhibit_evaluation_warnings;
9567 /* Restore the old message and the integral constant expression
9569 parser->type_definition_forbidden_message = saved_message;
9570 parser->integral_constant_expression_p
9571 = saved_integral_constant_expression_p;
9572 parser->non_integral_constant_expression_p
9573 = saved_non_integral_constant_expression_p;
9575 if (expr == error_mark_node)
9577 /* Skip everything up to the closing `)'. */
9578 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9579 /*consume_paren=*/true);
9580 return error_mark_node;
9583 /* Parse to the closing `)'. */
9584 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9586 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9587 /*consume_paren=*/true);
9588 return error_mark_node;
9591 return finish_decltype_type (expr, id_expression_or_member_access_p);
9594 /* Special member functions [gram.special] */
9596 /* Parse a conversion-function-id.
9598 conversion-function-id:
9599 operator conversion-type-id
9601 Returns an IDENTIFIER_NODE representing the operator. */
9604 cp_parser_conversion_function_id (cp_parser* parser)
9608 tree saved_qualifying_scope;
9609 tree saved_object_scope;
9610 tree pushed_scope = NULL_TREE;
9612 /* Look for the `operator' token. */
9613 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9614 return error_mark_node;
9615 /* When we parse the conversion-type-id, the current scope will be
9616 reset. However, we need that information in able to look up the
9617 conversion function later, so we save it here. */
9618 saved_scope = parser->scope;
9619 saved_qualifying_scope = parser->qualifying_scope;
9620 saved_object_scope = parser->object_scope;
9621 /* We must enter the scope of the class so that the names of
9622 entities declared within the class are available in the
9623 conversion-type-id. For example, consider:
9630 S::operator I() { ... }
9632 In order to see that `I' is a type-name in the definition, we
9633 must be in the scope of `S'. */
9635 pushed_scope = push_scope (saved_scope);
9636 /* Parse the conversion-type-id. */
9637 type = cp_parser_conversion_type_id (parser);
9638 /* Leave the scope of the class, if any. */
9640 pop_scope (pushed_scope);
9641 /* Restore the saved scope. */
9642 parser->scope = saved_scope;
9643 parser->qualifying_scope = saved_qualifying_scope;
9644 parser->object_scope = saved_object_scope;
9645 /* If the TYPE is invalid, indicate failure. */
9646 if (type == error_mark_node)
9647 return error_mark_node;
9648 return mangle_conv_op_name_for_type (type);
9651 /* Parse a conversion-type-id:
9654 type-specifier-seq conversion-declarator [opt]
9656 Returns the TYPE specified. */
9659 cp_parser_conversion_type_id (cp_parser* parser)
9662 cp_decl_specifier_seq type_specifiers;
9663 cp_declarator *declarator;
9664 tree type_specified;
9666 /* Parse the attributes. */
9667 attributes = cp_parser_attributes_opt (parser);
9668 /* Parse the type-specifiers. */
9669 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9671 /* If that didn't work, stop. */
9672 if (type_specifiers.type == error_mark_node)
9673 return error_mark_node;
9674 /* Parse the conversion-declarator. */
9675 declarator = cp_parser_conversion_declarator_opt (parser);
9677 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9678 /*initialized=*/0, &attributes);
9680 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9682 /* Don't give this error when parsing tentatively. This happens to
9683 work because we always parse this definitively once. */
9684 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9685 && type_uses_auto (type_specified))
9687 error ("invalid use of %<auto%> in conversion operator");
9688 return error_mark_node;
9691 return type_specified;
9694 /* Parse an (optional) conversion-declarator.
9696 conversion-declarator:
9697 ptr-operator conversion-declarator [opt]
9701 static cp_declarator *
9702 cp_parser_conversion_declarator_opt (cp_parser* parser)
9704 enum tree_code code;
9706 cp_cv_quals cv_quals;
9708 /* We don't know if there's a ptr-operator next, or not. */
9709 cp_parser_parse_tentatively (parser);
9710 /* Try the ptr-operator. */
9711 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9712 /* If it worked, look for more conversion-declarators. */
9713 if (cp_parser_parse_definitely (parser))
9715 cp_declarator *declarator;
9717 /* Parse another optional declarator. */
9718 declarator = cp_parser_conversion_declarator_opt (parser);
9720 return cp_parser_make_indirect_declarator
9721 (code, class_type, cv_quals, declarator);
9727 /* Parse an (optional) ctor-initializer.
9730 : mem-initializer-list
9732 Returns TRUE iff the ctor-initializer was actually present. */
9735 cp_parser_ctor_initializer_opt (cp_parser* parser)
9737 /* If the next token is not a `:', then there is no
9738 ctor-initializer. */
9739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9741 /* Do default initialization of any bases and members. */
9742 if (DECL_CONSTRUCTOR_P (current_function_decl))
9743 finish_mem_initializers (NULL_TREE);
9748 /* Consume the `:' token. */
9749 cp_lexer_consume_token (parser->lexer);
9750 /* And the mem-initializer-list. */
9751 cp_parser_mem_initializer_list (parser);
9756 /* Parse a mem-initializer-list.
9758 mem-initializer-list:
9759 mem-initializer ... [opt]
9760 mem-initializer ... [opt] , mem-initializer-list */
9763 cp_parser_mem_initializer_list (cp_parser* parser)
9765 tree mem_initializer_list = NULL_TREE;
9766 cp_token *token = cp_lexer_peek_token (parser->lexer);
9768 /* Let the semantic analysis code know that we are starting the
9769 mem-initializer-list. */
9770 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9771 error_at (token->location,
9772 "only constructors take base initializers");
9774 /* Loop through the list. */
9777 tree mem_initializer;
9779 token = cp_lexer_peek_token (parser->lexer);
9780 /* Parse the mem-initializer. */
9781 mem_initializer = cp_parser_mem_initializer (parser);
9782 /* If the next token is a `...', we're expanding member initializers. */
9783 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9785 /* Consume the `...'. */
9786 cp_lexer_consume_token (parser->lexer);
9788 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9789 can be expanded but members cannot. */
9790 if (mem_initializer != error_mark_node
9791 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9793 error_at (token->location,
9794 "cannot expand initializer for member %<%D%>",
9795 TREE_PURPOSE (mem_initializer));
9796 mem_initializer = error_mark_node;
9799 /* Construct the pack expansion type. */
9800 if (mem_initializer != error_mark_node)
9801 mem_initializer = make_pack_expansion (mem_initializer);
9803 /* Add it to the list, unless it was erroneous. */
9804 if (mem_initializer != error_mark_node)
9806 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9807 mem_initializer_list = mem_initializer;
9809 /* If the next token is not a `,', we're done. */
9810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9812 /* Consume the `,' token. */
9813 cp_lexer_consume_token (parser->lexer);
9816 /* Perform semantic analysis. */
9817 if (DECL_CONSTRUCTOR_P (current_function_decl))
9818 finish_mem_initializers (mem_initializer_list);
9821 /* Parse a mem-initializer.
9824 mem-initializer-id ( expression-list [opt] )
9825 mem-initializer-id braced-init-list
9830 ( expression-list [opt] )
9832 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9833 class) or FIELD_DECL (for a non-static data member) to initialize;
9834 the TREE_VALUE is the expression-list. An empty initialization
9835 list is represented by void_list_node. */
9838 cp_parser_mem_initializer (cp_parser* parser)
9840 tree mem_initializer_id;
9841 tree expression_list;
9843 cp_token *token = cp_lexer_peek_token (parser->lexer);
9845 /* Find out what is being initialized. */
9846 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9848 permerror (token->location,
9849 "anachronistic old-style base class initializer");
9850 mem_initializer_id = NULL_TREE;
9854 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9855 if (mem_initializer_id == error_mark_node)
9856 return mem_initializer_id;
9858 member = expand_member_init (mem_initializer_id);
9859 if (member && !DECL_P (member))
9860 in_base_initializer = 1;
9862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9864 bool expr_non_constant_p;
9865 maybe_warn_cpp0x ("extended initializer lists");
9866 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9867 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9868 expression_list = build_tree_list (NULL_TREE, expression_list);
9873 vec = cp_parser_parenthesized_expression_list (parser, false,
9875 /*allow_expansion_p=*/true,
9876 /*non_constant_p=*/NULL);
9878 return error_mark_node;
9879 expression_list = build_tree_list_vec (vec);
9880 release_tree_vector (vec);
9883 if (expression_list == error_mark_node)
9884 return error_mark_node;
9885 if (!expression_list)
9886 expression_list = void_type_node;
9888 in_base_initializer = 0;
9890 return member ? build_tree_list (member, expression_list) : error_mark_node;
9893 /* Parse a mem-initializer-id.
9896 :: [opt] nested-name-specifier [opt] class-name
9899 Returns a TYPE indicating the class to be initializer for the first
9900 production. Returns an IDENTIFIER_NODE indicating the data member
9901 to be initialized for the second production. */
9904 cp_parser_mem_initializer_id (cp_parser* parser)
9906 bool global_scope_p;
9907 bool nested_name_specifier_p;
9908 bool template_p = false;
9911 cp_token *token = cp_lexer_peek_token (parser->lexer);
9913 /* `typename' is not allowed in this context ([temp.res]). */
9914 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9916 error_at (token->location,
9917 "keyword %<typename%> not allowed in this context (a qualified "
9918 "member initializer is implicitly a type)");
9919 cp_lexer_consume_token (parser->lexer);
9921 /* Look for the optional `::' operator. */
9923 = (cp_parser_global_scope_opt (parser,
9924 /*current_scope_valid_p=*/false)
9926 /* Look for the optional nested-name-specifier. The simplest way to
9931 The keyword `typename' is not permitted in a base-specifier or
9932 mem-initializer; in these contexts a qualified name that
9933 depends on a template-parameter is implicitly assumed to be a
9936 is to assume that we have seen the `typename' keyword at this
9938 nested_name_specifier_p
9939 = (cp_parser_nested_name_specifier_opt (parser,
9940 /*typename_keyword_p=*/true,
9941 /*check_dependency_p=*/true,
9943 /*is_declaration=*/true)
9945 if (nested_name_specifier_p)
9946 template_p = cp_parser_optional_template_keyword (parser);
9947 /* If there is a `::' operator or a nested-name-specifier, then we
9948 are definitely looking for a class-name. */
9949 if (global_scope_p || nested_name_specifier_p)
9950 return cp_parser_class_name (parser,
9951 /*typename_keyword_p=*/true,
9952 /*template_keyword_p=*/template_p,
9954 /*check_dependency_p=*/true,
9955 /*class_head_p=*/false,
9956 /*is_declaration=*/true);
9957 /* Otherwise, we could also be looking for an ordinary identifier. */
9958 cp_parser_parse_tentatively (parser);
9959 /* Try a class-name. */
9960 id = cp_parser_class_name (parser,
9961 /*typename_keyword_p=*/true,
9962 /*template_keyword_p=*/false,
9964 /*check_dependency_p=*/true,
9965 /*class_head_p=*/false,
9966 /*is_declaration=*/true);
9967 /* If we found one, we're done. */
9968 if (cp_parser_parse_definitely (parser))
9970 /* Otherwise, look for an ordinary identifier. */
9971 return cp_parser_identifier (parser);
9974 /* Overloading [gram.over] */
9976 /* Parse an operator-function-id.
9978 operator-function-id:
9981 Returns an IDENTIFIER_NODE for the operator which is a
9982 human-readable spelling of the identifier, e.g., `operator +'. */
9985 cp_parser_operator_function_id (cp_parser* parser)
9987 /* Look for the `operator' keyword. */
9988 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9989 return error_mark_node;
9990 /* And then the name of the operator itself. */
9991 return cp_parser_operator (parser);
9994 /* Parse an operator.
9997 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9998 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9999 || ++ -- , ->* -> () []
10006 Returns an IDENTIFIER_NODE for the operator which is a
10007 human-readable spelling of the identifier, e.g., `operator +'. */
10010 cp_parser_operator (cp_parser* parser)
10012 tree id = NULL_TREE;
10015 /* Peek at the next token. */
10016 token = cp_lexer_peek_token (parser->lexer);
10017 /* Figure out which operator we have. */
10018 switch (token->type)
10024 /* The keyword should be either `new' or `delete'. */
10025 if (token->keyword == RID_NEW)
10027 else if (token->keyword == RID_DELETE)
10032 /* Consume the `new' or `delete' token. */
10033 cp_lexer_consume_token (parser->lexer);
10035 /* Peek at the next token. */
10036 token = cp_lexer_peek_token (parser->lexer);
10037 /* If it's a `[' token then this is the array variant of the
10039 if (token->type == CPP_OPEN_SQUARE)
10041 /* Consume the `[' token. */
10042 cp_lexer_consume_token (parser->lexer);
10043 /* Look for the `]' token. */
10044 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10045 id = ansi_opname (op == NEW_EXPR
10046 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10048 /* Otherwise, we have the non-array variant. */
10050 id = ansi_opname (op);
10056 id = ansi_opname (PLUS_EXPR);
10060 id = ansi_opname (MINUS_EXPR);
10064 id = ansi_opname (MULT_EXPR);
10068 id = ansi_opname (TRUNC_DIV_EXPR);
10072 id = ansi_opname (TRUNC_MOD_EXPR);
10076 id = ansi_opname (BIT_XOR_EXPR);
10080 id = ansi_opname (BIT_AND_EXPR);
10084 id = ansi_opname (BIT_IOR_EXPR);
10088 id = ansi_opname (BIT_NOT_EXPR);
10092 id = ansi_opname (TRUTH_NOT_EXPR);
10096 id = ansi_assopname (NOP_EXPR);
10100 id = ansi_opname (LT_EXPR);
10104 id = ansi_opname (GT_EXPR);
10108 id = ansi_assopname (PLUS_EXPR);
10112 id = ansi_assopname (MINUS_EXPR);
10116 id = ansi_assopname (MULT_EXPR);
10120 id = ansi_assopname (TRUNC_DIV_EXPR);
10124 id = ansi_assopname (TRUNC_MOD_EXPR);
10128 id = ansi_assopname (BIT_XOR_EXPR);
10132 id = ansi_assopname (BIT_AND_EXPR);
10136 id = ansi_assopname (BIT_IOR_EXPR);
10140 id = ansi_opname (LSHIFT_EXPR);
10144 id = ansi_opname (RSHIFT_EXPR);
10147 case CPP_LSHIFT_EQ:
10148 id = ansi_assopname (LSHIFT_EXPR);
10151 case CPP_RSHIFT_EQ:
10152 id = ansi_assopname (RSHIFT_EXPR);
10156 id = ansi_opname (EQ_EXPR);
10160 id = ansi_opname (NE_EXPR);
10164 id = ansi_opname (LE_EXPR);
10167 case CPP_GREATER_EQ:
10168 id = ansi_opname (GE_EXPR);
10172 id = ansi_opname (TRUTH_ANDIF_EXPR);
10176 id = ansi_opname (TRUTH_ORIF_EXPR);
10179 case CPP_PLUS_PLUS:
10180 id = ansi_opname (POSTINCREMENT_EXPR);
10183 case CPP_MINUS_MINUS:
10184 id = ansi_opname (PREDECREMENT_EXPR);
10188 id = ansi_opname (COMPOUND_EXPR);
10191 case CPP_DEREF_STAR:
10192 id = ansi_opname (MEMBER_REF);
10196 id = ansi_opname (COMPONENT_REF);
10199 case CPP_OPEN_PAREN:
10200 /* Consume the `('. */
10201 cp_lexer_consume_token (parser->lexer);
10202 /* Look for the matching `)'. */
10203 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10204 return ansi_opname (CALL_EXPR);
10206 case CPP_OPEN_SQUARE:
10207 /* Consume the `['. */
10208 cp_lexer_consume_token (parser->lexer);
10209 /* Look for the matching `]'. */
10210 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10211 return ansi_opname (ARRAY_REF);
10214 /* Anything else is an error. */
10218 /* If we have selected an identifier, we need to consume the
10221 cp_lexer_consume_token (parser->lexer);
10222 /* Otherwise, no valid operator name was present. */
10225 cp_parser_error (parser, "expected operator");
10226 id = error_mark_node;
10232 /* Parse a template-declaration.
10234 template-declaration:
10235 export [opt] template < template-parameter-list > declaration
10237 If MEMBER_P is TRUE, this template-declaration occurs within a
10240 The grammar rule given by the standard isn't correct. What
10241 is really meant is:
10243 template-declaration:
10244 export [opt] template-parameter-list-seq
10245 decl-specifier-seq [opt] init-declarator [opt] ;
10246 export [opt] template-parameter-list-seq
10247 function-definition
10249 template-parameter-list-seq:
10250 template-parameter-list-seq [opt]
10251 template < template-parameter-list > */
10254 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10256 /* Check for `export'. */
10257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10259 /* Consume the `export' token. */
10260 cp_lexer_consume_token (parser->lexer);
10261 /* Warn that we do not support `export'. */
10262 warning (0, "keyword %<export%> not implemented, and will be ignored");
10265 cp_parser_template_declaration_after_export (parser, member_p);
10268 /* Parse a template-parameter-list.
10270 template-parameter-list:
10272 template-parameter-list , template-parameter
10274 Returns a TREE_LIST. Each node represents a template parameter.
10275 The nodes are connected via their TREE_CHAINs. */
10278 cp_parser_template_parameter_list (cp_parser* parser)
10280 tree parameter_list = NULL_TREE;
10282 begin_template_parm_list ();
10287 bool is_parameter_pack;
10288 location_t parm_loc;
10290 /* Parse the template-parameter. */
10291 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10292 parameter = cp_parser_template_parameter (parser,
10294 &is_parameter_pack);
10295 /* Add it to the list. */
10296 if (parameter != error_mark_node)
10297 parameter_list = process_template_parm (parameter_list,
10301 is_parameter_pack);
10304 tree err_parm = build_tree_list (parameter, parameter);
10305 TREE_VALUE (err_parm) = error_mark_node;
10306 parameter_list = chainon (parameter_list, err_parm);
10309 /* If the next token is not a `,', we're done. */
10310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10312 /* Otherwise, consume the `,' token. */
10313 cp_lexer_consume_token (parser->lexer);
10316 return end_template_parm_list (parameter_list);
10319 /* Parse a template-parameter.
10321 template-parameter:
10323 parameter-declaration
10325 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
10326 the parameter. The TREE_PURPOSE is the default value, if any.
10327 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
10328 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
10329 set to true iff this parameter is a parameter pack. */
10332 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10333 bool *is_parameter_pack)
10336 cp_parameter_declarator *parameter_declarator;
10337 cp_declarator *id_declarator;
10340 /* Assume it is a type parameter or a template parameter. */
10341 *is_non_type = false;
10342 /* Assume it not a parameter pack. */
10343 *is_parameter_pack = false;
10344 /* Peek at the next token. */
10345 token = cp_lexer_peek_token (parser->lexer);
10346 /* If it is `class' or `template', we have a type-parameter. */
10347 if (token->keyword == RID_TEMPLATE)
10348 return cp_parser_type_parameter (parser, is_parameter_pack);
10349 /* If it is `class' or `typename' we do not know yet whether it is a
10350 type parameter or a non-type parameter. Consider:
10352 template <typename T, typename T::X X> ...
10356 template <class C, class D*> ...
10358 Here, the first parameter is a type parameter, and the second is
10359 a non-type parameter. We can tell by looking at the token after
10360 the identifier -- if it is a `,', `=', or `>' then we have a type
10362 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10364 /* Peek at the token after `class' or `typename'. */
10365 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10366 /* If it's an ellipsis, we have a template type parameter
10368 if (token->type == CPP_ELLIPSIS)
10369 return cp_parser_type_parameter (parser, is_parameter_pack);
10370 /* If it's an identifier, skip it. */
10371 if (token->type == CPP_NAME)
10372 token = cp_lexer_peek_nth_token (parser->lexer, 3);
10373 /* Now, see if the token looks like the end of a template
10375 if (token->type == CPP_COMMA
10376 || token->type == CPP_EQ
10377 || token->type == CPP_GREATER)
10378 return cp_parser_type_parameter (parser, is_parameter_pack);
10381 /* Otherwise, it is a non-type parameter.
10385 When parsing a default template-argument for a non-type
10386 template-parameter, the first non-nested `>' is taken as the end
10387 of the template parameter-list rather than a greater-than
10389 *is_non_type = true;
10390 parameter_declarator
10391 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10392 /*parenthesized_p=*/NULL);
10394 /* If the parameter declaration is marked as a parameter pack, set
10395 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10396 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10398 if (parameter_declarator
10399 && parameter_declarator->declarator
10400 && parameter_declarator->declarator->parameter_pack_p)
10402 *is_parameter_pack = true;
10403 parameter_declarator->declarator->parameter_pack_p = false;
10406 /* If the next token is an ellipsis, and we don't already have it
10407 marked as a parameter pack, then we have a parameter pack (that
10408 has no declarator). */
10409 if (!*is_parameter_pack
10410 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10411 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10413 /* Consume the `...'. */
10414 cp_lexer_consume_token (parser->lexer);
10415 maybe_warn_variadic_templates ();
10417 *is_parameter_pack = true;
10419 /* We might end up with a pack expansion as the type of the non-type
10420 template parameter, in which case this is a non-type template
10422 else if (parameter_declarator
10423 && parameter_declarator->decl_specifiers.type
10424 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10426 *is_parameter_pack = true;
10427 parameter_declarator->decl_specifiers.type =
10428 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10431 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10433 /* Parameter packs cannot have default arguments. However, a
10434 user may try to do so, so we'll parse them and give an
10435 appropriate diagnostic here. */
10437 /* Consume the `='. */
10438 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10439 cp_lexer_consume_token (parser->lexer);
10441 /* Find the name of the parameter pack. */
10442 id_declarator = parameter_declarator->declarator;
10443 while (id_declarator && id_declarator->kind != cdk_id)
10444 id_declarator = id_declarator->declarator;
10446 if (id_declarator && id_declarator->kind == cdk_id)
10447 error_at (start_token->location,
10448 "template parameter pack %qD cannot have a default argument",
10449 id_declarator->u.id.unqualified_name);
10451 error_at (start_token->location,
10452 "template parameter pack cannot have a default argument");
10454 /* Parse the default argument, but throw away the result. */
10455 cp_parser_default_argument (parser, /*template_parm_p=*/true);
10458 parm = grokdeclarator (parameter_declarator->declarator,
10459 ¶meter_declarator->decl_specifiers,
10460 PARM, /*initialized=*/0,
10461 /*attrlist=*/NULL);
10462 if (parm == error_mark_node)
10463 return error_mark_node;
10465 return build_tree_list (parameter_declarator->default_argument, parm);
10468 /* Parse a type-parameter.
10471 class identifier [opt]
10472 class identifier [opt] = type-id
10473 typename identifier [opt]
10474 typename identifier [opt] = type-id
10475 template < template-parameter-list > class identifier [opt]
10476 template < template-parameter-list > class identifier [opt]
10479 GNU Extension (variadic templates):
10482 class ... identifier [opt]
10483 typename ... identifier [opt]
10485 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
10486 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
10487 the declaration of the parameter.
10489 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10492 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10497 /* Look for a keyword to tell us what kind of parameter this is. */
10498 token = cp_parser_require (parser, CPP_KEYWORD,
10499 "%<class%>, %<typename%>, or %<template%>");
10501 return error_mark_node;
10503 switch (token->keyword)
10509 tree default_argument;
10511 /* If the next token is an ellipsis, we have a template
10513 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10515 /* Consume the `...' token. */
10516 cp_lexer_consume_token (parser->lexer);
10517 maybe_warn_variadic_templates ();
10519 *is_parameter_pack = true;
10522 /* If the next token is an identifier, then it names the
10524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10525 identifier = cp_parser_identifier (parser);
10527 identifier = NULL_TREE;
10529 /* Create the parameter. */
10530 parameter = finish_template_type_parm (class_type_node, identifier);
10532 /* If the next token is an `=', we have a default argument. */
10533 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10535 /* Consume the `=' token. */
10536 cp_lexer_consume_token (parser->lexer);
10537 /* Parse the default-argument. */
10538 push_deferring_access_checks (dk_no_deferred);
10539 default_argument = cp_parser_type_id (parser);
10541 /* Template parameter packs cannot have default
10543 if (*is_parameter_pack)
10546 error_at (token->location,
10547 "template parameter pack %qD cannot have a "
10548 "default argument", identifier);
10550 error_at (token->location,
10551 "template parameter packs cannot have "
10552 "default arguments");
10553 default_argument = NULL_TREE;
10555 pop_deferring_access_checks ();
10558 default_argument = NULL_TREE;
10560 /* Create the combined representation of the parameter and the
10561 default argument. */
10562 parameter = build_tree_list (default_argument, parameter);
10568 tree parameter_list;
10570 tree default_argument;
10572 /* Look for the `<'. */
10573 cp_parser_require (parser, CPP_LESS, "%<<%>");
10574 /* Parse the template-parameter-list. */
10575 parameter_list = cp_parser_template_parameter_list (parser);
10576 /* Look for the `>'. */
10577 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10578 /* Look for the `class' keyword. */
10579 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10580 /* If the next token is an ellipsis, we have a template
10582 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10584 /* Consume the `...' token. */
10585 cp_lexer_consume_token (parser->lexer);
10586 maybe_warn_variadic_templates ();
10588 *is_parameter_pack = true;
10590 /* If the next token is an `=', then there is a
10591 default-argument. If the next token is a `>', we are at
10592 the end of the parameter-list. If the next token is a `,',
10593 then we are at the end of this parameter. */
10594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10595 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10596 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10598 identifier = cp_parser_identifier (parser);
10599 /* Treat invalid names as if the parameter were nameless. */
10600 if (identifier == error_mark_node)
10601 identifier = NULL_TREE;
10604 identifier = NULL_TREE;
10606 /* Create the template parameter. */
10607 parameter = finish_template_template_parm (class_type_node,
10610 /* If the next token is an `=', then there is a
10611 default-argument. */
10612 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10616 /* Consume the `='. */
10617 cp_lexer_consume_token (parser->lexer);
10618 /* Parse the id-expression. */
10619 push_deferring_access_checks (dk_no_deferred);
10620 /* save token before parsing the id-expression, for error
10622 token = cp_lexer_peek_token (parser->lexer);
10624 = cp_parser_id_expression (parser,
10625 /*template_keyword_p=*/false,
10626 /*check_dependency_p=*/true,
10627 /*template_p=*/&is_template,
10628 /*declarator_p=*/false,
10629 /*optional_p=*/false);
10630 if (TREE_CODE (default_argument) == TYPE_DECL)
10631 /* If the id-expression was a template-id that refers to
10632 a template-class, we already have the declaration here,
10633 so no further lookup is needed. */
10636 /* Look up the name. */
10638 = cp_parser_lookup_name (parser, default_argument,
10640 /*is_template=*/is_template,
10641 /*is_namespace=*/false,
10642 /*check_dependency=*/true,
10643 /*ambiguous_decls=*/NULL,
10645 /* See if the default argument is valid. */
10647 = check_template_template_default_arg (default_argument);
10649 /* Template parameter packs cannot have default
10651 if (*is_parameter_pack)
10654 error_at (token->location,
10655 "template parameter pack %qD cannot "
10656 "have a default argument",
10659 error_at (token->location, "template parameter packs cannot "
10660 "have default arguments");
10661 default_argument = NULL_TREE;
10663 pop_deferring_access_checks ();
10666 default_argument = NULL_TREE;
10668 /* Create the combined representation of the parameter and the
10669 default argument. */
10670 parameter = build_tree_list (default_argument, parameter);
10675 gcc_unreachable ();
10682 /* Parse a template-id.
10685 template-name < template-argument-list [opt] >
10687 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10688 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10689 returned. Otherwise, if the template-name names a function, or set
10690 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10691 names a class, returns a TYPE_DECL for the specialization.
10693 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10694 uninstantiated templates. */
10697 cp_parser_template_id (cp_parser *parser,
10698 bool template_keyword_p,
10699 bool check_dependency_p,
10700 bool is_declaration)
10706 cp_token_position start_of_id = 0;
10707 deferred_access_check *chk;
10708 VEC (deferred_access_check,gc) *access_check;
10709 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10710 bool is_identifier;
10712 /* If the next token corresponds to a template-id, there is no need
10714 next_token = cp_lexer_peek_token (parser->lexer);
10715 if (next_token->type == CPP_TEMPLATE_ID)
10717 struct tree_check *check_value;
10719 /* Get the stored value. */
10720 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10721 /* Perform any access checks that were deferred. */
10722 access_check = check_value->checks;
10726 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10729 perform_or_defer_access_check (chk->binfo,
10734 /* Return the stored value. */
10735 return check_value->value;
10738 /* Avoid performing name lookup if there is no possibility of
10739 finding a template-id. */
10740 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10741 || (next_token->type == CPP_NAME
10742 && !cp_parser_nth_token_starts_template_argument_list_p
10745 cp_parser_error (parser, "expected template-id");
10746 return error_mark_node;
10749 /* Remember where the template-id starts. */
10750 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10751 start_of_id = cp_lexer_token_position (parser->lexer, false);
10753 push_deferring_access_checks (dk_deferred);
10755 /* Parse the template-name. */
10756 is_identifier = false;
10757 token = cp_lexer_peek_token (parser->lexer);
10758 templ = cp_parser_template_name (parser, template_keyword_p,
10759 check_dependency_p,
10762 if (templ == error_mark_node || is_identifier)
10764 pop_deferring_access_checks ();
10768 /* If we find the sequence `[:' after a template-name, it's probably
10769 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10770 parse correctly the argument list. */
10771 next_token = cp_lexer_peek_token (parser->lexer);
10772 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10773 if (next_token->type == CPP_OPEN_SQUARE
10774 && next_token->flags & DIGRAPH
10775 && next_token_2->type == CPP_COLON
10776 && !(next_token_2->flags & PREV_WHITE))
10778 cp_parser_parse_tentatively (parser);
10779 /* Change `:' into `::'. */
10780 next_token_2->type = CPP_SCOPE;
10781 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10783 cp_lexer_consume_token (parser->lexer);
10785 /* Parse the arguments. */
10786 arguments = cp_parser_enclosed_template_argument_list (parser);
10787 if (!cp_parser_parse_definitely (parser))
10789 /* If we couldn't parse an argument list, then we revert our changes
10790 and return simply an error. Maybe this is not a template-id
10792 next_token_2->type = CPP_COLON;
10793 cp_parser_error (parser, "expected %<<%>");
10794 pop_deferring_access_checks ();
10795 return error_mark_node;
10797 /* Otherwise, emit an error about the invalid digraph, but continue
10798 parsing because we got our argument list. */
10799 if (permerror (next_token->location,
10800 "%<<::%> cannot begin a template-argument list"))
10802 static bool hint = false;
10803 inform (next_token->location,
10804 "%<<:%> is an alternate spelling for %<[%>."
10805 " Insert whitespace between %<<%> and %<::%>");
10806 if (!hint && !flag_permissive)
10808 inform (next_token->location, "(if you use %<-fpermissive%>"
10809 " G++ will accept your code)");
10816 /* Look for the `<' that starts the template-argument-list. */
10817 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10819 pop_deferring_access_checks ();
10820 return error_mark_node;
10822 /* Parse the arguments. */
10823 arguments = cp_parser_enclosed_template_argument_list (parser);
10826 /* Build a representation of the specialization. */
10827 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10828 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10829 else if (DECL_CLASS_TEMPLATE_P (templ)
10830 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10832 bool entering_scope;
10833 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10834 template (rather than some instantiation thereof) only if
10835 is not nested within some other construct. For example, in
10836 "template <typename T> void f(T) { A<T>::", A<T> is just an
10837 instantiation of A. */
10838 entering_scope = (template_parm_scope_p ()
10839 && cp_lexer_next_token_is (parser->lexer,
10842 = finish_template_type (templ, arguments, entering_scope);
10846 /* If it's not a class-template or a template-template, it should be
10847 a function-template. */
10848 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10849 || TREE_CODE (templ) == OVERLOAD
10850 || BASELINK_P (templ)));
10852 template_id = lookup_template_function (templ, arguments);
10855 /* If parsing tentatively, replace the sequence of tokens that makes
10856 up the template-id with a CPP_TEMPLATE_ID token. That way,
10857 should we re-parse the token stream, we will not have to repeat
10858 the effort required to do the parse, nor will we issue duplicate
10859 error messages about problems during instantiation of the
10863 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10865 /* Reset the contents of the START_OF_ID token. */
10866 token->type = CPP_TEMPLATE_ID;
10867 /* Retrieve any deferred checks. Do not pop this access checks yet
10868 so the memory will not be reclaimed during token replacing below. */
10869 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10870 token->u.tree_check_value->value = template_id;
10871 token->u.tree_check_value->checks = get_deferred_access_checks ();
10872 token->keyword = RID_MAX;
10874 /* Purge all subsequent tokens. */
10875 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10877 /* ??? Can we actually assume that, if template_id ==
10878 error_mark_node, we will have issued a diagnostic to the
10879 user, as opposed to simply marking the tentative parse as
10881 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10882 error_at (token->location, "parse error in template argument list");
10885 pop_deferring_access_checks ();
10886 return template_id;
10889 /* Parse a template-name.
10894 The standard should actually say:
10898 operator-function-id
10900 A defect report has been filed about this issue.
10902 A conversion-function-id cannot be a template name because they cannot
10903 be part of a template-id. In fact, looking at this code:
10905 a.operator K<int>()
10907 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10908 It is impossible to call a templated conversion-function-id with an
10909 explicit argument list, since the only allowed template parameter is
10910 the type to which it is converting.
10912 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10913 `template' keyword, in a construction like:
10917 In that case `f' is taken to be a template-name, even though there
10918 is no way of knowing for sure.
10920 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10921 name refers to a set of overloaded functions, at least one of which
10922 is a template, or an IDENTIFIER_NODE with the name of the template,
10923 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10924 names are looked up inside uninstantiated templates. */
10927 cp_parser_template_name (cp_parser* parser,
10928 bool template_keyword_p,
10929 bool check_dependency_p,
10930 bool is_declaration,
10931 bool *is_identifier)
10936 cp_token *token = cp_lexer_peek_token (parser->lexer);
10938 /* If the next token is `operator', then we have either an
10939 operator-function-id or a conversion-function-id. */
10940 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10942 /* We don't know whether we're looking at an
10943 operator-function-id or a conversion-function-id. */
10944 cp_parser_parse_tentatively (parser);
10945 /* Try an operator-function-id. */
10946 identifier = cp_parser_operator_function_id (parser);
10947 /* If that didn't work, try a conversion-function-id. */
10948 if (!cp_parser_parse_definitely (parser))
10950 cp_parser_error (parser, "expected template-name");
10951 return error_mark_node;
10954 /* Look for the identifier. */
10956 identifier = cp_parser_identifier (parser);
10958 /* If we didn't find an identifier, we don't have a template-id. */
10959 if (identifier == error_mark_node)
10960 return error_mark_node;
10962 /* If the name immediately followed the `template' keyword, then it
10963 is a template-name. However, if the next token is not `<', then
10964 we do not treat it as a template-name, since it is not being used
10965 as part of a template-id. This enables us to handle constructs
10968 template <typename T> struct S { S(); };
10969 template <typename T> S<T>::S();
10971 correctly. We would treat `S' as a template -- if it were `S<T>'
10972 -- but we do not if there is no `<'. */
10974 if (processing_template_decl
10975 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10977 /* In a declaration, in a dependent context, we pretend that the
10978 "template" keyword was present in order to improve error
10979 recovery. For example, given:
10981 template <typename T> void f(T::X<int>);
10983 we want to treat "X<int>" as a template-id. */
10985 && !template_keyword_p
10986 && parser->scope && TYPE_P (parser->scope)
10987 && check_dependency_p
10988 && dependent_scope_p (parser->scope)
10989 /* Do not do this for dtors (or ctors), since they never
10990 need the template keyword before their name. */
10991 && !constructor_name_p (identifier, parser->scope))
10993 cp_token_position start = 0;
10995 /* Explain what went wrong. */
10996 error_at (token->location, "non-template %qD used as template",
10998 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
10999 parser->scope, identifier);
11000 /* If parsing tentatively, find the location of the "<" token. */
11001 if (cp_parser_simulate_error (parser))
11002 start = cp_lexer_token_position (parser->lexer, true);
11003 /* Parse the template arguments so that we can issue error
11004 messages about them. */
11005 cp_lexer_consume_token (parser->lexer);
11006 cp_parser_enclosed_template_argument_list (parser);
11007 /* Skip tokens until we find a good place from which to
11008 continue parsing. */
11009 cp_parser_skip_to_closing_parenthesis (parser,
11010 /*recovering=*/true,
11012 /*consume_paren=*/false);
11013 /* If parsing tentatively, permanently remove the
11014 template argument list. That will prevent duplicate
11015 error messages from being issued about the missing
11016 "template" keyword. */
11018 cp_lexer_purge_tokens_after (parser->lexer, start);
11020 *is_identifier = true;
11024 /* If the "template" keyword is present, then there is generally
11025 no point in doing name-lookup, so we just return IDENTIFIER.
11026 But, if the qualifying scope is non-dependent then we can
11027 (and must) do name-lookup normally. */
11028 if (template_keyword_p
11030 || (TYPE_P (parser->scope)
11031 && dependent_type_p (parser->scope))))
11035 /* Look up the name. */
11036 decl = cp_parser_lookup_name (parser, identifier,
11038 /*is_template=*/false,
11039 /*is_namespace=*/false,
11040 check_dependency_p,
11041 /*ambiguous_decls=*/NULL,
11043 decl = maybe_get_template_decl_from_type_decl (decl);
11045 /* If DECL is a template, then the name was a template-name. */
11046 if (TREE_CODE (decl) == TEMPLATE_DECL)
11050 tree fn = NULL_TREE;
11052 /* The standard does not explicitly indicate whether a name that
11053 names a set of overloaded declarations, some of which are
11054 templates, is a template-name. However, such a name should
11055 be a template-name; otherwise, there is no way to form a
11056 template-id for the overloaded templates. */
11057 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11058 if (TREE_CODE (fns) == OVERLOAD)
11059 for (fn = fns; fn; fn = OVL_NEXT (fn))
11060 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11065 /* The name does not name a template. */
11066 cp_parser_error (parser, "expected template-name");
11067 return error_mark_node;
11071 /* If DECL is dependent, and refers to a function, then just return
11072 its name; we will look it up again during template instantiation. */
11073 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11075 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11076 if (TYPE_P (scope) && dependent_type_p (scope))
11083 /* Parse a template-argument-list.
11085 template-argument-list:
11086 template-argument ... [opt]
11087 template-argument-list , template-argument ... [opt]
11089 Returns a TREE_VEC containing the arguments. */
11092 cp_parser_template_argument_list (cp_parser* parser)
11094 tree fixed_args[10];
11095 unsigned n_args = 0;
11096 unsigned alloced = 10;
11097 tree *arg_ary = fixed_args;
11099 bool saved_in_template_argument_list_p;
11101 bool saved_non_ice_p;
11103 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11104 parser->in_template_argument_list_p = true;
11105 /* Even if the template-id appears in an integral
11106 constant-expression, the contents of the argument list do
11108 saved_ice_p = parser->integral_constant_expression_p;
11109 parser->integral_constant_expression_p = false;
11110 saved_non_ice_p = parser->non_integral_constant_expression_p;
11111 parser->non_integral_constant_expression_p = false;
11112 /* Parse the arguments. */
11118 /* Consume the comma. */
11119 cp_lexer_consume_token (parser->lexer);
11121 /* Parse the template-argument. */
11122 argument = cp_parser_template_argument (parser);
11124 /* If the next token is an ellipsis, we're expanding a template
11126 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11128 if (argument == error_mark_node)
11130 cp_token *token = cp_lexer_peek_token (parser->lexer);
11131 error_at (token->location,
11132 "expected parameter pack before %<...%>");
11134 /* Consume the `...' token. */
11135 cp_lexer_consume_token (parser->lexer);
11137 /* Make the argument into a TYPE_PACK_EXPANSION or
11138 EXPR_PACK_EXPANSION. */
11139 argument = make_pack_expansion (argument);
11142 if (n_args == alloced)
11146 if (arg_ary == fixed_args)
11148 arg_ary = XNEWVEC (tree, alloced);
11149 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11152 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11154 arg_ary[n_args++] = argument;
11156 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11158 vec = make_tree_vec (n_args);
11161 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11163 if (arg_ary != fixed_args)
11165 parser->non_integral_constant_expression_p = saved_non_ice_p;
11166 parser->integral_constant_expression_p = saved_ice_p;
11167 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11171 /* Parse a template-argument.
11174 assignment-expression
11178 The representation is that of an assignment-expression, type-id, or
11179 id-expression -- except that the qualified id-expression is
11180 evaluated, so that the value returned is either a DECL or an
11183 Although the standard says "assignment-expression", it forbids
11184 throw-expressions or assignments in the template argument.
11185 Therefore, we use "conditional-expression" instead. */
11188 cp_parser_template_argument (cp_parser* parser)
11193 bool maybe_type_id = false;
11194 cp_token *token = NULL, *argument_start_token = NULL;
11197 /* There's really no way to know what we're looking at, so we just
11198 try each alternative in order.
11202 In a template-argument, an ambiguity between a type-id and an
11203 expression is resolved to a type-id, regardless of the form of
11204 the corresponding template-parameter.
11206 Therefore, we try a type-id first. */
11207 cp_parser_parse_tentatively (parser);
11208 argument = cp_parser_template_type_arg (parser);
11209 /* If there was no error parsing the type-id but the next token is a
11210 '>>', our behavior depends on which dialect of C++ we're
11211 parsing. In C++98, we probably found a typo for '> >'. But there
11212 are type-id which are also valid expressions. For instance:
11214 struct X { int operator >> (int); };
11215 template <int V> struct Foo {};
11218 Here 'X()' is a valid type-id of a function type, but the user just
11219 wanted to write the expression "X() >> 5". Thus, we remember that we
11220 found a valid type-id, but we still try to parse the argument as an
11221 expression to see what happens.
11223 In C++0x, the '>>' will be considered two separate '>'
11225 if (!cp_parser_error_occurred (parser)
11226 && cxx_dialect == cxx98
11227 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11229 maybe_type_id = true;
11230 cp_parser_abort_tentative_parse (parser);
11234 /* If the next token isn't a `,' or a `>', then this argument wasn't
11235 really finished. This means that the argument is not a valid
11237 if (!cp_parser_next_token_ends_template_argument_p (parser))
11238 cp_parser_error (parser, "expected template-argument");
11239 /* If that worked, we're done. */
11240 if (cp_parser_parse_definitely (parser))
11243 /* We're still not sure what the argument will be. */
11244 cp_parser_parse_tentatively (parser);
11245 /* Try a template. */
11246 argument_start_token = cp_lexer_peek_token (parser->lexer);
11247 argument = cp_parser_id_expression (parser,
11248 /*template_keyword_p=*/false,
11249 /*check_dependency_p=*/true,
11251 /*declarator_p=*/false,
11252 /*optional_p=*/false);
11253 /* If the next token isn't a `,' or a `>', then this argument wasn't
11254 really finished. */
11255 if (!cp_parser_next_token_ends_template_argument_p (parser))
11256 cp_parser_error (parser, "expected template-argument");
11257 if (!cp_parser_error_occurred (parser))
11259 /* Figure out what is being referred to. If the id-expression
11260 was for a class template specialization, then we will have a
11261 TYPE_DECL at this point. There is no need to do name lookup
11262 at this point in that case. */
11263 if (TREE_CODE (argument) != TYPE_DECL)
11264 argument = cp_parser_lookup_name (parser, argument,
11266 /*is_template=*/template_p,
11267 /*is_namespace=*/false,
11268 /*check_dependency=*/true,
11269 /*ambiguous_decls=*/NULL,
11270 argument_start_token->location);
11271 if (TREE_CODE (argument) != TEMPLATE_DECL
11272 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11273 cp_parser_error (parser, "expected template-name");
11275 if (cp_parser_parse_definitely (parser))
11277 /* It must be a non-type argument. There permitted cases are given
11278 in [temp.arg.nontype]:
11280 -- an integral constant-expression of integral or enumeration
11283 -- the name of a non-type template-parameter; or
11285 -- the name of an object or function with external linkage...
11287 -- the address of an object or function with external linkage...
11289 -- a pointer to member... */
11290 /* Look for a non-type template parameter. */
11291 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11293 cp_parser_parse_tentatively (parser);
11294 argument = cp_parser_primary_expression (parser,
11295 /*address_p=*/false,
11297 /*template_arg_p=*/true,
11299 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11300 || !cp_parser_next_token_ends_template_argument_p (parser))
11301 cp_parser_simulate_error (parser);
11302 if (cp_parser_parse_definitely (parser))
11306 /* If the next token is "&", the argument must be the address of an
11307 object or function with external linkage. */
11308 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11310 cp_lexer_consume_token (parser->lexer);
11311 /* See if we might have an id-expression. */
11312 token = cp_lexer_peek_token (parser->lexer);
11313 if (token->type == CPP_NAME
11314 || token->keyword == RID_OPERATOR
11315 || token->type == CPP_SCOPE
11316 || token->type == CPP_TEMPLATE_ID
11317 || token->type == CPP_NESTED_NAME_SPECIFIER)
11319 cp_parser_parse_tentatively (parser);
11320 argument = cp_parser_primary_expression (parser,
11323 /*template_arg_p=*/true,
11325 if (cp_parser_error_occurred (parser)
11326 || !cp_parser_next_token_ends_template_argument_p (parser))
11327 cp_parser_abort_tentative_parse (parser);
11330 if (TREE_CODE (argument) == INDIRECT_REF)
11332 gcc_assert (REFERENCE_REF_P (argument));
11333 argument = TREE_OPERAND (argument, 0);
11336 if (TREE_CODE (argument) == VAR_DECL)
11338 /* A variable without external linkage might still be a
11339 valid constant-expression, so no error is issued here
11340 if the external-linkage check fails. */
11341 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11342 cp_parser_simulate_error (parser);
11344 else if (is_overloaded_fn (argument))
11345 /* All overloaded functions are allowed; if the external
11346 linkage test does not pass, an error will be issued
11350 && (TREE_CODE (argument) == OFFSET_REF
11351 || TREE_CODE (argument) == SCOPE_REF))
11352 /* A pointer-to-member. */
11354 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11357 cp_parser_simulate_error (parser);
11359 if (cp_parser_parse_definitely (parser))
11362 argument = build_x_unary_op (ADDR_EXPR, argument,
11363 tf_warning_or_error);
11368 /* If the argument started with "&", there are no other valid
11369 alternatives at this point. */
11372 cp_parser_error (parser, "invalid non-type template argument");
11373 return error_mark_node;
11376 /* If the argument wasn't successfully parsed as a type-id followed
11377 by '>>', the argument can only be a constant expression now.
11378 Otherwise, we try parsing the constant-expression tentatively,
11379 because the argument could really be a type-id. */
11381 cp_parser_parse_tentatively (parser);
11382 argument = cp_parser_constant_expression (parser,
11383 /*allow_non_constant_p=*/false,
11384 /*non_constant_p=*/NULL);
11385 argument = fold_non_dependent_expr (argument);
11386 if (!maybe_type_id)
11388 if (!cp_parser_next_token_ends_template_argument_p (parser))
11389 cp_parser_error (parser, "expected template-argument");
11390 if (cp_parser_parse_definitely (parser))
11392 /* We did our best to parse the argument as a non type-id, but that
11393 was the only alternative that matched (albeit with a '>' after
11394 it). We can assume it's just a typo from the user, and a
11395 diagnostic will then be issued. */
11396 return cp_parser_template_type_arg (parser);
11399 /* Parse an explicit-instantiation.
11401 explicit-instantiation:
11402 template declaration
11404 Although the standard says `declaration', what it really means is:
11406 explicit-instantiation:
11407 template decl-specifier-seq [opt] declarator [opt] ;
11409 Things like `template int S<int>::i = 5, int S<double>::j;' are not
11410 supposed to be allowed. A defect report has been filed about this
11415 explicit-instantiation:
11416 storage-class-specifier template
11417 decl-specifier-seq [opt] declarator [opt] ;
11418 function-specifier template
11419 decl-specifier-seq [opt] declarator [opt] ; */
11422 cp_parser_explicit_instantiation (cp_parser* parser)
11424 int declares_class_or_enum;
11425 cp_decl_specifier_seq decl_specifiers;
11426 tree extension_specifier = NULL_TREE;
11429 /* Look for an (optional) storage-class-specifier or
11430 function-specifier. */
11431 if (cp_parser_allow_gnu_extensions_p (parser))
11433 extension_specifier
11434 = cp_parser_storage_class_specifier_opt (parser);
11435 if (!extension_specifier)
11436 extension_specifier
11437 = cp_parser_function_specifier_opt (parser,
11438 /*decl_specs=*/NULL);
11441 /* Look for the `template' keyword. */
11442 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11443 /* Let the front end know that we are processing an explicit
11445 begin_explicit_instantiation ();
11446 /* [temp.explicit] says that we are supposed to ignore access
11447 control while processing explicit instantiation directives. */
11448 push_deferring_access_checks (dk_no_check);
11449 /* Parse a decl-specifier-seq. */
11450 token = cp_lexer_peek_token (parser->lexer);
11451 cp_parser_decl_specifier_seq (parser,
11452 CP_PARSER_FLAGS_OPTIONAL,
11454 &declares_class_or_enum);
11455 /* If there was exactly one decl-specifier, and it declared a class,
11456 and there's no declarator, then we have an explicit type
11458 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11462 type = check_tag_decl (&decl_specifiers);
11463 /* Turn access control back on for names used during
11464 template instantiation. */
11465 pop_deferring_access_checks ();
11467 do_type_instantiation (type, extension_specifier,
11468 /*complain=*/tf_error);
11472 cp_declarator *declarator;
11475 /* Parse the declarator. */
11477 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11478 /*ctor_dtor_or_conv_p=*/NULL,
11479 /*parenthesized_p=*/NULL,
11480 /*member_p=*/false);
11481 if (declares_class_or_enum & 2)
11482 cp_parser_check_for_definition_in_return_type (declarator,
11483 decl_specifiers.type,
11484 decl_specifiers.type_location);
11485 if (declarator != cp_error_declarator)
11487 decl = grokdeclarator (declarator, &decl_specifiers,
11488 NORMAL, 0, &decl_specifiers.attributes);
11489 /* Turn access control back on for names used during
11490 template instantiation. */
11491 pop_deferring_access_checks ();
11492 /* Do the explicit instantiation. */
11493 do_decl_instantiation (decl, extension_specifier);
11497 pop_deferring_access_checks ();
11498 /* Skip the body of the explicit instantiation. */
11499 cp_parser_skip_to_end_of_statement (parser);
11502 /* We're done with the instantiation. */
11503 end_explicit_instantiation ();
11505 cp_parser_consume_semicolon_at_end_of_statement (parser);
11508 /* Parse an explicit-specialization.
11510 explicit-specialization:
11511 template < > declaration
11513 Although the standard says `declaration', what it really means is:
11515 explicit-specialization:
11516 template <> decl-specifier [opt] init-declarator [opt] ;
11517 template <> function-definition
11518 template <> explicit-specialization
11519 template <> template-declaration */
11522 cp_parser_explicit_specialization (cp_parser* parser)
11524 bool need_lang_pop;
11525 cp_token *token = cp_lexer_peek_token (parser->lexer);
11527 /* Look for the `template' keyword. */
11528 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11529 /* Look for the `<'. */
11530 cp_parser_require (parser, CPP_LESS, "%<<%>");
11531 /* Look for the `>'. */
11532 cp_parser_require (parser, CPP_GREATER, "%<>%>");
11533 /* We have processed another parameter list. */
11534 ++parser->num_template_parameter_lists;
11537 A template ... explicit specialization ... shall not have C
11539 if (current_lang_name == lang_name_c)
11541 error_at (token->location, "template specialization with C linkage");
11542 /* Give it C++ linkage to avoid confusing other parts of the
11544 push_lang_context (lang_name_cplusplus);
11545 need_lang_pop = true;
11548 need_lang_pop = false;
11549 /* Let the front end know that we are beginning a specialization. */
11550 if (!begin_specialization ())
11552 end_specialization ();
11556 /* If the next keyword is `template', we need to figure out whether
11557 or not we're looking a template-declaration. */
11558 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11560 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11561 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11562 cp_parser_template_declaration_after_export (parser,
11563 /*member_p=*/false);
11565 cp_parser_explicit_specialization (parser);
11568 /* Parse the dependent declaration. */
11569 cp_parser_single_declaration (parser,
11571 /*member_p=*/false,
11572 /*explicit_specialization_p=*/true,
11573 /*friend_p=*/NULL);
11574 /* We're done with the specialization. */
11575 end_specialization ();
11576 /* For the erroneous case of a template with C linkage, we pushed an
11577 implicit C++ linkage scope; exit that scope now. */
11579 pop_lang_context ();
11580 /* We're done with this parameter list. */
11581 --parser->num_template_parameter_lists;
11584 /* Parse a type-specifier.
11587 simple-type-specifier
11590 elaborated-type-specifier
11598 Returns a representation of the type-specifier. For a
11599 class-specifier, enum-specifier, or elaborated-type-specifier, a
11600 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11602 The parser flags FLAGS is used to control type-specifier parsing.
11604 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11605 in a decl-specifier-seq.
11607 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11608 class-specifier, enum-specifier, or elaborated-type-specifier, then
11609 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11610 if a type is declared; 2 if it is defined. Otherwise, it is set to
11613 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11614 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11615 is set to FALSE. */
11618 cp_parser_type_specifier (cp_parser* parser,
11619 cp_parser_flags flags,
11620 cp_decl_specifier_seq *decl_specs,
11621 bool is_declaration,
11622 int* declares_class_or_enum,
11623 bool* is_cv_qualifier)
11625 tree type_spec = NULL_TREE;
11628 cp_decl_spec ds = ds_last;
11630 /* Assume this type-specifier does not declare a new type. */
11631 if (declares_class_or_enum)
11632 *declares_class_or_enum = 0;
11633 /* And that it does not specify a cv-qualifier. */
11634 if (is_cv_qualifier)
11635 *is_cv_qualifier = false;
11636 /* Peek at the next token. */
11637 token = cp_lexer_peek_token (parser->lexer);
11639 /* If we're looking at a keyword, we can use that to guide the
11640 production we choose. */
11641 keyword = token->keyword;
11645 /* Look for the enum-specifier. */
11646 type_spec = cp_parser_enum_specifier (parser);
11647 /* If that worked, we're done. */
11650 if (declares_class_or_enum)
11651 *declares_class_or_enum = 2;
11653 cp_parser_set_decl_spec_type (decl_specs,
11656 /*user_defined_p=*/true);
11660 goto elaborated_type_specifier;
11662 /* Any of these indicate either a class-specifier, or an
11663 elaborated-type-specifier. */
11667 /* Parse tentatively so that we can back up if we don't find a
11668 class-specifier. */
11669 cp_parser_parse_tentatively (parser);
11670 /* Look for the class-specifier. */
11671 type_spec = cp_parser_class_specifier (parser);
11672 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11673 /* If that worked, we're done. */
11674 if (cp_parser_parse_definitely (parser))
11676 if (declares_class_or_enum)
11677 *declares_class_or_enum = 2;
11679 cp_parser_set_decl_spec_type (decl_specs,
11682 /*user_defined_p=*/true);
11686 /* Fall through. */
11687 elaborated_type_specifier:
11688 /* We're declaring (not defining) a class or enum. */
11689 if (declares_class_or_enum)
11690 *declares_class_or_enum = 1;
11692 /* Fall through. */
11694 /* Look for an elaborated-type-specifier. */
11696 = (cp_parser_elaborated_type_specifier
11698 decl_specs && decl_specs->specs[(int) ds_friend],
11701 cp_parser_set_decl_spec_type (decl_specs,
11704 /*user_defined_p=*/true);
11709 if (is_cv_qualifier)
11710 *is_cv_qualifier = true;
11715 if (is_cv_qualifier)
11716 *is_cv_qualifier = true;
11721 if (is_cv_qualifier)
11722 *is_cv_qualifier = true;
11726 /* The `__complex__' keyword is a GNU extension. */
11734 /* Handle simple keywords. */
11739 ++decl_specs->specs[(int)ds];
11740 decl_specs->any_specifiers_p = true;
11742 return cp_lexer_consume_token (parser->lexer)->u.value;
11745 /* If we do not already have a type-specifier, assume we are looking
11746 at a simple-type-specifier. */
11747 type_spec = cp_parser_simple_type_specifier (parser,
11751 /* If we didn't find a type-specifier, and a type-specifier was not
11752 optional in this context, issue an error message. */
11753 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11755 cp_parser_error (parser, "expected type specifier");
11756 return error_mark_node;
11762 /* Parse a simple-type-specifier.
11764 simple-type-specifier:
11765 :: [opt] nested-name-specifier [opt] type-name
11766 :: [opt] nested-name-specifier template template-id
11781 simple-type-specifier:
11783 decltype ( expression )
11789 simple-type-specifier:
11790 __typeof__ unary-expression
11791 __typeof__ ( type-id )
11793 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11794 appropriately updated. */
11797 cp_parser_simple_type_specifier (cp_parser* parser,
11798 cp_decl_specifier_seq *decl_specs,
11799 cp_parser_flags flags)
11801 tree type = NULL_TREE;
11804 /* Peek at the next token. */
11805 token = cp_lexer_peek_token (parser->lexer);
11807 /* If we're looking at a keyword, things are easy. */
11808 switch (token->keyword)
11812 decl_specs->explicit_char_p = true;
11813 type = char_type_node;
11816 type = char16_type_node;
11819 type = char32_type_node;
11822 type = wchar_type_node;
11825 type = boolean_type_node;
11829 ++decl_specs->specs[(int) ds_short];
11830 type = short_integer_type_node;
11834 decl_specs->explicit_int_p = true;
11835 type = integer_type_node;
11839 ++decl_specs->specs[(int) ds_long];
11840 type = long_integer_type_node;
11844 ++decl_specs->specs[(int) ds_signed];
11845 type = integer_type_node;
11849 ++decl_specs->specs[(int) ds_unsigned];
11850 type = unsigned_type_node;
11853 type = float_type_node;
11856 type = double_type_node;
11859 type = void_type_node;
11863 maybe_warn_cpp0x ("C++0x auto");
11864 type = make_auto ();
11868 /* Parse the `decltype' type. */
11869 type = cp_parser_decltype (parser);
11872 cp_parser_set_decl_spec_type (decl_specs, type,
11874 /*user_defined_p=*/true);
11879 /* Consume the `typeof' token. */
11880 cp_lexer_consume_token (parser->lexer);
11881 /* Parse the operand to `typeof'. */
11882 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11883 /* If it is not already a TYPE, take its type. */
11884 if (!TYPE_P (type))
11885 type = finish_typeof (type);
11888 cp_parser_set_decl_spec_type (decl_specs, type,
11890 /*user_defined_p=*/true);
11898 /* If the type-specifier was for a built-in type, we're done. */
11903 /* Record the type. */
11905 && (token->keyword != RID_SIGNED
11906 && token->keyword != RID_UNSIGNED
11907 && token->keyword != RID_SHORT
11908 && token->keyword != RID_LONG))
11909 cp_parser_set_decl_spec_type (decl_specs,
11912 /*user_defined=*/false);
11914 decl_specs->any_specifiers_p = true;
11916 /* Consume the token. */
11917 id = cp_lexer_consume_token (parser->lexer)->u.value;
11919 /* There is no valid C++ program where a non-template type is
11920 followed by a "<". That usually indicates that the user thought
11921 that the type was a template. */
11922 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11924 return TYPE_NAME (type);
11927 /* The type-specifier must be a user-defined type. */
11928 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11933 /* Don't gobble tokens or issue error messages if this is an
11934 optional type-specifier. */
11935 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11936 cp_parser_parse_tentatively (parser);
11938 /* Look for the optional `::' operator. */
11940 = (cp_parser_global_scope_opt (parser,
11941 /*current_scope_valid_p=*/false)
11943 /* Look for the nested-name specifier. */
11945 = (cp_parser_nested_name_specifier_opt (parser,
11946 /*typename_keyword_p=*/false,
11947 /*check_dependency_p=*/true,
11949 /*is_declaration=*/false)
11951 token = cp_lexer_peek_token (parser->lexer);
11952 /* If we have seen a nested-name-specifier, and the next token
11953 is `template', then we are using the template-id production. */
11955 && cp_parser_optional_template_keyword (parser))
11957 /* Look for the template-id. */
11958 type = cp_parser_template_id (parser,
11959 /*template_keyword_p=*/true,
11960 /*check_dependency_p=*/true,
11961 /*is_declaration=*/false);
11962 /* If the template-id did not name a type, we are out of
11964 if (TREE_CODE (type) != TYPE_DECL)
11966 cp_parser_error (parser, "expected template-id for type");
11970 /* Otherwise, look for a type-name. */
11972 type = cp_parser_type_name (parser);
11973 /* Keep track of all name-lookups performed in class scopes. */
11977 && TREE_CODE (type) == TYPE_DECL
11978 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11979 maybe_note_name_used_in_class (DECL_NAME (type), type);
11980 /* If it didn't work out, we don't have a TYPE. */
11981 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11982 && !cp_parser_parse_definitely (parser))
11984 if (type && decl_specs)
11985 cp_parser_set_decl_spec_type (decl_specs, type,
11987 /*user_defined=*/true);
11990 /* If we didn't get a type-name, issue an error message. */
11991 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11993 cp_parser_error (parser, "expected type-name");
11994 return error_mark_node;
11997 /* There is no valid C++ program where a non-template type is
11998 followed by a "<". That usually indicates that the user thought
11999 that the type was a template. */
12000 if (type && type != error_mark_node)
12002 /* As a last-ditch effort, see if TYPE is an Objective-C type.
12003 If it is, then the '<'...'>' enclose protocol names rather than
12004 template arguments, and so everything is fine. */
12005 if (c_dialect_objc ()
12006 && (objc_is_id (type) || objc_is_class_name (type)))
12008 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12009 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12011 /* Clobber the "unqualified" type previously entered into
12012 DECL_SPECS with the new, improved protocol-qualified version. */
12014 decl_specs->type = qual_type;
12019 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12026 /* Parse a type-name.
12039 Returns a TYPE_DECL for the type. */
12042 cp_parser_type_name (cp_parser* parser)
12046 /* We can't know yet whether it is a class-name or not. */
12047 cp_parser_parse_tentatively (parser);
12048 /* Try a class-name. */
12049 type_decl = cp_parser_class_name (parser,
12050 /*typename_keyword_p=*/false,
12051 /*template_keyword_p=*/false,
12053 /*check_dependency_p=*/true,
12054 /*class_head_p=*/false,
12055 /*is_declaration=*/false);
12056 /* If it's not a class-name, keep looking. */
12057 if (!cp_parser_parse_definitely (parser))
12059 /* It must be a typedef-name or an enum-name. */
12060 return cp_parser_nonclass_name (parser);
12066 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12074 Returns a TYPE_DECL for the type. */
12077 cp_parser_nonclass_name (cp_parser* parser)
12082 cp_token *token = cp_lexer_peek_token (parser->lexer);
12083 identifier = cp_parser_identifier (parser);
12084 if (identifier == error_mark_node)
12085 return error_mark_node;
12087 /* Look up the type-name. */
12088 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12090 if (TREE_CODE (type_decl) != TYPE_DECL
12091 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12093 /* See if this is an Objective-C type. */
12094 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12095 tree type = objc_get_protocol_qualified_type (identifier, protos);
12097 type_decl = TYPE_NAME (type);
12100 /* Issue an error if we did not find a type-name. */
12101 if (TREE_CODE (type_decl) != TYPE_DECL)
12103 if (!cp_parser_simulate_error (parser))
12104 cp_parser_name_lookup_error (parser, identifier, type_decl,
12105 "is not a type", token->location);
12106 return error_mark_node;
12108 /* Remember that the name was used in the definition of the
12109 current class so that we can check later to see if the
12110 meaning would have been different after the class was
12111 entirely defined. */
12112 else if (type_decl != error_mark_node
12114 maybe_note_name_used_in_class (identifier, type_decl);
12119 /* Parse an elaborated-type-specifier. Note that the grammar given
12120 here incorporates the resolution to DR68.
12122 elaborated-type-specifier:
12123 class-key :: [opt] nested-name-specifier [opt] identifier
12124 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12125 enum-key :: [opt] nested-name-specifier [opt] identifier
12126 typename :: [opt] nested-name-specifier identifier
12127 typename :: [opt] nested-name-specifier template [opt]
12132 elaborated-type-specifier:
12133 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12134 class-key attributes :: [opt] nested-name-specifier [opt]
12135 template [opt] template-id
12136 enum attributes :: [opt] nested-name-specifier [opt] identifier
12138 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12139 declared `friend'. If IS_DECLARATION is TRUE, then this
12140 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12141 something is being declared.
12143 Returns the TYPE specified. */
12146 cp_parser_elaborated_type_specifier (cp_parser* parser,
12148 bool is_declaration)
12150 enum tag_types tag_type;
12152 tree type = NULL_TREE;
12153 tree attributes = NULL_TREE;
12155 cp_token *token = NULL;
12157 /* See if we're looking at the `enum' keyword. */
12158 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12160 /* Consume the `enum' token. */
12161 cp_lexer_consume_token (parser->lexer);
12162 /* Remember that it's an enumeration type. */
12163 tag_type = enum_type;
12164 /* Parse the optional `struct' or `class' key (for C++0x scoped
12166 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12167 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12169 if (cxx_dialect == cxx98)
12170 maybe_warn_cpp0x ("scoped enums");
12172 /* Consume the `struct' or `class'. */
12173 cp_lexer_consume_token (parser->lexer);
12175 /* Parse the attributes. */
12176 attributes = cp_parser_attributes_opt (parser);
12178 /* Or, it might be `typename'. */
12179 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12182 /* Consume the `typename' token. */
12183 cp_lexer_consume_token (parser->lexer);
12184 /* Remember that it's a `typename' type. */
12185 tag_type = typename_type;
12187 /* Otherwise it must be a class-key. */
12190 tag_type = cp_parser_class_key (parser);
12191 if (tag_type == none_type)
12192 return error_mark_node;
12193 /* Parse the attributes. */
12194 attributes = cp_parser_attributes_opt (parser);
12197 /* Look for the `::' operator. */
12198 globalscope = cp_parser_global_scope_opt (parser,
12199 /*current_scope_valid_p=*/false);
12200 /* Look for the nested-name-specifier. */
12201 if (tag_type == typename_type && !globalscope)
12203 if (!cp_parser_nested_name_specifier (parser,
12204 /*typename_keyword_p=*/true,
12205 /*check_dependency_p=*/true,
12208 return error_mark_node;
12211 /* Even though `typename' is not present, the proposed resolution
12212 to Core Issue 180 says that in `class A<T>::B', `B' should be
12213 considered a type-name, even if `A<T>' is dependent. */
12214 cp_parser_nested_name_specifier_opt (parser,
12215 /*typename_keyword_p=*/true,
12216 /*check_dependency_p=*/true,
12219 /* For everything but enumeration types, consider a template-id.
12220 For an enumeration type, consider only a plain identifier. */
12221 if (tag_type != enum_type)
12223 bool template_p = false;
12226 /* Allow the `template' keyword. */
12227 template_p = cp_parser_optional_template_keyword (parser);
12228 /* If we didn't see `template', we don't know if there's a
12229 template-id or not. */
12231 cp_parser_parse_tentatively (parser);
12232 /* Parse the template-id. */
12233 token = cp_lexer_peek_token (parser->lexer);
12234 decl = cp_parser_template_id (parser, template_p,
12235 /*check_dependency_p=*/true,
12237 /* If we didn't find a template-id, look for an ordinary
12239 if (!template_p && !cp_parser_parse_definitely (parser))
12241 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12242 in effect, then we must assume that, upon instantiation, the
12243 template will correspond to a class. */
12244 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12245 && tag_type == typename_type)
12246 type = make_typename_type (parser->scope, decl,
12248 /*complain=*/tf_error);
12249 /* If the `typename' keyword is in effect and DECL is not a type
12250 decl. Then type is non existant. */
12251 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12254 type = TREE_TYPE (decl);
12259 token = cp_lexer_peek_token (parser->lexer);
12260 identifier = cp_parser_identifier (parser);
12262 if (identifier == error_mark_node)
12264 parser->scope = NULL_TREE;
12265 return error_mark_node;
12268 /* For a `typename', we needn't call xref_tag. */
12269 if (tag_type == typename_type
12270 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12271 return cp_parser_make_typename_type (parser, parser->scope,
12274 /* Look up a qualified name in the usual way. */
12278 tree ambiguous_decls;
12280 decl = cp_parser_lookup_name (parser, identifier,
12282 /*is_template=*/false,
12283 /*is_namespace=*/false,
12284 /*check_dependency=*/true,
12288 /* If the lookup was ambiguous, an error will already have been
12290 if (ambiguous_decls)
12291 return error_mark_node;
12293 /* If we are parsing friend declaration, DECL may be a
12294 TEMPLATE_DECL tree node here. However, we need to check
12295 whether this TEMPLATE_DECL results in valid code. Consider
12296 the following example:
12299 template <class T> class C {};
12302 template <class T> friend class N::C; // #1, valid code
12304 template <class T> class Y {
12305 friend class N::C; // #2, invalid code
12308 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12309 name lookup of `N::C'. We see that friend declaration must
12310 be template for the code to be valid. Note that
12311 processing_template_decl does not work here since it is
12312 always 1 for the above two cases. */
12314 decl = (cp_parser_maybe_treat_template_as_class
12315 (decl, /*tag_name_p=*/is_friend
12316 && parser->num_template_parameter_lists));
12318 if (TREE_CODE (decl) != TYPE_DECL)
12320 cp_parser_diagnose_invalid_type_name (parser,
12324 return error_mark_node;
12327 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12329 bool allow_template = (parser->num_template_parameter_lists
12330 || DECL_SELF_REFERENCE_P (decl));
12331 type = check_elaborated_type_specifier (tag_type, decl,
12334 if (type == error_mark_node)
12335 return error_mark_node;
12338 /* Forward declarations of nested types, such as
12343 are invalid unless all components preceding the final '::'
12344 are complete. If all enclosing types are complete, these
12345 declarations become merely pointless.
12347 Invalid forward declarations of nested types are errors
12348 caught elsewhere in parsing. Those that are pointless arrive
12351 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12352 && !is_friend && !processing_explicit_instantiation)
12353 warning (0, "declaration %qD does not declare anything", decl);
12355 type = TREE_TYPE (decl);
12359 /* An elaborated-type-specifier sometimes introduces a new type and
12360 sometimes names an existing type. Normally, the rule is that it
12361 introduces a new type only if there is not an existing type of
12362 the same name already in scope. For example, given:
12365 void f() { struct S s; }
12367 the `struct S' in the body of `f' is the same `struct S' as in
12368 the global scope; the existing definition is used. However, if
12369 there were no global declaration, this would introduce a new
12370 local class named `S'.
12372 An exception to this rule applies to the following code:
12374 namespace N { struct S; }
12376 Here, the elaborated-type-specifier names a new type
12377 unconditionally; even if there is already an `S' in the
12378 containing scope this declaration names a new type.
12379 This exception only applies if the elaborated-type-specifier
12380 forms the complete declaration:
12384 A declaration consisting solely of `class-key identifier ;' is
12385 either a redeclaration of the name in the current scope or a
12386 forward declaration of the identifier as a class name. It
12387 introduces the name into the current scope.
12389 We are in this situation precisely when the next token is a `;'.
12391 An exception to the exception is that a `friend' declaration does
12392 *not* name a new type; i.e., given:
12394 struct S { friend struct T; };
12396 `T' is not a new type in the scope of `S'.
12398 Also, `new struct S' or `sizeof (struct S)' never results in the
12399 definition of a new type; a new type can only be declared in a
12400 declaration context. */
12406 /* Friends have special name lookup rules. */
12407 ts = ts_within_enclosing_non_class;
12408 else if (is_declaration
12409 && cp_lexer_next_token_is (parser->lexer,
12411 /* This is a `class-key identifier ;' */
12417 (parser->num_template_parameter_lists
12418 && (cp_parser_next_token_starts_class_definition_p (parser)
12419 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12420 /* An unqualified name was used to reference this type, so
12421 there were no qualifying templates. */
12422 if (!cp_parser_check_template_parameters (parser,
12423 /*num_templates=*/0,
12425 /*declarator=*/NULL))
12426 return error_mark_node;
12427 type = xref_tag (tag_type, identifier, ts, template_p);
12431 if (type == error_mark_node)
12432 return error_mark_node;
12434 /* Allow attributes on forward declarations of classes. */
12437 if (TREE_CODE (type) == TYPENAME_TYPE)
12438 warning (OPT_Wattributes,
12439 "attributes ignored on uninstantiated type");
12440 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12441 && ! processing_explicit_instantiation)
12442 warning (OPT_Wattributes,
12443 "attributes ignored on template instantiation");
12444 else if (is_declaration && cp_parser_declares_only_class_p (parser))
12445 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12447 warning (OPT_Wattributes,
12448 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12451 if (tag_type != enum_type)
12452 cp_parser_check_class_key (tag_type, type);
12454 /* A "<" cannot follow an elaborated type specifier. If that
12455 happens, the user was probably trying to form a template-id. */
12456 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12461 /* Parse an enum-specifier.
12464 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12469 enum struct [C++0x]
12472 : type-specifier-seq
12475 enum-key attributes[opt] identifier [opt] enum-base [opt]
12476 { enumerator-list [opt] }attributes[opt]
12478 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12479 if the token stream isn't an enum-specifier after all. */
12482 cp_parser_enum_specifier (cp_parser* parser)
12487 bool scoped_enum_p = false;
12488 bool has_underlying_type = false;
12489 tree underlying_type = NULL_TREE;
12491 /* Parse tentatively so that we can back up if we don't find a
12493 cp_parser_parse_tentatively (parser);
12495 /* Caller guarantees that the current token is 'enum', an identifier
12496 possibly follows, and the token after that is an opening brace.
12497 If we don't have an identifier, fabricate an anonymous name for
12498 the enumeration being defined. */
12499 cp_lexer_consume_token (parser->lexer);
12501 /* Parse the "class" or "struct", which indicates a scoped
12502 enumeration type in C++0x. */
12503 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12504 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12506 if (cxx_dialect == cxx98)
12507 maybe_warn_cpp0x ("scoped enums");
12509 /* Consume the `struct' or `class' token. */
12510 cp_lexer_consume_token (parser->lexer);
12512 scoped_enum_p = true;
12515 attributes = cp_parser_attributes_opt (parser);
12517 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12518 identifier = cp_parser_identifier (parser);
12520 identifier = make_anon_name ();
12522 /* Check for the `:' that denotes a specified underlying type in C++0x.
12523 Note that a ':' could also indicate a bitfield width, however. */
12524 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12526 cp_decl_specifier_seq type_specifiers;
12528 /* Consume the `:'. */
12529 cp_lexer_consume_token (parser->lexer);
12531 /* Parse the type-specifier-seq. */
12532 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12535 /* At this point this is surely not elaborated type specifier. */
12536 if (!cp_parser_parse_definitely (parser))
12539 if (cxx_dialect == cxx98)
12540 maybe_warn_cpp0x ("scoped enums");
12542 has_underlying_type = true;
12544 /* If that didn't work, stop. */
12545 if (type_specifiers.type != error_mark_node)
12547 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12548 /*initialized=*/0, NULL);
12549 if (underlying_type == error_mark_node)
12550 underlying_type = NULL_TREE;
12554 /* Look for the `{' but don't consume it yet. */
12555 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12557 cp_parser_error (parser, "expected %<{%>");
12558 if (has_underlying_type)
12562 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12565 /* Issue an error message if type-definitions are forbidden here. */
12566 if (!cp_parser_check_type_definition (parser))
12567 type = error_mark_node;
12569 /* Create the new type. We do this before consuming the opening
12570 brace so the enum will be recorded as being on the line of its
12571 tag (or the 'enum' keyword, if there is no tag). */
12572 type = start_enum (identifier, underlying_type, scoped_enum_p);
12574 /* Consume the opening brace. */
12575 cp_lexer_consume_token (parser->lexer);
12577 if (type == error_mark_node)
12579 cp_parser_skip_to_end_of_block_or_statement (parser);
12580 return error_mark_node;
12583 /* If the next token is not '}', then there are some enumerators. */
12584 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12585 cp_parser_enumerator_list (parser, type);
12587 /* Consume the final '}'. */
12588 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12590 /* Look for trailing attributes to apply to this enumeration, and
12591 apply them if appropriate. */
12592 if (cp_parser_allow_gnu_extensions_p (parser))
12594 tree trailing_attr = cp_parser_attributes_opt (parser);
12595 trailing_attr = chainon (trailing_attr, attributes);
12596 cplus_decl_attributes (&type,
12598 (int) ATTR_FLAG_TYPE_IN_PLACE);
12601 /* Finish up the enumeration. */
12602 finish_enum (type);
12607 /* Parse an enumerator-list. The enumerators all have the indicated
12611 enumerator-definition
12612 enumerator-list , enumerator-definition */
12615 cp_parser_enumerator_list (cp_parser* parser, tree type)
12619 /* Parse an enumerator-definition. */
12620 cp_parser_enumerator_definition (parser, type);
12622 /* If the next token is not a ',', we've reached the end of
12624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12626 /* Otherwise, consume the `,' and keep going. */
12627 cp_lexer_consume_token (parser->lexer);
12628 /* If the next token is a `}', there is a trailing comma. */
12629 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12631 if (!in_system_header)
12632 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12638 /* Parse an enumerator-definition. The enumerator has the indicated
12641 enumerator-definition:
12643 enumerator = constant-expression
12649 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12654 /* Look for the identifier. */
12655 identifier = cp_parser_identifier (parser);
12656 if (identifier == error_mark_node)
12659 /* If the next token is an '=', then there is an explicit value. */
12660 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12662 /* Consume the `=' token. */
12663 cp_lexer_consume_token (parser->lexer);
12664 /* Parse the value. */
12665 value = cp_parser_constant_expression (parser,
12666 /*allow_non_constant_p=*/false,
12672 /* If we are processing a template, make sure the initializer of the
12673 enumerator doesn't contain any bare template parameter pack. */
12674 if (check_for_bare_parameter_packs (value))
12675 value = error_mark_node;
12677 /* Create the enumerator. */
12678 build_enumerator (identifier, value, type);
12681 /* Parse a namespace-name.
12684 original-namespace-name
12687 Returns the NAMESPACE_DECL for the namespace. */
12690 cp_parser_namespace_name (cp_parser* parser)
12693 tree namespace_decl;
12695 cp_token *token = cp_lexer_peek_token (parser->lexer);
12697 /* Get the name of the namespace. */
12698 identifier = cp_parser_identifier (parser);
12699 if (identifier == error_mark_node)
12700 return error_mark_node;
12702 /* Look up the identifier in the currently active scope. Look only
12703 for namespaces, due to:
12705 [basic.lookup.udir]
12707 When looking up a namespace-name in a using-directive or alias
12708 definition, only namespace names are considered.
12712 [basic.lookup.qual]
12714 During the lookup of a name preceding the :: scope resolution
12715 operator, object, function, and enumerator names are ignored.
12717 (Note that cp_parser_qualifying_entity only calls this
12718 function if the token after the name is the scope resolution
12720 namespace_decl = cp_parser_lookup_name (parser, identifier,
12722 /*is_template=*/false,
12723 /*is_namespace=*/true,
12724 /*check_dependency=*/true,
12725 /*ambiguous_decls=*/NULL,
12727 /* If it's not a namespace, issue an error. */
12728 if (namespace_decl == error_mark_node
12729 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12731 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12732 error_at (token->location, "%qD is not a namespace-name", identifier);
12733 cp_parser_error (parser, "expected namespace-name");
12734 namespace_decl = error_mark_node;
12737 return namespace_decl;
12740 /* Parse a namespace-definition.
12742 namespace-definition:
12743 named-namespace-definition
12744 unnamed-namespace-definition
12746 named-namespace-definition:
12747 original-namespace-definition
12748 extension-namespace-definition
12750 original-namespace-definition:
12751 namespace identifier { namespace-body }
12753 extension-namespace-definition:
12754 namespace original-namespace-name { namespace-body }
12756 unnamed-namespace-definition:
12757 namespace { namespace-body } */
12760 cp_parser_namespace_definition (cp_parser* parser)
12762 tree identifier, attribs;
12763 bool has_visibility;
12766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12769 cp_lexer_consume_token (parser->lexer);
12774 /* Look for the `namespace' keyword. */
12775 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12777 /* Get the name of the namespace. We do not attempt to distinguish
12778 between an original-namespace-definition and an
12779 extension-namespace-definition at this point. The semantic
12780 analysis routines are responsible for that. */
12781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12782 identifier = cp_parser_identifier (parser);
12784 identifier = NULL_TREE;
12786 /* Parse any specified attributes. */
12787 attribs = cp_parser_attributes_opt (parser);
12789 /* Look for the `{' to start the namespace. */
12790 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12791 /* Start the namespace. */
12792 push_namespace (identifier);
12794 /* "inline namespace" is equivalent to a stub namespace definition
12795 followed by a strong using directive. */
12798 tree name_space = current_namespace;
12799 /* Set up namespace association. */
12800 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12801 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12802 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12803 /* Import the contents of the inline namespace. */
12805 do_using_directive (name_space);
12806 push_namespace (identifier);
12809 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12811 /* Parse the body of the namespace. */
12812 cp_parser_namespace_body (parser);
12814 #ifdef HANDLE_PRAGMA_VISIBILITY
12815 if (has_visibility)
12819 /* Finish the namespace. */
12821 /* Look for the final `}'. */
12822 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12825 /* Parse a namespace-body.
12828 declaration-seq [opt] */
12831 cp_parser_namespace_body (cp_parser* parser)
12833 cp_parser_declaration_seq_opt (parser);
12836 /* Parse a namespace-alias-definition.
12838 namespace-alias-definition:
12839 namespace identifier = qualified-namespace-specifier ; */
12842 cp_parser_namespace_alias_definition (cp_parser* parser)
12845 tree namespace_specifier;
12847 cp_token *token = cp_lexer_peek_token (parser->lexer);
12849 /* Look for the `namespace' keyword. */
12850 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12851 /* Look for the identifier. */
12852 identifier = cp_parser_identifier (parser);
12853 if (identifier == error_mark_node)
12855 /* Look for the `=' token. */
12856 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12857 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12859 error_at (token->location, "%<namespace%> definition is not allowed here");
12860 /* Skip the definition. */
12861 cp_lexer_consume_token (parser->lexer);
12862 if (cp_parser_skip_to_closing_brace (parser))
12863 cp_lexer_consume_token (parser->lexer);
12866 cp_parser_require (parser, CPP_EQ, "%<=%>");
12867 /* Look for the qualified-namespace-specifier. */
12868 namespace_specifier
12869 = cp_parser_qualified_namespace_specifier (parser);
12870 /* Look for the `;' token. */
12871 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12873 /* Register the alias in the symbol table. */
12874 do_namespace_alias (identifier, namespace_specifier);
12877 /* Parse a qualified-namespace-specifier.
12879 qualified-namespace-specifier:
12880 :: [opt] nested-name-specifier [opt] namespace-name
12882 Returns a NAMESPACE_DECL corresponding to the specified
12886 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12888 /* Look for the optional `::'. */
12889 cp_parser_global_scope_opt (parser,
12890 /*current_scope_valid_p=*/false);
12892 /* Look for the optional nested-name-specifier. */
12893 cp_parser_nested_name_specifier_opt (parser,
12894 /*typename_keyword_p=*/false,
12895 /*check_dependency_p=*/true,
12897 /*is_declaration=*/true);
12899 return cp_parser_namespace_name (parser);
12902 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12903 access declaration.
12906 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12907 using :: unqualified-id ;
12909 access-declaration:
12915 cp_parser_using_declaration (cp_parser* parser,
12916 bool access_declaration_p)
12919 bool typename_p = false;
12920 bool global_scope_p;
12925 if (access_declaration_p)
12926 cp_parser_parse_tentatively (parser);
12929 /* Look for the `using' keyword. */
12930 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12932 /* Peek at the next token. */
12933 token = cp_lexer_peek_token (parser->lexer);
12934 /* See if it's `typename'. */
12935 if (token->keyword == RID_TYPENAME)
12937 /* Remember that we've seen it. */
12939 /* Consume the `typename' token. */
12940 cp_lexer_consume_token (parser->lexer);
12944 /* Look for the optional global scope qualification. */
12946 = (cp_parser_global_scope_opt (parser,
12947 /*current_scope_valid_p=*/false)
12950 /* If we saw `typename', or didn't see `::', then there must be a
12951 nested-name-specifier present. */
12952 if (typename_p || !global_scope_p)
12953 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12954 /*check_dependency_p=*/true,
12956 /*is_declaration=*/true);
12957 /* Otherwise, we could be in either of the two productions. In that
12958 case, treat the nested-name-specifier as optional. */
12960 qscope = cp_parser_nested_name_specifier_opt (parser,
12961 /*typename_keyword_p=*/false,
12962 /*check_dependency_p=*/true,
12964 /*is_declaration=*/true);
12966 qscope = global_namespace;
12968 if (access_declaration_p && cp_parser_error_occurred (parser))
12969 /* Something has already gone wrong; there's no need to parse
12970 further. Since an error has occurred, the return value of
12971 cp_parser_parse_definitely will be false, as required. */
12972 return cp_parser_parse_definitely (parser);
12974 token = cp_lexer_peek_token (parser->lexer);
12975 /* Parse the unqualified-id. */
12976 identifier = cp_parser_unqualified_id (parser,
12977 /*template_keyword_p=*/false,
12978 /*check_dependency_p=*/true,
12979 /*declarator_p=*/true,
12980 /*optional_p=*/false);
12982 if (access_declaration_p)
12984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12985 cp_parser_simulate_error (parser);
12986 if (!cp_parser_parse_definitely (parser))
12990 /* The function we call to handle a using-declaration is different
12991 depending on what scope we are in. */
12992 if (qscope == error_mark_node || identifier == error_mark_node)
12994 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12995 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12996 /* [namespace.udecl]
12998 A using declaration shall not name a template-id. */
12999 error_at (token->location,
13000 "a template-id may not appear in a using-declaration");
13003 if (at_class_scope_p ())
13005 /* Create the USING_DECL. */
13006 decl = do_class_using_decl (parser->scope, identifier);
13008 if (check_for_bare_parameter_packs (decl))
13011 /* Add it to the list of members in this class. */
13012 finish_member_declaration (decl);
13016 decl = cp_parser_lookup_name_simple (parser,
13019 if (decl == error_mark_node)
13020 cp_parser_name_lookup_error (parser, identifier,
13023 else if (check_for_bare_parameter_packs (decl))
13025 else if (!at_namespace_scope_p ())
13026 do_local_using_decl (decl, qscope, identifier);
13028 do_toplevel_using_decl (decl, qscope, identifier);
13032 /* Look for the final `;'. */
13033 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13038 /* Parse a using-directive.
13041 using namespace :: [opt] nested-name-specifier [opt]
13042 namespace-name ; */
13045 cp_parser_using_directive (cp_parser* parser)
13047 tree namespace_decl;
13050 /* Look for the `using' keyword. */
13051 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13052 /* And the `namespace' keyword. */
13053 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13054 /* Look for the optional `::' operator. */
13055 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13056 /* And the optional nested-name-specifier. */
13057 cp_parser_nested_name_specifier_opt (parser,
13058 /*typename_keyword_p=*/false,
13059 /*check_dependency_p=*/true,
13061 /*is_declaration=*/true);
13062 /* Get the namespace being used. */
13063 namespace_decl = cp_parser_namespace_name (parser);
13064 /* And any specified attributes. */
13065 attribs = cp_parser_attributes_opt (parser);
13066 /* Update the symbol table. */
13067 parse_using_directive (namespace_decl, attribs);
13068 /* Look for the final `;'. */
13069 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13072 /* Parse an asm-definition.
13075 asm ( string-literal ) ;
13080 asm volatile [opt] ( string-literal ) ;
13081 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13082 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13083 : asm-operand-list [opt] ) ;
13084 asm volatile [opt] ( string-literal : asm-operand-list [opt]
13085 : asm-operand-list [opt]
13086 : asm-clobber-list [opt] ) ;
13087 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13088 : asm-clobber-list [opt]
13089 : asm-goto-list ) ; */
13092 cp_parser_asm_definition (cp_parser* parser)
13095 tree outputs = NULL_TREE;
13096 tree inputs = NULL_TREE;
13097 tree clobbers = NULL_TREE;
13098 tree labels = NULL_TREE;
13100 bool volatile_p = false;
13101 bool extended_p = false;
13102 bool invalid_inputs_p = false;
13103 bool invalid_outputs_p = false;
13104 bool goto_p = false;
13105 const char *missing = NULL;
13107 /* Look for the `asm' keyword. */
13108 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13109 /* See if the next token is `volatile'. */
13110 if (cp_parser_allow_gnu_extensions_p (parser)
13111 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13113 /* Remember that we saw the `volatile' keyword. */
13115 /* Consume the token. */
13116 cp_lexer_consume_token (parser->lexer);
13118 if (cp_parser_allow_gnu_extensions_p (parser)
13119 && parser->in_function_body
13120 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13122 /* Remember that we saw the `goto' keyword. */
13124 /* Consume the token. */
13125 cp_lexer_consume_token (parser->lexer);
13127 /* Look for the opening `('. */
13128 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13130 /* Look for the string. */
13131 string = cp_parser_string_literal (parser, false, false);
13132 if (string == error_mark_node)
13134 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13135 /*consume_paren=*/true);
13139 /* If we're allowing GNU extensions, check for the extended assembly
13140 syntax. Unfortunately, the `:' tokens need not be separated by
13141 a space in C, and so, for compatibility, we tolerate that here
13142 too. Doing that means that we have to treat the `::' operator as
13144 if (cp_parser_allow_gnu_extensions_p (parser)
13145 && parser->in_function_body
13146 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13147 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13149 bool inputs_p = false;
13150 bool clobbers_p = false;
13151 bool labels_p = false;
13153 /* The extended syntax was used. */
13156 /* Look for outputs. */
13157 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13159 /* Consume the `:'. */
13160 cp_lexer_consume_token (parser->lexer);
13161 /* Parse the output-operands. */
13162 if (cp_lexer_next_token_is_not (parser->lexer,
13164 && cp_lexer_next_token_is_not (parser->lexer,
13166 && cp_lexer_next_token_is_not (parser->lexer,
13169 outputs = cp_parser_asm_operand_list (parser);
13171 if (outputs == error_mark_node)
13172 invalid_outputs_p = true;
13174 /* If the next token is `::', there are no outputs, and the
13175 next token is the beginning of the inputs. */
13176 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13177 /* The inputs are coming next. */
13180 /* Look for inputs. */
13182 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13184 /* Consume the `:' or `::'. */
13185 cp_lexer_consume_token (parser->lexer);
13186 /* Parse the output-operands. */
13187 if (cp_lexer_next_token_is_not (parser->lexer,
13189 && cp_lexer_next_token_is_not (parser->lexer,
13191 && cp_lexer_next_token_is_not (parser->lexer,
13193 inputs = cp_parser_asm_operand_list (parser);
13195 if (inputs == error_mark_node)
13196 invalid_inputs_p = true;
13198 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13199 /* The clobbers are coming next. */
13202 /* Look for clobbers. */
13204 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13207 /* Consume the `:' or `::'. */
13208 cp_lexer_consume_token (parser->lexer);
13209 /* Parse the clobbers. */
13210 if (cp_lexer_next_token_is_not (parser->lexer,
13212 && cp_lexer_next_token_is_not (parser->lexer,
13214 clobbers = cp_parser_asm_clobber_list (parser);
13217 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13218 /* The labels are coming next. */
13221 /* Look for labels. */
13223 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13226 /* Consume the `:' or `::'. */
13227 cp_lexer_consume_token (parser->lexer);
13228 /* Parse the labels. */
13229 labels = cp_parser_asm_label_list (parser);
13232 if (goto_p && !labels_p)
13233 missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13236 missing = "%<:%> or %<::%>";
13238 /* Look for the closing `)'. */
13239 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13240 missing ? missing : "%<)%>"))
13241 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13242 /*consume_paren=*/true);
13243 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13245 if (!invalid_inputs_p && !invalid_outputs_p)
13247 /* Create the ASM_EXPR. */
13248 if (parser->in_function_body)
13250 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13251 inputs, clobbers, labels);
13252 /* If the extended syntax was not used, mark the ASM_EXPR. */
13255 tree temp = asm_stmt;
13256 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13257 temp = TREE_OPERAND (temp, 0);
13259 ASM_INPUT_P (temp) = 1;
13263 cgraph_add_asm_node (string);
13267 /* Declarators [gram.dcl.decl] */
13269 /* Parse an init-declarator.
13272 declarator initializer [opt]
13277 declarator asm-specification [opt] attributes [opt] initializer [opt]
13279 function-definition:
13280 decl-specifier-seq [opt] declarator ctor-initializer [opt]
13282 decl-specifier-seq [opt] declarator function-try-block
13286 function-definition:
13287 __extension__ function-definition
13289 The DECL_SPECIFIERS apply to this declarator. Returns a
13290 representation of the entity declared. If MEMBER_P is TRUE, then
13291 this declarator appears in a class scope. The new DECL created by
13292 this declarator is returned.
13294 The CHECKS are access checks that should be performed once we know
13295 what entity is being declared (and, therefore, what classes have
13298 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13299 for a function-definition here as well. If the declarator is a
13300 declarator for a function-definition, *FUNCTION_DEFINITION_P will
13301 be TRUE upon return. By that point, the function-definition will
13302 have been completely parsed.
13304 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13308 cp_parser_init_declarator (cp_parser* parser,
13309 cp_decl_specifier_seq *decl_specifiers,
13310 VEC (deferred_access_check,gc)* checks,
13311 bool function_definition_allowed_p,
13313 int declares_class_or_enum,
13314 bool* function_definition_p)
13316 cp_token *token = NULL, *asm_spec_start_token = NULL,
13317 *attributes_start_token = NULL;
13318 cp_declarator *declarator;
13319 tree prefix_attributes;
13321 tree asm_specification;
13323 tree decl = NULL_TREE;
13325 int is_initialized;
13326 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
13327 initialized with "= ..", CPP_OPEN_PAREN if initialized with
13329 enum cpp_ttype initialization_kind;
13330 bool is_direct_init = false;
13331 bool is_non_constant_init;
13332 int ctor_dtor_or_conv_p;
13334 tree pushed_scope = NULL;
13336 /* Gather the attributes that were provided with the
13337 decl-specifiers. */
13338 prefix_attributes = decl_specifiers->attributes;
13340 /* Assume that this is not the declarator for a function
13342 if (function_definition_p)
13343 *function_definition_p = false;
13345 /* Defer access checks while parsing the declarator; we cannot know
13346 what names are accessible until we know what is being
13348 resume_deferring_access_checks ();
13350 /* Parse the declarator. */
13351 token = cp_lexer_peek_token (parser->lexer);
13353 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13354 &ctor_dtor_or_conv_p,
13355 /*parenthesized_p=*/NULL,
13356 /*member_p=*/false);
13357 /* Gather up the deferred checks. */
13358 stop_deferring_access_checks ();
13360 /* If the DECLARATOR was erroneous, there's no need to go
13362 if (declarator == cp_error_declarator)
13363 return error_mark_node;
13365 /* Check that the number of template-parameter-lists is OK. */
13366 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13368 return error_mark_node;
13370 if (declares_class_or_enum & 2)
13371 cp_parser_check_for_definition_in_return_type (declarator,
13372 decl_specifiers->type,
13373 decl_specifiers->type_location);
13375 /* Figure out what scope the entity declared by the DECLARATOR is
13376 located in. `grokdeclarator' sometimes changes the scope, so
13377 we compute it now. */
13378 scope = get_scope_of_declarator (declarator);
13380 /* If we're allowing GNU extensions, look for an asm-specification
13382 if (cp_parser_allow_gnu_extensions_p (parser))
13384 /* Look for an asm-specification. */
13385 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13386 asm_specification = cp_parser_asm_specification_opt (parser);
13387 /* And attributes. */
13388 attributes_start_token = cp_lexer_peek_token (parser->lexer);
13389 attributes = cp_parser_attributes_opt (parser);
13393 asm_specification = NULL_TREE;
13394 attributes = NULL_TREE;
13397 /* Peek at the next token. */
13398 token = cp_lexer_peek_token (parser->lexer);
13399 /* Check to see if the token indicates the start of a
13400 function-definition. */
13401 if (function_declarator_p (declarator)
13402 && cp_parser_token_starts_function_definition_p (token))
13404 if (!function_definition_allowed_p)
13406 /* If a function-definition should not appear here, issue an
13408 cp_parser_error (parser,
13409 "a function-definition is not allowed here");
13410 return error_mark_node;
13414 location_t func_brace_location
13415 = cp_lexer_peek_token (parser->lexer)->location;
13417 /* Neither attributes nor an asm-specification are allowed
13418 on a function-definition. */
13419 if (asm_specification)
13420 error_at (asm_spec_start_token->location,
13421 "an asm-specification is not allowed "
13422 "on a function-definition");
13424 error_at (attributes_start_token->location,
13425 "attributes are not allowed on a function-definition");
13426 /* This is a function-definition. */
13427 *function_definition_p = true;
13429 /* Parse the function definition. */
13431 decl = cp_parser_save_member_function_body (parser,
13434 prefix_attributes);
13437 = (cp_parser_function_definition_from_specifiers_and_declarator
13438 (parser, decl_specifiers, prefix_attributes, declarator));
13440 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13442 /* This is where the prologue starts... */
13443 DECL_STRUCT_FUNCTION (decl)->function_start_locus
13444 = func_brace_location;
13453 Only in function declarations for constructors, destructors, and
13454 type conversions can the decl-specifier-seq be omitted.
13456 We explicitly postpone this check past the point where we handle
13457 function-definitions because we tolerate function-definitions
13458 that are missing their return types in some modes. */
13459 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13461 cp_parser_error (parser,
13462 "expected constructor, destructor, or type conversion");
13463 return error_mark_node;
13466 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
13467 if (token->type == CPP_EQ
13468 || token->type == CPP_OPEN_PAREN
13469 || token->type == CPP_OPEN_BRACE)
13471 is_initialized = SD_INITIALIZED;
13472 initialization_kind = token->type;
13474 if (token->type == CPP_EQ
13475 && function_declarator_p (declarator))
13477 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13478 if (t2->keyword == RID_DEFAULT)
13479 is_initialized = SD_DEFAULTED;
13480 else if (t2->keyword == RID_DELETE)
13481 is_initialized = SD_DELETED;
13486 /* If the init-declarator isn't initialized and isn't followed by a
13487 `,' or `;', it's not a valid init-declarator. */
13488 if (token->type != CPP_COMMA
13489 && token->type != CPP_SEMICOLON)
13491 cp_parser_error (parser, "expected initializer");
13492 return error_mark_node;
13494 is_initialized = SD_UNINITIALIZED;
13495 initialization_kind = CPP_EOF;
13498 /* Because start_decl has side-effects, we should only call it if we
13499 know we're going ahead. By this point, we know that we cannot
13500 possibly be looking at any other construct. */
13501 cp_parser_commit_to_tentative_parse (parser);
13503 /* If the decl specifiers were bad, issue an error now that we're
13504 sure this was intended to be a declarator. Then continue
13505 declaring the variable(s), as int, to try to cut down on further
13507 if (decl_specifiers->any_specifiers_p
13508 && decl_specifiers->type == error_mark_node)
13510 cp_parser_error (parser, "invalid type in declaration");
13511 decl_specifiers->type = integer_type_node;
13514 /* Check to see whether or not this declaration is a friend. */
13515 friend_p = cp_parser_friend_p (decl_specifiers);
13517 /* Enter the newly declared entry in the symbol table. If we're
13518 processing a declaration in a class-specifier, we wait until
13519 after processing the initializer. */
13522 if (parser->in_unbraced_linkage_specification_p)
13523 decl_specifiers->storage_class = sc_extern;
13524 decl = start_decl (declarator, decl_specifiers,
13525 is_initialized, attributes, prefix_attributes,
13529 /* Enter the SCOPE. That way unqualified names appearing in the
13530 initializer will be looked up in SCOPE. */
13531 pushed_scope = push_scope (scope);
13533 /* Perform deferred access control checks, now that we know in which
13534 SCOPE the declared entity resides. */
13535 if (!member_p && decl)
13537 tree saved_current_function_decl = NULL_TREE;
13539 /* If the entity being declared is a function, pretend that we
13540 are in its scope. If it is a `friend', it may have access to
13541 things that would not otherwise be accessible. */
13542 if (TREE_CODE (decl) == FUNCTION_DECL)
13544 saved_current_function_decl = current_function_decl;
13545 current_function_decl = decl;
13548 /* Perform access checks for template parameters. */
13549 cp_parser_perform_template_parameter_access_checks (checks);
13551 /* Perform the access control checks for the declarator and the
13552 decl-specifiers. */
13553 perform_deferred_access_checks ();
13555 /* Restore the saved value. */
13556 if (TREE_CODE (decl) == FUNCTION_DECL)
13557 current_function_decl = saved_current_function_decl;
13560 /* Parse the initializer. */
13561 initializer = NULL_TREE;
13562 is_direct_init = false;
13563 is_non_constant_init = true;
13564 if (is_initialized)
13566 if (function_declarator_p (declarator))
13568 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13569 if (initialization_kind == CPP_EQ)
13570 initializer = cp_parser_pure_specifier (parser);
13573 /* If the declaration was erroneous, we don't really
13574 know what the user intended, so just silently
13575 consume the initializer. */
13576 if (decl != error_mark_node)
13577 error_at (initializer_start_token->location,
13578 "initializer provided for function");
13579 cp_parser_skip_to_closing_parenthesis (parser,
13580 /*recovering=*/true,
13581 /*or_comma=*/false,
13582 /*consume_paren=*/true);
13587 /* We want to record the extra mangling scope for in-class
13588 initializers of class members and initializers of static data
13589 member templates. The former is a C++0x feature which isn't
13590 implemented yet, and I expect it will involve deferring
13591 parsing of the initializer until end of class as with default
13592 arguments. So right here we only handle the latter. */
13593 if (!member_p && processing_template_decl)
13594 start_lambda_scope (decl);
13595 initializer = cp_parser_initializer (parser,
13597 &is_non_constant_init);
13598 if (!member_p && processing_template_decl)
13599 finish_lambda_scope ();
13603 /* The old parser allows attributes to appear after a parenthesized
13604 initializer. Mark Mitchell proposed removing this functionality
13605 on the GCC mailing lists on 2002-08-13. This parser accepts the
13606 attributes -- but ignores them. */
13607 if (cp_parser_allow_gnu_extensions_p (parser)
13608 && initialization_kind == CPP_OPEN_PAREN)
13609 if (cp_parser_attributes_opt (parser))
13610 warning (OPT_Wattributes,
13611 "attributes after parenthesized initializer ignored");
13613 /* For an in-class declaration, use `grokfield' to create the
13619 pop_scope (pushed_scope);
13620 pushed_scope = false;
13622 decl = grokfield (declarator, decl_specifiers,
13623 initializer, !is_non_constant_init,
13624 /*asmspec=*/NULL_TREE,
13625 prefix_attributes);
13626 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13627 cp_parser_save_default_args (parser, decl);
13630 /* Finish processing the declaration. But, skip friend
13632 if (!friend_p && decl && decl != error_mark_node)
13634 cp_finish_decl (decl,
13635 initializer, !is_non_constant_init,
13637 /* If the initializer is in parentheses, then this is
13638 a direct-initialization, which means that an
13639 `explicit' constructor is OK. Otherwise, an
13640 `explicit' constructor cannot be used. */
13641 ((is_direct_init || !is_initialized)
13642 ? 0 : LOOKUP_ONLYCONVERTING));
13644 else if ((cxx_dialect != cxx98) && friend_p
13645 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13646 /* Core issue #226 (C++0x only): A default template-argument
13647 shall not be specified in a friend class template
13649 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13650 /*is_partial=*/0, /*is_friend_decl=*/1);
13652 if (!friend_p && pushed_scope)
13653 pop_scope (pushed_scope);
13658 /* Parse a declarator.
13662 ptr-operator declarator
13664 abstract-declarator:
13665 ptr-operator abstract-declarator [opt]
13666 direct-abstract-declarator
13671 attributes [opt] direct-declarator
13672 attributes [opt] ptr-operator declarator
13674 abstract-declarator:
13675 attributes [opt] ptr-operator abstract-declarator [opt]
13676 attributes [opt] direct-abstract-declarator
13678 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13679 detect constructor, destructor or conversion operators. It is set
13680 to -1 if the declarator is a name, and +1 if it is a
13681 function. Otherwise it is set to zero. Usually you just want to
13682 test for >0, but internally the negative value is used.
13684 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13685 a decl-specifier-seq unless it declares a constructor, destructor,
13686 or conversion. It might seem that we could check this condition in
13687 semantic analysis, rather than parsing, but that makes it difficult
13688 to handle something like `f()'. We want to notice that there are
13689 no decl-specifiers, and therefore realize that this is an
13690 expression, not a declaration.)
13692 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13693 the declarator is a direct-declarator of the form "(...)".
13695 MEMBER_P is true iff this declarator is a member-declarator. */
13697 static cp_declarator *
13698 cp_parser_declarator (cp_parser* parser,
13699 cp_parser_declarator_kind dcl_kind,
13700 int* ctor_dtor_or_conv_p,
13701 bool* parenthesized_p,
13705 cp_declarator *declarator;
13706 enum tree_code code;
13707 cp_cv_quals cv_quals;
13709 tree attributes = NULL_TREE;
13711 /* Assume this is not a constructor, destructor, or type-conversion
13713 if (ctor_dtor_or_conv_p)
13714 *ctor_dtor_or_conv_p = 0;
13716 if (cp_parser_allow_gnu_extensions_p (parser))
13717 attributes = cp_parser_attributes_opt (parser);
13719 /* Peek at the next token. */
13720 token = cp_lexer_peek_token (parser->lexer);
13722 /* Check for the ptr-operator production. */
13723 cp_parser_parse_tentatively (parser);
13724 /* Parse the ptr-operator. */
13725 code = cp_parser_ptr_operator (parser,
13728 /* If that worked, then we have a ptr-operator. */
13729 if (cp_parser_parse_definitely (parser))
13731 /* If a ptr-operator was found, then this declarator was not
13733 if (parenthesized_p)
13734 *parenthesized_p = true;
13735 /* The dependent declarator is optional if we are parsing an
13736 abstract-declarator. */
13737 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13738 cp_parser_parse_tentatively (parser);
13740 /* Parse the dependent declarator. */
13741 declarator = cp_parser_declarator (parser, dcl_kind,
13742 /*ctor_dtor_or_conv_p=*/NULL,
13743 /*parenthesized_p=*/NULL,
13744 /*member_p=*/false);
13746 /* If we are parsing an abstract-declarator, we must handle the
13747 case where the dependent declarator is absent. */
13748 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13749 && !cp_parser_parse_definitely (parser))
13752 declarator = cp_parser_make_indirect_declarator
13753 (code, class_type, cv_quals, declarator);
13755 /* Everything else is a direct-declarator. */
13758 if (parenthesized_p)
13759 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13761 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13762 ctor_dtor_or_conv_p,
13766 if (attributes && declarator && declarator != cp_error_declarator)
13767 declarator->attributes = attributes;
13772 /* Parse a direct-declarator or direct-abstract-declarator.
13776 direct-declarator ( parameter-declaration-clause )
13777 cv-qualifier-seq [opt]
13778 exception-specification [opt]
13779 direct-declarator [ constant-expression [opt] ]
13782 direct-abstract-declarator:
13783 direct-abstract-declarator [opt]
13784 ( parameter-declaration-clause )
13785 cv-qualifier-seq [opt]
13786 exception-specification [opt]
13787 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13788 ( abstract-declarator )
13790 Returns a representation of the declarator. DCL_KIND is
13791 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13792 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13793 we are parsing a direct-declarator. It is
13794 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13795 of ambiguity we prefer an abstract declarator, as per
13796 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13797 cp_parser_declarator. */
13799 static cp_declarator *
13800 cp_parser_direct_declarator (cp_parser* parser,
13801 cp_parser_declarator_kind dcl_kind,
13802 int* ctor_dtor_or_conv_p,
13806 cp_declarator *declarator = NULL;
13807 tree scope = NULL_TREE;
13808 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13809 bool saved_in_declarator_p = parser->in_declarator_p;
13811 tree pushed_scope = NULL_TREE;
13815 /* Peek at the next token. */
13816 token = cp_lexer_peek_token (parser->lexer);
13817 if (token->type == CPP_OPEN_PAREN)
13819 /* This is either a parameter-declaration-clause, or a
13820 parenthesized declarator. When we know we are parsing a
13821 named declarator, it must be a parenthesized declarator
13822 if FIRST is true. For instance, `(int)' is a
13823 parameter-declaration-clause, with an omitted
13824 direct-abstract-declarator. But `((*))', is a
13825 parenthesized abstract declarator. Finally, when T is a
13826 template parameter `(T)' is a
13827 parameter-declaration-clause, and not a parenthesized
13830 We first try and parse a parameter-declaration-clause,
13831 and then try a nested declarator (if FIRST is true).
13833 It is not an error for it not to be a
13834 parameter-declaration-clause, even when FIRST is
13840 The first is the declaration of a function while the
13841 second is the definition of a variable, including its
13844 Having seen only the parenthesis, we cannot know which of
13845 these two alternatives should be selected. Even more
13846 complex are examples like:
13851 The former is a function-declaration; the latter is a
13852 variable initialization.
13854 Thus again, we try a parameter-declaration-clause, and if
13855 that fails, we back out and return. */
13857 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13860 unsigned saved_num_template_parameter_lists;
13861 bool is_declarator = false;
13864 /* In a member-declarator, the only valid interpretation
13865 of a parenthesis is the start of a
13866 parameter-declaration-clause. (It is invalid to
13867 initialize a static data member with a parenthesized
13868 initializer; only the "=" form of initialization is
13871 cp_parser_parse_tentatively (parser);
13873 /* Consume the `('. */
13874 cp_lexer_consume_token (parser->lexer);
13877 /* If this is going to be an abstract declarator, we're
13878 in a declarator and we can't have default args. */
13879 parser->default_arg_ok_p = false;
13880 parser->in_declarator_p = true;
13883 /* Inside the function parameter list, surrounding
13884 template-parameter-lists do not apply. */
13885 saved_num_template_parameter_lists
13886 = parser->num_template_parameter_lists;
13887 parser->num_template_parameter_lists = 0;
13889 begin_scope (sk_function_parms, NULL_TREE);
13891 /* Parse the parameter-declaration-clause. */
13892 params = cp_parser_parameter_declaration_clause (parser);
13894 parser->num_template_parameter_lists
13895 = saved_num_template_parameter_lists;
13897 /* If all went well, parse the cv-qualifier-seq and the
13898 exception-specification. */
13899 if (member_p || cp_parser_parse_definitely (parser))
13901 cp_cv_quals cv_quals;
13902 tree exception_specification;
13905 is_declarator = true;
13907 if (ctor_dtor_or_conv_p)
13908 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13910 /* Consume the `)'. */
13911 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13913 /* Parse the cv-qualifier-seq. */
13914 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13915 /* And the exception-specification. */
13916 exception_specification
13917 = cp_parser_exception_specification_opt (parser);
13920 = cp_parser_late_return_type_opt (parser);
13922 /* Create the function-declarator. */
13923 declarator = make_call_declarator (declarator,
13926 exception_specification,
13928 /* Any subsequent parameter lists are to do with
13929 return type, so are not those of the declared
13931 parser->default_arg_ok_p = false;
13934 /* Remove the function parms from scope. */
13935 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13936 pop_binding (DECL_NAME (t), t);
13940 /* Repeat the main loop. */
13944 /* If this is the first, we can try a parenthesized
13948 bool saved_in_type_id_in_expr_p;
13950 parser->default_arg_ok_p = saved_default_arg_ok_p;
13951 parser->in_declarator_p = saved_in_declarator_p;
13953 /* Consume the `('. */
13954 cp_lexer_consume_token (parser->lexer);
13955 /* Parse the nested declarator. */
13956 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13957 parser->in_type_id_in_expr_p = true;
13959 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13960 /*parenthesized_p=*/NULL,
13962 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13964 /* Expect a `)'. */
13965 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13966 declarator = cp_error_declarator;
13967 if (declarator == cp_error_declarator)
13970 goto handle_declarator;
13972 /* Otherwise, we must be done. */
13976 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13977 && token->type == CPP_OPEN_SQUARE)
13979 /* Parse an array-declarator. */
13982 if (ctor_dtor_or_conv_p)
13983 *ctor_dtor_or_conv_p = 0;
13986 parser->default_arg_ok_p = false;
13987 parser->in_declarator_p = true;
13988 /* Consume the `['. */
13989 cp_lexer_consume_token (parser->lexer);
13990 /* Peek at the next token. */
13991 token = cp_lexer_peek_token (parser->lexer);
13992 /* If the next token is `]', then there is no
13993 constant-expression. */
13994 if (token->type != CPP_CLOSE_SQUARE)
13996 bool non_constant_p;
13999 = cp_parser_constant_expression (parser,
14000 /*allow_non_constant=*/true,
14002 if (!non_constant_p)
14003 bounds = fold_non_dependent_expr (bounds);
14004 /* Normally, the array bound must be an integral constant
14005 expression. However, as an extension, we allow VLAs
14006 in function scopes. */
14007 else if (!parser->in_function_body)
14009 error_at (token->location,
14010 "array bound is not an integer constant");
14011 bounds = error_mark_node;
14013 else if (processing_template_decl && !error_operand_p (bounds))
14015 /* Remember this wasn't a constant-expression. */
14016 bounds = build_nop (TREE_TYPE (bounds), bounds);
14017 TREE_SIDE_EFFECTS (bounds) = 1;
14021 bounds = NULL_TREE;
14022 /* Look for the closing `]'. */
14023 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14025 declarator = cp_error_declarator;
14029 declarator = make_array_declarator (declarator, bounds);
14031 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14034 tree qualifying_scope;
14035 tree unqualified_name;
14036 special_function_kind sfk;
14038 bool pack_expansion_p = false;
14039 cp_token *declarator_id_start_token;
14041 /* Parse a declarator-id */
14042 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14045 cp_parser_parse_tentatively (parser);
14047 /* If we see an ellipsis, we should be looking at a
14049 if (token->type == CPP_ELLIPSIS)
14051 /* Consume the `...' */
14052 cp_lexer_consume_token (parser->lexer);
14054 pack_expansion_p = true;
14058 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14060 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14061 qualifying_scope = parser->scope;
14066 if (!unqualified_name && pack_expansion_p)
14068 /* Check whether an error occurred. */
14069 okay = !cp_parser_error_occurred (parser);
14071 /* We already consumed the ellipsis to mark a
14072 parameter pack, but we have no way to report it,
14073 so abort the tentative parse. We will be exiting
14074 immediately anyway. */
14075 cp_parser_abort_tentative_parse (parser);
14078 okay = cp_parser_parse_definitely (parser);
14081 unqualified_name = error_mark_node;
14082 else if (unqualified_name
14083 && (qualifying_scope
14084 || (TREE_CODE (unqualified_name)
14085 != IDENTIFIER_NODE)))
14087 cp_parser_error (parser, "expected unqualified-id");
14088 unqualified_name = error_mark_node;
14092 if (!unqualified_name)
14094 if (unqualified_name == error_mark_node)
14096 declarator = cp_error_declarator;
14097 pack_expansion_p = false;
14098 declarator->parameter_pack_p = false;
14102 if (qualifying_scope && at_namespace_scope_p ()
14103 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14105 /* In the declaration of a member of a template class
14106 outside of the class itself, the SCOPE will sometimes
14107 be a TYPENAME_TYPE. For example, given:
14109 template <typename T>
14110 int S<T>::R::i = 3;
14112 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
14113 this context, we must resolve S<T>::R to an ordinary
14114 type, rather than a typename type.
14116 The reason we normally avoid resolving TYPENAME_TYPEs
14117 is that a specialization of `S' might render
14118 `S<T>::R' not a type. However, if `S' is
14119 specialized, then this `i' will not be used, so there
14120 is no harm in resolving the types here. */
14123 /* Resolve the TYPENAME_TYPE. */
14124 type = resolve_typename_type (qualifying_scope,
14125 /*only_current_p=*/false);
14126 /* If that failed, the declarator is invalid. */
14127 if (TREE_CODE (type) == TYPENAME_TYPE)
14128 error_at (declarator_id_start_token->location,
14129 "%<%T::%E%> is not a type",
14130 TYPE_CONTEXT (qualifying_scope),
14131 TYPE_IDENTIFIER (qualifying_scope));
14132 qualifying_scope = type;
14137 if (unqualified_name)
14141 if (qualifying_scope
14142 && CLASS_TYPE_P (qualifying_scope))
14143 class_type = qualifying_scope;
14145 class_type = current_class_type;
14147 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14149 tree name_type = TREE_TYPE (unqualified_name);
14150 if (class_type && same_type_p (name_type, class_type))
14152 if (qualifying_scope
14153 && CLASSTYPE_USE_TEMPLATE (name_type))
14155 error_at (declarator_id_start_token->location,
14156 "invalid use of constructor as a template");
14157 inform (declarator_id_start_token->location,
14158 "use %<%T::%D%> instead of %<%T::%D%> to "
14159 "name the constructor in a qualified name",
14161 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14162 class_type, name_type);
14163 declarator = cp_error_declarator;
14167 unqualified_name = constructor_name (class_type);
14171 /* We do not attempt to print the declarator
14172 here because we do not have enough
14173 information about its original syntactic
14175 cp_parser_error (parser, "invalid declarator");
14176 declarator = cp_error_declarator;
14183 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14184 sfk = sfk_destructor;
14185 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14186 sfk = sfk_conversion;
14187 else if (/* There's no way to declare a constructor
14188 for an anonymous type, even if the type
14189 got a name for linkage purposes. */
14190 !TYPE_WAS_ANONYMOUS (class_type)
14191 && constructor_name_p (unqualified_name,
14194 unqualified_name = constructor_name (class_type);
14195 sfk = sfk_constructor;
14198 if (ctor_dtor_or_conv_p && sfk != sfk_none)
14199 *ctor_dtor_or_conv_p = -1;
14202 declarator = make_id_declarator (qualifying_scope,
14205 declarator->id_loc = token->location;
14206 declarator->parameter_pack_p = pack_expansion_p;
14208 if (pack_expansion_p)
14209 maybe_warn_variadic_templates ();
14212 handle_declarator:;
14213 scope = get_scope_of_declarator (declarator);
14215 /* Any names that appear after the declarator-id for a
14216 member are looked up in the containing scope. */
14217 pushed_scope = push_scope (scope);
14218 parser->in_declarator_p = true;
14219 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14220 || (declarator && declarator->kind == cdk_id))
14221 /* Default args are only allowed on function
14223 parser->default_arg_ok_p = saved_default_arg_ok_p;
14225 parser->default_arg_ok_p = false;
14234 /* For an abstract declarator, we might wind up with nothing at this
14235 point. That's an error; the declarator is not optional. */
14237 cp_parser_error (parser, "expected declarator");
14239 /* If we entered a scope, we must exit it now. */
14241 pop_scope (pushed_scope);
14243 parser->default_arg_ok_p = saved_default_arg_ok_p;
14244 parser->in_declarator_p = saved_in_declarator_p;
14249 /* Parse a ptr-operator.
14252 * cv-qualifier-seq [opt]
14254 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14259 & cv-qualifier-seq [opt]
14261 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14262 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14263 an rvalue reference. In the case of a pointer-to-member, *TYPE is
14264 filled in with the TYPE containing the member. *CV_QUALS is
14265 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14266 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
14267 Note that the tree codes returned by this function have nothing
14268 to do with the types of trees that will be eventually be created
14269 to represent the pointer or reference type being parsed. They are
14270 just constants with suggestive names. */
14271 static enum tree_code
14272 cp_parser_ptr_operator (cp_parser* parser,
14274 cp_cv_quals *cv_quals)
14276 enum tree_code code = ERROR_MARK;
14279 /* Assume that it's not a pointer-to-member. */
14281 /* And that there are no cv-qualifiers. */
14282 *cv_quals = TYPE_UNQUALIFIED;
14284 /* Peek at the next token. */
14285 token = cp_lexer_peek_token (parser->lexer);
14287 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
14288 if (token->type == CPP_MULT)
14289 code = INDIRECT_REF;
14290 else if (token->type == CPP_AND)
14292 else if ((cxx_dialect != cxx98) &&
14293 token->type == CPP_AND_AND) /* C++0x only */
14294 code = NON_LVALUE_EXPR;
14296 if (code != ERROR_MARK)
14298 /* Consume the `*', `&' or `&&'. */
14299 cp_lexer_consume_token (parser->lexer);
14301 /* A `*' can be followed by a cv-qualifier-seq, and so can a
14302 `&', if we are allowing GNU extensions. (The only qualifier
14303 that can legally appear after `&' is `restrict', but that is
14304 enforced during semantic analysis. */
14305 if (code == INDIRECT_REF
14306 || cp_parser_allow_gnu_extensions_p (parser))
14307 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14311 /* Try the pointer-to-member case. */
14312 cp_parser_parse_tentatively (parser);
14313 /* Look for the optional `::' operator. */
14314 cp_parser_global_scope_opt (parser,
14315 /*current_scope_valid_p=*/false);
14316 /* Look for the nested-name specifier. */
14317 token = cp_lexer_peek_token (parser->lexer);
14318 cp_parser_nested_name_specifier (parser,
14319 /*typename_keyword_p=*/false,
14320 /*check_dependency_p=*/true,
14322 /*is_declaration=*/false);
14323 /* If we found it, and the next token is a `*', then we are
14324 indeed looking at a pointer-to-member operator. */
14325 if (!cp_parser_error_occurred (parser)
14326 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14328 /* Indicate that the `*' operator was used. */
14329 code = INDIRECT_REF;
14331 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14332 error_at (token->location, "%qD is a namespace", parser->scope);
14335 /* The type of which the member is a member is given by the
14337 *type = parser->scope;
14338 /* The next name will not be qualified. */
14339 parser->scope = NULL_TREE;
14340 parser->qualifying_scope = NULL_TREE;
14341 parser->object_scope = NULL_TREE;
14342 /* Look for the optional cv-qualifier-seq. */
14343 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14346 /* If that didn't work we don't have a ptr-operator. */
14347 if (!cp_parser_parse_definitely (parser))
14348 cp_parser_error (parser, "expected ptr-operator");
14354 /* Parse an (optional) cv-qualifier-seq.
14357 cv-qualifier cv-qualifier-seq [opt]
14368 Returns a bitmask representing the cv-qualifiers. */
14371 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14373 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14378 cp_cv_quals cv_qualifier;
14380 /* Peek at the next token. */
14381 token = cp_lexer_peek_token (parser->lexer);
14382 /* See if it's a cv-qualifier. */
14383 switch (token->keyword)
14386 cv_qualifier = TYPE_QUAL_CONST;
14390 cv_qualifier = TYPE_QUAL_VOLATILE;
14394 cv_qualifier = TYPE_QUAL_RESTRICT;
14398 cv_qualifier = TYPE_UNQUALIFIED;
14405 if (cv_quals & cv_qualifier)
14407 error_at (token->location, "duplicate cv-qualifier");
14408 cp_lexer_purge_token (parser->lexer);
14412 cp_lexer_consume_token (parser->lexer);
14413 cv_quals |= cv_qualifier;
14420 /* Parse a late-specified return type, if any. This is not a separate
14421 non-terminal, but part of a function declarator, which looks like
14425 Returns the type indicated by the type-id. */
14428 cp_parser_late_return_type_opt (cp_parser* parser)
14432 /* Peek at the next token. */
14433 token = cp_lexer_peek_token (parser->lexer);
14434 /* A late-specified return type is indicated by an initial '->'. */
14435 if (token->type != CPP_DEREF)
14438 /* Consume the ->. */
14439 cp_lexer_consume_token (parser->lexer);
14441 return cp_parser_type_id (parser);
14444 /* Parse a declarator-id.
14448 :: [opt] nested-name-specifier [opt] type-name
14450 In the `id-expression' case, the value returned is as for
14451 cp_parser_id_expression if the id-expression was an unqualified-id.
14452 If the id-expression was a qualified-id, then a SCOPE_REF is
14453 returned. The first operand is the scope (either a NAMESPACE_DECL
14454 or TREE_TYPE), but the second is still just a representation of an
14458 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14461 /* The expression must be an id-expression. Assume that qualified
14462 names are the names of types so that:
14465 int S<T>::R::i = 3;
14467 will work; we must treat `S<T>::R' as the name of a type.
14468 Similarly, assume that qualified names are templates, where
14472 int S<T>::R<T>::i = 3;
14475 id = cp_parser_id_expression (parser,
14476 /*template_keyword_p=*/false,
14477 /*check_dependency_p=*/false,
14478 /*template_p=*/NULL,
14479 /*declarator_p=*/true,
14481 if (id && BASELINK_P (id))
14482 id = BASELINK_FUNCTIONS (id);
14486 /* Parse a type-id.
14489 type-specifier-seq abstract-declarator [opt]
14491 Returns the TYPE specified. */
14494 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
14496 cp_decl_specifier_seq type_specifier_seq;
14497 cp_declarator *abstract_declarator;
14499 /* Parse the type-specifier-seq. */
14500 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14501 &type_specifier_seq);
14502 if (type_specifier_seq.type == error_mark_node)
14503 return error_mark_node;
14505 /* There might or might not be an abstract declarator. */
14506 cp_parser_parse_tentatively (parser);
14507 /* Look for the declarator. */
14508 abstract_declarator
14509 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14510 /*parenthesized_p=*/NULL,
14511 /*member_p=*/false);
14512 /* Check to see if there really was a declarator. */
14513 if (!cp_parser_parse_definitely (parser))
14514 abstract_declarator = NULL;
14516 if (type_specifier_seq.type
14517 && type_uses_auto (type_specifier_seq.type))
14519 /* A type-id with type 'auto' is only ok if the abstract declarator
14520 is a function declarator with a late-specified return type. */
14521 if (abstract_declarator
14522 && abstract_declarator->kind == cdk_function
14523 && abstract_declarator->u.function.late_return_type)
14527 error ("invalid use of %<auto%>");
14528 return error_mark_node;
14532 return groktypename (&type_specifier_seq, abstract_declarator,
14536 static tree cp_parser_type_id (cp_parser *parser)
14538 return cp_parser_type_id_1 (parser, false);
14541 static tree cp_parser_template_type_arg (cp_parser *parser)
14543 return cp_parser_type_id_1 (parser, true);
14546 /* Parse a type-specifier-seq.
14548 type-specifier-seq:
14549 type-specifier type-specifier-seq [opt]
14553 type-specifier-seq:
14554 attributes type-specifier-seq [opt]
14556 If IS_CONDITION is true, we are at the start of a "condition",
14557 e.g., we've just seen "if (".
14559 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
14562 cp_parser_type_specifier_seq (cp_parser* parser,
14564 cp_decl_specifier_seq *type_specifier_seq)
14566 bool seen_type_specifier = false;
14567 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14568 cp_token *start_token = NULL;
14570 /* Clear the TYPE_SPECIFIER_SEQ. */
14571 clear_decl_specs (type_specifier_seq);
14573 /* Parse the type-specifiers and attributes. */
14576 tree type_specifier;
14577 bool is_cv_qualifier;
14579 /* Check for attributes first. */
14580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14582 type_specifier_seq->attributes =
14583 chainon (type_specifier_seq->attributes,
14584 cp_parser_attributes_opt (parser));
14588 /* record the token of the beginning of the type specifier seq,
14589 for error reporting purposes*/
14591 start_token = cp_lexer_peek_token (parser->lexer);
14593 /* Look for the type-specifier. */
14594 type_specifier = cp_parser_type_specifier (parser,
14596 type_specifier_seq,
14597 /*is_declaration=*/false,
14600 if (!type_specifier)
14602 /* If the first type-specifier could not be found, this is not a
14603 type-specifier-seq at all. */
14604 if (!seen_type_specifier)
14606 cp_parser_error (parser, "expected type-specifier");
14607 type_specifier_seq->type = error_mark_node;
14610 /* If subsequent type-specifiers could not be found, the
14611 type-specifier-seq is complete. */
14615 seen_type_specifier = true;
14616 /* The standard says that a condition can be:
14618 type-specifier-seq declarator = assignment-expression
14625 we should treat the "S" as a declarator, not as a
14626 type-specifier. The standard doesn't say that explicitly for
14627 type-specifier-seq, but it does say that for
14628 decl-specifier-seq in an ordinary declaration. Perhaps it
14629 would be clearer just to allow a decl-specifier-seq here, and
14630 then add a semantic restriction that if any decl-specifiers
14631 that are not type-specifiers appear, the program is invalid. */
14632 if (is_condition && !is_cv_qualifier)
14633 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14636 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14639 /* Parse a parameter-declaration-clause.
14641 parameter-declaration-clause:
14642 parameter-declaration-list [opt] ... [opt]
14643 parameter-declaration-list , ...
14645 Returns a representation for the parameter declarations. A return
14646 value of NULL indicates a parameter-declaration-clause consisting
14647 only of an ellipsis. */
14650 cp_parser_parameter_declaration_clause (cp_parser* parser)
14657 /* Peek at the next token. */
14658 token = cp_lexer_peek_token (parser->lexer);
14659 /* Check for trivial parameter-declaration-clauses. */
14660 if (token->type == CPP_ELLIPSIS)
14662 /* Consume the `...' token. */
14663 cp_lexer_consume_token (parser->lexer);
14666 else if (token->type == CPP_CLOSE_PAREN)
14667 /* There are no parameters. */
14669 #ifndef NO_IMPLICIT_EXTERN_C
14670 if (in_system_header && current_class_type == NULL
14671 && current_lang_name == lang_name_c)
14675 return void_list_node;
14677 /* Check for `(void)', too, which is a special case. */
14678 else if (token->keyword == RID_VOID
14679 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14680 == CPP_CLOSE_PAREN))
14682 /* Consume the `void' token. */
14683 cp_lexer_consume_token (parser->lexer);
14684 /* There are no parameters. */
14685 return void_list_node;
14688 /* Parse the parameter-declaration-list. */
14689 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14690 /* If a parse error occurred while parsing the
14691 parameter-declaration-list, then the entire
14692 parameter-declaration-clause is erroneous. */
14696 /* Peek at the next token. */
14697 token = cp_lexer_peek_token (parser->lexer);
14698 /* If it's a `,', the clause should terminate with an ellipsis. */
14699 if (token->type == CPP_COMMA)
14701 /* Consume the `,'. */
14702 cp_lexer_consume_token (parser->lexer);
14703 /* Expect an ellipsis. */
14705 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14707 /* It might also be `...' if the optional trailing `,' was
14709 else if (token->type == CPP_ELLIPSIS)
14711 /* Consume the `...' token. */
14712 cp_lexer_consume_token (parser->lexer);
14713 /* And remember that we saw it. */
14717 ellipsis_p = false;
14719 /* Finish the parameter list. */
14721 parameters = chainon (parameters, void_list_node);
14726 /* Parse a parameter-declaration-list.
14728 parameter-declaration-list:
14729 parameter-declaration
14730 parameter-declaration-list , parameter-declaration
14732 Returns a representation of the parameter-declaration-list, as for
14733 cp_parser_parameter_declaration_clause. However, the
14734 `void_list_node' is never appended to the list. Upon return,
14735 *IS_ERROR will be true iff an error occurred. */
14738 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14740 tree parameters = NULL_TREE;
14741 tree *tail = ¶meters;
14742 bool saved_in_unbraced_linkage_specification_p;
14745 /* Assume all will go well. */
14747 /* The special considerations that apply to a function within an
14748 unbraced linkage specifications do not apply to the parameters
14749 to the function. */
14750 saved_in_unbraced_linkage_specification_p
14751 = parser->in_unbraced_linkage_specification_p;
14752 parser->in_unbraced_linkage_specification_p = false;
14754 /* Look for more parameters. */
14757 cp_parameter_declarator *parameter;
14758 tree decl = error_mark_node;
14759 bool parenthesized_p;
14760 /* Parse the parameter. */
14762 = cp_parser_parameter_declaration (parser,
14763 /*template_parm_p=*/false,
14766 /* We don't know yet if the enclosing context is deprecated, so wait
14767 and warn in grokparms if appropriate. */
14768 deprecated_state = DEPRECATED_SUPPRESS;
14771 decl = grokdeclarator (parameter->declarator,
14772 ¶meter->decl_specifiers,
14774 parameter->default_argument != NULL_TREE,
14775 ¶meter->decl_specifiers.attributes);
14777 deprecated_state = DEPRECATED_NORMAL;
14779 /* If a parse error occurred parsing the parameter declaration,
14780 then the entire parameter-declaration-list is erroneous. */
14781 if (decl == error_mark_node)
14784 parameters = error_mark_node;
14788 if (parameter->decl_specifiers.attributes)
14789 cplus_decl_attributes (&decl,
14790 parameter->decl_specifiers.attributes,
14792 if (DECL_NAME (decl))
14793 decl = pushdecl (decl);
14795 if (decl != error_mark_node)
14797 retrofit_lang_decl (decl);
14798 DECL_PARM_INDEX (decl) = ++index;
14801 /* Add the new parameter to the list. */
14802 *tail = build_tree_list (parameter->default_argument, decl);
14803 tail = &TREE_CHAIN (*tail);
14805 /* Peek at the next token. */
14806 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14807 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14808 /* These are for Objective-C++ */
14809 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14810 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14811 /* The parameter-declaration-list is complete. */
14813 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14817 /* Peek at the next token. */
14818 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14819 /* If it's an ellipsis, then the list is complete. */
14820 if (token->type == CPP_ELLIPSIS)
14822 /* Otherwise, there must be more parameters. Consume the
14824 cp_lexer_consume_token (parser->lexer);
14825 /* When parsing something like:
14827 int i(float f, double d)
14829 we can tell after seeing the declaration for "f" that we
14830 are not looking at an initialization of a variable "i",
14831 but rather at the declaration of a function "i".
14833 Due to the fact that the parsing of template arguments
14834 (as specified to a template-id) requires backtracking we
14835 cannot use this technique when inside a template argument
14837 if (!parser->in_template_argument_list_p
14838 && !parser->in_type_id_in_expr_p
14839 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14840 /* However, a parameter-declaration of the form
14841 "foat(f)" (which is a valid declaration of a
14842 parameter "f") can also be interpreted as an
14843 expression (the conversion of "f" to "float"). */
14844 && !parenthesized_p)
14845 cp_parser_commit_to_tentative_parse (parser);
14849 cp_parser_error (parser, "expected %<,%> or %<...%>");
14850 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14851 cp_parser_skip_to_closing_parenthesis (parser,
14852 /*recovering=*/true,
14853 /*or_comma=*/false,
14854 /*consume_paren=*/false);
14859 parser->in_unbraced_linkage_specification_p
14860 = saved_in_unbraced_linkage_specification_p;
14865 /* Parse a parameter declaration.
14867 parameter-declaration:
14868 decl-specifier-seq ... [opt] declarator
14869 decl-specifier-seq declarator = assignment-expression
14870 decl-specifier-seq ... [opt] abstract-declarator [opt]
14871 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14873 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14874 declares a template parameter. (In that case, a non-nested `>'
14875 token encountered during the parsing of the assignment-expression
14876 is not interpreted as a greater-than operator.)
14878 Returns a representation of the parameter, or NULL if an error
14879 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14880 true iff the declarator is of the form "(p)". */
14882 static cp_parameter_declarator *
14883 cp_parser_parameter_declaration (cp_parser *parser,
14884 bool template_parm_p,
14885 bool *parenthesized_p)
14887 int declares_class_or_enum;
14888 bool greater_than_is_operator_p;
14889 cp_decl_specifier_seq decl_specifiers;
14890 cp_declarator *declarator;
14891 tree default_argument;
14892 cp_token *token = NULL, *declarator_token_start = NULL;
14893 const char *saved_message;
14895 /* In a template parameter, `>' is not an operator.
14899 When parsing a default template-argument for a non-type
14900 template-parameter, the first non-nested `>' is taken as the end
14901 of the template parameter-list rather than a greater-than
14903 greater_than_is_operator_p = !template_parm_p;
14905 /* Type definitions may not appear in parameter types. */
14906 saved_message = parser->type_definition_forbidden_message;
14907 parser->type_definition_forbidden_message
14908 = "types may not be defined in parameter types";
14910 /* Parse the declaration-specifiers. */
14911 cp_parser_decl_specifier_seq (parser,
14912 CP_PARSER_FLAGS_NONE,
14914 &declares_class_or_enum);
14915 /* If an error occurred, there's no reason to attempt to parse the
14916 rest of the declaration. */
14917 if (cp_parser_error_occurred (parser))
14919 parser->type_definition_forbidden_message = saved_message;
14923 /* Peek at the next token. */
14924 token = cp_lexer_peek_token (parser->lexer);
14926 /* If the next token is a `)', `,', `=', `>', or `...', then there
14927 is no declarator. However, when variadic templates are enabled,
14928 there may be a declarator following `...'. */
14929 if (token->type == CPP_CLOSE_PAREN
14930 || token->type == CPP_COMMA
14931 || token->type == CPP_EQ
14932 || token->type == CPP_GREATER)
14935 if (parenthesized_p)
14936 *parenthesized_p = false;
14938 /* Otherwise, there should be a declarator. */
14941 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14942 parser->default_arg_ok_p = false;
14944 /* After seeing a decl-specifier-seq, if the next token is not a
14945 "(", there is no possibility that the code is a valid
14946 expression. Therefore, if parsing tentatively, we commit at
14948 if (!parser->in_template_argument_list_p
14949 /* In an expression context, having seen:
14953 we cannot be sure whether we are looking at a
14954 function-type (taking a "char" as a parameter) or a cast
14955 of some object of type "char" to "int". */
14956 && !parser->in_type_id_in_expr_p
14957 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14958 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14959 cp_parser_commit_to_tentative_parse (parser);
14960 /* Parse the declarator. */
14961 declarator_token_start = token;
14962 declarator = cp_parser_declarator (parser,
14963 CP_PARSER_DECLARATOR_EITHER,
14964 /*ctor_dtor_or_conv_p=*/NULL,
14966 /*member_p=*/false);
14967 parser->default_arg_ok_p = saved_default_arg_ok_p;
14968 /* After the declarator, allow more attributes. */
14969 decl_specifiers.attributes
14970 = chainon (decl_specifiers.attributes,
14971 cp_parser_attributes_opt (parser));
14974 /* If the next token is an ellipsis, and we have not seen a
14975 declarator name, and the type of the declarator contains parameter
14976 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14977 a parameter pack expansion expression. Otherwise, leave the
14978 ellipsis for a C-style variadic function. */
14979 token = cp_lexer_peek_token (parser->lexer);
14980 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14982 tree type = decl_specifiers.type;
14984 if (type && DECL_P (type))
14985 type = TREE_TYPE (type);
14988 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14989 && declarator_can_be_parameter_pack (declarator)
14990 && (!declarator || !declarator->parameter_pack_p)
14991 && uses_parameter_packs (type))
14993 /* Consume the `...'. */
14994 cp_lexer_consume_token (parser->lexer);
14995 maybe_warn_variadic_templates ();
14997 /* Build a pack expansion type */
14999 declarator->parameter_pack_p = true;
15001 decl_specifiers.type = make_pack_expansion (type);
15005 /* The restriction on defining new types applies only to the type
15006 of the parameter, not to the default argument. */
15007 parser->type_definition_forbidden_message = saved_message;
15009 /* If the next token is `=', then process a default argument. */
15010 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15012 /* Consume the `='. */
15013 cp_lexer_consume_token (parser->lexer);
15015 /* If we are defining a class, then the tokens that make up the
15016 default argument must be saved and processed later. */
15017 if (!template_parm_p && at_class_scope_p ()
15018 && TYPE_BEING_DEFINED (current_class_type)
15019 && !LAMBDA_TYPE_P (current_class_type))
15021 unsigned depth = 0;
15022 int maybe_template_id = 0;
15023 cp_token *first_token;
15026 /* Add tokens until we have processed the entire default
15027 argument. We add the range [first_token, token). */
15028 first_token = cp_lexer_peek_token (parser->lexer);
15033 /* Peek at the next token. */
15034 token = cp_lexer_peek_token (parser->lexer);
15035 /* What we do depends on what token we have. */
15036 switch (token->type)
15038 /* In valid code, a default argument must be
15039 immediately followed by a `,' `)', or `...'. */
15041 if (depth == 0 && maybe_template_id)
15043 /* If we've seen a '<', we might be in a
15044 template-argument-list. Until Core issue 325 is
15045 resolved, we don't know how this situation ought
15046 to be handled, so try to DTRT. We check whether
15047 what comes after the comma is a valid parameter
15048 declaration list. If it is, then the comma ends
15049 the default argument; otherwise the default
15050 argument continues. */
15051 bool error = false;
15053 /* Set ITALP so cp_parser_parameter_declaration_list
15054 doesn't decide to commit to this parse. */
15055 bool saved_italp = parser->in_template_argument_list_p;
15056 parser->in_template_argument_list_p = true;
15058 cp_parser_parse_tentatively (parser);
15059 cp_lexer_consume_token (parser->lexer);
15060 cp_parser_parameter_declaration_list (parser, &error);
15061 if (!cp_parser_error_occurred (parser) && !error)
15063 cp_parser_abort_tentative_parse (parser);
15065 parser->in_template_argument_list_p = saved_italp;
15068 case CPP_CLOSE_PAREN:
15070 /* If we run into a non-nested `;', `}', or `]',
15071 then the code is invalid -- but the default
15072 argument is certainly over. */
15073 case CPP_SEMICOLON:
15074 case CPP_CLOSE_BRACE:
15075 case CPP_CLOSE_SQUARE:
15078 /* Update DEPTH, if necessary. */
15079 else if (token->type == CPP_CLOSE_PAREN
15080 || token->type == CPP_CLOSE_BRACE
15081 || token->type == CPP_CLOSE_SQUARE)
15085 case CPP_OPEN_PAREN:
15086 case CPP_OPEN_SQUARE:
15087 case CPP_OPEN_BRACE:
15093 /* This might be the comparison operator, or it might
15094 start a template argument list. */
15095 ++maybe_template_id;
15099 if (cxx_dialect == cxx98)
15101 /* Fall through for C++0x, which treats the `>>'
15102 operator like two `>' tokens in certain
15108 /* This might be an operator, or it might close a
15109 template argument list. But if a previous '<'
15110 started a template argument list, this will have
15111 closed it, so we can't be in one anymore. */
15112 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15113 if (maybe_template_id < 0)
15114 maybe_template_id = 0;
15118 /* If we run out of tokens, issue an error message. */
15120 case CPP_PRAGMA_EOL:
15121 error_at (token->location, "file ends in default argument");
15127 /* In these cases, we should look for template-ids.
15128 For example, if the default argument is
15129 `X<int, double>()', we need to do name lookup to
15130 figure out whether or not `X' is a template; if
15131 so, the `,' does not end the default argument.
15133 That is not yet done. */
15140 /* If we've reached the end, stop. */
15144 /* Add the token to the token block. */
15145 token = cp_lexer_consume_token (parser->lexer);
15148 /* Create a DEFAULT_ARG to represent the unparsed default
15150 default_argument = make_node (DEFAULT_ARG);
15151 DEFARG_TOKENS (default_argument)
15152 = cp_token_cache_new (first_token, token);
15153 DEFARG_INSTANTIATIONS (default_argument) = NULL;
15155 /* Outside of a class definition, we can just parse the
15156 assignment-expression. */
15159 token = cp_lexer_peek_token (parser->lexer);
15161 = cp_parser_default_argument (parser, template_parm_p);
15164 if (!parser->default_arg_ok_p)
15166 if (flag_permissive)
15167 warning (0, "deprecated use of default argument for parameter of non-function");
15170 error_at (token->location,
15171 "default arguments are only "
15172 "permitted for function parameters");
15173 default_argument = NULL_TREE;
15176 else if ((declarator && declarator->parameter_pack_p)
15177 || (decl_specifiers.type
15178 && PACK_EXPANSION_P (decl_specifiers.type)))
15180 /* Find the name of the parameter pack. */
15181 cp_declarator *id_declarator = declarator;
15182 while (id_declarator && id_declarator->kind != cdk_id)
15183 id_declarator = id_declarator->declarator;
15185 if (id_declarator && id_declarator->kind == cdk_id)
15186 error_at (declarator_token_start->location,
15188 ? "template parameter pack %qD"
15189 " cannot have a default argument"
15190 : "parameter pack %qD cannot have a default argument",
15191 id_declarator->u.id.unqualified_name);
15193 error_at (declarator_token_start->location,
15195 ? "template parameter pack cannot have a default argument"
15196 : "parameter pack cannot have a default argument");
15198 default_argument = NULL_TREE;
15202 default_argument = NULL_TREE;
15204 return make_parameter_declarator (&decl_specifiers,
15209 /* Parse a default argument and return it.
15211 TEMPLATE_PARM_P is true if this is a default argument for a
15212 non-type template parameter. */
15214 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15216 tree default_argument = NULL_TREE;
15217 bool saved_greater_than_is_operator_p;
15218 bool saved_local_variables_forbidden_p;
15220 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15222 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15223 parser->greater_than_is_operator_p = !template_parm_p;
15224 /* Local variable names (and the `this' keyword) may not
15225 appear in a default argument. */
15226 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15227 parser->local_variables_forbidden_p = true;
15228 /* Parse the assignment-expression. */
15229 if (template_parm_p)
15230 push_deferring_access_checks (dk_no_deferred);
15232 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15233 if (template_parm_p)
15234 pop_deferring_access_checks ();
15235 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15236 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15238 return default_argument;
15241 /* Parse a function-body.
15244 compound_statement */
15247 cp_parser_function_body (cp_parser *parser)
15249 cp_parser_compound_statement (parser, NULL, false);
15252 /* Parse a ctor-initializer-opt followed by a function-body. Return
15253 true if a ctor-initializer was present. */
15256 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15259 bool ctor_initializer_p;
15261 /* Begin the function body. */
15262 body = begin_function_body ();
15263 /* Parse the optional ctor-initializer. */
15264 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15265 /* Parse the function-body. */
15266 cp_parser_function_body (parser);
15267 /* Finish the function body. */
15268 finish_function_body (body);
15270 return ctor_initializer_p;
15273 /* Parse an initializer.
15276 = initializer-clause
15277 ( expression-list )
15279 Returns an expression representing the initializer. If no
15280 initializer is present, NULL_TREE is returned.
15282 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15283 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
15284 set to TRUE if there is no initializer present. If there is an
15285 initializer, and it is not a constant-expression, *NON_CONSTANT_P
15286 is set to true; otherwise it is set to false. */
15289 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15290 bool* non_constant_p)
15295 /* Peek at the next token. */
15296 token = cp_lexer_peek_token (parser->lexer);
15298 /* Let our caller know whether or not this initializer was
15300 *is_direct_init = (token->type != CPP_EQ);
15301 /* Assume that the initializer is constant. */
15302 *non_constant_p = false;
15304 if (token->type == CPP_EQ)
15306 /* Consume the `='. */
15307 cp_lexer_consume_token (parser->lexer);
15308 /* Parse the initializer-clause. */
15309 init = cp_parser_initializer_clause (parser, non_constant_p);
15311 else if (token->type == CPP_OPEN_PAREN)
15314 vec = cp_parser_parenthesized_expression_list (parser, false,
15316 /*allow_expansion_p=*/true,
15319 return error_mark_node;
15320 init = build_tree_list_vec (vec);
15321 release_tree_vector (vec);
15323 else if (token->type == CPP_OPEN_BRACE)
15325 maybe_warn_cpp0x ("extended initializer lists");
15326 init = cp_parser_braced_list (parser, non_constant_p);
15327 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15331 /* Anything else is an error. */
15332 cp_parser_error (parser, "expected initializer");
15333 init = error_mark_node;
15339 /* Parse an initializer-clause.
15341 initializer-clause:
15342 assignment-expression
15345 Returns an expression representing the initializer.
15347 If the `assignment-expression' production is used the value
15348 returned is simply a representation for the expression.
15350 Otherwise, calls cp_parser_braced_list. */
15353 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15357 /* Assume the expression is constant. */
15358 *non_constant_p = false;
15360 /* If it is not a `{', then we are looking at an
15361 assignment-expression. */
15362 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15365 = cp_parser_constant_expression (parser,
15366 /*allow_non_constant_p=*/true,
15368 if (!*non_constant_p)
15369 initializer = fold_non_dependent_expr (initializer);
15372 initializer = cp_parser_braced_list (parser, non_constant_p);
15374 return initializer;
15377 /* Parse a brace-enclosed initializer list.
15380 { initializer-list , [opt] }
15383 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
15384 the elements of the initializer-list (or NULL, if the last
15385 production is used). The TREE_TYPE for the CONSTRUCTOR will be
15386 NULL_TREE. There is no way to detect whether or not the optional
15387 trailing `,' was provided. NON_CONSTANT_P is as for
15388 cp_parser_initializer. */
15391 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15395 /* Consume the `{' token. */
15396 cp_lexer_consume_token (parser->lexer);
15397 /* Create a CONSTRUCTOR to represent the braced-initializer. */
15398 initializer = make_node (CONSTRUCTOR);
15399 /* If it's not a `}', then there is a non-trivial initializer. */
15400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15402 /* Parse the initializer list. */
15403 CONSTRUCTOR_ELTS (initializer)
15404 = cp_parser_initializer_list (parser, non_constant_p);
15405 /* A trailing `,' token is allowed. */
15406 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15407 cp_lexer_consume_token (parser->lexer);
15409 /* Now, there should be a trailing `}'. */
15410 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15411 TREE_TYPE (initializer) = init_list_type_node;
15412 return initializer;
15415 /* Parse an initializer-list.
15418 initializer-clause ... [opt]
15419 initializer-list , initializer-clause ... [opt]
15424 identifier : initializer-clause
15425 initializer-list, identifier : initializer-clause
15427 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
15428 for the initializer. If the INDEX of the elt is non-NULL, it is the
15429 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
15430 as for cp_parser_initializer. */
15432 static VEC(constructor_elt,gc) *
15433 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15435 VEC(constructor_elt,gc) *v = NULL;
15437 /* Assume all of the expressions are constant. */
15438 *non_constant_p = false;
15440 /* Parse the rest of the list. */
15446 bool clause_non_constant_p;
15448 /* If the next token is an identifier and the following one is a
15449 colon, we are looking at the GNU designated-initializer
15451 if (cp_parser_allow_gnu_extensions_p (parser)
15452 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15453 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15455 /* Warn the user that they are using an extension. */
15456 pedwarn (input_location, OPT_pedantic,
15457 "ISO C++ does not allow designated initializers");
15458 /* Consume the identifier. */
15459 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15460 /* Consume the `:'. */
15461 cp_lexer_consume_token (parser->lexer);
15464 identifier = NULL_TREE;
15466 /* Parse the initializer. */
15467 initializer = cp_parser_initializer_clause (parser,
15468 &clause_non_constant_p);
15469 /* If any clause is non-constant, so is the entire initializer. */
15470 if (clause_non_constant_p)
15471 *non_constant_p = true;
15473 /* If we have an ellipsis, this is an initializer pack
15475 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15477 /* Consume the `...'. */
15478 cp_lexer_consume_token (parser->lexer);
15480 /* Turn the initializer into an initializer expansion. */
15481 initializer = make_pack_expansion (initializer);
15484 /* Add it to the vector. */
15485 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15487 /* If the next token is not a comma, we have reached the end of
15489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15492 /* Peek at the next token. */
15493 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15494 /* If the next token is a `}', then we're still done. An
15495 initializer-clause can have a trailing `,' after the
15496 initializer-list and before the closing `}'. */
15497 if (token->type == CPP_CLOSE_BRACE)
15500 /* Consume the `,' token. */
15501 cp_lexer_consume_token (parser->lexer);
15507 /* Classes [gram.class] */
15509 /* Parse a class-name.
15515 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15516 to indicate that names looked up in dependent types should be
15517 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
15518 keyword has been used to indicate that the name that appears next
15519 is a template. TAG_TYPE indicates the explicit tag given before
15520 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
15521 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
15522 is the class being defined in a class-head.
15524 Returns the TYPE_DECL representing the class. */
15527 cp_parser_class_name (cp_parser *parser,
15528 bool typename_keyword_p,
15529 bool template_keyword_p,
15530 enum tag_types tag_type,
15531 bool check_dependency_p,
15533 bool is_declaration)
15539 tree identifier = NULL_TREE;
15541 /* All class-names start with an identifier. */
15542 token = cp_lexer_peek_token (parser->lexer);
15543 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15545 cp_parser_error (parser, "expected class-name");
15546 return error_mark_node;
15549 /* PARSER->SCOPE can be cleared when parsing the template-arguments
15550 to a template-id, so we save it here. */
15551 scope = parser->scope;
15552 if (scope == error_mark_node)
15553 return error_mark_node;
15555 /* Any name names a type if we're following the `typename' keyword
15556 in a qualified name where the enclosing scope is type-dependent. */
15557 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15558 && dependent_type_p (scope));
15559 /* Handle the common case (an identifier, but not a template-id)
15561 if (token->type == CPP_NAME
15562 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15564 cp_token *identifier_token;
15567 /* Look for the identifier. */
15568 identifier_token = cp_lexer_peek_token (parser->lexer);
15569 ambiguous_p = identifier_token->ambiguous_p;
15570 identifier = cp_parser_identifier (parser);
15571 /* If the next token isn't an identifier, we are certainly not
15572 looking at a class-name. */
15573 if (identifier == error_mark_node)
15574 decl = error_mark_node;
15575 /* If we know this is a type-name, there's no need to look it
15577 else if (typename_p)
15581 tree ambiguous_decls;
15582 /* If we already know that this lookup is ambiguous, then
15583 we've already issued an error message; there's no reason
15587 cp_parser_simulate_error (parser);
15588 return error_mark_node;
15590 /* If the next token is a `::', then the name must be a type
15593 [basic.lookup.qual]
15595 During the lookup for a name preceding the :: scope
15596 resolution operator, object, function, and enumerator
15597 names are ignored. */
15598 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15599 tag_type = typename_type;
15600 /* Look up the name. */
15601 decl = cp_parser_lookup_name (parser, identifier,
15603 /*is_template=*/false,
15604 /*is_namespace=*/false,
15605 check_dependency_p,
15607 identifier_token->location);
15608 if (ambiguous_decls)
15610 error_at (identifier_token->location,
15611 "reference to %qD is ambiguous", identifier);
15612 print_candidates (ambiguous_decls);
15613 if (cp_parser_parsing_tentatively (parser))
15615 identifier_token->ambiguous_p = true;
15616 cp_parser_simulate_error (parser);
15618 return error_mark_node;
15624 /* Try a template-id. */
15625 decl = cp_parser_template_id (parser, template_keyword_p,
15626 check_dependency_p,
15628 if (decl == error_mark_node)
15629 return error_mark_node;
15632 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15634 /* If this is a typename, create a TYPENAME_TYPE. */
15635 if (typename_p && decl != error_mark_node)
15637 decl = make_typename_type (scope, decl, typename_type,
15638 /*complain=*/tf_error);
15639 if (decl != error_mark_node)
15640 decl = TYPE_NAME (decl);
15643 /* Check to see that it is really the name of a class. */
15644 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15645 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15646 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15647 /* Situations like this:
15649 template <typename T> struct A {
15650 typename T::template X<int>::I i;
15653 are problematic. Is `T::template X<int>' a class-name? The
15654 standard does not seem to be definitive, but there is no other
15655 valid interpretation of the following `::'. Therefore, those
15656 names are considered class-names. */
15658 decl = make_typename_type (scope, decl, tag_type, tf_error);
15659 if (decl != error_mark_node)
15660 decl = TYPE_NAME (decl);
15662 else if (TREE_CODE (decl) != TYPE_DECL
15663 || TREE_TYPE (decl) == error_mark_node
15664 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15665 decl = error_mark_node;
15667 if (decl == error_mark_node)
15668 cp_parser_error (parser, "expected class-name");
15669 else if (identifier && !parser->scope)
15670 maybe_note_name_used_in_class (identifier, decl);
15675 /* Parse a class-specifier.
15678 class-head { member-specification [opt] }
15680 Returns the TREE_TYPE representing the class. */
15683 cp_parser_class_specifier (cp_parser* parser)
15686 tree attributes = NULL_TREE;
15687 bool nested_name_specifier_p;
15688 unsigned saved_num_template_parameter_lists;
15689 bool saved_in_function_body;
15690 bool saved_in_unbraced_linkage_specification_p;
15691 tree old_scope = NULL_TREE;
15692 tree scope = NULL_TREE;
15695 push_deferring_access_checks (dk_no_deferred);
15697 /* Parse the class-head. */
15698 type = cp_parser_class_head (parser,
15699 &nested_name_specifier_p,
15702 /* If the class-head was a semantic disaster, skip the entire body
15706 cp_parser_skip_to_end_of_block_or_statement (parser);
15707 pop_deferring_access_checks ();
15708 return error_mark_node;
15711 /* Look for the `{'. */
15712 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15714 pop_deferring_access_checks ();
15715 return error_mark_node;
15718 /* Process the base classes. If they're invalid, skip the
15719 entire class body. */
15720 if (!xref_basetypes (type, bases))
15722 /* Consuming the closing brace yields better error messages
15724 if (cp_parser_skip_to_closing_brace (parser))
15725 cp_lexer_consume_token (parser->lexer);
15726 pop_deferring_access_checks ();
15727 return error_mark_node;
15730 /* Issue an error message if type-definitions are forbidden here. */
15731 cp_parser_check_type_definition (parser);
15732 /* Remember that we are defining one more class. */
15733 ++parser->num_classes_being_defined;
15734 /* Inside the class, surrounding template-parameter-lists do not
15736 saved_num_template_parameter_lists
15737 = parser->num_template_parameter_lists;
15738 parser->num_template_parameter_lists = 0;
15739 /* We are not in a function body. */
15740 saved_in_function_body = parser->in_function_body;
15741 parser->in_function_body = false;
15742 /* We are not immediately inside an extern "lang" block. */
15743 saved_in_unbraced_linkage_specification_p
15744 = parser->in_unbraced_linkage_specification_p;
15745 parser->in_unbraced_linkage_specification_p = false;
15747 /* Start the class. */
15748 if (nested_name_specifier_p)
15750 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15751 old_scope = push_inner_scope (scope);
15753 type = begin_class_definition (type, attributes);
15755 if (type == error_mark_node)
15756 /* If the type is erroneous, skip the entire body of the class. */
15757 cp_parser_skip_to_closing_brace (parser);
15759 /* Parse the member-specification. */
15760 cp_parser_member_specification_opt (parser);
15762 /* Look for the trailing `}'. */
15763 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15764 /* Look for trailing attributes to apply to this class. */
15765 if (cp_parser_allow_gnu_extensions_p (parser))
15766 attributes = cp_parser_attributes_opt (parser);
15767 if (type != error_mark_node)
15768 type = finish_struct (type, attributes);
15769 if (nested_name_specifier_p)
15770 pop_inner_scope (old_scope, scope);
15771 /* If this class is not itself within the scope of another class,
15772 then we need to parse the bodies of all of the queued function
15773 definitions. Note that the queued functions defined in a class
15774 are not always processed immediately following the
15775 class-specifier for that class. Consider:
15778 struct B { void f() { sizeof (A); } };
15781 If `f' were processed before the processing of `A' were
15782 completed, there would be no way to compute the size of `A'.
15783 Note that the nesting we are interested in here is lexical --
15784 not the semantic nesting given by TYPE_CONTEXT. In particular,
15787 struct A { struct B; };
15788 struct A::B { void f() { } };
15790 there is no need to delay the parsing of `A::B::f'. */
15791 if (--parser->num_classes_being_defined == 0)
15795 tree class_type = NULL_TREE;
15796 tree pushed_scope = NULL_TREE;
15798 /* In a first pass, parse default arguments to the functions.
15799 Then, in a second pass, parse the bodies of the functions.
15800 This two-phased approach handles cases like:
15808 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15809 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15810 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15811 TREE_PURPOSE (parser->unparsed_functions_queues)
15812 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15814 fn = TREE_VALUE (queue_entry);
15815 /* If there are default arguments that have not yet been processed,
15816 take care of them now. */
15817 if (class_type != TREE_PURPOSE (queue_entry))
15820 pop_scope (pushed_scope);
15821 class_type = TREE_PURPOSE (queue_entry);
15822 pushed_scope = push_scope (class_type);
15824 /* Make sure that any template parameters are in scope. */
15825 maybe_begin_member_template_processing (fn);
15826 /* Parse the default argument expressions. */
15827 cp_parser_late_parsing_default_args (parser, fn);
15828 /* Remove any template parameters from the symbol table. */
15829 maybe_end_member_template_processing ();
15832 pop_scope (pushed_scope);
15833 /* Now parse the body of the functions. */
15834 for (TREE_VALUE (parser->unparsed_functions_queues)
15835 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15836 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15837 TREE_VALUE (parser->unparsed_functions_queues)
15838 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15840 /* Figure out which function we need to process. */
15841 fn = TREE_VALUE (queue_entry);
15842 /* Parse the function. */
15843 cp_parser_late_parsing_for_member (parser, fn);
15847 /* Put back any saved access checks. */
15848 pop_deferring_access_checks ();
15850 /* Restore saved state. */
15851 parser->in_function_body = saved_in_function_body;
15852 parser->num_template_parameter_lists
15853 = saved_num_template_parameter_lists;
15854 parser->in_unbraced_linkage_specification_p
15855 = saved_in_unbraced_linkage_specification_p;
15860 /* Parse a class-head.
15863 class-key identifier [opt] base-clause [opt]
15864 class-key nested-name-specifier identifier base-clause [opt]
15865 class-key nested-name-specifier [opt] template-id
15869 class-key attributes identifier [opt] base-clause [opt]
15870 class-key attributes nested-name-specifier identifier base-clause [opt]
15871 class-key attributes nested-name-specifier [opt] template-id
15874 Upon return BASES is initialized to the list of base classes (or
15875 NULL, if there are none) in the same form returned by
15876 cp_parser_base_clause.
15878 Returns the TYPE of the indicated class. Sets
15879 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15880 involving a nested-name-specifier was used, and FALSE otherwise.
15882 Returns error_mark_node if this is not a class-head.
15884 Returns NULL_TREE if the class-head is syntactically valid, but
15885 semantically invalid in a way that means we should skip the entire
15886 body of the class. */
15889 cp_parser_class_head (cp_parser* parser,
15890 bool* nested_name_specifier_p,
15891 tree *attributes_p,
15894 tree nested_name_specifier;
15895 enum tag_types class_key;
15896 tree id = NULL_TREE;
15897 tree type = NULL_TREE;
15899 bool template_id_p = false;
15900 bool qualified_p = false;
15901 bool invalid_nested_name_p = false;
15902 bool invalid_explicit_specialization_p = false;
15903 tree pushed_scope = NULL_TREE;
15904 unsigned num_templates;
15905 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15906 /* Assume no nested-name-specifier will be present. */
15907 *nested_name_specifier_p = false;
15908 /* Assume no template parameter lists will be used in defining the
15912 *bases = NULL_TREE;
15914 /* Look for the class-key. */
15915 class_key = cp_parser_class_key (parser);
15916 if (class_key == none_type)
15917 return error_mark_node;
15919 /* Parse the attributes. */
15920 attributes = cp_parser_attributes_opt (parser);
15922 /* If the next token is `::', that is invalid -- but sometimes
15923 people do try to write:
15927 Handle this gracefully by accepting the extra qualifier, and then
15928 issuing an error about it later if this really is a
15929 class-head. If it turns out just to be an elaborated type
15930 specifier, remain silent. */
15931 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15932 qualified_p = true;
15934 push_deferring_access_checks (dk_no_check);
15936 /* Determine the name of the class. Begin by looking for an
15937 optional nested-name-specifier. */
15938 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15939 nested_name_specifier
15940 = cp_parser_nested_name_specifier_opt (parser,
15941 /*typename_keyword_p=*/false,
15942 /*check_dependency_p=*/false,
15944 /*is_declaration=*/false);
15945 /* If there was a nested-name-specifier, then there *must* be an
15947 if (nested_name_specifier)
15949 type_start_token = cp_lexer_peek_token (parser->lexer);
15950 /* Although the grammar says `identifier', it really means
15951 `class-name' or `template-name'. You are only allowed to
15952 define a class that has already been declared with this
15955 The proposed resolution for Core Issue 180 says that wherever
15956 you see `class T::X' you should treat `X' as a type-name.
15958 It is OK to define an inaccessible class; for example:
15960 class A { class B; };
15963 We do not know if we will see a class-name, or a
15964 template-name. We look for a class-name first, in case the
15965 class-name is a template-id; if we looked for the
15966 template-name first we would stop after the template-name. */
15967 cp_parser_parse_tentatively (parser);
15968 type = cp_parser_class_name (parser,
15969 /*typename_keyword_p=*/false,
15970 /*template_keyword_p=*/false,
15972 /*check_dependency_p=*/false,
15973 /*class_head_p=*/true,
15974 /*is_declaration=*/false);
15975 /* If that didn't work, ignore the nested-name-specifier. */
15976 if (!cp_parser_parse_definitely (parser))
15978 invalid_nested_name_p = true;
15979 type_start_token = cp_lexer_peek_token (parser->lexer);
15980 id = cp_parser_identifier (parser);
15981 if (id == error_mark_node)
15984 /* If we could not find a corresponding TYPE, treat this
15985 declaration like an unqualified declaration. */
15986 if (type == error_mark_node)
15987 nested_name_specifier = NULL_TREE;
15988 /* Otherwise, count the number of templates used in TYPE and its
15989 containing scopes. */
15994 for (scope = TREE_TYPE (type);
15995 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15996 scope = (TYPE_P (scope)
15997 ? TYPE_CONTEXT (scope)
15998 : DECL_CONTEXT (scope)))
16000 && CLASS_TYPE_P (scope)
16001 && CLASSTYPE_TEMPLATE_INFO (scope)
16002 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16003 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16007 /* Otherwise, the identifier is optional. */
16010 /* We don't know whether what comes next is a template-id,
16011 an identifier, or nothing at all. */
16012 cp_parser_parse_tentatively (parser);
16013 /* Check for a template-id. */
16014 type_start_token = cp_lexer_peek_token (parser->lexer);
16015 id = cp_parser_template_id (parser,
16016 /*template_keyword_p=*/false,
16017 /*check_dependency_p=*/true,
16018 /*is_declaration=*/true);
16019 /* If that didn't work, it could still be an identifier. */
16020 if (!cp_parser_parse_definitely (parser))
16022 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16024 type_start_token = cp_lexer_peek_token (parser->lexer);
16025 id = cp_parser_identifier (parser);
16032 template_id_p = true;
16037 pop_deferring_access_checks ();
16040 cp_parser_check_for_invalid_template_id (parser, id,
16041 type_start_token->location);
16043 /* If it's not a `:' or a `{' then we can't really be looking at a
16044 class-head, since a class-head only appears as part of a
16045 class-specifier. We have to detect this situation before calling
16046 xref_tag, since that has irreversible side-effects. */
16047 if (!cp_parser_next_token_starts_class_definition_p (parser))
16049 cp_parser_error (parser, "expected %<{%> or %<:%>");
16050 return error_mark_node;
16053 /* At this point, we're going ahead with the class-specifier, even
16054 if some other problem occurs. */
16055 cp_parser_commit_to_tentative_parse (parser);
16056 /* Issue the error about the overly-qualified name now. */
16059 cp_parser_error (parser,
16060 "global qualification of class name is invalid");
16061 return error_mark_node;
16063 else if (invalid_nested_name_p)
16065 cp_parser_error (parser,
16066 "qualified name does not name a class");
16067 return error_mark_node;
16069 else if (nested_name_specifier)
16073 /* Reject typedef-names in class heads. */
16074 if (!DECL_IMPLICIT_TYPEDEF_P (type))
16076 error_at (type_start_token->location,
16077 "invalid class name in declaration of %qD",
16083 /* Figure out in what scope the declaration is being placed. */
16084 scope = current_scope ();
16085 /* If that scope does not contain the scope in which the
16086 class was originally declared, the program is invalid. */
16087 if (scope && !is_ancestor (scope, nested_name_specifier))
16089 if (at_namespace_scope_p ())
16090 error_at (type_start_token->location,
16091 "declaration of %qD in namespace %qD which does not "
16093 type, scope, nested_name_specifier);
16095 error_at (type_start_token->location,
16096 "declaration of %qD in %qD which does not enclose %qD",
16097 type, scope, nested_name_specifier);
16103 A declarator-id shall not be qualified except for the
16104 definition of a ... nested class outside of its class
16105 ... [or] the definition or explicit instantiation of a
16106 class member of a namespace outside of its namespace. */
16107 if (scope == nested_name_specifier)
16109 permerror (nested_name_specifier_token_start->location,
16110 "extra qualification not allowed");
16111 nested_name_specifier = NULL_TREE;
16115 /* An explicit-specialization must be preceded by "template <>". If
16116 it is not, try to recover gracefully. */
16117 if (at_namespace_scope_p ()
16118 && parser->num_template_parameter_lists == 0
16121 error_at (type_start_token->location,
16122 "an explicit specialization must be preceded by %<template <>%>");
16123 invalid_explicit_specialization_p = true;
16124 /* Take the same action that would have been taken by
16125 cp_parser_explicit_specialization. */
16126 ++parser->num_template_parameter_lists;
16127 begin_specialization ();
16129 /* There must be no "return" statements between this point and the
16130 end of this function; set "type "to the correct return value and
16131 use "goto done;" to return. */
16132 /* Make sure that the right number of template parameters were
16134 if (!cp_parser_check_template_parameters (parser, num_templates,
16135 type_start_token->location,
16136 /*declarator=*/NULL))
16138 /* If something went wrong, there is no point in even trying to
16139 process the class-definition. */
16144 /* Look up the type. */
16147 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16148 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16149 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16151 error_at (type_start_token->location,
16152 "function template %qD redeclared as a class template", id);
16153 type = error_mark_node;
16157 type = TREE_TYPE (id);
16158 type = maybe_process_partial_specialization (type);
16160 if (nested_name_specifier)
16161 pushed_scope = push_scope (nested_name_specifier);
16163 else if (nested_name_specifier)
16169 template <typename T> struct S { struct T };
16170 template <typename T> struct S<T>::T { };
16172 we will get a TYPENAME_TYPE when processing the definition of
16173 `S::T'. We need to resolve it to the actual type before we
16174 try to define it. */
16175 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16177 class_type = resolve_typename_type (TREE_TYPE (type),
16178 /*only_current_p=*/false);
16179 if (TREE_CODE (class_type) != TYPENAME_TYPE)
16180 type = TYPE_NAME (class_type);
16183 cp_parser_error (parser, "could not resolve typename type");
16184 type = error_mark_node;
16188 if (maybe_process_partial_specialization (TREE_TYPE (type))
16189 == error_mark_node)
16195 class_type = current_class_type;
16196 /* Enter the scope indicated by the nested-name-specifier. */
16197 pushed_scope = push_scope (nested_name_specifier);
16198 /* Get the canonical version of this type. */
16199 type = TYPE_MAIN_DECL (TREE_TYPE (type));
16200 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16201 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16203 type = push_template_decl (type);
16204 if (type == error_mark_node)
16211 type = TREE_TYPE (type);
16212 *nested_name_specifier_p = true;
16214 else /* The name is not a nested name. */
16216 /* If the class was unnamed, create a dummy name. */
16218 id = make_anon_name ();
16219 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16220 parser->num_template_parameter_lists);
16223 /* Indicate whether this class was declared as a `class' or as a
16225 if (TREE_CODE (type) == RECORD_TYPE)
16226 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16227 cp_parser_check_class_key (class_key, type);
16229 /* If this type was already complete, and we see another definition,
16230 that's an error. */
16231 if (type != error_mark_node && COMPLETE_TYPE_P (type))
16233 error_at (type_start_token->location, "redefinition of %q#T",
16235 error_at (type_start_token->location, "previous definition of %q+#T",
16240 else if (type == error_mark_node)
16243 /* We will have entered the scope containing the class; the names of
16244 base classes should be looked up in that context. For example:
16246 struct A { struct B {}; struct C; };
16247 struct A::C : B {};
16251 /* Get the list of base-classes, if there is one. */
16252 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16253 *bases = cp_parser_base_clause (parser);
16256 /* Leave the scope given by the nested-name-specifier. We will
16257 enter the class scope itself while processing the members. */
16259 pop_scope (pushed_scope);
16261 if (invalid_explicit_specialization_p)
16263 end_specialization ();
16264 --parser->num_template_parameter_lists;
16266 *attributes_p = attributes;
16270 /* Parse a class-key.
16277 Returns the kind of class-key specified, or none_type to indicate
16280 static enum tag_types
16281 cp_parser_class_key (cp_parser* parser)
16284 enum tag_types tag_type;
16286 /* Look for the class-key. */
16287 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16291 /* Check to see if the TOKEN is a class-key. */
16292 tag_type = cp_parser_token_is_class_key (token);
16294 cp_parser_error (parser, "expected class-key");
16298 /* Parse an (optional) member-specification.
16300 member-specification:
16301 member-declaration member-specification [opt]
16302 access-specifier : member-specification [opt] */
16305 cp_parser_member_specification_opt (cp_parser* parser)
16312 /* Peek at the next token. */
16313 token = cp_lexer_peek_token (parser->lexer);
16314 /* If it's a `}', or EOF then we've seen all the members. */
16315 if (token->type == CPP_CLOSE_BRACE
16316 || token->type == CPP_EOF
16317 || token->type == CPP_PRAGMA_EOL)
16320 /* See if this token is a keyword. */
16321 keyword = token->keyword;
16325 case RID_PROTECTED:
16327 /* Consume the access-specifier. */
16328 cp_lexer_consume_token (parser->lexer);
16329 /* Remember which access-specifier is active. */
16330 current_access_specifier = token->u.value;
16331 /* Look for the `:'. */
16332 cp_parser_require (parser, CPP_COLON, "%<:%>");
16336 /* Accept #pragmas at class scope. */
16337 if (token->type == CPP_PRAGMA)
16339 cp_parser_pragma (parser, pragma_external);
16343 /* Otherwise, the next construction must be a
16344 member-declaration. */
16345 cp_parser_member_declaration (parser);
16350 /* Parse a member-declaration.
16352 member-declaration:
16353 decl-specifier-seq [opt] member-declarator-list [opt] ;
16354 function-definition ; [opt]
16355 :: [opt] nested-name-specifier template [opt] unqualified-id ;
16357 template-declaration
16359 member-declarator-list:
16361 member-declarator-list , member-declarator
16364 declarator pure-specifier [opt]
16365 declarator constant-initializer [opt]
16366 identifier [opt] : constant-expression
16370 member-declaration:
16371 __extension__ member-declaration
16374 declarator attributes [opt] pure-specifier [opt]
16375 declarator attributes [opt] constant-initializer [opt]
16376 identifier [opt] attributes [opt] : constant-expression
16380 member-declaration:
16381 static_assert-declaration */
16384 cp_parser_member_declaration (cp_parser* parser)
16386 cp_decl_specifier_seq decl_specifiers;
16387 tree prefix_attributes;
16389 int declares_class_or_enum;
16391 cp_token *token = NULL;
16392 cp_token *decl_spec_token_start = NULL;
16393 cp_token *initializer_token_start = NULL;
16394 int saved_pedantic;
16396 /* Check for the `__extension__' keyword. */
16397 if (cp_parser_extension_opt (parser, &saved_pedantic))
16400 cp_parser_member_declaration (parser);
16401 /* Restore the old value of the PEDANTIC flag. */
16402 pedantic = saved_pedantic;
16407 /* Check for a template-declaration. */
16408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16410 /* An explicit specialization here is an error condition, and we
16411 expect the specialization handler to detect and report this. */
16412 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16413 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16414 cp_parser_explicit_specialization (parser);
16416 cp_parser_template_declaration (parser, /*member_p=*/true);
16421 /* Check for a using-declaration. */
16422 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16424 /* Parse the using-declaration. */
16425 cp_parser_using_declaration (parser,
16426 /*access_declaration_p=*/false);
16430 /* Check for @defs. */
16431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16434 tree ivar_chains = cp_parser_objc_defs_expression (parser);
16435 ivar = ivar_chains;
16439 ivar = TREE_CHAIN (member);
16440 TREE_CHAIN (member) = NULL_TREE;
16441 finish_member_declaration (member);
16446 /* If the next token is `static_assert' we have a static assertion. */
16447 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16449 cp_parser_static_assert (parser, /*member_p=*/true);
16453 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16456 /* Parse the decl-specifier-seq. */
16457 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16458 cp_parser_decl_specifier_seq (parser,
16459 CP_PARSER_FLAGS_OPTIONAL,
16461 &declares_class_or_enum);
16462 prefix_attributes = decl_specifiers.attributes;
16463 decl_specifiers.attributes = NULL_TREE;
16464 /* Check for an invalid type-name. */
16465 if (!decl_specifiers.type
16466 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16468 /* If there is no declarator, then the decl-specifier-seq should
16470 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16472 /* If there was no decl-specifier-seq, and the next token is a
16473 `;', then we have something like:
16479 Each member-declaration shall declare at least one member
16480 name of the class. */
16481 if (!decl_specifiers.any_specifiers_p)
16483 cp_token *token = cp_lexer_peek_token (parser->lexer);
16484 if (!in_system_header_at (token->location))
16485 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16491 /* See if this declaration is a friend. */
16492 friend_p = cp_parser_friend_p (&decl_specifiers);
16493 /* If there were decl-specifiers, check to see if there was
16494 a class-declaration. */
16495 type = check_tag_decl (&decl_specifiers);
16496 /* Nested classes have already been added to the class, but
16497 a `friend' needs to be explicitly registered. */
16500 /* If the `friend' keyword was present, the friend must
16501 be introduced with a class-key. */
16502 if (!declares_class_or_enum)
16503 error_at (decl_spec_token_start->location,
16504 "a class-key must be used when declaring a friend");
16507 template <typename T> struct A {
16508 friend struct A<T>::B;
16511 A<T>::B will be represented by a TYPENAME_TYPE, and
16512 therefore not recognized by check_tag_decl. */
16514 && decl_specifiers.type
16515 && TYPE_P (decl_specifiers.type))
16516 type = decl_specifiers.type;
16517 if (!type || !TYPE_P (type))
16518 error_at (decl_spec_token_start->location,
16519 "friend declaration does not name a class or "
16522 make_friend_class (current_class_type, type,
16523 /*complain=*/true);
16525 /* If there is no TYPE, an error message will already have
16527 else if (!type || type == error_mark_node)
16529 /* An anonymous aggregate has to be handled specially; such
16530 a declaration really declares a data member (with a
16531 particular type), as opposed to a nested class. */
16532 else if (ANON_AGGR_TYPE_P (type))
16534 /* Remove constructors and such from TYPE, now that we
16535 know it is an anonymous aggregate. */
16536 fixup_anonymous_aggr (type);
16537 /* And make the corresponding data member. */
16538 decl = build_decl (decl_spec_token_start->location,
16539 FIELD_DECL, NULL_TREE, type);
16540 /* Add it to the class. */
16541 finish_member_declaration (decl);
16544 cp_parser_check_access_in_redeclaration
16546 decl_spec_token_start->location);
16551 /* See if these declarations will be friends. */
16552 friend_p = cp_parser_friend_p (&decl_specifiers);
16554 /* Keep going until we hit the `;' at the end of the
16556 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16558 tree attributes = NULL_TREE;
16559 tree first_attribute;
16561 /* Peek at the next token. */
16562 token = cp_lexer_peek_token (parser->lexer);
16564 /* Check for a bitfield declaration. */
16565 if (token->type == CPP_COLON
16566 || (token->type == CPP_NAME
16567 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16573 /* Get the name of the bitfield. Note that we cannot just
16574 check TOKEN here because it may have been invalidated by
16575 the call to cp_lexer_peek_nth_token above. */
16576 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16577 identifier = cp_parser_identifier (parser);
16579 identifier = NULL_TREE;
16581 /* Consume the `:' token. */
16582 cp_lexer_consume_token (parser->lexer);
16583 /* Get the width of the bitfield. */
16585 = cp_parser_constant_expression (parser,
16586 /*allow_non_constant=*/false,
16589 /* Look for attributes that apply to the bitfield. */
16590 attributes = cp_parser_attributes_opt (parser);
16591 /* Remember which attributes are prefix attributes and
16593 first_attribute = attributes;
16594 /* Combine the attributes. */
16595 attributes = chainon (prefix_attributes, attributes);
16597 /* Create the bitfield declaration. */
16598 decl = grokbitfield (identifier
16599 ? make_id_declarator (NULL_TREE,
16609 cp_declarator *declarator;
16611 tree asm_specification;
16612 int ctor_dtor_or_conv_p;
16614 /* Parse the declarator. */
16616 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16617 &ctor_dtor_or_conv_p,
16618 /*parenthesized_p=*/NULL,
16619 /*member_p=*/true);
16621 /* If something went wrong parsing the declarator, make sure
16622 that we at least consume some tokens. */
16623 if (declarator == cp_error_declarator)
16625 /* Skip to the end of the statement. */
16626 cp_parser_skip_to_end_of_statement (parser);
16627 /* If the next token is not a semicolon, that is
16628 probably because we just skipped over the body of
16629 a function. So, we consume a semicolon if
16630 present, but do not issue an error message if it
16632 if (cp_lexer_next_token_is (parser->lexer,
16634 cp_lexer_consume_token (parser->lexer);
16638 if (declares_class_or_enum & 2)
16639 cp_parser_check_for_definition_in_return_type
16640 (declarator, decl_specifiers.type,
16641 decl_specifiers.type_location);
16643 /* Look for an asm-specification. */
16644 asm_specification = cp_parser_asm_specification_opt (parser);
16645 /* Look for attributes that apply to the declaration. */
16646 attributes = cp_parser_attributes_opt (parser);
16647 /* Remember which attributes are prefix attributes and
16649 first_attribute = attributes;
16650 /* Combine the attributes. */
16651 attributes = chainon (prefix_attributes, attributes);
16653 /* If it's an `=', then we have a constant-initializer or a
16654 pure-specifier. It is not correct to parse the
16655 initializer before registering the member declaration
16656 since the member declaration should be in scope while
16657 its initializer is processed. However, the rest of the
16658 front end does not yet provide an interface that allows
16659 us to handle this correctly. */
16660 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16664 A pure-specifier shall be used only in the declaration of
16665 a virtual function.
16667 A member-declarator can contain a constant-initializer
16668 only if it declares a static member of integral or
16671 Therefore, if the DECLARATOR is for a function, we look
16672 for a pure-specifier; otherwise, we look for a
16673 constant-initializer. When we call `grokfield', it will
16674 perform more stringent semantics checks. */
16675 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16676 if (function_declarator_p (declarator))
16677 initializer = cp_parser_pure_specifier (parser);
16679 /* Parse the initializer. */
16680 initializer = cp_parser_constant_initializer (parser);
16682 /* Otherwise, there is no initializer. */
16684 initializer = NULL_TREE;
16686 /* See if we are probably looking at a function
16687 definition. We are certainly not looking at a
16688 member-declarator. Calling `grokfield' has
16689 side-effects, so we must not do it unless we are sure
16690 that we are looking at a member-declarator. */
16691 if (cp_parser_token_starts_function_definition_p
16692 (cp_lexer_peek_token (parser->lexer)))
16694 /* The grammar does not allow a pure-specifier to be
16695 used when a member function is defined. (It is
16696 possible that this fact is an oversight in the
16697 standard, since a pure function may be defined
16698 outside of the class-specifier. */
16700 error_at (initializer_token_start->location,
16701 "pure-specifier on function-definition");
16702 decl = cp_parser_save_member_function_body (parser,
16706 /* If the member was not a friend, declare it here. */
16708 finish_member_declaration (decl);
16709 /* Peek at the next token. */
16710 token = cp_lexer_peek_token (parser->lexer);
16711 /* If the next token is a semicolon, consume it. */
16712 if (token->type == CPP_SEMICOLON)
16713 cp_lexer_consume_token (parser->lexer);
16717 if (declarator->kind == cdk_function)
16718 declarator->id_loc = token->location;
16719 /* Create the declaration. */
16720 decl = grokfield (declarator, &decl_specifiers,
16721 initializer, /*init_const_expr_p=*/true,
16726 /* Reset PREFIX_ATTRIBUTES. */
16727 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16728 attributes = TREE_CHAIN (attributes);
16730 TREE_CHAIN (attributes) = NULL_TREE;
16732 /* If there is any qualification still in effect, clear it
16733 now; we will be starting fresh with the next declarator. */
16734 parser->scope = NULL_TREE;
16735 parser->qualifying_scope = NULL_TREE;
16736 parser->object_scope = NULL_TREE;
16737 /* If it's a `,', then there are more declarators. */
16738 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16739 cp_lexer_consume_token (parser->lexer);
16740 /* If the next token isn't a `;', then we have a parse error. */
16741 else if (cp_lexer_next_token_is_not (parser->lexer,
16744 cp_parser_error (parser, "expected %<;%>");
16745 /* Skip tokens until we find a `;'. */
16746 cp_parser_skip_to_end_of_statement (parser);
16753 /* Add DECL to the list of members. */
16755 finish_member_declaration (decl);
16757 if (TREE_CODE (decl) == FUNCTION_DECL)
16758 cp_parser_save_default_args (parser, decl);
16763 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16766 /* Parse a pure-specifier.
16771 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16772 Otherwise, ERROR_MARK_NODE is returned. */
16775 cp_parser_pure_specifier (cp_parser* parser)
16779 /* Look for the `=' token. */
16780 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16781 return error_mark_node;
16782 /* Look for the `0' token. */
16783 token = cp_lexer_peek_token (parser->lexer);
16785 if (token->type == CPP_EOF
16786 || token->type == CPP_PRAGMA_EOL)
16787 return error_mark_node;
16789 cp_lexer_consume_token (parser->lexer);
16791 /* Accept = default or = delete in c++0x mode. */
16792 if (token->keyword == RID_DEFAULT
16793 || token->keyword == RID_DELETE)
16795 maybe_warn_cpp0x ("defaulted and deleted functions");
16796 return token->u.value;
16799 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16800 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16802 cp_parser_error (parser,
16803 "invalid pure specifier (only %<= 0%> is allowed)");
16804 cp_parser_skip_to_end_of_statement (parser);
16805 return error_mark_node;
16807 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16809 error_at (token->location, "templates may not be %<virtual%>");
16810 return error_mark_node;
16813 return integer_zero_node;
16816 /* Parse a constant-initializer.
16818 constant-initializer:
16819 = constant-expression
16821 Returns a representation of the constant-expression. */
16824 cp_parser_constant_initializer (cp_parser* parser)
16826 /* Look for the `=' token. */
16827 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16828 return error_mark_node;
16830 /* It is invalid to write:
16832 struct S { static const int i = { 7 }; };
16835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16837 cp_parser_error (parser,
16838 "a brace-enclosed initializer is not allowed here");
16839 /* Consume the opening brace. */
16840 cp_lexer_consume_token (parser->lexer);
16841 /* Skip the initializer. */
16842 cp_parser_skip_to_closing_brace (parser);
16843 /* Look for the trailing `}'. */
16844 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16846 return error_mark_node;
16849 return cp_parser_constant_expression (parser,
16850 /*allow_non_constant=*/false,
16854 /* Derived classes [gram.class.derived] */
16856 /* Parse a base-clause.
16859 : base-specifier-list
16861 base-specifier-list:
16862 base-specifier ... [opt]
16863 base-specifier-list , base-specifier ... [opt]
16865 Returns a TREE_LIST representing the base-classes, in the order in
16866 which they were declared. The representation of each node is as
16867 described by cp_parser_base_specifier.
16869 In the case that no bases are specified, this function will return
16870 NULL_TREE, not ERROR_MARK_NODE. */
16873 cp_parser_base_clause (cp_parser* parser)
16875 tree bases = NULL_TREE;
16877 /* Look for the `:' that begins the list. */
16878 cp_parser_require (parser, CPP_COLON, "%<:%>");
16880 /* Scan the base-specifier-list. */
16885 bool pack_expansion_p = false;
16887 /* Look for the base-specifier. */
16888 base = cp_parser_base_specifier (parser);
16889 /* Look for the (optional) ellipsis. */
16890 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16892 /* Consume the `...'. */
16893 cp_lexer_consume_token (parser->lexer);
16895 pack_expansion_p = true;
16898 /* Add BASE to the front of the list. */
16899 if (base != error_mark_node)
16901 if (pack_expansion_p)
16902 /* Make this a pack expansion type. */
16903 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16906 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16908 TREE_CHAIN (base) = bases;
16912 /* Peek at the next token. */
16913 token = cp_lexer_peek_token (parser->lexer);
16914 /* If it's not a comma, then the list is complete. */
16915 if (token->type != CPP_COMMA)
16917 /* Consume the `,'. */
16918 cp_lexer_consume_token (parser->lexer);
16921 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16922 base class had a qualified name. However, the next name that
16923 appears is certainly not qualified. */
16924 parser->scope = NULL_TREE;
16925 parser->qualifying_scope = NULL_TREE;
16926 parser->object_scope = NULL_TREE;
16928 return nreverse (bases);
16931 /* Parse a base-specifier.
16934 :: [opt] nested-name-specifier [opt] class-name
16935 virtual access-specifier [opt] :: [opt] nested-name-specifier
16937 access-specifier virtual [opt] :: [opt] nested-name-specifier
16940 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16941 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16942 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16943 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16946 cp_parser_base_specifier (cp_parser* parser)
16950 bool virtual_p = false;
16951 bool duplicate_virtual_error_issued_p = false;
16952 bool duplicate_access_error_issued_p = false;
16953 bool class_scope_p, template_p;
16954 tree access = access_default_node;
16957 /* Process the optional `virtual' and `access-specifier'. */
16960 /* Peek at the next token. */
16961 token = cp_lexer_peek_token (parser->lexer);
16962 /* Process `virtual'. */
16963 switch (token->keyword)
16966 /* If `virtual' appears more than once, issue an error. */
16967 if (virtual_p && !duplicate_virtual_error_issued_p)
16969 cp_parser_error (parser,
16970 "%<virtual%> specified more than once in base-specified");
16971 duplicate_virtual_error_issued_p = true;
16976 /* Consume the `virtual' token. */
16977 cp_lexer_consume_token (parser->lexer);
16982 case RID_PROTECTED:
16984 /* If more than one access specifier appears, issue an
16986 if (access != access_default_node
16987 && !duplicate_access_error_issued_p)
16989 cp_parser_error (parser,
16990 "more than one access specifier in base-specified");
16991 duplicate_access_error_issued_p = true;
16994 access = ridpointers[(int) token->keyword];
16996 /* Consume the access-specifier. */
16997 cp_lexer_consume_token (parser->lexer);
17006 /* It is not uncommon to see programs mechanically, erroneously, use
17007 the 'typename' keyword to denote (dependent) qualified types
17008 as base classes. */
17009 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17011 token = cp_lexer_peek_token (parser->lexer);
17012 if (!processing_template_decl)
17013 error_at (token->location,
17014 "keyword %<typename%> not allowed outside of templates");
17016 error_at (token->location,
17017 "keyword %<typename%> not allowed in this context "
17018 "(the base class is implicitly a type)");
17019 cp_lexer_consume_token (parser->lexer);
17022 /* Look for the optional `::' operator. */
17023 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17024 /* Look for the nested-name-specifier. The simplest way to
17029 The keyword `typename' is not permitted in a base-specifier or
17030 mem-initializer; in these contexts a qualified name that
17031 depends on a template-parameter is implicitly assumed to be a
17034 is to pretend that we have seen the `typename' keyword at this
17036 cp_parser_nested_name_specifier_opt (parser,
17037 /*typename_keyword_p=*/true,
17038 /*check_dependency_p=*/true,
17040 /*is_declaration=*/true);
17041 /* If the base class is given by a qualified name, assume that names
17042 we see are type names or templates, as appropriate. */
17043 class_scope_p = (parser->scope && TYPE_P (parser->scope));
17044 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17046 /* Finally, look for the class-name. */
17047 type = cp_parser_class_name (parser,
17051 /*check_dependency_p=*/true,
17052 /*class_head_p=*/false,
17053 /*is_declaration=*/true);
17055 if (type == error_mark_node)
17056 return error_mark_node;
17058 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17061 /* Exception handling [gram.exception] */
17063 /* Parse an (optional) exception-specification.
17065 exception-specification:
17066 throw ( type-id-list [opt] )
17068 Returns a TREE_LIST representing the exception-specification. The
17069 TREE_VALUE of each node is a type. */
17072 cp_parser_exception_specification_opt (cp_parser* parser)
17077 /* Peek at the next token. */
17078 token = cp_lexer_peek_token (parser->lexer);
17079 /* If it's not `throw', then there's no exception-specification. */
17080 if (!cp_parser_is_keyword (token, RID_THROW))
17083 /* Consume the `throw'. */
17084 cp_lexer_consume_token (parser->lexer);
17086 /* Look for the `('. */
17087 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17089 /* Peek at the next token. */
17090 token = cp_lexer_peek_token (parser->lexer);
17091 /* If it's not a `)', then there is a type-id-list. */
17092 if (token->type != CPP_CLOSE_PAREN)
17094 const char *saved_message;
17096 /* Types may not be defined in an exception-specification. */
17097 saved_message = parser->type_definition_forbidden_message;
17098 parser->type_definition_forbidden_message
17099 = "types may not be defined in an exception-specification";
17100 /* Parse the type-id-list. */
17101 type_id_list = cp_parser_type_id_list (parser);
17102 /* Restore the saved message. */
17103 parser->type_definition_forbidden_message = saved_message;
17106 type_id_list = empty_except_spec;
17108 /* Look for the `)'. */
17109 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17111 return type_id_list;
17114 /* Parse an (optional) type-id-list.
17118 type-id-list , type-id ... [opt]
17120 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
17121 in the order that the types were presented. */
17124 cp_parser_type_id_list (cp_parser* parser)
17126 tree types = NULL_TREE;
17133 /* Get the next type-id. */
17134 type = cp_parser_type_id (parser);
17135 /* Parse the optional ellipsis. */
17136 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17138 /* Consume the `...'. */
17139 cp_lexer_consume_token (parser->lexer);
17141 /* Turn the type into a pack expansion expression. */
17142 type = make_pack_expansion (type);
17144 /* Add it to the list. */
17145 types = add_exception_specifier (types, type, /*complain=*/1);
17146 /* Peek at the next token. */
17147 token = cp_lexer_peek_token (parser->lexer);
17148 /* If it is not a `,', we are done. */
17149 if (token->type != CPP_COMMA)
17151 /* Consume the `,'. */
17152 cp_lexer_consume_token (parser->lexer);
17155 return nreverse (types);
17158 /* Parse a try-block.
17161 try compound-statement handler-seq */
17164 cp_parser_try_block (cp_parser* parser)
17168 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17169 try_block = begin_try_block ();
17170 cp_parser_compound_statement (parser, NULL, true);
17171 finish_try_block (try_block);
17172 cp_parser_handler_seq (parser);
17173 finish_handler_sequence (try_block);
17178 /* Parse a function-try-block.
17180 function-try-block:
17181 try ctor-initializer [opt] function-body handler-seq */
17184 cp_parser_function_try_block (cp_parser* parser)
17186 tree compound_stmt;
17188 bool ctor_initializer_p;
17190 /* Look for the `try' keyword. */
17191 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17193 /* Let the rest of the front end know where we are. */
17194 try_block = begin_function_try_block (&compound_stmt);
17195 /* Parse the function-body. */
17197 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17198 /* We're done with the `try' part. */
17199 finish_function_try_block (try_block);
17200 /* Parse the handlers. */
17201 cp_parser_handler_seq (parser);
17202 /* We're done with the handlers. */
17203 finish_function_handler_sequence (try_block, compound_stmt);
17205 return ctor_initializer_p;
17208 /* Parse a handler-seq.
17211 handler handler-seq [opt] */
17214 cp_parser_handler_seq (cp_parser* parser)
17220 /* Parse the handler. */
17221 cp_parser_handler (parser);
17222 /* Peek at the next token. */
17223 token = cp_lexer_peek_token (parser->lexer);
17224 /* If it's not `catch' then there are no more handlers. */
17225 if (!cp_parser_is_keyword (token, RID_CATCH))
17230 /* Parse a handler.
17233 catch ( exception-declaration ) compound-statement */
17236 cp_parser_handler (cp_parser* parser)
17241 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17242 handler = begin_handler ();
17243 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17244 declaration = cp_parser_exception_declaration (parser);
17245 finish_handler_parms (declaration, handler);
17246 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17247 cp_parser_compound_statement (parser, NULL, false);
17248 finish_handler (handler);
17251 /* Parse an exception-declaration.
17253 exception-declaration:
17254 type-specifier-seq declarator
17255 type-specifier-seq abstract-declarator
17259 Returns a VAR_DECL for the declaration, or NULL_TREE if the
17260 ellipsis variant is used. */
17263 cp_parser_exception_declaration (cp_parser* parser)
17265 cp_decl_specifier_seq type_specifiers;
17266 cp_declarator *declarator;
17267 const char *saved_message;
17269 /* If it's an ellipsis, it's easy to handle. */
17270 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17272 /* Consume the `...' token. */
17273 cp_lexer_consume_token (parser->lexer);
17277 /* Types may not be defined in exception-declarations. */
17278 saved_message = parser->type_definition_forbidden_message;
17279 parser->type_definition_forbidden_message
17280 = "types may not be defined in exception-declarations";
17282 /* Parse the type-specifier-seq. */
17283 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
17285 /* If it's a `)', then there is no declarator. */
17286 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17289 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17290 /*ctor_dtor_or_conv_p=*/NULL,
17291 /*parenthesized_p=*/NULL,
17292 /*member_p=*/false);
17294 /* Restore the saved message. */
17295 parser->type_definition_forbidden_message = saved_message;
17297 if (!type_specifiers.any_specifiers_p)
17298 return error_mark_node;
17300 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17303 /* Parse a throw-expression.
17306 throw assignment-expression [opt]
17308 Returns a THROW_EXPR representing the throw-expression. */
17311 cp_parser_throw_expression (cp_parser* parser)
17316 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17317 token = cp_lexer_peek_token (parser->lexer);
17318 /* Figure out whether or not there is an assignment-expression
17319 following the "throw" keyword. */
17320 if (token->type == CPP_COMMA
17321 || token->type == CPP_SEMICOLON
17322 || token->type == CPP_CLOSE_PAREN
17323 || token->type == CPP_CLOSE_SQUARE
17324 || token->type == CPP_CLOSE_BRACE
17325 || token->type == CPP_COLON)
17326 expression = NULL_TREE;
17328 expression = cp_parser_assignment_expression (parser,
17329 /*cast_p=*/false, NULL);
17331 return build_throw (expression);
17334 /* GNU Extensions */
17336 /* Parse an (optional) asm-specification.
17339 asm ( string-literal )
17341 If the asm-specification is present, returns a STRING_CST
17342 corresponding to the string-literal. Otherwise, returns
17346 cp_parser_asm_specification_opt (cp_parser* parser)
17349 tree asm_specification;
17351 /* Peek at the next token. */
17352 token = cp_lexer_peek_token (parser->lexer);
17353 /* If the next token isn't the `asm' keyword, then there's no
17354 asm-specification. */
17355 if (!cp_parser_is_keyword (token, RID_ASM))
17358 /* Consume the `asm' token. */
17359 cp_lexer_consume_token (parser->lexer);
17360 /* Look for the `('. */
17361 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17363 /* Look for the string-literal. */
17364 asm_specification = cp_parser_string_literal (parser, false, false);
17366 /* Look for the `)'. */
17367 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17369 return asm_specification;
17372 /* Parse an asm-operand-list.
17376 asm-operand-list , asm-operand
17379 string-literal ( expression )
17380 [ string-literal ] string-literal ( expression )
17382 Returns a TREE_LIST representing the operands. The TREE_VALUE of
17383 each node is the expression. The TREE_PURPOSE is itself a
17384 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17385 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17386 is a STRING_CST for the string literal before the parenthesis. Returns
17387 ERROR_MARK_NODE if any of the operands are invalid. */
17390 cp_parser_asm_operand_list (cp_parser* parser)
17392 tree asm_operands = NULL_TREE;
17393 bool invalid_operands = false;
17397 tree string_literal;
17401 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17403 /* Consume the `[' token. */
17404 cp_lexer_consume_token (parser->lexer);
17405 /* Read the operand name. */
17406 name = cp_parser_identifier (parser);
17407 if (name != error_mark_node)
17408 name = build_string (IDENTIFIER_LENGTH (name),
17409 IDENTIFIER_POINTER (name));
17410 /* Look for the closing `]'. */
17411 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17415 /* Look for the string-literal. */
17416 string_literal = cp_parser_string_literal (parser, false, false);
17418 /* Look for the `('. */
17419 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17420 /* Parse the expression. */
17421 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17422 /* Look for the `)'. */
17423 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17425 if (name == error_mark_node
17426 || string_literal == error_mark_node
17427 || expression == error_mark_node)
17428 invalid_operands = true;
17430 /* Add this operand to the list. */
17431 asm_operands = tree_cons (build_tree_list (name, string_literal),
17434 /* If the next token is not a `,', there are no more
17436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17438 /* Consume the `,'. */
17439 cp_lexer_consume_token (parser->lexer);
17442 return invalid_operands ? error_mark_node : nreverse (asm_operands);
17445 /* Parse an asm-clobber-list.
17449 asm-clobber-list , string-literal
17451 Returns a TREE_LIST, indicating the clobbers in the order that they
17452 appeared. The TREE_VALUE of each node is a STRING_CST. */
17455 cp_parser_asm_clobber_list (cp_parser* parser)
17457 tree clobbers = NULL_TREE;
17461 tree string_literal;
17463 /* Look for the string literal. */
17464 string_literal = cp_parser_string_literal (parser, false, false);
17465 /* Add it to the list. */
17466 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17467 /* If the next token is not a `,', then the list is
17469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17471 /* Consume the `,' token. */
17472 cp_lexer_consume_token (parser->lexer);
17478 /* Parse an asm-label-list.
17482 asm-label-list , identifier
17484 Returns a TREE_LIST, indicating the labels in the order that they
17485 appeared. The TREE_VALUE of each node is a label. */
17488 cp_parser_asm_label_list (cp_parser* parser)
17490 tree labels = NULL_TREE;
17494 tree identifier, label, name;
17496 /* Look for the identifier. */
17497 identifier = cp_parser_identifier (parser);
17498 if (!error_operand_p (identifier))
17500 label = lookup_label (identifier);
17501 if (TREE_CODE (label) == LABEL_DECL)
17503 TREE_USED (label) = 1;
17504 check_goto (label);
17505 name = build_string (IDENTIFIER_LENGTH (identifier),
17506 IDENTIFIER_POINTER (identifier));
17507 labels = tree_cons (name, label, labels);
17510 /* If the next token is not a `,', then the list is
17512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17514 /* Consume the `,' token. */
17515 cp_lexer_consume_token (parser->lexer);
17518 return nreverse (labels);
17521 /* Parse an (optional) series of attributes.
17524 attributes attribute
17527 __attribute__ (( attribute-list [opt] ))
17529 The return value is as for cp_parser_attribute_list. */
17532 cp_parser_attributes_opt (cp_parser* parser)
17534 tree attributes = NULL_TREE;
17539 tree attribute_list;
17541 /* Peek at the next token. */
17542 token = cp_lexer_peek_token (parser->lexer);
17543 /* If it's not `__attribute__', then we're done. */
17544 if (token->keyword != RID_ATTRIBUTE)
17547 /* Consume the `__attribute__' keyword. */
17548 cp_lexer_consume_token (parser->lexer);
17549 /* Look for the two `(' tokens. */
17550 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17551 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17553 /* Peek at the next token. */
17554 token = cp_lexer_peek_token (parser->lexer);
17555 if (token->type != CPP_CLOSE_PAREN)
17556 /* Parse the attribute-list. */
17557 attribute_list = cp_parser_attribute_list (parser);
17559 /* If the next token is a `)', then there is no attribute
17561 attribute_list = NULL;
17563 /* Look for the two `)' tokens. */
17564 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17565 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17567 /* Add these new attributes to the list. */
17568 attributes = chainon (attributes, attribute_list);
17574 /* Parse an attribute-list.
17578 attribute-list , attribute
17582 identifier ( identifier )
17583 identifier ( identifier , expression-list )
17584 identifier ( expression-list )
17586 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
17587 to an attribute. The TREE_PURPOSE of each node is the identifier
17588 indicating which attribute is in use. The TREE_VALUE represents
17589 the arguments, if any. */
17592 cp_parser_attribute_list (cp_parser* parser)
17594 tree attribute_list = NULL_TREE;
17595 bool save_translate_strings_p = parser->translate_strings_p;
17597 parser->translate_strings_p = false;
17604 /* Look for the identifier. We also allow keywords here; for
17605 example `__attribute__ ((const))' is legal. */
17606 token = cp_lexer_peek_token (parser->lexer);
17607 if (token->type == CPP_NAME
17608 || token->type == CPP_KEYWORD)
17610 tree arguments = NULL_TREE;
17612 /* Consume the token. */
17613 token = cp_lexer_consume_token (parser->lexer);
17615 /* Save away the identifier that indicates which attribute
17617 identifier = (token->type == CPP_KEYWORD)
17618 /* For keywords, use the canonical spelling, not the
17619 parsed identifier. */
17620 ? ridpointers[(int) token->keyword]
17623 attribute = build_tree_list (identifier, NULL_TREE);
17625 /* Peek at the next token. */
17626 token = cp_lexer_peek_token (parser->lexer);
17627 /* If it's an `(', then parse the attribute arguments. */
17628 if (token->type == CPP_OPEN_PAREN)
17631 vec = cp_parser_parenthesized_expression_list
17632 (parser, true, /*cast_p=*/false,
17633 /*allow_expansion_p=*/false,
17634 /*non_constant_p=*/NULL);
17636 arguments = error_mark_node;
17639 arguments = build_tree_list_vec (vec);
17640 release_tree_vector (vec);
17642 /* Save the arguments away. */
17643 TREE_VALUE (attribute) = arguments;
17646 if (arguments != error_mark_node)
17648 /* Add this attribute to the list. */
17649 TREE_CHAIN (attribute) = attribute_list;
17650 attribute_list = attribute;
17653 token = cp_lexer_peek_token (parser->lexer);
17655 /* Now, look for more attributes. If the next token isn't a
17656 `,', we're done. */
17657 if (token->type != CPP_COMMA)
17660 /* Consume the comma and keep going. */
17661 cp_lexer_consume_token (parser->lexer);
17663 parser->translate_strings_p = save_translate_strings_p;
17665 /* We built up the list in reverse order. */
17666 return nreverse (attribute_list);
17669 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17670 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17671 current value of the PEDANTIC flag, regardless of whether or not
17672 the `__extension__' keyword is present. The caller is responsible
17673 for restoring the value of the PEDANTIC flag. */
17676 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17678 /* Save the old value of the PEDANTIC flag. */
17679 *saved_pedantic = pedantic;
17681 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17683 /* Consume the `__extension__' token. */
17684 cp_lexer_consume_token (parser->lexer);
17685 /* We're not being pedantic while the `__extension__' keyword is
17695 /* Parse a label declaration.
17698 __label__ label-declarator-seq ;
17700 label-declarator-seq:
17701 identifier , label-declarator-seq
17705 cp_parser_label_declaration (cp_parser* parser)
17707 /* Look for the `__label__' keyword. */
17708 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17714 /* Look for an identifier. */
17715 identifier = cp_parser_identifier (parser);
17716 /* If we failed, stop. */
17717 if (identifier == error_mark_node)
17719 /* Declare it as a label. */
17720 finish_label_decl (identifier);
17721 /* If the next token is a `;', stop. */
17722 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17724 /* Look for the `,' separating the label declarations. */
17725 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17728 /* Look for the final `;'. */
17729 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17732 /* Support Functions */
17734 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17735 NAME should have one of the representations used for an
17736 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17737 is returned. If PARSER->SCOPE is a dependent type, then a
17738 SCOPE_REF is returned.
17740 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17741 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17742 was formed. Abstractly, such entities should not be passed to this
17743 function, because they do not need to be looked up, but it is
17744 simpler to check for this special case here, rather than at the
17747 In cases not explicitly covered above, this function returns a
17748 DECL, OVERLOAD, or baselink representing the result of the lookup.
17749 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17752 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17753 (e.g., "struct") that was used. In that case bindings that do not
17754 refer to types are ignored.
17756 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17759 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17762 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17765 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17766 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17767 NULL_TREE otherwise. */
17770 cp_parser_lookup_name (cp_parser *parser, tree name,
17771 enum tag_types tag_type,
17774 bool check_dependency,
17775 tree *ambiguous_decls,
17776 location_t name_location)
17780 tree object_type = parser->context->object_type;
17782 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17783 flags |= LOOKUP_COMPLAIN;
17785 /* Assume that the lookup will be unambiguous. */
17786 if (ambiguous_decls)
17787 *ambiguous_decls = NULL_TREE;
17789 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17790 no longer valid. Note that if we are parsing tentatively, and
17791 the parse fails, OBJECT_TYPE will be automatically restored. */
17792 parser->context->object_type = NULL_TREE;
17794 if (name == error_mark_node)
17795 return error_mark_node;
17797 /* A template-id has already been resolved; there is no lookup to
17799 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17801 if (BASELINK_P (name))
17803 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17804 == TEMPLATE_ID_EXPR);
17808 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17809 it should already have been checked to make sure that the name
17810 used matches the type being destroyed. */
17811 if (TREE_CODE (name) == BIT_NOT_EXPR)
17815 /* Figure out to which type this destructor applies. */
17817 type = parser->scope;
17818 else if (object_type)
17819 type = object_type;
17821 type = current_class_type;
17822 /* If that's not a class type, there is no destructor. */
17823 if (!type || !CLASS_TYPE_P (type))
17824 return error_mark_node;
17825 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17826 lazily_declare_fn (sfk_destructor, type);
17827 if (!CLASSTYPE_DESTRUCTORS (type))
17828 return error_mark_node;
17829 /* If it was a class type, return the destructor. */
17830 return CLASSTYPE_DESTRUCTORS (type);
17833 /* By this point, the NAME should be an ordinary identifier. If
17834 the id-expression was a qualified name, the qualifying scope is
17835 stored in PARSER->SCOPE at this point. */
17836 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17838 /* Perform the lookup. */
17843 if (parser->scope == error_mark_node)
17844 return error_mark_node;
17846 /* If the SCOPE is dependent, the lookup must be deferred until
17847 the template is instantiated -- unless we are explicitly
17848 looking up names in uninstantiated templates. Even then, we
17849 cannot look up the name if the scope is not a class type; it
17850 might, for example, be a template type parameter. */
17851 dependent_p = (TYPE_P (parser->scope)
17852 && dependent_scope_p (parser->scope));
17853 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17855 /* Defer lookup. */
17856 decl = error_mark_node;
17859 tree pushed_scope = NULL_TREE;
17861 /* If PARSER->SCOPE is a dependent type, then it must be a
17862 class type, and we must not be checking dependencies;
17863 otherwise, we would have processed this lookup above. So
17864 that PARSER->SCOPE is not considered a dependent base by
17865 lookup_member, we must enter the scope here. */
17867 pushed_scope = push_scope (parser->scope);
17868 /* If the PARSER->SCOPE is a template specialization, it
17869 may be instantiated during name lookup. In that case,
17870 errors may be issued. Even if we rollback the current
17871 tentative parse, those errors are valid. */
17872 decl = lookup_qualified_name (parser->scope, name,
17873 tag_type != none_type,
17874 /*complain=*/true);
17876 /* If we have a single function from a using decl, pull it out. */
17877 if (TREE_CODE (decl) == OVERLOAD
17878 && !really_overloaded_fn (decl))
17879 decl = OVL_FUNCTION (decl);
17882 pop_scope (pushed_scope);
17885 /* If the scope is a dependent type and either we deferred lookup or
17886 we did lookup but didn't find the name, rememeber the name. */
17887 if (decl == error_mark_node && TYPE_P (parser->scope)
17888 && dependent_type_p (parser->scope))
17894 /* The resolution to Core Issue 180 says that `struct
17895 A::B' should be considered a type-name, even if `A'
17897 type = make_typename_type (parser->scope, name, tag_type,
17898 /*complain=*/tf_error);
17899 decl = TYPE_NAME (type);
17901 else if (is_template
17902 && (cp_parser_next_token_ends_template_argument_p (parser)
17903 || cp_lexer_next_token_is (parser->lexer,
17905 decl = make_unbound_class_template (parser->scope,
17907 /*complain=*/tf_error);
17909 decl = build_qualified_name (/*type=*/NULL_TREE,
17910 parser->scope, name,
17913 parser->qualifying_scope = parser->scope;
17914 parser->object_scope = NULL_TREE;
17916 else if (object_type)
17918 tree object_decl = NULL_TREE;
17919 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17920 OBJECT_TYPE is not a class. */
17921 if (CLASS_TYPE_P (object_type))
17922 /* If the OBJECT_TYPE is a template specialization, it may
17923 be instantiated during name lookup. In that case, errors
17924 may be issued. Even if we rollback the current tentative
17925 parse, those errors are valid. */
17926 object_decl = lookup_member (object_type,
17929 tag_type != none_type);
17930 /* Look it up in the enclosing context, too. */
17931 decl = lookup_name_real (name, tag_type != none_type,
17933 /*block_p=*/true, is_namespace, flags);
17934 parser->object_scope = object_type;
17935 parser->qualifying_scope = NULL_TREE;
17937 decl = object_decl;
17941 decl = lookup_name_real (name, tag_type != none_type,
17943 /*block_p=*/true, is_namespace, flags);
17944 parser->qualifying_scope = NULL_TREE;
17945 parser->object_scope = NULL_TREE;
17948 /* If the lookup failed, let our caller know. */
17949 if (!decl || decl == error_mark_node)
17950 return error_mark_node;
17952 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17953 if (TREE_CODE (decl) == TREE_LIST)
17955 if (ambiguous_decls)
17956 *ambiguous_decls = decl;
17957 /* The error message we have to print is too complicated for
17958 cp_parser_error, so we incorporate its actions directly. */
17959 if (!cp_parser_simulate_error (parser))
17961 error_at (name_location, "reference to %qD is ambiguous",
17963 print_candidates (decl);
17965 return error_mark_node;
17968 gcc_assert (DECL_P (decl)
17969 || TREE_CODE (decl) == OVERLOAD
17970 || TREE_CODE (decl) == SCOPE_REF
17971 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17972 || BASELINK_P (decl));
17974 /* If we have resolved the name of a member declaration, check to
17975 see if the declaration is accessible. When the name resolves to
17976 set of overloaded functions, accessibility is checked when
17977 overload resolution is done.
17979 During an explicit instantiation, access is not checked at all,
17980 as per [temp.explicit]. */
17982 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17987 /* Like cp_parser_lookup_name, but for use in the typical case where
17988 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17989 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17992 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17994 return cp_parser_lookup_name (parser, name,
17996 /*is_template=*/false,
17997 /*is_namespace=*/false,
17998 /*check_dependency=*/true,
17999 /*ambiguous_decls=*/NULL,
18003 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18004 the current context, return the TYPE_DECL. If TAG_NAME_P is
18005 true, the DECL indicates the class being defined in a class-head,
18006 or declared in an elaborated-type-specifier.
18008 Otherwise, return DECL. */
18011 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18013 /* If the TEMPLATE_DECL is being declared as part of a class-head,
18014 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18017 template <typename T> struct B;
18020 template <typename T> struct A::B {};
18022 Similarly, in an elaborated-type-specifier:
18024 namespace N { struct X{}; }
18027 template <typename T> friend struct N::X;
18030 However, if the DECL refers to a class type, and we are in
18031 the scope of the class, then the name lookup automatically
18032 finds the TYPE_DECL created by build_self_reference rather
18033 than a TEMPLATE_DECL. For example, in:
18035 template <class T> struct S {
18039 there is no need to handle such case. */
18041 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18042 return DECL_TEMPLATE_RESULT (decl);
18047 /* If too many, or too few, template-parameter lists apply to the
18048 declarator, issue an error message. Returns TRUE if all went well,
18049 and FALSE otherwise. */
18052 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18053 cp_declarator *declarator,
18054 location_t declarator_location)
18056 unsigned num_templates;
18058 /* We haven't seen any classes that involve template parameters yet. */
18061 switch (declarator->kind)
18064 if (declarator->u.id.qualifying_scope)
18069 scope = declarator->u.id.qualifying_scope;
18070 member = declarator->u.id.unqualified_name;
18072 while (scope && CLASS_TYPE_P (scope))
18074 /* You're supposed to have one `template <...>'
18075 for every template class, but you don't need one
18076 for a full specialization. For example:
18078 template <class T> struct S{};
18079 template <> struct S<int> { void f(); };
18080 void S<int>::f () {}
18082 is correct; there shouldn't be a `template <>' for
18083 the definition of `S<int>::f'. */
18084 if (!CLASSTYPE_TEMPLATE_INFO (scope))
18085 /* If SCOPE does not have template information of any
18086 kind, then it is not a template, nor is it nested
18087 within a template. */
18089 if (explicit_class_specialization_p (scope))
18091 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18094 scope = TYPE_CONTEXT (scope);
18097 else if (TREE_CODE (declarator->u.id.unqualified_name)
18098 == TEMPLATE_ID_EXPR)
18099 /* If the DECLARATOR has the form `X<y>' then it uses one
18100 additional level of template parameters. */
18103 return cp_parser_check_template_parameters
18104 (parser, num_templates, declarator_location, declarator);
18110 case cdk_reference:
18112 return (cp_parser_check_declarator_template_parameters
18113 (parser, declarator->declarator, declarator_location));
18119 gcc_unreachable ();
18124 /* NUM_TEMPLATES were used in the current declaration. If that is
18125 invalid, return FALSE and issue an error messages. Otherwise,
18126 return TRUE. If DECLARATOR is non-NULL, then we are checking a
18127 declarator and we can print more accurate diagnostics. */
18130 cp_parser_check_template_parameters (cp_parser* parser,
18131 unsigned num_templates,
18132 location_t location,
18133 cp_declarator *declarator)
18135 /* If there are the same number of template classes and parameter
18136 lists, that's OK. */
18137 if (parser->num_template_parameter_lists == num_templates)
18139 /* If there are more, but only one more, then we are referring to a
18140 member template. That's OK too. */
18141 if (parser->num_template_parameter_lists == num_templates + 1)
18143 /* If there are more template classes than parameter lists, we have
18146 template <class T> void S<T>::R<T>::f (); */
18147 if (parser->num_template_parameter_lists < num_templates)
18150 error_at (location, "specializing member %<%T::%E%> "
18151 "requires %<template<>%> syntax",
18152 declarator->u.id.qualifying_scope,
18153 declarator->u.id.unqualified_name);
18155 error_at (location, "too few template-parameter-lists");
18158 /* Otherwise, there are too many template parameter lists. We have
18161 template <class T> template <class U> void S::f(); */
18162 error_at (location, "too many template-parameter-lists");
18166 /* Parse an optional `::' token indicating that the following name is
18167 from the global namespace. If so, PARSER->SCOPE is set to the
18168 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18169 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18170 Returns the new value of PARSER->SCOPE, if the `::' token is
18171 present, and NULL_TREE otherwise. */
18174 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18178 /* Peek at the next token. */
18179 token = cp_lexer_peek_token (parser->lexer);
18180 /* If we're looking at a `::' token then we're starting from the
18181 global namespace, not our current location. */
18182 if (token->type == CPP_SCOPE)
18184 /* Consume the `::' token. */
18185 cp_lexer_consume_token (parser->lexer);
18186 /* Set the SCOPE so that we know where to start the lookup. */
18187 parser->scope = global_namespace;
18188 parser->qualifying_scope = global_namespace;
18189 parser->object_scope = NULL_TREE;
18191 return parser->scope;
18193 else if (!current_scope_valid_p)
18195 parser->scope = NULL_TREE;
18196 parser->qualifying_scope = NULL_TREE;
18197 parser->object_scope = NULL_TREE;
18203 /* Returns TRUE if the upcoming token sequence is the start of a
18204 constructor declarator. If FRIEND_P is true, the declarator is
18205 preceded by the `friend' specifier. */
18208 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18210 bool constructor_p;
18211 tree type_decl = NULL_TREE;
18212 bool nested_name_p;
18213 cp_token *next_token;
18215 /* The common case is that this is not a constructor declarator, so
18216 try to avoid doing lots of work if at all possible. It's not
18217 valid declare a constructor at function scope. */
18218 if (parser->in_function_body)
18220 /* And only certain tokens can begin a constructor declarator. */
18221 next_token = cp_lexer_peek_token (parser->lexer);
18222 if (next_token->type != CPP_NAME
18223 && next_token->type != CPP_SCOPE
18224 && next_token->type != CPP_NESTED_NAME_SPECIFIER
18225 && next_token->type != CPP_TEMPLATE_ID)
18228 /* Parse tentatively; we are going to roll back all of the tokens
18230 cp_parser_parse_tentatively (parser);
18231 /* Assume that we are looking at a constructor declarator. */
18232 constructor_p = true;
18234 /* Look for the optional `::' operator. */
18235 cp_parser_global_scope_opt (parser,
18236 /*current_scope_valid_p=*/false);
18237 /* Look for the nested-name-specifier. */
18239 = (cp_parser_nested_name_specifier_opt (parser,
18240 /*typename_keyword_p=*/false,
18241 /*check_dependency_p=*/false,
18243 /*is_declaration=*/false)
18245 /* Outside of a class-specifier, there must be a
18246 nested-name-specifier. */
18247 if (!nested_name_p &&
18248 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18250 constructor_p = false;
18251 /* If we still think that this might be a constructor-declarator,
18252 look for a class-name. */
18257 template <typename T> struct S { S(); };
18258 template <typename T> S<T>::S ();
18260 we must recognize that the nested `S' names a class.
18263 template <typename T> S<T>::S<T> ();
18265 we must recognize that the nested `S' names a template. */
18266 type_decl = cp_parser_class_name (parser,
18267 /*typename_keyword_p=*/false,
18268 /*template_keyword_p=*/false,
18270 /*check_dependency_p=*/false,
18271 /*class_head_p=*/false,
18272 /*is_declaration=*/false);
18273 /* If there was no class-name, then this is not a constructor. */
18274 constructor_p = !cp_parser_error_occurred (parser);
18277 /* If we're still considering a constructor, we have to see a `(',
18278 to begin the parameter-declaration-clause, followed by either a
18279 `)', an `...', or a decl-specifier. We need to check for a
18280 type-specifier to avoid being fooled into thinking that:
18284 is a constructor. (It is actually a function named `f' that
18285 takes one parameter (of type `int') and returns a value of type
18288 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18291 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18292 /* A parameter declaration begins with a decl-specifier,
18293 which is either the "attribute" keyword, a storage class
18294 specifier, or (usually) a type-specifier. */
18295 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18298 tree pushed_scope = NULL_TREE;
18299 unsigned saved_num_template_parameter_lists;
18301 /* Names appearing in the type-specifier should be looked up
18302 in the scope of the class. */
18303 if (current_class_type)
18307 type = TREE_TYPE (type_decl);
18308 if (TREE_CODE (type) == TYPENAME_TYPE)
18310 type = resolve_typename_type (type,
18311 /*only_current_p=*/false);
18312 if (TREE_CODE (type) == TYPENAME_TYPE)
18314 cp_parser_abort_tentative_parse (parser);
18318 pushed_scope = push_scope (type);
18321 /* Inside the constructor parameter list, surrounding
18322 template-parameter-lists do not apply. */
18323 saved_num_template_parameter_lists
18324 = parser->num_template_parameter_lists;
18325 parser->num_template_parameter_lists = 0;
18327 /* Look for the type-specifier. */
18328 cp_parser_type_specifier (parser,
18329 CP_PARSER_FLAGS_NONE,
18330 /*decl_specs=*/NULL,
18331 /*is_declarator=*/true,
18332 /*declares_class_or_enum=*/NULL,
18333 /*is_cv_qualifier=*/NULL);
18335 parser->num_template_parameter_lists
18336 = saved_num_template_parameter_lists;
18338 /* Leave the scope of the class. */
18340 pop_scope (pushed_scope);
18342 constructor_p = !cp_parser_error_occurred (parser);
18346 constructor_p = false;
18347 /* We did not really want to consume any tokens. */
18348 cp_parser_abort_tentative_parse (parser);
18350 return constructor_p;
18353 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18354 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
18355 they must be performed once we are in the scope of the function.
18357 Returns the function defined. */
18360 cp_parser_function_definition_from_specifiers_and_declarator
18361 (cp_parser* parser,
18362 cp_decl_specifier_seq *decl_specifiers,
18364 const cp_declarator *declarator)
18369 /* Begin the function-definition. */
18370 success_p = start_function (decl_specifiers, declarator, attributes);
18372 /* The things we're about to see are not directly qualified by any
18373 template headers we've seen thus far. */
18374 reset_specialization ();
18376 /* If there were names looked up in the decl-specifier-seq that we
18377 did not check, check them now. We must wait until we are in the
18378 scope of the function to perform the checks, since the function
18379 might be a friend. */
18380 perform_deferred_access_checks ();
18384 /* Skip the entire function. */
18385 cp_parser_skip_to_end_of_block_or_statement (parser);
18386 fn = error_mark_node;
18388 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18390 /* Seen already, skip it. An error message has already been output. */
18391 cp_parser_skip_to_end_of_block_or_statement (parser);
18392 fn = current_function_decl;
18393 current_function_decl = NULL_TREE;
18394 /* If this is a function from a class, pop the nested class. */
18395 if (current_class_name)
18396 pop_nested_class ();
18399 fn = cp_parser_function_definition_after_declarator (parser,
18400 /*inline_p=*/false);
18405 /* Parse the part of a function-definition that follows the
18406 declarator. INLINE_P is TRUE iff this function is an inline
18407 function defined within a class-specifier.
18409 Returns the function defined. */
18412 cp_parser_function_definition_after_declarator (cp_parser* parser,
18416 bool ctor_initializer_p = false;
18417 bool saved_in_unbraced_linkage_specification_p;
18418 bool saved_in_function_body;
18419 unsigned saved_num_template_parameter_lists;
18422 saved_in_function_body = parser->in_function_body;
18423 parser->in_function_body = true;
18424 /* If the next token is `return', then the code may be trying to
18425 make use of the "named return value" extension that G++ used to
18427 token = cp_lexer_peek_token (parser->lexer);
18428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18430 /* Consume the `return' keyword. */
18431 cp_lexer_consume_token (parser->lexer);
18432 /* Look for the identifier that indicates what value is to be
18434 cp_parser_identifier (parser);
18435 /* Issue an error message. */
18436 error_at (token->location,
18437 "named return values are no longer supported");
18438 /* Skip tokens until we reach the start of the function body. */
18441 cp_token *token = cp_lexer_peek_token (parser->lexer);
18442 if (token->type == CPP_OPEN_BRACE
18443 || token->type == CPP_EOF
18444 || token->type == CPP_PRAGMA_EOL)
18446 cp_lexer_consume_token (parser->lexer);
18449 /* The `extern' in `extern "C" void f () { ... }' does not apply to
18450 anything declared inside `f'. */
18451 saved_in_unbraced_linkage_specification_p
18452 = parser->in_unbraced_linkage_specification_p;
18453 parser->in_unbraced_linkage_specification_p = false;
18454 /* Inside the function, surrounding template-parameter-lists do not
18456 saved_num_template_parameter_lists
18457 = parser->num_template_parameter_lists;
18458 parser->num_template_parameter_lists = 0;
18460 start_lambda_scope (current_function_decl);
18462 /* If the next token is `try', then we are looking at a
18463 function-try-block. */
18464 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18465 ctor_initializer_p = cp_parser_function_try_block (parser);
18466 /* A function-try-block includes the function-body, so we only do
18467 this next part if we're not processing a function-try-block. */
18470 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18472 finish_lambda_scope ();
18474 /* Finish the function. */
18475 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18476 (inline_p ? 2 : 0));
18477 /* Generate code for it, if necessary. */
18478 expand_or_defer_fn (fn);
18479 /* Restore the saved values. */
18480 parser->in_unbraced_linkage_specification_p
18481 = saved_in_unbraced_linkage_specification_p;
18482 parser->num_template_parameter_lists
18483 = saved_num_template_parameter_lists;
18484 parser->in_function_body = saved_in_function_body;
18489 /* Parse a template-declaration, assuming that the `export' (and
18490 `extern') keywords, if present, has already been scanned. MEMBER_P
18491 is as for cp_parser_template_declaration. */
18494 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18496 tree decl = NULL_TREE;
18497 VEC (deferred_access_check,gc) *checks;
18498 tree parameter_list;
18499 bool friend_p = false;
18500 bool need_lang_pop;
18503 /* Look for the `template' keyword. */
18504 token = cp_lexer_peek_token (parser->lexer);
18505 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18509 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18511 if (at_class_scope_p () && current_function_decl)
18513 /* 14.5.2.2 [temp.mem]
18515 A local class shall not have member templates. */
18516 error_at (token->location,
18517 "invalid declaration of member template in local class");
18518 cp_parser_skip_to_end_of_block_or_statement (parser);
18523 A template ... shall not have C linkage. */
18524 if (current_lang_name == lang_name_c)
18526 error_at (token->location, "template with C linkage");
18527 /* Give it C++ linkage to avoid confusing other parts of the
18529 push_lang_context (lang_name_cplusplus);
18530 need_lang_pop = true;
18533 need_lang_pop = false;
18535 /* We cannot perform access checks on the template parameter
18536 declarations until we know what is being declared, just as we
18537 cannot check the decl-specifier list. */
18538 push_deferring_access_checks (dk_deferred);
18540 /* If the next token is `>', then we have an invalid
18541 specialization. Rather than complain about an invalid template
18542 parameter, issue an error message here. */
18543 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18545 cp_parser_error (parser, "invalid explicit specialization");
18546 begin_specialization ();
18547 parameter_list = NULL_TREE;
18550 /* Parse the template parameters. */
18551 parameter_list = cp_parser_template_parameter_list (parser);
18553 /* Get the deferred access checks from the parameter list. These
18554 will be checked once we know what is being declared, as for a
18555 member template the checks must be performed in the scope of the
18556 class containing the member. */
18557 checks = get_deferred_access_checks ();
18559 /* Look for the `>'. */
18560 cp_parser_skip_to_end_of_template_parameter_list (parser);
18561 /* We just processed one more parameter list. */
18562 ++parser->num_template_parameter_lists;
18563 /* If the next token is `template', there are more template
18565 if (cp_lexer_next_token_is_keyword (parser->lexer,
18567 cp_parser_template_declaration_after_export (parser, member_p);
18570 /* There are no access checks when parsing a template, as we do not
18571 know if a specialization will be a friend. */
18572 push_deferring_access_checks (dk_no_check);
18573 token = cp_lexer_peek_token (parser->lexer);
18574 decl = cp_parser_single_declaration (parser,
18577 /*explicit_specialization_p=*/false,
18579 pop_deferring_access_checks ();
18581 /* If this is a member template declaration, let the front
18583 if (member_p && !friend_p && decl)
18585 if (TREE_CODE (decl) == TYPE_DECL)
18586 cp_parser_check_access_in_redeclaration (decl, token->location);
18588 decl = finish_member_template_decl (decl);
18590 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18591 make_friend_class (current_class_type, TREE_TYPE (decl),
18592 /*complain=*/true);
18594 /* We are done with the current parameter list. */
18595 --parser->num_template_parameter_lists;
18597 pop_deferring_access_checks ();
18600 finish_template_decl (parameter_list);
18602 /* Register member declarations. */
18603 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18604 finish_member_declaration (decl);
18605 /* For the erroneous case of a template with C linkage, we pushed an
18606 implicit C++ linkage scope; exit that scope now. */
18608 pop_lang_context ();
18609 /* If DECL is a function template, we must return to parse it later.
18610 (Even though there is no definition, there might be default
18611 arguments that need handling.) */
18612 if (member_p && decl
18613 && (TREE_CODE (decl) == FUNCTION_DECL
18614 || DECL_FUNCTION_TEMPLATE_P (decl)))
18615 TREE_VALUE (parser->unparsed_functions_queues)
18616 = tree_cons (NULL_TREE, decl,
18617 TREE_VALUE (parser->unparsed_functions_queues));
18620 /* Perform the deferred access checks from a template-parameter-list.
18621 CHECKS is a TREE_LIST of access checks, as returned by
18622 get_deferred_access_checks. */
18625 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18627 ++processing_template_parmlist;
18628 perform_access_checks (checks);
18629 --processing_template_parmlist;
18632 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18633 `function-definition' sequence. MEMBER_P is true, this declaration
18634 appears in a class scope.
18636 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
18637 *FRIEND_P is set to TRUE iff the declaration is a friend. */
18640 cp_parser_single_declaration (cp_parser* parser,
18641 VEC (deferred_access_check,gc)* checks,
18643 bool explicit_specialization_p,
18646 int declares_class_or_enum;
18647 tree decl = NULL_TREE;
18648 cp_decl_specifier_seq decl_specifiers;
18649 bool function_definition_p = false;
18650 cp_token *decl_spec_token_start;
18652 /* This function is only used when processing a template
18654 gcc_assert (innermost_scope_kind () == sk_template_parms
18655 || innermost_scope_kind () == sk_template_spec);
18657 /* Defer access checks until we know what is being declared. */
18658 push_deferring_access_checks (dk_deferred);
18660 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18662 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18663 cp_parser_decl_specifier_seq (parser,
18664 CP_PARSER_FLAGS_OPTIONAL,
18666 &declares_class_or_enum);
18668 *friend_p = cp_parser_friend_p (&decl_specifiers);
18670 /* There are no template typedefs. */
18671 if (decl_specifiers.specs[(int) ds_typedef])
18673 error_at (decl_spec_token_start->location,
18674 "template declaration of %<typedef%>");
18675 decl = error_mark_node;
18678 /* Gather up the access checks that occurred the
18679 decl-specifier-seq. */
18680 stop_deferring_access_checks ();
18682 /* Check for the declaration of a template class. */
18683 if (declares_class_or_enum)
18685 if (cp_parser_declares_only_class_p (parser))
18687 decl = shadow_tag (&decl_specifiers);
18692 friend template <typename T> struct A<T>::B;
18695 A<T>::B will be represented by a TYPENAME_TYPE, and
18696 therefore not recognized by shadow_tag. */
18697 if (friend_p && *friend_p
18699 && decl_specifiers.type
18700 && TYPE_P (decl_specifiers.type))
18701 decl = decl_specifiers.type;
18703 if (decl && decl != error_mark_node)
18704 decl = TYPE_NAME (decl);
18706 decl = error_mark_node;
18708 /* Perform access checks for template parameters. */
18709 cp_parser_perform_template_parameter_access_checks (checks);
18712 /* If it's not a template class, try for a template function. If
18713 the next token is a `;', then this declaration does not declare
18714 anything. But, if there were errors in the decl-specifiers, then
18715 the error might well have come from an attempted class-specifier.
18716 In that case, there's no need to warn about a missing declarator. */
18718 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18719 || decl_specifiers.type != error_mark_node))
18721 decl = cp_parser_init_declarator (parser,
18724 /*function_definition_allowed_p=*/true,
18726 declares_class_or_enum,
18727 &function_definition_p);
18729 /* 7.1.1-1 [dcl.stc]
18731 A storage-class-specifier shall not be specified in an explicit
18732 specialization... */
18734 && explicit_specialization_p
18735 && decl_specifiers.storage_class != sc_none)
18737 error_at (decl_spec_token_start->location,
18738 "explicit template specialization cannot have a storage class");
18739 decl = error_mark_node;
18743 pop_deferring_access_checks ();
18745 /* Clear any current qualification; whatever comes next is the start
18746 of something new. */
18747 parser->scope = NULL_TREE;
18748 parser->qualifying_scope = NULL_TREE;
18749 parser->object_scope = NULL_TREE;
18750 /* Look for a trailing `;' after the declaration. */
18751 if (!function_definition_p
18752 && (decl == error_mark_node
18753 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18754 cp_parser_skip_to_end_of_block_or_statement (parser);
18759 /* Parse a cast-expression that is not the operand of a unary "&". */
18762 cp_parser_simple_cast_expression (cp_parser *parser)
18764 return cp_parser_cast_expression (parser, /*address_p=*/false,
18765 /*cast_p=*/false, NULL);
18768 /* Parse a functional cast to TYPE. Returns an expression
18769 representing the cast. */
18772 cp_parser_functional_cast (cp_parser* parser, tree type)
18775 tree expression_list;
18779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18781 maybe_warn_cpp0x ("extended initializer lists");
18782 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18783 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18784 if (TREE_CODE (type) == TYPE_DECL)
18785 type = TREE_TYPE (type);
18786 return finish_compound_literal (type, expression_list);
18790 vec = cp_parser_parenthesized_expression_list (parser, false,
18792 /*allow_expansion_p=*/true,
18793 /*non_constant_p=*/NULL);
18795 expression_list = error_mark_node;
18798 expression_list = build_tree_list_vec (vec);
18799 release_tree_vector (vec);
18802 cast = build_functional_cast (type, expression_list,
18803 tf_warning_or_error);
18804 /* [expr.const]/1: In an integral constant expression "only type
18805 conversions to integral or enumeration type can be used". */
18806 if (TREE_CODE (type) == TYPE_DECL)
18807 type = TREE_TYPE (type);
18808 if (cast != error_mark_node
18809 && !cast_valid_in_integral_constant_expression_p (type)
18810 && (cp_parser_non_integral_constant_expression
18811 (parser, "a call to a constructor")))
18812 return error_mark_node;
18816 /* Save the tokens that make up the body of a member function defined
18817 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18818 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18819 specifiers applied to the declaration. Returns the FUNCTION_DECL
18820 for the member function. */
18823 cp_parser_save_member_function_body (cp_parser* parser,
18824 cp_decl_specifier_seq *decl_specifiers,
18825 cp_declarator *declarator,
18832 /* Create the FUNCTION_DECL. */
18833 fn = grokmethod (decl_specifiers, declarator, attributes);
18834 /* If something went badly wrong, bail out now. */
18835 if (fn == error_mark_node)
18837 /* If there's a function-body, skip it. */
18838 if (cp_parser_token_starts_function_definition_p
18839 (cp_lexer_peek_token (parser->lexer)))
18840 cp_parser_skip_to_end_of_block_or_statement (parser);
18841 return error_mark_node;
18844 /* Remember it, if there default args to post process. */
18845 cp_parser_save_default_args (parser, fn);
18847 /* Save away the tokens that make up the body of the
18849 first = parser->lexer->next_token;
18850 /* We can have braced-init-list mem-initializers before the fn body. */
18851 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18853 cp_lexer_consume_token (parser->lexer);
18854 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18855 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18857 /* cache_group will stop after an un-nested { } pair, too. */
18858 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18861 /* variadic mem-inits have ... after the ')'. */
18862 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18863 cp_lexer_consume_token (parser->lexer);
18866 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18867 /* Handle function try blocks. */
18868 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18869 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18870 last = parser->lexer->next_token;
18872 /* Save away the inline definition; we will process it when the
18873 class is complete. */
18874 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18875 DECL_PENDING_INLINE_P (fn) = 1;
18877 /* We need to know that this was defined in the class, so that
18878 friend templates are handled correctly. */
18879 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18881 /* Add FN to the queue of functions to be parsed later. */
18882 TREE_VALUE (parser->unparsed_functions_queues)
18883 = tree_cons (NULL_TREE, fn,
18884 TREE_VALUE (parser->unparsed_functions_queues));
18889 /* Parse a template-argument-list, as well as the trailing ">" (but
18890 not the opening ">"). See cp_parser_template_argument_list for the
18894 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18898 tree saved_qualifying_scope;
18899 tree saved_object_scope;
18900 bool saved_greater_than_is_operator_p;
18901 int saved_unevaluated_operand;
18902 int saved_inhibit_evaluation_warnings;
18906 When parsing a template-id, the first non-nested `>' is taken as
18907 the end of the template-argument-list rather than a greater-than
18909 saved_greater_than_is_operator_p
18910 = parser->greater_than_is_operator_p;
18911 parser->greater_than_is_operator_p = false;
18912 /* Parsing the argument list may modify SCOPE, so we save it
18914 saved_scope = parser->scope;
18915 saved_qualifying_scope = parser->qualifying_scope;
18916 saved_object_scope = parser->object_scope;
18917 /* We need to evaluate the template arguments, even though this
18918 template-id may be nested within a "sizeof". */
18919 saved_unevaluated_operand = cp_unevaluated_operand;
18920 cp_unevaluated_operand = 0;
18921 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18922 c_inhibit_evaluation_warnings = 0;
18923 /* Parse the template-argument-list itself. */
18924 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18925 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18926 arguments = NULL_TREE;
18928 arguments = cp_parser_template_argument_list (parser);
18929 /* Look for the `>' that ends the template-argument-list. If we find
18930 a '>>' instead, it's probably just a typo. */
18931 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18933 if (cxx_dialect != cxx98)
18935 /* In C++0x, a `>>' in a template argument list or cast
18936 expression is considered to be two separate `>'
18937 tokens. So, change the current token to a `>', but don't
18938 consume it: it will be consumed later when the outer
18939 template argument list (or cast expression) is parsed.
18940 Note that this replacement of `>' for `>>' is necessary
18941 even if we are parsing tentatively: in the tentative
18942 case, after calling
18943 cp_parser_enclosed_template_argument_list we will always
18944 throw away all of the template arguments and the first
18945 closing `>', either because the template argument list
18946 was erroneous or because we are replacing those tokens
18947 with a CPP_TEMPLATE_ID token. The second `>' (which will
18948 not have been thrown away) is needed either to close an
18949 outer template argument list or to complete a new-style
18951 cp_token *token = cp_lexer_peek_token (parser->lexer);
18952 token->type = CPP_GREATER;
18954 else if (!saved_greater_than_is_operator_p)
18956 /* If we're in a nested template argument list, the '>>' has
18957 to be a typo for '> >'. We emit the error message, but we
18958 continue parsing and we push a '>' as next token, so that
18959 the argument list will be parsed correctly. Note that the
18960 global source location is still on the token before the
18961 '>>', so we need to say explicitly where we want it. */
18962 cp_token *token = cp_lexer_peek_token (parser->lexer);
18963 error_at (token->location, "%<>>%> should be %<> >%> "
18964 "within a nested template argument list");
18966 token->type = CPP_GREATER;
18970 /* If this is not a nested template argument list, the '>>'
18971 is a typo for '>'. Emit an error message and continue.
18972 Same deal about the token location, but here we can get it
18973 right by consuming the '>>' before issuing the diagnostic. */
18974 cp_token *token = cp_lexer_consume_token (parser->lexer);
18975 error_at (token->location,
18976 "spurious %<>>%>, use %<>%> to terminate "
18977 "a template argument list");
18981 cp_parser_skip_to_end_of_template_parameter_list (parser);
18982 /* The `>' token might be a greater-than operator again now. */
18983 parser->greater_than_is_operator_p
18984 = saved_greater_than_is_operator_p;
18985 /* Restore the SAVED_SCOPE. */
18986 parser->scope = saved_scope;
18987 parser->qualifying_scope = saved_qualifying_scope;
18988 parser->object_scope = saved_object_scope;
18989 cp_unevaluated_operand = saved_unevaluated_operand;
18990 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18995 /* MEMBER_FUNCTION is a member function, or a friend. If default
18996 arguments, or the body of the function have not yet been parsed,
19000 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19002 /* If this member is a template, get the underlying
19004 if (DECL_FUNCTION_TEMPLATE_P (member_function))
19005 member_function = DECL_TEMPLATE_RESULT (member_function);
19007 /* There should not be any class definitions in progress at this
19008 point; the bodies of members are only parsed outside of all class
19010 gcc_assert (parser->num_classes_being_defined == 0);
19011 /* While we're parsing the member functions we might encounter more
19012 classes. We want to handle them right away, but we don't want
19013 them getting mixed up with functions that are currently in the
19015 parser->unparsed_functions_queues
19016 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19018 /* Make sure that any template parameters are in scope. */
19019 maybe_begin_member_template_processing (member_function);
19021 /* If the body of the function has not yet been parsed, parse it
19023 if (DECL_PENDING_INLINE_P (member_function))
19025 tree function_scope;
19026 cp_token_cache *tokens;
19028 /* The function is no longer pending; we are processing it. */
19029 tokens = DECL_PENDING_INLINE_INFO (member_function);
19030 DECL_PENDING_INLINE_INFO (member_function) = NULL;
19031 DECL_PENDING_INLINE_P (member_function) = 0;
19033 /* If this is a local class, enter the scope of the containing
19035 function_scope = current_function_decl;
19036 if (function_scope)
19037 push_function_context ();
19039 /* Push the body of the function onto the lexer stack. */
19040 cp_parser_push_lexer_for_tokens (parser, tokens);
19042 /* Let the front end know that we going to be defining this
19044 start_preparsed_function (member_function, NULL_TREE,
19045 SF_PRE_PARSED | SF_INCLASS_INLINE);
19047 /* Don't do access checking if it is a templated function. */
19048 if (processing_template_decl)
19049 push_deferring_access_checks (dk_no_check);
19051 /* Now, parse the body of the function. */
19052 cp_parser_function_definition_after_declarator (parser,
19053 /*inline_p=*/true);
19055 if (processing_template_decl)
19056 pop_deferring_access_checks ();
19058 /* Leave the scope of the containing function. */
19059 if (function_scope)
19060 pop_function_context ();
19061 cp_parser_pop_lexer (parser);
19064 /* Remove any template parameters from the symbol table. */
19065 maybe_end_member_template_processing ();
19067 /* Restore the queue. */
19068 parser->unparsed_functions_queues
19069 = TREE_CHAIN (parser->unparsed_functions_queues);
19072 /* If DECL contains any default args, remember it on the unparsed
19073 functions queue. */
19076 cp_parser_save_default_args (cp_parser* parser, tree decl)
19080 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19082 probe = TREE_CHAIN (probe))
19083 if (TREE_PURPOSE (probe))
19085 TREE_PURPOSE (parser->unparsed_functions_queues)
19086 = tree_cons (current_class_type, decl,
19087 TREE_PURPOSE (parser->unparsed_functions_queues));
19092 /* FN is a FUNCTION_DECL which may contains a parameter with an
19093 unparsed DEFAULT_ARG. Parse the default args now. This function
19094 assumes that the current scope is the scope in which the default
19095 argument should be processed. */
19098 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19100 bool saved_local_variables_forbidden_p;
19101 tree parm, parmdecl;
19103 /* While we're parsing the default args, we might (due to the
19104 statement expression extension) encounter more classes. We want
19105 to handle them right away, but we don't want them getting mixed
19106 up with default args that are currently in the queue. */
19107 parser->unparsed_functions_queues
19108 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19110 /* Local variable names (and the `this' keyword) may not appear
19111 in a default argument. */
19112 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19113 parser->local_variables_forbidden_p = true;
19115 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19116 parmdecl = DECL_ARGUMENTS (fn);
19117 parm && parm != void_list_node;
19118 parm = TREE_CHAIN (parm),
19119 parmdecl = TREE_CHAIN (parmdecl))
19121 cp_token_cache *tokens;
19122 tree default_arg = TREE_PURPOSE (parm);
19124 VEC(tree,gc) *insts;
19131 if (TREE_CODE (default_arg) != DEFAULT_ARG)
19132 /* This can happen for a friend declaration for a function
19133 already declared with default arguments. */
19136 /* Push the saved tokens for the default argument onto the parser's
19138 tokens = DEFARG_TOKENS (default_arg);
19139 cp_parser_push_lexer_for_tokens (parser, tokens);
19141 start_lambda_scope (parmdecl);
19143 /* Parse the assignment-expression. */
19144 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19145 if (parsed_arg == error_mark_node)
19147 cp_parser_pop_lexer (parser);
19151 if (!processing_template_decl)
19152 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19154 TREE_PURPOSE (parm) = parsed_arg;
19156 /* Update any instantiations we've already created. */
19157 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19158 VEC_iterate (tree, insts, ix, copy); ix++)
19159 TREE_PURPOSE (copy) = parsed_arg;
19161 finish_lambda_scope ();
19163 /* If the token stream has not been completely used up, then
19164 there was extra junk after the end of the default
19166 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19167 cp_parser_error (parser, "expected %<,%>");
19169 /* Revert to the main lexer. */
19170 cp_parser_pop_lexer (parser);
19173 /* Make sure no default arg is missing. */
19174 check_default_args (fn);
19176 /* Restore the state of local_variables_forbidden_p. */
19177 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19179 /* Restore the queue. */
19180 parser->unparsed_functions_queues
19181 = TREE_CHAIN (parser->unparsed_functions_queues);
19184 /* Parse the operand of `sizeof' (or a similar operator). Returns
19185 either a TYPE or an expression, depending on the form of the
19186 input. The KEYWORD indicates which kind of expression we have
19190 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19192 tree expr = NULL_TREE;
19193 const char *saved_message;
19195 bool saved_integral_constant_expression_p;
19196 bool saved_non_integral_constant_expression_p;
19197 bool pack_expansion_p = false;
19199 /* Types cannot be defined in a `sizeof' expression. Save away the
19201 saved_message = parser->type_definition_forbidden_message;
19202 /* And create the new one. */
19203 tmp = concat ("types may not be defined in %<",
19204 IDENTIFIER_POINTER (ridpointers[keyword]),
19205 "%> expressions", NULL);
19206 parser->type_definition_forbidden_message = tmp;
19208 /* The restrictions on constant-expressions do not apply inside
19209 sizeof expressions. */
19210 saved_integral_constant_expression_p
19211 = parser->integral_constant_expression_p;
19212 saved_non_integral_constant_expression_p
19213 = parser->non_integral_constant_expression_p;
19214 parser->integral_constant_expression_p = false;
19216 /* If it's a `...', then we are computing the length of a parameter
19218 if (keyword == RID_SIZEOF
19219 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19221 /* Consume the `...'. */
19222 cp_lexer_consume_token (parser->lexer);
19223 maybe_warn_variadic_templates ();
19225 /* Note that this is an expansion. */
19226 pack_expansion_p = true;
19229 /* Do not actually evaluate the expression. */
19230 ++cp_unevaluated_operand;
19231 ++c_inhibit_evaluation_warnings;
19232 /* If it's a `(', then we might be looking at the type-id
19234 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19237 bool saved_in_type_id_in_expr_p;
19239 /* We can't be sure yet whether we're looking at a type-id or an
19241 cp_parser_parse_tentatively (parser);
19242 /* Consume the `('. */
19243 cp_lexer_consume_token (parser->lexer);
19244 /* Parse the type-id. */
19245 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19246 parser->in_type_id_in_expr_p = true;
19247 type = cp_parser_type_id (parser);
19248 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19249 /* Now, look for the trailing `)'. */
19250 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19251 /* If all went well, then we're done. */
19252 if (cp_parser_parse_definitely (parser))
19254 cp_decl_specifier_seq decl_specs;
19256 /* Build a trivial decl-specifier-seq. */
19257 clear_decl_specs (&decl_specs);
19258 decl_specs.type = type;
19260 /* Call grokdeclarator to figure out what type this is. */
19261 expr = grokdeclarator (NULL,
19265 /*attrlist=*/NULL);
19269 /* If the type-id production did not work out, then we must be
19270 looking at the unary-expression production. */
19272 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19273 /*cast_p=*/false, NULL);
19275 if (pack_expansion_p)
19276 /* Build a pack expansion. */
19277 expr = make_pack_expansion (expr);
19279 /* Go back to evaluating expressions. */
19280 --cp_unevaluated_operand;
19281 --c_inhibit_evaluation_warnings;
19283 /* Free the message we created. */
19285 /* And restore the old one. */
19286 parser->type_definition_forbidden_message = saved_message;
19287 parser->integral_constant_expression_p
19288 = saved_integral_constant_expression_p;
19289 parser->non_integral_constant_expression_p
19290 = saved_non_integral_constant_expression_p;
19295 /* If the current declaration has no declarator, return true. */
19298 cp_parser_declares_only_class_p (cp_parser *parser)
19300 /* If the next token is a `;' or a `,' then there is no
19302 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19303 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19306 /* Update the DECL_SPECS to reflect the storage class indicated by
19310 cp_parser_set_storage_class (cp_parser *parser,
19311 cp_decl_specifier_seq *decl_specs,
19313 location_t location)
19315 cp_storage_class storage_class;
19317 if (parser->in_unbraced_linkage_specification_p)
19319 error_at (location, "invalid use of %qD in linkage specification",
19320 ridpointers[keyword]);
19323 else if (decl_specs->storage_class != sc_none)
19325 decl_specs->conflicting_specifiers_p = true;
19329 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19330 && decl_specs->specs[(int) ds_thread])
19332 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19333 decl_specs->specs[(int) ds_thread] = 0;
19339 storage_class = sc_auto;
19342 storage_class = sc_register;
19345 storage_class = sc_static;
19348 storage_class = sc_extern;
19351 storage_class = sc_mutable;
19354 gcc_unreachable ();
19356 decl_specs->storage_class = storage_class;
19358 /* A storage class specifier cannot be applied alongside a typedef
19359 specifier. If there is a typedef specifier present then set
19360 conflicting_specifiers_p which will trigger an error later
19361 on in grokdeclarator. */
19362 if (decl_specs->specs[(int)ds_typedef])
19363 decl_specs->conflicting_specifiers_p = true;
19366 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
19367 is true, the type is a user-defined type; otherwise it is a
19368 built-in type specified by a keyword. */
19371 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19373 location_t location,
19374 bool user_defined_p)
19376 decl_specs->any_specifiers_p = true;
19378 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19379 (with, for example, in "typedef int wchar_t;") we remember that
19380 this is what happened. In system headers, we ignore these
19381 declarations so that G++ can work with system headers that are not
19383 if (decl_specs->specs[(int) ds_typedef]
19385 && (type_spec == boolean_type_node
19386 || type_spec == char16_type_node
19387 || type_spec == char32_type_node
19388 || type_spec == wchar_type_node)
19389 && (decl_specs->type
19390 || decl_specs->specs[(int) ds_long]
19391 || decl_specs->specs[(int) ds_short]
19392 || decl_specs->specs[(int) ds_unsigned]
19393 || decl_specs->specs[(int) ds_signed]))
19395 decl_specs->redefined_builtin_type = type_spec;
19396 if (!decl_specs->type)
19398 decl_specs->type = type_spec;
19399 decl_specs->user_defined_type_p = false;
19400 decl_specs->type_location = location;
19403 else if (decl_specs->type)
19404 decl_specs->multiple_types_p = true;
19407 decl_specs->type = type_spec;
19408 decl_specs->user_defined_type_p = user_defined_p;
19409 decl_specs->redefined_builtin_type = NULL_TREE;
19410 decl_specs->type_location = location;
19414 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19415 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
19418 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19420 return decl_specifiers->specs[(int) ds_friend] != 0;
19423 /* If the next token is of the indicated TYPE, consume it. Otherwise,
19424 issue an error message indicating that TOKEN_DESC was expected.
19426 Returns the token consumed, if the token had the appropriate type.
19427 Otherwise, returns NULL. */
19430 cp_parser_require (cp_parser* parser,
19431 enum cpp_ttype type,
19432 const char* token_desc)
19434 if (cp_lexer_next_token_is (parser->lexer, type))
19435 return cp_lexer_consume_token (parser->lexer);
19438 /* Output the MESSAGE -- unless we're parsing tentatively. */
19439 if (!cp_parser_simulate_error (parser))
19441 char *message = concat ("expected ", token_desc, NULL);
19442 cp_parser_error (parser, message);
19449 /* An error message is produced if the next token is not '>'.
19450 All further tokens are skipped until the desired token is
19451 found or '{', '}', ';' or an unbalanced ')' or ']'. */
19454 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19456 /* Current level of '< ... >'. */
19457 unsigned level = 0;
19458 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
19459 unsigned nesting_depth = 0;
19461 /* Are we ready, yet? If not, issue error message. */
19462 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19465 /* Skip tokens until the desired token is found. */
19468 /* Peek at the next token. */
19469 switch (cp_lexer_peek_token (parser->lexer)->type)
19472 if (!nesting_depth)
19477 if (cxx_dialect == cxx98)
19478 /* C++0x views the `>>' operator as two `>' tokens, but
19481 else if (!nesting_depth && level-- == 0)
19483 /* We've hit a `>>' where the first `>' closes the
19484 template argument list, and the second `>' is
19485 spurious. Just consume the `>>' and stop; we've
19486 already produced at least one error. */
19487 cp_lexer_consume_token (parser->lexer);
19490 /* Fall through for C++0x, so we handle the second `>' in
19494 if (!nesting_depth && level-- == 0)
19496 /* We've reached the token we want, consume it and stop. */
19497 cp_lexer_consume_token (parser->lexer);
19502 case CPP_OPEN_PAREN:
19503 case CPP_OPEN_SQUARE:
19507 case CPP_CLOSE_PAREN:
19508 case CPP_CLOSE_SQUARE:
19509 if (nesting_depth-- == 0)
19514 case CPP_PRAGMA_EOL:
19515 case CPP_SEMICOLON:
19516 case CPP_OPEN_BRACE:
19517 case CPP_CLOSE_BRACE:
19518 /* The '>' was probably forgotten, don't look further. */
19525 /* Consume this token. */
19526 cp_lexer_consume_token (parser->lexer);
19530 /* If the next token is the indicated keyword, consume it. Otherwise,
19531 issue an error message indicating that TOKEN_DESC was expected.
19533 Returns the token consumed, if the token had the appropriate type.
19534 Otherwise, returns NULL. */
19537 cp_parser_require_keyword (cp_parser* parser,
19539 const char* token_desc)
19541 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19543 if (token && token->keyword != keyword)
19545 dyn_string_t error_msg;
19547 /* Format the error message. */
19548 error_msg = dyn_string_new (0);
19549 dyn_string_append_cstr (error_msg, "expected ");
19550 dyn_string_append_cstr (error_msg, token_desc);
19551 cp_parser_error (parser, error_msg->s);
19552 dyn_string_delete (error_msg);
19559 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19560 function-definition. */
19563 cp_parser_token_starts_function_definition_p (cp_token* token)
19565 return (/* An ordinary function-body begins with an `{'. */
19566 token->type == CPP_OPEN_BRACE
19567 /* A ctor-initializer begins with a `:'. */
19568 || token->type == CPP_COLON
19569 /* A function-try-block begins with `try'. */
19570 || token->keyword == RID_TRY
19571 /* The named return value extension begins with `return'. */
19572 || token->keyword == RID_RETURN);
19575 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19579 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19583 token = cp_lexer_peek_token (parser->lexer);
19584 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19587 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19588 C++0x) ending a template-argument. */
19591 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19595 token = cp_lexer_peek_token (parser->lexer);
19596 return (token->type == CPP_COMMA
19597 || token->type == CPP_GREATER
19598 || token->type == CPP_ELLIPSIS
19599 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19602 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19603 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
19606 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19611 token = cp_lexer_peek_nth_token (parser->lexer, n);
19612 if (token->type == CPP_LESS)
19614 /* Check for the sequence `<::' in the original code. It would be lexed as
19615 `[:', where `[' is a digraph, and there is no whitespace before
19617 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19620 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19621 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19627 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19628 or none_type otherwise. */
19630 static enum tag_types
19631 cp_parser_token_is_class_key (cp_token* token)
19633 switch (token->keyword)
19638 return record_type;
19647 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
19650 cp_parser_check_class_key (enum tag_types class_key, tree type)
19652 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19653 permerror (input_location, "%qs tag used in naming %q#T",
19654 class_key == union_type ? "union"
19655 : class_key == record_type ? "struct" : "class",
19659 /* Issue an error message if DECL is redeclared with different
19660 access than its original declaration [class.access.spec/3].
19661 This applies to nested classes and nested class templates.
19665 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19667 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19670 if ((TREE_PRIVATE (decl)
19671 != (current_access_specifier == access_private_node))
19672 || (TREE_PROTECTED (decl)
19673 != (current_access_specifier == access_protected_node)))
19674 error_at (location, "%qD redeclared with different access", decl);
19677 /* Look for the `template' keyword, as a syntactic disambiguator.
19678 Return TRUE iff it is present, in which case it will be
19682 cp_parser_optional_template_keyword (cp_parser *parser)
19684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19686 /* The `template' keyword can only be used within templates;
19687 outside templates the parser can always figure out what is a
19688 template and what is not. */
19689 if (!processing_template_decl)
19691 cp_token *token = cp_lexer_peek_token (parser->lexer);
19692 error_at (token->location,
19693 "%<template%> (as a disambiguator) is only allowed "
19694 "within templates");
19695 /* If this part of the token stream is rescanned, the same
19696 error message would be generated. So, we purge the token
19697 from the stream. */
19698 cp_lexer_purge_token (parser->lexer);
19703 /* Consume the `template' keyword. */
19704 cp_lexer_consume_token (parser->lexer);
19712 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19713 set PARSER->SCOPE, and perform other related actions. */
19716 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19719 struct tree_check *check_value;
19720 deferred_access_check *chk;
19721 VEC (deferred_access_check,gc) *checks;
19723 /* Get the stored value. */
19724 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19725 /* Perform any access checks that were deferred. */
19726 checks = check_value->checks;
19730 VEC_iterate (deferred_access_check, checks, i, chk) ;
19733 perform_or_defer_access_check (chk->binfo,
19738 /* Set the scope from the stored value. */
19739 parser->scope = check_value->value;
19740 parser->qualifying_scope = check_value->qualifying_scope;
19741 parser->object_scope = NULL_TREE;
19744 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19745 encounter the end of a block before what we were looking for. */
19748 cp_parser_cache_group (cp_parser *parser,
19749 enum cpp_ttype end,
19754 cp_token *token = cp_lexer_peek_token (parser->lexer);
19756 /* Abort a parenthesized expression if we encounter a semicolon. */
19757 if ((end == CPP_CLOSE_PAREN || depth == 0)
19758 && token->type == CPP_SEMICOLON)
19760 /* If we've reached the end of the file, stop. */
19761 if (token->type == CPP_EOF
19762 || (end != CPP_PRAGMA_EOL
19763 && token->type == CPP_PRAGMA_EOL))
19765 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19766 /* We've hit the end of an enclosing block, so there's been some
19767 kind of syntax error. */
19770 /* Consume the token. */
19771 cp_lexer_consume_token (parser->lexer);
19772 /* See if it starts a new group. */
19773 if (token->type == CPP_OPEN_BRACE)
19775 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19776 /* In theory this should probably check end == '}', but
19777 cp_parser_save_member_function_body needs it to exit
19778 after either '}' or ')' when called with ')'. */
19782 else if (token->type == CPP_OPEN_PAREN)
19784 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19785 if (depth == 0 && end == CPP_CLOSE_PAREN)
19788 else if (token->type == CPP_PRAGMA)
19789 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19790 else if (token->type == end)
19795 /* Begin parsing tentatively. We always save tokens while parsing
19796 tentatively so that if the tentative parsing fails we can restore the
19800 cp_parser_parse_tentatively (cp_parser* parser)
19802 /* Enter a new parsing context. */
19803 parser->context = cp_parser_context_new (parser->context);
19804 /* Begin saving tokens. */
19805 cp_lexer_save_tokens (parser->lexer);
19806 /* In order to avoid repetitive access control error messages,
19807 access checks are queued up until we are no longer parsing
19809 push_deferring_access_checks (dk_deferred);
19812 /* Commit to the currently active tentative parse. */
19815 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19817 cp_parser_context *context;
19820 /* Mark all of the levels as committed. */
19821 lexer = parser->lexer;
19822 for (context = parser->context; context->next; context = context->next)
19824 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19826 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19827 while (!cp_lexer_saving_tokens (lexer))
19828 lexer = lexer->next;
19829 cp_lexer_commit_tokens (lexer);
19833 /* Abort the currently active tentative parse. All consumed tokens
19834 will be rolled back, and no diagnostics will be issued. */
19837 cp_parser_abort_tentative_parse (cp_parser* parser)
19839 cp_parser_simulate_error (parser);
19840 /* Now, pretend that we want to see if the construct was
19841 successfully parsed. */
19842 cp_parser_parse_definitely (parser);
19845 /* Stop parsing tentatively. If a parse error has occurred, restore the
19846 token stream. Otherwise, commit to the tokens we have consumed.
19847 Returns true if no error occurred; false otherwise. */
19850 cp_parser_parse_definitely (cp_parser* parser)
19852 bool error_occurred;
19853 cp_parser_context *context;
19855 /* Remember whether or not an error occurred, since we are about to
19856 destroy that information. */
19857 error_occurred = cp_parser_error_occurred (parser);
19858 /* Remove the topmost context from the stack. */
19859 context = parser->context;
19860 parser->context = context->next;
19861 /* If no parse errors occurred, commit to the tentative parse. */
19862 if (!error_occurred)
19864 /* Commit to the tokens read tentatively, unless that was
19866 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19867 cp_lexer_commit_tokens (parser->lexer);
19869 pop_to_parent_deferring_access_checks ();
19871 /* Otherwise, if errors occurred, roll back our state so that things
19872 are just as they were before we began the tentative parse. */
19875 cp_lexer_rollback_tokens (parser->lexer);
19876 pop_deferring_access_checks ();
19878 /* Add the context to the front of the free list. */
19879 context->next = cp_parser_context_free_list;
19880 cp_parser_context_free_list = context;
19882 return !error_occurred;
19885 /* Returns true if we are parsing tentatively and are not committed to
19886 this tentative parse. */
19889 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19891 return (cp_parser_parsing_tentatively (parser)
19892 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19895 /* Returns nonzero iff an error has occurred during the most recent
19896 tentative parse. */
19899 cp_parser_error_occurred (cp_parser* parser)
19901 return (cp_parser_parsing_tentatively (parser)
19902 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19905 /* Returns nonzero if GNU extensions are allowed. */
19908 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19910 return parser->allow_gnu_extensions_p;
19913 /* Objective-C++ Productions */
19916 /* Parse an Objective-C expression, which feeds into a primary-expression
19920 objc-message-expression
19921 objc-string-literal
19922 objc-encode-expression
19923 objc-protocol-expression
19924 objc-selector-expression
19926 Returns a tree representation of the expression. */
19929 cp_parser_objc_expression (cp_parser* parser)
19931 /* Try to figure out what kind of declaration is present. */
19932 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19936 case CPP_OPEN_SQUARE:
19937 return cp_parser_objc_message_expression (parser);
19939 case CPP_OBJC_STRING:
19940 kwd = cp_lexer_consume_token (parser->lexer);
19941 return objc_build_string_object (kwd->u.value);
19944 switch (kwd->keyword)
19946 case RID_AT_ENCODE:
19947 return cp_parser_objc_encode_expression (parser);
19949 case RID_AT_PROTOCOL:
19950 return cp_parser_objc_protocol_expression (parser);
19952 case RID_AT_SELECTOR:
19953 return cp_parser_objc_selector_expression (parser);
19959 error_at (kwd->location,
19960 "misplaced %<@%D%> Objective-C++ construct",
19962 cp_parser_skip_to_end_of_block_or_statement (parser);
19965 return error_mark_node;
19968 /* Parse an Objective-C message expression.
19970 objc-message-expression:
19971 [ objc-message-receiver objc-message-args ]
19973 Returns a representation of an Objective-C message. */
19976 cp_parser_objc_message_expression (cp_parser* parser)
19978 tree receiver, messageargs;
19980 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19981 receiver = cp_parser_objc_message_receiver (parser);
19982 messageargs = cp_parser_objc_message_args (parser);
19983 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19985 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19988 /* Parse an objc-message-receiver.
19990 objc-message-receiver:
19992 simple-type-specifier
19994 Returns a representation of the type or expression. */
19997 cp_parser_objc_message_receiver (cp_parser* parser)
20001 /* An Objective-C message receiver may be either (1) a type
20002 or (2) an expression. */
20003 cp_parser_parse_tentatively (parser);
20004 rcv = cp_parser_expression (parser, false, NULL);
20006 if (cp_parser_parse_definitely (parser))
20009 rcv = cp_parser_simple_type_specifier (parser,
20010 /*decl_specs=*/NULL,
20011 CP_PARSER_FLAGS_NONE);
20013 return objc_get_class_reference (rcv);
20016 /* Parse the arguments and selectors comprising an Objective-C message.
20021 objc-selector-args , objc-comma-args
20023 objc-selector-args:
20024 objc-selector [opt] : assignment-expression
20025 objc-selector-args objc-selector [opt] : assignment-expression
20028 assignment-expression
20029 objc-comma-args , assignment-expression
20031 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20032 selector arguments and TREE_VALUE containing a list of comma
20036 cp_parser_objc_message_args (cp_parser* parser)
20038 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20039 bool maybe_unary_selector_p = true;
20040 cp_token *token = cp_lexer_peek_token (parser->lexer);
20042 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20044 tree selector = NULL_TREE, arg;
20046 if (token->type != CPP_COLON)
20047 selector = cp_parser_objc_selector (parser);
20049 /* Detect if we have a unary selector. */
20050 if (maybe_unary_selector_p
20051 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20052 return build_tree_list (selector, NULL_TREE);
20054 maybe_unary_selector_p = false;
20055 cp_parser_require (parser, CPP_COLON, "%<:%>");
20056 arg = cp_parser_assignment_expression (parser, false, NULL);
20059 = chainon (sel_args,
20060 build_tree_list (selector, arg));
20062 token = cp_lexer_peek_token (parser->lexer);
20065 /* Handle non-selector arguments, if any. */
20066 while (token->type == CPP_COMMA)
20070 cp_lexer_consume_token (parser->lexer);
20071 arg = cp_parser_assignment_expression (parser, false, NULL);
20074 = chainon (addl_args,
20075 build_tree_list (NULL_TREE, arg));
20077 token = cp_lexer_peek_token (parser->lexer);
20080 return build_tree_list (sel_args, addl_args);
20083 /* Parse an Objective-C encode expression.
20085 objc-encode-expression:
20086 @encode objc-typename
20088 Returns an encoded representation of the type argument. */
20091 cp_parser_objc_encode_expression (cp_parser* parser)
20096 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
20097 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20098 token = cp_lexer_peek_token (parser->lexer);
20099 type = complete_type (cp_parser_type_id (parser));
20100 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20104 error_at (token->location,
20105 "%<@encode%> must specify a type as an argument");
20106 return error_mark_node;
20109 return objc_build_encode_expr (type);
20112 /* Parse an Objective-C @defs expression. */
20115 cp_parser_objc_defs_expression (cp_parser *parser)
20119 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
20120 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20121 name = cp_parser_identifier (parser);
20122 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20124 return objc_get_class_ivars (name);
20127 /* Parse an Objective-C protocol expression.
20129 objc-protocol-expression:
20130 @protocol ( identifier )
20132 Returns a representation of the protocol expression. */
20135 cp_parser_objc_protocol_expression (cp_parser* parser)
20139 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20140 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20141 proto = cp_parser_identifier (parser);
20142 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20144 return objc_build_protocol_expr (proto);
20147 /* Parse an Objective-C selector expression.
20149 objc-selector-expression:
20150 @selector ( objc-method-signature )
20152 objc-method-signature:
20158 objc-selector-seq objc-selector :
20160 Returns a representation of the method selector. */
20163 cp_parser_objc_selector_expression (cp_parser* parser)
20165 tree sel_seq = NULL_TREE;
20166 bool maybe_unary_selector_p = true;
20168 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20170 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
20171 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20172 token = cp_lexer_peek_token (parser->lexer);
20174 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20175 || token->type == CPP_SCOPE)
20177 tree selector = NULL_TREE;
20179 if (token->type != CPP_COLON
20180 || token->type == CPP_SCOPE)
20181 selector = cp_parser_objc_selector (parser);
20183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20184 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20186 /* Detect if we have a unary selector. */
20187 if (maybe_unary_selector_p)
20189 sel_seq = selector;
20190 goto finish_selector;
20194 cp_parser_error (parser, "expected %<:%>");
20197 maybe_unary_selector_p = false;
20198 token = cp_lexer_consume_token (parser->lexer);
20200 if (token->type == CPP_SCOPE)
20203 = chainon (sel_seq,
20204 build_tree_list (selector, NULL_TREE));
20206 = chainon (sel_seq,
20207 build_tree_list (NULL_TREE, NULL_TREE));
20211 = chainon (sel_seq,
20212 build_tree_list (selector, NULL_TREE));
20214 token = cp_lexer_peek_token (parser->lexer);
20218 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20220 return objc_build_selector_expr (loc, sel_seq);
20223 /* Parse a list of identifiers.
20225 objc-identifier-list:
20227 objc-identifier-list , identifier
20229 Returns a TREE_LIST of identifier nodes. */
20232 cp_parser_objc_identifier_list (cp_parser* parser)
20234 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20235 cp_token *sep = cp_lexer_peek_token (parser->lexer);
20237 while (sep->type == CPP_COMMA)
20239 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20240 list = chainon (list,
20241 build_tree_list (NULL_TREE,
20242 cp_parser_identifier (parser)));
20243 sep = cp_lexer_peek_token (parser->lexer);
20249 /* Parse an Objective-C alias declaration.
20251 objc-alias-declaration:
20252 @compatibility_alias identifier identifier ;
20254 This function registers the alias mapping with the Objective-C front end.
20255 It returns nothing. */
20258 cp_parser_objc_alias_declaration (cp_parser* parser)
20262 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
20263 alias = cp_parser_identifier (parser);
20264 orig = cp_parser_identifier (parser);
20265 objc_declare_alias (alias, orig);
20266 cp_parser_consume_semicolon_at_end_of_statement (parser);
20269 /* Parse an Objective-C class forward-declaration.
20271 objc-class-declaration:
20272 @class objc-identifier-list ;
20274 The function registers the forward declarations with the Objective-C
20275 front end. It returns nothing. */
20278 cp_parser_objc_class_declaration (cp_parser* parser)
20280 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
20281 objc_declare_class (cp_parser_objc_identifier_list (parser));
20282 cp_parser_consume_semicolon_at_end_of_statement (parser);
20285 /* Parse a list of Objective-C protocol references.
20287 objc-protocol-refs-opt:
20288 objc-protocol-refs [opt]
20290 objc-protocol-refs:
20291 < objc-identifier-list >
20293 Returns a TREE_LIST of identifiers, if any. */
20296 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20298 tree protorefs = NULL_TREE;
20300 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20302 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
20303 protorefs = cp_parser_objc_identifier_list (parser);
20304 cp_parser_require (parser, CPP_GREATER, "%<>%>");
20310 /* Parse a Objective-C visibility specification. */
20313 cp_parser_objc_visibility_spec (cp_parser* parser)
20315 cp_token *vis = cp_lexer_peek_token (parser->lexer);
20317 switch (vis->keyword)
20319 case RID_AT_PRIVATE:
20320 objc_set_visibility (2);
20322 case RID_AT_PROTECTED:
20323 objc_set_visibility (0);
20325 case RID_AT_PUBLIC:
20326 objc_set_visibility (1);
20332 /* Eat '@private'/'@protected'/'@public'. */
20333 cp_lexer_consume_token (parser->lexer);
20336 /* Parse an Objective-C method type. */
20339 cp_parser_objc_method_type (cp_parser* parser)
20341 objc_set_method_type
20342 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20347 /* Parse an Objective-C protocol qualifier. */
20350 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20352 tree quals = NULL_TREE, node;
20353 cp_token *token = cp_lexer_peek_token (parser->lexer);
20355 node = token->u.value;
20357 while (node && TREE_CODE (node) == IDENTIFIER_NODE
20358 && (node == ridpointers [(int) RID_IN]
20359 || node == ridpointers [(int) RID_OUT]
20360 || node == ridpointers [(int) RID_INOUT]
20361 || node == ridpointers [(int) RID_BYCOPY]
20362 || node == ridpointers [(int) RID_BYREF]
20363 || node == ridpointers [(int) RID_ONEWAY]))
20365 quals = tree_cons (NULL_TREE, node, quals);
20366 cp_lexer_consume_token (parser->lexer);
20367 token = cp_lexer_peek_token (parser->lexer);
20368 node = token->u.value;
20374 /* Parse an Objective-C typename. */
20377 cp_parser_objc_typename (cp_parser* parser)
20379 tree type_name = NULL_TREE;
20381 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20383 tree proto_quals, cp_type = NULL_TREE;
20385 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20386 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20388 /* An ObjC type name may consist of just protocol qualifiers, in which
20389 case the type shall default to 'id'. */
20390 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20391 cp_type = cp_parser_type_id (parser);
20393 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20394 type_name = build_tree_list (proto_quals, cp_type);
20400 /* Check to see if TYPE refers to an Objective-C selector name. */
20403 cp_parser_objc_selector_p (enum cpp_ttype type)
20405 return (type == CPP_NAME || type == CPP_KEYWORD
20406 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20407 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20408 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20409 || type == CPP_XOR || type == CPP_XOR_EQ);
20412 /* Parse an Objective-C selector. */
20415 cp_parser_objc_selector (cp_parser* parser)
20417 cp_token *token = cp_lexer_consume_token (parser->lexer);
20419 if (!cp_parser_objc_selector_p (token->type))
20421 error_at (token->location, "invalid Objective-C++ selector name");
20422 return error_mark_node;
20425 /* C++ operator names are allowed to appear in ObjC selectors. */
20426 switch (token->type)
20428 case CPP_AND_AND: return get_identifier ("and");
20429 case CPP_AND_EQ: return get_identifier ("and_eq");
20430 case CPP_AND: return get_identifier ("bitand");
20431 case CPP_OR: return get_identifier ("bitor");
20432 case CPP_COMPL: return get_identifier ("compl");
20433 case CPP_NOT: return get_identifier ("not");
20434 case CPP_NOT_EQ: return get_identifier ("not_eq");
20435 case CPP_OR_OR: return get_identifier ("or");
20436 case CPP_OR_EQ: return get_identifier ("or_eq");
20437 case CPP_XOR: return get_identifier ("xor");
20438 case CPP_XOR_EQ: return get_identifier ("xor_eq");
20439 default: return token->u.value;
20443 /* Parse an Objective-C params list. */
20446 cp_parser_objc_method_keyword_params (cp_parser* parser)
20448 tree params = NULL_TREE;
20449 bool maybe_unary_selector_p = true;
20450 cp_token *token = cp_lexer_peek_token (parser->lexer);
20452 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20454 tree selector = NULL_TREE, type_name, identifier;
20456 if (token->type != CPP_COLON)
20457 selector = cp_parser_objc_selector (parser);
20459 /* Detect if we have a unary selector. */
20460 if (maybe_unary_selector_p
20461 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20464 maybe_unary_selector_p = false;
20465 cp_parser_require (parser, CPP_COLON, "%<:%>");
20466 type_name = cp_parser_objc_typename (parser);
20467 identifier = cp_parser_identifier (parser);
20471 objc_build_keyword_decl (selector,
20475 token = cp_lexer_peek_token (parser->lexer);
20481 /* Parse the non-keyword Objective-C params. */
20484 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20486 tree params = make_node (TREE_LIST);
20487 cp_token *token = cp_lexer_peek_token (parser->lexer);
20488 *ellipsisp = false; /* Initially, assume no ellipsis. */
20490 while (token->type == CPP_COMMA)
20492 cp_parameter_declarator *parmdecl;
20495 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20496 token = cp_lexer_peek_token (parser->lexer);
20498 if (token->type == CPP_ELLIPSIS)
20500 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
20505 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20506 parm = grokdeclarator (parmdecl->declarator,
20507 &parmdecl->decl_specifiers,
20508 PARM, /*initialized=*/0,
20509 /*attrlist=*/NULL);
20511 chainon (params, build_tree_list (NULL_TREE, parm));
20512 token = cp_lexer_peek_token (parser->lexer);
20518 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
20521 cp_parser_objc_interstitial_code (cp_parser* parser)
20523 cp_token *token = cp_lexer_peek_token (parser->lexer);
20525 /* If the next token is `extern' and the following token is a string
20526 literal, then we have a linkage specification. */
20527 if (token->keyword == RID_EXTERN
20528 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20529 cp_parser_linkage_specification (parser);
20530 /* Handle #pragma, if any. */
20531 else if (token->type == CPP_PRAGMA)
20532 cp_parser_pragma (parser, pragma_external);
20533 /* Allow stray semicolons. */
20534 else if (token->type == CPP_SEMICOLON)
20535 cp_lexer_consume_token (parser->lexer);
20536 /* Finally, try to parse a block-declaration, or a function-definition. */
20538 cp_parser_block_declaration (parser, /*statement_p=*/false);
20541 /* Parse a method signature. */
20544 cp_parser_objc_method_signature (cp_parser* parser)
20546 tree rettype, kwdparms, optparms;
20547 bool ellipsis = false;
20549 cp_parser_objc_method_type (parser);
20550 rettype = cp_parser_objc_typename (parser);
20551 kwdparms = cp_parser_objc_method_keyword_params (parser);
20552 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20554 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20557 /* Pars an Objective-C method prototype list. */
20560 cp_parser_objc_method_prototype_list (cp_parser* parser)
20562 cp_token *token = cp_lexer_peek_token (parser->lexer);
20564 while (token->keyword != RID_AT_END)
20566 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20568 objc_add_method_declaration
20569 (cp_parser_objc_method_signature (parser));
20570 cp_parser_consume_semicolon_at_end_of_statement (parser);
20573 /* Allow for interspersed non-ObjC++ code. */
20574 cp_parser_objc_interstitial_code (parser);
20576 token = cp_lexer_peek_token (parser->lexer);
20579 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20580 objc_finish_interface ();
20583 /* Parse an Objective-C method definition list. */
20586 cp_parser_objc_method_definition_list (cp_parser* parser)
20588 cp_token *token = cp_lexer_peek_token (parser->lexer);
20590 while (token->keyword != RID_AT_END)
20594 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20596 push_deferring_access_checks (dk_deferred);
20597 objc_start_method_definition
20598 (cp_parser_objc_method_signature (parser));
20600 /* For historical reasons, we accept an optional semicolon. */
20601 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20602 cp_lexer_consume_token (parser->lexer);
20604 perform_deferred_access_checks ();
20605 stop_deferring_access_checks ();
20606 meth = cp_parser_function_definition_after_declarator (parser,
20608 pop_deferring_access_checks ();
20609 objc_finish_method_definition (meth);
20612 /* Allow for interspersed non-ObjC++ code. */
20613 cp_parser_objc_interstitial_code (parser);
20615 token = cp_lexer_peek_token (parser->lexer);
20618 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20619 objc_finish_implementation ();
20622 /* Parse Objective-C ivars. */
20625 cp_parser_objc_class_ivars (cp_parser* parser)
20627 cp_token *token = cp_lexer_peek_token (parser->lexer);
20629 if (token->type != CPP_OPEN_BRACE)
20630 return; /* No ivars specified. */
20632 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
20633 token = cp_lexer_peek_token (parser->lexer);
20635 while (token->type != CPP_CLOSE_BRACE)
20637 cp_decl_specifier_seq declspecs;
20638 int decl_class_or_enum_p;
20639 tree prefix_attributes;
20641 cp_parser_objc_visibility_spec (parser);
20643 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20646 cp_parser_decl_specifier_seq (parser,
20647 CP_PARSER_FLAGS_OPTIONAL,
20649 &decl_class_or_enum_p);
20650 prefix_attributes = declspecs.attributes;
20651 declspecs.attributes = NULL_TREE;
20653 /* Keep going until we hit the `;' at the end of the
20655 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20657 tree width = NULL_TREE, attributes, first_attribute, decl;
20658 cp_declarator *declarator = NULL;
20659 int ctor_dtor_or_conv_p;
20661 /* Check for a (possibly unnamed) bitfield declaration. */
20662 token = cp_lexer_peek_token (parser->lexer);
20663 if (token->type == CPP_COLON)
20666 if (token->type == CPP_NAME
20667 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20670 /* Get the name of the bitfield. */
20671 declarator = make_id_declarator (NULL_TREE,
20672 cp_parser_identifier (parser),
20676 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20677 /* Get the width of the bitfield. */
20679 = cp_parser_constant_expression (parser,
20680 /*allow_non_constant=*/false,
20685 /* Parse the declarator. */
20687 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20688 &ctor_dtor_or_conv_p,
20689 /*parenthesized_p=*/NULL,
20690 /*member_p=*/false);
20693 /* Look for attributes that apply to the ivar. */
20694 attributes = cp_parser_attributes_opt (parser);
20695 /* Remember which attributes are prefix attributes and
20697 first_attribute = attributes;
20698 /* Combine the attributes. */
20699 attributes = chainon (prefix_attributes, attributes);
20702 /* Create the bitfield declaration. */
20703 decl = grokbitfield (declarator, &declspecs,
20707 decl = grokfield (declarator, &declspecs,
20708 NULL_TREE, /*init_const_expr_p=*/false,
20709 NULL_TREE, attributes);
20711 /* Add the instance variable. */
20712 objc_add_instance_variable (decl);
20714 /* Reset PREFIX_ATTRIBUTES. */
20715 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20716 attributes = TREE_CHAIN (attributes);
20718 TREE_CHAIN (attributes) = NULL_TREE;
20720 token = cp_lexer_peek_token (parser->lexer);
20722 if (token->type == CPP_COMMA)
20724 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20730 cp_parser_consume_semicolon_at_end_of_statement (parser);
20731 token = cp_lexer_peek_token (parser->lexer);
20734 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20735 /* For historical reasons, we accept an optional semicolon. */
20736 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20737 cp_lexer_consume_token (parser->lexer);
20740 /* Parse an Objective-C protocol declaration. */
20743 cp_parser_objc_protocol_declaration (cp_parser* parser)
20745 tree proto, protorefs;
20748 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20751 tok = cp_lexer_peek_token (parser->lexer);
20752 error_at (tok->location, "identifier expected after %<@protocol%>");
20756 /* See if we have a forward declaration or a definition. */
20757 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20759 /* Try a forward declaration first. */
20760 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20762 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20764 cp_parser_consume_semicolon_at_end_of_statement (parser);
20767 /* Ok, we got a full-fledged definition (or at least should). */
20770 proto = cp_parser_identifier (parser);
20771 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20772 objc_start_protocol (proto, protorefs);
20773 cp_parser_objc_method_prototype_list (parser);
20777 /* Parse an Objective-C superclass or category. */
20780 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20783 cp_token *next = cp_lexer_peek_token (parser->lexer);
20785 *super = *categ = NULL_TREE;
20786 if (next->type == CPP_COLON)
20788 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20789 *super = cp_parser_identifier (parser);
20791 else if (next->type == CPP_OPEN_PAREN)
20793 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20794 *categ = cp_parser_identifier (parser);
20795 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20799 /* Parse an Objective-C class interface. */
20802 cp_parser_objc_class_interface (cp_parser* parser)
20804 tree name, super, categ, protos;
20806 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20807 name = cp_parser_identifier (parser);
20808 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20809 protos = cp_parser_objc_protocol_refs_opt (parser);
20811 /* We have either a class or a category on our hands. */
20813 objc_start_category_interface (name, categ, protos);
20816 objc_start_class_interface (name, super, protos);
20817 /* Handle instance variable declarations, if any. */
20818 cp_parser_objc_class_ivars (parser);
20819 objc_continue_interface ();
20822 cp_parser_objc_method_prototype_list (parser);
20825 /* Parse an Objective-C class implementation. */
20828 cp_parser_objc_class_implementation (cp_parser* parser)
20830 tree name, super, categ;
20832 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20833 name = cp_parser_identifier (parser);
20834 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20836 /* We have either a class or a category on our hands. */
20838 objc_start_category_implementation (name, categ);
20841 objc_start_class_implementation (name, super);
20842 /* Handle instance variable declarations, if any. */
20843 cp_parser_objc_class_ivars (parser);
20844 objc_continue_implementation ();
20847 cp_parser_objc_method_definition_list (parser);
20850 /* Consume the @end token and finish off the implementation. */
20853 cp_parser_objc_end_implementation (cp_parser* parser)
20855 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20856 objc_finish_implementation ();
20859 /* Parse an Objective-C declaration. */
20862 cp_parser_objc_declaration (cp_parser* parser)
20864 /* Try to figure out what kind of declaration is present. */
20865 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20867 switch (kwd->keyword)
20870 cp_parser_objc_alias_declaration (parser);
20873 cp_parser_objc_class_declaration (parser);
20875 case RID_AT_PROTOCOL:
20876 cp_parser_objc_protocol_declaration (parser);
20878 case RID_AT_INTERFACE:
20879 cp_parser_objc_class_interface (parser);
20881 case RID_AT_IMPLEMENTATION:
20882 cp_parser_objc_class_implementation (parser);
20885 cp_parser_objc_end_implementation (parser);
20888 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20890 cp_parser_skip_to_end_of_block_or_statement (parser);
20894 /* Parse an Objective-C try-catch-finally statement.
20896 objc-try-catch-finally-stmt:
20897 @try compound-statement objc-catch-clause-seq [opt]
20898 objc-finally-clause [opt]
20900 objc-catch-clause-seq:
20901 objc-catch-clause objc-catch-clause-seq [opt]
20904 @catch ( exception-declaration ) compound-statement
20906 objc-finally-clause
20907 @finally compound-statement
20909 Returns NULL_TREE. */
20912 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20913 location_t location;
20916 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20917 location = cp_lexer_peek_token (parser->lexer)->location;
20918 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20919 node, lest it get absorbed into the surrounding block. */
20920 stmt = push_stmt_list ();
20921 cp_parser_compound_statement (parser, NULL, false);
20922 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20924 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20926 cp_parameter_declarator *parmdecl;
20929 cp_lexer_consume_token (parser->lexer);
20930 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20931 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20932 parm = grokdeclarator (parmdecl->declarator,
20933 &parmdecl->decl_specifiers,
20934 PARM, /*initialized=*/0,
20935 /*attrlist=*/NULL);
20936 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20937 objc_begin_catch_clause (parm);
20938 cp_parser_compound_statement (parser, NULL, false);
20939 objc_finish_catch_clause ();
20942 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20944 cp_lexer_consume_token (parser->lexer);
20945 location = cp_lexer_peek_token (parser->lexer)->location;
20946 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20947 node, lest it get absorbed into the surrounding block. */
20948 stmt = push_stmt_list ();
20949 cp_parser_compound_statement (parser, NULL, false);
20950 objc_build_finally_clause (location, pop_stmt_list (stmt));
20953 return objc_finish_try_stmt ();
20956 /* Parse an Objective-C synchronized statement.
20958 objc-synchronized-stmt:
20959 @synchronized ( expression ) compound-statement
20961 Returns NULL_TREE. */
20964 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20965 location_t location;
20968 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20970 location = cp_lexer_peek_token (parser->lexer)->location;
20971 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20972 lock = cp_parser_expression (parser, false, NULL);
20973 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20975 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20976 node, lest it get absorbed into the surrounding block. */
20977 stmt = push_stmt_list ();
20978 cp_parser_compound_statement (parser, NULL, false);
20980 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20983 /* Parse an Objective-C throw statement.
20986 @throw assignment-expression [opt] ;
20988 Returns a constructed '@throw' statement. */
20991 cp_parser_objc_throw_statement (cp_parser *parser) {
20992 tree expr = NULL_TREE;
20993 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20995 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20998 expr = cp_parser_assignment_expression (parser, false, NULL);
21000 cp_parser_consume_semicolon_at_end_of_statement (parser);
21002 return objc_build_throw_stmt (loc, expr);
21005 /* Parse an Objective-C statement. */
21008 cp_parser_objc_statement (cp_parser * parser) {
21009 /* Try to figure out what kind of declaration is present. */
21010 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21012 switch (kwd->keyword)
21015 return cp_parser_objc_try_catch_finally_statement (parser);
21016 case RID_AT_SYNCHRONIZED:
21017 return cp_parser_objc_synchronized_statement (parser);
21019 return cp_parser_objc_throw_statement (parser);
21021 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21023 cp_parser_skip_to_end_of_block_or_statement (parser);
21026 return error_mark_node;
21029 /* OpenMP 2.5 parsing routines. */
21031 /* Returns name of the next clause.
21032 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21033 the token is not consumed. Otherwise appropriate pragma_omp_clause is
21034 returned and the token is consumed. */
21036 static pragma_omp_clause
21037 cp_parser_omp_clause_name (cp_parser *parser)
21039 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21042 result = PRAGMA_OMP_CLAUSE_IF;
21043 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21044 result = PRAGMA_OMP_CLAUSE_DEFAULT;
21045 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21046 result = PRAGMA_OMP_CLAUSE_PRIVATE;
21047 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21049 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21050 const char *p = IDENTIFIER_POINTER (id);
21055 if (!strcmp ("collapse", p))
21056 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21057 else if (!strcmp ("copyin", p))
21058 result = PRAGMA_OMP_CLAUSE_COPYIN;
21059 else if (!strcmp ("copyprivate", p))
21060 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21063 if (!strcmp ("firstprivate", p))
21064 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21067 if (!strcmp ("lastprivate", p))
21068 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21071 if (!strcmp ("nowait", p))
21072 result = PRAGMA_OMP_CLAUSE_NOWAIT;
21073 else if (!strcmp ("num_threads", p))
21074 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21077 if (!strcmp ("ordered", p))
21078 result = PRAGMA_OMP_CLAUSE_ORDERED;
21081 if (!strcmp ("reduction", p))
21082 result = PRAGMA_OMP_CLAUSE_REDUCTION;
21085 if (!strcmp ("schedule", p))
21086 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21087 else if (!strcmp ("shared", p))
21088 result = PRAGMA_OMP_CLAUSE_SHARED;
21091 if (!strcmp ("untied", p))
21092 result = PRAGMA_OMP_CLAUSE_UNTIED;
21097 if (result != PRAGMA_OMP_CLAUSE_NONE)
21098 cp_lexer_consume_token (parser->lexer);
21103 /* Validate that a clause of the given type does not already exist. */
21106 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21107 const char *name, location_t location)
21111 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21112 if (OMP_CLAUSE_CODE (c) == code)
21114 error_at (location, "too many %qs clauses", name);
21122 variable-list , identifier
21124 In addition, we match a closing parenthesis. An opening parenthesis
21125 will have been consumed by the caller.
21127 If KIND is nonzero, create the appropriate node and install the decl
21128 in OMP_CLAUSE_DECL and add the node to the head of the list.
21130 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21131 return the list created. */
21134 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21142 token = cp_lexer_peek_token (parser->lexer);
21143 name = cp_parser_id_expression (parser, /*template_p=*/false,
21144 /*check_dependency_p=*/true,
21145 /*template_p=*/NULL,
21146 /*declarator_p=*/false,
21147 /*optional_p=*/false);
21148 if (name == error_mark_node)
21151 decl = cp_parser_lookup_name_simple (parser, name, token->location);
21152 if (decl == error_mark_node)
21153 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21154 else if (kind != 0)
21156 tree u = build_omp_clause (token->location, kind);
21157 OMP_CLAUSE_DECL (u) = decl;
21158 OMP_CLAUSE_CHAIN (u) = list;
21162 list = tree_cons (decl, NULL_TREE, list);
21165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21167 cp_lexer_consume_token (parser->lexer);
21170 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21174 /* Try to resync to an unnested comma. Copied from
21175 cp_parser_parenthesized_expression_list. */
21177 ending = cp_parser_skip_to_closing_parenthesis (parser,
21178 /*recovering=*/true,
21180 /*consume_paren=*/true);
21188 /* Similarly, but expect leading and trailing parenthesis. This is a very
21189 common case for omp clauses. */
21192 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21194 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21195 return cp_parser_omp_var_list_no_open (parser, kind, list);
21200 collapse ( constant-expression ) */
21203 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21209 loc = cp_lexer_peek_token (parser->lexer)->location;
21210 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21213 num = cp_parser_constant_expression (parser, false, NULL);
21215 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21217 /*or_comma=*/false,
21218 /*consume_paren=*/true);
21220 if (num == error_mark_node)
21222 num = fold_non_dependent_expr (num);
21223 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21224 || !host_integerp (num, 0)
21225 || (n = tree_low_cst (num, 0)) <= 0
21228 error_at (loc, "collapse argument needs positive constant integer expression");
21232 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21233 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21234 OMP_CLAUSE_CHAIN (c) = list;
21235 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21241 default ( shared | none ) */
21244 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21246 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21249 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21253 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21254 const char *p = IDENTIFIER_POINTER (id);
21259 if (strcmp ("none", p) != 0)
21261 kind = OMP_CLAUSE_DEFAULT_NONE;
21265 if (strcmp ("shared", p) != 0)
21267 kind = OMP_CLAUSE_DEFAULT_SHARED;
21274 cp_lexer_consume_token (parser->lexer);
21279 cp_parser_error (parser, "expected %<none%> or %<shared%>");
21282 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21283 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21284 /*or_comma=*/false,
21285 /*consume_paren=*/true);
21287 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21290 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21291 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21292 OMP_CLAUSE_CHAIN (c) = list;
21293 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21299 if ( expression ) */
21302 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21306 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21309 t = cp_parser_condition (parser);
21311 if (t == error_mark_node
21312 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21314 /*or_comma=*/false,
21315 /*consume_paren=*/true);
21317 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21319 c = build_omp_clause (location, OMP_CLAUSE_IF);
21320 OMP_CLAUSE_IF_EXPR (c) = t;
21321 OMP_CLAUSE_CHAIN (c) = list;
21330 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21331 tree list, location_t location)
21335 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21337 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21338 OMP_CLAUSE_CHAIN (c) = list;
21343 num_threads ( expression ) */
21346 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21347 location_t location)
21351 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21354 t = cp_parser_expression (parser, false, NULL);
21356 if (t == error_mark_node
21357 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21358 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21359 /*or_comma=*/false,
21360 /*consume_paren=*/true);
21362 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21363 "num_threads", location);
21365 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21366 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21367 OMP_CLAUSE_CHAIN (c) = list;
21376 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21377 tree list, location_t location)
21381 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21382 "ordered", location);
21384 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21385 OMP_CLAUSE_CHAIN (c) = list;
21390 reduction ( reduction-operator : variable-list )
21392 reduction-operator:
21393 One of: + * - & ^ | && || */
21396 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21398 enum tree_code code;
21401 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21404 switch (cp_lexer_peek_token (parser->lexer)->type)
21416 code = BIT_AND_EXPR;
21419 code = BIT_XOR_EXPR;
21422 code = BIT_IOR_EXPR;
21425 code = TRUTH_ANDIF_EXPR;
21428 code = TRUTH_ORIF_EXPR;
21431 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21432 "%<|%>, %<&&%>, or %<||%>");
21434 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21435 /*or_comma=*/false,
21436 /*consume_paren=*/true);
21439 cp_lexer_consume_token (parser->lexer);
21441 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21444 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21445 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21446 OMP_CLAUSE_REDUCTION_CODE (c) = code;
21452 schedule ( schedule-kind )
21453 schedule ( schedule-kind , expression )
21456 static | dynamic | guided | runtime | auto */
21459 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21463 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21466 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21468 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21470 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21471 const char *p = IDENTIFIER_POINTER (id);
21476 if (strcmp ("dynamic", p) != 0)
21478 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21482 if (strcmp ("guided", p) != 0)
21484 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21488 if (strcmp ("runtime", p) != 0)
21490 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21497 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21498 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21499 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21500 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21503 cp_lexer_consume_token (parser->lexer);
21505 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21508 cp_lexer_consume_token (parser->lexer);
21510 token = cp_lexer_peek_token (parser->lexer);
21511 t = cp_parser_assignment_expression (parser, false, NULL);
21513 if (t == error_mark_node)
21515 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21516 error_at (token->location, "schedule %<runtime%> does not take "
21517 "a %<chunk_size%> parameter");
21518 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21519 error_at (token->location, "schedule %<auto%> does not take "
21520 "a %<chunk_size%> parameter");
21522 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21524 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21527 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21530 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21531 OMP_CLAUSE_CHAIN (c) = list;
21535 cp_parser_error (parser, "invalid schedule kind");
21537 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21538 /*or_comma=*/false,
21539 /*consume_paren=*/true);
21547 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21548 tree list, location_t location)
21552 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21554 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21555 OMP_CLAUSE_CHAIN (c) = list;
21559 /* Parse all OpenMP clauses. The set clauses allowed by the directive
21560 is a bitmask in MASK. Return the list of clauses found; the result
21561 of clause default goes in *pdefault. */
21564 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21565 const char *where, cp_token *pragma_tok)
21567 tree clauses = NULL;
21569 cp_token *token = NULL;
21571 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21573 pragma_omp_clause c_kind;
21574 const char *c_name;
21575 tree prev = clauses;
21577 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21578 cp_lexer_consume_token (parser->lexer);
21580 token = cp_lexer_peek_token (parser->lexer);
21581 c_kind = cp_parser_omp_clause_name (parser);
21586 case PRAGMA_OMP_CLAUSE_COLLAPSE:
21587 clauses = cp_parser_omp_clause_collapse (parser, clauses,
21589 c_name = "collapse";
21591 case PRAGMA_OMP_CLAUSE_COPYIN:
21592 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21595 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21596 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21598 c_name = "copyprivate";
21600 case PRAGMA_OMP_CLAUSE_DEFAULT:
21601 clauses = cp_parser_omp_clause_default (parser, clauses,
21603 c_name = "default";
21605 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21606 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21608 c_name = "firstprivate";
21610 case PRAGMA_OMP_CLAUSE_IF:
21611 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21614 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21615 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21617 c_name = "lastprivate";
21619 case PRAGMA_OMP_CLAUSE_NOWAIT:
21620 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21623 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21624 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21626 c_name = "num_threads";
21628 case PRAGMA_OMP_CLAUSE_ORDERED:
21629 clauses = cp_parser_omp_clause_ordered (parser, clauses,
21631 c_name = "ordered";
21633 case PRAGMA_OMP_CLAUSE_PRIVATE:
21634 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21636 c_name = "private";
21638 case PRAGMA_OMP_CLAUSE_REDUCTION:
21639 clauses = cp_parser_omp_clause_reduction (parser, clauses);
21640 c_name = "reduction";
21642 case PRAGMA_OMP_CLAUSE_SCHEDULE:
21643 clauses = cp_parser_omp_clause_schedule (parser, clauses,
21645 c_name = "schedule";
21647 case PRAGMA_OMP_CLAUSE_SHARED:
21648 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21652 case PRAGMA_OMP_CLAUSE_UNTIED:
21653 clauses = cp_parser_omp_clause_untied (parser, clauses,
21658 cp_parser_error (parser, "expected %<#pragma omp%> clause");
21662 if (((mask >> c_kind) & 1) == 0)
21664 /* Remove the invalid clause(s) from the list to avoid
21665 confusing the rest of the compiler. */
21667 error_at (token->location, "%qs is not valid for %qs", c_name, where);
21671 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21672 return finish_omp_clauses (clauses);
21679 In practice, we're also interested in adding the statement to an
21680 outer node. So it is convenient if we work around the fact that
21681 cp_parser_statement calls add_stmt. */
21684 cp_parser_begin_omp_structured_block (cp_parser *parser)
21686 unsigned save = parser->in_statement;
21688 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21689 This preserves the "not within loop or switch" style error messages
21690 for nonsense cases like
21696 if (parser->in_statement)
21697 parser->in_statement = IN_OMP_BLOCK;
21703 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21705 parser->in_statement = save;
21709 cp_parser_omp_structured_block (cp_parser *parser)
21711 tree stmt = begin_omp_structured_block ();
21712 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21714 cp_parser_statement (parser, NULL_TREE, false, NULL);
21716 cp_parser_end_omp_structured_block (parser, save);
21717 return finish_omp_structured_block (stmt);
21721 # pragma omp atomic new-line
21725 x binop= expr | x++ | ++x | x-- | --x
21727 +, *, -, /, &, ^, |, <<, >>
21729 where x is an lvalue expression with scalar type. */
21732 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21735 enum tree_code code;
21737 cp_parser_require_pragma_eol (parser, pragma_tok);
21739 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21740 /*cast_p=*/false, NULL);
21741 switch (TREE_CODE (lhs))
21746 case PREINCREMENT_EXPR:
21747 case POSTINCREMENT_EXPR:
21748 lhs = TREE_OPERAND (lhs, 0);
21750 rhs = integer_one_node;
21753 case PREDECREMENT_EXPR:
21754 case POSTDECREMENT_EXPR:
21755 lhs = TREE_OPERAND (lhs, 0);
21757 rhs = integer_one_node;
21761 switch (cp_lexer_peek_token (parser->lexer)->type)
21767 code = TRUNC_DIV_EXPR;
21775 case CPP_LSHIFT_EQ:
21776 code = LSHIFT_EXPR;
21778 case CPP_RSHIFT_EQ:
21779 code = RSHIFT_EXPR;
21782 code = BIT_AND_EXPR;
21785 code = BIT_IOR_EXPR;
21788 code = BIT_XOR_EXPR;
21791 cp_parser_error (parser,
21792 "invalid operator for %<#pragma omp atomic%>");
21795 cp_lexer_consume_token (parser->lexer);
21797 rhs = cp_parser_expression (parser, false, NULL);
21798 if (rhs == error_mark_node)
21802 finish_omp_atomic (code, lhs, rhs);
21803 cp_parser_consume_semicolon_at_end_of_statement (parser);
21807 cp_parser_skip_to_end_of_block_or_statement (parser);
21812 # pragma omp barrier new-line */
21815 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21817 cp_parser_require_pragma_eol (parser, pragma_tok);
21818 finish_omp_barrier ();
21822 # pragma omp critical [(name)] new-line
21823 structured-block */
21826 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21828 tree stmt, name = NULL;
21830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21832 cp_lexer_consume_token (parser->lexer);
21834 name = cp_parser_identifier (parser);
21836 if (name == error_mark_node
21837 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21838 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21839 /*or_comma=*/false,
21840 /*consume_paren=*/true);
21841 if (name == error_mark_node)
21844 cp_parser_require_pragma_eol (parser, pragma_tok);
21846 stmt = cp_parser_omp_structured_block (parser);
21847 return c_finish_omp_critical (input_location, stmt, name);
21851 # pragma omp flush flush-vars[opt] new-line
21854 ( variable-list ) */
21857 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21859 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21860 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21861 cp_parser_require_pragma_eol (parser, pragma_tok);
21863 finish_omp_flush ();
21866 /* Helper function, to parse omp for increment expression. */
21869 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21871 tree cond = cp_parser_binary_expression (parser, false, true,
21872 PREC_NOT_OPERATOR, NULL);
21875 if (cond == error_mark_node
21876 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21878 cp_parser_skip_to_end_of_statement (parser);
21879 return error_mark_node;
21882 switch (TREE_CODE (cond))
21890 return error_mark_node;
21893 /* If decl is an iterator, preserve LHS and RHS of the relational
21894 expr until finish_omp_for. */
21896 && (type_dependent_expression_p (decl)
21897 || CLASS_TYPE_P (TREE_TYPE (decl))))
21900 return build_x_binary_op (TREE_CODE (cond),
21901 TREE_OPERAND (cond, 0), ERROR_MARK,
21902 TREE_OPERAND (cond, 1), ERROR_MARK,
21903 &overloaded_p, tf_warning_or_error);
21906 /* Helper function, to parse omp for increment expression. */
21909 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21911 cp_token *token = cp_lexer_peek_token (parser->lexer);
21917 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21919 op = (token->type == CPP_PLUS_PLUS
21920 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21921 cp_lexer_consume_token (parser->lexer);
21922 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21924 return error_mark_node;
21925 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21928 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21930 return error_mark_node;
21932 token = cp_lexer_peek_token (parser->lexer);
21933 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21935 op = (token->type == CPP_PLUS_PLUS
21936 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21937 cp_lexer_consume_token (parser->lexer);
21938 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21941 op = cp_parser_assignment_operator_opt (parser);
21942 if (op == ERROR_MARK)
21943 return error_mark_node;
21945 if (op != NOP_EXPR)
21947 rhs = cp_parser_assignment_expression (parser, false, NULL);
21948 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21949 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21952 lhs = cp_parser_binary_expression (parser, false, false,
21953 PREC_ADDITIVE_EXPRESSION, NULL);
21954 token = cp_lexer_peek_token (parser->lexer);
21955 decl_first = lhs == decl;
21958 if (token->type != CPP_PLUS
21959 && token->type != CPP_MINUS)
21960 return error_mark_node;
21964 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21965 cp_lexer_consume_token (parser->lexer);
21966 rhs = cp_parser_binary_expression (parser, false, false,
21967 PREC_ADDITIVE_EXPRESSION, NULL);
21968 token = cp_lexer_peek_token (parser->lexer);
21969 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21971 if (lhs == NULL_TREE)
21973 if (op == PLUS_EXPR)
21976 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21979 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21980 NULL, tf_warning_or_error);
21983 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21987 if (rhs != decl || op == MINUS_EXPR)
21988 return error_mark_node;
21989 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21992 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21994 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21997 /* Parse the restricted form of the for statement allowed by OpenMP. */
22000 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22002 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22003 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22004 tree this_pre_body, cl;
22005 location_t loc_first;
22006 bool collapse_err = false;
22007 int i, collapse = 1, nbraces = 0;
22009 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22010 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22011 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22013 gcc_assert (collapse >= 1);
22015 declv = make_tree_vec (collapse);
22016 initv = make_tree_vec (collapse);
22017 condv = make_tree_vec (collapse);
22018 incrv = make_tree_vec (collapse);
22020 loc_first = cp_lexer_peek_token (parser->lexer)->location;
22022 for (i = 0; i < collapse; i++)
22024 int bracecount = 0;
22025 bool add_private_clause = false;
22028 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22030 cp_parser_error (parser, "for statement expected");
22033 loc = cp_lexer_consume_token (parser->lexer)->location;
22035 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22038 init = decl = real_decl = NULL;
22039 this_pre_body = push_stmt_list ();
22040 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22042 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22046 integer-type var = lb
22047 random-access-iterator-type var = lb
22048 pointer-type var = lb
22050 cp_decl_specifier_seq type_specifiers;
22052 /* First, try to parse as an initialized declaration. See
22053 cp_parser_condition, from whence the bulk of this is copied. */
22055 cp_parser_parse_tentatively (parser);
22056 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
22058 if (cp_parser_parse_definitely (parser))
22060 /* If parsing a type specifier seq succeeded, then this
22061 MUST be a initialized declaration. */
22062 tree asm_specification, attributes;
22063 cp_declarator *declarator;
22065 declarator = cp_parser_declarator (parser,
22066 CP_PARSER_DECLARATOR_NAMED,
22067 /*ctor_dtor_or_conv_p=*/NULL,
22068 /*parenthesized_p=*/NULL,
22069 /*member_p=*/false);
22070 attributes = cp_parser_attributes_opt (parser);
22071 asm_specification = cp_parser_asm_specification_opt (parser);
22073 if (declarator == cp_error_declarator)
22074 cp_parser_skip_to_end_of_statement (parser);
22078 tree pushed_scope, auto_node;
22080 decl = start_decl (declarator, &type_specifiers,
22081 SD_INITIALIZED, attributes,
22082 /*prefix_attributes=*/NULL_TREE,
22085 auto_node = type_uses_auto (TREE_TYPE (decl));
22086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22088 if (cp_lexer_next_token_is (parser->lexer,
22090 error ("parenthesized initialization is not allowed in "
22091 "OpenMP %<for%> loop");
22093 /* Trigger an error. */
22094 cp_parser_require (parser, CPP_EQ, "%<=%>");
22096 init = error_mark_node;
22097 cp_parser_skip_to_end_of_statement (parser);
22099 else if (CLASS_TYPE_P (TREE_TYPE (decl))
22100 || type_dependent_expression_p (decl)
22103 bool is_direct_init, is_non_constant_init;
22105 init = cp_parser_initializer (parser,
22107 &is_non_constant_init);
22109 if (auto_node && describable_type (init))
22112 = do_auto_deduction (TREE_TYPE (decl), init,
22115 if (!CLASS_TYPE_P (TREE_TYPE (decl))
22116 && !type_dependent_expression_p (decl))
22120 cp_finish_decl (decl, init, !is_non_constant_init,
22122 LOOKUP_ONLYCONVERTING);
22123 if (CLASS_TYPE_P (TREE_TYPE (decl)))
22126 = tree_cons (NULL, this_pre_body, for_block);
22130 init = pop_stmt_list (this_pre_body);
22131 this_pre_body = NULL_TREE;
22136 cp_lexer_consume_token (parser->lexer);
22137 init = cp_parser_assignment_expression (parser, false, NULL);
22140 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22141 init = error_mark_node;
22143 cp_finish_decl (decl, NULL_TREE,
22144 /*init_const_expr_p=*/false,
22146 LOOKUP_ONLYCONVERTING);
22150 pop_scope (pushed_scope);
22156 /* If parsing a type specifier sequence failed, then
22157 this MUST be a simple expression. */
22158 cp_parser_parse_tentatively (parser);
22159 decl = cp_parser_primary_expression (parser, false, false,
22161 if (!cp_parser_error_occurred (parser)
22164 && CLASS_TYPE_P (TREE_TYPE (decl)))
22168 cp_parser_parse_definitely (parser);
22169 cp_parser_require (parser, CPP_EQ, "%<=%>");
22170 rhs = cp_parser_assignment_expression (parser, false, NULL);
22171 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22173 tf_warning_or_error));
22174 add_private_clause = true;
22179 cp_parser_abort_tentative_parse (parser);
22180 init = cp_parser_expression (parser, false, NULL);
22183 if (TREE_CODE (init) == MODIFY_EXPR
22184 || TREE_CODE (init) == MODOP_EXPR)
22185 real_decl = TREE_OPERAND (init, 0);
22190 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22193 this_pre_body = pop_stmt_list (this_pre_body);
22197 pre_body = push_stmt_list ();
22199 add_stmt (this_pre_body);
22200 pre_body = pop_stmt_list (pre_body);
22203 pre_body = this_pre_body;
22208 if (par_clauses != NULL && real_decl != NULL_TREE)
22211 for (c = par_clauses; *c ; )
22212 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22213 && OMP_CLAUSE_DECL (*c) == real_decl)
22215 error_at (loc, "iteration variable %qD"
22216 " should not be firstprivate", real_decl);
22217 *c = OMP_CLAUSE_CHAIN (*c);
22219 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22220 && OMP_CLAUSE_DECL (*c) == real_decl)
22222 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22223 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
22224 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22225 OMP_CLAUSE_DECL (l) = real_decl;
22226 OMP_CLAUSE_CHAIN (l) = clauses;
22227 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22229 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22230 CP_OMP_CLAUSE_INFO (*c) = NULL;
22231 add_private_clause = false;
22235 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22236 && OMP_CLAUSE_DECL (*c) == real_decl)
22237 add_private_clause = false;
22238 c = &OMP_CLAUSE_CHAIN (*c);
22242 if (add_private_clause)
22245 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22247 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22248 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22249 && OMP_CLAUSE_DECL (c) == decl)
22251 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22252 && OMP_CLAUSE_DECL (c) == decl)
22253 error_at (loc, "iteration variable %qD "
22254 "should not be firstprivate",
22256 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22257 && OMP_CLAUSE_DECL (c) == decl)
22258 error_at (loc, "iteration variable %qD should not be reduction",
22263 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22264 OMP_CLAUSE_DECL (c) = decl;
22265 c = finish_omp_clauses (c);
22268 OMP_CLAUSE_CHAIN (c) = clauses;
22275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22276 cond = cp_parser_omp_for_cond (parser, decl);
22277 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22282 /* If decl is an iterator, preserve the operator on decl
22283 until finish_omp_for. */
22285 && (type_dependent_expression_p (decl)
22286 || CLASS_TYPE_P (TREE_TYPE (decl))))
22287 incr = cp_parser_omp_for_incr (parser, decl);
22289 incr = cp_parser_expression (parser, false, NULL);
22292 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22293 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22294 /*or_comma=*/false,
22295 /*consume_paren=*/true);
22297 TREE_VEC_ELT (declv, i) = decl;
22298 TREE_VEC_ELT (initv, i) = init;
22299 TREE_VEC_ELT (condv, i) = cond;
22300 TREE_VEC_ELT (incrv, i) = incr;
22302 if (i == collapse - 1)
22305 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22306 in between the collapsed for loops to be still considered perfectly
22307 nested. Hopefully the final version clarifies this.
22308 For now handle (multiple) {'s and empty statements. */
22309 cp_parser_parse_tentatively (parser);
22312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22314 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22316 cp_lexer_consume_token (parser->lexer);
22319 else if (bracecount
22320 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22321 cp_lexer_consume_token (parser->lexer);
22324 loc = cp_lexer_peek_token (parser->lexer)->location;
22325 error_at (loc, "not enough collapsed for loops");
22326 collapse_err = true;
22327 cp_parser_abort_tentative_parse (parser);
22336 cp_parser_parse_definitely (parser);
22337 nbraces += bracecount;
22341 /* Note that we saved the original contents of this flag when we entered
22342 the structured block, and so we don't need to re-save it here. */
22343 parser->in_statement = IN_OMP_FOR;
22345 /* Note that the grammar doesn't call for a structured block here,
22346 though the loop as a whole is a structured block. */
22347 body = push_stmt_list ();
22348 cp_parser_statement (parser, NULL_TREE, false, NULL);
22349 body = pop_stmt_list (body);
22351 if (declv == NULL_TREE)
22354 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22355 pre_body, clauses);
22359 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22361 cp_lexer_consume_token (parser->lexer);
22364 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22365 cp_lexer_consume_token (parser->lexer);
22370 error_at (cp_lexer_peek_token (parser->lexer)->location,
22371 "collapsed loops not perfectly nested");
22373 collapse_err = true;
22374 cp_parser_statement_seq_opt (parser, NULL);
22375 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22381 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22382 for_block = TREE_CHAIN (for_block);
22389 #pragma omp for for-clause[optseq] new-line
22392 #define OMP_FOR_CLAUSE_MASK \
22393 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22394 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22395 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22396 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22397 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
22398 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
22399 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
22400 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22403 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22405 tree clauses, sb, ret;
22408 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22409 "#pragma omp for", pragma_tok);
22411 sb = begin_omp_structured_block ();
22412 save = cp_parser_begin_omp_structured_block (parser);
22414 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22416 cp_parser_end_omp_structured_block (parser, save);
22417 add_stmt (finish_omp_structured_block (sb));
22423 # pragma omp master new-line
22424 structured-block */
22427 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22429 cp_parser_require_pragma_eol (parser, pragma_tok);
22430 return c_finish_omp_master (input_location,
22431 cp_parser_omp_structured_block (parser));
22435 # pragma omp ordered new-line
22436 structured-block */
22439 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22441 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22442 cp_parser_require_pragma_eol (parser, pragma_tok);
22443 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22449 { section-sequence }
22452 section-directive[opt] structured-block
22453 section-sequence section-directive structured-block */
22456 cp_parser_omp_sections_scope (cp_parser *parser)
22458 tree stmt, substmt;
22459 bool error_suppress = false;
22462 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22465 stmt = push_stmt_list ();
22467 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22471 substmt = begin_omp_structured_block ();
22472 save = cp_parser_begin_omp_structured_block (parser);
22476 cp_parser_statement (parser, NULL_TREE, false, NULL);
22478 tok = cp_lexer_peek_token (parser->lexer);
22479 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22481 if (tok->type == CPP_CLOSE_BRACE)
22483 if (tok->type == CPP_EOF)
22487 cp_parser_end_omp_structured_block (parser, save);
22488 substmt = finish_omp_structured_block (substmt);
22489 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22490 add_stmt (substmt);
22495 tok = cp_lexer_peek_token (parser->lexer);
22496 if (tok->type == CPP_CLOSE_BRACE)
22498 if (tok->type == CPP_EOF)
22501 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22503 cp_lexer_consume_token (parser->lexer);
22504 cp_parser_require_pragma_eol (parser, tok);
22505 error_suppress = false;
22507 else if (!error_suppress)
22509 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22510 error_suppress = true;
22513 substmt = cp_parser_omp_structured_block (parser);
22514 substmt = build1 (OMP_SECTION, void_type_node, substmt);
22515 add_stmt (substmt);
22517 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22519 substmt = pop_stmt_list (stmt);
22521 stmt = make_node (OMP_SECTIONS);
22522 TREE_TYPE (stmt) = void_type_node;
22523 OMP_SECTIONS_BODY (stmt) = substmt;
22530 # pragma omp sections sections-clause[optseq] newline
22533 #define OMP_SECTIONS_CLAUSE_MASK \
22534 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22535 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22536 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
22537 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22538 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22541 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22545 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22546 "#pragma omp sections", pragma_tok);
22548 ret = cp_parser_omp_sections_scope (parser);
22550 OMP_SECTIONS_CLAUSES (ret) = clauses;
22556 # pragma parallel parallel-clause new-line
22557 # pragma parallel for parallel-for-clause new-line
22558 # pragma parallel sections parallel-sections-clause new-line */
22560 #define OMP_PARALLEL_CLAUSE_MASK \
22561 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22562 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22563 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22564 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22565 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
22566 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
22567 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
22568 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22571 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22573 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22574 const char *p_name = "#pragma omp parallel";
22575 tree stmt, clauses, par_clause, ws_clause, block;
22576 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22578 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22582 cp_lexer_consume_token (parser->lexer);
22583 p_kind = PRAGMA_OMP_PARALLEL_FOR;
22584 p_name = "#pragma omp parallel for";
22585 mask |= OMP_FOR_CLAUSE_MASK;
22586 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22588 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22590 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22591 const char *p = IDENTIFIER_POINTER (id);
22592 if (strcmp (p, "sections") == 0)
22594 cp_lexer_consume_token (parser->lexer);
22595 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22596 p_name = "#pragma omp parallel sections";
22597 mask |= OMP_SECTIONS_CLAUSE_MASK;
22598 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22602 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22603 block = begin_omp_parallel ();
22604 save = cp_parser_begin_omp_structured_block (parser);
22608 case PRAGMA_OMP_PARALLEL:
22609 cp_parser_statement (parser, NULL_TREE, false, NULL);
22610 par_clause = clauses;
22613 case PRAGMA_OMP_PARALLEL_FOR:
22614 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22615 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22618 case PRAGMA_OMP_PARALLEL_SECTIONS:
22619 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22620 stmt = cp_parser_omp_sections_scope (parser);
22622 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22626 gcc_unreachable ();
22629 cp_parser_end_omp_structured_block (parser, save);
22630 stmt = finish_omp_parallel (par_clause, block);
22631 if (p_kind != PRAGMA_OMP_PARALLEL)
22632 OMP_PARALLEL_COMBINED (stmt) = 1;
22637 # pragma omp single single-clause[optseq] new-line
22638 structured-block */
22640 #define OMP_SINGLE_CLAUSE_MASK \
22641 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22642 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22643 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
22644 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22647 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22649 tree stmt = make_node (OMP_SINGLE);
22650 TREE_TYPE (stmt) = void_type_node;
22652 OMP_SINGLE_CLAUSES (stmt)
22653 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22654 "#pragma omp single", pragma_tok);
22655 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22657 return add_stmt (stmt);
22661 # pragma omp task task-clause[optseq] new-line
22662 structured-block */
22664 #define OMP_TASK_CLAUSE_MASK \
22665 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
22666 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
22667 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
22668 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
22669 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
22670 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22673 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22675 tree clauses, block;
22678 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22679 "#pragma omp task", pragma_tok);
22680 block = begin_omp_task ();
22681 save = cp_parser_begin_omp_structured_block (parser);
22682 cp_parser_statement (parser, NULL_TREE, false, NULL);
22683 cp_parser_end_omp_structured_block (parser, save);
22684 return finish_omp_task (clauses, block);
22688 # pragma omp taskwait new-line */
22691 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22693 cp_parser_require_pragma_eol (parser, pragma_tok);
22694 finish_omp_taskwait ();
22698 # pragma omp threadprivate (variable-list) */
22701 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22705 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22706 cp_parser_require_pragma_eol (parser, pragma_tok);
22708 finish_omp_threadprivate (vars);
22711 /* Main entry point to OpenMP statement pragmas. */
22714 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22718 switch (pragma_tok->pragma_kind)
22720 case PRAGMA_OMP_ATOMIC:
22721 cp_parser_omp_atomic (parser, pragma_tok);
22723 case PRAGMA_OMP_CRITICAL:
22724 stmt = cp_parser_omp_critical (parser, pragma_tok);
22726 case PRAGMA_OMP_FOR:
22727 stmt = cp_parser_omp_for (parser, pragma_tok);
22729 case PRAGMA_OMP_MASTER:
22730 stmt = cp_parser_omp_master (parser, pragma_tok);
22732 case PRAGMA_OMP_ORDERED:
22733 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22735 case PRAGMA_OMP_PARALLEL:
22736 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22738 case PRAGMA_OMP_SECTIONS:
22739 stmt = cp_parser_omp_sections (parser, pragma_tok);
22741 case PRAGMA_OMP_SINGLE:
22742 stmt = cp_parser_omp_single (parser, pragma_tok);
22744 case PRAGMA_OMP_TASK:
22745 stmt = cp_parser_omp_task (parser, pragma_tok);
22748 gcc_unreachable ();
22752 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22757 static GTY (()) cp_parser *the_parser;
22760 /* Special handling for the first token or line in the file. The first
22761 thing in the file might be #pragma GCC pch_preprocess, which loads a
22762 PCH file, which is a GC collection point. So we need to handle this
22763 first pragma without benefit of an existing lexer structure.
22765 Always returns one token to the caller in *FIRST_TOKEN. This is
22766 either the true first token of the file, or the first token after
22767 the initial pragma. */
22770 cp_parser_initial_pragma (cp_token *first_token)
22774 cp_lexer_get_preprocessor_token (NULL, first_token);
22775 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22778 cp_lexer_get_preprocessor_token (NULL, first_token);
22779 if (first_token->type == CPP_STRING)
22781 name = first_token->u.value;
22783 cp_lexer_get_preprocessor_token (NULL, first_token);
22784 if (first_token->type != CPP_PRAGMA_EOL)
22785 error_at (first_token->location,
22786 "junk at end of %<#pragma GCC pch_preprocess%>");
22789 error_at (first_token->location, "expected string literal");
22791 /* Skip to the end of the pragma. */
22792 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22793 cp_lexer_get_preprocessor_token (NULL, first_token);
22795 /* Now actually load the PCH file. */
22797 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22799 /* Read one more token to return to our caller. We have to do this
22800 after reading the PCH file in, since its pointers have to be
22802 cp_lexer_get_preprocessor_token (NULL, first_token);
22805 /* Normal parsing of a pragma token. Here we can (and must) use the
22809 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22811 cp_token *pragma_tok;
22814 pragma_tok = cp_lexer_consume_token (parser->lexer);
22815 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22816 parser->lexer->in_pragma = true;
22818 id = pragma_tok->pragma_kind;
22821 case PRAGMA_GCC_PCH_PREPROCESS:
22822 error_at (pragma_tok->location,
22823 "%<#pragma GCC pch_preprocess%> must be first");
22826 case PRAGMA_OMP_BARRIER:
22829 case pragma_compound:
22830 cp_parser_omp_barrier (parser, pragma_tok);
22833 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22834 "used in compound statements");
22841 case PRAGMA_OMP_FLUSH:
22844 case pragma_compound:
22845 cp_parser_omp_flush (parser, pragma_tok);
22848 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22849 "used in compound statements");
22856 case PRAGMA_OMP_TASKWAIT:
22859 case pragma_compound:
22860 cp_parser_omp_taskwait (parser, pragma_tok);
22863 error_at (pragma_tok->location,
22864 "%<#pragma omp taskwait%> may only be "
22865 "used in compound statements");
22872 case PRAGMA_OMP_THREADPRIVATE:
22873 cp_parser_omp_threadprivate (parser, pragma_tok);
22876 case PRAGMA_OMP_ATOMIC:
22877 case PRAGMA_OMP_CRITICAL:
22878 case PRAGMA_OMP_FOR:
22879 case PRAGMA_OMP_MASTER:
22880 case PRAGMA_OMP_ORDERED:
22881 case PRAGMA_OMP_PARALLEL:
22882 case PRAGMA_OMP_SECTIONS:
22883 case PRAGMA_OMP_SINGLE:
22884 case PRAGMA_OMP_TASK:
22885 if (context == pragma_external)
22887 cp_parser_omp_construct (parser, pragma_tok);
22890 case PRAGMA_OMP_SECTION:
22891 error_at (pragma_tok->location,
22892 "%<#pragma omp section%> may only be used in "
22893 "%<#pragma omp sections%> construct");
22897 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22898 c_invoke_pragma_handler (id);
22902 cp_parser_error (parser, "expected declaration specifiers");
22906 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22910 /* The interface the pragma parsers have to the lexer. */
22913 pragma_lex (tree *value)
22916 enum cpp_ttype ret;
22918 tok = cp_lexer_peek_token (the_parser->lexer);
22921 *value = tok->u.value;
22923 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22925 else if (ret == CPP_STRING)
22926 *value = cp_parser_string_literal (the_parser, false, false);
22929 cp_lexer_consume_token (the_parser->lexer);
22930 if (ret == CPP_KEYWORD)
22938 /* External interface. */
22940 /* Parse one entire translation unit. */
22943 c_parse_file (void)
22945 bool error_occurred;
22946 static bool already_called = false;
22948 if (already_called)
22950 sorry ("inter-module optimizations not implemented for C++");
22953 already_called = true;
22955 the_parser = cp_parser_new ();
22956 push_deferring_access_checks (flag_access_control
22957 ? dk_no_deferred : dk_no_check);
22958 error_occurred = cp_parser_translation_unit (the_parser);
22962 #include "gt-cp-parser.h"