2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct GTY(()) tree_check {
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
154 static cp_lexer *cp_lexer_new_main
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
160 static int cp_lexer_saving_tokens
162 static cp_token_position cp_lexer_token_position
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
180 static void cp_lexer_purge_token
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
186 static void cp_lexer_commit_tokens
188 static void cp_lexer_rollback_tokens
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251 sizeof, typeof, or alignof. */
252 int cp_unevaluated_operand;
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
258 cp_lexer_new_main (void)
260 cp_token first_token;
267 /* It's possible that parsing the first pragma will load a PCH file,
268 which is a GC collection point. So we have to do that before
269 allocating any memory. */
270 cp_parser_initial_pragma (&first_token);
272 c_common_no_more_pch ();
274 /* Allocate the memory. */
275 lexer = GGC_CNEW (cp_lexer);
277 #ifdef ENABLE_CHECKING
278 /* Initially we are not debugging. */
279 lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282 CP_SAVED_TOKEN_STACK);
284 /* Create the buffer. */
285 alloc = CP_LEXER_BUFFER_SIZE;
286 buffer = GGC_NEWVEC (cp_token, alloc);
288 /* Put the first token in the buffer. */
293 /* Get the remaining tokens from the preprocessor. */
294 while (pos->type != CPP_EOF)
301 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302 pos = buffer + space;
304 cp_lexer_get_preprocessor_token (lexer, pos);
306 lexer->buffer = buffer;
307 lexer->buffer_length = alloc - space;
308 lexer->last_token = pos;
309 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
311 /* Subsequent preprocessor diagnostics should use compiler
312 diagnostic functions to get the compiler source location. */
315 gcc_assert (lexer->next_token->type != CPP_PURGED);
319 /* Create a new lexer whose token stream is primed with the tokens in
320 CACHE. When these tokens are exhausted, no new tokens will be read. */
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 cp_token *first = cache->first;
326 cp_token *last = cache->last;
327 cp_lexer *lexer = GGC_CNEW (cp_lexer);
329 /* We do not own the buffer. */
330 lexer->buffer = NULL;
331 lexer->buffer_length = 0;
332 lexer->next_token = first == last ? &eof_token : first;
333 lexer->last_token = last;
335 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336 CP_SAVED_TOKEN_STACK);
338 #ifdef ENABLE_CHECKING
339 /* Initially we are not debugging. */
340 lexer->debugging_p = false;
343 gcc_assert (lexer->next_token->type != CPP_PURGED);
347 /* Frees all resources associated with LEXER. */
350 cp_lexer_destroy (cp_lexer *lexer)
353 ggc_free (lexer->buffer);
354 VEC_free (cp_token_position, heap, lexer->saved_tokens);
358 /* Returns nonzero if debugging information should be output. */
360 #ifdef ENABLE_CHECKING
363 cp_lexer_debugging_p (cp_lexer *lexer)
365 return lexer->debugging_p;
368 #endif /* ENABLE_CHECKING */
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 gcc_assert (!previous_p || lexer->next_token != &eof_token);
375 return lexer->next_token - previous_p;
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
384 /* nonzero if we are presently saving tokens. */
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
392 /* Store the next token from the preprocessor in *TOKEN. Return true
393 if we reach EOF. If LEXER is NULL, assume we are handling an
394 initial #pragma pch_preprocess, and thus want the lexer to return
395 processed strings. */
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 static int is_extern_c = 0;
402 /* Get a new token from the preprocessor. */
404 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
406 token->keyword = RID_MAX;
407 token->pragma_kind = PRAGMA_NONE;
409 /* On some systems, some header files are surrounded by an
410 implicit extern "C" block. Set a flag in the token if it
411 comes from such a header. */
412 is_extern_c += pending_lang_change;
413 pending_lang_change = 0;
414 token->implicit_extern_c = is_extern_c > 0;
416 /* Check to see if this token is a keyword. */
417 if (token->type == CPP_NAME)
419 if (C_IS_RESERVED_WORD (token->u.value))
421 /* Mark this token as a keyword. */
422 token->type = CPP_KEYWORD;
423 /* Record which keyword. */
424 token->keyword = C_RID_CODE (token->u.value);
428 if (warn_cxx0x_compat
429 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
432 /* Warn about the C++0x keyword (but still treat it as
434 warning (OPT_Wc__0x_compat,
435 "identifier %qE will become a keyword in C++0x",
438 /* Clear out the C_RID_CODE so we don't warn about this
439 particular identifier-turned-keyword again. */
440 C_SET_RID_CODE (token->u.value, RID_MAX);
443 token->ambiguous_p = false;
444 token->keyword = RID_MAX;
447 /* Handle Objective-C++ keywords. */
448 else if (token->type == CPP_AT_NAME)
450 token->type = CPP_KEYWORD;
451 switch (C_RID_CODE (token->u.value))
453 /* Map 'class' to '@class', 'private' to '@private', etc. */
454 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458 case RID_THROW: token->keyword = RID_AT_THROW; break;
459 case RID_TRY: token->keyword = RID_AT_TRY; break;
460 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461 default: token->keyword = C_RID_CODE (token->u.value);
464 else if (token->type == CPP_PRAGMA)
466 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
467 token->pragma_kind = ((enum pragma_kind)
468 TREE_INT_CST_LOW (token->u.value));
469 token->u.value = NULL_TREE;
473 /* Update the globals input_location and the input file stack from TOKEN. */
475 cp_lexer_set_source_position_from_token (cp_token *token)
477 if (token->type != CPP_EOF)
479 input_location = token->location;
483 /* Return a pointer to the next token in the token stream, but do not
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
489 if (cp_lexer_debugging_p (lexer))
491 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493 putc ('\n', cp_lexer_debug_stream);
495 return lexer->next_token;
498 /* Return true if the next token has the indicated TYPE. */
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
503 return cp_lexer_peek_token (lexer)->type == type;
506 /* Return true if the next token does not have the indicated TYPE. */
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
511 return !cp_lexer_next_token_is (lexer, type);
514 /* Return true if the next token is the indicated KEYWORD. */
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
519 return cp_lexer_peek_token (lexer)->keyword == keyword;
522 /* Return true if the next token is not the indicated KEYWORD. */
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
527 return cp_lexer_peek_token (lexer)->keyword != keyword;
530 /* Return true if the next token is a keyword for a decl-specifier. */
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
537 token = cp_lexer_peek_token (lexer);
538 switch (token->keyword)
540 /* auto specifier: storage-class-specifier in C++,
541 simple-type-specifier in C++0x. */
543 /* Storage classes. */
549 /* Elaborated type specifiers. */
555 /* Simple type specifiers. */
569 /* GNU extensions. */
572 /* C++0x extensions. */
581 /* Return a pointer to the Nth token in the token stream. If N is 1,
582 then this is precisely equivalent to cp_lexer_peek_token (except
583 that it is not inline). One would like to disallow that case, but
584 there is one case (cp_parser_nth_token_starts_template_id) where
585 the caller passes a variable for N and it might be 1. */
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
592 /* N is 1-based, not zero-based. */
595 if (cp_lexer_debugging_p (lexer))
596 fprintf (cp_lexer_debug_stream,
597 "cp_lexer: peeking ahead %ld at token: ", (long)n);
600 token = lexer->next_token;
601 gcc_assert (!n || token != &eof_token);
605 if (token == lexer->last_token)
611 if (token->type != CPP_PURGED)
615 if (cp_lexer_debugging_p (lexer))
617 cp_lexer_print_token (cp_lexer_debug_stream, token);
618 putc ('\n', cp_lexer_debug_stream);
624 /* Return the next token, and advance the lexer's next_token pointer
625 to point to the next non-purged token. */
628 cp_lexer_consume_token (cp_lexer* lexer)
630 cp_token *token = lexer->next_token;
632 gcc_assert (token != &eof_token);
633 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
638 if (lexer->next_token == lexer->last_token)
640 lexer->next_token = &eof_token;
645 while (lexer->next_token->type == CPP_PURGED);
647 cp_lexer_set_source_position_from_token (token);
649 /* Provide debugging output. */
650 if (cp_lexer_debugging_p (lexer))
652 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653 cp_lexer_print_token (cp_lexer_debug_stream, token);
654 putc ('\n', cp_lexer_debug_stream);
660 /* Permanently remove the next token from the token stream, and
661 advance the next_token pointer to refer to the next non-purged
665 cp_lexer_purge_token (cp_lexer *lexer)
667 cp_token *tok = lexer->next_token;
669 gcc_assert (tok != &eof_token);
670 tok->type = CPP_PURGED;
671 tok->location = UNKNOWN_LOCATION;
672 tok->u.value = NULL_TREE;
673 tok->keyword = RID_MAX;
678 if (tok == lexer->last_token)
684 while (tok->type == CPP_PURGED);
685 lexer->next_token = tok;
688 /* Permanently remove all tokens after TOK, up to, but not
689 including, the token that will be returned next by
690 cp_lexer_peek_token. */
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
695 cp_token *peek = lexer->next_token;
697 if (peek == &eof_token)
698 peek = lexer->last_token;
700 gcc_assert (tok < peek);
702 for ( tok += 1; tok != peek; tok += 1)
704 tok->type = CPP_PURGED;
705 tok->location = UNKNOWN_LOCATION;
706 tok->u.value = NULL_TREE;
707 tok->keyword = RID_MAX;
711 /* Begin saving tokens. All tokens consumed after this point will be
715 cp_lexer_save_tokens (cp_lexer* lexer)
717 /* Provide debugging output. */
718 if (cp_lexer_debugging_p (lexer))
719 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
721 VEC_safe_push (cp_token_position, heap,
722 lexer->saved_tokens, lexer->next_token);
725 /* Commit to the portion of the token stream most recently saved. */
728 cp_lexer_commit_tokens (cp_lexer* lexer)
730 /* Provide debugging output. */
731 if (cp_lexer_debugging_p (lexer))
732 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
734 VEC_pop (cp_token_position, lexer->saved_tokens);
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738 to the token stream. Stop saving tokens. */
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
743 /* Provide debugging output. */
744 if (cp_lexer_debugging_p (lexer))
745 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
747 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
750 /* Print a representation of the TOKEN on the STREAM. */
752 #ifdef ENABLE_CHECKING
755 cp_lexer_print_token (FILE * stream, cp_token *token)
757 /* We don't use cpp_type2name here because the parser defines
758 a few tokens of its own. */
759 static const char *const token_names[] = {
760 /* cpplib-defined token types */
766 /* C++ parser token types - see "Manifest constants", above. */
769 "NESTED_NAME_SPECIFIER",
773 /* If we have a name for the token, print it out. Otherwise, we
774 simply give the numeric code. */
775 gcc_assert (token->type < ARRAY_SIZE(token_names));
776 fputs (token_names[token->type], stream);
778 /* For some tokens, print the associated data. */
782 /* Some keywords have a value that is not an IDENTIFIER_NODE.
783 For example, `struct' is mapped to an INTEGER_CST. */
784 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
786 /* else fall through */
788 fputs (IDENTIFIER_POINTER (token->u.value), stream);
795 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
803 /* Start emitting debugging information. */
806 cp_lexer_start_debugging (cp_lexer* lexer)
808 lexer->debugging_p = true;
811 /* Stop emitting debugging information. */
814 cp_lexer_stop_debugging (cp_lexer* lexer)
816 lexer->debugging_p = false;
819 #endif /* ENABLE_CHECKING */
821 /* Create a new cp_token_cache, representing a range of tokens. */
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
826 cp_token_cache *cache = GGC_NEW (cp_token_cache);
827 cache->first = first;
833 /* Decl-specifiers. */
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
840 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 /* Nothing other than the parser should be creating declarators;
846 declarators are a semi-syntactic representation of C++ entities.
847 Other parts of the front end that need to create entities (like
848 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
850 static cp_declarator *make_call_declarator
851 (cp_declarator *, tree, cp_cv_quals, tree, tree);
852 static cp_declarator *make_array_declarator
853 (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855 (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857 (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859 (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861 (cp_cv_quals, tree, cp_declarator *);
863 /* An erroneous declarator. */
864 static cp_declarator *cp_error_declarator;
866 /* The obstack on which declarators and related data structures are
868 static struct obstack declarator_obstack;
870 /* Alloc BYTES from the declarator memory pool. */
873 alloc_declarator (size_t bytes)
875 return obstack_alloc (&declarator_obstack, bytes);
878 /* Allocate a declarator of the indicated KIND. Clear fields that are
879 common to all declarators. */
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
884 cp_declarator *declarator;
886 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887 declarator->kind = kind;
888 declarator->attributes = NULL_TREE;
889 declarator->declarator = NULL;
890 declarator->parameter_pack_p = false;
895 /* Make a declarator for a generalized identifier. If
896 QUALIFYING_SCOPE is non-NULL, the identifier is
897 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898 UNQUALIFIED_NAME. SFK indicates the kind of special function this
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903 special_function_kind sfk)
905 cp_declarator *declarator;
907 /* It is valid to write:
909 class C { void f(); };
913 The standard is not clear about whether `typedef const C D' is
914 legal; as of 2002-09-15 the committee is considering that
915 question. EDG 3.0 allows that syntax. Therefore, we do as
917 if (qualifying_scope && TYPE_P (qualifying_scope))
918 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
920 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
924 declarator = make_declarator (cdk_id);
925 declarator->u.id.qualifying_scope = qualifying_scope;
926 declarator->u.id.unqualified_name = unqualified_name;
927 declarator->u.id.sfk = sfk;
932 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
933 of modifiers such as const or volatile to apply to the pointer
934 type, represented as identifiers. */
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
939 cp_declarator *declarator;
941 declarator = make_declarator (cdk_pointer);
942 declarator->declarator = target;
943 declarator->u.pointer.qualifiers = cv_qualifiers;
944 declarator->u.pointer.class_type = NULL_TREE;
947 declarator->parameter_pack_p = target->parameter_pack_p;
948 target->parameter_pack_p = false;
951 declarator->parameter_pack_p = false;
956 /* Like make_pointer_declarator -- but for references. */
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962 cp_declarator *declarator;
964 declarator = make_declarator (cdk_reference);
965 declarator->declarator = target;
966 declarator->u.reference.qualifiers = cv_qualifiers;
967 declarator->u.reference.rvalue_ref = rvalue_ref;
970 declarator->parameter_pack_p = target->parameter_pack_p;
971 target->parameter_pack_p = false;
974 declarator->parameter_pack_p = false;
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980 member of CLASS_TYPE. */
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984 cp_declarator *pointee)
986 cp_declarator *declarator;
988 declarator = make_declarator (cdk_ptrmem);
989 declarator->declarator = pointee;
990 declarator->u.pointer.qualifiers = cv_qualifiers;
991 declarator->u.pointer.class_type = class_type;
995 declarator->parameter_pack_p = pointee->parameter_pack_p;
996 pointee->parameter_pack_p = false;
999 declarator->parameter_pack_p = false;
1004 /* Make a declarator for the function given by TARGET, with the
1005 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1006 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1007 indicates what exceptions can be thrown. */
1010 make_call_declarator (cp_declarator *target,
1012 cp_cv_quals cv_qualifiers,
1013 tree exception_specification,
1014 tree late_return_type)
1016 cp_declarator *declarator;
1018 declarator = make_declarator (cdk_function);
1019 declarator->declarator = target;
1020 declarator->u.function.parameters = parms;
1021 declarator->u.function.qualifiers = cv_qualifiers;
1022 declarator->u.function.exception_specification = exception_specification;
1023 declarator->u.function.late_return_type = late_return_type;
1026 declarator->parameter_pack_p = target->parameter_pack_p;
1027 target->parameter_pack_p = false;
1030 declarator->parameter_pack_p = false;
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036 defined by ELEMENT. */
1039 make_array_declarator (cp_declarator *element, tree bounds)
1041 cp_declarator *declarator;
1043 declarator = make_declarator (cdk_array);
1044 declarator->declarator = element;
1045 declarator->u.array.bounds = bounds;
1048 declarator->parameter_pack_p = element->parameter_pack_p;
1049 element->parameter_pack_p = false;
1052 declarator->parameter_pack_p = false;
1057 /* Determine whether the declarator we've seen so far can be a
1058 parameter pack, when followed by an ellipsis. */
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 /* Search for a declarator name, or any other declarator that goes
1063 after the point where the ellipsis could appear in a parameter
1064 pack. If we find any of these, then this declarator can not be
1065 made into a parameter pack. */
1067 while (declarator && !found)
1069 switch ((int)declarator->kind)
1080 declarator = declarator->declarator;
1088 cp_parameter_declarator *no_parameters;
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091 DECLARATOR and DEFAULT_ARGUMENT. */
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095 cp_declarator *declarator,
1096 tree default_argument)
1098 cp_parameter_declarator *parameter;
1100 parameter = ((cp_parameter_declarator *)
1101 alloc_declarator (sizeof (cp_parameter_declarator)));
1102 parameter->next = NULL;
1103 if (decl_specifiers)
1104 parameter->decl_specifiers = *decl_specifiers;
1106 clear_decl_specs (¶meter->decl_specifiers);
1107 parameter->declarator = declarator;
1108 parameter->default_argument = default_argument;
1109 parameter->ellipsis_p = false;
1114 /* Returns true iff DECLARATOR is a declaration for a function. */
1117 function_declarator_p (const cp_declarator *declarator)
1121 if (declarator->kind == cdk_function
1122 && declarator->declarator->kind == cdk_id)
1124 if (declarator->kind == cdk_id
1125 || declarator->kind == cdk_error)
1127 declarator = declarator->declarator;
1137 A cp_parser parses the token stream as specified by the C++
1138 grammar. Its job is purely parsing, not semantic analysis. For
1139 example, the parser breaks the token stream into declarators,
1140 expressions, statements, and other similar syntactic constructs.
1141 It does not check that the types of the expressions on either side
1142 of an assignment-statement are compatible, or that a function is
1143 not declared with a parameter of type `void'.
1145 The parser invokes routines elsewhere in the compiler to perform
1146 semantic analysis and to build up the abstract syntax tree for the
1149 The parser (and the template instantiation code, which is, in a
1150 way, a close relative of parsing) are the only parts of the
1151 compiler that should be calling push_scope and pop_scope, or
1152 related functions. The parser (and template instantiation code)
1153 keeps track of what scope is presently active; everything else
1154 should simply honor that. (The code that generates static
1155 initializers may also need to set the scope, in order to check
1156 access control correctly when emitting the initializers.)
1161 The parser is of the standard recursive-descent variety. Upcoming
1162 tokens in the token stream are examined in order to determine which
1163 production to use when parsing a non-terminal. Some C++ constructs
1164 require arbitrary look ahead to disambiguate. For example, it is
1165 impossible, in the general case, to tell whether a statement is an
1166 expression or declaration without scanning the entire statement.
1167 Therefore, the parser is capable of "parsing tentatively." When the
1168 parser is not sure what construct comes next, it enters this mode.
1169 Then, while we attempt to parse the construct, the parser queues up
1170 error messages, rather than issuing them immediately, and saves the
1171 tokens it consumes. If the construct is parsed successfully, the
1172 parser "commits", i.e., it issues any queued error messages and
1173 the tokens that were being preserved are permanently discarded.
1174 If, however, the construct is not parsed successfully, the parser
1175 rolls back its state completely so that it can resume parsing using
1176 a different alternative.
1181 The performance of the parser could probably be improved substantially.
1182 We could often eliminate the need to parse tentatively by looking ahead
1183 a little bit. In some places, this approach might not entirely eliminate
1184 the need to parse tentatively, but it might still speed up the average
1187 /* Flags that are passed to some parsing functions. These values can
1188 be bitwise-ored together. */
1193 CP_PARSER_FLAGS_NONE = 0x0,
1194 /* The construct is optional. If it is not present, then no error
1195 should be issued. */
1196 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197 /* When parsing a type-specifier, do not allow user-defined types. */
1198 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1201 /* This type is used for parameters and variables which hold
1202 combinations of the above flags. */
1203 typedef int cp_parser_flags;
1205 /* The different kinds of declarators we want to parse. */
1207 typedef enum cp_parser_declarator_kind
1209 /* We want an abstract declarator. */
1210 CP_PARSER_DECLARATOR_ABSTRACT,
1211 /* We want a named declarator. */
1212 CP_PARSER_DECLARATOR_NAMED,
1213 /* We don't mind, but the name must be an unqualified-id. */
1214 CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1217 /* The precedence values used to parse binary expressions. The minimum value
1218 of PREC must be 1, because zero is reserved to quickly discriminate
1219 binary operators from other tokens. */
1224 PREC_LOGICAL_OR_EXPRESSION,
1225 PREC_LOGICAL_AND_EXPRESSION,
1226 PREC_INCLUSIVE_OR_EXPRESSION,
1227 PREC_EXCLUSIVE_OR_EXPRESSION,
1228 PREC_AND_EXPRESSION,
1229 PREC_EQUALITY_EXPRESSION,
1230 PREC_RELATIONAL_EXPRESSION,
1231 PREC_SHIFT_EXPRESSION,
1232 PREC_ADDITIVE_EXPRESSION,
1233 PREC_MULTIPLICATIVE_EXPRESSION,
1235 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239 precedence value. */
1241 typedef struct cp_parser_binary_operations_map_node
1243 /* The token type. */
1244 enum cpp_ttype token_type;
1245 /* The corresponding tree code. */
1246 enum tree_code tree_type;
1247 /* The precedence of this operator. */
1248 enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1251 /* The status of a tentative parse. */
1253 typedef enum cp_parser_status_kind
1255 /* No errors have occurred. */
1256 CP_PARSER_STATUS_KIND_NO_ERROR,
1257 /* An error has occurred. */
1258 CP_PARSER_STATUS_KIND_ERROR,
1259 /* We are committed to this tentative parse, whether or not an error
1261 CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1264 typedef struct cp_parser_expression_stack_entry
1266 /* Left hand side of the binary operation we are currently
1269 /* Original tree code for left hand side, if it was a binary
1270 expression itself (used for -Wparentheses). */
1271 enum tree_code lhs_type;
1272 /* Tree code for the binary operation we are parsing. */
1273 enum tree_code tree_type;
1274 /* Precedence of the binary operation we are parsing. */
1275 enum cp_parser_prec prec;
1276 } cp_parser_expression_stack_entry;
1278 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1279 entries because precedence levels on the stack are monotonically
1281 typedef struct cp_parser_expression_stack_entry
1282 cp_parser_expression_stack[NUM_PREC_VALUES];
1284 /* Context that is saved and restored when parsing tentatively. */
1285 typedef struct GTY (()) cp_parser_context {
1286 /* If this is a tentative parsing context, the status of the
1288 enum cp_parser_status_kind status;
1289 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1290 that are looked up in this context must be looked up both in the
1291 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292 the context of the containing expression. */
1295 /* The next parsing context in the stack. */
1296 struct cp_parser_context *next;
1297 } cp_parser_context;
1301 /* Constructors and destructors. */
1303 static cp_parser_context *cp_parser_context_new
1304 (cp_parser_context *);
1306 /* Class variables. */
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311 Transformed into an associative array (binops_by_token) by
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1318 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1336 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1338 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1340 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1342 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1344 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 /* The same as binops, but initialized by cp_parser_new so that
1348 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1352 /* Constructors and destructors. */
1354 /* Construct a new context. The context below this one on the stack
1355 is given by NEXT. */
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1360 cp_parser_context *context;
1362 /* Allocate the storage. */
1363 if (cp_parser_context_free_list != NULL)
1365 /* Pull the first entry from the free list. */
1366 context = cp_parser_context_free_list;
1367 cp_parser_context_free_list = context->next;
1368 memset (context, 0, sizeof (*context));
1371 context = GGC_CNEW (cp_parser_context);
1373 /* No errors have occurred yet in this context. */
1374 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375 /* If this is not the bottommost context, copy information that we
1376 need from the previous context. */
1379 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380 expression, then we are parsing one in this context, too. */
1381 context->object_type = next->object_type;
1382 /* Thread the stack. */
1383 context->next = next;
1389 /* The cp_parser structure represents the C++ parser. */
1391 typedef struct GTY(()) cp_parser {
1392 /* The lexer from which we are obtaining tokens. */
1395 /* The scope in which names should be looked up. If NULL_TREE, then
1396 we look up names in the scope that is currently open in the
1397 source program. If non-NULL, this is either a TYPE or
1398 NAMESPACE_DECL for the scope in which we should look. It can
1399 also be ERROR_MARK, when we've parsed a bogus scope.
1401 This value is not cleared automatically after a name is looked
1402 up, so we must be careful to clear it before starting a new look
1403 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1404 will look up `Z' in the scope of `X', rather than the current
1405 scope.) Unfortunately, it is difficult to tell when name lookup
1406 is complete, because we sometimes peek at a token, look it up,
1407 and then decide not to consume it. */
1410 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411 last lookup took place. OBJECT_SCOPE is used if an expression
1412 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413 respectively. QUALIFYING_SCOPE is used for an expression of the
1414 form "X::Y"; it refers to X. */
1416 tree qualifying_scope;
1418 /* A stack of parsing contexts. All but the bottom entry on the
1419 stack will be tentative contexts.
1421 We parse tentatively in order to determine which construct is in
1422 use in some situations. For example, in order to determine
1423 whether a statement is an expression-statement or a
1424 declaration-statement we parse it tentatively as a
1425 declaration-statement. If that fails, we then reparse the same
1426 token stream as an expression-statement. */
1427 cp_parser_context *context;
1429 /* True if we are parsing GNU C++. If this flag is not set, then
1430 GNU extensions are not recognized. */
1431 bool allow_gnu_extensions_p;
1433 /* TRUE if the `>' token should be interpreted as the greater-than
1434 operator. FALSE if it is the end of a template-id or
1435 template-parameter-list. In C++0x mode, this flag also applies to
1436 `>>' tokens, which are viewed as two consecutive `>' tokens when
1437 this flag is FALSE. */
1438 bool greater_than_is_operator_p;
1440 /* TRUE if default arguments are allowed within a parameter list
1441 that starts at this point. FALSE if only a gnu extension makes
1442 them permissible. */
1443 bool default_arg_ok_p;
1445 /* TRUE if we are parsing an integral constant-expression. See
1446 [expr.const] for a precise definition. */
1447 bool integral_constant_expression_p;
1449 /* TRUE if we are parsing an integral constant-expression -- but a
1450 non-constant expression should be permitted as well. This flag
1451 is used when parsing an array bound so that GNU variable-length
1452 arrays are tolerated. */
1453 bool allow_non_integral_constant_expression_p;
1455 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456 been seen that makes the expression non-constant. */
1457 bool non_integral_constant_expression_p;
1459 /* TRUE if local variable names and `this' are forbidden in the
1461 bool local_variables_forbidden_p;
1463 /* TRUE if the declaration we are parsing is part of a
1464 linkage-specification of the form `extern string-literal
1466 bool in_unbraced_linkage_specification_p;
1468 /* TRUE if we are presently parsing a declarator, after the
1469 direct-declarator. */
1470 bool in_declarator_p;
1472 /* TRUE if we are presently parsing a template-argument-list. */
1473 bool in_template_argument_list_p;
1475 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476 to IN_OMP_BLOCK if parsing OpenMP structured block and
1477 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1478 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479 iteration-statement, OpenMP block or loop within that switch. */
1480 #define IN_SWITCH_STMT 1
1481 #define IN_ITERATION_STMT 2
1482 #define IN_OMP_BLOCK 4
1483 #define IN_OMP_FOR 8
1484 #define IN_IF_STMT 16
1485 unsigned char in_statement;
1487 /* TRUE if we are presently parsing the body of a switch statement.
1488 Note that this doesn't quite overlap with in_statement above.
1489 The difference relates to giving the right sets of error messages:
1490 "case not in switch" vs "break statement used with OpenMP...". */
1491 bool in_switch_statement_p;
1493 /* TRUE if we are parsing a type-id in an expression context. In
1494 such a situation, both "type (expr)" and "type (type)" are valid
1496 bool in_type_id_in_expr_p;
1498 /* TRUE if we are currently in a header file where declarations are
1499 implicitly extern "C". */
1500 bool implicit_extern_c;
1502 /* TRUE if strings in expressions should be translated to the execution
1504 bool translate_strings_p;
1506 /* TRUE if we are presently parsing the body of a function, but not
1508 bool in_function_body;
1510 /* If non-NULL, then we are parsing a construct where new type
1511 definitions are not permitted. The string stored here will be
1512 issued as an error message if a type is defined. */
1513 const char *type_definition_forbidden_message;
1515 /* A list of lists. The outer list is a stack, used for member
1516 functions of local classes. At each level there are two sub-list,
1517 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519 TREE_VALUE's. The functions are chained in reverse declaration
1522 The TREE_PURPOSE sublist contains those functions with default
1523 arguments that need post processing, and the TREE_VALUE sublist
1524 contains those functions with definitions that need post
1527 These lists can only be processed once the outermost class being
1528 defined is complete. */
1529 tree unparsed_functions_queues;
1531 /* The number of classes whose definitions are currently in
1533 unsigned num_classes_being_defined;
1535 /* The number of template parameter lists that apply directly to the
1536 current declaration. */
1537 unsigned num_template_parameter_lists;
1542 /* Constructors and destructors. */
1544 static cp_parser *cp_parser_new
1547 /* Routines to parse various constructs.
1549 Those that return `tree' will return the error_mark_node (rather
1550 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551 Sometimes, they will return an ordinary node if error-recovery was
1552 attempted, even though a parse error occurred. So, to check
1553 whether or not a parse error occurred, you should always use
1554 cp_parser_error_occurred. If the construct is optional (indicated
1555 either by an `_opt' in the name of the function that does the
1556 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557 the construct is not present. */
1559 /* Lexical conventions [gram.lex] */
1561 static tree cp_parser_identifier
1563 static tree cp_parser_string_literal
1564 (cp_parser *, bool, bool);
1566 /* Basic concepts [gram.basic] */
1568 static bool cp_parser_translation_unit
1571 /* Expressions [gram.expr] */
1573 static tree cp_parser_primary_expression
1574 (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576 (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580 (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582 (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_qualifying_entity
1584 (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586 (cp_parser *, bool, bool, bool, cp_id_kind *);
1587 static tree cp_parser_postfix_open_square_expression
1588 (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1591 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1592 (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594 (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596 (cp_parser *, bool, bool, cp_id_kind *);
1597 static enum tree_code cp_parser_unary_operator
1599 static tree cp_parser_new_expression
1601 static VEC(tree,gc) *cp_parser_new_placement
1603 static tree cp_parser_new_type_id
1604 (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1607 static cp_declarator *cp_parser_direct_new_declarator
1609 static VEC(tree,gc) *cp_parser_new_initializer
1611 static tree cp_parser_delete_expression
1613 static tree cp_parser_cast_expression
1614 (cp_parser *, bool, bool, cp_id_kind *);
1615 static tree cp_parser_binary_expression
1616 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1617 static tree cp_parser_question_colon_clause
1618 (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620 (cp_parser *, bool, cp_id_kind *);
1621 static enum tree_code cp_parser_assignment_operator_opt
1623 static tree cp_parser_expression
1624 (cp_parser *, bool, cp_id_kind *);
1625 static tree cp_parser_constant_expression
1626 (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1630 /* Statements [gram.stmt.stmt] */
1632 static void cp_parser_statement
1633 (cp_parser *, tree, bool, bool *);
1634 static void cp_parser_label_for_labeled_statement
1636 static tree cp_parser_expression_statement
1637 (cp_parser *, tree);
1638 static tree cp_parser_compound_statement
1639 (cp_parser *, tree, bool);
1640 static void cp_parser_statement_seq_opt
1641 (cp_parser *, tree);
1642 static tree cp_parser_selection_statement
1643 (cp_parser *, bool *);
1644 static tree cp_parser_condition
1646 static tree cp_parser_iteration_statement
1648 static void cp_parser_for_init_statement
1650 static tree cp_parser_jump_statement
1652 static void cp_parser_declaration_statement
1655 static tree cp_parser_implicitly_scoped_statement
1656 (cp_parser *, bool *);
1657 static void cp_parser_already_scoped_statement
1660 /* Declarations [gram.dcl.dcl] */
1662 static void cp_parser_declaration_seq_opt
1664 static void cp_parser_declaration
1666 static void cp_parser_block_declaration
1667 (cp_parser *, bool);
1668 static void cp_parser_simple_declaration
1669 (cp_parser *, bool);
1670 static void cp_parser_decl_specifier_seq
1671 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1672 static tree cp_parser_storage_class_specifier_opt
1674 static tree cp_parser_function_specifier_opt
1675 (cp_parser *, cp_decl_specifier_seq *);
1676 static tree cp_parser_type_specifier
1677 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679 static tree cp_parser_simple_type_specifier
1680 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1681 static tree cp_parser_type_name
1683 static tree cp_parser_nonclass_name
1684 (cp_parser* parser);
1685 static tree cp_parser_elaborated_type_specifier
1686 (cp_parser *, bool, bool);
1687 static tree cp_parser_enum_specifier
1689 static void cp_parser_enumerator_list
1690 (cp_parser *, tree);
1691 static void cp_parser_enumerator_definition
1692 (cp_parser *, tree);
1693 static tree cp_parser_namespace_name
1695 static void cp_parser_namespace_definition
1697 static void cp_parser_namespace_body
1699 static tree cp_parser_qualified_namespace_specifier
1701 static void cp_parser_namespace_alias_definition
1703 static bool cp_parser_using_declaration
1704 (cp_parser *, bool);
1705 static void cp_parser_using_directive
1707 static void cp_parser_asm_definition
1709 static void cp_parser_linkage_specification
1711 static void cp_parser_static_assert
1712 (cp_parser *, bool);
1713 static tree cp_parser_decltype
1716 /* Declarators [gram.dcl.decl] */
1718 static tree cp_parser_init_declarator
1719 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1720 static cp_declarator *cp_parser_declarator
1721 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1722 static cp_declarator *cp_parser_direct_declarator
1723 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1724 static enum tree_code cp_parser_ptr_operator
1725 (cp_parser *, tree *, cp_cv_quals *);
1726 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728 static tree cp_parser_late_return_type_opt
1730 static tree cp_parser_declarator_id
1731 (cp_parser *, bool);
1732 static tree cp_parser_type_id
1734 static tree cp_parser_template_type_arg
1736 static tree cp_parser_type_id_1
1737 (cp_parser *, bool);
1738 static void cp_parser_type_specifier_seq
1739 (cp_parser *, bool, cp_decl_specifier_seq *);
1740 static tree cp_parser_parameter_declaration_clause
1742 static tree cp_parser_parameter_declaration_list
1743 (cp_parser *, bool *);
1744 static cp_parameter_declarator *cp_parser_parameter_declaration
1745 (cp_parser *, bool, bool *);
1746 static tree cp_parser_default_argument
1747 (cp_parser *, bool);
1748 static void cp_parser_function_body
1750 static tree cp_parser_initializer
1751 (cp_parser *, bool *, bool *);
1752 static tree cp_parser_initializer_clause
1753 (cp_parser *, bool *);
1754 static tree cp_parser_braced_list
1755 (cp_parser*, bool*);
1756 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1757 (cp_parser *, bool *);
1759 static bool cp_parser_ctor_initializer_opt_and_function_body
1762 /* Classes [gram.class] */
1764 static tree cp_parser_class_name
1765 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1766 static tree cp_parser_class_specifier
1768 static tree cp_parser_class_head
1769 (cp_parser *, bool *, tree *, tree *);
1770 static enum tag_types cp_parser_class_key
1772 static void cp_parser_member_specification_opt
1774 static void cp_parser_member_declaration
1776 static tree cp_parser_pure_specifier
1778 static tree cp_parser_constant_initializer
1781 /* Derived classes [gram.class.derived] */
1783 static tree cp_parser_base_clause
1785 static tree cp_parser_base_specifier
1788 /* Special member functions [gram.special] */
1790 static tree cp_parser_conversion_function_id
1792 static tree cp_parser_conversion_type_id
1794 static cp_declarator *cp_parser_conversion_declarator_opt
1796 static bool cp_parser_ctor_initializer_opt
1798 static void cp_parser_mem_initializer_list
1800 static tree cp_parser_mem_initializer
1802 static tree cp_parser_mem_initializer_id
1805 /* Overloading [gram.over] */
1807 static tree cp_parser_operator_function_id
1809 static tree cp_parser_operator
1812 /* Templates [gram.temp] */
1814 static void cp_parser_template_declaration
1815 (cp_parser *, bool);
1816 static tree cp_parser_template_parameter_list
1818 static tree cp_parser_template_parameter
1819 (cp_parser *, bool *, bool *);
1820 static tree cp_parser_type_parameter
1821 (cp_parser *, bool *);
1822 static tree cp_parser_template_id
1823 (cp_parser *, bool, bool, bool);
1824 static tree cp_parser_template_name
1825 (cp_parser *, bool, bool, bool, bool *);
1826 static tree cp_parser_template_argument_list
1828 static tree cp_parser_template_argument
1830 static void cp_parser_explicit_instantiation
1832 static void cp_parser_explicit_specialization
1835 /* Exception handling [gram.exception] */
1837 static tree cp_parser_try_block
1839 static bool cp_parser_function_try_block
1841 static void cp_parser_handler_seq
1843 static void cp_parser_handler
1845 static tree cp_parser_exception_declaration
1847 static tree cp_parser_throw_expression
1849 static tree cp_parser_exception_specification_opt
1851 static tree cp_parser_type_id_list
1854 /* GNU Extensions */
1856 static tree cp_parser_asm_specification_opt
1858 static tree cp_parser_asm_operand_list
1860 static tree cp_parser_asm_clobber_list
1862 static tree cp_parser_attributes_opt
1864 static tree cp_parser_attribute_list
1866 static bool cp_parser_extension_opt
1867 (cp_parser *, int *);
1868 static void cp_parser_label_declaration
1871 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1872 static bool cp_parser_pragma
1873 (cp_parser *, enum pragma_context);
1875 /* Objective-C++ Productions */
1877 static tree cp_parser_objc_message_receiver
1879 static tree cp_parser_objc_message_args
1881 static tree cp_parser_objc_message_expression
1883 static tree cp_parser_objc_encode_expression
1885 static tree cp_parser_objc_defs_expression
1887 static tree cp_parser_objc_protocol_expression
1889 static tree cp_parser_objc_selector_expression
1891 static tree cp_parser_objc_expression
1893 static bool cp_parser_objc_selector_p
1895 static tree cp_parser_objc_selector
1897 static tree cp_parser_objc_protocol_refs_opt
1899 static void cp_parser_objc_declaration
1901 static tree cp_parser_objc_statement
1904 /* Utility Routines */
1906 static tree cp_parser_lookup_name
1907 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1908 static tree cp_parser_lookup_name_simple
1909 (cp_parser *, tree, location_t);
1910 static tree cp_parser_maybe_treat_template_as_class
1912 static bool cp_parser_check_declarator_template_parameters
1913 (cp_parser *, cp_declarator *, location_t);
1914 static bool cp_parser_check_template_parameters
1915 (cp_parser *, unsigned, location_t, cp_declarator *);
1916 static tree cp_parser_simple_cast_expression
1918 static tree cp_parser_global_scope_opt
1919 (cp_parser *, bool);
1920 static bool cp_parser_constructor_declarator_p
1921 (cp_parser *, bool);
1922 static tree cp_parser_function_definition_from_specifiers_and_declarator
1923 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1924 static tree cp_parser_function_definition_after_declarator
1925 (cp_parser *, bool);
1926 static void cp_parser_template_declaration_after_export
1927 (cp_parser *, bool);
1928 static void cp_parser_perform_template_parameter_access_checks
1929 (VEC (deferred_access_check,gc)*);
1930 static tree cp_parser_single_declaration
1931 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1932 static tree cp_parser_functional_cast
1933 (cp_parser *, tree);
1934 static tree cp_parser_save_member_function_body
1935 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1936 static tree cp_parser_enclosed_template_argument_list
1938 static void cp_parser_save_default_args
1939 (cp_parser *, tree);
1940 static void cp_parser_late_parsing_for_member
1941 (cp_parser *, tree);
1942 static void cp_parser_late_parsing_default_args
1943 (cp_parser *, tree);
1944 static tree cp_parser_sizeof_operand
1945 (cp_parser *, enum rid);
1946 static tree cp_parser_trait_expr
1947 (cp_parser *, enum rid);
1948 static bool cp_parser_declares_only_class_p
1950 static void cp_parser_set_storage_class
1951 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1952 static void cp_parser_set_decl_spec_type
1953 (cp_decl_specifier_seq *, tree, location_t, bool);
1954 static bool cp_parser_friend_p
1955 (const cp_decl_specifier_seq *);
1956 static cp_token *cp_parser_require
1957 (cp_parser *, enum cpp_ttype, const char *);
1958 static cp_token *cp_parser_require_keyword
1959 (cp_parser *, enum rid, const char *);
1960 static bool cp_parser_token_starts_function_definition_p
1962 static bool cp_parser_next_token_starts_class_definition_p
1964 static bool cp_parser_next_token_ends_template_argument_p
1966 static bool cp_parser_nth_token_starts_template_argument_list_p
1967 (cp_parser *, size_t);
1968 static enum tag_types cp_parser_token_is_class_key
1970 static void cp_parser_check_class_key
1971 (enum tag_types, tree type);
1972 static void cp_parser_check_access_in_redeclaration
1973 (tree type, location_t location);
1974 static bool cp_parser_optional_template_keyword
1976 static void cp_parser_pre_parsed_nested_name_specifier
1978 static bool cp_parser_cache_group
1979 (cp_parser *, enum cpp_ttype, unsigned);
1980 static void cp_parser_parse_tentatively
1982 static void cp_parser_commit_to_tentative_parse
1984 static void cp_parser_abort_tentative_parse
1986 static bool cp_parser_parse_definitely
1988 static inline bool cp_parser_parsing_tentatively
1990 static bool cp_parser_uncommitted_to_tentative_parse_p
1992 static void cp_parser_error
1993 (cp_parser *, const char *);
1994 static void cp_parser_name_lookup_error
1995 (cp_parser *, tree, tree, const char *, location_t);
1996 static bool cp_parser_simulate_error
1998 static bool cp_parser_check_type_definition
2000 static void cp_parser_check_for_definition_in_return_type
2001 (cp_declarator *, tree, location_t type_location);
2002 static void cp_parser_check_for_invalid_template_id
2003 (cp_parser *, tree, location_t location);
2004 static bool cp_parser_non_integral_constant_expression
2005 (cp_parser *, const char *);
2006 static void cp_parser_diagnose_invalid_type_name
2007 (cp_parser *, tree, tree, location_t);
2008 static bool cp_parser_parse_and_diagnose_invalid_type_name
2010 static int cp_parser_skip_to_closing_parenthesis
2011 (cp_parser *, bool, bool, bool);
2012 static void cp_parser_skip_to_end_of_statement
2014 static void cp_parser_consume_semicolon_at_end_of_statement
2016 static void cp_parser_skip_to_end_of_block_or_statement
2018 static bool cp_parser_skip_to_closing_brace
2020 static void cp_parser_skip_to_end_of_template_parameter_list
2022 static void cp_parser_skip_to_pragma_eol
2023 (cp_parser*, cp_token *);
2024 static bool cp_parser_error_occurred
2026 static bool cp_parser_allow_gnu_extensions_p
2028 static bool cp_parser_is_string_literal
2030 static bool cp_parser_is_keyword
2031 (cp_token *, enum rid);
2032 static tree cp_parser_make_typename_type
2033 (cp_parser *, tree, tree, location_t location);
2034 static cp_declarator * cp_parser_make_indirect_declarator
2035 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2037 /* Returns nonzero if we are parsing tentatively. */
2040 cp_parser_parsing_tentatively (cp_parser* parser)
2042 return parser->context->next != NULL;
2045 /* Returns nonzero if TOKEN is a string literal. */
2048 cp_parser_is_string_literal (cp_token* token)
2050 return (token->type == CPP_STRING ||
2051 token->type == CPP_STRING16 ||
2052 token->type == CPP_STRING32 ||
2053 token->type == CPP_WSTRING);
2056 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2059 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2061 return token->keyword == keyword;
2064 /* If not parsing tentatively, issue a diagnostic of the form
2065 FILE:LINE: MESSAGE before TOKEN
2066 where TOKEN is the next token in the input stream. MESSAGE
2067 (specified by the caller) is usually of the form "expected
2071 cp_parser_error (cp_parser* parser, const char* message)
2073 if (!cp_parser_simulate_error (parser))
2075 cp_token *token = cp_lexer_peek_token (parser->lexer);
2076 /* This diagnostic makes more sense if it is tagged to the line
2077 of the token we just peeked at. */
2078 cp_lexer_set_source_position_from_token (token);
2080 if (token->type == CPP_PRAGMA)
2082 error ("%H%<#pragma%> is not allowed here", &token->location);
2083 cp_parser_skip_to_pragma_eol (parser, token);
2087 c_parse_error (message,
2088 /* Because c_parser_error does not understand
2089 CPP_KEYWORD, keywords are treated like
2091 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2092 token->u.value, token->flags);
2096 /* Issue an error about name-lookup failing. NAME is the
2097 IDENTIFIER_NODE DECL is the result of
2098 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2099 the thing that we hoped to find. */
2102 cp_parser_name_lookup_error (cp_parser* parser,
2105 const char* desired,
2106 location_t location)
2108 /* If name lookup completely failed, tell the user that NAME was not
2110 if (decl == error_mark_node)
2112 if (parser->scope && parser->scope != global_namespace)
2113 error ("%H%<%E::%E%> has not been declared",
2114 &location, parser->scope, name);
2115 else if (parser->scope == global_namespace)
2116 error ("%H%<::%E%> has not been declared", &location, name);
2117 else if (parser->object_scope
2118 && !CLASS_TYPE_P (parser->object_scope))
2119 error ("%Hrequest for member %qE in non-class type %qT",
2120 &location, name, parser->object_scope);
2121 else if (parser->object_scope)
2122 error ("%H%<%T::%E%> has not been declared",
2123 &location, parser->object_scope, name);
2125 error ("%H%qE has not been declared", &location, name);
2127 else if (parser->scope && parser->scope != global_namespace)
2128 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2129 else if (parser->scope == global_namespace)
2130 error ("%H%<::%E%> %s", &location, name, desired);
2132 error ("%H%qE %s", &location, name, desired);
2135 /* If we are parsing tentatively, remember that an error has occurred
2136 during this tentative parse. Returns true if the error was
2137 simulated; false if a message should be issued by the caller. */
2140 cp_parser_simulate_error (cp_parser* parser)
2142 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2144 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2150 /* Check for repeated decl-specifiers. */
2153 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2154 location_t location)
2158 for (ds = ds_first; ds != ds_last; ++ds)
2160 unsigned count = decl_specs->specs[ds];
2163 /* The "long" specifier is a special case because of "long long". */
2167 error ("%H%<long long long%> is too long for GCC", &location);
2169 pedwarn_cxx98 (location, OPT_Wlong_long,
2170 "ISO C++ 1998 does not support %<long long%>");
2174 static const char *const decl_spec_names[] = {
2190 error ("%Hduplicate %qs", &location, decl_spec_names[ds]);
2195 /* This function is called when a type is defined. If type
2196 definitions are forbidden at this point, an error message is
2200 cp_parser_check_type_definition (cp_parser* parser)
2202 /* If types are forbidden here, issue a message. */
2203 if (parser->type_definition_forbidden_message)
2205 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206 in the message need to be interpreted. */
2207 error (parser->type_definition_forbidden_message);
2213 /* This function is called when the DECLARATOR is processed. The TYPE
2214 was a type defined in the decl-specifiers. If it is invalid to
2215 define a type in the decl-specifiers for DECLARATOR, an error is
2216 issued. TYPE_LOCATION is the location of TYPE and is used
2217 for error reporting. */
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221 tree type, location_t type_location)
2223 /* [dcl.fct] forbids type definitions in return types.
2224 Unfortunately, it's not easy to know whether or not we are
2225 processing a return type until after the fact. */
2227 && (declarator->kind == cdk_pointer
2228 || declarator->kind == cdk_reference
2229 || declarator->kind == cdk_ptrmem))
2230 declarator = declarator->declarator;
2232 && declarator->kind == cdk_function)
2234 error ("%Hnew types may not be defined in a return type", &type_location);
2235 inform (type_location,
2236 "(perhaps a semicolon is missing after the definition of %qT)",
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242 "<" in any valid C++ program. If the next token is indeed "<",
2243 issue a message warning the user about what appears to be an
2244 invalid attempt to form a template-id. LOCATION is the location
2245 of the type-specifier (TYPE) */
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249 tree type, location_t location)
2251 cp_token_position start = 0;
2253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2256 error ("%H%qT is not a template", &location, type);
2257 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258 error ("%H%qE is not a template", &location, type);
2260 error ("%Hinvalid template-id", &location);
2261 /* Remember the location of the invalid "<". */
2262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263 start = cp_lexer_token_position (parser->lexer, true);
2264 /* Consume the "<". */
2265 cp_lexer_consume_token (parser->lexer);
2266 /* Parse the template arguments. */
2267 cp_parser_enclosed_template_argument_list (parser);
2268 /* Permanently remove the invalid template arguments so that
2269 this error message is not issued again. */
2271 cp_lexer_purge_tokens_after (parser->lexer, start);
2275 /* If parsing an integral constant-expression, issue an error message
2276 about the fact that THING appeared and return true. Otherwise,
2277 return false. In either case, set
2278 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2281 cp_parser_non_integral_constant_expression (cp_parser *parser,
2284 parser->non_integral_constant_expression_p = true;
2285 if (parser->integral_constant_expression_p)
2287 if (!parser->allow_non_integral_constant_expression_p)
2289 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290 in the message need to be interpreted. */
2291 char *message = concat (thing,
2292 " cannot appear in a constant-expression",
2302 /* Emit a diagnostic for an invalid type name. SCOPE is the
2303 qualifying scope (or NULL, if none) for ID. This function commits
2304 to the current active tentative parse, if any. (Otherwise, the
2305 problematic construct might be encountered again later, resulting
2306 in duplicate error messages.) LOCATION is the location of ID. */
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310 tree scope, tree id,
2311 location_t location)
2313 tree decl, old_scope;
2314 /* Try to lookup the identifier. */
2315 old_scope = parser->scope;
2316 parser->scope = scope;
2317 decl = cp_parser_lookup_name_simple (parser, id, location);
2318 parser->scope = old_scope;
2319 /* If the lookup found a template-name, it means that the user forgot
2320 to specify an argument list. Emit a useful error message. */
2321 if (TREE_CODE (decl) == TEMPLATE_DECL)
2322 error ("%Hinvalid use of template-name %qE without an argument list",
2324 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326 else if (TREE_CODE (decl) == TYPE_DECL)
2327 /* Something like 'unsigned A a;' */
2328 error ("%Hinvalid combination of multiple type-specifiers",
2330 else if (!parser->scope)
2332 /* Issue an error message. */
2333 error ("%H%qE does not name a type", &location, id);
2334 /* If we're in a template class, it's possible that the user was
2335 referring to a type from a base class. For example:
2337 template <typename T> struct A { typedef T X; };
2338 template <typename T> struct B : public A<T> { X x; };
2340 The user should have said "typename A<T>::X". */
2341 if (processing_template_decl && current_class_type
2342 && TYPE_BINFO (current_class_type))
2346 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2350 tree base_type = BINFO_TYPE (b);
2351 if (CLASS_TYPE_P (base_type)
2352 && dependent_type_p (base_type))
2355 /* Go from a particular instantiation of the
2356 template (which will have an empty TYPE_FIELDs),
2357 to the main version. */
2358 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359 for (field = TYPE_FIELDS (base_type);
2361 field = TREE_CHAIN (field))
2362 if (TREE_CODE (field) == TYPE_DECL
2363 && DECL_NAME (field) == id)
2366 "(perhaps %<typename %T::%E%> was intended)",
2367 BINFO_TYPE (b), id);
2376 /* Here we diagnose qualified-ids where the scope is actually correct,
2377 but the identifier does not resolve to a valid type name. */
2378 else if (parser->scope != error_mark_node)
2380 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381 error ("%H%qE in namespace %qE does not name a type",
2382 &location, id, parser->scope);
2383 else if (TYPE_P (parser->scope))
2384 error ("%H%qE in class %qT does not name a type",
2385 &location, id, parser->scope);
2389 cp_parser_commit_to_tentative_parse (parser);
2392 /* Check for a common situation where a type-name should be present,
2393 but is not, and issue a sensible error message. Returns true if an
2394 invalid type-name was detected.
2396 The situation handled by this function are variable declarations of the
2397 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398 Usually, `ID' should name a type, but if we got here it means that it
2399 does not. We try to emit the best possible error message depending on
2400 how exactly the id-expression looks like. */
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2406 cp_token *token = cp_lexer_peek_token (parser->lexer);
2408 cp_parser_parse_tentatively (parser);
2409 id = cp_parser_id_expression (parser,
2410 /*template_keyword_p=*/false,
2411 /*check_dependency_p=*/true,
2412 /*template_p=*/NULL,
2413 /*declarator_p=*/true,
2414 /*optional_p=*/false);
2415 /* After the id-expression, there should be a plain identifier,
2416 otherwise this is not a simple variable declaration. Also, if
2417 the scope is dependent, we cannot do much. */
2418 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419 || (parser->scope && TYPE_P (parser->scope)
2420 && dependent_type_p (parser->scope))
2421 || TREE_CODE (id) == TYPE_DECL)
2423 cp_parser_abort_tentative_parse (parser);
2426 if (!cp_parser_parse_definitely (parser))
2429 /* Emit a diagnostic for the invalid type. */
2430 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431 id, token->location);
2432 /* Skip to the end of the declaration; there's no point in
2433 trying to process it. */
2434 cp_parser_skip_to_end_of_block_or_statement (parser);
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2440 are doing error recovery. Returns -1 if OR_COMMA is true and we
2441 found an unnested comma. */
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2449 unsigned paren_depth = 0;
2450 unsigned brace_depth = 0;
2452 if (recovering && !or_comma
2453 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2458 cp_token * token = cp_lexer_peek_token (parser->lexer);
2460 switch (token->type)
2463 case CPP_PRAGMA_EOL:
2464 /* If we've run out of tokens, then there is no closing `)'. */
2468 /* This matches the processing in skip_to_end_of_statement. */
2473 case CPP_OPEN_BRACE:
2476 case CPP_CLOSE_BRACE:
2482 if (recovering && or_comma && !brace_depth && !paren_depth)
2486 case CPP_OPEN_PAREN:
2491 case CPP_CLOSE_PAREN:
2492 if (!brace_depth && !paren_depth--)
2495 cp_lexer_consume_token (parser->lexer);
2504 /* Consume the token. */
2505 cp_lexer_consume_token (parser->lexer);
2509 /* Consume tokens until we reach the end of the current statement.
2510 Normally, that will be just before consuming a `;'. However, if a
2511 non-nested `}' comes first, then we stop before consuming that. */
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2516 unsigned nesting_depth = 0;
2520 cp_token *token = cp_lexer_peek_token (parser->lexer);
2522 switch (token->type)
2525 case CPP_PRAGMA_EOL:
2526 /* If we've run out of tokens, stop. */
2530 /* If the next token is a `;', we have reached the end of the
2536 case CPP_CLOSE_BRACE:
2537 /* If this is a non-nested '}', stop before consuming it.
2538 That way, when confronted with something like:
2542 we stop before consuming the closing '}', even though we
2543 have not yet reached a `;'. */
2544 if (nesting_depth == 0)
2547 /* If it is the closing '}' for a block that we have
2548 scanned, stop -- but only after consuming the token.
2554 we will stop after the body of the erroneously declared
2555 function, but before consuming the following `typedef'
2557 if (--nesting_depth == 0)
2559 cp_lexer_consume_token (parser->lexer);
2563 case CPP_OPEN_BRACE:
2571 /* Consume the token. */
2572 cp_lexer_consume_token (parser->lexer);
2576 /* This function is called at the end of a statement or declaration.
2577 If the next token is a semicolon, it is consumed; otherwise, error
2578 recovery is attempted. */
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2583 /* Look for the trailing `;'. */
2584 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2586 /* If there is additional (erroneous) input, skip to the end of
2588 cp_parser_skip_to_end_of_statement (parser);
2589 /* If the next token is now a `;', consume it. */
2590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591 cp_lexer_consume_token (parser->lexer);
2595 /* Skip tokens until we have consumed an entire block, or until we
2596 have consumed a non-nested `;'. */
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2601 int nesting_depth = 0;
2603 while (nesting_depth >= 0)
2605 cp_token *token = cp_lexer_peek_token (parser->lexer);
2607 switch (token->type)
2610 case CPP_PRAGMA_EOL:
2611 /* If we've run out of tokens, stop. */
2615 /* Stop if this is an unnested ';'. */
2620 case CPP_CLOSE_BRACE:
2621 /* Stop if this is an unnested '}', or closes the outermost
2624 if (nesting_depth < 0)
2630 case CPP_OPEN_BRACE:
2639 /* Consume the token. */
2640 cp_lexer_consume_token (parser->lexer);
2644 /* Skip tokens until a non-nested closing curly brace is the next
2645 token, or there are no more tokens. Return true in the first case,
2649 cp_parser_skip_to_closing_brace (cp_parser *parser)
2651 unsigned nesting_depth = 0;
2655 cp_token *token = cp_lexer_peek_token (parser->lexer);
2657 switch (token->type)
2660 case CPP_PRAGMA_EOL:
2661 /* If we've run out of tokens, stop. */
2664 case CPP_CLOSE_BRACE:
2665 /* If the next token is a non-nested `}', then we have reached
2666 the end of the current block. */
2667 if (nesting_depth-- == 0)
2671 case CPP_OPEN_BRACE:
2672 /* If it the next token is a `{', then we are entering a new
2673 block. Consume the entire block. */
2681 /* Consume the token. */
2682 cp_lexer_consume_token (parser->lexer);
2686 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2687 parameter is the PRAGMA token, allowing us to purge the entire pragma
2691 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2695 parser->lexer->in_pragma = false;
2698 token = cp_lexer_consume_token (parser->lexer);
2699 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2701 /* Ensure that the pragma is not parsed again. */
2702 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2705 /* Require pragma end of line, resyncing with it as necessary. The
2706 arguments are as for cp_parser_skip_to_pragma_eol. */
2709 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2711 parser->lexer->in_pragma = false;
2712 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2713 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2716 /* This is a simple wrapper around make_typename_type. When the id is
2717 an unresolved identifier node, we can provide a superior diagnostic
2718 using cp_parser_diagnose_invalid_type_name. */
2721 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2722 tree id, location_t id_location)
2725 if (TREE_CODE (id) == IDENTIFIER_NODE)
2727 result = make_typename_type (scope, id, typename_type,
2728 /*complain=*/tf_none);
2729 if (result == error_mark_node)
2730 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2733 return make_typename_type (scope, id, typename_type, tf_error);
2736 /* This is a wrapper around the
2737 make_{pointer,ptrmem,reference}_declarator functions that decides
2738 which one to call based on the CODE and CLASS_TYPE arguments. The
2739 CODE argument should be one of the values returned by
2740 cp_parser_ptr_operator. */
2741 static cp_declarator *
2742 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2743 cp_cv_quals cv_qualifiers,
2744 cp_declarator *target)
2746 if (code == ERROR_MARK)
2747 return cp_error_declarator;
2749 if (code == INDIRECT_REF)
2750 if (class_type == NULL_TREE)
2751 return make_pointer_declarator (cv_qualifiers, target);
2753 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2754 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2755 return make_reference_declarator (cv_qualifiers, target, false);
2756 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2757 return make_reference_declarator (cv_qualifiers, target, true);
2761 /* Create a new C++ parser. */
2764 cp_parser_new (void)
2770 /* cp_lexer_new_main is called before calling ggc_alloc because
2771 cp_lexer_new_main might load a PCH file. */
2772 lexer = cp_lexer_new_main ();
2774 /* Initialize the binops_by_token so that we can get the tree
2775 directly from the token. */
2776 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2777 binops_by_token[binops[i].token_type] = binops[i];
2779 parser = GGC_CNEW (cp_parser);
2780 parser->lexer = lexer;
2781 parser->context = cp_parser_context_new (NULL);
2783 /* For now, we always accept GNU extensions. */
2784 parser->allow_gnu_extensions_p = 1;
2786 /* The `>' token is a greater-than operator, not the end of a
2788 parser->greater_than_is_operator_p = true;
2790 parser->default_arg_ok_p = true;
2792 /* We are not parsing a constant-expression. */
2793 parser->integral_constant_expression_p = false;
2794 parser->allow_non_integral_constant_expression_p = false;
2795 parser->non_integral_constant_expression_p = false;
2797 /* Local variable names are not forbidden. */
2798 parser->local_variables_forbidden_p = false;
2800 /* We are not processing an `extern "C"' declaration. */
2801 parser->in_unbraced_linkage_specification_p = false;
2803 /* We are not processing a declarator. */
2804 parser->in_declarator_p = false;
2806 /* We are not processing a template-argument-list. */
2807 parser->in_template_argument_list_p = false;
2809 /* We are not in an iteration statement. */
2810 parser->in_statement = 0;
2812 /* We are not in a switch statement. */
2813 parser->in_switch_statement_p = false;
2815 /* We are not parsing a type-id inside an expression. */
2816 parser->in_type_id_in_expr_p = false;
2818 /* Declarations aren't implicitly extern "C". */
2819 parser->implicit_extern_c = false;
2821 /* String literals should be translated to the execution character set. */
2822 parser->translate_strings_p = true;
2824 /* We are not parsing a function body. */
2825 parser->in_function_body = false;
2827 /* The unparsed function queue is empty. */
2828 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2830 /* There are no classes being defined. */
2831 parser->num_classes_being_defined = 0;
2833 /* No template parameters apply. */
2834 parser->num_template_parameter_lists = 0;
2839 /* Create a cp_lexer structure which will emit the tokens in CACHE
2840 and push it onto the parser's lexer stack. This is used for delayed
2841 parsing of in-class method bodies and default arguments, and should
2842 not be confused with tentative parsing. */
2844 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2846 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2847 lexer->next = parser->lexer;
2848 parser->lexer = lexer;
2850 /* Move the current source position to that of the first token in the
2852 cp_lexer_set_source_position_from_token (lexer->next_token);
2855 /* Pop the top lexer off the parser stack. This is never used for the
2856 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2858 cp_parser_pop_lexer (cp_parser *parser)
2860 cp_lexer *lexer = parser->lexer;
2861 parser->lexer = lexer->next;
2862 cp_lexer_destroy (lexer);
2864 /* Put the current source position back where it was before this
2865 lexer was pushed. */
2866 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2869 /* Lexical conventions [gram.lex] */
2871 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2875 cp_parser_identifier (cp_parser* parser)
2879 /* Look for the identifier. */
2880 token = cp_parser_require (parser, CPP_NAME, "identifier");
2881 /* Return the value. */
2882 return token ? token->u.value : error_mark_node;
2885 /* Parse a sequence of adjacent string constants. Returns a
2886 TREE_STRING representing the combined, nul-terminated string
2887 constant. If TRANSLATE is true, translate the string to the
2888 execution character set. If WIDE_OK is true, a wide string is
2891 C++98 [lex.string] says that if a narrow string literal token is
2892 adjacent to a wide string literal token, the behavior is undefined.
2893 However, C99 6.4.5p4 says that this results in a wide string literal.
2894 We follow C99 here, for consistency with the C front end.
2896 This code is largely lifted from lex_string() in c-lex.c.
2898 FUTURE: ObjC++ will need to handle @-strings here. */
2900 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2904 struct obstack str_ob;
2905 cpp_string str, istr, *strs;
2907 enum cpp_ttype type;
2909 tok = cp_lexer_peek_token (parser->lexer);
2910 if (!cp_parser_is_string_literal (tok))
2912 cp_parser_error (parser, "expected string-literal");
2913 return error_mark_node;
2918 /* Try to avoid the overhead of creating and destroying an obstack
2919 for the common case of just one string. */
2920 if (!cp_parser_is_string_literal
2921 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2923 cp_lexer_consume_token (parser->lexer);
2925 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2926 str.len = TREE_STRING_LENGTH (tok->u.value);
2933 gcc_obstack_init (&str_ob);
2938 cp_lexer_consume_token (parser->lexer);
2940 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2941 str.len = TREE_STRING_LENGTH (tok->u.value);
2943 if (type != tok->type)
2945 if (type == CPP_STRING)
2947 else if (tok->type != CPP_STRING)
2948 error ("%Hunsupported non-standard concatenation "
2949 "of string literals", &tok->location);
2952 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2954 tok = cp_lexer_peek_token (parser->lexer);
2956 while (cp_parser_is_string_literal (tok));
2958 strs = (cpp_string *) obstack_finish (&str_ob);
2961 if (type != CPP_STRING && !wide_ok)
2963 cp_parser_error (parser, "a wide string is invalid in this context");
2967 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2968 (parse_in, strs, count, &istr, type))
2970 value = build_string (istr.len, (const char *)istr.text);
2971 free (CONST_CAST (unsigned char *, istr.text));
2977 TREE_TYPE (value) = char_array_type_node;
2980 TREE_TYPE (value) = char16_array_type_node;
2983 TREE_TYPE (value) = char32_array_type_node;
2986 TREE_TYPE (value) = wchar_array_type_node;
2990 value = fix_string_type (value);
2993 /* cpp_interpret_string has issued an error. */
2994 value = error_mark_node;
2997 obstack_free (&str_ob, 0);
3003 /* Basic concepts [gram.basic] */
3005 /* Parse a translation-unit.
3008 declaration-seq [opt]
3010 Returns TRUE if all went well. */
3013 cp_parser_translation_unit (cp_parser* parser)
3015 /* The address of the first non-permanent object on the declarator
3017 static void *declarator_obstack_base;
3021 /* Create the declarator obstack, if necessary. */
3022 if (!cp_error_declarator)
3024 gcc_obstack_init (&declarator_obstack);
3025 /* Create the error declarator. */
3026 cp_error_declarator = make_declarator (cdk_error);
3027 /* Create the empty parameter list. */
3028 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3029 /* Remember where the base of the declarator obstack lies. */
3030 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3033 cp_parser_declaration_seq_opt (parser);
3035 /* If there are no tokens left then all went well. */
3036 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3038 /* Get rid of the token array; we don't need it any more. */
3039 cp_lexer_destroy (parser->lexer);
3040 parser->lexer = NULL;
3042 /* This file might have been a context that's implicitly extern
3043 "C". If so, pop the lang context. (Only relevant for PCH.) */
3044 if (parser->implicit_extern_c)
3046 pop_lang_context ();
3047 parser->implicit_extern_c = false;
3051 finish_translation_unit ();
3057 cp_parser_error (parser, "expected declaration");
3061 /* Make sure the declarator obstack was fully cleaned up. */
3062 gcc_assert (obstack_next_free (&declarator_obstack)
3063 == declarator_obstack_base);
3065 /* All went well. */
3069 /* Expressions [gram.expr] */
3071 /* Parse a primary-expression.
3082 ( compound-statement )
3083 __builtin_va_arg ( assignment-expression , type-id )
3084 __builtin_offsetof ( type-id , offsetof-expression )
3087 __has_nothrow_assign ( type-id )
3088 __has_nothrow_constructor ( type-id )
3089 __has_nothrow_copy ( type-id )
3090 __has_trivial_assign ( type-id )
3091 __has_trivial_constructor ( type-id )
3092 __has_trivial_copy ( type-id )
3093 __has_trivial_destructor ( type-id )
3094 __has_virtual_destructor ( type-id )
3095 __is_abstract ( type-id )
3096 __is_base_of ( type-id , type-id )
3097 __is_class ( type-id )
3098 __is_convertible_to ( type-id , type-id )
3099 __is_empty ( type-id )
3100 __is_enum ( type-id )
3101 __is_pod ( type-id )
3102 __is_polymorphic ( type-id )
3103 __is_union ( type-id )
3105 Objective-C++ Extension:
3113 ADDRESS_P is true iff this expression was immediately preceded by
3114 "&" and therefore might denote a pointer-to-member. CAST_P is true
3115 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3116 true iff this expression is a template argument.
3118 Returns a representation of the expression. Upon return, *IDK
3119 indicates what kind of id-expression (if any) was present. */
3122 cp_parser_primary_expression (cp_parser *parser,
3125 bool template_arg_p,
3128 cp_token *token = NULL;
3130 /* Assume the primary expression is not an id-expression. */
3131 *idk = CP_ID_KIND_NONE;
3133 /* Peek at the next token. */
3134 token = cp_lexer_peek_token (parser->lexer);
3135 switch (token->type)
3148 token = cp_lexer_consume_token (parser->lexer);
3149 if (TREE_CODE (token->u.value) == FIXED_CST)
3151 error ("%Hfixed-point types not supported in C++",
3153 return error_mark_node;
3155 /* Floating-point literals are only allowed in an integral
3156 constant expression if they are cast to an integral or
3157 enumeration type. */
3158 if (TREE_CODE (token->u.value) == REAL_CST
3159 && parser->integral_constant_expression_p
3162 /* CAST_P will be set even in invalid code like "int(2.7 +
3163 ...)". Therefore, we have to check that the next token
3164 is sure to end the cast. */
3167 cp_token *next_token;
3169 next_token = cp_lexer_peek_token (parser->lexer);
3170 if (/* The comma at the end of an
3171 enumerator-definition. */
3172 next_token->type != CPP_COMMA
3173 /* The curly brace at the end of an enum-specifier. */
3174 && next_token->type != CPP_CLOSE_BRACE
3175 /* The end of a statement. */
3176 && next_token->type != CPP_SEMICOLON
3177 /* The end of the cast-expression. */
3178 && next_token->type != CPP_CLOSE_PAREN
3179 /* The end of an array bound. */
3180 && next_token->type != CPP_CLOSE_SQUARE
3181 /* The closing ">" in a template-argument-list. */
3182 && (next_token->type != CPP_GREATER
3183 || parser->greater_than_is_operator_p)
3184 /* C++0x only: A ">>" treated like two ">" tokens,
3185 in a template-argument-list. */
3186 && (next_token->type != CPP_RSHIFT
3187 || (cxx_dialect == cxx98)
3188 || parser->greater_than_is_operator_p))
3192 /* If we are within a cast, then the constraint that the
3193 cast is to an integral or enumeration type will be
3194 checked at that point. If we are not within a cast, then
3195 this code is invalid. */
3197 cp_parser_non_integral_constant_expression
3198 (parser, "floating-point literal");
3200 return token->u.value;
3206 /* ??? Should wide strings be allowed when parser->translate_strings_p
3207 is false (i.e. in attributes)? If not, we can kill the third
3208 argument to cp_parser_string_literal. */
3209 return cp_parser_string_literal (parser,
3210 parser->translate_strings_p,
3213 case CPP_OPEN_PAREN:
3216 bool saved_greater_than_is_operator_p;
3218 /* Consume the `('. */
3219 cp_lexer_consume_token (parser->lexer);
3220 /* Within a parenthesized expression, a `>' token is always
3221 the greater-than operator. */
3222 saved_greater_than_is_operator_p
3223 = parser->greater_than_is_operator_p;
3224 parser->greater_than_is_operator_p = true;
3225 /* If we see `( { ' then we are looking at the beginning of
3226 a GNU statement-expression. */
3227 if (cp_parser_allow_gnu_extensions_p (parser)
3228 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3230 /* Statement-expressions are not allowed by the standard. */
3231 pedwarn (token->location, OPT_pedantic,
3232 "ISO C++ forbids braced-groups within expressions");
3234 /* And they're not allowed outside of a function-body; you
3235 cannot, for example, write:
3237 int i = ({ int j = 3; j + 1; });
3239 at class or namespace scope. */
3240 if (!parser->in_function_body
3241 || parser->in_template_argument_list_p)
3243 error ("%Hstatement-expressions are not allowed outside "
3244 "functions nor in template-argument lists",
3246 cp_parser_skip_to_end_of_block_or_statement (parser);
3247 expr = error_mark_node;
3251 /* Start the statement-expression. */
3252 expr = begin_stmt_expr ();
3253 /* Parse the compound-statement. */
3254 cp_parser_compound_statement (parser, expr, false);
3256 expr = finish_stmt_expr (expr, false);
3261 /* Parse the parenthesized expression. */
3262 expr = cp_parser_expression (parser, cast_p, idk);
3263 /* Let the front end know that this expression was
3264 enclosed in parentheses. This matters in case, for
3265 example, the expression is of the form `A::B', since
3266 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3268 finish_parenthesized_expr (expr);
3270 /* The `>' token might be the end of a template-id or
3271 template-parameter-list now. */
3272 parser->greater_than_is_operator_p
3273 = saved_greater_than_is_operator_p;
3274 /* Consume the `)'. */
3275 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3276 cp_parser_skip_to_end_of_statement (parser);
3282 switch (token->keyword)
3284 /* These two are the boolean literals. */
3286 cp_lexer_consume_token (parser->lexer);
3287 return boolean_true_node;
3289 cp_lexer_consume_token (parser->lexer);
3290 return boolean_false_node;
3292 /* The `__null' literal. */
3294 cp_lexer_consume_token (parser->lexer);
3297 /* Recognize the `this' keyword. */
3299 cp_lexer_consume_token (parser->lexer);
3300 if (parser->local_variables_forbidden_p)
3302 error ("%H%<this%> may not be used in this context",
3304 return error_mark_node;
3306 /* Pointers cannot appear in constant-expressions. */
3307 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3308 return error_mark_node;
3309 return finish_this_expr ();
3311 /* The `operator' keyword can be the beginning of an
3316 case RID_FUNCTION_NAME:
3317 case RID_PRETTY_FUNCTION_NAME:
3318 case RID_C99_FUNCTION_NAME:
3322 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3323 __func__ are the names of variables -- but they are
3324 treated specially. Therefore, they are handled here,
3325 rather than relying on the generic id-expression logic
3326 below. Grammatically, these names are id-expressions.
3328 Consume the token. */
3329 token = cp_lexer_consume_token (parser->lexer);
3331 switch (token->keyword)
3333 case RID_FUNCTION_NAME:
3334 name = "%<__FUNCTION__%>";
3336 case RID_PRETTY_FUNCTION_NAME:
3337 name = "%<__PRETTY_FUNCTION__%>";
3339 case RID_C99_FUNCTION_NAME:
3340 name = "%<__func__%>";
3346 if (cp_parser_non_integral_constant_expression (parser, name))
3347 return error_mark_node;
3349 /* Look up the name. */
3350 return finish_fname (token->u.value);
3358 /* The `__builtin_va_arg' construct is used to handle
3359 `va_arg'. Consume the `__builtin_va_arg' token. */
3360 cp_lexer_consume_token (parser->lexer);
3361 /* Look for the opening `('. */
3362 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3363 /* Now, parse the assignment-expression. */
3364 expression = cp_parser_assignment_expression (parser,
3365 /*cast_p=*/false, NULL);
3366 /* Look for the `,'. */
3367 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3368 /* Parse the type-id. */
3369 type = cp_parser_type_id (parser);
3370 /* Look for the closing `)'. */
3371 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3372 /* Using `va_arg' in a constant-expression is not
3374 if (cp_parser_non_integral_constant_expression (parser,
3376 return error_mark_node;
3377 return build_x_va_arg (expression, type);
3381 return cp_parser_builtin_offsetof (parser);
3383 case RID_HAS_NOTHROW_ASSIGN:
3384 case RID_HAS_NOTHROW_CONSTRUCTOR:
3385 case RID_HAS_NOTHROW_COPY:
3386 case RID_HAS_TRIVIAL_ASSIGN:
3387 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3388 case RID_HAS_TRIVIAL_COPY:
3389 case RID_HAS_TRIVIAL_DESTRUCTOR:
3390 case RID_HAS_VIRTUAL_DESTRUCTOR:
3391 case RID_IS_ABSTRACT:
3392 case RID_IS_BASE_OF:
3394 case RID_IS_CONVERTIBLE_TO:
3398 case RID_IS_POLYMORPHIC:
3400 return cp_parser_trait_expr (parser, token->keyword);
3402 /* Objective-C++ expressions. */
3404 case RID_AT_PROTOCOL:
3405 case RID_AT_SELECTOR:
3406 return cp_parser_objc_expression (parser);
3409 cp_parser_error (parser, "expected primary-expression");
3410 return error_mark_node;
3413 /* An id-expression can start with either an identifier, a
3414 `::' as the beginning of a qualified-id, or the "operator"
3418 case CPP_TEMPLATE_ID:
3419 case CPP_NESTED_NAME_SPECIFIER:
3423 const char *error_msg;
3426 cp_token *id_expr_token;
3429 /* Parse the id-expression. */
3431 = cp_parser_id_expression (parser,
3432 /*template_keyword_p=*/false,
3433 /*check_dependency_p=*/true,
3435 /*declarator_p=*/false,
3436 /*optional_p=*/false);
3437 if (id_expression == error_mark_node)
3438 return error_mark_node;
3439 id_expr_token = token;
3440 token = cp_lexer_peek_token (parser->lexer);
3441 done = (token->type != CPP_OPEN_SQUARE
3442 && token->type != CPP_OPEN_PAREN
3443 && token->type != CPP_DOT
3444 && token->type != CPP_DEREF
3445 && token->type != CPP_PLUS_PLUS
3446 && token->type != CPP_MINUS_MINUS);
3447 /* If we have a template-id, then no further lookup is
3448 required. If the template-id was for a template-class, we
3449 will sometimes have a TYPE_DECL at this point. */
3450 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3451 || TREE_CODE (id_expression) == TYPE_DECL)
3452 decl = id_expression;
3453 /* Look up the name. */
3456 tree ambiguous_decls;
3458 decl = cp_parser_lookup_name (parser, id_expression,
3461 /*is_namespace=*/false,
3462 /*check_dependency=*/true,
3464 id_expr_token->location);
3465 /* If the lookup was ambiguous, an error will already have
3467 if (ambiguous_decls)
3468 return error_mark_node;
3470 /* In Objective-C++, an instance variable (ivar) may be preferred
3471 to whatever cp_parser_lookup_name() found. */
3472 decl = objc_lookup_ivar (decl, id_expression);
3474 /* If name lookup gives us a SCOPE_REF, then the
3475 qualifying scope was dependent. */
3476 if (TREE_CODE (decl) == SCOPE_REF)
3478 /* At this point, we do not know if DECL is a valid
3479 integral constant expression. We assume that it is
3480 in fact such an expression, so that code like:
3482 template <int N> struct A {
3486 is accepted. At template-instantiation time, we
3487 will check that B<N>::i is actually a constant. */
3490 /* Check to see if DECL is a local variable in a context
3491 where that is forbidden. */
3492 if (parser->local_variables_forbidden_p
3493 && local_variable_p (decl))
3495 /* It might be that we only found DECL because we are
3496 trying to be generous with pre-ISO scoping rules.
3497 For example, consider:
3501 for (int i = 0; i < 10; ++i) {}
3502 extern void f(int j = i);
3505 Here, name look up will originally find the out
3506 of scope `i'. We need to issue a warning message,
3507 but then use the global `i'. */
3508 decl = check_for_out_of_scope_variable (decl);
3509 if (local_variable_p (decl))
3511 error ("%Hlocal variable %qD may not appear in this context",
3512 &id_expr_token->location, decl);
3513 return error_mark_node;
3518 decl = (finish_id_expression
3519 (id_expression, decl, parser->scope,
3521 parser->integral_constant_expression_p,
3522 parser->allow_non_integral_constant_expression_p,
3523 &parser->non_integral_constant_expression_p,
3524 template_p, done, address_p,
3527 id_expr_token->location));
3529 cp_parser_error (parser, error_msg);
3533 /* Anything else is an error. */
3535 /* ...unless we have an Objective-C++ message or string literal,
3537 if (c_dialect_objc ()
3538 && (token->type == CPP_OPEN_SQUARE
3539 || token->type == CPP_OBJC_STRING))
3540 return cp_parser_objc_expression (parser);
3542 cp_parser_error (parser, "expected primary-expression");
3543 return error_mark_node;
3547 /* Parse an id-expression.
3554 :: [opt] nested-name-specifier template [opt] unqualified-id
3556 :: operator-function-id
3559 Return a representation of the unqualified portion of the
3560 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3561 a `::' or nested-name-specifier.
3563 Often, if the id-expression was a qualified-id, the caller will
3564 want to make a SCOPE_REF to represent the qualified-id. This
3565 function does not do this in order to avoid wastefully creating
3566 SCOPE_REFs when they are not required.
3568 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3571 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3572 uninstantiated templates.
3574 If *TEMPLATE_P is non-NULL, it is set to true iff the
3575 `template' keyword is used to explicitly indicate that the entity
3576 named is a template.
3578 If DECLARATOR_P is true, the id-expression is appearing as part of
3579 a declarator, rather than as part of an expression. */
3582 cp_parser_id_expression (cp_parser *parser,
3583 bool template_keyword_p,
3584 bool check_dependency_p,
3589 bool global_scope_p;
3590 bool nested_name_specifier_p;
3592 /* Assume the `template' keyword was not used. */
3594 *template_p = template_keyword_p;
3596 /* Look for the optional `::' operator. */
3598 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3600 /* Look for the optional nested-name-specifier. */
3601 nested_name_specifier_p
3602 = (cp_parser_nested_name_specifier_opt (parser,
3603 /*typename_keyword_p=*/false,
3608 /* If there is a nested-name-specifier, then we are looking at
3609 the first qualified-id production. */
3610 if (nested_name_specifier_p)
3613 tree saved_object_scope;
3614 tree saved_qualifying_scope;
3615 tree unqualified_id;
3618 /* See if the next token is the `template' keyword. */
3620 template_p = &is_template;
3621 *template_p = cp_parser_optional_template_keyword (parser);
3622 /* Name lookup we do during the processing of the
3623 unqualified-id might obliterate SCOPE. */
3624 saved_scope = parser->scope;
3625 saved_object_scope = parser->object_scope;
3626 saved_qualifying_scope = parser->qualifying_scope;
3627 /* Process the final unqualified-id. */
3628 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3631 /*optional_p=*/false);
3632 /* Restore the SAVED_SCOPE for our caller. */
3633 parser->scope = saved_scope;
3634 parser->object_scope = saved_object_scope;
3635 parser->qualifying_scope = saved_qualifying_scope;
3637 return unqualified_id;
3639 /* Otherwise, if we are in global scope, then we are looking at one
3640 of the other qualified-id productions. */
3641 else if (global_scope_p)
3646 /* Peek at the next token. */
3647 token = cp_lexer_peek_token (parser->lexer);
3649 /* If it's an identifier, and the next token is not a "<", then
3650 we can avoid the template-id case. This is an optimization
3651 for this common case. */
3652 if (token->type == CPP_NAME
3653 && !cp_parser_nth_token_starts_template_argument_list_p
3655 return cp_parser_identifier (parser);
3657 cp_parser_parse_tentatively (parser);
3658 /* Try a template-id. */
3659 id = cp_parser_template_id (parser,
3660 /*template_keyword_p=*/false,
3661 /*check_dependency_p=*/true,
3663 /* If that worked, we're done. */
3664 if (cp_parser_parse_definitely (parser))
3667 /* Peek at the next token. (Changes in the token buffer may
3668 have invalidated the pointer obtained above.) */
3669 token = cp_lexer_peek_token (parser->lexer);
3671 switch (token->type)
3674 return cp_parser_identifier (parser);
3677 if (token->keyword == RID_OPERATOR)
3678 return cp_parser_operator_function_id (parser);
3682 cp_parser_error (parser, "expected id-expression");
3683 return error_mark_node;
3687 return cp_parser_unqualified_id (parser, template_keyword_p,
3688 /*check_dependency_p=*/true,
3693 /* Parse an unqualified-id.
3697 operator-function-id
3698 conversion-function-id
3702 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3703 keyword, in a construct like `A::template ...'.
3705 Returns a representation of unqualified-id. For the `identifier'
3706 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3707 production a BIT_NOT_EXPR is returned; the operand of the
3708 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3709 other productions, see the documentation accompanying the
3710 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3711 names are looked up in uninstantiated templates. If DECLARATOR_P
3712 is true, the unqualified-id is appearing as part of a declarator,
3713 rather than as part of an expression. */
3716 cp_parser_unqualified_id (cp_parser* parser,
3717 bool template_keyword_p,
3718 bool check_dependency_p,
3724 /* Peek at the next token. */
3725 token = cp_lexer_peek_token (parser->lexer);
3727 switch (token->type)
3733 /* We don't know yet whether or not this will be a
3735 cp_parser_parse_tentatively (parser);
3736 /* Try a template-id. */
3737 id = cp_parser_template_id (parser, template_keyword_p,
3740 /* If it worked, we're done. */
3741 if (cp_parser_parse_definitely (parser))
3743 /* Otherwise, it's an ordinary identifier. */
3744 return cp_parser_identifier (parser);
3747 case CPP_TEMPLATE_ID:
3748 return cp_parser_template_id (parser, template_keyword_p,
3755 tree qualifying_scope;
3760 /* Consume the `~' token. */
3761 cp_lexer_consume_token (parser->lexer);
3762 /* Parse the class-name. The standard, as written, seems to
3765 template <typename T> struct S { ~S (); };
3766 template <typename T> S<T>::~S() {}
3768 is invalid, since `~' must be followed by a class-name, but
3769 `S<T>' is dependent, and so not known to be a class.
3770 That's not right; we need to look in uninstantiated
3771 templates. A further complication arises from:
3773 template <typename T> void f(T t) {
3777 Here, it is not possible to look up `T' in the scope of `T'
3778 itself. We must look in both the current scope, and the
3779 scope of the containing complete expression.
3781 Yet another issue is:
3790 The standard does not seem to say that the `S' in `~S'
3791 should refer to the type `S' and not the data member
3794 /* DR 244 says that we look up the name after the "~" in the
3795 same scope as we looked up the qualifying name. That idea
3796 isn't fully worked out; it's more complicated than that. */
3797 scope = parser->scope;
3798 object_scope = parser->object_scope;
3799 qualifying_scope = parser->qualifying_scope;
3801 /* Check for invalid scopes. */
3802 if (scope == error_mark_node)
3804 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3805 cp_lexer_consume_token (parser->lexer);
3806 return error_mark_node;
3808 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3810 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3811 error ("%Hscope %qT before %<~%> is not a class-name",
3812 &token->location, scope);
3813 cp_parser_simulate_error (parser);
3814 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3815 cp_lexer_consume_token (parser->lexer);
3816 return error_mark_node;
3818 gcc_assert (!scope || TYPE_P (scope));
3820 /* If the name is of the form "X::~X" it's OK. */
3821 token = cp_lexer_peek_token (parser->lexer);
3823 && token->type == CPP_NAME
3824 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3826 && constructor_name_p (token->u.value, scope))
3828 cp_lexer_consume_token (parser->lexer);
3829 return build_nt (BIT_NOT_EXPR, scope);
3832 /* If there was an explicit qualification (S::~T), first look
3833 in the scope given by the qualification (i.e., S). */
3835 type_decl = NULL_TREE;
3838 cp_parser_parse_tentatively (parser);
3839 type_decl = cp_parser_class_name (parser,
3840 /*typename_keyword_p=*/false,
3841 /*template_keyword_p=*/false,
3843 /*check_dependency=*/false,
3844 /*class_head_p=*/false,
3846 if (cp_parser_parse_definitely (parser))
3849 /* In "N::S::~S", look in "N" as well. */
3850 if (!done && scope && qualifying_scope)
3852 cp_parser_parse_tentatively (parser);
3853 parser->scope = qualifying_scope;
3854 parser->object_scope = NULL_TREE;
3855 parser->qualifying_scope = NULL_TREE;
3857 = cp_parser_class_name (parser,
3858 /*typename_keyword_p=*/false,
3859 /*template_keyword_p=*/false,
3861 /*check_dependency=*/false,
3862 /*class_head_p=*/false,
3864 if (cp_parser_parse_definitely (parser))
3867 /* In "p->S::~T", look in the scope given by "*p" as well. */
3868 else if (!done && object_scope)
3870 cp_parser_parse_tentatively (parser);
3871 parser->scope = object_scope;
3872 parser->object_scope = NULL_TREE;
3873 parser->qualifying_scope = NULL_TREE;
3875 = cp_parser_class_name (parser,
3876 /*typename_keyword_p=*/false,
3877 /*template_keyword_p=*/false,
3879 /*check_dependency=*/false,
3880 /*class_head_p=*/false,
3882 if (cp_parser_parse_definitely (parser))
3885 /* Look in the surrounding context. */
3888 parser->scope = NULL_TREE;
3889 parser->object_scope = NULL_TREE;
3890 parser->qualifying_scope = NULL_TREE;
3891 if (processing_template_decl)
3892 cp_parser_parse_tentatively (parser);
3894 = cp_parser_class_name (parser,
3895 /*typename_keyword_p=*/false,
3896 /*template_keyword_p=*/false,
3898 /*check_dependency=*/false,
3899 /*class_head_p=*/false,
3901 if (processing_template_decl
3902 && ! cp_parser_parse_definitely (parser))
3904 /* We couldn't find a type with this name, so just accept
3905 it and check for a match at instantiation time. */
3906 type_decl = cp_parser_identifier (parser);
3907 if (type_decl != error_mark_node)
3908 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3912 /* If an error occurred, assume that the name of the
3913 destructor is the same as the name of the qualifying
3914 class. That allows us to keep parsing after running
3915 into ill-formed destructor names. */
3916 if (type_decl == error_mark_node && scope)
3917 return build_nt (BIT_NOT_EXPR, scope);
3918 else if (type_decl == error_mark_node)
3919 return error_mark_node;
3921 /* Check that destructor name and scope match. */
3922 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3924 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3925 error ("%Hdeclaration of %<~%T%> as member of %qT",
3926 &token->location, type_decl, scope);
3927 cp_parser_simulate_error (parser);
3928 return error_mark_node;
3933 A typedef-name that names a class shall not be used as the
3934 identifier in the declarator for a destructor declaration. */
3936 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3937 && !DECL_SELF_REFERENCE_P (type_decl)
3938 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3939 error ("%Htypedef-name %qD used as destructor declarator",
3940 &token->location, type_decl);
3942 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3946 if (token->keyword == RID_OPERATOR)
3950 /* This could be a template-id, so we try that first. */
3951 cp_parser_parse_tentatively (parser);
3952 /* Try a template-id. */
3953 id = cp_parser_template_id (parser, template_keyword_p,
3954 /*check_dependency_p=*/true,
3956 /* If that worked, we're done. */
3957 if (cp_parser_parse_definitely (parser))
3959 /* We still don't know whether we're looking at an
3960 operator-function-id or a conversion-function-id. */
3961 cp_parser_parse_tentatively (parser);
3962 /* Try an operator-function-id. */
3963 id = cp_parser_operator_function_id (parser);
3964 /* If that didn't work, try a conversion-function-id. */
3965 if (!cp_parser_parse_definitely (parser))
3966 id = cp_parser_conversion_function_id (parser);
3975 cp_parser_error (parser, "expected unqualified-id");
3976 return error_mark_node;
3980 /* Parse an (optional) nested-name-specifier.
3982 nested-name-specifier: [C++98]
3983 class-or-namespace-name :: nested-name-specifier [opt]
3984 class-or-namespace-name :: template nested-name-specifier [opt]
3986 nested-name-specifier: [C++0x]
3989 nested-name-specifier identifier ::
3990 nested-name-specifier template [opt] simple-template-id ::
3992 PARSER->SCOPE should be set appropriately before this function is
3993 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3994 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3997 Sets PARSER->SCOPE to the class (TYPE) or namespace
3998 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3999 it unchanged if there is no nested-name-specifier. Returns the new
4000 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4002 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4003 part of a declaration and/or decl-specifier. */
4006 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4007 bool typename_keyword_p,
4008 bool check_dependency_p,
4010 bool is_declaration)
4012 bool success = false;
4013 cp_token_position start = 0;
4016 /* Remember where the nested-name-specifier starts. */
4017 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4019 start = cp_lexer_token_position (parser->lexer, false);
4020 push_deferring_access_checks (dk_deferred);
4027 tree saved_qualifying_scope;
4028 bool template_keyword_p;
4030 /* Spot cases that cannot be the beginning of a
4031 nested-name-specifier. */
4032 token = cp_lexer_peek_token (parser->lexer);
4034 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4035 the already parsed nested-name-specifier. */
4036 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4038 /* Grab the nested-name-specifier and continue the loop. */
4039 cp_parser_pre_parsed_nested_name_specifier (parser);
4040 /* If we originally encountered this nested-name-specifier
4041 with IS_DECLARATION set to false, we will not have
4042 resolved TYPENAME_TYPEs, so we must do so here. */
4044 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4046 new_scope = resolve_typename_type (parser->scope,
4047 /*only_current_p=*/false);
4048 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4049 parser->scope = new_scope;
4055 /* Spot cases that cannot be the beginning of a
4056 nested-name-specifier. On the second and subsequent times
4057 through the loop, we look for the `template' keyword. */
4058 if (success && token->keyword == RID_TEMPLATE)
4060 /* A template-id can start a nested-name-specifier. */
4061 else if (token->type == CPP_TEMPLATE_ID)
4065 /* If the next token is not an identifier, then it is
4066 definitely not a type-name or namespace-name. */
4067 if (token->type != CPP_NAME)
4069 /* If the following token is neither a `<' (to begin a
4070 template-id), nor a `::', then we are not looking at a
4071 nested-name-specifier. */
4072 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4073 if (token->type != CPP_SCOPE
4074 && !cp_parser_nth_token_starts_template_argument_list_p
4079 /* The nested-name-specifier is optional, so we parse
4081 cp_parser_parse_tentatively (parser);
4083 /* Look for the optional `template' keyword, if this isn't the
4084 first time through the loop. */
4086 template_keyword_p = cp_parser_optional_template_keyword (parser);
4088 template_keyword_p = false;
4090 /* Save the old scope since the name lookup we are about to do
4091 might destroy it. */
4092 old_scope = parser->scope;
4093 saved_qualifying_scope = parser->qualifying_scope;
4094 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4095 look up names in "X<T>::I" in order to determine that "Y" is
4096 a template. So, if we have a typename at this point, we make
4097 an effort to look through it. */
4099 && !typename_keyword_p
4101 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4102 parser->scope = resolve_typename_type (parser->scope,
4103 /*only_current_p=*/false);
4104 /* Parse the qualifying entity. */
4106 = cp_parser_qualifying_entity (parser,
4112 /* Look for the `::' token. */
4113 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4115 /* If we found what we wanted, we keep going; otherwise, we're
4117 if (!cp_parser_parse_definitely (parser))
4119 bool error_p = false;
4121 /* Restore the OLD_SCOPE since it was valid before the
4122 failed attempt at finding the last
4123 class-or-namespace-name. */
4124 parser->scope = old_scope;
4125 parser->qualifying_scope = saved_qualifying_scope;
4126 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4128 /* If the next token is an identifier, and the one after
4129 that is a `::', then any valid interpretation would have
4130 found a class-or-namespace-name. */
4131 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4132 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4134 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4137 token = cp_lexer_consume_token (parser->lexer);
4140 if (!token->ambiguous_p)
4143 tree ambiguous_decls;
4145 decl = cp_parser_lookup_name (parser, token->u.value,
4147 /*is_template=*/false,
4148 /*is_namespace=*/false,
4149 /*check_dependency=*/true,
4152 if (TREE_CODE (decl) == TEMPLATE_DECL)
4153 error ("%H%qD used without template parameters",
4154 &token->location, decl);
4155 else if (ambiguous_decls)
4157 error ("%Hreference to %qD is ambiguous",
4158 &token->location, token->u.value);
4159 print_candidates (ambiguous_decls);
4160 decl = error_mark_node;
4164 const char* msg = "is not a class or namespace";
4165 if (cxx_dialect != cxx98)
4166 msg = "is not a class, namespace, or enumeration";
4167 cp_parser_name_lookup_error
4168 (parser, token->u.value, decl, msg,
4172 parser->scope = error_mark_node;
4174 /* Treat this as a successful nested-name-specifier
4179 If the name found is not a class-name (clause
4180 _class_) or namespace-name (_namespace.def_), the
4181 program is ill-formed. */
4184 cp_lexer_consume_token (parser->lexer);
4188 /* We've found one valid nested-name-specifier. */
4190 /* Name lookup always gives us a DECL. */
4191 if (TREE_CODE (new_scope) == TYPE_DECL)
4192 new_scope = TREE_TYPE (new_scope);
4193 /* Uses of "template" must be followed by actual templates. */
4194 if (template_keyword_p
4195 && !(CLASS_TYPE_P (new_scope)
4196 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4197 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4198 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4199 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4200 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4201 == TEMPLATE_ID_EXPR)))
4202 permerror (input_location, TYPE_P (new_scope)
4203 ? "%qT is not a template"
4204 : "%qD is not a template",
4206 /* If it is a class scope, try to complete it; we are about to
4207 be looking up names inside the class. */
4208 if (TYPE_P (new_scope)
4209 /* Since checking types for dependency can be expensive,
4210 avoid doing it if the type is already complete. */
4211 && !COMPLETE_TYPE_P (new_scope)
4212 /* Do not try to complete dependent types. */
4213 && !dependent_type_p (new_scope))
4215 new_scope = complete_type (new_scope);
4216 /* If it is a typedef to current class, use the current
4217 class instead, as the typedef won't have any names inside
4219 if (!COMPLETE_TYPE_P (new_scope)
4220 && currently_open_class (new_scope))
4221 new_scope = TYPE_MAIN_VARIANT (new_scope);
4223 /* Make sure we look in the right scope the next time through
4225 parser->scope = new_scope;
4228 /* If parsing tentatively, replace the sequence of tokens that makes
4229 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4230 token. That way, should we re-parse the token stream, we will
4231 not have to repeat the effort required to do the parse, nor will
4232 we issue duplicate error messages. */
4233 if (success && start)
4237 token = cp_lexer_token_at (parser->lexer, start);
4238 /* Reset the contents of the START token. */
4239 token->type = CPP_NESTED_NAME_SPECIFIER;
4240 /* Retrieve any deferred checks. Do not pop this access checks yet
4241 so the memory will not be reclaimed during token replacing below. */
4242 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4243 token->u.tree_check_value->value = parser->scope;
4244 token->u.tree_check_value->checks = get_deferred_access_checks ();
4245 token->u.tree_check_value->qualifying_scope =
4246 parser->qualifying_scope;
4247 token->keyword = RID_MAX;
4249 /* Purge all subsequent tokens. */
4250 cp_lexer_purge_tokens_after (parser->lexer, start);
4254 pop_to_parent_deferring_access_checks ();
4256 return success ? parser->scope : NULL_TREE;
4259 /* Parse a nested-name-specifier. See
4260 cp_parser_nested_name_specifier_opt for details. This function
4261 behaves identically, except that it will an issue an error if no
4262 nested-name-specifier is present. */
4265 cp_parser_nested_name_specifier (cp_parser *parser,
4266 bool typename_keyword_p,
4267 bool check_dependency_p,
4269 bool is_declaration)
4273 /* Look for the nested-name-specifier. */
4274 scope = cp_parser_nested_name_specifier_opt (parser,
4279 /* If it was not present, issue an error message. */
4282 cp_parser_error (parser, "expected nested-name-specifier");
4283 parser->scope = NULL_TREE;
4289 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4290 this is either a class-name or a namespace-name (which corresponds
4291 to the class-or-namespace-name production in the grammar). For
4292 C++0x, it can also be a type-name that refers to an enumeration
4295 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4296 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4297 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4298 TYPE_P is TRUE iff the next name should be taken as a class-name,
4299 even the same name is declared to be another entity in the same
4302 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4303 specified by the class-or-namespace-name. If neither is found the
4304 ERROR_MARK_NODE is returned. */
4307 cp_parser_qualifying_entity (cp_parser *parser,
4308 bool typename_keyword_p,
4309 bool template_keyword_p,
4310 bool check_dependency_p,
4312 bool is_declaration)
4315 tree saved_qualifying_scope;
4316 tree saved_object_scope;
4319 bool successful_parse_p;
4321 /* Before we try to parse the class-name, we must save away the
4322 current PARSER->SCOPE since cp_parser_class_name will destroy
4324 saved_scope = parser->scope;
4325 saved_qualifying_scope = parser->qualifying_scope;
4326 saved_object_scope = parser->object_scope;
4327 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4328 there is no need to look for a namespace-name. */
4329 only_class_p = template_keyword_p
4330 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4332 cp_parser_parse_tentatively (parser);
4333 scope = cp_parser_class_name (parser,
4336 type_p ? class_type : none_type,
4338 /*class_head_p=*/false,
4340 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4341 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4343 && cxx_dialect != cxx98
4344 && !successful_parse_p)
4346 /* Restore the saved scope. */
4347 parser->scope = saved_scope;
4348 parser->qualifying_scope = saved_qualifying_scope;
4349 parser->object_scope = saved_object_scope;
4351 /* Parse tentatively. */
4352 cp_parser_parse_tentatively (parser);
4354 /* Parse a typedef-name or enum-name. */
4355 scope = cp_parser_nonclass_name (parser);
4356 successful_parse_p = cp_parser_parse_definitely (parser);
4358 /* If that didn't work, try for a namespace-name. */
4359 if (!only_class_p && !successful_parse_p)
4361 /* Restore the saved scope. */
4362 parser->scope = saved_scope;
4363 parser->qualifying_scope = saved_qualifying_scope;
4364 parser->object_scope = saved_object_scope;
4365 /* If we are not looking at an identifier followed by the scope
4366 resolution operator, then this is not part of a
4367 nested-name-specifier. (Note that this function is only used
4368 to parse the components of a nested-name-specifier.) */
4369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4370 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4371 return error_mark_node;
4372 scope = cp_parser_namespace_name (parser);
4378 /* Parse a postfix-expression.
4382 postfix-expression [ expression ]
4383 postfix-expression ( expression-list [opt] )
4384 simple-type-specifier ( expression-list [opt] )
4385 typename :: [opt] nested-name-specifier identifier
4386 ( expression-list [opt] )
4387 typename :: [opt] nested-name-specifier template [opt] template-id
4388 ( expression-list [opt] )
4389 postfix-expression . template [opt] id-expression
4390 postfix-expression -> template [opt] id-expression
4391 postfix-expression . pseudo-destructor-name
4392 postfix-expression -> pseudo-destructor-name
4393 postfix-expression ++
4394 postfix-expression --
4395 dynamic_cast < type-id > ( expression )
4396 static_cast < type-id > ( expression )
4397 reinterpret_cast < type-id > ( expression )
4398 const_cast < type-id > ( expression )
4399 typeid ( expression )
4405 ( type-id ) { initializer-list , [opt] }
4407 This extension is a GNU version of the C99 compound-literal
4408 construct. (The C99 grammar uses `type-name' instead of `type-id',
4409 but they are essentially the same concept.)
4411 If ADDRESS_P is true, the postfix expression is the operand of the
4412 `&' operator. CAST_P is true if this expression is the target of a
4415 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4416 class member access expressions [expr.ref].
4418 Returns a representation of the expression. */
4421 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4422 bool member_access_only_p,
4423 cp_id_kind * pidk_return)
4427 cp_id_kind idk = CP_ID_KIND_NONE;
4428 tree postfix_expression = NULL_TREE;
4429 bool is_member_access = false;
4431 /* Peek at the next token. */
4432 token = cp_lexer_peek_token (parser->lexer);
4433 /* Some of the productions are determined by keywords. */
4434 keyword = token->keyword;
4444 const char *saved_message;
4446 /* All of these can be handled in the same way from the point
4447 of view of parsing. Begin by consuming the token
4448 identifying the cast. */
4449 cp_lexer_consume_token (parser->lexer);
4451 /* New types cannot be defined in the cast. */
4452 saved_message = parser->type_definition_forbidden_message;
4453 parser->type_definition_forbidden_message
4454 = "types may not be defined in casts";
4456 /* Look for the opening `<'. */
4457 cp_parser_require (parser, CPP_LESS, "%<<%>");
4458 /* Parse the type to which we are casting. */
4459 type = cp_parser_type_id (parser);
4460 /* Look for the closing `>'. */
4461 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4462 /* Restore the old message. */
4463 parser->type_definition_forbidden_message = saved_message;
4465 /* And the expression which is being cast. */
4466 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4467 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4468 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4470 /* Only type conversions to integral or enumeration types
4471 can be used in constant-expressions. */
4472 if (!cast_valid_in_integral_constant_expression_p (type)
4473 && (cp_parser_non_integral_constant_expression
4475 "a cast to a type other than an integral or "
4476 "enumeration type")))
4477 return error_mark_node;
4483 = build_dynamic_cast (type, expression, tf_warning_or_error);
4487 = build_static_cast (type, expression, tf_warning_or_error);
4491 = build_reinterpret_cast (type, expression,
4492 tf_warning_or_error);
4496 = build_const_cast (type, expression, tf_warning_or_error);
4507 const char *saved_message;
4508 bool saved_in_type_id_in_expr_p;
4510 /* Consume the `typeid' token. */
4511 cp_lexer_consume_token (parser->lexer);
4512 /* Look for the `(' token. */
4513 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4514 /* Types cannot be defined in a `typeid' expression. */
4515 saved_message = parser->type_definition_forbidden_message;
4516 parser->type_definition_forbidden_message
4517 = "types may not be defined in a %<typeid%> expression";
4518 /* We can't be sure yet whether we're looking at a type-id or an
4520 cp_parser_parse_tentatively (parser);
4521 /* Try a type-id first. */
4522 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4523 parser->in_type_id_in_expr_p = true;
4524 type = cp_parser_type_id (parser);
4525 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4526 /* Look for the `)' token. Otherwise, we can't be sure that
4527 we're not looking at an expression: consider `typeid (int
4528 (3))', for example. */
4529 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4530 /* If all went well, simply lookup the type-id. */
4531 if (cp_parser_parse_definitely (parser))
4532 postfix_expression = get_typeid (type);
4533 /* Otherwise, fall back to the expression variant. */
4538 /* Look for an expression. */
4539 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4540 /* Compute its typeid. */
4541 postfix_expression = build_typeid (expression);
4542 /* Look for the `)' token. */
4543 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4545 /* Restore the saved message. */
4546 parser->type_definition_forbidden_message = saved_message;
4547 /* `typeid' may not appear in an integral constant expression. */
4548 if (cp_parser_non_integral_constant_expression(parser,
4549 "%<typeid%> operator"))
4550 return error_mark_node;
4557 /* The syntax permitted here is the same permitted for an
4558 elaborated-type-specifier. */
4559 type = cp_parser_elaborated_type_specifier (parser,
4560 /*is_friend=*/false,
4561 /*is_declaration=*/false);
4562 postfix_expression = cp_parser_functional_cast (parser, type);
4570 /* If the next thing is a simple-type-specifier, we may be
4571 looking at a functional cast. We could also be looking at
4572 an id-expression. So, we try the functional cast, and if
4573 that doesn't work we fall back to the primary-expression. */
4574 cp_parser_parse_tentatively (parser);
4575 /* Look for the simple-type-specifier. */
4576 type = cp_parser_simple_type_specifier (parser,
4577 /*decl_specs=*/NULL,
4578 CP_PARSER_FLAGS_NONE);
4579 /* Parse the cast itself. */
4580 if (!cp_parser_error_occurred (parser))
4582 = cp_parser_functional_cast (parser, type);
4583 /* If that worked, we're done. */
4584 if (cp_parser_parse_definitely (parser))
4587 /* If the functional-cast didn't work out, try a
4588 compound-literal. */
4589 if (cp_parser_allow_gnu_extensions_p (parser)
4590 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4592 VEC(constructor_elt,gc) *initializer_list = NULL;
4593 bool saved_in_type_id_in_expr_p;
4595 cp_parser_parse_tentatively (parser);
4596 /* Consume the `('. */
4597 cp_lexer_consume_token (parser->lexer);
4598 /* Parse the type. */
4599 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4600 parser->in_type_id_in_expr_p = true;
4601 type = cp_parser_type_id (parser);
4602 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4603 /* Look for the `)'. */
4604 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4605 /* Look for the `{'. */
4606 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4607 /* If things aren't going well, there's no need to
4609 if (!cp_parser_error_occurred (parser))
4611 bool non_constant_p;
4612 /* Parse the initializer-list. */
4614 = cp_parser_initializer_list (parser, &non_constant_p);
4615 /* Allow a trailing `,'. */
4616 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4617 cp_lexer_consume_token (parser->lexer);
4618 /* Look for the final `}'. */
4619 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4621 /* If that worked, we're definitely looking at a
4622 compound-literal expression. */
4623 if (cp_parser_parse_definitely (parser))
4625 /* Warn the user that a compound literal is not
4626 allowed in standard C++. */
4627 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4628 /* For simplicity, we disallow compound literals in
4629 constant-expressions. We could
4630 allow compound literals of integer type, whose
4631 initializer was a constant, in constant
4632 expressions. Permitting that usage, as a further
4633 extension, would not change the meaning of any
4634 currently accepted programs. (Of course, as
4635 compound literals are not part of ISO C++, the
4636 standard has nothing to say.) */
4637 if (cp_parser_non_integral_constant_expression
4638 (parser, "non-constant compound literals"))
4640 postfix_expression = error_mark_node;
4643 /* Form the representation of the compound-literal. */
4645 = (finish_compound_literal
4646 (type, build_constructor (init_list_type_node,
4647 initializer_list)));
4652 /* It must be a primary-expression. */
4654 = cp_parser_primary_expression (parser, address_p, cast_p,
4655 /*template_arg_p=*/false,
4661 /* Keep looping until the postfix-expression is complete. */
4664 if (idk == CP_ID_KIND_UNQUALIFIED
4665 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4666 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4667 /* It is not a Koenig lookup function call. */
4669 = unqualified_name_lookup_error (postfix_expression);
4671 /* Peek at the next token. */
4672 token = cp_lexer_peek_token (parser->lexer);
4674 switch (token->type)
4676 case CPP_OPEN_SQUARE:
4678 = cp_parser_postfix_open_square_expression (parser,
4681 idk = CP_ID_KIND_NONE;
4682 is_member_access = false;
4685 case CPP_OPEN_PAREN:
4686 /* postfix-expression ( expression-list [opt] ) */
4689 bool is_builtin_constant_p;
4690 bool saved_integral_constant_expression_p = false;
4691 bool saved_non_integral_constant_expression_p = false;
4694 is_member_access = false;
4696 is_builtin_constant_p
4697 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4698 if (is_builtin_constant_p)
4700 /* The whole point of __builtin_constant_p is to allow
4701 non-constant expressions to appear as arguments. */
4702 saved_integral_constant_expression_p
4703 = parser->integral_constant_expression_p;
4704 saved_non_integral_constant_expression_p
4705 = parser->non_integral_constant_expression_p;
4706 parser->integral_constant_expression_p = false;
4708 args = (cp_parser_parenthesized_expression_list
4709 (parser, /*is_attribute_list=*/false,
4710 /*cast_p=*/false, /*allow_expansion_p=*/true,
4711 /*non_constant_p=*/NULL));
4712 if (is_builtin_constant_p)
4714 parser->integral_constant_expression_p
4715 = saved_integral_constant_expression_p;
4716 parser->non_integral_constant_expression_p
4717 = saved_non_integral_constant_expression_p;
4722 postfix_expression = error_mark_node;
4726 /* Function calls are not permitted in
4727 constant-expressions. */
4728 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4729 && cp_parser_non_integral_constant_expression (parser,
4732 postfix_expression = error_mark_node;
4733 release_tree_vector (args);
4738 if (idk == CP_ID_KIND_UNQUALIFIED
4739 || idk == CP_ID_KIND_TEMPLATE_ID)
4741 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4743 if (!VEC_empty (tree, args))
4746 if (!any_type_dependent_arguments_p (args))
4748 = perform_koenig_lookup (postfix_expression, args);
4752 = unqualified_fn_lookup_error (postfix_expression);
4754 /* We do not perform argument-dependent lookup if
4755 normal lookup finds a non-function, in accordance
4756 with the expected resolution of DR 218. */
4757 else if (!VEC_empty (tree, args)
4758 && is_overloaded_fn (postfix_expression))
4760 tree fn = get_first_fn (postfix_expression);
4762 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4763 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4765 /* Only do argument dependent lookup if regular
4766 lookup does not find a set of member functions.
4767 [basic.lookup.koenig]/2a */
4768 if (!DECL_FUNCTION_MEMBER_P (fn))
4771 if (!any_type_dependent_arguments_p (args))
4773 = perform_koenig_lookup (postfix_expression, args);
4778 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4780 tree instance = TREE_OPERAND (postfix_expression, 0);
4781 tree fn = TREE_OPERAND (postfix_expression, 1);
4783 if (processing_template_decl
4784 && (type_dependent_expression_p (instance)
4785 || (!BASELINK_P (fn)
4786 && TREE_CODE (fn) != FIELD_DECL)
4787 || type_dependent_expression_p (fn)
4788 || any_type_dependent_arguments_p (args)))
4791 = build_nt_call_vec (postfix_expression, args);
4792 release_tree_vector (args);
4796 if (BASELINK_P (fn))
4799 = (build_new_method_call
4800 (instance, fn, &args, NULL_TREE,
4801 (idk == CP_ID_KIND_QUALIFIED
4802 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4804 tf_warning_or_error));
4808 = finish_call_expr (postfix_expression, &args,
4809 /*disallow_virtual=*/false,
4811 tf_warning_or_error);
4813 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4814 || TREE_CODE (postfix_expression) == MEMBER_REF
4815 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4816 postfix_expression = (build_offset_ref_call_from_tree
4817 (postfix_expression, &args));
4818 else if (idk == CP_ID_KIND_QUALIFIED)
4819 /* A call to a static class member, or a namespace-scope
4822 = finish_call_expr (postfix_expression, &args,
4823 /*disallow_virtual=*/true,
4825 tf_warning_or_error);
4827 /* All other function calls. */
4829 = finish_call_expr (postfix_expression, &args,
4830 /*disallow_virtual=*/false,
4832 tf_warning_or_error);
4834 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4835 idk = CP_ID_KIND_NONE;
4837 release_tree_vector (args);
4843 /* postfix-expression . template [opt] id-expression
4844 postfix-expression . pseudo-destructor-name
4845 postfix-expression -> template [opt] id-expression
4846 postfix-expression -> pseudo-destructor-name */
4848 /* Consume the `.' or `->' operator. */
4849 cp_lexer_consume_token (parser->lexer);
4852 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4857 is_member_access = true;
4861 /* postfix-expression ++ */
4862 /* Consume the `++' token. */
4863 cp_lexer_consume_token (parser->lexer);
4864 /* Generate a representation for the complete expression. */
4866 = finish_increment_expr (postfix_expression,
4867 POSTINCREMENT_EXPR);
4868 /* Increments may not appear in constant-expressions. */
4869 if (cp_parser_non_integral_constant_expression (parser,
4871 postfix_expression = error_mark_node;
4872 idk = CP_ID_KIND_NONE;
4873 is_member_access = false;
4876 case CPP_MINUS_MINUS:
4877 /* postfix-expression -- */
4878 /* Consume the `--' token. */
4879 cp_lexer_consume_token (parser->lexer);
4880 /* Generate a representation for the complete expression. */
4882 = finish_increment_expr (postfix_expression,
4883 POSTDECREMENT_EXPR);
4884 /* Decrements may not appear in constant-expressions. */
4885 if (cp_parser_non_integral_constant_expression (parser,
4887 postfix_expression = error_mark_node;
4888 idk = CP_ID_KIND_NONE;
4889 is_member_access = false;
4893 if (pidk_return != NULL)
4894 * pidk_return = idk;
4895 if (member_access_only_p)
4896 return is_member_access? postfix_expression : error_mark_node;
4898 return postfix_expression;
4902 /* We should never get here. */
4904 return error_mark_node;
4907 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4908 by cp_parser_builtin_offsetof. We're looking for
4910 postfix-expression [ expression ]
4912 FOR_OFFSETOF is set if we're being called in that context, which
4913 changes how we deal with integer constant expressions. */
4916 cp_parser_postfix_open_square_expression (cp_parser *parser,
4917 tree postfix_expression,
4922 /* Consume the `[' token. */
4923 cp_lexer_consume_token (parser->lexer);
4925 /* Parse the index expression. */
4926 /* ??? For offsetof, there is a question of what to allow here. If
4927 offsetof is not being used in an integral constant expression context,
4928 then we *could* get the right answer by computing the value at runtime.
4929 If we are in an integral constant expression context, then we might
4930 could accept any constant expression; hard to say without analysis.
4931 Rather than open the barn door too wide right away, allow only integer
4932 constant expressions here. */
4934 index = cp_parser_constant_expression (parser, false, NULL);
4936 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4938 /* Look for the closing `]'. */
4939 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4941 /* Build the ARRAY_REF. */
4942 postfix_expression = grok_array_decl (postfix_expression, index);
4944 /* When not doing offsetof, array references are not permitted in
4945 constant-expressions. */
4947 && (cp_parser_non_integral_constant_expression
4948 (parser, "an array reference")))
4949 postfix_expression = error_mark_node;
4951 return postfix_expression;
4954 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4955 by cp_parser_builtin_offsetof. We're looking for
4957 postfix-expression . template [opt] id-expression
4958 postfix-expression . pseudo-destructor-name
4959 postfix-expression -> template [opt] id-expression
4960 postfix-expression -> pseudo-destructor-name
4962 FOR_OFFSETOF is set if we're being called in that context. That sorta
4963 limits what of the above we'll actually accept, but nevermind.
4964 TOKEN_TYPE is the "." or "->" token, which will already have been
4965 removed from the stream. */
4968 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4969 enum cpp_ttype token_type,
4970 tree postfix_expression,
4971 bool for_offsetof, cp_id_kind *idk,
4972 location_t location)
4976 bool pseudo_destructor_p;
4977 tree scope = NULL_TREE;
4979 /* If this is a `->' operator, dereference the pointer. */
4980 if (token_type == CPP_DEREF)
4981 postfix_expression = build_x_arrow (postfix_expression);
4982 /* Check to see whether or not the expression is type-dependent. */
4983 dependent_p = type_dependent_expression_p (postfix_expression);
4984 /* The identifier following the `->' or `.' is not qualified. */
4985 parser->scope = NULL_TREE;
4986 parser->qualifying_scope = NULL_TREE;
4987 parser->object_scope = NULL_TREE;
4988 *idk = CP_ID_KIND_NONE;
4990 /* Enter the scope corresponding to the type of the object
4991 given by the POSTFIX_EXPRESSION. */
4992 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4994 scope = TREE_TYPE (postfix_expression);
4995 /* According to the standard, no expression should ever have
4996 reference type. Unfortunately, we do not currently match
4997 the standard in this respect in that our internal representation
4998 of an expression may have reference type even when the standard
4999 says it does not. Therefore, we have to manually obtain the
5000 underlying type here. */
5001 scope = non_reference (scope);
5002 /* The type of the POSTFIX_EXPRESSION must be complete. */
5003 if (scope == unknown_type_node)
5005 error ("%H%qE does not have class type", &location, postfix_expression);
5009 scope = complete_type_or_else (scope, NULL_TREE);
5010 /* Let the name lookup machinery know that we are processing a
5011 class member access expression. */
5012 parser->context->object_type = scope;
5013 /* If something went wrong, we want to be able to discern that case,
5014 as opposed to the case where there was no SCOPE due to the type
5015 of expression being dependent. */
5017 scope = error_mark_node;
5018 /* If the SCOPE was erroneous, make the various semantic analysis
5019 functions exit quickly -- and without issuing additional error
5021 if (scope == error_mark_node)
5022 postfix_expression = error_mark_node;
5025 /* Assume this expression is not a pseudo-destructor access. */
5026 pseudo_destructor_p = false;
5028 /* If the SCOPE is a scalar type, then, if this is a valid program,
5029 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5030 is type dependent, it can be pseudo-destructor-name or something else.
5031 Try to parse it as pseudo-destructor-name first. */
5032 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5037 cp_parser_parse_tentatively (parser);
5038 /* Parse the pseudo-destructor-name. */
5040 cp_parser_pseudo_destructor_name (parser, &s, &type);
5042 && (cp_parser_error_occurred (parser)
5043 || TREE_CODE (type) != TYPE_DECL
5044 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5045 cp_parser_abort_tentative_parse (parser);
5046 else if (cp_parser_parse_definitely (parser))
5048 pseudo_destructor_p = true;
5050 = finish_pseudo_destructor_expr (postfix_expression,
5051 s, TREE_TYPE (type));
5055 if (!pseudo_destructor_p)
5057 /* If the SCOPE is not a scalar type, we are looking at an
5058 ordinary class member access expression, rather than a
5059 pseudo-destructor-name. */
5061 cp_token *token = cp_lexer_peek_token (parser->lexer);
5062 /* Parse the id-expression. */
5063 name = (cp_parser_id_expression
5065 cp_parser_optional_template_keyword (parser),
5066 /*check_dependency_p=*/true,
5068 /*declarator_p=*/false,
5069 /*optional_p=*/false));
5070 /* In general, build a SCOPE_REF if the member name is qualified.
5071 However, if the name was not dependent and has already been
5072 resolved; there is no need to build the SCOPE_REF. For example;
5074 struct X { void f(); };
5075 template <typename T> void f(T* t) { t->X::f(); }
5077 Even though "t" is dependent, "X::f" is not and has been resolved
5078 to a BASELINK; there is no need to include scope information. */
5080 /* But we do need to remember that there was an explicit scope for
5081 virtual function calls. */
5083 *idk = CP_ID_KIND_QUALIFIED;
5085 /* If the name is a template-id that names a type, we will get a
5086 TYPE_DECL here. That is invalid code. */
5087 if (TREE_CODE (name) == TYPE_DECL)
5089 error ("%Hinvalid use of %qD", &token->location, name);
5090 postfix_expression = error_mark_node;
5094 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5096 name = build_qualified_name (/*type=*/NULL_TREE,
5100 parser->scope = NULL_TREE;
5101 parser->qualifying_scope = NULL_TREE;
5102 parser->object_scope = NULL_TREE;
5104 if (scope && name && BASELINK_P (name))
5105 adjust_result_of_qualified_name_lookup
5106 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5108 = finish_class_member_access_expr (postfix_expression, name,
5110 tf_warning_or_error);
5114 /* We no longer need to look up names in the scope of the object on
5115 the left-hand side of the `.' or `->' operator. */
5116 parser->context->object_type = NULL_TREE;
5118 /* Outside of offsetof, these operators may not appear in
5119 constant-expressions. */
5121 && (cp_parser_non_integral_constant_expression
5122 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5123 postfix_expression = error_mark_node;
5125 return postfix_expression;
5128 /* Parse a parenthesized expression-list.
5131 assignment-expression
5132 expression-list, assignment-expression
5137 identifier, expression-list
5139 CAST_P is true if this expression is the target of a cast.
5141 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5144 Returns a vector of trees. Each element is a representation of an
5145 assignment-expression. NULL is returned if the ( and or ) are
5146 missing. An empty, but allocated, vector is returned on no
5147 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5148 if this is really an attribute list being parsed. If
5149 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5150 not all of the expressions in the list were constant. */
5152 static VEC(tree,gc) *
5153 cp_parser_parenthesized_expression_list (cp_parser* parser,
5154 bool is_attribute_list,
5156 bool allow_expansion_p,
5157 bool *non_constant_p)
5159 VEC(tree,gc) *expression_list;
5160 bool fold_expr_p = is_attribute_list;
5161 tree identifier = NULL_TREE;
5162 bool saved_greater_than_is_operator_p;
5164 /* Assume all the expressions will be constant. */
5166 *non_constant_p = false;
5168 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5171 expression_list = make_tree_vector ();
5173 /* Within a parenthesized expression, a `>' token is always
5174 the greater-than operator. */
5175 saved_greater_than_is_operator_p
5176 = parser->greater_than_is_operator_p;
5177 parser->greater_than_is_operator_p = true;
5179 /* Consume expressions until there are no more. */
5180 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5185 /* At the beginning of attribute lists, check to see if the
5186 next token is an identifier. */
5187 if (is_attribute_list
5188 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5192 /* Consume the identifier. */
5193 token = cp_lexer_consume_token (parser->lexer);
5194 /* Save the identifier. */
5195 identifier = token->u.value;
5199 bool expr_non_constant_p;
5201 /* Parse the next assignment-expression. */
5202 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5204 /* A braced-init-list. */
5205 maybe_warn_cpp0x ("extended initializer lists");
5206 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5207 if (non_constant_p && expr_non_constant_p)
5208 *non_constant_p = true;
5210 else if (non_constant_p)
5212 expr = (cp_parser_constant_expression
5213 (parser, /*allow_non_constant_p=*/true,
5214 &expr_non_constant_p));
5215 if (expr_non_constant_p)
5216 *non_constant_p = true;
5219 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5222 expr = fold_non_dependent_expr (expr);
5224 /* If we have an ellipsis, then this is an expression
5226 if (allow_expansion_p
5227 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5229 /* Consume the `...'. */
5230 cp_lexer_consume_token (parser->lexer);
5232 /* Build the argument pack. */
5233 expr = make_pack_expansion (expr);
5236 /* Add it to the list. We add error_mark_node
5237 expressions to the list, so that we can still tell if
5238 the correct form for a parenthesized expression-list
5239 is found. That gives better errors. */
5240 VEC_safe_push (tree, gc, expression_list, expr);
5242 if (expr == error_mark_node)
5246 /* After the first item, attribute lists look the same as
5247 expression lists. */
5248 is_attribute_list = false;
5251 /* If the next token isn't a `,', then we are done. */
5252 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5255 /* Otherwise, consume the `,' and keep going. */
5256 cp_lexer_consume_token (parser->lexer);
5259 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5264 /* We try and resync to an unnested comma, as that will give the
5265 user better diagnostics. */
5266 ending = cp_parser_skip_to_closing_parenthesis (parser,
5267 /*recovering=*/true,
5269 /*consume_paren=*/true);
5274 parser->greater_than_is_operator_p
5275 = saved_greater_than_is_operator_p;
5280 parser->greater_than_is_operator_p
5281 = saved_greater_than_is_operator_p;
5284 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5286 return expression_list;
5289 /* Parse a pseudo-destructor-name.
5291 pseudo-destructor-name:
5292 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5293 :: [opt] nested-name-specifier template template-id :: ~ type-name
5294 :: [opt] nested-name-specifier [opt] ~ type-name
5296 If either of the first two productions is used, sets *SCOPE to the
5297 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5298 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5299 or ERROR_MARK_NODE if the parse fails. */
5302 cp_parser_pseudo_destructor_name (cp_parser* parser,
5306 bool nested_name_specifier_p;
5308 /* Assume that things will not work out. */
5309 *type = error_mark_node;
5311 /* Look for the optional `::' operator. */
5312 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5313 /* Look for the optional nested-name-specifier. */
5314 nested_name_specifier_p
5315 = (cp_parser_nested_name_specifier_opt (parser,
5316 /*typename_keyword_p=*/false,
5317 /*check_dependency_p=*/true,
5319 /*is_declaration=*/false)
5321 /* Now, if we saw a nested-name-specifier, we might be doing the
5322 second production. */
5323 if (nested_name_specifier_p
5324 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5326 /* Consume the `template' keyword. */
5327 cp_lexer_consume_token (parser->lexer);
5328 /* Parse the template-id. */
5329 cp_parser_template_id (parser,
5330 /*template_keyword_p=*/true,
5331 /*check_dependency_p=*/false,
5332 /*is_declaration=*/true);
5333 /* Look for the `::' token. */
5334 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5336 /* If the next token is not a `~', then there might be some
5337 additional qualification. */
5338 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5340 /* At this point, we're looking for "type-name :: ~". The type-name
5341 must not be a class-name, since this is a pseudo-destructor. So,
5342 it must be either an enum-name, or a typedef-name -- both of which
5343 are just identifiers. So, we peek ahead to check that the "::"
5344 and "~" tokens are present; if they are not, then we can avoid
5345 calling type_name. */
5346 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5347 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5348 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5350 cp_parser_error (parser, "non-scalar type");
5354 /* Look for the type-name. */
5355 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5356 if (*scope == error_mark_node)
5359 /* Look for the `::' token. */
5360 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5365 /* Look for the `~'. */
5366 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5367 /* Look for the type-name again. We are not responsible for
5368 checking that it matches the first type-name. */
5369 *type = cp_parser_nonclass_name (parser);
5372 /* Parse a unary-expression.
5378 unary-operator cast-expression
5379 sizeof unary-expression
5387 __extension__ cast-expression
5388 __alignof__ unary-expression
5389 __alignof__ ( type-id )
5390 __real__ cast-expression
5391 __imag__ cast-expression
5394 ADDRESS_P is true iff the unary-expression is appearing as the
5395 operand of the `&' operator. CAST_P is true if this expression is
5396 the target of a cast.
5398 Returns a representation of the expression. */
5401 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5405 enum tree_code unary_operator;
5407 /* Peek at the next token. */
5408 token = cp_lexer_peek_token (parser->lexer);
5409 /* Some keywords give away the kind of expression. */
5410 if (token->type == CPP_KEYWORD)
5412 enum rid keyword = token->keyword;
5422 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5423 /* Consume the token. */
5424 cp_lexer_consume_token (parser->lexer);
5425 /* Parse the operand. */
5426 operand = cp_parser_sizeof_operand (parser, keyword);
5428 if (TYPE_P (operand))
5429 return cxx_sizeof_or_alignof_type (operand, op, true);
5431 return cxx_sizeof_or_alignof_expr (operand, op, true);
5435 return cp_parser_new_expression (parser);
5438 return cp_parser_delete_expression (parser);
5442 /* The saved value of the PEDANTIC flag. */
5446 /* Save away the PEDANTIC flag. */
5447 cp_parser_extension_opt (parser, &saved_pedantic);
5448 /* Parse the cast-expression. */
5449 expr = cp_parser_simple_cast_expression (parser);
5450 /* Restore the PEDANTIC flag. */
5451 pedantic = saved_pedantic;
5461 /* Consume the `__real__' or `__imag__' token. */
5462 cp_lexer_consume_token (parser->lexer);
5463 /* Parse the cast-expression. */
5464 expression = cp_parser_simple_cast_expression (parser);
5465 /* Create the complete representation. */
5466 return build_x_unary_op ((keyword == RID_REALPART
5467 ? REALPART_EXPR : IMAGPART_EXPR),
5469 tf_warning_or_error);
5478 /* Look for the `:: new' and `:: delete', which also signal the
5479 beginning of a new-expression, or delete-expression,
5480 respectively. If the next token is `::', then it might be one of
5482 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5486 /* See if the token after the `::' is one of the keywords in
5487 which we're interested. */
5488 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5489 /* If it's `new', we have a new-expression. */
5490 if (keyword == RID_NEW)
5491 return cp_parser_new_expression (parser);
5492 /* Similarly, for `delete'. */
5493 else if (keyword == RID_DELETE)
5494 return cp_parser_delete_expression (parser);
5497 /* Look for a unary operator. */
5498 unary_operator = cp_parser_unary_operator (token);
5499 /* The `++' and `--' operators can be handled similarly, even though
5500 they are not technically unary-operators in the grammar. */
5501 if (unary_operator == ERROR_MARK)
5503 if (token->type == CPP_PLUS_PLUS)
5504 unary_operator = PREINCREMENT_EXPR;
5505 else if (token->type == CPP_MINUS_MINUS)
5506 unary_operator = PREDECREMENT_EXPR;
5507 /* Handle the GNU address-of-label extension. */
5508 else if (cp_parser_allow_gnu_extensions_p (parser)
5509 && token->type == CPP_AND_AND)
5513 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5515 /* Consume the '&&' token. */
5516 cp_lexer_consume_token (parser->lexer);
5517 /* Look for the identifier. */
5518 identifier = cp_parser_identifier (parser);
5519 /* Create an expression representing the address. */
5520 expression = finish_label_address_expr (identifier, loc);
5521 if (cp_parser_non_integral_constant_expression (parser,
5522 "the address of a label"))
5523 expression = error_mark_node;
5527 if (unary_operator != ERROR_MARK)
5529 tree cast_expression;
5530 tree expression = error_mark_node;
5531 const char *non_constant_p = NULL;
5533 /* Consume the operator token. */
5534 token = cp_lexer_consume_token (parser->lexer);
5535 /* Parse the cast-expression. */
5537 = cp_parser_cast_expression (parser,
5538 unary_operator == ADDR_EXPR,
5539 /*cast_p=*/false, pidk);
5540 /* Now, build an appropriate representation. */
5541 switch (unary_operator)
5544 non_constant_p = "%<*%>";
5545 expression = build_x_indirect_ref (cast_expression, "unary *",
5546 tf_warning_or_error);
5550 non_constant_p = "%<&%>";
5553 expression = build_x_unary_op (unary_operator, cast_expression,
5554 tf_warning_or_error);
5557 case PREINCREMENT_EXPR:
5558 case PREDECREMENT_EXPR:
5559 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5560 ? "%<++%>" : "%<--%>");
5562 case UNARY_PLUS_EXPR:
5564 case TRUTH_NOT_EXPR:
5565 expression = finish_unary_op_expr (unary_operator, cast_expression);
5573 && cp_parser_non_integral_constant_expression (parser,
5575 expression = error_mark_node;
5580 return cp_parser_postfix_expression (parser, address_p, cast_p,
5581 /*member_access_only_p=*/false,
5585 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5586 unary-operator, the corresponding tree code is returned. */
5588 static enum tree_code
5589 cp_parser_unary_operator (cp_token* token)
5591 switch (token->type)
5594 return INDIRECT_REF;
5600 return UNARY_PLUS_EXPR;
5606 return TRUTH_NOT_EXPR;
5609 return BIT_NOT_EXPR;
5616 /* Parse a new-expression.
5619 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5620 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5622 Returns a representation of the expression. */
5625 cp_parser_new_expression (cp_parser* parser)
5627 bool global_scope_p;
5628 VEC(tree,gc) *placement;
5630 VEC(tree,gc) *initializer;
5634 /* Look for the optional `::' operator. */
5636 = (cp_parser_global_scope_opt (parser,
5637 /*current_scope_valid_p=*/false)
5639 /* Look for the `new' operator. */
5640 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5641 /* There's no easy way to tell a new-placement from the
5642 `( type-id )' construct. */
5643 cp_parser_parse_tentatively (parser);
5644 /* Look for a new-placement. */
5645 placement = cp_parser_new_placement (parser);
5646 /* If that didn't work out, there's no new-placement. */
5647 if (!cp_parser_parse_definitely (parser))
5649 if (placement != NULL)
5650 release_tree_vector (placement);
5654 /* If the next token is a `(', then we have a parenthesized
5656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5659 /* Consume the `('. */
5660 cp_lexer_consume_token (parser->lexer);
5661 /* Parse the type-id. */
5662 type = cp_parser_type_id (parser);
5663 /* Look for the closing `)'. */
5664 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5665 token = cp_lexer_peek_token (parser->lexer);
5666 /* There should not be a direct-new-declarator in this production,
5667 but GCC used to allowed this, so we check and emit a sensible error
5668 message for this case. */
5669 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5671 error ("%Harray bound forbidden after parenthesized type-id",
5673 inform (token->location,
5674 "try removing the parentheses around the type-id");
5675 cp_parser_direct_new_declarator (parser);
5679 /* Otherwise, there must be a new-type-id. */
5681 type = cp_parser_new_type_id (parser, &nelts);
5683 /* If the next token is a `(' or '{', then we have a new-initializer. */
5684 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5685 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5686 initializer = cp_parser_new_initializer (parser);
5690 /* A new-expression may not appear in an integral constant
5692 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5693 ret = error_mark_node;
5696 /* Create a representation of the new-expression. */
5697 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5698 tf_warning_or_error);
5701 if (placement != NULL)
5702 release_tree_vector (placement);
5703 if (initializer != NULL)
5704 release_tree_vector (initializer);
5709 /* Parse a new-placement.
5714 Returns the same representation as for an expression-list. */
5716 static VEC(tree,gc) *
5717 cp_parser_new_placement (cp_parser* parser)
5719 VEC(tree,gc) *expression_list;
5721 /* Parse the expression-list. */
5722 expression_list = (cp_parser_parenthesized_expression_list
5723 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5724 /*non_constant_p=*/NULL));
5726 return expression_list;
5729 /* Parse a new-type-id.
5732 type-specifier-seq new-declarator [opt]
5734 Returns the TYPE allocated. If the new-type-id indicates an array
5735 type, *NELTS is set to the number of elements in the last array
5736 bound; the TYPE will not include the last array bound. */
5739 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5741 cp_decl_specifier_seq type_specifier_seq;
5742 cp_declarator *new_declarator;
5743 cp_declarator *declarator;
5744 cp_declarator *outer_declarator;
5745 const char *saved_message;
5748 /* The type-specifier sequence must not contain type definitions.
5749 (It cannot contain declarations of new types either, but if they
5750 are not definitions we will catch that because they are not
5752 saved_message = parser->type_definition_forbidden_message;
5753 parser->type_definition_forbidden_message
5754 = "types may not be defined in a new-type-id";
5755 /* Parse the type-specifier-seq. */
5756 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5757 &type_specifier_seq);
5758 /* Restore the old message. */
5759 parser->type_definition_forbidden_message = saved_message;
5760 /* Parse the new-declarator. */
5761 new_declarator = cp_parser_new_declarator_opt (parser);
5763 /* Determine the number of elements in the last array dimension, if
5766 /* Skip down to the last array dimension. */
5767 declarator = new_declarator;
5768 outer_declarator = NULL;
5769 while (declarator && (declarator->kind == cdk_pointer
5770 || declarator->kind == cdk_ptrmem))
5772 outer_declarator = declarator;
5773 declarator = declarator->declarator;
5776 && declarator->kind == cdk_array
5777 && declarator->declarator
5778 && declarator->declarator->kind == cdk_array)
5780 outer_declarator = declarator;
5781 declarator = declarator->declarator;
5784 if (declarator && declarator->kind == cdk_array)
5786 *nelts = declarator->u.array.bounds;
5787 if (*nelts == error_mark_node)
5788 *nelts = integer_one_node;
5790 if (outer_declarator)
5791 outer_declarator->declarator = declarator->declarator;
5793 new_declarator = NULL;
5796 type = groktypename (&type_specifier_seq, new_declarator, false);
5800 /* Parse an (optional) new-declarator.
5803 ptr-operator new-declarator [opt]
5804 direct-new-declarator
5806 Returns the declarator. */
5808 static cp_declarator *
5809 cp_parser_new_declarator_opt (cp_parser* parser)
5811 enum tree_code code;
5813 cp_cv_quals cv_quals;
5815 /* We don't know if there's a ptr-operator next, or not. */
5816 cp_parser_parse_tentatively (parser);
5817 /* Look for a ptr-operator. */
5818 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5819 /* If that worked, look for more new-declarators. */
5820 if (cp_parser_parse_definitely (parser))
5822 cp_declarator *declarator;
5824 /* Parse another optional declarator. */
5825 declarator = cp_parser_new_declarator_opt (parser);
5827 return cp_parser_make_indirect_declarator
5828 (code, type, cv_quals, declarator);
5831 /* If the next token is a `[', there is a direct-new-declarator. */
5832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5833 return cp_parser_direct_new_declarator (parser);
5838 /* Parse a direct-new-declarator.
5840 direct-new-declarator:
5842 direct-new-declarator [constant-expression]
5846 static cp_declarator *
5847 cp_parser_direct_new_declarator (cp_parser* parser)
5849 cp_declarator *declarator = NULL;
5855 /* Look for the opening `['. */
5856 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5857 /* The first expression is not required to be constant. */
5860 cp_token *token = cp_lexer_peek_token (parser->lexer);
5861 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5862 /* The standard requires that the expression have integral
5863 type. DR 74 adds enumeration types. We believe that the
5864 real intent is that these expressions be handled like the
5865 expression in a `switch' condition, which also allows
5866 classes with a single conversion to integral or
5867 enumeration type. */
5868 if (!processing_template_decl)
5871 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5876 error ("%Hexpression in new-declarator must have integral "
5877 "or enumeration type", &token->location);
5878 expression = error_mark_node;
5882 /* But all the other expressions must be. */
5885 = cp_parser_constant_expression (parser,
5886 /*allow_non_constant=*/false,
5888 /* Look for the closing `]'. */
5889 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5891 /* Add this bound to the declarator. */
5892 declarator = make_array_declarator (declarator, expression);
5894 /* If the next token is not a `[', then there are no more
5896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5903 /* Parse a new-initializer.
5906 ( expression-list [opt] )
5909 Returns a representation of the expression-list. */
5911 static VEC(tree,gc) *
5912 cp_parser_new_initializer (cp_parser* parser)
5914 VEC(tree,gc) *expression_list;
5916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5919 bool expr_non_constant_p;
5920 maybe_warn_cpp0x ("extended initializer lists");
5921 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5922 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5923 expression_list = make_tree_vector_single (t);
5926 expression_list = (cp_parser_parenthesized_expression_list
5927 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5928 /*non_constant_p=*/NULL));
5930 return expression_list;
5933 /* Parse a delete-expression.
5936 :: [opt] delete cast-expression
5937 :: [opt] delete [ ] cast-expression
5939 Returns a representation of the expression. */
5942 cp_parser_delete_expression (cp_parser* parser)
5944 bool global_scope_p;
5948 /* Look for the optional `::' operator. */
5950 = (cp_parser_global_scope_opt (parser,
5951 /*current_scope_valid_p=*/false)
5953 /* Look for the `delete' keyword. */
5954 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5955 /* See if the array syntax is in use. */
5956 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5958 /* Consume the `[' token. */
5959 cp_lexer_consume_token (parser->lexer);
5960 /* Look for the `]' token. */
5961 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5962 /* Remember that this is the `[]' construct. */
5968 /* Parse the cast-expression. */
5969 expression = cp_parser_simple_cast_expression (parser);
5971 /* A delete-expression may not appear in an integral constant
5973 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5974 return error_mark_node;
5976 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5979 /* Returns true if TOKEN may start a cast-expression and false
5983 cp_parser_token_starts_cast_expression (cp_token *token)
5985 switch (token->type)
5991 case CPP_CLOSE_SQUARE:
5992 case CPP_CLOSE_PAREN:
5993 case CPP_CLOSE_BRACE:
5997 case CPP_DEREF_STAR:
6005 case CPP_GREATER_EQ:
6025 /* '[' may start a primary-expression in obj-c++. */
6026 case CPP_OPEN_SQUARE:
6027 return c_dialect_objc ();
6034 /* Parse a cast-expression.
6038 ( type-id ) cast-expression
6040 ADDRESS_P is true iff the unary-expression is appearing as the
6041 operand of the `&' operator. CAST_P is true if this expression is
6042 the target of a cast.
6044 Returns a representation of the expression. */
6047 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6050 /* If it's a `(', then we might be looking at a cast. */
6051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6053 tree type = NULL_TREE;
6054 tree expr = NULL_TREE;
6055 bool compound_literal_p;
6056 const char *saved_message;
6058 /* There's no way to know yet whether or not this is a cast.
6059 For example, `(int (3))' is a unary-expression, while `(int)
6060 3' is a cast. So, we resort to parsing tentatively. */
6061 cp_parser_parse_tentatively (parser);
6062 /* Types may not be defined in a cast. */
6063 saved_message = parser->type_definition_forbidden_message;
6064 parser->type_definition_forbidden_message
6065 = "types may not be defined in casts";
6066 /* Consume the `('. */
6067 cp_lexer_consume_token (parser->lexer);
6068 /* A very tricky bit is that `(struct S) { 3 }' is a
6069 compound-literal (which we permit in C++ as an extension).
6070 But, that construct is not a cast-expression -- it is a
6071 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6072 is legal; if the compound-literal were a cast-expression,
6073 you'd need an extra set of parentheses.) But, if we parse
6074 the type-id, and it happens to be a class-specifier, then we
6075 will commit to the parse at that point, because we cannot
6076 undo the action that is done when creating a new class. So,
6077 then we cannot back up and do a postfix-expression.
6079 Therefore, we scan ahead to the closing `)', and check to see
6080 if the token after the `)' is a `{'. If so, we are not
6081 looking at a cast-expression.
6083 Save tokens so that we can put them back. */
6084 cp_lexer_save_tokens (parser->lexer);
6085 /* Skip tokens until the next token is a closing parenthesis.
6086 If we find the closing `)', and the next token is a `{', then
6087 we are looking at a compound-literal. */
6089 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6090 /*consume_paren=*/true)
6091 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6092 /* Roll back the tokens we skipped. */
6093 cp_lexer_rollback_tokens (parser->lexer);
6094 /* If we were looking at a compound-literal, simulate an error
6095 so that the call to cp_parser_parse_definitely below will
6097 if (compound_literal_p)
6098 cp_parser_simulate_error (parser);
6101 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6102 parser->in_type_id_in_expr_p = true;
6103 /* Look for the type-id. */
6104 type = cp_parser_type_id (parser);
6105 /* Look for the closing `)'. */
6106 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6107 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6110 /* Restore the saved message. */
6111 parser->type_definition_forbidden_message = saved_message;
6113 /* At this point this can only be either a cast or a
6114 parenthesized ctor such as `(T ())' that looks like a cast to
6115 function returning T. */
6116 if (!cp_parser_error_occurred (parser)
6117 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6120 cp_parser_parse_definitely (parser);
6121 expr = cp_parser_cast_expression (parser,
6122 /*address_p=*/false,
6123 /*cast_p=*/true, pidk);
6125 /* Warn about old-style casts, if so requested. */
6126 if (warn_old_style_cast
6127 && !in_system_header
6128 && !VOID_TYPE_P (type)
6129 && current_lang_name != lang_name_c)
6130 warning (OPT_Wold_style_cast, "use of old-style cast");
6132 /* Only type conversions to integral or enumeration types
6133 can be used in constant-expressions. */
6134 if (!cast_valid_in_integral_constant_expression_p (type)
6135 && (cp_parser_non_integral_constant_expression
6137 "a cast to a type other than an integral or "
6138 "enumeration type")))
6139 return error_mark_node;
6141 /* Perform the cast. */
6142 expr = build_c_cast (input_location, type, expr);
6146 cp_parser_abort_tentative_parse (parser);
6149 /* If we get here, then it's not a cast, so it must be a
6150 unary-expression. */
6151 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6154 /* Parse a binary expression of the general form:
6158 pm-expression .* cast-expression
6159 pm-expression ->* cast-expression
6161 multiplicative-expression:
6163 multiplicative-expression * pm-expression
6164 multiplicative-expression / pm-expression
6165 multiplicative-expression % pm-expression
6167 additive-expression:
6168 multiplicative-expression
6169 additive-expression + multiplicative-expression
6170 additive-expression - multiplicative-expression
6174 shift-expression << additive-expression
6175 shift-expression >> additive-expression
6177 relational-expression:
6179 relational-expression < shift-expression
6180 relational-expression > shift-expression
6181 relational-expression <= shift-expression
6182 relational-expression >= shift-expression
6186 relational-expression:
6187 relational-expression <? shift-expression
6188 relational-expression >? shift-expression
6190 equality-expression:
6191 relational-expression
6192 equality-expression == relational-expression
6193 equality-expression != relational-expression
6197 and-expression & equality-expression
6199 exclusive-or-expression:
6201 exclusive-or-expression ^ and-expression
6203 inclusive-or-expression:
6204 exclusive-or-expression
6205 inclusive-or-expression | exclusive-or-expression
6207 logical-and-expression:
6208 inclusive-or-expression
6209 logical-and-expression && inclusive-or-expression
6211 logical-or-expression:
6212 logical-and-expression
6213 logical-or-expression || logical-and-expression
6215 All these are implemented with a single function like:
6218 simple-cast-expression
6219 binary-expression <token> binary-expression
6221 CAST_P is true if this expression is the target of a cast.
6223 The binops_by_token map is used to get the tree codes for each <token> type.
6224 binary-expressions are associated according to a precedence table. */
6226 #define TOKEN_PRECEDENCE(token) \
6227 (((token->type == CPP_GREATER \
6228 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6229 && !parser->greater_than_is_operator_p) \
6230 ? PREC_NOT_OPERATOR \
6231 : binops_by_token[token->type].prec)
6234 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6235 bool no_toplevel_fold_p,
6236 enum cp_parser_prec prec,
6239 cp_parser_expression_stack stack;
6240 cp_parser_expression_stack_entry *sp = &stack[0];
6243 enum tree_code tree_type, lhs_type, rhs_type;
6244 enum cp_parser_prec new_prec, lookahead_prec;
6247 /* Parse the first expression. */
6248 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6249 lhs_type = ERROR_MARK;
6253 /* Get an operator token. */
6254 token = cp_lexer_peek_token (parser->lexer);
6256 if (warn_cxx0x_compat
6257 && token->type == CPP_RSHIFT
6258 && !parser->greater_than_is_operator_p)
6260 warning (OPT_Wc__0x_compat,
6261 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6263 warning (OPT_Wc__0x_compat,
6264 "suggest parentheses around %<>>%> expression");
6267 new_prec = TOKEN_PRECEDENCE (token);
6269 /* Popping an entry off the stack means we completed a subexpression:
6270 - either we found a token which is not an operator (`>' where it is not
6271 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6272 will happen repeatedly;
6273 - or, we found an operator which has lower priority. This is the case
6274 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6276 if (new_prec <= prec)
6285 tree_type = binops_by_token[token->type].tree_type;
6287 /* We used the operator token. */
6288 cp_lexer_consume_token (parser->lexer);
6290 /* For "false && x" or "true || x", x will never be executed;
6291 disable warnings while evaluating it. */
6292 if (tree_type == TRUTH_ANDIF_EXPR)
6293 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6294 else if (tree_type == TRUTH_ORIF_EXPR)
6295 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6297 /* Extract another operand. It may be the RHS of this expression
6298 or the LHS of a new, higher priority expression. */
6299 rhs = cp_parser_simple_cast_expression (parser);
6300 rhs_type = ERROR_MARK;
6302 /* Get another operator token. Look up its precedence to avoid
6303 building a useless (immediately popped) stack entry for common
6304 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6305 token = cp_lexer_peek_token (parser->lexer);
6306 lookahead_prec = TOKEN_PRECEDENCE (token);
6307 if (lookahead_prec > new_prec)
6309 /* ... and prepare to parse the RHS of the new, higher priority
6310 expression. Since precedence levels on the stack are
6311 monotonically increasing, we do not have to care about
6314 sp->tree_type = tree_type;
6316 sp->lhs_type = lhs_type;
6319 lhs_type = rhs_type;
6321 new_prec = lookahead_prec;
6325 lookahead_prec = new_prec;
6326 /* If the stack is not empty, we have parsed into LHS the right side
6327 (`4' in the example above) of an expression we had suspended.
6328 We can use the information on the stack to recover the LHS (`3')
6329 from the stack together with the tree code (`MULT_EXPR'), and
6330 the precedence of the higher level subexpression
6331 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6332 which will be used to actually build the additive expression. */
6335 tree_type = sp->tree_type;
6337 rhs_type = lhs_type;
6339 lhs_type = sp->lhs_type;
6342 /* Undo the disabling of warnings done above. */
6343 if (tree_type == TRUTH_ANDIF_EXPR)
6344 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6345 else if (tree_type == TRUTH_ORIF_EXPR)
6346 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6348 overloaded_p = false;
6349 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6350 ERROR_MARK for everything that is not a binary expression.
6351 This makes warn_about_parentheses miss some warnings that
6352 involve unary operators. For unary expressions we should
6353 pass the correct tree_code unless the unary expression was
6354 surrounded by parentheses.
6356 if (no_toplevel_fold_p
6357 && lookahead_prec <= prec
6359 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6360 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6362 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6363 &overloaded_p, tf_warning_or_error);
6364 lhs_type = tree_type;
6366 /* If the binary operator required the use of an overloaded operator,
6367 then this expression cannot be an integral constant-expression.
6368 An overloaded operator can be used even if both operands are
6369 otherwise permissible in an integral constant-expression if at
6370 least one of the operands is of enumeration type. */
6373 && (cp_parser_non_integral_constant_expression
6374 (parser, "calls to overloaded operators")))
6375 return error_mark_node;
6382 /* Parse the `? expression : assignment-expression' part of a
6383 conditional-expression. The LOGICAL_OR_EXPR is the
6384 logical-or-expression that started the conditional-expression.
6385 Returns a representation of the entire conditional-expression.
6387 This routine is used by cp_parser_assignment_expression.
6389 ? expression : assignment-expression
6393 ? : assignment-expression */
6396 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6399 tree assignment_expr;
6401 /* Consume the `?' token. */
6402 cp_lexer_consume_token (parser->lexer);
6403 if (cp_parser_allow_gnu_extensions_p (parser)
6404 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6406 /* Implicit true clause. */
6408 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6412 /* Parse the expression. */
6413 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6414 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6415 c_inhibit_evaluation_warnings +=
6416 ((logical_or_expr == truthvalue_true_node)
6417 - (logical_or_expr == truthvalue_false_node));
6420 /* The next token should be a `:'. */
6421 cp_parser_require (parser, CPP_COLON, "%<:%>");
6422 /* Parse the assignment-expression. */
6423 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6424 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6426 /* Build the conditional-expression. */
6427 return build_x_conditional_expr (logical_or_expr,
6430 tf_warning_or_error);
6433 /* Parse an assignment-expression.
6435 assignment-expression:
6436 conditional-expression
6437 logical-or-expression assignment-operator assignment_expression
6440 CAST_P is true if this expression is the target of a cast.
6442 Returns a representation for the expression. */
6445 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6450 /* If the next token is the `throw' keyword, then we're looking at
6451 a throw-expression. */
6452 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6453 expr = cp_parser_throw_expression (parser);
6454 /* Otherwise, it must be that we are looking at a
6455 logical-or-expression. */
6458 /* Parse the binary expressions (logical-or-expression). */
6459 expr = cp_parser_binary_expression (parser, cast_p, false,
6460 PREC_NOT_OPERATOR, pidk);
6461 /* If the next token is a `?' then we're actually looking at a
6462 conditional-expression. */
6463 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6464 return cp_parser_question_colon_clause (parser, expr);
6467 enum tree_code assignment_operator;
6469 /* If it's an assignment-operator, we're using the second
6472 = cp_parser_assignment_operator_opt (parser);
6473 if (assignment_operator != ERROR_MARK)
6475 bool non_constant_p;
6477 /* Parse the right-hand side of the assignment. */
6478 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6480 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6481 maybe_warn_cpp0x ("extended initializer lists");
6483 /* An assignment may not appear in a
6484 constant-expression. */
6485 if (cp_parser_non_integral_constant_expression (parser,
6487 return error_mark_node;
6488 /* Build the assignment expression. */
6489 expr = build_x_modify_expr (expr,
6490 assignment_operator,
6492 tf_warning_or_error);
6500 /* Parse an (optional) assignment-operator.
6502 assignment-operator: one of
6503 = *= /= %= += -= >>= <<= &= ^= |=
6507 assignment-operator: one of
6510 If the next token is an assignment operator, the corresponding tree
6511 code is returned, and the token is consumed. For example, for
6512 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6513 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6514 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6515 operator, ERROR_MARK is returned. */
6517 static enum tree_code
6518 cp_parser_assignment_operator_opt (cp_parser* parser)
6523 /* Peek at the next token. */
6524 token = cp_lexer_peek_token (parser->lexer);
6526 switch (token->type)
6537 op = TRUNC_DIV_EXPR;
6541 op = TRUNC_MOD_EXPR;
6573 /* Nothing else is an assignment operator. */
6577 /* If it was an assignment operator, consume it. */
6578 if (op != ERROR_MARK)
6579 cp_lexer_consume_token (parser->lexer);
6584 /* Parse an expression.
6587 assignment-expression
6588 expression , assignment-expression
6590 CAST_P is true if this expression is the target of a cast.
6592 Returns a representation of the expression. */
6595 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6597 tree expression = NULL_TREE;
6601 tree assignment_expression;
6603 /* Parse the next assignment-expression. */
6604 assignment_expression
6605 = cp_parser_assignment_expression (parser, cast_p, pidk);
6606 /* If this is the first assignment-expression, we can just
6609 expression = assignment_expression;
6611 expression = build_x_compound_expr (expression,
6612 assignment_expression,
6613 tf_warning_or_error);
6614 /* If the next token is not a comma, then we are done with the
6616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6618 /* Consume the `,'. */
6619 cp_lexer_consume_token (parser->lexer);
6620 /* A comma operator cannot appear in a constant-expression. */
6621 if (cp_parser_non_integral_constant_expression (parser,
6622 "a comma operator"))
6623 expression = error_mark_node;
6629 /* Parse a constant-expression.
6631 constant-expression:
6632 conditional-expression
6634 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6635 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6636 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6637 is false, NON_CONSTANT_P should be NULL. */
6640 cp_parser_constant_expression (cp_parser* parser,
6641 bool allow_non_constant_p,
6642 bool *non_constant_p)
6644 bool saved_integral_constant_expression_p;
6645 bool saved_allow_non_integral_constant_expression_p;
6646 bool saved_non_integral_constant_expression_p;
6649 /* It might seem that we could simply parse the
6650 conditional-expression, and then check to see if it were
6651 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6652 one that the compiler can figure out is constant, possibly after
6653 doing some simplifications or optimizations. The standard has a
6654 precise definition of constant-expression, and we must honor
6655 that, even though it is somewhat more restrictive.
6661 is not a legal declaration, because `(2, 3)' is not a
6662 constant-expression. The `,' operator is forbidden in a
6663 constant-expression. However, GCC's constant-folding machinery
6664 will fold this operation to an INTEGER_CST for `3'. */
6666 /* Save the old settings. */
6667 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6668 saved_allow_non_integral_constant_expression_p
6669 = parser->allow_non_integral_constant_expression_p;
6670 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6671 /* We are now parsing a constant-expression. */
6672 parser->integral_constant_expression_p = true;
6673 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6674 parser->non_integral_constant_expression_p = false;
6675 /* Although the grammar says "conditional-expression", we parse an
6676 "assignment-expression", which also permits "throw-expression"
6677 and the use of assignment operators. In the case that
6678 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6679 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6680 actually essential that we look for an assignment-expression.
6681 For example, cp_parser_initializer_clauses uses this function to
6682 determine whether a particular assignment-expression is in fact
6684 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6685 /* Restore the old settings. */
6686 parser->integral_constant_expression_p
6687 = saved_integral_constant_expression_p;
6688 parser->allow_non_integral_constant_expression_p
6689 = saved_allow_non_integral_constant_expression_p;
6690 if (allow_non_constant_p)
6691 *non_constant_p = parser->non_integral_constant_expression_p;
6692 else if (parser->non_integral_constant_expression_p)
6693 expression = error_mark_node;
6694 parser->non_integral_constant_expression_p
6695 = saved_non_integral_constant_expression_p;
6700 /* Parse __builtin_offsetof.
6702 offsetof-expression:
6703 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6705 offsetof-member-designator:
6707 | offsetof-member-designator "." id-expression
6708 | offsetof-member-designator "[" expression "]"
6709 | offsetof-member-designator "->" id-expression */
6712 cp_parser_builtin_offsetof (cp_parser *parser)
6714 int save_ice_p, save_non_ice_p;
6719 /* We're about to accept non-integral-constant things, but will
6720 definitely yield an integral constant expression. Save and
6721 restore these values around our local parsing. */
6722 save_ice_p = parser->integral_constant_expression_p;
6723 save_non_ice_p = parser->non_integral_constant_expression_p;
6725 /* Consume the "__builtin_offsetof" token. */
6726 cp_lexer_consume_token (parser->lexer);
6727 /* Consume the opening `('. */
6728 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6729 /* Parse the type-id. */
6730 type = cp_parser_type_id (parser);
6731 /* Look for the `,'. */
6732 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6733 token = cp_lexer_peek_token (parser->lexer);
6735 /* Build the (type *)null that begins the traditional offsetof macro. */
6736 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6737 tf_warning_or_error);
6739 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6740 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6741 true, &dummy, token->location);
6744 token = cp_lexer_peek_token (parser->lexer);
6745 switch (token->type)
6747 case CPP_OPEN_SQUARE:
6748 /* offsetof-member-designator "[" expression "]" */
6749 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6753 /* offsetof-member-designator "->" identifier */
6754 expr = grok_array_decl (expr, integer_zero_node);
6758 /* offsetof-member-designator "." identifier */
6759 cp_lexer_consume_token (parser->lexer);
6760 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6765 case CPP_CLOSE_PAREN:
6766 /* Consume the ")" token. */
6767 cp_lexer_consume_token (parser->lexer);
6771 /* Error. We know the following require will fail, but
6772 that gives the proper error message. */
6773 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6774 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6775 expr = error_mark_node;
6781 /* If we're processing a template, we can't finish the semantics yet.
6782 Otherwise we can fold the entire expression now. */
6783 if (processing_template_decl)
6784 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6786 expr = finish_offsetof (expr);
6789 parser->integral_constant_expression_p = save_ice_p;
6790 parser->non_integral_constant_expression_p = save_non_ice_p;
6795 /* Parse a trait expression. */
6798 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6801 tree type1, type2 = NULL_TREE;
6802 bool binary = false;
6803 cp_decl_specifier_seq decl_specs;
6807 case RID_HAS_NOTHROW_ASSIGN:
6808 kind = CPTK_HAS_NOTHROW_ASSIGN;
6810 case RID_HAS_NOTHROW_CONSTRUCTOR:
6811 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6813 case RID_HAS_NOTHROW_COPY:
6814 kind = CPTK_HAS_NOTHROW_COPY;
6816 case RID_HAS_TRIVIAL_ASSIGN:
6817 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6819 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6820 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6822 case RID_HAS_TRIVIAL_COPY:
6823 kind = CPTK_HAS_TRIVIAL_COPY;
6825 case RID_HAS_TRIVIAL_DESTRUCTOR:
6826 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6828 case RID_HAS_VIRTUAL_DESTRUCTOR:
6829 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6831 case RID_IS_ABSTRACT:
6832 kind = CPTK_IS_ABSTRACT;
6834 case RID_IS_BASE_OF:
6835 kind = CPTK_IS_BASE_OF;
6839 kind = CPTK_IS_CLASS;
6841 case RID_IS_CONVERTIBLE_TO:
6842 kind = CPTK_IS_CONVERTIBLE_TO;
6846 kind = CPTK_IS_EMPTY;
6849 kind = CPTK_IS_ENUM;
6854 case RID_IS_POLYMORPHIC:
6855 kind = CPTK_IS_POLYMORPHIC;
6858 kind = CPTK_IS_UNION;
6864 /* Consume the token. */
6865 cp_lexer_consume_token (parser->lexer);
6867 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6869 type1 = cp_parser_type_id (parser);
6871 if (type1 == error_mark_node)
6872 return error_mark_node;
6874 /* Build a trivial decl-specifier-seq. */
6875 clear_decl_specs (&decl_specs);
6876 decl_specs.type = type1;
6878 /* Call grokdeclarator to figure out what type this is. */
6879 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6880 /*initialized=*/0, /*attrlist=*/NULL);
6884 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6886 type2 = cp_parser_type_id (parser);
6888 if (type2 == error_mark_node)
6889 return error_mark_node;
6891 /* Build a trivial decl-specifier-seq. */
6892 clear_decl_specs (&decl_specs);
6893 decl_specs.type = type2;
6895 /* Call grokdeclarator to figure out what type this is. */
6896 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6897 /*initialized=*/0, /*attrlist=*/NULL);
6900 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6902 /* Complete the trait expression, which may mean either processing
6903 the trait expr now or saving it for template instantiation. */
6904 return finish_trait_expr (kind, type1, type2);
6907 /* Statements [gram.stmt.stmt] */
6909 /* Parse a statement.
6913 expression-statement
6918 declaration-statement
6921 IN_COMPOUND is true when the statement is nested inside a
6922 cp_parser_compound_statement; this matters for certain pragmas.
6924 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6925 is a (possibly labeled) if statement which is not enclosed in braces
6926 and has an else clause. This is used to implement -Wparentheses. */
6929 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6930 bool in_compound, bool *if_p)
6934 location_t statement_location;
6939 /* There is no statement yet. */
6940 statement = NULL_TREE;
6941 /* Peek at the next token. */
6942 token = cp_lexer_peek_token (parser->lexer);
6943 /* Remember the location of the first token in the statement. */
6944 statement_location = token->location;
6945 /* If this is a keyword, then that will often determine what kind of
6946 statement we have. */
6947 if (token->type == CPP_KEYWORD)
6949 enum rid keyword = token->keyword;
6955 /* Looks like a labeled-statement with a case label.
6956 Parse the label, and then use tail recursion to parse
6958 cp_parser_label_for_labeled_statement (parser);
6963 statement = cp_parser_selection_statement (parser, if_p);
6969 statement = cp_parser_iteration_statement (parser);
6976 statement = cp_parser_jump_statement (parser);
6979 /* Objective-C++ exception-handling constructs. */
6982 case RID_AT_FINALLY:
6983 case RID_AT_SYNCHRONIZED:
6985 statement = cp_parser_objc_statement (parser);
6989 statement = cp_parser_try_block (parser);
6993 /* This must be a namespace alias definition. */
6994 cp_parser_declaration_statement (parser);
6998 /* It might be a keyword like `int' that can start a
6999 declaration-statement. */
7003 else if (token->type == CPP_NAME)
7005 /* If the next token is a `:', then we are looking at a
7006 labeled-statement. */
7007 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7008 if (token->type == CPP_COLON)
7010 /* Looks like a labeled-statement with an ordinary label.
7011 Parse the label, and then use tail recursion to parse
7013 cp_parser_label_for_labeled_statement (parser);
7017 /* Anything that starts with a `{' must be a compound-statement. */
7018 else if (token->type == CPP_OPEN_BRACE)
7019 statement = cp_parser_compound_statement (parser, NULL, false);
7020 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7021 a statement all its own. */
7022 else if (token->type == CPP_PRAGMA)
7024 /* Only certain OpenMP pragmas are attached to statements, and thus
7025 are considered statements themselves. All others are not. In
7026 the context of a compound, accept the pragma as a "statement" and
7027 return so that we can check for a close brace. Otherwise we
7028 require a real statement and must go back and read one. */
7030 cp_parser_pragma (parser, pragma_compound);
7031 else if (!cp_parser_pragma (parser, pragma_stmt))
7035 else if (token->type == CPP_EOF)
7037 cp_parser_error (parser, "expected statement");
7041 /* Everything else must be a declaration-statement or an
7042 expression-statement. Try for the declaration-statement
7043 first, unless we are looking at a `;', in which case we know that
7044 we have an expression-statement. */
7047 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7049 cp_parser_parse_tentatively (parser);
7050 /* Try to parse the declaration-statement. */
7051 cp_parser_declaration_statement (parser);
7052 /* If that worked, we're done. */
7053 if (cp_parser_parse_definitely (parser))
7056 /* Look for an expression-statement instead. */
7057 statement = cp_parser_expression_statement (parser, in_statement_expr);
7060 /* Set the line number for the statement. */
7061 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7062 SET_EXPR_LOCATION (statement, statement_location);
7065 /* Parse the label for a labeled-statement, i.e.
7068 case constant-expression :
7072 case constant-expression ... constant-expression : statement
7074 When a label is parsed without errors, the label is added to the
7075 parse tree by the finish_* functions, so this function doesn't
7076 have to return the label. */
7079 cp_parser_label_for_labeled_statement (cp_parser* parser)
7082 tree label = NULL_TREE;
7084 /* The next token should be an identifier. */
7085 token = cp_lexer_peek_token (parser->lexer);
7086 if (token->type != CPP_NAME
7087 && token->type != CPP_KEYWORD)
7089 cp_parser_error (parser, "expected labeled-statement");
7093 switch (token->keyword)
7100 /* Consume the `case' token. */
7101 cp_lexer_consume_token (parser->lexer);
7102 /* Parse the constant-expression. */
7103 expr = cp_parser_constant_expression (parser,
7104 /*allow_non_constant_p=*/false,
7107 ellipsis = cp_lexer_peek_token (parser->lexer);
7108 if (ellipsis->type == CPP_ELLIPSIS)
7110 /* Consume the `...' token. */
7111 cp_lexer_consume_token (parser->lexer);
7113 cp_parser_constant_expression (parser,
7114 /*allow_non_constant_p=*/false,
7116 /* We don't need to emit warnings here, as the common code
7117 will do this for us. */
7120 expr_hi = NULL_TREE;
7122 if (parser->in_switch_statement_p)
7123 finish_case_label (token->location, expr, expr_hi);
7125 error ("%Hcase label %qE not within a switch statement",
7126 &token->location, expr);
7131 /* Consume the `default' token. */
7132 cp_lexer_consume_token (parser->lexer);
7134 if (parser->in_switch_statement_p)
7135 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7137 error ("%Hcase label not within a switch statement", &token->location);
7141 /* Anything else must be an ordinary label. */
7142 label = finish_label_stmt (cp_parser_identifier (parser));
7146 /* Require the `:' token. */
7147 cp_parser_require (parser, CPP_COLON, "%<:%>");
7149 /* An ordinary label may optionally be followed by attributes.
7150 However, this is only permitted if the attributes are then
7151 followed by a semicolon. This is because, for backward
7152 compatibility, when parsing
7153 lab: __attribute__ ((unused)) int i;
7154 we want the attribute to attach to "i", not "lab". */
7155 if (label != NULL_TREE
7156 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7160 cp_parser_parse_tentatively (parser);
7161 attrs = cp_parser_attributes_opt (parser);
7162 if (attrs == NULL_TREE
7163 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7164 cp_parser_abort_tentative_parse (parser);
7165 else if (!cp_parser_parse_definitely (parser))
7168 cplus_decl_attributes (&label, attrs, 0);
7172 /* Parse an expression-statement.
7174 expression-statement:
7177 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7178 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7179 indicates whether this expression-statement is part of an
7180 expression statement. */
7183 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7185 tree statement = NULL_TREE;
7187 /* If the next token is a ';', then there is no expression
7189 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7190 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7192 /* Consume the final `;'. */
7193 cp_parser_consume_semicolon_at_end_of_statement (parser);
7195 if (in_statement_expr
7196 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7197 /* This is the final expression statement of a statement
7199 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7201 statement = finish_expr_stmt (statement);
7208 /* Parse a compound-statement.
7211 { statement-seq [opt] }
7216 { label-declaration-seq [opt] statement-seq [opt] }
7218 label-declaration-seq:
7220 label-declaration-seq label-declaration
7222 Returns a tree representing the statement. */
7225 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7230 /* Consume the `{'. */
7231 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7232 return error_mark_node;
7233 /* Begin the compound-statement. */
7234 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7235 /* If the next keyword is `__label__' we have a label declaration. */
7236 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7237 cp_parser_label_declaration (parser);
7238 /* Parse an (optional) statement-seq. */
7239 cp_parser_statement_seq_opt (parser, in_statement_expr);
7240 /* Finish the compound-statement. */
7241 finish_compound_stmt (compound_stmt);
7242 /* Consume the `}'. */
7243 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7245 return compound_stmt;
7248 /* Parse an (optional) statement-seq.
7252 statement-seq [opt] statement */
7255 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7257 /* Scan statements until there aren't any more. */
7260 cp_token *token = cp_lexer_peek_token (parser->lexer);
7262 /* If we're looking at a `}', then we've run out of statements. */
7263 if (token->type == CPP_CLOSE_BRACE
7264 || token->type == CPP_EOF
7265 || token->type == CPP_PRAGMA_EOL)
7268 /* If we are in a compound statement and find 'else' then
7269 something went wrong. */
7270 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7272 if (parser->in_statement & IN_IF_STMT)
7276 token = cp_lexer_consume_token (parser->lexer);
7277 error ("%H%<else%> without a previous %<if%>", &token->location);
7281 /* Parse the statement. */
7282 cp_parser_statement (parser, in_statement_expr, true, NULL);
7286 /* Parse a selection-statement.
7288 selection-statement:
7289 if ( condition ) statement
7290 if ( condition ) statement else statement
7291 switch ( condition ) statement
7293 Returns the new IF_STMT or SWITCH_STMT.
7295 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7296 is a (possibly labeled) if statement which is not enclosed in
7297 braces and has an else clause. This is used to implement
7301 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7309 /* Peek at the next token. */
7310 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7312 /* See what kind of keyword it is. */
7313 keyword = token->keyword;
7322 /* Look for the `('. */
7323 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7325 cp_parser_skip_to_end_of_statement (parser);
7326 return error_mark_node;
7329 /* Begin the selection-statement. */
7330 if (keyword == RID_IF)
7331 statement = begin_if_stmt ();
7333 statement = begin_switch_stmt ();
7335 /* Parse the condition. */
7336 condition = cp_parser_condition (parser);
7337 /* Look for the `)'. */
7338 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7339 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7340 /*consume_paren=*/true);
7342 if (keyword == RID_IF)
7345 unsigned char in_statement;
7347 /* Add the condition. */
7348 finish_if_stmt_cond (condition, statement);
7350 /* Parse the then-clause. */
7351 in_statement = parser->in_statement;
7352 parser->in_statement |= IN_IF_STMT;
7353 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7355 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7356 add_stmt (build_empty_stmt (loc));
7357 cp_lexer_consume_token (parser->lexer);
7358 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7359 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7360 "empty body in an %<if%> statement");
7364 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7365 parser->in_statement = in_statement;
7367 finish_then_clause (statement);
7369 /* If the next token is `else', parse the else-clause. */
7370 if (cp_lexer_next_token_is_keyword (parser->lexer,
7373 /* Consume the `else' keyword. */
7374 cp_lexer_consume_token (parser->lexer);
7375 begin_else_clause (statement);
7376 /* Parse the else-clause. */
7377 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7380 loc = cp_lexer_peek_token (parser->lexer)->location;
7382 OPT_Wempty_body, "suggest braces around "
7383 "empty body in an %<else%> statement");
7384 add_stmt (build_empty_stmt (loc));
7385 cp_lexer_consume_token (parser->lexer);
7388 cp_parser_implicitly_scoped_statement (parser, NULL);
7390 finish_else_clause (statement);
7392 /* If we are currently parsing a then-clause, then
7393 IF_P will not be NULL. We set it to true to
7394 indicate that this if statement has an else clause.
7395 This may trigger the Wparentheses warning below
7396 when we get back up to the parent if statement. */
7402 /* This if statement does not have an else clause. If
7403 NESTED_IF is true, then the then-clause is an if
7404 statement which does have an else clause. We warn
7405 about the potential ambiguity. */
7407 warning (OPT_Wparentheses,
7408 ("%Hsuggest explicit braces "
7409 "to avoid ambiguous %<else%>"),
7410 EXPR_LOCUS (statement));
7413 /* Now we're all done with the if-statement. */
7414 finish_if_stmt (statement);
7418 bool in_switch_statement_p;
7419 unsigned char in_statement;
7421 /* Add the condition. */
7422 finish_switch_cond (condition, statement);
7424 /* Parse the body of the switch-statement. */
7425 in_switch_statement_p = parser->in_switch_statement_p;
7426 in_statement = parser->in_statement;
7427 parser->in_switch_statement_p = true;
7428 parser->in_statement |= IN_SWITCH_STMT;
7429 cp_parser_implicitly_scoped_statement (parser, NULL);
7430 parser->in_switch_statement_p = in_switch_statement_p;
7431 parser->in_statement = in_statement;
7433 /* Now we're all done with the switch-statement. */
7434 finish_switch_stmt (statement);
7442 cp_parser_error (parser, "expected selection-statement");
7443 return error_mark_node;
7447 /* Parse a condition.
7451 type-specifier-seq declarator = initializer-clause
7452 type-specifier-seq declarator braced-init-list
7457 type-specifier-seq declarator asm-specification [opt]
7458 attributes [opt] = assignment-expression
7460 Returns the expression that should be tested. */
7463 cp_parser_condition (cp_parser* parser)
7465 cp_decl_specifier_seq type_specifiers;
7466 const char *saved_message;
7468 /* Try the declaration first. */
7469 cp_parser_parse_tentatively (parser);
7470 /* New types are not allowed in the type-specifier-seq for a
7472 saved_message = parser->type_definition_forbidden_message;
7473 parser->type_definition_forbidden_message
7474 = "types may not be defined in conditions";
7475 /* Parse the type-specifier-seq. */
7476 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7478 /* Restore the saved message. */
7479 parser->type_definition_forbidden_message = saved_message;
7480 /* If all is well, we might be looking at a declaration. */
7481 if (!cp_parser_error_occurred (parser))
7484 tree asm_specification;
7486 cp_declarator *declarator;
7487 tree initializer = NULL_TREE;
7489 /* Parse the declarator. */
7490 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7491 /*ctor_dtor_or_conv_p=*/NULL,
7492 /*parenthesized_p=*/NULL,
7493 /*member_p=*/false);
7494 /* Parse the attributes. */
7495 attributes = cp_parser_attributes_opt (parser);
7496 /* Parse the asm-specification. */
7497 asm_specification = cp_parser_asm_specification_opt (parser);
7498 /* If the next token is not an `=' or '{', then we might still be
7499 looking at an expression. For example:
7503 looks like a decl-specifier-seq and a declarator -- but then
7504 there is no `=', so this is an expression. */
7505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7506 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7507 cp_parser_simulate_error (parser);
7509 /* If we did see an `=' or '{', then we are looking at a declaration
7511 if (cp_parser_parse_definitely (parser))
7514 bool non_constant_p;
7515 bool flags = LOOKUP_ONLYCONVERTING;
7517 /* Create the declaration. */
7518 decl = start_decl (declarator, &type_specifiers,
7519 /*initialized_p=*/true,
7520 attributes, /*prefix_attributes=*/NULL_TREE,
7523 /* Parse the initializer. */
7524 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7526 initializer = cp_parser_braced_list (parser, &non_constant_p);
7527 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7532 /* Consume the `='. */
7533 cp_parser_require (parser, CPP_EQ, "%<=%>");
7534 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7536 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7537 maybe_warn_cpp0x ("extended initializer lists");
7539 if (!non_constant_p)
7540 initializer = fold_non_dependent_expr (initializer);
7542 /* Process the initializer. */
7543 cp_finish_decl (decl,
7544 initializer, !non_constant_p,
7549 pop_scope (pushed_scope);
7551 return convert_from_reference (decl);
7554 /* If we didn't even get past the declarator successfully, we are
7555 definitely not looking at a declaration. */
7557 cp_parser_abort_tentative_parse (parser);
7559 /* Otherwise, we are looking at an expression. */
7560 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7563 /* Parse an iteration-statement.
7565 iteration-statement:
7566 while ( condition ) statement
7567 do statement while ( expression ) ;
7568 for ( for-init-statement condition [opt] ; expression [opt] )
7571 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7574 cp_parser_iteration_statement (cp_parser* parser)
7579 unsigned char in_statement;
7581 /* Peek at the next token. */
7582 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7584 return error_mark_node;
7586 /* Remember whether or not we are already within an iteration
7588 in_statement = parser->in_statement;
7590 /* See what kind of keyword it is. */
7591 keyword = token->keyword;
7598 /* Begin the while-statement. */
7599 statement = begin_while_stmt ();
7600 /* Look for the `('. */
7601 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7602 /* Parse the condition. */
7603 condition = cp_parser_condition (parser);
7604 finish_while_stmt_cond (condition, statement);
7605 /* Look for the `)'. */
7606 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7607 /* Parse the dependent statement. */
7608 parser->in_statement = IN_ITERATION_STMT;
7609 cp_parser_already_scoped_statement (parser);
7610 parser->in_statement = in_statement;
7611 /* We're done with the while-statement. */
7612 finish_while_stmt (statement);
7620 /* Begin the do-statement. */
7621 statement = begin_do_stmt ();
7622 /* Parse the body of the do-statement. */
7623 parser->in_statement = IN_ITERATION_STMT;
7624 cp_parser_implicitly_scoped_statement (parser, NULL);
7625 parser->in_statement = in_statement;
7626 finish_do_body (statement);
7627 /* Look for the `while' keyword. */
7628 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7629 /* Look for the `('. */
7630 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7631 /* Parse the expression. */
7632 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7633 /* We're done with the do-statement. */
7634 finish_do_stmt (expression, statement);
7635 /* Look for the `)'. */
7636 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7637 /* Look for the `;'. */
7638 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7644 tree condition = NULL_TREE;
7645 tree expression = NULL_TREE;
7647 /* Begin the for-statement. */
7648 statement = begin_for_stmt ();
7649 /* Look for the `('. */
7650 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7651 /* Parse the initialization. */
7652 cp_parser_for_init_statement (parser);
7653 finish_for_init_stmt (statement);
7655 /* If there's a condition, process it. */
7656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7657 condition = cp_parser_condition (parser);
7658 finish_for_cond (condition, statement);
7659 /* Look for the `;'. */
7660 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7662 /* If there's an expression, process it. */
7663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7664 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7665 finish_for_expr (expression, statement);
7666 /* Look for the `)'. */
7667 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7669 /* Parse the body of the for-statement. */
7670 parser->in_statement = IN_ITERATION_STMT;
7671 cp_parser_already_scoped_statement (parser);
7672 parser->in_statement = in_statement;
7674 /* We're done with the for-statement. */
7675 finish_for_stmt (statement);
7680 cp_parser_error (parser, "expected iteration-statement");
7681 statement = error_mark_node;
7688 /* Parse a for-init-statement.
7691 expression-statement
7692 simple-declaration */
7695 cp_parser_for_init_statement (cp_parser* parser)
7697 /* If the next token is a `;', then we have an empty
7698 expression-statement. Grammatically, this is also a
7699 simple-declaration, but an invalid one, because it does not
7700 declare anything. Therefore, if we did not handle this case
7701 specially, we would issue an error message about an invalid
7703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7705 /* We're going to speculatively look for a declaration, falling back
7706 to an expression, if necessary. */
7707 cp_parser_parse_tentatively (parser);
7708 /* Parse the declaration. */
7709 cp_parser_simple_declaration (parser,
7710 /*function_definition_allowed_p=*/false);
7711 /* If the tentative parse failed, then we shall need to look for an
7712 expression-statement. */
7713 if (cp_parser_parse_definitely (parser))
7717 cp_parser_expression_statement (parser, false);
7720 /* Parse a jump-statement.
7725 return expression [opt] ;
7726 return braced-init-list ;
7734 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7737 cp_parser_jump_statement (cp_parser* parser)
7739 tree statement = error_mark_node;
7742 unsigned char in_statement;
7744 /* Peek at the next token. */
7745 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7747 return error_mark_node;
7749 /* See what kind of keyword it is. */
7750 keyword = token->keyword;
7754 in_statement = parser->in_statement & ~IN_IF_STMT;
7755 switch (in_statement)
7758 error ("%Hbreak statement not within loop or switch", &token->location);
7761 gcc_assert ((in_statement & IN_SWITCH_STMT)
7762 || in_statement == IN_ITERATION_STMT);
7763 statement = finish_break_stmt ();
7766 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7769 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7772 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7776 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7779 error ("%Hcontinue statement not within a loop", &token->location);
7781 case IN_ITERATION_STMT:
7783 statement = finish_continue_stmt ();
7786 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7791 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7797 bool expr_non_constant_p;
7799 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7801 maybe_warn_cpp0x ("extended initializer lists");
7802 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7804 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7805 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7807 /* If the next token is a `;', then there is no
7810 /* Build the return-statement. */
7811 statement = finish_return_stmt (expr);
7812 /* Look for the final `;'. */
7813 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7818 /* Create the goto-statement. */
7819 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7821 /* Issue a warning about this use of a GNU extension. */
7822 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7823 /* Consume the '*' token. */
7824 cp_lexer_consume_token (parser->lexer);
7825 /* Parse the dependent expression. */
7826 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7829 finish_goto_stmt (cp_parser_identifier (parser));
7830 /* Look for the final `;'. */
7831 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7835 cp_parser_error (parser, "expected jump-statement");
7842 /* Parse a declaration-statement.
7844 declaration-statement:
7845 block-declaration */
7848 cp_parser_declaration_statement (cp_parser* parser)
7852 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7853 p = obstack_alloc (&declarator_obstack, 0);
7855 /* Parse the block-declaration. */
7856 cp_parser_block_declaration (parser, /*statement_p=*/true);
7858 /* Free any declarators allocated. */
7859 obstack_free (&declarator_obstack, p);
7861 /* Finish off the statement. */
7865 /* Some dependent statements (like `if (cond) statement'), are
7866 implicitly in their own scope. In other words, if the statement is
7867 a single statement (as opposed to a compound-statement), it is
7868 none-the-less treated as if it were enclosed in braces. Any
7869 declarations appearing in the dependent statement are out of scope
7870 after control passes that point. This function parses a statement,
7871 but ensures that is in its own scope, even if it is not a
7874 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7875 is a (possibly labeled) if statement which is not enclosed in
7876 braces and has an else clause. This is used to implement
7879 Returns the new statement. */
7882 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7889 /* Mark if () ; with a special NOP_EXPR. */
7890 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7893 cp_lexer_consume_token (parser->lexer);
7894 statement = add_stmt (build_empty_stmt (loc));
7896 /* if a compound is opened, we simply parse the statement directly. */
7897 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7898 statement = cp_parser_compound_statement (parser, NULL, false);
7899 /* If the token is not a `{', then we must take special action. */
7902 /* Create a compound-statement. */
7903 statement = begin_compound_stmt (0);
7904 /* Parse the dependent-statement. */
7905 cp_parser_statement (parser, NULL_TREE, false, if_p);
7906 /* Finish the dummy compound-statement. */
7907 finish_compound_stmt (statement);
7910 /* Return the statement. */
7914 /* For some dependent statements (like `while (cond) statement'), we
7915 have already created a scope. Therefore, even if the dependent
7916 statement is a compound-statement, we do not want to create another
7920 cp_parser_already_scoped_statement (cp_parser* parser)
7922 /* If the token is a `{', then we must take special action. */
7923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7924 cp_parser_statement (parser, NULL_TREE, false, NULL);
7927 /* Avoid calling cp_parser_compound_statement, so that we
7928 don't create a new scope. Do everything else by hand. */
7929 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7930 /* If the next keyword is `__label__' we have a label declaration. */
7931 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7932 cp_parser_label_declaration (parser);
7933 /* Parse an (optional) statement-seq. */
7934 cp_parser_statement_seq_opt (parser, NULL_TREE);
7935 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7939 /* Declarations [gram.dcl.dcl] */
7941 /* Parse an optional declaration-sequence.
7945 declaration-seq declaration */
7948 cp_parser_declaration_seq_opt (cp_parser* parser)
7954 token = cp_lexer_peek_token (parser->lexer);
7956 if (token->type == CPP_CLOSE_BRACE
7957 || token->type == CPP_EOF
7958 || token->type == CPP_PRAGMA_EOL)
7961 if (token->type == CPP_SEMICOLON)
7963 /* A declaration consisting of a single semicolon is
7964 invalid. Allow it unless we're being pedantic. */
7965 cp_lexer_consume_token (parser->lexer);
7966 if (!in_system_header)
7967 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7971 /* If we're entering or exiting a region that's implicitly
7972 extern "C", modify the lang context appropriately. */
7973 if (!parser->implicit_extern_c && token->implicit_extern_c)
7975 push_lang_context (lang_name_c);
7976 parser->implicit_extern_c = true;
7978 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7980 pop_lang_context ();
7981 parser->implicit_extern_c = false;
7984 if (token->type == CPP_PRAGMA)
7986 /* A top-level declaration can consist solely of a #pragma.
7987 A nested declaration cannot, so this is done here and not
7988 in cp_parser_declaration. (A #pragma at block scope is
7989 handled in cp_parser_statement.) */
7990 cp_parser_pragma (parser, pragma_external);
7994 /* Parse the declaration itself. */
7995 cp_parser_declaration (parser);
7999 /* Parse a declaration.
8004 template-declaration
8005 explicit-instantiation
8006 explicit-specialization
8007 linkage-specification
8008 namespace-definition
8013 __extension__ declaration */
8016 cp_parser_declaration (cp_parser* parser)
8023 /* Check for the `__extension__' keyword. */
8024 if (cp_parser_extension_opt (parser, &saved_pedantic))
8026 /* Parse the qualified declaration. */
8027 cp_parser_declaration (parser);
8028 /* Restore the PEDANTIC flag. */
8029 pedantic = saved_pedantic;
8034 /* Try to figure out what kind of declaration is present. */
8035 token1 = *cp_lexer_peek_token (parser->lexer);
8037 if (token1.type != CPP_EOF)
8038 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8041 token2.type = CPP_EOF;
8042 token2.keyword = RID_MAX;
8045 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8046 p = obstack_alloc (&declarator_obstack, 0);
8048 /* If the next token is `extern' and the following token is a string
8049 literal, then we have a linkage specification. */
8050 if (token1.keyword == RID_EXTERN
8051 && cp_parser_is_string_literal (&token2))
8052 cp_parser_linkage_specification (parser);
8053 /* If the next token is `template', then we have either a template
8054 declaration, an explicit instantiation, or an explicit
8056 else if (token1.keyword == RID_TEMPLATE)
8058 /* `template <>' indicates a template specialization. */
8059 if (token2.type == CPP_LESS
8060 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8061 cp_parser_explicit_specialization (parser);
8062 /* `template <' indicates a template declaration. */
8063 else if (token2.type == CPP_LESS)
8064 cp_parser_template_declaration (parser, /*member_p=*/false);
8065 /* Anything else must be an explicit instantiation. */
8067 cp_parser_explicit_instantiation (parser);
8069 /* If the next token is `export', then we have a template
8071 else if (token1.keyword == RID_EXPORT)
8072 cp_parser_template_declaration (parser, /*member_p=*/false);
8073 /* If the next token is `extern', 'static' or 'inline' and the one
8074 after that is `template', we have a GNU extended explicit
8075 instantiation directive. */
8076 else if (cp_parser_allow_gnu_extensions_p (parser)
8077 && (token1.keyword == RID_EXTERN
8078 || token1.keyword == RID_STATIC
8079 || token1.keyword == RID_INLINE)
8080 && token2.keyword == RID_TEMPLATE)
8081 cp_parser_explicit_instantiation (parser);
8082 /* If the next token is `namespace', check for a named or unnamed
8083 namespace definition. */
8084 else if (token1.keyword == RID_NAMESPACE
8085 && (/* A named namespace definition. */
8086 (token2.type == CPP_NAME
8087 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8089 /* An unnamed namespace definition. */
8090 || token2.type == CPP_OPEN_BRACE
8091 || token2.keyword == RID_ATTRIBUTE))
8092 cp_parser_namespace_definition (parser);
8093 /* An inline (associated) namespace definition. */
8094 else if (token1.keyword == RID_INLINE
8095 && token2.keyword == RID_NAMESPACE)
8096 cp_parser_namespace_definition (parser);
8097 /* Objective-C++ declaration/definition. */
8098 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8099 cp_parser_objc_declaration (parser);
8100 /* We must have either a block declaration or a function
8103 /* Try to parse a block-declaration, or a function-definition. */
8104 cp_parser_block_declaration (parser, /*statement_p=*/false);
8106 /* Free any declarators allocated. */
8107 obstack_free (&declarator_obstack, p);
8110 /* Parse a block-declaration.
8115 namespace-alias-definition
8122 __extension__ block-declaration
8127 static_assert-declaration
8129 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8130 part of a declaration-statement. */
8133 cp_parser_block_declaration (cp_parser *parser,
8139 /* Check for the `__extension__' keyword. */
8140 if (cp_parser_extension_opt (parser, &saved_pedantic))
8142 /* Parse the qualified declaration. */
8143 cp_parser_block_declaration (parser, statement_p);
8144 /* Restore the PEDANTIC flag. */
8145 pedantic = saved_pedantic;
8150 /* Peek at the next token to figure out which kind of declaration is
8152 token1 = cp_lexer_peek_token (parser->lexer);
8154 /* If the next keyword is `asm', we have an asm-definition. */
8155 if (token1->keyword == RID_ASM)
8158 cp_parser_commit_to_tentative_parse (parser);
8159 cp_parser_asm_definition (parser);
8161 /* If the next keyword is `namespace', we have a
8162 namespace-alias-definition. */
8163 else if (token1->keyword == RID_NAMESPACE)
8164 cp_parser_namespace_alias_definition (parser);
8165 /* If the next keyword is `using', we have either a
8166 using-declaration or a using-directive. */
8167 else if (token1->keyword == RID_USING)
8172 cp_parser_commit_to_tentative_parse (parser);
8173 /* If the token after `using' is `namespace', then we have a
8175 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8176 if (token2->keyword == RID_NAMESPACE)
8177 cp_parser_using_directive (parser);
8178 /* Otherwise, it's a using-declaration. */
8180 cp_parser_using_declaration (parser,
8181 /*access_declaration_p=*/false);
8183 /* If the next keyword is `__label__' we have a misplaced label
8185 else if (token1->keyword == RID_LABEL)
8187 cp_lexer_consume_token (parser->lexer);
8188 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8189 cp_parser_skip_to_end_of_statement (parser);
8190 /* If the next token is now a `;', consume it. */
8191 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8192 cp_lexer_consume_token (parser->lexer);
8194 /* If the next token is `static_assert' we have a static assertion. */
8195 else if (token1->keyword == RID_STATIC_ASSERT)
8196 cp_parser_static_assert (parser, /*member_p=*/false);
8197 /* Anything else must be a simple-declaration. */
8199 cp_parser_simple_declaration (parser, !statement_p);
8202 /* Parse a simple-declaration.
8205 decl-specifier-seq [opt] init-declarator-list [opt] ;
8207 init-declarator-list:
8209 init-declarator-list , init-declarator
8211 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8212 function-definition as a simple-declaration. */
8215 cp_parser_simple_declaration (cp_parser* parser,
8216 bool function_definition_allowed_p)
8218 cp_decl_specifier_seq decl_specifiers;
8219 int declares_class_or_enum;
8220 bool saw_declarator;
8222 /* Defer access checks until we know what is being declared; the
8223 checks for names appearing in the decl-specifier-seq should be
8224 done as if we were in the scope of the thing being declared. */
8225 push_deferring_access_checks (dk_deferred);
8227 /* Parse the decl-specifier-seq. We have to keep track of whether
8228 or not the decl-specifier-seq declares a named class or
8229 enumeration type, since that is the only case in which the
8230 init-declarator-list is allowed to be empty.
8234 In a simple-declaration, the optional init-declarator-list can be
8235 omitted only when declaring a class or enumeration, that is when
8236 the decl-specifier-seq contains either a class-specifier, an
8237 elaborated-type-specifier, or an enum-specifier. */
8238 cp_parser_decl_specifier_seq (parser,
8239 CP_PARSER_FLAGS_OPTIONAL,
8241 &declares_class_or_enum);
8242 /* We no longer need to defer access checks. */
8243 stop_deferring_access_checks ();
8245 /* In a block scope, a valid declaration must always have a
8246 decl-specifier-seq. By not trying to parse declarators, we can
8247 resolve the declaration/expression ambiguity more quickly. */
8248 if (!function_definition_allowed_p
8249 && !decl_specifiers.any_specifiers_p)
8251 cp_parser_error (parser, "expected declaration");
8255 /* If the next two tokens are both identifiers, the code is
8256 erroneous. The usual cause of this situation is code like:
8260 where "T" should name a type -- but does not. */
8261 if (!decl_specifiers.type
8262 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8264 /* If parsing tentatively, we should commit; we really are
8265 looking at a declaration. */
8266 cp_parser_commit_to_tentative_parse (parser);
8271 /* If we have seen at least one decl-specifier, and the next token
8272 is not a parenthesis, then we must be looking at a declaration.
8273 (After "int (" we might be looking at a functional cast.) */
8274 if (decl_specifiers.any_specifiers_p
8275 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8276 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8277 && !cp_parser_error_occurred (parser))
8278 cp_parser_commit_to_tentative_parse (parser);
8280 /* Keep going until we hit the `;' at the end of the simple
8282 saw_declarator = false;
8283 while (cp_lexer_next_token_is_not (parser->lexer,
8287 bool function_definition_p;
8292 /* If we are processing next declarator, coma is expected */
8293 token = cp_lexer_peek_token (parser->lexer);
8294 gcc_assert (token->type == CPP_COMMA);
8295 cp_lexer_consume_token (parser->lexer);
8298 saw_declarator = true;
8300 /* Parse the init-declarator. */
8301 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8303 function_definition_allowed_p,
8305 declares_class_or_enum,
8306 &function_definition_p);
8307 /* If an error occurred while parsing tentatively, exit quickly.
8308 (That usually happens when in the body of a function; each
8309 statement is treated as a declaration-statement until proven
8311 if (cp_parser_error_occurred (parser))
8313 /* Handle function definitions specially. */
8314 if (function_definition_p)
8316 /* If the next token is a `,', then we are probably
8317 processing something like:
8321 which is erroneous. */
8322 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8324 cp_token *token = cp_lexer_peek_token (parser->lexer);
8325 error ("%Hmixing declarations and function-definitions is forbidden",
8328 /* Otherwise, we're done with the list of declarators. */
8331 pop_deferring_access_checks ();
8335 /* The next token should be either a `,' or a `;'. */
8336 token = cp_lexer_peek_token (parser->lexer);
8337 /* If it's a `,', there are more declarators to come. */
8338 if (token->type == CPP_COMMA)
8339 /* will be consumed next time around */;
8340 /* If it's a `;', we are done. */
8341 else if (token->type == CPP_SEMICOLON)
8343 /* Anything else is an error. */
8346 /* If we have already issued an error message we don't need
8347 to issue another one. */
8348 if (decl != error_mark_node
8349 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8350 cp_parser_error (parser, "expected %<,%> or %<;%>");
8351 /* Skip tokens until we reach the end of the statement. */
8352 cp_parser_skip_to_end_of_statement (parser);
8353 /* If the next token is now a `;', consume it. */
8354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8355 cp_lexer_consume_token (parser->lexer);
8358 /* After the first time around, a function-definition is not
8359 allowed -- even if it was OK at first. For example:
8364 function_definition_allowed_p = false;
8367 /* Issue an error message if no declarators are present, and the
8368 decl-specifier-seq does not itself declare a class or
8370 if (!saw_declarator)
8372 if (cp_parser_declares_only_class_p (parser))
8373 shadow_tag (&decl_specifiers);
8374 /* Perform any deferred access checks. */
8375 perform_deferred_access_checks ();
8378 /* Consume the `;'. */
8379 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8382 pop_deferring_access_checks ();
8385 /* Parse a decl-specifier-seq.
8388 decl-specifier-seq [opt] decl-specifier
8391 storage-class-specifier
8402 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8404 The parser flags FLAGS is used to control type-specifier parsing.
8406 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8409 1: one of the decl-specifiers is an elaborated-type-specifier
8410 (i.e., a type declaration)
8411 2: one of the decl-specifiers is an enum-specifier or a
8412 class-specifier (i.e., a type definition)
8417 cp_parser_decl_specifier_seq (cp_parser* parser,
8418 cp_parser_flags flags,
8419 cp_decl_specifier_seq *decl_specs,
8420 int* declares_class_or_enum)
8422 bool constructor_possible_p = !parser->in_declarator_p;
8423 cp_token *start_token = NULL;
8425 /* Clear DECL_SPECS. */
8426 clear_decl_specs (decl_specs);
8428 /* Assume no class or enumeration type is declared. */
8429 *declares_class_or_enum = 0;
8431 /* Keep reading specifiers until there are no more to read. */
8435 bool found_decl_spec;
8438 /* Peek at the next token. */
8439 token = cp_lexer_peek_token (parser->lexer);
8441 /* Save the first token of the decl spec list for error
8444 start_token = token;
8445 /* Handle attributes. */
8446 if (token->keyword == RID_ATTRIBUTE)
8448 /* Parse the attributes. */
8449 decl_specs->attributes
8450 = chainon (decl_specs->attributes,
8451 cp_parser_attributes_opt (parser));
8454 /* Assume we will find a decl-specifier keyword. */
8455 found_decl_spec = true;
8456 /* If the next token is an appropriate keyword, we can simply
8457 add it to the list. */
8458 switch (token->keyword)
8463 if (!at_class_scope_p ())
8465 error ("%H%<friend%> used outside of class", &token->location);
8466 cp_lexer_purge_token (parser->lexer);
8470 ++decl_specs->specs[(int) ds_friend];
8471 /* Consume the token. */
8472 cp_lexer_consume_token (parser->lexer);
8476 /* function-specifier:
8483 cp_parser_function_specifier_opt (parser, decl_specs);
8489 ++decl_specs->specs[(int) ds_typedef];
8490 /* Consume the token. */
8491 cp_lexer_consume_token (parser->lexer);
8492 /* A constructor declarator cannot appear in a typedef. */
8493 constructor_possible_p = false;
8494 /* The "typedef" keyword can only occur in a declaration; we
8495 may as well commit at this point. */
8496 cp_parser_commit_to_tentative_parse (parser);
8498 if (decl_specs->storage_class != sc_none)
8499 decl_specs->conflicting_specifiers_p = true;
8502 /* storage-class-specifier:
8512 if (cxx_dialect == cxx98)
8514 /* Consume the token. */
8515 cp_lexer_consume_token (parser->lexer);
8517 /* Complain about `auto' as a storage specifier, if
8518 we're complaining about C++0x compatibility. */
8521 "%H%<auto%> will change meaning in C++0x; please remove it",
8524 /* Set the storage class anyway. */
8525 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8529 /* C++0x auto type-specifier. */
8530 found_decl_spec = false;
8537 /* Consume the token. */
8538 cp_lexer_consume_token (parser->lexer);
8539 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8543 /* Consume the token. */
8544 cp_lexer_consume_token (parser->lexer);
8545 ++decl_specs->specs[(int) ds_thread];
8549 /* We did not yet find a decl-specifier yet. */
8550 found_decl_spec = false;
8554 /* Constructors are a special case. The `S' in `S()' is not a
8555 decl-specifier; it is the beginning of the declarator. */
8558 && constructor_possible_p
8559 && (cp_parser_constructor_declarator_p
8560 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8562 /* If we don't have a DECL_SPEC yet, then we must be looking at
8563 a type-specifier. */
8564 if (!found_decl_spec && !constructor_p)
8566 int decl_spec_declares_class_or_enum;
8567 bool is_cv_qualifier;
8571 = cp_parser_type_specifier (parser, flags,
8573 /*is_declaration=*/true,
8574 &decl_spec_declares_class_or_enum,
8576 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8578 /* If this type-specifier referenced a user-defined type
8579 (a typedef, class-name, etc.), then we can't allow any
8580 more such type-specifiers henceforth.
8584 The longest sequence of decl-specifiers that could
8585 possibly be a type name is taken as the
8586 decl-specifier-seq of a declaration. The sequence shall
8587 be self-consistent as described below.
8591 As a general rule, at most one type-specifier is allowed
8592 in the complete decl-specifier-seq of a declaration. The
8593 only exceptions are the following:
8595 -- const or volatile can be combined with any other
8598 -- signed or unsigned can be combined with char, long,
8606 void g (const int Pc);
8608 Here, Pc is *not* part of the decl-specifier seq; it's
8609 the declarator. Therefore, once we see a type-specifier
8610 (other than a cv-qualifier), we forbid any additional
8611 user-defined types. We *do* still allow things like `int
8612 int' to be considered a decl-specifier-seq, and issue the
8613 error message later. */
8614 if (type_spec && !is_cv_qualifier)
8615 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8616 /* A constructor declarator cannot follow a type-specifier. */
8619 constructor_possible_p = false;
8620 found_decl_spec = true;
8624 /* If we still do not have a DECL_SPEC, then there are no more
8626 if (!found_decl_spec)
8629 decl_specs->any_specifiers_p = true;
8630 /* After we see one decl-specifier, further decl-specifiers are
8632 flags |= CP_PARSER_FLAGS_OPTIONAL;
8635 cp_parser_check_decl_spec (decl_specs, start_token->location);
8637 /* Don't allow a friend specifier with a class definition. */
8638 if (decl_specs->specs[(int) ds_friend] != 0
8639 && (*declares_class_or_enum & 2))
8640 error ("%Hclass definition may not be declared a friend",
8641 &start_token->location);
8644 /* Parse an (optional) storage-class-specifier.
8646 storage-class-specifier:
8655 storage-class-specifier:
8658 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8661 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8663 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8666 if (cxx_dialect != cxx98)
8668 /* Fall through for C++98. */
8675 /* Consume the token. */
8676 return cp_lexer_consume_token (parser->lexer)->u.value;
8683 /* Parse an (optional) function-specifier.
8690 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8691 Updates DECL_SPECS, if it is non-NULL. */
8694 cp_parser_function_specifier_opt (cp_parser* parser,
8695 cp_decl_specifier_seq *decl_specs)
8697 cp_token *token = cp_lexer_peek_token (parser->lexer);
8698 switch (token->keyword)
8702 ++decl_specs->specs[(int) ds_inline];
8706 /* 14.5.2.3 [temp.mem]
8708 A member function template shall not be virtual. */
8709 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8710 error ("%Htemplates may not be %<virtual%>", &token->location);
8711 else if (decl_specs)
8712 ++decl_specs->specs[(int) ds_virtual];
8717 ++decl_specs->specs[(int) ds_explicit];
8724 /* Consume the token. */
8725 return cp_lexer_consume_token (parser->lexer)->u.value;
8728 /* Parse a linkage-specification.
8730 linkage-specification:
8731 extern string-literal { declaration-seq [opt] }
8732 extern string-literal declaration */
8735 cp_parser_linkage_specification (cp_parser* parser)
8739 /* Look for the `extern' keyword. */
8740 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8742 /* Look for the string-literal. */
8743 linkage = cp_parser_string_literal (parser, false, false);
8745 /* Transform the literal into an identifier. If the literal is a
8746 wide-character string, or contains embedded NULs, then we can't
8747 handle it as the user wants. */
8748 if (strlen (TREE_STRING_POINTER (linkage))
8749 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8751 cp_parser_error (parser, "invalid linkage-specification");
8752 /* Assume C++ linkage. */
8753 linkage = lang_name_cplusplus;
8756 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8758 /* We're now using the new linkage. */
8759 push_lang_context (linkage);
8761 /* If the next token is a `{', then we're using the first
8763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8765 /* Consume the `{' token. */
8766 cp_lexer_consume_token (parser->lexer);
8767 /* Parse the declarations. */
8768 cp_parser_declaration_seq_opt (parser);
8769 /* Look for the closing `}'. */
8770 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8772 /* Otherwise, there's just one declaration. */
8775 bool saved_in_unbraced_linkage_specification_p;
8777 saved_in_unbraced_linkage_specification_p
8778 = parser->in_unbraced_linkage_specification_p;
8779 parser->in_unbraced_linkage_specification_p = true;
8780 cp_parser_declaration (parser);
8781 parser->in_unbraced_linkage_specification_p
8782 = saved_in_unbraced_linkage_specification_p;
8785 /* We're done with the linkage-specification. */
8786 pop_lang_context ();
8789 /* Parse a static_assert-declaration.
8791 static_assert-declaration:
8792 static_assert ( constant-expression , string-literal ) ;
8794 If MEMBER_P, this static_assert is a class member. */
8797 cp_parser_static_assert(cp_parser *parser, bool member_p)
8802 location_t saved_loc;
8804 /* Peek at the `static_assert' token so we can keep track of exactly
8805 where the static assertion started. */
8806 token = cp_lexer_peek_token (parser->lexer);
8807 saved_loc = token->location;
8809 /* Look for the `static_assert' keyword. */
8810 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8811 "%<static_assert%>"))
8814 /* We know we are in a static assertion; commit to any tentative
8816 if (cp_parser_parsing_tentatively (parser))
8817 cp_parser_commit_to_tentative_parse (parser);
8819 /* Parse the `(' starting the static assertion condition. */
8820 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8822 /* Parse the constant-expression. */
8824 cp_parser_constant_expression (parser,
8825 /*allow_non_constant_p=*/false,
8826 /*non_constant_p=*/NULL);
8828 /* Parse the separating `,'. */
8829 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8831 /* Parse the string-literal message. */
8832 message = cp_parser_string_literal (parser,
8833 /*translate=*/false,
8836 /* A `)' completes the static assertion. */
8837 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8838 cp_parser_skip_to_closing_parenthesis (parser,
8839 /*recovering=*/true,
8841 /*consume_paren=*/true);
8843 /* A semicolon terminates the declaration. */
8844 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8846 /* Complete the static assertion, which may mean either processing
8847 the static assert now or saving it for template instantiation. */
8848 finish_static_assert (condition, message, saved_loc, member_p);
8851 /* Parse a `decltype' type. Returns the type.
8853 simple-type-specifier:
8854 decltype ( expression ) */
8857 cp_parser_decltype (cp_parser *parser)
8860 bool id_expression_or_member_access_p = false;
8861 const char *saved_message;
8862 bool saved_integral_constant_expression_p;
8863 bool saved_non_integral_constant_expression_p;
8864 cp_token *id_expr_start_token;
8866 /* Look for the `decltype' token. */
8867 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8868 return error_mark_node;
8870 /* Types cannot be defined in a `decltype' expression. Save away the
8872 saved_message = parser->type_definition_forbidden_message;
8874 /* And create the new one. */
8875 parser->type_definition_forbidden_message
8876 = "types may not be defined in %<decltype%> expressions";
8878 /* The restrictions on constant-expressions do not apply inside
8879 decltype expressions. */
8880 saved_integral_constant_expression_p
8881 = parser->integral_constant_expression_p;
8882 saved_non_integral_constant_expression_p
8883 = parser->non_integral_constant_expression_p;
8884 parser->integral_constant_expression_p = false;
8886 /* Do not actually evaluate the expression. */
8887 ++cp_unevaluated_operand;
8889 /* Do not warn about problems with the expression. */
8890 ++c_inhibit_evaluation_warnings;
8892 /* Parse the opening `('. */
8893 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8894 return error_mark_node;
8896 /* First, try parsing an id-expression. */
8897 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8898 cp_parser_parse_tentatively (parser);
8899 expr = cp_parser_id_expression (parser,
8900 /*template_keyword_p=*/false,
8901 /*check_dependency_p=*/true,
8902 /*template_p=*/NULL,
8903 /*declarator_p=*/false,
8904 /*optional_p=*/false);
8906 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8908 bool non_integral_constant_expression_p = false;
8909 tree id_expression = expr;
8911 const char *error_msg;
8913 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8914 /* Lookup the name we got back from the id-expression. */
8915 expr = cp_parser_lookup_name (parser, expr,
8917 /*is_template=*/false,
8918 /*is_namespace=*/false,
8919 /*check_dependency=*/true,
8920 /*ambiguous_decls=*/NULL,
8921 id_expr_start_token->location);
8924 && expr != error_mark_node
8925 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8926 && TREE_CODE (expr) != TYPE_DECL
8927 && (TREE_CODE (expr) != BIT_NOT_EXPR
8928 || !TYPE_P (TREE_OPERAND (expr, 0)))
8929 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8931 /* Complete lookup of the id-expression. */
8932 expr = (finish_id_expression
8933 (id_expression, expr, parser->scope, &idk,
8934 /*integral_constant_expression_p=*/false,
8935 /*allow_non_integral_constant_expression_p=*/true,
8936 &non_integral_constant_expression_p,
8937 /*template_p=*/false,
8939 /*address_p=*/false,
8940 /*template_arg_p=*/false,
8942 id_expr_start_token->location));
8944 if (expr == error_mark_node)
8945 /* We found an id-expression, but it was something that we
8946 should not have found. This is an error, not something
8947 we can recover from, so note that we found an
8948 id-expression and we'll recover as gracefully as
8950 id_expression_or_member_access_p = true;
8954 && expr != error_mark_node
8955 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8956 /* We have an id-expression. */
8957 id_expression_or_member_access_p = true;
8960 if (!id_expression_or_member_access_p)
8962 /* Abort the id-expression parse. */
8963 cp_parser_abort_tentative_parse (parser);
8965 /* Parsing tentatively, again. */
8966 cp_parser_parse_tentatively (parser);
8968 /* Parse a class member access. */
8969 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8971 /*member_access_only_p=*/true, NULL);
8974 && expr != error_mark_node
8975 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8976 /* We have an id-expression. */
8977 id_expression_or_member_access_p = true;
8980 if (id_expression_or_member_access_p)
8981 /* We have parsed the complete id-expression or member access. */
8982 cp_parser_parse_definitely (parser);
8985 /* Abort our attempt to parse an id-expression or member access
8987 cp_parser_abort_tentative_parse (parser);
8989 /* Parse a full expression. */
8990 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8993 /* Go back to evaluating expressions. */
8994 --cp_unevaluated_operand;
8995 --c_inhibit_evaluation_warnings;
8997 /* Restore the old message and the integral constant expression
8999 parser->type_definition_forbidden_message = saved_message;
9000 parser->integral_constant_expression_p
9001 = saved_integral_constant_expression_p;
9002 parser->non_integral_constant_expression_p
9003 = saved_non_integral_constant_expression_p;
9005 if (expr == error_mark_node)
9007 /* Skip everything up to the closing `)'. */
9008 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9009 /*consume_paren=*/true);
9010 return error_mark_node;
9013 /* Parse to the closing `)'. */
9014 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9016 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9017 /*consume_paren=*/true);
9018 return error_mark_node;
9021 return finish_decltype_type (expr, id_expression_or_member_access_p);
9024 /* Special member functions [gram.special] */
9026 /* Parse a conversion-function-id.
9028 conversion-function-id:
9029 operator conversion-type-id
9031 Returns an IDENTIFIER_NODE representing the operator. */
9034 cp_parser_conversion_function_id (cp_parser* parser)
9038 tree saved_qualifying_scope;
9039 tree saved_object_scope;
9040 tree pushed_scope = NULL_TREE;
9042 /* Look for the `operator' token. */
9043 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9044 return error_mark_node;
9045 /* When we parse the conversion-type-id, the current scope will be
9046 reset. However, we need that information in able to look up the
9047 conversion function later, so we save it here. */
9048 saved_scope = parser->scope;
9049 saved_qualifying_scope = parser->qualifying_scope;
9050 saved_object_scope = parser->object_scope;
9051 /* We must enter the scope of the class so that the names of
9052 entities declared within the class are available in the
9053 conversion-type-id. For example, consider:
9060 S::operator I() { ... }
9062 In order to see that `I' is a type-name in the definition, we
9063 must be in the scope of `S'. */
9065 pushed_scope = push_scope (saved_scope);
9066 /* Parse the conversion-type-id. */
9067 type = cp_parser_conversion_type_id (parser);
9068 /* Leave the scope of the class, if any. */
9070 pop_scope (pushed_scope);
9071 /* Restore the saved scope. */
9072 parser->scope = saved_scope;
9073 parser->qualifying_scope = saved_qualifying_scope;
9074 parser->object_scope = saved_object_scope;
9075 /* If the TYPE is invalid, indicate failure. */
9076 if (type == error_mark_node)
9077 return error_mark_node;
9078 return mangle_conv_op_name_for_type (type);
9081 /* Parse a conversion-type-id:
9084 type-specifier-seq conversion-declarator [opt]
9086 Returns the TYPE specified. */
9089 cp_parser_conversion_type_id (cp_parser* parser)
9092 cp_decl_specifier_seq type_specifiers;
9093 cp_declarator *declarator;
9094 tree type_specified;
9096 /* Parse the attributes. */
9097 attributes = cp_parser_attributes_opt (parser);
9098 /* Parse the type-specifiers. */
9099 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9101 /* If that didn't work, stop. */
9102 if (type_specifiers.type == error_mark_node)
9103 return error_mark_node;
9104 /* Parse the conversion-declarator. */
9105 declarator = cp_parser_conversion_declarator_opt (parser);
9107 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9108 /*initialized=*/0, &attributes);
9110 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9112 /* Don't give this error when parsing tentatively. This happens to
9113 work because we always parse this definitively once. */
9114 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9115 && type_uses_auto (type_specified))
9117 error ("invalid use of %<auto%> in conversion operator");
9118 return error_mark_node;
9121 return type_specified;
9124 /* Parse an (optional) conversion-declarator.
9126 conversion-declarator:
9127 ptr-operator conversion-declarator [opt]
9131 static cp_declarator *
9132 cp_parser_conversion_declarator_opt (cp_parser* parser)
9134 enum tree_code code;
9136 cp_cv_quals cv_quals;
9138 /* We don't know if there's a ptr-operator next, or not. */
9139 cp_parser_parse_tentatively (parser);
9140 /* Try the ptr-operator. */
9141 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9142 /* If it worked, look for more conversion-declarators. */
9143 if (cp_parser_parse_definitely (parser))
9145 cp_declarator *declarator;
9147 /* Parse another optional declarator. */
9148 declarator = cp_parser_conversion_declarator_opt (parser);
9150 return cp_parser_make_indirect_declarator
9151 (code, class_type, cv_quals, declarator);
9157 /* Parse an (optional) ctor-initializer.
9160 : mem-initializer-list
9162 Returns TRUE iff the ctor-initializer was actually present. */
9165 cp_parser_ctor_initializer_opt (cp_parser* parser)
9167 /* If the next token is not a `:', then there is no
9168 ctor-initializer. */
9169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9171 /* Do default initialization of any bases and members. */
9172 if (DECL_CONSTRUCTOR_P (current_function_decl))
9173 finish_mem_initializers (NULL_TREE);
9178 /* Consume the `:' token. */
9179 cp_lexer_consume_token (parser->lexer);
9180 /* And the mem-initializer-list. */
9181 cp_parser_mem_initializer_list (parser);
9186 /* Parse a mem-initializer-list.
9188 mem-initializer-list:
9189 mem-initializer ... [opt]
9190 mem-initializer ... [opt] , mem-initializer-list */
9193 cp_parser_mem_initializer_list (cp_parser* parser)
9195 tree mem_initializer_list = NULL_TREE;
9196 cp_token *token = cp_lexer_peek_token (parser->lexer);
9198 /* Let the semantic analysis code know that we are starting the
9199 mem-initializer-list. */
9200 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9201 error ("%Honly constructors take base initializers",
9204 /* Loop through the list. */
9207 tree mem_initializer;
9209 token = cp_lexer_peek_token (parser->lexer);
9210 /* Parse the mem-initializer. */
9211 mem_initializer = cp_parser_mem_initializer (parser);
9212 /* If the next token is a `...', we're expanding member initializers. */
9213 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9215 /* Consume the `...'. */
9216 cp_lexer_consume_token (parser->lexer);
9218 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9219 can be expanded but members cannot. */
9220 if (mem_initializer != error_mark_node
9221 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9223 error ("%Hcannot expand initializer for member %<%D%>",
9224 &token->location, TREE_PURPOSE (mem_initializer));
9225 mem_initializer = error_mark_node;
9228 /* Construct the pack expansion type. */
9229 if (mem_initializer != error_mark_node)
9230 mem_initializer = make_pack_expansion (mem_initializer);
9232 /* Add it to the list, unless it was erroneous. */
9233 if (mem_initializer != error_mark_node)
9235 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9236 mem_initializer_list = mem_initializer;
9238 /* If the next token is not a `,', we're done. */
9239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9241 /* Consume the `,' token. */
9242 cp_lexer_consume_token (parser->lexer);
9245 /* Perform semantic analysis. */
9246 if (DECL_CONSTRUCTOR_P (current_function_decl))
9247 finish_mem_initializers (mem_initializer_list);
9250 /* Parse a mem-initializer.
9253 mem-initializer-id ( expression-list [opt] )
9254 mem-initializer-id braced-init-list
9259 ( expression-list [opt] )
9261 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9262 class) or FIELD_DECL (for a non-static data member) to initialize;
9263 the TREE_VALUE is the expression-list. An empty initialization
9264 list is represented by void_list_node. */
9267 cp_parser_mem_initializer (cp_parser* parser)
9269 tree mem_initializer_id;
9270 tree expression_list;
9272 cp_token *token = cp_lexer_peek_token (parser->lexer);
9274 /* Find out what is being initialized. */
9275 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9277 permerror (token->location,
9278 "anachronistic old-style base class initializer");
9279 mem_initializer_id = NULL_TREE;
9283 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9284 if (mem_initializer_id == error_mark_node)
9285 return mem_initializer_id;
9287 member = expand_member_init (mem_initializer_id);
9288 if (member && !DECL_P (member))
9289 in_base_initializer = 1;
9291 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9293 bool expr_non_constant_p;
9294 maybe_warn_cpp0x ("extended initializer lists");
9295 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9296 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9297 expression_list = build_tree_list (NULL_TREE, expression_list);
9302 vec = cp_parser_parenthesized_expression_list (parser, false,
9304 /*allow_expansion_p=*/true,
9305 /*non_constant_p=*/NULL);
9307 return error_mark_node;
9308 expression_list = build_tree_list_vec (vec);
9309 release_tree_vector (vec);
9312 if (expression_list == error_mark_node)
9313 return error_mark_node;
9314 if (!expression_list)
9315 expression_list = void_type_node;
9317 in_base_initializer = 0;
9319 return member ? build_tree_list (member, expression_list) : error_mark_node;
9322 /* Parse a mem-initializer-id.
9325 :: [opt] nested-name-specifier [opt] class-name
9328 Returns a TYPE indicating the class to be initializer for the first
9329 production. Returns an IDENTIFIER_NODE indicating the data member
9330 to be initialized for the second production. */
9333 cp_parser_mem_initializer_id (cp_parser* parser)
9335 bool global_scope_p;
9336 bool nested_name_specifier_p;
9337 bool template_p = false;
9340 cp_token *token = cp_lexer_peek_token (parser->lexer);
9342 /* `typename' is not allowed in this context ([temp.res]). */
9343 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9345 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9346 "member initializer is implicitly a type)",
9348 cp_lexer_consume_token (parser->lexer);
9350 /* Look for the optional `::' operator. */
9352 = (cp_parser_global_scope_opt (parser,
9353 /*current_scope_valid_p=*/false)
9355 /* Look for the optional nested-name-specifier. The simplest way to
9360 The keyword `typename' is not permitted in a base-specifier or
9361 mem-initializer; in these contexts a qualified name that
9362 depends on a template-parameter is implicitly assumed to be a
9365 is to assume that we have seen the `typename' keyword at this
9367 nested_name_specifier_p
9368 = (cp_parser_nested_name_specifier_opt (parser,
9369 /*typename_keyword_p=*/true,
9370 /*check_dependency_p=*/true,
9372 /*is_declaration=*/true)
9374 if (nested_name_specifier_p)
9375 template_p = cp_parser_optional_template_keyword (parser);
9376 /* If there is a `::' operator or a nested-name-specifier, then we
9377 are definitely looking for a class-name. */
9378 if (global_scope_p || nested_name_specifier_p)
9379 return cp_parser_class_name (parser,
9380 /*typename_keyword_p=*/true,
9381 /*template_keyword_p=*/template_p,
9383 /*check_dependency_p=*/true,
9384 /*class_head_p=*/false,
9385 /*is_declaration=*/true);
9386 /* Otherwise, we could also be looking for an ordinary identifier. */
9387 cp_parser_parse_tentatively (parser);
9388 /* Try a class-name. */
9389 id = cp_parser_class_name (parser,
9390 /*typename_keyword_p=*/true,
9391 /*template_keyword_p=*/false,
9393 /*check_dependency_p=*/true,
9394 /*class_head_p=*/false,
9395 /*is_declaration=*/true);
9396 /* If we found one, we're done. */
9397 if (cp_parser_parse_definitely (parser))
9399 /* Otherwise, look for an ordinary identifier. */
9400 return cp_parser_identifier (parser);
9403 /* Overloading [gram.over] */
9405 /* Parse an operator-function-id.
9407 operator-function-id:
9410 Returns an IDENTIFIER_NODE for the operator which is a
9411 human-readable spelling of the identifier, e.g., `operator +'. */
9414 cp_parser_operator_function_id (cp_parser* parser)
9416 /* Look for the `operator' keyword. */
9417 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9418 return error_mark_node;
9419 /* And then the name of the operator itself. */
9420 return cp_parser_operator (parser);
9423 /* Parse an operator.
9426 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9427 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9428 || ++ -- , ->* -> () []
9435 Returns an IDENTIFIER_NODE for the operator which is a
9436 human-readable spelling of the identifier, e.g., `operator +'. */
9439 cp_parser_operator (cp_parser* parser)
9441 tree id = NULL_TREE;
9444 /* Peek at the next token. */
9445 token = cp_lexer_peek_token (parser->lexer);
9446 /* Figure out which operator we have. */
9447 switch (token->type)
9453 /* The keyword should be either `new' or `delete'. */
9454 if (token->keyword == RID_NEW)
9456 else if (token->keyword == RID_DELETE)
9461 /* Consume the `new' or `delete' token. */
9462 cp_lexer_consume_token (parser->lexer);
9464 /* Peek at the next token. */
9465 token = cp_lexer_peek_token (parser->lexer);
9466 /* If it's a `[' token then this is the array variant of the
9468 if (token->type == CPP_OPEN_SQUARE)
9470 /* Consume the `[' token. */
9471 cp_lexer_consume_token (parser->lexer);
9472 /* Look for the `]' token. */
9473 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9474 id = ansi_opname (op == NEW_EXPR
9475 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9477 /* Otherwise, we have the non-array variant. */
9479 id = ansi_opname (op);
9485 id = ansi_opname (PLUS_EXPR);
9489 id = ansi_opname (MINUS_EXPR);
9493 id = ansi_opname (MULT_EXPR);
9497 id = ansi_opname (TRUNC_DIV_EXPR);
9501 id = ansi_opname (TRUNC_MOD_EXPR);
9505 id = ansi_opname (BIT_XOR_EXPR);
9509 id = ansi_opname (BIT_AND_EXPR);
9513 id = ansi_opname (BIT_IOR_EXPR);
9517 id = ansi_opname (BIT_NOT_EXPR);
9521 id = ansi_opname (TRUTH_NOT_EXPR);
9525 id = ansi_assopname (NOP_EXPR);
9529 id = ansi_opname (LT_EXPR);
9533 id = ansi_opname (GT_EXPR);
9537 id = ansi_assopname (PLUS_EXPR);
9541 id = ansi_assopname (MINUS_EXPR);
9545 id = ansi_assopname (MULT_EXPR);
9549 id = ansi_assopname (TRUNC_DIV_EXPR);
9553 id = ansi_assopname (TRUNC_MOD_EXPR);
9557 id = ansi_assopname (BIT_XOR_EXPR);
9561 id = ansi_assopname (BIT_AND_EXPR);
9565 id = ansi_assopname (BIT_IOR_EXPR);
9569 id = ansi_opname (LSHIFT_EXPR);
9573 id = ansi_opname (RSHIFT_EXPR);
9577 id = ansi_assopname (LSHIFT_EXPR);
9581 id = ansi_assopname (RSHIFT_EXPR);
9585 id = ansi_opname (EQ_EXPR);
9589 id = ansi_opname (NE_EXPR);
9593 id = ansi_opname (LE_EXPR);
9596 case CPP_GREATER_EQ:
9597 id = ansi_opname (GE_EXPR);
9601 id = ansi_opname (TRUTH_ANDIF_EXPR);
9605 id = ansi_opname (TRUTH_ORIF_EXPR);
9609 id = ansi_opname (POSTINCREMENT_EXPR);
9612 case CPP_MINUS_MINUS:
9613 id = ansi_opname (PREDECREMENT_EXPR);
9617 id = ansi_opname (COMPOUND_EXPR);
9620 case CPP_DEREF_STAR:
9621 id = ansi_opname (MEMBER_REF);
9625 id = ansi_opname (COMPONENT_REF);
9628 case CPP_OPEN_PAREN:
9629 /* Consume the `('. */
9630 cp_lexer_consume_token (parser->lexer);
9631 /* Look for the matching `)'. */
9632 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9633 return ansi_opname (CALL_EXPR);
9635 case CPP_OPEN_SQUARE:
9636 /* Consume the `['. */
9637 cp_lexer_consume_token (parser->lexer);
9638 /* Look for the matching `]'. */
9639 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9640 return ansi_opname (ARRAY_REF);
9643 /* Anything else is an error. */
9647 /* If we have selected an identifier, we need to consume the
9650 cp_lexer_consume_token (parser->lexer);
9651 /* Otherwise, no valid operator name was present. */
9654 cp_parser_error (parser, "expected operator");
9655 id = error_mark_node;
9661 /* Parse a template-declaration.
9663 template-declaration:
9664 export [opt] template < template-parameter-list > declaration
9666 If MEMBER_P is TRUE, this template-declaration occurs within a
9669 The grammar rule given by the standard isn't correct. What
9672 template-declaration:
9673 export [opt] template-parameter-list-seq
9674 decl-specifier-seq [opt] init-declarator [opt] ;
9675 export [opt] template-parameter-list-seq
9678 template-parameter-list-seq:
9679 template-parameter-list-seq [opt]
9680 template < template-parameter-list > */
9683 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9685 /* Check for `export'. */
9686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9688 /* Consume the `export' token. */
9689 cp_lexer_consume_token (parser->lexer);
9690 /* Warn that we do not support `export'. */
9691 warning (0, "keyword %<export%> not implemented, and will be ignored");
9694 cp_parser_template_declaration_after_export (parser, member_p);
9697 /* Parse a template-parameter-list.
9699 template-parameter-list:
9701 template-parameter-list , template-parameter
9703 Returns a TREE_LIST. Each node represents a template parameter.
9704 The nodes are connected via their TREE_CHAINs. */
9707 cp_parser_template_parameter_list (cp_parser* parser)
9709 tree parameter_list = NULL_TREE;
9711 begin_template_parm_list ();
9716 bool is_parameter_pack;
9717 location_t parm_loc;
9719 /* Parse the template-parameter. */
9720 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
9721 parameter = cp_parser_template_parameter (parser,
9723 &is_parameter_pack);
9724 /* Add it to the list. */
9725 if (parameter != error_mark_node)
9726 parameter_list = process_template_parm (parameter_list,
9733 tree err_parm = build_tree_list (parameter, parameter);
9734 TREE_VALUE (err_parm) = error_mark_node;
9735 parameter_list = chainon (parameter_list, err_parm);
9738 /* If the next token is not a `,', we're done. */
9739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9741 /* Otherwise, consume the `,' token. */
9742 cp_lexer_consume_token (parser->lexer);
9745 return end_template_parm_list (parameter_list);
9748 /* Parse a template-parameter.
9752 parameter-declaration
9754 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9755 the parameter. The TREE_PURPOSE is the default value, if any.
9756 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9757 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9758 set to true iff this parameter is a parameter pack. */
9761 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9762 bool *is_parameter_pack)
9765 cp_parameter_declarator *parameter_declarator;
9766 cp_declarator *id_declarator;
9769 /* Assume it is a type parameter or a template parameter. */
9770 *is_non_type = false;
9771 /* Assume it not a parameter pack. */
9772 *is_parameter_pack = false;
9773 /* Peek at the next token. */
9774 token = cp_lexer_peek_token (parser->lexer);
9775 /* If it is `class' or `template', we have a type-parameter. */
9776 if (token->keyword == RID_TEMPLATE)
9777 return cp_parser_type_parameter (parser, is_parameter_pack);
9778 /* If it is `class' or `typename' we do not know yet whether it is a
9779 type parameter or a non-type parameter. Consider:
9781 template <typename T, typename T::X X> ...
9785 template <class C, class D*> ...
9787 Here, the first parameter is a type parameter, and the second is
9788 a non-type parameter. We can tell by looking at the token after
9789 the identifier -- if it is a `,', `=', or `>' then we have a type
9791 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9793 /* Peek at the token after `class' or `typename'. */
9794 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9795 /* If it's an ellipsis, we have a template type parameter
9797 if (token->type == CPP_ELLIPSIS)
9798 return cp_parser_type_parameter (parser, is_parameter_pack);
9799 /* If it's an identifier, skip it. */
9800 if (token->type == CPP_NAME)
9801 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9802 /* Now, see if the token looks like the end of a template
9804 if (token->type == CPP_COMMA
9805 || token->type == CPP_EQ
9806 || token->type == CPP_GREATER)
9807 return cp_parser_type_parameter (parser, is_parameter_pack);
9810 /* Otherwise, it is a non-type parameter.
9814 When parsing a default template-argument for a non-type
9815 template-parameter, the first non-nested `>' is taken as the end
9816 of the template parameter-list rather than a greater-than
9818 *is_non_type = true;
9819 parameter_declarator
9820 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9821 /*parenthesized_p=*/NULL);
9823 /* If the parameter declaration is marked as a parameter pack, set
9824 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9825 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9827 if (parameter_declarator
9828 && parameter_declarator->declarator
9829 && parameter_declarator->declarator->parameter_pack_p)
9831 *is_parameter_pack = true;
9832 parameter_declarator->declarator->parameter_pack_p = false;
9835 /* If the next token is an ellipsis, and we don't already have it
9836 marked as a parameter pack, then we have a parameter pack (that
9837 has no declarator). */
9838 if (!*is_parameter_pack
9839 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9840 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9842 /* Consume the `...'. */
9843 cp_lexer_consume_token (parser->lexer);
9844 maybe_warn_variadic_templates ();
9846 *is_parameter_pack = true;
9848 /* We might end up with a pack expansion as the type of the non-type
9849 template parameter, in which case this is a non-type template
9851 else if (parameter_declarator
9852 && parameter_declarator->decl_specifiers.type
9853 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9855 *is_parameter_pack = true;
9856 parameter_declarator->decl_specifiers.type =
9857 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9860 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9862 /* Parameter packs cannot have default arguments. However, a
9863 user may try to do so, so we'll parse them and give an
9864 appropriate diagnostic here. */
9866 /* Consume the `='. */
9867 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9868 cp_lexer_consume_token (parser->lexer);
9870 /* Find the name of the parameter pack. */
9871 id_declarator = parameter_declarator->declarator;
9872 while (id_declarator && id_declarator->kind != cdk_id)
9873 id_declarator = id_declarator->declarator;
9875 if (id_declarator && id_declarator->kind == cdk_id)
9876 error ("%Htemplate parameter pack %qD cannot have a default argument",
9877 &start_token->location, id_declarator->u.id.unqualified_name);
9879 error ("%Htemplate parameter pack cannot have a default argument",
9880 &start_token->location);
9882 /* Parse the default argument, but throw away the result. */
9883 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9886 parm = grokdeclarator (parameter_declarator->declarator,
9887 ¶meter_declarator->decl_specifiers,
9888 PARM, /*initialized=*/0,
9890 if (parm == error_mark_node)
9891 return error_mark_node;
9893 return build_tree_list (parameter_declarator->default_argument, parm);
9896 /* Parse a type-parameter.
9899 class identifier [opt]
9900 class identifier [opt] = type-id
9901 typename identifier [opt]
9902 typename identifier [opt] = type-id
9903 template < template-parameter-list > class identifier [opt]
9904 template < template-parameter-list > class identifier [opt]
9907 GNU Extension (variadic templates):
9910 class ... identifier [opt]
9911 typename ... identifier [opt]
9913 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9914 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9915 the declaration of the parameter.
9917 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9920 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9925 /* Look for a keyword to tell us what kind of parameter this is. */
9926 token = cp_parser_require (parser, CPP_KEYWORD,
9927 "%<class%>, %<typename%>, or %<template%>");
9929 return error_mark_node;
9931 switch (token->keyword)
9937 tree default_argument;
9939 /* If the next token is an ellipsis, we have a template
9941 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9943 /* Consume the `...' token. */
9944 cp_lexer_consume_token (parser->lexer);
9945 maybe_warn_variadic_templates ();
9947 *is_parameter_pack = true;
9950 /* If the next token is an identifier, then it names the
9952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9953 identifier = cp_parser_identifier (parser);
9955 identifier = NULL_TREE;
9957 /* Create the parameter. */
9958 parameter = finish_template_type_parm (class_type_node, identifier);
9960 /* If the next token is an `=', we have a default argument. */
9961 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9963 /* Consume the `=' token. */
9964 cp_lexer_consume_token (parser->lexer);
9965 /* Parse the default-argument. */
9966 push_deferring_access_checks (dk_no_deferred);
9967 default_argument = cp_parser_type_id (parser);
9969 /* Template parameter packs cannot have default
9971 if (*is_parameter_pack)
9974 error ("%Htemplate parameter pack %qD cannot have a "
9975 "default argument", &token->location, identifier);
9977 error ("%Htemplate parameter packs cannot have "
9978 "default arguments", &token->location);
9979 default_argument = NULL_TREE;
9981 pop_deferring_access_checks ();
9984 default_argument = NULL_TREE;
9986 /* Create the combined representation of the parameter and the
9987 default argument. */
9988 parameter = build_tree_list (default_argument, parameter);
9994 tree parameter_list;
9996 tree default_argument;
9998 /* Look for the `<'. */
9999 cp_parser_require (parser, CPP_LESS, "%<<%>");
10000 /* Parse the template-parameter-list. */
10001 parameter_list = cp_parser_template_parameter_list (parser);
10002 /* Look for the `>'. */
10003 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10004 /* Look for the `class' keyword. */
10005 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10006 /* If the next token is an ellipsis, we have a template
10008 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10010 /* Consume the `...' token. */
10011 cp_lexer_consume_token (parser->lexer);
10012 maybe_warn_variadic_templates ();
10014 *is_parameter_pack = true;
10016 /* If the next token is an `=', then there is a
10017 default-argument. If the next token is a `>', we are at
10018 the end of the parameter-list. If the next token is a `,',
10019 then we are at the end of this parameter. */
10020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10021 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10022 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10024 identifier = cp_parser_identifier (parser);
10025 /* Treat invalid names as if the parameter were nameless. */
10026 if (identifier == error_mark_node)
10027 identifier = NULL_TREE;
10030 identifier = NULL_TREE;
10032 /* Create the template parameter. */
10033 parameter = finish_template_template_parm (class_type_node,
10036 /* If the next token is an `=', then there is a
10037 default-argument. */
10038 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10042 /* Consume the `='. */
10043 cp_lexer_consume_token (parser->lexer);
10044 /* Parse the id-expression. */
10045 push_deferring_access_checks (dk_no_deferred);
10046 /* save token before parsing the id-expression, for error
10048 token = cp_lexer_peek_token (parser->lexer);
10050 = cp_parser_id_expression (parser,
10051 /*template_keyword_p=*/false,
10052 /*check_dependency_p=*/true,
10053 /*template_p=*/&is_template,
10054 /*declarator_p=*/false,
10055 /*optional_p=*/false);
10056 if (TREE_CODE (default_argument) == TYPE_DECL)
10057 /* If the id-expression was a template-id that refers to
10058 a template-class, we already have the declaration here,
10059 so no further lookup is needed. */
10062 /* Look up the name. */
10064 = cp_parser_lookup_name (parser, default_argument,
10066 /*is_template=*/is_template,
10067 /*is_namespace=*/false,
10068 /*check_dependency=*/true,
10069 /*ambiguous_decls=*/NULL,
10071 /* See if the default argument is valid. */
10073 = check_template_template_default_arg (default_argument);
10075 /* Template parameter packs cannot have default
10077 if (*is_parameter_pack)
10080 error ("%Htemplate parameter pack %qD cannot "
10081 "have a default argument",
10082 &token->location, identifier);
10084 error ("%Htemplate parameter packs cannot "
10085 "have default arguments",
10087 default_argument = NULL_TREE;
10089 pop_deferring_access_checks ();
10092 default_argument = NULL_TREE;
10094 /* Create the combined representation of the parameter and the
10095 default argument. */
10096 parameter = build_tree_list (default_argument, parameter);
10101 gcc_unreachable ();
10108 /* Parse a template-id.
10111 template-name < template-argument-list [opt] >
10113 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10114 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10115 returned. Otherwise, if the template-name names a function, or set
10116 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10117 names a class, returns a TYPE_DECL for the specialization.
10119 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10120 uninstantiated templates. */
10123 cp_parser_template_id (cp_parser *parser,
10124 bool template_keyword_p,
10125 bool check_dependency_p,
10126 bool is_declaration)
10132 cp_token_position start_of_id = 0;
10133 deferred_access_check *chk;
10134 VEC (deferred_access_check,gc) *access_check;
10135 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10136 bool is_identifier;
10138 /* If the next token corresponds to a template-id, there is no need
10140 next_token = cp_lexer_peek_token (parser->lexer);
10141 if (next_token->type == CPP_TEMPLATE_ID)
10143 struct tree_check *check_value;
10145 /* Get the stored value. */
10146 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10147 /* Perform any access checks that were deferred. */
10148 access_check = check_value->checks;
10152 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10155 perform_or_defer_access_check (chk->binfo,
10160 /* Return the stored value. */
10161 return check_value->value;
10164 /* Avoid performing name lookup if there is no possibility of
10165 finding a template-id. */
10166 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10167 || (next_token->type == CPP_NAME
10168 && !cp_parser_nth_token_starts_template_argument_list_p
10171 cp_parser_error (parser, "expected template-id");
10172 return error_mark_node;
10175 /* Remember where the template-id starts. */
10176 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10177 start_of_id = cp_lexer_token_position (parser->lexer, false);
10179 push_deferring_access_checks (dk_deferred);
10181 /* Parse the template-name. */
10182 is_identifier = false;
10183 token = cp_lexer_peek_token (parser->lexer);
10184 templ = cp_parser_template_name (parser, template_keyword_p,
10185 check_dependency_p,
10188 if (templ == error_mark_node || is_identifier)
10190 pop_deferring_access_checks ();
10194 /* If we find the sequence `[:' after a template-name, it's probably
10195 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10196 parse correctly the argument list. */
10197 next_token = cp_lexer_peek_token (parser->lexer);
10198 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10199 if (next_token->type == CPP_OPEN_SQUARE
10200 && next_token->flags & DIGRAPH
10201 && next_token_2->type == CPP_COLON
10202 && !(next_token_2->flags & PREV_WHITE))
10204 cp_parser_parse_tentatively (parser);
10205 /* Change `:' into `::'. */
10206 next_token_2->type = CPP_SCOPE;
10207 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10209 cp_lexer_consume_token (parser->lexer);
10211 /* Parse the arguments. */
10212 arguments = cp_parser_enclosed_template_argument_list (parser);
10213 if (!cp_parser_parse_definitely (parser))
10215 /* If we couldn't parse an argument list, then we revert our changes
10216 and return simply an error. Maybe this is not a template-id
10218 next_token_2->type = CPP_COLON;
10219 cp_parser_error (parser, "expected %<<%>");
10220 pop_deferring_access_checks ();
10221 return error_mark_node;
10223 /* Otherwise, emit an error about the invalid digraph, but continue
10224 parsing because we got our argument list. */
10225 if (permerror (next_token->location,
10226 "%<<::%> cannot begin a template-argument list"))
10228 static bool hint = false;
10229 inform (next_token->location,
10230 "%<<:%> is an alternate spelling for %<[%>."
10231 " Insert whitespace between %<<%> and %<::%>");
10232 if (!hint && !flag_permissive)
10234 inform (next_token->location, "(if you use %<-fpermissive%>"
10235 " G++ will accept your code)");
10242 /* Look for the `<' that starts the template-argument-list. */
10243 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10245 pop_deferring_access_checks ();
10246 return error_mark_node;
10248 /* Parse the arguments. */
10249 arguments = cp_parser_enclosed_template_argument_list (parser);
10252 /* Build a representation of the specialization. */
10253 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10254 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10255 else if (DECL_CLASS_TEMPLATE_P (templ)
10256 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10258 bool entering_scope;
10259 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10260 template (rather than some instantiation thereof) only if
10261 is not nested within some other construct. For example, in
10262 "template <typename T> void f(T) { A<T>::", A<T> is just an
10263 instantiation of A. */
10264 entering_scope = (template_parm_scope_p ()
10265 && cp_lexer_next_token_is (parser->lexer,
10268 = finish_template_type (templ, arguments, entering_scope);
10272 /* If it's not a class-template or a template-template, it should be
10273 a function-template. */
10274 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10275 || TREE_CODE (templ) == OVERLOAD
10276 || BASELINK_P (templ)));
10278 template_id = lookup_template_function (templ, arguments);
10281 /* If parsing tentatively, replace the sequence of tokens that makes
10282 up the template-id with a CPP_TEMPLATE_ID token. That way,
10283 should we re-parse the token stream, we will not have to repeat
10284 the effort required to do the parse, nor will we issue duplicate
10285 error messages about problems during instantiation of the
10289 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10291 /* Reset the contents of the START_OF_ID token. */
10292 token->type = CPP_TEMPLATE_ID;
10293 /* Retrieve any deferred checks. Do not pop this access checks yet
10294 so the memory will not be reclaimed during token replacing below. */
10295 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10296 token->u.tree_check_value->value = template_id;
10297 token->u.tree_check_value->checks = get_deferred_access_checks ();
10298 token->keyword = RID_MAX;
10300 /* Purge all subsequent tokens. */
10301 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10303 /* ??? Can we actually assume that, if template_id ==
10304 error_mark_node, we will have issued a diagnostic to the
10305 user, as opposed to simply marking the tentative parse as
10307 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10308 error ("%Hparse error in template argument list",
10312 pop_deferring_access_checks ();
10313 return template_id;
10316 /* Parse a template-name.
10321 The standard should actually say:
10325 operator-function-id
10327 A defect report has been filed about this issue.
10329 A conversion-function-id cannot be a template name because they cannot
10330 be part of a template-id. In fact, looking at this code:
10332 a.operator K<int>()
10334 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10335 It is impossible to call a templated conversion-function-id with an
10336 explicit argument list, since the only allowed template parameter is
10337 the type to which it is converting.
10339 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10340 `template' keyword, in a construction like:
10344 In that case `f' is taken to be a template-name, even though there
10345 is no way of knowing for sure.
10347 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10348 name refers to a set of overloaded functions, at least one of which
10349 is a template, or an IDENTIFIER_NODE with the name of the template,
10350 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10351 names are looked up inside uninstantiated templates. */
10354 cp_parser_template_name (cp_parser* parser,
10355 bool template_keyword_p,
10356 bool check_dependency_p,
10357 bool is_declaration,
10358 bool *is_identifier)
10363 cp_token *token = cp_lexer_peek_token (parser->lexer);
10365 /* If the next token is `operator', then we have either an
10366 operator-function-id or a conversion-function-id. */
10367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10369 /* We don't know whether we're looking at an
10370 operator-function-id or a conversion-function-id. */
10371 cp_parser_parse_tentatively (parser);
10372 /* Try an operator-function-id. */
10373 identifier = cp_parser_operator_function_id (parser);
10374 /* If that didn't work, try a conversion-function-id. */
10375 if (!cp_parser_parse_definitely (parser))
10377 cp_parser_error (parser, "expected template-name");
10378 return error_mark_node;
10381 /* Look for the identifier. */
10383 identifier = cp_parser_identifier (parser);
10385 /* If we didn't find an identifier, we don't have a template-id. */
10386 if (identifier == error_mark_node)
10387 return error_mark_node;
10389 /* If the name immediately followed the `template' keyword, then it
10390 is a template-name. However, if the next token is not `<', then
10391 we do not treat it as a template-name, since it is not being used
10392 as part of a template-id. This enables us to handle constructs
10395 template <typename T> struct S { S(); };
10396 template <typename T> S<T>::S();
10398 correctly. We would treat `S' as a template -- if it were `S<T>'
10399 -- but we do not if there is no `<'. */
10401 if (processing_template_decl
10402 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10404 /* In a declaration, in a dependent context, we pretend that the
10405 "template" keyword was present in order to improve error
10406 recovery. For example, given:
10408 template <typename T> void f(T::X<int>);
10410 we want to treat "X<int>" as a template-id. */
10412 && !template_keyword_p
10413 && parser->scope && TYPE_P (parser->scope)
10414 && check_dependency_p
10415 && dependent_scope_p (parser->scope)
10416 /* Do not do this for dtors (or ctors), since they never
10417 need the template keyword before their name. */
10418 && !constructor_name_p (identifier, parser->scope))
10420 cp_token_position start = 0;
10422 /* Explain what went wrong. */
10423 error ("%Hnon-template %qD used as template",
10424 &token->location, identifier);
10425 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10426 parser->scope, identifier);
10427 /* If parsing tentatively, find the location of the "<" token. */
10428 if (cp_parser_simulate_error (parser))
10429 start = cp_lexer_token_position (parser->lexer, true);
10430 /* Parse the template arguments so that we can issue error
10431 messages about them. */
10432 cp_lexer_consume_token (parser->lexer);
10433 cp_parser_enclosed_template_argument_list (parser);
10434 /* Skip tokens until we find a good place from which to
10435 continue parsing. */
10436 cp_parser_skip_to_closing_parenthesis (parser,
10437 /*recovering=*/true,
10439 /*consume_paren=*/false);
10440 /* If parsing tentatively, permanently remove the
10441 template argument list. That will prevent duplicate
10442 error messages from being issued about the missing
10443 "template" keyword. */
10445 cp_lexer_purge_tokens_after (parser->lexer, start);
10447 *is_identifier = true;
10451 /* If the "template" keyword is present, then there is generally
10452 no point in doing name-lookup, so we just return IDENTIFIER.
10453 But, if the qualifying scope is non-dependent then we can
10454 (and must) do name-lookup normally. */
10455 if (template_keyword_p
10457 || (TYPE_P (parser->scope)
10458 && dependent_type_p (parser->scope))))
10462 /* Look up the name. */
10463 decl = cp_parser_lookup_name (parser, identifier,
10465 /*is_template=*/false,
10466 /*is_namespace=*/false,
10467 check_dependency_p,
10468 /*ambiguous_decls=*/NULL,
10470 decl = maybe_get_template_decl_from_type_decl (decl);
10472 /* If DECL is a template, then the name was a template-name. */
10473 if (TREE_CODE (decl) == TEMPLATE_DECL)
10477 tree fn = NULL_TREE;
10479 /* The standard does not explicitly indicate whether a name that
10480 names a set of overloaded declarations, some of which are
10481 templates, is a template-name. However, such a name should
10482 be a template-name; otherwise, there is no way to form a
10483 template-id for the overloaded templates. */
10484 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10485 if (TREE_CODE (fns) == OVERLOAD)
10486 for (fn = fns; fn; fn = OVL_NEXT (fn))
10487 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10492 /* The name does not name a template. */
10493 cp_parser_error (parser, "expected template-name");
10494 return error_mark_node;
10498 /* If DECL is dependent, and refers to a function, then just return
10499 its name; we will look it up again during template instantiation. */
10500 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10502 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10503 if (TYPE_P (scope) && dependent_type_p (scope))
10510 /* Parse a template-argument-list.
10512 template-argument-list:
10513 template-argument ... [opt]
10514 template-argument-list , template-argument ... [opt]
10516 Returns a TREE_VEC containing the arguments. */
10519 cp_parser_template_argument_list (cp_parser* parser)
10521 tree fixed_args[10];
10522 unsigned n_args = 0;
10523 unsigned alloced = 10;
10524 tree *arg_ary = fixed_args;
10526 bool saved_in_template_argument_list_p;
10528 bool saved_non_ice_p;
10530 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10531 parser->in_template_argument_list_p = true;
10532 /* Even if the template-id appears in an integral
10533 constant-expression, the contents of the argument list do
10535 saved_ice_p = parser->integral_constant_expression_p;
10536 parser->integral_constant_expression_p = false;
10537 saved_non_ice_p = parser->non_integral_constant_expression_p;
10538 parser->non_integral_constant_expression_p = false;
10539 /* Parse the arguments. */
10545 /* Consume the comma. */
10546 cp_lexer_consume_token (parser->lexer);
10548 /* Parse the template-argument. */
10549 argument = cp_parser_template_argument (parser);
10551 /* If the next token is an ellipsis, we're expanding a template
10553 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10555 if (argument == error_mark_node)
10557 cp_token *token = cp_lexer_peek_token (parser->lexer);
10558 error ("%Hexpected parameter pack before %<...%>",
10561 /* Consume the `...' token. */
10562 cp_lexer_consume_token (parser->lexer);
10564 /* Make the argument into a TYPE_PACK_EXPANSION or
10565 EXPR_PACK_EXPANSION. */
10566 argument = make_pack_expansion (argument);
10569 if (n_args == alloced)
10573 if (arg_ary == fixed_args)
10575 arg_ary = XNEWVEC (tree, alloced);
10576 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10579 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10581 arg_ary[n_args++] = argument;
10583 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10585 vec = make_tree_vec (n_args);
10588 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10590 if (arg_ary != fixed_args)
10592 parser->non_integral_constant_expression_p = saved_non_ice_p;
10593 parser->integral_constant_expression_p = saved_ice_p;
10594 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10598 /* Parse a template-argument.
10601 assignment-expression
10605 The representation is that of an assignment-expression, type-id, or
10606 id-expression -- except that the qualified id-expression is
10607 evaluated, so that the value returned is either a DECL or an
10610 Although the standard says "assignment-expression", it forbids
10611 throw-expressions or assignments in the template argument.
10612 Therefore, we use "conditional-expression" instead. */
10615 cp_parser_template_argument (cp_parser* parser)
10620 bool maybe_type_id = false;
10621 cp_token *token = NULL, *argument_start_token = NULL;
10624 /* There's really no way to know what we're looking at, so we just
10625 try each alternative in order.
10629 In a template-argument, an ambiguity between a type-id and an
10630 expression is resolved to a type-id, regardless of the form of
10631 the corresponding template-parameter.
10633 Therefore, we try a type-id first. */
10634 cp_parser_parse_tentatively (parser);
10635 argument = cp_parser_template_type_arg (parser);
10636 /* If there was no error parsing the type-id but the next token is a
10637 '>>', our behavior depends on which dialect of C++ we're
10638 parsing. In C++98, we probably found a typo for '> >'. But there
10639 are type-id which are also valid expressions. For instance:
10641 struct X { int operator >> (int); };
10642 template <int V> struct Foo {};
10645 Here 'X()' is a valid type-id of a function type, but the user just
10646 wanted to write the expression "X() >> 5". Thus, we remember that we
10647 found a valid type-id, but we still try to parse the argument as an
10648 expression to see what happens.
10650 In C++0x, the '>>' will be considered two separate '>'
10652 if (!cp_parser_error_occurred (parser)
10653 && cxx_dialect == cxx98
10654 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10656 maybe_type_id = true;
10657 cp_parser_abort_tentative_parse (parser);
10661 /* If the next token isn't a `,' or a `>', then this argument wasn't
10662 really finished. This means that the argument is not a valid
10664 if (!cp_parser_next_token_ends_template_argument_p (parser))
10665 cp_parser_error (parser, "expected template-argument");
10666 /* If that worked, we're done. */
10667 if (cp_parser_parse_definitely (parser))
10670 /* We're still not sure what the argument will be. */
10671 cp_parser_parse_tentatively (parser);
10672 /* Try a template. */
10673 argument_start_token = cp_lexer_peek_token (parser->lexer);
10674 argument = cp_parser_id_expression (parser,
10675 /*template_keyword_p=*/false,
10676 /*check_dependency_p=*/true,
10678 /*declarator_p=*/false,
10679 /*optional_p=*/false);
10680 /* If the next token isn't a `,' or a `>', then this argument wasn't
10681 really finished. */
10682 if (!cp_parser_next_token_ends_template_argument_p (parser))
10683 cp_parser_error (parser, "expected template-argument");
10684 if (!cp_parser_error_occurred (parser))
10686 /* Figure out what is being referred to. If the id-expression
10687 was for a class template specialization, then we will have a
10688 TYPE_DECL at this point. There is no need to do name lookup
10689 at this point in that case. */
10690 if (TREE_CODE (argument) != TYPE_DECL)
10691 argument = cp_parser_lookup_name (parser, argument,
10693 /*is_template=*/template_p,
10694 /*is_namespace=*/false,
10695 /*check_dependency=*/true,
10696 /*ambiguous_decls=*/NULL,
10697 argument_start_token->location);
10698 if (TREE_CODE (argument) != TEMPLATE_DECL
10699 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10700 cp_parser_error (parser, "expected template-name");
10702 if (cp_parser_parse_definitely (parser))
10704 /* It must be a non-type argument. There permitted cases are given
10705 in [temp.arg.nontype]:
10707 -- an integral constant-expression of integral or enumeration
10710 -- the name of a non-type template-parameter; or
10712 -- the name of an object or function with external linkage...
10714 -- the address of an object or function with external linkage...
10716 -- a pointer to member... */
10717 /* Look for a non-type template parameter. */
10718 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10720 cp_parser_parse_tentatively (parser);
10721 argument = cp_parser_primary_expression (parser,
10722 /*address_p=*/false,
10724 /*template_arg_p=*/true,
10726 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10727 || !cp_parser_next_token_ends_template_argument_p (parser))
10728 cp_parser_simulate_error (parser);
10729 if (cp_parser_parse_definitely (parser))
10733 /* If the next token is "&", the argument must be the address of an
10734 object or function with external linkage. */
10735 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10737 cp_lexer_consume_token (parser->lexer);
10738 /* See if we might have an id-expression. */
10739 token = cp_lexer_peek_token (parser->lexer);
10740 if (token->type == CPP_NAME
10741 || token->keyword == RID_OPERATOR
10742 || token->type == CPP_SCOPE
10743 || token->type == CPP_TEMPLATE_ID
10744 || token->type == CPP_NESTED_NAME_SPECIFIER)
10746 cp_parser_parse_tentatively (parser);
10747 argument = cp_parser_primary_expression (parser,
10750 /*template_arg_p=*/true,
10752 if (cp_parser_error_occurred (parser)
10753 || !cp_parser_next_token_ends_template_argument_p (parser))
10754 cp_parser_abort_tentative_parse (parser);
10757 if (TREE_CODE (argument) == INDIRECT_REF)
10759 gcc_assert (REFERENCE_REF_P (argument));
10760 argument = TREE_OPERAND (argument, 0);
10763 if (TREE_CODE (argument) == VAR_DECL)
10765 /* A variable without external linkage might still be a
10766 valid constant-expression, so no error is issued here
10767 if the external-linkage check fails. */
10768 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10769 cp_parser_simulate_error (parser);
10771 else if (is_overloaded_fn (argument))
10772 /* All overloaded functions are allowed; if the external
10773 linkage test does not pass, an error will be issued
10777 && (TREE_CODE (argument) == OFFSET_REF
10778 || TREE_CODE (argument) == SCOPE_REF))
10779 /* A pointer-to-member. */
10781 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10784 cp_parser_simulate_error (parser);
10786 if (cp_parser_parse_definitely (parser))
10789 argument = build_x_unary_op (ADDR_EXPR, argument,
10790 tf_warning_or_error);
10795 /* If the argument started with "&", there are no other valid
10796 alternatives at this point. */
10799 cp_parser_error (parser, "invalid non-type template argument");
10800 return error_mark_node;
10803 /* If the argument wasn't successfully parsed as a type-id followed
10804 by '>>', the argument can only be a constant expression now.
10805 Otherwise, we try parsing the constant-expression tentatively,
10806 because the argument could really be a type-id. */
10808 cp_parser_parse_tentatively (parser);
10809 argument = cp_parser_constant_expression (parser,
10810 /*allow_non_constant_p=*/false,
10811 /*non_constant_p=*/NULL);
10812 argument = fold_non_dependent_expr (argument);
10813 if (!maybe_type_id)
10815 if (!cp_parser_next_token_ends_template_argument_p (parser))
10816 cp_parser_error (parser, "expected template-argument");
10817 if (cp_parser_parse_definitely (parser))
10819 /* We did our best to parse the argument as a non type-id, but that
10820 was the only alternative that matched (albeit with a '>' after
10821 it). We can assume it's just a typo from the user, and a
10822 diagnostic will then be issued. */
10823 return cp_parser_template_type_arg (parser);
10826 /* Parse an explicit-instantiation.
10828 explicit-instantiation:
10829 template declaration
10831 Although the standard says `declaration', what it really means is:
10833 explicit-instantiation:
10834 template decl-specifier-seq [opt] declarator [opt] ;
10836 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10837 supposed to be allowed. A defect report has been filed about this
10842 explicit-instantiation:
10843 storage-class-specifier template
10844 decl-specifier-seq [opt] declarator [opt] ;
10845 function-specifier template
10846 decl-specifier-seq [opt] declarator [opt] ; */
10849 cp_parser_explicit_instantiation (cp_parser* parser)
10851 int declares_class_or_enum;
10852 cp_decl_specifier_seq decl_specifiers;
10853 tree extension_specifier = NULL_TREE;
10856 /* Look for an (optional) storage-class-specifier or
10857 function-specifier. */
10858 if (cp_parser_allow_gnu_extensions_p (parser))
10860 extension_specifier
10861 = cp_parser_storage_class_specifier_opt (parser);
10862 if (!extension_specifier)
10863 extension_specifier
10864 = cp_parser_function_specifier_opt (parser,
10865 /*decl_specs=*/NULL);
10868 /* Look for the `template' keyword. */
10869 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10870 /* Let the front end know that we are processing an explicit
10872 begin_explicit_instantiation ();
10873 /* [temp.explicit] says that we are supposed to ignore access
10874 control while processing explicit instantiation directives. */
10875 push_deferring_access_checks (dk_no_check);
10876 /* Parse a decl-specifier-seq. */
10877 token = cp_lexer_peek_token (parser->lexer);
10878 cp_parser_decl_specifier_seq (parser,
10879 CP_PARSER_FLAGS_OPTIONAL,
10881 &declares_class_or_enum);
10882 /* If there was exactly one decl-specifier, and it declared a class,
10883 and there's no declarator, then we have an explicit type
10885 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10889 type = check_tag_decl (&decl_specifiers);
10890 /* Turn access control back on for names used during
10891 template instantiation. */
10892 pop_deferring_access_checks ();
10894 do_type_instantiation (type, extension_specifier,
10895 /*complain=*/tf_error);
10899 cp_declarator *declarator;
10902 /* Parse the declarator. */
10904 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10905 /*ctor_dtor_or_conv_p=*/NULL,
10906 /*parenthesized_p=*/NULL,
10907 /*member_p=*/false);
10908 if (declares_class_or_enum & 2)
10909 cp_parser_check_for_definition_in_return_type (declarator,
10910 decl_specifiers.type,
10911 decl_specifiers.type_location);
10912 if (declarator != cp_error_declarator)
10914 decl = grokdeclarator (declarator, &decl_specifiers,
10915 NORMAL, 0, &decl_specifiers.attributes);
10916 /* Turn access control back on for names used during
10917 template instantiation. */
10918 pop_deferring_access_checks ();
10919 /* Do the explicit instantiation. */
10920 do_decl_instantiation (decl, extension_specifier);
10924 pop_deferring_access_checks ();
10925 /* Skip the body of the explicit instantiation. */
10926 cp_parser_skip_to_end_of_statement (parser);
10929 /* We're done with the instantiation. */
10930 end_explicit_instantiation ();
10932 cp_parser_consume_semicolon_at_end_of_statement (parser);
10935 /* Parse an explicit-specialization.
10937 explicit-specialization:
10938 template < > declaration
10940 Although the standard says `declaration', what it really means is:
10942 explicit-specialization:
10943 template <> decl-specifier [opt] init-declarator [opt] ;
10944 template <> function-definition
10945 template <> explicit-specialization
10946 template <> template-declaration */
10949 cp_parser_explicit_specialization (cp_parser* parser)
10951 bool need_lang_pop;
10952 cp_token *token = cp_lexer_peek_token (parser->lexer);
10954 /* Look for the `template' keyword. */
10955 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10956 /* Look for the `<'. */
10957 cp_parser_require (parser, CPP_LESS, "%<<%>");
10958 /* Look for the `>'. */
10959 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10960 /* We have processed another parameter list. */
10961 ++parser->num_template_parameter_lists;
10964 A template ... explicit specialization ... shall not have C
10966 if (current_lang_name == lang_name_c)
10968 error ("%Htemplate specialization with C linkage", &token->location);
10969 /* Give it C++ linkage to avoid confusing other parts of the
10971 push_lang_context (lang_name_cplusplus);
10972 need_lang_pop = true;
10975 need_lang_pop = false;
10976 /* Let the front end know that we are beginning a specialization. */
10977 if (!begin_specialization ())
10979 end_specialization ();
10983 /* If the next keyword is `template', we need to figure out whether
10984 or not we're looking a template-declaration. */
10985 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10987 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10988 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10989 cp_parser_template_declaration_after_export (parser,
10990 /*member_p=*/false);
10992 cp_parser_explicit_specialization (parser);
10995 /* Parse the dependent declaration. */
10996 cp_parser_single_declaration (parser,
10998 /*member_p=*/false,
10999 /*explicit_specialization_p=*/true,
11000 /*friend_p=*/NULL);
11001 /* We're done with the specialization. */
11002 end_specialization ();
11003 /* For the erroneous case of a template with C linkage, we pushed an
11004 implicit C++ linkage scope; exit that scope now. */
11006 pop_lang_context ();
11007 /* We're done with this parameter list. */
11008 --parser->num_template_parameter_lists;
11011 /* Parse a type-specifier.
11014 simple-type-specifier
11017 elaborated-type-specifier
11025 Returns a representation of the type-specifier. For a
11026 class-specifier, enum-specifier, or elaborated-type-specifier, a
11027 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11029 The parser flags FLAGS is used to control type-specifier parsing.
11031 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11032 in a decl-specifier-seq.
11034 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11035 class-specifier, enum-specifier, or elaborated-type-specifier, then
11036 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11037 if a type is declared; 2 if it is defined. Otherwise, it is set to
11040 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11041 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11042 is set to FALSE. */
11045 cp_parser_type_specifier (cp_parser* parser,
11046 cp_parser_flags flags,
11047 cp_decl_specifier_seq *decl_specs,
11048 bool is_declaration,
11049 int* declares_class_or_enum,
11050 bool* is_cv_qualifier)
11052 tree type_spec = NULL_TREE;
11055 cp_decl_spec ds = ds_last;
11057 /* Assume this type-specifier does not declare a new type. */
11058 if (declares_class_or_enum)
11059 *declares_class_or_enum = 0;
11060 /* And that it does not specify a cv-qualifier. */
11061 if (is_cv_qualifier)
11062 *is_cv_qualifier = false;
11063 /* Peek at the next token. */
11064 token = cp_lexer_peek_token (parser->lexer);
11066 /* If we're looking at a keyword, we can use that to guide the
11067 production we choose. */
11068 keyword = token->keyword;
11072 /* Look for the enum-specifier. */
11073 type_spec = cp_parser_enum_specifier (parser);
11074 /* If that worked, we're done. */
11077 if (declares_class_or_enum)
11078 *declares_class_or_enum = 2;
11080 cp_parser_set_decl_spec_type (decl_specs,
11083 /*user_defined_p=*/true);
11087 goto elaborated_type_specifier;
11089 /* Any of these indicate either a class-specifier, or an
11090 elaborated-type-specifier. */
11094 /* Parse tentatively so that we can back up if we don't find a
11095 class-specifier. */
11096 cp_parser_parse_tentatively (parser);
11097 /* Look for the class-specifier. */
11098 type_spec = cp_parser_class_specifier (parser);
11099 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11100 /* If that worked, we're done. */
11101 if (cp_parser_parse_definitely (parser))
11103 if (declares_class_or_enum)
11104 *declares_class_or_enum = 2;
11106 cp_parser_set_decl_spec_type (decl_specs,
11109 /*user_defined_p=*/true);
11113 /* Fall through. */
11114 elaborated_type_specifier:
11115 /* We're declaring (not defining) a class or enum. */
11116 if (declares_class_or_enum)
11117 *declares_class_or_enum = 1;
11119 /* Fall through. */
11121 /* Look for an elaborated-type-specifier. */
11123 = (cp_parser_elaborated_type_specifier
11125 decl_specs && decl_specs->specs[(int) ds_friend],
11128 cp_parser_set_decl_spec_type (decl_specs,
11131 /*user_defined_p=*/true);
11136 if (is_cv_qualifier)
11137 *is_cv_qualifier = true;
11142 if (is_cv_qualifier)
11143 *is_cv_qualifier = true;
11148 if (is_cv_qualifier)
11149 *is_cv_qualifier = true;
11153 /* The `__complex__' keyword is a GNU extension. */
11161 /* Handle simple keywords. */
11166 ++decl_specs->specs[(int)ds];
11167 decl_specs->any_specifiers_p = true;
11169 return cp_lexer_consume_token (parser->lexer)->u.value;
11172 /* If we do not already have a type-specifier, assume we are looking
11173 at a simple-type-specifier. */
11174 type_spec = cp_parser_simple_type_specifier (parser,
11178 /* If we didn't find a type-specifier, and a type-specifier was not
11179 optional in this context, issue an error message. */
11180 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11182 cp_parser_error (parser, "expected type specifier");
11183 return error_mark_node;
11189 /* Parse a simple-type-specifier.
11191 simple-type-specifier:
11192 :: [opt] nested-name-specifier [opt] type-name
11193 :: [opt] nested-name-specifier template template-id
11208 simple-type-specifier:
11210 decltype ( expression )
11216 simple-type-specifier:
11217 __typeof__ unary-expression
11218 __typeof__ ( type-id )
11220 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11221 appropriately updated. */
11224 cp_parser_simple_type_specifier (cp_parser* parser,
11225 cp_decl_specifier_seq *decl_specs,
11226 cp_parser_flags flags)
11228 tree type = NULL_TREE;
11231 /* Peek at the next token. */
11232 token = cp_lexer_peek_token (parser->lexer);
11234 /* If we're looking at a keyword, things are easy. */
11235 switch (token->keyword)
11239 decl_specs->explicit_char_p = true;
11240 type = char_type_node;
11243 type = char16_type_node;
11246 type = char32_type_node;
11249 type = wchar_type_node;
11252 type = boolean_type_node;
11256 ++decl_specs->specs[(int) ds_short];
11257 type = short_integer_type_node;
11261 decl_specs->explicit_int_p = true;
11262 type = integer_type_node;
11266 ++decl_specs->specs[(int) ds_long];
11267 type = long_integer_type_node;
11271 ++decl_specs->specs[(int) ds_signed];
11272 type = integer_type_node;
11276 ++decl_specs->specs[(int) ds_unsigned];
11277 type = unsigned_type_node;
11280 type = float_type_node;
11283 type = double_type_node;
11286 type = void_type_node;
11290 maybe_warn_cpp0x ("C++0x auto");
11291 type = make_auto ();
11295 /* Parse the `decltype' type. */
11296 type = cp_parser_decltype (parser);
11299 cp_parser_set_decl_spec_type (decl_specs, type,
11301 /*user_defined_p=*/true);
11306 /* Consume the `typeof' token. */
11307 cp_lexer_consume_token (parser->lexer);
11308 /* Parse the operand to `typeof'. */
11309 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11310 /* If it is not already a TYPE, take its type. */
11311 if (!TYPE_P (type))
11312 type = finish_typeof (type);
11315 cp_parser_set_decl_spec_type (decl_specs, type,
11317 /*user_defined_p=*/true);
11325 /* If the type-specifier was for a built-in type, we're done. */
11330 /* Record the type. */
11332 && (token->keyword != RID_SIGNED
11333 && token->keyword != RID_UNSIGNED
11334 && token->keyword != RID_SHORT
11335 && token->keyword != RID_LONG))
11336 cp_parser_set_decl_spec_type (decl_specs,
11339 /*user_defined=*/false);
11341 decl_specs->any_specifiers_p = true;
11343 /* Consume the token. */
11344 id = cp_lexer_consume_token (parser->lexer)->u.value;
11346 /* There is no valid C++ program where a non-template type is
11347 followed by a "<". That usually indicates that the user thought
11348 that the type was a template. */
11349 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11351 return TYPE_NAME (type);
11354 /* The type-specifier must be a user-defined type. */
11355 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11360 /* Don't gobble tokens or issue error messages if this is an
11361 optional type-specifier. */
11362 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11363 cp_parser_parse_tentatively (parser);
11365 /* Look for the optional `::' operator. */
11367 = (cp_parser_global_scope_opt (parser,
11368 /*current_scope_valid_p=*/false)
11370 /* Look for the nested-name specifier. */
11372 = (cp_parser_nested_name_specifier_opt (parser,
11373 /*typename_keyword_p=*/false,
11374 /*check_dependency_p=*/true,
11376 /*is_declaration=*/false)
11378 token = cp_lexer_peek_token (parser->lexer);
11379 /* If we have seen a nested-name-specifier, and the next token
11380 is `template', then we are using the template-id production. */
11382 && cp_parser_optional_template_keyword (parser))
11384 /* Look for the template-id. */
11385 type = cp_parser_template_id (parser,
11386 /*template_keyword_p=*/true,
11387 /*check_dependency_p=*/true,
11388 /*is_declaration=*/false);
11389 /* If the template-id did not name a type, we are out of
11391 if (TREE_CODE (type) != TYPE_DECL)
11393 cp_parser_error (parser, "expected template-id for type");
11397 /* Otherwise, look for a type-name. */
11399 type = cp_parser_type_name (parser);
11400 /* Keep track of all name-lookups performed in class scopes. */
11404 && TREE_CODE (type) == TYPE_DECL
11405 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11406 maybe_note_name_used_in_class (DECL_NAME (type), type);
11407 /* If it didn't work out, we don't have a TYPE. */
11408 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11409 && !cp_parser_parse_definitely (parser))
11411 if (type && decl_specs)
11412 cp_parser_set_decl_spec_type (decl_specs, type,
11414 /*user_defined=*/true);
11417 /* If we didn't get a type-name, issue an error message. */
11418 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11420 cp_parser_error (parser, "expected type-name");
11421 return error_mark_node;
11424 /* There is no valid C++ program where a non-template type is
11425 followed by a "<". That usually indicates that the user thought
11426 that the type was a template. */
11427 if (type && type != error_mark_node)
11429 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11430 If it is, then the '<'...'>' enclose protocol names rather than
11431 template arguments, and so everything is fine. */
11432 if (c_dialect_objc ()
11433 && (objc_is_id (type) || objc_is_class_name (type)))
11435 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11436 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11438 /* Clobber the "unqualified" type previously entered into
11439 DECL_SPECS with the new, improved protocol-qualified version. */
11441 decl_specs->type = qual_type;
11446 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11453 /* Parse a type-name.
11466 Returns a TYPE_DECL for the type. */
11469 cp_parser_type_name (cp_parser* parser)
11473 /* We can't know yet whether it is a class-name or not. */
11474 cp_parser_parse_tentatively (parser);
11475 /* Try a class-name. */
11476 type_decl = cp_parser_class_name (parser,
11477 /*typename_keyword_p=*/false,
11478 /*template_keyword_p=*/false,
11480 /*check_dependency_p=*/true,
11481 /*class_head_p=*/false,
11482 /*is_declaration=*/false);
11483 /* If it's not a class-name, keep looking. */
11484 if (!cp_parser_parse_definitely (parser))
11486 /* It must be a typedef-name or an enum-name. */
11487 return cp_parser_nonclass_name (parser);
11493 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11501 Returns a TYPE_DECL for the type. */
11504 cp_parser_nonclass_name (cp_parser* parser)
11509 cp_token *token = cp_lexer_peek_token (parser->lexer);
11510 identifier = cp_parser_identifier (parser);
11511 if (identifier == error_mark_node)
11512 return error_mark_node;
11514 /* Look up the type-name. */
11515 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11517 if (TREE_CODE (type_decl) != TYPE_DECL
11518 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11520 /* See if this is an Objective-C type. */
11521 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11522 tree type = objc_get_protocol_qualified_type (identifier, protos);
11524 type_decl = TYPE_NAME (type);
11527 /* Issue an error if we did not find a type-name. */
11528 if (TREE_CODE (type_decl) != TYPE_DECL)
11530 if (!cp_parser_simulate_error (parser))
11531 cp_parser_name_lookup_error (parser, identifier, type_decl,
11532 "is not a type", token->location);
11533 return error_mark_node;
11535 /* Remember that the name was used in the definition of the
11536 current class so that we can check later to see if the
11537 meaning would have been different after the class was
11538 entirely defined. */
11539 else if (type_decl != error_mark_node
11541 maybe_note_name_used_in_class (identifier, type_decl);
11546 /* Parse an elaborated-type-specifier. Note that the grammar given
11547 here incorporates the resolution to DR68.
11549 elaborated-type-specifier:
11550 class-key :: [opt] nested-name-specifier [opt] identifier
11551 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11552 enum-key :: [opt] nested-name-specifier [opt] identifier
11553 typename :: [opt] nested-name-specifier identifier
11554 typename :: [opt] nested-name-specifier template [opt]
11559 elaborated-type-specifier:
11560 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11561 class-key attributes :: [opt] nested-name-specifier [opt]
11562 template [opt] template-id
11563 enum attributes :: [opt] nested-name-specifier [opt] identifier
11565 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11566 declared `friend'. If IS_DECLARATION is TRUE, then this
11567 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11568 something is being declared.
11570 Returns the TYPE specified. */
11573 cp_parser_elaborated_type_specifier (cp_parser* parser,
11575 bool is_declaration)
11577 enum tag_types tag_type;
11579 tree type = NULL_TREE;
11580 tree attributes = NULL_TREE;
11581 cp_token *token = NULL;
11583 /* See if we're looking at the `enum' keyword. */
11584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11586 /* Consume the `enum' token. */
11587 cp_lexer_consume_token (parser->lexer);
11588 /* Remember that it's an enumeration type. */
11589 tag_type = enum_type;
11590 /* Parse the optional `struct' or `class' key (for C++0x scoped
11592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11593 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11595 if (cxx_dialect == cxx98)
11596 maybe_warn_cpp0x ("scoped enums");
11598 /* Consume the `struct' or `class'. */
11599 cp_lexer_consume_token (parser->lexer);
11601 /* Parse the attributes. */
11602 attributes = cp_parser_attributes_opt (parser);
11604 /* Or, it might be `typename'. */
11605 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11608 /* Consume the `typename' token. */
11609 cp_lexer_consume_token (parser->lexer);
11610 /* Remember that it's a `typename' type. */
11611 tag_type = typename_type;
11612 /* The `typename' keyword is only allowed in templates. */
11613 if (!processing_template_decl)
11614 permerror (input_location, "using %<typename%> outside of template");
11616 /* Otherwise it must be a class-key. */
11619 tag_type = cp_parser_class_key (parser);
11620 if (tag_type == none_type)
11621 return error_mark_node;
11622 /* Parse the attributes. */
11623 attributes = cp_parser_attributes_opt (parser);
11626 /* Look for the `::' operator. */
11627 cp_parser_global_scope_opt (parser,
11628 /*current_scope_valid_p=*/false);
11629 /* Look for the nested-name-specifier. */
11630 if (tag_type == typename_type)
11632 if (!cp_parser_nested_name_specifier (parser,
11633 /*typename_keyword_p=*/true,
11634 /*check_dependency_p=*/true,
11637 return error_mark_node;
11640 /* Even though `typename' is not present, the proposed resolution
11641 to Core Issue 180 says that in `class A<T>::B', `B' should be
11642 considered a type-name, even if `A<T>' is dependent. */
11643 cp_parser_nested_name_specifier_opt (parser,
11644 /*typename_keyword_p=*/true,
11645 /*check_dependency_p=*/true,
11648 /* For everything but enumeration types, consider a template-id.
11649 For an enumeration type, consider only a plain identifier. */
11650 if (tag_type != enum_type)
11652 bool template_p = false;
11655 /* Allow the `template' keyword. */
11656 template_p = cp_parser_optional_template_keyword (parser);
11657 /* If we didn't see `template', we don't know if there's a
11658 template-id or not. */
11660 cp_parser_parse_tentatively (parser);
11661 /* Parse the template-id. */
11662 token = cp_lexer_peek_token (parser->lexer);
11663 decl = cp_parser_template_id (parser, template_p,
11664 /*check_dependency_p=*/true,
11666 /* If we didn't find a template-id, look for an ordinary
11668 if (!template_p && !cp_parser_parse_definitely (parser))
11670 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11671 in effect, then we must assume that, upon instantiation, the
11672 template will correspond to a class. */
11673 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11674 && tag_type == typename_type)
11675 type = make_typename_type (parser->scope, decl,
11677 /*complain=*/tf_error);
11678 /* If the `typename' keyword is in effect and DECL is not a type
11679 decl. Then type is non existant. */
11680 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11683 type = TREE_TYPE (decl);
11688 token = cp_lexer_peek_token (parser->lexer);
11689 identifier = cp_parser_identifier (parser);
11691 if (identifier == error_mark_node)
11693 parser->scope = NULL_TREE;
11694 return error_mark_node;
11697 /* For a `typename', we needn't call xref_tag. */
11698 if (tag_type == typename_type
11699 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11700 return cp_parser_make_typename_type (parser, parser->scope,
11703 /* Look up a qualified name in the usual way. */
11707 tree ambiguous_decls;
11709 decl = cp_parser_lookup_name (parser, identifier,
11711 /*is_template=*/false,
11712 /*is_namespace=*/false,
11713 /*check_dependency=*/true,
11717 /* If the lookup was ambiguous, an error will already have been
11719 if (ambiguous_decls)
11720 return error_mark_node;
11722 /* If we are parsing friend declaration, DECL may be a
11723 TEMPLATE_DECL tree node here. However, we need to check
11724 whether this TEMPLATE_DECL results in valid code. Consider
11725 the following example:
11728 template <class T> class C {};
11731 template <class T> friend class N::C; // #1, valid code
11733 template <class T> class Y {
11734 friend class N::C; // #2, invalid code
11737 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11738 name lookup of `N::C'. We see that friend declaration must
11739 be template for the code to be valid. Note that
11740 processing_template_decl does not work here since it is
11741 always 1 for the above two cases. */
11743 decl = (cp_parser_maybe_treat_template_as_class
11744 (decl, /*tag_name_p=*/is_friend
11745 && parser->num_template_parameter_lists));
11747 if (TREE_CODE (decl) != TYPE_DECL)
11749 cp_parser_diagnose_invalid_type_name (parser,
11753 return error_mark_node;
11756 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11758 bool allow_template = (parser->num_template_parameter_lists
11759 || DECL_SELF_REFERENCE_P (decl));
11760 type = check_elaborated_type_specifier (tag_type, decl,
11763 if (type == error_mark_node)
11764 return error_mark_node;
11767 /* Forward declarations of nested types, such as
11772 are invalid unless all components preceding the final '::'
11773 are complete. If all enclosing types are complete, these
11774 declarations become merely pointless.
11776 Invalid forward declarations of nested types are errors
11777 caught elsewhere in parsing. Those that are pointless arrive
11780 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11781 && !is_friend && !processing_explicit_instantiation)
11782 warning (0, "declaration %qD does not declare anything", decl);
11784 type = TREE_TYPE (decl);
11788 /* An elaborated-type-specifier sometimes introduces a new type and
11789 sometimes names an existing type. Normally, the rule is that it
11790 introduces a new type only if there is not an existing type of
11791 the same name already in scope. For example, given:
11794 void f() { struct S s; }
11796 the `struct S' in the body of `f' is the same `struct S' as in
11797 the global scope; the existing definition is used. However, if
11798 there were no global declaration, this would introduce a new
11799 local class named `S'.
11801 An exception to this rule applies to the following code:
11803 namespace N { struct S; }
11805 Here, the elaborated-type-specifier names a new type
11806 unconditionally; even if there is already an `S' in the
11807 containing scope this declaration names a new type.
11808 This exception only applies if the elaborated-type-specifier
11809 forms the complete declaration:
11813 A declaration consisting solely of `class-key identifier ;' is
11814 either a redeclaration of the name in the current scope or a
11815 forward declaration of the identifier as a class name. It
11816 introduces the name into the current scope.
11818 We are in this situation precisely when the next token is a `;'.
11820 An exception to the exception is that a `friend' declaration does
11821 *not* name a new type; i.e., given:
11823 struct S { friend struct T; };
11825 `T' is not a new type in the scope of `S'.
11827 Also, `new struct S' or `sizeof (struct S)' never results in the
11828 definition of a new type; a new type can only be declared in a
11829 declaration context. */
11835 /* Friends have special name lookup rules. */
11836 ts = ts_within_enclosing_non_class;
11837 else if (is_declaration
11838 && cp_lexer_next_token_is (parser->lexer,
11840 /* This is a `class-key identifier ;' */
11846 (parser->num_template_parameter_lists
11847 && (cp_parser_next_token_starts_class_definition_p (parser)
11848 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11849 /* An unqualified name was used to reference this type, so
11850 there were no qualifying templates. */
11851 if (!cp_parser_check_template_parameters (parser,
11852 /*num_templates=*/0,
11854 /*declarator=*/NULL))
11855 return error_mark_node;
11856 type = xref_tag (tag_type, identifier, ts, template_p);
11860 if (type == error_mark_node)
11861 return error_mark_node;
11863 /* Allow attributes on forward declarations of classes. */
11866 if (TREE_CODE (type) == TYPENAME_TYPE)
11867 warning (OPT_Wattributes,
11868 "attributes ignored on uninstantiated type");
11869 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11870 && ! processing_explicit_instantiation)
11871 warning (OPT_Wattributes,
11872 "attributes ignored on template instantiation");
11873 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11874 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11876 warning (OPT_Wattributes,
11877 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11880 if (tag_type != enum_type)
11881 cp_parser_check_class_key (tag_type, type);
11883 /* A "<" cannot follow an elaborated type specifier. If that
11884 happens, the user was probably trying to form a template-id. */
11885 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11890 /* Parse an enum-specifier.
11893 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11898 enum struct [C++0x]
11901 : type-specifier-seq
11904 enum-key attributes[opt] identifier [opt] enum-base [opt]
11905 { enumerator-list [opt] }attributes[opt]
11907 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11908 if the token stream isn't an enum-specifier after all. */
11911 cp_parser_enum_specifier (cp_parser* parser)
11916 bool scoped_enum_p = false;
11917 bool has_underlying_type = false;
11918 tree underlying_type = NULL_TREE;
11920 /* Parse tentatively so that we can back up if we don't find a
11922 cp_parser_parse_tentatively (parser);
11924 /* Caller guarantees that the current token is 'enum', an identifier
11925 possibly follows, and the token after that is an opening brace.
11926 If we don't have an identifier, fabricate an anonymous name for
11927 the enumeration being defined. */
11928 cp_lexer_consume_token (parser->lexer);
11930 /* Parse the "class" or "struct", which indicates a scoped
11931 enumeration type in C++0x. */
11932 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11933 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11935 if (cxx_dialect == cxx98)
11936 maybe_warn_cpp0x ("scoped enums");
11938 /* Consume the `struct' or `class' token. */
11939 cp_lexer_consume_token (parser->lexer);
11941 scoped_enum_p = true;
11944 attributes = cp_parser_attributes_opt (parser);
11946 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11947 identifier = cp_parser_identifier (parser);
11949 identifier = make_anon_name ();
11951 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11952 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11954 cp_decl_specifier_seq type_specifiers;
11956 /* At this point this is surely not elaborated type specifier. */
11957 if (!cp_parser_parse_definitely (parser))
11960 if (cxx_dialect == cxx98)
11961 maybe_warn_cpp0x ("scoped enums");
11963 /* Consume the `:'. */
11964 cp_lexer_consume_token (parser->lexer);
11966 has_underlying_type = true;
11968 /* Parse the type-specifier-seq. */
11969 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11972 /* If that didn't work, stop. */
11973 if (type_specifiers.type != error_mark_node)
11975 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11976 /*initialized=*/0, NULL);
11977 if (underlying_type == error_mark_node)
11978 underlying_type = NULL_TREE;
11982 /* Look for the `{' but don't consume it yet. */
11983 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11985 cp_parser_error (parser, "expected %<{%>");
11986 if (has_underlying_type)
11990 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11993 /* Issue an error message if type-definitions are forbidden here. */
11994 if (!cp_parser_check_type_definition (parser))
11995 type = error_mark_node;
11997 /* Create the new type. We do this before consuming the opening
11998 brace so the enum will be recorded as being on the line of its
11999 tag (or the 'enum' keyword, if there is no tag). */
12000 type = start_enum (identifier, underlying_type, scoped_enum_p);
12002 /* Consume the opening brace. */
12003 cp_lexer_consume_token (parser->lexer);
12005 if (type == error_mark_node)
12007 cp_parser_skip_to_end_of_block_or_statement (parser);
12008 return error_mark_node;
12011 /* If the next token is not '}', then there are some enumerators. */
12012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12013 cp_parser_enumerator_list (parser, type);
12015 /* Consume the final '}'. */
12016 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12018 /* Look for trailing attributes to apply to this enumeration, and
12019 apply them if appropriate. */
12020 if (cp_parser_allow_gnu_extensions_p (parser))
12022 tree trailing_attr = cp_parser_attributes_opt (parser);
12023 trailing_attr = chainon (trailing_attr, attributes);
12024 cplus_decl_attributes (&type,
12026 (int) ATTR_FLAG_TYPE_IN_PLACE);
12029 /* Finish up the enumeration. */
12030 finish_enum (type);
12035 /* Parse an enumerator-list. The enumerators all have the indicated
12039 enumerator-definition
12040 enumerator-list , enumerator-definition */
12043 cp_parser_enumerator_list (cp_parser* parser, tree type)
12047 /* Parse an enumerator-definition. */
12048 cp_parser_enumerator_definition (parser, type);
12050 /* If the next token is not a ',', we've reached the end of
12052 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12054 /* Otherwise, consume the `,' and keep going. */
12055 cp_lexer_consume_token (parser->lexer);
12056 /* If the next token is a `}', there is a trailing comma. */
12057 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12059 if (!in_system_header)
12060 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12066 /* Parse an enumerator-definition. The enumerator has the indicated
12069 enumerator-definition:
12071 enumerator = constant-expression
12077 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12082 /* Look for the identifier. */
12083 identifier = cp_parser_identifier (parser);
12084 if (identifier == error_mark_node)
12087 /* If the next token is an '=', then there is an explicit value. */
12088 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12090 /* Consume the `=' token. */
12091 cp_lexer_consume_token (parser->lexer);
12092 /* Parse the value. */
12093 value = cp_parser_constant_expression (parser,
12094 /*allow_non_constant_p=*/false,
12100 /* If we are processing a template, make sure the initializer of the
12101 enumerator doesn't contain any bare template parameter pack. */
12102 if (check_for_bare_parameter_packs (value))
12103 value = error_mark_node;
12105 /* Create the enumerator. */
12106 build_enumerator (identifier, value, type);
12109 /* Parse a namespace-name.
12112 original-namespace-name
12115 Returns the NAMESPACE_DECL for the namespace. */
12118 cp_parser_namespace_name (cp_parser* parser)
12121 tree namespace_decl;
12123 cp_token *token = cp_lexer_peek_token (parser->lexer);
12125 /* Get the name of the namespace. */
12126 identifier = cp_parser_identifier (parser);
12127 if (identifier == error_mark_node)
12128 return error_mark_node;
12130 /* Look up the identifier in the currently active scope. Look only
12131 for namespaces, due to:
12133 [basic.lookup.udir]
12135 When looking up a namespace-name in a using-directive or alias
12136 definition, only namespace names are considered.
12140 [basic.lookup.qual]
12142 During the lookup of a name preceding the :: scope resolution
12143 operator, object, function, and enumerator names are ignored.
12145 (Note that cp_parser_qualifying_entity only calls this
12146 function if the token after the name is the scope resolution
12148 namespace_decl = cp_parser_lookup_name (parser, identifier,
12150 /*is_template=*/false,
12151 /*is_namespace=*/true,
12152 /*check_dependency=*/true,
12153 /*ambiguous_decls=*/NULL,
12155 /* If it's not a namespace, issue an error. */
12156 if (namespace_decl == error_mark_node
12157 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12159 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12160 error ("%H%qD is not a namespace-name", &token->location, identifier);
12161 cp_parser_error (parser, "expected namespace-name");
12162 namespace_decl = error_mark_node;
12165 return namespace_decl;
12168 /* Parse a namespace-definition.
12170 namespace-definition:
12171 named-namespace-definition
12172 unnamed-namespace-definition
12174 named-namespace-definition:
12175 original-namespace-definition
12176 extension-namespace-definition
12178 original-namespace-definition:
12179 namespace identifier { namespace-body }
12181 extension-namespace-definition:
12182 namespace original-namespace-name { namespace-body }
12184 unnamed-namespace-definition:
12185 namespace { namespace-body } */
12188 cp_parser_namespace_definition (cp_parser* parser)
12190 tree identifier, attribs;
12191 bool has_visibility;
12194 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12197 cp_lexer_consume_token (parser->lexer);
12202 /* Look for the `namespace' keyword. */
12203 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12205 /* Get the name of the namespace. We do not attempt to distinguish
12206 between an original-namespace-definition and an
12207 extension-namespace-definition at this point. The semantic
12208 analysis routines are responsible for that. */
12209 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12210 identifier = cp_parser_identifier (parser);
12212 identifier = NULL_TREE;
12214 /* Parse any specified attributes. */
12215 attribs = cp_parser_attributes_opt (parser);
12217 /* Look for the `{' to start the namespace. */
12218 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12219 /* Start the namespace. */
12220 push_namespace (identifier);
12222 /* "inline namespace" is equivalent to a stub namespace definition
12223 followed by a strong using directive. */
12226 tree name_space = current_namespace;
12227 /* Set up namespace association. */
12228 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12229 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12230 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12231 /* Import the contents of the inline namespace. */
12233 do_using_directive (name_space);
12234 push_namespace (identifier);
12237 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12239 /* Parse the body of the namespace. */
12240 cp_parser_namespace_body (parser);
12242 #ifdef HANDLE_PRAGMA_VISIBILITY
12243 if (has_visibility)
12247 /* Finish the namespace. */
12249 /* Look for the final `}'. */
12250 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12253 /* Parse a namespace-body.
12256 declaration-seq [opt] */
12259 cp_parser_namespace_body (cp_parser* parser)
12261 cp_parser_declaration_seq_opt (parser);
12264 /* Parse a namespace-alias-definition.
12266 namespace-alias-definition:
12267 namespace identifier = qualified-namespace-specifier ; */
12270 cp_parser_namespace_alias_definition (cp_parser* parser)
12273 tree namespace_specifier;
12275 cp_token *token = cp_lexer_peek_token (parser->lexer);
12277 /* Look for the `namespace' keyword. */
12278 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12279 /* Look for the identifier. */
12280 identifier = cp_parser_identifier (parser);
12281 if (identifier == error_mark_node)
12283 /* Look for the `=' token. */
12284 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12285 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12287 error ("%H%<namespace%> definition is not allowed here", &token->location);
12288 /* Skip the definition. */
12289 cp_lexer_consume_token (parser->lexer);
12290 if (cp_parser_skip_to_closing_brace (parser))
12291 cp_lexer_consume_token (parser->lexer);
12294 cp_parser_require (parser, CPP_EQ, "%<=%>");
12295 /* Look for the qualified-namespace-specifier. */
12296 namespace_specifier
12297 = cp_parser_qualified_namespace_specifier (parser);
12298 /* Look for the `;' token. */
12299 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12301 /* Register the alias in the symbol table. */
12302 do_namespace_alias (identifier, namespace_specifier);
12305 /* Parse a qualified-namespace-specifier.
12307 qualified-namespace-specifier:
12308 :: [opt] nested-name-specifier [opt] namespace-name
12310 Returns a NAMESPACE_DECL corresponding to the specified
12314 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12316 /* Look for the optional `::'. */
12317 cp_parser_global_scope_opt (parser,
12318 /*current_scope_valid_p=*/false);
12320 /* Look for the optional nested-name-specifier. */
12321 cp_parser_nested_name_specifier_opt (parser,
12322 /*typename_keyword_p=*/false,
12323 /*check_dependency_p=*/true,
12325 /*is_declaration=*/true);
12327 return cp_parser_namespace_name (parser);
12330 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12331 access declaration.
12334 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12335 using :: unqualified-id ;
12337 access-declaration:
12343 cp_parser_using_declaration (cp_parser* parser,
12344 bool access_declaration_p)
12347 bool typename_p = false;
12348 bool global_scope_p;
12353 if (access_declaration_p)
12354 cp_parser_parse_tentatively (parser);
12357 /* Look for the `using' keyword. */
12358 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12360 /* Peek at the next token. */
12361 token = cp_lexer_peek_token (parser->lexer);
12362 /* See if it's `typename'. */
12363 if (token->keyword == RID_TYPENAME)
12365 /* Remember that we've seen it. */
12367 /* Consume the `typename' token. */
12368 cp_lexer_consume_token (parser->lexer);
12372 /* Look for the optional global scope qualification. */
12374 = (cp_parser_global_scope_opt (parser,
12375 /*current_scope_valid_p=*/false)
12378 /* If we saw `typename', or didn't see `::', then there must be a
12379 nested-name-specifier present. */
12380 if (typename_p || !global_scope_p)
12381 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12382 /*check_dependency_p=*/true,
12384 /*is_declaration=*/true);
12385 /* Otherwise, we could be in either of the two productions. In that
12386 case, treat the nested-name-specifier as optional. */
12388 qscope = cp_parser_nested_name_specifier_opt (parser,
12389 /*typename_keyword_p=*/false,
12390 /*check_dependency_p=*/true,
12392 /*is_declaration=*/true);
12394 qscope = global_namespace;
12396 if (access_declaration_p && cp_parser_error_occurred (parser))
12397 /* Something has already gone wrong; there's no need to parse
12398 further. Since an error has occurred, the return value of
12399 cp_parser_parse_definitely will be false, as required. */
12400 return cp_parser_parse_definitely (parser);
12402 token = cp_lexer_peek_token (parser->lexer);
12403 /* Parse the unqualified-id. */
12404 identifier = cp_parser_unqualified_id (parser,
12405 /*template_keyword_p=*/false,
12406 /*check_dependency_p=*/true,
12407 /*declarator_p=*/true,
12408 /*optional_p=*/false);
12410 if (access_declaration_p)
12412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12413 cp_parser_simulate_error (parser);
12414 if (!cp_parser_parse_definitely (parser))
12418 /* The function we call to handle a using-declaration is different
12419 depending on what scope we are in. */
12420 if (qscope == error_mark_node || identifier == error_mark_node)
12422 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12423 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12424 /* [namespace.udecl]
12426 A using declaration shall not name a template-id. */
12427 error ("%Ha template-id may not appear in a using-declaration",
12431 if (at_class_scope_p ())
12433 /* Create the USING_DECL. */
12434 decl = do_class_using_decl (parser->scope, identifier);
12436 if (check_for_bare_parameter_packs (decl))
12439 /* Add it to the list of members in this class. */
12440 finish_member_declaration (decl);
12444 decl = cp_parser_lookup_name_simple (parser,
12447 if (decl == error_mark_node)
12448 cp_parser_name_lookup_error (parser, identifier,
12451 else if (check_for_bare_parameter_packs (decl))
12453 else if (!at_namespace_scope_p ())
12454 do_local_using_decl (decl, qscope, identifier);
12456 do_toplevel_using_decl (decl, qscope, identifier);
12460 /* Look for the final `;'. */
12461 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12466 /* Parse a using-directive.
12469 using namespace :: [opt] nested-name-specifier [opt]
12470 namespace-name ; */
12473 cp_parser_using_directive (cp_parser* parser)
12475 tree namespace_decl;
12478 /* Look for the `using' keyword. */
12479 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12480 /* And the `namespace' keyword. */
12481 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12482 /* Look for the optional `::' operator. */
12483 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12484 /* And the optional nested-name-specifier. */
12485 cp_parser_nested_name_specifier_opt (parser,
12486 /*typename_keyword_p=*/false,
12487 /*check_dependency_p=*/true,
12489 /*is_declaration=*/true);
12490 /* Get the namespace being used. */
12491 namespace_decl = cp_parser_namespace_name (parser);
12492 /* And any specified attributes. */
12493 attribs = cp_parser_attributes_opt (parser);
12494 /* Update the symbol table. */
12495 parse_using_directive (namespace_decl, attribs);
12496 /* Look for the final `;'. */
12497 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12500 /* Parse an asm-definition.
12503 asm ( string-literal ) ;
12508 asm volatile [opt] ( string-literal ) ;
12509 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12510 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12511 : asm-operand-list [opt] ) ;
12512 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12513 : asm-operand-list [opt]
12514 : asm-operand-list [opt] ) ; */
12517 cp_parser_asm_definition (cp_parser* parser)
12520 tree outputs = NULL_TREE;
12521 tree inputs = NULL_TREE;
12522 tree clobbers = NULL_TREE;
12524 bool volatile_p = false;
12525 bool extended_p = false;
12526 bool invalid_inputs_p = false;
12527 bool invalid_outputs_p = false;
12529 /* Look for the `asm' keyword. */
12530 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12531 /* See if the next token is `volatile'. */
12532 if (cp_parser_allow_gnu_extensions_p (parser)
12533 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12535 /* Remember that we saw the `volatile' keyword. */
12537 /* Consume the token. */
12538 cp_lexer_consume_token (parser->lexer);
12540 /* Look for the opening `('. */
12541 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12543 /* Look for the string. */
12544 string = cp_parser_string_literal (parser, false, false);
12545 if (string == error_mark_node)
12547 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12548 /*consume_paren=*/true);
12552 /* If we're allowing GNU extensions, check for the extended assembly
12553 syntax. Unfortunately, the `:' tokens need not be separated by
12554 a space in C, and so, for compatibility, we tolerate that here
12555 too. Doing that means that we have to treat the `::' operator as
12557 if (cp_parser_allow_gnu_extensions_p (parser)
12558 && parser->in_function_body
12559 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12560 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12562 bool inputs_p = false;
12563 bool clobbers_p = false;
12565 /* The extended syntax was used. */
12568 /* Look for outputs. */
12569 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12571 /* Consume the `:'. */
12572 cp_lexer_consume_token (parser->lexer);
12573 /* Parse the output-operands. */
12574 if (cp_lexer_next_token_is_not (parser->lexer,
12576 && cp_lexer_next_token_is_not (parser->lexer,
12578 && cp_lexer_next_token_is_not (parser->lexer,
12580 outputs = cp_parser_asm_operand_list (parser);
12582 if (outputs == error_mark_node)
12583 invalid_outputs_p = true;
12585 /* If the next token is `::', there are no outputs, and the
12586 next token is the beginning of the inputs. */
12587 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12588 /* The inputs are coming next. */
12591 /* Look for inputs. */
12593 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12595 /* Consume the `:' or `::'. */
12596 cp_lexer_consume_token (parser->lexer);
12597 /* Parse the output-operands. */
12598 if (cp_lexer_next_token_is_not (parser->lexer,
12600 && cp_lexer_next_token_is_not (parser->lexer,
12602 inputs = cp_parser_asm_operand_list (parser);
12604 if (inputs == error_mark_node)
12605 invalid_inputs_p = true;
12607 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12608 /* The clobbers are coming next. */
12611 /* Look for clobbers. */
12613 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12615 /* Consume the `:' or `::'. */
12616 cp_lexer_consume_token (parser->lexer);
12617 /* Parse the clobbers. */
12618 if (cp_lexer_next_token_is_not (parser->lexer,
12620 clobbers = cp_parser_asm_clobber_list (parser);
12623 /* Look for the closing `)'. */
12624 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12625 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12626 /*consume_paren=*/true);
12627 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12629 if (!invalid_inputs_p && !invalid_outputs_p)
12631 /* Create the ASM_EXPR. */
12632 if (parser->in_function_body)
12634 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12636 /* If the extended syntax was not used, mark the ASM_EXPR. */
12639 tree temp = asm_stmt;
12640 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12641 temp = TREE_OPERAND (temp, 0);
12643 ASM_INPUT_P (temp) = 1;
12647 cgraph_add_asm_node (string);
12651 /* Declarators [gram.dcl.decl] */
12653 /* Parse an init-declarator.
12656 declarator initializer [opt]
12661 declarator asm-specification [opt] attributes [opt] initializer [opt]
12663 function-definition:
12664 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12666 decl-specifier-seq [opt] declarator function-try-block
12670 function-definition:
12671 __extension__ function-definition
12673 The DECL_SPECIFIERS apply to this declarator. Returns a
12674 representation of the entity declared. If MEMBER_P is TRUE, then
12675 this declarator appears in a class scope. The new DECL created by
12676 this declarator is returned.
12678 The CHECKS are access checks that should be performed once we know
12679 what entity is being declared (and, therefore, what classes have
12682 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12683 for a function-definition here as well. If the declarator is a
12684 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12685 be TRUE upon return. By that point, the function-definition will
12686 have been completely parsed.
12688 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12692 cp_parser_init_declarator (cp_parser* parser,
12693 cp_decl_specifier_seq *decl_specifiers,
12694 VEC (deferred_access_check,gc)* checks,
12695 bool function_definition_allowed_p,
12697 int declares_class_or_enum,
12698 bool* function_definition_p)
12700 cp_token *token = NULL, *asm_spec_start_token = NULL,
12701 *attributes_start_token = NULL;
12702 cp_declarator *declarator;
12703 tree prefix_attributes;
12705 tree asm_specification;
12707 tree decl = NULL_TREE;
12709 int is_initialized;
12710 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12711 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12713 enum cpp_ttype initialization_kind;
12714 bool is_direct_init = false;
12715 bool is_non_constant_init;
12716 int ctor_dtor_or_conv_p;
12718 tree pushed_scope = NULL;
12720 /* Gather the attributes that were provided with the
12721 decl-specifiers. */
12722 prefix_attributes = decl_specifiers->attributes;
12724 /* Assume that this is not the declarator for a function
12726 if (function_definition_p)
12727 *function_definition_p = false;
12729 /* Defer access checks while parsing the declarator; we cannot know
12730 what names are accessible until we know what is being
12732 resume_deferring_access_checks ();
12734 /* Parse the declarator. */
12735 token = cp_lexer_peek_token (parser->lexer);
12737 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12738 &ctor_dtor_or_conv_p,
12739 /*parenthesized_p=*/NULL,
12740 /*member_p=*/false);
12741 /* Gather up the deferred checks. */
12742 stop_deferring_access_checks ();
12744 /* If the DECLARATOR was erroneous, there's no need to go
12746 if (declarator == cp_error_declarator)
12747 return error_mark_node;
12749 /* Check that the number of template-parameter-lists is OK. */
12750 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12752 return error_mark_node;
12754 if (declares_class_or_enum & 2)
12755 cp_parser_check_for_definition_in_return_type (declarator,
12756 decl_specifiers->type,
12757 decl_specifiers->type_location);
12759 /* Figure out what scope the entity declared by the DECLARATOR is
12760 located in. `grokdeclarator' sometimes changes the scope, so
12761 we compute it now. */
12762 scope = get_scope_of_declarator (declarator);
12764 /* If we're allowing GNU extensions, look for an asm-specification
12766 if (cp_parser_allow_gnu_extensions_p (parser))
12768 /* Look for an asm-specification. */
12769 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12770 asm_specification = cp_parser_asm_specification_opt (parser);
12771 /* And attributes. */
12772 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12773 attributes = cp_parser_attributes_opt (parser);
12777 asm_specification = NULL_TREE;
12778 attributes = NULL_TREE;
12781 /* Peek at the next token. */
12782 token = cp_lexer_peek_token (parser->lexer);
12783 /* Check to see if the token indicates the start of a
12784 function-definition. */
12785 if (function_declarator_p (declarator)
12786 && cp_parser_token_starts_function_definition_p (token))
12788 if (!function_definition_allowed_p)
12790 /* If a function-definition should not appear here, issue an
12792 cp_parser_error (parser,
12793 "a function-definition is not allowed here");
12794 return error_mark_node;
12798 location_t func_brace_location
12799 = cp_lexer_peek_token (parser->lexer)->location;
12801 /* Neither attributes nor an asm-specification are allowed
12802 on a function-definition. */
12803 if (asm_specification)
12804 error ("%Han asm-specification is not allowed "
12805 "on a function-definition",
12806 &asm_spec_start_token->location);
12808 error ("%Hattributes are not allowed on a function-definition",
12809 &attributes_start_token->location);
12810 /* This is a function-definition. */
12811 *function_definition_p = true;
12813 /* Parse the function definition. */
12815 decl = cp_parser_save_member_function_body (parser,
12818 prefix_attributes);
12821 = (cp_parser_function_definition_from_specifiers_and_declarator
12822 (parser, decl_specifiers, prefix_attributes, declarator));
12824 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12826 /* This is where the prologue starts... */
12827 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12828 = func_brace_location;
12837 Only in function declarations for constructors, destructors, and
12838 type conversions can the decl-specifier-seq be omitted.
12840 We explicitly postpone this check past the point where we handle
12841 function-definitions because we tolerate function-definitions
12842 that are missing their return types in some modes. */
12843 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12845 cp_parser_error (parser,
12846 "expected constructor, destructor, or type conversion");
12847 return error_mark_node;
12850 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12851 if (token->type == CPP_EQ
12852 || token->type == CPP_OPEN_PAREN
12853 || token->type == CPP_OPEN_BRACE)
12855 is_initialized = SD_INITIALIZED;
12856 initialization_kind = token->type;
12858 if (token->type == CPP_EQ
12859 && function_declarator_p (declarator))
12861 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12862 if (t2->keyword == RID_DEFAULT)
12863 is_initialized = SD_DEFAULTED;
12864 else if (t2->keyword == RID_DELETE)
12865 is_initialized = SD_DELETED;
12870 /* If the init-declarator isn't initialized and isn't followed by a
12871 `,' or `;', it's not a valid init-declarator. */
12872 if (token->type != CPP_COMMA
12873 && token->type != CPP_SEMICOLON)
12875 cp_parser_error (parser, "expected initializer");
12876 return error_mark_node;
12878 is_initialized = SD_UNINITIALIZED;
12879 initialization_kind = CPP_EOF;
12882 /* Because start_decl has side-effects, we should only call it if we
12883 know we're going ahead. By this point, we know that we cannot
12884 possibly be looking at any other construct. */
12885 cp_parser_commit_to_tentative_parse (parser);
12887 /* If the decl specifiers were bad, issue an error now that we're
12888 sure this was intended to be a declarator. Then continue
12889 declaring the variable(s), as int, to try to cut down on further
12891 if (decl_specifiers->any_specifiers_p
12892 && decl_specifiers->type == error_mark_node)
12894 cp_parser_error (parser, "invalid type in declaration");
12895 decl_specifiers->type = integer_type_node;
12898 /* Check to see whether or not this declaration is a friend. */
12899 friend_p = cp_parser_friend_p (decl_specifiers);
12901 /* Enter the newly declared entry in the symbol table. If we're
12902 processing a declaration in a class-specifier, we wait until
12903 after processing the initializer. */
12906 if (parser->in_unbraced_linkage_specification_p)
12907 decl_specifiers->storage_class = sc_extern;
12908 decl = start_decl (declarator, decl_specifiers,
12909 is_initialized, attributes, prefix_attributes,
12913 /* Enter the SCOPE. That way unqualified names appearing in the
12914 initializer will be looked up in SCOPE. */
12915 pushed_scope = push_scope (scope);
12917 /* Perform deferred access control checks, now that we know in which
12918 SCOPE the declared entity resides. */
12919 if (!member_p && decl)
12921 tree saved_current_function_decl = NULL_TREE;
12923 /* If the entity being declared is a function, pretend that we
12924 are in its scope. If it is a `friend', it may have access to
12925 things that would not otherwise be accessible. */
12926 if (TREE_CODE (decl) == FUNCTION_DECL)
12928 saved_current_function_decl = current_function_decl;
12929 current_function_decl = decl;
12932 /* Perform access checks for template parameters. */
12933 cp_parser_perform_template_parameter_access_checks (checks);
12935 /* Perform the access control checks for the declarator and the
12936 decl-specifiers. */
12937 perform_deferred_access_checks ();
12939 /* Restore the saved value. */
12940 if (TREE_CODE (decl) == FUNCTION_DECL)
12941 current_function_decl = saved_current_function_decl;
12944 /* Parse the initializer. */
12945 initializer = NULL_TREE;
12946 is_direct_init = false;
12947 is_non_constant_init = true;
12948 if (is_initialized)
12950 if (function_declarator_p (declarator))
12952 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12953 if (initialization_kind == CPP_EQ)
12954 initializer = cp_parser_pure_specifier (parser);
12957 /* If the declaration was erroneous, we don't really
12958 know what the user intended, so just silently
12959 consume the initializer. */
12960 if (decl != error_mark_node)
12961 error ("%Hinitializer provided for function",
12962 &initializer_start_token->location);
12963 cp_parser_skip_to_closing_parenthesis (parser,
12964 /*recovering=*/true,
12965 /*or_comma=*/false,
12966 /*consume_paren=*/true);
12970 initializer = cp_parser_initializer (parser,
12972 &is_non_constant_init);
12975 /* The old parser allows attributes to appear after a parenthesized
12976 initializer. Mark Mitchell proposed removing this functionality
12977 on the GCC mailing lists on 2002-08-13. This parser accepts the
12978 attributes -- but ignores them. */
12979 if (cp_parser_allow_gnu_extensions_p (parser)
12980 && initialization_kind == CPP_OPEN_PAREN)
12981 if (cp_parser_attributes_opt (parser))
12982 warning (OPT_Wattributes,
12983 "attributes after parenthesized initializer ignored");
12985 /* For an in-class declaration, use `grokfield' to create the
12991 pop_scope (pushed_scope);
12992 pushed_scope = false;
12994 decl = grokfield (declarator, decl_specifiers,
12995 initializer, !is_non_constant_init,
12996 /*asmspec=*/NULL_TREE,
12997 prefix_attributes);
12998 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12999 cp_parser_save_default_args (parser, decl);
13002 /* Finish processing the declaration. But, skip friend
13004 if (!friend_p && decl && decl != error_mark_node)
13006 cp_finish_decl (decl,
13007 initializer, !is_non_constant_init,
13009 /* If the initializer is in parentheses, then this is
13010 a direct-initialization, which means that an
13011 `explicit' constructor is OK. Otherwise, an
13012 `explicit' constructor cannot be used. */
13013 ((is_direct_init || !is_initialized)
13014 ? 0 : LOOKUP_ONLYCONVERTING));
13016 else if ((cxx_dialect != cxx98) && friend_p
13017 && decl && TREE_CODE (decl) == FUNCTION_DECL)
13018 /* Core issue #226 (C++0x only): A default template-argument
13019 shall not be specified in a friend class template
13021 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
13022 /*is_partial=*/0, /*is_friend_decl=*/1);
13024 if (!friend_p && pushed_scope)
13025 pop_scope (pushed_scope);
13030 /* Parse a declarator.
13034 ptr-operator declarator
13036 abstract-declarator:
13037 ptr-operator abstract-declarator [opt]
13038 direct-abstract-declarator
13043 attributes [opt] direct-declarator
13044 attributes [opt] ptr-operator declarator
13046 abstract-declarator:
13047 attributes [opt] ptr-operator abstract-declarator [opt]
13048 attributes [opt] direct-abstract-declarator
13050 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13051 detect constructor, destructor or conversion operators. It is set
13052 to -1 if the declarator is a name, and +1 if it is a
13053 function. Otherwise it is set to zero. Usually you just want to
13054 test for >0, but internally the negative value is used.
13056 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13057 a decl-specifier-seq unless it declares a constructor, destructor,
13058 or conversion. It might seem that we could check this condition in
13059 semantic analysis, rather than parsing, but that makes it difficult
13060 to handle something like `f()'. We want to notice that there are
13061 no decl-specifiers, and therefore realize that this is an
13062 expression, not a declaration.)
13064 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13065 the declarator is a direct-declarator of the form "(...)".
13067 MEMBER_P is true iff this declarator is a member-declarator. */
13069 static cp_declarator *
13070 cp_parser_declarator (cp_parser* parser,
13071 cp_parser_declarator_kind dcl_kind,
13072 int* ctor_dtor_or_conv_p,
13073 bool* parenthesized_p,
13077 cp_declarator *declarator;
13078 enum tree_code code;
13079 cp_cv_quals cv_quals;
13081 tree attributes = NULL_TREE;
13083 /* Assume this is not a constructor, destructor, or type-conversion
13085 if (ctor_dtor_or_conv_p)
13086 *ctor_dtor_or_conv_p = 0;
13088 if (cp_parser_allow_gnu_extensions_p (parser))
13089 attributes = cp_parser_attributes_opt (parser);
13091 /* Peek at the next token. */
13092 token = cp_lexer_peek_token (parser->lexer);
13094 /* Check for the ptr-operator production. */
13095 cp_parser_parse_tentatively (parser);
13096 /* Parse the ptr-operator. */
13097 code = cp_parser_ptr_operator (parser,
13100 /* If that worked, then we have a ptr-operator. */
13101 if (cp_parser_parse_definitely (parser))
13103 /* If a ptr-operator was found, then this declarator was not
13105 if (parenthesized_p)
13106 *parenthesized_p = true;
13107 /* The dependent declarator is optional if we are parsing an
13108 abstract-declarator. */
13109 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13110 cp_parser_parse_tentatively (parser);
13112 /* Parse the dependent declarator. */
13113 declarator = cp_parser_declarator (parser, dcl_kind,
13114 /*ctor_dtor_or_conv_p=*/NULL,
13115 /*parenthesized_p=*/NULL,
13116 /*member_p=*/false);
13118 /* If we are parsing an abstract-declarator, we must handle the
13119 case where the dependent declarator is absent. */
13120 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13121 && !cp_parser_parse_definitely (parser))
13124 declarator = cp_parser_make_indirect_declarator
13125 (code, class_type, cv_quals, declarator);
13127 /* Everything else is a direct-declarator. */
13130 if (parenthesized_p)
13131 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13133 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13134 ctor_dtor_or_conv_p,
13138 if (attributes && declarator && declarator != cp_error_declarator)
13139 declarator->attributes = attributes;
13144 /* Parse a direct-declarator or direct-abstract-declarator.
13148 direct-declarator ( parameter-declaration-clause )
13149 cv-qualifier-seq [opt]
13150 exception-specification [opt]
13151 direct-declarator [ constant-expression [opt] ]
13154 direct-abstract-declarator:
13155 direct-abstract-declarator [opt]
13156 ( parameter-declaration-clause )
13157 cv-qualifier-seq [opt]
13158 exception-specification [opt]
13159 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13160 ( abstract-declarator )
13162 Returns a representation of the declarator. DCL_KIND is
13163 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13164 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13165 we are parsing a direct-declarator. It is
13166 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13167 of ambiguity we prefer an abstract declarator, as per
13168 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13169 cp_parser_declarator. */
13171 static cp_declarator *
13172 cp_parser_direct_declarator (cp_parser* parser,
13173 cp_parser_declarator_kind dcl_kind,
13174 int* ctor_dtor_or_conv_p,
13178 cp_declarator *declarator = NULL;
13179 tree scope = NULL_TREE;
13180 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13181 bool saved_in_declarator_p = parser->in_declarator_p;
13183 tree pushed_scope = NULL_TREE;
13187 /* Peek at the next token. */
13188 token = cp_lexer_peek_token (parser->lexer);
13189 if (token->type == CPP_OPEN_PAREN)
13191 /* This is either a parameter-declaration-clause, or a
13192 parenthesized declarator. When we know we are parsing a
13193 named declarator, it must be a parenthesized declarator
13194 if FIRST is true. For instance, `(int)' is a
13195 parameter-declaration-clause, with an omitted
13196 direct-abstract-declarator. But `((*))', is a
13197 parenthesized abstract declarator. Finally, when T is a
13198 template parameter `(T)' is a
13199 parameter-declaration-clause, and not a parenthesized
13202 We first try and parse a parameter-declaration-clause,
13203 and then try a nested declarator (if FIRST is true).
13205 It is not an error for it not to be a
13206 parameter-declaration-clause, even when FIRST is
13212 The first is the declaration of a function while the
13213 second is the definition of a variable, including its
13216 Having seen only the parenthesis, we cannot know which of
13217 these two alternatives should be selected. Even more
13218 complex are examples like:
13223 The former is a function-declaration; the latter is a
13224 variable initialization.
13226 Thus again, we try a parameter-declaration-clause, and if
13227 that fails, we back out and return. */
13229 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13232 unsigned saved_num_template_parameter_lists;
13233 bool is_declarator = false;
13236 /* In a member-declarator, the only valid interpretation
13237 of a parenthesis is the start of a
13238 parameter-declaration-clause. (It is invalid to
13239 initialize a static data member with a parenthesized
13240 initializer; only the "=" form of initialization is
13243 cp_parser_parse_tentatively (parser);
13245 /* Consume the `('. */
13246 cp_lexer_consume_token (parser->lexer);
13249 /* If this is going to be an abstract declarator, we're
13250 in a declarator and we can't have default args. */
13251 parser->default_arg_ok_p = false;
13252 parser->in_declarator_p = true;
13255 /* Inside the function parameter list, surrounding
13256 template-parameter-lists do not apply. */
13257 saved_num_template_parameter_lists
13258 = parser->num_template_parameter_lists;
13259 parser->num_template_parameter_lists = 0;
13261 begin_scope (sk_function_parms, NULL_TREE);
13263 /* Parse the parameter-declaration-clause. */
13264 params = cp_parser_parameter_declaration_clause (parser);
13266 parser->num_template_parameter_lists
13267 = saved_num_template_parameter_lists;
13269 /* If all went well, parse the cv-qualifier-seq and the
13270 exception-specification. */
13271 if (member_p || cp_parser_parse_definitely (parser))
13273 cp_cv_quals cv_quals;
13274 tree exception_specification;
13277 is_declarator = true;
13279 if (ctor_dtor_or_conv_p)
13280 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13282 /* Consume the `)'. */
13283 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13285 /* Parse the cv-qualifier-seq. */
13286 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13287 /* And the exception-specification. */
13288 exception_specification
13289 = cp_parser_exception_specification_opt (parser);
13292 = cp_parser_late_return_type_opt (parser);
13294 /* Create the function-declarator. */
13295 declarator = make_call_declarator (declarator,
13298 exception_specification,
13300 /* Any subsequent parameter lists are to do with
13301 return type, so are not those of the declared
13303 parser->default_arg_ok_p = false;
13306 /* Remove the function parms from scope. */
13307 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13308 pop_binding (DECL_NAME (t), t);
13312 /* Repeat the main loop. */
13316 /* If this is the first, we can try a parenthesized
13320 bool saved_in_type_id_in_expr_p;
13322 parser->default_arg_ok_p = saved_default_arg_ok_p;
13323 parser->in_declarator_p = saved_in_declarator_p;
13325 /* Consume the `('. */
13326 cp_lexer_consume_token (parser->lexer);
13327 /* Parse the nested declarator. */
13328 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13329 parser->in_type_id_in_expr_p = true;
13331 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13332 /*parenthesized_p=*/NULL,
13334 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13336 /* Expect a `)'. */
13337 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13338 declarator = cp_error_declarator;
13339 if (declarator == cp_error_declarator)
13342 goto handle_declarator;
13344 /* Otherwise, we must be done. */
13348 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13349 && token->type == CPP_OPEN_SQUARE)
13351 /* Parse an array-declarator. */
13354 if (ctor_dtor_or_conv_p)
13355 *ctor_dtor_or_conv_p = 0;
13358 parser->default_arg_ok_p = false;
13359 parser->in_declarator_p = true;
13360 /* Consume the `['. */
13361 cp_lexer_consume_token (parser->lexer);
13362 /* Peek at the next token. */
13363 token = cp_lexer_peek_token (parser->lexer);
13364 /* If the next token is `]', then there is no
13365 constant-expression. */
13366 if (token->type != CPP_CLOSE_SQUARE)
13368 bool non_constant_p;
13371 = cp_parser_constant_expression (parser,
13372 /*allow_non_constant=*/true,
13374 if (!non_constant_p)
13375 bounds = fold_non_dependent_expr (bounds);
13376 /* Normally, the array bound must be an integral constant
13377 expression. However, as an extension, we allow VLAs
13378 in function scopes. */
13379 else if (!parser->in_function_body)
13381 error ("%Harray bound is not an integer constant",
13383 bounds = error_mark_node;
13385 else if (processing_template_decl && !error_operand_p (bounds))
13387 /* Remember this wasn't a constant-expression. */
13388 bounds = build_nop (TREE_TYPE (bounds), bounds);
13389 TREE_SIDE_EFFECTS (bounds) = 1;
13393 bounds = NULL_TREE;
13394 /* Look for the closing `]'. */
13395 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13397 declarator = cp_error_declarator;
13401 declarator = make_array_declarator (declarator, bounds);
13403 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13406 tree qualifying_scope;
13407 tree unqualified_name;
13408 special_function_kind sfk;
13410 bool pack_expansion_p = false;
13411 cp_token *declarator_id_start_token;
13413 /* Parse a declarator-id */
13414 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13417 cp_parser_parse_tentatively (parser);
13419 /* If we see an ellipsis, we should be looking at a
13421 if (token->type == CPP_ELLIPSIS)
13423 /* Consume the `...' */
13424 cp_lexer_consume_token (parser->lexer);
13426 pack_expansion_p = true;
13430 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13432 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13433 qualifying_scope = parser->scope;
13438 if (!unqualified_name && pack_expansion_p)
13440 /* Check whether an error occurred. */
13441 okay = !cp_parser_error_occurred (parser);
13443 /* We already consumed the ellipsis to mark a
13444 parameter pack, but we have no way to report it,
13445 so abort the tentative parse. We will be exiting
13446 immediately anyway. */
13447 cp_parser_abort_tentative_parse (parser);
13450 okay = cp_parser_parse_definitely (parser);
13453 unqualified_name = error_mark_node;
13454 else if (unqualified_name
13455 && (qualifying_scope
13456 || (TREE_CODE (unqualified_name)
13457 != IDENTIFIER_NODE)))
13459 cp_parser_error (parser, "expected unqualified-id");
13460 unqualified_name = error_mark_node;
13464 if (!unqualified_name)
13466 if (unqualified_name == error_mark_node)
13468 declarator = cp_error_declarator;
13469 pack_expansion_p = false;
13470 declarator->parameter_pack_p = false;
13474 if (qualifying_scope && at_namespace_scope_p ()
13475 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13477 /* In the declaration of a member of a template class
13478 outside of the class itself, the SCOPE will sometimes
13479 be a TYPENAME_TYPE. For example, given:
13481 template <typename T>
13482 int S<T>::R::i = 3;
13484 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13485 this context, we must resolve S<T>::R to an ordinary
13486 type, rather than a typename type.
13488 The reason we normally avoid resolving TYPENAME_TYPEs
13489 is that a specialization of `S' might render
13490 `S<T>::R' not a type. However, if `S' is
13491 specialized, then this `i' will not be used, so there
13492 is no harm in resolving the types here. */
13495 /* Resolve the TYPENAME_TYPE. */
13496 type = resolve_typename_type (qualifying_scope,
13497 /*only_current_p=*/false);
13498 /* If that failed, the declarator is invalid. */
13499 if (TREE_CODE (type) == TYPENAME_TYPE)
13500 error ("%H%<%T::%E%> is not a type",
13501 &declarator_id_start_token->location,
13502 TYPE_CONTEXT (qualifying_scope),
13503 TYPE_IDENTIFIER (qualifying_scope));
13504 qualifying_scope = type;
13509 if (unqualified_name)
13513 if (qualifying_scope
13514 && CLASS_TYPE_P (qualifying_scope))
13515 class_type = qualifying_scope;
13517 class_type = current_class_type;
13519 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13521 tree name_type = TREE_TYPE (unqualified_name);
13522 if (class_type && same_type_p (name_type, class_type))
13524 if (qualifying_scope
13525 && CLASSTYPE_USE_TEMPLATE (name_type))
13527 error ("%Hinvalid use of constructor as a template",
13528 &declarator_id_start_token->location);
13529 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13530 "name the constructor in a qualified name",
13532 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13533 class_type, name_type);
13534 declarator = cp_error_declarator;
13538 unqualified_name = constructor_name (class_type);
13542 /* We do not attempt to print the declarator
13543 here because we do not have enough
13544 information about its original syntactic
13546 cp_parser_error (parser, "invalid declarator");
13547 declarator = cp_error_declarator;
13554 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13555 sfk = sfk_destructor;
13556 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13557 sfk = sfk_conversion;
13558 else if (/* There's no way to declare a constructor
13559 for an anonymous type, even if the type
13560 got a name for linkage purposes. */
13561 !TYPE_WAS_ANONYMOUS (class_type)
13562 && constructor_name_p (unqualified_name,
13565 unqualified_name = constructor_name (class_type);
13566 sfk = sfk_constructor;
13569 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13570 *ctor_dtor_or_conv_p = -1;
13573 declarator = make_id_declarator (qualifying_scope,
13576 declarator->id_loc = token->location;
13577 declarator->parameter_pack_p = pack_expansion_p;
13579 if (pack_expansion_p)
13580 maybe_warn_variadic_templates ();
13583 handle_declarator:;
13584 scope = get_scope_of_declarator (declarator);
13586 /* Any names that appear after the declarator-id for a
13587 member are looked up in the containing scope. */
13588 pushed_scope = push_scope (scope);
13589 parser->in_declarator_p = true;
13590 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13591 || (declarator && declarator->kind == cdk_id))
13592 /* Default args are only allowed on function
13594 parser->default_arg_ok_p = saved_default_arg_ok_p;
13596 parser->default_arg_ok_p = false;
13605 /* For an abstract declarator, we might wind up with nothing at this
13606 point. That's an error; the declarator is not optional. */
13608 cp_parser_error (parser, "expected declarator");
13610 /* If we entered a scope, we must exit it now. */
13612 pop_scope (pushed_scope);
13614 parser->default_arg_ok_p = saved_default_arg_ok_p;
13615 parser->in_declarator_p = saved_in_declarator_p;
13620 /* Parse a ptr-operator.
13623 * cv-qualifier-seq [opt]
13625 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13630 & cv-qualifier-seq [opt]
13632 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13633 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13634 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13635 filled in with the TYPE containing the member. *CV_QUALS is
13636 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13637 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13638 Note that the tree codes returned by this function have nothing
13639 to do with the types of trees that will be eventually be created
13640 to represent the pointer or reference type being parsed. They are
13641 just constants with suggestive names. */
13642 static enum tree_code
13643 cp_parser_ptr_operator (cp_parser* parser,
13645 cp_cv_quals *cv_quals)
13647 enum tree_code code = ERROR_MARK;
13650 /* Assume that it's not a pointer-to-member. */
13652 /* And that there are no cv-qualifiers. */
13653 *cv_quals = TYPE_UNQUALIFIED;
13655 /* Peek at the next token. */
13656 token = cp_lexer_peek_token (parser->lexer);
13658 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13659 if (token->type == CPP_MULT)
13660 code = INDIRECT_REF;
13661 else if (token->type == CPP_AND)
13663 else if ((cxx_dialect != cxx98) &&
13664 token->type == CPP_AND_AND) /* C++0x only */
13665 code = NON_LVALUE_EXPR;
13667 if (code != ERROR_MARK)
13669 /* Consume the `*', `&' or `&&'. */
13670 cp_lexer_consume_token (parser->lexer);
13672 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13673 `&', if we are allowing GNU extensions. (The only qualifier
13674 that can legally appear after `&' is `restrict', but that is
13675 enforced during semantic analysis. */
13676 if (code == INDIRECT_REF
13677 || cp_parser_allow_gnu_extensions_p (parser))
13678 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13682 /* Try the pointer-to-member case. */
13683 cp_parser_parse_tentatively (parser);
13684 /* Look for the optional `::' operator. */
13685 cp_parser_global_scope_opt (parser,
13686 /*current_scope_valid_p=*/false);
13687 /* Look for the nested-name specifier. */
13688 token = cp_lexer_peek_token (parser->lexer);
13689 cp_parser_nested_name_specifier (parser,
13690 /*typename_keyword_p=*/false,
13691 /*check_dependency_p=*/true,
13693 /*is_declaration=*/false);
13694 /* If we found it, and the next token is a `*', then we are
13695 indeed looking at a pointer-to-member operator. */
13696 if (!cp_parser_error_occurred (parser)
13697 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13699 /* Indicate that the `*' operator was used. */
13700 code = INDIRECT_REF;
13702 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13703 error ("%H%qD is a namespace", &token->location, parser->scope);
13706 /* The type of which the member is a member is given by the
13708 *type = parser->scope;
13709 /* The next name will not be qualified. */
13710 parser->scope = NULL_TREE;
13711 parser->qualifying_scope = NULL_TREE;
13712 parser->object_scope = NULL_TREE;
13713 /* Look for the optional cv-qualifier-seq. */
13714 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13717 /* If that didn't work we don't have a ptr-operator. */
13718 if (!cp_parser_parse_definitely (parser))
13719 cp_parser_error (parser, "expected ptr-operator");
13725 /* Parse an (optional) cv-qualifier-seq.
13728 cv-qualifier cv-qualifier-seq [opt]
13739 Returns a bitmask representing the cv-qualifiers. */
13742 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13744 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13749 cp_cv_quals cv_qualifier;
13751 /* Peek at the next token. */
13752 token = cp_lexer_peek_token (parser->lexer);
13753 /* See if it's a cv-qualifier. */
13754 switch (token->keyword)
13757 cv_qualifier = TYPE_QUAL_CONST;
13761 cv_qualifier = TYPE_QUAL_VOLATILE;
13765 cv_qualifier = TYPE_QUAL_RESTRICT;
13769 cv_qualifier = TYPE_UNQUALIFIED;
13776 if (cv_quals & cv_qualifier)
13778 error ("%Hduplicate cv-qualifier", &token->location);
13779 cp_lexer_purge_token (parser->lexer);
13783 cp_lexer_consume_token (parser->lexer);
13784 cv_quals |= cv_qualifier;
13791 /* Parse a late-specified return type, if any. This is not a separate
13792 non-terminal, but part of a function declarator, which looks like
13796 Returns the type indicated by the type-id. */
13799 cp_parser_late_return_type_opt (cp_parser* parser)
13803 /* Peek at the next token. */
13804 token = cp_lexer_peek_token (parser->lexer);
13805 /* A late-specified return type is indicated by an initial '->'. */
13806 if (token->type != CPP_DEREF)
13809 /* Consume the ->. */
13810 cp_lexer_consume_token (parser->lexer);
13812 return cp_parser_type_id (parser);
13815 /* Parse a declarator-id.
13819 :: [opt] nested-name-specifier [opt] type-name
13821 In the `id-expression' case, the value returned is as for
13822 cp_parser_id_expression if the id-expression was an unqualified-id.
13823 If the id-expression was a qualified-id, then a SCOPE_REF is
13824 returned. The first operand is the scope (either a NAMESPACE_DECL
13825 or TREE_TYPE), but the second is still just a representation of an
13829 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13832 /* The expression must be an id-expression. Assume that qualified
13833 names are the names of types so that:
13836 int S<T>::R::i = 3;
13838 will work; we must treat `S<T>::R' as the name of a type.
13839 Similarly, assume that qualified names are templates, where
13843 int S<T>::R<T>::i = 3;
13846 id = cp_parser_id_expression (parser,
13847 /*template_keyword_p=*/false,
13848 /*check_dependency_p=*/false,
13849 /*template_p=*/NULL,
13850 /*declarator_p=*/true,
13852 if (id && BASELINK_P (id))
13853 id = BASELINK_FUNCTIONS (id);
13857 /* Parse a type-id.
13860 type-specifier-seq abstract-declarator [opt]
13862 Returns the TYPE specified. */
13865 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13867 cp_decl_specifier_seq type_specifier_seq;
13868 cp_declarator *abstract_declarator;
13870 /* Parse the type-specifier-seq. */
13871 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13872 &type_specifier_seq);
13873 if (type_specifier_seq.type == error_mark_node)
13874 return error_mark_node;
13876 /* There might or might not be an abstract declarator. */
13877 cp_parser_parse_tentatively (parser);
13878 /* Look for the declarator. */
13879 abstract_declarator
13880 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13881 /*parenthesized_p=*/NULL,
13882 /*member_p=*/false);
13883 /* Check to see if there really was a declarator. */
13884 if (!cp_parser_parse_definitely (parser))
13885 abstract_declarator = NULL;
13887 if (type_specifier_seq.type
13888 && type_uses_auto (type_specifier_seq.type))
13890 /* A type-id with type 'auto' is only ok if the abstract declarator
13891 is a function declarator with a late-specified return type. */
13892 if (abstract_declarator
13893 && abstract_declarator->kind == cdk_function
13894 && abstract_declarator->u.function.late_return_type)
13898 error ("invalid use of %<auto%>");
13899 return error_mark_node;
13903 return groktypename (&type_specifier_seq, abstract_declarator,
13907 static tree cp_parser_type_id (cp_parser *parser)
13909 return cp_parser_type_id_1 (parser, false);
13912 static tree cp_parser_template_type_arg (cp_parser *parser)
13914 return cp_parser_type_id_1 (parser, true);
13917 /* Parse a type-specifier-seq.
13919 type-specifier-seq:
13920 type-specifier type-specifier-seq [opt]
13924 type-specifier-seq:
13925 attributes type-specifier-seq [opt]
13927 If IS_CONDITION is true, we are at the start of a "condition",
13928 e.g., we've just seen "if (".
13930 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13933 cp_parser_type_specifier_seq (cp_parser* parser,
13935 cp_decl_specifier_seq *type_specifier_seq)
13937 bool seen_type_specifier = false;
13938 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13939 cp_token *start_token = NULL;
13941 /* Clear the TYPE_SPECIFIER_SEQ. */
13942 clear_decl_specs (type_specifier_seq);
13944 /* Parse the type-specifiers and attributes. */
13947 tree type_specifier;
13948 bool is_cv_qualifier;
13950 /* Check for attributes first. */
13951 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13953 type_specifier_seq->attributes =
13954 chainon (type_specifier_seq->attributes,
13955 cp_parser_attributes_opt (parser));
13959 /* record the token of the beginning of the type specifier seq,
13960 for error reporting purposes*/
13962 start_token = cp_lexer_peek_token (parser->lexer);
13964 /* Look for the type-specifier. */
13965 type_specifier = cp_parser_type_specifier (parser,
13967 type_specifier_seq,
13968 /*is_declaration=*/false,
13971 if (!type_specifier)
13973 /* If the first type-specifier could not be found, this is not a
13974 type-specifier-seq at all. */
13975 if (!seen_type_specifier)
13977 cp_parser_error (parser, "expected type-specifier");
13978 type_specifier_seq->type = error_mark_node;
13981 /* If subsequent type-specifiers could not be found, the
13982 type-specifier-seq is complete. */
13986 seen_type_specifier = true;
13987 /* The standard says that a condition can be:
13989 type-specifier-seq declarator = assignment-expression
13996 we should treat the "S" as a declarator, not as a
13997 type-specifier. The standard doesn't say that explicitly for
13998 type-specifier-seq, but it does say that for
13999 decl-specifier-seq in an ordinary declaration. Perhaps it
14000 would be clearer just to allow a decl-specifier-seq here, and
14001 then add a semantic restriction that if any decl-specifiers
14002 that are not type-specifiers appear, the program is invalid. */
14003 if (is_condition && !is_cv_qualifier)
14004 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14007 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14010 /* Parse a parameter-declaration-clause.
14012 parameter-declaration-clause:
14013 parameter-declaration-list [opt] ... [opt]
14014 parameter-declaration-list , ...
14016 Returns a representation for the parameter declarations. A return
14017 value of NULL indicates a parameter-declaration-clause consisting
14018 only of an ellipsis. */
14021 cp_parser_parameter_declaration_clause (cp_parser* parser)
14028 /* Peek at the next token. */
14029 token = cp_lexer_peek_token (parser->lexer);
14030 /* Check for trivial parameter-declaration-clauses. */
14031 if (token->type == CPP_ELLIPSIS)
14033 /* Consume the `...' token. */
14034 cp_lexer_consume_token (parser->lexer);
14037 else if (token->type == CPP_CLOSE_PAREN)
14038 /* There are no parameters. */
14040 #ifndef NO_IMPLICIT_EXTERN_C
14041 if (in_system_header && current_class_type == NULL
14042 && current_lang_name == lang_name_c)
14046 return void_list_node;
14048 /* Check for `(void)', too, which is a special case. */
14049 else if (token->keyword == RID_VOID
14050 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14051 == CPP_CLOSE_PAREN))
14053 /* Consume the `void' token. */
14054 cp_lexer_consume_token (parser->lexer);
14055 /* There are no parameters. */
14056 return void_list_node;
14059 /* Parse the parameter-declaration-list. */
14060 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14061 /* If a parse error occurred while parsing the
14062 parameter-declaration-list, then the entire
14063 parameter-declaration-clause is erroneous. */
14067 /* Peek at the next token. */
14068 token = cp_lexer_peek_token (parser->lexer);
14069 /* If it's a `,', the clause should terminate with an ellipsis. */
14070 if (token->type == CPP_COMMA)
14072 /* Consume the `,'. */
14073 cp_lexer_consume_token (parser->lexer);
14074 /* Expect an ellipsis. */
14076 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14078 /* It might also be `...' if the optional trailing `,' was
14080 else if (token->type == CPP_ELLIPSIS)
14082 /* Consume the `...' token. */
14083 cp_lexer_consume_token (parser->lexer);
14084 /* And remember that we saw it. */
14088 ellipsis_p = false;
14090 /* Finish the parameter list. */
14092 parameters = chainon (parameters, void_list_node);
14097 /* Parse a parameter-declaration-list.
14099 parameter-declaration-list:
14100 parameter-declaration
14101 parameter-declaration-list , parameter-declaration
14103 Returns a representation of the parameter-declaration-list, as for
14104 cp_parser_parameter_declaration_clause. However, the
14105 `void_list_node' is never appended to the list. Upon return,
14106 *IS_ERROR will be true iff an error occurred. */
14109 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14111 tree parameters = NULL_TREE;
14112 tree *tail = ¶meters;
14113 bool saved_in_unbraced_linkage_specification_p;
14116 /* Assume all will go well. */
14118 /* The special considerations that apply to a function within an
14119 unbraced linkage specifications do not apply to the parameters
14120 to the function. */
14121 saved_in_unbraced_linkage_specification_p
14122 = parser->in_unbraced_linkage_specification_p;
14123 parser->in_unbraced_linkage_specification_p = false;
14125 /* Look for more parameters. */
14128 cp_parameter_declarator *parameter;
14129 tree decl = error_mark_node;
14130 bool parenthesized_p;
14131 /* Parse the parameter. */
14133 = cp_parser_parameter_declaration (parser,
14134 /*template_parm_p=*/false,
14137 /* We don't know yet if the enclosing context is deprecated, so wait
14138 and warn in grokparms if appropriate. */
14139 deprecated_state = DEPRECATED_SUPPRESS;
14142 decl = grokdeclarator (parameter->declarator,
14143 ¶meter->decl_specifiers,
14145 parameter->default_argument != NULL_TREE,
14146 ¶meter->decl_specifiers.attributes);
14148 deprecated_state = DEPRECATED_NORMAL;
14150 /* If a parse error occurred parsing the parameter declaration,
14151 then the entire parameter-declaration-list is erroneous. */
14152 if (decl == error_mark_node)
14155 parameters = error_mark_node;
14159 if (parameter->decl_specifiers.attributes)
14160 cplus_decl_attributes (&decl,
14161 parameter->decl_specifiers.attributes,
14163 if (DECL_NAME (decl))
14164 decl = pushdecl (decl);
14166 if (decl != error_mark_node)
14168 retrofit_lang_decl (decl);
14169 DECL_PARM_INDEX (decl) = ++index;
14172 /* Add the new parameter to the list. */
14173 *tail = build_tree_list (parameter->default_argument, decl);
14174 tail = &TREE_CHAIN (*tail);
14176 /* Peek at the next token. */
14177 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14178 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14179 /* These are for Objective-C++ */
14180 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14181 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14182 /* The parameter-declaration-list is complete. */
14184 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14188 /* Peek at the next token. */
14189 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14190 /* If it's an ellipsis, then the list is complete. */
14191 if (token->type == CPP_ELLIPSIS)
14193 /* Otherwise, there must be more parameters. Consume the
14195 cp_lexer_consume_token (parser->lexer);
14196 /* When parsing something like:
14198 int i(float f, double d)
14200 we can tell after seeing the declaration for "f" that we
14201 are not looking at an initialization of a variable "i",
14202 but rather at the declaration of a function "i".
14204 Due to the fact that the parsing of template arguments
14205 (as specified to a template-id) requires backtracking we
14206 cannot use this technique when inside a template argument
14208 if (!parser->in_template_argument_list_p
14209 && !parser->in_type_id_in_expr_p
14210 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14211 /* However, a parameter-declaration of the form
14212 "foat(f)" (which is a valid declaration of a
14213 parameter "f") can also be interpreted as an
14214 expression (the conversion of "f" to "float"). */
14215 && !parenthesized_p)
14216 cp_parser_commit_to_tentative_parse (parser);
14220 cp_parser_error (parser, "expected %<,%> or %<...%>");
14221 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14222 cp_parser_skip_to_closing_parenthesis (parser,
14223 /*recovering=*/true,
14224 /*or_comma=*/false,
14225 /*consume_paren=*/false);
14230 parser->in_unbraced_linkage_specification_p
14231 = saved_in_unbraced_linkage_specification_p;
14236 /* Parse a parameter declaration.
14238 parameter-declaration:
14239 decl-specifier-seq ... [opt] declarator
14240 decl-specifier-seq declarator = assignment-expression
14241 decl-specifier-seq ... [opt] abstract-declarator [opt]
14242 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14244 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14245 declares a template parameter. (In that case, a non-nested `>'
14246 token encountered during the parsing of the assignment-expression
14247 is not interpreted as a greater-than operator.)
14249 Returns a representation of the parameter, or NULL if an error
14250 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14251 true iff the declarator is of the form "(p)". */
14253 static cp_parameter_declarator *
14254 cp_parser_parameter_declaration (cp_parser *parser,
14255 bool template_parm_p,
14256 bool *parenthesized_p)
14258 int declares_class_or_enum;
14259 bool greater_than_is_operator_p;
14260 cp_decl_specifier_seq decl_specifiers;
14261 cp_declarator *declarator;
14262 tree default_argument;
14263 cp_token *token = NULL, *declarator_token_start = NULL;
14264 const char *saved_message;
14266 /* In a template parameter, `>' is not an operator.
14270 When parsing a default template-argument for a non-type
14271 template-parameter, the first non-nested `>' is taken as the end
14272 of the template parameter-list rather than a greater-than
14274 greater_than_is_operator_p = !template_parm_p;
14276 /* Type definitions may not appear in parameter types. */
14277 saved_message = parser->type_definition_forbidden_message;
14278 parser->type_definition_forbidden_message
14279 = "types may not be defined in parameter types";
14281 /* Parse the declaration-specifiers. */
14282 cp_parser_decl_specifier_seq (parser,
14283 CP_PARSER_FLAGS_NONE,
14285 &declares_class_or_enum);
14286 /* If an error occurred, there's no reason to attempt to parse the
14287 rest of the declaration. */
14288 if (cp_parser_error_occurred (parser))
14290 parser->type_definition_forbidden_message = saved_message;
14294 /* Peek at the next token. */
14295 token = cp_lexer_peek_token (parser->lexer);
14297 /* If the next token is a `)', `,', `=', `>', or `...', then there
14298 is no declarator. However, when variadic templates are enabled,
14299 there may be a declarator following `...'. */
14300 if (token->type == CPP_CLOSE_PAREN
14301 || token->type == CPP_COMMA
14302 || token->type == CPP_EQ
14303 || token->type == CPP_GREATER)
14306 if (parenthesized_p)
14307 *parenthesized_p = false;
14309 /* Otherwise, there should be a declarator. */
14312 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14313 parser->default_arg_ok_p = false;
14315 /* After seeing a decl-specifier-seq, if the next token is not a
14316 "(", there is no possibility that the code is a valid
14317 expression. Therefore, if parsing tentatively, we commit at
14319 if (!parser->in_template_argument_list_p
14320 /* In an expression context, having seen:
14324 we cannot be sure whether we are looking at a
14325 function-type (taking a "char" as a parameter) or a cast
14326 of some object of type "char" to "int". */
14327 && !parser->in_type_id_in_expr_p
14328 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14329 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14330 cp_parser_commit_to_tentative_parse (parser);
14331 /* Parse the declarator. */
14332 declarator_token_start = token;
14333 declarator = cp_parser_declarator (parser,
14334 CP_PARSER_DECLARATOR_EITHER,
14335 /*ctor_dtor_or_conv_p=*/NULL,
14337 /*member_p=*/false);
14338 parser->default_arg_ok_p = saved_default_arg_ok_p;
14339 /* After the declarator, allow more attributes. */
14340 decl_specifiers.attributes
14341 = chainon (decl_specifiers.attributes,
14342 cp_parser_attributes_opt (parser));
14345 /* If the next token is an ellipsis, and we have not seen a
14346 declarator name, and the type of the declarator contains parameter
14347 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14348 a parameter pack expansion expression. Otherwise, leave the
14349 ellipsis for a C-style variadic function. */
14350 token = cp_lexer_peek_token (parser->lexer);
14351 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14353 tree type = decl_specifiers.type;
14355 if (type && DECL_P (type))
14356 type = TREE_TYPE (type);
14359 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14360 && declarator_can_be_parameter_pack (declarator)
14361 && (!declarator || !declarator->parameter_pack_p)
14362 && uses_parameter_packs (type))
14364 /* Consume the `...'. */
14365 cp_lexer_consume_token (parser->lexer);
14366 maybe_warn_variadic_templates ();
14368 /* Build a pack expansion type */
14370 declarator->parameter_pack_p = true;
14372 decl_specifiers.type = make_pack_expansion (type);
14376 /* The restriction on defining new types applies only to the type
14377 of the parameter, not to the default argument. */
14378 parser->type_definition_forbidden_message = saved_message;
14380 /* If the next token is `=', then process a default argument. */
14381 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14383 /* Consume the `='. */
14384 cp_lexer_consume_token (parser->lexer);
14386 /* If we are defining a class, then the tokens that make up the
14387 default argument must be saved and processed later. */
14388 if (!template_parm_p && at_class_scope_p ()
14389 && TYPE_BEING_DEFINED (current_class_type))
14391 unsigned depth = 0;
14392 int maybe_template_id = 0;
14393 cp_token *first_token;
14396 /* Add tokens until we have processed the entire default
14397 argument. We add the range [first_token, token). */
14398 first_token = cp_lexer_peek_token (parser->lexer);
14403 /* Peek at the next token. */
14404 token = cp_lexer_peek_token (parser->lexer);
14405 /* What we do depends on what token we have. */
14406 switch (token->type)
14408 /* In valid code, a default argument must be
14409 immediately followed by a `,' `)', or `...'. */
14411 if (depth == 0 && maybe_template_id)
14413 /* If we've seen a '<', we might be in a
14414 template-argument-list. Until Core issue 325 is
14415 resolved, we don't know how this situation ought
14416 to be handled, so try to DTRT. We check whether
14417 what comes after the comma is a valid parameter
14418 declaration list. If it is, then the comma ends
14419 the default argument; otherwise the default
14420 argument continues. */
14421 bool error = false;
14423 /* Set ITALP so cp_parser_parameter_declaration_list
14424 doesn't decide to commit to this parse. */
14425 bool saved_italp = parser->in_template_argument_list_p;
14426 parser->in_template_argument_list_p = true;
14428 cp_parser_parse_tentatively (parser);
14429 cp_lexer_consume_token (parser->lexer);
14430 cp_parser_parameter_declaration_list (parser, &error);
14431 if (!cp_parser_error_occurred (parser) && !error)
14433 cp_parser_abort_tentative_parse (parser);
14435 parser->in_template_argument_list_p = saved_italp;
14438 case CPP_CLOSE_PAREN:
14440 /* If we run into a non-nested `;', `}', or `]',
14441 then the code is invalid -- but the default
14442 argument is certainly over. */
14443 case CPP_SEMICOLON:
14444 case CPP_CLOSE_BRACE:
14445 case CPP_CLOSE_SQUARE:
14448 /* Update DEPTH, if necessary. */
14449 else if (token->type == CPP_CLOSE_PAREN
14450 || token->type == CPP_CLOSE_BRACE
14451 || token->type == CPP_CLOSE_SQUARE)
14455 case CPP_OPEN_PAREN:
14456 case CPP_OPEN_SQUARE:
14457 case CPP_OPEN_BRACE:
14463 /* This might be the comparison operator, or it might
14464 start a template argument list. */
14465 ++maybe_template_id;
14469 if (cxx_dialect == cxx98)
14471 /* Fall through for C++0x, which treats the `>>'
14472 operator like two `>' tokens in certain
14478 /* This might be an operator, or it might close a
14479 template argument list. But if a previous '<'
14480 started a template argument list, this will have
14481 closed it, so we can't be in one anymore. */
14482 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14483 if (maybe_template_id < 0)
14484 maybe_template_id = 0;
14488 /* If we run out of tokens, issue an error message. */
14490 case CPP_PRAGMA_EOL:
14491 error ("%Hfile ends in default argument", &token->location);
14497 /* In these cases, we should look for template-ids.
14498 For example, if the default argument is
14499 `X<int, double>()', we need to do name lookup to
14500 figure out whether or not `X' is a template; if
14501 so, the `,' does not end the default argument.
14503 That is not yet done. */
14510 /* If we've reached the end, stop. */
14514 /* Add the token to the token block. */
14515 token = cp_lexer_consume_token (parser->lexer);
14518 /* Create a DEFAULT_ARG to represent the unparsed default
14520 default_argument = make_node (DEFAULT_ARG);
14521 DEFARG_TOKENS (default_argument)
14522 = cp_token_cache_new (first_token, token);
14523 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14525 /* Outside of a class definition, we can just parse the
14526 assignment-expression. */
14529 token = cp_lexer_peek_token (parser->lexer);
14531 = cp_parser_default_argument (parser, template_parm_p);
14534 if (!parser->default_arg_ok_p)
14536 if (flag_permissive)
14537 warning (0, "deprecated use of default argument for parameter of non-function");
14540 error ("%Hdefault arguments are only "
14541 "permitted for function parameters",
14543 default_argument = NULL_TREE;
14546 else if ((declarator && declarator->parameter_pack_p)
14547 || (decl_specifiers.type
14548 && PACK_EXPANSION_P (decl_specifiers.type)))
14550 const char* kind = template_parm_p? "template " : "";
14552 /* Find the name of the parameter pack. */
14553 cp_declarator *id_declarator = declarator;
14554 while (id_declarator && id_declarator->kind != cdk_id)
14555 id_declarator = id_declarator->declarator;
14557 if (id_declarator && id_declarator->kind == cdk_id)
14558 error ("%H%sparameter pack %qD cannot have a default argument",
14559 &declarator_token_start->location,
14560 kind, id_declarator->u.id.unqualified_name);
14562 error ("%H%sparameter pack cannot have a default argument",
14563 &declarator_token_start->location, kind);
14565 default_argument = NULL_TREE;
14569 default_argument = NULL_TREE;
14571 return make_parameter_declarator (&decl_specifiers,
14576 /* Parse a default argument and return it.
14578 TEMPLATE_PARM_P is true if this is a default argument for a
14579 non-type template parameter. */
14581 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14583 tree default_argument = NULL_TREE;
14584 bool saved_greater_than_is_operator_p;
14585 bool saved_local_variables_forbidden_p;
14587 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14589 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14590 parser->greater_than_is_operator_p = !template_parm_p;
14591 /* Local variable names (and the `this' keyword) may not
14592 appear in a default argument. */
14593 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14594 parser->local_variables_forbidden_p = true;
14595 /* The default argument expression may cause implicitly
14596 defined member functions to be synthesized, which will
14597 result in garbage collection. We must treat this
14598 situation as if we were within the body of function so as
14599 to avoid collecting live data on the stack. */
14601 /* Parse the assignment-expression. */
14602 if (template_parm_p)
14603 push_deferring_access_checks (dk_no_deferred);
14605 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14606 if (template_parm_p)
14607 pop_deferring_access_checks ();
14608 /* Restore saved state. */
14610 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14611 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14613 return default_argument;
14616 /* Parse a function-body.
14619 compound_statement */
14622 cp_parser_function_body (cp_parser *parser)
14624 cp_parser_compound_statement (parser, NULL, false);
14627 /* Parse a ctor-initializer-opt followed by a function-body. Return
14628 true if a ctor-initializer was present. */
14631 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14634 bool ctor_initializer_p;
14636 /* Begin the function body. */
14637 body = begin_function_body ();
14638 /* Parse the optional ctor-initializer. */
14639 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14640 /* Parse the function-body. */
14641 cp_parser_function_body (parser);
14642 /* Finish the function body. */
14643 finish_function_body (body);
14645 return ctor_initializer_p;
14648 /* Parse an initializer.
14651 = initializer-clause
14652 ( expression-list )
14654 Returns an expression representing the initializer. If no
14655 initializer is present, NULL_TREE is returned.
14657 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14658 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14659 set to TRUE if there is no initializer present. If there is an
14660 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14661 is set to true; otherwise it is set to false. */
14664 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14665 bool* non_constant_p)
14670 /* Peek at the next token. */
14671 token = cp_lexer_peek_token (parser->lexer);
14673 /* Let our caller know whether or not this initializer was
14675 *is_direct_init = (token->type != CPP_EQ);
14676 /* Assume that the initializer is constant. */
14677 *non_constant_p = false;
14679 if (token->type == CPP_EQ)
14681 /* Consume the `='. */
14682 cp_lexer_consume_token (parser->lexer);
14683 /* Parse the initializer-clause. */
14684 init = cp_parser_initializer_clause (parser, non_constant_p);
14686 else if (token->type == CPP_OPEN_PAREN)
14689 vec = cp_parser_parenthesized_expression_list (parser, false,
14691 /*allow_expansion_p=*/true,
14694 return error_mark_node;
14695 init = build_tree_list_vec (vec);
14696 release_tree_vector (vec);
14698 else if (token->type == CPP_OPEN_BRACE)
14700 maybe_warn_cpp0x ("extended initializer lists");
14701 init = cp_parser_braced_list (parser, non_constant_p);
14702 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14706 /* Anything else is an error. */
14707 cp_parser_error (parser, "expected initializer");
14708 init = error_mark_node;
14714 /* Parse an initializer-clause.
14716 initializer-clause:
14717 assignment-expression
14720 Returns an expression representing the initializer.
14722 If the `assignment-expression' production is used the value
14723 returned is simply a representation for the expression.
14725 Otherwise, calls cp_parser_braced_list. */
14728 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14732 /* Assume the expression is constant. */
14733 *non_constant_p = false;
14735 /* If it is not a `{', then we are looking at an
14736 assignment-expression. */
14737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14740 = cp_parser_constant_expression (parser,
14741 /*allow_non_constant_p=*/true,
14743 if (!*non_constant_p)
14744 initializer = fold_non_dependent_expr (initializer);
14747 initializer = cp_parser_braced_list (parser, non_constant_p);
14749 return initializer;
14752 /* Parse a brace-enclosed initializer list.
14755 { initializer-list , [opt] }
14758 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14759 the elements of the initializer-list (or NULL, if the last
14760 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14761 NULL_TREE. There is no way to detect whether or not the optional
14762 trailing `,' was provided. NON_CONSTANT_P is as for
14763 cp_parser_initializer. */
14766 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14770 /* Consume the `{' token. */
14771 cp_lexer_consume_token (parser->lexer);
14772 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14773 initializer = make_node (CONSTRUCTOR);
14774 /* If it's not a `}', then there is a non-trivial initializer. */
14775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14777 /* Parse the initializer list. */
14778 CONSTRUCTOR_ELTS (initializer)
14779 = cp_parser_initializer_list (parser, non_constant_p);
14780 /* A trailing `,' token is allowed. */
14781 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14782 cp_lexer_consume_token (parser->lexer);
14784 /* Now, there should be a trailing `}'. */
14785 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14786 TREE_TYPE (initializer) = init_list_type_node;
14787 return initializer;
14790 /* Parse an initializer-list.
14793 initializer-clause ... [opt]
14794 initializer-list , initializer-clause ... [opt]
14799 identifier : initializer-clause
14800 initializer-list, identifier : initializer-clause
14802 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14803 for the initializer. If the INDEX of the elt is non-NULL, it is the
14804 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14805 as for cp_parser_initializer. */
14807 static VEC(constructor_elt,gc) *
14808 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14810 VEC(constructor_elt,gc) *v = NULL;
14812 /* Assume all of the expressions are constant. */
14813 *non_constant_p = false;
14815 /* Parse the rest of the list. */
14821 bool clause_non_constant_p;
14823 /* If the next token is an identifier and the following one is a
14824 colon, we are looking at the GNU designated-initializer
14826 if (cp_parser_allow_gnu_extensions_p (parser)
14827 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14828 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14830 /* Warn the user that they are using an extension. */
14831 pedwarn (input_location, OPT_pedantic,
14832 "ISO C++ does not allow designated initializers");
14833 /* Consume the identifier. */
14834 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14835 /* Consume the `:'. */
14836 cp_lexer_consume_token (parser->lexer);
14839 identifier = NULL_TREE;
14841 /* Parse the initializer. */
14842 initializer = cp_parser_initializer_clause (parser,
14843 &clause_non_constant_p);
14844 /* If any clause is non-constant, so is the entire initializer. */
14845 if (clause_non_constant_p)
14846 *non_constant_p = true;
14848 /* If we have an ellipsis, this is an initializer pack
14850 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14852 /* Consume the `...'. */
14853 cp_lexer_consume_token (parser->lexer);
14855 /* Turn the initializer into an initializer expansion. */
14856 initializer = make_pack_expansion (initializer);
14859 /* Add it to the vector. */
14860 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14862 /* If the next token is not a comma, we have reached the end of
14864 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14867 /* Peek at the next token. */
14868 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14869 /* If the next token is a `}', then we're still done. An
14870 initializer-clause can have a trailing `,' after the
14871 initializer-list and before the closing `}'. */
14872 if (token->type == CPP_CLOSE_BRACE)
14875 /* Consume the `,' token. */
14876 cp_lexer_consume_token (parser->lexer);
14882 /* Classes [gram.class] */
14884 /* Parse a class-name.
14890 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14891 to indicate that names looked up in dependent types should be
14892 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14893 keyword has been used to indicate that the name that appears next
14894 is a template. TAG_TYPE indicates the explicit tag given before
14895 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14896 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14897 is the class being defined in a class-head.
14899 Returns the TYPE_DECL representing the class. */
14902 cp_parser_class_name (cp_parser *parser,
14903 bool typename_keyword_p,
14904 bool template_keyword_p,
14905 enum tag_types tag_type,
14906 bool check_dependency_p,
14908 bool is_declaration)
14914 tree identifier = NULL_TREE;
14916 /* All class-names start with an identifier. */
14917 token = cp_lexer_peek_token (parser->lexer);
14918 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14920 cp_parser_error (parser, "expected class-name");
14921 return error_mark_node;
14924 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14925 to a template-id, so we save it here. */
14926 scope = parser->scope;
14927 if (scope == error_mark_node)
14928 return error_mark_node;
14930 /* Any name names a type if we're following the `typename' keyword
14931 in a qualified name where the enclosing scope is type-dependent. */
14932 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14933 && dependent_type_p (scope));
14934 /* Handle the common case (an identifier, but not a template-id)
14936 if (token->type == CPP_NAME
14937 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14939 cp_token *identifier_token;
14942 /* Look for the identifier. */
14943 identifier_token = cp_lexer_peek_token (parser->lexer);
14944 ambiguous_p = identifier_token->ambiguous_p;
14945 identifier = cp_parser_identifier (parser);
14946 /* If the next token isn't an identifier, we are certainly not
14947 looking at a class-name. */
14948 if (identifier == error_mark_node)
14949 decl = error_mark_node;
14950 /* If we know this is a type-name, there's no need to look it
14952 else if (typename_p)
14956 tree ambiguous_decls;
14957 /* If we already know that this lookup is ambiguous, then
14958 we've already issued an error message; there's no reason
14962 cp_parser_simulate_error (parser);
14963 return error_mark_node;
14965 /* If the next token is a `::', then the name must be a type
14968 [basic.lookup.qual]
14970 During the lookup for a name preceding the :: scope
14971 resolution operator, object, function, and enumerator
14972 names are ignored. */
14973 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14974 tag_type = typename_type;
14975 /* Look up the name. */
14976 decl = cp_parser_lookup_name (parser, identifier,
14978 /*is_template=*/false,
14979 /*is_namespace=*/false,
14980 check_dependency_p,
14982 identifier_token->location);
14983 if (ambiguous_decls)
14985 error ("%Hreference to %qD is ambiguous",
14986 &identifier_token->location, identifier);
14987 print_candidates (ambiguous_decls);
14988 if (cp_parser_parsing_tentatively (parser))
14990 identifier_token->ambiguous_p = true;
14991 cp_parser_simulate_error (parser);
14993 return error_mark_node;
14999 /* Try a template-id. */
15000 decl = cp_parser_template_id (parser, template_keyword_p,
15001 check_dependency_p,
15003 if (decl == error_mark_node)
15004 return error_mark_node;
15007 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15009 /* If this is a typename, create a TYPENAME_TYPE. */
15010 if (typename_p && decl != error_mark_node)
15012 decl = make_typename_type (scope, decl, typename_type,
15013 /*complain=*/tf_error);
15014 if (decl != error_mark_node)
15015 decl = TYPE_NAME (decl);
15018 /* Check to see that it is really the name of a class. */
15019 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15020 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15021 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15022 /* Situations like this:
15024 template <typename T> struct A {
15025 typename T::template X<int>::I i;
15028 are problematic. Is `T::template X<int>' a class-name? The
15029 standard does not seem to be definitive, but there is no other
15030 valid interpretation of the following `::'. Therefore, those
15031 names are considered class-names. */
15033 decl = make_typename_type (scope, decl, tag_type, tf_error);
15034 if (decl != error_mark_node)
15035 decl = TYPE_NAME (decl);
15037 else if (TREE_CODE (decl) != TYPE_DECL
15038 || TREE_TYPE (decl) == error_mark_node
15039 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15040 decl = error_mark_node;
15042 if (decl == error_mark_node)
15043 cp_parser_error (parser, "expected class-name");
15044 else if (identifier && !parser->scope)
15045 maybe_note_name_used_in_class (identifier, decl);
15050 /* Parse a class-specifier.
15053 class-head { member-specification [opt] }
15055 Returns the TREE_TYPE representing the class. */
15058 cp_parser_class_specifier (cp_parser* parser)
15061 tree attributes = NULL_TREE;
15062 bool nested_name_specifier_p;
15063 unsigned saved_num_template_parameter_lists;
15064 bool saved_in_function_body;
15065 bool saved_in_unbraced_linkage_specification_p;
15066 tree old_scope = NULL_TREE;
15067 tree scope = NULL_TREE;
15070 push_deferring_access_checks (dk_no_deferred);
15072 /* Parse the class-head. */
15073 type = cp_parser_class_head (parser,
15074 &nested_name_specifier_p,
15077 /* If the class-head was a semantic disaster, skip the entire body
15081 cp_parser_skip_to_end_of_block_or_statement (parser);
15082 pop_deferring_access_checks ();
15083 return error_mark_node;
15086 /* Look for the `{'. */
15087 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15089 pop_deferring_access_checks ();
15090 return error_mark_node;
15093 /* Process the base classes. If they're invalid, skip the
15094 entire class body. */
15095 if (!xref_basetypes (type, bases))
15097 /* Consuming the closing brace yields better error messages
15099 if (cp_parser_skip_to_closing_brace (parser))
15100 cp_lexer_consume_token (parser->lexer);
15101 pop_deferring_access_checks ();
15102 return error_mark_node;
15105 /* Issue an error message if type-definitions are forbidden here. */
15106 cp_parser_check_type_definition (parser);
15107 /* Remember that we are defining one more class. */
15108 ++parser->num_classes_being_defined;
15109 /* Inside the class, surrounding template-parameter-lists do not
15111 saved_num_template_parameter_lists
15112 = parser->num_template_parameter_lists;
15113 parser->num_template_parameter_lists = 0;
15114 /* We are not in a function body. */
15115 saved_in_function_body = parser->in_function_body;
15116 parser->in_function_body = false;
15117 /* We are not immediately inside an extern "lang" block. */
15118 saved_in_unbraced_linkage_specification_p
15119 = parser->in_unbraced_linkage_specification_p;
15120 parser->in_unbraced_linkage_specification_p = false;
15122 /* Start the class. */
15123 if (nested_name_specifier_p)
15125 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15126 old_scope = push_inner_scope (scope);
15128 type = begin_class_definition (type, attributes);
15130 if (type == error_mark_node)
15131 /* If the type is erroneous, skip the entire body of the class. */
15132 cp_parser_skip_to_closing_brace (parser);
15134 /* Parse the member-specification. */
15135 cp_parser_member_specification_opt (parser);
15137 /* Look for the trailing `}'. */
15138 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15139 /* Look for trailing attributes to apply to this class. */
15140 if (cp_parser_allow_gnu_extensions_p (parser))
15141 attributes = cp_parser_attributes_opt (parser);
15142 if (type != error_mark_node)
15143 type = finish_struct (type, attributes);
15144 if (nested_name_specifier_p)
15145 pop_inner_scope (old_scope, scope);
15146 /* If this class is not itself within the scope of another class,
15147 then we need to parse the bodies of all of the queued function
15148 definitions. Note that the queued functions defined in a class
15149 are not always processed immediately following the
15150 class-specifier for that class. Consider:
15153 struct B { void f() { sizeof (A); } };
15156 If `f' were processed before the processing of `A' were
15157 completed, there would be no way to compute the size of `A'.
15158 Note that the nesting we are interested in here is lexical --
15159 not the semantic nesting given by TYPE_CONTEXT. In particular,
15162 struct A { struct B; };
15163 struct A::B { void f() { } };
15165 there is no need to delay the parsing of `A::B::f'. */
15166 if (--parser->num_classes_being_defined == 0)
15170 tree class_type = NULL_TREE;
15171 tree pushed_scope = NULL_TREE;
15173 /* In a first pass, parse default arguments to the functions.
15174 Then, in a second pass, parse the bodies of the functions.
15175 This two-phased approach handles cases like:
15183 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15184 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15185 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15186 TREE_PURPOSE (parser->unparsed_functions_queues)
15187 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15189 fn = TREE_VALUE (queue_entry);
15190 /* If there are default arguments that have not yet been processed,
15191 take care of them now. */
15192 if (class_type != TREE_PURPOSE (queue_entry))
15195 pop_scope (pushed_scope);
15196 class_type = TREE_PURPOSE (queue_entry);
15197 pushed_scope = push_scope (class_type);
15199 /* Make sure that any template parameters are in scope. */
15200 maybe_begin_member_template_processing (fn);
15201 /* Parse the default argument expressions. */
15202 cp_parser_late_parsing_default_args (parser, fn);
15203 /* Remove any template parameters from the symbol table. */
15204 maybe_end_member_template_processing ();
15207 pop_scope (pushed_scope);
15208 /* Now parse the body of the functions. */
15209 for (TREE_VALUE (parser->unparsed_functions_queues)
15210 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15211 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15212 TREE_VALUE (parser->unparsed_functions_queues)
15213 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15215 /* Figure out which function we need to process. */
15216 fn = TREE_VALUE (queue_entry);
15217 /* Parse the function. */
15218 cp_parser_late_parsing_for_member (parser, fn);
15222 /* Put back any saved access checks. */
15223 pop_deferring_access_checks ();
15225 /* Restore saved state. */
15226 parser->in_function_body = saved_in_function_body;
15227 parser->num_template_parameter_lists
15228 = saved_num_template_parameter_lists;
15229 parser->in_unbraced_linkage_specification_p
15230 = saved_in_unbraced_linkage_specification_p;
15235 /* Parse a class-head.
15238 class-key identifier [opt] base-clause [opt]
15239 class-key nested-name-specifier identifier base-clause [opt]
15240 class-key nested-name-specifier [opt] template-id
15244 class-key attributes identifier [opt] base-clause [opt]
15245 class-key attributes nested-name-specifier identifier base-clause [opt]
15246 class-key attributes nested-name-specifier [opt] template-id
15249 Upon return BASES is initialized to the list of base classes (or
15250 NULL, if there are none) in the same form returned by
15251 cp_parser_base_clause.
15253 Returns the TYPE of the indicated class. Sets
15254 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15255 involving a nested-name-specifier was used, and FALSE otherwise.
15257 Returns error_mark_node if this is not a class-head.
15259 Returns NULL_TREE if the class-head is syntactically valid, but
15260 semantically invalid in a way that means we should skip the entire
15261 body of the class. */
15264 cp_parser_class_head (cp_parser* parser,
15265 bool* nested_name_specifier_p,
15266 tree *attributes_p,
15269 tree nested_name_specifier;
15270 enum tag_types class_key;
15271 tree id = NULL_TREE;
15272 tree type = NULL_TREE;
15274 bool template_id_p = false;
15275 bool qualified_p = false;
15276 bool invalid_nested_name_p = false;
15277 bool invalid_explicit_specialization_p = false;
15278 tree pushed_scope = NULL_TREE;
15279 unsigned num_templates;
15280 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15281 /* Assume no nested-name-specifier will be present. */
15282 *nested_name_specifier_p = false;
15283 /* Assume no template parameter lists will be used in defining the
15287 *bases = NULL_TREE;
15289 /* Look for the class-key. */
15290 class_key = cp_parser_class_key (parser);
15291 if (class_key == none_type)
15292 return error_mark_node;
15294 /* Parse the attributes. */
15295 attributes = cp_parser_attributes_opt (parser);
15297 /* If the next token is `::', that is invalid -- but sometimes
15298 people do try to write:
15302 Handle this gracefully by accepting the extra qualifier, and then
15303 issuing an error about it later if this really is a
15304 class-head. If it turns out just to be an elaborated type
15305 specifier, remain silent. */
15306 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15307 qualified_p = true;
15309 push_deferring_access_checks (dk_no_check);
15311 /* Determine the name of the class. Begin by looking for an
15312 optional nested-name-specifier. */
15313 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15314 nested_name_specifier
15315 = cp_parser_nested_name_specifier_opt (parser,
15316 /*typename_keyword_p=*/false,
15317 /*check_dependency_p=*/false,
15319 /*is_declaration=*/false);
15320 /* If there was a nested-name-specifier, then there *must* be an
15322 if (nested_name_specifier)
15324 type_start_token = cp_lexer_peek_token (parser->lexer);
15325 /* Although the grammar says `identifier', it really means
15326 `class-name' or `template-name'. You are only allowed to
15327 define a class that has already been declared with this
15330 The proposed resolution for Core Issue 180 says that wherever
15331 you see `class T::X' you should treat `X' as a type-name.
15333 It is OK to define an inaccessible class; for example:
15335 class A { class B; };
15338 We do not know if we will see a class-name, or a
15339 template-name. We look for a class-name first, in case the
15340 class-name is a template-id; if we looked for the
15341 template-name first we would stop after the template-name. */
15342 cp_parser_parse_tentatively (parser);
15343 type = cp_parser_class_name (parser,
15344 /*typename_keyword_p=*/false,
15345 /*template_keyword_p=*/false,
15347 /*check_dependency_p=*/false,
15348 /*class_head_p=*/true,
15349 /*is_declaration=*/false);
15350 /* If that didn't work, ignore the nested-name-specifier. */
15351 if (!cp_parser_parse_definitely (parser))
15353 invalid_nested_name_p = true;
15354 type_start_token = cp_lexer_peek_token (parser->lexer);
15355 id = cp_parser_identifier (parser);
15356 if (id == error_mark_node)
15359 /* If we could not find a corresponding TYPE, treat this
15360 declaration like an unqualified declaration. */
15361 if (type == error_mark_node)
15362 nested_name_specifier = NULL_TREE;
15363 /* Otherwise, count the number of templates used in TYPE and its
15364 containing scopes. */
15369 for (scope = TREE_TYPE (type);
15370 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15371 scope = (TYPE_P (scope)
15372 ? TYPE_CONTEXT (scope)
15373 : DECL_CONTEXT (scope)))
15375 && CLASS_TYPE_P (scope)
15376 && CLASSTYPE_TEMPLATE_INFO (scope)
15377 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15378 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15382 /* Otherwise, the identifier is optional. */
15385 /* We don't know whether what comes next is a template-id,
15386 an identifier, or nothing at all. */
15387 cp_parser_parse_tentatively (parser);
15388 /* Check for a template-id. */
15389 type_start_token = cp_lexer_peek_token (parser->lexer);
15390 id = cp_parser_template_id (parser,
15391 /*template_keyword_p=*/false,
15392 /*check_dependency_p=*/true,
15393 /*is_declaration=*/true);
15394 /* If that didn't work, it could still be an identifier. */
15395 if (!cp_parser_parse_definitely (parser))
15397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15399 type_start_token = cp_lexer_peek_token (parser->lexer);
15400 id = cp_parser_identifier (parser);
15407 template_id_p = true;
15412 pop_deferring_access_checks ();
15415 cp_parser_check_for_invalid_template_id (parser, id,
15416 type_start_token->location);
15418 /* If it's not a `:' or a `{' then we can't really be looking at a
15419 class-head, since a class-head only appears as part of a
15420 class-specifier. We have to detect this situation before calling
15421 xref_tag, since that has irreversible side-effects. */
15422 if (!cp_parser_next_token_starts_class_definition_p (parser))
15424 cp_parser_error (parser, "expected %<{%> or %<:%>");
15425 return error_mark_node;
15428 /* At this point, we're going ahead with the class-specifier, even
15429 if some other problem occurs. */
15430 cp_parser_commit_to_tentative_parse (parser);
15431 /* Issue the error about the overly-qualified name now. */
15434 cp_parser_error (parser,
15435 "global qualification of class name is invalid");
15436 return error_mark_node;
15438 else if (invalid_nested_name_p)
15440 cp_parser_error (parser,
15441 "qualified name does not name a class");
15442 return error_mark_node;
15444 else if (nested_name_specifier)
15448 /* Reject typedef-names in class heads. */
15449 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15451 error ("%Hinvalid class name in declaration of %qD",
15452 &type_start_token->location, type);
15457 /* Figure out in what scope the declaration is being placed. */
15458 scope = current_scope ();
15459 /* If that scope does not contain the scope in which the
15460 class was originally declared, the program is invalid. */
15461 if (scope && !is_ancestor (scope, nested_name_specifier))
15463 if (at_namespace_scope_p ())
15464 error ("%Hdeclaration of %qD in namespace %qD which does not "
15466 &type_start_token->location,
15467 type, scope, nested_name_specifier);
15469 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15470 &type_start_token->location,
15471 type, scope, nested_name_specifier);
15477 A declarator-id shall not be qualified except for the
15478 definition of a ... nested class outside of its class
15479 ... [or] the definition or explicit instantiation of a
15480 class member of a namespace outside of its namespace. */
15481 if (scope == nested_name_specifier)
15483 permerror (input_location, "%Hextra qualification not allowed",
15484 &nested_name_specifier_token_start->location);
15485 nested_name_specifier = NULL_TREE;
15489 /* An explicit-specialization must be preceded by "template <>". If
15490 it is not, try to recover gracefully. */
15491 if (at_namespace_scope_p ()
15492 && parser->num_template_parameter_lists == 0
15495 error ("%Han explicit specialization must be preceded by %<template <>%>",
15496 &type_start_token->location);
15497 invalid_explicit_specialization_p = true;
15498 /* Take the same action that would have been taken by
15499 cp_parser_explicit_specialization. */
15500 ++parser->num_template_parameter_lists;
15501 begin_specialization ();
15503 /* There must be no "return" statements between this point and the
15504 end of this function; set "type "to the correct return value and
15505 use "goto done;" to return. */
15506 /* Make sure that the right number of template parameters were
15508 if (!cp_parser_check_template_parameters (parser, num_templates,
15509 type_start_token->location,
15510 /*declarator=*/NULL))
15512 /* If something went wrong, there is no point in even trying to
15513 process the class-definition. */
15518 /* Look up the type. */
15521 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15522 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15523 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15525 error ("%Hfunction template %qD redeclared as a class template",
15526 &type_start_token->location, id);
15527 type = error_mark_node;
15531 type = TREE_TYPE (id);
15532 type = maybe_process_partial_specialization (type);
15534 if (nested_name_specifier)
15535 pushed_scope = push_scope (nested_name_specifier);
15537 else if (nested_name_specifier)
15543 template <typename T> struct S { struct T };
15544 template <typename T> struct S<T>::T { };
15546 we will get a TYPENAME_TYPE when processing the definition of
15547 `S::T'. We need to resolve it to the actual type before we
15548 try to define it. */
15549 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15551 class_type = resolve_typename_type (TREE_TYPE (type),
15552 /*only_current_p=*/false);
15553 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15554 type = TYPE_NAME (class_type);
15557 cp_parser_error (parser, "could not resolve typename type");
15558 type = error_mark_node;
15562 if (maybe_process_partial_specialization (TREE_TYPE (type))
15563 == error_mark_node)
15569 class_type = current_class_type;
15570 /* Enter the scope indicated by the nested-name-specifier. */
15571 pushed_scope = push_scope (nested_name_specifier);
15572 /* Get the canonical version of this type. */
15573 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15574 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15575 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15577 type = push_template_decl (type);
15578 if (type == error_mark_node)
15585 type = TREE_TYPE (type);
15586 *nested_name_specifier_p = true;
15588 else /* The name is not a nested name. */
15590 /* If the class was unnamed, create a dummy name. */
15592 id = make_anon_name ();
15593 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15594 parser->num_template_parameter_lists);
15597 /* Indicate whether this class was declared as a `class' or as a
15599 if (TREE_CODE (type) == RECORD_TYPE)
15600 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15601 cp_parser_check_class_key (class_key, type);
15603 /* If this type was already complete, and we see another definition,
15604 that's an error. */
15605 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15607 error ("%Hredefinition of %q#T",
15608 &type_start_token->location, type);
15609 error ("%Hprevious definition of %q+#T",
15610 &type_start_token->location, type);
15614 else if (type == error_mark_node)
15617 /* We will have entered the scope containing the class; the names of
15618 base classes should be looked up in that context. For example:
15620 struct A { struct B {}; struct C; };
15621 struct A::C : B {};
15625 /* Get the list of base-classes, if there is one. */
15626 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15627 *bases = cp_parser_base_clause (parser);
15630 /* Leave the scope given by the nested-name-specifier. We will
15631 enter the class scope itself while processing the members. */
15633 pop_scope (pushed_scope);
15635 if (invalid_explicit_specialization_p)
15637 end_specialization ();
15638 --parser->num_template_parameter_lists;
15640 *attributes_p = attributes;
15644 /* Parse a class-key.
15651 Returns the kind of class-key specified, or none_type to indicate
15654 static enum tag_types
15655 cp_parser_class_key (cp_parser* parser)
15658 enum tag_types tag_type;
15660 /* Look for the class-key. */
15661 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15665 /* Check to see if the TOKEN is a class-key. */
15666 tag_type = cp_parser_token_is_class_key (token);
15668 cp_parser_error (parser, "expected class-key");
15672 /* Parse an (optional) member-specification.
15674 member-specification:
15675 member-declaration member-specification [opt]
15676 access-specifier : member-specification [opt] */
15679 cp_parser_member_specification_opt (cp_parser* parser)
15686 /* Peek at the next token. */
15687 token = cp_lexer_peek_token (parser->lexer);
15688 /* If it's a `}', or EOF then we've seen all the members. */
15689 if (token->type == CPP_CLOSE_BRACE
15690 || token->type == CPP_EOF
15691 || token->type == CPP_PRAGMA_EOL)
15694 /* See if this token is a keyword. */
15695 keyword = token->keyword;
15699 case RID_PROTECTED:
15701 /* Consume the access-specifier. */
15702 cp_lexer_consume_token (parser->lexer);
15703 /* Remember which access-specifier is active. */
15704 current_access_specifier = token->u.value;
15705 /* Look for the `:'. */
15706 cp_parser_require (parser, CPP_COLON, "%<:%>");
15710 /* Accept #pragmas at class scope. */
15711 if (token->type == CPP_PRAGMA)
15713 cp_parser_pragma (parser, pragma_external);
15717 /* Otherwise, the next construction must be a
15718 member-declaration. */
15719 cp_parser_member_declaration (parser);
15724 /* Parse a member-declaration.
15726 member-declaration:
15727 decl-specifier-seq [opt] member-declarator-list [opt] ;
15728 function-definition ; [opt]
15729 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15731 template-declaration
15733 member-declarator-list:
15735 member-declarator-list , member-declarator
15738 declarator pure-specifier [opt]
15739 declarator constant-initializer [opt]
15740 identifier [opt] : constant-expression
15744 member-declaration:
15745 __extension__ member-declaration
15748 declarator attributes [opt] pure-specifier [opt]
15749 declarator attributes [opt] constant-initializer [opt]
15750 identifier [opt] attributes [opt] : constant-expression
15754 member-declaration:
15755 static_assert-declaration */
15758 cp_parser_member_declaration (cp_parser* parser)
15760 cp_decl_specifier_seq decl_specifiers;
15761 tree prefix_attributes;
15763 int declares_class_or_enum;
15765 cp_token *token = NULL;
15766 cp_token *decl_spec_token_start = NULL;
15767 cp_token *initializer_token_start = NULL;
15768 int saved_pedantic;
15770 /* Check for the `__extension__' keyword. */
15771 if (cp_parser_extension_opt (parser, &saved_pedantic))
15774 cp_parser_member_declaration (parser);
15775 /* Restore the old value of the PEDANTIC flag. */
15776 pedantic = saved_pedantic;
15781 /* Check for a template-declaration. */
15782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15784 /* An explicit specialization here is an error condition, and we
15785 expect the specialization handler to detect and report this. */
15786 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15787 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15788 cp_parser_explicit_specialization (parser);
15790 cp_parser_template_declaration (parser, /*member_p=*/true);
15795 /* Check for a using-declaration. */
15796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15798 /* Parse the using-declaration. */
15799 cp_parser_using_declaration (parser,
15800 /*access_declaration_p=*/false);
15804 /* Check for @defs. */
15805 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15808 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15809 ivar = ivar_chains;
15813 ivar = TREE_CHAIN (member);
15814 TREE_CHAIN (member) = NULL_TREE;
15815 finish_member_declaration (member);
15820 /* If the next token is `static_assert' we have a static assertion. */
15821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15823 cp_parser_static_assert (parser, /*member_p=*/true);
15827 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15830 /* Parse the decl-specifier-seq. */
15831 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15832 cp_parser_decl_specifier_seq (parser,
15833 CP_PARSER_FLAGS_OPTIONAL,
15835 &declares_class_or_enum);
15836 prefix_attributes = decl_specifiers.attributes;
15837 decl_specifiers.attributes = NULL_TREE;
15838 /* Check for an invalid type-name. */
15839 if (!decl_specifiers.type
15840 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15842 /* If there is no declarator, then the decl-specifier-seq should
15844 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15846 /* If there was no decl-specifier-seq, and the next token is a
15847 `;', then we have something like:
15853 Each member-declaration shall declare at least one member
15854 name of the class. */
15855 if (!decl_specifiers.any_specifiers_p)
15857 cp_token *token = cp_lexer_peek_token (parser->lexer);
15858 if (!in_system_header_at (token->location))
15859 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15865 /* See if this declaration is a friend. */
15866 friend_p = cp_parser_friend_p (&decl_specifiers);
15867 /* If there were decl-specifiers, check to see if there was
15868 a class-declaration. */
15869 type = check_tag_decl (&decl_specifiers);
15870 /* Nested classes have already been added to the class, but
15871 a `friend' needs to be explicitly registered. */
15874 /* If the `friend' keyword was present, the friend must
15875 be introduced with a class-key. */
15876 if (!declares_class_or_enum)
15877 error ("%Ha class-key must be used when declaring a friend",
15878 &decl_spec_token_start->location);
15881 template <typename T> struct A {
15882 friend struct A<T>::B;
15885 A<T>::B will be represented by a TYPENAME_TYPE, and
15886 therefore not recognized by check_tag_decl. */
15888 && decl_specifiers.type
15889 && TYPE_P (decl_specifiers.type))
15890 type = decl_specifiers.type;
15891 if (!type || !TYPE_P (type))
15892 error ("%Hfriend declaration does not name a class or "
15893 "function", &decl_spec_token_start->location);
15895 make_friend_class (current_class_type, type,
15896 /*complain=*/true);
15898 /* If there is no TYPE, an error message will already have
15900 else if (!type || type == error_mark_node)
15902 /* An anonymous aggregate has to be handled specially; such
15903 a declaration really declares a data member (with a
15904 particular type), as opposed to a nested class. */
15905 else if (ANON_AGGR_TYPE_P (type))
15907 /* Remove constructors and such from TYPE, now that we
15908 know it is an anonymous aggregate. */
15909 fixup_anonymous_aggr (type);
15910 /* And make the corresponding data member. */
15911 decl = build_decl (decl_spec_token_start->location,
15912 FIELD_DECL, NULL_TREE, type);
15913 /* Add it to the class. */
15914 finish_member_declaration (decl);
15917 cp_parser_check_access_in_redeclaration
15919 decl_spec_token_start->location);
15924 /* See if these declarations will be friends. */
15925 friend_p = cp_parser_friend_p (&decl_specifiers);
15927 /* Keep going until we hit the `;' at the end of the
15929 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15931 tree attributes = NULL_TREE;
15932 tree first_attribute;
15934 /* Peek at the next token. */
15935 token = cp_lexer_peek_token (parser->lexer);
15937 /* Check for a bitfield declaration. */
15938 if (token->type == CPP_COLON
15939 || (token->type == CPP_NAME
15940 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15946 /* Get the name of the bitfield. Note that we cannot just
15947 check TOKEN here because it may have been invalidated by
15948 the call to cp_lexer_peek_nth_token above. */
15949 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15950 identifier = cp_parser_identifier (parser);
15952 identifier = NULL_TREE;
15954 /* Consume the `:' token. */
15955 cp_lexer_consume_token (parser->lexer);
15956 /* Get the width of the bitfield. */
15958 = cp_parser_constant_expression (parser,
15959 /*allow_non_constant=*/false,
15962 /* Look for attributes that apply to the bitfield. */
15963 attributes = cp_parser_attributes_opt (parser);
15964 /* Remember which attributes are prefix attributes and
15966 first_attribute = attributes;
15967 /* Combine the attributes. */
15968 attributes = chainon (prefix_attributes, attributes);
15970 /* Create the bitfield declaration. */
15971 decl = grokbitfield (identifier
15972 ? make_id_declarator (NULL_TREE,
15982 cp_declarator *declarator;
15984 tree asm_specification;
15985 int ctor_dtor_or_conv_p;
15987 /* Parse the declarator. */
15989 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15990 &ctor_dtor_or_conv_p,
15991 /*parenthesized_p=*/NULL,
15992 /*member_p=*/true);
15994 /* If something went wrong parsing the declarator, make sure
15995 that we at least consume some tokens. */
15996 if (declarator == cp_error_declarator)
15998 /* Skip to the end of the statement. */
15999 cp_parser_skip_to_end_of_statement (parser);
16000 /* If the next token is not a semicolon, that is
16001 probably because we just skipped over the body of
16002 a function. So, we consume a semicolon if
16003 present, but do not issue an error message if it
16005 if (cp_lexer_next_token_is (parser->lexer,
16007 cp_lexer_consume_token (parser->lexer);
16011 if (declares_class_or_enum & 2)
16012 cp_parser_check_for_definition_in_return_type
16013 (declarator, decl_specifiers.type,
16014 decl_specifiers.type_location);
16016 /* Look for an asm-specification. */
16017 asm_specification = cp_parser_asm_specification_opt (parser);
16018 /* Look for attributes that apply to the declaration. */
16019 attributes = cp_parser_attributes_opt (parser);
16020 /* Remember which attributes are prefix attributes and
16022 first_attribute = attributes;
16023 /* Combine the attributes. */
16024 attributes = chainon (prefix_attributes, attributes);
16026 /* If it's an `=', then we have a constant-initializer or a
16027 pure-specifier. It is not correct to parse the
16028 initializer before registering the member declaration
16029 since the member declaration should be in scope while
16030 its initializer is processed. However, the rest of the
16031 front end does not yet provide an interface that allows
16032 us to handle this correctly. */
16033 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16037 A pure-specifier shall be used only in the declaration of
16038 a virtual function.
16040 A member-declarator can contain a constant-initializer
16041 only if it declares a static member of integral or
16044 Therefore, if the DECLARATOR is for a function, we look
16045 for a pure-specifier; otherwise, we look for a
16046 constant-initializer. When we call `grokfield', it will
16047 perform more stringent semantics checks. */
16048 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16049 if (function_declarator_p (declarator))
16050 initializer = cp_parser_pure_specifier (parser);
16052 /* Parse the initializer. */
16053 initializer = cp_parser_constant_initializer (parser);
16055 /* Otherwise, there is no initializer. */
16057 initializer = NULL_TREE;
16059 /* See if we are probably looking at a function
16060 definition. We are certainly not looking at a
16061 member-declarator. Calling `grokfield' has
16062 side-effects, so we must not do it unless we are sure
16063 that we are looking at a member-declarator. */
16064 if (cp_parser_token_starts_function_definition_p
16065 (cp_lexer_peek_token (parser->lexer)))
16067 /* The grammar does not allow a pure-specifier to be
16068 used when a member function is defined. (It is
16069 possible that this fact is an oversight in the
16070 standard, since a pure function may be defined
16071 outside of the class-specifier. */
16073 error ("%Hpure-specifier on function-definition",
16074 &initializer_token_start->location);
16075 decl = cp_parser_save_member_function_body (parser,
16079 /* If the member was not a friend, declare it here. */
16081 finish_member_declaration (decl);
16082 /* Peek at the next token. */
16083 token = cp_lexer_peek_token (parser->lexer);
16084 /* If the next token is a semicolon, consume it. */
16085 if (token->type == CPP_SEMICOLON)
16086 cp_lexer_consume_token (parser->lexer);
16090 if (declarator->kind == cdk_function)
16091 declarator->id_loc = token->location;
16092 /* Create the declaration. */
16093 decl = grokfield (declarator, &decl_specifiers,
16094 initializer, /*init_const_expr_p=*/true,
16099 /* Reset PREFIX_ATTRIBUTES. */
16100 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16101 attributes = TREE_CHAIN (attributes);
16103 TREE_CHAIN (attributes) = NULL_TREE;
16105 /* If there is any qualification still in effect, clear it
16106 now; we will be starting fresh with the next declarator. */
16107 parser->scope = NULL_TREE;
16108 parser->qualifying_scope = NULL_TREE;
16109 parser->object_scope = NULL_TREE;
16110 /* If it's a `,', then there are more declarators. */
16111 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16112 cp_lexer_consume_token (parser->lexer);
16113 /* If the next token isn't a `;', then we have a parse error. */
16114 else if (cp_lexer_next_token_is_not (parser->lexer,
16117 cp_parser_error (parser, "expected %<;%>");
16118 /* Skip tokens until we find a `;'. */
16119 cp_parser_skip_to_end_of_statement (parser);
16126 /* Add DECL to the list of members. */
16128 finish_member_declaration (decl);
16130 if (TREE_CODE (decl) == FUNCTION_DECL)
16131 cp_parser_save_default_args (parser, decl);
16136 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16139 /* Parse a pure-specifier.
16144 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16145 Otherwise, ERROR_MARK_NODE is returned. */
16148 cp_parser_pure_specifier (cp_parser* parser)
16152 /* Look for the `=' token. */
16153 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16154 return error_mark_node;
16155 /* Look for the `0' token. */
16156 token = cp_lexer_peek_token (parser->lexer);
16158 if (token->type == CPP_EOF
16159 || token->type == CPP_PRAGMA_EOL)
16160 return error_mark_node;
16162 cp_lexer_consume_token (parser->lexer);
16164 /* Accept = default or = delete in c++0x mode. */
16165 if (token->keyword == RID_DEFAULT
16166 || token->keyword == RID_DELETE)
16168 maybe_warn_cpp0x ("defaulted and deleted functions");
16169 return token->u.value;
16172 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16173 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16175 cp_parser_error (parser,
16176 "invalid pure specifier (only %<= 0%> is allowed)");
16177 cp_parser_skip_to_end_of_statement (parser);
16178 return error_mark_node;
16180 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16182 error ("%Htemplates may not be %<virtual%>", &token->location);
16183 return error_mark_node;
16186 return integer_zero_node;
16189 /* Parse a constant-initializer.
16191 constant-initializer:
16192 = constant-expression
16194 Returns a representation of the constant-expression. */
16197 cp_parser_constant_initializer (cp_parser* parser)
16199 /* Look for the `=' token. */
16200 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16201 return error_mark_node;
16203 /* It is invalid to write:
16205 struct S { static const int i = { 7 }; };
16208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16210 cp_parser_error (parser,
16211 "a brace-enclosed initializer is not allowed here");
16212 /* Consume the opening brace. */
16213 cp_lexer_consume_token (parser->lexer);
16214 /* Skip the initializer. */
16215 cp_parser_skip_to_closing_brace (parser);
16216 /* Look for the trailing `}'. */
16217 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16219 return error_mark_node;
16222 return cp_parser_constant_expression (parser,
16223 /*allow_non_constant=*/false,
16227 /* Derived classes [gram.class.derived] */
16229 /* Parse a base-clause.
16232 : base-specifier-list
16234 base-specifier-list:
16235 base-specifier ... [opt]
16236 base-specifier-list , base-specifier ... [opt]
16238 Returns a TREE_LIST representing the base-classes, in the order in
16239 which they were declared. The representation of each node is as
16240 described by cp_parser_base_specifier.
16242 In the case that no bases are specified, this function will return
16243 NULL_TREE, not ERROR_MARK_NODE. */
16246 cp_parser_base_clause (cp_parser* parser)
16248 tree bases = NULL_TREE;
16250 /* Look for the `:' that begins the list. */
16251 cp_parser_require (parser, CPP_COLON, "%<:%>");
16253 /* Scan the base-specifier-list. */
16258 bool pack_expansion_p = false;
16260 /* Look for the base-specifier. */
16261 base = cp_parser_base_specifier (parser);
16262 /* Look for the (optional) ellipsis. */
16263 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16265 /* Consume the `...'. */
16266 cp_lexer_consume_token (parser->lexer);
16268 pack_expansion_p = true;
16271 /* Add BASE to the front of the list. */
16272 if (base != error_mark_node)
16274 if (pack_expansion_p)
16275 /* Make this a pack expansion type. */
16276 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16279 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16281 TREE_CHAIN (base) = bases;
16285 /* Peek at the next token. */
16286 token = cp_lexer_peek_token (parser->lexer);
16287 /* If it's not a comma, then the list is complete. */
16288 if (token->type != CPP_COMMA)
16290 /* Consume the `,'. */
16291 cp_lexer_consume_token (parser->lexer);
16294 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16295 base class had a qualified name. However, the next name that
16296 appears is certainly not qualified. */
16297 parser->scope = NULL_TREE;
16298 parser->qualifying_scope = NULL_TREE;
16299 parser->object_scope = NULL_TREE;
16301 return nreverse (bases);
16304 /* Parse a base-specifier.
16307 :: [opt] nested-name-specifier [opt] class-name
16308 virtual access-specifier [opt] :: [opt] nested-name-specifier
16310 access-specifier virtual [opt] :: [opt] nested-name-specifier
16313 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16314 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16315 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16316 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16319 cp_parser_base_specifier (cp_parser* parser)
16323 bool virtual_p = false;
16324 bool duplicate_virtual_error_issued_p = false;
16325 bool duplicate_access_error_issued_p = false;
16326 bool class_scope_p, template_p;
16327 tree access = access_default_node;
16330 /* Process the optional `virtual' and `access-specifier'. */
16333 /* Peek at the next token. */
16334 token = cp_lexer_peek_token (parser->lexer);
16335 /* Process `virtual'. */
16336 switch (token->keyword)
16339 /* If `virtual' appears more than once, issue an error. */
16340 if (virtual_p && !duplicate_virtual_error_issued_p)
16342 cp_parser_error (parser,
16343 "%<virtual%> specified more than once in base-specified");
16344 duplicate_virtual_error_issued_p = true;
16349 /* Consume the `virtual' token. */
16350 cp_lexer_consume_token (parser->lexer);
16355 case RID_PROTECTED:
16357 /* If more than one access specifier appears, issue an
16359 if (access != access_default_node
16360 && !duplicate_access_error_issued_p)
16362 cp_parser_error (parser,
16363 "more than one access specifier in base-specified");
16364 duplicate_access_error_issued_p = true;
16367 access = ridpointers[(int) token->keyword];
16369 /* Consume the access-specifier. */
16370 cp_lexer_consume_token (parser->lexer);
16379 /* It is not uncommon to see programs mechanically, erroneously, use
16380 the 'typename' keyword to denote (dependent) qualified types
16381 as base classes. */
16382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16384 token = cp_lexer_peek_token (parser->lexer);
16385 if (!processing_template_decl)
16386 error ("%Hkeyword %<typename%> not allowed outside of templates",
16389 error ("%Hkeyword %<typename%> not allowed in this context "
16390 "(the base class is implicitly a type)",
16392 cp_lexer_consume_token (parser->lexer);
16395 /* Look for the optional `::' operator. */
16396 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16397 /* Look for the nested-name-specifier. The simplest way to
16402 The keyword `typename' is not permitted in a base-specifier or
16403 mem-initializer; in these contexts a qualified name that
16404 depends on a template-parameter is implicitly assumed to be a
16407 is to pretend that we have seen the `typename' keyword at this
16409 cp_parser_nested_name_specifier_opt (parser,
16410 /*typename_keyword_p=*/true,
16411 /*check_dependency_p=*/true,
16413 /*is_declaration=*/true);
16414 /* If the base class is given by a qualified name, assume that names
16415 we see are type names or templates, as appropriate. */
16416 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16417 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16419 /* Finally, look for the class-name. */
16420 type = cp_parser_class_name (parser,
16424 /*check_dependency_p=*/true,
16425 /*class_head_p=*/false,
16426 /*is_declaration=*/true);
16428 if (type == error_mark_node)
16429 return error_mark_node;
16431 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16434 /* Exception handling [gram.exception] */
16436 /* Parse an (optional) exception-specification.
16438 exception-specification:
16439 throw ( type-id-list [opt] )
16441 Returns a TREE_LIST representing the exception-specification. The
16442 TREE_VALUE of each node is a type. */
16445 cp_parser_exception_specification_opt (cp_parser* parser)
16450 /* Peek at the next token. */
16451 token = cp_lexer_peek_token (parser->lexer);
16452 /* If it's not `throw', then there's no exception-specification. */
16453 if (!cp_parser_is_keyword (token, RID_THROW))
16456 /* Consume the `throw'. */
16457 cp_lexer_consume_token (parser->lexer);
16459 /* Look for the `('. */
16460 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16462 /* Peek at the next token. */
16463 token = cp_lexer_peek_token (parser->lexer);
16464 /* If it's not a `)', then there is a type-id-list. */
16465 if (token->type != CPP_CLOSE_PAREN)
16467 const char *saved_message;
16469 /* Types may not be defined in an exception-specification. */
16470 saved_message = parser->type_definition_forbidden_message;
16471 parser->type_definition_forbidden_message
16472 = "types may not be defined in an exception-specification";
16473 /* Parse the type-id-list. */
16474 type_id_list = cp_parser_type_id_list (parser);
16475 /* Restore the saved message. */
16476 parser->type_definition_forbidden_message = saved_message;
16479 type_id_list = empty_except_spec;
16481 /* Look for the `)'. */
16482 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16484 return type_id_list;
16487 /* Parse an (optional) type-id-list.
16491 type-id-list , type-id ... [opt]
16493 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16494 in the order that the types were presented. */
16497 cp_parser_type_id_list (cp_parser* parser)
16499 tree types = NULL_TREE;
16506 /* Get the next type-id. */
16507 type = cp_parser_type_id (parser);
16508 /* Parse the optional ellipsis. */
16509 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16511 /* Consume the `...'. */
16512 cp_lexer_consume_token (parser->lexer);
16514 /* Turn the type into a pack expansion expression. */
16515 type = make_pack_expansion (type);
16517 /* Add it to the list. */
16518 types = add_exception_specifier (types, type, /*complain=*/1);
16519 /* Peek at the next token. */
16520 token = cp_lexer_peek_token (parser->lexer);
16521 /* If it is not a `,', we are done. */
16522 if (token->type != CPP_COMMA)
16524 /* Consume the `,'. */
16525 cp_lexer_consume_token (parser->lexer);
16528 return nreverse (types);
16531 /* Parse a try-block.
16534 try compound-statement handler-seq */
16537 cp_parser_try_block (cp_parser* parser)
16541 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16542 try_block = begin_try_block ();
16543 cp_parser_compound_statement (parser, NULL, true);
16544 finish_try_block (try_block);
16545 cp_parser_handler_seq (parser);
16546 finish_handler_sequence (try_block);
16551 /* Parse a function-try-block.
16553 function-try-block:
16554 try ctor-initializer [opt] function-body handler-seq */
16557 cp_parser_function_try_block (cp_parser* parser)
16559 tree compound_stmt;
16561 bool ctor_initializer_p;
16563 /* Look for the `try' keyword. */
16564 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16566 /* Let the rest of the front end know where we are. */
16567 try_block = begin_function_try_block (&compound_stmt);
16568 /* Parse the function-body. */
16570 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16571 /* We're done with the `try' part. */
16572 finish_function_try_block (try_block);
16573 /* Parse the handlers. */
16574 cp_parser_handler_seq (parser);
16575 /* We're done with the handlers. */
16576 finish_function_handler_sequence (try_block, compound_stmt);
16578 return ctor_initializer_p;
16581 /* Parse a handler-seq.
16584 handler handler-seq [opt] */
16587 cp_parser_handler_seq (cp_parser* parser)
16593 /* Parse the handler. */
16594 cp_parser_handler (parser);
16595 /* Peek at the next token. */
16596 token = cp_lexer_peek_token (parser->lexer);
16597 /* If it's not `catch' then there are no more handlers. */
16598 if (!cp_parser_is_keyword (token, RID_CATCH))
16603 /* Parse a handler.
16606 catch ( exception-declaration ) compound-statement */
16609 cp_parser_handler (cp_parser* parser)
16614 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16615 handler = begin_handler ();
16616 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16617 declaration = cp_parser_exception_declaration (parser);
16618 finish_handler_parms (declaration, handler);
16619 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16620 cp_parser_compound_statement (parser, NULL, false);
16621 finish_handler (handler);
16624 /* Parse an exception-declaration.
16626 exception-declaration:
16627 type-specifier-seq declarator
16628 type-specifier-seq abstract-declarator
16632 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16633 ellipsis variant is used. */
16636 cp_parser_exception_declaration (cp_parser* parser)
16638 cp_decl_specifier_seq type_specifiers;
16639 cp_declarator *declarator;
16640 const char *saved_message;
16642 /* If it's an ellipsis, it's easy to handle. */
16643 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16645 /* Consume the `...' token. */
16646 cp_lexer_consume_token (parser->lexer);
16650 /* Types may not be defined in exception-declarations. */
16651 saved_message = parser->type_definition_forbidden_message;
16652 parser->type_definition_forbidden_message
16653 = "types may not be defined in exception-declarations";
16655 /* Parse the type-specifier-seq. */
16656 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16658 /* If it's a `)', then there is no declarator. */
16659 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16662 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16663 /*ctor_dtor_or_conv_p=*/NULL,
16664 /*parenthesized_p=*/NULL,
16665 /*member_p=*/false);
16667 /* Restore the saved message. */
16668 parser->type_definition_forbidden_message = saved_message;
16670 if (!type_specifiers.any_specifiers_p)
16671 return error_mark_node;
16673 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16676 /* Parse a throw-expression.
16679 throw assignment-expression [opt]
16681 Returns a THROW_EXPR representing the throw-expression. */
16684 cp_parser_throw_expression (cp_parser* parser)
16689 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16690 token = cp_lexer_peek_token (parser->lexer);
16691 /* Figure out whether or not there is an assignment-expression
16692 following the "throw" keyword. */
16693 if (token->type == CPP_COMMA
16694 || token->type == CPP_SEMICOLON
16695 || token->type == CPP_CLOSE_PAREN
16696 || token->type == CPP_CLOSE_SQUARE
16697 || token->type == CPP_CLOSE_BRACE
16698 || token->type == CPP_COLON)
16699 expression = NULL_TREE;
16701 expression = cp_parser_assignment_expression (parser,
16702 /*cast_p=*/false, NULL);
16704 return build_throw (expression);
16707 /* GNU Extensions */
16709 /* Parse an (optional) asm-specification.
16712 asm ( string-literal )
16714 If the asm-specification is present, returns a STRING_CST
16715 corresponding to the string-literal. Otherwise, returns
16719 cp_parser_asm_specification_opt (cp_parser* parser)
16722 tree asm_specification;
16724 /* Peek at the next token. */
16725 token = cp_lexer_peek_token (parser->lexer);
16726 /* If the next token isn't the `asm' keyword, then there's no
16727 asm-specification. */
16728 if (!cp_parser_is_keyword (token, RID_ASM))
16731 /* Consume the `asm' token. */
16732 cp_lexer_consume_token (parser->lexer);
16733 /* Look for the `('. */
16734 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16736 /* Look for the string-literal. */
16737 asm_specification = cp_parser_string_literal (parser, false, false);
16739 /* Look for the `)'. */
16740 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16742 return asm_specification;
16745 /* Parse an asm-operand-list.
16749 asm-operand-list , asm-operand
16752 string-literal ( expression )
16753 [ string-literal ] string-literal ( expression )
16755 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16756 each node is the expression. The TREE_PURPOSE is itself a
16757 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16758 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16759 is a STRING_CST for the string literal before the parenthesis. Returns
16760 ERROR_MARK_NODE if any of the operands are invalid. */
16763 cp_parser_asm_operand_list (cp_parser* parser)
16765 tree asm_operands = NULL_TREE;
16766 bool invalid_operands = false;
16770 tree string_literal;
16774 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16776 /* Consume the `[' token. */
16777 cp_lexer_consume_token (parser->lexer);
16778 /* Read the operand name. */
16779 name = cp_parser_identifier (parser);
16780 if (name != error_mark_node)
16781 name = build_string (IDENTIFIER_LENGTH (name),
16782 IDENTIFIER_POINTER (name));
16783 /* Look for the closing `]'. */
16784 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16788 /* Look for the string-literal. */
16789 string_literal = cp_parser_string_literal (parser, false, false);
16791 /* Look for the `('. */
16792 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16793 /* Parse the expression. */
16794 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16795 /* Look for the `)'. */
16796 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16798 if (name == error_mark_node
16799 || string_literal == error_mark_node
16800 || expression == error_mark_node)
16801 invalid_operands = true;
16803 /* Add this operand to the list. */
16804 asm_operands = tree_cons (build_tree_list (name, string_literal),
16807 /* If the next token is not a `,', there are no more
16809 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16811 /* Consume the `,'. */
16812 cp_lexer_consume_token (parser->lexer);
16815 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16818 /* Parse an asm-clobber-list.
16822 asm-clobber-list , string-literal
16824 Returns a TREE_LIST, indicating the clobbers in the order that they
16825 appeared. The TREE_VALUE of each node is a STRING_CST. */
16828 cp_parser_asm_clobber_list (cp_parser* parser)
16830 tree clobbers = NULL_TREE;
16834 tree string_literal;
16836 /* Look for the string literal. */
16837 string_literal = cp_parser_string_literal (parser, false, false);
16838 /* Add it to the list. */
16839 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16840 /* If the next token is not a `,', then the list is
16842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16844 /* Consume the `,' token. */
16845 cp_lexer_consume_token (parser->lexer);
16851 /* Parse an (optional) series of attributes.
16854 attributes attribute
16857 __attribute__ (( attribute-list [opt] ))
16859 The return value is as for cp_parser_attribute_list. */
16862 cp_parser_attributes_opt (cp_parser* parser)
16864 tree attributes = NULL_TREE;
16869 tree attribute_list;
16871 /* Peek at the next token. */
16872 token = cp_lexer_peek_token (parser->lexer);
16873 /* If it's not `__attribute__', then we're done. */
16874 if (token->keyword != RID_ATTRIBUTE)
16877 /* Consume the `__attribute__' keyword. */
16878 cp_lexer_consume_token (parser->lexer);
16879 /* Look for the two `(' tokens. */
16880 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16881 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16883 /* Peek at the next token. */
16884 token = cp_lexer_peek_token (parser->lexer);
16885 if (token->type != CPP_CLOSE_PAREN)
16886 /* Parse the attribute-list. */
16887 attribute_list = cp_parser_attribute_list (parser);
16889 /* If the next token is a `)', then there is no attribute
16891 attribute_list = NULL;
16893 /* Look for the two `)' tokens. */
16894 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16895 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16897 /* Add these new attributes to the list. */
16898 attributes = chainon (attributes, attribute_list);
16904 /* Parse an attribute-list.
16908 attribute-list , attribute
16912 identifier ( identifier )
16913 identifier ( identifier , expression-list )
16914 identifier ( expression-list )
16916 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16917 to an attribute. The TREE_PURPOSE of each node is the identifier
16918 indicating which attribute is in use. The TREE_VALUE represents
16919 the arguments, if any. */
16922 cp_parser_attribute_list (cp_parser* parser)
16924 tree attribute_list = NULL_TREE;
16925 bool save_translate_strings_p = parser->translate_strings_p;
16927 parser->translate_strings_p = false;
16934 /* Look for the identifier. We also allow keywords here; for
16935 example `__attribute__ ((const))' is legal. */
16936 token = cp_lexer_peek_token (parser->lexer);
16937 if (token->type == CPP_NAME
16938 || token->type == CPP_KEYWORD)
16940 tree arguments = NULL_TREE;
16942 /* Consume the token. */
16943 token = cp_lexer_consume_token (parser->lexer);
16945 /* Save away the identifier that indicates which attribute
16947 identifier = (token->type == CPP_KEYWORD)
16948 /* For keywords, use the canonical spelling, not the
16949 parsed identifier. */
16950 ? ridpointers[(int) token->keyword]
16953 attribute = build_tree_list (identifier, NULL_TREE);
16955 /* Peek at the next token. */
16956 token = cp_lexer_peek_token (parser->lexer);
16957 /* If it's an `(', then parse the attribute arguments. */
16958 if (token->type == CPP_OPEN_PAREN)
16961 vec = cp_parser_parenthesized_expression_list
16962 (parser, true, /*cast_p=*/false,
16963 /*allow_expansion_p=*/false,
16964 /*non_constant_p=*/NULL);
16966 arguments = error_mark_node;
16969 arguments = build_tree_list_vec (vec);
16970 release_tree_vector (vec);
16972 /* Save the arguments away. */
16973 TREE_VALUE (attribute) = arguments;
16976 if (arguments != error_mark_node)
16978 /* Add this attribute to the list. */
16979 TREE_CHAIN (attribute) = attribute_list;
16980 attribute_list = attribute;
16983 token = cp_lexer_peek_token (parser->lexer);
16985 /* Now, look for more attributes. If the next token isn't a
16986 `,', we're done. */
16987 if (token->type != CPP_COMMA)
16990 /* Consume the comma and keep going. */
16991 cp_lexer_consume_token (parser->lexer);
16993 parser->translate_strings_p = save_translate_strings_p;
16995 /* We built up the list in reverse order. */
16996 return nreverse (attribute_list);
16999 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
17000 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
17001 current value of the PEDANTIC flag, regardless of whether or not
17002 the `__extension__' keyword is present. The caller is responsible
17003 for restoring the value of the PEDANTIC flag. */
17006 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17008 /* Save the old value of the PEDANTIC flag. */
17009 *saved_pedantic = pedantic;
17011 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17013 /* Consume the `__extension__' token. */
17014 cp_lexer_consume_token (parser->lexer);
17015 /* We're not being pedantic while the `__extension__' keyword is
17025 /* Parse a label declaration.
17028 __label__ label-declarator-seq ;
17030 label-declarator-seq:
17031 identifier , label-declarator-seq
17035 cp_parser_label_declaration (cp_parser* parser)
17037 /* Look for the `__label__' keyword. */
17038 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17044 /* Look for an identifier. */
17045 identifier = cp_parser_identifier (parser);
17046 /* If we failed, stop. */
17047 if (identifier == error_mark_node)
17049 /* Declare it as a label. */
17050 finish_label_decl (identifier);
17051 /* If the next token is a `;', stop. */
17052 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17054 /* Look for the `,' separating the label declarations. */
17055 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17058 /* Look for the final `;'. */
17059 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17062 /* Support Functions */
17064 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17065 NAME should have one of the representations used for an
17066 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17067 is returned. If PARSER->SCOPE is a dependent type, then a
17068 SCOPE_REF is returned.
17070 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17071 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17072 was formed. Abstractly, such entities should not be passed to this
17073 function, because they do not need to be looked up, but it is
17074 simpler to check for this special case here, rather than at the
17077 In cases not explicitly covered above, this function returns a
17078 DECL, OVERLOAD, or baselink representing the result of the lookup.
17079 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17082 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17083 (e.g., "struct") that was used. In that case bindings that do not
17084 refer to types are ignored.
17086 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17089 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17092 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17095 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17096 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17097 NULL_TREE otherwise. */
17100 cp_parser_lookup_name (cp_parser *parser, tree name,
17101 enum tag_types tag_type,
17104 bool check_dependency,
17105 tree *ambiguous_decls,
17106 location_t name_location)
17110 tree object_type = parser->context->object_type;
17112 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17113 flags |= LOOKUP_COMPLAIN;
17115 /* Assume that the lookup will be unambiguous. */
17116 if (ambiguous_decls)
17117 *ambiguous_decls = NULL_TREE;
17119 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17120 no longer valid. Note that if we are parsing tentatively, and
17121 the parse fails, OBJECT_TYPE will be automatically restored. */
17122 parser->context->object_type = NULL_TREE;
17124 if (name == error_mark_node)
17125 return error_mark_node;
17127 /* A template-id has already been resolved; there is no lookup to
17129 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17131 if (BASELINK_P (name))
17133 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17134 == TEMPLATE_ID_EXPR);
17138 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17139 it should already have been checked to make sure that the name
17140 used matches the type being destroyed. */
17141 if (TREE_CODE (name) == BIT_NOT_EXPR)
17145 /* Figure out to which type this destructor applies. */
17147 type = parser->scope;
17148 else if (object_type)
17149 type = object_type;
17151 type = current_class_type;
17152 /* If that's not a class type, there is no destructor. */
17153 if (!type || !CLASS_TYPE_P (type))
17154 return error_mark_node;
17155 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17156 lazily_declare_fn (sfk_destructor, type);
17157 if (!CLASSTYPE_DESTRUCTORS (type))
17158 return error_mark_node;
17159 /* If it was a class type, return the destructor. */
17160 return CLASSTYPE_DESTRUCTORS (type);
17163 /* By this point, the NAME should be an ordinary identifier. If
17164 the id-expression was a qualified name, the qualifying scope is
17165 stored in PARSER->SCOPE at this point. */
17166 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17168 /* Perform the lookup. */
17173 if (parser->scope == error_mark_node)
17174 return error_mark_node;
17176 /* If the SCOPE is dependent, the lookup must be deferred until
17177 the template is instantiated -- unless we are explicitly
17178 looking up names in uninstantiated templates. Even then, we
17179 cannot look up the name if the scope is not a class type; it
17180 might, for example, be a template type parameter. */
17181 dependent_p = (TYPE_P (parser->scope)
17182 && dependent_scope_p (parser->scope));
17183 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17185 /* Defer lookup. */
17186 decl = error_mark_node;
17189 tree pushed_scope = NULL_TREE;
17191 /* If PARSER->SCOPE is a dependent type, then it must be a
17192 class type, and we must not be checking dependencies;
17193 otherwise, we would have processed this lookup above. So
17194 that PARSER->SCOPE is not considered a dependent base by
17195 lookup_member, we must enter the scope here. */
17197 pushed_scope = push_scope (parser->scope);
17198 /* If the PARSER->SCOPE is a template specialization, it
17199 may be instantiated during name lookup. In that case,
17200 errors may be issued. Even if we rollback the current
17201 tentative parse, those errors are valid. */
17202 decl = lookup_qualified_name (parser->scope, name,
17203 tag_type != none_type,
17204 /*complain=*/true);
17206 /* If we have a single function from a using decl, pull it out. */
17207 if (TREE_CODE (decl) == OVERLOAD
17208 && !really_overloaded_fn (decl))
17209 decl = OVL_FUNCTION (decl);
17212 pop_scope (pushed_scope);
17215 /* If the scope is a dependent type and either we deferred lookup or
17216 we did lookup but didn't find the name, rememeber the name. */
17217 if (decl == error_mark_node && TYPE_P (parser->scope)
17218 && dependent_type_p (parser->scope))
17224 /* The resolution to Core Issue 180 says that `struct
17225 A::B' should be considered a type-name, even if `A'
17227 type = make_typename_type (parser->scope, name, tag_type,
17228 /*complain=*/tf_error);
17229 decl = TYPE_NAME (type);
17231 else if (is_template
17232 && (cp_parser_next_token_ends_template_argument_p (parser)
17233 || cp_lexer_next_token_is (parser->lexer,
17235 decl = make_unbound_class_template (parser->scope,
17237 /*complain=*/tf_error);
17239 decl = build_qualified_name (/*type=*/NULL_TREE,
17240 parser->scope, name,
17243 parser->qualifying_scope = parser->scope;
17244 parser->object_scope = NULL_TREE;
17246 else if (object_type)
17248 tree object_decl = NULL_TREE;
17249 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17250 OBJECT_TYPE is not a class. */
17251 if (CLASS_TYPE_P (object_type))
17252 /* If the OBJECT_TYPE is a template specialization, it may
17253 be instantiated during name lookup. In that case, errors
17254 may be issued. Even if we rollback the current tentative
17255 parse, those errors are valid. */
17256 object_decl = lookup_member (object_type,
17259 tag_type != none_type);
17260 /* Look it up in the enclosing context, too. */
17261 decl = lookup_name_real (name, tag_type != none_type,
17263 /*block_p=*/true, is_namespace, flags);
17264 parser->object_scope = object_type;
17265 parser->qualifying_scope = NULL_TREE;
17267 decl = object_decl;
17271 decl = lookup_name_real (name, tag_type != none_type,
17273 /*block_p=*/true, is_namespace, flags);
17274 parser->qualifying_scope = NULL_TREE;
17275 parser->object_scope = NULL_TREE;
17278 /* If the lookup failed, let our caller know. */
17279 if (!decl || decl == error_mark_node)
17280 return error_mark_node;
17282 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17283 if (TREE_CODE (decl) == TREE_LIST)
17285 if (ambiguous_decls)
17286 *ambiguous_decls = decl;
17287 /* The error message we have to print is too complicated for
17288 cp_parser_error, so we incorporate its actions directly. */
17289 if (!cp_parser_simulate_error (parser))
17291 error ("%Hreference to %qD is ambiguous",
17292 &name_location, name);
17293 print_candidates (decl);
17295 return error_mark_node;
17298 gcc_assert (DECL_P (decl)
17299 || TREE_CODE (decl) == OVERLOAD
17300 || TREE_CODE (decl) == SCOPE_REF
17301 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17302 || BASELINK_P (decl));
17304 /* If we have resolved the name of a member declaration, check to
17305 see if the declaration is accessible. When the name resolves to
17306 set of overloaded functions, accessibility is checked when
17307 overload resolution is done.
17309 During an explicit instantiation, access is not checked at all,
17310 as per [temp.explicit]. */
17312 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17317 /* Like cp_parser_lookup_name, but for use in the typical case where
17318 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17319 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17322 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17324 return cp_parser_lookup_name (parser, name,
17326 /*is_template=*/false,
17327 /*is_namespace=*/false,
17328 /*check_dependency=*/true,
17329 /*ambiguous_decls=*/NULL,
17333 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17334 the current context, return the TYPE_DECL. If TAG_NAME_P is
17335 true, the DECL indicates the class being defined in a class-head,
17336 or declared in an elaborated-type-specifier.
17338 Otherwise, return DECL. */
17341 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17343 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17344 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17347 template <typename T> struct B;
17350 template <typename T> struct A::B {};
17352 Similarly, in an elaborated-type-specifier:
17354 namespace N { struct X{}; }
17357 template <typename T> friend struct N::X;
17360 However, if the DECL refers to a class type, and we are in
17361 the scope of the class, then the name lookup automatically
17362 finds the TYPE_DECL created by build_self_reference rather
17363 than a TEMPLATE_DECL. For example, in:
17365 template <class T> struct S {
17369 there is no need to handle such case. */
17371 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17372 return DECL_TEMPLATE_RESULT (decl);
17377 /* If too many, or too few, template-parameter lists apply to the
17378 declarator, issue an error message. Returns TRUE if all went well,
17379 and FALSE otherwise. */
17382 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17383 cp_declarator *declarator,
17384 location_t declarator_location)
17386 unsigned num_templates;
17388 /* We haven't seen any classes that involve template parameters yet. */
17391 switch (declarator->kind)
17394 if (declarator->u.id.qualifying_scope)
17399 scope = declarator->u.id.qualifying_scope;
17400 member = declarator->u.id.unqualified_name;
17402 while (scope && CLASS_TYPE_P (scope))
17404 /* You're supposed to have one `template <...>'
17405 for every template class, but you don't need one
17406 for a full specialization. For example:
17408 template <class T> struct S{};
17409 template <> struct S<int> { void f(); };
17410 void S<int>::f () {}
17412 is correct; there shouldn't be a `template <>' for
17413 the definition of `S<int>::f'. */
17414 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17415 /* If SCOPE does not have template information of any
17416 kind, then it is not a template, nor is it nested
17417 within a template. */
17419 if (explicit_class_specialization_p (scope))
17421 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17424 scope = TYPE_CONTEXT (scope);
17427 else if (TREE_CODE (declarator->u.id.unqualified_name)
17428 == TEMPLATE_ID_EXPR)
17429 /* If the DECLARATOR has the form `X<y>' then it uses one
17430 additional level of template parameters. */
17433 return cp_parser_check_template_parameters
17434 (parser, num_templates, declarator_location, declarator);
17440 case cdk_reference:
17442 return (cp_parser_check_declarator_template_parameters
17443 (parser, declarator->declarator, declarator_location));
17449 gcc_unreachable ();
17454 /* NUM_TEMPLATES were used in the current declaration. If that is
17455 invalid, return FALSE and issue an error messages. Otherwise,
17456 return TRUE. If DECLARATOR is non-NULL, then we are checking a
17457 declarator and we can print more accurate diagnostics. */
17460 cp_parser_check_template_parameters (cp_parser* parser,
17461 unsigned num_templates,
17462 location_t location,
17463 cp_declarator *declarator)
17465 /* If there are the same number of template classes and parameter
17466 lists, that's OK. */
17467 if (parser->num_template_parameter_lists == num_templates)
17469 /* If there are more, but only one more, then we are referring to a
17470 member template. That's OK too. */
17471 if (parser->num_template_parameter_lists == num_templates + 1)
17473 /* If there are more template classes than parameter lists, we have
17476 template <class T> void S<T>::R<T>::f (); */
17477 if (parser->num_template_parameter_lists < num_templates)
17480 error_at (location, "specializing member %<%T::%E%> "
17481 "requires %<template<>%> syntax",
17482 declarator->u.id.qualifying_scope,
17483 declarator->u.id.unqualified_name);
17485 error_at (location, "too few template-parameter-lists");
17488 /* Otherwise, there are too many template parameter lists. We have
17491 template <class T> template <class U> void S::f(); */
17492 error ("%Htoo many template-parameter-lists", &location);
17496 /* Parse an optional `::' token indicating that the following name is
17497 from the global namespace. If so, PARSER->SCOPE is set to the
17498 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17499 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17500 Returns the new value of PARSER->SCOPE, if the `::' token is
17501 present, and NULL_TREE otherwise. */
17504 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17508 /* Peek at the next token. */
17509 token = cp_lexer_peek_token (parser->lexer);
17510 /* If we're looking at a `::' token then we're starting from the
17511 global namespace, not our current location. */
17512 if (token->type == CPP_SCOPE)
17514 /* Consume the `::' token. */
17515 cp_lexer_consume_token (parser->lexer);
17516 /* Set the SCOPE so that we know where to start the lookup. */
17517 parser->scope = global_namespace;
17518 parser->qualifying_scope = global_namespace;
17519 parser->object_scope = NULL_TREE;
17521 return parser->scope;
17523 else if (!current_scope_valid_p)
17525 parser->scope = NULL_TREE;
17526 parser->qualifying_scope = NULL_TREE;
17527 parser->object_scope = NULL_TREE;
17533 /* Returns TRUE if the upcoming token sequence is the start of a
17534 constructor declarator. If FRIEND_P is true, the declarator is
17535 preceded by the `friend' specifier. */
17538 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17540 bool constructor_p;
17541 tree type_decl = NULL_TREE;
17542 bool nested_name_p;
17543 cp_token *next_token;
17545 /* The common case is that this is not a constructor declarator, so
17546 try to avoid doing lots of work if at all possible. It's not
17547 valid declare a constructor at function scope. */
17548 if (parser->in_function_body)
17550 /* And only certain tokens can begin a constructor declarator. */
17551 next_token = cp_lexer_peek_token (parser->lexer);
17552 if (next_token->type != CPP_NAME
17553 && next_token->type != CPP_SCOPE
17554 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17555 && next_token->type != CPP_TEMPLATE_ID)
17558 /* Parse tentatively; we are going to roll back all of the tokens
17560 cp_parser_parse_tentatively (parser);
17561 /* Assume that we are looking at a constructor declarator. */
17562 constructor_p = true;
17564 /* Look for the optional `::' operator. */
17565 cp_parser_global_scope_opt (parser,
17566 /*current_scope_valid_p=*/false);
17567 /* Look for the nested-name-specifier. */
17569 = (cp_parser_nested_name_specifier_opt (parser,
17570 /*typename_keyword_p=*/false,
17571 /*check_dependency_p=*/false,
17573 /*is_declaration=*/false)
17575 /* Outside of a class-specifier, there must be a
17576 nested-name-specifier. */
17577 if (!nested_name_p &&
17578 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17580 constructor_p = false;
17581 /* If we still think that this might be a constructor-declarator,
17582 look for a class-name. */
17587 template <typename T> struct S { S(); };
17588 template <typename T> S<T>::S ();
17590 we must recognize that the nested `S' names a class.
17593 template <typename T> S<T>::S<T> ();
17595 we must recognize that the nested `S' names a template. */
17596 type_decl = cp_parser_class_name (parser,
17597 /*typename_keyword_p=*/false,
17598 /*template_keyword_p=*/false,
17600 /*check_dependency_p=*/false,
17601 /*class_head_p=*/false,
17602 /*is_declaration=*/false);
17603 /* If there was no class-name, then this is not a constructor. */
17604 constructor_p = !cp_parser_error_occurred (parser);
17607 /* If we're still considering a constructor, we have to see a `(',
17608 to begin the parameter-declaration-clause, followed by either a
17609 `)', an `...', or a decl-specifier. We need to check for a
17610 type-specifier to avoid being fooled into thinking that:
17614 is a constructor. (It is actually a function named `f' that
17615 takes one parameter (of type `int') and returns a value of type
17618 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17621 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17622 /* A parameter declaration begins with a decl-specifier,
17623 which is either the "attribute" keyword, a storage class
17624 specifier, or (usually) a type-specifier. */
17625 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17628 tree pushed_scope = NULL_TREE;
17629 unsigned saved_num_template_parameter_lists;
17631 /* Names appearing in the type-specifier should be looked up
17632 in the scope of the class. */
17633 if (current_class_type)
17637 type = TREE_TYPE (type_decl);
17638 if (TREE_CODE (type) == TYPENAME_TYPE)
17640 type = resolve_typename_type (type,
17641 /*only_current_p=*/false);
17642 if (TREE_CODE (type) == TYPENAME_TYPE)
17644 cp_parser_abort_tentative_parse (parser);
17648 pushed_scope = push_scope (type);
17651 /* Inside the constructor parameter list, surrounding
17652 template-parameter-lists do not apply. */
17653 saved_num_template_parameter_lists
17654 = parser->num_template_parameter_lists;
17655 parser->num_template_parameter_lists = 0;
17657 /* Look for the type-specifier. */
17658 cp_parser_type_specifier (parser,
17659 CP_PARSER_FLAGS_NONE,
17660 /*decl_specs=*/NULL,
17661 /*is_declarator=*/true,
17662 /*declares_class_or_enum=*/NULL,
17663 /*is_cv_qualifier=*/NULL);
17665 parser->num_template_parameter_lists
17666 = saved_num_template_parameter_lists;
17668 /* Leave the scope of the class. */
17670 pop_scope (pushed_scope);
17672 constructor_p = !cp_parser_error_occurred (parser);
17676 constructor_p = false;
17677 /* We did not really want to consume any tokens. */
17678 cp_parser_abort_tentative_parse (parser);
17680 return constructor_p;
17683 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17684 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17685 they must be performed once we are in the scope of the function.
17687 Returns the function defined. */
17690 cp_parser_function_definition_from_specifiers_and_declarator
17691 (cp_parser* parser,
17692 cp_decl_specifier_seq *decl_specifiers,
17694 const cp_declarator *declarator)
17699 /* Begin the function-definition. */
17700 success_p = start_function (decl_specifiers, declarator, attributes);
17702 /* The things we're about to see are not directly qualified by any
17703 template headers we've seen thus far. */
17704 reset_specialization ();
17706 /* If there were names looked up in the decl-specifier-seq that we
17707 did not check, check them now. We must wait until we are in the
17708 scope of the function to perform the checks, since the function
17709 might be a friend. */
17710 perform_deferred_access_checks ();
17714 /* Skip the entire function. */
17715 cp_parser_skip_to_end_of_block_or_statement (parser);
17716 fn = error_mark_node;
17718 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17720 /* Seen already, skip it. An error message has already been output. */
17721 cp_parser_skip_to_end_of_block_or_statement (parser);
17722 fn = current_function_decl;
17723 current_function_decl = NULL_TREE;
17724 /* If this is a function from a class, pop the nested class. */
17725 if (current_class_name)
17726 pop_nested_class ();
17729 fn = cp_parser_function_definition_after_declarator (parser,
17730 /*inline_p=*/false);
17735 /* Parse the part of a function-definition that follows the
17736 declarator. INLINE_P is TRUE iff this function is an inline
17737 function defined with a class-specifier.
17739 Returns the function defined. */
17742 cp_parser_function_definition_after_declarator (cp_parser* parser,
17746 bool ctor_initializer_p = false;
17747 bool saved_in_unbraced_linkage_specification_p;
17748 bool saved_in_function_body;
17749 unsigned saved_num_template_parameter_lists;
17752 saved_in_function_body = parser->in_function_body;
17753 parser->in_function_body = true;
17754 /* If the next token is `return', then the code may be trying to
17755 make use of the "named return value" extension that G++ used to
17757 token = cp_lexer_peek_token (parser->lexer);
17758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17760 /* Consume the `return' keyword. */
17761 cp_lexer_consume_token (parser->lexer);
17762 /* Look for the identifier that indicates what value is to be
17764 cp_parser_identifier (parser);
17765 /* Issue an error message. */
17766 error ("%Hnamed return values are no longer supported",
17768 /* Skip tokens until we reach the start of the function body. */
17771 cp_token *token = cp_lexer_peek_token (parser->lexer);
17772 if (token->type == CPP_OPEN_BRACE
17773 || token->type == CPP_EOF
17774 || token->type == CPP_PRAGMA_EOL)
17776 cp_lexer_consume_token (parser->lexer);
17779 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17780 anything declared inside `f'. */
17781 saved_in_unbraced_linkage_specification_p
17782 = parser->in_unbraced_linkage_specification_p;
17783 parser->in_unbraced_linkage_specification_p = false;
17784 /* Inside the function, surrounding template-parameter-lists do not
17786 saved_num_template_parameter_lists
17787 = parser->num_template_parameter_lists;
17788 parser->num_template_parameter_lists = 0;
17789 /* If the next token is `try', then we are looking at a
17790 function-try-block. */
17791 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17792 ctor_initializer_p = cp_parser_function_try_block (parser);
17793 /* A function-try-block includes the function-body, so we only do
17794 this next part if we're not processing a function-try-block. */
17797 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17799 /* Finish the function. */
17800 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17801 (inline_p ? 2 : 0));
17802 /* Generate code for it, if necessary. */
17803 expand_or_defer_fn (fn);
17804 /* Restore the saved values. */
17805 parser->in_unbraced_linkage_specification_p
17806 = saved_in_unbraced_linkage_specification_p;
17807 parser->num_template_parameter_lists
17808 = saved_num_template_parameter_lists;
17809 parser->in_function_body = saved_in_function_body;
17814 /* Parse a template-declaration, assuming that the `export' (and
17815 `extern') keywords, if present, has already been scanned. MEMBER_P
17816 is as for cp_parser_template_declaration. */
17819 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17821 tree decl = NULL_TREE;
17822 VEC (deferred_access_check,gc) *checks;
17823 tree parameter_list;
17824 bool friend_p = false;
17825 bool need_lang_pop;
17828 /* Look for the `template' keyword. */
17829 token = cp_lexer_peek_token (parser->lexer);
17830 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17834 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17836 if (at_class_scope_p () && current_function_decl)
17838 /* 14.5.2.2 [temp.mem]
17840 A local class shall not have member templates. */
17841 error ("%Hinvalid declaration of member template in local class",
17843 cp_parser_skip_to_end_of_block_or_statement (parser);
17848 A template ... shall not have C linkage. */
17849 if (current_lang_name == lang_name_c)
17851 error ("%Htemplate with C linkage", &token->location);
17852 /* Give it C++ linkage to avoid confusing other parts of the
17854 push_lang_context (lang_name_cplusplus);
17855 need_lang_pop = true;
17858 need_lang_pop = false;
17860 /* We cannot perform access checks on the template parameter
17861 declarations until we know what is being declared, just as we
17862 cannot check the decl-specifier list. */
17863 push_deferring_access_checks (dk_deferred);
17865 /* If the next token is `>', then we have an invalid
17866 specialization. Rather than complain about an invalid template
17867 parameter, issue an error message here. */
17868 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17870 cp_parser_error (parser, "invalid explicit specialization");
17871 begin_specialization ();
17872 parameter_list = NULL_TREE;
17875 /* Parse the template parameters. */
17876 parameter_list = cp_parser_template_parameter_list (parser);
17878 /* Get the deferred access checks from the parameter list. These
17879 will be checked once we know what is being declared, as for a
17880 member template the checks must be performed in the scope of the
17881 class containing the member. */
17882 checks = get_deferred_access_checks ();
17884 /* Look for the `>'. */
17885 cp_parser_skip_to_end_of_template_parameter_list (parser);
17886 /* We just processed one more parameter list. */
17887 ++parser->num_template_parameter_lists;
17888 /* If the next token is `template', there are more template
17890 if (cp_lexer_next_token_is_keyword (parser->lexer,
17892 cp_parser_template_declaration_after_export (parser, member_p);
17895 /* There are no access checks when parsing a template, as we do not
17896 know if a specialization will be a friend. */
17897 push_deferring_access_checks (dk_no_check);
17898 token = cp_lexer_peek_token (parser->lexer);
17899 decl = cp_parser_single_declaration (parser,
17902 /*explicit_specialization_p=*/false,
17904 pop_deferring_access_checks ();
17906 /* If this is a member template declaration, let the front
17908 if (member_p && !friend_p && decl)
17910 if (TREE_CODE (decl) == TYPE_DECL)
17911 cp_parser_check_access_in_redeclaration (decl, token->location);
17913 decl = finish_member_template_decl (decl);
17915 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17916 make_friend_class (current_class_type, TREE_TYPE (decl),
17917 /*complain=*/true);
17919 /* We are done with the current parameter list. */
17920 --parser->num_template_parameter_lists;
17922 pop_deferring_access_checks ();
17925 finish_template_decl (parameter_list);
17927 /* Register member declarations. */
17928 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17929 finish_member_declaration (decl);
17930 /* For the erroneous case of a template with C linkage, we pushed an
17931 implicit C++ linkage scope; exit that scope now. */
17933 pop_lang_context ();
17934 /* If DECL is a function template, we must return to parse it later.
17935 (Even though there is no definition, there might be default
17936 arguments that need handling.) */
17937 if (member_p && decl
17938 && (TREE_CODE (decl) == FUNCTION_DECL
17939 || DECL_FUNCTION_TEMPLATE_P (decl)))
17940 TREE_VALUE (parser->unparsed_functions_queues)
17941 = tree_cons (NULL_TREE, decl,
17942 TREE_VALUE (parser->unparsed_functions_queues));
17945 /* Perform the deferred access checks from a template-parameter-list.
17946 CHECKS is a TREE_LIST of access checks, as returned by
17947 get_deferred_access_checks. */
17950 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17952 ++processing_template_parmlist;
17953 perform_access_checks (checks);
17954 --processing_template_parmlist;
17957 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17958 `function-definition' sequence. MEMBER_P is true, this declaration
17959 appears in a class scope.
17961 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17962 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17965 cp_parser_single_declaration (cp_parser* parser,
17966 VEC (deferred_access_check,gc)* checks,
17968 bool explicit_specialization_p,
17971 int declares_class_or_enum;
17972 tree decl = NULL_TREE;
17973 cp_decl_specifier_seq decl_specifiers;
17974 bool function_definition_p = false;
17975 cp_token *decl_spec_token_start;
17977 /* This function is only used when processing a template
17979 gcc_assert (innermost_scope_kind () == sk_template_parms
17980 || innermost_scope_kind () == sk_template_spec);
17982 /* Defer access checks until we know what is being declared. */
17983 push_deferring_access_checks (dk_deferred);
17985 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17987 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17988 cp_parser_decl_specifier_seq (parser,
17989 CP_PARSER_FLAGS_OPTIONAL,
17991 &declares_class_or_enum);
17993 *friend_p = cp_parser_friend_p (&decl_specifiers);
17995 /* There are no template typedefs. */
17996 if (decl_specifiers.specs[(int) ds_typedef])
17998 error ("%Htemplate declaration of %qs",
17999 &decl_spec_token_start->location, "typedef");
18000 decl = error_mark_node;
18003 /* Gather up the access checks that occurred the
18004 decl-specifier-seq. */
18005 stop_deferring_access_checks ();
18007 /* Check for the declaration of a template class. */
18008 if (declares_class_or_enum)
18010 if (cp_parser_declares_only_class_p (parser))
18012 decl = shadow_tag (&decl_specifiers);
18017 friend template <typename T> struct A<T>::B;
18020 A<T>::B will be represented by a TYPENAME_TYPE, and
18021 therefore not recognized by shadow_tag. */
18022 if (friend_p && *friend_p
18024 && decl_specifiers.type
18025 && TYPE_P (decl_specifiers.type))
18026 decl = decl_specifiers.type;
18028 if (decl && decl != error_mark_node)
18029 decl = TYPE_NAME (decl);
18031 decl = error_mark_node;
18033 /* Perform access checks for template parameters. */
18034 cp_parser_perform_template_parameter_access_checks (checks);
18037 /* If it's not a template class, try for a template function. If
18038 the next token is a `;', then this declaration does not declare
18039 anything. But, if there were errors in the decl-specifiers, then
18040 the error might well have come from an attempted class-specifier.
18041 In that case, there's no need to warn about a missing declarator. */
18043 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18044 || decl_specifiers.type != error_mark_node))
18046 decl = cp_parser_init_declarator (parser,
18049 /*function_definition_allowed_p=*/true,
18051 declares_class_or_enum,
18052 &function_definition_p);
18054 /* 7.1.1-1 [dcl.stc]
18056 A storage-class-specifier shall not be specified in an explicit
18057 specialization... */
18059 && explicit_specialization_p
18060 && decl_specifiers.storage_class != sc_none)
18062 error ("%Hexplicit template specialization cannot have a storage class",
18063 &decl_spec_token_start->location);
18064 decl = error_mark_node;
18068 pop_deferring_access_checks ();
18070 /* Clear any current qualification; whatever comes next is the start
18071 of something new. */
18072 parser->scope = NULL_TREE;
18073 parser->qualifying_scope = NULL_TREE;
18074 parser->object_scope = NULL_TREE;
18075 /* Look for a trailing `;' after the declaration. */
18076 if (!function_definition_p
18077 && (decl == error_mark_node
18078 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18079 cp_parser_skip_to_end_of_block_or_statement (parser);
18084 /* Parse a cast-expression that is not the operand of a unary "&". */
18087 cp_parser_simple_cast_expression (cp_parser *parser)
18089 return cp_parser_cast_expression (parser, /*address_p=*/false,
18090 /*cast_p=*/false, NULL);
18093 /* Parse a functional cast to TYPE. Returns an expression
18094 representing the cast. */
18097 cp_parser_functional_cast (cp_parser* parser, tree type)
18100 tree expression_list;
18104 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18106 maybe_warn_cpp0x ("extended initializer lists");
18107 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18108 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18109 if (TREE_CODE (type) == TYPE_DECL)
18110 type = TREE_TYPE (type);
18111 return finish_compound_literal (type, expression_list);
18115 vec = cp_parser_parenthesized_expression_list (parser, false,
18117 /*allow_expansion_p=*/true,
18118 /*non_constant_p=*/NULL);
18120 expression_list = error_mark_node;
18123 expression_list = build_tree_list_vec (vec);
18124 release_tree_vector (vec);
18127 cast = build_functional_cast (type, expression_list,
18128 tf_warning_or_error);
18129 /* [expr.const]/1: In an integral constant expression "only type
18130 conversions to integral or enumeration type can be used". */
18131 if (TREE_CODE (type) == TYPE_DECL)
18132 type = TREE_TYPE (type);
18133 if (cast != error_mark_node
18134 && !cast_valid_in_integral_constant_expression_p (type)
18135 && (cp_parser_non_integral_constant_expression
18136 (parser, "a call to a constructor")))
18137 return error_mark_node;
18141 /* Save the tokens that make up the body of a member function defined
18142 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18143 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18144 specifiers applied to the declaration. Returns the FUNCTION_DECL
18145 for the member function. */
18148 cp_parser_save_member_function_body (cp_parser* parser,
18149 cp_decl_specifier_seq *decl_specifiers,
18150 cp_declarator *declarator,
18157 /* Create the function-declaration. */
18158 fn = start_method (decl_specifiers, declarator, attributes);
18159 /* If something went badly wrong, bail out now. */
18160 if (fn == error_mark_node)
18162 /* If there's a function-body, skip it. */
18163 if (cp_parser_token_starts_function_definition_p
18164 (cp_lexer_peek_token (parser->lexer)))
18165 cp_parser_skip_to_end_of_block_or_statement (parser);
18166 return error_mark_node;
18169 /* Remember it, if there default args to post process. */
18170 cp_parser_save_default_args (parser, fn);
18172 /* Save away the tokens that make up the body of the
18174 first = parser->lexer->next_token;
18175 /* We can have braced-init-list mem-initializers before the fn body. */
18176 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18178 cp_lexer_consume_token (parser->lexer);
18179 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18180 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18182 /* cache_group will stop after an un-nested { } pair, too. */
18183 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18186 /* variadic mem-inits have ... after the ')'. */
18187 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18188 cp_lexer_consume_token (parser->lexer);
18191 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18192 /* Handle function try blocks. */
18193 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18194 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18195 last = parser->lexer->next_token;
18197 /* Save away the inline definition; we will process it when the
18198 class is complete. */
18199 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18200 DECL_PENDING_INLINE_P (fn) = 1;
18202 /* We need to know that this was defined in the class, so that
18203 friend templates are handled correctly. */
18204 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18206 /* We're done with the inline definition. */
18207 finish_method (fn);
18209 /* Add FN to the queue of functions to be parsed later. */
18210 TREE_VALUE (parser->unparsed_functions_queues)
18211 = tree_cons (NULL_TREE, fn,
18212 TREE_VALUE (parser->unparsed_functions_queues));
18217 /* Parse a template-argument-list, as well as the trailing ">" (but
18218 not the opening ">"). See cp_parser_template_argument_list for the
18222 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18226 tree saved_qualifying_scope;
18227 tree saved_object_scope;
18228 bool saved_greater_than_is_operator_p;
18229 int saved_unevaluated_operand;
18230 int saved_inhibit_evaluation_warnings;
18234 When parsing a template-id, the first non-nested `>' is taken as
18235 the end of the template-argument-list rather than a greater-than
18237 saved_greater_than_is_operator_p
18238 = parser->greater_than_is_operator_p;
18239 parser->greater_than_is_operator_p = false;
18240 /* Parsing the argument list may modify SCOPE, so we save it
18242 saved_scope = parser->scope;
18243 saved_qualifying_scope = parser->qualifying_scope;
18244 saved_object_scope = parser->object_scope;
18245 /* We need to evaluate the template arguments, even though this
18246 template-id may be nested within a "sizeof". */
18247 saved_unevaluated_operand = cp_unevaluated_operand;
18248 cp_unevaluated_operand = 0;
18249 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18250 c_inhibit_evaluation_warnings = 0;
18251 /* Parse the template-argument-list itself. */
18252 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18253 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18254 arguments = NULL_TREE;
18256 arguments = cp_parser_template_argument_list (parser);
18257 /* Look for the `>' that ends the template-argument-list. If we find
18258 a '>>' instead, it's probably just a typo. */
18259 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18261 if (cxx_dialect != cxx98)
18263 /* In C++0x, a `>>' in a template argument list or cast
18264 expression is considered to be two separate `>'
18265 tokens. So, change the current token to a `>', but don't
18266 consume it: it will be consumed later when the outer
18267 template argument list (or cast expression) is parsed.
18268 Note that this replacement of `>' for `>>' is necessary
18269 even if we are parsing tentatively: in the tentative
18270 case, after calling
18271 cp_parser_enclosed_template_argument_list we will always
18272 throw away all of the template arguments and the first
18273 closing `>', either because the template argument list
18274 was erroneous or because we are replacing those tokens
18275 with a CPP_TEMPLATE_ID token. The second `>' (which will
18276 not have been thrown away) is needed either to close an
18277 outer template argument list or to complete a new-style
18279 cp_token *token = cp_lexer_peek_token (parser->lexer);
18280 token->type = CPP_GREATER;
18282 else if (!saved_greater_than_is_operator_p)
18284 /* If we're in a nested template argument list, the '>>' has
18285 to be a typo for '> >'. We emit the error message, but we
18286 continue parsing and we push a '>' as next token, so that
18287 the argument list will be parsed correctly. Note that the
18288 global source location is still on the token before the
18289 '>>', so we need to say explicitly where we want it. */
18290 cp_token *token = cp_lexer_peek_token (parser->lexer);
18291 error ("%H%<>>%> should be %<> >%> "
18292 "within a nested template argument list",
18295 token->type = CPP_GREATER;
18299 /* If this is not a nested template argument list, the '>>'
18300 is a typo for '>'. Emit an error message and continue.
18301 Same deal about the token location, but here we can get it
18302 right by consuming the '>>' before issuing the diagnostic. */
18303 cp_token *token = cp_lexer_consume_token (parser->lexer);
18304 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18305 "a template argument list", &token->location);
18309 cp_parser_skip_to_end_of_template_parameter_list (parser);
18310 /* The `>' token might be a greater-than operator again now. */
18311 parser->greater_than_is_operator_p
18312 = saved_greater_than_is_operator_p;
18313 /* Restore the SAVED_SCOPE. */
18314 parser->scope = saved_scope;
18315 parser->qualifying_scope = saved_qualifying_scope;
18316 parser->object_scope = saved_object_scope;
18317 cp_unevaluated_operand = saved_unevaluated_operand;
18318 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18323 /* MEMBER_FUNCTION is a member function, or a friend. If default
18324 arguments, or the body of the function have not yet been parsed,
18328 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18330 /* If this member is a template, get the underlying
18332 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18333 member_function = DECL_TEMPLATE_RESULT (member_function);
18335 /* There should not be any class definitions in progress at this
18336 point; the bodies of members are only parsed outside of all class
18338 gcc_assert (parser->num_classes_being_defined == 0);
18339 /* While we're parsing the member functions we might encounter more
18340 classes. We want to handle them right away, but we don't want
18341 them getting mixed up with functions that are currently in the
18343 parser->unparsed_functions_queues
18344 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18346 /* Make sure that any template parameters are in scope. */
18347 maybe_begin_member_template_processing (member_function);
18349 /* If the body of the function has not yet been parsed, parse it
18351 if (DECL_PENDING_INLINE_P (member_function))
18353 tree function_scope;
18354 cp_token_cache *tokens;
18356 /* The function is no longer pending; we are processing it. */
18357 tokens = DECL_PENDING_INLINE_INFO (member_function);
18358 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18359 DECL_PENDING_INLINE_P (member_function) = 0;
18361 /* If this is a local class, enter the scope of the containing
18363 function_scope = current_function_decl;
18364 if (function_scope)
18365 push_function_context ();
18367 /* Push the body of the function onto the lexer stack. */
18368 cp_parser_push_lexer_for_tokens (parser, tokens);
18370 /* Let the front end know that we going to be defining this
18372 start_preparsed_function (member_function, NULL_TREE,
18373 SF_PRE_PARSED | SF_INCLASS_INLINE);
18375 /* Don't do access checking if it is a templated function. */
18376 if (processing_template_decl)
18377 push_deferring_access_checks (dk_no_check);
18379 /* Now, parse the body of the function. */
18380 cp_parser_function_definition_after_declarator (parser,
18381 /*inline_p=*/true);
18383 if (processing_template_decl)
18384 pop_deferring_access_checks ();
18386 /* Leave the scope of the containing function. */
18387 if (function_scope)
18388 pop_function_context ();
18389 cp_parser_pop_lexer (parser);
18392 /* Remove any template parameters from the symbol table. */
18393 maybe_end_member_template_processing ();
18395 /* Restore the queue. */
18396 parser->unparsed_functions_queues
18397 = TREE_CHAIN (parser->unparsed_functions_queues);
18400 /* If DECL contains any default args, remember it on the unparsed
18401 functions queue. */
18404 cp_parser_save_default_args (cp_parser* parser, tree decl)
18408 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18410 probe = TREE_CHAIN (probe))
18411 if (TREE_PURPOSE (probe))
18413 TREE_PURPOSE (parser->unparsed_functions_queues)
18414 = tree_cons (current_class_type, decl,
18415 TREE_PURPOSE (parser->unparsed_functions_queues));
18420 /* FN is a FUNCTION_DECL which may contains a parameter with an
18421 unparsed DEFAULT_ARG. Parse the default args now. This function
18422 assumes that the current scope is the scope in which the default
18423 argument should be processed. */
18426 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18428 bool saved_local_variables_forbidden_p;
18431 /* While we're parsing the default args, we might (due to the
18432 statement expression extension) encounter more classes. We want
18433 to handle them right away, but we don't want them getting mixed
18434 up with default args that are currently in the queue. */
18435 parser->unparsed_functions_queues
18436 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18438 /* Local variable names (and the `this' keyword) may not appear
18439 in a default argument. */
18440 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18441 parser->local_variables_forbidden_p = true;
18443 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18445 parm = TREE_CHAIN (parm))
18447 cp_token_cache *tokens;
18448 tree default_arg = TREE_PURPOSE (parm);
18450 VEC(tree,gc) *insts;
18457 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18458 /* This can happen for a friend declaration for a function
18459 already declared with default arguments. */
18462 /* Push the saved tokens for the default argument onto the parser's
18464 tokens = DEFARG_TOKENS (default_arg);
18465 cp_parser_push_lexer_for_tokens (parser, tokens);
18467 /* Parse the assignment-expression. */
18468 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18469 if (parsed_arg == error_mark_node)
18471 cp_parser_pop_lexer (parser);
18475 if (!processing_template_decl)
18476 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18478 TREE_PURPOSE (parm) = parsed_arg;
18480 /* Update any instantiations we've already created. */
18481 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18482 VEC_iterate (tree, insts, ix, copy); ix++)
18483 TREE_PURPOSE (copy) = parsed_arg;
18485 /* If the token stream has not been completely used up, then
18486 there was extra junk after the end of the default
18488 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18489 cp_parser_error (parser, "expected %<,%>");
18491 /* Revert to the main lexer. */
18492 cp_parser_pop_lexer (parser);
18495 /* Make sure no default arg is missing. */
18496 check_default_args (fn);
18498 /* Restore the state of local_variables_forbidden_p. */
18499 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18501 /* Restore the queue. */
18502 parser->unparsed_functions_queues
18503 = TREE_CHAIN (parser->unparsed_functions_queues);
18506 /* Parse the operand of `sizeof' (or a similar operator). Returns
18507 either a TYPE or an expression, depending on the form of the
18508 input. The KEYWORD indicates which kind of expression we have
18512 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18514 tree expr = NULL_TREE;
18515 const char *saved_message;
18517 bool saved_integral_constant_expression_p;
18518 bool saved_non_integral_constant_expression_p;
18519 bool pack_expansion_p = false;
18521 /* Types cannot be defined in a `sizeof' expression. Save away the
18523 saved_message = parser->type_definition_forbidden_message;
18524 /* And create the new one. */
18525 tmp = concat ("types may not be defined in %<",
18526 IDENTIFIER_POINTER (ridpointers[keyword]),
18527 "%> expressions", NULL);
18528 parser->type_definition_forbidden_message = tmp;
18530 /* The restrictions on constant-expressions do not apply inside
18531 sizeof expressions. */
18532 saved_integral_constant_expression_p
18533 = parser->integral_constant_expression_p;
18534 saved_non_integral_constant_expression_p
18535 = parser->non_integral_constant_expression_p;
18536 parser->integral_constant_expression_p = false;
18538 /* If it's a `...', then we are computing the length of a parameter
18540 if (keyword == RID_SIZEOF
18541 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18543 /* Consume the `...'. */
18544 cp_lexer_consume_token (parser->lexer);
18545 maybe_warn_variadic_templates ();
18547 /* Note that this is an expansion. */
18548 pack_expansion_p = true;
18551 /* Do not actually evaluate the expression. */
18552 ++cp_unevaluated_operand;
18553 ++c_inhibit_evaluation_warnings;
18554 /* If it's a `(', then we might be looking at the type-id
18556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18559 bool saved_in_type_id_in_expr_p;
18561 /* We can't be sure yet whether we're looking at a type-id or an
18563 cp_parser_parse_tentatively (parser);
18564 /* Consume the `('. */
18565 cp_lexer_consume_token (parser->lexer);
18566 /* Parse the type-id. */
18567 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18568 parser->in_type_id_in_expr_p = true;
18569 type = cp_parser_type_id (parser);
18570 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18571 /* Now, look for the trailing `)'. */
18572 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18573 /* If all went well, then we're done. */
18574 if (cp_parser_parse_definitely (parser))
18576 cp_decl_specifier_seq decl_specs;
18578 /* Build a trivial decl-specifier-seq. */
18579 clear_decl_specs (&decl_specs);
18580 decl_specs.type = type;
18582 /* Call grokdeclarator to figure out what type this is. */
18583 expr = grokdeclarator (NULL,
18587 /*attrlist=*/NULL);
18591 /* If the type-id production did not work out, then we must be
18592 looking at the unary-expression production. */
18594 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18595 /*cast_p=*/false, NULL);
18597 if (pack_expansion_p)
18598 /* Build a pack expansion. */
18599 expr = make_pack_expansion (expr);
18601 /* Go back to evaluating expressions. */
18602 --cp_unevaluated_operand;
18603 --c_inhibit_evaluation_warnings;
18605 /* Free the message we created. */
18607 /* And restore the old one. */
18608 parser->type_definition_forbidden_message = saved_message;
18609 parser->integral_constant_expression_p
18610 = saved_integral_constant_expression_p;
18611 parser->non_integral_constant_expression_p
18612 = saved_non_integral_constant_expression_p;
18617 /* If the current declaration has no declarator, return true. */
18620 cp_parser_declares_only_class_p (cp_parser *parser)
18622 /* If the next token is a `;' or a `,' then there is no
18624 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18625 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18628 /* Update the DECL_SPECS to reflect the storage class indicated by
18632 cp_parser_set_storage_class (cp_parser *parser,
18633 cp_decl_specifier_seq *decl_specs,
18635 location_t location)
18637 cp_storage_class storage_class;
18639 if (parser->in_unbraced_linkage_specification_p)
18641 error ("%Hinvalid use of %qD in linkage specification",
18642 &location, ridpointers[keyword]);
18645 else if (decl_specs->storage_class != sc_none)
18647 decl_specs->conflicting_specifiers_p = true;
18651 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18652 && decl_specs->specs[(int) ds_thread])
18654 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18655 decl_specs->specs[(int) ds_thread] = 0;
18661 storage_class = sc_auto;
18664 storage_class = sc_register;
18667 storage_class = sc_static;
18670 storage_class = sc_extern;
18673 storage_class = sc_mutable;
18676 gcc_unreachable ();
18678 decl_specs->storage_class = storage_class;
18680 /* A storage class specifier cannot be applied alongside a typedef
18681 specifier. If there is a typedef specifier present then set
18682 conflicting_specifiers_p which will trigger an error later
18683 on in grokdeclarator. */
18684 if (decl_specs->specs[(int)ds_typedef])
18685 decl_specs->conflicting_specifiers_p = true;
18688 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18689 is true, the type is a user-defined type; otherwise it is a
18690 built-in type specified by a keyword. */
18693 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18695 location_t location,
18696 bool user_defined_p)
18698 decl_specs->any_specifiers_p = true;
18700 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18701 (with, for example, in "typedef int wchar_t;") we remember that
18702 this is what happened. In system headers, we ignore these
18703 declarations so that G++ can work with system headers that are not
18705 if (decl_specs->specs[(int) ds_typedef]
18707 && (type_spec == boolean_type_node
18708 || type_spec == char16_type_node
18709 || type_spec == char32_type_node
18710 || type_spec == wchar_type_node)
18711 && (decl_specs->type
18712 || decl_specs->specs[(int) ds_long]
18713 || decl_specs->specs[(int) ds_short]
18714 || decl_specs->specs[(int) ds_unsigned]
18715 || decl_specs->specs[(int) ds_signed]))
18717 decl_specs->redefined_builtin_type = type_spec;
18718 if (!decl_specs->type)
18720 decl_specs->type = type_spec;
18721 decl_specs->user_defined_type_p = false;
18722 decl_specs->type_location = location;
18725 else if (decl_specs->type)
18726 decl_specs->multiple_types_p = true;
18729 decl_specs->type = type_spec;
18730 decl_specs->user_defined_type_p = user_defined_p;
18731 decl_specs->redefined_builtin_type = NULL_TREE;
18732 decl_specs->type_location = location;
18736 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18737 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18740 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18742 return decl_specifiers->specs[(int) ds_friend] != 0;
18745 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18746 issue an error message indicating that TOKEN_DESC was expected.
18748 Returns the token consumed, if the token had the appropriate type.
18749 Otherwise, returns NULL. */
18752 cp_parser_require (cp_parser* parser,
18753 enum cpp_ttype type,
18754 const char* token_desc)
18756 if (cp_lexer_next_token_is (parser->lexer, type))
18757 return cp_lexer_consume_token (parser->lexer);
18760 /* Output the MESSAGE -- unless we're parsing tentatively. */
18761 if (!cp_parser_simulate_error (parser))
18763 char *message = concat ("expected ", token_desc, NULL);
18764 cp_parser_error (parser, message);
18771 /* An error message is produced if the next token is not '>'.
18772 All further tokens are skipped until the desired token is
18773 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18776 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18778 /* Current level of '< ... >'. */
18779 unsigned level = 0;
18780 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18781 unsigned nesting_depth = 0;
18783 /* Are we ready, yet? If not, issue error message. */
18784 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18787 /* Skip tokens until the desired token is found. */
18790 /* Peek at the next token. */
18791 switch (cp_lexer_peek_token (parser->lexer)->type)
18794 if (!nesting_depth)
18799 if (cxx_dialect == cxx98)
18800 /* C++0x views the `>>' operator as two `>' tokens, but
18803 else if (!nesting_depth && level-- == 0)
18805 /* We've hit a `>>' where the first `>' closes the
18806 template argument list, and the second `>' is
18807 spurious. Just consume the `>>' and stop; we've
18808 already produced at least one error. */
18809 cp_lexer_consume_token (parser->lexer);
18812 /* Fall through for C++0x, so we handle the second `>' in
18816 if (!nesting_depth && level-- == 0)
18818 /* We've reached the token we want, consume it and stop. */
18819 cp_lexer_consume_token (parser->lexer);
18824 case CPP_OPEN_PAREN:
18825 case CPP_OPEN_SQUARE:
18829 case CPP_CLOSE_PAREN:
18830 case CPP_CLOSE_SQUARE:
18831 if (nesting_depth-- == 0)
18836 case CPP_PRAGMA_EOL:
18837 case CPP_SEMICOLON:
18838 case CPP_OPEN_BRACE:
18839 case CPP_CLOSE_BRACE:
18840 /* The '>' was probably forgotten, don't look further. */
18847 /* Consume this token. */
18848 cp_lexer_consume_token (parser->lexer);
18852 /* If the next token is the indicated keyword, consume it. Otherwise,
18853 issue an error message indicating that TOKEN_DESC was expected.
18855 Returns the token consumed, if the token had the appropriate type.
18856 Otherwise, returns NULL. */
18859 cp_parser_require_keyword (cp_parser* parser,
18861 const char* token_desc)
18863 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18865 if (token && token->keyword != keyword)
18867 dyn_string_t error_msg;
18869 /* Format the error message. */
18870 error_msg = dyn_string_new (0);
18871 dyn_string_append_cstr (error_msg, "expected ");
18872 dyn_string_append_cstr (error_msg, token_desc);
18873 cp_parser_error (parser, error_msg->s);
18874 dyn_string_delete (error_msg);
18881 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18882 function-definition. */
18885 cp_parser_token_starts_function_definition_p (cp_token* token)
18887 return (/* An ordinary function-body begins with an `{'. */
18888 token->type == CPP_OPEN_BRACE
18889 /* A ctor-initializer begins with a `:'. */
18890 || token->type == CPP_COLON
18891 /* A function-try-block begins with `try'. */
18892 || token->keyword == RID_TRY
18893 /* The named return value extension begins with `return'. */
18894 || token->keyword == RID_RETURN);
18897 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18901 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18905 token = cp_lexer_peek_token (parser->lexer);
18906 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18909 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18910 C++0x) ending a template-argument. */
18913 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18917 token = cp_lexer_peek_token (parser->lexer);
18918 return (token->type == CPP_COMMA
18919 || token->type == CPP_GREATER
18920 || token->type == CPP_ELLIPSIS
18921 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18924 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18925 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18928 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18933 token = cp_lexer_peek_nth_token (parser->lexer, n);
18934 if (token->type == CPP_LESS)
18936 /* Check for the sequence `<::' in the original code. It would be lexed as
18937 `[:', where `[' is a digraph, and there is no whitespace before
18939 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18942 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18943 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18949 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18950 or none_type otherwise. */
18952 static enum tag_types
18953 cp_parser_token_is_class_key (cp_token* token)
18955 switch (token->keyword)
18960 return record_type;
18969 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18972 cp_parser_check_class_key (enum tag_types class_key, tree type)
18974 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18975 permerror (input_location, "%qs tag used in naming %q#T",
18976 class_key == union_type ? "union"
18977 : class_key == record_type ? "struct" : "class",
18981 /* Issue an error message if DECL is redeclared with different
18982 access than its original declaration [class.access.spec/3].
18983 This applies to nested classes and nested class templates.
18987 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18989 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18992 if ((TREE_PRIVATE (decl)
18993 != (current_access_specifier == access_private_node))
18994 || (TREE_PROTECTED (decl)
18995 != (current_access_specifier == access_protected_node)))
18996 error ("%H%qD redeclared with different access", &location, decl);
18999 /* Look for the `template' keyword, as a syntactic disambiguator.
19000 Return TRUE iff it is present, in which case it will be
19004 cp_parser_optional_template_keyword (cp_parser *parser)
19006 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19008 /* The `template' keyword can only be used within templates;
19009 outside templates the parser can always figure out what is a
19010 template and what is not. */
19011 if (!processing_template_decl)
19013 cp_token *token = cp_lexer_peek_token (parser->lexer);
19014 error ("%H%<template%> (as a disambiguator) is only allowed "
19015 "within templates", &token->location);
19016 /* If this part of the token stream is rescanned, the same
19017 error message would be generated. So, we purge the token
19018 from the stream. */
19019 cp_lexer_purge_token (parser->lexer);
19024 /* Consume the `template' keyword. */
19025 cp_lexer_consume_token (parser->lexer);
19033 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
19034 set PARSER->SCOPE, and perform other related actions. */
19037 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19040 struct tree_check *check_value;
19041 deferred_access_check *chk;
19042 VEC (deferred_access_check,gc) *checks;
19044 /* Get the stored value. */
19045 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19046 /* Perform any access checks that were deferred. */
19047 checks = check_value->checks;
19051 VEC_iterate (deferred_access_check, checks, i, chk) ;
19054 perform_or_defer_access_check (chk->binfo,
19059 /* Set the scope from the stored value. */
19060 parser->scope = check_value->value;
19061 parser->qualifying_scope = check_value->qualifying_scope;
19062 parser->object_scope = NULL_TREE;
19065 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19066 encounter the end of a block before what we were looking for. */
19069 cp_parser_cache_group (cp_parser *parser,
19070 enum cpp_ttype end,
19075 cp_token *token = cp_lexer_peek_token (parser->lexer);
19077 /* Abort a parenthesized expression if we encounter a semicolon. */
19078 if ((end == CPP_CLOSE_PAREN || depth == 0)
19079 && token->type == CPP_SEMICOLON)
19081 /* If we've reached the end of the file, stop. */
19082 if (token->type == CPP_EOF
19083 || (end != CPP_PRAGMA_EOL
19084 && token->type == CPP_PRAGMA_EOL))
19086 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19087 /* We've hit the end of an enclosing block, so there's been some
19088 kind of syntax error. */
19091 /* Consume the token. */
19092 cp_lexer_consume_token (parser->lexer);
19093 /* See if it starts a new group. */
19094 if (token->type == CPP_OPEN_BRACE)
19096 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19097 /* In theory this should probably check end == '}', but
19098 cp_parser_save_member_function_body needs it to exit
19099 after either '}' or ')' when called with ')'. */
19103 else if (token->type == CPP_OPEN_PAREN)
19105 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19106 if (depth == 0 && end == CPP_CLOSE_PAREN)
19109 else if (token->type == CPP_PRAGMA)
19110 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19111 else if (token->type == end)
19116 /* Begin parsing tentatively. We always save tokens while parsing
19117 tentatively so that if the tentative parsing fails we can restore the
19121 cp_parser_parse_tentatively (cp_parser* parser)
19123 /* Enter a new parsing context. */
19124 parser->context = cp_parser_context_new (parser->context);
19125 /* Begin saving tokens. */
19126 cp_lexer_save_tokens (parser->lexer);
19127 /* In order to avoid repetitive access control error messages,
19128 access checks are queued up until we are no longer parsing
19130 push_deferring_access_checks (dk_deferred);
19133 /* Commit to the currently active tentative parse. */
19136 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19138 cp_parser_context *context;
19141 /* Mark all of the levels as committed. */
19142 lexer = parser->lexer;
19143 for (context = parser->context; context->next; context = context->next)
19145 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19147 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19148 while (!cp_lexer_saving_tokens (lexer))
19149 lexer = lexer->next;
19150 cp_lexer_commit_tokens (lexer);
19154 /* Abort the currently active tentative parse. All consumed tokens
19155 will be rolled back, and no diagnostics will be issued. */
19158 cp_parser_abort_tentative_parse (cp_parser* parser)
19160 cp_parser_simulate_error (parser);
19161 /* Now, pretend that we want to see if the construct was
19162 successfully parsed. */
19163 cp_parser_parse_definitely (parser);
19166 /* Stop parsing tentatively. If a parse error has occurred, restore the
19167 token stream. Otherwise, commit to the tokens we have consumed.
19168 Returns true if no error occurred; false otherwise. */
19171 cp_parser_parse_definitely (cp_parser* parser)
19173 bool error_occurred;
19174 cp_parser_context *context;
19176 /* Remember whether or not an error occurred, since we are about to
19177 destroy that information. */
19178 error_occurred = cp_parser_error_occurred (parser);
19179 /* Remove the topmost context from the stack. */
19180 context = parser->context;
19181 parser->context = context->next;
19182 /* If no parse errors occurred, commit to the tentative parse. */
19183 if (!error_occurred)
19185 /* Commit to the tokens read tentatively, unless that was
19187 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19188 cp_lexer_commit_tokens (parser->lexer);
19190 pop_to_parent_deferring_access_checks ();
19192 /* Otherwise, if errors occurred, roll back our state so that things
19193 are just as they were before we began the tentative parse. */
19196 cp_lexer_rollback_tokens (parser->lexer);
19197 pop_deferring_access_checks ();
19199 /* Add the context to the front of the free list. */
19200 context->next = cp_parser_context_free_list;
19201 cp_parser_context_free_list = context;
19203 return !error_occurred;
19206 /* Returns true if we are parsing tentatively and are not committed to
19207 this tentative parse. */
19210 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19212 return (cp_parser_parsing_tentatively (parser)
19213 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19216 /* Returns nonzero iff an error has occurred during the most recent
19217 tentative parse. */
19220 cp_parser_error_occurred (cp_parser* parser)
19222 return (cp_parser_parsing_tentatively (parser)
19223 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19226 /* Returns nonzero if GNU extensions are allowed. */
19229 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19231 return parser->allow_gnu_extensions_p;
19234 /* Objective-C++ Productions */
19237 /* Parse an Objective-C expression, which feeds into a primary-expression
19241 objc-message-expression
19242 objc-string-literal
19243 objc-encode-expression
19244 objc-protocol-expression
19245 objc-selector-expression
19247 Returns a tree representation of the expression. */
19250 cp_parser_objc_expression (cp_parser* parser)
19252 /* Try to figure out what kind of declaration is present. */
19253 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19257 case CPP_OPEN_SQUARE:
19258 return cp_parser_objc_message_expression (parser);
19260 case CPP_OBJC_STRING:
19261 kwd = cp_lexer_consume_token (parser->lexer);
19262 return objc_build_string_object (kwd->u.value);
19265 switch (kwd->keyword)
19267 case RID_AT_ENCODE:
19268 return cp_parser_objc_encode_expression (parser);
19270 case RID_AT_PROTOCOL:
19271 return cp_parser_objc_protocol_expression (parser);
19273 case RID_AT_SELECTOR:
19274 return cp_parser_objc_selector_expression (parser);
19280 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19281 &kwd->location, kwd->u.value);
19282 cp_parser_skip_to_end_of_block_or_statement (parser);
19285 return error_mark_node;
19288 /* Parse an Objective-C message expression.
19290 objc-message-expression:
19291 [ objc-message-receiver objc-message-args ]
19293 Returns a representation of an Objective-C message. */
19296 cp_parser_objc_message_expression (cp_parser* parser)
19298 tree receiver, messageargs;
19300 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19301 receiver = cp_parser_objc_message_receiver (parser);
19302 messageargs = cp_parser_objc_message_args (parser);
19303 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19305 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19308 /* Parse an objc-message-receiver.
19310 objc-message-receiver:
19312 simple-type-specifier
19314 Returns a representation of the type or expression. */
19317 cp_parser_objc_message_receiver (cp_parser* parser)
19321 /* An Objective-C message receiver may be either (1) a type
19322 or (2) an expression. */
19323 cp_parser_parse_tentatively (parser);
19324 rcv = cp_parser_expression (parser, false, NULL);
19326 if (cp_parser_parse_definitely (parser))
19329 rcv = cp_parser_simple_type_specifier (parser,
19330 /*decl_specs=*/NULL,
19331 CP_PARSER_FLAGS_NONE);
19333 return objc_get_class_reference (rcv);
19336 /* Parse the arguments and selectors comprising an Objective-C message.
19341 objc-selector-args , objc-comma-args
19343 objc-selector-args:
19344 objc-selector [opt] : assignment-expression
19345 objc-selector-args objc-selector [opt] : assignment-expression
19348 assignment-expression
19349 objc-comma-args , assignment-expression
19351 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19352 selector arguments and TREE_VALUE containing a list of comma
19356 cp_parser_objc_message_args (cp_parser* parser)
19358 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19359 bool maybe_unary_selector_p = true;
19360 cp_token *token = cp_lexer_peek_token (parser->lexer);
19362 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19364 tree selector = NULL_TREE, arg;
19366 if (token->type != CPP_COLON)
19367 selector = cp_parser_objc_selector (parser);
19369 /* Detect if we have a unary selector. */
19370 if (maybe_unary_selector_p
19371 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19372 return build_tree_list (selector, NULL_TREE);
19374 maybe_unary_selector_p = false;
19375 cp_parser_require (parser, CPP_COLON, "%<:%>");
19376 arg = cp_parser_assignment_expression (parser, false, NULL);
19379 = chainon (sel_args,
19380 build_tree_list (selector, arg));
19382 token = cp_lexer_peek_token (parser->lexer);
19385 /* Handle non-selector arguments, if any. */
19386 while (token->type == CPP_COMMA)
19390 cp_lexer_consume_token (parser->lexer);
19391 arg = cp_parser_assignment_expression (parser, false, NULL);
19394 = chainon (addl_args,
19395 build_tree_list (NULL_TREE, arg));
19397 token = cp_lexer_peek_token (parser->lexer);
19400 return build_tree_list (sel_args, addl_args);
19403 /* Parse an Objective-C encode expression.
19405 objc-encode-expression:
19406 @encode objc-typename
19408 Returns an encoded representation of the type argument. */
19411 cp_parser_objc_encode_expression (cp_parser* parser)
19416 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19417 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19418 token = cp_lexer_peek_token (parser->lexer);
19419 type = complete_type (cp_parser_type_id (parser));
19420 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19424 error ("%H%<@encode%> must specify a type as an argument",
19426 return error_mark_node;
19429 return objc_build_encode_expr (type);
19432 /* Parse an Objective-C @defs expression. */
19435 cp_parser_objc_defs_expression (cp_parser *parser)
19439 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19440 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19441 name = cp_parser_identifier (parser);
19442 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19444 return objc_get_class_ivars (name);
19447 /* Parse an Objective-C protocol expression.
19449 objc-protocol-expression:
19450 @protocol ( identifier )
19452 Returns a representation of the protocol expression. */
19455 cp_parser_objc_protocol_expression (cp_parser* parser)
19459 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19460 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19461 proto = cp_parser_identifier (parser);
19462 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19464 return objc_build_protocol_expr (proto);
19467 /* Parse an Objective-C selector expression.
19469 objc-selector-expression:
19470 @selector ( objc-method-signature )
19472 objc-method-signature:
19478 objc-selector-seq objc-selector :
19480 Returns a representation of the method selector. */
19483 cp_parser_objc_selector_expression (cp_parser* parser)
19485 tree sel_seq = NULL_TREE;
19486 bool maybe_unary_selector_p = true;
19488 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
19490 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19491 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19492 token = cp_lexer_peek_token (parser->lexer);
19494 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19495 || token->type == CPP_SCOPE)
19497 tree selector = NULL_TREE;
19499 if (token->type != CPP_COLON
19500 || token->type == CPP_SCOPE)
19501 selector = cp_parser_objc_selector (parser);
19503 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19504 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19506 /* Detect if we have a unary selector. */
19507 if (maybe_unary_selector_p)
19509 sel_seq = selector;
19510 goto finish_selector;
19514 cp_parser_error (parser, "expected %<:%>");
19517 maybe_unary_selector_p = false;
19518 token = cp_lexer_consume_token (parser->lexer);
19520 if (token->type == CPP_SCOPE)
19523 = chainon (sel_seq,
19524 build_tree_list (selector, NULL_TREE));
19526 = chainon (sel_seq,
19527 build_tree_list (NULL_TREE, NULL_TREE));
19531 = chainon (sel_seq,
19532 build_tree_list (selector, NULL_TREE));
19534 token = cp_lexer_peek_token (parser->lexer);
19538 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19540 return objc_build_selector_expr (loc, sel_seq);
19543 /* Parse a list of identifiers.
19545 objc-identifier-list:
19547 objc-identifier-list , identifier
19549 Returns a TREE_LIST of identifier nodes. */
19552 cp_parser_objc_identifier_list (cp_parser* parser)
19554 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19555 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19557 while (sep->type == CPP_COMMA)
19559 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19560 list = chainon (list,
19561 build_tree_list (NULL_TREE,
19562 cp_parser_identifier (parser)));
19563 sep = cp_lexer_peek_token (parser->lexer);
19569 /* Parse an Objective-C alias declaration.
19571 objc-alias-declaration:
19572 @compatibility_alias identifier identifier ;
19574 This function registers the alias mapping with the Objective-C front end.
19575 It returns nothing. */
19578 cp_parser_objc_alias_declaration (cp_parser* parser)
19582 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19583 alias = cp_parser_identifier (parser);
19584 orig = cp_parser_identifier (parser);
19585 objc_declare_alias (alias, orig);
19586 cp_parser_consume_semicolon_at_end_of_statement (parser);
19589 /* Parse an Objective-C class forward-declaration.
19591 objc-class-declaration:
19592 @class objc-identifier-list ;
19594 The function registers the forward declarations with the Objective-C
19595 front end. It returns nothing. */
19598 cp_parser_objc_class_declaration (cp_parser* parser)
19600 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19601 objc_declare_class (cp_parser_objc_identifier_list (parser));
19602 cp_parser_consume_semicolon_at_end_of_statement (parser);
19605 /* Parse a list of Objective-C protocol references.
19607 objc-protocol-refs-opt:
19608 objc-protocol-refs [opt]
19610 objc-protocol-refs:
19611 < objc-identifier-list >
19613 Returns a TREE_LIST of identifiers, if any. */
19616 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19618 tree protorefs = NULL_TREE;
19620 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19622 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19623 protorefs = cp_parser_objc_identifier_list (parser);
19624 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19630 /* Parse a Objective-C visibility specification. */
19633 cp_parser_objc_visibility_spec (cp_parser* parser)
19635 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19637 switch (vis->keyword)
19639 case RID_AT_PRIVATE:
19640 objc_set_visibility (2);
19642 case RID_AT_PROTECTED:
19643 objc_set_visibility (0);
19645 case RID_AT_PUBLIC:
19646 objc_set_visibility (1);
19652 /* Eat '@private'/'@protected'/'@public'. */
19653 cp_lexer_consume_token (parser->lexer);
19656 /* Parse an Objective-C method type. */
19659 cp_parser_objc_method_type (cp_parser* parser)
19661 objc_set_method_type
19662 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19667 /* Parse an Objective-C protocol qualifier. */
19670 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19672 tree quals = NULL_TREE, node;
19673 cp_token *token = cp_lexer_peek_token (parser->lexer);
19675 node = token->u.value;
19677 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19678 && (node == ridpointers [(int) RID_IN]
19679 || node == ridpointers [(int) RID_OUT]
19680 || node == ridpointers [(int) RID_INOUT]
19681 || node == ridpointers [(int) RID_BYCOPY]
19682 || node == ridpointers [(int) RID_BYREF]
19683 || node == ridpointers [(int) RID_ONEWAY]))
19685 quals = tree_cons (NULL_TREE, node, quals);
19686 cp_lexer_consume_token (parser->lexer);
19687 token = cp_lexer_peek_token (parser->lexer);
19688 node = token->u.value;
19694 /* Parse an Objective-C typename. */
19697 cp_parser_objc_typename (cp_parser* parser)
19699 tree type_name = NULL_TREE;
19701 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19703 tree proto_quals, cp_type = NULL_TREE;
19705 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19706 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19708 /* An ObjC type name may consist of just protocol qualifiers, in which
19709 case the type shall default to 'id'. */
19710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19711 cp_type = cp_parser_type_id (parser);
19713 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19714 type_name = build_tree_list (proto_quals, cp_type);
19720 /* Check to see if TYPE refers to an Objective-C selector name. */
19723 cp_parser_objc_selector_p (enum cpp_ttype type)
19725 return (type == CPP_NAME || type == CPP_KEYWORD
19726 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19727 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19728 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19729 || type == CPP_XOR || type == CPP_XOR_EQ);
19732 /* Parse an Objective-C selector. */
19735 cp_parser_objc_selector (cp_parser* parser)
19737 cp_token *token = cp_lexer_consume_token (parser->lexer);
19739 if (!cp_parser_objc_selector_p (token->type))
19741 error ("%Hinvalid Objective-C++ selector name", &token->location);
19742 return error_mark_node;
19745 /* C++ operator names are allowed to appear in ObjC selectors. */
19746 switch (token->type)
19748 case CPP_AND_AND: return get_identifier ("and");
19749 case CPP_AND_EQ: return get_identifier ("and_eq");
19750 case CPP_AND: return get_identifier ("bitand");
19751 case CPP_OR: return get_identifier ("bitor");
19752 case CPP_COMPL: return get_identifier ("compl");
19753 case CPP_NOT: return get_identifier ("not");
19754 case CPP_NOT_EQ: return get_identifier ("not_eq");
19755 case CPP_OR_OR: return get_identifier ("or");
19756 case CPP_OR_EQ: return get_identifier ("or_eq");
19757 case CPP_XOR: return get_identifier ("xor");
19758 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19759 default: return token->u.value;
19763 /* Parse an Objective-C params list. */
19766 cp_parser_objc_method_keyword_params (cp_parser* parser)
19768 tree params = NULL_TREE;
19769 bool maybe_unary_selector_p = true;
19770 cp_token *token = cp_lexer_peek_token (parser->lexer);
19772 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19774 tree selector = NULL_TREE, type_name, identifier;
19776 if (token->type != CPP_COLON)
19777 selector = cp_parser_objc_selector (parser);
19779 /* Detect if we have a unary selector. */
19780 if (maybe_unary_selector_p
19781 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19784 maybe_unary_selector_p = false;
19785 cp_parser_require (parser, CPP_COLON, "%<:%>");
19786 type_name = cp_parser_objc_typename (parser);
19787 identifier = cp_parser_identifier (parser);
19791 objc_build_keyword_decl (selector,
19795 token = cp_lexer_peek_token (parser->lexer);
19801 /* Parse the non-keyword Objective-C params. */
19804 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19806 tree params = make_node (TREE_LIST);
19807 cp_token *token = cp_lexer_peek_token (parser->lexer);
19808 *ellipsisp = false; /* Initially, assume no ellipsis. */
19810 while (token->type == CPP_COMMA)
19812 cp_parameter_declarator *parmdecl;
19815 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19816 token = cp_lexer_peek_token (parser->lexer);
19818 if (token->type == CPP_ELLIPSIS)
19820 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19825 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19826 parm = grokdeclarator (parmdecl->declarator,
19827 &parmdecl->decl_specifiers,
19828 PARM, /*initialized=*/0,
19829 /*attrlist=*/NULL);
19831 chainon (params, build_tree_list (NULL_TREE, parm));
19832 token = cp_lexer_peek_token (parser->lexer);
19838 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19841 cp_parser_objc_interstitial_code (cp_parser* parser)
19843 cp_token *token = cp_lexer_peek_token (parser->lexer);
19845 /* If the next token is `extern' and the following token is a string
19846 literal, then we have a linkage specification. */
19847 if (token->keyword == RID_EXTERN
19848 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19849 cp_parser_linkage_specification (parser);
19850 /* Handle #pragma, if any. */
19851 else if (token->type == CPP_PRAGMA)
19852 cp_parser_pragma (parser, pragma_external);
19853 /* Allow stray semicolons. */
19854 else if (token->type == CPP_SEMICOLON)
19855 cp_lexer_consume_token (parser->lexer);
19856 /* Finally, try to parse a block-declaration, or a function-definition. */
19858 cp_parser_block_declaration (parser, /*statement_p=*/false);
19861 /* Parse a method signature. */
19864 cp_parser_objc_method_signature (cp_parser* parser)
19866 tree rettype, kwdparms, optparms;
19867 bool ellipsis = false;
19869 cp_parser_objc_method_type (parser);
19870 rettype = cp_parser_objc_typename (parser);
19871 kwdparms = cp_parser_objc_method_keyword_params (parser);
19872 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19874 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19877 /* Pars an Objective-C method prototype list. */
19880 cp_parser_objc_method_prototype_list (cp_parser* parser)
19882 cp_token *token = cp_lexer_peek_token (parser->lexer);
19884 while (token->keyword != RID_AT_END)
19886 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19888 objc_add_method_declaration
19889 (cp_parser_objc_method_signature (parser));
19890 cp_parser_consume_semicolon_at_end_of_statement (parser);
19893 /* Allow for interspersed non-ObjC++ code. */
19894 cp_parser_objc_interstitial_code (parser);
19896 token = cp_lexer_peek_token (parser->lexer);
19899 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19900 objc_finish_interface ();
19903 /* Parse an Objective-C method definition list. */
19906 cp_parser_objc_method_definition_list (cp_parser* parser)
19908 cp_token *token = cp_lexer_peek_token (parser->lexer);
19910 while (token->keyword != RID_AT_END)
19914 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19916 push_deferring_access_checks (dk_deferred);
19917 objc_start_method_definition
19918 (cp_parser_objc_method_signature (parser));
19920 /* For historical reasons, we accept an optional semicolon. */
19921 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19922 cp_lexer_consume_token (parser->lexer);
19924 perform_deferred_access_checks ();
19925 stop_deferring_access_checks ();
19926 meth = cp_parser_function_definition_after_declarator (parser,
19928 pop_deferring_access_checks ();
19929 objc_finish_method_definition (meth);
19932 /* Allow for interspersed non-ObjC++ code. */
19933 cp_parser_objc_interstitial_code (parser);
19935 token = cp_lexer_peek_token (parser->lexer);
19938 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19939 objc_finish_implementation ();
19942 /* Parse Objective-C ivars. */
19945 cp_parser_objc_class_ivars (cp_parser* parser)
19947 cp_token *token = cp_lexer_peek_token (parser->lexer);
19949 if (token->type != CPP_OPEN_BRACE)
19950 return; /* No ivars specified. */
19952 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19953 token = cp_lexer_peek_token (parser->lexer);
19955 while (token->type != CPP_CLOSE_BRACE)
19957 cp_decl_specifier_seq declspecs;
19958 int decl_class_or_enum_p;
19959 tree prefix_attributes;
19961 cp_parser_objc_visibility_spec (parser);
19963 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19966 cp_parser_decl_specifier_seq (parser,
19967 CP_PARSER_FLAGS_OPTIONAL,
19969 &decl_class_or_enum_p);
19970 prefix_attributes = declspecs.attributes;
19971 declspecs.attributes = NULL_TREE;
19973 /* Keep going until we hit the `;' at the end of the
19975 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19977 tree width = NULL_TREE, attributes, first_attribute, decl;
19978 cp_declarator *declarator = NULL;
19979 int ctor_dtor_or_conv_p;
19981 /* Check for a (possibly unnamed) bitfield declaration. */
19982 token = cp_lexer_peek_token (parser->lexer);
19983 if (token->type == CPP_COLON)
19986 if (token->type == CPP_NAME
19987 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19990 /* Get the name of the bitfield. */
19991 declarator = make_id_declarator (NULL_TREE,
19992 cp_parser_identifier (parser),
19996 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19997 /* Get the width of the bitfield. */
19999 = cp_parser_constant_expression (parser,
20000 /*allow_non_constant=*/false,
20005 /* Parse the declarator. */
20007 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20008 &ctor_dtor_or_conv_p,
20009 /*parenthesized_p=*/NULL,
20010 /*member_p=*/false);
20013 /* Look for attributes that apply to the ivar. */
20014 attributes = cp_parser_attributes_opt (parser);
20015 /* Remember which attributes are prefix attributes and
20017 first_attribute = attributes;
20018 /* Combine the attributes. */
20019 attributes = chainon (prefix_attributes, attributes);
20022 /* Create the bitfield declaration. */
20023 decl = grokbitfield (declarator, &declspecs,
20027 decl = grokfield (declarator, &declspecs,
20028 NULL_TREE, /*init_const_expr_p=*/false,
20029 NULL_TREE, attributes);
20031 /* Add the instance variable. */
20032 objc_add_instance_variable (decl);
20034 /* Reset PREFIX_ATTRIBUTES. */
20035 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20036 attributes = TREE_CHAIN (attributes);
20038 TREE_CHAIN (attributes) = NULL_TREE;
20040 token = cp_lexer_peek_token (parser->lexer);
20042 if (token->type == CPP_COMMA)
20044 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20050 cp_parser_consume_semicolon_at_end_of_statement (parser);
20051 token = cp_lexer_peek_token (parser->lexer);
20054 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20055 /* For historical reasons, we accept an optional semicolon. */
20056 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20057 cp_lexer_consume_token (parser->lexer);
20060 /* Parse an Objective-C protocol declaration. */
20063 cp_parser_objc_protocol_declaration (cp_parser* parser)
20065 tree proto, protorefs;
20068 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20071 tok = cp_lexer_peek_token (parser->lexer);
20072 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
20076 /* See if we have a forward declaration or a definition. */
20077 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20079 /* Try a forward declaration first. */
20080 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20082 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20084 cp_parser_consume_semicolon_at_end_of_statement (parser);
20087 /* Ok, we got a full-fledged definition (or at least should). */
20090 proto = cp_parser_identifier (parser);
20091 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20092 objc_start_protocol (proto, protorefs);
20093 cp_parser_objc_method_prototype_list (parser);
20097 /* Parse an Objective-C superclass or category. */
20100 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20103 cp_token *next = cp_lexer_peek_token (parser->lexer);
20105 *super = *categ = NULL_TREE;
20106 if (next->type == CPP_COLON)
20108 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20109 *super = cp_parser_identifier (parser);
20111 else if (next->type == CPP_OPEN_PAREN)
20113 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20114 *categ = cp_parser_identifier (parser);
20115 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20119 /* Parse an Objective-C class interface. */
20122 cp_parser_objc_class_interface (cp_parser* parser)
20124 tree name, super, categ, protos;
20126 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20127 name = cp_parser_identifier (parser);
20128 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20129 protos = cp_parser_objc_protocol_refs_opt (parser);
20131 /* We have either a class or a category on our hands. */
20133 objc_start_category_interface (name, categ, protos);
20136 objc_start_class_interface (name, super, protos);
20137 /* Handle instance variable declarations, if any. */
20138 cp_parser_objc_class_ivars (parser);
20139 objc_continue_interface ();
20142 cp_parser_objc_method_prototype_list (parser);
20145 /* Parse an Objective-C class implementation. */
20148 cp_parser_objc_class_implementation (cp_parser* parser)
20150 tree name, super, categ;
20152 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20153 name = cp_parser_identifier (parser);
20154 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20156 /* We have either a class or a category on our hands. */
20158 objc_start_category_implementation (name, categ);
20161 objc_start_class_implementation (name, super);
20162 /* Handle instance variable declarations, if any. */
20163 cp_parser_objc_class_ivars (parser);
20164 objc_continue_implementation ();
20167 cp_parser_objc_method_definition_list (parser);
20170 /* Consume the @end token and finish off the implementation. */
20173 cp_parser_objc_end_implementation (cp_parser* parser)
20175 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20176 objc_finish_implementation ();
20179 /* Parse an Objective-C declaration. */
20182 cp_parser_objc_declaration (cp_parser* parser)
20184 /* Try to figure out what kind of declaration is present. */
20185 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20187 switch (kwd->keyword)
20190 cp_parser_objc_alias_declaration (parser);
20193 cp_parser_objc_class_declaration (parser);
20195 case RID_AT_PROTOCOL:
20196 cp_parser_objc_protocol_declaration (parser);
20198 case RID_AT_INTERFACE:
20199 cp_parser_objc_class_interface (parser);
20201 case RID_AT_IMPLEMENTATION:
20202 cp_parser_objc_class_implementation (parser);
20205 cp_parser_objc_end_implementation (parser);
20208 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20209 &kwd->location, kwd->u.value);
20210 cp_parser_skip_to_end_of_block_or_statement (parser);
20214 /* Parse an Objective-C try-catch-finally statement.
20216 objc-try-catch-finally-stmt:
20217 @try compound-statement objc-catch-clause-seq [opt]
20218 objc-finally-clause [opt]
20220 objc-catch-clause-seq:
20221 objc-catch-clause objc-catch-clause-seq [opt]
20224 @catch ( exception-declaration ) compound-statement
20226 objc-finally-clause
20227 @finally compound-statement
20229 Returns NULL_TREE. */
20232 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20233 location_t location;
20236 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20237 location = cp_lexer_peek_token (parser->lexer)->location;
20238 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20239 node, lest it get absorbed into the surrounding block. */
20240 stmt = push_stmt_list ();
20241 cp_parser_compound_statement (parser, NULL, false);
20242 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20244 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20246 cp_parameter_declarator *parmdecl;
20249 cp_lexer_consume_token (parser->lexer);
20250 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20251 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20252 parm = grokdeclarator (parmdecl->declarator,
20253 &parmdecl->decl_specifiers,
20254 PARM, /*initialized=*/0,
20255 /*attrlist=*/NULL);
20256 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20257 objc_begin_catch_clause (parm);
20258 cp_parser_compound_statement (parser, NULL, false);
20259 objc_finish_catch_clause ();
20262 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20264 cp_lexer_consume_token (parser->lexer);
20265 location = cp_lexer_peek_token (parser->lexer)->location;
20266 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20267 node, lest it get absorbed into the surrounding block. */
20268 stmt = push_stmt_list ();
20269 cp_parser_compound_statement (parser, NULL, false);
20270 objc_build_finally_clause (location, pop_stmt_list (stmt));
20273 return objc_finish_try_stmt ();
20276 /* Parse an Objective-C synchronized statement.
20278 objc-synchronized-stmt:
20279 @synchronized ( expression ) compound-statement
20281 Returns NULL_TREE. */
20284 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20285 location_t location;
20288 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20290 location = cp_lexer_peek_token (parser->lexer)->location;
20291 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20292 lock = cp_parser_expression (parser, false, NULL);
20293 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20295 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20296 node, lest it get absorbed into the surrounding block. */
20297 stmt = push_stmt_list ();
20298 cp_parser_compound_statement (parser, NULL, false);
20300 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20303 /* Parse an Objective-C throw statement.
20306 @throw assignment-expression [opt] ;
20308 Returns a constructed '@throw' statement. */
20311 cp_parser_objc_throw_statement (cp_parser *parser) {
20312 tree expr = NULL_TREE;
20313 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20315 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20318 expr = cp_parser_assignment_expression (parser, false, NULL);
20320 cp_parser_consume_semicolon_at_end_of_statement (parser);
20322 return objc_build_throw_stmt (loc, expr);
20325 /* Parse an Objective-C statement. */
20328 cp_parser_objc_statement (cp_parser * parser) {
20329 /* Try to figure out what kind of declaration is present. */
20330 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20332 switch (kwd->keyword)
20335 return cp_parser_objc_try_catch_finally_statement (parser);
20336 case RID_AT_SYNCHRONIZED:
20337 return cp_parser_objc_synchronized_statement (parser);
20339 return cp_parser_objc_throw_statement (parser);
20341 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20342 &kwd->location, kwd->u.value);
20343 cp_parser_skip_to_end_of_block_or_statement (parser);
20346 return error_mark_node;
20349 /* OpenMP 2.5 parsing routines. */
20351 /* Returns name of the next clause.
20352 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20353 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20354 returned and the token is consumed. */
20356 static pragma_omp_clause
20357 cp_parser_omp_clause_name (cp_parser *parser)
20359 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20361 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20362 result = PRAGMA_OMP_CLAUSE_IF;
20363 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20364 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20365 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20366 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20367 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20369 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20370 const char *p = IDENTIFIER_POINTER (id);
20375 if (!strcmp ("collapse", p))
20376 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20377 else if (!strcmp ("copyin", p))
20378 result = PRAGMA_OMP_CLAUSE_COPYIN;
20379 else if (!strcmp ("copyprivate", p))
20380 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20383 if (!strcmp ("firstprivate", p))
20384 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20387 if (!strcmp ("lastprivate", p))
20388 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20391 if (!strcmp ("nowait", p))
20392 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20393 else if (!strcmp ("num_threads", p))
20394 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20397 if (!strcmp ("ordered", p))
20398 result = PRAGMA_OMP_CLAUSE_ORDERED;
20401 if (!strcmp ("reduction", p))
20402 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20405 if (!strcmp ("schedule", p))
20406 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20407 else if (!strcmp ("shared", p))
20408 result = PRAGMA_OMP_CLAUSE_SHARED;
20411 if (!strcmp ("untied", p))
20412 result = PRAGMA_OMP_CLAUSE_UNTIED;
20417 if (result != PRAGMA_OMP_CLAUSE_NONE)
20418 cp_lexer_consume_token (parser->lexer);
20423 /* Validate that a clause of the given type does not already exist. */
20426 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20427 const char *name, location_t location)
20431 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20432 if (OMP_CLAUSE_CODE (c) == code)
20434 error ("%Htoo many %qs clauses", &location, name);
20442 variable-list , identifier
20444 In addition, we match a closing parenthesis. An opening parenthesis
20445 will have been consumed by the caller.
20447 If KIND is nonzero, create the appropriate node and install the decl
20448 in OMP_CLAUSE_DECL and add the node to the head of the list.
20450 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20451 return the list created. */
20454 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20462 token = cp_lexer_peek_token (parser->lexer);
20463 name = cp_parser_id_expression (parser, /*template_p=*/false,
20464 /*check_dependency_p=*/true,
20465 /*template_p=*/NULL,
20466 /*declarator_p=*/false,
20467 /*optional_p=*/false);
20468 if (name == error_mark_node)
20471 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20472 if (decl == error_mark_node)
20473 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20474 else if (kind != 0)
20476 tree u = build_omp_clause (token->location, kind);
20477 OMP_CLAUSE_DECL (u) = decl;
20478 OMP_CLAUSE_CHAIN (u) = list;
20482 list = tree_cons (decl, NULL_TREE, list);
20485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20487 cp_lexer_consume_token (parser->lexer);
20490 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20494 /* Try to resync to an unnested comma. Copied from
20495 cp_parser_parenthesized_expression_list. */
20497 ending = cp_parser_skip_to_closing_parenthesis (parser,
20498 /*recovering=*/true,
20500 /*consume_paren=*/true);
20508 /* Similarly, but expect leading and trailing parenthesis. This is a very
20509 common case for omp clauses. */
20512 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20514 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20515 return cp_parser_omp_var_list_no_open (parser, kind, list);
20520 collapse ( constant-expression ) */
20523 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20529 loc = cp_lexer_peek_token (parser->lexer)->location;
20530 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20533 num = cp_parser_constant_expression (parser, false, NULL);
20535 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20536 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20537 /*or_comma=*/false,
20538 /*consume_paren=*/true);
20540 if (num == error_mark_node)
20542 num = fold_non_dependent_expr (num);
20543 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20544 || !host_integerp (num, 0)
20545 || (n = tree_low_cst (num, 0)) <= 0
20548 error ("%Hcollapse argument needs positive constant integer expression",
20553 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20554 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
20555 OMP_CLAUSE_CHAIN (c) = list;
20556 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20562 default ( shared | none ) */
20565 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20567 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20570 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20572 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20574 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20575 const char *p = IDENTIFIER_POINTER (id);
20580 if (strcmp ("none", p) != 0)
20582 kind = OMP_CLAUSE_DEFAULT_NONE;
20586 if (strcmp ("shared", p) != 0)
20588 kind = OMP_CLAUSE_DEFAULT_SHARED;
20595 cp_lexer_consume_token (parser->lexer);
20600 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20603 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20604 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20605 /*or_comma=*/false,
20606 /*consume_paren=*/true);
20608 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20611 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20612 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
20613 OMP_CLAUSE_CHAIN (c) = list;
20614 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20620 if ( expression ) */
20623 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20627 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20630 t = cp_parser_condition (parser);
20632 if (t == error_mark_node
20633 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20634 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20635 /*or_comma=*/false,
20636 /*consume_paren=*/true);
20638 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20640 c = build_omp_clause (location, OMP_CLAUSE_IF);
20641 OMP_CLAUSE_IF_EXPR (c) = t;
20642 OMP_CLAUSE_CHAIN (c) = list;
20651 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20652 tree list, location_t location)
20656 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20658 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
20659 OMP_CLAUSE_CHAIN (c) = list;
20664 num_threads ( expression ) */
20667 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20668 location_t location)
20672 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20675 t = cp_parser_expression (parser, false, NULL);
20677 if (t == error_mark_node
20678 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20679 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20680 /*or_comma=*/false,
20681 /*consume_paren=*/true);
20683 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20684 "num_threads", location);
20686 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
20687 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20688 OMP_CLAUSE_CHAIN (c) = list;
20697 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20698 tree list, location_t location)
20702 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20703 "ordered", location);
20705 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
20706 OMP_CLAUSE_CHAIN (c) = list;
20711 reduction ( reduction-operator : variable-list )
20713 reduction-operator:
20714 One of: + * - & ^ | && || */
20717 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20719 enum tree_code code;
20722 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20725 switch (cp_lexer_peek_token (parser->lexer)->type)
20737 code = BIT_AND_EXPR;
20740 code = BIT_XOR_EXPR;
20743 code = BIT_IOR_EXPR;
20746 code = TRUTH_ANDIF_EXPR;
20749 code = TRUTH_ORIF_EXPR;
20752 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20753 "%<|%>, %<&&%>, or %<||%>");
20755 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20756 /*or_comma=*/false,
20757 /*consume_paren=*/true);
20760 cp_lexer_consume_token (parser->lexer);
20762 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20765 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20766 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20767 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20773 schedule ( schedule-kind )
20774 schedule ( schedule-kind , expression )
20777 static | dynamic | guided | runtime | auto */
20780 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20784 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20787 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
20789 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20791 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20792 const char *p = IDENTIFIER_POINTER (id);
20797 if (strcmp ("dynamic", p) != 0)
20799 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20803 if (strcmp ("guided", p) != 0)
20805 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20809 if (strcmp ("runtime", p) != 0)
20811 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20818 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20819 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20820 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20821 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20824 cp_lexer_consume_token (parser->lexer);
20826 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20829 cp_lexer_consume_token (parser->lexer);
20831 token = cp_lexer_peek_token (parser->lexer);
20832 t = cp_parser_assignment_expression (parser, false, NULL);
20834 if (t == error_mark_node)
20836 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20837 error ("%Hschedule %<runtime%> does not take "
20838 "a %<chunk_size%> parameter", &token->location);
20839 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20840 error ("%Hschedule %<auto%> does not take "
20841 "a %<chunk_size%> parameter", &token->location);
20843 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20845 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20848 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20851 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20852 OMP_CLAUSE_CHAIN (c) = list;
20856 cp_parser_error (parser, "invalid schedule kind");
20858 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20859 /*or_comma=*/false,
20860 /*consume_paren=*/true);
20868 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20869 tree list, location_t location)
20873 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20875 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
20876 OMP_CLAUSE_CHAIN (c) = list;
20880 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20881 is a bitmask in MASK. Return the list of clauses found; the result
20882 of clause default goes in *pdefault. */
20885 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20886 const char *where, cp_token *pragma_tok)
20888 tree clauses = NULL;
20890 cp_token *token = NULL;
20892 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20894 pragma_omp_clause c_kind;
20895 const char *c_name;
20896 tree prev = clauses;
20898 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20899 cp_lexer_consume_token (parser->lexer);
20901 token = cp_lexer_peek_token (parser->lexer);
20902 c_kind = cp_parser_omp_clause_name (parser);
20907 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20908 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20910 c_name = "collapse";
20912 case PRAGMA_OMP_CLAUSE_COPYIN:
20913 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20916 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20917 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20919 c_name = "copyprivate";
20921 case PRAGMA_OMP_CLAUSE_DEFAULT:
20922 clauses = cp_parser_omp_clause_default (parser, clauses,
20924 c_name = "default";
20926 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20927 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20929 c_name = "firstprivate";
20931 case PRAGMA_OMP_CLAUSE_IF:
20932 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20935 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20936 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20938 c_name = "lastprivate";
20940 case PRAGMA_OMP_CLAUSE_NOWAIT:
20941 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20944 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20945 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20947 c_name = "num_threads";
20949 case PRAGMA_OMP_CLAUSE_ORDERED:
20950 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20952 c_name = "ordered";
20954 case PRAGMA_OMP_CLAUSE_PRIVATE:
20955 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20957 c_name = "private";
20959 case PRAGMA_OMP_CLAUSE_REDUCTION:
20960 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20961 c_name = "reduction";
20963 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20964 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20966 c_name = "schedule";
20968 case PRAGMA_OMP_CLAUSE_SHARED:
20969 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20973 case PRAGMA_OMP_CLAUSE_UNTIED:
20974 clauses = cp_parser_omp_clause_untied (parser, clauses,
20979 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20983 if (((mask >> c_kind) & 1) == 0)
20985 /* Remove the invalid clause(s) from the list to avoid
20986 confusing the rest of the compiler. */
20988 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20992 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20993 return finish_omp_clauses (clauses);
21000 In practice, we're also interested in adding the statement to an
21001 outer node. So it is convenient if we work around the fact that
21002 cp_parser_statement calls add_stmt. */
21005 cp_parser_begin_omp_structured_block (cp_parser *parser)
21007 unsigned save = parser->in_statement;
21009 /* Only move the values to IN_OMP_BLOCK if they weren't false.
21010 This preserves the "not within loop or switch" style error messages
21011 for nonsense cases like
21017 if (parser->in_statement)
21018 parser->in_statement = IN_OMP_BLOCK;
21024 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21026 parser->in_statement = save;
21030 cp_parser_omp_structured_block (cp_parser *parser)
21032 tree stmt = begin_omp_structured_block ();
21033 unsigned int save = cp_parser_begin_omp_structured_block (parser);
21035 cp_parser_statement (parser, NULL_TREE, false, NULL);
21037 cp_parser_end_omp_structured_block (parser, save);
21038 return finish_omp_structured_block (stmt);
21042 # pragma omp atomic new-line
21046 x binop= expr | x++ | ++x | x-- | --x
21048 +, *, -, /, &, ^, |, <<, >>
21050 where x is an lvalue expression with scalar type. */
21053 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21056 enum tree_code code;
21058 cp_parser_require_pragma_eol (parser, pragma_tok);
21060 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21061 /*cast_p=*/false, NULL);
21062 switch (TREE_CODE (lhs))
21067 case PREINCREMENT_EXPR:
21068 case POSTINCREMENT_EXPR:
21069 lhs = TREE_OPERAND (lhs, 0);
21071 rhs = integer_one_node;
21074 case PREDECREMENT_EXPR:
21075 case POSTDECREMENT_EXPR:
21076 lhs = TREE_OPERAND (lhs, 0);
21078 rhs = integer_one_node;
21082 switch (cp_lexer_peek_token (parser->lexer)->type)
21088 code = TRUNC_DIV_EXPR;
21096 case CPP_LSHIFT_EQ:
21097 code = LSHIFT_EXPR;
21099 case CPP_RSHIFT_EQ:
21100 code = RSHIFT_EXPR;
21103 code = BIT_AND_EXPR;
21106 code = BIT_IOR_EXPR;
21109 code = BIT_XOR_EXPR;
21112 cp_parser_error (parser,
21113 "invalid operator for %<#pragma omp atomic%>");
21116 cp_lexer_consume_token (parser->lexer);
21118 rhs = cp_parser_expression (parser, false, NULL);
21119 if (rhs == error_mark_node)
21123 finish_omp_atomic (code, lhs, rhs);
21124 cp_parser_consume_semicolon_at_end_of_statement (parser);
21128 cp_parser_skip_to_end_of_block_or_statement (parser);
21133 # pragma omp barrier new-line */
21136 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21138 cp_parser_require_pragma_eol (parser, pragma_tok);
21139 finish_omp_barrier ();
21143 # pragma omp critical [(name)] new-line
21144 structured-block */
21147 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21149 tree stmt, name = NULL;
21151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21153 cp_lexer_consume_token (parser->lexer);
21155 name = cp_parser_identifier (parser);
21157 if (name == error_mark_node
21158 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21160 /*or_comma=*/false,
21161 /*consume_paren=*/true);
21162 if (name == error_mark_node)
21165 cp_parser_require_pragma_eol (parser, pragma_tok);
21167 stmt = cp_parser_omp_structured_block (parser);
21168 return c_finish_omp_critical (input_location, stmt, name);
21172 # pragma omp flush flush-vars[opt] new-line
21175 ( variable-list ) */
21178 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21180 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21181 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21182 cp_parser_require_pragma_eol (parser, pragma_tok);
21184 finish_omp_flush ();
21187 /* Helper function, to parse omp for increment expression. */
21190 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21192 tree cond = cp_parser_binary_expression (parser, false, true,
21193 PREC_NOT_OPERATOR, NULL);
21196 if (cond == error_mark_node
21197 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21199 cp_parser_skip_to_end_of_statement (parser);
21200 return error_mark_node;
21203 switch (TREE_CODE (cond))
21211 return error_mark_node;
21214 /* If decl is an iterator, preserve LHS and RHS of the relational
21215 expr until finish_omp_for. */
21217 && (type_dependent_expression_p (decl)
21218 || CLASS_TYPE_P (TREE_TYPE (decl))))
21221 return build_x_binary_op (TREE_CODE (cond),
21222 TREE_OPERAND (cond, 0), ERROR_MARK,
21223 TREE_OPERAND (cond, 1), ERROR_MARK,
21224 &overloaded_p, tf_warning_or_error);
21227 /* Helper function, to parse omp for increment expression. */
21230 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21232 cp_token *token = cp_lexer_peek_token (parser->lexer);
21238 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21240 op = (token->type == CPP_PLUS_PLUS
21241 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21242 cp_lexer_consume_token (parser->lexer);
21243 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21245 return error_mark_node;
21246 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21249 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21251 return error_mark_node;
21253 token = cp_lexer_peek_token (parser->lexer);
21254 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21256 op = (token->type == CPP_PLUS_PLUS
21257 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21258 cp_lexer_consume_token (parser->lexer);
21259 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21262 op = cp_parser_assignment_operator_opt (parser);
21263 if (op == ERROR_MARK)
21264 return error_mark_node;
21266 if (op != NOP_EXPR)
21268 rhs = cp_parser_assignment_expression (parser, false, NULL);
21269 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21270 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21273 lhs = cp_parser_binary_expression (parser, false, false,
21274 PREC_ADDITIVE_EXPRESSION, NULL);
21275 token = cp_lexer_peek_token (parser->lexer);
21276 decl_first = lhs == decl;
21279 if (token->type != CPP_PLUS
21280 && token->type != CPP_MINUS)
21281 return error_mark_node;
21285 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21286 cp_lexer_consume_token (parser->lexer);
21287 rhs = cp_parser_binary_expression (parser, false, false,
21288 PREC_ADDITIVE_EXPRESSION, NULL);
21289 token = cp_lexer_peek_token (parser->lexer);
21290 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21292 if (lhs == NULL_TREE)
21294 if (op == PLUS_EXPR)
21297 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21300 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21301 NULL, tf_warning_or_error);
21304 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21308 if (rhs != decl || op == MINUS_EXPR)
21309 return error_mark_node;
21310 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21313 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21315 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21318 /* Parse the restricted form of the for statement allowed by OpenMP. */
21321 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21323 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21324 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21325 tree this_pre_body, cl;
21326 location_t loc_first;
21327 bool collapse_err = false;
21328 int i, collapse = 1, nbraces = 0;
21330 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21331 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21332 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21334 gcc_assert (collapse >= 1);
21336 declv = make_tree_vec (collapse);
21337 initv = make_tree_vec (collapse);
21338 condv = make_tree_vec (collapse);
21339 incrv = make_tree_vec (collapse);
21341 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21343 for (i = 0; i < collapse; i++)
21345 int bracecount = 0;
21346 bool add_private_clause = false;
21349 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21351 cp_parser_error (parser, "for statement expected");
21354 loc = cp_lexer_consume_token (parser->lexer)->location;
21356 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21359 init = decl = real_decl = NULL;
21360 this_pre_body = push_stmt_list ();
21361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21363 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21367 integer-type var = lb
21368 random-access-iterator-type var = lb
21369 pointer-type var = lb
21371 cp_decl_specifier_seq type_specifiers;
21373 /* First, try to parse as an initialized declaration. See
21374 cp_parser_condition, from whence the bulk of this is copied. */
21376 cp_parser_parse_tentatively (parser);
21377 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21379 if (cp_parser_parse_definitely (parser))
21381 /* If parsing a type specifier seq succeeded, then this
21382 MUST be a initialized declaration. */
21383 tree asm_specification, attributes;
21384 cp_declarator *declarator;
21386 declarator = cp_parser_declarator (parser,
21387 CP_PARSER_DECLARATOR_NAMED,
21388 /*ctor_dtor_or_conv_p=*/NULL,
21389 /*parenthesized_p=*/NULL,
21390 /*member_p=*/false);
21391 attributes = cp_parser_attributes_opt (parser);
21392 asm_specification = cp_parser_asm_specification_opt (parser);
21394 if (declarator == cp_error_declarator)
21395 cp_parser_skip_to_end_of_statement (parser);
21399 tree pushed_scope, auto_node;
21401 decl = start_decl (declarator, &type_specifiers,
21402 SD_INITIALIZED, attributes,
21403 /*prefix_attributes=*/NULL_TREE,
21406 auto_node = type_uses_auto (TREE_TYPE (decl));
21407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21409 if (cp_lexer_next_token_is (parser->lexer,
21411 error ("parenthesized initialization is not allowed in "
21412 "OpenMP %<for%> loop");
21414 /* Trigger an error. */
21415 cp_parser_require (parser, CPP_EQ, "%<=%>");
21417 init = error_mark_node;
21418 cp_parser_skip_to_end_of_statement (parser);
21420 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21421 || type_dependent_expression_p (decl)
21424 bool is_direct_init, is_non_constant_init;
21426 init = cp_parser_initializer (parser,
21428 &is_non_constant_init);
21430 if (auto_node && describable_type (init))
21433 = do_auto_deduction (TREE_TYPE (decl), init,
21436 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21437 && !type_dependent_expression_p (decl))
21441 cp_finish_decl (decl, init, !is_non_constant_init,
21443 LOOKUP_ONLYCONVERTING);
21444 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21447 = tree_cons (NULL, this_pre_body, for_block);
21451 init = pop_stmt_list (this_pre_body);
21452 this_pre_body = NULL_TREE;
21457 cp_lexer_consume_token (parser->lexer);
21458 init = cp_parser_assignment_expression (parser, false, NULL);
21461 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21462 init = error_mark_node;
21464 cp_finish_decl (decl, NULL_TREE,
21465 /*init_const_expr_p=*/false,
21467 LOOKUP_ONLYCONVERTING);
21471 pop_scope (pushed_scope);
21477 /* If parsing a type specifier sequence failed, then
21478 this MUST be a simple expression. */
21479 cp_parser_parse_tentatively (parser);
21480 decl = cp_parser_primary_expression (parser, false, false,
21482 if (!cp_parser_error_occurred (parser)
21485 && CLASS_TYPE_P (TREE_TYPE (decl)))
21489 cp_parser_parse_definitely (parser);
21490 cp_parser_require (parser, CPP_EQ, "%<=%>");
21491 rhs = cp_parser_assignment_expression (parser, false, NULL);
21492 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21494 tf_warning_or_error));
21495 add_private_clause = true;
21500 cp_parser_abort_tentative_parse (parser);
21501 init = cp_parser_expression (parser, false, NULL);
21504 if (TREE_CODE (init) == MODIFY_EXPR
21505 || TREE_CODE (init) == MODOP_EXPR)
21506 real_decl = TREE_OPERAND (init, 0);
21511 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21514 this_pre_body = pop_stmt_list (this_pre_body);
21518 pre_body = push_stmt_list ();
21520 add_stmt (this_pre_body);
21521 pre_body = pop_stmt_list (pre_body);
21524 pre_body = this_pre_body;
21529 if (par_clauses != NULL && real_decl != NULL_TREE)
21532 for (c = par_clauses; *c ; )
21533 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21534 && OMP_CLAUSE_DECL (*c) == real_decl)
21536 error ("%Hiteration variable %qD should not be firstprivate",
21538 *c = OMP_CLAUSE_CHAIN (*c);
21540 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21541 && OMP_CLAUSE_DECL (*c) == real_decl)
21543 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21544 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21545 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
21546 OMP_CLAUSE_DECL (l) = real_decl;
21547 OMP_CLAUSE_CHAIN (l) = clauses;
21548 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21550 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21551 CP_OMP_CLAUSE_INFO (*c) = NULL;
21552 add_private_clause = false;
21556 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21557 && OMP_CLAUSE_DECL (*c) == real_decl)
21558 add_private_clause = false;
21559 c = &OMP_CLAUSE_CHAIN (*c);
21563 if (add_private_clause)
21566 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21568 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21569 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21570 && OMP_CLAUSE_DECL (c) == decl)
21572 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21573 && OMP_CLAUSE_DECL (c) == decl)
21574 error ("%Hiteration variable %qD should not be firstprivate",
21576 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21577 && OMP_CLAUSE_DECL (c) == decl)
21578 error ("%Hiteration variable %qD should not be reduction",
21583 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
21584 OMP_CLAUSE_DECL (c) = decl;
21585 c = finish_omp_clauses (c);
21588 OMP_CLAUSE_CHAIN (c) = clauses;
21595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21596 cond = cp_parser_omp_for_cond (parser, decl);
21597 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21600 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21602 /* If decl is an iterator, preserve the operator on decl
21603 until finish_omp_for. */
21605 && (type_dependent_expression_p (decl)
21606 || CLASS_TYPE_P (TREE_TYPE (decl))))
21607 incr = cp_parser_omp_for_incr (parser, decl);
21609 incr = cp_parser_expression (parser, false, NULL);
21612 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21613 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21614 /*or_comma=*/false,
21615 /*consume_paren=*/true);
21617 TREE_VEC_ELT (declv, i) = decl;
21618 TREE_VEC_ELT (initv, i) = init;
21619 TREE_VEC_ELT (condv, i) = cond;
21620 TREE_VEC_ELT (incrv, i) = incr;
21622 if (i == collapse - 1)
21625 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21626 in between the collapsed for loops to be still considered perfectly
21627 nested. Hopefully the final version clarifies this.
21628 For now handle (multiple) {'s and empty statements. */
21629 cp_parser_parse_tentatively (parser);
21632 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21634 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21636 cp_lexer_consume_token (parser->lexer);
21639 else if (bracecount
21640 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21641 cp_lexer_consume_token (parser->lexer);
21644 loc = cp_lexer_peek_token (parser->lexer)->location;
21645 error ("%Hnot enough collapsed for loops", &loc);
21646 collapse_err = true;
21647 cp_parser_abort_tentative_parse (parser);
21656 cp_parser_parse_definitely (parser);
21657 nbraces += bracecount;
21661 /* Note that we saved the original contents of this flag when we entered
21662 the structured block, and so we don't need to re-save it here. */
21663 parser->in_statement = IN_OMP_FOR;
21665 /* Note that the grammar doesn't call for a structured block here,
21666 though the loop as a whole is a structured block. */
21667 body = push_stmt_list ();
21668 cp_parser_statement (parser, NULL_TREE, false, NULL);
21669 body = pop_stmt_list (body);
21671 if (declv == NULL_TREE)
21674 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21675 pre_body, clauses);
21679 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21681 cp_lexer_consume_token (parser->lexer);
21684 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21685 cp_lexer_consume_token (parser->lexer);
21690 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21691 error ("%Hcollapsed loops not perfectly nested", &loc);
21693 collapse_err = true;
21694 cp_parser_statement_seq_opt (parser, NULL);
21695 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21701 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21702 for_block = TREE_CHAIN (for_block);
21709 #pragma omp for for-clause[optseq] new-line
21712 #define OMP_FOR_CLAUSE_MASK \
21713 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21714 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21715 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21716 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21717 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21718 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21719 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21720 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21723 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21725 tree clauses, sb, ret;
21728 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21729 "#pragma omp for", pragma_tok);
21731 sb = begin_omp_structured_block ();
21732 save = cp_parser_begin_omp_structured_block (parser);
21734 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21736 cp_parser_end_omp_structured_block (parser, save);
21737 add_stmt (finish_omp_structured_block (sb));
21743 # pragma omp master new-line
21744 structured-block */
21747 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21749 cp_parser_require_pragma_eol (parser, pragma_tok);
21750 return c_finish_omp_master (input_location,
21751 cp_parser_omp_structured_block (parser));
21755 # pragma omp ordered new-line
21756 structured-block */
21759 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21761 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21762 cp_parser_require_pragma_eol (parser, pragma_tok);
21763 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
21769 { section-sequence }
21772 section-directive[opt] structured-block
21773 section-sequence section-directive structured-block */
21776 cp_parser_omp_sections_scope (cp_parser *parser)
21778 tree stmt, substmt;
21779 bool error_suppress = false;
21782 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21785 stmt = push_stmt_list ();
21787 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21791 substmt = begin_omp_structured_block ();
21792 save = cp_parser_begin_omp_structured_block (parser);
21796 cp_parser_statement (parser, NULL_TREE, false, NULL);
21798 tok = cp_lexer_peek_token (parser->lexer);
21799 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21801 if (tok->type == CPP_CLOSE_BRACE)
21803 if (tok->type == CPP_EOF)
21807 cp_parser_end_omp_structured_block (parser, save);
21808 substmt = finish_omp_structured_block (substmt);
21809 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21810 add_stmt (substmt);
21815 tok = cp_lexer_peek_token (parser->lexer);
21816 if (tok->type == CPP_CLOSE_BRACE)
21818 if (tok->type == CPP_EOF)
21821 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21823 cp_lexer_consume_token (parser->lexer);
21824 cp_parser_require_pragma_eol (parser, tok);
21825 error_suppress = false;
21827 else if (!error_suppress)
21829 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21830 error_suppress = true;
21833 substmt = cp_parser_omp_structured_block (parser);
21834 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21835 add_stmt (substmt);
21837 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21839 substmt = pop_stmt_list (stmt);
21841 stmt = make_node (OMP_SECTIONS);
21842 TREE_TYPE (stmt) = void_type_node;
21843 OMP_SECTIONS_BODY (stmt) = substmt;
21850 # pragma omp sections sections-clause[optseq] newline
21853 #define OMP_SECTIONS_CLAUSE_MASK \
21854 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21855 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21856 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21857 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21858 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21861 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21865 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21866 "#pragma omp sections", pragma_tok);
21868 ret = cp_parser_omp_sections_scope (parser);
21870 OMP_SECTIONS_CLAUSES (ret) = clauses;
21876 # pragma parallel parallel-clause new-line
21877 # pragma parallel for parallel-for-clause new-line
21878 # pragma parallel sections parallel-sections-clause new-line */
21880 #define OMP_PARALLEL_CLAUSE_MASK \
21881 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21882 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21883 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21884 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21885 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21886 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21887 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21888 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21891 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21893 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21894 const char *p_name = "#pragma omp parallel";
21895 tree stmt, clauses, par_clause, ws_clause, block;
21896 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21898 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21900 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21902 cp_lexer_consume_token (parser->lexer);
21903 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21904 p_name = "#pragma omp parallel for";
21905 mask |= OMP_FOR_CLAUSE_MASK;
21906 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21908 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21910 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21911 const char *p = IDENTIFIER_POINTER (id);
21912 if (strcmp (p, "sections") == 0)
21914 cp_lexer_consume_token (parser->lexer);
21915 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21916 p_name = "#pragma omp parallel sections";
21917 mask |= OMP_SECTIONS_CLAUSE_MASK;
21918 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21922 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21923 block = begin_omp_parallel ();
21924 save = cp_parser_begin_omp_structured_block (parser);
21928 case PRAGMA_OMP_PARALLEL:
21929 cp_parser_statement (parser, NULL_TREE, false, NULL);
21930 par_clause = clauses;
21933 case PRAGMA_OMP_PARALLEL_FOR:
21934 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21935 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21938 case PRAGMA_OMP_PARALLEL_SECTIONS:
21939 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21940 stmt = cp_parser_omp_sections_scope (parser);
21942 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21946 gcc_unreachable ();
21949 cp_parser_end_omp_structured_block (parser, save);
21950 stmt = finish_omp_parallel (par_clause, block);
21951 if (p_kind != PRAGMA_OMP_PARALLEL)
21952 OMP_PARALLEL_COMBINED (stmt) = 1;
21957 # pragma omp single single-clause[optseq] new-line
21958 structured-block */
21960 #define OMP_SINGLE_CLAUSE_MASK \
21961 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21962 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21963 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21964 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21967 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21969 tree stmt = make_node (OMP_SINGLE);
21970 TREE_TYPE (stmt) = void_type_node;
21972 OMP_SINGLE_CLAUSES (stmt)
21973 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21974 "#pragma omp single", pragma_tok);
21975 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21977 return add_stmt (stmt);
21981 # pragma omp task task-clause[optseq] new-line
21982 structured-block */
21984 #define OMP_TASK_CLAUSE_MASK \
21985 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21986 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21987 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21988 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21989 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21990 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21993 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21995 tree clauses, block;
21998 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21999 "#pragma omp task", pragma_tok);
22000 block = begin_omp_task ();
22001 save = cp_parser_begin_omp_structured_block (parser);
22002 cp_parser_statement (parser, NULL_TREE, false, NULL);
22003 cp_parser_end_omp_structured_block (parser, save);
22004 return finish_omp_task (clauses, block);
22008 # pragma omp taskwait new-line */
22011 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22013 cp_parser_require_pragma_eol (parser, pragma_tok);
22014 finish_omp_taskwait ();
22018 # pragma omp threadprivate (variable-list) */
22021 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22025 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22026 cp_parser_require_pragma_eol (parser, pragma_tok);
22028 finish_omp_threadprivate (vars);
22031 /* Main entry point to OpenMP statement pragmas. */
22034 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22038 switch (pragma_tok->pragma_kind)
22040 case PRAGMA_OMP_ATOMIC:
22041 cp_parser_omp_atomic (parser, pragma_tok);
22043 case PRAGMA_OMP_CRITICAL:
22044 stmt = cp_parser_omp_critical (parser, pragma_tok);
22046 case PRAGMA_OMP_FOR:
22047 stmt = cp_parser_omp_for (parser, pragma_tok);
22049 case PRAGMA_OMP_MASTER:
22050 stmt = cp_parser_omp_master (parser, pragma_tok);
22052 case PRAGMA_OMP_ORDERED:
22053 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22055 case PRAGMA_OMP_PARALLEL:
22056 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22058 case PRAGMA_OMP_SECTIONS:
22059 stmt = cp_parser_omp_sections (parser, pragma_tok);
22061 case PRAGMA_OMP_SINGLE:
22062 stmt = cp_parser_omp_single (parser, pragma_tok);
22064 case PRAGMA_OMP_TASK:
22065 stmt = cp_parser_omp_task (parser, pragma_tok);
22068 gcc_unreachable ();
22072 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22077 static GTY (()) cp_parser *the_parser;
22080 /* Special handling for the first token or line in the file. The first
22081 thing in the file might be #pragma GCC pch_preprocess, which loads a
22082 PCH file, which is a GC collection point. So we need to handle this
22083 first pragma without benefit of an existing lexer structure.
22085 Always returns one token to the caller in *FIRST_TOKEN. This is
22086 either the true first token of the file, or the first token after
22087 the initial pragma. */
22090 cp_parser_initial_pragma (cp_token *first_token)
22094 cp_lexer_get_preprocessor_token (NULL, first_token);
22095 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22098 cp_lexer_get_preprocessor_token (NULL, first_token);
22099 if (first_token->type == CPP_STRING)
22101 name = first_token->u.value;
22103 cp_lexer_get_preprocessor_token (NULL, first_token);
22104 if (first_token->type != CPP_PRAGMA_EOL)
22105 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22106 &first_token->location);
22109 error ("%Hexpected string literal", &first_token->location);
22111 /* Skip to the end of the pragma. */
22112 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22113 cp_lexer_get_preprocessor_token (NULL, first_token);
22115 /* Now actually load the PCH file. */
22117 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22119 /* Read one more token to return to our caller. We have to do this
22120 after reading the PCH file in, since its pointers have to be
22122 cp_lexer_get_preprocessor_token (NULL, first_token);
22125 /* Normal parsing of a pragma token. Here we can (and must) use the
22129 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22131 cp_token *pragma_tok;
22134 pragma_tok = cp_lexer_consume_token (parser->lexer);
22135 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22136 parser->lexer->in_pragma = true;
22138 id = pragma_tok->pragma_kind;
22141 case PRAGMA_GCC_PCH_PREPROCESS:
22142 error ("%H%<#pragma GCC pch_preprocess%> must be first",
22143 &pragma_tok->location);
22146 case PRAGMA_OMP_BARRIER:
22149 case pragma_compound:
22150 cp_parser_omp_barrier (parser, pragma_tok);
22153 error ("%H%<#pragma omp barrier%> may only be "
22154 "used in compound statements", &pragma_tok->location);
22161 case PRAGMA_OMP_FLUSH:
22164 case pragma_compound:
22165 cp_parser_omp_flush (parser, pragma_tok);
22168 error ("%H%<#pragma omp flush%> may only be "
22169 "used in compound statements", &pragma_tok->location);
22176 case PRAGMA_OMP_TASKWAIT:
22179 case pragma_compound:
22180 cp_parser_omp_taskwait (parser, pragma_tok);
22183 error ("%H%<#pragma omp taskwait%> may only be "
22184 "used in compound statements",
22185 &pragma_tok->location);
22192 case PRAGMA_OMP_THREADPRIVATE:
22193 cp_parser_omp_threadprivate (parser, pragma_tok);
22196 case PRAGMA_OMP_ATOMIC:
22197 case PRAGMA_OMP_CRITICAL:
22198 case PRAGMA_OMP_FOR:
22199 case PRAGMA_OMP_MASTER:
22200 case PRAGMA_OMP_ORDERED:
22201 case PRAGMA_OMP_PARALLEL:
22202 case PRAGMA_OMP_SECTIONS:
22203 case PRAGMA_OMP_SINGLE:
22204 case PRAGMA_OMP_TASK:
22205 if (context == pragma_external)
22207 cp_parser_omp_construct (parser, pragma_tok);
22210 case PRAGMA_OMP_SECTION:
22211 error ("%H%<#pragma omp section%> may only be used in "
22212 "%<#pragma omp sections%> construct", &pragma_tok->location);
22216 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22217 c_invoke_pragma_handler (id);
22221 cp_parser_error (parser, "expected declaration specifiers");
22225 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22229 /* The interface the pragma parsers have to the lexer. */
22232 pragma_lex (tree *value)
22235 enum cpp_ttype ret;
22237 tok = cp_lexer_peek_token (the_parser->lexer);
22240 *value = tok->u.value;
22242 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22244 else if (ret == CPP_STRING)
22245 *value = cp_parser_string_literal (the_parser, false, false);
22248 cp_lexer_consume_token (the_parser->lexer);
22249 if (ret == CPP_KEYWORD)
22257 /* External interface. */
22259 /* Parse one entire translation unit. */
22262 c_parse_file (void)
22264 bool error_occurred;
22265 static bool already_called = false;
22267 if (already_called)
22269 sorry ("inter-module optimizations not implemented for C++");
22272 already_called = true;
22274 the_parser = cp_parser_new ();
22275 push_deferring_access_checks (flag_access_control
22276 ? dk_no_deferred : dk_no_check);
22277 error_occurred = cp_parser_translation_unit (the_parser);
22281 #include "gt-cp-parser.h"